Skip to content

fix: respect pre-commit reformats when bumping #505

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

Merged
merged 3 commits into from
May 16, 2022

Conversation

yajo
Copy link
Contributor

@yajo yajo commented Apr 28, 2022

Description

Fix #502. Just attempt to commit twice if the 1st commit fails.

Checklist

  • Add test cases to all the changes you introduce
  • Run ./script/format and ./script/test locally to ensure this change passes linter check and test
  • Test the changes on the local machine manually
  • Update the documentation for the changes

Expected behavior

Compatible with prettier.

Steps to Test This Pull Request

Changelog is now compatible with prettier + editorconfig.

Additional context

@codecov
Copy link

codecov bot commented Apr 28, 2022

Codecov Report

Merging #505 (49f1302) into master (8c2a6f4) will increase coverage by 0.14%.
The diff coverage is 100.00%.

@@            Coverage Diff             @@
##           master     #505      +/-   ##
==========================================
+ Coverage   97.85%   97.99%   +0.14%     
==========================================
  Files          39       39              
  Lines        1539     1548       +9     
==========================================
+ Hits         1506     1517      +11     
+ Misses         33       31       -2     
Flag Coverage Δ
unittests 97.99% <100.00%> (+0.14%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Δ
commitizen/cli.py 93.93% <ø> (ø)
commitizen/__version__.py 100.00% <100.00%> (ø)
commitizen/commands/bump.py 96.42% <100.00%> (+1.23%) ⬆️
commitizen/changelog.py 97.26% <0.00%> (+0.56%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 66c6320...49f1302. Read the comment docs.

@woile
Copy link
Member

woile commented Apr 29, 2022

Thanks!

@Lee-W ? LGTM

@Kurt-von-Laven
Copy link
Contributor

Kurt-von-Laven commented May 2, 2022

Automatically retrying something as potentially permanent as a commit seems quite risky considering Commitizen has no idea what caused the error. We also run Prettier and many other linters via pre-commit and have never had it automatically reformat anything generated by Commitizen. What types of changes is Prettier making? Indentation I'm guessing from your issue? I would suggest this feature be governed by an option (e.g., --retry n, where n defaults to 0 and is the maximum number of retry attempts that will be made) since its ramifications may be somewhat unpredictable.

@yajo
Copy link
Contributor Author

yajo commented May 2, 2022

Automatically retrying something as potentially permanent as a commit seems quite risky considering Commitizen has no idea what caused the error [...] its ramifications may be somewhat unpredictable

In reality there's no risk. If something fails because of reasons the 1st time (e.g.: disk is full, a linter fails...), it will fail with the same reasons the 2nd time.

But if the reason is that a formatter changed the format of the CHANGELOG.md file, then doing the retry will be simply making cz compatible with any pre-commit toolkit out there.

I maintain a similar patch in Copier and it's worked perfectly for years.

I don't think we really need any option to implement this. AFAICS It's a sane fix that works out of the box without any side effects.

What types of changes is Prettier making? Indentation I'm guessing from your issue?

That's one of them. Another one is when a commit contains illegal (or imperfect) markdown in the title. For example, this commit produced a line like:

- use _mrchef origin only privately

and Prettier modified it to add a \ before the _ (apart from indentation).

Both use cases are exercised in the supplied test. Just remove the patch and run the test, you'll see the failure.

@Kurt-von-Laven
Copy link
Contributor

Consider what would happen if a tool (or even just a confused human) were attempting to parse the output though or something else (e.g., an alert regarding the error) was triggered an extra time unintentionally. It would be desirable to have the ability to configure Commitizen in a manner that would avoid alerting twice per error in such cases. Some commit failures are ephemeral, such as a pre-commit hook that conducts a security audit or otherwise hits the network for any reason whatsoever. A really frustrating situation could arise in the presence of a totally unrelated but buggy pre-commit hook that snuck in because it always worked when retried, but sometimes didn't work on the first try.

I meant to say that n would default to 0 when --retry is not present, but 1 when it is. I feel less strongly about the former point though. It sounds from your experience like retrying once is suitable in the vast majority of cases. In any case it might be nice to log a message saying that a retry is occuring (and how to disable it) so that it is clear which tool is triggering this behavior.

@yajo
Copy link
Contributor Author

yajo commented May 3, 2022

If I understand you properly, you're worried that if, for example, somebody sets up a pre-commit hook that launches a rocket 🚀 every time they attempt to commit, we'd be sending two rockets 🚀 🚀 instead of one?

That's very unlikely to happen. And if it happens, it's probably more a bug in the use of pre-commit than a bug in commitizen. I don't think it's a use case that should worry us. The user would be sending 2 rockets anyway whenever prettier reformats another file.

Just think about this: if committing changes to CHANGELOG.md fails the 1st time, why would someone ever want to run cz bump --changelog --retry=0? It's gonna fail anyway, and you already have the (2nd) failure logs. 🤷🏼‍♂️

OTOH, if committing changes to CHANGELOG.md fails the 2nd time, why would you ever want to run cz bump --changelog --retry=0? You already have the logs of the 2nd failure, those will tell you the problem; besides, you also have the dirty changes in your repo. Why would you need more info?

The point is: if your use case was working before this patch, it will continue working (1st commit attempt passes OK). And if it was failing, it could get fixed (if it was a reformat), or it could continue failing (if it was a real failure). So there's nothing we lose with this fix (unless you send rockets with commits, of course). 😁

I'll add some logs for the retry however. That seems an easy way to tell what's happening behind the scenes, in case it matters for some users.

@yajo yajo force-pushed the pre-commit-bump branch from 62b66a7 to 968713c Compare May 3, 2022 07:46
@yajo
Copy link
Contributor Author

yajo commented May 3, 2022

Logs added. In debug mode, you get full error.

@Kurt-von-Laven
Copy link
Contributor

Kurt-von-Laven commented May 3, 2022

If I understand you properly, you're worried that if, for example, somebody sets up a pre-commit hook that launches a rocket rocket every time they attempt to commit, we'd be sending two rockets rocket rocket instead of one?

Not at all. I'm saying a "launch" button that fires two rockets instead of one violates the user design principle of least surprise even if it notifies the user it did so after the fact in the fine print (although that's clearly better than nothing).

The user would be sending 2 rockets anyway whenever prettier reformats another file.

I don't know that I follow the metaphor, but if they wrote code that handles a commit failure in some manner, it would only be triggered once unless they themselves committed twice. Prettier doesn't automatically retry commits. Everyone accepts that two rockets will fire if they click "launch" twice.

Just think about this: if committing changes to CHANGELOG.md fails the 1st time, why would someone ever want to run cz bump --changelog --retry=0? It's gonna fail anyway, and you already have the (2nd) failure logs. 🤷🏼‍♂️

OTOH, if committing changes to CHANGELOG.md fails the 2nd time, why would you ever want to run cz bump --changelog --retry=0? You already have the logs of the 2nd failure, those will tell you the problem; besides, you also have the dirty changes in your repo. Why would you need more info?

The point is: if your use case was working before this patch, it will continue working (1st commit attempt passes OK). And if it was failing, it could get fixed (if it was a reformat), or it could continue failing (if it was a real failure). So there's nothing we lose with this fix (unless you send rockets with commits, of course).

I am not following this, but the simplest reason someone would want to retry 0 times is that they have no reason to expect that retrying would ever be constructive (e.g., they aren't running auto-formatters as pre-commit hooks that may potentially clash with Commitizen). They may not want to spend time sorting through unexpected duplicate logs and/or time + money (e.g., build credits) re-running all of their other pre-commit hooks to no avail. They may not want an ephemeral error to be swallowed without even knowing what happened or that anything even had happened. And again, this retry logic can mask a bug in another hook that sometimes fails on the first attempt, but never on the second. On the other hand, they may not have any other pre-commit hooks, in which case retrying seems quite bizarre. There are a huge number of scenarios in computing where something can fail and be retried, and it's (a) quite rare for that retry behavior not to be configurable by the developer in polished APIs, and (b) not retrying is by-and-large the default, because it often results in more robust systems overall.

@yajo yajo force-pushed the pre-commit-bump branch from 968713c to 8f4cf27 Compare May 3, 2022 16:00
@yajo
Copy link
Contributor Author

yajo commented May 3, 2022

Just try to think in one practical example where having this fix is worse than not having it. If you can't find any, it means that will just never happen. 🤷🏼‍♂️

Can't we just merge and fix that theoretical bug once your theories really happen somewhere? 🙄

@Kurt-von-Laven
Copy link
Contributor

In the most common case, retrying is not productive and may even be detrimental.

@yajo
Copy link
Contributor Author

yajo commented May 4, 2022 via email

Fix commitizen-tools#502.

Just add `--retry` to `cz bump` and it will attempt to commit twice if the 1st commit fails. Useful if your 1st commit runs a formatter that can for example change the contents of CHANGELOG.md.
@yajo yajo force-pushed the pre-commit-bump branch from 8f4cf27 to 6ab2204 Compare May 4, 2022 15:40
@yajo
Copy link
Contributor Author

yajo commented May 4, 2022

Option added. Now, if you don't run it with cz bump --retry, the old behavior is kept.

@Kurt-von-Laven
Copy link
Contributor

Thank you!

@yajo
Copy link
Contributor Author

yajo commented May 10, 2022

So is there anything else left to merge?

@woile woile requested a review from Lee-W May 10, 2022 11:31
@woile
Copy link
Member

woile commented May 10, 2022

The feature seems to cover specific situation: Maybe pre-commit reformatted some files? Retry once.
It's important to add documentation about the use cases, so we know later why it's being introduced.

To avoid confusions, it would be nice to document the feature 👍🏻 thanks!

Includes an update to the `cz bump --help` output.
@yajo
Copy link
Contributor Author

yajo commented May 10, 2022

Documented.

Copy link
Member

@Lee-W Lee-W left a comment

Choose a reason for hiding this comment

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

Overall, everything looks good to me! Thanks everyone's hard work on this. I love this "--retry" flag. Just left some questions. I think we're almost ready to merge this one.

Copy link
Member

@Lee-W Lee-W left a comment

Choose a reason for hiding this comment

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

LGTM. Thanks @yajo for your contribution! @woile I'd like to merge this one if no further comment is added these days

@Lee-W Lee-W merged commit 44f5b3e into commitizen-tools:master May 16, 2022
@yajo yajo deleted the pre-commit-bump branch May 16, 2022 13:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

cz bump fails if pre-commit reformats CHANGELOG.md
4 participants