Skip to content

Commit 1273738

Browse files
committed
Merge branch 'nd/struct-pathspec'
* nd/struct-pathspec: pathspec: rename per-item field has_wildcard to use_wildcard Improve tree_entry_interesting() handling code Convert read_tree{,_recursive} to support struct pathspec Reimplement read_tree_recursive() using tree_entry_interesting()
2 parents 9fdc1cc + 33e0f62 commit 1273738

File tree

15 files changed

+151
-172
lines changed

15 files changed

+151
-172
lines changed

archive.c

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,7 @@ int write_archive_entries(struct archiver_args *args,
157157
struct archiver_context context;
158158
struct unpack_trees_options opts;
159159
struct tree_desc t;
160+
struct pathspec pathspec;
160161
int err;
161162

162163
if (args->baselen > 0 && args->base[args->baselen - 1] == '/') {
@@ -191,8 +192,10 @@ int write_archive_entries(struct archiver_args *args,
191192
git_attr_set_direction(GIT_ATTR_INDEX, &the_index);
192193
}
193194

194-
err = read_tree_recursive(args->tree, "", 0, 0, args->pathspec,
195+
init_pathspec(&pathspec, args->pathspec);
196+
err = read_tree_recursive(args->tree, "", 0, 0, &pathspec,
195197
write_archive_entry, &context);
198+
free_pathspec(&pathspec);
196199
if (err == READ_TREE_RECURSIVE)
197200
err = 0;
198201
return err;
@@ -221,11 +224,14 @@ static int reject_entry(const unsigned char *sha1, const char *base,
221224

222225
static int path_exists(struct tree *tree, const char *path)
223226
{
224-
const char *pathspec[] = { path, NULL };
225-
226-
if (read_tree_recursive(tree, "", 0, 0, pathspec, reject_entry, NULL))
227-
return 1;
228-
return 0;
227+
const char *paths[] = { path, NULL };
228+
struct pathspec pathspec;
229+
int ret;
230+
231+
init_pathspec(&pathspec, paths);
232+
ret = read_tree_recursive(tree, "", 0, 0, &pathspec, reject_entry, NULL);
233+
free_pathspec(&pathspec);
234+
return ret != 0;
229235
}
230236

231237
static void parse_pathspec_arg(const char **pathspec,

builtin/checkout.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,10 @@ static int update_some(const unsigned char *sha1, const char *base, int baselen,
7979

8080
static int read_tree_some(struct tree *tree, const char **pathspec)
8181
{
82-
read_tree_recursive(tree, "", 0, 0, pathspec, update_some, NULL);
82+
struct pathspec ps;
83+
init_pathspec(&ps, pathspec);
84+
read_tree_recursive(tree, "", 0, 0, &ps, update_some, NULL);
85+
free_pathspec(&ps);
8386

8487
/* update the index with the given tree's info
8588
* for all args, expanding wildcards, and exit

builtin/grep.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -533,18 +533,18 @@ static int grep_cache(struct grep_opt *opt, const struct pathspec *pathspec, int
533533
static int grep_tree(struct grep_opt *opt, const struct pathspec *pathspec,
534534
struct tree_desc *tree, struct strbuf *base, int tn_len)
535535
{
536-
int hit = 0, matched = 0;
536+
int hit = 0, match = 0;
537537
struct name_entry entry;
538538
int old_baselen = base->len;
539539

540540
while (tree_entry(tree, &entry)) {
541541
int te_len = tree_entry_len(entry.path, entry.sha1);
542542

543-
if (matched != 2) {
544-
matched = tree_entry_interesting(&entry, base, tn_len, pathspec);
545-
if (matched == -1)
546-
break; /* no more matches */
547-
if (!matched)
543+
if (match != 2) {
544+
match = tree_entry_interesting(&entry, base, tn_len, pathspec);
545+
if (match < 0)
546+
break;
547+
if (match == 0)
548548
continue;
549549
}
550550

builtin/log.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -414,13 +414,15 @@ int cmd_show(int argc, const char **argv, const char *prefix)
414414
struct rev_info rev;
415415
struct object_array_entry *objects;
416416
struct setup_revision_opt opt;
417+
struct pathspec match_all;
417418
int i, count, ret = 0;
418419

419420
git_config(git_log_config, NULL);
420421

421422
if (diff_use_color_default == -1)
422423
diff_use_color_default = git_use_color_default;
423424

425+
init_pathspec(&match_all, NULL);
424426
init_revisions(&rev, prefix);
425427
rev.diff = 1;
426428
rev.always_show_header = 1;
@@ -467,7 +469,7 @@ int cmd_show(int argc, const char **argv, const char *prefix)
467469
diff_get_color_opt(&rev.diffopt, DIFF_COMMIT),
468470
name,
469471
diff_get_color_opt(&rev.diffopt, DIFF_RESET));
470-
read_tree_recursive((struct tree *)o, "", 0, 0, NULL,
472+
read_tree_recursive((struct tree *)o, "", 0, 0, &match_all,
471473
show_tree_object, NULL);
472474
rev.shown_one = 1;
473475
break;

builtin/ls-files.c

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -338,7 +338,7 @@ void overlay_tree_on_cache(const char *tree_name, const char *prefix)
338338
{
339339
struct tree *tree;
340340
unsigned char sha1[20];
341-
const char **match;
341+
struct pathspec pathspec;
342342
struct cache_entry *last_stage0 = NULL;
343343
int i;
344344

@@ -360,10 +360,11 @@ void overlay_tree_on_cache(const char *tree_name, const char *prefix)
360360
static const char *(matchbuf[2]);
361361
matchbuf[0] = prefix;
362362
matchbuf[1] = NULL;
363-
match = matchbuf;
363+
init_pathspec(&pathspec, matchbuf);
364+
pathspec.items[0].use_wildcard = 0;
364365
} else
365-
match = NULL;
366-
if (read_tree(tree, 1, match))
366+
init_pathspec(&pathspec, NULL);
367+
if (read_tree(tree, 1, &pathspec))
367368
die("unable to read tree entries %s", tree_name);
368369

369370
for (i = 0; i < active_nr; i++) {

builtin/ls-tree.c

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ static int line_termination = '\n';
1919
#define LS_SHOW_SIZE 16
2020
static int abbrev;
2121
static int ls_options;
22-
static const char **pathspec;
22+
static struct pathspec pathspec;
2323
static int chomp_prefix;
2424
static const char *ls_tree_prefix;
2525

@@ -35,7 +35,7 @@ static int show_recursive(const char *base, int baselen, const char *pathname)
3535
if (ls_options & LS_RECURSIVE)
3636
return 1;
3737

38-
s = pathspec;
38+
s = pathspec.raw;
3939
if (!s)
4040
return 0;
4141

@@ -120,7 +120,7 @@ int cmd_ls_tree(int argc, const char **argv, const char *prefix)
120120
{
121121
unsigned char sha1[20];
122122
struct tree *tree;
123-
int full_tree = 0;
123+
int i, full_tree = 0;
124124
const struct option ls_tree_options[] = {
125125
OPT_BIT('d', NULL, &ls_options, "only show trees",
126126
LS_TREE_ONLY),
@@ -166,11 +166,14 @@ int cmd_ls_tree(int argc, const char **argv, const char *prefix)
166166
if (get_sha1(argv[0], sha1))
167167
die("Not a valid object name %s", argv[0]);
168168

169-
pathspec = get_pathspec(prefix, argv + 1);
169+
init_pathspec(&pathspec, get_pathspec(prefix, argv + 1));
170+
for (i = 0; i < pathspec.nr; i++)
171+
pathspec.items[i].use_wildcard = 0;
172+
pathspec.has_wildcard = 0;
170173
tree = parse_tree_indirect(sha1);
171174
if (!tree)
172175
die("not a tree object");
173-
read_tree_recursive(tree, "", 0, 0, pathspec, show_tree, NULL);
176+
read_tree_recursive(tree, "", 0, 0, &pathspec, show_tree, NULL);
174177

175178
return 0;
176179
}

cache.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -511,7 +511,7 @@ struct pathspec {
511511
struct pathspec_item {
512512
const char *match;
513513
int len;
514-
unsigned int has_wildcard:1;
514+
unsigned int use_wildcard:1;
515515
} *items;
516516
};
517517

dir.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,7 @@ static int match_pathspec_item(const struct pathspec_item *item, int prefix,
230230
return MATCHED_RECURSIVELY;
231231
}
232232

233-
if (item->has_wildcard && !fnmatch(match, name, 0))
233+
if (item->use_wildcard && !fnmatch(match, name, 0))
234234
return MATCHED_FNMATCH;
235235

236236
return 0;
@@ -1274,8 +1274,8 @@ int init_pathspec(struct pathspec *pathspec, const char **paths)
12741274

12751275
item->match = path;
12761276
item->len = strlen(path);
1277-
item->has_wildcard = !no_wildcard(path);
1278-
if (item->has_wildcard)
1277+
item->use_wildcard = !no_wildcard(path);
1278+
if (item->use_wildcard)
12791279
pathspec->has_wildcard = 1;
12801280
}
12811281

list-objects.c

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ static void process_tree(struct rev_info *revs,
6868
struct tree_desc desc;
6969
struct name_entry entry;
7070
struct name_path me;
71-
int all_interesting = (revs->diffopt.pathspec.nr == 0);
71+
int match = revs->diffopt.pathspec.nr == 0 ? 2 : 0;
7272
int baselen = base->len;
7373

7474
if (!revs->tree_objects)
@@ -85,7 +85,7 @@ static void process_tree(struct rev_info *revs,
8585
me.elem = name;
8686
me.elem_len = strlen(name);
8787

88-
if (!all_interesting) {
88+
if (!match) {
8989
strbuf_addstr(base, name);
9090
if (base->len)
9191
strbuf_addch(base, '/');
@@ -94,17 +94,13 @@ static void process_tree(struct rev_info *revs,
9494
init_tree_desc(&desc, tree->buffer, tree->size);
9595

9696
while (tree_entry(&desc, &entry)) {
97-
if (!all_interesting) {
98-
int showit = tree_entry_interesting(&entry,
99-
base, 0,
100-
&revs->diffopt.pathspec);
101-
102-
if (showit < 0)
97+
if (match != 2) {
98+
match = tree_entry_interesting(&entry, base, 0,
99+
&revs->diffopt.pathspec);
100+
if (match < 0)
103101
break;
104-
else if (!showit)
102+
if (match == 0)
105103
continue;
106-
else if (showit == 2)
107-
all_interesting = 1;
108104
}
109105

110106
if (S_ISDIR(entry.mode))

merge-recursive.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,9 @@ static int save_files_dirs(const unsigned char *sha1,
273273
static int get_files_dirs(struct merge_options *o, struct tree *tree)
274274
{
275275
int n;
276-
if (read_tree_recursive(tree, "", 0, 0, NULL, save_files_dirs, o))
276+
struct pathspec match_all;
277+
init_pathspec(&match_all, NULL);
278+
if (read_tree_recursive(tree, "", 0, 0, &match_all, save_files_dirs, o))
277279
return 0;
278280
n = o->current_file_set.nr + o->current_directory_set.nr;
279281
return n;

0 commit comments

Comments
 (0)