Skip to content

Commit

Permalink
Support remotes other than "origin"
Browse files Browse the repository at this point in the history
Signed-off-by: Simon Pasquier <[email protected]>
  • Loading branch information
simonpasquier committed Oct 4, 2019
1 parent 97c626e commit 8f2ac0a
Showing 1 changed file with 26 additions and 9 deletions.
35 changes: 26 additions & 9 deletions pkg/repository/info.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,16 @@ type Info struct {
Version string
}

// shellOutput executes a shell command and returns the trimmed output
// shellOutput executes a shell command and returns the trimmed output.
func shellOutput(cmd string, arg ...string) string {
out, _ := exec.Command(cmd, arg...).Output()
return strings.Trim(string(out), " \n\r")
out, _ := shellOutputWithError(cmd, arg...)
return out
}

// shellOutputWithError executes a shell command and returns the trimmed output and error.
func shellOutputWithError(cmd string, arg ...string) (string, error) {
out, err := exec.Command(cmd, arg...).Output()
return strings.Trim(string(out), " \n\r"), err
}

// NewInfo returns a new Info.
Expand All @@ -59,6 +65,7 @@ func NewInfo(warnf func(error)) (Info, error) {
cmd := exec.Command("git", "rev-parse", "--show-toplevel")
cmd.Stdout, cmd.Stderr = nil, nil
if err := cmd.Run(); err != nil {
// Not a git repository.
repo, err := os.Getwd()
if err != nil {
return info, errors.Wrap(err, "couldn't get current working directory")
Expand All @@ -79,17 +86,27 @@ func NewInfo(warnf func(error)) (Info, error) {
Revision: "non-git",
}
} else {
cmd := exec.Command("git", "config", "--get", "remote.origin.url")
repoURL, err := cmd.Output()
branch, err := shellOutputWithError("git", "rev-parse", "--abbrev-ref", "HEAD")
if err != nil {
return info, errors.Wrap(err, "unable to get the current branch")
}

remote, err := shellOutputWithError("git", "config", "--get", fmt.Sprintf("branch.%s.remote", branch))
if err != nil {
warnf(errors.Wrapf(err, "unable to get the remote for branch %q", branch))
remote = "origin"
}

repoURL, err := shellOutputWithError("git", "config", "--get", fmt.Sprintf("remote.%s.url", remote))
if err != nil {
warnf(errors.Wrap(err, "unable to get repo location info from 'origin' remote"))
warnf(errors.Wrapf(err, "unable to get repository location for remote %q", remote))
}
repo, err := repoLocation(strings.Trim(string(repoURL), " \n\r"))
repo, err := repoLocation(repoURL)
if err != nil {
return info, errors.Wrap(err, "couldn't parse repo location")
return info, errors.Wrapf(err, "couldn't parse repository location: %q", repoURL)
}
info = Info{
Branch: shellOutput("git", "rev-parse", "--abbrev-ref", "HEAD"),
Branch: branch,
Name: filepath.Base(repo),
Owner: filepath.Base(filepath.Dir(repo)),
Repo: repo,
Expand Down

0 comments on commit 8f2ac0a

Please sign in to comment.