@@ -234,15 +234,18 @@ func (r *ImageUpdateAutomationReconciler) Reconcile(ctx context.Context, req ctr
234234 }
235235 }()
236236
237+ // pushBranch contains the branch name the commit needs to be pushed to.
238+ // It takes the value of the push branch if one is specified or, if the push
239+ // config is nil, then it takes the value of the checkout branch if possible.
237240 var pushBranch string
238241 var switchBranch bool
239- if gitSpec .Push != nil {
242+ if gitSpec .Push != nil && gitSpec .Push .Branch != "" {
243+ pushBranch = gitSpec .Push .Branch
244+ tracelog .Info ("using push branch from .spec.push.branch" , "branch" , pushBranch )
240245 // We only need to switch branches when a branch has been specified in
241246 // the push spec and it is different than the one in the checkout ref.
242- if gitSpec .Push .Branch != "" && gitSpec .Push .Branch != checkoutRef .Branch {
243- pushBranch = gitSpec .Push .Branch
247+ if gitSpec .Push .Branch != checkoutRef .Branch {
244248 switchBranch = true
245- tracelog .Info ("using push branch from .spec.push.branch" , "branch" , pushBranch )
246249 }
247250 } else {
248251 // Here's where it gets constrained. If there's no push branch
@@ -402,25 +405,11 @@ func (r *ImageUpdateAutomationReconciler) Reconcile(ctx context.Context, req ctr
402405 pushCtx , cancel := context .WithTimeout (ctx , origin .Spec .Timeout .Duration )
403406 defer cancel ()
404407
405- var pushToBranch bool
406- var pushWithRefspec bool
407- // If a refspec is specified, then we need to perform a push using
408- // that refspec.
409- if gitSpec .Push != nil && gitSpec .Push .Refspec != "" {
410- pushWithRefspec = true
411- }
412- // We need to push the commit to the push branch if one was specified, or if
413- // no push config was specified, then we need to push to the branch we checked
414- // out to.
415- if (gitSpec .Push != nil && gitSpec .Push .Branch != "" ) || gitSpec .Push == nil {
416- pushToBranch = true
417- }
418-
419408 var pushConfig repository.PushConfig
420409 if gitSpec .Push != nil {
421410 pushConfig .Options = gitSpec .Push .Options
422411 }
423- if pushToBranch {
412+ if pushBranch != "" {
424413 // If the force push feature flag is true and we are pushing to a
425414 // different branch than the one we checked out to, then force push
426415 // these changes.
@@ -433,17 +422,17 @@ func (r *ImageUpdateAutomationReconciler) Reconcile(ctx context.Context, req ctr
433422 return failWithError (err )
434423 }
435424 log .Info ("pushed commit to origin" , "revision" , rev , "branch" , pushBranch )
436- statusMessage .WriteString (fmt .Sprintf ("commited and pushed commit '%s' to branch '%s'" , rev , pushBranch ))
425+ statusMessage .WriteString (fmt .Sprintf ("committed and pushed commit '%s' to branch '%s'" , rev , pushBranch ))
437426 }
438427
439- if pushWithRefspec {
428+ if gitSpec . Push != nil && gitSpec . Push . Refspec != "" {
440429 pushConfig .Refspecs = []string {gitSpec .Push .Refspec }
441430 if err := gitClient .Push (pushCtx , pushConfig ); err != nil {
442431 return failWithError (err )
443432 }
444433 log .Info ("pushed commit to origin" , "revision" , rev , "refspec" , gitSpec .Push .Refspec )
445434
446- if pushToBranch {
435+ if statusMessage . Len () > 0 {
447436 statusMessage .WriteString (fmt .Sprintf (" and using refspec '%s'" , gitSpec .Push .Refspec ))
448437 } else {
449438 statusMessage .WriteString (fmt .Sprintf ("committed and pushed commit '%s' using refspec '%s'" , rev , gitSpec .Push .Refspec ))
0 commit comments