]> git.ipfire.org Git - thirdparty/git.git/blame - diff-tree.c
combine-diff: minor output changes.
[thirdparty/git.git] / diff-tree.c
CommitLineData
9174026c 1#include "cache.h"
3ebfd4aa 2#include "diff.h"
e3bc7a3b 3#include "commit.h"
9174026c 4
dc26bd89 5static int show_root_diff = 0;
601c978c 6static int no_commit_id = 0;
cee99d22 7static int verbose_header = 0;
e0965d83 8static int ignore_merges = 1;
af3feefa
JH
9static int show_empty_combined = 0;
10static int combine_merges = 0;
d8f4790e 11static int dense_combined_merges = 0;
e0965d83 12static int read_stdin = 0;
6b5ee137 13
f4f21ce3 14static const char *header = NULL;
cee99d22 15static const char *header_prefix = "";
000182ea 16static enum cmit_fmt commit_format = CMIT_FMT_RAW;
9174026c 17
ac1b3d12 18static struct diff_options diff_options;
73134b6d 19
09d74b3b 20static int call_diff_flush(void)
38c6f780 21{
6b5ee137 22 diffcore_std(&diff_options);
9ab55bd2 23 if (diff_queue_is_empty()) {
6b5ee137
JH
24 int saved_fmt = diff_options.output_format;
25 diff_options.output_format = DIFF_FORMAT_NO_OUTPUT;
26 diff_flush(&diff_options);
27 diff_options.output_format = saved_fmt;
9ab55bd2 28 return 0;
6b14d7fa 29 }
6b14d7fa 30 if (header) {
601c978c
PR
31 if (!no_commit_id)
32 printf("%s%c", header, diff_options.line_termination);
6b14d7fa
JH
33 header = NULL;
34 }
6b5ee137 35 diff_flush(&diff_options);
6b14d7fa 36 return 1;
38c6f780
JH
37}
38
5c97558c
JH
39static int diff_tree_sha1_top(const unsigned char *old,
40 const unsigned char *new, const char *base)
41{
42 int ret;
57fe64a4 43
ac1b3d12 44 ret = diff_tree_sha1(old, new, base, &diff_options);
38c6f780 45 call_diff_flush();
5c97558c
JH
46 return ret;
47}
48
dc26bd89
LT
49static int diff_root_tree(const unsigned char *new, const char *base)
50{
51 int retval;
52 void *tree;
ac1b3d12 53 struct tree_desc empty, real;
dc26bd89 54
ac1b3d12 55 tree = read_object_with_reference(new, "tree", &real.size, NULL);
dc26bd89
LT
56 if (!tree)
57 die("unable to read root tree (%s)", sha1_to_hex(new));
ac1b3d12
LT
58 real.buf = tree;
59
60 empty.buf = "";
61 empty.size = 0;
62 retval = diff_tree(&empty, &real, base, &diff_options);
dc26bd89 63 free(tree);
38c6f780 64 call_diff_flush();
dc26bd89
LT
65 return retval;
66}
67
47dd0d59
JH
68static const char *generate_header(const unsigned char *commit_sha1,
69 const unsigned char *parent_sha1,
70 const char *msg)
cee99d22 71{
3258c902 72 static char this_header[16384];
cee99d22 73 int offset;
a50b870a 74 unsigned long len;
47dd0d59 75 int abbrev = diff_options.abbrev;
cee99d22 76
18092663 77 if (!verbose_header)
47dd0d59 78 return sha1_to_hex(commit_sha1);
cee99d22 79
a50b870a 80 len = strlen(msg);
47dd0d59
JH
81
82 offset = sprintf(this_header, "%s%s ",
83 header_prefix,
84 diff_unique_abbrev(commit_sha1, abbrev));
af3feefa
JH
85 if (commit_sha1 != parent_sha1)
86 offset += sprintf(this_header + offset, "(from %s)\n",
87 parent_sha1
88 ? diff_unique_abbrev(parent_sha1, abbrev)
89 : "root");
90 else
91 offset += sprintf(this_header + offset, "(from parents)\n");
47dd0d59
JH
92 offset += pretty_print_commit(commit_format, msg, len,
93 this_header + offset,
94 sizeof(this_header) - offset);
cee99d22
LT
95 return this_header;
96}
97
a50b870a 98static int diff_tree_commit(const unsigned char *commit_sha1)
e0965d83 99{
a50b870a
JH
100 struct commit *commit;
101 struct commit_list *parents;
102 char name[50];
103 unsigned char sha1[20];
e0965d83 104
a50b870a
JH
105 sprintf(name, "%s^0", sha1_to_hex(commit_sha1));
106 if (get_sha1(name, sha1))
e0965d83 107 return -1;
a50b870a
JH
108 name[40] = 0;
109 commit = lookup_commit(sha1);
110
dc26bd89 111 /* Root commit? */
a50b870a 112 if (show_root_diff && !commit->parents) {
47dd0d59 113 header = generate_header(sha1, NULL, commit->buffer);
a50b870a 114 diff_root_tree(commit_sha1, "");
dc26bd89
LT
115 }
116
117 /* More than one parent? */
af3feefa
JH
118 if (commit->parents && commit->parents->next) {
119 if (ignore_merges)
120 return 0;
121 else if (combine_merges) {
122 header = generate_header(sha1, sha1,
123 commit->buffer);
124 return diff_tree_combined_merge(sha1, header,
d8f4790e
JH
125 show_empty_combined,
126 dense_combined_merges);
af3feefa
JH
127 }
128 }
dc26bd89 129
a50b870a
JH
130 for (parents = commit->parents; parents; parents = parents->next) {
131 struct commit *parent = parents->item;
47dd0d59
JH
132 header = generate_header(sha1,
133 parent->object.sha1,
a50b870a
JH
134 commit->buffer);
135 diff_tree_sha1_top(parent->object.sha1, commit_sha1, "");
d6db0107 136 if (!header && verbose_header) {
cee99d22 137 header_prefix = "\ndiff-tree ";
d6db0107
LT
138 /*
139 * Don't print multiple merge entries if we
140 * don't print the diffs.
141 */
d6db0107 142 }
e0965d83 143 }
b11645be
LT
144 return 0;
145}
146
147static int diff_tree_stdin(char *line)
148{
149 int len = strlen(line);
150 unsigned char commit[20], parent[20];
151 static char this_header[1000];
47dd0d59 152 int abbrev = diff_options.abbrev;
b11645be
LT
153
154 if (!len || line[len-1] != '\n')
155 return -1;
156 line[len-1] = 0;
157 if (get_sha1_hex(line, commit))
158 return -1;
159 if (isspace(line[40]) && !get_sha1_hex(line+41, parent)) {
160 line[40] = 0;
161 line[81] = 0;
47dd0d59
JH
162 sprintf(this_header, "%s (from %s)\n",
163 diff_unique_abbrev(commit, abbrev),
164 diff_unique_abbrev(parent, abbrev));
b11645be 165 header = this_header;
5c97558c 166 return diff_tree_sha1_top(parent, commit, "");
b11645be
LT
167 }
168 line[40] = 0;
a50b870a 169 return diff_tree_commit(commit);
e0965d83
LT
170}
171
4d1f1190 172static const char diff_tree_usage[] =
d8f4790e 173"git-diff-tree [--stdin] [-m] [-c] [--cc] [-s] [-v] [--pretty] [-t] [-r] [--root] "
50b8e355
CS
174"[<common diff options>] <tree-ish> [<tree-ish>] [<path>...]\n"
175" -r diff recursively\n"
176" --root include the initial commit as diff against /dev/null\n"
dda2d79a 177COMMON_DIFF_OPTIONS_HELP;
a8db165e 178
6b5ee137 179int main(int argc, const char **argv)
73134b6d 180{
0a8365a1 181 int nr_sha1;
e0965d83 182 char line[1000];
0a8365a1 183 unsigned char sha1[2][20];
d288a700 184 const char *prefix = setup_git_directory();
73134b6d 185
9ce392f4 186 git_config(git_diff_config);
0a8365a1 187 nr_sha1 = 0;
6b5ee137
JH
188 diff_setup(&diff_options);
189
c5b42386 190 for (;;) {
6b5ee137 191 int diff_opt_cnt;
6b14d7fa 192 const char *arg;
c5b42386 193
e0965d83
LT
194 argv++;
195 argc--;
196 arg = *argv;
0a8365a1 197 if (!arg)
c5b42386
LT
198 break;
199
0a8365a1
LT
200 if (*arg != '-') {
201 if (nr_sha1 < 2 && !get_sha1(arg, sha1[nr_sha1])) {
202 nr_sha1++;
203 continue;
204 }
205 break;
206 }
207
6b5ee137
JH
208 diff_opt_cnt = diff_opt_parse(&diff_options, argv, argc);
209 if (diff_opt_cnt < 0)
210 usage(diff_tree_usage);
211 else if (diff_opt_cnt) {
212 argv += diff_opt_cnt - 1;
213 argc -= diff_opt_cnt - 1;
214 continue;
215 }
216
217
0a8365a1 218 if (!strcmp(arg, "--")) {
e0965d83
LT
219 argv++;
220 argc--;
221 break;
222 }
bf16c71e 223 if (!strcmp(arg, "-r")) {
ac1b3d12 224 diff_options.recursive = 1;
73134b6d
LT
225 continue;
226 }
4cae1a96 227 if (!strcmp(arg, "-t")) {
ac1b3d12
LT
228 diff_options.recursive = 1;
229 diff_options.tree_in_recursive = 1;
4cae1a96
JH
230 continue;
231 }
e0965d83
LT
232 if (!strcmp(arg, "-m")) {
233 ignore_merges = 0;
234 continue;
235 }
af3feefa
JH
236 if (!strcmp(arg, "-c")) {
237 combine_merges = 1;
238 continue;
239 }
d8f4790e
JH
240 if (!strcmp(arg, "--cc")) {
241 dense_combined_merges = combine_merges = 1;
242 continue;
243 }
cee99d22
LT
244 if (!strcmp(arg, "-v")) {
245 verbose_header = 1;
246 header_prefix = "diff-tree ";
247 continue;
248 }
a8db165e
JH
249 if (!strncmp(arg, "--pretty", 8)) {
250 verbose_header = 1;
ba88e54b 251 header_prefix = "diff-tree ";
a8db165e
JH
252 commit_format = get_commit_format(arg+8);
253 continue;
254 }
e0965d83
LT
255 if (!strcmp(arg, "--stdin")) {
256 read_stdin = 1;
257 continue;
258 }
dc26bd89
LT
259 if (!strcmp(arg, "--root")) {
260 show_root_diff = 1;
261 continue;
262 }
601c978c
PR
263 if (!strcmp(arg, "--no-commit-id")) {
264 no_commit_id = 1;
265 continue;
266 }
c5bac17a 267 usage(diff_tree_usage);
73134b6d 268 }
6b5ee137 269 if (diff_options.output_format == DIFF_FORMAT_PATCH)
ac1b3d12 270 diff_options.recursive = 1;
73134b6d 271
af3feefa
JH
272 if (combine_merges) {
273 diff_options.output_format = DIFF_FORMAT_PATCH;
274 show_empty_combined = !ignore_merges;
275 ignore_merges = 0;
276 }
277
ac1b3d12 278 diff_tree_setup_paths(get_pathspec(prefix, argv));
47dd0d59 279 diff_setup_done(&diff_options);
c5b42386 280
0a8365a1
LT
281 switch (nr_sha1) {
282 case 0:
283 if (!read_stdin)
284 usage(diff_tree_usage);
285 break;
286 case 1:
a50b870a 287 diff_tree_commit(sha1[0]);
0a8365a1
LT
288 break;
289 case 2:
5c97558c 290 diff_tree_sha1_top(sha1[0], sha1[1], "");
0a8365a1
LT
291 break;
292 }
293
e0965d83 294 if (!read_stdin)
0a8365a1 295 return 0;
e0965d83 296
6b5ee137
JH
297 if (diff_options.detect_rename)
298 diff_options.setup |= (DIFF_SETUP_USE_SIZE_CACHE |
299 DIFF_SETUP_USE_CACHE);
e0965d83
LT
300 while (fgets(line, sizeof(line), stdin))
301 diff_tree_stdin(line);
302
303 return 0;
9174026c 304}