[PATCH] Remove leftover comment from documentation.
[alt-git.git] / diff-tree.c
blob4e80526afa3f55f71d0309c47b5eb1f04d6be402
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 find_copies_harder = 0;
15 static int diff_setup_opt = 0;
16 static int diff_score_opt = 0;
17 static const char *pickaxe = NULL;
18 static int pickaxe_opts = 0;
19 static int diff_break_opt = -1;
20 static const char *orderfile = NULL;
21 static const char *diff_filter = NULL;
22 static const char *header = NULL;
23 static const char *header_prefix = "";
24 static enum cmit_fmt commit_format = CMIT_FMT_RAW;
26 // What paths are we interested in?
27 static int nr_paths = 0;
28 static const char **paths = NULL;
29 static int *pathlens = NULL;
31 static int diff_tree_sha1(const unsigned char *old, const unsigned char *new, const char *base);
33 static void update_tree_entry(void **bufp, unsigned long *sizep)
35 void *buf = *bufp;
36 unsigned long size = *sizep;
37 int len = strlen(buf) + 1 + 20;
39 if (size < len)
40 die("corrupt tree file");
41 *bufp = buf + len;
42 *sizep = size - len;
45 static const unsigned char *extract(void *tree, unsigned long size, const char **pathp, unsigned int *modep)
47 int len = strlen(tree)+1;
48 const unsigned char *sha1 = tree + len;
49 const char *path = strchr(tree, ' ');
50 unsigned int mode;
52 if (!path || size < len + 20 || sscanf(tree, "%o", &mode) != 1)
53 die("corrupt tree file");
54 *pathp = path+1;
55 *modep = DIFF_FILE_CANON_MODE(mode);
56 return sha1;
59 static char *malloc_base(const char *base, const char *path, int pathlen)
61 int baselen = strlen(base);
62 char *newbase = xmalloc(baselen + pathlen + 2);
63 memcpy(newbase, base, baselen);
64 memcpy(newbase + baselen, path, pathlen);
65 memcpy(newbase + baselen + pathlen, "/", 2);
66 return newbase;
69 static void show_file(const char *prefix, void *tree, unsigned long size, const char *base);
70 static void show_tree(const char *prefix, void *tree, unsigned long size, const char *base);
72 /* A file entry went away or appeared */
73 static void show_file(const char *prefix, void *tree, unsigned long size, const char *base)
75 unsigned mode;
76 const char *path;
77 const unsigned char *sha1 = extract(tree, size, &path, &mode);
79 if (recursive && S_ISDIR(mode)) {
80 char type[20];
81 unsigned long size;
82 char *newbase = malloc_base(base, path, strlen(path));
83 void *tree;
85 tree = read_sha1_file(sha1, type, &size);
86 if (!tree || strcmp(type, "tree"))
87 die("corrupt tree sha %s", sha1_to_hex(sha1));
89 show_tree(prefix, tree, size, newbase);
91 free(tree);
92 free(newbase);
93 return;
96 diff_addremove(prefix[0], mode, sha1, base, path);
99 static int compare_tree_entry(void *tree1, unsigned long size1, void *tree2, unsigned long size2, const char *base)
101 unsigned mode1, mode2;
102 const char *path1, *path2;
103 const unsigned char *sha1, *sha2;
104 int cmp, pathlen1, pathlen2;
106 sha1 = extract(tree1, size1, &path1, &mode1);
107 sha2 = extract(tree2, size2, &path2, &mode2);
109 pathlen1 = strlen(path1);
110 pathlen2 = strlen(path2);
111 cmp = base_name_compare(path1, pathlen1, mode1, path2, pathlen2, mode2);
112 if (cmp < 0) {
113 show_file("-", tree1, size1, base);
114 return -1;
116 if (cmp > 0) {
117 show_file("+", tree2, size2, base);
118 return 1;
120 if (!find_copies_harder && !memcmp(sha1, sha2, 20) && mode1 == mode2)
121 return 0;
124 * If the filemode has changed to/from a directory from/to a regular
125 * file, we need to consider it a remove and an add.
127 if (S_ISDIR(mode1) != S_ISDIR(mode2)) {
128 show_file("-", tree1, size1, base);
129 show_file("+", tree2, size2, base);
130 return 0;
133 if (recursive && S_ISDIR(mode1)) {
134 int retval;
135 char *newbase = malloc_base(base, path1, pathlen1);
136 if (show_tree_entry_in_recursive)
137 diff_change(mode1, mode2, sha1, sha2, base, path1);
138 retval = diff_tree_sha1(sha1, sha2, newbase);
139 free(newbase);
140 return retval;
143 diff_change(mode1, mode2, sha1, sha2, base, path1);
144 return 0;
147 static int interesting(void *tree, unsigned long size, const char *base)
149 const char *path;
150 unsigned mode;
151 int i;
152 int baselen, pathlen;
154 if (!nr_paths)
155 return 1;
157 (void)extract(tree, size, &path, &mode);
159 pathlen = strlen(path);
160 baselen = strlen(base);
162 for (i=0; i < nr_paths; i++) {
163 const char *match = paths[i];
164 int matchlen = pathlens[i];
166 if (baselen >= matchlen) {
167 /* If it doesn't match, move along... */
168 if (strncmp(base, match, matchlen))
169 continue;
171 /* The base is a subdirectory of a path which was specified. */
172 return 1;
175 /* Does the base match? */
176 if (strncmp(base, match, baselen))
177 continue;
179 match += baselen;
180 matchlen -= baselen;
182 if (pathlen > matchlen)
183 continue;
185 if (matchlen > pathlen) {
186 if (match[pathlen] != '/')
187 continue;
188 if (!S_ISDIR(mode))
189 continue;
192 if (strncmp(path, match, pathlen))
193 continue;
195 return 1;
197 return 0; /* No matches */
200 /* A whole sub-tree went away or appeared */
201 static void show_tree(const char *prefix, void *tree, unsigned long size, const char *base)
203 while (size) {
204 if (interesting(tree, size, base))
205 show_file(prefix, tree, size, base);
206 update_tree_entry(&tree, &size);
210 static int diff_tree(void *tree1, unsigned long size1, void *tree2, unsigned long size2, const char *base)
212 while (size1 | size2) {
213 if (nr_paths && size1 && !interesting(tree1, size1, base)) {
214 update_tree_entry(&tree1, &size1);
215 continue;
217 if (nr_paths && size2 && !interesting(tree2, size2, base)) {
218 update_tree_entry(&tree2, &size2);
219 continue;
221 if (!size1) {
222 show_file("+", tree2, size2, base);
223 update_tree_entry(&tree2, &size2);
224 continue;
226 if (!size2) {
227 show_file("-", tree1, size1, base);
228 update_tree_entry(&tree1, &size1);
229 continue;
231 switch (compare_tree_entry(tree1, size1, tree2, size2, base)) {
232 case -1:
233 update_tree_entry(&tree1, &size1);
234 continue;
235 case 0:
236 update_tree_entry(&tree1, &size1);
237 /* Fallthrough */
238 case 1:
239 update_tree_entry(&tree2, &size2);
240 continue;
242 die("git-diff-tree: internal error");
244 return 0;
247 static int diff_tree_sha1(const unsigned char *old, const unsigned char *new, const char *base)
249 void *tree1, *tree2;
250 unsigned long size1, size2;
251 int retval;
253 tree1 = read_object_with_reference(old, "tree", &size1, NULL);
254 if (!tree1)
255 die("unable to read source tree (%s)", sha1_to_hex(old));
256 tree2 = read_object_with_reference(new, "tree", &size2, NULL);
257 if (!tree2)
258 die("unable to read destination tree (%s)", sha1_to_hex(new));
259 retval = diff_tree(tree1, size1, tree2, size2, base);
260 free(tree1);
261 free(tree2);
262 return retval;
265 static void call_diff_setup(void)
267 diff_setup(diff_setup_opt);
270 static int call_diff_flush(void)
272 diffcore_std(NULL,
273 detect_rename, diff_score_opt,
274 pickaxe, pickaxe_opts,
275 diff_break_opt,
276 orderfile,
277 diff_filter);
278 if (diff_queue_is_empty()) {
279 diff_flush(DIFF_FORMAT_NO_OUTPUT);
280 return 0;
282 if (header) {
283 printf("%s%c", header, diff_output_format == DIFF_FORMAT_MACHINE ? 0 : '\n');
284 header = NULL;
286 diff_flush(diff_output_format);
287 return 1;
290 static int diff_tree_sha1_top(const unsigned char *old,
291 const unsigned char *new, const char *base)
293 int ret;
295 call_diff_setup();
296 ret = diff_tree_sha1(old, new, base);
297 call_diff_flush();
298 return ret;
301 static int diff_root_tree(const unsigned char *new, const char *base)
303 int retval;
304 void *tree;
305 unsigned long size;
307 call_diff_setup();
308 tree = read_object_with_reference(new, "tree", &size, NULL);
309 if (!tree)
310 die("unable to read root tree (%s)", sha1_to_hex(new));
311 retval = diff_tree("", 0, tree, size, base);
312 free(tree);
313 call_diff_flush();
314 return retval;
317 static const char *generate_header(const char *commit, const char *parent, const char *msg, unsigned long len)
319 static char this_header[16384];
320 int offset;
322 if (!verbose_header)
323 return commit;
325 offset = sprintf(this_header, "%s%s (from %s)\n", header_prefix, commit, parent);
326 offset += pretty_print_commit(commit_format, msg, len, this_header + offset, sizeof(this_header) - offset);
327 return this_header;
330 static int diff_tree_commit(const unsigned char *commit, const char *name)
332 unsigned long size, offset;
333 char *buf = read_object_with_reference(commit, "commit", &size, NULL);
335 if (!buf)
336 return -1;
338 if (!name) {
339 static char commit_name[60];
340 strcpy(commit_name, sha1_to_hex(commit));
341 name = commit_name;
344 /* Root commit? */
345 if (show_root_diff && memcmp(buf + 46, "parent ", 7)) {
346 header = generate_header(name, "root", buf, size);
347 diff_root_tree(commit, "");
350 /* More than one parent? */
351 if (ignore_merges) {
352 if (!memcmp(buf + 46 + 48, "parent ", 7))
353 return 0;
356 offset = 46;
357 while (offset + 48 < size && !memcmp(buf + offset, "parent ", 7)) {
358 unsigned char parent[20];
359 if (get_sha1_hex(buf + offset + 7, parent))
360 return -1;
361 header = generate_header(name, sha1_to_hex(parent), buf, size);
362 diff_tree_sha1_top(parent, commit, "");
363 if (!header && verbose_header) {
364 header_prefix = "\ndiff-tree ";
366 * Don't print multiple merge entries if we
367 * don't print the diffs.
370 offset += 48;
372 return 0;
375 static int diff_tree_stdin(char *line)
377 int len = strlen(line);
378 unsigned char commit[20], parent[20];
379 static char this_header[1000];
381 if (!len || line[len-1] != '\n')
382 return -1;
383 line[len-1] = 0;
384 if (get_sha1_hex(line, commit))
385 return -1;
386 if (isspace(line[40]) && !get_sha1_hex(line+41, parent)) {
387 line[40] = 0;
388 line[81] = 0;
389 sprintf(this_header, "%s (from %s)\n", line, line+41);
390 header = this_header;
391 return diff_tree_sha1_top(parent, commit, "");
393 line[40] = 0;
394 return diff_tree_commit(commit, line);
397 static char *diff_tree_usage =
398 "git-diff-tree [--stdin] [-m] [-s] [-v] [--pretty] [-t] "
399 "[<common diff options>] <tree-ish> <tree-ish>"
400 COMMON_DIFF_OPTIONS_HELP;
402 int main(int argc, const char **argv)
404 int nr_sha1;
405 char line[1000];
406 unsigned char sha1[2][20];
408 nr_sha1 = 0;
409 for (;;) {
410 const char *arg;
412 argv++;
413 argc--;
414 arg = *argv;
415 if (!arg)
416 break;
418 if (*arg != '-') {
419 if (nr_sha1 < 2 && !get_sha1(arg, sha1[nr_sha1])) {
420 nr_sha1++;
421 continue;
423 break;
426 if (!strcmp(arg, "--")) {
427 argv++;
428 argc--;
429 break;
431 if (!strcmp(arg, "-r")) {
432 recursive = 1;
433 continue;
435 if (!strcmp(arg, "-t")) {
436 recursive = show_tree_entry_in_recursive = 1;
437 continue;
439 if (!strcmp(arg, "-R")) {
440 diff_setup_opt |= DIFF_SETUP_REVERSE;
441 continue;
443 if (!strcmp(arg, "-p") || !strcmp(arg, "-u")) {
444 diff_output_format = DIFF_FORMAT_PATCH;
445 recursive = 1;
446 continue;
448 if (!strncmp(arg, "-S", 2)) {
449 pickaxe = arg + 2;
450 continue;
452 if (!strncmp(arg, "-O", 2)) {
453 orderfile = arg + 2;
454 continue;
456 if (!strncmp(arg, "--diff-filter=", 14)) {
457 diff_filter = arg + 14;
458 continue;
460 if (!strcmp(arg, "--pickaxe-all")) {
461 pickaxe_opts = DIFF_PICKAXE_ALL;
462 continue;
464 if (!strncmp(arg, "-M", 2)) {
465 detect_rename = DIFF_DETECT_RENAME;
466 if ((diff_score_opt = diff_scoreopt_parse(arg)) == -1)
467 usage(diff_tree_usage);
468 continue;
470 if (!strncmp(arg, "-C", 2)) {
471 detect_rename = DIFF_DETECT_COPY;
472 if ((diff_score_opt = diff_scoreopt_parse(arg)) == -1)
473 usage(diff_tree_usage);
474 continue;
476 if (!strncmp(arg, "-B", 2)) {
477 if ((diff_break_opt = diff_scoreopt_parse(arg)) == -1)
478 usage(diff_tree_usage);
479 continue;
481 if (!strcmp(arg, "--find-copies-harder")) {
482 find_copies_harder = 1;
483 continue;
485 if (!strcmp(arg, "--name-only")) {
486 diff_output_format = DIFF_FORMAT_NAME;
487 continue;
489 if (!strcmp(arg, "--name-only-z")) {
490 diff_output_format = DIFF_FORMAT_NAME_Z;
491 continue;
493 if (!strcmp(arg, "-z")) {
494 diff_output_format = DIFF_FORMAT_MACHINE;
495 continue;
497 if (!strcmp(arg, "-m")) {
498 ignore_merges = 0;
499 continue;
501 if (!strcmp(arg, "-s")) {
502 diff_output_format = DIFF_FORMAT_NO_OUTPUT;
503 continue;
505 if (!strcmp(arg, "-v")) {
506 verbose_header = 1;
507 header_prefix = "diff-tree ";
508 continue;
510 if (!strncmp(arg, "--pretty", 8)) {
511 verbose_header = 1;
512 header_prefix = "diff-tree ";
513 commit_format = get_commit_format(arg+8);
514 continue;
516 if (!strcmp(arg, "--stdin")) {
517 read_stdin = 1;
518 continue;
520 if (!strcmp(arg, "--root")) {
521 show_root_diff = 1;
522 continue;
524 usage(diff_tree_usage);
526 if (find_copies_harder && detect_rename != DIFF_DETECT_COPY)
527 usage(diff_tree_usage);
529 if (argc > 0) {
530 int i;
532 paths = argv;
533 nr_paths = argc;
534 pathlens = xmalloc(nr_paths * sizeof(int));
535 for (i=0; i<nr_paths; i++)
536 pathlens[i] = strlen(paths[i]);
539 switch (nr_sha1) {
540 case 0:
541 if (!read_stdin)
542 usage(diff_tree_usage);
543 break;
544 case 1:
545 diff_tree_commit(sha1[0], NULL);
546 break;
547 case 2:
548 diff_tree_sha1_top(sha1[0], sha1[1], "");
549 break;
552 if (!read_stdin)
553 return 0;
555 if (detect_rename)
556 diff_setup_opt |= (DIFF_SETUP_USE_SIZE_CACHE |
557 DIFF_SETUP_USE_CACHE);
558 while (fgets(line, sizeof(line), stdin))
559 diff_tree_stdin(line);
561 return 0;