1#include "cache.h"
2#include "diff.h"
3#include "commit.h"
45
static int show_root_diff = 0;
6static int no_commit_id = 0;
7static int verbose_header = 0;
8static int ignore_merges = 1;
9static int show_empty_combined = 0;
10static int combine_merges = 0;
11static int read_stdin = 0;
1213
static const char *header = NULL;
14static const char *header_prefix = "";
15static enum cmit_fmt commit_format = CMIT_FMT_RAW;
1617
static struct diff_options diff_options;
1819
static int call_diff_flush(void)
20{
21diffcore_std(&diff_options);
22if (diff_queue_is_empty()) {
23int saved_fmt = diff_options.output_format;
24diff_options.output_format = DIFF_FORMAT_NO_OUTPUT;
25diff_flush(&diff_options);
26diff_options.output_format = saved_fmt;
27return 0;
28}
29if (header) {
30if (!no_commit_id)
31printf("%s%c", header, diff_options.line_termination);
32header = NULL;
33}
34diff_flush(&diff_options);
35return 1;
36}
3738
static int diff_tree_sha1_top(const unsigned char *old,
39const unsigned char *new, const char *base)
40{
41int ret;
4243
ret = diff_tree_sha1(old, new, base, &diff_options);
44call_diff_flush();
45return ret;
46}
4748
static int diff_root_tree(const unsigned char *new, const char *base)
49{
50int retval;
51void *tree;
52struct tree_desc empty, real;
5354
tree = read_object_with_reference(new, "tree", &real.size, NULL);
55if (!tree)
56die("unable to read root tree (%s)", sha1_to_hex(new));
57real.buf = tree;
5859
empty.buf = "";
60empty.size = 0;
61retval = diff_tree(&empty, &real, base, &diff_options);
62free(tree);
63call_diff_flush();
64return retval;
65}
6667
static const char *generate_header(const unsigned char *commit_sha1,
68const unsigned char *parent_sha1,
69const char *msg)
70{
71static char this_header[16384];
72int offset;
73unsigned long len;
74int abbrev = diff_options.abbrev;
7576
if (!verbose_header)
77return sha1_to_hex(commit_sha1);
7879
len = strlen(msg);
8081
offset = sprintf(this_header, "%s%s ",
82header_prefix,
83diff_unique_abbrev(commit_sha1, abbrev));
84if (commit_sha1 != parent_sha1)
85offset += sprintf(this_header + offset, "(from %s)\n",
86parent_sha1
87? diff_unique_abbrev(parent_sha1, abbrev)
88: "root");
89else
90offset += sprintf(this_header + offset, "(from parents)\n");
91offset += pretty_print_commit(commit_format, msg, len,
92this_header + offset,
93sizeof(this_header) - offset);
94return this_header;
95}
9697
static int diff_tree_commit(const unsigned char *commit_sha1)
98{
99struct commit *commit;
100struct commit_list *parents;
101char name[50];
102unsigned char sha1[20];
103104
sprintf(name, "%s^0", sha1_to_hex(commit_sha1));
105if (get_sha1(name, sha1))
106return -1;
107name[40] = 0;
108commit = lookup_commit(sha1);
109110
/* Root commit? */
111if (show_root_diff && !commit->parents) {
112header = generate_header(sha1, NULL, commit->buffer);
113diff_root_tree(commit_sha1, "");
114}
115116
/* More than one parent? */
117if (commit->parents && commit->parents->next) {
118if (ignore_merges)
119return 0;
120else if (combine_merges) {
121header = generate_header(sha1, sha1,
122commit->buffer);
123return diff_tree_combined_merge(sha1, header,
124show_empty_combined);
125}
126}
127128
for (parents = commit->parents; parents; parents = parents->next) {
129struct commit *parent = parents->item;
130header = generate_header(sha1,
131parent->object.sha1,
132commit->buffer);
133diff_tree_sha1_top(parent->object.sha1, commit_sha1, "");
134if (!header && verbose_header) {
135header_prefix = "\ndiff-tree ";
136/*
137* Don't print multiple merge entries if we
138* don't print the diffs.
139*/
140}
141}
142return 0;
143}
144145
static int diff_tree_stdin(char *line)
146{
147int len = strlen(line);
148unsigned char commit[20], parent[20];
149static char this_header[1000];
150int abbrev = diff_options.abbrev;
151152
if (!len || line[len-1] != '\n')
153return -1;
154line[len-1] = 0;
155if (get_sha1_hex(line, commit))
156return -1;
157if (isspace(line[40]) && !get_sha1_hex(line+41, parent)) {
158line[40] = 0;
159line[81] = 0;
160sprintf(this_header, "%s (from %s)\n",
161diff_unique_abbrev(commit, abbrev),
162diff_unique_abbrev(parent, abbrev));
163header = this_header;
164return diff_tree_sha1_top(parent, commit, "");
165}
166line[40] = 0;
167return diff_tree_commit(commit);
168}
169170
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"
175COMMON_DIFF_OPTIONS_HELP;
176177
int main(int argc, const char **argv)
178{
179int nr_sha1;
180char line[1000];
181unsigned char sha1[2][20];
182const char *prefix = setup_git_directory();
183184
git_config(git_diff_config);
185nr_sha1 = 0;
186diff_setup(&diff_options);
187188
for (;;) {
189int diff_opt_cnt;
190const char *arg;
191192
argv++;
193argc--;
194arg = *argv;
195if (!arg)
196break;
197198
if (*arg != '-') {
199if (nr_sha1 < 2 && !get_sha1(arg, sha1[nr_sha1])) {
200nr_sha1++;
201continue;
202}
203break;
204}
205206
diff_opt_cnt = diff_opt_parse(&diff_options, argv, argc);
207if (diff_opt_cnt < 0)
208usage(diff_tree_usage);
209else if (diff_opt_cnt) {
210argv += diff_opt_cnt - 1;
211argc -= diff_opt_cnt - 1;
212continue;
213}
214215
216
if (!strcmp(arg, "--")) {
217argv++;
218argc--;
219break;
220}
221if (!strcmp(arg, "-r")) {
222diff_options.recursive = 1;
223continue;
224}
225if (!strcmp(arg, "-t")) {
226diff_options.recursive = 1;
227diff_options.tree_in_recursive = 1;
228continue;
229}
230if (!strcmp(arg, "-m")) {
231ignore_merges = 0;
232continue;
233}
234if (!strcmp(arg, "-c")) {
235combine_merges = 1;
236continue;
237}
238if (!strcmp(arg, "-v")) {
239verbose_header = 1;
240header_prefix = "diff-tree ";
241continue;
242}
243if (!strncmp(arg, "--pretty", 8)) {
244verbose_header = 1;
245header_prefix = "diff-tree ";
246commit_format = get_commit_format(arg+8);
247continue;
248}
249if (!strcmp(arg, "--stdin")) {
250read_stdin = 1;
251continue;
252}
253if (!strcmp(arg, "--root")) {
254show_root_diff = 1;
255continue;
256}
257if (!strcmp(arg, "--no-commit-id")) {
258no_commit_id = 1;
259continue;
260}
261usage(diff_tree_usage);
262}
263if (diff_options.output_format == DIFF_FORMAT_PATCH)
264diff_options.recursive = 1;
265266
if (combine_merges) {
267diff_options.output_format = DIFF_FORMAT_PATCH;
268show_empty_combined = !ignore_merges;
269ignore_merges = 0;
270}
271272
diff_tree_setup_paths(get_pathspec(prefix, argv));
273diff_setup_done(&diff_options);
274275
switch (nr_sha1) {
276case 0:
277if (!read_stdin)
278usage(diff_tree_usage);
279break;
280case 1:
281diff_tree_commit(sha1[0]);
282break;
283case 2:
284diff_tree_sha1_top(sha1[0], sha1[1], "");
285break;
286}
287288
if (!read_stdin)
289return 0;
290291
if (diff_options.detect_rename)
292diff_options.setup |= (DIFF_SETUP_USE_SIZE_CACHE |
293DIFF_SETUP_USE_CACHE);
294while (fgets(line, sizeof(line), stdin))
295diff_tree_stdin(line);
296297
return 0;
298}