blob: f55a35a9d5f9ae4e3f7d3fd778a953cc9cc533ad [file] [log] [blame]
Linus Torvalds91740262005-04-09 13:00:54 -07001#include "cache.h"
Junio C Hamano3ebfd4a2005-04-27 09:21:00 -07002#include "diff.h"
Linus Torvaldse3bc7a32005-06-01 08:34:23 -07003#include "commit.h"
Linus Torvalds91740262005-04-09 13:00:54 -07004
Linus Torvaldsdc26bd82005-05-19 13:44:29 -07005static int show_root_diff = 0;
Pavel Roskin601c9782005-11-10 00:30:12 -05006static int no_commit_id = 0;
Linus Torvaldscee99d22005-05-06 11:42:47 -07007static int verbose_header = 0;
Linus Torvaldse0965d82005-05-06 10:03:17 -07008static int ignore_merges = 1;
Junio C Hamano6932c782006-02-11 16:43:30 -08009static int combine_merges = 0;
Junio C Hamanod8f47902006-01-24 01:22:04 -080010static int dense_combined_merges = 0;
Linus Torvaldse0965d82005-05-06 10:03:17 -070011static int read_stdin = 0;
Linus Torvaldsec0bdb62006-02-05 11:42:09 -080012static int always_show_header = 0;
Junio C Hamano6b5ee132005-09-21 00:00:47 -070013
Linus Torvaldsf4f21ce2005-05-06 10:56:35 -070014static const char *header = NULL;
Linus Torvaldscee99d22005-05-06 11:42:47 -070015static const char *header_prefix = "";
Linus Torvalds000182e2005-06-05 09:02:03 -070016static enum cmit_fmt commit_format = CMIT_FMT_RAW;
Linus Torvalds91740262005-04-09 13:00:54 -070017
Linus Torvaldsac1b3d12005-10-20 21:05:05 -070018static struct diff_options diff_options;
Linus Torvalds73134b62005-04-10 14:03:58 -070019
Linus Torvalds09d74b32005-05-22 14:33:43 -070020static int call_diff_flush(void)
Junio C Hamano38c6f782005-05-21 19:40:36 -070021{
Junio C Hamano6b5ee132005-09-21 00:00:47 -070022 diffcore_std(&diff_options);
Linus Torvalds9ab55bd2005-05-23 16:37:47 -070023 if (diff_queue_is_empty()) {
Junio C Hamano6b5ee132005-09-21 00:00:47 -070024 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;
Linus Torvalds9ab55bd2005-05-23 16:37:47 -070028 return 0;
Junio C Hamano6b14d7f2005-05-22 10:04:37 -070029 }
Junio C Hamano6b14d7f2005-05-22 10:04:37 -070030 if (header) {
Pavel Roskin601c9782005-11-10 00:30:12 -050031 if (!no_commit_id)
32 printf("%s%c", header, diff_options.line_termination);
Junio C Hamano6b14d7f2005-05-22 10:04:37 -070033 header = NULL;
34 }
Junio C Hamano6b5ee132005-09-21 00:00:47 -070035 diff_flush(&diff_options);
Junio C Hamano6b14d7f2005-05-22 10:04:37 -070036 return 1;
Junio C Hamano38c6f782005-05-21 19:40:36 -070037}
38
Junio C Hamano5c975582005-05-19 03:32:35 -070039static int diff_tree_sha1_top(const unsigned char *old,
40 const unsigned char *new, const char *base)
41{
42 int ret;
Junio C Hamano57fe64a2005-05-19 19:00:36 -070043
Linus Torvaldsac1b3d12005-10-20 21:05:05 -070044 ret = diff_tree_sha1(old, new, base, &diff_options);
Junio C Hamano38c6f782005-05-21 19:40:36 -070045 call_diff_flush();
Junio C Hamano5c975582005-05-19 03:32:35 -070046 return ret;
47}
48
Linus Torvaldsdc26bd82005-05-19 13:44:29 -070049static int diff_root_tree(const unsigned char *new, const char *base)
50{
51 int retval;
52 void *tree;
Linus Torvaldsac1b3d12005-10-20 21:05:05 -070053 struct tree_desc empty, real;
Linus Torvaldsdc26bd82005-05-19 13:44:29 -070054
Linus Torvaldsac1b3d12005-10-20 21:05:05 -070055 tree = read_object_with_reference(new, "tree", &real.size, NULL);
Linus Torvaldsdc26bd82005-05-19 13:44:29 -070056 if (!tree)
57 die("unable to read root tree (%s)", sha1_to_hex(new));
Linus Torvaldsac1b3d12005-10-20 21:05:05 -070058 real.buf = tree;
59
60 empty.buf = "";
61 empty.size = 0;
62 retval = diff_tree(&empty, &real, base, &diff_options);
Linus Torvaldsdc26bd82005-05-19 13:44:29 -070063 free(tree);
Junio C Hamano38c6f782005-05-21 19:40:36 -070064 call_diff_flush();
Linus Torvaldsdc26bd82005-05-19 13:44:29 -070065 return retval;
66}
67
Junio C Hamano47dd0d52005-12-13 17:21:41 -080068static const char *generate_header(const unsigned char *commit_sha1,
69 const unsigned char *parent_sha1,
Junio C Hamano3815f422006-01-27 01:54:59 -080070 const struct commit *commit)
Linus Torvaldscee99d22005-05-06 11:42:47 -070071{
Linus Torvalds3258c902005-05-21 11:04:19 -070072 static char this_header[16384];
Linus Torvaldscee99d22005-05-06 11:42:47 -070073 int offset;
Junio C Hamanoa50b8702005-11-21 21:49:06 -080074 unsigned long len;
Junio C Hamano47dd0d52005-12-13 17:21:41 -080075 int abbrev = diff_options.abbrev;
Junio C Hamano3815f422006-01-27 01:54:59 -080076 const char *msg = commit->buffer;
Linus Torvaldscee99d22005-05-06 11:42:47 -070077
Linus Torvalds18092662005-06-23 13:56:55 -070078 if (!verbose_header)
Junio C Hamano47dd0d52005-12-13 17:21:41 -080079 return sha1_to_hex(commit_sha1);
Linus Torvaldscee99d22005-05-06 11:42:47 -070080
Junio C Hamanoa50b8702005-11-21 21:49:06 -080081 len = strlen(msg);
Junio C Hamano47dd0d52005-12-13 17:21:41 -080082
83 offset = sprintf(this_header, "%s%s ",
84 header_prefix,
85 diff_unique_abbrev(commit_sha1, abbrev));
Junio C Hamanoaf3feef2006-01-24 01:22:04 -080086 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");
Junio C Hamano3815f422006-01-27 01:54:59 -080093 offset += pretty_print_commit(commit_format, commit, len,
Junio C Hamano47dd0d52005-12-13 17:21:41 -080094 this_header + offset,
Junio C Hamanob2d4c562006-01-25 02:37:40 -080095 sizeof(this_header) - offset, abbrev);
Linus Torvaldsec0bdb62006-02-05 11:42:09 -080096 if (always_show_header) {
97 puts(this_header);
98 return NULL;
99 }
Linus Torvaldscee99d22005-05-06 11:42:47 -0700100 return this_header;
101}
102
Junio C Hamano45392a62006-02-05 23:00:41 -0800103static int diff_tree_commit(struct commit *commit)
Linus Torvaldsb11645b2005-05-18 13:06:47 -0700104{
Junio C Hamanoa50b8702005-11-21 21:49:06 -0800105 struct commit_list *parents;
Junio C Hamano45392a62006-02-05 23:00:41 -0800106 unsigned const char *sha1 = commit->object.sha1;
Linus Torvaldsb11645b2005-05-18 13:06:47 -0700107
Linus Torvaldsdc26bd82005-05-19 13:44:29 -0700108 /* Root commit? */
Junio C Hamanoa50b8702005-11-21 21:49:06 -0800109 if (show_root_diff && !commit->parents) {
Junio C Hamano3815f422006-01-27 01:54:59 -0800110 header = generate_header(sha1, NULL, commit);
Junio C Hamano45392a62006-02-05 23:00:41 -0800111 diff_root_tree(sha1, "");
Linus Torvaldsdc26bd82005-05-19 13:44:29 -0700112 }
113
114 /* More than one parent? */
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800115 if (commit->parents && commit->parents->next) {
116 if (ignore_merges)
117 return 0;
118 else if (combine_merges) {
Junio C Hamanoaddafaf2006-01-28 00:16:09 -0800119 header = generate_header(sha1, sha1, commit);
Linus Torvaldsee638022006-02-09 10:30:28 -0800120 header = diff_tree_combined_merge(sha1, header,
121 dense_combined_merges,
122 &diff_options);
123 if (!header && verbose_header)
124 header_prefix = "\ndiff-tree ";
125 return 0;
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800126 }
127 }
Linus Torvaldsdc26bd82005-05-19 13:44:29 -0700128
Junio C Hamanoa50b8702005-11-21 21:49:06 -0800129 for (parents = commit->parents; parents; parents = parents->next) {
130 struct commit *parent = parents->item;
Junio C Hamano3815f422006-01-27 01:54:59 -0800131 header = generate_header(sha1, parent->object.sha1, commit);
Junio C Hamano45392a62006-02-05 23:00:41 -0800132 diff_tree_sha1_top(parent->object.sha1, sha1, "");
Linus Torvaldsd6db0102005-05-21 15:42:53 -0700133 if (!header && verbose_header) {
Linus Torvaldsb11645b2005-05-18 13:06:47 -0700134 header_prefix = "\ndiff-tree ";
Linus Torvaldsd6db0102005-05-21 15:42:53 -0700135 /*
136 * Don't print multiple merge entries if we
137 * don't print the diffs.
138 */
Linus Torvaldsd6db0102005-05-21 15:42:53 -0700139 }
Linus Torvaldsb11645b2005-05-18 13:06:47 -0700140 }
141 return 0;
142}
143
Junio C Hamano45392a62006-02-05 23:00:41 -0800144static int diff_tree_commit_sha1(const unsigned char *sha1)
145{
146 struct commit *commit = lookup_commit_reference(sha1);
147 if (!commit)
148 return -1;
149 return diff_tree_commit(commit);
150}
151
Linus Torvaldse0965d82005-05-06 10:03:17 -0700152static int diff_tree_stdin(char *line)
153{
154 int len = strlen(line);
Junio C Hamano45392a62006-02-05 23:00:41 -0800155 unsigned char sha1[20];
156 struct commit *commit;
Linus Torvaldse0965d82005-05-06 10:03:17 -0700157
158 if (!len || line[len-1] != '\n')
159 return -1;
160 line[len-1] = 0;
Junio C Hamano45392a62006-02-05 23:00:41 -0800161 if (get_sha1_hex(line, sha1))
Linus Torvaldse0965d82005-05-06 10:03:17 -0700162 return -1;
Junio C Hamano45392a62006-02-05 23:00:41 -0800163 commit = lookup_commit(sha1);
164 if (!commit || parse_commit(commit))
165 return -1;
166 if (isspace(line[40]) && !get_sha1_hex(line+41, sha1)) {
167 /* Graft the fake parents locally to the commit */
168 int pos = 41;
169 struct commit_list **pptr, *parents;
170
171 /* Free the real parent list */
172 for (parents = commit->parents; parents; ) {
173 struct commit_list *tmp = parents->next;
174 free(parents);
175 parents = tmp;
176 }
177 commit->parents = NULL;
178 pptr = &(commit->parents);
179 while (line[pos] && !get_sha1_hex(line + pos, sha1)) {
180 struct commit *parent = lookup_commit(sha1);
181 if (parent) {
182 pptr = &commit_list_insert(parent, pptr)->next;
183 }
184 pos += 41;
185 }
Linus Torvaldse0965d82005-05-06 10:03:17 -0700186 }
Junio C Hamanoa50b8702005-11-21 21:49:06 -0800187 return diff_tree_commit(commit);
Linus Torvaldse0965d82005-05-06 10:03:17 -0700188}
189
Petr Baudis4d1f1192005-07-29 11:01:26 +0200190static const char diff_tree_usage[] =
Junio C Hamanod8f47902006-01-24 01:22:04 -0800191"git-diff-tree [--stdin] [-m] [-c] [--cc] [-s] [-v] [--pretty] [-t] [-r] [--root] "
Chris Shoemaker50b8e352005-10-28 13:04:49 -0400192"[<common diff options>] <tree-ish> [<tree-ish>] [<path>...]\n"
193" -r diff recursively\n"
194" --root include the initial commit as diff against /dev/null\n"
Junio C Hamanodda2d792005-07-13 12:52:35 -0700195COMMON_DIFF_OPTIONS_HELP;
Junio C Hamanoa8db1652005-06-12 17:44:21 -0700196
Junio C Hamano6b5ee132005-09-21 00:00:47 -0700197int main(int argc, const char **argv)
Linus Torvalds73134b62005-04-10 14:03:58 -0700198{
Linus Torvalds0a8365a2005-05-18 13:10:17 -0700199 int nr_sha1;
Linus Torvaldse0965d82005-05-06 10:03:17 -0700200 char line[1000];
Linus Torvalds0a8365a2005-05-18 13:10:17 -0700201 unsigned char sha1[2][20];
Linus Torvaldsd288a702005-08-16 18:06:34 -0700202 const char *prefix = setup_git_directory();
Linus Torvalds73134b62005-04-10 14:03:58 -0700203
Junio C Hamano9ce392f2005-11-21 22:52:37 -0800204 git_config(git_diff_config);
Linus Torvalds0a8365a2005-05-18 13:10:17 -0700205 nr_sha1 = 0;
Junio C Hamano6b5ee132005-09-21 00:00:47 -0700206 diff_setup(&diff_options);
207
Linus Torvaldsc5b42382005-04-23 22:08:00 -0700208 for (;;) {
Junio C Hamano6b5ee132005-09-21 00:00:47 -0700209 int diff_opt_cnt;
Junio C Hamano6b14d7f2005-05-22 10:04:37 -0700210 const char *arg;
Linus Torvaldsc5b42382005-04-23 22:08:00 -0700211
Linus Torvalds73134b62005-04-10 14:03:58 -0700212 argv++;
213 argc--;
Linus Torvaldse0965d82005-05-06 10:03:17 -0700214 arg = *argv;
Linus Torvalds0a8365a2005-05-18 13:10:17 -0700215 if (!arg)
Linus Torvaldse0965d82005-05-06 10:03:17 -0700216 break;
217
Linus Torvalds0a8365a2005-05-18 13:10:17 -0700218 if (*arg != '-') {
219 if (nr_sha1 < 2 && !get_sha1(arg, sha1[nr_sha1])) {
220 nr_sha1++;
221 continue;
222 }
223 break;
224 }
225
Junio C Hamano6b5ee132005-09-21 00:00:47 -0700226 diff_opt_cnt = diff_opt_parse(&diff_options, argv, argc);
227 if (diff_opt_cnt < 0)
228 usage(diff_tree_usage);
229 else if (diff_opt_cnt) {
230 argv += diff_opt_cnt - 1;
231 argc -= diff_opt_cnt - 1;
232 continue;
233 }
234
235
Linus Torvalds0a8365a2005-05-18 13:10:17 -0700236 if (!strcmp(arg, "--")) {
Linus Torvaldse0965d82005-05-06 10:03:17 -0700237 argv++;
238 argc--;
239 break;
240 }
Linus Torvaldsbf16c712005-04-11 08:37:17 -0700241 if (!strcmp(arg, "-r")) {
Linus Torvaldsac1b3d12005-10-20 21:05:05 -0700242 diff_options.recursive = 1;
Linus Torvalds73134b62005-04-10 14:03:58 -0700243 continue;
244 }
Junio C Hamano4cae1a92005-05-24 23:24:22 -0700245 if (!strcmp(arg, "-t")) {
Linus Torvaldsac1b3d12005-10-20 21:05:05 -0700246 diff_options.recursive = 1;
247 diff_options.tree_in_recursive = 1;
Junio C Hamano4cae1a92005-05-24 23:24:22 -0700248 continue;
249 }
Linus Torvaldse0965d82005-05-06 10:03:17 -0700250 if (!strcmp(arg, "-m")) {
Junio C Hamano6932c782006-02-11 16:43:30 -0800251 ignore_merges = 0;
Linus Torvaldse0965d82005-05-06 10:03:17 -0700252 continue;
253 }
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800254 if (!strcmp(arg, "-c")) {
255 combine_merges = 1;
256 continue;
257 }
Junio C Hamanod8f47902006-01-24 01:22:04 -0800258 if (!strcmp(arg, "--cc")) {
259 dense_combined_merges = combine_merges = 1;
260 continue;
261 }
Linus Torvaldscee99d22005-05-06 11:42:47 -0700262 if (!strcmp(arg, "-v")) {
263 verbose_header = 1;
264 header_prefix = "diff-tree ";
265 continue;
266 }
Junio C Hamanoa8db1652005-06-12 17:44:21 -0700267 if (!strncmp(arg, "--pretty", 8)) {
268 verbose_header = 1;
Linus Torvaldsba88e542005-06-12 20:34:09 -0700269 header_prefix = "diff-tree ";
Junio C Hamanoa8db1652005-06-12 17:44:21 -0700270 commit_format = get_commit_format(arg+8);
271 continue;
272 }
Linus Torvaldse0965d82005-05-06 10:03:17 -0700273 if (!strcmp(arg, "--stdin")) {
274 read_stdin = 1;
275 continue;
276 }
Linus Torvaldsdc26bd82005-05-19 13:44:29 -0700277 if (!strcmp(arg, "--root")) {
278 show_root_diff = 1;
279 continue;
280 }
Pavel Roskin601c9782005-11-10 00:30:12 -0500281 if (!strcmp(arg, "--no-commit-id")) {
282 no_commit_id = 1;
283 continue;
284 }
Linus Torvaldsec0bdb62006-02-05 11:42:09 -0800285 if (!strcmp(arg, "--always")) {
286 always_show_header = 1;
287 continue;
288 }
Junio C Hamanoc5bac172005-04-20 19:49:16 -0700289 usage(diff_tree_usage);
Linus Torvalds73134b62005-04-10 14:03:58 -0700290 }
291
Linus Torvaldsee638022006-02-09 10:30:28 -0800292 if (combine_merges)
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800293 ignore_merges = 0;
Linus Torvaldsee638022006-02-09 10:30:28 -0800294
295 /* We can only do dense combined merges with diff output */
296 if (dense_combined_merges)
297 diff_options.output_format = DIFF_FORMAT_PATCH;
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800298
Linus Torvaldsdeb989b2006-02-07 10:26:41 -0800299 if (diff_options.output_format == DIFF_FORMAT_PATCH)
300 diff_options.recursive = 1;
301
Linus Torvaldsac1b3d12005-10-20 21:05:05 -0700302 diff_tree_setup_paths(get_pathspec(prefix, argv));
Junio C Hamano47dd0d52005-12-13 17:21:41 -0800303 diff_setup_done(&diff_options);
Linus Torvaldsc5b42382005-04-23 22:08:00 -0700304
Linus Torvalds0a8365a2005-05-18 13:10:17 -0700305 switch (nr_sha1) {
306 case 0:
307 if (!read_stdin)
308 usage(diff_tree_usage);
309 break;
310 case 1:
Junio C Hamano45392a62006-02-05 23:00:41 -0800311 diff_tree_commit_sha1(sha1[0]);
Linus Torvalds0a8365a2005-05-18 13:10:17 -0700312 break;
313 case 2:
Junio C Hamano5c975582005-05-19 03:32:35 -0700314 diff_tree_sha1_top(sha1[0], sha1[1], "");
Linus Torvalds0a8365a2005-05-18 13:10:17 -0700315 break;
316 }
317
Linus Torvaldse0965d82005-05-06 10:03:17 -0700318 if (!read_stdin)
Linus Torvalds0a8365a2005-05-18 13:10:17 -0700319 return 0;
Linus Torvaldse0965d82005-05-06 10:03:17 -0700320
Junio C Hamano6b5ee132005-09-21 00:00:47 -0700321 if (diff_options.detect_rename)
322 diff_options.setup |= (DIFF_SETUP_USE_SIZE_CACHE |
323 DIFF_SETUP_USE_CACHE);
Linus Torvaldse0965d82005-05-06 10:03:17 -0700324 while (fgets(line, sizeof(line), stdin))
325 diff_tree_stdin(line);
326
327 return 0;
Linus Torvalds91740262005-04-09 13:00:54 -0700328}