set_git_dir: die when setenv() fails

The set_git_dir() function returns an error if setenv() fails, but there are zero callers who pay attention to this return value. If this ever were to happen, it could cause confusing results, as sub-processes would see a potentially stale GIT_DIR (e.g., if it is relative and we chdir()-ed to the root of the working tree). We _could_ try to fix each caller, but there's really nothing useful to do after this failure except die. Let's just lump setenv() failure into the same category as malloc failure: things that should never happen and cause us to abort catastrophically. Signed-off-by: Jeff King <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Jeff King committed Mar 30, 2018 at 14:34 UTC 48988c4d0c35b5c569a2645b61c9346f6062021d
2 files changed +3 -4
cache.h
+1 -1
@@ -477,7 +477,7 @@ extern const char *get_git_common_dir(void);
477 extern char *get_object_directory(void);
478 extern char *get_index_file(void);
479 extern char *get_graft_file(void);
480 -extern int set_git_dir(const char *path);
480 +extern void set_git_dir(const char *path);
481 extern int get_common_dir_noenv(struct strbuf *sb, const char *gitdir);
482 extern int get_common_dir(struct strbuf *sb, const char *gitdir);
483 extern const char *get_git_namespace(void);
environment.c
+2 -3
@@ -296,13 +296,12 @@ char *get_graft_file(void)
296 return the_repository->graft_file;
297 }
298
299 -int set_git_dir(const char *path)
299 +void set_git_dir(const char *path)
300 {
301 if (setenv(GIT_DIR_ENVIRONMENT, path, 1))
302 - return error("Could not set GIT_DIR to '%s'", path);
302 + die("could not set GIT_DIR to '%s'", path);
303 repo_set_gitdir(the_repository, path);
304 setup_git_env();
305 - return 0;
305 }
306
307 const char *get_log_output_encoding(void)