Skip to content

Commit

Permalink
feat: Show friendly error message when there is no remote (#116)
Browse files Browse the repository at this point in the history
Currently if there's no remote "origin", it shows "error: git remote:
exit status 2". This change shows "this repository doesn't have a remote
origin".
  • Loading branch information
draftcode authored May 17, 2023
1 parent 94e375a commit b3b37d8
Showing 1 changed file with 12 additions and 1 deletion.
13 changes: 12 additions & 1 deletion internal/git/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ import (
giturls "github.com/whilp/git-urls"
)

var ErrRemoteNotFound = errors.Sentinel("this repository doesn't have a remote origin")

type Repo struct {
repoDir string
log logrus.FieldLogger
Expand Down Expand Up @@ -331,10 +333,19 @@ func (r *Repo) Origin() (*Origin, error) {
// Note: `git remote get-url` gets the "real" URL of the remote (taking
// `insteadOf` from git config into account) whereas `git config --get ...`
// does *not*. Not sure if it matters here.
origin, err := r.Git("remote", "get-url", "origin")
output, err := r.Run(&RunOpts{
Args: []string{"remote", "get-url", "origin"},
})
if err != nil {
return nil, err
}
if output.ExitCode != 0 {
if strings.Contains(string(output.Stderr), "No such remote") {
return nil, ErrRemoteNotFound
}
return nil, errors.New("cannot get the remote of the repository")
}
origin := strings.TrimSpace(string(output.Stdout))
if origin == "" {
return nil, errors.New("origin URL is empty")
}
Expand Down

0 comments on commit b3b37d8

Please sign in to comment.