How to get branch names in a custom git merge file?

I am writing a custom merge driver that needs to know the names of the branches it merges. I managed to get the name of the branch that merges at (destination) with git symbolic-ref HEAD and the name of the branch that merges at (source) from the GITHEAD_<SHA> environment variable.

 # retrieve merged branch name from an env var GITHEAD_<sha>=<branchName> # we cannot use a sym ref of MERGE_HEAD, as it doesn't yet exist gitHead=$(env | grep GITHEAD) # eg GITHEAD_<sha>=release/1.43 # cut out everything up to the last "=" sign source="${gitHead##*=}" # retrieve base branch name from a sym ref of HEAD branch=$(git symbolic-ref HEAD) # eg refs/heads/master # cut out "refs/heads" destination="${branch#refs/heads/}" echo "Merging from $source into $destination" 

Is this the right way to do this? In particular, getting the original name from an environment variable seems like a screen. Please note that MERGE_HEAD is currently missing, so I cannot use the same approach as with HEAD.

+6
source share
1 answer

Yes, this is as close as possible to the merge driver.

The merge strategy gets the correct name by looking at each GITHEAD_%s , where each argument %s populated from the argument (s) specified by the merge strategy, one per โ€œremote headโ€. For more details, see my answer to the corresponding question . It is not documented anywhere, it is just from the source . Unfortunately, by the time you switch to the merge driver, this information is lost : there are no % directives that extract these names, although there may be. (Adding some % directives should be simple: just increase the size of the dictionary and add the appropriate directives and strbuf objects.)

If you are working with a regular merge with two heads and the base, there is only one variable GITHEAD_* , and the other is the other, but if you are working with a merge octopus, you are out of luck.

0
source

Source: https://habr.com/ru/post/971039/


All Articles