blob: ed323d877cad1e78e08f84c1e9ee1ebb09f05d91 [file] [log] [blame]
Linus Torvalds91740262005-04-09 20:00:541#include "cache.h"
Junio C Hamano3ebfd4a2005-04-27 16:21:002#include "diff.h"
Linus Torvaldse3bc7a32005-06-01 15:34:233#include "commit.h"
Linus Torvalds91740262005-04-09 20:00:544
Linus Torvaldsdc26bd82005-05-19 20:44:295static int show_root_diff = 0;
Linus Torvaldscee99d22005-05-06 18:42:476static int verbose_header = 0;
Linus Torvaldse0965d82005-05-06 17:03:177static int ignore_merges = 1;
Linus Torvaldse0965d82005-05-06 17:03:178static int read_stdin = 0;
Junio C Hamano6b5ee132005-09-21 07:00:479
Linus Torvaldsf4f21ce2005-05-06 17:56:3510static const char *header = NULL;
Linus Torvaldscee99d22005-05-06 18:42:4711static const char *header_prefix = "";
Linus Torvalds000182e2005-06-05 16:02:0312static enum cmit_fmt commit_format = CMIT_FMT_RAW;
Linus Torvalds91740262005-04-09 20:00:5413
Linus Torvaldsac1b3d12005-10-21 04:05:0514static struct diff_options diff_options;
Linus Torvalds73134b62005-04-10 21:03:5815
Junio C Hamano6b5ee132005-09-21 07:00:4716static void call_diff_setup_done(void)
Junio C Hamano38c6f782005-05-22 02:40:3617{
Junio C Hamano6b5ee132005-09-21 07:00:4718 diff_setup_done(&diff_options);
Junio C Hamano38c6f782005-05-22 02:40:3619}
20
Linus Torvalds09d74b32005-05-22 21:33:4321static int call_diff_flush(void)
Junio C Hamano38c6f782005-05-22 02:40:3622{
Junio C Hamano6b5ee132005-09-21 07:00:4723 diffcore_std(&diff_options);
Linus Torvalds9ab55bd2005-05-23 23:37:4724 if (diff_queue_is_empty()) {
Junio C Hamano6b5ee132005-09-21 07:00:4725 int saved_fmt = diff_options.output_format;
26 diff_options.output_format = DIFF_FORMAT_NO_OUTPUT;
27 diff_flush(&diff_options);
28 diff_options.output_format = saved_fmt;
Linus Torvalds9ab55bd2005-05-23 23:37:4729 return 0;
Junio C Hamano6b14d7f2005-05-22 17:04:3730 }
Junio C Hamano6b14d7f2005-05-22 17:04:3731 if (header) {
Junio C Hamano6b5ee132005-09-21 07:00:4732 printf("%s%c", header, diff_options.line_termination);
Junio C Hamano6b14d7f2005-05-22 17:04:3733 header = NULL;
34 }
Junio C Hamano6b5ee132005-09-21 07:00:4735 diff_flush(&diff_options);
Junio C Hamano6b14d7f2005-05-22 17:04:3736 return 1;
Junio C Hamano38c6f782005-05-22 02:40:3637}
38
Junio C Hamano5c975582005-05-19 10:32:3539static 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-20 02:00:3643
Junio C Hamano6b5ee132005-09-21 07:00:4744 call_diff_setup_done();
Linus Torvaldsac1b3d12005-10-21 04:05:0545 ret = diff_tree_sha1(old, new, base, &diff_options);
Junio C Hamano38c6f782005-05-22 02:40:3646 call_diff_flush();
Junio C Hamano5c975582005-05-19 10:32:3547 return ret;
48}
49
Linus Torvaldsdc26bd82005-05-19 20:44:2950static int diff_root_tree(const unsigned char *new, const char *base)
51{
52 int retval;
53 void *tree;
Linus Torvaldsac1b3d12005-10-21 04:05:0554 struct tree_desc empty, real;
Linus Torvaldsdc26bd82005-05-19 20:44:2955
Junio C Hamano6b5ee132005-09-21 07:00:4756 call_diff_setup_done();
Linus Torvaldsac1b3d12005-10-21 04:05:0557 tree = read_object_with_reference(new, "tree", &real.size, NULL);
Linus Torvaldsdc26bd82005-05-19 20:44:2958 if (!tree)
59 die("unable to read root tree (%s)", sha1_to_hex(new));
Linus Torvaldsac1b3d12005-10-21 04:05:0560 real.buf = tree;
61
62 empty.buf = "";
63 empty.size = 0;
64 retval = diff_tree(&empty, &real, base, &diff_options);
Linus Torvaldsdc26bd82005-05-19 20:44:2965 free(tree);
Junio C Hamano38c6f782005-05-22 02:40:3666 call_diff_flush();
Linus Torvaldsdc26bd82005-05-19 20:44:2967 return retval;
68}
69
Linus Torvalds18092662005-06-23 20:56:5570static const char *generate_header(const char *commit, const char *parent, const char *msg, unsigned long len)
Linus Torvaldscee99d22005-05-06 18:42:4771{
Linus Torvalds3258c902005-05-21 18:04:1972 static char this_header[16384];
Linus Torvaldscee99d22005-05-06 18:42:4773 int offset;
74
Linus Torvalds18092662005-06-23 20:56:5575 if (!verbose_header)
76 return commit;
Linus Torvaldscee99d22005-05-06 18:42:4777
Linus Torvalds18092662005-06-23 20:56:5578 offset = sprintf(this_header, "%s%s (from %s)\n", header_prefix, commit, parent);
79 offset += pretty_print_commit(commit_format, msg, len, this_header + offset, sizeof(this_header) - offset);
Linus Torvaldscee99d22005-05-06 18:42:4780 return this_header;
81}
82
Linus Torvaldsb11645b2005-05-18 20:06:4783static int diff_tree_commit(const unsigned char *commit, const char *name)
84{
85 unsigned long size, offset;
86 char *buf = read_object_with_reference(commit, "commit", &size, NULL);
87
88 if (!buf)
89 return -1;
90
Linus Torvalds73848892005-05-18 20:43:5891 if (!name) {
92 static char commit_name[60];
93 strcpy(commit_name, sha1_to_hex(commit));
94 name = commit_name;
95 }
96
Linus Torvaldsdc26bd82005-05-19 20:44:2997 /* Root commit? */
98 if (show_root_diff && memcmp(buf + 46, "parent ", 7)) {
99 header = generate_header(name, "root", buf, size);
100 diff_root_tree(commit, "");
101 }
102
103 /* More than one parent? */
104 if (ignore_merges) {
105 if (!memcmp(buf + 46 + 48, "parent ", 7))
106 return 0;
107 }
108
Linus Torvaldsb11645b2005-05-18 20:06:47109 offset = 46;
110 while (offset + 48 < size && !memcmp(buf + offset, "parent ", 7)) {
111 unsigned char parent[20];
112 if (get_sha1_hex(buf + offset + 7, parent))
113 return -1;
114 header = generate_header(name, sha1_to_hex(parent), buf, size);
Junio C Hamano5c975582005-05-19 10:32:35115 diff_tree_sha1_top(parent, commit, "");
Linus Torvaldsd6db0102005-05-21 22:42:53116 if (!header && verbose_header) {
Linus Torvaldsb11645b2005-05-18 20:06:47117 header_prefix = "\ndiff-tree ";
Linus Torvaldsd6db0102005-05-21 22:42:53118 /*
119 * Don't print multiple merge entries if we
120 * don't print the diffs.
121 */
Linus Torvaldsd6db0102005-05-21 22:42:53122 }
Linus Torvaldsb11645b2005-05-18 20:06:47123 offset += 48;
124 }
Junio C Hamano5098baf2005-09-15 23:13:43125 free(buf);
Linus Torvaldsb11645b2005-05-18 20:06:47126 return 0;
127}
128
Linus Torvaldse0965d82005-05-06 17:03:17129static int diff_tree_stdin(char *line)
130{
131 int len = strlen(line);
132 unsigned char commit[20], parent[20];
Linus Torvaldscee99d22005-05-06 18:42:47133 static char this_header[1000];
Linus Torvaldse0965d82005-05-06 17:03:17134
135 if (!len || line[len-1] != '\n')
136 return -1;
137 line[len-1] = 0;
138 if (get_sha1_hex(line, commit))
139 return -1;
140 if (isspace(line[40]) && !get_sha1_hex(line+41, parent)) {
Linus Torvaldsf4f21ce2005-05-06 17:56:35141 line[40] = 0;
Linus Torvaldse0965d82005-05-06 17:03:17142 line[81] = 0;
Linus Torvaldsf4f21ce2005-05-06 17:56:35143 sprintf(this_header, "%s (from %s)\n", line, line+41);
144 header = this_header;
Junio C Hamano5c975582005-05-19 10:32:35145 return diff_tree_sha1_top(parent, commit, "");
Linus Torvaldse0965d82005-05-06 17:03:17146 }
Linus Torvaldse0965d82005-05-06 17:03:17147 line[40] = 0;
Linus Torvaldsb11645b2005-05-18 20:06:47148 return diff_tree_commit(commit, line);
Linus Torvaldse0965d82005-05-06 17:03:17149}
150
Petr Baudis4d1f1192005-07-29 09:01:26151static const char diff_tree_usage[] =
Chris Shoemaker50b8e352005-10-28 17:04:49152"git-diff-tree [--stdin] [-m] [-s] [-v] [--pretty] [-t] [-r] [--root] "
153"[<common diff options>] <tree-ish> [<tree-ish>] [<path>...]\n"
154" -r diff recursively\n"
155" --root include the initial commit as diff against /dev/null\n"
Junio C Hamanodda2d792005-07-13 19:52:35156COMMON_DIFF_OPTIONS_HELP;
Junio C Hamanoa8db1652005-06-13 00:44:21157
Junio C Hamano6b5ee132005-09-21 07:00:47158int main(int argc, const char **argv)
Linus Torvalds73134b62005-04-10 21:03:58159{
Linus Torvalds0a8365a2005-05-18 20:10:17160 int nr_sha1;
Linus Torvaldse0965d82005-05-06 17:03:17161 char line[1000];
Linus Torvalds0a8365a2005-05-18 20:10:17162 unsigned char sha1[2][20];
Linus Torvaldsd288a702005-08-17 01:06:34163 const char *prefix = setup_git_directory();
Linus Torvalds73134b62005-04-10 21:03:58164
Linus Torvalds17712992005-10-10 23:31:08165 git_config(git_default_config);
Linus Torvalds0a8365a2005-05-18 20:10:17166 nr_sha1 = 0;
Junio C Hamano6b5ee132005-09-21 07:00:47167 diff_setup(&diff_options);
168
Linus Torvaldsc5b42382005-04-24 05:08:00169 for (;;) {
Junio C Hamano6b5ee132005-09-21 07:00:47170 int diff_opt_cnt;
Junio C Hamano6b14d7f2005-05-22 17:04:37171 const char *arg;
Linus Torvaldsc5b42382005-04-24 05:08:00172
Linus Torvalds73134b62005-04-10 21:03:58173 argv++;
174 argc--;
Linus Torvaldse0965d82005-05-06 17:03:17175 arg = *argv;
Linus Torvalds0a8365a2005-05-18 20:10:17176 if (!arg)
Linus Torvaldse0965d82005-05-06 17:03:17177 break;
178
Linus Torvalds0a8365a2005-05-18 20:10:17179 if (*arg != '-') {
180 if (nr_sha1 < 2 && !get_sha1(arg, sha1[nr_sha1])) {
181 nr_sha1++;
182 continue;
183 }
184 break;
185 }
186
Junio C Hamano6b5ee132005-09-21 07:00:47187 diff_opt_cnt = diff_opt_parse(&diff_options, argv, argc);
188 if (diff_opt_cnt < 0)
189 usage(diff_tree_usage);
190 else if (diff_opt_cnt) {
191 argv += diff_opt_cnt - 1;
192 argc -= diff_opt_cnt - 1;
193 continue;
194 }
195
196
Linus Torvalds0a8365a2005-05-18 20:10:17197 if (!strcmp(arg, "--")) {
Linus Torvaldse0965d82005-05-06 17:03:17198 argv++;
199 argc--;
200 break;
201 }
Linus Torvaldsbf16c712005-04-11 15:37:17202 if (!strcmp(arg, "-r")) {
Linus Torvaldsac1b3d12005-10-21 04:05:05203 diff_options.recursive = 1;
Linus Torvalds73134b62005-04-10 21:03:58204 continue;
205 }
Junio C Hamano4cae1a92005-05-25 06:24:22206 if (!strcmp(arg, "-t")) {
Linus Torvaldsac1b3d12005-10-21 04:05:05207 diff_options.recursive = 1;
208 diff_options.tree_in_recursive = 1;
Junio C Hamano4cae1a92005-05-25 06:24:22209 continue;
210 }
Linus Torvaldse0965d82005-05-06 17:03:17211 if (!strcmp(arg, "-m")) {
212 ignore_merges = 0;
213 continue;
214 }
Linus Torvaldscee99d22005-05-06 18:42:47215 if (!strcmp(arg, "-v")) {
216 verbose_header = 1;
217 header_prefix = "diff-tree ";
218 continue;
219 }
Junio C Hamanoa8db1652005-06-13 00:44:21220 if (!strncmp(arg, "--pretty", 8)) {
221 verbose_header = 1;
Linus Torvaldsba88e542005-06-13 03:34:09222 header_prefix = "diff-tree ";
Junio C Hamanoa8db1652005-06-13 00:44:21223 commit_format = get_commit_format(arg+8);
224 continue;
225 }
Linus Torvaldse0965d82005-05-06 17:03:17226 if (!strcmp(arg, "--stdin")) {
227 read_stdin = 1;
228 continue;
229 }
Linus Torvaldsdc26bd82005-05-19 20:44:29230 if (!strcmp(arg, "--root")) {
231 show_root_diff = 1;
232 continue;
233 }
Junio C Hamanoc5bac172005-04-21 02:49:16234 usage(diff_tree_usage);
Linus Torvalds73134b62005-04-10 21:03:58235 }
Junio C Hamano6b5ee132005-09-21 07:00:47236 if (diff_options.output_format == DIFF_FORMAT_PATCH)
Linus Torvaldsac1b3d12005-10-21 04:05:05237 diff_options.recursive = 1;
Linus Torvalds73134b62005-04-10 21:03:58238
Linus Torvaldsac1b3d12005-10-21 04:05:05239 diff_tree_setup_paths(get_pathspec(prefix, argv));
Linus Torvaldsc5b42382005-04-24 05:08:00240
Linus Torvalds0a8365a2005-05-18 20:10:17241 switch (nr_sha1) {
242 case 0:
243 if (!read_stdin)
244 usage(diff_tree_usage);
245 break;
246 case 1:
Linus Torvalds73848892005-05-18 20:43:58247 diff_tree_commit(sha1[0], NULL);
Linus Torvalds0a8365a2005-05-18 20:10:17248 break;
249 case 2:
Junio C Hamano5c975582005-05-19 10:32:35250 diff_tree_sha1_top(sha1[0], sha1[1], "");
Linus Torvalds0a8365a2005-05-18 20:10:17251 break;
252 }
253
Linus Torvaldse0965d82005-05-06 17:03:17254 if (!read_stdin)
Linus Torvalds0a8365a2005-05-18 20:10:17255 return 0;
Linus Torvaldse0965d82005-05-06 17:03:17256
Junio C Hamano6b5ee132005-09-21 07:00:47257 if (diff_options.detect_rename)
258 diff_options.setup |= (DIFF_SETUP_USE_SIZE_CACHE |
259 DIFF_SETUP_USE_CACHE);
Linus Torvaldse0965d82005-05-06 17:03:17260 while (fgets(line, sizeof(line), stdin))
261 diff_tree_stdin(line);
262
263 return 0;
Linus Torvalds91740262005-04-09 20:00:54264}