2 * Helper functions for tree diff generation
10 static int ll_diff_tree_sha1(const unsigned char *old, const unsigned char *new,
11 struct strbuf *base, struct diff_options *opt);
14 * Compare two tree entries, taking into account only path/S_ISDIR(mode),
15 * but not their sha1's.
17 * NOTE files and directories *always* compare differently, even when having
18 * the same name - thanks to base_name_compare().
20 * NOTE empty (=invalid) descriptor(s) take part in comparison as +infty,
21 * so that they sort *after* valid tree entries.
23 * Due to this convention, if trees are scanned in sorted order, all
24 * non-empty descriptors will be processed first.
26 static int tree_entry_pathcmp(struct tree_desc *t1, struct tree_desc *t2)
28 struct name_entry *e1, *e2;
31 /* empty descriptors sort after valid tree entries */
33 return t2->size ? 1 : 0;
39 cmp = base_name_compare(e1->path, tree_entry_len(e1), e1->mode,
40 e2->path, tree_entry_len(e2), e2->mode);
45 /* convert path, t1/t2 -> opt->diff_*() callbacks */
46 static void emit_diff(struct diff_options *opt, struct strbuf *path,
47 struct tree_desc *t1, struct tree_desc *t2)
49 unsigned int mode1 = t1 ? t1->entry.mode : 0;
50 unsigned int mode2 = t2 ? t2->entry.mode : 0;
53 opt->change(opt, mode1, mode2, t1->entry.sha1, t2->entry.sha1,
54 1, 1, path->buf, 0, 0);
57 const unsigned char *sha1;
63 sha1 = t2->entry.sha1;
67 sha1 = t1->entry.sha1;
71 opt->add_remove(opt, addremove, mode, sha1, 1, path->buf, 0);
76 /* new path should be added to diff
78 * 3 cases on how/when it should be called and behaves:
80 * !t1, t2 -> path added, parent lacks it
81 * t1, !t2 -> path removed from parent
82 * t1, t2 -> path modified
84 static void show_path(struct strbuf *base, struct diff_options *opt,
85 struct tree_desc *t1, struct tree_desc *t2)
90 int old_baselen = base->len;
91 int isdir, recurse = 0, emitthis = 1;
93 /* at least something has to be valid */
97 /* path present in resulting tree */
98 tree_entry_extract(t2, &path, &mode);
99 pathlen = tree_entry_len(&t2->entry);
100 isdir = S_ISDIR(mode);
103 * a path was removed - take path from parent. Also take
104 * mode from parent, to decide on recursion.
106 tree_entry_extract(t1, &path, &mode);
107 pathlen = tree_entry_len(&t1->entry);
109 isdir = S_ISDIR(mode);
113 if (DIFF_OPT_TST(opt, RECURSIVE) && isdir) {
115 emitthis = DIFF_OPT_TST(opt, TREE_IN_RECURSIVE);
118 strbuf_add(base, path, pathlen);
121 emit_diff(opt, base, t1, t2);
124 strbuf_addch(base, '/');
125 ll_diff_tree_sha1(t1 ? t1->entry.sha1 : NULL,
126 t2 ? t2->entry.sha1 : NULL, base, opt);
129 strbuf_setlen(base, old_baselen);
132 static void skip_uninteresting(struct tree_desc *t, struct strbuf *base,
133 struct diff_options *opt)
135 enum interesting match;
138 match = tree_entry_interesting(&t->entry, base, 0, &opt->pathspec);
140 if (match == all_entries_not_interesting)
144 update_tree_entry(t);
148 static int ll_diff_tree_sha1(const unsigned char *old, const unsigned char *new,
149 struct strbuf *base, struct diff_options *opt)
151 struct tree_desc t1, t2;
152 void *t1tree, *t2tree;
154 t1tree = fill_tree_descriptor(&t1, old);
155 t2tree = fill_tree_descriptor(&t2, new);
157 /* Enable recursion indefinitely */
158 opt->pathspec.recursive = DIFF_OPT_TST(opt, RECURSIVE);
163 if (diff_can_quit_early(opt))
165 if (opt->pathspec.nr) {
166 skip_uninteresting(&t1, base, opt);
167 skip_uninteresting(&t2, base, opt);
169 if (!t1.size && !t2.size)
172 cmp = tree_entry_pathcmp(&t1, &t2);
176 if (DIFF_OPT_TST(opt, FIND_COPIES_HARDER) ||
177 hashcmp(t1.entry.sha1, t2.entry.sha1) ||
178 (t1.entry.mode != t2.entry.mode))
179 show_path(base, opt, &t1, &t2);
181 update_tree_entry(&t1);
182 update_tree_entry(&t2);
187 show_path(base, opt, &t1, /*t2=*/NULL);
188 update_tree_entry(&t1);
193 show_path(base, opt, /*t1=*/NULL, &t2);
194 update_tree_entry(&t2);
204 * Does it look like the resulting diff might be due to a rename?
206 * - not a valid previous file
208 static inline int diff_might_be_rename(void)
210 return diff_queued_diff.nr == 1 &&
211 !DIFF_FILE_VALID(diff_queued_diff.queue[0]->one);
214 static void try_to_follow_renames(const unsigned char *old, const unsigned char *new, struct strbuf *base, struct diff_options *opt)
216 struct diff_options diff_opts;
217 struct diff_queue_struct *q = &diff_queued_diff;
218 struct diff_filepair *choice;
222 * follow-rename code is very specific, we need exactly one
223 * path. Magic that matches more than one path is not
226 GUARD_PATHSPEC(&opt->pathspec, PATHSPEC_FROMTOP | PATHSPEC_LITERAL);
229 * We should reject wildcards as well. Unfortunately we
230 * haven't got a reliable way to detect that 'foo\*bar' in
231 * fact has no wildcards. nowildcard_len is merely a hint for
232 * optimization. Let it slip for now until wildmatch is taught
233 * about dry-run mode and returns wildcard info.
235 if (opt->pathspec.has_wildcard)
236 die("BUG:%s:%d: wildcards are not supported",
240 /* Remove the file creation entry from the diff queue, and remember it */
241 choice = q->queue[0];
244 diff_setup(&diff_opts);
245 DIFF_OPT_SET(&diff_opts, RECURSIVE);
246 DIFF_OPT_SET(&diff_opts, FIND_COPIES_HARDER);
247 diff_opts.output_format = DIFF_FORMAT_NO_OUTPUT;
248 diff_opts.single_follow = opt->pathspec.items[0].match;
249 diff_opts.break_opt = opt->break_opt;
250 diff_opts.rename_score = opt->rename_score;
251 diff_setup_done(&diff_opts);
252 ll_diff_tree_sha1(old, new, base, &diff_opts);
253 diffcore_std(&diff_opts);
254 free_pathspec(&diff_opts.pathspec);
256 /* Go through the new set of filepairing, and see if we find a more interesting one */
257 opt->found_follow = 0;
258 for (i = 0; i < q->nr; i++) {
259 struct diff_filepair *p = q->queue[i];
262 * Found a source? Not only do we use that for the new
263 * diff_queued_diff, we will also use that as the path in
266 if ((p->status == 'R' || p->status == 'C') &&
267 !strcmp(p->two->path, opt->pathspec.items[0].match)) {
270 /* Switch the file-pairs around */
271 q->queue[i] = choice;
274 /* Update the path we use from now on.. */
275 path[0] = p->one->path;
277 free_pathspec(&opt->pathspec);
278 parse_pathspec(&opt->pathspec,
279 PATHSPEC_ALL_MAGIC & ~PATHSPEC_LITERAL,
280 PATHSPEC_LITERAL_PATH, "", path);
283 * The caller expects us to return a set of vanilla
284 * filepairs to let a later call to diffcore_std()
285 * it makes to sort the renames out (among other
286 * things), but we already have found renames
287 * ourselves; signal diffcore_std() not to muck with
288 * rename information.
290 opt->found_follow = 1;
296 * Then, discard all the non-relevant file pairs...
298 for (i = 0; i < q->nr; i++) {
299 struct diff_filepair *p = q->queue[i];
300 diff_free_filepair(p);
304 * .. and re-instate the one we want (which might be either the
305 * original one, or the rename/copy we found)
307 q->queue[0] = choice;
311 int diff_tree_sha1(const unsigned char *old, const unsigned char *new, const char *base_str, struct diff_options *opt)
316 strbuf_init(&base, PATH_MAX);
317 strbuf_addstr(&base, base_str);
319 retval = ll_diff_tree_sha1(old, new, &base, opt);
320 if (!*base_str && DIFF_OPT_TST(opt, FOLLOW_RENAMES) && diff_might_be_rename())
321 try_to_follow_renames(old, new, &base, opt);
323 strbuf_release(&base);
328 int diff_root_tree_sha1(const unsigned char *new, const char *base, struct diff_options *opt)
330 return diff_tree_sha1(NULL, new, base, opt);