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

Have initial refresh use a logger to warn #1815

Merged
merged 4 commits into from
Feb 2, 2024

Conversation

EliahKagan
Copy link
Contributor

@EliahKagan EliahKagan commented Feb 2, 2024

Fixes #1808

Code changes in the git module:

  • When the initial call to git.refresh that happens on import fails to find a git command and is configured to warn, it warns using a logger rather than with print. Although this is conceptually a warning, it is emitted with level CRTICAL, for the reasons given in ac50d8e (the commit in this PR that changes the tests for it).

  • The message is changed to document this.

  • A few more values, for existing GIT_PYTHON_REFRESH modes, are added as synonyms. No new modes are added, nor are their states expanded. The motivation for adding new synonyms is so log achieves logging, i.e., it is another way to say warn. But l is also added as its short form. (This might ordinarily be objected to on the grounds that, when written as l and not the also-recognized L, it could be confused with 1. Fortunately 1 also means warn, so 1 and l will have the same meaning anyway.) Furthermore, silent is added as a synonym of quiet, since silence is already recognized for that, so silent seems likely to be used by accident anyway.

    The reason I have not gone further at this time (as was discussed in the second part of this comment and in this reply) is it can be made more configurable in the future if it looks like that is valuable, but removing existing configurability could break more uses. Furthermore, because the level is CRITICAL which differs from that of any other GitPython messages, the better approach of controlling the effect in an application's own logging configuration may be sufficient.

Test changes:

  • Rename some existing tests more specifically, so they are not confused with new ones. These are the tests of subsequent (i.e., non-initial) calls to git.refresh when an explicit path argument is passed.

  • Test subsequent refreshes performed with no arguments. That is, test calls to git.refresh with no arguments, without simulating the state of not yet having performed an initial refresh. This behavior is not changed in this pull request, but it makes sense to test it, doing so may avoid confusion with the different initial-refresh behavior when reading the tests, and the important subtlety of when relative paths are and aren't resolved is made clearer.

  • Test an initial refresh. This is a refresh with no arguments that is performed on import, but to make the relationship to the other tests clearer, reduce test complexity, and make the tests run faster (since some of them run with all documented values of GIT_PYTHON_REFRESH), this simulates that state by setting Git.GIT_PYTHON_GIT_EXECUTABLE to None rather than starting a Python subprocess to do an import.

    This includes testing the behavioral change made in this PR and described above.

  • Test the new synonyms of existing values for GIT_PYTHON_REFRESH (such as log for warn).

I added the tests first. For the initial-refresh test, I first wrote it for the preexisting behavior, modified it for the desired behavior and saw it fail. Then I applied the changes, causing the tests to pass again. Those changes are based on, but not quite the same as, dc62096 as discussed in #1813 and #1808 (comment).

There is some more information in commit messages, especially ac50d8e.

This adds tests like the existing ones but for when git.refresh is
called with no arguments and the path is provided as the value of
the GIT_PYTHON_GIT_EXECUTABLE environment variable.

The preexisting tests, which this retains unchanged except with
slightly more specific names to avoid confusion with the new tests,
are of git.refresh(path).

One benefit of these tests is to make a subtle but important aspect
of the established behavior clear: relative paths are immediately
resolved when passed as a path argument, but they are kept relative
when given as the value of the environment variable.
This adds tests of the initial refresh that is attempted
automatically on import. All the refresh tests prior to this point
test subsequent refreshes. Those tests are kept, and new ones are
added that simulate the condition of not having yet done the
initial refresh by setting Git.GIT_PYTHON_GIT_EXECUTABLE to None.

Some current behavior these tests assert may change for gitpython-developers#1808.
This is instead of the current behavior writing the message to
stdout.

This commit does not change the behavior of the code under test,
but it changes tests to assert the following:

- "Bad git executable" messages are logged, at level CRITICAL.
- "log" (and "l") is recognized as another synonym of "warn".
- "silent" is recognized as a synonym of "quiet" (as "silence" is).

Although it is ambiguous whether this should be logged at level
ERROR or CRITICAL, because git.refresh is still expected to be
usable and can be called manually, not having a working git is a
condition in which GitPython, and any program that really relies on
its functionality, should be expected not work. That is the general
rationale for using CRIICAL here. There are also two specific
reasons:

- Existing messages GitPython logs as ERROR typically represent
  errors in individual operations on repositories, which could fail
  without indicating that GitPython's overall functionality is in
  an unusable state. Using the higher CRITICAL level for this
  situation makes sense for contrast.

