Skip to content

Commit a8baa7b

Browse files
author
Junio C Hamano
committed
tree-diff: do not assume we use only one pathspec
The way tree-diff was set up assumed we would use only one set of pathspec during the entire life of the program. Move the pathspec related static variables out to diff_options structure so that we can filter commits with one set of paths while show the actual diffs using different set of paths. I suspect this breaks blame.c, and makes "git log paths..." to default to the --full-diff, the latter of which is dealt with the next commit. Signed-off-by: Junio C Hamano <junkio@cox.net>
1 parent 6d46a23 commit a8baa7b

File tree

4 files changed

+32
-24
lines changed

4 files changed

+32
-24
lines changed

diff-tree.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ int main(int argc, const char **argv)
120120
if (opt->diffopt.output_format == DIFF_FORMAT_PATCH)
121121
opt->diffopt.recursive = 1;
122122

123-
diff_tree_setup_paths(get_pathspec(prefix, argv));
123+
diff_tree_setup_paths(get_pathspec(prefix, argv), opt);
124124
diff_setup_done(&opt->diffopt);
125125

126126
switch (nr_sha1) {

diff.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,15 @@ struct diff_options {
3838
int setup;
3939
int abbrev;
4040

41+
int nr_paths;
42+
const char **paths;
43+
int *pathlens;
4144
change_fn_t change;
4245
add_remove_fn_t add_remove;
4346
};
4447

45-
extern void diff_tree_setup_paths(const char **paths);
48+
extern void diff_tree_setup_paths(const char **paths, struct diff_options *);
49+
extern void diff_tree_release_paths(struct diff_options *);
4650
extern int diff_tree(struct tree_desc *t1, struct tree_desc *t2,
4751
const char *base, struct diff_options *opt);
4852
extern int diff_tree_sha1(const unsigned char *old, const unsigned char *new,

revision.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -707,7 +707,7 @@ int setup_revisions(int argc, const char **argv, struct rev_info *revs, const ch
707707
revs->limited = 1;
708708

709709
if (revs->prune_data) {
710-
diff_tree_setup_paths(revs->prune_data);
710+
diff_tree_setup_paths(revs->prune_data, &diff_opt);
711711
revs->prune_fn = try_to_simplify_commit;
712712
}
713713

tree-diff.c

Lines changed: 25 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,6 @@
55
#include "diff.h"
66
#include "tree.h"
77

8-
// What paths are we interested in?
9-
static int nr_paths = 0;
10-
static const char **paths = NULL;
11-
static int *pathlens = NULL;
12-
138
static char *malloc_base(const char *base, const char *path, int pathlen)
149
{
1510
int baselen = strlen(base);
@@ -72,24 +67,24 @@ static int compare_tree_entry(struct tree_desc *t1, struct tree_desc *t2, const
7267
return 0;
7368
}
7469

75-
static int interesting(struct tree_desc *desc, const char *base)
70+
static int interesting(struct tree_desc *desc, const char *base, struct diff_options *opt)
7671
{
7772
const char *path;
7873
unsigned mode;
7974
int i;
8075
int baselen, pathlen;
8176

82-
if (!nr_paths)
77+
if (!opt->nr_paths)
8378
return 1;
8479

8580
(void)tree_entry_extract(desc, &path, &mode);
8681

8782
pathlen = strlen(path);
8883
baselen = strlen(base);
8984

90-
for (i=0; i < nr_paths; i++) {
91-
const char *match = paths[i];
92-
int matchlen = pathlens[i];
85+
for (i=0; i < opt->nr_paths; i++) {
86+
const char *match = opt->paths[i];
87+
int matchlen = opt->pathlens[i];
9388

9489
if (baselen >= matchlen) {
9590
/* If it doesn't match, move along... */
@@ -129,7 +124,7 @@ static int interesting(struct tree_desc *desc, const char *base)
129124
static void show_tree(struct diff_options *opt, const char *prefix, struct tree_desc *desc, const char *base)
130125
{
131126
while (desc->size) {
132-
if (interesting(desc, base))
127+
if (interesting(desc, base, opt))
133128
show_entry(opt, prefix, desc, base);
134129
update_tree_entry(desc);
135130
}
@@ -167,11 +162,11 @@ static int show_entry(struct diff_options *opt, const char *prefix, struct tree_
167162
int diff_tree(struct tree_desc *t1, struct tree_desc *t2, const char *base, struct diff_options *opt)
168163
{
169164
while (t1->size | t2->size) {
170-
if (nr_paths && t1->size && !interesting(t1, base)) {
165+
if (opt->nr_paths && t1->size && !interesting(t1, base, opt)) {
171166
update_tree_entry(t1);
172167
continue;
173168
}
174-
if (nr_paths && t2->size && !interesting(t2, base)) {
169+
if (opt->nr_paths && t2->size && !interesting(t2, base, opt)) {
175170
update_tree_entry(t2);
176171
continue;
177172
}
@@ -229,19 +224,28 @@ static int count_paths(const char **paths)
229224
return i;
230225
}
231226

232-
void diff_tree_setup_paths(const char **p)
227+
void diff_tree_release_paths(struct diff_options *opt)
233228
{
229+
free(opt->pathlens);
230+
}
231+
232+
void diff_tree_setup_paths(const char **p, struct diff_options *opt)
233+
{
234+
opt->nr_paths = 0;
235+
opt->pathlens = NULL;
236+
opt->paths = NULL;
237+
234238
if (p) {
235239
int i;
236240

237-
paths = p;
238-
nr_paths = count_paths(paths);
239-
if (nr_paths == 0) {
240-
pathlens = NULL;
241+
opt->paths = p;
242+
opt->nr_paths = count_paths(p);
243+
if (opt->nr_paths == 0) {
244+
opt->pathlens = NULL;
241245
return;
242246
}
243-
pathlens = xmalloc(nr_paths * sizeof(int));
244-
for (i=0; i<nr_paths; i++)
245-
pathlens[i] = strlen(paths[i]);
247+
opt->pathlens = xmalloc(opt->nr_paths * sizeof(int));
248+
for (i=0; i < opt->nr_paths; i++)
249+
opt->pathlens[i] = strlen(p[i]);
246250
}
247251
}

0 commit comments

Comments
 (0)