diff options
author | Rodrigo Oliveira <rodrigo.redcode@gmail.com> | 2024-02-05 10:55:04 -0300 |
---|---|---|
committer | Rodrigo Oliveira <rodrigo.redcode@gmail.com> | 2024-02-05 10:55:04 -0300 |
commit | 093134604cde84f51625efdcf5266a62cd5ab6e9 (patch) | |
tree | b3f887bf3af5b5dad7489190b39413d8aacee1e8 | |
parent | 03a57f8f5179a990083e8b9c2c9e40854ae402ea (diff) | |
download | go-git-093134604cde84f51625efdcf5266a62cd5ab6e9.tar.gz |
git: worktree, Build status based on the current index instead of building it empty. Fixes #119
-rw-r--r-- | worktree_status.go | 39 | ||||
-rw-r--r-- | worktree_test.go | 19 |
2 files changed, 57 insertions, 1 deletions
diff --git a/worktree_status.go b/worktree_status.go index dd9b243..2f865ce 100644 --- a/worktree_status.go +++ b/worktree_status.go @@ -47,8 +47,45 @@ func (w *Worktree) Status() (Status, error) { return w.status(hash) } +func (w *Worktree) newStatusFromIndex() (Status, error) { + idx, err := w.r.Storer.Index() + if err != nil { + return nil, err + } + + idxRoot := mindex.NewRootNode(idx) + nodes := []noder.Noder{idxRoot} + + if err != nil { + return nil, err + } + + status := make(Status) + + for len(nodes) > 0 { + var node noder.Noder + node, nodes = nodes[0], nodes[1:] + if node.IsDir() { + children, err := node.Children() + if err != nil { + return nil, err + } + nodes = append(nodes, children...) + continue + } + fs := status.File(node.Name()) + fs.Worktree = Unmodified + fs.Staging = Unmodified + } + + return status, nil +} + func (w *Worktree) status(commit plumbing.Hash) (Status, error) { - s := make(Status) + s, err := w.newStatusFromIndex() + if err != nil { + return nil, err + } left, err := w.diffCommitWithStaging(commit, false) if err != nil { diff --git a/worktree_test.go b/worktree_test.go index 2c3c592..deaf5e5 100644 --- a/worktree_test.go +++ b/worktree_test.go @@ -1052,6 +1052,25 @@ func (s *WorktreeSuite) TestStatusEmptyDirty(c *C) { c.Assert(status, HasLen, 1) } +func (s *WorktreeSuite) TestStatusUnmodified(c *C) { + fs := memfs.New() + w := &Worktree{ + r: s.Repository, + Filesystem: fs, + } + + err := w.Checkout(&CheckoutOptions{Force: true}) + c.Assert(err, IsNil) + + status, err := w.Status() + c.Assert(err, IsNil) + c.Assert(status.IsClean(), Equals, true) + c.Assert(status.IsUntracked("LICENSE"), Equals, false) + + c.Assert(status.File("LICENSE").Staging, Equals, Unmodified) + c.Assert(status.File("LICENSE").Worktree, Equals, Unmodified) +} + func (s *WorktreeSuite) TestReset(c *C) { fs := memfs.New() w := &Worktree{ |