Skip to content

Several fixes when setting revision #40

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Sep 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 39 additions & 15 deletions repo_resource/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,23 +94,41 @@ def getRevision(remote, remoteUrl, project, branch):
with git ls-remote command for each project
without downloading the whole repo
"""
# v1.0^{} is the commit referring to tag v1.0
# git ls-remote returns the tag sha1 if left as is
if branch.startswith('refs/tags'):
branch += '^{}'
try:
with redirect_stdout(sys.stderr):
# return tuple (remote/project, revision)
print('Fetching revision for {}/{}...'.format(remote, project))
# return tuple (remote/project, branch, revision)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks weird to return branch as such which is in the function parameters

I wish we could return a tuple like (remote/project/branch, revision), remote/project/branch being a key kind of... it'd make things easier to update revisions in update_manifest, avoiding the for url, branch, rev in revisionList: in update_manifest

but... a branch can contain '/' (slash) character and project name too, so it seems...
So the following 2 would match:
project "a/1" branch "alpha"
project "a" branch "1/alpha"

So that's the right thing to do: get rid of the revisionTable variable. Maybe we could store as revisionTable[projectRemote][project][branch] or something similar in a future release...?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've turned this into an issue instead, to continue the discussion.

Since this has been approved, I'll go ahead and merge and draft a new minor release.

@massonju please continue discussion the review comments here or in the dedicated issue: #42

print('Fetching revision for {}/{} - {}...'.format(
remote, project, branch))
if is_sha1(branch):
return (remote + '/' + project, branch)
return (remote + '/' + project, branch, branch)
g = git.cmd.Git()
url, revision = (

headRef = branch
# v1.0^{} is the commit referring to tag v1.0
# git ls-remote returns the tag sha1 if left as is
if branch.startswith('refs/tags'):
headRef += '^{}'

url, revList = (
remote + '/' + project,
g.ls_remote(remoteUrl+'/'+project, branch).split()[0]
g.ls_remote(remoteUrl+'/'+project, headRef).split()
)
print('{}: {}'.format(url, revision))
return (url, revision)

# convert revision list to revision dict:
# ['SHA1', 'refs/heads/XXXX', 'SHA1', 'refs/heads/YYYY']
# -> {'refs/heads/XXXX': 'SHA1', 'refs/heads/YYYY': 'SHA1'}
revDict = dict([(b, a)
for a, b in zip(revList[::2], revList[1::2])])

if branch.startswith('refs/tags'):
rev = headRef
else:
rev = 'refs/heads/' + branch

print('{} - {}: {}'.format(url, branch, revDict[rev]))

# return url, branch (without any suffix) and revision
return (url, branch, revDict[rev])
except Exception as e:
with redirect_stdout(sys.stderr):
print('Cannot fetch project {}/{}'.format(remoteUrl, project))
Expand Down Expand Up @@ -450,15 +468,21 @@ def update_manifest(self, jobs):

with Pool(jobs) as pool:
revisionList = pool.map(multi_run_wrapper, projects)
# Convert (remote/project, revision) tuple list
# to hash table dict[remote/project]=revision
revisionTable = dict((proj, rev) for proj, rev in revisionList)

# Update revisions
for p in manifest.findall('project'):
project = p.get('name')
projectBranch = p.get('revision') or defaultBranch
projectRemote = p.get('remote') or defaultRemote
p.set('revision', revisionTable[projectRemote+'/'+project])
# find revision of the project in revisionList
for url, branch, rev in revisionList:
if (
projectRemote + "/" + project == url
and branch == projectBranch
):
projectRev = rev
break
p.set('revision', projectRev)

self.__version = Version(
ET.canonicalize(
Expand Down
34 changes: 34 additions & 0 deletions repo_resource/test_check.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,20 @@ def setUp(self):
'name': 'aosp_include_multiple_projects.xml'
}
}
self.branch_matching_source = {
'source': {
'url': 'https://github.com/makohoek/demo-manifests.git',
'revision': 'main',
'name': 'branch_matching.xml'
}
}
self.one_project_multi_path_source = {
'source': {
'url': 'https://github.com/makohoek/demo-manifests.git',
'revision': 'main',
'name': 'one_project_multi_path.xml'
}
}

def tearDown(self):
p = common.CACHEDIR
Expand Down Expand Up @@ -437,6 +451,26 @@ def test_include_multiple_projects_version(self):
version = versions[0]['version']
self.assertEqual(version, expected_version)

def test_branch_matching(self):
data = self.branch_matching_source
instream = StringIO(json.dumps(data))
versions = check.check(instream)

expected_version = '<manifest><remote fetch=\"https://github.com/\" name=\"github\"></remote><project name=\"makohoek/demo-manifests.git\" path=\"rev\" remote=\"github\" revision=\"bd2eb4ba9b5581373ff276f619a88e248a2c77e7\"></project></manifest>' # noqa: E501

version = versions[0]['version']
self.assertEqual(version, expected_version)

def test_one_project_multi_path(self):
data = self.one_project_multi_path_source
instream = StringIO(json.dumps(data))
versions = check.check(instream)

expected_version = '<manifest><remote fetch=\"https://github.com/\" name=\"github\"></remote><project name=\"makohoek/demo-manifests.git\" path=\"rev-1\" remote=\"github\" revision=\"0370566f0c65d8d7f03b78071b23a1e7480beac7\"></project><project name=\"makohoek/demo-manifests.git\" path=\"rev-2\" remote=\"github\" revision=\"524a18fc90ba5b4d298c4367fa4af959baf73a5f\"></project></manifest>' # noqa: E501

version = versions[0]['version']
self.assertEqual(version, expected_version)


if __name__ == '__main__':
unittest.main()
Loading