better error reporting + catch for empty repo

This commit is contained in:
vilmibm 2020-03-23 15:21:58 -05:00
parent deb7ee6aa4
commit 121173c330
3 changed files with 11 additions and 2 deletions

View file

@ -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)

View file

@ -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

View file

@ -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()