]> git.ipfire.org Git - thirdparty/git.git/blame - tree-diff.c
Merge branch 'maint'
[thirdparty/git.git] / tree-diff.c
CommitLineData
ac1b3d12
LT
1/*
2 * Helper functions for tree diff generation
3 */
4#include "cache.h"
5#include "diff.h"
750f7b66 6#include "diffcore.h"
8e440259 7#include "tree.h"
ac1b3d12 8
48932677
NTND
9static void show_entry(struct diff_options *opt, const char *prefix,
10 struct tree_desc *desc, struct strbuf *base);
ac1b3d12 11
48932677
NTND
12static int compare_tree_entry(struct tree_desc *t1, struct tree_desc *t2,
13 struct strbuf *base, struct diff_options *opt)
ac1b3d12
LT
14{
15 unsigned mode1, mode2;
16 const char *path1, *path2;
17 const unsigned char *sha1, *sha2;
18 int cmp, pathlen1, pathlen2;
48932677 19 int old_baselen = base->len;
ac1b3d12 20
50f9a858
LT
21 sha1 = tree_entry_extract(t1, &path1, &mode1);
22 sha2 = tree_entry_extract(t2, &path2, &mode2);
ac1b3d12 23
304de2d2
LT
24 pathlen1 = tree_entry_len(path1, sha1);
25 pathlen2 = tree_entry_len(path2, sha2);
ac1b3d12
LT
26 cmp = base_name_compare(path1, pathlen1, mode1, path2, pathlen2, mode2);
27 if (cmp < 0) {
48932677 28 show_entry(opt, "-", t1, base);
ac1b3d12
LT
29 return -1;
30 }
31 if (cmp > 0) {
48932677 32 show_entry(opt, "+", t2, base);
ac1b3d12
LT
33 return 1;
34 }
8f67f8ae 35 if (!DIFF_OPT_TST(opt, FIND_COPIES_HARDER) && !hashcmp(sha1, sha2) && mode1 == mode2)
ac1b3d12
LT
36 return 0;
37
38 /*
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.
41 */
42 if (S_ISDIR(mode1) != S_ISDIR(mode2)) {
48932677
NTND
43 show_entry(opt, "-", t1, base);
44 show_entry(opt, "+", t2, base);
ac1b3d12
LT
45 return 0;
46 }
47
48932677 48 strbuf_add(base, path1, pathlen1);
8f67f8ae 49 if (DIFF_OPT_TST(opt, RECURSIVE) && S_ISDIR(mode1)) {
fd55a19e 50 if (DIFF_OPT_TST(opt, TREE_IN_RECURSIVE)) {
ac1b3d12 51 opt->change(opt, mode1, mode2,
48932677 52 sha1, sha2, base->buf, 0, 0);
fd55a19e 53 }
48932677 54 strbuf_addch(base, '/');
c0aa335c 55 diff_tree_sha1(sha1, sha2, base->buf, opt);
48932677
NTND
56 } else {
57 opt->change(opt, mode1, mode2, sha1, sha2, base->buf, 0, 0);
ac1b3d12 58 }
48932677 59 strbuf_setlen(base, old_baselen);
ac1b3d12
LT
60 return 0;
61}
62
ac1b3d12 63/* A whole sub-tree went away or appeared */
48932677
NTND
64static void show_tree(struct diff_options *opt, const char *prefix,
65 struct tree_desc *desc, struct strbuf *base)
ac1b3d12 66{
97d0b74a
NTND
67 int match = 0;
68 for (; desc->size; update_tree_entry(desc)) {
69 if (match != 2) {
70 match = tree_entry_interesting(&desc->entry, base, 0,
71 &opt->pathspec);
72 if (match < 0)
73 break;
74 if (match == 0)
75 continue;
1d848f64 76 }
97d0b74a 77 show_entry(opt, prefix, desc, base);
ac1b3d12
LT
78 }
79}
80
81/* A file entry went away or appeared */
48932677
NTND
82static void show_entry(struct diff_options *opt, const char *prefix,
83 struct tree_desc *desc, struct strbuf *base)
ac1b3d12
LT
84{
85 unsigned mode;
86 const char *path;
50f9a858 87 const unsigned char *sha1 = tree_entry_extract(desc, &path, &mode);
fd55a19e 88 int pathlen = tree_entry_len(path, sha1);
48932677 89 int old_baselen = base->len;
ac1b3d12 90
48932677 91 strbuf_add(base, path, pathlen);
8f67f8ae 92 if (DIFF_OPT_TST(opt, RECURSIVE) && S_ISDIR(mode)) {
21666f1a 93 enum object_type type;
ac1b3d12
LT
94 struct tree_desc inner;
95 void *tree;
6fda5e51 96 unsigned long size;
ac1b3d12 97
6fda5e51 98 tree = read_sha1_file(sha1, &type, &size);
21666f1a 99 if (!tree || type != OBJ_TREE)
ac1b3d12
LT
100 die("corrupt tree sha %s", sha1_to_hex(sha1));
101
48932677
NTND
102 if (DIFF_OPT_TST(opt, TREE_IN_RECURSIVE))
103 opt->add_remove(opt, *prefix, mode, sha1, base->buf, 0);
df533f34 104
48932677 105 strbuf_addch(base, '/');
ac1b3d12 106
48932677
NTND
107 init_tree_desc(&inner, tree, size);
108 show_tree(opt, prefix, &inner, base);
ac1b3d12 109 free(tree);
48932677
NTND
110 } else
111 opt->add_remove(opt, prefix[0], mode, sha1, base->buf, 0);
112
113 strbuf_setlen(base, old_baselen);
ac1b3d12
LT
114}
115
48932677 116static void skip_uninteresting(struct tree_desc *t, struct strbuf *base,
97d0b74a 117 struct diff_options *opt, int *match)
5d865017
LT
118{
119 while (t->size) {
97d0b74a
NTND
120 *match = tree_entry_interesting(&t->entry, base, 0, &opt->pathspec);
121 if (*match) {
122 if (*match < 0)
123 t->size = 0;
124 break;
5d865017 125 }
97d0b74a 126 update_tree_entry(t);
5d865017
LT
127 }
128}
129
48932677
NTND
130int diff_tree(struct tree_desc *t1, struct tree_desc *t2,
131 const char *base_str, struct diff_options *opt)
ac1b3d12 132{
48932677
NTND
133 struct strbuf base;
134 int baselen = strlen(base_str);
97d0b74a 135 int t1_match = 0, t2_match = 0;
304de2d2 136
bc96cc87
NTND
137 /* Enable recursion indefinitely */
138 opt->pathspec.recursive = DIFF_OPT_TST(opt, RECURSIVE);
139 opt->pathspec.max_depth = -1;
140
48932677
NTND
141 strbuf_init(&base, PATH_MAX);
142 strbuf_add(&base, base_str, baselen);
143
5d865017 144 for (;;) {
28b9264d 145 if (diff_can_quit_early(opt))
822cac01 146 break;
66f13625 147 if (opt->pathspec.nr) {
97d0b74a
NTND
148 skip_uninteresting(t1, &base, opt, &t1_match);
149 skip_uninteresting(t2, &base, opt, &t2_match);
ac1b3d12
LT
150 }
151 if (!t1->size) {
5d865017
LT
152 if (!t2->size)
153 break;
48932677 154 show_entry(opt, "+", t2, &base);
ac1b3d12
LT
155 update_tree_entry(t2);
156 continue;
157 }
158 if (!t2->size) {
48932677 159 show_entry(opt, "-", t1, &base);
ac1b3d12
LT
160 update_tree_entry(t1);
161 continue;
162 }
48932677 163 switch (compare_tree_entry(t1, t2, &base, opt)) {
ac1b3d12
LT
164 case -1:
165 update_tree_entry(t1);
166 continue;
167 case 0:
168 update_tree_entry(t1);
169 /* Fallthrough */
170 case 1:
171 update_tree_entry(t2);
172 continue;
173 }
7e44c935 174 die("git diff-tree: internal error");
ac1b3d12 175 }
48932677
NTND
176
177 strbuf_release(&base);
ac1b3d12
LT
178 return 0;
179}
180
750f7b66
LT
181/*
182 * Does it look like the resulting diff might be due to a rename?
183 * - single entry
184 * - not a valid previous file
185 */
186static inline int diff_might_be_rename(void)
187{
188 return diff_queued_diff.nr == 1 &&
189 !DIFF_FILE_VALID(diff_queued_diff.queue[0]->one);
190}
191
192static void try_to_follow_renames(struct tree_desc *t1, struct tree_desc *t2, const char *base, struct diff_options *opt)
193{
194 struct diff_options diff_opts;
9f38e1ef
LT
195 struct diff_queue_struct *q = &diff_queued_diff;
196 struct diff_filepair *choice;
197 const char *paths[1];
750f7b66
LT
198 int i;
199
9f38e1ef
LT
200 /* Remove the file creation entry from the diff queue, and remember it */
201 choice = q->queue[0];
202 q->nr = 0;
203
750f7b66 204 diff_setup(&diff_opts);
8f67f8ae 205 DIFF_OPT_SET(&diff_opts, RECURSIVE);
0cdca133 206 DIFF_OPT_SET(&diff_opts, FIND_COPIES_HARDER);
750f7b66 207 diff_opts.output_format = DIFF_FORMAT_NO_OUTPUT;
66f13625 208 diff_opts.single_follow = opt->pathspec.raw[0];
6dd4b66f 209 diff_opts.break_opt = opt->break_opt;
750f7b66
LT
210 paths[0] = NULL;
211 diff_tree_setup_paths(paths, &diff_opts);
212 if (diff_setup_done(&diff_opts) < 0)
213 die("unable to set up diff options to follow renames");
214 diff_tree(t1, t2, base, &diff_opts);
215 diffcore_std(&diff_opts);
03b69c76 216 diff_tree_release_paths(&diff_opts);
750f7b66 217
9f38e1ef 218 /* Go through the new set of filepairing, and see if we find a more interesting one */
44c48a90 219 opt->found_follow = 0;
9f38e1ef
LT
220 for (i = 0; i < q->nr; i++) {
221 struct diff_filepair *p = q->queue[i];
750f7b66
LT
222
223 /*
224 * Found a source? Not only do we use that for the new
9f38e1ef 225 * diff_queued_diff, we will also use that as the path in
750f7b66
LT
226 * the future!
227 */
66f13625
NTND
228 if ((p->status == 'R' || p->status == 'C') &&
229 !strcmp(p->two->path, opt->pathspec.raw[0])) {
9f38e1ef
LT
230 /* Switch the file-pairs around */
231 q->queue[i] = choice;
232 choice = p;
233
234 /* Update the path we use from now on.. */
03b69c76 235 diff_tree_release_paths(opt);
66f13625
NTND
236 opt->pathspec.raw[0] = xstrdup(p->one->path);
237 diff_tree_setup_paths(opt->pathspec.raw, opt);
44c48a90
JH
238
239 /*
240 * The caller expects us to return a set of vanilla
241 * filepairs to let a later call to diffcore_std()
242 * it makes to sort the renames out (among other
243 * things), but we already have found renames
244 * ourselves; signal diffcore_std() not to muck with
245 * rename information.
246 */
247 opt->found_follow = 1;
750f7b66
LT
248 break;
249 }
250 }
251
252 /*
3ea3c215 253 * Then, discard all the non-relevant file pairs...
9f38e1ef
LT
254 */
255 for (i = 0; i < q->nr; i++) {
256 struct diff_filepair *p = q->queue[i];
257 diff_free_filepair(p);
258 }
259
260 /*
261 * .. and re-instate the one we want (which might be either the
262 * original one, or the rename/copy we found)
750f7b66 263 */
9f38e1ef
LT
264 q->queue[0] = choice;
265 q->nr = 1;
750f7b66
LT
266}
267
ac1b3d12
LT
268int diff_tree_sha1(const unsigned char *old, const unsigned char *new, const char *base, struct diff_options *opt)
269{
270 void *tree1, *tree2;
271 struct tree_desc t1, t2;
6fda5e51 272 unsigned long size1, size2;
ac1b3d12
LT
273 int retval;
274
6fda5e51 275 tree1 = read_object_with_reference(old, tree_type, &size1, NULL);
ac1b3d12
LT
276 if (!tree1)
277 die("unable to read source tree (%s)", sha1_to_hex(old));
6fda5e51 278 tree2 = read_object_with_reference(new, tree_type, &size2, NULL);
ac1b3d12
LT
279 if (!tree2)
280 die("unable to read destination tree (%s)", sha1_to_hex(new));
6fda5e51
LT
281 init_tree_desc(&t1, tree1, size1);
282 init_tree_desc(&t2, tree2, size2);
ac1b3d12 283 retval = diff_tree(&t1, &t2, base, opt);
39f75d26 284 if (!*base && DIFF_OPT_TST(opt, FOLLOW_RENAMES) && diff_might_be_rename()) {
750f7b66
LT
285 init_tree_desc(&t1, tree1, size1);
286 init_tree_desc(&t2, tree2, size2);
287 try_to_follow_renames(&t1, &t2, base, opt);
288 }
ac1b3d12
LT
289 free(tree1);
290 free(tree2);
291 return retval;
292}
293
2b60356d
RS
294int diff_root_tree_sha1(const unsigned char *new, const char *base, struct diff_options *opt)
295{
296 int retval;
297 void *tree;
6fda5e51 298 unsigned long size;
2b60356d
RS
299 struct tree_desc empty, real;
300
6fda5e51 301 tree = read_object_with_reference(new, tree_type, &size, NULL);
2b60356d
RS
302 if (!tree)
303 die("unable to read root tree (%s)", sha1_to_hex(new));
6fda5e51 304 init_tree_desc(&real, tree, size);
2b60356d 305
6fda5e51 306 init_tree_desc(&empty, "", 0);
2b60356d
RS
307 retval = diff_tree(&empty, &real, base, opt);
308 free(tree);
309 return retval;
310}
311
a8baa7b9 312void diff_tree_release_paths(struct diff_options *opt)
ac1b3d12 313{
66f13625 314 free_pathspec(&opt->pathspec);
a8baa7b9
JH
315}
316
317void diff_tree_setup_paths(const char **p, struct diff_options *opt)
318{
66f13625 319 init_pathspec(&opt->pathspec, p);
ac1b3d12 320}