db37aa714f0d0a7b0b2437ffb5b04351642f1e00
1#include <ctype.h>
2#include "cache.h"
3#include "diff.h"
4#include "commit.h"
5
6static int show_root_diff = 0;
7static int verbose_header = 0;
8static int ignore_merges = 1;
9static int recursive = 0;
10static int show_tree_entry_in_recursive = 0;
11static int read_stdin = 0;
12static int diff_output_format = DIFF_FORMAT_HUMAN;
13static int detect_rename = 0;
14static int diff_setup_opt = 0;
15static int diff_score_opt = 0;
16static const char *pickaxe = NULL;
17static int pickaxe_opts = 0;
18static int diff_break_opt = -1;
19static const char *orderfile = NULL;
20static const char *header = NULL;
21static const char *header_prefix = "";
22
23// What paths are we interested in?
24static int nr_paths = 0;
25static const char **paths = NULL;
26static int *pathlens = NULL;
27
28static int diff_tree_sha1(const unsigned char *old, const unsigned char *new, const char *base);
29
30static void update_tree_entry(void **bufp, unsigned long *sizep)
31{
32 void *buf = *bufp;
33 unsigned long size = *sizep;
34 int len = strlen(buf) + 1 + 20;
35
36 if (size < len)
37 die("corrupt tree file");
38 *bufp = buf + len;
39 *sizep = size - len;
40}
41
42static const unsigned char *extract(void *tree, unsigned long size, const char **pathp, unsigned int *modep)
43{
44 int len = strlen(tree)+1;
45 const unsigned char *sha1 = tree + len;
46 const char *path = strchr(tree, ' ');
47
48 if (!path || size < len + 20 || sscanf(tree, "%o", modep) != 1)
49 die("corrupt tree file");
50 *pathp = path+1;
51 return sha1;
52}
53
54static char *malloc_base(const char *base, const char *path, int pathlen)
55{
56 int baselen = strlen(base);
57 char *newbase = xmalloc(baselen + pathlen + 2);
58 memcpy(newbase, base, baselen);
59 memcpy(newbase + baselen, path, pathlen);
60 memcpy(newbase + baselen + pathlen, "/", 2);
61 return newbase;
62}
63
64static void show_file(const char *prefix, void *tree, unsigned long size, const char *base);
65static void show_tree(const char *prefix, void *tree, unsigned long size, const char *base);
66
67/* A file entry went away or appeared */
68static void show_file(const char *prefix, void *tree, unsigned long size, const char *base)
69{
70 unsigned mode;
71 const char *path;
72 const unsigned char *sha1 = extract(tree, size, &path, &mode);
73
74 if (recursive && S_ISDIR(mode)) {
75 char type[20];
76 unsigned long size;
77 char *newbase = malloc_base(base, path, strlen(path));
78 void *tree;
79
80 tree = read_sha1_file(sha1, type, &size);
81 if (!tree || strcmp(type, "tree"))
82 die("corrupt tree sha %s", sha1_to_hex(sha1));
83
84 show_tree(prefix, tree, size, newbase);
85
86 free(tree);
87 free(newbase);
88 return;
89 }
90
91 diff_addremove(prefix[0], mode, sha1, base, path);
92}
93
94static int compare_tree_entry(void *tree1, unsigned long size1, void *tree2, unsigned long size2, const char *base)
95{
96 unsigned mode1, mode2;
97 const char *path1, *path2;
98 const unsigned char *sha1, *sha2;
99 int cmp, pathlen1, pathlen2;
100
101 sha1 = extract(tree1, size1, &path1, &mode1);
102 sha2 = extract(tree2, size2, &path2, &mode2);
103
104 pathlen1 = strlen(path1);
105 pathlen2 = strlen(path2);
106 cmp = base_name_compare(path1, pathlen1, mode1, path2, pathlen2, mode2);
107 if (cmp < 0) {
108 show_file("-", tree1, size1, base);
109 return -1;
110 }
111 if (cmp > 0) {
112 show_file("+", tree2, size2, base);
113 return 1;
114 }
115 if (!memcmp(sha1, sha2, 20) && mode1 == mode2)
116 return 0;
117
118 /*
119 * If the filemode has changed to/from a directory from/to a regular
120 * file, we need to consider it a remove and an add.
121 */
122 if (S_ISDIR(mode1) != S_ISDIR(mode2)) {
123 show_file("-", tree1, size1, base);
124 show_file("+", tree2, size2, base);
125 return 0;
126 }
127
128 if (recursive && S_ISDIR(mode1)) {
129 int retval;
130 char *newbase = malloc_base(base, path1, pathlen1);
131 if (show_tree_entry_in_recursive)
132 diff_change(mode1, mode2, sha1, sha2, base, path1);
133 retval = diff_tree_sha1(sha1, sha2, newbase);
134 free(newbase);
135 return retval;
136 }
137
138 diff_change(mode1, mode2, sha1, sha2, base, path1);
139 return 0;
140}
141
142static int interesting(void *tree, unsigned long size, const char *base)
143{
144 const char *path;
145 unsigned mode;
146 int i;
147 int baselen, pathlen;
148
149 if (!nr_paths)
150 return 1;
151
152 (void)extract(tree, size, &path, &mode);
153
154 pathlen = strlen(path);
155 baselen = strlen(base);
156
157 for (i=0; i < nr_paths; i++) {
158 const char *match = paths[i];
159 int matchlen = pathlens[i];
160
161 if (baselen >= matchlen) {
162 /* If it doesn't match, move along... */
163 if (strncmp(base, match, matchlen))
164 continue;
165
166 /* The base is a subdirectory of a path which was specified. */
167 return 1;
168 }
169
170 /* Does the base match? */
171 if (strncmp(base, match, baselen))
172 continue;
173
174 match += baselen;
175 matchlen -= baselen;
176
177 if (pathlen > matchlen)
178 continue;
179
180 if (matchlen > pathlen) {
181 if (match[pathlen] != '/')
182 continue;
183 if (!S_ISDIR(mode))
184 continue;
185 }
186
187 if (strncmp(path, match, pathlen))
188 continue;
189
190 return 1;
191 }
192 return 0; /* No matches */
193}
194
195/* A whole sub-tree went away or appeared */
196static void show_tree(const char *prefix, void *tree, unsigned long size, const char *base)
197{
198 while (size) {
199 if (interesting(tree, size, base))
200 show_file(prefix, tree, size, base);
201 update_tree_entry(&tree, &size);
202 }
203}
204
205static int diff_tree(void *tree1, unsigned long size1, void *tree2, unsigned long size2, const char *base)
206{
207 while (size1 | size2) {
208 if (nr_paths && size1 && !interesting(tree1, size1, base)) {
209 update_tree_entry(&tree1, &size1);
210 continue;
211 }
212 if (nr_paths && size2 && !interesting(tree2, size2, base)) {
213 update_tree_entry(&tree2, &size2);
214 continue;
215 }
216 if (!size1) {
217 show_file("+", tree2, size2, base);
218 update_tree_entry(&tree2, &size2);
219 continue;
220 }
221 if (!size2) {
222 show_file("-", tree1, size1, base);
223 update_tree_entry(&tree1, &size1);
224 continue;
225 }
226 switch (compare_tree_entry(tree1, size1, tree2, size2, base)) {
227 case -1:
228 update_tree_entry(&tree1, &size1);
229 continue;
230 case 0:
231 update_tree_entry(&tree1, &size1);
232 /* Fallthrough */
233 case 1:
234 update_tree_entry(&tree2, &size2);
235 continue;
236 }
237 die("git-diff-tree: internal error");
238 }
239 return 0;
240}
241
242static int diff_tree_sha1(const unsigned char *old, const unsigned char *new, const char *base)
243{
244 void *tree1, *tree2;
245 unsigned long size1, size2;
246 int retval;
247
248 tree1 = read_object_with_reference(old, "tree", &size1, NULL);
249 if (!tree1)
250 die("unable to read source tree (%s)", sha1_to_hex(old));
251 tree2 = read_object_with_reference(new, "tree", &size2, NULL);
252 if (!tree2)
253 die("unable to read destination tree (%s)", sha1_to_hex(new));
254 retval = diff_tree(tree1, size1, tree2, size2, base);
255 free(tree1);
256 free(tree2);
257 return retval;
258}
259
260static void call_diff_setup(void)
261{
262 diff_setup(diff_setup_opt);
263}
264
265static int call_diff_flush(void)
266{
267 diffcore_std(0,
268 detect_rename, diff_score_opt,
269 pickaxe, pickaxe_opts,
270 diff_break_opt,
271 orderfile);
272 if (diff_queue_is_empty()) {
273 diff_flush(DIFF_FORMAT_NO_OUTPUT, 0);
274 return 0;
275 }
276 if (header) {
277 const char *fmt = "%s";
278 if (diff_output_format == DIFF_FORMAT_MACHINE)
279 fmt = "%s%c";
280
281 printf(fmt, header, 0);
282 header = NULL;
283 }
284 diff_flush(diff_output_format, 1);
285 return 1;
286}
287
288static int diff_tree_sha1_top(const unsigned char *old,
289 const unsigned char *new, const char *base)
290{
291 int ret;
292
293 call_diff_setup();
294 ret = diff_tree_sha1(old, new, base);
295 call_diff_flush();
296 return ret;
297}
298
299static int diff_root_tree(const unsigned char *new, const char *base)
300{
301 int retval;
302 void *tree;
303 unsigned long size;
304
305 call_diff_setup();
306 tree = read_object_with_reference(new, "tree", &size, NULL);
307 if (!tree)
308 die("unable to read root tree (%s)", sha1_to_hex(new));
309 retval = diff_tree("", 0, tree, size, base);
310 free(tree);
311 call_diff_flush();
312 return retval;
313}
314
315static char *generate_header(const char *commit, const char *parent, const char *msg, unsigned long len)
316{
317 static char this_header[16384];
318 int offset;
319
320 offset = sprintf(this_header, "%s%s (from %s)\n", header_prefix, commit, parent);
321 if (verbose_header) {
322 offset += pretty_print_commit(msg, len, this_header + offset, sizeof(this_header) - offset);
323 this_header[offset++] = '\n';
324 this_header[offset++] = 0;
325 }
326
327 return this_header;
328}
329
330static int diff_tree_commit(const unsigned char *commit, const char *name)
331{
332 unsigned long size, offset;
333 char *buf = read_object_with_reference(commit, "commit", &size, NULL);
334
335 if (!buf)
336 return -1;
337
338 if (!name) {
339 static char commit_name[60];
340 strcpy(commit_name, sha1_to_hex(commit));
341 name = commit_name;
342 }
343
344 /* Root commit? */
345 if (show_root_diff && memcmp(buf + 46, "parent ", 7)) {
346 header = generate_header(name, "root", buf, size);
347 diff_root_tree(commit, "");
348 }
349
350 /* More than one parent? */
351 if (ignore_merges) {
352 if (!memcmp(buf + 46 + 48, "parent ", 7))
353 return 0;
354 }
355
356 offset = 46;
357 while (offset + 48 < size && !memcmp(buf + offset, "parent ", 7)) {
358 unsigned char parent[20];
359 if (get_sha1_hex(buf + offset + 7, parent))
360 return -1;
361 header = generate_header(name, sha1_to_hex(parent), buf, size);
362 diff_tree_sha1_top(parent, commit, "");
363 if (!header && verbose_header) {
364 header_prefix = "\ndiff-tree ";
365 /*
366 * Don't print multiple merge entries if we
367 * don't print the diffs.
368 */
369 }
370 offset += 48;
371 }
372 return 0;
373}
374
375static int diff_tree_stdin(char *line)
376{
377 int len = strlen(line);
378 unsigned char commit[20], parent[20];
379 static char this_header[1000];
380
381 if (!len || line[len-1] != '\n')
382 return -1;
383 line[len-1] = 0;
384 if (get_sha1_hex(line, commit))
385 return -1;
386 if (isspace(line[40]) && !get_sha1_hex(line+41, parent)) {
387 line[40] = 0;
388 line[81] = 0;
389 sprintf(this_header, "%s (from %s)\n", line, line+41);
390 header = this_header;
391 return diff_tree_sha1_top(parent, commit, "");
392 }
393 line[40] = 0;
394 return diff_tree_commit(commit, line);
395}
396
397static char *diff_tree_usage =
398"git-diff-tree [-p] [-r] [-z] [--stdin] [-M] [-C] [-R] [-S<string>] [-m] [-s] [-v] [-t] <tree-ish> <tree-ish>";
399
400int main(int argc, const char **argv)
401{
402 int nr_sha1;
403 char line[1000];
404 unsigned char sha1[2][20];
405
406 nr_sha1 = 0;
407 for (;;) {
408 const char *arg;
409
410 argv++;
411 argc--;
412 arg = *argv;
413 if (!arg)
414 break;
415
416 if (*arg != '-') {
417 if (nr_sha1 < 2 && !get_sha1(arg, sha1[nr_sha1])) {
418 nr_sha1++;
419 continue;
420 }
421 break;
422 }
423
424 if (!strcmp(arg, "--")) {
425 argv++;
426 argc--;
427 break;
428 }
429 if (!strcmp(arg, "-r")) {
430 recursive = 1;
431 continue;
432 }
433 if (!strcmp(arg, "-t")) {
434 recursive = show_tree_entry_in_recursive = 1;
435 continue;
436 }
437 if (!strcmp(arg, "-R")) {
438 diff_setup_opt |= DIFF_SETUP_REVERSE;
439 continue;
440 }
441 if (!strcmp(arg, "-p")) {
442 diff_output_format = DIFF_FORMAT_PATCH;
443 recursive = 1;
444 continue;
445 }
446 if (!strncmp(arg, "-S", 2)) {
447 pickaxe = arg + 2;
448 continue;
449 }
450 if (!strncmp(arg, "-O", 2)) {
451 orderfile = arg + 2;
452 continue;
453 }
454 if (!strcmp(arg, "--pickaxe-all")) {
455 pickaxe_opts = DIFF_PICKAXE_ALL;
456 continue;
457 }
458 if (!strncmp(arg, "-M", 2)) {
459 detect_rename = DIFF_DETECT_RENAME;
460 diff_score_opt = diff_scoreopt_parse(arg);
461 continue;
462 }
463 if (!strncmp(arg, "-C", 2)) {
464 detect_rename = DIFF_DETECT_COPY;
465 diff_score_opt = diff_scoreopt_parse(arg);
466 continue;
467 }
468 if (!strncmp(arg, "-B", 2)) {
469 diff_break_opt = diff_scoreopt_parse(arg);
470 continue;
471 }
472 if (!strcmp(arg, "-z")) {
473 diff_output_format = DIFF_FORMAT_MACHINE;
474 continue;
475 }
476 if (!strcmp(arg, "-m")) {
477 ignore_merges = 0;
478 continue;
479 }
480 if (!strcmp(arg, "-s")) {
481 diff_output_format = DIFF_FORMAT_NO_OUTPUT;
482 continue;
483 }
484 if (!strcmp(arg, "-v")) {
485 verbose_header = 1;
486 header_prefix = "diff-tree ";
487 continue;
488 }
489 if (!strcmp(arg, "--stdin")) {
490 read_stdin = 1;
491 continue;
492 }
493 if (!strcmp(arg, "--root")) {
494 show_root_diff = 1;
495 continue;
496 }
497 usage(diff_tree_usage);
498 }
499
500 if (argc > 0) {
501 int i;
502
503 paths = argv;
504 nr_paths = argc;
505 pathlens = xmalloc(nr_paths * sizeof(int));
506 for (i=0; i<nr_paths; i++)
507 pathlens[i] = strlen(paths[i]);
508 }
509
510 switch (nr_sha1) {
511 case 0:
512 if (!read_stdin)
513 usage(diff_tree_usage);
514 break;
515 case 1:
516 diff_tree_commit(sha1[0], NULL);
517 break;
518 case 2:
519 diff_tree_sha1_top(sha1[0], sha1[1], "");
520 break;
521 }
522
523 if (!read_stdin)
524 return 0;
525
526 if (detect_rename)
527 diff_setup_opt |= (DIFF_SETUP_USE_SIZE_CACHE |
528 DIFF_SETUP_USE_CACHE);
529 while (fgets(line, sizeof(line), stdin))
530 diff_tree_stdin(line);
531
532 return 0;
533}