diff --git a/pungi/util.py b/pungi/util.py index 86f35d81..d5a9eed3 100644 --- a/pungi/util.py +++ b/pungi/util.py @@ -263,7 +263,10 @@ def resolve_git_ref(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: # Branch does not exist in remote repo raise GitUrlResolveError( @@ -272,7 +275,7 @@ def resolve_git_ref(repourl, ref): if len(lines) != 1: # This should never happen. HEAD can not match multiple commits in a # 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] diff --git a/tests/test_util.py b/tests/test_util.py index 5a65df2c..820d1ccf 100644 --- a/tests/test_util.py +++ b/tests/test_util.py @@ -47,6 +47,20 @@ class TestGitRefResolver(unittest.TestCase): ref = util.resolve_git_ref("https://git.example.com/repo.git", "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') def test_resolve_missing_spec(self, run): url = util.resolve_git_url('https://git.example.com/repo.git')