3 static int recursive = 0;
4 static int line_termination = '\n';
6 static int diff_tree_sha1(const unsigned char *old, const unsigned char *new, const char *base);
8 static void update_tree_entry(void **bufp, unsigned long *sizep)
11 unsigned long size = *sizep;
12 int len = strlen(buf) + 1 + 20;
15 die("corrupt tree file");
20 static const unsigned char *extract(void *tree, unsigned long size, const char **pathp, unsigned int *modep)
22 int len = strlen(tree)+1;
23 const unsigned char *sha1 = tree + len;
24 const char *path = strchr(tree, ' ');
26 if (!path || size < len + 20 || sscanf(tree, "%o", modep) != 1)
27 die("corrupt tree file");
32 static char *malloc_base(const char *base, const char *path, int pathlen)
34 int baselen = strlen(base);
35 char *newbase = malloc(baselen + pathlen + 2);
36 memcpy(newbase, base, baselen);
37 memcpy(newbase + baselen, path, pathlen);
38 memcpy(newbase + baselen + pathlen, "/", 2);
42 static void show_file(const char *prefix, void *tree, unsigned long size, const char *base);
44 /* A whole sub-tree went away or appeared */
45 static void show_tree(const char *prefix, void *tree, unsigned long size, const char *base)
48 show_file(prefix, tree, size, base);
49 update_tree_entry(&tree, &size);
53 /* A file entry went away or appeared */
54 static void show_file(const char *prefix, void *tree, unsigned long size, const char *base)
58 const unsigned char *sha1 = extract(tree, size, &path, &mode);
60 if (recursive && S_ISDIR(mode)) {
63 char *newbase = malloc_base(base, path, strlen(path));
66 tree = read_sha1_file(sha1, type, &size);
67 if (!tree || strcmp(type, "tree"))
68 die("corrupt tree sha %s", sha1_to_hex(sha1));
70 show_tree(prefix, tree, size, newbase);
77 printf("%s%o\t%s\t%s\t%s%s%c", prefix, mode,
78 S_ISDIR(mode) ? "tree" : "blob",
79 sha1_to_hex(sha1), base, path,
83 static int compare_tree_entry(void *tree1, unsigned long size1, void *tree2, unsigned long size2, const char *base)
85 unsigned mode1, mode2;
86 const char *path1, *path2;
87 const unsigned char *sha1, *sha2;
88 int cmp, pathlen1, pathlen2;
89 char old_sha1_hex[50];
91 sha1 = extract(tree1, size1, &path1, &mode1);
92 sha2 = extract(tree2, size2, &path2, &mode2);
94 pathlen1 = strlen(path1);
95 pathlen2 = strlen(path2);
96 cmp = cache_name_compare(path1, pathlen1, path2, pathlen2);
98 show_file("-", tree1, size1, base);
102 show_file("+", tree2, size2, base);
105 if (!memcmp(sha1, sha2, 20) && mode1 == mode2)
109 * If the filemode has changed to/from a directory from/to a regular
110 * file, we need to consider it a remove and an add.
112 if (S_ISDIR(mode1) != S_ISDIR(mode2)) {
113 show_file("-", tree1, size1, base);
114 show_file("+", tree2, size2, base);
118 if (recursive && S_ISDIR(mode1)) {
120 char *newbase = malloc_base(base, path1, pathlen1);
121 retval = diff_tree_sha1(sha1, sha2, newbase);
126 strcpy(old_sha1_hex, sha1_to_hex(sha1));
127 printf("*%o->%o\t%s\t%s->%s\t%s%s%c", mode1, mode2,
128 S_ISDIR(mode1) ? "tree" : "blob",
129 old_sha1_hex, sha1_to_hex(sha2), base, path1,
134 static int diff_tree(void *tree1, unsigned long size1, void *tree2, unsigned long size2, const char *base)
136 while (size1 | size2) {
138 show_file("+", tree2, size2, base);
139 update_tree_entry(&tree2, &size2);
143 show_file("-", tree1, size1, base);
144 update_tree_entry(&tree1, &size1);
147 switch (compare_tree_entry(tree1, size1, tree2, size2, base)) {
149 update_tree_entry(&tree1, &size1);
152 update_tree_entry(&tree1, &size1);
155 update_tree_entry(&tree2, &size2);
158 die("diff-tree: internal error");
163 static int diff_tree_sha1(const unsigned char *old, const unsigned char *new, const char *base)
166 unsigned long size1, size2;
170 tree1 = read_sha1_file(old, type, &size1);
171 if (!tree1 || strcmp(type, "tree"))
172 die("unable to read source tree (%s)", sha1_to_hex(old));
173 tree2 = read_sha1_file(new, type, &size2);
174 if (!tree2 || strcmp(type, "tree"))
175 die("unable to read destination tree (%s)", sha1_to_hex(new));
176 retval = diff_tree(tree1, size1, tree2, size2, base);
182 static void commit_to_tree(unsigned char *sha1)
188 buf = read_sha1_file(sha1, type, &size);
190 if (!strcmp(type, "commit"))
191 get_sha1_hex(buf+5, sha1);
196 int main(int argc, char **argv)
198 unsigned char old[20], new[20];
204 if (!strcmp(arg, "-r")) {
208 if (!strcmp(arg, "-z")) {
209 line_termination = '\0';
212 usage("diff-tree [-r] [-z] <tree sha1> <tree sha1>");
215 if (argc != 3 || get_sha1_hex(argv[1], old) || get_sha1_hex(argv[2], new))
216 usage("diff-tree <tree sha1> <tree sha1>");
219 return diff_tree_sha1(old, new, "");