Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Git hooks to checking of code formatting #1482

Merged
merged 6 commits into from
Feb 2, 2023
Merged

Git hooks to checking of code formatting #1482

merged 6 commits into from
Feb 2, 2023

Conversation

xDimon
Copy link
Member

@xDimon xDimon commented Jan 31, 2023

Referenced issues

No special

Description of the Change

Added external git-hooks and shell-script for their activation
pre-commit git-hook with checkings of code formatting (C++ and Cmake files)
Recommended clang-format version promoted to version 15
Activated 'InsertBraces' clang-format rule

Benefits

Uniformly formatted code without unintentionally missed non-formatted ones

Signed-off-by: Dmitriy Khaustov aka xDimon <khaustov.dm@gmail.com>
Signed-off-by: Dmitriy Khaustov aka xDimon <khaustov.dm@gmail.com>
Signed-off-by: Dmitriy Khaustov aka xDimon <khaustov.dm@gmail.com>
Signed-off-by: Dmitriy Khaustov aka xDimon <khaustov.dm@gmail.com>
Signed-off-by: Dmitriy Khaustov aka xDimon <khaustov.dm@gmail.com>
.githooks/README.md Outdated Show resolved Hide resolved
.githooks/README.md Outdated Show resolved Hide resolved
.githooks/README.md Outdated Show resolved Hide resolved
Signed-off-by: Dmitriy Khaustov aka xDimon <khaustov.dm@gmail.com>
if [ $CLANG_FORMAT_ENABLED ]; then
for FILE in $(git diff-index --name-only ${BASE} | grep -e "\\.[ch]pp$"); do
TMPFILE=$(mktemp /tmp/kagome_precommit_hook.XXXXXX)
clang-format "$FILE" >"$TMPFILE"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

May avoid temp file

expected=$(clang-format "$file")
expected_hash=$(echo "$expected" | shasum)
actual_hash=$(cat "$file" | shasum)

if [ "$actual_hash" != "$expected_hash" ]; then
  echo "format: fixing"
  tmp="$file.tmp"
  echo "$expected" > "$tmp"
  mv "$tmp" "$file"
else
  echo "format: ok"
fi

# check c++ files' format with clang-format
CXX_RES=0
if [ $CLANG_FORMAT_ENABLED ]; then
for FILE in $(git diff-index --name-only ${BASE} | grep -e "\\.[ch]pp$"); do
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Must skip deleted files

@xDimon xDimon merged commit 4e10b25 into master Feb 2, 2023
@xDimon xDimon deleted the git/hooks branch February 2, 2023 11:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants