Skip to content

Commit

Permalink
git: worktree, Build status based on the current index instead of bui…
Browse files Browse the repository at this point in the history
…lding it empty. Fixes go-git#119
  • Loading branch information
rodrigocam committed Feb 5, 2024
1 parent 03a57f8 commit 0931346
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 1 deletion.
39 changes: 38 additions & 1 deletion worktree_status.go
Expand Up @@ -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 {
Expand Down
19 changes: 19 additions & 0 deletions worktree_test.go
Expand Up @@ -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{
Expand Down

0 comments on commit 0931346

Please sign in to comment.