Skip to content

Instantly share code, notes, and snippets.

@lordcodes
Last active January 11, 2024 03:54
Show Gist options
  • Select an option

  • Save lordcodes/15b2a4aecbeff7c3238a70bfd20f0931 to your computer and use it in GitHub Desktop.

Select an option

Save lordcodes/15b2a4aecbeff7c3238a70bfd20f0931 to your computer and use it in GitHub Desktop.
Gradle function to get the current Git branch
def getCurrentGitBranch() {
def gitBranch = "Unknown branch"
try {
def workingDir = new File("${project.projectDir}")
def result = 'git rev-parse --abbrev-ref HEAD'.execute(null, workingDir)
result.waitFor()
if (result.exitValue() == 0) {
gitBranch = result.text.trim()
}
} catch (e) {
}
return gitBranch
}
@rygelouv

Copy link
Copy Markdown

Thanks! πŸ™

@lordcodes

Copy link
Copy Markdown
Author

Thanks! πŸ™

You're welcome!

@DovidBaars

Copy link
Copy Markdown

This returns head when built in Appcenter because the head is detached, do you know of another method that will work in appcenter?

@lordcodes

lordcodes commented Dec 22, 2020 •

Copy link
Copy Markdown
Author

This returns head when built in Appcenter because the head is detached, do you know of another method that will work in appcenter?

@DovidBaars If the current 'HEAD' is detached then surely there isn't a current branch. You would only have a current branch if you checked out a branch surely?

@alfdev

alfdev commented May 27, 2021

Copy link
Copy Markdown

Thanks a lot!

@ilya3a

ilya3a commented Oct 6, 2021

Copy link
Copy Markdown

in my project I have few modules and they are sit on different repositories is it possible to get git branch of each module?

@TOPWN

TOPWN commented Oct 9, 2022

Copy link
Copy Markdown

Thanks! πŸ™

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment