diff options
Diffstat (limited to 'misc/hooks/pre-commit-black')
-rwxr-xr-x | misc/hooks/pre-commit-black | 84 |
1 files changed, 77 insertions, 7 deletions
diff --git a/misc/hooks/pre-commit-black b/misc/hooks/pre-commit-black index 2dcc2e8cf1..76d97294da 100755 --- a/misc/hooks/pre-commit-black +++ b/misc/hooks/pre-commit-black @@ -6,7 +6,7 @@ ################################################################## # SETTINGS # Set path to black binary. -BLACK=`which black` +BLACK=`which black 2>/dev/null` BLACK_OPTIONS="-l 120" # Remove any older patches from previous commits. Set to true or false. @@ -18,13 +18,22 @@ FILE_EXTS="py" # Use pygmentize instead of cat to parse diff with highlighting. # Install it with `pip install pygments` (Linux) or `easy_install Pygments` (Mac) -PYGMENTIZE=`which pygmentize` +PYGMENTIZE=`which pygmentize 2>/dev/null` if [ ! -z "$PYGMENTIZE" ]; then READER="pygmentize -l diff" else READER=cat fi +# Path to zenity +ZENITY=`which zenity 2>/dev/null` + +# Path to xmessage +XMSG=`which xmessage 2>/dev/null` + +# Path to powershell (Windows only) +PWSH=`which powershell 2>/dev/null` + ################################################################## # There should be no need to change anything below this line. @@ -53,6 +62,19 @@ else fi if [ ! -x "$BLACK" ] ; then + if [ ! -t 1 ] ; then + if [ -x "$ZENITY" ] ; then + $ZENITY --error --title="Error" --text="Error: black executable not found." + exit 1 + elif [ -x "$XMSG" ] ; then + $XMSG -center -title "Error" "Error: black executable not found." + exit 1 + elif [ \( \( "$OSTYPE" = "msys" \) -o \( "$OSTYPE" = "win32" \) \) -a \( -x "$PWSH" \) ]; then + winmessage="$(canonicalize_filename "./.git/hooks/winmessage.ps1")" + $PWSH -noprofile -executionpolicy bypass -file "$winmessage" -center -title "Error" --text "Error: black executable not found." + exit 1 + fi + fi printf "Error: black executable not found.\n" printf "Set the correct path in $(canonicalize_filename "$0").\n" exit 1 @@ -99,14 +121,62 @@ fi # a patch has been created, notify the user and exit printf "\nThe following differences were found between the code to commit " printf "and the black formatter rules:\n\n" -$READER "$patch" -printf "\n" -# Allows us to read user input below, assigns stdin to keyboard -exec < /dev/tty +if [ -t 1 ] ; then + $READER "$patch" + printf "\n" + # Allows us to read user input below, assigns stdin to keyboard + exec < /dev/tty + terminal="1" +else + cat "$patch" + printf "\n" + # Allows non zero zenity/powershell output + set +e + terminal="0" +fi while true; do - read -p "Do you want to apply that patch (Y - Apply, N - Do not apply, S - Apply and stage files)? [Y/N/S] " yn + if [ $terminal = "0" ] ; then + if [ -x "$ZENITY" ] ; then + ans=$($ZENITY --text-info --filename="$patch" --width=800 --height=600 --title="Do you want to apply that patch?" --ok-label="Apply" --cancel-label="Do not apply" --extra-button="Apply and stage") + if [ "$?" = "0" ] ; then + yn="Y" + else + if [ "$ans" = "Apply and stage" ] ; then + yn="S" + else + yn="N" + fi + fi + elif [ -x "$XMSG" ] ; then + $XMSG -file "$patch" -buttons "Apply":100,"Apply and stage":200,"Do not apply":0 -center -default "Do not apply" -geometry 800x600 -title "Do you want to apply that patch?" + ans=$? + if [ "$ans" = "100" ] ; then + yn="Y" + elif [ "$ans" = "200" ] ; then + yn="S" + else + yn="N" + fi + elif [ \( \( "$OSTYPE" = "msys" \) -o \( "$OSTYPE" = "win32" \) \) -a \( -x "$PWSH" \) ]; then + winmessage="$(canonicalize_filename "./.git/hooks/winmessage.ps1")" + $PWSH -noprofile -executionpolicy bypass -file "$winmessage" -file "$patch" -buttons "Apply":100,"Apply and stage":200,"Do not apply":0 -center -default "Do not apply" -geometry 800x600 -title "Do you want to apply that patch?" + ans=$? + if [ "$ans" = "100" ] ; then + yn="Y" + elif [ "$ans" = "200" ] ; then + yn="S" + else + yn="N" + fi + else + printf "Error: zenity, xmessage, or powershell executable not found.\n" + exit 1 + fi + else + read -p "Do you want to apply that patch (Y - Apply, N - Do not apply, S - Apply and stage files)? [Y/N/S] " yn + fi case $yn in [Yy] ) git apply $patch; printf "The patch was applied. You can now stage the changes and commit again.\n\n"; |