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