|
| 1 | +#!/bin/sh |
| 2 | + |
| 3 | +DIR="$1" |
| 4 | +COMMIT="$2" |
| 5 | +if [ -z "$COMMIT" ]; then |
| 6 | + COMMIT=HEAD |
| 7 | +fi |
| 8 | + |
| 9 | +# Taken from git-subtree (Copyright (C) 2009 Avery Pennarun <[email protected]>) |
| 10 | +find_latest_squash() |
| 11 | +{ |
| 12 | + dir="$1" |
| 13 | + sq= |
| 14 | + main= |
| 15 | + sub= |
| 16 | + git log --grep="^git-subtree-dir: $dir/*\$" \ |
| 17 | + --pretty=format:'START %H%n%s%n%n%b%nEND%n' "$COMMIT" | |
| 18 | + while read a b junk; do |
| 19 | + case "$a" in |
| 20 | + START) sq="$b" ;; |
| 21 | + git-subtree-mainline:) main="$b" ;; |
| 22 | + git-subtree-split:) sub="$b" ;; |
| 23 | + END) |
| 24 | + if [ -n "$sub" ]; then |
| 25 | + if [ -n "$main" ]; then |
| 26 | + # a rejoin commit? |
| 27 | + # Pretend its sub was a squash. |
| 28 | + sq="$sub" |
| 29 | + fi |
| 30 | + echo "$sq" "$sub" |
| 31 | + break |
| 32 | + fi |
| 33 | + sq= |
| 34 | + main= |
| 35 | + sub= |
| 36 | + ;; |
| 37 | + esac |
| 38 | + done |
| 39 | +} |
| 40 | + |
| 41 | +latest_squash="$(find_latest_squash "$DIR")" |
| 42 | +if [ -z "$latest_squash" ]; then |
| 43 | + echo "ERROR: $DIR is not a subtree" >&2 |
| 44 | + exit 2 |
| 45 | +fi |
| 46 | + |
| 47 | +set $latest_squash |
| 48 | +old=$1 |
| 49 | +rev=$2 |
| 50 | +if [ "d$(git cat-file -t $rev 2>/dev/null)" != dcommit ]; then |
| 51 | + echo "ERROR: subtree commit $rev unavailable. Fetch/update the subtree repository" >&2 |
| 52 | + exit 2 |
| 53 | +fi |
| 54 | +tree_subtree=$(git show -s --format="%T" $rev) |
| 55 | +echo "$DIR in $COMMIT was last updated to upstream commit $rev (tree $tree_subtree)" |
| 56 | +tree_actual=$(git ls-tree -d "$COMMIT" "$DIR" | head -n 1) |
| 57 | +if [ -z "$tree_actual" ]; then |
| 58 | + echo "FAIL: subtree directory $DIR not found in $COMMIT" >&2 |
| 59 | + exit 1 |
| 60 | +fi |
| 61 | +set $tree_actual |
| 62 | +tree_actual_type=$2 |
| 63 | +tree_actual_tree=$3 |
| 64 | +echo "$DIR in $COMMIT currently refers to $tree_actual_type $tree_actual_tree" |
| 65 | +if [ "d$tree_actual_type" != "dtree" ]; then |
| 66 | + echo "FAIL: subtree directory $DIR is not a tree in $COMMIT" >&2 |
| 67 | + exit 1 |
| 68 | +fi |
| 69 | +if [ "$tree_actual_tree" != "$tree_subtree" ]; then |
| 70 | + git diff-tree $tree_actual_tree $tree_subtree >&2 |
| 71 | + echo "FAIL: subtree directory tree doesn't match subtree commit tree" >&2 |
| 72 | + exit 1 |
| 73 | +fi |
| 74 | +echo "GOOD" |
0 commit comments