github.com/jasonkeene/cli@v6.14.1-0.20160816203908-ca5715166dfb+incompatible/githooks/pre-commit/gofmt (about)

     1  #!/bin/sh
     2  
     3  set -e
     4  
     5  # Redirect output to stderr.
     6  exec 1>&2
     7  ROOT_DIR=$(cd $(dirname $(dirname $0)/)/.. && pwd)
     8  
     9  # Copyright 2012 The Go Authors. All rights reserved.
    10  # Use of this source code is governed by a BSD-style
    11  # license that can be found in the LICENSE file.
    12  
    13  # git gofmt pre-commit hook
    14  #
    15  # To use, store as .git/hooks/ratchet inside your repository and make sure
    16  # it has execute permissions.
    17  #
    18  # This script does not handle file names that contain spaces.
    19  
    20  set +e
    21  gofiles=$(git diff --cached --name-only --diff-filter=ACM | grep '.go$')
    22  set -e
    23  [ -z "$gofiles" ] && exit 0
    24  
    25  set +e
    26  unformatted=$(gofmt -l $gofiles)
    27  set -e
    28  [ -z "$unformatted" ] && exit 0
    29  
    30  # Some files are not gofmt'd. Print message and fail.
    31  
    32  echo "Go files must be formatted with gofmt. Please run:"
    33  for fn in $unformatted; do
    34      echo "  gofmt -w $PWD/$fn"
    35  done
    36  
    37  exit 1