setup: split off $GIT_DIR-set case from setup_git_directory_gently
If $GIT_DIR is set, setup_git_directory_gently does not have to do any repository discovery at all. Split off a function for the validation it still does do, in the hope that this will make setup_git_directory_gently proper less daunting to read. Signed-off-by: Jonathan Nieder <jrnieder@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Jonathan Nieder committed
Jul 24, 2010 at 06:19 UTC
e4e303479b62e474c52ee1f9edd265d7333117d9
1 file changed
+37
-32
setup.c
+37
-32
@@ -313,6 +313,41 @@ const char *read_gitfile_gently(const char *path)
313
return path;
314
}
315
316
+static const char *setup_explicit_git_dir(const char *gitdirenv,
317
+ const char *work_tree_env, int *nongit_ok)
318
+{
319
+ static char buffer[1024 + 1];
320
+ const char *retval;
321
+
322
+ if (PATH_MAX - 40 < strlen(gitdirenv))
323
+ die("'$%s' too big", GIT_DIR_ENVIRONMENT);
324
+ if (!is_git_directory(gitdirenv)) {
325
+ if (nongit_ok) {
326
+ *nongit_ok = 1;
327
+ return NULL;
328
+ }
329
+ die("Not a git repository: '%s'", gitdirenv);
330
+ }
331
+ if (!work_tree_env) {
332
+ retval = set_work_tree(gitdirenv);
333
+ /* config may override worktree */
334
+ if (check_repository_format_gently(nongit_ok))
335
+ return NULL;
336
+ return retval;
337
+ }
338
+ if (check_repository_format_gently(nongit_ok))
339
+ return NULL;
340
+ retval = get_relative_cwd(buffer, sizeof(buffer) - 1,
341
+ get_git_work_tree());
342
+ if (!retval || !*retval)
343
+ return NULL;
344
+ set_git_dir(make_absolute_path(gitdirenv));
345
+ if (chdir(work_tree_env) < 0)
346
+ die_errno ("Could not chdir to '%s'", work_tree_env);
347
+ strcat(buffer, "/");
348
+ return retval;
349
+}
350
+
351
/*
352
* We cannot decide in this function whether we are in the work tree or
353
* not, since the config can only be read _after_ this function was called.
@@ -343,38 +378,8 @@ const char *setup_git_directory_gently(int *nongit_ok)
378
* validation.
379
*/
380
gitdirenv = getenv(GIT_DIR_ENVIRONMENT);
346
- if (gitdirenv) {
347
- if (PATH_MAX - 40 < strlen(gitdirenv))
348
- die("'$%s' too big", GIT_DIR_ENVIRONMENT);
349
- if (is_git_directory(gitdirenv)) {
350
- static char buffer[1024 + 1];
351
- const char *retval;
352
-
353
- if (!work_tree_env) {
354
- retval = set_work_tree(gitdirenv);
355
- /* config may override worktree */
356
- if (check_repository_format_gently(nongit_ok))
357
- return NULL;
358
- return retval;
359
- }
360
- if (check_repository_format_gently(nongit_ok))
361
- return NULL;
362
- retval = get_relative_cwd(buffer, sizeof(buffer) - 1,
363
- get_git_work_tree());
364
- if (!retval || !*retval)
365
- return NULL;
366
- set_git_dir(make_absolute_path(gitdirenv));
367
- if (chdir(work_tree_env) < 0)
368
- die_errno ("Could not chdir to '%s'", work_tree_env);
369
- strcat(buffer, "/");
370
- return retval;
371
- }
372
- if (nongit_ok) {
373
- *nongit_ok = 1;
374
- return NULL;
375
- }
376
- die("Not a git repository: '%s'", gitdirenv);
377
- }
381
+ if (gitdirenv)
382
+ return setup_explicit_git_dir(gitdirenv, work_tree_env, nongit_ok);
383
384
if (!getcwd(cwd, sizeof(cwd)-1))
385
die_errno("Unable to read current working directory");