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 int call_diff_flush(void)
19 diffcore_std(&diff_options
);
20 if (diff_queue_is_empty()) {
21 int saved_fmt
= diff_options
.output_format
;
22 diff_options
.output_format
= DIFF_FORMAT_NO_OUTPUT
;
23 diff_flush(&diff_options
);
24 diff_options
.output_format
= saved_fmt
;
29 printf("%s%c", header
, diff_options
.line_termination
);
32 diff_flush(&diff_options
);
36 static int diff_tree_sha1_top(const unsigned char *old
,
37 const unsigned char *new, const char *base
)
41 ret
= diff_tree_sha1(old
, new, base
, &diff_options
);
46 static int diff_root_tree(const unsigned char *new, const char *base
)
50 struct tree_desc empty
, real
;
52 tree
= read_object_with_reference(new, "tree", &real
.size
, NULL
);
54 die("unable to read root tree (%s)", sha1_to_hex(new));
59 retval
= diff_tree(&empty
, &real
, base
, &diff_options
);
65 static const char *generate_header(const unsigned char *commit_sha1
,
66 const unsigned char *parent_sha1
,
69 static char this_header
[16384];
72 int abbrev
= diff_options
.abbrev
;
75 return sha1_to_hex(commit_sha1
);
79 offset
= sprintf(this_header
, "%s%s ",
81 diff_unique_abbrev(commit_sha1
, abbrev
));
82 offset
+= sprintf(this_header
+ offset
, "(from %s)\n",
84 diff_unique_abbrev(parent_sha1
, abbrev
) : "root");
85 offset
+= pretty_print_commit(commit_format
, msg
, len
,
87 sizeof(this_header
) - offset
);
91 static int diff_tree_commit(const unsigned char *commit_sha1
)
93 struct commit
*commit
;
94 struct commit_list
*parents
;
96 unsigned char sha1
[20];
98 sprintf(name
, "%s^0", sha1_to_hex(commit_sha1
));
99 if (get_sha1(name
, sha1
))
102 commit
= lookup_commit(sha1
);
105 if (show_root_diff
&& !commit
->parents
) {
106 header
= generate_header(sha1
, NULL
, commit
->buffer
);
107 diff_root_tree(commit_sha1
, "");
110 /* More than one parent? */
111 if (ignore_merges
&& commit
->parents
&& commit
->parents
->next
)
114 for (parents
= commit
->parents
; parents
; parents
= parents
->next
) {
115 struct commit
*parent
= parents
->item
;
116 header
= generate_header(sha1
,
119 diff_tree_sha1_top(parent
->object
.sha1
, commit_sha1
, "");
120 if (!header
&& verbose_header
) {
121 header_prefix
= "\ndiff-tree ";
123 * Don't print multiple merge entries if we
124 * 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];
136 int abbrev
= diff_options
.abbrev
;
138 if (!len
|| line
[len
-1] != '\n')
141 if (get_sha1_hex(line
, commit
))
143 if (isspace(line
[40]) && !get_sha1_hex(line
+41, parent
)) {
146 sprintf(this_header
, "%s (from %s)\n",
147 diff_unique_abbrev(commit
, abbrev
),
148 diff_unique_abbrev(parent
, abbrev
));
149 header
= this_header
;
150 return diff_tree_sha1_top(parent
, commit
, "");
153 return diff_tree_commit(commit
);
156 static const char diff_tree_usage
[] =
157 "git-diff-tree [--stdin] [-m] [-s] [-v] [--pretty] [-t] [-r] [--root] "
158 "[<common diff options>] <tree-ish> [<tree-ish>] [<path>...]\n"
159 " -r diff recursively\n"
160 " --root include the initial commit as diff against /dev/null\n"
161 COMMON_DIFF_OPTIONS_HELP
;
163 int main(int argc
, const char **argv
)
167 unsigned char sha1
[2][20];
168 const char *prefix
= setup_git_directory();
170 git_config(git_diff_config
);
172 diff_setup(&diff_options
);
185 if (nr_sha1
< 2 && !get_sha1(arg
, sha1
[nr_sha1
])) {
192 diff_opt_cnt
= diff_opt_parse(&diff_options
, argv
, argc
);
193 if (diff_opt_cnt
< 0)
194 usage(diff_tree_usage
);
195 else if (diff_opt_cnt
) {
196 argv
+= diff_opt_cnt
- 1;
197 argc
-= diff_opt_cnt
- 1;
202 if (!strcmp(arg
, "--")) {
207 if (!strcmp(arg
, "-r")) {
208 diff_options
.recursive
= 1;
211 if (!strcmp(arg
, "-t")) {
212 diff_options
.recursive
= 1;
213 diff_options
.tree_in_recursive
= 1;
216 if (!strcmp(arg
, "-m")) {
220 if (!strcmp(arg
, "-v")) {
222 header_prefix
= "diff-tree ";
225 if (!strncmp(arg
, "--pretty", 8)) {
227 header_prefix
= "diff-tree ";
228 commit_format
= get_commit_format(arg
+8);
231 if (!strcmp(arg
, "--stdin")) {
235 if (!strcmp(arg
, "--root")) {
239 if (!strcmp(arg
, "--no-commit-id")) {
243 usage(diff_tree_usage
);
245 if (diff_options
.output_format
== DIFF_FORMAT_PATCH
)
246 diff_options
.recursive
= 1;
248 diff_tree_setup_paths(get_pathspec(prefix
, argv
));
249 diff_setup_done(&diff_options
);
254 usage(diff_tree_usage
);
257 diff_tree_commit(sha1
[0]);
260 diff_tree_sha1_top(sha1
[0], sha1
[1], "");
267 if (diff_options
.detect_rename
)
268 diff_options
.setup
|= (DIFF_SETUP_USE_SIZE_CACHE
|
269 DIFF_SETUP_USE_CACHE
);
270 while (fgets(line
, sizeof(line
), stdin
))
271 diff_tree_stdin(line
);