Add git-pack-intersect
[git/spearce.git] / diff-tree.c
blob09d16ad6619d7e95959adf2003a8d0f8626491ce
1 #include "cache.h"
2 #include "diff.h"
3 #include "commit.h"
5 static int show_root_diff = 0;
6 static int no_commit_id = 0;
7 static int verbose_header = 0;
8 static int ignore_merges = 1;
9 static int read_stdin = 0;
11 static const char *header = NULL;
12 static const char *header_prefix = "";
13 static enum cmit_fmt commit_format = CMIT_FMT_RAW;
15 static struct diff_options diff_options;
17 static void call_diff_setup_done(void)
19 diff_setup_done(&diff_options);
22 static int call_diff_flush(void)
24 diffcore_std(&diff_options);
25 if (diff_queue_is_empty()) {
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;
30 return 0;
32 if (header) {
33 if (!no_commit_id)
34 printf("%s%c", header, diff_options.line_termination);
35 header = NULL;
37 diff_flush(&diff_options);
38 return 1;
41 static int diff_tree_sha1_top(const unsigned char *old,
42 const unsigned char *new, const char *base)
44 int ret;
46 call_diff_setup_done();
47 ret = diff_tree_sha1(old, new, base, &diff_options);
48 call_diff_flush();
49 return ret;
52 static int diff_root_tree(const unsigned char *new, const char *base)
54 int retval;
55 void *tree;
56 struct tree_desc empty, real;
58 call_diff_setup_done();
59 tree = read_object_with_reference(new, "tree", &real.size, NULL);
60 if (!tree)
61 die("unable to read root tree (%s)", sha1_to_hex(new));
62 real.buf = tree;
64 empty.buf = "";
65 empty.size = 0;
66 retval = diff_tree(&empty, &real, base, &diff_options);
67 free(tree);
68 call_diff_flush();
69 return retval;
72 static const char *generate_header(const char *commit, const char *parent, const char *msg, unsigned long len)
74 static char this_header[16384];
75 int offset;
77 if (!verbose_header)
78 return commit;
80 offset = sprintf(this_header, "%s%s (from %s)\n", header_prefix, commit, parent);
81 offset += pretty_print_commit(commit_format, msg, len, this_header + offset, sizeof(this_header) - offset);
82 return this_header;
85 static int diff_tree_commit(const unsigned char *commit, const char *name)
87 unsigned long size, offset;
88 char *buf = read_object_with_reference(commit, "commit", &size, NULL);
90 if (!buf)
91 return -1;
93 if (!name) {
94 static char commit_name[60];
95 strcpy(commit_name, sha1_to_hex(commit));
96 name = commit_name;
99 /* Root commit? */
100 if (show_root_diff && memcmp(buf + 46, "parent ", 7)) {
101 header = generate_header(name, "root", buf, size);
102 diff_root_tree(commit, "");
105 /* More than one parent? */
106 if (ignore_merges) {
107 if (!memcmp(buf + 46 + 48, "parent ", 7))
108 return 0;
111 offset = 46;
112 while (offset + 48 < size && !memcmp(buf + offset, "parent ", 7)) {
113 unsigned char parent[20];
114 if (get_sha1_hex(buf + offset + 7, parent))
115 return -1;
116 header = generate_header(name, sha1_to_hex(parent), buf, size);
117 diff_tree_sha1_top(parent, commit, "");
118 if (!header && verbose_header) {
119 header_prefix = "\ndiff-tree ";
121 * Don't print multiple merge entries if we
122 * don't print the diffs.
125 offset += 48;
127 free(buf);
128 return 0;
131 static int diff_tree_stdin(char *line)
133 int len = strlen(line);
134 unsigned char commit[20], parent[20];
135 static char this_header[1000];
137 if (!len || line[len-1] != '\n')
138 return -1;
139 line[len-1] = 0;
140 if (get_sha1_hex(line, commit))
141 return -1;
142 if (isspace(line[40]) && !get_sha1_hex(line+41, parent)) {
143 line[40] = 0;
144 line[81] = 0;
145 sprintf(this_header, "%s (from %s)\n", line, line+41);
146 header = this_header;
147 return diff_tree_sha1_top(parent, commit, "");
149 line[40] = 0;
150 return diff_tree_commit(commit, line);
153 static const char diff_tree_usage[] =
154 "git-diff-tree [--stdin] [-m] [-s] [-v] [--pretty] [-t] [-r] [--root] "
155 "[<common diff options>] <tree-ish> [<tree-ish>] [<path>...]\n"
156 " -r diff recursively\n"
157 " --root include the initial commit as diff against /dev/null\n"
158 COMMON_DIFF_OPTIONS_HELP;
160 int main(int argc, const char **argv)
162 int nr_sha1;
163 char line[1000];
164 unsigned char sha1[2][20];
165 const char *prefix = setup_git_directory();
167 git_config(git_default_config);
168 nr_sha1 = 0;
169 diff_setup(&diff_options);
171 for (;;) {
172 int diff_opt_cnt;
173 const char *arg;
175 argv++;
176 argc--;
177 arg = *argv;
178 if (!arg)
179 break;
181 if (*arg != '-') {
182 if (nr_sha1 < 2 && !get_sha1(arg, sha1[nr_sha1])) {
183 nr_sha1++;
184 continue;
186 break;
189 diff_opt_cnt = diff_opt_parse(&diff_options, argv, argc);
190 if (diff_opt_cnt < 0)
191 usage(diff_tree_usage);
192 else if (diff_opt_cnt) {
193 argv += diff_opt_cnt - 1;
194 argc -= diff_opt_cnt - 1;
195 continue;
199 if (!strcmp(arg, "--")) {
200 argv++;
201 argc--;
202 break;
204 if (!strcmp(arg, "-r")) {
205 diff_options.recursive = 1;
206 continue;
208 if (!strcmp(arg, "-t")) {
209 diff_options.recursive = 1;
210 diff_options.tree_in_recursive = 1;
211 continue;
213 if (!strcmp(arg, "-m")) {
214 ignore_merges = 0;
215 continue;
217 if (!strcmp(arg, "-v")) {
218 verbose_header = 1;
219 header_prefix = "diff-tree ";
220 continue;
222 if (!strncmp(arg, "--pretty", 8)) {
223 verbose_header = 1;
224 header_prefix = "diff-tree ";
225 commit_format = get_commit_format(arg+8);
226 continue;
228 if (!strcmp(arg, "--stdin")) {
229 read_stdin = 1;
230 continue;
232 if (!strcmp(arg, "--root")) {
233 show_root_diff = 1;
234 continue;
236 if (!strcmp(arg, "--no-commit-id")) {
237 no_commit_id = 1;
238 continue;
240 usage(diff_tree_usage);
242 if (diff_options.output_format == DIFF_FORMAT_PATCH)
243 diff_options.recursive = 1;
245 diff_tree_setup_paths(get_pathspec(prefix, argv));
247 switch (nr_sha1) {
248 case 0:
249 if (!read_stdin)
250 usage(diff_tree_usage);
251 break;
252 case 1:
253 diff_tree_commit(sha1[0], NULL);
254 break;
255 case 2:
256 diff_tree_sha1_top(sha1[0], sha1[1], "");
257 break;
260 if (!read_stdin)
261 return 0;
263 if (diff_options.detect_rename)
264 diff_options.setup |= (DIFF_SETUP_USE_SIZE_CACHE |
265 DIFF_SETUP_USE_CACHE);
266 while (fgets(line, sizeof(line), stdin))
267 diff_tree_stdin(line);
269 return 0;