diff --git a/pungi/util.py b/pungi/util.py index 0dfaeec0..038e9b02 100644 --- a/pungi/util.py +++ b/pungi/util.py @@ -265,7 +265,10 @@ def resolve_git_ref(repourl, ref): lines = [] for line in output.split("\n"): - if line and ("refs/heads/" in line or "refs/tags/" in line or "HEAD" in line): + # Keep only lines that represent branches and tags, and also a line for + # currently checked out HEAD. The leading tab is required to + # distinguish it from HEADs that could exist in remotes. + if line and ("refs/heads/" in line or "refs/tags/" in line or "\tHEAD" in line): lines.append(line) if len(lines) == 0: # Branch does not exist in remote repo diff --git a/tests/test_util.py b/tests/test_util.py index 793d258a..7e44d957 100644 --- a/tests/test_util.py +++ b/tests/test_util.py @@ -61,6 +61,20 @@ class TestGitRefResolver(unittest.TestCase): universal_newlines=True, ) + @mock.patch('pungi.util.run') + def test_resolve_ref_with_remote_head(self, run): + run.return_value = ( + 0, "CAFEBABE\tHEAD\nBABECAFE\trefs/remotes/origin/HEAD" + ) + + ref = util.resolve_git_ref("https://git.example.com/repo.git", "HEAD") + + self.assertEqual(ref, "CAFEBABE") + run.assert_called_once_with( + ["git", "ls-remote", "https://git.example.com/repo.git", "HEAD"], + 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')