2 * Helper functions for tree diff generation
9 static void show_entry(struct diff_options *opt, const char *prefix,
10 struct tree_desc *desc, struct strbuf *base);
12 static int compare_tree_entry(struct tree_desc *t1, struct tree_desc *t2,
13 struct strbuf *base, struct diff_options *opt)
15 unsigned mode1, mode2;
16 const char *path1, *path2;
17 const unsigned char *sha1, *sha2;
18 int cmp, pathlen1, pathlen2;
19 int old_baselen = base->len;
21 sha1 = tree_entry_extract(t1, &path1, &mode1);
22 sha2 = tree_entry_extract(t2, &path2, &mode2);
24 pathlen1 = tree_entry_len(&t1->entry);
25 pathlen2 = tree_entry_len(&t2->entry);
28 * NOTE files and directories *always* compare differently,
29 * even when having the same name.
31 cmp = base_name_compare(path1, pathlen1, mode1, path2, pathlen2, mode2);
33 show_entry(opt, "-", t1, base);
37 show_entry(opt, "+", t2, base);
40 if (!DIFF_OPT_TST(opt, FIND_COPIES_HARDER) && !hashcmp(sha1, sha2) && mode1 == mode2)
43 strbuf_add(base, path1, pathlen1);
44 if (DIFF_OPT_TST(opt, RECURSIVE) && S_ISDIR(mode1)) {
45 if (DIFF_OPT_TST(opt, TREE_IN_RECURSIVE)) {
46 opt->change(opt, mode1, mode2,
47 sha1, sha2, 1, 1, base->buf, 0, 0);
49 strbuf_addch(base, '/');
50 diff_tree_sha1(sha1, sha2, base->buf, opt);
52 opt->change(opt, mode1, mode2, sha1, sha2, 1, 1, base->buf, 0, 0);
54 strbuf_setlen(base, old_baselen);
58 /* A whole sub-tree went away or appeared */
59 static void show_tree(struct diff_options *opt, const char *prefix,
60 struct tree_desc *desc, struct strbuf *base)
62 enum interesting match = entry_not_interesting;
63 for (; desc->size; update_tree_entry(desc)) {
64 if (match != all_entries_interesting) {
65 match = tree_entry_interesting(&desc->entry, base, 0,
67 if (match == all_entries_not_interesting)
69 if (match == entry_not_interesting)
72 show_entry(opt, prefix, desc, base);
76 /* A file entry went away or appeared */
77 static void show_entry(struct diff_options *opt, const char *prefix,
78 struct tree_desc *desc, struct strbuf *base)
82 const unsigned char *sha1 = tree_entry_extract(desc, &path, &mode);
83 int pathlen = tree_entry_len(&desc->entry);
84 int old_baselen = base->len;
86 strbuf_add(base, path, pathlen);
87 if (DIFF_OPT_TST(opt, RECURSIVE) && S_ISDIR(mode)) {
88 enum object_type type;
89 struct tree_desc inner;
93 tree = read_sha1_file(sha1, &type, &size);
94 if (!tree || type != OBJ_TREE)
95 die("corrupt tree sha %s", sha1_to_hex(sha1));
97 if (DIFF_OPT_TST(opt, TREE_IN_RECURSIVE))
98 opt->add_remove(opt, *prefix, mode, sha1, 1, base->buf, 0);
100 strbuf_addch(base, '/');
102 init_tree_desc(&inner, tree, size);
103 show_tree(opt, prefix, &inner, base);
106 opt->add_remove(opt, prefix[0], mode, sha1, 1, base->buf, 0);
108 strbuf_setlen(base, old_baselen);
111 static void skip_uninteresting(struct tree_desc *t, struct strbuf *base,
112 struct diff_options *opt,
113 enum interesting *match)
116 *match = tree_entry_interesting(&t->entry, base, 0, &opt->pathspec);
118 if (*match == all_entries_not_interesting)
122 update_tree_entry(t);
126 int diff_tree(struct tree_desc *t1, struct tree_desc *t2,
127 const char *base_str, struct diff_options *opt)
130 int baselen = strlen(base_str);
131 enum interesting t1_match = entry_not_interesting;
132 enum interesting t2_match = entry_not_interesting;
134 /* Enable recursion indefinitely */
135 opt->pathspec.recursive = DIFF_OPT_TST(opt, RECURSIVE);
137 strbuf_init(&base, PATH_MAX);
138 strbuf_add(&base, base_str, baselen);
141 if (diff_can_quit_early(opt))
143 if (opt->pathspec.nr) {
144 skip_uninteresting(t1, &base, opt, &t1_match);
145 skip_uninteresting(t2, &base, opt, &t2_match);
150 show_entry(opt, "+", t2, &base);
151 update_tree_entry(t2);
155 show_entry(opt, "-", t1, &base);
156 update_tree_entry(t1);
159 switch (compare_tree_entry(t1, t2, &base, opt)) {
161 update_tree_entry(t1);
164 update_tree_entry(t1);
167 update_tree_entry(t2);
170 die("git diff-tree: internal error");
173 strbuf_release(&base);
178 * Does it look like the resulting diff might be due to a rename?
180 * - not a valid previous file
182 static inline int diff_might_be_rename(void)
184 return diff_queued_diff.nr == 1 &&
185 !DIFF_FILE_VALID(diff_queued_diff.queue[0]->one);
188 static void try_to_follow_renames(struct tree_desc *t1, struct tree_desc *t2, const char *base, struct diff_options *opt)
190 struct diff_options diff_opts;
191 struct diff_queue_struct *q = &diff_queued_diff;
192 struct diff_filepair *choice;
196 * follow-rename code is very specific, we need exactly one
197 * path. Magic that matches more than one path is not
200 GUARD_PATHSPEC(&opt->pathspec, PATHSPEC_FROMTOP | PATHSPEC_LITERAL);
203 * We should reject wildcards as well. Unfortunately we
204 * haven't got a reliable way to detect that 'foo\*bar' in
205 * fact has no wildcards. nowildcard_len is merely a hint for
206 * optimization. Let it slip for now until wildmatch is taught
207 * about dry-run mode and returns wildcard info.
209 if (opt->pathspec.has_wildcard)
210 die("BUG:%s:%d: wildcards are not supported",
214 /* Remove the file creation entry from the diff queue, and remember it */
215 choice = q->queue[0];
218 diff_setup(&diff_opts);
219 DIFF_OPT_SET(&diff_opts, RECURSIVE);
220 DIFF_OPT_SET(&diff_opts, FIND_COPIES_HARDER);
221 diff_opts.output_format = DIFF_FORMAT_NO_OUTPUT;
222 diff_opts.single_follow = opt->pathspec.items[0].match;
223 diff_opts.break_opt = opt->break_opt;
224 diff_opts.rename_score = opt->rename_score;
225 diff_setup_done(&diff_opts);
226 diff_tree(t1, t2, base, &diff_opts);
227 diffcore_std(&diff_opts);
228 free_pathspec(&diff_opts.pathspec);
230 /* Go through the new set of filepairing, and see if we find a more interesting one */
231 opt->found_follow = 0;
232 for (i = 0; i < q->nr; i++) {
233 struct diff_filepair *p = q->queue[i];
236 * Found a source? Not only do we use that for the new
237 * diff_queued_diff, we will also use that as the path in
240 if ((p->status == 'R' || p->status == 'C') &&
241 !strcmp(p->two->path, opt->pathspec.items[0].match)) {
244 /* Switch the file-pairs around */
245 q->queue[i] = choice;
248 /* Update the path we use from now on.. */
249 path[0] = p->one->path;
251 free_pathspec(&opt->pathspec);
252 parse_pathspec(&opt->pathspec,
253 PATHSPEC_ALL_MAGIC & ~PATHSPEC_LITERAL,
254 PATHSPEC_LITERAL_PATH, "", path);
257 * The caller expects us to return a set of vanilla
258 * filepairs to let a later call to diffcore_std()
259 * it makes to sort the renames out (among other
260 * things), but we already have found renames
261 * ourselves; signal diffcore_std() not to muck with
262 * rename information.
264 opt->found_follow = 1;
270 * Then, discard all the non-relevant file pairs...
272 for (i = 0; i < q->nr; i++) {
273 struct diff_filepair *p = q->queue[i];
274 diff_free_filepair(p);
278 * .. and re-instate the one we want (which might be either the
279 * original one, or the rename/copy we found)
281 q->queue[0] = choice;
285 int diff_tree_sha1(const unsigned char *old, const unsigned char *new, const char *base, struct diff_options *opt)
288 struct tree_desc t1, t2;
289 unsigned long size1, size2;
292 tree1 = fill_tree_descriptor(&t1, old);
293 tree2 = fill_tree_descriptor(&t2, new);
296 retval = diff_tree(&t1, &t2, base, opt);
297 if (!*base && DIFF_OPT_TST(opt, FOLLOW_RENAMES) && diff_might_be_rename()) {
298 init_tree_desc(&t1, tree1, size1);
299 init_tree_desc(&t2, tree2, size2);
300 try_to_follow_renames(&t1, &t2, base, opt);
307 int diff_root_tree_sha1(const unsigned char *new, const char *base, struct diff_options *opt)
309 return diff_tree_sha1(NULL, new, base, opt);