From a325229a78e2a118889a6902f7efe6435338f76c Mon Sep 17 00:00:00 2001 From: Cheng Fang Date: Fri, 1 Nov 2024 10:45:50 -0400 Subject: [PATCH] fix: #895 Image Updater continuously force-pushes new commits when checkout branch is specified Signed-off-by: Cheng Fang --- pkg/argocd/git.go | 26 +++++++++++++++++++------- 1 file changed, 19 insertions(+), 7 deletions(-) diff --git a/pkg/argocd/git.go b/pkg/argocd/git.go index 13e62b66..7d30cec3 100644 --- a/pkg/argocd/git.go +++ b/pkg/argocd/git.go @@ -182,10 +182,6 @@ func commitChangesGit(app *v1alpha1.Application, wbc *WriteBackConfig, changeLis return err } } - err = gitC.ShallowFetch(checkOutBranch, 1) - if err != nil { - return err - } // The push branch is by default the same as the checkout branch, unless // specified after a : separator git-branch annotation, in which case a @@ -196,14 +192,30 @@ func commitChangesGit(app *v1alpha1.Application, wbc *WriteBackConfig, changeLis if wbc.GitWriteBranch != "" { logCtx.Debugf("Using branch template: %s", wbc.GitWriteBranch) pushBranch = TemplateBranchName(wbc.GitWriteBranch, changeList) - if pushBranch != "" { + if pushBranch == "" { + return fmt.Errorf("Git branch name could not be created from the template: %s", wbc.GitWriteBranch) + } + } + + // If the pushBranch already exists in the remote origin, directly use it. + // Otherwise, create the new pushBranch from checkoutBranch + if checkOutBranch != pushBranch { + fetchErr := gitC.ShallowFetch(pushBranch, 1) + if fetchErr != nil { + err = gitC.ShallowFetch(checkOutBranch, 1) + if err != nil { + return err + } logCtx.Debugf("Creating branch '%s' and using that for push operations", pushBranch) err = gitC.Branch(checkOutBranch, pushBranch) if err != nil { return err } - } else { - return fmt.Errorf("Git branch name could not be created from the template: %s", wbc.GitWriteBranch) + } + } else { + err = gitC.ShallowFetch(checkOutBranch, 1) + if err != nil { + return err } }