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
,
67 const struct commit
*commit
)
69 static char this_header
[16384];
72 int abbrev
= diff_options
.abbrev
;
73 const char *msg
= commit
->buffer
;
76 return sha1_to_hex(commit_sha1
);
80 offset
= sprintf(this_header
, "%s%s ",
82 diff_unique_abbrev(commit_sha1
, abbrev
));
83 offset
+= sprintf(this_header
+ offset
, "(from %s)\n",
85 diff_unique_abbrev(parent_sha1
, abbrev
) : "root");
86 offset
+= pretty_print_commit(commit_format
, commit
, len
,
88 sizeof(this_header
) - offset
, abbrev
);
92 static int diff_tree_commit(const unsigned char *commit_sha1
)
94 struct commit
*commit
;
95 struct commit_list
*parents
;
97 unsigned char sha1
[20];
99 sprintf(name
, "%s^0", sha1_to_hex(commit_sha1
));
100 if (get_sha1(name
, sha1
))
103 commit
= lookup_commit(sha1
);
106 if (show_root_diff
&& !commit
->parents
) {
107 header
= generate_header(sha1
, NULL
, commit
);
108 diff_root_tree(commit_sha1
, "");
111 /* More than one parent? */
112 if (ignore_merges
&& commit
->parents
&& commit
->parents
->next
)
115 for (parents
= commit
->parents
; parents
; parents
= parents
->next
) {
116 struct commit
*parent
= parents
->item
;
117 header
= generate_header(sha1
, parent
->object
.sha1
, commit
);
118 diff_tree_sha1_top(parent
->object
.sha1
, commit_sha1
, "");
119 if (!header
&& verbose_header
) {
120 header_prefix
= "\ndiff-tree ";
122 * Don't print multiple merge entries if we
123 * don't print the diffs.
130 static int diff_tree_stdin(char *line
)
132 int len
= strlen(line
);
133 unsigned char commit
[20], parent
[20];
134 static char this_header
[1000];
135 int abbrev
= diff_options
.abbrev
;
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",
146 diff_unique_abbrev(commit
, abbrev
),
147 diff_unique_abbrev(parent
, abbrev
));
148 header
= this_header
;
149 return diff_tree_sha1_top(parent
, commit
, "");
152 return diff_tree_commit(commit
);
155 static const char diff_tree_usage
[] =
156 "git-diff-tree [--stdin] [-m] [-s] [-v] [--pretty] [-t] [-r] [--root] "
157 "[<common diff options>] <tree-ish> [<tree-ish>] [<path>...]\n"
158 " -r diff recursively\n"
159 " --root include the initial commit as diff against /dev/null\n"
160 COMMON_DIFF_OPTIONS_HELP
;
162 int main(int argc
, const char **argv
)
166 unsigned char sha1
[2][20];
167 const char *prefix
= setup_git_directory();
169 git_config(git_diff_config
);
171 diff_setup(&diff_options
);
184 if (nr_sha1
< 2 && !get_sha1(arg
, sha1
[nr_sha1
])) {
191 diff_opt_cnt
= diff_opt_parse(&diff_options
, argv
, argc
);
192 if (diff_opt_cnt
< 0)
193 usage(diff_tree_usage
);
194 else if (diff_opt_cnt
) {
195 argv
+= diff_opt_cnt
- 1;
196 argc
-= diff_opt_cnt
- 1;
201 if (!strcmp(arg
, "--")) {
206 if (!strcmp(arg
, "-r")) {
207 diff_options
.recursive
= 1;
210 if (!strcmp(arg
, "-t")) {
211 diff_options
.recursive
= 1;
212 diff_options
.tree_in_recursive
= 1;
215 if (!strcmp(arg
, "-m")) {
219 if (!strcmp(arg
, "-v")) {
221 header_prefix
= "diff-tree ";
224 if (!strncmp(arg
, "--pretty", 8)) {
226 header_prefix
= "diff-tree ";
227 commit_format
= get_commit_format(arg
+8);
230 if (!strcmp(arg
, "--stdin")) {
234 if (!strcmp(arg
, "--root")) {
238 if (!strcmp(arg
, "--no-commit-id")) {
242 usage(diff_tree_usage
);
244 if (diff_options
.output_format
== DIFF_FORMAT_PATCH
)
245 diff_options
.recursive
= 1;
247 diff_tree_setup_paths(get_pathspec(prefix
, argv
));
248 diff_setup_done(&diff_options
);
253 usage(diff_tree_usage
);
256 diff_tree_commit(sha1
[0]);
259 diff_tree_sha1_top(sha1
[0], sha1
[1], "");
266 if (diff_options
.detect_rename
)
267 diff_options
.setup
|= (DIFF_SETUP_USE_SIZE_CACHE
|
268 DIFF_SETUP_USE_CACHE
);
269 while (fgets(line
, sizeof(line
), stdin
))
270 diff_tree_stdin(line
);