Reputation: 5846
If one branch have commits A-B-C and another branch have commits A-C'-B', is there some way to make a hash of the patches of B and B'? I want to (script a) compare of 2 branches, and report the differences, so in the case above I want it to notice they are the same. If I use log between C and B' will show 2 commits, even if they are the same commits in another order.
Tested to do some diffs betweens git show
, but as the line numbers differs, and I don't know how to get them to be equal.
Here is a script to set up a git test-case:
git init
seq 1 50 > a
git add a
git commit -m "fill a"
git checkout -b top_bottom
sed -i '1 a\
Add this line high up in the file' a
git add a
git commit -m "top of a"
sed -i '$ a\
Add this line at the bottom of the file' a
git add a
git commit -m "bottom of a"
git checkout master
git checkout -b bottom_top
git cherry-pick top_bottom
git cherry-pick top_bottom~
git checkout master
Doing a compare like diff <(git show --format=raw top_bottom) <(git show --format=raw bottom_top~1)
show line-number diffs in the patch part (and expected hashes differences in the header)
Upvotes: 2
Views: 381
Reputation: 488183
You want git patch-id
, which is designed for exactly this purpose.
I had to modify your setup script slightly for FreeBSD sed
(change sed -i '1a\
to sed -i -e '1a\
) but having done so:
$ git show bottom_top | git patch-id --stable
3a28b42e4aae9c248c00c63451756cbd881cf046 2c33ad0a41076e5a47ec780e8ab6c6b8450db582
$ git show top_bottom~1 | git patch-id --stable
3a28b42e4aae9c248c00c63451756cbd881cf046 7cd871013b7bbfc69f03315d43285a2b78b81a86
The first number is the patch ID (you may use either --stable
or --unstable
; read the documentation to decide which to use) and the second is the commit hash. Note that top_bottom
produces a different patch ID (from, of course, a different commit):
$ git show top_bottom | git patch-id --stable
69febc37e58b8aeb210b3e1e71fa59a11c369a74 9a34efdfe098f6b690199a124cc3ce34f48002b8
Upvotes: 5
Reputation: 51850
To compare patches, I would rather use git diff
:
git diff B^ B > B.patch
Taking back your example :
# name B1 and B2 the 2 commits you want to inspect :
git tag B1 top_bottom^
git tag B2 bottom_top
Get the pacthes :
# first patch :
git diff B1^ B1 > B1.patch
# second patch :
git diff B2^ B2 > B2.patch
The blunt comparison will still show the differences in line numbers :
diff B1.patch B2.patch
An approximation would be : drop the lines mentionning line numbers, and see if the simple "added / removed" blocks match :
cat B1.patch |
grep -v "^index" | # remove lines which mention blob or tree hashes
grep -v "^@@" # remove lines which mention line numbers
> B1.trimmed
# same with B2
diff B1.trimmed B2.trimmed
Upvotes: 2