[PATCH] Rewrite ls-tree to behave more like "/bin/ls -a"
[git/dscho.git] / diff-tree.c
blob8bdb1dba59f9b36a9660273eb17577c1fc76cdbe
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 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 diff_setup_opt = 0;
14 static int diff_score_opt = 0;
15 static const char *pickaxe = NULL;
16 static int pickaxe_opts = 0;
17 static const char *header = NULL;
18 static const char *header_prefix = "";
20 // What paths are we interested in?
21 static int nr_paths = 0;
22 static const char **paths = NULL;
23 static int *pathlens = NULL;
25 static int diff_tree_sha1(const unsigned char *old, const unsigned char *new, const char *base);
27 static void update_tree_entry(void **bufp, unsigned long *sizep)
29 void *buf = *bufp;
30 unsigned long size = *sizep;
31 int len = strlen(buf) + 1 + 20;
33 if (size < len)
34 die("corrupt tree file");
35 *bufp = buf + len;
36 *sizep = size - len;
39 static const unsigned char *extract(void *tree, unsigned long size, const char **pathp, unsigned int *modep)
41 int len = strlen(tree)+1;
42 const unsigned char *sha1 = tree + len;
43 const char *path = strchr(tree, ' ');
45 if (!path || size < len + 20 || sscanf(tree, "%o", modep) != 1)
46 die("corrupt tree file");
47 *pathp = path+1;
48 return sha1;
51 static char *malloc_base(const char *base, const char *path, int pathlen)
53 int baselen = strlen(base);
54 char *newbase = xmalloc(baselen + pathlen + 2);
55 memcpy(newbase, base, baselen);
56 memcpy(newbase + baselen, path, pathlen);
57 memcpy(newbase + baselen + pathlen, "/", 2);
58 return newbase;
61 static void show_file(const char *prefix, void *tree, unsigned long size, const char *base);
62 static void show_tree(const char *prefix, void *tree, unsigned long size, const char *base);
64 /* A file entry went away or appeared */
65 static void show_file(const char *prefix, void *tree, unsigned long size, const char *base)
67 unsigned mode;
68 const char *path;
69 const unsigned char *sha1 = extract(tree, size, &path, &mode);
71 if (recursive && S_ISDIR(mode)) {
72 char type[20];
73 unsigned long size;
74 char *newbase = malloc_base(base, path, strlen(path));
75 void *tree;
77 tree = read_sha1_file(sha1, type, &size);
78 if (!tree || strcmp(type, "tree"))
79 die("corrupt tree sha %s", sha1_to_hex(sha1));
81 show_tree(prefix, tree, size, newbase);
83 free(tree);
84 free(newbase);
85 return;
88 diff_addremove(prefix[0], mode, sha1, base, path);
91 static int compare_tree_entry(void *tree1, unsigned long size1, void *tree2, unsigned long size2, const char *base)
93 unsigned mode1, mode2;
94 const char *path1, *path2;
95 const unsigned char *sha1, *sha2;
96 int cmp, pathlen1, pathlen2;
98 sha1 = extract(tree1, size1, &path1, &mode1);
99 sha2 = extract(tree2, size2, &path2, &mode2);
101 pathlen1 = strlen(path1);
102 pathlen2 = strlen(path2);
103 cmp = base_name_compare(path1, pathlen1, mode1, path2, pathlen2, mode2);
104 if (cmp < 0) {
105 show_file("-", tree1, size1, base);
106 return -1;
108 if (cmp > 0) {
109 show_file("+", tree2, size2, base);
110 return 1;
112 if (!memcmp(sha1, sha2, 20) && mode1 == mode2)
113 return 0;
116 * If the filemode has changed to/from a directory from/to a regular
117 * file, we need to consider it a remove and an add.
119 if (S_ISDIR(mode1) != S_ISDIR(mode2)) {
120 show_file("-", tree1, size1, base);
121 show_file("+", tree2, size2, base);
122 return 0;
125 if (recursive && S_ISDIR(mode1)) {
126 int retval;
127 char *newbase = malloc_base(base, path1, pathlen1);
128 if (show_tree_entry_in_recursive)
129 diff_change(mode1, mode2, sha1, sha2, base, path1);
130 retval = diff_tree_sha1(sha1, sha2, newbase);
131 free(newbase);
132 return retval;
135 diff_change(mode1, mode2, sha1, sha2, base, path1);
136 return 0;
139 static int interesting(void *tree, unsigned long size, const char *base)
141 const char *path;
142 unsigned mode;
143 int i;
144 int baselen, pathlen;
146 if (!nr_paths)
147 return 1;
149 (void)extract(tree, size, &path, &mode);
151 pathlen = strlen(path);
152 baselen = strlen(base);
154 for (i=0; i < nr_paths; i++) {
155 const char *match = paths[i];
156 int matchlen = pathlens[i];
158 if (baselen >= matchlen) {
159 /* If it doesn't match, move along... */
160 if (strncmp(base, match, matchlen))
161 continue;
163 /* The base is a subdirectory of a path which was specified. */
164 return 1;
167 /* Does the base match? */
168 if (strncmp(base, match, baselen))
169 continue;
171 match += baselen;
172 matchlen -= baselen;
174 if (pathlen > matchlen)
175 continue;
177 if (matchlen > pathlen) {
178 if (match[pathlen] != '/')
179 continue;
180 if (!S_ISDIR(mode))
181 continue;
184 if (strncmp(path, match, pathlen))
185 continue;
187 return 1;
189 return 0; /* No matches */
192 /* A whole sub-tree went away or appeared */
193 static void show_tree(const char *prefix, void *tree, unsigned long size, const char *base)
195 while (size) {
196 if (interesting(tree, size, base))
197 show_file(prefix, tree, size, base);
198 update_tree_entry(&tree, &size);
202 static int diff_tree(void *tree1, unsigned long size1, void *tree2, unsigned long size2, const char *base)
204 while (size1 | size2) {
205 if (nr_paths && size1 && !interesting(tree1, size1, base)) {
206 update_tree_entry(&tree1, &size1);
207 continue;
209 if (nr_paths && size2 && !interesting(tree2, size2, base)) {
210 update_tree_entry(&tree2, &size2);
211 continue;
213 if (!size1) {
214 show_file("+", tree2, size2, base);
215 update_tree_entry(&tree2, &size2);
216 continue;
218 if (!size2) {
219 show_file("-", tree1, size1, base);
220 update_tree_entry(&tree1, &size1);
221 continue;
223 switch (compare_tree_entry(tree1, size1, tree2, size2, base)) {
224 case -1:
225 update_tree_entry(&tree1, &size1);
226 continue;
227 case 0:
228 update_tree_entry(&tree1, &size1);
229 /* Fallthrough */
230 case 1:
231 update_tree_entry(&tree2, &size2);
232 continue;
234 die("git-diff-tree: internal error");
236 return 0;
239 static int diff_tree_sha1(const unsigned char *old, const unsigned char *new, const char *base)
241 void *tree1, *tree2;
242 unsigned long size1, size2;
243 int retval;
245 tree1 = read_object_with_reference(old, "tree", &size1, NULL);
246 if (!tree1)
247 die("unable to read source tree (%s)", sha1_to_hex(old));
248 tree2 = read_object_with_reference(new, "tree", &size2, NULL);
249 if (!tree2)
250 die("unable to read destination tree (%s)", sha1_to_hex(new));
251 retval = diff_tree(tree1, size1, tree2, size2, base);
252 free(tree1);
253 free(tree2);
254 return retval;
257 static void call_diff_setup(void)
259 diff_setup(diff_setup_opt);
262 static int call_diff_flush(void)
264 if (detect_rename)
265 diffcore_rename(detect_rename, diff_score_opt);
266 if (pickaxe)
267 diffcore_pickaxe(pickaxe, pickaxe_opts);
268 if (diff_queue_is_empty()) {
269 diff_flush(DIFF_FORMAT_NO_OUTPUT, 0);
270 return 0;
272 if (header) {
273 const char *fmt = "%s";
274 if (diff_output_format == DIFF_FORMAT_MACHINE)
275 fmt = "%s%c";
277 printf(fmt, header, 0);
278 header = NULL;
280 diff_flush(diff_output_format, 1);
281 return 1;
284 static int diff_tree_sha1_top(const unsigned char *old,
285 const unsigned char *new, const char *base)
287 int ret;
289 call_diff_setup();
290 ret = diff_tree_sha1(old, new, base);
291 call_diff_flush();
292 return ret;
295 static int diff_root_tree(const unsigned char *new, const char *base)
297 int retval;
298 void *tree;
299 unsigned long size;
301 call_diff_setup();
302 tree = read_object_with_reference(new, "tree", &size, NULL);
303 if (!tree)
304 die("unable to read root tree (%s)", sha1_to_hex(new));
305 retval = diff_tree("", 0, tree, size, base);
306 free(tree);
307 call_diff_flush();
308 return retval;
311 static int get_one_line(const char *msg, unsigned long len)
313 int ret = 0;
315 while (len--) {
316 ret++;
317 if (*msg++ == '\n')
318 break;
320 return ret;
323 static int add_author_info(char *buf, const char *line, int len)
325 char *date;
326 unsigned int namelen;
327 unsigned long time;
328 int tz;
330 line += strlen("author ");
331 date = strchr(line, '>');
332 if (!date)
333 return 0;
334 namelen = ++date - line;
335 time = strtoul(date, &date, 10);
336 tz = strtol(date, NULL, 10);
338 return sprintf(buf, "Author: %.*s\nDate: %s\n",
339 namelen, line,
340 show_date(time, tz));
343 static char *generate_header(const char *commit, const char *parent, const char *msg, unsigned long len)
345 static char this_header[16384];
346 int offset;
348 offset = sprintf(this_header, "%s%s (from %s)\n", header_prefix, commit, parent);
349 if (verbose_header) {
350 int hdr = 1;
352 for (;;) {
353 const char *line = msg;
354 int linelen = get_one_line(msg, len);
356 if (!linelen)
357 break;
360 * We want some slop for indentation and a possible
361 * final "...". Thus the "+ 20".
363 if (offset + linelen + 20 > sizeof(this_header)) {
364 memcpy(this_header + offset, " ...\n", 8);
365 offset += 8;
366 break;
369 msg += linelen;
370 len -= linelen;
371 if (linelen == 1)
372 hdr = 0;
373 if (hdr) {
374 if (!memcmp(line, "author ", 7))
375 offset += add_author_info(this_header + offset, line, linelen);
376 continue;
378 memset(this_header + offset, ' ', 4);
379 memcpy(this_header + offset + 4, line, linelen);
380 offset += linelen + 4;
382 /* Make sure there is an EOLN */
383 if (this_header[offset-1] != '\n')
384 this_header[offset++] = '\n';
385 /* Add _another_ EOLN if we are doing diff output */
386 this_header[offset++] = '\n';
387 this_header[offset] = 0;
390 return this_header;
393 static int diff_tree_commit(const unsigned char *commit, const char *name)
395 unsigned long size, offset;
396 char *buf = read_object_with_reference(commit, "commit", &size, NULL);
398 if (!buf)
399 return -1;
401 if (!name) {
402 static char commit_name[60];
403 strcpy(commit_name, sha1_to_hex(commit));
404 name = commit_name;
407 /* Root commit? */
408 if (show_root_diff && memcmp(buf + 46, "parent ", 7)) {
409 header = generate_header(name, "root", buf, size);
410 diff_root_tree(commit, "");
413 /* More than one parent? */
414 if (ignore_merges) {
415 if (!memcmp(buf + 46 + 48, "parent ", 7))
416 return 0;
419 offset = 46;
420 while (offset + 48 < size && !memcmp(buf + offset, "parent ", 7)) {
421 unsigned char parent[20];
422 if (get_sha1_hex(buf + offset + 7, parent))
423 return -1;
424 header = generate_header(name, sha1_to_hex(parent), buf, size);
425 diff_tree_sha1_top(parent, commit, "");
426 if (!header && verbose_header) {
427 header_prefix = "\ndiff-tree ";
429 * Don't print multiple merge entries if we
430 * don't print the diffs.
433 offset += 48;
435 return 0;
438 static int diff_tree_stdin(char *line)
440 int len = strlen(line);
441 unsigned char commit[20], parent[20];
442 static char this_header[1000];
444 if (!len || line[len-1] != '\n')
445 return -1;
446 line[len-1] = 0;
447 if (get_sha1_hex(line, commit))
448 return -1;
449 if (isspace(line[40]) && !get_sha1_hex(line+41, parent)) {
450 line[40] = 0;
451 line[81] = 0;
452 sprintf(this_header, "%s (from %s)\n", line, line+41);
453 header = this_header;
454 return diff_tree_sha1_top(parent, commit, "");
456 line[40] = 0;
457 return diff_tree_commit(commit, line);
460 static char *diff_tree_usage =
461 "git-diff-tree [-p] [-r] [-z] [--stdin] [-M] [-C] [-R] [-S<string>] [-m] [-s] [-v] [-t] <tree-ish> <tree-ish>";
463 int main(int argc, const char **argv)
465 int nr_sha1;
466 char line[1000];
467 unsigned char sha1[2][20];
469 nr_sha1 = 0;
470 for (;;) {
471 const char *arg;
473 argv++;
474 argc--;
475 arg = *argv;
476 if (!arg)
477 break;
479 if (*arg != '-') {
480 if (nr_sha1 < 2 && !get_sha1(arg, sha1[nr_sha1])) {
481 nr_sha1++;
482 continue;
484 break;
487 if (!strcmp(arg, "--")) {
488 argv++;
489 argc--;
490 break;
492 if (!strcmp(arg, "-r")) {
493 recursive = 1;
494 continue;
496 if (!strcmp(arg, "-t")) {
497 recursive = show_tree_entry_in_recursive = 1;
498 continue;
500 if (!strcmp(arg, "-R")) {
501 diff_setup_opt |= DIFF_SETUP_REVERSE;
502 continue;
504 if (!strcmp(arg, "-p")) {
505 diff_output_format = DIFF_FORMAT_PATCH;
506 recursive = 1;
507 continue;
509 if (!strncmp(arg, "-S", 2)) {
510 pickaxe = arg + 2;
511 continue;
513 if (!strcmp(arg, "--pickaxe-all")) {
514 pickaxe_opts = DIFF_PICKAXE_ALL;
515 continue;
517 if (!strncmp(arg, "-M", 2)) {
518 detect_rename = DIFF_DETECT_RENAME;
519 diff_score_opt = diff_scoreopt_parse(arg);
520 continue;
522 if (!strncmp(arg, "-C", 2)) {
523 detect_rename = DIFF_DETECT_COPY;
524 diff_score_opt = diff_scoreopt_parse(arg);
525 continue;
527 if (!strcmp(arg, "-z")) {
528 diff_output_format = DIFF_FORMAT_MACHINE;
529 continue;
531 if (!strcmp(arg, "-m")) {
532 ignore_merges = 0;
533 continue;
535 if (!strcmp(arg, "-s")) {
536 diff_output_format = DIFF_FORMAT_NO_OUTPUT;
537 continue;
539 if (!strcmp(arg, "-v")) {
540 verbose_header = 1;
541 header_prefix = "diff-tree ";
542 continue;
544 if (!strcmp(arg, "--stdin")) {
545 read_stdin = 1;
546 continue;
548 if (!strcmp(arg, "--root")) {
549 show_root_diff = 1;
550 continue;
552 usage(diff_tree_usage);
555 if (argc > 0) {
556 int i;
558 paths = argv;
559 nr_paths = argc;
560 pathlens = xmalloc(nr_paths * sizeof(int));
561 for (i=0; i<nr_paths; i++)
562 pathlens[i] = strlen(paths[i]);
565 switch (nr_sha1) {
566 case 0:
567 if (!read_stdin)
568 usage(diff_tree_usage);
569 break;
570 case 1:
571 diff_tree_commit(sha1[0], NULL);
572 break;
573 case 2:
574 diff_tree_sha1_top(sha1[0], sha1[1], "");
575 break;
578 if (!read_stdin)
579 return 0;
581 if (detect_rename)
582 diff_setup_opt |= (DIFF_SETUP_USE_SIZE_CACHE |
583 DIFF_SETUP_USE_CACHE);
584 while (fgets(line, sizeof(line), stdin))
585 diff_tree_stdin(line);
587 return 0;