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