[PATCH] Add -B flag to diff-* brothers.
[git/dscho.git] / diff-tree.c
blobc33f54a2d4c2885aed55c4fc9cd5e6c6f6259783
1 #include <ctype.h>
2 #include "cache.h"
3 #include "diff.h"
5 static int show_root_diff = 0;
6 static int verbose_header = 0;
7 static int ignore_merges = 1;
8 static int recursive = 0;
9 static int show_tree_entry_in_recursive = 0;
10 static int read_stdin = 0;
11 static int diff_output_format = DIFF_FORMAT_HUMAN;
12 static int detect_rename = 0;
13 static int diff_setup_opt = 0;
14 static int diff_score_opt = 0;
15 static const char *pickaxe = NULL;
16 static int pickaxe_opts = 0;
17 static int diff_break_opt = -1;
18 static const char *header = NULL;
19 static const char *header_prefix = "";
21 // What paths are we interested in?
22 static int nr_paths = 0;
23 static const char **paths = NULL;
24 static int *pathlens = NULL;
26 static int diff_tree_sha1(const unsigned char *old, const unsigned char *new, const char *base);
28 static void update_tree_entry(void **bufp, unsigned long *sizep)
30 void *buf = *bufp;
31 unsigned long size = *sizep;
32 int len = strlen(buf) + 1 + 20;
34 if (size < len)
35 die("corrupt tree file");
36 *bufp = buf + len;
37 *sizep = size - len;
40 static const unsigned char *extract(void *tree, unsigned long size, const char **pathp, unsigned int *modep)
42 int len = strlen(tree)+1;
43 const unsigned char *sha1 = tree + len;
44 const char *path = strchr(tree, ' ');
46 if (!path || size < len + 20 || sscanf(tree, "%o", modep) != 1)
47 die("corrupt tree file");
48 *pathp = path+1;
49 return sha1;
52 static char *malloc_base(const char *base, const char *path, int pathlen)
54 int baselen = strlen(base);
55 char *newbase = xmalloc(baselen + pathlen + 2);
56 memcpy(newbase, base, baselen);
57 memcpy(newbase + baselen, path, pathlen);
58 memcpy(newbase + baselen + pathlen, "/", 2);
59 return newbase;
62 static void show_file(const char *prefix, void *tree, unsigned long size, const char *base);
63 static void show_tree(const char *prefix, void *tree, unsigned long size, const char *base);
65 /* A file entry went away or appeared */
66 static void show_file(const char *prefix, void *tree, unsigned long size, const char *base)
68 unsigned mode;
69 const char *path;
70 const unsigned char *sha1 = extract(tree, size, &path, &mode);
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;
78 tree = read_sha1_file(sha1, type, &size);
79 if (!tree || strcmp(type, "tree"))
80 die("corrupt tree sha %s", sha1_to_hex(sha1));
82 show_tree(prefix, tree, size, newbase);
84 free(tree);
85 free(newbase);
86 return;
89 diff_addremove(prefix[0], mode, sha1, base, path);
92 static int compare_tree_entry(void *tree1, unsigned long size1, void *tree2, unsigned long size2, const char *base)
94 unsigned mode1, mode2;
95 const char *path1, *path2;
96 const unsigned char *sha1, *sha2;
97 int cmp, pathlen1, pathlen2;
99 sha1 = extract(tree1, size1, &path1, &mode1);
100 sha2 = extract(tree2, size2, &path2, &mode2);
102 pathlen1 = strlen(path1);
103 pathlen2 = strlen(path2);
104 cmp = base_name_compare(path1, pathlen1, mode1, path2, pathlen2, mode2);
105 if (cmp < 0) {
106 show_file("-", tree1, size1, base);
107 return -1;
109 if (cmp > 0) {
110 show_file("+", tree2, size2, base);
111 return 1;
113 if (!memcmp(sha1, sha2, 20) && mode1 == mode2)
114 return 0;
117 * If the filemode has changed to/from a directory from/to a regular
118 * file, we need to consider it a remove and an add.
120 if (S_ISDIR(mode1) != S_ISDIR(mode2)) {
121 show_file("-", tree1, size1, base);
122 show_file("+", tree2, size2, base);
123 return 0;
126 if (recursive && S_ISDIR(mode1)) {
127 int retval;
128 char *newbase = malloc_base(base, path1, pathlen1);
129 if (show_tree_entry_in_recursive)
130 diff_change(mode1, mode2, sha1, sha2, base, path1);
131 retval = diff_tree_sha1(sha1, sha2, newbase);
132 free(newbase);
133 return retval;
136 diff_change(mode1, mode2, sha1, sha2, base, path1);
137 return 0;
140 static int interesting(void *tree, unsigned long size, const char *base)
142 const char *path;
143 unsigned mode;
144 int i;
145 int baselen, pathlen;
147 if (!nr_paths)
148 return 1;
150 (void)extract(tree, size, &path, &mode);
152 pathlen = strlen(path);
153 baselen = strlen(base);
155 for (i=0; i < nr_paths; i++) {
156 const char *match = paths[i];
157 int matchlen = pathlens[i];
159 if (baselen >= matchlen) {
160 /* If it doesn't match, move along... */
161 if (strncmp(base, match, matchlen))
162 continue;
164 /* The base is a subdirectory of a path which was specified. */
165 return 1;
168 /* Does the base match? */
169 if (strncmp(base, match, baselen))
170 continue;
172 match += baselen;
173 matchlen -= baselen;
175 if (pathlen > matchlen)
176 continue;
178 if (matchlen > pathlen) {
179 if (match[pathlen] != '/')
180 continue;
181 if (!S_ISDIR(mode))
182 continue;
185 if (strncmp(path, match, pathlen))
186 continue;
188 return 1;
190 return 0; /* No matches */
193 /* A whole sub-tree went away or appeared */
194 static void show_tree(const char *prefix, void *tree, unsigned long size, const char *base)
196 while (size) {
197 if (interesting(tree, size, base))
198 show_file(prefix, tree, size, base);
199 update_tree_entry(&tree, &size);
203 static int diff_tree(void *tree1, unsigned long size1, void *tree2, unsigned long size2, const char *base)
205 while (size1 | size2) {
206 if (nr_paths && size1 && !interesting(tree1, size1, base)) {
207 update_tree_entry(&tree1, &size1);
208 continue;
210 if (nr_paths && size2 && !interesting(tree2, size2, base)) {
211 update_tree_entry(&tree2, &size2);
212 continue;
214 if (!size1) {
215 show_file("+", tree2, size2, base);
216 update_tree_entry(&tree2, &size2);
217 continue;
219 if (!size2) {
220 show_file("-", tree1, size1, base);
221 update_tree_entry(&tree1, &size1);
222 continue;
224 switch (compare_tree_entry(tree1, size1, tree2, size2, base)) {
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;
235 die("git-diff-tree: internal error");
237 return 0;
240 static int diff_tree_sha1(const unsigned char *old, const unsigned char *new, const char *base)
242 void *tree1, *tree2;
243 unsigned long size1, size2;
244 int retval;
246 tree1 = read_object_with_reference(old, "tree", &size1, NULL);
247 if (!tree1)
248 die("unable to read source tree (%s)", sha1_to_hex(old));
249 tree2 = read_object_with_reference(new, "tree", &size2, NULL);
250 if (!tree2)
251 die("unable to read destination tree (%s)", sha1_to_hex(new));
252 retval = diff_tree(tree1, size1, tree2, size2, base);
253 free(tree1);
254 free(tree2);
255 return retval;
258 static void call_diff_setup(void)
260 diff_setup(diff_setup_opt);
263 static int call_diff_flush(void)
265 diffcore_std(0,
266 detect_rename, diff_score_opt,
267 pickaxe, pickaxe_opts,
268 diff_break_opt);
269 if (diff_queue_is_empty()) {
270 diff_flush(DIFF_FORMAT_NO_OUTPUT, 0);
271 return 0;
273 if (header) {
274 const char *fmt = "%s";
275 if (diff_output_format == DIFF_FORMAT_MACHINE)
276 fmt = "%s%c";
278 printf(fmt, header, 0);
279 header = NULL;
281 diff_flush(diff_output_format, 1);
282 return 1;
285 static int diff_tree_sha1_top(const unsigned char *old,
286 const unsigned char *new, const char *base)
288 int ret;
290 call_diff_setup();
291 ret = diff_tree_sha1(old, new, base);
292 call_diff_flush();
293 return ret;
296 static int diff_root_tree(const unsigned char *new, const char *base)
298 int retval;
299 void *tree;
300 unsigned long size;
302 call_diff_setup();
303 tree = read_object_with_reference(new, "tree", &size, NULL);
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);
308 call_diff_flush();
309 return retval;
312 static int get_one_line(const char *msg, unsigned long len)
314 int ret = 0;
316 while (len--) {
317 ret++;
318 if (*msg++ == '\n')
319 break;
321 return ret;
324 static int add_author_info(char *buf, const char *line, int len)
326 char *date;
327 unsigned int namelen;
328 unsigned long time;
329 int tz;
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);
339 return sprintf(buf, "Author: %.*s\nDate: %s\n",
340 namelen, line,
341 show_date(time, tz));
344 static char *generate_header(const char *commit, const char *parent, const char *msg, unsigned long len)
346 static char this_header[16384];
347 int offset;
349 offset = sprintf(this_header, "%s%s (from %s)\n", header_prefix, commit, parent);
350 if (verbose_header) {
351 int hdr = 1;
353 for (;;) {
354 const char *line = msg;
355 int linelen = get_one_line(msg, len);
357 if (!linelen)
358 break;
361 * We want some slop for indentation and a possible
362 * final "...". Thus the "+ 20".
364 if (offset + linelen + 20 > sizeof(this_header)) {
365 memcpy(this_header + offset, " ...\n", 8);
366 offset += 8;
367 break;
370 msg += linelen;
371 len -= linelen;
372 if (linelen == 1)
373 hdr = 0;
374 if (hdr) {
375 if (!memcmp(line, "author ", 7))
376 offset += add_author_info(this_header + offset, line, linelen);
377 continue;
379 memset(this_header + offset, ' ', 4);
380 memcpy(this_header + offset + 4, line, linelen);
381 offset += linelen + 4;
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 */
387 this_header[offset++] = '\n';
388 this_header[offset] = 0;
391 return this_header;
394 static int diff_tree_commit(const unsigned char *commit, const char *name)
396 unsigned long size, offset;
397 char *buf = read_object_with_reference(commit, "commit", &size, NULL);
399 if (!buf)
400 return -1;
402 if (!name) {
403 static char commit_name[60];
404 strcpy(commit_name, sha1_to_hex(commit));
405 name = commit_name;
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, "");
414 /* More than one parent? */
415 if (ignore_merges) {
416 if (!memcmp(buf + 46 + 48, "parent ", 7))
417 return 0;
420 offset = 46;
421 while (offset + 48 < size && !memcmp(buf + offset, "parent ", 7)) {
422 unsigned char parent[20];
423 if (get_sha1_hex(buf + offset + 7, parent))
424 return -1;
425 header = generate_header(name, sha1_to_hex(parent), buf, size);
426 diff_tree_sha1_top(parent, commit, "");
427 if (!header && verbose_header) {
428 header_prefix = "\ndiff-tree ";
430 * Don't print multiple merge entries if we
431 * don't print the diffs.
434 offset += 48;
436 return 0;
439 static int diff_tree_stdin(char *line)
441 int len = strlen(line);
442 unsigned char commit[20], parent[20];
443 static char this_header[1000];
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;
455 return diff_tree_sha1_top(parent, commit, "");
457 line[40] = 0;
458 return diff_tree_commit(commit, line);
461 static char *diff_tree_usage =
462 "git-diff-tree [-p] [-r] [-z] [--stdin] [-M] [-C] [-R] [-S<string>] [-m] [-s] [-v] [-t] <tree-ish> <tree-ish>";
464 int main(int argc, const char **argv)
466 int nr_sha1;
467 char line[1000];
468 unsigned char sha1[2][20];
470 nr_sha1 = 0;
471 for (;;) {
472 const char *arg;
474 argv++;
475 argc--;
476 arg = *argv;
477 if (!arg)
478 break;
480 if (*arg != '-') {
481 if (nr_sha1 < 2 && !get_sha1(arg, sha1[nr_sha1])) {
482 nr_sha1++;
483 continue;
485 break;
488 if (!strcmp(arg, "--")) {
489 argv++;
490 argc--;
491 break;
493 if (!strcmp(arg, "-r")) {
494 recursive = 1;
495 continue;
497 if (!strcmp(arg, "-t")) {
498 recursive = show_tree_entry_in_recursive = 1;
499 continue;
501 if (!strcmp(arg, "-R")) {
502 diff_setup_opt |= DIFF_SETUP_REVERSE;
503 continue;
505 if (!strcmp(arg, "-p")) {
506 diff_output_format = DIFF_FORMAT_PATCH;
507 recursive = 1;
508 continue;
510 if (!strncmp(arg, "-S", 2)) {
511 pickaxe = arg + 2;
512 continue;
514 if (!strcmp(arg, "--pickaxe-all")) {
515 pickaxe_opts = DIFF_PICKAXE_ALL;
516 continue;
518 if (!strncmp(arg, "-M", 2)) {
519 detect_rename = DIFF_DETECT_RENAME;
520 diff_score_opt = diff_scoreopt_parse(arg);
521 continue;
523 if (!strncmp(arg, "-C", 2)) {
524 detect_rename = DIFF_DETECT_COPY;
525 diff_score_opt = diff_scoreopt_parse(arg);
526 continue;
528 if (!strncmp(arg, "-B", 2)) {
529 diff_break_opt = diff_scoreopt_parse(arg);
530 continue;
532 if (!strcmp(arg, "-z")) {
533 diff_output_format = DIFF_FORMAT_MACHINE;
534 continue;
536 if (!strcmp(arg, "-m")) {
537 ignore_merges = 0;
538 continue;
540 if (!strcmp(arg, "-s")) {
541 diff_output_format = DIFF_FORMAT_NO_OUTPUT;
542 continue;
544 if (!strcmp(arg, "-v")) {
545 verbose_header = 1;
546 header_prefix = "diff-tree ";
547 continue;
549 if (!strcmp(arg, "--stdin")) {
550 read_stdin = 1;
551 continue;
553 if (!strcmp(arg, "--root")) {
554 show_root_diff = 1;
555 continue;
557 usage(diff_tree_usage);
560 if (argc > 0) {
561 int i;
563 paths = argv;
564 nr_paths = argc;
565 pathlens = xmalloc(nr_paths * sizeof(int));
566 for (i=0; i<nr_paths; i++)
567 pathlens[i] = strlen(paths[i]);
570 switch (nr_sha1) {
571 case 0:
572 if (!read_stdin)
573 usage(diff_tree_usage);
574 break;
575 case 1:
576 diff_tree_commit(sha1[0], NULL);
577 break;
578 case 2:
579 diff_tree_sha1_top(sha1[0], sha1[1], "");
580 break;
583 if (!read_stdin)
584 return 0;
586 if (detect_rename)
587 diff_setup_opt |= (DIFF_SETUP_USE_SIZE_CACHE |
588 DIFF_SETUP_USE_CACHE);
589 while (fgets(line, sizeof(line), stdin))
590 diff_tree_stdin(line);
592 return 0;