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 find_copies_harder
= 0;
15 static int diff_setup_opt
= 0;
16 static int diff_score_opt
= 0;
17 static const char *pickaxe
= NULL
;
18 static int pickaxe_opts
= 0;
19 static int diff_break_opt
= -1;
20 static const char *orderfile
= NULL
;
21 static const char *header
= NULL
;
22 static const char *header_prefix
= "";
23 static enum cmit_fmt commit_format
= CMIT_FMT_RAW
;
25 // What paths are we interested in?
26 static int nr_paths
= 0;
27 static const char **paths
= NULL
;
28 static int *pathlens
= NULL
;
30 static int diff_tree_sha1(const unsigned char *old
, const unsigned char *new, const char *base
);
32 static void update_tree_entry(void **bufp
, unsigned long *sizep
)
35 unsigned long size
= *sizep
;
36 int len
= strlen(buf
) + 1 + 20;
39 die("corrupt tree file");
44 static const unsigned char *extract(void *tree
, unsigned long size
, const char **pathp
, unsigned int *modep
)
46 int len
= strlen(tree
)+1;
47 const unsigned char *sha1
= tree
+ len
;
48 const char *path
= strchr(tree
, ' ');
51 if (!path
|| size
< len
+ 20 || sscanf(tree
, "%o", &mode
) != 1)
52 die("corrupt tree file");
54 *modep
= DIFF_FILE_CANON_MODE(mode
);
58 static char *malloc_base(const char *base
, const char *path
, int pathlen
)
60 int baselen
= strlen(base
);
61 char *newbase
= xmalloc(baselen
+ pathlen
+ 2);
62 memcpy(newbase
, base
, baselen
);
63 memcpy(newbase
+ baselen
, path
, pathlen
);
64 memcpy(newbase
+ baselen
+ pathlen
, "/", 2);
68 static void show_file(const char *prefix
, void *tree
, unsigned long size
, const char *base
);
69 static void show_tree(const char *prefix
, void *tree
, unsigned long size
, const char *base
);
71 /* A file entry went away or appeared */
72 static void show_file(const char *prefix
, void *tree
, unsigned long size
, const char *base
)
76 const unsigned char *sha1
= extract(tree
, size
, &path
, &mode
);
78 if (recursive
&& S_ISDIR(mode
)) {
81 char *newbase
= malloc_base(base
, path
, strlen(path
));
84 tree
= read_sha1_file(sha1
, type
, &size
);
85 if (!tree
|| strcmp(type
, "tree"))
86 die("corrupt tree sha %s", sha1_to_hex(sha1
));
88 show_tree(prefix
, tree
, size
, newbase
);
95 diff_addremove(prefix
[0], mode
, sha1
, base
, path
);
98 static int compare_tree_entry(void *tree1
, unsigned long size1
, void *tree2
, unsigned long size2
, const char *base
)
100 unsigned mode1
, mode2
;
101 const char *path1
, *path2
;
102 const unsigned char *sha1
, *sha2
;
103 int cmp
, pathlen1
, pathlen2
;
105 sha1
= extract(tree1
, size1
, &path1
, &mode1
);
106 sha2
= extract(tree2
, size2
, &path2
, &mode2
);
108 pathlen1
= strlen(path1
);
109 pathlen2
= strlen(path2
);
110 cmp
= base_name_compare(path1
, pathlen1
, mode1
, path2
, pathlen2
, mode2
);
112 show_file("-", tree1
, size1
, base
);
116 show_file("+", tree2
, size2
, base
);
119 if (!find_copies_harder
&& !memcmp(sha1
, sha2
, 20) && mode1
== mode2
)
123 * If the filemode has changed to/from a directory from/to a regular
124 * file, we need to consider it a remove and an add.
126 if (S_ISDIR(mode1
) != S_ISDIR(mode2
)) {
127 show_file("-", tree1
, size1
, base
);
128 show_file("+", tree2
, size2
, base
);
132 if (recursive
&& S_ISDIR(mode1
)) {
134 char *newbase
= malloc_base(base
, path1
, pathlen1
);
135 if (show_tree_entry_in_recursive
)
136 diff_change(mode1
, mode2
, sha1
, sha2
, base
, path1
);
137 retval
= diff_tree_sha1(sha1
, sha2
, newbase
);
142 diff_change(mode1
, mode2
, sha1
, sha2
, base
, path1
);
146 static int interesting(void *tree
, unsigned long size
, const char *base
)
151 int baselen
, pathlen
;
156 (void)extract(tree
, size
, &path
, &mode
);
158 pathlen
= strlen(path
);
159 baselen
= strlen(base
);
161 for (i
=0; i
< nr_paths
; i
++) {
162 const char *match
= paths
[i
];
163 int matchlen
= pathlens
[i
];
165 if (baselen
>= matchlen
) {
166 /* If it doesn't match, move along... */
167 if (strncmp(base
, match
, matchlen
))
170 /* The base is a subdirectory of a path which was specified. */
174 /* Does the base match? */
175 if (strncmp(base
, match
, baselen
))
181 if (pathlen
> matchlen
)
184 if (matchlen
> pathlen
) {
185 if (match
[pathlen
] != '/')
191 if (strncmp(path
, match
, pathlen
))
196 return 0; /* No matches */
199 /* A whole sub-tree went away or appeared */
200 static void show_tree(const char *prefix
, void *tree
, unsigned long size
, const char *base
)
203 if (find_copies_harder
|| interesting(tree
, size
, base
))
204 show_file(prefix
, tree
, size
, base
);
205 update_tree_entry(&tree
, &size
);
209 static int diff_tree(void *tree1
, unsigned long size1
, void *tree2
, unsigned long size2
, const char *base
)
211 while (size1
| size2
) {
212 if (nr_paths
&& size1
&& !interesting(tree1
, size1
, base
)) {
213 update_tree_entry(&tree1
, &size1
);
216 if (nr_paths
&& size2
&& !interesting(tree2
, size2
, base
)) {
217 update_tree_entry(&tree2
, &size2
);
221 show_file("+", tree2
, size2
, base
);
222 update_tree_entry(&tree2
, &size2
);
226 show_file("-", tree1
, size1
, base
);
227 update_tree_entry(&tree1
, &size1
);
230 switch (compare_tree_entry(tree1
, size1
, tree2
, size2
, base
)) {
232 update_tree_entry(&tree1
, &size1
);
235 update_tree_entry(&tree1
, &size1
);
238 update_tree_entry(&tree2
, &size2
);
241 die("git-diff-tree: internal error");
246 static int diff_tree_sha1(const unsigned char *old
, const unsigned char *new, const char *base
)
249 unsigned long size1
, size2
;
252 tree1
= read_object_with_reference(old
, "tree", &size1
, NULL
);
254 die("unable to read source tree (%s)", sha1_to_hex(old
));
255 tree2
= read_object_with_reference(new, "tree", &size2
, NULL
);
257 die("unable to read destination tree (%s)", sha1_to_hex(new));
258 retval
= diff_tree(tree1
, size1
, tree2
, size2
, base
);
264 static void call_diff_setup(void)
266 diff_setup(diff_setup_opt
);
269 static int call_diff_flush(void)
271 diffcore_std(find_copies_harder
? paths
: 0,
272 detect_rename
, diff_score_opt
,
273 pickaxe
, pickaxe_opts
,
276 if (diff_queue_is_empty()) {
277 diff_flush(DIFF_FORMAT_NO_OUTPUT
, 0);
281 const char *fmt
= "%s";
282 if (diff_output_format
== DIFF_FORMAT_MACHINE
)
285 printf(fmt
, header
, 0);
288 diff_flush(diff_output_format
, 1);
292 static int diff_tree_sha1_top(const unsigned char *old
,
293 const unsigned char *new, const char *base
)
298 ret
= diff_tree_sha1(old
, new, base
);
303 static int diff_root_tree(const unsigned char *new, const char *base
)
310 tree
= read_object_with_reference(new, "tree", &size
, NULL
);
312 die("unable to read root tree (%s)", sha1_to_hex(new));
313 retval
= diff_tree("", 0, tree
, size
, base
);
319 static char *generate_header(const char *commit
, const char *parent
, const char *msg
, unsigned long len
)
321 static char this_header
[16384];
324 offset
= sprintf(this_header
, "%s%s (from %s)\n", header_prefix
, commit
, parent
);
325 if (verbose_header
) {
326 offset
+= pretty_print_commit(commit_format
, msg
, len
, this_header
+ offset
, sizeof(this_header
) - offset
);
327 this_header
[offset
++] = '\n';
328 this_header
[offset
++] = 0;
334 static int diff_tree_commit(const unsigned char *commit
, const char *name
)
336 unsigned long size
, offset
;
337 char *buf
= read_object_with_reference(commit
, "commit", &size
, NULL
);
343 static char commit_name
[60];
344 strcpy(commit_name
, sha1_to_hex(commit
));
349 if (show_root_diff
&& memcmp(buf
+ 46, "parent ", 7)) {
350 header
= generate_header(name
, "root", buf
, size
);
351 diff_root_tree(commit
, "");
354 /* More than one parent? */
356 if (!memcmp(buf
+ 46 + 48, "parent ", 7))
361 while (offset
+ 48 < size
&& !memcmp(buf
+ offset
, "parent ", 7)) {
362 unsigned char parent
[20];
363 if (get_sha1_hex(buf
+ offset
+ 7, parent
))
365 header
= generate_header(name
, sha1_to_hex(parent
), buf
, size
);
366 diff_tree_sha1_top(parent
, commit
, "");
367 if (!header
&& verbose_header
) {
368 header_prefix
= "\ndiff-tree ";
370 * Don't print multiple merge entries if we
371 * don't print the diffs.
379 static int diff_tree_stdin(char *line
)
381 int len
= strlen(line
);
382 unsigned char commit
[20], parent
[20];
383 static char this_header
[1000];
385 if (!len
|| line
[len
-1] != '\n')
388 if (get_sha1_hex(line
, commit
))
390 if (isspace(line
[40]) && !get_sha1_hex(line
+41, parent
)) {
393 sprintf(this_header
, "%s (from %s)\n", line
, line
+41);
394 header
= this_header
;
395 return diff_tree_sha1_top(parent
, commit
, "");
398 return diff_tree_commit(commit
, line
);
401 static char *diff_tree_usage
=
402 "git-diff-tree [-p] [-r] [-z] [--stdin] [-M] [-C] [-R] [-S<string>] [-O<orderfile>] [-m] [-s] [-v] [--pretty] [-t] <tree-ish> <tree-ish>";
404 static enum cmit_fmt
get_commit_format(const char *arg
)
407 return CMIT_FMT_DEFAULT
;
408 if (!strcmp(arg
, "=raw"))
410 if (!strcmp(arg
, "=medium"))
411 return CMIT_FMT_MEDIUM
;
412 if (!strcmp(arg
, "=short"))
413 return CMIT_FMT_SHORT
;
414 usage(diff_tree_usage
);
417 int main(int argc
, const char **argv
)
421 unsigned char sha1
[2][20];
434 if (nr_sha1
< 2 && !get_sha1(arg
, sha1
[nr_sha1
])) {
441 if (!strcmp(arg
, "--")) {
446 if (!strcmp(arg
, "-r")) {
450 if (!strcmp(arg
, "-t")) {
451 recursive
= show_tree_entry_in_recursive
= 1;
454 if (!strcmp(arg
, "-R")) {
455 diff_setup_opt
|= DIFF_SETUP_REVERSE
;
458 if (!strcmp(arg
, "-p")) {
459 diff_output_format
= DIFF_FORMAT_PATCH
;
463 if (!strncmp(arg
, "-S", 2)) {
467 if (!strncmp(arg
, "-O", 2)) {
471 if (!strcmp(arg
, "--pickaxe-all")) {
472 pickaxe_opts
= DIFF_PICKAXE_ALL
;
475 if (!strncmp(arg
, "-M", 2)) {
476 detect_rename
= DIFF_DETECT_RENAME
;
477 if ((diff_score_opt
= diff_scoreopt_parse(arg
)) == -1)
478 usage(diff_tree_usage
);
481 if (!strncmp(arg
, "-C", 2)) {
482 detect_rename
= DIFF_DETECT_COPY
;
483 if ((diff_score_opt
= diff_scoreopt_parse(arg
)) == -1)
484 usage(diff_tree_usage
);
487 if (!strncmp(arg
, "-B", 2)) {
488 if ((diff_break_opt
= diff_scoreopt_parse(arg
)) == -1)
489 usage(diff_tree_usage
);
492 if (!strcmp(arg
, "--find-copies-harder")) {
493 find_copies_harder
= 1;
496 if (!strcmp(arg
, "-z")) {
497 diff_output_format
= DIFF_FORMAT_MACHINE
;
500 if (!strcmp(arg
, "-m")) {
504 if (!strcmp(arg
, "-s")) {
505 diff_output_format
= DIFF_FORMAT_NO_OUTPUT
;
508 if (!strcmp(arg
, "-v")) {
510 header_prefix
= "diff-tree ";
513 if (!strncmp(arg
, "--pretty", 8)) {
515 header_prefix
= "diff-tree ";
516 commit_format
= get_commit_format(arg
+8);
519 if (!strcmp(arg
, "--stdin")) {
523 if (!strcmp(arg
, "--root")) {
527 usage(diff_tree_usage
);
529 if (find_copies_harder
&& detect_rename
!= DIFF_DETECT_COPY
)
530 usage(diff_tree_usage
);
537 pathlens
= xmalloc(nr_paths
* sizeof(int));
538 for (i
=0; i
<nr_paths
; i
++)
539 pathlens
[i
] = strlen(paths
[i
]);
545 usage(diff_tree_usage
);
548 diff_tree_commit(sha1
[0], NULL
);
551 diff_tree_sha1_top(sha1
[0], sha1
[1], "");
559 diff_setup_opt
|= (DIFF_SETUP_USE_SIZE_CACHE
|
560 DIFF_SETUP_USE_CACHE
);
561 while (fgets(line
, sizeof(line
), stdin
))
562 diff_tree_stdin(line
);