Get source branch name in commit-msg hook of merge commit

In the commit-msg declaration for compiling the merge, how can I get the branch name of the immediate origin branch to be merged? I can get the target branch with something like

git rev-parse --abbrev-ref HEAD

      

But HEAD and HEAD ^ both refer to the target branch. Is there a way to programmatically determine the source? I am using a bash script to connect.

The default merge message contains this text:

Merge source branch into target branch

This way, the commit-msg can get the name directly from the message, but there is no guarantee that the user has not edited it. If there is a more reliable method, I would prefer this.

Edit: ah, unfortunately I also didn't understand that commit-msg doesn't even run on compile! So I'll have to find another way to do what I want anyway.

+3


source to share


2 answers


This will merge the commit:

git rev-parse MERGE_HEAD

      

I don't think there is a way to find the branch name other than guessing with a command like:

git for-each-ref | grep ^$(git rev-parse MERGE_HEAD)

      

(which finds all branches pointing to the command you are merging)



Note that the merge to be merged does not have to be a branch, it is also possible to merge the commit directly like git merge deadbeef

.

In the case of an octopus merger, more than one merger merges at the same time, but MERGE_HEAD

not.

If you want to extract it from the merge message, then using it .git/MERGE_MSG

is safer than .git/COMMIT_EDITMSG

that, as it is less believable for manual editing.

The message is generated git merge

hence has access to the branch name from the arguments git merge

, but that doesn't seem to be saved to disk.

+2


source


but there is no guarantee that the user has not edited it

imvho you should use the merge commit pivot line after the user has been able to edit it.

I edited the subject lines for a good reason. All branch names are repo-local. Sometimes you go out of a coworker, sometimes you realize that you typed up while creating a branch, or you post out of a wipe that worked well, there are many more ways to get there.



If you are worried that incoming commits are not up to your standards for one of your own repos, check that incoming transactions are prefetching it. No dvcs can be sure without doing it anyway.

#!/bin/sh
rc=0
existing=$(git for-each-ref --format='%(object)' refs/heads refs/tags);
validmergesubject='Merge (branch|tag) '\''[^ ]*'\'' (of|into) .*'
while read old new ref; do
    while read commit Subject; do
        if [[ ! $Subject =~ $validmergesubject ]]; then
            echo Merge $commit in $ref history has invalid summary line \"$Subject\"
            rc=1;
        fi;
    done  >&2 <<EOD
$(git log --merges --pretty='%H %s' $new --not $existing)
EOD
done
exit $rc

      

+1


source







All Articles