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/node.go | |
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/node.go')
-rw-r--r-- | utils/merkletrie/filesystem/node.go | 41 |
1 files changed, 35 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 { |