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