The clang format plug-in can be installed in the Xcode IDE to format the iOS code. But someone will always forget, so plus git client pre-commit hook one for the stay, after the execution of git commit command check, check after the normal completion of a commit:
But some people are too lazy to even hook are not worthy, so plus Jenkins verification, unexpectedly don't change on the library. Hum ╭ (╯^╰), call back again ~ ~ ~ ~
For example, Git hooks take the form of a global hook (you can also copy them to the Tancang after each clone, if you have more time:)
"Use premises"
Local need to install Clang-format tools,. clangformat files are ready
the Deployment Method
1, set up the Global hook directory (please perform step 2nd):
$mkdir-p~/.git_template/hooks/
2, copy the following two lines to the end of the ~/.gitconfig file (please perform step 3rd):
$git config--global init.templatedir ~/.git_template
3, increase the Pre-commit script and add executable permissions. This script will check to see if there is no formatting, no doing will help him to change the direct submission:
#!/bin/bash
style=$ (git config--get hooks.clangformat.style)
if [-N "${style}"]; then stylearg=
"-style= ${style} "
else
stylearg=" "
fi
format_file () {
file=" ${1} "
if [" ${file##*.} "=" C "] | | ["${file##*.}" = "M"] | | ["${file##*.}" = "MM"] | | ["${file##*.}" = "H"]; Then
clang-format-i ${stylearg} ${1}
git add ${1}
fi
} case
"${1}" in
--about)
echo "runs Clang-format on source files"
;;
*) for
file in ' git diff-index--cached--diff-filter=acmr--name-only head '; does
format_file ' ${file} '
do Ne
;;
Esac
4, configure the Jenkins Gerrit Trigger, the script is as follows, did not do the format of the prohibit submit, but do not help him to change, only play negative points:
#!/bin/sh
if [ -d "checkstyle_dir" ]; then
rm -fr checkstyle_dir
mkdir checkstyle_dir
else
mkdir checkstyle_dir
fi
# get json string:
GERRIT_CHANGE_ID="$1"
ssh -p 29418 jenkins@x.x.x.x gerrit query --files --current-patch-set $GERRIT_CHANGE_ID --format=json > checkstyle_dir/string.json
# delete line 2 to the end (cat xx | wc -l)
sed -i '' '2,$d' checkstyle_dir/string.json
# read filelist by change-id:
cat checkstyle_dir/string.json | jq '.currentPatchSet | .files | map(select(.type != "DELETED")) | .[] | .file' | sed 's/"//g' > checkstyle_dir/filelist
while read ONE_LINE
do
if [[ ${ONE_LINE} = "Libraries"* ]] || [[ ${ONE_LINE} = *".framework"* ]]; then
echo "skip $ONE_LINE"
elif [ "${ONE_LINE##*.}" = "c" ] || [ "${ONE_LINE##*.}" = "m" ] || [ "${ONE_LINE##*.}" = "mm" ] || [ "${ONE_LINE##*.}" = "h" ]; then
#cp --parents $ONE_LINE checkstyle_dir
cp $ONE_LINE checkstyle_dir
fi
done < checkstyle_dir/filelist
# checkstyle
cp ~/.clang-format .
RESULT=`ls checkstyle_dir/*.[chm] | xargs clang-format -style=file -output-replacements-xml | grep -c "<replacement "`
if [ $RESULT -ne 0 ]; then
echo "===================================================================================================="
echo "Commit did not match clang-format, please use git commit --amend to modify and push to gerrit again!"
echo "===================================================================================================="
exit 1;
fi
"Use Method"
1, before deployment, the cloned warehouse, please manually copy to the local Engineering hooks directory:
$CP ~/.git_template/hooks/pre-commit. git/hooks/.
2, after deployment, the newly cloned warehouse, without any operation, can be used directly:
Reference
Http://stackoverflow.com/questions/22866609/can-clang-format-tell-me-if-formatting-changes-are-necessary
Https://github.com/andrewseidl/githook-clang-format/blob/master/clang-format.hook