Skip to contentSkip to conversation

BUG: git-gui no longer executes hook scripts

PATCH Superseded Mark Levedahl 4 participants 25 messages Sep 15, 2023 – Sep 20, 2023

lore
Share & tools
lore
#0 lore raw

The commit titled "Work around Tcl's default |PATH| lookup",|aae9560, adds checking on all commands to be executed to assure these are on the PATH. Any script in .git/hooks is rejected as .git/hooks is not (in general) on the PATH, even if the entry in .git/hooks is a symlink to a file on the PATH. Instead, git-gui throws and error without completing the operation. This is easily demonstrated by say, enabling the commit-msg script (hooks-commit-msg.sample templates) and attempting a commit. |

|I don't have a suggested solution to this: reverting the above commit will fix this problem, but that commit  was made to mitigate a security issue.  Perhaps anything in .git/hooks should be accepted without further checks? |

|
|

|Mark
|

#1 lore raw
Quoted 3 lines · Mark Levedahl

The commit titled "Work around Tcl's default |PATH| lookup",|aae9560, adds checking on all commands to be executed to assure these are on the PATH.

commit aae9560a355d4ab91385e49eae62fade2ddd27ef

Author: Johannes Schindelin <[email protected]>
Date:   Wed Nov 23 09:31:06 2022 +0100

Work around Tcl's default PATH lookup

As per https://www.tcl.tk/man/tcl8.6/TclCmd/exec.html#M23, Tcl's exec function goes out of its way to imitate the highly dangerous path lookup of cmd.exe, but _of course_ only on Windows:

If a directory name was not specified as part of the application name, the following directories are automatically searched in order when attempting to locate the application:

In other words, if somebody tries to run ".git/hooks/pre-commit", because a directory name _is_ given (i.e. ".git/hooks/" in this case), the path lookup is *not* done. Which is what I would expect, and then "oh, only on Windows to match what cmd.exe does, the current directory is early in the search order" should not be a problem.

To avoid that, Git GUI already has the _which function that does not imitate that dangerous practice when looking up executables in the search path.

Sounds good, but ...

git-gui.sh +10 -0
index b0eb5a6ae4..cb92bba1c4 100755
@@ -121,6 +121,62 @@ proc _which {what args} {
 	return {}
 }
 
+proc sanitize_command_line {command_line from_index} {
+	set i $from_index
+	while {$i < [llength $command_line]} {
+		set cmd [lindex $command_line $i]
+		if {[file pathtype $cmd] ne "absolute"} {
+			set fullpath [_which $cmd]
+			if {$fullpath eq ""} {
+				throw {NOT-FOUND} "$cmd not found in PATH"
+			}
+			lset command_line $i $fullpath
 

Shouldn't this "is it absolute" check with "$cmd" also check if $cmd has either forward or backward slash in it? I do not know about the Windows cmd.exe convention, but with Unix background, I would be surprised if dir/cmd gave by end users ran "C:\program files\dir\cmd" (unless I happened to be in the "C:\program files\" folder, that is).

Checking the use of _which with fixed arguments, it is used to spawn git, gitk, nice, sh; and _which finding where they appear on the search path does sound sane. But _which does not seem to have the "if given a command with directory separator, the search path does not matter. The caller means it is relative to the $cwd" logic at all, so it seems it is the callers responsibility to make sure it does not pass things like ".git/hooks/pre-commit" to it.

+ }
+
+ # handle piped commands, e.g. exec A | B
+ for {incr i} {$i < [llength $command_line]} {incr i} {
+ if {[lindex $command_line $i] eq "|"} {
+ incr i
+ break
+ }
+ }
+ }
+ return $command_line
+}
+
+# Override exec to avoid unsafe PATH lookup
+
+rename exec real_exec
+
+proc exec {args} {
+ # skip options
+ for {set i 0} {$i < [llength $args]} {incr i} {
+ set arg [lindex $args $i]
+ if {$arg eq "--"} {
+ incr i
+ break
+ }
+ if {[string range $arg 0 0] ne "-"} {
+ break
+ }
+ }
+ set args [sanitize_command_line $args $i]
+ uplevel 1 real_exec $args
+}
+
+# Override open to avoid unsafe PATH lookup
+
+rename open real_open
+
+proc open {args} {
+ set arg0 [lindex $args 0]
+ if {[string range $arg0 0 0] eq "|"} {
+ set command_line [string trim [string range $arg0 1 end]]
+ lset args 0 "| [sanitize_command_line $command_line 0]"
+ }
+ uplevel 1 real_open $args
+}
+
######################################################################
##
## locate our library

#2 lore raw
Quoted 10 lines · Junio C Hamano

Shouldn't this "is it absolute" check with "$cmd" also check if $cmd has either forward or backward slash in it?

Checking the use of _which with fixed arguments, it is used to spawn git, gitk, nice, sh; and _which finding where they appear on the search path does sound sane. But _which does not seem to have the "if given a command with directory separator, the search path does not matter. The caller means it is relative to the $cwd" logic at all, so it seems it is the callers responsibility to make sure it does not pass things like ".git/hooks/pre-commit" to it.

