Skip to content

Commit

Permalink
common: return right revision when git ls-remote
Browse files Browse the repository at this point in the history
When we execute git ls-remote, the pattern is interpreted as a glob
[1].

Thus we may retrieve the revision of several remote branchs and we
returned the revision of the first branch found.
But the first branch may not be the right branch.

Example with a remote containing these branchs:
a/rev
rev

If we git ls-remote 'rev', we would have returned the revision of
a/rev branch.

The issue is now fixed and we return the revision of the requested
branch.

[1] https://git-scm.com/docs/git-ls-remote/en#Documentation/git-ls-remote.txt-ltpatternsgt82308203

Signed-off-by: Julien Masson <[email protected]>
  • Loading branch information
massonju authored and makohoek committed Sep 13, 2024
1 parent d4a269b commit 40a16fc
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 4 deletions.
20 changes: 16 additions & 4 deletions repo_resource/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,12 +105,24 @@ def getRevision(remote, remoteUrl, project, branch):
if is_sha1(branch):
return (remote + '/' + project, branch)
g = git.cmd.Git()
url, revision = (
url, revList = (
remote + '/' + project,
g.ls_remote(remoteUrl+'/'+project, branch).split()[0]
g.ls_remote(remoteUrl+'/'+project, branch).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 = branch
else:
rev = 'refs/heads/' + branch

print('{}: {}'.format(url, revDict[rev]))
return (url, revDict[rev])
except Exception as e:
with redirect_stdout(sys.stderr):
print('Cannot fetch project {}/{}'.format(remoteUrl, project))
Expand Down
17 changes: 17 additions & 0 deletions repo_resource/test_check.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,13 @@ 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'
}
}

def tearDown(self):
p = common.CACHEDIR
Expand Down Expand Up @@ -437,6 +444,16 @@ 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)


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

0 comments on commit 40a16fc

Please sign in to comment.