2 * Copyright (C) 2005 Junio C Hamano
12 static const char *diff_opts = "-pu";
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;
72 const char *env_diff_opts;
75 return external_diff_cmd;
78 * Default values above are meant to match the
79 * Linux kernel development style. Examples of
80 * alternative styles you can specify via environment
85 external_diff_cmd = getenv("GIT_EXTERNAL_DIFF");
87 /* In case external diff fails... */
88 env_diff_opts = getenv("GIT_DIFF_OPTS");
89 if (env_diff_opts) diff_opts = env_diff_opts;
92 return external_diff_cmd;
95 #define TEMPFILE_PATH_LEN 50
97 static struct diff_tempfile {
98 const char *name; /* filename external diff should read from */
101 char tmp_path[TEMPFILE_PATH_LEN];
104 static int count_lines(const char *filename)
107 int count, ch, completely_empty = 1, nl_just_seen = 0;
108 in = fopen(filename, "r");
110 while ((ch = fgetc(in)) != EOF)
114 completely_empty = 0;
118 completely_empty = 0;
121 if (completely_empty)
124 count++; /* no trailing newline */
128 static void print_line_count(int count)
138 printf("1,%d", count);
143 static void copy_file(int prefix, const char *filename)
146 int ch, nl_just_seen = 1;
147 in = fopen(filename, "r");
148 while ((ch = fgetc(in)) != EOF) {
159 printf("\n\\ No newline at end of file\n");
162 static void emit_rewrite_diff(const char *name_a,
164 struct diff_tempfile *temp)
166 /* Use temp[i].name as input, name_a and name_b as labels */
168 lc_a = count_lines(temp[0].name);
169 lc_b = count_lines(temp[1].name);
170 printf("--- %s\n+++ %s\n@@ -", name_a, name_b);
171 print_line_count(lc_a);
173 print_line_count(lc_b);
176 copy_file('-', temp[0].name);
178 copy_file('+', temp[1].name);
181 static void builtin_diff(const char *name_a,
183 struct diff_tempfile *temp,
184 const char *xfrm_msg,
185 int complete_rewrite)
187 int i, next_at, cmd_size;
188 const char *const diff_cmd = "diff -L%s -L%s";
189 const char *const diff_arg = "-- %s %s||:"; /* "||:" is to return 0 */
190 const char *input_name_sq[2];
191 const char *label_path[2];
194 /* diff_cmd and diff_arg have 4 %s in total which makes
195 * the sum of these strings 8 bytes larger than required.
196 * we use 2 spaces around diff-opts, and we need to count
197 * terminating NUL; we used to subtract 5 here, but we do not
198 * care about small leaks in this subprocess that is about
199 * to exec "diff" anymore.
201 cmd_size = (strlen(diff_cmd) + strlen(diff_opts) + strlen(diff_arg)
204 for (i = 0; i < 2; i++) {
205 input_name_sq[i] = sq_quote(temp[i].name);
206 if (!strcmp(temp[i].name, "/dev/null"))
207 label_path[i] = "/dev/null";
209 label_path[i] = sq_quote(quote_two("a/", name_a));
211 label_path[i] = sq_quote(quote_two("b/", name_b));
212 cmd_size += (strlen(label_path[i]) + strlen(input_name_sq[i]));
215 cmd = xmalloc(cmd_size);
218 next_at += snprintf(cmd+next_at, cmd_size-next_at,
219 diff_cmd, label_path[0], label_path[1]);
220 next_at += snprintf(cmd+next_at, cmd_size-next_at,
222 next_at += snprintf(cmd+next_at, cmd_size-next_at,
223 diff_arg, input_name_sq[0], input_name_sq[1]);
225 printf("diff --git %s %s\n",
226 quote_two("a/", name_a), quote_two("b/", name_b));
227 if (label_path[0][0] == '/') {
229 printf("new file mode %s\n", temp[1].mode);
230 if (xfrm_msg && xfrm_msg[0])
233 else if (label_path[1][0] == '/') {
234 printf("deleted file mode %s\n", temp[0].mode);
235 if (xfrm_msg && xfrm_msg[0])
239 if (strcmp(temp[0].mode, temp[1].mode)) {
240 printf("old mode %s\n", temp[0].mode);
241 printf("new mode %s\n", temp[1].mode);
243 if (xfrm_msg && xfrm_msg[0])
245 if (strncmp(temp[0].mode, temp[1].mode, 3))
246 /* we do not run diff between different kind
250 if (complete_rewrite) {
252 emit_rewrite_diff(name_a, name_b, temp);
257 execlp("/bin/sh","sh", "-c", cmd, NULL);
260 struct diff_filespec *alloc_filespec(const char *path)
262 int namelen = strlen(path);
263 struct diff_filespec *spec = xmalloc(sizeof(*spec) + namelen + 1);
265 memset(spec, 0, sizeof(*spec));
266 spec->path = (char *)(spec + 1);
267 memcpy(spec->path, path, namelen+1);
271 void fill_filespec(struct diff_filespec *spec, const unsigned char *sha1,
275 spec->mode = DIFF_FILE_CANON_MODE(mode);
276 memcpy(spec->sha1, sha1, 20);
277 spec->sha1_valid = !!memcmp(sha1, null_sha1, 20);
282 * Given a name and sha1 pair, if the dircache tells us the file in
283 * the work tree has that object contents, return true, so that
284 * prepare_temp_file() does not have to inflate and extract.
286 static int work_tree_matches(const char *name, const unsigned char *sha1)
288 struct cache_entry *ce;
292 /* We do not read the cache ourselves here, because the
293 * benchmark with my previous version that always reads cache
294 * shows that it makes things worse for diff-tree comparing
295 * two linux-2.6 kernel trees in an already checked out work
296 * tree. This is because most diff-tree comparisons deal with
297 * only a small number of files, while reading the cache is
298 * expensive for a large project, and its cost outweighs the
299 * savings we get by not inflating the object to a temporary
300 * file. Practically, this code only helps when we are used
301 * by diff-cache --cached, which does read the cache before
308 pos = cache_name_pos(name, len);
311 ce = active_cache[pos];
312 if ((lstat(name, &st) < 0) ||
313 !S_ISREG(st.st_mode) || /* careful! */
314 ce_match_stat(ce, &st) ||
315 memcmp(sha1, ce->sha1, 20))
317 /* we return 1 only when we can stat, it is a regular file,
318 * stat information matches, and sha1 recorded in the cache
319 * matches. I.e. we know the file in the work tree really is
320 * the same as the <name, sha1> pair.
325 static struct sha1_size_cache {
326 unsigned char sha1[20];
329 static int sha1_size_cache_nr, sha1_size_cache_alloc;
331 static struct sha1_size_cache *locate_size_cache(unsigned char *sha1,
336 struct sha1_size_cache *e;
339 last = sha1_size_cache_nr;
340 while (last > first) {
341 int cmp, next = (last + first) >> 1;
342 e = sha1_size_cache[next];
343 cmp = memcmp(e->sha1, sha1, 20);
355 /* insert to make it at "first" */
356 if (sha1_size_cache_alloc <= sha1_size_cache_nr) {
357 sha1_size_cache_alloc = alloc_nr(sha1_size_cache_alloc);
358 sha1_size_cache = xrealloc(sha1_size_cache,
359 sha1_size_cache_alloc *
360 sizeof(*sha1_size_cache));
362 sha1_size_cache_nr++;
363 if (first < sha1_size_cache_nr)
364 memmove(sha1_size_cache + first + 1, sha1_size_cache + first,
365 (sha1_size_cache_nr - first - 1) *
366 sizeof(*sha1_size_cache));
367 e = xmalloc(sizeof(struct sha1_size_cache));
368 sha1_size_cache[first] = e;
369 memcpy(e->sha1, sha1, 20);
375 * While doing rename detection and pickaxe operation, we may need to
376 * grab the data for the blob (or file) for our own in-core comparison.
377 * diff_filespec has data and size fields for this purpose.
379 int diff_populate_filespec(struct diff_filespec *s, int size_only)
382 if (!DIFF_FILE_VALID(s))
383 die("internal error: asking to populate invalid file.");
384 if (S_ISDIR(s->mode))
392 if (!s->sha1_valid ||
393 work_tree_matches(s->path, s->sha1)) {
396 if (lstat(s->path, &st) < 0) {
397 if (errno == ENOENT) {
406 s->size = st.st_size;
411 if (S_ISLNK(st.st_mode)) {
413 s->data = xmalloc(s->size);
415 ret = readlink(s->path, s->data, s->size);
422 fd = open(s->path, O_RDONLY);
425 s->data = mmap(NULL, s->size, PROT_READ, MAP_PRIVATE, fd, 0);
427 if (s->data == MAP_FAILED)
429 s->should_munmap = 1;
433 struct sha1_size_cache *e;
436 e = locate_size_cache(s->sha1, 1, 0);
441 if (!sha1_object_info(s->sha1, type, &s->size))
442 locate_size_cache(s->sha1, 0, s->size);
445 s->data = read_sha1_file(s->sha1, type, &s->size);
452 void diff_free_filespec_data(struct diff_filespec *s)
456 else if (s->should_munmap)
457 munmap(s->data, s->size);
458 s->should_free = s->should_munmap = 0;
462 static void prep_temp_blob(struct diff_tempfile *temp,
465 const unsigned char *sha1,
470 fd = git_mkstemp(temp->tmp_path, TEMPFILE_PATH_LEN, ".diff_XXXXXX");
472 die("unable to create temp-file");
473 if (write(fd, blob, size) != size)
474 die("unable to write temp-file");
476 temp->name = temp->tmp_path;
477 strcpy(temp->hex, sha1_to_hex(sha1));
479 sprintf(temp->mode, "%06o", mode);
482 static void prepare_temp_file(const char *name,
483 struct diff_tempfile *temp,
484 struct diff_filespec *one)
486 if (!DIFF_FILE_VALID(one)) {
488 /* A '-' entry produces this for file-2, and
489 * a '+' entry produces this for file-1.
491 temp->name = "/dev/null";
492 strcpy(temp->hex, ".");
493 strcpy(temp->mode, ".");
497 if (!one->sha1_valid ||
498 work_tree_matches(name, one->sha1)) {
500 if (lstat(name, &st) < 0) {
502 goto not_a_valid_file;
503 die("stat(%s): %s", name, strerror(errno));
505 if (S_ISLNK(st.st_mode)) {
507 char buf[PATH_MAX + 1]; /* ought to be SYMLINK_MAX */
508 if (sizeof(buf) <= st.st_size)
509 die("symlink too long: %s", name);
510 ret = readlink(name, buf, st.st_size);
512 die("readlink(%s)", name);
513 prep_temp_blob(temp, buf, st.st_size,
515 one->sha1 : null_sha1),
517 one->mode : S_IFLNK));
520 /* we can borrow from the file in the work tree */
522 if (!one->sha1_valid)
523 strcpy(temp->hex, sha1_to_hex(null_sha1));
525 strcpy(temp->hex, sha1_to_hex(one->sha1));
526 /* Even though we may sometimes borrow the
527 * contents from the work tree, we always want
528 * one->mode. mode is trustworthy even when
529 * !(one->sha1_valid), as long as
530 * DIFF_FILE_VALID(one).
532 sprintf(temp->mode, "%06o", one->mode);
537 if (diff_populate_filespec(one, 0))
538 die("cannot read data blob for %s", one->path);
539 prep_temp_blob(temp, one->data, one->size,
540 one->sha1, one->mode);
544 static void remove_tempfile(void)
548 for (i = 0; i < 2; i++)
549 if (diff_temp[i].name == diff_temp[i].tmp_path) {
550 unlink(diff_temp[i].name);
551 diff_temp[i].name = NULL;
555 static void remove_tempfile_on_signal(int signo)
560 /* An external diff command takes:
562 * diff-cmd name infile1 infile1-sha1 infile1-mode \
563 * infile2 infile2-sha1 infile2-mode [ rename-to ]
566 static void run_external_diff(const char *pgm,
569 struct diff_filespec *one,
570 struct diff_filespec *two,
571 const char *xfrm_msg,
572 int complete_rewrite)
574 struct diff_tempfile *temp = diff_temp;
577 static int atexit_asked = 0;
578 const char *othername;
580 othername = (other? other : name);
582 prepare_temp_file(name, &temp[0], one);
583 prepare_temp_file(othername, &temp[1], two);
584 if (! atexit_asked &&
585 (temp[0].name == temp[0].tmp_path ||
586 temp[1].name == temp[1].tmp_path)) {
588 atexit(remove_tempfile);
590 signal(SIGINT, remove_tempfile_on_signal);
596 die("unable to fork");
600 const char *exec_arg[10];
601 const char **arg = &exec_arg[0];
604 *arg++ = temp[0].name;
605 *arg++ = temp[0].hex;
606 *arg++ = temp[0].mode;
607 *arg++ = temp[1].name;
608 *arg++ = temp[1].hex;
609 *arg++ = temp[1].mode;
615 execvp(pgm, (char *const*) exec_arg);
618 execlp(pgm, pgm, name, NULL);
621 * otherwise we use the built-in one.
624 builtin_diff(name, othername, temp, xfrm_msg,
627 printf("* Unmerged path %s\n", name);
630 if (waitpid(pid, &status, 0) < 0 ||
631 !WIFEXITED(status) || WEXITSTATUS(status)) {
632 /* Earlier we did not check the exit status because
633 * diff exits non-zero if files are different, and
634 * we are not interested in knowing that. It was a
635 * mistake which made it harder to quit a diff-*
636 * session that uses the git-apply-patch-script as
637 * the GIT_EXTERNAL_DIFF. A custom GIT_EXTERNAL_DIFF
638 * should also exit non-zero only when it wants to
639 * abort the entire diff-* session.
642 fprintf(stderr, "external diff died, stopping at %s.\n", name);
648 static void diff_fill_sha1_info(struct diff_filespec *one)
650 if (DIFF_FILE_VALID(one)) {
651 if (!one->sha1_valid) {
653 if (lstat(one->path, &st) < 0)
654 die("stat %s", one->path);
655 if (index_path(one->sha1, one->path, &st, 0))
656 die("cannot hash %s\n", one->path);
660 memset(one->sha1, 0, 20);
663 static void run_diff(struct diff_filepair *p, struct diff_options *o)
665 const char *pgm = external_diff();
666 char msg[PATH_MAX*2+300], *xfrm_msg;
667 struct diff_filespec *one;
668 struct diff_filespec *two;
671 char *name_munged, *other_munged;
672 int complete_rewrite = 0;
675 if (DIFF_PAIR_UNMERGED(p)) {
677 run_external_diff(pgm, p->one->path, NULL, NULL, NULL, NULL,
683 other = (strcmp(name, p->two->path) ? p->two->path : NULL);
684 name_munged = quote_one(name);
685 other_munged = quote_one(other);
686 one = p->one; two = p->two;
688 diff_fill_sha1_info(one);
689 diff_fill_sha1_info(two);
693 case DIFF_STATUS_COPIED:
694 len += snprintf(msg + len, sizeof(msg) - len,
695 "similarity index %d%%\n"
698 (int)(0.5 + p->score * 100.0/MAX_SCORE),
699 name_munged, other_munged);
701 case DIFF_STATUS_RENAMED:
702 len += snprintf(msg + len, sizeof(msg) - len,
703 "similarity index %d%%\n"
706 (int)(0.5 + p->score * 100.0/MAX_SCORE),
707 name_munged, other_munged);
709 case DIFF_STATUS_MODIFIED:
711 len += snprintf(msg + len, sizeof(msg) - len,
712 "dissimilarity index %d%%\n",
713 (int)(0.5 + p->score *
715 complete_rewrite = 1;
724 if (memcmp(one->sha1, two->sha1, 20)) {
726 int abbrev = o->full_index ? 40 : DEFAULT_ABBREV;
727 memcpy(one_sha1, sha1_to_hex(one->sha1), 41);
729 len += snprintf(msg + len, sizeof(msg) - len,
731 abbrev, one_sha1, abbrev,
732 sha1_to_hex(two->sha1));
733 if (one->mode == two->mode)
734 len += snprintf(msg + len, sizeof(msg) - len,
736 len += snprintf(msg + len, sizeof(msg) - len, "\n");
741 xfrm_msg = len ? msg : NULL;
744 DIFF_FILE_VALID(one) && DIFF_FILE_VALID(two) &&
745 (S_IFMT & one->mode) != (S_IFMT & two->mode)) {
746 /* a filepair that changes between file and symlink
747 * needs to be split into deletion and creation.
749 struct diff_filespec *null = alloc_filespec(two->path);
750 run_external_diff(NULL, name, other, one, null, xfrm_msg, 0);
752 null = alloc_filespec(one->path);
753 run_external_diff(NULL, name, other, null, two, xfrm_msg, 0);
757 run_external_diff(pgm, name, other, one, two, xfrm_msg,
764 void diff_setup(struct diff_options *options)
766 memset(options, 0, sizeof(*options));
767 options->output_format = DIFF_FORMAT_RAW;
768 options->line_termination = '\n';
769 options->break_opt = -1;
770 options->rename_limit = -1;
772 options->change = diff_change;
773 options->add_remove = diff_addremove;
776 int diff_setup_done(struct diff_options *options)
778 if ((options->find_copies_harder &&
779 options->detect_rename != DIFF_DETECT_COPY) ||
780 (0 <= options->rename_limit && !options->detect_rename))
782 if (options->detect_rename && options->rename_limit < 0)
783 options->rename_limit = diff_rename_limit_default;
784 if (options->setup & DIFF_SETUP_USE_CACHE) {
786 /* read-cache does not die even when it fails
787 * so it is safe for us to do this here. Also
788 * it does not smudge active_cache or active_nr
789 * when it fails, so we do not have to worry about
790 * cleaning it up ourselves either.
794 if (options->setup & DIFF_SETUP_USE_SIZE_CACHE)
796 if (options->abbrev <= 0 || 40 < options->abbrev)
797 options->abbrev = 40; /* full */
802 int diff_opt_parse(struct diff_options *options, const char **av, int ac)
804 const char *arg = av[0];
805 if (!strcmp(arg, "-p") || !strcmp(arg, "-u"))
806 options->output_format = DIFF_FORMAT_PATCH;
807 else if (!strcmp(arg, "-z"))
808 options->line_termination = 0;
809 else if (!strncmp(arg, "-l", 2))
810 options->rename_limit = strtoul(arg+2, NULL, 10);
811 else if (!strcmp(arg, "--full-index"))
812 options->full_index = 1;
813 else if (!strcmp(arg, "--name-only"))
814 options->output_format = DIFF_FORMAT_NAME;
815 else if (!strcmp(arg, "--name-status"))
816 options->output_format = DIFF_FORMAT_NAME_STATUS;
817 else if (!strcmp(arg, "-R"))
818 options->reverse_diff = 1;
819 else if (!strncmp(arg, "-S", 2))
820 options->pickaxe = arg + 2;
821 else if (!strcmp(arg, "-s"))
822 options->output_format = DIFF_FORMAT_NO_OUTPUT;
823 else if (!strncmp(arg, "-O", 2))
824 options->orderfile = arg + 2;
825 else if (!strncmp(arg, "--diff-filter=", 14))
826 options->filter = arg + 14;
827 else if (!strcmp(arg, "--pickaxe-all"))
828 options->pickaxe_opts = DIFF_PICKAXE_ALL;
829 else if (!strncmp(arg, "-B", 2)) {
830 if ((options->break_opt =
831 diff_scoreopt_parse(arg)) == -1)
834 else if (!strncmp(arg, "-M", 2)) {
835 if ((options->rename_score =
836 diff_scoreopt_parse(arg)) == -1)
838 options->detect_rename = DIFF_DETECT_RENAME;
840 else if (!strncmp(arg, "-C", 2)) {
841 if ((options->rename_score =
842 diff_scoreopt_parse(arg)) == -1)
844 options->detect_rename = DIFF_DETECT_COPY;
846 else if (!strcmp(arg, "--find-copies-harder"))
847 options->find_copies_harder = 1;
848 else if (!strcmp(arg, "--abbrev"))
849 options->abbrev = DEFAULT_ABBREV;
850 else if (!strncmp(arg, "--abbrev=", 9)) {
851 options->abbrev = strtoul(arg + 9, NULL, 10);
852 if (options->abbrev < MINIMUM_ABBREV)
853 options->abbrev = MINIMUM_ABBREV;
854 else if (40 < options->abbrev)
855 options->abbrev = 40;
862 static int parse_num(const char **cp_p)
864 unsigned long num, scale;
866 const char *cp = *cp_p;
873 if ( !dot && ch == '.' ) {
876 } else if ( ch == '%' ) {
877 scale = dot ? scale*100 : 100;
878 cp++; /* % is always at the end */
880 } else if ( ch >= '0' && ch <= '9' ) {
881 if ( scale < 100000 ) {
883 num = (num*10) + (ch-'0');
892 /* user says num divided by scale and we say internally that
893 * is MAX_SCORE * num / scale.
895 return (num >= scale) ? MAX_SCORE : (MAX_SCORE * num / scale);
898 int diff_scoreopt_parse(const char *opt)
905 if (cmd != 'M' && cmd != 'C' && cmd != 'B')
906 return -1; /* that is not a -M, -C nor -B option */
908 opt1 = parse_num(&opt);
914 else if (*opt != '/')
915 return -1; /* we expect -B80/99 or -B80 */
918 opt2 = parse_num(&opt);
923 return opt1 | (opt2 << 16);
926 struct diff_queue_struct diff_queued_diff;
928 void diff_q(struct diff_queue_struct *queue, struct diff_filepair *dp)
930 if (queue->alloc <= queue->nr) {
931 queue->alloc = alloc_nr(queue->alloc);
932 queue->queue = xrealloc(queue->queue,
933 sizeof(dp) * queue->alloc);
935 queue->queue[queue->nr++] = dp;
938 struct diff_filepair *diff_queue(struct diff_queue_struct *queue,
939 struct diff_filespec *one,
940 struct diff_filespec *two)
942 struct diff_filepair *dp = xmalloc(sizeof(*dp));
947 dp->source_stays = 0;
954 void diff_free_filepair(struct diff_filepair *p)
956 diff_free_filespec_data(p->one);
957 diff_free_filespec_data(p->two);
963 /* This is different from find_unique_abbrev() in that
964 * it needs to deal with 0{40} SHA1.
966 const char *diff_unique_abbrev(const unsigned char *sha1, int len)
971 return sha1_to_hex(sha1);
973 abbrev = find_unique_abbrev(sha1, len);
975 if (!memcmp(sha1, null_sha1, 20)) {
976 char *buf = sha1_to_hex(null_sha1);
978 strcpy(buf + len, "...");
982 return sha1_to_hex(sha1);
984 abblen = strlen(abbrev);
987 if (len < abblen && abblen <= len + 2)
988 sprintf(hex, "%s%.*s", abbrev, len+3-abblen, "..");
990 sprintf(hex, "%s...", abbrev);
993 return sha1_to_hex(sha1);
996 static void diff_flush_raw(struct diff_filepair *p,
997 int line_termination,
998 int inter_name_termination,
999 struct diff_options *options)
1003 int abbrev = options->abbrev;
1004 const char *path_one, *path_two;
1005 int output_format = options->output_format;
1007 path_one = p->one->path;
1008 path_two = p->two->path;
1009 if (line_termination) {
1010 path_one = quote_one(path_one);
1011 path_two = quote_one(path_two);
1015 sprintf(status, "%c%03d", p->status,
1016 (int)(0.5 + p->score * 100.0/MAX_SCORE));
1018 status[0] = p->status;
1021 switch (p->status) {
1022 case DIFF_STATUS_COPIED:
1023 case DIFF_STATUS_RENAMED:
1026 case DIFF_STATUS_ADDED:
1027 case DIFF_STATUS_DELETED:
1034 if (output_format != DIFF_FORMAT_NAME_STATUS) {
1035 printf(":%06o %06o %s ",
1036 p->one->mode, p->two->mode,
1037 diff_unique_abbrev(p->one->sha1, abbrev));
1039 diff_unique_abbrev(p->two->sha1, abbrev));
1041 printf("%s%c%s", status, inter_name_termination, path_one);
1043 printf("%c%s", inter_name_termination, path_two);
1044 putchar(line_termination);
1045 if (path_one != p->one->path)
1046 free((void*)path_one);
1047 if (path_two != p->two->path)
1048 free((void*)path_two);
1051 static void diff_flush_name(struct diff_filepair *p,
1052 int inter_name_termination,
1053 int line_termination)
1055 char *path = p->two->path;
1057 if (line_termination)
1058 path = quote_one(p->two->path);
1060 path = p->two->path;
1061 printf("%s%c", path, line_termination);
1062 if (p->two->path != path)
1066 int diff_unmodified_pair(struct diff_filepair *p)
1068 /* This function is written stricter than necessary to support
1069 * the currently implemented transformers, but the idea is to
1070 * let transformers to produce diff_filepairs any way they want,
1071 * and filter and clean them up here before producing the output.
1073 struct diff_filespec *one, *two;
1075 if (DIFF_PAIR_UNMERGED(p))
1076 return 0; /* unmerged is interesting */
1081 /* deletion, addition, mode or type change
1082 * and rename are all interesting.
1084 if (DIFF_FILE_VALID(one) != DIFF_FILE_VALID(two) ||
1085 DIFF_PAIR_MODE_CHANGED(p) ||
1086 strcmp(one->path, two->path))
1089 /* both are valid and point at the same path. that is, we are
1090 * dealing with a change.
1092 if (one->sha1_valid && two->sha1_valid &&
1093 !memcmp(one->sha1, two->sha1, sizeof(one->sha1)))
1094 return 1; /* no change */
1095 if (!one->sha1_valid && !two->sha1_valid)
1096 return 1; /* both look at the same file on the filesystem. */
1100 static void diff_flush_patch(struct diff_filepair *p, struct diff_options *o)
1102 if (diff_unmodified_pair(p))
1105 if ((DIFF_FILE_VALID(p->one) && S_ISDIR(p->one->mode)) ||
1106 (DIFF_FILE_VALID(p->two) && S_ISDIR(p->two->mode)))
1107 return; /* no tree diffs in patch format */
1112 int diff_queue_is_empty(void)
1114 struct diff_queue_struct *q = &diff_queued_diff;
1116 for (i = 0; i < q->nr; i++)
1117 if (!diff_unmodified_pair(q->queue[i]))
1123 void diff_debug_filespec(struct diff_filespec *s, int x, const char *one)
1125 fprintf(stderr, "queue[%d] %s (%s) %s %06o %s\n",
1128 DIFF_FILE_VALID(s) ? "valid" : "invalid",
1130 s->sha1_valid ? sha1_to_hex(s->sha1) : "");
1131 fprintf(stderr, "queue[%d] %s size %lu flags %d\n",
1133 s->size, s->xfrm_flags);
1136 void diff_debug_filepair(const struct diff_filepair *p, int i)
1138 diff_debug_filespec(p->one, i, "one");
1139 diff_debug_filespec(p->two, i, "two");
1140 fprintf(stderr, "score %d, status %c stays %d broken %d\n",
1141 p->score, p->status ? p->status : '?',
1142 p->source_stays, p->broken_pair);
1145 void diff_debug_queue(const char *msg, struct diff_queue_struct *q)
1149 fprintf(stderr, "%s\n", msg);
1150 fprintf(stderr, "q->nr = %d\n", q->nr);
1151 for (i = 0; i < q->nr; i++) {
1152 struct diff_filepair *p = q->queue[i];
1153 diff_debug_filepair(p, i);
1158 static void diff_resolve_rename_copy(void)
1161 struct diff_filepair *p, *pp;
1162 struct diff_queue_struct *q = &diff_queued_diff;
1164 diff_debug_queue("resolve-rename-copy", q);
1166 for (i = 0; i < q->nr; i++) {
1168 p->status = 0; /* undecided */
1169 if (DIFF_PAIR_UNMERGED(p))
1170 p->status = DIFF_STATUS_UNMERGED;
1171 else if (!DIFF_FILE_VALID(p->one))
1172 p->status = DIFF_STATUS_ADDED;
1173 else if (!DIFF_FILE_VALID(p->two))
1174 p->status = DIFF_STATUS_DELETED;
1175 else if (DIFF_PAIR_TYPE_CHANGED(p))
1176 p->status = DIFF_STATUS_TYPE_CHANGED;
1178 /* from this point on, we are dealing with a pair
1179 * whose both sides are valid and of the same type, i.e.
1180 * either in-place edit or rename/copy edit.
1182 else if (DIFF_PAIR_RENAME(p)) {
1183 if (p->source_stays) {
1184 p->status = DIFF_STATUS_COPIED;
1187 /* See if there is some other filepair that
1188 * copies from the same source as us. If so
1189 * we are a copy. Otherwise we are either a
1190 * copy if the path stays, or a rename if it
1191 * does not, but we already handled "stays" case.
1193 for (j = i + 1; j < q->nr; j++) {
1195 if (strcmp(pp->one->path, p->one->path))
1196 continue; /* not us */
1197 if (!DIFF_PAIR_RENAME(pp))
1198 continue; /* not a rename/copy */
1199 /* pp is a rename/copy from the same source */
1200 p->status = DIFF_STATUS_COPIED;
1204 p->status = DIFF_STATUS_RENAMED;
1206 else if (memcmp(p->one->sha1, p->two->sha1, 20) ||
1207 p->one->mode != p->two->mode)
1208 p->status = DIFF_STATUS_MODIFIED;
1210 /* This is a "no-change" entry and should not
1211 * happen anymore, but prepare for broken callers.
1213 error("feeding unmodified %s to diffcore",
1215 p->status = DIFF_STATUS_UNKNOWN;
1218 diff_debug_queue("resolve-rename-copy done", q);
1221 void diff_flush(struct diff_options *options)
1223 struct diff_queue_struct *q = &diff_queued_diff;
1225 int inter_name_termination = '\t';
1226 int diff_output_format = options->output_format;
1227 int line_termination = options->line_termination;
1229 if (!line_termination)
1230 inter_name_termination = 0;
1232 for (i = 0; i < q->nr; i++) {
1233 struct diff_filepair *p = q->queue[i];
1234 if ((diff_output_format == DIFF_FORMAT_NO_OUTPUT) ||
1235 (p->status == DIFF_STATUS_UNKNOWN))
1238 die("internal error in diff-resolve-rename-copy");
1239 switch (diff_output_format) {
1240 case DIFF_FORMAT_PATCH:
1241 diff_flush_patch(p, options);
1243 case DIFF_FORMAT_RAW:
1244 case DIFF_FORMAT_NAME_STATUS:
1245 diff_flush_raw(p, line_termination,
1246 inter_name_termination,
1249 case DIFF_FORMAT_NAME:
1251 inter_name_termination,
1255 diff_free_filepair(q->queue[i]);
1259 q->nr = q->alloc = 0;
1262 static void diffcore_apply_filter(const char *filter)
1265 struct diff_queue_struct *q = &diff_queued_diff;
1266 struct diff_queue_struct outq;
1268 outq.nr = outq.alloc = 0;
1273 if (strchr(filter, DIFF_STATUS_FILTER_AON)) {
1275 for (i = found = 0; !found && i < q->nr; i++) {
1276 struct diff_filepair *p = q->queue[i];
1277 if (((p->status == DIFF_STATUS_MODIFIED) &&
1279 strchr(filter, DIFF_STATUS_FILTER_BROKEN)) ||
1281 strchr(filter, DIFF_STATUS_MODIFIED)))) ||
1282 ((p->status != DIFF_STATUS_MODIFIED) &&
1283 strchr(filter, p->status)))
1289 /* otherwise we will clear the whole queue
1290 * by copying the empty outq at the end of this
1291 * function, but first clear the current entries
1294 for (i = 0; i < q->nr; i++)
1295 diff_free_filepair(q->queue[i]);
1298 /* Only the matching ones */
1299 for (i = 0; i < q->nr; i++) {
1300 struct diff_filepair *p = q->queue[i];
1302 if (((p->status == DIFF_STATUS_MODIFIED) &&
1304 strchr(filter, DIFF_STATUS_FILTER_BROKEN)) ||
1306 strchr(filter, DIFF_STATUS_MODIFIED)))) ||
1307 ((p->status != DIFF_STATUS_MODIFIED) &&
1308 strchr(filter, p->status)))
1311 diff_free_filepair(p);
1318 void diffcore_std(struct diff_options *options)
1320 if (options->paths && options->paths[0])
1321 diffcore_pathspec(options->paths);
1322 if (options->break_opt != -1)
1323 diffcore_break(options->break_opt);
1324 if (options->detect_rename)
1325 diffcore_rename(options);
1326 if (options->break_opt != -1)
1327 diffcore_merge_broken();
1328 if (options->pickaxe)
1329 diffcore_pickaxe(options->pickaxe, options->pickaxe_opts);
1330 if (options->orderfile)
1331 diffcore_order(options->orderfile);
1332 diff_resolve_rename_copy();
1333 diffcore_apply_filter(options->filter);
1337 void diffcore_std_no_resolve(struct diff_options *options)
1339 if (options->pickaxe)
1340 diffcore_pickaxe(options->pickaxe, options->pickaxe_opts);
1341 if (options->orderfile)
1342 diffcore_order(options->orderfile);
1343 diffcore_apply_filter(options->filter);
1346 void diff_addremove(struct diff_options *options,
1347 int addremove, unsigned mode,
1348 const unsigned char *sha1,
1349 const char *base, const char *path)
1351 char concatpath[PATH_MAX];
1352 struct diff_filespec *one, *two;
1354 /* This may look odd, but it is a preparation for
1355 * feeding "there are unchanged files which should
1356 * not produce diffs, but when you are doing copy
1357 * detection you would need them, so here they are"
1358 * entries to the diff-core. They will be prefixed
1359 * with something like '=' or '*' (I haven't decided
1360 * which but should not make any difference).
1361 * Feeding the same new and old to diff_change()
1362 * also has the same effect.
1363 * Before the final output happens, they are pruned after
1364 * merged into rename/copy pairs as appropriate.
1366 if (options->reverse_diff)
1367 addremove = (addremove == '+' ? '-' :
1368 addremove == '-' ? '+' : addremove);
1370 if (!path) path = "";
1371 sprintf(concatpath, "%s%s", base, path);
1372 one = alloc_filespec(concatpath);
1373 two = alloc_filespec(concatpath);
1375 if (addremove != '+')
1376 fill_filespec(one, sha1, mode);
1377 if (addremove != '-')
1378 fill_filespec(two, sha1, mode);
1380 diff_queue(&diff_queued_diff, one, two);
1383 void diff_change(struct diff_options *options,
1384 unsigned old_mode, unsigned new_mode,
1385 const unsigned char *old_sha1,
1386 const unsigned char *new_sha1,
1387 const char *base, const char *path)
1389 char concatpath[PATH_MAX];
1390 struct diff_filespec *one, *two;
1392 if (options->reverse_diff) {
1394 const unsigned char *tmp_c;
1395 tmp = old_mode; old_mode = new_mode; new_mode = tmp;
1396 tmp_c = old_sha1; old_sha1 = new_sha1; new_sha1 = tmp_c;
1398 if (!path) path = "";
1399 sprintf(concatpath, "%s%s", base, path);
1400 one = alloc_filespec(concatpath);
1401 two = alloc_filespec(concatpath);
1402 fill_filespec(one, old_sha1, old_mode);
1403 fill_filespec(two, new_sha1, new_mode);
1405 diff_queue(&diff_queued_diff, one, two);
1408 void diff_unmerge(struct diff_options *options,
1411 struct diff_filespec *one, *two;
1412 one = alloc_filespec(path);
1413 two = alloc_filespec(path);
1414 diff_queue(&diff_queued_diff, one, two);