threads / patch / 53073

patchFix dir sep handling of GIT_ASKPASS on Windows

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

## tl;dr

13 messages between Mar 23, 2020 and Mar 27, 2020. Diffs are folded; open one to read it.

replies: 12people: 5as markdown or json

András Kucsma via GitGitGadget· Mar 23, 2020, 21:13 UTC · lore
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(-)
Show changes to run-command.c +5 −5
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
Junio C Hamano· Mar 24, 2020, 20:51 UTC · re: András Kucsma via GitGitGadget · lore

Re: [PATCH] Fix dir sep handling of GIT_ASKPASS on Windows

"András Kucsma via GitGitGadget"  <gitgitgadget@gmail.com> writes:
Show 8 quoted lines
> 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?
Show 24 quoted lines
> 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
András Kucsma via GitGitGadget· Mar 25, 2020, 13:45 UTC · re: András Kucsma via GitGitGadget · lore

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

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(-)
Show changes to 3 files +24 −5

compat/win32/path-utils.h, git-compat-util.h, run-command.c

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
Torsten Bögershausen· Mar 25, 2020, 16:35 UTC · re: András Kucsma via GitGitGadget · lore

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

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.

András Kucsma· Mar 25, 2020, 17:09 UTC · re: Torsten Bögershausen · lore

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

On Wed, Mar 25, 2020 at 5:35 PM Torsten Bögershausen <tboegi@web.de> wrote:
Show 11 quoted lines
>
> 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.

Junio C Hamano· Mar 26, 2020, 21:16 UTC · re: Torsten Bögershausen · lore

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

Torsten Bögershausen <tboegi@web.de> writes:
Show 5 quoted lines
>> 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.

Junio C Hamano· Mar 26, 2020, 21:14 UTC · re: András Kucsma via GitGitGadget · lore

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

"András Kucsma via GitGitGadget"  <gitgitgadget@gmail.com> writes:
Show 5 quoted lines
> 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...
András Kucsma· Mar 27, 2020, 00:21 UTC · re: Junio C Hamano · lore

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

On Thu, Mar 26, 2020 at 10:14 PM Junio C Hamano <gitster@pobox.com> wrote:
Show 14 quoted lines
>
> "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.

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

András Kucsma via GitGitGadget· Mar 27, 2020, 00:36 UTC · re: András Kucsma via GitGitGadget · lore

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

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(-)
Show changes to 3 files +24 −5

compat/win32/path-utils.h, git-compat-util.h, run-command.c

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
Junio C Hamano· Mar 27, 2020, 18:04 UTC · re: András Kucsma via GitGitGadget · lore

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

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

Show 18 quoted lines
>  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.
András Kucsma· Mar 27, 2020, 18:10 UTC · re: Junio C Hamano · lore

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

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!
Andreas Schwab· Mar 27, 2020, 18:41 UTC · re: Junio C Hamano · lore

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

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."
Junio C Hamano· Mar 27, 2020, 21:27 UTC · re: Andreas Schwab · lore

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

Andreas Schwab <schwab@linux-m68k.org> writes:
Show 6 quoted lines
> 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.

← back to recent threads