From f1263eeacb6a6db34dd97d7fd1d929187c17ae68 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lubom=C3=ADr=20Sedl=C3=A1=C5=99?= Date: Tue, 18 Jun 2019 12:50:39 +0200 Subject: [PATCH] util: Resolve HEAD in repos that have a remote MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit JIRA: COMPOSE-3597 Signed-off-by: Lubomír Sedlář --- pungi/util.py | 5 ++++- tests/test_util.py | 14 ++++++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) 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')