backfill: assume --sparse when sparse-checkout is enabled

The previous change introduced the '--[no-]sparse' option for the 'git backfill' command, but did not assume it as enabled by default. However, this is likely the behavior that users will most often want to happen. Without this default, users with a small sparse-checkout may be confused when 'git backfill' downloads every version of every object in the full history. However, this is left as a separate change so this decision can be reviewed independently of the value of the '--[no-]sparse' option. Add a test of adding the '--sparse' option to a repo without sparse-checkout to make it clear that supplying it without a sparse-checkout is an error. Signed-off-by: Derrick Stolee <stolee@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Derrick Stolee committed Feb 3, 2025 at 17:11 UTC 85127bcdeab5ab34f9c738da3fcc88d637f39089
3 files changed +21 -2
Documentation/git-backfill.txt
+2 -1
@@ -59,7 +59,8 @@ OPTIONS
59
60 `--[no-]sparse`::
61 Only download objects if they appear at a path that matches the
62 - current sparse-checkout.
62 + current sparse-checkout. If the sparse-checkout feature is enabled,
63 + then `--sparse` is assumed and can be disabled with `--no-sparse`.
64
65 SEE ALSO
66 --------
builtin/backfill.c
+7
@@ -1,3 +1,6 @@
1 +/* We need this macro to access core_apply_sparse_checkout */
2 +#define USE_THE_REPOSITORY_VARIABLE
3 +
4 #include "builtin.h"
5 #include "git-compat-util.h"
6 #include "config.h"
@@ -5,6 +8,7 @@
8 #include "repository.h"
9 #include "commit.h"
10 #include "dir.h"
11 +#include "environment.h"
12 #include "hex.h"
13 #include "tree.h"
14 #include "tree-walk.h"
@@ -133,6 +137,9 @@ int cmd_backfill(int argc, const char **argv, const char *prefix, struct reposit
137
138 repo_config(repo, git_default_config, NULL);
139
140 + if (ctx.sparse < 0)
141 + ctx.sparse = core_apply_sparse_checkout;
142 +
143 result = do_backfill(&ctx);
144 backfill_context_clear(&ctx);
145 return result;
t/t5620-backfill.sh
+12 -1
@@ -77,6 +77,12 @@ test_expect_success 'do partial clone 2, backfill min batch size' '
77 test_line_count = 0 revs2
78 '
79
80 +test_expect_success 'backfill --sparse without sparse-checkout fails' '
81 + git init not-sparse &&
82 + test_must_fail git -C not-sparse backfill --sparse 2>err &&
83 + grep "problem loading sparse-checkout" err
84 +'
85 +
86 test_expect_success 'backfill --sparse' '
87 git clone --sparse --filter=blob:none \
88 --single-branch --branch=main \
@@ -105,7 +111,12 @@ test_expect_success 'backfill --sparse' '
111 test_trace2_data promisor fetch_count 8 <sparse-trace2 &&
112 test_trace2_data path-walk paths 15 <sparse-trace2 &&
113 git -C backfill3 rev-list --quiet --objects --missing=print HEAD >missing &&
108 - test_line_count = 24 missing
114 + test_line_count = 24 missing &&
115 +
116 + # Disabling the --sparse option (on by default) will download everything
117 + git -C backfill3 backfill --no-sparse &&
118 + git -C backfill3 rev-list --quiet --objects --missing=print HEAD >missing &&
119 + test_line_count = 0 missing
120 '
121
122 test_expect_success 'backfill --sparse without cone mode (positive)' '