2 * Copyright (C) 2005 Junio C Hamano
12 #include "xdiff-interface.h"
14 static int use_size_cache;
16 static int diff_rename_limit_default = -1;
17 static int diff_use_color_default = 0;
28 #define COLOR_NORMAL ""
29 #define COLOR_BOLD "\033[1m"
30 #define COLOR_DIM "\033[2m"
31 #define COLOR_UL "\033[4m"
32 #define COLOR_BLINK "\033[5m"
33 #define COLOR_REVERSE "\033[7m"
34 #define COLOR_RESET "\033[m"
36 #define COLOR_BLACK "\033[30m"
37 #define COLOR_RED "\033[31m"
38 #define COLOR_GREEN "\033[32m"
39 #define COLOR_YELLOW "\033[33m"
40 #define COLOR_BLUE "\033[34m"
41 #define COLOR_MAGENTA "\033[35m"
42 #define COLOR_CYAN "\033[36m"
43 #define COLOR_WHITE "\033[37m"
45 static const char *diff_colors[] = {
46 [DIFF_RESET] = COLOR_RESET,
47 [DIFF_PLAIN] = COLOR_NORMAL,
48 [DIFF_METAINFO] = COLOR_BOLD,
49 [DIFF_FRAGINFO] = COLOR_CYAN,
50 [DIFF_FILE_OLD] = COLOR_RED,
51 [DIFF_FILE_NEW] = COLOR_GREEN,
54 static int parse_diff_color_slot(const char *var, int ofs)
56 if (!strcasecmp(var+ofs, "plain"))
58 if (!strcasecmp(var+ofs, "meta"))
60 if (!strcasecmp(var+ofs, "frag"))
62 if (!strcasecmp(var+ofs, "old"))
64 if (!strcasecmp(var+ofs, "new"))
66 die("bad config variable '%s'", var);
69 static const char *parse_diff_color_value(const char *value, const char *var)
71 if (!strcasecmp(value, "normal"))
73 if (!strcasecmp(value, "bold"))
75 if (!strcasecmp(value, "dim"))
77 if (!strcasecmp(value, "ul"))
79 if (!strcasecmp(value, "blink"))
81 if (!strcasecmp(value, "reverse"))
83 if (!strcasecmp(value, "reset"))
85 if (!strcasecmp(value, "black"))
87 if (!strcasecmp(value, "red"))
89 if (!strcasecmp(value, "green"))
91 if (!strcasecmp(value, "yellow"))
93 if (!strcasecmp(value, "blue"))
95 if (!strcasecmp(value, "magenta"))
97 if (!strcasecmp(value, "cyan"))
99 if (!strcasecmp(value, "white"))
101 die("bad config value '%s' for variable '%s'", value, var);
104 int git_diff_config(const char *var, const char *value)
106 if (!strcmp(var, "diff.renamelimit")) {
107 diff_rename_limit_default = git_config_int(var, value);
110 if (!strcmp(var, "diff.color")) {
112 diff_use_color_default = 1; /* bool */
113 else if (!strcasecmp(value, "auto"))
114 diff_use_color_default = isatty(1);
115 else if (!strcasecmp(value, "never"))
116 diff_use_color_default = 0;
117 else if (!strcasecmp(value, "always"))
118 diff_use_color_default = 1;
120 diff_use_color_default = git_config_bool(var, value);
123 if (!strncmp(var, "diff.color.", 11)) {
124 int slot = parse_diff_color_slot(var, 11);
125 diff_colors[slot] = parse_diff_color_value(value, var);
128 return git_default_config(var, value);
131 static char *quote_one(const char *str)
138 needlen = quote_c_style(str, NULL, NULL, 0);
141 xp = xmalloc(needlen + 1);
142 quote_c_style(str, xp, NULL, 0);
146 static char *quote_two(const char *one, const char *two)
148 int need_one = quote_c_style(one, NULL, NULL, 1);
149 int need_two = quote_c_style(two, NULL, NULL, 1);
152 if (need_one + need_two) {
153 if (!need_one) need_one = strlen(one);
154 if (!need_two) need_one = strlen(two);
156 xp = xmalloc(need_one + need_two + 3);
158 quote_c_style(one, xp + 1, NULL, 1);
159 quote_c_style(two, xp + need_one + 1, NULL, 1);
160 strcpy(xp + need_one + need_two + 1, "\"");
163 need_one = strlen(one);
164 need_two = strlen(two);
165 xp = xmalloc(need_one + need_two + 1);
167 strcpy(xp + need_one, two);
171 static const char *external_diff(void)
173 static const char *external_diff_cmd = NULL;
174 static int done_preparing = 0;
177 return external_diff_cmd;
178 external_diff_cmd = getenv("GIT_EXTERNAL_DIFF");
180 return external_diff_cmd;
183 #define TEMPFILE_PATH_LEN 50
185 static struct diff_tempfile {
186 const char *name; /* filename external diff should read from */
189 char tmp_path[TEMPFILE_PATH_LEN];
192 static int count_lines(const char *data, int size)
194 int count, ch, completely_empty = 1, nl_just_seen = 0;
201 completely_empty = 0;
205 completely_empty = 0;
208 if (completely_empty)
211 count++; /* no trailing newline */
215 static void print_line_count(int count)
225 printf("1,%d", count);
230 static void copy_file(int prefix, const char *data, int size)
232 int ch, nl_just_seen = 1;
244 printf("\n\\ No newline at end of file\n");
247 static void emit_rewrite_diff(const char *name_a,
249 struct diff_filespec *one,
250 struct diff_filespec *two)
253 diff_populate_filespec(one, 0);
254 diff_populate_filespec(two, 0);
255 lc_a = count_lines(one->data, one->size);
256 lc_b = count_lines(two->data, two->size);
257 printf("--- %s\n+++ %s\n@@ -", name_a, name_b);
258 print_line_count(lc_a);
260 print_line_count(lc_b);
263 copy_file('-', one->data, one->size);
265 copy_file('+', two->data, two->size);
268 static int fill_mmfile(mmfile_t *mf, struct diff_filespec *one)
270 if (!DIFF_FILE_VALID(one)) {
271 mf->ptr = (char *)""; /* does not matter */
275 else if (diff_populate_filespec(one, 0))
278 mf->size = one->size;
282 struct emit_callback {
283 struct xdiff_emit_state xm;
284 int nparents, color_diff;
285 const char **label_path;
288 static inline const char *get_color(int diff_use_color, enum color_diff ix)
291 return diff_colors[ix];
295 static void fn_out_consume(void *priv, char *line, unsigned long len)
298 struct emit_callback *ecbdata = priv;
299 const char *set = get_color(ecbdata->color_diff, DIFF_METAINFO);
300 const char *reset = get_color(ecbdata->color_diff, DIFF_RESET);
302 if (ecbdata->label_path[0]) {
303 printf("%s--- %s%s\n", set, ecbdata->label_path[0], reset);
304 printf("%s+++ %s%s\n", set, ecbdata->label_path[1], reset);
305 ecbdata->label_path[0] = ecbdata->label_path[1] = NULL;
308 /* This is not really necessary for now because
309 * this codepath only deals with two-way diffs.
311 for (i = 0; i < len && line[i] == '@'; i++)
313 if (2 <= i && i < len && line[i] == ' ') {
314 ecbdata->nparents = i - 1;
315 set = get_color(ecbdata->color_diff, DIFF_FRAGINFO);
317 else if (len < ecbdata->nparents)
320 int nparents = ecbdata->nparents;
321 int color = DIFF_PLAIN;
322 for (i = 0; i < nparents && len; i++) {
324 color = DIFF_FILE_OLD;
325 else if (line[i] == '+')
326 color = DIFF_FILE_NEW;
328 set = get_color(ecbdata->color_diff, color);
330 if (len > 0 && line[len-1] == '\n')
332 printf("%s%.*s%s\n", set, (int) len, line, reset);
335 static char *pprint_rename(const char *a, const char *b)
340 int pfx_length, sfx_length;
341 int len_a = strlen(a);
342 int len_b = strlen(b);
344 /* Find common prefix */
346 while (*old && *new && *old == *new) {
348 pfx_length = old - a + 1;
353 /* Find common suffix */
357 while (a <= old && b <= new && *old == *new) {
359 sfx_length = len_a - (old - a);
365 * pfx{mid-a => mid-b}sfx
366 * {pfx-a => pfx-b}sfx
367 * pfx{sfx-a => sfx-b}
370 if (pfx_length + sfx_length) {
371 int a_midlen = len_a - pfx_length - sfx_length;
372 int b_midlen = len_b - pfx_length - sfx_length;
373 if (a_midlen < 0) a_midlen = 0;
374 if (b_midlen < 0) b_midlen = 0;
376 name = xmalloc(pfx_length + a_midlen + b_midlen + sfx_length + 7);
377 sprintf(name, "%.*s{%.*s => %.*s}%s",
379 a_midlen, a + pfx_length,
380 b_midlen, b + pfx_length,
381 a + len_a - sfx_length);
384 name = xmalloc(len_a + len_b + 5);
385 sprintf(name, "%s => %s", a, b);
391 struct xdiff_emit_state xm;
395 struct diffstat_file {
397 unsigned is_unmerged:1;
398 unsigned is_binary:1;
399 unsigned is_renamed:1;
400 unsigned int added, deleted;
404 static struct diffstat_file *diffstat_add(struct diffstat_t *diffstat,
408 struct diffstat_file *x;
409 x = xcalloc(sizeof (*x), 1);
410 if (diffstat->nr == diffstat->alloc) {
411 diffstat->alloc = alloc_nr(diffstat->alloc);
412 diffstat->files = xrealloc(diffstat->files,
413 diffstat->alloc * sizeof(x));
415 diffstat->files[diffstat->nr++] = x;
417 x->name = pprint_rename(name_a, name_b);
421 x->name = strdup(name_a);
425 static void diffstat_consume(void *priv, char *line, unsigned long len)
427 struct diffstat_t *diffstat = priv;
428 struct diffstat_file *x = diffstat->files[diffstat->nr - 1];
432 else if (line[0] == '-')
436 static const char pluses[] = "++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++";
437 static const char minuses[]= "----------------------------------------------------------------------";
438 const char mime_boundary_leader[] = "------------";
440 static void show_stats(struct diffstat_t* data)
442 int i, len, add, del, total, adds = 0, dels = 0;
443 int max, max_change = 0, max_len = 0;
444 int total_files = data->nr;
449 for (i = 0; i < data->nr; i++) {
450 struct diffstat_file *file = data->files[i];
452 len = strlen(file->name);
456 if (file->is_binary || file->is_unmerged)
458 if (max_change < file->added + file->deleted)
459 max_change = file->added + file->deleted;
462 for (i = 0; i < data->nr; i++) {
463 const char *prefix = "";
464 char *name = data->files[i]->name;
465 int added = data->files[i]->added;
466 int deleted = data->files[i]->deleted;
468 if (0 < (len = quote_c_style(name, NULL, NULL, 0))) {
469 char *qname = xmalloc(len + 1);
470 quote_c_style(name, qname, NULL, 0);
472 data->files[i]->name = name = qname;
476 * "scale" the filename
487 slash = strchr(name, '/');
494 * scale the add/delete
500 if (data->files[i]->is_binary) {
501 printf(" %s%-*s | Bin\n", prefix, len, name);
502 goto free_diffstat_file;
504 else if (data->files[i]->is_unmerged) {
505 printf(" %s%-*s | Unmerged\n", prefix, len, name);
506 goto free_diffstat_file;
508 else if (!data->files[i]->is_renamed &&
509 (added + deleted == 0)) {
511 goto free_diffstat_file;
520 if (max_change > 0) {
521 total = (total * max + max_change / 2) / max_change;
522 add = (add * max + max_change / 2) / max_change;
525 printf(" %s%-*s |%5d %.*s%.*s\n", prefix,
526 len, name, added + deleted,
527 add, pluses, del, minuses);
529 free(data->files[i]->name);
530 free(data->files[i]);
533 printf(" %d files changed, %d insertions(+), %d deletions(-)\n",
534 total_files, adds, dels);
538 struct xdiff_emit_state xm;
539 const char *filename;
543 static void checkdiff_consume(void *priv, char *line, unsigned long len)
545 struct checkdiff_t *data = priv;
547 if (line[0] == '+') {
552 /* check space before tab */
553 for (i = 1; i < len && (line[i] == ' ' || line[i] == '\t'); i++)
556 if (line[i - 1] == '\t' && spaces)
557 printf("%s:%d: space before tab:%.*s\n",
558 data->filename, data->lineno, (int)len, line);
560 /* check white space at line end */
561 if (line[len - 1] == '\n')
563 if (isspace(line[len - 1]))
564 printf("%s:%d: white space at end: %.*s\n",
565 data->filename, data->lineno, (int)len, line);
566 } else if (line[0] == ' ')
568 else if (line[0] == '@') {
569 char *plus = strchr(line, '+');
571 data->lineno = strtol(plus, NULL, 10);
577 static unsigned char *deflate_it(char *data,
579 unsigned long *result_size)
582 unsigned char *deflated;
585 memset(&stream, 0, sizeof(stream));
586 deflateInit(&stream, zlib_compression_level);
587 bound = deflateBound(&stream, size);
588 deflated = xmalloc(bound);
589 stream.next_out = deflated;
590 stream.avail_out = bound;
592 stream.next_in = (unsigned char *)data;
593 stream.avail_in = size;
594 while (deflate(&stream, Z_FINISH) == Z_OK)
597 *result_size = stream.total_out;
601 static void emit_binary_diff(mmfile_t *one, mmfile_t *two)
607 unsigned long orig_size;
608 unsigned long delta_size;
609 unsigned long deflate_size;
610 unsigned long data_size;
612 printf("GIT binary patch\n");
613 /* We could do deflated delta, or we could do just deflated two,
614 * whichever is smaller.
617 deflated = deflate_it(two->ptr, two->size, &deflate_size);
618 if (one->size && two->size) {
619 delta = diff_delta(one->ptr, one->size,
621 &delta_size, deflate_size);
623 void *to_free = delta;
624 orig_size = delta_size;
625 delta = deflate_it(delta, delta_size, &delta_size);
630 if (delta && delta_size < deflate_size) {
631 printf("delta %lu\n", orig_size);
634 data_size = delta_size;
637 printf("literal %lu\n", two->size);
640 data_size = deflate_size;
643 /* emit data encoded in base85 */
646 int bytes = (52 < data_size) ? 52 : data_size;
650 line[0] = bytes + 'A' - 1;
652 line[0] = bytes - 26 + 'a' - 1;
653 encode_85(line + 1, cp, bytes);
654 cp = (char *) cp + bytes;
661 #define FIRST_FEW_BYTES 8000
662 static int mmfile_is_binary(mmfile_t *mf)
665 if (FIRST_FEW_BYTES < sz)
666 sz = FIRST_FEW_BYTES;
667 if (memchr(mf->ptr, 0, sz))
672 static void builtin_diff(const char *name_a,
674 struct diff_filespec *one,
675 struct diff_filespec *two,
676 const char *xfrm_msg,
677 struct diff_options *o,
678 int complete_rewrite)
683 const char *set = get_color(o->color_diff, DIFF_METAINFO);
684 const char *reset = get_color(o->color_diff, DIFF_RESET);
686 a_one = quote_two("a/", name_a);
687 b_two = quote_two("b/", name_b);
688 lbl[0] = DIFF_FILE_VALID(one) ? a_one : "/dev/null";
689 lbl[1] = DIFF_FILE_VALID(two) ? b_two : "/dev/null";
690 printf("%sdiff --git %s %s%s\n", set, a_one, b_two, reset);
691 if (lbl[0][0] == '/') {
693 printf("%snew file mode %06o%s\n", set, two->mode, reset);
694 if (xfrm_msg && xfrm_msg[0])
695 printf("%s%s%s\n", set, xfrm_msg, reset);
697 else if (lbl[1][0] == '/') {
698 printf("%sdeleted file mode %06o%s\n", set, one->mode, reset);
699 if (xfrm_msg && xfrm_msg[0])
700 printf("%s%s%s\n", set, xfrm_msg, reset);
703 if (one->mode != two->mode) {
704 printf("%sold mode %06o%s\n", set, one->mode, reset);
705 printf("%snew 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 * we do not run diff between different kind
713 if ((one->mode ^ two->mode) & S_IFMT)
714 goto free_ab_and_return;
715 if (complete_rewrite) {
716 emit_rewrite_diff(name_a, name_b, one, two);
717 goto free_ab_and_return;
721 if (fill_mmfile(&mf1, one) < 0 || fill_mmfile(&mf2, two) < 0)
722 die("unable to read files to diff");
724 if (mmfile_is_binary(&mf1) || mmfile_is_binary(&mf2)) {
725 /* Quite common confusing case */
726 if (mf1.size == mf2.size &&
727 !memcmp(mf1.ptr, mf2.ptr, mf1.size))
728 goto free_ab_and_return;
730 emit_binary_diff(&mf1, &mf2);
732 printf("Binary files %s and %s differ\n",
736 /* Crazy xdl interfaces.. */
737 const char *diffopts = getenv("GIT_DIFF_OPTS");
741 struct emit_callback ecbdata;
743 memset(&ecbdata, 0, sizeof(ecbdata));
744 ecbdata.label_path = lbl;
745 ecbdata.color_diff = o->color_diff;
746 xpp.flags = XDF_NEED_MINIMAL | o->xdl_opts;
747 xecfg.ctxlen = o->context;
748 xecfg.flags = XDL_EMIT_FUNCNAMES;
751 else if (!strncmp(diffopts, "--unified=", 10))
752 xecfg.ctxlen = strtoul(diffopts + 10, NULL, 10);
753 else if (!strncmp(diffopts, "-u", 2))
754 xecfg.ctxlen = strtoul(diffopts + 2, NULL, 10);
755 ecb.outf = xdiff_outf;
757 ecbdata.xm.consume = fn_out_consume;
758 xdl_diff(&mf1, &mf2, &xpp, &xecfg, &ecb);
767 static void builtin_diffstat(const char *name_a, const char *name_b,
768 struct diff_filespec *one,
769 struct diff_filespec *two,
770 struct diffstat_t *diffstat,
771 struct diff_options *o,
772 int complete_rewrite)
775 struct diffstat_file *data;
777 data = diffstat_add(diffstat, name_a, name_b);
780 data->is_unmerged = 1;
783 if (complete_rewrite) {
784 diff_populate_filespec(one, 0);
785 diff_populate_filespec(two, 0);
786 data->deleted = count_lines(one->data, one->size);
787 data->added = count_lines(two->data, two->size);
790 if (fill_mmfile(&mf1, one) < 0 || fill_mmfile(&mf2, two) < 0)
791 die("unable to read files to diff");
793 if (mmfile_is_binary(&mf1) || mmfile_is_binary(&mf2))
796 /* Crazy xdl interfaces.. */
801 xpp.flags = XDF_NEED_MINIMAL | o->xdl_opts;
804 ecb.outf = xdiff_outf;
806 xdl_diff(&mf1, &mf2, &xpp, &xecfg, &ecb);
810 static void builtin_checkdiff(const char *name_a, const char *name_b,
811 struct diff_filespec *one,
812 struct diff_filespec *two)
815 struct checkdiff_t data;
820 memset(&data, 0, sizeof(data));
821 data.xm.consume = checkdiff_consume;
822 data.filename = name_b ? name_b : name_a;
825 if (fill_mmfile(&mf1, one) < 0 || fill_mmfile(&mf2, two) < 0)
826 die("unable to read files to diff");
828 if (mmfile_is_binary(&mf2))
831 /* Crazy xdl interfaces.. */
836 xpp.flags = XDF_NEED_MINIMAL;
839 ecb.outf = xdiff_outf;
841 xdl_diff(&mf1, &mf2, &xpp, &xecfg, &ecb);
845 struct diff_filespec *alloc_filespec(const char *path)
847 int namelen = strlen(path);
848 struct diff_filespec *spec = xmalloc(sizeof(*spec) + namelen + 1);
850 memset(spec, 0, sizeof(*spec));
851 spec->path = (char *)(spec + 1);
852 memcpy(spec->path, path, namelen+1);
856 void fill_filespec(struct diff_filespec *spec, const unsigned char *sha1,
860 spec->mode = canon_mode(mode);
861 memcpy(spec->sha1, sha1, 20);
862 spec->sha1_valid = !!memcmp(sha1, null_sha1, 20);
867 * Given a name and sha1 pair, if the dircache tells us the file in
868 * the work tree has that object contents, return true, so that
869 * prepare_temp_file() does not have to inflate and extract.
871 static int work_tree_matches(const char *name, const unsigned char *sha1)
873 struct cache_entry *ce;
877 /* We do not read the cache ourselves here, because the
878 * benchmark with my previous version that always reads cache
879 * shows that it makes things worse for diff-tree comparing
880 * two linux-2.6 kernel trees in an already checked out work
881 * tree. This is because most diff-tree comparisons deal with
882 * only a small number of files, while reading the cache is
883 * expensive for a large project, and its cost outweighs the
884 * savings we get by not inflating the object to a temporary
885 * file. Practically, this code only helps when we are used
886 * by diff-cache --cached, which does read the cache before
893 pos = cache_name_pos(name, len);
896 ce = active_cache[pos];
897 if ((lstat(name, &st) < 0) ||
898 !S_ISREG(st.st_mode) || /* careful! */
899 ce_match_stat(ce, &st, 0) ||
900 memcmp(sha1, ce->sha1, 20))
902 /* we return 1 only when we can stat, it is a regular file,
903 * stat information matches, and sha1 recorded in the cache
904 * matches. I.e. we know the file in the work tree really is
905 * the same as the <name, sha1> pair.
910 static struct sha1_size_cache {
911 unsigned char sha1[20];
914 static int sha1_size_cache_nr, sha1_size_cache_alloc;
916 static struct sha1_size_cache *locate_size_cache(unsigned char *sha1,
921 struct sha1_size_cache *e;
924 last = sha1_size_cache_nr;
925 while (last > first) {
926 int cmp, next = (last + first) >> 1;
927 e = sha1_size_cache[next];
928 cmp = memcmp(e->sha1, sha1, 20);
940 /* insert to make it at "first" */
941 if (sha1_size_cache_alloc <= sha1_size_cache_nr) {
942 sha1_size_cache_alloc = alloc_nr(sha1_size_cache_alloc);
943 sha1_size_cache = xrealloc(sha1_size_cache,
944 sha1_size_cache_alloc *
945 sizeof(*sha1_size_cache));
947 sha1_size_cache_nr++;
948 if (first < sha1_size_cache_nr)
949 memmove(sha1_size_cache + first + 1, sha1_size_cache + first,
950 (sha1_size_cache_nr - first - 1) *
951 sizeof(*sha1_size_cache));
952 e = xmalloc(sizeof(struct sha1_size_cache));
953 sha1_size_cache[first] = e;
954 memcpy(e->sha1, sha1, 20);
960 * While doing rename detection and pickaxe operation, we may need to
961 * grab the data for the blob (or file) for our own in-core comparison.
962 * diff_filespec has data and size fields for this purpose.
964 int diff_populate_filespec(struct diff_filespec *s, int size_only)
967 if (!DIFF_FILE_VALID(s))
968 die("internal error: asking to populate invalid file.");
969 if (S_ISDIR(s->mode))
977 if (!s->sha1_valid ||
978 work_tree_matches(s->path, s->sha1)) {
981 if (lstat(s->path, &st) < 0) {
982 if (errno == ENOENT) {
986 s->data = (char *)"";
991 s->size = st.st_size;
996 if (S_ISLNK(st.st_mode)) {
998 s->data = xmalloc(s->size);
1000 ret = readlink(s->path, s->data, s->size);
1007 fd = open(s->path, O_RDONLY);
1010 s->data = mmap(NULL, s->size, PROT_READ, MAP_PRIVATE, fd, 0);
1012 if (s->data == MAP_FAILED)
1014 s->should_munmap = 1;
1018 struct sha1_size_cache *e;
1021 e = locate_size_cache(s->sha1, 1, 0);
1026 if (!sha1_object_info(s->sha1, type, &s->size))
1027 locate_size_cache(s->sha1, 0, s->size);
1030 s->data = read_sha1_file(s->sha1, type, &s->size);
1037 void diff_free_filespec_data(struct diff_filespec *s)
1041 else if (s->should_munmap)
1042 munmap(s->data, s->size);
1043 s->should_free = s->should_munmap = 0;
1049 static void prep_temp_blob(struct diff_tempfile *temp,
1052 const unsigned char *sha1,
1057 fd = git_mkstemp(temp->tmp_path, TEMPFILE_PATH_LEN, ".diff_XXXXXX");
1059 die("unable to create temp-file");
1060 if (write(fd, blob, size) != size)
1061 die("unable to write temp-file");
1063 temp->name = temp->tmp_path;
1064 strcpy(temp->hex, sha1_to_hex(sha1));
1066 sprintf(temp->mode, "%06o", mode);
1069 static void prepare_temp_file(const char *name,
1070 struct diff_tempfile *temp,
1071 struct diff_filespec *one)
1073 if (!DIFF_FILE_VALID(one)) {
1075 /* A '-' entry produces this for file-2, and
1076 * a '+' entry produces this for file-1.
1078 temp->name = "/dev/null";
1079 strcpy(temp->hex, ".");
1080 strcpy(temp->mode, ".");
1084 if (!one->sha1_valid ||
1085 work_tree_matches(name, one->sha1)) {
1087 if (lstat(name, &st) < 0) {
1088 if (errno == ENOENT)
1089 goto not_a_valid_file;
1090 die("stat(%s): %s", name, strerror(errno));
1092 if (S_ISLNK(st.st_mode)) {
1094 char buf[PATH_MAX + 1]; /* ought to be SYMLINK_MAX */
1095 if (sizeof(buf) <= st.st_size)
1096 die("symlink too long: %s", name);
1097 ret = readlink(name, buf, st.st_size);
1099 die("readlink(%s)", name);
1100 prep_temp_blob(temp, buf, st.st_size,
1102 one->sha1 : null_sha1),
1104 one->mode : S_IFLNK));
1107 /* we can borrow from the file in the work tree */
1109 if (!one->sha1_valid)
1110 strcpy(temp->hex, sha1_to_hex(null_sha1));
1112 strcpy(temp->hex, sha1_to_hex(one->sha1));
1113 /* Even though we may sometimes borrow the
1114 * contents from the work tree, we always want
1115 * one->mode. mode is trustworthy even when
1116 * !(one->sha1_valid), as long as
1117 * DIFF_FILE_VALID(one).
1119 sprintf(temp->mode, "%06o", one->mode);
1124 if (diff_populate_filespec(one, 0))
1125 die("cannot read data blob for %s", one->path);
1126 prep_temp_blob(temp, one->data, one->size,
1127 one->sha1, one->mode);
1131 static void remove_tempfile(void)
1135 for (i = 0; i < 2; i++)
1136 if (diff_temp[i].name == diff_temp[i].tmp_path) {
1137 unlink(diff_temp[i].name);
1138 diff_temp[i].name = NULL;
1142 static void remove_tempfile_on_signal(int signo)
1145 signal(SIGINT, SIG_DFL);
1149 static int spawn_prog(const char *pgm, const char **arg)
1157 die("unable to fork");
1159 execvp(pgm, (char *const*) arg);
1163 while (waitpid(pid, &status, 0) < 0) {
1169 /* Earlier we did not check the exit status because
1170 * diff exits non-zero if files are different, and
1171 * we are not interested in knowing that. It was a
1172 * mistake which made it harder to quit a diff-*
1173 * session that uses the git-apply-patch-script as
1174 * the GIT_EXTERNAL_DIFF. A custom GIT_EXTERNAL_DIFF
1175 * should also exit non-zero only when it wants to
1176 * abort the entire diff-* session.
1178 if (WIFEXITED(status) && !WEXITSTATUS(status))
1183 /* An external diff command takes:
1185 * diff-cmd name infile1 infile1-sha1 infile1-mode \
1186 * infile2 infile2-sha1 infile2-mode [ rename-to ]
1189 static void run_external_diff(const char *pgm,
1192 struct diff_filespec *one,
1193 struct diff_filespec *two,
1194 const char *xfrm_msg,
1195 int complete_rewrite)
1197 const char *spawn_arg[10];
1198 struct diff_tempfile *temp = diff_temp;
1200 static int atexit_asked = 0;
1201 const char *othername;
1202 const char **arg = &spawn_arg[0];
1204 othername = (other? other : name);
1206 prepare_temp_file(name, &temp[0], one);
1207 prepare_temp_file(othername, &temp[1], two);
1208 if (! atexit_asked &&
1209 (temp[0].name == temp[0].tmp_path ||
1210 temp[1].name == temp[1].tmp_path)) {
1212 atexit(remove_tempfile);
1214 signal(SIGINT, remove_tempfile_on_signal);
1220 *arg++ = temp[0].name;
1221 *arg++ = temp[0].hex;
1222 *arg++ = temp[0].mode;
1223 *arg++ = temp[1].name;
1224 *arg++ = temp[1].hex;
1225 *arg++ = temp[1].mode;
1235 retval = spawn_prog(pgm, spawn_arg);
1238 fprintf(stderr, "external diff died, stopping at %s.\n", name);
1243 static void run_diff_cmd(const char *pgm,
1246 struct diff_filespec *one,
1247 struct diff_filespec *two,
1248 const char *xfrm_msg,
1249 struct diff_options *o,
1250 int complete_rewrite)
1253 run_external_diff(pgm, name, other, one, two, xfrm_msg,
1258 builtin_diff(name, other ? other : name,
1259 one, two, xfrm_msg, o, complete_rewrite);
1261 printf("* Unmerged path %s\n", name);
1264 static void diff_fill_sha1_info(struct diff_filespec *one)
1266 if (DIFF_FILE_VALID(one)) {
1267 if (!one->sha1_valid) {
1269 if (lstat(one->path, &st) < 0)
1270 die("stat %s", one->path);
1271 if (index_path(one->sha1, one->path, &st, 0))
1272 die("cannot hash %s\n", one->path);
1276 memset(one->sha1, 0, 20);
1279 static void run_diff(struct diff_filepair *p, struct diff_options *o)
1281 const char *pgm = external_diff();
1282 char msg[PATH_MAX*2+300], *xfrm_msg;
1283 struct diff_filespec *one;
1284 struct diff_filespec *two;
1287 char *name_munged, *other_munged;
1288 int complete_rewrite = 0;
1291 if (DIFF_PAIR_UNMERGED(p)) {
1293 run_diff_cmd(pgm, p->one->path, NULL, NULL, NULL, NULL, o, 0);
1297 name = p->one->path;
1298 other = (strcmp(name, p->two->path) ? p->two->path : NULL);
1299 name_munged = quote_one(name);
1300 other_munged = quote_one(other);
1301 one = p->one; two = p->two;
1303 diff_fill_sha1_info(one);
1304 diff_fill_sha1_info(two);
1307 switch (p->status) {
1308 case DIFF_STATUS_COPIED:
1309 len += snprintf(msg + len, sizeof(msg) - len,
1310 "similarity index %d%%\n"
1313 (int)(0.5 + p->score * 100.0/MAX_SCORE),
1314 name_munged, other_munged);
1316 case DIFF_STATUS_RENAMED:
1317 len += snprintf(msg + len, sizeof(msg) - len,
1318 "similarity index %d%%\n"
1321 (int)(0.5 + p->score * 100.0/MAX_SCORE),
1322 name_munged, other_munged);
1324 case DIFF_STATUS_MODIFIED:
1326 len += snprintf(msg + len, sizeof(msg) - len,
1327 "dissimilarity index %d%%\n",
1328 (int)(0.5 + p->score *
1330 complete_rewrite = 1;
1339 if (memcmp(one->sha1, two->sha1, 20)) {
1340 int abbrev = o->full_index ? 40 : DEFAULT_ABBREV;
1342 len += snprintf(msg + len, sizeof(msg) - len,
1344 abbrev, sha1_to_hex(one->sha1),
1345 abbrev, sha1_to_hex(two->sha1));
1346 if (one->mode == two->mode)
1347 len += snprintf(msg + len, sizeof(msg) - len,
1348 " %06o", one->mode);
1349 len += snprintf(msg + len, sizeof(msg) - len, "\n");
1354 xfrm_msg = len ? msg : NULL;
1357 DIFF_FILE_VALID(one) && DIFF_FILE_VALID(two) &&
1358 (S_IFMT & one->mode) != (S_IFMT & two->mode)) {
1359 /* a filepair that changes between file and symlink
1360 * needs to be split into deletion and creation.
1362 struct diff_filespec *null = alloc_filespec(two->path);
1363 run_diff_cmd(NULL, name, other, one, null, xfrm_msg, o, 0);
1365 null = alloc_filespec(one->path);
1366 run_diff_cmd(NULL, name, other, null, two, xfrm_msg, o, 0);
1370 run_diff_cmd(pgm, name, other, one, two, xfrm_msg, o,
1377 static void run_diffstat(struct diff_filepair *p, struct diff_options *o,
1378 struct diffstat_t *diffstat)
1382 int complete_rewrite = 0;
1384 if (DIFF_PAIR_UNMERGED(p)) {
1386 builtin_diffstat(p->one->path, NULL, NULL, NULL, diffstat, o, 0);
1390 name = p->one->path;
1391 other = (strcmp(name, p->two->path) ? p->two->path : NULL);
1393 diff_fill_sha1_info(p->one);
1394 diff_fill_sha1_info(p->two);
1396 if (p->status == DIFF_STATUS_MODIFIED && p->score)
1397 complete_rewrite = 1;
1398 builtin_diffstat(name, other, p->one, p->two, diffstat, o, complete_rewrite);
1401 static void run_checkdiff(struct diff_filepair *p, struct diff_options *o)
1406 if (DIFF_PAIR_UNMERGED(p)) {
1411 name = p->one->path;
1412 other = (strcmp(name, p->two->path) ? p->two->path : NULL);
1414 diff_fill_sha1_info(p->one);
1415 diff_fill_sha1_info(p->two);
1417 builtin_checkdiff(name, other, p->one, p->two);
1420 void diff_setup(struct diff_options *options)
1422 memset(options, 0, sizeof(*options));
1423 options->line_termination = '\n';
1424 options->break_opt = -1;
1425 options->rename_limit = -1;
1426 options->context = 3;
1427 options->msg_sep = "";
1429 options->change = diff_change;
1430 options->add_remove = diff_addremove;
1431 options->color_diff = diff_use_color_default;
1434 int diff_setup_done(struct diff_options *options)
1436 if ((options->find_copies_harder &&
1437 options->detect_rename != DIFF_DETECT_COPY) ||
1438 (0 <= options->rename_limit && !options->detect_rename))
1441 if (options->output_format & (DIFF_FORMAT_NAME |
1442 DIFF_FORMAT_NAME_STATUS |
1443 DIFF_FORMAT_CHECKDIFF |
1444 DIFF_FORMAT_NO_OUTPUT))
1445 options->output_format &= ~(DIFF_FORMAT_RAW |
1446 DIFF_FORMAT_DIFFSTAT |
1447 DIFF_FORMAT_SUMMARY |
1451 * These cases always need recursive; we do not drop caller-supplied
1452 * recursive bits for other formats here.
1454 if (options->output_format & (DIFF_FORMAT_PATCH |
1455 DIFF_FORMAT_DIFFSTAT |
1456 DIFF_FORMAT_CHECKDIFF))
1457 options->recursive = 1;
1459 * Also pickaxe would not work very well if you do not say recursive
1461 if (options->pickaxe)
1462 options->recursive = 1;
1464 if (options->detect_rename && options->rename_limit < 0)
1465 options->rename_limit = diff_rename_limit_default;
1466 if (options->setup & DIFF_SETUP_USE_CACHE) {
1468 /* read-cache does not die even when it fails
1469 * so it is safe for us to do this here. Also
1470 * it does not smudge active_cache or active_nr
1471 * when it fails, so we do not have to worry about
1472 * cleaning it up ourselves either.
1476 if (options->setup & DIFF_SETUP_USE_SIZE_CACHE)
1478 if (options->abbrev <= 0 || 40 < options->abbrev)
1479 options->abbrev = 40; /* full */
1484 static int opt_arg(const char *arg, int arg_short, const char *arg_long, int *val)
1494 if (c == arg_short) {
1498 if (val && isdigit(c)) {
1500 int n = strtoul(arg, &end, 10);
1511 eq = strchr(arg, '=');
1516 if (!len || strncmp(arg, arg_long, len))
1521 if (!isdigit(*++eq))
1523 n = strtoul(eq, &end, 10);
1531 int diff_opt_parse(struct diff_options *options, const char **av, int ac)
1533 const char *arg = av[0];
1534 if (!strcmp(arg, "-p") || !strcmp(arg, "-u"))
1535 options->output_format |= DIFF_FORMAT_PATCH;
1536 else if (opt_arg(arg, 'U', "unified", &options->context))
1537 options->output_format |= DIFF_FORMAT_PATCH;
1538 else if (!strcmp(arg, "--raw"))
1539 options->output_format |= DIFF_FORMAT_RAW;
1540 else if (!strcmp(arg, "--patch-with-raw")) {
1541 options->output_format |= DIFF_FORMAT_PATCH | DIFF_FORMAT_RAW;
1543 else if (!strcmp(arg, "--stat"))
1544 options->output_format |= DIFF_FORMAT_DIFFSTAT;
1545 else if (!strcmp(arg, "--check"))
1546 options->output_format |= DIFF_FORMAT_CHECKDIFF;
1547 else if (!strcmp(arg, "--summary"))
1548 options->output_format |= DIFF_FORMAT_SUMMARY;
1549 else if (!strcmp(arg, "--patch-with-stat")) {
1550 options->output_format |= DIFF_FORMAT_PATCH | DIFF_FORMAT_DIFFSTAT;
1552 else if (!strcmp(arg, "-z"))
1553 options->line_termination = 0;
1554 else if (!strncmp(arg, "-l", 2))
1555 options->rename_limit = strtoul(arg+2, NULL, 10);
1556 else if (!strcmp(arg, "--full-index"))
1557 options->full_index = 1;
1558 else if (!strcmp(arg, "--binary")) {
1559 options->output_format |= DIFF_FORMAT_PATCH;
1560 options->full_index = options->binary = 1;
1562 else if (!strcmp(arg, "--name-only"))
1563 options->output_format |= DIFF_FORMAT_NAME;
1564 else if (!strcmp(arg, "--name-status"))
1565 options->output_format |= DIFF_FORMAT_NAME_STATUS;
1566 else if (!strcmp(arg, "-R"))
1567 options->reverse_diff = 1;
1568 else if (!strncmp(arg, "-S", 2))
1569 options->pickaxe = arg + 2;
1570 else if (!strcmp(arg, "-s")) {
1571 options->output_format |= DIFF_FORMAT_NO_OUTPUT;
1573 else if (!strncmp(arg, "-O", 2))
1574 options->orderfile = arg + 2;
1575 else if (!strncmp(arg, "--diff-filter=", 14))
1576 options->filter = arg + 14;
1577 else if (!strcmp(arg, "--pickaxe-all"))
1578 options->pickaxe_opts = DIFF_PICKAXE_ALL;
1579 else if (!strcmp(arg, "--pickaxe-regex"))
1580 options->pickaxe_opts = DIFF_PICKAXE_REGEX;
1581 else if (!strncmp(arg, "-B", 2)) {
1582 if ((options->break_opt =
1583 diff_scoreopt_parse(arg)) == -1)
1586 else if (!strncmp(arg, "-M", 2)) {
1587 if ((options->rename_score =
1588 diff_scoreopt_parse(arg)) == -1)
1590 options->detect_rename = DIFF_DETECT_RENAME;
1592 else if (!strncmp(arg, "-C", 2)) {
1593 if ((options->rename_score =
1594 diff_scoreopt_parse(arg)) == -1)
1596 options->detect_rename = DIFF_DETECT_COPY;
1598 else if (!strcmp(arg, "--find-copies-harder"))
1599 options->find_copies_harder = 1;
1600 else if (!strcmp(arg, "--abbrev"))
1601 options->abbrev = DEFAULT_ABBREV;
1602 else if (!strncmp(arg, "--abbrev=", 9)) {
1603 options->abbrev = strtoul(arg + 9, NULL, 10);
1604 if (options->abbrev < MINIMUM_ABBREV)
1605 options->abbrev = MINIMUM_ABBREV;
1606 else if (40 < options->abbrev)
1607 options->abbrev = 40;
1609 else if (!strcmp(arg, "--color"))
1610 options->color_diff = 1;
1611 else if (!strcmp(arg, "-w") || !strcmp(arg, "--ignore-all-space"))
1612 options->xdl_opts |= XDF_IGNORE_WHITESPACE;
1613 else if (!strcmp(arg, "-b") || !strcmp(arg, "--ignore-space-change"))
1614 options->xdl_opts |= XDF_IGNORE_WHITESPACE_CHANGE;
1620 static int parse_num(const char **cp_p)
1622 unsigned long num, scale;
1624 const char *cp = *cp_p;
1631 if ( !dot && ch == '.' ) {
1634 } else if ( ch == '%' ) {
1635 scale = dot ? scale*100 : 100;
1636 cp++; /* % is always at the end */
1638 } else if ( ch >= '0' && ch <= '9' ) {
1639 if ( scale < 100000 ) {
1641 num = (num*10) + (ch-'0');
1650 /* user says num divided by scale and we say internally that
1651 * is MAX_SCORE * num / scale.
1653 return (num >= scale) ? MAX_SCORE : (MAX_SCORE * num / scale);
1656 int diff_scoreopt_parse(const char *opt)
1658 int opt1, opt2, cmd;
1663 if (cmd != 'M' && cmd != 'C' && cmd != 'B')
1664 return -1; /* that is not a -M, -C nor -B option */
1666 opt1 = parse_num(&opt);
1672 else if (*opt != '/')
1673 return -1; /* we expect -B80/99 or -B80 */
1676 opt2 = parse_num(&opt);
1681 return opt1 | (opt2 << 16);
1684 struct diff_queue_struct diff_queued_diff;
1686 void diff_q(struct diff_queue_struct *queue, struct diff_filepair *dp)
1688 if (queue->alloc <= queue->nr) {
1689 queue->alloc = alloc_nr(queue->alloc);
1690 queue->queue = xrealloc(queue->queue,
1691 sizeof(dp) * queue->alloc);
1693 queue->queue[queue->nr++] = dp;
1696 struct diff_filepair *diff_queue(struct diff_queue_struct *queue,
1697 struct diff_filespec *one,
1698 struct diff_filespec *two)
1700 struct diff_filepair *dp = xmalloc(sizeof(*dp));
1705 dp->source_stays = 0;
1706 dp->broken_pair = 0;
1712 void diff_free_filepair(struct diff_filepair *p)
1714 diff_free_filespec_data(p->one);
1715 diff_free_filespec_data(p->two);
1721 /* This is different from find_unique_abbrev() in that
1722 * it stuffs the result with dots for alignment.
1724 const char *diff_unique_abbrev(const unsigned char *sha1, int len)
1729 return sha1_to_hex(sha1);
1731 abbrev = find_unique_abbrev(sha1, len);
1733 return sha1_to_hex(sha1);
1734 abblen = strlen(abbrev);
1736 static char hex[41];
1737 if (len < abblen && abblen <= len + 2)
1738 sprintf(hex, "%s%.*s", abbrev, len+3-abblen, "..");
1740 sprintf(hex, "%s...", abbrev);
1743 return sha1_to_hex(sha1);
1746 static void diff_flush_raw(struct diff_filepair *p,
1747 struct diff_options *options)
1751 int abbrev = options->abbrev;
1752 const char *path_one, *path_two;
1753 int inter_name_termination = '\t';
1754 int line_termination = options->line_termination;
1756 if (!line_termination)
1757 inter_name_termination = 0;
1759 path_one = p->one->path;
1760 path_two = p->two->path;
1761 if (line_termination) {
1762 path_one = quote_one(path_one);
1763 path_two = quote_one(path_two);
1767 sprintf(status, "%c%03d", p->status,
1768 (int)(0.5 + p->score * 100.0/MAX_SCORE));
1770 status[0] = p->status;
1773 switch (p->status) {
1774 case DIFF_STATUS_COPIED:
1775 case DIFF_STATUS_RENAMED:
1778 case DIFF_STATUS_ADDED:
1779 case DIFF_STATUS_DELETED:
1786 if (!(options->output_format & DIFF_FORMAT_NAME_STATUS)) {
1787 printf(":%06o %06o %s ",
1788 p->one->mode, p->two->mode,
1789 diff_unique_abbrev(p->one->sha1, abbrev));
1791 diff_unique_abbrev(p->two->sha1, abbrev));
1793 printf("%s%c%s", status, inter_name_termination, path_one);
1795 printf("%c%s", inter_name_termination, path_two);
1796 putchar(line_termination);
1797 if (path_one != p->one->path)
1798 free((void*)path_one);
1799 if (path_two != p->two->path)
1800 free((void*)path_two);
1803 static void diff_flush_name(struct diff_filepair *p, int line_termination)
1805 char *path = p->two->path;
1807 if (line_termination)
1808 path = quote_one(p->two->path);
1809 printf("%s%c", path, line_termination);
1810 if (p->two->path != path)
1814 int diff_unmodified_pair(struct diff_filepair *p)
1816 /* This function is written stricter than necessary to support
1817 * the currently implemented transformers, but the idea is to
1818 * let transformers to produce diff_filepairs any way they want,
1819 * and filter and clean them up here before producing the output.
1821 struct diff_filespec *one, *two;
1823 if (DIFF_PAIR_UNMERGED(p))
1824 return 0; /* unmerged is interesting */
1829 /* deletion, addition, mode or type change
1830 * and rename are all interesting.
1832 if (DIFF_FILE_VALID(one) != DIFF_FILE_VALID(two) ||
1833 DIFF_PAIR_MODE_CHANGED(p) ||
1834 strcmp(one->path, two->path))
1837 /* both are valid and point at the same path. that is, we are
1838 * dealing with a change.
1840 if (one->sha1_valid && two->sha1_valid &&
1841 !memcmp(one->sha1, two->sha1, sizeof(one->sha1)))
1842 return 1; /* no change */
1843 if (!one->sha1_valid && !two->sha1_valid)
1844 return 1; /* both look at the same file on the filesystem. */
1848 static void diff_flush_patch(struct diff_filepair *p, struct diff_options *o)
1850 if (diff_unmodified_pair(p))
1853 if ((DIFF_FILE_VALID(p->one) && S_ISDIR(p->one->mode)) ||
1854 (DIFF_FILE_VALID(p->two) && S_ISDIR(p->two->mode)))
1855 return; /* no tree diffs in patch format */
1860 static void diff_flush_stat(struct diff_filepair *p, struct diff_options *o,
1861 struct diffstat_t *diffstat)
1863 if (diff_unmodified_pair(p))
1866 if ((DIFF_FILE_VALID(p->one) && S_ISDIR(p->one->mode)) ||
1867 (DIFF_FILE_VALID(p->two) && S_ISDIR(p->two->mode)))
1868 return; /* no tree diffs in patch format */
1870 run_diffstat(p, o, diffstat);
1873 static void diff_flush_checkdiff(struct diff_filepair *p,
1874 struct diff_options *o)
1876 if (diff_unmodified_pair(p))
1879 if ((DIFF_FILE_VALID(p->one) && S_ISDIR(p->one->mode)) ||
1880 (DIFF_FILE_VALID(p->two) && S_ISDIR(p->two->mode)))
1881 return; /* no tree diffs in patch format */
1883 run_checkdiff(p, o);
1886 int diff_queue_is_empty(void)
1888 struct diff_queue_struct *q = &diff_queued_diff;
1890 for (i = 0; i < q->nr; i++)
1891 if (!diff_unmodified_pair(q->queue[i]))
1897 void diff_debug_filespec(struct diff_filespec *s, int x, const char *one)
1899 fprintf(stderr, "queue[%d] %s (%s) %s %06o %s\n",
1902 DIFF_FILE_VALID(s) ? "valid" : "invalid",
1904 s->sha1_valid ? sha1_to_hex(s->sha1) : "");
1905 fprintf(stderr, "queue[%d] %s size %lu flags %d\n",
1907 s->size, s->xfrm_flags);
1910 void diff_debug_filepair(const struct diff_filepair *p, int i)
1912 diff_debug_filespec(p->one, i, "one");
1913 diff_debug_filespec(p->two, i, "two");
1914 fprintf(stderr, "score %d, status %c stays %d broken %d\n",
1915 p->score, p->status ? p->status : '?',
1916 p->source_stays, p->broken_pair);
1919 void diff_debug_queue(const char *msg, struct diff_queue_struct *q)
1923 fprintf(stderr, "%s\n", msg);
1924 fprintf(stderr, "q->nr = %d\n", q->nr);
1925 for (i = 0; i < q->nr; i++) {
1926 struct diff_filepair *p = q->queue[i];
1927 diff_debug_filepair(p, i);
1932 static void diff_resolve_rename_copy(void)
1935 struct diff_filepair *p, *pp;
1936 struct diff_queue_struct *q = &diff_queued_diff;
1938 diff_debug_queue("resolve-rename-copy", q);
1940 for (i = 0; i < q->nr; i++) {
1942 p->status = 0; /* undecided */
1943 if (DIFF_PAIR_UNMERGED(p))
1944 p->status = DIFF_STATUS_UNMERGED;
1945 else if (!DIFF_FILE_VALID(p->one))
1946 p->status = DIFF_STATUS_ADDED;
1947 else if (!DIFF_FILE_VALID(p->two))
1948 p->status = DIFF_STATUS_DELETED;
1949 else if (DIFF_PAIR_TYPE_CHANGED(p))
1950 p->status = DIFF_STATUS_TYPE_CHANGED;
1952 /* from this point on, we are dealing with a pair
1953 * whose both sides are valid and of the same type, i.e.
1954 * either in-place edit or rename/copy edit.
1956 else if (DIFF_PAIR_RENAME(p)) {
1957 if (p->source_stays) {
1958 p->status = DIFF_STATUS_COPIED;
1961 /* See if there is some other filepair that
1962 * copies from the same source as us. If so
1963 * we are a copy. Otherwise we are either a
1964 * copy if the path stays, or a rename if it
1965 * does not, but we already handled "stays" case.
1967 for (j = i + 1; j < q->nr; j++) {
1969 if (strcmp(pp->one->path, p->one->path))
1970 continue; /* not us */
1971 if (!DIFF_PAIR_RENAME(pp))
1972 continue; /* not a rename/copy */
1973 /* pp is a rename/copy from the same source */
1974 p->status = DIFF_STATUS_COPIED;
1978 p->status = DIFF_STATUS_RENAMED;
1980 else if (memcmp(p->one->sha1, p->two->sha1, 20) ||
1981 p->one->mode != p->two->mode)
1982 p->status = DIFF_STATUS_MODIFIED;
1984 /* This is a "no-change" entry and should not
1985 * happen anymore, but prepare for broken callers.
1987 error("feeding unmodified %s to diffcore",
1989 p->status = DIFF_STATUS_UNKNOWN;
1992 diff_debug_queue("resolve-rename-copy done", q);
1995 static int check_pair_status(struct diff_filepair *p)
1997 switch (p->status) {
1998 case DIFF_STATUS_UNKNOWN:
2001 die("internal error in diff-resolve-rename-copy");
2007 static void flush_one_pair(struct diff_filepair *p, struct diff_options *opt)
2009 int fmt = opt->output_format;
2011 if (fmt & DIFF_FORMAT_CHECKDIFF)
2012 diff_flush_checkdiff(p, opt);
2013 else if (fmt & (DIFF_FORMAT_RAW | DIFF_FORMAT_NAME_STATUS))
2014 diff_flush_raw(p, opt);
2015 else if (fmt & DIFF_FORMAT_NAME)
2016 diff_flush_name(p, opt->line_termination);
2019 static void show_file_mode_name(const char *newdelete, struct diff_filespec *fs)
2022 printf(" %s mode %06o %s\n", newdelete, fs->mode, fs->path);
2024 printf(" %s %s\n", newdelete, fs->path);
2028 static void show_mode_change(struct diff_filepair *p, int show_name)
2030 if (p->one->mode && p->two->mode && p->one->mode != p->two->mode) {
2032 printf(" mode change %06o => %06o %s\n",
2033 p->one->mode, p->two->mode, p->two->path);
2035 printf(" mode change %06o => %06o\n",
2036 p->one->mode, p->two->mode);
2040 static void show_rename_copy(const char *renamecopy, struct diff_filepair *p)
2042 const char *old, *new;
2044 /* Find common prefix */
2048 const char *slash_old, *slash_new;
2049 slash_old = strchr(old, '/');
2050 slash_new = strchr(new, '/');
2053 slash_old - old != slash_new - new ||
2054 memcmp(old, new, slash_new - new))
2056 old = slash_old + 1;
2057 new = slash_new + 1;
2059 /* p->one->path thru old is the common prefix, and old and new
2060 * through the end of names are renames
2062 if (old != p->one->path)
2063 printf(" %s %.*s{%s => %s} (%d%%)\n", renamecopy,
2064 (int)(old - p->one->path), p->one->path,
2065 old, new, (int)(0.5 + p->score * 100.0/MAX_SCORE));
2067 printf(" %s %s => %s (%d%%)\n", renamecopy,
2068 p->one->path, p->two->path,
2069 (int)(0.5 + p->score * 100.0/MAX_SCORE));
2070 show_mode_change(p, 0);
2073 static void diff_summary(struct diff_filepair *p)
2076 case DIFF_STATUS_DELETED:
2077 show_file_mode_name("delete", p->one);
2079 case DIFF_STATUS_ADDED:
2080 show_file_mode_name("create", p->two);
2082 case DIFF_STATUS_COPIED:
2083 show_rename_copy("copy", p);
2085 case DIFF_STATUS_RENAMED:
2086 show_rename_copy("rename", p);
2090 printf(" rewrite %s (%d%%)\n", p->two->path,
2091 (int)(0.5 + p->score * 100.0/MAX_SCORE));
2092 show_mode_change(p, 0);
2093 } else show_mode_change(p, 1);
2099 struct xdiff_emit_state xm;
2104 static int remove_space(char *line, int len)
2110 for (i = 0; i < len; i++)
2111 if (!isspace((c = line[i])))
2117 static void patch_id_consume(void *priv, char *line, unsigned long len)
2119 struct patch_id_t *data = priv;
2122 /* Ignore line numbers when computing the SHA1 of the patch */
2123 if (!strncmp(line, "@@ -", 4))
2126 new_len = remove_space(line, len);
2128 SHA1_Update(data->ctx, line, new_len);
2129 data->patchlen += new_len;
2132 /* returns 0 upon success, and writes result into sha1 */
2133 static int diff_get_patch_id(struct diff_options *options, unsigned char *sha1)
2135 struct diff_queue_struct *q = &diff_queued_diff;
2138 struct patch_id_t data;
2139 char buffer[PATH_MAX * 4 + 20];
2142 memset(&data, 0, sizeof(struct patch_id_t));
2144 data.xm.consume = patch_id_consume;
2146 for (i = 0; i < q->nr; i++) {
2151 struct diff_filepair *p = q->queue[i];
2155 return error("internal diff status error");
2156 if (p->status == DIFF_STATUS_UNKNOWN)
2158 if (diff_unmodified_pair(p))
2160 if ((DIFF_FILE_VALID(p->one) && S_ISDIR(p->one->mode)) ||
2161 (DIFF_FILE_VALID(p->two) && S_ISDIR(p->two->mode)))
2163 if (DIFF_PAIR_UNMERGED(p))
2166 diff_fill_sha1_info(p->one);
2167 diff_fill_sha1_info(p->two);
2168 if (fill_mmfile(&mf1, p->one) < 0 ||
2169 fill_mmfile(&mf2, p->two) < 0)
2170 return error("unable to read files to diff");
2172 /* Maybe hash p->two? into the patch id? */
2173 if (mmfile_is_binary(&mf2))
2176 len1 = remove_space(p->one->path, strlen(p->one->path));
2177 len2 = remove_space(p->two->path, strlen(p->two->path));
2178 if (p->one->mode == 0)
2179 len1 = snprintf(buffer, sizeof(buffer),
2180 "diff--gita/%.*sb/%.*s"
2187 len2, p->two->path);
2188 else if (p->two->mode == 0)
2189 len1 = snprintf(buffer, sizeof(buffer),
2190 "diff--gita/%.*sb/%.*s"
2191 "deletedfilemode%06o"
2197 len1, p->one->path);
2199 len1 = snprintf(buffer, sizeof(buffer),
2200 "diff--gita/%.*sb/%.*s"
2206 len2, p->two->path);
2207 SHA1_Update(&ctx, buffer, len1);
2209 xpp.flags = XDF_NEED_MINIMAL;
2211 xecfg.flags = XDL_EMIT_FUNCNAMES;
2212 ecb.outf = xdiff_outf;
2214 xdl_diff(&mf1, &mf2, &xpp, &xecfg, &ecb);
2217 SHA1_Final(sha1, &ctx);
2221 int diff_flush_patch_id(struct diff_options *options, unsigned char *sha1)
2223 struct diff_queue_struct *q = &diff_queued_diff;
2225 int result = diff_get_patch_id(options, sha1);
2227 for (i = 0; i < q->nr; i++)
2228 diff_free_filepair(q->queue[i]);
2232 q->nr = q->alloc = 0;
2237 static int is_summary_empty(const struct diff_queue_struct *q)
2241 for (i = 0; i < q->nr; i++) {
2242 const struct diff_filepair *p = q->queue[i];
2244 switch (p->status) {
2245 case DIFF_STATUS_DELETED:
2246 case DIFF_STATUS_ADDED:
2247 case DIFF_STATUS_COPIED:
2248 case DIFF_STATUS_RENAMED:
2253 if (p->one->mode && p->two->mode &&
2254 p->one->mode != p->two->mode)
2262 void diff_flush(struct diff_options *options)
2264 struct diff_queue_struct *q = &diff_queued_diff;
2265 int i, output_format = options->output_format;
2269 * Order: raw, stat, summary, patch
2270 * or: name/name-status/checkdiff (other bits clear)
2275 if (output_format & (DIFF_FORMAT_RAW |
2277 DIFF_FORMAT_NAME_STATUS |
2278 DIFF_FORMAT_CHECKDIFF)) {
2279 for (i = 0; i < q->nr; i++) {
2280 struct diff_filepair *p = q->queue[i];
2281 if (check_pair_status(p))
2282 flush_one_pair(p, options);
2287 if (output_format & DIFF_FORMAT_DIFFSTAT) {
2288 struct diffstat_t diffstat;
2290 memset(&diffstat, 0, sizeof(struct diffstat_t));
2291 diffstat.xm.consume = diffstat_consume;
2292 for (i = 0; i < q->nr; i++) {
2293 struct diff_filepair *p = q->queue[i];
2294 if (check_pair_status(p))
2295 diff_flush_stat(p, options, &diffstat);
2297 show_stats(&diffstat);
2301 if (output_format & DIFF_FORMAT_SUMMARY && !is_summary_empty(q)) {
2302 for (i = 0; i < q->nr; i++)
2303 diff_summary(q->queue[i]);
2307 if (output_format & DIFF_FORMAT_PATCH) {
2309 if (options->stat_sep) {
2310 /* attach patch instead of inline */
2311 fputs(options->stat_sep, stdout);
2313 putchar(options->line_termination);
2317 for (i = 0; i < q->nr; i++) {
2318 struct diff_filepair *p = q->queue[i];
2319 if (check_pair_status(p))
2320 diff_flush_patch(p, options);
2324 for (i = 0; i < q->nr; i++)
2325 diff_free_filepair(q->queue[i]);
2329 q->nr = q->alloc = 0;
2332 static void diffcore_apply_filter(const char *filter)
2335 struct diff_queue_struct *q = &diff_queued_diff;
2336 struct diff_queue_struct outq;
2338 outq.nr = outq.alloc = 0;
2343 if (strchr(filter, DIFF_STATUS_FILTER_AON)) {
2345 for (i = found = 0; !found && i < q->nr; i++) {
2346 struct diff_filepair *p = q->queue[i];
2347 if (((p->status == DIFF_STATUS_MODIFIED) &&
2349 strchr(filter, DIFF_STATUS_FILTER_BROKEN)) ||
2351 strchr(filter, DIFF_STATUS_MODIFIED)))) ||
2352 ((p->status != DIFF_STATUS_MODIFIED) &&
2353 strchr(filter, p->status)))
2359 /* otherwise we will clear the whole queue
2360 * by copying the empty outq at the end of this
2361 * function, but first clear the current entries
2364 for (i = 0; i < q->nr; i++)
2365 diff_free_filepair(q->queue[i]);
2368 /* Only the matching ones */
2369 for (i = 0; i < q->nr; i++) {
2370 struct diff_filepair *p = q->queue[i];
2372 if (((p->status == DIFF_STATUS_MODIFIED) &&
2374 strchr(filter, DIFF_STATUS_FILTER_BROKEN)) ||
2376 strchr(filter, DIFF_STATUS_MODIFIED)))) ||
2377 ((p->status != DIFF_STATUS_MODIFIED) &&
2378 strchr(filter, p->status)))
2381 diff_free_filepair(p);
2388 void diffcore_std(struct diff_options *options)
2390 if (options->break_opt != -1)
2391 diffcore_break(options->break_opt);
2392 if (options->detect_rename)
2393 diffcore_rename(options);
2394 if (options->break_opt != -1)
2395 diffcore_merge_broken();
2396 if (options->pickaxe)
2397 diffcore_pickaxe(options->pickaxe, options->pickaxe_opts);
2398 if (options->orderfile)
2399 diffcore_order(options->orderfile);
2400 diff_resolve_rename_copy();
2401 diffcore_apply_filter(options->filter);
2405 void diffcore_std_no_resolve(struct diff_options *options)
2407 if (options->pickaxe)
2408 diffcore_pickaxe(options->pickaxe, options->pickaxe_opts);
2409 if (options->orderfile)
2410 diffcore_order(options->orderfile);
2411 diffcore_apply_filter(options->filter);
2414 void diff_addremove(struct diff_options *options,
2415 int addremove, unsigned mode,
2416 const unsigned char *sha1,
2417 const char *base, const char *path)
2419 char concatpath[PATH_MAX];
2420 struct diff_filespec *one, *two;
2422 /* This may look odd, but it is a preparation for
2423 * feeding "there are unchanged files which should
2424 * not produce diffs, but when you are doing copy
2425 * detection you would need them, so here they are"
2426 * entries to the diff-core. They will be prefixed
2427 * with something like '=' or '*' (I haven't decided
2428 * which but should not make any difference).
2429 * Feeding the same new and old to diff_change()
2430 * also has the same effect.
2431 * Before the final output happens, they are pruned after
2432 * merged into rename/copy pairs as appropriate.
2434 if (options->reverse_diff)
2435 addremove = (addremove == '+' ? '-' :
2436 addremove == '-' ? '+' : addremove);
2438 if (!path) path = "";
2439 sprintf(concatpath, "%s%s", base, path);
2440 one = alloc_filespec(concatpath);
2441 two = alloc_filespec(concatpath);
2443 if (addremove != '+')
2444 fill_filespec(one, sha1, mode);
2445 if (addremove != '-')
2446 fill_filespec(two, sha1, mode);
2448 diff_queue(&diff_queued_diff, one, two);
2451 void diff_change(struct diff_options *options,
2452 unsigned old_mode, unsigned new_mode,
2453 const unsigned char *old_sha1,
2454 const unsigned char *new_sha1,
2455 const char *base, const char *path)
2457 char concatpath[PATH_MAX];
2458 struct diff_filespec *one, *two;
2460 if (options->reverse_diff) {
2462 const unsigned char *tmp_c;
2463 tmp = old_mode; old_mode = new_mode; new_mode = tmp;
2464 tmp_c = old_sha1; old_sha1 = new_sha1; new_sha1 = tmp_c;
2466 if (!path) path = "";
2467 sprintf(concatpath, "%s%s", base, path);
2468 one = alloc_filespec(concatpath);
2469 two = alloc_filespec(concatpath);
2470 fill_filespec(one, old_sha1, old_mode);
2471 fill_filespec(two, new_sha1, new_mode);
2473 diff_queue(&diff_queued_diff, one, two);
2476 void diff_unmerge(struct diff_options *options,
2479 struct diff_filespec *one, *two;
2480 one = alloc_filespec(path);
2481 two = alloc_filespec(path);
2482 diff_queue(&diff_queued_diff, one, two);