github.com/ActiveState/cli@v0.0.0-20240508170324-6801f60cd051/.githooks/pre-commit (about)

     1  #!/bin/sh
     2  
     3  exerr() {
     4    local code="${1}"
     5    local msg="${2}"
     6    [ "${code}" -gt 0 ] && echo >&2 "${msg}" && exit "${code}"
     7  }
     8  
     9  state run lint-staged
    10  exerr $? "Linting of staged files has failed"
    11  
    12  # Detect unformatted files.
    13  gofiles=$(git diff --cached --name-only --diff-filter=ACM | grep '.go$' | grep -vE '^vendor/')
    14  [ -z "$gofiles" ] && exit 0
    15  
    16  unformatted=$(gofmt -l $gofiles)
    17  [ -z "$unformatted" ] && exit 0
    18  
    19  # Format them.
    20  gofmt -w $unformatted
    21  exerr $? "Formatting of staged files has failed"
    22  
    23  # Notify the user of formatted files and fail.
    24  echo >&2 "The following files have been reformatted. Please verify and/or re-stage changes before attempting to commit again."
    25  for file in $unformatted; do
    26    echo >&2 "  $PWD/$file"
    27  done
    28  
    29  exit 1