Skip to content

Commit b8e318e

Browse files
committed
Merge branch 'jc/pass-repo-to-builtins'
The convention to calling into built-in command implementation has been updated to pass the repository, if known, together with the prefix value. * jc/pass-repo-to-builtins: add: pass in repo variable instead of global the_repository builtin: remove USE_THE_REPOSITORY for those without the_repository builtin: remove USE_THE_REPOSITORY_VARIABLE from builtin.h builtin: add a repository parameter for builtin functions
2 parents 0f41fd2 + 8364745 commit b8e318e

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

128 files changed

+903
-437
lines changed

builtin.h

Lines changed: 139 additions & 146 deletions
Large diffs are not rendered by default.

builtin/add.c

Lines changed: 67 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
*
44
* Copyright (C) 2006 Linus Torvalds
55
*/
6-
76
#include "builtin.h"
87
#include "advice.h"
98
#include "config.h"
@@ -18,7 +17,6 @@
1817
#include "preload-index.h"
1918
#include "diff.h"
2019
#include "read-cache.h"
21-
#include "repository.h"
2220
#include "revision.h"
2321
#include "bulk-checkin.h"
2422
#include "strvec.h"
@@ -36,24 +34,27 @@ static int pathspec_file_nul;
3634
static int include_sparse;
3735
static const char *pathspec_from_file;
3836

39-
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)
4041
{
4142
int i, ret = 0;
4243

43-
for (i = 0; i < the_repository->index->cache_nr; i++) {
44-
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];
4546
int err;
4647

4748
if (!include_sparse &&
4849
(ce_skip_worktree(ce) ||
49-
!path_in_sparse_checkout(ce->name, the_repository->index)))
50+
!path_in_sparse_checkout(ce->name, repo->index)))
5051
continue;
5152

52-
if (pathspec && !ce_path_match(the_repository->index, ce, pathspec, NULL))
53+
if (pathspec && !ce_path_match(repo->index, ce, pathspec, NULL))
5354
continue;
5455

5556
if (!show_only)
56-
err = chmod_index_entry(the_repository->index, ce, flip);
57+
err = chmod_index_entry(repo->index, ce, flip);
5758
else
5859
err = S_ISREG(ce->ce_mode) ? 0 : -1;
5960

@@ -64,31 +65,36 @@ static int chmod_pathspec(struct pathspec *pathspec, char flip, int show_only)
6465
return ret;
6566
}
6667

67-
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)
6871
{
6972
int i, retval = 0;
7073

71-
for (i = 0; i < the_repository->index->cache_nr; i++) {
72-
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];
7376

7477
if (!include_sparse &&
7578
(ce_skip_worktree(ce) ||
76-
!path_in_sparse_checkout(ce->name, the_repository->index)))
79+
!path_in_sparse_checkout(ce->name, repo->index)))
7780
continue;
7881
if (ce_stage(ce))
7982
continue; /* do not touch unmerged paths */
8083
if (!S_ISREG(ce->ce_mode) && !S_ISLNK(ce->ce_mode))
8184
continue; /* do not touch non blobs */
82-
if (pathspec && !ce_path_match(the_repository->index, ce, pathspec, NULL))
85+
if (pathspec && !ce_path_match(repo->index, ce, pathspec, NULL))
8386
continue;
84-
retval |= add_file_to_index(the_repository->index, ce->name,
87+
retval |= add_file_to_index(repo->index, ce->name,
8588
flags | ADD_CACHE_RENORMALIZE);
8689
}
8790

8891
return retval;
8992
}
9093

