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';
19 static const char **pathspec
;
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
= 0;
169 spec
->xfrm_flags
= 0;
173 memset(spec
->sha1
, 0, 20);
177 void fill_filespec(struct diff_filespec
*spec
, const unsigned char *sha1
,
180 if (mode
) { /* just playing defensive */
182 memcpy(spec
->sha1
, sha1
, 20);
183 spec
->sha1_valid
= !!memcmp(sha1
, null_sha1
, 20);
188 * Given a name and sha1 pair, if the dircache tells us the file in
189 * the work tree has that object contents, return true, so that
190 * prepare_temp_file() does not have to inflate and extract.
192 static int work_tree_matches(const char *name
, const unsigned char *sha1
)
194 struct cache_entry
*ce
;
198 /* We do not read the cache ourselves here, because the
199 * benchmark with my previous version that always reads cache
200 * shows that it makes things worse for diff-tree comparing
201 * two linux-2.6 kernel trees in an already checked out work
202 * tree. This is because most diff-tree comparisons deal with
203 * only a small number of files, while reading the cache is
204 * expensive for a large project, and its cost outweighs the
205 * savings we get by not inflating the object to a temporary
206 * file. Practically, this code only helps when we are used
207 * by diff-cache --cached, which does read the cache before
214 pos
= cache_name_pos(name
, len
);
217 ce
= active_cache
[pos
];
218 if ((lstat(name
, &st
) < 0) ||
219 !S_ISREG(st
.st_mode
) || /* careful! */
220 ce_match_stat(ce
, &st
) ||
221 memcmp(sha1
, ce
->sha1
, 20))
223 /* we return 1 only when we can stat, it is a regular file,
224 * stat information matches, and sha1 recorded in the cache
225 * matches. I.e. we know the file in the work tree really is
226 * the same as the <name, sha1> pair.
232 * While doing rename detection and pickaxe operation, we may need to
233 * grab the data for the blob (or file) for our own in-core comparison.
234 * diff_filespec has data and size fields for this purpose.
236 int diff_populate_filespec(struct diff_filespec
*s
)
239 if (!DIFF_FILE_VALID(s
))
240 die("internal error: asking to populate invalid file.");
241 if (S_ISDIR(s
->mode
))
246 if (!s
->sha1_valid
||
247 work_tree_matches(s
->path
, s
->sha1
)) {
250 if (lstat(s
->path
, &st
) < 0) {
251 if (errno
== ENOENT
) {
260 s
->size
= st
.st_size
;
263 if (S_ISLNK(st
.st_mode
)) {
265 s
->data
= xmalloc(s
->size
);
267 ret
= readlink(s
->path
, s
->data
, s
->size
);
274 fd
= open(s
->path
, O_RDONLY
);
277 s
->data
= mmap(NULL
, s
->size
, PROT_READ
, MAP_PRIVATE
, fd
, 0);
278 s
->should_munmap
= 1;
283 s
->data
= read_sha1_file(s
->sha1
, type
, &s
->size
);
289 void diff_free_filespec_data(struct diff_filespec
*s
)
293 else if (s
->should_munmap
)
294 munmap(s
->data
, s
->size
);
295 s
->should_free
= s
->should_munmap
= 0;
299 static void prep_temp_blob(struct diff_tempfile
*temp
,
307 strcpy(temp
->tmp_path
, ".diff_XXXXXX");
308 fd
= mkstemp(temp
->tmp_path
);
310 die("unable to create temp-file");
311 if (write(fd
, blob
, size
) != size
)
312 die("unable to write temp-file");
314 temp
->name
= temp
->tmp_path
;
315 strcpy(temp
->hex
, sha1_to_hex(sha1
));
317 sprintf(temp
->mode
, "%06o", mode
);
320 static void prepare_temp_file(const char *name
,
321 struct diff_tempfile
*temp
,
322 struct diff_filespec
*one
)
324 if (!DIFF_FILE_VALID(one
)) {
326 /* A '-' entry produces this for file-2, and
327 * a '+' entry produces this for file-1.
329 temp
->name
= "/dev/null";
330 strcpy(temp
->hex
, ".");
331 strcpy(temp
->mode
, ".");
335 if (!one
->sha1_valid
||
336 work_tree_matches(name
, one
->sha1
)) {
338 if (lstat(name
, &st
) < 0) {
340 goto not_a_valid_file
;
341 die("stat(%s): %s", name
, strerror(errno
));
343 if (S_ISLNK(st
.st_mode
)) {
345 char *buf
, buf_
[1024];
346 buf
= ((sizeof(buf_
) < st
.st_size
) ?
347 xmalloc(st
.st_size
) : buf_
);
348 ret
= readlink(name
, buf
, st
.st_size
);
350 die("readlink(%s)", name
);
351 prep_temp_blob(temp
, buf
, st
.st_size
,
353 one
->sha1
: null_sha1
),
355 one
->mode
: S_IFLNK
));
358 /* we can borrow from the file in the work tree */
360 if (!one
->sha1_valid
)
361 strcpy(temp
->hex
, sha1_to_hex(null_sha1
));
363 strcpy(temp
->hex
, sha1_to_hex(one
->sha1
));
364 sprintf(temp
->mode
, "%06o",
365 S_IFREG
|ce_permissions(st
.st_mode
));
370 if (diff_populate_filespec(one
))
371 die("cannot read data blob for %s", one
->path
);
372 prep_temp_blob(temp
, one
->data
, one
->size
,
373 one
->sha1
, one
->mode
);
377 static void remove_tempfile(void)
381 for (i
= 0; i
< 2; i
++)
382 if (diff_temp
[i
].name
== diff_temp
[i
].tmp_path
) {
383 unlink(diff_temp
[i
].name
);
384 diff_temp
[i
].name
= NULL
;
388 static void remove_tempfile_on_signal(int signo
)
393 static int matches_pathspec(const char *name
)
401 namelen
= strlen(name
);
402 for (i
= 0; i
< speccnt
; i
++) {
403 int speclen
= strlen(pathspec
[i
]);
404 if (! strncmp(pathspec
[i
], name
, speclen
) &&
405 speclen
<= namelen
&&
406 (name
[speclen
] == 0 || name
[speclen
] == '/'))
412 /* An external diff command takes:
414 * diff-cmd name infile1 infile1-sha1 infile1-mode \
415 * infile2 infile2-sha1 infile2-mode [ rename-to ]
418 static void run_external_diff(const char *name
,
420 struct diff_filespec
*one
,
421 struct diff_filespec
*two
,
422 const char *xfrm_msg
)
424 struct diff_tempfile
*temp
= diff_temp
;
427 static int atexit_asked
= 0;
429 if (!matches_pathspec(name
) && (!other
|| !matches_pathspec(other
)))
433 prepare_temp_file(name
, &temp
[0], one
);
434 prepare_temp_file(other
? : name
, &temp
[1], two
);
435 if (! atexit_asked
&&
436 (temp
[0].name
== temp
[0].tmp_path
||
437 temp
[1].name
== temp
[1].tmp_path
)) {
439 atexit(remove_tempfile
);
441 signal(SIGINT
, remove_tempfile_on_signal
);
447 die("unable to fork");
449 const char *pgm
= external_diff();
452 const char *exec_arg
[10];
453 const char **arg
= &exec_arg
[0];
456 *arg
++ = temp
[0].name
;
457 *arg
++ = temp
[0].hex
;
458 *arg
++ = temp
[0].mode
;
459 *arg
++ = temp
[1].name
;
460 *arg
++ = temp
[1].hex
;
461 *arg
++ = temp
[1].mode
;
467 execvp(pgm
, (char *const*) exec_arg
);
470 execlp(pgm
, pgm
, name
, NULL
);
473 * otherwise we use the built-in one.
476 builtin_diff(name
, other
? : name
, temp
, xfrm_msg
);
478 printf("* Unmerged path %s\n", name
);
481 if (waitpid(pid
, &status
, 0) < 0 ||
482 !WIFEXITED(status
) || WEXITSTATUS(status
)) {
483 /* Earlier we did not check the exit status because
484 * diff exits non-zero if files are different, and
485 * we are not interested in knowing that. It was a
486 * mistake which made it harder to quit a diff-*
487 * session that uses the git-apply-patch-script as
488 * the GIT_EXTERNAL_DIFF. A custom GIT_EXTERNAL_DIFF
489 * should also exit non-zero only when it wants to
490 * abort the entire diff-* session.
493 fprintf(stderr
, "external diff died, stopping at %s.\n", name
);
499 int diff_scoreopt_parse(const char *opt
)
501 int diglen
, num
, scale
, i
;
502 if (opt
[0] != '-' || (opt
[1] != 'M' && opt
[1] != 'C'))
503 return -1; /* that is not a -M nor -C option */
504 diglen
= strspn(opt
+2, "0123456789");
505 if (diglen
== 0 || strlen(opt
+2) != diglen
)
506 return 0; /* use default */
507 sscanf(opt
+2, "%d", &num
);
508 for (i
= 0, scale
= 1; i
< diglen
; i
++)
511 /* user says num divided by scale and we say internally that
512 * is MAX_SCORE * num / scale.
514 return MAX_SCORE
* num
/ scale
;
517 void diff_setup(int reverse_diff_
, int diff_output_style
)
519 reverse_diff
= reverse_diff_
;
521 switch (diff_output_style
) {
522 case DIFF_FORMAT_HUMAN
:
523 line_termination
= '\n';
524 inter_name_termination
= '\t';
526 case DIFF_FORMAT_MACHINE
:
527 line_termination
= inter_name_termination
= 0;
529 case DIFF_FORMAT_PATCH
:
535 struct diff_queue_struct diff_queued_diff
;
537 struct diff_filepair
*diff_queue(struct diff_queue_struct
*queue
,
538 struct diff_filespec
*one
,
539 struct diff_filespec
*two
)
541 struct diff_filepair
*dp
= xmalloc(sizeof(*dp
));
545 dp
->orig_order
= queue
->nr
;
547 if (queue
->alloc
<= queue
->nr
) {
548 queue
->alloc
= alloc_nr(queue
->alloc
);
549 queue
->queue
= xrealloc(queue
->queue
,
550 sizeof(dp
) * queue
->alloc
);
552 queue
->queue
[queue
->nr
++] = dp
;
556 static void diff_flush_raw(struct diff_filepair
*p
)
559 * We used to reject rename/copy but new diff-raw can express them.
561 printf(":%06o %06o %s ",
562 p
->one
->mode
, p
->two
->mode
, sha1_to_hex(p
->one
->sha1
));
563 printf("%s%c%s%c%s%c",
564 sha1_to_hex(p
->two
->sha1
), inter_name_termination
,
565 p
->one
->path
, inter_name_termination
,
566 p
->two
->path
, line_termination
);
569 static void diff_flush_patch(struct diff_filepair
*p
)
571 const char *name
, *other
;
574 other
= (strcmp(name
, p
->two
->path
) ? p
->two
->path
: NULL
);
575 if ((DIFF_FILE_VALID(p
->one
) && S_ISDIR(p
->one
->mode
)) ||
576 (DIFF_FILE_VALID(p
->two
) && S_ISDIR(p
->two
->mode
)))
577 return; /* no tree diffs in patch format */
579 run_external_diff(name
, other
, p
->one
, p
->two
, p
->xfrm_msg
);
582 static int identical(struct diff_filespec
*one
, struct diff_filespec
*two
)
584 /* This function is written stricter than necessary to support
585 * the currently implemented transformers, but the idea is to
586 * let transformers to produce diff_filepairs any way they want,
587 * and filter and clean them up here before producing the output.
590 if (!DIFF_FILE_VALID(one
) && !DIFF_FILE_VALID(two
))
591 return 1; /* not interesting */
593 /* deletion, addition, mode change and renames are all interesting. */
594 if (DIFF_FILE_VALID(one
) != DIFF_FILE_VALID(two
) ||
595 (one
->mode
!= two
->mode
) ||
596 strcmp(one
->path
, two
->path
))
599 /* both are valid and point at the same path. that is, we are
600 * dealing with a change.
602 if (one
->sha1_valid
&& two
->sha1_valid
&&
603 !memcmp(one
->sha1
, two
->sha1
, sizeof(one
->sha1
)))
604 return 1; /* no change */
605 if (!one
->sha1_valid
&& !two
->sha1_valid
)
606 return 1; /* both look at the same file on the filesystem. */
610 static void diff_flush_one(struct diff_filepair
*p
)
612 if (identical(p
->one
, p
->two
))
620 int diff_queue_is_empty(void)
622 struct diff_queue_struct
*q
= &diff_queued_diff
;
625 for (i
= 0; i
< q
->nr
; i
++) {
626 struct diff_filepair
*p
= q
->queue
[i
];
627 if (!identical(p
->one
, p
->two
))
633 void diff_flush(const char **pathspec_
, int speccnt_
)
635 struct diff_queue_struct
*q
= &diff_queued_diff
;
638 pathspec
= pathspec_
;
641 for (i
= 0; i
< q
->nr
; i
++)
642 diff_flush_one(q
->queue
[i
]);
644 for (i
= 0; i
< q
->nr
; i
++) {
645 struct diff_filepair
*p
= q
->queue
[i
];
646 diff_free_filespec_data(p
->one
);
647 diff_free_filespec_data(p
->two
);
653 q
->nr
= q
->alloc
= 0;
656 void diff_addremove(int addremove
, unsigned mode
,
657 const unsigned char *sha1
,
658 const char *base
, const char *path
)
660 char concatpath
[PATH_MAX
];
661 struct diff_filespec
*one
, *two
;
663 /* This may look odd, but it is a preparation for
664 * feeding "there are unchanged files which should
665 * not produce diffs, but when you are doing copy
666 * detection you would need them, so here they are"
667 * entries to the diff-core. They will be prefixed
668 * with something like '=' or '*' (I haven't decided
669 * which but should not make any difference).
670 * Feeding the same new and old to diff_change() should
671 * also have the same effect. diff_flush() should
672 * filter the identical ones out at the final output
676 addremove
= (addremove
== '+' ? '-' :
677 addremove
== '-' ? '+' : addremove
);
679 if (!path
) path
= "";
680 sprintf(concatpath
, "%s%s", base
, path
);
681 one
= alloc_filespec(concatpath
);
682 two
= alloc_filespec(concatpath
);
684 if (addremove
!= '+')
685 fill_filespec(one
, sha1
, mode
);
686 if (addremove
!= '-')
687 fill_filespec(two
, sha1
, mode
);
689 diff_queue(&diff_queued_diff
, one
, two
);
692 void diff_guif(unsigned old_mode
,
694 const unsigned char *old_sha1
,
695 const unsigned char *new_sha1
,
696 const char *old_path
,
697 const char *new_path
)
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 one
= alloc_filespec(old_path
);
708 two
= alloc_filespec(new_path
);
710 fill_filespec(one
, old_sha1
, old_mode
);
712 fill_filespec(two
, new_sha1
, new_mode
);
713 diff_queue(&diff_queued_diff
, one
, two
);
716 void diff_change(unsigned old_mode
, unsigned new_mode
,
717 const unsigned char *old_sha1
,
718 const unsigned char *new_sha1
,
719 const char *base
, const char *path
)
721 char concatpath
[PATH_MAX
];
722 struct diff_filespec
*one
, *two
;
726 const unsigned char *tmp_c
;
727 tmp
= old_mode
; old_mode
= new_mode
; new_mode
= tmp
;
728 tmp_c
= old_sha1
; old_sha1
= new_sha1
; new_sha1
= tmp_c
;
730 if (!path
) path
= "";
731 sprintf(concatpath
, "%s%s", base
, path
);
732 one
= alloc_filespec(concatpath
);
733 two
= alloc_filespec(concatpath
);
734 fill_filespec(one
, old_sha1
, old_mode
);
735 fill_filespec(two
, new_sha1
, new_mode
);
737 diff_queue(&diff_queued_diff
, one
, two
);
740 void diff_unmerge(const char *path
)
743 run_external_diff(path
, NULL
, NULL
, NULL
, NULL
);
745 printf("U %s%c", path
, line_termination
);