# claude-inbox, for the host: which of the repositories that the claude # VM has (~claude/src, but for symlinks, i.e. third parties' in src/tmp) # 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 tv'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 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() { echo "claude-inbox: $*" >&2 exit 1 } src=''${CLAUDE_INBOX_SRC:-$HOME/sync/src} vm=''${CLAUDE_INBOX_VM:-claude} fetch=1 by=repo 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 | --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 ;; *) 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 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 seen() { git -C "$r" update-ref "refs/claude-seen/$1" "refs/remotes/claude/$1" } # 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 rows=$( for n in $repos; do r=$src/$n test -e "$r/.git" && git -C "$r" remote get-url claude >/dev/null 2>&1 || continue if git -C "$r" rev-parse -q --verify refs/remotes/claude/master >/dev/null; 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 tv push 0 $behind -" fi fi git -C "$r" for-each-ref --format='%(refname:lstrip=3) %(objectname)' refs/remotes/claude | while read -r b tip; do case $b in master | HEAD) continue ;; esac new=$(git -C "$r" cherry master "claude/$b" | grep -c '^+' || :) behind=$(git -C "$r" rev-list --count "claude/$b..master") case $b in */*) owner=''${b%%/*}; case $remotes in *" $owner "*) ;; *) owner="$owner?" ;; esac ;; *) owner=tv esac if test "$new" = 0; then state=done elif test "$owner" = tv; then state=yours elif test "$behind" = 0; then state=review else state=rebase fi if test "$(git -C "$r" rev-parse -q --verify "refs/claude-seen/$b" || :)" != "$tip"; then state="$state*" fi age=$(git -C "$r" log -1 --format=%cr "claude/$b" | sed 's/ ago$//; s/ /~/g') # ~: in no branch name echo "$n $b $owner $state $new $behind $age" done done ) 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 { echo "REPO BRANCH OWNER STATE NEW BEHIND AGE"; printf '%s\n' "$rows"; } | column -t | sed 's/~/ /g' ;; show) 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 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/$b" seen "$b" fi ;; take) test "$(git -C "$r" symbolic-ref -q --short HEAD)" = master || die "$name: master isn't checked out" 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 ! 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 git -C "$r" -c advice.diverging=false merge --ff-only "claude/$b" || die "$name: $b isn't master and more (--take --pick, or --force)" push_master fi seen "$b" ;; 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 " ;; -*) 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#--} ;; --diff) [ "''${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 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 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 # 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" ;; list) opts="$opts --by-remote" ;; show) opts="$opts --diff" ;; 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=() for g in "$cmds" "$left" "$repolist" "$branchlist"; do for c in $(compgen -W "$g" -- "$cur"); do case " ''${COMPREPLY[*]} " in *" $c "*) ;; *) COMPREPLY+=("$c") ;; esac done done } # in that order (bash 4.4 and later) 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 ''