/ test / lint / commit-script-check.sh
commit-script-check.sh
 1  #!/bin/sh
 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              (eval "$SCRIPT")
45              git --no-pager diff --exit-code "$commit" && echo "OK" >&2 || (echo "Failed" >&2; false) || RET=1
46          fi
47          git reset --quiet --hard HEAD
48       else
49          if git rev-list "--format=%b" -n1 "$commit" | grep -q '^-\(BEGIN\|END\)[ a-zA-Z]*-$'; then
50              echo "Error: script block marker but no scripted-diff in title of commit $commit" >&2
51              echo "Failed" >&2
52              RET=1
53          fi
54      fi
55  done
56  git checkout --quiet "$PREV_BRANCH" 2>/dev/null || git checkout --quiet "$PREV_HEAD"
57  exit $RET