From 054980e0f2c55d72e00498bbf4b94d7a2de81d28 Mon Sep 17 00:00:00 2001 From: Riyyi Date: Fri, 19 Aug 2022 02:27:34 +0200 Subject: [PATCH] Meta: Add feature to temporary clear unstaged files in lint-ci.sh --- script/lint-ci.sh | 35 +++++++++++++++++++++++++---------- 1 file changed, 25 insertions(+), 10 deletions(-) diff --git a/script/lint-ci.sh b/script/lint-ci.sh index 73b6aec..c5f4b47 100755 --- a/script/lint-ci.sh +++ b/script/lint-ci.sh @@ -7,11 +7,30 @@ b="$(tput bold)" red="$(tput setf 4)" +yellow="$(tput setf 6)" +blue="$(tput setf 1)" +green="$(tput setf 2)" n="$(tput sgr0)" if [ ! -d ".git" ]; then - echo "${b}${red}Error:${n} please run this script from the project root" >&2 - exit 1 + echo "${b}${red}Error:${n} please run this script from the project root" >&2 + exit 1 +fi + +# Temporary clear unstaged files from the repository to pass the diff checker +unstaged="$(git --no-pager diff --name-only)" +if [ -n "$unstaged" ]; then + patch=".git/patch-$(date +%s)" + echo "[${yellow}WARNING${n}]: Stashing unstaged files to $patch" + git --no-pager diff > "$patch" + git restore . + + remove() { + git apply "$patch" + rm -rf "${patch:?}" + echo "[${blue}INFO${n}]: Restored changes from $patch" + } + trap remove EXIT HUP INT TERM fi # Get the path from the project root to the script @@ -20,10 +39,6 @@ subDir="$(dirname -- "$0")" # Get all files staged for commit files="$(git --no-pager diff --cached --name-only)" -green="$(tput setf 2)" -red="$(tput setf 4)" -nc="$(tput sgr0)" - failures=0 linters=" @@ -33,9 +48,9 @@ lint-shell-script.sh for linter in $linters; do echo "Running $subDir/$linter" if "$subDir/$linter" "$files"; then - echo "[${green}PASS${nc}]: $subDir/$linter" + echo "[${green}PASS${n}]: $subDir/$linter" else - echo "[${red}FAIL${nc}]: $subDir/$linter" + echo "[${red}FAIL${n}]: $subDir/$linter" failures=$(( failures + 1 )) fi done @@ -43,9 +58,9 @@ done echo "Running $subDir/lint-clang-format.sh" # shellcheck disable=SC2086 if "$subDir/lint-clang-format.sh" "$files" && git diff --exit-code $files; then - echo "[${green}PASS${nc}]: $subDir/lint-clang-format.sh" + echo "[${green}PASS${n}]: $subDir/lint-clang-format.sh" else - echo "[${red}FAIL${nc}]: $subDir/lint-clang-format.sh" + echo "[${red}FAIL${n}]: $subDir/lint-clang-format.sh" failures=$(( failures + 1 )) fi