From d0aa9cb542252341a58bf988025a0546ae6db8bc Mon Sep 17 00:00:00 2001 From: Noah Gorny Date: Thu, 22 Apr 2021 01:38:11 +0300 Subject: [PATCH] hooks: Add check-clean-files-txt.sh --- .pre-commit-config.yaml | 5 +++++ hooks/check-clean-files-txt.sh | 35 ++++++++++++++++++++++++++++++++++ 2 files changed, 40 insertions(+) create mode 100755 hooks/check-clean-files-txt.sh diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index c549d178..6b682bae 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -43,3 +43,8 @@ repos: language: system files: "\\.bash$" types: [file] + - id: clean-files-txt + name: Check that clean_files.txt is sorted alphabetically. + entry: ./hooks/check-clean-files-txt.sh + language: system + files: clean_files.txt diff --git a/hooks/check-clean-files-txt.sh b/hooks/check-clean-files-txt.sh new file mode 100755 index 00000000..e9a76d8f --- /dev/null +++ b/hooks/check-clean-files-txt.sh @@ -0,0 +1,35 @@ +#!/usr/bin/env bash + +file=$1 +# Should only be run on clean_files.txt +if [ "$file" != "clean_files.txt" ]; then + echo "Please run this script on clean_files.txt only!" + exit 1 +fi + +function compare_lines() { + prev="" + while read -r line; do + # Skip unimportant lines + [[ $line =~ "#" ]] && continue + [[ $line == "" ]] && continue + # Actual check + if [[ $prev > $line ]]; then + echo "$line should be before $prev" + exit 1 + fi + prev=$line + done <<< "$1" +} + +# Test root files +compare_lines "$(grep -v "/" "$file")" + +# Test root directory +compare_lines "$(grep "/$" "$file")" + +# Test non root directories +compare_lines "$(grep "/." "$file")" + +# Yay, all good! +exit 0