environment.c caches results of many getenv calls. Under MinGW setenv(X) invalidates all previous values returned by getenv(X) so cached values become dangling pointers. Added cache-aware function set_git_dir to complement get_git_dir Signed-off-by: Dmitry Kakurin <Dmitry.Kakurin@xxxxxxxxx> --- builtin-init-db.c | 4 +--- cache.h | 1 + environment.c | 6 ++++++ git.c | 6 +++--- path.c | 2 +- setup.c | 6 +++--- 7 files changed, 21 insertions(+), 10 deletions(-) create mode 100644 config.mak diff --git a/builtin-init-db.c b/builtin-init-db.c index 5c0feba..62e579d 100644 --- a/builtin-init-db.c +++ b/builtin-init-db.c @@ -344,9 +344,7 @@ int cmd_init_db(int argc, const char **argv, const char *prefix) /* * Set up the default .git directory contents */ - git_dir = getenv(GIT_DIR_ENVIRONMENT); - if (!git_dir) - git_dir = DEFAULT_GIT_DIR_ENVIRONMENT; + git_dir = get_git_dir(); safe_create_dir(git_dir, 0); /* Check to see if the repository version is right. diff --git a/cache.h b/cache.h index 91e9f71..bc2916e 100644 --- a/cache.h +++ b/cache.h @@ -210,6 +210,7 @@ extern int is_bare_repository(void); extern int is_inside_git_dir(void); extern int is_inside_work_tree(void); extern const char *get_git_dir(void); +extern void set_git_dir(const char *newDir); extern char *get_object_directory(void); extern char *get_refs_directory(void); extern char *get_index_file(void); diff --git a/environment.c b/environment.c index f83fb9e..6ea7088 100644 --- a/environment.c +++ b/environment.c @@ -80,6 +80,12 @@ const char *get_git_dir(void) return git_dir; } +void set_git_dir(const char *newDir) +{ + setenv(GIT_DIR_ENVIRONMENT, newDir, 1); + git_dir = NULL; // reset cache +} + char *get_object_directory(void) { if (!git_object_dir) diff --git a/git.c b/git.c index 210a763..53dfa05 100644 --- a/git.c +++ b/git.c @@ -67,14 +67,14 @@ static int handle_options(const char*** argv, int* argc, int* envchanged) fprintf(stderr, "No directory given for --git-dir.\n" ); usage(git_usage_string); } - setenv(GIT_DIR_ENVIRONMENT, (*argv)[1], 1); + set_git_dir( (*argv)[1] ); if (envchanged) *envchanged = 1; (*argv)++; (*argc)--; handled++; } else if (!prefixcmp(cmd, "--git-dir=")) { - setenv(GIT_DIR_ENVIRONMENT, cmd + 10, 1); + set_git_dir(cmd + 10); if (envchanged) *envchanged = 1; } else if (!strcmp(cmd, "--work-tree")) { @@ -93,7 +93,7 @@ static int handle_options(const char*** argv, int* argc, int* envchanged) *envchanged = 1; } else if (!strcmp(cmd, "--bare")) { static char git_dir[PATH_MAX+1]; - setenv(GIT_DIR_ENVIRONMENT, getcwd(git_dir, sizeof(git_dir)), 1); + set_git_dir(getcwd(git_dir, sizeof(git_dir))); if (envchanged) *envchanged = 1; } else { diff --git a/path.c b/path.c index 8a06cf7..14af033 100644 --- a/path.c +++ b/path.c @@ -266,7 +266,7 @@ char *enter_repo(char *path, int strict) if (access("objects", X_OK) == 0 && access("refs", X_OK) == 0 && validate_headref("HEAD") == 0) { - setenv(GIT_DIR_ENVIRONMENT, ".", 1); + set_git_dir("."); check_repository_format(); return path; } diff --git a/setup.c b/setup.c index 47cd790..c5cf3ea 100644 --- a/setup.c +++ b/setup.c @@ -292,7 +292,7 @@ const char *setup_git_directory_gently(int *nongit_ok) } die("Not a git repository"); } - setenv(GIT_DIR_ENVIRONMENT, cwd, 1); + set_git_dir(cwd); gitdirenv = getenv(GIT_DIR_ENVIRONMENT); if (!gitdirenv) die("getenv after setenv failed"); @@ -332,8 +332,8 @@ const char *setup_git_directory_gently(int *nongit_ok) * In case there is a work tree we may change the directory, * therefore make GIT_DIR an absolute path. */ - if ( !is_absolute_path( gitdirenv ) ) { - setenv(GIT_DIR_ENVIRONMENT, gitdir, 1); + if (!is_absolute_path(gitdirenv)) { + set_git_dir(gitdir); gitdirenv = getenv(GIT_DIR_ENVIRONMENT); if (!gitdirenv) die("getenv after setenv failed"); -- 1.5.3.GIT - Dmitry - To unsubscribe from this list: send the line "unsubscribe git" in the body of a message to majordomo@xxxxxxxxxxxxxxx More majordomo info at http://vger.kernel.org/majordomo-info.html