|
| 1 | +#!/usr/bin/env bash |
| 2 | + |
| 3 | +# Validate that documentation is kept up to date with the sources being documented. |
| 4 | +# |
| 5 | +# This is a low-tech approach to ensure that documentation is validated when relevant sources |
| 6 | +# are changed. A markdown file can 'call out' the sources being referenced using an HTML comment. |
| 7 | +# For example: |
| 8 | +# |
| 9 | +# <!-- SOURCE-CHECKSUM pkg/spi/instance/* ABCDEF --> |
| 10 | +# |
| 11 | +# This instructs the script to compute a checksum of the files under pkg/spi/instance and check |
| 12 | +# that it matches ABCDEF. |
| 13 | +# |
| 14 | +# The goal is to remind developers to update documentation when code changes. Hashing the source |
| 15 | +# files undoubtedly raises false positives, but mitigates stale documentation. |
| 16 | + |
| 17 | +set -o errexit |
| 18 | +set -o nounset |
| 19 | + |
| 20 | +HERE="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" |
| 21 | +cd "$HERE/.." |
| 22 | + |
| 23 | +# Check all markdown files we own. |
| 24 | +for f in $(find . -name '*.md' -not -path './vendor/*') |
| 25 | +do |
| 26 | + # Look for the sentinel string. |
| 27 | + doc_source_link=$(grep '<!-- SOURCE-CHECKSUM' "$f" || true) |
| 28 | + if [ "$doc_source_link" == "" ] |
| 29 | + then |
| 30 | + continue |
| 31 | + fi |
| 32 | + |
| 33 | + echo -n "Validating source checksum in $f: " |
| 34 | + |
| 35 | + # Split the command arguments. |
| 36 | + IFS=' ' read -r -a link_cmd <<< "$doc_source_link" |
| 37 | + if [ "${link_cmd[1]}" != "SOURCE-CHECKSUM" -o "${#link_cmd[@]}" -lt 5 ] |
| 38 | + then |
| 39 | + echo "FAIL" |
| 40 | + echo "Invalid SOURCE-CHECKSUM command" |
| 41 | + echo "<!-- SOURCE-CHECKSUM file-pattern checksum -->" |
| 42 | + exit 1 |
| 43 | + fi |
| 44 | + |
| 45 | + file_pattern="${link_cmd[2]}" |
| 46 | + checksum="${link_cmd[3]}" |
| 47 | + |
| 48 | + # Use git to hash the source files specified, concatenating them to form a single hash string. |
| 49 | + hash=$(git hash-object $file_pattern | tr -d '\n') |
| 50 | + if [ "$hash" == "$checksum" ] |
| 51 | + then |
| 52 | + echo "PASS" |
| 53 | + else |
| 54 | + echo "FAIL" |
| 55 | + echo "Expected: $checksum" |
| 56 | + echo "Actual: $hash" |
| 57 | + exit 1 |
| 58 | + fi |
| 59 | +done |
0 commit comments