@@ -65,10 +65,11 @@ cmd_init() {
6565 ;;
6666 esac
6767
68- # Generate output (cached to ~/.cache/gtr/, auto-invalidates on version change )
68+ # Generate output (cached to ~/.cache/gtr/, auto-invalidates on shell integration changes )
6969 local cache_dir=" ${XDG_CACHE_HOME:- $HOME / .cache} /gtr"
7070 local cache_file=" $cache_dir /init-${func_name} .${shell} "
71- local cache_stamp=" # gtr-cache: version=${GTR_VERSION:- unknown} func=$func_name shell=$shell "
71+ local cache_schema=" ${GTR_INIT_CACHE_VERSION:- 2} "
72+ local cache_stamp=" # gtr-cache: version=${GTR_VERSION:- unknown} init=${cache_schema} func=$func_name shell=$shell "
7273
7374 # Return cached output if version matches
7475 if [ -f " $cache_file " ]; then
@@ -94,6 +95,26 @@ _init_bash() {
9495# git-gtr shell integration (cached to ~/.cache/gtr/)
9596# Setup: see git gtr help init
9697
98+ __FUNC___gtrconfig_path() {
99+ local _gtr_git_common_dir _gtr_repo_root
100+ _gtr_git_common_dir="$(git rev-parse --git-common-dir 2>/dev/null)" || return 1
101+
102+ if [ "$_gtr_git_common_dir" = ".git" ]; then
103+ _gtr_repo_root="$(git rev-parse --show-toplevel 2>/dev/null)" || return 1
104+ else
105+ _gtr_repo_root="${_gtr_git_common_dir%/.git}"
106+ fi
107+
108+ printf '%s/.gtrconfig\n' "$_gtr_repo_root"
109+ }
110+
111+ __FUNC___hooks_hash() {
112+ local _gtr_config_file="$1"
113+ local _gtr_hook_defs
114+ _gtr_hook_defs="$(git config -f "$_gtr_config_file" --get-regexp '^hooks\.' 2>/dev/null)" || return 1
115+ printf '%s\n' "$_gtr_hook_defs" | shasum -a 256 | cut -d' ' -f1
116+ }
117+
97118__FUNC___run_post_cd_hooks() {
98119 local dir="$1"
99120 local _gtr_trust_dir="${XDG_CONFIG_HOME:-$HOME/.config}/gtr/trusted"
@@ -105,14 +126,14 @@ __FUNC___run_post_cd_hooks() {
105126 # Read from git config (local > global > system)
106127 _gtr_hooks="$(git config --get-all gtr.hook.postCd 2>/dev/null)" || true
107128 # Read from .gtrconfig if it exists — only if trusted
108- _gtr_config_file="$(git rev-parse --show-toplevel 2>/dev/null)/.gtrconfig"
129+ _gtr_config_file="$(__FUNC___gtrconfig_path 2>/dev/null)" || true
109130 if [ -f "$_gtr_config_file" ]; then
110131 local _gtr_file_hooks
111132 _gtr_file_hooks="$(git config -f "$_gtr_config_file" --get-all hooks.postCd 2>/dev/null)" || true
112133 if [ -n "$_gtr_file_hooks" ]; then
113134 # Verify trust before including .gtrconfig hooks
114135 local _gtr_hook_hash
115- _gtr_hook_hash="$(git config -f "$_gtr_config_file" --get-regexp '^hooks\.' 2>/dev/null | shasum -a 256 | cut -d' ' -f1 )" || true
136+ _gtr_hook_hash="$(__FUNC___hooks_hash "$_gtr_config_file" 2>/dev/null)" || true
116137 if [ -n "$_gtr_hook_hash" ] && [ -f "$_gtr_trust_dir/$_gtr_hook_hash" ]; then
117138 if [ -n "$_gtr_hooks" ]; then
118139 _gtr_hooks="$_gtr_hooks"$'\n'"$_gtr_file_hooks"
@@ -253,7 +274,7 @@ ___FUNC___completion() {
253274
254275 if [ "$COMP_CWORD" -eq 1 ]; then
255276 # First argument: cd + all git-gtr subcommands
256- COMPREPLY=($(compgen -W "cd new go run copy editor ai rm mv rename ls list clean doctor adapter config completion init help version" -- "$cur"))
277+ COMPREPLY=($(compgen -W "cd new go run copy editor ai rm mv rename ls list clean doctor adapter config completion init trust help version" -- "$cur"))
257278 elif [ "${COMP_WORDS[1]}" = "cd" ] && [ "$COMP_CWORD" -eq 2 ]; then
258279 # Worktree names for cd
259280 local worktrees
@@ -278,6 +299,28 @@ _init_zsh() {
278299# git-gtr shell integration (cached to ~/.cache/gtr/)
279300# Setup: see git gtr help init
280301
302+ __FUNC___gtrconfig_path() {
303+ emulate -L zsh
304+ local _gtr_git_common_dir _gtr_repo_root
305+ _gtr_git_common_dir="$(git rev-parse --git-common-dir 2>/dev/null)" || return 1
306+
307+ if [ "$_gtr_git_common_dir" = ".git" ]; then
308+ _gtr_repo_root="$(git rev-parse --show-toplevel 2>/dev/null)" || return 1
309+ else
310+ _gtr_repo_root="${_gtr_git_common_dir%/.git}"
311+ fi
312+
313+ printf '%s/.gtrconfig\n' "$_gtr_repo_root"
314+ }
315+
316+ __FUNC___hooks_hash() {
317+ emulate -L zsh
318+ local _gtr_config_file="$1"
319+ local _gtr_hook_defs
320+ _gtr_hook_defs="$(git config -f "$_gtr_config_file" --get-regexp '^hooks\.' 2>/dev/null)" || return 1
321+ printf '%s\n' "$_gtr_hook_defs" | shasum -a 256 | cut -d' ' -f1
322+ }
323+
281324__FUNC___run_post_cd_hooks() {
282325 emulate -L zsh
283326 local dir="$1"
@@ -290,14 +333,14 @@ __FUNC___run_post_cd_hooks() {
290333 # Read from git config (local > global > system)
291334 _gtr_hooks="$(git config --get-all gtr.hook.postCd 2>/dev/null)" || true
292335 # Read from .gtrconfig if it exists — only if trusted
293- _gtr_config_file="$(git rev-parse --show-toplevel 2>/dev/null)/.gtrconfig"
336+ _gtr_config_file="$(__FUNC___gtrconfig_path 2>/dev/null)" || true
294337 if [ -f "$_gtr_config_file" ]; then
295338 local _gtr_file_hooks
296339 _gtr_file_hooks="$(git config -f "$_gtr_config_file" --get-all hooks.postCd 2>/dev/null)" || true
297340 if [ -n "$_gtr_file_hooks" ]; then
298341 # Verify trust before including .gtrconfig hooks
299342 local _gtr_hook_hash
300- _gtr_hook_hash="$(git config -f "$_gtr_config_file" --get-regexp '^hooks\.' 2>/dev/null | shasum -a 256 | cut -d' ' -f1 )" || true
343+ _gtr_hook_hash="$(__FUNC___hooks_hash "$_gtr_config_file" 2>/dev/null)" || true
301344 if [ -n "$_gtr_hook_hash" ] && [ -f "$_gtr_trust_dir/$_gtr_hook_hash" ]; then
302345 if [ -n "$_gtr_hooks" ]; then
303346 _gtr_hooks="$_gtr_hooks"$'\n'"$_gtr_file_hooks"
@@ -465,6 +508,26 @@ _init_fish() {
465508# Add to ~/.config/fish/config.fish:
466509# git gtr init fish | source
467510
511+ function __FUNC___gtrconfig_path
512+ set -l _gtr_git_common_dir (git rev-parse --git-common-dir 2>/dev/null)
513+ test -n "$_gtr_git_common_dir"; or return 1
514+
515+ if test "$_gtr_git_common_dir" = ".git"
516+ set -l _gtr_repo_root (git rev-parse --show-toplevel 2>/dev/null)
517+ test -n "$_gtr_repo_root"; or return 1
518+ printf '%s/.gtrconfig\n' "$_gtr_repo_root"
519+ else
520+ printf '%s/.gtrconfig\n' (string replace -r '/\\.git$' '' -- "$_gtr_git_common_dir")
521+ end
522+ end
523+
524+ function __FUNC___hooks_hash
525+ set -l _gtr_config_file "$argv[1]"
526+ set -l _gtr_hook_defs (git config -f "$_gtr_config_file" --get-regexp '^hooks\.' 2>/dev/null)
527+ test $status -eq 0; or return 1
528+ printf '%s\n' "$_gtr_hook_defs" | shasum -a 256 | cut -d' ' -f1
529+ end
530+
468531function __FUNC___run_post_cd_hooks
469532 set -l dir "$argv[1]"
470533 set -l _gtr_trust_dir "$HOME/.config/gtr/trusted"
@@ -478,13 +541,13 @@ function __FUNC___run_post_cd_hooks
478541 # Read from git config (local > global > system)
479542 set -l _gtr_git_hooks (git config --get-all gtr.hook.postCd 2>/dev/null)
480543 # Read from .gtrconfig if it exists — only if trusted
481- set -l _gtr_config_file (git rev-parse --show-toplevel 2>/dev/null)"/.gtrconfig"
544+ set -l _gtr_config_file (__FUNC___gtrconfig_path 2>/dev/null)
482545 set -l _gtr_file_hooks
483546 if test -f "$_gtr_config_file"
484547 set -l _gtr_candidate_hooks (git config -f "$_gtr_config_file" --get-all hooks.postCd 2>/dev/null)
485548 if test (count $_gtr_candidate_hooks) -gt 0
486549 # Verify trust before including .gtrconfig hooks
487- set -l _gtr_hook_hash (git config -f "$_gtr_config_file" --get-regexp '^hooks\.' 2>/dev/null | shasum -a 256 | cut -d' ' -f1 )
550+ set -l _gtr_hook_hash (__FUNC___hooks_hash "$_gtr_config_file" 2>/dev/null)
488551 if test -n "$_gtr_hook_hash"; and test -f "$_gtr_trust_dir/$_gtr_hook_hash"
489552 set _gtr_file_hooks $_gtr_candidate_hooks
490553 else
0 commit comments