@matheusbsbs / matheus-bagliano / commits / 21985a1136
Fork 0

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 <giuseppe.bilotta@gmail.com> Signed-off-by: Shawn O. Pearce <spearce@spearce.org>

Giuseppe Bilotta committed Jan 23, 2010 at 11:03 UTC 21985a11360fc845b73f8300a8739b7b073f0627
1 file changed +28 -9
git-gui.sh
+28 -9
@@ -121,6 +121,7 @@ unset oguimsg
121
122 set _appname {Git Gui}
123 set _gitdir {}
124 +set _gitworktree {}
125 set _gitexec {}
126 set _githtmldir {}
127 set _reponame {}
@@ -1100,13 +1101,25 @@ if {![file isdirectory $_gitdir]} {
1101 error_popup [strcat [mc "Git directory not found:"] "\n\n$_gitdir"]
1102 exit 1
1103 }
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 +}
1111 if {$_prefix ne {}} {
1104 - regsub -all {[^/]+/} $_prefix ../ cdup
1112 + if {$_gitworktree eq {}} {
1113 + regsub -all {[^/]+/} $_prefix ../ cdup
1114 + } else {
1115 + set cdup $_gitworktree
1116 + }
1117 if {[catch {cd $cdup} err]} {
1118 catch {wm withdraw .}
1119 error_popup [strcat [mc "Cannot move to top of working directory:"] "\n\n$err"]
1120 exit 1
1121 }
1122 + set _gitworktree [pwd]
1123 unset cdup
1124 } elseif {![is_enabled bare]} {
1125 if {[lindex [file split $_gitdir] end] ne {.git}} {
@@ -1114,11 +1127,15 @@ if {$_prefix ne {}} {
1127 error_popup [strcat [mc "Cannot use funny .git directory:"] "\n\n$_gitdir"]
1128 exit 1
1129 }
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]} {
1134 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"]
1136 exit 1
1137 }
1138 + set _gitworktree [pwd]
1139 }
1140 set _reponame [file split [file normalize $_gitdir]]
1141 if {[lindex $_reponame end] eq {.git}} {
@@ -1935,6 +1952,7 @@ set starting_gitk_msg [mc "Starting gitk... please wait..."]
1952
1953 proc do_gitk {revs {is_submodule false}} {
1954 global current_diff_path file_states current_diff_side ui_index
1955 + global _gitworktree
1956
1957 # -- Always start gitk through whatever we were loaded with. This
1958 # lets us bypass using shell process on Windows systems.
@@ -1955,8 +1973,10 @@ proc do_gitk {revs {is_submodule false}} {
1973 set pwd [pwd]
1974
1975 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]]
1980 } else {
1981 cd $current_diff_path
1982 if {$revs eq {--}} {
@@ -2032,6 +2052,7 @@ proc do_git_gui {} {
2052 }
2053
2054 proc do_explore {} {
2055 + global _gitworktree
2056 set explorer {}
2057 if {[is_Cygwin] || [is_Windows]} {
2058 set explorer "explorer.exe"
@@ -2041,7 +2062,7 @@ proc do_explore {} {
2062 # freedesktop.org-conforming system is our best shot
2063 set explorer "xdg-open"
2064 }
2044 - eval exec $explorer [list [file nativename [file dirname [gitdir]]]] &
2065 + eval exec $explorer $_gitworktree &
2066 }
2067
2068 set is_quitting 0
@@ -2405,8 +2426,6 @@ proc show_less_context {} {
2426 ##
2427 ## ui construction
2428
2408 -load_config 0
2409 -apply_config
2429 set ui_comm {}
2430
2431 # -- Menu Bar
@@ -3492,7 +3511,7 @@ unset i
3511 set file_lists($ui_index) [list]
3512 set file_lists($ui_workdir) [list]
3513
3495 -wm title . "[appname] ([reponame]) [file normalize [file dirname [gitdir]]]"
3514 +wm title . "[appname] ([reponame]) [file normalize $_gitworktree]"
3515 focus -force $ui_comm
3516
3517 # -- Warn the user about environmental problems. Cygwin's Tcl