From 6754497c51bf48009834e274ba37d18717c22636 Mon Sep 17 00:00:00 2001 From: Johannes Schindelin Date: Wed, 2 Jun 2010 00:41:33 +0200 Subject: [PATCH] Add a Windows-specific fallback to getenv("HOME"); This fixes msysGit issue 482 properly. Signed-off-by: Johannes Schindelin --- builtin/config.c | 4 ++-- compat/mingw.c | 18 ++++++++++++++++++ compat/mingw.h | 3 +++ config.c | 2 +- git-compat-util.h | 4 ++++ path.c | 2 +- 6 files changed, 29 insertions(+), 4 deletions(-) diff --git a/builtin/config.c b/builtin/config.c index f3d1660d02..902a9f9c21 100644 --- a/builtin/config.c +++ b/builtin/config.c @@ -158,7 +158,7 @@ static int get_value(const char *key_, const char *regex_) local = config_exclusive_filename; if (!local) { - const char *home = getenv("HOME"); + const char *home = get_home_directory(); local = repo_config = git_pathdup("config"); if (git_config_global() && home) global = xstrdup(mkpath("%s/.gitconfig", home)); @@ -348,7 +348,7 @@ int cmd_config(int argc, const char **argv, const char *unused_prefix) } if (use_global_config) { - char *home = getenv("HOME"); + char *home = get_home_directory(); if (home) { char *user_config = xstrdup(mkpath("%s/.gitconfig", home)); config_exclusive_filename = user_config; diff --git a/compat/mingw.c b/compat/mingw.c index a77dc3a77e..ffe20432ee 100644 --- a/compat/mingw.c +++ b/compat/mingw.c @@ -1709,3 +1709,21 @@ struct dirent *mingw_readdir(DIR *dir) return (struct dirent*)&dir->dd_dir; } #endif // !NO_MINGW_REPLACE_READDIR + +const char *get_windows_home_directory() +{ + 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; +} diff --git a/compat/mingw.h b/compat/mingw.h index 72a3536854..bff552cb21 100644 --- a/compat/mingw.h +++ b/compat/mingw.h @@ -334,3 +334,6 @@ struct dirent *mingw_readdir(DIR *dir); * 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/config.c b/config.c index 67a82f055e..303fc48ccc 100644 --- a/config.c +++ b/config.c @@ -788,7 +788,7 @@ int git_config(config_fn_t fn, void *data) found += 1; } - home = getenv("HOME"); + home = get_home_directory(); if (git_config_global() && home) { char *user_config = xstrdup(mkpath("%s/.gitconfig", home)); if (!access(user_config, R_OK)) { diff --git a/git-compat-util.h b/git-compat-util.h index 801e3cfa0d..f957894b51 100644 --- a/git-compat-util.h +++ b/git-compat-util.h @@ -503,4 +503,8 @@ int remove_or_warn(unsigned int mode, const char *path); #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 b4c8d91722..21f20ae7f2 100644 --- a/path.c +++ b/path.c @@ -315,7 +315,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(); strbuf_add(&user_path, home, strlen(home)); } else { struct passwd *pw = getpw_str(username, username_len); -- 2.11.4.GIT