Skip to content

Commit

Permalink
Merge pull request #73 from kblees/kb/environment-fixes
Browse files Browse the repository at this point in the history
Signed-off-by: Johannes Schindelin <[email protected]>
  • Loading branch information
dscho committed Apr 10, 2015
2 parents 2f464fb + d4e943c commit 27c85de
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 46 deletions.
90 changes: 58 additions & 32 deletions compat/mingw.c
Original file line number Diff line number Diff line change
Expand Up @@ -2218,6 +2218,62 @@ int handle_long_path(wchar_t *path, int len, int max_path, int expand)
}
}

static void setup_windows_environment()
{
char *tmp;

/* on Windows it is TMP and TEMP */
if (!getenv("TMPDIR")) {
if (!(tmp = getenv("TMP")))
tmp = getenv("TEMP");
if (tmp)
setenv("TMPDIR", tmp, 1);
}

if ((tmp = getenv("TMPDIR"))) {
/*
* Convert all dir separators to forward slashes,
* to help shell commands called from the Git
* executable (by not mistaking the dir separators
* for escape characters).
*/
for (; *tmp; tmp++)
if (*tmp == '\\')
*tmp = '/';
}

if (!getenv("TZ") && (tmp = getenv("MSYS2_TZ")))
setenv("TZ", tmp, 1);

/* simulate TERM to enable auto-color (see color.c) */
if (!getenv("TERM"))
setenv("TERM", "cygwin", 1);

/* calculate HOME if not set */
if (!getenv("HOME")) {
/*
* try $HOMEDRIVE$HOMEPATH - the home share may be a network
* location, thus also check if the path exists (i.e. is not
* disconnected)
*/
if ((tmp = getenv("HOMEDRIVE"))) {
struct strbuf buf = STRBUF_INIT;
strbuf_addstr(&buf, tmp);
if ((tmp = getenv("HOMEPATH"))) {
strbuf_addstr(&buf, tmp);
if (is_directory(buf.buf))
setenv("HOME", buf.buf, 1);
else
tmp = NULL; /* use $USERPROFILE */
}
strbuf_release(&buf);
}
/* use $USERPROFILE if the home share is not available */
if (!tmp && (tmp = getenv("USERPROFILE")))
setenv("HOME", tmp, 1);
}
}

/*
* Disable MSVCRT command line wildcard expansion (__getmainargs called from
* mingw startup code, see init.c in mingw runtime).
Expand Down Expand Up @@ -2287,46 +2343,16 @@ void mingw_startup()
__argv[0] = wcstoutfdup_startup(buffer, _wpgmptr, maxlen);
for (i = 1; i < argc; i++)
__argv[i] = wcstoutfdup_startup(buffer, wargv[i], maxlen);
for (i = 0; wenv[i]; i++) {
for (i = 0; wenv[i]; i++)
environ[i] = wcstoutfdup_startup(buffer, wenv[i], maxlen);
if (!strncasecmp(environ[i], "MSYS2_TZ=", 9)) {
char *to_free = environ[i];
environ[i] = xstrdup(to_free + 6);
free(to_free);
}
if (!strncasecmp(environ[i], "TMP=", 4)) {
/*
* Convert all dir separators to forward slashes,
* to help shell commands called from the Git
* executable (by not mistaking the dir separators
* for escape characters).
*/
char *p;
for (p = environ[i]; *p; p++)
if (*p == '\\')
*p = '/';
}
}
environ[i] = NULL;
free(buffer);

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

/* fix Windows specific environment settings */

/* on Windows it is TMP and TEMP */
if (!mingw_getenv("TMPDIR")) {
const char *tmp = mingw_getenv("TMP");
if (!tmp)
tmp = mingw_getenv("TEMP");
if (tmp)
setenv("TMPDIR", tmp, 1);
}

/* simulate TERM to enable auto-color (see color.c) */
if (!getenv("TERM"))
setenv("TERM", "cygwin", 1);
setup_windows_environment();

/*
* Avoid a segmentation fault when cURL tries to set the CHARSET
Expand Down
31 changes: 17 additions & 14 deletions compat/win32/git-wrapper.c
Original file line number Diff line number Diff line change
Expand Up @@ -48,21 +48,33 @@ static void setup_environment(LPWSTR exepath, int full_path)
L"MINGW%d", (int) sizeof(void *) * 8);
SetEnvironmentVariable(L"MSYSTEM", msystem);

/* if not set, set TERM to cygwin */
if (!GetEnvironmentVariable(L"TERM", NULL, 0))
SetEnvironmentVariable(L"TERM", L"cygwin");

/* if not set, set PLINK_PROTOCOL to ssh */
if (!GetEnvironmentVariable(L"PLINK_PROTOCOL", NULL, 0))
SetEnvironmentVariable(L"PLINK_PROTOCOL", L"ssh");

/* set HOME to %HOMEDRIVE%%HOMEPATH% or %USERPROFILE%
/*
* set HOME to %HOMEDRIVE%%HOMEPATH% or %USERPROFILE%
* With roaming profiles: HOMEPATH is the roaming location and
* USERPROFILE is the local location
*/
if (!GetEnvironmentVariable(L"HOME", NULL, 0)) {
LPWSTR e = NULL;
len = GetEnvironmentVariable(L"HOMEPATH", NULL, 0);
if (len) {
DWORD attr, drvlen = GetEnvironmentVariable(L"HOMEDRIVE", NULL, 0);
e = (LPWSTR)malloc(sizeof(WCHAR) * (drvlen + len));
drvlen = GetEnvironmentVariable(L"HOMEDRIVE", e, drvlen);
GetEnvironmentVariable(L"HOMEPATH", e + drvlen, len);
/* check if the path exists */
attr = GetFileAttributesW(e);
if (attr != INVALID_FILE_ATTRIBUTES
&& (attr & FILE_ATTRIBUTE_DIRECTORY))
SetEnvironmentVariable(L"HOME", e);
else
len = 0; /* use USERPROFILE */
free(e);
}

if (len == 0) {
len = GetEnvironmentVariable(L"USERPROFILE", NULL, 0);
if (len != 0) {
Expand All @@ -72,15 +84,6 @@ static void setup_environment(LPWSTR exepath, int full_path)
free(e);
}
}
else {
int n;
len += GetEnvironmentVariable(L"HOMEDRIVE", NULL, 0);
e = (LPWSTR)malloc(sizeof(WCHAR) * (len + 2));
n = GetEnvironmentVariable(L"HOMEDRIVE", e, len);
GetEnvironmentVariable(L"HOMEPATH", &e[n], len-n);
SetEnvironmentVariable(L"HOME", e);
free(e);
}
}

/* extend the PATH */
Expand Down

0 comments on commit 27c85de

Please sign in to comment.