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 const char *header
= NULL
;
13 static const char *header_prefix
= "";
15 // What paths are we interested in?
16 static int nr_paths
= 0;
17 static char **paths
= NULL
;
18 static int *pathlens
= NULL
;
20 static int diff_tree_sha1(const unsigned char *old
, const unsigned char *new, const char *base
);
22 static void update_tree_entry(void **bufp
, unsigned long *sizep
)
25 unsigned long size
= *sizep
;
26 int len
= strlen(buf
) + 1 + 20;
29 die("corrupt tree file");
34 static const unsigned char *extract(void *tree
, unsigned long size
, const char **pathp
, unsigned int *modep
)
36 int len
= strlen(tree
)+1;
37 const unsigned char *sha1
= tree
+ len
;
38 const char *path
= strchr(tree
, ' ');
40 if (!path
|| size
< len
+ 20 || sscanf(tree
, "%o", modep
) != 1)
41 die("corrupt tree file");
46 static char *malloc_base(const char *base
, const char *path
, int pathlen
)
48 int baselen
= strlen(base
);
49 char *newbase
= xmalloc(baselen
+ pathlen
+ 2);
50 memcpy(newbase
, base
, baselen
);
51 memcpy(newbase
+ baselen
, path
, pathlen
);
52 memcpy(newbase
+ baselen
+ pathlen
, "/", 2);
56 static void show_file(const char *prefix
, void *tree
, unsigned long size
, const char *base
);
58 /* A whole sub-tree went away or appeared */
59 static void show_tree(const char *prefix
, void *tree
, unsigned long size
, const char *base
)
62 show_file(prefix
, tree
, size
, base
);
63 update_tree_entry(&tree
, &size
);
67 /* A file entry went away or appeared */
68 static void show_file(const char *prefix
, void *tree
, unsigned long size
, const char *base
)
72 const unsigned char *sha1
= extract(tree
, size
, &path
, &mode
);
82 if (recursive
&& S_ISDIR(mode
)) {
85 char *newbase
= malloc_base(base
, path
, strlen(path
));
88 tree
= read_sha1_file(sha1
, type
, &size
);
89 if (!tree
|| strcmp(type
, "tree"))
90 die("corrupt tree sha %s", sha1_to_hex(sha1
));
92 show_tree(prefix
, tree
, size
, newbase
);
101 diff_addremove(prefix
[0], mode
, sha1
, base
, path
);
104 printf("%s%06o\t%s\t%s\t%s%s%c", prefix
, mode
,
105 S_ISDIR(mode
) ? "tree" : "blob",
106 sha1_to_hex(sha1
), base
, path
,
110 static int compare_tree_entry(void *tree1
, unsigned long size1
, void *tree2
, unsigned long size2
, const char *base
)
112 unsigned mode1
, mode2
;
113 const char *path1
, *path2
;
114 const unsigned char *sha1
, *sha2
;
115 int cmp
, pathlen1
, pathlen2
;
116 char old_sha1_hex
[50];
118 sha1
= extract(tree1
, size1
, &path1
, &mode1
);
119 sha2
= extract(tree2
, size2
, &path2
, &mode2
);
121 pathlen1
= strlen(path1
);
122 pathlen2
= strlen(path2
);
123 cmp
= cache_name_compare(path1
, pathlen1
, path2
, pathlen2
);
125 show_file("-", tree1
, size1
, base
);
129 show_file("+", tree2
, size2
, base
);
132 if (!memcmp(sha1
, sha2
, 20) && mode1
== mode2
)
136 * If the filemode has changed to/from a directory from/to a regular
137 * file, we need to consider it a remove and an add.
139 if (S_ISDIR(mode1
) != S_ISDIR(mode2
)) {
140 show_file("-", tree1
, size1
, base
);
141 show_file("+", tree2
, size2
, base
);
145 if (recursive
&& S_ISDIR(mode1
)) {
147 char *newbase
= malloc_base(base
, path1
, pathlen1
);
148 retval
= diff_tree_sha1(sha1
, sha2
, newbase
);
154 printf("%s", header
);
160 if (generate_patch
) {
162 diff_change(mode1
, mode2
, sha1
, sha2
, base
, path1
);
165 strcpy(old_sha1_hex
, sha1_to_hex(sha1
));
166 printf("*%06o->%06o\t%s\t%s->%s\t%s%s%c", mode1
, mode2
,
167 S_ISDIR(mode1
) ? "tree" : "blob",
168 old_sha1_hex
, sha1_to_hex(sha2
), base
, path1
,
174 static int interesting(void *tree
, unsigned long size
, const char *base
)
179 int baselen
, pathlen
;
184 (void)extract(tree
, size
, &path
, &mode
);
186 pathlen
= strlen(path
);
187 baselen
= strlen(base
);
189 for (i
=0; i
< nr_paths
; i
++) {
190 const char *match
= paths
[i
];
191 int matchlen
= pathlens
[i
];
193 if (baselen
>= matchlen
) {
194 /* If it doesn't match, move along... */
195 if (strncmp(base
, match
, matchlen
))
198 /* The base is a subdirectory of a path which was specified. */
202 /* Does the base match? */
203 if (strncmp(base
, match
, baselen
))
209 if (pathlen
> matchlen
)
212 if (strncmp(path
, match
, pathlen
))
217 return 0; /* No matches */
220 static int diff_tree(void *tree1
, unsigned long size1
, void *tree2
, unsigned long size2
, const char *base
)
222 while (size1
| size2
) {
223 if (nr_paths
&& size1
&& !interesting(tree1
, size1
, base
)) {
224 update_tree_entry(&tree1
, &size1
);
227 if (nr_paths
&& size2
&& !interesting(tree2
, size2
, base
)) {
228 update_tree_entry(&tree2
, &size2
);
232 show_file("+", tree2
, size2
, base
);
233 update_tree_entry(&tree2
, &size2
);
237 show_file("-", tree1
, size1
, base
);
238 update_tree_entry(&tree1
, &size1
);
241 switch (compare_tree_entry(tree1
, size1
, tree2
, size2
, base
)) {
243 update_tree_entry(&tree1
, &size1
);
246 update_tree_entry(&tree1
, &size1
);
249 update_tree_entry(&tree2
, &size2
);
252 die("diff-tree: internal error");
257 static int diff_tree_sha1(const unsigned char *old
, const unsigned char *new, const char *base
)
260 unsigned long size1
, size2
;
263 tree1
= read_object_with_reference(old
, "tree", &size1
, 0);
265 die("unable to read source tree (%s)", sha1_to_hex(old
));
266 tree2
= read_object_with_reference(new, "tree", &size2
, 0);
268 die("unable to read destination tree (%s)", sha1_to_hex(new));
269 retval
= diff_tree(tree1
, size1
, tree2
, size2
, base
);
275 static int get_one_line(const char *msg
, unsigned long len
)
287 static char *generate_header(const char *commit
, const char *parent
, const char *msg
, unsigned long len
)
289 static char this_header
[1000];
292 offset
= sprintf(this_header
, "%s%s (from %s)\n", header_prefix
, commit
, parent
);
293 if (verbose_header
) {
297 const char *line
= msg
;
298 int linelen
= get_one_line(msg
, len
);
302 if (offset
+ linelen
+ 10 > sizeof(this_header
))
311 memset(this_header
+ offset
, ' ', 4);
312 memcpy(this_header
+ offset
+ 4, line
, linelen
);
313 offset
+= linelen
+ 4;
315 this_header
[offset
++] = '\n';
316 this_header
[offset
] = 0;
322 static int diff_tree_stdin(char *line
)
324 int len
= strlen(line
);
325 unsigned char commit
[20], parent
[20];
326 unsigned long size
, offset
;
327 static char this_header
[1000];
330 if (!len
|| line
[len
-1] != '\n')
333 if (get_sha1_hex(line
, commit
))
335 if (isspace(line
[40]) && !get_sha1_hex(line
+41, parent
)) {
338 sprintf(this_header
, "%s (from %s)\n", line
, line
+41);
339 header
= this_header
;
340 return diff_tree_sha1(parent
, commit
, "");
342 buf
= read_object_with_reference(commit
, "commit", &size
, NULL
);
346 /* More than one parent? */
348 if (!memcmp(buf
+ 46 + 48, "parent ", 7))
354 while (offset
+ 48 < size
&& !memcmp(buf
+ offset
, "parent ", 7)) {
355 if (get_sha1_hex(buf
+ offset
+ 7, parent
))
357 header
= generate_header(line
, sha1_to_hex(parent
), buf
, size
);
358 diff_tree_sha1(parent
, commit
, "");
359 if (!header
&& verbose_header
)
360 header_prefix
= "\ndiff-tree ";
366 static char *diff_tree_usage
= "diff-tree [-p] [-r] [-z] <tree sha1> <tree sha1>";
368 int main(int argc
, char **argv
)
371 unsigned char old
[20], new[20];
379 if (!arg
|| *arg
!= '-')
382 if (!strcmp(arg
, "-")) {
387 if (!strcmp(arg
, "-r")) {
391 if (!strcmp(arg
, "-p")) {
392 recursive
= generate_patch
= 1;
395 if (!strcmp(arg
, "-z")) {
396 line_termination
= '\0';
399 if (!strcmp(arg
, "-m")) {
403 if (!strcmp(arg
, "-s")) {
407 if (!strcmp(arg
, "-v")) {
409 header_prefix
= "diff-tree ";
412 if (!strcmp(arg
, "--stdin")) {
416 usage(diff_tree_usage
);
420 if (argc
< 2 || get_sha1(argv
[0], old
) || get_sha1(argv
[1], new))
421 usage(diff_tree_usage
);
431 pathlens
= xmalloc(nr_paths
* sizeof(int));
432 for (i
=0; i
<nr_paths
; i
++)
433 pathlens
[i
] = strlen(paths
[i
]);
437 return diff_tree_sha1(old
, new, "");
439 while (fgets(line
, sizeof(line
), stdin
))
440 diff_tree_stdin(line
);