diff options
-rw-r--r-- | utils/merkletrie/index/node.go | 6 | ||||
-rw-r--r-- | utils/merkletrie/index/node_test.go | 16 | ||||
-rw-r--r-- | worktree.go | 6 | ||||
-rw-r--r-- | worktree_status.go | 25 |
4 files changed, 27 insertions, 26 deletions
diff --git a/utils/merkletrie/index/node.go b/utils/merkletrie/index/node.go index 2c72f6d..859c097 100644 --- a/utils/merkletrie/index/node.go +++ b/utils/merkletrie/index/node.go @@ -16,7 +16,7 @@ import ( // compared with any other noder.Noder implementation inside of go-git type node struct { path string - entry index.Entry + entry *index.Entry children []noder.Noder isDir bool } @@ -66,6 +66,10 @@ func (n *node) String() string { // If the node is computed and not based on a index.Entry the hash is equals // to a 24-bytes slices of zero values. func (n *node) Hash() []byte { + if n.entry == nil { + return make([]byte, 24) + } + return append(n.entry.Hash[:], n.entry.Mode.Bytes()...) } diff --git a/utils/merkletrie/index/node_test.go b/utils/merkletrie/index/node_test.go index 48aa35f..00da8da 100644 --- a/utils/merkletrie/index/node_test.go +++ b/utils/merkletrie/index/node_test.go @@ -19,7 +19,7 @@ var _ = Suite(&NoderSuite{}) func (s *NoderSuite) TestDiff(c *C) { indexA := &index.Index{ - Entries: []index.Entry{ + Entries: []*index.Entry{ {Name: "foo", Hash: plumbing.NewHash("8ab686eafeb1f44702738c8b0f24f2567c36da6d")}, {Name: "bar/foo", Hash: plumbing.NewHash("8ab686eafeb1f44702738c8b0f24f2567c36da6d")}, {Name: "bar/qux", Hash: plumbing.NewHash("8ab686eafeb1f44702738c8b0f24f2567c36da6d")}, @@ -28,7 +28,7 @@ func (s *NoderSuite) TestDiff(c *C) { } indexB := &index.Index{ - Entries: []index.Entry{ + Entries: []*index.Entry{ {Name: "foo", Hash: plumbing.NewHash("8ab686eafeb1f44702738c8b0f24f2567c36da6d")}, {Name: "bar/foo", Hash: plumbing.NewHash("8ab686eafeb1f44702738c8b0f24f2567c36da6d")}, {Name: "bar/qux", Hash: plumbing.NewHash("8ab686eafeb1f44702738c8b0f24f2567c36da6d")}, @@ -43,13 +43,13 @@ func (s *NoderSuite) TestDiff(c *C) { func (s *NoderSuite) TestDiffChange(c *C) { indexA := &index.Index{ - Entries: []index.Entry{ + Entries: []*index.Entry{ {Name: "bar/baz/bar", Hash: plumbing.NewHash("8ab686eafeb1f44702738c8b0f24f2567c36da6d")}, }, } indexB := &index.Index{ - Entries: []index.Entry{ + Entries: []*index.Entry{ {Name: "bar/baz/foo", Hash: plumbing.NewHash("8ab686eafeb1f44702738c8b0f24f2567c36da6d")}, }, } @@ -61,13 +61,13 @@ func (s *NoderSuite) TestDiffChange(c *C) { func (s *NoderSuite) TestDiffDir(c *C) { indexA := &index.Index{ - Entries: []index.Entry{ + Entries: []*index.Entry{ {Name: "foo", Hash: plumbing.NewHash("8ab686eafeb1f44702738c8b0f24f2567c36da6d")}, }, } indexB := &index.Index{ - Entries: []index.Entry{ + Entries: []*index.Entry{ {Name: "foo/bar", Hash: plumbing.NewHash("8ab686eafeb1f44702738c8b0f24f2567c36da6d")}, }, } @@ -79,14 +79,14 @@ func (s *NoderSuite) TestDiffDir(c *C) { func (s *NoderSuite) TestDiffSameRoot(c *C) { indexA := &index.Index{ - Entries: []index.Entry{ + Entries: []*index.Entry{ {Name: "foo.go", Hash: plumbing.NewHash("aab686eafeb1f44702738c8b0f24f2567c36da6d")}, {Name: "foo/bar", Hash: plumbing.NewHash("8ab686eafeb1f44702738c8b0f24f2567c36da6d")}, }, } indexB := &index.Index{ - Entries: []index.Entry{ + Entries: []*index.Entry{ {Name: "foo/bar", Hash: plumbing.NewHash("8ab686eafeb1f44702738c8b0f24f2567c36da6d")}, {Name: "foo.go", Hash: plumbing.NewHash("8ab686eafeb1f44702738c8b0f24f2567c36da6d")}, }, diff --git a/worktree.go b/worktree.go index e92449c..ad6e0f7 100644 --- a/worktree.go +++ b/worktree.go @@ -352,7 +352,7 @@ func (w *Worktree) checkoutFile(f *object.File) error { } func (w *Worktree) addIndexFromTreeEntry(name string, f *object.TreeEntry, idx *index.Index) error { - idx.Entries = append(idx.Entries, index.Entry{ + idx.Entries = append(idx.Entries, &index.Entry{ Hash: f.Hash, Name: name, Mode: filemode.Submodule, @@ -372,7 +372,7 @@ func (w *Worktree) addIndexFromFile(name string, h plumbing.Hash, idx *index.Ind return err } - e := index.Entry{ + e := &index.Entry{ Hash: h, Name: name, Mode: mode, @@ -383,7 +383,7 @@ func (w *Worktree) addIndexFromFile(name string, h plumbing.Hash, idx *index.Ind // if the FileInfo.Sys() comes from os the ctime, dev, inode, uid and gid // can be retrieved, otherwise this doesn't apply if fillSystemInfo != nil { - fillSystemInfo(&e, fi.Sys()) + fillSystemInfo(e, fi.Sys()) } idx.Entries = append(idx.Entries, e) diff --git a/worktree_status.go b/worktree_status.go index 6becada..46922da 100644 --- a/worktree_status.go +++ b/worktree_status.go @@ -234,7 +234,7 @@ func (w *Worktree) addOrUpdateFileToIndex(filename string, h plumbing.Hash) erro } func (w *Worktree) doAddFileToIndex(idx *index.Index, filename string) error { - idx.Entries = append(idx.Entries, index.Entry{ + idx.Entries = append(idx.Entries, &index.Entry{ Name: filename, }) @@ -247,21 +247,18 @@ func (w *Worktree) doUpdateFileToIndex(idx *index.Index, filename string, h plum return err } - for i, e := range idx.Entries { - if e.Name != filename { - continue - } - - e.Hash = h - e.ModifiedAt = info.ModTime() - e.Mode, err = filemode.NewFromOSFileMode(info.Mode()) - if err != nil { - return err - } + e, err := idx.Entry(filename) + if err != nil { + return err + } - fillSystemInfo(&e, info.Sys()) - idx.Entries[i] = e + e.Hash = h + e.ModifiedAt = info.ModTime() + e.Mode, err = filemode.NewFromOSFileMode(info.Mode()) + if err != nil { + return err } + fillSystemInfo(e, info.Sys()) return nil } |