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 combine_merges
= 0;
10 static int dense_combined_merges
= 0;
11 static int read_stdin
= 0;
12 static int always_show_header
= 0;
14 static const char *header
= NULL
;
15 static const char *header_prefix
= "";
16 static enum cmit_fmt commit_format
= CMIT_FMT_RAW
;
18 static struct diff_options diff_options
;
20 static int call_diff_flush(void)
22 diffcore_std(&diff_options
);
23 if (diff_queue_is_empty()) {
24 int saved_fmt
= diff_options
.output_format
;
25 diff_options
.output_format
= DIFF_FORMAT_NO_OUTPUT
;
26 diff_flush(&diff_options
);
27 diff_options
.output_format
= saved_fmt
;
32 printf("%s%c", header
, diff_options
.line_termination
);
35 diff_flush(&diff_options
);
39 static int diff_tree_sha1_top(const unsigned char *old
,
40 const unsigned char *new, const char *base
)
44 ret
= diff_tree_sha1(old
, new, base
, &diff_options
);
49 static int diff_root_tree(const unsigned char *new, const char *base
)
53 struct tree_desc empty
, real
;
55 tree
= read_object_with_reference(new, "tree", &real
.size
, NULL
);
57 die("unable to read root tree (%s)", sha1_to_hex(new));
62 retval
= diff_tree(&empty
, &real
, base
, &diff_options
);
68 static const char *generate_header(const unsigned char *commit_sha1
,
69 const unsigned char *parent_sha1
,
70 const struct commit
*commit
)
72 static char this_header
[16384];
75 int abbrev
= diff_options
.abbrev
;
76 const char *msg
= commit
->buffer
;
79 return sha1_to_hex(commit_sha1
);
83 offset
= sprintf(this_header
, "%s%s ",
85 diff_unique_abbrev(commit_sha1
, abbrev
));
86 if (commit_sha1
!= parent_sha1
)
87 offset
+= sprintf(this_header
+ offset
, "(from %s)\n",
89 ? diff_unique_abbrev(parent_sha1
, abbrev
)
92 offset
+= sprintf(this_header
+ offset
, "(from parents)\n");
93 offset
+= pretty_print_commit(commit_format
, commit
, len
,
95 sizeof(this_header
) - offset
, abbrev
);
96 if (always_show_header
) {
103 static int diff_tree_commit(const unsigned char *commit_sha1
)
105 struct commit
*commit
;
106 struct commit_list
*parents
;
108 unsigned char sha1
[20];
110 sprintf(name
, "%s^0", sha1_to_hex(commit_sha1
));
111 if (get_sha1(name
, sha1
))
114 commit
= lookup_commit(sha1
);
117 if (show_root_diff
&& !commit
->parents
) {
118 header
= generate_header(sha1
, NULL
, commit
);
119 diff_root_tree(commit_sha1
, "");
122 /* More than one parent? */
123 if (commit
->parents
&& commit
->parents
->next
) {
126 else if (combine_merges
) {
127 header
= generate_header(sha1
, sha1
, commit
);
128 return diff_tree_combined_merge(sha1
, header
,
129 dense_combined_merges
);
133 for (parents
= commit
->parents
; parents
; parents
= parents
->next
) {
134 struct commit
*parent
= parents
->item
;
135 header
= generate_header(sha1
, parent
->object
.sha1
, commit
);
136 diff_tree_sha1_top(parent
->object
.sha1
, commit_sha1
, "");
137 if (!header
&& verbose_header
) {
138 header_prefix
= "\ndiff-tree ";
140 * Don't print multiple merge entries if we
141 * don't print the diffs.
148 static int diff_tree_stdin(char *line
)
150 int len
= strlen(line
);
151 unsigned char commit
[20], parent
[20];
152 static char this_header
[1000];
153 int abbrev
= diff_options
.abbrev
;
155 if (!len
|| line
[len
-1] != '\n')
158 if (get_sha1_hex(line
, commit
))
160 if (isspace(line
[40]) && !get_sha1_hex(line
+41, parent
)) {
163 sprintf(this_header
, "%s (from %s)\n",
164 diff_unique_abbrev(commit
, abbrev
),
165 diff_unique_abbrev(parent
, abbrev
));
166 header
= this_header
;
167 return diff_tree_sha1_top(parent
, commit
, "");
170 return diff_tree_commit(commit
);
173 static const char diff_tree_usage
[] =
174 "git-diff-tree [--stdin] [-m] [-c] [--cc] [-s] [-v] [--pretty] [-t] [-r] [--root] "
175 "[<common diff options>] <tree-ish> [<tree-ish>] [<path>...]\n"
176 " -r diff recursively\n"
177 " --root include the initial commit as diff against /dev/null\n"
178 COMMON_DIFF_OPTIONS_HELP
;
180 int main(int argc
, const char **argv
)
184 unsigned char sha1
[2][20];
185 const char *prefix
= setup_git_directory();
187 git_config(git_diff_config
);
189 diff_setup(&diff_options
);
202 if (nr_sha1
< 2 && !get_sha1(arg
, sha1
[nr_sha1
])) {
209 diff_opt_cnt
= diff_opt_parse(&diff_options
, argv
, argc
);
210 if (diff_opt_cnt
< 0)
211 usage(diff_tree_usage
);
212 else if (diff_opt_cnt
) {
213 argv
+= diff_opt_cnt
- 1;
214 argc
-= diff_opt_cnt
- 1;
219 if (!strcmp(arg
, "--")) {
224 if (!strcmp(arg
, "-r")) {
225 diff_options
.recursive
= 1;
228 if (!strcmp(arg
, "-t")) {
229 diff_options
.recursive
= 1;
230 diff_options
.tree_in_recursive
= 1;
233 if (!strcmp(arg
, "-m")) {
237 if (!strcmp(arg
, "-c")) {
241 if (!strcmp(arg
, "--cc")) {
242 dense_combined_merges
= combine_merges
= 1;
245 if (!strcmp(arg
, "-v")) {
247 header_prefix
= "diff-tree ";
250 if (!strncmp(arg
, "--pretty", 8)) {
252 header_prefix
= "diff-tree ";
253 commit_format
= get_commit_format(arg
+8);
256 if (!strcmp(arg
, "--stdin")) {
260 if (!strcmp(arg
, "--root")) {
264 if (!strcmp(arg
, "--no-commit-id")) {
268 if (!strcmp(arg
, "--always")) {
269 always_show_header
= 1;
272 usage(diff_tree_usage
);
274 if (diff_options
.output_format
== DIFF_FORMAT_PATCH
)
275 diff_options
.recursive
= 1;
277 if (combine_merges
) {
278 diff_options
.output_format
= DIFF_FORMAT_PATCH
;
282 diff_tree_setup_paths(get_pathspec(prefix
, argv
));
283 diff_setup_done(&diff_options
);
288 usage(diff_tree_usage
);
291 diff_tree_commit(sha1
[0]);
294 diff_tree_sha1_top(sha1
[0], sha1
[1], "");
301 if (diff_options
.detect_rename
)
302 diff_options
.setup
|= (DIFF_SETUP_USE_SIZE_CACHE
|
303 DIFF_SETUP_USE_CACHE
);
304 while (fgets(line
, sizeof(line
), stdin
))
305 diff_tree_stdin(line
);