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 name = xmalloc(len_a + len_b - pfx_length - sfx_length + 7);
236 sprintf(name, "%.*s{%.*s => %.*s}%s",
238 len_a - pfx_length - sfx_length, a + pfx_length,
239 len_b - pfx_length - sfx_length, b + pfx_length,
240 a + len_a - sfx_length);
243 name = xmalloc(len_a + len_b + 5);
244 sprintf(name, "%s => %s", a, b);
250 struct xdiff_emit_state xm;
254 struct diffstat_file {
256 unsigned is_unmerged:1;
257 unsigned is_binary:1;
258 unsigned is_renamed:1;
259 unsigned int added, deleted;
263 static struct diffstat_file *diffstat_add(struct diffstat_t *diffstat,
267 struct diffstat_file *x;
268 x = xcalloc(sizeof (*x), 1);
269 if (diffstat->nr == diffstat->alloc) {
270 diffstat->alloc = alloc_nr(diffstat->alloc);
271 diffstat->files = xrealloc(diffstat->files,
272 diffstat->alloc * sizeof(x));
274 diffstat->files[diffstat->nr++] = x;
276 x->name = pprint_rename(name_a, name_b);
280 x->name = strdup(name_a);
284 static void diffstat_consume(void *priv, char *line, unsigned long len)
286 struct diffstat_t *diffstat = priv;
287 struct diffstat_file *x = diffstat->files[diffstat->nr - 1];
291 else if (line[0] == '-')
295 static const char pluses[] = "++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++";
296 static const char minuses[]= "----------------------------------------------------------------------";
298 static void show_stats(struct diffstat_t* data)
300 int i, len, add, del, total, adds = 0, dels = 0;
301 int max, max_change = 0, max_len = 0;
302 int total_files = data->nr;
307 for (i = 0; i < data->nr; i++) {
308 struct diffstat_file *file = data->files[i];
310 len = strlen(file->name);
314 if (file->is_binary || file->is_unmerged)
316 if (max_change < file->added + file->deleted)
317 max_change = file->added + file->deleted;
320 for (i = 0; i < data->nr; i++) {
322 char *name = data->files[i]->name;
323 int added = data->files[i]->added;
324 int deleted = data->files[i]->deleted;
326 if (0 < (len = quote_c_style(name, NULL, NULL, 0))) {
327 char *qname = xmalloc(len + 1);
328 quote_c_style(name, qname, NULL, 0);
330 data->files[i]->name = name = qname;
334 * "scale" the filename
345 slash = strchr(name, '/');
352 * scale the add/delete
358 if (data->files[i]->is_binary) {
359 printf(" %s%-*s | Bin\n", prefix, len, name);
360 goto free_diffstat_file;
362 else if (data->files[i]->is_unmerged) {
363 printf(" %s%-*s | Unmerged\n", prefix, len, name);
364 goto free_diffstat_file;
366 else if (!data->files[i]->is_renamed &&
367 (added + deleted == 0)) {
369 goto free_diffstat_file;
378 if (max_change > 0) {
379 total = (total * max + max_change / 2) / max_change;
380 add = (add * max + max_change / 2) / max_change;
383 printf(" %s%-*s |%5d %.*s%.*s\n", prefix,
384 len, name, added + deleted,
385 add, pluses, del, minuses);
387 free(data->files[i]->name);
388 free(data->files[i]);
391 printf(" %d files changed, %d insertions(+), %d deletions(-)\n",
392 total_files, adds, dels);
395 static unsigned char *deflate_it(char *data,
397 unsigned long *result_size)
400 unsigned char *deflated;
403 memset(&stream, 0, sizeof(stream));
404 deflateInit(&stream, Z_BEST_COMPRESSION);
405 bound = deflateBound(&stream, size);
406 deflated = xmalloc(bound);
407 stream.next_out = deflated;
408 stream.avail_out = bound;
410 stream.next_in = (unsigned char *)data;
411 stream.avail_in = size;
412 while (deflate(&stream, Z_FINISH) == Z_OK)
415 *result_size = stream.total_out;
419 static void emit_binary_diff(mmfile_t *one, mmfile_t *two)
425 unsigned long orig_size;
426 unsigned long delta_size;
427 unsigned long deflate_size;
428 unsigned long data_size;
430 printf("GIT binary patch\n");
431 /* We could do deflated delta, or we could do just deflated two,
432 * whichever is smaller.
435 deflated = deflate_it(two->ptr, two->size, &deflate_size);
436 if (one->size && two->size) {
437 delta = diff_delta(one->ptr, one->size,
439 &delta_size, deflate_size);
441 void *to_free = delta;
442 orig_size = delta_size;
443 delta = deflate_it(delta, delta_size, &delta_size);
448 if (delta && delta_size < deflate_size) {
449 printf("delta %lu\n", orig_size);
452 data_size = delta_size;
455 printf("literal %lu\n", two->size);
458 data_size = deflate_size;
461 /* emit data encoded in base85 */
464 int bytes = (52 < data_size) ? 52 : data_size;
468 line[0] = bytes + 'A' - 1;
470 line[0] = bytes - 26 + 'a' - 1;
471 encode_85(line + 1, cp, bytes);
479 #define FIRST_FEW_BYTES 8000
480 static int mmfile_is_binary(mmfile_t *mf)
483 if (FIRST_FEW_BYTES < sz)
484 sz = FIRST_FEW_BYTES;
485 if (memchr(mf->ptr, 0, sz))
490 static void builtin_diff(const char *name_a,
492 struct diff_filespec *one,
493 struct diff_filespec *two,
494 const char *xfrm_msg,
495 struct diff_options *o,
496 int complete_rewrite)
502 a_one = quote_two("a/", name_a);
503 b_two = quote_two("b/", name_b);
504 lbl[0] = DIFF_FILE_VALID(one) ? a_one : "/dev/null";
505 lbl[1] = DIFF_FILE_VALID(two) ? b_two : "/dev/null";
506 printf("diff --git %s %s\n", a_one, b_two);
507 if (lbl[0][0] == '/') {
509 printf("new file mode %06o\n", two->mode);
510 if (xfrm_msg && xfrm_msg[0])
513 else if (lbl[1][0] == '/') {
514 printf("deleted file mode %06o\n", one->mode);
515 if (xfrm_msg && xfrm_msg[0])
519 if (one->mode != two->mode) {
520 printf("old mode %06o\n", one->mode);
521 printf("new mode %06o\n", two->mode);
523 if (xfrm_msg && xfrm_msg[0])
526 * we do not run diff between different kind
529 if ((one->mode ^ two->mode) & S_IFMT)
530 goto free_ab_and_return;
531 if (complete_rewrite) {
532 emit_rewrite_diff(name_a, name_b, one, two);
533 goto free_ab_and_return;
537 if (fill_mmfile(&mf1, one) < 0 || fill_mmfile(&mf2, two) < 0)
538 die("unable to read files to diff");
540 if (mmfile_is_binary(&mf1) || mmfile_is_binary(&mf2)) {
541 /* Quite common confusing case */
542 if (mf1.size == mf2.size &&
543 !memcmp(mf1.ptr, mf2.ptr, mf1.size))
544 goto free_ab_and_return;
546 emit_binary_diff(&mf1, &mf2);
548 printf("Binary files %s and %s differ\n",
552 /* Crazy xdl interfaces.. */
553 const char *diffopts = getenv("GIT_DIFF_OPTS");
557 struct emit_callback ecbdata;
559 ecbdata.label_path = lbl;
560 xpp.flags = XDF_NEED_MINIMAL;
562 xecfg.flags = XDL_EMIT_FUNCNAMES;
565 else if (!strncmp(diffopts, "--unified=", 10))
566 xecfg.ctxlen = strtoul(diffopts + 10, NULL, 10);
567 else if (!strncmp(diffopts, "-u", 2))
568 xecfg.ctxlen = strtoul(diffopts + 2, NULL, 10);
571 xdl_diff(&mf1, &mf2, &xpp, &xecfg, &ecb);
580 static void builtin_diffstat(const char *name_a, const char *name_b,
581 struct diff_filespec *one,
582 struct diff_filespec *two,
583 struct diffstat_t *diffstat,
584 int complete_rewrite)
587 struct diffstat_file *data;
589 data = diffstat_add(diffstat, name_a, name_b);
592 data->is_unmerged = 1;
595 if (complete_rewrite) {
596 diff_populate_filespec(one, 0);
597 diff_populate_filespec(two, 0);
598 data->deleted = count_lines(one->data, one->size);
599 data->added = count_lines(two->data, two->size);
602 if (fill_mmfile(&mf1, one) < 0 || fill_mmfile(&mf2, two) < 0)
603 die("unable to read files to diff");
605 if (mmfile_is_binary(&mf1) || mmfile_is_binary(&mf2))
608 /* Crazy xdl interfaces.. */
613 xpp.flags = XDF_NEED_MINIMAL;
616 ecb.outf = xdiff_outf;
618 xdl_diff(&mf1, &mf2, &xpp, &xecfg, &ecb);
622 struct diff_filespec *alloc_filespec(const char *path)
624 int namelen = strlen(path);
625 struct diff_filespec *spec = xmalloc(sizeof(*spec) + namelen + 1);
627 memset(spec, 0, sizeof(*spec));
628 spec->path = (char *)(spec + 1);
629 memcpy(spec->path, path, namelen+1);
633 void fill_filespec(struct diff_filespec *spec, const unsigned char *sha1,
637 spec->mode = canon_mode(mode);
638 memcpy(spec->sha1, sha1, 20);
639 spec->sha1_valid = !!memcmp(sha1, null_sha1, 20);
644 * Given a name and sha1 pair, if the dircache tells us the file in
645 * the work tree has that object contents, return true, so that
646 * prepare_temp_file() does not have to inflate and extract.
648 static int work_tree_matches(const char *name, const unsigned char *sha1)
650 struct cache_entry *ce;
654 /* We do not read the cache ourselves here, because the
655 * benchmark with my previous version that always reads cache
656 * shows that it makes things worse for diff-tree comparing
657 * two linux-2.6 kernel trees in an already checked out work
658 * tree. This is because most diff-tree comparisons deal with
659 * only a small number of files, while reading the cache is
660 * expensive for a large project, and its cost outweighs the
661 * savings we get by not inflating the object to a temporary
662 * file. Practically, this code only helps when we are used
663 * by diff-cache --cached, which does read the cache before
670 pos = cache_name_pos(name, len);
673 ce = active_cache[pos];
674 if ((lstat(name, &st) < 0) ||
675 !S_ISREG(st.st_mode) || /* careful! */
676 ce_match_stat(ce, &st, 0) ||
677 memcmp(sha1, ce->sha1, 20))
679 /* we return 1 only when we can stat, it is a regular file,
680 * stat information matches, and sha1 recorded in the cache
681 * matches. I.e. we know the file in the work tree really is
682 * the same as the <name, sha1> pair.
687 static struct sha1_size_cache {
688 unsigned char sha1[20];
691 static int sha1_size_cache_nr, sha1_size_cache_alloc;
693 static struct sha1_size_cache *locate_size_cache(unsigned char *sha1,
698 struct sha1_size_cache *e;
701 last = sha1_size_cache_nr;
702 while (last > first) {
703 int cmp, next = (last + first) >> 1;
704 e = sha1_size_cache[next];
705 cmp = memcmp(e->sha1, sha1, 20);
717 /* insert to make it at "first" */
718 if (sha1_size_cache_alloc <= sha1_size_cache_nr) {
719 sha1_size_cache_alloc = alloc_nr(sha1_size_cache_alloc);
720 sha1_size_cache = xrealloc(sha1_size_cache,
721 sha1_size_cache_alloc *
722 sizeof(*sha1_size_cache));
724 sha1_size_cache_nr++;
725 if (first < sha1_size_cache_nr)
726 memmove(sha1_size_cache + first + 1, sha1_size_cache + first,
727 (sha1_size_cache_nr - first - 1) *
728 sizeof(*sha1_size_cache));
729 e = xmalloc(sizeof(struct sha1_size_cache));
730 sha1_size_cache[first] = e;
731 memcpy(e->sha1, sha1, 20);
737 * While doing rename detection and pickaxe operation, we may need to
738 * grab the data for the blob (or file) for our own in-core comparison.
739 * diff_filespec has data and size fields for this purpose.
741 int diff_populate_filespec(struct diff_filespec *s, int size_only)
744 if (!DIFF_FILE_VALID(s))
745 die("internal error: asking to populate invalid file.");
746 if (S_ISDIR(s->mode))
754 if (!s->sha1_valid ||
755 work_tree_matches(s->path, s->sha1)) {
758 if (lstat(s->path, &st) < 0) {
759 if (errno == ENOENT) {
768 s->size = st.st_size;
773 if (S_ISLNK(st.st_mode)) {
775 s->data = xmalloc(s->size);
777 ret = readlink(s->path, s->data, s->size);
784 fd = open(s->path, O_RDONLY);
787 s->data = mmap(NULL, s->size, PROT_READ, MAP_PRIVATE, fd, 0);
789 if (s->data == MAP_FAILED)
791 s->should_munmap = 1;
795 struct sha1_size_cache *e;
798 e = locate_size_cache(s->sha1, 1, 0);
803 if (!sha1_object_info(s->sha1, type, &s->size))
804 locate_size_cache(s->sha1, 0, s->size);
807 s->data = read_sha1_file(s->sha1, type, &s->size);
814 void diff_free_filespec_data(struct diff_filespec *s)
818 else if (s->should_munmap)
819 munmap(s->data, s->size);
820 s->should_free = s->should_munmap = 0;
826 static void prep_temp_blob(struct diff_tempfile *temp,
829 const unsigned char *sha1,
834 fd = git_mkstemp(temp->tmp_path, TEMPFILE_PATH_LEN, ".diff_XXXXXX");
836 die("unable to create temp-file");
837 if (write(fd, blob, size) != size)
838 die("unable to write temp-file");
840 temp->name = temp->tmp_path;
841 strcpy(temp->hex, sha1_to_hex(sha1));
843 sprintf(temp->mode, "%06o", mode);
846 static void prepare_temp_file(const char *name,
847 struct diff_tempfile *temp,
848 struct diff_filespec *one)
850 if (!DIFF_FILE_VALID(one)) {
852 /* A '-' entry produces this for file-2, and
853 * a '+' entry produces this for file-1.
855 temp->name = "/dev/null";
856 strcpy(temp->hex, ".");
857 strcpy(temp->mode, ".");
861 if (!one->sha1_valid ||
862 work_tree_matches(name, one->sha1)) {
864 if (lstat(name, &st) < 0) {
866 goto not_a_valid_file;
867 die("stat(%s): %s", name, strerror(errno));
869 if (S_ISLNK(st.st_mode)) {
871 char buf[PATH_MAX + 1]; /* ought to be SYMLINK_MAX */
872 if (sizeof(buf) <= st.st_size)
873 die("symlink too long: %s", name);
874 ret = readlink(name, buf, st.st_size);
876 die("readlink(%s)", name);
877 prep_temp_blob(temp, buf, st.st_size,
879 one->sha1 : null_sha1),
881 one->mode : S_IFLNK));
884 /* we can borrow from the file in the work tree */
886 if (!one->sha1_valid)
887 strcpy(temp->hex, sha1_to_hex(null_sha1));
889 strcpy(temp->hex, sha1_to_hex(one->sha1));
890 /* Even though we may sometimes borrow the
891 * contents from the work tree, we always want
892 * one->mode. mode is trustworthy even when
893 * !(one->sha1_valid), as long as
894 * DIFF_FILE_VALID(one).
896 sprintf(temp->mode, "%06o", one->mode);
901 if (diff_populate_filespec(one, 0))
902 die("cannot read data blob for %s", one->path);
903 prep_temp_blob(temp, one->data, one->size,
904 one->sha1, one->mode);
908 static void remove_tempfile(void)
912 for (i = 0; i < 2; i++)
913 if (diff_temp[i].name == diff_temp[i].tmp_path) {
914 unlink(diff_temp[i].name);
915 diff_temp[i].name = NULL;
919 static void remove_tempfile_on_signal(int signo)
922 signal(SIGINT, SIG_DFL);
926 static int spawn_prog(const char *pgm, const char **arg)
934 die("unable to fork");
936 execvp(pgm, (char *const*) arg);
940 while (waitpid(pid, &status, 0) < 0) {
946 /* Earlier we did not check the exit status because
947 * diff exits non-zero if files are different, and
948 * we are not interested in knowing that. It was a
949 * mistake which made it harder to quit a diff-*
950 * session that uses the git-apply-patch-script as
951 * the GIT_EXTERNAL_DIFF. A custom GIT_EXTERNAL_DIFF
952 * should also exit non-zero only when it wants to
953 * abort the entire diff-* session.
955 if (WIFEXITED(status) && !WEXITSTATUS(status))
960 /* An external diff command takes:
962 * diff-cmd name infile1 infile1-sha1 infile1-mode \
963 * infile2 infile2-sha1 infile2-mode [ rename-to ]
966 static void run_external_diff(const char *pgm,
969 struct diff_filespec *one,
970 struct diff_filespec *two,
971 const char *xfrm_msg,
972 int complete_rewrite)
974 const char *spawn_arg[10];
975 struct diff_tempfile *temp = diff_temp;
977 static int atexit_asked = 0;
978 const char *othername;
979 const char **arg = &spawn_arg[0];
981 othername = (other? other : name);
983 prepare_temp_file(name, &temp[0], one);
984 prepare_temp_file(othername, &temp[1], two);
985 if (! atexit_asked &&
986 (temp[0].name == temp[0].tmp_path ||
987 temp[1].name == temp[1].tmp_path)) {
989 atexit(remove_tempfile);
991 signal(SIGINT, remove_tempfile_on_signal);
997 *arg++ = temp[0].name;
998 *arg++ = temp[0].hex;
999 *arg++ = temp[0].mode;
1000 *arg++ = temp[1].name;
1001 *arg++ = temp[1].hex;
1002 *arg++ = temp[1].mode;
1012 retval = spawn_prog(pgm, spawn_arg);
1015 fprintf(stderr, "external diff died, stopping at %s.\n", name);
1020 static void run_diff_cmd(const char *pgm,
1023 struct diff_filespec *one,
1024 struct diff_filespec *two,
1025 const char *xfrm_msg,
1026 struct diff_options *o,
1027 int complete_rewrite)
1030 run_external_diff(pgm, name, other, one, two, xfrm_msg,
1035 builtin_diff(name, other ? other : name,
1036 one, two, xfrm_msg, o, complete_rewrite);
1038 printf("* Unmerged path %s\n", name);
1041 static void diff_fill_sha1_info(struct diff_filespec *one)
1043 if (DIFF_FILE_VALID(one)) {
1044 if (!one->sha1_valid) {
1046 if (lstat(one->path, &st) < 0)
1047 die("stat %s", one->path);
1048 if (index_path(one->sha1, one->path, &st, 0))
1049 die("cannot hash %s\n", one->path);
1053 memset(one->sha1, 0, 20);
1056 static void run_diff(struct diff_filepair *p, struct diff_options *o)
1058 const char *pgm = external_diff();
1059 char msg[PATH_MAX*2+300], *xfrm_msg;
1060 struct diff_filespec *one;
1061 struct diff_filespec *two;
1064 char *name_munged, *other_munged;
1065 int complete_rewrite = 0;
1068 if (DIFF_PAIR_UNMERGED(p)) {
1070 run_diff_cmd(pgm, p->one->path, NULL, NULL, NULL, NULL, o, 0);
1074 name = p->one->path;
1075 other = (strcmp(name, p->two->path) ? p->two->path : NULL);
1076 name_munged = quote_one(name);
1077 other_munged = quote_one(other);
1078 one = p->one; two = p->two;
1080 diff_fill_sha1_info(one);
1081 diff_fill_sha1_info(two);
1084 switch (p->status) {
1085 case DIFF_STATUS_COPIED:
1086 len += snprintf(msg + len, sizeof(msg) - len,
1087 "similarity index %d%%\n"
1090 (int)(0.5 + p->score * 100.0/MAX_SCORE),
1091 name_munged, other_munged);
1093 case DIFF_STATUS_RENAMED:
1094 len += snprintf(msg + len, sizeof(msg) - len,
1095 "similarity index %d%%\n"
1098 (int)(0.5 + p->score * 100.0/MAX_SCORE),
1099 name_munged, other_munged);
1101 case DIFF_STATUS_MODIFIED:
1103 len += snprintf(msg + len, sizeof(msg) - len,
1104 "dissimilarity index %d%%\n",
1105 (int)(0.5 + p->score *
1107 complete_rewrite = 1;
1116 if (memcmp(one->sha1, two->sha1, 20)) {
1117 int abbrev = o->full_index ? 40 : DEFAULT_ABBREV;
1119 len += snprintf(msg + len, sizeof(msg) - len,
1121 abbrev, sha1_to_hex(one->sha1),
1122 abbrev, sha1_to_hex(two->sha1));
1123 if (one->mode == two->mode)
1124 len += snprintf(msg + len, sizeof(msg) - len,
1125 " %06o", one->mode);
1126 len += snprintf(msg + len, sizeof(msg) - len, "\n");
1131 xfrm_msg = len ? msg : NULL;
1134 DIFF_FILE_VALID(one) && DIFF_FILE_VALID(two) &&
1135 (S_IFMT & one->mode) != (S_IFMT & two->mode)) {
1136 /* a filepair that changes between file and symlink
1137 * needs to be split into deletion and creation.
1139 struct diff_filespec *null = alloc_filespec(two->path);
1140 run_diff_cmd(NULL, name, other, one, null, xfrm_msg, o, 0);
1142 null = alloc_filespec(one->path);
1143 run_diff_cmd(NULL, name, other, null, two, xfrm_msg, o, 0);
1147 run_diff_cmd(pgm, name, other, one, two, xfrm_msg, o,
1154 static void run_diffstat(struct diff_filepair *p, struct diff_options *o,
1155 struct diffstat_t *diffstat)
1159 int complete_rewrite = 0;
1161 if (DIFF_PAIR_UNMERGED(p)) {
1163 builtin_diffstat(p->one->path, NULL, NULL, NULL, diffstat, 0);
1167 name = p->one->path;
1168 other = (strcmp(name, p->two->path) ? p->two->path : NULL);
1170 diff_fill_sha1_info(p->one);
1171 diff_fill_sha1_info(p->two);
1173 if (p->status == DIFF_STATUS_MODIFIED && p->score)
1174 complete_rewrite = 1;
1175 builtin_diffstat(name, other, p->one, p->two, diffstat, complete_rewrite);
1178 void diff_setup(struct diff_options *options)
1180 memset(options, 0, sizeof(*options));
1181 options->output_format = DIFF_FORMAT_RAW;
1182 options->line_termination = '\n';
1183 options->break_opt = -1;
1184 options->rename_limit = -1;
1186 options->change = diff_change;
1187 options->add_remove = diff_addremove;
1190 int diff_setup_done(struct diff_options *options)
1192 if ((options->find_copies_harder &&
1193 options->detect_rename != DIFF_DETECT_COPY) ||
1194 (0 <= options->rename_limit && !options->detect_rename))
1198 * These cases always need recursive; we do not drop caller-supplied
1199 * recursive bits for other formats here.
1201 if ((options->output_format == DIFF_FORMAT_PATCH) ||
1202 (options->output_format == DIFF_FORMAT_DIFFSTAT))
1203 options->recursive = 1;
1205 if (options->detect_rename && options->rename_limit < 0)
1206 options->rename_limit = diff_rename_limit_default;
1207 if (options->setup & DIFF_SETUP_USE_CACHE) {
1209 /* read-cache does not die even when it fails
1210 * so it is safe for us to do this here. Also
1211 * it does not smudge active_cache or active_nr
1212 * when it fails, so we do not have to worry about
1213 * cleaning it up ourselves either.
1217 if (options->setup & DIFF_SETUP_USE_SIZE_CACHE)
1219 if (options->abbrev <= 0 || 40 < options->abbrev)
1220 options->abbrev = 40; /* full */
1225 int diff_opt_parse(struct diff_options *options, const char **av, int ac)
1227 const char *arg = av[0];
1228 if (!strcmp(arg, "-p") || !strcmp(arg, "-u"))
1229 options->output_format = DIFF_FORMAT_PATCH;
1230 else if (!strcmp(arg, "--patch-with-raw")) {
1231 options->output_format = DIFF_FORMAT_PATCH;
1232 options->with_raw = 1;
1234 else if (!strcmp(arg, "--stat"))
1235 options->output_format = DIFF_FORMAT_DIFFSTAT;
1236 else if (!strcmp(arg, "--summary"))
1237 options->summary = 1;
1238 else if (!strcmp(arg, "--patch-with-stat")) {
1239 options->output_format = DIFF_FORMAT_PATCH;
1240 options->with_stat = 1;
1242 else if (!strcmp(arg, "-z"))
1243 options->line_termination = 0;
1244 else if (!strncmp(arg, "-l", 2))
1245 options->rename_limit = strtoul(arg+2, NULL, 10);
1246 else if (!strcmp(arg, "--full-index"))
1247 options->full_index = 1;
1248 else if (!strcmp(arg, "--binary")) {
1249 options->output_format = DIFF_FORMAT_PATCH;
1250 options->full_index = options->binary = 1;
1252 else if (!strcmp(arg, "--name-only"))
1253 options->output_format = DIFF_FORMAT_NAME;
1254 else if (!strcmp(arg, "--name-status"))
1255 options->output_format = DIFF_FORMAT_NAME_STATUS;
1256 else if (!strcmp(arg, "-R"))
1257 options->reverse_diff = 1;
1258 else if (!strncmp(arg, "-S", 2))
1259 options->pickaxe = arg + 2;
1260 else if (!strcmp(arg, "-s"))
1261 options->output_format = DIFF_FORMAT_NO_OUTPUT;
1262 else if (!strncmp(arg, "-O", 2))
1263 options->orderfile = arg + 2;
1264 else if (!strncmp(arg, "--diff-filter=", 14))
1265 options->filter = arg + 14;
1266 else if (!strcmp(arg, "--pickaxe-all"))
1267 options->pickaxe_opts = DIFF_PICKAXE_ALL;
1268 else if (!strcmp(arg, "--pickaxe-regex"))
1269 options->pickaxe_opts = DIFF_PICKAXE_REGEX;
1270 else if (!strncmp(arg, "-B", 2)) {
1271 if ((options->break_opt =
1272 diff_scoreopt_parse(arg)) == -1)
1275 else if (!strncmp(arg, "-M", 2)) {
1276 if ((options->rename_score =
1277 diff_scoreopt_parse(arg)) == -1)
1279 options->detect_rename = DIFF_DETECT_RENAME;
1281 else if (!strncmp(arg, "-C", 2)) {
1282 if ((options->rename_score =
1283 diff_scoreopt_parse(arg)) == -1)
1285 options->detect_rename = DIFF_DETECT_COPY;
1287 else if (!strcmp(arg, "--find-copies-harder"))
1288 options->find_copies_harder = 1;
1289 else if (!strcmp(arg, "--abbrev"))
1290 options->abbrev = DEFAULT_ABBREV;
1291 else if (!strncmp(arg, "--abbrev=", 9)) {
1292 options->abbrev = strtoul(arg + 9, NULL, 10);
1293 if (options->abbrev < MINIMUM_ABBREV)
1294 options->abbrev = MINIMUM_ABBREV;
1295 else if (40 < options->abbrev)
1296 options->abbrev = 40;
1303 static int parse_num(const char **cp_p)
1305 unsigned long num, scale;
1307 const char *cp = *cp_p;
1314 if ( !dot && ch == '.' ) {
1317 } else if ( ch == '%' ) {
1318 scale = dot ? scale*100 : 100;
1319 cp++; /* % is always at the end */
1321 } else if ( ch >= '0' && ch <= '9' ) {
1322 if ( scale < 100000 ) {
1324 num = (num*10) + (ch-'0');
1333 /* user says num divided by scale and we say internally that
1334 * is MAX_SCORE * num / scale.
1336 return (num >= scale) ? MAX_SCORE : (MAX_SCORE * num / scale);
1339 int diff_scoreopt_parse(const char *opt)
1341 int opt1, opt2, cmd;
1346 if (cmd != 'M' && cmd != 'C' && cmd != 'B')
1347 return -1; /* that is not a -M, -C nor -B option */
1349 opt1 = parse_num(&opt);
1355 else if (*opt != '/')
1356 return -1; /* we expect -B80/99 or -B80 */
1359 opt2 = parse_num(&opt);
1364 return opt1 | (opt2 << 16);
1367 struct diff_queue_struct diff_queued_diff;
1369 void diff_q(struct diff_queue_struct *queue, struct diff_filepair *dp)
1371 if (queue->alloc <= queue->nr) {
1372 queue->alloc = alloc_nr(queue->alloc);
1373 queue->queue = xrealloc(queue->queue,
1374 sizeof(dp) * queue->alloc);
1376 queue->queue[queue->nr++] = dp;
1379 struct diff_filepair *diff_queue(struct diff_queue_struct *queue,
1380 struct diff_filespec *one,
1381 struct diff_filespec *two)
1383 struct diff_filepair *dp = xmalloc(sizeof(*dp));
1388 dp->source_stays = 0;
1389 dp->broken_pair = 0;
1395 void diff_free_filepair(struct diff_filepair *p)
1397 diff_free_filespec_data(p->one);
1398 diff_free_filespec_data(p->two);
1404 /* This is different from find_unique_abbrev() in that
1405 * it stuffs the result with dots for alignment.
1407 const char *diff_unique_abbrev(const unsigned char *sha1, int len)
1412 return sha1_to_hex(sha1);
1414 abbrev = find_unique_abbrev(sha1, len);
1416 return sha1_to_hex(sha1);
1417 abblen = strlen(abbrev);
1419 static char hex[41];
1420 if (len < abblen && abblen <= len + 2)
1421 sprintf(hex, "%s%.*s", abbrev, len+3-abblen, "..");
1423 sprintf(hex, "%s...", abbrev);
1426 return sha1_to_hex(sha1);
1429 static void diff_flush_raw(struct diff_filepair *p,
1430 int line_termination,
1431 int inter_name_termination,
1432 struct diff_options *options,
1437 int abbrev = options->abbrev;
1438 const char *path_one, *path_two;
1440 path_one = p->one->path;
1441 path_two = p->two->path;
1442 if (line_termination) {
1443 path_one = quote_one(path_one);
1444 path_two = quote_one(path_two);
1448 sprintf(status, "%c%03d", p->status,
1449 (int)(0.5 + p->score * 100.0/MAX_SCORE));
1451 status[0] = p->status;
1454 switch (p->status) {
1455 case DIFF_STATUS_COPIED:
1456 case DIFF_STATUS_RENAMED:
1459 case DIFF_STATUS_ADDED:
1460 case DIFF_STATUS_DELETED:
1467 if (output_format != DIFF_FORMAT_NAME_STATUS) {
1468 printf(":%06o %06o %s ",
1469 p->one->mode, p->two->mode,
1470 diff_unique_abbrev(p->one->sha1, abbrev));
1472 diff_unique_abbrev(p->two->sha1, abbrev));
1474 printf("%s%c%s", status, inter_name_termination, path_one);
1476 printf("%c%s", inter_name_termination, path_two);
1477 putchar(line_termination);
1478 if (path_one != p->one->path)
1479 free((void*)path_one);
1480 if (path_two != p->two->path)
1481 free((void*)path_two);
1484 static void diff_flush_name(struct diff_filepair *p,
1485 int inter_name_termination,
1486 int line_termination)
1488 char *path = p->two->path;
1490 if (line_termination)
1491 path = quote_one(p->two->path);
1493 path = p->two->path;
1494 printf("%s%c", path, line_termination);
1495 if (p->two->path != path)
1499 int diff_unmodified_pair(struct diff_filepair *p)
1501 /* This function is written stricter than necessary to support
1502 * the currently implemented transformers, but the idea is to
1503 * let transformers to produce diff_filepairs any way they want,
1504 * and filter and clean them up here before producing the output.
1506 struct diff_filespec *one, *two;
1508 if (DIFF_PAIR_UNMERGED(p))
1509 return 0; /* unmerged is interesting */
1514 /* deletion, addition, mode or type change
1515 * and rename are all interesting.
1517 if (DIFF_FILE_VALID(one) != DIFF_FILE_VALID(two) ||
1518 DIFF_PAIR_MODE_CHANGED(p) ||
1519 strcmp(one->path, two->path))
1522 /* both are valid and point at the same path. that is, we are
1523 * dealing with a change.
1525 if (one->sha1_valid && two->sha1_valid &&
1526 !memcmp(one->sha1, two->sha1, sizeof(one->sha1)))
1527 return 1; /* no change */
1528 if (!one->sha1_valid && !two->sha1_valid)
1529 return 1; /* both look at the same file on the filesystem. */
1533 static void diff_flush_patch(struct diff_filepair *p, struct diff_options *o)
1535 if (diff_unmodified_pair(p))
1538 if ((DIFF_FILE_VALID(p->one) && S_ISDIR(p->one->mode)) ||
1539 (DIFF_FILE_VALID(p->two) && S_ISDIR(p->two->mode)))
1540 return; /* no tree diffs in patch format */
1545 static void diff_flush_stat(struct diff_filepair *p, struct diff_options *o,
1546 struct diffstat_t *diffstat)
1548 if (diff_unmodified_pair(p))
1551 if ((DIFF_FILE_VALID(p->one) && S_ISDIR(p->one->mode)) ||
1552 (DIFF_FILE_VALID(p->two) && S_ISDIR(p->two->mode)))
1553 return; /* no tree diffs in patch format */
1555 run_diffstat(p, o, diffstat);
1558 int diff_queue_is_empty(void)
1560 struct diff_queue_struct *q = &diff_queued_diff;
1562 for (i = 0; i < q->nr; i++)
1563 if (!diff_unmodified_pair(q->queue[i]))
1569 void diff_debug_filespec(struct diff_filespec *s, int x, const char *one)
1571 fprintf(stderr, "queue[%d] %s (%s) %s %06o %s\n",
1574 DIFF_FILE_VALID(s) ? "valid" : "invalid",
1576 s->sha1_valid ? sha1_to_hex(s->sha1) : "");
1577 fprintf(stderr, "queue[%d] %s size %lu flags %d\n",
1579 s->size, s->xfrm_flags);
1582 void diff_debug_filepair(const struct diff_filepair *p, int i)
1584 diff_debug_filespec(p->one, i, "one");
1585 diff_debug_filespec(p->two, i, "two");
1586 fprintf(stderr, "score %d, status %c stays %d broken %d\n",
1587 p->score, p->status ? p->status : '?',
1588 p->source_stays, p->broken_pair);
1591 void diff_debug_queue(const char *msg, struct diff_queue_struct *q)
1595 fprintf(stderr, "%s\n", msg);
1596 fprintf(stderr, "q->nr = %d\n", q->nr);
1597 for (i = 0; i < q->nr; i++) {
1598 struct diff_filepair *p = q->queue[i];
1599 diff_debug_filepair(p, i);
1604 static void diff_resolve_rename_copy(void)
1607 struct diff_filepair *p, *pp;
1608 struct diff_queue_struct *q = &diff_queued_diff;
1610 diff_debug_queue("resolve-rename-copy", q);
1612 for (i = 0; i < q->nr; i++) {
1614 p->status = 0; /* undecided */
1615 if (DIFF_PAIR_UNMERGED(p))
1616 p->status = DIFF_STATUS_UNMERGED;
1617 else if (!DIFF_FILE_VALID(p->one))
1618 p->status = DIFF_STATUS_ADDED;
1619 else if (!DIFF_FILE_VALID(p->two))
1620 p->status = DIFF_STATUS_DELETED;
1621 else if (DIFF_PAIR_TYPE_CHANGED(p))
1622 p->status = DIFF_STATUS_TYPE_CHANGED;
1624 /* from this point on, we are dealing with a pair
1625 * whose both sides are valid and of the same type, i.e.
1626 * either in-place edit or rename/copy edit.
1628 else if (DIFF_PAIR_RENAME(p)) {
1629 if (p->source_stays) {
1630 p->status = DIFF_STATUS_COPIED;
1633 /* See if there is some other filepair that
1634 * copies from the same source as us. If so
1635 * we are a copy. Otherwise we are either a
1636 * copy if the path stays, or a rename if it
1637 * does not, but we already handled "stays" case.
1639 for (j = i + 1; j < q->nr; j++) {
1641 if (strcmp(pp->one->path, p->one->path))
1642 continue; /* not us */
1643 if (!DIFF_PAIR_RENAME(pp))
1644 continue; /* not a rename/copy */
1645 /* pp is a rename/copy from the same source */
1646 p->status = DIFF_STATUS_COPIED;
1650 p->status = DIFF_STATUS_RENAMED;
1652 else if (memcmp(p->one->sha1, p->two->sha1, 20) ||
1653 p->one->mode != p->two->mode)
1654 p->status = DIFF_STATUS_MODIFIED;
1656 /* This is a "no-change" entry and should not
1657 * happen anymore, but prepare for broken callers.
1659 error("feeding unmodified %s to diffcore",
1661 p->status = DIFF_STATUS_UNKNOWN;
1664 diff_debug_queue("resolve-rename-copy done", q);
1667 static void flush_one_pair(struct diff_filepair *p,
1668 int diff_output_format,
1669 struct diff_options *options,
1670 struct diffstat_t *diffstat)
1672 int inter_name_termination = '\t';
1673 int line_termination = options->line_termination;
1674 if (!line_termination)
1675 inter_name_termination = 0;
1677 switch (p->status) {
1678 case DIFF_STATUS_UNKNOWN:
1681 die("internal error in diff-resolve-rename-copy");
1684 switch (diff_output_format) {
1685 case DIFF_FORMAT_DIFFSTAT:
1686 diff_flush_stat(p, options, diffstat);
1688 case DIFF_FORMAT_PATCH:
1689 diff_flush_patch(p, options);
1691 case DIFF_FORMAT_RAW:
1692 case DIFF_FORMAT_NAME_STATUS:
1693 diff_flush_raw(p, line_termination,
1694 inter_name_termination,
1695 options, diff_output_format);
1697 case DIFF_FORMAT_NAME:
1699 inter_name_termination,
1702 case DIFF_FORMAT_NO_OUTPUT:
1708 static void show_file_mode_name(const char *newdelete, struct diff_filespec *fs)
1711 printf(" %s mode %06o %s\n", newdelete, fs->mode, fs->path);
1713 printf(" %s %s\n", newdelete, fs->path);
1717 static void show_mode_change(struct diff_filepair *p, int show_name)
1719 if (p->one->mode && p->two->mode && p->one->mode != p->two->mode) {
1721 printf(" mode change %06o => %06o %s\n",
1722 p->one->mode, p->two->mode, p->two->path);
1724 printf(" mode change %06o => %06o\n",
1725 p->one->mode, p->two->mode);
1729 static void show_rename_copy(const char *renamecopy, struct diff_filepair *p)
1731 const char *old, *new;
1733 /* Find common prefix */
1737 const char *slash_old, *slash_new;
1738 slash_old = strchr(old, '/');
1739 slash_new = strchr(new, '/');
1742 slash_old - old != slash_new - new ||
1743 memcmp(old, new, slash_new - new))
1745 old = slash_old + 1;
1746 new = slash_new + 1;
1748 /* p->one->path thru old is the common prefix, and old and new
1749 * through the end of names are renames
1751 if (old != p->one->path)
1752 printf(" %s %.*s{%s => %s} (%d%%)\n", renamecopy,
1753 (int)(old - p->one->path), p->one->path,
1754 old, new, (int)(0.5 + p->score * 100.0/MAX_SCORE));
1756 printf(" %s %s => %s (%d%%)\n", renamecopy,
1757 p->one->path, p->two->path,
1758 (int)(0.5 + p->score * 100.0/MAX_SCORE));
1759 show_mode_change(p, 0);
1762 static void diff_summary(struct diff_filepair *p)
1765 case DIFF_STATUS_DELETED:
1766 show_file_mode_name("delete", p->one);
1768 case DIFF_STATUS_ADDED:
1769 show_file_mode_name("create", p->two);
1771 case DIFF_STATUS_COPIED:
1772 show_rename_copy("copy", p);
1774 case DIFF_STATUS_RENAMED:
1775 show_rename_copy("rename", p);
1779 printf(" rewrite %s (%d%%)\n", p->two->path,
1780 (int)(0.5 + p->score * 100.0/MAX_SCORE));
1781 show_mode_change(p, 0);
1782 } else show_mode_change(p, 1);
1787 void diff_flush(struct diff_options *options)
1789 struct diff_queue_struct *q = &diff_queued_diff;
1791 int diff_output_format = options->output_format;
1792 struct diffstat_t *diffstat = NULL;
1794 if (diff_output_format == DIFF_FORMAT_DIFFSTAT || options->with_stat) {
1795 diffstat = xcalloc(sizeof (struct diffstat_t), 1);
1796 diffstat->xm.consume = diffstat_consume;
1799 if (options->with_raw) {
1800 for (i = 0; i < q->nr; i++) {
1801 struct diff_filepair *p = q->queue[i];
1802 flush_one_pair(p, DIFF_FORMAT_RAW, options, NULL);
1804 putchar(options->line_termination);
1806 if (options->with_stat) {
1807 for (i = 0; i < q->nr; i++) {
1808 struct diff_filepair *p = q->queue[i];
1809 flush_one_pair(p, DIFF_FORMAT_DIFFSTAT, options,
1812 show_stats(diffstat);
1815 putchar(options->line_termination);
1817 for (i = 0; i < q->nr; i++) {
1818 struct diff_filepair *p = q->queue[i];
1819 flush_one_pair(p, diff_output_format, options, diffstat);
1823 show_stats(diffstat);
1827 for (i = 0; i < q->nr; i++) {
1828 if (options->summary)
1829 diff_summary(q->queue[i]);
1830 diff_free_filepair(q->queue[i]);
1835 q->nr = q->alloc = 0;
1838 static void diffcore_apply_filter(const char *filter)
1841 struct diff_queue_struct *q = &diff_queued_diff;
1842 struct diff_queue_struct outq;
1844 outq.nr = outq.alloc = 0;
1849 if (strchr(filter, DIFF_STATUS_FILTER_AON)) {
1851 for (i = found = 0; !found && i < q->nr; i++) {
1852 struct diff_filepair *p = q->queue[i];
1853 if (((p->status == DIFF_STATUS_MODIFIED) &&
1855 strchr(filter, DIFF_STATUS_FILTER_BROKEN)) ||
1857 strchr(filter, DIFF_STATUS_MODIFIED)))) ||
1858 ((p->status != DIFF_STATUS_MODIFIED) &&
1859 strchr(filter, p->status)))
1865 /* otherwise we will clear the whole queue
1866 * by copying the empty outq at the end of this
1867 * function, but first clear the current entries
1870 for (i = 0; i < q->nr; i++)
1871 diff_free_filepair(q->queue[i]);
1874 /* Only the matching ones */
1875 for (i = 0; i < q->nr; i++) {
1876 struct diff_filepair *p = q->queue[i];
1878 if (((p->status == DIFF_STATUS_MODIFIED) &&
1880 strchr(filter, DIFF_STATUS_FILTER_BROKEN)) ||
1882 strchr(filter, DIFF_STATUS_MODIFIED)))) ||
1883 ((p->status != DIFF_STATUS_MODIFIED) &&
1884 strchr(filter, p->status)))
1887 diff_free_filepair(p);
1894 void diffcore_std(struct diff_options *options)
1896 if (options->break_opt != -1)
1897 diffcore_break(options->break_opt);
1898 if (options->detect_rename)
1899 diffcore_rename(options);
1900 if (options->break_opt != -1)
1901 diffcore_merge_broken();
1902 if (options->pickaxe)
1903 diffcore_pickaxe(options->pickaxe, options->pickaxe_opts);
1904 if (options->orderfile)
1905 diffcore_order(options->orderfile);
1906 diff_resolve_rename_copy();
1907 diffcore_apply_filter(options->filter);
1911 void diffcore_std_no_resolve(struct diff_options *options)
1913 if (options->pickaxe)
1914 diffcore_pickaxe(options->pickaxe, options->pickaxe_opts);
1915 if (options->orderfile)
1916 diffcore_order(options->orderfile);
1917 diffcore_apply_filter(options->filter);
1920 void diff_addremove(struct diff_options *options,
1921 int addremove, unsigned mode,
1922 const unsigned char *sha1,
1923 const char *base, const char *path)
1925 char concatpath[PATH_MAX];
1926 struct diff_filespec *one, *two;
1928 /* This may look odd, but it is a preparation for
1929 * feeding "there are unchanged files which should
1930 * not produce diffs, but when you are doing copy
1931 * detection you would need them, so here they are"
1932 * entries to the diff-core. They will be prefixed
1933 * with something like '=' or '*' (I haven't decided
1934 * which but should not make any difference).
1935 * Feeding the same new and old to diff_change()
1936 * also has the same effect.
1937 * Before the final output happens, they are pruned after
1938 * merged into rename/copy pairs as appropriate.
1940 if (options->reverse_diff)
1941 addremove = (addremove == '+' ? '-' :
1942 addremove == '-' ? '+' : addremove);
1944 if (!path) path = "";
1945 sprintf(concatpath, "%s%s", base, path);
1946 one = alloc_filespec(concatpath);
1947 two = alloc_filespec(concatpath);
1949 if (addremove != '+')
1950 fill_filespec(one, sha1, mode);
1951 if (addremove != '-')
1952 fill_filespec(two, sha1, mode);
1954 diff_queue(&diff_queued_diff, one, two);
1957 void diff_change(struct diff_options *options,
1958 unsigned old_mode, unsigned new_mode,
1959 const unsigned char *old_sha1,
1960 const unsigned char *new_sha1,
1961 const char *base, const char *path)
1963 char concatpath[PATH_MAX];
1964 struct diff_filespec *one, *two;
1966 if (options->reverse_diff) {
1968 const unsigned char *tmp_c;
1969 tmp = old_mode; old_mode = new_mode; new_mode = tmp;
1970 tmp_c = old_sha1; old_sha1 = new_sha1; new_sha1 = tmp_c;
1972 if (!path) path = "";
1973 sprintf(concatpath, "%s%s", base, path);
1974 one = alloc_filespec(concatpath);
1975 two = alloc_filespec(concatpath);
1976 fill_filespec(one, old_sha1, old_mode);
1977 fill_filespec(two, new_sha1, new_mode);
1979 diff_queue(&diff_queued_diff, one, two);
1982 void diff_unmerge(struct diff_options *options,
1985 struct diff_filespec *one, *two;
1986 one = alloc_filespec(path);
1987 two = alloc_filespec(path);
1988 diff_queue(&diff_queued_diff, one, two);