aboutsummaryrefslogtreecommitdiffstats
path: root/worktree_status.go
diff options
context:
space:
mode:
authorMáximo Cuadros <mcuadros@gmail.com>2017-06-19 18:27:30 +0200
committerGitHub <noreply@github.com>2017-06-19 18:27:30 +0200
commitdfd68a1f987fc529a7626cc6ea1141f371115777 (patch)
treee1af261c1601bfcaec77ea45be6f2a0943d116a6 /worktree_status.go
parentfc268a1a27414d7d1259a7f366e6191bd18c016f (diff)
parent85a91266571a966f10e73bdd641d753eb0431a7a (diff)
downloadgo-git-dfd68a1f987fc529a7626cc6ea1141f371115777.tar.gz
Merge pull request #441 from mcuadros/fix-add
worktree: Add create and push the blob objects to the storer
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 {