In other words, something along this line may go in the right direction (I no longer speak Tcl, and this is done with manual in one hand, while typing with the other hand).

 git-gui.sh | 12 ++++++++----
 1 file changed, 8 insertions(+), 4 deletions(-)
git-gui/git-gui.sh +8 -4
index 8bc8892c40..45d8f48b39 100755
@@ -119,11 +119,15 @@ proc sanitize_command_line {command_line from_index} {
 	while {$i < [llength $command_line]} {
 		set cmd [lindex $command_line $i]
 		if {[file pathtype $cmd] ne "absolute"} {
-			set fullpath [_which $cmd]
-			if {$fullpath eq ""} {
-				throw {NOT-FOUND} "$cmd not found in PATH"
+			if {1 < [llength [file split $cmd]]]} {
+			    set cmdpath [_which $cmd]
+			    if {$cmdpath eq ""} {
+				    throw {NOT-FOUND} "$cmd not found in PATH"
+			    }
+			} else {
+				set cmdpath $cmd
 			}
-			lset command_line $i $fullpath
+			lset command_line $i $cmdpath
 		}
 
 		# handle piped commands, e.g. `exec A | B`
#3 lore raw
Quoted 12 lines · Junio C Hamano
Quoted 10 lines · Junio C Hamano

Shouldn't this "is it absolute" check with "$cmd" also check if $cmd has either forward or backward slash in it?

Checking the use of _which with fixed arguments, it is used to spawn git, gitk, nice, sh; and _which finding where they appear on the search path does sound sane. But _which does not seem to have the "if given a command with directory separator, the search path does not matter. The caller means it is relative to the $cwd" logic at all, so it seems it is the callers responsibility to make sure it does not pass things like ".git/hooks/pre-commit" to it.

Quoted 4 lines

In other words, something along this line may go in the right direction (I no longer speak Tcl, and this is done with manual in one hand, while typing with the other hand).

I think a simpler fix is just to examine the number of path components - more than one means a relative or absolute command (/foo splits into two parts). The below works for me on Linux.

git-gui/git-gui.sh +0 -0
index 277a2b1c8c..0c39d9fa26 100755
@@ -118,7 +118,7 @@ proc sanitize_command_line {command_line from_index} {

    set i $from_index
    while {$i < [llength $command_line]} {
        set cmd [lindex $command_line $i]
-       if {[file pathtype $cmd] ne "absolute"} { +       if {[llength [file split $cmd]] < 2} { set fullpath [_which $cmd]
            if {$fullpath eq ""} {
                throw {NOT-FOUND} "$cmd not found in PATH"

We could also wrap the entirety of commit aae9560a in

    if {[is_Windows]} { ... }

as all of this code is fixing a Windows specific vulnerability, though a fix like the above is needed regardless.

Mark

#4 lore raw

PATCH 1/1 git-gui - re-enable use of hook scripts

Commit aae9560a introduced search in $PATH to find executables before running them, avoiding an issue where on Windows a same named file in the current directory can be executed in preference to anything on the path. The updated search excludes files given with an absolute path (e.g., /bin/sh). However this change precludes operation of hook scripts as these are named with a relative path (.git/hooks/$HOOK), while a search on $PATH can succeed only for bare file names, not relative paths. Furthermore, the current repository's .git/hooks directory is in general not listed in PATH.

Fix this by changing the "absolute" check to a check for more than one component in the pathname, thereby avoiding the PATH check for anything given with a relative path as well. Bare "git" has one component, "/sh" has two components, and .git/hooks/$HOOK has more than two, so relative and absolute pathnames avoid the check.

---
 git-gui.sh | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
git-gui.sh +1 -1
index 8bc8892..8603437 100755
@@ -118,7 +118,7 @@ proc sanitize_command_line {command_line from_index} {
 	set i $from_index
 	while {$i < [llength $command_line]} {
 		set cmd [lindex $command_line $i]
-		if {[file pathtype $cmd] ne "absolute"} {
+		if {[llength [file split $cmd]] < 2} {
 			set fullpath [_which $cmd]
 			if {$fullpath eq ""} {
 				throw {NOT-FOUND} "$cmd not found in PATH"
Signature · 1 line
2.41.0.99.19
#5 lore raw

Re: [PATCH] git-gui - re-enable use of hook scripts

Quoted 10 lines · Mark Levedahl

Commit aae9560a introduced search in $PATH to find executables before running them, avoiding an issue where on Windows a same named file in the current directory can be executed in preference to anything on the path. The updated search excludes files given with an absolute path (e.g., /bin/sh). However this change precludes operation of hook scripts as these are named with a relative path (.git/hooks/$HOOK), while a search on $PATH can succeed only for bare file names, not relative paths. Furthermore, the current repository's .git/hooks directory is in general not listed in PATH.

Quoted 8 lines

Fix this by changing the "absolute" check to a check for more than one component in the pathname, thereby avoiding the PATH check for anything given with a relative path as well. Bare "git" has one component, "/sh" has two components, and .git/hooks/$HOOK has more than two, so relative and absolute pathnames avoid the check.

---

With your experiments in the other thread, I think this is quite a reasonable fix to the problem. I'd prefer a few updates to the proposed log message above, though.

* Refer the older commit like so:

Earlier, aae9560a (Work around Tcl's default PATH lookup, 2022-11-23) introduced ...

* It would help readers if you clarify that "The updated search excludes ..." and the rest of that paragraph of the log gives a bug/problem/undesirable behaviour of the current code introduced by the earlier change. Perhaps something along the lines of ...

The updated search excludes commands given as an absolute path (e.g., /bin/sh), which is good, but it also tries to find commands given as a path relative to the current directory with directory separator (e.g., .git/hooks/pre-commit), which makes the hooks from running at all. We only want to apply the $PATH logic to a token without any directory separator in it.

* Mention that we already know the new logic works for absolute paths even on Windows by tweaking the sentence starting with "Bare 'git' has ...". Something like:

Bare "git" has one component (which we want to use $PATH), "/bin/sh", "C:\some\command", and ".git/hooks/$HOOK" all split into 2 or more (which we want to use as-is). The only case we want to use $PATH is when result of [file split] has only one element.

But other than that it looks good.

Dscho?

Quoted 16 lines
 git-gui.sh | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
git-gui.sh +1 -1
index 8bc8892..8603437 100755
@@ -118,7 +118,7 @@ proc sanitize_command_line {command_line from_index} {
 	set i $from_index
 	while {$i < [llength $command_line]} {
 		set cmd [lindex $command_line $i]
-		if {[file pathtype $cmd] ne "absolute"} {
+		if {[llength [file split $cmd]] < 2} {
 			set fullpath [_which $cmd]
 			if {$fullpath eq ""} {
 				throw {NOT-FOUND} "$cmd not found in PATH"
#6 lore raw

PATCH v2 1/1 git-gui - re-enable use of hook scripts

Earlier, commit aae9560a introduced search in $PATH to find executables before running them, avoiding an issue where on Windows a same named file in the current directory can be executed in preference to anything in a directory in $PATH. This search is intended to find an absolute path for a bare executable ( e.g, a function "foo") by finding the first instance of "foo" in a directory given in $PATH, and this search works correctly. The search is explicitly avoided for an executable named with an absolute path (e.g., /bin/sh), and that works as well.

Unfortunately, the search is also applied to commands named with a relative path. A hook script (or executable) $HOOK is usually located relative to the project directory as .git/hooks/$HOOK. The search for this will generally fail as that relative path will (probably) not exist on any directory in $PATH. This means that git hooks in general now fail to run. Considerable mayhem could occur should a directory on $PATH be git controlled. If such a directory includes .git/hooks/$HOOK, that repository's $HOOK will be substituted for the one in the current project, with unknown consequences.

This lookup failure also occurs in worktrees linked to a remote .git directory using git-new-workdir. However, a worktree using a .git file pointing to a separate git directory apparently avoids this: in that case the hook command is resolved to an absolute path before being passed down to the code introduced in aae9560a.

Fix this by replacing the test for an "absolute" pathname to a check for a command name having more than one pathname component. This limits the search and absolute pathname resolution to bare commands. The new test uses tcl's "file split" command. Experiments on Linux and Windows, using tclsh, show that command names with relative and absolute paths always give at least two components, while a bare command gives only one.

	  Linux:   puts [file split {foo}]       ==>  foo
	  Linux:   puts [file split {/foo}]      ==>  / foo
	  Linux:   puts [file split {.git/foo}]  ==> .git foo
	  Windows: puts [file split {foo}]       ==>  foo
	  Windows: puts [file split {c:\foo}]    ==>  c:/ foo
	  Windows: puts [file split {.git\foo}]  ==> .git foo

The above results show the new test limits search and replacement to bare commands on both Linux and Windows.

---
 git-gui.sh | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
git-gui.sh +1 -1
index 8bc8892..8603437 100755
@@ -118,7 +118,7 @@ proc sanitize_command_line {command_line from_index} {
 	set i $from_index
 	while {$i < [llength $command_line]} {
 		set cmd [lindex $command_line $i]
-		if {[file pathtype $cmd] ne "absolute"} {
+		if {[llength [file split $cmd]] < 2} {
 			set fullpath [_which $cmd]
 			if {$fullpath eq ""} {
 				throw {NOT-FOUND} "$cmd not found in PATH"
Signature · 1 line
2.41.0.99.19
#7 lore raw

Re: [PATCH v2] git-gui - re-enable use of hook scripts

Quoted 10 lines · Mark Levedahl

uses tcl's "file split" command. Experiments on Linux and Windows, using tclsh, show that command names with relative and absolute paths always give at least two components, while a bare command gives only one.

	  Linux:   puts [file split {foo}]       ==>  foo
	  Linux:   puts [file split {/foo}]      ==>  / foo
	  Linux:   puts [file split {.git/foo}]  ==> .git foo
	  Windows: puts [file split {foo}]       ==>  foo
	  Windows: puts [file split {c:\foo}]    ==>  c:/ foo
	  Windows: puts [file split {.git\foo}]  ==> .git foo

;-) Nice documentation of what you found out.

git-gui.sh proc sanitize_command_line {command_line from_index} { · line 124

Quoted 13 lines
git-gui.sh +1 -1
index 8bc8892..8603437 100755
@@ -118,7 +118,7 @@ proc sanitize_command_line {command_line from_index} {
 	set i $from_index
 	while {$i < [llength $command_line]} {
 		set cmd [lindex $command_line $i]
-		if {[file pathtype $cmd] ne "absolute"} {
+		if {[llength [file split $cmd]] < 2} {
 			set fullpath [_which $cmd]
 			if {$fullpath eq ""} {
 				throw {NOT-FOUND} "$cmd not found in PATH"

Nice. Now we need to find a replacement maintainer for Git-gui ;-) In the meantime, I can queue this patch on top of what I updated git-gui part the last time with and merge it in.

Thanks.

#8 lore raw

Re: [PATCH v2] git-gui - re-enable use of hook scripts

Quoted 6 lines · Junio C Hamano

Nice. Now we need to find a replacement maintainer for Git-gui ;-) In the meantime, I can queue this patch on top of what I updated git-gui part the last time with and merge it in.

Thanks.

Thank you for help on this too. I retired some time ago, and stopped using git much a decade ago. My popping up on the list was inspired by cleaning out an old laptop and finding some old patches I thought would be useful, especially as I'd helped Shawn create some of that old git-gui/Cygwin code. My interest is unlikely to endure so I'm definitely not a good candidate to maintain git-gui.

On this hook execution problem, looking further, I find using git-hook run will fix some other issues in git-gui's hook handling, and that would actually also patch around the problem we just fixed. So, another patch follows, the commit message presumes the one fixing relative path search remains. I would suggest keeping the one fixing the relative path search regardless.

Mark

#9 lore raw

PATCH 1/1 git-gui - use git-hook, honor core.hooksPath

git-gui currently runs some hooks directly using its own code written before 2010, long predating git v2.9 that added the core.hooksPath configuration to override the assumed location at $GIT_DIR/hooks. Thus, git-gui looks for and runs hooks including prepare-commit-msg, commit-msg, pre-commit, post-commit, and post-checkout from $GIT_DIR/hooks, regardless of configuration. Commands (e.g., git-merge) that git-gui invokes directly do honor core.hooksPath, meaning the overall behaviour is inconsistent.

Furthermore, since v2.36 git exposes its hook exection machinery via git-hook run, eliminating the need for others to maintain code duplicating that functionality. Using git-hook will both fix git-gui's current issues on hook configuration and (presumably) reduce the maintenance burden going forward. So, teach git-gui to use git-hook.

---
 git-gui.sh | 27 ++-------------------------
 1 file changed, 2 insertions(+), 25 deletions(-)
git-gui.sh +2 -25
index 8603437..3e5907a 100755
@@ -661,31 +661,8 @@ proc git_write {args} {
 }
 
 proc githook_read {hook_name args} {
-	set pchook [gitdir hooks $hook_name]
-	lappend args 2>@1
-
-	# On Windows [file executable] might lie so we need to ask
-	# the shell if the hook is executable.  Yes that's annoying.
-	#
-	if {[is_Windows]} {
-		upvar #0 _sh interp
-		if {![info exists interp]} {
-			set interp [_which sh]
-		}
-		if {$interp eq {}} {
-			error "hook execution requires sh (not in PATH)"
-		}
-
-		set scr {if test -x "$1";then exec "$@";fi}
-		set sh_c [list $interp -c $scr $interp $pchook]
-		return [_open_stdout_stderr [concat $sh_c $args]]
-	}
-
-	if {[file executable $pchook]} {
-		return [_open_stdout_stderr [concat [list $pchook] $args]]
-	}
-
-	return {}
+	set cmd [concat git hook run --ignore-missing $hook_name -- $args 2>@1]
+	return [_open_stdout_stderr $cmd]
 }
 
 proc kill_file_process {fd} {
Signature · 1 line
2.41.0.99.19
#10 lore raw

Re: [PATCH] git-gui - use git-hook, honor core.hooksPath

Hi Mark,

Quoted 45 lines · Mark Levedahl

git-gui currently runs some hooks directly using its own code written before 2010, long predating git v2.9 that added the core.hooksPath configuration to override the assumed location at $GIT_DIR/hooks. Thus, git-gui looks for and runs hooks including prepare-commit-msg, commit-msg, pre-commit, post-commit, and post-checkout from $GIT_DIR/hooks, regardless of configuration. Commands (e.g., git-merge) that git-gui invokes directly do honor core.hooksPath, meaning the overall behaviour is inconsistent.

Furthermore, since v2.36 git exposes its hook exection machinery via git-hook run, eliminating the need for others to maintain code duplicating that functionality. Using git-hook will both fix git-gui's current issues on hook configuration and (presumably) reduce the maintenance burden going forward. So, teach git-gui to use git-hook.

---
 git-gui.sh | 27 ++-------------------------
 1 file changed, 2 insertions(+), 25 deletions(-)
git-gui.sh +0 -17
index 8603437..3e5907a 100755
@@ -661,31 +661,8 @@ proc git_write {args} {
 }
 
 proc githook_read {hook_name args} {
-	set pchook [gitdir hooks $hook_name]
-	lappend args 2>@1
-
-	# On Windows [file executable] might lie so we need to ask
-	# the shell if the hook is executable.  Yes that's annoying.
-	#
-	if {[is_Windows]} {
-		upvar #0 _sh interp
-		if {![info exists interp]} {
-			set interp [_which sh]
-		}
-		if {$interp eq {}} {
-			error "hook execution requires sh (not in PATH)"
-		}
-
-		set scr {if test -x "$1";then exec "$@";fi}
-		set sh_c [list $interp -c $scr $interp $pchook]

git-gui.sh proc git_write {args} { · line 665

Quoted 10 lines
git-gui.sh +2 -8
-		return [_open_stdout_stderr [concat $sh_c $args]]
-	}
-
-	if {[file executable $pchook]} {
-		return [_open_stdout_stderr [concat [list $pchook] $args]]
-	}
-
-	return {}
+	set cmd [concat git hook run --ignore-missing $hook_name -- $args 2>@1]
+	return [_open_stdout_stderr $cmd]

This looks so much nicer than the original code.

Thank you,
Johannes

Quoted 7 lines
git-gui.sh +0 -1
 }
 
 proc kill_file_process {fd} {
--

2.41.0.99.19

#11 lore raw

Re: [PATCH] git-gui - use git-hook, honor core.hooksPath

Quoted 7 lines · Johannes Schindelin

git-gui.sh proc git_write {args} { · line 665

Quoted 2 lines
git-gui.sh +2 -0
+	set cmd [concat git hook run --ignore-missing $hook_name -- $args 2>@1]
+	return [_open_stdout_stderr $cmd]

This looks so much nicer than the original code.

Thank you,
Johannes

Yup, looking good.

#12 lore raw

Re: [PATCH] git-gui - use git-hook, honor core.hooksPath

Quoted 9 lines · Junio C Hamano
Quoted 6 lines · Johannes Schindelin

git-gui.sh proc git_write {args} { · line 665

Quoted 2 lines
git-gui.sh +2 -0
+	set cmd [concat git hook run --ignore-missing $hook_name -- $args 2>@1]
+	return [_open_stdout_stderr $cmd]

This looks so much nicer than the original code.

Thank you,
Johannes

Yup, looking good.

Thanks. BTW, my commit message at "Furthermore, since v2.36 git exposes its hook exection machinery via" needs

    s/exection/execution/

Should I resend?

Mark

#13 lore raw

Re: [PATCH] git-gui - use git-hook, honor core.hooksPath

Quoted 9 lines · Mark Levedahl
Quoted 8 lines · Junio C Hamano
Quoted 6 lines · Johannes Schindelin

git-gui.sh proc git_write {args} { · line 665

Quoted 2 lines
git-gui.sh +2 -0
+	set cmd [concat git hook run --ignore-missing $hook_name -- $args 2>@1]
+	return [_open_stdout_stderr $cmd]

This looks so much nicer than the original code.

Thank you,
Johannes

Quoted 8 lines
Quoted 1 line

Yup, looking good.

Thanks. BTW, my commit message at "Furthermore, since v2.36 git exposes its hook exection machinery via" needs

    s/exection/execution/

Should I resend?

Nah, I'll just fix it up locally before merging.

#14 lore raw

Re: [PATCH] git-gui - use git-hook, honor core.hooksPath

In reply to Mark Levedahl

Hi,

Thanks for the patch.

Quoted 9 lines · Mark Levedahl

git-gui currently runs some hooks directly using its own code written before 2010, long predating git v2.9 that added the core.hooksPath configuration to override the assumed location at $GIT_DIR/hooks. Thus, git-gui looks for and runs hooks including prepare-commit-msg, commit-msg, pre-commit, post-commit, and post-checkout from $GIT_DIR/hooks, regardless of configuration. Commands (e.g., git-merge) that git-gui invokes directly do honor core.hooksPath, meaning the overall behaviour is inconsistent.

Quoted 5 lines

Furthermore, since v2.36 git exposes its hook exection machinery via git-hook run, eliminating the need for others to maintain code duplicating that functionality. Using git-hook will both fix git-gui's current issues on hook configuration and (presumably) reduce the maintenance burden going forward. So, teach git-gui to use git-hook.

In the past, git-gui has tried to keep backward compatibility with all versions of Git, not just the latest ones. v2.36 is relatively new and this code would not work for anyone using an older version of Git.

I have largely followed this practice for all the code I have written but I am not sure if it is a good idea to insist on it -- especially if it would end up adding some more complexity. I would be interested to hear what other people think about this.

Junio, I was under the impression that I would keep maintaining the tree until we found a replacement maintainer. If you are okay with being the interim maintainer, that sounds good to me. Let me know what works best.

I have applied another patch since my last pull request. So I can apply this one, send you a new one and sync our trees.

Quoted 31 lines
---
 git-gui.sh | 27 ++-------------------------
 1 file changed, 2 insertions(+), 25 deletions(-)
git-gui.sh +0 -17
index 8603437..3e5907a 100755
@@ -661,31 +661,8 @@ proc git_write {args} {
 }
 
 proc githook_read {hook_name args} {
-	set pchook [gitdir hooks $hook_name]
-	lappend args 2>@1
-
-	# On Windows [file executable] might lie so we need to ask
-	# the shell if the hook is executable.  Yes that's annoying.
-	#
-	if {[is_Windows]} {
-		upvar #0 _sh interp
-		if {![info exists interp]} {
-			set interp [_which sh]
-		}
-		if {$interp eq {}} {
-			error "hook execution requires sh (not in PATH)"
-		}
-
-		set scr {if test -x "$1";then exec "$@";fi}
-		set sh_c [list $interp -c $scr $interp $pchook]

git-gui.sh proc git_write {args} { · line 665

Quoted 10 lines
git-gui.sh +2 -8
-		return [_open_stdout_stderr [concat $sh_c $args]]
-	}
-
-	if {[file executable $pchook]} {
-		return [_open_stdout_stderr [concat [list $pchook] $args]]
-	}
-
-	return {}
+	set cmd [concat git hook run --ignore-missing $hook_name -- $args 2>@1]
+	return [_open_stdout_stderr $cmd]

LGTM, other than my concerns with backward compatibility.

Quoted 3 lines
git-gui.sh +0 -0
 }
 
 proc kill_file_process {fd} {
Signature · 2 lines
Regards,
Pratyush Yadav
#15 lore raw

Re: [PATCH] git-gui - use git-hook, honor core.hooksPath

Quoted 9 lines · Pratyush Yadav

In the past, git-gui has tried to keep backward compatibility with all versions of Git, not just the latest ones. v2.36 is relatively new and this code would not work for anyone using an older version of Git.

I have largely followed this practice for all the code I have written but I am not sure if it is a good idea to insist on it -- especially if it would end up adding some more complexity. I would be interested to hear what other people think about this.

I am not aware of any distribution (Linux, g4w, Mac) shipping anything except the git-gui in Junio's tree, which is specific to the git-core version, and the git-gui packages require (or are a part of) the same version git-core package: no cross-version compatibility of git components is assumed. Certainly, folks rolling their own can pull from upstream git-gui, but they take the risk of incompatibility with an outdated git. Other tools in Junio's tree have already made the switch to git-hook (send-email, git-p4) even though they are usually packaged separately from git-core, but also version locked to matching git-core.

Updating git-gui's hook execution to match git internals would be more complex than what I implemented or what was there before.  For instance, I never looked at what git-hook's g4w compatibility code uses to test if a hook is present and executable, it wouldn't surprise me to find git-gui was missing something there, but who wants to bother? Also, the commit language surrounding addition of git-hook is strongly suggestive of other changes in configuration coming, meaning more changes to hook execution code would be needed that are avoided by using git-hook. Note: I have one more patch to send, removing yet another work-around for early Cygwin tcl/tk, as more evidence of how many years it takes to clean some of this stuff out and the difficulty of keeping git-gui up to date.

I had considered the above when creating the patch, and I believe what I did is the right approach.

Mark

#16 lore raw

Re: [PATCH] git-gui - use git-hook, honor core.hooksPath

Quoted 6 lines · Mark Levedahl

Certainly, folks rolling their own can pull
from upstream git-gui, but they take the risk of incompatibility with an outdated git. Other tools in Junio's tree have already made the switch to git-hook (send-email, git-p4) even though they are usually packaged separately from git-core, but also version locked to matching git-core.

The cross-version compatibility story is the same for "gitk" (which I believe "git-gui" took the "do not too deeply depend on the matching version of git" mantra from). I can understand the desire and being able to aim for wider compatibility may be an advantage for these tools that are not tightly bundled with the rest of the system. It allowed them to evolve without waiting for Git to catch up, for example.

But at this point in their history where these tools are very mature, it may be fair to say that the cross-version compatibility is becoming a lost cause.

#17 lore raw

Re: [PATCH] git-gui - use git-hook, honor core.hooksPath

In reply to Pratyush Yadav
Quoted 8 lines · Pratyush Yadav

In the past, git-gui has tried to keep backward compatibility with all versions of Git, not just the latest ones. v2.36 is relatively new and this code would not work for anyone using an older version of Git.

I have largely followed this practice for all the code I have written but I am not sure if it is a good idea to insist on it -- especially if it would end up adding some more complexity. I would be interested to hear what other people think about this.

Good point.

Quoted 3 lines

Junio, I was under the impression that I would keep maintaining the tree until we found a replacement maintainer. If you are okay with being the interim maintainer, that sounds good to me. Let me know what works best.

I am actually not OK ;-).

I prefer to see somebody who does use git-gui, or at least somebody who uses Git in a graphical environment in their daily work, to be maintaining it. I am disqualified on both counts.

Quoted 2 lines

I have applied another patch since my last pull request. So I can apply this one, send you a new one and sync our trees.

OK. I'll drop the copy I have on my end when it happens, then.

Thanks.

#18 lore raw

Re: [PATCH v2] git-gui - re-enable use of hook scripts

In reply to Mark Levedahl

Hi,

Quoted 39 lines · Mark Levedahl

Earlier, commit aae9560a introduced search in $PATH to find executables before running them, avoiding an issue where on Windows a same named file in the current directory can be executed in preference to anything in a directory in $PATH. This search is intended to find an absolute path for a bare executable ( e.g, a function "foo") by finding the first instance of "foo" in a directory given in $PATH, and this search works correctly. The search is explicitly avoided for an executable named with an absolute path (e.g., /bin/sh), and that works as well.

Unfortunately, the search is also applied to commands named with a relative path. A hook script (or executable) $HOOK is usually located relative to the project directory as .git/hooks/$HOOK. The search for this will generally fail as that relative path will (probably) not exist on any directory in $PATH. This means that git hooks in general now fail to run. Considerable mayhem could occur should a directory on $PATH be git controlled. If such a directory includes .git/hooks/$HOOK, that repository's $HOOK will be substituted for the one in the current project, with unknown consequences.

This lookup failure also occurs in worktrees linked to a remote .git directory using git-new-workdir. However, a worktree using a .git file pointing to a separate git directory apparently avoids this: in that case the hook command is resolved to an absolute path before being passed down to the code introduced in aae9560a.

Fix this by replacing the test for an "absolute" pathname to a check for a command name having more than one pathname component. This limits the search and absolute pathname resolution to bare commands. The new test uses tcl's "file split" command. Experiments on Linux and Windows, using tclsh, show that command names with relative and absolute paths always give at least two components, while a bare command gives only one.

	  Linux:   puts [file split {foo}]       ==>  foo
	  Linux:   puts [file split {/foo}]      ==>  / foo
	  Linux:   puts [file split {.git/foo}]  ==> .git foo
	  Windows: puts [file split {foo}]       ==>  foo
	  Windows: puts [file split {c:\foo}]    ==>  c:/ foo
	  Windows: puts [file split {.git\foo}]  ==> .git foo
Quoted 2 lines

The above results show the new test limits search and replacement to bare commands on both Linux and Windows.

Sounds good. FWIW I ran a couple experiments here, too:

% file pathtype "C:/foo"
absolute
% file pathtype ".git/hooks"
relative
% file pathtype ".git\\hooks"
relative
% file pathtype "/foo"
volumerelative
% file pathtype "foo"
relative

The problem, therefore, is that file pathtype does not discern between a bare file name and a relative path. The proposed patch looks correct to me.

Thank you,
Johannes

Quoted 23 lines
---
 git-gui.sh | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
git-gui.sh +1 -2
index 8bc8892..8603437 100755
@@ -118,7 +118,7 @@ proc sanitize_command_line {command_line from_index} {
 	set i $from_index
 	while {$i < [llength $command_line]} {
 		set cmd [lindex $command_line $i]
-		if {[file pathtype $cmd] ne "absolute"} {
+		if {[llength [file split $cmd]] < 2} {
 			set fullpath [_which $cmd]
 			if {$fullpath eq ""} {
 				throw {NOT-FOUND} "$cmd not found in PATH"
--

2.41.0.99.19

#19 lore raw

Re: [PATCH v2] git-gui - re-enable use of hook scripts

Quoted 13 lines · Johannes Schindelin

Sounds good. FWIW I ran a couple experiments here, too:

% file pathtype "C:/foo"
absolute
% file pathtype ".git/hooks"
relative
% file pathtype ".git\\hooks"
relative
% file pathtype "/foo"
volumerelative
% file pathtype "foo"
relative

Quoted 6 lines

The problem, therefore, is that file pathtype does not discern between a bare file name and a relative path. The proposed patch looks correct to me.

Thank you,
Johannes

Yup, the other "run hooks in a more modern way using 'git hook'" patch is the right solution for the immediate breakage, but it still cannot remove this sanitize_command_line proc as we have other users and use cases where we want to use the sanitized $PATH search, so this fix is still needed.

Thanks for a quick review on both patches.

Reviewed-by on v2 1/1
#20 lore raw

Re: [PATCH v2] git-gui - re-enable use of hook scripts

In reply to Mark Levedahl

Hi,

Quoted 39 lines · Mark Levedahl

Earlier, commit aae9560a introduced search in $PATH to find executables before running them, avoiding an issue where on Windows a same named file in the current directory can be executed in preference to anything in a directory in $PATH. This search is intended to find an absolute path for a bare executable ( e.g, a function "foo") by finding the first instance of "foo" in a directory given in $PATH, and this search works correctly. The search is explicitly avoided for an executable named with an absolute path (e.g., /bin/sh), and that works as well.

Unfortunately, the search is also applied to commands named with a relative path. A hook script (or executable) $HOOK is usually located relative to the project directory as .git/hooks/$HOOK. The search for this will generally fail as that relative path will (probably) not exist on any directory in $PATH. This means that git hooks in general now fail to run. Considerable mayhem could occur should a directory on $PATH be git controlled. If such a directory includes .git/hooks/$HOOK, that repository's $HOOK will be substituted for the one in the current project, with unknown consequences.

This lookup failure also occurs in worktrees linked to a remote .git directory using git-new-workdir. However, a worktree using a .git file pointing to a separate git directory apparently avoids this: in that case the hook command is resolved to an absolute path before being passed down to the code introduced in aae9560a.

Fix this by replacing the test for an "absolute" pathname to a check for a command name having more than one pathname component. This limits the search and absolute pathname resolution to bare commands. The new test uses tcl's "file split" command. Experiments on Linux and Windows, using tclsh, show that command names with relative and absolute paths always give at least two components, while a bare command gives only one.

	  Linux:   puts [file split {foo}]       ==>  foo
	  Linux:   puts [file split {/foo}]      ==>  / foo
	  Linux:   puts [file split {.git/foo}]  ==> .git foo
	  Windows: puts [file split {foo}]       ==>  foo
	  Windows: puts [file split {c:\foo}]    ==>  c:/ foo
	  Windows: puts [file split {.git\foo}]  ==> .git foo
Quoted 4 lines

The above results show the new test limits search and replacement to bare commands on both Linux and Windows.

Looks good. Thanks.

Quoted 17 lines
---
 git-gui.sh | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
git-gui.sh +1 -1
index 8bc8892..8603437 100755
@@ -118,7 +118,7 @@ proc sanitize_command_line {command_line from_index} {
 	set i $from_index
 	while {$i < [llength $command_line]} {
 		set cmd [lindex $command_line $i]
-		if {[file pathtype $cmd] ne "absolute"} {
+		if {[llength [file split $cmd]] < 2} {
 			set fullpath [_which $cmd]
 			if {$fullpath eq ""} {
 				throw {NOT-FOUND} "$cmd not found in PATH"
Signature · 2 lines
Regards,
Pratyush Yadav
#21 lore raw
In reply to Mark Levedahl
Quoted 3 lines · Mark Levedahl

I think a simpler fix is just to examine the number of path components
- more than one means a relative or absolute command (/foo splits into two parts). The below works for me on Linux.

That is clever, but I cannot convince myself that it is not too clever for its own sake. The "pathtype" thing Dscho used in his original is documented to be aware of things like "C:\path\name", but I didn't re-read the Tcl manual page too carefully to know what "file split" does for such pathname to be certain.

#22 lore raw
Quoted 11 lines · Junio C Hamano
Quoted 3 lines · Mark Levedahl

I think a simpler fix is just to examine the number of path components
- more than one means a relative or absolute command (/foo splits into two parts). The below works for me on Linux.

That is clever, but I cannot convince myself that it is not too clever for its own sake. The "pathtype" thing Dscho used in his original is documented to be aware of things like "C:\path\name", but I didn't re-read the Tcl manual page too carefully to know what "file split" does for such pathname to be certain.

The manual does not talk about Windows explicitly. From https://www.tcl.tk/man/tcl/TclCmd/file.html#M35

*file split */name/
Returns a list whose elements are the path components in /name/. The first element of the list will have the same path type as /name/. All other elements will be relative. Path separators will be discarded unless they are needed to ensure that an element is unambiguously relative. For example, under Unix

*file split* /foo/~bar/baz

returns “*/ foo ./~bar baz*” to ensure that later commands that use the third component do not attempt to perform tilde substitution.

So, there is hope c:\foo will split into c: foo, or c:\ foo, but testing on Windows is needed. Really need Dscho or someone else from g4w to help out here.

#23 lore raw
Quoted 6 lines · Mark Levedahl

So, there is hope c:\foo will split into c: foo, or c:\ foo, but testing on Windows is needed. Really need Dscho or someone else from g4w to help out here.

I did install git for windows into a bare VM, running tclsh.exe on that

puts [file split {c:\foo}]
c:/ foo

puts [llength [file split {c:\foo}]]
2

puts [file split {hooks\foo}]
hooks foo

puts [llength [file split {hooks\foo}]]
2

puts [file split {foo}]
foo

puts [llength [file split {foo}]]
1

So, file split seems to work as needed on Windows.

#24 lore raw
Quoted 9 lines · Mark Levedahl
Quoted 8 lines · Mark Levedahl

So, there is hope c:\foo will split into c: foo, or c:\ foo, but testing on Windows is needed. Really need Dscho or someone else from g4w to help out here.

Quoted 5 lines

I did install git for windows into a bare VM, running tclsh.exe on that

puts [file split {c:\foo}]
c:/ foo

Great. That is exactly what we want to see. Thanks.