-
Notifications
You must be signed in to change notification settings - Fork 71
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
Automatically add a pre-commit hook to git repos for running clang-fo… #537
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
# Copyright (c) 2017, Juan Hernando <juan.hernando@epfl.ch> | ||
# | ||
|
||
if(NOT GIT_FOUND) | ||
find_package(Git QUIET) | ||
endif() | ||
|
||
if(NOT CLANG_FORMAT) | ||
find_program(CLANG_FORMAT clang-format) | ||
endif() | ||
|
||
# Installing clang-format precommit hook if prerequisites are met and it doesn't | ||
# exist yet. | ||
if(GIT_FOUND AND CLANG_FORMAT AND EXISTS ${PROJECT_SOURCE_DIR}/.git AND | ||
NOT EXISTS ${PROJECT_SOURCE_DIR}/.git/hooks/pre-commit) | ||
|
||
# We cannot write the file from here because we need exec permissions | ||
configure_file(${CMAKE_SOURCE_DIR}/CMake/common/util/git_pre-commit.in | ||
${PROJECT_SOURCE_DIR}/.git/hooks/pre-commit) | ||
endif() | ||
|
||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
#!/bin/bash | ||
# Copyright (c) 2017, Juan Hernando <juan.hernando@epfl.ch> | ||
|
||
GIT=${GIT_EXECUTABLE} | ||
CLANG_FORMAT=${CLANG_FORMAT} | ||
|
||
# Checking if this project has a .clang-format style file at the root dir. | ||
if ! [[ -e $PWD/.clang-format ]]; then | ||
exit 0 | ||
fi | ||
|
||
files=$($GIT diff --cached --name-only --diff-filter=ACM) | ||
|
||
for file in $files; do | ||
for ext in c cpp h hpp ipp frag vert glsl ispc ih; do | ||
if [[ "$file" == *.$ext ]]; then | ||
# We run clang-format only in files that are fully staged and warn | ||
# about the others | ||
if [[ $($GIT diff --name-only "$file") == "$file" ]]; then | ||
echo "clang-format not run on partially added file $file" | ||
else | ||
$CLANG_FORMAT -i -style=file $PWD/$file | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That's what man clang-format told me to do. Has this changed in a later version? |
||
$GIT add $file | ||
fi | ||
break; | ||
fi | ||
done | ||
done | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If I understand correctly, this applies only to fully staged files? What if I do a git commit -a? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I tested just in case, clang-format is still run on all modified files. Git seems to prepare the index before calling the pre-commit hook, and if you abort the commit it undoes the additions to the index. |
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 will hardcode a path at configure time, not a good idea because this file will not be overwritten once it has been created. Just use the executable from the path?
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.
And what if your git or clang format binaries are not in the path and you detected them with CMake?