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