From 7b3fd47344dd679cdf4ce97953b73d0f4dd90f6d Mon Sep 17 00:00:00 2001 From: Marco Lecheler Date: Fri, 6 Jan 2023 20:30:05 +0100 Subject: [PATCH 1/3] feat: verify if targetBranch is present in git repo If a targetBranch is not present in the git repository the `ct list-changed` command will fail. The error message for this is not really meaningful. This commit will validate if the branch exists and throw a error message which the user will understand. closes #330 Signed-off-by: Marco Lecheler --- pkg/chart/chart.go | 8 ++++++++ pkg/tool/git.go | 5 +++++ 2 files changed, 13 insertions(+) diff --git a/pkg/chart/chart.go b/pkg/chart/chart.go index ac575d9a..8dea21aa 100644 --- a/pkg/chart/chart.go +++ b/pkg/chart/chart.go @@ -49,6 +49,8 @@ const maxNameLength = 63 // // ValidateRepository checks that the current working directory is a valid git repository, // and returns nil if valid. +// +// BranchExists checks whether a given branch exists in the git repository. type Git interface { FileExistsOnBranch(file string, remote string, branch string) bool Show(file string, remote string, branch string) (string, error) @@ -58,6 +60,7 @@ type Git interface { ListChangedFilesInDirs(commit string, dirs ...string) ([]string, error) GetURLForRemote(remote string) (string, error) ValidateRepository() error + BranchExists(branch string) bool } // Helm is the interface that wraps Helm operations @@ -703,6 +706,11 @@ func (t *Testing) computeMergeBase() (string, error) { if err != nil { return "", errors.New("must be in a git repository") } + + if !t.git.BranchExists(t.config.TargetBranch) { + return "", errors.New(fmt.Sprintf("targetBranch '%s' does not exist", t.config.TargetBranch)) + } + return t.git.MergeBase(fmt.Sprintf("%s/%s", t.config.Remote, t.config.TargetBranch), t.config.Since) } diff --git a/pkg/tool/git.go b/pkg/tool/git.go index 1fe5551f..7fcb7ce6 100644 --- a/pkg/tool/git.go +++ b/pkg/tool/git.go @@ -74,3 +74,8 @@ func (g Git) ValidateRepository() error { _, err := g.exec.RunProcessAndCaptureOutput("git", "rev-parse", "--is-inside-work-tree") return err } + +func (g Git) BranchExists(branch string) bool { + _, err := g.exec.RunProcessAndCaptureOutput("git", "rev-parse", "--verify", branch) + return err == nil +} From 3a6d79c2199c3f877f6c02d8b22b375ce986b36b Mon Sep 17 00:00:00 2001 From: Marco Lecheler Date: Mon, 9 Jan 2023 22:43:12 +0100 Subject: [PATCH 2/3] chore(test): mocking test for chart.BranchExists() Signed-off-by: Marco Lecheler --- pkg/chart/chart_test.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/pkg/chart/chart_test.go b/pkg/chart/chart_test.go index 78cf8d1f..a45ff829 100644 --- a/pkg/chart/chart_test.go +++ b/pkg/chart/chart_test.go @@ -70,6 +70,10 @@ func (g fakeGit) ValidateRepository() error { return nil } +func (g fakeGit) BranchExists(branch string) bool { + return true +} + type fakeAccountValidator struct{} func (v fakeAccountValidator) Validate(repoDomain string, account string) error { From 34053eedac3b78dc58947fe660cd693790c4ad0f Mon Sep 17 00:00:00 2001 From: Marco Lecheler Date: Mon, 9 Jan 2023 22:44:14 +0100 Subject: [PATCH 3/3] fix(lint): use fmt.Errorf for error msg Signed-off-by: Marco Lecheler --- pkg/chart/chart.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/chart/chart.go b/pkg/chart/chart.go index 8dea21aa..d70b3832 100644 --- a/pkg/chart/chart.go +++ b/pkg/chart/chart.go @@ -708,7 +708,7 @@ func (t *Testing) computeMergeBase() (string, error) { } if !t.git.BranchExists(t.config.TargetBranch) { - return "", errors.New(fmt.Sprintf("targetBranch '%s' does not exist", t.config.TargetBranch)) + return "", fmt.Errorf("targetBranch '%s' does not exist", t.config.TargetBranch) } return t.git.MergeBase(fmt.Sprintf("%s/%s", t.config.Remote, t.config.TargetBranch), t.config.Since)