Skip to content

Commit 059fda1

Browse files
dschogitster
authored andcommitted
checkout/fetch/pull/pack-objects: allow -h outside a repository
When we taught these commands about the sparse index, we did not account for the fact that the `cmd_*()` functions _can_ be called without a gitdir, namely when `-h` is passed to show the usage. A plausible approach to address this is to move the `prepare_repo_settings()` calls right after the `parse_options()` calls: The latter will never return when it handles `-h`, and therefore it is safe to assume that we have a `gitdir` at that point, as long as the built-in is marked with the `RUN_SETUP` flag. However, it is unfortunately not that simple. In `cmd_pack_objects()`, for example, the repo settings need to be fully populated so that the command-line options `--sparse`/`--no-sparse` can override them, not the other way round. Therefore, we choose to imitate the strategy taken in `cmd_diff()`, where we simply do not bother to prepare and initialize the repo settings unless we have a `gitdir`. This fixes #3688 Signed-off-by: Johannes Schindelin <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 4c53a8c commit 059fda1

File tree

4 files changed

+17
-10
lines changed

4 files changed

+17
-10
lines changed

builtin/checkout.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1602,9 +1602,10 @@ static int checkout_main(int argc, const char **argv, const char *prefix,
16021602
opts->show_progress = -1;
16031603

16041604
git_config(git_checkout_config, opts);
1605-
1606-
prepare_repo_settings(the_repository);
1607-
the_repository->settings.command_requires_full_index = 0;
1605+
if (the_repository->gitdir) {
1606+
prepare_repo_settings(the_repository);
1607+
the_repository->settings.command_requires_full_index = 0;
1608+
}
16081609

16091610
opts->track = BRANCH_TRACK_UNSPECIFIED;
16101611

builtin/fetch.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2014,8 +2014,10 @@ int cmd_fetch(int argc, const char **argv, const char *prefix)
20142014
}
20152015

20162016
git_config(git_fetch_config, NULL);
2017-
prepare_repo_settings(the_repository);
2018-
the_repository->settings.command_requires_full_index = 0;
2017+
if (the_repository->gitdir) {
2018+
prepare_repo_settings(the_repository);
2019+
the_repository->settings.command_requires_full_index = 0;
2020+
}
20192021

20202022
argc = parse_options(argc, argv, prefix,
20212023
builtin_fetch_options, builtin_fetch_usage, 0);

builtin/pack-objects.c

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3976,9 +3976,11 @@ int cmd_pack_objects(int argc, const char **argv, const char *prefix)
39763976
read_replace_refs = 0;
39773977

39783978
sparse = git_env_bool("GIT_TEST_PACK_SPARSE", -1);
3979-
prepare_repo_settings(the_repository);
3980-
if (sparse < 0)
3981-
sparse = the_repository->settings.pack_use_sparse;
3979+
if (the_repository->gitdir) {
3980+
prepare_repo_settings(the_repository);
3981+
if (sparse < 0)
3982+
sparse = the_repository->settings.pack_use_sparse;
3983+
}
39823984

39833985
reset_pack_idx_option(&pack_idx_opts);
39843986
git_config(git_pack_config, NULL);

builtin/pull.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -994,8 +994,10 @@ int cmd_pull(int argc, const char **argv, const char *prefix)
994994
set_reflog_message(argc, argv);
995995

996996
git_config(git_pull_config, NULL);
997-
prepare_repo_settings(the_repository);
998-
the_repository->settings.command_requires_full_index = 0;
997+
if (the_repository->gitdir) {
998+
prepare_repo_settings(the_repository);
999+
the_repository->settings.command_requires_full_index = 0;
1000+
}
9991001

10001002
argc = parse_options(argc, argv, prefix, pull_options, pull_usage, 0);
10011003

0 commit comments

Comments
 (0)