#!/bin/sh# Commit through Git or Jujutsu and append reproducible machine metadata.set -eu
usage(){command_name=${VCS_COMMIT_COMMAND:-vcs-commit}case"$command_name"in git-commit) cat <<'EOF'Usage: git-commit [--add] [--push] [--dry-run] [--] MESSAGE...Commit the staged Git changes with ISO date and hostname trailers.With no MESSAGE, the (multi-line) message is read from stdin: type or paste it,then press Ctrl-D to finish. A piped message works too. --add run `git add -A` before committing --push run `git push` after a successful commit --dry-run show the message and actions without changing the repositoryExamples: git-commit "feat: add hostname to audit output" git-commit --add "docs: refresh usage notes" git-commit --add --push "chore: publish workstation update"EOF ;;
jj-commit) cat <<'EOF'Usage: jj-commit [--push] [--dry-run] [--] MESSAGE...Commit the Jujutsu working-copy change with ISO date and hostname trailers.With no MESSAGE, the (multi-line) message is read from stdin: type or paste it,then press Ctrl-D to finish. A piped message works too. --push run `jj git push --change @-` after a successful commit --dry-run show the message and actions without changing the repositoryExamples: jj-commit "feat: add hostname to audit output" jj-commit --push "chore: publish workstation update"EOF ;;
*) cat <<'EOF'Usage: vcs-commit {git|jj} [OPTIONS] [--] MESSAGE...Friendly entry points: git-commit [--add] [--push] MESSAGE... jj-commit [--push] MESSAGE...EOF ;;
esac}die(){printf'vcs-commit: %s\n'"$*" >&2exit2}iso_timestamp(){iftimestamp=$(date --iso-8601=seconds 2>/dev/null); thenprintf'%s\n'"$timestamp"returnfitimestamp=$(date '+%Y-%m-%dT%H:%M:%S%z')case"$timestamp"in *[+-][0-9][0-9][0-9][0-9])timestamp_hours=${timestamp%??}timestamp_minutes=${timestamp#"$timestamp_hours"}timestamp=${timestamp_hours}:${timestamp_minutes} ;;
esacprintf'%s\n'"$timestamp"}mode=${1:-}case"$mode"in git|jj)shift ;;
-h|--help|'') usage; exit0 ;;
*) die "first argument must be 'git' or 'jj'" ;;
esacadd_all=0push_after=0dry_run=0while["$#" -gt 0]; docase"$1"in --add|-A)add_all=1; shift ;;
--push|-p)push_after=1; shift ;;
--dry-run|-n)dry_run=1; shift ;;
-h|--help) usage; exit0 ;;
--) shift; break ;;
-*) die "unknown option: $1 (use -- before a message beginning with '-')" ;;
*)break ;;
esacdone["$mode"= git ]||["$add_all" -eq 0]|| die "--add is Git-only; JJ snapshots its working copy automatically"newline=''if["$#" -gt 0]; thenmessage=$*elif[ -t 0]; then# No message on the command line: read the (multi-line) message from the# terminal, the way `gpg` does when run on its own, ending it with Ctrl-D.## Read one chunk per read(2) rather than slurping with `cat`, because the# terminal is in canonical mode: it hands over at most one line per read, and# a Ctrl-D pressed part-way along a line delivers that partial line WITHOUT a# trailing newline instead of signalling EOF. `cat` cannot tell that apart# from an ordinary line and keeps waiting, which is why a single Ctrl-D used# to look like it did nothing. A newline-less chunk therefore means "the user# pressed Ctrl-D here", so one press ends the message wherever the cursor is;# a zero-byte read is a real EOF (Ctrl-D on an empty line) and ends it too.printf'commit message (Ctrl-D to finish, Ctrl-C to cancel):\n' >&2message=''while :; do# The trailing X survives command substitution stripping the newline we# need to test for; dd does a single read and never refills a short block.chunk=$(dd bs=65536count=12>/dev/null; printf'X')chunk=${chunk%X}[ -n "$chunk"]||breakmessage=$message$chunkcase"$chunk"in *"$newline") ;;
*)break ;;
esacdone# Drop the newline left by the closing Enter, so the trailers are separated# from the message by exactly one blank line -- what $(cat) used to do.while :; docase"$message"in *"$newline")message=${message%"$newline"} ;;
*)break ;;
esacdoneelse# Piped in: `printf '%s\n' "$msg" | jj-commit`. No terminal semantics apply,# so take the whole stream.message=$(cat)fi# Reject an empty or whitespace-only message rather than recording a blank one.case$(printf'%s'"$message" | tr -d '[:space:]')in'') die "a commit message is required" ;;
esaccommit_date=${VCS_COMMIT_DATE:-$(iso_timestamp)}commit_host=${VCS_COMMIT_HOST:-$(hostname 2>/dev/null || uname -n)}trailers=$(printf'Commit-Date: %s\nCommit-Host: %s'"$commit_date""$commit_host")full_message=$(printf'%s\n\n%s'"$message""$trailers")if["$dry_run" -eq 1]; thenprintf'Commit message:\n%s\n\nActions:\n'"$full_message"if["$mode"= git ]; then["$add_all" -eq 0]||printf' git add -A\n'printf' git commit\n'["$push_after" -eq 0]||printf' git push\n'elseprintf' jj commit\n'["$push_after" -eq 0]||printf' jj git push --change @-\n'fiexit0fiif["$mode"= git ]; thencommand -v git >/dev/null 2>&1|| die "git is not installed" git rev-parse --git-dir >/dev/null 2>&1|| die "not inside a Git repository"["$add_all" -eq 0]|| git add -A
git commit -m "$message" -m "$trailers"["$push_after" -eq 0]|| git push
elsecommand -v jj >/dev/null 2>&1|| die "jj is not installed" jj root >/dev/null 2>&1|| die "not inside a Jujutsu repository" jj commit -m "$full_message"["$push_after" -eq 0]|| jj git push --change @-
fi