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

Report actual attempted Git command when Git.refresh fails #1812

Merged
merged 10 commits into from
Jan 26, 2024

Conversation

EliahKagan
Copy link
Contributor

@EliahKagan EliahKagan commented Jan 25, 2024

Fixes #1809
Fixes #1811

This modifies Git.refresh to report the attempted Git command rather than always git even if a different command was used. This affects the command attribute of the GitCommandNotFound exception, as well as (and automatically) the text displayed as cmdline: since that is from that attribute.

It also expands and adds more tests of git.refesh (which calls Git.refresh) to encompass this behavior, verifying that they fail before #1809 is fixed and pass afterwards, as well as verify some behavior that was already established but not yet under test.

Since #1811 would otherwise be exacerbated by these changes, I've also made it so those tests roll back the global changes they make that may affect other test by calling git.refresh() with no arguments. I believe this can be considered to fix that issue.

There is a bit more information in the commit messages, including about why I've taken that approach to #1811, at least so far, rather than specifically capturing and restoring the pieces of state that are currently affected.

This PR also includes some minor refactoring and comment revision for readability in some code called by git.refresh that should make no behavioral change. This includes an improvement to the wording of the comment in Git.refresh discussed in review comments on #1810; the opportunity for this came closer than I'd predicted in #1810 (comment). (It might be possible to improve it further.)

Edit: Corrected "some behavior that was not established" to "some behavior that was already established but not yet under test".

For when refreshing should succeed and when it should fail.
shutil.which does not always find the git that Popen would run, at
least on Windows, but it is no worse than running a "command -v" or
"where" command as was previously done here via os.popen.
This extends test_refresh_bad_git_path so that it asserts that the
exception message shows the command the failed refresh used.

