]> git.ipfire.org Git - thirdparty/git.git/commitdiff
checkout/fetch/pull/pack-objects: allow `-h` outside a repository
authorJohannes Schindelin <johannes.schindelin@gmx.de>
Tue, 8 Feb 2022 11:21:53 +0000 (11:21 +0000)
committerJunio C Hamano <gitster@pobox.com>
Tue, 8 Feb 2022 17:54:44 +0000 (09:54 -0800)
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 https://github.com/git-for-windows/git/issues/3688

Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
builtin/checkout.c
builtin/fetch.c
builtin/pack-objects.c
builtin/pull.c

index cc804ba8e1e6cdb8748eabc7e91575ac2ee53a4a..1c13d7fedb3c59188e345f55946fc2409d9e5818 100644 (file)
@@ -1602,9 +1602,10 @@ static int checkout_main(int argc, const char **argv, const char *prefix,
        opts->show_progress = -1;
 
        git_config(git_checkout_config, opts);
-
-       prepare_repo_settings(the_repository);
-       the_repository->settings.command_requires_full_index = 0;
+       if (the_repository->gitdir) {
+               prepare_repo_settings(the_repository);
+               the_repository->settings.command_requires_full_index = 0;
+       }
 
        opts->track = BRANCH_TRACK_UNSPECIFIED;
 
index 5f06b21f8e97c5459fdb558302c5b9b95e99eee8..c8ca4262de6dbea7d8f8382e089bd3ea88069ee7 100644 (file)
@@ -2014,8 +2014,10 @@ int cmd_fetch(int argc, const char **argv, const char *prefix)
        }
 
        git_config(git_fetch_config, NULL);
-       prepare_repo_settings(the_repository);
-       the_repository->settings.command_requires_full_index = 0;
+       if (the_repository->gitdir) {
+               prepare_repo_settings(the_repository);
+               the_repository->settings.command_requires_full_index = 0;
+       }
 
        argc = parse_options(argc, argv, prefix,
                             builtin_fetch_options, builtin_fetch_usage, 0);
index ba2006f2212bea19b202ba3db155b345bf7fbb89..87cb7b45c370e718a52fde5f87665cacc5770ee5 100644 (file)
@@ -3976,9 +3976,11 @@ int cmd_pack_objects(int argc, const char **argv, const char *prefix)
        read_replace_refs = 0;
 
        sparse = git_env_bool("GIT_TEST_PACK_SPARSE", -1);
-       prepare_repo_settings(the_repository);
-       if (sparse < 0)
-               sparse = the_repository->settings.pack_use_sparse;
+       if (the_repository->gitdir) {
+               prepare_repo_settings(the_repository);
+               if (sparse < 0)
+                       sparse = the_repository->settings.pack_use_sparse;
+       }
 
        reset_pack_idx_option(&pack_idx_opts);
        git_config(git_pack_config, NULL);
index 100cbf9fb85b59fee6a8f9f90ced02c9abdaac6e..d15007d93f3a5aacfa61111e127a74175b5592e2 100644 (file)
@@ -994,8 +994,10 @@ int cmd_pull(int argc, const char **argv, const char *prefix)
                set_reflog_message(argc, argv);
 
        git_config(git_pull_config, NULL);
-       prepare_repo_settings(the_repository);
-       the_repository->settings.command_requires_full_index = 0;
+       if (the_repository->gitdir) {
+               prepare_repo_settings(the_repository);
+               the_repository->settings.command_requires_full_index = 0;
+       }
 
        argc = parse_options(argc, argv, prefix, pull_options, pull_usage, 0);