Skip to content

Commit 121173c

Browse files
committed
better error reporting + catch for empty repo
1 parent deb7ee6 commit 121173c

File tree

3 files changed

+11
-2
lines changed

3 files changed

+11
-2
lines changed

command/pr.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ func prStatus(cmd *cobra.Command, args []string) error {
8484
repoOverride, _ := cmd.Flags().GetString("repo")
8585
currentPRNumber, currentPRHeadRef, err := prSelectorForCurrentBranch(ctx, baseRepo)
8686
if err != nil && repoOverride == "" && err.Error() != "git: not on any branch" {
87-
return err
87+
return fmt.Errorf("could not query for pull request for current branch: %w", err)
8888
}
8989

9090
prPayload, err := api.PullRequests(apiClient, baseRepo, currentPRNumber, currentPRHeadRef, currentUser)

context/context.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ func (c *fsContext) Branch() (string, error) {
184184

185185
currentBranch, err := git.CurrentBranch()
186186
if err != nil {
187-
return "", err
187+
return "", fmt.Errorf("could not determine current branch: %w", err)
188188
}
189189

190190
c.branch = currentBranch

git/git.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,15 @@ func VerifyRef(ref string) bool {
2121

2222
// CurrentBranch reads the checked-out branch for the git repository
2323
func CurrentBranch() (string, error) {
24+
err := utils.PrepareCmd(GitCommand("log")).Run()
25+
if err != nil {
26+
// this is a hack.
27+
errRe := regexp.MustCompile("your current branch '([^']+)' does not have any commits yet")
28+
matches := errRe.FindAllStringSubmatch(err.Error(), -1)
29+
if len(matches) > 0 && matches[0][1] != "" {
30+
return matches[0][1], nil
31+
}
32+
}
2433
// we avoid using `git branch --show-current` for compatibility with git < 2.22
2534
branchCmd := exec.Command("git", "rev-parse", "--abbrev-ref", "HEAD")
2635
output, err := utils.PrepareCmd(branchCmd).Output()

0 commit comments

Comments
 (0)