add: pass in repo variable instead of global the_repository

With the repository variable available in the builtin function as an argument, pass this down into helper functions instead of using the global the_repository. Signed-off-by: John Cai <johncai86@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

John Cai committed Sep 13, 2024 at 21:16 UTC 836474560b653271a819c4de9fa60cb097df2067
3 files changed +68 -55
builtin/add.c
+63 -53
@@ -3,7 +3,6 @@
3 *
4 * Copyright (C) 2006 Linus Torvalds
5 */
6 -#define USE_THE_REPOSITORY_VARIABLE
6 #include "builtin.h"
7 #include "advice.h"
8 #include "config.h"
@@ -35,24 +34,27 @@ static int pathspec_file_nul;
34 static int include_sparse;
35 static const char *pathspec_from_file;
36
38 -static int chmod_pathspec(struct pathspec *pathspec, char flip, int show_only)
37 +static int chmod_pathspec(struct repository *repo,
38 + struct pathspec *pathspec,
39 + char flip,
40 + int show_only)
41 {
42 int i, ret = 0;
43
42 - for (i = 0; i < the_repository->index->cache_nr; i++) {
43 - struct cache_entry *ce = the_repository->index->cache[i];
44 + for (i = 0; i < repo->index->cache_nr; i++) {
45 + struct cache_entry *ce = repo->index->cache[i];
46 int err;
47
48 if (!include_sparse &&
49 (ce_skip_worktree(ce) ||
48 - !path_in_sparse_checkout(ce->name, the_repository->index)))
50 + !path_in_sparse_checkout(ce->name, repo->index)))
51 continue;
52
51 - if (pathspec && !ce_path_match(the_repository->index, ce, pathspec, NULL))
53 + if (pathspec && !ce_path_match(repo->index, ce, pathspec, NULL))
54 continue;
55
56 if (!show_only)
55 - err = chmod_index_entry(the_repository->index, ce, flip);
57 + err = chmod_index_entry(repo->index, ce, flip);
58 else
59 err = S_ISREG(ce->ce_mode) ? 0 : -1;
60
@@ -63,31 +65,36 @@ static int chmod_pathspec(struct pathspec *pathspec, char flip, int show_only)
65 return ret;
66 }
67
66 -static int renormalize_tracked_files(const struct pathspec *pathspec, int flags)
68 +static int renormalize_tracked_files(struct repository *repo,
69 + const struct pathspec *pathspec,
70 + int flags)
71 {
72 int i, retval = 0;
73
70 - for (i = 0; i < the_repository->index->cache_nr; i++) {
71 - struct cache_entry *ce = the_repository->index->cache[i];
74 + for (i = 0; i < repo->index->cache_nr; i++) {
75 + struct cache_entry *ce = repo->index->cache[i];
76
77 if (!include_sparse &&
78 (ce_skip_worktree(ce) ||
75 - !path_in_sparse_checkout(ce->name, the_repository->index)))
79 + !path_in_sparse_checkout(ce->name, repo->index)))
80 continue;
81 if (ce_stage(ce))
82 continue; /* do not touch unmerged paths */
83 if (!S_ISREG(ce->ce_mode) && !S_ISLNK(ce->ce_mode))
84 continue; /* do not touch non blobs */
81 - if (pathspec && !ce_path_match(the_repository->index, ce, pathspec, NULL))
85 + if (pathspec && !ce_path_match(repo->index, ce, pathspec, NULL))
86 continue;
83 - retval |= add_file_to_index(the_repository->index, ce->name,
87 + retval |= add_file_to_index(repo->index, ce->name,
88 flags | ADD_CACHE_RENORMALIZE);
89 }
90
91 return retval;
92 }
93
90 -static char *prune_directory(struct dir_struct *dir, struct pathspec *pathspec, int prefix)
94 +static char *prune_directory(struct repository *repo,
95 + struct dir_struct *dir,
96 + struct pathspec *pathspec,
97 + int prefix)
98 {
99 char *seen;
100 int i;
@@ -99,16 +106,16 @@ static char *prune_directory(struct dir_struct *dir, struct pathspec *pathspec,
106 i = dir->nr;
107 while (--i >= 0) {
108 struct dir_entry *entry = *src++;
102 - if (dir_path_match(the_repository->index, entry, pathspec, prefix, seen))
109 + if (dir_path_match(repo->index, entry, pathspec, prefix, seen))
110 *dst++ = entry;
111 }
112 dir->nr = dst - dir->entries;
106 - add_pathspec_matches_against_index(pathspec, the_repository->index, seen,
113 + add_pathspec_matches_against_index(pathspec, repo->index, seen,
114 PS_IGNORE_SKIP_WORKTREE);
115 return seen;
116 }
117
111 -static int refresh(int verbose, const struct pathspec *pathspec)
118 +static int refresh(struct repository *repo, int verbose, const struct pathspec *pathspec)
119 {
120 char *seen;
121 int i, ret = 0;
@@ -118,14 +125,14 @@ static int refresh(int verbose, const struct pathspec *pathspec)
125 (verbose ? REFRESH_IN_PORCELAIN : REFRESH_QUIET);
126
127 seen = xcalloc(pathspec->nr, 1);
121 - refresh_index(the_repository->index, flags, pathspec, seen,
128 + refresh_index(repo->index, flags, pathspec, seen,
129 _("Unstaged changes after refreshing the index:"));
130 for (i = 0; i < pathspec->nr; i++) {
131 if (!seen[i]) {
132 const char *path = pathspec->items[i].original;
133
134 if (matches_skip_worktree(pathspec, i, &skip_worktree_seen) ||
128 - !path_in_sparse_checkout(path, the_repository->index)) {
135 + !path_in_sparse_checkout(path, repo->index)) {
136 string_list_append(&only_match_skip_worktree,
137 pathspec->items[i].original);
138 } else {
@@ -146,7 +153,10 @@ static int refresh(int verbose, const struct pathspec *pathspec)
153 return ret;
154 }
155
149 -int interactive_add(const char **argv, const char *prefix, int patch)
156 +int interactive_add(struct repository *repo,
157 + const char **argv,
158 + const char *prefix,
159 + int patch)
160 {
161 struct pathspec pathspec;
162 int ret;
@@ -158,31 +168,31 @@ int interactive_add(const char **argv, const char *prefix, int patch)
168 prefix, argv);
169
170 if (patch)
161 - ret = !!run_add_p(the_repository, ADD_P_ADD, NULL, &pathspec);
171 + ret = !!run_add_p(repo, ADD_P_ADD, NULL, &pathspec);
172 else
163 - ret = !!run_add_i(the_repository, &pathspec);
173 + ret = !!run_add_i(repo, &pathspec);
174
175 clear_pathspec(&pathspec);
176 return ret;
177 }
178
169 -static int edit_patch(int argc,
179 +static int edit_patch(struct repository *repo,
180 + int argc,
181 const char **argv,
171 - const char *prefix,
172 - struct repository *repo UNUSED)
182 + const char *prefix)
183 {
174 - char *file = git_pathdup("ADD_EDIT.patch");
184 + char *file = repo_git_path(repo, "ADD_EDIT.patch");
185 struct child_process child = CHILD_PROCESS_INIT;
186 struct rev_info rev;
187 int out;
188 struct stat st;
189
180 - git_config(git_diff_basic_config, NULL); /* no "diff" UI options */
190 + repo_config(repo, git_diff_basic_config, NULL);
191
182 - if (repo_read_index(the_repository) < 0)
192 + if (repo_read_index(repo) < 0)
193 die(_("could not read the index"));
194
185 - repo_init_revisions(the_repository, &rev, prefix);
195 + repo_init_revisions(repo, &rev, prefix);
196 rev.diffopt.context = 7;
197
198 argc = setup_revisions(argc, argv, &rev, NULL);
@@ -320,7 +330,7 @@ static void check_embedded_repo(const char *path)
330 strbuf_release(&name);
331 }
332
323 -static int add_files(struct dir_struct *dir, int flags)
333 +static int add_files(struct repository *repo, struct dir_struct *dir, int flags)
334 {
335 int i, exit_status = 0;
336 struct string_list matched_sparse_paths = STRING_LIST_INIT_NODUP;
@@ -336,12 +346,12 @@ static int add_files(struct dir_struct *dir, int flags)
346
347 for (i = 0; i < dir->nr; i++) {
348 if (!include_sparse &&
339 - !path_in_sparse_checkout(dir->entries[i]->name, the_repository->index)) {
349 + !path_in_sparse_checkout(dir->entries[i]->name, repo->index)) {
350 string_list_append(&matched_sparse_paths,
351 dir->entries[i]->name);
352 continue;
353 }
344 - if (add_file_to_index(the_repository->index, dir->entries[i]->name, flags)) {
354 + if (add_file_to_index(repo->index, dir->entries[i]->name, flags)) {
355 if (!ignore_add_errors)
356 die(_("adding files failed"));
357 exit_status = 1;
@@ -363,7 +373,7 @@ static int add_files(struct dir_struct *dir, int flags)
373 int cmd_add(int argc,
374 const char **argv,
375 const char *prefix,
366 - struct repository *repo UNUSED)
376 + struct repository *repo)
377 {
378 int exit_status = 0;
379 struct pathspec pathspec;
@@ -375,7 +385,7 @@ int cmd_add(int argc,
385 char *ps_matched = NULL;
386 struct lock_file lock_file = LOCK_INIT;
387
378 - git_config(add_config, NULL);
388 + repo_config(repo, add_config, NULL);
389
390 argc = parse_options(argc, argv, prefix, builtin_add_options,
391 builtin_add_usage, PARSE_OPT_KEEP_ARGV0);
@@ -386,13 +396,13 @@ int cmd_add(int argc,
396 die(_("options '%s' and '%s' cannot be used together"), "--dry-run", "--interactive/--patch");
397 if (pathspec_from_file)
398 die(_("options '%s' and '%s' cannot be used together"), "--pathspec-from-file", "--interactive/--patch");
389 - exit(interactive_add(argv + 1, prefix, patch_interactive));
399 + exit(interactive_add(repo, argv + 1, prefix, patch_interactive));
400 }
401
402 if (edit_interactive) {
403 if (pathspec_from_file)
404 die(_("options '%s' and '%s' cannot be used together"), "--pathspec-from-file", "--edit");
395 - return(edit_patch(argc, argv, prefix, the_repository));
405 + return(edit_patch(repo, argc, argv, prefix));
406 }
407 argc--;
408 argv++;
@@ -415,10 +425,10 @@ int cmd_add(int argc,
425 add_new_files = !take_worktree_changes && !refresh_only && !add_renormalize;
426 require_pathspec = !(take_worktree_changes || (0 < addremove_explicit));
427
418 - prepare_repo_settings(the_repository);
419 - the_repository->settings.command_requires_full_index = 0;
428 + prepare_repo_settings(repo);
429 + repo->settings.command_requires_full_index = 0;
430
421 - repo_hold_locked_index(the_repository, &lock_file, LOCK_DIE_ON_ERROR);
431 + repo_hold_locked_index(repo, &lock_file, LOCK_DIE_ON_ERROR);
432
433 /*
434 * Check the "pathspec '%s' did not match any files" block
@@ -459,11 +469,11 @@ int cmd_add(int argc,
469 (!(addremove || take_worktree_changes)
470 ? ADD_CACHE_IGNORE_REMOVAL : 0));
471
462 - if (repo_read_index_preload(the_repository, &pathspec, 0) < 0)
472 + if (repo_read_index_preload(repo, &pathspec, 0) < 0)
473 die(_("index file corrupt"));
474
465 - die_in_unpopulated_submodule(the_repository->index, prefix);
466 - die_path_inside_submodule(the_repository->index, &pathspec);
475 + die_in_unpopulated_submodule(repo->index, prefix);
476 + die_path_inside_submodule(repo->index, &pathspec);
477
478 if (add_new_files) {
479 int baselen;
@@ -475,13 +485,13 @@ int cmd_add(int argc,
485 }
486
487 /* This picks up the paths that are not tracked */
478 - baselen = fill_directory(&dir, the_repository->index, &pathspec);
488 + baselen = fill_directory(&dir, repo->index, &pathspec);
489 if (pathspec.nr)
480 - seen = prune_directory(&dir, &pathspec, baselen);
490 + seen = prune_directory(repo, &dir, &pathspec, baselen);
491 }
492
493 if (refresh_only) {
484 - exit_status |= refresh(verbose, &pathspec);
494 + exit_status |= refresh(repo, verbose, &pathspec);
495 goto finish;
496 }
497
@@ -492,7 +502,7 @@ int cmd_add(int argc,
502
503 if (!seen)
504 seen = find_pathspecs_matching_against_index(&pathspec,
495 - the_repository->index, PS_IGNORE_SKIP_WORKTREE);
505 + repo->index, PS_IGNORE_SKIP_WORKTREE);
506
507 /*
508 * file_exists() assumes exact match
@@ -528,8 +538,8 @@ int cmd_add(int argc,
538 !file_exists(path)) {
539 if (ignore_missing) {
540 int dtype = DT_UNKNOWN;
531 - if (is_excluded(&dir, the_repository->index, path, &dtype))
532 - dir_add_ignored(&dir, the_repository->index,
541 + if (is_excluded(&dir, repo->index, path, &dtype))
542 + dir_add_ignored(&dir, repo->index,
543 path, pathspec.items[i].len);
544 } else
545 die(_("pathspec '%s' did not match any files"),
@@ -552,9 +562,9 @@ int cmd_add(int argc,
562
563 ps_matched = xcalloc(pathspec.nr, 1);
564 if (add_renormalize)
555 - exit_status |= renormalize_tracked_files(&pathspec, flags);
565 + exit_status |= renormalize_tracked_files(repo, &pathspec, flags);
566 else
557 - exit_status |= add_files_to_cache(the_repository, prefix,
567 + exit_status |= add_files_to_cache(repo, prefix,
568 &pathspec, ps_matched,
569 include_sparse, flags);
570
@@ -563,14 +573,14 @@ int cmd_add(int argc,
573 exit(128);
574
575 if (add_new_files)
566 - exit_status |= add_files(&dir, flags);
576 + exit_status |= add_files(repo, &dir, flags);
577
578 if (chmod_arg && pathspec.nr)
569 - exit_status |= chmod_pathspec(&pathspec, chmod_arg[0], show_only);
579 + exit_status |= chmod_pathspec(repo, &pathspec, chmod_arg[0], show_only);
580 end_odb_transaction();
581
582 finish:
573 - if (write_locked_index(the_repository->index, &lock_file,
583 + if (write_locked_index(repo->index, &lock_file,
584 COMMIT_LOCK | SKIP_IF_UNCHANGED))
585 die(_("unable to write new index file"));
586
builtin/commit.c
+1 -1
@@ -395,7 +395,7 @@ static const char *prepare_index(const char **argv, const char *prefix,
395 old_index_env = xstrdup_or_null(getenv(INDEX_ENVIRONMENT));
396 setenv(INDEX_ENVIRONMENT, the_repository->index_file, 1);
397
398 - if (interactive_add(argv, prefix, patch_interactive) != 0)
398 + if (interactive_add(the_repository, argv, prefix, patch_interactive) != 0)
399 die(_("interactive add failed"));
400
401 the_repository->index_file = old_repo_index_file;
commit.h
+4 -1
@@ -253,7 +253,10 @@ struct oid_array;
253 struct ref;
254 int for_each_commit_graft(each_commit_graft_fn, void *);
255
256 -int interactive_add(const char **argv, const char *prefix, int patch);
256 +int interactive_add(struct repository *repo,
257 + const char **argv,
258 + const char *prefix,
259 + int patch);
260
261 struct commit_extra_header {
262 struct commit_extra_header *next;