Code

tree_entry_interesting(): remove dependency on struct diff_options
[git.git] / tree-diff.c
1 /*
2  * Helper functions for tree diff generation
3  */
4 #include "cache.h"
5 #include "diff.h"
6 #include "diffcore.h"
7 #include "tree.h"
9 static char *malloc_base(const char *base, int baselen, const char *path, int pathlen)
10 {
11         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 }
18 static char *malloc_fullname(const char *base, int baselen, const char *path, int pathlen)
19 {
20         char *fullname = xmalloc(baselen + pathlen + 1);
21         memcpy(fullname, base, baselen);
22         memcpy(fullname + baselen, path, pathlen);
23         fullname[baselen + pathlen] = 0;
24         return fullname;
25 }
27 static void show_entry(struct diff_options *opt, const char *prefix, struct tree_desc *desc,
28                        const char *base, int baselen);
30 static int compare_tree_entry(struct tree_desc *t1, struct tree_desc *t2, const char *base, int baselen, struct diff_options *opt)
31 {
32         unsigned mode1, mode2;
33         const char *path1, *path2;
34         const unsigned char *sha1, *sha2;
35         int cmp, pathlen1, pathlen2;
36         char *fullname;
38         sha1 = tree_entry_extract(t1, &path1, &mode1);
39         sha2 = tree_entry_extract(t2, &path2, &mode2);
41         pathlen1 = tree_entry_len(path1, sha1);
42         pathlen2 = tree_entry_len(path2, sha2);
43         cmp = base_name_compare(path1, pathlen1, mode1, path2, pathlen2, mode2);
44         if (cmp < 0) {
45                 show_entry(opt, "-", t1, base, baselen);
46                 return -1;
47         }
48         if (cmp > 0) {
49                 show_entry(opt, "+", t2, base, baselen);
50                 return 1;
51         }
52         if (!DIFF_OPT_TST(opt, FIND_COPIES_HARDER) && !hashcmp(sha1, sha2) && mode1 == mode2)
53                 return 0;
55         /*
56          * If the filemode has changed to/from a directory from/to a regular
57          * file, we need to consider it a remove and an add.
58          */
59         if (S_ISDIR(mode1) != S_ISDIR(mode2)) {
60                 show_entry(opt, "-", t1, base, baselen);
61                 show_entry(opt, "+", t2, base, baselen);
62                 return 0;
63         }
65         if (DIFF_OPT_TST(opt, RECURSIVE) && S_ISDIR(mode1)) {
66                 int retval;
67                 char *newbase = malloc_base(base, baselen, path1, pathlen1);
68                 if (DIFF_OPT_TST(opt, TREE_IN_RECURSIVE)) {
69                         newbase[baselen + pathlen1] = 0;
70                         opt->change(opt, mode1, mode2,
71                                     sha1, sha2, newbase, 0, 0);
72                         newbase[baselen + pathlen1] = '/';
73                 }
74                 retval = diff_tree_sha1(sha1, sha2, newbase, opt);
75                 free(newbase);
76                 return retval;
77         }
79         fullname = malloc_fullname(base, baselen, path1, pathlen1);
80         opt->change(opt, mode1, mode2, sha1, sha2, fullname, 0, 0);
81         free(fullname);
82         return 0;
83 }
85 /*
86  * Is a tree entry interesting given the pathspec we have?
87  *
88  * Pre-condition: baselen == 0 || base[baselen-1] == '/'
89  *
90  * Return:
91  *  - 2 for "yes, and all subsequent entries will be"
92  *  - 1 for yes
93  *  - zero for no
94  *  - negative for "no, and no subsequent entries will be either"
95  */
96 static int tree_entry_interesting(const struct name_entry *entry, const char *base, int baselen, const struct pathspec *ps)
97 {
98         int i;
99         int pathlen;
100         int never_interesting = -1;
102         if (!ps || !ps->nr)
103                 return 2;
105         pathlen = tree_entry_len(entry->path, entry->sha1);
107         for (i = 0; i < ps->nr; i++) {
108                 const struct pathspec_item *item = ps->items+i;
109                 const char *match = item->match;
110                 int matchlen = item->len;
111                 int m = -1; /* signals that we haven't called strncmp() */
113                 if (baselen >= matchlen) {
114                         /* If it doesn't match, move along... */
115                         if (strncmp(base, match, matchlen))
116                                 continue;
118                         /*
119                          * If the base is a subdirectory of a path which
120                          * was specified, all of them are interesting.
121                          */
122                         if (!matchlen ||
123                             base[matchlen] == '/' ||
124                             match[matchlen - 1] == '/')
125                                 return 2;
127                         /* Just a random prefix match */
128                         continue;
129                 }
131                 /* Does the base match? */
132                 if (strncmp(base, match, baselen))
133                         continue;
135                 match += baselen;
136                 matchlen -= baselen;
138                 if (never_interesting) {
139                         /*
140                          * We have not seen any match that sorts later
141                          * than the current path.
142                          */
144                         /*
145                          * Does match sort strictly earlier than path
146                          * with their common parts?
147                          */
148                         m = strncmp(match, entry->path,
149                                     (matchlen < pathlen) ? matchlen : pathlen);
150                         if (m < 0)
151                                 continue;
153                         /*
154                          * If we come here even once, that means there is at
155                          * least one pathspec that would sort equal to or
156                          * later than the path we are currently looking at.
157                          * In other words, if we have never reached this point
158                          * after iterating all pathspecs, it means all
159                          * pathspecs are either outside of base, or inside the
160                          * base but sorts strictly earlier than the current
161                          * one.  In either case, they will never match the
162                          * subsequent entries.  In such a case, we initialized
163                          * the variable to -1 and that is what will be
164                          * returned, allowing the caller to terminate early.
165                          */
166                         never_interesting = 0;
167                 }
169                 if (pathlen > matchlen)
170                         continue;
172                 if (matchlen > pathlen) {
173                         if (match[pathlen] != '/')
174                                 continue;
175                         if (!S_ISDIR(entry->mode))
176                                 continue;
177                 }
179                 if (m == -1)
180                         /*
181                          * we cheated and did not do strncmp(), so we do
182                          * that here.
183                          */
184                         m = strncmp(match, entry->path, pathlen);
186                 /*
187                  * If common part matched earlier then it is a hit,
188                  * because we rejected the case where path is not a
189                  * leading directory and is shorter than match.
190                  */
191                 if (!m)
192                         return 1;
193         }
194         return never_interesting; /* No matches */
197 /* A whole sub-tree went away or appeared */
198 static void show_tree(struct diff_options *opt, const char *prefix, struct tree_desc *desc, const char *base, int baselen)
200         int all_interesting = 0;
201         while (desc->size) {
202                 int show;
204                 if (all_interesting)
205                         show = 1;
206                 else {
207                         show = tree_entry_interesting(&desc->entry, base, baselen, &opt->pathspec);
208                         if (show == 2)
209                                 all_interesting = 1;
210                 }
211                 if (show < 0)
212                         break;
213                 if (show)
214                         show_entry(opt, prefix, desc, base, baselen);
215                 update_tree_entry(desc);
216         }
219 /* A file entry went away or appeared */
220 static void show_entry(struct diff_options *opt, const char *prefix, struct tree_desc *desc,
221                        const char *base, int baselen)
223         unsigned mode;
224         const char *path;
225         const unsigned char *sha1 = tree_entry_extract(desc, &path, &mode);
226         int pathlen = tree_entry_len(path, sha1);
228         if (DIFF_OPT_TST(opt, RECURSIVE) && S_ISDIR(mode)) {
229                 enum object_type type;
230                 char *newbase = malloc_base(base, baselen, path, pathlen);
231                 struct tree_desc inner;
232                 void *tree;
233                 unsigned long size;
235                 tree = read_sha1_file(sha1, &type, &size);
236                 if (!tree || type != OBJ_TREE)
237                         die("corrupt tree sha %s", sha1_to_hex(sha1));
239                 if (DIFF_OPT_TST(opt, TREE_IN_RECURSIVE)) {
240                         newbase[baselen + pathlen] = 0;
241                         opt->add_remove(opt, *prefix, mode, sha1, newbase, 0);
242                         newbase[baselen + pathlen] = '/';
243                 }
245                 init_tree_desc(&inner, tree, size);
246                 show_tree(opt, prefix, &inner, newbase, baselen + 1 + pathlen);
248                 free(tree);
249                 free(newbase);
250         } else {
251                 char *fullname = malloc_fullname(base, baselen, path, pathlen);
252                 opt->add_remove(opt, prefix[0], mode, sha1, fullname, 0);
253                 free(fullname);
254         }
257 static void skip_uninteresting(struct tree_desc *t, const char *base, int baselen, struct diff_options *opt, int *all_interesting)
259         while (t->size) {
260                 int show = tree_entry_interesting(&t->entry, base, baselen, &opt->pathspec);
261                 if (show == 2)
262                         *all_interesting = 1;
263                 if (!show) {
264                         update_tree_entry(t);
265                         continue;
266                 }
267                 /* Skip it all? */
268                 if (show < 0)
269                         t->size = 0;
270                 return;
271         }
274 int diff_tree(struct tree_desc *t1, struct tree_desc *t2, const char *base, struct diff_options *opt)
276         int baselen = strlen(base);
277         int all_t1_interesting = 0;
278         int all_t2_interesting = 0;
280         for (;;) {
281                 if (DIFF_OPT_TST(opt, QUICK) &&
282                     DIFF_OPT_TST(opt, HAS_CHANGES))
283                         break;
284                 if (opt->pathspec.nr) {
285                         if (!all_t1_interesting)
286                                 skip_uninteresting(t1, base, baselen, opt,
287                                                    &all_t1_interesting);
288                         if (!all_t2_interesting)
289                                 skip_uninteresting(t2, base, baselen, opt,
290                                                    &all_t2_interesting);
291                 }
292                 if (!t1->size) {
293                         if (!t2->size)
294                                 break;
295                         show_entry(opt, "+", t2, base, baselen);
296                         update_tree_entry(t2);
297                         continue;
298                 }
299                 if (!t2->size) {
300                         show_entry(opt, "-", t1, base, baselen);
301                         update_tree_entry(t1);
302                         continue;
303                 }
304                 switch (compare_tree_entry(t1, t2, base, baselen, opt)) {
305                 case -1:
306                         update_tree_entry(t1);
307                         continue;
308                 case 0:
309                         update_tree_entry(t1);
310                         /* Fallthrough */
311                 case 1:
312                         update_tree_entry(t2);
313                         continue;
314                 }
315                 die("git diff-tree: internal error");
316         }
317         return 0;
320 /*
321  * Does it look like the resulting diff might be due to a rename?
322  *  - single entry
323  *  - not a valid previous file
324  */
325 static inline int diff_might_be_rename(void)
327         return diff_queued_diff.nr == 1 &&
328                 !DIFF_FILE_VALID(diff_queued_diff.queue[0]->one);
331 static void try_to_follow_renames(struct tree_desc *t1, struct tree_desc *t2, const char *base, struct diff_options *opt)
333         struct diff_options diff_opts;
334         struct diff_queue_struct *q = &diff_queued_diff;
335         struct diff_filepair *choice;
336         const char *paths[1];
337         int i;
339         /* Remove the file creation entry from the diff queue, and remember it */
340         choice = q->queue[0];
341         q->nr = 0;
343         diff_setup(&diff_opts);
344         DIFF_OPT_SET(&diff_opts, RECURSIVE);
345         DIFF_OPT_SET(&diff_opts, FIND_COPIES_HARDER);
346         diff_opts.output_format = DIFF_FORMAT_NO_OUTPUT;
347         diff_opts.single_follow = opt->pathspec.raw[0];
348         diff_opts.break_opt = opt->break_opt;
349         paths[0] = NULL;
350         diff_tree_setup_paths(paths, &diff_opts);
351         if (diff_setup_done(&diff_opts) < 0)
352                 die("unable to set up diff options to follow renames");
353         diff_tree(t1, t2, base, &diff_opts);
354         diffcore_std(&diff_opts);
355         diff_tree_release_paths(&diff_opts);
357         /* Go through the new set of filepairing, and see if we find a more interesting one */
358         opt->found_follow = 0;
359         for (i = 0; i < q->nr; i++) {
360                 struct diff_filepair *p = q->queue[i];
362                 /*
363                  * Found a source? Not only do we use that for the new
364                  * diff_queued_diff, we will also use that as the path in
365                  * the future!
366                  */
367                 if ((p->status == 'R' || p->status == 'C') &&
368                     !strcmp(p->two->path, opt->pathspec.raw[0])) {
369                         /* Switch the file-pairs around */
370                         q->queue[i] = choice;
371                         choice = p;
373                         /* Update the path we use from now on.. */
374                         diff_tree_release_paths(opt);
375                         opt->pathspec.raw[0] = xstrdup(p->one->path);
376                         diff_tree_setup_paths(opt->pathspec.raw, opt);
378                         /*
379                          * The caller expects us to return a set of vanilla
380                          * filepairs to let a later call to diffcore_std()
381                          * it makes to sort the renames out (among other
382                          * things), but we already have found renames
383                          * ourselves; signal diffcore_std() not to muck with
384                          * rename information.
385                          */
386                         opt->found_follow = 1;
387                         break;
388                 }
389         }
391         /*
392          * Then, discard all the non-relevant file pairs...
393          */
394         for (i = 0; i < q->nr; i++) {
395                 struct diff_filepair *p = q->queue[i];
396                 diff_free_filepair(p);
397         }
399         /*
400          * .. and re-instate the one we want (which might be either the
401          * original one, or the rename/copy we found)
402          */
403         q->queue[0] = choice;
404         q->nr = 1;
407 int diff_tree_sha1(const unsigned char *old, const unsigned char *new, const char *base, struct diff_options *opt)
409         void *tree1, *tree2;
410         struct tree_desc t1, t2;
411         unsigned long size1, size2;
412         int retval;
414         tree1 = read_object_with_reference(old, tree_type, &size1, NULL);
415         if (!tree1)
416                 die("unable to read source tree (%s)", sha1_to_hex(old));
417         tree2 = read_object_with_reference(new, tree_type, &size2, NULL);
418         if (!tree2)
419                 die("unable to read destination tree (%s)", sha1_to_hex(new));
420         init_tree_desc(&t1, tree1, size1);
421         init_tree_desc(&t2, tree2, size2);
422         retval = diff_tree(&t1, &t2, base, opt);
423         if (!*base && DIFF_OPT_TST(opt, FOLLOW_RENAMES) && diff_might_be_rename()) {
424                 init_tree_desc(&t1, tree1, size1);
425                 init_tree_desc(&t2, tree2, size2);
426                 try_to_follow_renames(&t1, &t2, base, opt);
427         }
428         free(tree1);
429         free(tree2);
430         return retval;
433 int diff_root_tree_sha1(const unsigned char *new, const char *base, struct diff_options *opt)
435         int retval;
436         void *tree;
437         unsigned long size;
438         struct tree_desc empty, real;
440         tree = read_object_with_reference(new, tree_type, &size, NULL);
441         if (!tree)
442                 die("unable to read root tree (%s)", sha1_to_hex(new));
443         init_tree_desc(&real, tree, size);
445         init_tree_desc(&empty, "", 0);
446         retval = diff_tree(&empty, &real, base, opt);
447         free(tree);
448         return retval;
451 void diff_tree_release_paths(struct diff_options *opt)
453         free_pathspec(&opt->pathspec);
456 void diff_tree_setup_paths(const char **p, struct diff_options *opt)
458         init_pathspec(&opt->pathspec, p);