Repository: lindell/multi-gitter
Pull request #51 (2021-01-15 16:23:49)

fix: fixed presentation of repos with existing repo


Patch:
diff --git a/internal/git/git.go b/internal/git/git.go
index c9f4fca..3465c29 100755
--- a/internal/git/git.go
+++ b/internal/git/git.go
@@ -8,6 +8,7 @@ import (
 
 	"github.com/go-git/go-git/v5/plumbing/object"
 	"github.com/lindell/multi-gitter/internal/domain"
+	"github.com/pkg/errors"
 
 	git "github.com/go-git/go-git/v5"
 	"github.com/go-git/go-git/v5/plumbing"
@@ -35,14 +36,13 @@ func (g *Git) Clone(branchName string) error {
 		RemoteName:    "origin",
 		Depth:         10,
 		ReferenceName: plumbing.NewBranchReferenceName(branchName),
-		SingleBranch:  true,
 	})
 	if err != nil {
-		return err
+		return errors.Wrap(err, "could not clone from the remote")
 	}
 	g.repo = r
 
-	return err
+	return nil
 }
 
 // ChangeBranch changes the branch
diff --git a/internal/multigitter/run.go b/internal/multigitter/run.go
index aa19f95..e863637 100755
--- a/internal/multigitter/run.go
+++ b/internal/multigitter/run.go
@@ -142,7 +142,7 @@ func (r Runner) runSingleRepo(ctx context.Context, repo domain.Repository) error
 
 	featureBranchExist, err := sourceController.BranchExist(r.FeatureBranch)
 	if err != nil {
-		return err
+		return errors.Wrap(err, "could not verify if branch already exist")
 	} else if featureBranchExist {
 		return domain.BranchExistError
 	}
@@ -188,7 +188,7 @@ func (r Runner) runSingleRepo(ctx context.Context, repo domain.Repository) error
 
 	err = sourceController.Push()
 	if err != nil {
-		return err
+		return errors.Wrap(err, "could not push changes")
 	}
 
 	log.Info("Change done, creating pull request")


Test patch:
diff --git a/tests/table_test.go b/tests/table_test.go
index a2a97d3..2b6d6ae 100644
--- a/tests/table_test.go
+++ b/tests/table_test.go
@@ -113,7 +113,7 @@ func TestTable(t *testing.T) {
 			},
 			verify: func(t *testing.T, vcMock *vcmock.VersionController, runData runData) {
 				require.Len(t, vcMock.PullRequests, 0)
-				assert.Contains(t, runData.logOut, `msg="couldn't find remote ref \"refs/heads/custom-base-branch\""`)
+				assert.Contains(t, runData.logOut, `msg="could not clone from the remote: reference not found"`)
 			},
 		},
 
@@ -244,6 +244,41 @@ func TestTable(t *testing.T) {
 				require.Less(t, runData.took.Milliseconds(), int64(600))
 			},
 		},
+
+		{
+			name: "existing head branch",
+			vcCreate: func(t *testing.T) *vcmock.VersionController {
+				repo := createRepo(t, "already-existing-branch", "i like apples")
+				changeBranch(t, repo.Path, "custom-branch-name", true)
+				changeTestFile(t, repo.Path, "i like apple", "test change")
+				changeBranch(t, repo.Path, "master", false)
+				return &vcmock.VersionController{
+					Repositories: []vcmock.Repository{
+						repo,
+						createRepo(t, "should-change", "i like apples"),
+					},
+				}
+			},
+			args: []string{
+				"run",
+				"--author-name", "Test Author",
+				"--author-email", "test@example.com",
+				"-B", "custom-branch-name",
+				"-m", "custom message",
+				path.Join(workingDir, "scripts/changer/main"),
+			},
+			verify: func(t *testing.T, vcMock *vcmock.VersionController, runData runData) {
+				require.Len(t, vcMock.PullRequests, 1)
+				assert.Contains(t, runData.logOut, "Running on 2 repositories")
+				assert.Contains(t, runData.logOut, "Cloning and running script")
+
+				assert.Equal(t, `The new branch does already exist:
+  already-existing-branch
+Repositories with a successful run:
+  should-change
+`, runData.out)
+			},
+		},
 	}
 
 	for _, test := range tests {