GitPython icon indicating copy to clipboard operation
GitPython copied to clipboard

__init__: don't run refresh on import

Open lheckemann opened this issue 3 months ago • 8 comments

Consumers of gitpython may not need to use it in all use cases, and may want to be able to run (without using gitpython) in environments where git is not available on PATH. While this can be worked around by setting the GIT_PYTHON_REFRESH environment variable, adding special handling for gitpython means that it can't be imported just like everything else in an import block at the top of the module, and environment variables have potentially undesired propagation behaviour.

Previously, it was also nontrivial to distinguish gitpython failing to import because of missing git or because e.g. gitpython isn't installed at all, because the exception that's raised is an ImportError without further qualification (except in the error message).

Thus, we now no longer perform refresh at the module top level.


This is technically observable behaviour that could break assumptions made downstream, so I've added a changelog entry with a major version bump. I'm not sure this is "breaking enough" that you would consider it needing a major bump.

Feedback both on the breakingness and on the change in principle very welcome!

lheckemann avatar Sep 23 '25 18:09 lheckemann

Setting this back to draft as I'd not want it merged just yet until it's clear if there are no better solutions to the problem.

Byron avatar Sep 24 '25 03:09 Byron

How does the behaviour of this variant look to you? The GitCommandNotFound exception is a bit awkward as is, but that should be easy enough to clean up.

lheckemann avatar Sep 24 '25 11:09 lheckemann

It looks like CI is failing, and I don't see my question about the lack of refresh() or lazy-refresh addressed.

Byron avatar Sep 25 '25 03:09 Byron

Sorry, I thought this was clear from the diff: I introduced a refresh call in _call_process, conditional on the executable not yet being known, so that it gets called on the first attempt to actually invoke git.

Will take a look at the tests.

lheckemann avatar Sep 25 '25 08:09 lheckemann

Thanks for clarifying. And now, I really have to drag @EliahKagan into this as he has much more experience by now with Python and the Git command.

Byron avatar Sep 25 '25 13:09 Byron

I've decided to avoid GitPython and invoke git myself in this particular use case, where a single command is enough. I still think this would be useful, but the problem is solved for my purposes.

lheckemann avatar Oct 20 '25 11:10 lheckemann

Thanks for sharing the workaround!

Since Eliah doesn't seem to have time for the review, this PR could hang around here for a long time. Maybe it's best to close it to at least serve people by means of offering a workaround to the problem they also might be having. I certainly don't feel confident this PR is anything we can merge, but I also don't know if maybe I am wrong about this or there is another way to fix a legitimate problem.

Byron avatar Oct 21 '25 03:10 Byron

It's true that I won't immediately be able to review this and it could remain open for a while, but if you're willing to have this PR remain open, then I look forward to reviewing it when I have time.

EliahKagan avatar Oct 21 '25 12:10 EliahKagan