Git Mailing List Archive on lore.kernel.org
 help / color / Atom feed
* [PATCH] Fix dir sep handling of GIT_ASKPASS on Windows
@ 2020-03-23 21:13 András Kucsma via GitGitGadget
  2020-03-24 20:51 ` Junio C Hamano
  2020-03-25 13:45 ` [PATCH v2] " András Kucsma via GitGitGadget
  0 siblings, 2 replies; 13+ messages in thread
From: András Kucsma via GitGitGadget @ 2020-03-23 21:13 UTC (permalink / raw)
  To: git; +Cc: András Kucsma, Andras Kucsma

From: Andras Kucsma <r0maikx02b@gmail.com>

On Windows with git installed through cygwin, GIT_ASKPASS failed to run
for relative and absolute paths containing only backslashes as directory
separators.

The reason was that git assumed that if there are no forward slashes in
the executable path, it has to search for the executable on the PATH.

The fix is to look for OS specific directory separators, not just
forward slashes.

Signed-off-by: Andras Kucsma <r0maikx02b@gmail.com>
---
    Fix dir sep handling of GIT_ASKPASS on Windows
    
    On Windows with git installed through cygwin, GIT_ASKPASS failed to run
    for relative and absolute paths containing only backslashes as directory
    separators.
    
    The reason was that git assumed that if there are no forward slashes in
    the executable path, it has to search for the executable on the PATH.
    
    The fix is to look for OS specific directory separators, not just
    forward slashes.
    
    Signed-off-by: Andras Kucsma r0maikx02b@gmail.com [r0maikx02b@gmail.com]
    
    CC: Torsten Bögershausen tboegi@web.de [tboegi@web.de]

Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-587%2Fr0mai%2Ffix-prepare_cmd-windows-v1
Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-587/r0mai/fix-prepare_cmd-windows-v1
Pull-Request: https://github.com/gitgitgadget/git/pull/587

 run-command.c | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)

diff --git a/run-command.c b/run-command.c
index f5e1149f9b3..9fcc12ebf9c 100644
--- a/run-command.c
+++ b/run-command.c
@@ -421,12 +421,12 @@ static int prepare_cmd(struct argv_array *out, const struct child_process *cmd)
 	}
 
 	/*
-	 * If there are no '/' characters in the command then perform a path
-	 * lookup and use the resolved path as the command to exec.  If there
-	 * are '/' characters, we have exec attempt to invoke the command
-	 * directly.
+	 * If there are no dir separator characters in the command then perform
+	 * a path lookup and use the resolved path as the command to exec. If
+	 * there are dir separator characters, we have exec attempt to invoke
+	 * the command directly.
 	 */
