All Projects → barisione → Clang Format Hooks

barisione / Clang Format Hooks

Licence: mit
Apply a coding style with clang-format only to new code added to an existing code base.

Programming Languages

python
139335 projects - #7 most used programming language
c
50402 projects - #5 most used programming language
cpp
1120 projects

Projects that are alternatives of or similar to Clang Format Hooks

Postcss Less
PostCSS Syntax for parsing LESS
Stars: ✭ 93 (-23.77%)
Mutual labels:  syntax
Breaking Gfw Book
整理所有的翻墙方法,持续更新....欢迎PR
Stars: ✭ 105 (-13.93%)
Mutual labels:  gitbook
Istio Handbook
Istio Service Mesh Advanced Practical - Istio服务网格进阶实战 https://www.servicemesher.com/istio-handbook/
Stars: ✭ 1,646 (+1249.18%)
Mutual labels:  gitbook
Violations Lib
Java library for parsing report files from static code analysis.
Stars: ✭ 94 (-22.95%)
Mutual labels:  clang
Cppinsights
C++ Insights - See your source code with the eyes of a compiler
Stars: ✭ 1,382 (+1032.79%)
Mutual labels:  clang
Moderncppci
This is an example of doing a Modern C++ project with CI
Stars: ✭ 109 (-10.66%)
Mutual labels:  clang
Docker Gitbook Pdf Generator
如果你想使用 GitBook 生成 PDF ,又不想折腾,使用这个项目就对啦!
Stars: ✭ 92 (-24.59%)
Mutual labels:  gitbook
Activiti 7 Developers Guide
https://activiti.gitbook.io/activiti-7-developers-guide/
Stars: ✭ 120 (-1.64%)
Mutual labels:  gitbook
Cmakepchcompiler
CMake precompiled header support via custom PCH compiler extension
Stars: ✭ 105 (-13.93%)
Mutual labels:  clang
Hmap
hmap is a command line tool written in Swift to work with Clang header maps produced by Xcode.
Stars: ✭ 110 (-9.84%)
Mutual labels:  clang
Termux Ndk
android-ndk for termux
Stars: ✭ 91 (-25.41%)
Mutual labels:  clang
Tre
LLVM backed progamming language (Go subset)
Stars: ✭ 100 (-18.03%)
Mutual labels:  clang
Nord Sublime Text
An arctic, north-bluish clean and elegant Sublime Text theme.
Stars: ✭ 109 (-10.66%)
Mutual labels:  syntax
Http restful api
整理HTTP后台端的RESTful API方面的知识
Stars: ✭ 94 (-22.95%)
Mutual labels:  gitbook
Microservices Architecture
《微服务架构设计》
Stars: ✭ 113 (-7.38%)
Mutual labels:  gitbook
Bazel Toolchain
LLVM toolchain for bazel
Stars: ✭ 93 (-23.77%)
Mutual labels:  clang
Container Monitor
容器监控方案汇总
Stars: ✭ 107 (-12.3%)
Mutual labels:  gitbook
Bsodsurvivor
This project aims to facilitate debugging a kernel driver in windows by adding support for a code change on the fly without reboot/unload, and more!
Stars: ✭ 122 (+0%)
Mutual labels:  clang
Vector
➿ A supercharged std::vector implementation (minus Allocator)
Stars: ✭ 118 (-3.28%)
Mutual labels:  clang
Pwning Juice Shop
GitBook markdown content for the eBook "Pwning OWASP Juice Shop"
Stars: ✭ 110 (-9.84%)
Mutual labels:  gitbook

Build status

clang-format git hooks

Apply a coding style with clang-format only to new code added to an existing code base.

At Undo, we decided to use the clang-format tool to enforce a consistent coding style for new and refactored code, but without changing the existing one.

This is done through a script (apply-format) which can be run manually or through a pre-commit git hook which is run every time just before code is committed.

What is clang-format?

clang-format is a tool, part of the LLVM project, which can reformat code to adhere to a configured style.

By default, clang-format supports a few pre-defined styles matching the style used by a few important projects, but it's possible to customise specific styles.

Setup

Dependencies

  • Ubuntu and Debian: install the clang-format package. You can install a newer version with these instructions.
  • Fedora, CentOS and Red Hat: install the clang package.
  • macOS: install clang-format using HomeBrew: brew install clang-format.

You can optionally install colordiff to get nicer output.

Configuring clang-format

clang-format needs to be configured to reformat code according to your project's preferred style.
All the options are described in the official style options documentation, but a great way to start is to use an interactive configurator.

Once you found the correct style options, just save them in a file called .clang-format in the top level directory of your project. From here, the scripts, will load the style automatically.

Scripts

Add the apply-format and git-pre-commit-format scripts to your repositories.

You can either copy them (maybe in a scripts/ sub-directory) or add this whole repository as a git submodule.

Registering the git hook

Run at a terminal:

$ ./git-pre-commit-format install

This will add a hook (in .git/hooks/) which is run every time you commit.

Using the pre-commit hook

After registering the git hook, you don't need to do anything except for committing your changes.

Every time you commit, the hook will check if your code matches the coding standard. If it doesn't, you get asked what to do and you can decide to:

  • Apply the fixes automatically (only to the code you are actually committing, not to unstaged code).
  • Commit anyway.
  • Abort the commit so you can fix the problem manually.

Note that, if you use git through a GUI or some other tool (i.e. not directly on the command line), the script will fail to get your input. In this case disable the interactive behaviour with:

$ git config hooks.clangFormatDiffInteractive false

For more information on the script use the --help option.

Manual script

You can also reformat changed code without committing it using the apply-format script directly.

By default, apply-format reformats only the code which was modified, i.e. the code that git diff would show, and prints the diff to the terminal.

If you want to automatically apply the suggested changes, run the script with -i.

If you only want to reformat your staged changes, but not the unstanged ones, use --staged.

For instance, a possible workflow would be similar to:

$ # Make some changes.
$ vi foo.c
$ # Stage changes you want to commit.
$ git add -p
$ # Reformat the staged changes you made.
$ ./scripts/apply-format -i --staged
$ # Commit the result.
$ git commit

It’s also possible to reformat a whole file (using --whole-file or its shorter form -f).

For more information on the script use the --help option.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].