From 91bcc3a07bede7108ce384b1badcf0e5c705dfe4 Mon Sep 17 00:00:00 2001 From: Victor Deryagin Date: Sat, 24 Oct 2026 15:49:22 +0300 Subject: [PATCH 1/6] more reliable worktree detection --- try.rb | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/try.rb b/try.rb index c3b3c0a..7ca2f9f 100755 --- a/try.rb +++ b/try.rb @@ -1572,6 +1572,14 @@ def script_worktree(path, repo = nil) ["mkdir -p #{q(path)}", "echo #{q("Using git worktree to create this trial from #{src}.")}", worktree_cmd] + script_cd(path) end + def linked_worktree?(path) + return false if File.symlink?(path) || !File.file?(File.join(path, '.git')) + + # Submodules and --separate-git-dir checkouts also have a .git file. + dirs = `git -C #{q(path)} rev-parse --git-dir --git-common-dir 2>/dev/null`.lines.map(&:strip) + dirs.length == 2 && File.expand_path(dirs[0], path) != File.expand_path(dirs[1], path) + end + def script_delete(paths, base_path) cmds = ["cd #{q(base_path)}"] paths.each { |item| cmds << "test -d #{q(item[:basename])} && rm -rf #{q(item[:basename])}" } @@ -1581,14 +1589,9 @@ def script_delete(paths, base_path) def script_ascend(source, dest, basename, base_path) symlink_path = File.join(base_path, basename) - # Check if source is a git worktree (has .git file, not directory) - git_file = File.join(source, '.git') - is_worktree = File.file?(git_file) - cmds = [] - if is_worktree - # Use git worktree move for proper bookkeeping - cmds << "git worktree move #{q(source)} #{q(dest)}" + if linked_worktree?(source) + cmds << "git -C #{q(source)} worktree move -- #{q(source)} #{q(dest)}" else cmds << "mv #{q(source)} #{q(dest)}" end From 6ee090453cf4816e994a3ac7a94a677c2613f1b3 Mon Sep 17 00:00:00 2001 From: Victor Deryagin Date: Sat, 24 Oct 2026 15:49:51 +0300 Subject: [PATCH 2/6] graduate test update --- spec/tests/test_37_graduate.sh | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/spec/tests/test_37_graduate.sh b/spec/tests/test_37_graduate.sh index e81aba4..511882b 100644 --- a/spec/tests/test_37_graduate.sh +++ b/spec/tests/test_37_graduate.sh @@ -81,10 +81,13 @@ fi # Test: Graduate with git worktree uses git worktree move WT_DIR=$(mktemp -d) -mkdir -p "$WT_DIR/2025-06-01-worktree-test" -echo "gitdir: /tmp/fake-repo/.git/worktrees/worktree-test" > "$WT_DIR/2025-06-01-worktree-test/.git" +WT_REPO=$(mktemp -d) +git -C "$WT_REPO" init -q +git -C "$WT_REPO" -c user.name=Test -c user.email=test@example.com \ + -c commit.gpgsign=false -c core.hooksPath=/dev/null commit --allow-empty -qm initial +git -C "$WT_REPO" worktree add -q --detach "$WT_DIR/2025-06-01-worktree-test" output=$(try_run --path="$WT_DIR" --and-keys='CTRL-G,ENTER' exec 2>/dev/null) -if echo "$output" | grep -q "git worktree move"; then +if echo "$output" | grep -q "worktree move"; then pass else fail "Git worktree should use git worktree move" "git worktree move" "$output" "graduate" @@ -92,7 +95,7 @@ fi # Test: Regular directory uses plain mv (not git worktree move) output=$(try_run --path="$GRAD_DIR" --and-keys='CTRL-G,ENTER' exec 2>/dev/null) -if echo "$output" | grep -q "git worktree move"; then +if echo "$output" | grep -q "worktree move"; then fail "Non-worktree should use mv, not git worktree move" "plain mv" "$output" "graduate" else pass @@ -201,7 +204,7 @@ fi REG_DIR=$(mktemp -d) mkdir -p "$REG_DIR/2025-06-01-regular-repo/.git/objects" output=$(try_run --path="$REG_DIR" --and-keys='CTRL-G,ENTER' exec 2>/dev/null) -if echo "$output" | grep -q "git worktree move"; then +if echo "$output" | grep -q "worktree move"; then fail "Regular .git directory should use mv, not git worktree move" "plain mv" "$output" "graduate" else pass @@ -222,4 +225,4 @@ else fail "Should strip YYYY-MM-DD- prefix from various dates" "year-end" "$output" "graduate" fi -rm -rf "$GRAD_DIR" "$PROJ_DIR" "$NO_DATE_DIR" "$WT_DIR" "$EDIT_DIR" "$E2E_DIR" "$E2E_DEST" "$REG_DIR" "$MULTI_DIR" +rm -rf "$GRAD_DIR" "$PROJ_DIR" "$NO_DATE_DIR" "$WT_DIR" "$WT_REPO" "$EDIT_DIR" "$E2E_DIR" "$E2E_DEST" "$REG_DIR" "$MULTI_DIR" From 5b141c03e66f8b2ad991bd7e4eee708829b4ca4d Mon Sep 17 00:00:00 2001 From: Victor Deryagin Date: Sat, 24 Oct 2026 15:50:10 +0300 Subject: [PATCH 3/6] rename, delete: worktree support --- spec/tests/test_40_worktree_lifecycle.sh | 184 +++++++++++++++++++++++ try.rb | 18 ++- 2 files changed, 200 insertions(+), 2 deletions(-) create mode 100644 spec/tests/test_40_worktree_lifecycle.sh diff --git a/spec/tests/test_40_worktree_lifecycle.sh b/spec/tests/test_40_worktree_lifecycle.sh new file mode 100644 index 0000000..1bc9f72 --- /dev/null +++ b/spec/tests/test_40_worktree_lifecycle.sh @@ -0,0 +1,184 @@ +# Execute emitted scripts against real worktrees, outside their repository. +# Spec: tui_spec.md#rename-and-delete-git-worktrees, delete_spec.md + +section "worktree lifecycle" + +# Earlier specs may delete the directory they leave as the runner's cwd. +cd "$TEST_ROOT" || return 1 + +wt_emit() { + set -- "--path=$wt_tries" "--and-keys=$1" exec + eval $TRY_CMD '"$@"' 2>/dev/null +} + +wt_check() { + if "$@" >/dev/null 2>&1; then + pass + else + fail "$wt_shell: $wt_description" "successful worktree operation" "$wt_script" "tui_spec.md#rename-and-delete-git-worktrees" + fi +} + +for wt_shell in bash zsh fish; do + command -v "$wt_shell" >/dev/null 2>&1 || continue + wt_root="$TEST_ROOT/worktree-lifecycle-$wt_shell" + mkdir -p "$wt_root" + wt_root=$(cd "$wt_root" && pwd -P) + wt_repo="$wt_root/owner's repo" + wt_tries="$wt_root/owner's tries" + wt_old="$wt_tries/2025-01-01-owner's-experiment" + wt_new="$wt_tries/renamed" + mkdir -p "$wt_repo" "$wt_tries" + git -C "$wt_repo" init -q --initial-branch=main + echo committed > "$wt_repo/tracked.txt" + git -C "$wt_repo" add tracked.txt + git -C "$wt_repo" -c user.name=Test -c user.email=test@example.com \ + -c commit.gpgsign=false -c core.hooksPath=/dev/null commit -qm initial + git -C "$wt_repo" worktree add -q -b experiment "$wt_old" + + # Preserve staged, unstaged, and untracked files when moving a worktree. + echo staged > "$wt_old/tracked.txt" + git -C "$wt_old" add tracked.txt + echo unstaged >> "$wt_old/tracked.txt" + echo untracked > "$wt_old/untracked.txt" + wt_status=$(git -C "$wt_old" status --porcelain) + wt_script=$(wt_emit 'CTRL-R,CTRL-A,CTRL-K,TYPE=renamed,ENTER') + wt_description="rename emitted script runs from outside the repository" + wt_check "$wt_shell" -c "$wt_script" + wt_description="renamed worktree remains usable" + wt_check git -C "$wt_new" rev-parse --verify HEAD + wt_description="rename preserves staged and unstaged changes" + wt_check test "$(git -C "$wt_new" status --porcelain)" = "$wt_status" + wt_description="rename updates Git registration" + wt_check test "$(git -C "$wt_repo" worktree list --porcelain | grep -Fxc "worktree $wt_new")" = 1 + wt_description="rename removes the old path" + wt_check test ! -e "$wt_old" + wt_description="rename keeps the branch name" + wt_check test "$(git -C "$wt_new" branch --show-current)" = experiment + + # A graduated worktree's symlink must not act on the target. + wt_link="$wt_tries/shortcut" + ln -s "$wt_new" "$wt_link" + wt_script=$(wt_emit 'TYPE=shortcut,CTRL-R,CTRL-A,CTRL-K,TYPE=renamed-link,ENTER') + wt_description="rename a symlink to a worktree" + wt_check "$wt_shell" -c "$wt_script" + wt_description="renamed symlink still points at the worktree" + wt_check test "$(readlink "$wt_tries/renamed-link")" = "$wt_new" + wt_script=$(wt_emit 'TYPE=renamed-link,CTRL-D,ENTER,Y,E,S,ENTER') + wt_description="delete the symlink" + wt_check "$wt_shell" -c "$wt_script" + wt_description="deleting a symlink keeps its worktree target" + wt_check test -f "$wt_new/untracked.txt" + wt_description="delete removes the symlink" + wt_check test ! -L "$wt_tries/renamed-link" + + # Locked worktrees must not fall back to mv or rm -rf. + git -C "$wt_repo" worktree lock "$wt_new" + wt_script=$(wt_emit 'CTRL-R,CTRL-A,CTRL-K,TYPE=blocked,ENTER') + "$wt_shell" -c "$wt_script" >/dev/null 2>&1 + wt_description="locked worktree cannot be renamed" + wt_check test -f "$wt_new/untracked.txt" + wt_description="failed rename creates no destination" + wt_check test ! -e "$wt_tries/blocked" + wt_script=$(wt_emit 'CTRL-D,ENTER,Y,E,S,ENTER') + "$wt_shell" -c "$wt_script" >/dev/null 2>&1 + wt_description="locked worktree cannot be deleted" + wt_check test -f "$wt_new/untracked.txt" + git -C "$wt_repo" worktree unlock "$wt_new" + + # The original YES confirmation is still required for worktrees. + wt_script=$(wt_emit 'CTRL-D,ENTER,N,O,ENTER') + wt_description="declining deletion emits no removal command" + wt_check test "$(printf '%s' "$wt_script" | grep -c 'worktree remove\|rm -rf')" = 0 + wt_script=$(wt_emit 'CTRL-D,ENTER,Y,E,S,ENTER') + wt_description="confirmed deletion removes a dirty worktree" + wt_check "$wt_shell" -c "$wt_script" + wt_description="delete removes the directory" + wt_check test ! -e "$wt_new" + wt_description="delete removes Git registration" + wt_check test "$(git -C "$wt_repo" worktree list --porcelain | grep -c '^worktree ')" = 1 + wt_description="delete keeps the experiment branch" + wt_check git -C "$wt_repo" show-ref --verify --quiet refs/heads/experiment + + # A .git file also occurs in standalone repositories with a separate git dir. + wt_separate="$wt_tries/separate" + git init -q --separate-git-dir="$wt_root/separate.git" "$wt_separate" + echo keep > "$wt_separate/file.txt" + wt_script=$(wt_emit 'CTRL-R,CTRL-A,CTRL-K,TYPE=standalone,ENTER') + wt_description="standalone repo with a .git file can be renamed" + wt_check "$wt_shell" -c "$wt_script" + wt_description="standalone repository content survives rename" + wt_check test -f "$wt_tries/standalone/file.txt" + wt_script=$(wt_emit 'CTRL-D,ENTER,Y,E,S,ENTER') + wt_description="standalone repo with a .git file can be deleted" + wt_check "$wt_shell" -c "$wt_script" + wt_description="standalone repo directory was deleted" + wt_check test ! -e "$wt_tries/standalone" + + # One batch may contain plain directories, standalone repos, and worktrees. + git -C "$wt_repo" worktree add -q -b batch "$wt_tries/worktree" + mkdir "$wt_tries/plain" + git init -q "$wt_tries/repo" + wt_script=$(wt_emit 'CTRL-D,DOWN,CTRL-D,DOWN,CTRL-D,ENTER,Y,E,S,ENTER') + wt_description="mixed batch deletion succeeds" + wt_check "$wt_shell" -c "$wt_script" + wt_description="mixed batch removes all entries" + wt_check test "$(ls -A "$wt_tries" | wc -l | tr -d ' ')" = 0 + wt_description="mixed batch unregisters the worktree" + wt_check test "$(git -C "$wt_repo" worktree list --porcelain | grep -c '^worktree ')" = 1 + + # Stock try creates detached worktrees rather than named branches. + wt_detached="$wt_tries/detached" + git -C "$wt_repo" worktree add -q --detach "$wt_detached" + wt_head=$(git -C "$wt_detached" rev-parse HEAD) + wt_script=$(wt_emit 'CTRL-R,CTRL-A,CTRL-K,TYPE=renamed-detached,ENTER') + wt_description="rename detached worktree" + wt_check "$wt_shell" -c "$wt_script" + wt_detached="$wt_tries/renamed-detached" + wt_description="renamed detached worktree keeps its HEAD" + wt_check test "$(git -C "$wt_detached" rev-parse HEAD)" = "$wt_head" + wt_description="rename leaves the worktree detached" + wt_check test -z "$(git -C "$wt_detached" branch --show-current)" + wt_description="rename updates detached worktree registration" + wt_check test "$(git -C "$wt_repo" worktree list --porcelain | grep -Fxc "worktree $wt_detached")" = 1 + wt_script=$(wt_emit 'CTRL-D,ENTER,Y,E,S,ENTER') + wt_description="delete detached worktree" + wt_check "$wt_shell" -c "$wt_script" + wt_description="delete removes detached worktree directory" + wt_check test ! -e "$wt_detached" + wt_description="delete unregisters detached worktree" + wt_check test "$(git -C "$wt_repo" worktree list --porcelain | grep -c '^worktree ')" = 1 + + # Graduate uses the same detection and works outside the parent repository. + wt_grad="$wt_tries/2025-01-01-graduate" + wt_dest="$wt_root/owner's graduated project" + git -C "$wt_repo" worktree add -q --detach "$wt_grad" + echo untracked > "$wt_grad/untracked.txt" + wt_script=$(wt_emit "CTRL-G,CTRL-A,CTRL-K,TYPE=$wt_dest,ENTER") + wt_description="graduate worktree from outside its repository" + wt_check "$wt_shell" -c "$wt_script" + wt_description="graduation preserves worktree files" + wt_check test -f "$wt_dest/untracked.txt" + wt_description="graduated worktree remains usable" + wt_check git -C "$wt_dest" rev-parse --verify HEAD + wt_description="graduation leaves the worktree detached" + wt_check test -z "$(git -C "$wt_dest" branch --show-current)" + wt_description="graduation updates Git registration" + wt_check test "$(git -C "$wt_repo" worktree list --porcelain | grep -Fxc "worktree $wt_dest")" = 1 + wt_description="graduation leaves a symlink" + wt_check test "$(readlink "$wt_grad")" = "$wt_dest" + + # Separate Git directories must not be mistaken for linked worktrees. + wt_tries="$wt_root/standalone tries" + mkdir "$wt_tries" + git init -q --separate-git-dir="$wt_root/graduate.git" "$wt_tries/separate" + echo keep > "$wt_tries/separate/file.txt" + wt_dest="$wt_root/standalone project" + wt_script=$(wt_emit "CTRL-G,CTRL-A,CTRL-K,TYPE=$wt_dest,ENTER") + wt_description="graduate standalone repository with a .git file" + wt_check "$wt_shell" -c "$wt_script" + wt_description="standalone graduation preserves files" + wt_check test -f "$wt_dest/file.txt" + wt_description="standalone graduation leaves a symlink" + wt_check test "$(readlink "$wt_tries/separate")" = "$wt_dest" +done diff --git a/try.rb b/try.rb index 7ca2f9f..c254589 100755 --- a/try.rb +++ b/try.rb @@ -1582,7 +1582,15 @@ def linked_worktree?(path) def script_delete(paths, base_path) cmds = ["cd #{q(base_path)}"] - paths.each { |item| cmds << "test -d #{q(item[:basename])} && rm -rf #{q(item[:basename])}" } + paths.each do |item| + name = item[:basename] + path = File.join(base_path, name) + if linked_worktree?(path) + cmds << "test -d #{q(name)} && git -C #{q(path)} worktree remove --force -- #{q(path)}" + else + cmds << "test -d #{q(name)} && rm -rf #{q(name)}" + end + end cmds << "cd #{q(Dir.pwd)} 2>/dev/null || cd #{q(base_path)}" cmds end @@ -1601,10 +1609,16 @@ def script_ascend(source, dest, basename, base_path) end def script_rename(base_path, old_name, new_name) + old_path = File.join(base_path, old_name) new_path = File.join(base_path, new_name) + move_cmd = if linked_worktree?(old_path) + "git -C #{q(old_path)} worktree move -- #{q(old_path)} #{q(new_path)}" + else + "mv #{q(old_name)} #{q(new_name)}" + end [ "cd #{q(base_path)}", - "mv #{q(old_name)} #{q(new_name)}", + move_cmd, "echo #{q(new_path)}", "cd #{q(new_path)}" ] + terminal_rename_commands(new_path) From e109e2720792a9ab128f8acc7325498b044b8521 Mon Sep 17 00:00:00 2001 From: Victor Deryagin Date: Sat, 24 Oct 2026 15:51:00 +0300 Subject: [PATCH 4/6] add Ctrl-R to readme --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 4eb0e66..a8ab896 100644 --- a/README.md +++ b/README.md @@ -194,6 +194,7 @@ automatically removed from URLs when generating directory names. - `↑/↓` or `Ctrl-P/N/J/K` - Navigate - `Enter` - Select or create - `Backspace` - Delete character +- `Ctrl-R` - Rename directory - `Ctrl-D` - Delete directory (with confirmation) - `ESC` - Cancel - Just type to filter From a83895e364113e1b4000a446e8556d40a85a637e Mon Sep 17 00:00:00 2001 From: Victor Deryagin Date: Sat, 24 Oct 2026 15:52:45 +0300 Subject: [PATCH 5/6] adjust specs --- spec/delete_spec.md | 9 +++++++++ spec/tui_spec.md | 12 ++++++++++++ 2 files changed, 21 insertions(+) diff --git a/spec/delete_spec.md b/spec/delete_spec.md index 26313bc..1d77985 100644 --- a/spec/delete_spec.md +++ b/spec/delete_spec.md @@ -81,6 +81,15 @@ Each command is on its own line, chained with `&& \` for readability, with 2-spa - Use basename only (not full path) - Each on its own line with continuation + For linked worktrees, remove the directory and Git registration: + ```sh + test -d 'name' && git -C '/path/to/tries/name' worktree remove --force -- '/path/to/tries/name' && \ + ``` + `YES` also confirms deletion of uncommitted and untracked files. Keep named + branches, protect locked worktrees, and never fall back to `rm -rf`. + Symlinks lose only the link. Submodules and `--separate-git-dir` repositories + keep ordinary deletion behavior. + 3. **PWD restoration** ```sh cd '/original/pwd' 2>/dev/null || cd '/path/to/tries' diff --git a/spec/tui_spec.md b/spec/tui_spec.md index 8ae787e..77796a0 100644 --- a/spec/tui_spec.md +++ b/spec/tui_spec.md @@ -307,6 +307,18 @@ Delete is a multi-step batch operation: - Must type `YES` to proceed with deletion - Any other input cancels +### Rename and delete Git worktrees + +- Rename (`Ctrl-R`) and graduate (`Ctrl-G`) use `git -C worktree move`. + Update Git's stored path and preserve the branch or detached HEAD. +- Delete (`Ctrl-D`) uses `git -C worktree remove --force` after `YES`. + Remove the directory, uncommitted and untracked files, and Git registration. + Keep any named branch. +- All three share linked worktree detection and work outside the repository. + Protect locked worktrees. Never fall back to filesystem operations on Git failure. +- Other directories and repositories keep ordinary behavior, including submodules + and `--separate-git-dir` repositories. Symlink rename/delete affects only the link. + ### Delete Script Output In exec mode, delete outputs a shell script (like all other actions). The script is evaluated by the shell wrapper, not executed directly by try. From 8c7a38fb892b8b796ef710de10fc5a730f76098c Mon Sep 17 00:00:00 2001 From: Victor Deryagin Date: Sat, 24 Oct 2026 16:06:15 +0300 Subject: [PATCH 6/6] simplify tests --- spec/tests/test_40_worktree_lifecycle.sh | 307 +++++++++++------------ 1 file changed, 152 insertions(+), 155 deletions(-) diff --git a/spec/tests/test_40_worktree_lifecycle.sh b/spec/tests/test_40_worktree_lifecycle.sh index 1bc9f72..076e059 100644 --- a/spec/tests/test_40_worktree_lifecycle.sh +++ b/spec/tests/test_40_worktree_lifecycle.sh @@ -15,170 +15,167 @@ wt_check() { if "$@" >/dev/null 2>&1; then pass else - fail "$wt_shell: $wt_description" "successful worktree operation" "$wt_script" "tui_spec.md#rename-and-delete-git-worktrees" + fail "$wt_description" "successful worktree operation" "$wt_script" "tui_spec.md#rename-and-delete-git-worktrees" fi } -for wt_shell in bash zsh fish; do - command -v "$wt_shell" >/dev/null 2>&1 || continue - wt_root="$TEST_ROOT/worktree-lifecycle-$wt_shell" - mkdir -p "$wt_root" - wt_root=$(cd "$wt_root" && pwd -P) - wt_repo="$wt_root/owner's repo" - wt_tries="$wt_root/owner's tries" - wt_old="$wt_tries/2025-01-01-owner's-experiment" - wt_new="$wt_tries/renamed" - mkdir -p "$wt_repo" "$wt_tries" - git -C "$wt_repo" init -q --initial-branch=main - echo committed > "$wt_repo/tracked.txt" - git -C "$wt_repo" add tracked.txt - git -C "$wt_repo" -c user.name=Test -c user.email=test@example.com \ - -c commit.gpgsign=false -c core.hooksPath=/dev/null commit -qm initial - git -C "$wt_repo" worktree add -q -b experiment "$wt_old" +wt_root="$TEST_ROOT/worktree-lifecycle" +mkdir -p "$wt_root" +wt_root=$(cd "$wt_root" && pwd -P) +wt_repo="$wt_root/owner's repo" +wt_tries="$wt_root/owner's tries" +wt_old="$wt_tries/2025-01-01-owner's-experiment" +wt_new="$wt_tries/renamed" +mkdir -p "$wt_repo" "$wt_tries" +git -C "$wt_repo" init -q --initial-branch=main +echo committed > "$wt_repo/tracked.txt" +git -C "$wt_repo" add tracked.txt +git -C "$wt_repo" -c user.name=Test -c user.email=test@example.com \ + -c commit.gpgsign=false -c core.hooksPath=/dev/null commit -qm initial +git -C "$wt_repo" worktree add -q -b experiment "$wt_old" - # Preserve staged, unstaged, and untracked files when moving a worktree. - echo staged > "$wt_old/tracked.txt" - git -C "$wt_old" add tracked.txt - echo unstaged >> "$wt_old/tracked.txt" - echo untracked > "$wt_old/untracked.txt" - wt_status=$(git -C "$wt_old" status --porcelain) - wt_script=$(wt_emit 'CTRL-R,CTRL-A,CTRL-K,TYPE=renamed,ENTER') - wt_description="rename emitted script runs from outside the repository" - wt_check "$wt_shell" -c "$wt_script" - wt_description="renamed worktree remains usable" - wt_check git -C "$wt_new" rev-parse --verify HEAD - wt_description="rename preserves staged and unstaged changes" - wt_check test "$(git -C "$wt_new" status --porcelain)" = "$wt_status" - wt_description="rename updates Git registration" - wt_check test "$(git -C "$wt_repo" worktree list --porcelain | grep -Fxc "worktree $wt_new")" = 1 - wt_description="rename removes the old path" - wt_check test ! -e "$wt_old" - wt_description="rename keeps the branch name" - wt_check test "$(git -C "$wt_new" branch --show-current)" = experiment +# Preserve staged, unstaged, and untracked files when moving a worktree. +echo staged > "$wt_old/tracked.txt" +git -C "$wt_old" add tracked.txt +echo unstaged >> "$wt_old/tracked.txt" +echo untracked > "$wt_old/untracked.txt" +wt_status=$(git -C "$wt_old" status --porcelain) +wt_script=$(wt_emit 'CTRL-R,CTRL-A,CTRL-K,TYPE=renamed,ENTER') +wt_description="rename emitted script runs from outside the repository" +wt_check bash -c "$wt_script" +wt_description="renamed worktree remains usable" +wt_check git -C "$wt_new" rev-parse --verify HEAD +wt_description="rename preserves staged and unstaged changes" +wt_check test "$(git -C "$wt_new" status --porcelain)" = "$wt_status" +wt_description="rename updates Git registration" +wt_check test "$(git -C "$wt_repo" worktree list --porcelain | grep -Fxc "worktree $wt_new")" = 1 +wt_description="rename removes the old path" +wt_check test ! -e "$wt_old" +wt_description="rename keeps the branch name" +wt_check test "$(git -C "$wt_new" branch --show-current)" = experiment - # A graduated worktree's symlink must not act on the target. - wt_link="$wt_tries/shortcut" - ln -s "$wt_new" "$wt_link" - wt_script=$(wt_emit 'TYPE=shortcut,CTRL-R,CTRL-A,CTRL-K,TYPE=renamed-link,ENTER') - wt_description="rename a symlink to a worktree" - wt_check "$wt_shell" -c "$wt_script" - wt_description="renamed symlink still points at the worktree" - wt_check test "$(readlink "$wt_tries/renamed-link")" = "$wt_new" - wt_script=$(wt_emit 'TYPE=renamed-link,CTRL-D,ENTER,Y,E,S,ENTER') - wt_description="delete the symlink" - wt_check "$wt_shell" -c "$wt_script" - wt_description="deleting a symlink keeps its worktree target" - wt_check test -f "$wt_new/untracked.txt" - wt_description="delete removes the symlink" - wt_check test ! -L "$wt_tries/renamed-link" +# A graduated worktree's symlink must not act on the target. +wt_link="$wt_tries/shortcut" +ln -s "$wt_new" "$wt_link" +wt_script=$(wt_emit 'TYPE=shortcut,CTRL-R,CTRL-A,CTRL-K,TYPE=renamed-link,ENTER') +wt_description="rename a symlink to a worktree" +wt_check bash -c "$wt_script" +wt_description="renamed symlink still points at the worktree" +wt_check test "$(readlink "$wt_tries/renamed-link")" = "$wt_new" +wt_script=$(wt_emit 'TYPE=renamed-link,CTRL-D,ENTER,Y,E,S,ENTER') +wt_description="delete the symlink" +wt_check bash -c "$wt_script" +wt_description="deleting a symlink keeps its worktree target" +wt_check test -f "$wt_new/untracked.txt" +wt_description="delete removes the symlink" +wt_check test ! -L "$wt_tries/renamed-link" - # Locked worktrees must not fall back to mv or rm -rf. - git -C "$wt_repo" worktree lock "$wt_new" - wt_script=$(wt_emit 'CTRL-R,CTRL-A,CTRL-K,TYPE=blocked,ENTER') - "$wt_shell" -c "$wt_script" >/dev/null 2>&1 - wt_description="locked worktree cannot be renamed" - wt_check test -f "$wt_new/untracked.txt" - wt_description="failed rename creates no destination" - wt_check test ! -e "$wt_tries/blocked" - wt_script=$(wt_emit 'CTRL-D,ENTER,Y,E,S,ENTER') - "$wt_shell" -c "$wt_script" >/dev/null 2>&1 - wt_description="locked worktree cannot be deleted" - wt_check test -f "$wt_new/untracked.txt" - git -C "$wt_repo" worktree unlock "$wt_new" +# Locked worktrees must not fall back to mv or rm -rf. +git -C "$wt_repo" worktree lock "$wt_new" +wt_script=$(wt_emit 'CTRL-R,CTRL-A,CTRL-K,TYPE=blocked,ENTER') +bash -c "$wt_script" >/dev/null 2>&1 +wt_description="locked worktree cannot be renamed" +wt_check test -f "$wt_new/untracked.txt" +wt_description="failed rename creates no destination" +wt_check test ! -e "$wt_tries/blocked" +wt_script=$(wt_emit 'CTRL-D,ENTER,Y,E,S,ENTER') +bash -c "$wt_script" >/dev/null 2>&1 +wt_description="locked worktree cannot be deleted" +wt_check test -f "$wt_new/untracked.txt" +git -C "$wt_repo" worktree unlock "$wt_new" - # The original YES confirmation is still required for worktrees. - wt_script=$(wt_emit 'CTRL-D,ENTER,N,O,ENTER') - wt_description="declining deletion emits no removal command" - wt_check test "$(printf '%s' "$wt_script" | grep -c 'worktree remove\|rm -rf')" = 0 - wt_script=$(wt_emit 'CTRL-D,ENTER,Y,E,S,ENTER') - wt_description="confirmed deletion removes a dirty worktree" - wt_check "$wt_shell" -c "$wt_script" - wt_description="delete removes the directory" - wt_check test ! -e "$wt_new" - wt_description="delete removes Git registration" - wt_check test "$(git -C "$wt_repo" worktree list --porcelain | grep -c '^worktree ')" = 1 - wt_description="delete keeps the experiment branch" - wt_check git -C "$wt_repo" show-ref --verify --quiet refs/heads/experiment +# The original YES confirmation is still required for worktrees. +wt_script=$(wt_emit 'CTRL-D,ENTER,N,O,ENTER') +wt_description="declining deletion emits no removal command" +wt_check test "$(printf '%s' "$wt_script" | grep -c 'worktree remove\|rm -rf')" = 0 +wt_script=$(wt_emit 'CTRL-D,ENTER,Y,E,S,ENTER') +wt_description="confirmed deletion removes a dirty worktree" +wt_check bash -c "$wt_script" +wt_description="delete removes the directory" +wt_check test ! -e "$wt_new" +wt_description="delete removes Git registration" +wt_check test "$(git -C "$wt_repo" worktree list --porcelain | grep -c '^worktree ')" = 1 +wt_description="delete keeps the experiment branch" +wt_check git -C "$wt_repo" show-ref --verify --quiet refs/heads/experiment - # A .git file also occurs in standalone repositories with a separate git dir. - wt_separate="$wt_tries/separate" - git init -q --separate-git-dir="$wt_root/separate.git" "$wt_separate" - echo keep > "$wt_separate/file.txt" - wt_script=$(wt_emit 'CTRL-R,CTRL-A,CTRL-K,TYPE=standalone,ENTER') - wt_description="standalone repo with a .git file can be renamed" - wt_check "$wt_shell" -c "$wt_script" - wt_description="standalone repository content survives rename" - wt_check test -f "$wt_tries/standalone/file.txt" - wt_script=$(wt_emit 'CTRL-D,ENTER,Y,E,S,ENTER') - wt_description="standalone repo with a .git file can be deleted" - wt_check "$wt_shell" -c "$wt_script" - wt_description="standalone repo directory was deleted" - wt_check test ! -e "$wt_tries/standalone" +# A .git file also occurs in standalone repositories with a separate git dir. +wt_separate="$wt_tries/separate" +git init -q --separate-git-dir="$wt_root/separate.git" "$wt_separate" +echo keep > "$wt_separate/file.txt" +wt_script=$(wt_emit 'CTRL-R,CTRL-A,CTRL-K,TYPE=standalone,ENTER') +wt_description="standalone repo with a .git file can be renamed" +wt_check bash -c "$wt_script" +wt_description="standalone repository content survives rename" +wt_check test -f "$wt_tries/standalone/file.txt" +wt_script=$(wt_emit 'CTRL-D,ENTER,Y,E,S,ENTER') +wt_description="standalone repo with a .git file can be deleted" +wt_check bash -c "$wt_script" +wt_description="standalone repo directory was deleted" +wt_check test ! -e "$wt_tries/standalone" - # One batch may contain plain directories, standalone repos, and worktrees. - git -C "$wt_repo" worktree add -q -b batch "$wt_tries/worktree" - mkdir "$wt_tries/plain" - git init -q "$wt_tries/repo" - wt_script=$(wt_emit 'CTRL-D,DOWN,CTRL-D,DOWN,CTRL-D,ENTER,Y,E,S,ENTER') - wt_description="mixed batch deletion succeeds" - wt_check "$wt_shell" -c "$wt_script" - wt_description="mixed batch removes all entries" - wt_check test "$(ls -A "$wt_tries" | wc -l | tr -d ' ')" = 0 - wt_description="mixed batch unregisters the worktree" - wt_check test "$(git -C "$wt_repo" worktree list --porcelain | grep -c '^worktree ')" = 1 +# One batch may contain plain directories, standalone repos, and worktrees. +git -C "$wt_repo" worktree add -q -b batch "$wt_tries/worktree" +mkdir "$wt_tries/plain" +git init -q "$wt_tries/repo" +wt_script=$(wt_emit 'CTRL-D,DOWN,CTRL-D,DOWN,CTRL-D,ENTER,Y,E,S,ENTER') +wt_description="mixed batch deletion succeeds" +wt_check bash -c "$wt_script" +wt_description="mixed batch removes all entries" +wt_check test "$(ls -A "$wt_tries" | wc -l | tr -d ' ')" = 0 +wt_description="mixed batch unregisters the worktree" +wt_check test "$(git -C "$wt_repo" worktree list --porcelain | grep -c '^worktree ')" = 1 - # Stock try creates detached worktrees rather than named branches. - wt_detached="$wt_tries/detached" - git -C "$wt_repo" worktree add -q --detach "$wt_detached" - wt_head=$(git -C "$wt_detached" rev-parse HEAD) - wt_script=$(wt_emit 'CTRL-R,CTRL-A,CTRL-K,TYPE=renamed-detached,ENTER') - wt_description="rename detached worktree" - wt_check "$wt_shell" -c "$wt_script" - wt_detached="$wt_tries/renamed-detached" - wt_description="renamed detached worktree keeps its HEAD" - wt_check test "$(git -C "$wt_detached" rev-parse HEAD)" = "$wt_head" - wt_description="rename leaves the worktree detached" - wt_check test -z "$(git -C "$wt_detached" branch --show-current)" - wt_description="rename updates detached worktree registration" - wt_check test "$(git -C "$wt_repo" worktree list --porcelain | grep -Fxc "worktree $wt_detached")" = 1 - wt_script=$(wt_emit 'CTRL-D,ENTER,Y,E,S,ENTER') - wt_description="delete detached worktree" - wt_check "$wt_shell" -c "$wt_script" - wt_description="delete removes detached worktree directory" - wt_check test ! -e "$wt_detached" - wt_description="delete unregisters detached worktree" - wt_check test "$(git -C "$wt_repo" worktree list --porcelain | grep -c '^worktree ')" = 1 +# Stock try creates detached worktrees rather than named branches. +wt_detached="$wt_tries/detached" +git -C "$wt_repo" worktree add -q --detach "$wt_detached" +wt_head=$(git -C "$wt_detached" rev-parse HEAD) +wt_script=$(wt_emit 'CTRL-R,CTRL-A,CTRL-K,TYPE=renamed-detached,ENTER') +wt_description="rename detached worktree" +wt_check bash -c "$wt_script" +wt_detached="$wt_tries/renamed-detached" +wt_description="renamed detached worktree keeps its HEAD" +wt_check test "$(git -C "$wt_detached" rev-parse HEAD)" = "$wt_head" +wt_description="rename leaves the worktree detached" +wt_check test -z "$(git -C "$wt_detached" branch --show-current)" +wt_description="rename updates detached worktree registration" +wt_check test "$(git -C "$wt_repo" worktree list --porcelain | grep -Fxc "worktree $wt_detached")" = 1 +wt_script=$(wt_emit 'CTRL-D,ENTER,Y,E,S,ENTER') +wt_description="delete detached worktree" +wt_check bash -c "$wt_script" +wt_description="delete removes detached worktree directory" +wt_check test ! -e "$wt_detached" +wt_description="delete unregisters detached worktree" +wt_check test "$(git -C "$wt_repo" worktree list --porcelain | grep -c '^worktree ')" = 1 - # Graduate uses the same detection and works outside the parent repository. - wt_grad="$wt_tries/2025-01-01-graduate" - wt_dest="$wt_root/owner's graduated project" - git -C "$wt_repo" worktree add -q --detach "$wt_grad" - echo untracked > "$wt_grad/untracked.txt" - wt_script=$(wt_emit "CTRL-G,CTRL-A,CTRL-K,TYPE=$wt_dest,ENTER") - wt_description="graduate worktree from outside its repository" - wt_check "$wt_shell" -c "$wt_script" - wt_description="graduation preserves worktree files" - wt_check test -f "$wt_dest/untracked.txt" - wt_description="graduated worktree remains usable" - wt_check git -C "$wt_dest" rev-parse --verify HEAD - wt_description="graduation leaves the worktree detached" - wt_check test -z "$(git -C "$wt_dest" branch --show-current)" - wt_description="graduation updates Git registration" - wt_check test "$(git -C "$wt_repo" worktree list --porcelain | grep -Fxc "worktree $wt_dest")" = 1 - wt_description="graduation leaves a symlink" - wt_check test "$(readlink "$wt_grad")" = "$wt_dest" +# Graduate uses the same detection and works outside the parent repository. +wt_grad="$wt_tries/2025-01-01-graduate" +wt_dest="$wt_root/owner's graduated project" +git -C "$wt_repo" worktree add -q --detach "$wt_grad" +echo untracked > "$wt_grad/untracked.txt" +wt_script=$(wt_emit "CTRL-G,CTRL-A,CTRL-K,TYPE=$wt_dest,ENTER") +wt_description="graduate worktree from outside its repository" +wt_check bash -c "$wt_script" +wt_description="graduation preserves worktree files" +wt_check test -f "$wt_dest/untracked.txt" +wt_description="graduated worktree remains usable" +wt_check git -C "$wt_dest" rev-parse --verify HEAD +wt_description="graduation leaves the worktree detached" +wt_check test -z "$(git -C "$wt_dest" branch --show-current)" +wt_description="graduation updates Git registration" +wt_check test "$(git -C "$wt_repo" worktree list --porcelain | grep -Fxc "worktree $wt_dest")" = 1 +wt_description="graduation leaves a symlink" +wt_check test "$(readlink "$wt_grad")" = "$wt_dest" - # Separate Git directories must not be mistaken for linked worktrees. - wt_tries="$wt_root/standalone tries" - mkdir "$wt_tries" - git init -q --separate-git-dir="$wt_root/graduate.git" "$wt_tries/separate" - echo keep > "$wt_tries/separate/file.txt" - wt_dest="$wt_root/standalone project" - wt_script=$(wt_emit "CTRL-G,CTRL-A,CTRL-K,TYPE=$wt_dest,ENTER") - wt_description="graduate standalone repository with a .git file" - wt_check "$wt_shell" -c "$wt_script" - wt_description="standalone graduation preserves files" - wt_check test -f "$wt_dest/file.txt" - wt_description="standalone graduation leaves a symlink" - wt_check test "$(readlink "$wt_tries/separate")" = "$wt_dest" -done +# Separate Git directories must not be mistaken for linked worktrees. +wt_tries="$wt_root/standalone tries" +mkdir "$wt_tries" +git init -q --separate-git-dir="$wt_root/graduate.git" "$wt_tries/separate" +echo keep > "$wt_tries/separate/file.txt" +wt_dest="$wt_root/standalone project" +wt_script=$(wt_emit "CTRL-G,CTRL-A,CTRL-K,TYPE=$wt_dest,ENTER") +wt_description="graduate standalone repository with a .git file" +wt_check bash -c "$wt_script" +wt_description="standalone graduation preserves files" +wt_check test -f "$wt_dest/file.txt" +wt_description="standalone graduation leaves a symlink" +wt_check test "$(readlink "$wt_tries/separate")" = "$wt_dest"