Skip to content
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

Merged
merged 1 commit into from
Jan 27, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# git master

* [537](https://github.com/Eyescale/CMake/pull/537):
Automatically add a pre-commit hook to git repos for running clang-format.
* [533](https://github.com/Eyescale/CMake/pull/533):
Add application-help-to-doxygen extraction
* [532](https://github.com/Eyescale/CMake/pull/532):
Expand Down
1 change: 1 addition & 0 deletions Common.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ include(CommonCompiler)
include(CommonCoverage)
include(GitInfo)
include(GitTargets)
include(GitHooks)
include(ProjectInfo)
include(UpdateGitExternal)

Expand Down
23 changes: 23 additions & 0 deletions GitHooks.cmake
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()



28 changes: 28 additions & 0 deletions util/git_pre-commit.in
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}
Copy link

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?

Copy link
Author

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?


# 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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

-style=file?

Copy link
Author

Choose a reason for hiding this comment

The 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
Copy link
Member

Choose a reason for hiding this comment

The 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?

Copy link
Author

Choose a reason for hiding this comment

The 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.