vitess.io/vitess@v0.16.2/misc/git/hooks/shellcheck (about)

     1  #!/bin/bash
     2  # This file is based on the Go linter precommit hook
     3  # "golint". Therefore, both files are very similar.
     4  
     5  # This script does not handle file names that contain spaces.
     6  shfiles=$(git diff --cached --name-only --diff-filter=ACM | grep '.*\.sh$')
     7  if [ -z "$shfiles" ] ; then
     8    # No .sh files modified.
     9    exit 0
    10  fi
    11  
    12  if [ -z "$(command -v shellcheck)" ]; then
    13    echo "shellcheck not found, please run: brew or apt-get install shellcheck"
    14    exit 0
    15  fi
    16  
    17  errors=
    18  for file in $shfiles
    19  do
    20    # The -e SC1090,SC1091 suppressing warnings about trying to find
    21    # files imported with "source foo.sh". We only want to lint
    22    # the files modified as part of this current diff.
    23    errors+=$(shellcheck -e SC1090,SC1091 "$file" 2>&1)
    24  done
    25  
    26  [ -z "$errors" ] && exit 0
    27  
    28  # git doesn't give us access to user input, so let's steal it.
    29  if exec < /dev/tty; then
    30    # interactive shell. Prompt the user.
    31    echo
    32    echo "shellcheck suggestions were found. They're not enforced, but we're pausing"
    33    echo "to let you know before they get clobbered in the scrollback buffer."
    34    echo
    35    read -r -p 'Press enter to cancel, "s" to show all warnings or type "ack" to continue: '
    36    if [ "$REPLY" = "ack" ]; then
    37      exit 0
    38    fi
    39    if [ "$REPLY" = "s" ]; then
    40      echo
    41      echo "$errors"
    42    fi
    43  else
    44    # non-interactive shell (e.g. called from Eclipse). Just display the errors.
    45    echo "$errors"
    46  fi
    47  
    48  exit 1