Skip to contentSkip to conversation

git-gui: allow opening work trees from the startup dialog

PATCH Superseded Mikhail Terekhov via GitGitGadget 3 participants 6 messages May 26, 2020 – Jun 22, 2020 Library code

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 git-gui: allow opening work trees from the startup dialog

From: Mikhail Terekhov <[email protected]>

In proc _is_git check that supplied path is a valid work tree path. This allows the choose_repository::pick dialog to accept path to a work tree directory.

---
git-gui: allow opening work trees from the startup dialog

Trying to open a working tree from the "Open Existing Repository" link of the git-gui startup dialog results in an error: Not a Git repository:
.... This patch fixes that error.

Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-644%2Ftermim%2Fgit-gui-open-worktree-v1
Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-644/termim/git-gui-open-worktree-v1
Pull-Request: https://github.com/gitgitgadget/git/pull/644

 lib/choose_repository.tcl | 11 +++++++++++
 1 file changed, 11 insertions(+)
lib/choose_repository.tcl +11 -0
index e54f3e66d8f..8f911e89fbb 100644
@@ -362,8 +362,19 @@ proc _is_git {path {outdir_var ""}} {
 		gets $fp line
 		close $fp
 		if {[regexp "^gitdir: (.+)$" $line line link_target]} {
+			set check_path [file normalize $path]
 			set path [file join [file dirname $path] $link_target]
 			set path [file normalize $path]
+
+			if {[file exists [file join $path gitdir]]} {
+				set fp [open [file join $path gitdir] r]
+				gets $fp worktree_path
+				close $fp
+				if {[string equal $check_path $worktree_path]} {
+					set outdir $path
+					return 1
+				}
+			}
 		}
 	}
 

base-commit: c195247812f8cd38ba7e1c603112e6c1d8d8e71e

Signature · 1 line
gitgitgadget
#1 lore raw

Hi Mikhail,

Thanks for the patch.

Quoted 5 lines · Mikhail Terekhov via GitGitGadget

From: Mikhail Terekhov <[email protected]>

In proc _is_git check that supplied path is a valid work tree path. This allows the choose_repository::pick dialog to accept path to a work tree directory.

IIUC, you are referring to work trees other than the main work tree, correct?

I tested my theory, and you are right. Applying this patch allows me to open git-gui in a worktree.

Quoted 20 lines

---
git-gui: allow opening work trees from the startup dialog

Trying to open a working tree from the "Open Existing Repository" link of the git-gui startup dialog results in an error: Not a Git repository:
.... This patch fixes that error.

Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-644%2Ftermim%2Fgit-gui-open-worktree-v1
Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-644/termim/git-gui-open-worktree-v1
Pull-Request: https://github.com/gitgitgadget/git/pull/644

 lib/choose_repository.tcl | 11 +++++++++++
 1 file changed, 11 insertions(+)
lib/choose_repository.tcl +0 -0
index e54f3e66d8f..8f911e89fbb 100644

