Skip to content

Commit 05ac858

Browse files
r0maigitster
authored andcommitted
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> Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent 274b9cc commit 05ac858

File tree

3 files changed

+24
-5
lines changed

3 files changed

+24
-5
lines changed

compat/win32/path-utils.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,17 @@ static inline char *win32_find_last_dir_sep(const char *path)
2020
return ret;
2121
}
2222
#define find_last_dir_sep win32_find_last_dir_sep
23+
static inline int win32_has_dir_sep(const char *path)
24+
{
25+
/*
26+
* See how long the non-separator part of the given path is, and
27+
* if and only if it covers the whole path (i.e. path[len] is NUL),
28+
* there is no separator in the path---otherwise there is a separator.
29+
*/
30+
size_t len = strcspn(path, "/\\");
31+
return !!path[len];
32+
}
33+
#define has_dir_sep(path) win32_has_dir_sep(path)
2334
int win32_offset_1st_component(const char *path);
2435
#define offset_1st_component win32_offset_1st_component
2536

git-compat-util.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -389,6 +389,14 @@ static inline char *git_find_last_dir_sep(const char *path)
389389
#define find_last_dir_sep git_find_last_dir_sep
390390
#endif
391391

392+
#ifndef has_dir_sep
393+
static inline int git_has_dir_sep(const char *path)
394+
{
395+
return !!strchr(path, '/');
396+
}
397+
#define has_dir_sep(path) git_has_dir_sep(path)
398+
#endif
399+
392400
#ifndef query_user_email
393401
#define query_user_email() NULL
394402
#endif

run-command.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -421,12 +421,12 @@ static int prepare_cmd(struct argv_array *out, const struct child_process *cmd)
421421
}
422422

423423
/*
424-
* If there are no '/' characters in the command then perform a path
425-
* lookup and use the resolved path as the command to exec. If there
426-
* are '/' characters, we have exec attempt to invoke the command
427-
* directly.
424+
* If there are no dir separator characters in the command then perform
425+
* a path lookup and use the resolved path as the command to exec. If
426+
* there are dir separator characters, we have exec attempt to invoke
427+
* the command directly.
428428
*/
429-
if (!strchr(out->argv[1], '/')) {
429+
if (!has_dir_sep(out->argv[1])) {
430430
char *program = locate_in_PATH(out->argv[1]);
431431
if (program) {
432432
free((char *)out->argv[1]);

0 commit comments

Comments
 (0)