diff --git a/command/pr.go b/command/pr.go index bd7743f46..924059882 100644 --- a/command/pr.go +++ b/command/pr.go @@ -84,7 +84,7 @@ func prStatus(cmd *cobra.Command, args []string) error { repoOverride, _ := cmd.Flags().GetString("repo") currentPRNumber, currentPRHeadRef, err := prSelectorForCurrentBranch(ctx, baseRepo) if err != nil && repoOverride == "" && err.Error() != "git: not on any branch" { - return err + return fmt.Errorf("could not query for pull request for current branch: %w", err) } prPayload, err := api.PullRequests(apiClient, baseRepo, currentPRNumber, currentPRHeadRef, currentUser) diff --git a/context/context.go b/context/context.go index 6c3bd5aee..23ce74d85 100644 --- a/context/context.go +++ b/context/context.go @@ -184,7 +184,7 @@ func (c *fsContext) Branch() (string, error) { currentBranch, err := git.CurrentBranch() if err != nil { - return "", err + return "", fmt.Errorf("could not determine current branch: %w", err) } c.branch = currentBranch diff --git a/git/git.go b/git/git.go index 37253462a..cdf1e869f 100644 --- a/git/git.go +++ b/git/git.go @@ -21,6 +21,15 @@ func VerifyRef(ref string) bool { // CurrentBranch reads the checked-out branch for the git repository func CurrentBranch() (string, error) { + err := utils.PrepareCmd(GitCommand("log")).Run() + if err != nil { + // this is a hack. + errRe := regexp.MustCompile("your current branch '([^']+)' does not have any commits yet") + matches := errRe.FindAllStringSubmatch(err.Error(), -1) + if len(matches) > 0 && matches[0][1] != "" { + return matches[0][1], nil + } + } // we avoid using `git branch --show-current` for compatibility with git < 2.22 branchCmd := exec.Command("git", "rev-parse", "--abbrev-ref", "HEAD") output, err := utils.PrepareCmd(branchCmd).Output()