Skip to content

Commit

Permalink
Fix regression on push branches
Browse files Browse the repository at this point in the history
Fixes regression in which IAC will fail to update push branch
when the push branch already exists and checkout branch is
ahead.

The reconciliation errors with: 'cannot push because a reference
that you are trying to update on the remote contains commits
that are not present locally.'

Regression introduced on fluxcd#330.

Signed-off-by: Paulo Gomes <[email protected]>
  • Loading branch information
Paulo Gomes committed May 27, 2022
1 parent 4acaa84 commit 48bcca5
Showing 1 changed file with 14 additions and 0 deletions.
14 changes: 14 additions & 0 deletions controllers/imageupdateautomation_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -735,6 +735,12 @@ var errRemoteBranchMissing = errors.New("remote branch missing")
// switchToBranch switches to a branch after fetching latest from upstream.
// If the branch does not exist, it is created using the head as the starting point.
func switchToBranch(repo *libgit2.Repository, ctx context.Context, branch string, access repoAccess) error {
origin, err := repo.Remotes.Lookup(originRemote)
if err != nil {
return fmt.Errorf("cannot lookup remote: %w", err)
}
defer origin.Free()

callbacks := access.remoteCallbacks(ctx)
if managed.Enabled() {
// Override callbacks with dummy ones as they are not needed within Managed Transport.
Expand All @@ -743,6 +749,14 @@ func switchToBranch(repo *libgit2.Repository, ctx context.Context, branch string
}

branchRef := fmt.Sprintf("origin/%s", branch)
// Force the fetching of the remote branch.
err = origin.Fetch([]string{branch}, &libgit2.FetchOptions{
RemoteCallbacks: callbacks,
}, "")
if err != nil {
return fmt.Errorf("cannot fetch remote branch: %w", err)
}

remoteBranch, err := repo.LookupBranch(branchRef, libgit2.BranchRemote)
if err != nil && !libgit2.IsErrorCode(err, libgit2.ErrorCodeNotFound) {
return err
Expand Down

0 comments on commit 48bcca5

Please sign in to comment.