setup: unify repository version callbacks

Once upon a time, check_repository_format_gently would parse the config with a single callback, and that callback would set up a bunch of global variables. But now that we have separate workdirs, we have to be more careful. Commit 31e26eb (setup.c: support multi-checkout repo setup, 2014-11-30) introduced a reduced callback which omits some values like core.worktree. In the "main" callback we call the reduced one, and then add back in the missing variables. Now that we have split the config-parsing from the munging of the global variables, we can do it all with a single callback, and keep all of the "are we in a separate workdir" logic together. Signed-off-by: Jeff King <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Jeff King committed Mar 11, 2016 at 17:37 UTC 652f18ee8734ffb4a98271e5020dfa550db0f37b
2 files changed +23 -43
cache.h
-1
@@ -1582,7 +1582,6 @@ extern void git_config_set_multivar_in_file(const char *, const char *, const ch
1582 extern int git_config_rename_section(const char *, const char *);
1583 extern int git_config_rename_section_in_file(const char *, const char *, const char *);
1584 extern const char *git_etc_gitconfig(void);
1585 -extern int check_repository_format_version(const char *var, const char *value, void *cb);
1585 extern int git_env_bool(const char *, int);
1586 extern unsigned long git_env_ulong(const char *, unsigned long);
1587 extern int git_config_system(void);
setup.c
+23 -42
@@ -388,27 +388,26 @@ static int check_repo_format(const char *var, const char *value, void *vdata)
388 data->precious_objects = git_config_bool(var, value);
389 else
390 string_list_append(&data->unknown_extensions, ext);
391 + } else if (strcmp(var, "core.bare") == 0) {
392 + data->is_bare = git_config_bool(var, value);
393 + } else if (strcmp(var, "core.worktree") == 0) {
394 + if (!value)
395 + return config_error_nonbool(var);
396 + data->work_tree = xstrdup(value);
397 }
398 return 0;
399 }
400
395 -static int read_repository_format_1(struct repository_format *, config_fn_t,
396 - const char *);
397 -
401 static int check_repository_format_gently(const char *gitdir, int *nongit_ok)
402 {
403 struct strbuf sb = STRBUF_INIT;
404 struct strbuf err = STRBUF_INIT;
405 struct repository_format candidate;
403 - config_fn_t fn;
404 -
405 - if (get_common_dir(&sb, gitdir))
406 - fn = check_repo_format;
407 - else
408 - fn = check_repository_format_version;
406 + int has_common;
407
408 + has_common = get_common_dir(&sb, gitdir);
409 strbuf_addstr(&sb, "/config");
411 - read_repository_format_1(&candidate, fn, sb.buf);
410 + read_repository_format(&candidate, sb.buf);
411 strbuf_release(&sb);
412
413 /*
@@ -432,36 +431,34 @@ static int check_repository_format_gently(const char *gitdir, int *nongit_ok)
431 repository_format_version = candidate.version;
432 repository_format_precious_objects = candidate.precious_objects;
433 string_list_clear(&candidate.unknown_extensions, 0);
435 - if (candidate.is_bare != -1) {
436 - is_bare_repository_cfg = candidate.is_bare;
437 - if (is_bare_repository_cfg == 1)
434 + if (!has_common) {
435 + if (candidate.is_bare != -1) {
436 + is_bare_repository_cfg = candidate.is_bare;
437 + if (is_bare_repository_cfg == 1)
438 + inside_work_tree = -1;
439 + }
440 + if (candidate.work_tree) {
441 + free(git_work_tree_cfg);
442 + git_work_tree_cfg = candidate.work_tree;
443 inside_work_tree = -1;
439 - }
440 - if (candidate.work_tree) {
441 - free(git_work_tree_cfg);
442 - git_work_tree_cfg = candidate.work_tree;
443 - inside_work_tree = -1;
444 + }
445 + } else {
446 + free(candidate.work_tree);
447 }
448
449 return 0;
450 }
451
449 -static int read_repository_format_1(struct repository_format *format,
450 - config_fn_t fn, const char *path)
452 +int read_repository_format(struct repository_format *format, const char *path)
453 {
454 memset(format, 0, sizeof(*format));
455 format->version = -1;
456 format->is_bare = -1;
457 string_list_init(&format->unknown_extensions, 1);
456 - git_config_from_file(fn, path, format);
458 + git_config_from_file(check_repo_format, path, format);
459 return format->version;
460 }
461
460 -int read_repository_format(struct repository_format *format, const char *path)
461 -{
462 - return read_repository_format_1(format, check_repository_format_version, path);
463 -}
464 -
462 int verify_repository_format(const struct repository_format *format,
463 struct strbuf *err)
464 {
@@ -999,22 +996,6 @@ int git_config_perm(const char *var, const char *value)
996 return -(i & 0666);
997 }
998
1002 -int check_repository_format_version(const char *var, const char *value, void *cb)
1003 -{
1004 - struct repository_format *data = cb;
1005 - int ret = check_repo_format(var, value, cb);
1006 - if (ret)
1007 - return ret;
1008 - if (strcmp(var, "core.bare") == 0) {
1009 - data->is_bare = git_config_bool(var, value);
1010 - } else if (strcmp(var, "core.worktree") == 0) {
1011 - if (!value)
1012 - return config_error_nonbool(var);
1013 - data->work_tree = xstrdup(value);
1014 - }
1015 - return 0;
1016 -}
1017 -
999 void check_repository_format(void)
1000 {
1001 check_repository_format_gently(get_git_dir(), NULL);