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