Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I add clang-formatting to pre-commit hook?

I am new to commit hooks as well as Clang formatting and am attempting to integrate the two. I have the pre-commit hook set up and I know how to run the Clang formatting on the command line, but am unsure of how to add it to the file.

This is the code I run in the command line for formatting: clang-format -i -style=llvm fileName

I am also trying to run this on all files that are staged for commit. git diff --cached --name-only

This is my pre-commit file:

hook_enabled=true

# Redirect output to stderr.
exec 1>&2

# If the hook is enabled and there are one or more files added to the commit run
# code formatting.
if [ "$hook_enabled" != "false" ] &&
    test $(git diff --cached --name-only $against | wc -c) != 0
then
    cat <<\EOF
  Code formatting changed some files, please review and re-add files with git add
EOF
    exit 1

I also added the clang-formatting to package.json:

    "pre-commit": "check-clang-format",
    "format": "git-clang-format",

Please help me integrate the clang-formatting.

like image 431
Martina Avatar asked May 03 '19 08:05

Martina


People also ask

Where do I put the clang format?

clang-format file, we need to place it in the project folder or in any parent folder of the file you want to format. clang-format.exe searches for the config file automatically starting with the folder where the file you want to format is located, all the way to the topmost directory.

How do you put a pre-commit on a hook?

Open a terminal window by using option + T in GitKraken Client. Once the terminal windows is open, change directory to . git/hooks . Then use the command chmod +x pre-commit to make the pre-commit file executable.

What is git-clang-format?

git-clang-format is a simple Python script distributed together with clang-format. The problem is that not so many are talking about git-clang-format. What git-clang-format solves for us is that it runs clang-format on the changes you made.


3 Answers

I'm adding the following to the top of my REPO_ROOT/.git/hooks/pre-commit file:

for FILE in $(git diff --cached --name-only)
do
        clang-format -i $FILE
done

The .clang-format file is placed in the REPO_ROOT.

The other answer and the first comment to the original question doesn't say why it is preferred to avoid this solution, so I'd be happy to hear more about that.

like image 171
Benjamin Avatar answered Sep 24 '22 18:09

Benjamin


This is now (finally) very simple using the open source https://pre-commit.com (the framework):

repos:
- repo: https://github.com/pre-commit/mirrors-clang-format
  rev: v14.0.6
  hooks:
  - id: clang-format

It grabs a 1-2 MB binary from PyPI for all common platforms (Windows 64 & 32, macOS universal, manylinux 64 & 32 & arm & ppc & s390x). You can pin clang-format 10, 11, or 12 (And now 13, 14, and various patch versions, often same day releases!). See https://github.com/ssciwr/clang-format-wheel. If you use https://pre-commit.ci, you get automatic update PRs and your PRs get automatically fixed.

like image 27
Henry Schreiner Avatar answered Sep 21 '22 18:09

Henry Schreiner


Actually, you don't invoke a clang-format binary at pre-commit hook.

Here is an instruction how to setup clang format at pre-commit hook: https://github.com/andrewseidl/githook-clang-format

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

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:

cp githook-clang-format/clang-format.hook myrepo/.git/hooks/pre-commit

Usage Once the pre-commit hook is installed, clang-format will be run on each file included in the commit when you run git commit.

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.

You can generate the .clang-format file from your desired style (here, llvm) using:

clang-format -style=llvm -dump-config > .clang-format

To use the git config method, inside your repo do:

git config hooks.clangformat.style llvm

like image 30
Anton Vlasov Avatar answered Sep 24 '22 18:09

Anton Vlasov