pbek / QOwnNotes

QOwnNotes is a plain-text file notepad and todo-list manager with Markdown support and Nextcloud / ownCloud integration.
https://www.qownnotes.org/
GNU General Public License v2.0
4.6k stars 409 forks source link

Add a clang-format action to enforce code formatting #2431

Open Waqar144 opened 2 years ago

Waqar144 commented 2 years ago

Code formatting is getting messier and inconsistent with the passage of time. It would be a good idea to enforce it via some action. Maybe this one? https://github.com/jidicula/clang-format-action

Also, not sure if this is possible, but having a pre-commit hook that is available whenever you clone the repo for formatting would be nice as well. The hook runs whenever you are going to commit and warns you if the formatting is bad. Following is the precommit hook being used in KDE repos:

.git/hooks/pre-commit

#!/usr/bin/env bash

./.git/hooks/scripts/clang-format.sh

.git/hooks/scripts/clang-format.sh

#!/usr/bin/env bash

# Based on okular/hooks/pre-commit, credits go to Albert Astals Cid

readonly output=$(git clang-format -v --diff)

if [[ ! -f .clang-format ]]; then
    echo "ERROR: no .clang-format file found in repository root, abort format"
    echo "       run cmake for this repository to generate it"
    exit 1
fi
if [[ "$output" == *"no modified files to format"* ]]; then exit 0; fi
if [[ "$output" == *"clang-format did not modify any files"* ]]; then exit 0; fi

echo "ERROR: You have unformatted changes, please format your files. You can do this using the following commands:"
echo "       git clang-format --force # format the changed parts"
echo "       git clang-format --diff # preview the changes done by the formatter"
exit 1
pbek commented 2 years ago

So someone needs to run the clang-format for the whole project first?

Waqar144 commented 2 years ago

Once such a check and action is in place, then yes, it would make sense to format everything once and then maintain it.

pbek commented 2 years ago

Files .git are not in the version control system, aren't they?

pbek commented 2 years ago

I created the files to do a git clang-format --diff, but that only says: git: 'clang-format' is not a git command. See 'git --help'.

Waqar144 commented 2 years ago

Files .git are not in the version control system, aren't they?

They aren't, but somehow they are in KDE repos. Maybe their manually inserted by a script or something?

Do you have clang-format installed? also check if you have which git-clang-format

pbek commented 2 years ago

They aren't, but somehow they are in KDE repos. Maybe their manually inserted by a script or something?

maybe you need a git command to create the hook

Do you have clang-format installed? also check if you have which git-clang-format

no, and I can't find a package for it, I only have clang-format installed

Waqar144 commented 2 years ago

Weird. I thought it was automatically installed when you install clang-format. It's basically a wrapper script over clang-format

https://github.com/llvm/llvm-project/blob/main/clang/tools/clang-format/git-clang-format