diff-tree: add "--root" flag to show a root commit as a big creation event.
[git/kirr.git] / diff-tree.c
blob9d94e0cc58be314017d570f75fc3169197f46a9e
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 line_termination = '\n';
12 static int generate_patch = 0;
13 static int detect_rename = 0;
14 static const char *header = NULL;
15 static const char *header_prefix = "";
17 // What paths are we interested in?
18 static int nr_paths = 0;
19 static char **paths = NULL;
20 static int *pathlens = NULL;
22 static int diff_tree_sha1(const unsigned char *old, const unsigned char *new, const char *base);
24 static void update_tree_entry(void **bufp, unsigned long *sizep)
26 void *buf = *bufp;
27 unsigned long size = *sizep;
28 int len = strlen(buf) + 1 + 20;
30 if (size < len)
31 die("corrupt tree file");
32 *bufp = buf + len;
33 *sizep = size - len;
36 static const unsigned char *extract(void *tree, unsigned long size, const char **pathp, unsigned int *modep)
38 int len = strlen(tree)+1;
39 const unsigned char *sha1 = tree + len;
40 const char *path = strchr(tree, ' ');
42 if (!path || size < len + 20 || sscanf(tree, "%o", modep) != 1)
43 die("corrupt tree file");
44 *pathp = path+1;
45 return sha1;
48 static char *malloc_base(const char *base, const char *path, int pathlen)
50 int baselen = strlen(base);
51 char *newbase = xmalloc(baselen + pathlen + 2);
52 memcpy(newbase, base, baselen);
53 memcpy(newbase + baselen, path, pathlen);
54 memcpy(newbase + baselen + pathlen, "/", 2);
55 return newbase;
58 static void show_file(const char *prefix, void *tree, unsigned long size, const char *base);
59 static void show_tree(const char *prefix, void *tree, unsigned long size, const char *base);
61 /* A file entry went away or appeared */
62 static void show_file(const char *prefix, void *tree, unsigned long size, const char *base)
64 unsigned mode;
65 const char *path;
66 const unsigned char *sha1 = extract(tree, size, &path, &mode);
68 if (header) {
69 printf("%s", header);
70 header = NULL;
73 if (silent)
74 return;
76 if (recursive && S_ISDIR(mode)) {
77 char type[20];
78 unsigned long size;
79 char *newbase = malloc_base(base, path, strlen(path));
80 void *tree;
82 tree = read_sha1_file(sha1, type, &size);
83 if (!tree || strcmp(type, "tree"))
84 die("corrupt tree sha %s", sha1_to_hex(sha1));
86 show_tree(prefix, tree, size, newbase);
88 free(tree);
89 free(newbase);
90 return;
93 if (generate_patch) {
94 if (!S_ISDIR(mode))
95 diff_addremove(prefix[0], mode, sha1, base, path);
97 else
98 printf("%s%06o\t%s\t%s\t%s%s%c", prefix, mode,
99 S_ISDIR(mode) ? "tree" : "blob",
100 sha1_to_hex(sha1), base, path,
101 line_termination);
104 static int compare_tree_entry(void *tree1, unsigned long size1, void *tree2, unsigned long size2, const char *base)
106 unsigned mode1, mode2;
107 const char *path1, *path2;
108 const unsigned char *sha1, *sha2;
109 int cmp, pathlen1, pathlen2;
110 char old_sha1_hex[50];
112 sha1 = extract(tree1, size1, &path1, &mode1);
113 sha2 = extract(tree2, size2, &path2, &mode2);
115 pathlen1 = strlen(path1);
116 pathlen2 = strlen(path2);
117 cmp = cache_name_compare(path1, pathlen1, path2, pathlen2);
118 if (cmp < 0) {
119 show_file("-", tree1, size1, base);
120 return -1;
122 if (cmp > 0) {
123 show_file("+", tree2, size2, base);
124 return 1;
126 if (!memcmp(sha1, sha2, 20) && mode1 == mode2)
127 return 0;
130 * If the filemode has changed to/from a directory from/to a regular
131 * file, we need to consider it a remove and an add.
133 if (S_ISDIR(mode1) != S_ISDIR(mode2)) {
134 show_file("-", tree1, size1, base);
135 show_file("+", tree2, size2, base);
136 return 0;
139 if (recursive && S_ISDIR(mode1)) {
140 int retval;
141 char *newbase = malloc_base(base, path1, pathlen1);
142 retval = diff_tree_sha1(sha1, sha2, newbase);
143 free(newbase);
144 return retval;
147 if (header) {
148 printf("%s", header);
149 header = NULL;
151 if (silent)
152 return 0;
154 if (generate_patch) {
155 if (!S_ISDIR(mode1))
156 diff_change(mode1, mode2, sha1, sha2, base, path1);
158 else {
159 strcpy(old_sha1_hex, sha1_to_hex(sha1));
160 printf("*%06o->%06o\t%s\t%s->%s\t%s%s%c", mode1, mode2,
161 S_ISDIR(mode1) ? "tree" : "blob",
162 old_sha1_hex, sha1_to_hex(sha2), base, path1,
163 line_termination);
165 return 0;
168 static int interesting(void *tree, unsigned long size, const char *base)
170 const char *path;
171 unsigned mode;
172 int i;
173 int baselen, pathlen;
175 if (!nr_paths)
176 return 1;
178 (void)extract(tree, size, &path, &mode);
180 pathlen = strlen(path);
181 baselen = strlen(base);
183 for (i=0; i < nr_paths; i++) {
184 const char *match = paths[i];
185 int matchlen = pathlens[i];
187 if (baselen >= matchlen) {
188 /* If it doesn't match, move along... */
189 if (strncmp(base, match, matchlen))
190 continue;
192 /* The base is a subdirectory of a path which was specified. */
193 return 1;
196 /* Does the base match? */
197 if (strncmp(base, match, baselen))
198 continue;
200 match += baselen;
201 matchlen -= baselen;
203 if (pathlen > matchlen)
204 continue;
206 if (matchlen > pathlen) {
207 if (match[pathlen] != '/')
208 continue;
209 if (!S_ISDIR(mode))
210 continue;
213 if (strncmp(path, match, pathlen))
214 continue;
216 return 1;
218 return 0; /* No matches */
221 /* A whole sub-tree went away or appeared */
222 static void show_tree(const char *prefix, void *tree, unsigned long size, const char *base)
224 while (size) {
225 if (interesting(tree, size, base))
226 show_file(prefix, tree, size, base);
227 update_tree_entry(&tree, &size);
231 static int diff_tree(void *tree1, unsigned long size1, void *tree2, unsigned long size2, const char *base)
233 while (size1 | size2) {
234 if (nr_paths && size1 && !interesting(tree1, size1, base)) {
235 update_tree_entry(&tree1, &size1);
236 continue;
238 if (nr_paths && size2 && !interesting(tree2, size2, base)) {
239 update_tree_entry(&tree2, &size2);
240 continue;
242 if (!size1) {
243 show_file("+", tree2, size2, base);
244 update_tree_entry(&tree2, &size2);
245 continue;
247 if (!size2) {
248 show_file("-", tree1, size1, base);
249 update_tree_entry(&tree1, &size1);
250 continue;
252 switch (compare_tree_entry(tree1, size1, tree2, size2, base)) {
253 case -1:
254 update_tree_entry(&tree1, &size1);
255 continue;
256 case 0:
257 update_tree_entry(&tree1, &size1);
258 /* Fallthrough */
259 case 1:
260 update_tree_entry(&tree2, &size2);
261 continue;
263 die("git-diff-tree: internal error");
265 return 0;
268 static int diff_tree_sha1(const unsigned char *old, const unsigned char *new, const char *base)
270 void *tree1, *tree2;
271 unsigned long size1, size2;
272 int retval;
274 tree1 = read_object_with_reference(old, "tree", &size1, 0);
275 if (!tree1)
276 die("unable to read source tree (%s)", sha1_to_hex(old));
277 tree2 = read_object_with_reference(new, "tree", &size2, 0);
278 if (!tree2)
279 die("unable to read destination tree (%s)", sha1_to_hex(new));
280 retval = diff_tree(tree1, size1, tree2, size2, base);
281 free(tree1);
282 free(tree2);
283 return retval;
286 static int diff_tree_sha1_top(const unsigned char *old,
287 const unsigned char *new, const char *base)
289 int ret;
290 if (generate_patch)
291 diff_setup(detect_rename, 0, 0, 0, 0);
292 ret = diff_tree_sha1(old, new, base);
293 if (generate_patch)
294 diff_flush();
295 return ret;
298 static int diff_root_tree(const unsigned char *new, const char *base)
300 int retval;
301 void *tree;
302 unsigned long size;
304 if (generate_patch)
305 diff_setup(detect_rename, 0, 0, 0, 0);
306 tree = read_object_with_reference(new, "tree", &size, 0);
307 if (!tree)
308 die("unable to read root tree (%s)", sha1_to_hex(new));
309 retval = diff_tree("", 0, tree, size, base);
310 free(tree);
311 if (generate_patch)
312 diff_flush();
313 return retval;
316 static int get_one_line(const char *msg, unsigned long len)
318 int ret = 0;
320 while (len--) {
321 ret++;
322 if (*msg++ == '\n')
323 break;
325 return ret;
328 static int add_author_info(char *buf, const char *line, int len)
330 char *date;
331 unsigned int namelen;
332 unsigned long time;
333 int tz;
335 line += strlen("author ");
336 date = strchr(line, '>');
337 if (!date)
338 return 0;
339 namelen = ++date - line;
340 time = strtoul(date, &date, 10);
341 tz = strtol(date, NULL, 10);
343 return sprintf(buf, "Author: %.*s\nDate: %s\n",
344 namelen, line,
345 show_date(time, tz));
348 static char *generate_header(const char *commit, const char *parent, const char *msg, unsigned long len)
350 static char this_header[1000];
351 int offset;
353 offset = sprintf(this_header, "%s%s (from %s)\n", header_prefix, commit, parent);
354 if (verbose_header) {
355 int hdr = 1;
357 for (;;) {
358 const char *line = msg;
359 int linelen = get_one_line(msg, len);
361 if (!linelen)
362 break;
363 if (offset + linelen + 10 > sizeof(this_header))
364 break;
366 msg += linelen;
367 len -= linelen;
368 if (linelen == 1)
369 hdr = 0;
370 if (hdr) {
371 if (!memcmp(line, "author ", 7))
372 offset += add_author_info(this_header + offset, line, linelen);
373 continue;
375 memset(this_header + offset, ' ', 4);
376 memcpy(this_header + offset + 4, line, linelen);
377 offset += linelen + 4;
379 this_header[offset++] = '\n';
380 this_header[offset] = 0;
383 return this_header;
386 static int diff_tree_commit(const unsigned char *commit, const char *name)
388 unsigned long size, offset;
389 char *buf = read_object_with_reference(commit, "commit", &size, NULL);
391 if (!buf)
392 return -1;
394 if (!name) {
395 static char commit_name[60];
396 strcpy(commit_name, sha1_to_hex(commit));
397 name = commit_name;
400 /* Root commit? */
401 if (show_root_diff && memcmp(buf + 46, "parent ", 7)) {
402 header = generate_header(name, "root", buf, size);
403 diff_root_tree(commit, "");
406 /* More than one parent? */
407 if (ignore_merges) {
408 if (!memcmp(buf + 46 + 48, "parent ", 7))
409 return 0;
412 offset = 46;
413 while (offset + 48 < size && !memcmp(buf + offset, "parent ", 7)) {
414 unsigned char parent[20];
415 if (get_sha1_hex(buf + offset + 7, parent))
416 return -1;
417 header = generate_header(name, sha1_to_hex(parent), buf, size);
418 diff_tree_sha1_top(parent, commit, "");
419 if (!header && verbose_header)
420 header_prefix = "\ndiff-tree ";
421 offset += 48;
423 return 0;
426 static int diff_tree_stdin(char *line)
428 int len = strlen(line);
429 unsigned char commit[20], parent[20];
430 static char this_header[1000];
432 if (!len || line[len-1] != '\n')
433 return -1;
434 line[len-1] = 0;
435 if (get_sha1_hex(line, commit))
436 return -1;
437 if (isspace(line[40]) && !get_sha1_hex(line+41, parent)) {
438 line[40] = 0;
439 line[81] = 0;
440 sprintf(this_header, "%s (from %s)\n", line, line+41);
441 header = this_header;
442 return diff_tree_sha1_top(parent, commit, "");
444 line[40] = 0;
445 return diff_tree_commit(commit, line);
448 static char *diff_tree_usage =
449 "git-diff-tree [-p] [-r] [-z] [--stdin] [-M] [-m] [-s] [-v] <tree-ish> <tree-ish>";
451 int main(int argc, char **argv)
453 int nr_sha1;
454 char line[1000];
455 unsigned char sha1[2][20];
457 nr_sha1 = 0;
458 for (;;) {
459 char *arg;
461 argv++;
462 argc--;
463 arg = *argv;
464 if (!arg)
465 break;
467 if (*arg != '-') {
468 if (nr_sha1 < 2 && !get_sha1(arg, sha1[nr_sha1])) {
469 nr_sha1++;
470 continue;
472 break;
475 if (!strcmp(arg, "--")) {
476 argv++;
477 argc--;
478 break;
480 if (!strcmp(arg, "-r")) {
481 recursive = 1;
482 continue;
484 if (!strcmp(arg, "-p")) {
485 recursive = generate_patch = 1;
486 continue;
488 if (!strcmp(arg, "-M")) {
489 detect_rename = recursive = generate_patch = 1;
490 continue;
492 if (!strcmp(arg, "-z")) {
493 line_termination = '\0';
494 continue;
496 if (!strcmp(arg, "-m")) {
497 ignore_merges = 0;
498 continue;
500 if (!strcmp(arg, "-s")) {
501 silent = 1;
502 continue;
504 if (!strcmp(arg, "-v")) {
505 verbose_header = 1;
506 header_prefix = "diff-tree ";
507 continue;
509 if (!strcmp(arg, "--stdin")) {
510 read_stdin = 1;
511 continue;
513 if (!strcmp(arg, "--root")) {
514 show_root_diff = 1;
515 continue;
517 usage(diff_tree_usage);
520 if (argc > 0) {
521 int i;
523 paths = argv;
524 nr_paths = argc;
525 pathlens = xmalloc(nr_paths * sizeof(int));
526 for (i=0; i<nr_paths; i++)
527 pathlens[i] = strlen(paths[i]);
530 switch (nr_sha1) {
531 case 0:
532 if (!read_stdin)
533 usage(diff_tree_usage);
534 break;
535 case 1:
536 diff_tree_commit(sha1[0], NULL);
537 break;
538 case 2:
539 diff_tree_sha1_top(sha1[0], sha1[1], "");
540 break;
543 if (!read_stdin)
544 return 0;
546 while (fgets(line, sizeof(line), stdin))
547 diff_tree_stdin(line);
549 return 0;