Skip to content

Commit 27c85de

Browse files
committed
Merge pull request #73 from kblees/kb/environment-fixes
Signed-off-by: Johannes Schindelin <[email protected]>
2 parents 2f464fb + d4e943c commit 27c85de

File tree

2 files changed

+75
-46
lines changed

2 files changed

+75
-46
lines changed

compat/mingw.c

+58-32
Original file line numberDiff line numberDiff line change
@@ -2218,6 +2218,62 @@ int handle_long_path(wchar_t *path, int len, int max_path, int expand)
22182218
}
22192219
}
22202220

2221+
static void setup_windows_environment()
2222+
{
2223+
char *tmp;
2224+
2225+
/* on Windows it is TMP and TEMP */
2226+
if (!getenv("TMPDIR")) {
2227+
if (!(tmp = getenv("TMP")))
2228+
tmp = getenv("TEMP");
2229+
if (tmp)
2230+
setenv("TMPDIR", tmp, 1);
2231+
}
2232+
2233+
if ((tmp = getenv("TMPDIR"))) {
2234+
/*
2235+
* Convert all dir separators to forward slashes,
2236+
* to help shell commands called from the Git
2237+
* executable (by not mistaking the dir separators
2238+
* for escape characters).
2239+
*/
2240+
for (; *tmp; tmp++)
2241+
if (*tmp == '\\')
2242+
*tmp = '/';
2243+
}
2244+
2245+
if (!getenv("TZ") && (tmp = getenv("MSYS2_TZ")))
2246+
setenv("TZ", tmp, 1);
2247+
2248+
/* simulate TERM to enable auto-color (see color.c) */
2249+
if (!getenv("TERM"))
2250+
setenv("TERM", "cygwin", 1);
2251+
2252+
/* calculate HOME if not set */
2253+
if (!getenv("HOME")) {
2254+
/*
2255+
* try $HOMEDRIVE$HOMEPATH - the home share may be a network
2256+
* location, thus also check if the path exists (i.e. is not
2257+
* disconnected)
2258+
*/
2259+
if ((tmp = getenv("HOMEDRIVE"))) {
2260+
struct strbuf buf = STRBUF_INIT;
2261+
strbuf_addstr(&buf, tmp);
2262+
if ((tmp = getenv("HOMEPATH"))) {
2263+
strbuf_addstr(&buf, tmp);
2264+
if (is_directory(buf.buf))
2265+
setenv("HOME", buf.buf, 1);
2266+
else
2267+
tmp = NULL; /* use $USERPROFILE */
2268+
}
2269+
strbuf_release(&buf);
2270+
}
2271+
/* use $USERPROFILE if the home share is not available */
2272+
if (!tmp && (tmp = getenv("USERPROFILE")))
2273+
setenv("HOME", tmp, 1);
2274+
}
2275+
}
2276+
22212277
/*
22222278
* Disable MSVCRT command line wildcard expansion (__getmainargs called from
22232279
* mingw startup code, see init.c in mingw runtime).
@@ -2287,46 +2343,16 @@ void mingw_startup()
22872343
__argv[0] = wcstoutfdup_startup(buffer, _wpgmptr, maxlen);
22882344
for (i = 1; i < argc; i++)
22892345
__argv[i] = wcstoutfdup_startup(buffer, wargv[i], maxlen);
2290-
for (i = 0; wenv[i]; i++) {
2346+
for (i = 0; wenv[i]; i++)
22912347
environ[i] = wcstoutfdup_startup(buffer, wenv[i], maxlen);
2292-
if (!strncasecmp(environ[i], "MSYS2_TZ=", 9)) {
2293-
char *to_free = environ[i];
2294-
environ[i] = xstrdup(to_free + 6);
2295-
free(to_free);
2296-
}
2297-
if (!strncasecmp(environ[i], "TMP=", 4)) {
2298-
/*
2299-
* Convert all dir separators to forward slashes,
2300-
* to help shell commands called from the Git
2301-
* executable (by not mistaking the dir separators
2302-
* for escape characters).
2303-
*/
2304-
char *p;
2305-
for (p = environ[i]; *p; p++)
2306-
if (*p == '\\')
2307-
*p = '/';
2308-
}
2309-
}
23102348
environ[i] = NULL;
23112349
free(buffer);
23122350

