util: Resolve ref if duplicate branches are present

If the repo contains the same name under multiple directories, make the
resolving work by filtering only to refs/heads and refs/tags.

Fixes: https://pagure.io/pungi/issue/1180
Signed-off-by: Lubomír Sedlář <lsedlar@redhat.com>
This commit is contained in:
Lubomír Sedlář 2019-05-07 08:28:20 +02:00
parent 9517df44c7
commit b0f0579a9e
2 changed files with 19 additions and 2 deletions

View File

@ -263,7 +263,10 @@ def resolve_git_ref(repourl, ref):
_, output = git_ls_remote(repourl, ref) _, output = git_ls_remote(repourl, ref)
lines = [line for line in output.split('\n') if line] lines = []
for line in output.split("\n"):
if line and ("refs/heads/" in line or "refs/tags/" in line or "HEAD" in line):
lines.append(line)
if len(lines) == 0: if len(lines) == 0:
# Branch does not exist in remote repo # Branch does not exist in remote repo
raise GitUrlResolveError( raise GitUrlResolveError(
@ -272,7 +275,7 @@ def resolve_git_ref(repourl, ref):
if len(lines) != 1: if len(lines) != 1:
# This should never happen. HEAD can not match multiple commits in a # This should never happen. HEAD can not match multiple commits in a
# single repo, and there can not be a repo without a HEAD. # single repo, and there can not be a repo without a HEAD.
raise GitUrlResolveError("Failed to resolve %s", repourl) raise GitUrlResolveError("Failed to resolve %r in %s" % (ref, repourl))
return lines[0].split()[0] return lines[0].split()[0]

View File

@ -47,6 +47,20 @@ class TestGitRefResolver(unittest.TestCase):
ref = util.resolve_git_ref("https://git.example.com/repo.git", "a" * 40) ref = util.resolve_git_ref("https://git.example.com/repo.git", "a" * 40)
self.assertEqual(ref, "a" * 40) self.assertEqual(ref, "a" * 40)
@mock.patch('pungi.util.run')
def test_resolve_ref_multiple_matches(self, run):
run.return_value = (
0, "CAFEBABE\trefs/heads/master\nBABECAFE\trefs/remotes/origin/master"
)
ref = util.resolve_git_ref("https://git.example.com/repo.git", "master")
self.assertEqual(ref, "CAFEBABE")
run.assert_called_once_with(
["git", "ls-remote", "https://git.example.com/repo.git", "master"],
universal_newlines=True,
)
@mock.patch('pungi.util.run') @mock.patch('pungi.util.run')
def test_resolve_missing_spec(self, run): def test_resolve_missing_spec(self, run):
url = util.resolve_git_url('https://git.example.com/repo.git') url = util.resolve_git_url('https://git.example.com/repo.git')