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)
28 unsigned long size = *sizep;
29 int len = strlen(buf) + 1 + 20;
32 die("corrupt tree file");
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");
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);
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)
67 const unsigned char *sha1 = extract(tree, size, &path, &mode);
69 if (recursive && S_ISDIR(mode)) {
72 char *newbase = malloc_base(base, path, strlen(path));
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);
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);
103 show_file("-", tree1, size1, base);
107 show_file("+", tree2, size2, base);
110 if (!memcmp(sha1, sha2, 20) && mode1 == mode2)
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);
123 if (recursive && S_ISDIR(mode1)) {
125 char *newbase = malloc_base(base, path1, pathlen1);
126 retval = diff_tree_sha1(sha1, sha2, newbase);
131 diff_change(mode1, mode2, sha1, sha2, base, path1);
135 static int interesting(void *tree, unsigned long size, const char *base)
140 int baselen, pathlen;
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))
159 /* The base is a subdirectory of a path which was specified. */
163 /* Does the base match? */
164 if (strncmp(base, match, baselen))
170 if (pathlen > matchlen)
173 if (matchlen > pathlen) {
174 if (match[pathlen] != '/')
180 if (strncmp(path, match, pathlen))
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)
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);
205 if (nr_paths && size2 && !interesting(tree2, size2, base)) {
206 update_tree_entry(&tree2, &size2);
210 show_file("+", tree2, size2, base);
211 update_tree_entry(&tree2, &size2);
215 show_file("-", tree1, size1, base);
216 update_tree_entry(&tree1, &size1);
219 switch (compare_tree_entry(tree1, size1, tree2, size2, base)) {
221 update_tree_entry(&tree1, &size1);
224 update_tree_entry(&tree1, &size1);
227 update_tree_entry(&tree2, &size2);
230 die("git-diff-tree: internal error");
235 static int diff_tree_sha1(const unsigned char *old, const unsigned char *new, const char *base)
238 unsigned long size1, size2;
241 tree1 = read_object_with_reference(old, "tree", &size1, NULL);
243 die("unable to read source tree (%s)", sha1_to_hex(old));
244 tree2 = read_object_with_reference(new, "tree", &size2, NULL);
246 die("unable to read destination tree (%s)", sha1_to_hex(new));
247 retval = diff_tree(tree1, size1, tree2, size2, base);
253 static void call_diff_setup(void)
255 diff_setup(reverse_diff);
258 static int call_diff_flush(void)
261 diffcore_rename(detect_rename, diff_score_opt);
263 diffcore_pickaxe(pickaxe);
264 if (diff_queue_is_empty()) {
265 diff_flush(DIFF_FORMAT_NO_OUTPUT, 0);
269 diffcore_pathspec(paths);
271 if (diff_output_format == DIFF_FORMAT_MACHINE) {
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);
281 printf("%s", header);
285 diff_flush(diff_output_format, 1);
289 static int diff_tree_sha1_top(const unsigned char *old,
290 const unsigned char *new, const char *base)
295 ret = diff_tree_sha1(old, new, base);
300 static int diff_root_tree(const unsigned char *new, const char *base)
307 tree = read_object_with_reference(new, "tree", &size, NULL);
309 die("unable to read root tree (%s)", sha1_to_hex(new));
310 retval = diff_tree("", 0, tree, size, base);
316 static int get_one_line(const char *msg, unsigned long len)
328 static int add_author_info(char *buf, const char *line, int len)
331 unsigned int namelen;
335 line += strlen("author ");
336 date = strchr(line, '>');
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",
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];
353 offset = sprintf(this_header, "%s%s (from %s)\n", header_prefix, commit, parent);
354 if (verbose_header) {
358 const char *line = msg;
359 int linelen = get_one_line(msg, len);
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);
379 if (!memcmp(line, "author ", 7))
380 offset += add_author_info(this_header + offset, line, linelen);
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;
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);
407 static char commit_name[60];
408 strcpy(commit_name, sha1_to_hex(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? */
420 if (!memcmp(buf + 46 + 48, "parent ", 7))
425 while (offset + 48 < size && !memcmp(buf + offset, "parent ", 7)) {
426 unsigned char parent[20];
427 if (get_sha1_hex(buf + offset + 7, parent))
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.
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')
452 if (get_sha1_hex(line, commit))
454 if (isspace(line[40]) && !get_sha1_hex(line+41, parent)) {
457 sprintf(this_header, "%s (from %s)\n", line, line+41);
458 header = this_header;
459 return diff_tree_sha1_top(parent, commit, "");
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)
472 unsigned char sha1[2][20];
485 if (nr_sha1 < 2 && !get_sha1(arg, sha1[nr_sha1])) {
492 if (!strcmp(arg, "--")) {
497 if (!strcmp(arg, "-r")) {
501 if (!strcmp(arg, "-R")) {
505 if (!strcmp(arg, "-p")) {
506 diff_output_format = DIFF_FORMAT_PATCH;
510 if (!strncmp(arg, "-S", 2)) {
514 if (!strncmp(arg, "-M", 2)) {
515 detect_rename = DIFF_DETECT_RENAME;
516 diff_score_opt = diff_scoreopt_parse(arg);
519 if (!strncmp(arg, "-C", 2)) {
520 detect_rename = DIFF_DETECT_COPY;
521 diff_score_opt = diff_scoreopt_parse(arg);
524 if (!strcmp(arg, "-z")) {
525 diff_output_format = DIFF_FORMAT_MACHINE;
528 if (!strcmp(arg, "-m")) {
532 if (!strcmp(arg, "-s")) {
533 diff_output_format = DIFF_FORMAT_NO_OUTPUT;
536 if (!strcmp(arg, "-v")) {
538 header_prefix = "diff-tree ";
541 if (!strcmp(arg, "--stdin")) {
545 if (!strcmp(arg, "--root")) {
549 usage(diff_tree_usage);
557 pathlens = xmalloc(nr_paths * sizeof(int));
558 for (i=0; i<nr_paths; i++)
559 pathlens[i] = strlen(paths[i]);
565 usage(diff_tree_usage);
568 diff_tree_commit(sha1[0], NULL);
571 diff_tree_sha1_top(sha1[0], sha1[1], "");
578 while (fgets(line, sizeof(line), stdin))
579 diff_tree_stdin(line);