Skip to content

Commit abade65

Browse files
bk2204gitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 4123bca commit abade65

File tree

1 file changed

+25
-21
lines changed

1 file changed

+25
-21
lines changed

setup.c

Lines changed: 25 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -434,27 +434,26 @@ static int check_repo_format(const char *var, const char *value, void *vdata)
434434
return 0;
435435
}
436436

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)
438438
{
439439
struct strbuf sb = STRBUF_INIT;
440440
struct strbuf err = STRBUF_INIT;
441-
struct repository_format candidate;
442441
int has_common;
443442

444443
has_common = get_common_dir(&sb, gitdir);
445444
strbuf_addstr(&sb, "/config");
446-
read_repository_format(&candidate, sb.buf);
445+
read_repository_format(candidate, sb.buf);
447446
strbuf_release(&sb);
448447

449448
/*
450449
* For historical use of check_repository_format() in git-init,
451450
* we treat a missing config as a silent "ok", even when nongit_ok
452451
* is unset.
453452
*/
454-
if (candidate.version < 0)
453+
if (candidate->version < 0)
455454
return 0;
456455

457-
if (verify_repository_format(&candidate, &err) < 0) {
456+
if (verify_repository_format(candidate, &err) < 0) {
458457
if (nongit_ok) {
459458
warning("%s", err.buf);
460459
strbuf_release(&err);
@@ -464,21 +463,21 @@ static int check_repository_format_gently(const char *gitdir, int *nongit_ok)
464463
die("%s", err.buf);
465464
}
466465

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);
469468
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;
472471
if (is_bare_repository_cfg == 1)
473472
inside_work_tree = -1;
474473
}
475-
if (candidate.work_tree) {
474+
if (candidate->work_tree) {
476475
free(git_work_tree_cfg);
477-
git_work_tree_cfg = candidate.work_tree;
476+
git_work_tree_cfg = candidate->work_tree;
478477
inside_work_tree = -1;
479478
}
480479
} else {
481-
free(candidate.work_tree);
480+
free(candidate->work_tree);
482481
}
483482

484483
return 0;
@@ -625,6 +624,7 @@ const char *read_gitfile_gently(const char *path, int *return_error_code)
625624

626625
static const char *setup_explicit_git_dir(const char *gitdirenv,
627626
struct strbuf *cwd,
627+
struct repository_format *repo_fmt,
628628
int *nongit_ok)
629629
{
630630
const char *work_tree_env = getenv(GIT_WORK_TREE_ENVIRONMENT);
@@ -650,7 +650,7 @@ static const char *setup_explicit_git_dir(const char *gitdirenv,
650650
die("Not a git repository: '%s'", gitdirenv);
651651
}
652652

653-
if (check_repository_format_gently(gitdirenv, nongit_ok)) {
653+
if (check_repository_format_gently(gitdirenv, repo_fmt, nongit_ok)) {
654654
free(gitfile);
655655
return NULL;
656656
}
@@ -723,9 +723,10 @@ static const char *setup_explicit_git_dir(const char *gitdirenv,
723723

724724
static const char *setup_discovered_git_dir(const char *gitdir,
725725
struct strbuf *cwd, int offset,
726+
struct repository_format *repo_fmt,
726727
int *nongit_ok)
727728
{
728-
if (check_repository_format_gently(gitdir, nongit_ok))
729+
if (check_repository_format_gently(gitdir, repo_fmt, nongit_ok))
729730
return NULL;
730731

731732
/* --work-tree is set without --git-dir; use discovered one */
@@ -737,7 +738,7 @@ static const char *setup_discovered_git_dir(const char *gitdir,
737738
gitdir = to_free = real_pathdup(gitdir, 1);
738739
if (chdir(cwd->buf))
739740
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);
741742
free(to_free);
742743
return ret;
743744
}
@@ -769,11 +770,12 @@ static const char *setup_discovered_git_dir(const char *gitdir,
769770

770771
/* #16.1, #17.1, #20.1, #21.1, #22.1 (see t1510) */
771772
static const char *setup_bare_git_dir(struct strbuf *cwd, int offset,
773+
struct repository_format *repo_fmt,
772774
int *nongit_ok)
773775
{
774776
int root_len;
775777

776-
if (check_repository_format_gently(".", nongit_ok))
778+
if (check_repository_format_gently(".", repo_fmt, nongit_ok))
777779
return NULL;
778780

779781
setenv(GIT_IMPLICIT_WORK_TREE_ENVIRONMENT, "0", 1);
@@ -785,7 +787,7 @@ static const char *setup_bare_git_dir(struct strbuf *cwd, int offset,
785787
gitdir = offset == cwd->len ? "." : xmemdupz(cwd->buf, offset);
786788
if (chdir(cwd->buf))
787789
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);
789791
}
790792

791793
inside_git_dir = 1;
@@ -1026,6 +1028,7 @@ const char *setup_git_directory_gently(int *nongit_ok)
10261028
static struct strbuf cwd = STRBUF_INIT;
10271029
struct strbuf dir = STRBUF_INIT, gitdir = STRBUF_INIT;
10281030
const char *prefix;
1031+
struct repository_format repo_fmt;
10291032

10301033
/*
10311034
* We may have read an incomplete configuration before
@@ -1053,18 +1056,18 @@ const char *setup_git_directory_gently(int *nongit_ok)
10531056
prefix = NULL;
10541057
break;
10551058
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);
10571060
break;
10581061
case GIT_DIR_DISCOVERED:
10591062
if (dir.len < cwd.len && chdir(dir.buf))
10601063
die(_("Cannot change to '%s'"), dir.buf);
10611064
prefix = setup_discovered_git_dir(gitdir.buf, &cwd, dir.len,
1062-
nongit_ok);
1065+
&repo_fmt, nongit_ok);
10631066
break;
10641067
case GIT_DIR_BARE:
10651068
if (dir.len < cwd.len && chdir(dir.buf))
10661069
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);
10681071
break;
10691072
case GIT_DIR_HIT_CEILING:
10701073
prefix = setup_nongit(cwd.buf, nongit_ok);
@@ -1171,7 +1174,8 @@ int git_config_perm(const char *var, const char *value)
11711174

11721175
void check_repository_format(void)
11731176
{
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);
11751179
startup_info->have_repository = 1;
11761180
}
11771181

0 commit comments

Comments
 (0)