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