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 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/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" diff --git a/spec/tests/test_40_worktree_lifecycle.sh b/spec/tests/test_40_worktree_lifecycle.sh new file mode 100644 index 0000000..076e059 --- /dev/null +++ b/spec/tests/test_40_worktree_lifecycle.sh @@ -0,0 +1,181 @@ +# 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_description" "successful worktree operation" "$wt_script" "tui_spec.md#rename-and-delete-git-worktrees" + fi +} + +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 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 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') +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 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 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 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 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 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 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" 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. diff --git a/try.rb b/try.rb index c3b3c0a..c254589 100755 --- a/try.rb +++ b/try.rb @@ -1572,23 +1572,34 @@ 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])}" } + 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 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 @@ -1598,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)