Skip to content

Commit 65c2fa3

Browse files
committed
Fix gitdir e.g. to respect core.hooksPath
Since v2.9.0, Git knows about the config variable core.hookspath that allows overriding the path to the directory containing the Git hooks. Since v2.10.0, the `--git-path` option respects that config variable, too, so we may just as well use that command. For Git versions older than v2.5.0 (which was the first version to support the `--git-path` option for the `rev-parse` command), we simply fall back to the previous code. An original patch handled only the hooksPath setting, however, during the code submission it was deemed better to fix all call to the `gitdir` function. To avoid spawning a gazillion `git rev-parse --git-path` instances, we cache the returned paths, priming the cache upon startup in a single `git rev-parse invocation` with the known entries. This fixes git-for-windows#1755 Initial-patch-by: Philipp Gortan <philipp@gortan.org> Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
1 parent 60c60b6 commit 65c2fa3

File tree

1 file changed

+49
-3
lines changed

1 file changed

+49
-3
lines changed

git-gui.sh

Lines changed: 49 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,7 @@ if {[tk windowingsystem] eq "aqua"} {
158158

159159
set _appname {Git Gui}
160160
set _gitdir {}
161+
array set _gitdir_cache {}
161162
set _gitworktree {}
162163
set _isbare {}
163164
set _gitexec {}
@@ -197,12 +198,57 @@ proc appname {} {
197198
return $_appname
198199
}
199200

201+
proc prime_gitdir_cache {} {
202+
global _gitdir _gitdir_cache
203+
204+
set gitdir_cmd [list git rev-parse --git-dir]
205+
206+
# `--git-path` is only supported since Git v2.5.0
207+
if {[package vcompare $::_git_version 2.5.0] >= 0} {
208+
set gitdir_keys [list \
209+
CHERRY_PICK_HEAD FETCH_HEAD GITGUI_BCK GITGUI_EDITMSG \
210+
GITGUI_MSG HEAD hooks hooks/prepare-commit-msg \
211+
index.lock info info/exclude logs MERGE_HEAD MERGE_MSG \
212+
MERGE_RR objects "objects/4\[0-1\]/*" \
213+
"objects/4\[0-3\]/*" objects/info \
214+
objects/info/alternates objects/pack packed-refs \
215+
PREPARE_COMMIT_MSG rebase-merge/head-name remotes \
216+
rr-cache rr-cache/MERGE_RR SQUASH_MSG \
217+
]
218+
219+
foreach key $gitdir_keys {
220+
lappend gitdir_cmd --git-path $key
221+
}
222+
}
223+
224+
set i -1
225+
foreach path [split [eval $gitdir_cmd] "\n"] {
226+
if {$i eq -1} {
227+
set _gitdir $path
228+
} else {
229+
set _gitdir_cache([lindex $gitdir_keys $i]) $path
230+
}
231+
incr i
232+
}
233+
}
234+
200235
proc gitdir {args} {
201-
global _gitdir
236+
global _gitdir _gitdir_cache
237+
202238
if {$args eq {}} {
203239
return $_gitdir
204240
}
205-
return [eval [list file join $_gitdir] $args]
241+
242+
set args [eval [list file join] $args]
243+
if {![info exists _gitdir_cache($args)]} {
244+
if {[package vcompare $::_git_version 2.5.0] >= 0} {
245+
set _gitdir_cache($args) [git rev-parse --git-path $args]
246+
} else {
247+
set _gitdir_cache($args) [file join $_gitdir $args]
248+
}
249+
}
250+
251+
return $_gitdir_cache($args)
206252
}
207253

208254
proc gitexec {args} {
@@ -1242,7 +1288,7 @@ if {[catch {
12421288
&& [catch {
12431289
# beware that from the .git dir this sets _gitdir to .
12441290
# and _prefix to the empty string
1245-
set _gitdir [git rev-parse --git-dir]
1291+
prime_gitdir_cache
12461292
set _prefix [git rev-parse --show-prefix]
12471293
} err]} {
12481294
load_config 1

0 commit comments

Comments
 (0)