23132351
/* sort environment for O(log n) getenv / putenv */
23142352
qsort(environ, i, sizeof(char*), compareenv);
23152353

23162354
/* fix Windows specific environment settings */
2317-
2318-
/* on Windows it is TMP and TEMP */
2319-
if (!mingw_getenv("TMPDIR")) {
2320-
const char *tmp = mingw_getenv("TMP");
2321-
if (!tmp)
2322-
tmp = mingw_getenv("TEMP");
2323-
if (tmp)
2324-
setenv("TMPDIR", tmp, 1);
2325-
}
2326-
2327-
/* simulate TERM to enable auto-color (see color.c) */
2328-
if (!getenv("TERM"))
2329-
setenv("TERM", "cygwin", 1);
2355+
setup_windows_environment();
23302356

23312357
/*
23322358
* Avoid a segmentation fault when cURL tries to set the CHARSET

compat/win32/git-wrapper.c

+17-14
Original file line numberDiff line numberDiff line change
@@ -48,21 +48,33 @@ static void setup_environment(LPWSTR exepath, int full_path)
4848
L"MINGW%d", (int) sizeof(void *) * 8);
4949
SetEnvironmentVariable(L"MSYSTEM", msystem);
5050

51-
/* if not set, set TERM to cygwin */
52-
if (!GetEnvironmentVariable(L"TERM", NULL, 0))
53-
SetEnvironmentVariable(L"TERM", L"cygwin");
54-
5551
/* if not set, set PLINK_PROTOCOL to ssh */
5652
if (!GetEnvironmentVariable(L"PLINK_PROTOCOL", NULL, 0))
5753
SetEnvironmentVariable(L"PLINK_PROTOCOL", L"ssh");
5854

59-
/* set HOME to %HOMEDRIVE%%HOMEPATH% or %USERPROFILE%
55+
/*
56+
* set HOME to %HOMEDRIVE%%HOMEPATH% or %USERPROFILE%
6057
* With roaming profiles: HOMEPATH is the roaming location and
6158
* USERPROFILE is the local location
6259
*/
6360
if (!GetEnvironmentVariable(L"HOME", NULL, 0)) {
6461
LPWSTR e = NULL;
6562
len = GetEnvironmentVariable(L"HOMEPATH", NULL, 0);
63+
if (len) {
64+
DWORD attr, drvlen = GetEnvironmentVariable(L"HOMEDRIVE", NULL, 0);
65+
e = (LPWSTR)malloc(sizeof(WCHAR) * (drvlen + len));
66+
drvlen = GetEnvironmentVariable(L"HOMEDRIVE", e, drvlen);
67+
GetEnvironmentVariable(L"HOMEPATH", e + drvlen, len);
68+
/* check if the path exists */
69+
attr = GetFileAttributesW(e);
70+
if (attr != INVALID_FILE_ATTRIBUTES
71+
&& (attr & FILE_ATTRIBUTE_DIRECTORY))
72+
SetEnvironmentVariable(L"HOME", e);
73+
else
74+
len = 0; /* use USERPROFILE */
75+
free(e);
76+
}
77+
6678
if (len == 0) {
6779
len = GetEnvironmentVariable(L"USERPROFILE", NULL, 0);
6880
if (len != 0) {
@@ -72,15 +84,6 @@ static void setup_environment(LPWSTR exepath, int full_path)
7284
free(e);
7385
}
7486
}
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-
}
8487
}
8588

8689
/* extend the PATH */

0 commit comments

Comments
 (0)