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