6 static int verbose_header
= 0;
7 static int ignore_merges
= 1;
8 static int recursive
= 0;
9 static int read_stdin
= 0;
10 static int line_termination
= '\n';
11 static int generate_patch
= 0;
12 static int detect_rename
= 0;
13 static const char *header
= NULL
;
14 static const char *header_prefix
= "";
16 // What paths are we interested in?
17 static int nr_paths
= 0;
18 static char **paths
= NULL
;
19 static int *pathlens
= NULL
;
21 static int diff_tree_sha1(const unsigned char *old
, const unsigned char *new, const char *base
);
23 static void update_tree_entry(void **bufp
, unsigned long *sizep
)
26 unsigned long size
= *sizep
;
27 int len
= strlen(buf
) + 1 + 20;
30 die("corrupt tree file");
35 static const unsigned char *extract(void *tree
, unsigned long size
, const char **pathp
, unsigned int *modep
)
37 int len
= strlen(tree
)+1;
38 const unsigned char *sha1
= tree
+ len
;
39 const char *path
= strchr(tree
, ' ');
41 if (!path
|| size
< len
+ 20 || sscanf(tree
, "%o", modep
) != 1)
42 die("corrupt tree file");
47 static char *malloc_base(const char *base
, const char *path
, int pathlen
)
49 int baselen
= strlen(base
);
50 char *newbase
= xmalloc(baselen
+ pathlen
+ 2);
51 memcpy(newbase
, base
, baselen
);
52 memcpy(newbase
+ baselen
, path
, pathlen
);
53 memcpy(newbase
+ baselen
+ pathlen
, "/", 2);
57 static void show_file(const char *prefix
, void *tree
, unsigned long size
, const char *base
);
58 static void show_tree(const char *prefix
, void *tree
, unsigned long size
, const char *base
);
60 /* A file entry went away or appeared */
61 static void show_file(const char *prefix
, void *tree
, unsigned long size
, const char *base
)
65 const unsigned char *sha1
= extract(tree
, size
, &path
, &mode
);
75 if (recursive
&& S_ISDIR(mode
)) {
78 char *newbase
= malloc_base(base
, path
, strlen(path
));
81 tree
= read_sha1_file(sha1
, type
, &size
);
82 if (!tree
|| strcmp(type
, "tree"))
83 die("corrupt tree sha %s", sha1_to_hex(sha1
));
85 show_tree(prefix
, tree
, size
, newbase
);
94 diff_addremove(prefix
[0], mode
, sha1
, base
, path
);
97 printf("%s%06o\t%s\t%s\t%s%s%c", prefix
, mode
,
98 S_ISDIR(mode
) ? "tree" : "blob",
99 sha1_to_hex(sha1
), base
, path
,
103 static int compare_tree_entry(void *tree1
, unsigned long size1
, void *tree2
, unsigned long size2
, const char *base
)
105 unsigned mode1
, mode2
;
106 const char *path1
, *path2
;
107 const unsigned char *sha1
, *sha2
;
108 int cmp
, pathlen1
, pathlen2
;
109 char old_sha1_hex
[50];
111 sha1
= extract(tree1
, size1
, &path1
, &mode1
);
112 sha2
= extract(tree2
, size2
, &path2
, &mode2
);
114 pathlen1
= strlen(path1
);
115 pathlen2
= strlen(path2
);
116 cmp
= cache_name_compare(path1
, pathlen1
, path2
, pathlen2
);
118 show_file("-", tree1
, size1
, base
);
122 show_file("+", tree2
, size2
, base
);
125 if (!memcmp(sha1
, sha2
, 20) && mode1
== mode2
)
129 * If the filemode has changed to/from a directory from/to a regular
130 * file, we need to consider it a remove and an add.
132 if (S_ISDIR(mode1
) != S_ISDIR(mode2
)) {
133 show_file("-", tree1
, size1
, base
);
134 show_file("+", tree2
, size2
, base
);
138 if (recursive
&& S_ISDIR(mode1
)) {
140 char *newbase
= malloc_base(base
, path1
, pathlen1
);
141 retval
= diff_tree_sha1(sha1
, sha2
, newbase
);
147 printf("%s", header
);
153 if (generate_patch
) {
155 diff_change(mode1
, mode2
, sha1
, sha2
, base
, path1
);
158 strcpy(old_sha1_hex
, sha1_to_hex(sha1
));
159 printf("*%06o->%06o\t%s\t%s->%s\t%s%s%c", mode1
, mode2
,
160 S_ISDIR(mode1
) ? "tree" : "blob",
161 old_sha1_hex
, sha1_to_hex(sha2
), base
, path1
,
167 static int interesting(void *tree
, unsigned long size
, const char *base
)
172 int baselen
, pathlen
;
177 (void)extract(tree
, size
, &path
, &mode
);
179 pathlen
= strlen(path
);
180 baselen
= strlen(base
);
182 for (i
=0; i
< nr_paths
; i
++) {
183 const char *match
= paths
[i
];
184 int matchlen
= pathlens
[i
];
186 if (baselen
>= matchlen
) {
187 /* If it doesn't match, move along... */
188 if (strncmp(base
, match
, matchlen
))
191 /* The base is a subdirectory of a path which was specified. */
195 /* Does the base match? */
196 if (strncmp(base
, match
, baselen
))
202 if (pathlen
> matchlen
)
205 if (matchlen
> pathlen
) {
206 if (match
[pathlen
] != '/')
212 if (strncmp(path
, match
, pathlen
))
217 return 0; /* No matches */
220 /* A whole sub-tree went away or appeared */
221 static void show_tree(const char *prefix
, void *tree
, unsigned long size
, const char *base
)
224 if (interesting(tree
, size
, base
))
225 show_file(prefix
, tree
, size
, base
);
226 update_tree_entry(&tree
, &size
);
230 static int diff_tree(void *tree1
, unsigned long size1
, void *tree2
, unsigned long size2
, const char *base
)
232 while (size1
| size2
) {
233 if (nr_paths
&& size1
&& !interesting(tree1
, size1
, base
)) {
234 update_tree_entry(&tree1
, &size1
);
237 if (nr_paths
&& size2
&& !interesting(tree2
, size2
, base
)) {
238 update_tree_entry(&tree2
, &size2
);
242 show_file("+", tree2
, size2
, base
);
243 update_tree_entry(&tree2
, &size2
);
247 show_file("-", tree1
, size1
, base
);
248 update_tree_entry(&tree1
, &size1
);
251 switch (compare_tree_entry(tree1
, size1
, tree2
, size2
, base
)) {
253 update_tree_entry(&tree1
, &size1
);
256 update_tree_entry(&tree1
, &size1
);
259 update_tree_entry(&tree2
, &size2
);
262 die("diff-tree: internal error");
267 static int diff_tree_sha1(const unsigned char *old
, const unsigned char *new, const char *base
)
270 unsigned long size1
, size2
;
273 tree1
= read_object_with_reference(old
, "tree", &size1
, 0);
275 die("unable to read source tree (%s)", sha1_to_hex(old
));
276 tree2
= read_object_with_reference(new, "tree", &size2
, 0);
278 die("unable to read destination tree (%s)", sha1_to_hex(new));
279 retval
= diff_tree(tree1
, size1
, tree2
, size2
, base
);
285 static int diff_tree_sha1_top(const unsigned char *old
,
286 const unsigned char *new, const char *base
)
290 diff_setup(detect_rename
, 0, 0, 0, 0);
291 ret
= diff_tree_sha1(old
, new, base
);
297 static int get_one_line(const char *msg
, unsigned long len
)
309 static int add_author_info(char *buf
, const char *line
, int len
)
312 unsigned int namelen
;
316 line
+= strlen("author ");
317 date
= strchr(line
, '>');
320 namelen
= ++date
- line
;
321 time
= strtoul(date
, &date
, 10);
322 tz
= strtol(date
, NULL
, 10);
324 return sprintf(buf
, "Author: %.*s\nDate: %s\n",
326 show_date(time
, tz
));
329 static char *generate_header(const char *commit
, const char *parent
, const char *msg
, unsigned long len
)
331 static char this_header
[1000];
334 offset
= sprintf(this_header
, "%s%s (from %s)\n", header_prefix
, commit
, parent
);
335 if (verbose_header
) {
339 const char *line
= msg
;
340 int linelen
= get_one_line(msg
, len
);
344 if (offset
+ linelen
+ 10 > sizeof(this_header
))
352 if (!memcmp(line
, "author ", 7))
353 offset
+= add_author_info(this_header
+ offset
, line
, linelen
);
356 memset(this_header
+ offset
, ' ', 4);
357 memcpy(this_header
+ offset
+ 4, line
, linelen
);
358 offset
+= linelen
+ 4;
360 this_header
[offset
++] = '\n';
361 this_header
[offset
] = 0;
367 static int diff_tree_commit(const unsigned char *commit
, const char *name
)
369 unsigned long size
, offset
;
370 char *buf
= read_object_with_reference(commit
, "commit", &size
, NULL
);
375 /* More than one parent? */
377 if (!memcmp(buf
+ 46 + 48, "parent ", 7))
382 static char commit_name
[60];
383 strcpy(commit_name
, sha1_to_hex(commit
));
388 while (offset
+ 48 < size
&& !memcmp(buf
+ offset
, "parent ", 7)) {
389 unsigned char parent
[20];
390 if (get_sha1_hex(buf
+ offset
+ 7, parent
))
392 header
= generate_header(name
, sha1_to_hex(parent
), buf
, size
);
393 diff_tree_sha1_top(parent
, commit
, "");
394 if (!header
&& verbose_header
)
395 header_prefix
= "\ndiff-tree ";
401 static int diff_tree_stdin(char *line
)
403 int len
= strlen(line
);
404 unsigned char commit
[20], parent
[20];
405 static char this_header
[1000];
407 if (!len
|| line
[len
-1] != '\n')
410 if (get_sha1_hex(line
, commit
))
412 if (isspace(line
[40]) && !get_sha1_hex(line
+41, parent
)) {
415 sprintf(this_header
, "%s (from %s)\n", line
, line
+41);
416 header
= this_header
;
417 return diff_tree_sha1_top(parent
, commit
, "");
420 return diff_tree_commit(commit
, line
);
423 static char *diff_tree_usage
=
424 "diff-tree [-p] [-r] [-z] [--stdin] [-M] [-m] [-s] [-v] <tree-ish> <tree-ish>";
426 int main(int argc
, char **argv
)
430 unsigned char sha1
[2][20];
443 if (nr_sha1
< 2 && !get_sha1(arg
, sha1
[nr_sha1
])) {
450 if (!strcmp(arg
, "--")) {
455 if (!strcmp(arg
, "-r")) {
459 if (!strcmp(arg
, "-p")) {
460 recursive
= generate_patch
= 1;
463 if (!strcmp(arg
, "-M")) {
464 detect_rename
= recursive
= generate_patch
= 1;
467 if (!strcmp(arg
, "-z")) {
468 line_termination
= '\0';
471 if (!strcmp(arg
, "-m")) {
475 if (!strcmp(arg
, "-s")) {
479 if (!strcmp(arg
, "-v")) {
481 header_prefix
= "diff-tree ";
484 if (!strcmp(arg
, "--stdin")) {
488 usage(diff_tree_usage
);
496 pathlens
= xmalloc(nr_paths
* sizeof(int));
497 for (i
=0; i
<nr_paths
; i
++)
498 pathlens
[i
] = strlen(paths
[i
]);
504 usage(diff_tree_usage
);
507 diff_tree_commit(sha1
[0], NULL
);
510 diff_tree_sha1_top(sha1
[0], sha1
[1], "");
517 while (fgets(line
, sizeof(line
), stdin
))
518 diff_tree_stdin(line
);