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(path1, sha1);
25 pathlen2 = tree_entry_len(path2, sha2);
26 cmp = base_name_compare(path1, pathlen1, mode1, path2, pathlen2, mode2);
28 show_entry(opt, "-", t1, base);
32 show_entry(opt, "+", t2, base);
35 if (!DIFF_OPT_TST(opt, FIND_COPIES_HARDER) && !hashcmp(sha1, sha2) && mode1 == mode2)
39 * If the filemode has changed to/from a directory from/to a regular
40 * file, we need to consider it a remove and an add.
42 if (S_ISDIR(mode1) != S_ISDIR(mode2)) {
43 show_entry(opt, "-", t1, base);
44 show_entry(opt, "+", t2, base);
48 strbuf_add(base, path1, pathlen1);
49 if (DIFF_OPT_TST(opt, RECURSIVE) && S_ISDIR(mode1)) {
50 if (DIFF_OPT_TST(opt, TREE_IN_RECURSIVE)) {
51 opt->change(opt, mode1, mode2,
52 sha1, sha2, base->buf, 0, 0);
54 strbuf_addch(base, '/');
55 diff_tree_sha1(sha1, sha2, base->buf, opt);
57 opt->change(opt, mode1, mode2, sha1, sha2, base->buf, 0, 0);
59 strbuf_setlen(base, old_baselen);
63 /* A whole sub-tree went away or appeared */
64 static void show_tree(struct diff_options *opt, const char *prefix,
65 struct tree_desc *desc, struct strbuf *base)
68 for (; desc->size; update_tree_entry(desc)) {
70 match = tree_entry_interesting(&desc->entry, base, 0,
77 show_entry(opt, prefix, desc, base);
81 /* A file entry went away or appeared */
82 static void show_entry(struct diff_options *opt, const char *prefix,
83 struct tree_desc *desc, struct strbuf *base)
87 const unsigned char *sha1 = tree_entry_extract(desc, &path, &mode);
88 int pathlen = tree_entry_len(path, sha1);
89 int old_baselen = base->len;
91 strbuf_add(base, path, pathlen);
92 if (DIFF_OPT_TST(opt, RECURSIVE) && S_ISDIR(mode)) {
93 enum object_type type;
94 struct tree_desc inner;
98 tree = read_sha1_file(sha1, &type, &size);
99 if (!tree || type != OBJ_TREE)
100 die("corrupt tree sha %s", sha1_to_hex(sha1));
102 if (DIFF_OPT_TST(opt, TREE_IN_RECURSIVE))
103 opt->add_remove(opt, *prefix, mode, sha1, base->buf, 0);
105 strbuf_addch(base, '/');
107 init_tree_desc(&inner, tree, size);
108 show_tree(opt, prefix, &inner, base);
111 opt->add_remove(opt, prefix[0], mode, sha1, base->buf, 0);
113 strbuf_setlen(base, old_baselen);
116 static void skip_uninteresting(struct tree_desc *t, struct strbuf *base,
117 struct diff_options *opt, int *match)
120 *match = tree_entry_interesting(&t->entry, base, 0, &opt->pathspec);
126 update_tree_entry(t);
130 int diff_tree(struct tree_desc *t1, struct tree_desc *t2,
131 const char *base_str, struct diff_options *opt)
134 int baselen = strlen(base_str);
135 int t1_match = 0, t2_match = 0;
137 /* Enable recursion indefinitely */
138 opt->pathspec.recursive = DIFF_OPT_TST(opt, RECURSIVE);
139 opt->pathspec.max_depth = -1;
141 strbuf_init(&base, PATH_MAX);
142 strbuf_add(&base, base_str, baselen);
145 if (DIFF_OPT_TST(opt, QUICK) &&
146 DIFF_OPT_TST(opt, HAS_CHANGES))
148 if (opt->pathspec.nr) {
149 skip_uninteresting(t1, &base, opt, &t1_match);
150 skip_uninteresting(t2, &base, opt, &t2_match);
155 show_entry(opt, "+", t2, &base);
156 update_tree_entry(t2);
160 show_entry(opt, "-", t1, &base);
161 update_tree_entry(t1);
164 switch (compare_tree_entry(t1, t2, &base, opt)) {
166 update_tree_entry(t1);
169 update_tree_entry(t1);
172 update_tree_entry(t2);
175 die("git diff-tree: internal error");
178 strbuf_release(&base);
183 * Does it look like the resulting diff might be due to a rename?
185 * - not a valid previous file
187 static inline int diff_might_be_rename(void)
189 return diff_queued_diff.nr == 1 &&
190 !DIFF_FILE_VALID(diff_queued_diff.queue[0]->one);
193 static void try_to_follow_renames(struct tree_desc *t1, struct tree_desc *t2, const char *base, struct diff_options *opt)
195 struct diff_options diff_opts;
196 struct diff_queue_struct *q = &diff_queued_diff;
197 struct diff_filepair *choice;
198 const char *paths[1];
201 /* Remove the file creation entry from the diff queue, and remember it */
202 choice = q->queue[0];
205 diff_setup(&diff_opts);
206 DIFF_OPT_SET(&diff_opts, RECURSIVE);
207 DIFF_OPT_SET(&diff_opts, FIND_COPIES_HARDER);
208 diff_opts.output_format = DIFF_FORMAT_NO_OUTPUT;
209 diff_opts.single_follow = opt->pathspec.raw[0];
210 diff_opts.break_opt = opt->break_opt;
212 diff_tree_setup_paths(paths, &diff_opts);
213 if (diff_setup_done(&diff_opts) < 0)
214 die("unable to set up diff options to follow renames");
215 diff_tree(t1, t2, base, &diff_opts);
216 diffcore_std(&diff_opts);
217 diff_tree_release_paths(&diff_opts);
219 /* Go through the new set of filepairing, and see if we find a more interesting one */
220 opt->found_follow = 0;
221 for (i = 0; i < q->nr; i++) {
222 struct diff_filepair *p = q->queue[i];
225 * Found a source? Not only do we use that for the new
226 * diff_queued_diff, we will also use that as the path in
229 if ((p->status == 'R' || p->status == 'C') &&
230 !strcmp(p->two->path, opt->pathspec.raw[0])) {
231 /* Switch the file-pairs around */
232 q->queue[i] = choice;
235 /* Update the path we use from now on.. */
236 diff_tree_release_paths(opt);
237 opt->pathspec.raw[0] = xstrdup(p->one->path);
238 diff_tree_setup_paths(opt->pathspec.raw, opt);
241 * The caller expects us to return a set of vanilla
242 * filepairs to let a later call to diffcore_std()
243 * it makes to sort the renames out (among other
244 * things), but we already have found renames
245 * ourselves; signal diffcore_std() not to muck with
246 * rename information.
248 opt->found_follow = 1;
254 * Then, discard all the non-relevant file pairs...
256 for (i = 0; i < q->nr; i++) {
257 struct diff_filepair *p = q->queue[i];
258 diff_free_filepair(p);
262 * .. and re-instate the one we want (which might be either the
263 * original one, or the rename/copy we found)
265 q->queue[0] = choice;
269 int diff_tree_sha1(const unsigned char *old, const unsigned char *new, const char *base, struct diff_options *opt)
272 struct tree_desc t1, t2;
273 unsigned long size1, size2;
276 tree1 = read_object_with_reference(old, tree_type, &size1, NULL);
278 die("unable to read source tree (%s)", sha1_to_hex(old));
279 tree2 = read_object_with_reference(new, tree_type, &size2, NULL);
281 die("unable to read destination tree (%s)", sha1_to_hex(new));
282 init_tree_desc(&t1, tree1, size1);
283 init_tree_desc(&t2, tree2, size2);
284 retval = diff_tree(&t1, &t2, base, opt);
285 if (!*base && DIFF_OPT_TST(opt, FOLLOW_RENAMES) && diff_might_be_rename()) {
286 init_tree_desc(&t1, tree1, size1);
287 init_tree_desc(&t2, tree2, size2);
288 try_to_follow_renames(&t1, &t2, base, opt);
295 int diff_root_tree_sha1(const unsigned char *new, const char *base, struct diff_options *opt)
300 struct tree_desc empty, real;
302 tree = read_object_with_reference(new, tree_type, &size, NULL);
304 die("unable to read root tree (%s)", sha1_to_hex(new));
305 init_tree_desc(&real, tree, size);
307 init_tree_desc(&empty, "", 0);
308 retval = diff_tree(&empty, &real, base, opt);
313 void diff_tree_release_paths(struct diff_options *opt)
315 free_pathspec(&opt->pathspec);
318 void diff_tree_setup_paths(const char **p, struct diff_options *opt)
320 init_pathspec(&opt->pathspec, p);