Skip to content

Commit

Permalink
Worktree: Add, fix add removed files. Fixes go-git#223 (go-git#652)
Browse files Browse the repository at this point in the history
  • Loading branch information
tfujiwar authored and durandj committed Jul 1, 2023
1 parent 0f535c5 commit 62e8cf8
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 16 deletions.
41 changes: 25 additions & 16 deletions worktree_status.go
Expand Up @@ -270,10 +270,6 @@ func (w *Worktree) Add(path string) (plumbing.Hash, error) {
}

func (w *Worktree) doAddDirectory(idx *index.Index, s Status, directory string, ignorePattern []gitignore.Pattern) (added bool, err error) {
files, err := w.Filesystem.ReadDir(directory)
if err != nil {
return false, err
}
if len(ignorePattern) > 0 {
m := gitignore.NewMatcher(ignorePattern)
matchPath := strings.Split(directory, string(os.PathSeparator))
Expand All @@ -283,20 +279,13 @@ func (w *Worktree) doAddDirectory(idx *index.Index, s Status, directory string,
}
}

for _, file := range files {
name := path.Join(directory, file.Name())

var a bool
if file.IsDir() {
if file.Name() == GitDirName {
// ignore special git directory
continue
}
a, err = w.doAddDirectory(idx, s, name, ignorePattern)
} else {
a, _, err = w.doAddFile(idx, s, name, ignorePattern)
for name := range s {
if !isPathInDirectory(name, filepath.ToSlash(filepath.Clean(directory))) {
continue
}

var a bool
a, _, err = w.doAddFile(idx, s, name, ignorePattern)
if err != nil {
return
}
Expand All @@ -309,6 +298,26 @@ func (w *Worktree) doAddDirectory(idx *index.Index, s Status, directory string,
return
}

func isPathInDirectory(path, directory string) bool {
ps := strings.Split(path, "/")
ds := strings.Split(directory, "/")

if len(ds) == 1 && ds[0] == "." {
return true
}

if len(ps) < len(ds) {
return false
}

for i := 0; i < len(ds); i++ {
if ps[i] != ds[i] {
return false
}
}
return true
}

// AddWithOptions file contents to the index, updates the index using the
// current content found in the working tree, to prepare the content staged for
// the next commit.
Expand Down
45 changes: 45 additions & 0 deletions worktree_test.go
Expand Up @@ -1398,6 +1398,51 @@ 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("go/example.go")
c.Assert(err, IsNil)

err = w.Filesystem.Remove("json/short.json")
c.Assert(err, IsNil)

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

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

e, err = idx.Entry("json/short.json")
c.Assert(err, IsNil)
c.Assert(e.Hash, Equals, plumbing.NewHash("c8f1d8c61f9da76f4cb49fd86322b6e685dba956"))
c.Assert(e.Mode, Equals, filemode.Regular)

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

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

file = status.File("json/short.json")
c.Assert(file.Staging, Equals, Unmodified)
}

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

0 comments on commit 62e8cf8

Please sign in to comment.