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[]= "----------------------------------------------------------------------";
303 static void show_stats(struct diffstat_t* data)
305 int i, len, add, del, total, adds = 0, dels = 0;
306 int max, max_change = 0, max_len = 0;
307 int total_files = data->nr;
312 for (i = 0; i < data->nr; i++) {
313 struct diffstat_file *file = data->files[i];
315 len = strlen(file->name);
319 if (file->is_binary || file->is_unmerged)
321 if (max_change < file->added + file->deleted)
322 max_change = file->added + file->deleted;
325 for (i = 0; i < data->nr; i++) {
327 char *name = data->files[i]->name;
328 int added = data->files[i]->added;
329 int deleted = data->files[i]->deleted;
331 if (0 < (len = quote_c_style(name, NULL, NULL, 0))) {
332 char *qname = xmalloc(len + 1);
333 quote_c_style(name, qname, NULL, 0);
335 data->files[i]->name = name = qname;
339 * "scale" the filename
350 slash = strchr(name, '/');
357 * scale the add/delete
363 if (data->files[i]->is_binary) {
364 printf(" %s%-*s | Bin\n", prefix, len, name);
365 goto free_diffstat_file;
367 else if (data->files[i]->is_unmerged) {
368 printf(" %s%-*s | Unmerged\n", prefix, len, name);
369 goto free_diffstat_file;
371 else if (!data->files[i]->is_renamed &&
372 (added + deleted == 0)) {
374 goto free_diffstat_file;
383 if (max_change > 0) {
384 total = (total * max + max_change / 2) / max_change;
385 add = (add * max + max_change / 2) / max_change;
388 printf(" %s%-*s |%5d %.*s%.*s\n", prefix,
389 len, name, added + deleted,
390 add, pluses, del, minuses);
392 free(data->files[i]->name);
393 free(data->files[i]);
396 printf(" %d files changed, %d insertions(+), %d deletions(-)\n",
397 total_files, adds, dels);
401 struct xdiff_emit_state xm;
402 const char *filename;
406 static void checkdiff_consume(void *priv, char *line, unsigned long len)
408 struct checkdiff_t *data = priv;
410 if (line[0] == '+') {
415 /* check space before tab */
416 for (i = 1; i < len && (line[i] == ' ' || line[i] == '\t'); i++)
419 if (line[i - 1] == '\t' && spaces)
420 printf("%s:%d: space before tab:%.*s\n",
421 data->filename, data->lineno, (int)len, line);
423 /* check white space at line end */
424 if (line[len - 1] == '\n')
426 if (isspace(line[len - 1]))
427 printf("%s:%d: white space at end: %.*s\n",
428 data->filename, data->lineno, (int)len, line);
429 } else if (line[0] == ' ')
431 else if (line[0] == '@') {
432 char *plus = strchr(line, '+');
434 data->lineno = strtol(plus, NULL, 10);
440 static unsigned char *deflate_it(char *data,
442 unsigned long *result_size)
445 unsigned char *deflated;
448 memset(&stream, 0, sizeof(stream));
449 deflateInit(&stream, Z_BEST_COMPRESSION);
450 bound = deflateBound(&stream, size);
451 deflated = xmalloc(bound);
452 stream.next_out = deflated;
453 stream.avail_out = bound;
455 stream.next_in = (unsigned char *)data;
456 stream.avail_in = size;
457 while (deflate(&stream, Z_FINISH) == Z_OK)
460 *result_size = stream.total_out;
464 static void emit_binary_diff(mmfile_t *one, mmfile_t *two)
470 unsigned long orig_size;
471 unsigned long delta_size;
472 unsigned long deflate_size;
473 unsigned long data_size;
475 printf("GIT binary patch\n");
476 /* We could do deflated delta, or we could do just deflated two,
477 * whichever is smaller.
480 deflated = deflate_it(two->ptr, two->size, &deflate_size);
481 if (one->size && two->size) {
482 delta = diff_delta(one->ptr, one->size,
484 &delta_size, deflate_size);
486 void *to_free = delta;
487 orig_size = delta_size;
488 delta = deflate_it(delta, delta_size, &delta_size);
493 if (delta && delta_size < deflate_size) {
494 printf("delta %lu\n", orig_size);
497 data_size = delta_size;
500 printf("literal %lu\n", two->size);
503 data_size = deflate_size;
506 /* emit data encoded in base85 */
509 int bytes = (52 < data_size) ? 52 : data_size;
513 line[0] = bytes + 'A' - 1;
515 line[0] = bytes - 26 + 'a' - 1;
516 encode_85(line + 1, cp, bytes);
524 #define FIRST_FEW_BYTES 8000
525 static int mmfile_is_binary(mmfile_t *mf)
528 if (FIRST_FEW_BYTES < sz)
529 sz = FIRST_FEW_BYTES;
530 if (memchr(mf->ptr, 0, sz))
535 static void builtin_diff(const char *name_a,
537 struct diff_filespec *one,
538 struct diff_filespec *two,
539 const char *xfrm_msg,
540 struct diff_options *o,
541 int complete_rewrite)
547 a_one = quote_two("a/", name_a);
548 b_two = quote_two("b/", name_b);
549 lbl[0] = DIFF_FILE_VALID(one) ? a_one : "/dev/null";
550 lbl[1] = DIFF_FILE_VALID(two) ? b_two : "/dev/null";
551 printf("diff --git %s %s\n", a_one, b_two);
552 if (lbl[0][0] == '/') {
554 printf("new file mode %06o\n", two->mode);
555 if (xfrm_msg && xfrm_msg[0])
558 else if (lbl[1][0] == '/') {
559 printf("deleted file mode %06o\n", one->mode);
560 if (xfrm_msg && xfrm_msg[0])
564 if (one->mode != two->mode) {
565 printf("old mode %06o\n", one->mode);
566 printf("new mode %06o\n", two->mode);
568 if (xfrm_msg && xfrm_msg[0])
571 * we do not run diff between different kind
574 if ((one->mode ^ two->mode) & S_IFMT)
575 goto free_ab_and_return;
576 if (complete_rewrite) {
577 emit_rewrite_diff(name_a, name_b, one, two);
578 goto free_ab_and_return;
582 if (fill_mmfile(&mf1, one) < 0 || fill_mmfile(&mf2, two) < 0)
583 die("unable to read files to diff");
585 if (mmfile_is_binary(&mf1) || mmfile_is_binary(&mf2)) {
586 /* Quite common confusing case */
587 if (mf1.size == mf2.size &&
588 !memcmp(mf1.ptr, mf2.ptr, mf1.size))
589 goto free_ab_and_return;
591 emit_binary_diff(&mf1, &mf2);
593 printf("Binary files %s and %s differ\n",
597 /* Crazy xdl interfaces.. */
598 const char *diffopts = getenv("GIT_DIFF_OPTS");
602 struct emit_callback ecbdata;
604 ecbdata.label_path = lbl;
605 xpp.flags = XDF_NEED_MINIMAL;
606 xecfg.ctxlen = o->context;
607 xecfg.flags = XDL_EMIT_FUNCNAMES;
610 else if (!strncmp(diffopts, "--unified=", 10))
611 xecfg.ctxlen = strtoul(diffopts + 10, NULL, 10);
612 else if (!strncmp(diffopts, "-u", 2))
613 xecfg.ctxlen = strtoul(diffopts + 2, NULL, 10);
616 xdl_diff(&mf1, &mf2, &xpp, &xecfg, &ecb);
625 static void builtin_diffstat(const char *name_a, const char *name_b,
626 struct diff_filespec *one,
627 struct diff_filespec *two,
628 struct diffstat_t *diffstat,
629 int complete_rewrite)
632 struct diffstat_file *data;
634 data = diffstat_add(diffstat, name_a, name_b);
637 data->is_unmerged = 1;
640 if (complete_rewrite) {
641 diff_populate_filespec(one, 0);
642 diff_populate_filespec(two, 0);
643 data->deleted = count_lines(one->data, one->size);
644 data->added = count_lines(two->data, two->size);
647 if (fill_mmfile(&mf1, one) < 0 || fill_mmfile(&mf2, two) < 0)
648 die("unable to read files to diff");
650 if (mmfile_is_binary(&mf1) || mmfile_is_binary(&mf2))
653 /* Crazy xdl interfaces.. */
658 xpp.flags = XDF_NEED_MINIMAL;
661 ecb.outf = xdiff_outf;
663 xdl_diff(&mf1, &mf2, &xpp, &xecfg, &ecb);
667 static void builtin_checkdiff(const char *name_a, const char *name_b,
668 struct diff_filespec *one,
669 struct diff_filespec *two)
672 struct checkdiff_t data;
677 memset(&data, 0, sizeof(data));
678 data.xm.consume = checkdiff_consume;
679 data.filename = name_b ? name_b : name_a;
682 if (fill_mmfile(&mf1, one) < 0 || fill_mmfile(&mf2, two) < 0)
683 die("unable to read files to diff");
685 if (mmfile_is_binary(&mf2))
688 /* Crazy xdl interfaces.. */
693 xpp.flags = XDF_NEED_MINIMAL;
696 ecb.outf = xdiff_outf;
698 xdl_diff(&mf1, &mf2, &xpp, &xecfg, &ecb);
702 struct diff_filespec *alloc_filespec(const char *path)
704 int namelen = strlen(path);
705 struct diff_filespec *spec = xmalloc(sizeof(*spec) + namelen + 1);
707 memset(spec, 0, sizeof(*spec));
708 spec->path = (char *)(spec + 1);
709 memcpy(spec->path, path, namelen+1);
713 void fill_filespec(struct diff_filespec *spec, const unsigned char *sha1,
717 spec->mode = canon_mode(mode);
718 memcpy(spec->sha1, sha1, 20);
719 spec->sha1_valid = !!memcmp(sha1, null_sha1, 20);
724 * Given a name and sha1 pair, if the dircache tells us the file in
725 * the work tree has that object contents, return true, so that
726 * prepare_temp_file() does not have to inflate and extract.
728 static int work_tree_matches(const char *name, const unsigned char *sha1)
730 struct cache_entry *ce;
734 /* We do not read the cache ourselves here, because the
735 * benchmark with my previous version that always reads cache
736 * shows that it makes things worse for diff-tree comparing
737 * two linux-2.6 kernel trees in an already checked out work
738 * tree. This is because most diff-tree comparisons deal with
739 * only a small number of files, while reading the cache is
740 * expensive for a large project, and its cost outweighs the
741 * savings we get by not inflating the object to a temporary
742 * file. Practically, this code only helps when we are used
743 * by diff-cache --cached, which does read the cache before
750 pos = cache_name_pos(name, len);
753 ce = active_cache[pos];
754 if ((lstat(name, &st) < 0) ||
755 !S_ISREG(st.st_mode) || /* careful! */
756 ce_match_stat(ce, &st, 0) ||
757 memcmp(sha1, ce->sha1, 20))
759 /* we return 1 only when we can stat, it is a regular file,
760 * stat information matches, and sha1 recorded in the cache
761 * matches. I.e. we know the file in the work tree really is
762 * the same as the <name, sha1> pair.
767 static struct sha1_size_cache {
768 unsigned char sha1[20];
771 static int sha1_size_cache_nr, sha1_size_cache_alloc;
773 static struct sha1_size_cache *locate_size_cache(unsigned char *sha1,
778 struct sha1_size_cache *e;
781 last = sha1_size_cache_nr;
782 while (last > first) {
783 int cmp, next = (last + first) >> 1;
784 e = sha1_size_cache[next];
785 cmp = memcmp(e->sha1, sha1, 20);
797 /* insert to make it at "first" */
798 if (sha1_size_cache_alloc <= sha1_size_cache_nr) {
799 sha1_size_cache_alloc = alloc_nr(sha1_size_cache_alloc);
800 sha1_size_cache = xrealloc(sha1_size_cache,
801 sha1_size_cache_alloc *
802 sizeof(*sha1_size_cache));
804 sha1_size_cache_nr++;
805 if (first < sha1_size_cache_nr)
806 memmove(sha1_size_cache + first + 1, sha1_size_cache + first,
807 (sha1_size_cache_nr - first - 1) *
808 sizeof(*sha1_size_cache));
809 e = xmalloc(sizeof(struct sha1_size_cache));
810 sha1_size_cache[first] = e;
811 memcpy(e->sha1, sha1, 20);
817 * While doing rename detection and pickaxe operation, we may need to
818 * grab the data for the blob (or file) for our own in-core comparison.
819 * diff_filespec has data and size fields for this purpose.
821 int diff_populate_filespec(struct diff_filespec *s, int size_only)
824 if (!DIFF_FILE_VALID(s))
825 die("internal error: asking to populate invalid file.");
826 if (S_ISDIR(s->mode))
834 if (!s->sha1_valid ||
835 work_tree_matches(s->path, s->sha1)) {
838 if (lstat(s->path, &st) < 0) {
839 if (errno == ENOENT) {
848 s->size = st.st_size;
853 if (S_ISLNK(st.st_mode)) {
855 s->data = xmalloc(s->size);
857 ret = readlink(s->path, s->data, s->size);
864 fd = open(s->path, O_RDONLY);
867 s->data = mmap(NULL, s->size, PROT_READ, MAP_PRIVATE, fd, 0);
869 if (s->data == MAP_FAILED)
871 s->should_munmap = 1;
875 struct sha1_size_cache *e;
878 e = locate_size_cache(s->sha1, 1, 0);
883 if (!sha1_object_info(s->sha1, type, &s->size))
884 locate_size_cache(s->sha1, 0, s->size);
887 s->data = read_sha1_file(s->sha1, type, &s->size);
894 void diff_free_filespec_data(struct diff_filespec *s)
898 else if (s->should_munmap)
899 munmap(s->data, s->size);
900 s->should_free = s->should_munmap = 0;
906 static void prep_temp_blob(struct diff_tempfile *temp,
909 const unsigned char *sha1,
914 fd = git_mkstemp(temp->tmp_path, TEMPFILE_PATH_LEN, ".diff_XXXXXX");
916 die("unable to create temp-file");
917 if (write(fd, blob, size) != size)
918 die("unable to write temp-file");
920 temp->name = temp->tmp_path;
921 strcpy(temp->hex, sha1_to_hex(sha1));
923 sprintf(temp->mode, "%06o", mode);
926 static void prepare_temp_file(const char *name,
927 struct diff_tempfile *temp,
928 struct diff_filespec *one)
930 if (!DIFF_FILE_VALID(one)) {
932 /* A '-' entry produces this for file-2, and
933 * a '+' entry produces this for file-1.
935 temp->name = "/dev/null";
936 strcpy(temp->hex, ".");
937 strcpy(temp->mode, ".");
941 if (!one->sha1_valid ||
942 work_tree_matches(name, one->sha1)) {
944 if (lstat(name, &st) < 0) {
946 goto not_a_valid_file;
947 die("stat(%s): %s", name, strerror(errno));
949 if (S_ISLNK(st.st_mode)) {
951 char buf[PATH_MAX + 1]; /* ought to be SYMLINK_MAX */
952 if (sizeof(buf) <= st.st_size)
953 die("symlink too long: %s", name);
954 ret = readlink(name, buf, st.st_size);
956 die("readlink(%s)", name);
957 prep_temp_blob(temp, buf, st.st_size,
959 one->sha1 : null_sha1),
961 one->mode : S_IFLNK));
964 /* we can borrow from the file in the work tree */
966 if (!one->sha1_valid)
967 strcpy(temp->hex, sha1_to_hex(null_sha1));
969 strcpy(temp->hex, sha1_to_hex(one->sha1));
970 /* Even though we may sometimes borrow the
971 * contents from the work tree, we always want
972 * one->mode. mode is trustworthy even when
973 * !(one->sha1_valid), as long as
974 * DIFF_FILE_VALID(one).
976 sprintf(temp->mode, "%06o", one->mode);
981 if (diff_populate_filespec(one, 0))
982 die("cannot read data blob for %s", one->path);
983 prep_temp_blob(temp, one->data, one->size,
984 one->sha1, one->mode);
988 static void remove_tempfile(void)
992 for (i = 0; i < 2; i++)
993 if (diff_temp[i].name == diff_temp[i].tmp_path) {
994 unlink(diff_temp[i].name);
995 diff_temp[i].name = NULL;
999 static void remove_tempfile_on_signal(int signo)
1002 signal(SIGINT, SIG_DFL);
1006 static int spawn_prog(const char *pgm, const char **arg)
1014 die("unable to fork");
1016 execvp(pgm, (char *const*) arg);
1020 while (waitpid(pid, &status, 0) < 0) {
1026 /* Earlier we did not check the exit status because
1027 * diff exits non-zero if files are different, and
1028 * we are not interested in knowing that. It was a
1029 * mistake which made it harder to quit a diff-*
1030 * session that uses the git-apply-patch-script as
1031 * the GIT_EXTERNAL_DIFF. A custom GIT_EXTERNAL_DIFF
1032 * should also exit non-zero only when it wants to
1033 * abort the entire diff-* session.
1035 if (WIFEXITED(status) && !WEXITSTATUS(status))
1040 /* An external diff command takes:
1042 * diff-cmd name infile1 infile1-sha1 infile1-mode \
1043 * infile2 infile2-sha1 infile2-mode [ rename-to ]
1046 static void run_external_diff(const char *pgm,
1049 struct diff_filespec *one,
1050 struct diff_filespec *two,
1051 const char *xfrm_msg,
1052 int complete_rewrite)
1054 const char *spawn_arg[10];
1055 struct diff_tempfile *temp = diff_temp;
1057 static int atexit_asked = 0;
1058 const char *othername;
1059 const char **arg = &spawn_arg[0];
1061 othername = (other? other : name);
1063 prepare_temp_file(name, &temp[0], one);
1064 prepare_temp_file(othername, &temp[1], two);
1065 if (! atexit_asked &&
1066 (temp[0].name == temp[0].tmp_path ||
1067 temp[1].name == temp[1].tmp_path)) {
1069 atexit(remove_tempfile);
1071 signal(SIGINT, remove_tempfile_on_signal);
1077 *arg++ = temp[0].name;
1078 *arg++ = temp[0].hex;
1079 *arg++ = temp[0].mode;
1080 *arg++ = temp[1].name;
1081 *arg++ = temp[1].hex;
1082 *arg++ = temp[1].mode;
1092 retval = spawn_prog(pgm, spawn_arg);
1095 fprintf(stderr, "external diff died, stopping at %s.\n", name);
1100 static void run_diff_cmd(const char *pgm,
1103 struct diff_filespec *one,
1104 struct diff_filespec *two,
1105 const char *xfrm_msg,
1106 struct diff_options *o,
1107 int complete_rewrite)
1110 run_external_diff(pgm, name, other, one, two, xfrm_msg,
1115 builtin_diff(name, other ? other : name,
1116 one, two, xfrm_msg, o, complete_rewrite);
1118 printf("* Unmerged path %s\n", name);
1121 static void diff_fill_sha1_info(struct diff_filespec *one)
1123 if (DIFF_FILE_VALID(one)) {
1124 if (!one->sha1_valid) {
1126 if (lstat(one->path, &st) < 0)
1127 die("stat %s", one->path);
1128 if (index_path(one->sha1, one->path, &st, 0))
1129 die("cannot hash %s\n", one->path);
1133 memset(one->sha1, 0, 20);
1136 static void run_diff(struct diff_filepair *p, struct diff_options *o)
1138 const char *pgm = external_diff();
1139 char msg[PATH_MAX*2+300], *xfrm_msg;
1140 struct diff_filespec *one;
1141 struct diff_filespec *two;
1144 char *name_munged, *other_munged;
1145 int complete_rewrite = 0;
1148 if (DIFF_PAIR_UNMERGED(p)) {
1150 run_diff_cmd(pgm, p->one->path, NULL, NULL, NULL, NULL, o, 0);
1154 name = p->one->path;
1155 other = (strcmp(name, p->two->path) ? p->two->path : NULL);
1156 name_munged = quote_one(name);
1157 other_munged = quote_one(other);
1158 one = p->one; two = p->two;
1160 diff_fill_sha1_info(one);
1161 diff_fill_sha1_info(two);
1164 switch (p->status) {
1165 case DIFF_STATUS_COPIED:
1166 len += snprintf(msg + len, sizeof(msg) - len,
1167 "similarity index %d%%\n"
1170 (int)(0.5 + p->score * 100.0/MAX_SCORE),
1171 name_munged, other_munged);
1173 case DIFF_STATUS_RENAMED:
1174 len += snprintf(msg + len, sizeof(msg) - len,
1175 "similarity index %d%%\n"
1178 (int)(0.5 + p->score * 100.0/MAX_SCORE),
1179 name_munged, other_munged);
1181 case DIFF_STATUS_MODIFIED:
1183 len += snprintf(msg + len, sizeof(msg) - len,
1184 "dissimilarity index %d%%\n",
1185 (int)(0.5 + p->score *
1187 complete_rewrite = 1;
1196 if (memcmp(one->sha1, two->sha1, 20)) {
1197 int abbrev = o->full_index ? 40 : DEFAULT_ABBREV;
1199 len += snprintf(msg + len, sizeof(msg) - len,
1201 abbrev, sha1_to_hex(one->sha1),
1202 abbrev, sha1_to_hex(two->sha1));
1203 if (one->mode == two->mode)
1204 len += snprintf(msg + len, sizeof(msg) - len,
1205 " %06o", one->mode);
1206 len += snprintf(msg + len, sizeof(msg) - len, "\n");
1211 xfrm_msg = len ? msg : NULL;
1214 DIFF_FILE_VALID(one) && DIFF_FILE_VALID(two) &&
1215 (S_IFMT & one->mode) != (S_IFMT & two->mode)) {
1216 /* a filepair that changes between file and symlink
1217 * needs to be split into deletion and creation.
1219 struct diff_filespec *null = alloc_filespec(two->path);
1220 run_diff_cmd(NULL, name, other, one, null, xfrm_msg, o, 0);
1222 null = alloc_filespec(one->path);
1223 run_diff_cmd(NULL, name, other, null, two, xfrm_msg, o, 0);
1227 run_diff_cmd(pgm, name, other, one, two, xfrm_msg, o,
1234 static void run_diffstat(struct diff_filepair *p, struct diff_options *o,
1235 struct diffstat_t *diffstat)
1239 int complete_rewrite = 0;
1241 if (DIFF_PAIR_UNMERGED(p)) {
1243 builtin_diffstat(p->one->path, NULL, NULL, NULL, diffstat, 0);
1247 name = p->one->path;
1248 other = (strcmp(name, p->two->path) ? p->two->path : NULL);
1250 diff_fill_sha1_info(p->one);
1251 diff_fill_sha1_info(p->two);
1253 if (p->status == DIFF_STATUS_MODIFIED && p->score)
1254 complete_rewrite = 1;
1255 builtin_diffstat(name, other, p->one, p->two, diffstat, complete_rewrite);
1258 static void run_checkdiff(struct diff_filepair *p, struct diff_options *o)
1263 if (DIFF_PAIR_UNMERGED(p)) {
1268 name = p->one->path;
1269 other = (strcmp(name, p->two->path) ? p->two->path : NULL);
1271 diff_fill_sha1_info(p->one);
1272 diff_fill_sha1_info(p->two);
1274 builtin_checkdiff(name, other, p->one, p->two);
1277 void diff_setup(struct diff_options *options)
1279 memset(options, 0, sizeof(*options));
1280 options->output_format = DIFF_FORMAT_RAW;
1281 options->line_termination = '\n';
1282 options->break_opt = -1;
1283 options->rename_limit = -1;
1284 options->context = 3;
1286 options->change = diff_change;
1287 options->add_remove = diff_addremove;
1290 int diff_setup_done(struct diff_options *options)
1292 if ((options->find_copies_harder &&
1293 options->detect_rename != DIFF_DETECT_COPY) ||
1294 (0 <= options->rename_limit && !options->detect_rename))
1298 * These cases always need recursive; we do not drop caller-supplied
1299 * recursive bits for other formats here.
1301 if ((options->output_format == DIFF_FORMAT_PATCH) ||
1302 (options->output_format == DIFF_FORMAT_DIFFSTAT) ||
1303 (options->output_format == DIFF_FORMAT_CHECKDIFF))
1304 options->recursive = 1;
1307 * These combinations do not make sense.
1309 if (options->output_format == DIFF_FORMAT_RAW)
1310 options->with_raw = 0;
1311 if (options->output_format == DIFF_FORMAT_DIFFSTAT)
1312 options->with_stat = 0;
1314 if (options->detect_rename && options->rename_limit < 0)
1315 options->rename_limit = diff_rename_limit_default;
1316 if (options->setup & DIFF_SETUP_USE_CACHE) {
1318 /* read-cache does not die even when it fails
1319 * so it is safe for us to do this here. Also
1320 * it does not smudge active_cache or active_nr
1321 * when it fails, so we do not have to worry about
1322 * cleaning it up ourselves either.
1326 if (options->setup & DIFF_SETUP_USE_SIZE_CACHE)
1328 if (options->abbrev <= 0 || 40 < options->abbrev)
1329 options->abbrev = 40; /* full */
1334 int opt_arg(const char *arg, int arg_short, const char *arg_long, int *val)
1344 if (c == arg_short) {
1348 if (val && isdigit(c)) {
1350 int n = strtoul(arg, &end, 10);
1361 eq = strchr(arg, '=');
1366 if (!len || strncmp(arg, arg_long, len))
1371 if (!isdigit(*++eq))
1373 n = strtoul(eq, &end, 10);
1381 int diff_opt_parse(struct diff_options *options, const char **av, int ac)
1383 const char *arg = av[0];
1384 if (!strcmp(arg, "-p") || !strcmp(arg, "-u"))
1385 options->output_format = DIFF_FORMAT_PATCH;
1386 else if (opt_arg(arg, 'U', "unified", &options->context))
1387 options->output_format = DIFF_FORMAT_PATCH;
1388 else if (!strcmp(arg, "--patch-with-raw")) {
1389 options->output_format = DIFF_FORMAT_PATCH;
1390 options->with_raw = 1;
1392 else if (!strcmp(arg, "--stat"))
1393 options->output_format = DIFF_FORMAT_DIFFSTAT;
1394 else if (!strcmp(arg, "--check"))
1395 options->output_format = DIFF_FORMAT_CHECKDIFF;
1396 else if (!strcmp(arg, "--summary"))
1397 options->summary = 1;
1398 else if (!strcmp(arg, "--patch-with-stat")) {
1399 options->output_format = DIFF_FORMAT_PATCH;
1400 options->with_stat = 1;
1402 else if (!strcmp(arg, "-z"))
1403 options->line_termination = 0;
1404 else if (!strncmp(arg, "-l", 2))
1405 options->rename_limit = strtoul(arg+2, NULL, 10);
1406 else if (!strcmp(arg, "--full-index"))
1407 options->full_index = 1;
1408 else if (!strcmp(arg, "--binary")) {
1409 options->output_format = DIFF_FORMAT_PATCH;
1410 options->full_index = options->binary = 1;
1412 else if (!strcmp(arg, "--name-only"))
1413 options->output_format = DIFF_FORMAT_NAME;
1414 else if (!strcmp(arg, "--name-status"))
1415 options->output_format = DIFF_FORMAT_NAME_STATUS;
1416 else if (!strcmp(arg, "-R"))
1417 options->reverse_diff = 1;
1418 else if (!strncmp(arg, "-S", 2))
1419 options->pickaxe = arg + 2;
1420 else if (!strcmp(arg, "-s"))
1421 options->output_format = DIFF_FORMAT_NO_OUTPUT;
1422 else if (!strncmp(arg, "-O", 2))
1423 options->orderfile = arg + 2;
1424 else if (!strncmp(arg, "--diff-filter=", 14))
1425 options->filter = arg + 14;
1426 else if (!strcmp(arg, "--pickaxe-all"))
1427 options->pickaxe_opts = DIFF_PICKAXE_ALL;
1428 else if (!strcmp(arg, "--pickaxe-regex"))
1429 options->pickaxe_opts = DIFF_PICKAXE_REGEX;
1430 else if (!strncmp(arg, "-B", 2)) {
1431 if ((options->break_opt =
1432 diff_scoreopt_parse(arg)) == -1)
1435 else if (!strncmp(arg, "-M", 2)) {
1436 if ((options->rename_score =
1437 diff_scoreopt_parse(arg)) == -1)
1439 options->detect_rename = DIFF_DETECT_RENAME;
1441 else if (!strncmp(arg, "-C", 2)) {
1442 if ((options->rename_score =
1443 diff_scoreopt_parse(arg)) == -1)
1445 options->detect_rename = DIFF_DETECT_COPY;
1447 else if (!strcmp(arg, "--find-copies-harder"))
1448 options->find_copies_harder = 1;
1449 else if (!strcmp(arg, "--abbrev"))
1450 options->abbrev = DEFAULT_ABBREV;
1451 else if (!strncmp(arg, "--abbrev=", 9)) {
1452 options->abbrev = strtoul(arg + 9, NULL, 10);
1453 if (options->abbrev < MINIMUM_ABBREV)
1454 options->abbrev = MINIMUM_ABBREV;
1455 else if (40 < options->abbrev)
1456 options->abbrev = 40;
1463 static int parse_num(const char **cp_p)
1465 unsigned long num, scale;
1467 const char *cp = *cp_p;
1474 if ( !dot && ch == '.' ) {
1477 } else if ( ch == '%' ) {
1478 scale = dot ? scale*100 : 100;
1479 cp++; /* % is always at the end */
1481 } else if ( ch >= '0' && ch <= '9' ) {
1482 if ( scale < 100000 ) {
1484 num = (num*10) + (ch-'0');
1493 /* user says num divided by scale and we say internally that
1494 * is MAX_SCORE * num / scale.
1496 return (num >= scale) ? MAX_SCORE : (MAX_SCORE * num / scale);
1499 int diff_scoreopt_parse(const char *opt)
1501 int opt1, opt2, cmd;
1506 if (cmd != 'M' && cmd != 'C' && cmd != 'B')
1507 return -1; /* that is not a -M, -C nor -B option */
1509 opt1 = parse_num(&opt);
1515 else if (*opt != '/')
1516 return -1; /* we expect -B80/99 or -B80 */
1519 opt2 = parse_num(&opt);
1524 return opt1 | (opt2 << 16);
1527 struct diff_queue_struct diff_queued_diff;
1529 void diff_q(struct diff_queue_struct *queue, struct diff_filepair *dp)
1531 if (queue->alloc <= queue->nr) {
1532 queue->alloc = alloc_nr(queue->alloc);
1533 queue->queue = xrealloc(queue->queue,
1534 sizeof(dp) * queue->alloc);
1536 queue->queue[queue->nr++] = dp;
1539 struct diff_filepair *diff_queue(struct diff_queue_struct *queue,
1540 struct diff_filespec *one,
1541 struct diff_filespec *two)
1543 struct diff_filepair *dp = xmalloc(sizeof(*dp));
1548 dp->source_stays = 0;
1549 dp->broken_pair = 0;
1555 void diff_free_filepair(struct diff_filepair *p)
1557 diff_free_filespec_data(p->one);
1558 diff_free_filespec_data(p->two);
1564 /* This is different from find_unique_abbrev() in that
1565 * it stuffs the result with dots for alignment.
1567 const char *diff_unique_abbrev(const unsigned char *sha1, int len)
1572 return sha1_to_hex(sha1);
1574 abbrev = find_unique_abbrev(sha1, len);
1576 return sha1_to_hex(sha1);
1577 abblen = strlen(abbrev);
1579 static char hex[41];
1580 if (len < abblen && abblen <= len + 2)
1581 sprintf(hex, "%s%.*s", abbrev, len+3-abblen, "..");
1583 sprintf(hex, "%s...", abbrev);
1586 return sha1_to_hex(sha1);
1589 static void diff_flush_raw(struct diff_filepair *p,
1590 int line_termination,
1591 int inter_name_termination,
1592 struct diff_options *options,
1597 int abbrev = options->abbrev;
1598 const char *path_one, *path_two;
1600 path_one = p->one->path;
1601 path_two = p->two->path;
1602 if (line_termination) {
1603 path_one = quote_one(path_one);
1604 path_two = quote_one(path_two);
1608 sprintf(status, "%c%03d", p->status,
1609 (int)(0.5 + p->score * 100.0/MAX_SCORE));
1611 status[0] = p->status;
1614 switch (p->status) {
1615 case DIFF_STATUS_COPIED:
1616 case DIFF_STATUS_RENAMED:
1619 case DIFF_STATUS_ADDED:
1620 case DIFF_STATUS_DELETED:
1627 if (output_format != DIFF_FORMAT_NAME_STATUS) {
1628 printf(":%06o %06o %s ",
1629 p->one->mode, p->two->mode,
1630 diff_unique_abbrev(p->one->sha1, abbrev));
1632 diff_unique_abbrev(p->two->sha1, abbrev));
1634 printf("%s%c%s", status, inter_name_termination, path_one);
1636 printf("%c%s", inter_name_termination, path_two);
1637 putchar(line_termination);
1638 if (path_one != p->one->path)
1639 free((void*)path_one);
1640 if (path_two != p->two->path)
1641 free((void*)path_two);
1644 static void diff_flush_name(struct diff_filepair *p,
1645 int inter_name_termination,
1646 int line_termination)
1648 char *path = p->two->path;
1650 if (line_termination)
1651 path = quote_one(p->two->path);
1653 path = p->two->path;
1654 printf("%s%c", path, line_termination);
1655 if (p->two->path != path)
1659 int diff_unmodified_pair(struct diff_filepair *p)
1661 /* This function is written stricter than necessary to support
1662 * the currently implemented transformers, but the idea is to
1663 * let transformers to produce diff_filepairs any way they want,
1664 * and filter and clean them up here before producing the output.
1666 struct diff_filespec *one, *two;
1668 if (DIFF_PAIR_UNMERGED(p))
1669 return 0; /* unmerged is interesting */
1674 /* deletion, addition, mode or type change
1675 * and rename are all interesting.
1677 if (DIFF_FILE_VALID(one) != DIFF_FILE_VALID(two) ||
1678 DIFF_PAIR_MODE_CHANGED(p) ||
1679 strcmp(one->path, two->path))
1682 /* both are valid and point at the same path. that is, we are
1683 * dealing with a change.
1685 if (one->sha1_valid && two->sha1_valid &&
1686 !memcmp(one->sha1, two->sha1, sizeof(one->sha1)))
1687 return 1; /* no change */
1688 if (!one->sha1_valid && !two->sha1_valid)
1689 return 1; /* both look at the same file on the filesystem. */
1693 static void diff_flush_patch(struct diff_filepair *p, struct diff_options *o)
1695 if (diff_unmodified_pair(p))
1698 if ((DIFF_FILE_VALID(p->one) && S_ISDIR(p->one->mode)) ||
1699 (DIFF_FILE_VALID(p->two) && S_ISDIR(p->two->mode)))
1700 return; /* no tree diffs in patch format */
1705 static void diff_flush_stat(struct diff_filepair *p, struct diff_options *o,
1706 struct diffstat_t *diffstat)
1708 if (diff_unmodified_pair(p))
1711 if ((DIFF_FILE_VALID(p->one) && S_ISDIR(p->one->mode)) ||
1712 (DIFF_FILE_VALID(p->two) && S_ISDIR(p->two->mode)))
1713 return; /* no tree diffs in patch format */
1715 run_diffstat(p, o, diffstat);
1718 static void diff_flush_checkdiff(struct diff_filepair *p,
1719 struct diff_options *o)
1721 if (diff_unmodified_pair(p))
1724 if ((DIFF_FILE_VALID(p->one) && S_ISDIR(p->one->mode)) ||
1725 (DIFF_FILE_VALID(p->two) && S_ISDIR(p->two->mode)))
1726 return; /* no tree diffs in patch format */
1728 run_checkdiff(p, o);
1731 int diff_queue_is_empty(void)
1733 struct diff_queue_struct *q = &diff_queued_diff;
1735 for (i = 0; i < q->nr; i++)
1736 if (!diff_unmodified_pair(q->queue[i]))
1742 void diff_debug_filespec(struct diff_filespec *s, int x, const char *one)
1744 fprintf(stderr, "queue[%d] %s (%s) %s %06o %s\n",
1747 DIFF_FILE_VALID(s) ? "valid" : "invalid",
1749 s->sha1_valid ? sha1_to_hex(s->sha1) : "");
1750 fprintf(stderr, "queue[%d] %s size %lu flags %d\n",
1752 s->size, s->xfrm_flags);
1755 void diff_debug_filepair(const struct diff_filepair *p, int i)
1757 diff_debug_filespec(p->one, i, "one");
1758 diff_debug_filespec(p->two, i, "two");
1759 fprintf(stderr, "score %d, status %c stays %d broken %d\n",
1760 p->score, p->status ? p->status : '?',
1761 p->source_stays, p->broken_pair);
1764 void diff_debug_queue(const char *msg, struct diff_queue_struct *q)
1768 fprintf(stderr, "%s\n", msg);
1769 fprintf(stderr, "q->nr = %d\n", q->nr);
1770 for (i = 0; i < q->nr; i++) {
1771 struct diff_filepair *p = q->queue[i];
1772 diff_debug_filepair(p, i);
1777 static void diff_resolve_rename_copy(void)
1780 struct diff_filepair *p, *pp;
1781 struct diff_queue_struct *q = &diff_queued_diff;
1783 diff_debug_queue("resolve-rename-copy", q);
1785 for (i = 0; i < q->nr; i++) {
1787 p->status = 0; /* undecided */
1788 if (DIFF_PAIR_UNMERGED(p))
1789 p->status = DIFF_STATUS_UNMERGED;
1790 else if (!DIFF_FILE_VALID(p->one))
1791 p->status = DIFF_STATUS_ADDED;
1792 else if (!DIFF_FILE_VALID(p->two))
1793 p->status = DIFF_STATUS_DELETED;
1794 else if (DIFF_PAIR_TYPE_CHANGED(p))
1795 p->status = DIFF_STATUS_TYPE_CHANGED;
1797 /* from this point on, we are dealing with a pair
1798 * whose both sides are valid and of the same type, i.e.
1799 * either in-place edit or rename/copy edit.
1801 else if (DIFF_PAIR_RENAME(p)) {
1802 if (p->source_stays) {
1803 p->status = DIFF_STATUS_COPIED;
1806 /* See if there is some other filepair that
1807 * copies from the same source as us. If so
1808 * we are a copy. Otherwise we are either a
1809 * copy if the path stays, or a rename if it
1810 * does not, but we already handled "stays" case.
1812 for (j = i + 1; j < q->nr; j++) {
1814 if (strcmp(pp->one->path, p->one->path))
1815 continue; /* not us */
1816 if (!DIFF_PAIR_RENAME(pp))
1817 continue; /* not a rename/copy */
1818 /* pp is a rename/copy from the same source */
1819 p->status = DIFF_STATUS_COPIED;
1823 p->status = DIFF_STATUS_RENAMED;
1825 else if (memcmp(p->one->sha1, p->two->sha1, 20) ||
1826 p->one->mode != p->two->mode)
1827 p->status = DIFF_STATUS_MODIFIED;
1829 /* This is a "no-change" entry and should not
1830 * happen anymore, but prepare for broken callers.
1832 error("feeding unmodified %s to diffcore",
1834 p->status = DIFF_STATUS_UNKNOWN;
1837 diff_debug_queue("resolve-rename-copy done", q);
1840 static void flush_one_pair(struct diff_filepair *p,
1841 int diff_output_format,
1842 struct diff_options *options,
1843 struct diffstat_t *diffstat)
1845 int inter_name_termination = '\t';
1846 int line_termination = options->line_termination;
1847 if (!line_termination)
1848 inter_name_termination = 0;
1850 switch (p->status) {
1851 case DIFF_STATUS_UNKNOWN:
1854 die("internal error in diff-resolve-rename-copy");
1857 switch (diff_output_format) {
1858 case DIFF_FORMAT_DIFFSTAT:
1859 diff_flush_stat(p, options, diffstat);
1861 case DIFF_FORMAT_CHECKDIFF:
1862 diff_flush_checkdiff(p, options);
1864 case DIFF_FORMAT_PATCH:
1865 diff_flush_patch(p, options);
1867 case DIFF_FORMAT_RAW:
1868 case DIFF_FORMAT_NAME_STATUS:
1869 diff_flush_raw(p, line_termination,
1870 inter_name_termination,
1871 options, diff_output_format);
1873 case DIFF_FORMAT_NAME:
1875 inter_name_termination,
1878 case DIFF_FORMAT_NO_OUTPUT:
1884 static void show_file_mode_name(const char *newdelete, struct diff_filespec *fs)
1887 printf(" %s mode %06o %s\n", newdelete, fs->mode, fs->path);
1889 printf(" %s %s\n", newdelete, fs->path);
1893 static void show_mode_change(struct diff_filepair *p, int show_name)
1895 if (p->one->mode && p->two->mode && p->one->mode != p->two->mode) {
1897 printf(" mode change %06o => %06o %s\n",
1898 p->one->mode, p->two->mode, p->two->path);
1900 printf(" mode change %06o => %06o\n",
1901 p->one->mode, p->two->mode);
1905 static void show_rename_copy(const char *renamecopy, struct diff_filepair *p)
1907 const char *old, *new;
1909 /* Find common prefix */
1913 const char *slash_old, *slash_new;
1914 slash_old = strchr(old, '/');
1915 slash_new = strchr(new, '/');
1918 slash_old - old != slash_new - new ||
1919 memcmp(old, new, slash_new - new))
1921 old = slash_old + 1;
1922 new = slash_new + 1;
1924 /* p->one->path thru old is the common prefix, and old and new
1925 * through the end of names are renames
1927 if (old != p->one->path)
1928 printf(" %s %.*s{%s => %s} (%d%%)\n", renamecopy,
1929 (int)(old - p->one->path), p->one->path,
1930 old, new, (int)(0.5 + p->score * 100.0/MAX_SCORE));
1932 printf(" %s %s => %s (%d%%)\n", renamecopy,
1933 p->one->path, p->two->path,
1934 (int)(0.5 + p->score * 100.0/MAX_SCORE));
1935 show_mode_change(p, 0);
1938 static void diff_summary(struct diff_filepair *p)
1941 case DIFF_STATUS_DELETED:
1942 show_file_mode_name("delete", p->one);
1944 case DIFF_STATUS_ADDED:
1945 show_file_mode_name("create", p->two);
1947 case DIFF_STATUS_COPIED:
1948 show_rename_copy("copy", p);
1950 case DIFF_STATUS_RENAMED:
1951 show_rename_copy("rename", p);
1955 printf(" rewrite %s (%d%%)\n", p->two->path,
1956 (int)(0.5 + p->score * 100.0/MAX_SCORE));
1957 show_mode_change(p, 0);
1958 } else show_mode_change(p, 1);
1963 void diff_flush(struct diff_options *options)
1965 struct diff_queue_struct *q = &diff_queued_diff;
1967 int diff_output_format = options->output_format;
1968 struct diffstat_t *diffstat = NULL;
1970 if (diff_output_format == DIFF_FORMAT_DIFFSTAT || options->with_stat) {
1971 diffstat = xcalloc(sizeof (struct diffstat_t), 1);
1972 diffstat->xm.consume = diffstat_consume;
1975 if (options->with_raw) {
1976 for (i = 0; i < q->nr; i++) {
1977 struct diff_filepair *p = q->queue[i];
1978 flush_one_pair(p, DIFF_FORMAT_RAW, options, NULL);
1980 putchar(options->line_termination);
1982 if (options->with_stat) {
1983 for (i = 0; i < q->nr; i++) {
1984 struct diff_filepair *p = q->queue[i];
1985 flush_one_pair(p, DIFF_FORMAT_DIFFSTAT, options,
1988 show_stats(diffstat);
1991 if (options->summary)
1992 for (i = 0; i < q->nr; i++)
1993 diff_summary(q->queue[i]);
1994 putchar(options->line_termination);
1996 for (i = 0; i < q->nr; i++) {
1997 struct diff_filepair *p = q->queue[i];
1998 flush_one_pair(p, diff_output_format, options, diffstat);
2002 show_stats(diffstat);
2006 for (i = 0; i < q->nr; i++) {
2007 if (diffstat && options->summary)
2008 diff_summary(q->queue[i]);
2009 diff_free_filepair(q->queue[i]);
2014 q->nr = q->alloc = 0;
2017 static void diffcore_apply_filter(const char *filter)
2020 struct diff_queue_struct *q = &diff_queued_diff;
2021 struct diff_queue_struct outq;
2023 outq.nr = outq.alloc = 0;
2028 if (strchr(filter, DIFF_STATUS_FILTER_AON)) {
2030 for (i = found = 0; !found && i < q->nr; i++) {
2031 struct diff_filepair *p = q->queue[i];
2032 if (((p->status == DIFF_STATUS_MODIFIED) &&
2034 strchr(filter, DIFF_STATUS_FILTER_BROKEN)) ||
2036 strchr(filter, DIFF_STATUS_MODIFIED)))) ||
2037 ((p->status != DIFF_STATUS_MODIFIED) &&
2038 strchr(filter, p->status)))
2044 /* otherwise we will clear the whole queue
2045 * by copying the empty outq at the end of this
2046 * function, but first clear the current entries
2049 for (i = 0; i < q->nr; i++)
2050 diff_free_filepair(q->queue[i]);
2053 /* Only the matching ones */
2054 for (i = 0; i < q->nr; i++) {
2055 struct diff_filepair *p = q->queue[i];
2057 if (((p->status == DIFF_STATUS_MODIFIED) &&
2059 strchr(filter, DIFF_STATUS_FILTER_BROKEN)) ||
2061 strchr(filter, DIFF_STATUS_MODIFIED)))) ||
2062 ((p->status != DIFF_STATUS_MODIFIED) &&
2063 strchr(filter, p->status)))
2066 diff_free_filepair(p);
2073 void diffcore_std(struct diff_options *options)
2075 if (options->break_opt != -1)
2076 diffcore_break(options->break_opt);
2077 if (options->detect_rename)
2078 diffcore_rename(options);
2079 if (options->break_opt != -1)
2080 diffcore_merge_broken();
2081 if (options->pickaxe)
2082 diffcore_pickaxe(options->pickaxe, options->pickaxe_opts);
2083 if (options->orderfile)
2084 diffcore_order(options->orderfile);
2085 diff_resolve_rename_copy();
2086 diffcore_apply_filter(options->filter);
2090 void diffcore_std_no_resolve(struct diff_options *options)
2092 if (options->pickaxe)
2093 diffcore_pickaxe(options->pickaxe, options->pickaxe_opts);
2094 if (options->orderfile)
2095 diffcore_order(options->orderfile);
2096 diffcore_apply_filter(options->filter);
2099 void diff_addremove(struct diff_options *options,
2100 int addremove, unsigned mode,
2101 const unsigned char *sha1,
2102 const char *base, const char *path)
2104 char concatpath[PATH_MAX];
2105 struct diff_filespec *one, *two;
2107 /* This may look odd, but it is a preparation for
2108 * feeding "there are unchanged files which should
2109 * not produce diffs, but when you are doing copy
2110 * detection you would need them, so here they are"
2111 * entries to the diff-core. They will be prefixed
2112 * with something like '=' or '*' (I haven't decided
2113 * which but should not make any difference).
2114 * Feeding the same new and old to diff_change()
2115 * also has the same effect.
2116 * Before the final output happens, they are pruned after
2117 * merged into rename/copy pairs as appropriate.
2119 if (options->reverse_diff)
2120 addremove = (addremove == '+' ? '-' :
2121 addremove == '-' ? '+' : addremove);
2123 if (!path) path = "";
2124 sprintf(concatpath, "%s%s", base, path);
2125 one = alloc_filespec(concatpath);
2126 two = alloc_filespec(concatpath);
2128 if (addremove != '+')
2129 fill_filespec(one, sha1, mode);
2130 if (addremove != '-')
2131 fill_filespec(two, sha1, mode);
2133 diff_queue(&diff_queued_diff, one, two);
2136 void diff_change(struct diff_options *options,
2137 unsigned old_mode, unsigned new_mode,
2138 const unsigned char *old_sha1,
2139 const unsigned char *new_sha1,
2140 const char *base, const char *path)
2142 char concatpath[PATH_MAX];
2143 struct diff_filespec *one, *two;
2145 if (options->reverse_diff) {
2147 const unsigned char *tmp_c;
2148 tmp = old_mode; old_mode = new_mode; new_mode = tmp;
2149 tmp_c = old_sha1; old_sha1 = new_sha1; new_sha1 = tmp_c;
2151 if (!path) path = "";
2152 sprintf(concatpath, "%s%s", base, path);
2153 one = alloc_filespec(concatpath);
2154 two = alloc_filespec(concatpath);
2155 fill_filespec(one, old_sha1, old_mode);
2156 fill_filespec(two, new_sha1, new_mode);
2158 diff_queue(&diff_queued_diff, one, two);
2161 void diff_unmerge(struct diff_options *options,
2164 struct diff_filespec *one, *two;
2165 one = alloc_filespec(path);
2166 two = alloc_filespec(path);
2167 diff_queue(&diff_queued_diff, one, two);