-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
Adding git pre commit hook for formatting staged changes #10219
Conversation
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 have no objections to adding this tool if the language team supports it (and it appears they do). However, I feel like there should be a better way to enable it. Could it be enabled in package.json
, which I believe is listed as the recommended way here?
https://www.npmjs.com/package/precise-commits#2-precommit-hook
@mikeharder Thanks for your feedback. However, I am trying to avoid spamming all |
I don't think adding something to every Other than needing to change every We should use the option we think gives the best overall developer experience, ignoring whether we need to modify every |
If we decide there is no value in adding this command to every |
@mikeharder Using
I do not think so. In fact, the hook will not commit the formatting changes and will leave it up to you to decide if you want to commit them. With this workflow, I can not think of a scenario where you would want to run the formatter manually. Ideally,
Duplication generally just does not sit right with me but this is a good and important point. |
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.
Adding this feature via the rush mechanisms is simpler than modifying every package.json
, so I am fine merging this PR as-is, and we can convert to the package.json
method in the future if we decide it's better.
echo Invoking $COMMAND | ||
$COMMAND | ||
else | ||
echo Command not installed: $COMMAND |
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.
Just wondering - should we actually exit with a non zero status here to block the commit or are we okay with allowing the commit to happen even if we were unable to run the prettier on it?
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 do not see why we might want to block the commit, do you have a reason in mind? Ideally, this case should never happen if rush
is doing its job. However if for whatever reason rush
state was corrupted and the binaries are missing, I doubt that aborting the commit will accomplish anything for the developer experience.
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 guess it's about how strong we are trying to push the enforcement of this behavior. Blocking the commit would make it harder to end up checking in code in cases where your environment is horked, but maybe we think that's a rare enough occurrence and that even if the code gets checked in without having been run through prettier it's not the end of the world.
Happy with whatever call you make, was just trying to get a sense for how strict we were trying to be here.
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.
Yeah, sorry I did not make a good write-up about my approach earlier. I wrote the assumptions I work with in a comment below.
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.
Looks great, thanks for adding this @deyaaeldeen!
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.
This is a great idea and there is prior art for doing this in the Rush Stack repo itself.
I think it's a little awkward that the hook fixes the files (but doesn't stage the fixes) and then allows the commit to go ahead anyway. Maybe we could either silently fix the formatting errors and stage the fixes before the commit, or do check only and reject the commit if the check fails.
|
||
if [ -f $COMMAND ]; then | ||
echo Invoking $COMMAND | ||
$COMMAND |
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 we should consider capturing the exit status here and passing it through at the end.
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.
This could cause the commit to fail, which conflicts with assumption 4 in my comment below. I am happy to change this if we get a consensus on another approach with another set of assumptions.
After today's offline discussion, we will wait to get acknowledgement from our partners. @ramya-rao-a would you please ask them to put a review/acknowledgement? For the sake of completeness, I will outline below the assumptions I made and how this hook affects our workflow. Assumptions: Description: Of course, not everyone will like the assumptions I made and another hook behavior might suit them better. So to get an idea about the consensus, please upvote this comment if this works for you. If not, please post another comment with your assumptions and a description of the behavior you like more. |
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.
That's a great step! Please make sure win/linux/mac can work with this. Looks good.
3bf9d7f
to
bb9b142
Compare
I verified that it works on linux (including wsl) and in git bash on windows. |
|
This hook will run on every commit to run
prettier
on the staged changes only (not whole files). The prettier configuration file used is https://github.com/Azure/azure-sdk-for-js/blob/master/sdk/.prettierrc.json. More details about the hook can be found here.To install the hook, run
rush update
.