15

If I install a package from git using https://pip.pypa.io/en/stable/reference/pip_install/#git does the specific commit that was checked out logged somewhere?

Ophir Yoktan
  • 8,149
  • 7
  • 58
  • 106

4 Answers4

4

You could use knittl's idea to find the nearest commit -- the only modification below is to address the fact that you are comparing the git tree to an installed package, not a git repository:

Since the installed package may lack some of the directory structure of the git repository, make a new directory for the git repo. I'll use html5lib for an example:

mkdir ~/tmp/html5lib
cd ~/tmp/html5lib/
git init

Now fetch the git tree:

git remote add foreign https://github.com/html5lib/html5lib-python
git fetch foreign

Copy the installed package into the git repo:

rsync -a ~/.virtualenvs/muffy/lib/python3.4/site-packages/html5lib ~/tmp/html5lib/

Run git diff to compare the current state of the repo (with the installed package's code) to each revision in the git tree:

for REV in $(git rev-list --all); do
    echo $(git diff --shortstat foreign/master $REV) $REV ;
done | sort -n

This sorts by the number of files changed, then the number of insertions, then deletions. The output will look something like this:

1 file changed, 3 insertions(+), 1 deletion(-) 17499b9763a090f7715af49555d21fe4b558958b
2 files changed, 10 insertions(+), 8 deletions(-) ec674a97243e76da43f06abfd0a891308f1ff801
3 files changed, 17 insertions(+), 12 deletions(-) 1a28d721091a2c433c6e8471d14cbb75afd70d1c
4 files changed, 18 insertions(+), 13 deletions(-) ff6111cd82191a2eb963d6d662c6da8fa2e7ddde
6 files changed, 19 insertions(+), 19 deletions(-) ea0fafdbff732b1272140b696d6948054ed1d6d2

The last item on each line is the associated git commit.

If the git history is very long you'll want to modify git rev-list --all to a range of commits. For example, use git rev-list tag1..tag2 to search between two tags. If you know approximately when the package was installed, you might have a good guess for what tags to use. Use git tag to show the names of the possible tags. See the docs for more options.

unutbu
  • 842,883
  • 184
  • 1,785
  • 1,677
2

One possible alternative is to use pip install --editable. In such case pip will clone the repo to $PREFIX/src/$egg_name (where $PREFIX is either your virtualenv directory or current working directory), and then just create and egg-link pointing to that path. This way, you will easily be able to check current revision for the cloned repo.

On the other hand, package installed this way might have different file structure than the one installed in a regularl way. So in some cases this won't work.

MarSoft
  • 3,555
  • 1
  • 33
  • 38
1

It is not. If you just want to know — lookup the commit at the head of the installed branch. If you want to install a specific commit — name the commit. For example:

pip install git+https://github.com/sqlobject/sqlobject.git@623a5802#egg=sqlobject
phd
  • 82,685
  • 13
  • 120
  • 165
1

I can easily find the commit hash for a package of mine.

I've used Cookie Cutter tool to generate my python package structure. When I install my package from a GitLab repository, a directory named <package-name>-<version>.dist-info is generated inside my Python Lib directory.

There's a file direct_url.json that records the commit_id of the repository: Install info

This file is a standard file in Python packages as posted in the comments.

neves
  • 33,186
  • 27
  • 159
  • 192
  • 1
    [Recording the Direct URL Origin of installed distributions — Python Packaging User Guide](https://packaging.python.org/en/latest/specifications/direct-url/) – ᄂ ᄀ May 20 '23 at 22:32