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