6 static int verbose_header = 0;
7 static int ignore_merges = 1;
8 static int recursive = 0;
9 static int read_stdin = 0;
10 static int line_termination = '\n';
11 static int generate_patch = 0;
12 static const char *header = NULL;
13 static const char *header_prefix = "";
15 // What paths are we interested in?
16 static int nr_paths = 0;
17 static char **paths = NULL;
18 static int *pathlens = NULL;
20 static int diff_tree_sha1(const unsigned char *old, const unsigned char *new, const char *base);
22 static void update_tree_entry(void **bufp, unsigned long *sizep)
25 unsigned long size = *sizep;
26 int len = strlen(buf) + 1 + 20;
29 die("corrupt tree file");
34 static const unsigned char *extract(void *tree, unsigned long size, const char **pathp, unsigned int *modep)
36 int len = strlen(tree)+1;
37 const unsigned char *sha1 = tree + len;
38 const char *path = strchr(tree, ' ');
40 if (!path || size < len + 20 || sscanf(tree, "%o", modep) != 1)
41 die("corrupt tree file");
46 static char *malloc_base(const char *base, const char *path, int pathlen)
48 int baselen = strlen(base);
49 char *newbase = xmalloc(baselen + pathlen + 2);
50 memcpy(newbase, base, baselen);
51 memcpy(newbase + baselen, path, pathlen);
52 memcpy(newbase + baselen + pathlen, "/", 2);
56 static void show_file(const char *prefix, void *tree, unsigned long size, const char *base);
57 static void show_tree(const char *prefix, void *tree, unsigned long size, const char *base);
59 /* A file entry went away or appeared */
60 static void show_file(const char *prefix, void *tree, unsigned long size, const char *base)
64 const unsigned char *sha1 = extract(tree, size, &path, &mode);
74 if (recursive && S_ISDIR(mode)) {
77 char *newbase = malloc_base(base, path, strlen(path));
80 tree = read_sha1_file(sha1, type, &size);
81 if (!tree || strcmp(type, "tree"))
82 die("corrupt tree sha %s", sha1_to_hex(sha1));
84 show_tree(prefix, tree, size, newbase);
93 diff_addremove(prefix[0], mode, sha1, base, path);
96 printf("%s%06o\t%s\t%s\t%s%s%c", prefix, mode,
97 S_ISDIR(mode) ? "tree" : "blob",
98 sha1_to_hex(sha1), base, path,
102 static int compare_tree_entry(void *tree1, unsigned long size1, void *tree2, unsigned long size2, const char *base)
104 unsigned mode1, mode2;
105 const char *path1, *path2;
106 const unsigned char *sha1, *sha2;
107 int cmp, pathlen1, pathlen2;
108 char old_sha1_hex[50];
110 sha1 = extract(tree1, size1, &path1, &mode1);
111 sha2 = extract(tree2, size2, &path2, &mode2);
113 pathlen1 = strlen(path1);
114 pathlen2 = strlen(path2);
115 cmp = cache_name_compare(path1, pathlen1, path2, pathlen2);
117 show_file("-", tree1, size1, base);
121 show_file("+", tree2, size2, base);
124 if (!memcmp(sha1, sha2, 20) && mode1 == mode2)
128 * If the filemode has changed to/from a directory from/to a regular
129 * file, we need to consider it a remove and an add.
131 if (S_ISDIR(mode1) != S_ISDIR(mode2)) {
132 show_file("-", tree1, size1, base);
133 show_file("+", tree2, size2, base);
137 if (recursive && S_ISDIR(mode1)) {
139 char *newbase = malloc_base(base, path1, pathlen1);
140 retval = diff_tree_sha1(sha1, sha2, newbase);
146 printf("%s", header);
152 if (generate_patch) {
154 diff_change(mode1, mode2, sha1, sha2, base, path1);
157 strcpy(old_sha1_hex, sha1_to_hex(sha1));
158 printf("*%06o->%06o\t%s\t%s->%s\t%s%s%c", mode1, mode2,
159 S_ISDIR(mode1) ? "tree" : "blob",
160 old_sha1_hex, sha1_to_hex(sha2), base, path1,
166 static int interesting(void *tree, unsigned long size, const char *base)
171 int baselen, pathlen;
176 (void)extract(tree, size, &path, &mode);
178 pathlen = strlen(path);
179 baselen = strlen(base);
181 for (i=0; i < nr_paths; i++) {
182 const char *match = paths[i];
183 int matchlen = pathlens[i];
185 if (baselen >= matchlen) {
186 /* If it doesn't match, move along... */
187 if (strncmp(base, match, matchlen))
190 /* The base is a subdirectory of a path which was specified. */
194 /* Does the base match? */
195 if (strncmp(base, match, baselen))
201 if (pathlen > matchlen)
204 if (matchlen > pathlen) {
205 if (match[pathlen] != '/')
211 if (strncmp(path, match, pathlen))
216 return 0; /* No matches */
219 /* A whole sub-tree went away or appeared */
220 static void show_tree(const char *prefix, void *tree, unsigned long size, const char *base)
223 if (interesting(tree, size, base))
224 show_file(prefix, tree, size, base);
225 update_tree_entry(&tree, &size);
229 static int diff_tree(void *tree1, unsigned long size1, void *tree2, unsigned long size2, const char *base)
231 while (size1 | size2) {
232 if (nr_paths && size1 && !interesting(tree1, size1, base)) {
233 update_tree_entry(&tree1, &size1);
236 if (nr_paths && size2 && !interesting(tree2, size2, base)) {
237 update_tree_entry(&tree2, &size2);
241 show_file("+", tree2, size2, base);
242 update_tree_entry(&tree2, &size2);
246 show_file("-", tree1, size1, base);
247 update_tree_entry(&tree1, &size1);
250 switch (compare_tree_entry(tree1, size1, tree2, size2, base)) {
252 update_tree_entry(&tree1, &size1);
255 update_tree_entry(&tree1, &size1);
258 update_tree_entry(&tree2, &size2);
261 die("diff-tree: internal error");
266 static int diff_tree_sha1(const unsigned char *old, const unsigned char *new, const char *base)
269 unsigned long size1, size2;
272 tree1 = read_object_with_reference(old, "tree", &size1, 0);
274 die("unable to read source tree (%s)", sha1_to_hex(old));
275 tree2 = read_object_with_reference(new, "tree", &size2, 0);
277 die("unable to read destination tree (%s)", sha1_to_hex(new));
278 retval = diff_tree(tree1, size1, tree2, size2, base);
284 static int get_one_line(const char *msg, unsigned long len)
296 static int add_author_info(char *buf, const char *line, int len)
299 unsigned int namelen;
303 line += strlen("author ");
304 date = strchr(line, '>');
307 namelen = ++date - line;
308 time = strtoul(date, &date, 10);
309 tz = strtol(date, NULL, 10);
311 return sprintf(buf, "Author: %.*s\nDate: %s\n",
313 show_date(time, tz));
316 static char *generate_header(const char *commit, const char *parent, const char *msg, unsigned long len)
318 static char this_header[1000];
321 offset = sprintf(this_header, "%s%s (from %s)\n", header_prefix, commit, parent);
322 if (verbose_header) {
326 const char *line = msg;
327 int linelen = get_one_line(msg, len);
331 if (offset + linelen + 10 > sizeof(this_header))
339 if (!memcmp(line, "author ", 7))
340 offset += add_author_info(this_header + offset, line, linelen);
343 memset(this_header + offset, ' ', 4);
344 memcpy(this_header + offset + 4, line, linelen);
345 offset += linelen + 4;
347 this_header[offset++] = '\n';
348 this_header[offset] = 0;
354 static int diff_tree_commit(const unsigned char *commit, const char *name)
356 unsigned long size, offset;
357 char *buf = read_object_with_reference(commit, "commit", &size, NULL);
362 /* More than one parent? */
364 if (!memcmp(buf + 46 + 48, "parent ", 7))
369 static char commit_name[60];
370 strcpy(commit_name, sha1_to_hex(commit));
375 while (offset + 48 < size && !memcmp(buf + offset, "parent ", 7)) {
376 unsigned char parent[20];
377 if (get_sha1_hex(buf + offset + 7, parent))
379 header = generate_header(name, sha1_to_hex(parent), buf, size);
380 diff_tree_sha1(parent, commit, "");
381 if (!header && verbose_header)
382 header_prefix = "\ndiff-tree ";
388 static int diff_tree_stdin(char *line)
390 int len = strlen(line);
391 unsigned char commit[20], parent[20];
392 static char this_header[1000];
394 if (!len || line[len-1] != '\n')
397 if (get_sha1_hex(line, commit))
399 if (isspace(line[40]) && !get_sha1_hex(line+41, parent)) {
402 sprintf(this_header, "%s (from %s)\n", line, line+41);
403 header = this_header;
404 return diff_tree_sha1(parent, commit, "");
407 return diff_tree_commit(commit, line);
410 static char *diff_tree_usage =
411 "diff-tree [-p] [-r] [-z] [--stdin] [-m] [-s] [-v] <tree sha1> <tree sha1>";
413 int main(int argc, char **argv)
417 unsigned char sha1[2][20];
430 if (nr_sha1 < 2 && !get_sha1(arg, sha1[nr_sha1])) {
437 if (!strcmp(arg, "--")) {
442 if (!strcmp(arg, "-r")) {
446 if (!strcmp(arg, "-p")) {
447 recursive = generate_patch = 1;
450 if (!strcmp(arg, "-z")) {
451 line_termination = '\0';
454 if (!strcmp(arg, "-m")) {
458 if (!strcmp(arg, "-s")) {
462 if (!strcmp(arg, "-v")) {
464 header_prefix = "diff-tree ";
467 if (!strcmp(arg, "--stdin")) {
471 usage(diff_tree_usage);
479 pathlens = xmalloc(nr_paths * sizeof(int));
480 for (i=0; i<nr_paths; i++)
481 pathlens[i] = strlen(paths[i]);
487 usage(diff_tree_usage);
490 diff_tree_commit(sha1[0], NULL);
493 diff_tree_sha1(sha1[0], sha1[1], "");
500 while (fgets(line, sizeof(line), stdin))
501 diff_tree_stdin(line);