summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--pkgs/claude-inbox.nix223
1 files changed, 119 insertions, 104 deletions
diff --git a/pkgs/claude-inbox.nix b/pkgs/claude-inbox.nix
index 051bb12..86bedcf 100644
--- a/pkgs/claude-inbox.nix
+++ b/pkgs/claude-inbox.nix
@@ -15,28 +15,34 @@ let
usage() {
cat <<'EOF'
- usage: claude-inbox [-d DIR] [--vm HOST] [--no-fetch] [--by-remote] [list]
- claude-inbox [-d DIR] show REPO [BRANCH [--diff[=WHAT]]]
- claude-inbox [-d DIR] take [-f|--force] REPO BRANCH [-f|--force]
- claude-inbox [-d DIR] push [-f|--force] REPO [-f|--force]
+ usage: claude-inbox [OPTION...] [list]
+ claude-inbox [OPTION...] [--] REPO [list]
+ claude-inbox [OPTION...] [--] REPO show [BRANCH [--diff[=WHAT]]]
+ claude-inbox [OPTION...] [--] REPO take [-f|--force] BRANCH
+ claude-inbox [OPTION...] [--] REPO push [-f|--force]
+ OPTION: -d DIR, --vm HOST, --no-fetch, --by-remote.
list (the default) fetches the repositories the VM has (ssh HOST: its
- ~/src, but for symlinks) from their remote claude, here in DIR, and
- shows the branches there with something master doesn't have, by
- content, and those it has all of (to delete), and whether master
- needs pushing. OWNER: the remote, by the branch's prefix, or tv,
- without one. STATE: review (it's new, and master's in it), rebase
- (master moved on: the remote's turn), done (master has it all: to
- delete), yours (tv's own); * marks what changed since show or take.
+ ~/src, but for symlinks), or just REPO, from their remote claude, here
+ in DIR, and shows the branches there with something master doesn't
+ have, by content, and those it has all of (to delete), and whether
+ master needs pushing. OWNER: the remote, by the branch's prefix, or
+ tv, without one. STATE: review (it's new, and master's in it),
+ rebase (master moved on: the remote's turn), done (master has it all:
+ to delete), yours (tv's own); * marks what changed since show or take.
+ --by-remote: grouped by OWNER.
show: master..BRANCH, with patches (and BRANCH is seen); --diff: the
diff to BRANCH from WHAT (any branch or commit), by default from the
local branch of BRANCH's name, without the remote's prefix (B, for
- REMOTE/B); without BRANCH, all branches, as a graph. take: fast-forward master (it has to be checked out) to
- BRANCH, and push it to claude; --force: rename master to
- abandoned/master-DATE, check out BRANCH as master (with the old one's
- upstream), and push it like push --force.
+ REMOTE/B); without BRANCH, all branches, as a graph.
+ take: fast-forward master (it has to be checked out) to BRANCH, and
+ push it to claude; --force: rename master to abandoned/master-DATE,
+ check out BRANCH as master (with the old one's upstream), and push it
+ like push --force.
push: master to claude; --force for a rewritten master, but only
while claude's is still the one last fetched (--force-with-lease).
DIR: $CLAUDE_INBOX_SRC, or ~/sync/src; HOST: $CLAUDE_INBOX_VM, or claude.
+ Options of a command (-f, --diff) can go anywhere after it. -- takes
+ the next word for REPO, even list or help.
EOF
}
die() {
@@ -48,33 +54,61 @@ let
vm=''${CLAUDE_INBOX_VM:-claude}
fetch=1
by=repo
+ dashdash=
while test $# -gt 0; do
case $1 in
-d) src=$2; shift 2 ;;
--vm) vm=$2; shift 2 ;;
--no-fetch) fetch=; shift ;;
--by-remote) by=remote; shift ;;
+ --) dashdash=1; shift; break ;;
-h | --help) usage; exit ;;
-*) usage >&2; exit 1 ;;
*) break
esac
done
- cmd=''${1-list}
- shift || :
+ # [--] [REPO] COMMAND: after --, a REPO, whatever its name; else, list
+ # and help are commands, and anything else a REPO
+ if test -n "$dashdash"; then
+ test $# -gt 0 || { usage >&2; exit 1; }
+ name=$1; shift; cmd=''${1-list}; shift || :
+ else
+ case ''${1-list} in
+ list) name=; cmd=list; shift || : ;;
+ help) usage; exit ;;
+ show | take | push) die "the repository comes first: claude-inbox REPO $1 ..." ;;
+ *) name=$1; shift; cmd=''${1-list}; shift || :
+ esac
+ fi
- # a repository here, with its remote claude
+ # REPO here, with its remote claude
repo() {
- local r=$src/$1
- test -n "$1" && test -e "$r/.git" || die "no repository $1 in $src"
- git -C "$r" remote get-url claude >/dev/null 2>&1 || die "$1: no remote claude"
+ local r=$src/$name
+ test -e "$r/.git" || die "no repository $name in $src"
+ git -C "$r" remote get-url claude >/dev/null 2>&1 || die "$name: no remote claude"
printf %s "$r"
}
seen() {
- git -C "$1" update-ref "refs/claude-seen/$2" "refs/remotes/claude/$2"
+ git -C "$r" update-ref "refs/claude-seen/$1" "refs/remotes/claude/$1"
}
+ # the command's own options (-f, --diff) anywhere among its arguments
+ force=
+ diff=
+ what=
+ args=()
+ for a; do
+ case $cmd:$a in
+ take:-f | take:--force | push:-f | push:--force) force=1 ;;
+ show:--diff) diff=1 what= ;;
+ show:--diff=*) diff=1 what=''${a#--diff=} ;;
+ *:-*) usage >&2; exit 1 ;;
+ *) args+=("$a")
+ esac
+ done
case $cmd in
list)
+ test ''${#args[@]} = 0 || { usage >&2; exit 1; }
# The VM's repositories (R) and remotes (M).
listing=$(ssh "$vm" '
cd src && for d in *; do
@@ -86,6 +120,10 @@ let
') || 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
@@ -144,59 +182,38 @@ let
column -t | sed 's/~/ /g'
;;
show)
- diff=
- what=
- args=()
- for a; do
- case $a in
- --diff) diff=1 what= ;;
- --diff=*) diff=1 what=''${a#--diff=} ;;
- -*) usage >&2; exit 1 ;;
- *) args+=("$a")
- esac
- done
- test ''${#args[@]} = 1 || test ''${#args[@]} = 2 || { usage >&2; exit 1; }
- r=$(repo "''${args[0]}")
- if test ''${#args[@]} = 1; then
+ test ''${#args[@]} -le 1 || { usage >&2; exit 1; }
+ r=$(repo)
+ if test ''${#args[@]} = 0; then
test -z "$diff" || die "show --diff needs a BRANCH"
git -C "$r" log --oneline --all --graph
elif test -n "$diff"; then
# from WHAT, any branch or commit, or by default, the local branch
# of the same name, but for the remote's prefix
- b=''${args[1]}
+ b=''${args[0]}
if test -n "$what"; then
git -C "$r" rev-parse -q --verify "$what^{commit}" >/dev/null ||
- die "''${args[0]}: no branch or commit $what"
+ die "$name: no branch or commit $what"
else
what=''${b#*/}
git -C "$r" rev-parse -q --verify "refs/heads/$what" >/dev/null ||
- die "''${args[0]}: no local branch $what (for $b; or --diff=WHAT)"
+ die "$name: no local branch $what (for $b; or --diff=WHAT)"
fi
git -C "$r" diff "$what..claude/$b"
- seen "$r" "$b"
+ seen "$b"
else
- git -C "$r" log -p --stat "master..claude/''${args[1]}"
- seen "$r" "''${args[1]}"
+ git -C "$r" log -p --stat "master..claude/''${args[0]}"
+ seen "''${args[0]}"
fi
;;
take)
- force=
- args=()
- for a; do
- case $a in
- -f | --force) force=1 ;;
- -*) usage >&2; exit 1 ;;
- *) args+=("$a")
- esac
- done
- test ''${#args[@]} = 2 || { usage >&2; exit 1; }
- n=''${args[0]}
- b=''${args[1]}
- r=$(repo "$n")
+ test ''${#args[@]} = 1 || { usage >&2; exit 1; }
+ b=''${args[0]}
+ r=$(repo)
test "$(git -C "$r" symbolic-ref -q --short HEAD)" = master ||
- die "$n: master isn't checked out"
+ die "$name: master isn't checked out"
git -C "$r" rev-parse -q --verify "refs/remotes/claude/$b" >/dev/null ||
- die "$n: claude has no branch $b"
+ die "$name: claude has no branch $b"
if test -n "$force"; then
# master, renamed, and a new one, of BRANCH, with the old one's
# upstream (which the rename took along)
@@ -205,36 +222,24 @@ let
git -C "$r" branch -m master "$old"
if ! git -C "$r" switch -q --no-track -c master "claude/$b"; then
git -C "$r" branch -m "$old" master
- die "$n: can't check out $b as master (uncommitted changes in the way?)"
+ 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: $n: master is $b now; the old one is $old"
+ echo "claude-inbox: $name: master is $b now; the old one is $old"
git -C "$r" push --force-with-lease claude master
else
git -C "$r" -c advice.diverging=false merge --ff-only "claude/$b" ||
- die "$n: $b isn't master and more (rebase it first, or take --force)"
+ die "$name: $b isn't master and more (rebase it first, or take --force)"
git -C "$r" push claude master
fi
- seen "$r" "$b"
+ seen "$b"
;;
push)
- force=
- n=
- for a; do
- case $a in
- -f | --force) force=--force-with-lease ;;
- -*) usage >&2; exit 1 ;;
- *) test -z "$n" || { usage >&2; exit 1; }; n=$a
- esac
- done
- test -n "$n" || { usage >&2; exit 1; }
- r=$(repo "$n")
- git -C "$r" push $force claude master
- ;;
- help)
- usage
+ test ''${#args[@]} = 0 || { usage >&2; exit 1; }
+ r=$(repo)
+ git -C "$r" push ''${force:+--force-with-lease} claude master
;;
*)
usage >&2
@@ -245,51 +250,61 @@ let
completion = writeText "claude-inbox-completion" ''
_claude_inbox() {
local cur=''${COMP_WORDS[COMP_CWORD]} src=''${CLAUDE_INBOX_SRC:-$HOME/sync/src} i=1
+ # the options, and -- (then the next word is a REPO, whatever it is)
+ local dashdash=
while [ "$i" -lt "$COMP_CWORD" ]; do
- case ''${COMP_WORDS[i]} in -d) src=''${COMP_WORDS[i+1]}; i=$((i + 2)) ;; --vm) i=$((i + 2)) ;; -*) i=$((i + 1)) ;; *) break ;; esac
+ case ''${COMP_WORDS[i]} in
+ -d) src=''${COMP_WORDS[i+1]}; i=$((i + 2)) ;;
+ --vm) i=$((i + 2)) ;;
+ --) dashdash=1; i=$((i + 1)); break ;;
+ -*) i=$((i + 1)) ;;
+ *) break
+ esac
done
- local cmd=''${COMP_WORDS[i]}
- local n=$((COMP_CWORD - i))
- _claude_inbox_repos() {
- (cd "$src" 2>/dev/null && for d in */; do git -C "$d" remote get-url claude >/dev/null 2>&1 && echo "''${d%/}"; done)
- }
- _claude_inbox_branches() {
- git -C "$src/$1" for-each-ref --format='%(refname:lstrip=3)' refs/remotes/claude 2>/dev/null | grep -vx -e master -e HEAD
- }
- # the arguments so far, but for options: REPO, BRANCH (bash splits
+ local name=''${COMP_WORDS[i]}
+ if [ "$COMP_CWORD" -eq "$i" ]; then
+ # [--] [REPO] COMMAND: after --, a REPO; else also list, help, options
+ local repos="$(cd "$src" 2>/dev/null && for d in */; do git -C "$d" remote get-url claude >/dev/null 2>&1 && echo "''${d%/}"; done)"
+ if [ -n "$dashdash" ]; then
+ COMPREPLY=($(compgen -W "$repos" -- "$cur"))
+ else
+ COMPREPLY=($(compgen -W "list help -d --vm --no-fetch --by-remote -- $repos" -- "$cur"))
+ fi
+ return
+ fi
+ [ -n "$dashdash" ] || case $name in list | help) return ;; esac
+ local cmd=''${COMP_WORDS[i+1]}
+ if [ "$COMP_CWORD" -eq $((i + 1)) ]; then
+ COMPREPLY=($(compgen -W "list show take push" -- "$cur"))
+ return
+ fi
+ # the command's arguments so far, but for options (bash splits
# --diff=WHAT into --diff, =, WHAT)
local w prev= pprev= args=()
- for w in "''${COMP_WORDS[@]:i+1:n-1}"; do
+ for w in "''${COMP_WORDS[@]:i+2:COMP_CWORD-i-2}"; do
case $prev:$w in
--diff:=) ;;
- =:*) [ "$prev" = = ] && [ "''${pprev-}" = --diff ] || args+=("$w") ;;
+ =:*) [ "$pprev" = --diff ] || args+=("$w") ;;
*:-*) ;;
*) args+=("$w") ;;
esac
pprev=$prev prev=$w
done
- local flags=
- case $cmd in push | take) flags="-f --force" ;; show) flags=--diff ;; esac
- # --diff=WHAT: the local branches, as the likeliest WHAT
local before=''${COMP_WORDS[COMP_CWORD-1]}
+ # --diff=WHAT: the local branches, as the likeliest WHAT
if [ "$cmd" = show ] && { { [ "$cur" = = ] && [ "$before" = --diff ]; } ||
{ [ "$before" = = ] && [ "''${COMP_WORDS[COMP_CWORD-2]}" = --diff ]; }; }; then
[ "$cur" = = ] && cur=
- COMPREPLY=($(compgen -W "$(git -C "$src/''${args[0]}" for-each-ref --format='%(refname:short)' refs/heads 2>/dev/null)" -- "$cur"))
+ COMPREPLY=($(compgen -W "$(git -C "$src/$name" for-each-ref --format='%(refname:short)' refs/heads 2>/dev/null)" -- "$cur"))
return
fi
- if [ "$n" -eq 0 ]; then
- COMPREPLY=($(compgen -W "list show take push help -d --vm --no-fetch --by-remote" -- "$cur"))
- else
- case $cmd:''${#args[@]} in
- show:0 | take:0 | push:0)
- COMPREPLY=($(compgen -W "$flags $(_claude_inbox_repos)" -- "$cur")) ;;
- show:1 | take:1)
- COMPREPLY=($(compgen -W "$flags $(_claude_inbox_branches "''${args[0]}")" -- "$cur")) ;;
- take:* | push:* | show:*)
- COMPREPLY=($(compgen -W "$flags" -- "$cur")) ;;
- esac
- fi
+ local branches="$(git -C "$src/$name" for-each-ref --format='%(refname:lstrip=3)' refs/remotes/claude 2>/dev/null | grep -vx -e master -e HEAD)"
+ case $cmd:''${#args[@]} in
+ show:0) COMPREPLY=($(compgen -W "--diff $branches" -- "$cur")) ;;
+ show:*) COMPREPLY=($(compgen -W "--diff" -- "$cur")) ;;
+ take:0) COMPREPLY=($(compgen -W "-f --force $branches" -- "$cur")) ;;
+ take:* | push:*) COMPREPLY=($(compgen -W "-f --force" -- "$cur")) ;;
+ esac
}
complete -F _claude_inbox claude-inbox
'';