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_RAW;
13 static int diff_line_termination = '\n';
14 static int detect_rename = 0;
15 static int find_copies_harder = 0;
16 static int diff_setup_opt = 0;
17 static int diff_score_opt = 0;
18 static const char *pickaxe = NULL;
19 static int pickaxe_opts = 0;
20 static int diff_break_opt = -1;
21 static const char *orderfile = NULL;
22 static const char *diff_filter = NULL;
23 static const char *header = NULL;
24 static const char *header_prefix = "";
25 static enum cmit_fmt commit_format = CMIT_FMT_RAW;
27 // What paths are we interested in?
28 static int nr_paths = 0;
29 static const char **paths = NULL;
30 static int *pathlens = NULL;
32 static int diff_tree_sha1(const unsigned char *old, const unsigned char *new, const char *base);
34 static void update_tree_entry(void **bufp, unsigned long *sizep)
37 unsigned long size = *sizep;
38 int len = strlen(buf) + 1 + 20;
41 die("corrupt tree file");
46 static const unsigned char *extract(void *tree, unsigned long size, const char **pathp, unsigned int *modep)
48 int len = strlen(tree)+1;
49 const unsigned char *sha1 = tree + len;
50 const char *path = strchr(tree, ' ');
53 if (!path || size < len + 20 || sscanf(tree, "%o", &mode) != 1)
54 die("corrupt tree file");
56 *modep = DIFF_FILE_CANON_MODE(mode);
60 static char *malloc_base(const char *base, const char *path, int pathlen)
62 int baselen = strlen(base);
63 char *newbase = xmalloc(baselen + pathlen + 2);
64 memcpy(newbase, base, baselen);
65 memcpy(newbase + baselen, path, pathlen);
66 memcpy(newbase + baselen + pathlen, "/", 2);
70 static void show_file(const char *prefix, void *tree, unsigned long size, const char *base);
71 static void show_tree(const char *prefix, void *tree, unsigned long size, const char *base);
73 /* A file entry went away or appeared */
74 static void show_file(const char *prefix, void *tree, unsigned long size, const char *base)
78 const unsigned char *sha1 = extract(tree, size, &path, &mode);
80 if (recursive && S_ISDIR(mode)) {
83 char *newbase = malloc_base(base, path, strlen(path));
86 tree = read_sha1_file(sha1, type, &size);
87 if (!tree || strcmp(type, "tree"))
88 die("corrupt tree sha %s", sha1_to_hex(sha1));
90 show_tree(prefix, tree, size, newbase);
97 diff_addremove(prefix[0], mode, sha1, base, path);
100 static int compare_tree_entry(void *tree1, unsigned long size1, void *tree2, unsigned long size2, const char *base)
102 unsigned mode1, mode2;
103 const char *path1, *path2;
104 const unsigned char *sha1, *sha2;
105 int cmp, pathlen1, pathlen2;
107 sha1 = extract(tree1, size1, &path1, &mode1);
108 sha2 = extract(tree2, size2, &path2, &mode2);
110 pathlen1 = strlen(path1);
111 pathlen2 = strlen(path2);
112 cmp = base_name_compare(path1, pathlen1, mode1, path2, pathlen2, mode2);
114 show_file("-", tree1, size1, base);
118 show_file("+", tree2, size2, base);
121 if (!find_copies_harder && !memcmp(sha1, sha2, 20) && mode1 == mode2)
125 * If the filemode has changed to/from a directory from/to a regular
126 * file, we need to consider it a remove and an add.
128 if (S_ISDIR(mode1) != S_ISDIR(mode2)) {
129 show_file("-", tree1, size1, base);
130 show_file("+", tree2, size2, base);
134 if (recursive && S_ISDIR(mode1)) {
136 char *newbase = malloc_base(base, path1, pathlen1);
137 if (show_tree_entry_in_recursive)
138 diff_change(mode1, mode2, sha1, sha2, base, path1);
139 retval = diff_tree_sha1(sha1, sha2, newbase);
144 diff_change(mode1, mode2, sha1, sha2, base, path1);
148 static int interesting(void *tree, unsigned long size, const char *base)
153 int baselen, pathlen;
158 (void)extract(tree, size, &path, &mode);
160 pathlen = strlen(path);
161 baselen = strlen(base);
163 for (i=0; i < nr_paths; i++) {
164 const char *match = paths[i];
165 int matchlen = pathlens[i];
167 if (baselen >= matchlen) {
168 /* If it doesn't match, move along... */
169 if (strncmp(base, match, matchlen))
172 /* The base is a subdirectory of a path which was specified. */
176 /* Does the base match? */
177 if (strncmp(base, match, baselen))
183 if (pathlen > matchlen)
186 if (matchlen > pathlen) {
187 if (match[pathlen] != '/')
193 if (strncmp(path, match, pathlen))
198 return 0; /* No matches */
201 /* A whole sub-tree went away or appeared */
202 static void show_tree(const char *prefix, void *tree, unsigned long size, const char *base)
205 if (interesting(tree, size, base))
206 show_file(prefix, tree, size, base);
207 update_tree_entry(&tree, &size);
211 static int diff_tree(void *tree1, unsigned long size1, void *tree2, unsigned long size2, const char *base)
213 while (size1 | size2) {
214 if (nr_paths && size1 && !interesting(tree1, size1, base)) {
215 update_tree_entry(&tree1, &size1);
218 if (nr_paths && size2 && !interesting(tree2, size2, base)) {
219 update_tree_entry(&tree2, &size2);
223 show_file("+", tree2, size2, base);
224 update_tree_entry(&tree2, &size2);
228 show_file("-", tree1, size1, base);
229 update_tree_entry(&tree1, &size1);
232 switch (compare_tree_entry(tree1, size1, tree2, size2, base)) {
234 update_tree_entry(&tree1, &size1);
237 update_tree_entry(&tree1, &size1);
240 update_tree_entry(&tree2, &size2);
243 die("git-diff-tree: internal error");
248 static int diff_tree_sha1(const unsigned char *old, const unsigned char *new, const char *base)
251 unsigned long size1, size2;
254 tree1 = read_object_with_reference(old, "tree", &size1, NULL);
256 die("unable to read source tree (%s)", sha1_to_hex(old));
257 tree2 = read_object_with_reference(new, "tree", &size2, NULL);
259 die("unable to read destination tree (%s)", sha1_to_hex(new));
260 retval = diff_tree(tree1, size1, tree2, size2, base);
266 static void call_diff_setup(void)
268 diff_setup(diff_setup_opt);
271 static int call_diff_flush(void)
274 detect_rename, diff_score_opt,
275 pickaxe, pickaxe_opts,
279 if (diff_queue_is_empty()) {
280 diff_flush(DIFF_FORMAT_NO_OUTPUT, diff_line_termination);
284 printf("%s%c", header, diff_line_termination);
287 diff_flush(diff_output_format, diff_line_termination);
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 const char *generate_header(const char *commit, const char *parent, const char *msg, unsigned long len)
320 static char this_header[16384];
326 offset = sprintf(this_header, "%s%s (from %s)\n", header_prefix, commit, parent);
327 offset += pretty_print_commit(commit_format, msg, len, this_header + offset, sizeof(this_header) - offset);
331 static int diff_tree_commit(const unsigned char *commit, const char *name)
333 unsigned long size, offset;
334 char *buf = read_object_with_reference(commit, "commit", &size, NULL);
340 static char commit_name[60];
341 strcpy(commit_name, sha1_to_hex(commit));
346 if (show_root_diff && memcmp(buf + 46, "parent ", 7)) {
347 header = generate_header(name, "root", buf, size);
348 diff_root_tree(commit, "");
351 /* More than one parent? */
353 if (!memcmp(buf + 46 + 48, "parent ", 7))
358 while (offset + 48 < size && !memcmp(buf + offset, "parent ", 7)) {
359 unsigned char parent[20];
360 if (get_sha1_hex(buf + offset + 7, parent))
362 header = generate_header(name, sha1_to_hex(parent), buf, size);
363 diff_tree_sha1_top(parent, commit, "");
364 if (!header && verbose_header) {
365 header_prefix = "\ndiff-tree ";
367 * Don't print multiple merge entries if we
368 * don't print the diffs.
376 static int diff_tree_stdin(char *line)
378 int len = strlen(line);
379 unsigned char commit[20], parent[20];
380 static char this_header[1000];
382 if (!len || line[len-1] != '\n')
385 if (get_sha1_hex(line, commit))
387 if (isspace(line[40]) && !get_sha1_hex(line+41, parent)) {
390 sprintf(this_header, "%s (from %s)\n", line, line+41);
391 header = this_header;
392 return diff_tree_sha1_top(parent, commit, "");
395 return diff_tree_commit(commit, line);
398 static const char diff_tree_usage[] =
399 "git-diff-tree [--stdin] [-m] [-s] [-v] [--pretty] [-t] "
400 "[<common diff options>] <tree-ish> <tree-ish>"
401 COMMON_DIFF_OPTIONS_HELP;
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") || !strcmp(arg, "-u")) {
445 diff_output_format = DIFF_FORMAT_PATCH;
449 if (!strncmp(arg, "-S", 2)) {
453 if (!strncmp(arg, "-O", 2)) {
457 if (!strncmp(arg, "--diff-filter=", 14)) {
458 diff_filter = arg + 14;
461 if (!strcmp(arg, "--pickaxe-all")) {
462 pickaxe_opts = DIFF_PICKAXE_ALL;
465 if (!strncmp(arg, "-M", 2)) {
466 detect_rename = DIFF_DETECT_RENAME;
467 if ((diff_score_opt = diff_scoreopt_parse(arg)) == -1)
468 usage(diff_tree_usage);
471 if (!strncmp(arg, "-C", 2)) {
472 detect_rename = DIFF_DETECT_COPY;
473 if ((diff_score_opt = diff_scoreopt_parse(arg)) == -1)
474 usage(diff_tree_usage);
477 if (!strncmp(arg, "-B", 2)) {
478 if ((diff_break_opt = diff_scoreopt_parse(arg)) == -1)
479 usage(diff_tree_usage);
482 if (!strcmp(arg, "--find-copies-harder")) {
483 find_copies_harder = 1;
486 if (!strcmp(arg, "--name-only")) {
487 diff_output_format = DIFF_FORMAT_NAME;
490 if (!strcmp(arg, "-z")) {
491 diff_line_termination = 0;
494 if (!strcmp(arg, "-m")) {
498 if (!strcmp(arg, "-s")) {
499 diff_output_format = DIFF_FORMAT_NO_OUTPUT;
502 if (!strcmp(arg, "-v")) {
504 header_prefix = "diff-tree ";
507 if (!strncmp(arg, "--pretty", 8)) {
509 header_prefix = "diff-tree ";
510 commit_format = get_commit_format(arg+8);
513 if (!strcmp(arg, "--stdin")) {
517 if (!strcmp(arg, "--root")) {
521 usage(diff_tree_usage);
523 if (find_copies_harder && detect_rename != DIFF_DETECT_COPY)
524 usage(diff_tree_usage);
531 pathlens = xmalloc(nr_paths * sizeof(int));
532 for (i=0; i<nr_paths; i++)
533 pathlens[i] = strlen(paths[i]);
539 usage(diff_tree_usage);
542 diff_tree_commit(sha1[0], NULL);
545 diff_tree_sha1_top(sha1[0], sha1[1], "");
553 diff_setup_opt |= (DIFF_SETUP_USE_SIZE_CACHE |
554 DIFF_SETUP_USE_CACHE);
555 while (fgets(line, sizeof(line), stdin))
556 diff_tree_stdin(line);