github.com/XiaoMi/Gaea@v1.2.5/misc/git/hooks/goimports (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 goimports 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  # This script does not handle file names that contain spaces.
    22  gofiles=$(git diff --cached --name-only --diff-filter=d | grep '.go$')
    23  
    24  [ -z "$gofiles" ] && exit 0
    25  unformatted=$(goimports -l=true $gofiles 2>&1 | awk -F: '{print $1}')
    26  [ -z "$unformatted" ] && exit 0
    27  
    28  # Some files are not goimports'd. Print message and fail.
    29  
    30  echo >&2 "Go files must be formatted with goimports. Please run:"
    31  echo >&2
    32  echo -n >&2 "  goimports -w"
    33  for fn in $unformatted; do
    34      echo -n >&2 " $PWD/$fn"
    35  done
    36  echo
    37  
    38  exit 1