6 static int show_root_diff = 0;
7 static int verbose_header = 0;
8 static int ignore_merges = 1;
9 static int recursive = 0;
10 static int read_stdin = 0;
11 static int line_termination = '\n';
12 static int generate_patch = 0;
13 static int detect_rename = 0;
14 static int reverse_diff = 0;
15 static int diff_score_opt = 0;
16 static const char *header = NULL;
17 static const char *header_prefix = "";
19 // What paths are we interested in?
20 static int nr_paths = 0;
21 static char **paths = NULL;
22 static int *pathlens = NULL;
24 static int diff_tree_sha1(const unsigned char *old, const unsigned char *new, const char *base);
26 static void update_tree_entry(void **bufp, unsigned long *sizep)
29 unsigned long size = *sizep;
30 int len = strlen(buf) + 1 + 20;
33 die("corrupt tree file");
38 static const unsigned char *extract(void *tree, unsigned long size, const char **pathp, unsigned int *modep)
40 int len = strlen(tree)+1;
41 const unsigned char *sha1 = tree + len;
42 const char *path = strchr(tree, ' ');
44 if (!path || size < len + 20 || sscanf(tree, "%o", modep) != 1)
45 die("corrupt tree file");
50 static char *malloc_base(const char *base, const char *path, int pathlen)
52 int baselen = strlen(base);
53 char *newbase = xmalloc(baselen + pathlen + 2);
54 memcpy(newbase, base, baselen);
55 memcpy(newbase + baselen, path, pathlen);
56 memcpy(newbase + baselen + pathlen, "/", 2);
60 static void show_file(const char *prefix, void *tree, unsigned long size, const char *base);
61 static void show_tree(const char *prefix, void *tree, unsigned long size, const char *base);
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);
78 if (recursive && S_ISDIR(mode)) {
81 char *newbase = malloc_base(base, path, strlen(path));
84 tree = read_sha1_file(sha1, type, &size);
85 if (!tree || strcmp(type, "tree"))
86 die("corrupt tree sha %s", sha1_to_hex(sha1));
88 show_tree(prefix, tree, size, newbase);
95 diff_addremove(prefix[0], mode, 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;
105 sha1 = extract(tree1, size1, &path1, &mode1);
106 sha2 = extract(tree2, size2, &path2, &mode2);
108 pathlen1 = strlen(path1);
109 pathlen2 = strlen(path2);
110 cmp = base_name_compare(path1, pathlen1, mode1, path2, pathlen2, mode2);
112 show_file("-", tree1, size1, base);
116 show_file("+", tree2, size2, base);
119 if (!memcmp(sha1, sha2, 20) && mode1 == mode2)
123 * If the filemode has changed to/from a directory from/to a regular
124 * file, we need to consider it a remove and an add.
126 if (S_ISDIR(mode1) != S_ISDIR(mode2)) {
127 show_file("-", tree1, size1, base);
128 show_file("+", tree2, size2, base);
132 if (recursive && S_ISDIR(mode1)) {
134 char *newbase = malloc_base(base, path1, pathlen1);
135 retval = diff_tree_sha1(sha1, sha2, newbase);
141 printf("%s", header);
147 diff_change(mode1, mode2, sha1, sha2, base, path1);
151 static int interesting(void *tree, unsigned long size, const char *base)
156 int baselen, pathlen;
161 (void)extract(tree, size, &path, &mode);
163 pathlen = strlen(path);
164 baselen = strlen(base);
166 for (i=0; i < nr_paths; i++) {
167 const char *match = paths[i];
168 int matchlen = pathlens[i];
170 if (baselen >= matchlen) {
171 /* If it doesn't match, move along... */
172 if (strncmp(base, match, matchlen))
175 /* The base is a subdirectory of a path which was specified. */
179 /* Does the base match? */
180 if (strncmp(base, match, baselen))
186 if (pathlen > matchlen)
189 if (matchlen > pathlen) {
190 if (match[pathlen] != '/')
196 if (strncmp(path, match, pathlen))
201 return 0; /* No matches */
204 /* A whole sub-tree went away or appeared */
205 static void show_tree(const char *prefix, void *tree, unsigned long size, const char *base)
208 if (interesting(tree, size, base))
209 show_file(prefix, tree, size, base);
210 update_tree_entry(&tree, &size);
214 static int diff_tree(void *tree1, unsigned long size1, void *tree2, unsigned long size2, const char *base)
216 while (size1 | size2) {
217 if (nr_paths && size1 && !interesting(tree1, size1, base)) {
218 update_tree_entry(&tree1, &size1);
221 if (nr_paths && size2 && !interesting(tree2, size2, base)) {
222 update_tree_entry(&tree2, &size2);
226 show_file("+", tree2, size2, base);
227 update_tree_entry(&tree2, &size2);
231 show_file("-", tree1, size1, base);
232 update_tree_entry(&tree1, &size1);
235 switch (compare_tree_entry(tree1, size1, tree2, size2, base)) {
237 update_tree_entry(&tree1, &size1);
240 update_tree_entry(&tree1, &size1);
243 update_tree_entry(&tree2, &size2);
246 die("git-diff-tree: internal error");
251 static int diff_tree_sha1(const unsigned char *old, const unsigned char *new, const char *base)
254 unsigned long size1, size2;
257 tree1 = read_object_with_reference(old, "tree", &size1, NULL);
259 die("unable to read source tree (%s)", sha1_to_hex(old));
260 tree2 = read_object_with_reference(new, "tree", &size2, NULL);
262 die("unable to read destination tree (%s)", sha1_to_hex(new));
263 retval = diff_tree(tree1, size1, tree2, size2, base);
269 static int diff_tree_sha1_top(const unsigned char *old,
270 const unsigned char *new, const char *base)
274 diff_setup(detect_rename, diff_score_opt, reverse_diff,
275 (generate_patch ? -1 : line_termination),
277 ret = diff_tree_sha1(old, new, base);
282 static int diff_root_tree(const unsigned char *new, const char *base)
288 diff_setup(detect_rename, diff_score_opt, reverse_diff,
289 (generate_patch ? -1 : line_termination),
291 tree = read_object_with_reference(new, "tree", &size, NULL);
293 die("unable to read root tree (%s)", sha1_to_hex(new));
294 retval = diff_tree("", 0, tree, size, base);
300 static int get_one_line(const char *msg, unsigned long len)
312 static int add_author_info(char *buf, const char *line, int len)
315 unsigned int namelen;
319 line += strlen("author ");
320 date = strchr(line, '>');
323 namelen = ++date - line;
324 time = strtoul(date, &date, 10);
325 tz = strtol(date, NULL, 10);
327 return sprintf(buf, "Author: %.*s\nDate: %s\n",
329 show_date(time, tz));
332 static char *generate_header(const char *commit, const char *parent, const char *msg, unsigned long len)
334 static char this_header[1000];
337 offset = sprintf(this_header, "%s%s (from %s)\n", header_prefix, commit, parent);
338 if (verbose_header) {
342 const char *line = msg;
343 int linelen = get_one_line(msg, len);
347 if (offset + linelen + 10 > sizeof(this_header))
355 if (!memcmp(line, "author ", 7))
356 offset += add_author_info(this_header + offset, line, linelen);
359 memset(this_header + offset, ' ', 4);
360 memcpy(this_header + offset + 4, line, linelen);
361 offset += linelen + 4;
363 this_header[offset++] = '\n';
364 this_header[offset] = 0;
370 static int diff_tree_commit(const unsigned char *commit, const char *name)
372 unsigned long size, offset;
373 char *buf = read_object_with_reference(commit, "commit", &size, NULL);
379 static char commit_name[60];
380 strcpy(commit_name, sha1_to_hex(commit));
385 if (show_root_diff && memcmp(buf + 46, "parent ", 7)) {
386 header = generate_header(name, "root", buf, size);
387 diff_root_tree(commit, "");
390 /* More than one parent? */
392 if (!memcmp(buf + 46 + 48, "parent ", 7))
397 while (offset + 48 < size && !memcmp(buf + offset, "parent ", 7)) {
398 unsigned char parent[20];
399 if (get_sha1_hex(buf + offset + 7, parent))
401 header = generate_header(name, sha1_to_hex(parent), buf, size);
402 diff_tree_sha1_top(parent, commit, "");
403 if (!header && verbose_header)
404 header_prefix = "\ndiff-tree ";
410 static int diff_tree_stdin(char *line)
412 int len = strlen(line);
413 unsigned char commit[20], parent[20];
414 static char this_header[1000];
416 if (!len || line[len-1] != '\n')
419 if (get_sha1_hex(line, commit))
421 if (isspace(line[40]) && !get_sha1_hex(line+41, parent)) {
424 sprintf(this_header, "%s (from %s)\n", line, line+41);
425 header = this_header;
426 return diff_tree_sha1_top(parent, commit, "");
429 return diff_tree_commit(commit, line);
432 static char *diff_tree_usage =
433 "git-diff-tree [-p] [-r] [-z] [--stdin] [-M] [-R] [-m] [-s] [-v] <tree-ish> <tree-ish>";
435 int main(int argc, char **argv)
439 unsigned char sha1[2][20];
452 if (nr_sha1 < 2 && !get_sha1(arg, sha1[nr_sha1])) {
459 if (!strcmp(arg, "--")) {
464 if (!strcmp(arg, "-r")) {
468 if (!strcmp(arg, "-R")) {
472 if (!strcmp(arg, "-p")) {
473 recursive = generate_patch = 1;
476 if (!strncmp(arg, "-M", 2)) {
477 detect_rename = recursive = generate_patch = 1;
478 diff_score_opt = diff_scoreopt_parse(arg);
481 if (!strcmp(arg, "-z")) {
482 line_termination = '\0';
485 if (!strcmp(arg, "-m")) {
489 if (!strcmp(arg, "-s")) {
493 if (!strcmp(arg, "-v")) {
495 header_prefix = "diff-tree ";
498 if (!strcmp(arg, "--stdin")) {
502 if (!strcmp(arg, "--root")) {
506 usage(diff_tree_usage);
514 pathlens = xmalloc(nr_paths * sizeof(int));
515 for (i=0; i<nr_paths; i++)
516 pathlens[i] = strlen(paths[i]);
522 usage(diff_tree_usage);
525 diff_tree_commit(sha1[0], NULL);
528 diff_tree_sha1_top(sha1[0], sha1[1], "");
535 while (fgets(line, sizeof(line), stdin))
536 diff_tree_stdin(line);