From 93abd6f1b3a1748a6ee37a42da3df7c96aeb8005 Mon Sep 17 00:00:00 2001 From: Taketoshi Fujiwara Date: Tue, 3 Jan 2023 15:24:17 +0900 Subject: [PATCH] git: fix doAddDirectory to add removed files. Fixes #223 --- worktree_status.go | 19 +++++++++++++++++++ worktree_test.go | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 53 insertions(+) diff --git a/worktree_status.go b/worktree_status.go index c639f1320..f55eb0454 100644 --- a/worktree_status.go +++ b/worktree_status.go @@ -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 } diff --git a/worktree_test.go b/worktree_test.go index 4c0633355..a2159af63 100644 --- a/worktree_test.go +++ b/worktree_test.go @@ -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()