Skip to content

pip freeze figures out the current commit SHA for a git repo but not (reliably) the current branch #2059

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

Closed
bjonen opened this issue Sep 22, 2014 · 6 comments
Labels
auto-locked Outdated issues that have been locked by automation C: vcs pip's interaction with version control systems like git, svn and bzr resolution: deferred till PR Further discussion will happen when a PR is made type: bug A confirmed bug or unintended behavior

Comments

@bjonen
Copy link

bjonen commented Sep 22, 2014

The problem originates from this line.

names_by_commit = dict((commit, ref) for ref, commit in refs.items())

When local branches track up to date remote branches, the mapping from SHA to branch is not unique and remote branches with syntax "origin/my_branch" may be picked (see comment in the code), even though they are NOT checked out.
Finally, pkg_resources does not allow a slash in the version pattern and raises a ValueError if a branch name containing a slash is picked.

My use case:
I have a local git repo which originates from a different local path on my file system. I installed it via:

pip install -e c:/workspace/my_proj

Now I do

pip freeze > requirements.txt

The file looks something like this:

-e git+<path_to_origin>/my_proj.git@<SHA>#egg=my_proj-origin/<branch>

If I then create a new virtual environment and try to install my requirements file I get an error in pip_vendor\pkg_resources.py", line 2573, in scan_list.

ValueError: ('Expected version spec in', 'my_proj-origin/<branch>', 'at', '/<branch>')

I noticed that the problem goes away when the line in the requirements file reads:

-e git+<path_to_origin>/my_proj.git@<SHA>#egg=my_proj-<branch>
@bjonen bjonen changed the title Cannot reinstall editable package (based on local git repo) in fresh virtualenv from requirements.txt pip freeze figures out the current commit SHA for a git repo but not (reliably) the current branch Sep 23, 2014
@bjonen
Copy link
Author

bjonen commented Sep 23, 2014

Following the command proposed here:

git rev-parse --abbrev-ref HEAD

It would be easy to add a method similar to get_revision to git.py

def get_branch:
    current_branch = call_subprocess(
            [self.cmd, 'rev-parse',--abrev-ref, 'HEAD'], show_stdout=False, cwd=location)
        return current_branch.strip()

Then there would be no need for randomly picking a branch, which the user might or might not be currently on. If you want I can prepare a PR for this.

@cjerdonek
Copy link
Member

The non-determinism has been reported here: #1867

Similarly, the issue with the slash has been reported here: #1083

@bjonen
Copy link
Author

bjonen commented Sep 25, 2014

@cjerdonek Thanks for the links!

I don't think #1867 goes far enough though. I think it makes more sense to "freeze" the current branch instead of some pointer to the current commit, even if this pointer is made deterministic through msabramo@7473615.

@dstufft
Copy link
Member

dstufft commented Mar 22, 2017

Looking at this, this appears to still be a real issue, however I believe the correct behavior here is to pin it to a commit and not keep a branch at all. Freezing an environment is intended to reproduce the current environment.

@pradyunsg pradyunsg added resolution: deferred till PR Further discussion will happen when a PR is made type: bug A confirmed bug or unintended behavior C: vcs pip's interaction with version control systems like git, svn and bzr labels Oct 2, 2017
@sbidoul sbidoul added the S: awaiting response Waiting for a response/more information label Jan 29, 2020
@sbidoul
Copy link
Member

sbidoul commented Jan 29, 2020

The branch is not emitted by pip freeze anymore since #3312, only the commit id. So we'll let the bot close this unless there is a reaction otherwise.

@no-response
Copy link

no-response bot commented Feb 13, 2020

This issue has been automatically closed because there has been no response to our request for more information from the original author. With only the information that is currently in the issue, we don't have enough information to take action. Please reach out if you have or find the answers we need so that we can investigate further.

@no-response no-response bot closed this as completed Feb 13, 2020
@lock lock bot added the auto-locked Outdated issues that have been locked by automation label Mar 17, 2020
@lock lock bot locked as resolved and limited conversation to collaborators Mar 17, 2020
@pradyunsg pradyunsg removed the S: awaiting response Waiting for a response/more information label Mar 17, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
auto-locked Outdated issues that have been locked by automation C: vcs pip's interaction with version control systems like git, svn and bzr resolution: deferred till PR Further discussion will happen when a PR is made type: bug A confirmed bug or unintended behavior
Projects
None yet
Development

No branches or pull requests

5 participants