threads / patch / 51931

patchgit-gui: respect core.hooksPath, falling back to .git/hooks

Subject: [PATCH 0/1] git-gui: respect core.hooksPath, falling back to .git/hooks

## tl;dr

24 messages between Sep 26, 2019 and Oct 17, 2019. Diffs are folded; open one to read it.

replies: 23people: 3as markdown or json

Johannes Schindelin via GitGitGadget· Sep 26, 2019, 21:17 UTC · lore
This is yet another patch from Git for Windows.
Johannes Schindelin (1):
  respect core.hooksPath, falling back to .git/hooks
 git-gui.sh | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
base-commit: 60c60b627e81bf84e1cb01729d2ae882178f079d
Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-361%2Fdscho%2Fgit-gui-hooks-path-v1
Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-361/dscho/git-gui-hooks-path-v1
Pull-Request: https://github.com/gitgitgadget/git/pull/361
-- 
gitgitgadget
Johannes Schindelin via GitGitGadget· Sep 26, 2019, 21:17 UTC · re: Johannes Schindelin via GitGitGadget · lore

[PATCH 1/1] respect core.hooksPath, falling back to .git/hooks

From: Johannes Schindelin <Johannes.Schindelin@gmx.de>

Since v2.9.0, Git knows about the config variable core.hookspath that allows overriding the path to the directory containing the Git hooks.

Since v2.10.0, the `--git-path` option respects that config variable, too, so we may just as well use that command.

For Git versions older than v2.5.0 (which was the first version to support the `--git-path` option for the `rev-parse` command), we simply fall back to the previous code.

This fixes https://github.com/git-for-windows/git/issues/1755
Initial-patch-by: Philipp Gortan <philipp@gortan.org>
Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
---
 git-gui.sh | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