91-
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)
9298
{
9399
char *seen;
94100
int i;
@@ -100,16 +106,16 @@ static char *prune_directory(struct dir_struct *dir, struct pathspec *pathspec,
100106
i = dir->nr;
101107
while (--i >= 0) {
102108
struct dir_entry *entry = *src++;
103-
if (dir_path_match(the_repository->index, entry, pathspec, prefix, seen))
109+
if (dir_path_match(repo->index, entry, pathspec, prefix, seen))
104110
*dst++ = entry;
105111
}
106112
dir->nr = dst - dir->entries;
107-
add_pathspec_matches_against_index(pathspec, the_repository->index, seen,
113+
add_pathspec_matches_against_index(pathspec, repo->index, seen,
108114
PS_IGNORE_SKIP_WORKTREE);
109115
return seen;
110116
}
111117

112-
static int refresh(int verbose, const struct pathspec *pathspec)
118+
static int refresh(struct repository *repo, int verbose, const struct pathspec *pathspec)
113119
{
114120
char *seen;
115121
int i, ret = 0;
@@ -119,14 +125,14 @@ static int refresh(int verbose, const struct pathspec *pathspec)
119125
(verbose ? REFRESH_IN_PORCELAIN : REFRESH_QUIET);
120126

121127
seen = xcalloc(pathspec->nr, 1);
122-
refresh_index(the_repository->index, flags, pathspec, seen,
128+
refresh_index(repo->index, flags, pathspec, seen,
123129
_("Unstaged changes after refreshing the index:"));
124130
for (i = 0; i < pathspec->nr; i++) {
125131
if (!seen[i]) {
126132
const char *path = pathspec->items[i].original;
127133

128134
if (matches_skip_worktree(pathspec, i, &skip_worktree_seen) ||
129-
!path_in_sparse_checkout(path, the_repository->index)) {
135+
!path_in_sparse_checkout(path, repo->index)) {
130136
string_list_append(&only_match_skip_worktree,
131137
pathspec->items[i].original);
132138
} else {
@@ -147,7 +153,10 @@ static int refresh(int verbose, const struct pathspec *pathspec)
147153
return ret;
148154
}
149155

150-
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)
151160
{
152161
struct pathspec pathspec;
153162
int ret;
@@ -159,28 +168,31 @@ int interactive_add(const char **argv, const char *prefix, int patch)
159168
prefix, argv);
160169

161170
if (patch)
162-
ret = !!run_add_p(the_repository, ADD_P_ADD, NULL, &pathspec);
171+
ret = !!run_add_p(repo, ADD_P_ADD, NULL, &pathspec);
163172
else
164-
ret = !!run_add_i(the_repository, &pathspec);
173+
ret = !!run_add_i(repo, &pathspec);
165174

166175
clear_pathspec(&pathspec);
167176
return ret;
168177
}
169178

170-
static int edit_patch(int argc, const char **argv, const char *prefix)
179+
static int edit_patch(struct repository *repo,
180+
int argc,
181+
const char **argv,
182+
const char *prefix)
171183
{
172-
char *file = git_pathdup("ADD_EDIT.patch");
184+
char *file = repo_git_path(repo, "ADD_EDIT.patch");
173185
struct child_process child = CHILD_PROCESS_INIT;
174186
struct rev_info rev;
175187
int out;
176188
struct stat st;
177189

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

180-
if (repo_read_index(the_repository) < 0)
192+
if (repo_read_index(repo) < 0)
181193
die(_("could not read the index"));
182194

183-
repo_init_revisions(the_repository, &rev, prefix);
195+
repo_init_revisions(repo, &rev, prefix);
184196
rev.diffopt.context = 7;
185197

186198
argc = setup_revisions(argc, argv, &rev, NULL);
@@ -318,7 +330,7 @@ static void check_embedded_repo(const char *path)
318330
strbuf_release(&name);
319331
}
320332

321-
static int add_files(struct dir_struct *dir, int flags)
333+
static int add_files(struct repository *repo, struct dir_struct *dir, int flags)
322334
{
323335
int i, exit_status = 0;
324336
struct string_list matched_sparse_paths = STRING_LIST_INIT_NODUP;
@@ -334,12 +346,12 @@ static int add_files(struct dir_struct *dir, int flags)
334346

335347
for (i = 0; i < dir->nr; i++) {
336348
if (!include_sparse &&
337-
!path_in_sparse_checkout(dir->entries[i]->name, the_repository->index)) {
349+
!path_in_sparse_checkout(dir->entries[i]->name, repo->index)) {
338350
string_list_append(&matched_sparse_paths,
339351
dir->entries[i]->name);
340352
continue;
341353
}
342-
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)) {
343355
if (!ignore_add_errors)
344356
die(_("adding files failed"));
345357
exit_status = 1;
@@ -358,7 +370,10 @@ static int add_files(struct dir_struct *dir, int flags)
358370
return exit_status;
359371
}
360372

361-
int cmd_add(int argc, const char **argv, const char *prefix)
373+
int cmd_add(int argc,
374+
const char **argv,
375+
const char *prefix,
376+
struct repository *repo)
362377
{
363378
int exit_status = 0;
364379
struct pathspec pathspec;
@@ -370,7 +385,7 @@ int cmd_add(int argc, const char **argv, const char *prefix)
370385
char *ps_matched = NULL;
371386
struct lock_file lock_file = LOCK_INIT;
372387

373-
git_config(add_config, NULL);
388+
repo_config(repo, add_config, NULL);
374389

375390
argc = parse_options(argc, argv, prefix, builtin_add_options,
376391
builtin_add_usage, PARSE_OPT_KEEP_ARGV0);
@@ -381,13 +396,13 @@ int cmd_add(int argc, const char **argv, const char *prefix)
381396
die(_("options '%s' and '%s' cannot be used together"), "--dry-run", "--interactive/--patch");
382397
if (pathspec_from_file)
383398
die(_("options '%s' and '%s' cannot be used together"), "--pathspec-from-file", "--interactive/--patch");
384-
exit(interactive_add(argv + 1, prefix, patch_interactive));
399+
exit(interactive_add(repo, argv + 1, prefix, patch_interactive));
385400
}
386401

387402
if (edit_interactive) {
388403
if (pathspec_from_file)
389404
die(_("options '%s' and '%s' cannot be used together"), "--pathspec-from-file", "--edit");
390-
return(edit_patch(argc, argv, prefix));
405+
return(edit_patch(repo, argc, argv, prefix));
391406
}
392407
argc--;
393408
argv++;
@@ -410,10 +425,10 @@ int cmd_add(int argc, const char **argv, const char *prefix)
410425
add_new_files = !take_worktree_changes && !refresh_only && !add_renormalize;
411426
require_pathspec = !(take_worktree_changes || (0 < addremove_explicit));
412427

413-
prepare_repo_settings(the_repository);
414-
the_repository->settings.command_requires_full_index = 0;
428+
prepare_repo_settings(repo);
429+
repo->settings.command_requires_full_index = 0;
415430

416-
repo_hold_locked_index(the_repository, &lock_file, LOCK_DIE_ON_ERROR);
431+
repo_hold_locked_index(repo, &lock_file, LOCK_DIE_ON_ERROR);
417432

418433
/*
419434
* Check the "pathspec '%s' did not match any files" block
@@ -454,11 +469,11 @@ int cmd_add(int argc, const char **argv, const char *prefix)
454469
(!(addremove || take_worktree_changes)
455470
? ADD_CACHE_IGNORE_REMOVAL : 0));
456471

457-
if (repo_read_index_preload(the_repository, &pathspec, 0) < 0)
472+
if (repo_read_index_preload(repo, &pathspec, 0) < 0)
458473
die(_("index file corrupt"));
459474

460-
die_in_unpopulated_submodule(the_repository->index, prefix);
461-
die_path_inside_submodule(the_repository->index, &pathspec);
475+
die_in_unpopulated_submodule(repo->index, prefix);
476+
die_path_inside_submodule(repo->index, &pathspec);
462477

463478
if (add_new_files) {
464479
int baselen;
@@ -470,13 +485,13 @@ int cmd_add(int argc, const char **argv, const char *prefix)
470485
}
471486

472487
/* This picks up the paths that are not tracked */
473-
baselen = fill_directory(&dir, the_repository->index, &pathspec);
488+
baselen = fill_directory(&dir, repo->index, &pathspec);
474489
if (pathspec.nr)
475-
seen = prune_directory(&dir, &pathspec, baselen);
490+
seen = prune_directory(repo, &dir, &pathspec, baselen);
476491
}
477492

478493
if (refresh_only) {
479-
exit_status |= refresh(verbose, &pathspec);
494+
exit_status |= refresh(repo, verbose, &pathspec);
480495
goto finish;
481496
}
482497

@@ -487,7 +502,7 @@ int cmd_add(int argc, const char **argv, const char *prefix)
487502

488503
if (!seen)
489504
seen = find_pathspecs_matching_against_index(&pathspec,
490-
the_repository->index, PS_IGNORE_SKIP_WORKTREE);
505+
repo->index, PS_IGNORE_SKIP_WORKTREE);
491506

492507
/*
493508
* file_exists() assumes exact match
@@ -523,8 +538,8 @@ int cmd_add(int argc, const char **argv, const char *prefix)
523538
!file_exists(path)) {
524539
if (ignore_missing) {
525540
int dtype = DT_UNKNOWN;
526-
if (is_excluded(&dir, the_repository->index, path, &dtype))
527-
dir_add_ignored(&dir, the_repository->index,
541+
if (is_excluded(&dir, repo->index, path, &dtype))
542+
dir_add_ignored(&dir, repo->index,
528543
path, pathspec.items[i].len);
529544
} else
530545
die(_("pathspec '%s' did not match any files"),
@@ -547,9 +562,9 @@ int cmd_add(int argc, const char **argv, const char *prefix)
547562

548563
ps_matched = xcalloc(pathspec.nr, 1);
549564
if (add_renormalize)
550-
exit_status |= renormalize_tracked_files(&pathspec, flags);
565+
exit_status |= renormalize_tracked_files(repo, &pathspec, flags);
551566
else
552-
exit_status |= add_files_to_cache(the_repository, prefix,
567+
exit_status |= add_files_to_cache(repo, prefix,
553568
&pathspec, ps_matched,
554569
include_sparse, flags);
555570

@@ -558,14 +573,14 @@ int cmd_add(int argc, const char **argv, const char *prefix)
558573
exit(128);
559574

560575
if (add_new_files)
561-
exit_status |= add_files(&dir, flags);
576+
exit_status |= add_files(repo, &dir, flags);
562577

563578
if (chmod_arg && pathspec.nr)
564-
exit_status |= chmod_pathspec(&pathspec, chmod_arg[0], show_only);
579+
exit_status |= chmod_pathspec(repo, &pathspec, chmod_arg[0], show_only);
565580
end_odb_transaction();
566581

567582
finish:
568-
if (write_locked_index(the_repository->index, &lock_file,
583+
if (write_locked_index(repo->index, &lock_file,
569584
COMMIT_LOCK | SKIP_IF_UNCHANGED))
570585
die(_("unable to write new index file"));
571586

builtin/am.c

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
* Based on git-am.sh by Junio C Hamano.
55
*/
66

7+
#define USE_THE_REPOSITORY_VARIABLE
78
#include "builtin.h"
89
#include "abspath.h"
910
#include "advice.h"
@@ -38,7 +39,6 @@
3839
#include "string-list.h"
3940
#include "pager.h"
4041
#include "path.h"
41-
#include "repository.h"
4242
#include "pretty.h"
4343

4444
/**
@@ -2303,7 +2303,10 @@ static int parse_opt_show_current_patch(const struct option *opt, const char *ar
23032303
return 0;
23042304
}
23052305

2306-
int cmd_am(int argc, const char **argv, const char *prefix)
2306+
int cmd_am(int argc,
2307+
const char **argv,
2308+
const char *prefix,
2309+
struct repository *repo UNUSED)
23072310
{
23082311
struct am_state state;
23092312
int binary = -1;

builtin/annotate.c

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,16 @@
33
*
44
* Copyright (C) 2006 Ryan Anderson
55
*/
6+
7+
#define USE_THE_REPOSITORY_VARIABLE
68
#include "git-compat-util.h"
79
#include "builtin.h"
810
#include "strvec.h"
911

10-
int cmd_annotate(int argc, const char **argv, const char *prefix)
12+
int cmd_annotate(int argc,
13+
const char **argv,
14+
const char *prefix,
15+
struct repository *repo UNUSED)
1116
{
1217
struct strvec args = STRVEC_INIT;
1318
int i;
@@ -18,5 +23,5 @@ int cmd_annotate(int argc, const char **argv, const char *prefix)
1823
strvec_push(&args, argv[i]);
1924
}
2025

21-
return cmd_blame(args.nr, args.v, prefix);
26+
return cmd_blame(args.nr, args.v, prefix, the_repository);
2227
}

0 commit comments

Comments
 (0)