hop
Hop: a small native macOS launcher. Clipboard history, find, on-device dictation and your coding agent behind one key.
Brought by drakestapleton
Comparing 59208f4d3b4a2af63706526f390002df2fd1890c ... 0efecb957849ec5decde270141b7030e0edcf80a
- Accept only folder-read and vanished-file errors from the file-list find
- Install the remote file list when find meets unreadable folders
4 files changed, +68 -4
| Sources/Machines.swift | +1 | -4 |
| Sources/RemoteIndex.swift | +15 | -0 |
| tests/RemoteIndexChecks.swift | +11 | -0 |
| tools/check-index-rebuild.sh | +41 | -0 |
Sources/Machines.swift
| @@ -51,10 +51,7 @@ enum Remote { | ||
| 51 | 51 | static func refreshIndex(_ m: Machine) async { |
| 52 | 52 | let prune = m.skip.map { "-name " + Shell.quote($0) }.joined(separator: " -o ") |
| 53 | 53 | let roots = m.roots.map(Shell.quote).joined(separator: " ") |
| 54 | - let build = "cd ~/.cache/hop && find \(roots) -xdev \\( -path '*/.*' -o \(prune) \\) -prune -o -type d -printf '%p/\\n' -o -printf '%p\\n' " + | |
| 55 | - "> index.tmp 2>/dev/null && rm -rf shards.new && mkdir shards.new && split -n l/\(shards) -d -a 2 index.tmp shards.new/s " + | |
| 56 | - "&& rm -f index.tmp index.txt && rm -rf shards.old && { [ ! -d shards ] || mv shards shards.old; } && mv shards.new shards && rm -rf shards.old; " + | |
| 57 | - "rm -f index.tmp; rmdir ~/.cache/hop/building" | |
| 54 | + let build = RemoteIndex.build(roots: roots, prune: prune, shards: shards) | |
| 58 | 55 | let script = """ |
| 59 | 56 | mkdir -p ~/.cache/hop; f=\(shardDir)/s00 |
| 60 | 57 | if [ -d ~/.cache/hop/building ] && [ -n "$(find ~/.cache/hop/building -maxdepth 0 -mmin +180 2>/dev/null)" ]; then rmdir ~/.cache/hop/building; fi |
Sources/RemoteIndex.swift
| new file mode 100644 | ||
| @@ -0,0 +1,15 @@ | ||
| 1 | +/// The shell command that rebuilds a machine's file list in ~/.cache/hop as shards (Remote.refreshIndex runs it there | |
| 2 | +/// under nohup). `roots` and `prune` come already quoted for the shell. Checked on Linux by tools/check-index-rebuild.sh. | |
| 3 | +enum RemoteIndex { | |
| 4 | + static func build(roots: String, prune: String, shards: Int) -> String { | |
| 5 | + // find exits 1 for every error it reports and walks on. Folders it may not read (root's or another user's | |
| 6 | + // data under a root) and files that vanish during the walk still leave a list worth having, so keep it then. | |
| 7 | + // Any other message (a write error when the disk fills) or a worse exit (killed, 2+) throws the list away. | |
| 8 | + // Before, any exit but 0 threw it away, the shards never installed and every refresh started over (L1290). | |
| 9 | + "cd ~/.cache/hop && LC_ALL=C find \(roots) -xdev \\( -path '*/.*' -o \(prune) \\) -prune -o -type d -printf '%p/\\n' -o -printf '%p\\n' " + | |
| 10 | + "> index.tmp 2> index.err; c=$?; { [ $c -eq 0 ] || { [ $c -eq 1 ] && ! LC_ALL=C grep -qv -e ': Permission denied$' -e ': No such file or directory$' index.err; }; } " + | |
| 11 | + "&& [ -s index.tmp ] && rm -rf shards.new && mkdir shards.new && split -n l/\(shards) -d -a 2 index.tmp shards.new/s " + | |
| 12 | + "&& rm -f index.tmp index.txt && rm -rf shards.old && { [ ! -d shards ] || mv shards shards.old; } && mv shards.new shards && rm -rf shards.old; " + | |
| 13 | + "rm -f index.tmp index.err; rmdir ~/.cache/hop/building" | |
| 14 | + } | |
| 15 | +} |
tests/RemoteIndexChecks.swift
| new file mode 100644 | ||
| @@ -0,0 +1,11 @@ | ||
| 1 | +// Prints, quoted for sh, the file-list rebuild command for the roots given (for tools/check-index-rebuild.sh). | |
| 2 | +import Foundation | |
| 3 | + | |
| 4 | +@main | |
| 5 | +struct RemoteIndexChecks { | |
| 6 | + static func quote(_ s: String) -> String { "'" + s.replacingOccurrences(of: "'", with: "'\\''") + "'" } | |
| 7 | + static func main() { | |
| 8 | + let roots = CommandLine.arguments.dropFirst().map(quote).joined(separator: " ") | |
| 9 | + print(quote(RemoteIndex.build(roots: roots, prune: "-name " + quote("node_modules"), shards: 20))) | |
| 10 | + } | |
| 11 | +} |
tools/check-index-rebuild.sh
| new file mode 100755 | ||
| @@ -0,0 +1,41 @@ | ||
| 1 | +#!/bin/sh | |
| 2 | +# Checks the command Hop uses to rebuild another machine's file list (Sources/RemoteIndex.swift) where it runs: on a | |
| 3 | +# Linux machine with GNU find. Builds the command here, runs five cases there in a scratch folder under /tmp. | |
| 4 | +# Usage: tools/check-index-rebuild.sh SSH_HOST | |
| 5 | +set -eu | |
| 6 | +cd "$(dirname "$0")/.." | |
| 7 | +host=${1:?usage: tools/check-index-rebuild.sh SSH_HOST} | |
| 8 | +work=$(mktemp -d "${TMPDIR:-/tmp}/hop-index-check.XXXXXXXX") | |
| 9 | +trap 'rm -rf "$work"' EXIT HUP INT TERM | |
| 10 | +sed "s#HOP_DIR#$PWD#" toolchain/overlay.yaml > "$work/overlay.yaml" | |
| 11 | +swiftc -swift-version 5 -vfsoverlay "$work/overlay.yaml" -Xcc -ivfsoverlay -Xcc "$work/overlay.yaml" \ | |
| 12 | + Sources/RemoteIndex.swift tests/RemoteIndexChecks.swift -o "$work/print" | |
| 13 | +T=/tmp/hop-index-check.$$ | |
| 14 | +{ | |
| 15 | + echo "T=$T" | |
| 16 | + echo "CMD=$("$work/print" "$T/root")" | |
| 17 | + echo "NONE=$("$work/print" "$T/missing")" | |
| 18 | + cat <<'REMOTE' | |
| 19 | +set -u | |
| 20 | +fail=0 | |
| 21 | +mkdir -p "$T/root/a" "$T/root/locked" "$T/root/node_modules/x" | |
| 22 | +i=0; while [ $i -lt 300 ]; do : > "$T/root/a/file-with-a-long-enough-name-to-fill-the-list-$i"; i=$((i + 1)); done | |
| 23 | +chmod 000 "$T/root/locked" | |
| 24 | +check() { # NAME WANT(new|old) SCRIPT | |
| 25 | + H="$T/home-$1"; mkdir -p "$H/.cache/hop/building" "$H/.cache/hop/shards"; echo old > "$H/.cache/hop/shards/s00" | |
| 26 | + HOME="$H" sh -c "$3" 2>/dev/null | |
| 27 | + if [ "$(cat "$H/.cache/hop/shards/s00")" = old ]; then got=old; else got=new; fi | |
| 28 | + left=$(cd "$H/.cache/hop" && ls -d index.tmp index.err building shards.new 2>/dev/null | tr '\n' ' ') | |
| 29 | + if [ "$2" = new ] && ! cat "$H"/.cache/hop/shards/s* | grep -q "^$T/root/locked/\$"; then got="$got-without-locked"; fi | |
| 30 | + if [ "$2" = new ] && cat "$H"/.cache/hop/shards/s* | grep -q node_modules/x; then got="$got-with-pruned"; fi | |
| 31 | + if [ "$got" = "$2" ] && [ -z "$left" ]; then echo "PASS $1"; else echo "FAIL $1: want $2, got $got, left [$left]"; fail=1; fi | |
| 32 | +} | |
| 33 | +check unreadable-folder new "$CMD" | |
| 34 | +check plain-folder new "chmod 755 '$T/root/locked'; $CMD; chmod 000 '$T/root/locked'" | |
| 35 | +check write-error old "trap '' XFSZ; ulimit -f 4; $CMD" | |
| 36 | +check killed-find old "ulimit -f 4; $CMD" | |
| 37 | +check no-root old "$NONE" | |
| 38 | +chmod 755 "$T/root/locked"; rm -rf "$T" | |
| 39 | +[ $fail = 0 ] && echo "INDEX REBUILD PASS" || { echo "INDEX REBUILD FAIL"; exit 1; } | |
| 40 | +REMOTE | |
| 41 | +} | ssh -o BatchMode=yes "$host" sh -s |