diff options
author | Máximo Cuadros <mcuadros@gmail.com> | 2017-06-19 12:24:28 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-06-19 12:24:28 +0200 |
commit | 8b17cf05989402408ee1b66953a12ebfdf700aac (patch) | |
tree | 512411e012ef8ff2cf1a51e8785d9edb3b4c2332 /utils/merkletrie/filesystem | |
parent | 3ae5d4de35e76f2f573b550d93bb2aed8137f1cb (diff) | |
parent | ada10c2978a6c7dea8109f2d92dd092f57f9de84 (diff) | |
download | go-git-8b17cf05989402408ee1b66953a12ebfdf700aac.tar.gz |
Merge pull request #436 from mcuadros/symlink
worktree: symlink support
Diffstat (limited to 'utils/merkletrie/filesystem')
-rw-r--r-- | utils/merkletrie/filesystem/node.go | 41 | ||||
-rw-r--r-- | utils/merkletrie/filesystem/node_test.go | 19 |
2 files changed, 54 insertions, 6 deletions
diff --git a/utils/merkletrie/filesystem/node.go b/utils/merkletrie/filesystem/node.go index 5461bd3..a8f3b86 100644 --- a/utils/merkletrie/filesystem/node.go +++ b/utils/merkletrie/filesystem/node.go @@ -136,25 +136,54 @@ func (n *node) calculateHash(path string, file os.FileInfo) ([]byte, error) { return make([]byte, 24), nil } - f, err := n.fs.Open(path) + var hash plumbing.Hash + var err error + if file.Mode()&os.ModeSymlink != 0 { + hash, err = n.doCalculateHashForSymlink(path, file) + } else { + hash, err = n.doCalculateHashForRegular(path, file) + } + + if err != nil { + return nil, err + } + + mode, err := filemode.NewFromOSFileMode(file.Mode()) if err != nil { return nil, err } + return append(hash[:], mode.Bytes()...), nil +} + +func (n *node) doCalculateHashForRegular(path string, file os.FileInfo) (plumbing.Hash, error) { + f, err := n.fs.Open(path) + if err != nil { + return plumbing.ZeroHash, err + } + defer f.Close() h := plumbing.NewHasher(plumbing.BlobObject, file.Size()) if _, err := io.Copy(h, f); err != nil { - return nil, err + return plumbing.ZeroHash, err } - mode, err := filemode.NewFromOSFileMode(file.Mode()) + return h.Sum(), nil +} + +func (n *node) doCalculateHashForSymlink(path string, file os.FileInfo) (plumbing.Hash, error) { + target, err := n.fs.Readlink(path) if err != nil { - return nil, err + return plumbing.ZeroHash, err } - hash := h.Sum() - return append(hash[:], mode.Bytes()...), nil + h := plumbing.NewHasher(plumbing.BlobObject, file.Size()) + if _, err := h.Write([]byte(target)); err != nil { + return plumbing.ZeroHash, err + } + + return h.Sum(), nil } func (n *node) String() string { diff --git a/utils/merkletrie/filesystem/node_test.go b/utils/merkletrie/filesystem/node_test.go index a383716..bf1178a 100644 --- a/utils/merkletrie/filesystem/node_test.go +++ b/utils/merkletrie/filesystem/node_test.go @@ -25,11 +25,13 @@ func (s *NoderSuite) TestDiff(c *C) { WriteFile(fsA, "foo", []byte("foo"), 0644) WriteFile(fsA, "qux/bar", []byte("foo"), 0644) WriteFile(fsA, "qux/qux", []byte("foo"), 0644) + fsA.Symlink("foo", "bar") fsB := memfs.New() WriteFile(fsB, "foo", []byte("foo"), 0644) WriteFile(fsB, "qux/bar", []byte("foo"), 0644) WriteFile(fsB, "qux/qux", []byte("foo"), 0644) + fsB.Symlink("foo", "bar") ch, err := merkletrie.DiffTree( NewRootNode(fsA, nil), @@ -41,6 +43,23 @@ func (s *NoderSuite) TestDiff(c *C) { c.Assert(ch, HasLen, 0) } +func (s *NoderSuite) TestDiffChangeLink(c *C) { + fsA := memfs.New() + fsA.Symlink("qux", "foo") + + fsB := memfs.New() + fsB.Symlink("bar", "foo") + + ch, err := merkletrie.DiffTree( + NewRootNode(fsA, nil), + NewRootNode(fsB, nil), + IsEquals, + ) + + c.Assert(err, IsNil) + c.Assert(ch, HasLen, 1) +} + func (s *NoderSuite) TestDiffChangeContent(c *C) { fsA := memfs.New() WriteFile(fsA, "foo", []byte("foo"), 0644) |