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