Skip to content

Commit 8364745

Browse files
john-caigitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 49d2434 commit 8364745

File tree

3 files changed

+68
-55
lines changed

3 files changed

+68
-55
lines changed

builtin/add.c

Lines changed: 63 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
*
44
* Copyright (C) 2006 Linus Torvalds
55
*/
6-
#define USE_THE_REPOSITORY_VARIABLE
76
#include "builtin.h"
87
#include "advice.h"
98
#include "config.h"
@@ -35,24 +34,27 @@ static int pathspec_file_nul;
3534
static int include_sparse;
3635
static const char *pathspec_from_file;
3736

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)
3941
{
4042
int i, ret = 0;
4143

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];
4446
int err;
4547

4648
if (!include_sparse &&
4749
(ce_skip_worktree(ce) ||
48-
!path_in_sparse_checkout(ce->name, the_repository->index)))
50+
!path_in_sparse_checkout(ce->name, repo->index)))
4951
continue;
5052

51-
if (pathspec && !ce_path_match(the_repository->index, ce, pathspec, NULL))
53+
if (pathspec && !ce_path_match(repo->index, ce, pathspec, NULL))
5254
continue;
5355

5456
if (!show_only)
55-
err = chmod_index_entry(the_repository->index, ce, flip);
57+
err = chmod_index_entry(repo->index, ce, flip);
5658
else
5759
err = S_ISREG(ce->ce_mode) ? 0 : -1;
5860

@@ -63,31 +65,36 @@ static int chmod_pathspec(struct pathspec *pathspec, char flip, int show_only)
6365
return ret;
6466
}
6567

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)
6771
{
6872
int i, retval = 0;
6973

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

7377
if (!include_sparse &&
7478
(ce_skip_worktree(ce) ||
75-
!path_in_sparse_checkout(ce->name, the_repository->index)))
79+
!path_in_sparse_checkout(ce->name, repo->index)))
7680
continue;
7781
if (ce_stage(ce))
7882
continue; /* do not touch unmerged paths */
7983
if (!S_ISREG(ce->ce_mode) && !S_ISLNK(ce->ce_mode))
8084
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))
8286
continue;
83-
retval |= add_file_to_index(the_repository->index, ce->name,
87+
retval |= add_file_to_index(repo->index, ce->name,
8488
flags | ADD_CACHE_RENORMALIZE);
8589
}
8690

8791
return retval;
8892
}
8993

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)
9198
{
9299
char *seen;
93100
int i;
@@ -99,16 +106,16 @@ static char *prune_directory(struct dir_struct *dir, struct pathspec *pathspec,
99106
i = dir->nr;
100107
while (--i >= 0) {
101108
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))
103110
*dst++ = entry;
104111
}
105112
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,
107114
PS_IGNORE_SKIP_WORKTREE);
108115
return seen;
109116
}
110117

111-
static int refresh(int verbose, const struct pathspec *pathspec)
118+
static int refresh(struct repository *repo, int verbose, const struct pathspec *pathspec)
112119
{
113120
char *seen;
114121
int i, ret = 0;
@@ -118,14 +125,14 @@ static int refresh(int verbose, const struct pathspec *pathspec)
118125
(verbose ? REFRESH_IN_PORCELAIN : REFRESH_QUIET);
119126

120127
seen = xcalloc(pathspec->nr, 1);
121-
refresh_index(the_repository->index, flags, pathspec, seen,
128+
refresh_index(repo->index, flags, pathspec, seen,
122129
_("Unstaged changes after refreshing the index:"));
123130
for (i = 0; i < pathspec->nr; i++) {
124131
if (!seen[i]) {
125132
const char *path = pathspec->items[i].original;
126133

127134
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)) {
129136
string_list_append(&only_match_skip_worktree,
130137
pathspec->items[i].original);
131138
} else {
@@ -146,7 +153,10 @@ static int refresh(int verbose, const struct pathspec *pathspec)
146153
return ret;
147154
}
148155

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)
150160
{
151161
struct pathspec pathspec;
152162
int ret;
@@ -158,31 +168,31 @@ int interactive_add(const char **argv, const char *prefix, int patch)
158168
prefix, argv);
159169

160170
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);
162172
else
163-
ret = !!run_add_i(the_repository, &pathspec);
173+
ret = !!run_add_i(repo, &pathspec);
164174

165175
clear_pathspec(&pathspec);
166176
return ret;
167177
}
168178

