diff --git a/compat/mingw.c b/compat/mingw.c index 6eabebc514..13ec7eae29 100644 --- a/compat/mingw.c +++ b/compat/mingw.c @@ -1969,6 +1969,23 @@ pid_t waitpid(pid_t pid, int *status, int options) return -1; } +const char *get_windows_home_directory(void) +{ + static const char *home_directory = NULL; + struct strbuf buf = STRBUF_INIT; + + if (home_directory) + return home_directory; + + home_directory = getenv("HOME"); + if (home_directory && *home_directory) + return home_directory; + + strbuf_addf(&buf, "%s/%s", getenv("HOMEDRIVE"), getenv("HOMEPATH")); + home_directory = strbuf_detach(&buf, NULL); + + return home_directory; +} int xutftowcsn(wchar_t *wcs, const char *utfs, size_t wcslen, int utflen) { int upos = 0, wpos = 0; @@ -2158,3 +2175,27 @@ void mingw_startup() /* initialize Unicode console */ winansi_init(); } + +int mingw_offset_1st_component(const char *path) +{ + int offset = 0; + if (has_dos_drive_prefix(path)) + offset = 2; + + /* unc paths */ + else if (is_dir_sep(path[0]) && is_dir_sep(path[1])) { + + /* skip server name */ + char *pos = strpbrk(path + 2, "\\/"); + if (!pos) + return 0; /* Error: malformed unc path */ + + do { + pos++; + } while (*pos && !is_dir_sep(*pos)); + + offset = pos - path; + } + + return offset + is_dir_sep(path[offset]); +} diff --git a/compat/mingw.h b/compat/mingw.h index e5006e92d9..2a4a03e487 100644 --- a/compat/mingw.h +++ b/compat/mingw.h @@ -487,3 +487,6 @@ static int mingw_main(c,v) * Used by Pthread API implementation for Windows */ extern int err_win_to_posix(DWORD winerr); + +extern const char *get_windows_home_directory(); +#define get_home_directory() get_windows_home_directory() diff --git a/git-compat-util.h b/git-compat-util.h index 087bf78db5..b7e145de62 100644 --- a/git-compat-util.h +++ b/git-compat-util.h @@ -727,4 +727,8 @@ struct tm *git_gmtime_r(const time_t *, struct tm *); #define mark_as_git_dir(x) /* noop */ #endif +#ifndef get_home_directory +#define get_home_directory() getenv("HOME") +#endif + #endif diff --git a/path.c b/path.c index f9c5062427..a5bb08cc3b 100644 --- a/path.c +++ b/path.c @@ -133,7 +133,7 @@ char *git_path(const char *fmt, ...) void home_config_paths(char **global, char **xdg, char *file) { char *xdg_home = getenv("XDG_CONFIG_HOME"); - char *home = getenv("HOME"); + const char *home = get_home_directory(); char *to_free = NULL; if (!home) { @@ -274,7 +274,7 @@ char *expand_user_path(const char *path) const char *username = path + 1; size_t username_len = first_slash - username; if (username_len == 0) { - const char *home = getenv("HOME"); + const char *home = get_home_directory(); if (!home) goto return_null; strbuf_add(&user_path, home, strlen(home)); diff --git a/shell.c b/shell.c index 5c0d47a5cc..edd8c3a1c1 100644 --- a/shell.c +++ b/shell.c @@ -55,7 +55,7 @@ static char *make_cmd(const char *prog) static void cd_to_homedir(void) { - const char *home = getenv("HOME"); + const char *home = get_home_directory(); if (!home) die("could not determine user's home directory; HOME is unset"); if (chdir(home) == -1)