Code

git-gui: handle non-standard worktree locations
[git.git] / git-gui.sh
index 26d75663018f8d89229fbf6b2dc7d0a9d16adcca..de089e38d79e5652259ee4bbbbc56e0b928b4567 100755 (executable)
@@ -121,6 +121,7 @@ unset oguimsg
 
 set _appname {Git Gui}
 set _gitdir {}
+set _gitworktree {}
 set _gitexec {}
 set _githtmldir {}
 set _reponame {}
@@ -1100,13 +1101,25 @@ if {![file isdirectory $_gitdir]} {
        error_popup [strcat [mc "Git directory not found:"] "\n\n$_gitdir"]
        exit 1
 }
+# _gitdir exists, so try loading the config
+load_config 0
+apply_config
+# try to set work tree from environment, falling back to core.worktree
+if {[catch { set _gitworktree $env(GIT_WORK_TREE) }]} {
+       set _gitworktree [get_config core.worktree]
+}
 if {$_prefix ne {}} {
-       regsub -all {[^/]+/} $_prefix ../ cdup
+       if {$_gitworktree eq {}} {
+               regsub -all {[^/]+/} $_prefix ../ cdup
+       } else {
+               set cdup $_gitworktree
+       }
        if {[catch {cd $cdup} err]} {
                catch {wm withdraw .}
                error_popup [strcat [mc "Cannot move to top of working directory:"] "\n\n$err"]
                exit 1
        }
+       set _gitworktree [pwd]
        unset cdup
 } elseif {![is_enabled bare]} {
        if {[lindex [file split $_gitdir] end] ne {.git}} {
@@ -1114,11 +1127,15 @@ if {$_prefix ne {}} {
                error_popup [strcat [mc "Cannot use funny .git directory:"] "\n\n$_gitdir"]
                exit 1
        }
-       if {[catch {cd [file dirname $_gitdir]} err]} {
+       if {$_gitworktree eq {}} {
+               set _gitworktree [file dirname $_gitdir]
+       }
+       if {[catch {cd $_gitworktree} err]} {
                catch {wm withdraw .}
-               error_popup [strcat [mc "No working directory"] " [file dirname $_gitdir]:\n\n$err"]
+               error_popup [strcat [mc "No working directory"] " $_gitworktree:\n\n$err"]
                exit 1
        }
+       set _gitworktree [pwd]
 }
 set _reponame [file split [file normalize $_gitdir]]
 if {[lindex $_reponame end] eq {.git}} {
@@ -1935,6 +1952,7 @@ set starting_gitk_msg [mc "Starting gitk... please wait..."]
 
 proc do_gitk {revs {is_submodule false}} {
        global current_diff_path file_states current_diff_side ui_index
+       global _gitworktree
 
        # -- Always start gitk through whatever we were loaded with.  This
        #    lets us bypass using shell process on Windows systems.
@@ -1955,8 +1973,10 @@ proc do_gitk {revs {is_submodule false}} {
                set pwd [pwd]
 
                if {!$is_submodule} {
-                       cd [file dirname [gitdir]]
-                       set env(GIT_DIR) [file tail [gitdir]]
+                       if {$_gitworktree ne {}} {
+                               cd $_gitworktree
+                       }
+                       set env(GIT_DIR) [file normalize [gitdir]]
                } else {
                        cd $current_diff_path
                        if {$revs eq {--}} {
@@ -2032,6 +2052,7 @@ proc do_git_gui {} {
 }
 
 proc do_explore {} {
+       global _gitworktree
        set explorer {}
        if {[is_Cygwin] || [is_Windows]} {
                set explorer "explorer.exe"
@@ -2041,7 +2062,7 @@ proc do_explore {} {
                # freedesktop.org-conforming system is our best shot
                set explorer "xdg-open"
        }
-       eval exec $explorer [list [file nativename [file dirname [gitdir]]]] &
+       eval exec $explorer $_gitworktree &
 }
 
 set is_quitting 0
@@ -2405,8 +2426,6 @@ proc show_less_context {} {
 ##
 ## ui construction
 
-load_config 0
-apply_config
 set ui_comm {}
 
 # -- Menu Bar
@@ -3492,7 +3511,7 @@ unset i
 set file_lists($ui_index) [list]
 set file_lists($ui_workdir) [list]
 
-wm title . "[appname] ([reponame]) [file normalize [file dirname [gitdir]]]"
+wm title . "[appname] ([reponame]) [file normalize $_gitworktree]"
 focus -force $ui_comm
 
 # -- Warn the user about environmental problems.  Cygwin's Tcl