diff --git a/news/10149.feature.rst b/news/10149.feature.rst new file mode 100644 index 00000000000..42a8de68afb --- /dev/null +++ b/news/10149.feature.rst @@ -0,0 +1 @@ +Log the resolved commit SHA when installing a package from a Git repository. diff --git a/src/pip/_internal/vcs/git.py b/src/pip/_internal/vcs/git.py index 269bf6a65b3..8919aa538dd 100644 --- a/src/pip/_internal/vcs/git.py +++ b/src/pip/_internal/vcs/git.py @@ -281,6 +281,11 @@ def fetch_new(self, dest, url, rev_options): 'checkout', '-b', branch_name, '--track', track_branch, ] self.run_command(cmd_args, cwd=dest) + else: + sha = self.get_revision(dest) + rev_options = rev_options.make_new(sha) + + logger.info("Resolved %s to commit %s", url, rev_options.rev) #: repo may contain submodules self.update_submodules(dest) diff --git a/tests/functional/test_install_vcs_git.py b/tests/functional/test_install_vcs_git.py index afd6ffae055..977011ae52c 100644 --- a/tests/functional/test_install_vcs_git.py +++ b/tests/functional/test_install_vcs_git.py @@ -271,6 +271,28 @@ def test_git_install_then_install_ref(script): assert '0.1' == version +@pytest.mark.network +@pytest.mark.parametrize('rev, expected_sha', [ + # Clone the default branch + ("", "5547fa909e83df8bd743d3978d6667497983a4b7"), + # Clone a specific tag + ("@0.1.1", "7d654e66c8fa7149c165ddeffa5b56bc06619458"), + # Clone a specific commit + ("@65cf0a5bdd906ecf48a0ac241c17d656d2071d56", + "65cf0a5bdd906ecf48a0ac241c17d656d2071d56") +]) +def test_install_git_logs_commit_sha(script, rev, expected_sha, tmpdir): + """ + Test installing from a git repository logs a commit SHA. + """ + url_path = "pypa/pip-test-package.git" + base_local_url = _github_checkout(url_path, tmpdir) + local_url = f"{base_local_url}{rev}#egg=pip-test-package" + result = script.pip("install", local_url) + # `[4:]` removes a 'git+' prefix + assert f"Resolved {base_local_url[4:]} to commit {expected_sha}" in result.stdout + + @pytest.mark.network def test_git_with_tag_name_and_update(script, tmpdir): """