Skip to content

Commit eb0339d

Browse files
committed
Address CodeRabbit review: set -e safety, docs accuracy, test isolation
- Make cache write best-effort in cmd_init (print output first, guard mkdir/write with || true) so set -e failures don't swallow shell init - Add || true guards to eval/source in README and help.sh setup examples to protect users with set -e in their shell rc - Fix misleading "auto-invalidates on update" wording in help text — the cached setup pattern only regenerates when the cache file is missing - Simplify cache tests to use setup()'s XDG_CACHE_HOME instead of a shared per-PID cache_dir that can leak between tests on failure
1 parent 64d7402 commit eb0339d

File tree

4 files changed

+28
-36
lines changed

4 files changed

+28
-36
lines changed

README.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -216,13 +216,13 @@ cd "$(git gtr go 1)" # Navigate to main repo
216216
```bash
217217
# Bash (add to ~/.bashrc)
218218
_gtr_init="${XDG_CACHE_HOME:-$HOME/.cache}/gtr/init-gtr.bash"
219-
[[ -f "$_gtr_init" ]] || eval "$(git gtr init bash)"
220-
source "$_gtr_init" 2>/dev/null; unset _gtr_init
219+
[[ -f "$_gtr_init" ]] || eval "$(git gtr init bash)" || true
220+
source "$_gtr_init" 2>/dev/null || true; unset _gtr_init
221221

222222
# Zsh (add to ~/.zshrc)
223223
_gtr_init="${XDG_CACHE_HOME:-$HOME/.cache}/gtr/init-gtr.zsh"
224-
[[ -f "$_gtr_init" ]] || eval "$(git gtr init zsh)"
225-
source "$_gtr_init" 2>/dev/null; unset _gtr_init
224+
[[ -f "$_gtr_init" ]] || eval "$(git gtr init zsh)" || true
225+
source "$_gtr_init" 2>/dev/null || true; unset _gtr_init
226226

227227
# Fish (add to ~/.config/fish/config.fish)
228228
git gtr init fish | source
@@ -233,7 +233,7 @@ gtr cd my-feature
233233
gtr cd 1
234234
```
235235

236-
The cache auto-regenerates on first run and auto-invalidates when git-gtr is updated. To force-regenerate: `rm -rf ~/.cache/gtr`
236+
The cache generates on first run and refreshes the next time `git gtr init <shell>` runs. To force-regenerate: `rm -rf ~/.cache/gtr`
237237

