Skip to content

Commit 21985a1

Browse files
Oblomovspearce
authored andcommitted
git-gui: handle non-standard worktree locations
Don't rely on the git worktree being the updir of the gitdir, since it might not be. Instead, define (and use) a new _gitworktree global variable, setting it to $GIT_WORK_TREE if present, falling back to core.worktree if defined, and finally to whatever we guess the correct worktree is. Getting core.worktree requires the config from the alleged git dir _gitdir to be loaded early. Supporting non-standard worktree locations also breaks the git-gui assumption (made when calling gitk) that the worktree was the dirname of $_gitdir and that, by consequence, the git dir could be set to the tail of $_gitdir once we changed to the worktree root directory. Therefore, we need to export a GIT_DIR environment variable set to the full, normalized path of $_gitdir instead. We also skip changing to the worktree directory if it's empty (i.e. if we're working on a bare repository). Signed-off-by: Giuseppe Bilotta <[email protected]> Signed-off-by: Shawn O. Pearce <[email protected]>
1 parent ff07c3b commit 21985a1

File tree

1 file changed

+28
-9
lines changed

1 file changed

+28
-9
lines changed

git-gui.sh

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@ unset oguimsg
121121

122122
set _appname {Git Gui}
123123
set _gitdir {}
124+
set _gitworktree {}
124125
set _gitexec {}
125126
set _githtmldir {}
126127
set _reponame {}
@@ -1100,25 +1101,41 @@ if {![file isdirectory $_gitdir]} {
11001101
error_popup [strcat [mc "Git directory not found:"] "\n\n$_gitdir"]
11011102
exit 1
11021103
}
1104+
# _gitdir exists, so try loading the config
1105+
load_config 0
1106+
apply_config
1107+
# try to set work tree from environment, falling back to core.worktree
1108+
if {[catch { set _gitworktree $env(GIT_WORK_TREE) }]} {
1109+
set _gitworktree [get_config core.worktree]
1110+
}
11031111
if {$_prefix ne {}} {
1104-
regsub -all {[^/]+/} $_prefix ../ cdup
1112+
if {$_gitworktree eq {}} {
1113+
regsub -all {[^/]+/} $_prefix ../ cdup
1114+
} else {
1115+
set cdup $_gitworktree
1116+
}
11051117
if {[catch {cd $cdup} err]} {
11061118
catch {wm withdraw .}
11071119
error_popup [strcat [mc "Cannot move to top of working directory:"] "\n\n$err"]
11081120
exit 1
11091121
}
1122+
set _gitworktree [pwd]
11101123
unset cdup
11111124
} elseif {![is_enabled bare]} {
11121125
if {[lindex [file split $_gitdir] end] ne {.git}} {
11131126
catch {wm withdraw .}
11141127
error_popup [strcat [mc "Cannot use funny .git directory:"] "\n\n$_gitdir"]
11151128
exit 1
11161129
}
1117-
if {[catch {cd [file dirname $_gitdir]} err]} {
1130+
if {$_gitworktree eq {}} {
1131+
set _gitworktree [file dirname $_gitdir]
1132+
}
1133+
if {[catch {cd $_gitworktree} err]} {
11181134
catch {wm withdraw .}
1119-
error_popup [strcat [mc "No working directory"] " [file dirname $_gitdir]:\n\n$err"]
1135+
error_popup [strcat [mc "No working directory"] " $_gitworktree:\n\n$err"]
11201136
exit 1
11211137
}
1138+
set _gitworktree [pwd]
11221139
}
11231140
set _reponame [file split [file normalize $_gitdir]]
11241141
if {[lindex $_reponame end] eq {.git}} {
@@ -1935,6 +1952,7 @@ set starting_gitk_msg [mc "Starting gitk... please wait..."]
19351952
19361953
proc do_gitk {revs {is_submodule false}} {
19371954
global current_diff_path file_states current_diff_side ui_index
1955+
global _gitworktree
19381956
19391957
# -- Always start gitk through whatever we were loaded with. This
19401958
# lets us bypass using shell process on Windows systems.
@@ -1955,8 +1973,10 @@ proc do_gitk {revs {is_submodule false}} {
19551973
set pwd [pwd]
19561974
19571975
if {!$is_submodule} {
1958-
cd [file dirname [gitdir]]
1959-
set env(GIT_DIR) [file tail [gitdir]]
1976+
if {$_gitworktree ne {}} {
1977+
cd $_gitworktree
1978+
}
1979+
set env(GIT_DIR) [file normalize [gitdir]]
19601980
} else {
19611981
cd $current_diff_path
19621982
if {$revs eq {--}} {
@@ -2032,6 +2052,7 @@ proc do_git_gui {} {
20322052
}
20332053
20342054
proc do_explore {} {
2055+
global _gitworktree
20352056
set explorer {}
20362057
if {[is_Cygwin] || [is_Windows]} {
20372058
set explorer "explorer.exe"
@@ -2041,7 +2062,7 @@ proc do_explore {} {
20412062
# freedesktop.org-conforming system is our best shot
20422063
set explorer "xdg-open"
20432064
}
2044-
eval exec $explorer [list [file nativename [file dirname [gitdir]]]] &
2065+
eval exec $explorer $_gitworktree &
20452066
}
20462067
20472068
set is_quitting 0
@@ -2405,8 +2426,6 @@ proc show_less_context {} {
24052426
##
24062427
## ui construction
24072428
2408-
load_config 0
2409-
apply_config
24102429
set ui_comm {}
24112430
24122431
# -- Menu Bar
@@ -3492,7 +3511,7 @@ unset i
34923511
set file_lists($ui_index) [list]
34933512
set file_lists($ui_workdir) [list]
34943513
3495-
wm title . "[appname] ([reponame]) [file normalize [file dirname [gitdir]]]"
3514+
wm title . "[appname] ([reponame]) [file normalize $_gitworktree]"
34963515
focus -force $ui_comm
34973516
34983517
# -- Warn the user about environmental problems. Cygwin's Tcl

0 commit comments

Comments
 (0)