Skip to content

Commit 6ddc2e0

Browse files
kbleesdscho
authored andcommitted
mingw: initialize HOME on startup
HOME initialization was historically duplicated in many different places, including /etc/profile, launch scripts such as git-bash.vbs and gitk.cmd, and (although slightly broken) in the git-wrapper. Even unrelated projects such as GitExtensions and TortoiseGit need to implement the same logic to be able to call git directly. Initialize HOME in git's own startup code so that we can eventually retire all the duplicate initialization code. Signed-off-by: Karsten Blees <[email protected]>
1 parent d5dab53 commit 6ddc2e0

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

compat/mingw.c

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2235,6 +2235,30 @@ static void setup_windows_environment()
22352235
/* simulate TERM to enable auto-color (see color.c) */
22362236
if (!getenv("TERM"))
22372237
setenv("TERM", "cygwin", 1);
2238+
2239+
/* calculate HOME if not set */
2240+
if (!getenv("HOME")) {
2241+
/*
2242+
* try $HOMEDRIVE$HOMEPATH - the home share may be a network
2243+
* location, thus also check if the path exists (i.e. is not
2244+
* disconnected)
2245+
*/
2246+
if ((tmp = getenv("HOMEDRIVE"))) {
2247+
struct strbuf buf = STRBUF_INIT;
2248+
strbuf_addstr(&buf, tmp);
2249+
if ((tmp = getenv("HOMEPATH"))) {
2250+
strbuf_addstr(&buf, tmp);
2251+
if (is_directory(buf.buf))
2252+
setenv("HOME", buf.buf, 1);
2253+
else
2254+
tmp = NULL; /* use $USERPROFILE */
2255+
}
2256+
strbuf_release(&buf);
2257+
}
2258+
/* use $USERPROFILE if the home share is not available */
2259+
if (!tmp && (tmp = getenv("USERPROFILE")))
2260+
setenv("HOME", tmp, 1);
2261+
}
22382262
}
22392263

22402264
int handle_long_path(wchar_t *path, int len, int max_path, int expand)

0 commit comments

Comments
 (0)