|
| 1 | +#!/usr/bin/env bash |
| 2 | +# Commit wrapper that constructs standards-compliant commit messages. |
| 3 | +# Resolves Co-Authored-By identities from docs/repository-standards.md. |
| 4 | + |
| 5 | +set -euo pipefail |
| 6 | + |
| 7 | +repo_root="$(git rev-parse --show-toplevel)" |
| 8 | + |
| 9 | +# --- Defaults --- |
| 10 | +type="" |
| 11 | +scope="" |
| 12 | +message="" |
| 13 | +body="" |
| 14 | +agent="" |
| 15 | + |
| 16 | +# --- Argument parsing --- |
| 17 | +usage() { |
| 18 | + cat >&2 <<'EOF' |
| 19 | +Usage: scripts/dev/commit.sh --type TYPE --message MESSAGE --agent AGENT [options] |
| 20 | +
|
| 21 | +Required: |
| 22 | + --type TYPE Conventional commit type (feat|fix|docs|style|refactor|test|chore|ci|build) |
| 23 | + --message MESSAGE Commit description (text after "type: ") |
| 24 | + --agent AGENT AI tool identity: claude or codex |
| 25 | +
|
| 26 | +Optional: |
| 27 | + --scope SCOPE Conventional commit scope |
| 28 | + --body BODY Detailed commit body |
| 29 | + -h, --help Show this help |
| 30 | +EOF |
| 31 | + exit 1 |
| 32 | +} |
| 33 | + |
| 34 | +while [[ $# -gt 0 ]]; do |
| 35 | + case "$1" in |
| 36 | + --type) type="$2"; shift 2 ;; |
| 37 | + --scope) scope="$2"; shift 2 ;; |
| 38 | + --message) message="$2"; shift 2 ;; |
| 39 | + --body) body="$2"; shift 2 ;; |
| 40 | + --agent) agent="$2"; shift 2 ;; |
| 41 | + -h|--help) usage ;; |
| 42 | + *) |
| 43 | + echo "ERROR: unknown argument: $1" >&2 |
| 44 | + usage |
| 45 | + ;; |
| 46 | + esac |
| 47 | +done |
| 48 | + |
| 49 | +# --- Validation --- |
| 50 | + |
| 51 | +# Required arguments. |
| 52 | +if [[ -z "$type" ]]; then |
| 53 | + echo "ERROR: --type is required." >&2 |
| 54 | + usage |
| 55 | +fi |
| 56 | +if [[ -z "$message" ]]; then |
| 57 | + echo "ERROR: --message is required." >&2 |
| 58 | + usage |
| 59 | +fi |
| 60 | +if [[ -z "$agent" ]]; then |
| 61 | + echo "ERROR: --agent is required." >&2 |
| 62 | + usage |
| 63 | +fi |
| 64 | + |
| 65 | +# Validate type. |
| 66 | +allowed_types="feat|fix|docs|style|refactor|test|chore|ci|build" |
| 67 | +if [[ ! "$type" =~ ^(feat|fix|docs|style|refactor|test|chore|ci|build)$ ]]; then |
| 68 | + echo "ERROR: invalid --type '$type'. Allowed: $allowed_types" >&2 |
| 69 | + exit 1 |
| 70 | +fi |
| 71 | + |
| 72 | +# Resolve agent identity from docs/repository-standards.md. |
| 73 | +profile_file="$repo_root/docs/repository-standards.md" |
| 74 | +if [[ ! -f "$profile_file" ]]; then |
| 75 | + echo "ERROR: repository profile not found at $profile_file" >&2 |
| 76 | + exit 1 |
| 77 | +fi |
| 78 | + |
| 79 | +# Look for the identity whose username contains -<agent> (e.g., -claude, -codex). |
| 80 | +identity="" |
| 81 | +while IFS= read -r line; do |
| 82 | + # Lines look like: - Co-Authored-By: name <email> |
| 83 | + stripped="${line#- }" |
| 84 | + if echo "$stripped" | grep -qi "\-${agent}[[:space:]]"; then |
| 85 | + identity="$stripped" |
| 86 | + break |
| 87 | + fi |
| 88 | +done < <(grep -i '^\- Co-Authored-By:' "$profile_file" || true) |
| 89 | + |
| 90 | +if [[ -z "$identity" ]]; then |
| 91 | + echo "ERROR: no approved identity found for agent '$agent' in $profile_file." >&2 |
| 92 | + echo "Approved identities are listed under 'AI co-authors'." >&2 |
| 93 | + exit 1 |
| 94 | +fi |
| 95 | + |
| 96 | +# Verify there are staged changes. |
| 97 | +if git diff --cached --quiet; then |
| 98 | + echo "ERROR: no staged changes. Stage files with 'git add' before committing." >&2 |
| 99 | + exit 1 |
| 100 | +fi |
| 101 | + |
| 102 | +# --- Construct commit message --- |
| 103 | +subject="$type" |
| 104 | +if [[ -n "$scope" ]]; then |
| 105 | + subject="${subject}(${scope})" |
| 106 | +fi |
| 107 | +subject="${subject}: ${message}" |
| 108 | + |
| 109 | +# Use a temporary file to preserve formatting (per AGENTS.md guidance). |
| 110 | +tmp_file="$(mktemp)" |
| 111 | +trap 'rm -f "$tmp_file"' EXIT |
| 112 | + |
| 113 | +printf '%s\n' "$subject" > "$tmp_file" |
| 114 | +if [[ -n "$body" ]]; then |
| 115 | + printf '\n%s\n' "$body" >> "$tmp_file" |
| 116 | +fi |
| 117 | +printf '\n%s\n' "$identity" >> "$tmp_file" |
| 118 | + |
| 119 | +# --- Commit --- |
| 120 | +git commit --file "$tmp_file" |
0 commit comments