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