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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
|
# claude-inbox, for the host: which of the repositories that the claude
# VM has (~claude/src, but for symlinks, i.e. third parties' in ~/tmp/src)
# need attention, i.e. have branches there with something master
# doesn't have (by content, so rebased ones count as merged), whose
# (a remote's, by the branch's prefix, or $LOGNAME's, else $USER's,
# without one), and what to do about them. Goes by each repository's
# own checkout here, in $CLAUDE_INBOX_SRC (default ~/sync/src), with its
# remote claude.
{ lib, runCommand, writeShellScript, writeText
, coreutils, findutils, gawk, git, gnugrep, gnused, openssh, util-linux
}:
let
script = writeShellScript "claude-inbox" ''
set -eu
export PATH=${lib.makeBinPath [ coreutils findutils gawk git gnugrep gnused openssh util-linux ]}:$PATH
usage() {
cat <<'EOF'
usage: claude-inbox [REPO] [BRANCH] [COMMAND] [OPTION...]
(in any order)
What among the claude VM's repositories needs attention, and doing it.
Commands:
--list [REPO] [--by-remote]
The default without BRANCH. Fetch the repositories the VM has
(ssh HOST: its ~/src, but for symlinks), or just REPO, and show
their branches with something master doesn't have (by content),
those it has all of (to delete), and whether master needs
pushing. --by-remote: grouped by OWNER.
--show [REPO|BRANCH] [--diff[=WHAT]] [--git=ARGS]...
The default with BRANCH. Fetch REPO (or else all) first, show
master..BRANCH, with patches, and mark BRANCH seen. Without
BRANCH: REPO's branches, as a graph.
--diff: the diff to BRANCH from WHAT, a branch or commit
(default: HEAD).
--git: more arguments for git log (or git diff), split at
spaces, e.g. --git=-p --git='-n 1', or --git='-p -n 1'.
--take BRANCH [-f|--force|--pick]
Fast-forward master to BRANCH, as last fetched (i.e. as shown),
and push it to claude. master: checked out, or nowhere (not in
another worktree).
--force: rename master to abandoned/master-DATE, make BRANCH
master (with the old one's upstream), push it like --push -f.
--pick: put the commits of BRANCH that master doesn't have yet
(by content) on top of it, one by one, and push it; on a
conflict, it stops there: resolve, git cherry-pick --continue,
then --push; or git cherry-pick --abort. Needs master checked
out.
--checkout=BRANCH (or --checkout BRANCH)
Fetch REPO first, then check out BRANCH there, detached (no
local branch). Only with a clean working tree: nothing
changed, nothing untracked, no merge, rebase, cherry-pick or
revert going on.
--stack=BRANCH (or --stack BRANCH)
Fetch REPO first, then put the commits of BRANCH that HEAD
doesn't have yet (by content) on top of HEAD, one by one,
detached (no branch moves, nothing gets pushed): to try several
branches at once, one --stack after the other (e.g. after
--checkout of the first). On a conflict, it stops there:
resolve, git cherry-pick --continue; or git cherry-pick --abort.
Only with a clean working tree, as --checkout.
--push [REPO] [-f|--force]
Push master to claude's master (and make that master's
upstream, if it has none).
--force: for a rewritten master, but only while claude's is the
one last fetched (git push --force-with-lease).
Words:
REPO a repository in DIR, with a remote claude; where a command
needs one and gets none, the one the current directory is
in
BRANCH a branch of claude's there; without REPO, in the one
repository that has it (as last fetched)
A word that could be either is what makes sense of all the words,
if exactly one reading does.
Options:
-d DIR the repositories ($CLAUDE_INBOX_SRC, or ~/sync/src)
--vm HOST the VM ($CLAUDE_INBOX_VM, or claude)
--no-fetch don't fetch (--list, --show)
-h, --help this
The list:
OWNER the remote, by the branch's prefix (?: no such remote), or
$LOGNAME (else $USER), without one
STATE review new, and master is in it
rebase master moved on: the remote's turn
done master has it all: to delete
yours $LOGNAME's own
* changed since --show or --take
NEW its commits that master doesn't have, by content
BEHIND master's commits that it doesn't have
AGE of its last commit
master push: master is ahead of claude's; VM-ahead!: claude's has
what master doesn't
EOF
}
# colors, if FD (1 or 2) is a terminal, as completion has them:
# commands, options, REPO, BRANCH, and bold for headings; none with
# NO_COLOR, or a dumb one
colors() {
C_CMD= C_OPT= C_REPO= C_BRANCH= C_DIM= C_RED= C_HEAD= C_OFF=
test -t "$1" && test -z "''${NO_COLOR-}" || return 0
case ''${TERM-dumb} in dumb | "") return 0 ;; esac
local n p
case ''${COLORTERM-} in
truecolor | 24bit) p="38;2;230;180;80 38;2;150;150;175 38;2;100;160;240 38;2;120;200;120 38;2;125;125;125 38;2;235;95;85" ;;
*)
case $TERM in
*256color*) n=256 ;;
*) n=$(tput colors 2>/dev/null) || n=8 ;;
esac
if test "''${n:-0}" -ge 256; then p="38;5;179 38;5;103 38;5;75 38;5;114 38;5;244 38;5;203"
elif test "''${n:-0}" -ge 8; then p="33 35 34 32 2 31"
else return 0
fi ;;
esac
set -- $p
C_CMD=$'\e['$1m C_OPT=$'\e['$2m C_REPO=$'\e['$3m C_BRANCH=$'\e['$4m C_OFF=$'\e[0m'
C_DIM=$'\e['$5m C_RED=$'\e['$6m
C_HEAD=$'\e[1m'
}
# the headings (bold), commands, options, REPO and BRANCH in a text
# (the help, an error), in their colors, if any (a colored word
# follows an m, so each loop ends)
paint() {
if test -z "$C_OFF"; then cat; return; fi
local x='[^-A-Za-z0-9_]'
sed -E \
-e "s/^([A-Z][A-Za-z ]*:)$/$C_HEAD\1$C_OFF/" \
-e ":a; s/(^|$x)(--(list|show|take|push|checkout|stack)|COMMAND)($x|$)/\1$C_CMD\2$C_OFF\4/; ta" \
-e ":b; s/(^|$x)(-d|--vm|--no-fetch|--by-remote|-f|--force|--pick|--diff|--git|-h|--help|OPTION)($x|$)/\1$C_OPT\2$C_OFF\3/; tb" \
-e ":c; s/(^|$x)(REPO)($x|$)/\1$C_REPO\2$C_OFF\3/; tc" \
-e ":d; s/(^|$x)(BRANCH)($x|$)/\1$C_BRANCH\2$C_OFF\3/; td"
}
# --list's rows, in color, if any: by whose turn it is, its STATE in
# the state's color (review, push, VM-ahead!: bold; yours, done,
# rebase: not), and the rest of the row in it, too, a bit muted
# (without the bold, or faint); * in bold
paint_list() {
if test -z "$C_OFF"; then cat; return; fi
awk -v h="$C_HEAD" -v o="$C_OFF" -v amber="$C_CMD" -v blue="$C_REPO" \
-v green="$C_BRANCH" -v dim="$C_DIM" -v red="$C_RED" -v faint=$'\e[2m' '
NR == 1 { print h $0 o; next }
{
s = $4; star = sub(/\*$/, "", s)
if (s == "review" || s == "push") { c = h amber; m = amber }
else if (s == "VM-ahead!") { c = h red; m = red }
else if (s == "yours") { c = blue; m = faint blue }
else if (s == "done") { c = green; m = faint green }
else { c = dim; m = faint dim }
# field by field, the spaces between them kept (column -t)
line = $0; out = ""; n = 0
while (match(line, /[^ ]+/)) {
n++
pre = substr(line, 1, RSTART - 1); t = substr(line, RSTART, RLENGTH)
line = substr(line, RSTART + RLENGTH)
t = n == 4 ? c s o (star ? h "*" o : "") : m t o
out = out pre t
}
print out line
}'
}
# an error, and the REPO and BRANCH it's about, if known, in color
die() {
local m="$*"
colors 2
if test -n "$C_OFF"; then
if test -n "''${name-}" && test "''${m#"$name: "}" != "$m"; then
m="$C_REPO$name$C_OFF: ''${m#"$name: "}"
fi
if test -n "''${b-}"; then
m=''${m//"$b"/"$C_BRANCH$b$C_OFF"}
fi
fi
printf 'claude-inbox: %s\n' "$m" | paint >&2
exit 1
}
src=''${CLAUDE_INBOX_SRC:-$HOME/sync/src}
vm=''${CLAUDE_INBOX_VM:-claude}
# the owner of branches without a remote's prefix
me=''${LOGNAME:-''${USER:-$(id -un)}}
fetch=1
by=repo
cmd=
force=
pick=
diff=
what=
gitargs=()
words=()
# options, commands and words, in any order: the options and commands
# are one set, without overlaps, and the words are REPO and BRANCH
while test $# -gt 0; do
case $1 in
-d | --vm) test $# -ge 2 || die "$1 needs an argument" ;;
esac
case $1 in
-d) src=$2; shift ;;
--vm) vm=$2; shift ;;
--no-fetch) fetch= ;;
--by-remote) by=remote ;;
--list | --show | --take | --push | --checkout | --checkout=* | --stack | --stack=*)
c=''${1%%=*} c=''${c#--}
test -z "$cmd" || test "$cmd" = "$c" || die "--$cmd or --$c, not both"
case $1 in --*=*) words+=("''${1#*=}") ;; esac
cmd=$c ;;
-f | --force) force=1 ;;
--pick) pick=1 ;;
--diff) diff=1 what= ;;
--diff=*) diff=1 what=''${1#--diff=} ;;
--git=*) read -ra a <<< "''${1#--git=}"; gitargs+=("''${a[@]}") ;;
--git) die "--git=ARGS: more arguments for git" ;;
-h | --help) colors 1; usage | paint; exit ;;
-*) die "$1: no such option (claude-inbox -h)" ;;
*) words+=("$1")
esac
shift
done
test ''${#words[@]} -le 2 || die "''${words[*]}: too many words (REPO and BRANCH, at most)"
# a repository here, with its remote claude; and a branch of claude's
# there (but master, which is no branch to show or take)
is_repo() {
case $1 in "" | . | .. | */*) return 1 ;; esac
# [remote "claude"] in .git/config, without running git (with many
# repositories, it adds up), but where .git isn't a directory
local l
if test -d "$src/$1/.git"; then
while IFS= read -r l; do
case $l in *'[remote "claude"]'*) return 0 ;; esac
done 2>/dev/null < "$src/$1/.git/config" || :
return 1
fi
test -e "$src/$1/.git" && git -C "$src/$1" remote get-url claude >/dev/null 2>&1
}
has_branch() {
case $2 in master | HEAD) return 1 ;; esac
git -C "$src/$1" rev-parse -q --verify "refs/remotes/claude/$2" >/dev/null
}
repos_here() {
local d
for d in "$src"/*/; do
d=''${d%/}; d=''${d##*/}
if is_repo "$d"; then printf '%s\n' "$d"; fi
done
}
fetch_repos() {
local n
for n; do
git -C "$src/$n" fetch -q -p claude 2>&1 | sed "s/^/claude-inbox: $n: /" >&2 &
done
wait
}
# REPO, where a command needs one and got none: the one $PWD is in,
# in DIR, as given, or else physically (cd -P, pwd: builtins, no git)
repo_of_pwd() {
local here=$PWD p s n
for s in given physical; do
if test "$s" = given; then
s=$src p=$here
else
CDPATH= cd -P "$src" 2>/dev/null || return 1
s=$PWD
CDPATH= cd -P "$here" && p=$PWD
CDPATH= cd "$here"
fi
s=''${s%/}
case $p/ in
"$s"/?*)
n=''${p#"$s"/} n=''${n%%/*}
if is_repo "$n"; then name=$n; return 0; fi ;;
esac
done
return 1
}
# the words, as REPO (name) and BRANCH (b): every reading that exists,
# and it takes exactly one
resolve() {
local w=("''${words[@]}") readings=() n
case ''${#w[@]} in
1)
if is_repo "''${w[0]}"; then readings+=("''${w[0]} "); fi
for n in $(repos_here); do
if has_branch "$n" "''${w[0]}"; then readings+=("$n ''${w[0]}"); fi
done ;;
2)
if is_repo "''${w[0]}" && has_branch "''${w[0]}" "''${w[1]}"; then
readings+=("''${w[0]} ''${w[1]}")
fi
if is_repo "''${w[1]}" && has_branch "''${w[1]}" "''${w[0]}"; then
readings+=("''${w[1]} ''${w[0]}")
fi ;;
esac
case ''${#readings[@]} in
1) name=''${readings[0]% *} b=''${readings[0]#* } ;;
0) return 1 ;;
*) die "''${w[*]}: ambiguous: $(printf '%s\n' "''${readings[@]}" |
sed 's/^\(.*\) \(.\+\)$/\2 in \1/; s/^\(.*\) $/repository \1/' |
paste -sd, | sed 's/,/, /g')"
esac
}
name=
b=
if test ''${#words[@]} -gt 0; then
# --show fetches first, and so does no command, which is --show if
# there's a BRANCH, i.e. if a word may be one (--list fetches anyway)
prefetch=
if test -n "$fetch"; then
case $cmd in
show | checkout | stack) prefetch=1 ;;
"") if test ''${#words[@]} = 2 || ! is_repo "''${words[0]}"; then prefetch=1; fi ;;
esac
fi
if test -n "$prefetch"; then
# a repository among the words, or else all
fetched=$(for w in "''${words[@]}"; do if is_repo "$w"; then echo "$w"; fi; done)
fetch_repos ''${fetched:-$(repos_here)}
fi
resolve || die "''${words[*]}: no such repository, or branch of claude's, in $src$(
test -n "$prefetch" || echo " (as last fetched)")"
fi
if test -z "$cmd"; then
if test -n "$b"; then cmd=show; else cmd=list; fi
fi
# what each command takes
test -z "$force" || case $cmd in take | push) ;; *) die "-f is for --take and --push" ;; esac
test -z "$pick" || test "$cmd" = take || die "--pick is for --take"
test -z "$diff" || test "$cmd" = show || die "--diff is for --show"
test ''${#gitargs[@]} = 0 || test "$cmd" = show || die "--git is for --show"
test "$by" = repo || test "$cmd" = list || die "--by-remote is for --list"
case $cmd in
list) test -z "$b" || die "--list takes no BRANCH" ;;
show) if test -z "$name"; then
repo_of_pwd || die "--show needs a REPO or BRANCH (or to be in a REPO)"
test -z "$fetch" || fetch_repos "$name"
fi
test -n "$b" || test -z "$diff" || die "--show --diff needs a BRANCH" ;;
take) test -n "$b" || die "--take needs a BRANCH"
test -z "$force" || test -z "$pick" || die "--take: --force or --pick, not both" ;;
checkout) test -n "$b" || die "--checkout needs a BRANCH" ;;
stack) test -n "$b" || die "--stack needs a BRANCH" ;;
push) test -n "$name" || repo_of_pwd || die "--push needs a REPO (or to be in one)"
test -z "$b" || die "--push takes no BRANCH" ;;
esac
r=$src/$name
seen() {
git -C "$r" update-ref "refs/claude-seen/$1" "refs/remotes/claude/$1"
}
# only with a clean working tree (--checkout, --stack): nothing
# changed, nothing untracked, no merge, rebase, cherry-pick or revert
# going on
clean_tree() {
local x
test -z "$(git -C "$r" status --porcelain)" ||
die "$name: the working tree isn't clean (git status)"
for x in MERGE_HEAD CHERRY_PICK_HEAD REVERT_HEAD rebase-merge rebase-apply sequencer; do
test ! -e "$(git -C "$r" rev-parse --path-format=absolute --git-path "$x")" ||
die "$name: a merge, rebase, cherry-pick or revert is going on"
done
}
# master to claude's master, always; and if master has no upstream
# (a new one, of take --force, say), claude's, so @{u} works
push_master() {
git -C "$r" push "$@" claude master
git -C "$r" rev-parse -q --verify 'master@{upstream}' >/dev/null 2>&1 ||
git -C "$r" branch -q --set-upstream-to=claude/master master
}
case $cmd in
list)
# The VM's repositories (R) and remotes (M).
listing=$(ssh "$vm" '
cd src && for d in *; do
if test -d "$d/.git" && ! test -L "$d"; then echo "R $d"; fi
done
cd ../remotes 2>/dev/null && for d in *; do
if test -d "$d"; then echo "M $d"; fi
done
') || die "can't list the VM's repositories (ssh $vm)"
repos=$(printf '%s\n' "$listing" | sed -n 's/^R //p')
remotes=" $(printf '%s\n' "$listing" | sed -n 's/^M //p' | tr '\n' ' ')"
if test -n "$name"; then
printf '%s\n' "$repos" | grep -qxF -- "$name" || die "the VM has no repository $name"
repos=$name
fi
for n in $repos; do
if ! test -e "$src/$n/.git"; then
echo "claude-inbox: $n: not in $src" >&2
elif ! git -C "$src/$n" remote get-url claude >/dev/null 2>&1; then
echo "claude-inbox: $n: no remote claude" >&2
elif test -n "$fetch"; then
git -C "$src/$n" fetch -q -p claude 2>&1 | sed "s/^/claude-inbox: $n: /" >&2 &
fi
done
wait
# one repository's rows: its branches, tips, ages and what's been
# seen from one for-each-ref, and per branch, just cherry and
# rev-list
rows_of() {
local n=$1 r=$src/$1 ref tip age i b new behind owner mine state ahead master= tilde="~"
local -A seen=()
local branches=() tips=() ages=()
while read -r ref tip age; do
case $ref in
refs/claude-seen/*) seen[''${ref#refs/claude-seen/}]=$tip ;;
refs/remotes/claude/HEAD) ;;
refs/remotes/claude/master) master=1 ;;
refs/remotes/claude/*)
branches+=("''${ref#refs/remotes/claude/}") tips+=("$tip") ages+=("$age") ;;
esac
done < <(git -C "$r" for-each-ref \
--format='%(refname) %(objectname) %(committerdate:relative)' \
refs/claude-seen refs/remotes/claude)
if test -n "$master"; then
ahead=$(git -C "$r" cherry master claude/master | grep -c '^+' || :)
behind=$(git -C "$r" rev-list --count claude/master..master)
if test "$ahead" -gt 0; then
echo "$n master - VM-ahead! $ahead $behind -"
elif test "$behind" -gt 0; then
echo "$n master $me push 0 $behind -"
fi
fi
for i in "''${!branches[@]}"; do
b=''${branches[i]}
new=$(git -C "$r" cherry master "claude/$b" | grep -c '^+' || :)
behind=$(git -C "$r" rev-list --count "claude/$b..master")
case $b in
*/*) mine= owner=''${b%%/*}; case $remotes in *" $owner "*) ;; *) owner="$owner?" ;; esac ;;
*) mine=1 owner=$me
esac
if test "$new" = 0; then state=done
elif test -n "$mine"; then state=yours
elif test "$behind" = 0; then state=review
else state=rebase
fi
if test "''${seen[$b]-}" != "''${tips[i]}"; then
state="$state*"
fi
age=''${ages[i]% ago} # spaces as ~, in no branch name
echo "$n $b $owner $state $new $behind ''${age// /$tilde}"
done
}
# the repositories in parallel (a line is one write: they don't mix)
rows=$(
for n in $repos; do
test -e "$src/$n/.git" && git -C "$src/$n" remote get-url claude >/dev/null 2>&1 || continue
rows_of "$n" &
done
wait
)
if test -z "$rows"; then
echo "nothing to do"
exit
fi
if test "$by" = remote; then
rows=$(printf '%s\n' "$rows" | sort -k3,3 -k1,1 -k2,2)
else
rows=$(printf '%s\n' "$rows" | sort -k1,1 -k2,2)
fi
colors 1
{ echo "REPO BRANCH OWNER STATE NEW BEHIND AGE"; printf '%s\n' "$rows"; } |
column -t | sed 's/~/ /g' | paint_list
;;
show)
if test -z "$b"; then
git -C "$r" log --oneline --all --graph "''${gitargs[@]}"
elif test -n "$diff"; then
# from WHAT, any branch or commit, or by default, HEAD
what=''${what:-HEAD}
git -C "$r" rev-parse -q --verify "$what^{commit}" >/dev/null ||
die "$name: no branch or commit $what"
git -C "$r" diff "''${gitargs[@]}" "$what..claude/$b"
seen "$b"
else
git -C "$r" log -p --stat "''${gitargs[@]}" "master..claude/$b"
seen "$b"
fi
;;
take)
# master, checked out here, or nowhere (then it'll do without a
# working tree, but --pick can't), but not in another worktree
if test "$(git -C "$r" symbolic-ref -q --short HEAD)" = master; then
here=1
elif git -C "$r" worktree list --porcelain | grep -qx 'branch refs/heads/master'; then
die "$name: master is checked out in another worktree (git worktree list)"
else
here=
test -z "$pick" || die "$name: --pick needs master checked out"
fi
if test -n "$force"; then
# master, renamed, and a new one, of BRANCH, with the old one's
# upstream (which the rename took along)
old=abandoned/master-$(date -Is | tr : -) # no : in branch names
i=1
s=
while git -C "$r" rev-parse -q --verify "refs/heads/$old$s" >/dev/null; do
i=$((i + 1)) # another in the same second: -2, -3, ...
s=-$i
done
old=$old$s
up=$(git -C "$r" rev-parse -q --verify --abbrev-ref 'master@{upstream}' 2>/dev/null || :)
git -C "$r" branch -m master "$old"
if test -z "$here"; then
git -C "$r" branch -q --no-track master "claude/$b"
elif ! git -C "$r" switch -q --no-track -c master "claude/$b"; then
git -C "$r" branch -m "$old" master
die "$name: can't check out $b as master (uncommitted changes in the way?)"
fi
if test -n "$up"; then
git -C "$r" branch -q --set-upstream-to="$up" master
fi
echo "claude-inbox: $name: master is $b now; the old one is $old"
push_master --force-with-lease
elif test -n "$pick"; then
# what git cherry calls new (+), oldest first: rebased commits
# that master has already are left out, as a rebase would
picks=$(git -C "$r" cherry master "claude/$b" | sed -n 's/^+ //p')
test -n "$picks" || die "$name: master has all of $b already"
# shellcheck disable=SC2086
git -C "$r" cherry-pick $picks ||
die "$name: stopped picking $b (resolve, git cherry-pick --continue, then claude-inbox $name --push; or git cherry-pick --abort)"
push_master
else
if test -z "$here"; then
# fetching into a branch that isn't checked out: fast-forwards only
git -C "$r" fetch -q . "refs/remotes/claude/$b:refs/heads/master" ||
die "$name: $b isn't master and more (--take --force; or check out master, for --pick)"
else
git -C "$r" -c advice.diverging=false merge --ff-only "claude/$b" ||
die "$name: $b isn't master and more (--take --pick, or --force)"
fi
push_master
fi
seen "$b"
;;
checkout)
clean_tree
git -C "$r" switch -q --detach "claude/$b"
echo "claude-inbox: $name: at $b, detached: $(git -C "$r" log -1 --format='%h %s')"
;;
stack)
# BRANCH's commits that HEAD doesn't have yet (by content), on top
# of it, detached, so that no branch moves: to try several
# branches at once, stacked
clean_tree
picks=$(git -C "$r" cherry HEAD "claude/$b" | sed -n 's/^+ //p')
test -n "$picks" || die "$name: HEAD has all of $b already"
was=$(git -C "$r" log -1 --format='%h %s')
git -C "$r" symbolic-ref -q HEAD >/dev/null && git -C "$r" switch -q --detach
# shellcheck disable=SC2086
git -C "$r" cherry-pick $picks ||
die "$name: stopped stacking $b (resolve, git cherry-pick --continue; or git cherry-pick --abort, back to $was, detached)"
echo "claude-inbox: $name: $b stacked ($(wc -w <<< "$picks") commits) on $was, detached: $(git -C "$r" log -1 --format='%h %s')"
;;
push)
# not halfway through take --pick: on a conflict, master has some
# of the picks already (CHERRY_PICK_HEAD while it's unresolved, the
# sequencer for the rest)
if git -C "$r" rev-parse -q --verify CHERRY_PICK_HEAD >/dev/null ||
test -e "$(git -C "$r" rev-parse --path-format=absolute --git-path sequencer)"; then
die "$name: a cherry-pick is in progress (git cherry-pick --continue, or --abort)"
fi
push_master ''${force:+--force-with-lease}
;;
esac
'';
completion = writeText "claude-inbox-completion" ''
_claude_inbox() {
local cur=''${COMP_WORDS[COMP_CWORD]} prev=''${COMP_WORDS[COMP_CWORD-1]}
local src=''${CLAUDE_INBOX_SRC:-$HOME/sync/src} i w cmd= words=() used=" "
# what's there so far: -d DIR, the command, the options used (and
# what they exclude), and the words (bash splits --diff=WHAT into
# --diff, =, WHAT)
for ((i = 1; i < COMP_CWORD; i++)); do
w=''${COMP_WORDS[i]}
case $w in
-f | --force | --pick) used="$used-f --force --pick " ;;
--git) ;; # again and again
-*) used="$used$w " ;;
esac
case $w in
-d) src=''${COMP_WORDS[i+1]}; i=$((i + 1)) ;;
--vm) i=$((i + 1)) ;;
--list | --show | --take | --push) cmd=''${w#--} ;;
--checkout | --stack)
cmd=''${w#--}
# --checkout=BRANCH (--stack=): BRANCH a word, but the one at hand
if [ "''${COMP_WORDS[i+1]}" = = ]; then
[ $((i + 2)) -ge "$COMP_CWORD" ] || words+=("''${COMP_WORDS[i+2]}")
i=$((i + 2))
fi ;;
--diff | --git) [ "''${COMP_WORDS[i+1]}" != = ] || i=$((i + 2)) ;;
-*) ;;
*) words+=("$w") ;;
esac
done
case $prev in
-d) COMPREPLY=($(compgen -d -- "$cur")); return ;;
--vm) COMPREPLY=($(compgen -A hostname -- "$cur")); return ;;
esac
# The repositories with a remote claude, and claude's branches, read
# from .git, as running git twice per repository on every TAB adds
# up; but by git, where .git isn't a plain directory (a worktree's),
# or the refs aren't files (reftable).
local repos=" " d l
for d in "$src"/*/; do
d=''${d%/}
if [ -d "$d/.git" ]; then
while IFS= read -r l; do
case $l in *'[remote "claude"]'*) repos="$repos''${d##*/} "; break ;; esac
done < "$d/.git/config"
elif [ -e "$d/.git" ] && git -C "$d" remote get-url claude >/dev/null 2>&1; then
repos="$repos''${d##*/} "
fi
done 2>/dev/null
_claude_inbox_branches() {
local n g x r
for n; do
g=$src/$n/.git
if [ -d "$g" ] && ! [ -e "$g/reftable" ]; then
if [ -f "$g/packed-refs" ]; then
while read -r x r; do
case $r in refs/remotes/claude/*) printf '%s\n' "''${r#refs/remotes/claude/}" ;; esac
done < "$g/packed-refs"
fi
_claude_inbox_loose "$g/refs/remotes/claude" ""
else
git -C "$src/$n" for-each-ref --format='%(refname:lstrip=3)' refs/remotes/claude 2>/dev/null
fi
done | grep -vx -e master -e HEAD | sort -u
}
# DIR PREFIX: the loose refs below DIR, by name
_claude_inbox_loose() {
local f
for f in "$1"/*; do
case $f in *.lock) continue ;; esac
if [ -d "$f" ]; then
_claude_inbox_loose "$f" "$2''${f##*/}/"
elif [ -f "$f" ]; then
printf '%s\n' "$2''${f##*/}"
fi
done
}
# the words so far: a repository's name is the REPO, anything else
# the BRANCH (good enough to complete the rest)
local repo= branch=
for w in "''${words[@]}"; do
if [ -z "$repo" ] && case $repos in *" $w "*) true ;; *) false ;; esac; then
repo=$w
else
branch=$w
fi
done
# --diff=WHAT: the local branches, as the likeliest WHAT
if { [ "$cur" = = ] && [ "$prev" = --diff ]; } ||
{ [ "$prev" = = ] && [ "''${COMP_WORDS[COMP_CWORD-2]}" = --diff ]; }; then
[ "$cur" = = ] && cur=
COMPREPLY=($(compgen -W "$(git -C "$src/''${repo:-.}" for-each-ref --format='%(refname:short)' refs/heads 2>/dev/null)" -- "$cur"))
return
fi
# --checkout=BRANCH, --stack=BRANCH: REPO's branches, or all
if { [ "$cur" = = ] && { [ "$prev" = --checkout ] || [ "$prev" = --stack ]; }; } ||
{ [ "$prev" = = ] && case ''${COMP_WORDS[COMP_CWORD-2]} in --checkout | --stack) true ;; *) false ;; esac; }; then
[ "$cur" = = ] && cur=
COMPREPLY=($(compgen -W "$(_claude_inbox_branches ''${repo:-$repos})" -- "$cur"))
return
fi
# --git=ARGS: git log's options, or with --diff, git diff's
if { [ "$cur" = = ] && [ "$prev" = --git ]; } ||
{ [ "$prev" = = ] && [ "''${COMP_WORDS[COMP_CWORD-2]}" = --git ]; }; then
[ "$cur" = = ] && cur=
local gitopts
case " ''${COMP_WORDS[*]} " in
*" --diff "*)
gitopts="--stat --shortstat --numstat --name-only --name-status
--summary -p -U --unified= --word-diff --color-words -w
--ignore-all-space -b --ignore-space-change --ignore-blank-lines
-M --find-renames --diff-filter= -R --minimal --patience
--histogram" ;;
*)
gitopts="-p --patch -s --no-patch --stat --shortstat --name-only
--name-status --oneline --graph --decorate --format= --pretty=
--abbrev-commit --date= -n --max-count= --skip= --since=
--until= --author= --grep= --no-merges --merges
--first-parent --reverse -S -G --follow -U --unified=
--word-diff --color-words -w --ignore-all-space" ;;
esac
# inside quotes (--git='-p -n 1'): the last word, after the others
local w=$cur pre=
case $w in \'* | \"*) w=''${w:1} ;; esac
case $w in *" "*) pre="''${w% *} " w=''${w##* } ;; esac
COMPREPLY=()
for c in $(compgen -W "$gitopts" -- "$w"); do COMPREPLY+=("$pre$c"); done
_claude_inbox_nospace
return
fi
# what's left, in groups: the commands (until there's one), the
# options (a command's, once it's there), REPO, BRANCH; but nothing
# that's there already (nor what that excludes)
local cmds= opts="-d --vm --no-fetch" o left= repolist= branchlist= g c
case $cmd in
"") cmds="--list --show --take --push --checkout= --stack=" ;;
list) opts="$opts --by-remote" ;;
show) opts="$opts --diff --git=" ;;
take) opts="$opts -f --force --pick" ;;
push) opts="$opts -f --force" ;;
esac
for o in $opts; do
case $used in *" $o "*) ;; *) left="$left $o" ;; esac
done
[ -n "$repo" ] || repolist=$repos
if [ -z "$branch" ]; then
case $cmd in
list | push) ;;
*) branchlist=$(_claude_inbox_branches ''${repo:-$repos}) ;;
esac
fi
COMPREPLY=()
local kinds=() k=0
for g in "$cmds" "$left" "$repolist" "$branchlist"; do
for c in $(compgen -W "$g" -- "$cur"); do
case " ''${COMPREPLY[*]} " in
*" $c "*) ;;
*) COMPREPLY+=("$c"); kinds+=("$k") ;;
esac
done
k=$((k + 1))
done
_claude_inbox_nospace
# Listing only (a second TAB), in color, by kind: readline would
# print escapes as ^[, so the list is printed here, and readline gets
# one blank candidate, to list instead, and redraw the prompt after.
if [ "$COMP_TYPE" = 63 ] && [ ''${#COMPREPLY[@]} -gt 1 ]; then
local colors=
colors=$(_claude_inbox_colors) || return 0
_claude_inbox_list "$colors" "''${kinds[*]}" "''${COMPREPLY[@]}" >/dev/tty
COMPREPLY=(" ")
fi
}
# the colors of commands, options, REPOs, BRANCHes, as SGR parameters:
# truecolor, 256 colors, or 8, as the terminal has them; none (and so
# readline's own listing) with NO_COLOR, or on a dumb terminal
_claude_inbox_colors() {
[ -z "''${NO_COLOR-}" ] && [ -w /dev/tty ] || return 1
case ''${TERM-dumb} in dumb | "") return 1 ;; esac
local n
case ''${COLORTERM-} in
truecolor | 24bit)
echo "38;2;230;180;80 38;2;150;150;175 38;2;100;160;240 38;2;120;200;120"
return ;;
esac
case $TERM in
*256color*) n=256 ;;
*) n=$(tput colors 2>/dev/null) || n=8 ;;
esac
if [ "''${n:-0}" -ge 256 ]; then
echo "38;5;179 38;5;103 38;5;75 38;5;114"
elif [ "''${n:-0}" -ge 8 ]; then
echo "33 35 34 32"
else
return 1
fi
}
# COLORS KINDS WORD...: the words in columns, down then across, like
# readline's, each in the color of its kind
_claude_inbox_list() {
local colors=($1) kinds=($2) words=("''${@:3}") w width=0 n=$(($# - 2))
for w in "''${words[@]}"; do [ ''${#w} -le "$width" ] || width=''${#w}; done
width=$((width + 2))
local cols=$(( ''${COLUMNS:-80} / width )) rows r c i
[ "$cols" -ge 1 ] || cols=1
rows=$(( (n + cols - 1) / cols ))
printf '\n'
for ((r = 0; r < rows; r++)); do
for ((c = 0; c < cols; c++)); do
i=$((c * rows + r))
[ "$i" -lt "$n" ] || break
if [ $((c + 1)) -lt "$cols" ] && [ $((i + rows)) -lt "$n" ]; then
printf '\e[%sm%s\e[0m%*s' "''${colors[kinds[i]]}" "''${words[i]}" $((width - ''${#words[i]})) ""
else
printf '\e[%sm%s\e[0m' "''${colors[kinds[i]]}" "''${words[i]}"
fi
done
[ $((r + 1)) -ge "$rows" ] || printf '\n'
done
}
# in that order (bash 4.4 and later)
# an option that takes a value (--git=), the only one: no space after
_claude_inbox_nospace() {
if [ ''${#COMPREPLY[@]} = 1 ] && [ "''${COMPREPLY[0]%=}" != "''${COMPREPLY[0]}" ]; then
compopt -o nospace
fi
}
complete -o nosort -F _claude_inbox claude-inbox 2>/dev/null ||
complete -F _claude_inbox claude-inbox
'';
in
runCommand "claude-inbox" { meta.mainProgram = "claude-inbox"; } ''
install -D -m 755 ${script} $out/bin/claude-inbox
install -D -m 644 ${completion} $out/share/bash-completion/completions/claude-inbox
''
|