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 /worktree_status.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 'worktree_status.go')
-rw-r--r-- | worktree_status.go | 33 |
1 files changed, 27 insertions, 6 deletions
diff --git a/worktree_status.go b/worktree_status.go index 7cc4b0f..648ea89 100644 --- a/worktree_status.go +++ b/worktree_status.go @@ -244,11 +244,15 @@ func (w *Worktree) Add(path string) (plumbing.Hash, error) { } func (w *Worktree) calculateBlobHash(filename string) (hash plumbing.Hash, err error) { - fi, err := w.fs.Stat(filename) + fi, err := w.fs.Lstat(filename) if err != nil { return plumbing.ZeroHash, err } + if fi.Mode()&os.ModeSymlink != 0 { + return w.calculateBlobHashFromSymlink(filename) + } + f, err := w.fs.Open(filename) if err != nil { return plumbing.ZeroHash, err @@ -265,6 +269,21 @@ func (w *Worktree) calculateBlobHash(filename string) (hash plumbing.Hash, err e return } +func (w *Worktree) calculateBlobHashFromSymlink(link string) (plumbing.Hash, error) { + target, err := w.fs.Readlink(link) + if err != nil { + return plumbing.ZeroHash, err + } + + h := plumbing.NewHasher(plumbing.BlobObject, int64(len(target))) + _, err = h.Write([]byte(target)) + if err != nil { + return plumbing.ZeroHash, err + } + + return h.Sum(), nil +} + func (w *Worktree) addOrUpdateFileToIndex(filename string, h plumbing.Hash) error { idx, err := w.r.Storer.Index() if err != nil { @@ -297,7 +316,7 @@ func (w *Worktree) doAddFileToIndex(idx *index.Index, filename string, h plumbin } func (w *Worktree) doUpdateFileToIndex(e *index.Entry, filename string, h plumbing.Hash) error { - info, err := w.fs.Stat(filename) + info, err := w.fs.Lstat(filename) if err != nil { return err } @@ -305,12 +324,14 @@ func (w *Worktree) doUpdateFileToIndex(e *index.Entry, filename string, h plumbi e.Hash = h e.ModifiedAt = info.ModTime() e.Mode, err = filemode.NewFromOSFileMode(info.Mode()) - e.Size = uint32(info.Size()) - if err != nil { return err } + if e.Mode.IsRegular() { + e.Size = uint32(info.Size()) + } + fillSystemInfo(e, info.Sys()) return nil } @@ -351,11 +372,11 @@ func (w *Worktree) deleteFromFilesystem(path string) error { // Move moves or rename a file in the worktree and the index, directories are // not supported. func (w *Worktree) Move(from, to string) (plumbing.Hash, error) { - if _, err := w.fs.Stat(from); err != nil { + if _, err := w.fs.Lstat(from); err != nil { return plumbing.ZeroHash, err } - if _, err := w.fs.Stat(to); err == nil { + if _, err := w.fs.Lstat(to); err == nil { return plumbing.ZeroHash, ErrDestinationExists } |