From a616a03b4fbd94aa9d25b0dbb55b08a73f0cccb1 Mon Sep 17 00:00:00 2001 From: Zorian Motso Date: Wed, 28 Feb 2024 12:01:32 +0200 Subject: [PATCH] fix: Empty project failed status (#43) After updating go-git v5.4.3 -> v5.11.0, an empty commit returns an error. It is due to change https://github.com/go-git/go-git/pull/623 Added property to commit to allowing empty commit. Change-Id: I8bd83e42e282dff78976276039159f1f1e647ad5 --- pkg/git/git.go | 1 + pkg/git/git_test.go | 33 ++++++++++++++++++++++++++++++++- 2 files changed, 33 insertions(+), 1 deletion(-) diff --git a/pkg/git/git.go b/pkg/git/git.go index 7239634..966f9da 100644 --- a/pkg/git/git.go +++ b/pkg/git/git.go @@ -205,6 +205,7 @@ func (*GitProvider) CommitChanges(directory, commitMsg string, ops ...CommitOps) Email: "codebase@edp.local", When: time.Now(), }, + AllowEmptyCommits: option.allowEmptyCommit, }) if err != nil { return fmt.Errorf("failed to commit: %w", err) diff --git a/pkg/git/git_test.go b/pkg/git/git_test.go index 038c0b6..0e7da0b 100644 --- a/pkg/git/git_test.go +++ b/pkg/git/git_test.go @@ -210,6 +210,7 @@ func TestGitProvider_CommitChanges(t *testing.T) { tests := []struct { name string + ops []git.CommitOps initRepo func(t *testing.T) string wantErr require.ErrorAssertionFunc checkRepo func(t *testing.T, dir string) @@ -271,6 +272,36 @@ func TestGitProvider_CommitChanges(t *testing.T) { require.Equalf(t, 0, count, "expected 0 commits, got %d", count) }, }, + { + name: "should create empty commit", + ops: []git.CommitOps{ + git.CommitAllowEmpty(), + }, + initRepo: func(t *testing.T) string { + dir := t.TempDir() + _, err := gogit.PlainInit(dir, false) + require.NoError(t, err) + + return dir + }, + wantErr: require.NoError, + checkRepo: func(t *testing.T, dir string) { + r, err := gogit.PlainOpen(dir) + require.NoError(t, err) + + commits, err := r.CommitObjects() + require.NoError(t, err) + + count := 0 + _ = commits.ForEach(func(*object.Commit) error { + count++ + + return nil + }) + + require.Equalf(t, 1, count, "expected 1 commits, got %d", count) + }, + }, } for _, tt := range tests { @@ -281,7 +312,7 @@ func TestGitProvider_CommitChanges(t *testing.T) { gp := &git.GitProvider{} dir := tt.initRepo(t) - err := gp.CommitChanges(dir, "test commit message") + err := gp.CommitChanges(dir, "test commit message", tt.ops...) tt.wantErr(t, err) tt.checkRepo(t, dir) })