# [PATCH 0/1] Give Git a HOME on Windows

2 messages from 2019-07-04 to 2019-07-04. Participants: Johannes Schindelin via GitGitGadget, Karsten Blees via GitGitGadget.
Thread: https://gitlist.dev/t/51429

## Johannes Schindelin via GitGitGadget, 2019-07-04 09:20

Subject: [PATCH 0/1] Give Git a HOME on Windows
Message-ID: <pull.218.git.gitgitgadget@gmail.com>
URL: https://gitlist.dev/e/pull.218.git.gitgitgadget%40gmail.com

```
The environment variable HOME is a well-known concept on Unix/Linux, but not
so much on Windows. In fact, there are competing concepts, and they fulfill
separate roles.

Let's try to map the closest that we can find to HOME so that Git is happy.

Git for Windows carries this patch since 2015, so I think we're pretty sure
that our chosen strategy works.

Karsten Blees (1):
  mingw: initialize HOME on startup

 compat/mingw.c | 24 ++++++++++++++++++++++++
 1 file changed, 24 insertions(+)


base-commit: aa25c82427ae70aebf3b8f970f2afd54e9a2a8c6
Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-218%2Fdscho%2Fmingw-home-v1
Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-218/dscho/mingw-home-v1
Pull-Request: https://github.com/gitgitgadget/git/pull/218
-- 
gitgitgadget

```

## Karsten Blees via GitGitGadget, 2019-07-04 09:20

Subject: [PATCH 1/1] mingw: initialize HOME on startup
Message-ID: <fd6df1795048ebebc54502ef8d2ddd7728a08085.1562232032.git.gitgitgadget@gmail.com>
URL: https://gitlist.dev/e/fd6df1795048ebebc54502ef8d2ddd7728a08085.1562232032.git.gitgitgadget%40gmail.com
In-Reply-To: <pull.218.git.gitgitgadget@gmail.com>

```
From: Karsten Blees <blees@dcon.de>

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 <blees@dcon.de>
Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
---
 compat/mingw.c | 24 ++++++++++++++++++++++++
 1 file changed, 24 insertions(+)

diff --git a/compat/mingw.c b/compat/mingw.c
index 9b6d2400e1..1047427cb5 100644
--- a/compat/mingw.c
+++ b/compat/mingw.c
@@ -2299,6 +2299,30 @@ static void setup_windows_environment(void)
 	/* simulate TERM to enable auto-color (see color.c) */
 	if (!getenv("TERM"))
 		setenv("TERM", "cygwin", 1);
+
+	/* calculate HOME if not set */
+	if (!getenv("HOME")) {
+		/*
+		 * try $HOMEDRIVE$HOMEPATH - the home share may be a network
+		 * location, thus also check if the path exists (i.e. is not
+		 * disconnected)
+		 */
+		if ((tmp = getenv("HOMEDRIVE"))) {
+			struct strbuf buf = STRBUF_INIT;
+			strbuf_addstr(&buf, tmp);
+			if ((tmp = getenv("HOMEPATH"))) {
+				strbuf_addstr(&buf, tmp);
+				if (is_directory(buf.buf))
+					setenv("HOME", buf.buf, 1);
+				else
+					tmp = NULL; /* use $USERPROFILE */
+			}
+			strbuf_release(&buf);
+		}
+		/* use $USERPROFILE if the home share is not available */
+		if (!tmp && (tmp = getenv("USERPROFILE")))
+			setenv("HOME", tmp, 1);
+	}
 }
 
 /*
-- 
gitgitgadget

```
