git-diff-tree: simplify header output with '-z'
[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 reverse_diff = 0;
14 static int diff_score_opt = 0;
15 static const char *pickaxe = NULL;
16 static const char *header = NULL;
17 static const char *header_prefix = "";
18
19 // What paths are we interested in?
20 static int nr_paths = 0;
21 static const char **paths = NULL;
22 static int *pathlens = NULL;
23
24 static int diff_tree_sha1(const unsigned char *old, const unsigned char *new, const char *base);
25
26 static void update_tree_entry(void **bufp, unsigned long *sizep)
27 {
28         void *buf = *bufp;
29         unsigned long size = *sizep;
30         int len = strlen(buf) + 1 + 20;
31
32         if (size < len)
33                 die("corrupt tree file");
34         *bufp = buf + len;
35         *sizep = size - len;
36 }
37
38 static const unsigned char *extract(void *tree, unsigned long size, const char **pathp, unsigned int *modep)
39 {
40         int len = strlen(tree)+1;
41         const unsigned char *sha1 = tree + len;
42         const char *path = strchr(tree, ' ');
43
44         if (!path || size < len + 20 || sscanf(tree, "%o", modep) != 1)
45                 die("corrupt tree file");
46         *pathp = path+1;
47         return sha1;
48 }
49
50 static char *malloc_base(const char *base, const char *path, int pathlen)
51 {
52         int baselen = strlen(base);
53         char *newbase = xmalloc(baselen + pathlen + 2);
54         memcpy(newbase, base, baselen);
55         memcpy(newbase + baselen, path, pathlen);
56         memcpy(newbase + baselen + pathlen, "/", 2);
57         return newbase;
58 }
59
60 static void show_file(const char *prefix, void *tree, unsigned long size, const char *base);
61 static void show_tree(const char *prefix, void *tree, unsigned long size, const char *base);
62
63 /* A file entry went away or appeared */
64 static void show_file(const char *prefix, void *tree, unsigned long size, const char *base)
65 {
66         unsigned mode;
67         const char *path;
68         const unsigned char *sha1 = extract(tree, size, &path, &mode);
69
70         if (recursive && S_ISDIR(mode)) {
71                 char type[20];
72                 unsigned long size;
73                 char *newbase = malloc_base(base, path, strlen(path));
74                 void *tree;
75
76                 tree = read_sha1_file(sha1, type, &size);
77                 if (!tree || strcmp(type, "tree"))
78                         die("corrupt tree sha %s", sha1_to_hex(sha1));
79
80                 show_tree(prefix, tree, size, newbase);
81
82                 free(tree);
83                 free(newbase);
84                 return;
85         }
86
87         diff_addremove(prefix[0], mode, sha1, base, path);
88 }
89
90 static int compare_tree_entry(void *tree1, unsigned long size1, void *tree2, unsigned long size2, const char *base)
91 {
92         unsigned mode1, mode2;
93         const char *path1, *path2;
94         const unsigned char *sha1, *sha2;
95         int cmp, pathlen1, pathlen2;
96
97         sha1 = extract(tree1, size1, &path1, &mode1);
98         sha2 = extract(tree2, size2, &path2, &mode2);
99
100         pathlen1 = strlen(path1);
101         pathlen2 = strlen(path2);
102         cmp = base_name_compare(path1, pathlen1, mode1, path2, pathlen2, mode2);
103         if (cmp < 0) {
104                 show_file("-", tree1, size1, base);
105                 return -1;
106         }
107         if (cmp > 0) {
108                 show_file("+", tree2, size2, base);
109                 return 1;
110         }
111         if (!memcmp(sha1, sha2, 20) && mode1 == mode2)
112                 return 0;
113
114         /*
115          * If the filemode has changed to/from a directory from/to a regular
116          * file, we need to consider it a remove and an add.
117          */
118         if (S_ISDIR(mode1) != S_ISDIR(mode2)) {
119                 show_file("-", tree1, size1, base);
120                 show_file("+", tree2, size2, base);
121                 return 0;
122         }
123
124         if (recursive && S_ISDIR(mode1)) {
125                 int retval;
126                 char *newbase = malloc_base(base, path1, pathlen1);
127                 if (show_tree_entry_in_recursive)
128                         diff_change(mode1, mode2, sha1, sha2, base, path1);
129                 retval = diff_tree_sha1(sha1, sha2, newbase);
130                 free(newbase);
131                 return retval;
132         }
133
134         diff_change(mode1, mode2, sha1, sha2, base, path1);
135         return 0;
136 }
137
138 static int interesting(void *tree, unsigned long size, const char *base)
139 {
140         const char *path;
141         unsigned mode;
142         int i;
143         int baselen, pathlen;
144
145         if (!nr_paths)
146                 return 1;
147
148         (void)extract(tree, size, &path, &mode);
149
150         pathlen = strlen(path);
151         baselen = strlen(base);
152
153         for (i=0; i < nr_paths; i++) {
154                 const char *match = paths[i];
155                 int matchlen = pathlens[i];
156
157                 if (baselen >= matchlen) {
158                         /* If it doesn't match, move along... */
159                         if (strncmp(base, match, matchlen))
160                                 continue;
161
162                         /* The base is a subdirectory of a path which was specified. */
163                         return 1;
164                 }
165
166                 /* Does the base match? */
167                 if (strncmp(base, match, baselen))
168                         continue;
169
170                 match += baselen;
171                 matchlen -= baselen;
172
173                 if (pathlen > matchlen)
174                         continue;
175
176                 if (matchlen > pathlen) {
177                         if (match[pathlen] != '/')
178                                 continue;
179                         if (!S_ISDIR(mode))
180                                 continue;
181                 }
182
183                 if (strncmp(path, match, pathlen))
184                         continue;
185
186                 return 1;
187         }
188         return 0; /* No matches */
189 }
190
191 /* A whole sub-tree went away or appeared */
192 static void show_tree(const char *prefix, void *tree, unsigned long size, const char *base)
193 {
194         while (size) {
195                 if (interesting(tree, size, base))
196                         show_file(prefix, tree, size, base);
197                 update_tree_entry(&tree, &size);
198         }
199 }
200
201 static int diff_tree(void *tree1, unsigned long size1, void *tree2, unsigned long size2, const char *base)
202 {
203         while (size1 | size2) {
204                 if (nr_paths && size1 && !interesting(tree1, size1, base)) {
205                         update_tree_entry(&tree1, &size1);
206                         continue;
207                 }
208                 if (nr_paths && size2 && !interesting(tree2, size2, base)) {
209                         update_tree_entry(&tree2, &size2);
210                         continue;
211                 }
212                 if (!size1) {
213                         show_file("+", tree2, size2, base);
214                         update_tree_entry(&tree2, &size2);
215                         continue;
216                 }
217                 if (!size2) {
218                         show_file("-", tree1, size1, base);
219                         update_tree_entry(&tree1, &size1);
220                         continue;
221                 }
222                 switch (compare_tree_entry(tree1, size1, tree2, size2, base)) {
223                 case -1:
224                         update_tree_entry(&tree1, &size1);
225                         continue;
226                 case 0:
227                         update_tree_entry(&tree1, &size1);
228                         /* Fallthrough */
229                 case 1:
230                         update_tree_entry(&tree2, &size2);
231                         continue;
232                 }
233                 die("git-diff-tree: internal error");
234         }
235         return 0;
236 }
237
238 static int diff_tree_sha1(const unsigned char *old, const unsigned char *new, const char *base)
239 {
240         void *tree1, *tree2;
241         unsigned long size1, size2;
242         int retval;
243
244         tree1 = read_object_with_reference(old, "tree", &size1, NULL);
245         if (!tree1)
246                 die("unable to read source tree (%s)", sha1_to_hex(old));
247         tree2 = read_object_with_reference(new, "tree", &size2, NULL);
248         if (!tree2)
249                 die("unable to read destination tree (%s)", sha1_to_hex(new));
250         retval = diff_tree(tree1, size1, tree2, size2, base);
251         free(tree1);
252         free(tree2);
253         return retval;
254 }
255
256 static void call_diff_setup(void)
257 {
258         diff_setup(reverse_diff);
259 }
260
261 static int call_diff_flush(void)
262 {
263         if (detect_rename)
264                 diffcore_rename(detect_rename, diff_score_opt);
265         if (pickaxe)
266                 diffcore_pickaxe(pickaxe);
267         if (diff_queue_is_empty()) {
268                 diff_flush(DIFF_FORMAT_NO_OUTPUT, 0);
269                 return 0;
270         }
271         if (header) {
272                 const char *fmt = "%s";
273                 if (diff_output_format == DIFF_FORMAT_MACHINE)
274                         fmt = "%s%c";
275                 
276                 printf(fmt, header, 0);
277                 header = NULL;
278         }
279         diff_flush(diff_output_format, 1);
280         return 1;
281 }
282
283 static int diff_tree_sha1_top(const unsigned char *old,
284                               const unsigned char *new, const char *base)
285 {
286         int ret;
287
288         call_diff_setup();
289         ret = diff_tree_sha1(old, new, base);
290         call_diff_flush();
291         return ret;
292 }
293
294 static int diff_root_tree(const unsigned char *new, const char *base)
295 {
296         int retval;
297         void *tree;
298         unsigned long size;
299
300         call_diff_setup();
301         tree = read_object_with_reference(new, "tree", &size, NULL);
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);
306         call_diff_flush();
307         return retval;
308 }
309
310 static 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
322 static 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
342 static char *generate_header(const char *commit, const char *parent, const char *msg, unsigned long len)
343 {
344         static char this_header[16384];
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;
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;
365                                 break;
366                         }
367
368                         msg += linelen;
369                         len -= linelen;
370                         if (linelen == 1)
371                                 hdr = 0;
372                         if (hdr) {
373                                 if (!memcmp(line, "author ", 7))
374                                         offset += add_author_info(this_header + offset, line, linelen);
375                                 continue;
376                         }
377                         memset(this_header + offset, ' ', 4);
378                         memcpy(this_header + offset + 4, line, linelen);
379                         offset += linelen + 4;
380                 }
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 */
385                 this_header[offset++] = '\n';
386                 this_header[offset] = 0;
387         }
388
389         return this_header;
390 }
391
392 static int diff_tree_commit(const unsigned char *commit, const char *name)
393 {
394         unsigned long size, offset;
395         char *buf = read_object_with_reference(commit, "commit", &size, NULL);
396
397         if (!buf)
398                 return -1;
399
400         if (!name) {
401                 static char commit_name[60];
402                 strcpy(commit_name, sha1_to_hex(commit));
403                 name = commit_name;
404         }
405
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
418         offset = 46;
419         while (offset + 48 < size && !memcmp(buf + offset, "parent ", 7)) {
420                 unsigned char parent[20];
421                 if (get_sha1_hex(buf + offset + 7, parent))
422                         return -1;
423                 header = generate_header(name, sha1_to_hex(parent), buf, size);
424                 diff_tree_sha1_top(parent, commit, "");
425                 if (!header && verbose_header) {
426                         header_prefix = "\ndiff-tree ";
427                         /*
428                          * Don't print multiple merge entries if we
429                          * don't print the diffs.
430                          */
431                 }
432                 offset += 48;
433         }
434         return 0;
435 }
436
437 static 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;
453                 return diff_tree_sha1_top(parent, commit, "");
454         }
455         line[40] = 0;
456         return diff_tree_commit(commit, line);
457 }
458
459 static char *diff_tree_usage =
460 "git-diff-tree [-p] [-r] [-z] [--stdin] [-M] [-C] [-R] [-S<string>] [-m] [-s] [-v] [-t] <tree-ish> <tree-ish>";
461
462 int main(int argc, const char **argv)
463 {
464         int nr_sha1;
465         char line[1000];
466         unsigned char sha1[2][20];
467
468         nr_sha1 = 0;
469         for (;;) {
470                 const char *arg;
471
472                 argv++;
473                 argc--;
474                 arg = *argv;
475                 if (!arg)
476                         break;
477
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, "--")) {
487                         argv++;
488                         argc--;
489                         break;
490                 }
491                 if (!strcmp(arg, "-r")) {
492                         recursive = 1;
493                         continue;
494                 }
495                 if (!strcmp(arg, "-t")) {
496                         recursive = show_tree_entry_in_recursive = 1;
497                         continue;
498                 }
499                 if (!strcmp(arg, "-R")) {
500                         reverse_diff = 1;
501                         continue;
502                 }
503                 if (!strcmp(arg, "-p")) {
504                         diff_output_format = DIFF_FORMAT_PATCH;
505                         recursive = 1;
506                         continue;
507                 }
508                 if (!strncmp(arg, "-S", 2)) {
509                         pickaxe = arg + 2;
510                         continue;
511                 }
512                 if (!strncmp(arg, "-M", 2)) {
513                         detect_rename = DIFF_DETECT_RENAME;
514                         diff_score_opt = diff_scoreopt_parse(arg);
515                         continue;
516                 }
517                 if (!strncmp(arg, "-C", 2)) {
518                         detect_rename = DIFF_DETECT_COPY;
519                         diff_score_opt = diff_scoreopt_parse(arg);
520                         continue;
521                 }
522                 if (!strcmp(arg, "-z")) {
523                         diff_output_format = DIFF_FORMAT_MACHINE;
524                         continue;
525                 }
526                 if (!strcmp(arg, "-m")) {
527                         ignore_merges = 0;
528                         continue;
529                 }
530                 if (!strcmp(arg, "-s")) {
531                         diff_output_format = DIFF_FORMAT_NO_OUTPUT;
532                         continue;
533                 }
534                 if (!strcmp(arg, "-v")) {
535                         verbose_header = 1;
536                         header_prefix = "diff-tree ";
537                         continue;
538                 }
539                 if (!strcmp(arg, "--stdin")) {
540                         read_stdin = 1;
541                         continue;
542                 }
543                 if (!strcmp(arg, "--root")) {
544                         show_root_diff = 1;
545                         continue;
546                 }
547                 usage(diff_tree_usage);
548         }
549
550         if (argc > 0) {
551                 int i;
552
553                 paths = argv;
554                 nr_paths = argc;
555                 pathlens = xmalloc(nr_paths * sizeof(int));
556                 for (i=0; i<nr_paths; i++)
557                         pathlens[i] = strlen(paths[i]);
558         }
559
560         switch (nr_sha1) {
561         case 0:
562                 if (!read_stdin)
563                         usage(diff_tree_usage);
564                 break;
565         case 1:
566                 diff_tree_commit(sha1[0], NULL);
567                 break;
568         case 2:
569                 diff_tree_sha1_top(sha1[0], sha1[1], "");
570                 break;
571         }
572
573         if (!read_stdin)
574                 return 0;
575
576         while (fgets(line, sizeof(line), stdin))
577                 diff_tree_stdin(line);
578
579         return 0;
580 }