Skip to content

Commit 99a104d

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 f93399e commit 99a104d

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
@@ -2390,6 +2390,30 @@ static void setup_windows_environment(void)
23902390
/* simulate TERM to enable auto-color (see color.c) */
23912391
if (!getenv("TERM"))
23922392
setenv("TERM", "cygwin", 1);
2393+
2394+
/* calculate HOME if not set */
2395+
if (!getenv("HOME")) {
2396+
/*
2397+
* try $HOMEDRIVE$HOMEPATH - the home share may be a network
2398+
* location, thus also check if the path exists (i.e. is not
2399+
* disconnected)
2400+
*/
2401+
if ((tmp = getenv("HOMEDRIVE"))) {
2402+
struct strbuf buf = STRBUF_INIT;
2403+
strbuf_addstr(&buf, tmp);
2404+
if ((tmp = getenv("HOMEPATH"))) {
2405+
strbuf_addstr(&buf, tmp);
2406+
if (is_directory(buf.buf))
2407+
setenv("HOME", buf.buf, 1);
2408+
else
2409+
tmp = NULL; /* use $USERPROFILE */
2410+
}
2411+
strbuf_release(&buf);
2412+
}
2413+
/* use $USERPROFILE if the home share is not available */
2414+
if (!tmp && (tmp = getenv("USERPROFILE")))
2415+
setenv("HOME", tmp, 1);
2416+
}
23932417
}
23942418

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

0 commit comments

Comments
 (0)