fix(steps/git): use force-fetch refspec so force-pushed branches are picked up
Default git fetch refspec refuses to update refs whose upstream SHA diverges from the local copy. When a branch is force-pushed (e.g. our smoke-test pushes of master, our re-applies of GITHUB_TOKEN fix), the local remote-tracking ref points at the old SHA and git refuses to overwrite it. This shows up as 'cannot lock ref ... is at <old> but expected <new>' and fails the entire clone step. Adding '+refs/heads/*:refs/remotes/<remote>/*' to each remote's refspec lets the fetch force-update the remote-tracking refs. Each remote's existing refspec is preserved; the force-refspec is appended only if not already present.
This commit is contained in:
@@ -103,13 +103,17 @@ class GitClone(AbstractStep[str]):
|
||||
remote.set_url(auth_url)
|
||||
else:
|
||||
remote = repo.create_remote(remote_name, auth_url)
|
||||
force_refspec = f"+refs/heads/*:refs/remotes/{remote_name}/*"
|
||||
existing_refspecs = list(remote.refspec)
|
||||
if force_refspec not in existing_refspecs:
|
||||
remote.set_refspec(remote.refspec + (force_refspec,))
|
||||
if remote_name == "origin" and self.branchNamePrefix != "":
|
||||
with repo.config_writer() as config:
|
||||
config.set_value('remote "origin"', "fetch",
|
||||
f"+refs/heads/{self.branchNamePrefix}/*:refs/remotes/origin/{self.branchNamePrefix}/*")
|
||||
result = remote.fetch(prune=True)
|
||||
if not result or any(fetch_info.flags & fetch_info.ERROR for fetch_info in result):
|
||||
raise BaseException(f"Failed to fetch updates for {repository_url}")
|
||||
raise BaseException(f"Failed to fetch updates for {remote_name}")
|
||||
if not os.path.exists(os.path.join(self.repo_path, ".git")):
|
||||
raise BaseException(f"Failed to clone repositories {self.remotes} to {self.repo_path}")
|
||||
except BaseException as e:
|
||||
|
||||
Reference in New Issue
Block a user