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