Convert read_tree{,_recursive} to support struct pathspec
This patch changes behavior of the two functions. Previously it does prefix matching only. Now it can also do wildcard matching. All callers are updated. Some gain wildcard matching (archive, checkout), others reset pathspec_item.has_wildcard to retain old behavior (ls-files, ls-tree as they are plumbing). Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Nguyễn Thái Ngọc Duy committed
Mar 25, 2011 at 16:34 UTC
f0096c06bcdeb7aa6ae8a749ddc9d6d4a2c381d1
9 files changed
+62
-29
archive.c
+12
-6
@@ -157,6 +157,7 @@ int write_archive_entries(struct archiver_args *args,
157
struct archiver_context context;
158
struct unpack_trees_options opts;
159
struct tree_desc t;
160
+ struct pathspec pathspec;
161
int err;
162
163
if (args->baselen > 0 && args->base[args->baselen - 1] == '/') {
@@ -191,8 +192,10 @@ int write_archive_entries(struct archiver_args *args,
192
git_attr_set_direction(GIT_ATTR_INDEX, &the_index);
193
}
194
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,
197
write_archive_entry, &context);
198
+ free_pathspec(&pathspec);
199
if (err == READ_TREE_RECURSIVE)
200
err = 0;
201
return err;
@@ -221,11 +224,14 @@ static int reject_entry(const unsigned char *sha1, const char *base,
224
225
static int path_exists(struct tree *tree, const char *path)
226
{
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;
235
}
236
237
static void parse_pathspec_arg(const char **pathspec,
builtin/checkout.c
+4
-1
@@ -78,7 +78,10 @@ static int update_some(const unsigned char *sha1, const char *base, int baselen,
78
79
static int read_tree_some(struct tree *tree, const char **pathspec)
80
{
81
- read_tree_recursive(tree, "", 0, 0, pathspec, update_some, NULL);
81
+ struct pathspec ps;
82
+ init_pathspec(&ps, pathspec);
83
+ read_tree_recursive(tree, "", 0, 0, &ps, update_some, NULL);
84
+ free_pathspec(&ps);
85
86
/* update the index with the given tree's info
87
* for all args, expanding wildcards, and exit
builtin/log.c
+3
-1
@@ -390,6 +390,7 @@ int cmd_show(int argc, const char **argv, const char *prefix)
390
struct rev_info rev;
391
struct object_array_entry *objects;
392
struct setup_revision_opt opt;
393
+ struct pathspec match_all;
394
int i, count, ret = 0;
395
396
git_config(git_log_config, NULL);
@@ -397,6 +398,7 @@ int cmd_show(int argc, const char **argv, const char *prefix)
398
if (diff_use_color_default == -1)
399
diff_use_color_default = git_use_color_default;
400
401
+ init_pathspec(&match_all, NULL);
402
init_revisions(&rev, prefix);
403
rev.diff = 1;
404
rev.always_show_header = 1;
@@ -443,7 +445,7 @@ int cmd_show(int argc, const char **argv, const char *prefix)
445
diff_get_color_opt(&rev.diffopt, DIFF_COMMIT),
446
name,
447
diff_get_color_opt(&rev.diffopt, DIFF_RESET));
446
- read_tree_recursive((struct tree *)o, "", 0, 0, NULL,
448
+ read_tree_recursive((struct tree *)o, "", 0, 0, &match_all,
449
show_tree_object, NULL);
450
rev.shown_one = 1;
451
break;
builtin/ls-files.c
+5
-4
@@ -338,7 +338,7 @@ void overlay_tree_on_cache(const char *tree_name, const char *prefix)
338
{
339
struct tree *tree;
340
unsigned char sha1[20];
341
- const char **match;
341
+ struct pathspec pathspec;
342
struct cache_entry *last_stage0 = NULL;
343
int i;
344
@@ -360,10 +360,11 @@ void overlay_tree_on_cache(const char *tree_name, const char *prefix)
360
static const char *(matchbuf[2]);
361
matchbuf[0] = prefix;
362
matchbuf[1] = NULL;
363
- match = matchbuf;
363
+ init_pathspec(&pathspec, matchbuf);
364
+ pathspec.items[0].has_wildcard = 0;
365
} else
365
- match = NULL;
366
- if (read_tree(tree, 1, match))
366
+ init_pathspec(&pathspec, NULL);
367
+ if (read_tree(tree, 1, &pathspec))
368
die("unable to read tree entries %s", tree_name);
369
370
for (i = 0; i < active_nr; i++) {
builtin/ls-tree.c
+7
-5
@@ -19,7 +19,7 @@ static int line_termination = '\n';
19
#define LS_SHOW_SIZE 16
20
static int abbrev;
21
static int ls_options;
22
-static const char **pathspec;
22
+static struct pathspec pathspec;
23
static int chomp_prefix;
24
static const char *ls_tree_prefix;
25
@@ -35,7 +35,7 @@ static int show_recursive(const char *base, int baselen, const char *pathname)
35
if (ls_options & LS_RECURSIVE)
36
return 1;
37
38
- s = pathspec;
38
+ s = pathspec.raw;
39
if (!s)
40
return 0;
41
@@ -120,7 +120,7 @@ int cmd_ls_tree(int argc, const char **argv, const char *prefix)
120
{
121
unsigned char sha1[20];
122
struct tree *tree;
123
- int full_tree = 0;
123
+ int i, full_tree = 0;
124
const struct option ls_tree_options[] = {
125
OPT_BIT('d', NULL, &ls_options, "only show trees",
126
LS_TREE_ONLY),
@@ -166,11 +166,13 @@ int cmd_ls_tree(int argc, const char **argv, const char *prefix)
166
if (get_sha1(argv[0], sha1))
167
die("Not a valid object name %s", argv[0]);
168
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].has_wildcard = 0;
172
tree = parse_tree_indirect(sha1);
173
if (!tree)
174
die("not a tree object");
173
- read_tree_recursive(tree, "", 0, 0, pathspec, show_tree, NULL);
175
+ read_tree_recursive(tree, "", 0, 0, &pathspec, show_tree, NULL);
176
177
return 0;
178
}
merge-recursive.c
+3
-1
@@ -288,7 +288,9 @@ static int save_files_dirs(const unsigned char *sha1,
288
static int get_files_dirs(struct merge_options *o, struct tree *tree)
289
{
290
int n;
291
- if (read_tree_recursive(tree, "", 0, 0, NULL, save_files_dirs, o))
291
+ struct pathspec match_all;
292
+ init_pathspec(&match_all, NULL);
293
+ if (read_tree_recursive(tree, "", 0, 0, &match_all, save_files_dirs, o))
294
return 0;
295
n = o->current_file_set.nr + o->current_directory_set.nr;
296
return n;
t/t3102-ls-tree-wildcards.sh
new
+22
@@ -0,0 +1,22 @@
1
+#!/bin/sh
2
+
3
+test_description='ls-tree with(out) wildcards'
4
+
5
+. ./test-lib.sh
6
+
7
+test_expect_success 'setup' '
8
+ mkdir a aa "a*" &&
9
+ touch a/one aa/two "a*/three" &&
10
+ git add a/one aa/two "a*/three" &&
11
+ git commit -m test
12
+'
13
+
14
+test_expect_success 'ls-tree a* matches literally' '
15
+ cat >expected <<EOF &&
16
+100644 blob e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 a*/three
17
+EOF
18
+ git ls-tree -r HEAD "a*" >actual &&
19
+ test_cmp expected actual
20
+'
21
+
22
+test_done
tree.c
+4
-9
@@ -114,20 +114,15 @@ static int read_tree_1(struct tree *tree, struct strbuf *base,
114
115
int read_tree_recursive(struct tree *tree,
116
const char *base, int baselen,
117
- int stage, const char **match,
117
+ int stage, struct pathspec *pathspec,
118
read_tree_fn_t fn, void *context)
119
{
120
struct strbuf sb = STRBUF_INIT;
121
- struct pathspec pathspec;
122
- int i, ret;
121
+ int ret;
122
124
- init_pathspec(&pathspec, match);
125
- for (i = 0; i < pathspec.nr; i++)
126
- pathspec.items[i].has_wildcard = 0;
123
strbuf_add(&sb, base, baselen);
128
- ret = read_tree_1(tree, &sb, stage, &pathspec, fn, context);
124
+ ret = read_tree_1(tree, &sb, stage, pathspec, fn, context);
125
strbuf_release(&sb);
130
- free_pathspec(&pathspec);
126
return ret;
127
}
128
@@ -141,7 +136,7 @@ static int cmp_cache_name_compare(const void *a_, const void *b_)
136
ce2->name, ce2->ce_flags);
137
}
138
144
-int read_tree(struct tree *tree, int stage, const char **match)
139
+int read_tree(struct tree *tree, int stage, struct pathspec *match)
140
{
141
read_tree_fn_t fn = NULL;
142
int i, err;
tree.h
+2
-2
@@ -25,9 +25,9 @@ typedef int (*read_tree_fn_t)(const unsigned char *, const char *, int, const ch
25
26
extern int read_tree_recursive(struct tree *tree,
27
const char *base, int baselen,
28
- int stage, const char **match,
28
+ int stage, struct pathspec *pathspec,
29
read_tree_fn_t fn, void *context);
30
31
-extern int read_tree(struct tree *tree, int stage, const char **paths);
31
+extern int read_tree(struct tree *tree, int stage, struct pathspec *pathspec);
32
33
#endif /* TREE_H */