Show changes to git-gui.sh +5 −1
diff --git a/git-gui.sh b/git-gui.sh
index fd476b6999..b2c6e7a1db 100755
--- a/git-gui.sh
+++ b/git-gui.sh
@@ -623,7 +623,11 @@ proc git_write {args} {
 }
 
 proc githook_read {hook_name args} {
-	set pchook [gitdir hooks $hook_name]
+	if {[package vcompare $::_git_version 2.5.0] >= 0} {
+		set pchook [git rev-parse --git-path "hooks/$hook_name"]
+	} else {
+		set pchook [gitdir hooks $hook_name]
+	}
 	lappend args 2>@1
 
 	# On Windows [file executable] might lie so we need to ask
-- 
gitgitgadget
Pratyush Yadav· Sep 26, 2019, 22:36 UTC · re: Johannes Schindelin via GitGitGadget · lore

Re: [PATCH 1/1] respect core.hooksPath, falling back to .git/hooks

Hi,
On 26/09/19 02:17PM, Johannes Schindelin via GitGitGadget wrote:
Show 35 quoted lines
> From: Johannes Schindelin <Johannes.Schindelin@gmx.de>
> 
> Since v2.9.0, Git knows about the config variable core.hookspath
> that allows overriding the path to the directory containing the
> Git hooks.
> 
> Since v2.10.0, the `--git-path` option respects that config
> variable, too, so we may just as well use that command.
> 
> For Git versions older than v2.5.0 (which was the first version to
> support the `--git-path` option for the `rev-parse` command), we
> simply fall back to the previous code.
> 
> This fixes https://github.com/git-for-windows/git/issues/1755
> 
> Initial-patch-by: Philipp Gortan <philipp@gortan.org>
> Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
> ---
>  git-gui.sh | 6 +++++-
>  1 file changed, 5 insertions(+), 1 deletion(-)
> 
> diff --git a/git-gui.sh b/git-gui.sh
> index fd476b6999..b2c6e7a1db 100755
> --- a/git-gui.sh
> +++ b/git-gui.sh
> @@ -623,7 +623,11 @@ proc git_write {args} {
>  }
>  
>  proc githook_read {hook_name args} {
> -	set pchook [gitdir hooks $hook_name]
> +	if {[package vcompare $::_git_version 2.5.0] >= 0} {
> +		set pchook [git rev-parse --git-path "hooks/$hook_name"]
> +	} else {
> +		set pchook [gitdir hooks $hook_name]
> +	}

gitdir is used in a lot of places, and I think all those would also benefit from using --git-path. So I think it is a better idea to move this to the procedure gitdir. It would have to be refactored to take any number of arguments, instead of the two it takes here.

Other than that, looks good. Thanks.
-- 
Regards,
Pratyush Yadav
Bert Wesarg· Sep 27, 2019, 06:10 UTC · re: Pratyush Yadav · lore

Re: [PATCH 1/1] respect core.hooksPath, falling back to .git/hooks

On Fri, Sep 27, 2019 at 12:40 AM Pratyush Yadav <me@yadavpratyush.com> wrote:
Show 44 quoted lines
>
> Hi,
>
> On 26/09/19 02:17PM, Johannes Schindelin via GitGitGadget wrote:
> > From: Johannes Schindelin <Johannes.Schindelin@gmx.de>
> >
> > Since v2.9.0, Git knows about the config variable core.hookspath
> > that allows overriding the path to the directory containing the
> > Git hooks.
> >
> > Since v2.10.0, the `--git-path` option respects that config
> > variable, too, so we may just as well use that command.
> >
> > For Git versions older than v2.5.0 (which was the first version to
> > support the `--git-path` option for the `rev-parse` command), we
> > simply fall back to the previous code.
> >
> > This fixes https://github.com/git-for-windows/git/issues/1755
> >
> > Initial-patch-by: Philipp Gortan <philipp@gortan.org>
> > Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
> > ---
> >  git-gui.sh | 6 +++++-
> >  1 file changed, 5 insertions(+), 1 deletion(-)
> >
> > diff --git a/git-gui.sh b/git-gui.sh
> > index fd476b6999..b2c6e7a1db 100755
> > --- a/git-gui.sh
> > +++ b/git-gui.sh
> > @@ -623,7 +623,11 @@ proc git_write {args} {
> >  }
> >
> >  proc githook_read {hook_name args} {
> > -     set pchook [gitdir hooks $hook_name]
> > +     if {[package vcompare $::_git_version 2.5.0] >= 0} {
> > +             set pchook [git rev-parse --git-path "hooks/$hook_name"]
> > +     } else {
> > +             set pchook [gitdir hooks $hook_name]
> > +     }
>
> gitdir is used in a lot of places, and I think all those would also
> benefit from using --git-path. So I think it is a better idea to move
> this to the procedure gitdir. It would have to be refactored to take any
> number of arguments, instead of the two it takes here.

gitdir already takes an arbitrary number of arguments and joins them to a path. The more imminent challenge is, that gitdir caches the GIT_DIR, thus it tries to avoid calling "git rev-parse". Which works for most, but not for hooks.

We could either maintain a blacklist, for what we cache the result too, or always call "git rev-parse --git-dir".

This blacklist would need to be in sync with the one in Git's path.c::adjust_git_path() than.

Bert
Show 6 quoted lines
>
> Other than that, looks good. Thanks.
>
> --
> Regards,
> Pratyush Yadav
Pratyush Yadav· Sep 27, 2019, 13:05 UTC · re: Bert Wesarg · lore

Re: [PATCH 1/1] respect core.hooksPath, falling back to .git/hooks

On 27/09/19 08:10AM, Bert Wesarg wrote:
Show 50 quoted lines
> On Fri, Sep 27, 2019 at 12:40 AM Pratyush Yadav <me@yadavpratyush.com> wrote:
> >
> > Hi,
> >
> > On 26/09/19 02:17PM, Johannes Schindelin via GitGitGadget wrote:
> > > From: Johannes Schindelin <Johannes.Schindelin@gmx.de>
> > >
> > > Since v2.9.0, Git knows about the config variable core.hookspath
> > > that allows overriding the path to the directory containing the
> > > Git hooks.
> > >
> > > Since v2.10.0, the `--git-path` option respects that config
> > > variable, too, so we may just as well use that command.
> > >
> > > For Git versions older than v2.5.0 (which was the first version to
> > > support the `--git-path` option for the `rev-parse` command), we
> > > simply fall back to the previous code.
> > >
> > > This fixes https://github.com/git-for-windows/git/issues/1755
> > >
> > > Initial-patch-by: Philipp Gortan <philipp@gortan.org>
> > > Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
> > > ---
> > >  git-gui.sh | 6 +++++-
> > >  1 file changed, 5 insertions(+), 1 deletion(-)
> > >
> > > diff --git a/git-gui.sh b/git-gui.sh
> > > index fd476b6999..b2c6e7a1db 100755
> > > --- a/git-gui.sh
> > > +++ b/git-gui.sh
> > > @@ -623,7 +623,11 @@ proc git_write {args} {
> > >  }
> > >
> > >  proc githook_read {hook_name args} {
> > > -     set pchook [gitdir hooks $hook_name]
> > > +     if {[package vcompare $::_git_version 2.5.0] >= 0} {
> > > +             set pchook [git rev-parse --git-path "hooks/$hook_name"]
> > > +     } else {
> > > +             set pchook [gitdir hooks $hook_name]
> > > +     }
> >
> > gitdir is used in a lot of places, and I think all those would also
> > benefit from using --git-path. So I think it is a better idea to move
> > this to the procedure gitdir. It would have to be refactored to take any
> > number of arguments, instead of the two it takes here.
> 
> gitdir already takes an arbitrary number of arguments and joins them
> to a path. The more imminent challenge is, that gitdir caches the
> GIT_DIR, thus it tries to avoid calling "git rev-parse". Which works
> for most, but not for hooks.
What I was thinking of was something like this:
  - If no args are passed, then just directly return $_gitdir. This is 
    already being done. I assume the GIT_DIR relocation is already 
    handled by `git rev-parse --git-dir`, so this would point to the 
    correct location.
  - If args are passed, then we want a subdirectory of GIT_DIR In this 
    case, it is possible that this subdirectory has also been relocated 
    (hooks/ being one of those subdirectories). So in this case, use 
    `git rev-parse --git-path` instead.
So the gitdir procedure would look something like:
  proc gitdir {args} {
  	global $_gitdir
  	if {$args eq {}} {
  		# Return the cached GIT_DIR
  		return $_gitdir
  	}
  
  	# Use `git rev-parse --git-path` to get the path instead of 
  	# using the cached value.
  }
Am I missing something? Or does this fix the issue you describe?
 
Show 5 quoted lines
> We could either maintain a blacklist, for what we cache the result
> too, or always call "git rev-parse --git-dir".
> 
> This blacklist would need to be in sync with the one in Git's
> path.c::adjust_git_path() than.

Is caching GIT_DIR that important in terms of performance? Otherwise, I'd say calling `git rev-parse --git-path` for _every_ subdirectory of GIT_DIR is a much simpler solution.

-- 
Regards,
Pratyush Yadav
Johannes Schindelin· Sep 30, 2019, 09:42 UTC · re: Pratyush Yadav · lore

Re: [PATCH 1/1] respect core.hooksPath, falling back to .git/hooks

Hi,
On Fri, 27 Sep 2019, Pratyush Yadav wrote:
Show 75 quoted lines
> On 27/09/19 08:10AM, Bert Wesarg wrote:
> > On Fri, Sep 27, 2019 at 12:40 AM Pratyush Yadav <me@yadavpratyush.com> wrote:
> > >
> > > On 26/09/19 02:17PM, Johannes Schindelin via GitGitGadget wrote:
> > > > From: Johannes Schindelin <Johannes.Schindelin@gmx.de>
> > > >
> > > > Since v2.9.0, Git knows about the config variable core.hookspath
> > > > that allows overriding the path to the directory containing the
> > > > Git hooks.
> > > >
> > > > Since v2.10.0, the `--git-path` option respects that config
> > > > variable, too, so we may just as well use that command.
> > > >
> > > > For Git versions older than v2.5.0 (which was the first version to
> > > > support the `--git-path` option for the `rev-parse` command), we
> > > > simply fall back to the previous code.
> > > >
> > > > This fixes https://github.com/git-for-windows/git/issues/1755
> > > >
> > > > Initial-patch-by: Philipp Gortan <philipp@gortan.org>
> > > > Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
> > > > ---
> > > >  git-gui.sh | 6 +++++-
> > > >  1 file changed, 5 insertions(+), 1 deletion(-)
> > > >
> > > > diff --git a/git-gui.sh b/git-gui.sh
> > > > index fd476b6999..b2c6e7a1db 100755
> > > > --- a/git-gui.sh
> > > > +++ b/git-gui.sh
> > > > @@ -623,7 +623,11 @@ proc git_write {args} {
> > > >  }
> > > >
> > > >  proc githook_read {hook_name args} {
> > > > -     set pchook [gitdir hooks $hook_name]
> > > > +     if {[package vcompare $::_git_version 2.5.0] >= 0} {
> > > > +             set pchook [git rev-parse --git-path "hooks/$hook_name"]
> > > > +     } else {
> > > > +             set pchook [gitdir hooks $hook_name]
> > > > +     }
> > >
> > > gitdir is used in a lot of places, and I think all those would also
> > > benefit from using --git-path. So I think it is a better idea to move
> > > this to the procedure gitdir. It would have to be refactored to take any
> > > number of arguments, instead of the two it takes here.
> >
> > gitdir already takes an arbitrary number of arguments and joins them
> > to a path. The more imminent challenge is, that gitdir caches the
> > GIT_DIR, thus it tries to avoid calling "git rev-parse". Which works
> > for most, but not for hooks.
>
> What I was thinking of was something like this:
>
>   - If no args are passed, then just directly return $_gitdir. This is
>     already being done. I assume the GIT_DIR relocation is already
>     handled by `git rev-parse --git-dir`, so this would point to the
>     correct location.
>   - If args are passed, then we want a subdirectory of GIT_DIR In this
>     case, it is possible that this subdirectory has also been relocated
>     (hooks/ being one of those subdirectories). So in this case, use
>     `git rev-parse --git-path` instead.
>
> So the gitdir procedure would look something like:
>
>   proc gitdir {args} {
>   	global $_gitdir
>   	if {$args eq {}} {
>   		# Return the cached GIT_DIR
>   		return $_gitdir
>   	}
>
>   	# Use `git rev-parse --git-path` to get the path instead of
>   	# using the cached value.
>   }
>
> Am I missing something? Or does this fix the issue you describe?

The `gitdir` function is called 13 times during startup alone, and who knows how many more times later.

So I am quite convinced that the original intention was to save on spawning processes left and right.

But since you are the Git GUI maintainer, and this was your suggestion, I made it so.

Ciao, Johannes

Show 14 quoted lines
> > We could either maintain a blacklist, for what we cache the result
> > too, or always call "git rev-parse --git-dir".
> >
> > This blacklist would need to be in sync with the one in Git's
> > path.c::adjust_git_path() than.
>
> Is caching GIT_DIR that important in terms of performance? Otherwise,
> I'd say calling `git rev-parse --git-path` for _every_ subdirectory of
> GIT_DIR is a much simpler solution.
>
> --
> Regards,
> Pratyush Yadav
>
Pratyush Yadav· Oct 1, 2019, 13:31 UTC · re: Johannes Schindelin · lore

Re: [PATCH 1/1] respect core.hooksPath, falling back to .git/hooks

On 30/09/19 11:42AM, Johannes Schindelin wrote:
Show 17 quoted lines
> On Fri, 27 Sep 2019, Pratyush Yadav wrote:
> > On 27/09/19 08:10AM, Bert Wesarg wrote:
> > > On Fri, Sep 27, 2019 at 12:40 AM Pratyush Yadav <me@yadavpratyush.com> wrote:
> > > > gitdir is used in a lot of places, and I think all those would 
> > > > also
> > > > benefit from using --git-path. So I think it is a better idea to move
> > > > this to the procedure gitdir. It would have to be refactored to take any
> > > > number of arguments, instead of the two it takes here.
> 
> The `gitdir` function is called 13 times during startup alone, and who
> knows how many more times later.
> 
> So I am quite convinced that the original intention was to save on
> spawning processes left and right.
> 
> But since you are the Git GUI maintainer, and this was your suggestion,
> I made it so.

Yes, I am the maintainer, but I am not an all-knowing, all-seeing entity. Your, and every other contributors, suggestions are very valuable. And my suggestions aren't gospel. I would hate to see someone send in a patch they weren't sure was the best thing to do just because I suggested it. Please feel free to object my suggestions.

In this case, I didn't expect gitdir to be called this many times.

While I don't notice much of a performance difference on my system (Linux), a quick measurement tells me that the time spent in gitdir is about 16 ms. In contrast, the same measurement without the v2 patch gives out 0 ms (IOW, very fast). 16 ms sounds a bit much for something so simple. It might not be the same for everyone else. AFAIK, spawning a process is much slower on Windows.

So now I'm not so sure my suggestion was a good one. My original aim was to be sure everything was correct, and no incorrect directories were being used. But the current solution comes at a performance hit.

Show 5 quoted lines
> > > We could either maintain a blacklist, for what we cache the result
> > > too, or always call "git rev-parse --git-dir".
> > >
> > > This blacklist would need to be in sync with the one in Git's
> > > path.c::adjust_git_path() than.

Bert's suggestion seems like a decent compromise. We run `git rev-parse --git-path` for the paths in the blacklist, and for the rest we use the cached value. This does run the risk of getting out of sync with git.git's list, but it might be better than spawing a process every time, and is very likely better than just doing it for hooks.

Thoughts?
-- 
Regards,
Pratyush Yadav
Johannes Schindelin· Oct 1, 2019, 17:38 UTC · re: Pratyush Yadav · lore

Re: [PATCH 1/1] respect core.hooksPath, falling back to .git/hooks

Hi,
On Tue, 1 Oct 2019, Pratyush Yadav wrote:
Show 49 quoted lines
> On 30/09/19 11:42AM, Johannes Schindelin wrote:
> > On Fri, 27 Sep 2019, Pratyush Yadav wrote:
> > > On 27/09/19 08:10AM, Bert Wesarg wrote:
> > > > On Fri, Sep 27, 2019 at 12:40 AM Pratyush Yadav <me@yadavpratyush.com> wrote:
> > > > > gitdir is used in a lot of places, and I think all those would
> > > > > also
> > > > > benefit from using --git-path. So I think it is a better idea to move
> > > > > this to the procedure gitdir. It would have to be refactored to take any
> > > > > number of arguments, instead of the two it takes here.
> >
> > The `gitdir` function is called 13 times during startup alone, and who
> > knows how many more times later.
> >
> > So I am quite convinced that the original intention was to save on
> > spawning processes left and right.
> >
> > But since you are the Git GUI maintainer, and this was your suggestion,
> > I made it so.
>
> Yes, I am the maintainer, but I am not an all-knowing, all-seeing
> entity. Your, and every other contributors, suggestions are very
> valuable. And my suggestions aren't gospel. I would hate to see someone
> send in a patch they weren't sure was the best thing to do just because
> I suggested it. Please feel free to object my suggestions.
>
> In this case, I didn't expect gitdir to be called this many times.
>
> While I don't notice much of a performance difference on my system
> (Linux), a quick measurement tells me that the time spent in gitdir is
> about 16 ms. In contrast, the same measurement without the v2 patch
> gives out 0 ms (IOW, very fast). 16 ms sounds a bit much for something
> so simple. It might not be the same for everyone else. AFAIK, spawning a
> process is much slower on Windows.
>
> So now I'm not so sure my suggestion was a good one. My original aim was
> to be sure everything was correct, and no incorrect directories were
> being used. But the current solution comes at a performance hit.
>
> > > > We could either maintain a blacklist, for what we cache the result
> > > > too, or always call "git rev-parse --git-dir".
> > > >
> > > > This blacklist would need to be in sync with the one in Git's
> > > > path.c::adjust_git_path() than.
>
> Bert's suggestion seems like a decent compromise. We run `git rev-parse
> --git-path` for the paths in the blacklist, and for the rest we use the
> cached value. This does run the risk of getting out of sync with
> git.git's list, but it might be better than spawing a process every
> time, and is very likely better than just doing it for hooks.
But what about this part of that function?
-- snip --
else if (repo->different_commondir)
	update_common_dir(buf, git_dir_len, repo->commondir);
-- snap --

It might well turn out that this blacklist is neither easy to implement nor will it help much.

So let's look at all the call sites:

-- snip -- $ git grep -w gitdir | sed -ne 's|\].*||' -e 's|.*\[gitdir ||p' | sort | uniq $file $name CHERRY_PICK_HEAD FETCH_HEAD GITGUI_BCK GITGUI_EDITMSG GITGUI_MSG HEAD hooks $hook_name index.lock info exclude logs $name MERGE_HEAD MERGE_MSG MERGE_RR objects 4\[0-[expr {$ndirs-1} objects info objects info alternates objects pack packed-refs PREPARE_COMMIT_MSG rebase-merge head-name remotes remotes $r rr-cache rr-cache MERGE_RR SQUASH_MSG -- snap --

The `$file` call looks for messages (probably commit, merge, tag messages and the likes), the `$name` one looks for refs.

Some of those arguments strike me as very good candidates to require the common directory while others require the real gitdir (remember, commondir != gitdir in worktrees other than the main worktree).

What _could_ be done (but we're certainly threatening to enter the realm of the ridiculous here) is to call `git rev-parse --git-dir --git-path CHERRY_PICK_HEAD --git-path FETCH_HEAD [...]`, which will output one path per line, and then store the result in an associative array (https://tcl.tk/man/tcl8.5/tutorial/Tcl22.html), and use that to look up paths based on their first component, caching as we go.

Something like this:
-- snipsnap --
Show changes to git-gui.sh +42 −3
diff --git a/git-gui.sh b/git-gui.sh
index fd476b6..9295c75 100755
--- a/git-gui.sh
+++ b/git-gui.sh
@@ -158,6 +158,7 @@ if {[tk windowingsystem] eq "aqua"} {

 set _appname {Git Gui}
 set _gitdir {}
+array set _gitdir_cached {}
 set _gitworktree {}
 set _isbare {}
 set _gitexec {}
@@ -197,12 +198,50 @@ proc appname {} {
 	return $_appname
 }

+proc init_gitdir_cached {} {
+	global _gitdir _gitdir_cached
+
+	set gitdir_keys [list \
+		CHERRY_PICK_HEAD FETCH_HEAD GITGUI_BCK GITGUI_EDITMSG \
+		GITGUI_MSG HEAD hooks index.lock info logs MERGE_HEAD \
+		MERGE_MSG MERGE_RR objects packed-refs PREPARE_COMMIT_MSG \
+		rebase-merge head-name remotes rr-cache SQUASH_MSG \
+		]
+
+	set gitdir_cmd [list git rev-parse --git-dir]
+	foreach key $gitdir_keys {
+		lappend gitdir_cmd --git-path $key
+	}
+
+	set i -1
+	foreach path [split [eval $gitdir_cmd] "\n"] {
+		if {$i eq -1} {
+			set _gitdir $path
+		} else {
+			set _gitdir_cached([lindex $gitdir_keys $i]) $path
+		}
+		incr i
+	}
+}
+
 proc gitdir {args} {
-	global _gitdir
+	global _gitdir _gitdir_cached
+
 	if {$args eq {}} {
 		return $_gitdir
 	}
-	return [eval [list file join $_gitdir] $args]
+
+	set arg0 [lindex $args 0]
+	set args [lrange $args 1 end]
+	if {![info exists _gitdir_cached($arg0)]} {
+		if {[package vcompare $::_git_version 2.5.0] >= 0} {
+			set _gitdir_cached($arg0) [git rev-parse --git-path $arg0]
+		} else {
+			set _gitdir_cached($arg0) [file join $_gitdir $arg0]
+		}
+	}
+
+	return [eval [concat [list file join $_gitdir_cached($arg0)] $args]]
 }

 proc gitexec {args} {
@@ -1242,7 +1281,7 @@ if {[catch {
 	&& [catch {
 		# beware that from the .git dir this sets _gitdir to .
 		# and _prefix to the empty string
-		set _gitdir [git rev-parse --git-dir]
+		init_gitdir_cached
 		set _prefix [git rev-parse --show-prefix]
 	} err]} {
 	load_config 1
Pratyush Yadav· Oct 4, 2019, 16:48 UTC · re: Johannes Schindelin · lore

Re: [PATCH 1/1] respect core.hooksPath, falling back to .git/hooks

On 01/10/19 07:38PM, Johannes Schindelin wrote:
Show 60 quoted lines
> Hi,
> 
> On Tue, 1 Oct 2019, Pratyush Yadav wrote:
> 
> > On 30/09/19 11:42AM, Johannes Schindelin wrote:
> > > On Fri, 27 Sep 2019, Pratyush Yadav wrote:
> > > > On 27/09/19 08:10AM, Bert Wesarg wrote:
> > > > > On Fri, Sep 27, 2019 at 12:40 AM Pratyush Yadav <me@yadavpratyush.com> wrote:
> > > > > > gitdir is used in a lot of places, and I think all those would
> > > > > > also
> > > > > > benefit from using --git-path. So I think it is a better idea to move
> > > > > > this to the procedure gitdir. It would have to be refactored to take any
> > > > > > number of arguments, instead of the two it takes here.
> > >
> > > The `gitdir` function is called 13 times during startup alone, and who
> > > knows how many more times later.
> > >
> > > So I am quite convinced that the original intention was to save on
> > > spawning processes left and right.
> > >
> > > But since you are the Git GUI maintainer, and this was your suggestion,
> > > I made it so.
> >
> > Yes, I am the maintainer, but I am not an all-knowing, all-seeing
> > entity. Your, and every other contributors, suggestions are very
> > valuable. And my suggestions aren't gospel. I would hate to see someone
> > send in a patch they weren't sure was the best thing to do just because
> > I suggested it. Please feel free to object my suggestions.
> >
> > In this case, I didn't expect gitdir to be called this many times.
> >
> > While I don't notice much of a performance difference on my system
> > (Linux), a quick measurement tells me that the time spent in gitdir is
> > about 16 ms. In contrast, the same measurement without the v2 patch
> > gives out 0 ms (IOW, very fast). 16 ms sounds a bit much for something
> > so simple. It might not be the same for everyone else. AFAIK, spawning a
> > process is much slower on Windows.
> >
> > So now I'm not so sure my suggestion was a good one. My original aim was
> > to be sure everything was correct, and no incorrect directories were
> > being used. But the current solution comes at a performance hit.
> >
> > > > > We could either maintain a blacklist, for what we cache the result
> > > > > too, or always call "git rev-parse --git-dir".
> > > > >
> > > > > This blacklist would need to be in sync with the one in Git's
> > > > > path.c::adjust_git_path() than.
> >
> > Bert's suggestion seems like a decent compromise. We run `git rev-parse
> > --git-path` for the paths in the blacklist, and for the rest we use the
> > cached value. This does run the risk of getting out of sync with
> > git.git's list, but it might be better than spawing a process every
> > time, and is very likely better than just doing it for hooks.
> 
> But what about this part of that function?
> 
> -- snip --
> else if (repo->different_commondir)
> 	update_common_dir(buf, git_dir_len, repo->commondir);
> -- snap --
I'm afraid I'm a bit out of my depth on this. I have no idea what a 
"common directory" is, and how is it different from the "git directory". 
I can't find anything useful on Google about it. My guess is that it is 
something related to separate worktrees.
 
> It might well turn out that this blacklist is neither easy to implement
> nor will it help much.
Am I correct in assuming that for other cases like "info", "grafts", 
"index", "objects", and "hooks" the blacklist would be simple to 
implement, and it is the "common directory" case that is problematic?
 
Show 35 quoted lines
> So let's look at all the call sites:
> 
> -- snip --
> $ git grep -w gitdir | sed -ne 's|\].*||' -e 's|.*\[gitdir ||p' | sort | uniq
> $file
> $name
> CHERRY_PICK_HEAD
> FETCH_HEAD
> GITGUI_BCK
> GITGUI_EDITMSG
> GITGUI_MSG
> HEAD
> hooks $hook_name
> index.lock
> info exclude
> logs $name
> MERGE_HEAD
> MERGE_MSG
> MERGE_RR
> objects 4\[0-[expr {$ndirs-1}
> objects info
> objects info alternates
> objects pack
> packed-refs
> PREPARE_COMMIT_MSG
> rebase-merge head-name
> remotes
> remotes $r
> rr-cache
> rr-cache MERGE_RR
> SQUASH_MSG
> -- snap --
> 
> The `$file` call looks for messages (probably commit, merge, tag
> messages and the likes), the `$name` one looks for refs.
So they should always be inside the '.git' or GIT_DIR, correct?
 
Show 10 quoted lines
> Some of those arguments strike me as very good candidates to require the
> common directory while others require the real gitdir (remember,
> commondir != gitdir in worktrees other than the main worktree).
> 
> What _could_ be done (but we're certainly threatening to enter the realm
> of the ridiculous here) is to call `git rev-parse --git-dir --git-path
> CHERRY_PICK_HEAD --git-path FETCH_HEAD [...]`, which will output one
> path per line, and then store the result in an associative array
> (https://tcl.tk/man/tcl8.5/tutorial/Tcl22.html), and use that to look up
> paths based on their first component, caching as we go.

Ah yes! That is certainly threatening to enter the realm of ridiculous. I'm not sure what benefit this will have. Right now, I don't think git-gui handles these cases. Have people complained? Is this a common problem?

I want to evaluate how much benefit we get doing something like this has 
over just using your original patch that works with hooks only.
 
Show 77 quoted lines
> Something like this:
> 
> -- snipsnap --
> diff --git a/git-gui.sh b/git-gui.sh
> index fd476b6..9295c75 100755
> --- a/git-gui.sh
> +++ b/git-gui.sh
> @@ -158,6 +158,7 @@ if {[tk windowingsystem] eq "aqua"} {
> 
>  set _appname {Git Gui}
>  set _gitdir {}
> +array set _gitdir_cached {}
>  set _gitworktree {}
>  set _isbare {}
>  set _gitexec {}
> @@ -197,12 +198,50 @@ proc appname {} {
>  	return $_appname
>  }
> 
> +proc init_gitdir_cached {} {
> +	global _gitdir _gitdir_cached
> +
> +	set gitdir_keys [list \
> +		CHERRY_PICK_HEAD FETCH_HEAD GITGUI_BCK GITGUI_EDITMSG \
> +		GITGUI_MSG HEAD hooks index.lock info logs MERGE_HEAD \
> +		MERGE_MSG MERGE_RR objects packed-refs PREPARE_COMMIT_MSG \
> +		rebase-merge head-name remotes rr-cache SQUASH_MSG \
> +		]
> +
> +	set gitdir_cmd [list git rev-parse --git-dir]
> +	foreach key $gitdir_keys {
> +		lappend gitdir_cmd --git-path $key
> +	}
> +
> +	set i -1
> +	foreach path [split [eval $gitdir_cmd] "\n"] {
> +		if {$i eq -1} {
> +			set _gitdir $path
> +		} else {
> +			set _gitdir_cached([lindex $gitdir_keys $i]) $path
> +		}
> +		incr i
> +	}
> +}
> +
>  proc gitdir {args} {
> -	global _gitdir
> +	global _gitdir _gitdir_cached
> +
>  	if {$args eq {}} {
>  		return $_gitdir
>  	}
> -	return [eval [list file join $_gitdir] $args]
> +
> +	set arg0 [lindex $args 0]
> +	set args [lrange $args 1 end]
> +	if {![info exists _gitdir_cached($arg0)]} {
> +		if {[package vcompare $::_git_version 2.5.0] >= 0} {
> +			set _gitdir_cached($arg0) [git rev-parse --git-path $arg0]
> +		} else {
> +			set _gitdir_cached($arg0) [file join $_gitdir $arg0]
> +		}
> +	}
> +
> +	return [eval [concat [list file join $_gitdir_cached($arg0)] $args]]
>  }
> 
>  proc gitexec {args} {
> @@ -1242,7 +1281,7 @@ if {[catch {
>  	&& [catch {
>  		# beware that from the .git dir this sets _gitdir to .
>  		# and _prefix to the empty string
> -		set _gitdir [git rev-parse --git-dir]
> +		init_gitdir_cached
>  		set _prefix [git rev-parse --show-prefix]
>  	} err]} {
>  	load_config 1

A nice way of tackling this problem overall considering the challenges, but I'm worried about whether all this is _actually_ needed for real use cases, and what breaks if we don't.

Honestly, I'm not too sure how to tackle this problem. That is also the reason I took so long in writing this response. What would your suggestion be?

Also, if some other people interested in git-gui could chime in, it would be great.

-- 
Regards,
Pratyush Yadav
Johannes Schindelin· Oct 4, 2019, 19:56 UTC · re: Pratyush Yadav · lore

Re: [PATCH 1/1] respect core.hooksPath, falling back to .git/hooks

Hi Pratyush,
On Fri, 4 Oct 2019, Pratyush Yadav wrote:
Show 65 quoted lines
> On 01/10/19 07:38PM, Johannes Schindelin wrote:
> >
> > On Tue, 1 Oct 2019, Pratyush Yadav wrote:
> >
> > > On 30/09/19 11:42AM, Johannes Schindelin wrote:
> > > > On Fri, 27 Sep 2019, Pratyush Yadav wrote:
> > > > > On 27/09/19 08:10AM, Bert Wesarg wrote:
> > > > > > On Fri, Sep 27, 2019 at 12:40 AM Pratyush Yadav <me@yadavpratyush.com> wrote:
> > > > > > > gitdir is used in a lot of places, and I think all those would
> > > > > > > also
> > > > > > > benefit from using --git-path. So I think it is a better idea to move
> > > > > > > this to the procedure gitdir. It would have to be refactored to take any
> > > > > > > number of arguments, instead of the two it takes here.
> > > >
> > > > The `gitdir` function is called 13 times during startup alone, and who
> > > > knows how many more times later.
> > > >
> > > > So I am quite convinced that the original intention was to save on
> > > > spawning processes left and right.
> > > >
> > > > But since you are the Git GUI maintainer, and this was your suggestion,
> > > > I made it so.
> > >
> > > Yes, I am the maintainer, but I am not an all-knowing, all-seeing
> > > entity. Your, and every other contributors, suggestions are very
> > > valuable. And my suggestions aren't gospel. I would hate to see someone
> > > send in a patch they weren't sure was the best thing to do just because
> > > I suggested it. Please feel free to object my suggestions.
> > >
> > > In this case, I didn't expect gitdir to be called this many times.
> > >
> > > While I don't notice much of a performance difference on my system
> > > (Linux), a quick measurement tells me that the time spent in gitdir is
> > > about 16 ms. In contrast, the same measurement without the v2 patch
> > > gives out 0 ms (IOW, very fast). 16 ms sounds a bit much for something
> > > so simple. It might not be the same for everyone else. AFAIK, spawning a
> > > process is much slower on Windows.
> > >
> > > So now I'm not so sure my suggestion was a good one. My original aim was
> > > to be sure everything was correct, and no incorrect directories were
> > > being used. But the current solution comes at a performance hit.
> > >
> > > > > > We could either maintain a blacklist, for what we cache the result
> > > > > > too, or always call "git rev-parse --git-dir".
> > > > > >
> > > > > > This blacklist would need to be in sync with the one in Git's
> > > > > > path.c::adjust_git_path() than.
> > >
> > > Bert's suggestion seems like a decent compromise. We run `git rev-parse
> > > --git-path` for the paths in the blacklist, and for the rest we use the
> > > cached value. This does run the risk of getting out of sync with
> > > git.git's list, but it might be better than spawing a process every
> > > time, and is very likely better than just doing it for hooks.
> >
> > But what about this part of that function?
> >
> > -- snip --
> > else if (repo->different_commondir)
> > 	update_common_dir(buf, git_dir_len, repo->commondir);
> > -- snap --
>
> I'm afraid I'm a bit out of my depth on this. I have no idea what a
> "common directory" is, and how is it different from the "git directory".
> I can't find anything useful on Google about it. My guess is that it is
> something related to separate worktrees.

It is indeed related to worktrees. If you create a secondary worktree via `git worktree add [...]`, that work tree will get its own git directory under `.git/worktrees/<name>` in the main worktree. That git directory will not, however, contain all contents of a regular git directory. Most refs, for example, are stored in the main worktree's git directory. That is what the "common dir" is.

Show 6 quoted lines
> > It might well turn out that this blacklist is neither easy to implement
> > nor will it help much.
>
> Am I correct in assuming that for other cases like "info", "grafts",
> "index", "objects", and "hooks" the blacklist would be simple to
> implement, and it is the "common directory" case that is problematic?

Indeed, for the other, simple cases, the list would be unproblematic to implement. Problematic to maintain, though, especially given that Git GUI is _supposed_ to support even very old Git versions.

And those simple cases don't include _all_ interesting cases. Take `logs/` for example. The git directory will contain the reflogs for `HEAD`, but unless you're on an unnamed branch (AKA "detached HEAD"), the reflogs for the current branch are _in the commondir_.

Show 37 quoted lines
> > So let's look at all the call sites:
> >
> > -- snip --
> > $ git grep -w gitdir | sed -ne 's|\].*||' -e 's|.*\[gitdir ||p' | sort | uniq
> > $file
> > $name
> > CHERRY_PICK_HEAD
> > FETCH_HEAD
> > GITGUI_BCK
> > GITGUI_EDITMSG
> > GITGUI_MSG
> > HEAD
> > hooks $hook_name
> > index.lock
> > info exclude
> > logs $name
> > MERGE_HEAD
> > MERGE_MSG
> > MERGE_RR
> > objects 4\[0-[expr {$ndirs-1}
> > objects info
> > objects info alternates
> > objects pack
> > packed-refs
> > PREPARE_COMMIT_MSG
> > rebase-merge head-name
> > remotes
> > remotes $r
> > rr-cache
> > rr-cache MERGE_RR
> > SQUASH_MSG
> > -- snap --
> >
> > The `$file` call looks for messages (probably commit, merge, tag
> > messages and the likes), the `$name` one looks for refs.
>
> So they should always be inside the '.git' or GIT_DIR, correct?

They should be inside the git directory. Note that `.git` in worktrees is just a file that contains `gitdir: <path>`. The indicated path is the actual git directory. Inside that git directory, the file `commondir` contains the path to the main worktree's git directory.

Show 15 quoted lines
> > Some of those arguments strike me as very good candidates to require the
> > common directory while others require the real gitdir (remember,
> > commondir != gitdir in worktrees other than the main worktree).
> >
> > What _could_ be done (but we're certainly threatening to enter the realm
> > of the ridiculous here) is to call `git rev-parse --git-dir --git-path
> > CHERRY_PICK_HEAD --git-path FETCH_HEAD [...]`, which will output one
> > path per line, and then store the result in an associative array
> > (https://tcl.tk/man/tcl8.5/tutorial/Tcl22.html), and use that to look up
> > paths based on their first component, caching as we go.
>
> Ah yes! That is certainly threatening to enter the realm of ridiculous.
> I'm not sure what benefit this will have. Right now, I don't think
> git-gui handles these cases. Have people complained? Is this a common
> problem?

Well, we know that people complained about the hooks directory. And that did not even involve worktrees.

I see that e.g. `packed-refs` is queried by Git GUI. And that file lives in the main worktree's git directory, i.e. in the commondir.

So either users don't use Git GUI in secondary worktrees, or they did not even notice the bug.

> I want to evaluate how much benefit we get doing something like this
> has over just using your original patch that works with hooks only.

Since I already have that ridiculous approach essentially implemented, and since it fixes very real bugs in Git GUI ever since `git worktree` was introduced, I'd say that you'd be better off taking the ridiculous patch than not.

Show 81 quoted lines
> > Something like this:
> >
> > -- snipsnap --
> > diff --git a/git-gui.sh b/git-gui.sh
> > index fd476b6..9295c75 100755
> > --- a/git-gui.sh
> > +++ b/git-gui.sh
> > @@ -158,6 +158,7 @@ if {[tk windowingsystem] eq "aqua"} {
> >
> >  set _appname {Git Gui}
> >  set _gitdir {}
> > +array set _gitdir_cached {}
> >  set _gitworktree {}
> >  set _isbare {}
> >  set _gitexec {}
> > @@ -197,12 +198,50 @@ proc appname {} {
> >  	return $_appname
> >  }
> >
> > +proc init_gitdir_cached {} {
> > +	global _gitdir _gitdir_cached
> > +
> > +	set gitdir_keys [list \
> > +		CHERRY_PICK_HEAD FETCH_HEAD GITGUI_BCK GITGUI_EDITMSG \
> > +		GITGUI_MSG HEAD hooks index.lock info logs MERGE_HEAD \
> > +		MERGE_MSG MERGE_RR objects packed-refs PREPARE_COMMIT_MSG \
> > +		rebase-merge head-name remotes rr-cache SQUASH_MSG \
> > +		]
> > +
> > +	set gitdir_cmd [list git rev-parse --git-dir]
> > +	foreach key $gitdir_keys {
> > +		lappend gitdir_cmd --git-path $key
> > +	}
> > +
> > +	set i -1
> > +	foreach path [split [eval $gitdir_cmd] "\n"] {
> > +		if {$i eq -1} {
> > +			set _gitdir $path
> > +		} else {
> > +			set _gitdir_cached([lindex $gitdir_keys $i]) $path
> > +		}
> > +		incr i
> > +	}
> > +}
> > +
> >  proc gitdir {args} {
> > -	global _gitdir
> > +	global _gitdir _gitdir_cached
> > +
> >  	if {$args eq {}} {
> >  		return $_gitdir
> >  	}
> > -	return [eval [list file join $_gitdir] $args]
> > +
> > +	set arg0 [lindex $args 0]
> > +	set args [lrange $args 1 end]
> > +	if {![info exists _gitdir_cached($arg0)]} {
> > +		if {[package vcompare $::_git_version 2.5.0] >= 0} {
> > +			set _gitdir_cached($arg0) [git rev-parse --git-path $arg0]
> > +		} else {
> > +			set _gitdir_cached($arg0) [file join $_gitdir $arg0]
> > +		}
> > +	}
> > +
> > +	return [eval [concat [list file join $_gitdir_cached($arg0)] $args]]
> >  }
> >
> >  proc gitexec {args} {
> > @@ -1242,7 +1281,7 @@ if {[catch {
> >  	&& [catch {
> >  		# beware that from the .git dir this sets _gitdir to .
> >  		# and _prefix to the empty string
> > -		set _gitdir [git rev-parse --git-dir]
> > +		init_gitdir_cached
> >  		set _prefix [git rev-parse --show-prefix]
> >  	} err]} {
> >  	load_config 1
>
> A nice way of tackling this problem overall considering the challenges,
> but I'm worried about whether all this is _actually_ needed for real use
> cases, and what breaks if we don't.

Why don't you try using Git GUI in a worktree for a while? I am sure you will encounter the issues sooner or later.

> Honestly, I'm not too sure how to tackle this problem. That is also the
> reason I took so long in writing this response. What would your
> suggestion be?

I would actually go for the ridiculous patch, as it provides the safest bet we have on fixing the `gitdir`-related bugs.

> Also, if some other people interested in git-gui could chime in, it
> would be great.
Sure.

Ciao, Johannes

Johannes Schindelin via GitGitGadget· Sep 30, 2019, 09:45 UTC · re: Johannes Schindelin via GitGitGadget · lore

[PATCH v2 0/1] git-gui: respect core.hooksPath, falling back to .git/hooks

This is yet another patch from Git for Windows.
Changes since v1:
 * Rather than a fine-grained override of gitdir just for the hooks path, we
   now spawn git rev-parse --git-path [...] every time gitdir is called with
   arguments. This makes the code safer, although at the cost of potentially
   many spawned processes.
Johannes Schindelin (1):
  respect core.hooksPath, falling back to .git/hooks
 git-gui.sh | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
base-commit: 60c60b627e81bf84e1cb01729d2ae882178f079d
Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-361%2Fdscho%2Fgit-gui-hooks-path-v2
Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-361/dscho/git-gui-hooks-path-v2
Pull-Request: https://github.com/gitgitgadget/git/pull/361
Range-diff vs v1:
 1:  eca193f91b < -:  ---------- respect core.hooksPath, falling back to .git/hooks
 -:  ---------- > 1:  c101422936 respect core.hooksPath, falling back to .git/hooks
-- 
gitgitgadget
Johannes Schindelin via GitGitGadget· Sep 30, 2019, 09:45 UTC · re: Johannes Schindelin via GitGitGadget · lore

[PATCH v2 1/1] respect core.hooksPath, falling back to .git/hooks

From: Johannes Schindelin <Johannes.Schindelin@gmx.de>

Since v2.9.0, Git knows about the config variable core.hookspath that allows overriding the path to the directory containing the Git hooks.

Since v2.10.0, the `--git-path` option respects that config variable, too, so we may just as well use that command.

For Git versions older than v2.5.0 (which was the first version to support the `--git-path` option for the `rev-parse` command), we simply fall back to the previous code.

An original patch handled only the hooksPath setting (as the title of this commit message suggests), however, during the code submission it was deemed better to fix all call to the `gitdir` function.

With this change, we spawn `git rev-parse --git-path [...]` 13 times during Git GUI's startup.

This fixes https://github.com/git-for-windows/git/issues/1755
Initial-patch-by: Philipp Gortan <philipp@gortan.org>
Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
---
 git-gui.sh | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
Show changes to git-gui.sh +5 −1
diff --git a/git-gui.sh b/git-gui.sh
index fd476b6999..b2f0e78077 100755
--- a/git-gui.sh
+++ b/git-gui.sh
@@ -202,7 +202,11 @@ proc gitdir {args} {
 	if {$args eq {}} {
 		return $_gitdir
 	}
-	return [eval [list file join $_gitdir] $args]
+	if {[package vcompare $::_git_version 2.5.0] >= 0} {
+		return [git rev-parse --git-path [eval [list file join] $args]]
+	} else {
+		return [eval [list file join $_gitdir] $args]
+	}
 }
 
 proc gitexec {args} {
-- 
gitgitgadget
Johannes Schindelin via GitGitGadget· Oct 4, 2019, 21:41 UTC · re: Johannes Schindelin via GitGitGadget · lore

[PATCH v3 0/1] git-gui: respect core.hooksPath, falling back to .git/hooks

This is yet another patch from Git for Windows.
Changes since v2:
 * The paths returned by git rev-parse --git-path are now cached, and the
   cache is primed with the most common paths.
Changes since v1:
 * Rather than a fine-grained override of gitdir just for the hooks path, we
   now spawn git rev-parse --git-path [...] every time gitdir is called with
   arguments. This makes the code safer, although at the cost of potentially
   many spawned processes.
Johannes Schindelin (1):
  Fix gitdir e.g. to respect core.hooksPath
 git-gui.sh | 52 +++++++++++++++++++++++++++++++++++++++++++++++++---
 1 file changed, 49 insertions(+), 3 deletions(-)
base-commit: 60c60b627e81bf84e1cb01729d2ae882178f079d
Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-361%2Fdscho%2Fgit-gui-hooks-path-v3
Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-361/dscho/git-gui-hooks-path-v3
Pull-Request: https://github.com/gitgitgadget/git/pull/361
Range-diff vs v2:
 1:  c101422936 < -:  ---------- respect core.hooksPath, falling back to .git/hooks
 -:  ---------- > 1:  65c2fa33e1 Fix gitdir e.g. to respect core.hooksPath
-- 
gitgitgadget
Johannes Schindelin via GitGitGadget· Oct 4, 2019, 21:41 UTC · re: Johannes Schindelin via GitGitGadget · lore

[PATCH v3 1/1] Fix gitdir e.g. to respect core.hooksPath

From: Johannes Schindelin <Johannes.Schindelin@gmx.de>

Since v2.9.0, Git knows about the config variable core.hookspath that allows overriding the path to the directory containing the Git hooks.

Since v2.10.0, the `--git-path` option respects that config variable, too, so we may just as well use that command.

For Git versions older than v2.5.0 (which was the first version to support the `--git-path` option for the `rev-parse` command), we simply fall back to the previous code.

An original patch handled only the hooksPath setting, however, during the code submission it was deemed better to fix all call to the `gitdir` function.

To avoid spawning a gazillion `git rev-parse --git-path` instances, we cache the returned paths, priming the cache upon startup in a single `git rev-parse invocation` with the known entries.

This fixes https://github.com/git-for-windows/git/issues/1755
Initial-patch-by: Philipp Gortan <philipp@gortan.org>
Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
---
 git-gui.sh | 52 +++++++++++++++++++++++++++++++++++++++++++++++++---
 1 file changed, 49 insertions(+), 3 deletions(-)
Show changes to git-gui.sh +49 −3
diff --git a/git-gui.sh b/git-gui.sh
index fd476b6999..8b72e59cd0 100755
--- a/git-gui.sh
+++ b/git-gui.sh
@@ -158,6 +158,7 @@ if {[tk windowingsystem] eq "aqua"} {
 
 set _appname {Git Gui}
 set _gitdir {}
+array set _gitdir_cache {}
 set _gitworktree {}
 set _isbare {}
 set _gitexec {}
@@ -197,12 +198,57 @@ proc appname {} {
 	return $_appname
 }
 
+proc prime_gitdir_cache {} {
+	global _gitdir _gitdir_cache
+
+	set gitdir_cmd [list git rev-parse --git-dir]
+
+	# `--git-path` is only supported since Git v2.5.0
+	if {[package vcompare $::_git_version 2.5.0] >= 0} {
+		set gitdir_keys [list \
+			CHERRY_PICK_HEAD FETCH_HEAD GITGUI_BCK GITGUI_EDITMSG \
+			GITGUI_MSG HEAD hooks hooks/prepare-commit-msg \
+			index.lock info info/exclude logs MERGE_HEAD MERGE_MSG \
+			MERGE_RR objects "objects/4\[0-1\]/*" \
+			"objects/4\[0-3\]/*" objects/info \
+			objects/info/alternates objects/pack packed-refs \
+			PREPARE_COMMIT_MSG rebase-merge/head-name remotes \
+			rr-cache rr-cache/MERGE_RR SQUASH_MSG \
+		]
+
+		foreach key $gitdir_keys {
+			lappend gitdir_cmd --git-path $key
+		}
+	}
+
+	set i -1
+	foreach path [split [eval $gitdir_cmd] "\n"] {
+		if {$i eq -1} {
+			set _gitdir $path
+		} else {
+			set _gitdir_cache([lindex $gitdir_keys $i]) $path
+		}
+		incr i
+	}
+}
+
 proc gitdir {args} {
-	global _gitdir
+	global _gitdir _gitdir_cache
+
 	if {$args eq {}} {
 		return $_gitdir
 	}
-	return [eval [list file join $_gitdir] $args]
+
+	set args [eval [list file join] $args]
+	if {![info exists _gitdir_cache($args)]} {
+		if {[package vcompare $::_git_version 2.5.0] >= 0} {
+			set _gitdir_cache($args) [git rev-parse --git-path $args]
+		} else {
+			set _gitdir_cache($args) [file join $_gitdir $args]
+		}
+	}
+
+	return $_gitdir_cache($args)
 }
 
 proc gitexec {args} {
@@ -1242,7 +1288,7 @@ if {[catch {
 	&& [catch {
 		# beware that from the .git dir this sets _gitdir to .
 		# and _prefix to the empty string
-		set _gitdir [git rev-parse --git-dir]
+		prime_gitdir_cache
 		set _prefix [git rev-parse --show-prefix]
 	} err]} {
 	load_config 1
-- 
gitgitgadget
Pratyush Yadav· Oct 8, 2019, 00:29 UTC · re: Johannes Schindelin via GitGitGadget · lore

Re: [PATCH v3 1/1] Fix gitdir e.g. to respect core.hooksPath

Hi Johannes,

Could you please change the commit subject to more clearly state that we are caching all paths. This is not something just related to hooks any more.

On 04/10/19 02:41PM, Johannes Schindelin via GitGitGadget wrote:
Show 20 quoted lines
> From: Johannes Schindelin <Johannes.Schindelin@gmx.de>
> 
> Since v2.9.0, Git knows about the config variable core.hookspath
> that allows overriding the path to the directory containing the
> Git hooks.
> 
> Since v2.10.0, the `--git-path` option respects that config
> variable, too, so we may just as well use that command.
> 
> For Git versions older than v2.5.0 (which was the first version to
> support the `--git-path` option for the `rev-parse` command), we
> simply fall back to the previous code.
> 
> An original patch handled only the hooksPath setting, however, during
> the code submission it was deemed better to fix all call to the `gitdir`
> function.
> 
> To avoid spawning a gazillion `git rev-parse --git-path` instances, we
> cache the returned paths, priming the cache upon startup in a single
> `git rev-parse invocation` with the known entries.
I think it would also be a good idea to mention that we are fixing 
worktree paths not being correct.
 
Show 31 quoted lines
> This fixes https://github.com/git-for-windows/git/issues/1755
> 
> Initial-patch-by: Philipp Gortan <philipp@gortan.org>
> Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
> ---
>  git-gui.sh | 52 +++++++++++++++++++++++++++++++++++++++++++++++++---
>  1 file changed, 49 insertions(+), 3 deletions(-)
> 
> diff --git a/git-gui.sh b/git-gui.sh
> index fd476b6999..8b72e59cd0 100755
> --- a/git-gui.sh
> +++ b/git-gui.sh
> @@ -158,6 +158,7 @@ if {[tk windowingsystem] eq "aqua"} {
>  
>  set _appname {Git Gui}
>  set _gitdir {}
> +array set _gitdir_cache {}
>  set _gitworktree {}
>  set _isbare {}
>  set _gitexec {}
> @@ -197,12 +198,57 @@ proc appname {} {
>  	return $_appname
>  }
>  
> +proc prime_gitdir_cache {} {
> +	global _gitdir _gitdir_cache
> +
> +	set gitdir_cmd [list git rev-parse --git-dir]
> +
> +	# `--git-path` is only supported since Git v2.5.0
> +	if {[package vcompare $::_git_version 2.5.0] >= 0} {

I think we should mention the source of the list of "magic" keys. A comment mentioning this list came from looking at the common calls to `gitdir` in the rest of the git-gui code would explain this function to a future reader better.

Show 18 quoted lines
> +		set gitdir_keys [list \
> +			CHERRY_PICK_HEAD FETCH_HEAD GITGUI_BCK GITGUI_EDITMSG \
> +			GITGUI_MSG HEAD hooks hooks/prepare-commit-msg \
> +			index.lock info info/exclude logs MERGE_HEAD MERGE_MSG \
> +			MERGE_RR objects "objects/4\[0-1\]/*" \
> +			"objects/4\[0-3\]/*" objects/info \
> +			objects/info/alternates objects/pack packed-refs \
> +			PREPARE_COMMIT_MSG rebase-merge/head-name remotes \
> +			rr-cache rr-cache/MERGE_RR SQUASH_MSG \
> +		]
> +
> +		foreach key $gitdir_keys {
> +			lappend gitdir_cmd --git-path $key
> +		}
> +	}
> +
> +	set i -1
> +	foreach path [split [eval $gitdir_cmd] "\n"] {

A call to the procedure 'git' is wrapped in a 'catch' in a lot of places. But it is also not wrapped in 'catch' in a lot of other places.

I'm not sure how something like this would fail, so I'm not sure if wrapping this call in a catch is a good idea. But it is something to consider.

Show 40 quoted lines
> +		if {$i eq -1} {
> +			set _gitdir $path
> +		} else {
> +			set _gitdir_cache([lindex $gitdir_keys $i]) $path
> +		}
> +		incr i
> +	}
> +}
> +
>  proc gitdir {args} {
> -	global _gitdir
> +	global _gitdir _gitdir_cache
> +
>  	if {$args eq {}} {
>  		return $_gitdir
>  	}
> -	return [eval [list file join $_gitdir] $args]
> +
> +	set args [eval [list file join] $args]
> +	if {![info exists _gitdir_cache($args)]} {
> +		if {[package vcompare $::_git_version 2.5.0] >= 0} {
> +			set _gitdir_cache($args) [git rev-parse --git-path $args]
> +		} else {
> +			set _gitdir_cache($args) [file join $_gitdir $args]
> +		}
> +	}
> +
> +	return $_gitdir_cache($args)
>  }
>  
>  proc gitexec {args} {
> @@ -1242,7 +1288,7 @@ if {[catch {
>  	&& [catch {
>  		# beware that from the .git dir this sets _gitdir to .
>  		# and _prefix to the empty string
> -		set _gitdir [git rev-parse --git-dir]
> +		prime_gitdir_cache
>  		set _prefix [git rev-parse --show-prefix]
>  	} err]} {
>  	load_config 1

Can these paths we cache change while git-gui is running, say by a command run by the user in the terminal? In that case, we should refresh the list when the user rescans.

Other than some minor comments, looks good. Thanks.
-- 
Regards,
Pratyush Yadav
Johannes Schindelin· Oct 8, 2019, 11:30 UTC · re: Pratyush Yadav · lore

Re: [PATCH v3 1/1] Fix gitdir e.g. to respect core.hooksPath

Hi Pratyush,
On Tue, 8 Oct 2019, Pratyush Yadav wrote:
> Could you please change the commit subject to more clearly state that we
> are caching all paths. This is not something just related to hooks any
> more.
It is related, but not exclusively so. Changed accordingly.
Show 24 quoted lines
> On 04/10/19 02:41PM, Johannes Schindelin via GitGitGadget wrote:
> > From: Johannes Schindelin <Johannes.Schindelin@gmx.de>
> >
> > Since v2.9.0, Git knows about the config variable core.hookspath
> > that allows overriding the path to the directory containing the
> > Git hooks.
> >
> > Since v2.10.0, the `--git-path` option respects that config
> > variable, too, so we may just as well use that command.
> >
> > For Git versions older than v2.5.0 (which was the first version to
> > support the `--git-path` option for the `rev-parse` command), we
> > simply fall back to the previous code.
> >
> > An original patch handled only the hooksPath setting, however, during
> > the code submission it was deemed better to fix all call to the `gitdir`
> > function.
> >
> > To avoid spawning a gazillion `git rev-parse --git-path` instances, we
> > cache the returned paths, priming the cache upon startup in a single
> > `git rev-parse invocation` with the known entries.
>
> I think it would also be a good idea to mention that we are fixing
> worktree paths not being correct.
Done.
Show 36 quoted lines
> > This fixes https://github.com/git-for-windows/git/issues/1755
> >
> > Initial-patch-by: Philipp Gortan <philipp@gortan.org>
> > Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
> > ---
> >  git-gui.sh | 52 +++++++++++++++++++++++++++++++++++++++++++++++++---
> >  1 file changed, 49 insertions(+), 3 deletions(-)
> >
> > diff --git a/git-gui.sh b/git-gui.sh
> > index fd476b6999..8b72e59cd0 100755
> > --- a/git-gui.sh
> > +++ b/git-gui.sh
> > @@ -158,6 +158,7 @@ if {[tk windowingsystem] eq "aqua"} {
> >
> >  set _appname {Git Gui}
> >  set _gitdir {}
> > +array set _gitdir_cache {}
> >  set _gitworktree {}
> >  set _isbare {}
> >  set _gitexec {}
> > @@ -197,12 +198,57 @@ proc appname {} {
> >  	return $_appname
> >  }
> >
> > +proc prime_gitdir_cache {} {
> > +	global _gitdir _gitdir_cache
> > +
> > +	set gitdir_cmd [list git rev-parse --git-dir]
> > +
> > +	# `--git-path` is only supported since Git v2.5.0
> > +	if {[package vcompare $::_git_version 2.5.0] >= 0} {
>
> I think we should mention the source of the list of "magic" keys. A
> comment mentioning this list came from looking at the common calls to
> `gitdir` in the rest of the git-gui code would explain this function to
> a future reader better.
Makes sense.
Show 25 quoted lines
> > +		set gitdir_keys [list \
> > +			CHERRY_PICK_HEAD FETCH_HEAD GITGUI_BCK GITGUI_EDITMSG \
> > +			GITGUI_MSG HEAD hooks hooks/prepare-commit-msg \
> > +			index.lock info info/exclude logs MERGE_HEAD MERGE_MSG \
> > +			MERGE_RR objects "objects/4\[0-1\]/*" \
> > +			"objects/4\[0-3\]/*" objects/info \
> > +			objects/info/alternates objects/pack packed-refs \
> > +			PREPARE_COMMIT_MSG rebase-merge/head-name remotes \
> > +			rr-cache rr-cache/MERGE_RR SQUASH_MSG \
> > +		]
> > +
> > +		foreach key $gitdir_keys {
> > +			lappend gitdir_cmd --git-path $key
> > +		}
> > +	}
> > +
> > +	set i -1
> > +	foreach path [split [eval $gitdir_cmd] "\n"] {
>
> A call to the procedure 'git' is wrapped in a 'catch' in a lot of
> places. But it is also not wrapped in 'catch' in a lot of other places.
>
> I'm not sure how something like this would fail, so I'm not sure if
> wrapping this call in a catch is a good idea. But it is something to
> consider.

If that call fails, we lack a `gitdir`, which is a rather fundamental prerequisite to running Git GUI. I'd rather show an (ugly, but also highly unexpected) exception.

In other words, I think the patch should stay "catch-less".
Show 44 quoted lines
> > +		if {$i eq -1} {
> > +			set _gitdir $path
> > +		} else {
> > +			set _gitdir_cache([lindex $gitdir_keys $i]) $path
> > +		}
> > +		incr i
> > +	}
> > +}
> > +
> >  proc gitdir {args} {
> > -	global _gitdir
> > +	global _gitdir _gitdir_cache
> > +
> >  	if {$args eq {}} {
> >  		return $_gitdir
> >  	}
> > -	return [eval [list file join $_gitdir] $args]
> > +
> > +	set args [eval [list file join] $args]
> > +	if {![info exists _gitdir_cache($args)]} {
> > +		if {[package vcompare $::_git_version 2.5.0] >= 0} {
> > +			set _gitdir_cache($args) [git rev-parse --git-path $args]
> > +		} else {
> > +			set _gitdir_cache($args) [file join $_gitdir $args]
> > +		}
> > +	}
> > +
> > +	return $_gitdir_cache($args)
> >  }
> >
> >  proc gitexec {args} {
> > @@ -1242,7 +1288,7 @@ if {[catch {
> >  	&& [catch {
> >  		# beware that from the .git dir this sets _gitdir to .
> >  		# and _prefix to the empty string
> > -		set _gitdir [git rev-parse --git-dir]
> > +		prime_gitdir_cache
> >  		set _prefix [git rev-parse --show-prefix]
> >  	} err]} {
> >  	load_config 1
>
> Can these paths we cache change while git-gui is running, say by a
> command run by the user in the terminal? In that case, we should refresh
> the list when the user rescans.

I guess it can. A user could configure `core.hooksPath` while Git GUI is open, after all. Or rewrite the `.git` file of a worktree.

I won't touch the `.git` file part, but I changed the `rescan` code to re-prime the gitdir cache.

Thanks for the review, Johannes

Show 7 quoted lines
>
> Other than some minor comments, looks good. Thanks.
>
> --
> Regards,
> Pratyush Yadav
>
Johannes Schindelin via GitGitGadget· Oct 8, 2019, 11:33 UTC · re: Johannes Schindelin via GitGitGadget · lore

[PATCH v4 0/1] git-gui: respect core.hooksPath, falling back to .git/hooks

This is yet another patch from Git for Windows.
Changes since v3:
 * Adjusted the commit message to reflect that this is no longer only about
   the hooks directory.
 * Added a code comment to indicate how the list of keys was determined that
   are used for the gitdir priming.
 * The gitdir cache is now re-primed upon F5.
Changes since v2:
 * The paths returned by git rev-parse --git-path are now cached, and the
   cache is primed with the most common paths.
Changes since v1:
 * Rather than a fine-grained override of gitdir just for the hooks path, we
   now spawn git rev-parse --git-path [...] every time gitdir is called with
   arguments. This makes the code safer, although at the cost of potentially
   many spawned processes.
Johannes Schindelin (1):
  Make gitdir work with worktrees, respect core.hooksPath, etc
 git-gui.sh | 62 ++++++++++++++++++++++++++++++++++++++++++++++++++----
 1 file changed, 58 insertions(+), 4 deletions(-)
base-commit: 60c60b627e81bf84e1cb01729d2ae882178f079d
Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-361%2Fdscho%2Fgit-gui-hooks-path-v4
Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-361/dscho/git-gui-hooks-path-v4
Pull-Request: https://github.com/gitgitgadget/git/pull/361
Range-diff vs v3:
 1:  65c2fa33e1 ! 1:  2f55d6fb2a Fix gitdir e.g. to respect core.hooksPath
     @@ -1,17 +1,21 @@
      Author: Johannes Schindelin <Johannes.Schindelin@gmx.de>
      
     -    Fix gitdir e.g. to respect core.hooksPath
     +    Make gitdir work with worktrees, respect core.hooksPath, etc
      
     -    Since v2.9.0, Git knows about the config variable core.hookspath
     -    that allows overriding the path to the directory containing the
     -    Git hooks.
     +    Since v2.9.0, Git knows about the config variable core.hookspath that
     +    allows overriding the path to the directory containing the Git hooks.
      
     -    Since v2.10.0, the `--git-path` option respects that config
     -    variable, too, so we may just as well use that command.
     +    Since v2.10.0, the `--git-path` option respects that config variable,
     +    too, so we may just as well use that command.
     +
     +    Other paths inside `.git` are equally subject to differ from
     +    `<gitdir>/<path>`, i.e. inside worktrees, where _some_ paths live in the
     +    "gitdir" and some live in the "commondir" (i.e. the "gitdir" of the main
     +    worktree).
      
          For Git versions older than v2.5.0 (which was the first version to
     -    support the `--git-path` option for the `rev-parse` command), we
     -    simply fall back to the previous code.
     +    support the `--git-path` option for the `rev-parse` command), we simply
     +    fall back to the previous code.
      
          An original patch handled only the hooksPath setting, however, during
          the code submission it was deemed better to fix all call to the `gitdir`
     @@ -19,7 +23,9 @@
      
          To avoid spawning a gazillion `git rev-parse --git-path` instances, we
          cache the returned paths, priming the cache upon startup in a single
     -    `git rev-parse invocation` with the known entries.
     +    `git rev-parse invocation` with some paths (that have been
     +    determined via a typical startup and via grepping the source code for
     +    calls to the `gitdir` function).
      
          This fixes https://github.com/git-for-windows/git/issues/1755
      
     @@ -48,6 +54,8 @@
      +
      +	# `--git-path` is only supported since Git v2.5.0
      +	if {[package vcompare $::_git_version 2.5.0] >= 0} {
     ++		# This list was generated from a typical startup as well as from
     ++		# grepping through Git GUI's source code.
      +		set gitdir_keys [list \
      +			CHERRY_PICK_HEAD FETCH_HEAD GITGUI_BCK GITGUI_EDITMSG \
      +			GITGUI_MSG HEAD hooks hooks/prepare-commit-msg \
     @@ -106,3 +114,21 @@
       		set _prefix [git rev-parse --show-prefix]
       	} err]} {
       	load_config 1
     +@@
     + 	global HEAD PARENT MERGE_HEAD commit_type
     + 	global ui_index ui_workdir ui_comm
     + 	global rescan_active file_states
     +-	global repo_config
     ++	global repo_config _gitdir_cache
     + 
     + 	if {$rescan_active > 0 || ![lock_index read]} return
     + 
     ++	# Only re-prime gitdir cache on a full rescan
     ++	if {$after ne "ui_ready"} {
     ++		array unset _gitdir_cache
     ++		prime_gitdir_cache
     ++	}
     ++
     + 	repository_state newType newHEAD newMERGE_HEAD
     + 	if {[string match amend* $commit_type]
     + 		&& $newType eq {normal}
-- 
gitgitgadget
Johannes Schindelin via GitGitGadget· Oct 8, 2019, 11:33 UTC · re: Johannes Schindelin via GitGitGadget · lore

[PATCH v4 1/1] Make gitdir work with worktrees, respect core.hooksPath, etc

From: Johannes Schindelin <Johannes.Schindelin@gmx.de>

Since v2.9.0, Git knows about the config variable core.hookspath that allows overriding the path to the directory containing the Git hooks.

Since v2.10.0, the `--git-path` option respects that config variable, too, so we may just as well use that command.

Other paths inside `.git` are equally subject to differ from `<gitdir>/<path>`, i.e. inside worktrees, where _some_ paths live in the "gitdir" and some live in the "commondir" (i.e. the "gitdir" of the main worktree).

For Git versions older than v2.5.0 (which was the first version to support the `--git-path` option for the `rev-parse` command), we simply fall back to the previous code.

An original patch handled only the hooksPath setting, however, during the code submission it was deemed better to fix all call to the `gitdir` function.

To avoid spawning a gazillion `git rev-parse --git-path` instances, we cache the returned paths, priming the cache upon startup in a single `git rev-parse invocation` with some paths (that have been determined via a typical startup and via grepping the source code for calls to the `gitdir` function).

This fixes https://github.com/git-for-windows/git/issues/1755
Initial-patch-by: Philipp Gortan <philipp@gortan.org>
Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
---
 git-gui.sh | 62 ++++++++++++++++++++++++++++++++++++++++++++++++++----
 1 file changed, 58 insertions(+), 4 deletions(-)
Show changes to git-gui.sh +58 −4
diff --git a/git-gui.sh b/git-gui.sh
index fd476b6999..c684dc7ae1 100755
--- a/git-gui.sh
+++ b/git-gui.sh
@@ -158,6 +158,7 @@ if {[tk windowingsystem] eq "aqua"} {
 
 set _appname {Git Gui}
 set _gitdir {}
+array set _gitdir_cache {}
 set _gitworktree {}
 set _isbare {}
 set _gitexec {}
@@ -197,12 +198,59 @@ proc appname {} {
 	return $_appname
 }
 
+proc prime_gitdir_cache {} {
+	global _gitdir _gitdir_cache
+
+	set gitdir_cmd [list git rev-parse --git-dir]
+
+	# `--git-path` is only supported since Git v2.5.0
+	if {[package vcompare $::_git_version 2.5.0] >= 0} {
+		# This list was generated from a typical startup as well as from
+		# grepping through Git GUI's source code.
+		set gitdir_keys [list \
+			CHERRY_PICK_HEAD FETCH_HEAD GITGUI_BCK GITGUI_EDITMSG \
+			GITGUI_MSG HEAD hooks hooks/prepare-commit-msg \
+			index.lock info info/exclude logs MERGE_HEAD MERGE_MSG \
+			MERGE_RR objects "objects/4\[0-1\]/*" \
+			"objects/4\[0-3\]/*" objects/info \
+			objects/info/alternates objects/pack packed-refs \
+			PREPARE_COMMIT_MSG rebase-merge/head-name remotes \
+			rr-cache rr-cache/MERGE_RR SQUASH_MSG \
+		]
+
+		foreach key $gitdir_keys {
+			lappend gitdir_cmd --git-path $key
+		}
+	}
+
+	set i -1
+	foreach path [split [eval $gitdir_cmd] "\n"] {
+		if {$i eq -1} {
+			set _gitdir $path
+		} else {
+			set _gitdir_cache([lindex $gitdir_keys $i]) $path
+		}
+		incr i
+	}
+}
+
 proc gitdir {args} {
-	global _gitdir
+	global _gitdir _gitdir_cache
+
 	if {$args eq {}} {
 		return $_gitdir
 	}
-	return [eval [list file join $_gitdir] $args]
+
+	set args [eval [list file join] $args]
+	if {![info exists _gitdir_cache($args)]} {
+		if {[package vcompare $::_git_version 2.5.0] >= 0} {
+			set _gitdir_cache($args) [git rev-parse --git-path $args]
+		} else {
+			set _gitdir_cache($args) [file join $_gitdir $args]
+		}
+	}
+
+	return $_gitdir_cache($args)
 }
 
 proc gitexec {args} {
@@ -1242,7 +1290,7 @@ if {[catch {
 	&& [catch {
 		# beware that from the .git dir this sets _gitdir to .
 		# and _prefix to the empty string
-		set _gitdir [git rev-parse --git-dir]
+		prime_gitdir_cache
 		set _prefix [git rev-parse --show-prefix]
 	} err]} {
 	load_config 1
@@ -1453,10 +1501,16 @@ proc rescan {after {honor_trustmtime 1}} {
 	global HEAD PARENT MERGE_HEAD commit_type
 	global ui_index ui_workdir ui_comm
 	global rescan_active file_states
-	global repo_config
+	global repo_config _gitdir_cache
 
 	if {$rescan_active > 0 || ![lock_index read]} return
 
+	# Only re-prime gitdir cache on a full rescan
+	if {$after ne "ui_ready"} {
+		array unset _gitdir_cache
+		prime_gitdir_cache
+	}
+
 	repository_state newType newHEAD newMERGE_HEAD
 	if {[string match amend* $commit_type]
 		&& $newType eq {normal}
-- 
gitgitgadget
Pratyush Yadav· Oct 11, 2019, 22:26 UTC · re: Johannes Schindelin via GitGitGadget · lore

Re: [PATCH v4 1/1] Make gitdir work with worktrees, respect core.hooksPath, etc

Hi Johannes,
Thanks for the re-roll. Some comments below...
On 08/10/19 04:33AM, Johannes Schindelin via GitGitGadget wrote:
Show 118 quoted lines
> From: Johannes Schindelin <Johannes.Schindelin@gmx.de>
> 
> Since v2.9.0, Git knows about the config variable core.hookspath that
> allows overriding the path to the directory containing the Git hooks.
> 
> Since v2.10.0, the `--git-path` option respects that config variable,
> too, so we may just as well use that command.
> 
> Other paths inside `.git` are equally subject to differ from
> `<gitdir>/<path>`, i.e. inside worktrees, where _some_ paths live in the
> "gitdir" and some live in the "commondir" (i.e. the "gitdir" of the main
> worktree).
> 
> For Git versions older than v2.5.0 (which was the first version to
> support the `--git-path` option for the `rev-parse` command), we simply
> fall back to the previous code.
> 
> An original patch handled only the hooksPath setting, however, during
> the code submission it was deemed better to fix all call to the `gitdir`
> function.
> 
> To avoid spawning a gazillion `git rev-parse --git-path` instances, we
> cache the returned paths, priming the cache upon startup in a single
> `git rev-parse invocation` with some paths (that have been
> determined via a typical startup and via grepping the source code for
> calls to the `gitdir` function).
> 
> This fixes https://github.com/git-for-windows/git/issues/1755
> 
> Initial-patch-by: Philipp Gortan <philipp@gortan.org>
> Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
> ---
>  git-gui.sh | 62 ++++++++++++++++++++++++++++++++++++++++++++++++++----
>  1 file changed, 58 insertions(+), 4 deletions(-)
> 
> diff --git a/git-gui.sh b/git-gui.sh
> index fd476b6999..c684dc7ae1 100755
> --- a/git-gui.sh
> +++ b/git-gui.sh
> @@ -158,6 +158,7 @@ if {[tk windowingsystem] eq "aqua"} {
>  
>  set _appname {Git Gui}
>  set _gitdir {}
> +array set _gitdir_cache {}
>  set _gitworktree {}
>  set _isbare {}
>  set _gitexec {}
> @@ -197,12 +198,59 @@ proc appname {} {
>  	return $_appname
>  }
>  
> +proc prime_gitdir_cache {} {
> +	global _gitdir _gitdir_cache
> +
> +	set gitdir_cmd [list git rev-parse --git-dir]
> +
> +	# `--git-path` is only supported since Git v2.5.0
> +	if {[package vcompare $::_git_version 2.5.0] >= 0} {
> +		# This list was generated from a typical startup as well as from
> +		# grepping through Git GUI's source code.
> +		set gitdir_keys [list \
> +			CHERRY_PICK_HEAD FETCH_HEAD GITGUI_BCK GITGUI_EDITMSG \
> +			GITGUI_MSG HEAD hooks hooks/prepare-commit-msg \
> +			index.lock info info/exclude logs MERGE_HEAD MERGE_MSG \
> +			MERGE_RR objects "objects/4\[0-1\]/*" \
> +			"objects/4\[0-3\]/*" objects/info \
> +			objects/info/alternates objects/pack packed-refs \
> +			PREPARE_COMMIT_MSG rebase-merge/head-name remotes \
> +			rr-cache rr-cache/MERGE_RR SQUASH_MSG \
> +		]
> +
> +		foreach key $gitdir_keys {
> +			lappend gitdir_cmd --git-path $key
> +		}
> +	}
> +
> +	set i -1
> +	foreach path [split [eval $gitdir_cmd] "\n"] {
> +		if {$i eq -1} {
> +			set _gitdir $path
> +		} else {
> +			set _gitdir_cache([lindex $gitdir_keys $i]) $path
> +		}
> +		incr i
> +	}
> +}
> +
>  proc gitdir {args} {
> -	global _gitdir
> +	global _gitdir _gitdir_cache
> +
>  	if {$args eq {}} {
>  		return $_gitdir
>  	}
> -	return [eval [list file join $_gitdir] $args]
> +
> +	set args [eval [list file join] $args]
> +	if {![info exists _gitdir_cache($args)]} {
> +		if {[package vcompare $::_git_version 2.5.0] >= 0} {
> +			set _gitdir_cache($args) [git rev-parse --git-path $args]
> +		} else {
> +			set _gitdir_cache($args) [file join $_gitdir $args]
> +		}
> +	}
> +
> +	return $_gitdir_cache($args)
>  }
>  
>  proc gitexec {args} {
> @@ -1242,7 +1290,7 @@ if {[catch {
>  	&& [catch {
>  		# beware that from the .git dir this sets _gitdir to .
>  		# and _prefix to the empty string
> -		set _gitdir [git rev-parse --git-dir]
> +		prime_gitdir_cache
>  		set _prefix [git rev-parse --show-prefix]
>  	} err]} {
>  	load_config 1
Looks good till here.
Show 11 quoted lines
> @@ -1453,10 +1501,16 @@ proc rescan {after {honor_trustmtime 1}} {
>  	global HEAD PARENT MERGE_HEAD commit_type
>  	global ui_index ui_workdir ui_comm
>  	global rescan_active file_states
> -	global repo_config
> +	global repo_config _gitdir_cache
>  
>  	if {$rescan_active > 0 || ![lock_index read]} return
>  
> +	# Only re-prime gitdir cache on a full rescan
> +	if {$after ne "ui_ready"} {

What do you mean by a "full rescan"? I assume you use it as the differentiator between `ui_do_rescan` (called when you hit F5 or choose rescan from the menu) and `do_rescan` (called when you revert a line or hunk), and a "full rescan" refers to `ui_do_rescan`.

Well in that case, this check is incorrect. `do_rescan` passes only "ui_ready" and `ui_do_rescan` passes "force_first_diff ui_ready".

But either way, I'm not a big fan of this. This check makes assumptions about the behaviour of its callers based on what they pass to $after. The way I see it, $after should be a black box to `rescan`, and it should make absolutely no assumptions about it.

Doing it this way is really brittle, and would break as soon as someone changes the behaviour of `ui_do_rescan`. If someone in the future passes a different value in $after, this would stop working as intended and would not refresh the cached list on a rescan.

So, I think a better place for this if statement would be in `ui_do_rescan`. This would mean adding a new function that does this. But if we unset _gitdir_cache in prime_gitdir_cache (I see no reason not to), we can get away with just something like:

  proc ui_do_rescan {} {
  	rescan {prime_gitdir_cache; ui_ready}
  }

Though since `prime_gitdir_cache` does not really depend on the rescan being finished, something like this would also work fine:

  proc ui_do_rescan {} {
  	rescan ui_ready
  	prime_gitdir_cache
  }

This would allow us to do these two things in parallel since `rescan` is asynchronous. But that would also mean it is possible that the status bar would show "Ready" while `prime_gitdir_cache` is still executing.

I can't really make up my mind on what is better. I'm inclining on using the latter way, effectively trading a bit of UI inconsistency for performance (at least in theory).

Thoughts?
Show 7 quoted lines
> +		array unset _gitdir_cache
> +		prime_gitdir_cache
> +	}
> +
>  	repository_state newType newHEAD newMERGE_HEAD
>  	if {[string match amend* $commit_type]
>  		&& $newType eq {normal}
-- 
Regards,
Pratyush Yadav
Johannes Schindelin· Oct 12, 2019, 21:24 UTC · re: Pratyush Yadav · lore

Re: [PATCH v4 1/1] Make gitdir work with worktrees, respect core.hooksPath, etc

Hi Pratyush,
On Sat, 12 Oct 2019, Pratyush Yadav wrote:
Show 48 quoted lines
> On 08/10/19 04:33AM, Johannes Schindelin via GitGitGadget wrote:
>
> > @@ -1453,10 +1501,16 @@ proc rescan {after {honor_trustmtime 1}} {
> >  	global HEAD PARENT MERGE_HEAD commit_type
> >  	global ui_index ui_workdir ui_comm
> >  	global rescan_active file_states
> > -	global repo_config
> > +	global repo_config _gitdir_cache
> >
> >  	if {$rescan_active > 0 || ![lock_index read]} return
> >
> > +	# Only re-prime gitdir cache on a full rescan
> > +	if {$after ne "ui_ready"} {
>
> What do you mean by a "full rescan"? I assume you use it as the
> differentiator between `ui_do_rescan` (called when you hit F5 or choose
> rescan from the menu) and `do_rescan` (called when you revert a line or
> hunk), and a "full rescan" refers to `ui_do_rescan`.
>
> Well in that case, this check is incorrect. `do_rescan` passes only
> "ui_ready" and `ui_do_rescan` passes "force_first_diff ui_ready".
>
> But either way, I'm not a big fan of this. This check makes assumptions
> about the behaviour of its callers based on what they pass to $after.
> The way I see it, $after should be a black box to `rescan`, and it
> should make absolutely no assumptions about it.
>
> Doing it this way is really brittle, and would break as soon as someone
> changes the behaviour of `ui_do_rescan`. If someone in the future passes
> a different value in $after, this would stop working as intended and
> would not refresh the cached list on a rescan.
>
> So, I think a better place for this if statement would be in
> `ui_do_rescan`. This would mean adding a new function that does this.
> But if we unset _gitdir_cache in prime_gitdir_cache (I see no reason not
> to), we can get away with just something like:
>
>   proc ui_do_rescan {} {
>   	rescan {prime_gitdir_cache; ui_ready}
>   }
>
> Though since `prime_gitdir_cache` does not really depend on the rescan
> being finished, something like this would also work fine:
>
>   proc ui_do_rescan {} {
>   	rescan ui_ready
>   	prime_gitdir_cache
>   }

That was my first attempt. However, there is a very important piece of code that is even still quoted above: that `if {$rescan_active > 0 || ![lock_index read]} return` part.

I do _not_ want to interfere with an actively-going-on rescan. If there is an active one, I don't want to re-prime the `_gitdir` cache.

That was the reason why I put the additional code into `rescan` rather than into `ui_do_rescan()`.

Ciao, Johannes

Show 23 quoted lines
>
> This would allow us to do these two things in parallel since `rescan` is
> asynchronous. But that would also mean it is possible that the status
> bar would show "Ready" while `prime_gitdir_cache` is still executing.
>
> I can't really make up my mind on what is better. I'm inclining on using
> the latter way, effectively trading a bit of UI inconsistency for
> performance (at least in theory).
>
> Thoughts?
>
> > +		array unset _gitdir_cache
> > +		prime_gitdir_cache
> > +	}
> > +
> >  	repository_state newType newHEAD newMERGE_HEAD
> >  	if {[string match amend* $commit_type]
> >  		&& $newType eq {normal}
>
> --
> Regards,
> Pratyush Yadav
>
Pratyush Yadav· Oct 13, 2019, 18:55 UTC · re: Johannes Schindelin · lore

Re: [PATCH v4 1/1] Make gitdir work with worktrees, respect core.hooksPath, etc

On 12/10/19 11:24PM, Johannes Schindelin wrote:
Show 59 quoted lines
> Hi Pratyush,
> 
> On Sat, 12 Oct 2019, Pratyush Yadav wrote:
> 
> > On 08/10/19 04:33AM, Johannes Schindelin via GitGitGadget wrote:
> >
> > > @@ -1453,10 +1501,16 @@ proc rescan {after {honor_trustmtime 1}} {
> > >  	global HEAD PARENT MERGE_HEAD commit_type
> > >  	global ui_index ui_workdir ui_comm
> > >  	global rescan_active file_states
> > > -	global repo_config
> > > +	global repo_config _gitdir_cache
> > >
> > >  	if {$rescan_active > 0 || ![lock_index read]} return
> > >
> > > +	# Only re-prime gitdir cache on a full rescan
> > > +	if {$after ne "ui_ready"} {
> >
> > What do you mean by a "full rescan"? I assume you use it as the
> > differentiator between `ui_do_rescan` (called when you hit F5 or choose
> > rescan from the menu) and `do_rescan` (called when you revert a line or
> > hunk), and a "full rescan" refers to `ui_do_rescan`.
> >
> > Well in that case, this check is incorrect. `do_rescan` passes only
> > "ui_ready" and `ui_do_rescan` passes "force_first_diff ui_ready".
> >
> > But either way, I'm not a big fan of this. This check makes assumptions
> > about the behaviour of its callers based on what they pass to $after.
> > The way I see it, $after should be a black box to `rescan`, and it
> > should make absolutely no assumptions about it.
> >
> > Doing it this way is really brittle, and would break as soon as someone
> > changes the behaviour of `ui_do_rescan`. If someone in the future passes
> > a different value in $after, this would stop working as intended and
> > would not refresh the cached list on a rescan.
> >
> > So, I think a better place for this if statement would be in
> > `ui_do_rescan`. This would mean adding a new function that does this.
> > But if we unset _gitdir_cache in prime_gitdir_cache (I see no reason not
> > to), we can get away with just something like:
> >
> >   proc ui_do_rescan {} {
> >   	rescan {prime_gitdir_cache; ui_ready}
> >   }
> >
> > Though since `prime_gitdir_cache` does not really depend on the rescan
> > being finished, something like this would also work fine:
> >
> >   proc ui_do_rescan {} {
> >   	rescan ui_ready
> >   	prime_gitdir_cache
> >   }
> 
> That was my first attempt. However, there is a very important piece of
> code that is even still quoted above: that `if {$rescan_active > 0 ||
> ![lock_index read]} return` part.
> 
> I do _not_ want to interfere with an actively-going-on rescan. If there
> is an active one, I don't want to re-prime the `_gitdir` cache.
Good catch! In that case I suppose refreshing the cache in $after would 
be the way to go (IOW, the former of my two suggestions). Anything 
$after won't get executed if we return early from that check.
 
Show 24 quoted lines
> That was the reason why I put the additional code into `rescan` rather
> than into `ui_do_rescan()`.
> 
> Ciao,
> Johannes
> 
> >
> > This would allow us to do these two things in parallel since `rescan` is
> > asynchronous. But that would also mean it is possible that the status
> > bar would show "Ready" while `prime_gitdir_cache` is still executing.
> >
> > I can't really make up my mind on what is better. I'm inclining on using
> > the latter way, effectively trading a bit of UI inconsistency for
> > performance (at least in theory).
> >
> > Thoughts?
> >
> > > +		array unset _gitdir_cache
> > > +		prime_gitdir_cache
> > > +	}
> > > +
> > >  	repository_state newType newHEAD newMERGE_HEAD
> > >  	if {[string match amend* $commit_type]
> > >  		&& $newType eq {normal}
-- 
Regards,
Pratyush Yadav
Johannes Schindelin· Oct 13, 2019, 22:18 UTC · re: Pratyush Yadav · lore

Re: [PATCH v4 1/1] Make gitdir work with worktrees, respect core.hooksPath, etc

Hi Pratyush,
On Mon, 14 Oct 2019, Pratyush Yadav wrote:
Show 64 quoted lines
> On 12/10/19 11:24PM, Johannes Schindelin wrote:
> > Hi Pratyush,
> >
> > On Sat, 12 Oct 2019, Pratyush Yadav wrote:
> >
> > > On 08/10/19 04:33AM, Johannes Schindelin via GitGitGadget wrote:
> > >
> > > > @@ -1453,10 +1501,16 @@ proc rescan {after {honor_trustmtime 1}} {
> > > >  	global HEAD PARENT MERGE_HEAD commit_type
> > > >  	global ui_index ui_workdir ui_comm
> > > >  	global rescan_active file_states
> > > > -	global repo_config
> > > > +	global repo_config _gitdir_cache
> > > >
> > > >  	if {$rescan_active > 0 || ![lock_index read]} return
> > > >
> > > > +	# Only re-prime gitdir cache on a full rescan
> > > > +	if {$after ne "ui_ready"} {
> > >
> > > What do you mean by a "full rescan"? I assume you use it as the
> > > differentiator between `ui_do_rescan` (called when you hit F5 or choose
> > > rescan from the menu) and `do_rescan` (called when you revert a line or
> > > hunk), and a "full rescan" refers to `ui_do_rescan`.
> > >
> > > Well in that case, this check is incorrect. `do_rescan` passes only
> > > "ui_ready" and `ui_do_rescan` passes "force_first_diff ui_ready".
> > >
> > > But either way, I'm not a big fan of this. This check makes assumptions
> > > about the behaviour of its callers based on what they pass to $after.
> > > The way I see it, $after should be a black box to `rescan`, and it
> > > should make absolutely no assumptions about it.
> > >
> > > Doing it this way is really brittle, and would break as soon as someone
> > > changes the behaviour of `ui_do_rescan`. If someone in the future passes
> > > a different value in $after, this would stop working as intended and
> > > would not refresh the cached list on a rescan.
> > >
> > > So, I think a better place for this if statement would be in
> > > `ui_do_rescan`. This would mean adding a new function that does this.
> > > But if we unset _gitdir_cache in prime_gitdir_cache (I see no reason not
> > > to), we can get away with just something like:
> > >
> > >   proc ui_do_rescan {} {
> > >   	rescan {prime_gitdir_cache; ui_ready}
> > >   }
> > >
> > > Though since `prime_gitdir_cache` does not really depend on the rescan
> > > being finished, something like this would also work fine:
> > >
> > >   proc ui_do_rescan {} {
> > >   	rescan ui_ready
> > >   	prime_gitdir_cache
> > >   }
> >
> > That was my first attempt. However, there is a very important piece of
> > code that is even still quoted above: that `if {$rescan_active > 0 ||
> > ![lock_index read]} return` part.
> >
> > I do _not_ want to interfere with an actively-going-on rescan. If there
> > is an active one, I don't want to re-prime the `_gitdir` cache.
>
> Good catch! In that case I suppose refreshing the cache in $after would
> be the way to go (IOW, the former of my two suggestions). Anything
> $after won't get executed if we return early from that check.

The obvious problem with that solution is that the `_gitdir` is reset _after_ the rescan. In my solution, it is reset _before_, as I have no idea how often the `_gitdir` values are used during a rescan (and if none of they were, I would like to be very cautious to prepare for a future where any of those `_gitdir` values _are_ used during a rescan).

So I am afraid that I find way too serious problems with both of your proposed alternatives.

Ciao, Johannes

Show 31 quoted lines
>
> > That was the reason why I put the additional code into `rescan` rather
> > than into `ui_do_rescan()`.
> >
> > Ciao,
> > Johannes
> >
> > >
> > > This would allow us to do these two things in parallel since `rescan` is
> > > asynchronous. But that would also mean it is possible that the status
> > > bar would show "Ready" while `prime_gitdir_cache` is still executing.
> > >
> > > I can't really make up my mind on what is better. I'm inclining on using
> > > the latter way, effectively trading a bit of UI inconsistency for
> > > performance (at least in theory).
> > >
> > > Thoughts?
> > >
> > > > +		array unset _gitdir_cache
> > > > +		prime_gitdir_cache
> > > > +	}
> > > > +
> > > >  	repository_state newType newHEAD newMERGE_HEAD
> > > >  	if {[string match amend* $commit_type]
> > > >  		&& $newType eq {normal}
>
> --
> Regards,
> Pratyush Yadav
>
>
Pratyush Yadav· Oct 17, 2019, 18:34 UTC · re: Johannes Schindelin · lore

Re: [PATCH v4 1/1] Make gitdir work with worktrees, respect core.hooksPath, etc

On 14/10/19 12:18AM, Johannes Schindelin wrote:
Show 74 quoted lines
> Hi Pratyush,
> 
> On Mon, 14 Oct 2019, Pratyush Yadav wrote:
> 
> > On 12/10/19 11:24PM, Johannes Schindelin wrote:
> > > Hi Pratyush,
> > >
> > > On Sat, 12 Oct 2019, Pratyush Yadav wrote:
> > >
> > > > On 08/10/19 04:33AM, Johannes Schindelin via GitGitGadget wrote:
> > > >
> > > > > @@ -1453,10 +1501,16 @@ proc rescan {after {honor_trustmtime 1}} {
> > > > >  	global HEAD PARENT MERGE_HEAD commit_type
> > > > >  	global ui_index ui_workdir ui_comm
> > > > >  	global rescan_active file_states
> > > > > -	global repo_config
> > > > > +	global repo_config _gitdir_cache
> > > > >
> > > > >  	if {$rescan_active > 0 || ![lock_index read]} return
> > > > >
> > > > > +	# Only re-prime gitdir cache on a full rescan
> > > > > +	if {$after ne "ui_ready"} {
> > > >
> > > > What do you mean by a "full rescan"? I assume you use it as the
> > > > differentiator between `ui_do_rescan` (called when you hit F5 or choose
> > > > rescan from the menu) and `do_rescan` (called when you revert a line or
> > > > hunk), and a "full rescan" refers to `ui_do_rescan`.
> > > >
> > > > Well in that case, this check is incorrect. `do_rescan` passes only
> > > > "ui_ready" and `ui_do_rescan` passes "force_first_diff ui_ready".
> > > >
> > > > But either way, I'm not a big fan of this. This check makes assumptions
> > > > about the behaviour of its callers based on what they pass to $after.
> > > > The way I see it, $after should be a black box to `rescan`, and it
> > > > should make absolutely no assumptions about it.
> > > >
> > > > Doing it this way is really brittle, and would break as soon as someone
> > > > changes the behaviour of `ui_do_rescan`. If someone in the future passes
> > > > a different value in $after, this would stop working as intended and
> > > > would not refresh the cached list on a rescan.
> > > >
> > > > So, I think a better place for this if statement would be in
> > > > `ui_do_rescan`. This would mean adding a new function that does this.
> > > > But if we unset _gitdir_cache in prime_gitdir_cache (I see no reason not
> > > > to), we can get away with just something like:
> > > >
> > > >   proc ui_do_rescan {} {
> > > >   	rescan {prime_gitdir_cache; ui_ready}
> > > >   }
> > > >
> > > > Though since `prime_gitdir_cache` does not really depend on the rescan
> > > > being finished, something like this would also work fine:
> > > >
> > > >   proc ui_do_rescan {} {
> > > >   	rescan ui_ready
> > > >   	prime_gitdir_cache
> > > >   }
> > >
> > > That was my first attempt. However, there is a very important piece of
> > > code that is even still quoted above: that `if {$rescan_active > 0 ||
> > > ![lock_index read]} return` part.
> > >
> > > I do _not_ want to interfere with an actively-going-on rescan. If there
> > > is an active one, I don't want to re-prime the `_gitdir` cache.
> >
> > Good catch! In that case I suppose refreshing the cache in $after would
> > be the way to go (IOW, the former of my two suggestions). Anything
> > $after won't get executed if we return early from that check.
> 
> The obvious problem with that solution is that the `_gitdir` is reset
> _after_ the rescan. In my solution, it is reset _before_, as I have no
> idea how often the `_gitdir` values are used during a rescan (and if
> none of they were, I would like to be very cautious to prepare for a
> future where any of those `_gitdir` values _are_ used during a rescan).
_gitdir values are used quite often during a rescan, so yes, this won't 
work.
 
> So I am afraid that I find way too serious problems with both of your
> proposed alternatives.

One alternative I can see right now is adding another optional parameter to `rescan` that controls whether we refresh the gitdir cache or not. That parameter would default to 0/false. I'm not the biggest fan of something like this, but it might be the easiest way to do it given the constraints.

I also thought about trying to acquire the index lock in `prime_gitdir_cache`, but that could create a race on the lock between `prime_gitdir_cache` and `rescan`.

If you have any better ideas, I'm all ears, but otherwise, I maybe our best bet is to just go with adding an optional parameter to `rescan`.

-- 
Regards,
Pratyush Yadav
Johannes Schindelin· Oct 14, 2019, 08:14 UTC · re: Pratyush Yadav · lore

Re: [PATCH v4 1/1] Make gitdir work with worktrees, respect core.hooksPath, etc

Hi Pratyush,
On Mon, 14 Oct 2019, Pratyush Yadav wrote:
Show 63 quoted lines
> On 12/10/19 11:24PM, Johannes Schindelin wrote:
> >
> > On Sat, 12 Oct 2019, Pratyush Yadav wrote:
> >
> > > On 08/10/19 04:33AM, Johannes Schindelin via GitGitGadget wrote:
> > >
> > > > @@ -1453,10 +1501,16 @@ proc rescan {after {honor_trustmtime 1}} {
> > > >  	global HEAD PARENT MERGE_HEAD commit_type
> > > >  	global ui_index ui_workdir ui_comm
> > > >  	global rescan_active file_states
> > > > -	global repo_config
> > > > +	global repo_config _gitdir_cache
> > > >
> > > >  	if {$rescan_active > 0 || ![lock_index read]} return
> > > >
> > > > +	# Only re-prime gitdir cache on a full rescan
> > > > +	if {$after ne "ui_ready"} {
> > >
> > > What do you mean by a "full rescan"? I assume you use it as the
> > > differentiator between `ui_do_rescan` (called when you hit F5 or choose
> > > rescan from the menu) and `do_rescan` (called when you revert a line or
> > > hunk), and a "full rescan" refers to `ui_do_rescan`.
> > >
> > > Well in that case, this check is incorrect. `do_rescan` passes only
> > > "ui_ready" and `ui_do_rescan` passes "force_first_diff ui_ready".
> > >
> > > But either way, I'm not a big fan of this. This check makes assumptions
> > > about the behaviour of its callers based on what they pass to $after.
> > > The way I see it, $after should be a black box to `rescan`, and it
> > > should make absolutely no assumptions about it.
> > >
> > > Doing it this way is really brittle, and would break as soon as someone
> > > changes the behaviour of `ui_do_rescan`. If someone in the future passes
> > > a different value in $after, this would stop working as intended and
> > > would not refresh the cached list on a rescan.
> > >
> > > So, I think a better place for this if statement would be in
> > > `ui_do_rescan`. This would mean adding a new function that does this.
> > > But if we unset _gitdir_cache in prime_gitdir_cache (I see no reason not
> > > to), we can get away with just something like:
> > >
> > >   proc ui_do_rescan {} {
> > >   	rescan {prime_gitdir_cache; ui_ready}
> > >   }
> > >
> > > Though since `prime_gitdir_cache` does not really depend on the rescan
> > > being finished, something like this would also work fine:
> > >
> > >   proc ui_do_rescan {} {
> > >   	rescan ui_ready
> > >   	prime_gitdir_cache
> > >   }
> >
> > That was my first attempt. However, there is a very important piece of
> > code that is even still quoted above: that `if {$rescan_active > 0 ||
> > ![lock_index read]} return` part.
> >
> > I do _not_ want to interfere with an actively-going-on rescan. If there
> > is an active one, I don't want to re-prime the `_gitdir` cache.
>
> Good catch! In that case I suppose refreshing the cache in $after would
> be the way to go (IOW, the former of my two suggestions). Anything
> $after won't get executed if we return early from that check.

But it also won't get executed before the actual rescan. Which is precisely what I tried to ensure.

Ciao, Johannes

Show 30 quoted lines
>
> > That was the reason why I put the additional code into `rescan` rather
> > than into `ui_do_rescan()`.
> >
> > Ciao,
> > Johannes
> >
> > >
> > > This would allow us to do these two things in parallel since `rescan` is
> > > asynchronous. But that would also mean it is possible that the status
> > > bar would show "Ready" while `prime_gitdir_cache` is still executing.
> > >
> > > I can't really make up my mind on what is better. I'm inclining on using
> > > the latter way, effectively trading a bit of UI inconsistency for
> > > performance (at least in theory).
> > >
> > > Thoughts?
> > >
> > > > +		array unset _gitdir_cache
> > > > +		prime_gitdir_cache
> > > > +	}
> > > > +
> > > >  	repository_state newType newHEAD newMERGE_HEAD
> > > >  	if {[string match amend* $commit_type]
> > > >  		&& $newType eq {normal}
>
> --
> Regards,
> Pratyush Yadav
>

← back to recent threads