Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 {
// default to origin.
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