Improve clang-format git hook support
Categories
(Developer Infrastructure :: Lint and Formatting, enhancement, P2)
Tracking
(firefox68 fixed)
Tracking | Status | |
---|---|---|
firefox68 | --- | fixed |
People
(Reporter: Sylvestre, Assigned: Sylvestre)
References
Details
Attachments
(2 files, 2 obsolete files)
Assignee | ||
Comment 1•6 years ago
|
||
Comment 3•6 years ago
|
||
bugherder |
Comment 4•6 years ago
|
||
Assignee | ||
Comment 5•6 years ago
|
||
Comment 6•6 years ago
|
||
Assignee | ||
Comment 7•6 years ago
|
||
Comment 8•6 years ago
|
||
Assignee | ||
Comment 9•6 years ago
|
||
Comment 10•6 years ago
|
||
Comment 11•6 years ago
|
||
Comment 12•6 years ago
|
||
Comment 13•6 years ago
|
||
Updated•6 years ago
|
Comment 14•6 years ago
|
||
Assignee | ||
Comment 15•6 years ago
|
||
Comment 16•6 years ago
|
||
Comment 17•6 years ago
|
||
Assignee | ||
Comment 18•6 years ago
|
||
Updated•6 years ago
|
Assignee | ||
Comment 19•6 years ago
|
||
Comment 20•6 years ago
|
||
Assignee | ||
Comment 21•6 years ago
|
||
Assignee | ||
Updated•6 years ago
|
Comment 22•6 years ago
|
||
Comment 23•6 years ago
|
||
Comment 24•6 years ago
|
||
Comment 25•6 years ago
|
||
Comment 26•6 years ago
|
||
Assignee | ||
Comment 27•6 years ago
|
||
Comment 28•6 years ago
|
||
Comment 29•6 years ago
|
||
Updated•6 years ago
|
Comment 30•6 years ago
|
||
How can we make progress here? I've kind of lost track of what the latest proposal and status is... :-)
Assignee | ||
Comment 31•6 years ago
|
||
I am happy with this change:
https://phabricator.services.mozilla.com/D15521
except:
- add the support of "git add -p "
- and improve the hg hook
I will try to finish it this week
Comment 32•6 years ago
|
||
For git add -p
something like the following should work:
git stash --keep-index && $(git rev-parse --show-toplevel)/mach clang-format && git add -u && git stash pop
Or something of that sort.
I think the only case it wouldn't handle is the case where there's nothing to save (both the commit and the staging area is empty), which you could do with git commit --allow-empty
, because in that case the stash will actually not save anything and the pop
will pop whatever was on the stack beforehand. You can detect that easily before-hand though.
Not sure if there's a better way to do it.
Comment 33•6 years ago
|
||
Ah! A much faster way could be:
git diff --staged | clang-format-diff.py -p 1 | git apply -p 0 --cached
Comment 34•6 years ago
|
||
(You may want to apply the patch both with and without --cached, so that it gets reflected both in the index and the working tree)
Assignee | ||
Comment 35•6 years ago
|
||
Thanks for the suggestion
clang-format-diff.py isn't perfect but this is better than nothing
Assignee | ||
Comment 36•6 years ago
|
||
And also, on the hg hook, the issue is that it isn't trivial to retrieve the list of touched files in the commit hook.
pretxncommit
would be perfect as it provides the node id (which is necessary to retrieve the list of files) except that it has no info about the files being added.
For example, using pretxnopen
and pretxnclose
don't provide the node id.
Sheeshan suggested that I use $HG_NODE
but it doesn't exist where I am.
I could use the hg python interface to retrieve these data but it seems pretty overkill.
Assignee | ||
Comment 37•6 years ago
|
||
Sheehan told me the following:
so you would use
pretxncommit
as the hook phase, addnode=None
to your function signature, and then do the steps a mentioned
above to get the list of changed files
Assignee | ||
Updated•6 years ago
|
Assignee | ||
Comment 38•6 years ago
|
||
I took a different approach for hg:
https://phabricator.services.mozilla.com/D18883
and maybe move the git hook in bash to make it easier
Assignee | ||
Comment 39•6 years ago
|
||
Depends on D23789
Assignee | ||
Comment 40•6 years ago
|
||
Here is that I used to test the work:
git checkout dom/presentation/
git reset dom/presentation/AvailabilityCollection.cpp
sed -i -e "s|;$| ;|g" dom/presentation/AvailabilityCollection.cpp
echo "int foo( ){}" >> dom/presentation/AvailabilityCollection.cpp
# No action as no file added to git
git commit -m "foo - should not do anything"
# Should have the execution
git add dom/presentation/
git commit -m "foo - should commit the file with the change"
git show
sed -i -e "s|#include |#include |g" dom/presentation/AvailabilityCollection.cpp
echo "int foo( ){}" >> dom/presentation/AvailabilityCollection.cpp
git diff dom/presentation/
git add -p dom/presentation/
git commit -m "foo - will commit everything when it should not"
git show
sed -i -e "s|#include |#include |g" dom/presentation/AvailabilityCollection.cpp
echo "int bar( ){}" >> dom/presentation/AvailabilityCollection.cpp
# Nothing should happen (just renaming)
git commit --amend
# update the title + reformat it
git commit --amend -m "updated" dom/presentation/AvailabilityCollection.cpp
Assignee | ||
Comment 41•6 years ago
|
||
I would like to land that first, and then work on "git add -p" support
Assignee | ||
Updated•6 years ago
|
Comment 42•6 years ago
|
||
Comment 43•6 years ago
|
||
bugherder |
Assignee | ||
Comment 44•6 years ago
|
||
git diff --staged | clang-format-diff.py -p 1 | git apply -p 0 --cached
To explain why I am not using this:
- because clang-format-diff.py doesn't always have the whole context, clang-format-diff.py can provide different results (we had an issue with that)
- "mach clang-format" has more features (third party ignore, parallel execution, etc)
Updated•6 years ago
|
Updated•2 years ago
|
Description
•