repositories

NixDaemon

NixOS pentest workstation as one flake — IceBreaker's successor

clonegit clone https://git.daemon-sec.xyz/NixDaemon.git
vcs-commit180 lines5.6 KiBraw download
#!/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 repository

Examples:
  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 repository

Examples:
  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' "$*" >&2
  exit 2
}

iso_timestamp() {
  if timestamp=$(date --iso-8601=seconds 2>/dev/null); then
    printf '%s\n' "$timestamp"
    return
  fi

  timestamp=$(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}
      ;;
  esac
  printf '%s\n' "$timestamp"
}

mode=${1:-}
case "$mode" in
  git|jj) shift ;;
  -h|--help|'') usage; exit 0 ;;
  *) die "first argument must be 'git' or 'jj'" ;;
esac

add_all=0
push_after=0
dry_run=0
while [ "$#" -gt 0 ]; do
  case "$1" in
    --add|-A) add_all=1; shift ;;
    --push|-p) push_after=1; shift ;;
    --dry-run|-n) dry_run=1; shift ;;
    -h|--help) usage; exit 0 ;;
    --) shift; break ;;
    -*) die "unknown option: $1 (use -- before a message beginning with '-')" ;;
    *) break ;;
  esac
done

[ "$mode" = git ] || [ "$add_all" -eq 0 ] || die "--add is Git-only; JJ snapshots its working copy automatically"

newline='
'

if [ "$#" -gt 0 ]; then
  message=$*
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' >&2
  message=''
  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=65536 count=1 2>/dev/null; printf 'X')
    chunk=${chunk%X}
    [ -n "$chunk" ] || break
    message=$message$chunk
    case "$chunk" in
      *"$newline") ;;
      *) break ;;
    esac
  done
  # 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 :; do
    case "$message" in
      *"$newline") message=${message%"$newline"} ;;
      *) break ;;
    esac
  done
else
  # 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" ;;
esac
commit_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 ]; then
  printf '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'
  else
    printf '  jj commit\n'
    [ "$push_after" -eq 0 ] || printf '  jj git push --change @-\n'
  fi
  exit 0
fi

if [ "$mode" = git ]; then
  command -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
else
  command -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