5 static int ignore_merges = 1;
6 static int recursive = 0;
7 static int read_stdin = 0;
8 static int line_termination = '\n';
9 static int generate_patch = 0;
11 // What paths are we interested in?
12 static int nr_paths = 0;
13 static char **paths = NULL;
14 static int *pathlens = NULL;
16 static int diff_tree_sha1(const unsigned char *old, const unsigned char *new, const char *base);
18 static void update_tree_entry(void **bufp, unsigned long *sizep)
21 unsigned long size = *sizep;
22 int len = strlen(buf) + 1 + 20;
25 die("corrupt tree file");
30 static const unsigned char *extract(void *tree, unsigned long size, const char **pathp, unsigned int *modep)
32 int len = strlen(tree)+1;
33 const unsigned char *sha1 = tree + len;
34 const char *path = strchr(tree, ' ');
36 if (!path || size < len + 20 || sscanf(tree, "%o", modep) != 1)
37 die("corrupt tree file");
42 static char *malloc_base(const char *base, const char *path, int pathlen)
44 int baselen = strlen(base);
45 char *newbase = xmalloc(baselen + pathlen + 2);
46 memcpy(newbase, base, baselen);
47 memcpy(newbase + baselen, path, pathlen);
48 memcpy(newbase + baselen + pathlen, "/", 2);
52 static void show_file(const char *prefix, void *tree, unsigned long size, const char *base);
54 /* A whole sub-tree went away or appeared */
55 static void show_tree(const char *prefix, void *tree, unsigned long size, const char *base)
58 show_file(prefix, tree, size, base);
59 update_tree_entry(&tree, &size);
63 /* A file entry went away or appeared */
64 static void show_file(const char *prefix, void *tree, unsigned long size, const char *base)
68 const unsigned char *sha1 = extract(tree, size, &path, &mode);
70 if (recursive && S_ISDIR(mode)) {
73 char *newbase = malloc_base(base, path, strlen(path));
76 tree = read_sha1_file(sha1, type, &size);
77 if (!tree || strcmp(type, "tree"))
78 die("corrupt tree sha %s", sha1_to_hex(sha1));
80 show_tree(prefix, tree, size, newbase);
89 diff_addremove(prefix[0], mode, sha1, base, path);
92 printf("%s%06o\t%s\t%s\t%s%s%c", prefix, mode,
93 S_ISDIR(mode) ? "tree" : "blob",
94 sha1_to_hex(sha1), base, path,
98 static int compare_tree_entry(void *tree1, unsigned long size1, void *tree2, unsigned long size2, const char *base)
100 unsigned mode1, mode2;
101 const char *path1, *path2;
102 const unsigned char *sha1, *sha2;
103 int cmp, pathlen1, pathlen2;
104 char old_sha1_hex[50];
106 sha1 = extract(tree1, size1, &path1, &mode1);
107 sha2 = extract(tree2, size2, &path2, &mode2);
109 pathlen1 = strlen(path1);
110 pathlen2 = strlen(path2);
111 cmp = cache_name_compare(path1, pathlen1, path2, pathlen2);
113 show_file("-", tree1, size1, base);
117 show_file("+", tree2, size2, base);
120 if (!memcmp(sha1, sha2, 20) && mode1 == mode2)
124 * If the filemode has changed to/from a directory from/to a regular
125 * file, we need to consider it a remove and an add.
127 if (S_ISDIR(mode1) != S_ISDIR(mode2)) {
128 show_file("-", tree1, size1, base);
129 show_file("+", tree2, size2, base);
133 if (recursive && S_ISDIR(mode1)) {
135 char *newbase = malloc_base(base, path1, pathlen1);
136 retval = diff_tree_sha1(sha1, sha2, newbase);
141 if (generate_patch) {
143 diff_change(mode1, mode2, sha1, sha2, base, path1);
146 strcpy(old_sha1_hex, sha1_to_hex(sha1));
147 printf("*%06o->%06o\t%s\t%s->%s\t%s%s%c", mode1, mode2,
148 S_ISDIR(mode1) ? "tree" : "blob",
149 old_sha1_hex, sha1_to_hex(sha2), base, path1,
155 static int interesting(void *tree, unsigned long size, const char *base)
160 int baselen, pathlen;
165 (void)extract(tree, size, &path, &mode);
167 pathlen = strlen(path);
168 baselen = strlen(base);
170 for (i=0; i < nr_paths; i++) {
171 const char *match = paths[i];
172 int matchlen = pathlens[i];
174 if (baselen >= matchlen) {
175 /* If it doesn't match, move along... */
176 if (strncmp(base, match, matchlen))
179 /* The base is a subdirectory of a path which was specified. */
183 /* Does the base match? */
184 if (strncmp(base, match, baselen))
190 if (pathlen > matchlen)
193 if (strncmp(path, match, pathlen))
198 return 0; /* No matches */
201 static int diff_tree(void *tree1, unsigned long size1, void *tree2, unsigned long size2, const char *base)
203 while (size1 | size2) {
204 if (nr_paths && size1 && !interesting(tree1, size1, base)) {
205 update_tree_entry(&tree1, &size1);
208 if (nr_paths && size2 && !interesting(tree2, size2, base)) {
209 update_tree_entry(&tree2, &size2);
213 show_file("+", tree2, size2, base);
214 update_tree_entry(&tree2, &size2);
218 show_file("-", tree1, size1, base);
219 update_tree_entry(&tree1, &size1);
222 switch (compare_tree_entry(tree1, size1, tree2, size2, base)) {
224 update_tree_entry(&tree1, &size1);
227 update_tree_entry(&tree1, &size1);
230 update_tree_entry(&tree2, &size2);
233 die("diff-tree: internal error");
238 static int diff_tree_sha1(const unsigned char *old, const unsigned char *new, const char *base)
241 unsigned long size1, size2;
244 tree1 = read_object_with_reference(old, "tree", &size1, 0);
246 die("unable to read source tree (%s)", sha1_to_hex(old));
247 tree2 = read_object_with_reference(new, "tree", &size2, 0);
249 die("unable to read destination tree (%s)", sha1_to_hex(new));
250 retval = diff_tree(tree1, size1, tree2, size2, base);
256 static int diff_tree_stdin(char *line)
258 int len = strlen(line);
259 unsigned char commit[20], parent[20];
260 unsigned long size, offset;
263 if (!len || line[len-1] != '\n')
266 if (get_sha1_hex(line, commit))
268 if (isspace(line[40]) && !get_sha1_hex(line+41, parent)) {
271 printf("%s:\n", line);
272 return diff_tree_sha1(parent, commit, "");
274 buf = read_object_with_reference(commit, "commit", &size, NULL);
278 /* More than one parent? */
280 if (!memcmp(buf + 46 + 48, "parent ", 7))
286 while (offset + 48 < size && !memcmp(buf + offset, "parent ", 7)) {
287 if (get_sha1_hex(buf + offset + 7, parent))
289 printf("%s %s:\n", line, sha1_to_hex(parent));
290 diff_tree_sha1(parent, commit, "");
296 static char *diff_tree_usage = "diff-tree [-p] [-r] [-z] <tree sha1> <tree sha1>";
298 int main(int argc, char **argv)
301 unsigned char old[20], new[20];
309 if (!arg || *arg != '-')
312 if (!strcmp(arg, "-")) {
317 if (!strcmp(arg, "-r")) {
321 if (!strcmp(arg, "-p")) {
322 recursive = generate_patch = 1;
325 if (!strcmp(arg, "-z")) {
326 line_termination = '\0';
329 if (!strcmp(arg, "-m")) {
333 if (!strcmp(arg, "--stdin")) {
337 usage(diff_tree_usage);
341 if (argc < 2 || get_sha1(argv[0], old) || get_sha1(argv[1], new))
342 usage(diff_tree_usage);
352 pathlens = xmalloc(nr_paths * sizeof(int));
353 for (i=0; i<nr_paths; i++)
354 pathlens[i] = strlen(paths[i]);
358 return diff_tree_sha1(old, new, "");
360 while (fgets(line, sizeof(line), stdin))
361 diff_tree_stdin(line);