5 static int show_root_diff = 0;
6 static int no_commit_id = 0;
7 static int verbose_header = 0;
8 static int ignore_merges = 1;
9 static int read_stdin = 0;
11 static const char *header = NULL;
12 static const char *header_prefix = "";
13 static enum cmit_fmt commit_format = CMIT_FMT_RAW;
15 static struct diff_options diff_options;
17 static void call_diff_setup_done(void)
19 diff_setup_done(&diff_options);
22 static int call_diff_flush(void)
24 diffcore_std(&diff_options);
25 if (diff_queue_is_empty()) {
26 int saved_fmt = diff_options.output_format;
27 diff_options.output_format = DIFF_FORMAT_NO_OUTPUT;
28 diff_flush(&diff_options);
29 diff_options.output_format = saved_fmt;
34 printf("%s%c", header, diff_options.line_termination);
37 diff_flush(&diff_options);
41 static int diff_tree_sha1_top(const unsigned char *old,
42 const unsigned char *new, const char *base)
46 call_diff_setup_done();
47 ret = diff_tree_sha1(old, new, base, &diff_options);
52 static int diff_root_tree(const unsigned char *new, const char *base)
56 struct tree_desc empty, real;
58 call_diff_setup_done();
59 tree = read_object_with_reference(new, "tree", &real.size, NULL);
61 die("unable to read root tree (%s)", sha1_to_hex(new));
66 retval = diff_tree(&empty, &real, base, &diff_options);
72 static const char *generate_header(const char *commit, const char *parent, const char *msg, unsigned long len)
74 static char this_header[16384];
80 offset = sprintf(this_header, "%s%s (from %s)\n", header_prefix, commit, parent);
81 offset += pretty_print_commit(commit_format, msg, len, this_header + offset, sizeof(this_header) - offset);
85 static int diff_tree_commit(const unsigned char *commit, const char *name)
87 unsigned long size, offset;
88 char *buf = read_object_with_reference(commit, "commit", &size, NULL);
94 static char commit_name[60];
95 strcpy(commit_name, sha1_to_hex(commit));
100 if (show_root_diff && memcmp(buf + 46, "parent ", 7)) {
101 header = generate_header(name, "root", buf, size);
102 diff_root_tree(commit, "");
105 /* More than one parent? */
107 if (!memcmp(buf + 46 + 48, "parent ", 7))
112 while (offset + 48 < size && !memcmp(buf + offset, "parent ", 7)) {
113 unsigned char parent[20];
114 if (get_sha1_hex(buf + offset + 7, parent))
116 header = generate_header(name, sha1_to_hex(parent), buf, size);
117 diff_tree_sha1_top(parent, commit, "");
118 if (!header && verbose_header) {
119 header_prefix = "\ndiff-tree ";
121 * Don't print multiple merge entries if we
122 * don't print the diffs.
131 static int diff_tree_stdin(char *line)
133 int len = strlen(line);
134 unsigned char commit[20], parent[20];
135 static char this_header[1000];
137 if (!len || line[len-1] != '\n')
140 if (get_sha1_hex(line, commit))
142 if (isspace(line[40]) && !get_sha1_hex(line+41, parent)) {
145 sprintf(this_header, "%s (from %s)\n", line, line+41);
146 header = this_header;
147 return diff_tree_sha1_top(parent, commit, "");
150 return diff_tree_commit(commit, line);
153 static const char diff_tree_usage[] =
154 "git-diff-tree [--stdin] [-m] [-s] [-v] [--pretty] [-t] [-r] [--root] "
155 "[<common diff options>] <tree-ish> [<tree-ish>] [<path>...]\n"
156 " -r diff recursively\n"
157 " --root include the initial commit as diff against /dev/null\n"
158 COMMON_DIFF_OPTIONS_HELP;
160 int main(int argc, const char **argv)
164 unsigned char sha1[2][20];
165 const char *prefix = setup_git_directory();
167 git_config(git_default_config);
169 diff_setup(&diff_options);
182 if (nr_sha1 < 2 && !get_sha1(arg, sha1[nr_sha1])) {
189 diff_opt_cnt = diff_opt_parse(&diff_options, argv, argc);
190 if (diff_opt_cnt < 0)
191 usage(diff_tree_usage);
192 else if (diff_opt_cnt) {
193 argv += diff_opt_cnt - 1;
194 argc -= diff_opt_cnt - 1;
199 if (!strcmp(arg, "--")) {
204 if (!strcmp(arg, "-r")) {
205 diff_options.recursive = 1;
208 if (!strcmp(arg, "-t")) {
209 diff_options.recursive = 1;
210 diff_options.tree_in_recursive = 1;
213 if (!strcmp(arg, "-m")) {
217 if (!strcmp(arg, "-v")) {
219 header_prefix = "diff-tree ";
222 if (!strncmp(arg, "--pretty", 8)) {
224 header_prefix = "diff-tree ";
225 commit_format = get_commit_format(arg+8);
228 if (!strcmp(arg, "--stdin")) {
232 if (!strcmp(arg, "--root")) {
236 if (!strcmp(arg, "--no-commit-id")) {
240 usage(diff_tree_usage);
242 if (diff_options.output_format == DIFF_FORMAT_PATCH)
243 diff_options.recursive = 1;
245 diff_tree_setup_paths(get_pathspec(prefix, argv));
250 usage(diff_tree_usage);
253 diff_tree_commit(sha1[0], NULL);
256 diff_tree_sha1_top(sha1[0], sha1[1], "");
263 if (diff_options.detect_rename)
264 diff_options.setup |= (DIFF_SETUP_USE_SIZE_CACHE |
265 DIFF_SETUP_USE_CACHE);
266 while (fgets(line, sizeof(line), stdin))
267 diff_tree_stdin(line);