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