Telling if a Git commit is a Merge/Revert commit

核能气质少年 提交于 2019-11-27 17:08:01

问题


I am writing a script that requires checking whether a particular commit is a Merge/Revert commit or not, and I am wondering if there is a git trick for that.

What I came up with so far (and I definitely don't want to depend on the commit message here) is to check HASH^2 and see if I don't get an error, is there a better way?


回答1:


Figuring out if something is a merge is easy. That's all commits with more than one parent. To check for that, you can do, for example

$ git cat-file -p $commit_id

If there's more than one `parent' line in the output, you found a merge.

For reverts it's not as easy. Generally reverts are just normal commits that happen to apply the diff of a previous commit in reverse, effectively removing the changes that commit introduced. There're not special otherwise.

If a revert was created with git revert $commit, then git usually generates a commit message indication the revert and what commit it reverted. However, it's quite possible to do reverts in other ways, or to just change the commit message of a commit generated by git revert.

Looking for those generated revert commit message might already be a good enough heuristic for what you're trying to achieve. If not, you'd have to actually look through other commits, comparing their diffs against each other, looking of one is the exact reverse operation of another. But even that isn't a good solution. Often enough reverts are slightly different than just the reverse of the commit they're reverting, for example to accomodate for code changes that happened between the commit and the revert.




回答2:


The following instruction will dump out only the parent hashes. Less filtering needed...

git show --no-patch --format="%P" <commit hash>




回答3:


The answer using git cat-file is using a git "plumbing" command, which is generally better for building scripts as the output format is not likely to change. The ones using git show and git rev-parse may need to change over time as they are using porcelain commands.

The bash function I've been using for a long time uses git rev-list:

gitismerge () {
    local sha="$1"
    msha=$(git rev-list -1 --merges ${sha}~1..${sha})
    [ -z "$msha" ] && return 1
    return 0
}

The list of porcelain/plumbing commands can be found in the docs for the top level git command.

This code uses git-rev-list with a specific gitrevisions query ${sha}~1..${sha} in a way that prints a SHA's second parent if it exists, or nothing if it is not present, which is the exact definition of a merge commit.

Specifically, SHA~1..SHA means include commits that are reachable from SHA but exclude those that are reachable SHA~1, which is the first parent of SHA.

The results are stored in $msha and tested for emptiness using bash [ -z "$msha" ] failing (returning 1) if empty, or passing (returning 0) if non-empty.




回答4:


One way to test for a merge commit:

$ test -z $(git rev-parse --verify $commit^2 2> /dev/null) || echo MERGE COMMIT

As for git revert commits, I agree with @rafl that the most realistic approach is to look for the revert message boilerplate in the commit message; if someone changed it, detecting so would be very involved.




回答5:


Easy way to test for merge commit:

git show --summary HEAD | grep -q ^Merge:

This will return 0 for merge commits, 1 for non-merge commits. Replace HEAD by your desired commit to test.

Example usage:

if git show --summary some-branch | grep -q ^Merge: ; then
    echo "some-branch is a merge"
fi



回答6:


Yet another way to find a commit's parents:

git show -s --pretty=%p <commit>

Use %P for full hash. This prints how many parents HEAD has:

git show -s --pretty=%p HEAD | wc -w


来源:https://stackoverflow.com/questions/3824050/telling-if-a-git-commit-is-a-merge-revert-commit

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!