diff-tree: fix "whole sub-tree disappeared or appeared" case
[git.git] / diff-tree.c
1 #include <ctype.h>
2 #include "cache.h"
3 #include "diff.h"
4
5 static int silent = 0;
6 static int verbose_header = 0;
7 static int ignore_merges = 1;
8 static int recursive = 0;
9 static int read_stdin = 0;
10 static int line_termination = '\n';
11 static int generate_patch = 0;
12 static const char *header = NULL;
13 static const char *header_prefix = "";
14
15 // What paths are we interested in?
16 static int nr_paths = 0;
17 static char **paths = NULL;
18 static int *pathlens = NULL;
19
20 static int diff_tree_sha1(const unsigned char *old, const unsigned char *new, const char *base);
21
22 static void update_tree_entry(void **bufp, unsigned long *sizep)
23 {
24         void *buf = *bufp;
25         unsigned long size = *sizep;
26         int len = strlen(buf) + 1 + 20;
27
28         if (size < len)
29                 die("corrupt tree file");
30         *bufp = buf + len;
31         *sizep = size - len;
32 }
33
34 static const unsigned char *extract(void *tree, unsigned long size, const char **pathp, unsigned int *modep)
35 {
36         int len = strlen(tree)+1;
37         const unsigned char *sha1 = tree + len;
38         const char *path = strchr(tree, ' ');
39
40         if (!path || size < len + 20 || sscanf(tree, "%o", modep) != 1)
41                 die("corrupt tree file");
42         *pathp = path+1;
43         return sha1;
44 }
45
46 static char *malloc_base(const char *base, const char *path, int pathlen)
47 {
48         int baselen = strlen(base);
49         char *newbase = xmalloc(baselen + pathlen + 2);
50         memcpy(newbase, base, baselen);
51         memcpy(newbase + baselen, path, pathlen);
52         memcpy(newbase + baselen + pathlen, "/", 2);
53         return newbase;
54 }
55
56 static void show_file(const char *prefix, void *tree, unsigned long size, const char *base);
57 static void show_tree(const char *prefix, void *tree, unsigned long size, const char *base);
58
59 /* A file entry went away or appeared */
60 static void show_file(const char *prefix, void *tree, unsigned long size, const char *base)
61 {
62         unsigned mode;
63         const char *path;
64         const unsigned char *sha1 = extract(tree, size, &path, &mode);
65
66         if (header) {
67                 printf("%s", header);
68                 header = NULL;
69         }
70
71         if (silent)
72                 return;
73
74         if (recursive && S_ISDIR(mode)) {
75                 char type[20];
76                 unsigned long size;
77                 char *newbase = malloc_base(base, path, strlen(path));
78                 void *tree;
79
80                 tree = read_sha1_file(sha1, type, &size);
81                 if (!tree || strcmp(type, "tree"))
82                         die("corrupt tree sha %s", sha1_to_hex(sha1));
83
84                 show_tree(prefix, tree, size, newbase);
85                 
86                 free(tree);
87                 free(newbase);
88                 return;
89         }
90
91         if (generate_patch) {
92                 if (!S_ISDIR(mode))
93                         diff_addremove(prefix[0], mode, sha1, base, path);
94         }
95         else
96                 printf("%s%06o\t%s\t%s\t%s%s%c", prefix, mode,
97                        S_ISDIR(mode) ? "tree" : "blob",
98                        sha1_to_hex(sha1), base, path,
99                        line_termination);
100 }
101
102 static int compare_tree_entry(void *tree1, unsigned long size1, void *tree2, unsigned long size2, const char *base)
103 {
104         unsigned mode1, mode2;
105         const char *path1, *path2;
106         const unsigned char *sha1, *sha2;
107         int cmp, pathlen1, pathlen2;
108         char old_sha1_hex[50];
109
110         sha1 = extract(tree1, size1, &path1, &mode1);
111         sha2 = extract(tree2, size2, &path2, &mode2);
112
113         pathlen1 = strlen(path1);
114         pathlen2 = strlen(path2);
115         cmp = cache_name_compare(path1, pathlen1, path2, pathlen2);
116         if (cmp < 0) {
117                 show_file("-", tree1, size1, base);
118                 return -1;
119         }
120         if (cmp > 0) {
121                 show_file("+", tree2, size2, base);
122                 return 1;
123         }
124         if (!memcmp(sha1, sha2, 20) && mode1 == mode2)
125                 return 0;
126
127         /*
128          * If the filemode has changed to/from a directory from/to a regular
129          * file, we need to consider it a remove and an add.
130          */
131         if (S_ISDIR(mode1) != S_ISDIR(mode2)) {
132                 show_file("-", tree1, size1, base);
133                 show_file("+", tree2, size2, base);
134                 return 0;
135         }
136
137         if (recursive && S_ISDIR(mode1)) {
138                 int retval;
139                 char *newbase = malloc_base(base, path1, pathlen1);
140                 retval = diff_tree_sha1(sha1, sha2, newbase);
141                 free(newbase);
142                 return retval;
143         }
144
145         if (header) {
146                 printf("%s", header);
147                 header = NULL;
148         }
149         if (silent)
150                 return 0;
151
152         if (generate_patch) {
153                 if (!S_ISDIR(mode1))
154                         diff_change(mode1, mode2, sha1, sha2, base, path1);
155         }
156         else {
157                 strcpy(old_sha1_hex, sha1_to_hex(sha1));
158                 printf("*%06o->%06o\t%s\t%s->%s\t%s%s%c", mode1, mode2,
159                        S_ISDIR(mode1) ? "tree" : "blob",
160                        old_sha1_hex, sha1_to_hex(sha2), base, path1,
161                        line_termination);
162         }
163         return 0;
164 }
165
166 static int interesting(void *tree, unsigned long size, const char *base)
167 {
168         const char *path;
169         unsigned mode;
170         int i;
171         int baselen, pathlen;
172
173         if (!nr_paths)
174                 return 1;
175
176         (void)extract(tree, size, &path, &mode);
177
178         pathlen = strlen(path);
179         baselen = strlen(base);
180
181         for (i=0; i < nr_paths; i++) {
182                 const char *match = paths[i];
183                 int matchlen = pathlens[i];
184
185                 if (baselen >= matchlen) {
186                         /* If it doesn't match, move along... */
187                         if (strncmp(base, match, matchlen))
188                                 continue;
189
190                         /* The base is a subdirectory of a path which was specified. */
191                         return 1;
192                 }
193
194                 /* Does the base match? */
195                 if (strncmp(base, match, baselen))
196                         continue;
197
198                 match += baselen;
199                 matchlen -= baselen;
200
201                 if (pathlen > matchlen)
202                         continue;
203
204                 if (matchlen > pathlen) {
205                         if (match[pathlen] != '/')
206                                 continue;
207                 }
208
209                 if (strncmp(path, match, pathlen))
210                         continue;
211
212                 return 1;
213         }
214         return 0; /* No matches */
215 }
216
217 /* A whole sub-tree went away or appeared */
218 static void show_tree(const char *prefix, void *tree, unsigned long size, const char *base)
219 {
220         while (size) {
221                 if (interesting(tree, size, base))
222                         show_file(prefix, tree, size, base);
223                 update_tree_entry(&tree, &size);
224         }
225 }
226
227 static int diff_tree(void *tree1, unsigned long size1, void *tree2, unsigned long size2, const char *base)
228 {
229         while (size1 | size2) {
230                 if (nr_paths && size1 && !interesting(tree1, size1, base)) {
231                         update_tree_entry(&tree1, &size1);
232                         continue;
233                 }
234                 if (nr_paths && size2 && !interesting(tree2, size2, base)) {
235                         update_tree_entry(&tree2, &size2);
236                         continue;
237                 }
238                 if (!size1) {
239                         show_file("+", tree2, size2, base);
240                         update_tree_entry(&tree2, &size2);
241                         continue;
242                 }
243                 if (!size2) {
244                         show_file("-", tree1, size1, base);
245                         update_tree_entry(&tree1, &size1);
246                         continue;
247                 }
248                 switch (compare_tree_entry(tree1, size1, tree2, size2, base)) {
249                 case -1:
250                         update_tree_entry(&tree1, &size1);
251                         continue;
252                 case 0:
253                         update_tree_entry(&tree1, &size1);
254                         /* Fallthrough */
255                 case 1:
256                         update_tree_entry(&tree2, &size2);
257                         continue;
258                 }
259                 die("diff-tree: internal error");
260         }
261         return 0;
262 }
263
264 static int diff_tree_sha1(const unsigned char *old, const unsigned char *new, const char *base)
265 {
266         void *tree1, *tree2;
267         unsigned long size1, size2;
268         int retval;
269
270         tree1 = read_object_with_reference(old, "tree", &size1, 0);
271         if (!tree1)
272                 die("unable to read source tree (%s)", sha1_to_hex(old));
273         tree2 = read_object_with_reference(new, "tree", &size2, 0);
274         if (!tree2)
275                 die("unable to read destination tree (%s)", sha1_to_hex(new));
276         retval = diff_tree(tree1, size1, tree2, size2, base);
277         free(tree1);
278         free(tree2);
279         return retval;
280 }
281
282 static int get_one_line(const char *msg, unsigned long len)
283 {
284         int ret = 0;
285
286         while (len--) {
287                 ret++;
288                 if (*msg++ == '\n')
289                         break;
290         }
291         return ret;
292 }
293
294 static int add_author_info(char *buf, const char *line, int len)
295 {
296         char *date;
297         unsigned int namelen;
298         unsigned long time;
299         int tz;
300
301         line += strlen("author ");
302         date = strchr(line, '>');
303         if (!date)
304                 return 0;
305         namelen = ++date - line;
306         time = strtoul(date, &date, 10);
307         tz = strtol(date, NULL, 10);
308
309         return sprintf(buf, "Author: %.*s\nDate:   %s\n",
310                 namelen, line,
311                 show_date(time, tz));
312 }
313
314 static char *generate_header(const char *commit, const char *parent, const char *msg, unsigned long len)
315 {
316         static char this_header[1000];
317         int offset;
318
319         offset = sprintf(this_header, "%s%s (from %s)\n", header_prefix, commit, parent);
320         if (verbose_header) {
321                 int hdr = 1;
322
323                 for (;;) {
324                         const char *line = msg;
325                         int linelen = get_one_line(msg, len);
326
327                         if (!linelen)
328                                 break;
329                         if (offset + linelen + 10 > sizeof(this_header))
330                                 break;
331
332                         msg += linelen;
333                         len -= linelen;
334                         if (linelen == 1)
335                                 hdr = 0;
336                         if (hdr) {
337                                 if (!memcmp(line, "author ", 7))
338                                         offset += add_author_info(this_header + offset, line, linelen);
339                                 continue;
340                         }
341                         memset(this_header + offset, ' ', 4);
342                         memcpy(this_header + offset + 4, line, linelen);
343                         offset += linelen + 4;
344                 }
345                 this_header[offset++] = '\n';
346                 this_header[offset] = 0;
347         }
348
349         return this_header;
350 }
351
352 static int diff_tree_commit(const unsigned char *commit, const char *name)
353 {
354         unsigned long size, offset;
355         char *buf = read_object_with_reference(commit, "commit", &size, NULL);
356
357         if (!buf)
358                 return -1;
359
360         /* More than one parent? */
361         if (ignore_merges) {
362                 if (!memcmp(buf + 46 + 48, "parent ", 7))
363                         return 0;
364         }
365
366         if (!name) {
367                 static char commit_name[60];
368                 strcpy(commit_name, sha1_to_hex(commit));
369                 name = commit_name;
370         }
371
372         offset = 46;
373         while (offset + 48 < size && !memcmp(buf + offset, "parent ", 7)) {
374                 unsigned char parent[20];
375                 if (get_sha1_hex(buf + offset + 7, parent))
376                         return -1;
377                 header = generate_header(name, sha1_to_hex(parent), buf, size);
378                 diff_tree_sha1(parent, commit, "");
379                 if (!header && verbose_header)
380                         header_prefix = "\ndiff-tree ";
381                 offset += 48;
382         }
383         return 0;
384 }
385
386 static int diff_tree_stdin(char *line)
387 {
388         int len = strlen(line);
389         unsigned char commit[20], parent[20];
390         static char this_header[1000];
391
392         if (!len || line[len-1] != '\n')
393                 return -1;
394         line[len-1] = 0;
395         if (get_sha1_hex(line, commit))
396                 return -1;
397         if (isspace(line[40]) && !get_sha1_hex(line+41, parent)) {
398                 line[40] = 0;
399                 line[81] = 0;
400                 sprintf(this_header, "%s (from %s)\n", line, line+41);
401                 header = this_header;
402                 return diff_tree_sha1(parent, commit, "");
403         }
404         line[40] = 0;
405         return diff_tree_commit(commit, line);
406 }
407
408 static char *diff_tree_usage =
409 "diff-tree [-p] [-r] [-z] [--stdin] [-m] [-s] [-v] <tree sha1> <tree sha1>";
410
411 int main(int argc, char **argv)
412 {
413         int nr_sha1;
414         char line[1000];
415         unsigned char sha1[2][20];
416
417         nr_sha1 = 0;
418         for (;;) {
419                 char *arg;
420
421                 argv++;
422                 argc--;
423                 arg = *argv;
424                 if (!arg)
425                         break;
426
427                 if (*arg != '-') {
428                         if (nr_sha1 < 2 && !get_sha1(arg, sha1[nr_sha1])) {
429                                 nr_sha1++;
430                                 continue;
431                         }
432                         break;
433                 }
434
435                 if (!strcmp(arg, "--")) {
436                         argv++;
437                         argc--;
438                         break;
439                 }
440                 if (!strcmp(arg, "-r")) {
441                         recursive = 1;
442                         continue;
443                 }
444                 if (!strcmp(arg, "-p")) {
445                         recursive = generate_patch = 1;
446                         continue;
447                 }
448                 if (!strcmp(arg, "-z")) {
449                         line_termination = '\0';
450                         continue;
451                 }
452                 if (!strcmp(arg, "-m")) {
453                         ignore_merges = 0;
454                         continue;
455                 }
456                 if (!strcmp(arg, "-s")) {
457                         silent = 1;
458                         continue;
459                 }
460                 if (!strcmp(arg, "-v")) {
461                         verbose_header = 1;
462                         header_prefix = "diff-tree ";
463                         continue;
464                 }
465                 if (!strcmp(arg, "--stdin")) {
466                         read_stdin = 1;
467                         continue;
468                 }
469                 usage(diff_tree_usage);
470         }
471
472         if (argc > 0) {
473                 int i;
474
475                 paths = argv;
476                 nr_paths = argc;
477                 pathlens = xmalloc(nr_paths * sizeof(int));
478                 for (i=0; i<nr_paths; i++)
479                         pathlens[i] = strlen(paths[i]);
480         }
481
482         switch (nr_sha1) {
483         case 0:
484                 if (!read_stdin)
485                         usage(diff_tree_usage);
486                 break;
487         case 1:
488                 diff_tree_commit(sha1[0], NULL);
489                 break;
490         case 2:
491                 diff_tree_sha1(sha1[0], sha1[1], "");
492                 break;
493         }
494
495         if (!read_stdin)
496                 return 0;
497
498         while (fgets(line, sizeof(line), stdin))
499                 diff_tree_stdin(line);
500
501         return 0;
502 }