diff options
author | Paulo Gomes <pjbgf@linux.com> | 2022-12-03 16:34:35 +0000 |
---|---|---|
committer | Paulo Gomes <pjbgf@linux.com> | 2022-12-03 16:34:35 +0000 |
commit | a513415283c4628259c016587858fe56d7b0fa13 (patch) | |
tree | c39b970b1403d10693795ad6d61dd1ea59db4a2d /worktree_test.go | |
parent | 3e07c5030b4e3b2fcbcb461f9f6b23212f978335 (diff) | |
download | go-git-a513415283c4628259c016587858fe56d7b0fa13.tar.gz |
Return error instead of creating empty commits
BuildTree now returns an ErrEmptyCommit error, when there are no
changes to be committed. This can be opted-out via
CommitOptions.AllowEmptyCommits.
This is a breaking change which enables applications to detect when
empty commits are to be created.
Some instances in which this can occur is when the fs (e.g. `billy/osfs`)
make changes to the underlying files, causing a conflict between what
the previous Git worktree state was, and the current state. Changes to
the fs implementations are orthogonal to this, and will be dealt with
separately.
The new behaviour aligns with the Git CLI, in which empty commits
returns the error message: 'nothing to commit, working tree clean'.
Signed-off-by: Paulo Gomes <pjbgf@linux.com>
Diffstat (limited to 'worktree_test.go')
-rw-r--r-- | worktree_test.go | 36 |
1 files changed, 33 insertions, 3 deletions
diff --git a/worktree_test.go b/worktree_test.go index 4a14126..4c06333 100644 --- a/worktree_test.go +++ b/worktree_test.go @@ -3,7 +3,6 @@ package git import ( "bytes" "context" - "errors" "io" "io/ioutil" "os" @@ -2167,6 +2166,8 @@ func (s *WorktreeSuite) TestGrep(c *C) { } func (s *WorktreeSuite) TestAddAndCommit(c *C) { + expectedFiles := 2 + dir, clean := s.TemporalDir() defer clean() @@ -2176,17 +2177,23 @@ func (s *WorktreeSuite) TestAddAndCommit(c *C) { w, err := repo.Worktree() c.Assert(err, IsNil) + os.WriteFile(filepath.Join(dir, "foo"), []byte("bar"), 0o644) + os.WriteFile(filepath.Join(dir, "bar"), []byte("foo"), 0o644) + _, err = w.Add(".") c.Assert(err, IsNil) - w.Commit("Test Add And Commit", &CommitOptions{Author: &object.Signature{ + _, err = w.Commit("Test Add And Commit", &CommitOptions{Author: &object.Signature{ Name: "foo", Email: "foo@foo.foo", When: time.Now(), }}) + c.Assert(err, IsNil) iter, err := w.r.Log(&LogOptions{}) c.Assert(err, IsNil) + + filesFound := 0 err = iter.ForEach(func(c *object.Commit) error { files, err := c.Files() if err != nil { @@ -2194,11 +2201,34 @@ func (s *WorktreeSuite) TestAddAndCommit(c *C) { } err = files.ForEach(func(f *object.File) error { - return errors.New("Expected no files, got at least 1") + filesFound++ + return nil }) return err }) c.Assert(err, IsNil) + c.Assert(filesFound, Equals, expectedFiles) +} + +func (s *WorktreeSuite) TestAddAndCommitEmpty(c *C) { + dir, clean := s.TemporalDir() + defer clean() + + repo, err := PlainInit(dir, false) + c.Assert(err, IsNil) + + w, err := repo.Worktree() + c.Assert(err, IsNil) + + _, err = w.Add(".") + c.Assert(err, IsNil) + + _, err = w.Commit("Test Add And Commit", &CommitOptions{Author: &object.Signature{ + Name: "foo", + Email: "foo@foo.foo", + When: time.Now(), + }}) + c.Assert(err, Equals, ErrEmptyCommit) } func (s *WorktreeSuite) TestLinkedWorktree(c *C) { |