aboutsummaryrefslogtreecommitdiffstats
path: root/worktree_status.go
diff options
context:
space:
mode:
Diffstat (limited to 'worktree_status.go')
-rw-r--r--worktree_status.go33
1 files changed, 27 insertions, 6 deletions
diff --git a/worktree_status.go b/worktree_status.go
index eb4a83a..ba0509a 100644
--- a/worktree_status.go
+++ b/worktree_status.go
@@ -212,11 +212,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
@@ -233,6 +237,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 {
@@ -265,7 +284,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
}
@@ -273,12 +292,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
}
@@ -319,11 +340,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
}