4 static int recursive
= 0;
5 static int line_termination
= '\n';
6 static int generate_patch
= 0;
8 // What paths are we interested in?
9 static int nr_paths
= 0;
10 static char **paths
= NULL
;
11 static int *pathlens
= NULL
;
13 static int diff_tree_sha1(const unsigned char *old
, const unsigned char *new, const char *base
);
15 static void update_tree_entry(void **bufp
, unsigned long *sizep
)
18 unsigned long size
= *sizep
;
19 int len
= strlen(buf
) + 1 + 20;
22 die("corrupt tree file");
27 static const unsigned char *extract(void *tree
, unsigned long size
, const char **pathp
, unsigned int *modep
)
29 int len
= strlen(tree
)+1;
30 const unsigned char *sha1
= tree
+ len
;
31 const char *path
= strchr(tree
, ' ');
33 if (!path
|| size
< len
+ 20 || sscanf(tree
, "%o", modep
) != 1)
34 die("corrupt tree file");
39 static char *malloc_base(const char *base
, const char *path
, int pathlen
)
41 int baselen
= strlen(base
);
42 char *newbase
= xmalloc(baselen
+ pathlen
+ 2);
43 memcpy(newbase
, base
, baselen
);
44 memcpy(newbase
+ baselen
, path
, pathlen
);
45 memcpy(newbase
+ baselen
+ pathlen
, "/", 2);
49 static void show_file(const char *prefix
, void *tree
, unsigned long size
, const char *base
);
51 /* A whole sub-tree went away or appeared */
52 static void show_tree(const char *prefix
, void *tree
, unsigned long size
, const char *base
)
55 show_file(prefix
, tree
, size
, base
);
56 update_tree_entry(&tree
, &size
);
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
);
67 if (recursive
&& S_ISDIR(mode
)) {
70 char *newbase
= malloc_base(base
, path
, strlen(path
));
73 tree
= read_sha1_file(sha1
, type
, &size
);
74 if (!tree
|| strcmp(type
, "tree"))
75 die("corrupt tree sha %s", sha1_to_hex(sha1
));
77 show_tree(prefix
, tree
, size
, newbase
);
86 diff_addremove(prefix
[0], mode
, sha1
, base
, path
);
89 printf("%s%06o\t%s\t%s\t%s%s%c", prefix
, mode
,
90 S_ISDIR(mode
) ? "tree" : "blob",
91 sha1_to_hex(sha1
), base
, path
,
95 static int compare_tree_entry(void *tree1
, unsigned long size1
, void *tree2
, unsigned long size2
, const char *base
)
97 unsigned mode1
, mode2
;
98 const char *path1
, *path2
;
99 const unsigned char *sha1
, *sha2
;
100 int cmp
, pathlen1
, pathlen2
;
101 char old_sha1_hex
[50];
103 sha1
= extract(tree1
, size1
, &path1
, &mode1
);
104 sha2
= extract(tree2
, size2
, &path2
, &mode2
);
106 pathlen1
= strlen(path1
);
107 pathlen2
= strlen(path2
);
108 cmp
= cache_name_compare(path1
, pathlen1
, path2
, pathlen2
);
110 show_file("-", tree1
, size1
, base
);
114 show_file("+", tree2
, size2
, base
);
117 if (!memcmp(sha1
, sha2
, 20) && mode1
== mode2
)
121 * If the filemode has changed to/from a directory from/to a regular
122 * file, we need to consider it a remove and an add.
124 if (S_ISDIR(mode1
) != S_ISDIR(mode2
)) {
125 show_file("-", tree1
, size1
, base
);
126 show_file("+", tree2
, size2
, base
);
130 if (recursive
&& S_ISDIR(mode1
)) {
132 char *newbase
= malloc_base(base
, path1
, pathlen1
);
133 retval
= diff_tree_sha1(sha1
, sha2
, newbase
);
138 if (generate_patch
) {
140 diff_change(mode1
, mode2
, sha1
, sha2
, base
, path1
);
143 strcpy(old_sha1_hex
, sha1_to_hex(sha1
));
144 printf("*%06o->%06o\t%s\t%s->%s\t%s%s%c", mode1
, mode2
,
145 S_ISDIR(mode1
) ? "tree" : "blob",
146 old_sha1_hex
, sha1_to_hex(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 (strncmp(path
, match
, pathlen
))
195 return 0; /* No matches */
198 static int diff_tree(void *tree1
, unsigned long size1
, void *tree2
, unsigned long size2
, const char *base
)
200 while (size1
| size2
) {
201 if (nr_paths
&& size1
&& !interesting(tree1
, size1
, base
)) {
202 update_tree_entry(&tree1
, &size1
);
205 if (nr_paths
&& size2
&& !interesting(tree2
, size2
, base
)) {
206 update_tree_entry(&tree2
, &size2
);
210 show_file("+", tree2
, size2
, base
);
211 update_tree_entry(&tree2
, &size2
);
215 show_file("-", tree1
, size1
, base
);
216 update_tree_entry(&tree1
, &size1
);
219 switch (compare_tree_entry(tree1
, size1
, tree2
, size2
, base
)) {
221 update_tree_entry(&tree1
, &size1
);
224 update_tree_entry(&tree1
, &size1
);
227 update_tree_entry(&tree2
, &size2
);
230 die("diff-tree: internal error");
235 static int diff_tree_sha1(const unsigned char *old
, const unsigned char *new, const char *base
)
238 unsigned long size1
, size2
;
241 tree1
= read_object_with_reference(old
, "tree", &size1
, 0);
243 die("unable to read source tree (%s)", sha1_to_hex(old
));
244 tree2
= read_object_with_reference(new, "tree", &size2
, 0);
246 die("unable to read destination tree (%s)", sha1_to_hex(new));
247 retval
= diff_tree(tree1
, size1
, tree2
, size2
, base
);
253 static char *diff_tree_usage
= "diff-tree [-p] [-r] [-z] <tree sha1> <tree sha1>";
255 int main(int argc
, char **argv
)
257 unsigned char old
[20], new[20];
262 if (!arg
|| *arg
!= '-')
267 if (!strcmp(arg
, "-r")) {
271 if (!strcmp(arg
, "-p")) {
272 recursive
= generate_patch
= 1;
275 if (!strcmp(arg
, "-z")) {
276 line_termination
= '\0';
279 usage(diff_tree_usage
);
282 if (argc
< 3 || get_sha1(argv
[1], old
) || get_sha1(argv
[2], new))
283 usage(diff_tree_usage
);
290 pathlens
= xmalloc(nr_paths
* sizeof(int));
291 for (i
=0; i
<nr_paths
; i
++)
292 pathlens
[i
] = strlen(paths
[i
]);
295 return diff_tree_sha1(old
, new, "");