This test fails due to a bug where "git" is always shown (gitpython-developers#1809).
See gitpython-developers#1809. The test extended in the previous commit now passes.
+ Test a successful refresh with a relative path, which will be
  safer to do once the refresh tests restore changed state.

See gitpython-developers#1811. This addresses it incompletely, because while it is
probably not necessary while running the test suite to preserve an
old False value of git.GIT_OK (most tests can't work if that
happens anyway), the value of Git.GIT_PYTHON_GIT_EXECUTABLE is not
the only other global state that effects the behavior of
subsequently run tests and that may be changed as a result of the
refresh tests.

1. After the git.refresh function calls git.cmd.Git.refresh, it
   calls git.remote.FetchInfo.refresh, which rebinds the
   git.remote.FetchInfo._flag_map attribute.

2. Future changes to git.cmd.Git.refresh may mutate other state in
   the Git class, and ideally the coupling would be loose enough
   that the refresh tests wouldn't have to be updated for that if
   the behavior being tested does not change.

3. Future changes to git.refresh may perform other refreshing
   actions, and ideally it would be easy (and obvious) what has to
   be done to patch it back. In particular, it will likely call
   another Git method that mutates class-wide state due to gitpython-developers#1791,
   and for such state that is also of the Git class, ideally no
   further changes would have to be made to the code that restores
   state after the refresh tests.

If we assume git.refresh is working at least in the case that it is
called with no arguments, then the cleanup can just be a call to
git.refresh(). Otherwise, sufficiently general cleanup may be more
complicated.
For gitpython-developers#1811. This is the simple way to do it. This relies on
git.refresh working when called when no arguments, so if that ever
breaks, then it may be difficult or inefficient to investigate. But
this is simpler than any alternatives that are equally or more
robust.

This is already better than the previous incomplete way that only
restored Git.GIT_PYTHON_GIT_EXECUTABLE (and nothing in FetchInfo).
Furthermore, because the tests already depend to a large extent on
git.refreh working when called with no arguments, as otherwise the
initial refresh may not have set Git.GIT_PYTHON_GIT_EXECUTABLE
usably, it might not be worthwhile to explore other approaches.
I think this refactoring slightly improves readability.

+ Condense/sort/group imports (while adding contextlib import).

+ Minor style tweaks in FetchInfo.refresh comments.

+ Make a comment in Git.refresh clearer, building on revisions in:
  gitpython-developers#1810 (comment)
This adds short docstrings to each of the four refresh tests to
briefly state the claims they verify.
@EliahKagan EliahKagan marked this pull request as ready for review January 25, 2024 09:45
Copy link
Member

@Byron Byron left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks a lot for tackling this, it's much appreciated!

(On a personal note, your contributions are so numerous, I have to think about ways to express my gratitude in a non-repetitive fashion 😅 - it's a great problem to have though, please keep 'em coming)

test/test_git.py Show resolved Hide resolved
@Byron Byron merged commit 9a7cec1 into gitpython-developers:main Jan 26, 2024
22 checks passed
@EliahKagan EliahKagan deleted the refresh-cmdline branch January 26, 2024 10:11
lettuce-bot bot referenced this pull request in lettuce-financial/github-bot-signed-commit Feb 15, 2024
[![Mend
Renovate](https://app.renovatebot.com/images/banner.svg)](https://renovatebot.com)

This PR contains the following updates:

| Package | Change | Age | Adoption | Passing | Confidence |
|---|---|---|---|---|---|
| [GitPython](https://github.com/gitpython-developers/GitPython) |
`==3.1.41` -> `==3.1.42` |
[![age](https://developer.mend.io/api/mc/badges/age/pypi/GitPython/3.1.42?slim=true)](https://docs.renovatebot.com/merge-confidence/)
|
[![adoption](https://developer.mend.io/api/mc/badges/adoption/pypi/GitPython/3.1.42?slim=true)](https://docs.renovatebot.com/merge-confidence/)
|
[![passing](https://developer.mend.io/api/mc/badges/compatibility/pypi/GitPython/3.1.41/3.1.42?slim=true)](https://docs.renovatebot.com/merge-confidence/)
|
[![confidence](https://developer.mend.io/api/mc/badges/confidence/pypi/GitPython/3.1.41/3.1.42?slim=true)](https://docs.renovatebot.com/merge-confidence/)
|

---

### Release Notes

<details>
<summary>gitpython-developers/GitPython (GitPython)</summary>

###
[`v3.1.42`](https://github.com/gitpython-developers/GitPython/releases/tag/3.1.42)

[Compare
Source](https://github.com/gitpython-developers/GitPython/compare/3.1.41...3.1.42)

#### What's Changed

- Fix release link in changelog by
[@&#8203;PeterJCLaw](https://github.com/PeterJCLaw) in
[https://github.com/gitpython-developers/GitPython/pull/1795](https://github.com/gitpython-developers/GitPython/pull/1795)
- Remove test dependency on sumtypes library by
[@&#8203;EliahKagan](https://github.com/EliahKagan) in
[https://github.com/gitpython-developers/GitPython/pull/1798](https://github.com/gitpython-developers/GitPython/pull/1798)
- Pin Sphinx plugins to compatible versions by
[@&#8203;EliahKagan](https://github.com/EliahKagan) in
[https://github.com/gitpython-developers/GitPython/pull/1803](https://github.com/gitpython-developers/GitPython/pull/1803)
- fix: treeNotSorted issue by
[@&#8203;et-repositories](https://github.com/et-repositories) in
[https://github.com/gitpython-developers/GitPython/pull/1799](https://github.com/gitpython-developers/GitPython/pull/1799)
- Remove git.util.NullHandler by
[@&#8203;EliahKagan](https://github.com/EliahKagan) in
[https://github.com/gitpython-developers/GitPython/pull/1807](https://github.com/gitpython-developers/GitPython/pull/1807)
- Clarify why GIT_PYTHON_GIT_EXECUTABLE may be set on failure by
[@&#8203;EliahKagan](https://github.com/EliahKagan) in
[https://github.com/gitpython-developers/GitPython/pull/1810](https://github.com/gitpython-developers/GitPython/pull/1810)
- Report actual attempted Git command when Git.refresh fails by
[@&#8203;EliahKagan](https://github.com/EliahKagan) in
[https://github.com/gitpython-developers/GitPython/pull/1812](https://github.com/gitpython-developers/GitPython/pull/1812)
- Don't suppress messages when logging is not configured by
[@&#8203;EliahKagan](https://github.com/EliahKagan) in
[https://github.com/gitpython-developers/GitPython/pull/1813](https://github.com/gitpython-developers/GitPython/pull/1813)
- Pin Python 3.9.16 on Cygwin CI by
[@&#8203;EliahKagan](https://github.com/EliahKagan) in
[https://github.com/gitpython-developers/GitPython/pull/1814](https://github.com/gitpython-developers/GitPython/pull/1814)
- Have initial refresh use a logger to warn by
[@&#8203;EliahKagan](https://github.com/EliahKagan) in
[https://github.com/gitpython-developers/GitPython/pull/1815](https://github.com/gitpython-developers/GitPython/pull/1815)
- Omit warning prefix in "Bad git executable" message by
[@&#8203;EliahKagan](https://github.com/EliahKagan) in
[https://github.com/gitpython-developers/GitPython/pull/1816](https://github.com/gitpython-developers/GitPython/pull/1816)
- Test with M1 macOS CI runner by
[@&#8203;EliahKagan](https://github.com/EliahKagan) in
[https://github.com/gitpython-developers/GitPython/pull/1817](https://github.com/gitpython-developers/GitPython/pull/1817)
- Bump pre-commit/action from 3.0.0 to 3.0.1 by
[@&#8203;dependabot](https://github.com/dependabot) in
[https://github.com/gitpython-developers/GitPython/pull/1818](https://github.com/gitpython-developers/GitPython/pull/1818)
- Bump Vampire/setup-wsl from 2.0.2 to 3.0.0 by
[@&#8203;dependabot](https://github.com/dependabot) in
[https://github.com/gitpython-developers/GitPython/pull/1819](https://github.com/gitpython-developers/GitPython/pull/1819)
- Remove deprecated section in README.md by
[@&#8203;marcm-ml](https://github.com/marcm-ml) in
[https://github.com/gitpython-developers/GitPython/pull/1823](https://github.com/gitpython-developers/GitPython/pull/1823)
- Keep temp files out of project dir and improve cleanup by
[@&#8203;EliahKagan](https://github.com/EliahKagan) in
[https://github.com/gitpython-developers/GitPython/pull/1825](https://github.com/gitpython-developers/GitPython/pull/1825)

#### New Contributors

- [@&#8203;PeterJCLaw](https://github.com/PeterJCLaw) made their first
contribution in
[https://github.com/gitpython-developers/GitPython/pull/1795](https://github.com/gitpython-developers/GitPython/pull/1795)
- [@&#8203;et-repositories](https://github.com/et-repositories) made
their first contribution in
[https://github.com/gitpython-developers/GitPython/pull/1799](https://github.com/gitpython-developers/GitPython/pull/1799)
- [@&#8203;marcm-ml](https://github.com/marcm-ml) made their first
contribution in
[https://github.com/gitpython-developers/GitPython/pull/1823](https://github.com/gitpython-developers/GitPython/pull/1823)

**Full Changelog**:
gitpython-developers/GitPython@3.1.41...3.1.42

</details>

---

### Configuration

📅 **Schedule**: Branch creation - At any time (no schedule defined),
Automerge - At any time (no schedule defined).

🚦 **Automerge**: Disabled by config. Please merge this manually once you
are satisfied.

♻ **Rebasing**: Whenever PR becomes conflicted, or you tick the
rebase/retry checkbox.

🔕 **Ignore**: Close this PR and you won't be reminded about these
updates again.

---

- [ ] <!-- rebase-check -->If you want to rebase/retry this PR, check
this box

---

This PR has been generated by [Mend
Renovate](https://www.mend.io/free-developer-tools/renovate/). View
repository job log
[here](https://developer.mend.io/github/lettuce-financial/github-bot-signed-commit).

<!--renovate-debug:eyJjcmVhdGVkSW5WZXIiOiIzNy4xNzMuMCIsInVwZGF0ZWRJblZlciI6IjM3LjE3My4wIiwidGFyZ2V0QnJhbmNoIjoibWFpbiJ9-->
renovate bot referenced this pull request in allenporter/flux-local Feb 16, 2024
[![Mend
Renovate](https://app.renovatebot.com/images/banner.svg)](https://renovatebot.com)

This PR contains the following updates:

| Package | Change | Age | Adoption | Passing | Confidence |
|---|---|---|---|---|---|
| [GitPython](https://github.com/gitpython-developers/GitPython) |
`==3.1.41` -> `==3.1.42` |
[![age](https://developer.mend.io/api/mc/badges/age/pypi/GitPython/3.1.42?slim=true)](https://docs.renovatebot.com/merge-confidence/)
|
[![adoption](https://developer.mend.io/api/mc/badges/adoption/pypi/GitPython/3.1.42?slim=true)](https://docs.renovatebot.com/merge-confidence/)
|
[![passing](https://developer.mend.io/api/mc/badges/compatibility/pypi/GitPython/3.1.41/3.1.42?slim=true)](https://docs.renovatebot.com/merge-confidence/)
|
[![confidence](https://developer.mend.io/api/mc/badges/confidence/pypi/GitPython/3.1.41/3.1.42?slim=true)](https://docs.renovatebot.com/merge-confidence/)
|

---

### Release Notes

<details>
<summary>gitpython-developers/GitPython (GitPython)</summary>

###
[`v3.1.42`](https://github.com/gitpython-developers/GitPython/releases/tag/3.1.42)

[Compare
Source](https://github.com/gitpython-developers/GitPython/compare/3.1.41...3.1.42)

#### What's Changed

- Fix release link in changelog by
[@&#8203;PeterJCLaw](https://github.com/PeterJCLaw) in
[https://github.com/gitpython-developers/GitPython/pull/1795](https://github.com/gitpython-developers/GitPython/pull/1795)
- Remove test dependency on sumtypes library by
[@&#8203;EliahKagan](https://github.com/EliahKagan) in
[https://github.com/gitpython-developers/GitPython/pull/1798](https://github.com/gitpython-developers/GitPython/pull/1798)
- Pin Sphinx plugins to compatible versions by
[@&#8203;EliahKagan](https://github.com/EliahKagan) in
[https://github.com/gitpython-developers/GitPython/pull/1803](https://github.com/gitpython-developers/GitPython/pull/1803)
- fix: treeNotSorted issue by
[@&#8203;et-repositories](https://github.com/et-repositories) in
[https://github.com/gitpython-developers/GitPython/pull/1799](https://github.com/gitpython-developers/GitPython/pull/1799)
- Remove git.util.NullHandler by
[@&#8203;EliahKagan](https://github.com/EliahKagan) in
[https://github.com/gitpython-developers/GitPython/pull/1807](https://github.com/gitpython-developers/GitPython/pull/1807)
- Clarify why GIT_PYTHON_GIT_EXECUTABLE may be set on failure by
[@&#8203;EliahKagan](https://github.com/EliahKagan) in
[https://github.com/gitpython-developers/GitPython/pull/1810](https://github.com/gitpython-developers/GitPython/pull/1810)
- Report actual attempted Git command when Git.refresh fails by
[@&#8203;EliahKagan](https://github.com/EliahKagan) in
[https://github.com/gitpython-developers/GitPython/pull/1812](https://github.com/gitpython-developers/GitPython/pull/1812)
- Don't suppress messages when logging is not configured by
[@&#8203;EliahKagan](https://github.com/EliahKagan) in
[https://github.com/gitpython-developers/GitPython/pull/1813](https://github.com/gitpython-developers/GitPython/pull/1813)
- Pin Python 3.9.16 on Cygwin CI by
[@&#8203;EliahKagan](https://github.com/EliahKagan) in
[https://github.com/gitpython-developers/GitPython/pull/1814](https://github.com/gitpython-developers/GitPython/pull/1814)
- Have initial refresh use a logger to warn by
[@&#8203;EliahKagan](https://github.com/EliahKagan) in
[https://github.com/gitpython-developers/GitPython/pull/1815](https://github.com/gitpython-developers/GitPython/pull/1815)
- Omit warning prefix in "Bad git executable" message by
[@&#8203;EliahKagan](https://github.com/EliahKagan) in
[https://github.com/gitpython-developers/GitPython/pull/1816](https://github.com/gitpython-developers/GitPython/pull/1816)
- Test with M1 macOS CI runner by
[@&#8203;EliahKagan](https://github.com/EliahKagan) in
[https://github.com/gitpython-developers/GitPython/pull/1817](https://github.com/gitpython-developers/GitPython/pull/1817)
- Bump pre-commit/action from 3.0.0 to 3.0.1 by
[@&#8203;dependabot](https://github.com/dependabot) in
[https://github.com/gitpython-developers/GitPython/pull/1818](https://github.com/gitpython-developers/GitPython/pull/1818)
- Bump Vampire/setup-wsl from 2.0.2 to 3.0.0 by
[@&#8203;dependabot](https://github.com/dependabot) in
[https://github.com/gitpython-developers/GitPython/pull/1819](https://github.com/gitpython-developers/GitPython/pull/1819)
- Remove deprecated section in README.md by
[@&#8203;marcm-ml](https://github.com/marcm-ml) in
[https://github.com/gitpython-developers/GitPython/pull/1823](https://github.com/gitpython-developers/GitPython/pull/1823)
- Keep temp files out of project dir and improve cleanup by
[@&#8203;EliahKagan](https://github.com/EliahKagan) in
[https://github.com/gitpython-developers/GitPython/pull/1825](https://github.com/gitpython-developers/GitPython/pull/1825)

#### New Contributors

- [@&#8203;PeterJCLaw](https://github.com/PeterJCLaw) made their first
contribution in
[https://github.com/gitpython-developers/GitPython/pull/1795](https://github.com/gitpython-developers/GitPython/pull/1795)
- [@&#8203;et-repositories](https://github.com/et-repositories) made
their first contribution in
[https://github.com/gitpython-developers/GitPython/pull/1799](https://github.com/gitpython-developers/GitPython/pull/1799)
- [@&#8203;marcm-ml](https://github.com/marcm-ml) made their first
contribution in
[https://github.com/gitpython-developers/GitPython/pull/1823](https://github.com/gitpython-developers/GitPython/pull/1823)

**Full Changelog**:
gitpython-developers/GitPython@3.1.41...3.1.42

</details>

---

### Configuration

📅 **Schedule**: Branch creation - At any time (no schedule defined),
Automerge - At any time (no schedule defined).

🚦 **Automerge**: Enabled.

♻ **Rebasing**: Whenever PR becomes conflicted, or you tick the
rebase/retry checkbox.

🔕 **Ignore**: Close this PR and you won't be reminded about this update
again.

---

- [ ] <!-- rebase-check -->If you want to rebase/retry this PR, check
this box

---

This PR has been generated by [Mend
Renovate](https://www.mend.io/free-developer-tools/renovate/). View
repository job log
[here](https://developer.mend.io/github/allenporter/flux-local).

<!--renovate-debug:eyJjcmVhdGVkSW5WZXIiOiIzNy4xNzMuMCIsInVwZGF0ZWRJblZlciI6IjM3LjE3My4wIiwidGFyZ2V0QnJhbmNoIjoibWFpbiJ9-->

Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
@EliahKagan EliahKagan mentioned this pull request Apr 1, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
2 participants