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 show_empty_combined
= 0;
10 static int combine_merges
= 0;
11 static int read_stdin
= 0;
13 static const char *header
= NULL
;
14 static const char *header_prefix
= "";
15 static enum cmit_fmt commit_format
= CMIT_FMT_RAW
;
17 static struct diff_options diff_options
;
19 static int call_diff_flush(void)
21 diffcore_std(&diff_options
);
22 if (diff_queue_is_empty()) {
23 int saved_fmt
= diff_options
.output_format
;
24 diff_options
.output_format
= DIFF_FORMAT_NO_OUTPUT
;
25 diff_flush(&diff_options
);
26 diff_options
.output_format
= saved_fmt
;
31 printf("%s%c", header
, diff_options
.line_termination
);
34 diff_flush(&diff_options
);
38 static int diff_tree_sha1_top(const unsigned char *old
,
39 const unsigned char *new, const char *base
)
43 ret
= diff_tree_sha1(old
, new, base
, &diff_options
);
48 static int diff_root_tree(const unsigned char *new, const char *base
)
52 struct tree_desc empty
, real
;
54 tree
= read_object_with_reference(new, "tree", &real
.size
, NULL
);
56 die("unable to read root tree (%s)", sha1_to_hex(new));
61 retval
= diff_tree(&empty
, &real
, base
, &diff_options
);
67 static const char *generate_header(const unsigned char *commit_sha1
,
68 const unsigned char *parent_sha1
,
71 static char this_header
[16384];
74 int abbrev
= diff_options
.abbrev
;
77 return sha1_to_hex(commit_sha1
);
81 offset
= sprintf(this_header
, "%s%s ",
83 diff_unique_abbrev(commit_sha1
, abbrev
));
84 if (commit_sha1
!= parent_sha1
)
85 offset
+= sprintf(this_header
+ offset
, "(from %s)\n",
87 ? diff_unique_abbrev(parent_sha1
, abbrev
)
90 offset
+= sprintf(this_header
+ offset
, "(from parents)\n");
91 offset
+= pretty_print_commit(commit_format
, msg
, len
,
93 sizeof(this_header
) - offset
);
97 static int diff_tree_commit(const unsigned char *commit_sha1
)
99 struct commit
*commit
;
100 struct commit_list
*parents
;
102 unsigned char sha1
[20];
104 sprintf(name
, "%s^0", sha1_to_hex(commit_sha1
));
105 if (get_sha1(name
, sha1
))
108 commit
= lookup_commit(sha1
);
111 if (show_root_diff
&& !commit
->parents
) {
112 header
= generate_header(sha1
, NULL
, commit
->buffer
);
113 diff_root_tree(commit_sha1
, "");
116 /* More than one parent? */
117 if (commit
->parents
&& commit
->parents
->next
) {
120 else if (combine_merges
) {
121 header
= generate_header(sha1
, sha1
,
123 return diff_tree_combined_merge(sha1
, header
,
124 show_empty_combined
);
128 for (parents
= commit
->parents
; parents
; parents
= parents
->next
) {
129 struct commit
*parent
= parents
->item
;
130 header
= generate_header(sha1
,
133 diff_tree_sha1_top(parent
->object
.sha1
, commit_sha1
, "");
134 if (!header
&& verbose_header
) {
135 header_prefix
= "\ndiff-tree ";
137 * Don't print multiple merge entries if we
138 * don't print the diffs.
145 static int diff_tree_stdin(char *line
)
147 int len
= strlen(line
);
148 unsigned char commit
[20], parent
[20];
149 static char this_header
[1000];
150 int abbrev
= diff_options
.abbrev
;
152 if (!len
|| line
[len
-1] != '\n')
155 if (get_sha1_hex(line
, commit
))
157 if (isspace(line
[40]) && !get_sha1_hex(line
+41, parent
)) {
160 sprintf(this_header
, "%s (from %s)\n",
161 diff_unique_abbrev(commit
, abbrev
),
162 diff_unique_abbrev(parent
, abbrev
));
163 header
= this_header
;
164 return diff_tree_sha1_top(parent
, commit
, "");
167 return diff_tree_commit(commit
);
170 static const char diff_tree_usage
[] =
171 "git-diff-tree [--stdin] [-m] [-c] [-s] [-v] [--pretty] [-t] [-r] [--root] "
172 "[<common diff options>] <tree-ish> [<tree-ish>] [<path>...]\n"
173 " -r diff recursively\n"
174 " --root include the initial commit as diff against /dev/null\n"
175 COMMON_DIFF_OPTIONS_HELP
;
177 int main(int argc
, const char **argv
)
181 unsigned char sha1
[2][20];
182 const char *prefix
= setup_git_directory();
184 git_config(git_diff_config
);
186 diff_setup(&diff_options
);
199 if (nr_sha1
< 2 && !get_sha1(arg
, sha1
[nr_sha1
])) {
206 diff_opt_cnt
= diff_opt_parse(&diff_options
, argv
, argc
);
207 if (diff_opt_cnt
< 0)
208 usage(diff_tree_usage
);
209 else if (diff_opt_cnt
) {
210 argv
+= diff_opt_cnt
- 1;
211 argc
-= diff_opt_cnt
- 1;
216 if (!strcmp(arg
, "--")) {
221 if (!strcmp(arg
, "-r")) {
222 diff_options
.recursive
= 1;
225 if (!strcmp(arg
, "-t")) {
226 diff_options
.recursive
= 1;
227 diff_options
.tree_in_recursive
= 1;
230 if (!strcmp(arg
, "-m")) {
234 if (!strcmp(arg
, "-c")) {
238 if (!strcmp(arg
, "-v")) {
240 header_prefix
= "diff-tree ";
243 if (!strncmp(arg
, "--pretty", 8)) {
245 header_prefix
= "diff-tree ";
246 commit_format
= get_commit_format(arg
+8);
249 if (!strcmp(arg
, "--stdin")) {
253 if (!strcmp(arg
, "--root")) {
257 if (!strcmp(arg
, "--no-commit-id")) {
261 usage(diff_tree_usage
);
263 if (diff_options
.output_format
== DIFF_FORMAT_PATCH
)
264 diff_options
.recursive
= 1;
266 if (combine_merges
) {
267 diff_options
.output_format
= DIFF_FORMAT_PATCH
;
268 show_empty_combined
= !ignore_merges
;
272 diff_tree_setup_paths(get_pathspec(prefix
, argv
));
273 diff_setup_done(&diff_options
);
278 usage(diff_tree_usage
);
281 diff_tree_commit(sha1
[0]);
284 diff_tree_sha1_top(sha1
[0], sha1
[1], "");
291 if (diff_options
.detect_rename
)
292 diff_options
.setup
|= (DIFF_SETUP_USE_SIZE_CACHE
|
293 DIFF_SETUP_USE_CACHE
);
294 while (fgets(line
, sizeof(line
), stdin
))
295 diff_tree_stdin(line
);