From 4efe407765c57538efc5e5c1bec75351ba6b5c28 Mon Sep 17 00:00:00 2001 From: Ernest Borowski Date: Wed, 3 Jul 2019 14:30:15 +0200 Subject: [PATCH] [Project][code_formatter] Fix multiple errors in shell scripts: - Use null character as path delimiter in git and find commands. It allows file path to contain newlines and other white space characters. - Add quotes when needed to prevent globbing and word splitting. - Simplify logic. Change-Id: I58de7a3bd1e2e3165ada8b7650b2863e1fc95c15 Signed-off-by: Ernest Borowski --- tools/codestyle/c++_clang_formatter.sh | 47 ++++++++++------------ tools/codestyle/code_formatter.sh | 44 +++++++++++--------- tools/codestyle/code_validation.sh | 73 +++++++++++++++++----------------- tools/codestyle/cpplint_tizen_dir.sh | 22 +++++----- tools/codestyle/js_eslint_formatter.sh | 52 +++++++++--------------- tools/redirect-stdout.sh | 2 +- 6 files changed, 114 insertions(+), 126 deletions(-) diff --git a/tools/codestyle/c++_clang_formatter.sh b/tools/codestyle/c++_clang_formatter.sh index 863e160..7e0b32e 100755 --- a/tools/codestyle/c++_clang_formatter.sh +++ b/tools/codestyle/c++_clang_formatter.sh @@ -1,7 +1,16 @@ #!/bin/bash +SOURCE="${BASH_SOURCE[0]}" +while [ -h "$SOURCE" ]; do # resolve $SOURCE until the file is no longer a symlink + SCRIPT_DIR="$( cd -P "$( dirname "$SOURCE" )" >/dev/null 2>&1 && pwd )" + SOURCE="$(readlink "$SOURCE")" + [[ $SOURCE != /* ]] && SOURCE="$SCRIPT_DIR/$SOURCE" + # if $SOURCE was a relative symlink, we need to resolve it relative to the path where the symlink file was located +done +SCRIPT_DIR="$( cd -P "$( dirname "$SOURCE" )" >/dev/null 2>&1 && pwd )" + # https://clangformat.com/ -command -v clang-format >/dev/null 2>&1 || { +if [ ! "$(command -v clang-format)" ]; then echo >&2 "clang-format is required, but it's not installed"; echo "-------------------------------------------------------------------------------------"; echo "To install clang-format on Debian/Ubuntu, execute the following commands." @@ -9,7 +18,7 @@ command -v clang-format >/dev/null 2>&1 || { echo " sudo ln -s /usr/bin/clang-format-3.9 /usr/bin/clang-format" echo "-------------------------------------------------------------------------------------"; exit 1; - } +fi config='{ BasedOnStyle: Google, @@ -24,41 +33,27 @@ AllowShortFunctionsOnASingleLine: false, formatC++() { printf "." - # ignoring file not found errors - ls "$1"/*.cc &>/dev/null && clang-format -i -style="$config" "$1"/*.cc; - ls "$1"/*.cpp &>/dev/null && clang-format -i -style="$config" "$1"/*.cpp - ls "$1"/*.h &>/dev/null && clang-format -i -style="$config" "$1"/*.h - ls "$1"/*.hpp &>/dev/null && clang-format -i -style="$config" "$1"/*.hpp -} - -checkDirectoriesRecursively() { - dirs="$(find $1 -type d)" - for d in $dirs; - do - if [[ -d "$d" ]]; then - #echo "FORMAT $d" - formatC++ "$d"; - fi - done + find -- "$1" -type f \( -name '*.cc' -o -name '*.cpp' -o -name '*.h' -o -name '*.hpp' \) \ + -print0 | xargs --null --no-run-if-empty clang-format -i -style="$config" } checkUpdatedFiles() { - files=$(git diff-index --name-only --diff-filter=AM HEAD --) - for f in $files; - do - printf "." - if [ ${f: -3} == ".cc" ] || [ ${f: -4} == ".cpp" ] || [ ${f: -2} == ".h" ] || [ ${f: -4} == ".hpp" ]; then + files="$(git diff-index -z --name-only --diff-filter=AM HEAD --)" + while IFS= read -r -d '' f; do + if [ "${f: -3}" == ".cc" ] || [ "${f: -4}" == ".cpp" ] || [ "${f: -2}" == ".h" ] || \ + [ "${f: -4}" == ".hpp" ]; then + printf '.' clang-format -i -style="$config" "$f"; fi - done + done <<< "$files" } printf "C/C++ reformatting: "; if [[ $# -eq 0 ]]; then - checkDirectoriesRecursively "src/" + formatC++ "$SCRIPT_DIR/../../src/" elif [[ "$1" == "-u" ]]; then checkUpdatedFiles else - checkDirectoriesRecursively "$1" + formatC++ "$1" fi printf " DONE C/C++ reformatting\n" diff --git a/tools/codestyle/code_formatter.sh b/tools/codestyle/code_formatter.sh index 4ba525e..ef553dd 100755 --- a/tools/codestyle/code_formatter.sh +++ b/tools/codestyle/code_formatter.sh @@ -1,7 +1,16 @@ #!/bin/bash -dir="$(dirname "$(readlink -f "$0")")" -dir2analyze="$(pwd)/src" +SOURCE="${BASH_SOURCE[0]}" +while [ -h "$SOURCE" ]; do # resolve $SOURCE until the file is no longer a symlink + SCRIPT_DIR="$( cd -P "$( dirname "$SOURCE" )" >/dev/null 2>&1 && pwd )" + SOURCE="$(readlink "$SOURCE")" + [[ $SOURCE != /* ]] && SOURCE="$SCRIPT_DIR/$SOURCE" + # if $SOURCE was a relative symlink, we need to resolve it relative to the path where the symlink file was located +done +SCRIPT_DIR="$( cd -P "$( dirname "$SOURCE" )" >/dev/null 2>&1 && pwd )" +echo "$SCRIPT_DIR" + +dir2analyze="$SCRIPT_DIR/../../src" analyzeJS="false"; analyzeC="false"; files_to_update=""; @@ -12,7 +21,7 @@ printHelp() { echo "coding style. THIS COULD CHANGE YOUR FILES. Please commit your changes locally" echo "if you want to have meaningful changes divided from style-only changes" echo "Using it without specifying a path would analyse \"src\" from current directory." - printf -- "\nUsage: $(basename $0) [directory] [options ...]\n" + printf -- "\nUsage: %s [directory] [options ...]\n" "$(basename "$0")" printf -- "Options:\n" printf -- "-u\t\tcheck all JS/C/C++ files that has been modified since last commit and fix issues\n" printf -- "-uc\t\tcheck all C/C++ files that has been modified since last commit and fix issues\n" @@ -25,24 +34,24 @@ printHelp() { } formatJsAndC++() { - path="$(readlink -f $1)" + path="$(readlink -f "$1")" if [[ "$analyzeC" == "true" ]]; then echo "Reformatting C++ sources recursively for directory $path"; - $dir/c++_clang_formatter.sh "$path"; + "$SCRIPT_DIR/c++_clang_formatter.sh" "$path"; else echo "no C++ sources reformatting - please use '-c' option to enable it"; fi if [[ "$analyzeJS" == "true" ]]; then echo "Reformatting JS sources recursively for directory $path"; - $dir/js_eslint_formatter.sh "$path"; + "$SCRIPT_DIR/js_eslint_formatter.sh" "$path"; else echo "no JS sources reformatting - please use '-js' option to enable it"; fi } backupUpdatedFiles() { - files_to_update=$(git diff-index --name-only --diff-filter=AM HEAD --); + files_to_update="$(git diff-index -z --name-only --diff-filter=AM HEAD --)"; if [[ "$files_to_update" == "" ]]; then echo "There are no updated files to dormat"; exit; @@ -51,10 +60,9 @@ backupUpdatedFiles() { dir_name="backup_${time_stamp}"; echo "Creating backup of not commited changes in directory: $dir_name"; mkdir -p "${dir_name}"; - for f in "$files_to_update" - do - cp $f "$dir_name"; - done + while IFS= read -r -d '' file; do + cp -- "$file" "$dir_name"; + done <<< "$files_to_update" } formatUpdatedJsAndC++() { @@ -63,8 +71,8 @@ formatUpdatedJsAndC++() { fi echo "Reformatting C++ and JS sources from list:"; git diff-index --name-only --diff-filter=AM HEAD --; - $dir/c++_clang_formatter.sh -u; - $dir/js_eslint_formatter.sh -u; + "$SCRIPT_DIR/c++_clang_formatter.sh" -u; + "$SCRIPT_DIR/js_eslint_formatter.sh" -u; } formatUpdatedJs() { @@ -73,16 +81,16 @@ formatUpdatedJs() { fi echo "Reformatting JS sources from list:"; git diff-index --name-only --diff-filter=AM HEAD --; - $dir/js_eslint_formatter.sh -u; + "$SCRIPT_DIR/js_eslint_formatter.sh" -u; } formatUpdatedC++() { - if [["$files_to_update" == "" ]]; then + if [[ "$files_to_update" == "" ]]; then exit fi echo "Reformatting C++ sources from list:"; git diff-index --name-only --diff-filter=AM HEAD --; - $dir/c++_clang_formatter.sh -u; + "$SCRIPT_DIR/c++_clang_formatter.sh" -u; } for arg in "$@"; @@ -113,11 +121,11 @@ do if [[ -d "$arg" ]]; then dir2analyze="$arg"; else - (>&2 printf "ERROR: directory '$arg' does not exist\n\n") + (>&2 printf "ERROR: directory %s does not exist\n\n" "$arg") printHelp exit 1 fi fi done -formatJsAndC++ "$dir2analyze" \ No newline at end of file +formatJsAndC++ "$dir2analyze" diff --git a/tools/codestyle/code_validation.sh b/tools/codestyle/code_validation.sh index 9e9b519..a2777e4 100755 --- a/tools/codestyle/code_validation.sh +++ b/tools/codestyle/code_validation.sh @@ -1,8 +1,17 @@ #!/bin/bash -script_dir="$(dirname "$(readlink -f "$0")")" + +SOURCE="${BASH_SOURCE[0]}" +while [ -h "$SOURCE" ]; do # resolve $SOURCE until the file is no longer a symlink + SCRIPT_DIR="$( cd -P "$( dirname "$SOURCE" )" >/dev/null 2>&1 && pwd )" + SOURCE="$(readlink "$SOURCE")" + [[ $SOURCE != /* ]] && SOURCE="$SCRIPT_DIR/$SOURCE" + # if $SOURCE was a relative symlink, we need to resolve it relative to the path where the symlink file was located +done +SCRIPT_DIR="$( cd -P "$( dirname "$SOURCE" )" >/dev/null 2>&1 && pwd )" + config_file="eslint_config_mandatory_length90.js" -dir2analyze="$(pwd)/src"; +dir2analyze="$SCRIPT_DIR/../../src"; analyzeJS="false"; analyzeC="false"; @@ -10,7 +19,7 @@ printHelp() { echo "Script for validation source code about coding style rules violations." echo "The script would not change any files, only print report about found issues." echo "Using it without specifying a path would analyse \"src\" from current directory." - printf -- "\nUsage: $(basename $0) [directory] [options ...]\n" + printf -- "\nUsage: %s [directory] [options ...]\n" "$(basename "$0")" printf -- "Options:\n" printf -- "-u\t\tcheck all JS/C/C++ files that has been modified since last commit\n" printf -- "-uc\t\tcheck all C/C++ files that has been modified since last commit\n" @@ -22,34 +31,31 @@ printHelp() { } checkEslint() { - command -v eslint >/dev/null 2>&1 || { - echo >&2 "eslint is required, but it's not installed."; + if [ ! "$(command -v eslint)" ]; then + echo "eslint is required, but it's not installed." 1>&2; echo "-------------------------------------------------------------------------------------"; echo "If you want to make ESLint available to tools that run across all of your projects, we recommend installing ESLint globally. You can do so using npm" echo "(it need to be installed on your machine - check https://www.rosehosting.com/blog/install-npm-on-ubuntu-16-04/):" echo "$ sudo npm install -g eslint" echo "-------------------------------------------------------------------------------------"; exit 1; - } + fi } checkC() { printf "." echo "Checking C style of $1 directory" - $script_dir/cpplint_tizen_dir.sh "$1" + "$SCRIPT_DIR/cpplint_tizen_dir.sh" "$1" } checkJS() { - ls "$1"/*.js &>/dev/null - if [[ "$?" -eq 0 ]]; then - printf "." - echo "Checking JS style of $1 directory" - eslint -c "$script_dir/$config_file" "$1"/*.js - fi + printf "." + find -- "$1" -type f -name '*.js' -print0 | \ + xargs --null --no-run-if-empty eslint -c "$SCRIPT_DIR/config_file" } checkJsAndC++() { - path=$(readlink -f $1) + path="$(readlink -f "$1")" if [[ "$analyzeC" = true ]]; then echo "Checking C++ recursively for directory $path" checkC "$path" @@ -60,13 +66,7 @@ checkJsAndC++() { if [[ "$analyzeJS" = "true" ]]; then echo "Checking JS recursively for directory $path" checkEslint - dirs=$(find $path -type d) - for d in $dirs; - do - if [[ -d "$d" ]]; then - checkJS "$d"; - fi - done + checkJS "$path" else echo "no JS sources validation"; fi @@ -74,17 +74,16 @@ checkJsAndC++() { checkUpdatedJs() { echo "Validating updated JS files:"; - files=$(git diff-index --name-only --diff-filter=AM HEAD --) - config_file="$script_path/eslintrc_mandatory_4spaces.js" + files="$(git diff-index -z --name-only --diff-filter=AM HEAD --)" + config_file="$SCRIPT_DIR/eslintrc_mandatory_4spaces.js" counter=0 - for f in $files; - do - if [[ ${f: -3} == ".js" ]]; then + while IFS= read -r -d '' f; do + if [[ "${f: -3}" == ".js" ]]; then echo "Checking: $f" counter=$((counter+1)) - eslint --no-eslintrc -c "$config_file" $f + eslint --no-eslintrc -c "$config_file" "$f" fi - done + done <<< "$(printf "%s" "$files")" if [[ "$counter" -eq 0 ]]; then echo "No updated JS files to validate." else @@ -94,17 +93,17 @@ checkUpdatedJs() { checkUpdatedC++() { echo "Validating updated C++ files:"; - patch_cpp=`dirname $0`/cpplint_tizen_160919.py - files=$(git diff-index --name-only --diff-filter=AM HEAD --) + patch_cpp="$SCRIPT_DIR/cpplint_tizen_160919.py" + files="$(git diff-index -z --name-only --diff-filter=AM HEAD --)" counter=0 - for f in $files; - do - if [ ${f: -3} == ".cc" ] || [ ${f: -4} == ".cpp" ] || [ ${f: -2} == ".h" ] || [ ${f: -4} == ".hpp" ]; then - echo "Checking: $f\n" + while IFS= read -r -d '' f; do + if [ "${f: -3}" == ".cc" ] || [ "${f: -4}" == ".cpp" ] || [ "${f: -2}" == ".h" ] || \ + [ "${f: -4}" == ".hpp" ]; then + echo "Checking: $f" counter=$((counter+1)) - python $patch_cpp $f; + python "$patch_cpp" "$f"; fi - done + done <<< "$(printf "%s" "$files")" if [[ "$counter" -eq 0 ]]; then echo "No updated C++ files to validate." else @@ -138,7 +137,7 @@ do if [[ -d "$arg" ]]; then dir2analyze="$arg"; else - (>&2 printf "ERROR: directory '$arg' does not exist\n\n") + (>&2 printf "ERROR: directory %s does not exist\n\n" "$arg") printHelp exit 1 fi diff --git a/tools/codestyle/cpplint_tizen_dir.sh b/tools/codestyle/cpplint_tizen_dir.sh index 465a5cc..b85820d 100755 --- a/tools/codestyle/cpplint_tizen_dir.sh +++ b/tools/codestyle/cpplint_tizen_dir.sh @@ -1,14 +1,16 @@ #!/bin/bash -patch_cpp=`dirname $0`/cpplint_tizen_160919.py - -# check rule to cpp -for i in `find $1 -name "*.cpp" -o -name "*.cc"` -do - python $patch_cpp $i +SOURCE="${BASH_SOURCE[0]}" +while [ -h "$SOURCE" ]; do # resolve $SOURCE until the file is no longer a symlink + SCRIPT_DIR="$( cd -P "$( dirname "$SOURCE" )" >/dev/null 2>&1 && pwd )" + SOURCE="$(readlink "$SOURCE")" + [[ $SOURCE != /* ]] && SOURCE="$SCRIPT_DIR/$SOURCE" + # if $SOURCE was a relative symlink, we need to resolve it relative to the path where the symlink file was located done +SCRIPT_DIR="$( cd -P "$( dirname "$SOURCE" )" >/dev/null 2>&1 && pwd )" -for i in `find $1 -name "*.h"` -do - python $patch_cpp $i -done +patch_cpp="$SCRIPT_DIR/cpplint_tizen_160919.py" + +# check rule to cpp +find -- "$1" \( -name "*.cpp" -o name "*.cc" \) -exec python "$patch_cpp" {} \; +find -- "$1" -name "*.h" -exec python "$patch_cpp" {} \; diff --git a/tools/codestyle/js_eslint_formatter.sh b/tools/codestyle/js_eslint_formatter.sh index e4e6067..2b23fb9 100755 --- a/tools/codestyle/js_eslint_formatter.sh +++ b/tools/codestyle/js_eslint_formatter.sh @@ -2,77 +2,61 @@ # https://github.sec.samsung.net/RS-SA/coding-style-guides/tree/master/javascript # https://eslint.org/ -command -v eslint >/dev/null 2>&1 || { - +if [ ! "$(command -v eslint)" ]; then echo >&2 "eslint is required, but it's not installed"; echo "-------------------------------------------------------------------------------------"; echo "To install eslint on Debian/Ubuntu, execute the following commands." echo "sudo npm install -g eslint" echo "-------------------------------------------------------------------------------------"; exit 1; -} +fi #https://prettier.io/docs/en/install.html -command -v prettier >/dev/null 2>&1 || { - +if [ ! "$(command -v prettier)" ]; then echo >&2 "prettier is required, but it's not installed"; echo "-------------------------------------------------------------------------------------"; echo "To install prettier on Debian/Ubuntu, execute the following commands." echo "sudo npm install -g prettier" echo "-------------------------------------------------------------------------------------"; exit 1; -} +fi -script_path="$( cd "$(dirname "$0")" ; pwd -P )"; +script_path="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null && pwd )" config_file="$script_path/eslintrc_mandatory_4spaces.js" prettier_config_file="$script_path/prettier.config.js" -formatJSDirectory() { - ls $1/*.js &>/dev/null - if [[ $? -eq 0 ]]; then - for f in $(ls $1/*.js) - do - formatJSFile $f; - done - fi -} - formatJSFile() { printf "." # fixing line breaks using prettier prettier --config "$prettier_config_file" "$1" --write # using eslint for fixing some js issues - eslint --no-eslintrc -c "$config_file" $1 --fix + eslint --no-eslintrc -c "$config_file" "$1" --fix # issues that are not fixed, will be printed fur the user to take care of them } -checkDirectoriesRecursive() { - dirs=$(find $1 -type d) - for d in $dirs; - do - if [[ -d $d ]]; then - formatJSDirectory $d; - fi - done +formatJSPath() { + js_files="$(find -- "$1" -type f -name "*.js" -print0)" + while IFS= read -r -d '' f; do + formatJSFile "$f"; + done <<< "$js_files" } checkUpdatedFiles() { - files=$(git diff-index --name-only --diff-filter=AM HEAD --) - for f in $files; - do - if [[ ${f: -3} == ".js" ]]; then - formatJSFile $f; + files="$(git diff-index -z --name-only --diff-filter=AM HEAD --)" + while IFS= read -r -d '' f; do + if [[ "${f: -3}" == ".js" ]]; then + formatJSFile "$f"; fi - done + done <<< "$files" } printf "JS reformatting: "; if [[ $# -eq 0 ]]; then - checkDirectoriesRecursive src/ + formatJSPath "$script_path/../../src/" elif [[ "$1" == "-u" ]]; then checkUpdatedFiles else - checkDirectoriesRecursive $1 + formatJSPath "$1" fi echo " DONE JS reformatting" diff --git a/tools/redirect-stdout.sh b/tools/redirect-stdout.sh index 5c9d4eb..2050611 100755 --- a/tools/redirect-stdout.sh +++ b/tools/redirect-stdout.sh @@ -17,4 +17,4 @@ if [ ${#} -ne 2 ] ; then exit 2 fi -exec $1 > $2 +exec "$1" > "$2" -- 2.7.4