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