[PATCH] Allow symlinks in the leading path in checkout-cache --prefix=
[git/dscho.git] / diff-tree.c
blob266528a143af6dfb3ae2e05de6c030872304ff9f
1 #include <ctype.h>
2 #include "cache.h"
3 #include "diff.h"
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 read_stdin = 0;
10 static int diff_output_format = DIFF_FORMAT_HUMAN;
11 static int detect_rename = 0;
12 static int reverse_diff = 0;
13 static int diff_score_opt = 0;
14 static const char *pickaxe = NULL;
15 static const char *header = NULL;
16 static const char *header_prefix = "";
18 // What paths are we interested in?
19 static int nr_paths = 0;
20 static const char **paths = NULL;
21 static int *pathlens = NULL;
23 static int diff_tree_sha1(const unsigned char *old, const unsigned char *new, const char *base);
25 static void update_tree_entry(void **bufp, unsigned long *sizep)
27 void *buf = *bufp;
28 unsigned long size = *sizep;
29 int len = strlen(buf) + 1 + 20;
31 if (size < len)
32 die("corrupt tree file");
33 *bufp = buf + len;
34 *sizep = size - len;
37 static const unsigned char *extract(void *tree, unsigned long size, const char **pathp, unsigned int *modep)
39 int len = strlen(tree)+1;
40 const unsigned char *sha1 = tree + len;
41 const char *path = strchr(tree, ' ');
43 if (!path || size < len + 20 || sscanf(tree, "%o", modep) != 1)
44 die("corrupt tree file");
45 *pathp = path+1;
46 return sha1;
49 static char *malloc_base(const char *base, const char *path, int pathlen)
51 int baselen = strlen(base);
52 char *newbase = xmalloc(baselen + pathlen + 2);
53 memcpy(newbase, base, baselen);
54 memcpy(newbase + baselen, path, pathlen);
55 memcpy(newbase + baselen + pathlen, "/", 2);
56 return newbase;
59 static void show_file(const char *prefix, void *tree, unsigned long size, const char *base);
60 static void show_tree(const char *prefix, void *tree, unsigned long size, const char *base);
62 /* A file entry went away or appeared */
63 static void show_file(const char *prefix, void *tree, unsigned long size, const char *base)
65 unsigned mode;
66 const char *path;
67 const unsigned char *sha1 = extract(tree, size, &path, &mode);
69 if (recursive && S_ISDIR(mode)) {
70 char type[20];
71 unsigned long size;
72 char *newbase = malloc_base(base, path, strlen(path));
73 void *tree;
75 tree = read_sha1_file(sha1, type, &size);
76 if (!tree || strcmp(type, "tree"))
77 die("corrupt tree sha %s", sha1_to_hex(sha1));
79 show_tree(prefix, tree, size, newbase);
81 free(tree);
82 free(newbase);
83 return;
86 diff_addremove(prefix[0], mode, sha1, base, path);
89 static int compare_tree_entry(void *tree1, unsigned long size1, void *tree2, unsigned long size2, const char *base)
91 unsigned mode1, mode2;
92 const char *path1, *path2;
93 const unsigned char *sha1, *sha2;
94 int cmp, pathlen1, pathlen2;
96 sha1 = extract(tree1, size1, &path1, &mode1);
97 sha2 = extract(tree2, size2, &path2, &mode2);
99 pathlen1 = strlen(path1);
100 pathlen2 = strlen(path2);
101 cmp = base_name_compare(path1, pathlen1, mode1, path2, pathlen2, mode2);
102 if (cmp < 0) {
103 show_file("-", tree1, size1, base);
104 return -1;
106 if (cmp > 0) {
107 show_file("+", tree2, size2, base);
108 return 1;
110 if (!memcmp(sha1, sha2, 20) && mode1 == mode2)
111 return 0;
114 * If the filemode has changed to/from a directory from/to a regular
115 * file, we need to consider it a remove and an add.
117 if (S_ISDIR(mode1) != S_ISDIR(mode2)) {
118 show_file("-", tree1, size1, base);
119 show_file("+", tree2, size2, base);
120 return 0;
123 if (recursive && S_ISDIR(mode1)) {
124 int retval;
125 char *newbase = malloc_base(base, path1, pathlen1);
126 retval = diff_tree_sha1(sha1, sha2, newbase);
127 free(newbase);
128 return retval;
131 diff_change(mode1, mode2, sha1, sha2, base, path1);
132 return 0;
135 static int interesting(void *tree, unsigned long size, const char *base)
137 const char *path;
138 unsigned mode;
139 int i;
140 int baselen, pathlen;
142 if (!nr_paths)
143 return 1;
145 (void)extract(tree, size, &path, &mode);
147 pathlen = strlen(path);
148 baselen = strlen(base);
150 for (i=0; i < nr_paths; i++) {
151 const char *match = paths[i];
152 int matchlen = pathlens[i];
154 if (baselen >= matchlen) {
155 /* If it doesn't match, move along... */
156 if (strncmp(base, match, matchlen))
157 continue;
159 /* The base is a subdirectory of a path which was specified. */
160 return 1;
163 /* Does the base match? */
164 if (strncmp(base, match, baselen))
165 continue;
167 match += baselen;
168 matchlen -= baselen;
170 if (pathlen > matchlen)
171 continue;
173 if (matchlen > pathlen) {
174 if (match[pathlen] != '/')
175 continue;
176 if (!S_ISDIR(mode))
177 continue;
180 if (strncmp(path, match, pathlen))
181 continue;
183 return 1;
185 return 0; /* No matches */
188 /* A whole sub-tree went away or appeared */
189 static void show_tree(const char *prefix, void *tree, unsigned long size, const char *base)
191 while (size) {
192 if (interesting(tree, size, base))
193 show_file(prefix, tree, size, base);
194 update_tree_entry(&tree, &size);
198 static int diff_tree(void *tree1, unsigned long size1, void *tree2, unsigned long size2, const char *base)
200 while (size1 | size2) {
201 if (nr_paths && size1 && !interesting(tree1, size1, base)) {
202 update_tree_entry(&tree1, &size1);
203 continue;
205 if (nr_paths && size2 && !interesting(tree2, size2, base)) {
206 update_tree_entry(&tree2, &size2);
207 continue;
209 if (!size1) {
210 show_file("+", tree2, size2, base);
211 update_tree_entry(&tree2, &size2);
212 continue;
214 if (!size2) {
215 show_file("-", tree1, size1, base);
216 update_tree_entry(&tree1, &size1);
217 continue;
219 switch (compare_tree_entry(tree1, size1, tree2, size2, base)) {
220 case -1:
221 update_tree_entry(&tree1, &size1);
222 continue;
223 case 0:
224 update_tree_entry(&tree1, &size1);
225 /* Fallthrough */
226 case 1:
227 update_tree_entry(&tree2, &size2);
228 continue;
230 die("git-diff-tree: internal error");
232 return 0;
235 static int diff_tree_sha1(const unsigned char *old, const unsigned char *new, const char *base)
237 void *tree1, *tree2;
238 unsigned long size1, size2;
239 int retval;
241 tree1 = read_object_with_reference(old, "tree", &size1, NULL);
242 if (!tree1)
243 die("unable to read source tree (%s)", sha1_to_hex(old));
244 tree2 = read_object_with_reference(new, "tree", &size2, NULL);
245 if (!tree2)
246 die("unable to read destination tree (%s)", sha1_to_hex(new));
247 retval = diff_tree(tree1, size1, tree2, size2, base);
248 free(tree1);
249 free(tree2);
250 return retval;
253 static void call_diff_setup(void)
255 diff_setup(reverse_diff);
258 static int call_diff_flush(void)
260 if (detect_rename)
261 diffcore_rename(detect_rename, diff_score_opt);
262 if (pickaxe)
263 diffcore_pickaxe(pickaxe);
264 if (diff_queue_is_empty()) {
265 diff_flush(DIFF_FORMAT_NO_OUTPUT, 0);
266 return 0;
268 if (nr_paths)
269 diffcore_pathspec(paths);
270 if (header) {
271 if (diff_output_format == DIFF_FORMAT_MACHINE) {
272 const char *ep, *cp;
273 for (cp = header; *cp; cp = ep) {
274 ep = strchr(cp, '\n');
275 if (ep == 0) ep = cp + strlen(cp);
276 printf("%.*s%c", ep-cp, cp, 0);
277 if (*ep) ep++;
280 else {
281 printf("%s", header);
283 header = NULL;
285 diff_flush(diff_output_format, 1);
286 return 1;
289 static int diff_tree_sha1_top(const unsigned char *old,
290 const unsigned char *new, const char *base)
292 int ret;
294 call_diff_setup();
295 ret = diff_tree_sha1(old, new, base);
296 call_diff_flush();
297 return ret;
300 static int diff_root_tree(const unsigned char *new, const char *base)
302 int retval;
303 void *tree;
304 unsigned long size;
306 call_diff_setup();
307 tree = read_object_with_reference(new, "tree", &size, NULL);
308 if (!tree)
309 die("unable to read root tree (%s)", sha1_to_hex(new));
310 retval = diff_tree("", 0, tree, size, base);
311 free(tree);
312 call_diff_flush();
313 return retval;
316 static int get_one_line(const char *msg, unsigned long len)
318 int ret = 0;
320 while (len--) {
321 ret++;
322 if (*msg++ == '\n')
323 break;
325 return ret;
328 static int add_author_info(char *buf, const char *line, int len)
330 char *date;
331 unsigned int namelen;
332 unsigned long time;
333 int tz;
335 line += strlen("author ");
336 date = strchr(line, '>');
337 if (!date)
338 return 0;
339 namelen = ++date - line;
340 time = strtoul(date, &date, 10);
341 tz = strtol(date, NULL, 10);
343 return sprintf(buf, "Author: %.*s\nDate: %s\n",
344 namelen, line,
345 show_date(time, tz));
348 static char *generate_header(const char *commit, const char *parent, const char *msg, unsigned long len)
350 static char this_header[16384];
351 int offset;
353 offset = sprintf(this_header, "%s%s (from %s)\n", header_prefix, commit, parent);
354 if (verbose_header) {
355 int hdr = 1;
357 for (;;) {
358 const char *line = msg;
359 int linelen = get_one_line(msg, len);
361 if (!linelen)
362 break;
365 * We want some slop for indentation and a possible
366 * final "...". Thus the "+ 20".
368 if (offset + linelen + 20 > sizeof(this_header)) {
369 memcpy(this_header + offset, " ...\n", 8);
370 offset += 8;
371 break;
374 msg += linelen;
375 len -= linelen;
376 if (linelen == 1)
377 hdr = 0;
378 if (hdr) {
379 if (!memcmp(line, "author ", 7))
380 offset += add_author_info(this_header + offset, line, linelen);
381 continue;
383 memset(this_header + offset, ' ', 4);
384 memcpy(this_header + offset + 4, line, linelen);
385 offset += linelen + 4;
387 /* Make sure there is an EOLN */
388 if (this_header[offset-1] != '\n')
389 this_header[offset++] = '\n';
390 /* Add _another_ EOLN if we are doing diff output */
391 this_header[offset++] = '\n';
392 this_header[offset] = 0;
395 return this_header;
398 static int diff_tree_commit(const unsigned char *commit, const char *name)
400 unsigned long size, offset;
401 char *buf = read_object_with_reference(commit, "commit", &size, NULL);
403 if (!buf)
404 return -1;
406 if (!name) {
407 static char commit_name[60];
408 strcpy(commit_name, sha1_to_hex(commit));
409 name = commit_name;
412 /* Root commit? */
413 if (show_root_diff && memcmp(buf + 46, "parent ", 7)) {
414 header = generate_header(name, "root", buf, size);
415 diff_root_tree(commit, "");
418 /* More than one parent? */
419 if (ignore_merges) {
420 if (!memcmp(buf + 46 + 48, "parent ", 7))
421 return 0;
424 offset = 46;
425 while (offset + 48 < size && !memcmp(buf + offset, "parent ", 7)) {
426 unsigned char parent[20];
427 if (get_sha1_hex(buf + offset + 7, parent))
428 return -1;
429 header = generate_header(name, sha1_to_hex(parent), buf, size);
430 diff_tree_sha1_top(parent, commit, "");
431 if (!header && verbose_header) {
432 header_prefix = "\ndiff-tree ";
434 * Don't print multiple merge entries if we
435 * don't print the diffs.
438 offset += 48;
440 return 0;
443 static int diff_tree_stdin(char *line)
445 int len = strlen(line);
446 unsigned char commit[20], parent[20];
447 static char this_header[1000];
449 if (!len || line[len-1] != '\n')
450 return -1;
451 line[len-1] = 0;
452 if (get_sha1_hex(line, commit))
453 return -1;
454 if (isspace(line[40]) && !get_sha1_hex(line+41, parent)) {
455 line[40] = 0;
456 line[81] = 0;
457 sprintf(this_header, "%s (from %s)\n", line, line+41);
458 header = this_header;
459 return diff_tree_sha1_top(parent, commit, "");
461 line[40] = 0;
462 return diff_tree_commit(commit, line);
465 static char *diff_tree_usage =
466 "git-diff-tree [-p] [-r] [-z] [--stdin] [-M] [-C] [-R] [-S<string>] [-m] [-s] [-v] <tree-ish> <tree-ish>";
468 int main(int argc, const char **argv)
470 int nr_sha1;
471 char line[1000];
472 unsigned char sha1[2][20];
474 nr_sha1 = 0;
475 for (;;) {
476 const char *arg;
478 argv++;
479 argc--;
480 arg = *argv;
481 if (!arg)
482 break;
484 if (*arg != '-') {
485 if (nr_sha1 < 2 && !get_sha1(arg, sha1[nr_sha1])) {
486 nr_sha1++;
487 continue;
489 break;
492 if (!strcmp(arg, "--")) {
493 argv++;
494 argc--;
495 break;
497 if (!strcmp(arg, "-r")) {
498 recursive = 1;
499 continue;
501 if (!strcmp(arg, "-R")) {
502 reverse_diff = 1;
503 continue;
505 if (!strcmp(arg, "-p")) {
506 diff_output_format = DIFF_FORMAT_PATCH;
507 recursive = 1;
508 continue;
510 if (!strncmp(arg, "-S", 2)) {
511 pickaxe = arg + 2;
512 continue;
514 if (!strncmp(arg, "-M", 2)) {
515 detect_rename = DIFF_DETECT_RENAME;
516 diff_score_opt = diff_scoreopt_parse(arg);
517 continue;
519 if (!strncmp(arg, "-C", 2)) {
520 detect_rename = DIFF_DETECT_COPY;
521 diff_score_opt = diff_scoreopt_parse(arg);
522 continue;
524 if (!strcmp(arg, "-z")) {
525 diff_output_format = DIFF_FORMAT_MACHINE;
526 continue;
528 if (!strcmp(arg, "-m")) {
529 ignore_merges = 0;
530 continue;
532 if (!strcmp(arg, "-s")) {
533 diff_output_format = DIFF_FORMAT_NO_OUTPUT;
534 continue;
536 if (!strcmp(arg, "-v")) {
537 verbose_header = 1;
538 header_prefix = "diff-tree ";
539 continue;
541 if (!strcmp(arg, "--stdin")) {
542 read_stdin = 1;
543 continue;
545 if (!strcmp(arg, "--root")) {
546 show_root_diff = 1;
547 continue;
549 usage(diff_tree_usage);
552 if (argc > 0) {
553 int i;
555 paths = argv;
556 nr_paths = argc;
557 pathlens = xmalloc(nr_paths * sizeof(int));
558 for (i=0; i<nr_paths; i++)
559 pathlens[i] = strlen(paths[i]);
562 switch (nr_sha1) {
563 case 0:
564 if (!read_stdin)
565 usage(diff_tree_usage);
566 break;
567 case 1:
568 diff_tree_commit(sha1[0], NULL);
569 break;
570 case 2:
571 diff_tree_sha1_top(sha1[0], sha1[1], "");
572 break;
575 if (!read_stdin)
576 return 0;
578 while (fgets(line, sizeof(line), stdin))
579 diff_tree_stdin(line);
581 return 0;