]>
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 | 7 | #include "cache.h" |
6fb737be | 8 | #include "quote.h" |
86436c28 | 9 | #include "diff.h" |
427dcb4b | 10 | #include "diffcore.h" |
3443546f | 11 | #include "xdiff/xdiff.h" |
86436c28 | 12 | |
f0c6b2a2 | 13 | static int use_size_cache; |
86436c28 | 14 | |
3299c6f6 JH |
15 | int diff_rename_limit_default = -1; |
16 | ||
9ce392f4 JH |
17 | int git_diff_config(const char *var, const char *value) |
18 | { | |
19 | if (!strcmp(var, "diff.renamelimit")) { | |
20 | diff_rename_limit_default = git_config_int(var, value); | |
21 | return 0; | |
22 | } | |
23 | ||
24 | return git_default_config(var, value); | |
25 | } | |
26 | ||
cf9dfc66 JH |
27 | static char *quote_one(const char *str) |
28 | { | |
29 | int needlen; | |
30 | char *xp; | |
31 | ||
32 | if (!str) | |
33 | return NULL; | |
34 | needlen = quote_c_style(str, NULL, NULL, 0); | |
35 | if (!needlen) | |
36 | return strdup(str); | |
37 | xp = xmalloc(needlen + 1); | |
38 | quote_c_style(str, xp, NULL, 0); | |
39 | return xp; | |
40 | } | |
41 | ||
42 | static char *quote_two(const char *one, const char *two) | |
43 | { | |
44 | int need_one = quote_c_style(one, NULL, NULL, 1); | |
45 | int need_two = quote_c_style(two, NULL, NULL, 1); | |
46 | char *xp; | |
47 | ||
48 | if (need_one + need_two) { | |
49 | if (!need_one) need_one = strlen(one); | |
50 | if (!need_two) need_one = strlen(two); | |
51 | ||
52 | xp = xmalloc(need_one + need_two + 3); | |
53 | xp[0] = '"'; | |
54 | quote_c_style(one, xp + 1, NULL, 1); | |
55 | quote_c_style(two, xp + need_one + 1, NULL, 1); | |
56 | strcpy(xp + need_one + need_two + 1, "\""); | |
57 | return xp; | |
58 | } | |
59 | need_one = strlen(one); | |
60 | need_two = strlen(two); | |
61 | xp = xmalloc(need_one + need_two + 1); | |
62 | strcpy(xp, one); | |
63 | strcpy(xp + need_one, two); | |
64 | return xp; | |
65 | } | |
66 | ||
be3cfa85 | 67 | static const char *external_diff(void) |
86436c28 | 68 | { |
d19938ab | 69 | static const char *external_diff_cmd = NULL; |
be3cfa85 JH |
70 | static int done_preparing = 0; |
71 | ||
72 | if (done_preparing) | |
73 | return external_diff_cmd; | |
a9ab586a | 74 | external_diff_cmd = getenv("GIT_EXTERNAL_DIFF"); |
be3cfa85 JH |
75 | done_preparing = 1; |
76 | return external_diff_cmd; | |
86436c28 JH |
77 | } |
78 | ||
64f8a631 HE |
79 | #define TEMPFILE_PATH_LEN 50 |
80 | ||
be3cfa85 | 81 | static struct diff_tempfile { |
427dcb4b | 82 | const char *name; /* filename external diff should read from */ |
be3cfa85 JH |
83 | char hex[41]; |
84 | char mode[10]; | |
64f8a631 | 85 | char tmp_path[TEMPFILE_PATH_LEN]; |
be3cfa85 JH |
86 | } diff_temp[2]; |
87 | ||
cebff98d | 88 | static int count_lines(const char *data, int size) |
366175ef | 89 | { |
366175ef | 90 | int count, ch, completely_empty = 1, nl_just_seen = 0; |
366175ef | 91 | count = 0; |
cebff98d JH |
92 | while (0 < size--) { |
93 | ch = *data++; | |
366175ef JH |
94 | if (ch == '\n') { |
95 | count++; | |
96 | nl_just_seen = 1; | |
97 | completely_empty = 0; | |
98 | } | |
99 | else { | |
100 | nl_just_seen = 0; | |
101 | completely_empty = 0; | |
102 | } | |
cebff98d | 103 | } |
366175ef JH |
104 | if (completely_empty) |
105 | return 0; | |
106 | if (!nl_just_seen) | |
107 | count++; /* no trailing newline */ | |
108 | return count; | |
109 | } | |
110 | ||
111 | static void print_line_count(int count) | |
112 | { | |
113 | switch (count) { | |
114 | case 0: | |
115 | printf("0,0"); | |
116 | break; | |
117 | case 1: | |
118 | printf("1"); | |
119 | break; | |
120 | default: | |
121 | printf("1,%d", count); | |
122 | break; | |
123 | } | |
124 | } | |
125 | ||
cebff98d | 126 | static void copy_file(int prefix, const char *data, int size) |
366175ef | 127 | { |
366175ef | 128 | int ch, nl_just_seen = 1; |
cebff98d JH |
129 | while (0 < size--) { |
130 | ch = *data++; | |
366175ef JH |
131 | if (nl_just_seen) |
132 | putchar(prefix); | |
133 | putchar(ch); | |
134 | if (ch == '\n') | |
135 | nl_just_seen = 1; | |
136 | else | |
137 | nl_just_seen = 0; | |
138 | } | |
366175ef JH |
139 | if (!nl_just_seen) |
140 | printf("\n\\ No newline at end of file\n"); | |
141 | } | |
142 | ||
143 | static void emit_rewrite_diff(const char *name_a, | |
144 | const char *name_b, | |
a041d94f | 145 | struct diff_filespec *one, |
cebff98d | 146 | struct diff_filespec *two) |
366175ef | 147 | { |
366175ef | 148 | int lc_a, lc_b; |
a041d94f JH |
149 | diff_populate_filespec(one, 0); |
150 | diff_populate_filespec(two, 0); | |
cebff98d JH |
151 | lc_a = count_lines(one->data, one->size); |
152 | lc_b = count_lines(two->data, two->size); | |
366175ef JH |
153 | printf("--- %s\n+++ %s\n@@ -", name_a, name_b); |
154 | print_line_count(lc_a); | |
155 | printf(" +"); | |
156 | print_line_count(lc_b); | |
157 | printf(" @@\n"); | |
158 | if (lc_a) | |
cebff98d | 159 | copy_file('-', one->data, one->size); |
366175ef | 160 | if (lc_b) |
cebff98d | 161 | copy_file('+', two->data, two->size); |
366175ef JH |
162 | } |
163 | ||
cebff98d | 164 | static int fill_mmfile(mmfile_t *mf, struct diff_filespec *one) |
3443546f | 165 | { |
cebff98d JH |
166 | if (!DIFF_FILE_VALID(one)) { |
167 | mf->ptr = ""; /* does not matter */ | |
168 | mf->size = 0; | |
3443546f | 169 | return 0; |
3443546f | 170 | } |
cebff98d JH |
171 | else if (diff_populate_filespec(one, 0)) |
172 | return -1; | |
173 | mf->ptr = one->data; | |
174 | mf->size = one->size; | |
3443546f LT |
175 | return 0; |
176 | } | |
177 | ||
3ce8f089 JH |
178 | struct emit_callback { |
179 | const char **label_path; | |
180 | }; | |
181 | ||
3443546f LT |
182 | static int fn_out(void *priv, mmbuffer_t *mb, int nbuf) |
183 | { | |
184 | int i; | |
3ce8f089 | 185 | struct emit_callback *ecbdata = priv; |
3443546f | 186 | |
3ce8f089 JH |
187 | if (ecbdata->label_path[0]) { |
188 | printf("--- %s\n", ecbdata->label_path[0]); | |
189 | printf("+++ %s\n", ecbdata->label_path[1]); | |
190 | ecbdata->label_path[0] = ecbdata->label_path[1] = NULL; | |
191 | } | |
3443546f LT |
192 | for (i = 0; i < nbuf; i++) |
193 | if (!fwrite(mb[i].ptr, mb[i].size, 1, stdout)) | |
194 | return -1; | |
195 | return 0; | |
196 | } | |
197 | ||
3ce8f089 JH |
198 | #define FIRST_FEW_BYTES 8000 |
199 | static int mmfile_is_binary(mmfile_t *mf) | |
200 | { | |
201 | long sz = mf->size; | |
202 | if (FIRST_FEW_BYTES < sz) | |
203 | sz = FIRST_FEW_BYTES; | |
204 | if (memchr(mf->ptr, 0, sz)) | |
205 | return 1; | |
206 | return 0; | |
207 | } | |
208 | ||
cebff98d | 209 | static void builtin_diff(const char *name_a, |
915838c3 | 210 | const char *name_b, |
cebff98d JH |
211 | struct diff_filespec *one, |
212 | struct diff_filespec *two, | |
366175ef | 213 | const char *xfrm_msg, |
cebff98d | 214 | int complete_rewrite) |
86436c28 | 215 | { |
3443546f | 216 | mmfile_t mf1, mf2; |
cebff98d JH |
217 | const char *lbl[2]; |
218 | char *a_one, *b_two; | |
219 | ||
220 | a_one = quote_two("a/", name_a); | |
221 | b_two = quote_two("b/", name_b); | |
222 | lbl[0] = DIFF_FILE_VALID(one) ? a_one : "/dev/null"; | |
223 | lbl[1] = DIFF_FILE_VALID(two) ? b_two : "/dev/null"; | |
224 | printf("diff --git %s %s\n", a_one, b_two); | |
225 | if (lbl[0][0] == '/') { | |
226 | /* /dev/null */ | |
227 | printf("new file mode %06o\n", two->mode); | |
70aadac0 JH |
228 | if (xfrm_msg && xfrm_msg[0]) |
229 | puts(xfrm_msg); | |
230 | } | |
cebff98d JH |
231 | else if (lbl[1][0] == '/') { |
232 | printf("deleted file mode %06o\n", one->mode); | |
70aadac0 JH |
233 | if (xfrm_msg && xfrm_msg[0]) |
234 | puts(xfrm_msg); | |
235 | } | |
273b9834 | 236 | else { |
cebff98d JH |
237 | if (one->mode != two->mode) { |
238 | printf("old mode %06o\n", one->mode); | |
239 | printf("new mode %06o\n", two->mode); | |
b58f23b3 | 240 | } |
427dcb4b | 241 | if (xfrm_msg && xfrm_msg[0]) |
09d9d1a6 | 242 | puts(xfrm_msg); |
8676eb43 LT |
243 | /* |
244 | * we do not run diff between different kind | |
245 | * of objects. | |
246 | */ | |
cebff98d JH |
247 | if ((one->mode ^ two->mode) & S_IFMT) |
248 | goto free_ab_and_return; | |
366175ef | 249 | if (complete_rewrite) { |
cebff98d JH |
250 | emit_rewrite_diff(name_a, name_b, one, two); |
251 | goto free_ab_and_return; | |
366175ef | 252 | } |
b28858bf | 253 | } |
8676eb43 | 254 | |
cebff98d | 255 | if (fill_mmfile(&mf1, one) < 0 || fill_mmfile(&mf2, two) < 0) |
3443546f LT |
256 | die("unable to read files to diff"); |
257 | ||
3ce8f089 | 258 | if (mmfile_is_binary(&mf1) || mmfile_is_binary(&mf2)) |
cebff98d | 259 | printf("Binary files %s and %s differ\n", lbl[0], lbl[1]); |
3ce8f089 JH |
260 | else { |
261 | /* Crazy xdl interfaces.. */ | |
262 | const char *diffopts = getenv("GIT_DIFF_OPTS"); | |
3443546f LT |
263 | xpparam_t xpp; |
264 | xdemitconf_t xecfg; | |
265 | xdemitcb_t ecb; | |
3ce8f089 | 266 | struct emit_callback ecbdata; |
3443546f | 267 | |
cebff98d | 268 | ecbdata.label_path = lbl; |
3443546f LT |
269 | xpp.flags = XDF_NEED_MINIMAL; |
270 | xecfg.ctxlen = 3; | |
acb72577 | 271 | xecfg.flags = XDL_EMIT_FUNCNAMES; |
3ce8f089 JH |
272 | if (!diffopts) |
273 | ; | |
274 | else if (!strncmp(diffopts, "--unified=", 10)) | |
275 | xecfg.ctxlen = strtoul(diffopts + 10, NULL, 10); | |
276 | else if (!strncmp(diffopts, "-u", 2)) | |
277 | xecfg.ctxlen = strtoul(diffopts + 2, NULL, 10); | |
3443546f | 278 | ecb.outf = fn_out; |
3ce8f089 | 279 | ecb.priv = &ecbdata; |
3443546f LT |
280 | xdl_diff(&mf1, &mf2, &xpp, &xecfg, &ecb); |
281 | } | |
282 | ||
cebff98d JH |
283 | free_ab_and_return: |
284 | free(a_one); | |
285 | free(b_two); | |
286 | return; | |
86436c28 JH |
287 | } |
288 | ||
427dcb4b JH |
289 | struct diff_filespec *alloc_filespec(const char *path) |
290 | { | |
291 | int namelen = strlen(path); | |
292 | struct diff_filespec *spec = xmalloc(sizeof(*spec) + namelen + 1); | |
705a7148 LT |
293 | |
294 | memset(spec, 0, sizeof(*spec)); | |
427dcb4b | 295 | spec->path = (char *)(spec + 1); |
705a7148 | 296 | memcpy(spec->path, path, namelen+1); |
427dcb4b JH |
297 | return spec; |
298 | } | |
299 | ||
300 | void fill_filespec(struct diff_filespec *spec, const unsigned char *sha1, | |
301 | unsigned short mode) | |
302 | { | |
4130b995 | 303 | if (mode) { |
1b0c7174 | 304 | spec->mode = canon_mode(mode); |
81e50eab JH |
305 | memcpy(spec->sha1, sha1, 20); |
306 | spec->sha1_valid = !!memcmp(sha1, null_sha1, 20); | |
307 | } | |
427dcb4b JH |
308 | } |
309 | ||
b46f0b6d JH |
310 | /* |
311 | * Given a name and sha1 pair, if the dircache tells us the file in | |
312 | * the work tree has that object contents, return true, so that | |
313 | * prepare_temp_file() does not have to inflate and extract. | |
314 | */ | |
315 | static int work_tree_matches(const char *name, const unsigned char *sha1) | |
316 | { | |
317 | struct cache_entry *ce; | |
318 | struct stat st; | |
319 | int pos, len; | |
5c97558c | 320 | |
b46f0b6d JH |
321 | /* We do not read the cache ourselves here, because the |
322 | * benchmark with my previous version that always reads cache | |
323 | * shows that it makes things worse for diff-tree comparing | |
324 | * two linux-2.6 kernel trees in an already checked out work | |
915838c3 | 325 | * tree. This is because most diff-tree comparisons deal with |
b46f0b6d JH |
326 | * only a small number of files, while reading the cache is |
327 | * expensive for a large project, and its cost outweighs the | |
328 | * savings we get by not inflating the object to a temporary | |
329 | * file. Practically, this code only helps when we are used | |
330 | * by diff-cache --cached, which does read the cache before | |
331 | * calling us. | |
57fe64a4 | 332 | */ |
b46f0b6d JH |
333 | if (!active_cache) |
334 | return 0; | |
335 | ||
336 | len = strlen(name); | |
337 | pos = cache_name_pos(name, len); | |
338 | if (pos < 0) | |
339 | return 0; | |
340 | ce = active_cache[pos]; | |
b28858bf | 341 | if ((lstat(name, &st) < 0) || |
427dcb4b | 342 | !S_ISREG(st.st_mode) || /* careful! */ |
5f73076c | 343 | ce_match_stat(ce, &st, 0) || |
b46f0b6d JH |
344 | memcmp(sha1, ce->sha1, 20)) |
345 | return 0; | |
427dcb4b JH |
346 | /* we return 1 only when we can stat, it is a regular file, |
347 | * stat information matches, and sha1 recorded in the cache | |
348 | * matches. I.e. we know the file in the work tree really is | |
349 | * the same as the <name, sha1> pair. | |
350 | */ | |
b46f0b6d JH |
351 | return 1; |
352 | } | |
353 | ||
f0c6b2a2 JH |
354 | static struct sha1_size_cache { |
355 | unsigned char sha1[20]; | |
356 | unsigned long size; | |
357 | } **sha1_size_cache; | |
358 | static int sha1_size_cache_nr, sha1_size_cache_alloc; | |
359 | ||
360 | static struct sha1_size_cache *locate_size_cache(unsigned char *sha1, | |
0601e131 | 361 | int find_only, |
f0c6b2a2 JH |
362 | unsigned long size) |
363 | { | |
364 | int first, last; | |
365 | struct sha1_size_cache *e; | |
366 | ||
367 | first = 0; | |
368 | last = sha1_size_cache_nr; | |
369 | while (last > first) { | |
0601e131 | 370 | int cmp, next = (last + first) >> 1; |
f0c6b2a2 | 371 | e = sha1_size_cache[next]; |
0601e131 | 372 | cmp = memcmp(e->sha1, sha1, 20); |
f0c6b2a2 JH |
373 | if (!cmp) |
374 | return e; | |
375 | if (cmp < 0) { | |
376 | last = next; | |
377 | continue; | |
378 | } | |
379 | first = next+1; | |
380 | } | |
381 | /* not found */ | |
0601e131 | 382 | if (find_only) |
f0c6b2a2 JH |
383 | return NULL; |
384 | /* insert to make it at "first" */ | |
385 | if (sha1_size_cache_alloc <= sha1_size_cache_nr) { | |
386 | sha1_size_cache_alloc = alloc_nr(sha1_size_cache_alloc); | |
387 | sha1_size_cache = xrealloc(sha1_size_cache, | |
388 | sha1_size_cache_alloc * | |
389 | sizeof(*sha1_size_cache)); | |
390 | } | |
391 | sha1_size_cache_nr++; | |
392 | if (first < sha1_size_cache_nr) | |
393 | memmove(sha1_size_cache + first + 1, sha1_size_cache + first, | |
394 | (sha1_size_cache_nr - first - 1) * | |
395 | sizeof(*sha1_size_cache)); | |
396 | e = xmalloc(sizeof(struct sha1_size_cache)); | |
397 | sha1_size_cache[first] = e; | |
398 | memcpy(e->sha1, sha1, 20); | |
399 | e->size = size; | |
400 | return e; | |
401 | } | |
402 | ||
427dcb4b JH |
403 | /* |
404 | * While doing rename detection and pickaxe operation, we may need to | |
405 | * grab the data for the blob (or file) for our own in-core comparison. | |
406 | * diff_filespec has data and size fields for this purpose. | |
407 | */ | |
f0c6b2a2 | 408 | int diff_populate_filespec(struct diff_filespec *s, int size_only) |
427dcb4b JH |
409 | { |
410 | int err = 0; | |
81e50eab | 411 | if (!DIFF_FILE_VALID(s)) |
427dcb4b JH |
412 | die("internal error: asking to populate invalid file."); |
413 | if (S_ISDIR(s->mode)) | |
414 | return -1; | |
415 | ||
f0c6b2a2 JH |
416 | if (!use_size_cache) |
417 | size_only = 0; | |
418 | ||
427dcb4b JH |
419 | if (s->data) |
420 | return err; | |
421 | if (!s->sha1_valid || | |
422 | work_tree_matches(s->path, s->sha1)) { | |
423 | struct stat st; | |
424 | int fd; | |
425 | if (lstat(s->path, &st) < 0) { | |
426 | if (errno == ENOENT) { | |
427 | err_empty: | |
428 | err = -1; | |
429 | empty: | |
430 | s->data = ""; | |
431 | s->size = 0; | |
432 | return err; | |
433 | } | |
434 | } | |
435 | s->size = st.st_size; | |
436 | if (!s->size) | |
437 | goto empty; | |
f0c6b2a2 JH |
438 | if (size_only) |
439 | return 0; | |
427dcb4b JH |
440 | if (S_ISLNK(st.st_mode)) { |
441 | int ret; | |
442 | s->data = xmalloc(s->size); | |
443 | s->should_free = 1; | |
444 | ret = readlink(s->path, s->data, s->size); | |
445 | if (ret < 0) { | |
446 | free(s->data); | |
447 | goto err_empty; | |
448 | } | |
449 | return 0; | |
450 | } | |
451 | fd = open(s->path, O_RDONLY); | |
452 | if (fd < 0) | |
453 | goto err_empty; | |
454 | s->data = mmap(NULL, s->size, PROT_READ, MAP_PRIVATE, fd, 0); | |
427dcb4b | 455 | close(fd); |
e35f9824 PR |
456 | if (s->data == MAP_FAILED) |
457 | goto err_empty; | |
458 | s->should_munmap = 1; | |
427dcb4b JH |
459 | } |
460 | else { | |
461 | char type[20]; | |
f0c6b2a2 JH |
462 | struct sha1_size_cache *e; |
463 | ||
464 | if (size_only) { | |
0601e131 | 465 | e = locate_size_cache(s->sha1, 1, 0); |
f0c6b2a2 JH |
466 | if (e) { |
467 | s->size = e->size; | |
468 | return 0; | |
469 | } | |
36e4d74a | 470 | if (!sha1_object_info(s->sha1, type, &s->size)) |
0601e131 | 471 | locate_size_cache(s->sha1, 0, s->size); |
65c2e0c3 JH |
472 | } |
473 | else { | |
474 | s->data = read_sha1_file(s->sha1, type, &s->size); | |
475 | s->should_free = 1; | |
f0c6b2a2 | 476 | } |
427dcb4b JH |
477 | } |
478 | return 0; | |
479 | } | |
480 | ||
19397b45 | 481 | void diff_free_filespec_data(struct diff_filespec *s) |
427dcb4b JH |
482 | { |
483 | if (s->should_free) | |
484 | free(s->data); | |
485 | else if (s->should_munmap) | |
486 | munmap(s->data, s->size); | |
19397b45 JH |
487 | s->should_free = s->should_munmap = 0; |
488 | s->data = NULL; | |
c06c7966 JH |
489 | free(s->cnt_data); |
490 | s->cnt_data = NULL; | |
427dcb4b JH |
491 | } |
492 | ||
b28858bf JH |
493 | static void prep_temp_blob(struct diff_tempfile *temp, |
494 | void *blob, | |
495 | unsigned long size, | |
88cd621d | 496 | const unsigned char *sha1, |
b28858bf JH |
497 | int mode) |
498 | { | |
499 | int fd; | |
500 | ||
64f8a631 | 501 | fd = git_mkstemp(temp->tmp_path, TEMPFILE_PATH_LEN, ".diff_XXXXXX"); |
b28858bf JH |
502 | if (fd < 0) |
503 | die("unable to create temp-file"); | |
504 | if (write(fd, blob, size) != size) | |
505 | die("unable to write temp-file"); | |
506 | close(fd); | |
507 | temp->name = temp->tmp_path; | |
508 | strcpy(temp->hex, sha1_to_hex(sha1)); | |
509 | temp->hex[40] = 0; | |
510 | sprintf(temp->mode, "%06o", mode); | |
511 | } | |
512 | ||
be3cfa85 JH |
513 | static void prepare_temp_file(const char *name, |
514 | struct diff_tempfile *temp, | |
427dcb4b | 515 | struct diff_filespec *one) |
86436c28 | 516 | { |
81e50eab | 517 | if (!DIFF_FILE_VALID(one)) { |
be3cfa85 | 518 | not_a_valid_file: |
532149d7 JH |
519 | /* A '-' entry produces this for file-2, and |
520 | * a '+' entry produces this for file-1. | |
521 | */ | |
be3cfa85 JH |
522 | temp->name = "/dev/null"; |
523 | strcpy(temp->hex, "."); | |
524 | strcpy(temp->mode, "."); | |
86436c28 JH |
525 | return; |
526 | } | |
be3cfa85 | 527 | |
5c97558c | 528 | if (!one->sha1_valid || |
427dcb4b | 529 | work_tree_matches(name, one->sha1)) { |
be3cfa85 | 530 | struct stat st; |
427dcb4b | 531 | if (lstat(name, &st) < 0) { |
be3cfa85 JH |
532 | if (errno == ENOENT) |
533 | goto not_a_valid_file; | |
427dcb4b | 534 | die("stat(%s): %s", name, strerror(errno)); |
be3cfa85 | 535 | } |
b28858bf JH |
536 | if (S_ISLNK(st.st_mode)) { |
537 | int ret; | |
7e4a2a84 JH |
538 | char buf[PATH_MAX + 1]; /* ought to be SYMLINK_MAX */ |
539 | if (sizeof(buf) <= st.st_size) | |
540 | die("symlink too long: %s", name); | |
b28858bf JH |
541 | ret = readlink(name, buf, st.st_size); |
542 | if (ret < 0) | |
543 | die("readlink(%s)", name); | |
544 | prep_temp_blob(temp, buf, st.st_size, | |
545 | (one->sha1_valid ? | |
427dcb4b | 546 | one->sha1 : null_sha1), |
b28858bf JH |
547 | (one->sha1_valid ? |
548 | one->mode : S_IFLNK)); | |
549 | } | |
550 | else { | |
427dcb4b JH |
551 | /* we can borrow from the file in the work tree */ |
552 | temp->name = name; | |
b28858bf JH |
553 | if (!one->sha1_valid) |
554 | strcpy(temp->hex, sha1_to_hex(null_sha1)); | |
555 | else | |
427dcb4b | 556 | strcpy(temp->hex, sha1_to_hex(one->sha1)); |
9d429ff6 JH |
557 | /* Even though we may sometimes borrow the |
558 | * contents from the work tree, we always want | |
559 | * one->mode. mode is trustworthy even when | |
560 | * !(one->sha1_valid), as long as | |
561 | * DIFF_FILE_VALID(one). | |
562 | */ | |
563 | sprintf(temp->mode, "%06o", one->mode); | |
b28858bf JH |
564 | } |
565 | return; | |
be3cfa85 JH |
566 | } |
567 | else { | |
f0c6b2a2 | 568 | if (diff_populate_filespec(one, 0)) |
427dcb4b JH |
569 | die("cannot read data blob for %s", one->path); |
570 | prep_temp_blob(temp, one->data, one->size, | |
571 | one->sha1, one->mode); | |
be3cfa85 JH |
572 | } |
573 | } | |
574 | ||
575 | static void remove_tempfile(void) | |
576 | { | |
577 | int i; | |
578 | ||
579 | for (i = 0; i < 2; i++) | |
580 | if (diff_temp[i].name == diff_temp[i].tmp_path) { | |
581 | unlink(diff_temp[i].name); | |
582 | diff_temp[i].name = NULL; | |
583 | } | |
584 | } | |
585 | ||
532149d7 JH |
586 | static void remove_tempfile_on_signal(int signo) |
587 | { | |
588 | remove_tempfile(); | |
6a1f79c1 PB |
589 | signal(SIGINT, SIG_DFL); |
590 | raise(signo); | |
532149d7 JH |
591 | } |
592 | ||
8676eb43 LT |
593 | static int spawn_prog(const char *pgm, const char **arg) |
594 | { | |
595 | pid_t pid; | |
596 | int status; | |
597 | ||
598 | fflush(NULL); | |
599 | pid = fork(); | |
600 | if (pid < 0) | |
601 | die("unable to fork"); | |
602 | if (!pid) { | |
603 | execvp(pgm, (char *const*) arg); | |
604 | exit(255); | |
605 | } | |
606 | ||
607 | while (waitpid(pid, &status, 0) < 0) { | |
608 | if (errno == EINTR) | |
609 | continue; | |
610 | return -1; | |
611 | } | |
612 | ||
613 | /* Earlier we did not check the exit status because | |
614 | * diff exits non-zero if files are different, and | |
615 | * we are not interested in knowing that. It was a | |
616 | * mistake which made it harder to quit a diff-* | |
617 | * session that uses the git-apply-patch-script as | |
618 | * the GIT_EXTERNAL_DIFF. A custom GIT_EXTERNAL_DIFF | |
619 | * should also exit non-zero only when it wants to | |
620 | * abort the entire diff-* session. | |
621 | */ | |
622 | if (WIFEXITED(status) && !WEXITSTATUS(status)) | |
623 | return 0; | |
624 | return -1; | |
625 | } | |
626 | ||
be3cfa85 JH |
627 | /* An external diff command takes: |
628 | * | |
629 | * diff-cmd name infile1 infile1-sha1 infile1-mode \ | |
5c97558c | 630 | * infile2 infile2-sha1 infile2-mode [ rename-to ] |
be3cfa85 JH |
631 | * |
632 | */ | |
4130b995 JH |
633 | static void run_external_diff(const char *pgm, |
634 | const char *name, | |
5c97558c | 635 | const char *other, |
427dcb4b JH |
636 | struct diff_filespec *one, |
637 | struct diff_filespec *two, | |
366175ef JH |
638 | const char *xfrm_msg, |
639 | int complete_rewrite) | |
be3cfa85 | 640 | { |
8676eb43 | 641 | const char *spawn_arg[10]; |
be3cfa85 | 642 | struct diff_tempfile *temp = diff_temp; |
8676eb43 | 643 | int retval; |
be3cfa85 | 644 | static int atexit_asked = 0; |
c7c81b3a | 645 | const char *othername; |
cebff98d | 646 | const char **arg = &spawn_arg[0]; |
be3cfa85 | 647 | |
c7c81b3a | 648 | othername = (other? other : name); |
77eb2720 JH |
649 | if (one && two) { |
650 | prepare_temp_file(name, &temp[0], one); | |
c7c81b3a | 651 | prepare_temp_file(othername, &temp[1], two); |
77eb2720 JH |
652 | if (! atexit_asked && |
653 | (temp[0].name == temp[0].tmp_path || | |
654 | temp[1].name == temp[1].tmp_path)) { | |
655 | atexit_asked = 1; | |
656 | atexit(remove_tempfile); | |
657 | } | |
532149d7 | 658 | signal(SIGINT, remove_tempfile_on_signal); |
be3cfa85 JH |
659 | } |
660 | ||
cebff98d JH |
661 | if (one && two) { |
662 | *arg++ = pgm; | |
663 | *arg++ = name; | |
664 | *arg++ = temp[0].name; | |
665 | *arg++ = temp[0].hex; | |
666 | *arg++ = temp[0].mode; | |
667 | *arg++ = temp[1].name; | |
668 | *arg++ = temp[1].hex; | |
669 | *arg++ = temp[1].mode; | |
670 | if (other) { | |
671 | *arg++ = other; | |
672 | *arg++ = xfrm_msg; | |
77eb2720 | 673 | } |
8676eb43 | 674 | } else { |
cebff98d JH |
675 | *arg++ = pgm; |
676 | *arg++ = name; | |
be3cfa85 | 677 | } |
cebff98d JH |
678 | *arg = NULL; |
679 | retval = spawn_prog(pgm, spawn_arg); | |
8676eb43 LT |
680 | remove_tempfile(); |
681 | if (retval) { | |
6fa28064 JH |
682 | fprintf(stderr, "external diff died, stopping at %s.\n", name); |
683 | exit(1); | |
532149d7 | 684 | } |
be3cfa85 JH |
685 | } |
686 | ||
cebff98d JH |
687 | static void run_diff_cmd(const char *pgm, |
688 | const char *name, | |
689 | const char *other, | |
690 | struct diff_filespec *one, | |
691 | struct diff_filespec *two, | |
692 | const char *xfrm_msg, | |
693 | int complete_rewrite) | |
694 | { | |
695 | if (pgm) { | |
696 | run_external_diff(pgm, name, other, one, two, xfrm_msg, | |
697 | complete_rewrite); | |
698 | return; | |
699 | } | |
700 | if (one && two) | |
701 | builtin_diff(name, other ? other : name, | |
702 | one, two, xfrm_msg, complete_rewrite); | |
703 | else | |
704 | printf("* Unmerged path %s\n", name); | |
705 | } | |
706 | ||
ec1fcc16 JH |
707 | static void diff_fill_sha1_info(struct diff_filespec *one) |
708 | { | |
709 | if (DIFF_FILE_VALID(one)) { | |
710 | if (!one->sha1_valid) { | |
711 | struct stat st; | |
975b31dc | 712 | if (lstat(one->path, &st) < 0) |
ec1fcc16 JH |
713 | die("stat %s", one->path); |
714 | if (index_path(one->sha1, one->path, &st, 0)) | |
715 | die("cannot hash %s\n", one->path); | |
716 | } | |
717 | } | |
718 | else | |
719 | memset(one->sha1, 0, 20); | |
720 | } | |
721 | ||
80b1e511 | 722 | static void run_diff(struct diff_filepair *p, struct diff_options *o) |
4130b995 JH |
723 | { |
724 | const char *pgm = external_diff(); | |
ec1fcc16 | 725 | char msg[PATH_MAX*2+300], *xfrm_msg; |
366175ef JH |
726 | struct diff_filespec *one; |
727 | struct diff_filespec *two; | |
728 | const char *name; | |
729 | const char *other; | |
cf9dfc66 | 730 | char *name_munged, *other_munged; |
366175ef | 731 | int complete_rewrite = 0; |
ec1fcc16 | 732 | int len; |
366175ef JH |
733 | |
734 | if (DIFF_PAIR_UNMERGED(p)) { | |
735 | /* unmerged */ | |
cebff98d | 736 | run_diff_cmd(pgm, p->one->path, NULL, NULL, NULL, NULL, 0); |
366175ef JH |
737 | return; |
738 | } | |
739 | ||
740 | name = p->one->path; | |
741 | other = (strcmp(name, p->two->path) ? p->two->path : NULL); | |
cf9dfc66 JH |
742 | name_munged = quote_one(name); |
743 | other_munged = quote_one(other); | |
366175ef | 744 | one = p->one; two = p->two; |
ec1fcc16 JH |
745 | |
746 | diff_fill_sha1_info(one); | |
747 | diff_fill_sha1_info(two); | |
748 | ||
749 | len = 0; | |
366175ef | 750 | switch (p->status) { |
e7baa4f4 | 751 | case DIFF_STATUS_COPIED: |
ec1fcc16 JH |
752 | len += snprintf(msg + len, sizeof(msg) - len, |
753 | "similarity index %d%%\n" | |
754 | "copy from %s\n" | |
755 | "copy to %s\n", | |
756 | (int)(0.5 + p->score * 100.0/MAX_SCORE), | |
cf9dfc66 | 757 | name_munged, other_munged); |
366175ef | 758 | break; |
e7baa4f4 | 759 | case DIFF_STATUS_RENAMED: |
ec1fcc16 JH |
760 | len += snprintf(msg + len, sizeof(msg) - len, |
761 | "similarity index %d%%\n" | |
762 | "rename from %s\n" | |
763 | "rename to %s\n", | |
764 | (int)(0.5 + p->score * 100.0/MAX_SCORE), | |
cf9dfc66 | 765 | name_munged, other_munged); |
366175ef | 766 | break; |
e7baa4f4 | 767 | case DIFF_STATUS_MODIFIED: |
366175ef | 768 | if (p->score) { |
ec1fcc16 JH |
769 | len += snprintf(msg + len, sizeof(msg) - len, |
770 | "dissimilarity index %d%%\n", | |
771 | (int)(0.5 + p->score * | |
772 | 100.0/MAX_SCORE)); | |
366175ef JH |
773 | complete_rewrite = 1; |
774 | break; | |
775 | } | |
776 | /* fallthru */ | |
777 | default: | |
ec1fcc16 JH |
778 | /* nothing */ |
779 | ; | |
366175ef JH |
780 | } |
781 | ||
ec1fcc16 JH |
782 | if (memcmp(one->sha1, two->sha1, 20)) { |
783 | char one_sha1[41]; | |
46a6c262 | 784 | int abbrev = o->full_index ? 40 : DEFAULT_ABBREV; |
ec1fcc16 JH |
785 | memcpy(one_sha1, sha1_to_hex(one->sha1), 41); |
786 | ||
787 | len += snprintf(msg + len, sizeof(msg) - len, | |
47dd0d59 JH |
788 | "index %.*s..%.*s", |
789 | abbrev, one_sha1, abbrev, | |
790 | sha1_to_hex(two->sha1)); | |
ec1fcc16 JH |
791 | if (one->mode == two->mode) |
792 | len += snprintf(msg + len, sizeof(msg) - len, | |
793 | " %06o", one->mode); | |
794 | len += snprintf(msg + len, sizeof(msg) - len, "\n"); | |
795 | } | |
796 | ||
797 | if (len) | |
798 | msg[--len] = 0; | |
799 | xfrm_msg = len ? msg : NULL; | |
800 | ||
4130b995 JH |
801 | if (!pgm && |
802 | DIFF_FILE_VALID(one) && DIFF_FILE_VALID(two) && | |
803 | (S_IFMT & one->mode) != (S_IFMT & two->mode)) { | |
804 | /* a filepair that changes between file and symlink | |
805 | * needs to be split into deletion and creation. | |
806 | */ | |
807 | struct diff_filespec *null = alloc_filespec(two->path); | |
cebff98d | 808 | run_diff_cmd(NULL, name, other, one, null, xfrm_msg, 0); |
4130b995 JH |
809 | free(null); |
810 | null = alloc_filespec(one->path); | |
cebff98d | 811 | run_diff_cmd(NULL, name, other, null, two, xfrm_msg, 0); |
4130b995 JH |
812 | free(null); |
813 | } | |
814 | else | |
cebff98d JH |
815 | run_diff_cmd(pgm, name, other, one, two, xfrm_msg, |
816 | complete_rewrite); | |
cf9dfc66 JH |
817 | |
818 | free(name_munged); | |
819 | free(other_munged); | |
4130b995 JH |
820 | } |
821 | ||
6b5ee137 | 822 | void diff_setup(struct diff_options *options) |
5c97558c | 823 | { |
6b5ee137 JH |
824 | memset(options, 0, sizeof(*options)); |
825 | options->output_format = DIFF_FORMAT_RAW; | |
826 | options->line_termination = '\n'; | |
827 | options->break_opt = -1; | |
8082d8d3 | 828 | options->rename_limit = -1; |
ac1b3d12 LT |
829 | |
830 | options->change = diff_change; | |
831 | options->add_remove = diff_addremove; | |
6b5ee137 JH |
832 | } |
833 | ||
834 | int diff_setup_done(struct diff_options *options) | |
835 | { | |
3299c6f6 JH |
836 | if ((options->find_copies_harder && |
837 | options->detect_rename != DIFF_DETECT_COPY) || | |
838 | (0 <= options->rename_limit && !options->detect_rename)) | |
6b5ee137 | 839 | return -1; |
3299c6f6 JH |
840 | if (options->detect_rename && options->rename_limit < 0) |
841 | options->rename_limit = diff_rename_limit_default; | |
6b5ee137 | 842 | if (options->setup & DIFF_SETUP_USE_CACHE) { |
f0c6b2a2 JH |
843 | if (!active_cache) |
844 | /* read-cache does not die even when it fails | |
845 | * so it is safe for us to do this here. Also | |
846 | * it does not smudge active_cache or active_nr | |
847 | * when it fails, so we do not have to worry about | |
82f9d58a | 848 | * cleaning it up ourselves either. |
f0c6b2a2 JH |
849 | */ |
850 | read_cache(); | |
851 | } | |
6b5ee137 | 852 | if (options->setup & DIFF_SETUP_USE_SIZE_CACHE) |
f0c6b2a2 | 853 | use_size_cache = 1; |
47dd0d59 JH |
854 | if (options->abbrev <= 0 || 40 < options->abbrev) |
855 | options->abbrev = 40; /* full */ | |
6b5ee137 JH |
856 | |
857 | return 0; | |
858 | } | |
859 | ||
860 | int diff_opt_parse(struct diff_options *options, const char **av, int ac) | |
861 | { | |
862 | const char *arg = av[0]; | |
863 | if (!strcmp(arg, "-p") || !strcmp(arg, "-u")) | |
864 | options->output_format = DIFF_FORMAT_PATCH; | |
86ff1d20 JH |
865 | else if (!strcmp(arg, "--patch-with-raw")) { |
866 | options->output_format = DIFF_FORMAT_PATCH; | |
867 | options->with_raw = 1; | |
868 | } | |
6b5ee137 JH |
869 | else if (!strcmp(arg, "-z")) |
870 | options->line_termination = 0; | |
8082d8d3 JH |
871 | else if (!strncmp(arg, "-l", 2)) |
872 | options->rename_limit = strtoul(arg+2, NULL, 10); | |
80b1e511 JH |
873 | else if (!strcmp(arg, "--full-index")) |
874 | options->full_index = 1; | |
6b5ee137 JH |
875 | else if (!strcmp(arg, "--name-only")) |
876 | options->output_format = DIFF_FORMAT_NAME; | |
946f5f7c JH |
877 | else if (!strcmp(arg, "--name-status")) |
878 | options->output_format = DIFF_FORMAT_NAME_STATUS; | |
6b5ee137 JH |
879 | else if (!strcmp(arg, "-R")) |
880 | options->reverse_diff = 1; | |
881 | else if (!strncmp(arg, "-S", 2)) | |
882 | options->pickaxe = arg + 2; | |
883 | else if (!strcmp(arg, "-s")) | |
884 | options->output_format = DIFF_FORMAT_NO_OUTPUT; | |
885 | else if (!strncmp(arg, "-O", 2)) | |
886 | options->orderfile = arg + 2; | |
887 | else if (!strncmp(arg, "--diff-filter=", 14)) | |
888 | options->filter = arg + 14; | |
889 | else if (!strcmp(arg, "--pickaxe-all")) | |
890 | options->pickaxe_opts = DIFF_PICKAXE_ALL; | |
d01d8c67 PB |
891 | else if (!strcmp(arg, "--pickaxe-regex")) |
892 | options->pickaxe_opts = DIFF_PICKAXE_REGEX; | |
6b5ee137 JH |
893 | else if (!strncmp(arg, "-B", 2)) { |
894 | if ((options->break_opt = | |
895 | diff_scoreopt_parse(arg)) == -1) | |
896 | return -1; | |
897 | } | |
898 | else if (!strncmp(arg, "-M", 2)) { | |
899 | if ((options->rename_score = | |
900 | diff_scoreopt_parse(arg)) == -1) | |
901 | return -1; | |
902 | options->detect_rename = DIFF_DETECT_RENAME; | |
903 | } | |
904 | else if (!strncmp(arg, "-C", 2)) { | |
905 | if ((options->rename_score = | |
906 | diff_scoreopt_parse(arg)) == -1) | |
907 | return -1; | |
908 | options->detect_rename = DIFF_DETECT_COPY; | |
909 | } | |
910 | else if (!strcmp(arg, "--find-copies-harder")) | |
911 | options->find_copies_harder = 1; | |
47dd0d59 | 912 | else if (!strcmp(arg, "--abbrev")) |
46a6c262 | 913 | options->abbrev = DEFAULT_ABBREV; |
6b1ddbdd | 914 | else if (!strncmp(arg, "--abbrev=", 9)) { |
47dd0d59 | 915 | options->abbrev = strtoul(arg + 9, NULL, 10); |
6b1ddbdd JH |
916 | if (options->abbrev < MINIMUM_ABBREV) |
917 | options->abbrev = MINIMUM_ABBREV; | |
918 | else if (40 < options->abbrev) | |
919 | options->abbrev = 40; | |
920 | } | |
6b5ee137 JH |
921 | else |
922 | return 0; | |
923 | return 1; | |
5c97558c JH |
924 | } |
925 | ||
0e3994fa JH |
926 | static int parse_num(const char **cp_p) |
927 | { | |
1b1480ff PA |
928 | unsigned long num, scale; |
929 | int ch, dot; | |
0e3994fa JH |
930 | const char *cp = *cp_p; |
931 | ||
1b1480ff | 932 | num = 0; |
0e3994fa | 933 | scale = 1; |
1b1480ff PA |
934 | dot = 0; |
935 | for(;;) { | |
936 | ch = *cp; | |
937 | if ( !dot && ch == '.' ) { | |
938 | scale = 1; | |
939 | dot = 1; | |
940 | } else if ( ch == '%' ) { | |
941 | scale = dot ? scale*100 : 100; | |
942 | cp++; /* % is always at the end */ | |
943 | break; | |
944 | } else if ( ch >= '0' && ch <= '9' ) { | |
945 | if ( scale < 100000 ) { | |
946 | scale *= 10; | |
947 | num = (num*10) + (ch-'0'); | |
948 | } | |
949 | } else { | |
950 | break; | |
0e3994fa | 951 | } |
5de36bfe | 952 | cp++; |
0e3994fa JH |
953 | } |
954 | *cp_p = cp; | |
955 | ||
956 | /* user says num divided by scale and we say internally that | |
957 | * is MAX_SCORE * num / scale. | |
958 | */ | |
1b1480ff | 959 | return (num >= scale) ? MAX_SCORE : (MAX_SCORE * num / scale); |
0e3994fa JH |
960 | } |
961 | ||
962 | int diff_scoreopt_parse(const char *opt) | |
963 | { | |
eeaa4603 | 964 | int opt1, opt2, cmd; |
0e3994fa JH |
965 | |
966 | if (*opt++ != '-') | |
967 | return -1; | |
968 | cmd = *opt++; | |
969 | if (cmd != 'M' && cmd != 'C' && cmd != 'B') | |
970 | return -1; /* that is not a -M, -C nor -B option */ | |
971 | ||
972 | opt1 = parse_num(&opt); | |
eeaa4603 JH |
973 | if (cmd != 'B') |
974 | opt2 = 0; | |
975 | else { | |
976 | if (*opt == 0) | |
977 | opt2 = 0; | |
978 | else if (*opt != '/') | |
979 | return -1; /* we expect -B80/99 or -B80 */ | |
980 | else { | |
981 | opt++; | |
982 | opt2 = parse_num(&opt); | |
983 | } | |
984 | } | |
0e3994fa JH |
985 | if (*opt != 0) |
986 | return -1; | |
eeaa4603 | 987 | return opt1 | (opt2 << 16); |
0e3994fa JH |
988 | } |
989 | ||
6b14d7fa JH |
990 | struct diff_queue_struct diff_queued_diff; |
991 | ||
992 | void diff_q(struct diff_queue_struct *queue, struct diff_filepair *dp) | |
5c97558c | 993 | { |
6b14d7fa JH |
994 | if (queue->alloc <= queue->nr) { |
995 | queue->alloc = alloc_nr(queue->alloc); | |
996 | queue->queue = xrealloc(queue->queue, | |
997 | sizeof(dp) * queue->alloc); | |
81e50eab | 998 | } |
6b14d7fa | 999 | queue->queue[queue->nr++] = dp; |
5c97558c JH |
1000 | } |
1001 | ||
52e95789 | 1002 | struct diff_filepair *diff_queue(struct diff_queue_struct *queue, |
6b14d7fa JH |
1003 | struct diff_filespec *one, |
1004 | struct diff_filespec *two) | |
5c97558c | 1005 | { |
52e95789 | 1006 | struct diff_filepair *dp = xmalloc(sizeof(*dp)); |
427dcb4b JH |
1007 | dp->one = one; |
1008 | dp->two = two; | |
f7c1512a | 1009 | dp->score = 0; |
2210100a | 1010 | dp->status = 0; |
15d061b4 | 1011 | dp->source_stays = 0; |
f345b0a0 | 1012 | dp->broken_pair = 0; |
5098bafb JH |
1013 | if (queue) |
1014 | diff_q(queue, dp); | |
427dcb4b | 1015 | return dp; |
5c97558c JH |
1016 | } |
1017 | ||
226406f6 JH |
1018 | void diff_free_filepair(struct diff_filepair *p) |
1019 | { | |
19397b45 JH |
1020 | diff_free_filespec_data(p->one); |
1021 | diff_free_filespec_data(p->two); | |
5098bafb JH |
1022 | free(p->one); |
1023 | free(p->two); | |
226406f6 JH |
1024 | free(p); |
1025 | } | |
1026 | ||
47dd0d59 | 1027 | /* This is different from find_unique_abbrev() in that |
297a1aad | 1028 | * it stuffs the result with dots for alignment. |
47dd0d59 JH |
1029 | */ |
1030 | const char *diff_unique_abbrev(const unsigned char *sha1, int len) | |
1031 | { | |
1032 | int abblen; | |
1033 | const char *abbrev; | |
1034 | if (len == 40) | |
1035 | return sha1_to_hex(sha1); | |
1036 | ||
1037 | abbrev = find_unique_abbrev(sha1, len); | |
297a1aad JH |
1038 | if (!abbrev) |
1039 | return sha1_to_hex(sha1); | |
47dd0d59 JH |
1040 | abblen = strlen(abbrev); |
1041 | if (abblen < 37) { | |
1042 | static char hex[41]; | |
1043 | if (len < abblen && abblen <= len + 2) | |
1044 | sprintf(hex, "%s%.*s", abbrev, len+3-abblen, ".."); | |
1045 | else | |
1046 | sprintf(hex, "%s...", abbrev); | |
1047 | return hex; | |
1048 | } | |
1049 | return sha1_to_hex(sha1); | |
1050 | } | |
1051 | ||
bceafe75 JH |
1052 | static void diff_flush_raw(struct diff_filepair *p, |
1053 | int line_termination, | |
946f5f7c | 1054 | int inter_name_termination, |
86ff1d20 JH |
1055 | struct diff_options *options, |
1056 | int output_format) | |
5c97558c | 1057 | { |
b6d8f309 JH |
1058 | int two_paths; |
1059 | char status[10]; | |
47dd0d59 | 1060 | int abbrev = options->abbrev; |
cf9dfc66 | 1061 | const char *path_one, *path_two; |
25d5ea41 | 1062 | |
cf9dfc66 JH |
1063 | path_one = p->one->path; |
1064 | path_two = p->two->path; | |
25d5ea41 | 1065 | if (line_termination) { |
cf9dfc66 JH |
1066 | path_one = quote_one(path_one); |
1067 | path_two = quote_one(path_two); | |
25d5ea41 JH |
1068 | } |
1069 | ||
366175ef JH |
1070 | if (p->score) |
1071 | sprintf(status, "%c%03d", p->status, | |
1072 | (int)(0.5 + p->score * 100.0/MAX_SCORE)); | |
1073 | else { | |
1074 | status[0] = p->status; | |
1075 | status[1] = 0; | |
1076 | } | |
b6d8f309 | 1077 | switch (p->status) { |
e7baa4f4 JH |
1078 | case DIFF_STATUS_COPIED: |
1079 | case DIFF_STATUS_RENAMED: | |
b6d8f309 | 1080 | two_paths = 1; |
b6d8f309 | 1081 | break; |
e7baa4f4 JH |
1082 | case DIFF_STATUS_ADDED: |
1083 | case DIFF_STATUS_DELETED: | |
f345b0a0 | 1084 | two_paths = 0; |
f345b0a0 | 1085 | break; |
b6d8f309 JH |
1086 | default: |
1087 | two_paths = 0; | |
b6d8f309 | 1088 | break; |
6b14d7fa | 1089 | } |
946f5f7c JH |
1090 | if (output_format != DIFF_FORMAT_NAME_STATUS) { |
1091 | printf(":%06o %06o %s ", | |
47dd0d59 JH |
1092 | p->one->mode, p->two->mode, |
1093 | diff_unique_abbrev(p->one->sha1, abbrev)); | |
1094 | printf("%s ", | |
1095 | diff_unique_abbrev(p->two->sha1, abbrev)); | |
946f5f7c | 1096 | } |
cf9dfc66 | 1097 | printf("%s%c%s", status, inter_name_termination, path_one); |
b6d8f309 | 1098 | if (two_paths) |
cf9dfc66 | 1099 | printf("%c%s", inter_name_termination, path_two); |
b6d8f309 | 1100 | putchar(line_termination); |
cf9dfc66 JH |
1101 | if (path_one != p->one->path) |
1102 | free((void*)path_one); | |
1103 | if (path_two != p->two->path) | |
1104 | free((void*)path_two); | |
5c97558c JH |
1105 | } |
1106 | ||
52f28529 | 1107 | static void diff_flush_name(struct diff_filepair *p, |
cf9dfc66 | 1108 | int inter_name_termination, |
52f28529 JH |
1109 | int line_termination) |
1110 | { | |
cf9dfc66 JH |
1111 | char *path = p->two->path; |
1112 | ||
1113 | if (line_termination) | |
1114 | path = quote_one(p->two->path); | |
1115 | else | |
1116 | path = p->two->path; | |
1117 | printf("%s%c", path, line_termination); | |
1118 | if (p->two->path != path) | |
1119 | free(path); | |
52f28529 JH |
1120 | } |
1121 | ||
f7c1512a | 1122 | int diff_unmodified_pair(struct diff_filepair *p) |
5c97558c | 1123 | { |
427dcb4b JH |
1124 | /* This function is written stricter than necessary to support |
1125 | * the currently implemented transformers, but the idea is to | |
52e95789 | 1126 | * let transformers to produce diff_filepairs any way they want, |
427dcb4b | 1127 | * and filter and clean them up here before producing the output. |
5c97558c | 1128 | */ |
6b14d7fa JH |
1129 | struct diff_filespec *one, *two; |
1130 | ||
1131 | if (DIFF_PAIR_UNMERGED(p)) | |
1132 | return 0; /* unmerged is interesting */ | |
5c97558c | 1133 | |
6b14d7fa JH |
1134 | one = p->one; |
1135 | two = p->two; | |
5c97558c | 1136 | |
4130b995 JH |
1137 | /* deletion, addition, mode or type change |
1138 | * and rename are all interesting. | |
1139 | */ | |
81e50eab | 1140 | if (DIFF_FILE_VALID(one) != DIFF_FILE_VALID(two) || |
4130b995 | 1141 | DIFF_PAIR_MODE_CHANGED(p) || |
427dcb4b JH |
1142 | strcmp(one->path, two->path)) |
1143 | return 0; | |
57fe64a4 | 1144 | |
427dcb4b JH |
1145 | /* both are valid and point at the same path. that is, we are |
1146 | * dealing with a change. | |
57fe64a4 | 1147 | */ |
427dcb4b JH |
1148 | if (one->sha1_valid && two->sha1_valid && |
1149 | !memcmp(one->sha1, two->sha1, sizeof(one->sha1))) | |
1150 | return 1; /* no change */ | |
1151 | if (!one->sha1_valid && !two->sha1_valid) | |
1152 | return 1; /* both look at the same file on the filesystem. */ | |
1153 | return 0; | |
57fe64a4 JH |
1154 | } |
1155 | ||
80b1e511 | 1156 | static void diff_flush_patch(struct diff_filepair *p, struct diff_options *o) |
f7c1512a | 1157 | { |
f7c1512a JH |
1158 | if (diff_unmodified_pair(p)) |
1159 | return; | |
1160 | ||
f7c1512a JH |
1161 | if ((DIFF_FILE_VALID(p->one) && S_ISDIR(p->one->mode)) || |
1162 | (DIFF_FILE_VALID(p->two) && S_ISDIR(p->two->mode))) | |
1163 | return; /* no tree diffs in patch format */ | |
1164 | ||
80b1e511 | 1165 | run_diff(p, o); |
f7c1512a JH |
1166 | } |
1167 | ||
bceafe75 | 1168 | int diff_queue_is_empty(void) |
f7c1512a | 1169 | { |
bceafe75 | 1170 | struct diff_queue_struct *q = &diff_queued_diff; |
25d5ea41 JH |
1171 | int i; |
1172 | for (i = 0; i < q->nr; i++) | |
1173 | if (!diff_unmodified_pair(q->queue[i])) | |
1174 | return 0; | |
1175 | return 1; | |
f7c1512a JH |
1176 | } |
1177 | ||
25d5ea41 JH |
1178 | #if DIFF_DEBUG |
1179 | void diff_debug_filespec(struct diff_filespec *s, int x, const char *one) | |
1180 | { | |
1181 | fprintf(stderr, "queue[%d] %s (%s) %s %06o %s\n", | |
c7c81b3a | 1182 | x, one ? one : "", |
25d5ea41 JH |
1183 | s->path, |
1184 | DIFF_FILE_VALID(s) ? "valid" : "invalid", | |
1185 | s->mode, | |
1186 | s->sha1_valid ? sha1_to_hex(s->sha1) : ""); | |
1187 | fprintf(stderr, "queue[%d] %s size %lu flags %d\n", | |
c7c81b3a | 1188 | x, one ? one : "", |
25d5ea41 JH |
1189 | s->size, s->xfrm_flags); |
1190 | } | |
1191 | ||
1192 | void diff_debug_filepair(const struct diff_filepair *p, int i) | |
1193 | { | |
1194 | diff_debug_filespec(p->one, i, "one"); | |
1195 | diff_debug_filespec(p->two, i, "two"); | |
f345b0a0 | 1196 | fprintf(stderr, "score %d, status %c stays %d broken %d\n", |
c7c81b3a | 1197 | p->score, p->status ? p->status : '?', |
f345b0a0 | 1198 | p->source_stays, p->broken_pair); |
25d5ea41 JH |
1199 | } |
1200 | ||
1201 | void diff_debug_queue(const char *msg, struct diff_queue_struct *q) | |
6b14d7fa | 1202 | { |
6b14d7fa | 1203 | int i; |
25d5ea41 JH |
1204 | if (msg) |
1205 | fprintf(stderr, "%s\n", msg); | |
1206 | fprintf(stderr, "q->nr = %d\n", q->nr); | |
6b14d7fa JH |
1207 | for (i = 0; i < q->nr; i++) { |
1208 | struct diff_filepair *p = q->queue[i]; | |
25d5ea41 JH |
1209 | diff_debug_filepair(p, i); |
1210 | } | |
1211 | } | |
1212 | #endif | |
1213 | ||
1214 | static void diff_resolve_rename_copy(void) | |
1215 | { | |
1216 | int i, j; | |
1217 | struct diff_filepair *p, *pp; | |
1218 | struct diff_queue_struct *q = &diff_queued_diff; | |
1219 | ||
25d5ea41 JH |
1220 | diff_debug_queue("resolve-rename-copy", q); |
1221 | ||
1222 | for (i = 0; i < q->nr; i++) { | |
1223 | p = q->queue[i]; | |
96716a19 | 1224 | p->status = 0; /* undecided */ |
bceafe75 | 1225 | if (DIFF_PAIR_UNMERGED(p)) |
e7baa4f4 | 1226 | p->status = DIFF_STATUS_UNMERGED; |
15d061b4 | 1227 | else if (!DIFF_FILE_VALID(p->one)) |
e7baa4f4 | 1228 | p->status = DIFF_STATUS_ADDED; |
2cd68882 | 1229 | else if (!DIFF_FILE_VALID(p->two)) |
e7baa4f4 | 1230 | p->status = DIFF_STATUS_DELETED; |
96716a19 | 1231 | else if (DIFF_PAIR_TYPE_CHANGED(p)) |
e7baa4f4 | 1232 | p->status = DIFF_STATUS_TYPE_CHANGED; |
96716a19 JH |
1233 | |
1234 | /* from this point on, we are dealing with a pair | |
1235 | * whose both sides are valid and of the same type, i.e. | |
1236 | * either in-place edit or rename/copy edit. | |
1237 | */ | |
01c4e70f | 1238 | else if (DIFF_PAIR_RENAME(p)) { |
15d061b4 | 1239 | if (p->source_stays) { |
e7baa4f4 | 1240 | p->status = DIFF_STATUS_COPIED; |
15d061b4 JH |
1241 | continue; |
1242 | } | |
1243 | /* See if there is some other filepair that | |
1244 | * copies from the same source as us. If so | |
6bac10d8 JH |
1245 | * we are a copy. Otherwise we are either a |
1246 | * copy if the path stays, or a rename if it | |
1247 | * does not, but we already handled "stays" case. | |
25d5ea41 | 1248 | */ |
15d061b4 | 1249 | for (j = i + 1; j < q->nr; j++) { |
25d5ea41 JH |
1250 | pp = q->queue[j]; |
1251 | if (strcmp(pp->one->path, p->one->path)) | |
15d061b4 | 1252 | continue; /* not us */ |
01c4e70f | 1253 | if (!DIFF_PAIR_RENAME(pp)) |
15d061b4 JH |
1254 | continue; /* not a rename/copy */ |
1255 | /* pp is a rename/copy from the same source */ | |
e7baa4f4 | 1256 | p->status = DIFF_STATUS_COPIED; |
15d061b4 | 1257 | break; |
25d5ea41 JH |
1258 | } |
1259 | if (!p->status) | |
e7baa4f4 | 1260 | p->status = DIFF_STATUS_RENAMED; |
bceafe75 | 1261 | } |
9fdade06 JH |
1262 | else if (memcmp(p->one->sha1, p->two->sha1, 20) || |
1263 | p->one->mode != p->two->mode) | |
e7baa4f4 | 1264 | p->status = DIFF_STATUS_MODIFIED; |
67574c40 JH |
1265 | else { |
1266 | /* This is a "no-change" entry and should not | |
1267 | * happen anymore, but prepare for broken callers. | |
15d061b4 | 1268 | */ |
67574c40 JH |
1269 | error("feeding unmodified %s to diffcore", |
1270 | p->one->path); | |
e7baa4f4 | 1271 | p->status = DIFF_STATUS_UNKNOWN; |
67574c40 | 1272 | } |
6b14d7fa | 1273 | } |
25d5ea41 | 1274 | diff_debug_queue("resolve-rename-copy done", q); |
38c6f780 JH |
1275 | } |
1276 | ||
86ff1d20 JH |
1277 | static void flush_one_pair(struct diff_filepair *p, |
1278 | int diff_output_format, | |
1279 | struct diff_options *options) | |
38c6f780 | 1280 | { |
bceafe75 | 1281 | int inter_name_termination = '\t'; |
6b5ee137 | 1282 | int line_termination = options->line_termination; |
e68b6f15 LT |
1283 | if (!line_termination) |
1284 | inter_name_termination = 0; | |
bceafe75 | 1285 | |
86ff1d20 JH |
1286 | switch (p->status) { |
1287 | case DIFF_STATUS_UNKNOWN: | |
1288 | break; | |
1289 | case 0: | |
1290 | die("internal error in diff-resolve-rename-copy"); | |
1291 | break; | |
1292 | default: | |
1293 | switch (diff_output_format) { | |
1294 | case DIFF_FORMAT_PATCH: | |
1295 | diff_flush_patch(p, options); | |
bceafe75 | 1296 | break; |
86ff1d20 JH |
1297 | case DIFF_FORMAT_RAW: |
1298 | case DIFF_FORMAT_NAME_STATUS: | |
1299 | diff_flush_raw(p, line_termination, | |
1300 | inter_name_termination, | |
1301 | options, diff_output_format); | |
52f28529 | 1302 | break; |
86ff1d20 JH |
1303 | case DIFF_FORMAT_NAME: |
1304 | diff_flush_name(p, | |
1305 | inter_name_termination, | |
1306 | line_termination); | |
1307 | break; | |
1308 | case DIFF_FORMAT_NO_OUTPUT: | |
1309 | break; | |
1310 | } | |
1311 | } | |
1312 | } | |
1313 | ||
1314 | void diff_flush(struct diff_options *options) | |
1315 | { | |
1316 | struct diff_queue_struct *q = &diff_queued_diff; | |
1317 | int i; | |
1318 | int diff_output_format = options->output_format; | |
1319 | ||
1320 | if (options->with_raw) { | |
1321 | for (i = 0; i < q->nr; i++) { | |
1322 | struct diff_filepair *p = q->queue[i]; | |
1323 | flush_one_pair(p, DIFF_FORMAT_RAW, options); | |
bceafe75 | 1324 | } |
86ff1d20 JH |
1325 | } |
1326 | for (i = 0; i < q->nr; i++) { | |
1327 | struct diff_filepair *p = q->queue[i]; | |
1328 | flush_one_pair(p, diff_output_format, options); | |
7d6c4471 | 1329 | diff_free_filepair(p); |
90a734dc | 1330 | } |
427dcb4b JH |
1331 | free(q->queue); |
1332 | q->queue = NULL; | |
1333 | q->nr = q->alloc = 0; | |
5c97558c JH |
1334 | } |
1335 | ||
f2ce9fde JH |
1336 | static void diffcore_apply_filter(const char *filter) |
1337 | { | |
1338 | int i; | |
1339 | struct diff_queue_struct *q = &diff_queued_diff; | |
1340 | struct diff_queue_struct outq; | |
1341 | outq.queue = NULL; | |
1342 | outq.nr = outq.alloc = 0; | |
1343 | ||
1344 | if (!filter) | |
1345 | return; | |
1346 | ||
e7baa4f4 | 1347 | if (strchr(filter, DIFF_STATUS_FILTER_AON)) { |
f2ce9fde JH |
1348 | int found; |
1349 | for (i = found = 0; !found && i < q->nr; i++) { | |
1350 | struct diff_filepair *p = q->queue[i]; | |
e7baa4f4 JH |
1351 | if (((p->status == DIFF_STATUS_MODIFIED) && |
1352 | ((p->score && | |
1353 | strchr(filter, DIFF_STATUS_FILTER_BROKEN)) || | |
1354 | (!p->score && | |
1355 | strchr(filter, DIFF_STATUS_MODIFIED)))) || | |
1356 | ((p->status != DIFF_STATUS_MODIFIED) && | |
1357 | strchr(filter, p->status))) | |
f2ce9fde JH |
1358 | found++; |
1359 | } | |
1360 | if (found) | |
1361 | return; | |
1362 | ||
1363 | /* otherwise we will clear the whole queue | |
1364 | * by copying the empty outq at the end of this | |
1365 | * function, but first clear the current entries | |
1366 | * in the queue. | |
1367 | */ | |
1368 | for (i = 0; i < q->nr; i++) | |
1369 | diff_free_filepair(q->queue[i]); | |
1370 | } | |
1371 | else { | |
1372 | /* Only the matching ones */ | |
1373 | for (i = 0; i < q->nr; i++) { | |
1374 | struct diff_filepair *p = q->queue[i]; | |
e7baa4f4 JH |
1375 | |
1376 | if (((p->status == DIFF_STATUS_MODIFIED) && | |
1377 | ((p->score && | |
1378 | strchr(filter, DIFF_STATUS_FILTER_BROKEN)) || | |
1379 | (!p->score && | |
1380 | strchr(filter, DIFF_STATUS_MODIFIED)))) || | |
1381 | ((p->status != DIFF_STATUS_MODIFIED) && | |
1382 | strchr(filter, p->status))) | |
f2ce9fde JH |
1383 | diff_q(&outq, p); |
1384 | else | |
1385 | diff_free_filepair(p); | |
1386 | } | |
1387 | } | |
1388 | free(q->queue); | |
1389 | *q = outq; | |
1390 | } | |
1391 | ||
6b5ee137 | 1392 | void diffcore_std(struct diff_options *options) |
befe8639 | 1393 | { |
6b5ee137 JH |
1394 | if (options->break_opt != -1) |
1395 | diffcore_break(options->break_opt); | |
1396 | if (options->detect_rename) | |
8082d8d3 | 1397 | diffcore_rename(options); |
6b5ee137 | 1398 | if (options->break_opt != -1) |
eeaa4603 | 1399 | diffcore_merge_broken(); |
6b5ee137 JH |
1400 | if (options->pickaxe) |
1401 | diffcore_pickaxe(options->pickaxe, options->pickaxe_opts); | |
1402 | if (options->orderfile) | |
1403 | diffcore_order(options->orderfile); | |
f2ce9fde | 1404 | diff_resolve_rename_copy(); |
6b5ee137 | 1405 | diffcore_apply_filter(options->filter); |
f2ce9fde JH |
1406 | } |
1407 | ||
1408 | ||
6b5ee137 | 1409 | void diffcore_std_no_resolve(struct diff_options *options) |
f2ce9fde | 1410 | { |
6b5ee137 JH |
1411 | if (options->pickaxe) |
1412 | diffcore_pickaxe(options->pickaxe, options->pickaxe_opts); | |
1413 | if (options->orderfile) | |
1414 | diffcore_order(options->orderfile); | |
1415 | diffcore_apply_filter(options->filter); | |
befe8639 JH |
1416 | } |
1417 | ||
6b5ee137 JH |
1418 | void diff_addremove(struct diff_options *options, |
1419 | int addremove, unsigned mode, | |
77eb2720 JH |
1420 | const unsigned char *sha1, |
1421 | const char *base, const char *path) | |
be3cfa85 | 1422 | { |
77eb2720 | 1423 | char concatpath[PATH_MAX]; |
427dcb4b JH |
1424 | struct diff_filespec *one, *two; |
1425 | ||
1426 | /* This may look odd, but it is a preparation for | |
1427 | * feeding "there are unchanged files which should | |
1428 | * not produce diffs, but when you are doing copy | |
1429 | * detection you would need them, so here they are" | |
1430 | * entries to the diff-core. They will be prefixed | |
1431 | * with something like '=' or '*' (I haven't decided | |
1432 | * which but should not make any difference). | |
f7c1512a | 1433 | * Feeding the same new and old to diff_change() |
bceafe75 JH |
1434 | * also has the same effect. |
1435 | * Before the final output happens, they are pruned after | |
1436 | * merged into rename/copy pairs as appropriate. | |
427dcb4b | 1437 | */ |
6b5ee137 | 1438 | if (options->reverse_diff) |
427dcb4b JH |
1439 | addremove = (addremove == '+' ? '-' : |
1440 | addremove == '-' ? '+' : addremove); | |
7ca45252 | 1441 | |
427dcb4b JH |
1442 | if (!path) path = ""; |
1443 | sprintf(concatpath, "%s%s", base, path); | |
1444 | one = alloc_filespec(concatpath); | |
1445 | two = alloc_filespec(concatpath); | |
be3cfa85 | 1446 | |
427dcb4b JH |
1447 | if (addremove != '+') |
1448 | fill_filespec(one, sha1, mode); | |
1449 | if (addremove != '-') | |
1450 | fill_filespec(two, sha1, mode); | |
5c97558c | 1451 | |
38c6f780 | 1452 | diff_queue(&diff_queued_diff, one, two); |
be3cfa85 JH |
1453 | } |
1454 | ||
6b5ee137 JH |
1455 | void diff_change(struct diff_options *options, |
1456 | unsigned old_mode, unsigned new_mode, | |
77eb2720 JH |
1457 | const unsigned char *old_sha1, |
1458 | const unsigned char *new_sha1, | |
81e50eab JH |
1459 | const char *base, const char *path) |
1460 | { | |
77eb2720 | 1461 | char concatpath[PATH_MAX]; |
427dcb4b | 1462 | struct diff_filespec *one, *two; |
77eb2720 | 1463 | |
6b5ee137 | 1464 | if (options->reverse_diff) { |
7ca45252 JH |
1465 | unsigned tmp; |
1466 | const unsigned char *tmp_c; | |
1467 | tmp = old_mode; old_mode = new_mode; new_mode = tmp; | |
1468 | tmp_c = old_sha1; old_sha1 = new_sha1; new_sha1 = tmp_c; | |
1469 | } | |
427dcb4b JH |
1470 | if (!path) path = ""; |
1471 | sprintf(concatpath, "%s%s", base, path); | |
1472 | one = alloc_filespec(concatpath); | |
1473 | two = alloc_filespec(concatpath); | |
1474 | fill_filespec(one, old_sha1, old_mode); | |
1475 | fill_filespec(two, new_sha1, new_mode); | |
1476 | ||
38c6f780 | 1477 | diff_queue(&diff_queued_diff, one, two); |
77eb2720 | 1478 | } |
be3cfa85 | 1479 | |
6b5ee137 JH |
1480 | void diff_unmerge(struct diff_options *options, |
1481 | const char *path) | |
77eb2720 | 1482 | { |
6b14d7fa JH |
1483 | struct diff_filespec *one, *two; |
1484 | one = alloc_filespec(path); | |
1485 | two = alloc_filespec(path); | |
1486 | diff_queue(&diff_queued_diff, one, two); | |
86436c28 | 1487 | } |