init-db: remove unnecessary global variable

This commit was prompted by a desire to move the functions which builtin/init-db.c and builtin/clone.c share out of the former file and into setup.c. One issue that made it difficult was the init_is_bare_repository global variable. init_is_bare_repository's sole use in life it to cache a value in init_db(), and then be used in create_default_files(). This is a bit odd since init_db() directly calls create_default_files(), and is the only caller of that function. Convert the global to a simple function parameter instead. (Of course, this doesn't fix the fact that this value is then ignored by create_default_files(), as noted in a big TODO comment in that function, but it at least includes no behavioral change other than getting rid of a very questionable global variable.) Signed-off-by: Elijah Newren <newren@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Elijah Newren committed May 16, 2023 at 06:33 UTC c2f76965d0202a894c9491e0647b7834b58cd168
1 file changed +7 -5
builtin/init-db.c
+7 -5
@@ -31,7 +31,6 @@
31
32 #define GIT_DEFAULT_HASH_ENVIRONMENT "GIT_DEFAULT_HASH"
33
34 -static int init_is_bare_repository = 0;
34 static int init_shared_repository = -1;
35
36 static void copy_templates_1(struct strbuf *path, struct strbuf *template_path,
@@ -199,6 +198,7 @@ static int create_default_files(const char *template_path,
198 const char *original_git_dir,
199 const char *initial_branch,
200 const struct repository_format *fmt,
201 + int prev_bare_repository,
202 int quiet)
203 {
204 struct stat st1;
@@ -237,7 +237,7 @@ static int create_default_files(const char *template_path,
237 * TODO: heed core.bare from config file in templates if no
238 * command-line override given
239 */
240 - is_bare_repository_cfg = init_is_bare_repository || !work_tree;
240 + is_bare_repository_cfg = prev_bare_repository || !work_tree;
241 /* TODO (continued):
242 *
243 * Unfortunately, the line above is equivalent to
@@ -246,7 +246,7 @@ static int create_default_files(const char *template_path,
246 * command line option was present.
247 *
248 * To see why, note that before this function, there was this call:
249 - * init_is_bare_repository = is_bare_repository()
249 + * prev_bare_repository = is_bare_repository()
250 * expanding the right hand side:
251 * = is_bare_repository_cfg && !get_git_work_tree()
252 * = is_bare_repository_cfg && !work_tree
@@ -256,7 +256,7 @@ static int create_default_files(const char *template_path,
256 * calls will return the same result each time. So, what we are
257 * interested in computing is the right hand side of the line of
258 * code just above this comment:
259 - * init_is_bare_repository || !work_tree
259 + * prev_bare_repository || !work_tree
260 * = is_bare_repository_cfg && !work_tree || !work_tree
261 * = !work_tree
262 * because "A && !B || !B == !B" for all boolean values of A & B.
@@ -424,6 +424,7 @@ int init_db(const char *git_dir, const char *real_git_dir,
424 int exist_ok = flags & INIT_DB_EXIST_OK;
425 char *original_git_dir = real_pathdup(git_dir, 1);
426 struct repository_format repo_fmt = REPOSITORY_FORMAT_INIT;
427 + int prev_bare_repository;
428
429 if (real_git_dir) {
430 struct stat st;
@@ -449,7 +450,7 @@ int init_db(const char *git_dir, const char *real_git_dir,
450
451 safe_create_dir(git_dir, 0);
452
452 - init_is_bare_repository = is_bare_repository();
453 + prev_bare_repository = is_bare_repository();
454
455 /* Check to see if the repository version is right.
456 * Note that a newly created repository does not have
@@ -462,6 +463,7 @@ int init_db(const char *git_dir, const char *real_git_dir,
463
464 reinit = create_default_files(template_dir, original_git_dir,
465 initial_branch, &repo_fmt,
466 + prev_bare_repository,
467 flags & INIT_DB_QUIET);
468 if (reinit && initial_branch)
469 warning(_("re-init: ignored --initial-branch=%s"),