setup: expose enumerated repo info

We enumerate several different items as part of struct repository_format, but then actually set up those values using the global variables we've initialized from them. Instead, let's pass a pointer to the structure down to the code where we enumerate these values, so we can later on use those values directly to perform setup. This technique makes it easier for us to determine additional items about the repository format (such as the hash algorithm) and then use them for setup later on, without needing to add additional global variables. We can't avoid using the existing global variables since they're intricately intertwined with how things work at the moment, but this improves things for the future. Signed-off-by: brian m. carlson <sandals@crustytoothpaste.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>

brian m. carlson committed Nov 12, 2017 at 21:28 UTC abade65b79bdf505603b3699a2f9de75b318aa6e
1 file changed +25 -21
setup.c
+25 -21
@@ -434,16 +434,15 @@ static int check_repo_format(const char *var, const char *value, void *vdata)
434 return 0;
435 }
436
437 -static int check_repository_format_gently(const char *gitdir, int *nongit_ok)
437 +static int check_repository_format_gently(const char *gitdir, struct repository_format *candidate, int *nongit_ok)
438 {
439 struct strbuf sb = STRBUF_INIT;
440 struct strbuf err = STRBUF_INIT;
441 - struct repository_format candidate;
441 int has_common;
442
443 has_common = get_common_dir(&sb, gitdir);
444 strbuf_addstr(&sb, "/config");
446 - read_repository_format(&candidate, sb.buf);
445 + read_repository_format(candidate, sb.buf);
446 strbuf_release(&sb);
447
448 /*
@@ -451,10 +450,10 @@ static int check_repository_format_gently(const char *gitdir, int *nongit_ok)
450 * we treat a missing config as a silent "ok", even when nongit_ok
451 * is unset.
452 */
454 - if (candidate.version < 0)
453 + if (candidate->version < 0)
454 return 0;
455
457 - if (verify_repository_format(&candidate, &err) < 0) {
456 + if (verify_repository_format(candidate, &err) < 0) {
457 if (nongit_ok) {
458 warning("%s", err.buf);
459 strbuf_release(&err);
@@ -464,21 +463,21 @@ static int check_repository_format_gently(const char *gitdir, int *nongit_ok)
463 die("%s", err.buf);
464 }
465
467 - repository_format_precious_objects = candidate.precious_objects;
468 - string_list_clear(&candidate.unknown_extensions, 0);
466 + repository_format_precious_objects = candidate->precious_objects;
467 + string_list_clear(&candidate->unknown_extensions, 0);
468 if (!has_common) {
470 - if (candidate.is_bare != -1) {
471 - is_bare_repository_cfg = candidate.is_bare;
469 + if (candidate->is_bare != -1) {
470 + is_bare_repository_cfg = candidate->is_bare;
471 if (is_bare_repository_cfg == 1)
472 inside_work_tree = -1;
473 }
475 - if (candidate.work_tree) {
474 + if (candidate->work_tree) {
475 free(git_work_tree_cfg);
477 - git_work_tree_cfg = candidate.work_tree;
476 + git_work_tree_cfg = candidate->work_tree;
477 inside_work_tree = -1;
478 }
479 } else {
481 - free(candidate.work_tree);
480 + free(candidate->work_tree);
481 }
482
483 return 0;
@@ -625,6 +624,7 @@ cleanup_return:
624
625 static const char *setup_explicit_git_dir(const char *gitdirenv,
626 struct strbuf *cwd,
627 + struct repository_format *repo_fmt,
628 int *nongit_ok)
629 {
630 const char *work_tree_env = getenv(GIT_WORK_TREE_ENVIRONMENT);
@@ -650,7 +650,7 @@ static const char *setup_explicit_git_dir(const char *gitdirenv,
650 die("Not a git repository: '%s'", gitdirenv);
651 }
652
653 - if (check_repository_format_gently(gitdirenv, nongit_ok)) {
653 + if (check_repository_format_gently(gitdirenv, repo_fmt, nongit_ok)) {
654 free(gitfile);
655 return NULL;
656 }
@@ -723,9 +723,10 @@ static const char *setup_explicit_git_dir(const char *gitdirenv,
723
724 static const char *setup_discovered_git_dir(const char *gitdir,
725 struct strbuf *cwd, int offset,
726 + struct repository_format *repo_fmt,
727 int *nongit_ok)
728 {
728 - if (check_repository_format_gently(gitdir, nongit_ok))
729 + if (check_repository_format_gently(gitdir, repo_fmt, nongit_ok))
730 return NULL;
731
732 /* --work-tree is set without --git-dir; use discovered one */
@@ -737,7 +738,7 @@ static const char *setup_discovered_git_dir(const char *gitdir,
738 gitdir = to_free = real_pathdup(gitdir, 1);
739 if (chdir(cwd->buf))
740 die_errno("Could not come back to cwd");
740 - ret = setup_explicit_git_dir(gitdir, cwd, nongit_ok);
741 + ret = setup_explicit_git_dir(gitdir, cwd, repo_fmt, nongit_ok);
742 free(to_free);
743 return ret;
744 }
@@ -769,11 +770,12 @@ static const char *setup_discovered_git_dir(const char *gitdir,
770
771 /* #16.1, #17.1, #20.1, #21.1, #22.1 (see t1510) */
772 static const char *setup_bare_git_dir(struct strbuf *cwd, int offset,
773 + struct repository_format *repo_fmt,
774 int *nongit_ok)
775 {
776 int root_len;
777
776 - if (check_repository_format_gently(".", nongit_ok))
778 + if (check_repository_format_gently(".", repo_fmt, nongit_ok))
779 return NULL;
780
781 setenv(GIT_IMPLICIT_WORK_TREE_ENVIRONMENT, "0", 1);
@@ -785,7 +787,7 @@ static const char *setup_bare_git_dir(struct strbuf *cwd, int offset,
787 gitdir = offset == cwd->len ? "." : xmemdupz(cwd->buf, offset);
788 if (chdir(cwd->buf))
789 die_errno("Could not come back to cwd");
788 - return setup_explicit_git_dir(gitdir, cwd, nongit_ok);
790 + return setup_explicit_git_dir(gitdir, cwd, repo_fmt, nongit_ok);
791 }
792
793 inside_git_dir = 1;
@@ -1026,6 +1028,7 @@ const char *setup_git_directory_gently(int *nongit_ok)
1028 static struct strbuf cwd = STRBUF_INIT;
1029 struct strbuf dir = STRBUF_INIT, gitdir = STRBUF_INIT;
1030 const char *prefix;
1031 + struct repository_format repo_fmt;
1032
1033 /*
1034 * We may have read an incomplete configuration before
@@ -1053,18 +1056,18 @@ const char *setup_git_directory_gently(int *nongit_ok)
1056 prefix = NULL;
1057 break;
1058 case GIT_DIR_EXPLICIT:
1056 - prefix = setup_explicit_git_dir(gitdir.buf, &cwd, nongit_ok);
1059 + prefix = setup_explicit_git_dir(gitdir.buf, &cwd, &repo_fmt, nongit_ok);
1060 break;
1061 case GIT_DIR_DISCOVERED:
1062 if (dir.len < cwd.len && chdir(dir.buf))
1063 die(_("Cannot change to '%s'"), dir.buf);
1064 prefix = setup_discovered_git_dir(gitdir.buf, &cwd, dir.len,
1062 - nongit_ok);
1065 + &repo_fmt, nongit_ok);
1066 break;
1067 case GIT_DIR_BARE:
1068 if (dir.len < cwd.len && chdir(dir.buf))
1069 die(_("Cannot change to '%s'"), dir.buf);
1067 - prefix = setup_bare_git_dir(&cwd, dir.len, nongit_ok);
1070 + prefix = setup_bare_git_dir(&cwd, dir.len, &repo_fmt, nongit_ok);
1071 break;
1072 case GIT_DIR_HIT_CEILING:
1073 prefix = setup_nongit(cwd.buf, nongit_ok);
@@ -1171,7 +1174,8 @@ int git_config_perm(const char *var, const char *value)
1174
1175 void check_repository_format(void)
1176 {
1174 - check_repository_format_gently(get_git_dir(), NULL);
1177 + struct repository_format repo_fmt;
1178 + check_repository_format_gently(get_git_dir(), &repo_fmt, NULL);
1179 startup_info->have_repository = 1;
1180 }
1181