-	if (!strchr(out->argv[1], '/')) {
+	if (find_last_dir_sep(out->argv[1]) == NULL) {
 		char *program = locate_in_PATH(out->argv[1]);
 		if (program) {
 			free((char *)out->argv[1]);

base-commit: 274b9cc25322d9ee79aa8e6d4e86f0ffe5ced925
-- 
gitgitgadget

^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: [PATCH] Fix dir sep handling of GIT_ASKPASS on Windows
  2020-03-23 21:13 [PATCH] Fix dir sep handling of GIT_ASKPASS on Windows András Kucsma via GitGitGadget
@ 2020-03-24 20:51 ` Junio C Hamano
  2020-03-25 13:45 ` [PATCH v2] " András Kucsma via GitGitGadget
  1 sibling, 0 replies; 13+ messages in thread
From: Junio C Hamano @ 2020-03-24 20:51 UTC (permalink / raw)
  To: András Kucsma via GitGitGadget; +Cc: git, András Kucsma

"András Kucsma via GitGitGadget"  <gitgitgadget@gmail.com> writes:

> From: Andras Kucsma <r0maikx02b@gmail.com>
>
> On Windows with git installed through cygwin, GIT_ASKPASS failed to run
> for relative and absolute paths containing only backslashes as directory
> separators.
>
> The reason was that git assumed that if there are no forward slashes in
> the executable path, it has to search for the executable on the PATH.

Also if I were reading the discussion correctly, there was a doubt
about locate_in_PATH() that may not work on Windows for at least two
reasons.  Is it OK to ignore these issues, and if so why?

I know if you have a full path, a broken locate_in_PATH() would be
skipped and won't cause an immediate issue, but this change to make
the code realize that "a\\b" is not asking to search in %PATH% feels
just a beginning of a fix, not the whole fix, at least to me.

> The fix is to look for OS specific directory separators, not just
> forward slashes.

Yes, but it is quite unfortunate that you would use a function that
has to scan the string to the end because it asks for the last one.

Perhaps introduce 

--------------------------------------------------
#ifndef has_dir_sep
static inline int git_has_dir_sep(const char *path)
{
	return !!strchr(path, '/');
}
#define has_dir_sep(path) git_has_dir_sep(path)
#endif
--------------------------------------------------

in <git-compat-util.h>, with a replacement definition in
<compat/win32/path-utils.h> that may read

--------------------------------------------------
#define has_dir_sep(path) win32_has_dir_sep(path)
static inline int has_dir_sep(const char *path)
{
        /* 
         * See how long the non-separator part of the given path is, and
         * if and only if it covers the whole path (i.e. path[len] is NUL),
         * there is no separator in the path---otherwise there is a separaptor.
         */
        size_t len = strcspn(path, "/\\");
        return !!path[len];
}
--------------------------------------------------

and use that instead?


> diff --git a/run-command.c b/run-command.c
> index f5e1149f9b3..9fcc12ebf9c 100644
> --- a/run-command.c
> +++ b/run-command.c
> @@ -421,12 +421,12 @@ static int prepare_cmd(struct argv_array *out, const struct child_process *cmd)
>  	}
>  
>  	/*
> -	 * If there are no '/' characters in the command then perform a path
> -	 * lookup and use the resolved path as the command to exec.  If there
> -	 * are '/' characters, we have exec attempt to invoke the command
> -	 * directly.
> +	 * If there are no dir separator characters in the command then perform
> +	 * a path lookup and use the resolved path as the command to exec. If
> +	 * there are dir separator characters, we have exec attempt to invoke
> +	 * the command directly.
>  	 */
> -	if (!strchr(out->argv[1], '/')) {
> +	if (find_last_dir_sep(out->argv[1]) == NULL) {
>  		char *program = locate_in_PATH(out->argv[1]);
>  		if (program) {
>  			free((char *)out->argv[1]);
>
> base-commit: 274b9cc25322d9ee79aa8e6d4e86f0ffe5ced925

^ permalink raw reply	[flat|nested] 13+ messages in thread

* [PATCH v2] Fix dir sep handling of GIT_ASKPASS on Windows
  2020-03-23 21:13 [PATCH] Fix dir sep handling of GIT_ASKPASS on Windows András Kucsma via GitGitGadget
  2020-03-24 20:51 ` Junio C Hamano
@ 2020-03-25 13:45 ` András Kucsma via GitGitGadget
  2020-03-25 16:35   ` Torsten Bögershausen
                     ` (2 more replies)
  1 sibling, 3 replies; 13+ messages in thread
From: András Kucsma via GitGitGadget @ 2020-03-25 13:45 UTC (permalink / raw)
  To: git
  Cc: Torsten Bögershausen, Junio C Hamano, András Kucsma,
	Andras Kucsma

From: Andras Kucsma <r0maikx02b@gmail.com>

On Windows with git installed through cygwin, GIT_ASKPASS failed to run
for relative and absolute paths containing only backslashes as directory
separators.

The reason was that git assumed that if there are no forward slashes in
the executable path, it has to search for the executable on the PATH.

The fix is to look for OS specific directory separators, not just
forward slashes.

Signed-off-by: Andras Kucsma <r0maikx02b@gmail.com>
---
    Fix dir sep handling of GIT_ASKPASS on Windows
    
    On Windows with git installed through cygwin, GIT_ASKPASS failed to run
    for relative and absolute paths containing only backslashes as directory
    separators.
    
    The reason was that git assumed that if there are no forward slashes in
    the executable path, it has to search for the executable on the PATH.
    
    The fix is to look for OS specific directory separators, not just
    forward slashes.
    
    Signed-off-by: Andras Kucsma r0maikx02b@gmail.com [r0maikx02b@gmail.com]
    
    Changes since v1:
    
     * Avoid scanning the whole path for a directory separator even if one
       is found earlier as suggested by Junio C Hamano.

Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-587%2Fr0mai%2Ffix-prepare_cmd-windows-v2
Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-587/r0mai/fix-prepare_cmd-windows-v2
Pull-Request: https://github.com/gitgitgadget/git/pull/587

Range-diff vs v1:

 1:  8fbfbec0d38 ! 1:  947931ac568 Fix dir sep handling of GIT_ASKPASS on Windows
     @@ -14,6 +14,47 @@
      
          Signed-off-by: Andras Kucsma <r0maikx02b@gmail.com>
      
     + diff --git a/compat/win32/path-utils.h b/compat/win32/path-utils.h
     + --- a/compat/win32/path-utils.h
     + +++ b/compat/win32/path-utils.h
     +@@
     + 	return ret;
     + }
     + #define find_last_dir_sep win32_find_last_dir_sep
     ++static inline int win32_has_dir_sep(const char *path)
     ++{
     ++	/*
     ++	 * See how long the non-separator part of the given path is, and
     ++	 * if and only if it covers the whole path (i.e. path[len] is NULL),
     ++	 * there is no separator in the path---otherwise there is a separator.
     ++	 */
     ++	size_t len = strcspn(path, "/\\");
     ++	return !!path[len];
     ++}
     ++#define has_dir_sep(path) win32_has_dir_sep(path)
     + int win32_offset_1st_component(const char *path);
     + #define offset_1st_component win32_offset_1st_component
     + 
     +
     + diff --git a/git-compat-util.h b/git-compat-util.h
     + --- a/git-compat-util.h
     + +++ b/git-compat-util.h
     +@@
     + #define find_last_dir_sep git_find_last_dir_sep
     + #endif
     + 
     ++#ifndef has_dir_sep
     ++static inline int git_has_dir_sep(const char *path)
     ++{
     ++	return !!strchr(path, '/');
     ++}
     ++#define has_dir_sep(path) git_has_dir_sep(path)
     ++#endif
     ++
     + #ifndef query_user_email
     + #define query_user_email() NULL
     + #endif
     +
       diff --git a/run-command.c b/run-command.c
       --- a/run-command.c
       +++ b/run-command.c
     @@ -31,7 +72,7 @@
      +	 * the command directly.
       	 */
      -	if (!strchr(out->argv[1], '/')) {
     -+	if (find_last_dir_sep(out->argv[1]) == NULL) {
     ++	if (!has_dir_sep(out->argv[1])) {
       		char *program = locate_in_PATH(out->argv[1]);
       		if (program) {
       			free((char *)out->argv[1]);


 compat/win32/path-utils.h | 11 +++++++++++
 git-compat-util.h         |  8 ++++++++
 run-command.c             | 10 +++++-----
 3 files changed, 24 insertions(+), 5 deletions(-)

diff --git a/compat/win32/path-utils.h b/compat/win32/path-utils.h
index f2e70872cd2..18eff7899e9 100644
--- a/compat/win32/path-utils.h
+++ b/compat/win32/path-utils.h
@@ -20,6 +20,17 @@ static inline char *win32_find_last_dir_sep(const char *path)
 	return ret;
 }
 #define find_last_dir_sep win32_find_last_dir_sep
+static inline int win32_has_dir_sep(const char *path)
+{
+	/*
+	 * See how long the non-separator part of the given path is, and
+	 * if and only if it covers the whole path (i.e. path[len] is NULL),
+	 * there is no separator in the path---otherwise there is a separator.
+	 */
+	size_t len = strcspn(path, "/\\");
+	return !!path[len];
+}
+#define has_dir_sep(path) win32_has_dir_sep(path)
 int win32_offset_1st_component(const char *path);
 #define offset_1st_component win32_offset_1st_component
 
diff --git a/git-compat-util.h b/git-compat-util.h
index aed0b5d4f90..8ba576e81e3 100644
--- a/git-compat-util.h
+++ b/git-compat-util.h
@@ -389,6 +389,14 @@ static inline char *git_find_last_dir_sep(const char *path)
 #define find_last_dir_sep git_find_last_dir_sep
 #endif
 
+#ifndef has_dir_sep
+static inline int git_has_dir_sep(const char *path)
+{
+	return !!strchr(path, '/');
+}
+#define has_dir_sep(path) git_has_dir_sep(path)
+#endif
+
 #ifndef query_user_email
 #define query_user_email() NULL
 #endif
diff --git a/run-command.c b/run-command.c
index f5e1149f9b3..0f41af3b550 100644
--- a/run-command.c
+++ b/run-command.c
@@ -421,12 +421,12 @@ static int prepare_cmd(struct argv_array *out, const struct child_process *cmd)
 	}
 
 	/*
-	 * If there are no '/' characters in the command then perform a path
-	 * lookup and use the resolved path as the command to exec.  If there
-	 * are '/' characters, we have exec attempt to invoke the command
-	 * directly.
+	 * If there are no dir separator characters in the command then perform
+	 * a path lookup and use the resolved path as the command to exec. If
+	 * there are dir separator characters, we have exec attempt to invoke
+	 * the command directly.
 	 */
-	if (!strchr(out->argv[1], '/')) {
+	if (!has_dir_sep(out->argv[1])) {
 		char *program = locate_in_PATH(out->argv[1]);
 		if (program) {
 			free((char *)out->argv[1]);

base-commit: 274b9cc25322d9ee79aa8e6d4e86f0ffe5ced925
-- 
gitgitgadget

^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: [PATCH v2] Fix dir sep handling of GIT_ASKPASS on Windows
  2020-03-25 13:45 ` [PATCH v2] " András Kucsma via GitGitGadget
@ 2020-03-25 16:35   ` Torsten Bögershausen
  2020-03-25 17:09     ` András Kucsma
  2020-03-26 21:16     ` Junio C Hamano
  2020-03-26 21:14   ` Junio C Hamano
  2020-03-27  0:36   ` [PATCH v3] run-command: trigger PATH lookup properly on Cygwin András Kucsma via GitGitGadget
  2 siblings, 2 replies; 13+ messages in thread
From: Torsten Bögershausen @ 2020-03-25 16:35 UTC (permalink / raw)
  To: András Kucsma via GitGitGadget
  Cc: git, Junio C Hamano, András Kucsma

Thanks for working on this. I have 1 or 2 nits/questions, please see below.

On Wed, Mar 25, 2020 at 01:45:10PM +0000, András Kucsma via GitGitGadget wrote:
> From: Andras Kucsma <r0maikx02b@gmail.com>
>
> On Windows with git installed through cygwin, GIT_ASKPASS failed to run

My understanding is, that git under cygwin needs this patch (so to say),
but isn't it so, that even Git for Windows has the same issue ?
The headline of the patch and the indicate so.
How about the following ?

On Windows GIT_ASKPASS failed to run for relative and absolute paths
containing only backslashes as directory separators.
The reason was that Git assumed that if there are no forward slashes in
the executable path, it has to search for the executable on the PATH.

The fix is to look for OS specific directory separators, not just
forward slashes, so introduce a helper function has_dir_sep() and use it
in run-command.


^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: [PATCH v2] Fix dir sep handling of GIT_ASKPASS on Windows
  2020-03-25 16:35   ` Torsten Bögershausen
@ 2020-03-25 17:09     ` András Kucsma
  2020-03-26 21:16     ` Junio C Hamano
  1 sibling, 0 replies; 13+ messages in thread
From: András Kucsma @ 2020-03-25 17:09 UTC (permalink / raw)
  To: Torsten Bögershausen
  Cc: András Kucsma via GitGitGadget, git, Junio C Hamano

On Wed, Mar 25, 2020 at 5:35 PM Torsten Bögershausen <tboegi@web.de> wrote:
>
> Thanks for working on this. I have 1 or 2 nits/questions, please see below.
>
> On Wed, Mar 25, 2020 at 01:45:10PM +0000, András Kucsma via GitGitGadget wrote:
> > From: Andras Kucsma <r0maikx02b@gmail.com>
> >
> > On Windows with git installed through cygwin, GIT_ASKPASS failed to run
>
> My understanding is, that git under cygwin needs this patch (so to say),
> but isn't it so, that even Git for Windows has the same issue ?
> The headline of the patch and the indicate so.

Git for Windows does not have this issue, because there
GIT_WINDOWS_NATIVE is defined, which is not true under Cygwin:
https://github.com/git/git/blob/274b9cc2/git-compat-util.h#L157-L165

You can see in start_command() that there are separate implementations
based on GIT_WINDOWS_NATIVE. The problematic code is in prepare_cmd,
which is not called in the branch where GIT_WINDOWS_NATIVE is defined:
https://github.com/git/git/blob/274b9cc2/run-command.c#L740

This means, that cygwin is running in the Unix-like branch of the
code, even though it supports backslashes in its paths.

^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: [PATCH v2] Fix dir sep handling of GIT_ASKPASS on Windows
  2020-03-25 13:45 ` [PATCH v2] " András Kucsma via GitGitGadget
  2020-03-25 16:35   ` Torsten Bögershausen
@ 2020-03-26 21:14   ` Junio C Hamano
  2020-03-27  0:21     ` András Kucsma
  2020-03-27  0:36   ` [PATCH v3] run-command: trigger PATH lookup properly on Cygwin András Kucsma via GitGitGadget
  2 siblings, 1 reply; 13+ messages in thread
From: Junio C Hamano @ 2020-03-26 21:14 UTC (permalink / raw)
  To: András Kucsma via GitGitGadget
  Cc: git, Torsten Bögershausen, András Kucsma

"András Kucsma via GitGitGadget"  <gitgitgadget@gmail.com> writes:

> From: Andras Kucsma <r0maikx02b@gmail.com>
>
> On Windows with git installed through cygwin, GIT_ASKPASS failed to run
> for relative and absolute paths containing only backslashes as directory
> separators.

> Subject: [PATCH v2] Fix dir sep handling of GIT_ASKPASS on Windows

Isn't it curious that there is nothing in the code that was touched
that is specific to GIT_ASKPASS?  We shouldn't have to see that in
the title.

Perhaps

    Subject: run-command: notice needs for PATH-lookup correctly on Cygwin

    On Cygwin, the codepath for POSIX-like systems is taken in
    run-command.c::start_command().  The prepare_cmd() helper
    function is called to decide if the command needs to be looked
    up in the $PATH, and the logic there is to do the PATH-lookup if
    and only if it does not have any slash '/' in it.

    Unfortunately, a end-user can give "c:\program files\askpass" or
    "a\b\c" to be absolute or relative path to the command, but in
    these strings there is no '/'.  We end up attempting to run the
    command by appending the absoluter or relative path after each
    colon-separated component of $PATH.

    Instead, introduce a has_dir_sep(path) helper function to
    abstract away the difference between true POSIX and Cygwin, and
    use it to make the decision for PATH-lookup.

Having said all that, I am not sure if we need to change anything.

As Cygwin is about trying to mimicking UNIXy environment as much as
possible, shouldn't "GIT_ASKPASS=//c/program files/askpass" the way
end-users would expect to work, not the one that uses backslashes?

And if the user pretends to be on UNIXy system by using Cygwin by
using slashes when specifying these commands run via the run_command
API, the code makes the decision for PATH-lookup quite correctly,
no?

So...

^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: [PATCH v2] Fix dir sep handling of GIT_ASKPASS on Windows
  2020-03-25 16:35   ` Torsten Bögershausen
  2020-03-25 17:09     ` András Kucsma
@ 2020-03-26 21:16     ` Junio C Hamano
  1 sibling, 0 replies; 13+ messages in thread
From: Junio C Hamano @ 2020-03-26 21:16 UTC (permalink / raw)
  To: Torsten Bögershausen
  Cc: András Kucsma via GitGitGadget, git, András Kucsma

Torsten Bögershausen <tboegi@web.de> writes:

>> On Windows with git installed through cygwin, GIT_ASKPASS failed to run
>
> My understanding is, that git under cygwin needs this patch (so to say),
> but isn't it so, that even Git for Windows has the same issue ?
> The headline of the patch and the indicate so.

Yes, I agree that the commit is mistitled.  It is not specific to
Windows (it only is Cygwin, as the support for native Windows goes a
separate codepath), and it is not specific to GIT_ASKPASS, either.

^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: [PATCH v2] Fix dir sep handling of GIT_ASKPASS on Windows
  2020-03-26 21:14   ` Junio C Hamano
@ 2020-03-27  0:21     ` András Kucsma
  0 siblings, 0 replies; 13+ messages in thread
From: András Kucsma @ 2020-03-27  0:21 UTC (permalink / raw)
  To: Junio C Hamano
  Cc: András Kucsma via GitGitGadget, git, Torsten Bögershausen

On Thu, Mar 26, 2020 at 10:14 PM Junio C Hamano <gitster@pobox.com> wrote:
>
> "András Kucsma via GitGitGadget"  <gitgitgadget@gmail.com> writes:
>
> > From: Andras Kucsma <r0maikx02b@gmail.com>
> >
> > On Windows with git installed through cygwin, GIT_ASKPASS failed to run
> > for relative and absolute paths containing only backslashes as directory
> > separators.
>
> > Subject: [PATCH v2] Fix dir sep handling of GIT_ASKPASS on Windows
>
> Isn't it curious that there is nothing in the code that was touched
> that is specific to GIT_ASKPASS?  We shouldn't have to see that in
> the title.

You're completely right, I'll rephrase the commit message based on
your suggestion and resubmit.

> Having said all that, I am not sure if we need to change anything.
>
> As Cygwin is about trying to mimicking UNIXy environment as much as
> possible, shouldn't "GIT_ASKPASS=//c/program files/askpass" the way
> end-users would expect to work, not the one that uses backslashes?
>
> And if the user pretends to be on UNIXy system by using Cygwin by
> using slashes when specifying these commands run via the run_command
> API, the code makes the decision for PATH-lookup quite correctly,
> no?
>
> So...

Cygwin provides a Unix like environment, while also maintaining
Windows compatibility, at least as far as path handling is concerned.
As a quick test, fopen can handle forward slashes, backslashes too.
These four all work under cygwin:

fopen("C:\\file.txt", "r");
fopen("C:/file.txt", "r");
fopen("/cygdrive/c/file.txt", "r");
fopen("/cygdrive\\c\\file.txt", "r");

There seems to be a precedent to support Cygwin as a kind of "hybrid"
platform in the git codebase. In git-compat-util.h, the
compat/win32/path-utils.h header is included, but GIT_WINDOWS_NATIVE
is not defined.

https://github.com/git/git/blob/a7d14a442/git-compat-util.h#L204-L206
https://github.com/git/git/blob/a7d14a442/git-compat-util.h#L157-L165

The compat/win32/path-utils.h header mostly provides utilities dealing
with directory separator related logic on Windows, but these utilities
are being used in the Unixy code paths on Cygwin.

The current version of the patch fits into this pattern. It only
changes behaviour under Cygwin, not touching pure Windows and
non-Cygwin Unix variants at all.

^ permalink raw reply	[flat|nested] 13+ messages in thread

* [PATCH v3] run-command: trigger PATH lookup properly on Cygwin
  2020-03-25 13:45 ` [PATCH v2] " András Kucsma via GitGitGadget
  2020-03-25 16:35   ` Torsten Bögershausen
  2020-03-26 21:14   ` Junio C Hamano
@ 2020-03-27  0:36   ` András Kucsma via GitGitGadget
  2020-03-27 18:04     ` Junio C Hamano
  2 siblings, 1 reply; 13+ messages in thread
From: András Kucsma via GitGitGadget @ 2020-03-27  0:36 UTC (permalink / raw)
  To: git
  Cc: Torsten Bögershausen, Junio C Hamano, András Kucsma,
	Andras Kucsma

From: Andras Kucsma <r0maikx02b@gmail.com>

On Cygwin, the codepath for POSIX-like systems is taken in
run-command.c::start_command(). The prepare_cmd() helper
function is called to decide if the command needs to be looked
up in the PATH. The logic there is to do the PATH-lookup if
and only if it does not have any slash '/' in it. If this test
passes we end up attempting to run the command by appending the
string after each colon-separated component of PATH.

The Cygwin environment supports both Windows and POSIX style
paths, so both forwardslahes '/' and back slashes '\' can be
used as directory separators for any external program the user
supplies.

Examples for path strings which are being incorrectly searched
for in the PATH instead of being executed as is:

- "C:\Program Files\some-program.exe"
- "a\b\c.exe"

To handle these, the PATH lookup detection logic in prepare_cmd()
is taught to know about this Cygwin quirk, by introducing
has_dir_sep(path) helper function to abstract away the difference
between true POSIX and Cygwin systems.

Signed-off-by: Andras Kucsma <r0maikx02b@gmail.com>
---
    run-command: trigger PATH lookup properly on Cygwin
    
    On Cygwin, the codepath for POSIX-like systems is taken in
    run-command.c::start_command(). The prepare_cmd() helper function is
    called to decide if the command needs to be looked up in the PATH. The
    logic there is to do the PATH-lookup if and only if it does not have any
    slash '/' in it. If this test passes we end up attempting to run the
    command by appending the string after each colon-separated component of
    PATH.
    
    The Cygwin environment supports both Windows and POSIX style paths, so
    both forwardslahes '/' and back slashes '' can be used as directory
    separators for any external program the user supplies.
    
    Examples for path strings which are being incorrectly searched for in
    the PATH instead of being executed as is:
    
     * "C:\Program Files\some-program.exe"
     * "a\b\c.exe"
    
    To handle these, the PATH lookup detection logic in prepare_cmd() is
    taught to know about this Cygwin quirk, by introducing has_dir_sep(path)
    helper function to abstract away the difference between true POSIX and
    Cygwin systems.
    
    Signed-off-by: Andras Kucsma r0maikx02b@gmail.com [r0maikx02b@gmail.com]
    
    Changes since v1:
    
     * Avoid scanning the whole path for a directory separator even if one
       is found earlier as suggested by Junio C Hamano. Changes since v2:
     * Rephrased the commit message based on Junio's suggestion.

Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-587%2Fr0mai%2Ffix-prepare_cmd-windows-v3
Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-587/r0mai/fix-prepare_cmd-windows-v3
Pull-Request: https://github.com/gitgitgadget/git/pull/587

Range-diff vs v2:

 1:  947931ac568 ! 1:  fd3cbd51635 Fix dir sep handling of GIT_ASKPASS on Windows
     @@ -1,16 +1,30 @@
      Author: Andras Kucsma <r0maikx02b@gmail.com>
      
     -    Fix dir sep handling of GIT_ASKPASS on Windows
     +    run-command: trigger PATH lookup properly on Cygwin
      
     -    On Windows with git installed through cygwin, GIT_ASKPASS failed to run
     -    for relative and absolute paths containing only backslashes as directory
     -    separators.
     +    On Cygwin, the codepath for POSIX-like systems is taken in
     +    run-command.c::start_command(). The prepare_cmd() helper
     +    function is called to decide if the command needs to be looked
     +    up in the PATH. The logic there is to do the PATH-lookup if
     +    and only if it does not have any slash '/' in it. If this test
     +    passes we end up attempting to run the command by appending the
     +    string after each colon-separated component of PATH.
      
     -    The reason was that git assumed that if there are no forward slashes in
     -    the executable path, it has to search for the executable on the PATH.
     +    The Cygwin environment supports both Windows and POSIX style
     +    paths, so both forwardslahes '/' and back slashes '\' can be
     +    used as directory separators for any external program the user
     +    supplies.
      
     -    The fix is to look for OS specific directory separators, not just
     -    forward slashes.
     +    Examples for path strings which are being incorrectly searched
     +    for in the PATH instead of being executed as is:
     +
     +    - "C:\Program Files\some-program.exe"
     +    - "a\b\c.exe"
     +
     +    To handle these, the PATH lookup detection logic in prepare_cmd()
     +    is taught to know about this Cygwin quirk, by introducing
     +    has_dir_sep(path) helper function to abstract away the difference
     +    between true POSIX and Cygwin systems.
      
          Signed-off-by: Andras Kucsma <r0maikx02b@gmail.com>
      


 compat/win32/path-utils.h | 11 +++++++++++
 git-compat-util.h         |  8 ++++++++
 run-command.c             | 10 +++++-----
 3 files changed, 24 insertions(+), 5 deletions(-)

diff --git a/compat/win32/path-utils.h b/compat/win32/path-utils.h
index f2e70872cd2..18eff7899e9 100644
--- a/compat/win32/path-utils.h
+++ b/compat/win32/path-utils.h
@@ -20,6 +20,17 @@ static inline char *win32_find_last_dir_sep(const char *path)
 	return ret;
 }
 #define find_last_dir_sep win32_find_last_dir_sep
+static inline int win32_has_dir_sep(const char *path)
+{
+	/*
+	 * See how long the non-separator part of the given path is, and
+	 * if and only if it covers the whole path (i.e. path[len] is NULL),
+	 * there is no separator in the path---otherwise there is a separator.
+	 */
+	size_t len = strcspn(path, "/\\");
+	return !!path[len];
+}
+#define has_dir_sep(path) win32_has_dir_sep(path)
 int win32_offset_1st_component(const char *path);
 #define offset_1st_component win32_offset_1st_component
 
diff --git a/git-compat-util.h b/git-compat-util.h
index aed0b5d4f90..8ba576e81e3 100644
--- a/git-compat-util.h
+++ b/git-compat-util.h
@@ -389,6 +389,14 @@ static inline char *git_find_last_dir_sep(const char *path)
 #define find_last_dir_sep git_find_last_dir_sep
 #endif
 
+#ifndef has_dir_sep
+static inline int git_has_dir_sep(const char *path)
+{
+	return !!strchr(path, '/');
+}
+#define has_dir_sep(path) git_has_dir_sep(path)
+#endif
+
 #ifndef query_user_email
 #define query_user_email() NULL
 #endif
diff --git a/run-command.c b/run-command.c
index f5e1149f9b3..0f41af3b550 100644
--- a/run-command.c
+++ b/run-command.c
@@ -421,12 +421,12 @@ static int prepare_cmd(struct argv_array *out, const struct child_process *cmd)
 	}
 
 	/*
-	 * If there are no '/' characters in the command then perform a path
-	 * lookup and use the resolved path as the command to exec.  If there
-	 * are '/' characters, we have exec attempt to invoke the command
-	 * directly.
+	 * If there are no dir separator characters in the command then perform
+	 * a path lookup and use the resolved path as the command to exec. If
+	 * there are dir separator characters, we have exec attempt to invoke
+	 * the command directly.
 	 */
-	if (!strchr(out->argv[1], '/')) {
+	if (!has_dir_sep(out->argv[1])) {
 		char *program = locate_in_PATH(out->argv[1]);
 		if (program) {
 			free((char *)out->argv[1]);

base-commit: 274b9cc25322d9ee79aa8e6d4e86f0ffe5ced925
-- 
gitgitgadget

^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: [PATCH v3] run-command: trigger PATH lookup properly on Cygwin
  2020-03-27  0:36   ` [PATCH v3] run-command: trigger PATH lookup properly on Cygwin András Kucsma via GitGitGadget
@ 2020-03-27 18:04     ` Junio C Hamano
  2020-03-27 18:10       ` András Kucsma
  2020-03-27 18:41       ` Andreas Schwab
  0 siblings, 2 replies; 13+ messages in thread
From: Junio C Hamano @ 2020-03-27 18:04 UTC (permalink / raw)
  To: András Kucsma via GitGitGadget
  Cc: git, Torsten Bögershausen, András Kucsma

"András Kucsma via GitGitGadget"  <gitgitgadget@gmail.com> writes:

> Subject: Re: [PATCH v3] run-command: trigger PATH lookup properly on Cygwin

You phrased it much better than my earlier attempt.  Succinct,
accurate and to the point.  Good.

>  compat/win32/path-utils.h | 11 +++++++++++
>  git-compat-util.h         |  8 ++++++++
>  run-command.c             | 10 +++++-----
>  3 files changed, 24 insertions(+), 5 deletions(-)
>
> diff --git a/compat/win32/path-utils.h b/compat/win32/path-utils.h
> index f2e70872cd2..18eff7899e9 100644
> --- a/compat/win32/path-utils.h
> +++ b/compat/win32/path-utils.h
> @@ -20,6 +20,17 @@ static inline char *win32_find_last_dir_sep(const char *path)
>  	return ret;
>  }
>  #define find_last_dir_sep win32_find_last_dir_sep
> +static inline int win32_has_dir_sep(const char *path)
> +{
> +	/*
> +	 * See how long the non-separator part of the given path is, and
> +	 * if and only if it covers the whole path (i.e. path[len] is NULL),

The name of the ASCII character '\0' is NUL, not NULL (I'll fix it
while applying, so no need to resend if you do not have anything
else that needs updating).

Otherwise, the patch looks good. 

Thanks.

^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: [PATCH v3] run-command: trigger PATH lookup properly on Cygwin
  2020-03-27 18:04     ` Junio C Hamano
@ 2020-03-27 18:10       ` András Kucsma
  2020-03-27 18:41       ` Andreas Schwab
  1 sibling, 0 replies; 13+ messages in thread
From: András Kucsma @ 2020-03-27 18:10 UTC (permalink / raw)
  To: Junio C Hamano
  Cc: András Kucsma via GitGitGadget, git, Torsten Bögershausen

On Fri, Mar 27, 2020 at 7:04 PM Junio C Hamano <gitster@pobox.com> wrote:
> The name of the ASCII character '\0' is NUL, not NULL (I'll fix it
> while applying, so no need to resend if you do not have anything
> else that needs updating).

Right, sorry! I have no other updates.

> Otherwise, the patch looks good.
>
> Thanks.

Thanks for the help!

^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: [PATCH v3] run-command: trigger PATH lookup properly on Cygwin
  2020-03-27 18:04     ` Junio C Hamano
  2020-03-27 18:10       ` András Kucsma
@ 2020-03-27 18:41       ` Andreas Schwab
  2020-03-27 21:27         ` Junio C Hamano
  1 sibling, 1 reply; 13+ messages in thread
From: Andreas Schwab @ 2020-03-27 18:41 UTC (permalink / raw)
  To: Junio C Hamano
  Cc: András Kucsma via GitGitGadget, git,
	Torsten Bögershausen, András Kucsma

On Mär 27 2020, Junio C Hamano wrote:

> The name of the ASCII character '\0' is NUL, not NULL

NUL is not a name, it is an abbreviation or acronym.  Its name is the
Null character.

Andreas.

-- 
Andreas Schwab, schwab@linux-m68k.org
GPG Key fingerprint = 7578 EB47 D4E5 4D69 2510  2552 DF73 E780 A9DA AEC1
"And now for something completely different."

^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: [PATCH v3] run-command: trigger PATH lookup properly on Cygwin
  2020-03-27 18:41       ` Andreas Schwab
@ 2020-03-27 21:27         ` Junio C Hamano
  0 siblings, 0 replies; 13+ messages in thread
From: Junio C Hamano @ 2020-03-27 21:27 UTC (permalink / raw)
  To: Andreas Schwab
  Cc: András Kucsma via GitGitGadget, git,
	Torsten Bögershausen, András Kucsma

Andreas Schwab <schwab@linux-m68k.org> writes:

> On Mär 27 2020, Junio C Hamano wrote:
>
>> The name of the ASCII character '\0' is NUL, not NULL
>
> NUL is not a name, it is an abbreviation or acronym.  Its name is the
> Null character.

OK, let's put it differently.

> +	 * See how long the non-separator part of the given path is, and
> +	 * if and only if it covers the whole path (i.e. path[len] is NULL),

When referring to character '\0' like so, write "NUL", not "NULL",
as the latter is how you write a null pointer.

^ permalink raw reply	[flat|nested] 13+ messages in thread

end of thread, back to index

Thread overview: 13+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-03-23 21:13 [PATCH] Fix dir sep handling of GIT_ASKPASS on Windows András Kucsma via GitGitGadget
2020-03-24 20:51 ` Junio C Hamano
2020-03-25 13:45 ` [PATCH v2] " András Kucsma via GitGitGadget
2020-03-25 16:35   ` Torsten Bögershausen
2020-03-25 17:09     ` András Kucsma
2020-03-26 21:16     ` Junio C Hamano
2020-03-26 21:14   ` Junio C Hamano
2020-03-27  0:21     ` András Kucsma
2020-03-27  0:36   ` [PATCH v3] run-command: trigger PATH lookup properly on Cygwin András Kucsma via GitGitGadget
2020-03-27 18:04     ` Junio C Hamano
2020-03-27 18:10       ` András Kucsma
2020-03-27 18:41       ` Andreas Schwab
2020-03-27 21:27         ` Junio C Hamano

Git Mailing List Archive on lore.kernel.org

Archives are clonable:
	git clone --mirror https://lore.kernel.org/git/0 git/git/0.git

	# If you have public-inbox 1.1+ installed, you may
	# initialize and index your mirror using the following commands:
	public-inbox-init -V2 git git/ https://lore.kernel.org/git \
		git@vger.kernel.org
	public-inbox-index git

Example config snippet for mirrors

Newsgroup available over NNTP:
	nntp://nntp.lore.kernel.org/org.kernel.vger.git


AGPL code for this site: git clone https://public-inbox.org/public-inbox.git