Skip to content

Commit fd6df17

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]> Signed-off-by: Johannes Schindelin <[email protected]>
1 parent aa25c82 commit fd6df17

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
@@ -2299,6 +2299,30 @@ static void setup_windows_environment(void)
22992299
/* simulate TERM to enable auto-color (see color.c) */
23002300
if (!getenv("TERM"))
23012301
setenv("TERM", "cygwin", 1);
2302+
2303+
/* calculate HOME if not set */
2304+
if (!getenv("HOME")) {
2305+
/*
2306+
* try $HOMEDRIVE$HOMEPATH - the home share may be a network
2307+
* location, thus also check if the path exists (i.e. is not
2308+
* disconnected)
2309+
*/
2310+
if ((tmp = getenv("HOMEDRIVE"))) {
2311+
struct strbuf buf = STRBUF_INIT;
2312+
strbuf_addstr(&buf, tmp);
2313+
if ((tmp = getenv("HOMEPATH"))) {
2314+
strbuf_addstr(&buf, tmp);
2315+
if (is_directory(buf.buf))
2316+
setenv("HOME", buf.buf, 1);
2317+
else
2318+
tmp = NULL; /* use $USERPROFILE */
2319+
}
2320+
strbuf_release(&buf);
2321+
}
2322+
/* use $USERPROFILE if the home share is not available */
2323+
if (!tmp && (tmp = getenv("USERPROFILE")))
2324+
setenv("HOME", tmp, 1);
2325+
}
23022326
}
23032327

23042328
/*

0 commit comments

Comments
 (0)