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

fix(gitextractor): support empty repos #7963

Merged
merged 2 commits into from
Aug 27, 2024
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: 33 additions & 2 deletions backend/plugins/gitextractor/parser/clone_gitcli.go
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ func (g *GitcliCloner) doubleClone() errors.Error {
return err
}
g.localDir = backup
// step 2: perform shallow clone aginst the intermediary dir
// step 2: perform shallow clone against the intermediary dir
backup = g.remoteUrl
g.remoteUrl = fmt.Sprintf("file://%s", intermediaryDir) // the file:// prefix is required for shallow clone to work
if err := g.shallowClone(); err != nil {
Expand All @@ -274,10 +274,30 @@ func (g *GitcliCloner) gitClone(args ...string) errors.Error {
}

func (g *GitcliCloner) gitFetch(args ...string) errors.Error {
empty, err := g.repoIsEmpty(args...)
if err != nil {
g.logger.Error(err, "repo is empty")
return err
}
if empty {
g.logger.Info("repo is empty, doesn't need to fetch")
return nil
}
args = append(args, g.syncArgs...)
return g.git(g.syncEnvs, g.localDir, "fetch", args...)
}

func (g *GitcliCloner) repoIsEmpty(args ...string) (bool, errors.Error) {
// try to run command: git log
// if repo is empty, it will return an error
err := g.git(g.syncEnvs, g.localDir, "log")
if err != nil {
g.logger.Warn(err, "git log failed")
return true, nil
}
return false, nil
}

func (g *GitcliCloner) gitCmd(gitcmd string, args ...string) errors.Error {
return g.git(nil, g.localDir, gitcmd, args...)
}
Expand All @@ -294,15 +314,26 @@ func (g *GitcliCloner) git(env []string, dir string, gitcmd string, args ...stri
func (g *GitcliCloner) execCommand(cmd *exec.Cmd) errors.Error {
output, err := cmd.CombinedOutput()
if err != nil {
g.logger.Debug("err: %v, output: %s", err, string(output))
outputString := string(output)
if strings.Contains(outputString, "fatal: error processing shallow info: 4") ||
strings.Contains(outputString, "fatal: the remote end hung up unexpectedly") {
return ErrNoData
}
return errors.Default.New(fmt.Sprintf("git cmd %v in %s failed: %s", sanitizeArgs(cmd.Args), cmd.Dir, outputString))
return errors.Default.New(fmt.Sprintf("git cmd %v in %s failed: %s", sanitizeArgs(cmd.Args), cmd.Dir, generateErrMsg(output, err)))
}
return nil
}
func generateErrMsg(output []byte, err error) string {
errMsg := strings.TrimSpace(string(output))
if errMsg == "" {
errMsg = err.Error()
}
if errMsg == "" {
errMsg = "unknown error"
}
return errMsg
}

func sanitizeArgs(args []string) []string {
var ret []string
Expand Down
Loading