[util] Resolve branches in git urls

Signed-off-by: Lubomír Sedlář <lsedlar@redhat.com>
This commit is contained in:
Lubomír Sedlář 2016-02-25 08:39:01 +01:00
parent 6e8970e648
commit 60d820d4fd
2 changed files with 23 additions and 4 deletions

View File

@ -220,9 +220,18 @@ def is_arch_multilib(conf, arch):
return bool(get_arch_variant_data(conf, 'multilib', arch, None)) 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): def resolve_git_url(url):
"""Given a url to a Git repo specifying HEAD as a ref, replace that """Given a url to a Git repo specifying HEAD or origin/<branch> as a ref,
specifier with actual SHA1 of the commit. replace that specifier with actual SHA1 of the commit.
Otherwise, the original URL will be returned. Otherwise, the original URL will be returned.
@ -230,11 +239,12 @@ def resolve_git_url(url):
run git command. run git command.
""" """
r = urlparse.urlsplit(url) r = urlparse.urlsplit(url)
if r.fragment != 'HEAD': ref = _get_git_ref(r.fragment)
if not ref:
return url return url
baseurl = urlparse.urlunsplit((r.scheme, r.netloc, r.path, '', '')) 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] lines = [line for line in output.split('\n') if line]
if len(lines) != 1: if len(lines) != 1:

View File

@ -25,6 +25,15 @@ class TestGitRefResolver(unittest.TestCase):
self.assertEqual(url, 'https://git.example.com/repo.git?somedir#CAFEBABE') 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']) 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') @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')