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