From faf5a724290787d518bd8b66cd408084ed70ba40 Mon Sep 17 00:00:00 2001 From: GRUNENWALD Florian Date: Mon, 4 Sep 2017 16:06:09 +0200 Subject: [PATCH 1/6] Deleting a file from the filesystem (os or memory) is now posible. Plus its associated test. Fixed, what I think, a false positive in TestFilenameNormalization in worktree_test.go. --- worktree_commit_test.go | 45 +++++++++++++++++++++++++++++++++++++++++ worktree_status.go | 19 ++++++++++++++++- worktree_test.go | 4 ++++ 3 files changed, 67 insertions(+), 1 deletion(-) diff --git a/worktree_commit_test.go b/worktree_commit_test.go index f6744bc68..7b3b89ec0 100644 --- a/worktree_commit_test.go +++ b/worktree_commit_test.go @@ -9,6 +9,7 @@ import ( "gopkg.in/src-d/go-git.v4/storage/memory" . "gopkg.in/check.v1" + "gopkg.in/src-d/go-billy.v3" "gopkg.in/src-d/go-billy.v3/memfs" "gopkg.in/src-d/go-billy.v3/util" ) @@ -99,6 +100,50 @@ 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 emulateExternalRemoval(c *C, fs billy.Basic, w *Worktree, path string) { + + s, errStatus := w.Status() + c.Assert(errStatus, IsNil) + s[path].Worktree = Unmodified + s[path].Staging = Unmodified +} + func assertStorageStatus( c *C, r *Repository, treesCount, blobCount, commitCount int, head plumbing.Hash, diff --git a/worktree_status.go b/worktree_status.go index 9b0773e84..d4bfda1a2 100644 --- a/worktree_status.go +++ b/worktree_status.go @@ -236,7 +236,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 { @@ -250,6 +250,23 @@ 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) + if ok && pathError.Op == "lstat" || w.isErrNotExist(err) { + h, errDelete := w.deleteFromIndex(path) + if errDelete != nil { + return h, errDelete + } + return hash, nil + } + + return hash, err +} + +func (w *Worktree) isErrNotExist(err error) bool { + return err.Error() == os.ErrNotExist.Error() +} + func (w *Worktree) copyFileToStorage(path string) (hash plumbing.Hash, err error) { fi, err := w.Filesystem.Lstat(path) if err != nil { diff --git a/worktree_test.go b/worktree_test.go index 70167f0b8..81c42f669 100644 --- a/worktree_test.go +++ b/worktree_test.go @@ -349,7 +349,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) From 238155ae0b7d74a5188e9faac7bdb6f565405a97 Mon Sep 17 00:00:00 2001 From: GRUNENWALD Florian Date: Mon, 4 Sep 2017 16:29:52 +0200 Subject: [PATCH 2/6] Remove unecessary function --- worktree_commit_test.go | 10 +--------- 1 file changed, 1 insertion(+), 9 deletions(-) diff --git a/worktree_commit_test.go b/worktree_commit_test.go index 7b3b89ec0..b026d65cf 100644 --- a/worktree_commit_test.go +++ b/worktree_commit_test.go @@ -9,9 +9,9 @@ import ( "gopkg.in/src-d/go-git.v4/storage/memory" . "gopkg.in/check.v1" - "gopkg.in/src-d/go-billy.v3" "gopkg.in/src-d/go-billy.v3/memfs" "gopkg.in/src-d/go-billy.v3/util" + "gopkg.in/src-d/go-billy.v3" ) func (s *WorktreeSuite) TestCommitInvalidOptions(c *C) { @@ -136,14 +136,6 @@ func (s *WorktreeSuite) TestRemoveAndCommitAll(c *C) { assertStorageStatus(c, s.Repository, 13, 11, 11, expected) } -func emulateExternalRemoval(c *C, fs billy.Basic, w *Worktree, path string) { - - s, errStatus := w.Status() - c.Assert(errStatus, IsNil) - s[path].Worktree = Unmodified - s[path].Staging = Unmodified -} - func assertStorageStatus( c *C, r *Repository, treesCount, blobCount, commitCount int, head plumbing.Hash, From c8e7ddba58551bd065f691adc6a8e82d88621d69 Mon Sep 17 00:00:00 2001 From: GRUNENWALD Florian Date: Mon, 4 Sep 2017 16:31:09 +0200 Subject: [PATCH 3/6] Remove unecessary function --- worktree_commit_test.go | 1 - 1 file changed, 1 deletion(-) diff --git a/worktree_commit_test.go b/worktree_commit_test.go index b026d65cf..09360afe9 100644 --- a/worktree_commit_test.go +++ b/worktree_commit_test.go @@ -11,7 +11,6 @@ import ( . "gopkg.in/check.v1" "gopkg.in/src-d/go-billy.v3/memfs" "gopkg.in/src-d/go-billy.v3/util" - "gopkg.in/src-d/go-billy.v3" ) func (s *WorktreeSuite) TestCommitInvalidOptions(c *C) { From 115cc65384752a445875d34a4a4bdc1a67637964 Mon Sep 17 00:00:00 2001 From: GRUNENWALD Florian Date: Tue, 5 Sep 2017 10:01:31 +0200 Subject: [PATCH 4/6] Using built-in function os.IsNotExist --- worktree_status.go | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/worktree_status.go b/worktree_status.go index aa54851e2..d4d6c0a65 100644 --- a/worktree_status.go +++ b/worktree_status.go @@ -268,7 +268,7 @@ func (w *Worktree) Add(path string) (plumbing.Hash, error) { func (w *Worktree) removeIfDeleted(path string, hash plumbing.Hash, err error) (plumbing.Hash, error) { pathError, ok := err.(*os.PathError) - if ok && pathError.Op == "lstat" || w.isErrNotExist(err) { + if ok && pathError.Op == "lstat" || os.IsNotExist(err) { h, errDelete := w.deleteFromIndex(path) if errDelete != nil { return h, errDelete @@ -279,10 +279,6 @@ func (w *Worktree) removeIfDeleted(path string, hash plumbing.Hash, err error) ( return hash, err } -func (w *Worktree) isErrNotExist(err error) bool { - return err.Error() == os.ErrNotExist.Error() -} - func (w *Worktree) copyFileToStorage(path string) (hash plumbing.Hash, err error) { fi, err := w.Filesystem.Lstat(path) if err != nil { From b14ebb4e41d8623cc97dbb66e57797521d168581 Mon Sep 17 00:00:00 2001 From: GRUNENWALD Florian Date: Tue, 5 Sep 2017 10:09:53 +0200 Subject: [PATCH 5/6] Removing cast --- worktree_status.go | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/worktree_status.go b/worktree_status.go index d4d6c0a65..77143c3d2 100644 --- a/worktree_status.go +++ b/worktree_status.go @@ -267,8 +267,7 @@ func (w *Worktree) Add(path string) (plumbing.Hash, error) { } func (w *Worktree) removeIfDeleted(path string, hash plumbing.Hash, err error) (plumbing.Hash, error) { - pathError, ok := err.(*os.PathError) - if ok && pathError.Op == "lstat" || os.IsNotExist(err) { + if os.IsNotExist(err) { h, errDelete := w.deleteFromIndex(path) if errDelete != nil { return h, errDelete From d2e5d331a20542d9e3968160ffbaaa1631a211ef Mon Sep 17 00:00:00 2001 From: GRUNENWALD Florian Date: Tue, 5 Sep 2017 10:17:58 +0200 Subject: [PATCH 6/6] Refactoring --- worktree_status.go | 17 ++++------------- 1 file changed, 4 insertions(+), 13 deletions(-) diff --git a/worktree_status.go b/worktree_status.go index 77143c3d2..b2848f982 100644 --- a/worktree_status.go +++ b/worktree_status.go @@ -252,7 +252,10 @@ func (w *Worktree) Add(path string) (plumbing.Hash, error) { h, err := w.copyFileToStorage(path) if err != nil { - return w.removeIfDeleted(path, h, err) + if os.IsNotExist(err) { + h, err = w.deleteFromIndex(path) + } + return h, err } if s.File(path).Worktree == Unmodified { @@ -266,18 +269,6 @@ 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) { - if 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 {