set_git_dir: die when setenv() fails
authorJeff King <peff@peff.net>
Fri, 30 Mar 2018 18:34:46 +0000 (14:34 -0400)
committerJunio C Hamano <gitster@pobox.com>
Fri, 30 Mar 2018 19:49:57 +0000 (12:49 -0700)
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>
cache.h
environment.c
diff --git a/cache.h b/cache.h
index a61b2d3f0d79b0f56992e0343803811f5265d716..5c24394d84426da27161db4925c226343a295969 100644 (file)
--- a/cache.h
+++ b/cache.h
@@ -477,7 +477,7 @@ extern const char *get_git_common_dir(void);
 extern char *get_object_directory(void);
 extern char *get_index_file(void);
 extern char *get_graft_file(void);
-extern int set_git_dir(const char *path);
+extern void set_git_dir(const char *path);
 extern int get_common_dir_noenv(struct strbuf *sb, const char *gitdir);
 extern int get_common_dir(struct strbuf *sb, const char *gitdir);
 extern const char *get_git_namespace(void);
index d6dd64662ce4d09296b61708b020a71eef3488f6..e01acf8b1132ebb2ece56c70b523c4eb18866b96 100644 (file)
@@ -296,13 +296,12 @@ char *get_graft_file(void)
        return the_repository->graft_file;
 }
 
-int set_git_dir(const char *path)
+void set_git_dir(const char *path)
 {
        if (setenv(GIT_DIR_ENVIRONMENT, path, 1))
-               return error("Could not set GIT_DIR to '%s'", path);
+               die("could not set GIT_DIR to '%s'", path);
        repo_set_gitdir(the_repository, path);
        setup_git_env();
-       return 0;
 }
 
 const char *get_log_output_encoding(void)