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