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