From 098dc93c5dcd05463ce826a9c1c144394336e802 Mon Sep 17 00:00:00 2001 From: d4x1 <1507509064@qq.com> Date: Tue, 27 Aug 2024 16:16:14 +0800 Subject: [PATCH 1/2] fix(gitextractor): support empty repos --- .../gitextractor/parser/clone_gitcli.go | 35 +++++++++++++++++-- 1 file changed, 33 insertions(+), 2 deletions(-) diff --git a/backend/plugins/gitextractor/parser/clone_gitcli.go b/backend/plugins/gitextractor/parser/clone_gitcli.go index 6dbf5481a0f..a9e708fd35b 100644 --- a/backend/plugins/gitextractor/parser/clone_gitcli.go +++ b/backend/plugins/gitextractor/parser/clone_gitcli.go @@ -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 { @@ -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...) } @@ -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 := string(output) + if errMsg == "" { + errMsg = err.Error() + } + if errMsg == "" { + errMsg = "unknown error" + } + return errMsg +} func sanitizeArgs(args []string) []string { var ret []string From 278869527e8d376009dbc2d8ba5b277cef671662 Mon Sep 17 00:00:00 2001 From: d4x1 <1507509064@qq.com> Date: Tue, 27 Aug 2024 16:25:11 +0800 Subject: [PATCH 2/2] fix(gitextractor): tiny fix --- backend/plugins/gitextractor/parser/clone_gitcli.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/backend/plugins/gitextractor/parser/clone_gitcli.go b/backend/plugins/gitextractor/parser/clone_gitcli.go index a9e708fd35b..adeceb9e358 100644 --- a/backend/plugins/gitextractor/parser/clone_gitcli.go +++ b/backend/plugins/gitextractor/parser/clone_gitcli.go @@ -325,7 +325,7 @@ func (g *GitcliCloner) execCommand(cmd *exec.Cmd) errors.Error { return nil } func generateErrMsg(output []byte, err error) string { - errMsg := string(output) + errMsg := strings.TrimSpace(string(output)) if errMsg == "" { errMsg = err.Error() }