1 #include "../common/git-compat-util.h"
2 #include "../common/strbuf.h"
3 #include "../common/systeminfo.h"
7 static struct strbuf gitPath
= STRBUF_INIT
;
8 static TCHAR msysPath
[MAX_PATH
];
10 static int get_msys_path_from_registry(HKEY root
)
13 DWORD path_len
= sizeof(msysPath
);
15 LONG result
= RegOpenKeyEx(root
, GIT_CHEETAH_REG_PATH
,
16 0, KEY_QUERY_VALUE
, &key
);
17 if (ERROR_SUCCESS
!= result
)
20 result
= RegQueryValueEx(key
,
21 GIT_CHEETAH_REG_PATHTOMSYS
,
22 NULL
, NULL
, (LPBYTE
)msysPath
, &path_len
);
26 return ERROR_SUCCESS
== result
;
29 static TCHAR
*msys_path(void)
31 static int found_path
= 0;
33 /* try to find user-specific settings first */
35 found_path
= get_msys_path_from_registry(HKEY_CURRENT_USER
);
37 /* if not found in user settings, try machine-wide */
39 found_path
= get_msys_path_from_registry(HKEY_LOCAL_MACHINE
);
47 static inline int test_msys_path(const char *postfix
, char *path
)
50 char stat_path
[MAX_PATH
];
51 struct stat stat_info
;
53 n
= snprintf(stat_path
, MAX_PATH
, "%s\\%s\\git",
54 msys_path(), postfix
);
56 goto test_git_path_failed
;
58 n
= snprintf(path
, MAX_PATH
, "%s\\%s", msys_path(), postfix
);
60 goto test_git_path_failed
;
62 if (lstat(path
, &stat_info
) == 0) {
71 const char *git_path()
81 if (test_msys_path("bin", path
))
82 strbuf_addstr(&gitPath
, path
);
84 if (test_msys_path("mingw\\bin", path
)) {
86 strbuf_addch(&gitPath
, ';');
87 strbuf_addstr(&gitPath
, path
);
90 return gitPath
.len
? gitPath
.buf
: NULL
;
93 void message_box(const char *string
)
95 MessageBox(0, string
, "Hello", MB_OK
|MB_ICONEXCLAMATION
);
98 int is_directory(const char *path
)
100 return (FILE_ATTRIBUTE_DIRECTORY
& GetFileAttributes(path
));
104 * builds an array of environment variables,
105 * as expected by mingw_spawnvpe
107 static char **env_for_git()
109 static char **environment
;
112 * if we can't find path to msys in the registry, return NULL and
113 * the CreateProcess will copy the environment for us
115 if (!environment
&& git_path()) {
116 char *old
= GetEnvironmentStrings();
117 size_t space
= 0, path_index
= -1, name_len
= 0;
121 /* if it's PATH variable (could be Path= too!) */
122 if (!strnicmp(old
+ space
, "PATH=", 5)) {
129 space
++; /* skip var-terminating NULL */
134 if (path_index
== -1)
137 environment
= malloc(sizeof(*environment
) * (total
+ 1));
139 for (i
= 0; i
< total
; i
++) {
140 if (path_index
== space
) {
141 char *path
= old
+ space
+ name_len
;
143 environment
[i
] = malloc(strlen(path
) + 1 +
144 2 * strlen(git_path()) + 32);
145 len
= sprintf(environment
[i
],
146 "PATH=%s%s%s", git_path(),
147 *path
? ";" : "", path
);
149 environment
[i
] = strdup(old
+ space
);
153 space
++; /* skip var-terminating NULL */
156 /* mark the end of the array */
159 FreeEnvironmentStrings(old
);
165 pid_t
fork_process(const char *cmd
, const char **args
, const char *wd
)
167 return mingw_spawnvpe_cwd(cmd
, args
, env_for_git(), wd
);
170 int wait_for_process(pid_t pid
, int max_time
, int *errcode
)
175 if (WAIT_OBJECT_0
== WaitForSingleObject((HANDLE
)pid
,
177 if (GetExitCodeProcess((HANDLE
)pid
, &status
)) {
179 debug_git("Exit code: %d", status
);
181 /* play safe, and return total failure */
182 *errcode
= GetLastError();