From a513415283c4628259c016587858fe56d7b0fa13 Mon Sep 17 00:00:00 2001 From: Paulo Gomes Date: Sat, 3 Dec 2022 16:34:35 +0000 Subject: 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 --- worktree_commit_test.go | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) (limited to 'worktree_commit_test.go') diff --git a/worktree_commit_test.go b/worktree_commit_test.go index 097c6e5..bfeb81d 100644 --- a/worktree_commit_test.go +++ b/worktree_commit_test.go @@ -26,12 +26,18 @@ import ( ) func (s *WorktreeSuite) TestCommitEmptyOptions(c *C) { - r, err := Init(memory.NewStorage(), memfs.New()) + fs := memfs.New() + r, err := Init(memory.NewStorage(), fs) c.Assert(err, IsNil) w, err := r.Worktree() c.Assert(err, IsNil) + util.WriteFile(fs, "foo", []byte("foo"), 0644) + + _, err = w.Add("foo") + c.Assert(err, IsNil) + hash, err := w.Commit("foo", &CommitOptions{}) c.Assert(err, IsNil) c.Assert(hash.IsZero(), Equals, false) @@ -65,6 +71,24 @@ func (s *WorktreeSuite) TestCommitInitial(c *C) { assertStorageStatus(c, r, 1, 1, 1, expected) } +func (s *WorktreeSuite) TestNothingToCommit(c *C) { + expected := plumbing.NewHash("838ea833ce893e8555907e5ef224aa076f5e274a") + + r, err := Init(memory.NewStorage(), memfs.New()) + c.Assert(err, IsNil) + + w, err := r.Worktree() + 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) + + hash, err = w.Commit("enable empty commits\n", &CommitOptions{Author: defaultSignature(), AllowEmptyCommits: true}) + c.Assert(hash, Equals, expected) + c.Assert(err, IsNil) +} + func (s *WorktreeSuite) TestCommitParent(c *C) { expected := plumbing.NewHash("ef3ca05477530b37f48564be33ddd48063fc7a22") -- cgit