blob: efa2b9476eae679b7f98a4605b07b6637e49de5a [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;
Linus Torvaldse0965d82005-05-06 10:03:17 -07009static int read_stdin = 0;
Junio C Hamano6b5ee132005-09-21 00:00:47 -070010
Linus Torvaldsf4f21ce2005-05-06 10:56:35 -070011static const char *header = NULL;
Linus Torvaldscee99d22005-05-06 11:42:47 -070012static const char *header_prefix = "";
Linus Torvalds000182e2005-06-05 09:02:03 -070013static enum cmit_fmt commit_format = CMIT_FMT_RAW;
Linus Torvalds91740262005-04-09 13:00:54 -070014
Linus Torvaldsac1b3d12005-10-20 21:05:05 -070015static struct diff_options diff_options;
Linus Torvalds73134b62005-04-10 14:03:58 -070016
Linus Torvalds09d74b32005-05-22 14:33:43 -070017static int call_diff_flush(void)
Junio C Hamano38c6f782005-05-21 19:40:36 -070018{
Junio C Hamano6b5ee132005-09-21 00:00:47 -070019 diffcore_std(&diff_options);
Linus Torvalds9ab55bd2005-05-23 16:37:47 -070020 if (diff_queue_is_empty()) {
Junio C Hamano6b5ee132005-09-21 00:00:47 -070021 int saved_fmt = diff_options.output_format;
22 diff_options.output_format = DIFF_FORMAT_NO_OUTPUT;
23 diff_flush(&diff_options);
24 diff_options.output_format = saved_fmt;
Linus Torvalds9ab55bd2005-05-23 16:37:47 -070025 return 0;
Junio C Hamano6b14d7f2005-05-22 10:04:37 -070026 }
Junio C Hamano6b14d7f2005-05-22 10:04:37 -070027 if (header) {
Pavel Roskin601c9782005-11-10 00:30:12 -050028 if (!no_commit_id)
29 printf("%s%c", header, diff_options.line_termination);
Junio C Hamano6b14d7f2005-05-22 10:04:37 -070030 header = NULL;
31 }
Junio C Hamano6b5ee132005-09-21 00:00:47 -070032 diff_flush(&diff_options);
Junio C Hamano6b14d7f2005-05-22 10:04:37 -070033 return 1;
Junio C Hamano38c6f782005-05-21 19:40:36 -070034}
35
Junio C Hamano5c975582005-05-19 03:32:35 -070036static int diff_tree_sha1_top(const unsigned char *old,
37 const unsigned char *new, const char *base)
38{
39 int ret;
Junio C Hamano57fe64a2005-05-19 19:00:36 -070040
Linus Torvaldsac1b3d12005-10-20 21:05:05 -070041 ret = diff_tree_sha1(old, new, base, &diff_options);
Junio C Hamano38c6f782005-05-21 19:40:36 -070042 call_diff_flush();
Junio C Hamano5c975582005-05-19 03:32:35 -070043 return ret;
44}
45
Linus Torvaldsdc26bd82005-05-19 13:44:29 -070046static int diff_root_tree(const unsigned char *new, const char *base)
47{
48 int retval;
49 void *tree;
Linus Torvaldsac1b3d12005-10-20 21:05:05 -070050 struct tree_desc empty, real;
Linus Torvaldsdc26bd82005-05-19 13:44:29 -070051
Linus Torvaldsac1b3d12005-10-20 21:05:05 -070052 tree = read_object_with_reference(new, "tree", &real.size, NULL);
Linus Torvaldsdc26bd82005-05-19 13:44:29 -070053 if (!tree)
54 die("unable to read root tree (%s)", sha1_to_hex(new));
Linus Torvaldsac1b3d12005-10-20 21:05:05 -070055 real.buf = tree;
56
57 empty.buf = "";
58 empty.size = 0;
59 retval = diff_tree(&empty, &real, base, &diff_options);
Linus Torvaldsdc26bd82005-05-19 13:44:29 -070060 free(tree);
Junio C Hamano38c6f782005-05-21 19:40:36 -070061 call_diff_flush();
Linus Torvaldsdc26bd82005-05-19 13:44:29 -070062 return retval;
63}
64
Junio C Hamano47dd0d52005-12-13 17:21:41 -080065static const char *generate_header(const unsigned char *commit_sha1,
66 const unsigned char *parent_sha1,
67 const char *msg)
Linus Torvaldscee99d22005-05-06 11:42:47 -070068{
Linus Torvalds3258c902005-05-21 11:04:19 -070069 static char this_header[16384];
Linus Torvaldscee99d22005-05-06 11:42:47 -070070 int offset;
Junio C Hamanoa50b8702005-11-21 21:49:06 -080071 unsigned long len;
Junio C Hamano47dd0d52005-12-13 17:21:41 -080072 int abbrev = diff_options.abbrev;
Linus Torvaldscee99d22005-05-06 11:42:47 -070073
Linus Torvalds18092662005-06-23 13:56:55 -070074 if (!verbose_header)
Junio C Hamano47dd0d52005-12-13 17:21:41 -080075 return sha1_to_hex(commit_sha1);
Linus Torvaldscee99d22005-05-06 11:42:47 -070076
Junio C Hamanoa50b8702005-11-21 21:49:06 -080077 len = strlen(msg);
Junio C Hamano47dd0d52005-12-13 17:21:41 -080078
79 offset = sprintf(this_header, "%s%s ",
80 header_prefix,
81 diff_unique_abbrev(commit_sha1, abbrev));
82 offset += sprintf(this_header + offset, "(from %s)\n",
83 parent_sha1 ?
84 diff_unique_abbrev(parent_sha1, abbrev) : "root");
85 offset += pretty_print_commit(commit_format, msg, len,
86 this_header + offset,
87 sizeof(this_header) - offset);
Linus Torvaldscee99d22005-05-06 11:42:47 -070088 return this_header;
89}
90
Junio C Hamanoa50b8702005-11-21 21:49:06 -080091static int diff_tree_commit(const unsigned char *commit_sha1)
Linus Torvaldsb11645b2005-05-18 13:06:47 -070092{
Junio C Hamanoa50b8702005-11-21 21:49:06 -080093 struct commit *commit;
94 struct commit_list *parents;
95 char name[50];
96 unsigned char sha1[20];
Linus Torvaldsb11645b2005-05-18 13:06:47 -070097
Junio C Hamanoa50b8702005-11-21 21:49:06 -080098 sprintf(name, "%s^0", sha1_to_hex(commit_sha1));
99 if (get_sha1(name, sha1))
Linus Torvaldsb11645b2005-05-18 13:06:47 -0700100 return -1;
Junio C Hamanoa50b8702005-11-21 21:49:06 -0800101 name[40] = 0;
102 commit = lookup_commit(sha1);
103
Linus Torvaldsdc26bd82005-05-19 13:44:29 -0700104 /* Root commit? */
Junio C Hamanoa50b8702005-11-21 21:49:06 -0800105 if (show_root_diff && !commit->parents) {
Junio C Hamano47dd0d52005-12-13 17:21:41 -0800106 header = generate_header(sha1, NULL, commit->buffer);
Junio C Hamanoa50b8702005-11-21 21:49:06 -0800107 diff_root_tree(commit_sha1, "");
Linus Torvaldsdc26bd82005-05-19 13:44:29 -0700108 }
109
110 /* More than one parent? */
Junio C Hamanoa50b8702005-11-21 21:49:06 -0800111 if (ignore_merges && commit->parents && commit->parents->next)
Junio C Hamano47dd0d52005-12-13 17:21:41 -0800112 return 0;
Linus Torvaldsdc26bd82005-05-19 13:44:29 -0700113
Junio C Hamanoa50b8702005-11-21 21:49:06 -0800114 for (parents = commit->parents; parents; parents = parents->next) {
115 struct commit *parent = parents->item;
Junio C Hamano47dd0d52005-12-13 17:21:41 -0800116 header = generate_header(sha1,
117 parent->object.sha1,
Junio C Hamanoa50b8702005-11-21 21:49:06 -0800118 commit->buffer);
119 diff_tree_sha1_top(parent->object.sha1, commit_sha1, "");
Linus Torvaldsd6db0102005-05-21 15:42:53 -0700120 if (!header && verbose_header) {
Linus Torvaldsb11645b2005-05-18 13:06:47 -0700121 header_prefix = "\ndiff-tree ";
Linus Torvaldsd6db0102005-05-21 15:42:53 -0700122 /*
123 * Don't print multiple merge entries if we
124 * don't print the diffs.
125 */
Linus Torvaldsd6db0102005-05-21 15:42:53 -0700126 }
Linus Torvaldsb11645b2005-05-18 13:06:47 -0700127 }
128 return 0;
129}
130
Linus Torvaldse0965d82005-05-06 10:03:17 -0700131static int diff_tree_stdin(char *line)
132{
133 int len = strlen(line);
134 unsigned char commit[20], parent[20];
Linus Torvaldscee99d22005-05-06 11:42:47 -0700135 static char this_header[1000];
Junio C Hamano47dd0d52005-12-13 17:21:41 -0800136 int abbrev = diff_options.abbrev;
Linus Torvaldse0965d82005-05-06 10:03:17 -0700137
138 if (!len || line[len-1] != '\n')
139 return -1;
140 line[len-1] = 0;
141 if (get_sha1_hex(line, commit))
142 return -1;
143 if (isspace(line[40]) && !get_sha1_hex(line+41, parent)) {
Linus Torvaldsf4f21ce2005-05-06 10:56:35 -0700144 line[40] = 0;
Linus Torvaldse0965d82005-05-06 10:03:17 -0700145 line[81] = 0;
Junio C Hamano47dd0d52005-12-13 17:21:41 -0800146 sprintf(this_header, "%s (from %s)\n",
147 diff_unique_abbrev(commit, abbrev),
148 diff_unique_abbrev(parent, abbrev));
Linus Torvaldsf4f21ce2005-05-06 10:56:35 -0700149 header = this_header;
Junio C Hamano5c975582005-05-19 03:32:35 -0700150 return diff_tree_sha1_top(parent, commit, "");
Linus Torvaldse0965d82005-05-06 10:03:17 -0700151 }
Linus Torvaldse0965d82005-05-06 10:03:17 -0700152 line[40] = 0;
Junio C Hamanoa50b8702005-11-21 21:49:06 -0800153 return diff_tree_commit(commit);
Linus Torvaldse0965d82005-05-06 10:03:17 -0700154}
155
Petr Baudis4d1f1192005-07-29 11:01:26 +0200156static const char diff_tree_usage[] =
Chris Shoemaker50b8e352005-10-28 13:04:49 -0400157"git-diff-tree [--stdin] [-m] [-s] [-v] [--pretty] [-t] [-r] [--root] "
158"[<common diff options>] <tree-ish> [<tree-ish>] [<path>...]\n"
159" -r diff recursively\n"
160" --root include the initial commit as diff against /dev/null\n"
Junio C Hamanodda2d792005-07-13 12:52:35 -0700161COMMON_DIFF_OPTIONS_HELP;
Junio C Hamanoa8db1652005-06-12 17:44:21 -0700162
Junio C Hamano6b5ee132005-09-21 00:00:47 -0700163int main(int argc, const char **argv)
Linus Torvalds73134b62005-04-10 14:03:58 -0700164{
Linus Torvalds0a8365a2005-05-18 13:10:17 -0700165 int nr_sha1;
Linus Torvaldse0965d82005-05-06 10:03:17 -0700166 char line[1000];
Linus Torvalds0a8365a2005-05-18 13:10:17 -0700167 unsigned char sha1[2][20];
Linus Torvaldsd288a702005-08-16 18:06:34 -0700168 const char *prefix = setup_git_directory();
Linus Torvalds73134b62005-04-10 14:03:58 -0700169
Junio C Hamano9ce392f2005-11-21 22:52:37 -0800170 git_config(git_diff_config);
Linus Torvalds0a8365a2005-05-18 13:10:17 -0700171 nr_sha1 = 0;
Junio C Hamano6b5ee132005-09-21 00:00:47 -0700172 diff_setup(&diff_options);
173
Linus Torvaldsc5b42382005-04-23 22:08:00 -0700174 for (;;) {
Junio C Hamano6b5ee132005-09-21 00:00:47 -0700175 int diff_opt_cnt;
Junio C Hamano6b14d7f2005-05-22 10:04:37 -0700176 const char *arg;
Linus Torvaldsc5b42382005-04-23 22:08:00 -0700177
Linus Torvalds73134b62005-04-10 14:03:58 -0700178 argv++;
179 argc--;
Linus Torvaldse0965d82005-05-06 10:03:17 -0700180 arg = *argv;
Linus Torvalds0a8365a2005-05-18 13:10:17 -0700181 if (!arg)
Linus Torvaldse0965d82005-05-06 10:03:17 -0700182 break;
183
Linus Torvalds0a8365a2005-05-18 13:10:17 -0700184 if (*arg != '-') {
185 if (nr_sha1 < 2 && !get_sha1(arg, sha1[nr_sha1])) {
186 nr_sha1++;
187 continue;
188 }
189 break;
190 }
191
Junio C Hamano6b5ee132005-09-21 00:00:47 -0700192 diff_opt_cnt = diff_opt_parse(&diff_options, argv, argc);
193 if (diff_opt_cnt < 0)
194 usage(diff_tree_usage);
195 else if (diff_opt_cnt) {
196 argv += diff_opt_cnt - 1;
197 argc -= diff_opt_cnt - 1;
198 continue;
199 }
200
201
Linus Torvalds0a8365a2005-05-18 13:10:17 -0700202 if (!strcmp(arg, "--")) {
Linus Torvaldse0965d82005-05-06 10:03:17 -0700203 argv++;
204 argc--;
205 break;
206 }
Linus Torvaldsbf16c712005-04-11 08:37:17 -0700207 if (!strcmp(arg, "-r")) {
Linus Torvaldsac1b3d12005-10-20 21:05:05 -0700208 diff_options.recursive = 1;
Linus Torvalds73134b62005-04-10 14:03:58 -0700209 continue;
210 }
Junio C Hamano4cae1a92005-05-24 23:24:22 -0700211 if (!strcmp(arg, "-t")) {
Linus Torvaldsac1b3d12005-10-20 21:05:05 -0700212 diff_options.recursive = 1;
213 diff_options.tree_in_recursive = 1;
Junio C Hamano4cae1a92005-05-24 23:24:22 -0700214 continue;
215 }
Linus Torvaldse0965d82005-05-06 10:03:17 -0700216 if (!strcmp(arg, "-m")) {
217 ignore_merges = 0;
218 continue;
219 }
Linus Torvaldscee99d22005-05-06 11:42:47 -0700220 if (!strcmp(arg, "-v")) {
221 verbose_header = 1;
222 header_prefix = "diff-tree ";
223 continue;
224 }
Junio C Hamanoa8db1652005-06-12 17:44:21 -0700225 if (!strncmp(arg, "--pretty", 8)) {
226 verbose_header = 1;
Linus Torvaldsba88e542005-06-12 20:34:09 -0700227 header_prefix = "diff-tree ";
Junio C Hamanoa8db1652005-06-12 17:44:21 -0700228 commit_format = get_commit_format(arg+8);
229 continue;
230 }
Linus Torvaldse0965d82005-05-06 10:03:17 -0700231 if (!strcmp(arg, "--stdin")) {
232 read_stdin = 1;
233 continue;
234 }
Linus Torvaldsdc26bd82005-05-19 13:44:29 -0700235 if (!strcmp(arg, "--root")) {
236 show_root_diff = 1;
237 continue;
238 }
Pavel Roskin601c9782005-11-10 00:30:12 -0500239 if (!strcmp(arg, "--no-commit-id")) {
240 no_commit_id = 1;
241 continue;
242 }
Junio C Hamanoc5bac172005-04-20 19:49:16 -0700243 usage(diff_tree_usage);
Linus Torvalds73134b62005-04-10 14:03:58 -0700244 }
Junio C Hamano6b5ee132005-09-21 00:00:47 -0700245 if (diff_options.output_format == DIFF_FORMAT_PATCH)
Linus Torvaldsac1b3d12005-10-20 21:05:05 -0700246 diff_options.recursive = 1;
Linus Torvalds73134b62005-04-10 14:03:58 -0700247
Linus Torvaldsac1b3d12005-10-20 21:05:05 -0700248 diff_tree_setup_paths(get_pathspec(prefix, argv));
Junio C Hamano47dd0d52005-12-13 17:21:41 -0800249 diff_setup_done(&diff_options);
Linus Torvaldsc5b42382005-04-23 22:08:00 -0700250
Linus Torvalds0a8365a2005-05-18 13:10:17 -0700251 switch (nr_sha1) {
252 case 0:
253 if (!read_stdin)
254 usage(diff_tree_usage);
255 break;
256 case 1:
Junio C Hamanoa50b8702005-11-21 21:49:06 -0800257 diff_tree_commit(sha1[0]);
Linus Torvalds0a8365a2005-05-18 13:10:17 -0700258 break;
259 case 2:
Junio C Hamano5c975582005-05-19 03:32:35 -0700260 diff_tree_sha1_top(sha1[0], sha1[1], "");
Linus Torvalds0a8365a2005-05-18 13:10:17 -0700261 break;
262 }
263
Linus Torvaldse0965d82005-05-06 10:03:17 -0700264 if (!read_stdin)
Linus Torvalds0a8365a2005-05-18 13:10:17 -0700265 return 0;
Linus Torvaldse0965d82005-05-06 10:03:17 -0700266
Junio C Hamano6b5ee132005-09-21 00:00:47 -0700267 if (diff_options.detect_rename)
268 diff_options.setup |= (DIFF_SETUP_USE_SIZE_CACHE |
269 DIFF_SETUP_USE_CACHE);
Linus Torvaldse0965d82005-05-06 10:03:17 -0700270 while (fgets(line, sizeof(line), stdin))
271 diff_tree_stdin(line);
272
273 return 0;
Linus Torvalds91740262005-04-09 13:00:54 -0700274}