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