Skip to content

Commit 4cf44b2

Browse files
fix: fixing force push for gitops (#5152)
* fixing force push for gitops * fix * resync conflicts
1 parent d6f4451 commit 4cf44b2

File tree

3 files changed

+9
-3
lines changed

3 files changed

+9
-3
lines changed

pkg/deployment/gitOps/git/GitOpsHelper.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,13 +96,19 @@ func (impl *GitOpsHelper) Pull(repoRoot string) (err error) {
9696
return impl.gitCommandManager.Pull(ctx, repoRoot)
9797
}
9898

99+
const PushErrorMessage = "failed to push some refs"
100+
99101
func (impl GitOpsHelper) CommitAndPushAllChanges(repoRoot, commitMsg, name, emailId string) (commitHash string, err error) {
100102
start := time.Now()
101103
defer func() {
102104
util.TriggerGitOpsMetrics("CommitAndPushAllChanges", "GitService", start, err)
103105
}()
104106
ctx := git.BuildGitContext(context.Background()).WithCredentials(impl.Auth)
105-
return impl.gitCommandManager.CommitAndPush(ctx, repoRoot, commitMsg, name, emailId)
107+
commitHash, err = impl.gitCommandManager.CommitAndPush(ctx, repoRoot, commitMsg, name, emailId)
108+
if err != nil && strings.Contains(err.Error(), PushErrorMessage) {
109+
return commitHash, fmt.Errorf("%s %v", "push failed due to conflicts", err)
110+
}
111+
return commitHash, nil
106112
}
107113

108114
func (impl *GitOpsHelper) pullFromBranch(ctx git.GitContext, rootDir string) (string, string, error) {

pkg/deployment/gitOps/git/commandManager/GitCliManager.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ func (impl *GitCliManagerImpl) add(ctx GitContext, rootDir string) (response, er
132132

133133
func (impl *GitCliManagerImpl) push(ctx GitContext, rootDir string) (response, errMsg string, err error) {
134134
impl.logger.Debugw("git push ", "location", rootDir)
135-
cmd, cancel := impl.createCmdWithContext(ctx, "git", "-C", rootDir, "push", "origin", "master", "--force")
135+
cmd, cancel := impl.createCmdWithContext(ctx, "git", "-C", rootDir, "push", "origin", "master")
136136
defer cancel()
137137
output, errMsg, err := impl.runCommandWithCred(cmd, ctx.auth)
138138
impl.logger.Debugw("git add output", "root", rootDir, "opt", output, "errMsg", errMsg, "error", err)

pkg/deployment/gitOps/git/commandManager/GitCommandBaseManager.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ func (impl *GitManagerBaseImpl) runCommand(cmd *exec.Cmd) (response, errMsg stri
9292
if err != nil {
9393
exErr, ok := err.(*exec.ExitError)
9494
if !ok {
95-
return "", "", err
95+
return "", "", fmt.Errorf("%s %v", outBytes, err)
9696
}
9797
errOutput := string(exErr.Stderr)
9898
return "", errOutput, err

0 commit comments

Comments
 (0)