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 reverse_diff
;
16 static int diff_raw_output
= -1;
17 static const char **pathspec
;
20 static const char *external_diff(void)
22 static const char *external_diff_cmd
= NULL
;
23 static int done_preparing
= 0;
26 return external_diff_cmd
;
29 * Default values above are meant to match the
30 * Linux kernel development style. Examples of
31 * alternative styles you can specify via environment
36 if (gitenv("GIT_EXTERNAL_DIFF"))
37 external_diff_cmd
= gitenv("GIT_EXTERNAL_DIFF");
39 /* In case external diff fails... */
40 diff_opts
= gitenv("GIT_DIFF_OPTS") ? : diff_opts
;
43 return external_diff_cmd
;
46 /* Help to copy the thing properly quoted for the shell safety.
47 * any single quote is replaced with '\'', and the caller is
48 * expected to enclose the result within a single quote pair.
51 * original sq_expand result
52 * name ==> name ==> 'name'
53 * a b ==> a b ==> 'a b'
54 * a'b ==> a'\''b ==> 'a'\''b'
56 static char *sq_expand(const char *src
)
58 static char *buf
= NULL
;
63 /* count bytes needed to store the quoted string. */
64 for (cnt
= 1, cp
= src
; *cp
; cnt
++, cp
++)
70 while ((c
= *src
++)) {
74 bp
= strcpy(bp
, "'\\''");
82 static struct diff_tempfile
{
83 const char *name
; /* filename external diff should read from */
89 static void builtin_diff(const char *name_a
,
91 struct diff_tempfile
*temp
,
94 int i
, next_at
, cmd_size
;
95 const char *diff_cmd
= "diff -L'%s%s' -L'%s%s'";
96 const char *diff_arg
= "'%s' '%s'||:"; /* "||:" is to return 0 */
97 const char *input_name_sq
[2];
100 const char *name_sq
[2];
103 name_sq
[0] = sq_expand(name_a
);
104 name_sq
[1] = sq_expand(name_b
);
106 /* diff_cmd and diff_arg have 6 %s in total which makes
107 * the sum of these strings 12 bytes larger than required.
108 * we use 2 spaces around diff-opts, and we need to count
109 * terminating NUL, so we subtract 9 here.
111 cmd_size
= (strlen(diff_cmd
) + strlen(diff_opts
) +
112 strlen(diff_arg
) - 9);
113 for (i
= 0; i
< 2; i
++) {
114 input_name_sq
[i
] = sq_expand(temp
[i
].name
);
115 if (!strcmp(temp
[i
].name
, "/dev/null")) {
116 path0
[i
] = "/dev/null";
119 path0
[i
] = i
? "b/" : "a/";
120 path1
[i
] = name_sq
[i
];
122 cmd_size
+= (strlen(path0
[i
]) + strlen(path1
[i
]) +
123 strlen(input_name_sq
[i
]));
126 cmd
= xmalloc(cmd_size
);
129 next_at
+= snprintf(cmd
+next_at
, cmd_size
-next_at
,
131 path0
[0], path1
[0], path0
[1], path1
[1]);
132 next_at
+= snprintf(cmd
+next_at
, cmd_size
-next_at
,
134 next_at
+= snprintf(cmd
+next_at
, cmd_size
-next_at
,
135 diff_arg
, input_name_sq
[0], input_name_sq
[1]);
137 printf("diff --git a/%s b/%s\n", name_a
, name_b
);
139 printf("new file mode %s\n", temp
[1].mode
);
140 else if (!path1
[1][0])
141 printf("deleted file mode %s\n", temp
[0].mode
);
143 if (strcmp(temp
[0].mode
, temp
[1].mode
)) {
144 printf("old mode %s\n", temp
[0].mode
);
145 printf("new mode %s\n", temp
[1].mode
);
147 if (xfrm_msg
&& xfrm_msg
[0])
148 fputs(xfrm_msg
, stdout
);
150 if (strncmp(temp
[0].mode
, temp
[1].mode
, 3))
151 /* we do not run diff between different kind
157 execlp("/bin/sh","sh", "-c", cmd
, NULL
);
160 struct diff_filespec
*alloc_filespec(const char *path
)
162 int namelen
= strlen(path
);
163 struct diff_filespec
*spec
= xmalloc(sizeof(*spec
) + namelen
+ 1);
164 spec
->path
= (char *)(spec
+ 1);
165 strcpy(spec
->path
, path
);
166 spec
->should_free
= spec
->should_munmap
= spec
->file_valid
= 0;
167 spec
->xfrm_flags
= 0;
173 void fill_filespec(struct diff_filespec
*spec
, const unsigned char *sha1
,
177 memcpy(spec
->sha1
, sha1
, 20);
178 spec
->sha1_valid
= !!memcmp(sha1
, null_sha1
, 20);
179 spec
->file_valid
= 1;
183 * Given a name and sha1 pair, if the dircache tells us the file in
184 * the work tree has that object contents, return true, so that
185 * prepare_temp_file() does not have to inflate and extract.
187 static int work_tree_matches(const char *name
, const unsigned char *sha1
)
189 struct cache_entry
*ce
;
193 /* We do not read the cache ourselves here, because the
194 * benchmark with my previous version that always reads cache
195 * shows that it makes things worse for diff-tree comparing
196 * two linux-2.6 kernel trees in an already checked out work
197 * tree. This is because most diff-tree comparisons deal with
198 * only a small number of files, while reading the cache is
199 * expensive for a large project, and its cost outweighs the
200 * savings we get by not inflating the object to a temporary
201 * file. Practically, this code only helps when we are used
202 * by diff-cache --cached, which does read the cache before
209 pos
= cache_name_pos(name
, len
);
212 ce
= active_cache
[pos
];
213 if ((lstat(name
, &st
) < 0) ||
214 !S_ISREG(st
.st_mode
) || /* careful! */
215 ce_match_stat(ce
, &st
) ||
216 memcmp(sha1
, ce
->sha1
, 20))
218 /* we return 1 only when we can stat, it is a regular file,
219 * stat information matches, and sha1 recorded in the cache
220 * matches. I.e. we know the file in the work tree really is
221 * the same as the <name, sha1> pair.
227 * While doing rename detection and pickaxe operation, we may need to
228 * grab the data for the blob (or file) for our own in-core comparison.
229 * diff_filespec has data and size fields for this purpose.
231 int diff_populate_filespec(struct diff_filespec
*s
)
235 die("internal error: asking to populate invalid file.");
236 if (S_ISDIR(s
->mode
))
241 if (!s
->sha1_valid
||
242 work_tree_matches(s
->path
, s
->sha1
)) {
245 if (lstat(s
->path
, &st
) < 0) {
246 if (errno
== ENOENT
) {
255 s
->size
= st
.st_size
;
258 if (S_ISLNK(st
.st_mode
)) {
260 s
->data
= xmalloc(s
->size
);
262 ret
= readlink(s
->path
, s
->data
, s
->size
);
269 fd
= open(s
->path
, O_RDONLY
);
272 s
->data
= mmap(NULL
, s
->size
, PROT_READ
, MAP_PRIVATE
, fd
, 0);
273 s
->should_munmap
= 1;
278 s
->data
= read_sha1_file(s
->sha1
, type
, &s
->size
);
284 void diff_free_filespec_data(struct diff_filespec
*s
)
288 else if (s
->should_munmap
)
289 munmap(s
->data
, s
->size
);
290 s
->should_free
= s
->should_munmap
= 0;
294 static void prep_temp_blob(struct diff_tempfile
*temp
,
302 strcpy(temp
->tmp_path
, ".diff_XXXXXX");
303 fd
= mkstemp(temp
->tmp_path
);
305 die("unable to create temp-file");
306 if (write(fd
, blob
, size
) != size
)
307 die("unable to write temp-file");
309 temp
->name
= temp
->tmp_path
;
310 strcpy(temp
->hex
, sha1_to_hex(sha1
));
312 sprintf(temp
->mode
, "%06o", mode
);
315 static void prepare_temp_file(const char *name
,
316 struct diff_tempfile
*temp
,
317 struct diff_filespec
*one
)
319 if (!one
->file_valid
) {
321 /* A '-' entry produces this for file-2, and
322 * a '+' entry produces this for file-1.
324 temp
->name
= "/dev/null";
325 strcpy(temp
->hex
, ".");
326 strcpy(temp
->mode
, ".");
330 if (!one
->sha1_valid
||
331 work_tree_matches(name
, one
->sha1
)) {
333 if (lstat(name
, &st
) < 0) {
335 goto not_a_valid_file
;
336 die("stat(%s): %s", name
, strerror(errno
));
338 if (S_ISLNK(st
.st_mode
)) {
340 char *buf
, buf_
[1024];
341 buf
= ((sizeof(buf_
) < st
.st_size
) ?
342 xmalloc(st
.st_size
) : buf_
);
343 ret
= readlink(name
, buf
, st
.st_size
);
345 die("readlink(%s)", name
);
346 prep_temp_blob(temp
, buf
, st
.st_size
,
348 one
->sha1
: null_sha1
),
350 one
->mode
: S_IFLNK
));
353 /* we can borrow from the file in the work tree */
355 if (!one
->sha1_valid
)
356 strcpy(temp
->hex
, sha1_to_hex(null_sha1
));
358 strcpy(temp
->hex
, sha1_to_hex(one
->sha1
));
359 sprintf(temp
->mode
, "%06o",
360 S_IFREG
|ce_permissions(st
.st_mode
));
365 if (diff_populate_filespec(one
))
366 die("cannot read data blob for %s", one
->path
);
367 prep_temp_blob(temp
, one
->data
, one
->size
,
368 one
->sha1
, one
->mode
);
372 static void remove_tempfile(void)
376 for (i
= 0; i
< 2; i
++)
377 if (diff_temp
[i
].name
== diff_temp
[i
].tmp_path
) {
378 unlink(diff_temp
[i
].name
);
379 diff_temp
[i
].name
= NULL
;
383 static void remove_tempfile_on_signal(int signo
)
388 static int matches_pathspec(const char *name
)
396 namelen
= strlen(name
);
397 for (i
= 0; i
< speccnt
; i
++) {
398 int speclen
= strlen(pathspec
[i
]);
399 if (! strncmp(pathspec
[i
], name
, speclen
) &&
400 speclen
<= namelen
&&
401 (name
[speclen
] == 0 || name
[speclen
] == '/'))
407 /* An external diff command takes:
409 * diff-cmd name infile1 infile1-sha1 infile1-mode \
410 * infile2 infile2-sha1 infile2-mode [ rename-to ]
413 static void run_external_diff(const char *name
,
415 struct diff_filespec
*one
,
416 struct diff_filespec
*two
,
417 const char *xfrm_msg
)
419 struct diff_tempfile
*temp
= diff_temp
;
422 static int atexit_asked
= 0;
424 if (!matches_pathspec(name
) && (!other
|| !matches_pathspec(other
)))
428 prepare_temp_file(name
, &temp
[0], one
);
429 prepare_temp_file(other
? : name
, &temp
[1], two
);
430 if (! atexit_asked
&&
431 (temp
[0].name
== temp
[0].tmp_path
||
432 temp
[1].name
== temp
[1].tmp_path
)) {
434 atexit(remove_tempfile
);
436 signal(SIGINT
, remove_tempfile_on_signal
);
442 die("unable to fork");
444 const char *pgm
= external_diff();
447 const char *exec_arg
[10];
448 const char **arg
= &exec_arg
[0];
451 *arg
++ = temp
[0].name
;
452 *arg
++ = temp
[0].hex
;
453 *arg
++ = temp
[0].mode
;
454 *arg
++ = temp
[1].name
;
455 *arg
++ = temp
[1].hex
;
456 *arg
++ = temp
[1].mode
;
462 execvp(pgm
, (char *const*) exec_arg
);
465 execlp(pgm
, pgm
, name
, NULL
);
468 * otherwise we use the built-in one.
471 builtin_diff(name
, other
? : name
, temp
, xfrm_msg
);
473 printf("* Unmerged path %s\n", name
);
476 if (waitpid(pid
, &status
, 0) < 0 ||
477 !WIFEXITED(status
) || WEXITSTATUS(status
)) {
478 /* Earlier we did not check the exit status because
479 * diff exits non-zero if files are different, and
480 * we are not interested in knowing that. It was a
481 * mistake which made it harder to quit a diff-*
482 * session that uses the git-apply-patch-script as
483 * the GIT_EXTERNAL_DIFF. A custom GIT_EXTERNAL_DIFF
484 * should also exit non-zero only when it wants to
485 * abort the entire diff-* session.
488 fprintf(stderr
, "external diff died, stopping at %s.\n", name
);
494 int diff_scoreopt_parse(const char *opt
)
496 int diglen
, num
, scale
, i
;
497 if (opt
[0] != '-' || (opt
[1] != 'M' && opt
[1] != 'C'))
498 return -1; /* that is not a -M nor -C option */
499 diglen
= strspn(opt
+2, "0123456789");
500 if (diglen
== 0 || strlen(opt
+2) != diglen
)
501 return 0; /* use default */
502 sscanf(opt
+2, "%d", &num
);
503 for (i
= 0, scale
= 1; i
< diglen
; i
++)
506 /* user says num divided by scale and we say internally that
507 * is MAX_SCORE * num / scale.
509 return MAX_SCORE
* num
/ scale
;
512 void diff_setup(int reverse_diff_
, int diff_raw_output_
)
514 reverse_diff
= reverse_diff_
;
515 diff_raw_output
= diff_raw_output_
;
518 struct diff_queue_struct diff_queued_diff
;
520 struct diff_filepair
*diff_queue(struct diff_queue_struct
*queue
,
521 struct diff_filespec
*one
,
522 struct diff_filespec
*two
)
524 struct diff_filepair
*dp
= xmalloc(sizeof(*dp
));
528 dp
->orig_order
= queue
->nr
;
530 if (queue
->alloc
<= queue
->nr
) {
531 queue
->alloc
= alloc_nr(queue
->alloc
);
532 queue
->queue
= xrealloc(queue
->queue
,
533 sizeof(dp
) * queue
->alloc
);
535 queue
->queue
[queue
->nr
++] = dp
;
539 static const char *git_object_type(unsigned mode
)
541 return S_ISDIR(mode
) ? "tree" : "blob";
544 static void diff_flush_raw(struct diff_filepair
*p
)
546 struct diff_filespec
*it
;
549 /* raw output does not have a way to express rename nor copy */
550 if (strcmp(p
->one
->path
, p
->two
->path
))
553 if (p
->one
->file_valid
&& p
->two
->file_valid
) {
555 strcpy(hex
, sha1_to_hex(p
->one
->sha1
));
556 printf("*%06o->%06o %s %s->%s %s%c",
557 p
->one
->mode
, p
->two
->mode
,
558 git_object_type(p
->one
->mode
),
559 hex
, sha1_to_hex(p
->two
->sha1
),
560 p
->one
->path
, diff_raw_output
);
564 if (p
->one
->file_valid
) {
572 printf("%c%06o %s %s %s%c",
574 it
->mode
, git_object_type(it
->mode
),
575 sha1_to_hex(it
->sha1
), it
->path
, diff_raw_output
);
578 static void diff_flush_patch(struct diff_filepair
*p
)
580 const char *name
, *other
;
583 other
= (strcmp(name
, p
->two
->path
) ? p
->two
->path
: NULL
);
584 if ((p
->one
->file_valid
&& S_ISDIR(p
->one
->mode
)) ||
585 (p
->two
->file_valid
&& S_ISDIR(p
->two
->mode
)))
586 return; /* no tree diffs in patch format */
588 run_external_diff(name
, other
, p
->one
, p
->two
, p
->xfrm_msg
);
591 static int identical(struct diff_filespec
*one
, struct diff_filespec
*two
)
593 /* This function is written stricter than necessary to support
594 * the currently implemented transformers, but the idea is to
595 * let transformers to produce diff_filepairs any way they want,
596 * and filter and clean them up here before producing the output.
599 if (!one
->file_valid
&& !two
->file_valid
)
600 return 1; /* not interesting */
602 /* deletion, addition, mode change and renames are all interesting. */
603 if ((one
->file_valid
!= two
->file_valid
) || (one
->mode
!= two
->mode
) ||
604 strcmp(one
->path
, two
->path
))
607 /* both are valid and point at the same path. that is, we are
608 * dealing with a change.
610 if (one
->sha1_valid
&& two
->sha1_valid
&&
611 !memcmp(one
->sha1
, two
->sha1
, sizeof(one
->sha1
)))
612 return 1; /* no change */
613 if (!one
->sha1_valid
&& !two
->sha1_valid
)
614 return 1; /* both look at the same file on the filesystem. */
618 static void diff_flush_one(struct diff_filepair
*p
)
620 if (identical(p
->one
, p
->two
))
622 if (0 <= diff_raw_output
)
628 int diff_queue_is_empty(void)
630 struct diff_queue_struct
*q
= &diff_queued_diff
;
633 for (i
= 0; i
< q
->nr
; i
++) {
634 struct diff_filepair
*p
= q
->queue
[i
];
635 if (!identical(p
->one
, p
->two
))
641 void diff_flush(const char **pathspec_
, int speccnt_
)
643 struct diff_queue_struct
*q
= &diff_queued_diff
;
646 pathspec
= pathspec_
;
649 for (i
= 0; i
< q
->nr
; i
++)
650 diff_flush_one(q
->queue
[i
]);
652 for (i
= 0; i
< q
->nr
; i
++) {
653 struct diff_filepair
*p
= q
->queue
[i
];
654 diff_free_filespec_data(p
->one
);
655 diff_free_filespec_data(p
->two
);
661 q
->nr
= q
->alloc
= 0;
664 void diff_addremove(int addremove
, unsigned mode
,
665 const unsigned char *sha1
,
666 const char *base
, const char *path
)
668 char concatpath
[PATH_MAX
];
669 struct diff_filespec
*one
, *two
;
671 /* This may look odd, but it is a preparation for
672 * feeding "there are unchanged files which should
673 * not produce diffs, but when you are doing copy
674 * detection you would need them, so here they are"
675 * entries to the diff-core. They will be prefixed
676 * with something like '=' or '*' (I haven't decided
677 * which but should not make any difference).
678 * Feeding the same new and old to diff_change() should
679 * also have the same effect. diff_flush() should
680 * filter the identical ones out at the final output
684 addremove
= (addremove
== '+' ? '-' :
685 addremove
== '-' ? '+' : addremove
);
687 if (!path
) path
= "";
688 sprintf(concatpath
, "%s%s", base
, path
);
689 one
= alloc_filespec(concatpath
);
690 two
= alloc_filespec(concatpath
);
692 if (addremove
!= '+')
693 fill_filespec(one
, sha1
, mode
);
694 if (addremove
!= '-')
695 fill_filespec(two
, sha1
, mode
);
697 diff_queue(&diff_queued_diff
, one
, two
);
700 void diff_change(unsigned old_mode
, unsigned new_mode
,
701 const unsigned char *old_sha1
,
702 const unsigned char *new_sha1
,
703 const char *base
, const char *path
) {
704 char concatpath
[PATH_MAX
];
705 struct diff_filespec
*one
, *two
;
709 const unsigned char *tmp_c
;
710 tmp
= old_mode
; old_mode
= new_mode
; new_mode
= tmp
;
711 tmp_c
= old_sha1
; old_sha1
= new_sha1
; new_sha1
= tmp_c
;
713 if (!path
) path
= "";
714 sprintf(concatpath
, "%s%s", base
, path
);
715 one
= alloc_filespec(concatpath
);
716 two
= alloc_filespec(concatpath
);
717 fill_filespec(one
, old_sha1
, old_mode
);
718 fill_filespec(two
, new_sha1
, new_mode
);
720 diff_queue(&diff_queued_diff
, one
, two
);
723 void diff_unmerge(const char *path
)
725 if (0 <= diff_raw_output
) {
726 printf("U %s%c", path
, diff_raw_output
);
729 run_external_diff(path
, NULL
, NULL
, NULL
, NULL
);