-
Notifications
You must be signed in to change notification settings - Fork 13.8k
Modify the pre-push githook to run Tidy on only committed changes. #147084
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
base: master
Are you sure you want to change the base?
Conversation
added error handling for git stash and apply fix tidy err abort if stash push fails fix tidy errors... again
rustbot has assigned @Mark-Simulacrum. Use |
if [ -n "$(git status --porcelain)" ]; then | ||
echo "Stashing local uncommitted changes before running Tidy." | ||
# Stash uncommitted changes so that tidy only checks what you are going to push. | ||
git stash push -u -q |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think that doing anything with untracked files might be a bit dangerous. I personally wouldn't want these scripts ever touching anything untracked by git.
So I think the best way to avoid stashing untracked files would probably be to have I did a little bit of digging anyways, and don't really think stashing untracked is extra dangerous though. Stash is basically all or nothing so on failure the working dir won't be affected. And once stashed it's still a commit under the hood so it's recoverable even if you explicitly drop the stash. But it is still stored locally, so there's always some risk I suppose. Just a note that Another option that could work well is throwing in a prompt asking if you want to stash changes before continuing. So if you're really worried you can back out and handle it manually if you prefer. I think it'd still have to include untracked unless you changed |
☔ The latest upstream changes (presumably #147118) made this pull request unmergeable. Please resolve the merge conflicts. |
I think there are two approaches that we could do here:
I would very much prefer the second solution, if we figure out that it's doable. |
So I looked into this a bit and option 2 seems doable. There's already a I think setting tidy to ignore untracked files as the default and adding an optional flag to include untracked is an option and a bit more user-friendly but adds some complexity. It's plausible people want to check untracked files and having to I think keeping the stash in the bash script makes sense and just remove the |
I agree that ignoring untracked files by default is a good idea, I often run into tidy errors because I have a bunch of crap laying on my disk in the source root 😆 When we do that, we might as well ignore uncommitted files in the git index, but this only makes sense in the pre-push hook. Still unsure about the stashing though, I think that some people (myself included) might be unhappy about the hook messing with their git state. I asked about this on our Zulip. |
I think this is a fairly simple solution for #125654. The basic idea is just to stash local changes, run tidy, and then pop the changes back after Tidy runs for the pre-push git hook. It sort of got a bit more complicated with the error handling, but still seems like a reasonable solution.
A few things to note:
I opted to fail the whole check if
git stash push -u
fails instead of continuing to run Tidy on the unstashed changes. My thinking isgit stash
doesn't really fail easily so if it does something is probably pretty wrong and this way there's no inconsistency on what is being checked.In general I think it's unlikely for
git stash apply
to fail because Tidy does not actually change files during its checks so there shouldn't be any risk of merge conflicts. If it does fail, it's a bit of a pain for the user, but handled and nothing is lost.