github.com/unigraph-dev/dgraph@v1.1.1-0.20200923154953-8b52b426f765/contrib/hooks/golint.sh (about)

     1  #!/bin/bash
     2  
     3  if [ -z "$GOPATH" ]; then
     4  	echo "ERROR: pre-commit hook for golint: \$GOPATH is empty."
     5  	exit 1
     6  fi
     7  
     8  if [ -z "$(which golint)" ]; then
     9  	echo "golint not found, please run: go get github.com/golang/lint/golint"
    10  	exit 1
    11  fi
    12  
    13  # This script does not handle file names that contain spaces.
    14  gofiles=$(git diff --cached --name-only --diff-filter=ACM | grep '\.go$' | grep -v '^vendor/')
    15  
    16  errors=
    17  
    18  # Run on one file at a time because a single invocation of golint
    19  # with multiple files requires the files to all be in one package.
    20  gofiles_with_warnings=()
    21  echo -e "\033[32m Running golint on staged files. You can either acknowledge the warnings or step through them.\033[0m"
    22  for gofile in $gofiles
    23  do
    24  	errcount=$(golint $gofile | wc -l)
    25  	if [ "$errcount" -gt "0" ]; then
    26  		errors=YES
    27  		echo "$errcount suggestions for:"
    28  		echo "golint $gofile"
    29  		gofiles_with_warnings+=($gofile)
    30  	fi
    31  done
    32  
    33  [ -z "$errors" ] && exit 0
    34  
    35  # git doesn't give us access to user input, so let's steal it.
    36  exec < /dev/tty
    37  if [[ $? -eq 0 ]]; then
    38  	# interactive shell. Prompt the user.
    39  	echo
    40  	echo "Lint suggestions were found. They're not enforced, but we're pausing"
    41  	echo "to let you know before they get clobbered in the scrollback buffer."
    42  	echo
    43  	read -r -p 'Press enter to cancel, "s" to step through the warnings or type "ack" to continue: '
    44  	if [ "$REPLY" = "ack" ]; then
    45  		exit 0
    46  	fi
    47  	if [ "$REPLY" = "s" ]; then
    48  		first_file="true"
    49  		for gofile in "${gofiles_with_warnings[@]}"
    50  		do
    51  			echo
    52  			if [ "$first_file" != "true" ]; then
    53  				echo "Press enter to show the warnings for the next file."
    54  				read
    55  			fi
    56  			golint $gofile
    57  			first_file="false"
    58  		done
    59  	fi
    60  else
    61  	# non-interactive shell (e.g. called from Eclipse). Just display the errors.
    62  	for gofile in "${gofiles_with_warnings[@]}"
    63  	do
    64  		golint $gofile
    65  	done
    66  fi
    67  exit 1
    68  
    69