github.com/XiaoMi/Gaea@v1.2.5/misc/git/hooks/golint (about)

     1  #!/bin/bash
     2  # Copyright 2017 Google Inc.
     3  # 
     4  # Licensed under the Apache License, Version 2.0 (the "License");
     5  # you may not use this file except in compliance with the License.
     6  # You may obtain a copy of the License at
     7  # 
     8  #     http://www.apache.org/licenses/LICENSE-2.0
     9  # 
    10  # Unless required by applicable law or agreed to in writing, software
    11  # distributed under the License is distributed on an "AS IS" BASIS,
    12  # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  # See the License for the specific language governing permissions and
    14  # limitations under the License.
    15  
    16  # git golint pre-commit hook
    17  #
    18  # To use, store as .git/hooks/pre-commit inside your repository and make sure
    19  # it has execute permissions.
    20  
    21  if [ -z "$GOPATH" ]; then
    22    echo "ERROR: pre-commit hook for golint: \$GOPATH is empty. Please run 'source dev.env' to set the correct \$GOPATH."
    23    exit 1
    24  fi
    25  
    26  if [ -z "$(which golint)" ]; then
    27    echo "golint not found, please run: go get github.com/golang/lint/golint"
    28    exit 1
    29  fi
    30  
    31  # This script does not handle file names that contain spaces.
    32  gofiles=$(git diff --cached --name-only --diff-filter=d | grep '.go$')
    33  echo $gofiles
    34  
    35  errors=
    36  
    37  # Run on one file at a time because a single invocation of golint 
    38  # with multiple files requires the files to all be in one package.
    39  gofiles_with_warnings=()
    40  for gofile in $gofiles
    41  do
    42    errcount=$(golint $gofile | wc -l)
    43    if [ "$errcount" -gt "0" ]; then
    44      errors=YES
    45      echo "$errcount suggestions for:"
    46      echo "golint $gofile"
    47      gofiles_with_warnings+=($gofile)
    48    fi
    49  done
    50  
    51  [ -z "$errors" ] && exit 0
    52  
    53  # git doesn't give us access to user input, so let's steal it.
    54  exec < /dev/tty
    55  if [[ $? -eq 0 ]]; then
    56    # interactive shell. Prompt the user.
    57    echo
    58    echo "Lint suggestions were found. They're not enforced, but we're pausing"
    59    echo "to let you know before they get clobbered in the scrollback buffer."
    60    echo
    61    read -r -p 'Press enter to cancel, "s" to step through the warnings or type "ack" to continue: '
    62    if [ "$REPLY" = "ack" ]; then
    63      exit 0
    64    fi
    65    if [ "$REPLY" = "s" ]; then
    66      first_file="true"
    67      for gofile in "${gofiles_with_warnings[@]}"
    68      do
    69        echo
    70        if [ "$first_file" != "true" ]; then
    71  	echo "Press enter to show the warnings for the next file."
    72  	read
    73        fi
    74        golint $gofile
    75        first_file="false"
    76      done
    77    fi
    78  else
    79    # non-interactive shell (e.g. called from Eclipse). Just display the errors.
    80    for gofile in "${gofiles_with_warnings[@]}"
    81    do
    82      golint $gofile
    83    done
    84  fi
    85  exit 1