Skip to content

Commit 8aa6e42

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 56cd5e4 commit 8aa6e42

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
@@ -2256,6 +2256,30 @@ static void setup_windows_environment()
22562256
/* simulate TERM to enable auto-color (see color.c) */
22572257
if (!getenv("TERM"))
22582258
setenv("TERM", "cygwin", 1);
2259+
2260+
/* calculate HOME if not set */
2261+
if (!getenv("HOME")) {
2262+
/*
2263+
* try $HOMEDRIVE$HOMEPATH - the home share may be a network
2264+
* location, thus also check if the path exists (i.e. is not
2265+
* disconnected)
2266+
*/
2267+
if ((tmp = getenv("HOMEDRIVE"))) {
2268+
struct strbuf buf = STRBUF_INIT;
2269+
strbuf_addstr(&buf, tmp);
2270+
if ((tmp = getenv("HOMEPATH"))) {
2271+
strbuf_addstr(&buf, tmp);
2272+
if (is_directory(buf.buf))
2273+
setenv("HOME", buf.buf, 1);
2274+
else
2275+
tmp = NULL; /* use $USERPROFILE */
2276+
}
2277+
strbuf_release(&buf);
2278+
}
2279+
/* use $USERPROFILE if the home share is not available */
2280+
if (!tmp && (tmp = getenv("USERPROFILE")))
2281+
setenv("HOME", tmp, 1);
2282+
}
22592283
}
22602284

22612285
/*

0 commit comments

Comments
 (0)