git finding duplicate commits (by patch-id)

后端 未结 7 1790
天命终不由人
天命终不由人 2020-12-16 02:05

I\'d like a recipe for finding duplicated changes. patch-id is likely to be the same but the commit attributes may not be.

This seems to be an intended use of patch-

7条回答
  •  独厮守ぢ
    2020-12-16 02:32

    For looking for duplicates of a specific commit, this may work for you.

    First, determine the patch id of the target commit:

    $ THE_COMMIT_REF_OR_SHA_YOURE_SEEKING_DUPES_OF='7a3e67c'
    $ git show $THE_COMMIT_REF_OR_SHA_YOURE_SEEKING_DUPES_OF | git patch-id
    f6ea51cd6acd30cd627ce1a56e2733c1777d5b52 7a3e67ce38dbef471889d9f706b9161da7dc5cf3
    

    The first SHA is the patch-id. Next, list the patch ids for every commit and filter out any that match:

    $ for c in $(git rev-list --all); do git show $c | git patch-id; done | grep 'f6ea51cd6acd30cd627ce1a56e2733c1777d5b52'
    f6ea51cd6acd30cd627ce1a56e2733c1777d5b52 5028e2b5500bd5f4637531e337e17b73f5d0c0b1
    f6ea51cd6acd30cd627ce1a56e2733c1777d5b52 7a3e67ce38dbef471889d9f706b9161da7dc5cf3
    f6ea51cd6acd30cd627ce1a56e2733c1777d5b52 929c66b5783a0127a7689020d70d398f095b9e00
    

    All together, with a few extra flags, and in the form of a utility script:

    test ! -z "$1" && TARGET_COMMIT_SHA="$1" || TARGET_COMMIT_SHA="HEAD"
    
    TARGET_COMMIT_PATCHID=$(
    git show --patch-with-raw "$TARGET_COMMIT_SHA" |
        git patch-id |
        cut -d' ' -f1
    )
    MATCHING_COMMIT_SHAS=$(
    for c in $(git rev-list --all); do
        git show --patch-with-raw "$c" |
            git patch-id
    done |
        fgrep "$TARGET_COMMIT_PATCHID" |
        cut -d' ' -f2
    )
    
    echo "$MATCHING_COMMIT_SHAS"
    

    Usage:

    $ git list-dupe-commits 7a3e67c
    5028e2b5500bd5f4637531e337e17b73f5d0c0b1
    7a3e67ce38dbef471889d9f706b9161da7dc5cf3
    929c66b5783a0127a7689020d70d398f095b9e00
    

    It isn't terribly speedy, but for most repos should get the job done (just measured 36 seconds for a repo with 826 commits and a 158MB .git dir on a 2.4GHz Core 2 Duo).

提交回复
热议问题