Skip to content

Commit 09cbf15

Browse files
Unique-Usmangitster
authored andcommitted
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 <[email protected]> Signed-off-by: Usman Akinyemi <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent d9dce89 commit 09cbf15

File tree

2 files changed

+28
-22
lines changed

2 files changed

+28
-22
lines changed

builtin/checkout-index.c

+21-22
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
*
66
*/
77

8-
#define USE_THE_REPOSITORY_VARIABLE
98
#define DISABLE_SIGN_COMPARE_WARNINGS
109

1110
#include "builtin.h"
@@ -68,10 +67,10 @@ static void write_tempfile_record(const char *name, const char *prefix)
6867
}
6968
}
7069

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)
7271
{
7372
int namelen = strlen(name);
74-
int pos = index_name_pos(the_repository->index, name, namelen);
73+
int pos = index_name_pos(index, name, namelen);
7574
int has_same_name = 0;
7675
int is_file = 0;
7776
int is_skipped = 1;
@@ -81,8 +80,8 @@ static int checkout_file(const char *name, const char *prefix)
8180
if (pos < 0)
8281
pos = -pos - 1;
8382

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];
8685
if (ce_namelen(ce) != namelen ||
8786
memcmp(ce->name, name, namelen))
8887
break;
@@ -137,13 +136,13 @@ static int checkout_file(const char *name, const char *prefix)
137136
return -1;
138137
}
139138

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)
141140
{
142141
int i, errs = 0;
143142
struct cache_entry *last_ce = NULL;
144143

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];
147146

148147
if (S_ISSPARSEDIR(ce->ce_mode)) {
149148
if (!ce_skip_worktree(ce))
@@ -156,8 +155,8 @@ static int checkout_all(const char *prefix, int prefix_length)
156155
* first entry inside the expanded sparse directory).
157156
*/
158157
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];
161160
}
162161
}
163162

@@ -213,7 +212,7 @@ static int option_parse_stage(const struct option *opt,
213212
int cmd_checkout_index(int argc,
214213
const char **argv,
215214
const char *prefix,
216-
struct repository *repo UNUSED)
215+
struct repository *repo)
217216
{
218217
int i;
219218
struct lock_file lock_file = LOCK_INIT;
@@ -253,19 +252,19 @@ int cmd_checkout_index(int argc,
253252
show_usage_with_options_if_asked(argc, argv,
254253
builtin_checkout_index_usage,
255254
builtin_checkout_index_options);
256-
git_config(git_default_config, NULL);
255+
repo_config(repo, git_default_config, NULL);
257256
prefix_length = prefix ? strlen(prefix) : 0;
258257

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;
261260

262-
if (repo_read_index(the_repository) < 0) {
261+
if (repo_read_index(repo) < 0) {
263262
die("invalid cache");
264263
}
265264

266265
argc = parse_options(argc, argv, prefix, builtin_checkout_index_options,
267266
builtin_checkout_index_usage, 0);
268-
state.istate = the_repository->index;
267+
state.istate = repo->index;
269268
state.force = force;
270269
state.quiet = quiet;
271270
state.not_new = not_new;
@@ -285,8 +284,8 @@ int cmd_checkout_index(int argc,
285284
*/
286285
if (index_opt && !state.base_dir_len && !to_tempfile) {
287286
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,
290289
LOCK_DIE_ON_ERROR);
291290
}
292291

@@ -304,7 +303,7 @@ int cmd_checkout_index(int argc,
304303
if (read_from_stdin)
305304
die("git checkout-index: don't mix '--stdin' and explicit filenames");
306305
p = prefix_path(prefix, prefix_length, arg);
307-
err |= checkout_file(p, prefix);
306+
err |= checkout_file(repo->index, p, prefix);
308307
free(p);
309308
}
310309

@@ -326,15 +325,15 @@ int cmd_checkout_index(int argc,
326325
strbuf_swap(&buf, &unquoted);
327326
}
328327
p = prefix_path(prefix, prefix_length, buf.buf);
329-
err |= checkout_file(p, prefix);
328+
err |= checkout_file(repo->index, p, prefix);
330329
free(p);
331330
}
332331
strbuf_release(&unquoted);
333332
strbuf_release(&buf);
334333
}
335334

336335
if (all)
337-
err |= checkout_all(prefix, prefix_length);
336+
err |= checkout_all(repo->index, prefix, prefix_length);
338337

339338
if (pc_workers > 1)
340339
err |= run_parallel_checkout(&state, pc_workers, pc_threshold,
@@ -344,7 +343,7 @@ int cmd_checkout_index(int argc,
344343
return 1;
345344

346345
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))
348347
die("Unable to write new index file");
349348
return 0;
350349
}

t/t2006-checkout-index-basic.sh

+7
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,13 @@ test_expect_success 'checkout-index -h in broken repository' '
2121
test_grep "[Uu]sage" broken/usage
2222
'
2323

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+
2431
test_expect_success 'checkout-index reports errors (cmdline)' '
2532
test_must_fail git checkout-index -- does-not-exist 2>stderr &&
2633
test_grep not.in.the.cache stderr

0 commit comments

Comments
 (0)