builtin/checkout-index: stop using `the_repository`

Remove the_repository global variable in favor of the repository argument that gets passed in "builtin/checkout-index.c". When `-h` is passed to the command outside a Git repository, the `run_builtin()` will call the `cmd_checkout_index()` function with `repo` set to NULL and then early in the function, `show_usage_with_options_if_asked()` call will give the options help and exit. Pass an instance of "struct index_state" available in the calling context to both `checkout_all()` and `checkout_file()` to remove their dependency on the global `the_repository` variable. Mentored-by: Christian Couder <chriscool@tuxfamily.org> Signed-off-by: Usman Akinyemi <usmanakinyemi202@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Usman Akinyemi committed Mar 8, 2025 at 05:05 UTC 09cbf1597edde2b0200ecbf469c78689c4dcb12f
2 files changed +28 -22
builtin/checkout-index.c
+21 -22
@@ -5,7 +5,6 @@
5 *
6 */
7
8 -#define USE_THE_REPOSITORY_VARIABLE
8 #define DISABLE_SIGN_COMPARE_WARNINGS
9
10 #include "builtin.h"
@@ -68,10 +67,10 @@ static void write_tempfile_record(const char *name, const char *prefix)
67 }
68 }
69
71 -static int checkout_file(const char *name, const char *prefix)
70 +static int checkout_file(struct index_state *index, const char *name, const char *prefix)
71 {
72 int namelen = strlen(name);
74 - int pos = index_name_pos(the_repository->index, name, namelen);
73 + int pos = index_name_pos(index, name, namelen);
74 int has_same_name = 0;
75 int is_file = 0;
76 int is_skipped = 1;
@@ -81,8 +80,8 @@ static int checkout_file(const char *name, const char *prefix)
80 if (pos < 0)
81 pos = -pos - 1;
82
84 - while (pos <the_repository->index->cache_nr) {
85 - struct cache_entry *ce =the_repository->index->cache[pos];
83 + while (pos < index->cache_nr) {
84 + struct cache_entry *ce = index->cache[pos];
85 if (ce_namelen(ce) != namelen ||
86 memcmp(ce->name, name, namelen))
87 break;
@@ -137,13 +136,13 @@ static int checkout_file(const char *name, const char *prefix)
136 return -1;
137 }
138
140 -static int checkout_all(const char *prefix, int prefix_length)
139 +static int checkout_all(struct index_state *index, const char *prefix, int prefix_length)
140 {
141 int i, errs = 0;
142 struct cache_entry *last_ce = NULL;
143
145 - for (i = 0; i < the_repository->index->cache_nr ; i++) {
146 - struct cache_entry *ce = the_repository->index->cache[i];
144 + for (i = 0; i < index->cache_nr ; i++) {
145 + struct cache_entry *ce = index->cache[i];
146
147 if (S_ISSPARSEDIR(ce->ce_mode)) {
148 if (!ce_skip_worktree(ce))
@@ -156,8 +155,8 @@ static int checkout_all(const char *prefix, int prefix_length)
155 * first entry inside the expanded sparse directory).
156 */
157 if (ignore_skip_worktree) {
159 - ensure_full_index(the_repository->index);
160 - ce = the_repository->index->cache[i];
158 + ensure_full_index(index);
159 + ce = index->cache[i];
160 }
161 }
162
@@ -213,7 +212,7 @@ static int option_parse_stage(const struct option *opt,
212 int cmd_checkout_index(int argc,
213 const char **argv,
214 const char *prefix,
216 - struct repository *repo UNUSED)
215 + struct repository *repo)
216 {
217 int i;
218 struct lock_file lock_file = LOCK_INIT;
@@ -253,19 +252,19 @@ int cmd_checkout_index(int argc,
252 show_usage_with_options_if_asked(argc, argv,
253 builtin_checkout_index_usage,
254 builtin_checkout_index_options);
256 - git_config(git_default_config, NULL);
255 + repo_config(repo, git_default_config, NULL);
256 prefix_length = prefix ? strlen(prefix) : 0;
257
259 - prepare_repo_settings(the_repository);
260 - the_repository->settings.command_requires_full_index = 0;
258 + prepare_repo_settings(repo);
259 + repo->settings.command_requires_full_index = 0;
260
262 - if (repo_read_index(the_repository) < 0) {
261 + if (repo_read_index(repo) < 0) {
262 die("invalid cache");
263 }
264
265 argc = parse_options(argc, argv, prefix, builtin_checkout_index_options,
266 builtin_checkout_index_usage, 0);
268 - state.istate = the_repository->index;
267 + state.istate = repo->index;
268 state.force = force;
269 state.quiet = quiet;
270 state.not_new = not_new;
@@ -285,8 +284,8 @@ int cmd_checkout_index(int argc,
284 */
285 if (index_opt && !state.base_dir_len && !to_tempfile) {
286 state.refresh_cache = 1;
288 - state.istate = the_repository->index;
289 - repo_hold_locked_index(the_repository, &lock_file,
287 + state.istate = repo->index;
288 + repo_hold_locked_index(repo, &lock_file,
289 LOCK_DIE_ON_ERROR);
290 }
291
@@ -304,7 +303,7 @@ int cmd_checkout_index(int argc,
303 if (read_from_stdin)
304 die("git checkout-index: don't mix '--stdin' and explicit filenames");
305 p = prefix_path(prefix, prefix_length, arg);
307 - err |= checkout_file(p, prefix);
306 + err |= checkout_file(repo->index, p, prefix);
307 free(p);
308 }
309
@@ -326,7 +325,7 @@ int cmd_checkout_index(int argc,
325 strbuf_swap(&buf, &unquoted);
326 }
327 p = prefix_path(prefix, prefix_length, buf.buf);
329 - err |= checkout_file(p, prefix);
328 + err |= checkout_file(repo->index, p, prefix);
329 free(p);
330 }
331 strbuf_release(&unquoted);
@@ -334,7 +333,7 @@ int cmd_checkout_index(int argc,
333 }
334
335 if (all)
337 - err |= checkout_all(prefix, prefix_length);
336 + err |= checkout_all(repo->index, prefix, prefix_length);
337
338 if (pc_workers > 1)
339 err |= run_parallel_checkout(&state, pc_workers, pc_threshold,
@@ -344,7 +343,7 @@ int cmd_checkout_index(int argc,
343 return 1;
344
345 if (is_lock_file_locked(&lock_file) &&
347 - write_locked_index(the_repository->index, &lock_file, COMMIT_LOCK))
346 + write_locked_index(repo->index, &lock_file, COMMIT_LOCK))
347 die("Unable to write new index file");
348 return 0;
349 }
t/t2006-checkout-index-basic.sh
+7
@@ -21,6 +21,13 @@ test_expect_success 'checkout-index -h in broken repository' '
21 test_grep "[Uu]sage" broken/usage
22 '
23
24 +test_expect_success 'checkout-index does not crash with -h' '
25 + test_expect_code 129 git checkout-index -h >usage &&
26 + test_grep "[Uu]sage: git checkout-index " usage &&
27 + test_expect_code 129 nongit git checkout-index -h >usage &&
28 + test_grep "[Uu]sage: git checkout-index " usage
29 +'
30 +
31 test_expect_success 'checkout-index reports errors (cmdline)' '
32 test_must_fail git checkout-index -- does-not-exist 2>stderr &&
33 test_grep not.in.the.cache stderr