3d24925d6e
Plan simplification: dropped the broken middle elif heuristic that had unbalanced shell expression. Kept clean two-step fallback: .template suffix first, bare filename second. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
71 lines
1.9 KiB
Bash
71 lines
1.9 KiB
Bash
#!/usr/bin/env bash
|
|
# Reverse sync: show diff between consumer-file and brain template, optionally apply
|
|
#
|
|
# Usage: extract.sh --source=<consumer-path> --file=<relative-path> [--dry-run]
|
|
set -euo pipefail
|
|
|
|
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
|
|
source "$SCRIPT_DIR/lib/common.sh"
|
|
|
|
BRAIN_ROOT="${BRAIN_ROOT:-$(cd "$SCRIPT_DIR/.." && pwd)}"
|
|
|
|
source_dir=""
|
|
file=""
|
|
dry_run=0
|
|
|
|
while [ $# -gt 0 ]; do
|
|
case "$1" in
|
|
--source=*) source_dir="${1#--source=}" ;;
|
|
--file=*) file="${1#--file=}" ;;
|
|
--dry-run) dry_run=1 ;;
|
|
*) log_error "Unknown: $1"; exit 1 ;;
|
|
esac
|
|
shift
|
|
done
|
|
|
|
[ -n "$source_dir" ] || { log_error "--source required"; exit 1; }
|
|
[ -n "$file" ] || { log_error "--file required"; exit 1; }
|
|
|
|
consumer_file="$source_dir/$file"
|
|
[ -f "$consumer_file" ] || { log_error "Not found: $consumer_file"; exit 1; }
|
|
|
|
# Find brain template (try with and without .template suffix)
|
|
brain_file=""
|
|
if [ -f "$BRAIN_ROOT/project-files/$file.template" ]; then
|
|
brain_file="$BRAIN_ROOT/project-files/$file.template"
|
|
elif [ -f "$BRAIN_ROOT/project-files/$file" ]; then
|
|
brain_file="$BRAIN_ROOT/project-files/$file"
|
|
else
|
|
log_error "Brain template not found for: $file"
|
|
exit 1
|
|
fi
|
|
|
|
log_info "Comparing $consumer_file <-> $brain_file"
|
|
if diff -q "$brain_file" "$consumer_file" >/dev/null; then
|
|
log_info "Identical, no changes needed"
|
|
exit 0
|
|
fi
|
|
|
|
log_warn "Files differ:"
|
|
diff -u "$brain_file" "$consumer_file" || true
|
|
|
|
if [ "$dry_run" -eq 1 ]; then
|
|
exit 2
|
|
fi
|
|
|
|
# Interactive prompt
|
|
printf "Action [a]pply / [r]eject / [q]uit: " >&2
|
|
read -r action
|
|
case "$action" in
|
|
a|apply)
|
|
cp "$consumer_file" "$brain_file"
|
|
log_info "Applied. Don't forget to commit brain repo + bump version."
|
|
;;
|
|
r|reject)
|
|
log_info "Rejected. Brain template unchanged."
|
|
;;
|
|
*)
|
|
log_info "Quit. No changes."
|
|
;;
|
|
esac
|