Join GitHub today
GitHub is home to over 31 million developers working together to host and review code, manage projects, and build software together.
Sign upSet up git pre-commit hook and format the code automatically #1108
Conversation
This comment has been minimized.
This comment has been minimized.
@kt3k it needs to work on Windows too, right? Currently it doesn't. |
kt3k
force-pushed the
kt3k:feature/pre-commit-hook
branch
from
2fe0945
to
3e0e1cd
Oct 27, 2018
piscisaureus
reviewed
Oct 27, 2018
[ -z "$staged" ] && exit 0 | ||
echo Formatting files | ||
./tools/format.py | ||
echo "$staged" | xargs git add |
This comment has been minimized.
This comment has been minimized.
piscisaureus
Oct 27, 2018
Collaborator
I don't want it to call "git add", because I very often don't commit all changes.
For this to be acceptable you need to run the contents from the index through the formatter, not the work tree file.
This comment has been minimized.
This comment has been minimized.
kt3k
Oct 27, 2018
Author
Contributor
This script formats all files, but it only adds staged files (because $staged
is created from git diff --cached ...
which only includes staged files.)
This comment has been minimized.
This comment has been minimized.
kt3k
Oct 27, 2018
Author
Contributor
I checked that this doesn't include unstaged changes (at least on my mac).
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
kt3k
Oct 28, 2018
Author
Contributor
I didn't think of that case enough... I'll try to think about the solution.
BTW lint-staged
authors seem trying to solve a similar problem. ref. okonet/lint-staged#75
We might use the above tool when they shipped the above change?
This comment has been minimized.
This comment has been minimized.
I've been trying to build on windows, but I'm seeing |
This comment has been minimized.
This comment has been minimized.
@kt3k |
This comment has been minimized.
This comment has been minimized.
@piscisaureus |
This comment has been minimized.
This comment has been minimized.
@kitsonk |
This comment has been minimized.
This comment has been minimized.
I wonder if solution is to refactor lint.py to exit with status 1 if it made changes. And if lint makes changes in git commit hook then reject commit with a message (rather than git add). |
This comment has been minimized.
This comment has been minimized.
Thanks for the suggestions and reviews. I understand rejecting commit is safer than fixing automatically, but I'm afraid it could be very stressful for contributors because of the loss of commit messages. Now I think this topic is too premature. I'm closing for now. Sorry for bothering.. |
kt3k commentedOct 27, 2018
Related to #363 (but not a direct solution)
This PR sets up
.git/hooks/pre-commit
from//tools/setup.py
and it runs//tools/format.py
atgit commit
and adds back the change to the staging of commit. With this change, committed files should be always formatted and it prevents unformatted codes to appear in future pull requests.