Skip to content

Creating a pre commit hook for clang format

Erik Nyquist edited this page Aug 2, 2016 · 5 revisions

1. Install clang-format

The best way to do this is to ask your package manager which versions of clang-format are available.

Debian/Ubuntu

From a terminal, enter the following:

apt-get update
apt-cache search clang-format

This will present you with a list of available versions. Pick one, and run

apt-get install clang-format-xxx

replacing clang-format-xxx with the full name of the version you picked.

Fedora

From a terminal, enter the following:

yum search clang-format

This will present you with a list of available versions. Pick one, and run

yum install clang-format-xxx

replacing clang-format-xxx with the full name of the version you picked.

2. Add the clang-format style file

download our style file from here, and save it to the root directory of the repository clone to which you wish to apply the hook. Then, navigate to the repository's root directory and rename the downloaded file to .clang-format.

Alternatively, do it entirely from a terminal:

cd corelibs-arduino101-fork
curl https://gist.githubusercontent.com/eriknyquist/e2f6ea988d007bdd448ac5b11edab1c6/raw > .clang-format

3. Add pre-commit hook

download our pre-commit script from here, and save it to the root directory of the repository clone to which you wish to apply the hook. Then, navigate to the repository's root directory and rename the downloaded file to .git/hooks/pre-commit.

Alternatively, do it entirely from a terminal:

cd corelibs-arduino101-fork
curl https://gist.githubusercontent.com/eriknyquist/d8d3c14406a76bd7d9516a3aaaa3423e/raw > .git/hooks/pre-commit
chmod a+x .git/hooks/pre-commit

That's it! Next time you type git commit, your changes will be automatically formatted with clang-format.