Skip to content

Commit d7ff2b2

Browse files
committed
git-wrapper: prepare for executing configurable command-lines
We are about to use the Git wrapper to call the Git Bash of Git for Windows. All the wrapper needs to do for that is to set up the environment variables, use the home directory as working directory and then hand off to a user-specified command-line. We prepare the existing code for this change by introducing flags to set up the environment variables, to launch a non-Git program, and to use the home directory as working directory. Signed-off-by: Johannes Schindelin <[email protected]>
1 parent 9dbe264 commit d7ff2b2

File tree

1 file changed

+26
-9
lines changed

1 file changed

+26
-9
lines changed

compat/win32/git-wrapper.c

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ static void setup_environment(LPWSTR exepath)
116116
* untouched.
117117
*/
118118
static LPWSTR fixup_commandline(LPWSTR exepath, LPWSTR *exep, int *wait,
119-
LPWSTR prefix_args, int prefix_args_len)
119+
LPWSTR prefix_args, int prefix_args_len, int is_git_command)
120120
{
121121
int wargc = 0, gui = 0;
122122
LPWSTR cmd = NULL, cmdline = NULL;
@@ -146,9 +146,14 @@ static LPWSTR fixup_commandline(LPWSTR exepath, LPWSTR *exep, int *wait,
146146
*exep = NULL;
147147
}
148148
}
149-
else if (prefix_args)
150-
_swprintf(cmd, L"%s\\%s %.*s",
151-
exepath, L"git.exe", prefix_args_len, prefix_args);
149+
else if (prefix_args) {
150+
if (is_git_command)
151+
_swprintf(cmd, L"%s\\%s %.*s", exepath, L"git.exe",
152+
prefix_args_len, prefix_args);
153+
else
154+
_swprintf(cmd, L"%.*s", prefix_args_len, prefix_args);
155+
156+
}
152157
else
153158
wcscpy(cmd, L"git.exe");
154159

@@ -170,9 +175,10 @@ static LPWSTR fixup_commandline(LPWSTR exepath, LPWSTR *exep, int *wait,
170175

171176
int main(void)
172177
{
173-
int r = 1, wait = 1, prefix_args_len = -1;
178+
int r = 1, wait = 1, prefix_args_len = -1, needs_env_setup = 1,
179+
is_git_command = 1, start_in_home = 0;
174180
WCHAR exepath[MAX_PATH], exe[MAX_PATH];
175-
LPWSTR cmd = NULL, exep = exe, prefix_args = NULL, basename;
181+
LPWSTR cmd = NULL, dir = NULL, exep = exe, prefix_args = NULL, basename;
176182
UINT codepage = 0;
177183

178184
/* Determine MSys2-based Git path. */
@@ -187,6 +193,8 @@ int main(void)
187193
}
188194
basename = exepath + wcslen(exepath) + 1;
189195
if (!wcsncmp(basename, L"git-", 4)) {
196+
needs_env_setup = 0;
197+
190198
/* Call a builtin */
191199
prefix_args = basename + 4;
192200
prefix_args_len = wcslen(prefix_args);
@@ -214,10 +222,19 @@ int main(void)
214222
}
215223
}
216224

217-
if (!prefix_args)
225+
if (needs_env_setup)
218226
setup_environment(exepath);
219227
cmd = fixup_commandline(exepath, &exep, &wait,
220-
prefix_args, prefix_args_len);
228+
prefix_args, prefix_args_len, is_git_command);
229+
230+
if (start_in_home) {
231+
int len = GetEnvironmentVariable(L"HOME", NULL, 0);
232+
233+
if (len) {
234+
dir = malloc(sizeof(WCHAR) * len);
235+
GetEnvironmentVariable(L"HOME", dir, len);
236+
}
237+
}
221238

222239
/* set the console to ANSI/GUI codepage */
223240
codepage = GetConsoleCP();
@@ -238,7 +255,7 @@ int main(void)
238255
TRUE, /* handles inheritable? */
239256
CREATE_UNICODE_ENVIRONMENT,
240257
NULL, /* environment: use parent */
241-
NULL, /* starting directory: use parent */
258+
dir, /* starting directory: use parent */
242259
&si, &pi);
243260
if (br) {
244261
if (wait)

0 commit comments

Comments
 (0)