environment: guard state depending on a repository

In "environment.h" we have quite a lot of functions and variables that either explicitly or implicitly depend on `the_repository`. The implicit set of stateful declarations includes for example variables which get populated when parsing a repository's Git configuration. This set of variables is broken by design, as their state often depends on the last repository config that has been parsed. So they may or may not represent the state of `the_repository`. Fixing that is quite a big undertaking, and later patches in this series will demonstrate a solution for a first small set of those variables. So for now, let's guard these with `USE_THE_REPOSITORY_VARIABLE` so that callers are aware of the implicit dependency. Signed-off-by: Patrick Steinhardt <ps@pks.im> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Patrick Steinhardt committed Sep 12, 2024 at 13:30 UTC 673af418d0f271faadb24486348430e547d32d2a
14 files changed +53 -1
compat/mingw.c
+2
@@ -1,3 +1,5 @@
1 +#define USE_THE_REPOSITORY_VARIABLE
2 +
3 #include "../git-compat-util.h"
4 #include "win32.h"
5 #include <aclapi.h>
compat/win32/path-utils.c
+2
@@ -1,3 +1,5 @@
1 +#define USE_THE_REPOSITORY_VARIABLE
2 +
3 #include "../../git-compat-util.h"
4 #include "../../environment.h"
5
config.c
+2
@@ -6,6 +6,8 @@
6 *
7 */
8
9 +#define USE_THE_REPOSITORY_VARIABLE
10 +
11 #include "git-compat-util.h"
12 #include "abspath.h"
13 #include "advice.h"
environment.h
+24 -1
@@ -102,6 +102,28 @@ int use_optional_locks(void);
102 const char *get_git_namespace(void);
103 const char *strip_namespace(const char *namespaced_ref);
104
105 +/*
106 + * TODO: All the below state either explicitly or implicitly relies on
107 + * `the_repository`. We should eventually get rid of these and make the
108 + * dependency on a repository explicit:
109 + *
110 + * - `setup_git_env()` ideally shouldn't exist as it modifies global state,
111 + * namely the environment. The current process shouldn't ever access that
112 + * state via envvars though, but should instead consult a `struct
113 + * repository`. When spawning new processes, we would ideally also pass a
114 + * `struct repository` and then set up the environment variables for the
115 + * child process, only.
116 + *
117 + * - `have_git_dir()` should not have to exist at all. Instead, we should
118 + * decide on whether or not we have a `struct repository`.
119 + *
120 + * - All the global config variables should become tied to a repository. Like
121 + * this, we'd correctly honor repository-local configuration and be able to
122 + * distinguish configuration values from different repositories.
123 + *
124 + * Please do not add new global config variables here.
125 + */
126 +# ifdef USE_THE_REPOSITORY_VARIABLE
127 void setup_git_env(const char *git_dir);
128
129 /*
@@ -213,4 +235,5 @@ extern const char *comment_line_str;
235 extern char *comment_line_str_to_free;
236 extern int auto_comment_line_char;
237
216 -#endif
238 +# endif /* USE_THE_REPOSITORY_VARIABLE */
239 +#endif /* ENVIRONMENT_H */
name-hash.c
+3
@@ -5,6 +5,9 @@
5 *
6 * Copyright (C) 2008 Linus Torvalds
7 */
8 +
9 +#define USE_THE_REPOSITORY_VARIABLE
10 +
11 #include "git-compat-util.h"
12 #include "environment.h"
13 #include "gettext.h"
path.c
+2
@@ -2,6 +2,8 @@
2 * Utilities for paths and pathnames
3 */
4
5 +#define USE_THE_REPOSITORY_VARIABLE
6 +
7 #include "git-compat-util.h"
8 #include "abspath.h"
9 #include "environment.h"
preload-index.c
+3
@@ -1,6 +1,9 @@
1 /*
2 * Copyright (C) 2008 Linus Torvalds
3 */
4 +
5 +#define USE_THE_REPOSITORY_VARIABLE
6 +
7 #include "git-compat-util.h"
8 #include "pathspec.h"
9 #include "dir.h"
prompt.c
+2
@@ -1,3 +1,5 @@
1 +#define USE_THE_REPOSITORY_VARIABLE
2 +
3 #include "git-compat-util.h"
4 #include "parse.h"
5 #include "environment.h"
refs/files-backend.c
+2
@@ -1,3 +1,5 @@
1 +#define USE_THE_REPOSITORY_VARIABLE
2 +
3 #include "../git-compat-util.h"
4 #include "../copy.h"
5 #include "../environment.h"
sparse-index.c
+2
@@ -1,3 +1,5 @@
1 +#define USE_THE_REPOSITORY_VARIABLE
2 +
3 #include "git-compat-util.h"
4 #include "environment.h"
5 #include "gettext.h"
statinfo.c
+2
@@ -1,3 +1,5 @@
1 +#define USE_THE_REPOSITORY_VARIABLE
2 +
3 #include "git-compat-util.h"
4 #include "environment.h"
5 #include "statinfo.h"
t/helper/test-path-utils.c
+2
@@ -1,3 +1,5 @@
1 +#define USE_THE_REPOSITORY_VARIABLE
2 +
3 #include "test-tool.h"
4 #include "abspath.h"
5 #include "environment.h"
tree-diff.c
+3
@@ -1,6 +1,9 @@
1 /*
2 * Helper functions for tree diff generation
3 */
4 +
5 +#define USE_THE_REPOSITORY_VARIABLE
6 +
7 #include "git-compat-util.h"
8 #include "diff.h"
9 #include "diffcore.h"
userdiff.c
+2
@@ -1,3 +1,5 @@
1 +#define USE_THE_REPOSITORY_VARIABLE
2 +
3 #include "git-compat-util.h"
4 #include "config.h"
5 #include "userdiff.h"