aboutsummaryrefslogtreecommitdiffstats
path: root/worktree_status.go
diff options
context:
space:
mode:
Diffstat (limited to 'worktree_status.go')
-rw-r--r--worktree_status.go52
1 files changed, 33 insertions, 19 deletions
diff --git a/worktree_status.go b/worktree_status.go
index 648ea89..7116445 100644
--- a/worktree_status.go
+++ b/worktree_status.go
@@ -227,7 +227,7 @@ func (w *Worktree) Add(path string) (plumbing.Hash, error) {
return plumbing.ZeroHash, err
}
- h, err := w.calculateBlobHash(path)
+ h, err := w.copyFileToStorage(path)
if err != nil {
return h, err
}
@@ -243,45 +243,59 @@ func (w *Worktree) Add(path string) (plumbing.Hash, error) {
return h, err
}
-func (w *Worktree) calculateBlobHash(filename string) (hash plumbing.Hash, err error) {
- fi, err := w.fs.Lstat(filename)
+func (w *Worktree) copyFileToStorage(path string) (hash plumbing.Hash, err error) {
+ fi, err := w.fs.Lstat(path)
if err != nil {
return plumbing.ZeroHash, err
}
- if fi.Mode()&os.ModeSymlink != 0 {
- return w.calculateBlobHashFromSymlink(filename)
- }
+ obj := w.r.Storer.NewEncodedObject()
+ obj.SetType(plumbing.BlobObject)
+ obj.SetSize(fi.Size())
- f, err := w.fs.Open(filename)
+ writer, err := obj.Writer()
if err != nil {
return plumbing.ZeroHash, err
}
- defer ioutil.CheckClose(f, &err)
+ defer ioutil.CheckClose(writer, &err)
+
+ if fi.Mode()&os.ModeSymlink != 0 {
+ err = w.fillEncodedObjectFromSymlink(writer, path, fi)
+ } else {
+ err = w.fillEncodedObjectFromFile(writer, path, fi)
+ }
- h := plumbing.NewHasher(plumbing.BlobObject, fi.Size())
- if _, err := io.Copy(h, f); err != nil {
+ if err != nil {
return plumbing.ZeroHash, err
}
- hash = h.Sum()
- return
+ return w.r.Storer.SetEncodedObject(obj)
}
-func (w *Worktree) calculateBlobHashFromSymlink(link string) (plumbing.Hash, error) {
- target, err := w.fs.Readlink(link)
+func (w *Worktree) fillEncodedObjectFromFile(dst io.Writer, path string, fi os.FileInfo) (err error) {
+ src, err := w.fs.Open(path)
if err != nil {
- return plumbing.ZeroHash, err
+ return err
}
- h := plumbing.NewHasher(plumbing.BlobObject, int64(len(target)))
- _, err = h.Write([]byte(target))
+ defer ioutil.CheckClose(src, &err)
+
+ if _, err := io.Copy(dst, src); err != nil {
+ return err
+ }
+
+ return err
+}
+
+func (w *Worktree) fillEncodedObjectFromSymlink(dst io.Writer, path string, fi os.FileInfo) error {
+ target, err := w.fs.Readlink(path)
if err != nil {
- return plumbing.ZeroHash, err
+ return err
}
- return h.Sum(), nil
+ _, err = dst.Write([]byte(target))
+ return err
}
func (w *Worktree) addOrUpdateFileToIndex(filename string, h plumbing.Hash) error {