238238
With [fzf](https://github.com/junegunn/fzf) installed, `gtr cd` (no arguments) opens a command palette with git log preview and keybindings: `ctrl-e` editor, `ctrl-a` AI, `ctrl-d` delete, `ctrl-y` copy, `ctrl-r` refresh.
239239

lib/commands/help.sh

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -347,8 +347,9 @@ Generates shell functions for enhanced features like 'gtr cd <branch>'
347347
which changes directory to a worktree. Add to your shell configuration.
348348
349349
Output is cached to ~/.cache/gtr/ for fast shell startup (~1ms vs ~60ms).
350-
The cache auto-invalidates when git-gtr is updated. To force-regenerate:
351-
rm -rf ~/.cache/gtr
350+
The cache refreshes the next time 'git gtr init <shell>' runs (checks version).
351+
With the recommended setup below, it regenerates when the cache file is missing.
352+
To force-regenerate: rm -rf ~/.cache/gtr
352353
353354
Supported shells: bash, zsh, fish
354355
@@ -359,13 +360,13 @@ Options:
359360
Setup (sources cached output directly for fast startup):
360361
# Bash (add to ~/.bashrc)
361362
_gtr_init="${XDG_CACHE_HOME:-$HOME/.cache}/gtr/init-gtr.bash"
362-
[[ -f "$_gtr_init" ]] || eval "$(git gtr init bash)"
363-
source "$_gtr_init" 2>/dev/null; unset _gtr_init
363+
[[ -f "$_gtr_init" ]] || eval "$(git gtr init bash)" || true
364+
source "$_gtr_init" 2>/dev/null || true; unset _gtr_init
364365
365366
# Zsh (add to ~/.zshrc)
366367
_gtr_init="${XDG_CACHE_HOME:-$HOME/.cache}/gtr/init-gtr.zsh"
367-
[[ -f "$_gtr_init" ]] || eval "$(git gtr init zsh)"
368-
source "$_gtr_init" 2>/dev/null; unset _gtr_init
368+
[[ -f "$_gtr_init" ]] || eval "$(git gtr init zsh)" || true
369+
source "$_gtr_init" 2>/dev/null || true; unset _gtr_init
369370
370371
# Fish (add to ~/.config/fish/config.fish)
371372
git gtr init fish | source

lib/commands/init.sh

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,12 +80,13 @@ cmd_init() {
8080
fi
8181
fi
8282

83-
# Generate, cache, and output
83+
# Generate, output, and cache (output first so set -e cache failures don't swallow it)
8484
local output
8585
output="$("$generator" | sed "s/__FUNC__/$func_name/g")"
86-
mkdir -p "$cache_dir"
87-
printf '%s\n%s\n' "$cache_stamp" "$output" > "$cache_file"
8886
printf '%s\n' "$output"
87+
if mkdir -p "$cache_dir" 2>/dev/null; then
88+
printf '%s\n%s\n' "$cache_stamp" "$output" > "$cache_file" 2>/dev/null || true
89+
fi
8990
}
9091

9192
_init_bash() {

tests/init.bats

Lines changed: 12 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -507,59 +507,49 @@ teardown() {
507507
# ── caching (default behavior) ──────────────────────────────────────────────
508508
509509
@test "init creates cache file and returns output" {
510-
local cache_dir="$BATS_TMPDIR/gtr-cache-test-$$"
511-
XDG_CACHE_HOME="$cache_dir" GTR_VERSION="9.9.9" run cmd_init zsh
510+
GTR_VERSION="9.9.9" run cmd_init zsh
512511
[ "$status" -eq 0 ]
513512
[[ "$output" == *"gtr()"* ]]
514-
[ -f "$cache_dir/gtr/init-gtr.zsh" ]
515-
rm -rf "$cache_dir"
513+
[ -f "$XDG_CACHE_HOME/gtr/init-gtr.zsh" ]
516514
}
517515
518516
@test "init returns cached output on second call" {
519-
local cache_dir="$BATS_TMPDIR/gtr-cache-test-$$"
520517
# First call: generates and caches
521-
XDG_CACHE_HOME="$cache_dir" GTR_VERSION="9.9.9" run cmd_init bash
518+
GTR_VERSION="9.9.9" run cmd_init bash
522519
[ "$status" -eq 0 ]
523520
local first_output="$output"
524521
# Second call: reads from cache
525-
XDG_CACHE_HOME="$cache_dir" GTR_VERSION="9.9.9" run cmd_init bash
522+
GTR_VERSION="9.9.9" run cmd_init bash
526523
[ "$status" -eq 0 ]
527524
[ "$output" = "$first_output" ]
528-
rm -rf "$cache_dir"
529525
}
530526
531527
@test "cache invalidates when version changes" {
532-
local cache_dir="$BATS_TMPDIR/gtr-cache-test-$$"
533528
# Generate with version 1.0.0
534-
XDG_CACHE_HOME="$cache_dir" GTR_VERSION="1.0.0" run cmd_init zsh
529+
GTR_VERSION="1.0.0" run cmd_init zsh
535530
[ "$status" -eq 0 ]
536531
# Check cache stamp
537532
local stamp
538-
stamp="$(head -1 "$cache_dir/gtr/init-gtr.zsh")"
533+
stamp="$(head -1 "$XDG_CACHE_HOME/gtr/init-gtr.zsh")"
539534
[[ "$stamp" == *"version=1.0.0"* ]]
540535
# Regenerate with version 2.0.0
541-
XDG_CACHE_HOME="$cache_dir" GTR_VERSION="2.0.0" run cmd_init zsh
536+
GTR_VERSION="2.0.0" run cmd_init zsh
542537
[ "$status" -eq 0 ]
543-
stamp="$(head -1 "$cache_dir/gtr/init-gtr.zsh")"
538+
stamp="$(head -1 "$XDG_CACHE_HOME/gtr/init-gtr.zsh")"
544539
[[ "$stamp" == *"version=2.0.0"* ]]
545-
rm -rf "$cache_dir"
546540
}
547541
548542
@test "cache uses --as func name in cache key" {
549-
local cache_dir="$BATS_TMPDIR/gtr-cache-test-$$"
550-
XDG_CACHE_HOME="$cache_dir" GTR_VERSION="9.9.9" run cmd_init bash --as myfn
543+
GTR_VERSION="9.9.9" run cmd_init bash --as myfn
551544
[ "$status" -eq 0 ]
552545
[[ "$output" == *"myfn()"* ]]
553-
[ -f "$cache_dir/gtr/init-myfn.bash" ]
554-
rm -rf "$cache_dir"
546+
[ -f "$XDG_CACHE_HOME/gtr/init-myfn.bash" ]
555547
}
556548
557549
@test "cache works for all shells" {
558-
local cache_dir="$BATS_TMPDIR/gtr-cache-test-$$"
559550
for sh in bash zsh fish; do
560-
XDG_CACHE_HOME="$cache_dir" GTR_VERSION="9.9.9" run cmd_init "$sh"
551+
GTR_VERSION="9.9.9" run cmd_init "$sh"
561552
[ "$status" -eq 0 ]
562-
[ -f "$cache_dir/gtr/init-gtr.${sh}" ]
553+
[ -f "$XDG_CACHE_HOME/gtr/init-gtr.${sh}" ]
563554
done
564-
rm -rf "$cache_dir"
565555
}

0 commit comments

Comments
 (0)