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 int diff_break_opt = -1;
18 static const char *orderfile = NULL;
19 static const char *header = NULL;
20 static const char *header_prefix = "";
22 // What paths are we interested in?
23 static int nr_paths = 0;
24 static const char **paths = NULL;
25 static int *pathlens = NULL;
27 static int diff_tree_sha1(const unsigned char *old, const unsigned char *new, const char *base);
29 static void update_tree_entry(void **bufp, unsigned long *sizep)
32 unsigned long size = *sizep;
33 int len = strlen(buf) + 1 + 20;
36 die("corrupt tree file");
41 static const unsigned char *extract(void *tree, unsigned long size, const char **pathp, unsigned int *modep)
43 int len = strlen(tree)+1;
44 const unsigned char *sha1 = tree + len;
45 const char *path = strchr(tree, ' ');
47 if (!path || size < len + 20 || sscanf(tree, "%o", modep) != 1)
48 die("corrupt tree file");
53 static char *malloc_base(const char *base, const char *path, int pathlen)
55 int baselen = strlen(base);
56 char *newbase = xmalloc(baselen + pathlen + 2);
57 memcpy(newbase, base, baselen);
58 memcpy(newbase + baselen, path, pathlen);
59 memcpy(newbase + baselen + pathlen, "/", 2);
63 static void show_file(const char *prefix, void *tree, unsigned long size, const char *base);
64 static void show_tree(const char *prefix, void *tree, unsigned long size, const char *base);
66 /* A file entry went away or appeared */
67 static void show_file(const char *prefix, void *tree, unsigned long size, const char *base)
71 const unsigned char *sha1 = extract(tree, size, &path, &mode);
73 if (recursive && S_ISDIR(mode)) {
76 char *newbase = malloc_base(base, path, strlen(path));
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);
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);
107 show_file("-", tree1, size1, base);
111 show_file("+", tree2, size2, base);
114 if (!memcmp(sha1, sha2, 20) && mode1 == mode2)
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);
127 if (recursive && S_ISDIR(mode1)) {
129 char *newbase = malloc_base(base, path1, pathlen1);
130 if (show_tree_entry_in_recursive)
131 diff_change(mode1, mode2, sha1, sha2, base, path1);
132 retval = diff_tree_sha1(sha1, sha2, newbase);
137 diff_change(mode1, mode2, sha1, sha2, base, path1);
141 static int interesting(void *tree, unsigned long size, const char *base)
146 int baselen, pathlen;
151 (void)extract(tree, size, &path, &mode);
153 pathlen = strlen(path);
154 baselen = strlen(base);
156 for (i=0; i < nr_paths; i++) {
157 const char *match = paths[i];
158 int matchlen = pathlens[i];
160 if (baselen >= matchlen) {
161 /* If it doesn't match, move along... */
162 if (strncmp(base, match, matchlen))
165 /* The base is a subdirectory of a path which was specified. */
169 /* Does the base match? */
170 if (strncmp(base, match, baselen))
176 if (pathlen > matchlen)
179 if (matchlen > pathlen) {
180 if (match[pathlen] != '/')
186 if (strncmp(path, match, pathlen))
191 return 0; /* No matches */
194 /* A whole sub-tree went away or appeared */
195 static void show_tree(const char *prefix, void *tree, unsigned long size, const char *base)
198 if (interesting(tree, size, base))
199 show_file(prefix, tree, size, base);
200 update_tree_entry(&tree, &size);
204 static int diff_tree(void *tree1, unsigned long size1, void *tree2, unsigned long size2, const char *base)
206 while (size1 | size2) {
207 if (nr_paths && size1 && !interesting(tree1, size1, base)) {
208 update_tree_entry(&tree1, &size1);
211 if (nr_paths && size2 && !interesting(tree2, size2, base)) {
212 update_tree_entry(&tree2, &size2);
216 show_file("+", tree2, size2, base);
217 update_tree_entry(&tree2, &size2);
221 show_file("-", tree1, size1, base);
222 update_tree_entry(&tree1, &size1);
225 switch (compare_tree_entry(tree1, size1, tree2, size2, base)) {
227 update_tree_entry(&tree1, &size1);
230 update_tree_entry(&tree1, &size1);
233 update_tree_entry(&tree2, &size2);
236 die("git-diff-tree: internal error");
241 static int diff_tree_sha1(const unsigned char *old, const unsigned char *new, const char *base)
244 unsigned long size1, size2;
247 tree1 = read_object_with_reference(old, "tree", &size1, NULL);
249 die("unable to read source tree (%s)", sha1_to_hex(old));
250 tree2 = read_object_with_reference(new, "tree", &size2, NULL);
252 die("unable to read destination tree (%s)", sha1_to_hex(new));
253 retval = diff_tree(tree1, size1, tree2, size2, base);
259 static void call_diff_setup(void)
261 diff_setup(diff_setup_opt);
264 static int call_diff_flush(void)
267 detect_rename, diff_score_opt,
268 pickaxe, pickaxe_opts,
271 if (diff_queue_is_empty()) {
272 diff_flush(DIFF_FORMAT_NO_OUTPUT, 0);
276 const char *fmt = "%s";
277 if (diff_output_format == DIFF_FORMAT_MACHINE)
280 printf(fmt, header, 0);
283 diff_flush(diff_output_format, 1);
287 static int diff_tree_sha1_top(const unsigned char *old,
288 const unsigned char *new, const char *base)
293 ret = diff_tree_sha1(old, new, base);
298 static int diff_root_tree(const unsigned char *new, const char *base)
305 tree = read_object_with_reference(new, "tree", &size, NULL);
307 die("unable to read root tree (%s)", sha1_to_hex(new));
308 retval = diff_tree("", 0, tree, size, base);
314 static int get_one_line(const char *msg, unsigned long len)
326 static int add_author_info(char *buf, const char *line, int len)
329 unsigned int namelen;
333 line += strlen("author ");
334 date = strchr(line, '>');
337 namelen = ++date - line;
338 time = strtoul(date, &date, 10);
339 tz = strtol(date, NULL, 10);
341 return sprintf(buf, "Author: %.*s\nDate: %s\n",
343 show_date(time, tz));
346 static char *generate_header(const char *commit, const char *parent, const char *msg, unsigned long len)
348 static char this_header[16384];
351 offset = sprintf(this_header, "%s%s (from %s)\n", header_prefix, commit, parent);
352 if (verbose_header) {
356 const char *line = msg;
357 int linelen = get_one_line(msg, len);
363 * We want some slop for indentation and a possible
364 * final "...". Thus the "+ 20".
366 if (offset + linelen + 20 > sizeof(this_header)) {
367 memcpy(this_header + offset, " ...\n", 8);
377 if (!memcmp(line, "author ", 7))
378 offset += add_author_info(this_header + offset, line, linelen);
381 memset(this_header + offset, ' ', 4);
382 memcpy(this_header + offset + 4, line, linelen);
383 offset += linelen + 4;
385 /* Make sure there is an EOLN */
386 if (this_header[offset-1] != '\n')
387 this_header[offset++] = '\n';
388 /* Add _another_ EOLN if we are doing diff output */
389 this_header[offset++] = '\n';
390 this_header[offset] = 0;
396 static int diff_tree_commit(const unsigned char *commit, const char *name)
398 unsigned long size, offset;
399 char *buf = read_object_with_reference(commit, "commit", &size, NULL);
405 static char commit_name[60];
406 strcpy(commit_name, sha1_to_hex(commit));
411 if (show_root_diff && memcmp(buf + 46, "parent ", 7)) {
412 header = generate_header(name, "root", buf, size);
413 diff_root_tree(commit, "");
416 /* More than one parent? */
418 if (!memcmp(buf + 46 + 48, "parent ", 7))
423 while (offset + 48 < size && !memcmp(buf + offset, "parent ", 7)) {
424 unsigned char parent[20];
425 if (get_sha1_hex(buf + offset + 7, parent))
427 header = generate_header(name, sha1_to_hex(parent), buf, size);
428 diff_tree_sha1_top(parent, commit, "");
429 if (!header && verbose_header) {
430 header_prefix = "\ndiff-tree ";
432 * Don't print multiple merge entries if we
433 * don't print the diffs.
441 static int diff_tree_stdin(char *line)
443 int len = strlen(line);
444 unsigned char commit[20], parent[20];
445 static char this_header[1000];
447 if (!len || line[len-1] != '\n')
450 if (get_sha1_hex(line, commit))
452 if (isspace(line[40]) && !get_sha1_hex(line+41, parent)) {
455 sprintf(this_header, "%s (from %s)\n", line, line+41);
456 header = this_header;
457 return diff_tree_sha1_top(parent, commit, "");
460 return diff_tree_commit(commit, line);
463 static char *diff_tree_usage =
464 "git-diff-tree [-p] [-r] [-z] [--stdin] [-M] [-C] [-R] [-S<string>] [-m] [-s] [-v] [-t] <tree-ish> <tree-ish>";
466 int main(int argc, const char **argv)
470 unsigned char sha1[2][20];
483 if (nr_sha1 < 2 && !get_sha1(arg, sha1[nr_sha1])) {
490 if (!strcmp(arg, "--")) {
495 if (!strcmp(arg, "-r")) {
499 if (!strcmp(arg, "-t")) {
500 recursive = show_tree_entry_in_recursive = 1;
503 if (!strcmp(arg, "-R")) {
504 diff_setup_opt |= DIFF_SETUP_REVERSE;
507 if (!strcmp(arg, "-p")) {
508 diff_output_format = DIFF_FORMAT_PATCH;
512 if (!strncmp(arg, "-S", 2)) {
516 if (!strncmp(arg, "-O", 2)) {
520 if (!strcmp(arg, "--pickaxe-all")) {
521 pickaxe_opts = DIFF_PICKAXE_ALL;
524 if (!strncmp(arg, "-M", 2)) {
525 detect_rename = DIFF_DETECT_RENAME;
526 diff_score_opt = diff_scoreopt_parse(arg);
529 if (!strncmp(arg, "-C", 2)) {
530 detect_rename = DIFF_DETECT_COPY;
531 diff_score_opt = diff_scoreopt_parse(arg);
534 if (!strncmp(arg, "-B", 2)) {
535 diff_break_opt = diff_scoreopt_parse(arg);
538 if (!strcmp(arg, "-z")) {
539 diff_output_format = DIFF_FORMAT_MACHINE;
542 if (!strcmp(arg, "-m")) {
546 if (!strcmp(arg, "-s")) {
547 diff_output_format = DIFF_FORMAT_NO_OUTPUT;
550 if (!strcmp(arg, "-v")) {
552 header_prefix = "diff-tree ";
555 if (!strcmp(arg, "--stdin")) {
559 if (!strcmp(arg, "--root")) {
563 usage(diff_tree_usage);
571 pathlens = xmalloc(nr_paths * sizeof(int));
572 for (i=0; i<nr_paths; i++)
573 pathlens[i] = strlen(paths[i]);
579 usage(diff_tree_usage);
582 diff_tree_commit(sha1[0], NULL);
585 diff_tree_sha1_top(sha1[0], sha1[1], "");
593 diff_setup_opt |= (DIFF_SETUP_USE_SIZE_CACHE |
594 DIFF_SETUP_USE_CACHE);
595 while (fgets(line, sizeof(line), stdin))
596 diff_tree_stdin(line);