From a2de267ba5819d1664c4dd4995b81c70b3ebace8 Mon Sep 17 00:00:00 2001 From: tv Date: Sat, 21 Nov 2026 21:56:43 +0000 Subject: pkgs/claude-inbox: REPO, BRANCH, --COMMAND and options, in any order Commands are options now (--list, --show, --take, --push), in one set with the others, without overlaps, so only REPO and BRANCH are words, and each word is what exists: a repository in DIR, or a branch of claude's in one, the one reading of all words that does, or an error. Without REPO, BRANCH is in the one repository that has it; without a command, it's --show if there's a BRANCH, else --list. --show fetches first (REPO, or else all), --take doesn't: it takes what was shown. An option that isn't for the command is an error, not ignored. --- pkgs/claude-inbox.nix | 342 +++++++++++++++++++++++++++++--------------------- 1 file changed, 200 insertions(+), 142 deletions(-) diff --git a/pkgs/claude-inbox.nix b/pkgs/claude-inbox.nix index 0e71ba2..74a665f 100644 --- a/pkgs/claude-inbox.nix +++ b/pkgs/claude-inbox.nix @@ -15,38 +15,40 @@ let usage() { cat <<'EOF' - 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|--pick] 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), 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 - HEAD; 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; --pick: put the commits of BRANCH that master - doesn't have yet (by content) on top of it, one by one (a rebase, - but onto master, and on it: on a conflict, it stops there, to resolve - and git cherry-pick --continue, then push, or --abort), and push it. - push: master to claude; --force for a rewritten master, but only - while claude's is still the one last fetched (--force-with-lease). - Both push master to claude's master, and give master that upstream, if - it has none. - DIR: $CLAUDE_INBOX_SRC, or ~/sync/src; HOST: $CLAUDE_INBOX_VM, or claude. - Options of a command (-f, --pick, --diff) can go anywhere after it. - -- takes the next word for REPO, even list or help. + usage: claude-inbox [REPO] [BRANCH] [COMMAND] [OPTION...], in any order + COMMAND: --list (the default without BRANCH), --show (the default with + BRANCH), --take, --push. REPO: a repository in DIR, with a remote + claude; BRANCH: one of claude's there, or without REPO, in the one + repository that has it (by the last fetch; --show fetches first). A + word that could be either is what makes sense of all the words, if + exactly one reading does. + --list [REPO] [--by-remote] fetches the repositories the VM has (ssh + HOST: its ~/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 REPO|BRANCH [--diff[=WHAT]]: master..BRANCH, with patches (and + BRANCH is seen); --diff: the diff to BRANCH from WHAT (any branch or + commit), by default from HEAD; without BRANCH, REPO's branches, as a + graph. Fetches first: REPO, or without it, all of DIR's. + --take BRANCH [-f|--force|--pick]: fast-forward master (it has to be + checked out) to BRANCH, as last fetched (i.e. as shown), 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; --pick: put the commits of BRANCH that master doesn't + have yet (by content) on top of it, one by one (a rebase, but onto + master, and on it: on a conflict, it stops there, to resolve and git + cherry-pick --continue, then --push, or --abort), and push it. + --push REPO [-f|--force]: master to claude; --force for a rewritten + master, but only while claude's is still the one last fetched + (--force-with-lease). Both push master to claude's master, and give + master that upstream, if it has none. + OPTION, besides a command's: -d DIR ($CLAUDE_INBOX_SRC, or ~/sync/src), + --vm HOST ($CLAUDE_INBOX_VM, or claude), --no-fetch. EOF } die() { @@ -58,40 +60,127 @@ let vm=''${CLAUDE_INBOX_VM:-claude} fetch=1 by=repo - dashdash= + cmd= + force= + pick= + diff= + what= + 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) src=$2; shift 2 ;; - --vm) vm=$2; shift 2 ;; - --no-fetch) fetch=; shift ;; - --by-remote) by=remote; shift ;; - --) dashdash=1; shift; break ;; + -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) + test -z "$cmd" || test "$cmd" = "''${1#--}" || die "--$cmd or $1, not both" + cmd=''${1#--} ;; + -f | --force) force=1 ;; + --pick) pick=1 ;; + --diff) diff=1 what= ;; + --diff=*) diff=1 what=''${1#--diff=} ;; -h | --help) usage; exit ;; -*) usage >&2; exit 1 ;; - *) break + *) words+=("$1") esac + shift done - # [--] [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 || : + 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 + 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 + } + # 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) 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 "$by" = repo || test "$cmd" = list || die "--by-remote is for --list" + case $cmd in + list) test -z "$b" || die "--list takes no BRANCH" ;; + show) test -n "$name" || die "--show needs a REPO or BRANCH" + 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" ;; + push) test -n "$name" || die "--push needs a REPO" + test -z "$b" || die "--push takes no BRANCH" ;; + esac + r=$src/$name - # REPO here, with its remote claude - repo() { - 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 "$r" update-ref "refs/claude-seen/$1" "refs/remotes/claude/$1" } @@ -102,27 +191,9 @@ let git -C "$r" rev-parse -q --verify 'master@{upstream}' >/dev/null 2>&1 || git -C "$r" branch -q --set-upstream-to=claude/master master } - # the command's own options (-f, --pick, --diff) anywhere among its - # arguments - force= - pick= - diff= - what= - args=() - for a; do - case $cmd:$a in - take:-f | take:--force | push:-f | push:--force) force=1 ;; - take:--pick) pick=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 @@ -196,33 +267,23 @@ let column -t | sed 's/~/ /g' ;; show) - test ''${#args[@]} -le 1 || { usage >&2; exit 1; } - r=$(repo) - if test ''${#args[@]} = 0; then - test -z "$diff" || die "show --diff needs a BRANCH" + if test -z "$b"; then git -C "$r" log --oneline --all --graph elif test -n "$diff"; then # from WHAT, any branch or commit, or by default, HEAD - b=''${args[0]} 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 "$what..claude/$b" seen "$b" else - git -C "$r" log -p --stat "master..claude/''${args[0]}" - seen "''${args[0]}" + git -C "$r" log -p --stat "master..claude/$b" + seen "$b" fi ;; take) - test ''${#args[@]} = 1 || { usage >&2; exit 1; } - b=''${args[0]} - r=$(repo) test "$(git -C "$r" symbolic-ref -q --short HEAD)" = master || die "$name: master isn't checked out" - git -C "$r" rev-parse -q --verify "refs/remotes/claude/$b" >/dev/null || - die "$name: claude has no branch $b" - test -z "$force" || test -z "$pick" || die "take: --force or --pick, not both" if test -n "$force"; then # master, renamed, and a new one, of BRANCH, with the old one's # upstream (which the rename took along) @@ -252,18 +313,16 @@ let 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)" + die "$name: stopped picking $b (resolve, git cherry-pick --continue, then claude-inbox $name --push; or git cherry-pick --abort)" push_master 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)" + die "$name: $b isn't master and more (--take --pick, or --force)" push_master fi seen "$b" ;; push) - test ''${#args[@]} = 0 || { usage >&2; exit 1; } - r=$(repo) # 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) @@ -273,71 +332,70 @@ let fi push_master ''${force:+--force-with-lease} ;; - *) - usage >&2 - exit 1 esac ''; 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)) ;; - --) dashdash=1; i=$((i + 1)); break ;; - -*) i=$((i + 1)) ;; - *) break + local cur=''${COMP_WORDS[COMP_CWORD]} prev=''${COMP_WORDS[COMP_CWORD-1]} + local src=''${CLAUDE_INBOX_SRC:-$HOME/sync/src} i w cmd= words=() + # what's there so far: -d DIR, the command, 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 + -d) src=''${COMP_WORDS[i+1]}; i=$((i + 1)) ;; + --vm) i=$((i + 1)) ;; + --list | --show | --take | --push) cmd=''${w#--} ;; + --diff) [ "''${COMP_WORDS[i+1]}" != = ] || i=$((i + 2)) ;; + -*) ;; + *) words+=("$w") ;; esac done - 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")) + case $prev in + -d) COMPREPLY=($(compgen -d -- "$cur")); return ;; + --vm) COMPREPLY=($(compgen -A hostname -- "$cur")); return ;; + esac + 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)" + _claude_inbox_branches() { + local n + for n; do + git -C "$src/$n" for-each-ref --format='%(refname:lstrip=3)' refs/remotes/claude 2>/dev/null + done | grep -vx -e master -e HEAD | sort -u + } + # 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" ] && printf '%s\n' "$repos" | grep -qxF -- "$w"; then + repo=$w else - COMPREPLY=($(compgen -W "list help -d --vm --no-fetch --by-remote -- $repos" -- "$cur")) + branch=$w 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+2:COMP_CWORD-i-2}"; do - case $prev:$w in - --diff:=) ;; - =:*) [ "$pprev" = --diff ] || args+=("$w") ;; - *:-*) ;; - *) args+=("$w") ;; - esac - pprev=$prev prev=$w done - 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 + if { [ "$cur" = = ] && [ "$prev" = --diff ]; } || + { [ "$prev" = = ] && [ "''${COMP_WORDS[COMP_CWORD-2]}" = --diff ]; }; then [ "$cur" = = ] && cur= - COMPREPLY=($(compgen -W "$(git -C "$src/$name" for-each-ref --format='%(refname:short)' refs/heads 2>/dev/null)" -- "$cur")) + COMPREPLY=($(compgen -W "$(git -C "$src/''${repo:-.}" for-each-ref --format='%(refname:short)' refs/heads 2>/dev/null)" -- "$cur")) return 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 --pick $branches" -- "$cur")) ;; - take:*) COMPREPLY=($(compgen -W "-f --force --pick" -- "$cur")) ;; - push:*) COMPREPLY=($(compgen -W "-f --force" -- "$cur")) ;; + local opts="-d --vm --no-fetch" more= + case $cmd in + "") opts="$opts --list --show --take --push --by-remote --diff -f --force --pick" ;; + list) opts="$opts --by-remote" ;; + show) opts="$opts --diff" ;; + take) opts="$opts -f --force --pick" ;; + push) opts="$opts -f --force" ;; esac + [ -n "$repo" ] || more=$repos + if [ -z "$branch" ]; then + case $cmd in + list | push) ;; + *) more="$more $(_claude_inbox_branches ''${repo:-$repos})" ;; + esac + fi + COMPREPLY=($(compgen -W "$opts $(printf '%s\n' $more | sort -u)" -- "$cur")) } complete -F _claude_inbox claude-inbox ''; -- cgit v1.3.1