mirror of
https://github.com/pypa/pip
synced 2023-12-13 21:30:23 +01:00
Combine Git get_tag_revs and get_branch_revs into single get_refs method.
This commit is contained in:
parent
92e0dbbf4c
commit
fa81a41d0b
|
@ -67,8 +67,7 @@ class Git(VersionControl):
|
||||||
and branches may need origin/ as a prefix.
|
and branches may need origin/ as a prefix.
|
||||||
Returns the SHA1 of the branch or tag if found.
|
Returns the SHA1 of the branch or tag if found.
|
||||||
"""
|
"""
|
||||||
revisions = self.get_tag_revs(dest)
|
revisions = self.get_refs(dest)
|
||||||
revisions.update(self.get_branch_revs(dest))
|
|
||||||
|
|
||||||
origin_rev = 'origin/%s' % rev
|
origin_rev = 'origin/%s' % rev
|
||||||
if origin_rev in revisions:
|
if origin_rev in revisions:
|
||||||
|
@ -129,30 +128,24 @@ class Git(VersionControl):
|
||||||
[self.cmd, 'rev-parse', 'HEAD'], show_stdout=False, cwd=location)
|
[self.cmd, 'rev-parse', 'HEAD'], show_stdout=False, cwd=location)
|
||||||
return current_rev.strip()
|
return current_rev.strip()
|
||||||
|
|
||||||
def get_tag_revs(self, location):
|
def get_refs(self, location):
|
||||||
tags = self._get_all_tag_names(location)
|
"""Return map of named refs (branches or tags) to commit hashes."""
|
||||||
tag_revs = {}
|
output = call_subprocess([self.cmd, 'show-ref'],
|
||||||
for line in tags.splitlines():
|
show_stdout=False, cwd=location)
|
||||||
tag = line.strip()
|
rv = {}
|
||||||
rev = self._get_revision_from_rev_parse(tag, location)
|
for line in output.strip().splitlines():
|
||||||
tag_revs[tag] = rev.strip()
|
commit, ref = line.split(' ', 1)
|
||||||
return tag_revs
|
|
||||||
|
|
||||||
def get_branch_revs(self, location):
|
|
||||||
rev_names = call_subprocess([self.cmd, 'show-ref'],
|
|
||||||
show_stdout=False, cwd=location)
|
|
||||||
branch_revs = {}
|
|
||||||
for line in rev_names.strip().splitlines():
|
|
||||||
rev, ref = line.split(' ', 1)
|
|
||||||
ref = ref.strip()
|
ref = ref.strip()
|
||||||
branch = None
|
ref_name = None
|
||||||
if ref.startswith('refs/remotes/'):
|
if ref.startswith('refs/remotes/'):
|
||||||
branch = ref[len('refs/remotes/'):]
|
ref_name = ref[len('refs/remotes/'):]
|
||||||
elif ref.startswith('refs/heads/'):
|
elif ref.startswith('refs/heads/'):
|
||||||
branch = ref[len('refs/heads/'):]
|
ref_name = ref[len('refs/heads/'):]
|
||||||
if branch is not None:
|
elif ref.startswith('refs/tags/'):
|
||||||
branch_revs[branch] = rev.strip()
|
ref_name = ref[len('refs/tags/'):]
|
||||||
return branch_revs
|
if ref_name is not None:
|
||||||
|
rv[ref_name] = commit.strip()
|
||||||
|
return rv
|
||||||
|
|
||||||
def get_src_requirement(self, dist, location, find_tags):
|
def get_src_requirement(self, dist, location, find_tags):
|
||||||
repo = self.get_url(location)
|
repo = self.get_url(location)
|
||||||
|
@ -162,19 +155,14 @@ class Git(VersionControl):
|
||||||
if not repo:
|
if not repo:
|
||||||
return None
|
return None
|
||||||
current_rev = self.get_revision(location)
|
current_rev = self.get_revision(location)
|
||||||
tag_revs = self.get_tag_revs(location)
|
refs = self.get_refs(location)
|
||||||
branch_revs = self.get_branch_revs(location)
|
# refs maps names to commit hashes; we need the inverse
|
||||||
|
# if multiple names map to a single commit, this arbitrarily picks one
|
||||||
|
names_by_commit = dict((commit, ref) for ref, commit in refs.items())
|
||||||
|
|
||||||
if current_rev in tag_revs:
|
if current_rev in names_by_commit:
|
||||||
# It's a tag
|
# It's a tag
|
||||||
full_egg_name = '%s-%s' % (egg_project_name, tag_revs[current_rev])
|
full_egg_name = '%s-%s' % (egg_project_name, names_by_commit[current_rev])
|
||||||
elif (current_rev in branch_revs and
|
|
||||||
branch_revs[current_rev] != 'origin/master'):
|
|
||||||
# It's the head of a branch
|
|
||||||
full_egg_name = '%s-%s' % (
|
|
||||||
egg_project_name,
|
|
||||||
branch_revs[current_rev].replace('origin/', '')
|
|
||||||
)
|
|
||||||
else:
|
else:
|
||||||
full_egg_name = '%s-dev' % egg_project_name
|
full_egg_name = '%s-dev' % egg_project_name
|
||||||
|
|
||||||
|
@ -197,16 +185,6 @@ class Git(VersionControl):
|
||||||
|
|
||||||
return url, rev
|
return url, rev
|
||||||
|
|
||||||
def _get_all_tag_names(self, location):
|
|
||||||
return call_subprocess([self.cmd, 'tag', '-l'],
|
|
||||||
show_stdout=False,
|
|
||||||
raise_on_returncode=False,
|
|
||||||
cwd=location)
|
|
||||||
|
|
||||||
def _get_revision_from_rev_parse(self, name, location):
|
|
||||||
return call_subprocess([self.cmd, 'rev-parse', name],
|
|
||||||
show_stdout=False, cwd=location)
|
|
||||||
|
|
||||||
def update_submodules(self, location):
|
def update_submodules(self, location):
|
||||||
if not os.path.exists(os.path.join(location, '.gitmodules')):
|
if not os.path.exists(os.path.join(location, '.gitmodules')):
|
||||||
return
|
return
|
||||||
|
|
|
@ -107,7 +107,7 @@ def test_freeze_git_clone():
|
||||||
Script result: pip freeze -f %(repo)s#egg=pip_test_package
|
Script result: pip freeze -f %(repo)s#egg=pip_test_package
|
||||||
-- stdout: --------------------
|
-- stdout: --------------------
|
||||||
-f %(repo)s#egg=pip_test_package...
|
-f %(repo)s#egg=pip_test_package...
|
||||||
-e %(repo)s@...#egg=pip_test_package-dev
|
-e %(repo)s@...#egg=pip_test_package-0.1.1
|
||||||
...""" % {'repo': local_checkout('git+http://github.com/pypa/pip-test-package.git')})
|
...""" % {'repo': local_checkout('git+http://github.com/pypa/pip-test-package.git')})
|
||||||
_check_output(result, expected)
|
_check_output(result, expected)
|
||||||
|
|
||||||
|
|
|
@ -11,7 +11,7 @@ from tests.git_submodule_helpers import (
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
def test_get_tag_revs_should_return_tag_name_and_commit_pair():
|
def test_get_refs_should_return_tag_name_and_commit_pair():
|
||||||
env = reset_env()
|
env = reset_env()
|
||||||
version_pkg_path = _create_test_package(env)
|
version_pkg_path = _create_test_package(env)
|
||||||
env.run('git', 'tag', '0.1', cwd=version_pkg_path)
|
env.run('git', 'tag', '0.1', cwd=version_pkg_path)
|
||||||
|
@ -19,22 +19,24 @@ def test_get_tag_revs_should_return_tag_name_and_commit_pair():
|
||||||
commit = env.run('git', 'rev-parse', 'HEAD',
|
commit = env.run('git', 'rev-parse', 'HEAD',
|
||||||
cwd=version_pkg_path).stdout.strip()
|
cwd=version_pkg_path).stdout.strip()
|
||||||
git = Git()
|
git = Git()
|
||||||
result = git.get_tag_revs(version_pkg_path)
|
result = git.get_refs(version_pkg_path)
|
||||||
assert result == {'0.1': commit, '0.2': commit}, result
|
assert result['0.1'] == commit, result
|
||||||
|
assert result['0.2'] == commit, result
|
||||||
|
|
||||||
|
|
||||||
def test_get_branch_revs_should_return_branch_name_and_commit_pair():
|
def test_get_refs_should_return_branch_name_and_commit_pair():
|
||||||
env = reset_env()
|
env = reset_env()
|
||||||
version_pkg_path = _create_test_package(env)
|
version_pkg_path = _create_test_package(env)
|
||||||
env.run('git', 'branch', 'branch0.1', cwd=version_pkg_path)
|
env.run('git', 'branch', 'branch0.1', cwd=version_pkg_path)
|
||||||
commit = env.run('git', 'rev-parse', 'HEAD',
|
commit = env.run('git', 'rev-parse', 'HEAD',
|
||||||
cwd=version_pkg_path).stdout.strip()
|
cwd=version_pkg_path).stdout.strip()
|
||||||
git = Git()
|
git = Git()
|
||||||
result = git.get_branch_revs(version_pkg_path)
|
result = git.get_refs(version_pkg_path)
|
||||||
assert result == {'master': commit, 'branch0.1': commit}, result
|
assert result['master'] == commit, result
|
||||||
|
assert result['branch0.1'] == commit, result
|
||||||
|
|
||||||
|
|
||||||
def test_get_branch_revs_should_ignore_no_branch():
|
def test_get_refs_should_ignore_no_branch():
|
||||||
env = reset_env()
|
env = reset_env()
|
||||||
version_pkg_path = _create_test_package(env)
|
version_pkg_path = _create_test_package(env)
|
||||||
env.run('git', 'branch', 'branch0.1', cwd=version_pkg_path)
|
env.run('git', 'branch', 'branch0.1', cwd=version_pkg_path)
|
||||||
|
@ -44,40 +46,32 @@ def test_get_branch_revs_should_ignore_no_branch():
|
||||||
env.run('git', 'checkout', commit,
|
env.run('git', 'checkout', commit,
|
||||||
cwd=version_pkg_path, expect_stderr=True)
|
cwd=version_pkg_path, expect_stderr=True)
|
||||||
git = Git()
|
git = Git()
|
||||||
result = git.get_branch_revs(version_pkg_path)
|
result = git.get_refs(version_pkg_path)
|
||||||
assert result == {'master': commit, 'branch0.1': commit}, result
|
assert result['master'] == commit, result
|
||||||
|
assert result['branch0.1'] == commit, result
|
||||||
|
|
||||||
|
|
||||||
@patch('pip.vcs.git.Git.get_tag_revs')
|
@patch('pip.vcs.git.Git.get_refs')
|
||||||
@patch('pip.vcs.git.Git.get_branch_revs')
|
def test_check_rev_options_should_handle_branch_name(get_refs_mock):
|
||||||
def test_check_rev_options_should_handle_branch_name(branches_revs_mock,
|
get_refs_mock.return_value = {'master': '123456', '0.1': '123456'}
|
||||||
tags_revs_mock):
|
|
||||||
branches_revs_mock.return_value = {'master': '123456'}
|
|
||||||
tags_revs_mock.return_value = {'0.1': '123456'}
|
|
||||||
git = Git()
|
git = Git()
|
||||||
|
|
||||||
result = git.check_rev_options('master', '.', [])
|
result = git.check_rev_options('master', '.', [])
|
||||||
assert result == ['123456']
|
assert result == ['123456']
|
||||||
|
|
||||||
|
|
||||||
@patch('pip.vcs.git.Git.get_tag_revs')
|
@patch('pip.vcs.git.Git.get_refs')
|
||||||
@patch('pip.vcs.git.Git.get_branch_revs')
|
def test_check_rev_options_should_handle_tag_name(get_refs_mock):
|
||||||
def test_check_rev_options_should_handle_tag_name(branches_revs_mock,
|
get_refs_mock.return_value = {'master': '123456', '0.1': '123456'}
|
||||||
tags_revs_mock):
|
|
||||||
branches_revs_mock.return_value = {'master': '123456'}
|
|
||||||
tags_revs_mock.return_value = {'0.1': '123456'}
|
|
||||||
git = Git()
|
git = Git()
|
||||||
|
|
||||||
result = git.check_rev_options('0.1', '.', [])
|
result = git.check_rev_options('0.1', '.', [])
|
||||||
assert result == ['123456']
|
assert result == ['123456']
|
||||||
|
|
||||||
|
|
||||||
@patch('pip.vcs.git.Git.get_tag_revs')
|
@patch('pip.vcs.git.Git.get_refs')
|
||||||
@patch('pip.vcs.git.Git.get_branch_revs')
|
def test_check_rev_options_should_handle_ambiguous_commit(get_refs_mock):
|
||||||
def test_check_rev_options_should_handle_ambiguous_commit(branches_revs_mock,
|
get_refs_mock.return_value = {'master': '123456', '0.1': '123456'}
|
||||||
tags_revs_mock):
|
|
||||||
branches_revs_mock.return_value = {'master': '123456'}
|
|
||||||
tags_revs_mock.return_value = {'0.1': '123456'}
|
|
||||||
git = Git()
|
git = Git()
|
||||||
|
|
||||||
result = git.check_rev_options('0.1', '.', [])
|
result = git.check_rev_options('0.1', '.', [])
|
||||||
|
|
Loading…
Reference in a new issue