Skip to content

Commit

Permalink
Show the git output when clone or fetch fail. (#188)
Browse files Browse the repository at this point in the history
Show the git output when clone or fetch fail in order to have
more information on the nature of the failure.

Related to #185.
  • Loading branch information
bfabio authored Sep 28, 2020
1 parent 15ce287 commit dc475e0
Showing 1 changed file with 7 additions and 9 deletions.
16 changes: 7 additions & 9 deletions crawler/crawler/cloneRepository.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package crawler

import (
"errors"
"fmt"
"os"
"os/exec"
"path/filepath"
Expand All @@ -28,26 +29,23 @@ func CloneRepository(domain Domain, hostname, name, gitURL, gitBranch, index str
// If folder already exists it will do a fetch instead of a clone.
if _, err := os.Stat(path); !os.IsNotExist(err) {
// Command is: git fetch --all
cmd := exec.Command("git", "-C", path, "fetch", "--all") // nolint: gas
err := cmd.Run()
out, err := exec.Command("git", "-C", path, "fetch", "--all").CombinedOutput() // nolint: gas
if err != nil {
return errors.New("cannot git pull the repository: " + err.Error())
return errors.New(fmt.Sprintf("cannot git pull the repository: %s: %s", err.Error(), out))
}
// Command is: git reset --hard origin/<branch_name>
cmd = exec.Command("git", "-C", path, "reset", "--hard", "origin/"+gitBranch) // nolint: gas
err = cmd.Run()
out, err = exec.Command("git", "-C", path, "reset", "--hard", "origin/"+gitBranch).CombinedOutput() // nolint: gas
if err != nil {
return errors.New("cannot git pull the repository: " + err.Error())
return errors.New(fmt.Sprintf("cannot git pull the repository: %s: %s", err.Error(), out))
}
return err
}

// Clone the repository using the external command "git".
// Command is: git clone -b <branch> <remote_repo>
cmd := exec.Command("git", "clone", "-b", gitBranch, gitURL, path) // nolint: gas
err := cmd.Run()
out, err := exec.Command("git", "clone", "-b", gitBranch, gitURL, path).CombinedOutput() // nolint: gas
if err != nil {
return errors.New("cannot git clone the repository: " + err.Error())
return errors.New(fmt.Sprintf("cannot git clone the repository: %s: %s", err.Error(), out))
}

metrics.GetCounter("repository_cloned", index).Inc()
Expand Down

0 comments on commit dc475e0

Please sign in to comment.