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