Skip to content
This repository was archived by the owner on Nov 9, 2017. It is now read-only.

Commit 8dfd8dc

Browse files
dschokasal
authored andcommitted
Add a Windows-specific fallback to getenv("HOME");
This fixes msysGit issue 482 properly. Signed-off-by: Johannes Schindelin <[email protected]>
1 parent 4620246 commit 8dfd8dc

File tree

5 files changed

+51
-3
lines changed

5 files changed

+51
-3
lines changed

compat/mingw.c

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1969,6 +1969,23 @@ pid_t waitpid(pid_t pid, int *status, int options)
19691969
return -1;
19701970
}
19711971

1972+
const char *get_windows_home_directory(void)
1973+
{
1974+
static const char *home_directory = NULL;
1975+
struct strbuf buf = STRBUF_INIT;
1976+
1977+
if (home_directory)
1978+
return home_directory;
1979+
1980+
home_directory = getenv("HOME");
1981+
if (home_directory && *home_directory)
1982+
return home_directory;
1983+
1984+
strbuf_addf(&buf, "%s/%s", getenv("HOMEDRIVE"), getenv("HOMEPATH"));
1985+
home_directory = strbuf_detach(&buf, NULL);
1986+
1987+
return home_directory;
1988+
}
19721989
int xutftowcsn(wchar_t *wcs, const char *utfs, size_t wcslen, int utflen)
19731990
{
19741991
int upos = 0, wpos = 0;
@@ -2158,3 +2175,27 @@ void mingw_startup()
21582175
/* initialize Unicode console */
21592176
winansi_init();
21602177
}
2178+
2179+
int mingw_offset_1st_component(const char *path)
2180+
{
2181+
int offset = 0;
2182+
if (has_dos_drive_prefix(path))
2183+
offset = 2;
2184+
2185+
/* unc paths */
2186+
else if (is_dir_sep(path[0]) && is_dir_sep(path[1])) {
2187+
2188+
/* skip server name */
2189+
char *pos = strpbrk(path + 2, "\\/");
2190+
if (!pos)
2191+
return 0; /* Error: malformed unc path */
2192+
2193+
do {
2194+
pos++;
2195+
} while (*pos && !is_dir_sep(*pos));
2196+
2197+
offset = pos - path;
2198+
}
2199+
2200+
return offset + is_dir_sep(path[offset]);
2201+
}

compat/mingw.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -487,3 +487,6 @@ static int mingw_main(c,v)
487487
* Used by Pthread API implementation for Windows
488488
*/
489489
extern int err_win_to_posix(DWORD winerr);
490+
491+
extern const char *get_windows_home_directory();
492+
#define get_home_directory() get_windows_home_directory()

git-compat-util.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -727,4 +727,8 @@ struct tm *git_gmtime_r(const time_t *, struct tm *);
727727
#define mark_as_git_dir(x) /* noop */
728728
#endif
729729

730+
#ifndef get_home_directory
731+
#define get_home_directory() getenv("HOME")
732+
#endif
733+
730734
#endif

path.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ char *git_path(const char *fmt, ...)
133133
void home_config_paths(char **global, char **xdg, char *file)
134134
{
135135
char *xdg_home = getenv("XDG_CONFIG_HOME");
136-
char *home = getenv("HOME");
136+
const char *home = get_home_directory();
137137
char *to_free = NULL;
138138

139139
if (!home) {
@@ -274,7 +274,7 @@ char *expand_user_path(const char *path)
274274
const char *username = path + 1;
275275
size_t username_len = first_slash - username;
276276
if (username_len == 0) {
277-
const char *home = getenv("HOME");
277+
const char *home = get_home_directory();
278278
if (!home)
279279
goto return_null;
280280
strbuf_add(&user_path, home, strlen(home));

shell.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ static char *make_cmd(const char *prog)
5555

5656
static void cd_to_homedir(void)
5757
{
58-
const char *home = getenv("HOME");
58+
const char *home = get_home_directory();
5959
if (!home)
6060
die("could not determine user's home directory; HOME is unset");
6161
if (chdir(home) == -1)

0 commit comments

Comments
 (0)