Skip to content

Commit 5b49112

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 08f5629 commit 5b49112

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
@@ -2462,6 +2462,30 @@ static void setup_windows_environment(void)
24622462
/* simulate TERM to enable auto-color (see color.c) */
24632463
if (!getenv("TERM"))
24642464
setenv("TERM", "cygwin", 1);
2465+
2466+
/* calculate HOME if not set */
2467+
if (!getenv("HOME")) {
2468+
/*
2469+
* try $HOMEDRIVE$HOMEPATH - the home share may be a network
2470+
* location, thus also check if the path exists (i.e. is not
2471+
* disconnected)
2472+
*/
2473+
if ((tmp = getenv("HOMEDRIVE"))) {
2474+
struct strbuf buf = STRBUF_INIT;
2475+
strbuf_addstr(&buf, tmp);
2476+
if ((tmp = getenv("HOMEPATH"))) {
2477+
strbuf_addstr(&buf, tmp);
2478+
if (is_directory(buf.buf))
2479+
setenv("HOME", buf.buf, 1);
2480+
else
2481+
tmp = NULL; /* use $USERPROFILE */
2482+
}
2483+
strbuf_release(&buf);
2484+
}
2485+
/* use $USERPROFILE if the home share is not available */
2486+
if (!tmp && (tmp = getenv("USERPROFILE")))
2487+
setenv("HOME", tmp, 1);
2488+
}
24652489
}
24662490

24672491
/*

0 commit comments

Comments
 (0)