- Prior to gitpython-developers#1813, logging messsges emitted from GitPython modules,
  no matter the level, were suppressed when logging was not
  configured, but because this message was printed instead of
  being logged, it was still shown. Now that it is to be logged,
  there may be a benefit to have an easy way for someone to bring
  back a close approximation of the old behavior. Having this
  message be at a higher logging level makes that easier to do.
  (This is a less important reason, as that should rarely be done.)

test_initial_refresh_from_bad_git_path_env_warn is the main changed
test. All tests should pass again once code is changed for gitpython-developers#1808.
@EliahKagan EliahKagan marked this pull request as ready for review February 2, 2024 12:50
@EliahKagan EliahKagan marked this pull request as draft February 2, 2024 12:56
@EliahKagan EliahKagan marked this pull request as ready for review February 2, 2024 12:59
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 so much! With this PR it seems reasonable to claim that the messaging in GitPython is finally consistent. Everything uses logging, and messages are no suppressed by default.

I particularly love that you managed to do test-driven development here, that's the way (for those who have the experience to set up such tests in the first place).

Admission: I only skimmed the tests themselves due to my own state of mind and the trust in the PRs author.

@Byron Byron merged commit db6fb90 into gitpython-developers:main Feb 2, 2024
43 checks passed
@EliahKagan EliahKagan deleted the refresh-env branch February 2, 2024 23:53
@EliahKagan
Copy link
Contributor Author

Although it's not really an inconsistency in the sense that you mean, I should mention that the way the message is prefixed is imperfect. I am unsure if there is a better way. The message itself begins, as before:

WARNING: Bad git executable.

When logging is not configured, logging.lastResort prints that to standard error just like that. However, if logging has been configured, then most commonly a prefix indicating the logger and level is shown. In particular, when logging.basicConfig() is called with no arguments to configure logging, the actual logged message is of course the same, but the way it is displayed begins:

CRITICAL:git.cmd:WARNING: Bad git executable.

That's a bit weird, but I'm unsure if anything should be done to change it. I don't recommend having the message itself differ based on whether or not or how logging is configured. But maybe WARNING should be changed to to Warning so it looks less like a contradictory indication of a logging level? Then again, that may make it slightly less visible when no prefix is shown. I am not sure.

With this PR it seems reasonable to claim that the messaging in GitPython is finally consistent.

I plan to look for possible remaining inconsistencies, so I'll try to either comment here about not finding them, or open one or more issues or PRs for any that I do find.

Everything uses logging, and messages are no suppressed by default.

I agree. In particular, I looked for other print statements in the git module and did not find them.

Admission: I only skimmed the tests themselves due to my own state of mind and the trust in the PRs author.

The tests contain some code duplication. The ways of reducing it that I thought of didn't seem like they would necessarily be improvements, though future changes might worsen the situation and make it more important to extract shared logic, if it becomes important to test more combinations of environment variables.

I bring this up only in case you already happened to notice that the duplication, or volume of the new tests, made them less scrutable than ideal. If that was the case, I can take another look at them.

@Byron
Copy link
Member

Byron commented Feb 3, 2024

That's a bit weird, but I'm unsure if anything should be done to change it. I don't recommend having the message itself differ based on whether or not or how logging is configured. But maybe WARNING should be changed to to Warning so it looks less like a contradictory indication of a logging level? Then again, that may make it slightly less visible when no prefix is shown. I am not sure.

My intuition here is to remove it, and allow logging to deal with levels so GitPython can focus on the message.

I bring this up only in case you already happened to notice that the duplication, or volume of the new tests, made them less scrutable than ideal. If that was the case, I can take another look at them.

I would not suggest to spend time on any deduplication, it was just me feeling a bit rushed while still thinking the PR should be merged, all things considered.

EliahKagan added a commit to EliahKagan/GitPython that referenced this pull request Feb 6, 2024
This changes test_initial_refresh_from_bad_git_path_env_warn to
assert a message with no added "WARNING:" prefix. As discussed
in gitpython-developers#1815, the extra prefix is confusing with logging configured,
showing "CRITICAL:git.cmd:WARNING: Bad git executable." instead of
"CRITICAL:git.cmd: Bad git executable."
@EliahKagan
Copy link
Contributor Author

The one other inconsistency I found in the messages is that the git command used is shown only in the exception message on a subsequent refresh, but not shown in exception message from an initial refresh or in the logged message on an initial refresh in warn mode.

I am unsure if this should really be changed or if it even really is a real inconsistency. ImportError is raised on an initial failed refresh, while GitCommandNotFoundError is raised on a failed subsequent refresh.

