Skip to content
Snippets Groups Projects

Correct code style checks/improve code style

Merged Jan Frenzel requested to merge improve-code-style-check into preview
2 files
+ 30
17
Compare changes
  • Side-by-side
  • Inline
Files
2
@@ -46,20 +46,20 @@ function pattern_matches() {
@@ -46,20 +46,20 @@ function pattern_matches() {
test_res_count=0
test_res_count=0
if [[ "${ext}" == "sh" ]]; then
if [[ "${ext}" == "sh" ]]; then
# If shell script is provided
# If shell script is provided
if grep -qP "${pattern}" "$myfile"; then
if grep -qP "${pattern}" "${myfile}"; then
echo -e "[WARNING] ${warning}"
echo -e "[WARNING] ${warning}"
echo "[WARNING] This coding style was not used for following lines in file $(realpath "${myfile}"):"
echo "[WARNING] This coding style was not used for following lines in file $(realpath "${myfile}"):"
grep -nP "${pattern}" "$myfile"
grep -nP "${pattern}" "${myfile}"
echo ""
echo ""
return 0
return 0
fi
fi
elif [[ "${ext}" == "md" ]]; then
elif [[ "${ext}" == "md" ]]; then
# If markdown file is provided
# If markdown file is provided
# Check if the code snippet exists in the markdown file
# Check if the code snippet exists in the markdown file
if sed -n '/^```bash$/,/^```$/p' "$myfile" | grep -qv '```'; then
if sed -n '/^```bash$/,/^```$/p' "${myfile}" | grep -qv '```'; then
# Extracting code snippet within ```bash ... ```
# Extracting code snippet within ```bash ... ```
local test_string
local test_string
test_string=$(sed -n '/^```bash$/,/^```$/p' "$myfile" | grep -v '```')
test_string=$(sed -n '/^```bash$/,/^```$/p' "${myfile}" | grep -v '```')
# Check the exit code of pattern match
# Check the exit code of pattern match
if echo "${test_string}" | grep -qP "${pattern}"; then
if echo "${test_string}" | grep -qP "${pattern}"; then
@@ -95,7 +95,7 @@ if [[ $# -eq 1 ]]; then
@@ -95,7 +95,7 @@ if [[ $# -eq 1 ]]; then
;;
;;
-a | --all)
-a | --all)
echo "Checking in all files."
echo "Checking in all files."
files=$(find "$basedir" -name '*.md' -o -name '*.sh')
files=$(find "${basedir}" -name '*.md' -o -name '*.sh')
;;
;;
*)
*)
files="$1"
files="$1"
@@ -103,7 +103,7 @@ if [[ $# -eq 1 ]]; then
@@ -103,7 +103,7 @@ if [[ $# -eq 1 ]]; then
esac
esac
elif [[ $# -eq 0 ]]; then
elif [[ $# -eq 0 ]]; then
echo "Search in git-changed files."
echo "Search in git-changed files."
files=$(git diff --name-only "$(git merge-base HEAD "$branch")" | grep -e '.md$' -e '.sh$' || true)
files=$(git diff --name-only "$(git merge-base HEAD "${branch}")" | grep -e '.md$' -e '.sh$' || true)
else
else
usage
usage
fi
fi
@@ -112,7 +112,7 @@ any_fails=false
@@ -112,7 +112,7 @@ any_fails=false
for file in ${files}; do
for file in ${files}; do
# Skip the check of this current ($0) script.
# Skip the check of this current ($0) script.
if echo "${file}" | grep -qP "check-code-style.sh$"; then
if [[ "${file}" =~ check-code-style.sh$ ]]; then
continue
continue
fi
fi
@@ -133,11 +133,15 @@ for file in ${files}; do
@@ -133,11 +133,15 @@ for file in ${files}; do
# Line length less than 100 char length
# Line length less than 100 char length
file_ext="${file##*.}"
file_ext="${file##*.}"
if [[ "${file_ext}" == "sh" ]]; then
if [[ "${file_ext}" == "sh" ]]; then
#echo "Checking for max line length..."
if [[ "${file}" =~ grep-forbidden-patterns.sh$ ]]; then
pattern='^.{100}.*$'
: # skip
warning="Recommended maximum line length is 100 characters."
else
if pattern_matches "${file}" "${pattern}" "${warning}"; then
#echo "Checking for max line length..."
any_fails=true
pattern='^.{100}.*$'
 
warning="Recommended maximum line length is 100 characters."
 
if pattern_matches "${file}" "${pattern}" "${warning}"; then
 
any_fails=true
 
fi
fi
fi
fi
fi
Loading