[PATCH] Diffcore updates.
[git/git.git] / diff-tree.c
CommitLineData
e0965d83 1#include <ctype.h>
9174026c 2#include "cache.h"
3ebfd4aa 3#include "diff.h"
9174026c 4
f4f21ce3 5static int silent = 0;
dc26bd89 6static int show_root_diff = 0;
cee99d22 7static int verbose_header = 0;
e0965d83 8static int ignore_merges = 1;
73134b6d 9static int recursive = 0;
e0965d83 10static int read_stdin = 0;
81e50eab 11static int diff_output_format = DIFF_FORMAT_HUMAN;
5c97558c 12static int detect_rename = 0;
de809dbb 13static int reverse_diff = 0;
57fe64a4 14static int diff_score_opt = 0;
057c7d30 15static const char *pickaxe = NULL;
f4f21ce3 16static const char *header = NULL;
cee99d22 17static const char *header_prefix = "";
9174026c 18
c5b42386
LT
19// What paths are we interested in?
20static int nr_paths = 0;
6b14d7fa 21static const char **paths = NULL;
c5b42386
LT
22static int *pathlens = NULL;
23
eeb79916 24static int diff_tree_sha1(const unsigned char *old, const unsigned char *new, const char *base);
73134b6d
LT
25
26static void update_tree_entry(void **bufp, unsigned long *sizep)
9174026c
LT
27{
28 void *buf = *bufp;
29 unsigned long size = *sizep;
30 int len = strlen(buf) + 1 + 20;
31
32 if (size < len)
2de381f9 33 die("corrupt tree file");
9174026c
LT
34 *bufp = buf + len;
35 *sizep = size - len;
9174026c
LT
36}
37
38static const unsigned char *extract(void *tree, unsigned long size, const char **pathp, unsigned int *modep)
39{
40 int len = strlen(tree)+1;
41 const unsigned char *sha1 = tree + len;
42 const char *path = strchr(tree, ' ');
43
44 if (!path || size < len + 20 || sscanf(tree, "%o", modep) != 1)
2de381f9 45 die("corrupt tree file");
9174026c
LT
46 *pathp = path+1;
47 return sha1;
48}
49
262e82b4
LT
50static char *malloc_base(const char *base, const char *path, int pathlen)
51{
52 int baselen = strlen(base);
812666c8 53 char *newbase = xmalloc(baselen + pathlen + 2);
262e82b4
LT
54 memcpy(newbase, base, baselen);
55 memcpy(newbase + baselen, path, pathlen);
56 memcpy(newbase + baselen + pathlen, "/", 2);
57 return newbase;
58}
59
60static void show_file(const char *prefix, void *tree, unsigned long size, const char *base);
ed1a368b 61static void show_tree(const char *prefix, void *tree, unsigned long size, const char *base);
262e82b4
LT
62
63/* A file entry went away or appeared */
73134b6d 64static void show_file(const char *prefix, void *tree, unsigned long size, const char *base)
9174026c
LT
65{
66 unsigned mode;
67 const char *path;
68 const unsigned char *sha1 = extract(tree, size, &path, &mode);
262e82b4 69
f4f21ce3
LT
70 if (silent)
71 return;
72
262e82b4
LT
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);
eeb79916
LT
130 retval = diff_tree_sha1(sha1, sha2, newbase);
131 free(newbase);
132 return retval;
73134b6d
LT
133 }
134
f4f21ce3
LT
135 if (silent)
136 return 0;
137
57fe64a4 138 diff_change(mode1, mode2, sha1, sha2, base, path1);
9174026c
LT
139 return 0;
140}
141
c5b42386
LT
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
cb6c8ed2
LT
180 if (matchlen > pathlen) {
181 if (match[pathlen] != '/')
182 continue;
850e82d8
LT
183 if (!S_ISDIR(mode))
184 continue;
cb6c8ed2
LT
185 }
186
c5b42386
LT
187 if (strncmp(path, match, pathlen))
188 continue;
189
190 return 1;
191 }
192 return 0; /* No matches */
193}
194
ed1a368b
LT
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
eeb79916 205static int diff_tree(void *tree1, unsigned long size1, void *tree2, unsigned long size2, const char *base)
9174026c
LT
206{
207 while (size1 | size2) {
c5b42386
LT
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 }
9174026c 216 if (!size1) {
eeb79916 217 show_file("+", tree2, size2, base);
9174026c
LT
218 update_tree_entry(&tree2, &size2);
219 continue;
220 }
221 if (!size2) {
eeb79916 222 show_file("-", tree1, size1, base);
9174026c
LT
223 update_tree_entry(&tree1, &size1);
224 continue;
225 }
eeb79916 226 switch (compare_tree_entry(tree1, size1, tree2, size2, base)) {
9174026c
LT
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 }
667bb59b 237 die("git-diff-tree: internal error");
9174026c
LT
238 }
239 return 0;
240}
241
eeb79916 242static int diff_tree_sha1(const unsigned char *old, const unsigned char *new, const char *base)
9174026c 243{
9174026c
LT
244 void *tree1, *tree2;
245 unsigned long size1, size2;
73134b6d 246 int retval;
9174026c 247
e99d59ff 248 tree1 = read_object_with_reference(old, "tree", &size1, NULL);
c1fdf2a6 249 if (!tree1)
2de381f9 250 die("unable to read source tree (%s)", sha1_to_hex(old));
e99d59ff 251 tree2 = read_object_with_reference(new, "tree", &size2, NULL);
c1fdf2a6 252 if (!tree2)
2de381f9 253 die("unable to read destination tree (%s)", sha1_to_hex(new));
eeb79916 254 retval = diff_tree(tree1, size1, tree2, size2, base);
73134b6d
LT
255 free(tree1);
256 free(tree2);
257 return retval;
258}
259
38c6f780
JH
260static void call_diff_setup(void)
261{
6b14d7fa 262 diff_setup(reverse_diff);
38c6f780
JH
263}
264
6b14d7fa 265static int call_diff_flush()
38c6f780
JH
266{
267 if (detect_rename)
6b14d7fa
JH
268 diffcore_rename(detect_rename, diff_score_opt);
269 diffcore_prune();
270 if (pickaxe) {
271 diffcore_pickaxe(pickaxe);
272 if (diff_queue_is_empty()) {
273 diff_flush(DIFF_FORMAT_NO_OUTPUT);
274 return 0;
275 }
276 }
277 if (nr_paths)
278 diffcore_pathspec(paths);
279 if (header) {
280 printf("%s", header);
281 header = NULL;
282 }
283 diff_flush(diff_output_format);
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 */
389 if (!silent)
390 this_header[offset++] = '\n';
cee99d22
LT
391 this_header[offset] = 0;
392 }
393
394 return this_header;
395}
396
b11645be 397static int diff_tree_commit(const unsigned char *commit, const char *name)
e0965d83 398{
e0965d83 399 unsigned long size, offset;
b11645be 400 char *buf = read_object_with_reference(commit, "commit", &size, NULL);
e0965d83 401
e0965d83
LT
402 if (!buf)
403 return -1;
404
73848892
LT
405 if (!name) {
406 static char commit_name[60];
407 strcpy(commit_name, sha1_to_hex(commit));
408 name = commit_name;
409 }
410
dc26bd89
LT
411 /* Root commit? */
412 if (show_root_diff && memcmp(buf + 46, "parent ", 7)) {
413 header = generate_header(name, "root", buf, size);
414 diff_root_tree(commit, "");
415 }
416
417 /* More than one parent? */
418 if (ignore_merges) {
419 if (!memcmp(buf + 46 + 48, "parent ", 7))
420 return 0;
421 }
422
e0965d83
LT
423 offset = 46;
424 while (offset + 48 < size && !memcmp(buf + offset, "parent ", 7)) {
b11645be 425 unsigned char parent[20];
e0965d83
LT
426 if (get_sha1_hex(buf + offset + 7, parent))
427 return -1;
b11645be 428 header = generate_header(name, sha1_to_hex(parent), buf, size);
5c97558c 429 diff_tree_sha1_top(parent, commit, "");
d6db0107 430 if (!header && verbose_header) {
cee99d22 431 header_prefix = "\ndiff-tree ";
d6db0107
LT
432 /*
433 * Don't print multiple merge entries if we
434 * don't print the diffs.
435 */
436 if (silent)
437 break;
438 }
e0965d83
LT
439 offset += 48;
440 }
b11645be
LT
441 return 0;
442}
443
444static int diff_tree_stdin(char *line)
445{
446 int len = strlen(line);
447 unsigned char commit[20], parent[20];
448 static char this_header[1000];
449
450 if (!len || line[len-1] != '\n')
451 return -1;
452 line[len-1] = 0;
453 if (get_sha1_hex(line, commit))
454 return -1;
455 if (isspace(line[40]) && !get_sha1_hex(line+41, parent)) {
456 line[40] = 0;
457 line[81] = 0;
458 sprintf(this_header, "%s (from %s)\n", line, line+41);
459 header = this_header;
5c97558c 460 return diff_tree_sha1_top(parent, commit, "");
b11645be
LT
461 }
462 line[40] = 0;
463 return diff_tree_commit(commit, line);
e0965d83
LT
464}
465
5aad72f2 466static char *diff_tree_usage =
52e95789 467"git-diff-tree [-p] [-r] [-z] [--stdin] [-M] [-C] [-R] [-S<string>] [-m] [-s] [-v] <tree-ish> <tree-ish>";
c5bac17a 468
6b14d7fa 469int main(int argc, const char **argv)
73134b6d 470{
0a8365a1 471 int nr_sha1;
e0965d83 472 char line[1000];
0a8365a1 473 unsigned char sha1[2][20];
73134b6d 474
0a8365a1 475 nr_sha1 = 0;
c5b42386 476 for (;;) {
6b14d7fa 477 const char *arg;
c5b42386 478
e0965d83
LT
479 argv++;
480 argc--;
481 arg = *argv;
0a8365a1 482 if (!arg)
c5b42386
LT
483 break;
484
0a8365a1
LT
485 if (*arg != '-') {
486 if (nr_sha1 < 2 && !get_sha1(arg, sha1[nr_sha1])) {
487 nr_sha1++;
488 continue;
489 }
490 break;
491 }
492
493 if (!strcmp(arg, "--")) {
e0965d83
LT
494 argv++;
495 argc--;
496 break;
497 }
bf16c71e 498 if (!strcmp(arg, "-r")) {
73134b6d
LT
499 recursive = 1;
500 continue;
501 }
de809dbb
LT
502 if (!strcmp(arg, "-R")) {
503 reverse_diff = 1;
504 continue;
505 }
3ebfd4aa 506 if (!strcmp(arg, "-p")) {
81e50eab
JH
507 diff_output_format = DIFF_FORMAT_PATCH;
508 recursive = 1;
3ebfd4aa
JH
509 continue;
510 }
52e95789
JH
511 if (!strncmp(arg, "-S", 2)) {
512 pickaxe = arg + 2;
513 continue;
514 }
57fe64a4 515 if (!strncmp(arg, "-M", 2)) {
6b14d7fa 516 detect_rename = DIFF_DETECT_RENAME;
57fe64a4 517 diff_score_opt = diff_scoreopt_parse(arg);
427dcb4b
JH
518 continue;
519 }
520 if (!strncmp(arg, "-C", 2)) {
6b14d7fa 521 detect_rename = DIFF_DETECT_COPY;
427dcb4b 522 diff_score_opt = diff_scoreopt_parse(arg);
5c97558c
JH
523 continue;
524 }
6cbd72f8 525 if (!strcmp(arg, "-z")) {
81e50eab 526 diff_output_format = DIFF_FORMAT_MACHINE;
6cbd72f8
LT
527 continue;
528 }
e0965d83
LT
529 if (!strcmp(arg, "-m")) {
530 ignore_merges = 0;
531 continue;
532 }
f4f21ce3
LT
533 if (!strcmp(arg, "-s")) {
534 silent = 1;
535 continue;
536 }
cee99d22
LT
537 if (!strcmp(arg, "-v")) {
538 verbose_header = 1;
539 header_prefix = "diff-tree ";
540 continue;
541 }
e0965d83
LT
542 if (!strcmp(arg, "--stdin")) {
543 read_stdin = 1;
544 continue;
545 }
dc26bd89
LT
546 if (!strcmp(arg, "--root")) {
547 show_root_diff = 1;
548 continue;
549 }
c5bac17a 550 usage(diff_tree_usage);
73134b6d
LT
551 }
552
e0965d83 553 if (argc > 0) {
c5b42386
LT
554 int i;
555
e0965d83
LT
556 paths = argv;
557 nr_paths = argc;
812666c8 558 pathlens = xmalloc(nr_paths * sizeof(int));
c5b42386
LT
559 for (i=0; i<nr_paths; i++)
560 pathlens[i] = strlen(paths[i]);
561 }
562
0a8365a1
LT
563 switch (nr_sha1) {
564 case 0:
565 if (!read_stdin)
566 usage(diff_tree_usage);
567 break;
568 case 1:
73848892 569 diff_tree_commit(sha1[0], NULL);
0a8365a1
LT
570 break;
571 case 2:
5c97558c 572 diff_tree_sha1_top(sha1[0], sha1[1], "");
0a8365a1
LT
573 break;
574 }
575
e0965d83 576 if (!read_stdin)
0a8365a1 577 return 0;
e0965d83
LT
578
579 while (fgets(line, sizeof(line), stdin))
580 diff_tree_stdin(line);
581
582 return 0;
9174026c 583}