github.com/resonatecoop/id@v1.1.0-43/add_gofmt_hook.sh (about)

     1  #!/bin/sh
     2  
     3  # make sure gofmt is installed
     4  command -v gofmt >/dev/null 2>&1 || { echo >&2 "gofmt is required but it's not installed."; exit 1; }
     5  
     6  # (over)write the pre-commit hook
     7  cat > .git/hooks/pre-commit <<'EOF'
     8  #!/usr/bin/env bash
     9  declare -a source_files=()
    10  files=$(git diff-index --name-only HEAD)
    11  for file in $files; do
    12      if [ ! -f "${file}" ]; then
    13          continue
    14      fi
    15      if [[ "${file}" == *.rs ]]; then
    16          source_files+=("${file}")
    17      fi
    18  done
    19  
    20  if [ ${#source_files[@]} -ne 0 ]; then
    21      command -v gofmt >/dev/null 2>&1 || { echo >&2 "gofmt is not installed. Aborting."; exit 1; }
    22      $(command -v gofmt) -w ${source_files[@]} &
    23  fi
    24  wait
    25  if [ ${#source_files[@]} -ne 0 ]; then
    26      git add ${source_files[@]}
    27      echo "adjusted source formatting in ${source_files[@]}"
    28  else
    29      echo "no formatting was needed"
    30  fi
    31  EOF
    32  
    33  chmod +x .git/hooks/pre-commit
    34  
    35  echo "hook updated"