Skip to content

Commit b8e9daa

Browse files
committed
Refactor git-wrapper into more functions
This prepares the wrapper for modifications to serve as a drop-in replacement for the builtins. This commit's diff is best viewed with the `-w` flag. Signed-off-by: Johannes Schindelin <[email protected]>
1 parent aa08645 commit b8e9daa

File tree

1 file changed

+68
-60
lines changed

1 file changed

+68
-60
lines changed

compat/win32/git-wrapper.c

Lines changed: 68 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -34,22 +34,10 @@ static void print_error(LPCWSTR prefix, DWORD error_number)
3434
LocalFree((HLOCAL)buffer);
3535
}
3636

37-
int main(void)
37+
static void setup_environment(LPWSTR exepath)
3838
{
39-
int r = 1, wait = 1;
40-
WCHAR exepath[MAX_PATH], exe[MAX_PATH];
41-
LPWSTR cmd = NULL, path2 = NULL, exep = exe;
42-
UINT codepage = 0;
4339
int len;
44-
45-
/* get the installation location */
46-
GetModuleFileName(NULL, exepath, MAX_PATH);
47-
PathRemoveFileSpec(exepath);
48-
PathRemoveFileSpec(exepath);
49-
50-
/* set the default exe module */
51-
wcscpy(exe, exepath);
52-
PathAppend(exe, L"bin\\git.exe");
40+
LPWSTR path2 = NULL;
5341

5442
/* if not set, set TERM to msys */
5543
if (!GetEnvironmentVariable(L"TERM", NULL, 0))
@@ -100,56 +88,76 @@ int main(void)
10088
SetEnvironmentVariable(L"PATH", path2);
10189
free(path2);
10290

91+
}
10392

104-
/* fix up the command line to call git.exe
105-
* We have to be very careful about quoting here so we just
106-
* trim off the first argument and replace it leaving the rest
107-
* untouched.
108-
*/
109-
{
110-
int wargc = 0, gui = 0;
111-
LPWSTR cmdline = NULL;
112-
LPWSTR *wargv = NULL, p = NULL;
113-
cmdline = GetCommandLine();
114-
wargv = CommandLineToArgvW(cmdline, &wargc);
115-
cmd = (LPWSTR)malloc(sizeof(WCHAR) *
116-
(wcslen(cmdline) + MAX_PATH));
117-
if (wargc > 1 && wcsicmp(L"gui", wargv[1]) == 0) {
118-
wait = 0;
119-
if (wargc > 2 && wcsicmp(L"citool", wargv[2]) == 0) {
120-
wait = 1;
121-
wcscpy(cmd, L"git.exe");
122-
}
123-
else {
124-
WCHAR script[MAX_PATH];
125-
gui = 1;
126-
wcscpy(script, exepath);
127-
PathAppend(script,
128-
L"libexec\\git-core\\git-gui");
129-
PathQuoteSpaces(script);
130-
wcscpy(cmd, L"wish.exe ");
131-
wcscat(cmd, script);
132-
wcscat(cmd, L" --");
133-
/* find the module from the commandline */
134-
exep = NULL;
135-
}
136-
}
137-
else
93+
/*
94+
* Fix up the command line to call git.exe
95+
* We have to be very careful about quoting here so we just
96+
* trim off the first argument and replace it leaving the rest
97+
* untouched.
98+
*/
99+
static LPWSTR fixup_commandline(LPWSTR exepath, LPWSTR *exep, int *wait)
100+
{
101+
int wargc = 0, gui = 0;
102+
LPWSTR cmd = NULL, cmdline = NULL;
103+
LPWSTR *wargv = NULL, p = NULL;
104+
105+
cmdline = GetCommandLine();
106+
wargv = CommandLineToArgvW(cmdline, &wargc);
107+
cmd = (LPWSTR)malloc(sizeof(WCHAR) *
108+
(wcslen(cmdline) + MAX_PATH));
109+
if (wargc > 1 && wcsicmp(L"gui", wargv[1]) == 0) {
110+
*wait = 0;
111+
if (wargc > 2 && wcsicmp(L"citool", wargv[2]) == 0) {
112+
*wait = 1;
138113
wcscpy(cmd, L"git.exe");
139-
140-
/* append all after first space after the initial parameter */
141-
p = wcschr(&cmdline[wcslen(wargv[0])], L' ');
142-
if (p && *p) {
143-
/* for git gui subcommands, remove the 'gui' word */
144-
if (gui) {
145-
while (*p == L' ') ++p;
146-
p = wcschr(p, L' ');
147-
}
148-
if (p && *p)
149-
wcscat(cmd, p);
150114
}
151-
LocalFree(wargv);
115+
else {
116+
WCHAR script[MAX_PATH];
117+
gui = 1;
118+
wcscpy(script, exepath);
119+
PathAppend(script,
120+
L"libexec\\git-core\\git-gui");
121+
PathQuoteSpaces(script);
122+
wcscpy(cmd, L"wish.exe ");
123+
wcscat(cmd, script);
124+
wcscat(cmd, L" --");
125+
/* find the module from the commandline */
126+
*exep = NULL;
127+
}
128+
}
129+
else
130+
wcscpy(cmd, L"git.exe");
131+
132+
/* append all after first space after the initial parameter */
133+
p = wcschr(&cmdline[wcslen(wargv[0])], L' ');
134+
if (p && *p) {
135+
/* for git gui subcommands, remove the 'gui' word */
136+
if (gui) {
137+
while (*p == L' ') ++p;
138+
p = wcschr(p, L' ');
139+
}
140+
if (p && *p)
141+
wcscat(cmd, p);
152142
}
143+
LocalFree(wargv);
144+
145+
return cmd;
146+
}
147+
148+
int main(void)
149+
{
150+
int r = 1, wait = 1;
151+
WCHAR exepath[MAX_PATH], exe[MAX_PATH];
152+
LPWSTR cmd = NULL, exep = exe, basename;
153+
UINT codepage = 0;
154+
155+
/* get the installation location */
156+
GetModuleFileName(NULL, exepath, MAX_PATH);
157+
PathRemoveFileSpec(exepath);
158+
PathRemoveFileSpec(exepath);
159+
setup_environment(exepath);
160+
cmd = fixup_commandline(exepath, &exep, &wait);
153161

154162
/* set the console to ANSI/GUI codepage */
155163
codepage = GetConsoleCP();

0 commit comments

Comments
 (0)