Fix bug introduced by the latest changes to git-pack-redundant
[git.git] / diff-tree.c
1 #include "cache.h"
2 #include "diff.h"
3 #include "commit.h"
4
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;
10
11 static const char *header = NULL;
12 static const char *header_prefix = "";
13 static enum cmit_fmt commit_format = CMIT_FMT_RAW;
14
15 static struct diff_options diff_options;
16
17 static void call_diff_setup_done(void)
18 {
19         diff_setup_done(&diff_options);
20 }
21
22 static int call_diff_flush(void)
23 {
24         diffcore_std(&diff_options);
25         if (diff_queue_is_empty()) {
26                 int saved_fmt = diff_options.output_format;
27                 diff_options.output_format = DIFF_FORMAT_NO_OUTPUT;
28                 diff_flush(&diff_options);
29                 diff_options.output_format = saved_fmt;
30                 return 0;
31         }
32         if (header) {
33                 if (!no_commit_id)
34                         printf("%s%c", header, diff_options.line_termination);
35                 header = NULL;
36         }
37         diff_flush(&diff_options);
38         return 1;
39 }
40
41 static int diff_tree_sha1_top(const unsigned char *old,
42                               const unsigned char *new, const char *base)
43 {
44         int ret;
45
46         call_diff_setup_done();
47         ret = diff_tree_sha1(old, new, base, &diff_options);
48         call_diff_flush();
49         return ret;
50 }
51
52 static int diff_root_tree(const unsigned char *new, const char *base)
53 {
54         int retval;
55         void *tree;
56         struct tree_desc empty, real;
57
58         call_diff_setup_done();
59         tree = read_object_with_reference(new, "tree", &real.size, NULL);
60         if (!tree)
61                 die("unable to read root tree (%s)", sha1_to_hex(new));
62         real.buf = tree;
63
64         empty.buf = "";
65         empty.size = 0;
66         retval = diff_tree(&empty, &real, base, &diff_options);
67         free(tree);
68         call_diff_flush();
69         return retval;
70 }
71
72 static const char *generate_header(const char *commit, const char *parent, const char *msg, unsigned long len)
73 {
74         static char this_header[16384];
75         int offset;
76
77         if (!verbose_header)
78                 return commit;
79
80         offset = sprintf(this_header, "%s%s (from %s)\n", header_prefix, commit, parent);
81         offset += pretty_print_commit(commit_format, msg, len, this_header + offset, sizeof(this_header) - offset);
82         return this_header;
83 }
84
85 static int diff_tree_commit(const unsigned char *commit, const char *name)
86 {
87         unsigned long size, offset;
88         char *buf = read_object_with_reference(commit, "commit", &size, NULL);
89
90         if (!buf)
91                 return -1;
92
93         if (!name) {
94                 static char commit_name[60];
95                 strcpy(commit_name, sha1_to_hex(commit));
96                 name = commit_name;
97         }
98
99         /* Root commit? */
100         if (show_root_diff && memcmp(buf + 46, "parent ", 7)) {
101                 header = generate_header(name, "root", buf, size);
102                 diff_root_tree(commit, "");
103         }
104
105         /* More than one parent? */
106         if (ignore_merges) {
107                 if (!memcmp(buf + 46 + 48, "parent ", 7))
108                         return 0;
109         }
110
111         offset = 46;
112         while (offset + 48 < size && !memcmp(buf + offset, "parent ", 7)) {
113                 unsigned char parent[20];
114                 if (get_sha1_hex(buf + offset + 7, parent))
115                         return -1;
116                 header = generate_header(name, sha1_to_hex(parent), buf, size);
117                 diff_tree_sha1_top(parent, commit, "");
118                 if (!header && verbose_header) {
119                         header_prefix = "\ndiff-tree ";
120                         /*
121                          * Don't print multiple merge entries if we
122                          * don't print the diffs.
123                          */
124                 }
125                 offset += 48;
126         }
127         free(buf);
128         return 0;
129 }
130
131 static int diff_tree_stdin(char *line)
132 {
133         int len = strlen(line);
134         unsigned char commit[20], parent[20];
135         static char this_header[1000];
136
137         if (!len || line[len-1] != '\n')
138                 return -1;
139         line[len-1] = 0;
140         if (get_sha1_hex(line, commit))
141                 return -1;
142         if (isspace(line[40]) && !get_sha1_hex(line+41, parent)) {
143                 line[40] = 0;
144                 line[81] = 0;
145                 sprintf(this_header, "%s (from %s)\n", line, line+41);
146                 header = this_header;
147                 return diff_tree_sha1_top(parent, commit, "");
148         }
149         line[40] = 0;
150         return diff_tree_commit(commit, line);
151 }
152
153 static const char diff_tree_usage[] =
154 "git-diff-tree [--stdin] [-m] [-s] [-v] [--pretty] [-t] [-r] [--root] "
155 "[<common diff options>] <tree-ish> [<tree-ish>] [<path>...]\n"
156 "  -r            diff recursively\n"
157 "  --root        include the initial commit as diff against /dev/null\n"
158 COMMON_DIFF_OPTIONS_HELP;
159
160 int main(int argc, const char **argv)
161 {
162         int nr_sha1;
163         char line[1000];
164         unsigned char sha1[2][20];
165         const char *prefix = setup_git_directory();
166
167         git_config(git_default_config);
168         nr_sha1 = 0;
169         diff_setup(&diff_options);
170
171         for (;;) {
172                 int diff_opt_cnt;
173                 const char *arg;
174
175                 argv++;
176                 argc--;
177                 arg = *argv;
178                 if (!arg)
179                         break;
180
181                 if (*arg != '-') {
182                         if (nr_sha1 < 2 && !get_sha1(arg, sha1[nr_sha1])) {
183                                 nr_sha1++;
184                                 continue;
185                         }
186                         break;
187                 }
188
189                 diff_opt_cnt = diff_opt_parse(&diff_options, argv, argc);
190                 if (diff_opt_cnt < 0)
191                         usage(diff_tree_usage);
192                 else if (diff_opt_cnt) {
193                         argv += diff_opt_cnt - 1;
194                         argc -= diff_opt_cnt - 1;
195                         continue;
196                 }
197
198
199                 if (!strcmp(arg, "--")) {
200                         argv++;
201                         argc--;
202                         break;
203                 }
204                 if (!strcmp(arg, "-r")) {
205                         diff_options.recursive = 1;
206                         continue;
207                 }
208                 if (!strcmp(arg, "-t")) {
209                         diff_options.recursive = 1;
210                         diff_options.tree_in_recursive = 1;
211                         continue;
212                 }
213                 if (!strcmp(arg, "-m")) {
214                         ignore_merges = 0;
215                         continue;
216                 }
217                 if (!strcmp(arg, "-v")) {
218                         verbose_header = 1;
219                         header_prefix = "diff-tree ";
220                         continue;
221                 }
222                 if (!strncmp(arg, "--pretty", 8)) {
223                         verbose_header = 1;
224                         header_prefix = "diff-tree ";
225                         commit_format = get_commit_format(arg+8);
226                         continue;
227                 }
228                 if (!strcmp(arg, "--stdin")) {
229                         read_stdin = 1;
230                         continue;
231                 }
232                 if (!strcmp(arg, "--root")) {
233                         show_root_diff = 1;
234                         continue;
235                 }
236                 if (!strcmp(arg, "--no-commit-id")) {
237                         no_commit_id = 1;
238                         continue;
239                 }
240                 usage(diff_tree_usage);
241         }
242         if (diff_options.output_format == DIFF_FORMAT_PATCH)
243                 diff_options.recursive = 1;
244
245         diff_tree_setup_paths(get_pathspec(prefix, argv));
246
247         switch (nr_sha1) {
248         case 0:
249                 if (!read_stdin)
250                         usage(diff_tree_usage);
251                 break;
252         case 1:
253                 diff_tree_commit(sha1[0], NULL);
254                 break;
255         case 2:
256                 diff_tree_sha1_top(sha1[0], sha1[1], "");
257                 break;
258         }
259
260         if (!read_stdin)
261                 return 0;
262
263         if (diff_options.detect_rename)
264                 diff_options.setup |= (DIFF_SETUP_USE_SIZE_CACHE |
265                                        DIFF_SETUP_USE_CACHE);
266         while (fgets(line, sizeof(line), stdin))
267                 diff_tree_stdin(line);
268
269         return 0;
270 }