Skip to content

Commit 1272359

Browse files
kbleesdscho
authored andcommitted
git-wrapper: fix HOME initialization
git-wrapper fails to initialize HOME correctly if $HOMEDRIVE$HOMEPATH points to a disconnected network drive. Check if the directory exists before using $HOMEDRIVE$HOMEPATH. Signed-off-by: Karsten Blees <[email protected]>
1 parent 01ecc42 commit 1272359

File tree

1 file changed

+17
-10
lines changed

1 file changed

+17
-10
lines changed

compat/win32/git-wrapper.c

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -56,13 +56,29 @@ static void setup_environment(LPWSTR exepath, int full_path)
5656
if (!GetEnvironmentVariable(L"PLINK_PROTOCOL", NULL, 0))
5757
SetEnvironmentVariable(L"PLINK_PROTOCOL", L"ssh");
5858

59-
/* set HOME to %HOMEDRIVE%%HOMEPATH% or %USERPROFILE%
59+
/*
60+
* set HOME to %HOMEDRIVE%%HOMEPATH% or %USERPROFILE%
6061
* With roaming profiles: HOMEPATH is the roaming location and
6162
* USERPROFILE is the local location
6263
*/
6364
if (!GetEnvironmentVariable(L"HOME", NULL, 0)) {
6465
LPWSTR e = NULL;
6566
len = GetEnvironmentVariable(L"HOMEPATH", NULL, 0);
67+
if (len) {
68+
DWORD attr, drvlen = GetEnvironmentVariable(L"HOMEDRIVE", NULL, 0);
69+
e = (LPWSTR)malloc(sizeof(WCHAR) * (drvlen + len));
70+
drvlen = GetEnvironmentVariable(L"HOMEDRIVE", e, drvlen);
71+
GetEnvironmentVariable(L"HOMEPATH", e + drvlen, len);
72+
/* check if the path exists */
73+
attr = GetFileAttributesW(e);
74+
if (attr != INVALID_FILE_ATTRIBUTES
75+
&& (attr & FILE_ATTRIBUTE_DIRECTORY))
76+
SetEnvironmentVariable(L"HOME", e);
77+
else
78+
len = 0; /* use USERPROFILE */
79+
free(e);
80+
}
81+
6682
if (len == 0) {
6783
len = GetEnvironmentVariable(L"USERPROFILE", NULL, 0);
6884
if (len != 0) {
@@ -72,15 +88,6 @@ static void setup_environment(LPWSTR exepath, int full_path)
7288
free(e);
7389
}
7490
}
75-
else {
76-
int n;
77-
len += GetEnvironmentVariable(L"HOMEDRIVE", NULL, 0);
78-
e = (LPWSTR)malloc(sizeof(WCHAR) * (len + 2));
79-
n = GetEnvironmentVariable(L"HOMEDRIVE", e, len);
80-
GetEnvironmentVariable(L"HOMEPATH", &e[n], len-n);
81-
SetEnvironmentVariable(L"HOME", e);
82-
free(e);
83-
}
8491
}
8592

8693
/* extend the PATH */

0 commit comments

Comments
 (0)