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 diff_score_opt = 0;
15 static const char *header = NULL;
16 static const char *header_prefix = "";
18 // What paths are we interested in?
19 static int nr_paths = 0;
20 static char **paths = NULL;
21 static int *pathlens = NULL;
23 static int diff_tree_sha1(const unsigned char *old, const unsigned char *new, const char *base);
25 static void update_tree_entry(void **bufp, unsigned long *sizep)
28 unsigned long size = *sizep;
29 int len = strlen(buf) + 1 + 20;
32 die("corrupt tree file");
37 static const unsigned char *extract(void *tree, unsigned long size, const char **pathp, unsigned int *modep)
39 int len = strlen(tree)+1;
40 const unsigned char *sha1 = tree + len;
41 const char *path = strchr(tree, ' ');
43 if (!path || size < len + 20 || sscanf(tree, "%o", modep) != 1)
44 die("corrupt tree file");
49 static char *malloc_base(const char *base, const char *path, int pathlen)
51 int baselen = strlen(base);
52 char *newbase = xmalloc(baselen + pathlen + 2);
53 memcpy(newbase, base, baselen);
54 memcpy(newbase + baselen, path, pathlen);
55 memcpy(newbase + baselen + pathlen, "/", 2);
59 static void show_file(const char *prefix, void *tree, unsigned long size, const char *base);
60 static void show_tree(const char *prefix, void *tree, unsigned long size, const char *base);
62 /* A file entry went away or appeared */
63 static void show_file(const char *prefix, void *tree, unsigned long size, const char *base)
67 const unsigned char *sha1 = extract(tree, size, &path, &mode);
77 if (recursive && S_ISDIR(mode)) {
80 char *newbase = malloc_base(base, path, strlen(path));
83 tree = read_sha1_file(sha1, type, &size);
84 if (!tree || strcmp(type, "tree"))
85 die("corrupt tree sha %s", sha1_to_hex(sha1));
87 show_tree(prefix, tree, size, newbase);
94 diff_addremove(prefix[0], mode, sha1, base, path);
97 static int compare_tree_entry(void *tree1, unsigned long size1, void *tree2, unsigned long size2, const char *base)
99 unsigned mode1, mode2;
100 const char *path1, *path2;
101 const unsigned char *sha1, *sha2;
102 int cmp, pathlen1, pathlen2;
104 sha1 = extract(tree1, size1, &path1, &mode1);
105 sha2 = extract(tree2, size2, &path2, &mode2);
107 pathlen1 = strlen(path1);
108 pathlen2 = strlen(path2);
109 cmp = cache_name_compare(path1, pathlen1, path2, pathlen2);
111 show_file("-", tree1, size1, base);
115 show_file("+", tree2, size2, base);
118 if (!memcmp(sha1, sha2, 20) && mode1 == mode2)
122 * If the filemode has changed to/from a directory from/to a regular
123 * file, we need to consider it a remove and an add.
125 if (S_ISDIR(mode1) != S_ISDIR(mode2)) {
126 show_file("-", tree1, size1, base);
127 show_file("+", tree2, size2, base);
131 if (recursive && S_ISDIR(mode1)) {
133 char *newbase = malloc_base(base, path1, pathlen1);
134 retval = diff_tree_sha1(sha1, sha2, newbase);
140 printf("%s", header);
146 diff_change(mode1, mode2, sha1, sha2, base, path1);
150 static int interesting(void *tree, unsigned long size, const char *base)
155 int baselen, pathlen;
160 (void)extract(tree, size, &path, &mode);
162 pathlen = strlen(path);
163 baselen = strlen(base);
165 for (i=0; i < nr_paths; i++) {
166 const char *match = paths[i];
167 int matchlen = pathlens[i];
169 if (baselen >= matchlen) {
170 /* If it doesn't match, move along... */
171 if (strncmp(base, match, matchlen))
174 /* The base is a subdirectory of a path which was specified. */
178 /* Does the base match? */
179 if (strncmp(base, match, baselen))
185 if (pathlen > matchlen)
188 if (matchlen > pathlen) {
189 if (match[pathlen] != '/')
195 if (strncmp(path, match, pathlen))
200 return 0; /* No matches */
203 /* A whole sub-tree went away or appeared */
204 static void show_tree(const char *prefix, void *tree, unsigned long size, const char *base)
207 if (interesting(tree, size, base))
208 show_file(prefix, tree, size, base);
209 update_tree_entry(&tree, &size);
213 static int diff_tree(void *tree1, unsigned long size1, void *tree2, unsigned long size2, const char *base)
215 while (size1 | size2) {
216 if (nr_paths && size1 && !interesting(tree1, size1, base)) {
217 update_tree_entry(&tree1, &size1);
220 if (nr_paths && size2 && !interesting(tree2, size2, base)) {
221 update_tree_entry(&tree2, &size2);
225 show_file("+", tree2, size2, base);
226 update_tree_entry(&tree2, &size2);
230 show_file("-", tree1, size1, base);
231 update_tree_entry(&tree1, &size1);
234 switch (compare_tree_entry(tree1, size1, tree2, size2, base)) {
236 update_tree_entry(&tree1, &size1);
239 update_tree_entry(&tree1, &size1);
242 update_tree_entry(&tree2, &size2);
245 die("git-diff-tree: internal error");
250 static int diff_tree_sha1(const unsigned char *old, const unsigned char *new, const char *base)
253 unsigned long size1, size2;
256 tree1 = read_object_with_reference(old, "tree", &size1, 0);
258 die("unable to read source tree (%s)", sha1_to_hex(old));
259 tree2 = read_object_with_reference(new, "tree", &size2, 0);
261 die("unable to read destination tree (%s)", sha1_to_hex(new));
262 retval = diff_tree(tree1, size1, tree2, size2, base);
268 static int diff_tree_sha1_top(const unsigned char *old,
269 const unsigned char *new, const char *base)
273 diff_setup(detect_rename, diff_score_opt, 0,
274 (generate_patch ? -1 : line_termination),
276 ret = diff_tree_sha1(old, new, base);
281 static int diff_root_tree(const unsigned char *new, const char *base)
287 diff_setup(detect_rename, diff_score_opt, 0,
288 (generate_patch ? -1 : line_termination),
290 tree = read_object_with_reference(new, "tree", &size, 0);
292 die("unable to read root tree (%s)", sha1_to_hex(new));
293 retval = diff_tree("", 0, tree, size, base);
299 static int get_one_line(const char *msg, unsigned long len)
311 static int add_author_info(char *buf, const char *line, int len)
314 unsigned int namelen;
318 line += strlen("author ");
319 date = strchr(line, '>');
322 namelen = ++date - line;
323 time = strtoul(date, &date, 10);
324 tz = strtol(date, NULL, 10);
326 return sprintf(buf, "Author: %.*s\nDate: %s\n",
328 show_date(time, tz));
331 static char *generate_header(const char *commit, const char *parent, const char *msg, unsigned long len)
333 static char this_header[1000];
336 offset = sprintf(this_header, "%s%s (from %s)\n", header_prefix, commit, parent);
337 if (verbose_header) {
341 const char *line = msg;
342 int linelen = get_one_line(msg, len);
346 if (offset + linelen + 10 > sizeof(this_header))
354 if (!memcmp(line, "author ", 7))
355 offset += add_author_info(this_header + offset, line, linelen);
358 memset(this_header + offset, ' ', 4);
359 memcpy(this_header + offset + 4, line, linelen);
360 offset += linelen + 4;
362 this_header[offset++] = '\n';
363 this_header[offset] = 0;
369 static int diff_tree_commit(const unsigned char *commit, const char *name)
371 unsigned long size, offset;
372 char *buf = read_object_with_reference(commit, "commit", &size, NULL);
378 static char commit_name[60];
379 strcpy(commit_name, sha1_to_hex(commit));
384 if (show_root_diff && memcmp(buf + 46, "parent ", 7)) {
385 header = generate_header(name, "root", buf, size);
386 diff_root_tree(commit, "");
389 /* More than one parent? */
391 if (!memcmp(buf + 46 + 48, "parent ", 7))
396 while (offset + 48 < size && !memcmp(buf + offset, "parent ", 7)) {
397 unsigned char parent[20];
398 if (get_sha1_hex(buf + offset + 7, parent))
400 header = generate_header(name, sha1_to_hex(parent), buf, size);
401 diff_tree_sha1_top(parent, commit, "");
402 if (!header && verbose_header)
403 header_prefix = "\ndiff-tree ";
409 static int diff_tree_stdin(char *line)
411 int len = strlen(line);
412 unsigned char commit[20], parent[20];
413 static char this_header[1000];
415 if (!len || line[len-1] != '\n')
418 if (get_sha1_hex(line, commit))
420 if (isspace(line[40]) && !get_sha1_hex(line+41, parent)) {
423 sprintf(this_header, "%s (from %s)\n", line, line+41);
424 header = this_header;
425 return diff_tree_sha1_top(parent, commit, "");
428 return diff_tree_commit(commit, line);
431 static char *diff_tree_usage =
432 "git-diff-tree [-p] [-r] [-z] [--stdin] [-M] [-m] [-s] [-v] <tree-ish> <tree-ish>";
434 int main(int argc, char **argv)
438 unsigned char sha1[2][20];
451 if (nr_sha1 < 2 && !get_sha1(arg, sha1[nr_sha1])) {
458 if (!strcmp(arg, "--")) {
463 if (!strcmp(arg, "-r")) {
467 if (!strcmp(arg, "-p")) {
468 recursive = generate_patch = 1;
471 if (!strncmp(arg, "-M", 2)) {
472 detect_rename = recursive = generate_patch = 1;
473 diff_score_opt = diff_scoreopt_parse(arg);
476 if (!strcmp(arg, "-z")) {
477 line_termination = '\0';
480 if (!strcmp(arg, "-m")) {
484 if (!strcmp(arg, "-s")) {
488 if (!strcmp(arg, "-v")) {
490 header_prefix = "diff-tree ";
493 if (!strcmp(arg, "--stdin")) {
497 if (!strcmp(arg, "--root")) {
501 usage(diff_tree_usage);
509 pathlens = xmalloc(nr_paths * sizeof(int));
510 for (i=0; i<nr_paths; i++)
511 pathlens[i] = strlen(paths[i]);
517 usage(diff_tree_usage);
520 diff_tree_commit(sha1[0], NULL);
523 diff_tree_sha1_top(sha1[0], sha1[1], "");
530 while (fgets(line, sizeof(line), stdin))
531 diff_tree_stdin(line);