Skip to content

Commit 98937be

Browse files
pcloudsgitster
authored andcommitted
setup: split off a function to handle ordinary .git directories
Finish the clean-up of setup_git_directory_gently() by splitting the last case of validation+setup (global variables, prefix, check_format, set_git_dir) into its own function. Now setup_git_git_directory_gently itself takes care of discovery only and the functions that pick up from there are nearby in the source file so they can be easily compared. Signed-off-by: Jonathan Nieder <jrnieder@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent 3f8099f commit 98937be

1 file changed

Lines changed: 25 additions & 18 deletions

File tree

setup.c

Lines changed: 25 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -360,6 +360,28 @@ static int cwd_contains_git_dir(const char **gitfile_dirp)
360360
return is_git_directory(DEFAULT_GIT_DIR_ENVIRONMENT);
361361
}
362362

363+
static const char *setup_discovered_git_dir(const char *work_tree_env,
364+
int offset, int len, char *cwd, int *nongit_ok)
365+
{
366+
int root_len;
367+
368+
inside_git_dir = 0;
369+
if (!work_tree_env)
370+
inside_work_tree = 1;
371+
root_len = offset_1st_component(cwd);
372+
git_work_tree_cfg = xstrndup(cwd, offset > root_len ? offset : root_len);
373+
if (check_repository_format_gently(nongit_ok))
374+
return NULL;
375+
if (offset == len)
376+
return NULL;
377+
378+
/* Make "offset" point to past the '/', and add a '/' at the end */
379+
offset++;
380+
cwd[len++] = '/';
381+
cwd[len] = 0;
382+
return cwd + offset;
383+
}
384+
363385
static const char *setup_bare_git_dir(const char *work_tree_env,
364386
int offset, int len, char *cwd, int *nongit_ok)
365387
{
@@ -411,7 +433,7 @@ const char *setup_git_directory_gently(int *nongit_ok)
411433
static char cwd[PATH_MAX+1];
412434
const char *gitdirenv;
413435
const char *gitfile_dir;
414-
int len, offset, ceil_offset, root_len;
436+
int len, offset, ceil_offset;
415437
dev_t current_device = 0;
416438
int one_filesystem = 1;
417439

@@ -456,7 +478,8 @@ const char *setup_git_directory_gently(int *nongit_ok)
456478
current_device = get_device_or_die(".", NULL);
457479
for (;;) {
458480
if (cwd_contains_git_dir(&gitfile_dir))
459-
break;
481+
return setup_discovered_git_dir(work_tree_env, offset,
482+
len, cwd, nongit_ok);
460483
if (is_git_directory("."))
461484
return setup_bare_git_dir(work_tree_env, offset,
462485
len, cwd, nongit_ok);
@@ -482,22 +505,6 @@ const char *setup_git_directory_gently(int *nongit_ok)
482505
die_errno("Cannot change to '%s/..'", cwd);
483506
}
484507
}
485-
486-
inside_git_dir = 0;
487-
if (!work_tree_env)
488-
inside_work_tree = 1;
489-
root_len = offset_1st_component(cwd);
490-
git_work_tree_cfg = xstrndup(cwd, offset > root_len ? offset : root_len);
491-
if (check_repository_format_gently(nongit_ok))
492-
return NULL;
493-
if (offset == len)
494-
return NULL;
495-
496-
/* Make "offset" point to past the '/', and add a '/' at the end */
497-
offset++;
498-
cwd[len++] = '/';
499-
cwd[len] = 0;
500-
return cwd + offset;
501508
}
502509

503510
int git_config_perm(const char *var, const char *value)

0 commit comments

Comments
 (0)