unpack_sha1_file: zero-pad the unpacked object.
[git.git] / diff-tree.c
blob7e16be42614a32a86897a4fab322757379ad3add
1 #include <ctype.h>
2 #include "cache.h"
3 #include "diff.h"
5 static int show_root_diff = 0;
6 static int verbose_header = 0;
7 static int ignore_merges = 1;
8 static int recursive = 0;
9 static int show_tree_entry_in_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 const 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 (recursive && S_ISDIR(mode)) {
71 char type[20];
72 unsigned long size;
73 char *newbase = malloc_base(base, path, strlen(path));
74 void *tree;
76 tree = read_sha1_file(sha1, type, &size);
77 if (!tree || strcmp(type, "tree"))
78 die("corrupt tree sha %s", sha1_to_hex(sha1));
80 show_tree(prefix, tree, size, newbase);
82 free(tree);
83 free(newbase);
84 return;
87 diff_addremove(prefix[0], mode, sha1, base, path);
90 static int compare_tree_entry(void *tree1, unsigned long size1, void *tree2, unsigned long size2, const char *base)
92 unsigned mode1, mode2;
93 const char *path1, *path2;
94 const unsigned char *sha1, *sha2;
95 int cmp, pathlen1, pathlen2;
97 sha1 = extract(tree1, size1, &path1, &mode1);
98 sha2 = extract(tree2, size2, &path2, &mode2);
100 pathlen1 = strlen(path1);
101 pathlen2 = strlen(path2);
102 cmp = base_name_compare(path1, pathlen1, mode1, path2, pathlen2, mode2);
103 if (cmp < 0) {
104 show_file("-", tree1, size1, base);
105 return -1;
107 if (cmp > 0) {
108 show_file("+", tree2, size2, base);
109 return 1;
111 if (!memcmp(sha1, sha2, 20) && mode1 == mode2)
112 return 0;
115 * If the filemode has changed to/from a directory from/to a regular
116 * file, we need to consider it a remove and an add.
118 if (S_ISDIR(mode1) != S_ISDIR(mode2)) {
119 show_file("-", tree1, size1, base);
120 show_file("+", tree2, size2, base);
121 return 0;
124 if (recursive && S_ISDIR(mode1)) {
125 int retval;
126 char *newbase = malloc_base(base, path1, pathlen1);
127 if (show_tree_entry_in_recursive)
128 diff_change(mode1, mode2, sha1, sha2, base, path1);
129 retval = diff_tree_sha1(sha1, sha2, newbase);
130 free(newbase);
131 return retval;
134 diff_change(mode1, mode2, sha1, sha2, base, path1);
135 return 0;
138 static int interesting(void *tree, unsigned long size, const char *base)
140 const char *path;
141 unsigned mode;
142 int i;
143 int baselen, pathlen;
145 if (!nr_paths)
146 return 1;
148 (void)extract(tree, size, &path, &mode);
150 pathlen = strlen(path);
151 baselen = strlen(base);
153 for (i=0; i < nr_paths; i++) {
154 const char *match = paths[i];
155 int matchlen = pathlens[i];
157 if (baselen >= matchlen) {
158 /* If it doesn't match, move along... */
159 if (strncmp(base, match, matchlen))
160 continue;
162 /* The base is a subdirectory of a path which was specified. */
163 return 1;
166 /* Does the base match? */
167 if (strncmp(base, match, baselen))
168 continue;
170 match += baselen;
171 matchlen -= baselen;
173 if (pathlen > matchlen)
174 continue;
176 if (matchlen > pathlen) {
177 if (match[pathlen] != '/')
178 continue;
179 if (!S_ISDIR(mode))
180 continue;
183 if (strncmp(path, match, pathlen))
184 continue;
186 return 1;
188 return 0; /* No matches */
191 /* A whole sub-tree went away or appeared */
192 static void show_tree(const char *prefix, void *tree, unsigned long size, const char *base)
194 while (size) {
195 if (interesting(tree, size, base))
196 show_file(prefix, tree, size, base);
197 update_tree_entry(&tree, &size);
201 static int diff_tree(void *tree1, unsigned long size1, void *tree2, unsigned long size2, const char *base)
203 while (size1 | size2) {
204 if (nr_paths && size1 && !interesting(tree1, size1, base)) {
205 update_tree_entry(&tree1, &size1);
206 continue;
208 if (nr_paths && size2 && !interesting(tree2, size2, base)) {
209 update_tree_entry(&tree2, &size2);
210 continue;
212 if (!size1) {
213 show_file("+", tree2, size2, base);
214 update_tree_entry(&tree2, &size2);
215 continue;
217 if (!size2) {
218 show_file("-", tree1, size1, base);
219 update_tree_entry(&tree1, &size1);
220 continue;
222 switch (compare_tree_entry(tree1, size1, tree2, size2, base)) {
223 case -1:
224 update_tree_entry(&tree1, &size1);
225 continue;
226 case 0:
227 update_tree_entry(&tree1, &size1);
228 /* Fallthrough */
229 case 1:
230 update_tree_entry(&tree2, &size2);
231 continue;
233 die("git-diff-tree: internal error");
235 return 0;
238 static int diff_tree_sha1(const unsigned char *old, const unsigned char *new, const char *base)
240 void *tree1, *tree2;
241 unsigned long size1, size2;
242 int retval;
244 tree1 = read_object_with_reference(old, "tree", &size1, NULL);
245 if (!tree1)
246 die("unable to read source tree (%s)", sha1_to_hex(old));
247 tree2 = read_object_with_reference(new, "tree", &size2, NULL);
248 if (!tree2)
249 die("unable to read destination tree (%s)", sha1_to_hex(new));
250 retval = diff_tree(tree1, size1, tree2, size2, base);
251 free(tree1);
252 free(tree2);
253 return retval;
256 static void call_diff_setup(void)
258 diff_setup(reverse_diff);
261 static int call_diff_flush(void)
263 if (detect_rename)
264 diffcore_rename(detect_rename, diff_score_opt);
265 if (pickaxe)
266 diffcore_pickaxe(pickaxe);
267 if (diff_queue_is_empty()) {
268 diff_flush(DIFF_FORMAT_NO_OUTPUT, 0);
269 return 0;
271 if (nr_paths)
272 diffcore_pathspec(paths);
273 if (header) {
274 if (diff_output_format == DIFF_FORMAT_MACHINE) {
275 const char *ep, *cp;
276 for (cp = header; *cp; cp = ep) {
277 ep = strchr(cp, '\n');
278 if (ep == 0) ep = cp + strlen(cp);
279 printf("%.*s%c", ep-cp, cp, 0);
280 if (*ep) ep++;
283 else {
284 printf("%s", header);
286 header = NULL;
288 diff_flush(diff_output_format, 1);
289 return 1;
292 static int diff_tree_sha1_top(const unsigned char *old,
293 const unsigned char *new, const char *base)
295 int ret;
297 call_diff_setup();
298 ret = diff_tree_sha1(old, new, base);
299 call_diff_flush();
300 return ret;
303 static int diff_root_tree(const unsigned char *new, const char *base)
305 int retval;
306 void *tree;
307 unsigned long size;
309 call_diff_setup();
310 tree = read_object_with_reference(new, "tree", &size, NULL);
311 if (!tree)
312 die("unable to read root tree (%s)", sha1_to_hex(new));
313 retval = diff_tree("", 0, tree, size, base);
314 free(tree);
315 call_diff_flush();
316 return retval;
319 static int get_one_line(const char *msg, unsigned long len)
321 int ret = 0;
323 while (len--) {
324 ret++;
325 if (*msg++ == '\n')
326 break;
328 return ret;
331 static int add_author_info(char *buf, const char *line, int len)
333 char *date;
334 unsigned int namelen;
335 unsigned long time;
336 int tz;
338 line += strlen("author ");
339 date = strchr(line, '>');
340 if (!date)
341 return 0;
342 namelen = ++date - line;
343 time = strtoul(date, &date, 10);
344 tz = strtol(date, NULL, 10);
346 return sprintf(buf, "Author: %.*s\nDate: %s\n",
347 namelen, line,
348 show_date(time, tz));
351 static char *generate_header(const char *commit, const char *parent, const char *msg, unsigned long len)
353 static char this_header[16384];
354 int offset;
356 offset = sprintf(this_header, "%s%s (from %s)\n", header_prefix, commit, parent);
357 if (verbose_header) {
358 int hdr = 1;
360 for (;;) {
361 const char *line = msg;
362 int linelen = get_one_line(msg, len);
364 if (!linelen)
365 break;
368 * We want some slop for indentation and a possible
369 * final "...". Thus the "+ 20".
371 if (offset + linelen + 20 > sizeof(this_header)) {
372 memcpy(this_header + offset, " ...\n", 8);
373 offset += 8;
374 break;
377 msg += linelen;
378 len -= linelen;
379 if (linelen == 1)
380 hdr = 0;
381 if (hdr) {
382 if (!memcmp(line, "author ", 7))
383 offset += add_author_info(this_header + offset, line, linelen);
384 continue;
386 memset(this_header + offset, ' ', 4);
387 memcpy(this_header + offset + 4, line, linelen);
388 offset += linelen + 4;
390 /* Make sure there is an EOLN */
391 if (this_header[offset-1] != '\n')
392 this_header[offset++] = '\n';
393 /* Add _another_ EOLN if we are doing diff output */
394 this_header[offset++] = '\n';
395 this_header[offset] = 0;
398 return this_header;
401 static int diff_tree_commit(const unsigned char *commit, const char *name)
403 unsigned long size, offset;
404 char *buf = read_object_with_reference(commit, "commit", &size, NULL);
406 if (!buf)
407 return -1;
409 if (!name) {
410 static char commit_name[60];
411 strcpy(commit_name, sha1_to_hex(commit));
412 name = commit_name;
415 /* Root commit? */
416 if (show_root_diff && memcmp(buf + 46, "parent ", 7)) {
417 header = generate_header(name, "root", buf, size);
418 diff_root_tree(commit, "");
421 /* More than one parent? */
422 if (ignore_merges) {
423 if (!memcmp(buf + 46 + 48, "parent ", 7))
424 return 0;
427 offset = 46;
428 while (offset + 48 < size && !memcmp(buf + offset, "parent ", 7)) {
429 unsigned char parent[20];
430 if (get_sha1_hex(buf + offset + 7, parent))
431 return -1;
432 header = generate_header(name, sha1_to_hex(parent), buf, size);
433 diff_tree_sha1_top(parent, commit, "");
434 if (!header && verbose_header) {
435 header_prefix = "\ndiff-tree ";
437 * Don't print multiple merge entries if we
438 * don't print the diffs.
441 offset += 48;
443 return 0;
446 static int diff_tree_stdin(char *line)
448 int len = strlen(line);
449 unsigned char commit[20], parent[20];
450 static char this_header[1000];
452 if (!len || line[len-1] != '\n')
453 return -1;
454 line[len-1] = 0;
455 if (get_sha1_hex(line, commit))
456 return -1;
457 if (isspace(line[40]) && !get_sha1_hex(line+41, parent)) {
458 line[40] = 0;
459 line[81] = 0;
460 sprintf(this_header, "%s (from %s)\n", line, line+41);
461 header = this_header;
462 return diff_tree_sha1_top(parent, commit, "");
464 line[40] = 0;
465 return diff_tree_commit(commit, line);
468 static char *diff_tree_usage =
469 "git-diff-tree [-p] [-r] [-z] [--stdin] [-M] [-C] [-R] [-S<string>] [-m] [-s] [-v] [-t] <tree-ish> <tree-ish>";
471 int main(int argc, const char **argv)
473 int nr_sha1;
474 char line[1000];
475 unsigned char sha1[2][20];
477 nr_sha1 = 0;
478 for (;;) {
479 const char *arg;
481 argv++;
482 argc--;
483 arg = *argv;
484 if (!arg)
485 break;
487 if (*arg != '-') {
488 if (nr_sha1 < 2 && !get_sha1(arg, sha1[nr_sha1])) {
489 nr_sha1++;
490 continue;
492 break;
495 if (!strcmp(arg, "--")) {
496 argv++;
497 argc--;
498 break;
500 if (!strcmp(arg, "-r")) {
501 recursive = 1;
502 continue;
504 if (!strcmp(arg, "-t")) {
505 recursive = show_tree_entry_in_recursive = 1;
506 continue;
508 if (!strcmp(arg, "-R")) {
509 reverse_diff = 1;
510 continue;
512 if (!strcmp(arg, "-p")) {
513 diff_output_format = DIFF_FORMAT_PATCH;
514 recursive = 1;
515 continue;
517 if (!strncmp(arg, "-S", 2)) {
518 pickaxe = arg + 2;
519 continue;
521 if (!strncmp(arg, "-M", 2)) {
522 detect_rename = DIFF_DETECT_RENAME;
523 diff_score_opt = diff_scoreopt_parse(arg);
524 continue;
526 if (!strncmp(arg, "-C", 2)) {
527 detect_rename = DIFF_DETECT_COPY;
528 diff_score_opt = diff_scoreopt_parse(arg);
529 continue;
531 if (!strcmp(arg, "-z")) {
532 diff_output_format = DIFF_FORMAT_MACHINE;
533 continue;
535 if (!strcmp(arg, "-m")) {
536 ignore_merges = 0;
537 continue;
539 if (!strcmp(arg, "-s")) {
540 diff_output_format = DIFF_FORMAT_NO_OUTPUT;
541 continue;
543 if (!strcmp(arg, "-v")) {
544 verbose_header = 1;
545 header_prefix = "diff-tree ";
546 continue;
548 if (!strcmp(arg, "--stdin")) {
549 read_stdin = 1;
550 continue;
552 if (!strcmp(arg, "--root")) {
553 show_root_diff = 1;
554 continue;
556 usage(diff_tree_usage);
559 if (argc > 0) {
560 int i;
562 paths = argv;
563 nr_paths = argc;
564 pathlens = xmalloc(nr_paths * sizeof(int));
565 for (i=0; i<nr_paths; i++)
566 pathlens[i] = strlen(paths[i]);
569 switch (nr_sha1) {
570 case 0:
571 if (!read_stdin)
572 usage(diff_tree_usage);
573 break;
574 case 1:
575 diff_tree_commit(sha1[0], NULL);
576 break;
577 case 2:
578 diff_tree_sha1_top(sha1[0], sha1[1], "");
579 break;
582 if (!read_stdin)
583 return 0;
585 while (fgets(line, sizeof(line), stdin))
586 diff_tree_stdin(line);
588 return 0;