]>
Commit | Line | Data |
---|---|---|
6973dcae JH |
1 | /* |
2 | * Copyright (C) 2005 Junio C Hamano | |
3 | */ | |
6973dcae | 4 | #include "cache.h" |
b2141fc1 | 5 | #include "config.h" |
284098f1 | 6 | #include "tempfile.h" |
6973dcae JH |
7 | #include "quote.h" |
8 | #include "diff.h" | |
9 | #include "diffcore.h" | |
051308f6 | 10 | #include "delta.h" |
6973dcae | 11 | #include "xdiff-interface.h" |
7c92fe0e | 12 | #include "color.h" |
8c701249 | 13 | #include "attr.h" |
d5535ec7 | 14 | #include "run-command.h" |
23707811 | 15 | #include "utf8.h" |
be58e70d | 16 | #include "userdiff.h" |
851e18c3 | 17 | #include "submodule-config.h" |
752c0c24 | 18 | #include "submodule.h" |
2e2d5ac1 | 19 | #include "hashmap.h" |
a757c646 | 20 | #include "ll-merge.h" |
02e8ca0e | 21 | #include "string-list.h" |
82fbf269 | 22 | #include "argv-array.h" |
660e113c | 23 | #include "graph.h" |
150e3001 | 24 | #include "packfile.h" |
3ac68a93 | 25 | #include "help.h" |
6973dcae | 26 | |
1510fea7 SP |
27 | #ifdef NO_FAST_WORKING_DIRECTORY |
28 | #define FAST_WORKING_DIRECTORY 0 | |
29 | #else | |
30 | #define FAST_WORKING_DIRECTORY 1 | |
31 | #endif | |
32 | ||
96f1e58f | 33 | static int diff_detect_rename_default; |
33de7163 | 34 | static int diff_indent_heuristic = 1; |
92c57e5c | 35 | static int diff_rename_limit_default = 400; |
a624eaa7 | 36 | static int diff_suppress_blank_empty; |
d2aea137 | 37 | static int diff_use_color_default = -1; |
2e2d5ac1 | 38 | static int diff_color_moved_default; |
6468a4e5 | 39 | static int diff_context_default = 3; |
c4888677 | 40 | static int diff_interhunk_context_default; |
98a4d87b | 41 | static const char *diff_word_regex_cfg; |
cbe02100 | 42 | static const char *external_diff_cmd_cfg; |
6d8940b5 | 43 | static const char *diff_order_file_cfg; |
aecbf914 | 44 | int diff_auto_refresh_index = 1; |
a5a818ee | 45 | static int diff_mnemonic_prefix; |
f89504dd | 46 | static int diff_no_prefix; |
df44483a | 47 | static int diff_stat_graph_width; |
712d2c7d | 48 | static int diff_dirstat_permille_default = 30; |
be4f2b40 | 49 | static struct diff_options default_diff_options; |
07ab4dec | 50 | static long diff_algorithm; |
a17505f2 | 51 | static unsigned ws_error_highlight_default = WSEH_NEW; |
6973dcae | 52 | |
7c92fe0e | 53 | static char diff_colors[][COLOR_MAXLEN] = { |
dc6ebd4c | 54 | GIT_COLOR_RESET, |
8dbf3eb6 | 55 | GIT_COLOR_NORMAL, /* CONTEXT */ |
dc6ebd4c AL |
56 | GIT_COLOR_BOLD, /* METAINFO */ |
57 | GIT_COLOR_CYAN, /* FRAGINFO */ | |
58 | GIT_COLOR_RED, /* OLD */ | |
59 | GIT_COLOR_GREEN, /* NEW */ | |
60 | GIT_COLOR_YELLOW, /* COMMIT */ | |
61 | GIT_COLOR_BG_RED, /* WHITESPACE */ | |
89cb73a1 | 62 | GIT_COLOR_NORMAL, /* FUNCINFO */ |
86b452e2 SB |
63 | GIT_COLOR_BOLD_MAGENTA, /* OLD_MOVED */ |
64 | GIT_COLOR_BOLD_BLUE, /* OLD_MOVED ALTERNATIVE */ | |
65 | GIT_COLOR_FAINT, /* OLD_MOVED_DIM */ | |
66 | GIT_COLOR_FAINT_ITALIC, /* OLD_MOVED_ALTERNATIVE_DIM */ | |
67 | GIT_COLOR_BOLD_CYAN, /* NEW_MOVED */ | |
68 | GIT_COLOR_BOLD_YELLOW, /* NEW_MOVED ALTERNATIVE */ | |
69 | GIT_COLOR_FAINT, /* NEW_MOVED_DIM */ | |
70 | GIT_COLOR_FAINT_ITALIC, /* NEW_MOVED_ALTERNATIVE_DIM */ | |
cd112cef JS |
71 | }; |
72 | ||
a73b3680 NTND |
73 | static const char *color_diff_slots[] = { |
74 | [DIFF_CONTEXT] = "context", | |
75 | [DIFF_METAINFO] = "meta", | |
76 | [DIFF_FRAGINFO] = "frag", | |
77 | [DIFF_FILE_OLD] = "old", | |
78 | [DIFF_FILE_NEW] = "new", | |
79 | [DIFF_COMMIT] = "commit", | |
80 | [DIFF_WHITESPACE] = "whitespace", | |
81 | [DIFF_FUNCINFO] = "func", | |
82 | [DIFF_FILE_OLD_MOVED] = "oldMoved", | |
83 | [DIFF_FILE_OLD_MOVED_ALT] = "oldMovedAlternative", | |
84 | [DIFF_FILE_OLD_MOVED_DIM] = "oldMovedDimmed", | |
85 | [DIFF_FILE_OLD_MOVED_ALT_DIM] = "oldMovedAlternativeDimmed", | |
86 | [DIFF_FILE_NEW_MOVED] = "newMoved", | |
87 | [DIFF_FILE_NEW_MOVED_ALT] = "newMovedAlternative", | |
88 | [DIFF_FILE_NEW_MOVED_DIM] = "newMovedDimmed", | |
89 | [DIFF_FILE_NEW_MOVED_ALT_DIM] = "newMovedAlternativeDimmed", | |
90 | }; | |
91 | ||
a2f05c94 JNA |
92 | static NORETURN void die_want_option(const char *option_name) |
93 | { | |
94 | die(_("option '%s' requires a value"), option_name); | |
95 | } | |
96 | ||
3ac68a93 NTND |
97 | define_list_config_array_extra(color_diff_slots, {"plain"}); |
98 | ||
9e1a5ebe | 99 | static int parse_diff_color_slot(const char *var) |
801235c5 | 100 | { |
a73b3680 | 101 | if (!strcasecmp(var, "plain")) |
8dbf3eb6 | 102 | return DIFF_CONTEXT; |
a73b3680 | 103 | return LOOKUP_CONFIG(color_diff_slots, var); |
801235c5 JH |
104 | } |
105 | ||
02e8ca0e | 106 | static int parse_dirstat_params(struct diff_options *options, const char *params_string, |
51670fc8 | 107 | struct strbuf *errmsg) |
333f3fb0 | 108 | { |
02e8ca0e MH |
109 | char *params_copy = xstrdup(params_string); |
110 | struct string_list params = STRING_LIST_INIT_NODUP; | |
111 | int ret = 0; | |
112 | int i; | |
51670fc8 | 113 | |
02e8ca0e MH |
114 | if (*params_copy) |
115 | string_list_split_in_place(¶ms, params_copy, ',', -1); | |
116 | for (i = 0; i < params.nr; i++) { | |
117 | const char *p = params.items[i].string; | |
118 | if (!strcmp(p, "changes")) { | |
0d1e0e78 BW |
119 | options->flags.dirstat_by_line = 0; |
120 | options->flags.dirstat_by_file = 0; | |
02e8ca0e | 121 | } else if (!strcmp(p, "lines")) { |
0d1e0e78 BW |
122 | options->flags.dirstat_by_line = 1; |
123 | options->flags.dirstat_by_file = 0; | |
02e8ca0e | 124 | } else if (!strcmp(p, "files")) { |
0d1e0e78 BW |
125 | options->flags.dirstat_by_line = 0; |
126 | options->flags.dirstat_by_file = 1; | |
02e8ca0e | 127 | } else if (!strcmp(p, "noncumulative")) { |
0d1e0e78 | 128 | options->flags.dirstat_cumulative = 0; |
02e8ca0e | 129 | } else if (!strcmp(p, "cumulative")) { |
0d1e0e78 | 130 | options->flags.dirstat_cumulative = 1; |
333f3fb0 JH |
131 | } else if (isdigit(*p)) { |
132 | char *end; | |
51670fc8 JH |
133 | int permille = strtoul(p, &end, 10) * 10; |
134 | if (*end == '.' && isdigit(*++end)) { | |
712d2c7d | 135 | /* only use first digit */ |
51670fc8 | 136 | permille += *end - '0'; |
712d2c7d | 137 | /* .. and ignore any further digits */ |
51670fc8 | 138 | while (isdigit(*++end)) |
712d2c7d JH |
139 | ; /* nothing */ |
140 | } | |
02e8ca0e | 141 | if (!*end) |
51670fc8 JH |
142 | options->dirstat_permille = permille; |
143 | else { | |
02e8ca0e MH |
144 | strbuf_addf(errmsg, _(" Failed to parse dirstat cut-off percentage '%s'\n"), |
145 | p); | |
51670fc8 JH |
146 | ret++; |
147 | } | |
148 | } else { | |
02e8ca0e | 149 | strbuf_addf(errmsg, _(" Unknown dirstat parameter '%s'\n"), p); |
51670fc8 | 150 | ret++; |
333f3fb0 | 151 | } |
51670fc8 | 152 | |
333f3fb0 | 153 | } |
02e8ca0e MH |
154 | string_list_clear(¶ms, 0); |
155 | free(params_copy); | |
51670fc8 | 156 | return ret; |
333f3fb0 JH |
157 | } |
158 | ||
c47ef57c RR |
159 | static int parse_submodule_params(struct diff_options *options, const char *value) |
160 | { | |
161 | if (!strcmp(value, "log")) | |
61cfbc05 | 162 | options->submodule_format = DIFF_SUBMODULE_LOG; |
c47ef57c | 163 | else if (!strcmp(value, "short")) |
61cfbc05 | 164 | options->submodule_format = DIFF_SUBMODULE_SHORT; |
fd47ae6a JK |
165 | else if (!strcmp(value, "diff")) |
166 | options->submodule_format = DIFF_SUBMODULE_INLINE_DIFF; | |
c47ef57c RR |
167 | else |
168 | return -1; | |
169 | return 0; | |
170 | } | |
171 | ||
cced5fbc LT |
172 | static int git_config_rename(const char *var, const char *value) |
173 | { | |
174 | if (!value) | |
175 | return DIFF_DETECT_RENAME; | |
176 | if (!strcasecmp(value, "copies") || !strcasecmp(value, "copy")) | |
177 | return DIFF_DETECT_COPY; | |
178 | return git_config_bool(var,value) ? DIFF_DETECT_RENAME : 0; | |
179 | } | |
180 | ||
07924d4d | 181 | long parse_algorithm_value(const char *value) |
07ab4dec MP |
182 | { |
183 | if (!value) | |
184 | return -1; | |
185 | else if (!strcasecmp(value, "myers") || !strcasecmp(value, "default")) | |
186 | return 0; | |
187 | else if (!strcasecmp(value, "minimal")) | |
188 | return XDF_NEED_MINIMAL; | |
189 | else if (!strcasecmp(value, "patience")) | |
190 | return XDF_PATIENCE_DIFF; | |
191 | else if (!strcasecmp(value, "histogram")) | |
192 | return XDF_HISTOGRAM_DIFF; | |
193 | return -1; | |
194 | } | |
195 | ||
0b4b42e7 JH |
196 | static int parse_one_token(const char **arg, const char *token) |
197 | { | |
198 | const char *rest; | |
199 | if (skip_prefix(*arg, token, &rest) && (!*rest || *rest == ',')) { | |
200 | *arg = rest; | |
201 | return 1; | |
202 | } | |
203 | return 0; | |
204 | } | |
205 | ||
206 | static int parse_ws_error_highlight(const char *arg) | |
207 | { | |
208 | const char *orig_arg = arg; | |
209 | unsigned val = 0; | |
210 | ||
211 | while (*arg) { | |
212 | if (parse_one_token(&arg, "none")) | |
213 | val = 0; | |
214 | else if (parse_one_token(&arg, "default")) | |
215 | val = WSEH_NEW; | |
216 | else if (parse_one_token(&arg, "all")) | |
217 | val = WSEH_NEW | WSEH_OLD | WSEH_CONTEXT; | |
218 | else if (parse_one_token(&arg, "new")) | |
219 | val |= WSEH_NEW; | |
220 | else if (parse_one_token(&arg, "old")) | |
221 | val |= WSEH_OLD; | |
222 | else if (parse_one_token(&arg, "context")) | |
223 | val |= WSEH_CONTEXT; | |
224 | else { | |
225 | return -1 - (int)(arg - orig_arg); | |
226 | } | |
227 | if (*arg) | |
228 | arg++; | |
229 | } | |
230 | return val; | |
231 | } | |
232 | ||
83ad63cf JH |
233 | /* |
234 | * These are to give UI layer defaults. | |
235 | * The core-level commands such as git-diff-files should | |
236 | * never be affected by the setting of diff.renames | |
237 | * the user happens to have in the configuration file. | |
238 | */ | |
5404c116 MM |
239 | void init_diff_ui_defaults(void) |
240 | { | |
06dba2b0 | 241 | diff_detect_rename_default = DIFF_DETECT_RENAME; |
5404c116 MM |
242 | } |
243 | ||
5b162879 MH |
244 | int git_diff_heuristic_config(const char *var, const char *value, void *cb) |
245 | { | |
3cde4e02 | 246 | if (!strcmp(var, "diff.indentheuristic")) |
5b162879 | 247 | diff_indent_heuristic = git_config_bool(var, value); |
5b162879 MH |
248 | return 0; |
249 | } | |
250 | ||
2e2d5ac1 SB |
251 | static int parse_color_moved(const char *arg) |
252 | { | |
253 | switch (git_parse_maybe_bool(arg)) { | |
254 | case 0: | |
255 | return COLOR_MOVED_NO; | |
256 | case 1: | |
257 | return COLOR_MOVED_DEFAULT; | |
258 | default: | |
259 | break; | |
260 | } | |
261 | ||
262 | if (!strcmp(arg, "no")) | |
263 | return COLOR_MOVED_NO; | |
176841f0 SB |
264 | else if (!strcmp(arg, "plain")) |
265 | return COLOR_MOVED_PLAIN; | |
2e2d5ac1 SB |
266 | else if (!strcmp(arg, "zebra")) |
267 | return COLOR_MOVED_ZEBRA; | |
268 | else if (!strcmp(arg, "default")) | |
269 | return COLOR_MOVED_DEFAULT; | |
86b452e2 SB |
270 | else if (!strcmp(arg, "dimmed_zebra")) |
271 | return COLOR_MOVED_ZEBRA_DIM; | |
2e2d5ac1 | 272 | else |
86b452e2 | 273 | return error(_("color moved setting must be one of 'no', 'default', 'zebra', 'dimmed_zebra', 'plain'")); |
2e2d5ac1 SB |
274 | } |
275 | ||
ef90d6d4 | 276 | int git_diff_ui_config(const char *var, const char *value, void *cb) |
801235c5 | 277 | { |
a159ca0c | 278 | if (!strcmp(var, "diff.color") || !strcmp(var, "color.diff")) { |
e269eb79 | 279 | diff_use_color_default = git_config_colorbool(var, value); |
801235c5 JH |
280 | return 0; |
281 | } | |
2e2d5ac1 SB |
282 | if (!strcmp(var, "diff.colormoved")) { |
283 | int cm = parse_color_moved(value); | |
284 | if (cm < 0) | |
285 | return -1; | |
286 | diff_color_moved_default = cm; | |
287 | return 0; | |
288 | } | |
6468a4e5 JM |
289 | if (!strcmp(var, "diff.context")) { |
290 | diff_context_default = git_config_int(var, value); | |
291 | if (diff_context_default < 0) | |
292 | return -1; | |
293 | return 0; | |
294 | } | |
c4888677 VN |
295 | if (!strcmp(var, "diff.interhunkcontext")) { |
296 | diff_interhunk_context_default = git_config_int(var, value); | |
297 | if (diff_interhunk_context_default < 0) | |
298 | return -1; | |
299 | return 0; | |
300 | } | |
b68ea12e | 301 | if (!strcmp(var, "diff.renames")) { |
cced5fbc | 302 | diff_detect_rename_default = git_config_rename(var, value); |
b68ea12e EW |
303 | return 0; |
304 | } | |
aecbf914 JH |
305 | if (!strcmp(var, "diff.autorefreshindex")) { |
306 | diff_auto_refresh_index = git_config_bool(var, value); | |
307 | return 0; | |
308 | } | |
a5a818ee JH |
309 | if (!strcmp(var, "diff.mnemonicprefix")) { |
310 | diff_mnemonic_prefix = git_config_bool(var, value); | |
311 | return 0; | |
312 | } | |
f89504dd EC |
313 | if (!strcmp(var, "diff.noprefix")) { |
314 | diff_no_prefix = git_config_bool(var, value); | |
315 | return 0; | |
316 | } | |
df44483a ZJS |
317 | if (!strcmp(var, "diff.statgraphwidth")) { |
318 | diff_stat_graph_width = git_config_int(var, value); | |
319 | return 0; | |
320 | } | |
daec808c BH |
321 | if (!strcmp(var, "diff.external")) |
322 | return git_config_string(&external_diff_cmd_cfg, var, value); | |
98a4d87b BSSJ |
323 | if (!strcmp(var, "diff.wordregex")) |
324 | return git_config_string(&diff_word_regex_cfg, var, value); | |
6d8940b5 SB |
325 | if (!strcmp(var, "diff.orderfile")) |
326 | return git_config_pathname(&diff_order_file_cfg, var, value); | |
f1af60bd | 327 | |
be4f2b40 JS |
328 | if (!strcmp(var, "diff.ignoresubmodules")) |
329 | handle_ignore_submodules_arg(&default_diff_options, value); | |
330 | ||
c47ef57c RR |
331 | if (!strcmp(var, "diff.submodule")) { |
332 | if (parse_submodule_params(&default_diff_options, value)) | |
333 | warning(_("Unknown value for 'diff.submodule' config variable: '%s'"), | |
334 | value); | |
335 | return 0; | |
336 | } | |
337 | ||
07ab4dec MP |
338 | if (!strcmp(var, "diff.algorithm")) { |
339 | diff_algorithm = parse_algorithm_value(value); | |
340 | if (diff_algorithm < 0) | |
341 | return -1; | |
342 | return 0; | |
343 | } | |
344 | ||
a17505f2 JH |
345 | if (!strcmp(var, "diff.wserrorhighlight")) { |
346 | int val = parse_ws_error_highlight(value); | |
347 | if (val < 0) | |
348 | return -1; | |
349 | ws_error_highlight_default = val; | |
350 | return 0; | |
351 | } | |
352 | ||
33c643bb JK |
353 | if (git_color_config(var, value, cb) < 0) |
354 | return -1; | |
355 | ||
ef90d6d4 | 356 | return git_diff_basic_config(var, value, cb); |
9a1805a8 JK |
357 | } |
358 | ||
ef90d6d4 | 359 | int git_diff_basic_config(const char *var, const char *value, void *cb) |
9a1805a8 | 360 | { |
ae021d87 JK |
361 | const char *name; |
362 | ||
2b6ca6df LT |
363 | if (!strcmp(var, "diff.renamelimit")) { |
364 | diff_rename_limit_default = git_config_int(var, value); | |
365 | return 0; | |
366 | } | |
367 | ||
6680a087 JK |
368 | if (userdiff_config(var, value) < 0) |
369 | return -1; | |
c7534ef4 | 370 | |
ae021d87 JK |
371 | if (skip_prefix(var, "diff.color.", &name) || |
372 | skip_prefix(var, "color.diff.", &name)) { | |
373 | int slot = parse_diff_color_slot(name); | |
8b8e8624 JK |
374 | if (slot < 0) |
375 | return 0; | |
64f30e94 JH |
376 | if (!value) |
377 | return config_error_nonbool(var); | |
f6c5a296 | 378 | return color_parse(value, diff_colors[slot]); |
801235c5 | 379 | } |
f1af60bd | 380 | |
a624eaa7 | 381 | /* like GNU diff's --suppress-blank-empty option */ |
950db879 JS |
382 | if (!strcmp(var, "diff.suppressblankempty") || |
383 | /* for backwards compatibility */ | |
384 | !strcmp(var, "diff.suppress-blank-empty")) { | |
a624eaa7 JM |
385 | diff_suppress_blank_empty = git_config_bool(var, value); |
386 | return 0; | |
387 | } | |
388 | ||
2d174951 | 389 | if (!strcmp(var, "diff.dirstat")) { |
51670fc8 | 390 | struct strbuf errmsg = STRBUF_INIT; |
712d2c7d | 391 | default_diff_options.dirstat_permille = diff_dirstat_permille_default; |
51670fc8 | 392 | if (parse_dirstat_params(&default_diff_options, value, &errmsg)) |
7478ac57 | 393 | warning(_("Found errors in 'diff.dirstat' config variable:\n%s"), |
51670fc8 JH |
394 | errmsg.buf); |
395 | strbuf_release(&errmsg); | |
712d2c7d | 396 | diff_dirstat_permille_default = default_diff_options.dirstat_permille; |
2d174951 JH |
397 | return 0; |
398 | } | |
399 | ||
cf5e7722 MB |
400 | if (git_diff_heuristic_config(var, value, cb) < 0) |
401 | return -1; | |
402 | ||
3e1dd17a | 403 | return git_default_config(var, value, cb); |
801235c5 JH |
404 | } |
405 | ||
6973dcae JH |
406 | static char *quote_two(const char *one, const char *two) |
407 | { | |
408 | int need_one = quote_c_style(one, NULL, NULL, 1); | |
409 | int need_two = quote_c_style(two, NULL, NULL, 1); | |
f285a2d7 | 410 | struct strbuf res = STRBUF_INIT; |
6973dcae JH |
411 | |
412 | if (need_one + need_two) { | |
663af342 PH |
413 | strbuf_addch(&res, '"'); |
414 | quote_c_style(one, &res, NULL, 1); | |
415 | quote_c_style(two, &res, NULL, 1); | |
416 | strbuf_addch(&res, '"'); | |
417 | } else { | |
418 | strbuf_addstr(&res, one); | |
419 | strbuf_addstr(&res, two); | |
6973dcae | 420 | } |
b315c5c0 | 421 | return strbuf_detach(&res, NULL); |
6973dcae JH |
422 | } |
423 | ||
424 | static const char *external_diff(void) | |
425 | { | |
426 | static const char *external_diff_cmd = NULL; | |
427 | static int done_preparing = 0; | |
428 | ||
429 | if (done_preparing) | |
430 | return external_diff_cmd; | |
431 | external_diff_cmd = getenv("GIT_EXTERNAL_DIFF"); | |
cbe02100 JS |
432 | if (!external_diff_cmd) |
433 | external_diff_cmd = external_diff_cmd_cfg; | |
6973dcae JH |
434 | done_preparing = 1; |
435 | return external_diff_cmd; | |
436 | } | |
437 | ||
284098f1 MH |
438 | /* |
439 | * Keep track of files used for diffing. Sometimes such an entry | |
440 | * refers to a temporary file, sometimes to an existing file, and | |
441 | * sometimes to "/dev/null". | |
442 | */ | |
6973dcae | 443 | static struct diff_tempfile { |
284098f1 MH |
444 | /* |
445 | * filename external diff should read from, or NULL if this | |
446 | * entry is currently not in use: | |
447 | */ | |
448 | const char *name; | |
449 | ||
dc01505f | 450 | char hex[GIT_MAX_HEXSZ + 1]; |
6973dcae | 451 | char mode[10]; |
284098f1 MH |
452 | |
453 | /* | |
454 | * If this diff_tempfile instance refers to a temporary file, | |
455 | * this tempfile object is used to manage its lifetime. | |
456 | */ | |
076aa2cb | 457 | struct tempfile *tempfile; |
6973dcae JH |
458 | } diff_temp[2]; |
459 | ||
6957eb9a | 460 | struct emit_callback { |
6957eb9a JH |
461 | int color_diff; |
462 | unsigned ws_rule; | |
463 | int blank_at_eof_in_preimage; | |
464 | int blank_at_eof_in_postimage; | |
465 | int lno_in_preimage; | |
466 | int lno_in_postimage; | |
6957eb9a JH |
467 | const char **label_path; |
468 | struct diff_words_data *diff_words; | |
a3c158d4 | 469 | struct diff_options *opt; |
3e97c7c6 | 470 | struct strbuf *header; |
6957eb9a JH |
471 | }; |
472 | ||
6973dcae JH |
473 | static int count_lines(const char *data, int size) |
474 | { | |
475 | int count, ch, completely_empty = 1, nl_just_seen = 0; | |
476 | count = 0; | |
477 | while (0 < size--) { | |
478 | ch = *data++; | |
479 | if (ch == '\n') { | |
480 | count++; | |
481 | nl_just_seen = 1; | |
482 | completely_empty = 0; | |
483 | } | |
484 | else { | |
485 | nl_just_seen = 0; | |
486 | completely_empty = 0; | |
487 | } | |
488 | } | |
489 | if (completely_empty) | |
490 | return 0; | |
491 | if (!nl_just_seen) | |
492 | count++; /* no trailing newline */ | |
493 | return count; | |
494 | } | |
495 | ||
6957eb9a JH |
496 | static int fill_mmfile(mmfile_t *mf, struct diff_filespec *one) |
497 | { | |
498 | if (!DIFF_FILE_VALID(one)) { | |
499 | mf->ptr = (char *)""; /* does not matter */ | |
500 | mf->size = 0; | |
501 | return 0; | |
502 | } | |
503 | else if (diff_populate_filespec(one, 0)) | |
504 | return -1; | |
bb35fefb | 505 | |
6957eb9a JH |
506 | mf->ptr = one->data; |
507 | mf->size = one->size; | |
508 | return 0; | |
509 | } | |
510 | ||
abb371a1 JK |
511 | /* like fill_mmfile, but only for size, so we can avoid retrieving blob */ |
512 | static unsigned long diff_filespec_size(struct diff_filespec *one) | |
513 | { | |
514 | if (!DIFF_FILE_VALID(one)) | |
515 | return 0; | |
8e5dd3d6 | 516 | diff_populate_filespec(one, CHECK_SIZE_ONLY); |
abb371a1 JK |
517 | return one->size; |
518 | } | |
519 | ||
6957eb9a JH |
520 | static int count_trailing_blank(mmfile_t *mf, unsigned ws_rule) |
521 | { | |
522 | char *ptr = mf->ptr; | |
523 | long size = mf->size; | |
524 | int cnt = 0; | |
525 | ||
526 | if (!size) | |
527 | return cnt; | |
528 | ptr += size - 1; /* pointing at the very end */ | |
529 | if (*ptr != '\n') | |
530 | ; /* incomplete line */ | |
531 | else | |
532 | ptr--; /* skip the last LF */ | |
533 | while (mf->ptr < ptr) { | |
534 | char *prev_eol; | |
535 | for (prev_eol = ptr; mf->ptr <= prev_eol; prev_eol--) | |
536 | if (*prev_eol == '\n') | |
537 | break; | |
538 | if (!ws_blank_line(prev_eol + 1, ptr - prev_eol, ws_rule)) | |
539 | break; | |
540 | cnt++; | |
541 | ptr = prev_eol - 1; | |
542 | } | |
543 | return cnt; | |
544 | } | |
545 | ||
546 | static void check_blank_at_eof(mmfile_t *mf1, mmfile_t *mf2, | |
547 | struct emit_callback *ecbdata) | |
548 | { | |
549 | int l1, l2, at; | |
550 | unsigned ws_rule = ecbdata->ws_rule; | |
551 | l1 = count_trailing_blank(mf1, ws_rule); | |
552 | l2 = count_trailing_blank(mf2, ws_rule); | |
553 | if (l2 <= l1) { | |
554 | ecbdata->blank_at_eof_in_preimage = 0; | |
555 | ecbdata->blank_at_eof_in_postimage = 0; | |
556 | return; | |
557 | } | |
558 | at = count_lines(mf1->ptr, mf1->size); | |
559 | ecbdata->blank_at_eof_in_preimage = (at - l1) + 1; | |
560 | ||
561 | at = count_lines(mf2->ptr, mf2->size); | |
562 | ecbdata->blank_at_eof_in_postimage = (at - l2) + 1; | |
563 | } | |
564 | ||
a3c158d4 | 565 | static void emit_line_0(struct diff_options *o, const char *set, const char *reset, |
250f7993 | 566 | int first, const char *line, int len) |
6957eb9a JH |
567 | { |
568 | int has_trailing_newline, has_trailing_carriage_return; | |
250f7993 | 569 | int nofirst; |
a3c158d4 BY |
570 | FILE *file = o->file; |
571 | ||
30997bb8 | 572 | fputs(diff_line_prefix(o), file); |
6957eb9a | 573 | |
250f7993 JH |
574 | if (len == 0) { |
575 | has_trailing_newline = (first == '\n'); | |
576 | has_trailing_carriage_return = (!has_trailing_newline && | |
577 | (first == '\r')); | |
578 | nofirst = has_trailing_newline || has_trailing_carriage_return; | |
579 | } else { | |
580 | has_trailing_newline = (len > 0 && line[len-1] == '\n'); | |
581 | if (has_trailing_newline) | |
582 | len--; | |
583 | has_trailing_carriage_return = (len > 0 && line[len-1] == '\r'); | |
584 | if (has_trailing_carriage_return) | |
585 | len--; | |
586 | nofirst = 0; | |
587 | } | |
6957eb9a | 588 | |
06a47552 JH |
589 | if (len || !nofirst) { |
590 | fputs(set, file); | |
591 | if (!nofirst) | |
592 | fputc(first, file); | |
593 | fwrite(line, len, 1, file); | |
594 | fputs(reset, file); | |
595 | } | |
6957eb9a JH |
596 | if (has_trailing_carriage_return) |
597 | fputc('\r', file); | |
598 | if (has_trailing_newline) | |
599 | fputc('\n', file); | |
600 | } | |
601 | ||
a3c158d4 | 602 | static void emit_line(struct diff_options *o, const char *set, const char *reset, |
250f7993 JH |
603 | const char *line, int len) |
604 | { | |
a3c158d4 | 605 | emit_line_0(o, set, reset, line[0], line+1, len-1); |
250f7993 JH |
606 | } |
607 | ||
36a4cefd | 608 | enum diff_symbol { |
4eed0ebd SB |
609 | DIFF_SYMBOL_BINARY_DIFF_HEADER, |
610 | DIFF_SYMBOL_BINARY_DIFF_HEADER_DELTA, | |
611 | DIFF_SYMBOL_BINARY_DIFF_HEADER_LITERAL, | |
612 | DIFF_SYMBOL_BINARY_DIFF_BODY, | |
613 | DIFF_SYMBOL_BINARY_DIFF_FOOTER, | |
0911c475 SB |
614 | DIFF_SYMBOL_STATS_SUMMARY_NO_FILES, |
615 | DIFF_SYMBOL_STATS_SUMMARY_ABBREV, | |
616 | DIFF_SYMBOL_STATS_SUMMARY_INSERTS_DELETES, | |
617 | DIFF_SYMBOL_STATS_LINE, | |
bd033291 | 618 | DIFF_SYMBOL_WORD_DIFF, |
30b7e1e7 | 619 | DIFF_SYMBOL_STAT_SEP, |
146fdb0d | 620 | DIFF_SYMBOL_SUMMARY, |
f3597138 SB |
621 | DIFF_SYMBOL_SUBMODULE_ADD, |
622 | DIFF_SYMBOL_SUBMODULE_DEL, | |
623 | DIFF_SYMBOL_SUBMODULE_UNTRACKED, | |
624 | DIFF_SYMBOL_SUBMODULE_MODIFIED, | |
625 | DIFF_SYMBOL_SUBMODULE_HEADER, | |
626 | DIFF_SYMBOL_SUBMODULE_ERROR, | |
627 | DIFF_SYMBOL_SUBMODULE_PIPETHROUGH, | |
5af6ea95 | 628 | DIFF_SYMBOL_REWRITE_DIFF, |
4acaaa7a | 629 | DIFF_SYMBOL_BINARY_FILES, |
a29b0a13 | 630 | DIFF_SYMBOL_HEADER, |
3ee8b7bf SB |
631 | DIFF_SYMBOL_FILEPAIR_PLUS, |
632 | DIFF_SYMBOL_FILEPAIR_MINUS, | |
ff958679 SB |
633 | DIFF_SYMBOL_WORDS_PORCELAIN, |
634 | DIFF_SYMBOL_WORDS, | |
091f8e28 | 635 | DIFF_SYMBOL_CONTEXT, |
f2bb1218 | 636 | DIFF_SYMBOL_CONTEXT_INCOMPLETE, |
091f8e28 SB |
637 | DIFF_SYMBOL_PLUS, |
638 | DIFF_SYMBOL_MINUS, | |
b9cbfde6 | 639 | DIFF_SYMBOL_NO_LF_EOF, |
68abc6f1 | 640 | DIFF_SYMBOL_CONTEXT_FRAGINFO, |
c64b420b | 641 | DIFF_SYMBOL_CONTEXT_MARKER, |
36a4cefd SB |
642 | DIFF_SYMBOL_SEPARATOR |
643 | }; | |
091f8e28 SB |
644 | /* |
645 | * Flags for content lines: | |
646 | * 0..12 are whitespace rules | |
647 | * 13-15 are WSEH_NEW | WSEH_OLD | WSEH_CONTEXT | |
648 | * 16 is marking if the line is blank at EOF | |
649 | */ | |
2e2d5ac1 SB |
650 | #define DIFF_SYMBOL_CONTENT_BLANK_LINE_EOF (1<<16) |
651 | #define DIFF_SYMBOL_MOVED_LINE (1<<17) | |
652 | #define DIFF_SYMBOL_MOVED_LINE_ALT (1<<18) | |
86b452e2 | 653 | #define DIFF_SYMBOL_MOVED_LINE_UNINTERESTING (1<<19) |
091f8e28 SB |
654 | #define DIFF_SYMBOL_CONTENT_WS_MASK (WSEH_NEW | WSEH_OLD | WSEH_CONTEXT | WS_RULE_MASK) |
655 | ||
e6e045f8 SB |
656 | /* |
657 | * This struct is used when we need to buffer the output of the diff output. | |
658 | * | |
659 | * NEEDSWORK: Instead of storing a copy of the line, add an offset pointer | |
660 | * into the pre/post image file. This pointer could be a union with the | |
661 | * line pointer. By storing an offset into the file instead of the literal line, | |
662 | * we can decrease the memory footprint for the buffered output. At first we | |
663 | * may want to only have indirection for the content lines, but we could also | |
664 | * enhance the state for emitting prefabricated lines, e.g. the similarity | |
665 | * score line or hunk/file headers would only need to store a number or path | |
666 | * and then the output can be constructed later on depending on state. | |
667 | */ | |
668 | struct emitted_diff_symbol { | |
669 | const char *line; | |
670 | int len; | |
671 | int flags; | |
672 | enum diff_symbol s; | |
673 | }; | |
674 | #define EMITTED_DIFF_SYMBOL_INIT {NULL} | |
675 | ||
676 | struct emitted_diff_symbols { | |
677 | struct emitted_diff_symbol *buf; | |
678 | int nr, alloc; | |
679 | }; | |
680 | #define EMITTED_DIFF_SYMBOLS_INIT {NULL, 0, 0} | |
681 | ||
682 | static void append_emitted_diff_symbol(struct diff_options *o, | |
683 | struct emitted_diff_symbol *e) | |
6957eb9a | 684 | { |
e6e045f8 SB |
685 | struct emitted_diff_symbol *f; |
686 | ||
687 | ALLOC_GROW(o->emitted_symbols->buf, | |
688 | o->emitted_symbols->nr + 1, | |
689 | o->emitted_symbols->alloc); | |
690 | f = &o->emitted_symbols->buf[o->emitted_symbols->nr++]; | |
691 | ||
692 | memcpy(f, e, sizeof(struct emitted_diff_symbol)); | |
693 | f->line = e->line ? xmemdupz(e->line, e->len) : NULL; | |
6957eb9a JH |
694 | } |
695 | ||
2e2d5ac1 SB |
696 | struct moved_entry { |
697 | struct hashmap_entry ent; | |
698 | const struct emitted_diff_symbol *es; | |
699 | struct moved_entry *next_line; | |
700 | }; | |
701 | ||
2e2d5ac1 SB |
702 | static int moved_entry_cmp(const struct diff_options *diffopt, |
703 | const struct moved_entry *a, | |
704 | const struct moved_entry *b, | |
705 | const void *keydata) | |
706 | { | |
01be97c2 SB |
707 | return !xdiff_compare_lines(a->es->line, a->es->len, |
708 | b->es->line, b->es->len, | |
709 | diffopt->xdl_opts); | |
2e2d5ac1 SB |
710 | } |
711 | ||
712 | static struct moved_entry *prepare_entry(struct diff_options *o, | |
713 | int line_no) | |
714 | { | |
715 | struct moved_entry *ret = xmalloc(sizeof(*ret)); | |
716 | struct emitted_diff_symbol *l = &o->emitted_symbols->buf[line_no]; | |
717 | ||
01be97c2 | 718 | ret->ent.hash = xdiff_hash_string(l->line, l->len, o->xdl_opts); |
2e2d5ac1 SB |
719 | ret->es = l; |
720 | ret->next_line = NULL; | |
721 | ||
722 | return ret; | |
723 | } | |
724 | ||
725 | static void add_lines_to_move_detection(struct diff_options *o, | |
726 | struct hashmap *add_lines, | |
727 | struct hashmap *del_lines) | |
728 | { | |
729 | struct moved_entry *prev_line = NULL; | |
730 | ||
731 | int n; | |
732 | for (n = 0; n < o->emitted_symbols->nr; n++) { | |
733 | struct hashmap *hm; | |
734 | struct moved_entry *key; | |
735 | ||
736 | switch (o->emitted_symbols->buf[n].s) { | |
737 | case DIFF_SYMBOL_PLUS: | |
738 | hm = add_lines; | |
739 | break; | |
740 | case DIFF_SYMBOL_MINUS: | |
741 | hm = del_lines; | |
742 | break; | |
743 | default: | |
744 | prev_line = NULL; | |
745 | continue; | |
746 | } | |
747 | ||
748 | key = prepare_entry(o, n); | |
749 | if (prev_line && prev_line->es->s == o->emitted_symbols->buf[n].s) | |
750 | prev_line->next_line = key; | |
751 | ||
752 | hashmap_add(hm, key); | |
753 | prev_line = key; | |
754 | } | |
755 | } | |
756 | ||
757 | static int shrink_potential_moved_blocks(struct moved_entry **pmb, | |
758 | int pmb_nr) | |
759 | { | |
760 | int lp, rp; | |
761 | ||
762 | /* Shrink the set of potential block to the remaining running */ | |
763 | for (lp = 0, rp = pmb_nr - 1; lp <= rp;) { | |
764 | while (lp < pmb_nr && pmb[lp]) | |
765 | lp++; | |
766 | /* lp points at the first NULL now */ | |
767 | ||
768 | while (rp > -1 && !pmb[rp]) | |
769 | rp--; | |
770 | /* rp points at the last non-NULL */ | |
771 | ||
772 | if (lp < pmb_nr && rp > -1 && lp < rp) { | |
773 | pmb[lp] = pmb[rp]; | |
774 | pmb[rp] = NULL; | |
775 | rp--; | |
776 | lp++; | |
777 | } | |
778 | } | |
779 | ||
780 | /* Remember the number of running sets */ | |
781 | return rp + 1; | |
782 | } | |
783 | ||
09153277 JT |
784 | /* |
785 | * If o->color_moved is COLOR_MOVED_PLAIN, this function does nothing. | |
786 | * | |
f0b8fb6e JT |
787 | * Otherwise, if the last block has fewer alphanumeric characters than |
788 | * COLOR_MOVED_MIN_ALNUM_COUNT, unset DIFF_SYMBOL_MOVED_LINE on all lines in | |
09153277 JT |
789 | * that block. |
790 | * | |
791 | * The last block consists of the (n - block_length)'th line up to but not | |
792 | * including the nth line. | |
f0b8fb6e JT |
793 | * |
794 | * NEEDSWORK: This uses the same heuristic as blame_entry_score() in blame.c. | |
795 | * Think of a way to unify them. | |
09153277 JT |
796 | */ |
797 | static void adjust_last_block(struct diff_options *o, int n, int block_length) | |
798 | { | |
f0b8fb6e JT |
799 | int i, alnum_count = 0; |
800 | if (o->color_moved == COLOR_MOVED_PLAIN) | |
09153277 | 801 | return; |
f0b8fb6e JT |
802 | for (i = 1; i < block_length + 1; i++) { |
803 | const char *c = o->emitted_symbols->buf[n - i].line; | |
804 | for (; *c; c++) { | |
805 | if (!isalnum(*c)) | |
806 | continue; | |
807 | alnum_count++; | |
808 | if (alnum_count >= COLOR_MOVED_MIN_ALNUM_COUNT) | |
809 | return; | |
810 | } | |
811 | } | |
09153277 JT |
812 | for (i = 1; i < block_length + 1; i++) |
813 | o->emitted_symbols->buf[n - i].flags &= ~DIFF_SYMBOL_MOVED_LINE; | |
814 | } | |
815 | ||
2e2d5ac1 SB |
816 | /* Find blocks of moved code, delegate actual coloring decision to helper */ |
817 | static void mark_color_as_moved(struct diff_options *o, | |
818 | struct hashmap *add_lines, | |
819 | struct hashmap *del_lines) | |
820 | { | |
821 | struct moved_entry **pmb = NULL; /* potentially moved blocks */ | |
822 | int pmb_nr = 0, pmb_alloc = 0; | |
823 | int n, flipped_block = 1, block_length = 0; | |
824 | ||
825 | ||
826 | for (n = 0; n < o->emitted_symbols->nr; n++) { | |
827 | struct hashmap *hm = NULL; | |
828 | struct moved_entry *key; | |
829 | struct moved_entry *match = NULL; | |
830 | struct emitted_diff_symbol *l = &o->emitted_symbols->buf[n]; | |
831 | int i; | |
832 | ||
833 | switch (l->s) { | |
834 | case DIFF_SYMBOL_PLUS: | |
835 | hm = del_lines; | |
836 | key = prepare_entry(o, n); | |
837 | match = hashmap_get(hm, key, o); | |
838 | free(key); | |
839 | break; | |
840 | case DIFF_SYMBOL_MINUS: | |
841 | hm = add_lines; | |
842 | key = prepare_entry(o, n); | |
843 | match = hashmap_get(hm, key, o); | |
844 | free(key); | |
845 | break; | |
846 | default: | |
847 | flipped_block = 1; | |
848 | } | |
849 | ||
850 | if (!match) { | |
09153277 | 851 | adjust_last_block(o, n, block_length); |
2e2d5ac1 SB |
852 | pmb_nr = 0; |
853 | block_length = 0; | |
854 | continue; | |
855 | } | |
856 | ||
857 | l->flags |= DIFF_SYMBOL_MOVED_LINE; | |
2e2d5ac1 | 858 | |
176841f0 SB |
859 | if (o->color_moved == COLOR_MOVED_PLAIN) |
860 | continue; | |
861 | ||
2e2d5ac1 SB |
862 | /* Check any potential block runs, advance each or nullify */ |
863 | for (i = 0; i < pmb_nr; i++) { | |
864 | struct moved_entry *p = pmb[i]; | |
865 | struct moved_entry *pnext = (p && p->next_line) ? | |
866 | p->next_line : NULL; | |
867 | if (pnext && !hm->cmpfn(o, pnext, match, NULL)) { | |
868 | pmb[i] = p->next_line; | |
869 | } else { | |
870 | pmb[i] = NULL; | |
871 | } | |
872 | } | |
873 | ||
874 | pmb_nr = shrink_potential_moved_blocks(pmb, pmb_nr); | |
875 | ||
876 | if (pmb_nr == 0) { | |
877 | /* | |
878 | * The current line is the start of a new block. | |
879 | * Setup the set of potential blocks. | |
880 | */ | |
881 | for (; match; match = hashmap_get_next(hm, match)) { | |
882 | ALLOC_GROW(pmb, pmb_nr + 1, pmb_alloc); | |
883 | pmb[pmb_nr++] = match; | |
884 | } | |
885 | ||
886 | flipped_block = (flipped_block + 1) % 2; | |
f0b8fb6e JT |
887 | |
888 | adjust_last_block(o, n, block_length); | |
889 | block_length = 0; | |
2e2d5ac1 SB |
890 | } |
891 | ||
f0b8fb6e JT |
892 | block_length++; |
893 | ||
2e2d5ac1 SB |
894 | if (flipped_block) |
895 | l->flags |= DIFF_SYMBOL_MOVED_LINE_ALT; | |
896 | } | |
09153277 | 897 | adjust_last_block(o, n, block_length); |
2e2d5ac1 SB |
898 | |
899 | free(pmb); | |
900 | } | |
e6e045f8 | 901 | |
86b452e2 SB |
902 | #define DIFF_SYMBOL_MOVED_LINE_ZEBRA_MASK \ |
903 | (DIFF_SYMBOL_MOVED_LINE | DIFF_SYMBOL_MOVED_LINE_ALT) | |
904 | static void dim_moved_lines(struct diff_options *o) | |
905 | { | |
906 | int n; | |
907 | for (n = 0; n < o->emitted_symbols->nr; n++) { | |
908 | struct emitted_diff_symbol *prev = (n != 0) ? | |
909 | &o->emitted_symbols->buf[n - 1] : NULL; | |
910 | struct emitted_diff_symbol *l = &o->emitted_symbols->buf[n]; | |
911 | struct emitted_diff_symbol *next = | |
912 | (n < o->emitted_symbols->nr - 1) ? | |
913 | &o->emitted_symbols->buf[n + 1] : NULL; | |
914 | ||
915 | /* Not a plus or minus line? */ | |
916 | if (l->s != DIFF_SYMBOL_PLUS && l->s != DIFF_SYMBOL_MINUS) | |
917 | continue; | |
918 | ||
919 | /* Not a moved line? */ | |
920 | if (!(l->flags & DIFF_SYMBOL_MOVED_LINE)) | |
921 | continue; | |
922 | ||
923 | /* | |
924 | * If prev or next are not a plus or minus line, | |
925 | * pretend they don't exist | |
926 | */ | |
927 | if (prev && prev->s != DIFF_SYMBOL_PLUS && | |
928 | prev->s != DIFF_SYMBOL_MINUS) | |
929 | prev = NULL; | |
930 | if (next && next->s != DIFF_SYMBOL_PLUS && | |
931 | next->s != DIFF_SYMBOL_MINUS) | |
932 | next = NULL; | |
933 | ||
934 | /* Inside a block? */ | |
935 | if ((prev && | |
936 | (prev->flags & DIFF_SYMBOL_MOVED_LINE_ZEBRA_MASK) == | |
937 | (l->flags & DIFF_SYMBOL_MOVED_LINE_ZEBRA_MASK)) && | |
938 | (next && | |
939 | (next->flags & DIFF_SYMBOL_MOVED_LINE_ZEBRA_MASK) == | |
940 | (l->flags & DIFF_SYMBOL_MOVED_LINE_ZEBRA_MASK))) { | |
941 | l->flags |= DIFF_SYMBOL_MOVED_LINE_UNINTERESTING; | |
942 | continue; | |
943 | } | |
944 | ||
945 | /* Check if we are at an interesting bound: */ | |
946 | if (prev && (prev->flags & DIFF_SYMBOL_MOVED_LINE) && | |
947 | (prev->flags & DIFF_SYMBOL_MOVED_LINE_ALT) != | |
948 | (l->flags & DIFF_SYMBOL_MOVED_LINE_ALT)) | |
949 | continue; | |
950 | if (next && (next->flags & DIFF_SYMBOL_MOVED_LINE) && | |
951 | (next->flags & DIFF_SYMBOL_MOVED_LINE_ALT) != | |
952 | (l->flags & DIFF_SYMBOL_MOVED_LINE_ALT)) | |
953 | continue; | |
954 | ||
955 | /* | |
956 | * The boundary to prev and next are not interesting, | |
957 | * so this line is not interesting as a whole | |
958 | */ | |
959 | l->flags |= DIFF_SYMBOL_MOVED_LINE_UNINTERESTING; | |
960 | } | |
961 | } | |
962 | ||
091f8e28 SB |
963 | static void emit_line_ws_markup(struct diff_options *o, |
964 | const char *set, const char *reset, | |
965 | const char *line, int len, char sign, | |
966 | unsigned ws_rule, int blank_at_eof) | |
6957eb9a | 967 | { |
b8767f79 | 968 | const char *ws = NULL; |
6957eb9a | 969 | |
091f8e28 SB |
970 | if (o->ws_error_highlight & ws_rule) { |
971 | ws = diff_get_color_opt(o, DIFF_WHITESPACE); | |
b8767f79 JH |
972 | if (!*ws) |
973 | ws = NULL; | |
974 | } | |
975 | ||
976 | if (!ws) | |
091f8e28 SB |
977 | emit_line_0(o, set, reset, sign, line, len); |
978 | else if (blank_at_eof) | |
6957eb9a | 979 | /* Blank line at EOF - paint '+' as well */ |
091f8e28 | 980 | emit_line_0(o, ws, reset, sign, line, len); |
6957eb9a JH |
981 | else { |
982 | /* Emit just the prefix, then the rest. */ | |
091f8e28 SB |
983 | emit_line_0(o, set, reset, sign, "", 0); |
984 | ws_check_emit(line, len, ws_rule, | |
985 | o->file, set, reset, ws); | |
6957eb9a JH |
986 | } |
987 | } | |
988 | ||
e6e045f8 SB |
989 | static void emit_diff_symbol_from_struct(struct diff_options *o, |
990 | struct emitted_diff_symbol *eds) | |
36a4cefd | 991 | { |
b9cbfde6 | 992 | static const char *nneof = " No newline at end of file\n"; |
5af6ea95 | 993 | const char *context, *reset, *set, *meta, *fraginfo; |
0911c475 | 994 | struct strbuf sb = STRBUF_INIT; |
e6e045f8 SB |
995 | |
996 | enum diff_symbol s = eds->s; | |
997 | const char *line = eds->line; | |
998 | int len = eds->len; | |
999 | unsigned flags = eds->flags; | |
1000 | ||
36a4cefd | 1001 | switch (s) { |
b9cbfde6 SB |
1002 | case DIFF_SYMBOL_NO_LF_EOF: |
1003 | context = diff_get_color_opt(o, DIFF_CONTEXT); | |
1004 | reset = diff_get_color_opt(o, DIFF_RESET); | |
1005 | putc('\n', o->file); | |
1006 | emit_line_0(o, context, reset, '\\', | |
1007 | nneof, strlen(nneof)); | |
1008 | break; | |
f3597138 SB |
1009 | case DIFF_SYMBOL_SUBMODULE_HEADER: |
1010 | case DIFF_SYMBOL_SUBMODULE_ERROR: | |
1011 | case DIFF_SYMBOL_SUBMODULE_PIPETHROUGH: | |
0911c475 | 1012 | case DIFF_SYMBOL_STATS_SUMMARY_INSERTS_DELETES: |
146fdb0d | 1013 | case DIFF_SYMBOL_SUMMARY: |
0911c475 | 1014 | case DIFF_SYMBOL_STATS_LINE: |
4eed0ebd | 1015 | case DIFF_SYMBOL_BINARY_DIFF_BODY: |
68abc6f1 SB |
1016 | case DIFF_SYMBOL_CONTEXT_FRAGINFO: |
1017 | emit_line(o, "", "", line, len); | |
1018 | break; | |
f2bb1218 | 1019 | case DIFF_SYMBOL_CONTEXT_INCOMPLETE: |
c64b420b SB |
1020 | case DIFF_SYMBOL_CONTEXT_MARKER: |
1021 | context = diff_get_color_opt(o, DIFF_CONTEXT); | |
1022 | reset = diff_get_color_opt(o, DIFF_RESET); | |
1023 | emit_line(o, context, reset, line, len); | |
1024 | break; | |
36a4cefd SB |
1025 | case DIFF_SYMBOL_SEPARATOR: |
1026 | fprintf(o->file, "%s%c", | |
1027 | diff_line_prefix(o), | |
1028 | o->line_termination); | |
1029 | break; | |
091f8e28 SB |
1030 | case DIFF_SYMBOL_CONTEXT: |
1031 | set = diff_get_color_opt(o, DIFF_CONTEXT); | |
1032 | reset = diff_get_color_opt(o, DIFF_RESET); | |
1033 | emit_line_ws_markup(o, set, reset, line, len, ' ', | |
1034 | flags & (DIFF_SYMBOL_CONTENT_WS_MASK), 0); | |
1035 | break; | |
1036 | case DIFF_SYMBOL_PLUS: | |
86b452e2 SB |
1037 | switch (flags & (DIFF_SYMBOL_MOVED_LINE | |
1038 | DIFF_SYMBOL_MOVED_LINE_ALT | | |
1039 | DIFF_SYMBOL_MOVED_LINE_UNINTERESTING)) { | |
1040 | case DIFF_SYMBOL_MOVED_LINE | | |
1041 | DIFF_SYMBOL_MOVED_LINE_ALT | | |
1042 | DIFF_SYMBOL_MOVED_LINE_UNINTERESTING: | |
1043 | set = diff_get_color_opt(o, DIFF_FILE_NEW_MOVED_ALT_DIM); | |
1044 | break; | |
1045 | case DIFF_SYMBOL_MOVED_LINE | | |
1046 | DIFF_SYMBOL_MOVED_LINE_ALT: | |
2e2d5ac1 | 1047 | set = diff_get_color_opt(o, DIFF_FILE_NEW_MOVED_ALT); |
86b452e2 SB |
1048 | break; |
1049 | case DIFF_SYMBOL_MOVED_LINE | | |
1050 | DIFF_SYMBOL_MOVED_LINE_UNINTERESTING: | |
1051 | set = diff_get_color_opt(o, DIFF_FILE_NEW_MOVED_DIM); | |
1052 | break; | |
1053 | case DIFF_SYMBOL_MOVED_LINE: | |
2e2d5ac1 | 1054 | set = diff_get_color_opt(o, DIFF_FILE_NEW_MOVED); |
86b452e2 SB |
1055 | break; |
1056 | default: | |
2e2d5ac1 | 1057 | set = diff_get_color_opt(o, DIFF_FILE_NEW); |
86b452e2 | 1058 | } |
091f8e28 SB |
1059 | reset = diff_get_color_opt(o, DIFF_RESET); |
1060 | emit_line_ws_markup(o, set, reset, line, len, '+', | |
1061 | flags & DIFF_SYMBOL_CONTENT_WS_MASK, | |
1062 | flags & DIFF_SYMBOL_CONTENT_BLANK_LINE_EOF); | |
1063 | break; | |
1064 | case DIFF_SYMBOL_MINUS: | |
86b452e2 SB |
1065 | switch (flags & (DIFF_SYMBOL_MOVED_LINE | |
1066 | DIFF_SYMBOL_MOVED_LINE_ALT | | |
1067 | DIFF_SYMBOL_MOVED_LINE_UNINTERESTING)) { | |
1068 | case DIFF_SYMBOL_MOVED_LINE | | |
1069 | DIFF_SYMBOL_MOVED_LINE_ALT | | |
1070 | DIFF_SYMBOL_MOVED_LINE_UNINTERESTING: | |
1071 | set = diff_get_color_opt(o, DIFF_FILE_OLD_MOVED_ALT_DIM); | |
1072 | break; | |
1073 | case DIFF_SYMBOL_MOVED_LINE | | |
1074 | DIFF_SYMBOL_MOVED_LINE_ALT: | |
2e2d5ac1 | 1075 | set = diff_get_color_opt(o, DIFF_FILE_OLD_MOVED_ALT); |
86b452e2 SB |
1076 | break; |
1077 | case DIFF_SYMBOL_MOVED_LINE | | |
1078 | DIFF_SYMBOL_MOVED_LINE_UNINTERESTING: | |
1079 | set = diff_get_color_opt(o, DIFF_FILE_OLD_MOVED_DIM); | |
1080 | break; | |
1081 | case DIFF_SYMBOL_MOVED_LINE: | |
2e2d5ac1 | 1082 | set = diff_get_color_opt(o, DIFF_FILE_OLD_MOVED); |
86b452e2 SB |
1083 | break; |
1084 | default: | |
2e2d5ac1 | 1085 | set = diff_get_color_opt(o, DIFF_FILE_OLD); |
86b452e2 | 1086 | } |
091f8e28 SB |
1087 | reset = diff_get_color_opt(o, DIFF_RESET); |
1088 | emit_line_ws_markup(o, set, reset, line, len, '-', | |
1089 | flags & DIFF_SYMBOL_CONTENT_WS_MASK, 0); | |
1090 | break; | |
ff958679 SB |
1091 | case DIFF_SYMBOL_WORDS_PORCELAIN: |
1092 | context = diff_get_color_opt(o, DIFF_CONTEXT); | |
1093 | reset = diff_get_color_opt(o, DIFF_RESET); | |
1094 | emit_line(o, context, reset, line, len); | |
1095 | fputs("~\n", o->file); | |
1096 | break; | |
1097 | case DIFF_SYMBOL_WORDS: | |
1098 | context = diff_get_color_opt(o, DIFF_CONTEXT); | |
1099 | reset = diff_get_color_opt(o, DIFF_RESET); | |
1100 | /* | |
1101 | * Skip the prefix character, if any. With | |
1102 | * diff_suppress_blank_empty, there may be | |
1103 | * none. | |
1104 | */ | |
1105 | if (line[0] != '\n') { | |
1106 | line++; | |
1107 | len--; | |
1108 | } | |
1109 | emit_line(o, context, reset, line, len); | |
1110 | break; | |
3ee8b7bf SB |
1111 | case DIFF_SYMBOL_FILEPAIR_PLUS: |
1112 | meta = diff_get_color_opt(o, DIFF_METAINFO); | |
1113 | reset = diff_get_color_opt(o, DIFF_RESET); | |
1114 | fprintf(o->file, "%s%s+++ %s%s%s\n", diff_line_prefix(o), meta, | |
1115 | line, reset, | |
1116 | strchr(line, ' ') ? "\t" : ""); | |
1117 | break; | |
1118 | case DIFF_SYMBOL_FILEPAIR_MINUS: | |
1119 | meta = diff_get_color_opt(o, DIFF_METAINFO); | |
1120 | reset = diff_get_color_opt(o, DIFF_RESET); | |
1121 | fprintf(o->file, "%s%s--- %s%s%s\n", diff_line_prefix(o), meta, | |
1122 | line, reset, | |
1123 | strchr(line, ' ') ? "\t" : ""); | |
1124 | break; | |
4acaaa7a | 1125 | case DIFF_SYMBOL_BINARY_FILES: |
a29b0a13 SB |
1126 | case DIFF_SYMBOL_HEADER: |
1127 | fprintf(o->file, "%s", line); | |
1128 | break; | |
4eed0ebd SB |
1129 | case DIFF_SYMBOL_BINARY_DIFF_HEADER: |
1130 | fprintf(o->file, "%sGIT binary patch\n", diff_line_prefix(o)); | |
1131 | break; | |
1132 | case DIFF_SYMBOL_BINARY_DIFF_HEADER_DELTA: | |
1133 | fprintf(o->file, "%sdelta %s\n", diff_line_prefix(o), line); | |
1134 | break; | |
1135 | case DIFF_SYMBOL_BINARY_DIFF_HEADER_LITERAL: | |
1136 | fprintf(o->file, "%sliteral %s\n", diff_line_prefix(o), line); | |
1137 | break; | |
1138 | case DIFF_SYMBOL_BINARY_DIFF_FOOTER: | |
1139 | fputs(diff_line_prefix(o), o->file); | |
1140 | fputc('\n', o->file); | |
1141 | break; | |
5af6ea95 SB |
1142 | case DIFF_SYMBOL_REWRITE_DIFF: |
1143 | fraginfo = diff_get_color(o->use_color, DIFF_FRAGINFO); | |
1144 | reset = diff_get_color_opt(o, DIFF_RESET); | |
1145 | emit_line(o, fraginfo, reset, line, len); | |
1146 | break; | |
f3597138 SB |
1147 | case DIFF_SYMBOL_SUBMODULE_ADD: |
1148 | set = diff_get_color_opt(o, DIFF_FILE_NEW); | |
1149 | reset = diff_get_color_opt(o, DIFF_RESET); | |
1150 | emit_line(o, set, reset, line, len); | |
1151 | break; | |
1152 | case DIFF_SYMBOL_SUBMODULE_DEL: | |
1153 | set = diff_get_color_opt(o, DIFF_FILE_OLD); | |
1154 | reset = diff_get_color_opt(o, DIFF_RESET); | |
1155 | emit_line(o, set, reset, line, len); | |
1156 | break; | |
1157 | case DIFF_SYMBOL_SUBMODULE_UNTRACKED: | |
1158 | fprintf(o->file, "%sSubmodule %s contains untracked content\n", | |
1159 | diff_line_prefix(o), line); | |
1160 | break; | |
1161 | case DIFF_SYMBOL_SUBMODULE_MODIFIED: | |
1162 | fprintf(o->file, "%sSubmodule %s contains modified content\n", | |
1163 | diff_line_prefix(o), line); | |
1164 | break; | |
0911c475 SB |
1165 | case DIFF_SYMBOL_STATS_SUMMARY_NO_FILES: |
1166 | emit_line(o, "", "", " 0 files changed\n", | |
1167 | strlen(" 0 files changed\n")); | |
1168 | break; | |
1169 | case DIFF_SYMBOL_STATS_SUMMARY_ABBREV: | |
1170 | emit_line(o, "", "", " ...\n", strlen(" ...\n")); | |
1171 | break; | |
bd033291 SB |
1172 | case DIFF_SYMBOL_WORD_DIFF: |
1173 | fprintf(o->file, "%.*s", len, line); | |
1174 | break; | |
30b7e1e7 SB |
1175 | case DIFF_SYMBOL_STAT_SEP: |
1176 | fputs(o->stat_sep, o->file); | |
1177 | break; | |
36a4cefd SB |
1178 | default: |
1179 | die("BUG: unknown diff symbol"); | |
1180 | } | |
0911c475 | 1181 | strbuf_release(&sb); |
36a4cefd SB |
1182 | } |
1183 | ||
e6e045f8 SB |
1184 | static void emit_diff_symbol(struct diff_options *o, enum diff_symbol s, |
1185 | const char *line, int len, unsigned flags) | |
1186 | { | |
1187 | struct emitted_diff_symbol e = {line, len, flags, s}; | |
1188 | ||
1189 | if (o->emitted_symbols) | |
1190 | append_emitted_diff_symbol(o, &e); | |
1191 | else | |
1192 | emit_diff_symbol_from_struct(o, &e); | |
1193 | } | |
1194 | ||
f3597138 SB |
1195 | void diff_emit_submodule_del(struct diff_options *o, const char *line) |
1196 | { | |
1197 | emit_diff_symbol(o, DIFF_SYMBOL_SUBMODULE_DEL, line, strlen(line), 0); | |
1198 | } | |
1199 | ||
1200 | void diff_emit_submodule_add(struct diff_options *o, const char *line) | |
1201 | { | |
1202 | emit_diff_symbol(o, DIFF_SYMBOL_SUBMODULE_ADD, line, strlen(line), 0); | |
1203 | } | |
1204 | ||
1205 | void diff_emit_submodule_untracked(struct diff_options *o, const char *path) | |
1206 | { | |
1207 | emit_diff_symbol(o, DIFF_SYMBOL_SUBMODULE_UNTRACKED, | |
1208 | path, strlen(path), 0); | |
1209 | } | |
1210 | ||
1211 | void diff_emit_submodule_modified(struct diff_options *o, const char *path) | |
1212 | { | |
1213 | emit_diff_symbol(o, DIFF_SYMBOL_SUBMODULE_MODIFIED, | |
1214 | path, strlen(path), 0); | |
1215 | } | |
1216 | ||
1217 | void diff_emit_submodule_header(struct diff_options *o, const char *header) | |
1218 | { | |
1219 | emit_diff_symbol(o, DIFF_SYMBOL_SUBMODULE_HEADER, | |
1220 | header, strlen(header), 0); | |
1221 | } | |
1222 | ||
1223 | void diff_emit_submodule_error(struct diff_options *o, const char *err) | |
1224 | { | |
1225 | emit_diff_symbol(o, DIFF_SYMBOL_SUBMODULE_ERROR, err, strlen(err), 0); | |
1226 | } | |
1227 | ||
1228 | void diff_emit_submodule_pipethrough(struct diff_options *o, | |
1229 | const char *line, int len) | |
1230 | { | |
1231 | emit_diff_symbol(o, DIFF_SYMBOL_SUBMODULE_PIPETHROUGH, line, len, 0); | |
1232 | } | |
1233 | ||
6957eb9a JH |
1234 | static int new_blank_line_at_eof(struct emit_callback *ecbdata, const char *line, int len) |
1235 | { | |
1236 | if (!((ecbdata->ws_rule & WS_BLANK_AT_EOF) && | |
1237 | ecbdata->blank_at_eof_in_preimage && | |
1238 | ecbdata->blank_at_eof_in_postimage && | |
1239 | ecbdata->blank_at_eof_in_preimage <= ecbdata->lno_in_preimage && | |
1240 | ecbdata->blank_at_eof_in_postimage <= ecbdata->lno_in_postimage)) | |
1241 | return 0; | |
018cff70 | 1242 | return ws_blank_line(line, len, ecbdata->ws_rule); |
6957eb9a JH |
1243 | } |
1244 | ||
b8767f79 | 1245 | static void emit_add_line(const char *reset, |
0e383e18 JH |
1246 | struct emit_callback *ecbdata, |
1247 | const char *line, int len) | |
1248 | { | |
091f8e28 SB |
1249 | unsigned flags = WSEH_NEW | ecbdata->ws_rule; |
1250 | if (new_blank_line_at_eof(ecbdata, line, len)) | |
1251 | flags |= DIFF_SYMBOL_CONTENT_BLANK_LINE_EOF; | |
1252 | ||
1253 | emit_diff_symbol(ecbdata->opt, DIFF_SYMBOL_PLUS, line, len, flags); | |
b8767f79 | 1254 | } |
0e383e18 | 1255 | |
b8767f79 JH |
1256 | static void emit_del_line(const char *reset, |
1257 | struct emit_callback *ecbdata, | |
1258 | const char *line, int len) | |
1259 | { | |
091f8e28 SB |
1260 | unsigned flags = WSEH_OLD | ecbdata->ws_rule; |
1261 | emit_diff_symbol(ecbdata->opt, DIFF_SYMBOL_MINUS, line, len, flags); | |
0e383e18 JH |
1262 | } |
1263 | ||
1264 | static void emit_context_line(const char *reset, | |
1265 | struct emit_callback *ecbdata, | |
1266 | const char *line, int len) | |
1267 | { | |
091f8e28 SB |
1268 | unsigned flags = WSEH_CONTEXT | ecbdata->ws_rule; |
1269 | emit_diff_symbol(ecbdata->opt, DIFF_SYMBOL_CONTEXT, line, len, flags); | |
0e383e18 JH |
1270 | } |
1271 | ||
89cb73a1 BW |
1272 | static void emit_hunk_header(struct emit_callback *ecbdata, |
1273 | const char *line, int len) | |
1274 | { | |
8dbf3eb6 | 1275 | const char *context = diff_get_color(ecbdata->color_diff, DIFF_CONTEXT); |
89cb73a1 BW |
1276 | const char *frag = diff_get_color(ecbdata->color_diff, DIFF_FRAGINFO); |
1277 | const char *func = diff_get_color(ecbdata->color_diff, DIFF_FUNCINFO); | |
1278 | const char *reset = diff_get_color(ecbdata->color_diff, DIFF_RESET); | |
1279 | static const char atat[2] = { '@', '@' }; | |
1280 | const char *cp, *ep; | |
2efcc977 BY |
1281 | struct strbuf msgbuf = STRBUF_INIT; |
1282 | int org_len = len; | |
1283 | int i = 1; | |
89cb73a1 BW |
1284 | |
1285 | /* | |
1286 | * As a hunk header must begin with "@@ -<old>, +<new> @@", | |
1287 | * it always is at least 10 bytes long. | |
1288 | */ | |
1289 | if (len < 10 || | |
1290 | memcmp(line, atat, 2) || | |
1291 | !(ep = memmem(line + 2, len - 2, atat, 2))) { | |
c64b420b | 1292 | emit_diff_symbol(ecbdata->opt, |
091f8e28 | 1293 | DIFF_SYMBOL_CONTEXT_MARKER, line, len, 0); |
89cb73a1 BW |
1294 | return; |
1295 | } | |
1296 | ep += 2; /* skip over @@ */ | |
1297 | ||
1298 | /* The hunk header in fraginfo color */ | |
cedc61a9 | 1299 | strbuf_addstr(&msgbuf, frag); |
2efcc977 | 1300 | strbuf_add(&msgbuf, line, ep - line); |
cedc61a9 | 1301 | strbuf_addstr(&msgbuf, reset); |
2efcc977 BY |
1302 | |
1303 | /* | |
1304 | * trailing "\r\n" | |
1305 | */ | |
1306 | for ( ; i < 3; i++) | |
1307 | if (line[len - i] == '\r' || line[len - i] == '\n') | |
1308 | len--; | |
89cb73a1 BW |
1309 | |
1310 | /* blank before the func header */ | |
1311 | for (cp = ep; ep - line < len; ep++) | |
1312 | if (*ep != ' ' && *ep != '\t') | |
1313 | break; | |
2efcc977 | 1314 | if (ep != cp) { |
8dbf3eb6 | 1315 | strbuf_addstr(&msgbuf, context); |
2efcc977 | 1316 | strbuf_add(&msgbuf, cp, ep - cp); |
cedc61a9 | 1317 | strbuf_addstr(&msgbuf, reset); |
2efcc977 BY |
1318 | } |
1319 | ||
1320 | if (ep < line + len) { | |
cedc61a9 | 1321 | strbuf_addstr(&msgbuf, func); |
2efcc977 | 1322 | strbuf_add(&msgbuf, ep, line + len - ep); |
cedc61a9 | 1323 | strbuf_addstr(&msgbuf, reset); |
2efcc977 | 1324 | } |
89cb73a1 | 1325 | |
2efcc977 | 1326 | strbuf_add(&msgbuf, line + len, org_len - len); |
dfb7728f | 1327 | strbuf_complete_line(&msgbuf); |
68abc6f1 | 1328 | emit_diff_symbol(ecbdata->opt, |
091f8e28 | 1329 | DIFF_SYMBOL_CONTEXT_FRAGINFO, msgbuf.buf, msgbuf.len, 0); |
2efcc977 | 1330 | strbuf_release(&msgbuf); |
89cb73a1 BW |
1331 | } |
1332 | ||
479b0ae8 JK |
1333 | static struct diff_tempfile *claim_diff_tempfile(void) { |
1334 | int i; | |
1335 | for (i = 0; i < ARRAY_SIZE(diff_temp); i++) | |
1336 | if (!diff_temp[i].name) | |
1337 | return diff_temp + i; | |
1338 | die("BUG: diff is failing to clean up its tempfiles"); | |
1339 | } | |
1340 | ||
479b0ae8 JK |
1341 | static void remove_tempfile(void) |
1342 | { | |
1343 | int i; | |
a8344abe | 1344 | for (i = 0; i < ARRAY_SIZE(diff_temp); i++) { |
076aa2cb | 1345 | if (is_tempfile_active(diff_temp[i].tempfile)) |
284098f1 | 1346 | delete_tempfile(&diff_temp[i].tempfile); |
a8344abe NR |
1347 | diff_temp[i].name = NULL; |
1348 | } | |
479b0ae8 JK |
1349 | } |
1350 | ||
5af6ea95 | 1351 | static void add_line_count(struct strbuf *out, int count) |
6973dcae JH |
1352 | { |
1353 | switch (count) { | |
1354 | case 0: | |
5af6ea95 | 1355 | strbuf_addstr(out, "0,0"); |
6973dcae JH |
1356 | break; |
1357 | case 1: | |
5af6ea95 | 1358 | strbuf_addstr(out, "1"); |
6973dcae JH |
1359 | break; |
1360 | default: | |
5af6ea95 | 1361 | strbuf_addf(out, "1,%d", count); |
6973dcae JH |
1362 | break; |
1363 | } | |
1364 | } | |
1365 | ||
7f7ee2ff JH |
1366 | static void emit_rewrite_lines(struct emit_callback *ecb, |
1367 | int prefix, const char *data, int size) | |
6973dcae | 1368 | { |
7f7ee2ff | 1369 | const char *endp = NULL; |
7f7ee2ff JH |
1370 | const char *reset = diff_get_color(ecb->color_diff, DIFF_RESET); |
1371 | ||
1372 | while (0 < size) { | |
1373 | int len; | |
1374 | ||
1375 | endp = memchr(data, '\n', size); | |
1376 | len = endp ? (endp - data + 1) : size; | |
1377 | if (prefix != '+') { | |
1378 | ecb->lno_in_preimage++; | |
0e383e18 | 1379 | emit_del_line(reset, ecb, data, len); |
7f7ee2ff JH |
1380 | } else { |
1381 | ecb->lno_in_postimage++; | |
1382 | emit_add_line(reset, ecb, data, len); | |
13e36ec5 | 1383 | } |
7f7ee2ff JH |
1384 | size -= len; |
1385 | data += len; | |
1386 | } | |
b9cbfde6 | 1387 | if (!endp) |
091f8e28 | 1388 | emit_diff_symbol(ecb->opt, DIFF_SYMBOL_NO_LF_EOF, NULL, 0, 0); |
6973dcae JH |
1389 | } |
1390 | ||
1391 | static void emit_rewrite_diff(const char *name_a, | |
1392 | const char *name_b, | |
1393 | struct diff_filespec *one, | |
13e36ec5 | 1394 | struct diff_filespec *two, |
d9bae1a1 JK |
1395 | struct userdiff_driver *textconv_one, |
1396 | struct userdiff_driver *textconv_two, | |
eab9a40b | 1397 | struct diff_options *o) |
6973dcae JH |
1398 | { |
1399 | int lc_a, lc_b; | |
d5625091 | 1400 | static struct strbuf a_name = STRBUF_INIT, b_name = STRBUF_INIT; |
a5a818ee | 1401 | const char *a_prefix, *b_prefix; |
840383b2 | 1402 | char *data_one, *data_two; |
3aa1f7ca | 1403 | size_t size_one, size_two; |
7f7ee2ff | 1404 | struct emit_callback ecbdata; |
5af6ea95 | 1405 | struct strbuf out = STRBUF_INIT; |
a5a818ee | 1406 | |
0d1e0e78 | 1407 | if (diff_mnemonic_prefix && o->flags.reverse_diff) { |
a5a818ee JH |
1408 | a_prefix = o->b_prefix; |
1409 | b_prefix = o->a_prefix; | |
1410 | } else { | |
1411 | a_prefix = o->a_prefix; | |
1412 | b_prefix = o->b_prefix; | |
1413 | } | |
1a9eb3b9 | 1414 | |
8a13becc JH |
1415 | name_a += (*name_a == '/'); |
1416 | name_b += (*name_b == '/'); | |
1a9eb3b9 | 1417 | |
d5625091 JH |
1418 | strbuf_reset(&a_name); |
1419 | strbuf_reset(&b_name); | |
a5a818ee JH |
1420 | quote_two_c_style(&a_name, a_prefix, name_a, 0); |
1421 | quote_two_c_style(&b_name, b_prefix, name_b, 0); | |
d5625091 | 1422 | |
840383b2 JK |
1423 | size_one = fill_textconv(textconv_one, one, &data_one); |
1424 | size_two = fill_textconv(textconv_two, two, &data_two); | |
3aa1f7ca | 1425 | |
d91ba8fa | 1426 | memset(&ecbdata, 0, sizeof(ecbdata)); |
daa0c3d9 | 1427 | ecbdata.color_diff = want_color(o->use_color); |
c189c4f2 | 1428 | ecbdata.ws_rule = whitespace_rule(name_b); |
a3c158d4 | 1429 | ecbdata.opt = o; |
d91ba8fa JH |
1430 | if (ecbdata.ws_rule & WS_BLANK_AT_EOF) { |
1431 | mmfile_t mf1, mf2; | |
1432 | mf1.ptr = (char *)data_one; | |
1433 | mf2.ptr = (char *)data_two; | |
1434 | mf1.size = size_one; | |
1435 | mf2.size = size_two; | |
1436 | check_blank_at_eof(&mf1, &mf2, &ecbdata); | |
1437 | } | |
1438 | ecbdata.lno_in_preimage = 1; | |
1439 | ecbdata.lno_in_postimage = 1; | |
1440 | ||
3aa1f7ca JK |
1441 | lc_a = count_lines(data_one, size_one); |
1442 | lc_b = count_lines(data_two, size_two); | |
3ee8b7bf SB |
1443 | |
1444 | emit_diff_symbol(o, DIFF_SYMBOL_FILEPAIR_MINUS, | |
1445 | a_name.buf, a_name.len, 0); | |
1446 | emit_diff_symbol(o, DIFF_SYMBOL_FILEPAIR_PLUS, | |
1447 | b_name.buf, b_name.len, 0); | |
1448 | ||
5af6ea95 | 1449 | strbuf_addstr(&out, "@@ -"); |
467ddc14 | 1450 | if (!o->irreversible_delete) |
5af6ea95 | 1451 | add_line_count(&out, lc_a); |
467ddc14 | 1452 | else |
5af6ea95 SB |
1453 | strbuf_addstr(&out, "?,?"); |
1454 | strbuf_addstr(&out, " +"); | |
1455 | add_line_count(&out, lc_b); | |
1456 | strbuf_addstr(&out, " @@\n"); | |
1457 | emit_diff_symbol(o, DIFF_SYMBOL_REWRITE_DIFF, out.buf, out.len, 0); | |
1458 | strbuf_release(&out); | |
1459 | ||
467ddc14 | 1460 | if (lc_a && !o->irreversible_delete) |
d91ba8fa | 1461 | emit_rewrite_lines(&ecbdata, '-', data_one, size_one); |
6973dcae | 1462 | if (lc_b) |
d91ba8fa | 1463 | emit_rewrite_lines(&ecbdata, '+', data_two, size_two); |
b76c056b | 1464 | if (textconv_one) |
aed6ca52 | 1465 | free((char *)data_one); |
b76c056b | 1466 | if (textconv_two) |
aed6ca52 | 1467 | free((char *)data_two); |
6973dcae JH |
1468 | } |
1469 | ||
f59a59e2 JS |
1470 | struct diff_words_buffer { |
1471 | mmfile_t text; | |
071bcaab | 1472 | unsigned long alloc; |
2e5d2003 JS |
1473 | struct diff_words_orig { |
1474 | const char *begin, *end; | |
1475 | } *orig; | |
1476 | int orig_nr, orig_alloc; | |
f59a59e2 JS |
1477 | }; |
1478 | ||
1479 | static void diff_words_append(char *line, unsigned long len, | |
1480 | struct diff_words_buffer *buffer) | |
1481 | { | |
23c1575f | 1482 | ALLOC_GROW(buffer->text.ptr, buffer->text.size + len, buffer->alloc); |
f59a59e2 JS |
1483 | line++; |
1484 | len--; | |
1485 | memcpy(buffer->text.ptr + buffer->text.size, line, len); | |
1486 | buffer->text.size += len; | |
2b6a5417 | 1487 | buffer->text.ptr[buffer->text.size] = '\0'; |
f59a59e2 JS |
1488 | } |
1489 | ||
9cba13ca | 1490 | struct diff_words_style_elem { |
882749a0 TR |
1491 | const char *prefix; |
1492 | const char *suffix; | |
1493 | const char *color; /* NULL; filled in by the setup code if | |
1494 | * color is enabled */ | |
1495 | }; | |
1496 | ||
9cba13ca | 1497 | struct diff_words_style { |
882749a0 | 1498 | enum diff_words_type type; |
63a01c3f | 1499 | struct diff_words_style_elem new_word, old_word, ctx; |
882749a0 TR |
1500 | const char *newline; |
1501 | }; | |
1502 | ||
c2e86add | 1503 | static struct diff_words_style diff_words_styles[] = { |
882749a0 TR |
1504 | { DIFF_WORDS_PORCELAIN, {"+", "\n"}, {"-", "\n"}, {" ", "\n"}, "~\n" }, |
1505 | { DIFF_WORDS_PLAIN, {"{+", "+}"}, {"[-", "-]"}, {"", ""}, "\n" }, | |
1506 | { DIFF_WORDS_COLOR, {"", ""}, {"", ""}, {"", ""}, "\n" } | |
1507 | }; | |
1508 | ||
f59a59e2 | 1509 | struct diff_words_data { |
f59a59e2 | 1510 | struct diff_words_buffer minus, plus; |
2e5d2003 | 1511 | const char *current_plus; |
4297c0ae BY |
1512 | int last_minus; |
1513 | struct diff_options *opt; | |
2b6a5417 | 1514 | regex_t *word_regex; |
882749a0 TR |
1515 | enum diff_words_type type; |
1516 | struct diff_words_style *style; | |
f59a59e2 JS |
1517 | }; |
1518 | ||
bd033291 | 1519 | static int fn_out_diff_words_write_helper(struct diff_options *o, |
882749a0 TR |
1520 | struct diff_words_style_elem *st_el, |
1521 | const char *newline, | |
bd033291 | 1522 | size_t count, const char *buf) |
882749a0 | 1523 | { |
4297c0ae | 1524 | int print = 0; |
bd033291 | 1525 | struct strbuf sb = STRBUF_INIT; |
4297c0ae | 1526 | |
882749a0 TR |
1527 | while (count) { |
1528 | char *p = memchr(buf, '\n', count); | |
4297c0ae | 1529 | if (print) |
bd033291 SB |
1530 | strbuf_addstr(&sb, diff_line_prefix(o)); |
1531 | ||
882749a0 | 1532 | if (p != buf) { |
bd033291 SB |
1533 | const char *reset = st_el->color && *st_el->color ? |
1534 | GIT_COLOR_RESET : NULL; | |
1535 | if (st_el->color && *st_el->color) | |
1536 | strbuf_addstr(&sb, st_el->color); | |
1537 | strbuf_addstr(&sb, st_el->prefix); | |
1538 | strbuf_add(&sb, buf, p ? p - buf : count); | |
1539 | strbuf_addstr(&sb, st_el->suffix); | |
1540 | if (reset) | |
1541 | strbuf_addstr(&sb, reset); | |
882749a0 TR |
1542 | } |
1543 | if (!p) | |
bd033291 SB |
1544 | goto out; |
1545 | ||
1546 | strbuf_addstr(&sb, newline); | |
882749a0 TR |
1547 | count -= p + 1 - buf; |
1548 | buf = p + 1; | |
4297c0ae | 1549 | print = 1; |
bd033291 SB |
1550 | if (count) { |
1551 | emit_diff_symbol(o, DIFF_SYMBOL_WORD_DIFF, | |
1552 | sb.buf, sb.len, 0); | |
1553 | strbuf_reset(&sb); | |
1554 | } | |
882749a0 | 1555 | } |
bd033291 SB |
1556 | |
1557 | out: | |
1558 | if (sb.len) | |
1559 | emit_diff_symbol(o, DIFF_SYMBOL_WORD_DIFF, | |
1560 | sb.buf, sb.len, 0); | |
1561 | strbuf_release(&sb); | |
882749a0 TR |
1562 | return 0; |
1563 | } | |
1564 | ||
4297c0ae BY |
1565 | /* |
1566 | * '--color-words' algorithm can be described as: | |
1567 | * | |
5621760f | 1568 | * 1. collect the minus/plus lines of a diff hunk, divided into |
4297c0ae BY |
1569 | * minus-lines and plus-lines; |
1570 | * | |
1571 | * 2. break both minus-lines and plus-lines into words and | |
1572 | * place them into two mmfile_t with one word for each line; | |
1573 | * | |
1574 | * 3. use xdiff to run diff on the two mmfile_t to get the words level diff; | |
1575 | * | |
1576 | * And for the common parts of the both file, we output the plus side text. | |
1577 | * diff_words->current_plus is used to trace the current position of the plus file | |
1578 | * which printed. diff_words->last_minus is used to trace the last minus word | |
1579 | * printed. | |
1580 | * | |
1581 | * For '--graph' to work with '--color-words', we need to output the graph prefix | |
1582 | * on each line of color words output. Generally, there are two conditions on | |
1583 | * which we should output the prefix. | |
1584 | * | |
1585 | * 1. diff_words->last_minus == 0 && | |
1586 | * diff_words->current_plus == diff_words->plus.text.ptr | |
1587 | * | |
1588 | * that is: the plus text must start as a new line, and if there is no minus | |
1589 | * word printed, a graph prefix must be printed. | |
1590 | * | |
1591 | * 2. diff_words->current_plus > diff_words->plus.text.ptr && | |
1592 | * *(diff_words->current_plus - 1) == '\n' | |
1593 | * | |
1594 | * that is: a graph prefix must be printed following a '\n' | |
1595 | */ | |
1596 | static int color_words_output_graph_prefix(struct diff_words_data *diff_words) | |
1597 | { | |
1598 | if ((diff_words->last_minus == 0 && | |
1599 | diff_words->current_plus == diff_words->plus.text.ptr) || | |
1600 | (diff_words->current_plus > diff_words->plus.text.ptr && | |
1601 | *(diff_words->current_plus - 1) == '\n')) { | |
1602 | return 1; | |
1603 | } else { | |
1604 | return 0; | |
1605 | } | |
1606 | } | |
1607 | ||
f59a59e2 | 1608 | static void fn_out_diff_words_aux(void *priv, char *line, unsigned long len) |
f59a59e2 | 1609 | { |
f59a59e2 | 1610 | struct diff_words_data *diff_words = priv; |
882749a0 | 1611 | struct diff_words_style *style = diff_words->style; |
2e5d2003 JS |
1612 | int minus_first, minus_len, plus_first, plus_len; |
1613 | const char *minus_begin, *minus_end, *plus_begin, *plus_end; | |
4297c0ae | 1614 | struct diff_options *opt = diff_words->opt; |
30997bb8 | 1615 | const char *line_prefix; |
f59a59e2 | 1616 | |
2e5d2003 JS |
1617 | if (line[0] != '@' || parse_hunk_header(line, len, |
1618 | &minus_first, &minus_len, &plus_first, &plus_len)) | |
f59a59e2 JS |
1619 | return; |
1620 | ||
4297c0ae | 1621 | assert(opt); |
30997bb8 | 1622 | line_prefix = diff_line_prefix(opt); |
4297c0ae | 1623 | |
2e5d2003 JS |
1624 | /* POSIX requires that first be decremented by one if len == 0... */ |
1625 | if (minus_len) { | |
1626 | minus_begin = diff_words->minus.orig[minus_first].begin; | |
1627 | minus_end = | |
1628 | diff_words->minus.orig[minus_first + minus_len - 1].end; | |
1629 | } else | |
1630 | minus_begin = minus_end = | |
1631 | diff_words->minus.orig[minus_first].end; | |
1632 | ||
1633 | if (plus_len) { | |
1634 | plus_begin = diff_words->plus.orig[plus_first].begin; | |
1635 | plus_end = diff_words->plus.orig[plus_first + plus_len - 1].end; | |
1636 | } else | |
1637 | plus_begin = plus_end = diff_words->plus.orig[plus_first].end; | |
1638 | ||
4297c0ae BY |
1639 | if (color_words_output_graph_prefix(diff_words)) { |
1640 | fputs(line_prefix, diff_words->opt->file); | |
1641 | } | |
1642 | if (diff_words->current_plus != plus_begin) { | |
bd033291 | 1643 | fn_out_diff_words_write_helper(diff_words->opt, |
882749a0 TR |
1644 | &style->ctx, style->newline, |
1645 | plus_begin - diff_words->current_plus, | |
bd033291 | 1646 | diff_words->current_plus); |
4297c0ae BY |
1647 | } |
1648 | if (minus_begin != minus_end) { | |
bd033291 | 1649 | fn_out_diff_words_write_helper(diff_words->opt, |
63a01c3f | 1650 | &style->old_word, style->newline, |
bd033291 | 1651 | minus_end - minus_begin, minus_begin); |
4297c0ae BY |
1652 | } |
1653 | if (plus_begin != plus_end) { | |
bd033291 | 1654 | fn_out_diff_words_write_helper(diff_words->opt, |
63a01c3f | 1655 | &style->new_word, style->newline, |
bd033291 | 1656 | plus_end - plus_begin, plus_begin); |
4297c0ae | 1657 | } |
2e5d2003 JS |
1658 | |
1659 | diff_words->current_plus = plus_end; | |
4297c0ae | 1660 | diff_words->last_minus = minus_first; |
f59a59e2 JS |
1661 | } |
1662 | ||
2b6a5417 JS |
1663 | /* This function starts looking at *begin, and returns 0 iff a word was found. */ |
1664 | static int find_word_boundaries(mmfile_t *buffer, regex_t *word_regex, | |
1665 | int *begin, int *end) | |
1666 | { | |
1667 | if (word_regex && *begin < buffer->size) { | |
1668 | regmatch_t match[1]; | |
b7d36ffc JS |
1669 | if (!regexec_buf(word_regex, buffer->ptr + *begin, |
1670 | buffer->size - *begin, 1, match, 0)) { | |
2b6a5417 JS |
1671 | char *p = memchr(buffer->ptr + *begin + match[0].rm_so, |
1672 | '\n', match[0].rm_eo - match[0].rm_so); | |
1673 | *end = p ? p - buffer->ptr : match[0].rm_eo + *begin; | |
1674 | *begin += match[0].rm_so; | |
1675 | return *begin >= *end; | |
1676 | } | |
1677 | return -1; | |
f59a59e2 JS |
1678 | } |
1679 | ||
2b6a5417 JS |
1680 | /* find the next word */ |
1681 | while (*begin < buffer->size && isspace(buffer->ptr[*begin])) | |
1682 | (*begin)++; | |
1683 | if (*begin >= buffer->size) | |
1684 | return -1; | |
f59a59e2 | 1685 | |
2b6a5417 JS |
1686 | /* find the end of the word */ |
1687 | *end = *begin + 1; | |
1688 | while (*end < buffer->size && !isspace(buffer->ptr[*end])) | |
1689 | (*end)++; | |
1690 | ||
1691 | return 0; | |
f59a59e2 JS |
1692 | } |
1693 | ||
23c1575f | 1694 | /* |
2e5d2003 JS |
1695 | * This function splits the words in buffer->text, stores the list with |
1696 | * newline separator into out, and saves the offsets of the original words | |
1697 | * in buffer->orig. | |
23c1575f | 1698 | */ |
2b6a5417 JS |
1699 | static void diff_words_fill(struct diff_words_buffer *buffer, mmfile_t *out, |
1700 | regex_t *word_regex) | |
f59a59e2 | 1701 | { |
2e5d2003 | 1702 | int i, j; |
2b6a5417 | 1703 | long alloc = 0; |
f59a59e2 | 1704 | |
2e5d2003 | 1705 | out->size = 0; |
2b6a5417 | 1706 | out->ptr = NULL; |
f59a59e2 | 1707 | |
2e5d2003 JS |
1708 | /* fake an empty "0th" word */ |
1709 | ALLOC_GROW(buffer->orig, 1, buffer->orig_alloc); | |
1710 | buffer->orig[0].begin = buffer->orig[0].end = buffer->text.ptr; | |
1711 | buffer->orig_nr = 1; | |
1712 | ||
1713 | for (i = 0; i < buffer->text.size; i++) { | |
2b6a5417 JS |
1714 | if (find_word_boundaries(&buffer->text, word_regex, &i, &j)) |
1715 | return; | |
2e5d2003 JS |
1716 | |
1717 | /* store original boundaries */ | |
1718 | ALLOC_GROW(buffer->orig, buffer->orig_nr + 1, | |
1719 | buffer->orig_alloc); | |
1720 | buffer->orig[buffer->orig_nr].begin = buffer->text.ptr + i; | |
1721 | buffer->orig[buffer->orig_nr].end = buffer->text.ptr + j; | |
1722 | buffer->orig_nr++; | |
1723 | ||
1724 | /* store one word */ | |
2b6a5417 | 1725 | ALLOC_GROW(out->ptr, out->size + j - i + 1, alloc); |
2e5d2003 JS |
1726 | memcpy(out->ptr + out->size, buffer->text.ptr + i, j - i); |
1727 | out->ptr[out->size + j - i] = '\n'; | |
1728 | out->size += j - i + 1; | |
1729 | ||
1730 | i = j - 1; | |
f59a59e2 JS |
1731 | } |
1732 | } | |
1733 | ||
1734 | /* this executes the word diff on the accumulated buffers */ | |
1735 | static void diff_words_show(struct diff_words_data *diff_words) | |
1736 | { | |
1737 | xpparam_t xpp; | |
1738 | xdemitconf_t xecfg; | |
f59a59e2 | 1739 | mmfile_t minus, plus; |
882749a0 | 1740 | struct diff_words_style *style = diff_words->style; |
f59a59e2 | 1741 | |
4297c0ae | 1742 | struct diff_options *opt = diff_words->opt; |
30997bb8 | 1743 | const char *line_prefix; |
4297c0ae BY |
1744 | |
1745 | assert(opt); | |
30997bb8 | 1746 | line_prefix = diff_line_prefix(opt); |
4297c0ae | 1747 | |
2e5d2003 JS |
1748 | /* special case: only removal */ |
1749 | if (!diff_words->plus.text.size) { | |
bd033291 SB |
1750 | emit_diff_symbol(diff_words->opt, DIFF_SYMBOL_WORD_DIFF, |
1751 | line_prefix, strlen(line_prefix), 0); | |
1752 | fn_out_diff_words_write_helper(diff_words->opt, | |
63a01c3f | 1753 | &style->old_word, style->newline, |
4297c0ae | 1754 | diff_words->minus.text.size, |
bd033291 | 1755 | diff_words->minus.text.ptr); |
2e5d2003 JS |
1756 | diff_words->minus.text.size = 0; |
1757 | return; | |
1758 | } | |
1759 | ||
1760 | diff_words->current_plus = diff_words->plus.text.ptr; | |
4297c0ae | 1761 | diff_words->last_minus = 0; |
f59a59e2 | 1762 | |
9ccd0a88 | 1763 | memset(&xpp, 0, sizeof(xpp)); |
30b25010 | 1764 | memset(&xecfg, 0, sizeof(xecfg)); |
2b6a5417 JS |
1765 | diff_words_fill(&diff_words->minus, &minus, diff_words->word_regex); |
1766 | diff_words_fill(&diff_words->plus, &plus, diff_words->word_regex); | |
582aa00b | 1767 | xpp.flags = 0; |
2b6a5417 | 1768 | /* as only the hunk header will be parsed, we need a 0-context */ |
2e5d2003 | 1769 | xecfg.ctxlen = 0; |
3efb9880 JK |
1770 | if (xdi_diff_outf(&minus, &plus, fn_out_diff_words_aux, diff_words, |
1771 | &xpp, &xecfg)) | |
1772 | die("unable to generate word diff"); | |
f59a59e2 JS |
1773 | free(minus.ptr); |
1774 | free(plus.ptr); | |
2e5d2003 | 1775 | if (diff_words->current_plus != diff_words->plus.text.ptr + |
4297c0ae BY |
1776 | diff_words->plus.text.size) { |
1777 | if (color_words_output_graph_prefix(diff_words)) | |
bd033291 SB |
1778 | emit_diff_symbol(diff_words->opt, DIFF_SYMBOL_WORD_DIFF, |
1779 | line_prefix, strlen(line_prefix), 0); | |
1780 | fn_out_diff_words_write_helper(diff_words->opt, | |
882749a0 | 1781 | &style->ctx, style->newline, |
2e5d2003 | 1782 | diff_words->plus.text.ptr + diff_words->plus.text.size |
bd033291 | 1783 | - diff_words->current_plus, diff_words->current_plus); |
4297c0ae | 1784 | } |
f59a59e2 | 1785 | diff_words->minus.text.size = diff_words->plus.text.size = 0; |
f59a59e2 JS |
1786 | } |
1787 | ||
76fd2828 JH |
1788 | /* In "color-words" mode, show word-diff of words accumulated in the buffer */ |
1789 | static void diff_words_flush(struct emit_callback *ecbdata) | |
1790 | { | |
e6e045f8 SB |
1791 | struct diff_options *wo = ecbdata->diff_words->opt; |
1792 | ||
76fd2828 JH |
1793 | if (ecbdata->diff_words->minus.text.size || |
1794 | ecbdata->diff_words->plus.text.size) | |
1795 | diff_words_show(ecbdata->diff_words); | |
e6e045f8 SB |
1796 | |
1797 | if (wo->emitted_symbols) { | |
1798 | struct diff_options *o = ecbdata->opt; | |
1799 | struct emitted_diff_symbols *wol = wo->emitted_symbols; | |
1800 | int i; | |
1801 | ||
1802 | /* | |
1803 | * NEEDSWORK: | |
1804 | * Instead of appending each, concat all words to a line? | |
1805 | */ | |
1806 | for (i = 0; i < wol->nr; i++) | |
1807 | append_emitted_diff_symbol(o, &wol->buf[i]); | |
1808 | ||
1809 | for (i = 0; i < wol->nr; i++) | |
1810 | free((void *)wol->buf[i].line); | |
1811 | ||
1812 | wol->nr = 0; | |
1813 | } | |
76fd2828 JH |
1814 | } |
1815 | ||
77d1a520 TR |
1816 | static void diff_filespec_load_driver(struct diff_filespec *one) |
1817 | { | |
1818 | /* Use already-loaded driver */ | |
1819 | if (one->driver) | |
1820 | return; | |
1821 | ||
1822 | if (S_ISREG(one->mode)) | |
1823 | one->driver = userdiff_find_by_path(one->path); | |
1824 | ||
1825 | /* Fallback to default settings */ | |
1826 | if (!one->driver) | |
1827 | one->driver = userdiff_find_by_name("default"); | |
1828 | } | |
1829 | ||
1830 | static const char *userdiff_word_regex(struct diff_filespec *one) | |
1831 | { | |
1832 | diff_filespec_load_driver(one); | |
1833 | return one->driver->word_regex; | |
1834 | } | |
1835 | ||
1836 | static void init_diff_words_data(struct emit_callback *ecbdata, | |
6440d341 | 1837 | struct diff_options *orig_opts, |
77d1a520 TR |
1838 | struct diff_filespec *one, |
1839 | struct diff_filespec *two) | |
1840 | { | |
1841 | int i; | |
6440d341 TR |
1842 | struct diff_options *o = xmalloc(sizeof(struct diff_options)); |
1843 | memcpy(o, orig_opts, sizeof(struct diff_options)); | |
77d1a520 TR |
1844 | |
1845 | ecbdata->diff_words = | |
1846 | xcalloc(1, sizeof(struct diff_words_data)); | |
1847 | ecbdata->diff_words->type = o->word_diff; | |
1848 | ecbdata->diff_words->opt = o; | |
e6e045f8 SB |
1849 | |
1850 | if (orig_opts->emitted_symbols) | |
1851 | o->emitted_symbols = | |
1852 | xcalloc(1, sizeof(struct emitted_diff_symbols)); | |
1853 | ||
77d1a520 TR |
1854 | if (!o->word_regex) |
1855 | o->word_regex = userdiff_word_regex(one); | |
1856 | if (!o->word_regex) | |
1857 | o->word_regex = userdiff_word_regex(two); | |
1858 | if (!o->word_regex) | |
1859 | o->word_regex = diff_word_regex_cfg; | |
1860 | if (o->word_regex) { | |
1861 | ecbdata->diff_words->word_regex = (regex_t *) | |
1862 | xmalloc(sizeof(regex_t)); | |
1863 | if (regcomp(ecbdata->diff_words->word_regex, | |
1864 | o->word_regex, | |
1865 | REG_EXTENDED | REG_NEWLINE)) | |
1866 | die ("Invalid regular expression: %s", | |
1867 | o->word_regex); | |
1868 | } | |
1869 | for (i = 0; i < ARRAY_SIZE(diff_words_styles); i++) { | |
1870 | if (o->word_diff == diff_words_styles[i].type) { | |
1871 | ecbdata->diff_words->style = | |
1872 | &diff_words_styles[i]; | |
1873 | break; | |
1874 | } | |
1875 | } | |
1876 | if (want_color(o->use_color)) { | |
1877 | struct diff_words_style *st = ecbdata->diff_words->style; | |
63a01c3f BW |
1878 | st->old_word.color = diff_get_color_opt(o, DIFF_FILE_OLD); |
1879 | st->new_word.color = diff_get_color_opt(o, DIFF_FILE_NEW); | |
8dbf3eb6 | 1880 | st->ctx.color = diff_get_color_opt(o, DIFF_CONTEXT); |
77d1a520 TR |
1881 | } |
1882 | } | |
1883 | ||
f59a59e2 JS |
1884 | static void free_diff_words_data(struct emit_callback *ecbdata) |
1885 | { | |
1886 | if (ecbdata->diff_words) { | |
76fd2828 | 1887 | diff_words_flush(ecbdata); |
e6e045f8 | 1888 | free (ecbdata->diff_words->opt->emitted_symbols); |
6440d341 | 1889 | free (ecbdata->diff_words->opt); |
8e0f7003 | 1890 | free (ecbdata->diff_words->minus.text.ptr); |
2e5d2003 | 1891 | free (ecbdata->diff_words->minus.orig); |
8e0f7003 | 1892 | free (ecbdata->diff_words->plus.text.ptr); |
2e5d2003 | 1893 | free (ecbdata->diff_words->plus.orig); |
ef5644ea BC |
1894 | if (ecbdata->diff_words->word_regex) { |
1895 | regfree(ecbdata->diff_words->word_regex); | |
1896 | free(ecbdata->diff_words->word_regex); | |
1897 | } | |
6a83d902 | 1898 | FREE_AND_NULL(ecbdata->diff_words); |
f59a59e2 JS |
1899 | } |
1900 | } | |
1901 | ||
ce436973 | 1902 | const char *diff_get_color(int diff_use_color, enum color_diff ix) |
cd112cef | 1903 | { |
daa0c3d9 | 1904 | if (want_color(diff_use_color)) |
50f575fc LT |
1905 | return diff_colors[ix]; |
1906 | return ""; | |
cd112cef JS |
1907 | } |
1908 | ||
f1922234 JK |
1909 | const char *diff_line_prefix(struct diff_options *opt) |
1910 | { | |
1911 | struct strbuf *msgbuf; | |
1912 | if (!opt->output_prefix) | |
1913 | return ""; | |
1914 | ||
1915 | msgbuf = opt->output_prefix(opt, opt->output_prefix_data); | |
1916 | return msgbuf->buf; | |
1917 | } | |
1918 | ||
23707811 JH |
1919 | static unsigned long sane_truncate_line(struct emit_callback *ecb, char *line, unsigned long len) |
1920 | { | |
1921 | const char *cp; | |
1922 | unsigned long allot; | |
1923 | size_t l = len; | |
1924 | ||
23707811 JH |
1925 | cp = line; |
1926 | allot = l; | |
1927 | while (0 < l) { | |
1928 | (void) utf8_width(&cp, &l); | |
1929 | if (!cp) | |
1930 | break; /* truncated in the middle? */ | |
1931 | } | |
1932 | return allot - l; | |
1933 | } | |
1934 | ||
d68fe26f | 1935 | static void find_lno(const char *line, struct emit_callback *ecbdata) |
690ed843 | 1936 | { |
d68fe26f JH |
1937 | const char *p; |
1938 | ecbdata->lno_in_preimage = 0; | |
1939 | ecbdata->lno_in_postimage = 0; | |
1940 | p = strchr(line, '-'); | |
690ed843 | 1941 | if (!p) |
d68fe26f JH |
1942 | return; /* cannot happen */ |
1943 | ecbdata->lno_in_preimage = strtol(p + 1, NULL, 10); | |
1944 | p = strchr(p, '+'); | |
1945 | if (!p) | |
1946 | return; /* cannot happen */ | |
1947 | ecbdata->lno_in_postimage = strtol(p + 1, NULL, 10); | |
690ed843 JH |
1948 | } |
1949 | ||
cd112cef | 1950 | static void fn_out_consume(void *priv, char *line, unsigned long len) |
6973dcae | 1951 | { |
6973dcae | 1952 | struct emit_callback *ecbdata = priv; |
ce436973 | 1953 | const char *reset = diff_get_color(ecbdata->color_diff, DIFF_RESET); |
7be57610 | 1954 | struct diff_options *o = ecbdata->opt; |
6973dcae | 1955 | |
ba16233c SB |
1956 | o->found_changes = 1; |
1957 | ||
3e97c7c6 | 1958 | if (ecbdata->header) { |
a29b0a13 SB |
1959 | emit_diff_symbol(o, DIFF_SYMBOL_HEADER, |
1960 | ecbdata->header->buf, ecbdata->header->len, 0); | |
3e97c7c6 GB |
1961 | strbuf_reset(ecbdata->header); |
1962 | ecbdata->header = NULL; | |
1963 | } | |
34a5e1a2 | 1964 | |
6973dcae | 1965 | if (ecbdata->label_path[0]) { |
3ee8b7bf SB |
1966 | emit_diff_symbol(o, DIFF_SYMBOL_FILEPAIR_MINUS, |
1967 | ecbdata->label_path[0], | |
1968 | strlen(ecbdata->label_path[0]), 0); | |
1969 | emit_diff_symbol(o, DIFF_SYMBOL_FILEPAIR_PLUS, | |
1970 | ecbdata->label_path[1], | |
1971 | strlen(ecbdata->label_path[1]), 0); | |
6973dcae JH |
1972 | ecbdata->label_path[0] = ecbdata->label_path[1] = NULL; |
1973 | } | |
cd112cef | 1974 | |
a624eaa7 JM |
1975 | if (diff_suppress_blank_empty |
1976 | && len == 2 && line[0] == ' ' && line[1] == '\n') { | |
1977 | line[0] = '\n'; | |
1978 | len = 1; | |
1979 | } | |
1980 | ||
b8d9c1a6 | 1981 | if (line[0] == '@') { |
76fd2828 JH |
1982 | if (ecbdata->diff_words) |
1983 | diff_words_flush(ecbdata); | |
23707811 | 1984 | len = sane_truncate_line(ecbdata, line, len); |
d68fe26f | 1985 | find_lno(line, ecbdata); |
89cb73a1 | 1986 | emit_hunk_header(ecbdata, line, len); |
448c3ef1 | 1987 | return; |
cd112cef | 1988 | } |
448c3ef1 | 1989 | |
448c3ef1 | 1990 | if (ecbdata->diff_words) { |
ff958679 SB |
1991 | enum diff_symbol s = |
1992 | ecbdata->diff_words->type == DIFF_WORDS_PORCELAIN ? | |
1993 | DIFF_SYMBOL_WORDS_PORCELAIN : DIFF_SYMBOL_WORDS; | |
448c3ef1 JH |
1994 | if (line[0] == '-') { |
1995 | diff_words_append(line, len, | |
1996 | &ecbdata->diff_words->minus); | |
1997 | return; | |
1998 | } else if (line[0] == '+') { | |
1999 | diff_words_append(line, len, | |
2000 | &ecbdata->diff_words->plus); | |
2001 | return; | |
59556548 | 2002 | } else if (starts_with(line, "\\ ")) { |
c7c2bc0a TR |
2003 | /* |
2004 | * Eat the "no newline at eof" marker as if we | |
2005 | * saw a "+" or "-" line with nothing on it, | |
2006 | * and return without diff_words_flush() to | |
2007 | * defer processing. If this is the end of | |
2008 | * preimage, more "+" lines may come after it. | |
2009 | */ | |
2010 | return; | |
448c3ef1 | 2011 | } |
76fd2828 | 2012 | diff_words_flush(ecbdata); |
ff958679 | 2013 | emit_diff_symbol(o, s, line, len, 0); |
448c3ef1 JH |
2014 | return; |
2015 | } | |
448c3ef1 | 2016 | |
0e383e18 JH |
2017 | switch (line[0]) { |
2018 | case '+': | |
d68fe26f | 2019 | ecbdata->lno_in_postimage++; |
018cff70 | 2020 | emit_add_line(reset, ecbdata, line + 1, len - 1); |
0e383e18 JH |
2021 | break; |
2022 | case '-': | |
2023 | ecbdata->lno_in_preimage++; | |
2024 | emit_del_line(reset, ecbdata, line + 1, len - 1); | |
2025 | break; | |
2026 | case ' ': | |
2027 | ecbdata->lno_in_postimage++; | |
2028 | ecbdata->lno_in_preimage++; | |
2029 | emit_context_line(reset, ecbdata, line + 1, len - 1); | |
2030 | break; | |
2031 | default: | |
2032 | /* incomplete line at the end */ | |
2033 | ecbdata->lno_in_preimage++; | |
f2bb1218 SB |
2034 | emit_diff_symbol(o, DIFF_SYMBOL_CONTEXT_INCOMPLETE, |
2035 | line, len, 0); | |
0e383e18 | 2036 | break; |
448c3ef1 | 2037 | } |
6973dcae JH |
2038 | } |
2039 | ||
c905cbc4 | 2040 | static void pprint_rename(struct strbuf *name, const char *a, const char *b) |
6973dcae | 2041 | { |
63a01c3f BW |
2042 | const char *old_name = a; |
2043 | const char *new_name = b; | |
6973dcae | 2044 | int pfx_length, sfx_length; |
dd281f09 | 2045 | int pfx_adjust_for_slash; |
6973dcae JH |
2046 | int len_a = strlen(a); |
2047 | int len_b = strlen(b); | |
663af342 | 2048 | int a_midlen, b_midlen; |
e5bfbf9b AJ |
2049 | int qlen_a = quote_c_style(a, NULL, NULL, 0); |
2050 | int qlen_b = quote_c_style(b, NULL, NULL, 0); | |
2051 | ||
2052 | if (qlen_a || qlen_b) { | |
c905cbc4 NTND |
2053 | quote_c_style(a, name, NULL, 0); |
2054 | strbuf_addstr(name, " => "); | |
2055 | quote_c_style(b, name, NULL, 0); | |
2056 | return; | |
e5bfbf9b | 2057 | } |
6973dcae JH |
2058 | |
2059 | /* Find common prefix */ | |
2060 | pfx_length = 0; | |
63a01c3f BW |
2061 | while (*old_name && *new_name && *old_name == *new_name) { |
2062 | if (*old_name == '/') | |
2063 | pfx_length = old_name - a + 1; | |
2064 | old_name++; | |
2065 | new_name++; | |
6973dcae JH |
2066 | } |
2067 | ||
2068 | /* Find common suffix */ | |
63a01c3f BW |
2069 | old_name = a + len_a; |
2070 | new_name = b + len_b; | |
6973dcae | 2071 | sfx_length = 0; |
d020e27f | 2072 | /* |
dd281f09 TR |
2073 | * If there is a common prefix, it must end in a slash. In |
2074 | * that case we let this loop run 1 into the prefix to see the | |
2075 | * same slash. | |
2076 | * | |
2077 | * If there is no common prefix, we cannot do this as it would | |
2078 | * underrun the input strings. | |
d020e27f | 2079 | */ |
dd281f09 | 2080 | pfx_adjust_for_slash = (pfx_length ? 1 : 0); |
63a01c3f BW |
2081 | while (a + pfx_length - pfx_adjust_for_slash <= old_name && |
2082 | b + pfx_length - pfx_adjust_for_slash <= new_name && | |
2083 | *old_name == *new_name) { | |
2084 | if (*old_name == '/') | |
2085 | sfx_length = len_a - (old_name - a); | |
2086 | old_name--; | |
2087 | new_name--; | |
6973dcae JH |
2088 | } |
2089 | ||
2090 | /* | |
2091 | * pfx{mid-a => mid-b}sfx | |
2092 | * {pfx-a => pfx-b}sfx | |
2093 | * pfx{sfx-a => sfx-b} | |
2094 | * name-a => name-b | |
2095 | */ | |
663af342 PH |
2096 | a_midlen = len_a - pfx_length - sfx_length; |
2097 | b_midlen = len_b - pfx_length - sfx_length; | |
2098 | if (a_midlen < 0) | |
2099 | a_midlen = 0; | |
2100 | if (b_midlen < 0) | |
2101 | b_midlen = 0; | |
2102 | ||
c905cbc4 | 2103 | strbuf_grow(name, pfx_length + a_midlen + b_midlen + sfx_length + 7); |
6973dcae | 2104 | if (pfx_length + sfx_length) { |
c905cbc4 NTND |
2105 | strbuf_add(name, a, pfx_length); |
2106 | strbuf_addch(name, '{'); | |
6973dcae | 2107 | } |
c905cbc4 NTND |
2108 | strbuf_add(name, a + pfx_length, a_midlen); |
2109 | strbuf_addstr(name, " => "); | |
2110 | strbuf_add(name, b + pfx_length, b_midlen); | |
663af342 | 2111 | if (pfx_length + sfx_length) { |
c905cbc4 NTND |
2112 | strbuf_addch(name, '}'); |
2113 | strbuf_add(name, a + len_a - sfx_length, sfx_length); | |
6973dcae | 2114 | } |
6973dcae JH |
2115 | } |
2116 | ||
2117 | struct diffstat_t { | |
6973dcae JH |
2118 | int nr; |
2119 | int alloc; | |
2120 | struct diffstat_file { | |
f604652e | 2121 | char *from_name; |
6973dcae | 2122 | char *name; |
f604652e | 2123 | char *print_name; |
ddf88fa6 | 2124 | const char *comments; |
6973dcae JH |
2125 | unsigned is_unmerged:1; |
2126 | unsigned is_binary:1; | |
2127 | unsigned is_renamed:1; | |
74faaa16 | 2128 | unsigned is_interesting:1; |
0974c117 | 2129 | uintmax_t added, deleted; |
6973dcae JH |
2130 | } **files; |
2131 | }; | |
2132 | ||
2133 | static struct diffstat_file *diffstat_add(struct diffstat_t *diffstat, | |
2134 | const char *name_a, | |
2135 | const char *name_b) | |
2136 | { | |
2137 | struct diffstat_file *x; | |
1a4927c5 | 2138 | x = xcalloc(1, sizeof(*x)); |
4c960a43 | 2139 | ALLOC_GROW(diffstat->files, diffstat->nr + 1, diffstat->alloc); |
6973dcae JH |
2140 | diffstat->files[diffstat->nr++] = x; |
2141 | if (name_b) { | |
f604652e JH |
2142 | x->from_name = xstrdup(name_a); |
2143 | x->name = xstrdup(name_b); | |
6973dcae JH |
2144 | x->is_renamed = 1; |
2145 | } | |
f604652e JH |
2146 | else { |
2147 | x->from_name = NULL; | |
9befac47 | 2148 | x->name = xstrdup(name_a); |
f604652e | 2149 | } |
6973dcae JH |
2150 | return x; |
2151 | } | |
2152 | ||
2153 | static void diffstat_consume(void *priv, char *line, unsigned long len) | |
2154 | { | |
2155 | struct diffstat_t *diffstat = priv; | |
2156 | struct diffstat_file *x = diffstat->files[diffstat->nr - 1]; | |
2157 | ||
2158 | if (line[0] == '+') | |
2159 | x->added++; | |
2160 | else if (line[0] == '-') | |
2161 | x->deleted++; | |
2162 | } | |
2163 | ||
698ce6f8 | 2164 | const char mime_boundary_leader[] = "------------"; |
6973dcae | 2165 | |
a2540023 JH |
2166 | static int scale_linear(int it, int width, int max_change) |
2167 | { | |
2eeeef24 JH |
2168 | if (!it) |
2169 | return 0; | |
a2540023 | 2170 | /* |
2eeeef24 JH |
2171 | * make sure that at least one '-' or '+' is printed if |
2172 | * there is any change to this path. The easiest way is to | |
2173 | * scale linearly as if the alloted width is one column shorter | |
2174 | * than it is, and then add 1 to the result. | |
a2540023 | 2175 | */ |
2eeeef24 | 2176 | return 1 + (it * (width - 1) / max_change); |
a2540023 JH |
2177 | } |
2178 | ||
0911c475 SB |
2179 | static void show_graph(struct strbuf *out, char ch, int cnt, |
2180 | const char *set, const char *reset) | |
a2540023 JH |
2181 | { |
2182 | if (cnt <= 0) | |
2183 | return; | |
0911c475 SB |
2184 | strbuf_addstr(out, set); |
2185 | strbuf_addchars(out, ch, cnt); | |
2186 | strbuf_addstr(out, reset); | |
a2540023 JH |
2187 | } |
2188 | ||
f604652e JH |
2189 | static void fill_print_name(struct diffstat_file *file) |
2190 | { | |
c905cbc4 | 2191 | struct strbuf pname = STRBUF_INIT; |
f604652e JH |
2192 | |
2193 | if (file->print_name) | |
2194 | return; | |
2195 | ||
c905cbc4 NTND |
2196 | if (file->is_renamed) |
2197 | pprint_rename(&pname, file->from_name, file->name); | |
2198 | else | |
2199 | quote_c_style(file->name, &pname, NULL, 0); | |
2200 | ||
ddf88fa6 NTND |
2201 | if (file->comments) |
2202 | strbuf_addf(&pname, " (%s)", file->comments); | |
2203 | ||
c905cbc4 | 2204 | file->print_name = strbuf_detach(&pname, NULL); |
f604652e JH |
2205 | } |
2206 | ||
0911c475 SB |
2207 | static void print_stat_summary_inserts_deletes(struct diff_options *options, |
2208 | int files, int insertions, int deletions) | |
7f814632 NTND |
2209 | { |
2210 | struct strbuf sb = STRBUF_INIT; | |
7f814632 NTND |
2211 | |
2212 | if (!files) { | |
2213 | assert(insertions == 0 && deletions == 0); | |
0911c475 SB |
2214 | emit_diff_symbol(options, DIFF_SYMBOL_STATS_SUMMARY_NO_FILES, |
2215 | NULL, 0, 0); | |
2216 | return; | |
7f814632 NTND |
2217 | } |
2218 | ||
2219 | strbuf_addf(&sb, | |
218adaaa | 2220 | (files == 1) ? " %d file changed" : " %d files changed", |
7f814632 NTND |
2221 | files); |
2222 | ||
2223 | /* | |
2224 | * For binary diff, the caller may want to print "x files | |
2225 | * changed" with insertions == 0 && deletions == 0. | |
2226 | * | |
2227 | * Not omitting "0 insertions(+), 0 deletions(-)" in this case | |
2228 | * is probably less confusing (i.e skip over "2 files changed | |
2229 | * but nothing about added/removed lines? Is this a bug in Git?"). | |
2230 | */ | |
2231 | if (insertions || deletions == 0) { | |
7f814632 | 2232 | strbuf_addf(&sb, |
218adaaa | 2233 | (insertions == 1) ? ", %d insertion(+)" : ", %d insertions(+)", |
7f814632 NTND |
2234 | insertions); |
2235 | } | |
2236 | ||
2237 | if (deletions || insertions == 0) { | |
7f814632 | 2238 | strbuf_addf(&sb, |
218adaaa | 2239 | (deletions == 1) ? ", %d deletion(-)" : ", %d deletions(-)", |
7f814632 NTND |
2240 | deletions); |
2241 | } | |
2242 | strbuf_addch(&sb, '\n'); | |
0911c475 SB |
2243 | emit_diff_symbol(options, DIFF_SYMBOL_STATS_SUMMARY_INSERTS_DELETES, |
2244 | sb.buf, sb.len, 0); | |
7f814632 | 2245 | strbuf_release(&sb); |
0911c475 SB |
2246 | } |
2247 | ||
2248 | void print_stat_summary(FILE *fp, int files, | |
2249 | int insertions, int deletions) | |
2250 | { | |
2251 | struct diff_options o; | |
2252 | memset(&o, 0, sizeof(o)); | |
2253 | o.file = fp; | |
2254 | ||
2255 | print_stat_summary_inserts_deletes(&o, files, insertions, deletions); | |
7f814632 NTND |
2256 | } |
2257 | ||
4b25d091 | 2258 | static void show_stats(struct diffstat_t *data, struct diff_options *options) |
6973dcae | 2259 | { |
eb3a9dd3 | 2260 | int i, len, add, del, adds = 0, dels = 0; |
0974c117 | 2261 | uintmax_t max_change = 0, max_len = 0; |
dc801e71 ZJS |
2262 | int total_files = data->nr, count; |
2263 | int width, name_width, graph_width, number_width = 0, bin_width = 0; | |
c0aa335c | 2264 | const char *reset, *add_c, *del_c; |
e5f85df8 | 2265 | int extra_shown = 0; |
0911c475 SB |
2266 | const char *line_prefix = diff_line_prefix(options); |
2267 | struct strbuf out = STRBUF_INIT; | |
6973dcae JH |
2268 | |
2269 | if (data->nr == 0) | |
2270 | return; | |
2271 | ||
808e1db2 | 2272 | count = options->stat_count ? options->stat_count : data->nr; |
a2540023 | 2273 | |
8f67f8ae | 2274 | reset = diff_get_color_opt(options, DIFF_RESET); |
8f67f8ae PH |
2275 | add_c = diff_get_color_opt(options, DIFF_FILE_NEW); |
2276 | del_c = diff_get_color_opt(options, DIFF_FILE_OLD); | |
785f7432 | 2277 | |
1b058bc3 ZJS |
2278 | /* |
2279 | * Find the longest filename and max number of changes | |
2280 | */ | |
808e1db2 | 2281 | for (i = 0; (i < count) && (i < data->nr); i++) { |
6973dcae | 2282 | struct diffstat_file *file = data->files[i]; |
0974c117 | 2283 | uintmax_t change = file->added + file->deleted; |
af0ed819 JH |
2284 | |
2285 | if (!file->is_interesting && (change == 0)) { | |
808e1db2 | 2286 | count++; /* not shown == room for one more */ |
358e460e MG |
2287 | continue; |
2288 | } | |
f604652e JH |
2289 | fill_print_name(file); |
2290 | len = strlen(file->print_name); | |
6973dcae JH |
2291 | if (max_len < len) |
2292 | max_len = len; | |
2293 | ||
dc801e71 ZJS |
2294 | if (file->is_unmerged) { |
2295 | /* "Unmerged" is 8 characters */ | |
2296 | bin_width = bin_width < 8 ? 8 : bin_width; | |
6973dcae | 2297 | continue; |
dc801e71 ZJS |
2298 | } |
2299 | if (file->is_binary) { | |
2300 | /* "Bin XXX -> YYY bytes" */ | |
2301 | int w = 14 + decimal_width(file->added) | |
2302 | + decimal_width(file->deleted); | |
2303 | bin_width = bin_width < w ? w : bin_width; | |
2304 | /* Display change counts aligned with "Bin" */ | |
2305 | number_width = 3; | |
2306 | continue; | |
2307 | } | |
2308 | ||
a2540023 JH |
2309 | if (max_change < change) |
2310 | max_change = change; | |
6973dcae | 2311 | } |
a20d3c0d | 2312 | count = i; /* where we can stop scanning in data->files[] */ |
6973dcae | 2313 | |
1b058bc3 ZJS |
2314 | /* |
2315 | * We have width = stat_width or term_columns() columns total. | |
2316 | * We want a maximum of min(max_len, stat_name_width) for the name part. | |
969fe57b | 2317 | * We want a maximum of min(max_change, stat_graph_width) for the +- part. |
1b058bc3 ZJS |
2318 | * We also need 1 for " " and 4 + decimal_width(max_change) |
2319 | * for " | NNNN " and one the empty column at the end, altogether | |
2320 | * 6 + decimal_width(max_change). | |
2321 | * | |
2322 | * If there's not enough space, we will use the smaller of | |
2323 | * stat_name_width (if set) and 5/8*width for the filename, | |
969fe57b ZJS |
2324 | * and the rest for constant elements + graph part, but no more |
2325 | * than stat_graph_width for the graph part. | |
1b058bc3 ZJS |
2326 | * (5/8 gives 50 for filename and 30 for the constant parts + graph |
2327 | * for the standard terminal size). | |
a2540023 | 2328 | * |
1b058bc3 ZJS |
2329 | * In other words: stat_width limits the maximum width, and |
2330 | * stat_name_width fixes the maximum width of the filename, | |
2331 | * and is also used to divide available columns if there | |
2332 | * aren't enough. | |
dc801e71 ZJS |
2333 | * |
2334 | * Binary files are displayed with "Bin XXX -> YYY bytes" | |
2335 | * instead of the change count and graph. This part is treated | |
2336 | * similarly to the graph part, except that it is not | |
41ccfdd9 | 2337 | * "scaled". If total width is too small to accommodate the |
dc801e71 ZJS |
2338 | * guaranteed minimum width of the filename part and the |
2339 | * separators and this message, this message will "overflow" | |
2340 | * making the line longer than the maximum width. | |
a2540023 | 2341 | */ |
1b058bc3 ZJS |
2342 | |
2343 | if (options->stat_width == -1) | |
cd48dadb | 2344 | width = term_columns() - strlen(line_prefix); |
a2540023 | 2345 | else |
1b058bc3 | 2346 | width = options->stat_width ? options->stat_width : 80; |
dc801e71 ZJS |
2347 | number_width = decimal_width(max_change) > number_width ? |
2348 | decimal_width(max_change) : number_width; | |
a2540023 | 2349 | |
df44483a ZJS |
2350 | if (options->stat_graph_width == -1) |
2351 | options->stat_graph_width = diff_stat_graph_width; | |
a2540023 | 2352 | |
1b058bc3 ZJS |
2353 | /* |
2354 | * Guarantee 3/8*16==6 for the graph part | |
2355 | * and 5/8*16==10 for the filename part | |
2356 | */ | |
2357 | if (width < 16 + 6 + number_width) | |
2358 | width = 16 + 6 + number_width; | |
2359 | ||
2360 | /* | |
2361 | * First assign sizes that are wanted, ignoring available width. | |
dc801e71 ZJS |
2362 | * strlen("Bin XXX -> YYY bytes") == bin_width, and the part |
2363 | * starting from "XXX" should fit in graph_width. | |
1b058bc3 | 2364 | */ |
dc801e71 ZJS |
2365 | graph_width = max_change + 4 > bin_width ? max_change : bin_width - 4; |
2366 | if (options->stat_graph_width && | |
2367 | options->stat_graph_width < graph_width) | |
2368 | graph_width = options->stat_graph_width; | |
2369 | ||
1b058bc3 ZJS |
2370 | name_width = (options->stat_name_width > 0 && |
2371 | options->stat_name_width < max_len) ? | |
2372 | options->stat_name_width : max_len; | |
2373 | ||
2374 | /* | |
2375 | * Adjust adjustable widths not to exceed maximum width | |
2376 | */ | |
2377 | if (name_width + number_width + 6 + graph_width > width) { | |
678c5741 | 2378 | if (graph_width > width * 3/8 - number_width - 6) { |
1b058bc3 | 2379 | graph_width = width * 3/8 - number_width - 6; |
678c5741 LP |
2380 | if (graph_width < 6) |
2381 | graph_width = 6; | |
2382 | } | |
2383 | ||
969fe57b ZJS |
2384 | if (options->stat_graph_width && |
2385 | graph_width > options->stat_graph_width) | |
2386 | graph_width = options->stat_graph_width; | |
1b058bc3 ZJS |
2387 | if (name_width > width - number_width - 6 - graph_width) |
2388 | name_width = width - number_width - 6 - graph_width; | |
2389 | else | |
2390 | graph_width = width - number_width - 6 - name_width; | |
2391 | } | |
2392 | ||
2393 | /* | |
2394 | * From here name_width is the width of the name area, | |
2395 | * and graph_width is the width of the graph area. | |
2396 | * max_change is used to scale graph properly. | |
2397 | */ | |
808e1db2 | 2398 | for (i = 0; i < count; i++) { |
d2543b8e | 2399 | const char *prefix = ""; |
af0ed819 JH |
2400 | struct diffstat_file *file = data->files[i]; |
2401 | char *name = file->print_name; | |
2402 | uintmax_t added = file->added; | |
2403 | uintmax_t deleted = file->deleted; | |
a2540023 | 2404 | int name_len; |
6973dcae | 2405 | |
a20d3c0d | 2406 | if (!file->is_interesting && (added + deleted == 0)) |
358e460e | 2407 | continue; |
a20d3c0d | 2408 | |
6973dcae JH |
2409 | /* |
2410 | * "scale" the filename | |
2411 | */ | |
a2540023 JH |
2412 | len = name_width; |
2413 | name_len = strlen(name); | |
2414 | if (name_width < name_len) { | |
6973dcae JH |
2415 | char *slash; |
2416 | prefix = "..."; | |
a2540023 JH |
2417 | len -= 3; |
2418 | name += name_len - len; | |
6973dcae JH |
2419 | slash = strchr(name, '/'); |
2420 | if (slash) | |
2421 | name = slash; | |
2422 | } | |
6973dcae | 2423 | |
af0ed819 | 2424 | if (file->is_binary) { |
0911c475 SB |
2425 | strbuf_addf(&out, " %s%-*s |", prefix, len, name); |
2426 | strbuf_addf(&out, " %*s", number_width, "Bin"); | |
e18872b2 | 2427 | if (!added && !deleted) { |
0911c475 SB |
2428 | strbuf_addch(&out, '\n'); |
2429 | emit_diff_symbol(options, DIFF_SYMBOL_STATS_LINE, | |
2430 | out.buf, out.len, 0); | |
2431 | strbuf_reset(&out); | |
e18872b2 ZJS |
2432 | continue; |
2433 | } | |
0911c475 | 2434 | strbuf_addf(&out, " %s%"PRIuMAX"%s", |
0974c117 | 2435 | del_c, deleted, reset); |
0911c475 SB |
2436 | strbuf_addstr(&out, " -> "); |
2437 | strbuf_addf(&out, "%s%"PRIuMAX"%s", | |
0974c117 | 2438 | add_c, added, reset); |
0911c475 SB |
2439 | strbuf_addstr(&out, " bytes\n"); |
2440 | emit_diff_symbol(options, DIFF_SYMBOL_STATS_LINE, | |
2441 | out.buf, out.len, 0); | |
2442 | strbuf_reset(&out); | |
f604652e | 2443 | continue; |
6973dcae | 2444 | } |
af0ed819 | 2445 | else if (file->is_unmerged) { |
0911c475 SB |
2446 | strbuf_addf(&out, " %s%-*s |", prefix, len, name); |
2447 | strbuf_addstr(&out, " Unmerged\n"); | |
2448 | emit_diff_symbol(options, DIFF_SYMBOL_STATS_LINE, | |
2449 | out.buf, out.len, 0); | |
2450 | strbuf_reset(&out); | |
f604652e | 2451 | continue; |
6973dcae | 2452 | } |
6973dcae | 2453 | |
a2540023 JH |
2454 | /* |
2455 | * scale the add/delete | |
2456 | */ | |
6973dcae JH |
2457 | add = added; |
2458 | del = deleted; | |
6973dcae | 2459 | |
1b058bc3 | 2460 | if (graph_width <= max_change) { |
d3c9cf32 | 2461 | int total = scale_linear(add + del, graph_width, max_change); |
2eeeef24 JH |
2462 | if (total < 2 && add && del) |
2463 | /* width >= 2 due to the sanity check */ | |
2464 | total = 2; | |
2465 | if (add < del) { | |
1b058bc3 | 2466 | add = scale_linear(add, graph_width, max_change); |
2eeeef24 JH |
2467 | del = total - add; |
2468 | } else { | |
1b058bc3 | 2469 | del = scale_linear(del, graph_width, max_change); |
2eeeef24 JH |
2470 | add = total - del; |
2471 | } | |
6973dcae | 2472 | } |
0911c475 SB |
2473 | strbuf_addf(&out, " %s%-*s |", prefix, len, name); |
2474 | strbuf_addf(&out, " %*"PRIuMAX"%s", | |
dc801e71 ZJS |
2475 | number_width, added + deleted, |
2476 | added + deleted ? " " : ""); | |
0911c475 SB |
2477 | show_graph(&out, '+', add, add_c, reset); |
2478 | show_graph(&out, '-', del, del_c, reset); | |
2479 | strbuf_addch(&out, '\n'); | |
2480 | emit_diff_symbol(options, DIFF_SYMBOL_STATS_LINE, | |
2481 | out.buf, out.len, 0); | |
2482 | strbuf_reset(&out); | |
c0c77734 | 2483 | } |
a20d3c0d JH |
2484 | |
2485 | for (i = 0; i < data->nr; i++) { | |
af0ed819 JH |
2486 | struct diffstat_file *file = data->files[i]; |
2487 | uintmax_t added = file->added; | |
2488 | uintmax_t deleted = file->deleted; | |
82dfc2c4 JH |
2489 | |
2490 | if (file->is_unmerged || | |
2491 | (!file->is_interesting && (added + deleted == 0))) { | |
808e1db2 MG |
2492 | total_files--; |
2493 | continue; | |
2494 | } | |
a20d3c0d | 2495 | |
82dfc2c4 | 2496 | if (!file->is_binary) { |
a20d3c0d JH |
2497 | adds += added; |
2498 | dels += deleted; | |
2499 | } | |
2500 | if (i < count) | |
2501 | continue; | |
e5f85df8 | 2502 | if (!extra_shown) |
0911c475 SB |
2503 | emit_diff_symbol(options, |
2504 | DIFF_SYMBOL_STATS_SUMMARY_ABBREV, | |
2505 | NULL, 0, 0); | |
e5f85df8 | 2506 | extra_shown = 1; |
808e1db2 | 2507 | } |
0911c475 SB |
2508 | |
2509 | print_stat_summary_inserts_deletes(options, total_files, adds, dels); | |
5a612017 | 2510 | strbuf_release(&out); |
6973dcae JH |
2511 | } |
2512 | ||
2775d92c | 2513 | static void show_shortstats(struct diffstat_t *data, struct diff_options *options) |
ebd124c6 NP |
2514 | { |
2515 | int i, adds = 0, dels = 0, total_files = data->nr; | |
2516 | ||
2517 | if (data->nr == 0) | |
2518 | return; | |
2519 | ||
2520 | for (i = 0; i < data->nr; i++) { | |
e18872b2 | 2521 | int added = data->files[i]->added; |
0911c475 | 2522 | int deleted = data->files[i]->deleted; |
e18872b2 | 2523 | |
20c8cde4 JH |
2524 | if (data->files[i]->is_unmerged || |
2525 | (!data->files[i]->is_interesting && (added + deleted == 0))) { | |
e18872b2 | 2526 | total_files--; |
de9658b5 | 2527 | } else if (!data->files[i]->is_binary) { /* don't count bytes */ |
e18872b2 ZJS |
2528 | adds += added; |
2529 | dels += deleted; | |
ebd124c6 | 2530 | } |
ebd124c6 | 2531 | } |
0911c475 | 2532 | print_stat_summary_inserts_deletes(options, total_files, adds, dels); |
ebd124c6 NP |
2533 | } |
2534 | ||
4b25d091 | 2535 | static void show_numstat(struct diffstat_t *data, struct diff_options *options) |
74e2abe5 JH |
2536 | { |
2537 | int i; | |
2538 | ||
f604652e JH |
2539 | if (data->nr == 0) |
2540 | return; | |
2541 | ||
74e2abe5 JH |
2542 | for (i = 0; i < data->nr; i++) { |
2543 | struct diffstat_file *file = data->files[i]; | |
2544 | ||
30997bb8 | 2545 | fprintf(options->file, "%s", diff_line_prefix(options)); |
7be57610 | 2546 | |
bfddbc5e | 2547 | if (file->is_binary) |
c0c77734 | 2548 | fprintf(options->file, "-\t-\t"); |
bfddbc5e | 2549 | else |
c0c77734 | 2550 | fprintf(options->file, |
0974c117 JK |
2551 | "%"PRIuMAX"\t%"PRIuMAX"\t", |
2552 | file->added, file->deleted); | |
f604652e JH |
2553 | if (options->line_termination) { |
2554 | fill_print_name(file); | |
2555 | if (!file->is_renamed) | |
c0c77734 | 2556 | write_name_quoted(file->name, options->file, |
f604652e JH |
2557 | options->line_termination); |
2558 | else { | |
c0c77734 DB |
2559 | fputs(file->print_name, options->file); |
2560 | putc(options->line_termination, options->file); | |
f604652e | 2561 | } |
663af342 | 2562 | } else { |
f604652e | 2563 | if (file->is_renamed) { |
c0c77734 DB |
2564 | putc('\0', options->file); |
2565 | write_name_quoted(file->from_name, options->file, '\0'); | |
f604652e | 2566 | } |
c0c77734 | 2567 | write_name_quoted(file->name, options->file, '\0'); |
663af342 | 2568 | } |
74e2abe5 JH |
2569 | } |
2570 | } | |
2571 | ||
c04a7155 JH |
2572 | struct dirstat_file { |
2573 | const char *name; | |
2574 | unsigned long changed; | |
7df7c019 LT |
2575 | }; |
2576 | ||
c04a7155 JH |
2577 | struct dirstat_dir { |
2578 | struct dirstat_file *files; | |
712d2c7d | 2579 | int alloc, nr, permille, cumulative; |
c04a7155 JH |
2580 | }; |
2581 | ||
7be57610 BY |
2582 | static long gather_dirstat(struct diff_options *opt, struct dirstat_dir *dir, |
2583 | unsigned long changed, const char *base, int baselen) | |
7df7c019 | 2584 | { |
585c0e2e | 2585 | unsigned long sum_changes = 0; |
7df7c019 | 2586 | unsigned int sources = 0; |
30997bb8 | 2587 | const char *line_prefix = diff_line_prefix(opt); |
7df7c019 LT |
2588 | |
2589 | while (dir->nr) { | |
c04a7155 | 2590 | struct dirstat_file *f = dir->files; |
7df7c019 | 2591 | int namelen = strlen(f->name); |
585c0e2e | 2592 | unsigned long changes; |
7df7c019 LT |
2593 | char *slash; |
2594 | ||
2595 | if (namelen < baselen) | |
2596 | break; | |
2597 | if (memcmp(f->name, base, baselen)) | |
2598 | break; | |
2599 | slash = strchr(f->name + baselen, '/'); | |
2600 | if (slash) { | |
2601 | int newbaselen = slash + 1 - f->name; | |
585c0e2e | 2602 | changes = gather_dirstat(opt, dir, changed, f->name, newbaselen); |
7df7c019 LT |
2603 | sources++; |
2604 | } else { | |
585c0e2e | 2605 | changes = f->changed; |
7df7c019 LT |
2606 | dir->files++; |
2607 | dir->nr--; | |
2608 | sources += 2; | |
2609 | } | |
585c0e2e | 2610 | sum_changes += changes; |
7df7c019 LT |
2611 | } |
2612 | ||
2613 | /* | |
2614 | * We don't report dirstat's for | |
2615 | * - the top level | |
2616 | * - or cases where everything came from a single directory | |
2617 | * under this directory (sources == 1). | |
2618 | */ | |
2619 | if (baselen && sources != 1) { | |
585c0e2e BW |
2620 | if (sum_changes) { |
2621 | int permille = sum_changes * 1000 / changed; | |
712d2c7d | 2622 | if (permille >= dir->permille) { |
7be57610 | 2623 | fprintf(opt->file, "%s%4d.%01d%% %.*s\n", line_prefix, |
712d2c7d | 2624 | permille / 10, permille % 10, baselen, base); |
7df7c019 LT |
2625 | if (!dir->cumulative) |
2626 | return 0; | |
2627 | } | |
2628 | } | |
2629 | } | |
585c0e2e | 2630 | return sum_changes; |
7df7c019 LT |
2631 | } |
2632 | ||
441bca0b LT |
2633 | static int dirstat_compare(const void *_a, const void *_b) |
2634 | { | |
2635 | const struct dirstat_file *a = _a; | |
2636 | const struct dirstat_file *b = _b; | |
2637 | return strcmp(a->name, b->name); | |
2638 | } | |
2639 | ||
c04a7155 | 2640 | static void show_dirstat(struct diff_options *options) |
7df7c019 LT |
2641 | { |
2642 | int i; | |
2643 | unsigned long changed; | |
c04a7155 JH |
2644 | struct dirstat_dir dir; |
2645 | struct diff_queue_struct *q = &diff_queued_diff; | |
2646 | ||
2647 | dir.files = NULL; | |
2648 | dir.alloc = 0; | |
2649 | dir.nr = 0; | |
712d2c7d | 2650 | dir.permille = options->dirstat_permille; |
0d1e0e78 | 2651 | dir.cumulative = options->flags.dirstat_cumulative; |
7df7c019 | 2652 | |
7df7c019 | 2653 | changed = 0; |
c04a7155 JH |
2654 | for (i = 0; i < q->nr; i++) { |
2655 | struct diff_filepair *p = q->queue[i]; | |
2656 | const char *name; | |
2657 | unsigned long copied, added, damage; | |
0133dab7 | 2658 | int content_changed; |
c04a7155 | 2659 | |
2ca86714 | 2660 | name = p->two->path ? p->two->path : p->one->path; |
c04a7155 | 2661 | |
41c9560e | 2662 | if (p->one->oid_valid && p->two->oid_valid) |
a0d12c44 | 2663 | content_changed = oidcmp(&p->one->oid, &p->two->oid); |
0133dab7 JH |
2664 | else |
2665 | content_changed = 1; | |
2666 | ||
2ff3a803 JH |
2667 | if (!content_changed) { |
2668 | /* | |
2669 | * The SHA1 has not changed, so pre-/post-content is | |
2670 | * identical. We can therefore skip looking at the | |
2671 | * file contents altogether. | |
2672 | */ | |
2673 | damage = 0; | |
2674 | goto found_damage; | |
2675 | } | |
2676 | ||
0d1e0e78 | 2677 | if (options->flags.dirstat_by_file) { |
0133dab7 JH |
2678 | /* |
2679 | * In --dirstat-by-file mode, we don't really need to | |
2680 | * look at the actual file contents at all. | |
2681 | * The fact that the SHA1 changed is enough for us to | |
2682 | * add this file to the list of results | |
2683 | * (with each file contributing equal damage). | |
2684 | */ | |
2ff3a803 | 2685 | damage = 1; |
0133dab7 JH |
2686 | goto found_damage; |
2687 | } | |
c04a7155 JH |
2688 | |
2689 | if (DIFF_FILE_VALID(p->one) && DIFF_FILE_VALID(p->two)) { | |
2690 | diff_populate_filespec(p->one, 0); | |
2691 | diff_populate_filespec(p->two, 0); | |
974e0044 | 2692 | diffcore_count_changes(p->one, p->two, NULL, NULL, |
c04a7155 JH |
2693 | &copied, &added); |
2694 | diff_free_filespec_data(p->one); | |
2695 | diff_free_filespec_data(p->two); | |
2696 | } else if (DIFF_FILE_VALID(p->one)) { | |
8e5dd3d6 | 2697 | diff_populate_filespec(p->one, CHECK_SIZE_ONLY); |
c04a7155 JH |
2698 | copied = added = 0; |
2699 | diff_free_filespec_data(p->one); | |
2700 | } else if (DIFF_FILE_VALID(p->two)) { | |
8e5dd3d6 | 2701 | diff_populate_filespec(p->two, CHECK_SIZE_ONLY); |
c04a7155 JH |
2702 | copied = 0; |
2703 | added = p->two->size; | |
2704 | diff_free_filespec_data(p->two); | |
2705 | } else | |
2b0b551d | 2706 | continue; |
c04a7155 JH |
2707 | |
2708 | /* | |
2709 | * Original minus copied is the removed material, | |
2710 | * added is the new material. They are both damages | |
0133dab7 | 2711 | * made to the preimage. |
2ff3a803 JH |
2712 | * If the resulting damage is zero, we know that |
2713 | * diffcore_count_changes() considers the two entries to | |
2714 | * be identical, but since content_changed is true, we | |
2715 | * know that there must have been _some_ kind of change, | |
2716 | * so we force all entries to have damage > 0. | |
c04a7155 JH |
2717 | */ |
2718 | damage = (p->one->size - copied) + added; | |
2ff3a803 | 2719 | if (!damage) |
fd33777b | 2720 | damage = 1; |
c04a7155 | 2721 | |
0133dab7 | 2722 | found_damage: |
c04a7155 JH |
2723 | ALLOC_GROW(dir.files, dir.nr + 1, dir.alloc); |
2724 | dir.files[dir.nr].name = name; | |
2725 | dir.files[dir.nr].changed = damage; | |
2726 | changed += damage; | |
2727 | dir.nr++; | |
7df7c019 LT |
2728 | } |
2729 | ||
2730 | /* This can happen even with many files, if everything was renames */ | |
2731 | if (!changed) | |
2732 | return; | |
2733 | ||
2734 | /* Show all directories with more than x% of the changes */ | |
9ed0d8d6 | 2735 | QSORT(dir.files, dir.nr, dirstat_compare); |
7be57610 | 2736 | gather_dirstat(options, &dir, changed, "", 0); |
7df7c019 LT |
2737 | } |
2738 | ||
1c57a627 JH |
2739 | static void show_dirstat_by_line(struct diffstat_t *data, struct diff_options *options) |
2740 | { | |
2741 | int i; | |
2742 | unsigned long changed; | |
2743 | struct dirstat_dir dir; | |
2744 | ||
2745 | if (data->nr == 0) | |
2746 | return; | |
2747 | ||
2748 | dir.files = NULL; | |
2749 | dir.alloc = 0; | |
2750 | dir.nr = 0; | |
2751 | dir.permille = options->dirstat_permille; | |
0d1e0e78 | 2752 | dir.cumulative = options->flags.dirstat_cumulative; |
1c57a627 JH |
2753 | |
2754 | changed = 0; | |
2755 | for (i = 0; i < data->nr; i++) { | |
2756 | struct diffstat_file *file = data->files[i]; | |
2757 | unsigned long damage = file->added + file->deleted; | |
2758 | if (file->is_binary) | |
2759 | /* | |
2760 | * binary files counts bytes, not lines. Must find some | |
2761 | * way to normalize binary bytes vs. textual lines. | |
2762 | * The following heuristic assumes that there are 64 | |
2763 | * bytes per "line". | |
2764 | * This is stupid and ugly, but very cheap... | |
2765 | */ | |
42c78a21 | 2766 | damage = DIV_ROUND_UP(damage, 64); |
1c57a627 JH |
2767 | ALLOC_GROW(dir.files, dir.nr + 1, dir.alloc); |
2768 | dir.files[dir.nr].name = file->name; | |
2769 | dir.files[dir.nr].changed = damage; | |
2770 | changed += damage; | |
2771 | dir.nr++; | |
2772 | } | |
2773 | ||
2774 | /* This can happen even with many files, if everything was renames */ | |
2775 | if (!changed) | |
2776 | return; | |
2777 | ||
2778 | /* Show all directories with more than x% of the changes */ | |
9ed0d8d6 | 2779 | QSORT(dir.files, dir.nr, dirstat_compare); |
1c57a627 JH |
2780 | gather_dirstat(options, &dir, changed, "", 0); |
2781 | } | |
2782 | ||
f604652e JH |
2783 | static void free_diffstat_info(struct diffstat_t *diffstat) |
2784 | { | |
2785 | int i; | |
2786 | for (i = 0; i < diffstat->nr; i++) { | |
2787 | struct diffstat_file *f = diffstat->files[i]; | |
c905cbc4 | 2788 | free(f->print_name); |
f604652e JH |
2789 | free(f->name); |
2790 | free(f->from_name); | |
2791 | free(f); | |
2792 | } | |
2793 | free(diffstat->files); | |
2794 | } | |
2795 | ||
88246898 | 2796 | struct checkdiff_t { |
88246898 | 2797 | const char *filename; |
1ba111d1 | 2798 | int lineno; |
a757c646 | 2799 | int conflict_marker_size; |
1ba111d1 | 2800 | struct diff_options *o; |
cf1b7869 | 2801 | unsigned ws_rule; |
62c64895 | 2802 | unsigned status; |
88246898 JS |
2803 | }; |
2804 | ||
a757c646 | 2805 | static int is_conflict_marker(const char *line, int marker_size, unsigned long len) |
04954043 JH |
2806 | { |
2807 | char firstchar; | |
2808 | int cnt; | |
2809 | ||
a757c646 | 2810 | if (len < marker_size + 1) |
04954043 JH |
2811 | return 0; |
2812 | firstchar = line[0]; | |
2813 | switch (firstchar) { | |
a757c646 | 2814 | case '=': case '>': case '<': case '|': |
04954043 JH |
2815 | break; |
2816 | default: | |
2817 | return 0; | |
2818 | } | |
a757c646 | 2819 | for (cnt = 1; cnt < marker_size; cnt++) |
04954043 JH |
2820 | if (line[cnt] != firstchar) |
2821 | return 0; | |
a757c646 JH |
2822 | /* line[1] thru line[marker_size-1] are same as firstchar */ |
2823 | if (len < marker_size + 1 || !isspace(line[marker_size])) | |
04954043 | 2824 | return 0; |
04954043 JH |
2825 | return 1; |
2826 | } | |
2827 | ||
88246898 JS |
2828 | static void checkdiff_consume(void *priv, char *line, unsigned long len) |
2829 | { | |
2830 | struct checkdiff_t *data = priv; | |
a757c646 | 2831 | int marker_size = data->conflict_marker_size; |
f1c96261 JK |
2832 | const char *ws = diff_get_color(data->o->use_color, DIFF_WHITESPACE); |
2833 | const char *reset = diff_get_color(data->o->use_color, DIFF_RESET); | |
2834 | const char *set = diff_get_color(data->o->use_color, DIFF_FILE_NEW); | |
c1795bb0 | 2835 | char *err; |
30997bb8 | 2836 | const char *line_prefix; |
7be57610 BY |
2837 | |
2838 | assert(data->o); | |
30997bb8 | 2839 | line_prefix = diff_line_prefix(data->o); |
88246898 JS |
2840 | |
2841 | if (line[0] == '+') { | |
18374e58 | 2842 | unsigned bad; |
0ef617f4 | 2843 | data->lineno++; |
a757c646 | 2844 | if (is_conflict_marker(line + 1, marker_size, len - 1)) { |
04954043 JH |
2845 | data->status |= 1; |
2846 | fprintf(data->o->file, | |
7be57610 BY |
2847 | "%s%s:%d: leftover conflict marker\n", |
2848 | line_prefix, data->filename, data->lineno); | |
04954043 | 2849 | } |
8f8841e9 | 2850 | bad = ws_check(line + 1, len - 1, data->ws_rule); |
18374e58 | 2851 | if (!bad) |
c1795bb0 | 2852 | return; |
18374e58 JH |
2853 | data->status |= bad; |
2854 | err = whitespace_error_string(bad); | |
7be57610 BY |
2855 | fprintf(data->o->file, "%s%s:%d: %s.\n", |
2856 | line_prefix, data->filename, data->lineno, err); | |
c1795bb0 | 2857 | free(err); |
a3c158d4 | 2858 | emit_line(data->o, set, reset, line, 1); |
8f8841e9 | 2859 | ws_check_emit(line + 1, len - 1, data->ws_rule, |
1ba111d1 | 2860 | data->o->file, set, reset, ws); |
877f23cc | 2861 | } else if (line[0] == ' ') { |
88246898 | 2862 | data->lineno++; |
877f23cc | 2863 | } else if (line[0] == '@') { |
88246898 JS |
2864 | char *plus = strchr(line, '+'); |
2865 | if (plus) | |
0ef617f4 | 2866 | data->lineno = strtol(plus, NULL, 10) - 1; |
88246898 JS |
2867 | else |
2868 | die("invalid diff"); | |
2869 | } | |
2870 | } | |
2871 | ||
0660626c JH |
2872 | static unsigned char *deflate_it(char *data, |
2873 | unsigned long size, | |
2874 | unsigned long *result_size) | |
051308f6 | 2875 | { |
0660626c JH |
2876 | int bound; |
2877 | unsigned char *deflated; | |
ef49a7a0 | 2878 | git_zstream stream; |
0660626c | 2879 | |
55bb5c91 | 2880 | git_deflate_init(&stream, zlib_compression_level); |
225a6f10 | 2881 | bound = git_deflate_bound(&stream, size); |
0660626c JH |
2882 | deflated = xmalloc(bound); |
2883 | stream.next_out = deflated; | |
2884 | stream.avail_out = bound; | |
2885 | ||
2886 | stream.next_in = (unsigned char *)data; | |
2887 | stream.avail_in = size; | |
55bb5c91 | 2888 | while (git_deflate(&stream, Z_FINISH) == Z_OK) |
0660626c | 2889 | ; /* nothing */ |
55bb5c91 | 2890 | git_deflate_end(&stream); |
0660626c JH |
2891 | *result_size = stream.total_out; |
2892 | return deflated; | |
051308f6 JH |
2893 | } |
2894 | ||
4eed0ebd SB |
2895 | static void emit_binary_diff_body(struct diff_options *o, |
2896 | mmfile_t *one, mmfile_t *two) | |
051308f6 | 2897 | { |
0660626c JH |
2898 | void *cp; |
2899 | void *delta; | |
2900 | void *deflated; | |
2901 | void *data; | |
2902 | unsigned long orig_size; | |
2903 | unsigned long delta_size; | |
2904 | unsigned long deflate_size; | |
2905 | unsigned long data_size; | |
051308f6 | 2906 | |
0660626c JH |
2907 | /* We could do deflated delta, or we could do just deflated two, |
2908 | * whichever is smaller. | |
051308f6 | 2909 | */ |
0660626c JH |
2910 | delta = NULL; |
2911 | deflated = deflate_it(two->ptr, two->size, &deflate_size); | |
2912 | if (one->size && two->size) { | |
2913 | delta = diff_delta(one->ptr, one->size, | |
2914 | two->ptr, two->size, | |
2915 | &delta_size, deflate_size); | |
2916 | if (delta) { | |
2917 | void *to_free = delta; | |
2918 | orig_size = delta_size; | |
2919 | delta = deflate_it(delta, delta_size, &delta_size); | |
2920 | free(to_free); | |
051308f6 JH |
2921 | } |
2922 | } | |
051308f6 | 2923 | |
0660626c | 2924 | if (delta && delta_size < deflate_size) { |
4eed0ebd SB |
2925 | char *s = xstrfmt("%lu", orig_size); |
2926 | emit_diff_symbol(o, DIFF_SYMBOL_BINARY_DIFF_HEADER_DELTA, | |
2927 | s, strlen(s), 0); | |
2928 | free(s); | |
0660626c JH |
2929 | free(deflated); |
2930 | data = delta; | |
2931 | data_size = delta_size; | |
4eed0ebd SB |
2932 | } else { |
2933 | char *s = xstrfmt("%lu", two->size); | |
2934 | emit_diff_symbol(o, DIFF_SYMBOL_BINARY_DIFF_HEADER_LITERAL, | |
2935 | s, strlen(s), 0); | |
2936 | free(s); | |
0660626c JH |
2937 | free(delta); |
2938 | data = deflated; | |
2939 | data_size = deflate_size; | |
2940 | } | |
051308f6 | 2941 | |
0660626c JH |
2942 | /* emit data encoded in base85 */ |
2943 | cp = data; | |
2944 | while (data_size) { | |
4eed0ebd | 2945 | int len; |
0660626c | 2946 | int bytes = (52 < data_size) ? 52 : data_size; |
4eed0ebd | 2947 | char line[71]; |
0660626c | 2948 | data_size -= bytes; |
051308f6 JH |
2949 | if (bytes <= 26) |
2950 | line[0] = bytes + 'A' - 1; | |
2951 | else | |
2952 | line[0] = bytes - 26 + 'a' - 1; | |
2953 | encode_85(line + 1, cp, bytes); | |
1d7f171c | 2954 | cp = (char *) cp + bytes; |
4eed0ebd SB |
2955 | |
2956 | len = strlen(line); | |
2957 | line[len++] = '\n'; | |
2958 | line[len] = '\0'; | |
2959 | ||
2960 | emit_diff_symbol(o, DIFF_SYMBOL_BINARY_DIFF_BODY, | |
2961 | line, len, 0); | |
051308f6 | 2962 | } |
4eed0ebd | 2963 | emit_diff_symbol(o, DIFF_SYMBOL_BINARY_DIFF_FOOTER, NULL, 0, 0); |
0660626c | 2964 | free(data); |
051308f6 JH |
2965 | } |
2966 | ||
4eed0ebd SB |
2967 | static void emit_binary_diff(struct diff_options *o, |
2968 | mmfile_t *one, mmfile_t *two) | |
d4c452f0 | 2969 | { |
4eed0ebd SB |
2970 | emit_diff_symbol(o, DIFF_SYMBOL_BINARY_DIFF_HEADER, NULL, 0, 0); |
2971 | emit_binary_diff_body(o, one, two); | |
2972 | emit_binary_diff_body(o, two, one); | |
d4c452f0 JH |
2973 | } |
2974 | ||
29a3eefd JH |
2975 | int diff_filespec_is_binary(struct diff_filespec *one) |
2976 | { | |
122aa6f9 JK |
2977 | if (one->is_binary == -1) { |
2978 | diff_filespec_load_driver(one); | |
2979 | if (one->driver->binary != -1) | |
2980 | one->is_binary = one->driver->binary; | |
2981 | else { | |
2982 | if (!one->data && DIFF_FILE_VALID(one)) | |
6bf3b813 NTND |
2983 | diff_populate_filespec(one, CHECK_BINARY); |
2984 | if (one->is_binary == -1 && one->data) | |
122aa6f9 JK |
2985 | one->is_binary = buffer_is_binary(one->data, |
2986 | one->size); | |
2987 | if (one->is_binary == -1) | |
2988 | one->is_binary = 0; | |
2989 | } | |
2990 | } | |
29a3eefd | 2991 | return one->is_binary; |
6973dcae JH |
2992 | } |
2993 | ||
be58e70d | 2994 | static const struct userdiff_funcname *diff_funcname_pattern(struct diff_filespec *one) |
f258475a | 2995 | { |
122aa6f9 JK |
2996 | diff_filespec_load_driver(one); |
2997 | return one->driver->funcname.pattern ? &one->driver->funcname : NULL; | |
f258475a JH |
2998 | } |
2999 | ||
a5a818ee JH |
3000 | void diff_set_mnemonic_prefix(struct diff_options *options, const char *a, const char *b) |
3001 | { | |
3002 | if (!options->a_prefix) | |
3003 | options->a_prefix = a; | |
3004 | if (!options->b_prefix) | |
3005 | options->b_prefix = b; | |
3006 | } | |
3007 | ||
a788d7d5 | 3008 | struct userdiff_driver *get_textconv(struct diff_filespec *one) |
04427ac8 JK |
3009 | { |
3010 | if (!DIFF_FILE_VALID(one)) | |
3011 | return NULL; | |
d391c0ff | 3012 | |
04427ac8 | 3013 | diff_filespec_load_driver(one); |
3813e690 | 3014 | return userdiff_get_textconv(one->driver); |
04427ac8 JK |
3015 | } |
3016 | ||
6973dcae JH |
3017 | static void builtin_diff(const char *name_a, |
3018 | const char *name_b, | |
3019 | struct diff_filespec *one, | |
3020 | struct diff_filespec *two, | |
3021 | const char *xfrm_msg, | |
296c6bb2 | 3022 | int must_show_header, |
051308f6 | 3023 | struct diff_options *o, |
6973dcae JH |
3024 | int complete_rewrite) |
3025 | { | |
3026 | mmfile_t mf1, mf2; | |
3027 | const char *lbl[2]; | |
3028 | char *a_one, *b_two; | |
d9c552f1 | 3029 | const char *meta = diff_get_color_opt(o, DIFF_METAINFO); |
8f67f8ae | 3030 | const char *reset = diff_get_color_opt(o, DIFF_RESET); |
a5a818ee | 3031 | const char *a_prefix, *b_prefix; |
d9bae1a1 JK |
3032 | struct userdiff_driver *textconv_one = NULL; |
3033 | struct userdiff_driver *textconv_two = NULL; | |
3e97c7c6 | 3034 | struct strbuf header = STRBUF_INIT; |
30997bb8 | 3035 | const char *line_prefix = diff_line_prefix(o); |
a5a818ee | 3036 | |
fd47ae6a | 3037 | diff_set_mnemonic_prefix(o, "a/", "b/"); |
0d1e0e78 | 3038 | if (o->flags.reverse_diff) { |
fd47ae6a JK |
3039 | a_prefix = o->b_prefix; |
3040 | b_prefix = o->a_prefix; | |
3041 | } else { | |
3042 | a_prefix = o->a_prefix; | |
3043 | b_prefix = o->b_prefix; | |
3044 | } | |
3045 | ||
61cfbc05 JK |
3046 | if (o->submodule_format == DIFF_SUBMODULE_LOG && |
3047 | (!one->mode || S_ISGITLINK(one->mode)) && | |
3048 | (!two->mode || S_ISGITLINK(two->mode))) { | |
f3597138 | 3049 | show_submodule_summary(o, one->path ? one->path : two->path, |
602a283a | 3050 | &one->oid, &two->oid, |
f3597138 | 3051 | two->dirty_submodule); |
752c0c24 | 3052 | return; |
fd47ae6a JK |
3053 | } else if (o->submodule_format == DIFF_SUBMODULE_INLINE_DIFF && |
3054 | (!one->mode || S_ISGITLINK(one->mode)) && | |
3055 | (!two->mode || S_ISGITLINK(two->mode))) { | |
f3597138 | 3056 | show_submodule_inline_diff(o, one->path ? one->path : two->path, |
fd47ae6a | 3057 | &one->oid, &two->oid, |
f3597138 | 3058 | two->dirty_submodule); |
fd47ae6a | 3059 | return; |
752c0c24 JS |
3060 | } |
3061 | ||
0d1e0e78 | 3062 | if (o->flags.allow_textconv) { |
3aa1f7ca JK |
3063 | textconv_one = get_textconv(one); |
3064 | textconv_two = get_textconv(two); | |
3065 | } | |
3066 | ||
71b989e7 LT |
3067 | /* Never use a non-valid filename anywhere if at all possible */ |
3068 | name_a = DIFF_FILE_VALID(one) ? name_a : name_b; | |
3069 | name_b = DIFF_FILE_VALID(two) ? name_b : name_a; | |
3070 | ||
a5a818ee JH |
3071 | a_one = quote_two(a_prefix, name_a + (*name_a == '/')); |
3072 | b_two = quote_two(b_prefix, name_b + (*name_b == '/')); | |
6973dcae JH |
3073 | lbl[0] = DIFF_FILE_VALID(one) ? a_one : "/dev/null"; |
3074 | lbl[1] = DIFF_FILE_VALID(two) ? b_two : "/dev/null"; | |
d9c552f1 | 3075 | strbuf_addf(&header, "%s%sdiff --git %s %s%s\n", line_prefix, meta, a_one, b_two, reset); |
6973dcae JH |
3076 | if (lbl[0][0] == '/') { |
3077 | /* /dev/null */ | |
d9c552f1 | 3078 | strbuf_addf(&header, "%s%snew file mode %06o%s\n", line_prefix, meta, two->mode, reset); |
37466447 BW |
3079 | if (xfrm_msg) |
3080 | strbuf_addstr(&header, xfrm_msg); | |
296c6bb2 | 3081 | must_show_header = 1; |
6973dcae JH |
3082 | } |
3083 | else if (lbl[1][0] == '/') { | |
d9c552f1 | 3084 | strbuf_addf(&header, "%s%sdeleted file mode %06o%s\n", line_prefix, meta, one->mode, reset); |
37466447 BW |
3085 | if (xfrm_msg) |
3086 | strbuf_addstr(&header, xfrm_msg); | |
296c6bb2 | 3087 | must_show_header = 1; |
6973dcae JH |
3088 | } |
3089 | else { | |
3090 | if (one->mode != two->mode) { | |
d9c552f1 JK |
3091 | strbuf_addf(&header, "%s%sold mode %06o%s\n", line_prefix, meta, one->mode, reset); |
3092 | strbuf_addf(&header, "%s%snew mode %06o%s\n", line_prefix, meta, two->mode, reset); | |
296c6bb2 | 3093 | must_show_header = 1; |
cd112cef | 3094 | } |
37466447 BW |
3095 | if (xfrm_msg) |
3096 | strbuf_addstr(&header, xfrm_msg); | |
3e97c7c6 | 3097 | |
6973dcae JH |
3098 | /* |
3099 | * we do not run diff between different kind | |
3100 | * of objects. | |
3101 | */ | |
3102 | if ((one->mode ^ two->mode) & S_IFMT) | |
3103 | goto free_ab_and_return; | |
0c01857d | 3104 | if (complete_rewrite && |
3aa1f7ca JK |
3105 | (textconv_one || !diff_filespec_is_binary(one)) && |
3106 | (textconv_two || !diff_filespec_is_binary(two))) { | |
a29b0a13 SB |
3107 | emit_diff_symbol(o, DIFF_SYMBOL_HEADER, |
3108 | header.buf, header.len, 0); | |
3e97c7c6 | 3109 | strbuf_reset(&header); |
3aa1f7ca JK |
3110 | emit_rewrite_diff(name_a, name_b, one, two, |
3111 | textconv_one, textconv_two, o); | |
34a5e1a2 | 3112 | o->found_changes = 1; |
6973dcae JH |
3113 | goto free_ab_and_return; |
3114 | } | |
3115 | } | |
3116 | ||
467ddc14 | 3117 | if (o->irreversible_delete && lbl[1][0] == '/') { |
a29b0a13 SB |
3118 | emit_diff_symbol(o, DIFF_SYMBOL_HEADER, header.buf, |
3119 | header.len, 0); | |
467ddc14 JH |
3120 | strbuf_reset(&header); |
3121 | goto free_ab_and_return; | |
0d1e0e78 | 3122 | } else if (!o->flags.text && |
b3373982 JK |
3123 | ( (!textconv_one && diff_filespec_is_binary(one)) || |
3124 | (!textconv_two && diff_filespec_is_binary(two)) )) { | |
4acaaa7a | 3125 | struct strbuf sb = STRBUF_INIT; |
1aaf69e6 NTND |
3126 | if (!one->data && !two->data && |
3127 | S_ISREG(one->mode) && S_ISREG(two->mode) && | |
0d1e0e78 | 3128 | !o->flags.binary) { |
a0d12c44 | 3129 | if (!oidcmp(&one->oid, &two->oid)) { |
1aaf69e6 | 3130 | if (must_show_header) |
a29b0a13 SB |
3131 | emit_diff_symbol(o, DIFF_SYMBOL_HEADER, |
3132 | header.buf, header.len, | |
3133 | 0); | |
1aaf69e6 NTND |
3134 | goto free_ab_and_return; |
3135 | } | |
a29b0a13 SB |
3136 | emit_diff_symbol(o, DIFF_SYMBOL_HEADER, |
3137 | header.buf, header.len, 0); | |
4acaaa7a SB |
3138 | strbuf_addf(&sb, "%sBinary files %s and %s differ\n", |
3139 | diff_line_prefix(o), lbl[0], lbl[1]); | |
3140 | emit_diff_symbol(o, DIFF_SYMBOL_BINARY_FILES, | |
3141 | sb.buf, sb.len, 0); | |
3142 | strbuf_release(&sb); | |
1aaf69e6 NTND |
3143 | goto free_ab_and_return; |
3144 | } | |
b3373982 JK |
3145 | if (fill_mmfile(&mf1, one) < 0 || fill_mmfile(&mf2, two) < 0) |
3146 | die("unable to read files to diff"); | |
0660626c JH |
3147 | /* Quite common confusing case */ |
3148 | if (mf1.size == mf2.size && | |
296c6bb2 CC |
3149 | !memcmp(mf1.ptr, mf2.ptr, mf1.size)) { |
3150 | if (must_show_header) | |
a29b0a13 SB |
3151 | emit_diff_symbol(o, DIFF_SYMBOL_HEADER, |
3152 | header.buf, header.len, 0); | |
0660626c | 3153 | goto free_ab_and_return; |
296c6bb2 | 3154 | } |
a29b0a13 | 3155 | emit_diff_symbol(o, DIFF_SYMBOL_HEADER, header.buf, header.len, 0); |
3e97c7c6 | 3156 | strbuf_reset(&header); |
0d1e0e78 | 3157 | if (o->flags.binary) |
4eed0ebd | 3158 | emit_binary_diff(o, &mf1, &mf2); |
4acaaa7a SB |
3159 | else { |
3160 | strbuf_addf(&sb, "%sBinary files %s and %s differ\n", | |
3161 | diff_line_prefix(o), lbl[0], lbl[1]); | |
3162 | emit_diff_symbol(o, DIFF_SYMBOL_BINARY_FILES, | |
3163 | sb.buf, sb.len, 0); | |
3164 | strbuf_release(&sb); | |
3165 | } | |
34a5e1a2 | 3166 | o->found_changes = 1; |
467ddc14 | 3167 | } else { |
6973dcae JH |
3168 | /* Crazy xdl interfaces.. */ |
3169 | const char *diffopts = getenv("GIT_DIFF_OPTS"); | |
ae021d87 | 3170 | const char *v; |
6973dcae JH |
3171 | xpparam_t xpp; |
3172 | xdemitconf_t xecfg; | |
6973dcae | 3173 | struct emit_callback ecbdata; |
be58e70d | 3174 | const struct userdiff_funcname *pe; |
f258475a | 3175 | |
b3f01ff2 | 3176 | if (must_show_header) { |
a29b0a13 SB |
3177 | emit_diff_symbol(o, DIFF_SYMBOL_HEADER, |
3178 | header.buf, header.len, 0); | |
3e97c7c6 GB |
3179 | strbuf_reset(&header); |
3180 | } | |
3181 | ||
840383b2 JK |
3182 | mf1.size = fill_textconv(textconv_one, one, &mf1.ptr); |
3183 | mf2.size = fill_textconv(textconv_two, two, &mf2.ptr); | |
04427ac8 | 3184 | |
45e7ca0f BC |
3185 | pe = diff_funcname_pattern(one); |
3186 | if (!pe) | |
3187 | pe = diff_funcname_pattern(two); | |
6973dcae | 3188 | |
9ccd0a88 | 3189 | memset(&xpp, 0, sizeof(xpp)); |
30b25010 | 3190 | memset(&xecfg, 0, sizeof(xecfg)); |
cd112cef | 3191 | memset(&ecbdata, 0, sizeof(ecbdata)); |
6973dcae | 3192 | ecbdata.label_path = lbl; |
daa0c3d9 | 3193 | ecbdata.color_diff = want_color(o->use_color); |
c189c4f2 | 3194 | ecbdata.ws_rule = whitespace_rule(name_b); |
690ed843 | 3195 | if (ecbdata.ws_rule & WS_BLANK_AT_EOF) |
d68fe26f | 3196 | check_blank_at_eof(&mf1, &mf2, &ecbdata); |
a3c158d4 | 3197 | ecbdata.opt = o; |
3e97c7c6 | 3198 | ecbdata.header = header.len ? &header : NULL; |
582aa00b | 3199 | xpp.flags = o->xdl_opts; |
2477ab2e JT |
3200 | xpp.anchors = o->anchors; |
3201 | xpp.anchors_nr = o->anchors_nr; | |
ee1e5412 | 3202 | xecfg.ctxlen = o->context; |
6d0e674a | 3203 | xecfg.interhunkctxlen = o->interhunkcontext; |
6973dcae | 3204 | xecfg.flags = XDL_EMIT_FUNCNAMES; |
0d1e0e78 | 3205 | if (o->flags.funccontext) |
14937c2c | 3206 | xecfg.flags |= XDL_EMIT_FUNCCONTEXT; |
45e7ca0f | 3207 | if (pe) |
a013585b | 3208 | xdiff_set_find_func(&xecfg, pe->pattern, pe->cflags); |
6973dcae JH |
3209 | if (!diffopts) |
3210 | ; | |
ae021d87 JK |
3211 | else if (skip_prefix(diffopts, "--unified=", &v)) |
3212 | xecfg.ctxlen = strtoul(v, NULL, 10); | |
3213 | else if (skip_prefix(diffopts, "-u", &v)) | |
3214 | xecfg.ctxlen = strtoul(v, NULL, 10); | |
77d1a520 TR |
3215 | if (o->word_diff) |
3216 | init_diff_words_data(&ecbdata, o, one, two); | |
3efb9880 JK |
3217 | if (xdi_diff_outf(&mf1, &mf2, fn_out_consume, &ecbdata, |
3218 | &xpp, &xecfg)) | |
3219 | die("unable to generate diff for %s", one->path); | |
882749a0 | 3220 | if (o->word_diff) |
f59a59e2 | 3221 | free_diff_words_data(&ecbdata); |
04427ac8 JK |
3222 | if (textconv_one) |
3223 | free(mf1.ptr); | |
3224 | if (textconv_two) | |
3225 | free(mf2.ptr); | |
8cfe5f1c | 3226 | xdiff_clear_find_func(&xecfg); |
6973dcae JH |
3227 | } |
3228 | ||
3229 | free_ab_and_return: | |
3e97c7c6 | 3230 | strbuf_release(&header); |
fc3abdf5 JH |
3231 | diff_free_filespec_data(one); |
3232 | diff_free_filespec_data(two); | |
6973dcae JH |
3233 | free(a_one); |
3234 | free(b_two); | |
3235 | return; | |
3236 | } | |
3237 | ||
ddf88fa6 NTND |
3238 | static char *get_compact_summary(const struct diff_filepair *p, int is_renamed) |
3239 | { | |
3240 | if (!is_renamed) { | |
3241 | if (p->status == DIFF_STATUS_ADDED) { | |
3242 | if (S_ISLNK(p->two->mode)) | |
3243 | return "new +l"; | |
3244 | else if ((p->two->mode & 0777) == 0755) | |
3245 | return "new +x"; | |
3246 | else | |
3247 | return "new"; | |
3248 | } else if (p->status == DIFF_STATUS_DELETED) | |
3249 | return "gone"; | |
3250 | } | |
3251 | if (S_ISLNK(p->one->mode) && !S_ISLNK(p->two->mode)) | |
3252 | return "mode -l"; | |
3253 | else if (!S_ISLNK(p->one->mode) && S_ISLNK(p->two->mode)) | |
3254 | return "mode +l"; | |
3255 | else if ((p->one->mode & 0777) == 0644 && | |
3256 | (p->two->mode & 0777) == 0755) | |
3257 | return "mode +x"; | |
3258 | else if ((p->one->mode & 0777) == 0755 && | |
3259 | (p->two->mode & 0777) == 0644) | |
3260 | return "mode -x"; | |
3261 | return NULL; | |
3262 | } | |
3263 | ||
6973dcae JH |
3264 | static void builtin_diffstat(const char *name_a, const char *name_b, |
3265 | struct diff_filespec *one, | |
3266 | struct diff_filespec *two, | |
710158e3 | 3267 | struct diffstat_t *diffstat, |
0d21efa5 | 3268 | struct diff_options *o, |
74faaa16 | 3269 | struct diff_filepair *p) |
6973dcae JH |
3270 | { |
3271 | mmfile_t mf1, mf2; | |
3272 | struct diffstat_file *data; | |
352ca4e1 | 3273 | int same_contents; |
74faaa16 LT |
3274 | int complete_rewrite = 0; |
3275 | ||
3276 | if (!DIFF_PAIR_UNMERGED(p)) { | |
3277 | if (p->status == DIFF_STATUS_MODIFIED && p->score) | |
3278 | complete_rewrite = 1; | |
3279 | } | |
6973dcae JH |
3280 | |
3281 | data = diffstat_add(diffstat, name_a, name_b); | |
99bfd407 | 3282 | data->is_interesting = p->status != DIFF_STATUS_UNKNOWN; |
ddf88fa6 NTND |
3283 | if (o->flags.stat_with_summary) |
3284 | data->comments = get_compact_summary(p, data->is_renamed); | |
6973dcae JH |
3285 | |
3286 | if (!one || !two) { | |
3287 | data->is_unmerged = 1; | |
3288 | return; | |
3289 | } | |
ded0abc7 | 3290 | |
a0d12c44 | 3291 | same_contents = !oidcmp(&one->oid, &two->oid); |
352ca4e1 | 3292 | |
ded0abc7 | 3293 | if (diff_filespec_is_binary(one) || diff_filespec_is_binary(two)) { |
ded0abc7 | 3294 | data->is_binary = 1; |
352ca4e1 | 3295 | if (same_contents) { |
e18872b2 ZJS |
3296 | data->added = 0; |
3297 | data->deleted = 0; | |
3298 | } else { | |
3299 | data->added = diff_filespec_size(two); | |
3300 | data->deleted = diff_filespec_size(one); | |
3301 | } | |
ded0abc7 JK |
3302 | } |
3303 | ||
3304 | else if (complete_rewrite) { | |
710158e3 JH |
3305 | diff_populate_filespec(one, 0); |
3306 | diff_populate_filespec(two, 0); | |
3307 | data->deleted = count_lines(one->data, one->size); | |
3308 | data->added = count_lines(two->data, two->size); | |
710158e3 | 3309 | } |
6973dcae | 3310 | |
352ca4e1 | 3311 | else if (!same_contents) { |
6973dcae JH |
3312 | /* Crazy xdl interfaces.. */ |
3313 | xpparam_t xpp; | |
3314 | xdemitconf_t xecfg; | |
6973dcae | 3315 | |
ded0abc7 JK |
3316 | if (fill_mmfile(&mf1, one) < 0 || fill_mmfile(&mf2, two) < 0) |
3317 | die("unable to read files to diff"); | |
3318 | ||
9ccd0a88 | 3319 | memset(&xpp, 0, sizeof(xpp)); |
30b25010 | 3320 | memset(&xecfg, 0, sizeof(xecfg)); |
582aa00b | 3321 | xpp.flags = o->xdl_opts; |
2477ab2e JT |
3322 | xpp.anchors = o->anchors; |
3323 | xpp.anchors_nr = o->anchors_nr; | |
f01cae91 JH |
3324 | xecfg.ctxlen = o->context; |
3325 | xecfg.interhunkctxlen = o->interhunkcontext; | |
3efb9880 JK |
3326 | if (xdi_diff_outf(&mf1, &mf2, diffstat_consume, diffstat, |
3327 | &xpp, &xecfg)) | |
3328 | die("unable to generate diffstat for %s", one->path); | |
6973dcae | 3329 | } |
fc3abdf5 | 3330 | |
fc3abdf5 JH |
3331 | diff_free_filespec_data(one); |
3332 | diff_free_filespec_data(two); | |
6973dcae JH |
3333 | } |
3334 | ||
88246898 | 3335 | static void builtin_checkdiff(const char *name_a, const char *name_b, |
cd676a51 | 3336 | const char *attr_path, |
5ff10dd6 JH |
3337 | struct diff_filespec *one, |
3338 | struct diff_filespec *two, | |
3339 | struct diff_options *o) | |
88246898 JS |
3340 | { |
3341 | mmfile_t mf1, mf2; | |
3342 | struct checkdiff_t data; | |
3343 | ||
3344 | if (!two) | |
3345 | return; | |
3346 | ||
3347 | memset(&data, 0, sizeof(data)); | |
88246898 JS |
3348 | data.filename = name_b ? name_b : name_a; |
3349 | data.lineno = 0; | |
1ba111d1 | 3350 | data.o = o; |
cd676a51 | 3351 | data.ws_rule = whitespace_rule(attr_path); |
a757c646 | 3352 | data.conflict_marker_size = ll_merge_marker_size(attr_path); |
88246898 JS |
3353 | |
3354 | if (fill_mmfile(&mf1, one) < 0 || fill_mmfile(&mf2, two) < 0) | |
3355 | die("unable to read files to diff"); | |
3356 | ||
5ff10dd6 JH |
3357 | /* |
3358 | * All the other codepaths check both sides, but not checking | |
3359 | * the "old" side here is deliberate. We are checking the newly | |
3360 | * introduced changes, and as long as the "new" side is text, we | |
3361 | * can and should check what it introduces. | |
3362 | */ | |
29a3eefd | 3363 | if (diff_filespec_is_binary(two)) |
fc3abdf5 | 3364 | goto free_and_return; |
88246898 JS |
3365 | else { |
3366 | /* Crazy xdl interfaces.. */ | |
3367 | xpparam_t xpp; | |
3368 | xdemitconf_t xecfg; | |
88246898 | 3369 | |
9ccd0a88 | 3370 | memset(&xpp, 0, sizeof(xpp)); |
30b25010 | 3371 | memset(&xecfg, 0, sizeof(xecfg)); |
c35539eb | 3372 | xecfg.ctxlen = 1; /* at least one context line */ |
582aa00b | 3373 | xpp.flags = 0; |
3efb9880 JK |
3374 | if (xdi_diff_outf(&mf1, &mf2, checkdiff_consume, &data, |
3375 | &xpp, &xecfg)) | |
3376 | die("unable to generate checkdiff for %s", one->path); | |
877f23cc | 3377 | |
467babf8 | 3378 | if (data.ws_rule & WS_BLANK_AT_EOF) { |
d68fe26f JH |
3379 | struct emit_callback ecbdata; |
3380 | int blank_at_eof; | |
3381 | ||
3382 | ecbdata.ws_rule = data.ws_rule; | |
3383 | check_blank_at_eof(&mf1, &mf2, &ecbdata); | |
8837d335 | 3384 | blank_at_eof = ecbdata.blank_at_eof_in_postimage; |
d68fe26f | 3385 | |
467babf8 JH |
3386 | if (blank_at_eof) { |
3387 | static char *err; | |
3388 | if (!err) | |
3389 | err = whitespace_error_string(WS_BLANK_AT_EOF); | |
3390 | fprintf(o->file, "%s:%d: %s.\n", | |
3391 | data.filename, blank_at_eof, err); | |
3392 | data.status = 1; /* report errors */ | |
3393 | } | |
877f23cc | 3394 | } |
88246898 | 3395 | } |
fc3abdf5 JH |
3396 | free_and_return: |
3397 | diff_free_filespec_data(one); | |
3398 | diff_free_filespec_data(two); | |
62c64895 | 3399 | if (data.status) |
0d1e0e78 | 3400 | o->flags.check_failed = 1; |
88246898 JS |
3401 | } |
3402 | ||
6973dcae JH |
3403 | struct diff_filespec *alloc_filespec(const char *path) |
3404 | { | |
96ffc06f | 3405 | struct diff_filespec *spec; |
6973dcae | 3406 | |
96ffc06f | 3407 | FLEXPTR_ALLOC_STR(spec, path, path); |
9fb88419 | 3408 | spec->count = 1; |
122aa6f9 | 3409 | spec->is_binary = -1; |
6973dcae JH |
3410 | return spec; |
3411 | } | |
3412 | ||
9fb88419 LT |
3413 | void free_filespec(struct diff_filespec *spec) |
3414 | { | |
3415 | if (!--spec->count) { | |
3416 | diff_free_filespec_data(spec); | |
3417 | free(spec); | |
3418 | } | |
3419 | } | |
3420 | ||
f9704c2d BW |
3421 | void fill_filespec(struct diff_filespec *spec, const struct object_id *oid, |
3422 | int oid_valid, unsigned short mode) | |
6973dcae JH |
3423 | { |
3424 | if (mode) { | |
3425 | spec->mode = canon_mode(mode); | |
f9704c2d BW |
3426 | oidcpy(&spec->oid, oid); |
3427 | spec->oid_valid = oid_valid; | |
6973dcae JH |
3428 | } |
3429 | } | |
3430 | ||
3431 | /* | |
5adf317b | 3432 | * Given a name and sha1 pair, if the index tells us the file in |
6973dcae JH |
3433 | * the work tree has that object contents, return true, so that |
3434 | * prepare_temp_file() does not have to inflate and extract. | |
3435 | */ | |
fb4a1c0d | 3436 | static int reuse_worktree_file(const char *name, const struct object_id *oid, int want_file) |
6973dcae | 3437 | { |
9c5e6c80 | 3438 | const struct cache_entry *ce; |
6973dcae JH |
3439 | struct stat st; |
3440 | int pos, len; | |
3441 | ||
150115ad JH |
3442 | /* |
3443 | * We do not read the cache ourselves here, because the | |
6973dcae JH |
3444 | * benchmark with my previous version that always reads cache |
3445 | * shows that it makes things worse for diff-tree comparing | |
3446 | * two linux-2.6 kernel trees in an already checked out work | |
3447 | * tree. This is because most diff-tree comparisons deal with | |
3448 | * only a small number of files, while reading the cache is | |
3449 | * expensive for a large project, and its cost outweighs the | |
3450 | * savings we get by not inflating the object to a temporary | |
3451 | * file. Practically, this code only helps when we are used | |
3452 | * by diff-cache --cached, which does read the cache before | |
3453 | * calling us. | |
3454 | */ | |
3455 | if (!active_cache) | |
3456 | return 0; | |
3457 | ||
1510fea7 SP |
3458 | /* We want to avoid the working directory if our caller |
3459 | * doesn't need the data in a normal file, this system | |
3460 | * is rather slow with its stat/open/mmap/close syscalls, | |
3461 | * and the object is contained in a pack file. The pack | |
3462 | * is probably already open and will be faster to obtain | |
3463 | * the data through than the working directory. Loose | |
3464 | * objects however would tend to be slower as they need | |
3465 | * to be individually opened and inflated. | |
3466 | */ | |
fb4a1c0d | 3467 | if (!FAST_WORKING_DIRECTORY && !want_file && has_sha1_pack(oid->hash)) |
1510fea7 SP |
3468 | return 0; |
3469 | ||
06dec439 JK |
3470 | /* |
3471 | * Similarly, if we'd have to convert the file contents anyway, that | |
3472 | * makes the optimization not worthwhile. | |
3473 | */ | |
82b474e0 | 3474 | if (!want_file && would_convert_to_git(&the_index, name)) |
06dec439 JK |
3475 | return 0; |
3476 | ||
6973dcae JH |
3477 | len = strlen(name); |
3478 | pos = cache_name_pos(name, len); | |
3479 | if (pos < 0) | |
3480 | return 0; | |
3481 | ce = active_cache[pos]; | |
eadb5831 JH |
3482 | |
3483 | /* | |
3484 | * This is not the sha1 we are looking for, or | |
3485 | * unreusable because it is not a regular file. | |
3486 | */ | |
fb4a1c0d | 3487 | if (oidcmp(oid, &ce->oid) || !S_ISREG(ce->ce_mode)) |
6973dcae | 3488 | return 0; |
eadb5831 | 3489 | |
150115ad JH |
3490 | /* |
3491 | * If ce is marked as "assume unchanged", there is no | |
3492 | * guarantee that work tree matches what we are looking for. | |
3493 | */ | |
b4d1690d | 3494 | if ((ce->ce_flags & CE_VALID) || ce_skip_worktree(ce)) |
150115ad JH |
3495 | return 0; |
3496 | ||
eadb5831 JH |
3497 | /* |
3498 | * If ce matches the file in the work tree, we can reuse it. | |
6973dcae | 3499 | */ |
eadb5831 JH |
3500 | if (ce_uptodate(ce) || |
3501 | (!lstat(name, &st) && !ce_match_stat(ce, &st, 0))) | |
3502 | return 1; | |
3503 | ||
3504 | return 0; | |
6973dcae JH |
3505 | } |
3506 | ||
04786756 LT |
3507 | static int diff_populate_gitlink(struct diff_filespec *s, int size_only) |
3508 | { | |
b1ddfb91 JK |
3509 | struct strbuf buf = STRBUF_INIT; |
3510 | char *dirty = ""; | |
8e08b419 JH |
3511 | |
3512 | /* Are we looking at the work tree? */ | |
85adbf2f | 3513 | if (s->dirty_submodule) |
8e08b419 JH |
3514 | dirty = "-dirty"; |
3515 | ||
a0d12c44 | 3516 | strbuf_addf(&buf, "Subproject commit %s%s\n", |
3517 | oid_to_hex(&s->oid), dirty); | |
b1ddfb91 | 3518 | s->size = buf.len; |
04786756 LT |
3519 | if (size_only) { |
3520 | s->data = NULL; | |
b1ddfb91 JK |
3521 | strbuf_release(&buf); |
3522 | } else { | |
3523 | s->data = strbuf_detach(&buf, NULL); | |
3524 | s->should_free = 1; | |
04786756 LT |
3525 | } |
3526 | return 0; | |
3527 | } | |
3528 | ||
6973dcae JH |
3529 | /* |
3530 | * While doing rename detection and pickaxe operation, we may need to | |
3531 | * grab the data for the blob (or file) for our own in-core comparison. | |
3532 | * diff_filespec has data and size fields for this purpose. | |
3533 | */ | |
8e5dd3d6 | 3534 | int diff_populate_filespec(struct diff_filespec *s, unsigned int flags) |
6973dcae | 3535 | { |
8e5dd3d6 | 3536 | int size_only = flags & CHECK_SIZE_ONLY; |
6973dcae | 3537 | int err = 0; |
8462ff43 | 3538 | int conv_flags = global_conv_flags_eol; |
5430bb28 JH |
3539 | /* |
3540 | * demote FAIL to WARN to allow inspecting the situation | |
3541 | * instead of refusing. | |
3542 | */ | |
8462ff43 TB |
3543 | if (conv_flags & CONV_EOL_RNDTRP_DIE) |
3544 | conv_flags = CONV_EOL_RNDTRP_WARN; | |
5430bb28 | 3545 | |
6973dcae JH |
3546 | if (!DIFF_FILE_VALID(s)) |
3547 | die("internal error: asking to populate invalid file."); | |
3548 | if (S_ISDIR(s->mode)) | |
3549 | return -1; | |
3550 | ||
6973dcae | 3551 | if (s->data) |
fc3abdf5 | 3552 | return 0; |
04786756 | 3553 | |
6e0b8ed6 JH |
3554 | if (size_only && 0 < s->size) |
3555 | return 0; | |
3556 | ||
302b9282 | 3557 | if (S_ISGITLINK(s->mode)) |
04786756 LT |
3558 | return diff_populate_gitlink(s, size_only); |
3559 | ||
41c9560e | 3560 | if (!s->oid_valid || |
fb4a1c0d | 3561 | reuse_worktree_file(s->path, &s->oid, 0)) { |
f285a2d7 | 3562 | struct strbuf buf = STRBUF_INIT; |
6973dcae JH |
3563 | struct stat st; |
3564 | int fd; | |
6c510bee | 3565 | |
6973dcae | 3566 | if (lstat(s->path, &st) < 0) { |
10e0ca84 AO |
3567 | err_empty: |
3568 | err = -1; | |
3569 | empty: | |
3570 | s->data = (char *)""; | |
3571 | s->size = 0; | |
3572 | return err; | |
6973dcae | 3573 | } |
dc49cd76 | 3574 | s->size = xsize_t(st.st_size); |
6973dcae JH |
3575 | if (!s->size) |
3576 | goto empty; | |
6973dcae | 3577 | if (S_ISLNK(st.st_mode)) { |
cf219d8c LT |
3578 | struct strbuf sb = STRBUF_INIT; |
3579 | ||
3580 | if (strbuf_readlink(&sb, s->path, s->size)) | |
6973dcae | 3581 | goto err_empty; |
0956a6db RS |
3582 | s->size = sb.len; |
3583 | s->data = strbuf_detach(&sb, NULL); | |
cf219d8c | 3584 | s->should_free = 1; |
6973dcae JH |
3585 | return 0; |
3586 | } | |
12426e11 JH |
3587 | |
3588 | /* | |
3589 | * Even if the caller would be happy with getting | |
3590 | * only the size, we cannot return early at this | |
3591 | * point if the path requires us to run the content | |
3592 | * conversion. | |
3593 | */ | |
82b474e0 | 3594 | if (size_only && !would_convert_to_git(&the_index, s->path)) |
cf219d8c | 3595 | return 0; |
12426e11 JH |
3596 | |
3597 | /* | |
3598 | * Note: this check uses xsize_t(st.st_size) that may | |
3599 | * not be the true size of the blob after it goes | |
3600 | * through convert_to_git(). This may not strictly be | |
3601 | * correct, but the whole point of big_file_threshold | |
3602 | * and is_binary check being that we want to avoid | |
3603 | * opening the file and inspecting the contents, this | |
3604 | * is probably fine. | |
3605 | */ | |
6bf3b813 NTND |
3606 | if ((flags & CHECK_BINARY) && |
3607 | s->size > big_file_threshold && s->is_binary == -1) { | |
3608 | s->is_binary = 1; | |
3609 | return 0; | |
3610 | } | |
6973dcae JH |
3611 | fd = open(s->path, O_RDONLY); |
3612 | if (fd < 0) | |
3613 | goto err_empty; | |
c4712e45 | 3614 | s->data = xmmap(NULL, s->size, PROT_READ, MAP_PRIVATE, fd, 0); |
6973dcae | 3615 | close(fd); |
6973dcae | 3616 | s->should_munmap = 1; |
6c510bee LT |
3617 | |
3618 | /* | |
3619 | * Convert from working tree format to canonical git format | |
3620 | */ | |
8462ff43 | 3621 | if (convert_to_git(&the_index, s->path, s->data, s->size, &buf, conv_flags)) { |
c32f749f | 3622 | size_t size = 0; |
6c510bee LT |
3623 | munmap(s->data, s->size); |
3624 | s->should_munmap = 0; | |
c32f749f RS |
3625 | s->data = strbuf_detach(&buf, &size); |
3626 | s->size = size; | |
6c510bee LT |
3627 | s->should_free = 1; |
3628 | } | |
6973dcae JH |
3629 | } |
3630 | else { | |
21666f1a | 3631 | enum object_type type; |
6bf3b813 | 3632 | if (size_only || (flags & CHECK_BINARY)) { |
0df8e965 SB |
3633 | type = oid_object_info(the_repository, &s->oid, |
3634 | &s->size); | |
c50c4316 | 3635 | if (type < 0) |
a0d12c44 | 3636 | die("unable to read %s", |
3637 | oid_to_hex(&s->oid)); | |
6bf3b813 NTND |
3638 | if (size_only) |
3639 | return 0; | |
3640 | if (s->size > big_file_threshold && s->is_binary == -1) { | |
3641 | s->is_binary = 1; | |
3642 | return 0; | |
3643 | } | |
6973dcae | 3644 | } |
b4f5aca4 | 3645 | s->data = read_object_file(&s->oid, &type, &s->size); |
6bf3b813 | 3646 | if (!s->data) |
a0d12c44 | 3647 | die("unable to read %s", oid_to_hex(&s->oid)); |
6bf3b813 | 3648 | s->should_free = 1; |
6973dcae JH |
3649 | } |
3650 | return 0; | |
3651 | } | |
3652 | ||
8ae92e63 | 3653 | void diff_free_filespec_blob(struct diff_filespec *s) |
6973dcae JH |
3654 | { |
3655 | if (s->should_free) | |
3656 | free(s->data); | |
3657 | else if (s->should_munmap) | |
3658 | munmap(s->data, s->size); | |
fc3abdf5 JH |
3659 | |
3660 | if (s->should_free || s->should_munmap) { | |
3661 | s->should_free = s->should_munmap = 0; | |
3662 | s->data = NULL; | |
3663 | } | |
eede7b7d JK |
3664 | } |
3665 | ||
3666 | void diff_free_filespec_data(struct diff_filespec *s) | |
3667 | { | |
8ae92e63 | 3668 | diff_free_filespec_blob(s); |
6a83d902 | 3669 | FREE_AND_NULL(s->cnt_data); |
6973dcae JH |
3670 | } |
3671 | ||
4e218f54 | 3672 | static void prep_temp_blob(const char *path, struct diff_tempfile *temp, |
6973dcae JH |
3673 | void *blob, |
3674 | unsigned long size, | |
09bdff29 | 3675 | const struct object_id *oid, |
6973dcae JH |
3676 | int mode) |
3677 | { | |
4e218f54 | 3678 | struct strbuf buf = STRBUF_INIT; |
c2a46a7c | 3679 | struct strbuf tempfile = STRBUF_INIT; |
003b33a8 DA |
3680 | char *path_dup = xstrdup(path); |
3681 | const char *base = basename(path_dup); | |
6973dcae | 3682 | |
003b33a8 | 3683 | /* Generate "XXXXXX_basename.ext" */ |
c2a46a7c BW |
3684 | strbuf_addstr(&tempfile, "XXXXXX_"); |
3685 | strbuf_addstr(&tempfile, base); | |
003b33a8 | 3686 | |
c2a46a7c | 3687 | temp->tempfile = mks_tempfile_ts(tempfile.buf, strlen(base) + 1); |
076aa2cb | 3688 | if (!temp->tempfile) |
d824cbba | 3689 | die_errno("unable to create temp-file"); |
4e218f54 JS |
3690 | if (convert_to_working_tree(path, |
3691 | (const char *)blob, (size_t)size, &buf)) { | |
3692 | blob = buf.buf; | |
3693 | size = buf.len; | |
3694 | } | |
c50424a6 | 3695 | if (write_in_full(temp->tempfile->fd, blob, size) < 0 || |
076aa2cb | 3696 | close_tempfile_gently(temp->tempfile)) |
0721c314 | 3697 | die_errno("unable to write temp-file"); |
076aa2cb | 3698 | temp->name = get_tempfile_path(temp->tempfile); |
09bdff29 | 3699 | oid_to_hex_r(temp->hex, oid); |
5096d490 | 3700 | xsnprintf(temp->mode, sizeof(temp->mode), "%06o", mode); |
4e218f54 | 3701 | strbuf_release(&buf); |
c2a46a7c | 3702 | strbuf_release(&tempfile); |
003b33a8 | 3703 | free(path_dup); |
6973dcae JH |
3704 | } |
3705 | ||
479b0ae8 JK |
3706 | static struct diff_tempfile *prepare_temp_file(const char *name, |
3707 | struct diff_filespec *one) | |
6973dcae | 3708 | { |
479b0ae8 JK |
3709 | struct diff_tempfile *temp = claim_diff_tempfile(); |
3710 | ||
6973dcae JH |
3711 | if (!DIFF_FILE_VALID(one)) { |
3712 | not_a_valid_file: | |
3713 | /* A '-' entry produces this for file-2, and | |
3714 | * a '+' entry produces this for file-1. | |
3715 | */ | |
3716 | temp->name = "/dev/null"; | |
5096d490 JK |
3717 | xsnprintf(temp->hex, sizeof(temp->hex), "."); |
3718 | xsnprintf(temp->mode, sizeof(temp->mode), "."); | |
479b0ae8 JK |
3719 | return temp; |
3720 | } | |
3721 | ||
aba47272 | 3722 | if (!S_ISGITLINK(one->mode) && |
41c9560e | 3723 | (!one->oid_valid || |
fb4a1c0d | 3724 | reuse_worktree_file(name, &one->oid, 1))) { |
6973dcae JH |
3725 | struct stat st; |
3726 | if (lstat(name, &st) < 0) { | |
3727 | if (errno == ENOENT) | |
3728 | goto not_a_valid_file; | |
d824cbba | 3729 | die_errno("stat(%s)", name); |
6973dcae JH |
3730 | } |
3731 | if (S_ISLNK(st.st_mode)) { | |
3cd7388d JK |
3732 | struct strbuf sb = STRBUF_INIT; |
3733 | if (strbuf_readlink(&sb, name, st.st_size) < 0) | |
0721c314 | 3734 | die_errno("readlink(%s)", name); |
3cd7388d | 3735 | prep_temp_blob(name, temp, sb.buf, sb.len, |
41c9560e | 3736 | (one->oid_valid ? |
09bdff29 | 3737 | &one->oid : &null_oid), |
41c9560e | 3738 | (one->oid_valid ? |
6973dcae | 3739 | one->mode : S_IFLNK)); |
3cd7388d | 3740 | strbuf_release(&sb); |
6973dcae JH |
3741 | } |
3742 | else { | |
3743 | /* we can borrow from the file in the work tree */ | |
3744 | temp->name = name; | |
41c9560e | 3745 | if (!one->oid_valid) |
74014152 | 3746 | oid_to_hex_r(temp->hex, &null_oid); |
6973dcae | 3747 | else |
2490574d | 3748 | oid_to_hex_r(temp->hex, &one->oid); |
6973dcae JH |
3749 | /* Even though we may sometimes borrow the |
3750 | * contents from the work tree, we always want | |
3751 | * one->mode. mode is trustworthy even when | |
74014152 | 3752 | * !(one->oid_valid), as long as |
6973dcae JH |
3753 | * DIFF_FILE_VALID(one). |
3754 | */ | |
5096d490 | 3755 | xsnprintf(temp->mode, sizeof(temp->mode), "%06o", one->mode); |
6973dcae | 3756 | } |
479b0ae8 | 3757 | return temp; |
6973dcae JH |
3758 | } |
3759 | else { | |
3760 | if (diff_populate_filespec(one, 0)) | |
3761 | die("cannot read data blob for %s", one->path); | |
4e218f54 | 3762 | prep_temp_blob(name, temp, one->data, one->size, |
09bdff29 | 3763 | &one->oid, one->mode); |
6973dcae | 3764 | } |
479b0ae8 | 3765 | return temp; |
6973dcae JH |
3766 | } |
3767 | ||
f3efe787 JK |
3768 | static void add_external_diff_name(struct argv_array *argv, |
3769 | const char *name, | |
3770 | struct diff_filespec *df) | |
3771 | { | |
3772 | struct diff_tempfile *temp = prepare_temp_file(name, df); | |
3773 | argv_array_push(argv, temp->name); | |
3774 | argv_array_push(argv, temp->hex); | |
3775 | argv_array_push(argv, temp->mode); | |
3776 | } | |
3777 | ||
6973dcae JH |
3778 | /* An external diff command takes: |
3779 | * | |
3780 | * diff-cmd name infile1 infile1-sha1 infile1-mode \ | |
3781 | * infile2 infile2-sha1 infile2-mode [ rename-to ] | |
3782 | * | |
3783 | */ | |
3784 | static void run_external_diff(const char *pgm, | |
3785 | const char *name, | |
3786 | const char *other, | |
3787 | struct diff_filespec *one, | |
3788 | struct diff_filespec *two, | |
3789 | const char *xfrm_msg, | |
ee7fb0b1 ZK |
3790 | int complete_rewrite, |
3791 | struct diff_options *o) | |
6973dcae | 3792 | { |
82fbf269 | 3793 | struct argv_array argv = ARGV_ARRAY_INIT; |
ae049c95 | 3794 | struct argv_array env = ARGV_ARRAY_INIT; |
ee7fb0b1 | 3795 | struct diff_queue_struct *q = &diff_queued_diff; |
6973dcae | 3796 | |
0d4217d9 JK |
3797 | argv_array_push(&argv, pgm); |
3798 | argv_array_push(&argv, name); | |
6973dcae | 3799 | |
6973dcae | 3800 | if (one && two) { |
f3efe787 JK |
3801 | add_external_diff_name(&argv, name, one); |
3802 | if (!other) | |
3803 | add_external_diff_name(&argv, name, two); | |
3804 | else { | |
3805 | add_external_diff_name(&argv, other, two); | |
82fbf269 JK |
3806 | argv_array_push(&argv, other); |
3807 | argv_array_push(&argv, xfrm_msg); | |
6973dcae | 3808 | } |
6973dcae | 3809 | } |
ee7fb0b1 | 3810 | |
ae049c95 JK |
3811 | argv_array_pushf(&env, "GIT_DIFF_PATH_COUNTER=%d", ++o->diff_path_counter); |
3812 | argv_array_pushf(&env, "GIT_DIFF_PATH_TOTAL=%d", q->nr); | |
ee7fb0b1 | 3813 | |
89294d14 JK |
3814 | if (run_command_v_opt_cd_env(argv.argv, RUN_USING_SHELL, NULL, env.argv)) |
3815 | die(_("external diff died, stopping at %s"), name); | |
ee7fb0b1 | 3816 | |
6973dcae | 3817 | remove_tempfile(); |
82fbf269 | 3818 | argv_array_clear(&argv); |
ae049c95 | 3819 | argv_array_clear(&env); |
6973dcae JH |
3820 | } |
3821 | ||
b67b9612 JH |
3822 | static int similarity_index(struct diff_filepair *p) |
3823 | { | |
3824 | return p->score * 100 / MAX_SCORE; | |
3825 | } | |
3826 | ||
4f03666a JK |
3827 | static const char *diff_abbrev_oid(const struct object_id *oid, int abbrev) |
3828 | { | |
3829 | if (startup_info->have_repository) | |
aab9583f | 3830 | return find_unique_abbrev(oid, abbrev); |
4f03666a JK |
3831 | else { |
3832 | char *hex = oid_to_hex(oid); | |
0d9c527d JH |
3833 | if (abbrev < 0) |
3834 | abbrev = FALLBACK_DEFAULT_ABBREV; | |
3835 | if (abbrev > GIT_SHA1_HEXSZ) | |
4f03666a | 3836 | die("BUG: oid abbreviation out of range: %d", abbrev); |
43d1948b JB |
3837 | if (abbrev) |
3838 | hex[abbrev] = '\0'; | |
4f03666a JK |
3839 | return hex; |
3840 | } | |
3841 | } | |
3842 | ||
b67b9612 JH |
3843 | static void fill_metainfo(struct strbuf *msg, |
3844 | const char *name, | |
3845 | const char *other, | |
3846 | struct diff_filespec *one, | |
3847 | struct diff_filespec *two, | |
3848 | struct diff_options *o, | |
37466447 | 3849 | struct diff_filepair *p, |
5977744d | 3850 | int *must_show_header, |
37466447 | 3851 | int use_color) |
b67b9612 | 3852 | { |
37466447 BW |
3853 | const char *set = diff_get_color(use_color, DIFF_METAINFO); |
3854 | const char *reset = diff_get_color(use_color, DIFF_RESET); | |
30997bb8 | 3855 | const char *line_prefix = diff_line_prefix(o); |
7be57610 | 3856 | |
296c6bb2 | 3857 | *must_show_header = 1; |
b67b9612 JH |
3858 | strbuf_init(msg, PATH_MAX * 2 + 300); |
3859 | switch (p->status) { | |
3860 | case DIFF_STATUS_COPIED: | |
98ad90fb JH |
3861 | strbuf_addf(msg, "%s%ssimilarity index %d%%", |
3862 | line_prefix, set, similarity_index(p)); | |
3863 | strbuf_addf(msg, "%s\n%s%scopy from ", | |
3864 | reset, line_prefix, set); | |
b67b9612 | 3865 | quote_c_style(name, msg, NULL, 0); |
98ad90fb | 3866 | strbuf_addf(msg, "%s\n%s%scopy to ", reset, line_prefix, set); |
b67b9612 | 3867 | quote_c_style(other, msg, NULL, 0); |
37466447 | 3868 | strbuf_addf(msg, "%s\n", reset); |
b67b9612 JH |
3869 | break; |
3870 | case DIFF_STATUS_RENAMED: | |
98ad90fb JH |
3871 | strbuf_addf(msg, "%s%ssimilarity index %d%%", |
3872 | line_prefix, set, similarity_index(p)); | |
3873 | strbuf_addf(msg, "%s\n%s%srename from ", | |
3874 | reset, line_prefix, set); | |
b67b9612 | 3875 | quote_c_style(name, msg, NULL, 0); |
98ad90fb JH |
3876 | strbuf_addf(msg, "%s\n%s%srename to ", |
3877 | reset, line_prefix, set); | |
b67b9612 | 3878 | quote_c_style(other, msg, NULL, 0); |
37466447 | 3879 | strbuf_addf(msg, "%s\n", reset); |
b67b9612 JH |
3880 | break; |
3881 | case DIFF_STATUS_MODIFIED: | |
3882 | if (p->score) { | |
98ad90fb JH |
3883 | strbuf_addf(msg, "%s%sdissimilarity index %d%%%s\n", |
3884 | line_prefix, | |
37466447 | 3885 | set, similarity_index(p), reset); |
b67b9612 JH |
3886 | break; |
3887 | } | |
3888 | /* fallthru */ | |
3889 | default: | |
296c6bb2 | 3890 | *must_show_header = 0; |
b67b9612 | 3891 | } |
a0d12c44 | 3892 | if (one && two && oidcmp(&one->oid, &two->oid)) { |
0d1e0e78 | 3893 | int abbrev = o->flags.full_index ? 40 : DEFAULT_ABBREV; |
b67b9612 | 3894 | |
0d1e0e78 | 3895 | if (o->flags.binary) { |
b67b9612 JH |
3896 | mmfile_t mf; |
3897 | if ((!fill_mmfile(&mf, one) && diff_filespec_is_binary(one)) || | |
3898 | (!fill_mmfile(&mf, two) && diff_filespec_is_binary(two))) | |
3899 | abbrev = 40; | |
3900 | } | |
4f03666a JK |
3901 | strbuf_addf(msg, "%s%sindex %s..%s", line_prefix, set, |
3902 | diff_abbrev_oid(&one->oid, abbrev), | |
3903 | diff_abbrev_oid(&two->oid, abbrev)); | |
b67b9612 JH |
3904 | if (one->mode == two->mode) |
3905 | strbuf_addf(msg, " %06o", one->mode); | |
37466447 | 3906 | strbuf_addf(msg, "%s\n", reset); |
b67b9612 | 3907 | } |
b67b9612 JH |
3908 | } |
3909 | ||
6973dcae JH |
3910 | static void run_diff_cmd(const char *pgm, |
3911 | const char *name, | |
3912 | const char *other, | |
cd676a51 | 3913 | const char *attr_path, |
6973dcae JH |
3914 | struct diff_filespec *one, |
3915 | struct diff_filespec *two, | |
b67b9612 | 3916 | struct strbuf *msg, |
051308f6 | 3917 | struct diff_options *o, |
b67b9612 | 3918 | struct diff_filepair *p) |
6973dcae | 3919 | { |
b67b9612 JH |
3920 | const char *xfrm_msg = NULL; |
3921 | int complete_rewrite = (p->status == DIFF_STATUS_MODIFIED) && p->score; | |
296c6bb2 | 3922 | int must_show_header = 0; |
b67b9612 | 3923 | |
bd8c1a9b | 3924 | |
0d1e0e78 | 3925 | if (o->flags.allow_external) { |
be58e70d JK |
3926 | struct userdiff_driver *drv = userdiff_find_by_path(attr_path); |
3927 | if (drv && drv->external) | |
3928 | pgm = drv->external; | |
f1af60bd JH |
3929 | } |
3930 | ||
37466447 BW |
3931 | if (msg) { |
3932 | /* | |
3933 | * don't use colors when the header is intended for an | |
3934 | * external diff driver | |
3935 | */ | |
3936 | fill_metainfo(msg, name, other, one, two, o, p, | |
5977744d | 3937 | &must_show_header, |
daa0c3d9 | 3938 | want_color(o->use_color) && !pgm); |
37466447 BW |
3939 | xfrm_msg = msg->len ? msg->buf : NULL; |
3940 | } | |
3941 | ||
6973dcae JH |
3942 | if (pgm) { |
3943 | run_external_diff(pgm, name, other, one, two, xfrm_msg, | |
ee7fb0b1 | 3944 | complete_rewrite, o); |
6973dcae JH |
3945 | return; |
3946 | } | |
3947 | if (one && two) | |
3948 | builtin_diff(name, other ? other : name, | |
296c6bb2 CC |
3949 | one, two, xfrm_msg, must_show_header, |
3950 | o, complete_rewrite); | |
6973dcae | 3951 | else |
c0c77734 | 3952 | fprintf(o->file, "* Unmerged path %s\n", name); |
6973dcae JH |
3953 | } |
3954 | ||
94e327e9 | 3955 | static void diff_fill_oid_info(struct diff_filespec *one) |
6973dcae JH |
3956 | { |
3957 | if (DIFF_FILE_VALID(one)) { | |
41c9560e | 3958 | if (!one->oid_valid) { |
6973dcae | 3959 | struct stat st; |
4682d852 | 3960 | if (one->is_stdin) { |
a0d12c44 | 3961 | oidclr(&one->oid); |
5332b2af JS |
3962 | return; |
3963 | } | |
6973dcae | 3964 | if (lstat(one->path, &st) < 0) |
0721c314 | 3965 | die_errno("stat '%s'", one->path); |
98e019b0 | 3966 | if (index_path(&one->oid, one->path, &st, 0)) |
d7530708 | 3967 | die("cannot hash %s", one->path); |
6973dcae JH |
3968 | } |
3969 | } | |
3970 | else | |
a0d12c44 | 3971 | oidclr(&one->oid); |
6973dcae JH |
3972 | } |
3973 | ||
cd676a51 JH |
3974 | static void strip_prefix(int prefix_length, const char **namep, const char **otherp) |
3975 | { | |
3976 | /* Strip the prefix but do not molest /dev/null and absolute paths */ | |
d8faea9d | 3977 | if (*namep && **namep != '/') { |
cd676a51 | 3978 | *namep += prefix_length; |
d8faea9d JN |
3979 | if (**namep == '/') |
3980 | ++*namep; | |
3981 | } | |
3982 | if (*otherp && **otherp != '/') { | |
cd676a51 | 3983 | *otherp += prefix_length; |
d8faea9d JN |
3984 | if (**otherp == '/') |
3985 | ++*otherp; | |
3986 | } | |
cd676a51 JH |
3987 | } |
3988 | ||
6973dcae JH |
3989 | static void run_diff(struct diff_filepair *p, struct diff_options *o) |
3990 | { | |
3991 | const char *pgm = external_diff(); | |
663af342 | 3992 | struct strbuf msg; |
663af342 PH |
3993 | struct diff_filespec *one = p->one; |
3994 | struct diff_filespec *two = p->two; | |
6973dcae JH |
3995 | const char *name; |
3996 | const char *other; | |
cd676a51 | 3997 | const char *attr_path; |
663af342 | 3998 | |
f2d2a5de SB |
3999 | name = one->path; |
4000 | other = (strcmp(name, two->path) ? two->path : NULL); | |
cd676a51 JH |
4001 | attr_path = name; |
4002 | if (o->prefix_length) | |
4003 | strip_prefix(o->prefix_length, &name, &other); | |
6973dcae | 4004 | |
0d1e0e78 | 4005 | if (!o->flags.allow_external) |
bd8c1a9b JH |
4006 | pgm = NULL; |
4007 | ||
6973dcae | 4008 | if (DIFF_PAIR_UNMERGED(p)) { |
cd676a51 | 4009 | run_diff_cmd(pgm, name, NULL, attr_path, |
b67b9612 | 4010 | NULL, NULL, NULL, o, p); |
6973dcae JH |
4011 | return; |
4012 | } | |
4013 | ||
94e327e9 BW |
4014 | diff_fill_oid_info(one); |
4015 | diff_fill_oid_info(two); | |
6973dcae | 4016 | |
6973dcae JH |
4017 | if (!pgm && |
4018 | DIFF_FILE_VALID(one) && DIFF_FILE_VALID(two) && | |
4019 | (S_IFMT & one->mode) != (S_IFMT & two->mode)) { | |
b67b9612 JH |
4020 | /* |
4021 | * a filepair that changes between file and symlink | |
6973dcae JH |
4022 | * needs to be split into deletion and creation. |
4023 | */ | |
4024 | struct diff_filespec *null = alloc_filespec(two->path); | |
cd676a51 | 4025 | run_diff_cmd(NULL, name, other, attr_path, |
b67b9612 | 4026 | one, null, &msg, o, p); |
6973dcae | 4027 | free(null); |
b67b9612 JH |
4028 | strbuf_release(&msg); |
4029 | ||
6973dcae | 4030 | null = alloc_filespec(one->path); |
cd676a51 | 4031 | run_diff_cmd(NULL, name, other, attr_path, |
b67b9612 | 4032 | null, two, &msg, o, p); |
6973dcae JH |
4033 | free(null); |
4034 | } | |
4035 | else | |
cd676a51 | 4036 | run_diff_cmd(pgm, name, other, attr_path, |
b67b9612 | 4037 | one, two, &msg, o, p); |
6973dcae | 4038 | |
663af342 | 4039 | strbuf_release(&msg); |
6973dcae JH |
4040 | } |
4041 | ||
4042 | static void run_diffstat(struct diff_filepair *p, struct diff_options *o, | |
4043 | struct diffstat_t *diffstat) | |
4044 | { | |
4045 | const char *name; | |
4046 | const char *other; | |
4047 | ||
4048 | if (DIFF_PAIR_UNMERGED(p)) { | |
4049 | /* unmerged */ | |
74faaa16 | 4050 | builtin_diffstat(p->one->path, NULL, NULL, NULL, diffstat, o, p); |
6973dcae JH |
4051 | return; |
4052 | } | |
4053 | ||
4054 | name = p->one->path; | |
4055 | other = (strcmp(name, p->two->path) ? p->two->path : NULL); | |
4056 | ||
cd676a51 JH |
4057 | if (o->prefix_length) |
4058 | strip_prefix(o->prefix_length, &name, &other); | |
4059 | ||
94e327e9 BW |
4060 | diff_fill_oid_info(p->one); |
4061 | diff_fill_oid_info(p->two); | |
6973dcae | 4062 | |
74faaa16 | 4063 | builtin_diffstat(name, other, p->one, p->two, diffstat, o, p); |
6973dcae JH |
4064 | } |
4065 | ||
88246898 JS |
4066 | static void run_checkdiff(struct diff_filepair *p, struct diff_options *o) |
4067 | { | |
4068 | const char *name; | |
4069 | const char *other; | |
cd676a51 | 4070 | const char *attr_path; |
88246898 JS |
4071 | |
4072 | if (DIFF_PAIR_UNMERGED(p)) { | |
4073 | /* unmerged */ | |
4074 | return; | |
4075 | } | |
4076 | ||
4077 | name = p->one->path; | |
4078 | other = (strcmp(name, p->two->path) ? p->two->path : NULL); | |
cd676a51 JH |
4079 | attr_path = other ? other : name; |
4080 | ||
4081 | if (o->prefix_length) | |
4082 | strip_prefix(o->prefix_length, &name, &other); | |
88246898 | 4083 | |
94e327e9 BW |
4084 | diff_fill_oid_info(p->one); |
4085 | diff_fill_oid_info(p->two); | |
88246898 | 4086 | |
cd676a51 | 4087 | builtin_checkdiff(name, other, attr_path, p->one, p->two, o); |
88246898 JS |
4088 | } |
4089 | ||
6973dcae JH |
4090 | void diff_setup(struct diff_options *options) |
4091 | { | |
be4f2b40 | 4092 | memcpy(options, &default_diff_options, sizeof(*options)); |
c0c77734 DB |
4093 | |
4094 | options->file = stdout; | |
4095 | ||
43d1948b | 4096 | options->abbrev = DEFAULT_ABBREV; |
6973dcae JH |
4097 | options->line_termination = '\n'; |
4098 | options->break_opt = -1; | |
4099 | options->rename_limit = -1; | |
712d2c7d | 4100 | options->dirstat_permille = diff_dirstat_permille_default; |
6468a4e5 | 4101 | options->context = diff_context_default; |
c4888677 | 4102 | options->interhunkcontext = diff_interhunk_context_default; |
a17505f2 | 4103 | options->ws_error_highlight = ws_error_highlight_default; |
0d1e0e78 | 4104 | options->flags.rename_empty = 1; |
15af58c1 | 4105 | options->objfind = NULL; |
6973dcae | 4106 | |
72441af7 | 4107 | /* pathchange left =NULL by default */ |
6973dcae JH |
4108 | options->change = diff_change; |
4109 | options->add_remove = diff_addremove; | |
f1c96261 | 4110 | options->use_color = diff_use_color_default; |
b68ea12e | 4111 | options->detect_rename = diff_detect_rename_default; |
07ab4dec | 4112 | options->xdl_opts |= diff_algorithm; |
433860f3 MH |
4113 | if (diff_indent_heuristic) |
4114 | DIFF_XDL_SET(options, INDENT_HEURISTIC); | |
eab9a40b | 4115 | |
6d8940b5 SB |
4116 | options->orderfile = diff_order_file_cfg; |
4117 | ||
f89504dd EC |
4118 | if (diff_no_prefix) { |
4119 | options->a_prefix = options->b_prefix = ""; | |
4120 | } else if (!diff_mnemonic_prefix) { | |
a5a818ee JH |
4121 | options->a_prefix = "a/"; |
4122 | options->b_prefix = "b/"; | |
4123 | } | |
2e2d5ac1 SB |
4124 | |
4125 | options->color_moved = diff_color_moved_default; | |
6973dcae JH |
4126 | } |
4127 | ||
28452655 | 4128 | void diff_setup_done(struct diff_options *options) |
6973dcae | 4129 | { |
4d8c51aa SB |
4130 | unsigned check_mask = DIFF_FORMAT_NAME | |
4131 | DIFF_FORMAT_NAME_STATUS | | |
4132 | DIFF_FORMAT_CHECKDIFF | | |
4133 | DIFF_FORMAT_NO_OUTPUT; | |
d7de00f7 | 4134 | |
6c374008 JH |
4135 | if (options->set_default) |
4136 | options->set_default(options); | |
4137 | ||
4d8c51aa | 4138 | if (HAS_MULTI_BITS(options->output_format & check_mask)) |
a2f05c94 | 4139 | die(_("--name-only, --name-status, --check and -s are mutually exclusive")); |
d7de00f7 | 4140 | |
5e505257 SB |
4141 | if (HAS_MULTI_BITS(options->pickaxe_opts & DIFF_PICKAXE_KINDS_MASK)) |
4142 | die(_("-G, -S and --find-object are mutually exclusive")); | |
4143 | ||
f245194f JH |
4144 | /* |
4145 | * Most of the time we can say "there are changes" | |
4146 | * only by checking if there are changed paths, but | |
4147 | * --ignore-whitespace* options force us to look | |
97bf2a08 | 4148 | * inside contents. |
f245194f JH |
4149 | */ |
4150 | ||
446d12cb | 4151 | if ((options->xdl_opts & XDF_WHITESPACE_FLAGS)) |
0d1e0e78 | 4152 | options->flags.diff_from_contents = 1; |
f245194f | 4153 | else |
0d1e0e78 | 4154 | options->flags.diff_from_contents = 0; |
f245194f | 4155 | |
0d1e0e78 | 4156 | if (options->flags.find_copies_harder) |
03b9d560 JH |
4157 | options->detect_rename = DIFF_DETECT_COPY; |
4158 | ||
0d1e0e78 | 4159 | if (!options->flags.relative_name) |
cd676a51 JH |
4160 | options->prefix = NULL; |
4161 | if (options->prefix) | |
4162 | options->prefix_length = strlen(options->prefix); | |
4163 | else | |
4164 | options->prefix_length = 0; | |
4165 | ||
c6744349 TH |
4166 | if (options->output_format & (DIFF_FORMAT_NAME | |
4167 | DIFF_FORMAT_NAME_STATUS | | |
4168 | DIFF_FORMAT_CHECKDIFF | | |
4169 | DIFF_FORMAT_NO_OUTPUT)) | |
4170 | options->output_format &= ~(DIFF_FORMAT_RAW | | |
74e2abe5 | 4171 | DIFF_FORMAT_NUMSTAT | |
c6744349 | 4172 | DIFF_FORMAT_DIFFSTAT | |
ebd124c6 | 4173 | DIFF_FORMAT_SHORTSTAT | |
7df7c019 | 4174 | DIFF_FORMAT_DIRSTAT | |
c6744349 TH |
4175 | DIFF_FORMAT_SUMMARY | |
4176 | DIFF_FORMAT_PATCH); | |
4177 | ||
6973dcae JH |
4178 | /* |
4179 | * These cases always need recursive; we do not drop caller-supplied | |
4180 | * recursive bits for other formats here. | |
4181 | */ | |
c6744349 | 4182 | if (options->output_format & (DIFF_FORMAT_PATCH | |
74e2abe5 | 4183 | DIFF_FORMAT_NUMSTAT | |
c6744349 | 4184 | DIFF_FORMAT_DIFFSTAT | |
ebd124c6 | 4185 | DIFF_FORMAT_SHORTSTAT | |
7df7c019 | 4186 | DIFF_FORMAT_DIRSTAT | |
d7014dc0 | 4187 | DIFF_FORMAT_SUMMARY | |
c6744349 | 4188 | DIFF_FORMAT_CHECKDIFF)) |
0d1e0e78 | 4189 | options->flags.recursive = 1; |
5e363541 | 4190 | /* |
3969cf7d | 4191 | * Also pickaxe would not work very well if you do not say recursive |
5e363541 | 4192 | */ |
cf63051a | 4193 | if (options->pickaxe_opts & DIFF_PICKAXE_KINDS_MASK) |
0d1e0e78 | 4194 | options->flags.recursive = 1; |
ae6d5c1b JL |
4195 | /* |
4196 | * When patches are generated, submodules diffed against the work tree | |
4197 | * must be checked for dirtiness too so it can be shown in the output | |
4198 | */ | |
4199 | if (options->output_format & DIFF_FORMAT_PATCH) | |
0d1e0e78 | 4200 | options->flags.dirty_submodules = 1; |
5e363541 | 4201 | |
6973dcae JH |
4202 | if (options->detect_rename && options->rename_limit < 0) |
4203 | options->rename_limit = diff_rename_limit_default; | |
4204 | if (options->setup & DIFF_SETUP_USE_CACHE) { | |
4205 | if (!active_cache) | |
4206 | /* read-cache does not die even when it fails | |
4207 | * so it is safe for us to do this here. Also | |
4208 | * it does not smudge active_cache or active_nr | |
4209 | * when it fails, so we do not have to worry about | |
4210 | * cleaning it up ourselves either. | |
4211 | */ | |
4212 | read_cache(); | |
4213 | } | |
7b5b7721 | 4214 | if (40 < options->abbrev) |
6973dcae JH |
4215 | options->abbrev = 40; /* full */ |
4216 | ||
68aacb2f JH |
4217 | /* |
4218 | * It does not make sense to show the first hit we happened | |
4219 | * to have found. It does not make sense not to return with | |
4220 | * exit code in such a case either. | |
4221 | */ | |
0d1e0e78 | 4222 | if (options->flags.quick) { |
68aacb2f | 4223 | options->output_format = DIFF_FORMAT_NO_OUTPUT; |
0d1e0e78 | 4224 | options->flags.exit_with_status = 1; |
68aacb2f | 4225 | } |
ee7fb0b1 | 4226 | |
0ea7d5b6 | 4227 | options->diff_path_counter = 0; |
b0e2c999 | 4228 | |
0d1e0e78 | 4229 | if (options->flags.follow_renames && options->pathspec.nr != 1) |
dd63f169 | 4230 | die(_("--follow requires exactly one pathspec")); |
2e2d5ac1 SB |
4231 | |
4232 | if (!options->use_color || external_diff()) | |
4233 | options->color_moved = 0; | |
6973dcae JH |
4234 | } |
4235 | ||
d2543b8e | 4236 | static int opt_arg(const char *arg, int arg_short, const char *arg_long, int *val) |
ee1e5412 LT |
4237 | { |
4238 | char c, *eq; | |
4239 | int len; | |
4240 | ||
4241 | if (*arg != '-') | |
4242 | return 0; | |
4243 | c = *++arg; | |
4244 | if (!c) | |
4245 | return 0; | |
4246 | if (c == arg_short) { | |
4247 | c = *++arg; | |
4248 | if (!c) | |
4249 | return 1; | |
4250 | if (val && isdigit(c)) { | |
4251 | char *end; | |
4252 | int n = strtoul(arg, &end, 10); | |
4253 | if (*end) | |
4254 | return 0; | |
4255 | *val = n; | |
4256 | return 1; | |
4257 | } | |
4258 | return 0; | |
4259 | } | |
4260 | if (c != '-') | |
4261 | return 0; | |
4262 | arg++; | |
2c5495f7 RM |
4263 | eq = strchrnul(arg, '='); |
4264 | len = eq - arg; | |
ee1e5412 LT |
4265 | if (!len || strncmp(arg, arg_long, len)) |
4266 | return 0; | |
2c5495f7 | 4267 | if (*eq) { |
ee1e5412 LT |
4268 | int n; |
4269 | char *end; | |
4270 | if (!isdigit(*++eq)) | |
4271 | return 0; | |
4272 | n = strtoul(eq, &end, 10); | |
4273 | if (*end) | |
4274 | return 0; | |
4275 | *val = n; | |
4276 | } | |
4277 | return 1; | |
4278 | } | |
4279 | ||
16befb8b JH |
4280 | static int diff_scoreopt_parse(const char *opt); |
4281 | ||
dea007fb MM |
4282 | static inline int short_opt(char opt, const char **argv, |
4283 | const char **optarg) | |
4284 | { | |
4285 | const char *arg = argv[0]; | |
4286 | if (arg[0] != '-' || arg[1] != opt) | |
4287 | return 0; | |
4288 | if (arg[2] != '\0') { | |
4289 | *optarg = arg + 2; | |
4290 | return 1; | |
4291 | } | |
4292 | if (!argv[1]) | |
4293 | die("Option '%c' requires a value", opt); | |
4294 | *optarg = argv[1]; | |
4295 | return 2; | |
4296 | } | |
4297 | ||
4298 | int parse_long_opt(const char *opt, const char **argv, | |
4299 | const char **optarg) | |
4300 | { | |
4301 | const char *arg = argv[0]; | |
95b567c7 | 4302 | if (!skip_prefix(arg, "--", &arg)) |
dea007fb | 4303 | return 0; |
95b567c7 | 4304 | if (!skip_prefix(arg, opt, &arg)) |
dea007fb | 4305 | return 0; |
b0d12fc9 | 4306 | if (*arg == '=') { /* stuck form: --option=value */ |
dea007fb MM |
4307 | *optarg = arg + 1; |
4308 | return 1; | |
4309 | } | |
4310 | if (*arg != '\0') | |
4311 | return 0; | |
4312 | /* separate form: --option value */ | |
4313 | if (!argv[1]) | |
4314 | die("Option '--%s' requires a value", opt); | |
4315 | *optarg = argv[1]; | |
4316 | return 2; | |
4317 | } | |
4318 | ||
4d7f7a4a JN |
4319 | static int stat_opt(struct diff_options *options, const char **av) |
4320 | { | |
4321 | const char *arg = av[0]; | |
4322 | char *end; | |
4323 | int width = options->stat_width; | |
4324 | int name_width = options->stat_name_width; | |
969fe57b | 4325 | int graph_width = options->stat_graph_width; |
808e1db2 | 4326 | int count = options->stat_count; |
1e57208e | 4327 | int argcount = 1; |
4d7f7a4a | 4328 | |
0539cc00 JK |
4329 | if (!skip_prefix(arg, "--stat", &arg)) |
4330 | die("BUG: stat option does not begin with --stat: %s", arg); | |
4d7f7a4a JN |
4331 | end = (char *)arg; |
4332 | ||
4333 | switch (*arg) { | |
4334 | case '-': | |
95b567c7 | 4335 | if (skip_prefix(arg, "-width", &arg)) { |
1e57208e MM |
4336 | if (*arg == '=') |
4337 | width = strtoul(arg + 1, &end, 10); | |
4338 | else if (!*arg && !av[1]) | |
a2f05c94 | 4339 | die_want_option("--stat-width"); |
1e57208e MM |
4340 | else if (!*arg) { |
4341 | width = strtoul(av[1], &end, 10); | |
4342 | argcount = 2; | |
4343 | } | |
95b567c7 | 4344 | } else if (skip_prefix(arg, "-name-width", &arg)) { |
1e57208e MM |
4345 | if (*arg == '=') |
4346 | name_width = strtoul(arg + 1, &end, 10); | |
4347 | else if (!*arg && !av[1]) | |
a2f05c94 | 4348 | die_want_option("--stat-name-width"); |
1e57208e MM |
4349 | else if (!*arg) { |
4350 | name_width = strtoul(av[1], &end, 10); | |
4351 | argcount = 2; | |
4352 | } | |
95b567c7 | 4353 | } else if (skip_prefix(arg, "-graph-width", &arg)) { |
969fe57b ZJS |
4354 | if (*arg == '=') |
4355 | graph_width = strtoul(arg + 1, &end, 10); | |
4356 | else if (!*arg && !av[1]) | |
a2f05c94 | 4357 | die_want_option("--stat-graph-width"); |
969fe57b ZJS |
4358 | else if (!*arg) { |
4359 | graph_width = strtoul(av[1], &end, 10); | |
4360 | argcount = 2; | |
4361 | } | |
95b567c7 | 4362 | } else if (skip_prefix(arg, "-count", &arg)) { |
808e1db2 MG |
4363 | if (*arg == '=') |
4364 | count = strtoul(arg + 1, &end, 10); | |
4365 | else if (!*arg && !av[1]) | |
a2f05c94 | 4366 | die_want_option("--stat-count"); |
808e1db2 MG |
4367 | else if (!*arg) { |
4368 | count = strtoul(av[1], &end, 10); | |
4369 | argcount = 2; | |
4370 | } | |
1e57208e | 4371 | } |
4d7f7a4a JN |
4372 | break; |
4373 | case '=': | |
4374 | width = strtoul(arg+1, &end, 10); | |
4375 | if (*end == ',') | |
4376 | name_width = strtoul(end+1, &end, 10); | |
808e1db2 MG |
4377 | if (*end == ',') |
4378 | count = strtoul(end+1, &end, 10); | |
4d7f7a4a JN |
4379 | } |
4380 | ||
4381 | /* Important! This checks all the error cases! */ | |
4382 | if (*end) | |
4383 | return 0; | |
4384 | options->output_format |= DIFF_FORMAT_DIFFSTAT; | |
4385 | options->stat_name_width = name_width; | |
969fe57b | 4386 | options->stat_graph_width = graph_width; |
4d7f7a4a | 4387 | options->stat_width = width; |
808e1db2 | 4388 | options->stat_count = count; |
1e57208e | 4389 | return argcount; |
4d7f7a4a JN |
4390 | } |
4391 | ||
333f3fb0 JH |
4392 | static int parse_dirstat_opt(struct diff_options *options, const char *params) |
4393 | { | |
51670fc8 JH |
4394 | struct strbuf errmsg = STRBUF_INIT; |
4395 | if (parse_dirstat_params(options, params, &errmsg)) | |
7478ac57 | 4396 | die(_("Failed to parse --dirstat/-X option parameter:\n%s"), |
51670fc8 JH |
4397 | errmsg.buf); |
4398 | strbuf_release(&errmsg); | |
333f3fb0 JH |
4399 | /* |
4400 | * The caller knows a dirstat-related option is given from the command | |
4401 | * line; allow it to say "return this_function();" | |
4402 | */ | |
4403 | options->output_format |= DIFF_FORMAT_DIRSTAT; | |
4404 | return 1; | |
4405 | } | |
4406 | ||
c47ef57c RR |
4407 | static int parse_submodule_opt(struct diff_options *options, const char *value) |
4408 | { | |
4409 | if (parse_submodule_params(options, value)) | |
4410 | die(_("Failed to parse --submodule option parameter: '%s'"), | |
4411 | value); | |
4412 | return 1; | |
4413 | } | |
4414 | ||
1ecc1cbd JH |
4415 | static const char diff_status_letters[] = { |
4416 | DIFF_STATUS_ADDED, | |
4417 | DIFF_STATUS_COPIED, | |
4418 | DIFF_STATUS_DELETED, | |
4419 | DIFF_STATUS_MODIFIED, | |
4420 | DIFF_STATUS_RENAMED, | |
4421 | DIFF_STATUS_TYPE_CHANGED, | |
4422 | DIFF_STATUS_UNKNOWN, | |
4423 | DIFF_STATUS_UNMERGED, | |
4424 | DIFF_STATUS_FILTER_AON, | |
4425 | DIFF_STATUS_FILTER_BROKEN, | |
4426 | '\0', | |
4427 | }; | |
4428 | ||
4429 | static unsigned int filter_bit['Z' + 1]; | |
4430 | ||
4431 | static void prepare_filter_bits(void) | |
4432 | { | |
4433 | int i; | |
4434 | ||
4435 | if (!filter_bit[DIFF_STATUS_ADDED]) { | |
4436 | for (i = 0; diff_status_letters[i]; i++) | |
4437 | filter_bit[(int) diff_status_letters[i]] = (1 << i); | |
4438 | } | |
4439 | } | |
4440 | ||
4441 | static unsigned filter_bit_tst(char status, const struct diff_options *opt) | |
4442 | { | |
4443 | return opt->filter & filter_bit[(int) status]; | |
4444 | } | |
4445 | ||
4446 | static int parse_diff_filter_opt(const char *optarg, struct diff_options *opt) | |
4447 | { | |
4448 | int i, optch; | |
4449 | ||
4450 | prepare_filter_bits(); | |
7f2ea5f0 JH |
4451 | |
4452 | /* | |
4453 | * If there is a negation e.g. 'd' in the input, and we haven't | |
4454 | * initialized the filter field with another --diff-filter, start | |
4455 | * from full set of bits, except for AON. | |
4456 | */ | |
4457 | if (!opt->filter) { | |
4458 | for (i = 0; (optch = optarg[i]) != '\0'; i++) { | |
4459 | if (optch < 'a' || 'z' < optch) | |
4460 | continue; | |
4461 | opt->filter = (1 << (ARRAY_SIZE(diff_status_letters) - 1)) - 1; | |
4462 | opt->filter &= ~filter_bit[DIFF_STATUS_FILTER_AON]; | |
4463 | break; | |
4464 | } | |
4465 | } | |
4466 | ||
1ecc1cbd JH |
4467 | for (i = 0; (optch = optarg[i]) != '\0'; i++) { |
4468 | unsigned int bit; | |
7f2ea5f0 JH |
4469 | int negate; |
4470 | ||
4471 | if ('a' <= optch && optch <= 'z') { | |
4472 | negate = 1; | |
4473 | optch = toupper(optch); | |
4474 | } else { | |
4475 | negate = 0; | |
4476 | } | |
1ecc1cbd JH |
4477 | |
4478 | bit = (0 <= optch && optch <= 'Z') ? filter_bit[optch] : 0; | |
4479 | if (!bit) | |
bf142ec4 | 4480 | return optarg[i]; |
7f2ea5f0 JH |
4481 | if (negate) |
4482 | opt->filter &= ~bit; | |
4483 | else | |
4484 | opt->filter |= bit; | |
1ecc1cbd JH |
4485 | } |
4486 | return 0; | |
4487 | } | |
4488 | ||
71482d38 MM |
4489 | static void enable_patch_output(int *fmt) { |
4490 | *fmt &= ~DIFF_FORMAT_NO_OUTPUT; | |
4491 | *fmt |= DIFF_FORMAT_PATCH; | |
4492 | } | |
4493 | ||
077965f8 | 4494 | static int parse_ws_error_highlight_opt(struct diff_options *opt, const char *arg) |
b8767f79 | 4495 | { |
077965f8 | 4496 | int val = parse_ws_error_highlight(arg); |
b8767f79 | 4497 | |
077965f8 JH |
4498 | if (val < 0) { |
4499 | error("unknown value after ws-error-highlight=%.*s", | |
4500 | -1 - val, arg); | |
4501 | return 0; | |
b8767f79 JH |
4502 | } |
4503 | opt->ws_error_highlight = val; | |
4504 | return 1; | |
4505 | } | |
4506 | ||
15af58c1 SB |
4507 | static int parse_objfind_opt(struct diff_options *opt, const char *arg) |
4508 | { | |
4509 | struct object_id oid; | |
4510 | ||
4511 | if (get_oid(arg, &oid)) | |
4512 | return error("unable to resolve '%s'", arg); | |
4513 | ||
4514 | if (!opt->objfind) | |
4515 | opt->objfind = xcalloc(1, sizeof(*opt->objfind)); | |
4516 | ||
4517 | opt->pickaxe_opts |= DIFF_PICKAXE_KIND_OBJFIND; | |
4518 | opt->flags.recursive = 1; | |
4519 | opt->flags.tree_in_recursive = 1; | |
4520 | oidset_insert(opt->objfind, &oid); | |
4521 | return 1; | |
4522 | } | |
4523 | ||
a97262c6 NTND |
4524 | int diff_opt_parse(struct diff_options *options, |
4525 | const char **av, int ac, const char *prefix) | |
6973dcae JH |
4526 | { |
4527 | const char *arg = av[0]; | |
dea007fb MM |
4528 | const char *optarg; |
4529 | int argcount; | |
d054680c | 4530 | |
a97262c6 NTND |
4531 | if (!prefix) |
4532 | prefix = ""; | |
4533 | ||
d054680c | 4534 | /* Output format options */ |
71482d38 MM |
4535 | if (!strcmp(arg, "-p") || !strcmp(arg, "-u") || !strcmp(arg, "--patch") |
4536 | || opt_arg(arg, 'U', "unified", &options->context)) | |
4537 | enable_patch_output(&options->output_format); | |
a610786f TH |
4538 | else if (!strcmp(arg, "--raw")) |
4539 | options->output_format |= DIFF_FORMAT_RAW; | |
71482d38 MM |
4540 | else if (!strcmp(arg, "--patch-with-raw")) { |
4541 | enable_patch_output(&options->output_format); | |
4542 | options->output_format |= DIFF_FORMAT_RAW; | |
4543 | } else if (!strcmp(arg, "--numstat")) | |
74e2abe5 | 4544 | options->output_format |= DIFF_FORMAT_NUMSTAT; |
8f67f8ae | 4545 | else if (!strcmp(arg, "--shortstat")) |
ebd124c6 | 4546 | options->output_format |= DIFF_FORMAT_SHORTSTAT; |
948cbe67 CC |
4547 | else if (skip_prefix(arg, "-X", &arg) || |
4548 | skip_to_optional_arg(arg, "--dirstat", &arg)) | |
ae021d87 | 4549 | return parse_dirstat_opt(options, arg); |
333f3fb0 JH |
4550 | else if (!strcmp(arg, "--cumulative")) |
4551 | return parse_dirstat_opt(options, "cumulative"); | |
948cbe67 | 4552 | else if (skip_to_optional_arg(arg, "--dirstat-by-file", &arg)) { |
333f3fb0 | 4553 | parse_dirstat_opt(options, "files"); |
ae021d87 | 4554 | return parse_dirstat_opt(options, arg); |
f88d225f | 4555 | } |
d054680c PH |
4556 | else if (!strcmp(arg, "--check")) |
4557 | options->output_format |= DIFF_FORMAT_CHECKDIFF; | |
4558 | else if (!strcmp(arg, "--summary")) | |
4559 | options->output_format |= DIFF_FORMAT_SUMMARY; | |
71482d38 MM |
4560 | else if (!strcmp(arg, "--patch-with-stat")) { |
4561 | enable_patch_output(&options->output_format); | |
4562 | options->output_format |= DIFF_FORMAT_DIFFSTAT; | |
4563 | } else if (!strcmp(arg, "--name-only")) | |
d054680c PH |
4564 | options->output_format |= DIFF_FORMAT_NAME; |
4565 | else if (!strcmp(arg, "--name-status")) | |
4566 | options->output_format |= DIFF_FORMAT_NAME_STATUS; | |
d09cd15d | 4567 | else if (!strcmp(arg, "-s") || !strcmp(arg, "--no-patch")) |
d054680c | 4568 | options->output_format |= DIFF_FORMAT_NO_OUTPUT; |
59556548 | 4569 | else if (starts_with(arg, "--stat")) |
808e1db2 | 4570 | /* --stat, --stat-width, --stat-name-width, or --stat-count */ |
4d7f7a4a | 4571 | return stat_opt(options, av); |
ddf88fa6 NTND |
4572 | else if (!strcmp(arg, "--compact-summary")) { |
4573 | options->flags.stat_with_summary = 1; | |
4574 | options->output_format |= DIFF_FORMAT_DIFFSTAT; | |
4575 | } else if (!strcmp(arg, "--no-compact-summary")) | |
4576 | options->flags.stat_with_summary = 0; | |
d054680c PH |
4577 | |
4578 | /* renames options */ | |
948cbe67 CC |
4579 | else if (starts_with(arg, "-B") || |
4580 | skip_to_optional_arg(arg, "--break-rewrites", NULL)) { | |
8f67f8ae | 4581 | if ((options->break_opt = diff_scoreopt_parse(arg)) == -1) |
07cd7265 | 4582 | return error("invalid argument to -B: %s", arg+2); |
6973dcae | 4583 | } |
948cbe67 CC |
4584 | else if (starts_with(arg, "-M") || |
4585 | skip_to_optional_arg(arg, "--find-renames", NULL)) { | |
8f67f8ae | 4586 | if ((options->rename_score = diff_scoreopt_parse(arg)) == -1) |
07cd7265 | 4587 | return error("invalid argument to -M: %s", arg+2); |
6973dcae JH |
4588 | options->detect_rename = DIFF_DETECT_RENAME; |
4589 | } | |
467ddc14 JH |
4590 | else if (!strcmp(arg, "-D") || !strcmp(arg, "--irreversible-delete")) { |
4591 | options->irreversible_delete = 1; | |
4592 | } | |
948cbe67 CC |
4593 | else if (starts_with(arg, "-C") || |
4594 | skip_to_optional_arg(arg, "--find-copies", NULL)) { | |
ca6c0970 | 4595 | if (options->detect_rename == DIFF_DETECT_COPY) |
0d1e0e78 | 4596 | options->flags.find_copies_harder = 1; |
8f67f8ae | 4597 | if ((options->rename_score = diff_scoreopt_parse(arg)) == -1) |
07cd7265 | 4598 | return error("invalid argument to -C: %s", arg+2); |
6973dcae JH |
4599 | options->detect_rename = DIFF_DETECT_COPY; |
4600 | } | |
d054680c PH |
4601 | else if (!strcmp(arg, "--no-renames")) |
4602 | options->detect_rename = 0; | |
90d43b07 | 4603 | else if (!strcmp(arg, "--rename-empty")) |
0d1e0e78 | 4604 | options->flags.rename_empty = 1; |
90d43b07 | 4605 | else if (!strcmp(arg, "--no-rename-empty")) |
0d1e0e78 | 4606 | options->flags.rename_empty = 0; |
1efad511 | 4607 | else if (skip_to_optional_arg_default(arg, "--relative", &arg, NULL)) { |
0d1e0e78 | 4608 | options->flags.relative_name = 1; |
1efad511 JH |
4609 | if (arg) |
4610 | options->prefix = arg; | |
c0cb4a06 | 4611 | } |
d054680c PH |
4612 | |
4613 | /* xdiff options */ | |
81b568c8 JH |
4614 | else if (!strcmp(arg, "--minimal")) |
4615 | DIFF_XDL_SET(options, NEED_MINIMAL); | |
4616 | else if (!strcmp(arg, "--no-minimal")) | |
4617 | DIFF_XDL_CLR(options, NEED_MINIMAL); | |
d054680c | 4618 | else if (!strcmp(arg, "-w") || !strcmp(arg, "--ignore-all-space")) |
628d5c2b | 4619 | DIFF_XDL_SET(options, IGNORE_WHITESPACE); |
d054680c | 4620 | else if (!strcmp(arg, "-b") || !strcmp(arg, "--ignore-space-change")) |
628d5c2b | 4621 | DIFF_XDL_SET(options, IGNORE_WHITESPACE_CHANGE); |
d054680c | 4622 | else if (!strcmp(arg, "--ignore-space-at-eol")) |
628d5c2b | 4623 | DIFF_XDL_SET(options, IGNORE_WHITESPACE_AT_EOL); |
e9282f02 JH |
4624 | else if (!strcmp(arg, "--ignore-cr-at-eol")) |
4625 | DIFF_XDL_SET(options, IGNORE_CR_AT_EOL); | |
36617af7 AP |
4626 | else if (!strcmp(arg, "--ignore-blank-lines")) |
4627 | DIFF_XDL_SET(options, IGNORE_BLANK_LINES); | |
3cde4e02 | 4628 | else if (!strcmp(arg, "--indent-heuristic")) |
433860f3 | 4629 | DIFF_XDL_SET(options, INDENT_HEURISTIC); |
3cde4e02 | 4630 | else if (!strcmp(arg, "--no-indent-heuristic")) |
433860f3 | 4631 | DIFF_XDL_CLR(options, INDENT_HEURISTIC); |
2477ab2e JT |
4632 | else if (!strcmp(arg, "--patience")) { |
4633 | int i; | |
307ab20b | 4634 | options->xdl_opts = DIFF_WITH_ALG(options, PATIENCE_DIFF); |
2477ab2e JT |
4635 | /* |
4636 | * Both --patience and --anchored use PATIENCE_DIFF | |
4637 | * internally, so remove any anchors previously | |
4638 | * specified. | |
4639 | */ | |
4640 | for (i = 0; i < options->anchors_nr; i++) | |
4641 | free(options->anchors[i]); | |
4642 | options->anchors_nr = 0; | |
4643 | } else if (!strcmp(arg, "--histogram")) | |
307ab20b | 4644 | options->xdl_opts = DIFF_WITH_ALG(options, HISTOGRAM_DIFF); |
0895c6d4 JK |
4645 | else if ((argcount = parse_long_opt("diff-algorithm", av, &optarg))) { |
4646 | long value = parse_algorithm_value(optarg); | |
07924d4d MP |
4647 | if (value < 0) |
4648 | return error("option diff-algorithm accepts \"myers\", " | |
4649 | "\"minimal\", \"patience\" and \"histogram\""); | |
4650 | /* clear out previous settings */ | |
4651 | DIFF_XDL_CLR(options, NEED_MINIMAL); | |
4652 | options->xdl_opts &= ~XDF_DIFF_ALGORITHM_MASK; | |
4653 | options->xdl_opts |= value; | |
0895c6d4 | 4654 | return argcount; |
2477ab2e JT |
4655 | } else if (skip_prefix(arg, "--anchored=", &arg)) { |
4656 | options->xdl_opts = DIFF_WITH_ALG(options, PATIENCE_DIFF); | |
4657 | ALLOC_GROW(options->anchors, options->anchors_nr + 1, | |
4658 | options->anchors_alloc); | |
4659 | options->anchors[options->anchors_nr++] = xstrdup(arg); | |
07924d4d | 4660 | } |
d054680c PH |
4661 | |
4662 | /* flags options */ | |
4663 | else if (!strcmp(arg, "--binary")) { | |
71482d38 | 4664 | enable_patch_output(&options->output_format); |
0d1e0e78 | 4665 | options->flags.binary = 1; |
d054680c PH |
4666 | } |
4667 | else if (!strcmp(arg, "--full-index")) | |
0d1e0e78 | 4668 | options->flags.full_index = 1; |
d054680c | 4669 | else if (!strcmp(arg, "-a") || !strcmp(arg, "--text")) |
0d1e0e78 | 4670 | options->flags.text = 1; |
d054680c | 4671 | else if (!strcmp(arg, "-R")) |
0d1e0e78 | 4672 | options->flags.reverse_diff = 1; |
6973dcae | 4673 | else if (!strcmp(arg, "--find-copies-harder")) |
0d1e0e78 | 4674 | options->flags.find_copies_harder = 1; |
750f7b66 | 4675 | else if (!strcmp(arg, "--follow")) |
0d1e0e78 | 4676 | options->flags.follow_renames = 1; |
076c9837 | 4677 | else if (!strcmp(arg, "--no-follow")) { |
0d1e0e78 BW |
4678 | options->flags.follow_renames = 0; |
4679 | options->flags.default_follow_renames = 0; | |
cf81f94d | 4680 | } else if (skip_to_optional_arg_default(arg, "--color", &arg, "always")) { |
ae021d87 | 4681 | int value = git_config_colorbool(NULL, arg); |
daa0c3d9 | 4682 | if (value < 0) |
73e9da01 | 4683 | return error("option `color' expects \"always\", \"auto\", or \"never\""); |
daa0c3d9 | 4684 | options->use_color = value; |
73e9da01 | 4685 | } |
fef88bb0 | 4686 | else if (!strcmp(arg, "--no-color")) |
f1c96261 | 4687 | options->use_color = 0; |
2e2d5ac1 SB |
4688 | else if (!strcmp(arg, "--color-moved")) { |
4689 | if (diff_color_moved_default) | |
4690 | options->color_moved = diff_color_moved_default; | |
4691 | if (options->color_moved == COLOR_MOVED_NO) | |
4692 | options->color_moved = COLOR_MOVED_DEFAULT; | |
4693 | } else if (!strcmp(arg, "--no-color-moved")) | |
4694 | options->color_moved = COLOR_MOVED_NO; | |
4695 | else if (skip_prefix(arg, "--color-moved=", &arg)) { | |
4696 | int cm = parse_color_moved(arg); | |
4697 | if (cm < 0) | |
4698 | die("bad --color-moved argument: %s", arg); | |
4699 | options->color_moved = cm; | |
cf81f94d | 4700 | } else if (skip_to_optional_arg_default(arg, "--color-words", &options->word_regex, NULL)) { |
f1c96261 | 4701 | options->use_color = 1; |
882749a0 | 4702 | options->word_diff = DIFF_WORDS_COLOR; |
2b6a5417 | 4703 | } |
882749a0 TR |
4704 | else if (!strcmp(arg, "--word-diff")) { |
4705 | if (options->word_diff == DIFF_WORDS_NONE) | |
4706 | options->word_diff = DIFF_WORDS_PLAIN; | |
4707 | } | |
ae021d87 JK |
4708 | else if (skip_prefix(arg, "--word-diff=", &arg)) { |
4709 | if (!strcmp(arg, "plain")) | |
882749a0 | 4710 | options->word_diff = DIFF_WORDS_PLAIN; |
ae021d87 | 4711 | else if (!strcmp(arg, "color")) { |
f1c96261 | 4712 | options->use_color = 1; |
882749a0 TR |
4713 | options->word_diff = DIFF_WORDS_COLOR; |
4714 | } | |
ae021d87 | 4715 | else if (!strcmp(arg, "porcelain")) |
882749a0 | 4716 | options->word_diff = DIFF_WORDS_PORCELAIN; |
ae021d87 | 4717 | else if (!strcmp(arg, "none")) |
882749a0 TR |
4718 | options->word_diff = DIFF_WORDS_NONE; |
4719 | else | |
ae021d87 | 4720 | die("bad --word-diff argument: %s", arg); |
882749a0 | 4721 | } |
dea007fb | 4722 | else if ((argcount = parse_long_opt("word-diff-regex", av, &optarg))) { |
882749a0 TR |
4723 | if (options->word_diff == DIFF_WORDS_NONE) |
4724 | options->word_diff = DIFF_WORDS_PLAIN; | |
dea007fb MM |
4725 | options->word_regex = optarg; |
4726 | return argcount; | |
882749a0 | 4727 | } |
41bbf9d5 | 4728 | else if (!strcmp(arg, "--exit-code")) |
0d1e0e78 | 4729 | options->flags.exit_with_status = 1; |
68aacb2f | 4730 | else if (!strcmp(arg, "--quiet")) |
0d1e0e78 | 4731 | options->flags.quick = 1; |
72909bef | 4732 | else if (!strcmp(arg, "--ext-diff")) |
0d1e0e78 | 4733 | options->flags.allow_external = 1; |
72909bef | 4734 | else if (!strcmp(arg, "--no-ext-diff")) |
0d1e0e78 | 4735 | options->flags.allow_external = 0; |
afa73c53 | 4736 | else if (!strcmp(arg, "--textconv")) { |
0d1e0e78 BW |
4737 | options->flags.allow_textconv = 1; |
4738 | options->flags.textconv_set_via_cmdline = 1; | |
afa73c53 | 4739 | } else if (!strcmp(arg, "--no-textconv")) |
0d1e0e78 | 4740 | options->flags.allow_textconv = 0; |
cf81f94d | 4741 | else if (skip_to_optional_arg_default(arg, "--ignore-submodules", &arg, "all")) { |
0d1e0e78 | 4742 | options->flags.override_submodule_config = 1; |
ae021d87 | 4743 | handle_ignore_submodules_arg(options, arg); |
cf81f94d | 4744 | } else if (skip_to_optional_arg_default(arg, "--submodule", &arg, "log")) |
ae021d87 | 4745 | return parse_submodule_opt(options, arg); |
b8767f79 | 4746 | else if (skip_prefix(arg, "--ws-error-highlight=", &arg)) |
077965f8 | 4747 | return parse_ws_error_highlight_opt(options, arg); |
b42b4519 NTND |
4748 | else if (!strcmp(arg, "--ita-invisible-in-index")) |
4749 | options->ita_invisible_in_index = 1; | |
4750 | else if (!strcmp(arg, "--ita-visible-in-index")) | |
4751 | options->ita_invisible_in_index = 0; | |
d054680c PH |
4752 | |
4753 | /* misc options */ | |
4754 | else if (!strcmp(arg, "-z")) | |
4755 | options->line_termination = 0; | |
dea007fb MM |
4756 | else if ((argcount = short_opt('l', av, &optarg))) { |
4757 | options->rename_limit = strtoul(optarg, NULL, 10); | |
4758 | return argcount; | |
4759 | } | |
4760 | else if ((argcount = short_opt('S', av, &optarg))) { | |
4761 | options->pickaxe = optarg; | |
f506b8e8 JH |
4762 | options->pickaxe_opts |= DIFF_PICKAXE_KIND_S; |
4763 | return argcount; | |
4764 | } else if ((argcount = short_opt('G', av, &optarg))) { | |
4765 | options->pickaxe = optarg; | |
4766 | options->pickaxe_opts |= DIFF_PICKAXE_KIND_G; | |
dea007fb MM |
4767 | return argcount; |
4768 | } | |
d054680c | 4769 | else if (!strcmp(arg, "--pickaxe-all")) |
f506b8e8 | 4770 | options->pickaxe_opts |= DIFF_PICKAXE_ALL; |
d054680c | 4771 | else if (!strcmp(arg, "--pickaxe-regex")) |
f506b8e8 | 4772 | options->pickaxe_opts |= DIFF_PICKAXE_REGEX; |
dea007fb | 4773 | else if ((argcount = short_opt('O', av, &optarg))) { |
e4da43b1 | 4774 | options->orderfile = prefix_filename(prefix, optarg); |
dea007fb | 4775 | return argcount; |
15af58c1 SB |
4776 | } else if (skip_prefix(arg, "--find-object=", &arg)) |
4777 | return parse_objfind_opt(options, arg); | |
dea007fb | 4778 | else if ((argcount = parse_long_opt("diff-filter", av, &optarg))) { |
1ecc1cbd JH |
4779 | int offending = parse_diff_filter_opt(optarg, options); |
4780 | if (offending) | |
4781 | die("unknown change class '%c' in --diff-filter=%s", | |
4782 | offending, optarg); | |
dea007fb MM |
4783 | return argcount; |
4784 | } | |
43d1948b JB |
4785 | else if (!strcmp(arg, "--no-abbrev")) |
4786 | options->abbrev = 0; | |
d054680c PH |
4787 | else if (!strcmp(arg, "--abbrev")) |
4788 | options->abbrev = DEFAULT_ABBREV; | |
ae021d87 JK |
4789 | else if (skip_prefix(arg, "--abbrev=", &arg)) { |
4790 | options->abbrev = strtoul(arg, NULL, 10); | |
d054680c PH |
4791 | if (options->abbrev < MINIMUM_ABBREV) |
4792 | options->abbrev = MINIMUM_ABBREV; | |
4793 | else if (40 < options->abbrev) | |
4794 | options->abbrev = 40; | |
4795 | } | |
dea007fb MM |
4796 | else if ((argcount = parse_long_opt("src-prefix", av, &optarg))) { |
4797 | options->a_prefix = optarg; | |
4798 | return argcount; | |
4799 | } | |
660e113c JK |
4800 | else if ((argcount = parse_long_opt("line-prefix", av, &optarg))) { |
4801 | options->line_prefix = optarg; | |
4802 | options->line_prefix_length = strlen(options->line_prefix); | |
4803 | graph_setup_line_prefix(options); | |
4804 | return argcount; | |
4805 | } | |
dea007fb MM |
4806 | else if ((argcount = parse_long_opt("dst-prefix", av, &optarg))) { |
4807 | options->b_prefix = optarg; | |
4808 | return argcount; | |
4809 | } | |
eab9a40b JS |
4810 | else if (!strcmp(arg, "--no-prefix")) |
4811 | options->a_prefix = options->b_prefix = ""; | |
6d0e674a RS |
4812 | else if (opt_arg(arg, '\0', "inter-hunk-context", |
4813 | &options->interhunkcontext)) | |
4814 | ; | |
14937c2c | 4815 | else if (!strcmp(arg, "-W")) |
0d1e0e78 | 4816 | options->flags.funccontext = 1; |
14937c2c | 4817 | else if (!strcmp(arg, "--function-context")) |
0d1e0e78 | 4818 | options->flags.funccontext = 1; |
14937c2c | 4819 | else if (!strcmp(arg, "--no-function-context")) |
0d1e0e78 | 4820 | options->flags.funccontext = 0; |
dea007fb | 4821 | else if ((argcount = parse_long_opt("output", av, &optarg))) { |
e4da43b1 | 4822 | char *path = prefix_filename(prefix, optarg); |
23a9e071 | 4823 | options->file = xfopen(path, "w"); |
c0c77734 | 4824 | options->close_file = 1; |
afc676f2 JS |
4825 | if (options->use_color != GIT_COLOR_ALWAYS) |
4826 | options->use_color = GIT_COLOR_NEVER; | |
e4da43b1 | 4827 | free(path); |
dea007fb | 4828 | return argcount; |
c0c77734 | 4829 | } else |
6973dcae JH |
4830 | return 0; |
4831 | return 1; | |
4832 | } | |
4833 | ||
10ae7526 | 4834 | int parse_rename_score(const char **cp_p) |
6973dcae JH |
4835 | { |
4836 | unsigned long num, scale; | |
4837 | int ch, dot; | |
4838 | const char *cp = *cp_p; | |
4839 | ||
4840 | num = 0; | |
4841 | scale = 1; | |
4842 | dot = 0; | |
eeefa7c9 | 4843 | for (;;) { |
6973dcae JH |
4844 | ch = *cp; |
4845 | if ( !dot && ch == '.' ) { | |
4846 | scale = 1; | |
4847 | dot = 1; | |
4848 | } else if ( ch == '%' ) { | |
4849 | scale = dot ? scale*100 : 100; | |
4850 | cp++; /* % is always at the end */ | |
4851 | break; | |
4852 | } else if ( ch >= '0' && ch <= '9' ) { | |
4853 | if ( scale < 100000 ) { | |
4854 | scale *= 10; | |
4855 | num = (num*10) + (ch-'0'); | |
4856 | } | |
4857 | } else { | |
4858 | break; | |
4859 | } | |
4860 | cp++; | |
4861 | } | |
4862 | *cp_p = cp; | |
4863 | ||
4864 | /* user says num divided by scale and we say internally that | |
4865 | * is MAX_SCORE * num / scale. | |
4866 | */ | |
dc49cd76 | 4867 | return (int)((num >= scale) ? MAX_SCORE : (MAX_SCORE * num / scale)); |
6973dcae JH |
4868 | } |
4869 | ||
16befb8b | 4870 | static int diff_scoreopt_parse(const char *opt) |
6973dcae JH |
4871 | { |
4872 | int opt1, opt2, cmd; | |
4873 | ||
4874 | if (*opt++ != '-') | |
4875 | return -1; | |
4876 | cmd = *opt++; | |
37ab5156 KB |
4877 | if (cmd == '-') { |
4878 | /* convert the long-form arguments into short-form versions */ | |
95b567c7 | 4879 | if (skip_prefix(opt, "break-rewrites", &opt)) { |
37ab5156 KB |
4880 | if (*opt == 0 || *opt++ == '=') |
4881 | cmd = 'B'; | |
95b567c7 | 4882 | } else if (skip_prefix(opt, "find-copies", &opt)) { |
37ab5156 KB |
4883 | if (*opt == 0 || *opt++ == '=') |
4884 | cmd = 'C'; | |
95b567c7 | 4885 | } else if (skip_prefix(opt, "find-renames", &opt)) { |
37ab5156 KB |
4886 | if (*opt == 0 || *opt++ == '=') |
4887 | cmd = 'M'; | |
4888 | } | |
4889 | } | |
6973dcae | 4890 | if (cmd != 'M' && cmd != 'C' && cmd != 'B') |
01689909 | 4891 | return -1; /* that is not a -M, -C, or -B option */ |
6973dcae | 4892 | |
10ae7526 | 4893 | opt1 = parse_rename_score(&opt); |
6973dcae JH |
4894 | if (cmd != 'B') |
4895 | opt2 = 0; | |
4896 | else { | |
4897 | if (*opt == 0) | |
4898 | opt2 = 0; | |
4899 | else if (*opt != '/') | |
4900 | return -1; /* we expect -B80/99 or -B80 */ | |
4901 | else { | |
4902 | opt++; | |
10ae7526 | 4903 | opt2 = parse_rename_score(&opt); |
6973dcae JH |
4904 | } |
4905 | } | |
4906 | if (*opt != 0) | |
4907 | return -1; | |
4908 | return opt1 | (opt2 << 16); | |
4909 | } | |
4910 | ||
4911 | struct diff_queue_struct diff_queued_diff; | |
4912 | ||
4913 | void diff_q(struct diff_queue_struct *queue, struct diff_filepair *dp) | |
4914 | { | |
4c960a43 | 4915 | ALLOC_GROW(queue->queue, queue->nr + 1, queue->alloc); |
6973dcae JH |
4916 | queue->queue[queue->nr++] = dp; |
4917 | } | |
4918 | ||
4919 | struct diff_filepair *diff_queue(struct diff_queue_struct *queue, | |
4920 | struct diff_filespec *one, | |
4921 | struct diff_filespec *two) | |
4922 | { | |
ef677686 | 4923 | struct diff_filepair *dp = xcalloc(1, sizeof(*dp)); |
6973dcae JH |
4924 | dp->one = one; |
4925 | dp->two = two; | |
6973dcae JH |
4926 | if (queue) |
4927 | diff_q(queue, dp); | |
4928 | return dp; | |
4929 | } | |
4930 | ||
4931 | void diff_free_filepair(struct diff_filepair *p) | |
4932 | { | |
9fb88419 LT |
4933 | free_filespec(p->one); |
4934 | free_filespec(p->two); | |
6973dcae JH |
4935 | free(p); |
4936 | } | |
4937 | ||
d6cece51 | 4938 | const char *diff_aligned_abbrev(const struct object_id *oid, int len) |
6973dcae JH |
4939 | { |
4940 | int abblen; | |
4941 | const char *abbrev; | |
6973dcae | 4942 | |
7cb6ac1e | 4943 | /* Do we want all 40 hex characters? */ |
d6cece51 JK |
4944 | if (len == GIT_SHA1_HEXSZ) |
4945 | return oid_to_hex(oid); | |
4946 | ||
7cb6ac1e | 4947 | /* An abbreviated value is fine, possibly followed by an ellipsis. */ |
4f03666a | 4948 | abbrev = diff_abbrev_oid(oid, len); |
7cb6ac1e AR |
4949 | |
4950 | if (!print_sha1_ellipsis()) | |
4951 | return abbrev; | |
4952 | ||
6973dcae | 4953 | abblen = strlen(abbrev); |
d709f1fb JH |
4954 | |
4955 | /* | |
7cb6ac1e | 4956 | * In well-behaved cases, where the abbreviated result is the |
d709f1fb JH |
4957 | * same as the requested length, append three dots after the |
4958 | * abbreviation (hence the whole logic is limited to the case | |
4959 | * where abblen < 37); when the actual abbreviated result is a | |
4960 | * bit longer than the requested length, we reduce the number | |
4961 | * of dots so that they match the well-behaved ones. However, | |
4962 | * if the actual abbreviation is longer than the requested | |
4963 | * length by more than three, we give up on aligning, and add | |
4964 | * three dots anyway, to indicate that the output is not the | |
4965 | * full object name. Yes, this may be suboptimal, but this | |
4966 | * appears only in "diff --raw --abbrev" output and it is not | |
4967 | * worth the effort to change it now. Note that this would | |
4968 | * likely to work fine when the automatic sizing of default | |
4969 | * abbreviation length is used--we would be fed -1 in "len" in | |
4970 | * that case, and will end up always appending three-dots, but | |
4971 | * the automatic sizing is supposed to give abblen that ensures | |
4972 | * uniqueness across all objects (statistically speaking). | |
4973 | */ | |
d6cece51 | 4974 | if (abblen < GIT_SHA1_HEXSZ - 3) { |
dc01505f | 4975 | static char hex[GIT_MAX_HEXSZ + 1]; |
6973dcae | 4976 | if (len < abblen && abblen <= len + 2) |
5096d490 | 4977 | xsnprintf(hex, sizeof(hex), "%s%.*s", abbrev, len+3-abblen, ".."); |
6973dcae | 4978 | else |
5096d490 | 4979 | xsnprintf(hex, sizeof(hex), "%s...", abbrev); |
6973dcae JH |
4980 | return hex; |
4981 | } | |
d6cece51 JK |
4982 | |
4983 | return oid_to_hex(oid); | |
6973dcae JH |
4984 | } |
4985 | ||
663af342 | 4986 | static void diff_flush_raw(struct diff_filepair *p, struct diff_options *opt) |
6973dcae | 4987 | { |
663af342 PH |
4988 | int line_termination = opt->line_termination; |
4989 | int inter_name_termination = line_termination ? '\t' : '\0'; | |
6973dcae | 4990 | |
30997bb8 | 4991 | fprintf(opt->file, "%s", diff_line_prefix(opt)); |
663af342 | 4992 | if (!(opt->output_format & DIFF_FORMAT_NAME_STATUS)) { |
c0c77734 | 4993 | fprintf(opt->file, ":%06o %06o %s ", p->one->mode, p->two->mode, |
d6cece51 | 4994 | diff_aligned_abbrev(&p->one->oid, opt->abbrev)); |
a0d12c44 | 4995 | fprintf(opt->file, "%s ", |
d6cece51 | 4996 | diff_aligned_abbrev(&p->two->oid, opt->abbrev)); |
6973dcae | 4997 | } |
663af342 | 4998 | if (p->score) { |
c0c77734 DB |
4999 | fprintf(opt->file, "%c%03d%c", p->status, similarity_index(p), |
5000 | inter_name_termination); | |
663af342 | 5001 | } else { |
c0c77734 | 5002 | fprintf(opt->file, "%c%c", p->status, inter_name_termination); |
6973dcae | 5003 | } |
6973dcae | 5004 | |
cd676a51 JH |
5005 | if (p->status == DIFF_STATUS_COPIED || |
5006 | p->status == DIFF_STATUS_RENAMED) { | |
5007 | const char *name_a, *name_b; | |
5008 | name_a = p->one->path; | |
5009 | name_b = p->two->path; | |
5010 | strip_prefix(opt->prefix_length, &name_a, &name_b); | |
c0c77734 DB |
5011 | write_name_quoted(name_a, opt->file, inter_name_termination); |
5012 | write_name_quoted(name_b, opt->file, line_termination); | |
663af342 | 5013 | } else { |
cd676a51 JH |
5014 | const char *name_a, *name_b; |
5015 | name_a = p->one->mode ? p->one->path : p->two->path; | |
5016 | name_b = NULL; | |
5017 | strip_prefix(opt->prefix_length, &name_a, &name_b); | |
c0c77734 | 5018 | write_name_quoted(name_a, opt->file, line_termination); |
663af342 | 5019 | } |
6973dcae JH |
5020 | } |
5021 | ||
5022 | int diff_unmodified_pair(struct diff_filepair *p) | |
5023 | { | |
5024 | /* This function is written stricter than necessary to support | |
5025 | * the currently implemented transformers, but the idea is to | |
5026 | * let transformers to produce diff_filepairs any way they want, | |
5027 | * and filter and clean them up here before producing the output. | |
5028 | */ | |
663af342 | 5029 | struct diff_filespec *one = p->one, *two = p->two; |
6973dcae JH |
5030 | |
5031 | if (DIFF_PAIR_UNMERGED(p)) | |
5032 | return 0; /* unmerged is interesting */ | |
5033 | ||
6973dcae JH |
5034 | /* deletion, addition, mode or type change |
5035 | * and rename are all interesting. | |
5036 | */ | |
5037 | if (DIFF_FILE_VALID(one) != DIFF_FILE_VALID(two) || | |
5038 | DIFF_PAIR_MODE_CHANGED(p) || | |
5039 | strcmp(one->path, two->path)) | |
5040 | return 0; | |
5041 | ||
5042 | /* both are valid and point at the same path. that is, we are | |
5043 | * dealing with a change. | |
5044 | */ | |
41c9560e | 5045 | if (one->oid_valid && two->oid_valid && |
a0d12c44 | 5046 | !oidcmp(&one->oid, &two->oid) && |
85adbf2f | 5047 | !one->dirty_submodule && !two->dirty_submodule) |
6973dcae | 5048 | return 1; /* no change */ |
41c9560e | 5049 | if (!one->oid_valid && !two->oid_valid) |
6973dcae JH |
5050 | return 1; /* both look at the same file on the filesystem. */ |
5051 | return 0; | |
5052 | } | |
5053 | ||
5054 | static void diff_flush_patch(struct diff_filepair *p, struct diff_options *o) | |
5055 | { | |
5056 | if (diff_unmodified_pair(p)) | |
5057 | return; | |
5058 | ||
5059 | if ((DIFF_FILE_VALID(p->one) && S_ISDIR(p->one->mode)) || | |
5060 | (DIFF_FILE_VALID(p->two) && S_ISDIR(p->two->mode))) | |
5061 | return; /* no tree diffs in patch format */ | |
5062 | ||
5063 | run_diff(p, o); | |
5064 | } | |
5065 | ||
5066 | static void diff_flush_stat(struct diff_filepair *p, struct diff_options *o, | |
5067 | struct diffstat_t *diffstat) | |
5068 | { | |
5069 | if (diff_unmodified_pair(p)) | |
5070 | return; | |
5071 | ||
5072 | if ((DIFF_FILE_VALID(p->one) && S_ISDIR(p->one->mode)) || | |
5073 | (DIFF_FILE_VALID(p->two) && S_ISDIR(p->two->mode))) | |
c3fced64 | 5074 | return; /* no useful stat for tree diffs */ |
6973dcae JH |
5075 | |
5076 | run_diffstat(p, o, diffstat); | |
5077 | } | |
5078 | ||
88246898 JS |
5079 | static void diff_flush_checkdiff(struct diff_filepair *p, |
5080 | struct diff_options *o) | |
5081 | { | |
5082 | if (diff_unmodified_pair(p)) | |
5083 | return; | |
5084 | ||
5085 | if ((DIFF_FILE_VALID(p->one) && S_ISDIR(p->one->mode)) || | |
5086 | (DIFF_FILE_VALID(p->two) && S_ISDIR(p->two->mode))) | |
c3fced64 | 5087 | return; /* nothing to check in tree diffs */ |
88246898 JS |
5088 | |
5089 | run_checkdiff(p, o); | |
5090 | } | |
5091 | ||
6973dcae JH |
5092 | int diff_queue_is_empty(void) |
5093 | { | |
5094 | struct diff_queue_struct *q = &diff_queued_diff; | |
5095 | int i; | |
5096 | for (i = 0; i < q->nr; i++) | |
5097 | if (!diff_unmodified_pair(q->queue[i])) | |
5098 | return 0; | |
5099 | return 1; | |
5100 | } | |
5101 | ||
5102 | #if DIFF_DEBUG | |
5103 | void diff_debug_filespec(struct diff_filespec *s, int x, const char *one) | |
5104 | { | |
5105 | fprintf(stderr, "queue[%d] %s (%s) %s %06o %s\n", | |
5106 | x, one ? one : "", | |
5107 | s->path, | |
5108 | DIFF_FILE_VALID(s) ? "valid" : "invalid", | |
5109 | s->mode, | |
41c9560e | 5110 | s->oid_valid ? oid_to_hex(&s->oid) : ""); |
428d52a5 | 5111 | fprintf(stderr, "queue[%d] %s size %lu\n", |
6973dcae | 5112 | x, one ? one : "", |
428d52a5 | 5113 | s->size); |
6973dcae JH |
5114 | } |
5115 | ||
5116 | void diff_debug_filepair(const struct diff_filepair *p, int i) | |
5117 | { | |
5118 | diff_debug_filespec(p->one, i, "one"); | |
5119 | diff_debug_filespec(p->two, i, "two"); | |
64479711 | 5120 | fprintf(stderr, "score %d, status %c rename_used %d broken %d\n", |
6973dcae | 5121 | p->score, p->status ? p->status : '?', |
64479711 | 5122 | p->one->rename_used, p->broken_pair); |
6973dcae JH |
5123 | } |
5124 | ||
5125 | void diff_debug_queue(const char *msg, struct diff_queue_struct *q) | |
5126 | { | |
5127 | int i; | |
5128 | if (msg) | |
5129 | fprintf(stderr, "%s\n", msg); | |
5130 | fprintf(stderr, "q->nr = %d\n", q->nr); | |
5131 | for (i = 0; i < q->nr; i++) { | |
5132 | struct diff_filepair *p = q->queue[i]; | |
5133 | diff_debug_filepair(p, i); | |
5134 | } | |
5135 | } | |
5136 | #endif | |
5137 | ||
5138 | static void diff_resolve_rename_copy(void) | |
5139 | { | |
64479711 LT |
5140 | int i; |
5141 | struct diff_filepair *p; | |
6973dcae JH |
5142 | struct diff_queue_struct *q = &diff_queued_diff; |
5143 | ||
5144 | diff_debug_queue("resolve-rename-copy", q); | |
5145 | ||
5146 | for (i = 0; i < q->nr; i++) { | |
5147 | p = q->queue[i]; | |
5148 | p->status = 0; /* undecided */ | |
5149 | if (DIFF_PAIR_UNMERGED(p)) | |
5150 | p->status = DIFF_STATUS_UNMERGED; | |
5151 | else if (!DIFF_FILE_VALID(p->one)) | |
5152 | p->status = DIFF_STATUS_ADDED; | |
5153 | else if (!DIFF_FILE_VALID(p->two)) | |
5154 | p->status = DIFF_STATUS_DELETED; | |
5155 | else if (DIFF_PAIR_TYPE_CHANGED(p)) | |
5156 | p->status = DIFF_STATUS_TYPE_CHANGED; | |
5157 | ||
5158 | /* from this point on, we are dealing with a pair | |
5159 | * whose both sides are valid and of the same type, i.e. | |
5160 | * either in-place edit or rename/copy edit. | |
5161 | */ | |
5162 | else if (DIFF_PAIR_RENAME(p)) { | |
64479711 LT |
5163 | /* |
5164 | * A rename might have re-connected a broken | |
5165 | * pair up, causing the pathnames to be the | |
5166 | * same again. If so, that's not a rename at | |
5167 | * all, just a modification.. | |
5168 | * | |
5169 | * Otherwise, see if this source was used for | |
5170 | * multiple renames, in which case we decrement | |
5171 | * the count, and call it a copy. | |
6973dcae | 5172 | */ |
64479711 LT |
5173 | if (!strcmp(p->one->path, p->two->path)) |
5174 | p->status = DIFF_STATUS_MODIFIED; | |
5175 | else if (--p->one->rename_used > 0) | |
6973dcae | 5176 | p->status = DIFF_STATUS_COPIED; |
64479711 | 5177 | else |
6973dcae JH |
5178 | p->status = DIFF_STATUS_RENAMED; |
5179 | } | |
a0d12c44 | 5180 | else if (oidcmp(&p->one->oid, &p->two->oid) || |
d516c2d1 | 5181 | p->one->mode != p->two->mode || |
85adbf2f JL |
5182 | p->one->dirty_submodule || |
5183 | p->two->dirty_submodule || | |
a0d12c44 | 5184 | is_null_oid(&p->one->oid)) |
6973dcae JH |
5185 | p->status = DIFF_STATUS_MODIFIED; |
5186 | else { | |
5187 | /* This is a "no-change" entry and should not | |
5188 | * happen anymore, but prepare for broken callers. | |
5189 | */ | |
5190 | error("feeding unmodified %s to diffcore", | |
5191 | p->one->path); | |
5192 | p->status = DIFF_STATUS_UNKNOWN; | |
5193 | } | |
5194 | } | |
5195 | diff_debug_queue("resolve-rename-copy done", q); | |
5196 | } | |
5197 | ||
c6744349 | 5198 | static int check_pair_status(struct diff_filepair *p) |
6973dcae | 5199 | { |
6973dcae JH |
5200 | switch (p->status) { |
5201 | case DIFF_STATUS_UNKNOWN: | |
c6744349 | 5202 | return 0; |
6973dcae JH |
5203 | case 0: |
5204 | die("internal error in diff-resolve-rename-copy"); | |
6973dcae | 5205 | default: |
c6744349 | 5206 | return 1; |
6973dcae JH |
5207 | } |
5208 | } | |
5209 | ||
c6744349 TH |
5210 | static void flush_one_pair(struct diff_filepair *p, struct diff_options *opt) |
5211 | { | |
5212 | int fmt = opt->output_format; | |
5213 | ||
5214 | if (fmt & DIFF_FORMAT_CHECKDIFF) | |
5215 | diff_flush_checkdiff(p, opt); | |
5216 | else if (fmt & (DIFF_FORMAT_RAW | DIFF_FORMAT_NAME_STATUS)) | |
5217 | diff_flush_raw(p, opt); | |
cd676a51 JH |
5218 | else if (fmt & DIFF_FORMAT_NAME) { |
5219 | const char *name_a, *name_b; | |
5220 | name_a = p->two->path; | |
5221 | name_b = NULL; | |
5222 | strip_prefix(opt->prefix_length, &name_a, &name_b); | |
f5022b5f | 5223 | fprintf(opt->file, "%s", diff_line_prefix(opt)); |
c0c77734 | 5224 | write_name_quoted(name_a, opt->file, opt->line_termination); |
cd676a51 | 5225 | } |
c6744349 TH |
5226 | } |
5227 | ||
146fdb0d | 5228 | static void show_file_mode_name(struct diff_options *opt, const char *newdelete, struct diff_filespec *fs) |
4bbd261b | 5229 | { |
146fdb0d | 5230 | struct strbuf sb = STRBUF_INIT; |
4bbd261b | 5231 | if (fs->mode) |
146fdb0d | 5232 | strbuf_addf(&sb, " %s mode %06o ", newdelete, fs->mode); |
4bbd261b | 5233 | else |
146fdb0d | 5234 | strbuf_addf(&sb, " %s ", newdelete); |
4bbd261b | 5235 | |
146fdb0d SB |
5236 | quote_c_style(fs->path, &sb, NULL, 0); |
5237 | strbuf_addch(&sb, '\n'); | |
5238 | emit_diff_symbol(opt, DIFF_SYMBOL_SUMMARY, | |
5239 | sb.buf, sb.len, 0); | |
5240 | strbuf_release(&sb); | |
5241 | } | |
4bbd261b | 5242 | |
146fdb0d SB |
5243 | static void show_mode_change(struct diff_options *opt, struct diff_filepair *p, |
5244 | int show_name) | |
4bbd261b SE |
5245 | { |
5246 | if (p->one->mode && p->two->mode && p->one->mode != p->two->mode) { | |
146fdb0d SB |
5247 | struct strbuf sb = STRBUF_INIT; |
5248 | strbuf_addf(&sb, " mode change %06o => %06o", | |
5249 | p->one->mode, p->two->mode); | |
0d26a64e | 5250 | if (show_name) { |
146fdb0d SB |
5251 | strbuf_addch(&sb, ' '); |
5252 | quote_c_style(p->two->path, &sb, NULL, 0); | |
0d26a64e | 5253 | } |
58aaced4 | 5254 | strbuf_addch(&sb, '\n'); |
146fdb0d SB |
5255 | emit_diff_symbol(opt, DIFF_SYMBOL_SUMMARY, |
5256 | sb.buf, sb.len, 0); | |
5257 | strbuf_release(&sb); | |
4bbd261b SE |
5258 | } |
5259 | } | |
5260 | ||
146fdb0d SB |
5261 | static void show_rename_copy(struct diff_options *opt, const char *renamecopy, |
5262 | struct diff_filepair *p) | |
4bbd261b | 5263 | { |
146fdb0d | 5264 | struct strbuf sb = STRBUF_INIT; |
c905cbc4 NTND |
5265 | struct strbuf names = STRBUF_INIT; |
5266 | ||
5267 | pprint_rename(&names, p->one->path, p->two->path); | |
146fdb0d | 5268 | strbuf_addf(&sb, " %s %s (%d%%)\n", |
c905cbc4 NTND |
5269 | renamecopy, names.buf, similarity_index(p)); |
5270 | strbuf_release(&names); | |
146fdb0d SB |
5271 | emit_diff_symbol(opt, DIFF_SYMBOL_SUMMARY, |
5272 | sb.buf, sb.len, 0); | |
5273 | show_mode_change(opt, p, 0); | |
348eda24 | 5274 | strbuf_release(&sb); |
4bbd261b SE |
5275 | } |
5276 | ||
7be57610 | 5277 | static void diff_summary(struct diff_options *opt, struct diff_filepair *p) |
4bbd261b SE |
5278 | { |
5279 | switch(p->status) { | |
5280 | case DIFF_STATUS_DELETED: | |
146fdb0d | 5281 | show_file_mode_name(opt, "delete", p->one); |
4bbd261b SE |
5282 | break; |
5283 | case DIFF_STATUS_ADDED: | |
146fdb0d | 5284 | show_file_mode_name(opt, "create", p->two); |
4bbd261b SE |
5285 | break; |
5286 | case DIFF_STATUS_COPIED: | |
146fdb0d | 5287 | show_rename_copy(opt, "copy", p); |
4bbd261b SE |
5288 | break; |
5289 | case DIFF_STATUS_RENAMED: | |
146fdb0d | 5290 | show_rename_copy(opt, "rename", p); |
4bbd261b SE |
5291 | break; |
5292 | default: | |
5293 | if (p->score) { | |
146fdb0d SB |
5294 | struct strbuf sb = STRBUF_INIT; |
5295 | strbuf_addstr(&sb, " rewrite "); | |
5296 | quote_c_style(p->two->path, &sb, NULL, 0); | |
5297 | strbuf_addf(&sb, " (%d%%)\n", similarity_index(p)); | |
5298 | emit_diff_symbol(opt, DIFF_SYMBOL_SUMMARY, | |
5299 | sb.buf, sb.len, 0); | |
fa842d84 | 5300 | strbuf_release(&sb); |
663af342 | 5301 | } |
146fdb0d | 5302 | show_mode_change(opt, p, !p->score); |
4bbd261b SE |
5303 | break; |
5304 | } | |
5305 | } | |
5306 | ||
fcb3d0ad | 5307 | struct patch_id_t { |
9126f009 | 5308 | git_SHA_CTX *ctx; |
fcb3d0ad JS |
5309 | int patchlen; |
5310 | }; | |
5311 | ||
5312 | static int remove_space(char *line, int len) | |
5313 | { | |
5314 | int i; | |
663af342 PH |
5315 | char *dst = line; |
5316 | unsigned char c; | |
fcb3d0ad | 5317 | |
663af342 PH |
5318 | for (i = 0; i < len; i++) |
5319 | if (!isspace((c = line[i]))) | |
5320 | *dst++ = c; | |
fcb3d0ad | 5321 | |
663af342 | 5322 | return dst - line; |
fcb3d0ad JS |
5323 | } |
5324 | ||
5325 | static void patch_id_consume(void *priv, char *line, unsigned long len) | |
5326 | { | |
5327 | struct patch_id_t *data = priv; | |
5328 | int new_len; | |
5329 | ||
5330 | /* Ignore line numbers when computing the SHA1 of the patch */ | |
59556548 | 5331 | if (starts_with(line, "@@ -")) |
fcb3d0ad JS |
5332 | return; |
5333 | ||
5334 | new_len = remove_space(line, len); | |
5335 | ||
9126f009 | 5336 | git_SHA1_Update(data->ctx, line, new_len); |
fcb3d0ad JS |
5337 | data->patchlen += new_len; |
5338 | } | |
5339 | ||
977db6b4 JK |
5340 | static void patch_id_add_string(git_SHA_CTX *ctx, const char *str) |
5341 | { | |
5342 | git_SHA1_Update(ctx, str, strlen(str)); | |
5343 | } | |
5344 | ||
5345 | static void patch_id_add_mode(git_SHA_CTX *ctx, unsigned mode) | |
5346 | { | |
5347 | /* large enough for 2^32 in octal */ | |
5348 | char buf[12]; | |
5349 | int len = xsnprintf(buf, sizeof(buf), "%06o", mode); | |
5350 | git_SHA1_Update(ctx, buf, len); | |
5351 | } | |
5352 | ||
fcb3d0ad | 5353 | /* returns 0 upon success, and writes result into sha1 */ |
bd25f288 | 5354 | static int diff_get_patch_id(struct diff_options *options, struct object_id *oid, int diff_header_only) |
fcb3d0ad JS |
5355 | { |
5356 | struct diff_queue_struct *q = &diff_queued_diff; | |
5357 | int i; | |
9126f009 | 5358 | git_SHA_CTX ctx; |
fcb3d0ad | 5359 | struct patch_id_t data; |
fcb3d0ad | 5360 | |
9126f009 | 5361 | git_SHA1_Init(&ctx); |
fcb3d0ad JS |
5362 | memset(&data, 0, sizeof(struct patch_id_t)); |
5363 | data.ctx = &ctx; | |
fcb3d0ad JS |
5364 | |
5365 | for (i = 0; i < q->nr; i++) { | |
5366 | xpparam_t xpp; | |
5367 | xdemitconf_t xecfg; | |
fcb3d0ad JS |
5368 | mmfile_t mf1, mf2; |
5369 | struct diff_filepair *p = q->queue[i]; | |
5370 | int len1, len2; | |
5371 | ||
9ccd0a88 | 5372 | memset(&xpp, 0, sizeof(xpp)); |
30b25010 | 5373 | memset(&xecfg, 0, sizeof(xecfg)); |
fcb3d0ad JS |
5374 | if (p->status == 0) |
5375 | return error("internal diff status error"); | |
5376 | if (p->status == DIFF_STATUS_UNKNOWN) | |
5377 | continue; | |
5378 | if (diff_unmodified_pair(p)) | |
5379 | continue; | |
5380 | if ((DIFF_FILE_VALID(p->one) && S_ISDIR(p->one->mode)) || | |
5381 | (DIFF_FILE_VALID(p->two) && S_ISDIR(p->two->mode))) | |
5382 | continue; | |
5383 | if (DIFF_PAIR_UNMERGED(p)) | |
5384 | continue; | |
5385 | ||
94e327e9 BW |
5386 | diff_fill_oid_info(p->one); |
5387 | diff_fill_oid_info(p->two); | |
fcb3d0ad | 5388 | |
fcb3d0ad JS |
5389 | len1 = remove_space(p->one->path, strlen(p->one->path)); |
5390 | len2 = remove_space(p->two->path, strlen(p->two->path)); | |
977db6b4 JK |
5391 | patch_id_add_string(&ctx, "diff--git"); |
5392 | patch_id_add_string(&ctx, "a/"); | |
5393 | git_SHA1_Update(&ctx, p->one->path, len1); | |
5394 | patch_id_add_string(&ctx, "b/"); | |
5395 | git_SHA1_Update(&ctx, p->two->path, len2); | |
5396 | ||
5397 | if (p->one->mode == 0) { | |
5398 | patch_id_add_string(&ctx, "newfilemode"); | |
5399 | patch_id_add_mode(&ctx, p->two->mode); | |
5400 | patch_id_add_string(&ctx, "---/dev/null"); | |
5401 | patch_id_add_string(&ctx, "+++b/"); | |
5402 | git_SHA1_Update(&ctx, p->two->path, len2); | |
5403 | } else if (p->two->mode == 0) { | |
5404 | patch_id_add_string(&ctx, "deletedfilemode"); | |
5405 | patch_id_add_mode(&ctx, p->one->mode); | |
5406 | patch_id_add_string(&ctx, "---a/"); | |
5407 | git_SHA1_Update(&ctx, p->one->path, len1); | |
5408 | patch_id_add_string(&ctx, "+++/dev/null"); | |
5409 | } else { | |
5410 | patch_id_add_string(&ctx, "---a/"); | |
5411 | git_SHA1_Update(&ctx, p->one->path, len1); | |
5412 | patch_id_add_string(&ctx, "+++b/"); | |
5413 | git_SHA1_Update(&ctx, p->two->path, len2); | |
5414 | } | |
fcb3d0ad | 5415 | |
3e8e32c3 KW |
5416 | if (diff_header_only) |
5417 | continue; | |
5418 | ||
5419 | if (fill_mmfile(&mf1, p->one) < 0 || | |
5420 | fill_mmfile(&mf2, p->two) < 0) | |
5421 | return error("unable to read files to diff"); | |
5422 | ||
34597c1f CB |
5423 | if (diff_filespec_is_binary(p->one) || |
5424 | diff_filespec_is_binary(p->two)) { | |
a0d12c44 | 5425 | git_SHA1_Update(&ctx, oid_to_hex(&p->one->oid), |
bd25f288 | 5426 | GIT_SHA1_HEXSZ); |
a0d12c44 | 5427 | git_SHA1_Update(&ctx, oid_to_hex(&p->two->oid), |
bd25f288 | 5428 | GIT_SHA1_HEXSZ); |
34597c1f CB |
5429 | continue; |
5430 | } | |
5431 | ||
582aa00b | 5432 | xpp.flags = 0; |
fcb3d0ad | 5433 | xecfg.ctxlen = 3; |
ad14b450 | 5434 | xecfg.flags = 0; |
3efb9880 JK |
5435 | if (xdi_diff_outf(&mf1, &mf2, patch_id_consume, &data, |
5436 | &xpp, &xecfg)) | |
5437 | return error("unable to generate patch-id diff for %s", | |
5438 | p->one->path); | |
fcb3d0ad JS |
5439 | } |
5440 | ||
bd25f288 | 5441 | git_SHA1_Final(oid->hash, &ctx); |
fcb3d0ad JS |
5442 | return 0; |
5443 | } | |
5444 | ||
bd25f288 | 5445 | int diff_flush_patch_id(struct diff_options *options, struct object_id *oid, int diff_header_only) |
fcb3d0ad JS |
5446 | { |
5447 | struct diff_queue_struct *q = &diff_queued_diff; | |
5448 | int i; | |
bd25f288 | 5449 | int result = diff_get_patch_id(options, oid, diff_header_only); |
fcb3d0ad JS |
5450 | |
5451 | for (i = 0; i < q->nr; i++) | |
5452 | diff_free_filepair(q->queue[i]); | |
5453 | ||
5454 | free(q->queue); | |
9ca5df90 | 5455 | DIFF_QUEUE_CLEAR(q); |
fcb3d0ad JS |
5456 | |
5457 | return result; | |
5458 | } | |
5459 | ||
946c3784 | 5460 | static int is_summary_empty(const struct diff_queue_struct *q) |
6973dcae | 5461 | { |
6973dcae | 5462 | int i; |
6973dcae | 5463 | |
946c3784 TH |
5464 | for (i = 0; i < q->nr; i++) { |
5465 | const struct diff_filepair *p = q->queue[i]; | |
5466 | ||
5467 | switch (p->status) { | |
5468 | case DIFF_STATUS_DELETED: | |
5469 | case DIFF_STATUS_ADDED: | |
5470 | case DIFF_STATUS_COPIED: | |
5471 | case DIFF_STATUS_RENAMED: | |
5472 | return 0; | |
5473 | default: | |
5474 | if (p->score) | |
5475 | return 0; | |
5476 | if (p->one->mode && p->two->mode && | |
5477 | p->one->mode != p->two->mode) | |
5478 | return 0; | |
5479 | break; | |
5480 | } | |
6973dcae | 5481 | } |
946c3784 TH |
5482 | return 1; |
5483 | } | |
5484 | ||
f31027c9 | 5485 | static const char rename_limit_warning[] = |
db424979 | 5486 | N_("inexact rename detection was skipped due to too many files."); |
f31027c9 JH |
5487 | |
5488 | static const char degrade_cc_to_c_warning[] = | |
db424979 | 5489 | N_("only found copies from modified paths due to too many files."); |
f31027c9 JH |
5490 | |
5491 | static const char rename_limit_advice[] = | |
db424979 VA |
5492 | N_("you may want to set your %s variable to at least " |
5493 | "%d and retry the command."); | |
f31027c9 JH |
5494 | |
5495 | void diff_warn_rename_limit(const char *varname, int needed, int degraded_cc) | |
5496 | { | |
4e056c98 | 5497 | fflush(stdout); |
f31027c9 | 5498 | if (degraded_cc) |
db424979 | 5499 | warning(_(degrade_cc_to_c_warning)); |
f31027c9 | 5500 | else if (needed) |
db424979 | 5501 | warning(_(rename_limit_warning)); |
f31027c9 JH |
5502 | else |
5503 | return; | |
9f7e4bfa | 5504 | if (0 < needed) |
db424979 | 5505 | warning(_(rename_limit_advice), varname, needed); |
f31027c9 JH |
5506 | } |
5507 | ||
ec331506 SB |
5508 | static void diff_flush_patch_all_file_pairs(struct diff_options *o) |
5509 | { | |
5510 | int i; | |
e6e045f8 | 5511 | static struct emitted_diff_symbols esm = EMITTED_DIFF_SYMBOLS_INIT; |
ec331506 | 5512 | struct diff_queue_struct *q = &diff_queued_diff; |
091f8e28 SB |
5513 | |
5514 | if (WSEH_NEW & WS_RULE_MASK) | |
5515 | die("BUG: WS rules bit mask overlaps with diff symbol flags"); | |
5516 | ||
2e2d5ac1 SB |
5517 | if (o->color_moved) |
5518 | o->emitted_symbols = &esm; | |
e6e045f8 | 5519 | |
ec331506 SB |
5520 | for (i = 0; i < q->nr; i++) { |
5521 | struct diff_filepair *p = q->queue[i]; | |
5522 | if (check_pair_status(p)) | |
5523 | diff_flush_patch(p, o); | |
5524 | } | |
e6e045f8 SB |
5525 | |
5526 | if (o->emitted_symbols) { | |
2e2d5ac1 SB |
5527 | if (o->color_moved) { |
5528 | struct hashmap add_lines, del_lines; | |
5529 | ||
5530 | hashmap_init(&del_lines, | |
5531 | (hashmap_cmp_fn)moved_entry_cmp, o, 0); | |
5532 | hashmap_init(&add_lines, | |
5533 | (hashmap_cmp_fn)moved_entry_cmp, o, 0); | |
5534 | ||
5535 | add_lines_to_move_detection(o, &add_lines, &del_lines); | |
5536 | mark_color_as_moved(o, &add_lines, &del_lines); | |
86b452e2 SB |
5537 | if (o->color_moved == COLOR_MOVED_ZEBRA_DIM) |
5538 | dim_moved_lines(o); | |
2e2d5ac1 SB |
5539 | |
5540 | hashmap_free(&add_lines, 0); | |
5541 | hashmap_free(&del_lines, 0); | |
5542 | } | |
5543 | ||
e6e045f8 SB |
5544 | for (i = 0; i < esm.nr; i++) |
5545 | emit_diff_symbol_from_struct(o, &esm.buf[i]); | |
5546 | ||
5547 | for (i = 0; i < esm.nr; i++) | |
5548 | free((void *)esm.buf[i].line); | |
5549 | } | |
5550 | esm.nr = 0; | |
ec331506 SB |
5551 | } |
5552 | ||
6973dcae JH |
5553 | void diff_flush(struct diff_options *options) |
5554 | { | |
5555 | struct diff_queue_struct *q = &diff_queued_diff; | |
c6744349 | 5556 | int i, output_format = options->output_format; |
946c3784 | 5557 | int separator = 0; |
1c57a627 | 5558 | int dirstat_by_line = 0; |
6973dcae | 5559 | |
c6744349 TH |
5560 | /* |
5561 | * Order: raw, stat, summary, patch | |
5562 | * or: name/name-status/checkdiff (other bits clear) | |
5563 | */ | |
946c3784 TH |
5564 | if (!q->nr) |
5565 | goto free_queue; | |
6973dcae | 5566 | |
c6744349 TH |
5567 | if (output_format & (DIFF_FORMAT_RAW | |
5568 | DIFF_FORMAT_NAME | | |
5569 | DIFF_FORMAT_NAME_STATUS | | |
5570 | DIFF_FORMAT_CHECKDIFF)) { | |
6973dcae JH |
5571 | for (i = 0; i < q->nr; i++) { |
5572 | struct diff_filepair *p = q->queue[i]; | |
c6744349 TH |
5573 | if (check_pair_status(p)) |
5574 | flush_one_pair(p, options); | |
6973dcae | 5575 | } |
946c3784 | 5576 | separator++; |
6973dcae | 5577 | } |
c6744349 | 5578 | |
0d1e0e78 | 5579 | if (output_format & DIFF_FORMAT_DIRSTAT && options->flags.dirstat_by_line) |
1c57a627 JH |
5580 | dirstat_by_line = 1; |
5581 | ||
5582 | if (output_format & (DIFF_FORMAT_DIFFSTAT|DIFF_FORMAT_SHORTSTAT|DIFF_FORMAT_NUMSTAT) || | |
5583 | dirstat_by_line) { | |
5e2b0636 | 5584 | struct diffstat_t diffstat; |
c6744349 | 5585 | |
5e2b0636 | 5586 | memset(&diffstat, 0, sizeof(struct diffstat_t)); |
6973dcae JH |
5587 | for (i = 0; i < q->nr; i++) { |
5588 | struct diff_filepair *p = q->queue[i]; | |
c6744349 | 5589 | if (check_pair_status(p)) |
5e2b0636 | 5590 | diff_flush_stat(p, options, &diffstat); |
6973dcae | 5591 | } |
74e2abe5 JH |
5592 | if (output_format & DIFF_FORMAT_NUMSTAT) |
5593 | show_numstat(&diffstat, options); | |
5594 | if (output_format & DIFF_FORMAT_DIFFSTAT) | |
5595 | show_stats(&diffstat, options); | |
f604652e | 5596 | if (output_format & DIFF_FORMAT_SHORTSTAT) |
c0c77734 | 5597 | show_shortstats(&diffstat, options); |
ab27389a | 5598 | if (output_format & DIFF_FORMAT_DIRSTAT && dirstat_by_line) |
1c57a627 | 5599 | show_dirstat_by_line(&diffstat, options); |
f604652e | 5600 | free_diffstat_info(&diffstat); |
3969cf7d | 5601 | separator++; |
6973dcae | 5602 | } |
1c57a627 | 5603 | if ((output_format & DIFF_FORMAT_DIRSTAT) && !dirstat_by_line) |
c04a7155 | 5604 | show_dirstat(options); |
6973dcae | 5605 | |
946c3784 | 5606 | if (output_format & DIFF_FORMAT_SUMMARY && !is_summary_empty(q)) { |
7be57610 BY |
5607 | for (i = 0; i < q->nr; i++) { |
5608 | diff_summary(options, q->queue[i]); | |
5609 | } | |
3969cf7d | 5610 | separator++; |
6973dcae JH |
5611 | } |
5612 | ||
6977c250 | 5613 | if (output_format & DIFF_FORMAT_NO_OUTPUT && |
0d1e0e78 BW |
5614 | options->flags.exit_with_status && |
5615 | options->flags.diff_from_contents) { | |
6977c250 LA |
5616 | /* |
5617 | * run diff_flush_patch for the exit status. setting | |
749f763d | 5618 | * options->file to /dev/null should be safe, because we |
6977c250 LA |
5619 | * aren't supposed to produce any output anyway. |
5620 | */ | |
5621 | if (options->close_file) | |
5622 | fclose(options->file); | |
23a9e071 | 5623 | options->file = xfopen("/dev/null", "w"); |
6977c250 | 5624 | options->close_file = 1; |
2e2d5ac1 | 5625 | options->color_moved = 0; |
6977c250 LA |
5626 | for (i = 0; i < q->nr; i++) { |
5627 | struct diff_filepair *p = q->queue[i]; | |
5628 | if (check_pair_status(p)) | |
5629 | diff_flush_patch(p, options); | |
5630 | if (options->found_changes) | |
5631 | break; | |
5632 | } | |
5633 | } | |
5634 | ||
c6744349 | 5635 | if (output_format & DIFF_FORMAT_PATCH) { |
946c3784 | 5636 | if (separator) { |
091f8e28 | 5637 | emit_diff_symbol(options, DIFF_SYMBOL_SEPARATOR, NULL, 0, 0); |
30b7e1e7 | 5638 | if (options->stat_sep) |
946c3784 | 5639 | /* attach patch instead of inline */ |
30b7e1e7 SB |
5640 | emit_diff_symbol(options, DIFF_SYMBOL_STAT_SEP, |
5641 | NULL, 0, 0); | |
c6744349 TH |
5642 | } |
5643 | ||
ec331506 | 5644 | diff_flush_patch_all_file_pairs(options); |
4bbd261b SE |
5645 | } |
5646 | ||
04245581 JK |
5647 | if (output_format & DIFF_FORMAT_CALLBACK) |
5648 | options->format_callback(q, options, options->format_callback_data); | |
5649 | ||
c6744349 TH |
5650 | for (i = 0; i < q->nr; i++) |
5651 | diff_free_filepair(q->queue[i]); | |
946c3784 | 5652 | free_queue: |
6973dcae | 5653 | free(q->queue); |
9ca5df90 | 5654 | DIFF_QUEUE_CLEAR(q); |
c0c77734 DB |
5655 | if (options->close_file) |
5656 | fclose(options->file); | |
f245194f JH |
5657 | |
5658 | /* | |
97bf2a08 | 5659 | * Report the content-level differences with HAS_CHANGES; |
f245194f JH |
5660 | * diff_addremove/diff_change does not set the bit when |
5661 | * DIFF_FROM_CONTENTS is in effect (e.g. with -w). | |
5662 | */ | |
0d1e0e78 | 5663 | if (options->flags.diff_from_contents) { |
f245194f | 5664 | if (options->found_changes) |
0d1e0e78 | 5665 | options->flags.has_changes = 1; |
f245194f | 5666 | else |
0d1e0e78 | 5667 | options->flags.has_changes = 0; |
f245194f | 5668 | } |
6973dcae JH |
5669 | } |
5670 | ||
08578fa1 JH |
5671 | static int match_filter(const struct diff_options *options, const struct diff_filepair *p) |
5672 | { | |
5673 | return (((p->status == DIFF_STATUS_MODIFIED) && | |
5674 | ((p->score && | |
1ecc1cbd | 5675 | filter_bit_tst(DIFF_STATUS_FILTER_BROKEN, options)) || |
08578fa1 | 5676 | (!p->score && |
1ecc1cbd | 5677 | filter_bit_tst(DIFF_STATUS_MODIFIED, options)))) || |
08578fa1 | 5678 | ((p->status != DIFF_STATUS_MODIFIED) && |
1ecc1cbd | 5679 | filter_bit_tst(p->status, options))); |
08578fa1 JH |
5680 | } |
5681 | ||
949226fe | 5682 | static void diffcore_apply_filter(struct diff_options *options) |
6973dcae JH |
5683 | { |
5684 | int i; | |
5685 | struct diff_queue_struct *q = &diff_queued_diff; | |
5686 | struct diff_queue_struct outq; | |
949226fe | 5687 | |
9ca5df90 | 5688 | DIFF_QUEUE_CLEAR(&outq); |
6973dcae | 5689 | |
1ecc1cbd | 5690 | if (!options->filter) |
6973dcae JH |
5691 | return; |
5692 | ||
1ecc1cbd | 5693 | if (filter_bit_tst(DIFF_STATUS_FILTER_AON, options)) { |
6973dcae JH |
5694 | int found; |
5695 | for (i = found = 0; !found && i < q->nr; i++) { | |
08578fa1 | 5696 | if (match_filter(options, q->queue[i])) |
6973dcae JH |
5697 | found++; |
5698 | } | |
5699 | if (found) | |
5700 | return; | |
5701 | ||
5702 | /* otherwise we will clear the whole queue | |
5703 | * by copying the empty outq at the end of this | |
5704 | * function, but first clear the current entries | |
5705 | * in the queue. | |
5706 | */ | |
5707 | for (i = 0; i < q->nr; i++) | |
5708 | diff_free_filepair(q->queue[i]); | |
5709 | } | |
5710 | else { | |
5711 | /* Only the matching ones */ | |
5712 | for (i = 0; i < q->nr; i++) { | |
5713 | struct diff_filepair *p = q->queue[i]; | |
08578fa1 | 5714 | if (match_filter(options, p)) |
6973dcae JH |
5715 | diff_q(&outq, p); |
5716 | else | |
5717 | diff_free_filepair(p); | |
5718 | } | |
5719 | } | |
5720 | free(q->queue); | |
5721 | *q = outq; | |
5722 | } | |
5723 | ||
5701115a SV |
5724 | /* Check whether two filespecs with the same mode and size are identical */ |
5725 | static int diff_filespec_is_identical(struct diff_filespec *one, | |
5726 | struct diff_filespec *two) | |
5727 | { | |
2b459b48 JH |
5728 | if (S_ISGITLINK(one->mode)) |
5729 | return 0; | |
5701115a SV |
5730 | if (diff_populate_filespec(one, 0)) |
5731 | return 0; | |
5732 | if (diff_populate_filespec(two, 0)) | |
5733 | return 0; | |
5734 | return !memcmp(one->data, two->data, one->size); | |
5735 | } | |
5736 | ||
fceb9072 NTND |
5737 | static int diff_filespec_check_stat_unmatch(struct diff_filepair *p) |
5738 | { | |
f34b205f NTND |
5739 | if (p->done_skip_stat_unmatch) |
5740 | return p->skip_stat_unmatch_result; | |
5741 | ||
5742 | p->done_skip_stat_unmatch = 1; | |
5743 | p->skip_stat_unmatch_result = 0; | |
fceb9072 NTND |
5744 | /* |
5745 | * 1. Entries that come from stat info dirtiness | |
5746 | * always have both sides (iow, not create/delete), | |
5747 | * one side of the object name is unknown, with | |
5748 | * the same mode and size. Keep the ones that | |
5749 | * do not match these criteria. They have real | |
5750 | * differences. | |
5751 | * | |
5752 | * 2. At this point, the file is known to be modified, | |
5753 | * with the same mode and size, and the object | |
5754 | * name of one side is unknown. Need to inspect | |
5755 | * the identical contents. | |
5756 | */ | |
5757 | if (!DIFF_FILE_VALID(p->one) || /* (1) */ | |
5758 | !DIFF_FILE_VALID(p->two) || | |
41c9560e | 5759 | (p->one->oid_valid && p->two->oid_valid) || |
fceb9072 | 5760 | (p->one->mode != p->two->mode) || |
8e5dd3d6 NTND |
5761 | diff_populate_filespec(p->one, CHECK_SIZE_ONLY) || |
5762 | diff_populate_filespec(p->two, CHECK_SIZE_ONLY) || | |
fceb9072 NTND |
5763 | (p->one->size != p->two->size) || |
5764 | !diff_filespec_is_identical(p->one, p->two)) /* (2) */ | |
f34b205f NTND |
5765 | p->skip_stat_unmatch_result = 1; |
5766 | return p->skip_stat_unmatch_result; | |
fceb9072 NTND |
5767 | } |
5768 | ||
fb13227e JH |
5769 | static void diffcore_skip_stat_unmatch(struct diff_options *diffopt) |
5770 | { | |
5771 | int i; | |
5772 | struct diff_queue_struct *q = &diff_queued_diff; | |
5773 | struct diff_queue_struct outq; | |
9ca5df90 | 5774 | DIFF_QUEUE_CLEAR(&outq); |
fb13227e JH |
5775 | |
5776 | for (i = 0; i < q->nr; i++) { | |
5777 | struct diff_filepair *p = q->queue[i]; | |
5778 | ||
fceb9072 | 5779 | if (diff_filespec_check_stat_unmatch(p)) |
fb13227e JH |
5780 | diff_q(&outq, p); |
5781 | else { | |
5782 | /* | |
5783 | * The caller can subtract 1 from skip_stat_unmatch | |
5784 | * to determine how many paths were dirty only | |
5785 | * due to stat info mismatch. | |
5786 | */ | |
0d1e0e78 | 5787 | if (!diffopt->flags.no_index) |
6d2d9e86 | 5788 | diffopt->skip_stat_unmatch++; |
fb13227e JH |
5789 | diff_free_filepair(p); |
5790 | } | |
5791 | } | |
5792 | free(q->queue); | |
5793 | *q = outq; | |
5794 | } | |
5795 | ||
730f7284 JH |
5796 | static int diffnamecmp(const void *a_, const void *b_) |
5797 | { | |
5798 | const struct diff_filepair *a = *((const struct diff_filepair **)a_); | |
5799 | const struct diff_filepair *b = *((const struct diff_filepair **)b_); | |
5800 | const char *name_a, *name_b; | |
5801 | ||
5802 | name_a = a->one ? a->one->path : a->two->path; | |
5803 | name_b = b->one ? b->one->path : b->two->path; | |
5804 | return strcmp(name_a, name_b); | |
5805 | } | |
5806 | ||
5807 | void diffcore_fix_diff_index(struct diff_options *options) | |
5808 | { | |
5809 | struct diff_queue_struct *q = &diff_queued_diff; | |
9ed0d8d6 | 5810 | QSORT(q->queue, q->nr, diffnamecmp); |
730f7284 JH |
5811 | } |
5812 | ||
6973dcae JH |
5813 | void diffcore_std(struct diff_options *options) |
5814 | { | |
7195fbfa | 5815 | /* NOTE please keep the following in sync with diff_tree_combined() */ |
9d865356 | 5816 | if (options->skip_stat_unmatch) |
fb13227e | 5817 | diffcore_skip_stat_unmatch(options); |
44c48a90 JH |
5818 | if (!options->found_follow) { |
5819 | /* See try_to_follow_renames() in tree-diff.c */ | |
5820 | if (options->break_opt != -1) | |
5821 | diffcore_break(options->break_opt); | |
5822 | if (options->detect_rename) | |
5823 | diffcore_rename(options); | |
5824 | if (options->break_opt != -1) | |
5825 | diffcore_merge_broken(); | |
5826 | } | |
cf63051a | 5827 | if (options->pickaxe_opts & DIFF_PICKAXE_KINDS_MASK) |
382f013b | 5828 | diffcore_pickaxe(options); |
6973dcae JH |
5829 | if (options->orderfile) |
5830 | diffcore_order(options->orderfile); | |
44c48a90 JH |
5831 | if (!options->found_follow) |
5832 | /* See try_to_follow_renames() in tree-diff.c */ | |
5833 | diff_resolve_rename_copy(); | |
949226fe | 5834 | diffcore_apply_filter(options); |
68aacb2f | 5835 | |
0d1e0e78 BW |
5836 | if (diff_queued_diff.nr && !options->flags.diff_from_contents) |
5837 | options->flags.has_changes = 1; | |
8f67f8ae | 5838 | else |
0d1e0e78 | 5839 | options->flags.has_changes = 0; |
1da6175d | 5840 | |
44c48a90 | 5841 | options->found_follow = 0; |
6973dcae JH |
5842 | } |
5843 | ||
da31b358 JH |
5844 | int diff_result_code(struct diff_options *opt, int status) |
5845 | { | |
5846 | int result = 0; | |
f31027c9 | 5847 | |
c9fc4415 | 5848 | diff_warn_rename_limit("diff.renameLimit", |
f31027c9 JH |
5849 | opt->needed_rename_limit, |
5850 | opt->degraded_cc_to_c); | |
0d1e0e78 | 5851 | if (!opt->flags.exit_with_status && |
da31b358 JH |
5852 | !(opt->output_format & DIFF_FORMAT_CHECKDIFF)) |
5853 | return status; | |
0d1e0e78 BW |
5854 | if (opt->flags.exit_with_status && |
5855 | opt->flags.has_changes) | |
da31b358 JH |
5856 | result |= 01; |
5857 | if ((opt->output_format & DIFF_FORMAT_CHECKDIFF) && | |
0d1e0e78 | 5858 | opt->flags.check_failed) |
da31b358 JH |
5859 | result |= 02; |
5860 | return result; | |
5861 | } | |
6973dcae | 5862 | |
28b9264d JH |
5863 | int diff_can_quit_early(struct diff_options *opt) |
5864 | { | |
0d1e0e78 | 5865 | return (opt->flags.quick && |
28b9264d | 5866 | !opt->filter && |
0d1e0e78 | 5867 | opt->flags.has_changes); |
28b9264d JH |
5868 | } |
5869 | ||
aee9c7d6 JL |
5870 | /* |
5871 | * Shall changes to this submodule be ignored? | |
5872 | * | |
5873 | * Submodule changes can be configured to be ignored separately for each path, | |
5874 | * but that configuration can be overridden from the command line. | |
5875 | */ | |
5876 | static int is_submodule_ignored(const char *path, struct diff_options *options) | |
5877 | { | |
5878 | int ignored = 0; | |
02f2f56b | 5879 | struct diff_flags orig_flags = options->flags; |
0d1e0e78 | 5880 | if (!options->flags.override_submodule_config) |
aee9c7d6 | 5881 | set_diffopt_flags_from_submodule_config(options, path); |
0d1e0e78 | 5882 | if (options->flags.ignore_submodules) |
aee9c7d6 JL |
5883 | ignored = 1; |
5884 | options->flags = orig_flags; | |
5885 | return ignored; | |
5886 | } | |
5887 | ||
6973dcae JH |
5888 | void diff_addremove(struct diff_options *options, |
5889 | int addremove, unsigned mode, | |
c26022ea BW |
5890 | const struct object_id *oid, |
5891 | int oid_valid, | |
e3d42c47 | 5892 | const char *concatpath, unsigned dirty_submodule) |
6973dcae | 5893 | { |
6973dcae JH |
5894 | struct diff_filespec *one, *two; |
5895 | ||
aee9c7d6 | 5896 | if (S_ISGITLINK(mode) && is_submodule_ignored(concatpath, options)) |
50fd9bd8 JS |
5897 | return; |
5898 | ||
6973dcae JH |
5899 | /* This may look odd, but it is a preparation for |
5900 | * feeding "there are unchanged files which should | |
5901 | * not produce diffs, but when you are doing copy | |
5902 | * detection you would need them, so here they are" | |
5903 | * entries to the diff-core. They will be prefixed | |
5904 | * with something like '=' or '*' (I haven't decided | |
5905 | * which but should not make any difference). | |
a6080a0a | 5906 | * Feeding the same new and old to diff_change() |
6973dcae JH |
5907 | * also has the same effect. |
5908 | * Before the final output happens, they are pruned after | |
5909 | * merged into rename/copy pairs as appropriate. | |
5910 | */ | |
0d1e0e78 | 5911 | if (options->flags.reverse_diff) |
6973dcae JH |
5912 | addremove = (addremove == '+' ? '-' : |
5913 | addremove == '-' ? '+' : addremove); | |
5914 | ||
cd676a51 JH |
5915 | if (options->prefix && |
5916 | strncmp(concatpath, options->prefix, options->prefix_length)) | |
5917 | return; | |
5918 | ||
6973dcae JH |
5919 | one = alloc_filespec(concatpath); |
5920 | two = alloc_filespec(concatpath); | |
5921 | ||
5922 | if (addremove != '+') | |
f9704c2d | 5923 | fill_filespec(one, oid, oid_valid, mode); |
e3d42c47 | 5924 | if (addremove != '-') { |
f9704c2d | 5925 | fill_filespec(two, oid, oid_valid, mode); |
e3d42c47 JL |
5926 | two->dirty_submodule = dirty_submodule; |
5927 | } | |
6973dcae JH |
5928 | |
5929 | diff_queue(&diff_queued_diff, one, two); | |
0d1e0e78 BW |
5930 | if (!options->flags.diff_from_contents) |
5931 | options->flags.has_changes = 1; | |
6973dcae JH |
5932 | } |
5933 | ||
5934 | void diff_change(struct diff_options *options, | |
5935 | unsigned old_mode, unsigned new_mode, | |
94a0097a BW |
5936 | const struct object_id *old_oid, |
5937 | const struct object_id *new_oid, | |
5938 | int old_oid_valid, int new_oid_valid, | |
e3d42c47 JL |
5939 | const char *concatpath, |
5940 | unsigned old_dirty_submodule, unsigned new_dirty_submodule) | |
6973dcae | 5941 | { |
6973dcae | 5942 | struct diff_filespec *one, *two; |
f34b205f | 5943 | struct diff_filepair *p; |
6973dcae | 5944 | |
aee9c7d6 JL |
5945 | if (S_ISGITLINK(old_mode) && S_ISGITLINK(new_mode) && |
5946 | is_submodule_ignored(concatpath, options)) | |
50fd9bd8 JS |
5947 | return; |
5948 | ||
0d1e0e78 | 5949 | if (options->flags.reverse_diff) { |
35d803bc | 5950 | SWAP(old_mode, new_mode); |
94a0097a BW |
5951 | SWAP(old_oid, new_oid); |
5952 | SWAP(old_oid_valid, new_oid_valid); | |
35d803bc | 5953 | SWAP(old_dirty_submodule, new_dirty_submodule); |
6973dcae | 5954 | } |
cd676a51 JH |
5955 | |
5956 | if (options->prefix && | |
5957 | strncmp(concatpath, options->prefix, options->prefix_length)) | |
5958 | return; | |
5959 | ||
6973dcae JH |
5960 | one = alloc_filespec(concatpath); |
5961 | two = alloc_filespec(concatpath); | |
f9704c2d BW |
5962 | fill_filespec(one, old_oid, old_oid_valid, old_mode); |
5963 | fill_filespec(two, new_oid, new_oid_valid, new_mode); | |
e3d42c47 JL |
5964 | one->dirty_submodule = old_dirty_submodule; |
5965 | two->dirty_submodule = new_dirty_submodule; | |
f34b205f | 5966 | p = diff_queue(&diff_queued_diff, one, two); |
6973dcae | 5967 | |
0d1e0e78 | 5968 | if (options->flags.diff_from_contents) |
f34b205f NTND |
5969 | return; |
5970 | ||
0d1e0e78 | 5971 | if (options->flags.quick && options->skip_stat_unmatch && |
f34b205f NTND |
5972 | !diff_filespec_check_stat_unmatch(p)) |
5973 | return; | |
5974 | ||
0d1e0e78 | 5975 | options->flags.has_changes = 1; |
6973dcae JH |
5976 | } |
5977 | ||
fa7b2908 | 5978 | struct diff_filepair *diff_unmerge(struct diff_options *options, const char *path) |
6973dcae | 5979 | { |
76399c01 | 5980 | struct diff_filepair *pair; |
6973dcae | 5981 | struct diff_filespec *one, *two; |
cd676a51 JH |
5982 | |
5983 | if (options->prefix && | |
5984 | strncmp(path, options->prefix, options->prefix_length)) | |
76399c01 | 5985 | return NULL; |
cd676a51 | 5986 | |
6973dcae JH |
5987 | one = alloc_filespec(path); |
5988 | two = alloc_filespec(path); | |
76399c01 JH |
5989 | pair = diff_queue(&diff_queued_diff, one, two); |
5990 | pair->is_unmerged = 1; | |
5991 | return pair; | |
6973dcae | 5992 | } |
9cb92c39 JK |
5993 | |
5994 | static char *run_textconv(const char *pgm, struct diff_filespec *spec, | |
5995 | size_t *outsize) | |
5996 | { | |
479b0ae8 | 5997 | struct diff_tempfile *temp; |
9cb92c39 JK |
5998 | const char *argv[3]; |
5999 | const char **arg = argv; | |
d3180279 | 6000 | struct child_process child = CHILD_PROCESS_INIT; |
9cb92c39 | 6001 | struct strbuf buf = STRBUF_INIT; |
da1fbed3 | 6002 | int err = 0; |
9cb92c39 | 6003 | |
479b0ae8 | 6004 | temp = prepare_temp_file(spec->path, spec); |
9cb92c39 | 6005 | *arg++ = pgm; |
479b0ae8 | 6006 | *arg++ = temp->name; |
9cb92c39 JK |
6007 | *arg = NULL; |
6008 | ||
41a457e4 | 6009 | child.use_shell = 1; |
9cb92c39 JK |
6010 | child.argv = argv; |
6011 | child.out = -1; | |
da1fbed3 | 6012 | if (start_command(&child)) { |
479b0ae8 | 6013 | remove_tempfile(); |
9cb92c39 JK |
6014 | return NULL; |
6015 | } | |
da1fbed3 JS |
6016 | |
6017 | if (strbuf_read(&buf, child.out, 0) < 0) | |
6018 | err = error("error reading from textconv command '%s'", pgm); | |
70d70999 | 6019 | close(child.out); |
da1fbed3 JS |
6020 | |
6021 | if (finish_command(&child) || err) { | |
6022 | strbuf_release(&buf); | |
6023 | remove_tempfile(); | |
6024 | return NULL; | |
6025 | } | |
479b0ae8 | 6026 | remove_tempfile(); |
9cb92c39 JK |
6027 | |
6028 | return strbuf_detach(&buf, outsize); | |
6029 | } | |
840383b2 | 6030 | |
a788d7d5 AB |
6031 | size_t fill_textconv(struct userdiff_driver *driver, |
6032 | struct diff_filespec *df, | |
6033 | char **outbuf) | |
840383b2 JK |
6034 | { |
6035 | size_t size; | |
6036 | ||
a64e6a44 | 6037 | if (!driver) { |
840383b2 JK |
6038 | if (!DIFF_FILE_VALID(df)) { |
6039 | *outbuf = ""; | |
6040 | return 0; | |
6041 | } | |
6042 | if (diff_populate_filespec(df, 0)) | |
6043 | die("unable to read files to diff"); | |
6044 | *outbuf = df->data; | |
6045 | return df->size; | |
6046 | } | |
6047 | ||
a64e6a44 JK |
6048 | if (!driver->textconv) |
6049 | die("BUG: fill_textconv called with non-textconv driver"); | |
6050 | ||
41c9560e | 6051 | if (driver->textconv_cache && df->oid_valid) { |
a0d12c44 | 6052 | *outbuf = notes_cache_get(driver->textconv_cache, |
569aa376 | 6053 | &df->oid, |
d9bae1a1 JK |
6054 | &size); |
6055 | if (*outbuf) | |
6056 | return size; | |
6057 | } | |
6058 | ||
6059 | *outbuf = run_textconv(driver->textconv, df, &size); | |
840383b2 JK |
6060 | if (!*outbuf) |
6061 | die("unable to read files to diff"); | |
d9bae1a1 | 6062 | |
41c9560e | 6063 | if (driver->textconv_cache && df->oid_valid) { |
d9bae1a1 | 6064 | /* ignore errors, as we might be in a readonly repository */ |
569aa376 | 6065 | notes_cache_put(driver->textconv_cache, &df->oid, *outbuf, |
d9bae1a1 JK |
6066 | size); |
6067 | /* | |
6068 | * we could save up changes and flush them all at the end, | |
6069 | * but we would need an extra call after all diffing is done. | |
6070 | * Since generating a cache entry is the slow path anyway, | |
6071 | * this extra overhead probably isn't a big deal. | |
6072 | */ | |
6073 | notes_cache_write(driver->textconv_cache); | |
6074 | } | |
6075 | ||
840383b2 JK |
6076 | return size; |
6077 | } | |
4914c962 | 6078 | |
3a35cb2e JS |
6079 | int textconv_object(const char *path, |
6080 | unsigned mode, | |
6081 | const struct object_id *oid, | |
6082 | int oid_valid, | |
6083 | char **buf, | |
6084 | unsigned long *buf_size) | |
6085 | { | |
6086 | struct diff_filespec *df; | |
6087 | struct userdiff_driver *textconv; | |
6088 | ||
6089 | df = alloc_filespec(path); | |
a6f38c10 | 6090 | fill_filespec(df, oid, oid_valid, mode); |
3a35cb2e JS |
6091 | textconv = get_textconv(df); |
6092 | if (!textconv) { | |
6093 | free_filespec(df); | |
6094 | return 0; | |
6095 | } | |
6096 | ||
6097 | *buf_size = fill_textconv(textconv, df, buf); | |
6098 | free_filespec(df); | |
6099 | return 1; | |
6100 | } | |
6101 | ||
4914c962 NTND |
6102 | void setup_diff_pager(struct diff_options *opt) |
6103 | { | |
6104 | /* | |
6105 | * If the user asked for our exit code, then either they want --quiet | |
6106 | * or --exit-code. We should definitely not bother with a pager in the | |
6107 | * former case, as we will generate no output. Since we still properly | |
6108 | * report our exit code even when a pager is run, we _could_ run a | |
6109 | * pager with --exit-code. But since we have not done so historically, | |
6110 | * and because it is easy to find people oneline advising "git diff | |
6111 | * --exit-code" in hooks and other scripts, we do not do so. | |
6112 | */ | |
0d1e0e78 | 6113 | if (!opt->flags.exit_with_status && |
4914c962 NTND |
6114 | check_pager_config("diff") != 0) |
6115 | setup_pager(); | |
6116 | } |