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 show_tree_entry_in_recursive
= 0;
10 static int read_stdin
= 0;
12 static struct diff_options diff_options
;
14 static const char *header
= NULL
;
15 static const char *header_prefix
= "";
16 static enum cmit_fmt commit_format
= CMIT_FMT_RAW
;
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
, ' ');
44 if (!path
|| size
< len
+ 20 || sscanf(tree
, "%o", &mode
) != 1)
45 die("corrupt tree file");
47 *modep
= DIFF_FILE_CANON_MODE(mode
);
51 static char *malloc_base(const char *base
, const char *path
, int pathlen
)
53 int baselen
= strlen(base
);
54 char *newbase
= xmalloc(baselen
+ pathlen
+ 2);
55 memcpy(newbase
, base
, baselen
);
56 memcpy(newbase
+ baselen
, path
, pathlen
);
57 memcpy(newbase
+ baselen
+ pathlen
, "/", 2);
61 static void show_file(const char *prefix
, void *tree
, unsigned long size
, const char *base
);
62 static void show_tree(const char *prefix
, void *tree
, unsigned long size
, const char *base
);
64 /* A file entry went away or appeared */
65 static void show_file(const char *prefix
, void *tree
, unsigned long size
, const char *base
)
69 const unsigned char *sha1
= extract(tree
, size
, &path
, &mode
);
71 if (recursive
&& S_ISDIR(mode
)) {
74 char *newbase
= malloc_base(base
, path
, strlen(path
));
77 tree
= read_sha1_file(sha1
, type
, &size
);
78 if (!tree
|| strcmp(type
, "tree"))
79 die("corrupt tree sha %s", sha1_to_hex(sha1
));
81 show_tree(prefix
, tree
, size
, newbase
);
88 diff_addremove(&diff_options
, prefix
[0], mode
, sha1
, base
, path
);
91 static int compare_tree_entry(void *tree1
, unsigned long size1
, void *tree2
, unsigned long size2
, const char *base
)
93 unsigned mode1
, mode2
;
94 const char *path1
, *path2
;
95 const unsigned char *sha1
, *sha2
;
96 int cmp
, pathlen1
, pathlen2
;
98 sha1
= extract(tree1
, size1
, &path1
, &mode1
);
99 sha2
= extract(tree2
, size2
, &path2
, &mode2
);
101 pathlen1
= strlen(path1
);
102 pathlen2
= strlen(path2
);
103 cmp
= base_name_compare(path1
, pathlen1
, mode1
, path2
, pathlen2
, mode2
);
105 show_file("-", tree1
, size1
, base
);
109 show_file("+", tree2
, size2
, base
);
112 if (!diff_options
.find_copies_harder
&&
113 !memcmp(sha1
, sha2
, 20) && mode1
== mode2
)
117 * If the filemode has changed to/from a directory from/to a regular
118 * file, we need to consider it a remove and an add.
120 if (S_ISDIR(mode1
) != S_ISDIR(mode2
)) {
121 show_file("-", tree1
, size1
, base
);
122 show_file("+", tree2
, size2
, base
);
126 if (recursive
&& S_ISDIR(mode1
)) {
128 char *newbase
= malloc_base(base
, path1
, pathlen1
);
129 if (show_tree_entry_in_recursive
)
130 diff_change(&diff_options
, mode1
, mode2
,
131 sha1
, sha2
, base
, path1
);
132 retval
= diff_tree_sha1(sha1
, sha2
, newbase
);
137 diff_change(&diff_options
, mode1
, mode2
, sha1
, sha2
, base
, path1
);
141 static int interesting(void *tree
, unsigned long size
, const char *base
)
146 int baselen
, pathlen
;
151 (void)extract(tree
, size
, &path
, &mode
);
153 pathlen
= strlen(path
);
154 baselen
= strlen(base
);
156 for (i
=0; i
< nr_paths
; i
++) {
157 const char *match
= paths
[i
];
158 int matchlen
= pathlens
[i
];
160 if (baselen
>= matchlen
) {
161 /* If it doesn't match, move along... */
162 if (strncmp(base
, match
, matchlen
))
165 /* The base is a subdirectory of a path which was specified. */
169 /* Does the base match? */
170 if (strncmp(base
, match
, baselen
))
176 if (pathlen
> matchlen
)
179 if (matchlen
> pathlen
) {
180 if (match
[pathlen
] != '/')
186 if (strncmp(path
, match
, pathlen
))
191 return 0; /* No matches */
194 /* A whole sub-tree went away or appeared */
195 static void show_tree(const char *prefix
, void *tree
, unsigned long size
, const char *base
)
198 if (interesting(tree
, size
, base
))
199 show_file(prefix
, tree
, size
, base
);
200 update_tree_entry(&tree
, &size
);
204 static int diff_tree(void *tree1
, unsigned long size1
, void *tree2
, unsigned long size2
, const char *base
)
206 while (size1
| size2
) {
207 if (nr_paths
&& size1
&& !interesting(tree1
, size1
, base
)) {
208 update_tree_entry(&tree1
, &size1
);
211 if (nr_paths
&& size2
&& !interesting(tree2
, size2
, base
)) {
212 update_tree_entry(&tree2
, &size2
);
216 show_file("+", tree2
, size2
, base
);
217 update_tree_entry(&tree2
, &size2
);
221 show_file("-", tree1
, size1
, base
);
222 update_tree_entry(&tree1
, &size1
);
225 switch (compare_tree_entry(tree1
, size1
, tree2
, size2
, base
)) {
227 update_tree_entry(&tree1
, &size1
);
230 update_tree_entry(&tree1
, &size1
);
233 update_tree_entry(&tree2
, &size2
);
236 die("git-diff-tree: internal error");
241 static int diff_tree_sha1(const unsigned char *old
, const unsigned char *new, const char *base
)
244 unsigned long size1
, size2
;
247 tree1
= read_object_with_reference(old
, "tree", &size1
, NULL
);
249 die("unable to read source tree (%s)", sha1_to_hex(old
));
250 tree2
= read_object_with_reference(new, "tree", &size2
, NULL
);
252 die("unable to read destination tree (%s)", sha1_to_hex(new));
253 retval
= diff_tree(tree1
, size1
, tree2
, size2
, base
);
259 static void call_diff_setup_done(void)
261 diff_setup_done(&diff_options
);
264 static int call_diff_flush(void)
266 diffcore_std(&diff_options
);
267 if (diff_queue_is_empty()) {
268 int saved_fmt
= diff_options
.output_format
;
269 diff_options
.output_format
= DIFF_FORMAT_NO_OUTPUT
;
270 diff_flush(&diff_options
);
271 diff_options
.output_format
= saved_fmt
;
275 printf("%s%c", header
, diff_options
.line_termination
);
278 diff_flush(&diff_options
);
282 static int diff_tree_sha1_top(const unsigned char *old
,
283 const unsigned char *new, const char *base
)
287 call_diff_setup_done();
288 ret
= diff_tree_sha1(old
, new, base
);
293 static int diff_root_tree(const unsigned char *new, const char *base
)
299 call_diff_setup_done();
300 tree
= read_object_with_reference(new, "tree", &size
, NULL
);
302 die("unable to read root tree (%s)", sha1_to_hex(new));
303 retval
= diff_tree("", 0, tree
, size
, base
);
309 static const char *generate_header(const char *commit
, const char *parent
, const char *msg
, unsigned long len
)
311 static char this_header
[16384];
317 offset
= sprintf(this_header
, "%s%s (from %s)\n", header_prefix
, commit
, parent
);
318 offset
+= pretty_print_commit(commit_format
, msg
, len
, this_header
+ offset
, sizeof(this_header
) - offset
);
322 static int diff_tree_commit(const unsigned char *commit
, const char *name
)
324 unsigned long size
, offset
;
325 char *buf
= read_object_with_reference(commit
, "commit", &size
, NULL
);
331 static char commit_name
[60];
332 strcpy(commit_name
, sha1_to_hex(commit
));
337 if (show_root_diff
&& memcmp(buf
+ 46, "parent ", 7)) {
338 header
= generate_header(name
, "root", buf
, size
);
339 diff_root_tree(commit
, "");
342 /* More than one parent? */
344 if (!memcmp(buf
+ 46 + 48, "parent ", 7))
349 while (offset
+ 48 < size
&& !memcmp(buf
+ offset
, "parent ", 7)) {
350 unsigned char parent
[20];
351 if (get_sha1_hex(buf
+ offset
+ 7, parent
))
353 header
= generate_header(name
, sha1_to_hex(parent
), buf
, size
);
354 diff_tree_sha1_top(parent
, commit
, "");
355 if (!header
&& verbose_header
) {
356 header_prefix
= "\ndiff-tree ";
358 * Don't print multiple merge entries if we
359 * don't print the diffs.
368 static int diff_tree_stdin(char *line
)
370 int len
= strlen(line
);
371 unsigned char commit
[20], parent
[20];
372 static char this_header
[1000];
374 if (!len
|| line
[len
-1] != '\n')
377 if (get_sha1_hex(line
, commit
))
379 if (isspace(line
[40]) && !get_sha1_hex(line
+41, parent
)) {
382 sprintf(this_header
, "%s (from %s)\n", line
, line
+41);
383 header
= this_header
;
384 return diff_tree_sha1_top(parent
, commit
, "");
387 return diff_tree_commit(commit
, line
);
390 static int count_paths(const char **paths
)
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];
408 const char *prefix
= setup_git_directory();
410 git_config(git_default_config
);
412 diff_setup(&diff_options
);
425 if (nr_sha1
< 2 && !get_sha1(arg
, sha1
[nr_sha1
])) {
432 diff_opt_cnt
= diff_opt_parse(&diff_options
, argv
, argc
);
433 if (diff_opt_cnt
< 0)
434 usage(diff_tree_usage
);
435 else if (diff_opt_cnt
) {
436 argv
+= diff_opt_cnt
- 1;
437 argc
-= diff_opt_cnt
- 1;
442 if (!strcmp(arg
, "--")) {
447 if (!strcmp(arg
, "-r")) {
451 if (!strcmp(arg
, "-t")) {
452 recursive
= show_tree_entry_in_recursive
= 1;
455 if (!strcmp(arg
, "-m")) {
459 if (!strcmp(arg
, "-v")) {
461 header_prefix
= "diff-tree ";
464 if (!strncmp(arg
, "--pretty", 8)) {
466 header_prefix
= "diff-tree ";
467 commit_format
= get_commit_format(arg
+8);
470 if (!strcmp(arg
, "--stdin")) {
474 if (!strcmp(arg
, "--root")) {
478 usage(diff_tree_usage
);
480 if (diff_options
.output_format
== DIFF_FORMAT_PATCH
)
483 paths
= get_pathspec(prefix
, argv
);
487 nr_paths
= count_paths(paths
);
488 pathlens
= xmalloc(nr_paths
* sizeof(int));
489 for (i
=0; i
<nr_paths
; i
++)
490 pathlens
[i
] = strlen(paths
[i
]);
496 usage(diff_tree_usage
);
499 diff_tree_commit(sha1
[0], NULL
);
502 diff_tree_sha1_top(sha1
[0], sha1
[1], "");
509 if (diff_options
.detect_rename
)
510 diff_options
.setup
|= (DIFF_SETUP_USE_SIZE_CACHE
|
511 DIFF_SETUP_USE_CACHE
);
512 while (fgets(line
, sizeof(line
), stdin
))
513 diff_tree_stdin(line
);