Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

improve error output of git commands #1958

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
31 changes: 22 additions & 9 deletions pkg/transformer/openshift/utils.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package openshift

import (
"github.com/pkg/errors"
"os/exec"
"strings"
)
Expand Down Expand Up @@ -34,12 +35,16 @@ func GetImageTag(image string) string {
func GetAbsBuildContext(context string) (string, error) {
cmd := exec.Command("git", "rev-parse", "--show-prefix")
cmd.Dir = context
out, err := cmd.Output()
var out strings.Builder
var stderr strings.Builder
cmd.Stdout = &out
cmd.Stderr = &stderr
err := cmd.Run()
if err != nil {
return "", err
return "", errors.New(stderr.String())
}
//convert output of command to string
contextDir := strings.Trim(string(out), "\n")
contextDir := strings.Trim(out.String(), "\n")
return contextDir, nil
}

Expand All @@ -53,11 +58,15 @@ func HasGitBinary() bool {
func GetGitCurrentRemoteURL(composeFileDir string) (string, error) {
cmd := exec.Command("git", "ls-remote", "--get-url")
cmd.Dir = composeFileDir
out, err := cmd.Output()
var out strings.Builder
var stderr strings.Builder
cmd.Stdout = &out
cmd.Stderr = &stderr
err := cmd.Run()
if err != nil {
return "", err
return "", errors.New(stderr.String())
}
url := strings.TrimRight(string(out), "\n")
url := strings.TrimRight(out.String(), "\n")
if !strings.HasSuffix(url, ".git") {
url += ".git"
}
Expand All @@ -68,9 +77,13 @@ func GetGitCurrentRemoteURL(composeFileDir string) (string, error) {
func GetGitCurrentBranch(composeFileDir string) (string, error) {
cmd := exec.Command("git", "rev-parse", "--abbrev-ref", "HEAD")
cmd.Dir = composeFileDir
out, err := cmd.Output()
var out strings.Builder
var stderr strings.Builder
cmd.Stdout = &out
cmd.Stderr = &stderr
err := cmd.Run()
if err != nil {
return "", err
return "", errors.New(stderr.String())
}
return strings.TrimRight(string(out), "\n"), nil
return strings.TrimRight(out.String(), "\n"), nil
}
Loading