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;
11 static int diff_output_format
= DIFF_FORMAT_HUMAN
;
12 static int detect_rename
= 0;
13 static int diff_setup_opt
= 0;
14 static int diff_score_opt
= 0;
15 static const char *pickaxe
= NULL
;
16 static int pickaxe_opts
= 0;
17 static int diff_break_opt
= -1;
18 static const char *header
= NULL
;
19 static const char *header_prefix
= "";
21 // What paths are we interested in?
22 static int nr_paths
= 0;
23 static const char **paths
= NULL
;
24 static int *pathlens
= NULL
;
26 static int diff_tree_sha1(const unsigned char *old
, const unsigned char *new, const char *base
);
28 static void update_tree_entry(void **bufp
, unsigned long *sizep
)
31 unsigned long size
= *sizep
;
32 int len
= strlen(buf
) + 1 + 20;
35 die("corrupt tree file");
40 static const unsigned char *extract(void *tree
, unsigned long size
, const char **pathp
, unsigned int *modep
)
42 int len
= strlen(tree
)+1;
43 const unsigned char *sha1
= tree
+ len
;
44 const char *path
= strchr(tree
, ' ');
46 if (!path
|| size
< len
+ 20 || sscanf(tree
, "%o", modep
) != 1)
47 die("corrupt tree file");
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(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 (!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(mode1
, mode2
, sha1
, sha2
, base
, path1
);
131 retval
= diff_tree_sha1(sha1
, sha2
, newbase
);
136 diff_change(mode1
, mode2
, sha1
, sha2
, base
, path1
);
140 static int interesting(void *tree
, unsigned long size
, const char *base
)
145 int baselen
, pathlen
;
150 (void)extract(tree
, size
, &path
, &mode
);
152 pathlen
= strlen(path
);
153 baselen
= strlen(base
);
155 for (i
=0; i
< nr_paths
; i
++) {
156 const char *match
= paths
[i
];
157 int matchlen
= pathlens
[i
];
159 if (baselen
>= matchlen
) {
160 /* If it doesn't match, move along... */
161 if (strncmp(base
, match
, matchlen
))
164 /* The base is a subdirectory of a path which was specified. */
168 /* Does the base match? */
169 if (strncmp(base
, match
, baselen
))
175 if (pathlen
> matchlen
)
178 if (matchlen
> pathlen
) {
179 if (match
[pathlen
] != '/')
185 if (strncmp(path
, match
, pathlen
))
190 return 0; /* No matches */
193 /* A whole sub-tree went away or appeared */
194 static void show_tree(const char *prefix
, void *tree
, unsigned long size
, const char *base
)
197 if (interesting(tree
, size
, base
))
198 show_file(prefix
, tree
, size
, base
);
199 update_tree_entry(&tree
, &size
);
203 static int diff_tree(void *tree1
, unsigned long size1
, void *tree2
, unsigned long size2
, const char *base
)
205 while (size1
| size2
) {
206 if (nr_paths
&& size1
&& !interesting(tree1
, size1
, base
)) {
207 update_tree_entry(&tree1
, &size1
);
210 if (nr_paths
&& size2
&& !interesting(tree2
, size2
, base
)) {
211 update_tree_entry(&tree2
, &size2
);
215 show_file("+", tree2
, size2
, base
);
216 update_tree_entry(&tree2
, &size2
);
220 show_file("-", tree1
, size1
, base
);
221 update_tree_entry(&tree1
, &size1
);
224 switch (compare_tree_entry(tree1
, size1
, tree2
, size2
, base
)) {
226 update_tree_entry(&tree1
, &size1
);
229 update_tree_entry(&tree1
, &size1
);
232 update_tree_entry(&tree2
, &size2
);
235 die("git-diff-tree: internal error");
240 static int diff_tree_sha1(const unsigned char *old
, const unsigned char *new, const char *base
)
243 unsigned long size1
, size2
;
246 tree1
= read_object_with_reference(old
, "tree", &size1
, NULL
);
248 die("unable to read source tree (%s)", sha1_to_hex(old
));
249 tree2
= read_object_with_reference(new, "tree", &size2
, NULL
);
251 die("unable to read destination tree (%s)", sha1_to_hex(new));
252 retval
= diff_tree(tree1
, size1
, tree2
, size2
, base
);
258 static void call_diff_setup(void)
260 diff_setup(diff_setup_opt
);
263 static int call_diff_flush(void)
266 detect_rename
, diff_score_opt
,
267 pickaxe
, pickaxe_opts
,
269 if (diff_queue_is_empty()) {
270 diff_flush(DIFF_FORMAT_NO_OUTPUT
, 0);
274 const char *fmt
= "%s";
275 if (diff_output_format
== DIFF_FORMAT_MACHINE
)
278 printf(fmt
, header
, 0);
281 diff_flush(diff_output_format
, 1);
285 static int diff_tree_sha1_top(const unsigned char *old
,
286 const unsigned char *new, const char *base
)
291 ret
= diff_tree_sha1(old
, new, base
);
296 static int diff_root_tree(const unsigned char *new, const char *base
)
303 tree
= read_object_with_reference(new, "tree", &size
, NULL
);
305 die("unable to read root tree (%s)", sha1_to_hex(new));
306 retval
= diff_tree("", 0, tree
, size
, base
);
312 static int get_one_line(const char *msg
, unsigned long len
)
324 static int add_author_info(char *buf
, const char *line
, int len
)
327 unsigned int namelen
;
331 line
+= strlen("author ");
332 date
= strchr(line
, '>');
335 namelen
= ++date
- line
;
336 time
= strtoul(date
, &date
, 10);
337 tz
= strtol(date
, NULL
, 10);
339 return sprintf(buf
, "Author: %.*s\nDate: %s\n",
341 show_date(time
, tz
));
344 static char *generate_header(const char *commit
, const char *parent
, const char *msg
, unsigned long len
)
346 static char this_header
[16384];
349 offset
= sprintf(this_header
, "%s%s (from %s)\n", header_prefix
, commit
, parent
);
350 if (verbose_header
) {
354 const char *line
= msg
;
355 int linelen
= get_one_line(msg
, len
);
361 * We want some slop for indentation and a possible
362 * final "...". Thus the "+ 20".
364 if (offset
+ linelen
+ 20 > sizeof(this_header
)) {
365 memcpy(this_header
+ offset
, " ...\n", 8);
375 if (!memcmp(line
, "author ", 7))
376 offset
+= add_author_info(this_header
+ offset
, line
, linelen
);
379 memset(this_header
+ offset
, ' ', 4);
380 memcpy(this_header
+ offset
+ 4, line
, linelen
);
381 offset
+= linelen
+ 4;
383 /* Make sure there is an EOLN */
384 if (this_header
[offset
-1] != '\n')
385 this_header
[offset
++] = '\n';
386 /* Add _another_ EOLN if we are doing diff output */
387 this_header
[offset
++] = '\n';
388 this_header
[offset
] = 0;
394 static int diff_tree_commit(const unsigned char *commit
, const char *name
)
396 unsigned long size
, offset
;
397 char *buf
= read_object_with_reference(commit
, "commit", &size
, NULL
);
403 static char commit_name
[60];
404 strcpy(commit_name
, sha1_to_hex(commit
));
409 if (show_root_diff
&& memcmp(buf
+ 46, "parent ", 7)) {
410 header
= generate_header(name
, "root", buf
, size
);
411 diff_root_tree(commit
, "");
414 /* More than one parent? */
416 if (!memcmp(buf
+ 46 + 48, "parent ", 7))
421 while (offset
+ 48 < size
&& !memcmp(buf
+ offset
, "parent ", 7)) {
422 unsigned char parent
[20];
423 if (get_sha1_hex(buf
+ offset
+ 7, parent
))
425 header
= generate_header(name
, sha1_to_hex(parent
), buf
, size
);
426 diff_tree_sha1_top(parent
, commit
, "");
427 if (!header
&& verbose_header
) {
428 header_prefix
= "\ndiff-tree ";
430 * Don't print multiple merge entries if we
431 * don't print the diffs.
439 static int diff_tree_stdin(char *line
)
441 int len
= strlen(line
);
442 unsigned char commit
[20], parent
[20];
443 static char this_header
[1000];
445 if (!len
|| line
[len
-1] != '\n')
448 if (get_sha1_hex(line
, commit
))
450 if (isspace(line
[40]) && !get_sha1_hex(line
+41, parent
)) {
453 sprintf(this_header
, "%s (from %s)\n", line
, line
+41);
454 header
= this_header
;
455 return diff_tree_sha1_top(parent
, commit
, "");
458 return diff_tree_commit(commit
, line
);
461 static char *diff_tree_usage
=
462 "git-diff-tree [-p] [-r] [-z] [--stdin] [-M] [-C] [-R] [-S<string>] [-m] [-s] [-v] [-t] <tree-ish> <tree-ish>";
464 int main(int argc
, const char **argv
)
468 unsigned char sha1
[2][20];
481 if (nr_sha1
< 2 && !get_sha1(arg
, sha1
[nr_sha1
])) {
488 if (!strcmp(arg
, "--")) {
493 if (!strcmp(arg
, "-r")) {
497 if (!strcmp(arg
, "-t")) {
498 recursive
= show_tree_entry_in_recursive
= 1;
501 if (!strcmp(arg
, "-R")) {
502 diff_setup_opt
|= DIFF_SETUP_REVERSE
;
505 if (!strcmp(arg
, "-p")) {
506 diff_output_format
= DIFF_FORMAT_PATCH
;
510 if (!strncmp(arg
, "-S", 2)) {
514 if (!strcmp(arg
, "--pickaxe-all")) {
515 pickaxe_opts
= DIFF_PICKAXE_ALL
;
518 if (!strncmp(arg
, "-M", 2)) {
519 detect_rename
= DIFF_DETECT_RENAME
;
520 diff_score_opt
= diff_scoreopt_parse(arg
);
523 if (!strncmp(arg
, "-C", 2)) {
524 detect_rename
= DIFF_DETECT_COPY
;
525 diff_score_opt
= diff_scoreopt_parse(arg
);
528 if (!strncmp(arg
, "-B", 2)) {
529 diff_break_opt
= diff_scoreopt_parse(arg
);
532 if (!strcmp(arg
, "-z")) {
533 diff_output_format
= DIFF_FORMAT_MACHINE
;
536 if (!strcmp(arg
, "-m")) {
540 if (!strcmp(arg
, "-s")) {
541 diff_output_format
= DIFF_FORMAT_NO_OUTPUT
;
544 if (!strcmp(arg
, "-v")) {
546 header_prefix
= "diff-tree ";
549 if (!strcmp(arg
, "--stdin")) {
553 if (!strcmp(arg
, "--root")) {
557 usage(diff_tree_usage
);
565 pathlens
= xmalloc(nr_paths
* sizeof(int));
566 for (i
=0; i
<nr_paths
; i
++)
567 pathlens
[i
] = strlen(paths
[i
]);
573 usage(diff_tree_usage
);
576 diff_tree_commit(sha1
[0], NULL
);
579 diff_tree_sha1_top(sha1
[0], sha1
[1], "");
587 diff_setup_opt
|= (DIFF_SETUP_USE_SIZE_CACHE
|
588 DIFF_SETUP_USE_CACHE
);
589 while (fgets(line
, sizeof(line
), stdin
))
590 diff_tree_stdin(line
);