aboutsummaryrefslogtreecommitdiffstats
path: root/worktree_commit_test.go
diff options
context:
space:
mode:
authorPaulo Gomes <pjbgf@linux.com>2024-05-22 21:09:35 +0000
committerGitHub <noreply@github.com>2024-05-22 21:09:35 +0000
commitc7feada87363f96c224214ba309efd5cf5a886fc (patch)
tree81b33ed60af5532c3e5887888fc853902b8839f0 /worktree_commit_test.go
parent7ff5a5d839262bfee69a47d315a624943f812d6b (diff)
parent746141efcf4c6dcab3063a6e54d6eb20a3004969 (diff)
downloadgo-git-c7feada87363f96c224214ba309efd5cf5a886fc.tar.gz
Merge pull request #1050 from onee-only/fix-empty-commit
git: worktree_commit, Modify checking empty commit. Fixes #723
Diffstat (limited to 'worktree_commit_test.go')
-rw-r--r--worktree_commit_test.go121
1 files changed, 118 insertions, 3 deletions
diff --git a/worktree_commit_test.go b/worktree_commit_test.go
index ae8f841..e028fac 100644
--- a/worktree_commit_test.go
+++ b/worktree_commit_test.go
@@ -89,6 +89,56 @@ func (s *WorktreeSuite) TestNothingToCommit(c *C) {
c.Assert(err, IsNil)
}
+func (s *WorktreeSuite) TestNothingToCommitNonEmptyRepo(c *C) {
+ fs := memfs.New()
+ r, err := Init(memory.NewStorage(), fs)
+ c.Assert(err, IsNil)
+
+ w, err := r.Worktree()
+ c.Assert(err, IsNil)
+
+ err = util.WriteFile(fs, "foo", []byte("foo"), 0644)
+ c.Assert(err, IsNil)
+
+ w.Add("foo")
+ _, err = w.Commit("previous commit\n", &CommitOptions{Author: defaultSignature()})
+ c.Assert(err, IsNil)
+
+ hash, err := w.Commit("failed empty commit\n", &CommitOptions{Author: defaultSignature()})
+ c.Assert(hash, Equals, plumbing.ZeroHash)
+ c.Assert(err, Equals, ErrEmptyCommit)
+
+ _, err = w.Commit("enable empty commits\n", &CommitOptions{Author: defaultSignature(), AllowEmptyCommits: true})
+ c.Assert(err, IsNil)
+}
+
+func (s *WorktreeSuite) TestRemoveAndCommitToMakeEmptyRepo(c *C) {
+ fs := memfs.New()
+ r, err := Init(memory.NewStorage(), fs)
+ c.Assert(err, IsNil)
+
+ w, err := r.Worktree()
+ c.Assert(err, IsNil)
+
+ err = util.WriteFile(fs, "foo", []byte("foo"), 0644)
+ c.Assert(err, IsNil)
+
+ _, err = w.Add("foo")
+ c.Assert(err, IsNil)
+
+ _, err = w.Commit("Add in Repo\n", &CommitOptions{Author: defaultSignature()})
+ c.Assert(err, IsNil)
+
+ err = fs.Remove("foo")
+ c.Assert(err, IsNil)
+
+ _, err = w.Add("foo")
+ c.Assert(err, IsNil)
+
+ _, err = w.Commit("Remove foo\n", &CommitOptions{Author: defaultSignature()})
+ c.Assert(err, IsNil)
+}
+
func (s *WorktreeSuite) TestCommitParent(c *C) {
expected := plumbing.NewHash("ef3ca05477530b37f48564be33ddd48063fc7a22")
@@ -101,7 +151,8 @@ func (s *WorktreeSuite) TestCommitParent(c *C) {
err := w.Checkout(&CheckoutOptions{})
c.Assert(err, IsNil)
- util.WriteFile(fs, "foo", []byte("foo"), 0644)
+ err = util.WriteFile(fs, "foo", []byte("foo"), 0644)
+ c.Assert(err, IsNil)
_, err = w.Add("foo")
c.Assert(err, IsNil)
@@ -113,7 +164,42 @@ func (s *WorktreeSuite) TestCommitParent(c *C) {
assertStorageStatus(c, s.Repository, 13, 11, 10, expected)
}
-func (s *WorktreeSuite) TestCommitAmend(c *C) {
+func (s *WorktreeSuite) TestCommitAmendWithoutChanges(c *C) {
+ fs := memfs.New()
+ w := &Worktree{
+ r: s.Repository,
+ Filesystem: fs,
+ }
+
+ err := w.Checkout(&CheckoutOptions{})
+ c.Assert(err, IsNil)
+
+ err = util.WriteFile(fs, "foo", []byte("foo"), 0644)
+ c.Assert(err, IsNil)
+
+ _, err = w.Add("foo")
+ c.Assert(err, IsNil)
+
+ prevHash, err := w.Commit("foo\n", &CommitOptions{Author: defaultSignature()})
+ c.Assert(err, IsNil)
+
+ amendedHash, err := w.Commit("foo\n", &CommitOptions{Author: defaultSignature(), Amend: true})
+ c.Assert(err, IsNil)
+
+ headRef, err := w.r.Head()
+ c.Assert(err, IsNil)
+
+ c.Assert(amendedHash, Equals, headRef.Hash())
+ c.Assert(amendedHash, Equals, prevHash)
+
+ commit, err := w.r.CommitObject(headRef.Hash())
+ c.Assert(err, IsNil)
+ c.Assert(commit.Message, Equals, "foo\n")
+
+ assertStorageStatus(c, s.Repository, 13, 11, 10, amendedHash)
+}
+
+func (s *WorktreeSuite) TestCommitAmendWithChanges(c *C) {
fs := memfs.New()
w := &Worktree{
r: s.Repository,
@@ -164,6 +250,34 @@ func (s *WorktreeSuite) TestCommitAmend(c *C) {
assertStorageStatus(c, s.Repository, 14, 12, 11, amendedHash)
}
+func (s *WorktreeSuite) TestCommitAmendNothingToCommit(c *C) {
+ fs := memfs.New()
+ w := &Worktree{
+ r: s.Repository,
+ Filesystem: fs,
+ }
+
+ err := w.Checkout(&CheckoutOptions{})
+ c.Assert(err, IsNil)
+
+ err = util.WriteFile(fs, "foo", []byte("foo"), 0644)
+ c.Assert(err, IsNil)
+
+ _, err = w.Add("foo")
+ c.Assert(err, IsNil)
+
+ prevHash, err := w.Commit("foo\n", &CommitOptions{Author: defaultSignature()})
+ c.Assert(err, IsNil)
+
+ _, err = w.Commit("bar\n", &CommitOptions{Author: defaultSignature(), AllowEmptyCommits: true})
+ c.Assert(err, IsNil)
+
+ amendedHash, err := w.Commit("foo\n", &CommitOptions{Author: defaultSignature(), Amend: true})
+ c.Log(prevHash, amendedHash)
+ c.Assert(err, Equals, ErrEmptyCommit)
+ c.Assert(amendedHash, Equals, plumbing.ZeroHash)
+}
+
func (s *WorktreeSuite) TestAddAndCommitWithSkipStatus(c *C) {
expected := plumbing.NewHash("375a3808ffde7f129cdd3c8c252fd0fe37cfd13b")
@@ -258,7 +372,8 @@ func (s *WorktreeSuite) TestAddAndCommitWithSkipStatusPathNotModified(c *C) {
c.Assert(foo.Worktree, Equals, Untracked)
hash, err = w.Commit("commit with no changes\n", &CommitOptions{
- Author: defaultSignature(),
+ Author: defaultSignature(),
+ AllowEmptyCommits: true,
})
c.Assert(hash, Equals, expected2)
c.Assert(err, IsNil)