Start using Git.get_revision_sha().

This commit is contained in:
Chris Jerdonek 2017-08-22 20:08:55 -07:00
parent 85dc157147
commit 15d058edf0
3 changed files with 20 additions and 129 deletions

View File

@ -113,34 +113,28 @@ class Git(VersionControl):
return refs.get('refs/tags/{}'.format(rev))
def check_rev_options(self, dest, rev_options):
"""Check the revision options before checkout to compensate that tags
and branches may need origin/ as a prefix.
"""Check the revision options before checkout.
Returns a new RevOptions object for the SHA1 of the branch or tag
if found.
Args:
rev_options: a RevOptions object.
"""
revisions = self.get_short_refs(dest)
rev = rev_options.arg_rev
origin_rev = 'origin/%s' % rev
if origin_rev in revisions:
# remote branch
return rev_options.make_new(revisions[origin_rev])
elif rev in revisions:
# a local tag or branch name
return rev_options.make_new(revisions[rev])
sha = self.get_revision_sha(dest, rev)
# Do not show a warning for the common case of something that has
# the form of a Git commit hash.
if not looks_like_hash(rev):
logger.warning(
"Did not find branch or tag '%s', assuming ref or revision.",
rev,
)
if sha is None:
if not looks_like_hash(rev):
logger.warning(
"Did not find branch or tag '%s', assuming revision or ref",
rev,
)
return rev_options
return rev_options
return rev_options.make_new(sha)
def is_commit_id_equal(self, dest, name):
"""
@ -221,38 +215,6 @@ class Git(VersionControl):
['rev-parse', 'HEAD'], show_stdout=False, cwd=location)
return current_rev.strip()
def get_full_refs(self, location):
"""Yields tuples of (commit, ref) for branches and tags"""
output = self.run_command(['show-ref'],
show_stdout=False, cwd=location)
for line in output.strip().splitlines():
commit, ref = line.split(' ', 1)
yield commit.strip(), ref.strip()
def is_ref_remote(self, ref):
return ref.startswith('refs/remotes/')
def is_ref_branch(self, ref):
return ref.startswith('refs/heads/')
def is_ref_tag(self, ref):
return ref.startswith('refs/tags/')
def get_short_refs(self, location):
"""Return map of named refs (branches or tags) to commit hashes."""
rv = {}
for commit, ref in self.get_full_refs(location):
ref_name = None
if self.is_ref_remote(ref):
ref_name = ref[len('refs/remotes/'):]
elif self.is_ref_branch(ref):
ref_name = ref[len('refs/heads/'):]
elif self.is_ref_tag(ref):
ref_name = ref[len('refs/tags/'):]
if ref_name is not None:
rv[ref_name] = commit
return rv
def _get_subdirectory(self, location):
"""Return the relative path of setup.py to the git repo root."""
# find the repo root

View File

@ -9,55 +9,6 @@ from tests.lib.git_submodule_helpers import (
)
@pytest.mark.network
def test_get_short_refs_should_return_tag_name_and_commit_pair(script):
version_pkg_path = _create_test_package(script)
script.run('git', 'tag', '0.1', cwd=version_pkg_path)
script.run('git', 'tag', '0.2', cwd=version_pkg_path)
commit = script.run(
'git', 'rev-parse', 'HEAD',
cwd=version_pkg_path
).stdout.strip()
git = Git()
result = git.get_short_refs(version_pkg_path)
assert result['0.1'] == commit, result
assert result['0.2'] == commit, result
@pytest.mark.network
def test_get_short_refs_should_return_branch_name_and_commit_pair(script):
version_pkg_path = _create_test_package(script)
script.run('git', 'branch', 'branch0.1', cwd=version_pkg_path)
commit = script.run(
'git', 'rev-parse', 'HEAD',
cwd=version_pkg_path
).stdout.strip()
git = Git()
result = git.get_short_refs(version_pkg_path)
assert result['master'] == commit, result
assert result['branch0.1'] == commit, result
@pytest.mark.network
def test_get_short_refs_should_ignore_no_branch(script):
version_pkg_path = _create_test_package(script)
script.run('git', 'branch', 'branch0.1', cwd=version_pkg_path)
commit = script.run(
'git', 'rev-parse', 'HEAD',
cwd=version_pkg_path
).stdout.strip()
# current branch here is "* (nobranch)"
script.run(
'git', 'checkout', commit,
cwd=version_pkg_path,
expect_stderr=True,
)
git = Git()
result = git.get_short_refs(version_pkg_path)
assert result['master'] == commit, result
assert result['branch0.1'] == commit, result
@pytest.mark.network
def test_is_commit_id_equal(script):
"""
@ -78,34 +29,24 @@ def test_is_commit_id_equal(script):
assert not git.is_commit_id_equal(version_pkg_path, None)
@patch('pip._internal.vcs.git.Git.get_short_refs')
def test_check_rev_options_should_handle_branch_name(get_refs_mock):
get_refs_mock.return_value = {'master': '123456', '0.1': 'abc123'}
@patch('pip._internal.vcs.git.Git.get_revision_sha')
def test_check_rev_options_ref_exists(get_sha_mock):
get_sha_mock.return_value = '123456'
git = Git()
rev_options = git.make_rev_options('master')
rev_options = git.make_rev_options('develop')
new_options = git.check_rev_options('.', rev_options)
assert new_options.rev == '123456'
@patch('pip._internal.vcs.git.Git.get_short_refs')
def test_check_rev_options_should_handle_tag_name(get_refs_mock):
get_refs_mock.return_value = {'master': '123456', '0.1': 'abc123'}
@patch('pip._internal.vcs.git.Git.get_revision_sha')
def test_check_rev_options_ref_not_found(get_sha_mock):
get_sha_mock.return_value = None
git = Git()
rev_options = git.make_rev_options('0.1')
rev_options = git.make_rev_options('develop')
new_options = git.check_rev_options('.', rev_options)
assert new_options.rev == 'abc123'
@patch('pip._internal.vcs.git.Git.get_short_refs')
def test_check_rev_options_should_handle_ambiguous_commit(get_refs_mock):
get_refs_mock.return_value = {'master': '123456', '0.1': '123456'}
git = Git()
rev_options = git.make_rev_options('0.1')
new_options = git.check_rev_options('.', rev_options)
assert new_options.rev == '123456'
assert new_options.rev == 'develop'
@patch('pip._internal.vcs.git.Git.get_short_refs')

View File

@ -74,21 +74,9 @@ def test_rev_options_make_new():
def git():
git_url = 'http://github.com/pypa/pip-test-package'
sha = '5547fa909e83df8bd743d3978d6667497983a4b7'
refs = {
'0.1': 'a8992fc7ee17e5b9ece022417b64594423caca7c',
'0.1.1': '7d654e66c8fa7149c165ddeffa5b56bc06619458',
'0.1.2': 'f1c1020ebac81f9aeb5c766ff7a772f709e696ee',
'foo': sha,
'bar': sha,
'master': sha,
'origin/master': sha,
'origin/HEAD': sha,
}
git = Git()
git.get_url = Mock(return_value=git_url)
git.get_revision = Mock(return_value=sha)
git.get_short_refs = Mock(return_value=refs)
return git