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