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)
114 enum interesting match;
117 match = tree_entry_interesting(&t->entry, base, 0, &opt->pathspec);
119 if (match == all_entries_not_interesting)
123 update_tree_entry(t);
127 int diff_tree(struct tree_desc *t1, struct tree_desc *t2,
128 const char *base_str, struct diff_options *opt)
131 int baselen = strlen(base_str);
133 /* Enable recursion indefinitely */
134 opt->pathspec.recursive = DIFF_OPT_TST(opt, RECURSIVE);
136 strbuf_init(&base, PATH_MAX);
137 strbuf_add(&base, base_str, baselen);
140 if (diff_can_quit_early(opt))
142 if (opt->pathspec.nr) {
143 skip_uninteresting(t1, &base, opt);
144 skip_uninteresting(t2, &base, opt);
149 show_entry(opt, "+", t2, &base);
150 update_tree_entry(t2);
154 show_entry(opt, "-", t1, &base);
155 update_tree_entry(t1);
158 switch (compare_tree_entry(t1, t2, &base, opt)) {
160 update_tree_entry(t1);
163 update_tree_entry(t1);
166 update_tree_entry(t2);
169 die("git diff-tree: internal error");
172 strbuf_release(&base);
177 * Does it look like the resulting diff might be due to a rename?
179 * - not a valid previous file
181 static inline int diff_might_be_rename(void)
183 return diff_queued_diff.nr == 1 &&
184 !DIFF_FILE_VALID(diff_queued_diff.queue[0]->one);
187 static void try_to_follow_renames(struct tree_desc *t1, struct tree_desc *t2, const char *base, struct diff_options *opt)
189 struct diff_options diff_opts;
190 struct diff_queue_struct *q = &diff_queued_diff;
191 struct diff_filepair *choice;
195 * follow-rename code is very specific, we need exactly one
196 * path. Magic that matches more than one path is not
199 GUARD_PATHSPEC(&opt->pathspec, PATHSPEC_FROMTOP | PATHSPEC_LITERAL);
202 * We should reject wildcards as well. Unfortunately we
203 * haven't got a reliable way to detect that 'foo\*bar' in
204 * fact has no wildcards. nowildcard_len is merely a hint for
205 * optimization. Let it slip for now until wildmatch is taught
206 * about dry-run mode and returns wildcard info.
208 if (opt->pathspec.has_wildcard)
209 die("BUG:%s:%d: wildcards are not supported",
213 /* Remove the file creation entry from the diff queue, and remember it */
214 choice = q->queue[0];
217 diff_setup(&diff_opts);
218 DIFF_OPT_SET(&diff_opts, RECURSIVE);
219 DIFF_OPT_SET(&diff_opts, FIND_COPIES_HARDER);
220 diff_opts.output_format = DIFF_FORMAT_NO_OUTPUT;
221 diff_opts.single_follow = opt->pathspec.items[0].match;
222 diff_opts.break_opt = opt->break_opt;
223 diff_opts.rename_score = opt->rename_score;
224 diff_setup_done(&diff_opts);
225 diff_tree(t1, t2, base, &diff_opts);
226 diffcore_std(&diff_opts);
227 free_pathspec(&diff_opts.pathspec);
229 /* Go through the new set of filepairing, and see if we find a more interesting one */
230 opt->found_follow = 0;
231 for (i = 0; i < q->nr; i++) {
232 struct diff_filepair *p = q->queue[i];
235 * Found a source? Not only do we use that for the new
236 * diff_queued_diff, we will also use that as the path in
239 if ((p->status == 'R' || p->status == 'C') &&
240 !strcmp(p->two->path, opt->pathspec.items[0].match)) {
243 /* Switch the file-pairs around */
244 q->queue[i] = choice;
247 /* Update the path we use from now on.. */
248 path[0] = p->one->path;
250 free_pathspec(&opt->pathspec);
251 parse_pathspec(&opt->pathspec,
252 PATHSPEC_ALL_MAGIC & ~PATHSPEC_LITERAL,
253 PATHSPEC_LITERAL_PATH, "", path);
256 * The caller expects us to return a set of vanilla
257 * filepairs to let a later call to diffcore_std()
258 * it makes to sort the renames out (among other
259 * things), but we already have found renames
260 * ourselves; signal diffcore_std() not to muck with
261 * rename information.
263 opt->found_follow = 1;
269 * Then, discard all the non-relevant file pairs...
271 for (i = 0; i < q->nr; i++) {
272 struct diff_filepair *p = q->queue[i];
273 diff_free_filepair(p);
277 * .. and re-instate the one we want (which might be either the
278 * original one, or the rename/copy we found)
280 q->queue[0] = choice;
284 int diff_tree_sha1(const unsigned char *old, const unsigned char *new, const char *base, struct diff_options *opt)
287 struct tree_desc t1, t2;
288 unsigned long size1, size2;
291 tree1 = fill_tree_descriptor(&t1, old);
292 tree2 = fill_tree_descriptor(&t2, new);
295 retval = diff_tree(&t1, &t2, base, opt);
296 if (!*base && DIFF_OPT_TST(opt, FOLLOW_RENAMES) && diff_might_be_rename()) {
297 init_tree_desc(&t1, tree1, size1);
298 init_tree_desc(&t2, tree2, size2);
299 try_to_follow_renames(&t1, &t2, base, opt);
306 int diff_root_tree_sha1(const unsigned char *new, const char *base, struct diff_options *opt)
308 return diff_tree_sha1(NULL, new, base, opt);