Fix agent fails to check out code from main branch when branch/commit is not explicitly specified

This commit is contained in:
allegroai 2022-02-07 20:04:08 +02:00
parent bfed3ccf4d
commit 1f53c4fd1b
2 changed files with 42 additions and 32 deletions

View File

@ -2135,7 +2135,8 @@ class Worker(ServiceCommandSection):
cwd = vcs.location if vcs and vcs.location else directory cwd = vcs.location if vcs and vcs.location else directory
if is_cached and not standalone_mode: if not standalone_mode:
if is_cached:
# reinstalling git / local packages # reinstalling git / local packages
package_api = copy(self.package_api) package_api = copy(self.package_api)
OnlyExternalRequirements.cwd = package_api.cwd = cwd OnlyExternalRequirements.cwd = package_api.cwd = cwd
@ -2152,8 +2153,7 @@ class Worker(ServiceCommandSection):
package_api.load_requirements(cached_requirements) package_api.load_requirements(cached_requirements)
# make sure we call the correct freeze # make sure we call the correct freeze
requirements_manager = package_api.requirements_manager requirements_manager = package_api.requirements_manager
else:
elif not is_cached and not standalone_mode:
self.install_requirements( self.install_requirements(
execution, execution,
repo_info, repo_info,

View File

@ -108,7 +108,7 @@ class VCS(object):
) )
self.url = url self.url = url
self.location = Text(location) self.location = Text(location)
self.revision = revision self._revision = revision
self.log = self.session.get_logger(__name__) self.log = self.session.get_logger(__name__)
@property @property
@ -390,7 +390,7 @@ class VCS(object):
""" """
Checkout repository at specified revision Checkout repository at specified revision
""" """
self.call("checkout", self.revision, *self.checkout_flags, cwd=self.location) self.call("checkout", self._revision, *self.checkout_flags, cwd=self.location)
@abc.abstractmethod @abc.abstractmethod
def pull(self): def pull(self):
@ -519,7 +519,7 @@ class VCS(object):
class Git(VCS): class Git(VCS):
executable_name = "git" executable_name = "git"
main_branch = "master" main_branch = ("master", "main")
clone_flags = ("--quiet", "--recursive") clone_flags = ("--quiet", "--recursive")
checkout_flags = ("--force",) checkout_flags = ("--force",)
COMMAND_ENV = { COMMAND_ENV = {
@ -531,7 +531,9 @@ class Git(VCS):
@staticmethod @staticmethod
def remote_branch_name(branch): def remote_branch_name(branch):
return "origin/{}".format(branch) return [
"origin/{}".format(b) for b in ([branch] if isinstance(branch, str) else branch)
]
def executable_not_found_error_help(self): def executable_not_found_error_help(self):
return 'Cannot find "{}" executable. {}'.format( return 'Cannot find "{}" executable. {}'.format(
@ -553,7 +555,15 @@ class Git(VCS):
""" """
Checkout repository at specified revision Checkout repository at specified revision
""" """
self.call("checkout", self.revision, *self.checkout_flags, cwd=self.location) revisions = [self._revision] if isinstance(self._revision, str) else self._revision
for i, revision in enumerate(revisions):
try:
self.call("checkout", revision, *self.checkout_flags, cwd=self.location)
break
except subprocess.CalledProcessError:
if i == len(revisions) - 1:
raise
try: try:
self.call("submodule", "update", "--recursive", cwd=self.location) self.call("submodule", "update", "--recursive", cwd=self.location)
except: # noqa except: # noqa
@ -593,7 +603,7 @@ class Hg(VCS):
"pull", "pull",
self.url_with_auth, self.url_with_auth,
cwd=self.location, cwd=self.location,
*(("-r", self.revision) if self.revision else ()) *(("-r", self._revision) if self._revision else ())
) )
info_commands = dict( info_commands = dict(