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 show_tree_entry_in_recursive = 0;
11 static int read_stdin = 0;
12 static int diff_output_format = DIFF_FORMAT_HUMAN;
13 static int detect_rename = 0;
14 static int diff_setup_opt = 0;
15 static int diff_score_opt = 0;
16 static const char *pickaxe = NULL;
17 static int pickaxe_opts = 0;
18 static int diff_break_opt = -1;
19 static const char *orderfile = NULL;
20 static const char *header = NULL;
21 static const char *header_prefix = "";
22 static enum cmit_fmt commit_format = CMIT_FMT_RAW;
24 // What paths are we interested in?
25 static int nr_paths = 0;
26 static const char **paths = NULL;
27 static int *pathlens = NULL;
29 static int diff_tree_sha1(const unsigned char *old, const unsigned char *new, const char *base);
31 static void update_tree_entry(void **bufp, unsigned long *sizep)
34 unsigned long size = *sizep;
35 int len = strlen(buf) + 1 + 20;
38 die("corrupt tree file");
43 static const unsigned char *extract(void *tree, unsigned long size, const char **pathp, unsigned int *modep)
45 int len = strlen(tree)+1;
46 const unsigned char *sha1 = tree + len;
47 const char *path = strchr(tree, ' ');
50 if (!path || size < len + 20 || sscanf(tree, "%o", &mode) != 1)
51 die("corrupt tree file");
53 *modep = DIFF_FILE_CANON_MODE(mode);
57 static char *malloc_base(const char *base, const char *path, int pathlen)
59 int baselen = strlen(base);
60 char *newbase = xmalloc(baselen + pathlen + 2);
61 memcpy(newbase, base, baselen);
62 memcpy(newbase + baselen, path, pathlen);
63 memcpy(newbase + baselen + pathlen, "/", 2);
67 static void show_file(const char *prefix, void *tree, unsigned long size, const char *base);
68 static void show_tree(const char *prefix, void *tree, unsigned long size, const char *base);
70 /* A file entry went away or appeared */
71 static void show_file(const char *prefix, void *tree, unsigned long size, const char *base)
75 const unsigned char *sha1 = extract(tree, size, &path, &mode);
77 if (recursive && S_ISDIR(mode)) {
80 char *newbase = malloc_base(base, path, strlen(path));
83 tree = read_sha1_file(sha1, type, &size);
84 if (!tree || strcmp(type, "tree"))
85 die("corrupt tree sha %s", sha1_to_hex(sha1));
87 show_tree(prefix, tree, size, newbase);
94 diff_addremove(prefix[0], mode, sha1, base, path);
97 static int compare_tree_entry(void *tree1, unsigned long size1, void *tree2, unsigned long size2, const char *base)
99 unsigned mode1, mode2;
100 const char *path1, *path2;
101 const unsigned char *sha1, *sha2;
102 int cmp, pathlen1, pathlen2;
104 sha1 = extract(tree1, size1, &path1, &mode1);
105 sha2 = extract(tree2, size2, &path2, &mode2);
107 pathlen1 = strlen(path1);
108 pathlen2 = strlen(path2);
109 cmp = base_name_compare(path1, pathlen1, mode1, path2, pathlen2, mode2);
111 show_file("-", tree1, size1, base);
115 show_file("+", tree2, size2, base);
118 if (!memcmp(sha1, sha2, 20) && mode1 == mode2)
122 * If the filemode has changed to/from a directory from/to a regular
123 * file, we need to consider it a remove and an add.
125 if (S_ISDIR(mode1) != S_ISDIR(mode2)) {
126 show_file("-", tree1, size1, base);
127 show_file("+", tree2, size2, base);
131 if (recursive && S_ISDIR(mode1)) {
133 char *newbase = malloc_base(base, path1, pathlen1);
134 if (show_tree_entry_in_recursive)
135 diff_change(mode1, mode2, sha1, sha2, base, path1);
136 retval = diff_tree_sha1(sha1, sha2, newbase);
141 diff_change(mode1, mode2, sha1, sha2, base, path1);
145 static int interesting(void *tree, unsigned long size, const char *base)
150 int baselen, pathlen;
155 (void)extract(tree, size, &path, &mode);
157 pathlen = strlen(path);
158 baselen = strlen(base);
160 for (i=0; i < nr_paths; i++) {
161 const char *match = paths[i];
162 int matchlen = pathlens[i];
164 if (baselen >= matchlen) {
165 /* If it doesn't match, move along... */
166 if (strncmp(base, match, matchlen))
169 /* The base is a subdirectory of a path which was specified. */
173 /* Does the base match? */
174 if (strncmp(base, match, baselen))
180 if (pathlen > matchlen)
183 if (matchlen > pathlen) {
184 if (match[pathlen] != '/')
190 if (strncmp(path, match, pathlen))
195 return 0; /* No matches */
198 /* A whole sub-tree went away or appeared */
199 static void show_tree(const char *prefix, void *tree, unsigned long size, const char *base)
202 if (interesting(tree, size, base))
203 show_file(prefix, tree, size, base);
204 update_tree_entry(&tree, &size);
208 static int diff_tree(void *tree1, unsigned long size1, void *tree2, unsigned long size2, const char *base)
210 while (size1 | size2) {
211 if (nr_paths && size1 && !interesting(tree1, size1, base)) {
212 update_tree_entry(&tree1, &size1);
215 if (nr_paths && size2 && !interesting(tree2, size2, base)) {
216 update_tree_entry(&tree2, &size2);
220 show_file("+", tree2, size2, base);
221 update_tree_entry(&tree2, &size2);
225 show_file("-", tree1, size1, base);
226 update_tree_entry(&tree1, &size1);
229 switch (compare_tree_entry(tree1, size1, tree2, size2, base)) {
231 update_tree_entry(&tree1, &size1);
234 update_tree_entry(&tree1, &size1);
237 update_tree_entry(&tree2, &size2);
240 die("git-diff-tree: internal error");
245 static int diff_tree_sha1(const unsigned char *old, const unsigned char *new, const char *base)
248 unsigned long size1, size2;
251 tree1 = read_object_with_reference(old, "tree", &size1, NULL);
253 die("unable to read source tree (%s)", sha1_to_hex(old));
254 tree2 = read_object_with_reference(new, "tree", &size2, NULL);
256 die("unable to read destination tree (%s)", sha1_to_hex(new));
257 retval = diff_tree(tree1, size1, tree2, size2, base);
263 static void call_diff_setup(void)
265 diff_setup(diff_setup_opt);
268 static int call_diff_flush(void)
271 detect_rename, diff_score_opt,
272 pickaxe, pickaxe_opts,
275 if (diff_queue_is_empty()) {
276 diff_flush(DIFF_FORMAT_NO_OUTPUT, 0);
280 const char *fmt = "%s";
281 if (diff_output_format == DIFF_FORMAT_MACHINE)
284 printf(fmt, header, 0);
287 diff_flush(diff_output_format, 1);
291 static int diff_tree_sha1_top(const unsigned char *old,
292 const unsigned char *new, const char *base)
297 ret = diff_tree_sha1(old, new, base);
302 static int diff_root_tree(const unsigned char *new, const char *base)
309 tree = read_object_with_reference(new, "tree", &size, NULL);
311 die("unable to read root tree (%s)", sha1_to_hex(new));
312 retval = diff_tree("", 0, tree, size, base);
318 static char *generate_header(const char *commit, const char *parent, const char *msg, unsigned long len)
320 static char this_header[16384];
323 offset = sprintf(this_header, "%s%s (from %s)\n", header_prefix, commit, parent);
324 if (verbose_header) {
325 offset += pretty_print_commit(commit_format, msg, len, this_header + offset, sizeof(this_header) - offset);
326 this_header[offset++] = '\n';
327 this_header[offset++] = 0;
333 static int diff_tree_commit(const unsigned char *commit, const char *name)
335 unsigned long size, offset;
336 char *buf = read_object_with_reference(commit, "commit", &size, NULL);
342 static char commit_name[60];
343 strcpy(commit_name, sha1_to_hex(commit));
348 if (show_root_diff && memcmp(buf + 46, "parent ", 7)) {
349 header = generate_header(name, "root", buf, size);
350 diff_root_tree(commit, "");
353 /* More than one parent? */
355 if (!memcmp(buf + 46 + 48, "parent ", 7))
360 while (offset + 48 < size && !memcmp(buf + offset, "parent ", 7)) {
361 unsigned char parent[20];
362 if (get_sha1_hex(buf + offset + 7, parent))
364 header = generate_header(name, sha1_to_hex(parent), buf, size);
365 diff_tree_sha1_top(parent, commit, "");
366 if (!header && verbose_header) {
367 header_prefix = "\ndiff-tree ";
369 * Don't print multiple merge entries if we
370 * don't print the diffs.
378 static int diff_tree_stdin(char *line)
380 int len = strlen(line);
381 unsigned char commit[20], parent[20];
382 static char this_header[1000];
384 if (!len || line[len-1] != '\n')
387 if (get_sha1_hex(line, commit))
389 if (isspace(line[40]) && !get_sha1_hex(line+41, parent)) {
392 sprintf(this_header, "%s (from %s)\n", line, line+41);
393 header = this_header;
394 return diff_tree_sha1_top(parent, commit, "");
397 return diff_tree_commit(commit, line);
400 static char *diff_tree_usage =
401 "git-diff-tree [-p] [-r] [-z] [--stdin] [-M] [-C] [-R] [-S<string>] [-O<orderfile>] [-m] [-s] [-v] [-t] <tree-ish> <tree-ish>";
403 int main(int argc, const char **argv)
407 unsigned char sha1[2][20];
420 if (nr_sha1 < 2 && !get_sha1(arg, sha1[nr_sha1])) {
427 if (!strcmp(arg, "--")) {
432 if (!strcmp(arg, "-r")) {
436 if (!strcmp(arg, "-t")) {
437 recursive = show_tree_entry_in_recursive = 1;
440 if (!strcmp(arg, "-R")) {
441 diff_setup_opt |= DIFF_SETUP_REVERSE;
444 if (!strcmp(arg, "-p")) {
445 diff_output_format = DIFF_FORMAT_PATCH;
449 if (!strncmp(arg, "-S", 2)) {
453 if (!strncmp(arg, "-O", 2)) {
457 if (!strcmp(arg, "--pickaxe-all")) {
458 pickaxe_opts = DIFF_PICKAXE_ALL;
461 if (!strncmp(arg, "-M", 2)) {
462 detect_rename = DIFF_DETECT_RENAME;
463 if ((diff_score_opt = diff_scoreopt_parse(arg)) == -1)
464 usage(diff_tree_usage);
467 if (!strncmp(arg, "-C", 2)) {
468 detect_rename = DIFF_DETECT_COPY;
469 if ((diff_score_opt = diff_scoreopt_parse(arg)) == -1)
470 usage(diff_tree_usage);
473 if (!strncmp(arg, "-B", 2)) {
474 if ((diff_break_opt = diff_scoreopt_parse(arg)) == -1)
475 usage(diff_tree_usage);
478 if (!strcmp(arg, "-z")) {
479 diff_output_format = DIFF_FORMAT_MACHINE;
482 if (!strcmp(arg, "-m")) {
486 if (!strcmp(arg, "-s")) {
487 diff_output_format = DIFF_FORMAT_NO_OUTPUT;
490 if (!strcmp(arg, "-v")) {
492 header_prefix = "diff-tree ";
495 if (!strcmp(arg, "--stdin")) {
499 if (!strcmp(arg, "--root")) {
503 usage(diff_tree_usage);
511 pathlens = xmalloc(nr_paths * sizeof(int));
512 for (i=0; i<nr_paths; i++)
513 pathlens[i] = strlen(paths[i]);
519 usage(diff_tree_usage);
522 diff_tree_commit(sha1[0], NULL);
525 diff_tree_sha1_top(sha1[0], sha1[1], "");
533 diff_setup_opt |= (DIFF_SETUP_USE_SIZE_CACHE |
534 DIFF_SETUP_USE_CACHE);
535 while (fgets(line, sizeof(line), stdin))
536 diff_tree_stdin(line);