Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

worktree: minor speedup for doAddDirectory #702

Merged
merged 2 commits into from
Jul 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
26 changes: 5 additions & 21 deletions worktree_status.go
Original file line number Diff line number Diff line change
Expand Up @@ -281,8 +281,10 @@ func (w *Worktree) doAddDirectory(idx *index.Index, s Status, directory string,
}
}

directory = filepath.ToSlash(filepath.Clean(directory))

for name := range s {
if !isPathInDirectory(name, filepath.ToSlash(filepath.Clean(directory))) {
if !isPathInDirectory(name, directory) {
continue
}

Expand All @@ -292,32 +294,14 @@ func (w *Worktree) doAddDirectory(idx *index.Index, s Status, directory string,
return
}

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

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
return directory == "." || strings.HasPrefix(path, directory+"/")
}

// AddWithOptions file contents to the index, updates the index using the
Expand Down
90 changes: 90 additions & 0 deletions worktree_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1488,6 +1488,96 @@ func (s *WorktreeSuite) TestAddRemovedInDirectory(c *C) {
c.Assert(file.Staging, Equals, Unmodified)
}

func (s *WorktreeSuite) TestAddRemovedInDirectoryWithTrailingSlash(c *C) {
ThinkChaos marked this conversation as resolved.
Show resolved Hide resolved
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) TestAddRemovedInDirectoryDot(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(".")
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, Deleted)
}

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