Make sure we error out if we can't remove a file on automatic merges.
[git/mingw.git] / diff-tree.c
blob6f8dc2024c289a92fe81014511a8377364340783
1 #include <ctype.h>
2 #include "cache.h"
3 #include "diff.h"
4 #include "commit.h"
6 static int show_root_diff = 0;
7 static int verbose_header = 0;
8 static int ignore_merges = 1;
9 static int recursive = 0;
10 static int show_tree_entry_in_recursive = 0;
11 static int read_stdin = 0;
12 static int diff_output_format = DIFF_FORMAT_HUMAN;
13 static int detect_rename = 0;
14 static int diff_setup_opt = 0;
15 static int diff_score_opt = 0;
16 static const char *pickaxe = NULL;
17 static int pickaxe_opts = 0;
18 static int diff_break_opt = -1;
19 static const char *orderfile = NULL;
20 static const char *header = NULL;
21 static const char *header_prefix = "";
22 static enum cmit_fmt commit_format = CMIT_FMT_RAW;
24 // What paths are we interested in?
25 static int nr_paths = 0;
26 static const char **paths = NULL;
27 static int *pathlens = NULL;
29 static int diff_tree_sha1(const unsigned char *old, const unsigned char *new, const char *base);
31 static void update_tree_entry(void **bufp, unsigned long *sizep)
33 void *buf = *bufp;
34 unsigned long size = *sizep;
35 int len = strlen(buf) + 1 + 20;
37 if (size < len)
38 die("corrupt tree file");
39 *bufp = buf + len;
40 *sizep = size - len;
43 static const unsigned char *extract(void *tree, unsigned long size, const char **pathp, unsigned int *modep)
45 int len = strlen(tree)+1;
46 const unsigned char *sha1 = tree + len;
47 const char *path = strchr(tree, ' ');
48 unsigned int mode;
50 if (!path || size < len + 20 || sscanf(tree, "%o", &mode) != 1)
51 die("corrupt tree file");
52 *pathp = path+1;
53 *modep = DIFF_FILE_CANON_MODE(mode);
54 return sha1;
57 static char *malloc_base(const char *base, const char *path, int pathlen)
59 int baselen = strlen(base);
60 char *newbase = xmalloc(baselen + pathlen + 2);
61 memcpy(newbase, base, baselen);
62 memcpy(newbase + baselen, path, pathlen);
63 memcpy(newbase + baselen + pathlen, "/", 2);
64 return newbase;
67 static void show_file(const char *prefix, void *tree, unsigned long size, const char *base);
68 static void show_tree(const char *prefix, void *tree, unsigned long size, const char *base);
70 /* A file entry went away or appeared */
71 static void show_file(const char *prefix, void *tree, unsigned long size, const char *base)
73 unsigned mode;
74 const char *path;
75 const unsigned char *sha1 = extract(tree, size, &path, &mode);
77 if (recursive && S_ISDIR(mode)) {
78 char type[20];
79 unsigned long size;
80 char *newbase = malloc_base(base, path, strlen(path));
81 void *tree;
83 tree = read_sha1_file(sha1, type, &size);
84 if (!tree || strcmp(type, "tree"))
85 die("corrupt tree sha %s", sha1_to_hex(sha1));
87 show_tree(prefix, tree, size, newbase);
89 free(tree);
90 free(newbase);
91 return;
94 diff_addremove(prefix[0], mode, sha1, base, path);
97 static int compare_tree_entry(void *tree1, unsigned long size1, void *tree2, unsigned long size2, const char *base)
99 unsigned mode1, mode2;
100 const char *path1, *path2;
101 const unsigned char *sha1, *sha2;
102 int cmp, pathlen1, pathlen2;
104 sha1 = extract(tree1, size1, &path1, &mode1);
105 sha2 = extract(tree2, size2, &path2, &mode2);
107 pathlen1 = strlen(path1);
108 pathlen2 = strlen(path2);
109 cmp = base_name_compare(path1, pathlen1, mode1, path2, pathlen2, mode2);
110 if (cmp < 0) {
111 show_file("-", tree1, size1, base);
112 return -1;
114 if (cmp > 0) {
115 show_file("+", tree2, size2, base);
116 return 1;
118 if (!memcmp(sha1, sha2, 20) && mode1 == mode2)
119 return 0;
122 * If the filemode has changed to/from a directory from/to a regular
123 * file, we need to consider it a remove and an add.
125 if (S_ISDIR(mode1) != S_ISDIR(mode2)) {
126 show_file("-", tree1, size1, base);
127 show_file("+", tree2, size2, base);
128 return 0;
131 if (recursive && S_ISDIR(mode1)) {
132 int retval;
133 char *newbase = malloc_base(base, path1, pathlen1);
134 if (show_tree_entry_in_recursive)
135 diff_change(mode1, mode2, sha1, sha2, base, path1);
136 retval = diff_tree_sha1(sha1, sha2, newbase);
137 free(newbase);
138 return retval;
141 diff_change(mode1, mode2, sha1, sha2, base, path1);
142 return 0;
145 static int interesting(void *tree, unsigned long size, const char *base)
147 const char *path;
148 unsigned mode;
149 int i;
150 int baselen, pathlen;
152 if (!nr_paths)
153 return 1;
155 (void)extract(tree, size, &path, &mode);
157 pathlen = strlen(path);
158 baselen = strlen(base);
160 for (i=0; i < nr_paths; i++) {
161 const char *match = paths[i];
162 int matchlen = pathlens[i];
164 if (baselen >= matchlen) {
165 /* If it doesn't match, move along... */
166 if (strncmp(base, match, matchlen))
167 continue;
169 /* The base is a subdirectory of a path which was specified. */
170 return 1;
173 /* Does the base match? */
174 if (strncmp(base, match, baselen))
175 continue;
177 match += baselen;
178 matchlen -= baselen;
180 if (pathlen > matchlen)
181 continue;
183 if (matchlen > pathlen) {
184 if (match[pathlen] != '/')
185 continue;
186 if (!S_ISDIR(mode))
187 continue;
190 if (strncmp(path, match, pathlen))
191 continue;
193 return 1;
195 return 0; /* No matches */
198 /* A whole sub-tree went away or appeared */
199 static void show_tree(const char *prefix, void *tree, unsigned long size, const char *base)
201 while (size) {
202 if (interesting(tree, size, base))
203 show_file(prefix, tree, size, base);
204 update_tree_entry(&tree, &size);
208 static int diff_tree(void *tree1, unsigned long size1, void *tree2, unsigned long size2, const char *base)
210 while (size1 | size2) {
211 if (nr_paths && size1 && !interesting(tree1, size1, base)) {
212 update_tree_entry(&tree1, &size1);
213 continue;
215 if (nr_paths && size2 && !interesting(tree2, size2, base)) {
216 update_tree_entry(&tree2, &size2);
217 continue;
219 if (!size1) {
220 show_file("+", tree2, size2, base);
221 update_tree_entry(&tree2, &size2);
222 continue;
224 if (!size2) {
225 show_file("-", tree1, size1, base);
226 update_tree_entry(&tree1, &size1);
227 continue;
229 switch (compare_tree_entry(tree1, size1, tree2, size2, base)) {
230 case -1:
231 update_tree_entry(&tree1, &size1);
232 continue;
233 case 0:
234 update_tree_entry(&tree1, &size1);
235 /* Fallthrough */
236 case 1:
237 update_tree_entry(&tree2, &size2);
238 continue;
240 die("git-diff-tree: internal error");
242 return 0;
245 static int diff_tree_sha1(const unsigned char *old, const unsigned char *new, const char *base)
247 void *tree1, *tree2;
248 unsigned long size1, size2;
249 int retval;
251 tree1 = read_object_with_reference(old, "tree", &size1, NULL);
252 if (!tree1)
253 die("unable to read source tree (%s)", sha1_to_hex(old));
254 tree2 = read_object_with_reference(new, "tree", &size2, NULL);
255 if (!tree2)
256 die("unable to read destination tree (%s)", sha1_to_hex(new));
257 retval = diff_tree(tree1, size1, tree2, size2, base);
258 free(tree1);
259 free(tree2);
260 return retval;
263 static void call_diff_setup(void)
265 diff_setup(diff_setup_opt);
268 static int call_diff_flush(void)
270 diffcore_std(0,
271 detect_rename, diff_score_opt,
272 pickaxe, pickaxe_opts,
273 diff_break_opt,
274 orderfile);
275 if (diff_queue_is_empty()) {
276 diff_flush(DIFF_FORMAT_NO_OUTPUT, 0);
277 return 0;
279 if (header) {
280 const char *fmt = "%s";
281 if (diff_output_format == DIFF_FORMAT_MACHINE)
282 fmt = "%s%c";
284 printf(fmt, header, 0);
285 header = NULL;
287 diff_flush(diff_output_format, 1);
288 return 1;
291 static int diff_tree_sha1_top(const unsigned char *old,
292 const unsigned char *new, const char *base)
294 int ret;
296 call_diff_setup();
297 ret = diff_tree_sha1(old, new, base);
298 call_diff_flush();
299 return ret;
302 static int diff_root_tree(const unsigned char *new, const char *base)
304 int retval;
305 void *tree;
306 unsigned long size;
308 call_diff_setup();
309 tree = read_object_with_reference(new, "tree", &size, NULL);
310 if (!tree)
311 die("unable to read root tree (%s)", sha1_to_hex(new));
312 retval = diff_tree("", 0, tree, size, base);
313 free(tree);
314 call_diff_flush();
315 return retval;
318 static char *generate_header(const char *commit, const char *parent, const char *msg, unsigned long len)
320 static char this_header[16384];
321 int offset;
323 offset = sprintf(this_header, "%s%s (from %s)\n", header_prefix, commit, parent);
324 if (verbose_header) {
325 offset += pretty_print_commit(commit_format, msg, len, this_header + offset, sizeof(this_header) - offset);
326 this_header[offset++] = '\n';
327 this_header[offset++] = 0;
330 return this_header;
333 static int diff_tree_commit(const unsigned char *commit, const char *name)
335 unsigned long size, offset;
336 char *buf = read_object_with_reference(commit, "commit", &size, NULL);
338 if (!buf)
339 return -1;
341 if (!name) {
342 static char commit_name[60];
343 strcpy(commit_name, sha1_to_hex(commit));
344 name = commit_name;
347 /* Root commit? */
348 if (show_root_diff && memcmp(buf + 46, "parent ", 7)) {
349 header = generate_header(name, "root", buf, size);
350 diff_root_tree(commit, "");
353 /* More than one parent? */
354 if (ignore_merges) {
355 if (!memcmp(buf + 46 + 48, "parent ", 7))
356 return 0;
359 offset = 46;
360 while (offset + 48 < size && !memcmp(buf + offset, "parent ", 7)) {
361 unsigned char parent[20];
362 if (get_sha1_hex(buf + offset + 7, parent))
363 return -1;
364 header = generate_header(name, sha1_to_hex(parent), buf, size);
365 diff_tree_sha1_top(parent, commit, "");
366 if (!header && verbose_header) {
367 header_prefix = "\ndiff-tree ";
369 * Don't print multiple merge entries if we
370 * don't print the diffs.
373 offset += 48;
375 return 0;
378 static int diff_tree_stdin(char *line)
380 int len = strlen(line);
381 unsigned char commit[20], parent[20];
382 static char this_header[1000];
384 if (!len || line[len-1] != '\n')
385 return -1;
386 line[len-1] = 0;
387 if (get_sha1_hex(line, commit))
388 return -1;
389 if (isspace(line[40]) && !get_sha1_hex(line+41, parent)) {
390 line[40] = 0;
391 line[81] = 0;
392 sprintf(this_header, "%s (from %s)\n", line, line+41);
393 header = this_header;
394 return diff_tree_sha1_top(parent, commit, "");
396 line[40] = 0;
397 return diff_tree_commit(commit, line);
400 static char *diff_tree_usage =
401 "git-diff-tree [-p] [-r] [-z] [--stdin] [-M] [-C] [-R] [-S<string>] [-O<orderfile>] [-m] [-s] [-v] [-t] <tree-ish> <tree-ish>";
403 int main(int argc, const char **argv)
405 int nr_sha1;
406 char line[1000];
407 unsigned char sha1[2][20];
409 nr_sha1 = 0;
410 for (;;) {
411 const char *arg;
413 argv++;
414 argc--;
415 arg = *argv;
416 if (!arg)
417 break;
419 if (*arg != '-') {
420 if (nr_sha1 < 2 && !get_sha1(arg, sha1[nr_sha1])) {
421 nr_sha1++;
422 continue;
424 break;
427 if (!strcmp(arg, "--")) {
428 argv++;
429 argc--;
430 break;
432 if (!strcmp(arg, "-r")) {
433 recursive = 1;
434 continue;
436 if (!strcmp(arg, "-t")) {
437 recursive = show_tree_entry_in_recursive = 1;
438 continue;
440 if (!strcmp(arg, "-R")) {
441 diff_setup_opt |= DIFF_SETUP_REVERSE;
442 continue;
444 if (!strcmp(arg, "-p")) {
445 diff_output_format = DIFF_FORMAT_PATCH;
446 recursive = 1;
447 continue;
449 if (!strncmp(arg, "-S", 2)) {
450 pickaxe = arg + 2;
451 continue;
453 if (!strncmp(arg, "-O", 2)) {
454 orderfile = arg + 2;
455 continue;
457 if (!strcmp(arg, "--pickaxe-all")) {
458 pickaxe_opts = DIFF_PICKAXE_ALL;
459 continue;
461 if (!strncmp(arg, "-M", 2)) {
462 detect_rename = DIFF_DETECT_RENAME;
463 if ((diff_score_opt = diff_scoreopt_parse(arg)) == -1)
464 usage(diff_tree_usage);
465 continue;
467 if (!strncmp(arg, "-C", 2)) {
468 detect_rename = DIFF_DETECT_COPY;
469 if ((diff_score_opt = diff_scoreopt_parse(arg)) == -1)
470 usage(diff_tree_usage);
471 continue;
473 if (!strncmp(arg, "-B", 2)) {
474 if ((diff_break_opt = diff_scoreopt_parse(arg)) == -1)
475 usage(diff_tree_usage);
476 continue;
478 if (!strcmp(arg, "-z")) {
479 diff_output_format = DIFF_FORMAT_MACHINE;
480 continue;
482 if (!strcmp(arg, "-m")) {
483 ignore_merges = 0;
484 continue;
486 if (!strcmp(arg, "-s")) {
487 diff_output_format = DIFF_FORMAT_NO_OUTPUT;
488 continue;
490 if (!strcmp(arg, "-v")) {
491 verbose_header = 1;
492 header_prefix = "diff-tree ";
493 continue;
495 if (!strcmp(arg, "--stdin")) {
496 read_stdin = 1;
497 continue;
499 if (!strcmp(arg, "--root")) {
500 show_root_diff = 1;
501 continue;
503 usage(diff_tree_usage);
506 if (argc > 0) {
507 int i;
509 paths = argv;
510 nr_paths = argc;
511 pathlens = xmalloc(nr_paths * sizeof(int));
512 for (i=0; i<nr_paths; i++)
513 pathlens[i] = strlen(paths[i]);
516 switch (nr_sha1) {
517 case 0:
518 if (!read_stdin)
519 usage(diff_tree_usage);
520 break;
521 case 1:
522 diff_tree_commit(sha1[0], NULL);
523 break;
524 case 2:
525 diff_tree_sha1_top(sha1[0], sha1[1], "");
526 break;
529 if (!read_stdin)
530 return 0;
532 if (detect_rename)
533 diff_setup_opt |= (DIFF_SETUP_USE_SIZE_CACHE |
534 DIFF_SETUP_USE_CACHE);
535 while (fgets(line, sizeof(line), stdin))
536 diff_tree_stdin(line);
538 return 0;