[PATCH] Fix tweak in similarity estimator.
[git/debian.git] / diff-tree.c
blob3428a6ee9db212b8fbf13d81602fe44394dae4cb
1 #include <ctype.h>
2 #include "cache.h"
3 #include "diff.h"
5 static int silent = 0;
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 read_stdin = 0;
11 static int diff_output_format = DIFF_FORMAT_HUMAN;
12 static int detect_rename = 0;
13 static int reverse_diff = 0;
14 static int diff_score_opt = 0;
15 static const char *pickaxe = NULL;
16 static const char *header = NULL;
17 static const char *header_prefix = "";
19 // What paths are we interested in?
20 static int nr_paths = 0;
21 static char **paths = NULL;
22 static int *pathlens = NULL;
24 static int diff_tree_sha1(const unsigned char *old, const unsigned char *new, const char *base);
26 static void update_tree_entry(void **bufp, unsigned long *sizep)
28 void *buf = *bufp;
29 unsigned long size = *sizep;
30 int len = strlen(buf) + 1 + 20;
32 if (size < len)
33 die("corrupt tree file");
34 *bufp = buf + len;
35 *sizep = size - len;
38 static const unsigned char *extract(void *tree, unsigned long size, const char **pathp, unsigned int *modep)
40 int len = strlen(tree)+1;
41 const unsigned char *sha1 = tree + len;
42 const char *path = strchr(tree, ' ');
44 if (!path || size < len + 20 || sscanf(tree, "%o", modep) != 1)
45 die("corrupt tree file");
46 *pathp = path+1;
47 return sha1;
50 static char *malloc_base(const char *base, const char *path, int pathlen)
52 int baselen = strlen(base);
53 char *newbase = xmalloc(baselen + pathlen + 2);
54 memcpy(newbase, base, baselen);
55 memcpy(newbase + baselen, path, pathlen);
56 memcpy(newbase + baselen + pathlen, "/", 2);
57 return newbase;
60 static void show_file(const char *prefix, void *tree, unsigned long size, const char *base);
61 static void show_tree(const char *prefix, void *tree, unsigned long size, const char *base);
63 /* A file entry went away or appeared */
64 static void show_file(const char *prefix, void *tree, unsigned long size, const char *base)
66 unsigned mode;
67 const char *path;
68 const unsigned char *sha1 = extract(tree, size, &path, &mode);
70 if (header) {
71 printf("%s", header);
72 header = NULL;
75 if (silent)
76 return;
78 if (recursive && S_ISDIR(mode)) {
79 char type[20];
80 unsigned long size;
81 char *newbase = malloc_base(base, path, strlen(path));
82 void *tree;
84 tree = read_sha1_file(sha1, type, &size);
85 if (!tree || strcmp(type, "tree"))
86 die("corrupt tree sha %s", sha1_to_hex(sha1));
88 show_tree(prefix, tree, size, newbase);
90 free(tree);
91 free(newbase);
92 return;
95 diff_addremove(prefix[0], mode, sha1, base, path);
98 static int compare_tree_entry(void *tree1, unsigned long size1, void *tree2, unsigned long size2, const char *base)
100 unsigned mode1, mode2;
101 const char *path1, *path2;
102 const unsigned char *sha1, *sha2;
103 int cmp, pathlen1, pathlen2;
105 sha1 = extract(tree1, size1, &path1, &mode1);
106 sha2 = extract(tree2, size2, &path2, &mode2);
108 pathlen1 = strlen(path1);
109 pathlen2 = strlen(path2);
110 cmp = base_name_compare(path1, pathlen1, mode1, path2, pathlen2, mode2);
111 if (cmp < 0) {
112 show_file("-", tree1, size1, base);
113 return -1;
115 if (cmp > 0) {
116 show_file("+", tree2, size2, base);
117 return 1;
119 if (!memcmp(sha1, sha2, 20) && mode1 == mode2)
120 return 0;
123 * If the filemode has changed to/from a directory from/to a regular
124 * file, we need to consider it a remove and an add.
126 if (S_ISDIR(mode1) != S_ISDIR(mode2)) {
127 show_file("-", tree1, size1, base);
128 show_file("+", tree2, size2, base);
129 return 0;
132 if (recursive && S_ISDIR(mode1)) {
133 int retval;
134 char *newbase = malloc_base(base, path1, pathlen1);
135 retval = diff_tree_sha1(sha1, sha2, newbase);
136 free(newbase);
137 return retval;
140 if (header) {
141 printf("%s", header);
142 header = NULL;
144 if (silent)
145 return 0;
147 diff_change(mode1, mode2, sha1, sha2, base, path1);
148 return 0;
151 static int interesting(void *tree, unsigned long size, const char *base)
153 const char *path;
154 unsigned mode;
155 int i;
156 int baselen, pathlen;
158 if (!nr_paths)
159 return 1;
161 (void)extract(tree, size, &path, &mode);
163 pathlen = strlen(path);
164 baselen = strlen(base);
166 for (i=0; i < nr_paths; i++) {
167 const char *match = paths[i];
168 int matchlen = pathlens[i];
170 if (baselen >= matchlen) {
171 /* If it doesn't match, move along... */
172 if (strncmp(base, match, matchlen))
173 continue;
175 /* The base is a subdirectory of a path which was specified. */
176 return 1;
179 /* Does the base match? */
180 if (strncmp(base, match, baselen))
181 continue;
183 match += baselen;
184 matchlen -= baselen;
186 if (pathlen > matchlen)
187 continue;
189 if (matchlen > pathlen) {
190 if (match[pathlen] != '/')
191 continue;
192 if (!S_ISDIR(mode))
193 continue;
196 if (strncmp(path, match, pathlen))
197 continue;
199 return 1;
201 return 0; /* No matches */
204 /* A whole sub-tree went away or appeared */
205 static void show_tree(const char *prefix, void *tree, unsigned long size, const char *base)
207 while (size) {
208 if (interesting(tree, size, base))
209 show_file(prefix, tree, size, base);
210 update_tree_entry(&tree, &size);
214 static int diff_tree(void *tree1, unsigned long size1, void *tree2, unsigned long size2, const char *base)
216 while (size1 | size2) {
217 if (nr_paths && size1 && !interesting(tree1, size1, base)) {
218 update_tree_entry(&tree1, &size1);
219 continue;
221 if (nr_paths && size2 && !interesting(tree2, size2, base)) {
222 update_tree_entry(&tree2, &size2);
223 continue;
225 if (!size1) {
226 show_file("+", tree2, size2, base);
227 update_tree_entry(&tree2, &size2);
228 continue;
230 if (!size2) {
231 show_file("-", tree1, size1, base);
232 update_tree_entry(&tree1, &size1);
233 continue;
235 switch (compare_tree_entry(tree1, size1, tree2, size2, base)) {
236 case -1:
237 update_tree_entry(&tree1, &size1);
238 continue;
239 case 0:
240 update_tree_entry(&tree1, &size1);
241 /* Fallthrough */
242 case 1:
243 update_tree_entry(&tree2, &size2);
244 continue;
246 die("git-diff-tree: internal error");
248 return 0;
251 static int diff_tree_sha1(const unsigned char *old, const unsigned char *new, const char *base)
253 void *tree1, *tree2;
254 unsigned long size1, size2;
255 int retval;
257 tree1 = read_object_with_reference(old, "tree", &size1, NULL);
258 if (!tree1)
259 die("unable to read source tree (%s)", sha1_to_hex(old));
260 tree2 = read_object_with_reference(new, "tree", &size2, NULL);
261 if (!tree2)
262 die("unable to read destination tree (%s)", sha1_to_hex(new));
263 retval = diff_tree(tree1, size1, tree2, size2, base);
264 free(tree1);
265 free(tree2);
266 return retval;
269 static void call_diff_setup(void)
271 diff_setup(reverse_diff, diff_output_format);
274 static void call_diff_flush(void)
276 if (detect_rename)
277 diff_detect_rename(detect_rename, diff_score_opt);
278 if (pickaxe)
279 diff_pickaxe(pickaxe);
280 diff_flush(NULL, 0);
283 static int diff_tree_sha1_top(const unsigned char *old,
284 const unsigned char *new, const char *base)
286 int ret;
288 call_diff_setup();
289 ret = diff_tree_sha1(old, new, base);
290 call_diff_flush();
291 return ret;
294 static int diff_root_tree(const unsigned char *new, const char *base)
296 int retval;
297 void *tree;
298 unsigned long size;
300 call_diff_setup();
301 tree = read_object_with_reference(new, "tree", &size, NULL);
302 if (!tree)
303 die("unable to read root tree (%s)", sha1_to_hex(new));
304 retval = diff_tree("", 0, tree, size, base);
305 free(tree);
306 call_diff_flush();
307 return retval;
310 static int get_one_line(const char *msg, unsigned long len)
312 int ret = 0;
314 while (len--) {
315 ret++;
316 if (*msg++ == '\n')
317 break;
319 return ret;
322 static int add_author_info(char *buf, const char *line, int len)
324 char *date;
325 unsigned int namelen;
326 unsigned long time;
327 int tz;
329 line += strlen("author ");
330 date = strchr(line, '>');
331 if (!date)
332 return 0;
333 namelen = ++date - line;
334 time = strtoul(date, &date, 10);
335 tz = strtol(date, NULL, 10);
337 return sprintf(buf, "Author: %.*s\nDate: %s\n",
338 namelen, line,
339 show_date(time, tz));
342 static char *generate_header(const char *commit, const char *parent, const char *msg, unsigned long len)
344 static char this_header[16384];
345 int offset;
347 offset = sprintf(this_header, "%s%s (from %s)\n", header_prefix, commit, parent);
348 if (verbose_header) {
349 int hdr = 1;
351 for (;;) {
352 const char *line = msg;
353 int linelen = get_one_line(msg, len);
355 if (!linelen)
356 break;
359 * We want some slop for indentation and a possible
360 * final "...". Thus the "+ 20".
362 if (offset + linelen + 20 > sizeof(this_header)) {
363 memcpy(this_header + offset, " ...\n", 8);
364 offset += 8;
365 break;
368 msg += linelen;
369 len -= linelen;
370 if (linelen == 1)
371 hdr = 0;
372 if (hdr) {
373 if (!memcmp(line, "author ", 7))
374 offset += add_author_info(this_header + offset, line, linelen);
375 continue;
377 memset(this_header + offset, ' ', 4);
378 memcpy(this_header + offset + 4, line, linelen);
379 offset += linelen + 4;
381 /* Make sure there is an EOLN */
382 if (this_header[offset-1] != '\n')
383 this_header[offset++] = '\n';
384 /* Add _another_ EOLN if we are doing diff output */
385 if (!silent)
386 this_header[offset++] = '\n';
387 this_header[offset] = 0;
390 return this_header;
393 static int diff_tree_commit(const unsigned char *commit, const char *name)
395 unsigned long size, offset;
396 char *buf = read_object_with_reference(commit, "commit", &size, NULL);
398 if (!buf)
399 return -1;
401 if (!name) {
402 static char commit_name[60];
403 strcpy(commit_name, sha1_to_hex(commit));
404 name = commit_name;
407 /* Root commit? */
408 if (show_root_diff && memcmp(buf + 46, "parent ", 7)) {
409 header = generate_header(name, "root", buf, size);
410 diff_root_tree(commit, "");
413 /* More than one parent? */
414 if (ignore_merges) {
415 if (!memcmp(buf + 46 + 48, "parent ", 7))
416 return 0;
419 offset = 46;
420 while (offset + 48 < size && !memcmp(buf + offset, "parent ", 7)) {
421 unsigned char parent[20];
422 if (get_sha1_hex(buf + offset + 7, parent))
423 return -1;
424 header = generate_header(name, sha1_to_hex(parent), buf, size);
425 diff_tree_sha1_top(parent, commit, "");
426 if (!header && verbose_header) {
427 header_prefix = "\ndiff-tree ";
429 * Don't print multiple merge entries if we
430 * don't print the diffs.
432 if (silent)
433 break;
435 offset += 48;
437 return 0;
440 static int diff_tree_stdin(char *line)
442 int len = strlen(line);
443 unsigned char commit[20], parent[20];
444 static char this_header[1000];
446 if (!len || line[len-1] != '\n')
447 return -1;
448 line[len-1] = 0;
449 if (get_sha1_hex(line, commit))
450 return -1;
451 if (isspace(line[40]) && !get_sha1_hex(line+41, parent)) {
452 line[40] = 0;
453 line[81] = 0;
454 sprintf(this_header, "%s (from %s)\n", line, line+41);
455 header = this_header;
456 return diff_tree_sha1_top(parent, commit, "");
458 line[40] = 0;
459 return diff_tree_commit(commit, line);
462 static char *diff_tree_usage =
463 "git-diff-tree [-p] [-r] [-z] [--stdin] [-M] [-C] [-R] [-S<string>] [-m] [-s] [-v] <tree-ish> <tree-ish>";
465 int main(int argc, char **argv)
467 int nr_sha1;
468 char line[1000];
469 unsigned char sha1[2][20];
471 nr_sha1 = 0;
472 for (;;) {
473 char *arg;
475 argv++;
476 argc--;
477 arg = *argv;
478 if (!arg)
479 break;
481 if (*arg != '-') {
482 if (nr_sha1 < 2 && !get_sha1(arg, sha1[nr_sha1])) {
483 nr_sha1++;
484 continue;
486 break;
489 if (!strcmp(arg, "--")) {
490 argv++;
491 argc--;
492 break;
494 if (!strcmp(arg, "-r")) {
495 recursive = 1;
496 continue;
498 if (!strcmp(arg, "-R")) {
499 reverse_diff = 1;
500 continue;
502 if (!strcmp(arg, "-p")) {
503 diff_output_format = DIFF_FORMAT_PATCH;
504 recursive = 1;
505 continue;
507 if (!strncmp(arg, "-S", 2)) {
508 pickaxe = arg + 2;
509 continue;
511 if (!strncmp(arg, "-M", 2)) {
512 detect_rename = 1;
513 diff_score_opt = diff_scoreopt_parse(arg);
514 continue;
516 if (!strncmp(arg, "-C", 2)) {
517 detect_rename = 2;
518 diff_score_opt = diff_scoreopt_parse(arg);
519 continue;
521 if (!strcmp(arg, "-z")) {
522 diff_output_format = DIFF_FORMAT_MACHINE;
523 continue;
525 if (!strcmp(arg, "-m")) {
526 ignore_merges = 0;
527 continue;
529 if (!strcmp(arg, "-s")) {
530 silent = 1;
531 continue;
533 if (!strcmp(arg, "-v")) {
534 verbose_header = 1;
535 header_prefix = "diff-tree ";
536 continue;
538 if (!strcmp(arg, "--stdin")) {
539 read_stdin = 1;
540 continue;
542 if (!strcmp(arg, "--root")) {
543 show_root_diff = 1;
544 continue;
546 usage(diff_tree_usage);
549 if (argc > 0) {
550 int i;
552 paths = argv;
553 nr_paths = argc;
554 pathlens = xmalloc(nr_paths * sizeof(int));
555 for (i=0; i<nr_paths; i++)
556 pathlens[i] = strlen(paths[i]);
559 switch (nr_sha1) {
560 case 0:
561 if (!read_stdin)
562 usage(diff_tree_usage);
563 break;
564 case 1:
565 diff_tree_commit(sha1[0], NULL);
566 break;
567 case 2:
568 diff_tree_sha1_top(sha1[0], sha1[1], "");
569 break;
572 if (!read_stdin)
573 return 0;
575 while (fgets(line, sizeof(line), stdin))
576 diff_tree_stdin(line);
578 return 0;