Skip to content
This repository has been archived by the owner on Sep 5, 2019. It is now read-only.

Commit

Permalink
git-init update and fixes
Browse files Browse the repository at this point in the history
- check for errors or at least log them trim spaces from url : if the
- given url contains some spaces (like non-breaking spaces),
  `git-init` may fail with `protocol ' https' is not supported`. This
  fixes that.

Signed-off-by: Vincent Demeester <[email protected]>
  • Loading branch information
vdemeester committed Apr 11, 2019
1 parent e82e9a3 commit fb415d0
Showing 1 changed file with 16 additions and 10 deletions.
26 changes: 16 additions & 10 deletions cmd/git-init/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"os"
"os/exec"
"path/filepath"
"strings"

"github.com/knative/pkg/logging"
"go.uber.org/zap"
Expand All @@ -32,27 +33,27 @@ var (
path = flag.String("path", "", "Path of directory under which git repository will be copied")
)

func run(logger *zap.SugaredLogger, cmd string, args ...string) {
func run(logger *zap.SugaredLogger, cmd string, args ...string) error {
c := exec.Command(cmd, args...)
var output bytes.Buffer
c.Stderr = &output
c.Stdout = &output
if err := c.Run(); err != nil {
logger.Errorf("Error running %v %v: %v\n%v", cmd, args, err, output.String())
return err
}
return nil
}

func runOrFail(logger *zap.SugaredLogger, cmd string, args ...string) error {
func runOrFail(logger *zap.SugaredLogger, cmd string, args ...string) {
c := exec.Command(cmd, args...)
var output bytes.Buffer
c.Stderr = &output
c.Stdout = &output

if err := c.Run(); err != nil {
logger.Errorf("Unexpected error running %v %v: %v\n%v", cmd, args, err, output.String())
return err
logger.Fatalf("Unexpected error running %v %v: %v\n%v", cmd, args, err, output.String())
}
return nil
}

func main() {
Expand All @@ -74,6 +75,9 @@ func main() {
if err != nil {
logger.Errorf("Failed to get current dir", err)
}
if *revision == "" {
*revision = "master"
}

if *path != "" {
runOrFail(logger, "git", "init", *path)
Expand All @@ -87,16 +91,18 @@ func main() {
run(logger, "git", "init")
}

run(logger, "git", "remote", "add", "origin", *url)
err = runOrFail(logger, "git", "fetch", "--depth=1", "--recurse-submodules=yes", "origin", *revision)
if err != nil {
trimedURL := strings.TrimSpace(*url)
runOrFail(logger, "git", "remote", "add", "origin", trimedURL)
if err = run(logger, "git", "fetch", "--depth=1", "--recurse-submodules=yes", "origin", *revision); err != nil {
// Fetch can fail if an old commitid was used so try git pull, performing regardless of error
// as no guarantee that the same error is returned by all git servers gitlab, github etc...
run(logger, "git", "pull", "--recurse-submodules=yes", "origin")
if err := run(logger, "git", "pull", "--recurse-submodules=yes", "origin"); err != nil {
logger.Warnf("Failed to pull origin: %s", err)
}
runOrFail(logger, "git", "checkout", *revision)
} else {
runOrFail(logger, "git", "reset", "--hard", "FETCH_HEAD")
}

logger.Infof("Successfully cloned %q @ %q in path %q", *url, *revision, dir)
logger.Infof("Successfully cloned %q @ %q in path %q", trimedURL, *revision, dir)
}

0 comments on commit fb415d0

Please sign in to comment.