2 * Copyright (C) 2005 Junio C Hamano
12 static const char *diff_opts = "-pu";
13 static unsigned char null_sha1[20] = { 0, };
15 static int detect_rename;
16 static int reverse_diff;
17 static int diff_raw_output = -1;
18 static const char **pathspec;
20 static int minimum_score;
22 static const char *external_diff(void)
24 static const char *external_diff_cmd = NULL;
25 static int done_preparing = 0;
28 return external_diff_cmd;
31 * Default values above are meant to match the
32 * Linux kernel development style. Examples of
33 * alternative styles you can specify via environment
38 if (gitenv("GIT_EXTERNAL_DIFF"))
39 external_diff_cmd = gitenv("GIT_EXTERNAL_DIFF");
41 /* In case external diff fails... */
42 diff_opts = gitenv("GIT_DIFF_OPTS") ? : diff_opts;
45 return external_diff_cmd;
48 /* Help to copy the thing properly quoted for the shell safety.
49 * any single quote is replaced with '\'', and the caller is
50 * expected to enclose the result within a single quote pair.
53 * original sq_expand result
54 * name ==> name ==> 'name'
55 * a b ==> a b ==> 'a b'
56 * a'b ==> a'\''b ==> 'a'\''b'
58 static char *sq_expand(const char *src)
60 static char *buf = NULL;
65 /* count bytes needed to store the quoted string. */
66 for (cnt = 1, cp = src; *cp; cnt++, cp++)
72 while ((c = *src++)) {
76 bp = strcpy(bp, "'\\''");
84 static struct diff_tempfile {
85 const char *name; /* filename external diff should read from */
91 static void builtin_diff(const char *name_a,
93 struct diff_tempfile *temp,
96 int i, next_at, cmd_size;
97 const char *diff_cmd = "diff -L'%s%s' -L'%s%s'";
98 const char *diff_arg = "'%s' '%s'||:"; /* "||:" is to return 0 */
99 const char *input_name_sq[2];
100 const char *path0[2];
101 const char *path1[2];
102 const char *name_sq[2];
105 name_sq[0] = sq_expand(name_a);
106 name_sq[1] = sq_expand(name_b);
108 /* diff_cmd and diff_arg have 6 %s in total which makes
109 * the sum of these strings 12 bytes larger than required.
110 * we use 2 spaces around diff-opts, and we need to count
111 * terminating NUL, so we subtract 9 here.
113 cmd_size = (strlen(diff_cmd) + strlen(diff_opts) +
114 strlen(diff_arg) - 9);
115 for (i = 0; i < 2; i++) {
116 input_name_sq[i] = sq_expand(temp[i].name);
117 if (!strcmp(temp[i].name, "/dev/null")) {
118 path0[i] = "/dev/null";
121 path0[i] = i ? "b/" : "a/";
122 path1[i] = name_sq[i];
124 cmd_size += (strlen(path0[i]) + strlen(path1[i]) +
125 strlen(input_name_sq[i]));
128 cmd = xmalloc(cmd_size);
131 next_at += snprintf(cmd+next_at, cmd_size-next_at,
133 path0[0], path1[0], path0[1], path1[1]);
134 next_at += snprintf(cmd+next_at, cmd_size-next_at,
136 next_at += snprintf(cmd+next_at, cmd_size-next_at,
137 diff_arg, input_name_sq[0], input_name_sq[1]);
139 printf("diff --git a/%s b/%s\n", name_a, name_b);
141 printf("new file mode %s\n", temp[1].mode);
142 else if (!path1[1][0])
143 printf("deleted file mode %s\n", temp[0].mode);
145 if (strcmp(temp[0].mode, temp[1].mode)) {
146 printf("old mode %s\n", temp[0].mode);
147 printf("new mode %s\n", temp[1].mode);
149 if (xfrm_msg && xfrm_msg[0])
150 fputs(xfrm_msg, stdout);
152 if (strncmp(temp[0].mode, temp[1].mode, 3))
153 /* we do not run diff between different kind
159 execlp("/bin/sh","sh", "-c", cmd, NULL);
162 struct diff_filespec *alloc_filespec(const char *path)
164 int namelen = strlen(path);
165 struct diff_filespec *spec = xmalloc(sizeof(*spec) + namelen + 1);
166 spec->path = (char *)(spec + 1);
167 strcpy(spec->path, path);
168 spec->should_free = spec->should_munmap = spec->file_valid = 0;
169 spec->xfrm_flags = 0;
175 void fill_filespec(struct diff_filespec *spec, const unsigned char *sha1,
179 memcpy(spec->sha1, sha1, 20);
180 spec->sha1_valid = !!memcmp(sha1, null_sha1, 20);
181 spec->file_valid = 1;
185 * Given a name and sha1 pair, if the dircache tells us the file in
186 * the work tree has that object contents, return true, so that
187 * prepare_temp_file() does not have to inflate and extract.
189 static int work_tree_matches(const char *name, const unsigned char *sha1)
191 struct cache_entry *ce;
195 /* We do not read the cache ourselves here, because the
196 * benchmark with my previous version that always reads cache
197 * shows that it makes things worse for diff-tree comparing
198 * two linux-2.6 kernel trees in an already checked out work
199 * tree. This is because most diff-tree comparisons deal with
200 * only a small number of files, while reading the cache is
201 * expensive for a large project, and its cost outweighs the
202 * savings we get by not inflating the object to a temporary
203 * file. Practically, this code only helps when we are used
204 * by diff-cache --cached, which does read the cache before
211 pos = cache_name_pos(name, len);
214 ce = active_cache[pos];
215 if ((lstat(name, &st) < 0) ||
216 !S_ISREG(st.st_mode) || /* careful! */
217 ce_match_stat(ce, &st) ||
218 memcmp(sha1, ce->sha1, 20))
220 /* we return 1 only when we can stat, it is a regular file,
221 * stat information matches, and sha1 recorded in the cache
222 * matches. I.e. we know the file in the work tree really is
223 * the same as the <name, sha1> pair.
229 * While doing rename detection and pickaxe operation, we may need to
230 * grab the data for the blob (or file) for our own in-core comparison.
231 * diff_filespec has data and size fields for this purpose.
233 int diff_populate_filespec(struct diff_filespec *s)
237 die("internal error: asking to populate invalid file.");
238 if (S_ISDIR(s->mode))
243 if (!s->sha1_valid ||
244 work_tree_matches(s->path, s->sha1)) {
247 if (lstat(s->path, &st) < 0) {
248 if (errno == ENOENT) {
257 s->size = st.st_size;
260 if (S_ISLNK(st.st_mode)) {
262 s->data = xmalloc(s->size);
264 ret = readlink(s->path, s->data, s->size);
271 fd = open(s->path, O_RDONLY);
274 s->data = mmap(NULL, s->size, PROT_READ, MAP_PRIVATE, fd, 0);
275 s->should_munmap = 1;
280 s->data = read_sha1_file(s->sha1, type, &s->size);
286 void diff_free_filespec_data(struct diff_filespec *s)
290 else if (s->should_munmap)
291 munmap(s->data, s->size);
292 s->should_free = s->should_munmap = 0;
296 static void prep_temp_blob(struct diff_tempfile *temp,
304 strcpy(temp->tmp_path, ".diff_XXXXXX");
305 fd = mkstemp(temp->tmp_path);
307 die("unable to create temp-file");
308 if (write(fd, blob, size) != size)
309 die("unable to write temp-file");
311 temp->name = temp->tmp_path;
312 strcpy(temp->hex, sha1_to_hex(sha1));
314 sprintf(temp->mode, "%06o", mode);
317 static void prepare_temp_file(const char *name,
318 struct diff_tempfile *temp,
319 struct diff_filespec *one)
321 if (!one->file_valid) {
323 /* A '-' entry produces this for file-2, and
324 * a '+' entry produces this for file-1.
326 temp->name = "/dev/null";
327 strcpy(temp->hex, ".");
328 strcpy(temp->mode, ".");
332 if (!one->sha1_valid ||
333 work_tree_matches(name, one->sha1)) {
335 if (lstat(name, &st) < 0) {
337 goto not_a_valid_file;
338 die("stat(%s): %s", name, strerror(errno));
340 if (S_ISLNK(st.st_mode)) {
342 char *buf, buf_[1024];
343 buf = ((sizeof(buf_) < st.st_size) ?
344 xmalloc(st.st_size) : buf_);
345 ret = readlink(name, buf, st.st_size);
347 die("readlink(%s)", name);
348 prep_temp_blob(temp, buf, st.st_size,
350 one->sha1 : null_sha1),
352 one->mode : S_IFLNK));
355 /* we can borrow from the file in the work tree */
357 if (!one->sha1_valid)
358 strcpy(temp->hex, sha1_to_hex(null_sha1));
360 strcpy(temp->hex, sha1_to_hex(one->sha1));
361 sprintf(temp->mode, "%06o",
362 S_IFREG |ce_permissions(st.st_mode));
367 if (diff_populate_filespec(one))
368 die("cannot read data blob for %s", one->path);
369 prep_temp_blob(temp, one->data, one->size,
370 one->sha1, one->mode);
374 static void remove_tempfile(void)
378 for (i = 0; i < 2; i++)
379 if (diff_temp[i].name == diff_temp[i].tmp_path) {
380 unlink(diff_temp[i].name);
381 diff_temp[i].name = NULL;
385 static void remove_tempfile_on_signal(int signo)
390 static int matches_pathspec(const char *name)
398 namelen = strlen(name);
399 for (i = 0; i < speccnt; i++) {
400 int speclen = strlen(pathspec[i]);
401 if (! strncmp(pathspec[i], name, speclen) &&
402 speclen <= namelen &&
403 (name[speclen] == 0 || name[speclen] == '/'))
409 /* An external diff command takes:
411 * diff-cmd name infile1 infile1-sha1 infile1-mode \
412 * infile2 infile2-sha1 infile2-mode [ rename-to ]
415 static void run_external_diff(const char *name,
417 struct diff_filespec *one,
418 struct diff_filespec *two,
419 const char *xfrm_msg)
421 struct diff_tempfile *temp = diff_temp;
424 static int atexit_asked = 0;
426 if (!matches_pathspec(name) && (!other || !matches_pathspec(other)))
430 prepare_temp_file(name, &temp[0], one);
431 prepare_temp_file(other ? : name, &temp[1], two);
432 if (! atexit_asked &&
433 (temp[0].name == temp[0].tmp_path ||
434 temp[1].name == temp[1].tmp_path)) {
436 atexit(remove_tempfile);
438 signal(SIGINT, remove_tempfile_on_signal);
444 die("unable to fork");
446 const char *pgm = external_diff();
449 const char *exec_arg[10];
450 const char **arg = &exec_arg[0];
453 *arg++ = temp[0].name;
454 *arg++ = temp[0].hex;
455 *arg++ = temp[0].mode;
456 *arg++ = temp[1].name;
457 *arg++ = temp[1].hex;
458 *arg++ = temp[1].mode;
464 execvp(pgm, (char *const*) exec_arg);
467 execlp(pgm, pgm, name, NULL);
470 * otherwise we use the built-in one.
473 builtin_diff(name, other ? : name, temp, xfrm_msg);
475 printf("* Unmerged path %s\n", name);
478 if (waitpid(pid, &status, 0) < 0 ||
479 !WIFEXITED(status) || WEXITSTATUS(status)) {
480 /* Earlier we did not check the exit status because
481 * diff exits non-zero if files are different, and
482 * we are not interested in knowing that. It was a
483 * mistake which made it harder to quit a diff-*
484 * session that uses the git-apply-patch-script as
485 * the GIT_EXTERNAL_DIFF. A custom GIT_EXTERNAL_DIFF
486 * should also exit non-zero only when it wants to
487 * abort the entire diff-* session.
490 fprintf(stderr, "external diff died, stopping at %s.\n", name);
496 int diff_scoreopt_parse(const char *opt)
498 int diglen, num, scale, i;
499 if (opt[0] != '-' || (opt[1] != 'M' && opt[1] != 'C'))
500 return -1; /* that is not a -M nor -C option */
501 diglen = strspn(opt+2, "0123456789");
502 if (diglen == 0 || strlen(opt+2) != diglen)
503 return 0; /* use default */
504 sscanf(opt+2, "%d", &num);
505 for (i = 0, scale = 1; i < diglen; i++)
508 /* user says num divided by scale and we say internally that
509 * is MAX_SCORE * num / scale.
511 return MAX_SCORE * num / scale;
514 void diff_setup(int detect_rename_, int minimum_score_, int reverse_diff_,
515 int diff_raw_output_,
516 const char **pathspec_, int speccnt_)
518 detect_rename = detect_rename_;
519 reverse_diff = reverse_diff_;
520 pathspec = pathspec_;
521 diff_raw_output = diff_raw_output_;
523 minimum_score = minimum_score_ ? : DEFAULT_MINIMUM_SCORE;
526 static struct diff_queue_struct queued_diff;
528 struct diff_file_pair *diff_queue(struct diff_queue_struct *queue,
529 struct diff_filespec *one,
530 struct diff_filespec *two)
532 struct diff_file_pair *dp = xmalloc(sizeof(*dp));
536 dp->orig_order = queue->nr;
538 if (queue->alloc <= queue->nr) {
539 queue->alloc = alloc_nr(queue->alloc);
540 queue->queue = xrealloc(queue->queue,
541 sizeof(dp) * queue->alloc);
543 queue->queue[queue->nr++] = dp;
547 static const char *git_object_type(unsigned mode)
549 return S_ISDIR(mode) ? "tree" : "blob";
552 static void diff_flush_raw(struct diff_file_pair *p)
554 struct diff_filespec *it;
557 /* raw output does not have a way to express rename nor copy */
558 if (strcmp(p->one->path, p->two->path))
561 if (p->one->file_valid && p->two->file_valid) {
563 strcpy(hex, sha1_to_hex(p->one->sha1));
564 printf("*%06o->%06o %s %s->%s %s%c",
565 p->one->mode, p->two->mode,
566 git_object_type(p->one->mode),
567 hex, sha1_to_hex(p->two->sha1),
568 p->one->path, diff_raw_output);
572 if (p->one->file_valid) {
580 printf("%c%06o %s %s %s%c",
582 it->mode, git_object_type(it->mode),
583 sha1_to_hex(it->sha1), it->path, diff_raw_output);
586 static void diff_flush_patch(struct diff_file_pair *p)
588 const char *name, *other;
591 other = (strcmp(name, p->two->path) ? p->two->path : NULL);
592 if ((p->one->file_valid && S_ISDIR(p->one->mode)) ||
593 (p->two->file_valid && S_ISDIR(p->two->mode)))
594 return; /* no tree diffs in patch format */
596 run_external_diff(name, other, p->one, p->two, p->xfrm_msg);
599 static int identical(struct diff_filespec *one, struct diff_filespec *two)
601 /* This function is written stricter than necessary to support
602 * the currently implemented transformers, but the idea is to
603 * let transformers to produce diff_file_pairs any way they want,
604 * and filter and clean them up here before producing the output.
607 if (!one->file_valid && !two->file_valid)
608 return 1; /* not interesting */
610 /* deletion, addition, mode change and renames are all interesting. */
611 if ((one->file_valid != two->file_valid) || (one->mode != two->mode) ||
612 strcmp(one->path, two->path))
615 /* both are valid and point at the same path. that is, we are
616 * dealing with a change.
618 if (one->sha1_valid && two->sha1_valid &&
619 !memcmp(one->sha1, two->sha1, sizeof(one->sha1)))
620 return 1; /* no change */
621 if (!one->sha1_valid && !two->sha1_valid)
622 return 1; /* both look at the same file on the filesystem. */
626 static void diff_flush_one(struct diff_file_pair *p)
628 if (identical(p->one, p->two))
630 if (0 <= diff_raw_output)
636 void diff_flush(void)
638 struct diff_queue_struct *q = &queued_diff;
642 diff_detect_rename(q, detect_rename, minimum_score);
643 for (i = 0; i < q->nr; i++)
644 diff_flush_one(q->queue[i]);
646 for (i = 0; i < q->nr; i++) {
647 struct diff_file_pair *p = q->queue[i];
648 diff_free_filespec_data(p->one);
649 diff_free_filespec_data(p->two);
655 q->nr = q->alloc = 0;
658 void diff_addremove(int addremove, unsigned mode,
659 const unsigned char *sha1,
660 const char *base, const char *path)
662 char concatpath[PATH_MAX];
663 struct diff_filespec *one, *two;
665 /* This may look odd, but it is a preparation for
666 * feeding "there are unchanged files which should
667 * not produce diffs, but when you are doing copy
668 * detection you would need them, so here they are"
669 * entries to the diff-core. They will be prefixed
670 * with something like '=' or '*' (I haven't decided
671 * which but should not make any difference).
672 * Feeding the same new and old to diff_change() should
673 * also have the same effect. diff_flush() should
674 * filter the identical ones out at the final output
678 addremove = (addremove == '+' ? '-' :
679 addremove == '-' ? '+' : addremove);
681 if (!path) path = "";
682 sprintf(concatpath, "%s%s", base, path);
683 one = alloc_filespec(concatpath);
684 two = alloc_filespec(concatpath);
686 if (addremove != '+')
687 fill_filespec(one, sha1, mode);
688 if (addremove != '-')
689 fill_filespec(two, sha1, mode);
691 diff_queue(&queued_diff, one, two);
694 void diff_change(unsigned old_mode, unsigned new_mode,
695 const unsigned char *old_sha1,
696 const unsigned char *new_sha1,
697 const char *base, const char *path) {
698 char concatpath[PATH_MAX];
699 struct diff_filespec *one, *two;
703 const unsigned char *tmp_c;
704 tmp = old_mode; old_mode = new_mode; new_mode = tmp;
705 tmp_c = old_sha1; old_sha1 = new_sha1; new_sha1 = tmp_c;
707 if (!path) path = "";
708 sprintf(concatpath, "%s%s", base, path);
709 one = alloc_filespec(concatpath);
710 two = alloc_filespec(concatpath);
711 fill_filespec(one, old_sha1, old_mode);
712 fill_filespec(two, new_sha1, new_mode);
714 diff_queue(&queued_diff, one, two);
717 void diff_unmerge(const char *path)
719 if (0 <= diff_raw_output) {
720 printf("U %s%c", path, diff_raw_output);
723 run_external_diff(path, NULL, NULL, NULL, NULL);