Skip to content

Commit e5b17bd

Browse files
ldenningtongitster
andcommitted
git: ensure correct git directory setup with -h
Ensure correct git directory setup when -h is passed with commands. This specifically applies to repos with special help text configuration variables and to commands run with -h outside a repository. This will also protect against test failures in the upcoming change to BUG in prepare_repo_settings if no git directory exists. Note: this diff is better seen when ignoring whitespace changes. Co-authored-by: Junio C Hamano <[email protected]> Signed-off-by: Lessley Dennington <[email protected]> Reviewed-by: Elijah Newren <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent ea6ae41 commit e5b17bd

File tree

1 file changed

+21
-18
lines changed

1 file changed

+21
-18
lines changed

git.c

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -421,27 +421,30 @@ static int run_builtin(struct cmd_struct *p, int argc, const char **argv)
421421
int status, help;
422422
struct stat st;
423423
const char *prefix;
424+
int run_setup = (p->option & (RUN_SETUP | RUN_SETUP_GENTLY));
424425

425-
prefix = NULL;
426426
help = argc == 2 && !strcmp(argv[1], "-h");
427-
if (!help) {
428-
if (p->option & RUN_SETUP)
429-
prefix = setup_git_directory();
430-
else if (p->option & RUN_SETUP_GENTLY) {
431-
int nongit_ok;
432-
prefix = setup_git_directory_gently(&nongit_ok);
433-
}
434-
precompose_argv_prefix(argc, argv, NULL);
435-
if (use_pager == -1 && p->option & (RUN_SETUP | RUN_SETUP_GENTLY) &&
436-
!(p->option & DELAY_PAGER_CONFIG))
437-
use_pager = check_pager_config(p->cmd);
438-
if (use_pager == -1 && p->option & USE_PAGER)
439-
use_pager = 1;
440-
441-
if ((p->option & (RUN_SETUP | RUN_SETUP_GENTLY)) &&
442-
startup_info->have_repository) /* get_git_dir() may set up repo, avoid that */
443-
trace_repo_setup(prefix);
427+
if (help && (run_setup & RUN_SETUP))
428+
/* demote to GENTLY to allow 'git cmd -h' outside repo */
429+
run_setup = RUN_SETUP_GENTLY;
430+
431+
if (run_setup & RUN_SETUP) {
432+
prefix = setup_git_directory();
433+
} else if (run_setup & RUN_SETUP_GENTLY) {
434+
int nongit_ok;
435+
prefix = setup_git_directory_gently(&nongit_ok);
436+
} else {
437+
prefix = NULL;
444438
}
439+
precompose_argv_prefix(argc, argv, NULL);
440+
if (use_pager == -1 && run_setup &&
441+
!(p->option & DELAY_PAGER_CONFIG))
442+
use_pager = check_pager_config(p->cmd);
443+
if (use_pager == -1 && p->option & USE_PAGER)
444+
use_pager = 1;
445+
if (run_setup && startup_info->have_repository)
446+
/* get_git_dir() may set up repo, avoid that */
447+
trace_repo_setup(prefix);
445448
commit_pager_choice();
446449

447450
if (!help && get_super_prefix()) {

0 commit comments

Comments
 (0)