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 const char *header
= NULL
;
18 static const char *header_prefix
= "";
20 // What paths are we interested in?
21 static int nr_paths
= 0;
22 static const char **paths
= NULL
;
23 static int *pathlens
= NULL
;
25 static int diff_tree_sha1(const unsigned char *old
, const unsigned char *new, const char *base
);
27 static void update_tree_entry(void **bufp
, unsigned long *sizep
)
30 unsigned long size
= *sizep
;
31 int len
= strlen(buf
) + 1 + 20;
34 die("corrupt tree file");
39 static const unsigned char *extract(void *tree
, unsigned long size
, const char **pathp
, unsigned int *modep
)
41 int len
= strlen(tree
)+1;
42 const unsigned char *sha1
= tree
+ len
;
43 const char *path
= strchr(tree
, ' ');
45 if (!path
|| size
< len
+ 20 || sscanf(tree
, "%o", modep
) != 1)
46 die("corrupt tree file");
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(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 (!memcmp(sha1
, sha2
, 20) && mode1
== mode2
)
116 * If the filemode has changed to/from a directory from/to a regular
117 * file, we need to consider it a remove and an add.
119 if (S_ISDIR(mode1
) != S_ISDIR(mode2
)) {
120 show_file("-", tree1
, size1
, base
);
121 show_file("+", tree2
, size2
, base
);
125 if (recursive
&& S_ISDIR(mode1
)) {
127 char *newbase
= malloc_base(base
, path1
, pathlen1
);
128 if (show_tree_entry_in_recursive
)
129 diff_change(mode1
, mode2
, sha1
, sha2
, base
, path1
);
130 retval
= diff_tree_sha1(sha1
, sha2
, newbase
);
135 diff_change(mode1
, mode2
, sha1
, sha2
, base
, path1
);
139 static int interesting(void *tree
, unsigned long size
, const char *base
)
144 int baselen
, pathlen
;
149 (void)extract(tree
, size
, &path
, &mode
);
151 pathlen
= strlen(path
);
152 baselen
= strlen(base
);
154 for (i
=0; i
< nr_paths
; i
++) {
155 const char *match
= paths
[i
];
156 int matchlen
= pathlens
[i
];
158 if (baselen
>= matchlen
) {
159 /* If it doesn't match, move along... */
160 if (strncmp(base
, match
, matchlen
))
163 /* The base is a subdirectory of a path which was specified. */
167 /* Does the base match? */
168 if (strncmp(base
, match
, baselen
))
174 if (pathlen
> matchlen
)
177 if (matchlen
> pathlen
) {
178 if (match
[pathlen
] != '/')
184 if (strncmp(path
, match
, pathlen
))
189 return 0; /* No matches */
192 /* A whole sub-tree went away or appeared */
193 static void show_tree(const char *prefix
, void *tree
, unsigned long size
, const char *base
)
196 if (interesting(tree
, size
, base
))
197 show_file(prefix
, tree
, size
, base
);
198 update_tree_entry(&tree
, &size
);
202 static int diff_tree(void *tree1
, unsigned long size1
, void *tree2
, unsigned long size2
, const char *base
)
204 while (size1
| size2
) {
205 if (nr_paths
&& size1
&& !interesting(tree1
, size1
, base
)) {
206 update_tree_entry(&tree1
, &size1
);
209 if (nr_paths
&& size2
&& !interesting(tree2
, size2
, base
)) {
210 update_tree_entry(&tree2
, &size2
);
214 show_file("+", tree2
, size2
, base
);
215 update_tree_entry(&tree2
, &size2
);
219 show_file("-", tree1
, size1
, base
);
220 update_tree_entry(&tree1
, &size1
);
223 switch (compare_tree_entry(tree1
, size1
, tree2
, size2
, base
)) {
225 update_tree_entry(&tree1
, &size1
);
228 update_tree_entry(&tree1
, &size1
);
231 update_tree_entry(&tree2
, &size2
);
234 die("git-diff-tree: internal error");
239 static int diff_tree_sha1(const unsigned char *old
, const unsigned char *new, const char *base
)
242 unsigned long size1
, size2
;
245 tree1
= read_object_with_reference(old
, "tree", &size1
, NULL
);
247 die("unable to read source tree (%s)", sha1_to_hex(old
));
248 tree2
= read_object_with_reference(new, "tree", &size2
, NULL
);
250 die("unable to read destination tree (%s)", sha1_to_hex(new));
251 retval
= diff_tree(tree1
, size1
, tree2
, size2
, base
);
257 static void call_diff_setup(void)
259 diff_setup(diff_setup_opt
);
262 static int call_diff_flush(void)
265 detect_rename
, diff_score_opt
,
266 pickaxe
, pickaxe_opts
);
267 if (diff_queue_is_empty()) {
268 diff_flush(DIFF_FORMAT_NO_OUTPUT
, 0);
272 const char *fmt
= "%s";
273 if (diff_output_format
== DIFF_FORMAT_MACHINE
)
276 printf(fmt
, header
, 0);
279 diff_flush(diff_output_format
, 1);
283 static int diff_tree_sha1_top(const unsigned char *old
,
284 const unsigned char *new, const char *base
)
289 ret
= diff_tree_sha1(old
, new, base
);
294 static int diff_root_tree(const unsigned char *new, const char *base
)
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 int get_one_line(const char *msg
, unsigned long len
)
322 static int add_author_info(char *buf
, const char *line
, int len
)
325 unsigned int namelen
;
329 line
+= strlen("author ");
330 date
= strchr(line
, '>');
333 namelen
= ++date
- line
;
334 time
= strtoul(date
, &date
, 10);
335 tz
= strtol(date
, NULL
, 10);
337 return sprintf(buf
, "Author: %.*s\nDate: %s\n",
339 show_date(time
, tz
));
342 static char *generate_header(const char *commit
, const char *parent
, const char *msg
, unsigned long len
)
344 static char this_header
[16384];
347 offset
= sprintf(this_header
, "%s%s (from %s)\n", header_prefix
, commit
, parent
);
348 if (verbose_header
) {
352 const char *line
= msg
;
353 int linelen
= get_one_line(msg
, len
);
359 * We want some slop for indentation and a possible
360 * final "...". Thus the "+ 20".
362 if (offset
+ linelen
+ 20 > sizeof(this_header
)) {
363 memcpy(this_header
+ offset
, " ...\n", 8);
373 if (!memcmp(line
, "author ", 7))
374 offset
+= add_author_info(this_header
+ offset
, line
, linelen
);
377 memset(this_header
+ offset
, ' ', 4);
378 memcpy(this_header
+ offset
+ 4, line
, linelen
);
379 offset
+= linelen
+ 4;
381 /* Make sure there is an EOLN */
382 if (this_header
[offset
-1] != '\n')
383 this_header
[offset
++] = '\n';
384 /* Add _another_ EOLN if we are doing diff output */
385 this_header
[offset
++] = '\n';
386 this_header
[offset
] = 0;
392 static int diff_tree_commit(const unsigned char *commit
, const char *name
)
394 unsigned long size
, offset
;
395 char *buf
= read_object_with_reference(commit
, "commit", &size
, NULL
);
401 static char commit_name
[60];
402 strcpy(commit_name
, sha1_to_hex(commit
));
407 if (show_root_diff
&& memcmp(buf
+ 46, "parent ", 7)) {
408 header
= generate_header(name
, "root", buf
, size
);
409 diff_root_tree(commit
, "");
412 /* More than one parent? */
414 if (!memcmp(buf
+ 46 + 48, "parent ", 7))
419 while (offset
+ 48 < size
&& !memcmp(buf
+ offset
, "parent ", 7)) {
420 unsigned char parent
[20];
421 if (get_sha1_hex(buf
+ offset
+ 7, parent
))
423 header
= generate_header(name
, sha1_to_hex(parent
), buf
, size
);
424 diff_tree_sha1_top(parent
, commit
, "");
425 if (!header
&& verbose_header
) {
426 header_prefix
= "\ndiff-tree ";
428 * Don't print multiple merge entries if we
429 * don't print the diffs.
437 static int diff_tree_stdin(char *line
)
439 int len
= strlen(line
);
440 unsigned char commit
[20], parent
[20];
441 static char this_header
[1000];
443 if (!len
|| line
[len
-1] != '\n')
446 if (get_sha1_hex(line
, commit
))
448 if (isspace(line
[40]) && !get_sha1_hex(line
+41, parent
)) {
451 sprintf(this_header
, "%s (from %s)\n", line
, line
+41);
452 header
= this_header
;
453 return diff_tree_sha1_top(parent
, commit
, "");
456 return diff_tree_commit(commit
, line
);
459 static char *diff_tree_usage
=
460 "git-diff-tree [-p] [-r] [-z] [--stdin] [-M] [-C] [-R] [-S<string>] [-m] [-s] [-v] [-t] <tree-ish> <tree-ish>";
462 int main(int argc
, const char **argv
)
466 unsigned char sha1
[2][20];
479 if (nr_sha1
< 2 && !get_sha1(arg
, sha1
[nr_sha1
])) {
486 if (!strcmp(arg
, "--")) {
491 if (!strcmp(arg
, "-r")) {
495 if (!strcmp(arg
, "-t")) {
496 recursive
= show_tree_entry_in_recursive
= 1;
499 if (!strcmp(arg
, "-R")) {
500 diff_setup_opt
|= DIFF_SETUP_REVERSE
;
503 if (!strcmp(arg
, "-p")) {
504 diff_output_format
= DIFF_FORMAT_PATCH
;
508 if (!strncmp(arg
, "-S", 2)) {
512 if (!strcmp(arg
, "--pickaxe-all")) {
513 pickaxe_opts
= DIFF_PICKAXE_ALL
;
516 if (!strncmp(arg
, "-M", 2)) {
517 detect_rename
= DIFF_DETECT_RENAME
;
518 diff_score_opt
= diff_scoreopt_parse(arg
);
521 if (!strncmp(arg
, "-C", 2)) {
522 detect_rename
= DIFF_DETECT_COPY
;
523 diff_score_opt
= diff_scoreopt_parse(arg
);
526 if (!strcmp(arg
, "-z")) {
527 diff_output_format
= DIFF_FORMAT_MACHINE
;
530 if (!strcmp(arg
, "-m")) {
534 if (!strcmp(arg
, "-s")) {
535 diff_output_format
= DIFF_FORMAT_NO_OUTPUT
;
538 if (!strcmp(arg
, "-v")) {
540 header_prefix
= "diff-tree ";
543 if (!strcmp(arg
, "--stdin")) {
547 if (!strcmp(arg
, "--root")) {
551 usage(diff_tree_usage
);
559 pathlens
= xmalloc(nr_paths
* sizeof(int));
560 for (i
=0; i
<nr_paths
; i
++)
561 pathlens
[i
] = strlen(paths
[i
]);
567 usage(diff_tree_usage
);
570 diff_tree_commit(sha1
[0], NULL
);
573 diff_tree_sha1_top(sha1
[0], sha1
[1], "");
581 diff_setup_opt
|= (DIFF_SETUP_USE_SIZE_CACHE
|
582 DIFF_SETUP_USE_CACHE
);
583 while (fgets(line
, sizeof(line
), stdin
))
584 diff_tree_stdin(line
);