git-diff-tree: fix output with just "--pretty".
[git/dscho.git] / diff-tree.c
blob6c98e62ad00962858e235f2a4299a530c2afdaf6
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] [--pretty] [-t] <tree-ish> <tree-ish>";
403 static enum cmit_fmt get_commit_format(const char *arg)
405 if (!*arg)
406 return CMIT_FMT_DEFAULT;
407 if (!strcmp(arg, "=raw"))
408 return CMIT_FMT_RAW;
409 if (!strcmp(arg, "=medium"))
410 return CMIT_FMT_MEDIUM;
411 if (!strcmp(arg, "=short"))
412 return CMIT_FMT_SHORT;
413 usage(diff_tree_usage);
416 int main(int argc, const char **argv)
418 int nr_sha1;
419 char line[1000];
420 unsigned char sha1[2][20];
422 nr_sha1 = 0;
423 for (;;) {
424 const char *arg;
426 argv++;
427 argc--;
428 arg = *argv;
429 if (!arg)
430 break;
432 if (*arg != '-') {
433 if (nr_sha1 < 2 && !get_sha1(arg, sha1[nr_sha1])) {
434 nr_sha1++;
435 continue;
437 break;
440 if (!strcmp(arg, "--")) {
441 argv++;
442 argc--;
443 break;
445 if (!strcmp(arg, "-r")) {
446 recursive = 1;
447 continue;
449 if (!strcmp(arg, "-t")) {
450 recursive = show_tree_entry_in_recursive = 1;
451 continue;
453 if (!strcmp(arg, "-R")) {
454 diff_setup_opt |= DIFF_SETUP_REVERSE;
455 continue;
457 if (!strcmp(arg, "-p")) {
458 diff_output_format = DIFF_FORMAT_PATCH;
459 recursive = 1;
460 continue;
462 if (!strncmp(arg, "-S", 2)) {
463 pickaxe = arg + 2;
464 continue;
466 if (!strncmp(arg, "-O", 2)) {
467 orderfile = arg + 2;
468 continue;
470 if (!strcmp(arg, "--pickaxe-all")) {
471 pickaxe_opts = DIFF_PICKAXE_ALL;
472 continue;
474 if (!strncmp(arg, "-M", 2)) {
475 detect_rename = DIFF_DETECT_RENAME;
476 if ((diff_score_opt = diff_scoreopt_parse(arg)) == -1)
477 usage(diff_tree_usage);
478 continue;
480 if (!strncmp(arg, "-C", 2)) {
481 detect_rename = DIFF_DETECT_COPY;
482 if ((diff_score_opt = diff_scoreopt_parse(arg)) == -1)
483 usage(diff_tree_usage);
484 continue;
486 if (!strncmp(arg, "-B", 2)) {
487 if ((diff_break_opt = diff_scoreopt_parse(arg)) == -1)
488 usage(diff_tree_usage);
489 continue;
491 if (!strcmp(arg, "-z")) {
492 diff_output_format = DIFF_FORMAT_MACHINE;
493 continue;
495 if (!strcmp(arg, "-m")) {
496 ignore_merges = 0;
497 continue;
499 if (!strcmp(arg, "-s")) {
500 diff_output_format = DIFF_FORMAT_NO_OUTPUT;
501 continue;
503 if (!strcmp(arg, "-v")) {
504 verbose_header = 1;
505 header_prefix = "diff-tree ";
506 continue;
508 if (!strncmp(arg, "--pretty", 8)) {
509 verbose_header = 1;
510 header_prefix = "diff-tree ";
511 commit_format = get_commit_format(arg+8);
512 continue;
514 if (!strcmp(arg, "--stdin")) {
515 read_stdin = 1;
516 continue;
518 if (!strcmp(arg, "--root")) {
519 show_root_diff = 1;
520 continue;
522 usage(diff_tree_usage);
525 if (argc > 0) {
526 int i;
528 paths = argv;
529 nr_paths = argc;
530 pathlens = xmalloc(nr_paths * sizeof(int));
531 for (i=0; i<nr_paths; i++)
532 pathlens[i] = strlen(paths[i]);
535 switch (nr_sha1) {
536 case 0:
537 if (!read_stdin)
538 usage(diff_tree_usage);
539 break;
540 case 1:
541 diff_tree_commit(sha1[0], NULL);
542 break;
543 case 2:
544 diff_tree_sha1_top(sha1[0], sha1[1], "");
545 break;
548 if (!read_stdin)
549 return 0;
551 if (detect_rename)
552 diff_setup_opt |= (DIFF_SETUP_USE_SIZE_CACHE |
553 DIFF_SETUP_USE_CACHE);
554 while (fgets(line, sizeof(line), stdin))
555 diff_tree_stdin(line);
557 return 0;