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