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