#!/bin/sh
# Cuts a delivery from work, refusing while an earlier one is unsettled.
#
# An unsettled patch means gold is behind what Chris committed, so a
# patch cut now would carry hunks his tree already has and would fail to
# apply. This refuses rather than cutting such a patch.
#
# cut_patch.sh a-page-that-fronts-the-news
#
# Writes the patch to the outputs folder, records it in the ledger as
# delivered, and dry-runs it against a fresh clone of gold checked out at
# confirmed-head.
WORK=${WORK:-/home/claude/work}
GOLD=${GOLD:-/home/claude/gold}
OUT=${OUT:-/mnt/user-data/outputs}
LEDGER=${LEDGER:-/home/claude/gold_ledger.txt}
touch "$LEDGER"
if [ -z "$1" ]; then
echo "cut: name the patch for what it does"
exit 1
fi
waiting=`grep -c ' delivered$' "$LEDGER"`
if [ "$waiting" -gt 0 ]; then
echo "cut: refused; an earlier patch is still waiting to be settled:"
grep ' delivered$' "$LEDGER"
exit 1
fi
patch="$OUT/$1.patch"
cd "$WORK" || exit 1
git diff confirmed-head..HEAD > "$patch"
if [ ! -s "$patch" ]; then
echo "cut: nothing to deliver"
rm -f "$patch"
exit 1
fi
rm -rf /tmp/cut_check
git clone -q "$GOLD" /tmp/cut_check
cd /tmp/cut_check || exit 1
git checkout -q confirmed-head 2> /dev/null
echo "clone sits at: `git log -1 --format='%h %s'`"
if git apply --check "$patch"; then
echo "apply check: PASS"
echo "$patch $1 delivered" >> "$LEDGER"
else
echo "apply check: FAIL; not recorded as delivered"
fi
rm -rf /tmp/cut_check