5
#include "diff.h"
6
#include "tree.h"
7
8
-static char *malloc_base(const char *base, const char *path, int pathlen)
8
+static char *malloc_base(const char *base, int baselen, const char *path, int pathlen)
9
{
10
- int baselen = strlen(base);
10
char *newbase = xmalloc(baselen + pathlen + 2);
11
memcpy(newbase, base, baselen);
12
memcpy(newbase + baselen, path, pathlen);
15
}
16
17
static void show_entry(struct diff_options *opt, const char *prefix, struct tree_desc *desc,
19
- const char *base);
18
+ const char *base, int baselen);
19
21
-static int compare_tree_entry(struct tree_desc *t1, struct tree_desc *t2, const char *base, struct diff_options *opt)
20
+static int compare_tree_entry(struct tree_desc *t1, struct tree_desc *t2, const char *base, int baselen, struct diff_options *opt)
21
{
22
unsigned mode1, mode2;
23
const char *path1, *path2;
27
sha1 = tree_entry_extract(t1, &path1, &mode1);
28
sha2 = tree_entry_extract(t2, &path2, &mode2);
29
31
- pathlen1 = strlen(path1);
32
- pathlen2 = strlen(path2);
30
+ pathlen1 = tree_entry_len(path1, sha1);
31
+ pathlen2 = tree_entry_len(path2, sha2);
32
cmp = base_name_compare(path1, pathlen1, mode1, path2, pathlen2, mode2);
33
if (cmp < 0) {
35
- show_entry(opt, "-", t1, base);
34
+ show_entry(opt, "-", t1, base, baselen);
35
return -1;
36
}
37
if (cmp > 0) {
39
- show_entry(opt, "+", t2, base);
38
+ show_entry(opt, "+", t2, base, baselen);
39
return 1;
40
}
41
if (!opt->find_copies_harder && !hashcmp(sha1, sha2) && mode1 == mode2)
46
* file, we need to consider it a remove and an add.
47
*/
48
if (S_ISDIR(mode1) != S_ISDIR(mode2)) {
50
- show_entry(opt, "-", t1, base);
51
- show_entry(opt, "+", t2, base);
49
+ show_entry(opt, "-", t1, base, baselen);
50
+ show_entry(opt, "+", t2, base, baselen);
51
return 0;
52
}
53
54
if (opt->recursive && S_ISDIR(mode1)) {
55
int retval;
57
- char *newbase = malloc_base(base, path1, pathlen1);
56
+ char *newbase = malloc_base(base, baselen, path1, pathlen1);
57
if (opt->tree_in_recursive)
58
opt->change(opt, mode1, mode2,
59
sha1, sha2, base, path1);
66
return 0;
67
}
68
70
-static int interesting(struct tree_desc *desc, const char *base, struct diff_options *opt)
69
+static int interesting(struct tree_desc *desc, const char *base, int baselen, struct diff_options *opt)
70
{
71
const char *path;
72
+ const unsigned char *sha1;
73
unsigned mode;
74
int i;
75
- int baselen, pathlen;
75
+ int pathlen;
76
77
if (!opt->nr_paths)
78
return 1;
79
80
- (void)tree_entry_extract(desc, &path, &mode);
80
+ sha1 = tree_entry_extract(desc, &path, &mode);
81
82
- pathlen = strlen(path);
83
- baselen = strlen(base);
82
+ pathlen = tree_entry_len(path, sha1);
83
84
for (i=0; i < opt->nr_paths; i++) {
85
const char *match = opt->paths[i];
120
}
121
122
/* A whole sub-tree went away or appeared */
124
-static void show_tree(struct diff_options *opt, const char *prefix, struct tree_desc *desc, const char *base)
123
+static void show_tree(struct diff_options *opt, const char *prefix, struct tree_desc *desc, const char *base, int baselen)
124
{
125
while (desc->size) {
127
- if (interesting(desc, base, opt))
128
- show_entry(opt, prefix, desc, base);
126
+ if (interesting(desc, base, baselen, opt))
127
+ show_entry(opt, prefix, desc, base, baselen);
128
update_tree_entry(desc);
129
}
130
}
131
132
/* A file entry went away or appeared */
133
static void show_entry(struct diff_options *opt, const char *prefix, struct tree_desc *desc,
135
- const char *base)
134
+ const char *base, int baselen)
135
{
136
unsigned mode;
137
const char *path;
139
140
if (opt->recursive && S_ISDIR(mode)) {
141
enum object_type type;
143
- char *newbase = malloc_base(base, path, strlen(path));
142
+ int pathlen = tree_entry_len(path, sha1);
143
+ char *newbase = malloc_base(base, baselen, path, pathlen);
144
struct tree_desc inner;
145
void *tree;
146
149
die("corrupt tree sha %s", sha1_to_hex(sha1));
150
151
inner.buf = tree;
152
- show_tree(opt, prefix, &inner, newbase);
152
+ show_tree(opt, prefix, &inner, newbase, baselen + 1 + pathlen);
153
154
free(tree);
155
free(newbase);
160
161
int diff_tree(struct tree_desc *t1, struct tree_desc *t2, const char *base, struct diff_options *opt)
162
{
163
+ int baselen = strlen(base);
164
+
165
while (t1->size | t2->size) {
164
- if (opt->nr_paths && t1->size && !interesting(t1, base, opt)) {
166
+ if (opt->nr_paths && t1->size && !interesting(t1, base, baselen, opt)) {
167
update_tree_entry(t1);
168
continue;
169
}
168
- if (opt->nr_paths && t2->size && !interesting(t2, base, opt)) {
170
+ if (opt->nr_paths && t2->size && !interesting(t2, base, baselen, opt)) {
171
update_tree_entry(t2);
172
continue;
173
}
174
if (!t1->size) {
173
- show_entry(opt, "+", t2, base);
175
+ show_entry(opt, "+", t2, base, baselen);
176
update_tree_entry(t2);
177
continue;
178
}
179
if (!t2->size) {
178
- show_entry(opt, "-", t1, base);
180
+ show_entry(opt, "-", t1, base, baselen);
181
update_tree_entry(t1);
182
continue;
183
}
182
- switch (compare_tree_entry(t1, t2, base, opt)) {
184
+ switch (compare_tree_entry(t1, t2, base, baselen, opt)) {
185
case -1:
186
update_tree_entry(t1);
187
continue;