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