1#include "cache.h"
2#include "diff.h"
3#include "commit.h"
45
static int show_root_diff = 0;
6static int verbose_header = 0;
7static int ignore_merges = 1;
8static int read_stdin = 0;
910
static const char *header = NULL;
11static const char *header_prefix = "";
12static enum cmit_fmt commit_format = CMIT_FMT_RAW;
1314
static struct diff_options diff_options;
1516
static void call_diff_setup_done(void)
17{
18diff_setup_done(&diff_options);
19}
2021
static int call_diff_flush(void)
22{
23diffcore_std(&diff_options);
24if (diff_queue_is_empty()) {
25int saved_fmt = diff_options.output_format;
26diff_options.output_format = DIFF_FORMAT_NO_OUTPUT;
27diff_flush(&diff_options);
28diff_options.output_format = saved_fmt;
29return 0;
30}
31if (header) {
32printf("%s%c", header, diff_options.line_termination);
33header = NULL;
34}
35diff_flush(&diff_options);
36return 1;
37}
3839
static int diff_tree_sha1_top(const unsigned char *old,
40const unsigned char *new, const char *base)
41{
42int ret;
4344
call_diff_setup_done();
45ret = diff_tree_sha1(old, new, base, &diff_options);
46call_diff_flush();
47return ret;
48}
4950
static int diff_root_tree(const unsigned char *new, const char *base)
51{
52int retval;
53void *tree;
54struct tree_desc empty, real;
5556
call_diff_setup_done();
57tree = read_object_with_reference(new, "tree", &real.size, NULL);
58if (!tree)
59die("unable to read root tree (%s)", sha1_to_hex(new));
60real.buf = tree;
6162
empty.buf = "";
63empty.size = 0;
64retval = diff_tree(&empty, &real, base, &diff_options);
65free(tree);
66call_diff_flush();
67return retval;
68}
6970
static const char *generate_header(const char *commit, const char *parent, const char *msg, unsigned long len)
71{
72static char this_header[16384];
73int offset;
7475
if (!verbose_header)
76return commit;
7778
offset = sprintf(this_header, "%s%s (from %s)\n", header_prefix, commit, parent);
79offset += pretty_print_commit(commit_format, msg, len, this_header + offset, sizeof(this_header) - offset);
80return this_header;
81}
8283
static int diff_tree_commit(const unsigned char *commit, const char *name)
84{
85unsigned long size, offset;
86char *buf = read_object_with_reference(commit, "commit", &size, NULL);
8788
if (!buf)
89return -1;
9091
if (!name) {
92static char commit_name[60];
93strcpy(commit_name, sha1_to_hex(commit));
94name = commit_name;
95}
9697
/* Root commit? */
98if (show_root_diff && memcmp(buf + 46, "parent ", 7)) {
99header = generate_header(name, "root", buf, size);
100diff_root_tree(commit, "");
101}
102103
/* More than one parent? */
104if (ignore_merges) {
105if (!memcmp(buf + 46 + 48, "parent ", 7))
106return 0;
107}
108109
offset = 46;
110while (offset + 48 < size && !memcmp(buf + offset, "parent ", 7)) {
111unsigned char parent[20];
112if (get_sha1_hex(buf + offset + 7, parent))
113return -1;
114header = generate_header(name, sha1_to_hex(parent), buf, size);
115diff_tree_sha1_top(parent, commit, "");
116if (!header && verbose_header) {
117header_prefix = "\ndiff-tree ";
118/*
119* Don't print multiple merge entries if we
120* don't print the diffs.
121*/
122}
123offset += 48;
124}
125free(buf);
126return 0;
127}
128129
static int diff_tree_stdin(char *line)
130{
131int len = strlen(line);
132unsigned char commit[20], parent[20];
133static char this_header[1000];
134135
if (!len || line[len-1] != '\n')
136return -1;
137line[len-1] = 0;
138if (get_sha1_hex(line, commit))
139return -1;
140if (isspace(line[40]) && !get_sha1_hex(line+41, parent)) {
141line[40] = 0;
142line[81] = 0;
143sprintf(this_header, "%s (from %s)\n", line, line+41);
144header = this_header;
145return diff_tree_sha1_top(parent, commit, "");
146}
147line[40] = 0;
148return diff_tree_commit(commit, line);
149}
150151
static const char diff_tree_usage[] =
152"git-diff-tree [--stdin] [-m] [-s] [-v] [--pretty] [-t] [-r] [--root] "
153"[<common diff options>] <tree-ish> [<tree-ish>] [<path>...]\n"
154" -r diff recursively\n"
155" --root include the initial commit as diff against /dev/null\n"
156COMMON_DIFF_OPTIONS_HELP;
157158
int main(int argc, const char **argv)
159{
160int nr_sha1;
161char line[1000];
162unsigned char sha1[2][20];
163const char *prefix = setup_git_directory();
164165
git_config(git_default_config);
166nr_sha1 = 0;
167diff_setup(&diff_options);
168169
for (;;) {
170int diff_opt_cnt;
171const char *arg;
172173
argv++;
174argc--;
175arg = *argv;
176if (!arg)
177break;
178179
if (*arg != '-') {
180if (nr_sha1 < 2 && !get_sha1(arg, sha1[nr_sha1])) {
181nr_sha1++;
182continue;
183}
184break;
185}
186187
diff_opt_cnt = diff_opt_parse(&diff_options, argv, argc);
188if (diff_opt_cnt < 0)
189usage(diff_tree_usage);
190else if (diff_opt_cnt) {
191argv += diff_opt_cnt - 1;
192argc -= diff_opt_cnt - 1;
193continue;
194}
195196
197
if (!strcmp(arg, "--")) {
198argv++;
199argc--;
200break;
201}
202if (!strcmp(arg, "-r")) {
203diff_options.recursive = 1;
204continue;
205}
206if (!strcmp(arg, "-t")) {
207diff_options.recursive = 1;
208diff_options.tree_in_recursive = 1;
209continue;
210}
211if (!strcmp(arg, "-m")) {
212ignore_merges = 0;
213continue;
214}
215if (!strcmp(arg, "-v")) {
216verbose_header = 1;
217header_prefix = "diff-tree ";
218continue;
219}
220if (!strncmp(arg, "--pretty", 8)) {
221verbose_header = 1;
222header_prefix = "diff-tree ";
223commit_format = get_commit_format(arg+8);
224continue;
225}
226if (!strcmp(arg, "--stdin")) {
227read_stdin = 1;
228continue;
229}
230if (!strcmp(arg, "--root")) {
231show_root_diff = 1;
232continue;
233}
234usage(diff_tree_usage);
235}
236if (diff_options.output_format == DIFF_FORMAT_PATCH)
237diff_options.recursive = 1;
238239
diff_tree_setup_paths(get_pathspec(prefix, argv));
240241
switch (nr_sha1) {
242case 0:
243if (!read_stdin)
244usage(diff_tree_usage);
245break;
246case 1:
247diff_tree_commit(sha1[0], NULL);
248break;
249case 2:
250diff_tree_sha1_top(sha1[0], sha1[1], "");
251break;
252}
253254
if (!read_stdin)
255return 0;
256257
if (diff_options.detect_rename)
258diff_options.setup |= (DIFF_SETUP_USE_SIZE_CACHE |
259DIFF_SETUP_USE_CACHE);
260while (fgets(line, sizeof(line), stdin))
261diff_tree_stdin(line);
262263
return 0;
264}