Skip to content

Commit fed4a17

Browse files
kbleesGit for Windows Build Agent
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 f5587ca commit fed4a17

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
@@ -2206,6 +2206,30 @@ static void setup_windows_environment(void)
22062206
/* simulate TERM to enable auto-color (see color.c) */
22072207
if (!getenv("TERM"))
22082208
setenv("TERM", "cygwin", 1);
2209+
2210+
/* calculate HOME if not set */
2211+
if (!getenv("HOME")) {
2212+
/*
2213+
* try $HOMEDRIVE$HOMEPATH - the home share may be a network
2214+
* location, thus also check if the path exists (i.e. is not
2215+
* disconnected)
2216+
*/
2217+
if ((tmp = getenv("HOMEDRIVE"))) {
2218+
struct strbuf buf = STRBUF_INIT;
2219+
strbuf_addstr(&buf, tmp);
2220+
if ((tmp = getenv("HOMEPATH"))) {
2221+
strbuf_addstr(&buf, tmp);
2222+
if (is_directory(buf.buf))
2223+
setenv("HOME", buf.buf, 1);
2224+
else
2225+
tmp = NULL; /* use $USERPROFILE */
2226+
}
2227+
strbuf_release(&buf);
2228+
}
2229+
/* use $USERPROFILE if the home share is not available */
2230+
if (!tmp && (tmp = getenv("USERPROFILE")))
2231+
setenv("HOME", tmp, 1);
2232+
}
22092233
}
22102234

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

0 commit comments

Comments
 (0)