1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146 | #!/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() {
# Print the header comment's Usage block: from "# Usage:" up to the first
# non-comment line, so a later "# Usage:" inside a function can't match.
awk '/^# Usage:/ { p = 1 } p && !/^#/ { exit } p { sub(/^# ?/, ""); print }' "$0"
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
|