commit-script-check.sh raw

   1  #!/usr/bin/env bash
   2  # Copyright (c) 2017-present The Bitcoin Core developers
   3  # Distributed under the MIT software license, see the accompanying
   4  # file COPYING or http://www.opensource.org/licenses/mit-license.php.
   5  
   6  # This simple script checks for commits beginning with: scripted-diff:
   7  # If found, looks for a script between the lines -BEGIN VERIFY SCRIPT- and
   8  # -END VERIFY SCRIPT-. If no ending is found, it reads until the end of the
   9  # commit message.
  10  
  11  # The resulting script should exactly transform the previous commit into the current
  12  # one. Any remaining diff signals an error.
  13  
  14  export LC_ALL=C
  15  if test -z "$1"; then
  16      echo "Usage: $0 <commit>..."
  17      exit 1
  18  fi
  19  
  20  if ! sed --help 2>&1 | grep -q 'GNU'; then
  21      echo "Error: the installed sed package is not compatible. Please make sure you have GNU sed installed in your system.";
  22      exit 1;
  23  fi
  24  
  25  if ! grep --help 2>&1 | grep -q 'GNU'; then
  26      echo "Error: the installed grep package is not compatible. Please make sure you have GNU grep installed in your system.";
  27      exit 1;
  28  fi
  29  
  30  RET=0
  31  PREV_BRANCH=$(git name-rev --name-only HEAD)
  32  PREV_HEAD=$(git rev-parse HEAD)
  33  for commit in $(git rev-list --reverse "$1"); do
  34      if git rev-list -n 1 --pretty="%s" "$commit" | grep -q "^scripted-diff:"; then
  35          git checkout --quiet "$commit"^ || exit
  36          SCRIPT="$(git rev-list --format=%b -n1 "$commit" | sed '/^-BEGIN VERIFY SCRIPT-$/,/^-END VERIFY SCRIPT-$/{//!b};d')"
  37          if test -z "$SCRIPT"; then
  38              echo "Error: missing script for: $commit" >&2
  39              echo "Failed" >&2
  40              RET=1
  41          else
  42              echo "Running script for: $commit" >&2
  43              echo "$SCRIPT" >&2
  44              if bash -o errexit -o nounset -o pipefail -c "$SCRIPT" && git --no-pager diff --exit-code "$commit"; then
  45                  echo "OK" >&2
  46              else
  47                  echo "Failed" >&2
  48                  RET=1
  49              fi
  50          fi
  51          git reset --quiet --hard HEAD
  52       else
  53          if git rev-list "--format=%b" -n1 "$commit" | grep -q '^-\(BEGIN\|END\)[ a-zA-Z]*-$'; then
  54              echo "Error: script block marker but no scripted-diff in title of commit $commit" >&2
  55              echo "Failed" >&2
  56              RET=1
  57          fi
  58      fi
  59  done
  60  git checkout --quiet "$PREV_BRANCH" 2>/dev/null || git checkout --quiet "$PREV_HEAD"
  61  exit $RET
  62