Skip to content

Commit

Permalink
Merge pull request #90 from devtron-labs/fix-local-ref-error
Browse files Browse the repository at this point in the history
fix: unable to fetch latest commit
  • Loading branch information
iamayushm authored Feb 10, 2024
2 parents 602eaea + b49fc6f commit 7e88791
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 6 deletions.
28 changes: 22 additions & 6 deletions pkg/git/GitBaseManager.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,22 @@ func (impl *GitManagerBaseImpl) Fetch(gitCtx GitContext, rootDir string) (respon
cmd, cancel := impl.createCmdWithContext(gitCtx, "git", "-C", rootDir, "fetch", "origin", "--tags", "--force")
defer cancel()
output, errMsg, err := impl.runCommandWithCred(cmd, gitCtx.Username, gitCtx.Password)
if strings.Contains(output, LOCK_REF_MESSAGE) {
impl.logger.Info("error in fetch, pruning local refs and retrying", "rootDir", rootDir)
// running git remote prune origin and retrying fetch. gitHub issue - https://github.com/devtron-labs/devtron/issues/4605
pruneCmd, pruneCmdCancel := impl.createCmdWithContext(gitCtx, "git", "-C", rootDir, "remote", "prune", "origin")
pruneOutput, pruneMsg, pruneErr := impl.runCommandWithCred(pruneCmd, gitCtx.Username, gitCtx.Password)
defer pruneCmdCancel()
if pruneErr != nil {
impl.logger.Errorw("error in pruning local refs that do not exist at remote")
return pruneOutput, pruneMsg, pruneErr
}

retryFetchCmd, retryFetchCancel := impl.createCmdWithContext(gitCtx, "git", "-C", rootDir, "fetch", "origin", "--tags", "--force")
defer retryFetchCancel()

output, errMsg, err = impl.runCommandWithCred(retryFetchCmd, gitCtx.Username, gitCtx.Password)
}
impl.logger.Debugw("fetch output", "root", rootDir, "opt", output, "errMsg", errMsg, "error", err)
return output, errMsg, err
}
Expand Down Expand Up @@ -145,21 +161,21 @@ func (impl *GitManagerBaseImpl) runCommandWithCred(cmd *exec.Cmd, userName, pass
func (impl *GitManagerBaseImpl) runCommand(cmd *exec.Cmd) (response, errMsg string, err error) {
cmd.Env = append(cmd.Env, "HOME=/dev/null")
outBytes, err := cmd.CombinedOutput()
output := string(outBytes)
output = strings.TrimSpace(output)
if err != nil {
impl.logger.Errorw("error in git cli operation", "msg", string(outBytes), "err", err)
exErr, ok := err.(*exec.ExitError)
if !ok {
return "", string(outBytes), err
return output, string(outBytes), err
}
if strings.Contains(string(outBytes), AUTHENTICATION_FAILED_ERROR) {
if strings.Contains(output, AUTHENTICATION_FAILED_ERROR) {
impl.logger.Errorw("authentication failed", "msg", string(outBytes), "err", err.Error())
return "", "authentication failed", errors.New("authentication failed")
return output, "authentication failed", errors.New("authentication failed")
}
errOutput := string(exErr.Stderr)
return "", errOutput, err
return output, errOutput, err
}
output := string(outBytes)
output = strings.TrimSpace(output)
return output, "", nil
}

Expand Down
1 change: 1 addition & 0 deletions pkg/git/GitCliManager.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ func NewGitCliManagerImpl(baseManager GitManagerBase, logger *zap.SugaredLogger)
const (
GIT_ASK_PASS = "/git-ask-pass.sh"
AUTHENTICATION_FAILED_ERROR = "Authentication failed"
LOCK_REF_MESSAGE = "cannot lock ref"
)

func (impl *GitCliManagerImpl) Init(gitCtx GitContext, rootDir string, remoteUrl string, isBare bool) error {
Expand Down

0 comments on commit 7e88791

Please sign in to comment.