threads / patch / 53550

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

Subject: [PATCH] git-gui: allow opening work trees from the startup dialog

## tl;dr

6 messages between May 26, 2020 and Jun 22, 2020. Diffs are folded; open one to read it.

replies: 5people: 3as markdown or json

Mikhail Terekhov via GitGitGadget· May 26, 2020, 15:33 UTC · lore
From: Mikhail Terekhov <termim@gmail.com>

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.

Signed-off-by: Mikhail Terekhov <termim@gmail.com>
---
    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(+)
Show changes to lib/choose_repository.tcl +11 −0
diff --git a/lib/choose_repository.tcl b/lib/choose_repository.tcl
index e54f3e66d8f..8f911e89fbb 100644
--- a/lib/choose_repository.tcl
+++ b/lib/choose_repository.tcl
@@ -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
-- 
gitgitgadget
Pratyush Yadav· Jun 5, 2020, 20:48 UTC · re: Mikhail Terekhov via GitGitGadget · lore

Re: [PATCH] git-gui: allow opening work trees from the startup dialog

Hi Mikhail,
Thanks for the patch.
On 26/05/20 03:33PM, Mikhail Terekhov via GitGitGadget wrote:
Show 5 quoted lines
> From: Mikhail Terekhov <termim@gmail.com>
> 
> 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.
 
Show 27 quoted lines
> Signed-off-by: Mikhail Terekhov <termim@gmail.com>
> ---
>     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(+)
> 
> diff --git a/lib/choose_repository.tcl b/lib/choose_repository.tcl
> index e54f3e66d8f..8f911e89fbb 100644
> --- a/lib/choose_repository.tcl
> +++ b/lib/choose_repository.tcl
> @@ -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.

Show 9 quoted lines
> +			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.

-- 
Regards,
Pratyush Yadav
Eric Sunshine· Jun 5, 2020, 21:13 UTC · re: Pratyush Yadav · lore

Re: [PATCH] git-gui: allow opening work trees from the startup dialog

On Fri, Jun 5, 2020 at 4:48 PM Pratyush Yadav <me@yadavpratyush.com> wrote:
Show 15 quoted lines
> On 26/05/20 03:33PM, Mikhail Terekhov via GitGitGadget wrote:
> > From: Mikhail Terekhov <termim@gmail.com>
> > +                     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.
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.

Mikhail Terekhov via GitGitGadget· Jun 6, 2020, 04:33 UTC · re: Mikhail Terekhov via GitGitGadget · lore

[PATCH v2] git-gui: allow opening work trees from the startup dialog

From: Mikhail Terekhov <termim@gmail.com>

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.

Signed-off-by: Mikhail Terekhov <termim@gmail.com>
---
    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 
       me@yadavpratyush.com [me@yadavpratyush.com] and Eric Sunshine 
       sunshine@sunshineco.com [sunshine@sunshineco.com].
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(-)
Show changes to lib/choose_repository.tcl +5 −24
diff --git a/lib/choose_repository.tcl b/lib/choose_repository.tcl
index e54f3e66d8f..07f29b0b13b 100644
--- a/lib/choose_repository.tcl
+++ b/lib/choose_repository.tcl
@@ -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
-- 
gitgitgadget
Pratyush Yadav· Jun 8, 2020, 09:31 UTC · re: Mikhail Terekhov via GitGitGadget · lore

Re: [PATCH v2] git-gui: allow opening work trees from the startup dialog

+Cc Eric.
On 06/06/20 04:33AM, Mikhail Terekhov via GitGitGadget wrote:
Show 46 quoted lines
> From: Mikhail Terekhov <termim@gmail.com>
> 
> 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.
> 
> Signed-off-by: Mikhail Terekhov <termim@gmail.com>
> ---
>  lib/choose_repository.tcl | 29 +++++------------------------
>  1 file changed, 5 insertions(+), 24 deletions(-)
> 
> diff --git a/lib/choose_repository.tcl b/lib/choose_repository.tcl
> index e54f3e66d8f..07f29b0b13b 100644
> --- a/lib/choose_repository.tcl
> +++ b/lib/choose_repository.tcl
> @@ -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

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
-- 
Regards,
Pratyush Yadav
Pratyush Yadav· Jun 22, 2020, 14:58 UTC · re: Pratyush Yadav · lore

Re: [PATCH v2] git-gui: allow opening work trees from the startup dialog

On 08/06/20 03:01PM, Pratyush Yadav wrote:
Show 54 quoted lines
> +Cc Eric.
> 
> On 06/06/20 04:33AM, Mikhail Terekhov via GitGitGadget wrote:
> > From: Mikhail Terekhov <termim@gmail.com>
> > 
> > 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.
> > 
> > Signed-off-by: Mikhail Terekhov <termim@gmail.com>
> > ---
> >  lib/choose_repository.tcl | 29 +++++------------------------
> >  1 file changed, 5 insertions(+), 24 deletions(-)
> > 
> > diff --git a/lib/choose_repository.tcl b/lib/choose_repository.tcl
> > index e54f3e66d8f..07f29b0b13b 100644
> > --- a/lib/choose_repository.tcl
> > +++ b/lib/choose_repository.tcl
> > @@ -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
> 
> 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
Merged to git-gui/master.
-- 
Regards,
Pratyush Yadav

← back to recent threads