From 85812cd25d9e2f015bb71b26d51458b3718bf6c7 Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Tue, 3 Nov 2020 07:07:48 +0100 Subject: Merging upstream version 0.14.0. Signed-off-by: Daniel Baumann --- gitlint/files/commit-msg | 78 ++++++++++-------------------------------------- 1 file changed, 16 insertions(+), 62 deletions(-) (limited to 'gitlint/files/commit-msg') diff --git a/gitlint/files/commit-msg b/gitlint/files/commit-msg index e468290..6a25d34 100644 --- a/gitlint/files/commit-msg +++ b/gitlint/files/commit-msg @@ -8,74 +8,28 @@ stdin_available=1 (exec < /dev/tty) 2> /dev/null || stdin_available=0 if [ $stdin_available -eq 1 ]; then - # Set bash color codes in case we have a tty - RED="\033[31m" - YELLOW="\033[33m" - GREEN="\033[32m" - END_COLOR="\033[0m" - # Now that we know we have a functional tty, set stdin to it so we can ask the user questions :-) exec < /dev/tty -else - # Unset bash colors if we don't have a tty - RED="" - YELLOW="" - GREEN="" - END_COLOR="" -fi -run_gitlint(){ - echo "gitlint: checking commit message..." - python -m gitlint.cli --staged --msg-filename "$1" - gitlint_exit_code=$? -} - -# Prompts a given yes/no question. -# Returns 0 if user answers yes, 1 if no -# Reprompts if different answer -ask_yes_no_edit(){ - ask_yes_no_edit_result="no" - # If we don't have a stdin available, then just return "No". - if [ $stdin_available -eq 0 ]; then - ask_yes_no_edit_result="no" - return; + # On Windows, we need to explicitely set our stdout to the tty to make terminal editing work (e.g. vim) + # See SO for windows detection in bash (slight modified to work on plain shell (not bash)): + # https://stackoverflow.com/questions/394230/how-to-detect-the-os-from-a-bash-script + if [ "$OSTYPE" = "cygwin" ] || [ "$OSTYPE" = "msys" ] || [ "$OSTYPE" = "win32" ]; then + exec > /dev/tty fi - # Otherwise, ask the question until the user answers yes or no - question="$1" - while true; do - read -p "$question" yn - case $yn in - [Yy]* ) ask_yes_no_edit_result="yes"; return;; - [Nn]* ) ask_yes_no_edit_result="no"; return;; - [Ee]* ) ask_yes_no_edit_result="edit"; return;; - esac - done -} - -run_gitlint "$1" +fi -while [ $gitlint_exit_code -gt 0 ]; do - echo "-----------------------------------------------" - echo "gitlint: ${RED}Your commit message contains the above violations.${END_COLOR}" - ask_yes_no_edit "Continue with commit anyways (this keeps the current commit message)? [y(es)/n(no)/e(dit)] " - if [ $ask_yes_no_edit_result = "yes" ]; then - exit 0 - elif [ $ask_yes_no_edit_result = "edit" ]; then - EDITOR=${EDITOR:-vim} - $EDITOR "$1" - run_gitlint "$1" - else - echo "Commit aborted." - echo "Your commit message: " - echo "-----------------------------------------------" - cat "$1" - echo "-----------------------------------------------" +gitlint --staged --msg-filename "$1" run-hook +exit_code=$? - exit $gitlint_exit_code - fi -done +# If we fail to find the gitlint binary (command not found), let's retry by executing as a python module. +# This is the case for Atlassian SourceTree, where $PATH deviates from the user's shell $PATH. +if [ $exit_code -eq 127 ]; then + echo "Fallback to python module execution" + python -m gitlint.cli --staged --msg-filename "$1" run-hook + exit_code=$? +fi -echo "gitlint: ${GREEN}OK${END_COLOR} (no violations in commit message)" -exit 0 +exit $exit_code ### gitlint commit-msg hook end ### -- cgit v1.2.3