Skip to content

Commit 6f100eb

Browse files
authored
feat: verify if targetBranch is present in git repo (#511)
* 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 <[email protected]> * chore(test): mocking test for chart.BranchExists() Signed-off-by: Marco Lecheler <[email protected]> * fix(lint): use fmt.Errorf for error msg Signed-off-by: Marco Lecheler <[email protected]> --------- Signed-off-by: Marco Lecheler <[email protected]>
1 parent 49c8f10 commit 6f100eb

File tree

3 files changed

+17
-0
lines changed

3 files changed

+17
-0
lines changed

pkg/chart/chart.go

+8
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ const maxNameLength = 63
4949
//
5050
// ValidateRepository checks that the current working directory is a valid git repository,
5151
// and returns nil if valid.
52+
//
53+
// BranchExists checks whether a given branch exists in the git repository.
5254
type Git interface {
5355
FileExistsOnBranch(file string, remote string, branch string) bool
5456
Show(file string, remote string, branch string) (string, error)
@@ -58,6 +60,7 @@ type Git interface {
5860
ListChangedFilesInDirs(commit string, dirs ...string) ([]string, error)
5961
GetURLForRemote(remote string) (string, error)
6062
ValidateRepository() error
63+
BranchExists(branch string) bool
6164
}
6265

6366
// Helm is the interface that wraps Helm operations
@@ -701,6 +704,11 @@ func (t *Testing) computeMergeBase() (string, error) {
701704
if err != nil {
702705
return "", errors.New("must be in a git repository")
703706
}
707+
708+
if !t.git.BranchExists(t.config.TargetBranch) {
709+
return "", fmt.Errorf("targetBranch '%s' does not exist", t.config.TargetBranch)
710+
}
711+
704712
return t.git.MergeBase(fmt.Sprintf("%s/%s", t.config.Remote, t.config.TargetBranch), t.config.Since)
705713
}
706714

pkg/chart/chart_test.go

+4
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,10 @@ func (g fakeGit) ValidateRepository() error {
7070
return nil
7171
}
7272

73+
func (g fakeGit) BranchExists(branch string) bool {
74+
return true
75+
}
76+
7377
type fakeAccountValidator struct{}
7478

7579
func (v fakeAccountValidator) Validate(repoDomain string, account string) error {

pkg/tool/git.go

+5
Original file line numberDiff line numberDiff line change
@@ -74,3 +74,8 @@ func (g Git) ValidateRepository() error {
7474
_, err := g.exec.RunProcessAndCaptureOutput("git", "rev-parse", "--is-inside-work-tree")
7575
return err
7676
}
77+
78+
func (g Git) BranchExists(branch string) bool {
79+
_, err := g.exec.RunProcessAndCaptureOutput("git", "rev-parse", "--verify", branch)
80+
return err == nil
81+
}

0 commit comments

Comments
 (0)