summaryrefslogtreecommitdiff
path: root/infra/command/format
diff options
context:
space:
mode:
Diffstat (limited to 'infra/command/format')
-rw-r--r--infra/command/format83
1 files changed, 74 insertions, 9 deletions
diff --git a/infra/command/format b/infra/command/format
index cc1fc0959..9fe475371 100644
--- a/infra/command/format
+++ b/infra/command/format
@@ -2,7 +2,56 @@
INVALID_EXIT=0
FILES_TO_CHECK=()
+DIRECTORIES_TO_BE_TESTED=()
DIRECTORIES_NOT_TO_BE_TESTED=()
+CLANG_FORMAT_CANDIDATES=()
+PATCH_FILE=format.patch
+CHECK_DIFF_ONLY="0"
+CHECK_STAGED_ONLY="0"
+
+function Usage()
+{
+ echo "Usage: $0 $(basename ${BASH_SOURCE[0]}) [OPTIONS] [<file|dir> ...]"
+ echo "If no arguments are specified, it formats all nnas codes"
+ echo "If <file>s are given, it reformats the files"
+ echo ""
+ echo "Options:"
+ echo " --clang-format <TOOL> clang format bin (default: clang-format-3.9, clang-format)"
+ echo " --diff-only check diff files with master"
+ echo " --staged-only check git staged files"
+}
+
+while [[ $# -gt 0 ]]
+do
+ arg="$1"
+ case $arg in
+ -h|--help|help)
+ Usage
+ exit 0
+ ;;
+ --clang-format)
+ CLANG_FORMAT_CANDIDATES=($2)
+ shift 2
+ ;;
+ --clang-format=*)
+ CLANG_FORMAT_CANDIDATES=(${1#*=})
+ shift
+ ;;
+ --staged-only)
+ CHECK_STAGED_ONLY="1"
+ CHECK_DIFF_ONLY="1"
+ shift
+ ;;
+ --diff-only)
+ CHECK_DIFF_ONLY="1"
+ shift
+ ;;
+ *)
+ DIRECTORIES_TO_BE_TESTED+=($1)
+ shift
+ ;;
+ esac
+done
function pushd () {
command pushd "$@" > /dev/null
@@ -29,6 +78,12 @@ function check_newline() {
for f in ${FILES_TO_FIX[@]}; do
tr '\r' '\n' < $f > $f.fixed && cat $f.fixed > $f && rm $f.fixed
done
+ # Check no new line at end of file
+ for f in ${FILES_TO_CHECK[@]}; do
+ if diff /dev/null "$f" | tail -1 | grep '^\\ No newline' > /dev/null; then
+ echo >> "$f"
+ fi
+ done
}
function check_permission() {
@@ -58,13 +113,13 @@ function check_cpp_files() {
return
fi
- CLANG_FORMAT_CANDIDATES=()
- CLANG_FORMAT_CANDIDATES+=("clang-format")
CLANG_FORMAT_CANDIDATES+=("clang-format-3.9")
+ CLANG_FORMAT_CANDIDATES+=("clang-format")
for CLANG_FORMAT_CANDIDATE in ${CLANG_FORMAT_CANDIDATES[@]}; do
if command_exists ${CLANG_FORMAT_CANDIDATE} ; then
CLANG_FORMAT="${CLANG_FORMAT_CANDIDATE}"
+ break
fi
done
@@ -139,15 +194,15 @@ function check_python_files() {
pushd ${NNAS_PROJECT_PATH}
-if [ -n "$(git diff)" ]; then
+if [[ -n "$(git diff)" ]] && { [[ "${CHECK_DIFF_ONLY}" != "1" ]] || [[ "${CHECK_STAGED_ONLY}" != "1" ]]; }; then
echo "[WARNING] Commit all the changes before running format check"
- echo " format.patch file will contain unstaged files"
+ echo " ${PATCH_FILE} file will contain unstaged files"
fi
__Check_CPP=${CHECK_CPP:-"1"}
__Check_PYTHON=${CHECK_PYTHON:-"1"}
-FILES_TO_CHECK=$(git ls-files -c --exclude-standard)
+FILES_TO_CHECK=$(git ls-files -c --exclude-standard ${DIRECTORIES_TO_BE_TESTED[@]})
if [[ "${CHECK_DIFF_ONLY}" = "1" ]]; then
MASTER_EXIST=$(git rev-parse --verify master)
CURRENT_BRANCH=$(git branch | grep \* | cut -d ' ' -f2-)
@@ -157,7 +212,11 @@ if [[ "${CHECK_DIFF_ONLY}" = "1" ]]; then
elif [[ "${CURRENT_BRANCH}" = "master" ]]; then
echo "Current branch is master"
else
- FILES_TO_CHECK=$(git diff --name-only --diff-filter=d HEAD~${DIFF_COMMITS})
+ if [[ "${CHECK_STAGED_ONLY}" = "1" ]]; then
+ FILES_TO_CHECK=$(git diff --staged --name-only --diff-filter=d)
+ else
+ FILES_TO_CHECK=$(git diff --name-only --diff-filter=d HEAD~${DIFF_COMMITS})
+ fi
fi
fi
@@ -170,7 +229,13 @@ check_permission
check_cpp_files
check_python_files
-DIFF=$(git diff | tee format.patch)
+if [[ "${CHECK_DIFF_ONLY}" = "1" ]] && [[ "${CHECK_STAGED_ONLY}" = "1" ]]; then
+ if [[ ! -z "${FILES_TO_CHECK}" ]]; then
+ DIFF=$(git diff ${FILES_TO_CHECK} | tee ${PATCH_FILE})
+ fi
+else
+ DIFF=$(git diff | tee ${PATCH_FILE})
+fi
popd
@@ -186,9 +251,9 @@ if [[ ! -z "${CRCHECK}" ]]; then
echo "${CRCHECK}"
fi
-if [[ ${PATCHFILE_SIZE} -ne 0 ]]; then
+if [[ -s ${PATCH_FILE} ]]; then
echo "[FAILED] Format checker failed and update code to follow convention."
- echo " You can find changes in format.patch"
+ echo " You can find changes in ${PATCH_FILE}"
fi
if [[ ${INVALID_EXIT} -ne 0 ]]; then