aboutsummaryrefslogtreecommitdiffstats
authorJesse Armand <jesse@jessearmand.com>2026-08-22 16:15:08 +0800
committerJesse Armand <jesse@jessearmand.com>2026-08-22 16:15:08 +0800
commitbde7246d3445cc7aebf9110a88dbaa55bb72eae3 (patch)
treeb9dfc1b43593b7de689405e834602943ba8b1a4c
parent92122f4a597bbb33bead77b3380a9a95ff387e5d (diff)
downloaddgit-bde7246d34.tar.gz zip
feat(scripts): add dgit-repo admin CLI (private-by-default create)
Wraps the /config, /gc and DELETE admin endpoints. `create` registers a repository with private=true before the first push, so a new repository is never publicly readable. Host from `git config dgit.host`, credentials via `git credential fill`, so no token handling lives in the script. Upstream ignores scripts/* wholesale; un-ignore this one file. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_015rhJxS6WcjmMZFETjqLv1D
Diffstat
.gitignore+1-0
scripts/dgit-repo (new)+144-0
2 files changed, 145 insertions(+), 0 deletions(-)
diff --git a/.gitignore b/.gitignore
@@ -4,3 +4,4 @@
dist/
scripts/*
!scripts/gen-sha1dc-tables.mjs
+!scripts/dgit-repo
diff --git a/scripts/dgit-repo b/scripts/dgit-repo
@@ -0,0 +1,144 @@
+#!/usr/bin/env bash
+# dgit-repo — manage repositories on a dgit host from the command line.
+#
+# Repositories are private by default here: `create` registers the name with
+# private=true BEFORE the first push, so there is never a window in which a
+# freshly pushed repository is publicly readable.
+#
+# Host: $DGIT_HOST, else `git config dgit.host` (e.g. git.jessearmand.com)
+# Auth: `git credential fill` for that host — whatever credential helper
+# ~/.gitconfig has for https://<host> (e.g. git-credential-fnox).
+#
+# Usage:
+# dgit-repo create NAME [--public] [-d DESC] [-s SECTION] [-o OWNER]
+# dgit-repo config NAME [--private|--public] [-d DESC] [-s SECTION] [-o OWNER]
+# dgit-repo show NAME current description/owner/section/private
+# dgit-repo gc NAME prune unreachable objects
+# dgit-repo delete NAME [--yes] delete the repository (asks first)
+# dgit-repo list public index (private repos are hidden)
+set -euo pipefail
+
+die() { printf 'dgit-repo: %s\n' "$*" >&2; exit 1; }
+
+usage() {
+ sed -n '/^# Usage:/,/^set -euo/p' "$0" | sed '$d' | sed 's/^# \{0,1\}//'
+ exit "${1:-0}"
+}
+
+need() { command -v "$1" >/dev/null 2>&1 || die "missing dependency: $1"; }
+need curl; need jq; need git
+
+host=${DGIT_HOST:-$(git config --get dgit.host 2>/dev/null || true)}
+[ -n "$host" ] || die 'no host: set DGIT_HOST or `git config --global dgit.host <host>`'
+base="https://$host"
+
+# Resolve credentials through git so the token never appears in argv or in
+# this script: git's credential output is rewritten into a curl config line
+# (`user = "name:secret"`) and handed to curl on stdin via -K -.
+curl_auth() {
+ printf 'protocol=https\nhost=%s\n' "$host" \
+ | git credential fill \
+ | awk -F= '
+ $1 == "username" { u = substr($0, index($0, "=") + 1) }
+ $1 == "password" { p = substr($0, index($0, "=") + 1) }
+ END {
+ if (p == "") { exit 3 }
+ gsub(/"/, "\\\"", u); gsub(/"/, "\\\"", p)
+ printf "user = \"%s:%s\"\n", u, p
+ }' \
+ | curl -sS -K - "$@" \
+ || die "request failed (is a credential helper configured for https://$host?)"
+}
+
+# HTTP status of an authenticated GET on the repo page: 404 = does not exist.
+repo_status() {
+ curl_auth -o /dev/null -w '%{http_code}' "$base/$1/"
+}
+repo_exists() {
+ [ "$(repo_status "$1")" != 404 ]
+}
+
+# Build the config JSON from the flags that were actually given.
+# Usage: config_json [--private|--public] [-d DESC] [-s SECTION] [-o OWNER]
+config_json() {
+ local args=()
+ while [ $# -gt 0 ]; do
+ case $1 in
+ --private) args+=(--argjson private true) ;;
+ --public) args+=(--argjson private false) ;;
+ -d|--description) args+=(--arg description "$2"); shift ;;
+ -s|--section) args+=(--arg section "$2"); shift ;;
+ -o|--owner) args+=(--arg owner "$2"); shift ;;
+ *) die "unknown option: $1" ;;
+ esac
+ shift
+ done
+ jq -cn '$ARGS.named' "${args[@]+"${args[@]}"}"
+}
+
+put_config() {
+ local name=$1 body=$2
+ curl_auth --fail-with-body -X PUT -H 'content-type: application/json' \
+ -d "$body" "$base/$name/config" | jq .
+}
+
+cmd_create() {
+ [ $# -ge 1 ] || usage 1
+ local name=$1; shift
+ local visibility=--private
+ local rest=()
+ for a in "$@"; do
+ case $a in --public) visibility=--public ;; *) rest+=("$a") ;; esac
+ done
+ if repo_exists "$name"; then
+ die "$name already exists on $host (use \`config\` to change it)"
+ fi
+ put_config "$name" "$(config_json "$visibility" "${rest[@]+"${rest[@]}"}")"
+ printf '\ncreated %s (%s). Push with:\n git remote add dgit %s/%s.git && git push -u dgit main\n' \
+ "$name" "${visibility#--}" "$base" "$name" >&2
+}
+
+cmd_config() {
+ [ $# -ge 2 ] || usage 1
+ local name=$1; shift
+ repo_exists "$name" || die "no such repository: $name"
+ put_config "$name" "$(config_json "$@")"
+}
+
+cmd_show() {
+ [ $# -eq 1 ] || usage 1
+ repo_exists "$1" || die "no such repository: $1"
+ put_config "$1" '{}' # an empty PUT changes nothing and echoes the config
+}
+
+cmd_gc() {
+ [ $# -eq 1 ] || usage 1
+ repo_exists "$1" || die "no such repository: $1"
+ curl_auth --fail-with-body -X POST "$base/$1/gc"
+}
+
+cmd_delete() {
+ [ $# -ge 1 ] || usage 1
+ local name=$1 yes=no
+ [ "${2:-}" = --yes ] && yes=yes
+ repo_exists "$name" || die "no such repository: $name"
+ if [ "$yes" = no ]; then
+ printf 'Delete %s on %s? This cannot be undone. Type the name to confirm: ' "$name" "$host" >&2
+ read -r answer
+ [ "$answer" = "$name" ] || die "aborted"
+ fi
+ curl_auth --fail-with-body -X DELETE "$base/$name"
+}
+
+cmd_list() {
+ curl -sS --fail-with-body "$base/api/repos" \
+ | jq -r '.repos[] | "\(.name)\t\(.section)\t\(.description)"'
+}
+
+[ $# -ge 1 ] || usage 1
+cmd=$1; shift
+case $cmd in
+ create|config|show|gc|delete|list) "cmd_$cmd" "$@" ;;
+ -h|--help|help) usage ;;
+ *) die "unknown command: $cmd (try --help)" ;;
+esac