summaryrefslogtreecommitdiff
path: root/bin/git-sz-push
diff options
context:
space:
mode:
Diffstat (limited to 'bin/git-sz-push')
-rwxr-xr-xbin/git-sz-push81
1 files changed, 81 insertions, 0 deletions
diff --git a/bin/git-sz-push b/bin/git-sz-push
new file mode 100755
index 0000000..e5a9c62
--- /dev/null
+++ b/bin/git-sz-push
@@ -0,0 +1,81 @@
+#!/bin/bash
+# git sz-push [<remote>] -- push this branch's commit to <remote>'s master
+# (origin), signed with a YubiKey: one touch to sign, one to push.
+#
+# The git host takes a new master only at a commit an author signed, and every
+# host that fetches it checks the same; the commits under it need no signature,
+# since the newest one's covers them. So this signs the newest commit and
+# pushes, for a machine that has only this repo and the YubiKey git keys
+# (bin/yubikey-git). Commit as usual; push master with this.
+#
+# Signing amends HEAD with `--only`: nothing staged is folded in, hooks run as
+# usual, and the tree, message, author and parents stay; the hash and committer
+# date change. Fetches nothing: run `git fetch` first if master may have moved.
+#
+# SZ_PUSH_KEYS (signing key files) and SZ_PUSH_SIGNERS (public keys) are for
+# tests. Runs under macOS's /bin/bash 3.2.
+set -euo pipefail
+
+die() { >&2 echo "git sz-push: $*"; exit 1; }
+
+# This script's real directory, as bin/yubikey-git finds its own.
+resolve_repo() {
+ local src="$0" dir
+ while [[ -L "${src}" ]]; do
+ dir="$(cd "$(dirname "${src}")" && pwd)"
+ src="$(readlink "${src}")"
+ [[ "${src}" == /* ]] || src="${dir}/${src}"
+ done
+ cd "$(dirname "${src}")/.." && pwd
+}
+
+remote="${1:-origin}"
+pubs="${SZ_PUSH_SIGNERS:-$(resolve_repo)/config/ssh/yubikey-git/*.pub}"
+keys="${SZ_PUSH_KEYS:-${HOME}/.ssh/id_ed25519_sk_git_main_nfc ${HOME}/.ssh/id_ed25519_sk_git_backup_nfc ${HOME}/.ssh/id_ed25519_sk_git}"
+
+gd="$(git rev-parse --git-dir 2>/dev/null)" || die "not in a git repository"
+for f in rebase-merge rebase-apply MERGE_HEAD CHERRY_PICK_HEAD REVERT_HEAD; do
+ [[ ! -e "${gd}/${f}" ]] || die "a rebase, merge, cherry-pick or revert is in progress; finish it first"
+done
+branch="$(git symbolic-ref -q --short HEAD)" || die "HEAD is detached; check out a branch first"
+up="$(git rev-parse -q --verify "refs/remotes/${remote}/master")" || die "no ${remote}/master here: git fetch ${remote} first"
+
+if git merge-base --is-ancestor HEAD "${up}"; then
+ echo "git sz-push: nothing to push, ${remote}/master already has HEAD"
+ exit 0
+fi
+git merge-base --is-ancestor "${up}" HEAD || die "HEAD does not build on ${remote}/master: rebase onto it first"
+
+echo "git sz-push: to ${remote} master, $(git rev-list --count "${up}..HEAD") commit(s):"
+git --no-pager log --oneline "${up}..HEAD" | sed 's/^/ /'
+others="$(git branch --contains HEAD --format='%(refname:short)' | grep -vxF "${branch}" || true)"
+[[ -z "${others}" ]] || echo "git sz-push: also on ${others//$'\n'/, }, which will keep the unsigned commit"
+
+signers="$(mktemp)"
+trap 'rm -f "${signers}"' EXIT
+for p in ${pubs}; do
+ read -r t k c < "${p}" && echo "${c} namespaces=\"git\" ${t} ${k}"
+done > "${signers}"
+[[ -s "${signers}" ]] || die "no YubiKey public keys found (${pubs})"
+signed() {
+ [[ "$(git -c gpg.format=ssh -c gpg.ssh.allowedSignersFile="${signers}" log -1 --format=%G? HEAD 2>/dev/null)" == G ]]
+}
+
+if signed; then
+ echo "git sz-push: HEAD is already signed by a YubiKey"
+else
+ echo "git sz-push: touch your YubiKey to sign HEAD"
+ ok=""
+ for k in ${keys}; do
+ [[ -f "${k}" ]] || continue
+ if git -c gpg.format=ssh -c user.signingkey="${k}" commit -q --amend --only --no-edit --allow-empty -S; then
+ ok=1
+ break
+ fi
+ done
+ [[ -n "${ok}" ]] || die "no YubiKey signed (is one plugged in?); nothing was pushed"
+ signed || die "HEAD is signed, but not by a YubiKey this repo knows; nothing was pushed"
+fi
+
+echo "git sz-push: touch it again to push"
+git push "${remote}" HEAD:master