]>
Commit | Line | Data |
---|---|---|
be3cfa85 JH |
1 | /* |
2 | * Copyright (C) 2005 Junio C Hamano | |
3 | */ | |
4 | #include <sys/types.h> | |
5 | #include <sys/wait.h> | |
532149d7 | 6 | #include <signal.h> |
86436c28 JH |
7 | #include "cache.h" |
8 | #include "diff.h" | |
427dcb4b | 9 | #include "diffcore.h" |
86436c28 | 10 | |
d19938ab | 11 | static const char *diff_opts = "-pu"; |
5c97558c | 12 | static unsigned char null_sha1[20] = { 0, }; |
427dcb4b | 13 | |
427dcb4b | 14 | static int reverse_diff; |
86436c28 | 15 | |
be3cfa85 | 16 | static const char *external_diff(void) |
86436c28 | 17 | { |
d19938ab | 18 | static const char *external_diff_cmd = NULL; |
be3cfa85 JH |
19 | static int done_preparing = 0; |
20 | ||
21 | if (done_preparing) | |
22 | return external_diff_cmd; | |
23 | ||
86436c28 JH |
24 | /* |
25 | * Default values above are meant to match the | |
26 | * Linux kernel development style. Examples of | |
27 | * alternative styles you can specify via environment | |
28 | * variables are: | |
29 | * | |
86436c28 JH |
30 | * GIT_DIFF_OPTS="-c"; |
31 | */ | |
d19938ab JH |
32 | if (gitenv("GIT_EXTERNAL_DIFF")) |
33 | external_diff_cmd = gitenv("GIT_EXTERNAL_DIFF"); | |
be3cfa85 JH |
34 | |
35 | /* In case external diff fails... */ | |
d19938ab | 36 | diff_opts = gitenv("GIT_DIFF_OPTS") ? : diff_opts; |
be3cfa85 JH |
37 | |
38 | done_preparing = 1; | |
39 | return external_diff_cmd; | |
86436c28 JH |
40 | } |
41 | ||
42 | /* Help to copy the thing properly quoted for the shell safety. | |
43 | * any single quote is replaced with '\'', and the caller is | |
44 | * expected to enclose the result within a single quote pair. | |
45 | * | |
46 | * E.g. | |
47 | * original sq_expand result | |
48 | * name ==> name ==> 'name' | |
49 | * a b ==> a b ==> 'a b' | |
50 | * a'b ==> a'\''b ==> 'a'\''b' | |
51 | */ | |
52 | static char *sq_expand(const char *src) | |
53 | { | |
54 | static char *buf = NULL; | |
55 | int cnt, c; | |
56 | const char *cp; | |
57 | char *bp; | |
58 | ||
57fe64a4 | 59 | /* count bytes needed to store the quoted string. */ |
86436c28 JH |
60 | for (cnt = 1, cp = src; *cp; cnt++, cp++) |
61 | if (*cp == '\'') | |
62 | cnt += 3; | |
63 | ||
812666c8 | 64 | buf = xmalloc(cnt); |
86436c28 JH |
65 | bp = buf; |
66 | while ((c = *src++)) { | |
67 | if (c != '\'') | |
68 | *bp++ = c; | |
69 | else { | |
70 | bp = strcpy(bp, "'\\''"); | |
71 | bp += 4; | |
72 | } | |
73 | } | |
74 | *bp = 0; | |
75 | return buf; | |
76 | } | |
77 | ||
be3cfa85 | 78 | static struct diff_tempfile { |
427dcb4b | 79 | const char *name; /* filename external diff should read from */ |
be3cfa85 JH |
80 | char hex[41]; |
81 | char mode[10]; | |
82 | char tmp_path[50]; | |
83 | } diff_temp[2]; | |
84 | ||
915838c3 JH |
85 | static void builtin_diff(const char *name_a, |
86 | const char *name_b, | |
57fe64a4 | 87 | struct diff_tempfile *temp, |
427dcb4b | 88 | const char *xfrm_msg) |
86436c28 | 89 | { |
9669e17a | 90 | int i, next_at, cmd_size; |
c983370e | 91 | const char *diff_cmd = "diff -L'%s%s' -L'%s%s'"; |
6fa28064 | 92 | const char *diff_arg = "'%s' '%s'||:"; /* "||:" is to return 0 */ |
2f978138 JH |
93 | const char *input_name_sq[2]; |
94 | const char *path0[2]; | |
95 | const char *path1[2]; | |
915838c3 | 96 | const char *name_sq[2]; |
2f978138 | 97 | char *cmd; |
915838c3 JH |
98 | |
99 | name_sq[0] = sq_expand(name_a); | |
100 | name_sq[1] = sq_expand(name_b); | |
101 | ||
c983370e JH |
102 | /* diff_cmd and diff_arg have 6 %s in total which makes |
103 | * the sum of these strings 12 bytes larger than required. | |
be3cfa85 | 104 | * we use 2 spaces around diff-opts, and we need to count |
c983370e | 105 | * terminating NUL, so we subtract 9 here. |
be3cfa85 | 106 | */ |
9669e17a | 107 | cmd_size = (strlen(diff_cmd) + strlen(diff_opts) + |
c983370e | 108 | strlen(diff_arg) - 9); |
2f978138 JH |
109 | for (i = 0; i < 2; i++) { |
110 | input_name_sq[i] = sq_expand(temp[i].name); | |
111 | if (!strcmp(temp[i].name, "/dev/null")) { | |
112 | path0[i] = "/dev/null"; | |
113 | path1[i] = ""; | |
2f978138 | 114 | } else { |
0980d9b3 | 115 | path0[i] = i ? "b/" : "a/"; |
915838c3 | 116 | path1[i] = name_sq[i]; |
2f978138 JH |
117 | } |
118 | cmd_size += (strlen(path0[i]) + strlen(path1[i]) + | |
c983370e | 119 | strlen(input_name_sq[i])); |
2f978138 | 120 | } |
be3cfa85 | 121 | |
2f978138 JH |
122 | cmd = xmalloc(cmd_size); |
123 | ||
124 | next_at = 0; | |
be3cfa85 | 125 | next_at += snprintf(cmd+next_at, cmd_size-next_at, |
2f978138 | 126 | diff_cmd, |
c983370e | 127 | path0[0], path1[0], path0[1], path1[1]); |
be3cfa85 JH |
128 | next_at += snprintf(cmd+next_at, cmd_size-next_at, |
129 | " %s ", diff_opts); | |
130 | next_at += snprintf(cmd+next_at, cmd_size-next_at, | |
2f978138 JH |
131 | diff_arg, input_name_sq[0], input_name_sq[1]); |
132 | ||
915838c3 | 133 | printf("diff --git a/%s b/%s\n", name_a, name_b); |
c983370e | 134 | if (!path1[0][0]) |
b58f23b3 | 135 | printf("new file mode %s\n", temp[1].mode); |
c983370e | 136 | else if (!path1[1][0]) |
b58f23b3 | 137 | printf("deleted file mode %s\n", temp[0].mode); |
273b9834 | 138 | else { |
b58f23b3 JH |
139 | if (strcmp(temp[0].mode, temp[1].mode)) { |
140 | printf("old mode %s\n", temp[0].mode); | |
141 | printf("new mode %s\n", temp[1].mode); | |
142 | } | |
427dcb4b JH |
143 | if (xfrm_msg && xfrm_msg[0]) |
144 | fputs(xfrm_msg, stdout); | |
145 | ||
273b9834 JH |
146 | if (strncmp(temp[0].mode, temp[1].mode, 3)) |
147 | /* we do not run diff between different kind | |
148 | * of objects. | |
149 | */ | |
b28858bf JH |
150 | exit(0); |
151 | } | |
c983370e | 152 | fflush(NULL); |
be3cfa85 | 153 | execlp("/bin/sh","sh", "-c", cmd, NULL); |
86436c28 JH |
154 | } |
155 | ||
427dcb4b JH |
156 | struct diff_filespec *alloc_filespec(const char *path) |
157 | { | |
158 | int namelen = strlen(path); | |
159 | struct diff_filespec *spec = xmalloc(sizeof(*spec) + namelen + 1); | |
160 | spec->path = (char *)(spec + 1); | |
161 | strcpy(spec->path, path); | |
81e50eab | 162 | spec->should_free = spec->should_munmap = 0; |
427dcb4b JH |
163 | spec->xfrm_flags = 0; |
164 | spec->size = 0; | |
09d74b3b | 165 | spec->data = NULL; |
81e50eab JH |
166 | spec->mode = 0; |
167 | memset(spec->sha1, 0, 20); | |
427dcb4b JH |
168 | return spec; |
169 | } | |
170 | ||
171 | void fill_filespec(struct diff_filespec *spec, const unsigned char *sha1, | |
172 | unsigned short mode) | |
173 | { | |
4130b995 JH |
174 | if (mode) { |
175 | spec->mode = DIFF_FILE_CANON_MODE(mode); | |
81e50eab JH |
176 | memcpy(spec->sha1, sha1, 20); |
177 | spec->sha1_valid = !!memcmp(sha1, null_sha1, 20); | |
178 | } | |
427dcb4b JH |
179 | } |
180 | ||
b46f0b6d JH |
181 | /* |
182 | * Given a name and sha1 pair, if the dircache tells us the file in | |
183 | * the work tree has that object contents, return true, so that | |
184 | * prepare_temp_file() does not have to inflate and extract. | |
185 | */ | |
186 | static int work_tree_matches(const char *name, const unsigned char *sha1) | |
187 | { | |
188 | struct cache_entry *ce; | |
189 | struct stat st; | |
190 | int pos, len; | |
5c97558c | 191 | |
b46f0b6d JH |
192 | /* We do not read the cache ourselves here, because the |
193 | * benchmark with my previous version that always reads cache | |
194 | * shows that it makes things worse for diff-tree comparing | |
195 | * two linux-2.6 kernel trees in an already checked out work | |
915838c3 | 196 | * tree. This is because most diff-tree comparisons deal with |
b46f0b6d JH |
197 | * only a small number of files, while reading the cache is |
198 | * expensive for a large project, and its cost outweighs the | |
199 | * savings we get by not inflating the object to a temporary | |
200 | * file. Practically, this code only helps when we are used | |
201 | * by diff-cache --cached, which does read the cache before | |
202 | * calling us. | |
57fe64a4 | 203 | */ |
b46f0b6d JH |
204 | if (!active_cache) |
205 | return 0; | |
206 | ||
207 | len = strlen(name); | |
208 | pos = cache_name_pos(name, len); | |
209 | if (pos < 0) | |
210 | return 0; | |
211 | ce = active_cache[pos]; | |
b28858bf | 212 | if ((lstat(name, &st) < 0) || |
427dcb4b | 213 | !S_ISREG(st.st_mode) || /* careful! */ |
5d728c84 | 214 | ce_match_stat(ce, &st) || |
b46f0b6d JH |
215 | memcmp(sha1, ce->sha1, 20)) |
216 | return 0; | |
427dcb4b JH |
217 | /* we return 1 only when we can stat, it is a regular file, |
218 | * stat information matches, and sha1 recorded in the cache | |
219 | * matches. I.e. we know the file in the work tree really is | |
220 | * the same as the <name, sha1> pair. | |
221 | */ | |
b46f0b6d JH |
222 | return 1; |
223 | } | |
224 | ||
427dcb4b JH |
225 | /* |
226 | * While doing rename detection and pickaxe operation, we may need to | |
227 | * grab the data for the blob (or file) for our own in-core comparison. | |
228 | * diff_filespec has data and size fields for this purpose. | |
229 | */ | |
230 | int diff_populate_filespec(struct diff_filespec *s) | |
231 | { | |
232 | int err = 0; | |
81e50eab | 233 | if (!DIFF_FILE_VALID(s)) |
427dcb4b JH |
234 | die("internal error: asking to populate invalid file."); |
235 | if (S_ISDIR(s->mode)) | |
236 | return -1; | |
237 | ||
238 | if (s->data) | |
239 | return err; | |
240 | if (!s->sha1_valid || | |
241 | work_tree_matches(s->path, s->sha1)) { | |
242 | struct stat st; | |
243 | int fd; | |
244 | if (lstat(s->path, &st) < 0) { | |
245 | if (errno == ENOENT) { | |
246 | err_empty: | |
247 | err = -1; | |
248 | empty: | |
249 | s->data = ""; | |
250 | s->size = 0; | |
251 | return err; | |
252 | } | |
253 | } | |
254 | s->size = st.st_size; | |
255 | if (!s->size) | |
256 | goto empty; | |
257 | if (S_ISLNK(st.st_mode)) { | |
258 | int ret; | |
259 | s->data = xmalloc(s->size); | |
260 | s->should_free = 1; | |
261 | ret = readlink(s->path, s->data, s->size); | |
262 | if (ret < 0) { | |
263 | free(s->data); | |
264 | goto err_empty; | |
265 | } | |
266 | return 0; | |
267 | } | |
268 | fd = open(s->path, O_RDONLY); | |
269 | if (fd < 0) | |
270 | goto err_empty; | |
271 | s->data = mmap(NULL, s->size, PROT_READ, MAP_PRIVATE, fd, 0); | |
272 | s->should_munmap = 1; | |
273 | close(fd); | |
274 | } | |
275 | else { | |
276 | char type[20]; | |
277 | s->data = read_sha1_file(s->sha1, type, &s->size); | |
278 | s->should_free = 1; | |
279 | } | |
280 | return 0; | |
281 | } | |
282 | ||
283 | void diff_free_filespec_data(struct diff_filespec *s) | |
284 | { | |
285 | if (s->should_free) | |
286 | free(s->data); | |
287 | else if (s->should_munmap) | |
288 | munmap(s->data, s->size); | |
289 | s->should_free = s->should_munmap = 0; | |
09d74b3b | 290 | s->data = NULL; |
427dcb4b JH |
291 | } |
292 | ||
b28858bf JH |
293 | static void prep_temp_blob(struct diff_tempfile *temp, |
294 | void *blob, | |
295 | unsigned long size, | |
296 | unsigned char *sha1, | |
297 | int mode) | |
298 | { | |
299 | int fd; | |
300 | ||
301 | strcpy(temp->tmp_path, ".diff_XXXXXX"); | |
302 | fd = mkstemp(temp->tmp_path); | |
303 | if (fd < 0) | |
304 | die("unable to create temp-file"); | |
305 | if (write(fd, blob, size) != size) | |
306 | die("unable to write temp-file"); | |
307 | close(fd); | |
308 | temp->name = temp->tmp_path; | |
309 | strcpy(temp->hex, sha1_to_hex(sha1)); | |
310 | temp->hex[40] = 0; | |
311 | sprintf(temp->mode, "%06o", mode); | |
312 | } | |
313 | ||
be3cfa85 JH |
314 | static void prepare_temp_file(const char *name, |
315 | struct diff_tempfile *temp, | |
427dcb4b | 316 | struct diff_filespec *one) |
86436c28 | 317 | { |
81e50eab | 318 | if (!DIFF_FILE_VALID(one)) { |
be3cfa85 | 319 | not_a_valid_file: |
532149d7 JH |
320 | /* A '-' entry produces this for file-2, and |
321 | * a '+' entry produces this for file-1. | |
322 | */ | |
be3cfa85 JH |
323 | temp->name = "/dev/null"; |
324 | strcpy(temp->hex, "."); | |
325 | strcpy(temp->mode, "."); | |
86436c28 JH |
326 | return; |
327 | } | |
be3cfa85 | 328 | |
5c97558c | 329 | if (!one->sha1_valid || |
427dcb4b | 330 | work_tree_matches(name, one->sha1)) { |
be3cfa85 | 331 | struct stat st; |
427dcb4b | 332 | if (lstat(name, &st) < 0) { |
be3cfa85 JH |
333 | if (errno == ENOENT) |
334 | goto not_a_valid_file; | |
427dcb4b | 335 | die("stat(%s): %s", name, strerror(errno)); |
be3cfa85 | 336 | } |
b28858bf JH |
337 | if (S_ISLNK(st.st_mode)) { |
338 | int ret; | |
339 | char *buf, buf_[1024]; | |
340 | buf = ((sizeof(buf_) < st.st_size) ? | |
341 | xmalloc(st.st_size) : buf_); | |
342 | ret = readlink(name, buf, st.st_size); | |
343 | if (ret < 0) | |
344 | die("readlink(%s)", name); | |
345 | prep_temp_blob(temp, buf, st.st_size, | |
346 | (one->sha1_valid ? | |
427dcb4b | 347 | one->sha1 : null_sha1), |
b28858bf JH |
348 | (one->sha1_valid ? |
349 | one->mode : S_IFLNK)); | |
350 | } | |
351 | else { | |
427dcb4b JH |
352 | /* we can borrow from the file in the work tree */ |
353 | temp->name = name; | |
b28858bf JH |
354 | if (!one->sha1_valid) |
355 | strcpy(temp->hex, sha1_to_hex(null_sha1)); | |
356 | else | |
427dcb4b | 357 | strcpy(temp->hex, sha1_to_hex(one->sha1)); |
b28858bf JH |
358 | sprintf(temp->mode, "%06o", |
359 | S_IFREG |ce_permissions(st.st_mode)); | |
360 | } | |
361 | return; | |
be3cfa85 JH |
362 | } |
363 | else { | |
427dcb4b JH |
364 | if (diff_populate_filespec(one)) |
365 | die("cannot read data blob for %s", one->path); | |
366 | prep_temp_blob(temp, one->data, one->size, | |
367 | one->sha1, one->mode); | |
be3cfa85 JH |
368 | } |
369 | } | |
370 | ||
371 | static void remove_tempfile(void) | |
372 | { | |
373 | int i; | |
374 | ||
375 | for (i = 0; i < 2; i++) | |
376 | if (diff_temp[i].name == diff_temp[i].tmp_path) { | |
377 | unlink(diff_temp[i].name); | |
378 | diff_temp[i].name = NULL; | |
379 | } | |
380 | } | |
381 | ||
532149d7 JH |
382 | static void remove_tempfile_on_signal(int signo) |
383 | { | |
384 | remove_tempfile(); | |
385 | } | |
386 | ||
be3cfa85 JH |
387 | /* An external diff command takes: |
388 | * | |
389 | * diff-cmd name infile1 infile1-sha1 infile1-mode \ | |
5c97558c | 390 | * infile2 infile2-sha1 infile2-mode [ rename-to ] |
be3cfa85 JH |
391 | * |
392 | */ | |
4130b995 JH |
393 | static void run_external_diff(const char *pgm, |
394 | const char *name, | |
5c97558c | 395 | const char *other, |
427dcb4b JH |
396 | struct diff_filespec *one, |
397 | struct diff_filespec *two, | |
398 | const char *xfrm_msg) | |
be3cfa85 JH |
399 | { |
400 | struct diff_tempfile *temp = diff_temp; | |
532149d7 JH |
401 | pid_t pid; |
402 | int status; | |
be3cfa85 JH |
403 | static int atexit_asked = 0; |
404 | ||
77eb2720 JH |
405 | if (one && two) { |
406 | prepare_temp_file(name, &temp[0], one); | |
915838c3 | 407 | prepare_temp_file(other ? : name, &temp[1], two); |
77eb2720 JH |
408 | if (! atexit_asked && |
409 | (temp[0].name == temp[0].tmp_path || | |
410 | temp[1].name == temp[1].tmp_path)) { | |
411 | atexit_asked = 1; | |
412 | atexit(remove_tempfile); | |
413 | } | |
532149d7 | 414 | signal(SIGINT, remove_tempfile_on_signal); |
be3cfa85 JH |
415 | } |
416 | ||
417 | fflush(NULL); | |
418 | pid = fork(); | |
419 | if (pid < 0) | |
420 | die("unable to fork"); | |
421 | if (!pid) { | |
5c97558c JH |
422 | if (pgm) { |
423 | if (one && two) { | |
427dcb4b | 424 | const char *exec_arg[10]; |
5c97558c JH |
425 | const char **arg = &exec_arg[0]; |
426 | *arg++ = pgm; | |
427 | *arg++ = name; | |
428 | *arg++ = temp[0].name; | |
429 | *arg++ = temp[0].hex; | |
430 | *arg++ = temp[0].mode; | |
431 | *arg++ = temp[1].name; | |
432 | *arg++ = temp[1].hex; | |
433 | *arg++ = temp[1].mode; | |
427dcb4b | 434 | if (other) { |
5c97558c | 435 | *arg++ = other; |
427dcb4b JH |
436 | *arg++ = xfrm_msg; |
437 | } | |
09d74b3b | 438 | *arg = NULL; |
5c97558c JH |
439 | execvp(pgm, (char *const*) exec_arg); |
440 | } | |
77eb2720 JH |
441 | else |
442 | execlp(pgm, pgm, name, NULL); | |
443 | } | |
be3cfa85 JH |
444 | /* |
445 | * otherwise we use the built-in one. | |
446 | */ | |
77eb2720 | 447 | if (one && two) |
427dcb4b | 448 | builtin_diff(name, other ? : name, temp, xfrm_msg); |
77eb2720 JH |
449 | else |
450 | printf("* Unmerged path %s\n", name); | |
be3cfa85 JH |
451 | exit(0); |
452 | } | |
6fa28064 JH |
453 | if (waitpid(pid, &status, 0) < 0 || |
454 | !WIFEXITED(status) || WEXITSTATUS(status)) { | |
455 | /* Earlier we did not check the exit status because | |
532149d7 | 456 | * diff exits non-zero if files are different, and |
6fa28064 JH |
457 | * we are not interested in knowing that. It was a |
458 | * mistake which made it harder to quit a diff-* | |
459 | * session that uses the git-apply-patch-script as | |
460 | * the GIT_EXTERNAL_DIFF. A custom GIT_EXTERNAL_DIFF | |
461 | * should also exit non-zero only when it wants to | |
462 | * abort the entire diff-* session. | |
532149d7 JH |
463 | */ |
464 | remove_tempfile(); | |
6fa28064 JH |
465 | fprintf(stderr, "external diff died, stopping at %s.\n", name); |
466 | exit(1); | |
532149d7 | 467 | } |
be3cfa85 JH |
468 | remove_tempfile(); |
469 | } | |
470 | ||
4130b995 JH |
471 | static void run_diff(const char *name, |
472 | const char *other, | |
473 | struct diff_filespec *one, | |
474 | struct diff_filespec *two, | |
475 | const char *xfrm_msg) | |
476 | { | |
477 | const char *pgm = external_diff(); | |
478 | if (!pgm && | |
479 | DIFF_FILE_VALID(one) && DIFF_FILE_VALID(two) && | |
480 | (S_IFMT & one->mode) != (S_IFMT & two->mode)) { | |
481 | /* a filepair that changes between file and symlink | |
482 | * needs to be split into deletion and creation. | |
483 | */ | |
484 | struct diff_filespec *null = alloc_filespec(two->path); | |
485 | run_external_diff(NULL, name, other, one, null, xfrm_msg); | |
486 | free(null); | |
487 | null = alloc_filespec(one->path); | |
488 | run_external_diff(NULL, name, other, null, two, xfrm_msg); | |
489 | free(null); | |
490 | } | |
491 | else | |
492 | run_external_diff(pgm, name, other, one, two, xfrm_msg); | |
493 | } | |
494 | ||
6b14d7fa | 495 | void diff_setup(int reverse_diff_) |
5c97558c | 496 | { |
6b14d7fa | 497 | reverse_diff = reverse_diff_; |
5c97558c JH |
498 | } |
499 | ||
6b14d7fa JH |
500 | struct diff_queue_struct diff_queued_diff; |
501 | ||
502 | void diff_q(struct diff_queue_struct *queue, struct diff_filepair *dp) | |
5c97558c | 503 | { |
6b14d7fa JH |
504 | if (queue->alloc <= queue->nr) { |
505 | queue->alloc = alloc_nr(queue->alloc); | |
506 | queue->queue = xrealloc(queue->queue, | |
507 | sizeof(dp) * queue->alloc); | |
81e50eab | 508 | } |
6b14d7fa | 509 | queue->queue[queue->nr++] = dp; |
5c97558c JH |
510 | } |
511 | ||
52e95789 | 512 | struct diff_filepair *diff_queue(struct diff_queue_struct *queue, |
6b14d7fa JH |
513 | struct diff_filespec *one, |
514 | struct diff_filespec *two) | |
5c97558c | 515 | { |
52e95789 | 516 | struct diff_filepair *dp = xmalloc(sizeof(*dp)); |
427dcb4b JH |
517 | dp->one = one; |
518 | dp->two = two; | |
f7c1512a | 519 | dp->score = 0; |
6b14d7fa | 520 | diff_q(queue, dp); |
427dcb4b | 521 | return dp; |
5c97558c JH |
522 | } |
523 | ||
226406f6 JH |
524 | void diff_free_filepair(struct diff_filepair *p) |
525 | { | |
526 | diff_free_filespec_data(p->one); | |
527 | diff_free_filespec_data(p->two); | |
528 | free(p); | |
529 | } | |
530 | ||
bceafe75 JH |
531 | static void diff_flush_raw(struct diff_filepair *p, |
532 | int line_termination, | |
533 | int inter_name_termination) | |
5c97558c | 534 | { |
b6d8f309 JH |
535 | int two_paths; |
536 | char status[10]; | |
25d5ea41 JH |
537 | |
538 | if (line_termination) { | |
539 | const char *err = "path %s cannot be expressed without -z"; | |
540 | if (strchr(p->one->path, line_termination) || | |
541 | strchr(p->one->path, inter_name_termination)) | |
542 | die(err, p->one->path); | |
543 | if (strchr(p->two->path, line_termination) || | |
544 | strchr(p->two->path, inter_name_termination)) | |
545 | die(err, p->two->path); | |
546 | } | |
547 | ||
b6d8f309 JH |
548 | switch (p->status) { |
549 | case 'C': case 'R': | |
550 | two_paths = 1; | |
9fdade06 JH |
551 | sprintf(status, "%c%03d", p->status, |
552 | (int)(0.5 + p->score * 100.0/MAX_SCORE)); | |
b6d8f309 JH |
553 | break; |
554 | default: | |
555 | two_paths = 0; | |
556 | status[0] = p->status; | |
557 | status[1] = 0; | |
558 | break; | |
6b14d7fa | 559 | } |
81e50eab JH |
560 | printf(":%06o %06o %s ", |
561 | p->one->mode, p->two->mode, sha1_to_hex(p->one->sha1)); | |
b6d8f309 JH |
562 | printf("%s %s%c%s", |
563 | sha1_to_hex(p->two->sha1), | |
564 | status, | |
565 | inter_name_termination, | |
566 | p->one->path); | |
567 | if (two_paths) | |
568 | printf("%c%s", inter_name_termination, p->two->path); | |
569 | putchar(line_termination); | |
5c97558c JH |
570 | } |
571 | ||
f7c1512a | 572 | int diff_unmodified_pair(struct diff_filepair *p) |
5c97558c | 573 | { |
427dcb4b JH |
574 | /* This function is written stricter than necessary to support |
575 | * the currently implemented transformers, but the idea is to | |
52e95789 | 576 | * let transformers to produce diff_filepairs any way they want, |
427dcb4b | 577 | * and filter and clean them up here before producing the output. |
5c97558c | 578 | */ |
6b14d7fa JH |
579 | struct diff_filespec *one, *two; |
580 | ||
581 | if (DIFF_PAIR_UNMERGED(p)) | |
582 | return 0; /* unmerged is interesting */ | |
5c97558c | 583 | |
6b14d7fa JH |
584 | one = p->one; |
585 | two = p->two; | |
5c97558c | 586 | |
4130b995 JH |
587 | /* deletion, addition, mode or type change |
588 | * and rename are all interesting. | |
589 | */ | |
81e50eab | 590 | if (DIFF_FILE_VALID(one) != DIFF_FILE_VALID(two) || |
4130b995 | 591 | DIFF_PAIR_MODE_CHANGED(p) || |
427dcb4b JH |
592 | strcmp(one->path, two->path)) |
593 | return 0; | |
57fe64a4 | 594 | |
427dcb4b JH |
595 | /* both are valid and point at the same path. that is, we are |
596 | * dealing with a change. | |
57fe64a4 | 597 | */ |
427dcb4b JH |
598 | if (one->sha1_valid && two->sha1_valid && |
599 | !memcmp(one->sha1, two->sha1, sizeof(one->sha1))) | |
600 | return 1; /* no change */ | |
601 | if (!one->sha1_valid && !two->sha1_valid) | |
602 | return 1; /* both look at the same file on the filesystem. */ | |
603 | return 0; | |
57fe64a4 JH |
604 | } |
605 | ||
b6d8f309 | 606 | static void diff_flush_patch(struct diff_filepair *p) |
f7c1512a JH |
607 | { |
608 | const char *name, *other; | |
b6d8f309 | 609 | char msg_[PATH_MAX*2+200], *msg; |
f7c1512a | 610 | |
f7c1512a JH |
611 | if (diff_unmodified_pair(p)) |
612 | return; | |
613 | ||
614 | name = p->one->path; | |
615 | other = (strcmp(name, p->two->path) ? p->two->path : NULL); | |
616 | if ((DIFF_FILE_VALID(p->one) && S_ISDIR(p->one->mode)) || | |
617 | (DIFF_FILE_VALID(p->two) && S_ISDIR(p->two->mode))) | |
618 | return; /* no tree diffs in patch format */ | |
619 | ||
b6d8f309 JH |
620 | switch (p->status) { |
621 | case 'C': | |
622 | sprintf(msg_, | |
623 | "similarity index %d%%\n" | |
624 | "copy from %s\n" | |
625 | "copy to %s\n", | |
bceafe75 | 626 | (int)(0.5 + p->score * 100.0/MAX_SCORE), |
b6d8f309 JH |
627 | p->one->path, p->two->path); |
628 | msg = msg_; | |
629 | break; | |
630 | case 'R': | |
631 | sprintf(msg_, | |
632 | "similarity index %d%%\n" | |
633 | "rename old %s\n" | |
634 | "rename new %s\n", | |
bceafe75 | 635 | (int)(0.5 + p->score * 100.0/MAX_SCORE), |
b6d8f309 JH |
636 | p->one->path, p->two->path); |
637 | msg = msg_; | |
638 | break; | |
639 | default: | |
640 | msg = NULL; | |
641 | } | |
642 | ||
f7c1512a | 643 | if (DIFF_PAIR_UNMERGED(p)) |
4130b995 | 644 | run_diff(name, NULL, NULL, NULL, NULL); |
f7c1512a | 645 | else |
4130b995 | 646 | run_diff(name, other, p->one, p->two, msg); |
f7c1512a JH |
647 | } |
648 | ||
649 | int diff_needs_to_stay(struct diff_queue_struct *q, int i, | |
650 | struct diff_filespec *it) | |
651 | { | |
652 | /* If it will be used in later entry (either stay or used | |
653 | * as the source of rename/copy), we need to copy, not rename. | |
654 | */ | |
655 | while (i < q->nr) { | |
656 | struct diff_filepair *p = q->queue[i++]; | |
657 | if (!DIFF_FILE_VALID(p->two)) | |
658 | continue; /* removed is fine */ | |
659 | if (strcmp(p->one->path, it->path)) | |
660 | continue; /* not relevant */ | |
661 | ||
662 | /* p has its src set to *it and it is not a delete; | |
663 | * it will be used for in-place change, rename/copy, | |
664 | * or just stays there. We cannot rename it out. | |
665 | */ | |
666 | return 1; | |
667 | } | |
668 | return 0; | |
669 | } | |
670 | ||
bceafe75 | 671 | int diff_queue_is_empty(void) |
f7c1512a | 672 | { |
bceafe75 | 673 | struct diff_queue_struct *q = &diff_queued_diff; |
25d5ea41 JH |
674 | int i; |
675 | for (i = 0; i < q->nr; i++) | |
676 | if (!diff_unmodified_pair(q->queue[i])) | |
677 | return 0; | |
678 | return 1; | |
f7c1512a JH |
679 | } |
680 | ||
25d5ea41 JH |
681 | #if DIFF_DEBUG |
682 | void diff_debug_filespec(struct diff_filespec *s, int x, const char *one) | |
683 | { | |
684 | fprintf(stderr, "queue[%d] %s (%s) %s %06o %s\n", | |
685 | x, one ? : "", | |
686 | s->path, | |
687 | DIFF_FILE_VALID(s) ? "valid" : "invalid", | |
688 | s->mode, | |
689 | s->sha1_valid ? sha1_to_hex(s->sha1) : ""); | |
690 | fprintf(stderr, "queue[%d] %s size %lu flags %d\n", | |
691 | x, one ? : "", | |
692 | s->size, s->xfrm_flags); | |
693 | } | |
694 | ||
695 | void diff_debug_filepair(const struct diff_filepair *p, int i) | |
696 | { | |
697 | diff_debug_filespec(p->one, i, "one"); | |
698 | diff_debug_filespec(p->two, i, "two"); | |
699 | fprintf(stderr, "score %d, status %c\n", | |
700 | p->score, p->status ? : '?'); | |
701 | } | |
702 | ||
703 | void diff_debug_queue(const char *msg, struct diff_queue_struct *q) | |
6b14d7fa | 704 | { |
6b14d7fa | 705 | int i; |
25d5ea41 JH |
706 | if (msg) |
707 | fprintf(stderr, "%s\n", msg); | |
708 | fprintf(stderr, "q->nr = %d\n", q->nr); | |
6b14d7fa JH |
709 | for (i = 0; i < q->nr; i++) { |
710 | struct diff_filepair *p = q->queue[i]; | |
25d5ea41 JH |
711 | diff_debug_filepair(p, i); |
712 | } | |
713 | } | |
714 | #endif | |
715 | ||
716 | static void diff_resolve_rename_copy(void) | |
717 | { | |
718 | int i, j; | |
719 | struct diff_filepair *p, *pp; | |
720 | struct diff_queue_struct *q = &diff_queued_diff; | |
721 | ||
722 | /* This should not depend on the ordering of things. */ | |
723 | ||
724 | diff_debug_queue("resolve-rename-copy", q); | |
725 | ||
726 | for (i = 0; i < q->nr; i++) { | |
727 | p = q->queue[i]; | |
96716a19 | 728 | p->status = 0; /* undecided */ |
bceafe75 JH |
729 | if (DIFF_PAIR_UNMERGED(p)) |
730 | p->status = 'U'; | |
731 | else if (!DIFF_FILE_VALID((p)->one)) | |
732 | p->status = 'N'; | |
733 | else if (!DIFF_FILE_VALID((p)->two)) { | |
25d5ea41 | 734 | /* Deletion record should be omitted if there |
96716a19 JH |
735 | * are rename/copy entries using this one as |
736 | * the source. Then we can say one of them | |
737 | * is a rename and the rest are copies. | |
bceafe75 | 738 | */ |
96716a19 | 739 | p->status = 'D'; |
25d5ea41 JH |
740 | for (j = 0; j < q->nr; j++) { |
741 | pp = q->queue[j]; | |
742 | if (!strcmp(pp->one->path, p->one->path) && | |
96716a19 JH |
743 | strcmp(pp->one->path, pp->two->path)) { |
744 | p->status = 'X'; | |
bceafe75 | 745 | break; |
96716a19 | 746 | } |
bceafe75 | 747 | } |
bceafe75 | 748 | } |
96716a19 JH |
749 | else if (DIFF_PAIR_TYPE_CHANGED(p)) |
750 | p->status = 'T'; | |
751 | ||
752 | /* from this point on, we are dealing with a pair | |
753 | * whose both sides are valid and of the same type, i.e. | |
754 | * either in-place edit or rename/copy edit. | |
755 | */ | |
bceafe75 | 756 | else if (strcmp(p->one->path, p->two->path)) { |
25d5ea41 JH |
757 | /* See if there is somebody else anywhere that |
758 | * will keep the path (either modified or | |
759 | * unmodified). If so, we have to be a copy, | |
760 | * not a rename. In addition, if there is | |
761 | * some other rename or copy that comes later | |
762 | * than us that uses the same source, we | |
96716a19 | 763 | * have to be a copy, not a rename. |
25d5ea41 JH |
764 | */ |
765 | for (j = 0; j < q->nr; j++) { | |
766 | pp = q->queue[j]; | |
767 | if (strcmp(pp->one->path, p->one->path)) | |
768 | continue; | |
769 | if (!strcmp(pp->one->path, pp->two->path)) { | |
770 | if (DIFF_FILE_VALID(pp->two)) { | |
771 | /* non-delete */ | |
772 | p->status = 'C'; | |
773 | break; | |
774 | } | |
775 | continue; | |
776 | } | |
777 | /* pp is a rename/copy ... */ | |
778 | if (i < j) { | |
779 | /* ... and comes later than us */ | |
780 | p->status = 'C'; | |
781 | break; | |
782 | } | |
783 | } | |
784 | if (!p->status) | |
bceafe75 JH |
785 | p->status = 'R'; |
786 | } | |
9fdade06 JH |
787 | else if (memcmp(p->one->sha1, p->two->sha1, 20) || |
788 | p->one->mode != p->two->mode) | |
bceafe75 | 789 | p->status = 'M'; |
96716a19 JH |
790 | else |
791 | /* this is a "no-change" entry */ | |
792 | p->status = 'X'; | |
6b14d7fa | 793 | } |
25d5ea41 | 794 | diff_debug_queue("resolve-rename-copy done", q); |
38c6f780 JH |
795 | } |
796 | ||
b6d8f309 | 797 | void diff_flush(int diff_output_style, int resolve_rename_copy) |
38c6f780 JH |
798 | { |
799 | struct diff_queue_struct *q = &diff_queued_diff; | |
800 | int i; | |
bceafe75 JH |
801 | int line_termination = '\n'; |
802 | int inter_name_termination = '\t'; | |
38c6f780 | 803 | |
bceafe75 | 804 | if (diff_output_style == DIFF_FORMAT_MACHINE) |
6b14d7fa | 805 | line_termination = inter_name_termination = 0; |
bceafe75 JH |
806 | if (resolve_rename_copy) |
807 | diff_resolve_rename_copy(); | |
808 | ||
f7c1512a | 809 | for (i = 0; i < q->nr; i++) { |
f7c1512a | 810 | struct diff_filepair *p = q->queue[i]; |
4130b995 JH |
811 | if ((diff_output_style == DIFF_FORMAT_NO_OUTPUT) || |
812 | (p->status == 'X')) | |
96716a19 | 813 | continue; |
bceafe75 | 814 | if (p->status == 0) |
96716a19 | 815 | die("internal error in diff-resolve-rename-copy"); |
bceafe75 JH |
816 | switch (diff_output_style) { |
817 | case DIFF_FORMAT_PATCH: | |
b6d8f309 | 818 | diff_flush_patch(p); |
bceafe75 JH |
819 | break; |
820 | case DIFF_FORMAT_HUMAN: | |
821 | case DIFF_FORMAT_MACHINE: | |
822 | diff_flush_raw(p, line_termination, | |
823 | inter_name_termination); | |
824 | break; | |
825 | } | |
f7c1512a | 826 | } |
226406f6 JH |
827 | for (i = 0; i < q->nr; i++) |
828 | diff_free_filepair(q->queue[i]); | |
427dcb4b JH |
829 | free(q->queue); |
830 | q->queue = NULL; | |
831 | q->nr = q->alloc = 0; | |
5c97558c JH |
832 | } |
833 | ||
77eb2720 JH |
834 | void diff_addremove(int addremove, unsigned mode, |
835 | const unsigned char *sha1, | |
836 | const char *base, const char *path) | |
be3cfa85 | 837 | { |
77eb2720 | 838 | char concatpath[PATH_MAX]; |
427dcb4b JH |
839 | struct diff_filespec *one, *two; |
840 | ||
841 | /* This may look odd, but it is a preparation for | |
842 | * feeding "there are unchanged files which should | |
843 | * not produce diffs, but when you are doing copy | |
844 | * detection you would need them, so here they are" | |
845 | * entries to the diff-core. They will be prefixed | |
846 | * with something like '=' or '*' (I haven't decided | |
847 | * which but should not make any difference). | |
f7c1512a | 848 | * Feeding the same new and old to diff_change() |
bceafe75 JH |
849 | * also has the same effect. |
850 | * Before the final output happens, they are pruned after | |
851 | * merged into rename/copy pairs as appropriate. | |
427dcb4b | 852 | */ |
7ca45252 | 853 | if (reverse_diff) |
427dcb4b JH |
854 | addremove = (addremove == '+' ? '-' : |
855 | addremove == '-' ? '+' : addremove); | |
7ca45252 | 856 | |
427dcb4b JH |
857 | if (!path) path = ""; |
858 | sprintf(concatpath, "%s%s", base, path); | |
859 | one = alloc_filespec(concatpath); | |
860 | two = alloc_filespec(concatpath); | |
be3cfa85 | 861 | |
427dcb4b JH |
862 | if (addremove != '+') |
863 | fill_filespec(one, sha1, mode); | |
864 | if (addremove != '-') | |
865 | fill_filespec(two, sha1, mode); | |
5c97558c | 866 | |
38c6f780 | 867 | diff_queue(&diff_queued_diff, one, two); |
be3cfa85 JH |
868 | } |
869 | ||
b6d8f309 JH |
870 | void diff_helper_input(unsigned old_mode, |
871 | unsigned new_mode, | |
872 | const unsigned char *old_sha1, | |
873 | const unsigned char *new_sha1, | |
874 | const char *old_path, | |
875 | int status, | |
876 | int score, | |
877 | const char *new_path) | |
81e50eab JH |
878 | { |
879 | struct diff_filespec *one, *two; | |
b6d8f309 | 880 | struct diff_filepair *dp; |
81e50eab | 881 | |
81e50eab JH |
882 | one = alloc_filespec(old_path); |
883 | two = alloc_filespec(new_path); | |
884 | if (old_mode) | |
885 | fill_filespec(one, old_sha1, old_mode); | |
886 | if (new_mode) | |
887 | fill_filespec(two, new_sha1, new_mode); | |
b6d8f309 | 888 | dp = diff_queue(&diff_queued_diff, one, two); |
903d475a | 889 | dp->score = score * MAX_SCORE / 100; |
b6d8f309 | 890 | dp->status = status; |
81e50eab JH |
891 | } |
892 | ||
77eb2720 JH |
893 | void diff_change(unsigned old_mode, unsigned new_mode, |
894 | const unsigned char *old_sha1, | |
895 | const unsigned char *new_sha1, | |
81e50eab JH |
896 | const char *base, const char *path) |
897 | { | |
77eb2720 | 898 | char concatpath[PATH_MAX]; |
427dcb4b | 899 | struct diff_filespec *one, *two; |
77eb2720 | 900 | |
7ca45252 JH |
901 | if (reverse_diff) { |
902 | unsigned tmp; | |
903 | const unsigned char *tmp_c; | |
904 | tmp = old_mode; old_mode = new_mode; new_mode = tmp; | |
905 | tmp_c = old_sha1; old_sha1 = new_sha1; new_sha1 = tmp_c; | |
906 | } | |
427dcb4b JH |
907 | if (!path) path = ""; |
908 | sprintf(concatpath, "%s%s", base, path); | |
909 | one = alloc_filespec(concatpath); | |
910 | two = alloc_filespec(concatpath); | |
911 | fill_filespec(one, old_sha1, old_mode); | |
912 | fill_filespec(two, new_sha1, new_mode); | |
913 | ||
38c6f780 | 914 | diff_queue(&diff_queued_diff, one, two); |
77eb2720 | 915 | } |
be3cfa85 | 916 | |
77eb2720 JH |
917 | void diff_unmerge(const char *path) |
918 | { | |
6b14d7fa JH |
919 | struct diff_filespec *one, *two; |
920 | one = alloc_filespec(path); | |
921 | two = alloc_filespec(path); | |
922 | diff_queue(&diff_queued_diff, one, two); | |
86436c28 | 923 | } |