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 = "";
23 // What paths are we interested in?
24 static int nr_paths = 0;
25 static const char **paths = NULL;
26 static int *pathlens = NULL;
28 static int diff_tree_sha1(const unsigned char *old, const unsigned char *new, const char *base);
30 static void update_tree_entry(void **bufp, unsigned long *sizep)
33 unsigned long size = *sizep;
34 int len = strlen(buf) + 1 + 20;
37 die("corrupt tree file");
42 static const unsigned char *extract(void *tree, unsigned long size, const char **pathp, unsigned int *modep)
44 int len = strlen(tree)+1;
45 const unsigned char *sha1 = tree + len;
46 const char *path = strchr(tree, ' ');
49 if (!path || size < len + 20 || sscanf(tree, "%o", &mode) != 1)
50 die("corrupt tree file");
52 *modep = DIFF_FILE_CANON_MODE(mode);
56 static char *malloc_base(const char *base, const char *path, int pathlen)
58 int baselen = strlen(base);
59 char *newbase = xmalloc(baselen + pathlen + 2);
60 memcpy(newbase, base, baselen);
61 memcpy(newbase + baselen, path, pathlen);
62 memcpy(newbase + baselen + pathlen, "/", 2);
66 static void show_file(const char *prefix, void *tree, unsigned long size, const char *base);
67 static void show_tree(const char *prefix, void *tree, unsigned long size, const char *base);
69 /* A file entry went away or appeared */
70 static void show_file(const char *prefix, void *tree, unsigned long size, const char *base)
74 const unsigned char *sha1 = extract(tree, size, &path, &mode);
76 if (recursive && S_ISDIR(mode)) {
79 char *newbase = malloc_base(base, path, strlen(path));
82 tree = read_sha1_file(sha1, type, &size);
83 if (!tree || strcmp(type, "tree"))
84 die("corrupt tree sha %s", sha1_to_hex(sha1));
86 show_tree(prefix, tree, size, newbase);
93 diff_addremove(prefix[0], mode, sha1, base, path);
96 static int compare_tree_entry(void *tree1, unsigned long size1, void *tree2, unsigned long size2, const char *base)
98 unsigned mode1, mode2;
99 const char *path1, *path2;
100 const unsigned char *sha1, *sha2;
101 int cmp, pathlen1, pathlen2;
103 sha1 = extract(tree1, size1, &path1, &mode1);
104 sha2 = extract(tree2, size2, &path2, &mode2);
106 pathlen1 = strlen(path1);
107 pathlen2 = strlen(path2);
108 cmp = base_name_compare(path1, pathlen1, mode1, path2, pathlen2, mode2);
110 show_file("-", tree1, size1, base);
114 show_file("+", tree2, size2, base);
117 if (!memcmp(sha1, sha2, 20) && mode1 == mode2)
121 * If the filemode has changed to/from a directory from/to a regular
122 * file, we need to consider it a remove and an add.
124 if (S_ISDIR(mode1) != S_ISDIR(mode2)) {
125 show_file("-", tree1, size1, base);
126 show_file("+", tree2, size2, base);
130 if (recursive && S_ISDIR(mode1)) {
132 char *newbase = malloc_base(base, path1, pathlen1);
133 if (show_tree_entry_in_recursive)
134 diff_change(mode1, mode2, sha1, sha2, base, path1);
135 retval = diff_tree_sha1(sha1, sha2, newbase);
140 diff_change(mode1, mode2, sha1, sha2, base, path1);
144 static int interesting(void *tree, unsigned long size, const char *base)
149 int baselen, pathlen;
154 (void)extract(tree, size, &path, &mode);
156 pathlen = strlen(path);
157 baselen = strlen(base);
159 for (i=0; i < nr_paths; i++) {
160 const char *match = paths[i];
161 int matchlen = pathlens[i];
163 if (baselen >= matchlen) {
164 /* If it doesn't match, move along... */
165 if (strncmp(base, match, matchlen))
168 /* The base is a subdirectory of a path which was specified. */
172 /* Does the base match? */
173 if (strncmp(base, match, baselen))
179 if (pathlen > matchlen)
182 if (matchlen > pathlen) {
183 if (match[pathlen] != '/')
189 if (strncmp(path, match, pathlen))
194 return 0; /* No matches */
197 /* A whole sub-tree went away or appeared */
198 static void show_tree(const char *prefix, void *tree, unsigned long size, const char *base)
201 if (interesting(tree, size, base))
202 show_file(prefix, tree, size, base);
203 update_tree_entry(&tree, &size);
207 static int diff_tree(void *tree1, unsigned long size1, void *tree2, unsigned long size2, const char *base)
209 while (size1 | size2) {
210 if (nr_paths && size1 && !interesting(tree1, size1, base)) {
211 update_tree_entry(&tree1, &size1);
214 if (nr_paths && size2 && !interesting(tree2, size2, base)) {
215 update_tree_entry(&tree2, &size2);
219 show_file("+", tree2, size2, base);
220 update_tree_entry(&tree2, &size2);
224 show_file("-", tree1, size1, base);
225 update_tree_entry(&tree1, &size1);
228 switch (compare_tree_entry(tree1, size1, tree2, size2, base)) {
230 update_tree_entry(&tree1, &size1);
233 update_tree_entry(&tree1, &size1);
236 update_tree_entry(&tree2, &size2);
239 die("git-diff-tree: internal error");
244 static int diff_tree_sha1(const unsigned char *old, const unsigned char *new, const char *base)
247 unsigned long size1, size2;
250 tree1 = read_object_with_reference(old, "tree", &size1, NULL);
252 die("unable to read source tree (%s)", sha1_to_hex(old));
253 tree2 = read_object_with_reference(new, "tree", &size2, NULL);
255 die("unable to read destination tree (%s)", sha1_to_hex(new));
256 retval = diff_tree(tree1, size1, tree2, size2, base);
262 static void call_diff_setup(void)
264 diff_setup(diff_setup_opt);
267 static int call_diff_flush(void)
270 detect_rename, diff_score_opt,
271 pickaxe, pickaxe_opts,
274 if (diff_queue_is_empty()) {
275 diff_flush(DIFF_FORMAT_NO_OUTPUT, 0);
279 const char *fmt = "%s";
280 if (diff_output_format == DIFF_FORMAT_MACHINE)
283 printf(fmt, header, 0);
286 diff_flush(diff_output_format, 1);
290 static int diff_tree_sha1_top(const unsigned char *old,
291 const unsigned char *new, const char *base)
296 ret = diff_tree_sha1(old, new, base);
301 static int diff_root_tree(const unsigned char *new, const char *base)
308 tree = read_object_with_reference(new, "tree", &size, NULL);
310 die("unable to read root tree (%s)", sha1_to_hex(new));
311 retval = diff_tree("", 0, tree, size, base);
317 static char *generate_header(const char *commit, const char *parent, const char *msg, unsigned long len)
319 static char this_header[16384];
322 offset = sprintf(this_header, "%s%s (from %s)\n", header_prefix, commit, parent);
323 if (verbose_header) {
324 offset += pretty_print_commit(msg, len, this_header + offset, sizeof(this_header) - offset);
325 this_header[offset++] = '\n';
326 this_header[offset++] = 0;
332 static int diff_tree_commit(const unsigned char *commit, const char *name)
334 unsigned long size, offset;
335 char *buf = read_object_with_reference(commit, "commit", &size, NULL);
341 static char commit_name[60];
342 strcpy(commit_name, sha1_to_hex(commit));
347 if (show_root_diff && memcmp(buf + 46, "parent ", 7)) {
348 header = generate_header(name, "root", buf, size);
349 diff_root_tree(commit, "");
352 /* More than one parent? */
354 if (!memcmp(buf + 46 + 48, "parent ", 7))
359 while (offset + 48 < size && !memcmp(buf + offset, "parent ", 7)) {
360 unsigned char parent[20];
361 if (get_sha1_hex(buf + offset + 7, parent))
363 header = generate_header(name, sha1_to_hex(parent), buf, size);
364 diff_tree_sha1_top(parent, commit, "");
365 if (!header && verbose_header) {
366 header_prefix = "\ndiff-tree ";
368 * Don't print multiple merge entries if we
369 * don't print the diffs.
377 static int diff_tree_stdin(char *line)
379 int len = strlen(line);
380 unsigned char commit[20], parent[20];
381 static char this_header[1000];
383 if (!len || line[len-1] != '\n')
386 if (get_sha1_hex(line, commit))
388 if (isspace(line[40]) && !get_sha1_hex(line+41, parent)) {
391 sprintf(this_header, "%s (from %s)\n", line, line+41);
392 header = this_header;
393 return diff_tree_sha1_top(parent, commit, "");
396 return diff_tree_commit(commit, line);
399 static char *diff_tree_usage =
400 "git-diff-tree [-p] [-r] [-z] [--stdin] [-M] [-C] [-R] [-S<string>] [-m] [-s] [-v] [-t] <tree-ish> <tree-ish>";
402 int main(int argc, const char **argv)
406 unsigned char sha1[2][20];
419 if (nr_sha1 < 2 && !get_sha1(arg, sha1[nr_sha1])) {
426 if (!strcmp(arg, "--")) {
431 if (!strcmp(arg, "-r")) {
435 if (!strcmp(arg, "-t")) {
436 recursive = show_tree_entry_in_recursive = 1;
439 if (!strcmp(arg, "-R")) {
440 diff_setup_opt |= DIFF_SETUP_REVERSE;
443 if (!strcmp(arg, "-p")) {
444 diff_output_format = DIFF_FORMAT_PATCH;
448 if (!strncmp(arg, "-S", 2)) {
452 if (!strncmp(arg, "-O", 2)) {
456 if (!strcmp(arg, "--pickaxe-all")) {
457 pickaxe_opts = DIFF_PICKAXE_ALL;
460 if (!strncmp(arg, "-M", 2)) {
461 detect_rename = DIFF_DETECT_RENAME;
462 diff_score_opt = diff_scoreopt_parse(arg);
465 if (!strncmp(arg, "-C", 2)) {
466 detect_rename = DIFF_DETECT_COPY;
467 diff_score_opt = diff_scoreopt_parse(arg);
470 if (!strncmp(arg, "-B", 2)) {
471 diff_break_opt = diff_scoreopt_parse(arg);
474 if (!strcmp(arg, "-z")) {
475 diff_output_format = DIFF_FORMAT_MACHINE;
478 if (!strcmp(arg, "-m")) {
482 if (!strcmp(arg, "-s")) {
483 diff_output_format = DIFF_FORMAT_NO_OUTPUT;
486 if (!strcmp(arg, "-v")) {
488 header_prefix = "diff-tree ";
491 if (!strcmp(arg, "--stdin")) {
495 if (!strcmp(arg, "--root")) {
499 usage(diff_tree_usage);
507 pathlens = xmalloc(nr_paths * sizeof(int));
508 for (i=0; i<nr_paths; i++)
509 pathlens[i] = strlen(paths[i]);
515 usage(diff_tree_usage);
518 diff_tree_commit(sha1[0], NULL);
521 diff_tree_sha1_top(sha1[0], sha1[1], "");
529 diff_setup_opt |= (DIFF_SETUP_USE_SIZE_CACHE |
530 DIFF_SETUP_USE_CACHE);
531 while (fgets(line, sizeof(line), stdin))
532 diff_tree_stdin(line);