I suspect the reason it was not included was that there was never originally a goal to include it anywhere, which may also partially explain #1809. Even though #1809 is fixed by the changes in #1812, I don't think this necessarily means the other messages--which are not in any way inaccurate or misleading--need to be expanded to include that information.

Other than the above, I can think of two other reasons the information may not be included in those messages, one of which is obsolete but the other of which may still be applicable:

  • Obsolete reason: In rare cases the path could be sensitive. It is not sensitive enough that it shouldn't be logged, but the message used to be printed to standard output with print rather than using the logging framework, and some applications (e.g., web applications) that use GitPython might make standard output accessible to users who are not fully trusted. This is a marginal reason and I suspect it was not even a factor, but even if this was the reason, it is not applicable anymore because logging is used since #1808.
  • Still-relevant reason: The message is already long and complicated enough. If it gave a path and the path is git, then the user probably already knew that. When the path is not git, that is useful debugging information, especially if the user expected it to be git, and this may be a reason to include the path. However, it is possible that some users would try to make git available at that path, rather than configuring the path differently as described in the rest of the message. When investigating problems interactively, one may call refresh manually, but then one gets a GitCommandNotFound exception with the path.

I would not suggest to spend time on any deduplication, it was just me feeling a bit rushed while still thinking the PR should be merged, all things considered.

No problem! If we end up showing the command in more messages as discussed above, then the duplication in the tests (to test such a change) might become worse, in which case I'll take another look. Otherwise I won't worry about it.

My intuition here is to remove it, and allow logging to deal with levels so GitPython can focus on the message.

I've made that change in #1816.

lettuce-bot bot added a commit to lettuce-financial/github-bot-signed-commit that referenced this pull request 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://togithub.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://togithub.com/gitpython-developers/GitPython/releases/tag/3.1.42)

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

#### What's Changed

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

#### New Contributors

- [@&#8203;PeterJCLaw](https://togithub.com/PeterJCLaw) made their first
contribution in
[gitpython-developers/GitPython#1795
- [@&#8203;et-repositories](https://togithub.com/et-repositories) made
their first contribution in
[gitpython-developers/GitPython#1799
- [@&#8203;marcm-ml](https://togithub.com/marcm-ml) made their first
contribution in
[gitpython-developers/GitPython#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 added a commit to allenporter/flux-local that referenced this pull request 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://togithub.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://togithub.com/gitpython-developers/GitPython/releases/tag/3.1.42)

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

#### What's Changed

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

#### New Contributors

- [@&#8203;PeterJCLaw](https://togithub.com/PeterJCLaw) made their first
contribution in
[gitpython-developers/GitPython#1795
- [@&#8203;et-repositories](https://togithub.com/et-repositories) made
their first contribution in
[gitpython-developers/GitPython#1799
- [@&#8203;marcm-ml](https://togithub.com/marcm-ml) made their first
contribution in
[gitpython-developers/GitPython#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 added a commit to EliahKagan/GitPython that referenced this pull request Feb 24, 2024
The fragment of the refresh failure message (which is often written
to a terminal) about the effect of setting GIT_PYTHON_REFRESH to
control refesh failure reporting had previously been formatted with
a maximum line length of 79 columns--in the message itself, not the
code for the message--so that it would fit in a traditional 80
column wide terminal. This remains one of the popular widths to set
for terminal windows in a GUI, so it seems worthwhile to preserve.

In 3a6e3ef (gitpython-developers#1815), I had inadvertently made the line one character
too long for that; at 80 columns, it would cause the newline to be
written at the start of the next line, creating an unsightly extra
line break.

This is pretty minor, but what seems to me like an equally good
alternative wording avoids it, so this commit shortens the wording.
EliahKagan added a commit to EliahKagan/GitPython that referenced this pull request Feb 26, 2024
The fragment of the refresh failure message (which is often written
to a terminal) about the effect of setting GIT_PYTHON_REFRESH to
control refesh failure reporting had previously been formatted with
a maximum line length of 79 columns--in the message itself, not the
code for the message--so that it would fit in a traditional 80
column wide terminal. This remains one of the popular widths to set
for terminal windows in a GUI, so it seems worthwhile to preserve.

In 3a6e3ef (gitpython-developers#1815), I had inadvertently made the line one character
too long for that; at 80 columns, it would cause the newline to be
written at the start of the next line, creating an unsightly extra
line break.

This is pretty minor, but what seems to me like an equally good
alternative wording avoids it, so this commit shortens the wording.
@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
Development

Successfully merging this pull request may close these issues.

Git.refresh warns to stdout and does not log
2 participants