From 60d820d4fd99a50e9023e90e84b0761f35ad4362 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lubom=C3=ADr=20Sedl=C3=A1=C5=99?= Date: Thu, 25 Feb 2016 08:39:01 +0100 Subject: [PATCH] [util] Resolve branches in git urls MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Lubomír Sedlář --- pungi/util.py | 18 ++++++++++++++---- tests/test_util.py | 9 +++++++++ 2 files changed, 23 insertions(+), 4 deletions(-) diff --git a/pungi/util.py b/pungi/util.py index 3b572f1c..761fc28e 100644 --- a/pungi/util.py +++ b/pungi/util.py @@ -220,9 +220,18 @@ def is_arch_multilib(conf, arch): return bool(get_arch_variant_data(conf, 'multilib', arch, None)) +def _get_git_ref(fragment): + if fragment == 'HEAD': + return fragment + if fragment.startswith('origin/'): + branch = fragment.split('/', 1)[1] + return 'refs/heads/' + branch + return None + + def resolve_git_url(url): - """Given a url to a Git repo specifying HEAD as a ref, replace that - specifier with actual SHA1 of the commit. + """Given a url to a Git repo specifying HEAD or origin/ as a ref, + replace that specifier with actual SHA1 of the commit. Otherwise, the original URL will be returned. @@ -230,11 +239,12 @@ def resolve_git_url(url): run git command. """ r = urlparse.urlsplit(url) - if r.fragment != 'HEAD': + ref = _get_git_ref(r.fragment) + if not ref: return url baseurl = urlparse.urlunsplit((r.scheme, r.netloc, r.path, '', '')) - _, output = run(['git', 'ls-remote', baseurl, r.fragment]) + _, output = run(['git', 'ls-remote', baseurl, ref]) lines = [line for line in output.split('\n') if line] if len(lines) != 1: diff --git a/tests/test_util.py b/tests/test_util.py index 9f5d5232..35b47fd6 100755 --- a/tests/test_util.py +++ b/tests/test_util.py @@ -25,6 +25,15 @@ class TestGitRefResolver(unittest.TestCase): self.assertEqual(url, 'https://git.example.com/repo.git?somedir#CAFEBABE') run.assert_called_once_with(['git', 'ls-remote', 'https://git.example.com/repo.git', 'HEAD']) + @mock.patch('pungi.util.run') + def test_successful_resolve_branch(self, run): + run.return_value = (0, 'CAFEBABE\trefs/heads/f24\n') + + url = util.resolve_git_url('https://git.example.com/repo.git?somedir#origin/f24') + + self.assertEqual(url, 'https://git.example.com/repo.git?somedir#CAFEBABE') + run.assert_called_once_with(['git', 'ls-remote', 'https://git.example.com/repo.git', 'refs/heads/f24']) + @mock.patch('pungi.util.run') def test_resolve_missing_spec(self, run): url = util.resolve_git_url('https://git.example.com/repo.git')