1

I do already have some bash code that is detecting the current git branch name and I want to port this to Makefile.

if [ -z ${GIT_BRANCH+x} ]; then
   if [ ! -z ${BRANCH_NAME+x} ]; then
       export GIT_BRANCH=${BRANCH_NAME}
   else
       export GIT_BRANCH=$(git branch | sed -n '/\* /s///p')
   fi
fi
echo "INFO:>Branch:>$GIT_BRANCH"

In case someone wonders why this more complex logic is needed in order to detect git branch name, that's because this code is executed in multiple contexts and in some of them (continuous integration systems like Jenkins and Travis that may use sparse checkouts) you cannot use git branch command. Luckily these environments do expose the branch names in an environment variable.

sorin
  • 161,544
  • 178
  • 535
  • 806
  • If the Makefile is part of the repository, it might be simpler to just edit branch-specific code directly into the Makefile. – chepner Jul 24 '16 at 13:39
  • 3
    It's not clear what you mean by "port this to Makefile". Do you mean, you want to rewrite the above shell code as makefile code? Or do you mean, you want to invoke a script containing the above code from within a makefile? Or something else? – MadScientist Jul 24 '16 at 15:05
  • http://stackoverflow.com/questions/589276/how-can-i-use-bash-syntax-in-makefile-targets?rq=1 – xxfelixxx Jul 25 '16 at 02:50

2 Answers2

0

Take the code you have, and put it in, say, /usr/local/bin/git-branch-report. Then, in your makefile you can access it thus:

GITVER = $(shell /usr/local/bin/git-branch-report)

That's GNU make. BSD make has a similar feature with different syntax.

James K. Lowden
  • 7,574
  • 1
  • 16
  • 31
0

The subject is old, but if it helps :

You can do this:

GIT_CURRENT_BRANCH_NAME = $(shell git branch --show-current)
doydoy44
  • 5,720
  • 4
  • 29
  • 45