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 read_stdin
= 0;
11 static int line_termination
= '\n';
12 static int generate_patch
= 0;
13 static int detect_rename
= 0;
14 static int reverse_diff
= 0;
15 static int diff_score_opt
= 0;
16 static char *pickaxe
= 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 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
);
79 if (recursive
&& S_ISDIR(mode
)) {
82 char *newbase
= malloc_base(base
, path
, strlen(path
));
85 tree
= read_sha1_file(sha1
, type
, &size
);
86 if (!tree
|| strcmp(type
, "tree"))
87 die("corrupt tree sha %s", sha1_to_hex(sha1
));
89 show_tree(prefix
, tree
, size
, newbase
);
96 diff_addremove(prefix
[0], mode
, sha1
, base
, path
);
99 static int compare_tree_entry(void *tree1
, unsigned long size1
, void *tree2
, unsigned long size2
, const char *base
)
101 unsigned mode1
, mode2
;
102 const char *path1
, *path2
;
103 const unsigned char *sha1
, *sha2
;
104 int cmp
, pathlen1
, pathlen2
;
106 sha1
= extract(tree1
, size1
, &path1
, &mode1
);
107 sha2
= extract(tree2
, size2
, &path2
, &mode2
);
109 pathlen1
= strlen(path1
);
110 pathlen2
= strlen(path2
);
111 cmp
= base_name_compare(path1
, pathlen1
, mode1
, path2
, pathlen2
, mode2
);
113 show_file("-", tree1
, size1
, base
);
117 show_file("+", tree2
, size2
, base
);
120 if (!memcmp(sha1
, sha2
, 20) && mode1
== mode2
)
124 * If the filemode has changed to/from a directory from/to a regular
125 * file, we need to consider it a remove and an add.
127 if (S_ISDIR(mode1
) != S_ISDIR(mode2
)) {
128 show_file("-", tree1
, size1
, base
);
129 show_file("+", tree2
, size2
, base
);
133 if (recursive
&& S_ISDIR(mode1
)) {
135 char *newbase
= malloc_base(base
, path1
, pathlen1
);
136 retval
= diff_tree_sha1(sha1
, sha2
, newbase
);
142 printf("%s", header
);
148 diff_change(mode1
, mode2
, sha1
, sha2
, base
, path1
);
152 static int interesting(void *tree
, unsigned long size
, const char *base
)
157 int baselen
, pathlen
;
162 (void)extract(tree
, size
, &path
, &mode
);
164 pathlen
= strlen(path
);
165 baselen
= strlen(base
);
167 for (i
=0; i
< nr_paths
; i
++) {
168 const char *match
= paths
[i
];
169 int matchlen
= pathlens
[i
];
171 if (baselen
>= matchlen
) {
172 /* If it doesn't match, move along... */
173 if (strncmp(base
, match
, matchlen
))
176 /* The base is a subdirectory of a path which was specified. */
180 /* Does the base match? */
181 if (strncmp(base
, match
, baselen
))
187 if (pathlen
> matchlen
)
190 if (matchlen
> pathlen
) {
191 if (match
[pathlen
] != '/')
197 if (strncmp(path
, match
, pathlen
))
202 return 0; /* No matches */
205 /* A whole sub-tree went away or appeared */
206 static void show_tree(const char *prefix
, void *tree
, unsigned long size
, const char *base
)
209 if (interesting(tree
, size
, base
))
210 show_file(prefix
, tree
, size
, base
);
211 update_tree_entry(&tree
, &size
);
215 static int diff_tree(void *tree1
, unsigned long size1
, void *tree2
, unsigned long size2
, const char *base
)
217 while (size1
| size2
) {
218 if (nr_paths
&& size1
&& !interesting(tree1
, size1
, base
)) {
219 update_tree_entry(&tree1
, &size1
);
222 if (nr_paths
&& size2
&& !interesting(tree2
, size2
, base
)) {
223 update_tree_entry(&tree2
, &size2
);
227 show_file("+", tree2
, size2
, base
);
228 update_tree_entry(&tree2
, &size2
);
232 show_file("-", tree1
, size1
, base
);
233 update_tree_entry(&tree1
, &size1
);
236 switch (compare_tree_entry(tree1
, size1
, tree2
, size2
, base
)) {
238 update_tree_entry(&tree1
, &size1
);
241 update_tree_entry(&tree1
, &size1
);
244 update_tree_entry(&tree2
, &size2
);
247 die("git-diff-tree: internal error");
252 static int diff_tree_sha1(const unsigned char *old
, const unsigned char *new, const char *base
)
255 unsigned long size1
, size2
;
258 tree1
= read_object_with_reference(old
, "tree", &size1
, NULL
);
260 die("unable to read source tree (%s)", sha1_to_hex(old
));
261 tree2
= read_object_with_reference(new, "tree", &size2
, NULL
);
263 die("unable to read destination tree (%s)", sha1_to_hex(new));
264 retval
= diff_tree(tree1
, size1
, tree2
, size2
, base
);
270 static int diff_tree_sha1_top(const unsigned char *old
,
271 const unsigned char *new, const char *base
)
275 diff_setup(detect_rename
, diff_score_opt
, pickaxe
,
276 reverse_diff
, (generate_patch
? -1 : line_termination
),
278 ret
= diff_tree_sha1(old
, new, base
);
283 static int diff_root_tree(const unsigned char *new, const char *base
)
289 diff_setup(detect_rename
, diff_score_opt
, pickaxe
,
290 reverse_diff
, (generate_patch
? -1 : line_termination
),
292 tree
= read_object_with_reference(new, "tree", &size
, NULL
);
294 die("unable to read root tree (%s)", sha1_to_hex(new));
295 retval
= diff_tree("", 0, tree
, size
, base
);
301 static int get_one_line(const char *msg
, unsigned long len
)
313 static int add_author_info(char *buf
, const char *line
, int len
)
316 unsigned int namelen
;
320 line
+= strlen("author ");
321 date
= strchr(line
, '>');
324 namelen
= ++date
- line
;
325 time
= strtoul(date
, &date
, 10);
326 tz
= strtol(date
, NULL
, 10);
328 return sprintf(buf
, "Author: %.*s\nDate: %s\n",
330 show_date(time
, tz
));
333 static char *generate_header(const char *commit
, const char *parent
, const char *msg
, unsigned long len
)
335 static char this_header
[1000];
338 offset
= sprintf(this_header
, "%s%s (from %s)\n", header_prefix
, commit
, parent
);
339 if (verbose_header
) {
343 const char *line
= msg
;
344 int linelen
= get_one_line(msg
, len
);
348 if (offset
+ linelen
+ 10 > sizeof(this_header
))
356 if (!memcmp(line
, "author ", 7))
357 offset
+= add_author_info(this_header
+ offset
, line
, linelen
);
360 memset(this_header
+ offset
, ' ', 4);
361 memcpy(this_header
+ offset
+ 4, line
, linelen
);
362 offset
+= linelen
+ 4;
364 this_header
[offset
++] = '\n';
365 this_header
[offset
] = 0;
371 static int diff_tree_commit(const unsigned char *commit
, const char *name
)
373 unsigned long size
, offset
;
374 char *buf
= read_object_with_reference(commit
, "commit", &size
, NULL
);
380 static char commit_name
[60];
381 strcpy(commit_name
, sha1_to_hex(commit
));
386 if (show_root_diff
&& memcmp(buf
+ 46, "parent ", 7)) {
387 header
= generate_header(name
, "root", buf
, size
);
388 diff_root_tree(commit
, "");
391 /* More than one parent? */
393 if (!memcmp(buf
+ 46 + 48, "parent ", 7))
398 while (offset
+ 48 < size
&& !memcmp(buf
+ offset
, "parent ", 7)) {
399 unsigned char parent
[20];
400 if (get_sha1_hex(buf
+ offset
+ 7, parent
))
402 header
= generate_header(name
, sha1_to_hex(parent
), buf
, size
);
403 diff_tree_sha1_top(parent
, commit
, "");
404 if (!header
&& verbose_header
)
405 header_prefix
= "\ndiff-tree ";
411 static int diff_tree_stdin(char *line
)
413 int len
= strlen(line
);
414 unsigned char commit
[20], parent
[20];
415 static char this_header
[1000];
417 if (!len
|| line
[len
-1] != '\n')
420 if (get_sha1_hex(line
, commit
))
422 if (isspace(line
[40]) && !get_sha1_hex(line
+41, parent
)) {
425 sprintf(this_header
, "%s (from %s)\n", line
, line
+41);
426 header
= this_header
;
427 return diff_tree_sha1_top(parent
, commit
, "");
430 return diff_tree_commit(commit
, line
);
433 static char *diff_tree_usage
=
434 "git-diff-tree [-p] [-r] [-z] [--stdin] [-M] [-C] [-R] [-S<string>] [-m] [-s] [-v] <tree-ish> <tree-ish>";
436 int main(int argc
, char **argv
)
440 unsigned char sha1
[2][20];
453 if (nr_sha1
< 2 && !get_sha1(arg
, sha1
[nr_sha1
])) {
460 if (!strcmp(arg
, "--")) {
465 if (!strcmp(arg
, "-r")) {
469 if (!strcmp(arg
, "-R")) {
473 if (!strcmp(arg
, "-p")) {
474 recursive
= generate_patch
= 1;
477 if (!strncmp(arg
, "-S", 2)) {
481 if (!strncmp(arg
, "-M", 2)) {
482 detect_rename
= recursive
= generate_patch
= 1;
483 diff_score_opt
= diff_scoreopt_parse(arg
);
486 if (!strncmp(arg
, "-C", 2)) {
488 recursive
= generate_patch
= 1;
489 diff_score_opt
= diff_scoreopt_parse(arg
);
492 if (!strcmp(arg
, "-z")) {
493 line_termination
= '\0';
496 if (!strcmp(arg
, "-m")) {
500 if (!strcmp(arg
, "-s")) {
504 if (!strcmp(arg
, "-v")) {
506 header_prefix
= "diff-tree ";
509 if (!strcmp(arg
, "--stdin")) {
513 if (!strcmp(arg
, "--root")) {
517 usage(diff_tree_usage
);
525 pathlens
= xmalloc(nr_paths
* sizeof(int));
526 for (i
=0; i
<nr_paths
; i
++)
527 pathlens
[i
] = strlen(paths
[i
]);
533 usage(diff_tree_usage
);
536 diff_tree_commit(sha1
[0], NULL
);
539 diff_tree_sha1_top(sha1
[0], sha1
[1], "");
546 while (fgets(line
, sizeof(line
), stdin
))
547 diff_tree_stdin(line
);