#!/usr/bin/env bash # Writes the old and new blob of each changed non-test Go file between two # commits under /old/ and /new/, for the spec trace's # kind DOCS (objects only; nothing from the PR is built or run). # # usage: changed-go-blobs.sh # # The paths come from the PR's tree objects, which git does not check when it # prints a diff. A path that is absolute or has an empty, '.' or '..' # component would write outside , so it is refused before anything is # written (exit 1). set -euo pipefail mb=$1 head=$2 out=$3 prefix=$(git rev-parse --show-prefix) mkdir -p "$out" git diff --relative --name-only --no-renames -z "$mb" "$head" -- "*.go" ":(exclude)*_test.go" >"$out/.paths" while IFS= read -r -d "" p; do case "/$p/" in *//* | */./* | */../*) printf 'changed-go-blobs: refusing path %q\n' "$p" >&2 exit 1 ;; esac done <"$out/.paths" while IFS= read -r -d "" p; do mkdir -p "$out/old/$(dirname "$p")" "$out/new/$(dirname "$p")" git show "$mb:$prefix$p" >"$out/old/$p" 2>/dev/null || rm -f "$out/old/$p" git show "$head:$prefix$p" >"$out/new/$p" 2>/dev/null || rm -f "$out/new/$p" done <"$out/.paths" rm -f "$out/.paths"