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 generate_patch
;
17 static int line_termination
= '\n';
18 static int inter_name_termination
= '\t';
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
= 0;
167 spec
->xfrm_flags
= 0;
171 memset(spec
->sha1
, 0, 20);
175 void fill_filespec(struct diff_filespec
*spec
, const unsigned char *sha1
,
178 if (mode
) { /* just playing defensive */
180 memcpy(spec
->sha1
, sha1
, 20);
181 spec
->sha1_valid
= !!memcmp(sha1
, null_sha1
, 20);
186 * Given a name and sha1 pair, if the dircache tells us the file in
187 * the work tree has that object contents, return true, so that
188 * prepare_temp_file() does not have to inflate and extract.
190 static int work_tree_matches(const char *name
, const unsigned char *sha1
)
192 struct cache_entry
*ce
;
196 /* We do not read the cache ourselves here, because the
197 * benchmark with my previous version that always reads cache
198 * shows that it makes things worse for diff-tree comparing
199 * two linux-2.6 kernel trees in an already checked out work
200 * tree. This is because most diff-tree comparisons deal with
201 * only a small number of files, while reading the cache is
202 * expensive for a large project, and its cost outweighs the
203 * savings we get by not inflating the object to a temporary
204 * file. Practically, this code only helps when we are used
205 * by diff-cache --cached, which does read the cache before
212 pos
= cache_name_pos(name
, len
);
215 ce
= active_cache
[pos
];
216 if ((lstat(name
, &st
) < 0) ||
217 !S_ISREG(st
.st_mode
) || /* careful! */
218 ce_match_stat(ce
, &st
) ||
219 memcmp(sha1
, ce
->sha1
, 20))
221 /* we return 1 only when we can stat, it is a regular file,
222 * stat information matches, and sha1 recorded in the cache
223 * matches. I.e. we know the file in the work tree really is
224 * the same as the <name, sha1> pair.
230 * While doing rename detection and pickaxe operation, we may need to
231 * grab the data for the blob (or file) for our own in-core comparison.
232 * diff_filespec has data and size fields for this purpose.
234 int diff_populate_filespec(struct diff_filespec
*s
)
237 if (!DIFF_FILE_VALID(s
))
238 die("internal error: asking to populate invalid file.");
239 if (S_ISDIR(s
->mode
))
244 if (!s
->sha1_valid
||
245 work_tree_matches(s
->path
, s
->sha1
)) {
248 if (lstat(s
->path
, &st
) < 0) {
249 if (errno
== ENOENT
) {
258 s
->size
= st
.st_size
;
261 if (S_ISLNK(st
.st_mode
)) {
263 s
->data
= xmalloc(s
->size
);
265 ret
= readlink(s
->path
, s
->data
, s
->size
);
272 fd
= open(s
->path
, O_RDONLY
);
275 s
->data
= mmap(NULL
, s
->size
, PROT_READ
, MAP_PRIVATE
, fd
, 0);
276 s
->should_munmap
= 1;
281 s
->data
= read_sha1_file(s
->sha1
, type
, &s
->size
);
287 void diff_free_filepair(struct diff_filepair
*p
)
293 void diff_free_filespec_data(struct diff_filespec
*s
)
297 else if (s
->should_munmap
)
298 munmap(s
->data
, s
->size
);
299 s
->should_free
= s
->should_munmap
= 0;
303 static void prep_temp_blob(struct diff_tempfile
*temp
,
311 strcpy(temp
->tmp_path
, ".diff_XXXXXX");
312 fd
= mkstemp(temp
->tmp_path
);
314 die("unable to create temp-file");
315 if (write(fd
, blob
, size
) != size
)
316 die("unable to write temp-file");
318 temp
->name
= temp
->tmp_path
;
319 strcpy(temp
->hex
, sha1_to_hex(sha1
));
321 sprintf(temp
->mode
, "%06o", mode
);
324 static void prepare_temp_file(const char *name
,
325 struct diff_tempfile
*temp
,
326 struct diff_filespec
*one
)
328 if (!DIFF_FILE_VALID(one
)) {
330 /* A '-' entry produces this for file-2, and
331 * a '+' entry produces this for file-1.
333 temp
->name
= "/dev/null";
334 strcpy(temp
->hex
, ".");
335 strcpy(temp
->mode
, ".");
339 if (!one
->sha1_valid
||
340 work_tree_matches(name
, one
->sha1
)) {
342 if (lstat(name
, &st
) < 0) {
344 goto not_a_valid_file
;
345 die("stat(%s): %s", name
, strerror(errno
));
347 if (S_ISLNK(st
.st_mode
)) {
349 char *buf
, buf_
[1024];
350 buf
= ((sizeof(buf_
) < st
.st_size
) ?
351 xmalloc(st
.st_size
) : buf_
);
352 ret
= readlink(name
, buf
, st
.st_size
);
354 die("readlink(%s)", name
);
355 prep_temp_blob(temp
, buf
, st
.st_size
,
357 one
->sha1
: null_sha1
),
359 one
->mode
: S_IFLNK
));
362 /* we can borrow from the file in the work tree */
364 if (!one
->sha1_valid
)
365 strcpy(temp
->hex
, sha1_to_hex(null_sha1
));
367 strcpy(temp
->hex
, sha1_to_hex(one
->sha1
));
368 sprintf(temp
->mode
, "%06o",
369 S_IFREG
|ce_permissions(st
.st_mode
));
374 if (diff_populate_filespec(one
))
375 die("cannot read data blob for %s", one
->path
);
376 prep_temp_blob(temp
, one
->data
, one
->size
,
377 one
->sha1
, one
->mode
);
381 static void remove_tempfile(void)
385 for (i
= 0; i
< 2; i
++)
386 if (diff_temp
[i
].name
== diff_temp
[i
].tmp_path
) {
387 unlink(diff_temp
[i
].name
);
388 diff_temp
[i
].name
= NULL
;
392 static void remove_tempfile_on_signal(int signo
)
397 /* An external diff command takes:
399 * diff-cmd name infile1 infile1-sha1 infile1-mode \
400 * infile2 infile2-sha1 infile2-mode [ rename-to ]
403 static void run_external_diff(const char *name
,
405 struct diff_filespec
*one
,
406 struct diff_filespec
*two
,
407 const char *xfrm_msg
)
409 struct diff_tempfile
*temp
= diff_temp
;
412 static int atexit_asked
= 0;
415 prepare_temp_file(name
, &temp
[0], one
);
416 prepare_temp_file(other
? : name
, &temp
[1], two
);
417 if (! atexit_asked
&&
418 (temp
[0].name
== temp
[0].tmp_path
||
419 temp
[1].name
== temp
[1].tmp_path
)) {
421 atexit(remove_tempfile
);
423 signal(SIGINT
, remove_tempfile_on_signal
);
429 die("unable to fork");
431 const char *pgm
= external_diff();
434 const char *exec_arg
[10];
435 const char **arg
= &exec_arg
[0];
438 *arg
++ = temp
[0].name
;
439 *arg
++ = temp
[0].hex
;
440 *arg
++ = temp
[0].mode
;
441 *arg
++ = temp
[1].name
;
442 *arg
++ = temp
[1].hex
;
443 *arg
++ = temp
[1].mode
;
449 execvp(pgm
, (char *const*) exec_arg
);
452 execlp(pgm
, pgm
, name
, NULL
);
455 * otherwise we use the built-in one.
458 builtin_diff(name
, other
? : name
, temp
, xfrm_msg
);
460 printf("* Unmerged path %s\n", name
);
463 if (waitpid(pid
, &status
, 0) < 0 ||
464 !WIFEXITED(status
) || WEXITSTATUS(status
)) {
465 /* Earlier we did not check the exit status because
466 * diff exits non-zero if files are different, and
467 * we are not interested in knowing that. It was a
468 * mistake which made it harder to quit a diff-*
469 * session that uses the git-apply-patch-script as
470 * the GIT_EXTERNAL_DIFF. A custom GIT_EXTERNAL_DIFF
471 * should also exit non-zero only when it wants to
472 * abort the entire diff-* session.
475 fprintf(stderr
, "external diff died, stopping at %s.\n", name
);
481 void diff_setup(int reverse_diff_
)
483 reverse_diff
= reverse_diff_
;
486 struct diff_queue_struct diff_queued_diff
;
488 void diff_q(struct diff_queue_struct
*queue
, struct diff_filepair
*dp
)
490 if (queue
->alloc
<= queue
->nr
) {
491 queue
->alloc
= alloc_nr(queue
->alloc
);
492 queue
->queue
= xrealloc(queue
->queue
,
493 sizeof(dp
) * queue
->alloc
);
495 queue
->queue
[queue
->nr
++] = dp
;
498 struct diff_filepair
*diff_queue(struct diff_queue_struct
*queue
,
499 struct diff_filespec
*one
,
500 struct diff_filespec
*two
)
502 struct diff_filepair
*dp
= xmalloc(sizeof(*dp
));
506 dp
->orig_order
= queue
->nr
;
512 static void diff_flush_raw(struct diff_filepair
*p
)
514 if (DIFF_PAIR_UNMERGED(p
)) {
515 printf("U %s%c", p
->one
->path
, line_termination
);
518 printf(":%06o %06o %s ",
519 p
->one
->mode
, p
->two
->mode
, sha1_to_hex(p
->one
->sha1
));
520 printf("%s%c%s%c%s%c",
521 sha1_to_hex(p
->two
->sha1
), inter_name_termination
,
522 p
->one
->path
, inter_name_termination
,
523 p
->two
->path
, line_termination
);
526 static void diff_flush_patch(struct diff_filepair
*p
)
528 const char *name
, *other
;
531 other
= (strcmp(name
, p
->two
->path
) ? p
->two
->path
: NULL
);
532 if ((DIFF_FILE_VALID(p
->one
) && S_ISDIR(p
->one
->mode
)) ||
533 (DIFF_FILE_VALID(p
->two
) && S_ISDIR(p
->two
->mode
)))
534 return; /* no tree diffs in patch format */
536 if (DIFF_PAIR_UNMERGED(p
))
537 run_external_diff(name
, NULL
, NULL
, NULL
, NULL
);
539 run_external_diff(name
, other
, p
->one
, p
->two
, p
->xfrm_msg
);
542 static int uninteresting(struct diff_filepair
*p
)
544 /* This function is written stricter than necessary to support
545 * the currently implemented transformers, but the idea is to
546 * let transformers to produce diff_filepairs any way they want,
547 * and filter and clean them up here before producing the output.
549 struct diff_filespec
*one
, *two
;
551 if (DIFF_PAIR_UNMERGED(p
))
552 return 0; /* unmerged is interesting */
557 /* deletion, addition, mode change and renames are all interesting. */
558 if (DIFF_FILE_VALID(one
) != DIFF_FILE_VALID(two
) ||
559 (one
->mode
!= two
->mode
) ||
560 strcmp(one
->path
, two
->path
))
563 /* both are valid and point at the same path. that is, we are
564 * dealing with a change.
566 if (one
->sha1_valid
&& two
->sha1_valid
&&
567 !memcmp(one
->sha1
, two
->sha1
, sizeof(one
->sha1
)))
568 return 1; /* no change */
569 if (!one
->sha1_valid
&& !two
->sha1_valid
)
570 return 1; /* both look at the same file on the filesystem. */
574 void diffcore_prune(void)
577 * Although rename/copy detection wants to have "no-change"
578 * entries fed into them, the downstream do not need to see
579 * them. This function removes such entries.
581 * The applications that use rename/copy should:
583 * (1) feed change and "no-change" entries via diff_queue().
584 * (2) call diffcore_rename, and any other future diffcore_xxx
585 * that would benefit by still having "no-change" entries.
586 * (3) call diffcore_prune
587 * (4) call other diffcore_xxx that do not need to see
588 * "no-change" entries.
590 struct diff_queue_struct
*q
= &diff_queued_diff
;
591 struct diff_queue_struct outq
;
595 outq
.nr
= outq
.alloc
= 0;
597 for (i
= 0; i
< q
->nr
; i
++) {
598 struct diff_filepair
*p
= q
->queue
[i
];
599 if (!uninteresting(p
))
602 diff_free_filepair(p
);
609 static void diff_flush_one(struct diff_filepair
*p
)
611 if (uninteresting(p
))
619 int diff_queue_is_empty(void)
621 struct diff_queue_struct
*q
= &diff_queued_diff
;
624 for (i
= 0; i
< q
->nr
; i
++) {
625 struct diff_filepair
*p
= q
->queue
[i
];
626 if (!uninteresting(p
))
632 void diff_flush(int diff_output_style
)
634 struct diff_queue_struct
*q
= &diff_queued_diff
;
638 switch (diff_output_style
) {
639 case DIFF_FORMAT_HUMAN
:
640 line_termination
= '\n';
641 inter_name_termination
= '\t';
643 case DIFF_FORMAT_MACHINE
:
644 line_termination
= inter_name_termination
= 0;
646 case DIFF_FORMAT_PATCH
:
650 for (i
= 0; i
< q
->nr
; i
++)
651 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 uninteresting 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_guif(unsigned old_mode
,
702 const unsigned char *old_sha1
,
703 const unsigned char *new_sha1
,
704 const char *old_path
,
705 const char *new_path
)
707 struct diff_filespec
*one
, *two
;
711 const unsigned char *tmp_c
;
712 tmp
= old_mode
; old_mode
= new_mode
; new_mode
= tmp
;
713 tmp_c
= old_sha1
; old_sha1
= new_sha1
; new_sha1
= tmp_c
;
715 one
= alloc_filespec(old_path
);
716 two
= alloc_filespec(new_path
);
718 fill_filespec(one
, old_sha1
, old_mode
);
720 fill_filespec(two
, new_sha1
, new_mode
);
721 diff_queue(&diff_queued_diff
, one
, two
);
724 void diff_change(unsigned old_mode
, unsigned new_mode
,
725 const unsigned char *old_sha1
,
726 const unsigned char *new_sha1
,
727 const char *base
, const char *path
)
729 char concatpath
[PATH_MAX
];
730 struct diff_filespec
*one
, *two
;
734 const unsigned char *tmp_c
;
735 tmp
= old_mode
; old_mode
= new_mode
; new_mode
= tmp
;
736 tmp_c
= old_sha1
; old_sha1
= new_sha1
; new_sha1
= tmp_c
;
738 if (!path
) path
= "";
739 sprintf(concatpath
, "%s%s", base
, path
);
740 one
= alloc_filespec(concatpath
);
741 two
= alloc_filespec(concatpath
);
742 fill_filespec(one
, old_sha1
, old_mode
);
743 fill_filespec(two
, new_sha1
, new_mode
);
745 diff_queue(&diff_queued_diff
, one
, two
);
748 void diff_unmerge(const char *path
)
750 struct diff_filespec
*one
, *two
;
751 one
= alloc_filespec(path
);
752 two
= alloc_filespec(path
);
753 diff_queue(&diff_queued_diff
, one
, two
);