lib/choose_repository.tcl proc _is_git {path {outdir_var ""}} { · line 368

Quoted 7 lines
lib/choose_repository.tcl +2 -0
@@ -362,8 +362,19 @@ proc _is_git {path {outdir_var ""}} {
 		gets $fp line
 		close $fp
 		if {[regexp "^gitdir: (.+)$" $line line link_target]} {
+			set check_path [file normalize $path]
 			set path [file join [file dirname $path] $link_target]
 			set path [file normalize $path]
+

This should have a comment explaining we are checking for a worktree. Will add it locally.

lib/choose_repository.tcl proc _is_git {path {outdir_var ""}} { · line 377

Quoted 9 lines
lib/choose_repository.tcl +9 -0
+			if {[file exists [file join $path gitdir]]} {
+				set fp [open [file join $path gitdir] r]
+				gets $fp worktree_path
+				close $fp
+				if {[string equal $check_path $worktree_path]} {
+					set outdir $path
+					return 1
+				}
+			}

I wonder if there is a way of finding if the path is a worktree path using a Git plumbing command. That IMO would be better than rolling our own logic to check if something is a worktree.

I'll check once if I can figure something out (you do that too if you can). Otherwise I'll merge it in. Thanks.

Signature · 2 lines
Regards,
Pratyush Yadav
#2 lore raw
Quoted 12 lines · Pratyush Yadav

lib/choose_repository.tcl proc _is_git {path {outdir_var ""}} { · line 377

Quoted 10 lines · Mikhail Terekhov via GitGitGadget

From: Mikhail Terekhov <[email protected]>

lib/choose_repository.tcl +9 -0
+                     if {[file exists [file join $path gitdir]]} {
+                             set fp [open [file join $path gitdir] r]
+                             gets $fp worktree_path
+                             close $fp
+                             if {[string equal $check_path $worktree_path]} {
+                                     set outdir $path
+                                     return 1
+                             }
+                     }
Quoted 3 lines

I wonder if there is a way of finding if the path is a worktree path using a Git plumbing command. That IMO would be better than rolling our own logic to check if something is a worktree.

Perhaps use:

    (cd "$path" && git rev-parse --is-inside-work-tree)

That will return "true" for both the main worktree and a linked worktree. You may need to suppress stderr if you expect to run the command in an arbitrary directory since it will error out if the directory is not under Git's control.

#3 lore raw

PATCH v2 1/1 git-gui: allow opening work trees from the startup dialog

In reply to Mikhail Terekhov via GitGitGadget

From: Mikhail Terekhov <[email protected]>

In proc _is_git check that supplied path is a valid work tree path. This allows the choose_repository::pick dialog to accept path to a work tree directory.

---
git-gui: allow opening work trees from the startup dialog

Trying to open a working tree from the "Open Existing Repository" link of the git-gui startup dialog results in an error: Not a Git repository:
.... This patch fixes that error.

Changes since v1:

* Use git rev-parse instead of rolling our own logic to check if something is a worktree as suggested by Pratyush Yadav [email protected] [[email protected]] and Eric Sunshine [email protected] [[email protected]].

Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-644%2Ftermim%2Fgit-gui-open-worktree-v2
Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-644/termim/git-gui-open-worktree-v2
Pull-Request: https://github.com/gitgitgadget/git/pull/644

Range-diff vs v1:

 1:  ca55ec3adb4 < -:  ----------- git-gui: allow opening work trees from the startup dialog
 -:  ----------- > 1:  c3b378fe397 git-gui: allow opening work trees from the startup dialog
 lib/choose_repository.tcl | 29 +++++------------------------
 1 file changed, 5 insertions(+), 24 deletions(-)
lib/choose_repository.tcl +5 -24
index e54f3e66d8f..07f29b0b13b 100644
@@ -357,31 +357,12 @@ proc _is_git {path {outdir_var ""}} {
 	if {$outdir_var ne ""} {
 		upvar 1 $outdir_var outdir
 	}
-	if {[file isfile $path]} {
-		set fp [open $path r]
-		gets $fp line
-		close $fp
-		if {[regexp "^gitdir: (.+)$" $line line link_target]} {
-			set path [file join [file dirname $path] $link_target]
-			set path [file normalize $path]
-		}
-	}
-
-	if {[file exists [file join $path HEAD]]
-	 && [file exists [file join $path objects]]
-	 && [file exists [file join $path config]]} {
-		set outdir $path
-		return 1
-	}
-	if {[is_Cygwin]} {
-		if {[file exists [file join $path HEAD]]
-		 && [file exists [file join $path objects.lnk]]
-		 && [file exists [file join $path config.lnk]]} {
-			set outdir $path
-			return 1
-		}
+	if {[catch { 
+		set outdir [git rev-parse --resolve-git-dir $path] 
+		} ]} {
+			return 0
 	}
-	return 0
+	return 1
 }
 
 proc _objdir {path} {

base-commit: c195247812f8cd38ba7e1c603112e6c1d8d8e71e

Signature · 1 line
gitgitgadget
#4 lore raw

+Cc Eric.

Quoted 36 lines · Mikhail Terekhov via GitGitGadget

From: Mikhail Terekhov <[email protected]>

In proc _is_git check that supplied path is a valid work tree path. This allows the choose_repository::pick dialog to accept path to a work tree directory.

---
 lib/choose_repository.tcl | 29 +++++------------------------
 1 file changed, 5 insertions(+), 24 deletions(-)
lib/choose_repository.tcl +0 -17
index e54f3e66d8f..07f29b0b13b 100644
@@ -357,31 +357,12 @@ proc _is_git {path {outdir_var ""}} {
 	if {$outdir_var ne ""} {
 		upvar 1 $outdir_var outdir
 	}
-	if {[file isfile $path]} {
-		set fp [open $path r]
-		gets $fp line
-		close $fp
-		if {[regexp "^gitdir: (.+)$" $line line link_target]} {
-			set path [file join [file dirname $path] $link_target]
-			set path [file normalize $path]
-		}
-	}
-
-	if {[file exists [file join $path HEAD]]
-	 && [file exists [file join $path objects]]
-	 && [file exists [file join $path config]]} {
-		set outdir $path
-		return 1
-	}
-	if {[is_Cygwin]} {

lib/choose_repository.tcl proc _is_git {path {outdir_var ""}} { · line 363

Quoted 10 lines
lib/choose_repository.tcl +4 -6
-		if {[file exists [file join $path HEAD]]
-		 && [file exists [file join $path objects.lnk]]
-		 && [file exists [file join $path config.lnk]]} {
-			set outdir $path
-			return 1
-		}
+	if {[catch { 
+		set outdir [git rev-parse --resolve-git-dir $path] 
+		} ]} {
+			return 0

I fixed trailing whitespace in a couple lines and a small style nitpick. You can check them here [0]. Will merge. Thanks both.

[0] https://github.com/prati0100/git-gui/commit/79089e52643ce1ba7d8494f32b2fb1891c926079

Signature · 2 lines
Regards,
Pratyush Yadav
#5 lore raw
Quoted 53 lines · Pratyush Yadav

+Cc Eric.

Quoted 36 lines · Mikhail Terekhov via GitGitGadget

From: Mikhail Terekhov <[email protected]>

In proc _is_git check that supplied path is a valid work tree path. This allows the choose_repository::pick dialog to accept path to a work tree directory.

---
 lib/choose_repository.tcl | 29 +++++------------------------
 1 file changed, 5 insertions(+), 24 deletions(-)
lib/choose_repository.tcl +0 -17
index e54f3e66d8f..07f29b0b13b 100644
@@ -357,31 +357,12 @@ proc _is_git {path {outdir_var ""}} {
 	if {$outdir_var ne ""} {
 		upvar 1 $outdir_var outdir
 	}
-	if {[file isfile $path]} {
-		set fp [open $path r]
-		gets $fp line
-		close $fp
-		if {[regexp "^gitdir: (.+)$" $line line link_target]} {
-			set path [file join [file dirname $path] $link_target]
-			set path [file normalize $path]
-		}
-	}
-
-	if {[file exists [file join $path HEAD]]
-	 && [file exists [file join $path objects]]
-	 && [file exists [file join $path config]]} {
-		set outdir $path
-		return 1
-	}
-	if {[is_Cygwin]} {

lib/choose_repository.tcl proc _is_git {path {outdir_var ""}} { · line 363

Quoted 10 lines
lib/choose_repository.tcl +4 -6
-		if {[file exists [file join $path HEAD]]
-		 && [file exists [file join $path objects.lnk]]
-		 && [file exists [file join $path config.lnk]]} {
-			set outdir $path
-			return 1
-		}
+	if {[catch { 
+		set outdir [git rev-parse --resolve-git-dir $path] 
+		} ]} {
+			return 0

I fixed trailing whitespace in a couple lines and a small style nitpick. You can check them here [0]. Will merge. Thanks both.

Quoted 1 line

[0] https://github.com/prati0100/git-gui/commit/79089e52643ce1ba7d8494f32b2fb1891c926079

Merged to git-gui/master.

Signature · 2 lines
Regards,
Pratyush Yadav