Skip to content

Coding Conventions

Aranud edited this page Aug 30, 2023 · 6 revisions

End of lines

Unix style EOL is used across the whole project. Refer to How to commit Your Code to avoid mixing Windows and Unix style EOL.

Uncrustify

To maintain a consistent coding style and prevent ugly commit diffs each developer should use uncrustify which reformats the code to a standard defined in uncrustify.cfg.

There are slight changes to how uncrustify formats code in between uncrustify software versions, so we all have to use the same version of uncrustify to achieve a consistent source code format. Current used uncrustify version is 0.77.1, released on 2022-05-14.

We reformat only our own code, so any 3rd party code is excluded.

single file

uncrustify --no-backup -c uncrustify.cfg src/folder/file.c

whole source

On Windows:

easybuild.bat crust

On Mac or Linux:

easybuild.sh crust

git pre-commit hook

Git can execute scripts before doing the actual commit. This pre-commit script can be saved as .git/hooks/pre-commit and will check all modified files if they can be uncrustified.

If there are crustified files, it will issue a warning and abort the commit.

#!/bin/bash
#
# Save this file as .git/hooks/pre-commit
#

# Redirect output to stderr.
exec 1>&2

RET=0

changed_files=$(git diff --cached --name-only)
crustified_files=""

for file in $changed_files;
do
    # compare uncrustified file with original
    uncrustify -q -c uncrustify.cfg -f $file | cmp --quiet $file -
    if [[ $? = 1 ]];
    then
	crustified_files+=" $file"
	RET=1
    fi
done

if [[ $RET = 1 ]];
then
    echo "Commit aborted because code is crustified."
    echo "Please run  'uncrustify --no-backup -c uncrustify.cfg $crustified_files'  before commiting."
    echo "To ignore this check run 'git commit --no-verify'."
fi

exit $RET