Use git_config_early() instead of git_config() during repo setup
When git_config() is called, either git_dir has already been set (by $GIT_DIR env or set_git_dir()), or it will default git_dir to ".git". git_config_early() gives setup functions more freedom because it does not require git_dir. Give it a config path, it will happily examine it. Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Nguyễn Thái Ngọc Duy committed
Nov 26, 2010 at 22:32 UTC
337e51cedfdf1f98e1c151b72f06c7ca8bcea0fd
1 file changed
+35
-12
setup.c
+35
-12
@@ -243,9 +243,21 @@ void setup_work_tree(void)
243
initialized = 1;
244
}
245
246
-static int check_repository_format_gently(int *nongit_ok)
246
+static int check_repository_format_gently(const char *gitdir, int *nongit_ok)
247
{
248
- git_config(check_repository_format_version, NULL);
248
+ char repo_config[PATH_MAX+1];
249
+
250
+ /*
251
+ * git_config() can't be used here because it calls git_pathdup()
252
+ * to get $GIT_CONFIG/config. That call will make setup_git_env()
253
+ * set git_dir to ".git".
254
+ *
255
+ * We are in gitdir setup, no git dir has been found useable yet.
256
+ * Use a gentler version of git_config() to check if this repo
257
+ * is a good one.
258
+ */
259
+ snprintf(repo_config, PATH_MAX, "%s/config", gitdir);
260
+ git_config_early(check_repository_format_version, NULL, repo_config);
261
if (GIT_REPO_VERSION < repository_format_version) {
262
if (!nongit_ok)
263
die ("Expected git repo version <= %d, found %d",
@@ -331,11 +343,11 @@ static const char *setup_explicit_git_dir(const char *gitdirenv,
343
if (!work_tree_env) {
344
retval = set_work_tree(gitdirenv);
345
/* config may override worktree */
334
- if (check_repository_format_gently(nongit_ok))
346
+ if (check_repository_format_gently(gitdirenv, nongit_ok))
347
return NULL;
348
return retval;
349
}
338
- if (check_repository_format_gently(nongit_ok))
350
+ if (check_repository_format_gently(gitdirenv, nongit_ok))
351
return NULL;
352
retval = get_relative_cwd(buffer, sizeof(buffer) - 1,
353
get_git_work_tree());
@@ -357,11 +369,17 @@ static int cwd_contains_git_dir(const char **gitfile_dirp)
369
die("Repository setup failed");
370
return 1;
371
}
360
- return is_git_directory(DEFAULT_GIT_DIR_ENVIRONMENT);
372
+ if (is_git_directory(DEFAULT_GIT_DIR_ENVIRONMENT)) {
373
+ *gitfile_dirp = DEFAULT_GIT_DIR_ENVIRONMENT;
374
+ return 1;
375
+ }
376
+ return 0;
377
}
378
379
static const char *setup_discovered_git_dir(const char *work_tree_env,
364
- int offset, int len, char *cwd, int *nongit_ok)
380
+ const char *gitdir,
381
+ int offset, int len,
382
+ char *cwd, int *nongit_ok)
383
{
384
int root_len;
385
@@ -370,7 +388,7 @@ static const char *setup_discovered_git_dir(const char *work_tree_env,
388
inside_work_tree = 1;
389
root_len = offset_1st_component(cwd);
390
git_work_tree_cfg = xstrndup(cwd, offset > root_len ? offset : root_len);
373
- if (check_repository_format_gently(nongit_ok))
391
+ if (check_repository_format_gently(gitdir, nongit_ok))
392
return NULL;
393
if (offset == len)
394
return NULL;
@@ -396,9 +414,12 @@ static const char *setup_bare_git_dir(const char *work_tree_env,
414
root_len = offset_1st_component(cwd);
415
cwd[offset > root_len ? offset : root_len] = '\0';
416
set_git_dir(cwd);
399
- } else
417
+ check_repository_format_gently(cwd, nongit_ok);
418
+ }
419
+ else {
420
set_git_dir(".");
401
- check_repository_format_gently(nongit_ok);
421
+ check_repository_format_gently(".", nongit_ok);
422
+ }
423
return NULL;
424
}
425
@@ -478,8 +499,10 @@ static const char *setup_git_directory_gently_1(int *nongit_ok)
499
current_device = get_device_or_die(".", NULL);
500
for (;;) {
501
if (cwd_contains_git_dir(&gitfile_dir))
481
- return setup_discovered_git_dir(work_tree_env, offset,
482
- len, cwd, nongit_ok);
502
+ return setup_discovered_git_dir(work_tree_env,
503
+ gitfile_dir,
504
+ offset, len,
505
+ cwd, nongit_ok);
506
if (is_git_directory("."))
507
return setup_bare_git_dir(work_tree_env, offset,
508
len, cwd, nongit_ok);
@@ -590,7 +613,7 @@ int check_repository_format_version(const char *var, const char *value, void *cb
613
614
int check_repository_format(void)
615
{
593
- return check_repository_format_gently(NULL);
616
+ return check_repository_format_gently(get_git_dir(), NULL);
617
}
618
619
/*