| 1 | #!/usr/bin/env bash |
| 2 | # |
| 3 | # Check that commits after a specified point do not contain new or modified |
| 4 | # lines with whitespace errors. An optional formatted summary can be generated |
| 5 | # by providing an output file path and url as additional arguments. |
| 6 | # |
| 7 | |
| 8 | baseCommit=$1 |
| 9 | outputFile=$2 |
| 10 | url=$3 |
| 11 | |
| 12 | if test "$#" -ne 1 && test "$#" -ne 3 || test -z "$1" |
| 13 | then |
| 14 | echo "USAGE: $0 <BASE_COMMIT> [<OUTPUT_FILE> <URL>]" |
| 15 | exit 1 |
| 16 | fi |
| 17 | |
| 18 | problems=() |
| 19 | commit= |
| 20 | commitText= |
| 21 | commitTextmd= |
| 22 | goodParent= |
| 23 | |
| 24 | if ! git rev-parse --quiet --verify "${baseCommit}" |
| 25 | then |
| 26 | echo "Invalid <BASE_COMMIT> '${baseCommit}'" |
| 27 | exit 1 |
| 28 | fi |
| 29 | |
| 30 | while read dash sha etc |
| 31 | do |
| 32 | case "${dash}" in |
| 33 | "---") # Line contains commit information. |
| 34 | if test -z "${goodParent}" |
| 35 | then |
| 36 | # Assume the commit has no whitespace errors until detected otherwise. |
| 37 | goodParent=${sha} |
| 38 | fi |
| 39 | |
| 40 | commit="${sha}" |
| 41 | commitText="${sha} ${etc}" |
| 42 | commitTextmd="[${sha}](${url}/commit/${sha}) ${etc}" |
| 43 | ;; |
| 44 | "") |
| 45 | ;; |
| 46 | *) # Line contains whitespace error information for current commit. |
| 47 | if test -n "${goodParent}" |
| 48 | then |
| 49 | problems+=("1) --- ${commitTextmd}") |
| 50 | echo "" |
| 51 | echo "--- ${commitText}" |
| 52 | goodParent= |
| 53 | fi |
| 54 | |
| 55 | case "${dash}" in |
| 56 | *:[1-9]*:) # contains file and line number information |
| 57 | dashend=${dash#*:} |
| 58 | problems+=("[${dash}](${url}/blob/${commit}/${dash%%:*}#L${dashend%:}) ${sha} ${etc}") |
| 59 | ;; |
| 60 | *) |
| 61 | problems+=("\`${dash} ${sha} ${etc}\`") |
| 62 | ;; |
| 63 | esac |
| 64 | echo "${dash} ${sha} ${etc}" |
| 65 | ;; |
| 66 | esac |
| 67 | done <<< "$(git log --check --pretty=format:"---% h% s" "${baseCommit}"..)" |
| 68 | |
| 69 | if test ${#problems[*]} -gt 0 |
| 70 | then |
| 71 | if test -z "${goodParent}" |
| 72 | then |
| 73 | goodParent=${baseCommit: 0:7} |
| 74 | fi |
| 75 | |
| 76 | echo "A whitespace issue was found in one or more of the commits." |
| 77 | echo "Run the following command to resolve whitespace issues:" |
| 78 | echo "git rebase --whitespace=fix ${goodParent}" |
| 79 | |
| 80 | # If target output file is provided, write formatted output. |
| 81 | if test -n "$outputFile" |
| 82 | then |
| 83 | echo "🛑 Please review the Summary output for further information." |
| 84 | ( |
| 85 | echo "### :x: A whitespace issue was found in one or more of the commits." |
| 86 | echo "" |
| 87 | echo "Run these commands to correct the problem:" |
| 88 | echo "1. \`git rebase --whitespace=fix ${goodParent}\`" |
| 89 | echo "1. \`git push --force\`" |
| 90 | echo "" |
| 91 | echo "Errors:" |
| 92 | |
| 93 | for i in "${problems[@]}" |
| 94 | do |
| 95 | echo "${i}" |
| 96 | done |
| 97 | ) >"$outputFile" |
| 98 | fi |
| 99 | |
| 100 | exit 2 |
| 101 | fi |