Skip to content

Commit

Permalink
git: fix doAddDirectory to add removed files. Fixes go-git#223
Browse files Browse the repository at this point in the history
  • Loading branch information
tfujiwar committed Jan 3, 2023
1 parent 736622f commit 93abd6f
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 0 deletions.
19 changes: 19 additions & 0 deletions worktree_status.go
Expand Up @@ -306,6 +306,25 @@ func (w *Worktree) doAddDirectory(idx *index.Index, s Status, directory string,
}
}

// add removed files in the directory
entries, err := idx.Glob(path.Join(directory, "*"))
if err != nil {
return
}

for _, entry := range entries {
var a bool
a, _, err = w.doAddFile(idx, s, entry.Name, ignorePattern)

if err != nil {
return
}

if !added && a {
added = true
}
}

return
}

Expand Down
34 changes: 34 additions & 0 deletions worktree_test.go
Expand Up @@ -1398,6 +1398,40 @@ func (s *WorktreeSuite) TestAddRemoved(c *C) {
c.Assert(file.Staging, Equals, Deleted)
}

func (s *WorktreeSuite) TestAddRemovedInDirectory(c *C) {
fs := memfs.New()
w := &Worktree{
r: s.Repository,
Filesystem: fs,
}

err := w.Checkout(&CheckoutOptions{Force: true})
c.Assert(err, IsNil)

idx, err := w.r.Storer.Index()
c.Assert(err, IsNil)
c.Assert(idx.Entries, HasLen, 9)

err = w.Filesystem.Remove(filepath.Join("go", "example.go"))
c.Assert(err, IsNil)

hash, err := w.Add(".")
c.Assert(err, IsNil)
c.Assert(hash.IsZero(), Equals, true)

e, err := idx.Entry(filepath.Join("go", "example.go"))
c.Assert(err, IsNil)
c.Assert(e.Hash, Equals, plumbing.NewHash("880cd14280f4b9b6ed3986d6671f907d7cc2a198"))
c.Assert(e.Mode, Equals, filemode.Regular)

status, err := w.Status()
c.Assert(err, IsNil)
c.Assert(status, HasLen, 1)

file := status.File(filepath.Join("go", "example.go"))
c.Assert(file.Staging, Equals, Deleted)
}

func (s *WorktreeSuite) TestAddSymlink(c *C) {
dir, clean := s.TemporalDir()
defer clean()
Expand Down

0 comments on commit 93abd6f

Please sign in to comment.