From 4805678f7f69a7cb3288063fe6a0d62b016dae1e Mon Sep 17 00:00:00 2001 From: Pablo Lamela Date: Wed, 17 Jul 2024 17:56:37 +0200 Subject: [PATCH 1/2] Add support for relative files and rebasing --- scripts/devshell/prettify | 23 ++++++++++++++--------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/scripts/devshell/prettify b/scripts/devshell/prettify index d78dcf5e10..89f4fb6ce0 100755 --- a/scripts/devshell/prettify +++ b/scripts/devshell/prettify @@ -6,24 +6,26 @@ show_help() { echo "Format Haskell source files using fourmolu and stylish-haskell." echo "" echo "Options:" - echo " -t, --tracked Format all tracked Haskell (*.hs) files in the repository" - echo " -s, --staged Format all staged Haskell (*.hs) files" - echo " -m, --modified Format all modified Haskell (*.hs) files, including staged and unstaged" - echo " -n, --not-staged Format all non-staged modified Haskell (*.hs) files" - echo " -h, --help Show this help message" + echo " -t, --tracked Format all tracked Haskell (*.hs) files in the repository" + echo " -s, --staged Format all staged Haskell (*.hs) files" + echo " -m, --modified Format all modified Haskell (*.hs) files, including staged and unstaged" + echo " -n, --not-staged Format all non-staged modified Haskell (*.hs) files" + echo " -p, --previous-commit Format all Haskell (*.hs) files modified before the last commit (HEAD~1)" + echo " -h, --help Show this help message" } # Function to run the formatting commands run_format() { + top_level=$(git rev-parse --show-toplevel) for file in "$@"; do if [[ $file == *.hs ]]; then - if grep -qE '^#' "$file"; then + if grep -qE '^#' "$top_level/$file"; then echo "$file contains CPP. Skipping." else echo "Formatting: $file" - fourmolu -q -i "$file" - fourmolu -q -i "$file" - stylish-haskell -i "$file" + fourmolu -q -i "$top_level/$file" + fourmolu -q -i "$top_level/$file" + stylish-haskell -i "$top_level/$file" fi fi @@ -44,6 +46,9 @@ case $1 in -n|--not-staged) files=$(git diff --name-only --diff-filter=ACM '*.hs') ;; + -p|--previous-commit) + files=$(git diff --name-only --diff-filter=ACM HEAD~1 '*.hs') + ;; -h|--help) show_help exit 0 From b8e4f1231314d42b09015f4856e15e98d3f2457c Mon Sep 17 00:00:00 2001 From: Pablo Lamela Date: Wed, 17 Jul 2024 18:05:17 +0200 Subject: [PATCH 2/2] Simplify tool checking and improve arg checking --- scripts/devshell/prettify | 36 +++++++++++++++++++++++++----------- 1 file changed, 25 insertions(+), 11 deletions(-) diff --git a/scripts/devshell/prettify b/scripts/devshell/prettify index 89f4fb6ce0..60db957899 100755 --- a/scripts/devshell/prettify +++ b/scripts/devshell/prettify @@ -27,11 +27,12 @@ run_format() { fourmolu -q -i "$top_level/$file" stylish-haskell -i "$top_level/$file" fi - fi done } +flag_passed="true" + # Parse command line arguments case $1 in -t|--tracked) @@ -55,25 +56,38 @@ case $1 in ;; *) files="$@" + flag_passed="false" ;; esac -if !(which stylish-haskell > /dev/null 2>&1); then - echo "ERROR: stylish-haskell is not available!" - echo -e - echo "Try entering the development shell with:" - echo " nix develop" - exit 1 +if [[ $flag_passed == "true" ]] && [[ $# -gt 1 ]]; then + echo "ERROR: only one flag is allowed!" + echo -e + show_help + exit 1 fi -if !(which fourmolu > /dev/null 2>&1); then - echo "ERROR: fourmolu is not available!" +for file in $files; do + if [[ ! -a $file ]]; then + echo "ERROR: $file does not exist" + exit 1 + if ![[ -f $file ]]; then + echo "ERROR: $file is not a regular file" + exit + fi + fi +done + +for tool in stylish-haskell fourmolu +do + if !(which $tool > /dev/null 2>&1); then + echo "ERROR: $tool is not available!" echo -e echo "Try entering the development shell with:" echo " nix develop" exit 1 -fi - + fi +done if [[ -z $files ]]; then echo "No files to format!"