diff --git a/pungi/util.py b/pungi/util.py index 6174bad5..2560f046 100644 --- a/pungi/util.py +++ b/pungi/util.py @@ -257,6 +257,10 @@ def resolve_git_url(url): _, output = run(['git', 'ls-remote', baseurl, ref]) lines = [line for line in output.split('\n') if line] + if len(lines) == 0: + # Branch does not exist in remote repo + raise RuntimeError('Failed to resolve %s: ref does not exist in remote repo' + % url) 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. diff --git a/tests/test_util.py b/tests/test_util.py index dac94396..4ba9df6c 100644 --- a/tests/test_util.py +++ b/tests/test_util.py @@ -81,6 +81,17 @@ class TestGitRefResolver(unittest.TestCase): run.assert_called_once_with(['git', 'ls-remote', 'https://git.example.com/repo.git', 'HEAD']) self.assertEqual(url, 'git+https://git.example.com/repo.git#CAFEBABE') + @mock.patch('pungi.util.run') + def test_resolve_no_branch_in_remote(self, run): + run.return_value = (0, '') + + with self.assertRaises(RuntimeError) as ctx: + util.resolve_git_url('https://git.example.com/repo.git?somedir#origin/my-branch') + + run.assert_called_once_with( + ['git', 'ls-remote', 'https://git.example.com/repo.git', 'refs/heads/my-branch']) + self.assertIn('ref does not exist in remote repo', str(ctx.exception)) + class TestGetVariantData(unittest.TestCase): def test_get_simple(self):