2 * Copyright (C) 2005 Junio C Hamano
11 #include "xdiff-interface.h"
13 static int use_size_cache;
15 int diff_rename_limit_default = -1;
17 int git_diff_config(const char *var, const char *value)
19 if (!strcmp(var, "diff.renamelimit")) {
20 diff_rename_limit_default = git_config_int(var, value);
24 return git_default_config(var, value);
27 static char *quote_one(const char *str)
34 needlen = quote_c_style(str, NULL, NULL, 0);
37 xp = xmalloc(needlen + 1);
38 quote_c_style(str, xp, NULL, 0);
42 static char *quote_two(const char *one, const char *two)
44 int need_one = quote_c_style(one, NULL, NULL, 1);
45 int need_two = quote_c_style(two, NULL, NULL, 1);
48 if (need_one + need_two) {
49 if (!need_one) need_one = strlen(one);
50 if (!need_two) need_one = strlen(two);
52 xp = xmalloc(need_one + need_two + 3);
54 quote_c_style(one, xp + 1, NULL, 1);
55 quote_c_style(two, xp + need_one + 1, NULL, 1);
56 strcpy(xp + need_one + need_two + 1, "\"");
59 need_one = strlen(one);
60 need_two = strlen(two);
61 xp = xmalloc(need_one + need_two + 1);
63 strcpy(xp + need_one, two);
67 static const char *external_diff(void)
69 static const char *external_diff_cmd = NULL;
70 static int done_preparing = 0;
73 return external_diff_cmd;
74 external_diff_cmd = getenv("GIT_EXTERNAL_DIFF");
76 return external_diff_cmd;
79 #define TEMPFILE_PATH_LEN 50
81 static struct diff_tempfile {
82 const char *name; /* filename external diff should read from */
85 char tmp_path[TEMPFILE_PATH_LEN];
88 static int count_lines(const char *data, int size)
90 int count, ch, completely_empty = 1, nl_just_seen = 0;
101 completely_empty = 0;
104 if (completely_empty)
107 count++; /* no trailing newline */
111 static void print_line_count(int count)
121 printf("1,%d", count);
126 static void copy_file(int prefix, const char *data, int size)
128 int ch, nl_just_seen = 1;
140 printf("\n\\ No newline at end of file\n");
143 static void emit_rewrite_diff(const char *name_a,
145 struct diff_filespec *one,
146 struct diff_filespec *two)
149 diff_populate_filespec(one, 0);
150 diff_populate_filespec(two, 0);
151 lc_a = count_lines(one->data, one->size);
152 lc_b = count_lines(two->data, two->size);
153 printf("--- %s\n+++ %s\n@@ -", name_a, name_b);
154 print_line_count(lc_a);
156 print_line_count(lc_b);
159 copy_file('-', one->data, one->size);
161 copy_file('+', two->data, two->size);
164 static int fill_mmfile(mmfile_t *mf, struct diff_filespec *one)
166 if (!DIFF_FILE_VALID(one)) {
167 mf->ptr = ""; /* does not matter */
171 else if (diff_populate_filespec(one, 0))
174 mf->size = one->size;
178 struct emit_callback {
179 const char **label_path;
182 static int fn_out(void *priv, mmbuffer_t *mb, int nbuf)
185 struct emit_callback *ecbdata = priv;
187 if (ecbdata->label_path[0]) {
188 printf("--- %s\n", ecbdata->label_path[0]);
189 printf("+++ %s\n", ecbdata->label_path[1]);
190 ecbdata->label_path[0] = ecbdata->label_path[1] = NULL;
192 for (i = 0; i < nbuf; i++)
193 if (!fwrite(mb[i].ptr, mb[i].size, 1, stdout))
199 struct xdiff_emit_state xm;
203 struct diffstat_file {
205 unsigned int added, deleted;
209 static struct diffstat_file *diffstat_add(struct diffstat_t *diffstat,
212 struct diffstat_file *x;
213 x = xcalloc(sizeof (*x), 1);
214 if (diffstat->nr == diffstat->alloc) {
215 diffstat->alloc = alloc_nr(diffstat->alloc);
216 diffstat->files = xrealloc(diffstat->files,
217 diffstat->alloc * sizeof(x));
219 diffstat->files[diffstat->nr++] = x;
220 x->name = strdup(name);
224 static void diffstat_consume(void *priv, char *line, unsigned long len)
226 struct diffstat_t *diffstat = priv;
227 struct diffstat_file *x = diffstat->files[diffstat->nr - 1];
231 else if (line[0] == '-')
235 static const char pluses[] = "++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++";
236 static const char minuses[]= "----------------------------------------------------------------------";
238 static void show_stats(struct diffstat_t* data)
241 int i, len, add, del, total, adds = 0, dels = 0;
242 int max, max_change = 0, max_len = 0;
243 int total_files = data->nr;
248 for (i = 0; i < data->nr; i++) {
249 struct diffstat_file *file = data->files[i];
251 if (max_change < file->added + file->deleted)
252 max_change = file->added + file->deleted;
253 len = strlen(file->name);
258 for (i = 0; i < data->nr; i++) {
259 char *name = data->files[i]->name;
260 int added = data->files[i]->added;
261 int deleted = data->files[i]->deleted;
263 if (0 < (len = quote_c_style(name, NULL, NULL, 0))) {
264 char *qname = xmalloc(len + 1);
265 quote_c_style(name, qname, NULL, 0);
267 data->files[i]->name = name = qname;
271 * "scale" the filename
282 slash = strchr(name, '/');
289 * scale the add/delete
297 printf(" %s%-*s | Bin\n", prefix, len, name);
298 goto free_diffstat_file;
299 } else if (added + deleted == 0) {
301 goto free_diffstat_file;
310 if (max_change > 0) {
311 total = (total * max + max_change / 2) / max_change;
312 add = (add * max + max_change / 2) / max_change;
315 printf(" %s%-*s |%5d %.*s%.*s\n", prefix,
316 len, name, added + deleted,
317 add, pluses, del, minuses);
319 free(data->files[i]->name);
320 free(data->files[i]);
323 printf(" %d files changed, %d insertions(+), %d deletions(-)\n",
324 total_files, adds, dels);
327 #define FIRST_FEW_BYTES 8000
328 static int mmfile_is_binary(mmfile_t *mf)
331 if (FIRST_FEW_BYTES < sz)
332 sz = FIRST_FEW_BYTES;
333 if (memchr(mf->ptr, 0, sz))
338 static void builtin_diff(const char *name_a,
340 struct diff_filespec *one,
341 struct diff_filespec *two,
342 const char *xfrm_msg,
343 int complete_rewrite)
349 a_one = quote_two("a/", name_a);
350 b_two = quote_two("b/", name_b);
351 lbl[0] = DIFF_FILE_VALID(one) ? a_one : "/dev/null";
352 lbl[1] = DIFF_FILE_VALID(two) ? b_two : "/dev/null";
353 printf("diff --git %s %s\n", a_one, b_two);
354 if (lbl[0][0] == '/') {
356 printf("new file mode %06o\n", two->mode);
357 if (xfrm_msg && xfrm_msg[0])
360 else if (lbl[1][0] == '/') {
361 printf("deleted file mode %06o\n", one->mode);
362 if (xfrm_msg && xfrm_msg[0])
366 if (one->mode != two->mode) {
367 printf("old mode %06o\n", one->mode);
368 printf("new mode %06o\n", two->mode);
370 if (xfrm_msg && xfrm_msg[0])
373 * we do not run diff between different kind
376 if ((one->mode ^ two->mode) & S_IFMT)
377 goto free_ab_and_return;
378 if (complete_rewrite) {
379 emit_rewrite_diff(name_a, name_b, one, two);
380 goto free_ab_and_return;
384 if (fill_mmfile(&mf1, one) < 0 || fill_mmfile(&mf2, two) < 0)
385 die("unable to read files to diff");
387 if (mmfile_is_binary(&mf1) || mmfile_is_binary(&mf2))
388 printf("Binary files %s and %s differ\n", lbl[0], lbl[1]);
390 /* Crazy xdl interfaces.. */
391 const char *diffopts = getenv("GIT_DIFF_OPTS");
395 struct emit_callback ecbdata;
397 ecbdata.label_path = lbl;
398 xpp.flags = XDF_NEED_MINIMAL;
400 xecfg.flags = XDL_EMIT_FUNCNAMES;
403 else if (!strncmp(diffopts, "--unified=", 10))
404 xecfg.ctxlen = strtoul(diffopts + 10, NULL, 10);
405 else if (!strncmp(diffopts, "-u", 2))
406 xecfg.ctxlen = strtoul(diffopts + 2, NULL, 10);
409 xdl_diff(&mf1, &mf2, &xpp, &xecfg, &ecb);
418 static void builtin_diffstat(const char *name_a, const char *name_b,
419 struct diff_filespec *one, struct diff_filespec *two,
420 struct diffstat_t *diffstat)
423 struct diffstat_file *data;
425 data = diffstat_add(diffstat, name_a ? name_a : name_b);
427 if (fill_mmfile(&mf1, one) < 0 || fill_mmfile(&mf2, two) < 0)
428 die("unable to read files to diff");
430 if (mmfile_is_binary(&mf1) || mmfile_is_binary(&mf2))
433 /* Crazy xdl interfaces.. */
438 xpp.flags = XDF_NEED_MINIMAL;
441 ecb.outf = xdiff_outf;
443 xdl_diff(&mf1, &mf2, &xpp, &xecfg, &ecb);
447 struct diff_filespec *alloc_filespec(const char *path)
449 int namelen = strlen(path);
450 struct diff_filespec *spec = xmalloc(sizeof(*spec) + namelen + 1);
452 memset(spec, 0, sizeof(*spec));
453 spec->path = (char *)(spec + 1);
454 memcpy(spec->path, path, namelen+1);
458 void fill_filespec(struct diff_filespec *spec, const unsigned char *sha1,
462 spec->mode = canon_mode(mode);
463 memcpy(spec->sha1, sha1, 20);
464 spec->sha1_valid = !!memcmp(sha1, null_sha1, 20);
469 * Given a name and sha1 pair, if the dircache tells us the file in
470 * the work tree has that object contents, return true, so that
471 * prepare_temp_file() does not have to inflate and extract.
473 static int work_tree_matches(const char *name, const unsigned char *sha1)
475 struct cache_entry *ce;
479 /* We do not read the cache ourselves here, because the
480 * benchmark with my previous version that always reads cache
481 * shows that it makes things worse for diff-tree comparing
482 * two linux-2.6 kernel trees in an already checked out work
483 * tree. This is because most diff-tree comparisons deal with
484 * only a small number of files, while reading the cache is
485 * expensive for a large project, and its cost outweighs the
486 * savings we get by not inflating the object to a temporary
487 * file. Practically, this code only helps when we are used
488 * by diff-cache --cached, which does read the cache before
495 pos = cache_name_pos(name, len);
498 ce = active_cache[pos];
499 if ((lstat(name, &st) < 0) ||
500 !S_ISREG(st.st_mode) || /* careful! */
501 ce_match_stat(ce, &st, 0) ||
502 memcmp(sha1, ce->sha1, 20))
504 /* we return 1 only when we can stat, it is a regular file,
505 * stat information matches, and sha1 recorded in the cache
506 * matches. I.e. we know the file in the work tree really is
507 * the same as the <name, sha1> pair.
512 static struct sha1_size_cache {
513 unsigned char sha1[20];
516 static int sha1_size_cache_nr, sha1_size_cache_alloc;
518 static struct sha1_size_cache *locate_size_cache(unsigned char *sha1,
523 struct sha1_size_cache *e;
526 last = sha1_size_cache_nr;
527 while (last > first) {
528 int cmp, next = (last + first) >> 1;
529 e = sha1_size_cache[next];
530 cmp = memcmp(e->sha1, sha1, 20);
542 /* insert to make it at "first" */
543 if (sha1_size_cache_alloc <= sha1_size_cache_nr) {
544 sha1_size_cache_alloc = alloc_nr(sha1_size_cache_alloc);
545 sha1_size_cache = xrealloc(sha1_size_cache,
546 sha1_size_cache_alloc *
547 sizeof(*sha1_size_cache));
549 sha1_size_cache_nr++;
550 if (first < sha1_size_cache_nr)
551 memmove(sha1_size_cache + first + 1, sha1_size_cache + first,
552 (sha1_size_cache_nr - first - 1) *
553 sizeof(*sha1_size_cache));
554 e = xmalloc(sizeof(struct sha1_size_cache));
555 sha1_size_cache[first] = e;
556 memcpy(e->sha1, sha1, 20);
562 * While doing rename detection and pickaxe operation, we may need to
563 * grab the data for the blob (or file) for our own in-core comparison.
564 * diff_filespec has data and size fields for this purpose.
566 int diff_populate_filespec(struct diff_filespec *s, int size_only)
569 if (!DIFF_FILE_VALID(s))
570 die("internal error: asking to populate invalid file.");
571 if (S_ISDIR(s->mode))
579 if (!s->sha1_valid ||
580 work_tree_matches(s->path, s->sha1)) {
583 if (lstat(s->path, &st) < 0) {
584 if (errno == ENOENT) {
593 s->size = st.st_size;
598 if (S_ISLNK(st.st_mode)) {
600 s->data = xmalloc(s->size);
602 ret = readlink(s->path, s->data, s->size);
609 fd = open(s->path, O_RDONLY);
612 s->data = mmap(NULL, s->size, PROT_READ, MAP_PRIVATE, fd, 0);
614 if (s->data == MAP_FAILED)
616 s->should_munmap = 1;
620 struct sha1_size_cache *e;
623 e = locate_size_cache(s->sha1, 1, 0);
628 if (!sha1_object_info(s->sha1, type, &s->size))
629 locate_size_cache(s->sha1, 0, s->size);
632 s->data = read_sha1_file(s->sha1, type, &s->size);
639 void diff_free_filespec_data(struct diff_filespec *s)
643 else if (s->should_munmap)
644 munmap(s->data, s->size);
645 s->should_free = s->should_munmap = 0;
651 static void prep_temp_blob(struct diff_tempfile *temp,
654 const unsigned char *sha1,
659 fd = git_mkstemp(temp->tmp_path, TEMPFILE_PATH_LEN, ".diff_XXXXXX");
661 die("unable to create temp-file");
662 if (write(fd, blob, size) != size)
663 die("unable to write temp-file");
665 temp->name = temp->tmp_path;
666 strcpy(temp->hex, sha1_to_hex(sha1));
668 sprintf(temp->mode, "%06o", mode);
671 static void prepare_temp_file(const char *name,
672 struct diff_tempfile *temp,
673 struct diff_filespec *one)
675 if (!DIFF_FILE_VALID(one)) {
677 /* A '-' entry produces this for file-2, and
678 * a '+' entry produces this for file-1.
680 temp->name = "/dev/null";
681 strcpy(temp->hex, ".");
682 strcpy(temp->mode, ".");
686 if (!one->sha1_valid ||
687 work_tree_matches(name, one->sha1)) {
689 if (lstat(name, &st) < 0) {
691 goto not_a_valid_file;
692 die("stat(%s): %s", name, strerror(errno));
694 if (S_ISLNK(st.st_mode)) {
696 char buf[PATH_MAX + 1]; /* ought to be SYMLINK_MAX */
697 if (sizeof(buf) <= st.st_size)
698 die("symlink too long: %s", name);
699 ret = readlink(name, buf, st.st_size);
701 die("readlink(%s)", name);
702 prep_temp_blob(temp, buf, st.st_size,
704 one->sha1 : null_sha1),
706 one->mode : S_IFLNK));
709 /* we can borrow from the file in the work tree */
711 if (!one->sha1_valid)
712 strcpy(temp->hex, sha1_to_hex(null_sha1));
714 strcpy(temp->hex, sha1_to_hex(one->sha1));
715 /* Even though we may sometimes borrow the
716 * contents from the work tree, we always want
717 * one->mode. mode is trustworthy even when
718 * !(one->sha1_valid), as long as
719 * DIFF_FILE_VALID(one).
721 sprintf(temp->mode, "%06o", one->mode);
726 if (diff_populate_filespec(one, 0))
727 die("cannot read data blob for %s", one->path);
728 prep_temp_blob(temp, one->data, one->size,
729 one->sha1, one->mode);
733 static void remove_tempfile(void)
737 for (i = 0; i < 2; i++)
738 if (diff_temp[i].name == diff_temp[i].tmp_path) {
739 unlink(diff_temp[i].name);
740 diff_temp[i].name = NULL;
744 static void remove_tempfile_on_signal(int signo)
747 signal(SIGINT, SIG_DFL);
751 static int spawn_prog(const char *pgm, const char **arg)
759 die("unable to fork");
761 execvp(pgm, (char *const*) arg);
765 while (waitpid(pid, &status, 0) < 0) {
771 /* Earlier we did not check the exit status because
772 * diff exits non-zero if files are different, and
773 * we are not interested in knowing that. It was a
774 * mistake which made it harder to quit a diff-*
775 * session that uses the git-apply-patch-script as
776 * the GIT_EXTERNAL_DIFF. A custom GIT_EXTERNAL_DIFF
777 * should also exit non-zero only when it wants to
778 * abort the entire diff-* session.
780 if (WIFEXITED(status) && !WEXITSTATUS(status))
785 /* An external diff command takes:
787 * diff-cmd name infile1 infile1-sha1 infile1-mode \
788 * infile2 infile2-sha1 infile2-mode [ rename-to ]
791 static void run_external_diff(const char *pgm,
794 struct diff_filespec *one,
795 struct diff_filespec *two,
796 const char *xfrm_msg,
797 int complete_rewrite)
799 const char *spawn_arg[10];
800 struct diff_tempfile *temp = diff_temp;
802 static int atexit_asked = 0;
803 const char *othername;
804 const char **arg = &spawn_arg[0];
806 othername = (other? other : name);
808 prepare_temp_file(name, &temp[0], one);
809 prepare_temp_file(othername, &temp[1], two);
810 if (! atexit_asked &&
811 (temp[0].name == temp[0].tmp_path ||
812 temp[1].name == temp[1].tmp_path)) {
814 atexit(remove_tempfile);
816 signal(SIGINT, remove_tempfile_on_signal);
822 *arg++ = temp[0].name;
823 *arg++ = temp[0].hex;
824 *arg++ = temp[0].mode;
825 *arg++ = temp[1].name;
826 *arg++ = temp[1].hex;
827 *arg++ = temp[1].mode;
837 retval = spawn_prog(pgm, spawn_arg);
840 fprintf(stderr, "external diff died, stopping at %s.\n", name);
845 static void run_diff_cmd(const char *pgm,
848 struct diff_filespec *one,
849 struct diff_filespec *two,
850 const char *xfrm_msg,
851 int complete_rewrite)
854 run_external_diff(pgm, name, other, one, two, xfrm_msg,
859 builtin_diff(name, other ? other : name,
860 one, two, xfrm_msg, complete_rewrite);
862 printf("* Unmerged path %s\n", name);
865 static void diff_fill_sha1_info(struct diff_filespec *one)
867 if (DIFF_FILE_VALID(one)) {
868 if (!one->sha1_valid) {
870 if (lstat(one->path, &st) < 0)
871 die("stat %s", one->path);
872 if (index_path(one->sha1, one->path, &st, 0))
873 die("cannot hash %s\n", one->path);
877 memset(one->sha1, 0, 20);
880 static void run_diff(struct diff_filepair *p, struct diff_options *o)
882 const char *pgm = external_diff();
883 char msg[PATH_MAX*2+300], *xfrm_msg;
884 struct diff_filespec *one;
885 struct diff_filespec *two;
888 char *name_munged, *other_munged;
889 int complete_rewrite = 0;
892 if (DIFF_PAIR_UNMERGED(p)) {
894 run_diff_cmd(pgm, p->one->path, NULL, NULL, NULL, NULL, 0);
899 other = (strcmp(name, p->two->path) ? p->two->path : NULL);
900 name_munged = quote_one(name);
901 other_munged = quote_one(other);
902 one = p->one; two = p->two;
904 diff_fill_sha1_info(one);
905 diff_fill_sha1_info(two);
909 case DIFF_STATUS_COPIED:
910 len += snprintf(msg + len, sizeof(msg) - len,
911 "similarity index %d%%\n"
914 (int)(0.5 + p->score * 100.0/MAX_SCORE),
915 name_munged, other_munged);
917 case DIFF_STATUS_RENAMED:
918 len += snprintf(msg + len, sizeof(msg) - len,
919 "similarity index %d%%\n"
922 (int)(0.5 + p->score * 100.0/MAX_SCORE),
923 name_munged, other_munged);
925 case DIFF_STATUS_MODIFIED:
927 len += snprintf(msg + len, sizeof(msg) - len,
928 "dissimilarity index %d%%\n",
929 (int)(0.5 + p->score *
931 complete_rewrite = 1;
940 if (memcmp(one->sha1, two->sha1, 20)) {
942 int abbrev = o->full_index ? 40 : DEFAULT_ABBREV;
943 memcpy(one_sha1, sha1_to_hex(one->sha1), 41);
945 len += snprintf(msg + len, sizeof(msg) - len,
947 abbrev, one_sha1, abbrev,
948 sha1_to_hex(two->sha1));
949 if (one->mode == two->mode)
950 len += snprintf(msg + len, sizeof(msg) - len,
952 len += snprintf(msg + len, sizeof(msg) - len, "\n");
957 xfrm_msg = len ? msg : NULL;
960 DIFF_FILE_VALID(one) && DIFF_FILE_VALID(two) &&
961 (S_IFMT & one->mode) != (S_IFMT & two->mode)) {
962 /* a filepair that changes between file and symlink
963 * needs to be split into deletion and creation.
965 struct diff_filespec *null = alloc_filespec(two->path);
966 run_diff_cmd(NULL, name, other, one, null, xfrm_msg, 0);
968 null = alloc_filespec(one->path);
969 run_diff_cmd(NULL, name, other, null, two, xfrm_msg, 0);
973 run_diff_cmd(pgm, name, other, one, two, xfrm_msg,
980 static void run_diffstat(struct diff_filepair *p, struct diff_options *o,
981 struct diffstat_t *diffstat)
986 if (DIFF_PAIR_UNMERGED(p)) {
988 builtin_diffstat(p->one->path, NULL, NULL, NULL, diffstat);
993 other = (strcmp(name, p->two->path) ? p->two->path : NULL);
995 diff_fill_sha1_info(p->one);
996 diff_fill_sha1_info(p->two);
998 builtin_diffstat(name, other, p->one, p->two, diffstat);
1001 void diff_setup(struct diff_options *options)
1003 memset(options, 0, sizeof(*options));
1004 options->output_format = DIFF_FORMAT_RAW;
1005 options->line_termination = '\n';
1006 options->break_opt = -1;
1007 options->rename_limit = -1;
1009 options->change = diff_change;
1010 options->add_remove = diff_addremove;
1013 int diff_setup_done(struct diff_options *options)
1015 if ((options->find_copies_harder &&
1016 options->detect_rename != DIFF_DETECT_COPY) ||
1017 (0 <= options->rename_limit && !options->detect_rename))
1019 if (options->detect_rename && options->rename_limit < 0)
1020 options->rename_limit = diff_rename_limit_default;
1021 if (options->setup & DIFF_SETUP_USE_CACHE) {
1023 /* read-cache does not die even when it fails
1024 * so it is safe for us to do this here. Also
1025 * it does not smudge active_cache or active_nr
1026 * when it fails, so we do not have to worry about
1027 * cleaning it up ourselves either.
1031 if (options->setup & DIFF_SETUP_USE_SIZE_CACHE)
1033 if (options->abbrev <= 0 || 40 < options->abbrev)
1034 options->abbrev = 40; /* full */
1039 int diff_opt_parse(struct diff_options *options, const char **av, int ac)
1041 const char *arg = av[0];
1042 if (!strcmp(arg, "-p") || !strcmp(arg, "-u"))
1043 options->output_format = DIFF_FORMAT_PATCH;
1044 else if (!strcmp(arg, "--patch-with-raw")) {
1045 options->output_format = DIFF_FORMAT_PATCH;
1046 options->with_raw = 1;
1048 else if (!strcmp(arg, "--stat"))
1049 options->output_format = DIFF_FORMAT_DIFFSTAT;
1050 else if (!strcmp(arg, "-z"))
1051 options->line_termination = 0;
1052 else if (!strncmp(arg, "-l", 2))
1053 options->rename_limit = strtoul(arg+2, NULL, 10);
1054 else if (!strcmp(arg, "--full-index"))
1055 options->full_index = 1;
1056 else if (!strcmp(arg, "--name-only"))
1057 options->output_format = DIFF_FORMAT_NAME;
1058 else if (!strcmp(arg, "--name-status"))
1059 options->output_format = DIFF_FORMAT_NAME_STATUS;
1060 else if (!strcmp(arg, "-R"))
1061 options->reverse_diff = 1;
1062 else if (!strncmp(arg, "-S", 2))
1063 options->pickaxe = arg + 2;
1064 else if (!strcmp(arg, "-s"))
1065 options->output_format = DIFF_FORMAT_NO_OUTPUT;
1066 else if (!strncmp(arg, "-O", 2))
1067 options->orderfile = arg + 2;
1068 else if (!strncmp(arg, "--diff-filter=", 14))
1069 options->filter = arg + 14;
1070 else if (!strcmp(arg, "--pickaxe-all"))
1071 options->pickaxe_opts = DIFF_PICKAXE_ALL;
1072 else if (!strcmp(arg, "--pickaxe-regex"))
1073 options->pickaxe_opts = DIFF_PICKAXE_REGEX;
1074 else if (!strncmp(arg, "-B", 2)) {
1075 if ((options->break_opt =
1076 diff_scoreopt_parse(arg)) == -1)
1079 else if (!strncmp(arg, "-M", 2)) {
1080 if ((options->rename_score =
1081 diff_scoreopt_parse(arg)) == -1)
1083 options->detect_rename = DIFF_DETECT_RENAME;
1085 else if (!strncmp(arg, "-C", 2)) {
1086 if ((options->rename_score =
1087 diff_scoreopt_parse(arg)) == -1)
1089 options->detect_rename = DIFF_DETECT_COPY;
1091 else if (!strcmp(arg, "--find-copies-harder"))
1092 options->find_copies_harder = 1;
1093 else if (!strcmp(arg, "--abbrev"))
1094 options->abbrev = DEFAULT_ABBREV;
1095 else if (!strncmp(arg, "--abbrev=", 9)) {
1096 options->abbrev = strtoul(arg + 9, NULL, 10);
1097 if (options->abbrev < MINIMUM_ABBREV)
1098 options->abbrev = MINIMUM_ABBREV;
1099 else if (40 < options->abbrev)
1100 options->abbrev = 40;
1107 static int parse_num(const char **cp_p)
1109 unsigned long num, scale;
1111 const char *cp = *cp_p;
1118 if ( !dot && ch == '.' ) {
1121 } else if ( ch == '%' ) {
1122 scale = dot ? scale*100 : 100;
1123 cp++; /* % is always at the end */
1125 } else if ( ch >= '0' && ch <= '9' ) {
1126 if ( scale < 100000 ) {
1128 num = (num*10) + (ch-'0');
1137 /* user says num divided by scale and we say internally that
1138 * is MAX_SCORE * num / scale.
1140 return (num >= scale) ? MAX_SCORE : (MAX_SCORE * num / scale);
1143 int diff_scoreopt_parse(const char *opt)
1145 int opt1, opt2, cmd;
1150 if (cmd != 'M' && cmd != 'C' && cmd != 'B')
1151 return -1; /* that is not a -M, -C nor -B option */
1153 opt1 = parse_num(&opt);
1159 else if (*opt != '/')
1160 return -1; /* we expect -B80/99 or -B80 */
1163 opt2 = parse_num(&opt);
1168 return opt1 | (opt2 << 16);
1171 struct diff_queue_struct diff_queued_diff;
1173 void diff_q(struct diff_queue_struct *queue, struct diff_filepair *dp)
1175 if (queue->alloc <= queue->nr) {
1176 queue->alloc = alloc_nr(queue->alloc);
1177 queue->queue = xrealloc(queue->queue,
1178 sizeof(dp) * queue->alloc);
1180 queue->queue[queue->nr++] = dp;
1183 struct diff_filepair *diff_queue(struct diff_queue_struct *queue,
1184 struct diff_filespec *one,
1185 struct diff_filespec *two)
1187 struct diff_filepair *dp = xmalloc(sizeof(*dp));
1192 dp->source_stays = 0;
1193 dp->broken_pair = 0;
1199 void diff_free_filepair(struct diff_filepair *p)
1201 diff_free_filespec_data(p->one);
1202 diff_free_filespec_data(p->two);
1208 /* This is different from find_unique_abbrev() in that
1209 * it stuffs the result with dots for alignment.
1211 const char *diff_unique_abbrev(const unsigned char *sha1, int len)
1216 return sha1_to_hex(sha1);
1218 abbrev = find_unique_abbrev(sha1, len);
1220 return sha1_to_hex(sha1);
1221 abblen = strlen(abbrev);
1223 static char hex[41];
1224 if (len < abblen && abblen <= len + 2)
1225 sprintf(hex, "%s%.*s", abbrev, len+3-abblen, "..");
1227 sprintf(hex, "%s...", abbrev);
1230 return sha1_to_hex(sha1);
1233 static void diff_flush_raw(struct diff_filepair *p,
1234 int line_termination,
1235 int inter_name_termination,
1236 struct diff_options *options,
1241 int abbrev = options->abbrev;
1242 const char *path_one, *path_two;
1244 path_one = p->one->path;
1245 path_two = p->two->path;
1246 if (line_termination) {
1247 path_one = quote_one(path_one);
1248 path_two = quote_one(path_two);
1252 sprintf(status, "%c%03d", p->status,
1253 (int)(0.5 + p->score * 100.0/MAX_SCORE));
1255 status[0] = p->status;
1258 switch (p->status) {
1259 case DIFF_STATUS_COPIED:
1260 case DIFF_STATUS_RENAMED:
1263 case DIFF_STATUS_ADDED:
1264 case DIFF_STATUS_DELETED:
1271 if (output_format != DIFF_FORMAT_NAME_STATUS) {
1272 printf(":%06o %06o %s ",
1273 p->one->mode, p->two->mode,
1274 diff_unique_abbrev(p->one->sha1, abbrev));
1276 diff_unique_abbrev(p->two->sha1, abbrev));
1278 printf("%s%c%s", status, inter_name_termination, path_one);
1280 printf("%c%s", inter_name_termination, path_two);
1281 putchar(line_termination);
1282 if (path_one != p->one->path)
1283 free((void*)path_one);
1284 if (path_two != p->two->path)
1285 free((void*)path_two);
1288 static void diff_flush_name(struct diff_filepair *p,
1289 int inter_name_termination,
1290 int line_termination)
1292 char *path = p->two->path;
1294 if (line_termination)
1295 path = quote_one(p->two->path);
1297 path = p->two->path;
1298 printf("%s%c", path, line_termination);
1299 if (p->two->path != path)
1303 int diff_unmodified_pair(struct diff_filepair *p)
1305 /* This function is written stricter than necessary to support
1306 * the currently implemented transformers, but the idea is to
1307 * let transformers to produce diff_filepairs any way they want,
1308 * and filter and clean them up here before producing the output.
1310 struct diff_filespec *one, *two;
1312 if (DIFF_PAIR_UNMERGED(p))
1313 return 0; /* unmerged is interesting */
1318 /* deletion, addition, mode or type change
1319 * and rename are all interesting.
1321 if (DIFF_FILE_VALID(one) != DIFF_FILE_VALID(two) ||
1322 DIFF_PAIR_MODE_CHANGED(p) ||
1323 strcmp(one->path, two->path))
1326 /* both are valid and point at the same path. that is, we are
1327 * dealing with a change.
1329 if (one->sha1_valid && two->sha1_valid &&
1330 !memcmp(one->sha1, two->sha1, sizeof(one->sha1)))
1331 return 1; /* no change */
1332 if (!one->sha1_valid && !two->sha1_valid)
1333 return 1; /* both look at the same file on the filesystem. */
1337 static void diff_flush_patch(struct diff_filepair *p, struct diff_options *o)
1339 if (diff_unmodified_pair(p))
1342 if ((DIFF_FILE_VALID(p->one) && S_ISDIR(p->one->mode)) ||
1343 (DIFF_FILE_VALID(p->two) && S_ISDIR(p->two->mode)))
1344 return; /* no tree diffs in patch format */
1349 static void diff_flush_stat(struct diff_filepair *p, struct diff_options *o,
1350 struct diffstat_t *diffstat)
1352 if (diff_unmodified_pair(p))
1355 if ((DIFF_FILE_VALID(p->one) && S_ISDIR(p->one->mode)) ||
1356 (DIFF_FILE_VALID(p->two) && S_ISDIR(p->two->mode)))
1357 return; /* no tree diffs in patch format */
1359 run_diffstat(p, o, diffstat);
1362 int diff_queue_is_empty(void)
1364 struct diff_queue_struct *q = &diff_queued_diff;
1366 for (i = 0; i < q->nr; i++)
1367 if (!diff_unmodified_pair(q->queue[i]))
1373 void diff_debug_filespec(struct diff_filespec *s, int x, const char *one)
1375 fprintf(stderr, "queue[%d] %s (%s) %s %06o %s\n",
1378 DIFF_FILE_VALID(s) ? "valid" : "invalid",
1380 s->sha1_valid ? sha1_to_hex(s->sha1) : "");
1381 fprintf(stderr, "queue[%d] %s size %lu flags %d\n",
1383 s->size, s->xfrm_flags);
1386 void diff_debug_filepair(const struct diff_filepair *p, int i)
1388 diff_debug_filespec(p->one, i, "one");
1389 diff_debug_filespec(p->two, i, "two");
1390 fprintf(stderr, "score %d, status %c stays %d broken %d\n",
1391 p->score, p->status ? p->status : '?',
1392 p->source_stays, p->broken_pair);
1395 void diff_debug_queue(const char *msg, struct diff_queue_struct *q)
1399 fprintf(stderr, "%s\n", msg);
1400 fprintf(stderr, "q->nr = %d\n", q->nr);
1401 for (i = 0; i < q->nr; i++) {
1402 struct diff_filepair *p = q->queue[i];
1403 diff_debug_filepair(p, i);
1408 static void diff_resolve_rename_copy(void)
1411 struct diff_filepair *p, *pp;
1412 struct diff_queue_struct *q = &diff_queued_diff;
1414 diff_debug_queue("resolve-rename-copy", q);
1416 for (i = 0; i < q->nr; i++) {
1418 p->status = 0; /* undecided */
1419 if (DIFF_PAIR_UNMERGED(p))
1420 p->status = DIFF_STATUS_UNMERGED;
1421 else if (!DIFF_FILE_VALID(p->one))
1422 p->status = DIFF_STATUS_ADDED;
1423 else if (!DIFF_FILE_VALID(p->two))
1424 p->status = DIFF_STATUS_DELETED;
1425 else if (DIFF_PAIR_TYPE_CHANGED(p))
1426 p->status = DIFF_STATUS_TYPE_CHANGED;
1428 /* from this point on, we are dealing with a pair
1429 * whose both sides are valid and of the same type, i.e.
1430 * either in-place edit or rename/copy edit.
1432 else if (DIFF_PAIR_RENAME(p)) {
1433 if (p->source_stays) {
1434 p->status = DIFF_STATUS_COPIED;
1437 /* See if there is some other filepair that
1438 * copies from the same source as us. If so
1439 * we are a copy. Otherwise we are either a
1440 * copy if the path stays, or a rename if it
1441 * does not, but we already handled "stays" case.
1443 for (j = i + 1; j < q->nr; j++) {
1445 if (strcmp(pp->one->path, p->one->path))
1446 continue; /* not us */
1447 if (!DIFF_PAIR_RENAME(pp))
1448 continue; /* not a rename/copy */
1449 /* pp is a rename/copy from the same source */
1450 p->status = DIFF_STATUS_COPIED;
1454 p->status = DIFF_STATUS_RENAMED;
1456 else if (memcmp(p->one->sha1, p->two->sha1, 20) ||
1457 p->one->mode != p->two->mode)
1458 p->status = DIFF_STATUS_MODIFIED;
1460 /* This is a "no-change" entry and should not
1461 * happen anymore, but prepare for broken callers.
1463 error("feeding unmodified %s to diffcore",
1465 p->status = DIFF_STATUS_UNKNOWN;
1468 diff_debug_queue("resolve-rename-copy done", q);
1471 static void flush_one_pair(struct diff_filepair *p,
1472 int diff_output_format,
1473 struct diff_options *options,
1474 struct diffstat_t *diffstat)
1476 int inter_name_termination = '\t';
1477 int line_termination = options->line_termination;
1478 if (!line_termination)
1479 inter_name_termination = 0;
1481 switch (p->status) {
1482 case DIFF_STATUS_UNKNOWN:
1485 die("internal error in diff-resolve-rename-copy");
1488 switch (diff_output_format) {
1489 case DIFF_FORMAT_DIFFSTAT:
1490 diff_flush_stat(p, options, diffstat);
1492 case DIFF_FORMAT_PATCH:
1493 diff_flush_patch(p, options);
1495 case DIFF_FORMAT_RAW:
1496 case DIFF_FORMAT_NAME_STATUS:
1497 diff_flush_raw(p, line_termination,
1498 inter_name_termination,
1499 options, diff_output_format);
1501 case DIFF_FORMAT_NAME:
1503 inter_name_termination,
1506 case DIFF_FORMAT_NO_OUTPUT:
1512 void diff_flush(struct diff_options *options)
1514 struct diff_queue_struct *q = &diff_queued_diff;
1516 int diff_output_format = options->output_format;
1517 struct diffstat_t *diffstat = NULL;
1519 if (diff_output_format == DIFF_FORMAT_DIFFSTAT) {
1520 diffstat = xcalloc(sizeof (struct diffstat_t), 1);
1521 diffstat->xm.consume = diffstat_consume;
1524 if (options->with_raw) {
1525 for (i = 0; i < q->nr; i++) {
1526 struct diff_filepair *p = q->queue[i];
1527 flush_one_pair(p, DIFF_FORMAT_RAW, options, NULL);
1529 putchar(options->line_termination);
1531 for (i = 0; i < q->nr; i++) {
1532 struct diff_filepair *p = q->queue[i];
1533 flush_one_pair(p, diff_output_format, options, diffstat);
1534 diff_free_filepair(p);
1538 show_stats(diffstat);
1544 q->nr = q->alloc = 0;
1547 static void diffcore_apply_filter(const char *filter)
1550 struct diff_queue_struct *q = &diff_queued_diff;
1551 struct diff_queue_struct outq;
1553 outq.nr = outq.alloc = 0;
1558 if (strchr(filter, DIFF_STATUS_FILTER_AON)) {
1560 for (i = found = 0; !found && i < q->nr; i++) {
1561 struct diff_filepair *p = q->queue[i];
1562 if (((p->status == DIFF_STATUS_MODIFIED) &&
1564 strchr(filter, DIFF_STATUS_FILTER_BROKEN)) ||
1566 strchr(filter, DIFF_STATUS_MODIFIED)))) ||
1567 ((p->status != DIFF_STATUS_MODIFIED) &&
1568 strchr(filter, p->status)))
1574 /* otherwise we will clear the whole queue
1575 * by copying the empty outq at the end of this
1576 * function, but first clear the current entries
1579 for (i = 0; i < q->nr; i++)
1580 diff_free_filepair(q->queue[i]);
1583 /* Only the matching ones */
1584 for (i = 0; i < q->nr; i++) {
1585 struct diff_filepair *p = q->queue[i];
1587 if (((p->status == DIFF_STATUS_MODIFIED) &&
1589 strchr(filter, DIFF_STATUS_FILTER_BROKEN)) ||
1591 strchr(filter, DIFF_STATUS_MODIFIED)))) ||
1592 ((p->status != DIFF_STATUS_MODIFIED) &&
1593 strchr(filter, p->status)))
1596 diff_free_filepair(p);
1603 void diffcore_std(struct diff_options *options)
1605 if (options->break_opt != -1)
1606 diffcore_break(options->break_opt);
1607 if (options->detect_rename)
1608 diffcore_rename(options);
1609 if (options->break_opt != -1)
1610 diffcore_merge_broken();
1611 if (options->pickaxe)
1612 diffcore_pickaxe(options->pickaxe, options->pickaxe_opts);
1613 if (options->orderfile)
1614 diffcore_order(options->orderfile);
1615 diff_resolve_rename_copy();
1616 diffcore_apply_filter(options->filter);
1620 void diffcore_std_no_resolve(struct diff_options *options)
1622 if (options->pickaxe)
1623 diffcore_pickaxe(options->pickaxe, options->pickaxe_opts);
1624 if (options->orderfile)
1625 diffcore_order(options->orderfile);
1626 diffcore_apply_filter(options->filter);
1629 void diff_addremove(struct diff_options *options,
1630 int addremove, unsigned mode,
1631 const unsigned char *sha1,
1632 const char *base, const char *path)
1634 char concatpath[PATH_MAX];
1635 struct diff_filespec *one, *two;
1637 /* This may look odd, but it is a preparation for
1638 * feeding "there are unchanged files which should
1639 * not produce diffs, but when you are doing copy
1640 * detection you would need them, so here they are"
1641 * entries to the diff-core. They will be prefixed
1642 * with something like '=' or '*' (I haven't decided
1643 * which but should not make any difference).
1644 * Feeding the same new and old to diff_change()
1645 * also has the same effect.
1646 * Before the final output happens, they are pruned after
1647 * merged into rename/copy pairs as appropriate.
1649 if (options->reverse_diff)
1650 addremove = (addremove == '+' ? '-' :
1651 addremove == '-' ? '+' : addremove);
1653 if (!path) path = "";
1654 sprintf(concatpath, "%s%s", base, path);
1655 one = alloc_filespec(concatpath);
1656 two = alloc_filespec(concatpath);
1658 if (addremove != '+')
1659 fill_filespec(one, sha1, mode);
1660 if (addremove != '-')
1661 fill_filespec(two, sha1, mode);
1663 diff_queue(&diff_queued_diff, one, two);
1666 void diff_change(struct diff_options *options,
1667 unsigned old_mode, unsigned new_mode,
1668 const unsigned char *old_sha1,
1669 const unsigned char *new_sha1,
1670 const char *base, const char *path)
1672 char concatpath[PATH_MAX];
1673 struct diff_filespec *one, *two;
1675 if (options->reverse_diff) {
1677 const unsigned char *tmp_c;
1678 tmp = old_mode; old_mode = new_mode; new_mode = tmp;
1679 tmp_c = old_sha1; old_sha1 = new_sha1; new_sha1 = tmp_c;
1681 if (!path) path = "";
1682 sprintf(concatpath, "%s%s", base, path);
1683 one = alloc_filespec(concatpath);
1684 two = alloc_filespec(concatpath);
1685 fill_filespec(one, old_sha1, old_mode);
1686 fill_filespec(two, new_sha1, new_mode);
1688 diff_queue(&diff_queued_diff, one, two);
1691 void diff_unmerge(struct diff_options *options,
1694 struct diff_filespec *one, *two;
1695 one = alloc_filespec(path);
1696 two = alloc_filespec(path);
1697 diff_queue(&diff_queued_diff, one, two);