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;
13 static struct diff_options diff_options
;
15 static const char *header
= NULL
;
16 static const char *header_prefix
= "";
17 static enum cmit_fmt commit_format
= CMIT_FMT_RAW
;
19 // What paths are we interested in?
20 static int nr_paths
= 0;
21 static const char **paths
= NULL
;
22 static int *pathlens
= NULL
;
24 static int diff_tree_sha1(const unsigned char *old
, const unsigned char *new, const char *base
);
26 static void update_tree_entry(void **bufp
, unsigned long *sizep
)
29 unsigned long size
= *sizep
;
30 int len
= strlen(buf
) + 1 + 20;
33 die("corrupt tree file");
38 static const unsigned char *extract(void *tree
, unsigned long size
, const char **pathp
, unsigned int *modep
)
40 int len
= strlen(tree
)+1;
41 const unsigned char *sha1
= tree
+ len
;
42 const char *path
= strchr(tree
, ' ');
45 if (!path
|| size
< len
+ 20 || sscanf(tree
, "%o", &mode
) != 1)
46 die("corrupt tree file");
48 *modep
= DIFF_FILE_CANON_MODE(mode
);
52 static char *malloc_base(const char *base
, const char *path
, int pathlen
)
54 int baselen
= strlen(base
);
55 char *newbase
= xmalloc(baselen
+ pathlen
+ 2);
56 memcpy(newbase
, base
, baselen
);
57 memcpy(newbase
+ baselen
, path
, pathlen
);
58 memcpy(newbase
+ baselen
+ pathlen
, "/", 2);
62 static void show_file(const char *prefix
, void *tree
, unsigned long size
, const char *base
);
63 static void show_tree(const char *prefix
, void *tree
, unsigned long size
, const char *base
);
65 /* A file entry went away or appeared */
66 static void show_file(const char *prefix
, void *tree
, unsigned long size
, const char *base
)
70 const unsigned char *sha1
= extract(tree
, size
, &path
, &mode
);
72 if (recursive
&& S_ISDIR(mode
)) {
75 char *newbase
= malloc_base(base
, path
, strlen(path
));
78 tree
= read_sha1_file(sha1
, type
, &size
);
79 if (!tree
|| strcmp(type
, "tree"))
80 die("corrupt tree sha %s", sha1_to_hex(sha1
));
82 show_tree(prefix
, tree
, size
, newbase
);
89 diff_addremove(&diff_options
, prefix
[0], mode
, sha1
, base
, path
);
92 static int compare_tree_entry(void *tree1
, unsigned long size1
, void *tree2
, unsigned long size2
, const char *base
)
94 unsigned mode1
, mode2
;
95 const char *path1
, *path2
;
96 const unsigned char *sha1
, *sha2
;
97 int cmp
, pathlen1
, pathlen2
;
99 sha1
= extract(tree1
, size1
, &path1
, &mode1
);
100 sha2
= extract(tree2
, size2
, &path2
, &mode2
);
102 pathlen1
= strlen(path1
);
103 pathlen2
= strlen(path2
);
104 cmp
= base_name_compare(path1
, pathlen1
, mode1
, path2
, pathlen2
, mode2
);
106 show_file("-", tree1
, size1
, base
);
110 show_file("+", tree2
, size2
, base
);
113 if (!diff_options
.find_copies_harder
&&
114 !memcmp(sha1
, sha2
, 20) && mode1
== mode2
)
118 * If the filemode has changed to/from a directory from/to a regular
119 * file, we need to consider it a remove and an add.
121 if (S_ISDIR(mode1
) != S_ISDIR(mode2
)) {
122 show_file("-", tree1
, size1
, base
);
123 show_file("+", tree2
, size2
, base
);
127 if (recursive
&& S_ISDIR(mode1
)) {
129 char *newbase
= malloc_base(base
, path1
, pathlen1
);
130 if (show_tree_entry_in_recursive
)
131 diff_change(&diff_options
, mode1
, mode2
,
132 sha1
, sha2
, base
, path1
);
133 retval
= diff_tree_sha1(sha1
, sha2
, newbase
);
138 diff_change(&diff_options
, mode1
, mode2
, sha1
, sha2
, base
, path1
);
142 static int interesting(void *tree
, unsigned long size
, const char *base
)
147 int baselen
, pathlen
;
152 (void)extract(tree
, size
, &path
, &mode
);
154 pathlen
= strlen(path
);
155 baselen
= strlen(base
);
157 for (i
=0; i
< nr_paths
; i
++) {
158 const char *match
= paths
[i
];
159 int matchlen
= pathlens
[i
];
161 if (baselen
>= matchlen
) {
162 /* If it doesn't match, move along... */
163 if (strncmp(base
, match
, matchlen
))
166 /* The base is a subdirectory of a path which was specified. */
170 /* Does the base match? */
171 if (strncmp(base
, match
, baselen
))
177 if (pathlen
> matchlen
)
180 if (matchlen
> pathlen
) {
181 if (match
[pathlen
] != '/')
187 if (strncmp(path
, match
, pathlen
))
192 return 0; /* No matches */
195 /* A whole sub-tree went away or appeared */
196 static void show_tree(const char *prefix
, void *tree
, unsigned long size
, const char *base
)
199 if (interesting(tree
, size
, base
))
200 show_file(prefix
, tree
, size
, base
);
201 update_tree_entry(&tree
, &size
);
205 static int diff_tree(void *tree1
, unsigned long size1
, void *tree2
, unsigned long size2
, const char *base
)
207 while (size1
| size2
) {
208 if (nr_paths
&& size1
&& !interesting(tree1
, size1
, base
)) {
209 update_tree_entry(&tree1
, &size1
);
212 if (nr_paths
&& size2
&& !interesting(tree2
, size2
, base
)) {
213 update_tree_entry(&tree2
, &size2
);
217 show_file("+", tree2
, size2
, base
);
218 update_tree_entry(&tree2
, &size2
);
222 show_file("-", tree1
, size1
, base
);
223 update_tree_entry(&tree1
, &size1
);
226 switch (compare_tree_entry(tree1
, size1
, tree2
, size2
, base
)) {
228 update_tree_entry(&tree1
, &size1
);
231 update_tree_entry(&tree1
, &size1
);
234 update_tree_entry(&tree2
, &size2
);
237 die("git-diff-tree: internal error");
242 static int diff_tree_sha1(const unsigned char *old
, const unsigned char *new, const char *base
)
245 unsigned long size1
, size2
;
248 tree1
= read_object_with_reference(old
, "tree", &size1
, NULL
);
250 die("unable to read source tree (%s)", sha1_to_hex(old
));
251 tree2
= read_object_with_reference(new, "tree", &size2
, NULL
);
253 die("unable to read destination tree (%s)", sha1_to_hex(new));
254 retval
= diff_tree(tree1
, size1
, tree2
, size2
, base
);
260 static void call_diff_setup_done(void)
262 diff_setup_done(&diff_options
);
265 static int call_diff_flush(void)
267 diffcore_std(&diff_options
);
268 if (diff_queue_is_empty()) {
269 int saved_fmt
= diff_options
.output_format
;
270 diff_options
.output_format
= DIFF_FORMAT_NO_OUTPUT
;
271 diff_flush(&diff_options
);
272 diff_options
.output_format
= saved_fmt
;
276 printf("%s%c", header
, diff_options
.line_termination
);
279 diff_flush(&diff_options
);
283 static int diff_tree_sha1_top(const unsigned char *old
,
284 const unsigned char *new, const char *base
)
288 call_diff_setup_done();
289 ret
= diff_tree_sha1(old
, new, base
);
294 static int diff_root_tree(const unsigned char *new, const char *base
)
300 call_diff_setup_done();
301 tree
= read_object_with_reference(new, "tree", &size
, NULL
);
303 die("unable to read root tree (%s)", sha1_to_hex(new));
304 retval
= diff_tree("", 0, tree
, size
, base
);
310 static const char *generate_header(const char *commit
, const char *parent
, const char *msg
, unsigned long len
)
312 static char this_header
[16384];
318 offset
= sprintf(this_header
, "%s%s (from %s)\n", header_prefix
, commit
, parent
);
319 offset
+= pretty_print_commit(commit_format
, msg
, len
, this_header
+ offset
, sizeof(this_header
) - offset
);
323 static int diff_tree_commit(const unsigned char *commit
, const char *name
)
325 unsigned long size
, offset
;
326 char *buf
= read_object_with_reference(commit
, "commit", &size
, NULL
);
332 static char commit_name
[60];
333 strcpy(commit_name
, sha1_to_hex(commit
));
338 if (show_root_diff
&& memcmp(buf
+ 46, "parent ", 7)) {
339 header
= generate_header(name
, "root", buf
, size
);
340 diff_root_tree(commit
, "");
343 /* More than one parent? */
345 if (!memcmp(buf
+ 46 + 48, "parent ", 7))
350 while (offset
+ 48 < size
&& !memcmp(buf
+ offset
, "parent ", 7)) {
351 unsigned char parent
[20];
352 if (get_sha1_hex(buf
+ offset
+ 7, parent
))
354 header
= generate_header(name
, sha1_to_hex(parent
), buf
, size
);
355 diff_tree_sha1_top(parent
, commit
, "");
356 if (!header
&& verbose_header
) {
357 header_prefix
= "\ndiff-tree ";
359 * Don't print multiple merge entries if we
360 * don't print the diffs.
369 static int diff_tree_stdin(char *line
)
371 int len
= strlen(line
);
372 unsigned char commit
[20], parent
[20];
373 static char this_header
[1000];
375 if (!len
|| line
[len
-1] != '\n')
378 if (get_sha1_hex(line
, commit
))
380 if (isspace(line
[40]) && !get_sha1_hex(line
+41, parent
)) {
383 sprintf(this_header
, "%s (from %s)\n", line
, line
+41);
384 header
= this_header
;
385 return diff_tree_sha1_top(parent
, commit
, "");
388 return diff_tree_commit(commit
, line
);
391 static int count_paths(const char **paths
)
399 static const char diff_tree_usage
[] =
400 "git-diff-tree [--stdin] [-m] [-s] [-v] [--pretty] [-t] "
401 "[<common diff options>] <tree-ish> <tree-ish>"
402 COMMON_DIFF_OPTIONS_HELP
;
404 int main(int argc
, const char **argv
)
408 unsigned char sha1
[2][20];
409 const char *prefix
= setup_git_directory();
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
);