Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

vcs.git: make sure to always be in detached HEAD #2340

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions pip/vcs/git.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,8 @@ def check_version(self, dest, rev_options):

def switch(self, dest, url, rev_options):
self.run_command(['config', 'remote.origin.url', url], cwd=dest)
self.run_command(['checkout', '-q'] + rev_options, cwd=dest)
self.run_command(
['checkout', '-q', '--detach'] + rev_options, cwd=dest)

self.update_submodules(dest)

Expand All @@ -111,12 +112,11 @@ def update(self, dest, rev_options):

def obtain(self, dest):
url, rev = self.get_url_rev()
if rev:
rev_options = [rev]
rev_display = ' (to %s)' % rev
else:
rev_options = ['origin/master']
rev_display = ''
if not rev:
rev = 'origin/master'
rev_options = [rev]
rev_display = ' (to %s)' % rev

if self.check_destination(dest, url, rev_options, rev_display):
logger.info(
'Cloning %s%s to %s', url, rev_display, display_path(dest),
Expand All @@ -128,7 +128,7 @@ def obtain(self, dest):
# Only do a checkout if rev_options differs from HEAD
if not self.check_version(dest, rev_options):
self.run_command(
['checkout', '-q'] + rev_options,
['checkout', '-q', '--detach'] + rev_options,
cwd=dest,
)
#: repo may contain submodules
Expand Down
6 changes: 4 additions & 2 deletions tests/functional/test_install_vcs.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,8 +144,10 @@ def test_git_branch_should_not_be_changed(script, tmpdir):
expect_error=True,
)
source_dir = script.venv_path / 'src' / 'pip-test-package'
result = script.run('git', 'branch', cwd=source_dir)
assert '* master' in result.stdout, result.stdout
# Check we are on master
head = script.run('git', 'rev-parse', 'HEAD', cwd=source_dir).stdout
master = script.run('git', 'rev-parse', 'master', cwd=source_dir).stdout
assert head == master


@pytest.mark.network
Expand Down
15 changes: 15 additions & 0 deletions tests/functional/test_install_vcs_git.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,3 +133,18 @@ def test_check_submodule_addition(script):
script.venv / 'src/version-pkg/testpkg/static/testfile2'
in update_result.files_created
)


def test_install_git_detached(script):
version_pkg_path = _create_test_package(script)
script.pip(
'install', '-e',
'git+file://' + version_pkg_path + '@master#egg=version_pkg'
)
source_dir = script.venv_path / 'src' / 'version-pkg'
branch = script.run(
'git', 'status', '--porcelain', '--branch',
cwd=source_dir
).stdout.strip()
# Check we are in detached HEAD
assert 'HEAD' in branch