From 85a91266571a966f10e73bdd641d753eb0431a7a Mon Sep 17 00:00:00 2001 From: Máximo Cuadros Date: Mon, 19 Jun 2017 15:03:30 +0200 Subject: worktree: Add create and push the blob objects to the storer MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Máximo Cuadros --- worktree_status.go | 52 +++++++++++++++++++++++++++++++++------------------- 1 file changed, 33 insertions(+), 19 deletions(-) (limited to 'worktree_status.go') 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 { -- cgit