6 static int ignore_merges = 1;
7 static int recursive = 0;
8 static int read_stdin = 0;
9 static int line_termination = '\n';
10 static int generate_patch = 0;
11 static const char *header = NULL;
13 // What paths are we interested in?
14 static int nr_paths = 0;
15 static char **paths = NULL;
16 static int *pathlens = NULL;
18 static int diff_tree_sha1(const unsigned char *old, const unsigned char *new, const char *base);
20 static void update_tree_entry(void **bufp, unsigned long *sizep)
23 unsigned long size = *sizep;
24 int len = strlen(buf) + 1 + 20;
27 die("corrupt tree file");
32 static const unsigned char *extract(void *tree, unsigned long size, const char **pathp, unsigned int *modep)
34 int len = strlen(tree)+1;
35 const unsigned char *sha1 = tree + len;
36 const char *path = strchr(tree, ' ');
38 if (!path || size < len + 20 || sscanf(tree, "%o", modep) != 1)
39 die("corrupt tree file");
44 static char *malloc_base(const char *base, const char *path, int pathlen)
46 int baselen = strlen(base);
47 char *newbase = xmalloc(baselen + pathlen + 2);
48 memcpy(newbase, base, baselen);
49 memcpy(newbase + baselen, path, pathlen);
50 memcpy(newbase + baselen + pathlen, "/", 2);
54 static void show_file(const char *prefix, void *tree, unsigned long size, const char *base);
56 /* A whole sub-tree went away or appeared */
57 static void show_tree(const char *prefix, void *tree, unsigned long size, const char *base)
60 show_file(prefix, tree, size, base);
61 update_tree_entry(&tree, &size);
65 /* A file entry went away or appeared */
66 static void show_file(const char *prefix, void *tree, unsigned long size, const char *base)
70 const unsigned char *sha1 = extract(tree, size, &path, &mode);
80 if (recursive && S_ISDIR(mode)) {
83 char *newbase = malloc_base(base, path, strlen(path));
86 tree = read_sha1_file(sha1, type, &size);
87 if (!tree || strcmp(type, "tree"))
88 die("corrupt tree sha %s", sha1_to_hex(sha1));
90 show_tree(prefix, tree, size, newbase);
99 diff_addremove(prefix[0], mode, sha1, base, path);
102 printf("%s%06o\t%s\t%s\t%s%s%c", prefix, mode,
103 S_ISDIR(mode) ? "tree" : "blob",
104 sha1_to_hex(sha1), base, path,
108 static int compare_tree_entry(void *tree1, unsigned long size1, void *tree2, unsigned long size2, const char *base)
110 unsigned mode1, mode2;
111 const char *path1, *path2;
112 const unsigned char *sha1, *sha2;
113 int cmp, pathlen1, pathlen2;
114 char old_sha1_hex[50];
116 sha1 = extract(tree1, size1, &path1, &mode1);
117 sha2 = extract(tree2, size2, &path2, &mode2);
119 pathlen1 = strlen(path1);
120 pathlen2 = strlen(path2);
121 cmp = cache_name_compare(path1, pathlen1, path2, pathlen2);
123 show_file("-", tree1, size1, base);
127 show_file("+", tree2, size2, base);
130 if (!memcmp(sha1, sha2, 20) && mode1 == mode2)
134 * If the filemode has changed to/from a directory from/to a regular
135 * file, we need to consider it a remove and an add.
137 if (S_ISDIR(mode1) != S_ISDIR(mode2)) {
138 show_file("-", tree1, size1, base);
139 show_file("+", tree2, size2, base);
143 if (recursive && S_ISDIR(mode1)) {
145 char *newbase = malloc_base(base, path1, pathlen1);
146 retval = diff_tree_sha1(sha1, sha2, newbase);
152 printf("%s", header);
158 if (generate_patch) {
160 diff_change(mode1, mode2, sha1, sha2, base, path1);
163 strcpy(old_sha1_hex, sha1_to_hex(sha1));
164 printf("*%06o->%06o\t%s\t%s->%s\t%s%s%c", mode1, mode2,
165 S_ISDIR(mode1) ? "tree" : "blob",
166 old_sha1_hex, sha1_to_hex(sha2), base, path1,
172 static int interesting(void *tree, unsigned long size, const char *base)
177 int baselen, pathlen;
182 (void)extract(tree, size, &path, &mode);
184 pathlen = strlen(path);
185 baselen = strlen(base);
187 for (i=0; i < nr_paths; i++) {
188 const char *match = paths[i];
189 int matchlen = pathlens[i];
191 if (baselen >= matchlen) {
192 /* If it doesn't match, move along... */
193 if (strncmp(base, match, matchlen))
196 /* The base is a subdirectory of a path which was specified. */
200 /* Does the base match? */
201 if (strncmp(base, match, baselen))
207 if (pathlen > matchlen)
210 if (strncmp(path, match, pathlen))
215 return 0; /* No matches */
218 static int diff_tree(void *tree1, unsigned long size1, void *tree2, unsigned long size2, const char *base)
220 while (size1 | size2) {
221 if (nr_paths && size1 && !interesting(tree1, size1, base)) {
222 update_tree_entry(&tree1, &size1);
225 if (nr_paths && size2 && !interesting(tree2, size2, base)) {
226 update_tree_entry(&tree2, &size2);
230 show_file("+", tree2, size2, base);
231 update_tree_entry(&tree2, &size2);
235 show_file("-", tree1, size1, base);
236 update_tree_entry(&tree1, &size1);
239 switch (compare_tree_entry(tree1, size1, tree2, size2, base)) {
241 update_tree_entry(&tree1, &size1);
244 update_tree_entry(&tree1, &size1);
247 update_tree_entry(&tree2, &size2);
250 die("diff-tree: internal error");
255 static int diff_tree_sha1(const unsigned char *old, const unsigned char *new, const char *base)
258 unsigned long size1, size2;
261 tree1 = read_object_with_reference(old, "tree", &size1, 0);
263 die("unable to read source tree (%s)", sha1_to_hex(old));
264 tree2 = read_object_with_reference(new, "tree", &size2, 0);
266 die("unable to read destination tree (%s)", sha1_to_hex(new));
267 retval = diff_tree(tree1, size1, tree2, size2, base);
273 static int diff_tree_stdin(char *line)
275 int len = strlen(line);
276 unsigned char commit[20], parent[20];
277 unsigned long size, offset;
278 static char this_header[100];
281 if (!len || line[len-1] != '\n')
284 if (get_sha1_hex(line, commit))
286 if (isspace(line[40]) && !get_sha1_hex(line+41, parent)) {
289 sprintf(this_header, "%s (from %s)\n", line, line+41);
290 header = this_header;
291 return diff_tree_sha1(parent, commit, "");
293 buf = read_object_with_reference(commit, "commit", &size, NULL);
297 /* More than one parent? */
299 if (!memcmp(buf + 46 + 48, "parent ", 7))
305 while (offset + 48 < size && !memcmp(buf + offset, "parent ", 7)) {
306 if (get_sha1_hex(buf + offset + 7, parent))
308 sprintf(this_header, "%s (from %s)\n", line, sha1_to_hex(parent));
309 header = this_header;
310 diff_tree_sha1(parent, commit, "");
316 static char *diff_tree_usage = "diff-tree [-p] [-r] [-z] <tree sha1> <tree sha1>";
318 int main(int argc, char **argv)
321 unsigned char old[20], new[20];
329 if (!arg || *arg != '-')
332 if (!strcmp(arg, "-")) {
337 if (!strcmp(arg, "-r")) {
341 if (!strcmp(arg, "-p")) {
342 recursive = generate_patch = 1;
345 if (!strcmp(arg, "-z")) {
346 line_termination = '\0';
349 if (!strcmp(arg, "-m")) {
353 if (!strcmp(arg, "-s")) {
357 if (!strcmp(arg, "--stdin")) {
361 usage(diff_tree_usage);
365 if (argc < 2 || get_sha1(argv[0], old) || get_sha1(argv[1], new))
366 usage(diff_tree_usage);
376 pathlens = xmalloc(nr_paths * sizeof(int));
377 for (i=0; i<nr_paths; i++)
378 pathlens[i] = strlen(paths[i]);
382 return diff_tree_sha1(old, new, "");
384 while (fgets(line, sizeof(line), stdin))
385 diff_tree_stdin(line);