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

6 messages from 2020-05-26 to 2020-06-22. Participants: Mikhail Terekhov via GitGitGadget, Pratyush Yadav, Eric Sunshine.
Thread: https://gitlist.dev/t/53550

## Mikhail Terekhov via GitGitGadget, 2020-05-26 15:33

Subject: [PATCH] git-gui: allow opening work trees from the startup dialog
Message-ID: <pull.644.git.1590507201474.gitgitgadget@gmail.com>
URL: https://gitlist.dev/e/pull.644.git.1590507201474.gitgitgadget%40gmail.com

```
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(+)

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, 2020-06-05 20:48

Subject: Re: [PATCH] git-gui: allow opening work trees from the startup dialog
Message-ID: <20200605204803.lbolbny5m5dczynn@yadavpratyush.com>
URL: https://gitlist.dev/e/20200605204803.lbolbny5m5dczynn%40yadavpratyush.com
In-Reply-To: <pull.644.git.1590507201474.gitgitgadget@gmail.com>

```
Hi Mikhail,

Thanks for the patch.

On 26/05/20 03:33PM, 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.

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.
 
> 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.

> +			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, 2020-06-05 21:13

Subject: Re: [PATCH] git-gui: allow opening work trees from the startup dialog
Message-ID: <CAPig+cQhm7XwKEJdjzmaG_gbOJZp1RbktJBeC0d8vTQfgLbydQ@mail.gmail.com>
URL: https://gitlist.dev/e/CAPig%2BcQhm7XwKEJdjzmaG_gbOJZp1RbktJBeC0d8vTQfgLbydQ%40mail.gmail.com
In-Reply-To: <20200605204803.lbolbny5m5dczynn@yadavpratyush.com>

```
On Fri, Jun 5, 2020 at 4:48 PM Pratyush Yadav <me@yadavpratyush.com> wrote:
> 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, 2020-06-06 04:33

Subject: [PATCH v2] git-gui: allow opening work trees from the startup dialog
Message-ID: <pull.644.v2.git.1591418019023.gitgitgadget@gmail.com>
URL: https://gitlist.dev/e/pull.644.v2.git.1591418019023.gitgitgadget%40gmail.com
In-Reply-To: <pull.644.git.1590507201474.gitgitgadget@gmail.com>

```
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(-)

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, 2020-06-08 09:31

Subject: Re: [PATCH v2] git-gui: allow opening work trees from the startup dialog
Message-ID: <20200608093118.ppwpydlkd5bmsjh5@yadavpratyush.com>
URL: https://gitlist.dev/e/20200608093118.ppwpydlkd5bmsjh5%40yadavpratyush.com
In-Reply-To: <pull.644.v2.git.1591418019023.gitgitgadget@gmail.com>

```
+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

-- 
Regards,
Pratyush Yadav

```

## Pratyush Yadav, 2020-06-22 14:58

Subject: Re: [PATCH v2] git-gui: allow opening work trees from the startup dialog
Message-ID: <20200622145835.refvvs36h4a6a275@yadavpratyush.com>
URL: https://gitlist.dev/e/20200622145835.refvvs36h4a6a275%40yadavpratyush.com
In-Reply-To: <20200608093118.ppwpydlkd5bmsjh5@yadavpratyush.com>

```
On 08/06/20 03:01PM, Pratyush Yadav wrote:
> +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

```
