From 602cfb6ac0fc464624c38cb89f1bb4d705c08132 Mon Sep 17 00:00:00 2001 From: tiosgz Date: Mon, 6 Dec 2021 21:51:21 +0000 Subject: [PATCH] Contributing: add a note about code formatting Wanted to share that pre-commit hook :) --- CONTRIBUTING.md | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index e15244c..63e6b12 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -11,3 +11,20 @@ Contributing entails that all contributions follow the [MPL 2.0 license](https:/ Commit messages should be both _clear_ and _descriptive_. If possible, commit titles should start with a verb describing the change. Don't be shy to include additional information such as motivation for the change in the commit body. Be sure to ensure other developers will be able to understand _why_ a specific change has occurred in the future. +## Code formatting + +To ensure consistent formatting, use `clang-format -i **/*.[ch]` (beware, not all shells support that glob). Don't worry too much about forgetting to do so, the pipeline will remind you by failing on your commit/PR. You can also make this your pre-commit git hook (see `man 5 githooks`) to avoid committing any misformatted code: + +```sh +#!/bin/sh + +# Check if there're any formatting errors +find . '(' -name .git -o -path ./build ')' -prune \ + -o -type f -name '*.[ch]' -print | + xargs -d '\n' clang-format --dry-run >/dev/null 2>&1 + +if [ $? -ne 0 ]; then + echo 'Code formatting is wrong, fix it before commit' >/dev/stderr + exit 1 +fi +```