-
-
Notifications
You must be signed in to change notification settings - Fork 952
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
Update pre-commit hook #1511
Update pre-commit hook #1511
Conversation
fbb8283
to
7cfe2b9
Compare
Detecting older and incompatible clang-format would be kinda nice. |
I could add a simple check that makes sure the version is at least 14.0.0. I could also add some slightly more complicated code that goes through all |
I've just implemented the more complicated option. It isn't very elegant sadly, if someone can come up with a better way to do it, I would appreciate it a lot. It should work in basically all situations though (except if the user has a space in their PATH, which they really shouldn't have and will probably break other things as well). |
842c26d
to
8d2477c
Compare
8d2477c
to
fcdf486
Compare
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 should probably follow the same conventions regarding variable naming.
fcdf486
to
3457040
Compare
Build size and comparison to main:
|
3457040
to
7c2fabb
Compare
8752c63
to
a3ec514
Compare
I've switched to a more robust method of getting the files that have changed. This doesn't rely on using human-readable output that could potentially be changed in an update. I have a git stash with a bit more logic that more conclusively finds the highest clang-format version, by using |
I'd be interested to see your new clang-format finding code regardless, if you could post a patch. |
diff --git a/hooks/pre-commit b/hooks/pre-commit
index e03b4217..60a01e34 100755
--- a/hooks/pre-commit
+++ b/hooks/pre-commit
@@ -1,23 +1,29 @@
#!/bin/sh
+name="clang-format"
+
+if [ -z "$(command -v "git-$name")" ]; then
+ name="$(basename -a $(find $(echo "$PATH" | tr ':' ' ') -maxdepth 1 -type f -executable -name 'git-clang-format*') | sort | tail -n 1 | sed 's/^git-//')"
+fi
+
minVersion="14.0.0"
-for file in $(find $(echo "$PATH" | tr ':' ' ') -maxdepth 1 -type f -executable -name 'git-clang-format*'); do
- curName="$(basename "$file" | sed 's/^git-//')"
- curVersion="$("$curName" --version | cut -d ' ' -f 3)"
+for file in $(find $(echo "$PATH" | tr ':' ' ') -maxdepth 1 -type f -executable -name 'clang-format*'); do
+ curBin="$file"
+ curVersion="$("$curBin" --version | cut -d ' ' -f 3)"
if [ "$(printf '%s\n' "$curVersion" "$version" "$minVersion" | sort -V | tail -n 1)" = "$curVersion" ]; then
- name="$curName"
+ bin="$curBin"
version="$curVersion"
fi
done
-if [ -z "$name" ]; then
+if [ -z "$name" ] || [ -z "$bin" ]; then
echo "Could not find a suitable clang-format installation. Install clang-format that includes the git-clang-format script, with at least version $minVersion"
exit 1
fi
-args='-q --extensions cpp,h --style file --staged -- :!src/FreeRTOS :!src/libs'
+args="--binary $bin -q --extensions cpp,h --style file --staged -- :!src/FreeRTOS :!src/libs"
changedFiles="$(git "$name" --diffstat $args)"
git "$name" $args Here is a patch. The code for finding the clang-format binary is mostly the same, but the script now stores both the git-clang-format script name and the actual clang-format binary. |
a3ec514
to
5b3db43
Compare
5b3db43
to
77af275
Compare
This updates the pre-commit hook to use git-clang-format.