]>
Commit | Line | Data |
---|---|---|
6973dcae JH |
1 | /* |
2 | * Copyright (C) 2005 Junio C Hamano | |
3 | */ | |
6973dcae JH |
4 | #include "cache.h" |
5 | #include "quote.h" | |
6 | #include "diff.h" | |
7 | #include "diffcore.h" | |
051308f6 | 8 | #include "delta.h" |
6973dcae | 9 | #include "xdiff-interface.h" |
7c92fe0e | 10 | #include "color.h" |
8c701249 | 11 | #include "attr.h" |
d5535ec7 | 12 | #include "run-command.h" |
23707811 | 13 | #include "utf8.h" |
be58e70d | 14 | #include "userdiff.h" |
4a16d072 | 15 | #include "sigchain.h" |
752c0c24 | 16 | #include "submodule.h" |
a757c646 | 17 | #include "ll-merge.h" |
6973dcae | 18 | |
1510fea7 SP |
19 | #ifdef NO_FAST_WORKING_DIRECTORY |
20 | #define FAST_WORKING_DIRECTORY 0 | |
21 | #else | |
22 | #define FAST_WORKING_DIRECTORY 1 | |
23 | #endif | |
24 | ||
96f1e58f | 25 | static int diff_detect_rename_default; |
50705915 | 26 | static int diff_rename_limit_default = 200; |
a624eaa7 | 27 | static int diff_suppress_blank_empty; |
6b2f2d98 | 28 | int diff_use_color_default = -1; |
98a4d87b | 29 | static const char *diff_word_regex_cfg; |
cbe02100 | 30 | static const char *external_diff_cmd_cfg; |
aecbf914 | 31 | int diff_auto_refresh_index = 1; |
a5a818ee | 32 | static int diff_mnemonic_prefix; |
6973dcae | 33 | |
7c92fe0e | 34 | static char diff_colors[][COLOR_MAXLEN] = { |
dc6ebd4c AL |
35 | GIT_COLOR_RESET, |
36 | GIT_COLOR_NORMAL, /* PLAIN */ | |
37 | GIT_COLOR_BOLD, /* METAINFO */ | |
38 | GIT_COLOR_CYAN, /* FRAGINFO */ | |
39 | GIT_COLOR_RED, /* OLD */ | |
40 | GIT_COLOR_GREEN, /* NEW */ | |
41 | GIT_COLOR_YELLOW, /* COMMIT */ | |
42 | GIT_COLOR_BG_RED, /* WHITESPACE */ | |
89cb73a1 | 43 | GIT_COLOR_NORMAL, /* FUNCINFO */ |
cd112cef JS |
44 | }; |
45 | ||
9cb92c39 JK |
46 | static void diff_filespec_load_driver(struct diff_filespec *one); |
47 | static char *run_textconv(const char *, struct diff_filespec *, size_t *); | |
48 | ||
801235c5 JH |
49 | static int parse_diff_color_slot(const char *var, int ofs) |
50 | { | |
51 | if (!strcasecmp(var+ofs, "plain")) | |
52 | return DIFF_PLAIN; | |
53 | if (!strcasecmp(var+ofs, "meta")) | |
54 | return DIFF_METAINFO; | |
55 | if (!strcasecmp(var+ofs, "frag")) | |
56 | return DIFF_FRAGINFO; | |
57 | if (!strcasecmp(var+ofs, "old")) | |
58 | return DIFF_FILE_OLD; | |
59 | if (!strcasecmp(var+ofs, "new")) | |
60 | return DIFF_FILE_NEW; | |
ce436973 JK |
61 | if (!strcasecmp(var+ofs, "commit")) |
62 | return DIFF_COMMIT; | |
448c3ef1 JH |
63 | if (!strcasecmp(var+ofs, "whitespace")) |
64 | return DIFF_WHITESPACE; | |
89cb73a1 BW |
65 | if (!strcasecmp(var+ofs, "func")) |
66 | return DIFF_FUNCINFO; | |
8b8e8624 | 67 | return -1; |
801235c5 JH |
68 | } |
69 | ||
cced5fbc LT |
70 | static int git_config_rename(const char *var, const char *value) |
71 | { | |
72 | if (!value) | |
73 | return DIFF_DETECT_RENAME; | |
74 | if (!strcasecmp(value, "copies") || !strcasecmp(value, "copy")) | |
75 | return DIFF_DETECT_COPY; | |
76 | return git_config_bool(var,value) ? DIFF_DETECT_RENAME : 0; | |
77 | } | |
78 | ||
83ad63cf JH |
79 | /* |
80 | * These are to give UI layer defaults. | |
81 | * The core-level commands such as git-diff-files should | |
82 | * never be affected by the setting of diff.renames | |
83 | * the user happens to have in the configuration file. | |
84 | */ | |
ef90d6d4 | 85 | int git_diff_ui_config(const char *var, const char *value, void *cb) |
801235c5 | 86 | { |
a159ca0c | 87 | if (!strcmp(var, "diff.color") || !strcmp(var, "color.diff")) { |
0f6f5a40 | 88 | diff_use_color_default = git_config_colorbool(var, value, -1); |
801235c5 JH |
89 | return 0; |
90 | } | |
b68ea12e | 91 | if (!strcmp(var, "diff.renames")) { |
cced5fbc | 92 | diff_detect_rename_default = git_config_rename(var, value); |
b68ea12e EW |
93 | return 0; |
94 | } | |
aecbf914 JH |
95 | if (!strcmp(var, "diff.autorefreshindex")) { |
96 | diff_auto_refresh_index = git_config_bool(var, value); | |
97 | return 0; | |
98 | } | |
a5a818ee JH |
99 | if (!strcmp(var, "diff.mnemonicprefix")) { |
100 | diff_mnemonic_prefix = git_config_bool(var, value); | |
101 | return 0; | |
102 | } | |
daec808c BH |
103 | if (!strcmp(var, "diff.external")) |
104 | return git_config_string(&external_diff_cmd_cfg, var, value); | |
98a4d87b BSSJ |
105 | if (!strcmp(var, "diff.wordregex")) |
106 | return git_config_string(&diff_word_regex_cfg, var, value); | |
f1af60bd | 107 | |
ef90d6d4 | 108 | return git_diff_basic_config(var, value, cb); |
9a1805a8 JK |
109 | } |
110 | ||
ef90d6d4 | 111 | int git_diff_basic_config(const char *var, const char *value, void *cb) |
9a1805a8 | 112 | { |
2b6ca6df LT |
113 | if (!strcmp(var, "diff.renamelimit")) { |
114 | diff_rename_limit_default = git_config_int(var, value); | |
115 | return 0; | |
116 | } | |
117 | ||
c7534ef4 JK |
118 | switch (userdiff_config(var, value)) { |
119 | case 0: break; | |
120 | case -1: return -1; | |
121 | default: return 0; | |
122 | } | |
123 | ||
1968d77d | 124 | if (!prefixcmp(var, "diff.color.") || !prefixcmp(var, "color.diff.")) { |
801235c5 | 125 | int slot = parse_diff_color_slot(var, 11); |
8b8e8624 JK |
126 | if (slot < 0) |
127 | return 0; | |
64f30e94 JH |
128 | if (!value) |
129 | return config_error_nonbool(var); | |
7c92fe0e | 130 | color_parse(value, var, diff_colors[slot]); |
801235c5 JH |
131 | return 0; |
132 | } | |
f1af60bd | 133 | |
a624eaa7 | 134 | /* like GNU diff's --suppress-blank-empty option */ |
950db879 JS |
135 | if (!strcmp(var, "diff.suppressblankempty") || |
136 | /* for backwards compatibility */ | |
137 | !strcmp(var, "diff.suppress-blank-empty")) { | |
a624eaa7 JM |
138 | diff_suppress_blank_empty = git_config_bool(var, value); |
139 | return 0; | |
140 | } | |
141 | ||
ef90d6d4 | 142 | return git_color_default_config(var, value, cb); |
801235c5 JH |
143 | } |
144 | ||
6973dcae JH |
145 | static char *quote_two(const char *one, const char *two) |
146 | { | |
147 | int need_one = quote_c_style(one, NULL, NULL, 1); | |
148 | int need_two = quote_c_style(two, NULL, NULL, 1); | |
f285a2d7 | 149 | struct strbuf res = STRBUF_INIT; |
6973dcae JH |
150 | |
151 | if (need_one + need_two) { | |
663af342 PH |
152 | strbuf_addch(&res, '"'); |
153 | quote_c_style(one, &res, NULL, 1); | |
154 | quote_c_style(two, &res, NULL, 1); | |
155 | strbuf_addch(&res, '"'); | |
156 | } else { | |
157 | strbuf_addstr(&res, one); | |
158 | strbuf_addstr(&res, two); | |
6973dcae | 159 | } |
b315c5c0 | 160 | return strbuf_detach(&res, NULL); |
6973dcae JH |
161 | } |
162 | ||
163 | static const char *external_diff(void) | |
164 | { | |
165 | static const char *external_diff_cmd = NULL; | |
166 | static int done_preparing = 0; | |
167 | ||
168 | if (done_preparing) | |
169 | return external_diff_cmd; | |
170 | external_diff_cmd = getenv("GIT_EXTERNAL_DIFF"); | |
cbe02100 JS |
171 | if (!external_diff_cmd) |
172 | external_diff_cmd = external_diff_cmd_cfg; | |
6973dcae JH |
173 | done_preparing = 1; |
174 | return external_diff_cmd; | |
175 | } | |
176 | ||
6973dcae JH |
177 | static struct diff_tempfile { |
178 | const char *name; /* filename external diff should read from */ | |
179 | char hex[41]; | |
180 | char mode[10]; | |
1472966c | 181 | char tmp_path[PATH_MAX]; |
6973dcae JH |
182 | } diff_temp[2]; |
183 | ||
6957eb9a JH |
184 | typedef unsigned long (*sane_truncate_fn)(char *line, unsigned long len); |
185 | ||
186 | struct emit_callback { | |
6957eb9a JH |
187 | int color_diff; |
188 | unsigned ws_rule; | |
189 | int blank_at_eof_in_preimage; | |
190 | int blank_at_eof_in_postimage; | |
191 | int lno_in_preimage; | |
192 | int lno_in_postimage; | |
193 | sane_truncate_fn truncate; | |
194 | const char **label_path; | |
195 | struct diff_words_data *diff_words; | |
196 | int *found_changesp; | |
197 | FILE *file; | |
3e97c7c6 | 198 | struct strbuf *header; |
6957eb9a JH |
199 | }; |
200 | ||
6973dcae JH |
201 | static int count_lines(const char *data, int size) |
202 | { | |
203 | int count, ch, completely_empty = 1, nl_just_seen = 0; | |
204 | count = 0; | |
205 | while (0 < size--) { | |
206 | ch = *data++; | |
207 | if (ch == '\n') { | |
208 | count++; | |
209 | nl_just_seen = 1; | |
210 | completely_empty = 0; | |
211 | } | |
212 | else { | |
213 | nl_just_seen = 0; | |
214 | completely_empty = 0; | |
215 | } | |
216 | } | |
217 | if (completely_empty) | |
218 | return 0; | |
219 | if (!nl_just_seen) | |
220 | count++; /* no trailing newline */ | |
221 | return count; | |
222 | } | |
223 | ||
6957eb9a JH |
224 | static int fill_mmfile(mmfile_t *mf, struct diff_filespec *one) |
225 | { | |
226 | if (!DIFF_FILE_VALID(one)) { | |
227 | mf->ptr = (char *)""; /* does not matter */ | |
228 | mf->size = 0; | |
229 | return 0; | |
230 | } | |
231 | else if (diff_populate_filespec(one, 0)) | |
232 | return -1; | |
bb35fefb | 233 | |
6957eb9a JH |
234 | mf->ptr = one->data; |
235 | mf->size = one->size; | |
236 | return 0; | |
237 | } | |
238 | ||
239 | static int count_trailing_blank(mmfile_t *mf, unsigned ws_rule) | |
240 | { | |
241 | char *ptr = mf->ptr; | |
242 | long size = mf->size; | |
243 | int cnt = 0; | |
244 | ||
245 | if (!size) | |
246 | return cnt; | |
247 | ptr += size - 1; /* pointing at the very end */ | |
248 | if (*ptr != '\n') | |
249 | ; /* incomplete line */ | |
250 | else | |
251 | ptr--; /* skip the last LF */ | |
252 | while (mf->ptr < ptr) { | |
253 | char *prev_eol; | |
254 | for (prev_eol = ptr; mf->ptr <= prev_eol; prev_eol--) | |
255 | if (*prev_eol == '\n') | |
256 | break; | |
257 | if (!ws_blank_line(prev_eol + 1, ptr - prev_eol, ws_rule)) | |
258 | break; | |
259 | cnt++; | |
260 | ptr = prev_eol - 1; | |
261 | } | |
262 | return cnt; | |
263 | } | |
264 | ||
265 | static void check_blank_at_eof(mmfile_t *mf1, mmfile_t *mf2, | |
266 | struct emit_callback *ecbdata) | |
267 | { | |
268 | int l1, l2, at; | |
269 | unsigned ws_rule = ecbdata->ws_rule; | |
270 | l1 = count_trailing_blank(mf1, ws_rule); | |
271 | l2 = count_trailing_blank(mf2, ws_rule); | |
272 | if (l2 <= l1) { | |
273 | ecbdata->blank_at_eof_in_preimage = 0; | |
274 | ecbdata->blank_at_eof_in_postimage = 0; | |
275 | return; | |
276 | } | |
277 | at = count_lines(mf1->ptr, mf1->size); | |
278 | ecbdata->blank_at_eof_in_preimage = (at - l1) + 1; | |
279 | ||
280 | at = count_lines(mf2->ptr, mf2->size); | |
281 | ecbdata->blank_at_eof_in_postimage = (at - l2) + 1; | |
282 | } | |
283 | ||
250f7993 JH |
284 | static void emit_line_0(FILE *file, const char *set, const char *reset, |
285 | int first, const char *line, int len) | |
6957eb9a JH |
286 | { |
287 | int has_trailing_newline, has_trailing_carriage_return; | |
250f7993 | 288 | int nofirst; |
6957eb9a | 289 | |
250f7993 JH |
290 | if (len == 0) { |
291 | has_trailing_newline = (first == '\n'); | |
292 | has_trailing_carriage_return = (!has_trailing_newline && | |
293 | (first == '\r')); | |
294 | nofirst = has_trailing_newline || has_trailing_carriage_return; | |
295 | } else { | |
296 | has_trailing_newline = (len > 0 && line[len-1] == '\n'); | |
297 | if (has_trailing_newline) | |
298 | len--; | |
299 | has_trailing_carriage_return = (len > 0 && line[len-1] == '\r'); | |
300 | if (has_trailing_carriage_return) | |
301 | len--; | |
302 | nofirst = 0; | |
303 | } | |
6957eb9a | 304 | |
06a47552 JH |
305 | if (len || !nofirst) { |
306 | fputs(set, file); | |
307 | if (!nofirst) | |
308 | fputc(first, file); | |
309 | fwrite(line, len, 1, file); | |
310 | fputs(reset, file); | |
311 | } | |
6957eb9a JH |
312 | if (has_trailing_carriage_return) |
313 | fputc('\r', file); | |
314 | if (has_trailing_newline) | |
315 | fputc('\n', file); | |
316 | } | |
317 | ||
250f7993 JH |
318 | static void emit_line(FILE *file, const char *set, const char *reset, |
319 | const char *line, int len) | |
320 | { | |
321 | emit_line_0(file, set, reset, line[0], line+1, len-1); | |
322 | } | |
323 | ||
6957eb9a JH |
324 | static int new_blank_line_at_eof(struct emit_callback *ecbdata, const char *line, int len) |
325 | { | |
326 | if (!((ecbdata->ws_rule & WS_BLANK_AT_EOF) && | |
327 | ecbdata->blank_at_eof_in_preimage && | |
328 | ecbdata->blank_at_eof_in_postimage && | |
329 | ecbdata->blank_at_eof_in_preimage <= ecbdata->lno_in_preimage && | |
330 | ecbdata->blank_at_eof_in_postimage <= ecbdata->lno_in_postimage)) | |
331 | return 0; | |
018cff70 | 332 | return ws_blank_line(line, len, ecbdata->ws_rule); |
6957eb9a JH |
333 | } |
334 | ||
018cff70 JH |
335 | static void emit_add_line(const char *reset, |
336 | struct emit_callback *ecbdata, | |
337 | const char *line, int len) | |
6957eb9a JH |
338 | { |
339 | const char *ws = diff_get_color(ecbdata->color_diff, DIFF_WHITESPACE); | |
340 | const char *set = diff_get_color(ecbdata->color_diff, DIFF_FILE_NEW); | |
341 | ||
342 | if (!*ws) | |
018cff70 | 343 | emit_line_0(ecbdata->file, set, reset, '+', line, len); |
6957eb9a JH |
344 | else if (new_blank_line_at_eof(ecbdata, line, len)) |
345 | /* Blank line at EOF - paint '+' as well */ | |
018cff70 | 346 | emit_line_0(ecbdata->file, ws, reset, '+', line, len); |
6957eb9a JH |
347 | else { |
348 | /* Emit just the prefix, then the rest. */ | |
018cff70 JH |
349 | emit_line_0(ecbdata->file, set, reset, '+', "", 0); |
350 | ws_check_emit(line, len, ecbdata->ws_rule, | |
6957eb9a JH |
351 | ecbdata->file, set, reset, ws); |
352 | } | |
353 | } | |
354 | ||
89cb73a1 BW |
355 | static void emit_hunk_header(struct emit_callback *ecbdata, |
356 | const char *line, int len) | |
357 | { | |
358 | const char *plain = diff_get_color(ecbdata->color_diff, DIFF_PLAIN); | |
359 | const char *frag = diff_get_color(ecbdata->color_diff, DIFF_FRAGINFO); | |
360 | const char *func = diff_get_color(ecbdata->color_diff, DIFF_FUNCINFO); | |
361 | const char *reset = diff_get_color(ecbdata->color_diff, DIFF_RESET); | |
362 | static const char atat[2] = { '@', '@' }; | |
363 | const char *cp, *ep; | |
364 | ||
365 | /* | |
366 | * As a hunk header must begin with "@@ -<old>, +<new> @@", | |
367 | * it always is at least 10 bytes long. | |
368 | */ | |
369 | if (len < 10 || | |
370 | memcmp(line, atat, 2) || | |
371 | !(ep = memmem(line + 2, len - 2, atat, 2))) { | |
372 | emit_line(ecbdata->file, plain, reset, line, len); | |
373 | return; | |
374 | } | |
375 | ep += 2; /* skip over @@ */ | |
376 | ||
377 | /* The hunk header in fraginfo color */ | |
378 | emit_line(ecbdata->file, frag, reset, line, ep - line); | |
379 | ||
380 | /* blank before the func header */ | |
381 | for (cp = ep; ep - line < len; ep++) | |
382 | if (*ep != ' ' && *ep != '\t') | |
383 | break; | |
384 | if (ep != cp) | |
385 | emit_line(ecbdata->file, plain, reset, cp, ep - cp); | |
386 | ||
387 | if (ep < line + len) | |
388 | emit_line(ecbdata->file, func, reset, ep, line + len - ep); | |
389 | } | |
390 | ||
479b0ae8 JK |
391 | static struct diff_tempfile *claim_diff_tempfile(void) { |
392 | int i; | |
393 | for (i = 0; i < ARRAY_SIZE(diff_temp); i++) | |
394 | if (!diff_temp[i].name) | |
395 | return diff_temp + i; | |
396 | die("BUG: diff is failing to clean up its tempfiles"); | |
397 | } | |
398 | ||
399 | static int remove_tempfile_installed; | |
400 | ||
401 | static void remove_tempfile(void) | |
402 | { | |
403 | int i; | |
a8344abe NR |
404 | for (i = 0; i < ARRAY_SIZE(diff_temp); i++) { |
405 | if (diff_temp[i].name == diff_temp[i].tmp_path) | |
691f1a28 | 406 | unlink_or_warn(diff_temp[i].name); |
a8344abe NR |
407 | diff_temp[i].name = NULL; |
408 | } | |
479b0ae8 JK |
409 | } |
410 | ||
411 | static void remove_tempfile_on_signal(int signo) | |
412 | { | |
413 | remove_tempfile(); | |
4a16d072 | 414 | sigchain_pop(signo); |
479b0ae8 JK |
415 | raise(signo); |
416 | } | |
417 | ||
c0c77734 | 418 | static void print_line_count(FILE *file, int count) |
6973dcae JH |
419 | { |
420 | switch (count) { | |
421 | case 0: | |
c0c77734 | 422 | fprintf(file, "0,0"); |
6973dcae JH |
423 | break; |
424 | case 1: | |
c0c77734 | 425 | fprintf(file, "1"); |
6973dcae JH |
426 | break; |
427 | default: | |
c0c77734 | 428 | fprintf(file, "1,%d", count); |
6973dcae JH |
429 | break; |
430 | } | |
431 | } | |
432 | ||
7f7ee2ff JH |
433 | static void emit_rewrite_lines(struct emit_callback *ecb, |
434 | int prefix, const char *data, int size) | |
6973dcae | 435 | { |
7f7ee2ff JH |
436 | const char *endp = NULL; |
437 | static const char *nneof = " No newline at end of file\n"; | |
438 | const char *old = diff_get_color(ecb->color_diff, DIFF_FILE_OLD); | |
439 | const char *reset = diff_get_color(ecb->color_diff, DIFF_RESET); | |
440 | ||
441 | while (0 < size) { | |
442 | int len; | |
443 | ||
444 | endp = memchr(data, '\n', size); | |
445 | len = endp ? (endp - data + 1) : size; | |
446 | if (prefix != '+') { | |
447 | ecb->lno_in_preimage++; | |
448 | emit_line_0(ecb->file, old, reset, '-', | |
449 | data, len); | |
450 | } else { | |
451 | ecb->lno_in_postimage++; | |
452 | emit_add_line(reset, ecb, data, len); | |
13e36ec5 | 453 | } |
7f7ee2ff JH |
454 | size -= len; |
455 | data += len; | |
456 | } | |
457 | if (!endp) { | |
458 | const char *plain = diff_get_color(ecb->color_diff, | |
459 | DIFF_PLAIN); | |
460 | emit_line_0(ecb->file, plain, reset, '\\', | |
461 | nneof, strlen(nneof)); | |
6973dcae | 462 | } |
6973dcae JH |
463 | } |
464 | ||
465 | static void emit_rewrite_diff(const char *name_a, | |
466 | const char *name_b, | |
467 | struct diff_filespec *one, | |
13e36ec5 | 468 | struct diff_filespec *two, |
3aa1f7ca JK |
469 | const char *textconv_one, |
470 | const char *textconv_two, | |
eab9a40b | 471 | struct diff_options *o) |
6973dcae JH |
472 | { |
473 | int lc_a, lc_b; | |
eab9a40b | 474 | int color_diff = DIFF_OPT_TST(o, COLOR_DIFF); |
1a9eb3b9 | 475 | const char *name_a_tab, *name_b_tab; |
13e36ec5 JS |
476 | const char *metainfo = diff_get_color(color_diff, DIFF_METAINFO); |
477 | const char *fraginfo = diff_get_color(color_diff, DIFF_FRAGINFO); | |
13e36ec5 | 478 | const char *reset = diff_get_color(color_diff, DIFF_RESET); |
d5625091 | 479 | static struct strbuf a_name = STRBUF_INIT, b_name = STRBUF_INIT; |
a5a818ee | 480 | const char *a_prefix, *b_prefix; |
3aa1f7ca JK |
481 | const char *data_one, *data_two; |
482 | size_t size_one, size_two; | |
7f7ee2ff | 483 | struct emit_callback ecbdata; |
a5a818ee JH |
484 | |
485 | if (diff_mnemonic_prefix && DIFF_OPT_TST(o, REVERSE_DIFF)) { | |
486 | a_prefix = o->b_prefix; | |
487 | b_prefix = o->a_prefix; | |
488 | } else { | |
489 | a_prefix = o->a_prefix; | |
490 | b_prefix = o->b_prefix; | |
491 | } | |
1a9eb3b9 | 492 | |
8a13becc JH |
493 | name_a += (*name_a == '/'); |
494 | name_b += (*name_b == '/'); | |
1a9eb3b9 JH |
495 | name_a_tab = strchr(name_a, ' ') ? "\t" : ""; |
496 | name_b_tab = strchr(name_b, ' ') ? "\t" : ""; | |
497 | ||
d5625091 JH |
498 | strbuf_reset(&a_name); |
499 | strbuf_reset(&b_name); | |
a5a818ee JH |
500 | quote_two_c_style(&a_name, a_prefix, name_a, 0); |
501 | quote_two_c_style(&b_name, b_prefix, name_b, 0); | |
d5625091 | 502 | |
6973dcae JH |
503 | diff_populate_filespec(one, 0); |
504 | diff_populate_filespec(two, 0); | |
3aa1f7ca JK |
505 | if (textconv_one) { |
506 | data_one = run_textconv(textconv_one, one, &size_one); | |
507 | if (!data_one) | |
508 | die("unable to read files to diff"); | |
509 | } | |
510 | else { | |
511 | data_one = one->data; | |
512 | size_one = one->size; | |
513 | } | |
514 | if (textconv_two) { | |
515 | data_two = run_textconv(textconv_two, two, &size_two); | |
516 | if (!data_two) | |
517 | die("unable to read files to diff"); | |
518 | } | |
519 | else { | |
520 | data_two = two->data; | |
521 | size_two = two->size; | |
522 | } | |
523 | ||
d91ba8fa JH |
524 | memset(&ecbdata, 0, sizeof(ecbdata)); |
525 | ecbdata.color_diff = color_diff; | |
526 | ecbdata.found_changesp = &o->found_changes; | |
527 | ecbdata.ws_rule = whitespace_rule(name_b ? name_b : name_a); | |
528 | ecbdata.file = o->file; | |
529 | if (ecbdata.ws_rule & WS_BLANK_AT_EOF) { | |
530 | mmfile_t mf1, mf2; | |
531 | mf1.ptr = (char *)data_one; | |
532 | mf2.ptr = (char *)data_two; | |
533 | mf1.size = size_one; | |
534 | mf2.size = size_two; | |
535 | check_blank_at_eof(&mf1, &mf2, &ecbdata); | |
536 | } | |
537 | ecbdata.lno_in_preimage = 1; | |
538 | ecbdata.lno_in_postimage = 1; | |
539 | ||
3aa1f7ca JK |
540 | lc_a = count_lines(data_one, size_one); |
541 | lc_b = count_lines(data_two, size_two); | |
c0c77734 DB |
542 | fprintf(o->file, |
543 | "%s--- %s%s%s\n%s+++ %s%s%s\n%s@@ -", | |
544 | metainfo, a_name.buf, name_a_tab, reset, | |
545 | metainfo, b_name.buf, name_b_tab, reset, fraginfo); | |
546 | print_line_count(o->file, lc_a); | |
547 | fprintf(o->file, " +"); | |
548 | print_line_count(o->file, lc_b); | |
549 | fprintf(o->file, " @@%s\n", reset); | |
6973dcae | 550 | if (lc_a) |
d91ba8fa | 551 | emit_rewrite_lines(&ecbdata, '-', data_one, size_one); |
6973dcae | 552 | if (lc_b) |
d91ba8fa | 553 | emit_rewrite_lines(&ecbdata, '+', data_two, size_two); |
6973dcae JH |
554 | } |
555 | ||
f59a59e2 JS |
556 | struct diff_words_buffer { |
557 | mmfile_t text; | |
558 | long alloc; | |
2e5d2003 JS |
559 | struct diff_words_orig { |
560 | const char *begin, *end; | |
561 | } *orig; | |
562 | int orig_nr, orig_alloc; | |
f59a59e2 JS |
563 | }; |
564 | ||
565 | static void diff_words_append(char *line, unsigned long len, | |
566 | struct diff_words_buffer *buffer) | |
567 | { | |
23c1575f | 568 | ALLOC_GROW(buffer->text.ptr, buffer->text.size + len, buffer->alloc); |
f59a59e2 JS |
569 | line++; |
570 | len--; | |
571 | memcpy(buffer->text.ptr + buffer->text.size, line, len); | |
572 | buffer->text.size += len; | |
2b6a5417 | 573 | buffer->text.ptr[buffer->text.size] = '\0'; |
f59a59e2 JS |
574 | } |
575 | ||
576 | struct diff_words_data { | |
f59a59e2 | 577 | struct diff_words_buffer minus, plus; |
2e5d2003 | 578 | const char *current_plus; |
c0c77734 | 579 | FILE *file; |
2b6a5417 | 580 | regex_t *word_regex; |
f59a59e2 JS |
581 | }; |
582 | ||
f59a59e2 | 583 | static void fn_out_diff_words_aux(void *priv, char *line, unsigned long len) |
f59a59e2 | 584 | { |
f59a59e2 | 585 | struct diff_words_data *diff_words = priv; |
2e5d2003 JS |
586 | int minus_first, minus_len, plus_first, plus_len; |
587 | const char *minus_begin, *minus_end, *plus_begin, *plus_end; | |
f59a59e2 | 588 | |
2e5d2003 JS |
589 | if (line[0] != '@' || parse_hunk_header(line, len, |
590 | &minus_first, &minus_len, &plus_first, &plus_len)) | |
f59a59e2 JS |
591 | return; |
592 | ||
2e5d2003 JS |
593 | /* POSIX requires that first be decremented by one if len == 0... */ |
594 | if (minus_len) { | |
595 | minus_begin = diff_words->minus.orig[minus_first].begin; | |
596 | minus_end = | |
597 | diff_words->minus.orig[minus_first + minus_len - 1].end; | |
598 | } else | |
599 | minus_begin = minus_end = | |
600 | diff_words->minus.orig[minus_first].end; | |
601 | ||
602 | if (plus_len) { | |
603 | plus_begin = diff_words->plus.orig[plus_first].begin; | |
604 | plus_end = diff_words->plus.orig[plus_first + plus_len - 1].end; | |
605 | } else | |
606 | plus_begin = plus_end = diff_words->plus.orig[plus_first].end; | |
607 | ||
608 | if (diff_words->current_plus != plus_begin) | |
609 | fwrite(diff_words->current_plus, | |
610 | plus_begin - diff_words->current_plus, 1, | |
611 | diff_words->file); | |
612 | if (minus_begin != minus_end) | |
613 | color_fwrite_lines(diff_words->file, | |
614 | diff_get_color(1, DIFF_FILE_OLD), | |
615 | minus_end - minus_begin, minus_begin); | |
616 | if (plus_begin != plus_end) | |
617 | color_fwrite_lines(diff_words->file, | |
618 | diff_get_color(1, DIFF_FILE_NEW), | |
619 | plus_end - plus_begin, plus_begin); | |
620 | ||
621 | diff_words->current_plus = plus_end; | |
f59a59e2 JS |
622 | } |
623 | ||
2b6a5417 JS |
624 | /* This function starts looking at *begin, and returns 0 iff a word was found. */ |
625 | static int find_word_boundaries(mmfile_t *buffer, regex_t *word_regex, | |
626 | int *begin, int *end) | |
627 | { | |
628 | if (word_regex && *begin < buffer->size) { | |
629 | regmatch_t match[1]; | |
630 | if (!regexec(word_regex, buffer->ptr + *begin, 1, match, 0)) { | |
631 | char *p = memchr(buffer->ptr + *begin + match[0].rm_so, | |
632 | '\n', match[0].rm_eo - match[0].rm_so); | |
633 | *end = p ? p - buffer->ptr : match[0].rm_eo + *begin; | |
634 | *begin += match[0].rm_so; | |
635 | return *begin >= *end; | |
636 | } | |
637 | return -1; | |
f59a59e2 JS |
638 | } |
639 | ||
2b6a5417 JS |
640 | /* find the next word */ |
641 | while (*begin < buffer->size && isspace(buffer->ptr[*begin])) | |
642 | (*begin)++; | |
643 | if (*begin >= buffer->size) | |
644 | return -1; | |
f59a59e2 | 645 | |
2b6a5417 JS |
646 | /* find the end of the word */ |
647 | *end = *begin + 1; | |
648 | while (*end < buffer->size && !isspace(buffer->ptr[*end])) | |
649 | (*end)++; | |
650 | ||
651 | return 0; | |
f59a59e2 JS |
652 | } |
653 | ||
23c1575f | 654 | /* |
2e5d2003 JS |
655 | * This function splits the words in buffer->text, stores the list with |
656 | * newline separator into out, and saves the offsets of the original words | |
657 | * in buffer->orig. | |
23c1575f | 658 | */ |
2b6a5417 JS |
659 | static void diff_words_fill(struct diff_words_buffer *buffer, mmfile_t *out, |
660 | regex_t *word_regex) | |
f59a59e2 | 661 | { |
2e5d2003 | 662 | int i, j; |
2b6a5417 | 663 | long alloc = 0; |
f59a59e2 | 664 | |
2e5d2003 | 665 | out->size = 0; |
2b6a5417 | 666 | out->ptr = NULL; |
f59a59e2 | 667 | |
2e5d2003 JS |
668 | /* fake an empty "0th" word */ |
669 | ALLOC_GROW(buffer->orig, 1, buffer->orig_alloc); | |
670 | buffer->orig[0].begin = buffer->orig[0].end = buffer->text.ptr; | |
671 | buffer->orig_nr = 1; | |
672 | ||
673 | for (i = 0; i < buffer->text.size; i++) { | |
2b6a5417 JS |
674 | if (find_word_boundaries(&buffer->text, word_regex, &i, &j)) |
675 | return; | |
2e5d2003 JS |
676 | |
677 | /* store original boundaries */ | |
678 | ALLOC_GROW(buffer->orig, buffer->orig_nr + 1, | |
679 | buffer->orig_alloc); | |
680 | buffer->orig[buffer->orig_nr].begin = buffer->text.ptr + i; | |
681 | buffer->orig[buffer->orig_nr].end = buffer->text.ptr + j; | |
682 | buffer->orig_nr++; | |
683 | ||
684 | /* store one word */ | |
2b6a5417 | 685 | ALLOC_GROW(out->ptr, out->size + j - i + 1, alloc); |
2e5d2003 JS |
686 | memcpy(out->ptr + out->size, buffer->text.ptr + i, j - i); |
687 | out->ptr[out->size + j - i] = '\n'; | |
688 | out->size += j - i + 1; | |
689 | ||
690 | i = j - 1; | |
f59a59e2 JS |
691 | } |
692 | } | |
693 | ||
694 | /* this executes the word diff on the accumulated buffers */ | |
695 | static void diff_words_show(struct diff_words_data *diff_words) | |
696 | { | |
697 | xpparam_t xpp; | |
698 | xdemitconf_t xecfg; | |
f59a59e2 | 699 | mmfile_t minus, plus; |
f59a59e2 | 700 | |
2e5d2003 JS |
701 | /* special case: only removal */ |
702 | if (!diff_words->plus.text.size) { | |
703 | color_fwrite_lines(diff_words->file, | |
704 | diff_get_color(1, DIFF_FILE_OLD), | |
705 | diff_words->minus.text.size, diff_words->minus.text.ptr); | |
706 | diff_words->minus.text.size = 0; | |
707 | return; | |
708 | } | |
709 | ||
710 | diff_words->current_plus = diff_words->plus.text.ptr; | |
f59a59e2 | 711 | |
9ccd0a88 | 712 | memset(&xpp, 0, sizeof(xpp)); |
30b25010 | 713 | memset(&xecfg, 0, sizeof(xecfg)); |
2b6a5417 JS |
714 | diff_words_fill(&diff_words->minus, &minus, diff_words->word_regex); |
715 | diff_words_fill(&diff_words->plus, &plus, diff_words->word_regex); | |
f59a59e2 | 716 | xpp.flags = XDF_NEED_MINIMAL; |
2b6a5417 | 717 | /* as only the hunk header will be parsed, we need a 0-context */ |
2e5d2003 | 718 | xecfg.ctxlen = 0; |
8a3f524b | 719 | xdi_diff_outf(&minus, &plus, fn_out_diff_words_aux, diff_words, |
dfea7900 | 720 | &xpp, &xecfg); |
f59a59e2 JS |
721 | free(minus.ptr); |
722 | free(plus.ptr); | |
2e5d2003 JS |
723 | if (diff_words->current_plus != diff_words->plus.text.ptr + |
724 | diff_words->plus.text.size) | |
725 | fwrite(diff_words->current_plus, | |
726 | diff_words->plus.text.ptr + diff_words->plus.text.size | |
727 | - diff_words->current_plus, 1, | |
728 | diff_words->file); | |
f59a59e2 | 729 | diff_words->minus.text.size = diff_words->plus.text.size = 0; |
f59a59e2 JS |
730 | } |
731 | ||
76fd2828 JH |
732 | /* In "color-words" mode, show word-diff of words accumulated in the buffer */ |
733 | static void diff_words_flush(struct emit_callback *ecbdata) | |
734 | { | |
735 | if (ecbdata->diff_words->minus.text.size || | |
736 | ecbdata->diff_words->plus.text.size) | |
737 | diff_words_show(ecbdata->diff_words); | |
738 | } | |
739 | ||
f59a59e2 JS |
740 | static void free_diff_words_data(struct emit_callback *ecbdata) |
741 | { | |
742 | if (ecbdata->diff_words) { | |
76fd2828 | 743 | diff_words_flush(ecbdata); |
8e0f7003 | 744 | free (ecbdata->diff_words->minus.text.ptr); |
2e5d2003 | 745 | free (ecbdata->diff_words->minus.orig); |
8e0f7003 | 746 | free (ecbdata->diff_words->plus.text.ptr); |
2e5d2003 | 747 | free (ecbdata->diff_words->plus.orig); |
2b6a5417 | 748 | free(ecbdata->diff_words->word_regex); |
f59a59e2 JS |
749 | free(ecbdata->diff_words); |
750 | ecbdata->diff_words = NULL; | |
751 | } | |
752 | } | |
753 | ||
ce436973 | 754 | const char *diff_get_color(int diff_use_color, enum color_diff ix) |
cd112cef JS |
755 | { |
756 | if (diff_use_color) | |
50f575fc LT |
757 | return diff_colors[ix]; |
758 | return ""; | |
cd112cef JS |
759 | } |
760 | ||
23707811 JH |
761 | static unsigned long sane_truncate_line(struct emit_callback *ecb, char *line, unsigned long len) |
762 | { | |
763 | const char *cp; | |
764 | unsigned long allot; | |
765 | size_t l = len; | |
766 | ||
767 | if (ecb->truncate) | |
768 | return ecb->truncate(line, len); | |
769 | cp = line; | |
770 | allot = l; | |
771 | while (0 < l) { | |
772 | (void) utf8_width(&cp, &l); | |
773 | if (!cp) | |
774 | break; /* truncated in the middle? */ | |
775 | } | |
776 | return allot - l; | |
777 | } | |
778 | ||
d68fe26f | 779 | static void find_lno(const char *line, struct emit_callback *ecbdata) |
690ed843 | 780 | { |
d68fe26f JH |
781 | const char *p; |
782 | ecbdata->lno_in_preimage = 0; | |
783 | ecbdata->lno_in_postimage = 0; | |
784 | p = strchr(line, '-'); | |
690ed843 | 785 | if (!p) |
d68fe26f JH |
786 | return; /* cannot happen */ |
787 | ecbdata->lno_in_preimage = strtol(p + 1, NULL, 10); | |
788 | p = strchr(p, '+'); | |
789 | if (!p) | |
790 | return; /* cannot happen */ | |
791 | ecbdata->lno_in_postimage = strtol(p + 1, NULL, 10); | |
690ed843 JH |
792 | } |
793 | ||
cd112cef | 794 | static void fn_out_consume(void *priv, char *line, unsigned long len) |
6973dcae | 795 | { |
6973dcae | 796 | struct emit_callback *ecbdata = priv; |
472ca780 JK |
797 | const char *meta = diff_get_color(ecbdata->color_diff, DIFF_METAINFO); |
798 | const char *plain = diff_get_color(ecbdata->color_diff, DIFF_PLAIN); | |
ce436973 | 799 | const char *reset = diff_get_color(ecbdata->color_diff, DIFF_RESET); |
6973dcae | 800 | |
3e97c7c6 GB |
801 | if (ecbdata->header) { |
802 | fprintf(ecbdata->file, "%s", ecbdata->header->buf); | |
803 | strbuf_reset(ecbdata->header); | |
804 | ecbdata->header = NULL; | |
805 | } | |
34a5e1a2 JS |
806 | *(ecbdata->found_changesp) = 1; |
807 | ||
6973dcae | 808 | if (ecbdata->label_path[0]) { |
1a9eb3b9 JH |
809 | const char *name_a_tab, *name_b_tab; |
810 | ||
811 | name_a_tab = strchr(ecbdata->label_path[0], ' ') ? "\t" : ""; | |
812 | name_b_tab = strchr(ecbdata->label_path[1], ' ') ? "\t" : ""; | |
813 | ||
c0c77734 DB |
814 | fprintf(ecbdata->file, "%s--- %s%s%s\n", |
815 | meta, ecbdata->label_path[0], reset, name_a_tab); | |
816 | fprintf(ecbdata->file, "%s+++ %s%s%s\n", | |
817 | meta, ecbdata->label_path[1], reset, name_b_tab); | |
6973dcae JH |
818 | ecbdata->label_path[0] = ecbdata->label_path[1] = NULL; |
819 | } | |
cd112cef | 820 | |
a624eaa7 JM |
821 | if (diff_suppress_blank_empty |
822 | && len == 2 && line[0] == ' ' && line[1] == '\n') { | |
823 | line[0] = '\n'; | |
824 | len = 1; | |
825 | } | |
826 | ||
b8d9c1a6 | 827 | if (line[0] == '@') { |
76fd2828 JH |
828 | if (ecbdata->diff_words) |
829 | diff_words_flush(ecbdata); | |
23707811 | 830 | len = sane_truncate_line(ecbdata, line, len); |
d68fe26f | 831 | find_lno(line, ecbdata); |
89cb73a1 | 832 | emit_hunk_header(ecbdata, line, len); |
23707811 | 833 | if (line[len-1] != '\n') |
c0c77734 | 834 | putc('\n', ecbdata->file); |
448c3ef1 | 835 | return; |
cd112cef | 836 | } |
448c3ef1 | 837 | |
b8d9c1a6 | 838 | if (len < 1) { |
c0c77734 | 839 | emit_line(ecbdata->file, reset, reset, line, len); |
448c3ef1 | 840 | return; |
cd112cef | 841 | } |
448c3ef1 | 842 | |
448c3ef1 JH |
843 | if (ecbdata->diff_words) { |
844 | if (line[0] == '-') { | |
845 | diff_words_append(line, len, | |
846 | &ecbdata->diff_words->minus); | |
847 | return; | |
848 | } else if (line[0] == '+') { | |
849 | diff_words_append(line, len, | |
850 | &ecbdata->diff_words->plus); | |
851 | return; | |
852 | } | |
76fd2828 | 853 | diff_words_flush(ecbdata); |
448c3ef1 | 854 | line++; |
50f575fc | 855 | len--; |
c0c77734 | 856 | emit_line(ecbdata->file, plain, reset, line, len); |
448c3ef1 JH |
857 | return; |
858 | } | |
448c3ef1 | 859 | |
690ed843 JH |
860 | if (line[0] != '+') { |
861 | const char *color = | |
862 | diff_get_color(ecbdata->color_diff, | |
863 | line[0] == '-' ? DIFF_FILE_OLD : DIFF_PLAIN); | |
864 | ecbdata->lno_in_preimage++; | |
d68fe26f JH |
865 | if (line[0] == ' ') |
866 | ecbdata->lno_in_postimage++; | |
690ed843 | 867 | emit_line(ecbdata->file, color, reset, line, len); |
d68fe26f JH |
868 | } else { |
869 | ecbdata->lno_in_postimage++; | |
018cff70 | 870 | emit_add_line(reset, ecbdata, line + 1, len - 1); |
448c3ef1 | 871 | } |
6973dcae JH |
872 | } |
873 | ||
874 | static char *pprint_rename(const char *a, const char *b) | |
875 | { | |
876 | const char *old = a; | |
877 | const char *new = b; | |
f285a2d7 | 878 | struct strbuf name = STRBUF_INIT; |
6973dcae JH |
879 | int pfx_length, sfx_length; |
880 | int len_a = strlen(a); | |
881 | int len_b = strlen(b); | |
663af342 | 882 | int a_midlen, b_midlen; |
e5bfbf9b AJ |
883 | int qlen_a = quote_c_style(a, NULL, NULL, 0); |
884 | int qlen_b = quote_c_style(b, NULL, NULL, 0); | |
885 | ||
886 | if (qlen_a || qlen_b) { | |
663af342 PH |
887 | quote_c_style(a, &name, NULL, 0); |
888 | strbuf_addstr(&name, " => "); | |
889 | quote_c_style(b, &name, NULL, 0); | |
b315c5c0 | 890 | return strbuf_detach(&name, NULL); |
e5bfbf9b | 891 | } |
6973dcae JH |
892 | |
893 | /* Find common prefix */ | |
894 | pfx_length = 0; | |
895 | while (*old && *new && *old == *new) { | |
896 | if (*old == '/') | |
897 | pfx_length = old - a + 1; | |
898 | old++; | |
899 | new++; | |
900 | } | |
901 | ||
902 | /* Find common suffix */ | |
903 | old = a + len_a; | |
904 | new = b + len_b; | |
905 | sfx_length = 0; | |
906 | while (a <= old && b <= new && *old == *new) { | |
907 | if (*old == '/') | |
908 | sfx_length = len_a - (old - a); | |
909 | old--; | |
910 | new--; | |
911 | } | |
912 | ||
913 | /* | |
914 | * pfx{mid-a => mid-b}sfx | |
915 | * {pfx-a => pfx-b}sfx | |
916 | * pfx{sfx-a => sfx-b} | |
917 | * name-a => name-b | |
918 | */ | |
663af342 PH |
919 | a_midlen = len_a - pfx_length - sfx_length; |
920 | b_midlen = len_b - pfx_length - sfx_length; | |
921 | if (a_midlen < 0) | |
922 | a_midlen = 0; | |
923 | if (b_midlen < 0) | |
924 | b_midlen = 0; | |
925 | ||
926 | strbuf_grow(&name, pfx_length + a_midlen + b_midlen + sfx_length + 7); | |
6973dcae | 927 | if (pfx_length + sfx_length) { |
663af342 PH |
928 | strbuf_add(&name, a, pfx_length); |
929 | strbuf_addch(&name, '{'); | |
6973dcae | 930 | } |
663af342 PH |
931 | strbuf_add(&name, a + pfx_length, a_midlen); |
932 | strbuf_addstr(&name, " => "); | |
933 | strbuf_add(&name, b + pfx_length, b_midlen); | |
934 | if (pfx_length + sfx_length) { | |
935 | strbuf_addch(&name, '}'); | |
936 | strbuf_add(&name, a + len_a - sfx_length, sfx_length); | |
6973dcae | 937 | } |
b315c5c0 | 938 | return strbuf_detach(&name, NULL); |
6973dcae JH |
939 | } |
940 | ||
941 | struct diffstat_t { | |
6973dcae JH |
942 | int nr; |
943 | int alloc; | |
944 | struct diffstat_file { | |
f604652e | 945 | char *from_name; |
6973dcae | 946 | char *name; |
f604652e | 947 | char *print_name; |
6973dcae JH |
948 | unsigned is_unmerged:1; |
949 | unsigned is_binary:1; | |
950 | unsigned is_renamed:1; | |
0974c117 | 951 | uintmax_t added, deleted; |
6973dcae JH |
952 | } **files; |
953 | }; | |
954 | ||
955 | static struct diffstat_file *diffstat_add(struct diffstat_t *diffstat, | |
956 | const char *name_a, | |
957 | const char *name_b) | |
958 | { | |
959 | struct diffstat_file *x; | |
960 | x = xcalloc(sizeof (*x), 1); | |
961 | if (diffstat->nr == diffstat->alloc) { | |
962 | diffstat->alloc = alloc_nr(diffstat->alloc); | |
963 | diffstat->files = xrealloc(diffstat->files, | |
964 | diffstat->alloc * sizeof(x)); | |
965 | } | |
966 | diffstat->files[diffstat->nr++] = x; | |
967 | if (name_b) { | |
f604652e JH |
968 | x->from_name = xstrdup(name_a); |
969 | x->name = xstrdup(name_b); | |
6973dcae JH |
970 | x->is_renamed = 1; |
971 | } | |
f604652e JH |
972 | else { |
973 | x->from_name = NULL; | |
9befac47 | 974 | x->name = xstrdup(name_a); |
f604652e | 975 | } |
6973dcae JH |
976 | return x; |
977 | } | |
978 | ||
979 | static void diffstat_consume(void *priv, char *line, unsigned long len) | |
980 | { | |
981 | struct diffstat_t *diffstat = priv; | |
982 | struct diffstat_file *x = diffstat->files[diffstat->nr - 1]; | |
983 | ||
984 | if (line[0] == '+') | |
985 | x->added++; | |
986 | else if (line[0] == '-') | |
987 | x->deleted++; | |
988 | } | |
989 | ||
698ce6f8 | 990 | const char mime_boundary_leader[] = "------------"; |
6973dcae | 991 | |
a2540023 JH |
992 | static int scale_linear(int it, int width, int max_change) |
993 | { | |
994 | /* | |
3ed74e60 JS |
995 | * make sure that at least one '-' is printed if there were deletions, |
996 | * and likewise for '+'. | |
a2540023 | 997 | */ |
3ed74e60 JS |
998 | if (max_change < 2) |
999 | return it; | |
1000 | return ((it - 1) * (width - 1) + max_change - 1) / (max_change - 1); | |
a2540023 JH |
1001 | } |
1002 | ||
c0c77734 | 1003 | static void show_name(FILE *file, |
a408e0e6 | 1004 | const char *prefix, const char *name, int len) |
a2540023 | 1005 | { |
a408e0e6 | 1006 | fprintf(file, " %s%-*s |", prefix, len, name); |
a2540023 JH |
1007 | } |
1008 | ||
c0c77734 | 1009 | static void show_graph(FILE *file, char ch, int cnt, const char *set, const char *reset) |
a2540023 JH |
1010 | { |
1011 | if (cnt <= 0) | |
1012 | return; | |
c0c77734 | 1013 | fprintf(file, "%s", set); |
a2540023 | 1014 | while (cnt--) |
c0c77734 DB |
1015 | putc(ch, file); |
1016 | fprintf(file, "%s", reset); | |
a2540023 JH |
1017 | } |
1018 | ||
f604652e JH |
1019 | static void fill_print_name(struct diffstat_file *file) |
1020 | { | |
1021 | char *pname; | |
1022 | ||
1023 | if (file->print_name) | |
1024 | return; | |
1025 | ||
1026 | if (!file->is_renamed) { | |
f285a2d7 | 1027 | struct strbuf buf = STRBUF_INIT; |
f604652e JH |
1028 | if (quote_c_style(file->name, &buf, NULL, 0)) { |
1029 | pname = strbuf_detach(&buf, NULL); | |
1030 | } else { | |
1031 | pname = file->name; | |
1032 | strbuf_release(&buf); | |
1033 | } | |
1034 | } else { | |
1035 | pname = pprint_rename(file->from_name, file->name); | |
1036 | } | |
1037 | file->print_name = pname; | |
1038 | } | |
1039 | ||
4b25d091 | 1040 | static void show_stats(struct diffstat_t *data, struct diff_options *options) |
6973dcae | 1041 | { |
eb3a9dd3 | 1042 | int i, len, add, del, adds = 0, dels = 0; |
0974c117 | 1043 | uintmax_t max_change = 0, max_len = 0; |
6973dcae | 1044 | int total_files = data->nr; |
a2540023 | 1045 | int width, name_width; |
785f7432 | 1046 | const char *reset, *set, *add_c, *del_c; |
6973dcae JH |
1047 | |
1048 | if (data->nr == 0) | |
1049 | return; | |
1050 | ||
a2540023 JH |
1051 | width = options->stat_width ? options->stat_width : 80; |
1052 | name_width = options->stat_name_width ? options->stat_name_width : 50; | |
1053 | ||
1054 | /* Sanity: give at least 5 columns to the graph, | |
1055 | * but leave at least 10 columns for the name. | |
1056 | */ | |
861d1af3 OM |
1057 | if (width < 25) |
1058 | width = 25; | |
1059 | if (name_width < 10) | |
1060 | name_width = 10; | |
1061 | else if (width < name_width + 15) | |
1062 | name_width = width - 15; | |
a2540023 JH |
1063 | |
1064 | /* Find the longest filename and max number of changes */ | |
8f67f8ae PH |
1065 | reset = diff_get_color_opt(options, DIFF_RESET); |
1066 | set = diff_get_color_opt(options, DIFF_PLAIN); | |
1067 | add_c = diff_get_color_opt(options, DIFF_FILE_NEW); | |
1068 | del_c = diff_get_color_opt(options, DIFF_FILE_OLD); | |
785f7432 | 1069 | |
6973dcae JH |
1070 | for (i = 0; i < data->nr; i++) { |
1071 | struct diffstat_file *file = data->files[i]; | |
0974c117 | 1072 | uintmax_t change = file->added + file->deleted; |
f604652e JH |
1073 | fill_print_name(file); |
1074 | len = strlen(file->print_name); | |
6973dcae JH |
1075 | if (max_len < len) |
1076 | max_len = len; | |
1077 | ||
1078 | if (file->is_binary || file->is_unmerged) | |
1079 | continue; | |
a2540023 JH |
1080 | if (max_change < change) |
1081 | max_change = change; | |
6973dcae JH |
1082 | } |
1083 | ||
a2540023 JH |
1084 | /* Compute the width of the graph part; |
1085 | * 10 is for one blank at the beginning of the line plus | |
1086 | * " | count " between the name and the graph. | |
1087 | * | |
1088 | * From here on, name_width is the width of the name area, | |
1089 | * and width is the width of the graph area. | |
1090 | */ | |
1091 | name_width = (name_width < max_len) ? name_width : max_len; | |
1092 | if (width < (name_width + 10) + max_change) | |
1093 | width = width - (name_width + 10); | |
1094 | else | |
1095 | width = max_change; | |
1096 | ||
6973dcae | 1097 | for (i = 0; i < data->nr; i++) { |
d2543b8e | 1098 | const char *prefix = ""; |
f604652e | 1099 | char *name = data->files[i]->print_name; |
0974c117 JK |
1100 | uintmax_t added = data->files[i]->added; |
1101 | uintmax_t deleted = data->files[i]->deleted; | |
a2540023 | 1102 | int name_len; |
6973dcae JH |
1103 | |
1104 | /* | |
1105 | * "scale" the filename | |
1106 | */ | |
a2540023 JH |
1107 | len = name_width; |
1108 | name_len = strlen(name); | |
1109 | if (name_width < name_len) { | |
6973dcae JH |
1110 | char *slash; |
1111 | prefix = "..."; | |
a2540023 JH |
1112 | len -= 3; |
1113 | name += name_len - len; | |
6973dcae JH |
1114 | slash = strchr(name, '/'); |
1115 | if (slash) | |
1116 | name = slash; | |
1117 | } | |
6973dcae JH |
1118 | |
1119 | if (data->files[i]->is_binary) { | |
a408e0e6 | 1120 | show_name(options->file, prefix, name, len); |
c0c77734 | 1121 | fprintf(options->file, " Bin "); |
0974c117 JK |
1122 | fprintf(options->file, "%s%"PRIuMAX"%s", |
1123 | del_c, deleted, reset); | |
c0c77734 | 1124 | fprintf(options->file, " -> "); |
0974c117 JK |
1125 | fprintf(options->file, "%s%"PRIuMAX"%s", |
1126 | add_c, added, reset); | |
c0c77734 DB |
1127 | fprintf(options->file, " bytes"); |
1128 | fprintf(options->file, "\n"); | |
f604652e | 1129 | continue; |
6973dcae JH |
1130 | } |
1131 | else if (data->files[i]->is_unmerged) { | |
a408e0e6 | 1132 | show_name(options->file, prefix, name, len); |
c0c77734 | 1133 | fprintf(options->file, " Unmerged\n"); |
f604652e | 1134 | continue; |
6973dcae JH |
1135 | } |
1136 | else if (!data->files[i]->is_renamed && | |
1137 | (added + deleted == 0)) { | |
1138 | total_files--; | |
f604652e | 1139 | continue; |
6973dcae JH |
1140 | } |
1141 | ||
a2540023 JH |
1142 | /* |
1143 | * scale the add/delete | |
1144 | */ | |
6973dcae JH |
1145 | add = added; |
1146 | del = deleted; | |
6973dcae JH |
1147 | adds += add; |
1148 | dels += del; | |
1149 | ||
a2540023 | 1150 | if (width <= max_change) { |
a2540023 | 1151 | add = scale_linear(add, width, max_change); |
3ed74e60 | 1152 | del = scale_linear(del, width, max_change); |
6973dcae | 1153 | } |
a408e0e6 | 1154 | show_name(options->file, prefix, name, len); |
0974c117 | 1155 | fprintf(options->file, "%5"PRIuMAX"%s", added + deleted, |
4d9b5359 | 1156 | added + deleted ? " " : ""); |
c0c77734 DB |
1157 | show_graph(options->file, '+', add, add_c, reset); |
1158 | show_graph(options->file, '-', del, del_c, reset); | |
1159 | fprintf(options->file, "\n"); | |
1160 | } | |
1161 | fprintf(options->file, | |
a408e0e6 MH |
1162 | " %d files changed, %d insertions(+), %d deletions(-)\n", |
1163 | total_files, adds, dels); | |
6973dcae JH |
1164 | } |
1165 | ||
2775d92c | 1166 | static void show_shortstats(struct diffstat_t *data, struct diff_options *options) |
ebd124c6 NP |
1167 | { |
1168 | int i, adds = 0, dels = 0, total_files = data->nr; | |
1169 | ||
1170 | if (data->nr == 0) | |
1171 | return; | |
1172 | ||
1173 | for (i = 0; i < data->nr; i++) { | |
1174 | if (!data->files[i]->is_binary && | |
1175 | !data->files[i]->is_unmerged) { | |
1176 | int added = data->files[i]->added; | |
1177 | int deleted= data->files[i]->deleted; | |
1178 | if (!data->files[i]->is_renamed && | |
1179 | (added + deleted == 0)) { | |
1180 | total_files--; | |
1181 | } else { | |
1182 | adds += added; | |
1183 | dels += deleted; | |
1184 | } | |
1185 | } | |
ebd124c6 | 1186 | } |
c0c77734 | 1187 | fprintf(options->file, " %d files changed, %d insertions(+), %d deletions(-)\n", |
ebd124c6 NP |
1188 | total_files, adds, dels); |
1189 | } | |
1190 | ||
4b25d091 | 1191 | static void show_numstat(struct diffstat_t *data, struct diff_options *options) |
74e2abe5 JH |
1192 | { |
1193 | int i; | |
1194 | ||
f604652e JH |
1195 | if (data->nr == 0) |
1196 | return; | |
1197 | ||
74e2abe5 JH |
1198 | for (i = 0; i < data->nr; i++) { |
1199 | struct diffstat_file *file = data->files[i]; | |
1200 | ||
bfddbc5e | 1201 | if (file->is_binary) |
c0c77734 | 1202 | fprintf(options->file, "-\t-\t"); |
bfddbc5e | 1203 | else |
c0c77734 | 1204 | fprintf(options->file, |
0974c117 JK |
1205 | "%"PRIuMAX"\t%"PRIuMAX"\t", |
1206 | file->added, file->deleted); | |
f604652e JH |
1207 | if (options->line_termination) { |
1208 | fill_print_name(file); | |
1209 | if (!file->is_renamed) | |
c0c77734 | 1210 | write_name_quoted(file->name, options->file, |
f604652e JH |
1211 | options->line_termination); |
1212 | else { | |
c0c77734 DB |
1213 | fputs(file->print_name, options->file); |
1214 | putc(options->line_termination, options->file); | |
f604652e | 1215 | } |
663af342 | 1216 | } else { |
f604652e | 1217 | if (file->is_renamed) { |
c0c77734 DB |
1218 | putc('\0', options->file); |
1219 | write_name_quoted(file->from_name, options->file, '\0'); | |
f604652e | 1220 | } |
c0c77734 | 1221 | write_name_quoted(file->name, options->file, '\0'); |
663af342 | 1222 | } |
74e2abe5 JH |
1223 | } |
1224 | } | |
1225 | ||
c04a7155 JH |
1226 | struct dirstat_file { |
1227 | const char *name; | |
1228 | unsigned long changed; | |
7df7c019 LT |
1229 | }; |
1230 | ||
c04a7155 JH |
1231 | struct dirstat_dir { |
1232 | struct dirstat_file *files; | |
1233 | int alloc, nr, percent, cumulative; | |
1234 | }; | |
1235 | ||
1236 | static long gather_dirstat(FILE *file, struct dirstat_dir *dir, unsigned long changed, const char *base, int baselen) | |
7df7c019 LT |
1237 | { |
1238 | unsigned long this_dir = 0; | |
1239 | unsigned int sources = 0; | |
1240 | ||
1241 | while (dir->nr) { | |
c04a7155 | 1242 | struct dirstat_file *f = dir->files; |
7df7c019 LT |
1243 | int namelen = strlen(f->name); |
1244 | unsigned long this; | |
1245 | char *slash; | |
1246 | ||
1247 | if (namelen < baselen) | |
1248 | break; | |
1249 | if (memcmp(f->name, base, baselen)) | |
1250 | break; | |
1251 | slash = strchr(f->name + baselen, '/'); | |
1252 | if (slash) { | |
1253 | int newbaselen = slash + 1 - f->name; | |
c0c77734 | 1254 | this = gather_dirstat(file, dir, changed, f->name, newbaselen); |
7df7c019 LT |
1255 | sources++; |
1256 | } else { | |
c04a7155 | 1257 | this = f->changed; |
7df7c019 LT |
1258 | dir->files++; |
1259 | dir->nr--; | |
1260 | sources += 2; | |
1261 | } | |
1262 | this_dir += this; | |
1263 | } | |
1264 | ||
1265 | /* | |
1266 | * We don't report dirstat's for | |
1267 | * - the top level | |
1268 | * - or cases where everything came from a single directory | |
1269 | * under this directory (sources == 1). | |
1270 | */ | |
1271 | if (baselen && sources != 1) { | |
1272 | int permille = this_dir * 1000 / changed; | |
1273 | if (permille) { | |
1274 | int percent = permille / 10; | |
1275 | if (percent >= dir->percent) { | |
c0c77734 | 1276 | fprintf(file, "%4d.%01d%% %.*s\n", percent, permille % 10, baselen, base); |
7df7c019 LT |
1277 | if (!dir->cumulative) |
1278 | return 0; | |
1279 | } | |
1280 | } | |
1281 | } | |
1282 | return this_dir; | |
1283 | } | |
1284 | ||
441bca0b LT |
1285 | static int dirstat_compare(const void *_a, const void *_b) |
1286 | { | |
1287 | const struct dirstat_file *a = _a; | |
1288 | const struct dirstat_file *b = _b; | |
1289 | return strcmp(a->name, b->name); | |
1290 | } | |
1291 | ||
c04a7155 | 1292 | static void show_dirstat(struct diff_options *options) |
7df7c019 LT |
1293 | { |
1294 | int i; | |
1295 | unsigned long changed; | |
c04a7155 JH |
1296 | struct dirstat_dir dir; |
1297 | struct diff_queue_struct *q = &diff_queued_diff; | |
1298 | ||
1299 | dir.files = NULL; | |
1300 | dir.alloc = 0; | |
1301 | dir.nr = 0; | |
1302 | dir.percent = options->dirstat_percent; | |
f88d225f | 1303 | dir.cumulative = DIFF_OPT_TST(options, DIRSTAT_CUMULATIVE); |
7df7c019 | 1304 | |
7df7c019 | 1305 | changed = 0; |
c04a7155 JH |
1306 | for (i = 0; i < q->nr; i++) { |
1307 | struct diff_filepair *p = q->queue[i]; | |
1308 | const char *name; | |
1309 | unsigned long copied, added, damage; | |
1310 | ||
1311 | name = p->one->path ? p->one->path : p->two->path; | |
1312 | ||
1313 | if (DIFF_FILE_VALID(p->one) && DIFF_FILE_VALID(p->two)) { | |
1314 | diff_populate_filespec(p->one, 0); | |
1315 | diff_populate_filespec(p->two, 0); | |
1316 | diffcore_count_changes(p->one, p->two, NULL, NULL, 0, | |
1317 | &copied, &added); | |
1318 | diff_free_filespec_data(p->one); | |
1319 | diff_free_filespec_data(p->two); | |
1320 | } else if (DIFF_FILE_VALID(p->one)) { | |
1321 | diff_populate_filespec(p->one, 1); | |
1322 | copied = added = 0; | |
1323 | diff_free_filespec_data(p->one); | |
1324 | } else if (DIFF_FILE_VALID(p->two)) { | |
1325 | diff_populate_filespec(p->two, 1); | |
1326 | copied = 0; | |
1327 | added = p->two->size; | |
1328 | diff_free_filespec_data(p->two); | |
1329 | } else | |
2b0b551d | 1330 | continue; |
c04a7155 JH |
1331 | |
1332 | /* | |
1333 | * Original minus copied is the removed material, | |
1334 | * added is the new material. They are both damages | |
fd33777b HO |
1335 | * made to the preimage. In --dirstat-by-file mode, count |
1336 | * damaged files, not damaged lines. This is done by | |
1337 | * counting only a single damaged line per file. | |
c04a7155 JH |
1338 | */ |
1339 | damage = (p->one->size - copied) + added; | |
fd33777b HO |
1340 | if (DIFF_OPT_TST(options, DIRSTAT_BY_FILE) && damage > 0) |
1341 | damage = 1; | |
c04a7155 JH |
1342 | |
1343 | ALLOC_GROW(dir.files, dir.nr + 1, dir.alloc); | |
1344 | dir.files[dir.nr].name = name; | |
1345 | dir.files[dir.nr].changed = damage; | |
1346 | changed += damage; | |
1347 | dir.nr++; | |
7df7c019 LT |
1348 | } |
1349 | ||
1350 | /* This can happen even with many files, if everything was renames */ | |
1351 | if (!changed) | |
1352 | return; | |
1353 | ||
1354 | /* Show all directories with more than x% of the changes */ | |
441bca0b | 1355 | qsort(dir.files, dir.nr, sizeof(dir.files[0]), dirstat_compare); |
c0c77734 | 1356 | gather_dirstat(options->file, &dir, changed, "", 0); |
7df7c019 LT |
1357 | } |
1358 | ||
f604652e JH |
1359 | static void free_diffstat_info(struct diffstat_t *diffstat) |
1360 | { | |
1361 | int i; | |
1362 | for (i = 0; i < diffstat->nr; i++) { | |
1363 | struct diffstat_file *f = diffstat->files[i]; | |
1364 | if (f->name != f->print_name) | |
1365 | free(f->print_name); | |
1366 | free(f->name); | |
1367 | free(f->from_name); | |
1368 | free(f); | |
1369 | } | |
1370 | free(diffstat->files); | |
1371 | } | |
1372 | ||
88246898 | 1373 | struct checkdiff_t { |
88246898 | 1374 | const char *filename; |
1ba111d1 | 1375 | int lineno; |
a757c646 | 1376 | int conflict_marker_size; |
1ba111d1 | 1377 | struct diff_options *o; |
cf1b7869 | 1378 | unsigned ws_rule; |
62c64895 | 1379 | unsigned status; |
88246898 JS |
1380 | }; |
1381 | ||
a757c646 | 1382 | static int is_conflict_marker(const char *line, int marker_size, unsigned long len) |
04954043 JH |
1383 | { |
1384 | char firstchar; | |
1385 | int cnt; | |
1386 | ||
a757c646 | 1387 | if (len < marker_size + 1) |
04954043 JH |
1388 | return 0; |
1389 | firstchar = line[0]; | |
1390 | switch (firstchar) { | |
a757c646 | 1391 | case '=': case '>': case '<': case '|': |
04954043 JH |
1392 | break; |
1393 | default: | |
1394 | return 0; | |
1395 | } | |
a757c646 | 1396 | for (cnt = 1; cnt < marker_size; cnt++) |
04954043 JH |
1397 | if (line[cnt] != firstchar) |
1398 | return 0; | |
a757c646 JH |
1399 | /* line[1] thru line[marker_size-1] are same as firstchar */ |
1400 | if (len < marker_size + 1 || !isspace(line[marker_size])) | |
04954043 | 1401 | return 0; |
04954043 JH |
1402 | return 1; |
1403 | } | |
1404 | ||
88246898 JS |
1405 | static void checkdiff_consume(void *priv, char *line, unsigned long len) |
1406 | { | |
1407 | struct checkdiff_t *data = priv; | |
1ba111d1 | 1408 | int color_diff = DIFF_OPT_TST(data->o, COLOR_DIFF); |
a757c646 | 1409 | int marker_size = data->conflict_marker_size; |
1ba111d1 JH |
1410 | const char *ws = diff_get_color(color_diff, DIFF_WHITESPACE); |
1411 | const char *reset = diff_get_color(color_diff, DIFF_RESET); | |
1412 | const char *set = diff_get_color(color_diff, DIFF_FILE_NEW); | |
c1795bb0 | 1413 | char *err; |
88246898 JS |
1414 | |
1415 | if (line[0] == '+') { | |
18374e58 | 1416 | unsigned bad; |
0ef617f4 | 1417 | data->lineno++; |
a757c646 | 1418 | if (is_conflict_marker(line + 1, marker_size, len - 1)) { |
04954043 JH |
1419 | data->status |= 1; |
1420 | fprintf(data->o->file, | |
1421 | "%s:%d: leftover conflict marker\n", | |
1422 | data->filename, data->lineno); | |
1423 | } | |
8f8841e9 | 1424 | bad = ws_check(line + 1, len - 1, data->ws_rule); |
18374e58 | 1425 | if (!bad) |
c1795bb0 | 1426 | return; |
18374e58 JH |
1427 | data->status |= bad; |
1428 | err = whitespace_error_string(bad); | |
1ba111d1 JH |
1429 | fprintf(data->o->file, "%s:%d: %s.\n", |
1430 | data->filename, data->lineno, err); | |
c1795bb0 | 1431 | free(err); |
1ba111d1 | 1432 | emit_line(data->o->file, set, reset, line, 1); |
8f8841e9 | 1433 | ws_check_emit(line + 1, len - 1, data->ws_rule, |
1ba111d1 | 1434 | data->o->file, set, reset, ws); |
877f23cc | 1435 | } else if (line[0] == ' ') { |
88246898 | 1436 | data->lineno++; |
877f23cc | 1437 | } else if (line[0] == '@') { |
88246898 JS |
1438 | char *plus = strchr(line, '+'); |
1439 | if (plus) | |
0ef617f4 | 1440 | data->lineno = strtol(plus, NULL, 10) - 1; |
88246898 JS |
1441 | else |
1442 | die("invalid diff"); | |
1443 | } | |
1444 | } | |
1445 | ||
0660626c JH |
1446 | static unsigned char *deflate_it(char *data, |
1447 | unsigned long size, | |
1448 | unsigned long *result_size) | |
051308f6 | 1449 | { |
0660626c JH |
1450 | int bound; |
1451 | unsigned char *deflated; | |
1452 | z_stream stream; | |
1453 | ||
1454 | memset(&stream, 0, sizeof(stream)); | |
12f6c308 | 1455 | deflateInit(&stream, zlib_compression_level); |
0660626c JH |
1456 | bound = deflateBound(&stream, size); |
1457 | deflated = xmalloc(bound); | |
1458 | stream.next_out = deflated; | |
1459 | stream.avail_out = bound; | |
1460 | ||
1461 | stream.next_in = (unsigned char *)data; | |
1462 | stream.avail_in = size; | |
1463 | while (deflate(&stream, Z_FINISH) == Z_OK) | |
1464 | ; /* nothing */ | |
1465 | deflateEnd(&stream); | |
1466 | *result_size = stream.total_out; | |
1467 | return deflated; | |
051308f6 JH |
1468 | } |
1469 | ||
c0c77734 | 1470 | static void emit_binary_diff_body(FILE *file, mmfile_t *one, mmfile_t *two) |
051308f6 | 1471 | { |
0660626c JH |
1472 | void *cp; |
1473 | void *delta; | |
1474 | void *deflated; | |
1475 | void *data; | |
1476 | unsigned long orig_size; | |
1477 | unsigned long delta_size; | |
1478 | unsigned long deflate_size; | |
1479 | unsigned long data_size; | |
051308f6 | 1480 | |
0660626c JH |
1481 | /* We could do deflated delta, or we could do just deflated two, |
1482 | * whichever is smaller. | |
051308f6 | 1483 | */ |
0660626c JH |
1484 | delta = NULL; |
1485 | deflated = deflate_it(two->ptr, two->size, &deflate_size); | |
1486 | if (one->size && two->size) { | |
1487 | delta = diff_delta(one->ptr, one->size, | |
1488 | two->ptr, two->size, | |
1489 | &delta_size, deflate_size); | |
1490 | if (delta) { | |
1491 | void *to_free = delta; | |
1492 | orig_size = delta_size; | |
1493 | delta = deflate_it(delta, delta_size, &delta_size); | |
1494 | free(to_free); | |
051308f6 JH |
1495 | } |
1496 | } | |
051308f6 | 1497 | |
0660626c | 1498 | if (delta && delta_size < deflate_size) { |
c0c77734 | 1499 | fprintf(file, "delta %lu\n", orig_size); |
0660626c JH |
1500 | free(deflated); |
1501 | data = delta; | |
1502 | data_size = delta_size; | |
1503 | } | |
1504 | else { | |
c0c77734 | 1505 | fprintf(file, "literal %lu\n", two->size); |
0660626c JH |
1506 | free(delta); |
1507 | data = deflated; | |
1508 | data_size = deflate_size; | |
1509 | } | |
051308f6 | 1510 | |
0660626c JH |
1511 | /* emit data encoded in base85 */ |
1512 | cp = data; | |
1513 | while (data_size) { | |
1514 | int bytes = (52 < data_size) ? 52 : data_size; | |
051308f6 | 1515 | char line[70]; |
0660626c | 1516 | data_size -= bytes; |
051308f6 JH |
1517 | if (bytes <= 26) |
1518 | line[0] = bytes + 'A' - 1; | |
1519 | else | |
1520 | line[0] = bytes - 26 + 'a' - 1; | |
1521 | encode_85(line + 1, cp, bytes); | |
1d7f171c | 1522 | cp = (char *) cp + bytes; |
c0c77734 DB |
1523 | fputs(line, file); |
1524 | fputc('\n', file); | |
051308f6 | 1525 | } |
c0c77734 | 1526 | fprintf(file, "\n"); |
0660626c | 1527 | free(data); |
051308f6 JH |
1528 | } |
1529 | ||
c0c77734 | 1530 | static void emit_binary_diff(FILE *file, mmfile_t *one, mmfile_t *two) |
d4c452f0 | 1531 | { |
c0c77734 DB |
1532 | fprintf(file, "GIT binary patch\n"); |
1533 | emit_binary_diff_body(file, one, two); | |
1534 | emit_binary_diff_body(file, two, one); | |
d4c452f0 JH |
1535 | } |
1536 | ||
72cf4841 | 1537 | static void diff_filespec_load_driver(struct diff_filespec *one) |
6973dcae | 1538 | { |
122aa6f9 JK |
1539 | if (!one->driver) |
1540 | one->driver = userdiff_find_by_path(one->path); | |
1541 | if (!one->driver) | |
1542 | one->driver = userdiff_find_by_name("default"); | |
29a3eefd JH |
1543 | } |
1544 | ||
1545 | int diff_filespec_is_binary(struct diff_filespec *one) | |
1546 | { | |
122aa6f9 JK |
1547 | if (one->is_binary == -1) { |
1548 | diff_filespec_load_driver(one); | |
1549 | if (one->driver->binary != -1) | |
1550 | one->is_binary = one->driver->binary; | |
1551 | else { | |
1552 | if (!one->data && DIFF_FILE_VALID(one)) | |
1553 | diff_populate_filespec(one, 0); | |
1554 | if (one->data) | |
1555 | one->is_binary = buffer_is_binary(one->data, | |
1556 | one->size); | |
1557 | if (one->is_binary == -1) | |
1558 | one->is_binary = 0; | |
1559 | } | |
1560 | } | |
29a3eefd | 1561 | return one->is_binary; |
6973dcae JH |
1562 | } |
1563 | ||
be58e70d | 1564 | static const struct userdiff_funcname *diff_funcname_pattern(struct diff_filespec *one) |
f258475a | 1565 | { |
122aa6f9 JK |
1566 | diff_filespec_load_driver(one); |
1567 | return one->driver->funcname.pattern ? &one->driver->funcname : NULL; | |
f258475a JH |
1568 | } |
1569 | ||
80c49c3d TR |
1570 | static const char *userdiff_word_regex(struct diff_filespec *one) |
1571 | { | |
1572 | diff_filespec_load_driver(one); | |
1573 | return one->driver->word_regex; | |
1574 | } | |
1575 | ||
a5a818ee JH |
1576 | void diff_set_mnemonic_prefix(struct diff_options *options, const char *a, const char *b) |
1577 | { | |
1578 | if (!options->a_prefix) | |
1579 | options->a_prefix = a; | |
1580 | if (!options->b_prefix) | |
1581 | options->b_prefix = b; | |
1582 | } | |
1583 | ||
04427ac8 JK |
1584 | static const char *get_textconv(struct diff_filespec *one) |
1585 | { | |
1586 | if (!DIFF_FILE_VALID(one)) | |
1587 | return NULL; | |
2675773a JK |
1588 | if (!S_ISREG(one->mode)) |
1589 | return NULL; | |
04427ac8 JK |
1590 | diff_filespec_load_driver(one); |
1591 | return one->driver->textconv; | |
1592 | } | |
1593 | ||
6973dcae JH |
1594 | static void builtin_diff(const char *name_a, |
1595 | const char *name_b, | |
1596 | struct diff_filespec *one, | |
1597 | struct diff_filespec *two, | |
1598 | const char *xfrm_msg, | |
051308f6 | 1599 | struct diff_options *o, |
6973dcae JH |
1600 | int complete_rewrite) |
1601 | { | |
1602 | mmfile_t mf1, mf2; | |
1603 | const char *lbl[2]; | |
1604 | char *a_one, *b_two; | |
8f67f8ae PH |
1605 | const char *set = diff_get_color_opt(o, DIFF_METAINFO); |
1606 | const char *reset = diff_get_color_opt(o, DIFF_RESET); | |
a5a818ee | 1607 | const char *a_prefix, *b_prefix; |
c7534ef4 | 1608 | const char *textconv_one = NULL, *textconv_two = NULL; |
3e97c7c6 | 1609 | struct strbuf header = STRBUF_INIT; |
a5a818ee | 1610 | |
752c0c24 JS |
1611 | if (DIFF_OPT_TST(o, SUBMODULE_LOG) && |
1612 | (!one->mode || S_ISGITLINK(one->mode)) && | |
1613 | (!two->mode || S_ISGITLINK(two->mode))) { | |
1614 | const char *del = diff_get_color_opt(o, DIFF_FILE_OLD); | |
1615 | const char *add = diff_get_color_opt(o, DIFF_FILE_NEW); | |
1616 | show_submodule_summary(o->file, one ? one->path : two->path, | |
721ceec1 | 1617 | one->sha1, two->sha1, two->dirty_submodule, |
752c0c24 JS |
1618 | del, add, reset); |
1619 | return; | |
1620 | } | |
1621 | ||
3aa1f7ca JK |
1622 | if (DIFF_OPT_TST(o, ALLOW_TEXTCONV)) { |
1623 | textconv_one = get_textconv(one); | |
1624 | textconv_two = get_textconv(two); | |
1625 | } | |
1626 | ||
a5a818ee JH |
1627 | diff_set_mnemonic_prefix(o, "a/", "b/"); |
1628 | if (DIFF_OPT_TST(o, REVERSE_DIFF)) { | |
1629 | a_prefix = o->b_prefix; | |
1630 | b_prefix = o->a_prefix; | |
1631 | } else { | |
1632 | a_prefix = o->a_prefix; | |
1633 | b_prefix = o->b_prefix; | |
1634 | } | |
6973dcae | 1635 | |
71b989e7 LT |
1636 | /* Never use a non-valid filename anywhere if at all possible */ |
1637 | name_a = DIFF_FILE_VALID(one) ? name_a : name_b; | |
1638 | name_b = DIFF_FILE_VALID(two) ? name_b : name_a; | |
1639 | ||
a5a818ee JH |
1640 | a_one = quote_two(a_prefix, name_a + (*name_a == '/')); |
1641 | b_two = quote_two(b_prefix, name_b + (*name_b == '/')); | |
6973dcae JH |
1642 | lbl[0] = DIFF_FILE_VALID(one) ? a_one : "/dev/null"; |
1643 | lbl[1] = DIFF_FILE_VALID(two) ? b_two : "/dev/null"; | |
3e97c7c6 | 1644 | strbuf_addf(&header, "%sdiff --git %s %s%s\n", set, a_one, b_two, reset); |
6973dcae JH |
1645 | if (lbl[0][0] == '/') { |
1646 | /* /dev/null */ | |
3e97c7c6 | 1647 | strbuf_addf(&header, "%snew file mode %06o%s\n", set, two->mode, reset); |
50f575fc | 1648 | if (xfrm_msg && xfrm_msg[0]) |
3e97c7c6 | 1649 | strbuf_addf(&header, "%s%s%s\n", set, xfrm_msg, reset); |
6973dcae JH |
1650 | } |
1651 | else if (lbl[1][0] == '/') { | |
3e97c7c6 | 1652 | strbuf_addf(&header, "%sdeleted file mode %06o%s\n", set, one->mode, reset); |
50f575fc | 1653 | if (xfrm_msg && xfrm_msg[0]) |
3e97c7c6 | 1654 | strbuf_addf(&header, "%s%s%s\n", set, xfrm_msg, reset); |
6973dcae JH |
1655 | } |
1656 | else { | |
1657 | if (one->mode != two->mode) { | |
3e97c7c6 GB |
1658 | strbuf_addf(&header, "%sold mode %06o%s\n", set, one->mode, reset); |
1659 | strbuf_addf(&header, "%snew mode %06o%s\n", set, two->mode, reset); | |
cd112cef | 1660 | } |
50f575fc | 1661 | if (xfrm_msg && xfrm_msg[0]) |
3e97c7c6 GB |
1662 | strbuf_addf(&header, "%s%s%s\n", set, xfrm_msg, reset); |
1663 | ||
6973dcae JH |
1664 | /* |
1665 | * we do not run diff between different kind | |
1666 | * of objects. | |
1667 | */ | |
1668 | if ((one->mode ^ two->mode) & S_IFMT) | |
1669 | goto free_ab_and_return; | |
0c01857d | 1670 | if (complete_rewrite && |
3aa1f7ca JK |
1671 | (textconv_one || !diff_filespec_is_binary(one)) && |
1672 | (textconv_two || !diff_filespec_is_binary(two))) { | |
3e97c7c6 GB |
1673 | fprintf(o->file, "%s", header.buf); |
1674 | strbuf_reset(&header); | |
3aa1f7ca JK |
1675 | emit_rewrite_diff(name_a, name_b, one, two, |
1676 | textconv_one, textconv_two, o); | |
34a5e1a2 | 1677 | o->found_changes = 1; |
6973dcae JH |
1678 | goto free_ab_and_return; |
1679 | } | |
1680 | } | |
1681 | ||
1682 | if (fill_mmfile(&mf1, one) < 0 || fill_mmfile(&mf2, two) < 0) | |
1683 | die("unable to read files to diff"); | |
1684 | ||
8f67f8ae | 1685 | if (!DIFF_OPT_TST(o, TEXT) && |
04427ac8 JK |
1686 | ( (diff_filespec_is_binary(one) && !textconv_one) || |
1687 | (diff_filespec_is_binary(two) && !textconv_two) )) { | |
0660626c JH |
1688 | /* Quite common confusing case */ |
1689 | if (mf1.size == mf2.size && | |
1690 | !memcmp(mf1.ptr, mf2.ptr, mf1.size)) | |
1691 | goto free_ab_and_return; | |
3e97c7c6 GB |
1692 | fprintf(o->file, "%s", header.buf); |
1693 | strbuf_reset(&header); | |
8f67f8ae | 1694 | if (DIFF_OPT_TST(o, BINARY)) |
c0c77734 | 1695 | emit_binary_diff(o->file, &mf1, &mf2); |
051308f6 | 1696 | else |
c0c77734 DB |
1697 | fprintf(o->file, "Binary files %s and %s differ\n", |
1698 | lbl[0], lbl[1]); | |
34a5e1a2 | 1699 | o->found_changes = 1; |
051308f6 | 1700 | } |
6973dcae JH |
1701 | else { |
1702 | /* Crazy xdl interfaces.. */ | |
1703 | const char *diffopts = getenv("GIT_DIFF_OPTS"); | |
1704 | xpparam_t xpp; | |
1705 | xdemitconf_t xecfg; | |
6973dcae | 1706 | struct emit_callback ecbdata; |
be58e70d | 1707 | const struct userdiff_funcname *pe; |
f258475a | 1708 | |
3e97c7c6 GB |
1709 | if (!DIFF_XDL_TST(o, WHITESPACE_FLAGS)) { |
1710 | fprintf(o->file, "%s", header.buf); | |
1711 | strbuf_reset(&header); | |
1712 | } | |
1713 | ||
04427ac8 JK |
1714 | if (textconv_one) { |
1715 | size_t size; | |
1716 | mf1.ptr = run_textconv(textconv_one, one, &size); | |
1717 | if (!mf1.ptr) | |
1718 | die("unable to read files to diff"); | |
1719 | mf1.size = size; | |
1720 | } | |
1721 | if (textconv_two) { | |
1722 | size_t size; | |
1723 | mf2.ptr = run_textconv(textconv_two, two, &size); | |
1724 | if (!mf2.ptr) | |
1725 | die("unable to read files to diff"); | |
1726 | mf2.size = size; | |
1727 | } | |
1728 | ||
45e7ca0f BC |
1729 | pe = diff_funcname_pattern(one); |
1730 | if (!pe) | |
1731 | pe = diff_funcname_pattern(two); | |
6973dcae | 1732 | |
9ccd0a88 | 1733 | memset(&xpp, 0, sizeof(xpp)); |
30b25010 | 1734 | memset(&xecfg, 0, sizeof(xecfg)); |
cd112cef | 1735 | memset(&ecbdata, 0, sizeof(ecbdata)); |
6973dcae | 1736 | ecbdata.label_path = lbl; |
8f67f8ae | 1737 | ecbdata.color_diff = DIFF_OPT_TST(o, COLOR_DIFF); |
34a5e1a2 | 1738 | ecbdata.found_changesp = &o->found_changes; |
cf1b7869 | 1739 | ecbdata.ws_rule = whitespace_rule(name_b ? name_b : name_a); |
690ed843 | 1740 | if (ecbdata.ws_rule & WS_BLANK_AT_EOF) |
d68fe26f | 1741 | check_blank_at_eof(&mf1, &mf2, &ecbdata); |
c0c77734 | 1742 | ecbdata.file = o->file; |
3e97c7c6 | 1743 | ecbdata.header = header.len ? &header : NULL; |
0d21efa5 | 1744 | xpp.flags = XDF_NEED_MINIMAL | o->xdl_opts; |
ee1e5412 | 1745 | xecfg.ctxlen = o->context; |
6d0e674a | 1746 | xecfg.interhunkctxlen = o->interhunkcontext; |
6973dcae | 1747 | xecfg.flags = XDL_EMIT_FUNCNAMES; |
45e7ca0f | 1748 | if (pe) |
a013585b | 1749 | xdiff_set_find_func(&xecfg, pe->pattern, pe->cflags); |
6973dcae JH |
1750 | if (!diffopts) |
1751 | ; | |
cc44c765 | 1752 | else if (!prefixcmp(diffopts, "--unified=")) |
6973dcae | 1753 | xecfg.ctxlen = strtoul(diffopts + 10, NULL, 10); |
cc44c765 | 1754 | else if (!prefixcmp(diffopts, "-u")) |
6973dcae | 1755 | xecfg.ctxlen = strtoul(diffopts + 2, NULL, 10); |
c0c77734 | 1756 | if (DIFF_OPT_TST(o, COLOR_DIFF_WORDS)) { |
f59a59e2 JS |
1757 | ecbdata.diff_words = |
1758 | xcalloc(1, sizeof(struct diff_words_data)); | |
c0c77734 | 1759 | ecbdata.diff_words->file = o->file; |
80c49c3d TR |
1760 | if (!o->word_regex) |
1761 | o->word_regex = userdiff_word_regex(one); | |
1762 | if (!o->word_regex) | |
1763 | o->word_regex = userdiff_word_regex(two); | |
98a4d87b BSSJ |
1764 | if (!o->word_regex) |
1765 | o->word_regex = diff_word_regex_cfg; | |
2b6a5417 JS |
1766 | if (o->word_regex) { |
1767 | ecbdata.diff_words->word_regex = (regex_t *) | |
1768 | xmalloc(sizeof(regex_t)); | |
1769 | if (regcomp(ecbdata.diff_words->word_regex, | |
bf82940d TR |
1770 | o->word_regex, |
1771 | REG_EXTENDED | REG_NEWLINE)) | |
2b6a5417 JS |
1772 | die ("Invalid regular expression: %s", |
1773 | o->word_regex); | |
1774 | } | |
c0c77734 | 1775 | } |
8a3f524b | 1776 | xdi_diff_outf(&mf1, &mf2, fn_out_consume, &ecbdata, |
dfea7900 | 1777 | &xpp, &xecfg); |
8f67f8ae | 1778 | if (DIFF_OPT_TST(o, COLOR_DIFF_WORDS)) |
f59a59e2 | 1779 | free_diff_words_data(&ecbdata); |
04427ac8 JK |
1780 | if (textconv_one) |
1781 | free(mf1.ptr); | |
1782 | if (textconv_two) | |
1783 | free(mf2.ptr); | |
8cfe5f1c | 1784 | xdiff_clear_find_func(&xecfg); |
6973dcae JH |
1785 | } |
1786 | ||
1787 | free_ab_and_return: | |
3e97c7c6 | 1788 | strbuf_release(&header); |
fc3abdf5 JH |
1789 | diff_free_filespec_data(one); |
1790 | diff_free_filespec_data(two); | |
6973dcae JH |
1791 | free(a_one); |
1792 | free(b_two); | |
1793 | return; | |
1794 | } | |
1795 | ||
1796 | static void builtin_diffstat(const char *name_a, const char *name_b, | |
1797 | struct diff_filespec *one, | |
1798 | struct diff_filespec *two, | |
710158e3 | 1799 | struct diffstat_t *diffstat, |
0d21efa5 | 1800 | struct diff_options *o, |
710158e3 | 1801 | int complete_rewrite) |
6973dcae JH |
1802 | { |
1803 | mmfile_t mf1, mf2; | |
1804 | struct diffstat_file *data; | |
1805 | ||
1806 | data = diffstat_add(diffstat, name_a, name_b); | |
1807 | ||
1808 | if (!one || !two) { | |
1809 | data->is_unmerged = 1; | |
1810 | return; | |
1811 | } | |
710158e3 JH |
1812 | if (complete_rewrite) { |
1813 | diff_populate_filespec(one, 0); | |
1814 | diff_populate_filespec(two, 0); | |
1815 | data->deleted = count_lines(one->data, one->size); | |
1816 | data->added = count_lines(two->data, two->size); | |
fc3abdf5 | 1817 | goto free_and_return; |
710158e3 | 1818 | } |
6973dcae JH |
1819 | if (fill_mmfile(&mf1, one) < 0 || fill_mmfile(&mf2, two) < 0) |
1820 | die("unable to read files to diff"); | |
1821 | ||
29a3eefd | 1822 | if (diff_filespec_is_binary(one) || diff_filespec_is_binary(two)) { |
6973dcae | 1823 | data->is_binary = 1; |
b1882587 AP |
1824 | data->added = mf2.size; |
1825 | data->deleted = mf1.size; | |
1826 | } else { | |
6973dcae JH |
1827 | /* Crazy xdl interfaces.. */ |
1828 | xpparam_t xpp; | |
1829 | xdemitconf_t xecfg; | |
6973dcae | 1830 | |
9ccd0a88 | 1831 | memset(&xpp, 0, sizeof(xpp)); |
30b25010 | 1832 | memset(&xecfg, 0, sizeof(xecfg)); |
0d21efa5 | 1833 | xpp.flags = XDF_NEED_MINIMAL | o->xdl_opts; |
8a3f524b | 1834 | xdi_diff_outf(&mf1, &mf2, diffstat_consume, diffstat, |
dfea7900 | 1835 | &xpp, &xecfg); |
6973dcae | 1836 | } |
fc3abdf5 JH |
1837 | |
1838 | free_and_return: | |
1839 | diff_free_filespec_data(one); | |
1840 | diff_free_filespec_data(two); | |
6973dcae JH |
1841 | } |
1842 | ||
88246898 | 1843 | static void builtin_checkdiff(const char *name_a, const char *name_b, |
cd676a51 | 1844 | const char *attr_path, |
5ff10dd6 JH |
1845 | struct diff_filespec *one, |
1846 | struct diff_filespec *two, | |
1847 | struct diff_options *o) | |
88246898 JS |
1848 | { |
1849 | mmfile_t mf1, mf2; | |
1850 | struct checkdiff_t data; | |
1851 | ||
1852 | if (!two) | |
1853 | return; | |
1854 | ||
1855 | memset(&data, 0, sizeof(data)); | |
88246898 JS |
1856 | data.filename = name_b ? name_b : name_a; |
1857 | data.lineno = 0; | |
1ba111d1 | 1858 | data.o = o; |
cd676a51 | 1859 | data.ws_rule = whitespace_rule(attr_path); |
a757c646 | 1860 | data.conflict_marker_size = ll_merge_marker_size(attr_path); |
88246898 JS |
1861 | |
1862 | if (fill_mmfile(&mf1, one) < 0 || fill_mmfile(&mf2, two) < 0) | |
1863 | die("unable to read files to diff"); | |
1864 | ||
5ff10dd6 JH |
1865 | /* |
1866 | * All the other codepaths check both sides, but not checking | |
1867 | * the "old" side here is deliberate. We are checking the newly | |
1868 | * introduced changes, and as long as the "new" side is text, we | |
1869 | * can and should check what it introduces. | |
1870 | */ | |
29a3eefd | 1871 | if (diff_filespec_is_binary(two)) |
fc3abdf5 | 1872 | goto free_and_return; |
88246898 JS |
1873 | else { |
1874 | /* Crazy xdl interfaces.. */ | |
1875 | xpparam_t xpp; | |
1876 | xdemitconf_t xecfg; | |
88246898 | 1877 | |
9ccd0a88 | 1878 | memset(&xpp, 0, sizeof(xpp)); |
30b25010 | 1879 | memset(&xecfg, 0, sizeof(xecfg)); |
c35539eb | 1880 | xecfg.ctxlen = 1; /* at least one context line */ |
88246898 | 1881 | xpp.flags = XDF_NEED_MINIMAL; |
8a3f524b | 1882 | xdi_diff_outf(&mf1, &mf2, checkdiff_consume, &data, |
dfea7900 | 1883 | &xpp, &xecfg); |
877f23cc | 1884 | |
467babf8 | 1885 | if (data.ws_rule & WS_BLANK_AT_EOF) { |
d68fe26f JH |
1886 | struct emit_callback ecbdata; |
1887 | int blank_at_eof; | |
1888 | ||
1889 | ecbdata.ws_rule = data.ws_rule; | |
1890 | check_blank_at_eof(&mf1, &mf2, &ecbdata); | |
1891 | blank_at_eof = ecbdata.blank_at_eof_in_preimage; | |
1892 | ||
467babf8 JH |
1893 | if (blank_at_eof) { |
1894 | static char *err; | |
1895 | if (!err) | |
1896 | err = whitespace_error_string(WS_BLANK_AT_EOF); | |
1897 | fprintf(o->file, "%s:%d: %s.\n", | |
1898 | data.filename, blank_at_eof, err); | |
1899 | data.status = 1; /* report errors */ | |
1900 | } | |
877f23cc | 1901 | } |
88246898 | 1902 | } |
fc3abdf5 JH |
1903 | free_and_return: |
1904 | diff_free_filespec_data(one); | |
1905 | diff_free_filespec_data(two); | |
62c64895 WC |
1906 | if (data.status) |
1907 | DIFF_OPT_SET(o, CHECK_FAILED); | |
88246898 JS |
1908 | } |
1909 | ||
6973dcae JH |
1910 | struct diff_filespec *alloc_filespec(const char *path) |
1911 | { | |
1912 | int namelen = strlen(path); | |
1913 | struct diff_filespec *spec = xmalloc(sizeof(*spec) + namelen + 1); | |
1914 | ||
1915 | memset(spec, 0, sizeof(*spec)); | |
1916 | spec->path = (char *)(spec + 1); | |
1917 | memcpy(spec->path, path, namelen+1); | |
9fb88419 | 1918 | spec->count = 1; |
122aa6f9 | 1919 | spec->is_binary = -1; |
6973dcae JH |
1920 | return spec; |
1921 | } | |
1922 | ||
9fb88419 LT |
1923 | void free_filespec(struct diff_filespec *spec) |
1924 | { | |
1925 | if (!--spec->count) { | |
1926 | diff_free_filespec_data(spec); | |
1927 | free(spec); | |
1928 | } | |
1929 | } | |
1930 | ||
6973dcae JH |
1931 | void fill_filespec(struct diff_filespec *spec, const unsigned char *sha1, |
1932 | unsigned short mode) | |
1933 | { | |
1934 | if (mode) { | |
1935 | spec->mode = canon_mode(mode); | |
e702496e | 1936 | hashcpy(spec->sha1, sha1); |
0bef57ee | 1937 | spec->sha1_valid = !is_null_sha1(sha1); |
6973dcae JH |
1938 | } |
1939 | } | |
1940 | ||
1941 | /* | |
5adf317b | 1942 | * Given a name and sha1 pair, if the index tells us the file in |
6973dcae JH |
1943 | * the work tree has that object contents, return true, so that |
1944 | * prepare_temp_file() does not have to inflate and extract. | |
1945 | */ | |
1510fea7 | 1946 | static int reuse_worktree_file(const char *name, const unsigned char *sha1, int want_file) |
6973dcae JH |
1947 | { |
1948 | struct cache_entry *ce; | |
1949 | struct stat st; | |
1950 | int pos, len; | |
1951 | ||
150115ad JH |
1952 | /* |
1953 | * We do not read the cache ourselves here, because the | |
6973dcae JH |
1954 | * benchmark with my previous version that always reads cache |
1955 | * shows that it makes things worse for diff-tree comparing | |
1956 | * two linux-2.6 kernel trees in an already checked out work | |
1957 | * tree. This is because most diff-tree comparisons deal with | |
1958 | * only a small number of files, while reading the cache is | |
1959 | * expensive for a large project, and its cost outweighs the | |
1960 | * savings we get by not inflating the object to a temporary | |
1961 | * file. Practically, this code only helps when we are used | |
1962 | * by diff-cache --cached, which does read the cache before | |
1963 | * calling us. | |
1964 | */ | |
1965 | if (!active_cache) | |
1966 | return 0; | |
1967 | ||
1510fea7 SP |
1968 | /* We want to avoid the working directory if our caller |
1969 | * doesn't need the data in a normal file, this system | |
1970 | * is rather slow with its stat/open/mmap/close syscalls, | |
1971 | * and the object is contained in a pack file. The pack | |
1972 | * is probably already open and will be faster to obtain | |
1973 | * the data through than the working directory. Loose | |
1974 | * objects however would tend to be slower as they need | |
1975 | * to be individually opened and inflated. | |
1976 | */ | |
cd673c1f | 1977 | if (!FAST_WORKING_DIRECTORY && !want_file && has_sha1_pack(sha1)) |
1510fea7 SP |
1978 | return 0; |
1979 | ||
6973dcae JH |
1980 | len = strlen(name); |
1981 | pos = cache_name_pos(name, len); | |
1982 | if (pos < 0) | |
1983 | return 0; | |
1984 | ce = active_cache[pos]; | |
eadb5831 JH |
1985 | |
1986 | /* | |
1987 | * This is not the sha1 we are looking for, or | |
1988 | * unreusable because it is not a regular file. | |
1989 | */ | |
1990 | if (hashcmp(sha1, ce->sha1) || !S_ISREG(ce->ce_mode)) | |
6973dcae | 1991 | return 0; |
eadb5831 | 1992 | |
150115ad JH |
1993 | /* |
1994 | * If ce is marked as "assume unchanged", there is no | |
1995 | * guarantee that work tree matches what we are looking for. | |
1996 | */ | |
b4d1690d | 1997 | if ((ce->ce_flags & CE_VALID) || ce_skip_worktree(ce)) |
150115ad JH |
1998 | return 0; |
1999 | ||
eadb5831 JH |
2000 | /* |
2001 | * If ce matches the file in the work tree, we can reuse it. | |
6973dcae | 2002 | */ |
eadb5831 JH |
2003 | if (ce_uptodate(ce) || |
2004 | (!lstat(name, &st) && !ce_match_stat(ce, &st, 0))) | |
2005 | return 1; | |
2006 | ||
2007 | return 0; | |
6973dcae JH |
2008 | } |
2009 | ||
3afaa72d JH |
2010 | static int populate_from_stdin(struct diff_filespec *s) |
2011 | { | |
f285a2d7 | 2012 | struct strbuf buf = STRBUF_INIT; |
c32f749f | 2013 | size_t size = 0; |
af6eb822 | 2014 | |
f1696ee3 | 2015 | if (strbuf_read(&buf, 0, 0) < 0) |
af6eb822 | 2016 | return error("error while reading from stdin %s", |
3afaa72d | 2017 | strerror(errno)); |
af6eb822 | 2018 | |
3afaa72d | 2019 | s->should_munmap = 0; |
c32f749f RS |
2020 | s->data = strbuf_detach(&buf, &size); |
2021 | s->size = size; | |
3afaa72d JH |
2022 | s->should_free = 1; |
2023 | return 0; | |
2024 | } | |
2025 | ||
04786756 LT |
2026 | static int diff_populate_gitlink(struct diff_filespec *s, int size_only) |
2027 | { | |
2028 | int len; | |
8e08b419 JH |
2029 | char *data = xmalloc(100), *dirty = ""; |
2030 | ||
2031 | /* Are we looking at the work tree? */ | |
e3d42c47 | 2032 | if (!s->sha1_valid && s->dirty_submodule) |
8e08b419 JH |
2033 | dirty = "-dirty"; |
2034 | ||
04786756 | 2035 | len = snprintf(data, 100, |
8e08b419 | 2036 | "Subproject commit %s%s\n", sha1_to_hex(s->sha1), dirty); |
04786756 LT |
2037 | s->data = data; |
2038 | s->size = len; | |
2039 | s->should_free = 1; | |
2040 | if (size_only) { | |
2041 | s->data = NULL; | |
2042 | free(data); | |
2043 | } | |
2044 | return 0; | |
2045 | } | |
2046 | ||
6973dcae JH |
2047 | /* |
2048 | * While doing rename detection and pickaxe operation, we may need to | |
2049 | * grab the data for the blob (or file) for our own in-core comparison. | |
2050 | * diff_filespec has data and size fields for this purpose. | |
2051 | */ | |
2052 | int diff_populate_filespec(struct diff_filespec *s, int size_only) | |
2053 | { | |
2054 | int err = 0; | |
2055 | if (!DIFF_FILE_VALID(s)) | |
2056 | die("internal error: asking to populate invalid file."); | |
2057 | if (S_ISDIR(s->mode)) | |
2058 | return -1; | |
2059 | ||
6973dcae | 2060 | if (s->data) |
fc3abdf5 | 2061 | return 0; |
04786756 | 2062 | |
6e0b8ed6 JH |
2063 | if (size_only && 0 < s->size) |
2064 | return 0; | |
2065 | ||
302b9282 | 2066 | if (S_ISGITLINK(s->mode)) |
04786756 LT |
2067 | return diff_populate_gitlink(s, size_only); |
2068 | ||
6973dcae | 2069 | if (!s->sha1_valid || |
1510fea7 | 2070 | reuse_worktree_file(s->path, s->sha1, 0)) { |
f285a2d7 | 2071 | struct strbuf buf = STRBUF_INIT; |
6973dcae JH |
2072 | struct stat st; |
2073 | int fd; | |
6c510bee | 2074 | |
3afaa72d JH |
2075 | if (!strcmp(s->path, "-")) |
2076 | return populate_from_stdin(s); | |
2077 | ||
6973dcae JH |
2078 | if (lstat(s->path, &st) < 0) { |
2079 | if (errno == ENOENT) { | |
2080 | err_empty: | |
2081 | err = -1; | |
2082 | empty: | |
d2543b8e | 2083 | s->data = (char *)""; |
6973dcae JH |
2084 | s->size = 0; |
2085 | return err; | |
2086 | } | |
2087 | } | |
dc49cd76 | 2088 | s->size = xsize_t(st.st_size); |
6973dcae JH |
2089 | if (!s->size) |
2090 | goto empty; | |
6973dcae | 2091 | if (S_ISLNK(st.st_mode)) { |
cf219d8c LT |
2092 | struct strbuf sb = STRBUF_INIT; |
2093 | ||
2094 | if (strbuf_readlink(&sb, s->path, s->size)) | |
6973dcae | 2095 | goto err_empty; |
0956a6db RS |
2096 | s->size = sb.len; |
2097 | s->data = strbuf_detach(&sb, NULL); | |
cf219d8c | 2098 | s->should_free = 1; |
6973dcae JH |
2099 | return 0; |
2100 | } | |
cf219d8c LT |
2101 | if (size_only) |
2102 | return 0; | |
6973dcae JH |
2103 | fd = open(s->path, O_RDONLY); |
2104 | if (fd < 0) | |
2105 | goto err_empty; | |
c4712e45 | 2106 | s->data = xmmap(NULL, s->size, PROT_READ, MAP_PRIVATE, fd, 0); |
6973dcae | 2107 | close(fd); |
6973dcae | 2108 | s->should_munmap = 1; |
6c510bee LT |
2109 | |
2110 | /* | |
2111 | * Convert from working tree format to canonical git format | |
2112 | */ | |
21e5ad50 | 2113 | if (convert_to_git(s->path, s->data, s->size, &buf, safe_crlf)) { |
c32f749f | 2114 | size_t size = 0; |
6c510bee LT |
2115 | munmap(s->data, s->size); |
2116 | s->should_munmap = 0; | |
c32f749f RS |
2117 | s->data = strbuf_detach(&buf, &size); |
2118 | s->size = size; | |
6c510bee LT |
2119 | s->should_free = 1; |
2120 | } | |
6973dcae JH |
2121 | } |
2122 | else { | |
21666f1a | 2123 | enum object_type type; |
6e0b8ed6 | 2124 | if (size_only) |
21666f1a | 2125 | type = sha1_object_info(s->sha1, &s->size); |
6973dcae | 2126 | else { |
21666f1a | 2127 | s->data = read_sha1_file(s->sha1, &type, &s->size); |
6973dcae JH |
2128 | s->should_free = 1; |
2129 | } | |
2130 | } | |
2131 | return 0; | |
2132 | } | |
2133 | ||
8ae92e63 | 2134 | void diff_free_filespec_blob(struct diff_filespec *s) |
6973dcae JH |
2135 | { |
2136 | if (s->should_free) | |
2137 | free(s->data); | |
2138 | else if (s->should_munmap) | |
2139 | munmap(s->data, s->size); | |
fc3abdf5 JH |
2140 | |
2141 | if (s->should_free || s->should_munmap) { | |
2142 | s->should_free = s->should_munmap = 0; | |
2143 | s->data = NULL; | |
2144 | } | |
eede7b7d JK |
2145 | } |
2146 | ||
2147 | void diff_free_filespec_data(struct diff_filespec *s) | |
2148 | { | |
8ae92e63 | 2149 | diff_free_filespec_blob(s); |
6973dcae JH |
2150 | free(s->cnt_data); |
2151 | s->cnt_data = NULL; | |
2152 | } | |
2153 | ||
4e218f54 | 2154 | static void prep_temp_blob(const char *path, struct diff_tempfile *temp, |
6973dcae JH |
2155 | void *blob, |
2156 | unsigned long size, | |
2157 | const unsigned char *sha1, | |
2158 | int mode) | |
2159 | { | |
2160 | int fd; | |
4e218f54 | 2161 | struct strbuf buf = STRBUF_INIT; |
003b33a8 DA |
2162 | struct strbuf template = STRBUF_INIT; |
2163 | char *path_dup = xstrdup(path); | |
2164 | const char *base = basename(path_dup); | |
6973dcae | 2165 | |
003b33a8 DA |
2166 | /* Generate "XXXXXX_basename.ext" */ |
2167 | strbuf_addstr(&template, "XXXXXX_"); | |
2168 | strbuf_addstr(&template, base); | |
2169 | ||
2170 | fd = git_mkstemps(temp->tmp_path, PATH_MAX, template.buf, | |
2171 | strlen(base) + 1); | |
6973dcae | 2172 | if (fd < 0) |
d824cbba | 2173 | die_errno("unable to create temp-file"); |
4e218f54 JS |
2174 | if (convert_to_working_tree(path, |
2175 | (const char *)blob, (size_t)size, &buf)) { | |
2176 | blob = buf.buf; | |
2177 | size = buf.len; | |
2178 | } | |
93822c22 | 2179 | if (write_in_full(fd, blob, size) != size) |
0721c314 | 2180 | die_errno("unable to write temp-file"); |
6973dcae JH |
2181 | close(fd); |
2182 | temp->name = temp->tmp_path; | |
2183 | strcpy(temp->hex, sha1_to_hex(sha1)); | |
2184 | temp->hex[40] = 0; | |
2185 | sprintf(temp->mode, "%06o", mode); | |
4e218f54 | 2186 | strbuf_release(&buf); |
003b33a8 DA |
2187 | strbuf_release(&template); |
2188 | free(path_dup); | |
6973dcae JH |
2189 | } |
2190 | ||
479b0ae8 JK |
2191 | static struct diff_tempfile *prepare_temp_file(const char *name, |
2192 | struct diff_filespec *one) | |
6973dcae | 2193 | { |
479b0ae8 JK |
2194 | struct diff_tempfile *temp = claim_diff_tempfile(); |
2195 | ||
6973dcae JH |
2196 | if (!DIFF_FILE_VALID(one)) { |
2197 | not_a_valid_file: | |
2198 | /* A '-' entry produces this for file-2, and | |
2199 | * a '+' entry produces this for file-1. | |
2200 | */ | |
2201 | temp->name = "/dev/null"; | |
2202 | strcpy(temp->hex, "."); | |
2203 | strcpy(temp->mode, "."); | |
479b0ae8 JK |
2204 | return temp; |
2205 | } | |
2206 | ||
2207 | if (!remove_tempfile_installed) { | |
2208 | atexit(remove_tempfile); | |
57b235a4 | 2209 | sigchain_push_common(remove_tempfile_on_signal); |
479b0ae8 | 2210 | remove_tempfile_installed = 1; |
6973dcae JH |
2211 | } |
2212 | ||
2213 | if (!one->sha1_valid || | |
1510fea7 | 2214 | reuse_worktree_file(name, one->sha1, 1)) { |
6973dcae JH |
2215 | struct stat st; |
2216 | if (lstat(name, &st) < 0) { | |
2217 | if (errno == ENOENT) | |
2218 | goto not_a_valid_file; | |
d824cbba | 2219 | die_errno("stat(%s)", name); |
6973dcae JH |
2220 | } |
2221 | if (S_ISLNK(st.st_mode)) { | |
3cd7388d JK |
2222 | struct strbuf sb = STRBUF_INIT; |
2223 | if (strbuf_readlink(&sb, name, st.st_size) < 0) | |
0721c314 | 2224 | die_errno("readlink(%s)", name); |
3cd7388d | 2225 | prep_temp_blob(name, temp, sb.buf, sb.len, |
6973dcae JH |
2226 | (one->sha1_valid ? |
2227 | one->sha1 : null_sha1), | |
2228 | (one->sha1_valid ? | |
2229 | one->mode : S_IFLNK)); | |
3cd7388d | 2230 | strbuf_release(&sb); |
6973dcae JH |
2231 | } |
2232 | else { | |
2233 | /* we can borrow from the file in the work tree */ | |
2234 | temp->name = name; | |
2235 | if (!one->sha1_valid) | |
2236 | strcpy(temp->hex, sha1_to_hex(null_sha1)); | |
2237 | else | |
2238 | strcpy(temp->hex, sha1_to_hex(one->sha1)); | |
2239 | /* Even though we may sometimes borrow the | |
2240 | * contents from the work tree, we always want | |
2241 | * one->mode. mode is trustworthy even when | |
2242 | * !(one->sha1_valid), as long as | |
2243 | * DIFF_FILE_VALID(one). | |
2244 | */ | |
2245 | sprintf(temp->mode, "%06o", one->mode); | |
2246 | } | |
479b0ae8 | 2247 | return temp; |
6973dcae JH |
2248 | } |
2249 | else { | |
2250 | if (diff_populate_filespec(one, 0)) | |
2251 | die("cannot read data blob for %s", one->path); | |
4e218f54 | 2252 | prep_temp_blob(name, temp, one->data, one->size, |
6973dcae JH |
2253 | one->sha1, one->mode); |
2254 | } | |
479b0ae8 | 2255 | return temp; |
6973dcae JH |
2256 | } |
2257 | ||
6973dcae JH |
2258 | /* An external diff command takes: |
2259 | * | |
2260 | * diff-cmd name infile1 infile1-sha1 infile1-mode \ | |
2261 | * infile2 infile2-sha1 infile2-mode [ rename-to ] | |
2262 | * | |
2263 | */ | |
2264 | static void run_external_diff(const char *pgm, | |
2265 | const char *name, | |
2266 | const char *other, | |
2267 | struct diff_filespec *one, | |
2268 | struct diff_filespec *two, | |
2269 | const char *xfrm_msg, | |
2270 | int complete_rewrite) | |
2271 | { | |
2272 | const char *spawn_arg[10]; | |
6973dcae | 2273 | int retval; |
6973dcae JH |
2274 | const char **arg = &spawn_arg[0]; |
2275 | ||
6973dcae | 2276 | if (one && two) { |
479b0ae8 JK |
2277 | struct diff_tempfile *temp_one, *temp_two; |
2278 | const char *othername = (other ? other : name); | |
2279 | temp_one = prepare_temp_file(name, one); | |
2280 | temp_two = prepare_temp_file(othername, two); | |
6973dcae JH |
2281 | *arg++ = pgm; |
2282 | *arg++ = name; | |
479b0ae8 JK |
2283 | *arg++ = temp_one->name; |
2284 | *arg++ = temp_one->hex; | |
2285 | *arg++ = temp_one->mode; | |
2286 | *arg++ = temp_two->name; | |
2287 | *arg++ = temp_two->hex; | |
2288 | *arg++ = temp_two->mode; | |
6973dcae JH |
2289 | if (other) { |
2290 | *arg++ = other; | |
2291 | *arg++ = xfrm_msg; | |
2292 | } | |
2293 | } else { | |
2294 | *arg++ = pgm; | |
2295 | *arg++ = name; | |
2296 | } | |
2297 | *arg = NULL; | |
d5535ec7 | 2298 | fflush(NULL); |
7ed7fac4 | 2299 | retval = run_command_v_opt(spawn_arg, RUN_USING_SHELL); |
6973dcae JH |
2300 | remove_tempfile(); |
2301 | if (retval) { | |
2302 | fprintf(stderr, "external diff died, stopping at %s.\n", name); | |
2303 | exit(1); | |
2304 | } | |
2305 | } | |
2306 | ||
b67b9612 JH |
2307 | static int similarity_index(struct diff_filepair *p) |
2308 | { | |
2309 | return p->score * 100 / MAX_SCORE; | |
2310 | } | |
2311 | ||
2312 | static void fill_metainfo(struct strbuf *msg, | |
2313 | const char *name, | |
2314 | const char *other, | |
2315 | struct diff_filespec *one, | |
2316 | struct diff_filespec *two, | |
2317 | struct diff_options *o, | |
2318 | struct diff_filepair *p) | |
2319 | { | |
2320 | strbuf_init(msg, PATH_MAX * 2 + 300); | |
2321 | switch (p->status) { | |
2322 | case DIFF_STATUS_COPIED: | |
2323 | strbuf_addf(msg, "similarity index %d%%", similarity_index(p)); | |
2324 | strbuf_addstr(msg, "\ncopy from "); | |
2325 | quote_c_style(name, msg, NULL, 0); | |
2326 | strbuf_addstr(msg, "\ncopy to "); | |
2327 | quote_c_style(other, msg, NULL, 0); | |
2328 | strbuf_addch(msg, '\n'); | |
2329 | break; | |
2330 | case DIFF_STATUS_RENAMED: | |
2331 | strbuf_addf(msg, "similarity index %d%%", similarity_index(p)); | |
2332 | strbuf_addstr(msg, "\nrename from "); | |
2333 | quote_c_style(name, msg, NULL, 0); | |
2334 | strbuf_addstr(msg, "\nrename to "); | |
2335 | quote_c_style(other, msg, NULL, 0); | |
2336 | strbuf_addch(msg, '\n'); | |
2337 | break; | |
2338 | case DIFF_STATUS_MODIFIED: | |
2339 | if (p->score) { | |
2340 | strbuf_addf(msg, "dissimilarity index %d%%\n", | |
2341 | similarity_index(p)); | |
2342 | break; | |
2343 | } | |
2344 | /* fallthru */ | |
2345 | default: | |
2346 | /* nothing */ | |
2347 | ; | |
2348 | } | |
2349 | if (one && two && hashcmp(one->sha1, two->sha1)) { | |
2350 | int abbrev = DIFF_OPT_TST(o, FULL_INDEX) ? 40 : DEFAULT_ABBREV; | |
2351 | ||
2352 | if (DIFF_OPT_TST(o, BINARY)) { | |
2353 | mmfile_t mf; | |
2354 | if ((!fill_mmfile(&mf, one) && diff_filespec_is_binary(one)) || | |
2355 | (!fill_mmfile(&mf, two) && diff_filespec_is_binary(two))) | |
2356 | abbrev = 40; | |
2357 | } | |
2358 | strbuf_addf(msg, "index %.*s..%.*s", | |
2359 | abbrev, sha1_to_hex(one->sha1), | |
2360 | abbrev, sha1_to_hex(two->sha1)); | |
2361 | if (one->mode == two->mode) | |
2362 | strbuf_addf(msg, " %06o", one->mode); | |
2363 | strbuf_addch(msg, '\n'); | |
2364 | } | |
2365 | if (msg->len) | |
2366 | strbuf_setlen(msg, msg->len - 1); | |
2367 | } | |
2368 | ||
6973dcae JH |
2369 | static void run_diff_cmd(const char *pgm, |
2370 | const char *name, | |
2371 | const char *other, | |
cd676a51 | 2372 | const char *attr_path, |
6973dcae JH |
2373 | struct diff_filespec *one, |
2374 | struct diff_filespec *two, | |
b67b9612 | 2375 | struct strbuf *msg, |
051308f6 | 2376 | struct diff_options *o, |
b67b9612 | 2377 | struct diff_filepair *p) |
6973dcae | 2378 | { |
b67b9612 JH |
2379 | const char *xfrm_msg = NULL; |
2380 | int complete_rewrite = (p->status == DIFF_STATUS_MODIFIED) && p->score; | |
2381 | ||
2382 | if (msg) { | |
2383 | fill_metainfo(msg, name, other, one, two, o, p); | |
2384 | xfrm_msg = msg->len ? msg->buf : NULL; | |
2385 | } | |
2386 | ||
8f67f8ae | 2387 | if (!DIFF_OPT_TST(o, ALLOW_EXTERNAL)) |
f1af60bd JH |
2388 | pgm = NULL; |
2389 | else { | |
be58e70d JK |
2390 | struct userdiff_driver *drv = userdiff_find_by_path(attr_path); |
2391 | if (drv && drv->external) | |
2392 | pgm = drv->external; | |
f1af60bd JH |
2393 | } |
2394 | ||
6973dcae JH |
2395 | if (pgm) { |
2396 | run_external_diff(pgm, name, other, one, two, xfrm_msg, | |
2397 | complete_rewrite); | |
2398 | return; | |
2399 | } | |
2400 | if (one && two) | |
2401 | builtin_diff(name, other ? other : name, | |
051308f6 | 2402 | one, two, xfrm_msg, o, complete_rewrite); |
6973dcae | 2403 | else |
c0c77734 | 2404 | fprintf(o->file, "* Unmerged path %s\n", name); |
6973dcae JH |
2405 | } |
2406 | ||
2407 | static void diff_fill_sha1_info(struct diff_filespec *one) | |
2408 | { | |
2409 | if (DIFF_FILE_VALID(one)) { | |
2410 | if (!one->sha1_valid) { | |
2411 | struct stat st; | |
5332b2af JS |
2412 | if (!strcmp(one->path, "-")) { |
2413 | hashcpy(one->sha1, null_sha1); | |
2414 | return; | |
2415 | } | |
6973dcae | 2416 | if (lstat(one->path, &st) < 0) |
0721c314 | 2417 | die_errno("stat '%s'", one->path); |
6973dcae | 2418 | if (index_path(one->sha1, one->path, &st, 0)) |
d7530708 | 2419 | die("cannot hash %s", one->path); |
6973dcae JH |
2420 | } |
2421 | } | |
2422 | else | |
e702496e | 2423 | hashclr(one->sha1); |
6973dcae JH |
2424 | } |
2425 | ||
cd676a51 JH |
2426 | static void strip_prefix(int prefix_length, const char **namep, const char **otherp) |
2427 | { | |
2428 | /* Strip the prefix but do not molest /dev/null and absolute paths */ | |
2429 | if (*namep && **namep != '/') | |
2430 | *namep += prefix_length; | |
2431 | if (*otherp && **otherp != '/') | |
2432 | *otherp += prefix_length; | |
2433 | } | |
2434 | ||
6973dcae JH |
2435 | static void run_diff(struct diff_filepair *p, struct diff_options *o) |
2436 | { | |
2437 | const char *pgm = external_diff(); | |
663af342 | 2438 | struct strbuf msg; |
663af342 PH |
2439 | struct diff_filespec *one = p->one; |
2440 | struct diff_filespec *two = p->two; | |
6973dcae JH |
2441 | const char *name; |
2442 | const char *other; | |
cd676a51 | 2443 | const char *attr_path; |
663af342 | 2444 | |
cd676a51 JH |
2445 | name = p->one->path; |
2446 | other = (strcmp(name, p->two->path) ? p->two->path : NULL); | |
2447 | attr_path = name; | |
2448 | if (o->prefix_length) | |
2449 | strip_prefix(o->prefix_length, &name, &other); | |
6973dcae JH |
2450 | |
2451 | if (DIFF_PAIR_UNMERGED(p)) { | |
cd676a51 | 2452 | run_diff_cmd(pgm, name, NULL, attr_path, |
b67b9612 | 2453 | NULL, NULL, NULL, o, p); |
6973dcae JH |
2454 | return; |
2455 | } | |
2456 | ||
6973dcae JH |
2457 | diff_fill_sha1_info(one); |
2458 | diff_fill_sha1_info(two); | |
2459 | ||
6973dcae JH |
2460 | if (!pgm && |
2461 | DIFF_FILE_VALID(one) && DIFF_FILE_VALID(two) && | |
2462 | (S_IFMT & one->mode) != (S_IFMT & two->mode)) { | |
b67b9612 JH |
2463 | /* |
2464 | * a filepair that changes between file and symlink | |
6973dcae JH |
2465 | * needs to be split into deletion and creation. |
2466 | */ | |
2467 | struct diff_filespec *null = alloc_filespec(two->path); | |
cd676a51 | 2468 | run_diff_cmd(NULL, name, other, attr_path, |
b67b9612 | 2469 | one, null, &msg, o, p); |
6973dcae | 2470 | free(null); |
b67b9612 JH |
2471 | strbuf_release(&msg); |
2472 | ||
6973dcae | 2473 | null = alloc_filespec(one->path); |
cd676a51 | 2474 | run_diff_cmd(NULL, name, other, attr_path, |
b67b9612 | 2475 | null, two, &msg, o, p); |
6973dcae JH |
2476 | free(null); |
2477 | } | |
2478 | else | |
cd676a51 | 2479 | run_diff_cmd(pgm, name, other, attr_path, |
b67b9612 | 2480 | one, two, &msg, o, p); |
6973dcae | 2481 | |
663af342 | 2482 | strbuf_release(&msg); |
6973dcae JH |
2483 | } |
2484 | ||
2485 | static void run_diffstat(struct diff_filepair *p, struct diff_options *o, | |
2486 | struct diffstat_t *diffstat) | |
2487 | { | |
2488 | const char *name; | |
2489 | const char *other; | |
710158e3 | 2490 | int complete_rewrite = 0; |
6973dcae JH |
2491 | |
2492 | if (DIFF_PAIR_UNMERGED(p)) { | |
2493 | /* unmerged */ | |
0d21efa5 | 2494 | builtin_diffstat(p->one->path, NULL, NULL, NULL, diffstat, o, 0); |
6973dcae JH |
2495 | return; |
2496 | } | |
2497 | ||
2498 | name = p->one->path; | |
2499 | other = (strcmp(name, p->two->path) ? p->two->path : NULL); | |
2500 | ||
cd676a51 JH |
2501 | if (o->prefix_length) |
2502 | strip_prefix(o->prefix_length, &name, &other); | |
2503 | ||
6973dcae JH |
2504 | diff_fill_sha1_info(p->one); |
2505 | diff_fill_sha1_info(p->two); | |
2506 | ||
710158e3 JH |
2507 | if (p->status == DIFF_STATUS_MODIFIED && p->score) |
2508 | complete_rewrite = 1; | |
0d21efa5 | 2509 | builtin_diffstat(name, other, p->one, p->two, diffstat, o, complete_rewrite); |
6973dcae JH |
2510 | } |
2511 | ||
88246898 JS |
2512 | static void run_checkdiff(struct diff_filepair *p, struct diff_options *o) |
2513 | { | |
2514 | const char *name; | |
2515 | const char *other; | |
cd676a51 | 2516 | const char *attr_path; |
88246898 JS |
2517 | |
2518 | if (DIFF_PAIR_UNMERGED(p)) { | |
2519 | /* unmerged */ | |
2520 | return; | |
2521 | } | |
2522 | ||
2523 | name = p->one->path; | |
2524 | other = (strcmp(name, p->two->path) ? p->two->path : NULL); | |
cd676a51 JH |
2525 | attr_path = other ? other : name; |
2526 | ||
2527 | if (o->prefix_length) | |
2528 | strip_prefix(o->prefix_length, &name, &other); | |
88246898 JS |
2529 | |
2530 | diff_fill_sha1_info(p->one); | |
2531 | diff_fill_sha1_info(p->two); | |
2532 | ||
cd676a51 | 2533 | builtin_checkdiff(name, other, attr_path, p->one, p->two, o); |
88246898 JS |
2534 | } |
2535 | ||
6973dcae JH |
2536 | void diff_setup(struct diff_options *options) |
2537 | { | |
2538 | memset(options, 0, sizeof(*options)); | |
c0c77734 DB |
2539 | |
2540 | options->file = stdout; | |
2541 | ||
6973dcae JH |
2542 | options->line_termination = '\n'; |
2543 | options->break_opt = -1; | |
2544 | options->rename_limit = -1; | |
7df7c019 | 2545 | options->dirstat_percent = 3; |
ee1e5412 | 2546 | options->context = 3; |
6973dcae JH |
2547 | |
2548 | options->change = diff_change; | |
2549 | options->add_remove = diff_addremove; | |
6b2f2d98 | 2550 | if (diff_use_color_default > 0) |
8f67f8ae | 2551 | DIFF_OPT_SET(options, COLOR_DIFF); |
b68ea12e | 2552 | options->detect_rename = diff_detect_rename_default; |
eab9a40b | 2553 | |
a5a818ee JH |
2554 | if (!diff_mnemonic_prefix) { |
2555 | options->a_prefix = "a/"; | |
2556 | options->b_prefix = "b/"; | |
2557 | } | |
6973dcae JH |
2558 | } |
2559 | ||
2560 | int diff_setup_done(struct diff_options *options) | |
2561 | { | |
d7de00f7 TH |
2562 | int count = 0; |
2563 | ||
2564 | if (options->output_format & DIFF_FORMAT_NAME) | |
2565 | count++; | |
2566 | if (options->output_format & DIFF_FORMAT_NAME_STATUS) | |
2567 | count++; | |
2568 | if (options->output_format & DIFF_FORMAT_CHECKDIFF) | |
2569 | count++; | |
2570 | if (options->output_format & DIFF_FORMAT_NO_OUTPUT) | |
2571 | count++; | |
2572 | if (count > 1) | |
2573 | die("--name-only, --name-status, --check and -s are mutually exclusive"); | |
2574 | ||
f245194f JH |
2575 | /* |
2576 | * Most of the time we can say "there are changes" | |
2577 | * only by checking if there are changed paths, but | |
2578 | * --ignore-whitespace* options force us to look | |
97bf2a08 | 2579 | * inside contents. |
f245194f JH |
2580 | */ |
2581 | ||
2582 | if (DIFF_XDL_TST(options, IGNORE_WHITESPACE) || | |
2583 | DIFF_XDL_TST(options, IGNORE_WHITESPACE_CHANGE) || | |
2584 | DIFF_XDL_TST(options, IGNORE_WHITESPACE_AT_EOL)) | |
2585 | DIFF_OPT_SET(options, DIFF_FROM_CONTENTS); | |
2586 | else | |
2587 | DIFF_OPT_CLR(options, DIFF_FROM_CONTENTS); | |
2588 | ||
8f67f8ae | 2589 | if (DIFF_OPT_TST(options, FIND_COPIES_HARDER)) |
03b9d560 JH |
2590 | options->detect_rename = DIFF_DETECT_COPY; |
2591 | ||
cd676a51 JH |
2592 | if (!DIFF_OPT_TST(options, RELATIVE_NAME)) |
2593 | options->prefix = NULL; | |
2594 | if (options->prefix) | |
2595 | options->prefix_length = strlen(options->prefix); | |
2596 | else | |
2597 | options->prefix_length = 0; | |
2598 | ||
c6744349 TH |
2599 | if (options->output_format & (DIFF_FORMAT_NAME | |
2600 | DIFF_FORMAT_NAME_STATUS | | |
2601 | DIFF_FORMAT_CHECKDIFF | | |
2602 | DIFF_FORMAT_NO_OUTPUT)) | |
2603 | options->output_format &= ~(DIFF_FORMAT_RAW | | |
74e2abe5 | 2604 | DIFF_FORMAT_NUMSTAT | |
c6744349 | 2605 | DIFF_FORMAT_DIFFSTAT | |
ebd124c6 | 2606 | DIFF_FORMAT_SHORTSTAT | |
7df7c019 | 2607 | DIFF_FORMAT_DIRSTAT | |
c6744349 TH |
2608 | DIFF_FORMAT_SUMMARY | |
2609 | DIFF_FORMAT_PATCH); | |
2610 | ||
6973dcae JH |
2611 | /* |
2612 | * These cases always need recursive; we do not drop caller-supplied | |
2613 | * recursive bits for other formats here. | |
2614 | */ | |
c6744349 | 2615 | if (options->output_format & (DIFF_FORMAT_PATCH | |
74e2abe5 | 2616 | DIFF_FORMAT_NUMSTAT | |
c6744349 | 2617 | DIFF_FORMAT_DIFFSTAT | |
ebd124c6 | 2618 | DIFF_FORMAT_SHORTSTAT | |
7df7c019 | 2619 | DIFF_FORMAT_DIRSTAT | |
d7014dc0 | 2620 | DIFF_FORMAT_SUMMARY | |
c6744349 | 2621 | DIFF_FORMAT_CHECKDIFF)) |
8f67f8ae | 2622 | DIFF_OPT_SET(options, RECURSIVE); |
5e363541 | 2623 | /* |
3969cf7d | 2624 | * Also pickaxe would not work very well if you do not say recursive |
5e363541 | 2625 | */ |
3969cf7d | 2626 | if (options->pickaxe) |
8f67f8ae | 2627 | DIFF_OPT_SET(options, RECURSIVE); |
5e363541 | 2628 | |
6973dcae JH |
2629 | if (options->detect_rename && options->rename_limit < 0) |
2630 | options->rename_limit = diff_rename_limit_default; | |
2631 | if (options->setup & DIFF_SETUP_USE_CACHE) { | |
2632 | if (!active_cache) | |
2633 | /* read-cache does not die even when it fails | |
2634 | * so it is safe for us to do this here. Also | |
2635 | * it does not smudge active_cache or active_nr | |
2636 | * when it fails, so we do not have to worry about | |
2637 | * cleaning it up ourselves either. | |
2638 | */ | |
2639 | read_cache(); | |
2640 | } | |
6973dcae JH |
2641 | if (options->abbrev <= 0 || 40 < options->abbrev) |
2642 | options->abbrev = 40; /* full */ | |
2643 | ||
68aacb2f JH |
2644 | /* |
2645 | * It does not make sense to show the first hit we happened | |
2646 | * to have found. It does not make sense not to return with | |
2647 | * exit code in such a case either. | |
2648 | */ | |
90b19941 | 2649 | if (DIFF_OPT_TST(options, QUICK)) { |
68aacb2f | 2650 | options->output_format = DIFF_FORMAT_NO_OUTPUT; |
8f67f8ae | 2651 | DIFF_OPT_SET(options, EXIT_WITH_STATUS); |
68aacb2f JH |
2652 | } |
2653 | ||
6973dcae JH |
2654 | return 0; |
2655 | } | |
2656 | ||
d2543b8e | 2657 | static int opt_arg(const char *arg, int arg_short, const char *arg_long, int *val) |
ee1e5412 LT |
2658 | { |
2659 | char c, *eq; | |
2660 | int len; | |
2661 | ||
2662 | if (*arg != '-') | |
2663 | return 0; | |
2664 | c = *++arg; | |
2665 | if (!c) | |
2666 | return 0; | |
2667 | if (c == arg_short) { | |
2668 | c = *++arg; | |
2669 | if (!c) | |
2670 | return 1; | |
2671 | if (val && isdigit(c)) { | |
2672 | char *end; | |
2673 | int n = strtoul(arg, &end, 10); | |
2674 | if (*end) | |
2675 | return 0; | |
2676 | *val = n; | |
2677 | return 1; | |
2678 | } | |
2679 | return 0; | |
2680 | } | |
2681 | if (c != '-') | |
2682 | return 0; | |
2683 | arg++; | |
2684 | eq = strchr(arg, '='); | |
2685 | if (eq) | |
2686 | len = eq - arg; | |
2687 | else | |
2688 | len = strlen(arg); | |
2689 | if (!len || strncmp(arg, arg_long, len)) | |
2690 | return 0; | |
2691 | if (eq) { | |
2692 | int n; | |
2693 | char *end; | |
2694 | if (!isdigit(*++eq)) | |
2695 | return 0; | |
2696 | n = strtoul(eq, &end, 10); | |
2697 | if (*end) | |
2698 | return 0; | |
2699 | *val = n; | |
2700 | } | |
2701 | return 1; | |
2702 | } | |
2703 | ||
16befb8b JH |
2704 | static int diff_scoreopt_parse(const char *opt); |
2705 | ||
6973dcae JH |
2706 | int diff_opt_parse(struct diff_options *options, const char **av, int ac) |
2707 | { | |
2708 | const char *arg = av[0]; | |
d054680c PH |
2709 | |
2710 | /* Output format options */ | |
6973dcae | 2711 | if (!strcmp(arg, "-p") || !strcmp(arg, "-u")) |
c6744349 | 2712 | options->output_format |= DIFF_FORMAT_PATCH; |
ee1e5412 | 2713 | else if (opt_arg(arg, 'U', "unified", &options->context)) |
c6744349 | 2714 | options->output_format |= DIFF_FORMAT_PATCH; |
a610786f TH |
2715 | else if (!strcmp(arg, "--raw")) |
2716 | options->output_format |= DIFF_FORMAT_RAW; | |
8f67f8ae | 2717 | else if (!strcmp(arg, "--patch-with-raw")) |
c6744349 | 2718 | options->output_format |= DIFF_FORMAT_PATCH | DIFF_FORMAT_RAW; |
8f67f8ae | 2719 | else if (!strcmp(arg, "--numstat")) |
74e2abe5 | 2720 | options->output_format |= DIFF_FORMAT_NUMSTAT; |
8f67f8ae | 2721 | else if (!strcmp(arg, "--shortstat")) |
ebd124c6 | 2722 | options->output_format |= DIFF_FORMAT_SHORTSTAT; |
7df7c019 LT |
2723 | else if (opt_arg(arg, 'X', "dirstat", &options->dirstat_percent)) |
2724 | options->output_format |= DIFF_FORMAT_DIRSTAT; | |
f88d225f JH |
2725 | else if (!strcmp(arg, "--cumulative")) { |
2726 | options->output_format |= DIFF_FORMAT_DIRSTAT; | |
2727 | DIFF_OPT_SET(options, DIRSTAT_CUMULATIVE); | |
fd33777b HO |
2728 | } else if (opt_arg(arg, 0, "dirstat-by-file", |
2729 | &options->dirstat_percent)) { | |
2730 | options->output_format |= DIFF_FORMAT_DIRSTAT; | |
2731 | DIFF_OPT_SET(options, DIRSTAT_BY_FILE); | |
f88d225f | 2732 | } |
d054680c PH |
2733 | else if (!strcmp(arg, "--check")) |
2734 | options->output_format |= DIFF_FORMAT_CHECKDIFF; | |
2735 | else if (!strcmp(arg, "--summary")) | |
2736 | options->output_format |= DIFF_FORMAT_SUMMARY; | |
2737 | else if (!strcmp(arg, "--patch-with-stat")) | |
2738 | options->output_format |= DIFF_FORMAT_PATCH | DIFF_FORMAT_DIFFSTAT; | |
2739 | else if (!strcmp(arg, "--name-only")) | |
2740 | options->output_format |= DIFF_FORMAT_NAME; | |
2741 | else if (!strcmp(arg, "--name-status")) | |
2742 | options->output_format |= DIFF_FORMAT_NAME_STATUS; | |
2743 | else if (!strcmp(arg, "-s")) | |
2744 | options->output_format |= DIFF_FORMAT_NO_OUTPUT; | |
cc44c765 | 2745 | else if (!prefixcmp(arg, "--stat")) { |
5c5b2ea9 LT |
2746 | char *end; |
2747 | int width = options->stat_width; | |
2748 | int name_width = options->stat_name_width; | |
2749 | arg += 6; | |
2750 | end = (char *)arg; | |
2751 | ||
2752 | switch (*arg) { | |
2753 | case '-': | |
cc44c765 | 2754 | if (!prefixcmp(arg, "-width=")) |
5c5b2ea9 | 2755 | width = strtoul(arg + 7, &end, 10); |
cc44c765 | 2756 | else if (!prefixcmp(arg, "-name-width=")) |
5c5b2ea9 LT |
2757 | name_width = strtoul(arg + 12, &end, 10); |
2758 | break; | |
2759 | case '=': | |
2760 | width = strtoul(arg+1, &end, 10); | |
2761 | if (*end == ',') | |
2762 | name_width = strtoul(end+1, &end, 10); | |
2763 | } | |
2764 | ||
2765 | /* Important! This checks all the error cases! */ | |
2766 | if (*end) | |
2767 | return 0; | |
c6744349 | 2768 | options->output_format |= DIFF_FORMAT_DIFFSTAT; |
5c5b2ea9 LT |
2769 | options->stat_name_width = name_width; |
2770 | options->stat_width = width; | |
a2540023 | 2771 | } |
d054680c PH |
2772 | |
2773 | /* renames options */ | |
cc44c765 | 2774 | else if (!prefixcmp(arg, "-B")) { |
8f67f8ae | 2775 | if ((options->break_opt = diff_scoreopt_parse(arg)) == -1) |
6973dcae JH |
2776 | return -1; |
2777 | } | |
cc44c765 | 2778 | else if (!prefixcmp(arg, "-M")) { |
8f67f8ae | 2779 | if ((options->rename_score = diff_scoreopt_parse(arg)) == -1) |
6973dcae JH |
2780 | return -1; |
2781 | options->detect_rename = DIFF_DETECT_RENAME; | |
2782 | } | |
cc44c765 | 2783 | else if (!prefixcmp(arg, "-C")) { |
ca6c0970 | 2784 | if (options->detect_rename == DIFF_DETECT_COPY) |
8f67f8ae PH |
2785 | DIFF_OPT_SET(options, FIND_COPIES_HARDER); |
2786 | if ((options->rename_score = diff_scoreopt_parse(arg)) == -1) | |
6973dcae JH |
2787 | return -1; |
2788 | options->detect_rename = DIFF_DETECT_COPY; | |
2789 | } | |
d054680c PH |
2790 | else if (!strcmp(arg, "--no-renames")) |
2791 | options->detect_rename = 0; | |
cd676a51 JH |
2792 | else if (!strcmp(arg, "--relative")) |
2793 | DIFF_OPT_SET(options, RELATIVE_NAME); | |
c0cb4a06 JH |
2794 | else if (!prefixcmp(arg, "--relative=")) { |
2795 | DIFF_OPT_SET(options, RELATIVE_NAME); | |
2796 | options->prefix = arg + 11; | |
2797 | } | |
d054680c PH |
2798 | |
2799 | /* xdiff options */ | |
2800 | else if (!strcmp(arg, "-w") || !strcmp(arg, "--ignore-all-space")) | |
628d5c2b | 2801 | DIFF_XDL_SET(options, IGNORE_WHITESPACE); |
d054680c | 2802 | else if (!strcmp(arg, "-b") || !strcmp(arg, "--ignore-space-change")) |
628d5c2b | 2803 | DIFF_XDL_SET(options, IGNORE_WHITESPACE_CHANGE); |
d054680c | 2804 | else if (!strcmp(arg, "--ignore-space-at-eol")) |
628d5c2b | 2805 | DIFF_XDL_SET(options, IGNORE_WHITESPACE_AT_EOL); |
34292bdd | 2806 | else if (!strcmp(arg, "--patience")) |
628d5c2b | 2807 | DIFF_XDL_SET(options, PATIENCE_DIFF); |
d054680c PH |
2808 | |
2809 | /* flags options */ | |
2810 | else if (!strcmp(arg, "--binary")) { | |
2811 | options->output_format |= DIFF_FORMAT_PATCH; | |
2812 | DIFF_OPT_SET(options, BINARY); | |
2813 | } | |
2814 | else if (!strcmp(arg, "--full-index")) | |
2815 | DIFF_OPT_SET(options, FULL_INDEX); | |
2816 | else if (!strcmp(arg, "-a") || !strcmp(arg, "--text")) | |
2817 | DIFF_OPT_SET(options, TEXT); | |
2818 | else if (!strcmp(arg, "-R")) | |
2819 | DIFF_OPT_SET(options, REVERSE_DIFF); | |
6973dcae | 2820 | else if (!strcmp(arg, "--find-copies-harder")) |
8f67f8ae | 2821 | DIFF_OPT_SET(options, FIND_COPIES_HARDER); |
750f7b66 | 2822 | else if (!strcmp(arg, "--follow")) |
8f67f8ae | 2823 | DIFF_OPT_SET(options, FOLLOW_RENAMES); |
cd112cef | 2824 | else if (!strcmp(arg, "--color")) |
8f67f8ae | 2825 | DIFF_OPT_SET(options, COLOR_DIFF); |
fef88bb0 | 2826 | else if (!strcmp(arg, "--no-color")) |
8f67f8ae | 2827 | DIFF_OPT_CLR(options, COLOR_DIFF); |
628d5c2b KC |
2828 | else if (!strcmp(arg, "--color-words")) { |
2829 | DIFF_OPT_SET(options, COLOR_DIFF); | |
2830 | DIFF_OPT_SET(options, COLOR_DIFF_WORDS); | |
2831 | } | |
2b6a5417 | 2832 | else if (!prefixcmp(arg, "--color-words=")) { |
628d5c2b KC |
2833 | DIFF_OPT_SET(options, COLOR_DIFF); |
2834 | DIFF_OPT_SET(options, COLOR_DIFF_WORDS); | |
2b6a5417 JS |
2835 | options->word_regex = arg + 14; |
2836 | } | |
41bbf9d5 | 2837 | else if (!strcmp(arg, "--exit-code")) |
8f67f8ae | 2838 | DIFF_OPT_SET(options, EXIT_WITH_STATUS); |
68aacb2f | 2839 | else if (!strcmp(arg, "--quiet")) |
90b19941 | 2840 | DIFF_OPT_SET(options, QUICK); |
72909bef | 2841 | else if (!strcmp(arg, "--ext-diff")) |
8f67f8ae | 2842 | DIFF_OPT_SET(options, ALLOW_EXTERNAL); |
72909bef | 2843 | else if (!strcmp(arg, "--no-ext-diff")) |
8f67f8ae | 2844 | DIFF_OPT_CLR(options, ALLOW_EXTERNAL); |
e10ea812 JK |
2845 | else if (!strcmp(arg, "--textconv")) |
2846 | DIFF_OPT_SET(options, ALLOW_TEXTCONV); | |
2847 | else if (!strcmp(arg, "--no-textconv")) | |
2848 | DIFF_OPT_CLR(options, ALLOW_TEXTCONV); | |
50fd9bd8 JS |
2849 | else if (!strcmp(arg, "--ignore-submodules")) |
2850 | DIFF_OPT_SET(options, IGNORE_SUBMODULES); | |
752c0c24 JS |
2851 | else if (!strcmp(arg, "--submodule")) |
2852 | DIFF_OPT_SET(options, SUBMODULE_LOG); | |
2853 | else if (!prefixcmp(arg, "--submodule=")) { | |
2854 | if (!strcmp(arg + 12, "log")) | |
2855 | DIFF_OPT_SET(options, SUBMODULE_LOG); | |
2856 | } | |
d054680c PH |
2857 | |
2858 | /* misc options */ | |
2859 | else if (!strcmp(arg, "-z")) | |
2860 | options->line_termination = 0; | |
2861 | else if (!prefixcmp(arg, "-l")) | |
2862 | options->rename_limit = strtoul(arg+2, NULL, 10); | |
2863 | else if (!prefixcmp(arg, "-S")) | |
2864 | options->pickaxe = arg + 2; | |
2865 | else if (!strcmp(arg, "--pickaxe-all")) | |
2866 | options->pickaxe_opts = DIFF_PICKAXE_ALL; | |
2867 | else if (!strcmp(arg, "--pickaxe-regex")) | |
2868 | options->pickaxe_opts = DIFF_PICKAXE_REGEX; | |
2869 | else if (!prefixcmp(arg, "-O")) | |
2870 | options->orderfile = arg + 2; | |
2871 | else if (!prefixcmp(arg, "--diff-filter=")) | |
2872 | options->filter = arg + 14; | |
2873 | else if (!strcmp(arg, "--abbrev")) | |
2874 | options->abbrev = DEFAULT_ABBREV; | |
2875 | else if (!prefixcmp(arg, "--abbrev=")) { | |
2876 | options->abbrev = strtoul(arg + 9, NULL, 10); | |
2877 | if (options->abbrev < MINIMUM_ABBREV) | |
2878 | options->abbrev = MINIMUM_ABBREV; | |
2879 | else if (40 < options->abbrev) | |
2880 | options->abbrev = 40; | |
2881 | } | |
eab9a40b JS |
2882 | else if (!prefixcmp(arg, "--src-prefix=")) |
2883 | options->a_prefix = arg + 13; | |
2884 | else if (!prefixcmp(arg, "--dst-prefix=")) | |
2885 | options->b_prefix = arg + 13; | |
2886 | else if (!strcmp(arg, "--no-prefix")) | |
2887 | options->a_prefix = options->b_prefix = ""; | |
6d0e674a RS |
2888 | else if (opt_arg(arg, '\0', "inter-hunk-context", |
2889 | &options->interhunkcontext)) | |
2890 | ; | |
c0c77734 DB |
2891 | else if (!prefixcmp(arg, "--output=")) { |
2892 | options->file = fopen(arg + strlen("--output="), "w"); | |
8324b977 LA |
2893 | if (!options->file) |
2894 | die_errno("Could not open '%s'", arg + strlen("--output=")); | |
c0c77734 DB |
2895 | options->close_file = 1; |
2896 | } else | |
6973dcae JH |
2897 | return 0; |
2898 | return 1; | |
2899 | } | |
2900 | ||
2901 | static int parse_num(const char **cp_p) | |
2902 | { | |
2903 | unsigned long num, scale; | |
2904 | int ch, dot; | |
2905 | const char *cp = *cp_p; | |
2906 | ||
2907 | num = 0; | |
2908 | scale = 1; | |
2909 | dot = 0; | |
eeefa7c9 | 2910 | for (;;) { |
6973dcae JH |
2911 | ch = *cp; |
2912 | if ( !dot && ch == '.' ) { | |
2913 | scale = 1; | |
2914 | dot = 1; | |
2915 | } else if ( ch == '%' ) { | |
2916 | scale = dot ? scale*100 : 100; | |
2917 | cp++; /* % is always at the end */ | |
2918 | break; | |
2919 | } else if ( ch >= '0' && ch <= '9' ) { | |
2920 | if ( scale < 100000 ) { | |
2921 | scale *= 10; | |
2922 | num = (num*10) + (ch-'0'); | |
2923 | } | |
2924 | } else { | |
2925 | break; | |
2926 | } | |
2927 | cp++; | |
2928 | } | |
2929 | *cp_p = cp; | |
2930 | ||
2931 | /* user says num divided by scale and we say internally that | |
2932 | * is MAX_SCORE * num / scale. | |
2933 | */ | |
dc49cd76 | 2934 | return (int)((num >= scale) ? MAX_SCORE : (MAX_SCORE * num / scale)); |
6973dcae JH |
2935 | } |
2936 | ||
16befb8b | 2937 | static int diff_scoreopt_parse(const char *opt) |
6973dcae JH |
2938 | { |
2939 | int opt1, opt2, cmd; | |
2940 | ||
2941 | if (*opt++ != '-') | |
2942 | return -1; | |
2943 | cmd = *opt++; | |
2944 | if (cmd != 'M' && cmd != 'C' && cmd != 'B') | |
2945 | return -1; /* that is not a -M, -C nor -B option */ | |
2946 | ||
2947 | opt1 = parse_num(&opt); | |
2948 | if (cmd != 'B') | |
2949 | opt2 = 0; | |
2950 | else { | |
2951 | if (*opt == 0) | |
2952 | opt2 = 0; | |
2953 | else if (*opt != '/') | |
2954 | return -1; /* we expect -B80/99 or -B80 */ | |
2955 | else { | |
2956 | opt++; | |
2957 | opt2 = parse_num(&opt); | |
2958 | } | |
2959 | } | |
2960 | if (*opt != 0) | |
2961 | return -1; | |
2962 | return opt1 | (opt2 << 16); | |
2963 | } | |
2964 | ||
2965 | struct diff_queue_struct diff_queued_diff; | |
2966 | ||
2967 | void diff_q(struct diff_queue_struct *queue, struct diff_filepair *dp) | |
2968 | { | |
2969 | if (queue->alloc <= queue->nr) { | |
2970 | queue->alloc = alloc_nr(queue->alloc); | |
2971 | queue->queue = xrealloc(queue->queue, | |
2972 | sizeof(dp) * queue->alloc); | |
2973 | } | |
2974 | queue->queue[queue->nr++] = dp; | |
2975 | } | |
2976 | ||
2977 | struct diff_filepair *diff_queue(struct diff_queue_struct *queue, | |
2978 | struct diff_filespec *one, | |
2979 | struct diff_filespec *two) | |
2980 | { | |
ef677686 | 2981 | struct diff_filepair *dp = xcalloc(1, sizeof(*dp)); |
6973dcae JH |
2982 | dp->one = one; |
2983 | dp->two = two; | |
6973dcae JH |
2984 | if (queue) |
2985 | diff_q(queue, dp); | |
2986 | return dp; | |
2987 | } | |
2988 | ||
2989 | void diff_free_filepair(struct diff_filepair *p) | |
2990 | { | |
9fb88419 LT |
2991 | free_filespec(p->one); |
2992 | free_filespec(p->two); | |
6973dcae JH |
2993 | free(p); |
2994 | } | |
2995 | ||
2996 | /* This is different from find_unique_abbrev() in that | |
2997 | * it stuffs the result with dots for alignment. | |
2998 | */ | |
2999 | const char *diff_unique_abbrev(const unsigned char *sha1, int len) | |
3000 | { | |
3001 | int abblen; | |
3002 | const char *abbrev; | |
3003 | if (len == 40) | |
3004 | return sha1_to_hex(sha1); | |
3005 | ||
3006 | abbrev = find_unique_abbrev(sha1, len); | |
6973dcae JH |
3007 | abblen = strlen(abbrev); |
3008 | if (abblen < 37) { | |
3009 | static char hex[41]; | |
3010 | if (len < abblen && abblen <= len + 2) | |
3011 | sprintf(hex, "%s%.*s", abbrev, len+3-abblen, ".."); | |
3012 | else | |
3013 | sprintf(hex, "%s...", abbrev); | |
3014 | return hex; | |
3015 | } | |
3016 | return sha1_to_hex(sha1); | |
3017 | } | |
3018 | ||
663af342 | 3019 | static void diff_flush_raw(struct diff_filepair *p, struct diff_options *opt) |
6973dcae | 3020 | { |
663af342 PH |
3021 | int line_termination = opt->line_termination; |
3022 | int inter_name_termination = line_termination ? '\t' : '\0'; | |
6973dcae | 3023 | |
663af342 | 3024 | if (!(opt->output_format & DIFF_FORMAT_NAME_STATUS)) { |
c0c77734 DB |
3025 | fprintf(opt->file, ":%06o %06o %s ", p->one->mode, p->two->mode, |
3026 | diff_unique_abbrev(p->one->sha1, opt->abbrev)); | |
3027 | fprintf(opt->file, "%s ", diff_unique_abbrev(p->two->sha1, opt->abbrev)); | |
6973dcae | 3028 | } |
663af342 | 3029 | if (p->score) { |
c0c77734 DB |
3030 | fprintf(opt->file, "%c%03d%c", p->status, similarity_index(p), |
3031 | inter_name_termination); | |
663af342 | 3032 | } else { |
c0c77734 | 3033 | fprintf(opt->file, "%c%c", p->status, inter_name_termination); |
6973dcae | 3034 | } |
6973dcae | 3035 | |
cd676a51 JH |
3036 | if (p->status == DIFF_STATUS_COPIED || |
3037 | p->status == DIFF_STATUS_RENAMED) { | |
3038 | const char *name_a, *name_b; | |
3039 | name_a = p->one->path; | |
3040 | name_b = p->two->path; | |
3041 | strip_prefix(opt->prefix_length, &name_a, &name_b); | |
c0c77734 DB |
3042 | write_name_quoted(name_a, opt->file, inter_name_termination); |
3043 | write_name_quoted(name_b, opt->file, line_termination); | |
663af342 | 3044 | } else { |
cd676a51 JH |
3045 | const char *name_a, *name_b; |
3046 | name_a = p->one->mode ? p->one->path : p->two->path; | |
3047 | name_b = NULL; | |
3048 | strip_prefix(opt->prefix_length, &name_a, &name_b); | |
c0c77734 | 3049 | write_name_quoted(name_a, opt->file, line_termination); |
663af342 | 3050 | } |
6973dcae JH |
3051 | } |
3052 | ||
3053 | int diff_unmodified_pair(struct diff_filepair *p) | |
3054 | { | |
3055 | /* This function is written stricter than necessary to support | |
3056 | * the currently implemented transformers, but the idea is to | |
3057 | * let transformers to produce diff_filepairs any way they want, | |
3058 | * and filter and clean them up here before producing the output. | |
3059 | */ | |
663af342 | 3060 | struct diff_filespec *one = p->one, *two = p->two; |
6973dcae JH |
3061 | |
3062 | if (DIFF_PAIR_UNMERGED(p)) | |
3063 | return 0; /* unmerged is interesting */ | |
3064 | ||
6973dcae JH |
3065 | /* deletion, addition, mode or type change |
3066 | * and rename are all interesting. | |
3067 | */ | |
3068 | if (DIFF_FILE_VALID(one) != DIFF_FILE_VALID(two) || | |
3069 | DIFF_PAIR_MODE_CHANGED(p) || | |
3070 | strcmp(one->path, two->path)) | |
3071 | return 0; | |
3072 | ||
3073 | /* both are valid and point at the same path. that is, we are | |
3074 | * dealing with a change. | |
3075 | */ | |
3076 | if (one->sha1_valid && two->sha1_valid && | |
a89fccd2 | 3077 | !hashcmp(one->sha1, two->sha1)) |
6973dcae JH |
3078 | return 1; /* no change */ |
3079 | if (!one->sha1_valid && !two->sha1_valid) | |
3080 | return 1; /* both look at the same file on the filesystem. */ | |
3081 | return 0; | |
3082 | } | |
3083 | ||
3084 | static void diff_flush_patch(struct diff_filepair *p, struct diff_options *o) | |
3085 | { | |
3086 | if (diff_unmodified_pair(p)) | |
3087 | return; | |
3088 | ||
3089 | if ((DIFF_FILE_VALID(p->one) && S_ISDIR(p->one->mode)) || | |
3090 | (DIFF_FILE_VALID(p->two) && S_ISDIR(p->two->mode))) | |
3091 | return; /* no tree diffs in patch format */ | |
3092 | ||
3093 | run_diff(p, o); | |
3094 | } | |
3095 | ||
3096 | static void diff_flush_stat(struct diff_filepair *p, struct diff_options *o, | |
3097 | struct diffstat_t *diffstat) | |
3098 | { | |
3099 | if (diff_unmodified_pair(p)) | |
3100 | return; | |
3101 | ||
3102 | if ((DIFF_FILE_VALID(p->one) && S_ISDIR(p->one->mode)) || | |
3103 | (DIFF_FILE_VALID(p->two) && S_ISDIR(p->two->mode))) | |
3104 | return; /* no tree diffs in patch format */ | |
3105 | ||
3106 | run_diffstat(p, o, diffstat); | |
3107 | } | |
3108 | ||
88246898 JS |
3109 | static void diff_flush_checkdiff(struct diff_filepair *p, |
3110 | struct diff_options *o) | |
3111 | { | |
3112 | if (diff_unmodified_pair(p)) | |
3113 | return; | |
3114 | ||
3115 | if ((DIFF_FILE_VALID(p->one) && S_ISDIR(p->one->mode)) || | |
3116 | (DIFF_FILE_VALID(p->two) && S_ISDIR(p->two->mode))) | |
3117 | return; /* no tree diffs in patch format */ | |
3118 | ||
3119 | run_checkdiff(p, o); | |
3120 | } | |
3121 | ||
6973dcae JH |
3122 | int diff_queue_is_empty(void) |
3123 | { | |
3124 | struct diff_queue_struct *q = &diff_queued_diff; | |
3125 | int i; | |
3126 | for (i = 0; i < q->nr; i++) | |
3127 | if (!diff_unmodified_pair(q->queue[i])) | |
3128 | return 0; | |
3129 | return 1; | |
3130 | } | |
3131 | ||
3132 | #if DIFF_DEBUG | |
3133 | void diff_debug_filespec(struct diff_filespec *s, int x, const char *one) | |
3134 | { | |
3135 | fprintf(stderr, "queue[%d] %s (%s) %s %06o %s\n", | |
3136 | x, one ? one : "", | |
3137 | s->path, | |
3138 | DIFF_FILE_VALID(s) ? "valid" : "invalid", | |
3139 | s->mode, | |
3140 | s->sha1_valid ? sha1_to_hex(s->sha1) : ""); | |
3141 | fprintf(stderr, "queue[%d] %s size %lu flags %d\n", | |
3142 | x, one ? one : "", | |
3143 | s->size, s->xfrm_flags); | |
3144 | } | |
3145 | ||
3146 | void diff_debug_filepair(const struct diff_filepair *p, int i) | |
3147 | { | |
3148 | diff_debug_filespec(p->one, i, "one"); | |
3149 | diff_debug_filespec(p->two, i, "two"); | |
64479711 | 3150 | fprintf(stderr, "score %d, status %c rename_used %d broken %d\n", |
6973dcae | 3151 | p->score, p->status ? p->status : '?', |
64479711 | 3152 | p->one->rename_used, p->broken_pair); |
6973dcae JH |
3153 | } |
3154 | ||
3155 | void diff_debug_queue(const char *msg, struct diff_queue_struct *q) | |
3156 | { | |
3157 | int i; | |
3158 | if (msg) | |
3159 | fprintf(stderr, "%s\n", msg); | |
3160 | fprintf(stderr, "q->nr = %d\n", q->nr); | |
3161 | for (i = 0; i < q->nr; i++) { | |
3162 | struct diff_filepair *p = q->queue[i]; | |
3163 | diff_debug_filepair(p, i); | |
3164 | } | |
3165 | } | |
3166 | #endif | |
3167 | ||
3168 | static void diff_resolve_rename_copy(void) | |
3169 | { | |
64479711 LT |
3170 | int i; |
3171 | struct diff_filepair *p; | |
6973dcae JH |
3172 | struct diff_queue_struct *q = &diff_queued_diff; |
3173 | ||
3174 | diff_debug_queue("resolve-rename-copy", q); | |
3175 | ||
3176 | for (i = 0; i < q->nr; i++) { | |
3177 | p = q->queue[i]; | |
3178 | p->status = 0; /* undecided */ | |
3179 | if (DIFF_PAIR_UNMERGED(p)) | |
3180 | p->status = DIFF_STATUS_UNMERGED; | |
3181 | else if (!DIFF_FILE_VALID(p->one)) | |
3182 | p->status = DIFF_STATUS_ADDED; | |
3183 | else if (!DIFF_FILE_VALID(p->two)) | |
3184 | p->status = DIFF_STATUS_DELETED; | |
3185 | else if (DIFF_PAIR_TYPE_CHANGED(p)) | |
3186 | p->status = DIFF_STATUS_TYPE_CHANGED; | |
3187 | ||
3188 | /* from this point on, we are dealing with a pair | |
3189 | * whose both sides are valid and of the same type, i.e. | |
3190 | * either in-place edit or rename/copy edit. | |
3191 | */ | |
3192 | else if (DIFF_PAIR_RENAME(p)) { | |
64479711 LT |
3193 | /* |
3194 | * A rename might have re-connected a broken | |
3195 | * pair up, causing the pathnames to be the | |
3196 | * same again. If so, that's not a rename at | |
3197 | * all, just a modification.. | |
3198 | * | |
3199 | * Otherwise, see if this source was used for | |
3200 | * multiple renames, in which case we decrement | |
3201 | * the count, and call it a copy. | |
6973dcae | 3202 | */ |
64479711 LT |
3203 | if (!strcmp(p->one->path, p->two->path)) |
3204 | p->status = DIFF_STATUS_MODIFIED; | |
3205 | else if (--p->one->rename_used > 0) | |
6973dcae | 3206 | p->status = DIFF_STATUS_COPIED; |
64479711 | 3207 | else |
6973dcae JH |
3208 | p->status = DIFF_STATUS_RENAMED; |
3209 | } | |
a89fccd2 | 3210 | else if (hashcmp(p->one->sha1, p->two->sha1) || |
d516c2d1 JS |
3211 | p->one->mode != p->two->mode || |
3212 | is_null_sha1(p->one->sha1)) | |
6973dcae JH |
3213 | p->status = DIFF_STATUS_MODIFIED; |
3214 | else { | |
3215 | /* This is a "no-change" entry and should not | |
3216 | * happen anymore, but prepare for broken callers. | |
3217 | */ | |
3218 | error("feeding unmodified %s to diffcore", | |
3219 | p->one->path); | |
3220 | p->status = DIFF_STATUS_UNKNOWN; | |
3221 | } | |
3222 | } | |
3223 | diff_debug_queue("resolve-rename-copy done", q); | |
3224 | } | |
3225 | ||
c6744349 | 3226 | static int check_pair_status(struct diff_filepair *p) |
6973dcae | 3227 | { |
6973dcae JH |
3228 | switch (p->status) { |
3229 | case DIFF_STATUS_UNKNOWN: | |
c6744349 | 3230 | return 0; |
6973dcae JH |
3231 | case 0: |
3232 | die("internal error in diff-resolve-rename-copy"); | |
6973dcae | 3233 | default: |
c6744349 | 3234 | return 1; |
6973dcae JH |
3235 | } |
3236 | } | |
3237 | ||
c6744349 TH |
3238 | static void flush_one_pair(struct diff_filepair *p, struct diff_options *opt) |
3239 | { | |
3240 | int fmt = opt->output_format; | |
3241 | ||
3242 | if (fmt & DIFF_FORMAT_CHECKDIFF) | |
3243 | diff_flush_checkdiff(p, opt); | |
3244 | else if (fmt & (DIFF_FORMAT_RAW | DIFF_FORMAT_NAME_STATUS)) | |
3245 | diff_flush_raw(p, opt); | |
cd676a51 JH |
3246 | else if (fmt & DIFF_FORMAT_NAME) { |
3247 | const char *name_a, *name_b; | |
3248 | name_a = p->two->path; | |
3249 | name_b = NULL; | |
3250 | strip_prefix(opt->prefix_length, &name_a, &name_b); | |
c0c77734 | 3251 | write_name_quoted(name_a, opt->file, opt->line_termination); |
cd676a51 | 3252 | } |
c6744349 TH |
3253 | } |
3254 | ||
c0c77734 | 3255 | static void show_file_mode_name(FILE *file, const char *newdelete, struct diff_filespec *fs) |
4bbd261b SE |
3256 | { |
3257 | if (fs->mode) | |
c0c77734 | 3258 | fprintf(file, " %s mode %06o ", newdelete, fs->mode); |
4bbd261b | 3259 | else |
c0c77734 DB |
3260 | fprintf(file, " %s ", newdelete); |
3261 | write_name_quoted(fs->path, file, '\n'); | |
4bbd261b SE |
3262 | } |
3263 | ||
3264 | ||
c0c77734 | 3265 | static void show_mode_change(FILE *file, struct diff_filepair *p, int show_name) |
4bbd261b SE |
3266 | { |
3267 | if (p->one->mode && p->two->mode && p->one->mode != p->two->mode) { | |
c0c77734 | 3268 | fprintf(file, " mode change %06o => %06o%c", p->one->mode, p->two->mode, |
663af342 | 3269 | show_name ? ' ' : '\n'); |
0d26a64e | 3270 | if (show_name) { |
c0c77734 | 3271 | write_name_quoted(p->two->path, file, '\n'); |
0d26a64e | 3272 | } |
4bbd261b SE |
3273 | } |
3274 | } | |
3275 | ||
c0c77734 | 3276 | static void show_rename_copy(FILE *file, const char *renamecopy, struct diff_filepair *p) |
4bbd261b | 3277 | { |
b9f44164 | 3278 | char *names = pprint_rename(p->one->path, p->two->path); |
4bbd261b | 3279 | |
c0c77734 | 3280 | fprintf(file, " %s %s (%d%%)\n", renamecopy, names, similarity_index(p)); |
b9f44164 | 3281 | free(names); |
c0c77734 | 3282 | show_mode_change(file, p, 0); |
4bbd261b SE |
3283 | } |
3284 | ||
c0c77734 | 3285 | static void diff_summary(FILE *file, struct diff_filepair *p) |
4bbd261b SE |
3286 | { |
3287 | switch(p->status) { | |
3288 | case DIFF_STATUS_DELETED: | |
c0c77734 | 3289 | show_file_mode_name(file, "delete", p->one); |
4bbd261b SE |
3290 | break; |
3291 | case DIFF_STATUS_ADDED: | |
c0c77734 | 3292 | show_file_mode_name(file, "create", p->two); |
4bbd261b SE |
3293 | break; |
3294 | case DIFF_STATUS_COPIED: | |
c0c77734 | 3295 | show_rename_copy(file, "copy", p); |
4bbd261b SE |
3296 | break; |
3297 | case DIFF_STATUS_RENAMED: | |
c0c77734 | 3298 | show_rename_copy(file, "rename", p); |
4bbd261b SE |
3299 | break; |
3300 | default: | |
3301 | if (p->score) { | |
c0c77734 DB |
3302 | fputs(" rewrite ", file); |
3303 | write_name_quoted(p->two->path, file, ' '); | |
3304 | fprintf(file, "(%d%%)\n", similarity_index(p)); | |
663af342 | 3305 | } |
c0c77734 | 3306 | show_mode_change(file, p, !p->score); |
4bbd261b SE |
3307 | break; |
3308 | } | |
3309 | } | |
3310 | ||
fcb3d0ad | 3311 | struct patch_id_t { |
9126f009 | 3312 | git_SHA_CTX *ctx; |
fcb3d0ad JS |
3313 | int patchlen; |
3314 | }; | |
3315 | ||
3316 | static int remove_space(char *line, int len) | |
3317 | { | |
3318 | int i; | |
663af342 PH |
3319 | char *dst = line; |
3320 | unsigned char c; | |
fcb3d0ad | 3321 | |
663af342 PH |
3322 | for (i = 0; i < len; i++) |
3323 | if (!isspace((c = line[i]))) | |
3324 | *dst++ = c; | |
fcb3d0ad | 3325 | |
663af342 | 3326 | return dst - line; |
fcb3d0ad JS |
3327 | } |
3328 | ||
3329 | static void patch_id_consume(void *priv, char *line, unsigned long len) | |
3330 | { | |
3331 | struct patch_id_t *data = priv; | |
3332 | int new_len; | |
3333 | ||
3334 | /* Ignore line numbers when computing the SHA1 of the patch */ | |
cc44c765 | 3335 | if (!prefixcmp(line, "@@ -")) |
fcb3d0ad JS |
3336 | return; |
3337 | ||
3338 | new_len = remove_space(line, len); | |
3339 | ||
9126f009 | 3340 | git_SHA1_Update(data->ctx, line, new_len); |
fcb3d0ad JS |
3341 | data->patchlen += new_len; |
3342 | } | |
3343 | ||
3344 | /* returns 0 upon success, and writes result into sha1 */ | |
3345 | static int diff_get_patch_id(struct diff_options *options, unsigned char *sha1) | |
3346 | { | |
3347 | struct diff_queue_struct *q = &diff_queued_diff; | |
3348 | int i; | |
9126f009 | 3349 | git_SHA_CTX ctx; |
fcb3d0ad JS |
3350 | struct patch_id_t data; |
3351 | char buffer[PATH_MAX * 4 + 20]; | |
3352 | ||
9126f009 | 3353 | git_SHA1_Init(&ctx); |
fcb3d0ad JS |
3354 | memset(&data, 0, sizeof(struct patch_id_t)); |
3355 | data.ctx = &ctx; | |
fcb3d0ad JS |
3356 | |
3357 | for (i = 0; i < q->nr; i++) { | |
3358 | xpparam_t xpp; | |
3359 | xdemitconf_t xecfg; | |
fcb3d0ad JS |
3360 | mmfile_t mf1, mf2; |
3361 | struct diff_filepair *p = q->queue[i]; | |
3362 | int len1, len2; | |
3363 | ||
9ccd0a88 | 3364 | memset(&xpp, 0, sizeof(xpp)); |
30b25010 | 3365 | memset(&xecfg, 0, sizeof(xecfg)); |
fcb3d0ad JS |
3366 | if (p->status == 0) |
3367 | return error("internal diff status error"); | |
3368 | if (p->status == DIFF_STATUS_UNKNOWN) | |
3369 | continue; | |
3370 | if (diff_unmodified_pair(p)) | |
3371 | continue; | |
3372 | if ((DIFF_FILE_VALID(p->one) && S_ISDIR(p->one->mode)) || | |
3373 | (DIFF_FILE_VALID(p->two) && S_ISDIR(p->two->mode))) | |
3374 | continue; | |
3375 | if (DIFF_PAIR_UNMERGED(p)) | |
3376 | continue; | |
3377 | ||
3378 | diff_fill_sha1_info(p->one); | |
3379 | diff_fill_sha1_info(p->two); | |
3380 | if (fill_mmfile(&mf1, p->one) < 0 || | |
3381 | fill_mmfile(&mf2, p->two) < 0) | |
3382 | return error("unable to read files to diff"); | |
3383 | ||
fcb3d0ad JS |
3384 | len1 = remove_space(p->one->path, strlen(p->one->path)); |
3385 | len2 = remove_space(p->two->path, strlen(p->two->path)); | |
3386 | if (p->one->mode == 0) | |
3387 | len1 = snprintf(buffer, sizeof(buffer), | |
3388 | "diff--gita/%.*sb/%.*s" | |
3389 | "newfilemode%06o" | |
3390 | "---/dev/null" | |
3391 | "+++b/%.*s", | |
3392 | len1, p->one->path, | |
3393 | len2, p->two->path, | |
3394 | p->two->mode, | |
3395 | len2, p->two->path); | |
3396 | else if (p->two->mode == 0) | |
3397 | len1 = snprintf(buffer, sizeof(buffer), | |
3398 | "diff--gita/%.*sb/%.*s" | |
3399 | "deletedfilemode%06o" | |
3400 | "---a/%.*s" | |
3401 | "+++/dev/null", | |
3402 | len1, p->one->path, | |
3403 | len2, p->two->path, | |
3404 | p->one->mode, | |
3405 | len1, p->one->path); | |
3406 | else | |
3407 | len1 = snprintf(buffer, sizeof(buffer), | |
3408 | "diff--gita/%.*sb/%.*s" | |
3409 | "---a/%.*s" | |
3410 | "+++b/%.*s", | |
3411 | len1, p->one->path, | |
3412 | len2, p->two->path, | |
3413 | len1, p->one->path, | |
3414 | len2, p->two->path); | |
9126f009 | 3415 | git_SHA1_Update(&ctx, buffer, len1); |
fcb3d0ad JS |
3416 | |
3417 | xpp.flags = XDF_NEED_MINIMAL; | |
3418 | xecfg.ctxlen = 3; | |
9fdc3bb5 | 3419 | xecfg.flags = XDL_EMIT_FUNCNAMES; |
8a3f524b | 3420 | xdi_diff_outf(&mf1, &mf2, patch_id_consume, &data, |
dfea7900 | 3421 | &xpp, &xecfg); |
fcb3d0ad JS |
3422 | } |
3423 | ||
9126f009 | 3424 | git_SHA1_Final(sha1, &ctx); |
fcb3d0ad JS |
3425 | return 0; |
3426 | } | |
3427 | ||
3428 | int diff_flush_patch_id(struct diff_options *options, unsigned char *sha1) | |
3429 | { | |
3430 | struct diff_queue_struct *q = &diff_queued_diff; | |
3431 | int i; | |
3432 | int result = diff_get_patch_id(options, sha1); | |
3433 | ||
3434 | for (i = 0; i < q->nr; i++) | |
3435 | diff_free_filepair(q->queue[i]); | |
3436 | ||
3437 | free(q->queue); | |
3438 | q->queue = NULL; | |
3439 | q->nr = q->alloc = 0; | |
3440 | ||
3441 | return result; | |
3442 | } | |
3443 | ||
946c3784 | 3444 | static int is_summary_empty(const struct diff_queue_struct *q) |
6973dcae | 3445 | { |
6973dcae | 3446 | int i; |
6973dcae | 3447 | |
946c3784 TH |
3448 | for (i = 0; i < q->nr; i++) { |
3449 | const struct diff_filepair *p = q->queue[i]; | |
3450 | ||
3451 | switch (p->status) { | |
3452 | case DIFF_STATUS_DELETED: | |
3453 | case DIFF_STATUS_ADDED: | |
3454 | case DIFF_STATUS_COPIED: | |
3455 | case DIFF_STATUS_RENAMED: | |
3456 | return 0; | |
3457 | default: | |
3458 | if (p->score) | |
3459 | return 0; | |
3460 | if (p->one->mode && p->two->mode && | |
3461 | p->one->mode != p->two->mode) | |
3462 | return 0; | |
3463 | break; | |
3464 | } | |
6973dcae | 3465 | } |
946c3784 TH |
3466 | return 1; |
3467 | } | |
3468 | ||
6973dcae JH |
3469 | void diff_flush(struct diff_options *options) |
3470 | { | |
3471 | struct diff_queue_struct *q = &diff_queued_diff; | |
c6744349 | 3472 | int i, output_format = options->output_format; |
946c3784 | 3473 | int separator = 0; |
6973dcae | 3474 | |
c6744349 TH |
3475 | /* |
3476 | * Order: raw, stat, summary, patch | |
3477 | * or: name/name-status/checkdiff (other bits clear) | |
3478 | */ | |
946c3784 TH |
3479 | if (!q->nr) |
3480 | goto free_queue; | |
6973dcae | 3481 | |
c6744349 TH |
3482 | if (output_format & (DIFF_FORMAT_RAW | |
3483 | DIFF_FORMAT_NAME | | |
3484 | DIFF_FORMAT_NAME_STATUS | | |
3485 | DIFF_FORMAT_CHECKDIFF)) { | |
6973dcae JH |
3486 | for (i = 0; i < q->nr; i++) { |
3487 | struct diff_filepair *p = q->queue[i]; | |
c6744349 TH |
3488 | if (check_pair_status(p)) |
3489 | flush_one_pair(p, options); | |
6973dcae | 3490 | } |
946c3784 | 3491 | separator++; |
6973dcae | 3492 | } |
c6744349 | 3493 | |
c04a7155 | 3494 | if (output_format & (DIFF_FORMAT_DIFFSTAT|DIFF_FORMAT_SHORTSTAT|DIFF_FORMAT_NUMSTAT)) { |
5e2b0636 | 3495 | struct diffstat_t diffstat; |
c6744349 | 3496 | |
5e2b0636 | 3497 | memset(&diffstat, 0, sizeof(struct diffstat_t)); |
6973dcae JH |
3498 | for (i = 0; i < q->nr; i++) { |
3499 | struct diff_filepair *p = q->queue[i]; | |
c6744349 | 3500 | if (check_pair_status(p)) |
5e2b0636 | 3501 | diff_flush_stat(p, options, &diffstat); |
6973dcae | 3502 | } |
74e2abe5 JH |
3503 | if (output_format & DIFF_FORMAT_NUMSTAT) |
3504 | show_numstat(&diffstat, options); | |
3505 | if (output_format & DIFF_FORMAT_DIFFSTAT) | |
3506 | show_stats(&diffstat, options); | |
f604652e | 3507 | if (output_format & DIFF_FORMAT_SHORTSTAT) |
c0c77734 | 3508 | show_shortstats(&diffstat, options); |
f604652e | 3509 | free_diffstat_info(&diffstat); |
3969cf7d | 3510 | separator++; |
6973dcae | 3511 | } |
c04a7155 JH |
3512 | if (output_format & DIFF_FORMAT_DIRSTAT) |
3513 | show_dirstat(options); | |
6973dcae | 3514 | |
946c3784 | 3515 | if (output_format & DIFF_FORMAT_SUMMARY && !is_summary_empty(q)) { |
c6744349 | 3516 | for (i = 0; i < q->nr; i++) |
c0c77734 | 3517 | diff_summary(options->file, q->queue[i]); |
3969cf7d | 3518 | separator++; |
6973dcae JH |
3519 | } |
3520 | ||
6977c250 LA |
3521 | if (output_format & DIFF_FORMAT_NO_OUTPUT && |
3522 | DIFF_OPT_TST(options, EXIT_WITH_STATUS) && | |
3523 | DIFF_OPT_TST(options, DIFF_FROM_CONTENTS)) { | |
3524 | /* | |
3525 | * run diff_flush_patch for the exit status. setting | |
3526 | * options->file to /dev/null should be safe, becaue we | |
3527 | * aren't supposed to produce any output anyway. | |
3528 | */ | |
3529 | if (options->close_file) | |
3530 | fclose(options->file); | |
3531 | options->file = fopen("/dev/null", "w"); | |
3532 | if (!options->file) | |
3533 | die_errno("Could not open /dev/null"); | |
3534 | options->close_file = 1; | |
3535 | for (i = 0; i < q->nr; i++) { | |
3536 | struct diff_filepair *p = q->queue[i]; | |
3537 | if (check_pair_status(p)) | |
3538 | diff_flush_patch(p, options); | |
3539 | if (options->found_changes) | |
3540 | break; | |
3541 | } | |
3542 | } | |
3543 | ||
c6744349 | 3544 | if (output_format & DIFF_FORMAT_PATCH) { |
946c3784 | 3545 | if (separator) { |
6b2fbaaf | 3546 | putc(options->line_termination, options->file); |
946c3784 TH |
3547 | if (options->stat_sep) { |
3548 | /* attach patch instead of inline */ | |
c0c77734 | 3549 | fputs(options->stat_sep, options->file); |
946c3784 | 3550 | } |
c6744349 TH |
3551 | } |
3552 | ||
3553 | for (i = 0; i < q->nr; i++) { | |
3554 | struct diff_filepair *p = q->queue[i]; | |
3555 | if (check_pair_status(p)) | |
3556 | diff_flush_patch(p, options); | |
3557 | } | |
4bbd261b SE |
3558 | } |
3559 | ||
04245581 JK |
3560 | if (output_format & DIFF_FORMAT_CALLBACK) |
3561 | options->format_callback(q, options, options->format_callback_data); | |
3562 | ||
c6744349 TH |
3563 | for (i = 0; i < q->nr; i++) |
3564 | diff_free_filepair(q->queue[i]); | |
946c3784 | 3565 | free_queue: |
6973dcae JH |
3566 | free(q->queue); |
3567 | q->queue = NULL; | |
3568 | q->nr = q->alloc = 0; | |
c0c77734 DB |
3569 | if (options->close_file) |
3570 | fclose(options->file); | |
f245194f JH |
3571 | |
3572 | /* | |
97bf2a08 | 3573 | * Report the content-level differences with HAS_CHANGES; |
f245194f JH |
3574 | * diff_addremove/diff_change does not set the bit when |
3575 | * DIFF_FROM_CONTENTS is in effect (e.g. with -w). | |
3576 | */ | |
3577 | if (DIFF_OPT_TST(options, DIFF_FROM_CONTENTS)) { | |
3578 | if (options->found_changes) | |
3579 | DIFF_OPT_SET(options, HAS_CHANGES); | |
3580 | else | |
3581 | DIFF_OPT_CLR(options, HAS_CHANGES); | |
3582 | } | |
6973dcae JH |
3583 | } |
3584 | ||
3585 | static void diffcore_apply_filter(const char *filter) | |
3586 | { | |
3587 | int i; | |
3588 | struct diff_queue_struct *q = &diff_queued_diff; | |
3589 | struct diff_queue_struct outq; | |
3590 | outq.queue = NULL; | |
3591 | outq.nr = outq.alloc = 0; | |
3592 | ||
3593 | if (!filter) | |
3594 | return; | |
3595 | ||
3596 | if (strchr(filter, DIFF_STATUS_FILTER_AON)) { | |
3597 | int found; | |
3598 | for (i = found = 0; !found && i < q->nr; i++) { | |
3599 | struct diff_filepair *p = q->queue[i]; | |
3600 | if (((p->status == DIFF_STATUS_MODIFIED) && | |
3601 | ((p->score && | |
3602 | strchr(filter, DIFF_STATUS_FILTER_BROKEN)) || | |
3603 | (!p->score && | |
3604 | strchr(filter, DIFF_STATUS_MODIFIED)))) || | |
3605 | ((p->status != DIFF_STATUS_MODIFIED) && | |
3606 | strchr(filter, p->status))) | |
3607 | found++; | |
3608 | } | |
3609 | if (found) | |
3610 | return; | |
3611 | ||
3612 | /* otherwise we will clear the whole queue | |
3613 | * by copying the empty outq at the end of this | |
3614 | * function, but first clear the current entries | |
3615 | * in the queue. | |
3616 | */ | |
3617 | for (i = 0; i < q->nr; i++) | |
3618 | diff_free_filepair(q->queue[i]); | |
3619 | } | |
3620 | else { | |
3621 | /* Only the matching ones */ | |
3622 | for (i = 0; i < q->nr; i++) { | |
3623 | struct diff_filepair *p = q->queue[i]; | |
3624 | ||
3625 | if (((p->status == DIFF_STATUS_MODIFIED) && | |
3626 | ((p->score && | |
3627 | strchr(filter, DIFF_STATUS_FILTER_BROKEN)) || | |
3628 | (!p->score && | |
3629 | strchr(filter, DIFF_STATUS_MODIFIED)))) || | |
3630 | ((p->status != DIFF_STATUS_MODIFIED) && | |
3631 | strchr(filter, p->status))) | |
3632 | diff_q(&outq, p); | |
3633 | else | |
3634 | diff_free_filepair(p); | |
3635 | } | |
3636 | } | |
3637 | free(q->queue); | |
3638 | *q = outq; | |
3639 | } | |
3640 | ||
5701115a SV |
3641 | /* Check whether two filespecs with the same mode and size are identical */ |
3642 | static int diff_filespec_is_identical(struct diff_filespec *one, | |
3643 | struct diff_filespec *two) | |
3644 | { | |
2b459b48 JH |
3645 | if (S_ISGITLINK(one->mode)) |
3646 | return 0; | |
5701115a SV |
3647 | if (diff_populate_filespec(one, 0)) |
3648 | return 0; | |
3649 | if (diff_populate_filespec(two, 0)) | |
3650 | return 0; | |
3651 | return !memcmp(one->data, two->data, one->size); | |
3652 | } | |
3653 | ||
fb13227e JH |
3654 | static void diffcore_skip_stat_unmatch(struct diff_options *diffopt) |
3655 | { | |
3656 | int i; | |
3657 | struct diff_queue_struct *q = &diff_queued_diff; | |
3658 | struct diff_queue_struct outq; | |
3659 | outq.queue = NULL; | |
3660 | outq.nr = outq.alloc = 0; | |
3661 | ||
3662 | for (i = 0; i < q->nr; i++) { | |
3663 | struct diff_filepair *p = q->queue[i]; | |
3664 | ||
3665 | /* | |
9517e6b8 | 3666 | * 1. Entries that come from stat info dirtiness |
fb13227e JH |
3667 | * always have both sides (iow, not create/delete), |
3668 | * one side of the object name is unknown, with | |
3669 | * the same mode and size. Keep the ones that | |
3670 | * do not match these criteria. They have real | |
3671 | * differences. | |
3672 | * | |
3673 | * 2. At this point, the file is known to be modified, | |
3674 | * with the same mode and size, and the object | |
3675 | * name of one side is unknown. Need to inspect | |
3676 | * the identical contents. | |
3677 | */ | |
3678 | if (!DIFF_FILE_VALID(p->one) || /* (1) */ | |
3679 | !DIFF_FILE_VALID(p->two) || | |
3680 | (p->one->sha1_valid && p->two->sha1_valid) || | |
3681 | (p->one->mode != p->two->mode) || | |
3682 | diff_populate_filespec(p->one, 1) || | |
3683 | diff_populate_filespec(p->two, 1) || | |
3684 | (p->one->size != p->two->size) || | |
5701115a | 3685 | !diff_filespec_is_identical(p->one, p->two)) /* (2) */ |
fb13227e JH |
3686 | diff_q(&outq, p); |
3687 | else { | |
3688 | /* | |
3689 | * The caller can subtract 1 from skip_stat_unmatch | |
3690 | * to determine how many paths were dirty only | |
3691 | * due to stat info mismatch. | |
3692 | */ | |
8f67f8ae | 3693 | if (!DIFF_OPT_TST(diffopt, NO_INDEX)) |
6d2d9e86 | 3694 | diffopt->skip_stat_unmatch++; |
fb13227e JH |
3695 | diff_free_filepair(p); |
3696 | } | |
3697 | } | |
3698 | free(q->queue); | |
3699 | *q = outq; | |
3700 | } | |
3701 | ||
730f7284 JH |
3702 | static int diffnamecmp(const void *a_, const void *b_) |
3703 | { | |
3704 | const struct diff_filepair *a = *((const struct diff_filepair **)a_); | |
3705 | const struct diff_filepair *b = *((const struct diff_filepair **)b_); | |
3706 | const char *name_a, *name_b; | |
3707 | ||
3708 | name_a = a->one ? a->one->path : a->two->path; | |
3709 | name_b = b->one ? b->one->path : b->two->path; | |
3710 | return strcmp(name_a, name_b); | |
3711 | } | |
3712 | ||
3713 | void diffcore_fix_diff_index(struct diff_options *options) | |
3714 | { | |
3715 | struct diff_queue_struct *q = &diff_queued_diff; | |
3716 | qsort(q->queue, q->nr, sizeof(q->queue[0]), diffnamecmp); | |
3717 | } | |
3718 | ||
6973dcae JH |
3719 | void diffcore_std(struct diff_options *options) |
3720 | { | |
9d865356 | 3721 | if (options->skip_stat_unmatch) |
fb13227e | 3722 | diffcore_skip_stat_unmatch(options); |
6973dcae JH |
3723 | if (options->break_opt != -1) |
3724 | diffcore_break(options->break_opt); | |
3725 | if (options->detect_rename) | |
3726 | diffcore_rename(options); | |
3727 | if (options->break_opt != -1) | |
3728 | diffcore_merge_broken(); | |
3729 | if (options->pickaxe) | |
3730 | diffcore_pickaxe(options->pickaxe, options->pickaxe_opts); | |
3731 | if (options->orderfile) | |
3732 | diffcore_order(options->orderfile); | |
3733 | diff_resolve_rename_copy(); | |
3734 | diffcore_apply_filter(options->filter); | |
68aacb2f | 3735 | |
f245194f | 3736 | if (diff_queued_diff.nr && !DIFF_OPT_TST(options, DIFF_FROM_CONTENTS)) |
8f67f8ae PH |
3737 | DIFF_OPT_SET(options, HAS_CHANGES); |
3738 | else | |
3739 | DIFF_OPT_CLR(options, HAS_CHANGES); | |
6973dcae JH |
3740 | } |
3741 | ||
da31b358 JH |
3742 | int diff_result_code(struct diff_options *opt, int status) |
3743 | { | |
3744 | int result = 0; | |
3745 | if (!DIFF_OPT_TST(opt, EXIT_WITH_STATUS) && | |
3746 | !(opt->output_format & DIFF_FORMAT_CHECKDIFF)) | |
3747 | return status; | |
3748 | if (DIFF_OPT_TST(opt, EXIT_WITH_STATUS) && | |
3749 | DIFF_OPT_TST(opt, HAS_CHANGES)) | |
3750 | result |= 01; | |
3751 | if ((opt->output_format & DIFF_FORMAT_CHECKDIFF) && | |
3752 | DIFF_OPT_TST(opt, CHECK_FAILED)) | |
3753 | result |= 02; | |
3754 | return result; | |
3755 | } | |
6973dcae | 3756 | |
6973dcae JH |
3757 | void diff_addremove(struct diff_options *options, |
3758 | int addremove, unsigned mode, | |
3759 | const unsigned char *sha1, | |
e3d42c47 | 3760 | const char *concatpath, unsigned dirty_submodule) |
6973dcae | 3761 | { |
6973dcae JH |
3762 | struct diff_filespec *one, *two; |
3763 | ||
50fd9bd8 JS |
3764 | if (DIFF_OPT_TST(options, IGNORE_SUBMODULES) && S_ISGITLINK(mode)) |
3765 | return; | |
3766 | ||
6973dcae JH |
3767 | /* This may look odd, but it is a preparation for |
3768 | * feeding "there are unchanged files which should | |
3769 | * not produce diffs, but when you are doing copy | |
3770 | * detection you would need them, so here they are" | |
3771 | * entries to the diff-core. They will be prefixed | |
3772 | * with something like '=' or '*' (I haven't decided | |
3773 | * which but should not make any difference). | |
a6080a0a | 3774 | * Feeding the same new and old to diff_change() |
6973dcae JH |
3775 | * also has the same effect. |
3776 | * Before the final output happens, they are pruned after | |
3777 | * merged into rename/copy pairs as appropriate. | |
3778 | */ | |
8f67f8ae | 3779 | if (DIFF_OPT_TST(options, REVERSE_DIFF)) |
6973dcae JH |
3780 | addremove = (addremove == '+' ? '-' : |
3781 | addremove == '-' ? '+' : addremove); | |
3782 | ||
cd676a51 JH |
3783 | if (options->prefix && |
3784 | strncmp(concatpath, options->prefix, options->prefix_length)) | |
3785 | return; | |
3786 | ||
6973dcae JH |
3787 | one = alloc_filespec(concatpath); |
3788 | two = alloc_filespec(concatpath); | |
3789 | ||
3790 | if (addremove != '+') | |
3791 | fill_filespec(one, sha1, mode); | |
e3d42c47 | 3792 | if (addremove != '-') { |
6973dcae | 3793 | fill_filespec(two, sha1, mode); |
e3d42c47 JL |
3794 | two->dirty_submodule = dirty_submodule; |
3795 | } | |
6973dcae JH |
3796 | |
3797 | diff_queue(&diff_queued_diff, one, two); | |
f245194f JH |
3798 | if (!DIFF_OPT_TST(options, DIFF_FROM_CONTENTS)) |
3799 | DIFF_OPT_SET(options, HAS_CHANGES); | |
6973dcae JH |
3800 | } |
3801 | ||
3802 | void diff_change(struct diff_options *options, | |
3803 | unsigned old_mode, unsigned new_mode, | |
3804 | const unsigned char *old_sha1, | |
3805 | const unsigned char *new_sha1, | |
e3d42c47 JL |
3806 | const char *concatpath, |
3807 | unsigned old_dirty_submodule, unsigned new_dirty_submodule) | |
6973dcae | 3808 | { |
6973dcae JH |
3809 | struct diff_filespec *one, *two; |
3810 | ||
50fd9bd8 JS |
3811 | if (DIFF_OPT_TST(options, IGNORE_SUBMODULES) && S_ISGITLINK(old_mode) |
3812 | && S_ISGITLINK(new_mode)) | |
3813 | return; | |
3814 | ||
8f67f8ae | 3815 | if (DIFF_OPT_TST(options, REVERSE_DIFF)) { |
6973dcae JH |
3816 | unsigned tmp; |
3817 | const unsigned char *tmp_c; | |
3818 | tmp = old_mode; old_mode = new_mode; new_mode = tmp; | |
3819 | tmp_c = old_sha1; old_sha1 = new_sha1; new_sha1 = tmp_c; | |
e3d42c47 JL |
3820 | tmp = old_dirty_submodule; old_dirty_submodule = new_dirty_submodule; |
3821 | new_dirty_submodule = tmp; | |
6973dcae | 3822 | } |
cd676a51 JH |
3823 | |
3824 | if (options->prefix && | |
3825 | strncmp(concatpath, options->prefix, options->prefix_length)) | |
3826 | return; | |
3827 | ||
6973dcae JH |
3828 | one = alloc_filespec(concatpath); |
3829 | two = alloc_filespec(concatpath); | |
3830 | fill_filespec(one, old_sha1, old_mode); | |
3831 | fill_filespec(two, new_sha1, new_mode); | |
e3d42c47 JL |
3832 | one->dirty_submodule = old_dirty_submodule; |
3833 | two->dirty_submodule = new_dirty_submodule; | |
6973dcae JH |
3834 | |
3835 | diff_queue(&diff_queued_diff, one, two); | |
f245194f JH |
3836 | if (!DIFF_OPT_TST(options, DIFF_FROM_CONTENTS)) |
3837 | DIFF_OPT_SET(options, HAS_CHANGES); | |
6973dcae JH |
3838 | } |
3839 | ||
3840 | void diff_unmerge(struct diff_options *options, | |
e9c84099 JH |
3841 | const char *path, |
3842 | unsigned mode, const unsigned char *sha1) | |
6973dcae JH |
3843 | { |
3844 | struct diff_filespec *one, *two; | |
cd676a51 JH |
3845 | |
3846 | if (options->prefix && | |
3847 | strncmp(path, options->prefix, options->prefix_length)) | |
3848 | return; | |
3849 | ||
6973dcae JH |
3850 | one = alloc_filespec(path); |
3851 | two = alloc_filespec(path); | |
e9c84099 JH |
3852 | fill_filespec(one, sha1, mode); |
3853 | diff_queue(&diff_queued_diff, one, two)->is_unmerged = 1; | |
6973dcae | 3854 | } |
9cb92c39 JK |
3855 | |
3856 | static char *run_textconv(const char *pgm, struct diff_filespec *spec, | |
3857 | size_t *outsize) | |
3858 | { | |
479b0ae8 | 3859 | struct diff_tempfile *temp; |
9cb92c39 JK |
3860 | const char *argv[3]; |
3861 | const char **arg = argv; | |
3862 | struct child_process child; | |
3863 | struct strbuf buf = STRBUF_INIT; | |
da1fbed3 | 3864 | int err = 0; |
9cb92c39 | 3865 | |
479b0ae8 | 3866 | temp = prepare_temp_file(spec->path, spec); |
9cb92c39 | 3867 | *arg++ = pgm; |
479b0ae8 | 3868 | *arg++ = temp->name; |
9cb92c39 JK |
3869 | *arg = NULL; |
3870 | ||
3871 | memset(&child, 0, sizeof(child)); | |
41a457e4 | 3872 | child.use_shell = 1; |
9cb92c39 JK |
3873 | child.argv = argv; |
3874 | child.out = -1; | |
da1fbed3 | 3875 | if (start_command(&child)) { |
479b0ae8 | 3876 | remove_tempfile(); |
9cb92c39 JK |
3877 | return NULL; |
3878 | } | |
da1fbed3 JS |
3879 | |
3880 | if (strbuf_read(&buf, child.out, 0) < 0) | |
3881 | err = error("error reading from textconv command '%s'", pgm); | |
70d70999 | 3882 | close(child.out); |
da1fbed3 JS |
3883 | |
3884 | if (finish_command(&child) || err) { | |
3885 | strbuf_release(&buf); | |
3886 | remove_tempfile(); | |
3887 | return NULL; | |
3888 | } | |
479b0ae8 | 3889 | remove_tempfile(); |
9cb92c39 JK |
3890 | |
3891 | return strbuf_detach(&buf, outsize); | |
3892 | } |