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

Log the resolved commit SHA when installing package via Git URI to make it easier to debug #10135

Closed
harupy opened this issue Jul 6, 2021 · 8 comments
Labels
C: logging Information Logging C: vcs pip's interaction with version control systems like git, svn and bzr

Comments

@harupy
Copy link
Contributor

harupy commented Jul 6, 2021

What's the problem this feature will solve?

Currently, when we install a package via Git URI, pip doesn't tell us what the current commit SHA is:

$ pip install git+https://github.com/scikit-learn/scikit-learn.git
Collecting git+https://github.com/scikit-learn/scikit-learn.git
  Cloning https://github.com/scikit-learn/scikit-learn.git to /tmp/pip-req-build-lna_339q
  Running command git clone -q https://github.com/scikit-learn/scikit-learn.git /tmp/pip-req-build-lna_339q

Let's say we run the command above in CI to run tests against the dev version of scikit-learn. If the current commit hash was shown, it would help us indetify commits/changes that break the tests.

Describe the solution you'd like

Just show the current commit SHA after cloning the repo.

Alternative Solutions

$ pip freeze | grep scikit-learn
scikit-learn @ git+https://github.com/scikit-learn/scikit-learn.git@f2fb07581339f063b501b3e820917d66ee4c1fa3

# However, this doesn't work for the following use case:
$ pip wheel git+https://github.com/scikit-learn/scikit-learn.git
$ pip install path/to/built/wheel
$ pip freeze | grep scikit-learn
scikit-learn @ file:/path/to/built/wheel

Additional context

@uranusjr
Copy link
Member

uranusjr commented Jul 7, 2021

I believe this is by design. When a wheel is built, the package is no longer a Git repository, so installing from it won't provide any Git information. If you need to preserve that information, you need to encode it into the wheel (e.g. in the version string, a few projects are already doing this, and there are helper tools like setuptools-scm).

@uranusjr uranusjr closed this as completed Jul 7, 2021
@uranusjr uranusjr added the resolution: no action When the resolution is to not do anything label Jul 7, 2021
@harupy
Copy link
Contributor Author

harupy commented Jul 8, 2021

@uranusjr Thanks for the feedback! Can we just show the current commit hash (which I think is useful for debugging) after git clone?

$ pip {install | wheel} git+https://github.com/scikit-learn/scikit-learn.git
Collecting git+https://github.com/scikit-learn/scikit-learn.git
  Cloning https://github.com/scikit-learn/scikit-learn.git to /tmp/pip-req-build-lna_339q
  Running command git clone -q https://github.com/scikit-learn/scikit-learn.git /tmp/pip-req-build-lna_339q
+ #  Just show the current commit SHA here

@uranusjr
Copy link
Member

uranusjr commented Jul 8, 2021

IIRC (didn't check) it's already available in debug mode, and moving it to INFO does make sense to me. We also recently introduced a new verbose log level, so changing it to show in -v also makes sense. And if it wasn't already present, we should add it.

A PR would be very welcomed!

@harupy
Copy link
Contributor Author

harupy commented Jul 8, 2021

@uranusjr Thanks for the reply!

I'm taking a look at this file: https://github.com/pypa/pip/blob/main/src/pip/_internal/vcs/git.py, but I don't see a line that show the commit SHA.

git rev-parse is called in get_revision: https://github.com/pypa/pip/blob/main/src/pip/_internal/vcs/git.py#L395. It looks like this function is called only when we specify a revision:

pip install git+https://github.com/foo/bar@{branch or commit SHA}

@uranusjr
Copy link
Member

uranusjr commented Jul 8, 2021

Yeah, it does not seem to have logged to debug right now (thanks for the link!) so we should just add it. I think it should be done a few stacks above get_revision, in fetch_new (which calls fetch_revision which calls get_revision). You can see this is also where the Cloning [URL] to [path] message is emitted. There is a RevOptions object in that function, it should contain the commit hash.

@uranusjr uranusjr added C: logging Information Logging C: vcs pip's interaction with version control systems like git, svn and bzr and removed resolution: no action When the resolution is to not do anything labels Jul 8, 2021
@uranusjr uranusjr reopened this Jul 8, 2021
@uranusjr uranusjr changed the title Show the current commit SHA when installing package via Git URI to make it easier to debug Log the resolved commit SHA when installing package via Git URI to make it easier to debug Jul 8, 2021
@harupy
Copy link
Contributor Author

harupy commented Jul 8, 2021

@uranusjr Thanks! I'm taking a look at fetch_new to decide where to update. Does the following code look good to you?

    def fetch_new(self, dest, url, rev_options):
        # type: (str, HiddenText, RevOptions) -> None
        rev_display = rev_options.to_display()
        logger.info('Cloning %s%s to %s', url, rev_display, display_path(dest))
        self.run_command(make_command('clone', '-q', url, dest))

        if rev_options.rev:
            # Then a specific revision was requested.
            rev_options = self.resolve_revision(dest, url, rev_options)
            ...

        ##### NEW LINES #####
        else:
            sha = self.get_revision(dest)
            rev_options = rev_options.make_new(sha)
        
        logger.info('Commit SHA: %s' rev_options.rev)
        ##### NEW LINES #####

        #: repo may contain submodules
        self.update_submodules(dest)

@uranusjr
Copy link
Member

uranusjr commented Jul 9, 2021

Yeah I think that makes sense. We’ll probably need some tests, and if possible, add the same to other vcs backends as well, but we can discuss that after the pull request is created.

@harupy
Copy link
Contributor Author

harupy commented Jul 9, 2021

Got it, I'll file a PR!

@harupy harupy closed this as completed Oct 16, 2021
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Nov 15, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
C: logging Information Logging C: vcs pip's interaction with version control systems like git, svn and bzr
Projects
None yet
Development

No branches or pull requests

2 participants