169-
static int edit_patch(int argc,
179+
static int edit_patch(struct repository *repo,
180+
int argc,
170181
const char **argv,
171-
const char *prefix,
172-
struct repository *repo UNUSED)
182+
const char *prefix)
173183
{
174-
char *file = git_pathdup("ADD_EDIT.patch");
184+
char *file = repo_git_path(repo, "ADD_EDIT.patch");
175185
struct child_process child = CHILD_PROCESS_INIT;
176186
struct rev_info rev;
177187
int out;
178188
struct stat st;
179189

180-
git_config(git_diff_basic_config, NULL); /* no "diff" UI options */
190+
repo_config(repo, git_diff_basic_config, NULL);
181191

182-
if (repo_read_index(the_repository) < 0)
192+
if (repo_read_index(repo) < 0)
183193
die(_("could not read the index"));
184194

185-
repo_init_revisions(the_repository, &rev, prefix);
195+
repo_init_revisions(repo, &rev, prefix);
186196
rev.diffopt.context = 7;
187197

188198
argc = setup_revisions(argc, argv, &rev, NULL);
@@ -320,7 +330,7 @@ static void check_embedded_repo(const char *path)
320330
strbuf_release(&name);
321331
}
322332

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)
324334
{
325335
int i, exit_status = 0;
326336
struct string_list matched_sparse_paths = STRING_LIST_INIT_NODUP;
@@ -336,12 +346,12 @@ static int add_files(struct dir_struct *dir, int flags)
336346

337347
for (i = 0; i < dir->nr; i++) {
338348
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)) {
340350
string_list_append(&matched_sparse_paths,
341351
dir->entries[i]->name);
342352
continue;
343353
}
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)) {
345355
if (!ignore_add_errors)
346356
die(_("adding files failed"));
347357
exit_status = 1;
@@ -363,7 +373,7 @@ static int add_files(struct dir_struct *dir, int flags)
363373
int cmd_add(int argc,
364374
const char **argv,
365375
const char *prefix,
366-
struct repository *repo UNUSED)
376+
struct repository *repo)
367377
{
368378
int exit_status = 0;
369379
struct pathspec pathspec;
@@ -375,7 +385,7 @@ int cmd_add(int argc,
375385
char *ps_matched = NULL;
376386
struct lock_file lock_file = LOCK_INIT;
377387

378-
git_config(add_config, NULL);
388+
repo_config(repo, add_config, NULL);
379389

380390
argc = parse_options(argc, argv, prefix, builtin_add_options,
381391
builtin_add_usage, PARSE_OPT_KEEP_ARGV0);
@@ -386,13 +396,13 @@ int cmd_add(int argc,
386396
die(_("options '%s' and '%s' cannot be used together"), "--dry-run", "--interactive/--patch");
387397
if (pathspec_from_file)
388398
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));
390400
}
391401

392402
if (edit_interactive) {
393403
if (pathspec_from_file)
394404
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));
396406
}
397407
argc--;
398408
argv++;
@@ -415,10 +425,10 @@ int cmd_add(int argc,
415425
add_new_files = !take_worktree_changes && !refresh_only && !add_renormalize;
416426
require_pathspec = !(take_worktree_changes || (0 < addremove_explicit));
417427

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

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);
422432

423433
/*
424434
* Check the "pathspec '%s' did not match any files" block
@@ -459,11 +469,11 @@ int cmd_add(int argc,
459469
(!(addremove || take_worktree_changes)
460470
? ADD_CACHE_IGNORE_REMOVAL : 0));
461471

462-
if (repo_read_index_preload(the_repository, &pathspec, 0) < 0)
472+
if (repo_read_index_preload(repo, &pathspec, 0) < 0)
463473
die(_("index file corrupt"));
464474

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);
467477

468478
if (add_new_files) {
469479
int baselen;
@@ -475,13 +485,13 @@ int cmd_add(int argc,
475485
}
476486

477487
/* 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);
479489
if (pathspec.nr)
480-
seen = prune_directory(&dir, &pathspec, baselen);
490+
seen = prune_directory(repo, &dir, &pathspec, baselen);
481491
}
482492

483493
if (refresh_only) {
484-
exit_status |= refresh(verbose, &pathspec);
494+
exit_status |= refresh(repo, verbose, &pathspec);
485495
goto finish;
486496
}
487497

@@ -492,7 +502,7 @@ int cmd_add(int argc,
492502

493503
if (!seen)
494504
seen = find_pathspecs_matching_against_index(&pathspec,
495-
the_repository->index, PS_IGNORE_SKIP_WORKTREE);
505+
repo->index, PS_IGNORE_SKIP_WORKTREE);
496506

497507
/*
498508
* file_exists() assumes exact match
@@ -528,8 +538,8 @@ int cmd_add(int argc,
528538
!file_exists(path)) {
529539
if (ignore_missing) {
530540
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,
533543
path, pathspec.items[i].len);
534544
} else
535545
die(_("pathspec '%s' did not match any files"),
@@ -552,9 +562,9 @@ int cmd_add(int argc,
552562

553563
ps_matched = xcalloc(pathspec.nr, 1);
554564
if (add_renormalize)
555-
exit_status |= renormalize_tracked_files(&pathspec, flags);
565+
exit_status |= renormalize_tracked_files(repo, &pathspec, flags);
556566
else
557-
exit_status |= add_files_to_cache(the_repository, prefix,
567+
exit_status |= add_files_to_cache(repo, prefix,
558568
&pathspec, ps_matched,
559569
include_sparse, flags);
560570

@@ -563,14 +573,14 @@ int cmd_add(int argc,
563573
exit(128);
564574

565575
if (add_new_files)
566-
exit_status |= add_files(&dir, flags);
576+
exit_status |= add_files(repo, &dir, flags);
567577

568578
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);
570580
end_odb_transaction();
571581

572582
finish:
573-
if (write_locked_index(the_repository->index, &lock_file,
583+
if (write_locked_index(repo->index, &lock_file,
574584
COMMIT_LOCK | SKIP_IF_UNCHANGED))
575585
die(_("unable to write new index file"));
576586

builtin/commit.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -395,7 +395,7 @@ static const char *prepare_index(const char **argv, const char *prefix,
395395
old_index_env = xstrdup_or_null(getenv(INDEX_ENVIRONMENT));
396396
setenv(INDEX_ENVIRONMENT, the_repository->index_file, 1);
397397

398-
if (interactive_add(argv, prefix, patch_interactive) != 0)
398+
if (interactive_add(the_repository, argv, prefix, patch_interactive) != 0)
399399
die(_("interactive add failed"));
400400

401401
the_repository->index_file = old_repo_index_file;

commit.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,10 @@ struct oid_array;
253253
struct ref;
254254
int for_each_commit_graft(each_commit_graft_fn, void *);
255255

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);
257260

258261
struct commit_extra_header {
259262
struct commit_extra_header *next;

0 commit comments

Comments
 (0)