diff --git a/pkg/chart/chart.go b/pkg/chart/chart.go index ac575d9a..d70b3832 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 "", 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) } 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 { 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 +}