Skip to content

Commit 0746d9d

Browse files
thiagowfxclaude
andcommitted
feat(try): "try -" deletes the workspace containing the current directory
Bare "-" resolves $PWD to the workspace under $tries_path and dispatches through the existing delete flow. Works from any subdirectory of the workspace; errors clearly when called outside the tries root or at the root itself. Switched path canonicalization to "pwd -P" so /tmp vs /private/tmp comparisons on macOS work. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
1 parent 0096e58 commit 0746d9d

2 files changed

Lines changed: 35 additions & 3 deletions

File tree

try/README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ try +myproject # Create workspace named myproject
2424
try + # Create workspace with random name
2525
try -myproject # Delete workspace matching myproject
2626
try --delete react # Delete workspace matching react
27+
try - # Delete the workspace you're currently inside
2728
try -p ~/projects # Use custom workspace path
2829
try -l # List all workspaces
2930
```
@@ -61,6 +62,9 @@ try -myproject
6162
# Delete via explicit long form (handy when name collides with flags)
6263
try --delete myproject
6364

65+
# Delete the workspace you're currently inside
66+
try -
67+
6468
# Use custom base directory
6569
try -p /tmp/experiments
6670

try/try.sh

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ EXAMPLES:
3333
$cmd + Create workspace with random name
3434
$cmd -myproject Delete workspace matching myproject
3535
$cmd --delete react Delete workspace matching react
36+
$cmd - Delete the workspace containing the current directory
3637
$cmd -p ~/projects Use custom workspace path
3738
$cmd -l List all workspaces
3839
@@ -241,7 +242,7 @@ delete_workspace() {
241242

242243
# Defensive: ensure resolved path stays inside tries_path
243244
local resolved
244-
resolved=$(cd "$full_path" 2>/dev/null && pwd) || {
245+
resolved=$(cd "$full_path" 2>/dev/null && pwd -P) || {
245246
echo "Error: cannot resolve $full_path"
246247
return 1
247248
}
@@ -252,7 +253,8 @@ delete_workspace() {
252253

253254
# Warn if caller's CWD is inside the workspace being deleted - the parent
254255
# shell will be left in a stale directory (we can't cd it from here).
255-
local caller_pwd="${PWD:-}"
256+
local caller_pwd
257+
caller_pwd=$(pwd -P 2>/dev/null || echo "")
256258
if [[ -n "$caller_pwd" && ( "$caller_pwd" == "$resolved" || "$caller_pwd" == "$resolved"/* ) ]]; then
257259
echo "Warning: your shell is inside $resolved - cd elsewhere before/after to avoid a stale CWD"
258260
fi
@@ -285,6 +287,7 @@ main() {
285287
local search_term=""
286288
local list_mode=false
287289
local delete_name=""
290+
local delete_cwd=false
288291

289292
# Parse options
290293
while [[ $# -gt 0 ]]; do
@@ -311,6 +314,11 @@ main() {
311314
search_term="+${1:-}"
312315
shift || true
313316
;;
317+
-)
318+
# Bare "-" deletes the workspace containing $PWD
319+
delete_cwd=true
320+
shift
321+
;;
314322
-*)
315323
# -NAME shorthand for deletion (parallels +NAME for creation)
316324
delete_name="${1#-}"
@@ -324,14 +332,34 @@ main() {
324332
done
325333

326334
mkdir -p "$tries_path"
327-
tries_path=$(cd "$tries_path" && pwd)
335+
tries_path=$(cd "$tries_path" && pwd -P)
328336

329337
# If --list mode, display workspaces and exit
330338
if [[ "$list_mode" == true ]]; then
331339
list_workspaces "$tries_path"
332340
exit $?
333341
fi
334342

343+
# Resolve bare "-" to the workspace containing $PWD
344+
if [[ "$delete_cwd" == true ]]; then
345+
local cwd_resolved
346+
cwd_resolved=$(pwd -P 2>/dev/null) || {
347+
echo "Error: cannot resolve current directory"
348+
exit 1
349+
}
350+
if [[ "$cwd_resolved" == "$tries_path" ]]; then
351+
echo "Error: at workspace root - cd into a workspace first (cwd: $cwd_resolved)"
352+
exit 1
353+
fi
354+
if [[ "$cwd_resolved" != "$tries_path"/* ]]; then
355+
echo "Error: not inside $tries_path (cwd: $cwd_resolved)"
356+
exit 1
357+
fi
358+
# First path component after tries_path is the workspace name
359+
local remainder="${cwd_resolved#"$tries_path"/}"
360+
delete_name="${remainder%%/*}"
361+
fi
362+
335363
# If delete requested, run delete flow and exit
336364
if [[ -n "$delete_name" ]]; then
337365
delete_workspace "$tries_path" "$delete_name"

0 commit comments

Comments
 (0)