[PATCH] Add -B flag to diff-* brothers.
[git.git] / diff-tree.c
1 #include <ctype.h>
2 #include "cache.h"
3 #include "diff.h"
4
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 = "";
20
21 // What paths are we interested in?
22 static int nr_paths = 0;
23 static const char **paths = NULL;
24 static int *pathlens = NULL;
25
26 static int diff_tree_sha1(const unsigned char *old, const unsigned char *new, const char *base);
27
28 static void update_tree_entry(void **bufp, unsigned long *sizep)
29 {
30         void *buf = *bufp;
31         unsigned long size = *sizep;
32         int len = strlen(buf) + 1 + 20;
33
34         if (size < len)
35                 die("corrupt tree file");
36         *bufp = buf + len;
37         *sizep = size - len;
38 }
39
40 static 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)
47                 die("corrupt tree file");
48         *pathp = path+1;
49         return sha1;
50 }
51
52 static char *malloc_base(const char *base, const char *path, int pathlen)
53 {
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;
60 }
61
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);
64
65 /* A file entry went away or appeared */
66 static void show_file(const char *prefix, void *tree, unsigned long size, const char *base)
67 {
68         unsigned mode;
69         const char *path;
70         const unsigned char *sha1 = extract(tree, size, &path, &mode);
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"))
80                         die("corrupt tree sha %s", sha1_to_hex(sha1));
81
82                 show_tree(prefix, tree, size, newbase);
83
84                 free(tree);
85                 free(newbase);
86                 return;
87         }
88
89         diff_addremove(prefix[0], mode, sha1, base, path);
90 }
91
92 static int compare_tree_entry(void *tree1, unsigned long size1, void *tree2, unsigned long size2, const char *base)
93 {
94         unsigned mode1, mode2;
95         const char *path1, *path2;
96         const unsigned char *sha1, *sha2;
97         int cmp, pathlen1, pathlen2;
98
99         sha1 = extract(tree1, size1, &path1, &mode1);
100         sha2 = extract(tree2, size2, &path2, &mode2);
101
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;
108         }
109         if (cmp > 0) {
110                 show_file("+", tree2, size2, base);
111                 return 1;
112         }
113         if (!memcmp(sha1, sha2, 20) && mode1 == mode2)
114                 return 0;
115
116         /*
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.
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)) {
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;
134         }
135
136         diff_change(mode1, mode2, sha1, sha2, base, path1);
137         return 0;
138 }
139
140 static 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
178                 if (matchlen > pathlen) {
179                         if (match[pathlen] != '/')
180                                 continue;
181                         if (!S_ISDIR(mode))
182                                 continue;
183                 }
184
185                 if (strncmp(path, match, pathlen))
186                         continue;
187
188                 return 1;
189         }
190         return 0; /* No matches */
191 }
192
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)
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
203 static int diff_tree(void *tree1, unsigned long size1, void *tree2, unsigned long size2, const char *base)
204 {
205         while (size1 | size2) {
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                 }
214                 if (!size1) {
215                         show_file("+", tree2, size2, base);
216                         update_tree_entry(&tree2, &size2);
217                         continue;
218                 }
219                 if (!size2) {
220                         show_file("-", tree1, size1, base);
221                         update_tree_entry(&tree1, &size1);
222                         continue;
223                 }
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;
234                 }
235                 die("git-diff-tree: internal error");
236         }
237         return 0;
238 }
239
240 static int diff_tree_sha1(const unsigned char *old, const unsigned char *new, const char *base)
241 {
242         void *tree1, *tree2;
243         unsigned long size1, size2;
244         int retval;
245
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;
256 }
257
258 static void call_diff_setup(void)
259 {
260         diff_setup(diff_setup_opt);
261 }
262
263 static int call_diff_flush(void)
264 {
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;
272         }
273         if (header) {
274                 const char *fmt = "%s";
275                 if (diff_output_format == DIFF_FORMAT_MACHINE)
276                         fmt = "%s%c";
277                 
278                 printf(fmt, header, 0);
279                 header = NULL;
280         }
281         diff_flush(diff_output_format, 1);
282         return 1;
283 }
284
285 static int diff_tree_sha1_top(const unsigned char *old,
286                               const unsigned char *new, const char *base)
287 {
288         int ret;
289
290         call_diff_setup();
291         ret = diff_tree_sha1(old, new, base);
292         call_diff_flush();
293         return ret;
294 }
295
296 static int diff_root_tree(const unsigned char *new, const char *base)
297 {
298         int retval;
299         void *tree;
300         unsigned long size;
301
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;
310 }
311
312 static 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
324 static 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
344 static char *generate_header(const char *commit, const char *parent, const char *msg, unsigned long len)
345 {
346         static char this_header[16384];
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;
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;
367                                 break;
368                         }
369
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;
378                         }
379                         memset(this_header + offset, ' ', 4);
380                         memcpy(this_header + offset + 4, line, linelen);
381                         offset += linelen + 4;
382                 }
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;
389         }
390
391         return this_header;
392 }
393
394 static int diff_tree_commit(const unsigned char *commit, const char *name)
395 {
396         unsigned long size, offset;
397         char *buf = read_object_with_reference(commit, "commit", &size, NULL);
398
399         if (!buf)
400                 return -1;
401
402         if (!name) {
403                 static char commit_name[60];
404                 strcpy(commit_name, sha1_to_hex(commit));
405                 name = commit_name;
406         }
407
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
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 ";
429                         /*
430                          * Don't print multiple merge entries if we
431                          * don't print the diffs.
432                          */
433                 }
434                 offset += 48;
435         }
436         return 0;
437 }
438
439 static 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;
455                 return diff_tree_sha1_top(parent, commit, "");
456         }
457         line[40] = 0;
458         return diff_tree_commit(commit, line);
459 }
460
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>";
463
464 int main(int argc, const char **argv)
465 {
466         int nr_sha1;
467         char line[1000];
468         unsigned char sha1[2][20];
469
470         nr_sha1 = 0;
471         for (;;) {
472                 const char *arg;
473
474                 argv++;
475                 argc--;
476                 arg = *argv;
477                 if (!arg)
478                         break;
479
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, "--")) {
489                         argv++;
490                         argc--;
491                         break;
492                 }
493                 if (!strcmp(arg, "-r")) {
494                         recursive = 1;
495                         continue;
496                 }
497                 if (!strcmp(arg, "-t")) {
498                         recursive = show_tree_entry_in_recursive = 1;
499                         continue;
500                 }
501                 if (!strcmp(arg, "-R")) {
502                         diff_setup_opt |= DIFF_SETUP_REVERSE;
503                         continue;
504                 }
505                 if (!strcmp(arg, "-p")) {
506                         diff_output_format = DIFF_FORMAT_PATCH;
507                         recursive = 1;
508                         continue;
509                 }
510                 if (!strncmp(arg, "-S", 2)) {
511                         pickaxe = arg + 2;
512                         continue;
513                 }
514                 if (!strcmp(arg, "--pickaxe-all")) {
515                         pickaxe_opts = DIFF_PICKAXE_ALL;
516                         continue;
517                 }
518                 if (!strncmp(arg, "-M", 2)) {
519                         detect_rename = DIFF_DETECT_RENAME;
520                         diff_score_opt = diff_scoreopt_parse(arg);
521                         continue;
522                 }
523                 if (!strncmp(arg, "-C", 2)) {
524                         detect_rename = DIFF_DETECT_COPY;
525                         diff_score_opt = diff_scoreopt_parse(arg);
526                         continue;
527                 }
528                 if (!strncmp(arg, "-B", 2)) {
529                         diff_break_opt = diff_scoreopt_parse(arg);
530                         continue;
531                 }
532                 if (!strcmp(arg, "-z")) {
533                         diff_output_format = DIFF_FORMAT_MACHINE;
534                         continue;
535                 }
536                 if (!strcmp(arg, "-m")) {
537                         ignore_merges = 0;
538                         continue;
539                 }
540                 if (!strcmp(arg, "-s")) {
541                         diff_output_format = DIFF_FORMAT_NO_OUTPUT;
542                         continue;
543                 }
544                 if (!strcmp(arg, "-v")) {
545                         verbose_header = 1;
546                         header_prefix = "diff-tree ";
547                         continue;
548                 }
549                 if (!strcmp(arg, "--stdin")) {
550                         read_stdin = 1;
551                         continue;
552                 }
553                 if (!strcmp(arg, "--root")) {
554                         show_root_diff = 1;
555                         continue;
556                 }
557                 usage(diff_tree_usage);
558         }
559
560         if (argc > 0) {
561                 int i;
562
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]);
568         }
569
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;
581         }
582
583         if (!read_stdin)
584                 return 0;
585
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);
591
592         return 0;
593 }