2 * Copyright (C) 2005 Junio C Hamano
12 #include "xdiff-interface.h"
14 static int use_size_cache;
16 int diff_rename_limit_default = -1;
18 int git_diff_config(const char *var, const char *value)
20 if (!strcmp(var, "diff.renamelimit")) {
21 diff_rename_limit_default = git_config_int(var, value);
25 return git_default_config(var, value);
28 static char *quote_one(const char *str)
35 needlen = quote_c_style(str, NULL, NULL, 0);
38 xp = xmalloc(needlen + 1);
39 quote_c_style(str, xp, NULL, 0);
43 static char *quote_two(const char *one, const char *two)
45 int need_one = quote_c_style(one, NULL, NULL, 1);
46 int need_two = quote_c_style(two, NULL, NULL, 1);
49 if (need_one + need_two) {
50 if (!need_one) need_one = strlen(one);
51 if (!need_two) need_one = strlen(two);
53 xp = xmalloc(need_one + need_two + 3);
55 quote_c_style(one, xp + 1, NULL, 1);
56 quote_c_style(two, xp + need_one + 1, NULL, 1);
57 strcpy(xp + need_one + need_two + 1, "\"");
60 need_one = strlen(one);
61 need_two = strlen(two);
62 xp = xmalloc(need_one + need_two + 1);
64 strcpy(xp + need_one, two);
68 static const char *external_diff(void)
70 static const char *external_diff_cmd = NULL;
71 static int done_preparing = 0;
74 return external_diff_cmd;
75 external_diff_cmd = getenv("GIT_EXTERNAL_DIFF");
77 return external_diff_cmd;
80 #define TEMPFILE_PATH_LEN 50
82 static struct diff_tempfile {
83 const char *name; /* filename external diff should read from */
86 char tmp_path[TEMPFILE_PATH_LEN];
89 static int count_lines(const char *data, int size)
91 int count, ch, completely_empty = 1, nl_just_seen = 0;
102 completely_empty = 0;
105 if (completely_empty)
108 count++; /* no trailing newline */
112 static void print_line_count(int count)
122 printf("1,%d", count);
127 static void copy_file(int prefix, const char *data, int size)
129 int ch, nl_just_seen = 1;
141 printf("\n\\ No newline at end of file\n");
144 static void emit_rewrite_diff(const char *name_a,
146 struct diff_filespec *one,
147 struct diff_filespec *two)
150 diff_populate_filespec(one, 0);
151 diff_populate_filespec(two, 0);
152 lc_a = count_lines(one->data, one->size);
153 lc_b = count_lines(two->data, two->size);
154 printf("--- %s\n+++ %s\n@@ -", name_a, name_b);
155 print_line_count(lc_a);
157 print_line_count(lc_b);
160 copy_file('-', one->data, one->size);
162 copy_file('+', two->data, two->size);
165 static int fill_mmfile(mmfile_t *mf, struct diff_filespec *one)
167 if (!DIFF_FILE_VALID(one)) {
168 mf->ptr = ""; /* does not matter */
172 else if (diff_populate_filespec(one, 0))
175 mf->size = one->size;
179 struct emit_callback {
180 const char **label_path;
183 static int fn_out(void *priv, mmbuffer_t *mb, int nbuf)
186 struct emit_callback *ecbdata = priv;
188 if (ecbdata->label_path[0]) {
189 printf("--- %s\n", ecbdata->label_path[0]);
190 printf("+++ %s\n", ecbdata->label_path[1]);
191 ecbdata->label_path[0] = ecbdata->label_path[1] = NULL;
193 for (i = 0; i < nbuf; i++)
194 if (!fwrite(mb[i].ptr, mb[i].size, 1, stdout))
199 static char *pprint_rename(const char *a, const char *b)
204 int pfx_length, sfx_length;
205 int len_a = strlen(a);
206 int len_b = strlen(b);
208 /* Find common prefix */
210 while (*old && *new && *old == *new) {
212 pfx_length = old - a + 1;
217 /* Find common suffix */
221 while (a <= old && b <= new && *old == *new) {
223 sfx_length = len_a - (old - a);
229 * pfx{mid-a => mid-b}sfx
230 * {pfx-a => pfx-b}sfx
231 * pfx{sfx-a => sfx-b}
234 if (pfx_length + sfx_length) {
235 int a_midlen = len_a - pfx_length - sfx_length;
236 int b_midlen = len_b - pfx_length - sfx_length;
237 if (a_midlen < 0) a_midlen = 0;
238 if (b_midlen < 0) b_midlen = 0;
240 name = xmalloc(pfx_length + a_midlen + b_midlen + sfx_length + 7);
241 sprintf(name, "%.*s{%.*s => %.*s}%s",
243 a_midlen, a + pfx_length,
244 b_midlen, b + pfx_length,
245 a + len_a - sfx_length);
248 name = xmalloc(len_a + len_b + 5);
249 sprintf(name, "%s => %s", a, b);
255 struct xdiff_emit_state xm;
259 struct diffstat_file {
261 unsigned is_unmerged:1;
262 unsigned is_binary:1;
263 unsigned is_renamed:1;
264 unsigned int added, deleted;
268 static struct diffstat_file *diffstat_add(struct diffstat_t *diffstat,
272 struct diffstat_file *x;
273 x = xcalloc(sizeof (*x), 1);
274 if (diffstat->nr == diffstat->alloc) {
275 diffstat->alloc = alloc_nr(diffstat->alloc);
276 diffstat->files = xrealloc(diffstat->files,
277 diffstat->alloc * sizeof(x));
279 diffstat->files[diffstat->nr++] = x;
281 x->name = pprint_rename(name_a, name_b);
285 x->name = strdup(name_a);
289 static void diffstat_consume(void *priv, char *line, unsigned long len)
291 struct diffstat_t *diffstat = priv;
292 struct diffstat_file *x = diffstat->files[diffstat->nr - 1];
296 else if (line[0] == '-')
300 static const char pluses[] = "++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++";
301 static const char minuses[]= "----------------------------------------------------------------------";
302 const char mime_boundary_leader[] = "------------";
304 static void show_stats(struct diffstat_t* data)
306 int i, len, add, del, total, adds = 0, dels = 0;
307 int max, max_change = 0, max_len = 0;
308 int total_files = data->nr;
313 for (i = 0; i < data->nr; i++) {
314 struct diffstat_file *file = data->files[i];
316 len = strlen(file->name);
320 if (file->is_binary || file->is_unmerged)
322 if (max_change < file->added + file->deleted)
323 max_change = file->added + file->deleted;
326 for (i = 0; i < data->nr; i++) {
328 char *name = data->files[i]->name;
329 int added = data->files[i]->added;
330 int deleted = data->files[i]->deleted;
332 if (0 < (len = quote_c_style(name, NULL, NULL, 0))) {
333 char *qname = xmalloc(len + 1);
334 quote_c_style(name, qname, NULL, 0);
336 data->files[i]->name = name = qname;
340 * "scale" the filename
351 slash = strchr(name, '/');
358 * scale the add/delete
364 if (data->files[i]->is_binary) {
365 printf(" %s%-*s | Bin\n", prefix, len, name);
366 goto free_diffstat_file;
368 else if (data->files[i]->is_unmerged) {
369 printf(" %s%-*s | Unmerged\n", prefix, len, name);
370 goto free_diffstat_file;
372 else if (!data->files[i]->is_renamed &&
373 (added + deleted == 0)) {
375 goto free_diffstat_file;
384 if (max_change > 0) {
385 total = (total * max + max_change / 2) / max_change;
386 add = (add * max + max_change / 2) / max_change;
389 printf(" %s%-*s |%5d %.*s%.*s\n", prefix,
390 len, name, added + deleted,
391 add, pluses, del, minuses);
393 free(data->files[i]->name);
394 free(data->files[i]);
397 printf(" %d files changed, %d insertions(+), %d deletions(-)\n",
398 total_files, adds, dels);
402 struct xdiff_emit_state xm;
403 const char *filename;
407 static void checkdiff_consume(void *priv, char *line, unsigned long len)
409 struct checkdiff_t *data = priv;
411 if (line[0] == '+') {
416 /* check space before tab */
417 for (i = 1; i < len && (line[i] == ' ' || line[i] == '\t'); i++)
420 if (line[i - 1] == '\t' && spaces)
421 printf("%s:%d: space before tab:%.*s\n",
422 data->filename, data->lineno, (int)len, line);
424 /* check white space at line end */
425 if (line[len - 1] == '\n')
427 if (isspace(line[len - 1]))
428 printf("%s:%d: white space at end: %.*s\n",
429 data->filename, data->lineno, (int)len, line);
430 } else if (line[0] == ' ')
432 else if (line[0] == '@') {
433 char *plus = strchr(line, '+');
435 data->lineno = strtol(plus, NULL, 10);
441 static unsigned char *deflate_it(char *data,
443 unsigned long *result_size)
446 unsigned char *deflated;
449 memset(&stream, 0, sizeof(stream));
450 deflateInit(&stream, Z_BEST_COMPRESSION);
451 bound = deflateBound(&stream, size);
452 deflated = xmalloc(bound);
453 stream.next_out = deflated;
454 stream.avail_out = bound;
456 stream.next_in = (unsigned char *)data;
457 stream.avail_in = size;
458 while (deflate(&stream, Z_FINISH) == Z_OK)
461 *result_size = stream.total_out;
465 static void emit_binary_diff(mmfile_t *one, mmfile_t *two)
471 unsigned long orig_size;
472 unsigned long delta_size;
473 unsigned long deflate_size;
474 unsigned long data_size;
476 printf("GIT binary patch\n");
477 /* We could do deflated delta, or we could do just deflated two,
478 * whichever is smaller.
481 deflated = deflate_it(two->ptr, two->size, &deflate_size);
482 if (one->size && two->size) {
483 delta = diff_delta(one->ptr, one->size,
485 &delta_size, deflate_size);
487 void *to_free = delta;
488 orig_size = delta_size;
489 delta = deflate_it(delta, delta_size, &delta_size);
494 if (delta && delta_size < deflate_size) {
495 printf("delta %lu\n", orig_size);
498 data_size = delta_size;
501 printf("literal %lu\n", two->size);
504 data_size = deflate_size;
507 /* emit data encoded in base85 */
510 int bytes = (52 < data_size) ? 52 : data_size;
514 line[0] = bytes + 'A' - 1;
516 line[0] = bytes - 26 + 'a' - 1;
517 encode_85(line + 1, cp, bytes);
525 #define FIRST_FEW_BYTES 8000
526 static int mmfile_is_binary(mmfile_t *mf)
529 if (FIRST_FEW_BYTES < sz)
530 sz = FIRST_FEW_BYTES;
531 if (memchr(mf->ptr, 0, sz))
536 static void builtin_diff(const char *name_a,
538 struct diff_filespec *one,
539 struct diff_filespec *two,
540 const char *xfrm_msg,
541 struct diff_options *o,
542 int complete_rewrite)
548 a_one = quote_two("a/", name_a);
549 b_two = quote_two("b/", name_b);
550 lbl[0] = DIFF_FILE_VALID(one) ? a_one : "/dev/null";
551 lbl[1] = DIFF_FILE_VALID(two) ? b_two : "/dev/null";
552 printf("diff --git %s %s\n", a_one, b_two);
553 if (lbl[0][0] == '/') {
555 printf("new file mode %06o\n", two->mode);
556 if (xfrm_msg && xfrm_msg[0])
559 else if (lbl[1][0] == '/') {
560 printf("deleted file mode %06o\n", one->mode);
561 if (xfrm_msg && xfrm_msg[0])
565 if (one->mode != two->mode) {
566 printf("old mode %06o\n", one->mode);
567 printf("new mode %06o\n", two->mode);
569 if (xfrm_msg && xfrm_msg[0])
572 * we do not run diff between different kind
575 if ((one->mode ^ two->mode) & S_IFMT)
576 goto free_ab_and_return;
577 if (complete_rewrite) {
578 emit_rewrite_diff(name_a, name_b, one, two);
579 goto free_ab_and_return;
583 if (fill_mmfile(&mf1, one) < 0 || fill_mmfile(&mf2, two) < 0)
584 die("unable to read files to diff");
586 if (mmfile_is_binary(&mf1) || mmfile_is_binary(&mf2)) {
587 /* Quite common confusing case */
588 if (mf1.size == mf2.size &&
589 !memcmp(mf1.ptr, mf2.ptr, mf1.size))
590 goto free_ab_and_return;
592 emit_binary_diff(&mf1, &mf2);
594 printf("Binary files %s and %s differ\n",
598 /* Crazy xdl interfaces.. */
599 const char *diffopts = getenv("GIT_DIFF_OPTS");
603 struct emit_callback ecbdata;
605 ecbdata.label_path = lbl;
606 xpp.flags = XDF_NEED_MINIMAL;
607 xecfg.ctxlen = o->context;
608 xecfg.flags = XDL_EMIT_FUNCNAMES;
611 else if (!strncmp(diffopts, "--unified=", 10))
612 xecfg.ctxlen = strtoul(diffopts + 10, NULL, 10);
613 else if (!strncmp(diffopts, "-u", 2))
614 xecfg.ctxlen = strtoul(diffopts + 2, NULL, 10);
617 xdl_diff(&mf1, &mf2, &xpp, &xecfg, &ecb);
626 static void builtin_diffstat(const char *name_a, const char *name_b,
627 struct diff_filespec *one,
628 struct diff_filespec *two,
629 struct diffstat_t *diffstat,
630 int complete_rewrite)
633 struct diffstat_file *data;
635 data = diffstat_add(diffstat, name_a, name_b);
638 data->is_unmerged = 1;
641 if (complete_rewrite) {
642 diff_populate_filespec(one, 0);
643 diff_populate_filespec(two, 0);
644 data->deleted = count_lines(one->data, one->size);
645 data->added = count_lines(two->data, two->size);
648 if (fill_mmfile(&mf1, one) < 0 || fill_mmfile(&mf2, two) < 0)
649 die("unable to read files to diff");
651 if (mmfile_is_binary(&mf1) || mmfile_is_binary(&mf2))
654 /* Crazy xdl interfaces.. */
659 xpp.flags = XDF_NEED_MINIMAL;
662 ecb.outf = xdiff_outf;
664 xdl_diff(&mf1, &mf2, &xpp, &xecfg, &ecb);
668 static void builtin_checkdiff(const char *name_a, const char *name_b,
669 struct diff_filespec *one,
670 struct diff_filespec *two)
673 struct checkdiff_t data;
678 memset(&data, 0, sizeof(data));
679 data.xm.consume = checkdiff_consume;
680 data.filename = name_b ? name_b : name_a;
683 if (fill_mmfile(&mf1, one) < 0 || fill_mmfile(&mf2, two) < 0)
684 die("unable to read files to diff");
686 if (mmfile_is_binary(&mf2))
689 /* Crazy xdl interfaces.. */
694 xpp.flags = XDF_NEED_MINIMAL;
697 ecb.outf = xdiff_outf;
699 xdl_diff(&mf1, &mf2, &xpp, &xecfg, &ecb);
703 struct diff_filespec *alloc_filespec(const char *path)
705 int namelen = strlen(path);
706 struct diff_filespec *spec = xmalloc(sizeof(*spec) + namelen + 1);
708 memset(spec, 0, sizeof(*spec));
709 spec->path = (char *)(spec + 1);
710 memcpy(spec->path, path, namelen+1);
714 void fill_filespec(struct diff_filespec *spec, const unsigned char *sha1,
718 spec->mode = canon_mode(mode);
719 memcpy(spec->sha1, sha1, 20);
720 spec->sha1_valid = !!memcmp(sha1, null_sha1, 20);
725 * Given a name and sha1 pair, if the dircache tells us the file in
726 * the work tree has that object contents, return true, so that
727 * prepare_temp_file() does not have to inflate and extract.
729 static int work_tree_matches(const char *name, const unsigned char *sha1)
731 struct cache_entry *ce;
735 /* We do not read the cache ourselves here, because the
736 * benchmark with my previous version that always reads cache
737 * shows that it makes things worse for diff-tree comparing
738 * two linux-2.6 kernel trees in an already checked out work
739 * tree. This is because most diff-tree comparisons deal with
740 * only a small number of files, while reading the cache is
741 * expensive for a large project, and its cost outweighs the
742 * savings we get by not inflating the object to a temporary
743 * file. Practically, this code only helps when we are used
744 * by diff-cache --cached, which does read the cache before
751 pos = cache_name_pos(name, len);
754 ce = active_cache[pos];
755 if ((lstat(name, &st) < 0) ||
756 !S_ISREG(st.st_mode) || /* careful! */
757 ce_match_stat(ce, &st, 0) ||
758 memcmp(sha1, ce->sha1, 20))
760 /* we return 1 only when we can stat, it is a regular file,
761 * stat information matches, and sha1 recorded in the cache
762 * matches. I.e. we know the file in the work tree really is
763 * the same as the <name, sha1> pair.
768 static struct sha1_size_cache {
769 unsigned char sha1[20];
772 static int sha1_size_cache_nr, sha1_size_cache_alloc;
774 static struct sha1_size_cache *locate_size_cache(unsigned char *sha1,
779 struct sha1_size_cache *e;
782 last = sha1_size_cache_nr;
783 while (last > first) {
784 int cmp, next = (last + first) >> 1;
785 e = sha1_size_cache[next];
786 cmp = memcmp(e->sha1, sha1, 20);
798 /* insert to make it at "first" */
799 if (sha1_size_cache_alloc <= sha1_size_cache_nr) {
800 sha1_size_cache_alloc = alloc_nr(sha1_size_cache_alloc);
801 sha1_size_cache = xrealloc(sha1_size_cache,
802 sha1_size_cache_alloc *
803 sizeof(*sha1_size_cache));
805 sha1_size_cache_nr++;
806 if (first < sha1_size_cache_nr)
807 memmove(sha1_size_cache + first + 1, sha1_size_cache + first,
808 (sha1_size_cache_nr - first - 1) *
809 sizeof(*sha1_size_cache));
810 e = xmalloc(sizeof(struct sha1_size_cache));
811 sha1_size_cache[first] = e;
812 memcpy(e->sha1, sha1, 20);
818 * While doing rename detection and pickaxe operation, we may need to
819 * grab the data for the blob (or file) for our own in-core comparison.
820 * diff_filespec has data and size fields for this purpose.
822 int diff_populate_filespec(struct diff_filespec *s, int size_only)
825 if (!DIFF_FILE_VALID(s))
826 die("internal error: asking to populate invalid file.");
827 if (S_ISDIR(s->mode))
835 if (!s->sha1_valid ||
836 work_tree_matches(s->path, s->sha1)) {
839 if (lstat(s->path, &st) < 0) {
840 if (errno == ENOENT) {
849 s->size = st.st_size;
854 if (S_ISLNK(st.st_mode)) {
856 s->data = xmalloc(s->size);
858 ret = readlink(s->path, s->data, s->size);
865 fd = open(s->path, O_RDONLY);
868 s->data = mmap(NULL, s->size, PROT_READ, MAP_PRIVATE, fd, 0);
870 if (s->data == MAP_FAILED)
872 s->should_munmap = 1;
876 struct sha1_size_cache *e;
879 e = locate_size_cache(s->sha1, 1, 0);
884 if (!sha1_object_info(s->sha1, type, &s->size))
885 locate_size_cache(s->sha1, 0, s->size);
888 s->data = read_sha1_file(s->sha1, type, &s->size);
895 void diff_free_filespec_data(struct diff_filespec *s)
899 else if (s->should_munmap)
900 munmap(s->data, s->size);
901 s->should_free = s->should_munmap = 0;
907 static void prep_temp_blob(struct diff_tempfile *temp,
910 const unsigned char *sha1,
915 fd = git_mkstemp(temp->tmp_path, TEMPFILE_PATH_LEN, ".diff_XXXXXX");
917 die("unable to create temp-file");
918 if (write(fd, blob, size) != size)
919 die("unable to write temp-file");
921 temp->name = temp->tmp_path;
922 strcpy(temp->hex, sha1_to_hex(sha1));
924 sprintf(temp->mode, "%06o", mode);
927 static void prepare_temp_file(const char *name,
928 struct diff_tempfile *temp,
929 struct diff_filespec *one)
931 if (!DIFF_FILE_VALID(one)) {
933 /* A '-' entry produces this for file-2, and
934 * a '+' entry produces this for file-1.
936 temp->name = "/dev/null";
937 strcpy(temp->hex, ".");
938 strcpy(temp->mode, ".");
942 if (!one->sha1_valid ||
943 work_tree_matches(name, one->sha1)) {
945 if (lstat(name, &st) < 0) {
947 goto not_a_valid_file;
948 die("stat(%s): %s", name, strerror(errno));
950 if (S_ISLNK(st.st_mode)) {
952 char buf[PATH_MAX + 1]; /* ought to be SYMLINK_MAX */
953 if (sizeof(buf) <= st.st_size)
954 die("symlink too long: %s", name);
955 ret = readlink(name, buf, st.st_size);
957 die("readlink(%s)", name);
958 prep_temp_blob(temp, buf, st.st_size,
960 one->sha1 : null_sha1),
962 one->mode : S_IFLNK));
965 /* we can borrow from the file in the work tree */
967 if (!one->sha1_valid)
968 strcpy(temp->hex, sha1_to_hex(null_sha1));
970 strcpy(temp->hex, sha1_to_hex(one->sha1));
971 /* Even though we may sometimes borrow the
972 * contents from the work tree, we always want
973 * one->mode. mode is trustworthy even when
974 * !(one->sha1_valid), as long as
975 * DIFF_FILE_VALID(one).
977 sprintf(temp->mode, "%06o", one->mode);
982 if (diff_populate_filespec(one, 0))
983 die("cannot read data blob for %s", one->path);
984 prep_temp_blob(temp, one->data, one->size,
985 one->sha1, one->mode);
989 static void remove_tempfile(void)
993 for (i = 0; i < 2; i++)
994 if (diff_temp[i].name == diff_temp[i].tmp_path) {
995 unlink(diff_temp[i].name);
996 diff_temp[i].name = NULL;
1000 static void remove_tempfile_on_signal(int signo)
1003 signal(SIGINT, SIG_DFL);
1007 static int spawn_prog(const char *pgm, const char **arg)
1015 die("unable to fork");
1017 execvp(pgm, (char *const*) arg);
1021 while (waitpid(pid, &status, 0) < 0) {
1027 /* Earlier we did not check the exit status because
1028 * diff exits non-zero if files are different, and
1029 * we are not interested in knowing that. It was a
1030 * mistake which made it harder to quit a diff-*
1031 * session that uses the git-apply-patch-script as
1032 * the GIT_EXTERNAL_DIFF. A custom GIT_EXTERNAL_DIFF
1033 * should also exit non-zero only when it wants to
1034 * abort the entire diff-* session.
1036 if (WIFEXITED(status) && !WEXITSTATUS(status))
1041 /* An external diff command takes:
1043 * diff-cmd name infile1 infile1-sha1 infile1-mode \
1044 * infile2 infile2-sha1 infile2-mode [ rename-to ]
1047 static void run_external_diff(const char *pgm,
1050 struct diff_filespec *one,
1051 struct diff_filespec *two,
1052 const char *xfrm_msg,
1053 int complete_rewrite)
1055 const char *spawn_arg[10];
1056 struct diff_tempfile *temp = diff_temp;
1058 static int atexit_asked = 0;
1059 const char *othername;
1060 const char **arg = &spawn_arg[0];
1062 othername = (other? other : name);
1064 prepare_temp_file(name, &temp[0], one);
1065 prepare_temp_file(othername, &temp[1], two);
1066 if (! atexit_asked &&
1067 (temp[0].name == temp[0].tmp_path ||
1068 temp[1].name == temp[1].tmp_path)) {
1070 atexit(remove_tempfile);
1072 signal(SIGINT, remove_tempfile_on_signal);
1078 *arg++ = temp[0].name;
1079 *arg++ = temp[0].hex;
1080 *arg++ = temp[0].mode;
1081 *arg++ = temp[1].name;
1082 *arg++ = temp[1].hex;
1083 *arg++ = temp[1].mode;
1093 retval = spawn_prog(pgm, spawn_arg);
1096 fprintf(stderr, "external diff died, stopping at %s.\n", name);
1101 static void run_diff_cmd(const char *pgm,
1104 struct diff_filespec *one,
1105 struct diff_filespec *two,
1106 const char *xfrm_msg,
1107 struct diff_options *o,
1108 int complete_rewrite)
1111 run_external_diff(pgm, name, other, one, two, xfrm_msg,
1116 builtin_diff(name, other ? other : name,
1117 one, two, xfrm_msg, o, complete_rewrite);
1119 printf("* Unmerged path %s\n", name);
1122 static void diff_fill_sha1_info(struct diff_filespec *one)
1124 if (DIFF_FILE_VALID(one)) {
1125 if (!one->sha1_valid) {
1127 if (lstat(one->path, &st) < 0)
1128 die("stat %s", one->path);
1129 if (index_path(one->sha1, one->path, &st, 0))
1130 die("cannot hash %s\n", one->path);
1134 memset(one->sha1, 0, 20);
1137 static void run_diff(struct diff_filepair *p, struct diff_options *o)
1139 const char *pgm = external_diff();
1140 char msg[PATH_MAX*2+300], *xfrm_msg;
1141 struct diff_filespec *one;
1142 struct diff_filespec *two;
1145 char *name_munged, *other_munged;
1146 int complete_rewrite = 0;
1149 if (DIFF_PAIR_UNMERGED(p)) {
1151 run_diff_cmd(pgm, p->one->path, NULL, NULL, NULL, NULL, o, 0);
1155 name = p->one->path;
1156 other = (strcmp(name, p->two->path) ? p->two->path : NULL);
1157 name_munged = quote_one(name);
1158 other_munged = quote_one(other);
1159 one = p->one; two = p->two;
1161 diff_fill_sha1_info(one);
1162 diff_fill_sha1_info(two);
1165 switch (p->status) {
1166 case DIFF_STATUS_COPIED:
1167 len += snprintf(msg + len, sizeof(msg) - len,
1168 "similarity index %d%%\n"
1171 (int)(0.5 + p->score * 100.0/MAX_SCORE),
1172 name_munged, other_munged);
1174 case DIFF_STATUS_RENAMED:
1175 len += snprintf(msg + len, sizeof(msg) - len,
1176 "similarity index %d%%\n"
1179 (int)(0.5 + p->score * 100.0/MAX_SCORE),
1180 name_munged, other_munged);
1182 case DIFF_STATUS_MODIFIED:
1184 len += snprintf(msg + len, sizeof(msg) - len,
1185 "dissimilarity index %d%%\n",
1186 (int)(0.5 + p->score *
1188 complete_rewrite = 1;
1197 if (memcmp(one->sha1, two->sha1, 20)) {
1198 int abbrev = o->full_index ? 40 : DEFAULT_ABBREV;
1200 len += snprintf(msg + len, sizeof(msg) - len,
1202 abbrev, sha1_to_hex(one->sha1),
1203 abbrev, sha1_to_hex(two->sha1));
1204 if (one->mode == two->mode)
1205 len += snprintf(msg + len, sizeof(msg) - len,
1206 " %06o", one->mode);
1207 len += snprintf(msg + len, sizeof(msg) - len, "\n");
1212 xfrm_msg = len ? msg : NULL;
1215 DIFF_FILE_VALID(one) && DIFF_FILE_VALID(two) &&
1216 (S_IFMT & one->mode) != (S_IFMT & two->mode)) {
1217 /* a filepair that changes between file and symlink
1218 * needs to be split into deletion and creation.
1220 struct diff_filespec *null = alloc_filespec(two->path);
1221 run_diff_cmd(NULL, name, other, one, null, xfrm_msg, o, 0);
1223 null = alloc_filespec(one->path);
1224 run_diff_cmd(NULL, name, other, null, two, xfrm_msg, o, 0);
1228 run_diff_cmd(pgm, name, other, one, two, xfrm_msg, o,
1235 static void run_diffstat(struct diff_filepair *p, struct diff_options *o,
1236 struct diffstat_t *diffstat)
1240 int complete_rewrite = 0;
1242 if (DIFF_PAIR_UNMERGED(p)) {
1244 builtin_diffstat(p->one->path, NULL, NULL, NULL, diffstat, 0);
1248 name = p->one->path;
1249 other = (strcmp(name, p->two->path) ? p->two->path : NULL);
1251 diff_fill_sha1_info(p->one);
1252 diff_fill_sha1_info(p->two);
1254 if (p->status == DIFF_STATUS_MODIFIED && p->score)
1255 complete_rewrite = 1;
1256 builtin_diffstat(name, other, p->one, p->two, diffstat, complete_rewrite);
1259 static void run_checkdiff(struct diff_filepair *p, struct diff_options *o)
1264 if (DIFF_PAIR_UNMERGED(p)) {
1269 name = p->one->path;
1270 other = (strcmp(name, p->two->path) ? p->two->path : NULL);
1272 diff_fill_sha1_info(p->one);
1273 diff_fill_sha1_info(p->two);
1275 builtin_checkdiff(name, other, p->one, p->two);
1278 void diff_setup(struct diff_options *options)
1280 memset(options, 0, sizeof(*options));
1281 options->output_format = DIFF_FORMAT_RAW;
1282 options->line_termination = '\n';
1283 options->break_opt = -1;
1284 options->rename_limit = -1;
1285 options->context = 3;
1287 options->change = diff_change;
1288 options->add_remove = diff_addremove;
1291 int diff_setup_done(struct diff_options *options)
1293 if ((options->find_copies_harder &&
1294 options->detect_rename != DIFF_DETECT_COPY) ||
1295 (0 <= options->rename_limit && !options->detect_rename))
1299 * These cases always need recursive; we do not drop caller-supplied
1300 * recursive bits for other formats here.
1302 if ((options->output_format == DIFF_FORMAT_PATCH) ||
1303 (options->output_format == DIFF_FORMAT_DIFFSTAT) ||
1304 (options->output_format == DIFF_FORMAT_CHECKDIFF))
1305 options->recursive = 1;
1308 * These combinations do not make sense.
1310 if (options->output_format == DIFF_FORMAT_RAW)
1311 options->with_raw = 0;
1312 if (options->output_format == DIFF_FORMAT_DIFFSTAT)
1313 options->with_stat = 0;
1315 if (options->detect_rename && options->rename_limit < 0)
1316 options->rename_limit = diff_rename_limit_default;
1317 if (options->setup & DIFF_SETUP_USE_CACHE) {
1319 /* read-cache does not die even when it fails
1320 * so it is safe for us to do this here. Also
1321 * it does not smudge active_cache or active_nr
1322 * when it fails, so we do not have to worry about
1323 * cleaning it up ourselves either.
1327 if (options->setup & DIFF_SETUP_USE_SIZE_CACHE)
1329 if (options->abbrev <= 0 || 40 < options->abbrev)
1330 options->abbrev = 40; /* full */
1335 int opt_arg(const char *arg, int arg_short, const char *arg_long, int *val)
1345 if (c == arg_short) {
1349 if (val && isdigit(c)) {
1351 int n = strtoul(arg, &end, 10);
1362 eq = strchr(arg, '=');
1367 if (!len || strncmp(arg, arg_long, len))
1372 if (!isdigit(*++eq))
1374 n = strtoul(eq, &end, 10);
1382 int diff_opt_parse(struct diff_options *options, const char **av, int ac)
1384 const char *arg = av[0];
1385 if (!strcmp(arg, "-p") || !strcmp(arg, "-u"))
1386 options->output_format = DIFF_FORMAT_PATCH;
1387 else if (opt_arg(arg, 'U', "unified", &options->context))
1388 options->output_format = DIFF_FORMAT_PATCH;
1389 else if (!strcmp(arg, "--patch-with-raw")) {
1390 options->output_format = DIFF_FORMAT_PATCH;
1391 options->with_raw = 1;
1393 else if (!strcmp(arg, "--stat"))
1394 options->output_format = DIFF_FORMAT_DIFFSTAT;
1395 else if (!strcmp(arg, "--check"))
1396 options->output_format = DIFF_FORMAT_CHECKDIFF;
1397 else if (!strcmp(arg, "--summary"))
1398 options->summary = 1;
1399 else if (!strcmp(arg, "--patch-with-stat")) {
1400 options->output_format = DIFF_FORMAT_PATCH;
1401 options->with_stat = 1;
1403 else if (!strcmp(arg, "-z"))
1404 options->line_termination = 0;
1405 else if (!strncmp(arg, "-l", 2))
1406 options->rename_limit = strtoul(arg+2, NULL, 10);
1407 else if (!strcmp(arg, "--full-index"))
1408 options->full_index = 1;
1409 else if (!strcmp(arg, "--binary")) {
1410 options->output_format = DIFF_FORMAT_PATCH;
1411 options->full_index = options->binary = 1;
1413 else if (!strcmp(arg, "--name-only"))
1414 options->output_format = DIFF_FORMAT_NAME;
1415 else if (!strcmp(arg, "--name-status"))
1416 options->output_format = DIFF_FORMAT_NAME_STATUS;
1417 else if (!strcmp(arg, "-R"))
1418 options->reverse_diff = 1;
1419 else if (!strncmp(arg, "-S", 2))
1420 options->pickaxe = arg + 2;
1421 else if (!strcmp(arg, "-s"))
1422 options->output_format = DIFF_FORMAT_NO_OUTPUT;
1423 else if (!strncmp(arg, "-O", 2))
1424 options->orderfile = arg + 2;
1425 else if (!strncmp(arg, "--diff-filter=", 14))
1426 options->filter = arg + 14;
1427 else if (!strcmp(arg, "--pickaxe-all"))
1428 options->pickaxe_opts = DIFF_PICKAXE_ALL;
1429 else if (!strcmp(arg, "--pickaxe-regex"))
1430 options->pickaxe_opts = DIFF_PICKAXE_REGEX;
1431 else if (!strncmp(arg, "-B", 2)) {
1432 if ((options->break_opt =
1433 diff_scoreopt_parse(arg)) == -1)
1436 else if (!strncmp(arg, "-M", 2)) {
1437 if ((options->rename_score =
1438 diff_scoreopt_parse(arg)) == -1)
1440 options->detect_rename = DIFF_DETECT_RENAME;
1442 else if (!strncmp(arg, "-C", 2)) {
1443 if ((options->rename_score =
1444 diff_scoreopt_parse(arg)) == -1)
1446 options->detect_rename = DIFF_DETECT_COPY;
1448 else if (!strcmp(arg, "--find-copies-harder"))
1449 options->find_copies_harder = 1;
1450 else if (!strcmp(arg, "--abbrev"))
1451 options->abbrev = DEFAULT_ABBREV;
1452 else if (!strncmp(arg, "--abbrev=", 9)) {
1453 options->abbrev = strtoul(arg + 9, NULL, 10);
1454 if (options->abbrev < MINIMUM_ABBREV)
1455 options->abbrev = MINIMUM_ABBREV;
1456 else if (40 < options->abbrev)
1457 options->abbrev = 40;
1464 static int parse_num(const char **cp_p)
1466 unsigned long num, scale;
1468 const char *cp = *cp_p;
1475 if ( !dot && ch == '.' ) {
1478 } else if ( ch == '%' ) {
1479 scale = dot ? scale*100 : 100;
1480 cp++; /* % is always at the end */
1482 } else if ( ch >= '0' && ch <= '9' ) {
1483 if ( scale < 100000 ) {
1485 num = (num*10) + (ch-'0');
1494 /* user says num divided by scale and we say internally that
1495 * is MAX_SCORE * num / scale.
1497 return (num >= scale) ? MAX_SCORE : (MAX_SCORE * num / scale);
1500 int diff_scoreopt_parse(const char *opt)
1502 int opt1, opt2, cmd;
1507 if (cmd != 'M' && cmd != 'C' && cmd != 'B')
1508 return -1; /* that is not a -M, -C nor -B option */
1510 opt1 = parse_num(&opt);
1516 else if (*opt != '/')
1517 return -1; /* we expect -B80/99 or -B80 */
1520 opt2 = parse_num(&opt);
1525 return opt1 | (opt2 << 16);
1528 struct diff_queue_struct diff_queued_diff;
1530 void diff_q(struct diff_queue_struct *queue, struct diff_filepair *dp)
1532 if (queue->alloc <= queue->nr) {
1533 queue->alloc = alloc_nr(queue->alloc);
1534 queue->queue = xrealloc(queue->queue,
1535 sizeof(dp) * queue->alloc);
1537 queue->queue[queue->nr++] = dp;
1540 struct diff_filepair *diff_queue(struct diff_queue_struct *queue,
1541 struct diff_filespec *one,
1542 struct diff_filespec *two)
1544 struct diff_filepair *dp = xmalloc(sizeof(*dp));
1549 dp->source_stays = 0;
1550 dp->broken_pair = 0;
1556 void diff_free_filepair(struct diff_filepair *p)
1558 diff_free_filespec_data(p->one);
1559 diff_free_filespec_data(p->two);
1565 /* This is different from find_unique_abbrev() in that
1566 * it stuffs the result with dots for alignment.
1568 const char *diff_unique_abbrev(const unsigned char *sha1, int len)
1573 return sha1_to_hex(sha1);
1575 abbrev = find_unique_abbrev(sha1, len);
1577 return sha1_to_hex(sha1);
1578 abblen = strlen(abbrev);
1580 static char hex[41];
1581 if (len < abblen && abblen <= len + 2)
1582 sprintf(hex, "%s%.*s", abbrev, len+3-abblen, "..");
1584 sprintf(hex, "%s...", abbrev);
1587 return sha1_to_hex(sha1);
1590 static void diff_flush_raw(struct diff_filepair *p,
1591 int line_termination,
1592 int inter_name_termination,
1593 struct diff_options *options,
1598 int abbrev = options->abbrev;
1599 const char *path_one, *path_two;
1601 path_one = p->one->path;
1602 path_two = p->two->path;
1603 if (line_termination) {
1604 path_one = quote_one(path_one);
1605 path_two = quote_one(path_two);
1609 sprintf(status, "%c%03d", p->status,
1610 (int)(0.5 + p->score * 100.0/MAX_SCORE));
1612 status[0] = p->status;
1615 switch (p->status) {
1616 case DIFF_STATUS_COPIED:
1617 case DIFF_STATUS_RENAMED:
1620 case DIFF_STATUS_ADDED:
1621 case DIFF_STATUS_DELETED:
1628 if (output_format != DIFF_FORMAT_NAME_STATUS) {
1629 printf(":%06o %06o %s ",
1630 p->one->mode, p->two->mode,
1631 diff_unique_abbrev(p->one->sha1, abbrev));
1633 diff_unique_abbrev(p->two->sha1, abbrev));
1635 printf("%s%c%s", status, inter_name_termination, path_one);
1637 printf("%c%s", inter_name_termination, path_two);
1638 putchar(line_termination);
1639 if (path_one != p->one->path)
1640 free((void*)path_one);
1641 if (path_two != p->two->path)
1642 free((void*)path_two);
1645 static void diff_flush_name(struct diff_filepair *p,
1646 int inter_name_termination,
1647 int line_termination)
1649 char *path = p->two->path;
1651 if (line_termination)
1652 path = quote_one(p->two->path);
1654 path = p->two->path;
1655 printf("%s%c", path, line_termination);
1656 if (p->two->path != path)
1660 int diff_unmodified_pair(struct diff_filepair *p)
1662 /* This function is written stricter than necessary to support
1663 * the currently implemented transformers, but the idea is to
1664 * let transformers to produce diff_filepairs any way they want,
1665 * and filter and clean them up here before producing the output.
1667 struct diff_filespec *one, *two;
1669 if (DIFF_PAIR_UNMERGED(p))
1670 return 0; /* unmerged is interesting */
1675 /* deletion, addition, mode or type change
1676 * and rename are all interesting.
1678 if (DIFF_FILE_VALID(one) != DIFF_FILE_VALID(two) ||
1679 DIFF_PAIR_MODE_CHANGED(p) ||
1680 strcmp(one->path, two->path))
1683 /* both are valid and point at the same path. that is, we are
1684 * dealing with a change.
1686 if (one->sha1_valid && two->sha1_valid &&
1687 !memcmp(one->sha1, two->sha1, sizeof(one->sha1)))
1688 return 1; /* no change */
1689 if (!one->sha1_valid && !two->sha1_valid)
1690 return 1; /* both look at the same file on the filesystem. */
1694 static void diff_flush_patch(struct diff_filepair *p, struct diff_options *o)
1696 if (diff_unmodified_pair(p))
1699 if ((DIFF_FILE_VALID(p->one) && S_ISDIR(p->one->mode)) ||
1700 (DIFF_FILE_VALID(p->two) && S_ISDIR(p->two->mode)))
1701 return; /* no tree diffs in patch format */
1706 static void diff_flush_stat(struct diff_filepair *p, struct diff_options *o,
1707 struct diffstat_t *diffstat)
1709 if (diff_unmodified_pair(p))
1712 if ((DIFF_FILE_VALID(p->one) && S_ISDIR(p->one->mode)) ||
1713 (DIFF_FILE_VALID(p->two) && S_ISDIR(p->two->mode)))
1714 return; /* no tree diffs in patch format */
1716 run_diffstat(p, o, diffstat);
1719 static void diff_flush_checkdiff(struct diff_filepair *p,
1720 struct diff_options *o)
1722 if (diff_unmodified_pair(p))
1725 if ((DIFF_FILE_VALID(p->one) && S_ISDIR(p->one->mode)) ||
1726 (DIFF_FILE_VALID(p->two) && S_ISDIR(p->two->mode)))
1727 return; /* no tree diffs in patch format */
1729 run_checkdiff(p, o);
1732 int diff_queue_is_empty(void)
1734 struct diff_queue_struct *q = &diff_queued_diff;
1736 for (i = 0; i < q->nr; i++)
1737 if (!diff_unmodified_pair(q->queue[i]))
1743 void diff_debug_filespec(struct diff_filespec *s, int x, const char *one)
1745 fprintf(stderr, "queue[%d] %s (%s) %s %06o %s\n",
1748 DIFF_FILE_VALID(s) ? "valid" : "invalid",
1750 s->sha1_valid ? sha1_to_hex(s->sha1) : "");
1751 fprintf(stderr, "queue[%d] %s size %lu flags %d\n",
1753 s->size, s->xfrm_flags);
1756 void diff_debug_filepair(const struct diff_filepair *p, int i)
1758 diff_debug_filespec(p->one, i, "one");
1759 diff_debug_filespec(p->two, i, "two");
1760 fprintf(stderr, "score %d, status %c stays %d broken %d\n",
1761 p->score, p->status ? p->status : '?',
1762 p->source_stays, p->broken_pair);
1765 void diff_debug_queue(const char *msg, struct diff_queue_struct *q)
1769 fprintf(stderr, "%s\n", msg);
1770 fprintf(stderr, "q->nr = %d\n", q->nr);
1771 for (i = 0; i < q->nr; i++) {
1772 struct diff_filepair *p = q->queue[i];
1773 diff_debug_filepair(p, i);
1778 static void diff_resolve_rename_copy(void)
1781 struct diff_filepair *p, *pp;
1782 struct diff_queue_struct *q = &diff_queued_diff;
1784 diff_debug_queue("resolve-rename-copy", q);
1786 for (i = 0; i < q->nr; i++) {
1788 p->status = 0; /* undecided */
1789 if (DIFF_PAIR_UNMERGED(p))
1790 p->status = DIFF_STATUS_UNMERGED;
1791 else if (!DIFF_FILE_VALID(p->one))
1792 p->status = DIFF_STATUS_ADDED;
1793 else if (!DIFF_FILE_VALID(p->two))
1794 p->status = DIFF_STATUS_DELETED;
1795 else if (DIFF_PAIR_TYPE_CHANGED(p))
1796 p->status = DIFF_STATUS_TYPE_CHANGED;
1798 /* from this point on, we are dealing with a pair
1799 * whose both sides are valid and of the same type, i.e.
1800 * either in-place edit or rename/copy edit.
1802 else if (DIFF_PAIR_RENAME(p)) {
1803 if (p->source_stays) {
1804 p->status = DIFF_STATUS_COPIED;
1807 /* See if there is some other filepair that
1808 * copies from the same source as us. If so
1809 * we are a copy. Otherwise we are either a
1810 * copy if the path stays, or a rename if it
1811 * does not, but we already handled "stays" case.
1813 for (j = i + 1; j < q->nr; j++) {
1815 if (strcmp(pp->one->path, p->one->path))
1816 continue; /* not us */
1817 if (!DIFF_PAIR_RENAME(pp))
1818 continue; /* not a rename/copy */
1819 /* pp is a rename/copy from the same source */
1820 p->status = DIFF_STATUS_COPIED;
1824 p->status = DIFF_STATUS_RENAMED;
1826 else if (memcmp(p->one->sha1, p->two->sha1, 20) ||
1827 p->one->mode != p->two->mode)
1828 p->status = DIFF_STATUS_MODIFIED;
1830 /* This is a "no-change" entry and should not
1831 * happen anymore, but prepare for broken callers.
1833 error("feeding unmodified %s to diffcore",
1835 p->status = DIFF_STATUS_UNKNOWN;
1838 diff_debug_queue("resolve-rename-copy done", q);
1841 static void flush_one_pair(struct diff_filepair *p,
1842 int diff_output_format,
1843 struct diff_options *options,
1844 struct diffstat_t *diffstat)
1846 int inter_name_termination = '\t';
1847 int line_termination = options->line_termination;
1848 if (!line_termination)
1849 inter_name_termination = 0;
1851 switch (p->status) {
1852 case DIFF_STATUS_UNKNOWN:
1855 die("internal error in diff-resolve-rename-copy");
1858 switch (diff_output_format) {
1859 case DIFF_FORMAT_DIFFSTAT:
1860 diff_flush_stat(p, options, diffstat);
1862 case DIFF_FORMAT_CHECKDIFF:
1863 diff_flush_checkdiff(p, options);
1865 case DIFF_FORMAT_PATCH:
1866 diff_flush_patch(p, options);
1868 case DIFF_FORMAT_RAW:
1869 case DIFF_FORMAT_NAME_STATUS:
1870 diff_flush_raw(p, line_termination,
1871 inter_name_termination,
1872 options, diff_output_format);
1874 case DIFF_FORMAT_NAME:
1876 inter_name_termination,
1879 case DIFF_FORMAT_NO_OUTPUT:
1885 static void show_file_mode_name(const char *newdelete, struct diff_filespec *fs)
1888 printf(" %s mode %06o %s\n", newdelete, fs->mode, fs->path);
1890 printf(" %s %s\n", newdelete, fs->path);
1894 static void show_mode_change(struct diff_filepair *p, int show_name)
1896 if (p->one->mode && p->two->mode && p->one->mode != p->two->mode) {
1898 printf(" mode change %06o => %06o %s\n",
1899 p->one->mode, p->two->mode, p->two->path);
1901 printf(" mode change %06o => %06o\n",
1902 p->one->mode, p->two->mode);
1906 static void show_rename_copy(const char *renamecopy, struct diff_filepair *p)
1908 const char *old, *new;
1910 /* Find common prefix */
1914 const char *slash_old, *slash_new;
1915 slash_old = strchr(old, '/');
1916 slash_new = strchr(new, '/');
1919 slash_old - old != slash_new - new ||
1920 memcmp(old, new, slash_new - new))
1922 old = slash_old + 1;
1923 new = slash_new + 1;
1925 /* p->one->path thru old is the common prefix, and old and new
1926 * through the end of names are renames
1928 if (old != p->one->path)
1929 printf(" %s %.*s{%s => %s} (%d%%)\n", renamecopy,
1930 (int)(old - p->one->path), p->one->path,
1931 old, new, (int)(0.5 + p->score * 100.0/MAX_SCORE));
1933 printf(" %s %s => %s (%d%%)\n", renamecopy,
1934 p->one->path, p->two->path,
1935 (int)(0.5 + p->score * 100.0/MAX_SCORE));
1936 show_mode_change(p, 0);
1939 static void diff_summary(struct diff_filepair *p)
1942 case DIFF_STATUS_DELETED:
1943 show_file_mode_name("delete", p->one);
1945 case DIFF_STATUS_ADDED:
1946 show_file_mode_name("create", p->two);
1948 case DIFF_STATUS_COPIED:
1949 show_rename_copy("copy", p);
1951 case DIFF_STATUS_RENAMED:
1952 show_rename_copy("rename", p);
1956 printf(" rewrite %s (%d%%)\n", p->two->path,
1957 (int)(0.5 + p->score * 100.0/MAX_SCORE));
1958 show_mode_change(p, 0);
1959 } else show_mode_change(p, 1);
1964 void diff_flush(struct diff_options *options)
1966 struct diff_queue_struct *q = &diff_queued_diff;
1968 int diff_output_format = options->output_format;
1969 struct diffstat_t *diffstat = NULL;
1971 if (diff_output_format == DIFF_FORMAT_DIFFSTAT || options->with_stat) {
1972 diffstat = xcalloc(sizeof (struct diffstat_t), 1);
1973 diffstat->xm.consume = diffstat_consume;
1976 if (options->with_raw) {
1977 for (i = 0; i < q->nr; i++) {
1978 struct diff_filepair *p = q->queue[i];
1979 flush_one_pair(p, DIFF_FORMAT_RAW, options, NULL);
1981 putchar(options->line_termination);
1983 if (options->with_stat) {
1984 for (i = 0; i < q->nr; i++) {
1985 struct diff_filepair *p = q->queue[i];
1986 flush_one_pair(p, DIFF_FORMAT_DIFFSTAT, options,
1989 show_stats(diffstat);
1992 if (options->stat_sep)
1993 fputs(options->stat_sep, stdout);
1995 putchar(options->line_termination);
1997 for (i = 0; i < q->nr; i++) {
1998 struct diff_filepair *p = q->queue[i];
1999 flush_one_pair(p, diff_output_format, options, diffstat);
2003 show_stats(diffstat);
2007 for (i = 0; i < q->nr; i++) {
2008 if (options->summary)
2009 diff_summary(q->queue[i]);
2010 diff_free_filepair(q->queue[i]);
2015 q->nr = q->alloc = 0;
2018 static void diffcore_apply_filter(const char *filter)
2021 struct diff_queue_struct *q = &diff_queued_diff;
2022 struct diff_queue_struct outq;
2024 outq.nr = outq.alloc = 0;
2029 if (strchr(filter, DIFF_STATUS_FILTER_AON)) {
2031 for (i = found = 0; !found && i < q->nr; i++) {
2032 struct diff_filepair *p = q->queue[i];
2033 if (((p->status == DIFF_STATUS_MODIFIED) &&
2035 strchr(filter, DIFF_STATUS_FILTER_BROKEN)) ||
2037 strchr(filter, DIFF_STATUS_MODIFIED)))) ||
2038 ((p->status != DIFF_STATUS_MODIFIED) &&
2039 strchr(filter, p->status)))
2045 /* otherwise we will clear the whole queue
2046 * by copying the empty outq at the end of this
2047 * function, but first clear the current entries
2050 for (i = 0; i < q->nr; i++)
2051 diff_free_filepair(q->queue[i]);
2054 /* Only the matching ones */
2055 for (i = 0; i < q->nr; i++) {
2056 struct diff_filepair *p = q->queue[i];
2058 if (((p->status == DIFF_STATUS_MODIFIED) &&
2060 strchr(filter, DIFF_STATUS_FILTER_BROKEN)) ||
2062 strchr(filter, DIFF_STATUS_MODIFIED)))) ||
2063 ((p->status != DIFF_STATUS_MODIFIED) &&
2064 strchr(filter, p->status)))
2067 diff_free_filepair(p);
2074 void diffcore_std(struct diff_options *options)
2076 if (options->break_opt != -1)
2077 diffcore_break(options->break_opt);
2078 if (options->detect_rename)
2079 diffcore_rename(options);
2080 if (options->break_opt != -1)
2081 diffcore_merge_broken();
2082 if (options->pickaxe)
2083 diffcore_pickaxe(options->pickaxe, options->pickaxe_opts);
2084 if (options->orderfile)
2085 diffcore_order(options->orderfile);
2086 diff_resolve_rename_copy();
2087 diffcore_apply_filter(options->filter);
2091 void diffcore_std_no_resolve(struct diff_options *options)
2093 if (options->pickaxe)
2094 diffcore_pickaxe(options->pickaxe, options->pickaxe_opts);
2095 if (options->orderfile)
2096 diffcore_order(options->orderfile);
2097 diffcore_apply_filter(options->filter);
2100 void diff_addremove(struct diff_options *options,
2101 int addremove, unsigned mode,
2102 const unsigned char *sha1,
2103 const char *base, const char *path)
2105 char concatpath[PATH_MAX];
2106 struct diff_filespec *one, *two;
2108 /* This may look odd, but it is a preparation for
2109 * feeding "there are unchanged files which should
2110 * not produce diffs, but when you are doing copy
2111 * detection you would need them, so here they are"
2112 * entries to the diff-core. They will be prefixed
2113 * with something like '=' or '*' (I haven't decided
2114 * which but should not make any difference).
2115 * Feeding the same new and old to diff_change()
2116 * also has the same effect.
2117 * Before the final output happens, they are pruned after
2118 * merged into rename/copy pairs as appropriate.
2120 if (options->reverse_diff)
2121 addremove = (addremove == '+' ? '-' :
2122 addremove == '-' ? '+' : addremove);
2124 if (!path) path = "";
2125 sprintf(concatpath, "%s%s", base, path);
2126 one = alloc_filespec(concatpath);
2127 two = alloc_filespec(concatpath);
2129 if (addremove != '+')
2130 fill_filespec(one, sha1, mode);
2131 if (addremove != '-')
2132 fill_filespec(two, sha1, mode);
2134 diff_queue(&diff_queued_diff, one, two);
2137 void diff_change(struct diff_options *options,
2138 unsigned old_mode, unsigned new_mode,
2139 const unsigned char *old_sha1,
2140 const unsigned char *new_sha1,
2141 const char *base, const char *path)
2143 char concatpath[PATH_MAX];
2144 struct diff_filespec *one, *two;
2146 if (options->reverse_diff) {
2148 const unsigned char *tmp_c;
2149 tmp = old_mode; old_mode = new_mode; new_mode = tmp;
2150 tmp_c = old_sha1; old_sha1 = new_sha1; new_sha1 = tmp_c;
2152 if (!path) path = "";
2153 sprintf(concatpath, "%s%s", base, path);
2154 one = alloc_filespec(concatpath);
2155 two = alloc_filespec(concatpath);
2156 fill_filespec(one, old_sha1, old_mode);
2157 fill_filespec(two, new_sha1, new_mode);
2159 diff_queue(&diff_queued_diff, one, two);
2162 void diff_unmerge(struct diff_options *options,
2165 struct diff_filespec *one, *two;
2166 one = alloc_filespec(path);
2167 two = alloc_filespec(path);
2168 diff_queue(&diff_queued_diff, one, two);