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