diff --git a/pip/vcs/git.py b/pip/vcs/git.py index 24528de1b59..e033e3c7f59 100644 --- a/pip/vcs/git.py +++ b/pip/vcs/git.py @@ -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) @@ -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), @@ -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 diff --git a/tests/functional/test_install_vcs.py b/tests/functional/test_install_vcs.py index 0f0ea71d248..3fae69f8b9e 100644 --- a/tests/functional/test_install_vcs.py +++ b/tests/functional/test_install_vcs.py @@ -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 diff --git a/tests/functional/test_install_vcs_git.py b/tests/functional/test_install_vcs_git.py index d06a4d05a3e..5719be80f1a 100644 --- a/tests/functional/test_install_vcs_git.py +++ b/tests/functional/test_install_vcs_git.py @@ -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