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

Coding Style #252

Merged
merged 3 commits into from
Feb 18, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions .githooks/pre-commit
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#!/bin/bash
#
# Pre-commit hook to apply clang-format to modified C/C++ source files.
# Outputs a temporary file (/.clang-format-list) for use by a `prepare-commit-msg`
# hook to include a list of reformatted files below main commit message
declare -a FILES
# Fetch files from Git and remove those flagged as deleted. Output only the file names
CHANGED_FILES=$(git diff --cached --name-status --no-renames --word-diff=porcelain -- *.{c,h,cc,hh} | awk -F$'\t' '$1 != "D" { print $2 }')

# Modify the field separator so that output of above isn't broken by filenames with spaces
OLD_IFS=$IFS
IFS=$'\t'
for file in $CHANGED_FILES; do
# git diff add's quotes that cause issues when passing names to commands, so here we remove them
FILES+=(${file//\"/})
done
IFS=$OLD_IFS


# Run action for each file, have to do this to prevent unusual file-names from causing issues
IFS=$'\t\n'
for file in $FILES; do
clang-format --style=file -i "${file}"
git add "${file}"
echo "${file}" >> .changed
done
8 changes: 8 additions & 0 deletions .githooks/prepare-commit-msg
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/bin/bash
if [ -s .changed ]; then
GIT_COMMIT_FORMATTED_FILES=`cat .changed`
tee -a "./${1}" << EOM
The following files were formatted by clang-format:
${GIT_COMMIT_FORMATTED_FILES}
EOM
fi
1 change: 1 addition & 0 deletions debian/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
/files
/configure.cmdline
/machinekit*.install
/machinekit*.postinst
/changelog

# directory artifacts
Expand Down
14 changes: 14 additions & 0 deletions src/.clang-format
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
BreakBeforeBraces: Linux
# BraceWrapping:
# AfterCaseLabel: false
# AfterControlStatement: true
# AfterEnum: false
UseTab: Never
TabWidth: 4
IndentWidth: 4
PointerAlignment: Right
AllowAllParametersOfDeclarationOnNextLine: false
AllowShortIfStatementsOnASingleLine: WithoutElse
AllowShortCaseLabelsOnASingleLine: true
AllowShortFunctionsOnASingleLine: true
ColumnLimit: 80
1 change: 1 addition & 0 deletions src/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,4 @@
machinetalk/webtalk/zws.pb.cc
machinetalk/webtalk/zws.pb.h
machinetalk/tutorial/htdocs/js/machinetalk

6 changes: 3 additions & 3 deletions src/CodingStyle
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ Implicit tests for zero should not be used except for boolean variables.
Only loop control statements must be included in a for() construct.
e.g. sum = 0;
for (i = 0; i < 10; i++) {
sum += value[i];
sum += value[i];
}

NOT
Expand All @@ -265,8 +265,8 @@ boolean variables instead.

The form while(true) should be used for infinite loops.
e.g. while (true) {
...;
}
...;
}

NOT

Expand Down