├── clang-format.hook └── README.md /clang-format.hook: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | STYLE=$(git config --get hooks.clangformat.style) 4 | if [ -n "${STYLE}" ] ; then 5 | STYLEARG="-style=${STYLE}" 6 | else 7 | STYLEARG="" 8 | fi 9 | 10 | format_file() { 11 | file="${1}" 12 | if [ -f $file ]; then 13 | clang-format -i ${STYLEARG} ${1} 14 | git add ${1} 15 | fi 16 | } 17 | 18 | case "${1}" in 19 | --about ) 20 | echo "Runs clang-format on source files" 21 | ;; 22 | * ) 23 | for file in `git diff-index --cached --name-only HEAD | grep -iE '\.(cpp|cc|h|hpp)$' ` ; do 24 | format_file "${file}" 25 | done 26 | ;; 27 | esac 28 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | githook-clang-format 2 | ==================== 3 | 4 | Don't use this. Instead, look into integrating clang-format into your editor. 5 | - Emacs: http://clang.llvm.org/docs/ClangFormat.html 6 | - SublimeText: https://github.com/rosshemsley/SublimeClangFormat 7 | - Vim: https://github.com/rhysd/vim-clang-format 8 | - Visual Studio: http://llvm.org/builds/, or use the [integrated support in Visual Studio 2017](https://blogs.msdn.microsoft.com/vcblog/2018/03/13/clangformat-support-in-visual-studio-2017-15-7-preview-1/) 9 | - Xcode: https://github.com/travisjeffery/ClangFormat-Xcode 10 | 11 | Alternatively look into a pre-commit framework such as [pre-commit](https://pre-commit.com/) which makes it easier to manage these hooks. An example hook for clang-format is at [clang-format-precommit](https://github.com/ssciwr/clang-format-precommit). 12 | 13 | This script will automatically run [`clang-format`](http://clang.llvm.org/docs/ClangFormat.html) on all changed files in a commit when set as a pre-commit hook. Proceed with caution. 14 | 15 | ## Warning 16 | Do not use this on an existing codebase that isn't already in your desired style. Doing so will lead to a string a dirty commits where your code changes are intermixed with `clang-format`'s formatting changes. 17 | 18 | Furthermore, every developer will need to install this hook. If they don't, you will again end up with commits with a mixture of code and formatting changes. 19 | 20 | ## Installation 21 | First, verify that `clang-format` is installed. On Linux this should be included with the regular `clang` package. For MacOSX with Homebrew, `clang-format` is available via `brew install clang-format`. 22 | 23 | Now install `clang-format.hook` from this repository into your repo's `.git/hooks`. If you don't already have a pre-commit hook, you can simply copy `clang-format.hook` to `.git/hooks/pre-commit`. For example: 24 | 25 | `cp githook-clang-format/clang-format.hook myrepo/.git/hooks/pre-commit` 26 | 27 | ## Usage 28 | Once the pre-commit hook is installed, `clang-format` will be run on each file included in the commit when you run `git commit`. 29 | 30 | By default, `clang-format` uses the LLVM style. To change this, either create a `.clang-format` file with your desired format in the top level of your repo, or set the `hooks.clangformat.style` config option in your repo. The `.clang-format` file method is preferred if you will be working with a team or will be doing any major customizations to the style. 31 | 32 | You can generate the `.clang-format` file from your desired style (here, llvm) using: 33 | 34 | `clang-format -style=llvm -dump-config > .clang-format` 35 | 36 | To use the `git config` method, inside your repo do: 37 | 38 | `git config hooks.clangformat.style llvm` 39 | --------------------------------------------------------------------------------