diff options
author | Alberto Cortés <alberto@sourced.tech> | 2017-03-01 18:09:18 +0100 |
---|---|---|
committer | Alberto Cortés <alberto@sourced.tech> | 2017-03-03 16:19:20 +0100 |
commit | b3aa41afcca829cfb3e2e71be052078152497b3c (patch) | |
tree | ee25a6cd5a310a2b50a7dcb74fe4275252c8298e /plumbing/object/difftree.go | |
parent | 047a795df6d5a0d5dd0782297cea918e4a4a6e10 (diff) | |
download | go-git-b3aa41afcca829cfb3e2e71be052078152497b3c.tar.gz |
difftree: simplify hash comparison with deprecated files modes
Difftree hash comparisson was quite complex because the hashes of
deprecated files were diferent from the hashes of regular files. But
git's difftree really treat them as equal.
This patch fix this by making treenoder return the same hash for regular
files than for deprecated files; now the hash comparison function is
just a bytes.Equal call.
Diffstat (limited to 'plumbing/object/difftree.go')
-rw-r--r-- | plumbing/object/difftree.go | 41 |
1 files changed, 4 insertions, 37 deletions
diff --git a/plumbing/object/difftree.go b/plumbing/object/difftree.go index bcc78ad..3bc29cb 100644 --- a/plumbing/object/difftree.go +++ b/plumbing/object/difftree.go @@ -3,7 +3,6 @@ package object import ( "bytes" - "srcd.works/go-git.v4/plumbing/filemode" "srcd.works/go-git.v4/utils/merkletrie" "srcd.works/go-git.v4/utils/merkletrie/noder" ) @@ -14,6 +13,10 @@ func DiffTree(a, b *Tree) (Changes, error) { from := newTreeNoder(a) to := newTreeNoder(b) + hashEqual := func(a, b noder.Hasher) bool { + return bytes.Equal(a.Hash(), b.Hash()) + } + merkletrieChanges, err := merkletrie.DiffTree(from, to, hashEqual) if err != nil { return nil, err @@ -21,39 +24,3 @@ func DiffTree(a, b *Tree) (Changes, error) { return newChanges(merkletrieChanges) } - -// check if the hash of the contents is different, if not, check if -// the permissions are different (but taking into account deprecated -// file modes). On a treenoder, the hash of the contents is codified -// in the first 20 bytes of the data returned by Hash() and the last -// 4 bytes is the mode. -func hashEqual(a, b noder.Hasher) bool { - hashA, hashB := a.Hash(), b.Hash() - contentsA, contentsB := hashA[:20], hashB[:20] - - sameContents := bytes.Equal(contentsA, contentsB) - if !sameContents { - return false - } - - modeA, modeB := hashA[20:], hashB[20:] - - return equivalentMode(modeA, modeB) -} - -func equivalentMode(a, b []byte) bool { - if isFilish(a) && isFilish(b) { - return true - } - return bytes.Equal(a, b) -} - -var ( - file = filemode.Regular.Bytes() - fileDeprecated = filemode.Deprecated.Bytes() -) - -func isFilish(b []byte) bool { - return bytes.Equal(b, file) || - bytes.Equal(b, fileDeprecated) -} |