From 16c864e161ba6d9f48fd7cb03c7c9b7beed1bfcb Mon Sep 17 00:00:00 2001 From: DarkFeather Date: Wed, 25 Jan 2023 22:31:55 -0600 Subject: [PATCH] Adding egrep hook; better iteration for whitespace; improving local tests --- Bash/header | 6 +++--- Hooks/pre-commit | 1 + Hooks/scripts.d/egrep | 7 +++++++ Hooks/scripts.d/local-hooks | 15 +++++++++------ Hooks/scripts.d/whitespace | 5 ++--- tests/test_hooks.py | 2 +- tests/test_imports.py | 2 +- 7 files changed, 24 insertions(+), 14 deletions(-) create mode 100755 Hooks/scripts.d/egrep diff --git a/Bash/header b/Bash/header index 065cc3d..0bf9650 100644 --- a/Bash/header +++ b/Bash/header @@ -47,7 +47,7 @@ function logstatement() { ## Storage functions ## function addfs() { - if [ -z "$1" ] || [ -z "$2" ] || (echo "$3" | egrep -v -E '^[0-9]{1,4}'); then + if [ -z "$1" ] || [ -z "$2" ] || (echo "$3" | grep -v -E '^[0-9]{1,4}'); then echo "Need a filesystem(1), volume group path(2), and size (3)" return fi @@ -139,7 +139,7 @@ function prompt-var() { fi inputtime="0" #echo "$0 is checking for $1 -- currently set to <${!1}>" - while [ -z "${!1}" ] || (echo "${!1}" | egrep -v '^[-0-9a-zA-Z ,.@]{1,50}$' &>/dev/null); do + while [ -z "${!1}" ] || (echo "${!1}" | grep -v -E '^[-0-9a-zA-Z ,.@]{1,50}$' &>/dev/null); do printf "%s: " "$2"; read $1; if [ "${!1}" == "OPT" ] || [ $inputtime -gt $maxattempts ]; then exit 0; fi @@ -254,7 +254,7 @@ function getmagnetlink { function findaninixcheckouts { ### Find git checkouts in $pwd that are from AniNIX/Foundation - find . -type f -name config -exec egrep -l 'url[[:space:]]=[[:space:]]/srv/foundation|url[[:space:]]=[[:space:]]https://(foundation.)aninix.net|url[[:space:]]=[[:space:]]([a-zA-Z0-9])+@(foundation.)aninix.net' {} \; 2>/dev/null | sed 's#.git/config$##' + find . -type f -name config -exec grep -l -E 'url[[:space:]]=[[:space:]]/srv/foundation|url[[:space:]]=[[:space:]]https://(foundation.)aninix.net|url[[:space:]]=[[:space:]]([a-zA-Z0-9])+@(foundation.)aninix.net' {} \; 2>/dev/null | sed 's#.git/config$##' } function aninixgitstatus { diff --git a/Hooks/pre-commit b/Hooks/pre-commit index b36ea79..ffff33f 100755 --- a/Hooks/pre-commit +++ b/Hooks/pre-commit @@ -9,6 +9,7 @@ # Run all our checking scripts. for script in $(find `dirname "$0"`/scripts.d -type f); do + echo "Running ${script}" "$script" if [ $? -ne 0 ]; then echo "$script" failed checks. diff --git a/Hooks/scripts.d/egrep b/Hooks/scripts.d/egrep new file mode 100755 index 0000000..02ce341 --- /dev/null +++ b/Hooks/scripts.d/egrep @@ -0,0 +1,7 @@ +#!/bin/bash + +if git ls-files | xargs --delimiter='\n' grep -irlE 'egrep' | grep -v 'Hooks/scripts.d/egrep' ; then + echo 'Egrep has been deprecated as of GNU grep 3.8 -- replace `egrep` with `grep -E`.' + echo https://www.gnu.org/software/grep/manual/grep.html + exit 1 +fi diff --git a/Hooks/scripts.d/local-hooks b/Hooks/scripts.d/local-hooks index 4a94399..d2a2d70 100755 --- a/Hooks/scripts.d/local-hooks +++ b/Hooks/scripts.d/local-hooks @@ -1,8 +1,11 @@ +#!/bin/bash # This script ensures that repos can commit personal hooks in the ./precommit-hooks folder. -for script in `find precommit-hooks -type f`; do - if ! bash "$script"; then - echo "Local script $script failed checks." - exit 1 - fi -done +if [ -d precommit-hooks ]; then + for script in `find precommit-hooks -type f`; do + if ! bash "$script"; then + echo "Local script $script failed checks." + exit 1 + fi + done +fi diff --git a/Hooks/scripts.d/whitespace b/Hooks/scripts.d/whitespace index b9a4927..cc8e13c 100755 --- a/Hooks/scripts.d/whitespace +++ b/Hooks/scripts.d/whitespace @@ -2,13 +2,12 @@ # Sourced from https://github.com/git/git/blob/master/templates/hooks--pre-commit.sample -if git ls-files | xargs egrep -irl '\s\+$' ; then +if git ls-files | xargs --delimiter='\n' grep -irlE '\s\+$'; then echo The above lines have trailing whitespace. Run "sed -i 's/\s\+$//'" on the affected files. exit 1 fi -if git rev-parse --verify HEAD >/dev/null 2>&1 -then +if git rev-parse --verify HEAD >/dev/null 2>&1; then against=HEAD else # Initial commit: diff against an empty tree object diff --git a/tests/test_hooks.py b/tests/test_hooks.py index e2edb2a..f90c8fd 100644 --- a/tests/test_hooks.py +++ b/tests/test_hooks.py @@ -2,7 +2,7 @@ import os import pytest def test_hooks_exec(): - fh = os.popen("find Hooks -type f -exec ls -l {} \\; | egrep ^-rw-") + fh = os.popen("find Hooks -type f -exec ls -l {} \\; | grep -E ^-rw-") output = fh.read() retcode = fh.close() assert retcode == 256 and output == '' diff --git a/tests/test_imports.py b/tests/test_imports.py index b61b290..fdb64d4 100644 --- a/tests/test_imports.py +++ b/tests/test_imports.py @@ -2,7 +2,7 @@ import os import pytest def test_bash_import(): - fh = os.popen("/bin/bash -c 'source Bash/header; [ `declare -F | wc -l` -eq `egrep -c ^function\\ Bash/header` ]'", mode='r', buffering=-1) + fh = os.popen("/bin/bash -c 'source Bash/header; [ `declare -F | wc -l` -eq `grep -E -c ^function\\ Bash/header` ]'", mode='r', buffering=-1) output = fh.read() retcode = fh.close() assert retcode == None