Git fork

builtin/ls-files: stop using `the_repository`

Remove the_repository global variable in favor of the repository
argument that gets passed in "builtin/ls-files.c".

When `-h` is passed to the command outside a Git repository, the
`run_builtin()` will call the `cmd_ls_files()` 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 the repository available in the calling context to both
`expand_objectsize()` and `show_ru_info()` 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>

authored by

Usman Akinyemi and committed by
Junio C Hamano
d9c5cfb1 72fe8bfa

+23 -16
+16 -16
builtin/ls-files.c
··· 6 6 * Copyright (C) Linus Torvalds, 2005 7 7 */ 8 8 9 - #define USE_THE_REPOSITORY_VARIABLE 10 9 #define DISABLE_SIGN_COMPARE_WARNINGS 11 10 12 11 #include "builtin.h" ··· 245 244 repo_clear(&subrepo); 246 245 } 247 246 248 - static void expand_objectsize(struct strbuf *line, const struct object_id *oid, 247 + static void expand_objectsize(struct repository *repo, struct strbuf *line, 248 + const struct object_id *oid, 249 249 const enum object_type type, unsigned int padded) 250 250 { 251 251 if (type == OBJ_BLOB) { 252 252 unsigned long size; 253 - if (oid_object_info(the_repository, oid, &size) < 0) 253 + if (oid_object_info(repo, oid, &size) < 0) 254 254 die(_("could not get object info about '%s'"), 255 255 oid_to_hex(oid)); 256 256 if (padded) ··· 283 283 else if (skip_prefix(format, "(objecttype)", &format)) 284 284 strbuf_addstr(&sb, type_name(object_type(ce->ce_mode))); 285 285 else if (skip_prefix(format, "(objectsize:padded)", &format)) 286 - expand_objectsize(&sb, &ce->oid, 286 + expand_objectsize(repo, &sb, &ce->oid, 287 287 object_type(ce->ce_mode), 1); 288 288 else if (skip_prefix(format, "(objectsize)", &format)) 289 - expand_objectsize(&sb, &ce->oid, 289 + expand_objectsize(repo, &sb, &ce->oid, 290 290 object_type(ce->ce_mode), 0); 291 291 else if (skip_prefix(format, "(stage)", &format)) 292 292 strbuf_addf(&sb, "%d", ce_stage(ce)); ··· 348 348 } 349 349 } 350 350 351 - static void show_ru_info(struct index_state *istate) 351 + static void show_ru_info(struct repository *repo, struct index_state *istate) 352 352 { 353 353 struct string_list_item *item; 354 354 ··· 370 370 if (!ui->mode[i]) 371 371 continue; 372 372 printf("%s%06o %s %d\t", tag_resolve_undo, ui->mode[i], 373 - repo_find_unique_abbrev(the_repository, &ui->oid[i], abbrev), 373 + repo_find_unique_abbrev(repo, &ui->oid[i], abbrev), 374 374 i + 1); 375 375 write_name(path); 376 376 } ··· 567 567 int cmd_ls_files(int argc, 568 568 const char **argv, 569 569 const char *cmd_prefix, 570 - struct repository *repo UNUSED) 570 + struct repository *repo) 571 571 { 572 572 int require_work_tree = 0, show_tag = 0, i; 573 573 char *max_prefix; ··· 647 647 show_usage_with_options_if_asked(argc, argv, 648 648 ls_files_usage, builtin_ls_files_options); 649 649 650 - prepare_repo_settings(the_repository); 651 - the_repository->settings.command_requires_full_index = 0; 650 + prepare_repo_settings(repo); 651 + repo->settings.command_requires_full_index = 0; 652 652 653 653 prefix = cmd_prefix; 654 654 if (prefix) 655 655 prefix_len = strlen(prefix); 656 - git_config(git_default_config, NULL); 656 + repo_config(repo, git_default_config, NULL); 657 657 658 - if (repo_read_index(the_repository) < 0) 658 + if (repo_read_index(repo) < 0) 659 659 die("index file corrupt"); 660 660 661 661 argc = parse_options(argc, argv, prefix, builtin_ls_files_options, ··· 724 724 max_prefix = common_prefix(&pathspec); 725 725 max_prefix_len = get_common_prefix_len(max_prefix); 726 726 727 - prune_index(the_repository->index, max_prefix, max_prefix_len); 727 + prune_index(repo->index, max_prefix, max_prefix_len); 728 728 729 729 /* Treat unmatching pathspec elements as errors */ 730 730 if (pathspec.nr && error_unmatch) ··· 748 748 */ 749 749 if (show_stage || show_unmerged) 750 750 die(_("options '%s' and '%s' cannot be used together"), "ls-files --with-tree", "-s/-u"); 751 - overlay_tree_on_index(the_repository->index, with_tree, max_prefix); 751 + overlay_tree_on_index(repo->index, with_tree, max_prefix); 752 752 } 753 753 754 - show_files(the_repository, &dir); 754 + show_files(repo, &dir); 755 755 756 756 if (show_resolve_undo) 757 - show_ru_info(the_repository->index); 757 + show_ru_info(repo, repo->index); 758 758 759 759 if (ps_matched && report_path_error(ps_matched, &pathspec)) { 760 760 fprintf(stderr, "Did you forget to 'git add'?\n");
+7
t/t3004-ls-files-basic.sh
··· 34 34 test_grep "[Uu]sage: git ls-files " broken/usage 35 35 ' 36 36 37 + test_expect_success 'ls-files does not crash with -h' ' 38 + test_expect_code 129 git ls-files -h >usage && 39 + test_grep "[Uu]sage: git ls-files " usage && 40 + test_expect_code 129 nongit git ls-files -h >usage && 41 + test_grep "[Uu]sage: git ls-files " usage 42 + ' 43 + 37 44 test_expect_success SYMLINKS 'ls-files with absolute paths to symlinks' ' 38 45 mkdir subs && 39 46 ln -s nosuch link &&