Review note script
This commit is contained in:
Executable
+70
@@ -0,0 +1,70 @@
|
||||
#!/usr/bin/env bash
|
||||
# review-note: manage code-review notes under refs/notes/review.
|
||||
# The protocol lives in REVIEW.md.
|
||||
set -euo pipefail
|
||||
|
||||
NOTES_REF=refs/notes/review
|
||||
REMOTE=origin
|
||||
|
||||
die() { echo "review-note: $*" >&2; exit 1; }
|
||||
|
||||
usage() {
|
||||
cat >&2 <<'EOF'
|
||||
usage:
|
||||
review-note show [commit]
|
||||
review-note write <file> [--force]
|
||||
review-note latest [branch]
|
||||
review-note push
|
||||
EOF
|
||||
exit 1
|
||||
}
|
||||
|
||||
validate() {
|
||||
local file="$1"
|
||||
grep -q '^branch: .\+' "$file" || die "missing or empty 'branch:' header"
|
||||
grep -Eq '^round: [0-9]+$' "$file" || die "missing or invalid 'round:' header"
|
||||
grep -Eq '^verdict: (approve|request-changes)$' "$file" \
|
||||
|| die "verdict must be 'approve' or 'request-changes'"
|
||||
}
|
||||
|
||||
cmd_show() {
|
||||
local commit="${1:-HEAD}"
|
||||
git notes --ref="$NOTES_REF" show "$commit" 2>/dev/null \
|
||||
|| die "no review note for $commit"
|
||||
}
|
||||
|
||||
cmd_write() {
|
||||
local force="" file=""
|
||||
local arg
|
||||
for arg in "$@"; do
|
||||
case "$arg" in
|
||||
--force) force=1 ;;
|
||||
*) file="$arg" ;;
|
||||
esac
|
||||
done
|
||||
[ -n "$file" ] || usage
|
||||
[ -f "$file" ] || die "no such file: $file"
|
||||
local tip
|
||||
tip=$(git rev-parse HEAD)
|
||||
if [ -z "$force" ] && git notes --ref="$NOTES_REF" show "$tip" >/dev/null 2>&1; then
|
||||
die "tip $tip already has a review note; pass --force to replace"
|
||||
fi
|
||||
validate "$file"
|
||||
if [ -n "$force" ]; then
|
||||
git notes --ref="$NOTES_REF" add -f -F "$file" "$tip"
|
||||
else
|
||||
git notes --ref="$NOTES_REF" add -F "$file" "$tip"
|
||||
fi
|
||||
}
|
||||
|
||||
main() {
|
||||
local cmd="${1:-}"
|
||||
if [ $# -gt 0 ]; then shift; fi
|
||||
case "$cmd" in
|
||||
show) cmd_show "$@" ;;
|
||||
write) cmd_write "$@" ;;
|
||||
*) usage ;;
|
||||
esac
|
||||
}
|
||||
|
||||
main "$@"
|
||||
Reference in New Issue
Block a user