[PATCH] diff-raw format update take #2.
[git/mjg.git] / diff-tree.c
blob0d21b18ff700a37509dfb9bd57f2fa636840cb5c
1 #include <ctype.h>
2 #include "cache.h"
3 #include "diff.h"
5 static int silent = 0;
6 static int show_root_diff = 0;
7 static int verbose_header = 0;
8 static int ignore_merges = 1;
9 static int 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 reverse_diff = 0;
14 static int diff_score_opt = 0;
15 static const char *pickaxe = NULL;
16 static const char *header = NULL;
17 static const char *header_prefix = "";
19 // What paths are we interested in?
20 static int nr_paths = 0;
21 static const char **paths = NULL;
22 static int *pathlens = NULL;
24 static int diff_tree_sha1(const unsigned char *old, const unsigned char *new, const char *base);
26 static void update_tree_entry(void **bufp, unsigned long *sizep)
28 void *buf = *bufp;
29 unsigned long size = *sizep;
30 int len = strlen(buf) + 1 + 20;
32 if (size < len)
33 die("corrupt tree file");
34 *bufp = buf + len;
35 *sizep = size - len;
38 static const unsigned char *extract(void *tree, unsigned long size, const char **pathp, unsigned int *modep)
40 int len = strlen(tree)+1;
41 const unsigned char *sha1 = tree + len;
42 const char *path = strchr(tree, ' ');
44 if (!path || size < len + 20 || sscanf(tree, "%o", modep) != 1)
45 die("corrupt tree file");
46 *pathp = path+1;
47 return sha1;
50 static char *malloc_base(const char *base, const char *path, int pathlen)
52 int baselen = strlen(base);
53 char *newbase = xmalloc(baselen + pathlen + 2);
54 memcpy(newbase, base, baselen);
55 memcpy(newbase + baselen, path, pathlen);
56 memcpy(newbase + baselen + pathlen, "/", 2);
57 return newbase;
60 static void show_file(const char *prefix, void *tree, unsigned long size, const char *base);
61 static void show_tree(const char *prefix, void *tree, unsigned long size, const char *base);
63 /* A file entry went away or appeared */
64 static void show_file(const char *prefix, void *tree, unsigned long size, const char *base)
66 unsigned mode;
67 const char *path;
68 const unsigned char *sha1 = extract(tree, size, &path, &mode);
70 if (silent)
71 return;
73 if (recursive && S_ISDIR(mode)) {
74 char type[20];
75 unsigned long size;
76 char *newbase = malloc_base(base, path, strlen(path));
77 void *tree;
79 tree = read_sha1_file(sha1, type, &size);
80 if (!tree || strcmp(type, "tree"))
81 die("corrupt tree sha %s", sha1_to_hex(sha1));
83 show_tree(prefix, tree, size, newbase);
85 free(tree);
86 free(newbase);
87 return;
90 diff_addremove(prefix[0], mode, sha1, base, path);
93 static int compare_tree_entry(void *tree1, unsigned long size1, void *tree2, unsigned long size2, const char *base)
95 unsigned mode1, mode2;
96 const char *path1, *path2;
97 const unsigned char *sha1, *sha2;
98 int cmp, pathlen1, pathlen2;
100 sha1 = extract(tree1, size1, &path1, &mode1);
101 sha2 = extract(tree2, size2, &path2, &mode2);
103 pathlen1 = strlen(path1);
104 pathlen2 = strlen(path2);
105 cmp = base_name_compare(path1, pathlen1, mode1, path2, pathlen2, mode2);
106 if (cmp < 0) {
107 show_file("-", tree1, size1, base);
108 return -1;
110 if (cmp > 0) {
111 show_file("+", tree2, size2, base);
112 return 1;
114 if (!memcmp(sha1, sha2, 20) && mode1 == mode2)
115 return 0;
118 * If the filemode has changed to/from a directory from/to a regular
119 * file, we need to consider it a remove and an add.
121 if (S_ISDIR(mode1) != S_ISDIR(mode2)) {
122 show_file("-", tree1, size1, base);
123 show_file("+", tree2, size2, base);
124 return 0;
127 if (recursive && S_ISDIR(mode1)) {
128 int retval;
129 char *newbase = malloc_base(base, path1, pathlen1);
130 retval = diff_tree_sha1(sha1, sha2, newbase);
131 free(newbase);
132 return retval;
135 if (silent)
136 return 0;
138 diff_change(mode1, mode2, sha1, sha2, base, path1);
139 return 0;
142 static int interesting(void *tree, unsigned long size, const char *base)
144 const char *path;
145 unsigned mode;
146 int i;
147 int baselen, pathlen;
149 if (!nr_paths)
150 return 1;
152 (void)extract(tree, size, &path, &mode);
154 pathlen = strlen(path);
155 baselen = strlen(base);
157 for (i=0; i < nr_paths; i++) {
158 const char *match = paths[i];
159 int matchlen = pathlens[i];
161 if (baselen >= matchlen) {
162 /* If it doesn't match, move along... */
163 if (strncmp(base, match, matchlen))
164 continue;
166 /* The base is a subdirectory of a path which was specified. */
167 return 1;
170 /* Does the base match? */
171 if (strncmp(base, match, baselen))
172 continue;
174 match += baselen;
175 matchlen -= baselen;
177 if (pathlen > matchlen)
178 continue;
180 if (matchlen > pathlen) {
181 if (match[pathlen] != '/')
182 continue;
183 if (!S_ISDIR(mode))
184 continue;
187 if (strncmp(path, match, pathlen))
188 continue;
190 return 1;
192 return 0; /* No matches */
195 /* A whole sub-tree went away or appeared */
196 static void show_tree(const char *prefix, void *tree, unsigned long size, const char *base)
198 while (size) {
199 if (interesting(tree, size, base))
200 show_file(prefix, tree, size, base);
201 update_tree_entry(&tree, &size);
205 static int diff_tree(void *tree1, unsigned long size1, void *tree2, unsigned long size2, const char *base)
207 while (size1 | size2) {
208 if (nr_paths && size1 && !interesting(tree1, size1, base)) {
209 update_tree_entry(&tree1, &size1);
210 continue;
212 if (nr_paths && size2 && !interesting(tree2, size2, base)) {
213 update_tree_entry(&tree2, &size2);
214 continue;
216 if (!size1) {
217 show_file("+", tree2, size2, base);
218 update_tree_entry(&tree2, &size2);
219 continue;
221 if (!size2) {
222 show_file("-", tree1, size1, base);
223 update_tree_entry(&tree1, &size1);
224 continue;
226 switch (compare_tree_entry(tree1, size1, tree2, size2, base)) {
227 case -1:
228 update_tree_entry(&tree1, &size1);
229 continue;
230 case 0:
231 update_tree_entry(&tree1, &size1);
232 /* Fallthrough */
233 case 1:
234 update_tree_entry(&tree2, &size2);
235 continue;
237 die("git-diff-tree: internal error");
239 return 0;
242 static int diff_tree_sha1(const unsigned char *old, const unsigned char *new, const char *base)
244 void *tree1, *tree2;
245 unsigned long size1, size2;
246 int retval;
248 tree1 = read_object_with_reference(old, "tree", &size1, NULL);
249 if (!tree1)
250 die("unable to read source tree (%s)", sha1_to_hex(old));
251 tree2 = read_object_with_reference(new, "tree", &size2, NULL);
252 if (!tree2)
253 die("unable to read destination tree (%s)", sha1_to_hex(new));
254 retval = diff_tree(tree1, size1, tree2, size2, base);
255 free(tree1);
256 free(tree2);
257 return retval;
260 static void call_diff_setup(void)
262 diff_setup(reverse_diff);
265 static int call_diff_flush(void)
267 if (detect_rename)
268 diffcore_rename(detect_rename, diff_score_opt);
269 diffcore_prune();
270 if (pickaxe) {
271 diffcore_pickaxe(pickaxe);
272 if (diff_queue_is_empty()) {
273 diff_flush(DIFF_FORMAT_NO_OUTPUT, 0);
274 return 0;
277 if (nr_paths)
278 diffcore_pathspec(paths);
279 if (header) {
280 if (diff_output_format == DIFF_FORMAT_MACHINE) {
281 const char *ep, *cp;
282 for (cp = header; *cp; cp = ep) {
283 ep = strchr(cp, '\n');
284 if (ep == 0) ep = cp + strlen(cp);
285 printf("%.*s%c", ep-cp, cp, 0);
286 if (*ep) ep++;
289 else {
290 printf("%s", header);
292 header = NULL;
294 diff_flush(diff_output_format, 1);
295 return 1;
298 static int diff_tree_sha1_top(const unsigned char *old,
299 const unsigned char *new, const char *base)
301 int ret;
303 call_diff_setup();
304 ret = diff_tree_sha1(old, new, base);
305 call_diff_flush();
306 return ret;
309 static int diff_root_tree(const unsigned char *new, const char *base)
311 int retval;
312 void *tree;
313 unsigned long size;
315 call_diff_setup();
316 tree = read_object_with_reference(new, "tree", &size, NULL);
317 if (!tree)
318 die("unable to read root tree (%s)", sha1_to_hex(new));
319 retval = diff_tree("", 0, tree, size, base);
320 free(tree);
321 call_diff_flush();
322 return retval;
325 static int get_one_line(const char *msg, unsigned long len)
327 int ret = 0;
329 while (len--) {
330 ret++;
331 if (*msg++ == '\n')
332 break;
334 return ret;
337 static int add_author_info(char *buf, const char *line, int len)
339 char *date;
340 unsigned int namelen;
341 unsigned long time;
342 int tz;
344 line += strlen("author ");
345 date = strchr(line, '>');
346 if (!date)
347 return 0;
348 namelen = ++date - line;
349 time = strtoul(date, &date, 10);
350 tz = strtol(date, NULL, 10);
352 return sprintf(buf, "Author: %.*s\nDate: %s\n",
353 namelen, line,
354 show_date(time, tz));
357 static char *generate_header(const char *commit, const char *parent, const char *msg, unsigned long len)
359 static char this_header[16384];
360 int offset;
362 offset = sprintf(this_header, "%s%s (from %s)\n", header_prefix, commit, parent);
363 if (verbose_header) {
364 int hdr = 1;
366 for (;;) {
367 const char *line = msg;
368 int linelen = get_one_line(msg, len);
370 if (!linelen)
371 break;
374 * We want some slop for indentation and a possible
375 * final "...". Thus the "+ 20".
377 if (offset + linelen + 20 > sizeof(this_header)) {
378 memcpy(this_header + offset, " ...\n", 8);
379 offset += 8;
380 break;
383 msg += linelen;
384 len -= linelen;
385 if (linelen == 1)
386 hdr = 0;
387 if (hdr) {
388 if (!memcmp(line, "author ", 7))
389 offset += add_author_info(this_header + offset, line, linelen);
390 continue;
392 memset(this_header + offset, ' ', 4);
393 memcpy(this_header + offset + 4, line, linelen);
394 offset += linelen + 4;
396 /* Make sure there is an EOLN */
397 if (this_header[offset-1] != '\n')
398 this_header[offset++] = '\n';
399 /* Add _another_ EOLN if we are doing diff output */
400 if (!silent)
401 this_header[offset++] = '\n';
402 this_header[offset] = 0;
405 return this_header;
408 static int diff_tree_commit(const unsigned char *commit, const char *name)
410 unsigned long size, offset;
411 char *buf = read_object_with_reference(commit, "commit", &size, NULL);
413 if (!buf)
414 return -1;
416 if (!name) {
417 static char commit_name[60];
418 strcpy(commit_name, sha1_to_hex(commit));
419 name = commit_name;
422 /* Root commit? */
423 if (show_root_diff && memcmp(buf + 46, "parent ", 7)) {
424 header = generate_header(name, "root", buf, size);
425 diff_root_tree(commit, "");
428 /* More than one parent? */
429 if (ignore_merges) {
430 if (!memcmp(buf + 46 + 48, "parent ", 7))
431 return 0;
434 offset = 46;
435 while (offset + 48 < size && !memcmp(buf + offset, "parent ", 7)) {
436 unsigned char parent[20];
437 if (get_sha1_hex(buf + offset + 7, parent))
438 return -1;
439 header = generate_header(name, sha1_to_hex(parent), buf, size);
440 diff_tree_sha1_top(parent, commit, "");
441 if (!header && verbose_header) {
442 header_prefix = "\ndiff-tree ";
444 * Don't print multiple merge entries if we
445 * don't print the diffs.
447 if (silent)
448 break;
450 offset += 48;
452 return 0;
455 static int diff_tree_stdin(char *line)
457 int len = strlen(line);
458 unsigned char commit[20], parent[20];
459 static char this_header[1000];
461 if (!len || line[len-1] != '\n')
462 return -1;
463 line[len-1] = 0;
464 if (get_sha1_hex(line, commit))
465 return -1;
466 if (isspace(line[40]) && !get_sha1_hex(line+41, parent)) {
467 line[40] = 0;
468 line[81] = 0;
469 sprintf(this_header, "%s (from %s)\n", line, line+41);
470 header = this_header;
471 return diff_tree_sha1_top(parent, commit, "");
473 line[40] = 0;
474 return diff_tree_commit(commit, line);
477 static char *diff_tree_usage =
478 "git-diff-tree [-p] [-r] [-z] [--stdin] [-M] [-C] [-R] [-S<string>] [-m] [-s] [-v] <tree-ish> <tree-ish>";
480 int main(int argc, const char **argv)
482 int nr_sha1;
483 char line[1000];
484 unsigned char sha1[2][20];
486 nr_sha1 = 0;
487 for (;;) {
488 const char *arg;
490 argv++;
491 argc--;
492 arg = *argv;
493 if (!arg)
494 break;
496 if (*arg != '-') {
497 if (nr_sha1 < 2 && !get_sha1(arg, sha1[nr_sha1])) {
498 nr_sha1++;
499 continue;
501 break;
504 if (!strcmp(arg, "--")) {
505 argv++;
506 argc--;
507 break;
509 if (!strcmp(arg, "-r")) {
510 recursive = 1;
511 continue;
513 if (!strcmp(arg, "-R")) {
514 reverse_diff = 1;
515 continue;
517 if (!strcmp(arg, "-p")) {
518 diff_output_format = DIFF_FORMAT_PATCH;
519 recursive = 1;
520 continue;
522 if (!strncmp(arg, "-S", 2)) {
523 pickaxe = arg + 2;
524 continue;
526 if (!strncmp(arg, "-M", 2)) {
527 detect_rename = DIFF_DETECT_RENAME;
528 diff_score_opt = diff_scoreopt_parse(arg);
529 continue;
531 if (!strncmp(arg, "-C", 2)) {
532 detect_rename = DIFF_DETECT_COPY;
533 diff_score_opt = diff_scoreopt_parse(arg);
534 continue;
536 if (!strcmp(arg, "-z")) {
537 diff_output_format = DIFF_FORMAT_MACHINE;
538 continue;
540 if (!strcmp(arg, "-m")) {
541 ignore_merges = 0;
542 continue;
544 if (!strcmp(arg, "-s")) {
545 silent = 1;
546 continue;
548 if (!strcmp(arg, "-v")) {
549 verbose_header = 1;
550 header_prefix = "diff-tree ";
551 continue;
553 if (!strcmp(arg, "--stdin")) {
554 read_stdin = 1;
555 continue;
557 if (!strcmp(arg, "--root")) {
558 show_root_diff = 1;
559 continue;
561 usage(diff_tree_usage);
564 if (argc > 0) {
565 int i;
567 paths = argv;
568 nr_paths = argc;
569 pathlens = xmalloc(nr_paths * sizeof(int));
570 for (i=0; i<nr_paths; i++)
571 pathlens[i] = strlen(paths[i]);
574 switch (nr_sha1) {
575 case 0:
576 if (!read_stdin)
577 usage(diff_tree_usage);
578 break;
579 case 1:
580 diff_tree_commit(sha1[0], NULL);
581 break;
582 case 2:
583 diff_tree_sha1_top(sha1[0], sha1[1], "");
584 break;
587 if (!read_stdin)
588 return 0;
590 while (fgets(line, sizeof(line), stdin))
591 diff_tree_stdin(line);
593 return 0;