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