Skip to content

Commit a9fa11f

Browse files
Oblomovspearce
authored andcommitted
git-gui: set GIT_DIR and GIT_WORK_TREE after setup
Rather than juggling with the env var GIT_DIR around the invocation of gitk, set it and GIT_WORK_TREE after finishing setup, ensuring that any external tool works with the setup we're running with. This also allows us to remove a couple of conditionals when running gitk or git gui in a submodule, as we know that the variables are present and have to be unset and reset before and after the invocation. Signed-off-by: Giuseppe Bilotta <[email protected]> Signed-off-by: Shawn O. Pearce <[email protected]>
1 parent 3748b03 commit a9fa11f

File tree

1 file changed

+20
-23
lines changed

1 file changed

+20
-23
lines changed

git-gui.sh

Lines changed: 20 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1171,6 +1171,9 @@ if {[lindex $_reponame end] eq {.git}} {
11711171
set _reponame [lindex $_reponame end]
11721172
}
11731173
1174+
set env(GIT_DIR) $_gitdir
1175+
set env(GIT_WORK_TREE) $_gitworktree
1176+
11741177
######################################################################
11751178
##
11761179
## global init
@@ -1979,7 +1982,7 @@ set starting_gitk_msg [mc "Starting gitk... please wait..."]
19791982
19801983
proc do_gitk {revs {is_submodule false}} {
19811984
global current_diff_path file_states current_diff_side ui_index
1982-
global _gitworktree
1985+
global _gitdir _gitworktree
19831986
19841987
# -- Always start gitk through whatever we were loaded with. This
19851988
# lets us bypass using shell process on Windows systems.
@@ -1991,19 +1994,12 @@ proc do_gitk {revs {is_submodule false}} {
19911994
} else {
19921995
global env
19931996
1994-
if {[info exists env(GIT_DIR)]} {
1995-
set old_GIT_DIR $env(GIT_DIR)
1996-
} else {
1997-
set old_GIT_DIR {}
1998-
}
1999-
20001997
set pwd [pwd]
20011998
20021999
if {!$is_submodule} {
20032000
if {![is_bare]} {
20042001
cd $_gitworktree
20052002
}
2006-
set env(GIT_DIR) [file normalize [gitdir]]
20072003
} else {
20082004
cd $current_diff_path
20092005
if {$revs eq {--}} {
@@ -2024,15 +2020,18 @@ proc do_gitk {revs {is_submodule false}} {
20242020
}
20252021
set revs $old_sha1...$new_sha1
20262022
}
2027-
if {[info exists env(GIT_DIR)]} {
2028-
unset env(GIT_DIR)
2029-
}
2023+
# GIT_DIR and GIT_WORK_TREE for the submodule are not the ones
2024+
# we've been using for the main repository, so unset them.
2025+
# TODO we could make life easier (start up faster?) for gitk
2026+
# by setting these to the appropriate values to allow gitk
2027+
# to skip the heuristics to find their proper value
2028+
unset env(GIT_DIR)
2029+
unset env(GIT_WORK_TREE)
20302030
}
20312031
eval exec $cmd $revs "--" "--" &
20322032
2033-
if {$old_GIT_DIR ne {}} {
2034-
set env(GIT_DIR) $old_GIT_DIR
2035-
}
2033+
set env(GIT_DIR) $_gitdir
2034+
set env(GIT_WORK_TREE) $_gitworktree
20362035
cd $pwd
20372036
20382037
ui_status $::starting_gitk_msg
@@ -2053,22 +2052,20 @@ proc do_git_gui {} {
20532052
error_popup [mc "Couldn't find git gui in PATH"]
20542053
} else {
20552054
global env
2055+
global _gitdir _gitworktree
20562056
2057-
if {[info exists env(GIT_DIR)]} {
2058-
set old_GIT_DIR $env(GIT_DIR)
2059-
unset env(GIT_DIR)
2060-
} else {
2061-
set old_GIT_DIR {}
2062-
}
2057+
# see note in do_gitk about unsetting these vars when
2058+
# running tools in a submodule
2059+
unset env(GIT_DIR)
2060+
unset env(GIT_WORK_TREE)
20632061
20642062
set pwd [pwd]
20652063
cd $current_diff_path
20662064
20672065
eval exec $exe gui &
20682066
2069-
if {$old_GIT_DIR ne {}} {
2070-
set env(GIT_DIR) $old_GIT_DIR
2071-
}
2067+
set env(GIT_DIR) $_gitdir
2068+
set env(GIT_WORK_TREE) $_gitworktree
20722069
cd $pwd
20732070
20742071
ui_status $::starting_gitk_msg

0 commit comments

Comments
 (0)