🌐 AI搜索 & 代理 主页
blob: 26bdbdd2bfea5eab99b9f1c38936efe56f1ab45a [file] [log] [blame]
Linus Torvaldsac1b3d12005-10-21 04:05:051/*
2 * Helper functions for tree diff generation
3 */
4#include "cache.h"
5#include "diff.h"
Linus Torvalds750f7b62007-06-19 21:22:466#include "diffcore.h"
Peter Eriksen8e440252006-04-02 12:44:097#include "tree.h"
Linus Torvaldsac1b3d12005-10-21 04:05:058
Linus Torvalds304de2d2007-03-18 03:06:249static char *malloc_base(const char *base, int baselen, const char *path, int pathlen)
Linus Torvaldsac1b3d12005-10-21 04:05:0510{
Linus Torvaldsac1b3d12005-10-21 04:05:0511 char *newbase = xmalloc(baselen + pathlen + 2);
12 memcpy(newbase, base, baselen);
13 memcpy(newbase + baselen, path, pathlen);
14 memcpy(newbase + baselen + pathlen, "/", 2);
15 return newbase;
16}
17
David Rientjescf995ed2006-08-14 20:39:2718static void show_entry(struct diff_options *opt, const char *prefix, struct tree_desc *desc,
Linus Torvalds304de2d2007-03-18 03:06:2419 const char *base, int baselen);
Linus Torvaldsac1b3d12005-10-21 04:05:0520
Linus Torvalds304de2d2007-03-18 03:06:2421static int compare_tree_entry(struct tree_desc *t1, struct tree_desc *t2, const char *base, int baselen, struct diff_options *opt)
Linus Torvaldsac1b3d12005-10-21 04:05:0522{
23 unsigned mode1, mode2;
24 const char *path1, *path2;
25 const unsigned char *sha1, *sha2;
26 int cmp, pathlen1, pathlen2;
27
Linus Torvalds50f9a852006-01-31 22:10:5628 sha1 = tree_entry_extract(t1, &path1, &mode1);
29 sha2 = tree_entry_extract(t2, &path2, &mode2);
Linus Torvaldsac1b3d12005-10-21 04:05:0530
Linus Torvalds304de2d2007-03-18 03:06:2431 pathlen1 = tree_entry_len(path1, sha1);
32 pathlen2 = tree_entry_len(path2, sha2);
Linus Torvaldsac1b3d12005-10-21 04:05:0533 cmp = base_name_compare(path1, pathlen1, mode1, path2, pathlen2, mode2);
34 if (cmp < 0) {
Linus Torvalds304de2d2007-03-18 03:06:2435 show_entry(opt, "-", t1, base, baselen);
Linus Torvaldsac1b3d12005-10-21 04:05:0536 return -1;
37 }
38 if (cmp > 0) {
Linus Torvalds304de2d2007-03-18 03:06:2439 show_entry(opt, "+", t2, base, baselen);
Linus Torvaldsac1b3d12005-10-21 04:05:0540 return 1;
41 }
David Rientjesa89fccd2006-08-17 18:54:5742 if (!opt->find_copies_harder && !hashcmp(sha1, sha2) && mode1 == mode2)
Linus Torvaldsac1b3d12005-10-21 04:05:0543 return 0;
44
45 /*
46 * If the filemode has changed to/from a directory from/to a regular
47 * file, we need to consider it a remove and an add.
48 */
49 if (S_ISDIR(mode1) != S_ISDIR(mode2)) {
Linus Torvalds304de2d2007-03-18 03:06:2450 show_entry(opt, "-", t1, base, baselen);
51 show_entry(opt, "+", t2, base, baselen);
Linus Torvaldsac1b3d12005-10-21 04:05:0552 return 0;
53 }
54
55 if (opt->recursive && S_ISDIR(mode1)) {
56 int retval;
Linus Torvalds304de2d2007-03-18 03:06:2457 char *newbase = malloc_base(base, baselen, path1, pathlen1);
Linus Torvaldsac1b3d12005-10-21 04:05:0558 if (opt->tree_in_recursive)
59 opt->change(opt, mode1, mode2,
60 sha1, sha2, base, path1);
61 retval = diff_tree_sha1(sha1, sha2, newbase, opt);
62 free(newbase);
63 return retval;
64 }
65
66 opt->change(opt, mode1, mode2, sha1, sha2, base, path1);
67 return 0;
68}
69
Linus Torvalds5d865012007-03-18 22:18:3070/*
71 * Is a tree entry interesting given the pathspec we have?
72 *
73 * Return:
Junio C Hamano1d848f62007-03-22 00:00:2774 * - 2 for "yes, and all subsequent entries will be"
75 * - 1 for yes
Linus Torvalds5d865012007-03-18 22:18:3076 * - zero for no
77 * - negative for "no, and no subsequent entries will be either"
78 */
79static int tree_entry_interesting(struct tree_desc *desc, const char *base, int baselen, struct diff_options *opt)
Linus Torvaldsac1b3d12005-10-21 04:05:0580{
81 const char *path;
Linus Torvalds304de2d2007-03-18 03:06:2482 const unsigned char *sha1;
Linus Torvaldsac1b3d12005-10-21 04:05:0583 unsigned mode;
84 int i;
Linus Torvalds304de2d2007-03-18 03:06:2485 int pathlen;
Junio C Hamano7d2f6672007-03-21 16:51:4786 int never_interesting = -1;
Linus Torvaldsac1b3d12005-10-21 04:05:0587
Junio C Hamanoa8baa7b2006-04-10 23:39:1188 if (!opt->nr_paths)
Linus Torvaldsac1b3d12005-10-21 04:05:0589 return 1;
90
Linus Torvalds304de2d2007-03-18 03:06:2491 sha1 = tree_entry_extract(desc, &path, &mode);
Linus Torvaldsac1b3d12005-10-21 04:05:0592
Linus Torvalds304de2d2007-03-18 03:06:2493 pathlen = tree_entry_len(path, sha1);
Linus Torvaldsac1b3d12005-10-21 04:05:0594
Junio C Hamano7d2f6672007-03-21 16:51:4795 for (i = 0; i < opt->nr_paths; i++) {
Junio C Hamanoa8baa7b2006-04-10 23:39:1196 const char *match = opt->paths[i];
97 int matchlen = opt->pathlens[i];
Junio C Hamanoccc744a2007-03-21 19:34:4698 int m = -1; /* signals that we haven't called strncmp() */
Linus Torvaldsac1b3d12005-10-21 04:05:0599
100 if (baselen >= matchlen) {
101 /* If it doesn't match, move along... */
102 if (strncmp(base, match, matchlen))
103 continue;
104
Junio C Hamano1d848f62007-03-22 00:00:27105 /*
106 * The base is a subdirectory of a path which
107 * was specified, so all of them are interesting.
108 */
109 return 2;
Linus Torvaldsac1b3d12005-10-21 04:05:05110 }
111
112 /* Does the base match? */
113 if (strncmp(base, match, baselen))
114 continue;
115
116 match += baselen;
117 matchlen -= baselen;
118
Junio C Hamanoccc744a2007-03-21 19:34:46119 if (never_interesting) {
120 /*
121 * We have not seen any match that sorts later
122 * than the current path.
123 */
Junio C Hamano7d2f6672007-03-21 16:51:47124
Junio C Hamanoccc744a2007-03-21 19:34:46125 /*
126 * Does match sort strictly earlier than path
127 * with their common parts?
128 */
129 m = strncmp(match, path,
130 (matchlen < pathlen) ? matchlen : pathlen);
131 if (m < 0)
132 continue;
133
134 /*
135 * If we come here even once, that means there is at
136 * least one pathspec that would sort equal to or
137 * later than the path we are currently looking at.
138 * In other words, if we have never reached this point
139 * after iterating all pathspecs, it means all
140 * pathspecs are either outside of base, or inside the
141 * base but sorts strictly earlier than the current
142 * one. In either case, they will never match the
143 * subsequent entries. In such a case, we initialized
144 * the variable to -1 and that is what will be
145 * returned, allowing the caller to terminate early.
146 */
147 never_interesting = 0;
148 }
Junio C Hamano7d2f6672007-03-21 16:51:47149
Linus Torvaldsac1b3d12005-10-21 04:05:05150 if (pathlen > matchlen)
151 continue;
152
153 if (matchlen > pathlen) {
154 if (match[pathlen] != '/')
155 continue;
156 if (!S_ISDIR(mode))
157 continue;
158 }
159
Junio C Hamanoccc744a2007-03-21 19:34:46160 if (m == -1)
161 /*
162 * we cheated and did not do strncmp(), so we do
163 * that here.
164 */
165 m = strncmp(match, path, pathlen);
166
Junio C Hamano7d2f6672007-03-21 16:51:47167 /*
168 * If common part matched earlier then it is a hit,
169 * because we rejected the case where path is not a
170 * leading directory and is shorter than match.
171 */
172 if (!m)
173 return 1;
Linus Torvaldsac1b3d12005-10-21 04:05:05174 }
Junio C Hamano7d2f6672007-03-21 16:51:47175 return never_interesting; /* No matches */
Linus Torvaldsac1b3d12005-10-21 04:05:05176}
177
178/* A whole sub-tree went away or appeared */
Linus Torvalds304de2d2007-03-18 03:06:24179static void show_tree(struct diff_options *opt, const char *prefix, struct tree_desc *desc, const char *base, int baselen)
Linus Torvaldsac1b3d12005-10-21 04:05:05180{
Junio C Hamano1d848f62007-03-22 00:00:27181 int all_interesting = 0;
Linus Torvaldsac1b3d12005-10-21 04:05:05182 while (desc->size) {
Junio C Hamano1d848f62007-03-22 00:00:27183 int show;
184
185 if (all_interesting)
186 show = 1;
187 else {
188 show = tree_entry_interesting(desc, base, baselen,
189 opt);
190 if (show == 2)
191 all_interesting = 1;
192 }
Linus Torvalds5d865012007-03-18 22:18:30193 if (show < 0)
194 break;
195 if (show)
Linus Torvalds304de2d2007-03-18 03:06:24196 show_entry(opt, prefix, desc, base, baselen);
Linus Torvaldsac1b3d12005-10-21 04:05:05197 update_tree_entry(desc);
198 }
199}
200
201/* A file entry went away or appeared */
David Rientjescf995ed2006-08-14 20:39:27202static void show_entry(struct diff_options *opt, const char *prefix, struct tree_desc *desc,
Linus Torvalds304de2d2007-03-18 03:06:24203 const char *base, int baselen)
Linus Torvaldsac1b3d12005-10-21 04:05:05204{
205 unsigned mode;
206 const char *path;
Linus Torvalds50f9a852006-01-31 22:10:56207 const unsigned char *sha1 = tree_entry_extract(desc, &path, &mode);
Linus Torvaldsac1b3d12005-10-21 04:05:05208
209 if (opt->recursive && S_ISDIR(mode)) {
Nicolas Pitre21666f12007-02-26 19:55:59210 enum object_type type;
Linus Torvalds304de2d2007-03-18 03:06:24211 int pathlen = tree_entry_len(path, sha1);
212 char *newbase = malloc_base(base, baselen, path, pathlen);
Linus Torvaldsac1b3d12005-10-21 04:05:05213 struct tree_desc inner;
214 void *tree;
Linus Torvalds6fda5e52007-03-21 17:08:25215 unsigned long size;
Linus Torvaldsac1b3d12005-10-21 04:05:05216
Linus Torvalds6fda5e52007-03-21 17:08:25217 tree = read_sha1_file(sha1, &type, &size);
Nicolas Pitre21666f12007-02-26 19:55:59218 if (!tree || type != OBJ_TREE)
Linus Torvaldsac1b3d12005-10-21 04:05:05219 die("corrupt tree sha %s", sha1_to_hex(sha1));
220
Linus Torvalds6fda5e52007-03-21 17:08:25221 init_tree_desc(&inner, tree, size);
Linus Torvalds304de2d2007-03-18 03:06:24222 show_tree(opt, prefix, &inner, newbase, baselen + 1 + pathlen);
Linus Torvaldsac1b3d12005-10-21 04:05:05223
224 free(tree);
225 free(newbase);
David Rientjescf995ed2006-08-14 20:39:27226 } else {
227 opt->add_remove(opt, prefix[0], mode, sha1, base, path);
Linus Torvaldsac1b3d12005-10-21 04:05:05228 }
Linus Torvaldsac1b3d12005-10-21 04:05:05229}
230
Linus Torvalds5d865012007-03-18 22:18:30231static void skip_uninteresting(struct tree_desc *t, const char *base, int baselen, struct diff_options *opt)
232{
Junio C Hamano1d848f62007-03-22 00:00:27233 int all_interesting = 0;
Linus Torvalds5d865012007-03-18 22:18:30234 while (t->size) {
Junio C Hamano1d848f62007-03-22 00:00:27235 int show;
236
237 if (all_interesting)
238 show = 1;
239 else {
240 show = tree_entry_interesting(t, base, baselen, opt);
241 if (show == 2)
242 all_interesting = 1;
243 }
Linus Torvalds5d865012007-03-18 22:18:30244 if (!show) {
245 update_tree_entry(t);
246 continue;
247 }
248 /* Skip it all? */
249 if (show < 0)
250 t->size = 0;
251 return;
252 }
253}
254
Linus Torvaldsac1b3d12005-10-21 04:05:05255int diff_tree(struct tree_desc *t1, struct tree_desc *t2, const char *base, struct diff_options *opt)
256{
Linus Torvalds304de2d2007-03-18 03:06:24257 int baselen = strlen(base);
258
Linus Torvalds5d865012007-03-18 22:18:30259 for (;;) {
Junio C Hamano822cac02007-03-14 18:12:51260 if (opt->quiet && opt->has_changes)
261 break;
Linus Torvalds5d865012007-03-18 22:18:30262 if (opt->nr_paths) {
263 skip_uninteresting(t1, base, baselen, opt);
264 skip_uninteresting(t2, base, baselen, opt);
Linus Torvaldsac1b3d12005-10-21 04:05:05265 }
266 if (!t1->size) {
Linus Torvalds5d865012007-03-18 22:18:30267 if (!t2->size)
268 break;
Linus Torvalds304de2d2007-03-18 03:06:24269 show_entry(opt, "+", t2, base, baselen);
Linus Torvaldsac1b3d12005-10-21 04:05:05270 update_tree_entry(t2);
271 continue;
272 }
273 if (!t2->size) {
Linus Torvalds304de2d2007-03-18 03:06:24274 show_entry(opt, "-", t1, base, baselen);
Linus Torvaldsac1b3d12005-10-21 04:05:05275 update_tree_entry(t1);
276 continue;
277 }
Linus Torvalds304de2d2007-03-18 03:06:24278 switch (compare_tree_entry(t1, t2, base, baselen, opt)) {
Linus Torvaldsac1b3d12005-10-21 04:05:05279 case -1:
280 update_tree_entry(t1);
281 continue;
282 case 0:
283 update_tree_entry(t1);
284 /* Fallthrough */
285 case 1:
286 update_tree_entry(t2);
287 continue;
288 }
289 die("git-diff-tree: internal error");
290 }
291 return 0;
292}
293
Linus Torvalds750f7b62007-06-19 21:22:46294/*
295 * Does it look like the resulting diff might be due to a rename?
296 * - single entry
297 * - not a valid previous file
298 */
299static inline int diff_might_be_rename(void)
300{
301 return diff_queued_diff.nr == 1 &&
302 !DIFF_FILE_VALID(diff_queued_diff.queue[0]->one);
303}
304
305static void try_to_follow_renames(struct tree_desc *t1, struct tree_desc *t2, const char *base, struct diff_options *opt)
306{
307 struct diff_options diff_opts;
Linus Torvalds9f38e1e2007-06-21 17:22:59308 struct diff_queue_struct *q = &diff_queued_diff;
309 struct diff_filepair *choice;
310 const char *paths[1];
Linus Torvalds750f7b62007-06-19 21:22:46311 int i;
312
Linus Torvalds9f38e1e2007-06-21 17:22:59313 /* Remove the file creation entry from the diff queue, and remember it */
314 choice = q->queue[0];
315 q->nr = 0;
316
Linus Torvalds750f7b62007-06-19 21:22:46317 diff_setup(&diff_opts);
318 diff_opts.recursive = 1;
319 diff_opts.detect_rename = DIFF_DETECT_RENAME;
320 diff_opts.output_format = DIFF_FORMAT_NO_OUTPUT;
321 diff_opts.single_follow = opt->paths[0];
322 paths[0] = NULL;
323 diff_tree_setup_paths(paths, &diff_opts);
324 if (diff_setup_done(&diff_opts) < 0)
325 die("unable to set up diff options to follow renames");
326 diff_tree(t1, t2, base, &diff_opts);
327 diffcore_std(&diff_opts);
328
Linus Torvalds9f38e1e2007-06-21 17:22:59329 /* Go through the new set of filepairing, and see if we find a more interesting one */
330 for (i = 0; i < q->nr; i++) {
331 struct diff_filepair *p = q->queue[i];
Linus Torvalds750f7b62007-06-19 21:22:46332
333 /*
334 * Found a source? Not only do we use that for the new
Linus Torvalds9f38e1e2007-06-21 17:22:59335 * diff_queued_diff, we will also use that as the path in
Linus Torvalds750f7b62007-06-19 21:22:46336 * the future!
337 */
338 if ((p->status == 'R' || p->status == 'C') && !strcmp(p->two->path, opt->paths[0])) {
Linus Torvalds9f38e1e2007-06-21 17:22:59339 /* Switch the file-pairs around */
340 q->queue[i] = choice;
341 choice = p;
342
343 /* Update the path we use from now on.. */
Linus Torvalds750f7b62007-06-19 21:22:46344 opt->paths[0] = xstrdup(p->one->path);
345 diff_tree_setup_paths(opt->paths, opt);
346 break;
347 }
348 }
349
350 /*
Linus Torvalds9f38e1e2007-06-21 17:22:59351 * Then, discard all the non-relevane file pairs...
Linus Torvalds750f7b62007-06-19 21:22:46352 */
Linus Torvalds9f38e1e2007-06-21 17:22:59353 for (i = 0; i < q->nr; i++) {
354 struct diff_filepair *p = q->queue[i];
355 diff_free_filepair(p);
356 }
357
358 /*
359 * .. and re-instate the one we want (which might be either the
360 * original one, or the rename/copy we found)
361 */
362 q->queue[0] = choice;
363 q->nr = 1;
Linus Torvalds750f7b62007-06-19 21:22:46364}
365
Linus Torvaldsac1b3d12005-10-21 04:05:05366int diff_tree_sha1(const unsigned char *old, const unsigned char *new, const char *base, struct diff_options *opt)
367{
368 void *tree1, *tree2;
369 struct tree_desc t1, t2;
Linus Torvalds6fda5e52007-03-21 17:08:25370 unsigned long size1, size2;
Linus Torvaldsac1b3d12005-10-21 04:05:05371 int retval;
372
Linus Torvalds6fda5e52007-03-21 17:08:25373 tree1 = read_object_with_reference(old, tree_type, &size1, NULL);
Linus Torvaldsac1b3d12005-10-21 04:05:05374 if (!tree1)
375 die("unable to read source tree (%s)", sha1_to_hex(old));
Linus Torvalds6fda5e52007-03-21 17:08:25376 tree2 = read_object_with_reference(new, tree_type, &size2, NULL);
Linus Torvaldsac1b3d12005-10-21 04:05:05377 if (!tree2)
378 die("unable to read destination tree (%s)", sha1_to_hex(new));
Linus Torvalds6fda5e52007-03-21 17:08:25379 init_tree_desc(&t1, tree1, size1);
380 init_tree_desc(&t2, tree2, size2);
Linus Torvaldsac1b3d12005-10-21 04:05:05381 retval = diff_tree(&t1, &t2, base, opt);
Linus Torvalds750f7b62007-06-19 21:22:46382 if (opt->follow_renames && diff_might_be_rename()) {
383 init_tree_desc(&t1, tree1, size1);
384 init_tree_desc(&t2, tree2, size2);
385 try_to_follow_renames(&t1, &t2, base, opt);
386 }
Linus Torvaldsac1b3d12005-10-21 04:05:05387 free(tree1);
388 free(tree2);
389 return retval;
390}
391
Rene Scharfe2b603562006-10-26 16:52:39392int diff_root_tree_sha1(const unsigned char *new, const char *base, struct diff_options *opt)
393{
394 int retval;
395 void *tree;
Linus Torvalds6fda5e52007-03-21 17:08:25396 unsigned long size;
Rene Scharfe2b603562006-10-26 16:52:39397 struct tree_desc empty, real;
398
Linus Torvalds6fda5e52007-03-21 17:08:25399 tree = read_object_with_reference(new, tree_type, &size, NULL);
Rene Scharfe2b603562006-10-26 16:52:39400 if (!tree)
401 die("unable to read root tree (%s)", sha1_to_hex(new));
Linus Torvalds6fda5e52007-03-21 17:08:25402 init_tree_desc(&real, tree, size);
Rene Scharfe2b603562006-10-26 16:52:39403
Linus Torvalds6fda5e52007-03-21 17:08:25404 init_tree_desc(&empty, "", 0);
Rene Scharfe2b603562006-10-26 16:52:39405 retval = diff_tree(&empty, &real, base, opt);
406 free(tree);
407 return retval;
408}
409
Linus Torvaldsac1b3d12005-10-21 04:05:05410static int count_paths(const char **paths)
411{
412 int i = 0;
413 while (*paths++)
414 i++;
415 return i;
416}
417
Junio C Hamanoa8baa7b2006-04-10 23:39:11418void diff_tree_release_paths(struct diff_options *opt)
Linus Torvaldsac1b3d12005-10-21 04:05:05419{
Junio C Hamanoa8baa7b2006-04-10 23:39:11420 free(opt->pathlens);
421}
422
423void diff_tree_setup_paths(const char **p, struct diff_options *opt)
424{
425 opt->nr_paths = 0;
426 opt->pathlens = NULL;
427 opt->paths = NULL;
428
Linus Torvaldsac1b3d12005-10-21 04:05:05429 if (p) {
430 int i;
431
Junio C Hamanoa8baa7b2006-04-10 23:39:11432 opt->paths = p;
433 opt->nr_paths = count_paths(p);
434 if (opt->nr_paths == 0) {
435 opt->pathlens = NULL;
Junio C Hamano7e4a2a82005-12-26 20:34:56436 return;
437 }
Junio C Hamanoa8baa7b2006-04-10 23:39:11438 opt->pathlens = xmalloc(opt->nr_paths * sizeof(int));
439 for (i=0; i < opt->nr_paths; i++)
440 opt->pathlens[i] = strlen(p[i]);
Linus Torvaldsac1b3d12005-10-21 04:05:05441 }
442}