glossary: define pathspec
[git.git] / tree-diff.c
blob23f4478cee5b3dd064529a35dc763cd74dcaa98c
1 /*
2 * Helper functions for tree diff generation
3 */
4 #include "cache.h"
5 #include "diff.h"
6 #include "diffcore.h"
7 #include "tree.h"
9 static char *malloc_base(const char *base, int baselen, const char *path, int pathlen)
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;
18 static char *malloc_fullname(const char *base, int baselen, const char *path, int pathlen)
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;
27 static void show_entry(struct diff_options *opt, const char *prefix, struct tree_desc *desc,
28 const char *base, int baselen);
30 static int compare_tree_entry(struct tree_desc *t1, struct tree_desc *t2, const char *base, int baselen, struct diff_options *opt)
32 unsigned mode1, mode2;
33 const char *path1, *path2;
34 const unsigned char *sha1, *sha2;
35 int cmp, pathlen1, pathlen2;
36 char *fullname;
38 sha1 = tree_entry_extract(t1, &path1, &mode1);
39 sha2 = tree_entry_extract(t2, &path2, &mode2);
41 pathlen1 = tree_entry_len(path1, sha1);
42 pathlen2 = tree_entry_len(path2, sha2);
43 cmp = base_name_compare(path1, pathlen1, mode1, path2, pathlen2, mode2);
44 if (cmp < 0) {
45 show_entry(opt, "-", t1, base, baselen);
46 return -1;
48 if (cmp > 0) {
49 show_entry(opt, "+", t2, base, baselen);
50 return 1;
52 if (!DIFF_OPT_TST(opt, FIND_COPIES_HARDER) && !hashcmp(sha1, sha2) && mode1 == mode2)
53 return 0;
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.
59 if (S_ISDIR(mode1) != S_ISDIR(mode2)) {
60 show_entry(opt, "-", t1, base, baselen);
61 show_entry(opt, "+", t2, base, baselen);
62 return 0;
65 if (DIFF_OPT_TST(opt, RECURSIVE) && S_ISDIR(mode1)) {
66 int retval;
67 char *newbase = malloc_base(base, baselen, path1, pathlen1);
68 if (DIFF_OPT_TST(opt, TREE_IN_RECURSIVE)) {
69 newbase[baselen + pathlen1] = 0;
70 opt->change(opt, mode1, mode2,
71 sha1, sha2, newbase, 0, 0);
72 newbase[baselen + pathlen1] = '/';
74 retval = diff_tree_sha1(sha1, sha2, newbase, opt);
75 free(newbase);
76 return retval;
79 fullname = malloc_fullname(base, baselen, path1, pathlen1);
80 opt->change(opt, mode1, mode2, sha1, sha2, fullname, 0, 0);
81 free(fullname);
82 return 0;
85 /* A whole sub-tree went away or appeared */
86 static void show_tree(struct diff_options *opt, const char *prefix, struct tree_desc *desc, const char *base, int baselen)
88 int all_interesting = 0;
89 while (desc->size) {
90 int show;
92 if (all_interesting)
93 show = 1;
94 else {
95 show = tree_entry_interesting(&desc->entry, base, baselen, &opt->pathspec);
96 if (show == 2)
97 all_interesting = 1;
99 if (show < 0)
100 break;
101 if (show)
102 show_entry(opt, prefix, desc, base, baselen);
103 update_tree_entry(desc);
107 /* A file entry went away or appeared */
108 static void show_entry(struct diff_options *opt, const char *prefix, struct tree_desc *desc,
109 const char *base, int baselen)
111 unsigned mode;
112 const char *path;
113 const unsigned char *sha1 = tree_entry_extract(desc, &path, &mode);
114 int pathlen = tree_entry_len(path, sha1);
116 if (DIFF_OPT_TST(opt, RECURSIVE) && S_ISDIR(mode)) {
117 enum object_type type;
118 char *newbase = malloc_base(base, baselen, path, pathlen);
119 struct tree_desc inner;
120 void *tree;
121 unsigned long size;
123 tree = read_sha1_file(sha1, &type, &size);
124 if (!tree || type != OBJ_TREE)
125 die("corrupt tree sha %s", sha1_to_hex(sha1));
127 if (DIFF_OPT_TST(opt, TREE_IN_RECURSIVE)) {
128 newbase[baselen + pathlen] = 0;
129 opt->add_remove(opt, *prefix, mode, sha1, newbase, 0);
130 newbase[baselen + pathlen] = '/';
133 init_tree_desc(&inner, tree, size);
134 show_tree(opt, prefix, &inner, newbase, baselen + 1 + pathlen);
136 free(tree);
137 free(newbase);
138 } else {
139 char *fullname = malloc_fullname(base, baselen, path, pathlen);
140 opt->add_remove(opt, prefix[0], mode, sha1, fullname, 0);
141 free(fullname);
145 static void skip_uninteresting(struct tree_desc *t, const char *base, int baselen, struct diff_options *opt, int *all_interesting)
147 while (t->size) {
148 int show = tree_entry_interesting(&t->entry, base, baselen, &opt->pathspec);
149 if (show == 2)
150 *all_interesting = 1;
151 if (!show) {
152 update_tree_entry(t);
153 continue;
155 /* Skip it all? */
156 if (show < 0)
157 t->size = 0;
158 return;
162 int diff_tree(struct tree_desc *t1, struct tree_desc *t2, const char *base, struct diff_options *opt)
164 int baselen = strlen(base);
165 int all_t1_interesting = 0;
166 int all_t2_interesting = 0;
168 for (;;) {
169 if (DIFF_OPT_TST(opt, QUICK) &&
170 DIFF_OPT_TST(opt, HAS_CHANGES))
171 break;
172 if (opt->pathspec.nr) {
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);
180 if (!t1->size) {
181 if (!t2->size)
182 break;
183 show_entry(opt, "+", t2, base, baselen);
184 update_tree_entry(t2);
185 continue;
187 if (!t2->size) {
188 show_entry(opt, "-", t1, base, baselen);
189 update_tree_entry(t1);
190 continue;
192 switch (compare_tree_entry(t1, t2, base, baselen, opt)) {
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;
203 die("git diff-tree: internal error");
205 return 0;
209 * Does it look like the resulting diff might be due to a rename?
210 * - single entry
211 * - not a valid previous file
213 static inline int diff_might_be_rename(void)
215 return diff_queued_diff.nr == 1 &&
216 !DIFF_FILE_VALID(diff_queued_diff.queue[0]->one);
219 static void try_to_follow_renames(struct tree_desc *t1, struct tree_desc *t2, const char *base, struct diff_options *opt)
221 struct diff_options diff_opts;
222 struct diff_queue_struct *q = &diff_queued_diff;
223 struct diff_filepair *choice;
224 const char *paths[1];
225 int i;
227 /* Remove the file creation entry from the diff queue, and remember it */
228 choice = q->queue[0];
229 q->nr = 0;
231 diff_setup(&diff_opts);
232 DIFF_OPT_SET(&diff_opts, RECURSIVE);
233 DIFF_OPT_SET(&diff_opts, FIND_COPIES_HARDER);
234 diff_opts.output_format = DIFF_FORMAT_NO_OUTPUT;
235 diff_opts.single_follow = opt->pathspec.raw[0];
236 diff_opts.break_opt = opt->break_opt;
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);
243 diff_tree_release_paths(&diff_opts);
245 /* Go through the new set of filepairing, and see if we find a more interesting one */
246 opt->found_follow = 0;
247 for (i = 0; i < q->nr; i++) {
248 struct diff_filepair *p = q->queue[i];
251 * Found a source? Not only do we use that for the new
252 * diff_queued_diff, we will also use that as the path in
253 * the future!
255 if ((p->status == 'R' || p->status == 'C') &&
256 !strcmp(p->two->path, opt->pathspec.raw[0])) {
257 /* Switch the file-pairs around */
258 q->queue[i] = choice;
259 choice = p;
261 /* Update the path we use from now on.. */
262 diff_tree_release_paths(opt);
263 opt->pathspec.raw[0] = xstrdup(p->one->path);
264 diff_tree_setup_paths(opt->pathspec.raw, opt);
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.
274 opt->found_follow = 1;
275 break;
280 * Then, discard all the non-relevant file pairs...
282 for (i = 0; i < q->nr; i++) {
283 struct diff_filepair *p = q->queue[i];
284 diff_free_filepair(p);
288 * .. and re-instate the one we want (which might be either the
289 * original one, or the rename/copy we found)
291 q->queue[0] = choice;
292 q->nr = 1;
295 int diff_tree_sha1(const unsigned char *old, const unsigned char *new, const char *base, struct diff_options *opt)
297 void *tree1, *tree2;
298 struct tree_desc t1, t2;
299 unsigned long size1, size2;
300 int retval;
302 tree1 = read_object_with_reference(old, tree_type, &size1, NULL);
303 if (!tree1)
304 die("unable to read source tree (%s)", sha1_to_hex(old));
305 tree2 = read_object_with_reference(new, tree_type, &size2, NULL);
306 if (!tree2)
307 die("unable to read destination tree (%s)", sha1_to_hex(new));
308 init_tree_desc(&t1, tree1, size1);
309 init_tree_desc(&t2, tree2, size2);
310 retval = diff_tree(&t1, &t2, base, opt);
311 if (!*base && DIFF_OPT_TST(opt, FOLLOW_RENAMES) && diff_might_be_rename()) {
312 init_tree_desc(&t1, tree1, size1);
313 init_tree_desc(&t2, tree2, size2);
314 try_to_follow_renames(&t1, &t2, base, opt);
316 free(tree1);
317 free(tree2);
318 return retval;
321 int diff_root_tree_sha1(const unsigned char *new, const char *base, struct diff_options *opt)
323 int retval;
324 void *tree;
325 unsigned long size;
326 struct tree_desc empty, real;
328 tree = read_object_with_reference(new, tree_type, &size, NULL);
329 if (!tree)
330 die("unable to read root tree (%s)", sha1_to_hex(new));
331 init_tree_desc(&real, tree, size);
333 init_tree_desc(&empty, "", 0);
334 retval = diff_tree(&empty, &real, base, opt);
335 free(tree);
336 return retval;
339 void diff_tree_release_paths(struct diff_options *opt)
341 free_pathspec(&opt->pathspec);
344 void diff_tree_setup_paths(const char **p, struct diff_options *opt)
346 init_pathspec(&opt->pathspec, p);