Remove "pathlen" from "struct name_entry"
[git/git.git] / tree-diff.c
CommitLineData
ac1b3d12
LT
1/*
2 * Helper functions for tree diff generation
3 */
4#include "cache.h"
5#include "diff.h"
8e440259 6#include "tree.h"
ac1b3d12 7
304de2d2 8static char *malloc_base(const char *base, int baselen, const char *path, int pathlen)
ac1b3d12 9{
ac1b3d12
LT
10 char *newbase = xmalloc(baselen + pathlen + 2);
11 memcpy(newbase, base, baselen);
12 memcpy(newbase + baselen, path, pathlen);
13 memcpy(newbase + baselen + pathlen, "/", 2);
14 return newbase;
15}
16
cf995ede 17static void show_entry(struct diff_options *opt, const char *prefix, struct tree_desc *desc,
304de2d2 18 const char *base, int baselen);
ac1b3d12 19
304de2d2 20static int compare_tree_entry(struct tree_desc *t1, struct tree_desc *t2, const char *base, int baselen, struct diff_options *opt)
ac1b3d12
LT
21{
22 unsigned mode1, mode2;
23 const char *path1, *path2;
24 const unsigned char *sha1, *sha2;
25 int cmp, pathlen1, pathlen2;
26
50f9a858
LT
27 sha1 = tree_entry_extract(t1, &path1, &mode1);
28 sha2 = tree_entry_extract(t2, &path2, &mode2);
ac1b3d12 29
304de2d2
LT
30 pathlen1 = tree_entry_len(path1, sha1);
31 pathlen2 = tree_entry_len(path2, sha2);
ac1b3d12
LT
32 cmp = base_name_compare(path1, pathlen1, mode1, path2, pathlen2, mode2);
33 if (cmp < 0) {
304de2d2 34 show_entry(opt, "-", t1, base, baselen);
ac1b3d12
LT
35 return -1;
36 }
37 if (cmp > 0) {
304de2d2 38 show_entry(opt, "+", t2, base, baselen);
ac1b3d12
LT
39 return 1;
40 }
a89fccd2 41 if (!opt->find_copies_harder && !hashcmp(sha1, sha2) && mode1 == mode2)
ac1b3d12
LT
42 return 0;
43
44 /*
45 * If the filemode has changed to/from a directory from/to a regular
46 * file, we need to consider it a remove and an add.
47 */
48 if (S_ISDIR(mode1) != S_ISDIR(mode2)) {
304de2d2
LT
49 show_entry(opt, "-", t1, base, baselen);
50 show_entry(opt, "+", t2, base, baselen);
ac1b3d12
LT
51 return 0;
52 }
53
54 if (opt->recursive && S_ISDIR(mode1)) {
55 int retval;
304de2d2 56 char *newbase = malloc_base(base, baselen, path1, pathlen1);
ac1b3d12
LT
57 if (opt->tree_in_recursive)
58 opt->change(opt, mode1, mode2,
59 sha1, sha2, base, path1);
60 retval = diff_tree_sha1(sha1, sha2, newbase, opt);
61 free(newbase);
62 return retval;
63 }
64
65 opt->change(opt, mode1, mode2, sha1, sha2, base, path1);
66 return 0;
67}
68
5d865017
LT
69/*
70 * Is a tree entry interesting given the pathspec we have?
71 *
72 * Return:
73 * - positive for yes
74 * - zero for no
75 * - negative for "no, and no subsequent entries will be either"
76 */
77static int tree_entry_interesting(struct tree_desc *desc, const char *base, int baselen, struct diff_options *opt)
ac1b3d12
LT
78{
79 const char *path;
304de2d2 80 const unsigned char *sha1;
ac1b3d12
LT
81 unsigned mode;
82 int i;
304de2d2 83 int pathlen;
ac1b3d12 84
a8baa7b9 85 if (!opt->nr_paths)
ac1b3d12
LT
86 return 1;
87
304de2d2 88 sha1 = tree_entry_extract(desc, &path, &mode);
ac1b3d12 89
304de2d2 90 pathlen = tree_entry_len(path, sha1);
ac1b3d12 91
a8baa7b9
JH
92 for (i=0; i < opt->nr_paths; i++) {
93 const char *match = opt->paths[i];
94 int matchlen = opt->pathlens[i];
ac1b3d12
LT
95
96 if (baselen >= matchlen) {
97 /* If it doesn't match, move along... */
98 if (strncmp(base, match, matchlen))
99 continue;
100
101 /* The base is a subdirectory of a path which was specified. */
102 return 1;
103 }
104
105 /* Does the base match? */
106 if (strncmp(base, match, baselen))
107 continue;
108
109 match += baselen;
110 matchlen -= baselen;
111
112 if (pathlen > matchlen)
113 continue;
114
115 if (matchlen > pathlen) {
116 if (match[pathlen] != '/')
117 continue;
118 if (!S_ISDIR(mode))
119 continue;
120 }
121
122 if (strncmp(path, match, pathlen))
123 continue;
124
125 return 1;
126 }
127 return 0; /* No matches */
128}
129
130/* A whole sub-tree went away or appeared */
304de2d2 131static void show_tree(struct diff_options *opt, const char *prefix, struct tree_desc *desc, const char *base, int baselen)
ac1b3d12
LT
132{
133 while (desc->size) {
5d865017
LT
134 int show = tree_entry_interesting(desc, base, baselen, opt);
135 if (show < 0)
136 break;
137 if (show)
304de2d2 138 show_entry(opt, prefix, desc, base, baselen);
ac1b3d12
LT
139 update_tree_entry(desc);
140 }
141}
142
143/* A file entry went away or appeared */
cf995ede 144static void show_entry(struct diff_options *opt, const char *prefix, struct tree_desc *desc,
304de2d2 145 const char *base, int baselen)
ac1b3d12
LT
146{
147 unsigned mode;
148 const char *path;
50f9a858 149 const unsigned char *sha1 = tree_entry_extract(desc, &path, &mode);
ac1b3d12
LT
150
151 if (opt->recursive && S_ISDIR(mode)) {
21666f1a 152 enum object_type type;
304de2d2
LT
153 int pathlen = tree_entry_len(path, sha1);
154 char *newbase = malloc_base(base, baselen, path, pathlen);
ac1b3d12
LT
155 struct tree_desc inner;
156 void *tree;
157
21666f1a
NP
158 tree = read_sha1_file(sha1, &type, &inner.size);
159 if (!tree || type != OBJ_TREE)
ac1b3d12
LT
160 die("corrupt tree sha %s", sha1_to_hex(sha1));
161
162 inner.buf = tree;
304de2d2 163 show_tree(opt, prefix, &inner, newbase, baselen + 1 + pathlen);
ac1b3d12
LT
164
165 free(tree);
166 free(newbase);
cf995ede
DR
167 } else {
168 opt->add_remove(opt, prefix[0], mode, sha1, base, path);
ac1b3d12 169 }
ac1b3d12
LT
170}
171
5d865017
LT
172static void skip_uninteresting(struct tree_desc *t, const char *base, int baselen, struct diff_options *opt)
173{
174 while (t->size) {
175 int show = tree_entry_interesting(t, base, baselen, opt);
176 if (!show) {
177 update_tree_entry(t);
178 continue;
179 }
180 /* Skip it all? */
181 if (show < 0)
182 t->size = 0;
183 return;
184 }
185}
186
ac1b3d12
LT
187int diff_tree(struct tree_desc *t1, struct tree_desc *t2, const char *base, struct diff_options *opt)
188{
304de2d2
LT
189 int baselen = strlen(base);
190
5d865017 191 for (;;) {
822cac01
JH
192 if (opt->quiet && opt->has_changes)
193 break;
5d865017
LT
194 if (opt->nr_paths) {
195 skip_uninteresting(t1, base, baselen, opt);
196 skip_uninteresting(t2, base, baselen, opt);
ac1b3d12
LT
197 }
198 if (!t1->size) {
5d865017
LT
199 if (!t2->size)
200 break;
304de2d2 201 show_entry(opt, "+", t2, base, baselen);
ac1b3d12
LT
202 update_tree_entry(t2);
203 continue;
204 }
205 if (!t2->size) {
304de2d2 206 show_entry(opt, "-", t1, base, baselen);
ac1b3d12
LT
207 update_tree_entry(t1);
208 continue;
209 }
304de2d2 210 switch (compare_tree_entry(t1, t2, base, baselen, opt)) {
ac1b3d12
LT
211 case -1:
212 update_tree_entry(t1);
213 continue;
214 case 0:
215 update_tree_entry(t1);
216 /* Fallthrough */
217 case 1:
218 update_tree_entry(t2);
219 continue;
220 }
221 die("git-diff-tree: internal error");
222 }
223 return 0;
224}
225
226int diff_tree_sha1(const unsigned char *old, const unsigned char *new, const char *base, struct diff_options *opt)
227{
228 void *tree1, *tree2;
229 struct tree_desc t1, t2;
230 int retval;
231
8e440259 232 tree1 = read_object_with_reference(old, tree_type, &t1.size, NULL);
ac1b3d12
LT
233 if (!tree1)
234 die("unable to read source tree (%s)", sha1_to_hex(old));
8e440259 235 tree2 = read_object_with_reference(new, tree_type, &t2.size, NULL);
ac1b3d12
LT
236 if (!tree2)
237 die("unable to read destination tree (%s)", sha1_to_hex(new));
238 t1.buf = tree1;
239 t2.buf = tree2;
240 retval = diff_tree(&t1, &t2, base, opt);
241 free(tree1);
242 free(tree2);
243 return retval;
244}
245
2b60356d
RS
246int diff_root_tree_sha1(const unsigned char *new, const char *base, struct diff_options *opt)
247{
248 int retval;
249 void *tree;
250 struct tree_desc empty, real;
251
252 tree = read_object_with_reference(new, tree_type, &real.size, NULL);
253 if (!tree)
254 die("unable to read root tree (%s)", sha1_to_hex(new));
255 real.buf = tree;
256
257 empty.size = 0;
258 empty.buf = "";
259 retval = diff_tree(&empty, &real, base, opt);
260 free(tree);
261 return retval;
262}
263
ac1b3d12
LT
264static int count_paths(const char **paths)
265{
266 int i = 0;
267 while (*paths++)
268 i++;
269 return i;
270}
271
a8baa7b9 272void diff_tree_release_paths(struct diff_options *opt)
ac1b3d12 273{
a8baa7b9
JH
274 free(opt->pathlens);
275}
276
277void diff_tree_setup_paths(const char **p, struct diff_options *opt)
278{
279 opt->nr_paths = 0;
280 opt->pathlens = NULL;
281 opt->paths = NULL;
282
ac1b3d12
LT
283 if (p) {
284 int i;
285
a8baa7b9
JH
286 opt->paths = p;
287 opt->nr_paths = count_paths(p);
288 if (opt->nr_paths == 0) {
289 opt->pathlens = NULL;
7e4a2a84
JH
290 return;
291 }
a8baa7b9
JH
292 opt->pathlens = xmalloc(opt->nr_paths * sizeof(int));
293 for (i=0; i < opt->nr_paths; i++)
294 opt->pathlens[i] = strlen(p[i]);
ac1b3d12
LT
295 }
296}