3 static int recursive = 0;
4 static int line_termination = '\n';
6 // What paths are we interested in?
7 static int nr_paths = 0;
8 static char **paths = NULL;
9 static int *pathlens = NULL;
11 static int diff_tree_sha1(const unsigned char *old, const unsigned char *new, const char *base);
13 static void update_tree_entry(void **bufp, unsigned long *sizep)
16 unsigned long size = *sizep;
17 int len = strlen(buf) + 1 + 20;
20 die("corrupt tree file");
25 static const unsigned char *extract(void *tree, unsigned long size, const char **pathp, unsigned int *modep)
27 int len = strlen(tree)+1;
28 const unsigned char *sha1 = tree + len;
29 const char *path = strchr(tree, ' ');
31 if (!path || size < len + 20 || sscanf(tree, "%o", modep) != 1)
32 die("corrupt tree file");
37 static char *malloc_base(const char *base, const char *path, int pathlen)
39 int baselen = strlen(base);
40 char *newbase = malloc(baselen + pathlen + 2);
41 memcpy(newbase, base, baselen);
42 memcpy(newbase + baselen, path, pathlen);
43 memcpy(newbase + baselen + pathlen, "/", 2);
47 static void show_file(const char *prefix, void *tree, unsigned long size, const char *base);
49 /* A whole sub-tree went away or appeared */
50 static void show_tree(const char *prefix, void *tree, unsigned long size, const char *base)
53 show_file(prefix, tree, size, base);
54 update_tree_entry(&tree, &size);
58 /* A file entry went away or appeared */
59 static void show_file(const char *prefix, void *tree, unsigned long size, const char *base)
63 const unsigned char *sha1 = extract(tree, size, &path, &mode);
65 if (recursive && S_ISDIR(mode)) {
68 char *newbase = malloc_base(base, path, strlen(path));
71 tree = read_sha1_file(sha1, type, &size);
72 if (!tree || strcmp(type, "tree"))
73 die("corrupt tree sha %s", sha1_to_hex(sha1));
75 show_tree(prefix, tree, size, newbase);
82 printf("%s%o\t%s\t%s\t%s%s%c", prefix, mode,
83 S_ISDIR(mode) ? "tree" : "blob",
84 sha1_to_hex(sha1), base, path,
88 static int compare_tree_entry(void *tree1, unsigned long size1, void *tree2, unsigned long size2, const char *base)
90 unsigned mode1, mode2;
91 const char *path1, *path2;
92 const unsigned char *sha1, *sha2;
93 int cmp, pathlen1, pathlen2;
94 char old_sha1_hex[50];
96 sha1 = extract(tree1, size1, &path1, &mode1);
97 sha2 = extract(tree2, size2, &path2, &mode2);
99 pathlen1 = strlen(path1);
100 pathlen2 = strlen(path2);
101 cmp = cache_name_compare(path1, pathlen1, path2, pathlen2);
103 show_file("-", tree1, size1, base);
107 show_file("+", tree2, size2, base);
110 if (!memcmp(sha1, sha2, 20) && mode1 == mode2)
114 * If the filemode has changed to/from a directory from/to a regular
115 * file, we need to consider it a remove and an add.
117 if (S_ISDIR(mode1) != S_ISDIR(mode2)) {
118 show_file("-", tree1, size1, base);
119 show_file("+", tree2, size2, base);
123 if (recursive && S_ISDIR(mode1)) {
125 char *newbase = malloc_base(base, path1, pathlen1);
126 retval = diff_tree_sha1(sha1, sha2, newbase);
131 strcpy(old_sha1_hex, sha1_to_hex(sha1));
132 printf("*%o->%o\t%s\t%s->%s\t%s%s%c", mode1, mode2,
133 S_ISDIR(mode1) ? "tree" : "blob",
134 old_sha1_hex, sha1_to_hex(sha2), base, path1,
139 static int interesting(void *tree, unsigned long size, const char *base)
144 int baselen, pathlen;
149 (void)extract(tree, size, &path, &mode);
151 pathlen = strlen(path);
152 baselen = strlen(base);
154 for (i=0; i < nr_paths; i++) {
155 const char *match = paths[i];
156 int matchlen = pathlens[i];
158 if (baselen >= matchlen) {
159 /* If it doesn't match, move along... */
160 if (strncmp(base, match, matchlen))
163 /* The base is a subdirectory of a path which was specified. */
167 /* Does the base match? */
168 if (strncmp(base, match, baselen))
174 if (pathlen > matchlen)
177 if (strncmp(path, match, pathlen))
182 return 0; /* No matches */
185 static int diff_tree(void *tree1, unsigned long size1, void *tree2, unsigned long size2, const char *base)
187 while (size1 | size2) {
188 if (nr_paths && size1 && !interesting(tree1, size1, base)) {
189 update_tree_entry(&tree1, &size1);
192 if (nr_paths && size2 && !interesting(tree2, size2, base)) {
193 update_tree_entry(&tree2, &size2);
197 show_file("+", tree2, size2, base);
198 update_tree_entry(&tree2, &size2);
202 show_file("-", tree1, size1, base);
203 update_tree_entry(&tree1, &size1);
206 switch (compare_tree_entry(tree1, size1, tree2, size2, base)) {
208 update_tree_entry(&tree1, &size1);
211 update_tree_entry(&tree1, &size1);
214 update_tree_entry(&tree2, &size2);
217 die("diff-tree: internal error");
222 static int diff_tree_sha1(const unsigned char *old, const unsigned char *new, const char *base)
225 unsigned long size1, size2;
228 tree1 = read_tree_with_tree_or_commit_sha1(old, &size1, 0);
230 die("unable to read source tree (%s)", sha1_to_hex(old));
231 tree2 = read_tree_with_tree_or_commit_sha1(new, &size2, 0);
233 die("unable to read destination tree (%s)", sha1_to_hex(new));
234 retval = diff_tree(tree1, size1, tree2, size2, base);
240 static char *diff_tree_usage = "diff-tree [-r] [-z] <tree sha1> <tree sha1>";
242 int main(int argc, char **argv)
244 unsigned char old[20], new[20];
249 if (!arg || *arg != '-')
254 if (!strcmp(arg, "-r")) {
258 if (!strcmp(arg, "-z")) {
259 line_termination = '\0';
262 usage(diff_tree_usage);
265 if (argc < 3 || get_sha1_hex(argv[1], old) || get_sha1_hex(argv[2], new))
266 usage(diff_tree_usage);
273 pathlens = malloc(nr_paths * sizeof(int));
274 for (i=0; i<nr_paths; i++)
275 pathlens[i] = strlen(paths[i]);
278 return diff_tree_sha1(old, new, "");