Gradle to determine parent branch

If you want to determine what is the parent branch of the current one, you may use this little snippet:

// Get the line describing the nearest merge point
def parentBranchLine = 'git show-branch -a'.execute()
  .text.readLines().find {
      it.contains('*') && !(it ==~ ".*\\[$ext.getBranch()[~^\\]].*")
// Extract the branch name
def parent = (parentBranchLine =~ /\[([^~^\]]*)[~^\]]/) [0][1]

You may wonder why someone could ever need it. Our use case is very simple - we use feature branches off larger story branches. Some of the build scripts need to know what is that larger story the change belongs to.