import org.ajoberstar.grgit.Grgit buildscript { repositories { mavenCentral() } dependencies { classpath 'org.ajoberstar.grgit:grgit-core:5.0.0' } } def getGrgit() { return Grgit.open(currentDir: project.rootDir) } def getCurrentBranch() { try { def grgit = getGrgit() def currentBranch = grgit.branch.getCurrent().name grgit.close() return currentBranch } catch (Exception ignored) { return "" } } def getCommitHash() { try { def grgit = getGrgit() def commitHash = grgit.head().id grgit.close() return commitHash } catch (Exception ignored) { return "" } } //Will return true if there are no uncommitted changes def isClean() { try { def grgit = getGrgit() def isClean = grgit.status().isClean() grgit.close() return isClean } catch (Exception ignored) { return true } } ext { getCurrentBranch = this.&getCurrentBranch getCommitHash = this.&getCommitHash isClean = this.&isClean }