This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Use `git symbolic-ref` instead of `git branch`
authorBram <perl-rt@wizbit.be>
Sat, 17 Sep 2022 18:55:17 +0000 (20:55 +0200)
committerTony Cook <tony@develop-help.com>
Mon, 19 Sep 2022 05:03:34 +0000 (15:03 +1000)
commit95aca9b049c0a768ab80bef52b8162b498735588
tree4bc08950c9c6c3b9ad9ce92f4596b3b37bbc58be
parent94e822ee24ba222efc1979d79e64ca7b2c0942cb
Use `git symbolic-ref` instead of `git branch`

`git branch` is a porcelain command and should not be used in scripts.[^1]

It can also cause failures when:
- color.ui in git config is set to 'always'
- HEAD is detached (i.e. `git checkout blead^0`)

In that case the current branch is colored and (partially) ends up in
the `$branch` var. (The code did check that the branch name didn't start
with `(` but that check doesn't really work when it's colored. In that case
the 'branch name' starts with an escape sequence for the color)

Example (without the patch):

$ git config color.ui always
$ git checkout blead^0
$ ./miniperl -Ilib make_patchnum.pl
sh: -c: line 0: syntax error near unexpected token `('
sh: -c: line 0: `git config branch.(detached.merge'
sh: -c: line 0: syntax error near unexpected token `('
sh: -c: line 0: `git config branch.(detached.remote'
$ grep git_branch lib/Config_git.pl
git_branch='(detached'
$ grep git_branch lib/Config_git.pl | cat -A
git_branch='^[[32m(detached'$

With the patch applied:

$ ./miniperl -Ilib make_patchnum.pl
Updating 'git_version.h' and 'lib/Config_git.pl'
$ grep git_branch lib/Config_git.pl
git_branch=''
$ grep git_branch lib/Config_git.pl | cat -A
git_branch=''$

[^1]: See https://git-blame.blogspot.com/2013/06/checking-current-branch-programatically.html
      which is a blog post from the git maintainer.
make_patchnum.pl