Skip to content
This repository was archived by the owner on Sep 11, 2020. It is now read-only.

Worktree.Add: Support Add deleted files, fixes #571 #577

Merged
merged 7 commits into from
Sep 5, 2017
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions worktree_commit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,42 @@ func (s *WorktreeSuite) TestCommitAll(c *C) {
assertStorageStatus(c, s.Repository, 13, 11, 10, expected)
}

func (s *WorktreeSuite) TestRemoveAndCommitAll(c *C) {
expected := plumbing.NewHash("907cd576c6ced2ecd3dab34a72bf9cf65944b9a9")

fs := memfs.New()
w := &Worktree{
r: s.Repository,
Filesystem: fs,
}

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

util.WriteFile(fs, "foo", []byte("foo"), 0644)
_, err = w.Add("foo")
c.Assert(err, IsNil)

_, errFirst := w.Commit("Add in Repo\n", &CommitOptions{
Author: defaultSignature(),
})
c.Assert(errFirst, IsNil)

errRemove := fs.Remove("foo")
c.Assert(errRemove, IsNil)

hash, errSecond := w.Commit("Remove foo\n", &CommitOptions{
All: true,
Author: defaultSignature(),
})
c.Assert(errSecond, IsNil)

c.Assert(hash, Equals, expected)
c.Assert(err, IsNil)

assertStorageStatus(c, s.Repository, 13, 11, 11, expected)
}

func assertStorageStatus(
c *C, r *Repository,
treesCount, blobCount, commitCount int, head plumbing.Hash,
Expand Down
15 changes: 14 additions & 1 deletion worktree_status.go
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ func (w *Worktree) Add(path string) (plumbing.Hash, error) {

h, err := w.copyFileToStorage(path)
if err != nil {
return h, err
return w.removeIfDeleted(path, h, err)
}

if s.File(path).Worktree == Unmodified {
Expand All @@ -266,6 +266,19 @@ func (w *Worktree) Add(path string) (plumbing.Hash, error) {
return h, err
}

func (w *Worktree) removeIfDeleted(path string, hash plumbing.Hash, err error) (plumbing.Hash, error) {
pathError, ok := err.(*os.PathError)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you don't need to do the cast or check if is a lsstat, the function is smart enough for detect any non-existan file

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also I rather having the if condition where the err is generated instead of having another function. Thanks

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

if ok && pathError.Op == "lstat" || os.IsNotExist(err) {
h, errDelete := w.deleteFromIndex(path)
if errDelete != nil {
return h, errDelete
}
return hash, nil
}

return hash, err
}

func (w *Worktree) copyFileToStorage(path string) (hash plumbing.Hash, err error) {
fi, err := w.Filesystem.Lstat(path)
if err != nil {
Expand Down
4 changes: 4 additions & 0 deletions worktree_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -372,7 +372,11 @@ func (s *WorktreeSuite) TestFilenameNormalization(c *C) {

modFilename := norm.Form(norm.NFKD).String(filename)
util.WriteFile(w.Filesystem, modFilename, []byte("foo"), 0755)

_, err = w.Add(filename)
c.Assert(err, IsNil)
_, err = w.Add(modFilename)
c.Assert(err, IsNil)

status, err = w.Status()
c.Assert(err, IsNil)
Expand Down