diff-tree: don't match non-directories as partial pathnames
[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                         if (!S_ISDIR(mode))
208                                 continue;
209                 }
210
211                 if (strncmp(path, match, pathlen))
212                         continue;
213
214                 return 1;
215         }
216         return 0; /* No matches */
217 }
218
219 /* A whole sub-tree went away or appeared */
220 static void show_tree(const char *prefix, void *tree, unsigned long size, const char *base)
221 {
222         while (size) {
223                 if (interesting(tree, size, base))
224                         show_file(prefix, tree, size, base);
225                 update_tree_entry(&tree, &size);
226         }
227 }
228
229 static int diff_tree(void *tree1, unsigned long size1, void *tree2, unsigned long size2, const char *base)
230 {
231         while (size1 | size2) {
232                 if (nr_paths && size1 && !interesting(tree1, size1, base)) {
233                         update_tree_entry(&tree1, &size1);
234                         continue;
235                 }
236                 if (nr_paths && size2 && !interesting(tree2, size2, base)) {
237                         update_tree_entry(&tree2, &size2);
238                         continue;
239                 }
240                 if (!size1) {
241                         show_file("+", tree2, size2, base);
242                         update_tree_entry(&tree2, &size2);
243                         continue;
244                 }
245                 if (!size2) {
246                         show_file("-", tree1, size1, base);
247                         update_tree_entry(&tree1, &size1);
248                         continue;
249                 }
250                 switch (compare_tree_entry(tree1, size1, tree2, size2, base)) {
251                 case -1:
252                         update_tree_entry(&tree1, &size1);
253                         continue;
254                 case 0:
255                         update_tree_entry(&tree1, &size1);
256                         /* Fallthrough */
257                 case 1:
258                         update_tree_entry(&tree2, &size2);
259                         continue;
260                 }
261                 die("diff-tree: internal error");
262         }
263         return 0;
264 }
265
266 static int diff_tree_sha1(const unsigned char *old, const unsigned char *new, const char *base)
267 {
268         void *tree1, *tree2;
269         unsigned long size1, size2;
270         int retval;
271
272         tree1 = read_object_with_reference(old, "tree", &size1, 0);
273         if (!tree1)
274                 die("unable to read source tree (%s)", sha1_to_hex(old));
275         tree2 = read_object_with_reference(new, "tree", &size2, 0);
276         if (!tree2)
277                 die("unable to read destination tree (%s)", sha1_to_hex(new));
278         retval = diff_tree(tree1, size1, tree2, size2, base);
279         free(tree1);
280         free(tree2);
281         return retval;
282 }
283
284 static int get_one_line(const char *msg, unsigned long len)
285 {
286         int ret = 0;
287
288         while (len--) {
289                 ret++;
290                 if (*msg++ == '\n')
291                         break;
292         }
293         return ret;
294 }
295
296 static int add_author_info(char *buf, const char *line, int len)
297 {
298         char *date;
299         unsigned int namelen;
300         unsigned long time;
301         int tz;
302
303         line += strlen("author ");
304         date = strchr(line, '>');
305         if (!date)
306                 return 0;
307         namelen = ++date - line;
308         time = strtoul(date, &date, 10);
309         tz = strtol(date, NULL, 10);
310
311         return sprintf(buf, "Author: %.*s\nDate:   %s\n",
312                 namelen, line,
313                 show_date(time, tz));
314 }
315
316 static char *generate_header(const char *commit, const char *parent, const char *msg, unsigned long len)
317 {
318         static char this_header[1000];
319         int offset;
320
321         offset = sprintf(this_header, "%s%s (from %s)\n", header_prefix, commit, parent);
322         if (verbose_header) {
323                 int hdr = 1;
324
325                 for (;;) {
326                         const char *line = msg;
327                         int linelen = get_one_line(msg, len);
328
329                         if (!linelen)
330                                 break;
331                         if (offset + linelen + 10 > sizeof(this_header))
332                                 break;
333
334                         msg += linelen;
335                         len -= linelen;
336                         if (linelen == 1)
337                                 hdr = 0;
338                         if (hdr) {
339                                 if (!memcmp(line, "author ", 7))
340                                         offset += add_author_info(this_header + offset, line, linelen);
341                                 continue;
342                         }
343                         memset(this_header + offset, ' ', 4);
344                         memcpy(this_header + offset + 4, line, linelen);
345                         offset += linelen + 4;
346                 }
347                 this_header[offset++] = '\n';
348                 this_header[offset] = 0;
349         }
350
351         return this_header;
352 }
353
354 static int diff_tree_commit(const unsigned char *commit, const char *name)
355 {
356         unsigned long size, offset;
357         char *buf = read_object_with_reference(commit, "commit", &size, NULL);
358
359         if (!buf)
360                 return -1;
361
362         /* More than one parent? */
363         if (ignore_merges) {
364                 if (!memcmp(buf + 46 + 48, "parent ", 7))
365                         return 0;
366         }
367
368         if (!name) {
369                 static char commit_name[60];
370                 strcpy(commit_name, sha1_to_hex(commit));
371                 name = commit_name;
372         }
373
374         offset = 46;
375         while (offset + 48 < size && !memcmp(buf + offset, "parent ", 7)) {
376                 unsigned char parent[20];
377                 if (get_sha1_hex(buf + offset + 7, parent))
378                         return -1;
379                 header = generate_header(name, sha1_to_hex(parent), buf, size);
380                 diff_tree_sha1(parent, commit, "");
381                 if (!header && verbose_header)
382                         header_prefix = "\ndiff-tree ";
383                 offset += 48;
384         }
385         return 0;
386 }
387
388 static int diff_tree_stdin(char *line)
389 {
390         int len = strlen(line);
391         unsigned char commit[20], parent[20];
392         static char this_header[1000];
393
394         if (!len || line[len-1] != '\n')
395                 return -1;
396         line[len-1] = 0;
397         if (get_sha1_hex(line, commit))
398                 return -1;
399         if (isspace(line[40]) && !get_sha1_hex(line+41, parent)) {
400                 line[40] = 0;
401                 line[81] = 0;
402                 sprintf(this_header, "%s (from %s)\n", line, line+41);
403                 header = this_header;
404                 return diff_tree_sha1(parent, commit, "");
405         }
406         line[40] = 0;
407         return diff_tree_commit(commit, line);
408 }
409
410 static char *diff_tree_usage =
411 "diff-tree [-p] [-r] [-z] [--stdin] [-m] [-s] [-v] <tree sha1> <tree sha1>";
412
413 int main(int argc, char **argv)
414 {
415         int nr_sha1;
416         char line[1000];
417         unsigned char sha1[2][20];
418
419         nr_sha1 = 0;
420         for (;;) {
421                 char *arg;
422
423                 argv++;
424                 argc--;
425                 arg = *argv;
426                 if (!arg)
427                         break;
428
429                 if (*arg != '-') {
430                         if (nr_sha1 < 2 && !get_sha1(arg, sha1[nr_sha1])) {
431                                 nr_sha1++;
432                                 continue;
433                         }
434                         break;
435                 }
436
437                 if (!strcmp(arg, "--")) {
438                         argv++;
439                         argc--;
440                         break;
441                 }
442                 if (!strcmp(arg, "-r")) {
443                         recursive = 1;
444                         continue;
445                 }
446                 if (!strcmp(arg, "-p")) {
447                         recursive = generate_patch = 1;
448                         continue;
449                 }
450                 if (!strcmp(arg, "-z")) {
451                         line_termination = '\0';
452                         continue;
453                 }
454                 if (!strcmp(arg, "-m")) {
455                         ignore_merges = 0;
456                         continue;
457                 }
458                 if (!strcmp(arg, "-s")) {
459                         silent = 1;
460                         continue;
461                 }
462                 if (!strcmp(arg, "-v")) {
463                         verbose_header = 1;
464                         header_prefix = "diff-tree ";
465                         continue;
466                 }
467                 if (!strcmp(arg, "--stdin")) {
468                         read_stdin = 1;
469                         continue;
470                 }
471                 usage(diff_tree_usage);
472         }
473
474         if (argc > 0) {
475                 int i;
476
477                 paths = argv;
478                 nr_paths = argc;
479                 pathlens = xmalloc(nr_paths * sizeof(int));
480                 for (i=0; i<nr_paths; i++)
481                         pathlens[i] = strlen(paths[i]);
482         }
483
484         switch (nr_sha1) {
485         case 0:
486                 if (!read_stdin)
487                         usage(diff_tree_usage);
488                 break;
489         case 1:
490                 diff_tree_commit(sha1[0], NULL);
491                 break;
492         case 2:
493                 diff_tree_sha1(sha1[0], sha1[1], "");
494                 break;
495         }
496
497         if (!read_stdin)
498                 return 0;
499
500         while (fgets(line, sizeof(line), stdin))
501                 diff_tree_stdin(line);
502
503         return 0;
504 }