2 * Copyright (C) 2005 Junio C Hamano
12 #include "xdiff-interface.h"
14 static int use_size_cache;
16 static int diff_detect_rename_default = 0;
17 static int diff_rename_limit_default = -1;
18 static int diff_use_color_default = 0;
29 #define COLOR_NORMAL ""
30 #define COLOR_BOLD "\033[1m"
31 #define COLOR_DIM "\033[2m"
32 #define COLOR_UL "\033[4m"
33 #define COLOR_BLINK "\033[5m"
34 #define COLOR_REVERSE "\033[7m"
35 #define COLOR_RESET "\033[m"
37 #define COLOR_BLACK "\033[30m"
38 #define COLOR_RED "\033[31m"
39 #define COLOR_GREEN "\033[32m"
40 #define COLOR_YELLOW "\033[33m"
41 #define COLOR_BLUE "\033[34m"
42 #define COLOR_MAGENTA "\033[35m"
43 #define COLOR_CYAN "\033[36m"
44 #define COLOR_WHITE "\033[37m"
46 static const char *diff_colors[] = {
47 [DIFF_RESET] = COLOR_RESET,
48 [DIFF_PLAIN] = COLOR_NORMAL,
49 [DIFF_METAINFO] = COLOR_BOLD,
50 [DIFF_FRAGINFO] = COLOR_CYAN,
51 [DIFF_FILE_OLD] = COLOR_RED,
52 [DIFF_FILE_NEW] = COLOR_GREEN,
55 static int parse_diff_color_slot(const char *var, int ofs)
57 if (!strcasecmp(var+ofs, "plain"))
59 if (!strcasecmp(var+ofs, "meta"))
61 if (!strcasecmp(var+ofs, "frag"))
63 if (!strcasecmp(var+ofs, "old"))
65 if (!strcasecmp(var+ofs, "new"))
67 die("bad config variable '%s'", var);
70 static const char *parse_diff_color_value(const char *value, const char *var)
72 if (!strcasecmp(value, "normal"))
74 if (!strcasecmp(value, "bold"))
76 if (!strcasecmp(value, "dim"))
78 if (!strcasecmp(value, "ul"))
80 if (!strcasecmp(value, "blink"))
82 if (!strcasecmp(value, "reverse"))
84 if (!strcasecmp(value, "reset"))
86 if (!strcasecmp(value, "black"))
88 if (!strcasecmp(value, "red"))
90 if (!strcasecmp(value, "green"))
92 if (!strcasecmp(value, "yellow"))
94 if (!strcasecmp(value, "blue"))
96 if (!strcasecmp(value, "magenta"))
98 if (!strcasecmp(value, "cyan"))
100 if (!strcasecmp(value, "white"))
102 die("bad config value '%s' for variable '%s'", value, var);
105 int git_diff_config(const char *var, const char *value)
107 if (!strcmp(var, "diff.renamelimit")) {
108 diff_rename_limit_default = git_config_int(var, value);
111 if (!strcmp(var, "diff.color")) {
113 diff_use_color_default = 1; /* bool */
114 else if (!strcasecmp(value, "auto"))
115 diff_use_color_default = isatty(1);
116 else if (!strcasecmp(value, "never"))
117 diff_use_color_default = 0;
118 else if (!strcasecmp(value, "always"))
119 diff_use_color_default = 1;
121 diff_use_color_default = git_config_bool(var, value);
124 if (!strcmp(var, "diff.renames")) {
126 diff_detect_rename_default = DIFF_DETECT_RENAME;
127 else if (!strcasecmp(value, "copies") ||
128 !strcasecmp(value, "copy"))
129 diff_detect_rename_default = DIFF_DETECT_COPY;
130 else if (git_config_bool(var,value))
131 diff_detect_rename_default = DIFF_DETECT_RENAME;
134 if (!strncmp(var, "diff.color.", 11)) {
135 int slot = parse_diff_color_slot(var, 11);
136 diff_colors[slot] = parse_diff_color_value(value, var);
139 return git_default_config(var, value);
142 static char *quote_one(const char *str)
149 needlen = quote_c_style(str, NULL, NULL, 0);
152 xp = xmalloc(needlen + 1);
153 quote_c_style(str, xp, NULL, 0);
157 static char *quote_two(const char *one, const char *two)
159 int need_one = quote_c_style(one, NULL, NULL, 1);
160 int need_two = quote_c_style(two, NULL, NULL, 1);
163 if (need_one + need_two) {
164 if (!need_one) need_one = strlen(one);
165 if (!need_two) need_one = strlen(two);
167 xp = xmalloc(need_one + need_two + 3);
169 quote_c_style(one, xp + 1, NULL, 1);
170 quote_c_style(two, xp + need_one + 1, NULL, 1);
171 strcpy(xp + need_one + need_two + 1, "\"");
174 need_one = strlen(one);
175 need_two = strlen(two);
176 xp = xmalloc(need_one + need_two + 1);
178 strcpy(xp + need_one, two);
182 static const char *external_diff(void)
184 static const char *external_diff_cmd = NULL;
185 static int done_preparing = 0;
188 return external_diff_cmd;
189 external_diff_cmd = getenv("GIT_EXTERNAL_DIFF");
191 return external_diff_cmd;
194 #define TEMPFILE_PATH_LEN 50
196 static struct diff_tempfile {
197 const char *name; /* filename external diff should read from */
200 char tmp_path[TEMPFILE_PATH_LEN];
203 static int count_lines(const char *data, int size)
205 int count, ch, completely_empty = 1, nl_just_seen = 0;
212 completely_empty = 0;
216 completely_empty = 0;
219 if (completely_empty)
222 count++; /* no trailing newline */
226 static void print_line_count(int count)
236 printf("1,%d", count);
241 static void copy_file(int prefix, const char *data, int size)
243 int ch, nl_just_seen = 1;
255 printf("\n\\ No newline at end of file\n");
258 static void emit_rewrite_diff(const char *name_a,
260 struct diff_filespec *one,
261 struct diff_filespec *two)
264 diff_populate_filespec(one, 0);
265 diff_populate_filespec(two, 0);
266 lc_a = count_lines(one->data, one->size);
267 lc_b = count_lines(two->data, two->size);
268 printf("--- %s\n+++ %s\n@@ -", name_a, name_b);
269 print_line_count(lc_a);
271 print_line_count(lc_b);
274 copy_file('-', one->data, one->size);
276 copy_file('+', two->data, two->size);
279 static int fill_mmfile(mmfile_t *mf, struct diff_filespec *one)
281 if (!DIFF_FILE_VALID(one)) {
282 mf->ptr = (char *)""; /* does not matter */
286 else if (diff_populate_filespec(one, 0))
289 mf->size = one->size;
293 struct emit_callback {
294 struct xdiff_emit_state xm;
295 int nparents, color_diff;
296 const char **label_path;
299 static inline const char *get_color(int diff_use_color, enum color_diff ix)
302 return diff_colors[ix];
306 static void fn_out_consume(void *priv, char *line, unsigned long len)
309 struct emit_callback *ecbdata = priv;
310 const char *set = get_color(ecbdata->color_diff, DIFF_METAINFO);
311 const char *reset = get_color(ecbdata->color_diff, DIFF_RESET);
313 if (ecbdata->label_path[0]) {
314 printf("%s--- %s%s\n", set, ecbdata->label_path[0], reset);
315 printf("%s+++ %s%s\n", set, ecbdata->label_path[1], reset);
316 ecbdata->label_path[0] = ecbdata->label_path[1] = NULL;
319 /* This is not really necessary for now because
320 * this codepath only deals with two-way diffs.
322 for (i = 0; i < len && line[i] == '@'; i++)
324 if (2 <= i && i < len && line[i] == ' ') {
325 ecbdata->nparents = i - 1;
326 set = get_color(ecbdata->color_diff, DIFF_FRAGINFO);
328 else if (len < ecbdata->nparents)
331 int nparents = ecbdata->nparents;
332 int color = DIFF_PLAIN;
333 for (i = 0; i < nparents && len; i++) {
335 color = DIFF_FILE_OLD;
336 else if (line[i] == '+')
337 color = DIFF_FILE_NEW;
339 set = get_color(ecbdata->color_diff, color);
341 if (len > 0 && line[len-1] == '\n')
344 fwrite (line, len, 1, stdout);
348 static char *pprint_rename(const char *a, const char *b)
353 int pfx_length, sfx_length;
354 int len_a = strlen(a);
355 int len_b = strlen(b);
357 /* Find common prefix */
359 while (*old && *new && *old == *new) {
361 pfx_length = old - a + 1;
366 /* Find common suffix */
370 while (a <= old && b <= new && *old == *new) {
372 sfx_length = len_a - (old - a);
378 * pfx{mid-a => mid-b}sfx
379 * {pfx-a => pfx-b}sfx
380 * pfx{sfx-a => sfx-b}
383 if (pfx_length + sfx_length) {
384 int a_midlen = len_a - pfx_length - sfx_length;
385 int b_midlen = len_b - pfx_length - sfx_length;
386 if (a_midlen < 0) a_midlen = 0;
387 if (b_midlen < 0) b_midlen = 0;
389 name = xmalloc(pfx_length + a_midlen + b_midlen + sfx_length + 7);
390 sprintf(name, "%.*s{%.*s => %.*s}%s",
392 a_midlen, a + pfx_length,
393 b_midlen, b + pfx_length,
394 a + len_a - sfx_length);
397 name = xmalloc(len_a + len_b + 5);
398 sprintf(name, "%s => %s", a, b);
404 struct xdiff_emit_state xm;
408 struct diffstat_file {
410 unsigned is_unmerged:1;
411 unsigned is_binary:1;
412 unsigned is_renamed:1;
413 unsigned int added, deleted;
417 static struct diffstat_file *diffstat_add(struct diffstat_t *diffstat,
421 struct diffstat_file *x;
422 x = xcalloc(sizeof (*x), 1);
423 if (diffstat->nr == diffstat->alloc) {
424 diffstat->alloc = alloc_nr(diffstat->alloc);
425 diffstat->files = xrealloc(diffstat->files,
426 diffstat->alloc * sizeof(x));
428 diffstat->files[diffstat->nr++] = x;
430 x->name = pprint_rename(name_a, name_b);
434 x->name = strdup(name_a);
438 static void diffstat_consume(void *priv, char *line, unsigned long len)
440 struct diffstat_t *diffstat = priv;
441 struct diffstat_file *x = diffstat->files[diffstat->nr - 1];
445 else if (line[0] == '-')
449 static const char pluses[] = "++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++";
450 static const char minuses[]= "----------------------------------------------------------------------";
451 const char mime_boundary_leader[] = "------------";
453 static void show_stats(struct diffstat_t* data)
455 int i, len, add, del, total, adds = 0, dels = 0;
456 int max, max_change = 0, max_len = 0;
457 int total_files = data->nr;
462 for (i = 0; i < data->nr; i++) {
463 struct diffstat_file *file = data->files[i];
465 len = strlen(file->name);
469 if (file->is_binary || file->is_unmerged)
471 if (max_change < file->added + file->deleted)
472 max_change = file->added + file->deleted;
475 for (i = 0; i < data->nr; i++) {
476 const char *prefix = "";
477 char *name = data->files[i]->name;
478 int added = data->files[i]->added;
479 int deleted = data->files[i]->deleted;
481 if (0 < (len = quote_c_style(name, NULL, NULL, 0))) {
482 char *qname = xmalloc(len + 1);
483 quote_c_style(name, qname, NULL, 0);
485 data->files[i]->name = name = qname;
489 * "scale" the filename
500 slash = strchr(name, '/');
507 * scale the add/delete
513 if (data->files[i]->is_binary) {
514 printf(" %s%-*s | Bin\n", prefix, len, name);
515 goto free_diffstat_file;
517 else if (data->files[i]->is_unmerged) {
518 printf(" %s%-*s | Unmerged\n", prefix, len, name);
519 goto free_diffstat_file;
521 else if (!data->files[i]->is_renamed &&
522 (added + deleted == 0)) {
524 goto free_diffstat_file;
533 if (max_change > 0) {
534 total = (total * max + max_change / 2) / max_change;
535 add = (add * max + max_change / 2) / max_change;
538 printf(" %s%-*s |%5d %.*s%.*s\n", prefix,
539 len, name, added + deleted,
540 add, pluses, del, minuses);
542 free(data->files[i]->name);
543 free(data->files[i]);
546 printf(" %d files changed, %d insertions(+), %d deletions(-)\n",
547 total_files, adds, dels);
551 struct xdiff_emit_state xm;
552 const char *filename;
556 static void checkdiff_consume(void *priv, char *line, unsigned long len)
558 struct checkdiff_t *data = priv;
560 if (line[0] == '+') {
565 /* check space before tab */
566 for (i = 1; i < len && (line[i] == ' ' || line[i] == '\t'); i++)
569 if (line[i - 1] == '\t' && spaces)
570 printf("%s:%d: space before tab:%.*s\n",
571 data->filename, data->lineno, (int)len, line);
573 /* check white space at line end */
574 if (line[len - 1] == '\n')
576 if (isspace(line[len - 1]))
577 printf("%s:%d: white space at end: %.*s\n",
578 data->filename, data->lineno, (int)len, line);
579 } else if (line[0] == ' ')
581 else if (line[0] == '@') {
582 char *plus = strchr(line, '+');
584 data->lineno = strtol(plus, NULL, 10);
590 static unsigned char *deflate_it(char *data,
592 unsigned long *result_size)
595 unsigned char *deflated;
598 memset(&stream, 0, sizeof(stream));
599 deflateInit(&stream, zlib_compression_level);
600 bound = deflateBound(&stream, size);
601 deflated = xmalloc(bound);
602 stream.next_out = deflated;
603 stream.avail_out = bound;
605 stream.next_in = (unsigned char *)data;
606 stream.avail_in = size;
607 while (deflate(&stream, Z_FINISH) == Z_OK)
610 *result_size = stream.total_out;
614 static void emit_binary_diff(mmfile_t *one, mmfile_t *two)
620 unsigned long orig_size;
621 unsigned long delta_size;
622 unsigned long deflate_size;
623 unsigned long data_size;
625 printf("GIT binary patch\n");
626 /* We could do deflated delta, or we could do just deflated two,
627 * whichever is smaller.
630 deflated = deflate_it(two->ptr, two->size, &deflate_size);
631 if (one->size && two->size) {
632 delta = diff_delta(one->ptr, one->size,
634 &delta_size, deflate_size);
636 void *to_free = delta;
637 orig_size = delta_size;
638 delta = deflate_it(delta, delta_size, &delta_size);
643 if (delta && delta_size < deflate_size) {
644 printf("delta %lu\n", orig_size);
647 data_size = delta_size;
650 printf("literal %lu\n", two->size);
653 data_size = deflate_size;
656 /* emit data encoded in base85 */
659 int bytes = (52 < data_size) ? 52 : data_size;
663 line[0] = bytes + 'A' - 1;
665 line[0] = bytes - 26 + 'a' - 1;
666 encode_85(line + 1, cp, bytes);
667 cp = (char *) cp + bytes;
674 #define FIRST_FEW_BYTES 8000
675 static int mmfile_is_binary(mmfile_t *mf)
678 if (FIRST_FEW_BYTES < sz)
679 sz = FIRST_FEW_BYTES;
680 if (memchr(mf->ptr, 0, sz))
685 static void builtin_diff(const char *name_a,
687 struct diff_filespec *one,
688 struct diff_filespec *two,
689 const char *xfrm_msg,
690 struct diff_options *o,
691 int complete_rewrite)
696 const char *set = get_color(o->color_diff, DIFF_METAINFO);
697 const char *reset = get_color(o->color_diff, DIFF_RESET);
699 a_one = quote_two("a/", name_a);
700 b_two = quote_two("b/", name_b);
701 lbl[0] = DIFF_FILE_VALID(one) ? a_one : "/dev/null";
702 lbl[1] = DIFF_FILE_VALID(two) ? b_two : "/dev/null";
703 printf("%sdiff --git %s %s%s\n", set, a_one, b_two, reset);
704 if (lbl[0][0] == '/') {
706 printf("%snew file mode %06o%s\n", set, two->mode, reset);
707 if (xfrm_msg && xfrm_msg[0])
708 printf("%s%s%s\n", set, xfrm_msg, reset);
710 else if (lbl[1][0] == '/') {
711 printf("%sdeleted file mode %06o%s\n", set, one->mode, reset);
712 if (xfrm_msg && xfrm_msg[0])
713 printf("%s%s%s\n", set, xfrm_msg, reset);
716 if (one->mode != two->mode) {
717 printf("%sold mode %06o%s\n", set, one->mode, reset);
718 printf("%snew mode %06o%s\n", set, two->mode, reset);
720 if (xfrm_msg && xfrm_msg[0])
721 printf("%s%s%s\n", set, xfrm_msg, reset);
723 * we do not run diff between different kind
726 if ((one->mode ^ two->mode) & S_IFMT)
727 goto free_ab_and_return;
728 if (complete_rewrite) {
729 emit_rewrite_diff(name_a, name_b, one, two);
730 goto free_ab_and_return;
734 if (fill_mmfile(&mf1, one) < 0 || fill_mmfile(&mf2, two) < 0)
735 die("unable to read files to diff");
737 if (!o->text && (mmfile_is_binary(&mf1) || mmfile_is_binary(&mf2))) {
738 /* Quite common confusing case */
739 if (mf1.size == mf2.size &&
740 !memcmp(mf1.ptr, mf2.ptr, mf1.size))
741 goto free_ab_and_return;
743 emit_binary_diff(&mf1, &mf2);
745 printf("Binary files %s and %s differ\n",
749 /* Crazy xdl interfaces.. */
750 const char *diffopts = getenv("GIT_DIFF_OPTS");
754 struct emit_callback ecbdata;
756 memset(&ecbdata, 0, sizeof(ecbdata));
757 ecbdata.label_path = lbl;
758 ecbdata.color_diff = o->color_diff;
759 xpp.flags = XDF_NEED_MINIMAL | o->xdl_opts;
760 xecfg.ctxlen = o->context;
761 xecfg.flags = XDL_EMIT_FUNCNAMES;
764 else if (!strncmp(diffopts, "--unified=", 10))
765 xecfg.ctxlen = strtoul(diffopts + 10, NULL, 10);
766 else if (!strncmp(diffopts, "-u", 2))
767 xecfg.ctxlen = strtoul(diffopts + 2, NULL, 10);
768 ecb.outf = xdiff_outf;
770 ecbdata.xm.consume = fn_out_consume;
771 xdl_diff(&mf1, &mf2, &xpp, &xecfg, &ecb);
780 static void builtin_diffstat(const char *name_a, const char *name_b,
781 struct diff_filespec *one,
782 struct diff_filespec *two,
783 struct diffstat_t *diffstat,
784 struct diff_options *o,
785 int complete_rewrite)
788 struct diffstat_file *data;
790 data = diffstat_add(diffstat, name_a, name_b);
793 data->is_unmerged = 1;
796 if (complete_rewrite) {
797 diff_populate_filespec(one, 0);
798 diff_populate_filespec(two, 0);
799 data->deleted = count_lines(one->data, one->size);
800 data->added = count_lines(two->data, two->size);
803 if (fill_mmfile(&mf1, one) < 0 || fill_mmfile(&mf2, two) < 0)
804 die("unable to read files to diff");
806 if (mmfile_is_binary(&mf1) || mmfile_is_binary(&mf2))
809 /* Crazy xdl interfaces.. */
814 xpp.flags = XDF_NEED_MINIMAL | o->xdl_opts;
817 ecb.outf = xdiff_outf;
819 xdl_diff(&mf1, &mf2, &xpp, &xecfg, &ecb);
823 static void builtin_checkdiff(const char *name_a, const char *name_b,
824 struct diff_filespec *one,
825 struct diff_filespec *two)
828 struct checkdiff_t data;
833 memset(&data, 0, sizeof(data));
834 data.xm.consume = checkdiff_consume;
835 data.filename = name_b ? name_b : name_a;
838 if (fill_mmfile(&mf1, one) < 0 || fill_mmfile(&mf2, two) < 0)
839 die("unable to read files to diff");
841 if (mmfile_is_binary(&mf2))
844 /* Crazy xdl interfaces.. */
849 xpp.flags = XDF_NEED_MINIMAL;
852 ecb.outf = xdiff_outf;
854 xdl_diff(&mf1, &mf2, &xpp, &xecfg, &ecb);
858 struct diff_filespec *alloc_filespec(const char *path)
860 int namelen = strlen(path);
861 struct diff_filespec *spec = xmalloc(sizeof(*spec) + namelen + 1);
863 memset(spec, 0, sizeof(*spec));
864 spec->path = (char *)(spec + 1);
865 memcpy(spec->path, path, namelen+1);
869 void fill_filespec(struct diff_filespec *spec, const unsigned char *sha1,
873 spec->mode = canon_mode(mode);
874 memcpy(spec->sha1, sha1, 20);
875 spec->sha1_valid = !!memcmp(sha1, null_sha1, 20);
880 * Given a name and sha1 pair, if the dircache tells us the file in
881 * the work tree has that object contents, return true, so that
882 * prepare_temp_file() does not have to inflate and extract.
884 static int work_tree_matches(const char *name, const unsigned char *sha1)
886 struct cache_entry *ce;
890 /* We do not read the cache ourselves here, because the
891 * benchmark with my previous version that always reads cache
892 * shows that it makes things worse for diff-tree comparing
893 * two linux-2.6 kernel trees in an already checked out work
894 * tree. This is because most diff-tree comparisons deal with
895 * only a small number of files, while reading the cache is
896 * expensive for a large project, and its cost outweighs the
897 * savings we get by not inflating the object to a temporary
898 * file. Practically, this code only helps when we are used
899 * by diff-cache --cached, which does read the cache before
906 pos = cache_name_pos(name, len);
909 ce = active_cache[pos];
910 if ((lstat(name, &st) < 0) ||
911 !S_ISREG(st.st_mode) || /* careful! */
912 ce_match_stat(ce, &st, 0) ||
913 memcmp(sha1, ce->sha1, 20))
915 /* we return 1 only when we can stat, it is a regular file,
916 * stat information matches, and sha1 recorded in the cache
917 * matches. I.e. we know the file in the work tree really is
918 * the same as the <name, sha1> pair.
923 static struct sha1_size_cache {
924 unsigned char sha1[20];
927 static int sha1_size_cache_nr, sha1_size_cache_alloc;
929 static struct sha1_size_cache *locate_size_cache(unsigned char *sha1,
934 struct sha1_size_cache *e;
937 last = sha1_size_cache_nr;
938 while (last > first) {
939 int cmp, next = (last + first) >> 1;
940 e = sha1_size_cache[next];
941 cmp = memcmp(e->sha1, sha1, 20);
953 /* insert to make it at "first" */
954 if (sha1_size_cache_alloc <= sha1_size_cache_nr) {
955 sha1_size_cache_alloc = alloc_nr(sha1_size_cache_alloc);
956 sha1_size_cache = xrealloc(sha1_size_cache,
957 sha1_size_cache_alloc *
958 sizeof(*sha1_size_cache));
960 sha1_size_cache_nr++;
961 if (first < sha1_size_cache_nr)
962 memmove(sha1_size_cache + first + 1, sha1_size_cache + first,
963 (sha1_size_cache_nr - first - 1) *
964 sizeof(*sha1_size_cache));
965 e = xmalloc(sizeof(struct sha1_size_cache));
966 sha1_size_cache[first] = e;
967 memcpy(e->sha1, sha1, 20);
973 * While doing rename detection and pickaxe operation, we may need to
974 * grab the data for the blob (or file) for our own in-core comparison.
975 * diff_filespec has data and size fields for this purpose.
977 int diff_populate_filespec(struct diff_filespec *s, int size_only)
980 if (!DIFF_FILE_VALID(s))
981 die("internal error: asking to populate invalid file.");
982 if (S_ISDIR(s->mode))
990 if (!s->sha1_valid ||
991 work_tree_matches(s->path, s->sha1)) {
994 if (lstat(s->path, &st) < 0) {
995 if (errno == ENOENT) {
999 s->data = (char *)"";
1004 s->size = st.st_size;
1009 if (S_ISLNK(st.st_mode)) {
1011 s->data = xmalloc(s->size);
1013 ret = readlink(s->path, s->data, s->size);
1020 fd = open(s->path, O_RDONLY);
1023 s->data = mmap(NULL, s->size, PROT_READ, MAP_PRIVATE, fd, 0);
1025 if (s->data == MAP_FAILED)
1027 s->should_munmap = 1;
1031 struct sha1_size_cache *e;
1034 e = locate_size_cache(s->sha1, 1, 0);
1039 if (!sha1_object_info(s->sha1, type, &s->size))
1040 locate_size_cache(s->sha1, 0, s->size);
1043 s->data = read_sha1_file(s->sha1, type, &s->size);
1050 void diff_free_filespec_data(struct diff_filespec *s)
1054 else if (s->should_munmap)
1055 munmap(s->data, s->size);
1056 s->should_free = s->should_munmap = 0;
1062 static void prep_temp_blob(struct diff_tempfile *temp,
1065 const unsigned char *sha1,
1070 fd = git_mkstemp(temp->tmp_path, TEMPFILE_PATH_LEN, ".diff_XXXXXX");
1072 die("unable to create temp-file");
1073 if (write(fd, blob, size) != size)
1074 die("unable to write temp-file");
1076 temp->name = temp->tmp_path;
1077 strcpy(temp->hex, sha1_to_hex(sha1));
1079 sprintf(temp->mode, "%06o", mode);
1082 static void prepare_temp_file(const char *name,
1083 struct diff_tempfile *temp,
1084 struct diff_filespec *one)
1086 if (!DIFF_FILE_VALID(one)) {
1088 /* A '-' entry produces this for file-2, and
1089 * a '+' entry produces this for file-1.
1091 temp->name = "/dev/null";
1092 strcpy(temp->hex, ".");
1093 strcpy(temp->mode, ".");
1097 if (!one->sha1_valid ||
1098 work_tree_matches(name, one->sha1)) {
1100 if (lstat(name, &st) < 0) {
1101 if (errno == ENOENT)
1102 goto not_a_valid_file;
1103 die("stat(%s): %s", name, strerror(errno));
1105 if (S_ISLNK(st.st_mode)) {
1107 char buf[PATH_MAX + 1]; /* ought to be SYMLINK_MAX */
1108 if (sizeof(buf) <= st.st_size)
1109 die("symlink too long: %s", name);
1110 ret = readlink(name, buf, st.st_size);
1112 die("readlink(%s)", name);
1113 prep_temp_blob(temp, buf, st.st_size,
1115 one->sha1 : null_sha1),
1117 one->mode : S_IFLNK));
1120 /* we can borrow from the file in the work tree */
1122 if (!one->sha1_valid)
1123 strcpy(temp->hex, sha1_to_hex(null_sha1));
1125 strcpy(temp->hex, sha1_to_hex(one->sha1));
1126 /* Even though we may sometimes borrow the
1127 * contents from the work tree, we always want
1128 * one->mode. mode is trustworthy even when
1129 * !(one->sha1_valid), as long as
1130 * DIFF_FILE_VALID(one).
1132 sprintf(temp->mode, "%06o", one->mode);
1137 if (diff_populate_filespec(one, 0))
1138 die("cannot read data blob for %s", one->path);
1139 prep_temp_blob(temp, one->data, one->size,
1140 one->sha1, one->mode);
1144 static void remove_tempfile(void)
1148 for (i = 0; i < 2; i++)
1149 if (diff_temp[i].name == diff_temp[i].tmp_path) {
1150 unlink(diff_temp[i].name);
1151 diff_temp[i].name = NULL;
1155 static void remove_tempfile_on_signal(int signo)
1158 signal(SIGINT, SIG_DFL);
1162 static int spawn_prog(const char *pgm, const char **arg)
1170 die("unable to fork");
1172 execvp(pgm, (char *const*) arg);
1176 while (waitpid(pid, &status, 0) < 0) {
1182 /* Earlier we did not check the exit status because
1183 * diff exits non-zero if files are different, and
1184 * we are not interested in knowing that. It was a
1185 * mistake which made it harder to quit a diff-*
1186 * session that uses the git-apply-patch-script as
1187 * the GIT_EXTERNAL_DIFF. A custom GIT_EXTERNAL_DIFF
1188 * should also exit non-zero only when it wants to
1189 * abort the entire diff-* session.
1191 if (WIFEXITED(status) && !WEXITSTATUS(status))
1196 /* An external diff command takes:
1198 * diff-cmd name infile1 infile1-sha1 infile1-mode \
1199 * infile2 infile2-sha1 infile2-mode [ rename-to ]
1202 static void run_external_diff(const char *pgm,
1205 struct diff_filespec *one,
1206 struct diff_filespec *two,
1207 const char *xfrm_msg,
1208 int complete_rewrite)
1210 const char *spawn_arg[10];
1211 struct diff_tempfile *temp = diff_temp;
1213 static int atexit_asked = 0;
1214 const char *othername;
1215 const char **arg = &spawn_arg[0];
1217 othername = (other? other : name);
1219 prepare_temp_file(name, &temp[0], one);
1220 prepare_temp_file(othername, &temp[1], two);
1221 if (! atexit_asked &&
1222 (temp[0].name == temp[0].tmp_path ||
1223 temp[1].name == temp[1].tmp_path)) {
1225 atexit(remove_tempfile);
1227 signal(SIGINT, remove_tempfile_on_signal);
1233 *arg++ = temp[0].name;
1234 *arg++ = temp[0].hex;
1235 *arg++ = temp[0].mode;
1236 *arg++ = temp[1].name;
1237 *arg++ = temp[1].hex;
1238 *arg++ = temp[1].mode;
1248 retval = spawn_prog(pgm, spawn_arg);
1251 fprintf(stderr, "external diff died, stopping at %s.\n", name);
1256 static void run_diff_cmd(const char *pgm,
1259 struct diff_filespec *one,
1260 struct diff_filespec *two,
1261 const char *xfrm_msg,
1262 struct diff_options *o,
1263 int complete_rewrite)
1266 run_external_diff(pgm, name, other, one, two, xfrm_msg,
1271 builtin_diff(name, other ? other : name,
1272 one, two, xfrm_msg, o, complete_rewrite);
1274 printf("* Unmerged path %s\n", name);
1277 static void diff_fill_sha1_info(struct diff_filespec *one)
1279 if (DIFF_FILE_VALID(one)) {
1280 if (!one->sha1_valid) {
1282 if (lstat(one->path, &st) < 0)
1283 die("stat %s", one->path);
1284 if (index_path(one->sha1, one->path, &st, 0))
1285 die("cannot hash %s\n", one->path);
1289 memset(one->sha1, 0, 20);
1292 static void run_diff(struct diff_filepair *p, struct diff_options *o)
1294 const char *pgm = external_diff();
1295 char msg[PATH_MAX*2+300], *xfrm_msg;
1296 struct diff_filespec *one;
1297 struct diff_filespec *two;
1300 char *name_munged, *other_munged;
1301 int complete_rewrite = 0;
1304 if (DIFF_PAIR_UNMERGED(p)) {
1306 run_diff_cmd(pgm, p->one->path, NULL, NULL, NULL, NULL, o, 0);
1310 name = p->one->path;
1311 other = (strcmp(name, p->two->path) ? p->two->path : NULL);
1312 name_munged = quote_one(name);
1313 other_munged = quote_one(other);
1314 one = p->one; two = p->two;
1316 diff_fill_sha1_info(one);
1317 diff_fill_sha1_info(two);
1320 switch (p->status) {
1321 case DIFF_STATUS_COPIED:
1322 len += snprintf(msg + len, sizeof(msg) - len,
1323 "similarity index %d%%\n"
1326 (int)(0.5 + p->score * 100.0/MAX_SCORE),
1327 name_munged, other_munged);
1329 case DIFF_STATUS_RENAMED:
1330 len += snprintf(msg + len, sizeof(msg) - len,
1331 "similarity index %d%%\n"
1334 (int)(0.5 + p->score * 100.0/MAX_SCORE),
1335 name_munged, other_munged);
1337 case DIFF_STATUS_MODIFIED:
1339 len += snprintf(msg + len, sizeof(msg) - len,
1340 "dissimilarity index %d%%\n",
1341 (int)(0.5 + p->score *
1343 complete_rewrite = 1;
1352 if (memcmp(one->sha1, two->sha1, 20)) {
1353 int abbrev = o->full_index ? 40 : DEFAULT_ABBREV;
1355 len += snprintf(msg + len, sizeof(msg) - len,
1357 abbrev, sha1_to_hex(one->sha1),
1358 abbrev, sha1_to_hex(two->sha1));
1359 if (one->mode == two->mode)
1360 len += snprintf(msg + len, sizeof(msg) - len,
1361 " %06o", one->mode);
1362 len += snprintf(msg + len, sizeof(msg) - len, "\n");
1367 xfrm_msg = len ? msg : NULL;
1370 DIFF_FILE_VALID(one) && DIFF_FILE_VALID(two) &&
1371 (S_IFMT & one->mode) != (S_IFMT & two->mode)) {
1372 /* a filepair that changes between file and symlink
1373 * needs to be split into deletion and creation.
1375 struct diff_filespec *null = alloc_filespec(two->path);
1376 run_diff_cmd(NULL, name, other, one, null, xfrm_msg, o, 0);
1378 null = alloc_filespec(one->path);
1379 run_diff_cmd(NULL, name, other, null, two, xfrm_msg, o, 0);
1383 run_diff_cmd(pgm, name, other, one, two, xfrm_msg, o,
1390 static void run_diffstat(struct diff_filepair *p, struct diff_options *o,
1391 struct diffstat_t *diffstat)
1395 int complete_rewrite = 0;
1397 if (DIFF_PAIR_UNMERGED(p)) {
1399 builtin_diffstat(p->one->path, NULL, NULL, NULL, diffstat, o, 0);
1403 name = p->one->path;
1404 other = (strcmp(name, p->two->path) ? p->two->path : NULL);
1406 diff_fill_sha1_info(p->one);
1407 diff_fill_sha1_info(p->two);
1409 if (p->status == DIFF_STATUS_MODIFIED && p->score)
1410 complete_rewrite = 1;
1411 builtin_diffstat(name, other, p->one, p->two, diffstat, o, complete_rewrite);
1414 static void run_checkdiff(struct diff_filepair *p, struct diff_options *o)
1419 if (DIFF_PAIR_UNMERGED(p)) {
1424 name = p->one->path;
1425 other = (strcmp(name, p->two->path) ? p->two->path : NULL);
1427 diff_fill_sha1_info(p->one);
1428 diff_fill_sha1_info(p->two);
1430 builtin_checkdiff(name, other, p->one, p->two);
1433 void diff_setup(struct diff_options *options)
1435 memset(options, 0, sizeof(*options));
1436 options->line_termination = '\n';
1437 options->break_opt = -1;
1438 options->rename_limit = -1;
1439 options->context = 3;
1440 options->msg_sep = "";
1442 options->change = diff_change;
1443 options->add_remove = diff_addremove;
1444 options->color_diff = diff_use_color_default;
1445 options->detect_rename = diff_detect_rename_default;
1448 int diff_setup_done(struct diff_options *options)
1450 if ((options->find_copies_harder &&
1451 options->detect_rename != DIFF_DETECT_COPY) ||
1452 (0 <= options->rename_limit && !options->detect_rename))
1455 if (options->output_format & (DIFF_FORMAT_NAME |
1456 DIFF_FORMAT_NAME_STATUS |
1457 DIFF_FORMAT_CHECKDIFF |
1458 DIFF_FORMAT_NO_OUTPUT))
1459 options->output_format &= ~(DIFF_FORMAT_RAW |
1460 DIFF_FORMAT_DIFFSTAT |
1461 DIFF_FORMAT_SUMMARY |
1465 * These cases always need recursive; we do not drop caller-supplied
1466 * recursive bits for other formats here.
1468 if (options->output_format & (DIFF_FORMAT_PATCH |
1469 DIFF_FORMAT_DIFFSTAT |
1470 DIFF_FORMAT_CHECKDIFF))
1471 options->recursive = 1;
1473 * Also pickaxe would not work very well if you do not say recursive
1475 if (options->pickaxe)
1476 options->recursive = 1;
1478 if (options->detect_rename && options->rename_limit < 0)
1479 options->rename_limit = diff_rename_limit_default;
1480 if (options->setup & DIFF_SETUP_USE_CACHE) {
1482 /* read-cache does not die even when it fails
1483 * so it is safe for us to do this here. Also
1484 * it does not smudge active_cache or active_nr
1485 * when it fails, so we do not have to worry about
1486 * cleaning it up ourselves either.
1490 if (options->setup & DIFF_SETUP_USE_SIZE_CACHE)
1492 if (options->abbrev <= 0 || 40 < options->abbrev)
1493 options->abbrev = 40; /* full */
1498 static int opt_arg(const char *arg, int arg_short, const char *arg_long, int *val)
1508 if (c == arg_short) {
1512 if (val && isdigit(c)) {
1514 int n = strtoul(arg, &end, 10);
1525 eq = strchr(arg, '=');
1530 if (!len || strncmp(arg, arg_long, len))
1535 if (!isdigit(*++eq))
1537 n = strtoul(eq, &end, 10);
1545 int diff_opt_parse(struct diff_options *options, const char **av, int ac)
1547 const char *arg = av[0];
1548 if (!strcmp(arg, "-p") || !strcmp(arg, "-u"))
1549 options->output_format |= DIFF_FORMAT_PATCH;
1550 else if (opt_arg(arg, 'U', "unified", &options->context))
1551 options->output_format |= DIFF_FORMAT_PATCH;
1552 else if (!strcmp(arg, "--raw"))
1553 options->output_format |= DIFF_FORMAT_RAW;
1554 else if (!strcmp(arg, "--patch-with-raw")) {
1555 options->output_format |= DIFF_FORMAT_PATCH | DIFF_FORMAT_RAW;
1557 else if (!strcmp(arg, "--stat"))
1558 options->output_format |= DIFF_FORMAT_DIFFSTAT;
1559 else if (!strcmp(arg, "--check"))
1560 options->output_format |= DIFF_FORMAT_CHECKDIFF;
1561 else if (!strcmp(arg, "--summary"))
1562 options->output_format |= DIFF_FORMAT_SUMMARY;
1563 else if (!strcmp(arg, "--patch-with-stat")) {
1564 options->output_format |= DIFF_FORMAT_PATCH | DIFF_FORMAT_DIFFSTAT;
1566 else if (!strcmp(arg, "-z"))
1567 options->line_termination = 0;
1568 else if (!strncmp(arg, "-l", 2))
1569 options->rename_limit = strtoul(arg+2, NULL, 10);
1570 else if (!strcmp(arg, "--full-index"))
1571 options->full_index = 1;
1572 else if (!strcmp(arg, "--binary")) {
1573 options->output_format |= DIFF_FORMAT_PATCH;
1574 options->full_index = options->binary = 1;
1576 else if (!strcmp(arg, "-a") || !strcmp(arg, "--text")) {
1579 else if (!strcmp(arg, "--name-only"))
1580 options->output_format |= DIFF_FORMAT_NAME;
1581 else if (!strcmp(arg, "--name-status"))
1582 options->output_format |= DIFF_FORMAT_NAME_STATUS;
1583 else if (!strcmp(arg, "-R"))
1584 options->reverse_diff = 1;
1585 else if (!strncmp(arg, "-S", 2))
1586 options->pickaxe = arg + 2;
1587 else if (!strcmp(arg, "-s")) {
1588 options->output_format |= DIFF_FORMAT_NO_OUTPUT;
1590 else if (!strncmp(arg, "-O", 2))
1591 options->orderfile = arg + 2;
1592 else if (!strncmp(arg, "--diff-filter=", 14))
1593 options->filter = arg + 14;
1594 else if (!strcmp(arg, "--pickaxe-all"))
1595 options->pickaxe_opts = DIFF_PICKAXE_ALL;
1596 else if (!strcmp(arg, "--pickaxe-regex"))
1597 options->pickaxe_opts = DIFF_PICKAXE_REGEX;
1598 else if (!strncmp(arg, "-B", 2)) {
1599 if ((options->break_opt =
1600 diff_scoreopt_parse(arg)) == -1)
1603 else if (!strncmp(arg, "-M", 2)) {
1604 if ((options->rename_score =
1605 diff_scoreopt_parse(arg)) == -1)
1607 options->detect_rename = DIFF_DETECT_RENAME;
1609 else if (!strncmp(arg, "-C", 2)) {
1610 if ((options->rename_score =
1611 diff_scoreopt_parse(arg)) == -1)
1613 options->detect_rename = DIFF_DETECT_COPY;
1615 else if (!strcmp(arg, "--find-copies-harder"))
1616 options->find_copies_harder = 1;
1617 else if (!strcmp(arg, "--abbrev"))
1618 options->abbrev = DEFAULT_ABBREV;
1619 else if (!strncmp(arg, "--abbrev=", 9)) {
1620 options->abbrev = strtoul(arg + 9, NULL, 10);
1621 if (options->abbrev < MINIMUM_ABBREV)
1622 options->abbrev = MINIMUM_ABBREV;
1623 else if (40 < options->abbrev)
1624 options->abbrev = 40;
1626 else if (!strcmp(arg, "--color"))
1627 options->color_diff = 1;
1628 else if (!strcmp(arg, "-w") || !strcmp(arg, "--ignore-all-space"))
1629 options->xdl_opts |= XDF_IGNORE_WHITESPACE;
1630 else if (!strcmp(arg, "-b") || !strcmp(arg, "--ignore-space-change"))
1631 options->xdl_opts |= XDF_IGNORE_WHITESPACE_CHANGE;
1632 else if (!strcmp(arg, "--no-renames"))
1633 options->detect_rename = 0;
1639 static int parse_num(const char **cp_p)
1641 unsigned long num, scale;
1643 const char *cp = *cp_p;
1650 if ( !dot && ch == '.' ) {
1653 } else if ( ch == '%' ) {
1654 scale = dot ? scale*100 : 100;
1655 cp++; /* % is always at the end */
1657 } else if ( ch >= '0' && ch <= '9' ) {
1658 if ( scale < 100000 ) {
1660 num = (num*10) + (ch-'0');
1669 /* user says num divided by scale and we say internally that
1670 * is MAX_SCORE * num / scale.
1672 return (num >= scale) ? MAX_SCORE : (MAX_SCORE * num / scale);
1675 int diff_scoreopt_parse(const char *opt)
1677 int opt1, opt2, cmd;
1682 if (cmd != 'M' && cmd != 'C' && cmd != 'B')
1683 return -1; /* that is not a -M, -C nor -B option */
1685 opt1 = parse_num(&opt);
1691 else if (*opt != '/')
1692 return -1; /* we expect -B80/99 or -B80 */
1695 opt2 = parse_num(&opt);
1700 return opt1 | (opt2 << 16);
1703 struct diff_queue_struct diff_queued_diff;
1705 void diff_q(struct diff_queue_struct *queue, struct diff_filepair *dp)
1707 if (queue->alloc <= queue->nr) {
1708 queue->alloc = alloc_nr(queue->alloc);
1709 queue->queue = xrealloc(queue->queue,
1710 sizeof(dp) * queue->alloc);
1712 queue->queue[queue->nr++] = dp;
1715 struct diff_filepair *diff_queue(struct diff_queue_struct *queue,
1716 struct diff_filespec *one,
1717 struct diff_filespec *two)
1719 struct diff_filepair *dp = xmalloc(sizeof(*dp));
1724 dp->source_stays = 0;
1725 dp->broken_pair = 0;
1731 void diff_free_filepair(struct diff_filepair *p)
1733 diff_free_filespec_data(p->one);
1734 diff_free_filespec_data(p->two);
1740 /* This is different from find_unique_abbrev() in that
1741 * it stuffs the result with dots for alignment.
1743 const char *diff_unique_abbrev(const unsigned char *sha1, int len)
1748 return sha1_to_hex(sha1);
1750 abbrev = find_unique_abbrev(sha1, len);
1752 return sha1_to_hex(sha1);
1753 abblen = strlen(abbrev);
1755 static char hex[41];
1756 if (len < abblen && abblen <= len + 2)
1757 sprintf(hex, "%s%.*s", abbrev, len+3-abblen, "..");
1759 sprintf(hex, "%s...", abbrev);
1762 return sha1_to_hex(sha1);
1765 static void diff_flush_raw(struct diff_filepair *p,
1766 struct diff_options *options)
1770 int abbrev = options->abbrev;
1771 const char *path_one, *path_two;
1772 int inter_name_termination = '\t';
1773 int line_termination = options->line_termination;
1775 if (!line_termination)
1776 inter_name_termination = 0;
1778 path_one = p->one->path;
1779 path_two = p->two->path;
1780 if (line_termination) {
1781 path_one = quote_one(path_one);
1782 path_two = quote_one(path_two);
1786 sprintf(status, "%c%03d", p->status,
1787 (int)(0.5 + p->score * 100.0/MAX_SCORE));
1789 status[0] = p->status;
1792 switch (p->status) {
1793 case DIFF_STATUS_COPIED:
1794 case DIFF_STATUS_RENAMED:
1797 case DIFF_STATUS_ADDED:
1798 case DIFF_STATUS_DELETED:
1805 if (!(options->output_format & DIFF_FORMAT_NAME_STATUS)) {
1806 printf(":%06o %06o %s ",
1807 p->one->mode, p->two->mode,
1808 diff_unique_abbrev(p->one->sha1, abbrev));
1810 diff_unique_abbrev(p->two->sha1, abbrev));
1812 printf("%s%c%s", status, inter_name_termination, path_one);
1814 printf("%c%s", inter_name_termination, path_two);
1815 putchar(line_termination);
1816 if (path_one != p->one->path)
1817 free((void*)path_one);
1818 if (path_two != p->two->path)
1819 free((void*)path_two);
1822 static void diff_flush_name(struct diff_filepair *p, int line_termination)
1824 char *path = p->two->path;
1826 if (line_termination)
1827 path = quote_one(p->two->path);
1828 printf("%s%c", path, line_termination);
1829 if (p->two->path != path)
1833 int diff_unmodified_pair(struct diff_filepair *p)
1835 /* This function is written stricter than necessary to support
1836 * the currently implemented transformers, but the idea is to
1837 * let transformers to produce diff_filepairs any way they want,
1838 * and filter and clean them up here before producing the output.
1840 struct diff_filespec *one, *two;
1842 if (DIFF_PAIR_UNMERGED(p))
1843 return 0; /* unmerged is interesting */
1848 /* deletion, addition, mode or type change
1849 * and rename are all interesting.
1851 if (DIFF_FILE_VALID(one) != DIFF_FILE_VALID(two) ||
1852 DIFF_PAIR_MODE_CHANGED(p) ||
1853 strcmp(one->path, two->path))
1856 /* both are valid and point at the same path. that is, we are
1857 * dealing with a change.
1859 if (one->sha1_valid && two->sha1_valid &&
1860 !memcmp(one->sha1, two->sha1, sizeof(one->sha1)))
1861 return 1; /* no change */
1862 if (!one->sha1_valid && !two->sha1_valid)
1863 return 1; /* both look at the same file on the filesystem. */
1867 static void diff_flush_patch(struct diff_filepair *p, struct diff_options *o)
1869 if (diff_unmodified_pair(p))
1872 if ((DIFF_FILE_VALID(p->one) && S_ISDIR(p->one->mode)) ||
1873 (DIFF_FILE_VALID(p->two) && S_ISDIR(p->two->mode)))
1874 return; /* no tree diffs in patch format */
1879 static void diff_flush_stat(struct diff_filepair *p, struct diff_options *o,
1880 struct diffstat_t *diffstat)
1882 if (diff_unmodified_pair(p))
1885 if ((DIFF_FILE_VALID(p->one) && S_ISDIR(p->one->mode)) ||
1886 (DIFF_FILE_VALID(p->two) && S_ISDIR(p->two->mode)))
1887 return; /* no tree diffs in patch format */
1889 run_diffstat(p, o, diffstat);
1892 static void diff_flush_checkdiff(struct diff_filepair *p,
1893 struct diff_options *o)
1895 if (diff_unmodified_pair(p))
1898 if ((DIFF_FILE_VALID(p->one) && S_ISDIR(p->one->mode)) ||
1899 (DIFF_FILE_VALID(p->two) && S_ISDIR(p->two->mode)))
1900 return; /* no tree diffs in patch format */
1902 run_checkdiff(p, o);
1905 int diff_queue_is_empty(void)
1907 struct diff_queue_struct *q = &diff_queued_diff;
1909 for (i = 0; i < q->nr; i++)
1910 if (!diff_unmodified_pair(q->queue[i]))
1916 void diff_debug_filespec(struct diff_filespec *s, int x, const char *one)
1918 fprintf(stderr, "queue[%d] %s (%s) %s %06o %s\n",
1921 DIFF_FILE_VALID(s) ? "valid" : "invalid",
1923 s->sha1_valid ? sha1_to_hex(s->sha1) : "");
1924 fprintf(stderr, "queue[%d] %s size %lu flags %d\n",
1926 s->size, s->xfrm_flags);
1929 void diff_debug_filepair(const struct diff_filepair *p, int i)
1931 diff_debug_filespec(p->one, i, "one");
1932 diff_debug_filespec(p->two, i, "two");
1933 fprintf(stderr, "score %d, status %c stays %d broken %d\n",
1934 p->score, p->status ? p->status : '?',
1935 p->source_stays, p->broken_pair);
1938 void diff_debug_queue(const char *msg, struct diff_queue_struct *q)
1942 fprintf(stderr, "%s\n", msg);
1943 fprintf(stderr, "q->nr = %d\n", q->nr);
1944 for (i = 0; i < q->nr; i++) {
1945 struct diff_filepair *p = q->queue[i];
1946 diff_debug_filepair(p, i);
1951 static void diff_resolve_rename_copy(void)
1954 struct diff_filepair *p, *pp;
1955 struct diff_queue_struct *q = &diff_queued_diff;
1957 diff_debug_queue("resolve-rename-copy", q);
1959 for (i = 0; i < q->nr; i++) {
1961 p->status = 0; /* undecided */
1962 if (DIFF_PAIR_UNMERGED(p))
1963 p->status = DIFF_STATUS_UNMERGED;
1964 else if (!DIFF_FILE_VALID(p->one))
1965 p->status = DIFF_STATUS_ADDED;
1966 else if (!DIFF_FILE_VALID(p->two))
1967 p->status = DIFF_STATUS_DELETED;
1968 else if (DIFF_PAIR_TYPE_CHANGED(p))
1969 p->status = DIFF_STATUS_TYPE_CHANGED;
1971 /* from this point on, we are dealing with a pair
1972 * whose both sides are valid and of the same type, i.e.
1973 * either in-place edit or rename/copy edit.
1975 else if (DIFF_PAIR_RENAME(p)) {
1976 if (p->source_stays) {
1977 p->status = DIFF_STATUS_COPIED;
1980 /* See if there is some other filepair that
1981 * copies from the same source as us. If so
1982 * we are a copy. Otherwise we are either a
1983 * copy if the path stays, or a rename if it
1984 * does not, but we already handled "stays" case.
1986 for (j = i + 1; j < q->nr; j++) {
1988 if (strcmp(pp->one->path, p->one->path))
1989 continue; /* not us */
1990 if (!DIFF_PAIR_RENAME(pp))
1991 continue; /* not a rename/copy */
1992 /* pp is a rename/copy from the same source */
1993 p->status = DIFF_STATUS_COPIED;
1997 p->status = DIFF_STATUS_RENAMED;
1999 else if (memcmp(p->one->sha1, p->two->sha1, 20) ||
2000 p->one->mode != p->two->mode)
2001 p->status = DIFF_STATUS_MODIFIED;
2003 /* This is a "no-change" entry and should not
2004 * happen anymore, but prepare for broken callers.
2006 error("feeding unmodified %s to diffcore",
2008 p->status = DIFF_STATUS_UNKNOWN;
2011 diff_debug_queue("resolve-rename-copy done", q);
2014 static int check_pair_status(struct diff_filepair *p)
2016 switch (p->status) {
2017 case DIFF_STATUS_UNKNOWN:
2020 die("internal error in diff-resolve-rename-copy");
2026 static void flush_one_pair(struct diff_filepair *p, struct diff_options *opt)
2028 int fmt = opt->output_format;
2030 if (fmt & DIFF_FORMAT_CHECKDIFF)
2031 diff_flush_checkdiff(p, opt);
2032 else if (fmt & (DIFF_FORMAT_RAW | DIFF_FORMAT_NAME_STATUS))
2033 diff_flush_raw(p, opt);
2034 else if (fmt & DIFF_FORMAT_NAME)
2035 diff_flush_name(p, opt->line_termination);
2038 static void show_file_mode_name(const char *newdelete, struct diff_filespec *fs)
2041 printf(" %s mode %06o %s\n", newdelete, fs->mode, fs->path);
2043 printf(" %s %s\n", newdelete, fs->path);
2047 static void show_mode_change(struct diff_filepair *p, int show_name)
2049 if (p->one->mode && p->two->mode && p->one->mode != p->two->mode) {
2051 printf(" mode change %06o => %06o %s\n",
2052 p->one->mode, p->two->mode, p->two->path);
2054 printf(" mode change %06o => %06o\n",
2055 p->one->mode, p->two->mode);
2059 static void show_rename_copy(const char *renamecopy, struct diff_filepair *p)
2061 const char *old, *new;
2063 /* Find common prefix */
2067 const char *slash_old, *slash_new;
2068 slash_old = strchr(old, '/');
2069 slash_new = strchr(new, '/');
2072 slash_old - old != slash_new - new ||
2073 memcmp(old, new, slash_new - new))
2075 old = slash_old + 1;
2076 new = slash_new + 1;
2078 /* p->one->path thru old is the common prefix, and old and new
2079 * through the end of names are renames
2081 if (old != p->one->path)
2082 printf(" %s %.*s{%s => %s} (%d%%)\n", renamecopy,
2083 (int)(old - p->one->path), p->one->path,
2084 old, new, (int)(0.5 + p->score * 100.0/MAX_SCORE));
2086 printf(" %s %s => %s (%d%%)\n", renamecopy,
2087 p->one->path, p->two->path,
2088 (int)(0.5 + p->score * 100.0/MAX_SCORE));
2089 show_mode_change(p, 0);
2092 static void diff_summary(struct diff_filepair *p)
2095 case DIFF_STATUS_DELETED:
2096 show_file_mode_name("delete", p->one);
2098 case DIFF_STATUS_ADDED:
2099 show_file_mode_name("create", p->two);
2101 case DIFF_STATUS_COPIED:
2102 show_rename_copy("copy", p);
2104 case DIFF_STATUS_RENAMED:
2105 show_rename_copy("rename", p);
2109 printf(" rewrite %s (%d%%)\n", p->two->path,
2110 (int)(0.5 + p->score * 100.0/MAX_SCORE));
2111 show_mode_change(p, 0);
2112 } else show_mode_change(p, 1);
2118 struct xdiff_emit_state xm;
2123 static int remove_space(char *line, int len)
2129 for (i = 0; i < len; i++)
2130 if (!isspace((c = line[i])))
2136 static void patch_id_consume(void *priv, char *line, unsigned long len)
2138 struct patch_id_t *data = priv;
2141 /* Ignore line numbers when computing the SHA1 of the patch */
2142 if (!strncmp(line, "@@ -", 4))
2145 new_len = remove_space(line, len);
2147 SHA1_Update(data->ctx, line, new_len);
2148 data->patchlen += new_len;
2151 /* returns 0 upon success, and writes result into sha1 */
2152 static int diff_get_patch_id(struct diff_options *options, unsigned char *sha1)
2154 struct diff_queue_struct *q = &diff_queued_diff;
2157 struct patch_id_t data;
2158 char buffer[PATH_MAX * 4 + 20];
2161 memset(&data, 0, sizeof(struct patch_id_t));
2163 data.xm.consume = patch_id_consume;
2165 for (i = 0; i < q->nr; i++) {
2170 struct diff_filepair *p = q->queue[i];
2174 return error("internal diff status error");
2175 if (p->status == DIFF_STATUS_UNKNOWN)
2177 if (diff_unmodified_pair(p))
2179 if ((DIFF_FILE_VALID(p->one) && S_ISDIR(p->one->mode)) ||
2180 (DIFF_FILE_VALID(p->two) && S_ISDIR(p->two->mode)))
2182 if (DIFF_PAIR_UNMERGED(p))
2185 diff_fill_sha1_info(p->one);
2186 diff_fill_sha1_info(p->two);
2187 if (fill_mmfile(&mf1, p->one) < 0 ||
2188 fill_mmfile(&mf2, p->two) < 0)
2189 return error("unable to read files to diff");
2191 /* Maybe hash p->two? into the patch id? */
2192 if (mmfile_is_binary(&mf2))
2195 len1 = remove_space(p->one->path, strlen(p->one->path));
2196 len2 = remove_space(p->two->path, strlen(p->two->path));
2197 if (p->one->mode == 0)
2198 len1 = snprintf(buffer, sizeof(buffer),
2199 "diff--gita/%.*sb/%.*s"
2206 len2, p->two->path);
2207 else if (p->two->mode == 0)
2208 len1 = snprintf(buffer, sizeof(buffer),
2209 "diff--gita/%.*sb/%.*s"
2210 "deletedfilemode%06o"
2216 len1, p->one->path);
2218 len1 = snprintf(buffer, sizeof(buffer),
2219 "diff--gita/%.*sb/%.*s"
2225 len2, p->two->path);
2226 SHA1_Update(&ctx, buffer, len1);
2228 xpp.flags = XDF_NEED_MINIMAL;
2230 xecfg.flags = XDL_EMIT_FUNCNAMES;
2231 ecb.outf = xdiff_outf;
2233 xdl_diff(&mf1, &mf2, &xpp, &xecfg, &ecb);
2236 SHA1_Final(sha1, &ctx);
2240 int diff_flush_patch_id(struct diff_options *options, unsigned char *sha1)
2242 struct diff_queue_struct *q = &diff_queued_diff;
2244 int result = diff_get_patch_id(options, sha1);
2246 for (i = 0; i < q->nr; i++)
2247 diff_free_filepair(q->queue[i]);
2251 q->nr = q->alloc = 0;
2256 static int is_summary_empty(const struct diff_queue_struct *q)
2260 for (i = 0; i < q->nr; i++) {
2261 const struct diff_filepair *p = q->queue[i];
2263 switch (p->status) {
2264 case DIFF_STATUS_DELETED:
2265 case DIFF_STATUS_ADDED:
2266 case DIFF_STATUS_COPIED:
2267 case DIFF_STATUS_RENAMED:
2272 if (p->one->mode && p->two->mode &&
2273 p->one->mode != p->two->mode)
2281 void diff_flush(struct diff_options *options)
2283 struct diff_queue_struct *q = &diff_queued_diff;
2284 int i, output_format = options->output_format;
2288 * Order: raw, stat, summary, patch
2289 * or: name/name-status/checkdiff (other bits clear)
2294 if (output_format & (DIFF_FORMAT_RAW |
2296 DIFF_FORMAT_NAME_STATUS |
2297 DIFF_FORMAT_CHECKDIFF)) {
2298 for (i = 0; i < q->nr; i++) {
2299 struct diff_filepair *p = q->queue[i];
2300 if (check_pair_status(p))
2301 flush_one_pair(p, options);
2306 if (output_format & DIFF_FORMAT_DIFFSTAT) {
2307 struct diffstat_t diffstat;
2309 memset(&diffstat, 0, sizeof(struct diffstat_t));
2310 diffstat.xm.consume = diffstat_consume;
2311 for (i = 0; i < q->nr; i++) {
2312 struct diff_filepair *p = q->queue[i];
2313 if (check_pair_status(p))
2314 diff_flush_stat(p, options, &diffstat);
2316 show_stats(&diffstat);
2320 if (output_format & DIFF_FORMAT_SUMMARY && !is_summary_empty(q)) {
2321 for (i = 0; i < q->nr; i++)
2322 diff_summary(q->queue[i]);
2326 if (output_format & DIFF_FORMAT_PATCH) {
2328 if (options->stat_sep) {
2329 /* attach patch instead of inline */
2330 fputs(options->stat_sep, stdout);
2332 putchar(options->line_termination);
2336 for (i = 0; i < q->nr; i++) {
2337 struct diff_filepair *p = q->queue[i];
2338 if (check_pair_status(p))
2339 diff_flush_patch(p, options);
2343 for (i = 0; i < q->nr; i++)
2344 diff_free_filepair(q->queue[i]);
2348 q->nr = q->alloc = 0;
2351 static void diffcore_apply_filter(const char *filter)
2354 struct diff_queue_struct *q = &diff_queued_diff;
2355 struct diff_queue_struct outq;
2357 outq.nr = outq.alloc = 0;
2362 if (strchr(filter, DIFF_STATUS_FILTER_AON)) {
2364 for (i = found = 0; !found && i < q->nr; i++) {
2365 struct diff_filepair *p = q->queue[i];
2366 if (((p->status == DIFF_STATUS_MODIFIED) &&
2368 strchr(filter, DIFF_STATUS_FILTER_BROKEN)) ||
2370 strchr(filter, DIFF_STATUS_MODIFIED)))) ||
2371 ((p->status != DIFF_STATUS_MODIFIED) &&
2372 strchr(filter, p->status)))
2378 /* otherwise we will clear the whole queue
2379 * by copying the empty outq at the end of this
2380 * function, but first clear the current entries
2383 for (i = 0; i < q->nr; i++)
2384 diff_free_filepair(q->queue[i]);
2387 /* Only the matching ones */
2388 for (i = 0; i < q->nr; i++) {
2389 struct diff_filepair *p = q->queue[i];
2391 if (((p->status == DIFF_STATUS_MODIFIED) &&
2393 strchr(filter, DIFF_STATUS_FILTER_BROKEN)) ||
2395 strchr(filter, DIFF_STATUS_MODIFIED)))) ||
2396 ((p->status != DIFF_STATUS_MODIFIED) &&
2397 strchr(filter, p->status)))
2400 diff_free_filepair(p);
2407 void diffcore_std(struct diff_options *options)
2409 if (options->break_opt != -1)
2410 diffcore_break(options->break_opt);
2411 if (options->detect_rename)
2412 diffcore_rename(options);
2413 if (options->break_opt != -1)
2414 diffcore_merge_broken();
2415 if (options->pickaxe)
2416 diffcore_pickaxe(options->pickaxe, options->pickaxe_opts);
2417 if (options->orderfile)
2418 diffcore_order(options->orderfile);
2419 diff_resolve_rename_copy();
2420 diffcore_apply_filter(options->filter);
2424 void diffcore_std_no_resolve(struct diff_options *options)
2426 if (options->pickaxe)
2427 diffcore_pickaxe(options->pickaxe, options->pickaxe_opts);
2428 if (options->orderfile)
2429 diffcore_order(options->orderfile);
2430 diffcore_apply_filter(options->filter);
2433 void diff_addremove(struct diff_options *options,
2434 int addremove, unsigned mode,
2435 const unsigned char *sha1,
2436 const char *base, const char *path)
2438 char concatpath[PATH_MAX];
2439 struct diff_filespec *one, *two;
2441 /* This may look odd, but it is a preparation for
2442 * feeding "there are unchanged files which should
2443 * not produce diffs, but when you are doing copy
2444 * detection you would need them, so here they are"
2445 * entries to the diff-core. They will be prefixed
2446 * with something like '=' or '*' (I haven't decided
2447 * which but should not make any difference).
2448 * Feeding the same new and old to diff_change()
2449 * also has the same effect.
2450 * Before the final output happens, they are pruned after
2451 * merged into rename/copy pairs as appropriate.
2453 if (options->reverse_diff)
2454 addremove = (addremove == '+' ? '-' :
2455 addremove == '-' ? '+' : addremove);
2457 if (!path) path = "";
2458 sprintf(concatpath, "%s%s", base, path);
2459 one = alloc_filespec(concatpath);
2460 two = alloc_filespec(concatpath);
2462 if (addremove != '+')
2463 fill_filespec(one, sha1, mode);
2464 if (addremove != '-')
2465 fill_filespec(two, sha1, mode);
2467 diff_queue(&diff_queued_diff, one, two);
2470 void diff_change(struct diff_options *options,
2471 unsigned old_mode, unsigned new_mode,
2472 const unsigned char *old_sha1,
2473 const unsigned char *new_sha1,
2474 const char *base, const char *path)
2476 char concatpath[PATH_MAX];
2477 struct diff_filespec *one, *two;
2479 if (options->reverse_diff) {
2481 const unsigned char *tmp_c;
2482 tmp = old_mode; old_mode = new_mode; new_mode = tmp;
2483 tmp_c = old_sha1; old_sha1 = new_sha1; new_sha1 = tmp_c;
2485 if (!path) path = "";
2486 sprintf(concatpath, "%s%s", base, path);
2487 one = alloc_filespec(concatpath);
2488 two = alloc_filespec(concatpath);
2489 fill_filespec(one, old_sha1, old_mode);
2490 fill_filespec(two, new_sha1, new_mode);
2492 diff_queue(&diff_queued_diff, one, two);
2495 void diff_unmerge(struct diff_options *options,
2498 struct diff_filespec *one, *two;
2499 one = alloc_filespec(path);
2500 two = alloc_filespec(path);
2501 diff_queue(&diff_queued_diff, one, two);