@matheusbsbs / matheus-bagliano / commits / 05ac8582bc
Fork 0

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>

Andras Kucsma committed Mar 27, 2020 at 00:36 UTC 05ac8582bc722c8bd3ab7a0cafa681bec198a06d
3 files changed +24 -5
compat/win32/path-utils.h
+11
@@ -20,6 +20,17 @@ static inline char *win32_find_last_dir_sep(const char *path)
20 return ret;
21 }
22 #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)
34 int win32_offset_1st_component(const char *path);
35 #define offset_1st_component win32_offset_1st_component
36
git-compat-util.h
+8
@@ -389,6 +389,14 @@ static inline char *git_find_last_dir_sep(const char *path)
389 #define find_last_dir_sep git_find_last_dir_sep
390 #endif
391
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 +
400 #ifndef query_user_email
401 #define query_user_email() NULL
402 #endif
run-command.c
+5 -5
@@ -421,12 +421,12 @@ static int prepare_cmd(struct argv_array *out, const struct child_process *cmd)
421 }
422
423 /*
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.
428 */
429 - if (!strchr(out->argv[1], '/')) {
429 + if (!has_dir_sep(out->argv[1])) {
430 char *program = locate_in_PATH(out->argv[1]);
431 if (program) {
432 free((char *)out->argv[1]);