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)
74 static char this_header[16384];
82 offset = sprintf(this_header, "%s%s (from %s)\n", header_prefix, commit, parent);
83 offset += pretty_print_commit(commit_format, msg, len, this_header + offset, sizeof(this_header) - offset);
87 static int diff_tree_commit(const unsigned char *commit_sha1)
89 struct commit *commit;
90 struct commit_list *parents;
92 unsigned char sha1[20];
94 sprintf(name, "%s^0", sha1_to_hex(commit_sha1));
95 if (get_sha1(name, sha1))
98 commit = lookup_commit(sha1);
101 if (show_root_diff && !commit->parents) {
102 header = generate_header(name, "root", commit->buffer);
103 diff_root_tree(commit_sha1, "");
106 /* More than one parent? */
107 if (ignore_merges && commit->parents && commit->parents->next)
110 for (parents = commit->parents; parents; parents = parents->next) {
111 struct commit *parent = parents->item;
112 header = generate_header(name,
113 sha1_to_hex(parent->object.sha1),
115 diff_tree_sha1_top(parent->object.sha1, commit_sha1, "");
116 if (!header && verbose_header) {
117 header_prefix = "\ndiff-tree ";
119 * Don't print multiple merge entries if we
120 * don't print the diffs.
127 static int diff_tree_stdin(char *line)
129 int len = strlen(line);
130 unsigned char commit[20], parent[20];
131 static char this_header[1000];
133 if (!len || line[len-1] != '\n')
136 if (get_sha1_hex(line, commit))
138 if (isspace(line[40]) && !get_sha1_hex(line+41, parent)) {
141 sprintf(this_header, "%s (from %s)\n", line, line+41);
142 header = this_header;
143 return diff_tree_sha1_top(parent, commit, "");
146 return diff_tree_commit(commit);
149 static const char diff_tree_usage[] =
150 "git-diff-tree [--stdin] [-m] [-s] [-v] [--pretty] [-t] [-r] [--root] "
151 "[<common diff options>] <tree-ish> [<tree-ish>] [<path>...]\n"
152 " -r diff recursively\n"
153 " --root include the initial commit as diff against /dev/null\n"
154 COMMON_DIFF_OPTIONS_HELP;
156 int main(int argc, const char **argv)
160 unsigned char sha1[2][20];
161 const char *prefix = setup_git_directory();
163 git_config(git_diff_config);
165 diff_setup(&diff_options);
178 if (nr_sha1 < 2 && !get_sha1(arg, sha1[nr_sha1])) {
185 diff_opt_cnt = diff_opt_parse(&diff_options, argv, argc);
186 if (diff_opt_cnt < 0)
187 usage(diff_tree_usage);
188 else if (diff_opt_cnt) {
189 argv += diff_opt_cnt - 1;
190 argc -= diff_opt_cnt - 1;
195 if (!strcmp(arg, "--")) {
200 if (!strcmp(arg, "-r")) {
201 diff_options.recursive = 1;
204 if (!strcmp(arg, "-t")) {
205 diff_options.recursive = 1;
206 diff_options.tree_in_recursive = 1;
209 if (!strcmp(arg, "-m")) {
213 if (!strcmp(arg, "-v")) {
215 header_prefix = "diff-tree ";
218 if (!strncmp(arg, "--pretty", 8)) {
220 header_prefix = "diff-tree ";
221 commit_format = get_commit_format(arg+8);
224 if (!strcmp(arg, "--stdin")) {
228 if (!strcmp(arg, "--root")) {
232 if (!strcmp(arg, "--no-commit-id")) {
236 usage(diff_tree_usage);
238 if (diff_options.output_format == DIFF_FORMAT_PATCH)
239 diff_options.recursive = 1;
241 diff_tree_setup_paths(get_pathspec(prefix, argv));
246 usage(diff_tree_usage);
249 diff_tree_commit(sha1[0]);
252 diff_tree_sha1_top(sha1[0], sha1[1], "");
259 if (diff_options.detect_rename)
260 diff_options.setup |= (DIFF_SETUP_USE_SIZE_CACHE |
261 DIFF_SETUP_USE_CACHE);
262 while (fgets(line, sizeof(line), stdin))
263 diff_tree_stdin(line);