blob: c8f530ffdbea8dbd724aff61c7a0aed4e83e5af6 [file] [log] [blame]
Junio C Hamano6973dca2006-04-21 23:57:45 -07001/*
2 * Copyright (C) 2005 Junio C Hamano
3 */
Junio C Hamano6973dca2006-04-21 23:57:45 -07004#include "cache.h"
Brandon Williamsb2141fc2017-06-14 11:07:36 -07005#include "config.h"
Michael Haggerty284098f2015-08-12 19:12:01 +02006#include "tempfile.h"
Junio C Hamano6973dca2006-04-21 23:57:45 -07007#include "quote.h"
8#include "diff.h"
9#include "diffcore.h"
Junio C Hamano051308f2006-05-04 16:51:44 -070010#include "delta.h"
Junio C Hamano6973dca2006-04-21 23:57:45 -070011#include "xdiff-interface.h"
Jeff King7c92fe02006-09-08 04:03:18 -040012#include "color.h"
Junio C Hamano8c701242007-04-12 23:05:29 -070013#include "attr.h"
Johannes Sixtd5535ec2007-10-19 21:47:56 +020014#include "run-command.h"
Junio C Hamano23707812008-01-02 01:50:11 -080015#include "utf8.h"
Stefan Bellercbd53a22018-05-15 16:42:15 -070016#include "object-store.h"
Jeff Kingbe58e702008-10-05 17:43:21 -040017#include "userdiff.h"
Heiko Voigt851e18c2015-08-17 17:21:59 -070018#include "submodule-config.h"
Johannes Schindelin752c0c22009-10-19 14:38:32 +020019#include "submodule.h"
Stefan Beller2e2d5ac2017-06-30 13:53:07 -070020#include "hashmap.h"
Junio C Hamanoa757c642010-03-24 19:21:32 -070021#include "ll-merge.h"
Michael Haggerty02e8ca02012-10-28 17:50:54 +010022#include "string-list.h"
Jeff Kingdbbcd442020-07-28 16:23:39 -040023#include "strvec.h"
Jacob Keller660e1132016-08-31 16:27:20 -070024#include "graph.h"
Jonathan Tan150e3002017-08-18 15:20:36 -070025#include "packfile.h"
Nguyễn Thái Ngọc Duy4a288472019-01-27 07:35:31 +070026#include "parse-options.h"
Nguyễn Thái Ngọc Duy3ac68a92018-05-26 15:55:24 +020027#include "help.h"
Christian Couderb14ed5a2019-06-25 15:40:31 +020028#include "promisor-remote.h"
Derrick Stoleead90da72021-09-08 11:23:56 +000029#include "dir.h"
Junio C Hamano6973dca2006-04-21 23:57:45 -070030
Shawn O. Pearce1510fea2006-12-14 06:15:57 -050031#ifdef NO_FAST_WORKING_DIRECTORY
32#define FAST_WORKING_DIRECTORY 0
33#else
34#define FAST_WORKING_DIRECTORY 1
35#endif
36
David Rientjes96f1e582006-08-15 10:23:48 -070037static int diff_detect_rename_default;
Stefan Beller33de7162017-05-08 12:03:38 -040038static int diff_indent_heuristic = 1;
Elijah Newren94b82d52021-07-15 00:45:24 +000039static int diff_rename_limit_default = 1000;
Jim Meyeringa624eaa2008-08-15 13:39:26 +020040static int diff_suppress_blank_empty;
Junio C Hamanod2aea132012-09-15 13:59:59 -070041static int diff_use_color_default = -1;
Stefan Beller2e2d5ac2017-06-30 13:53:07 -070042static int diff_color_moved_default;
Stefan Beller626c0b52018-07-18 12:31:56 -070043static int diff_color_moved_ws_default;
Jeff Muizelaar6468a4e2012-09-27 15:12:52 -040044static int diff_context_default = 3;
Vegard Nossumc4888672017-01-12 13:21:11 +010045static int diff_interhunk_context_default;
Boyd Stephen Smith Jr98a4d872009-01-20 21:46:57 -060046static const char *diff_word_regex_cfg;
Johannes Schindelincbe02102007-12-17 13:42:20 +000047static const char *external_diff_cmd_cfg;
Samuel Bronson6d8940b2013-12-18 19:08:12 -050048static const char *diff_order_file_cfg;
Junio C Hamanoaecbf912007-08-31 13:13:42 -070049int diff_auto_refresh_index = 1;
Junio C Hamanoa5a818e2008-08-18 20:08:09 -070050static int diff_mnemonic_prefix;
Eli Collinsf89504d2010-05-02 19:03:41 -070051static int diff_no_prefix;
Laurent Arnoudc28ded82020-05-22 12:46:18 +020052static int diff_relative;
Zbigniew Jędrzejewski-Szmekdf444832012-03-01 13:26:46 +010053static int diff_stat_graph_width;
Johan Herland712d2c72011-04-29 11:36:20 +020054static int diff_dirstat_permille_default = 30;
Johannes Schindelinbe4f2b42010-08-05 10:49:55 +020055static struct diff_options default_diff_options;
Michal Privoznik07ab4de2013-01-16 08:51:57 +010056static long diff_algorithm;
Junio C Hamanoa17505f2016-10-04 15:26:27 -070057static unsigned ws_error_highlight_default = WSEH_NEW;
Junio C Hamano6973dca2006-04-21 23:57:45 -070058
Jeff King7c92fe02006-09-08 04:03:18 -040059static char diff_colors[][COLOR_MAXLEN] = {
Arjen Laarhovendc6ebd42009-02-13 22:53:40 +010060 GIT_COLOR_RESET,
Jeff King8dbf3eb2015-05-27 16:48:46 -040061 GIT_COLOR_NORMAL, /* CONTEXT */
Arjen Laarhovendc6ebd42009-02-13 22:53:40 +010062 GIT_COLOR_BOLD, /* METAINFO */
63 GIT_COLOR_CYAN, /* FRAGINFO */
64 GIT_COLOR_RED, /* OLD */
65 GIT_COLOR_GREEN, /* NEW */
66 GIT_COLOR_YELLOW, /* COMMIT */
67 GIT_COLOR_BG_RED, /* WHITESPACE */
Bert Wesarg89cb73a2009-11-27 07:55:18 +010068 GIT_COLOR_NORMAL, /* FUNCINFO */
Stefan Beller86b452e2017-06-30 13:53:09 -070069 GIT_COLOR_BOLD_MAGENTA, /* OLD_MOVED */
70 GIT_COLOR_BOLD_BLUE, /* OLD_MOVED ALTERNATIVE */
71 GIT_COLOR_FAINT, /* OLD_MOVED_DIM */
72 GIT_COLOR_FAINT_ITALIC, /* OLD_MOVED_ALTERNATIVE_DIM */
73 GIT_COLOR_BOLD_CYAN, /* NEW_MOVED */
74 GIT_COLOR_BOLD_YELLOW, /* NEW_MOVED ALTERNATIVE */
75 GIT_COLOR_FAINT, /* NEW_MOVED_DIM */
76 GIT_COLOR_FAINT_ITALIC, /* NEW_MOVED_ALTERNATIVE_DIM */
Johannes Schindelina7be92a2018-08-13 04:33:32 -070077 GIT_COLOR_FAINT, /* CONTEXT_DIM */
78 GIT_COLOR_FAINT_RED, /* OLD_DIM */
79 GIT_COLOR_FAINT_GREEN, /* NEW_DIM */
80 GIT_COLOR_BOLD, /* CONTEXT_BOLD */
81 GIT_COLOR_BOLD_RED, /* OLD_BOLD */
82 GIT_COLOR_BOLD_GREEN, /* NEW_BOLD */
Johannes Schindelincd112ce2006-06-13 18:45:44 +020083};
84
Nguyễn Thái Ngọc Duya73b3682018-05-26 15:55:21 +020085static const char *color_diff_slots[] = {
86 [DIFF_CONTEXT] = "context",
87 [DIFF_METAINFO] = "meta",
88 [DIFF_FRAGINFO] = "frag",
89 [DIFF_FILE_OLD] = "old",
90 [DIFF_FILE_NEW] = "new",
91 [DIFF_COMMIT] = "commit",
92 [DIFF_WHITESPACE] = "whitespace",
93 [DIFF_FUNCINFO] = "func",
94 [DIFF_FILE_OLD_MOVED] = "oldMoved",
95 [DIFF_FILE_OLD_MOVED_ALT] = "oldMovedAlternative",
96 [DIFF_FILE_OLD_MOVED_DIM] = "oldMovedDimmed",
97 [DIFF_FILE_OLD_MOVED_ALT_DIM] = "oldMovedAlternativeDimmed",
98 [DIFF_FILE_NEW_MOVED] = "newMoved",
99 [DIFF_FILE_NEW_MOVED_ALT] = "newMovedAlternative",
100 [DIFF_FILE_NEW_MOVED_DIM] = "newMovedDimmed",
101 [DIFF_FILE_NEW_MOVED_ALT_DIM] = "newMovedAlternativeDimmed",
Johannes Schindelina7be92a2018-08-13 04:33:32 -0700102 [DIFF_CONTEXT_DIM] = "contextDimmed",
103 [DIFF_FILE_OLD_DIM] = "oldDimmed",
104 [DIFF_FILE_NEW_DIM] = "newDimmed",
105 [DIFF_CONTEXT_BOLD] = "contextBold",
106 [DIFF_FILE_OLD_BOLD] = "oldBold",
107 [DIFF_FILE_NEW_BOLD] = "newBold",
Nguyễn Thái Ngọc Duya73b3682018-05-26 15:55:21 +0200108};
109
Nguyễn Thái Ngọc Duy3ac68a92018-05-26 15:55:24 +0200110define_list_config_array_extra(color_diff_slots, {"plain"});
111
Jeff King9e1a5eb2014-06-18 15:41:50 -0400112static int parse_diff_color_slot(const char *var)
Junio C Hamano801235c2006-06-24 04:06:23 -0700113{
Nguyễn Thái Ngọc Duya73b3682018-05-26 15:55:21 +0200114 if (!strcasecmp(var, "plain"))
Jeff King8dbf3eb2015-05-27 16:48:46 -0400115 return DIFF_CONTEXT;
Nguyễn Thái Ngọc Duya73b3682018-05-26 15:55:21 +0200116 return LOOKUP_CONFIG(color_diff_slots, var);
Junio C Hamano801235c2006-06-24 04:06:23 -0700117}
118
Michael Haggerty02e8ca02012-10-28 17:50:54 +0100119static int parse_dirstat_params(struct diff_options *options, const char *params_string,
Johan Herland51670fc2011-04-29 11:36:22 +0200120 struct strbuf *errmsg)
Johan Herland333f3fb2011-04-29 11:36:18 +0200121{
Michael Haggerty02e8ca02012-10-28 17:50:54 +0100122 char *params_copy = xstrdup(params_string);
123 struct string_list params = STRING_LIST_INIT_NODUP;
124 int ret = 0;
125 int i;
Johan Herland51670fc2011-04-29 11:36:22 +0200126
Michael Haggerty02e8ca02012-10-28 17:50:54 +0100127 if (*params_copy)
128 string_list_split_in_place(&params, params_copy, ',', -1);
129 for (i = 0; i < params.nr; i++) {
130 const char *p = params.items[i].string;
131 if (!strcmp(p, "changes")) {
Brandon Williams0d1e0e72017-10-31 11:19:11 -0700132 options->flags.dirstat_by_line = 0;
133 options->flags.dirstat_by_file = 0;
Michael Haggerty02e8ca02012-10-28 17:50:54 +0100134 } else if (!strcmp(p, "lines")) {
Brandon Williams0d1e0e72017-10-31 11:19:11 -0700135 options->flags.dirstat_by_line = 1;
136 options->flags.dirstat_by_file = 0;
Michael Haggerty02e8ca02012-10-28 17:50:54 +0100137 } else if (!strcmp(p, "files")) {
Brandon Williams0d1e0e72017-10-31 11:19:11 -0700138 options->flags.dirstat_by_line = 0;
139 options->flags.dirstat_by_file = 1;
Michael Haggerty02e8ca02012-10-28 17:50:54 +0100140 } else if (!strcmp(p, "noncumulative")) {
Brandon Williams0d1e0e72017-10-31 11:19:11 -0700141 options->flags.dirstat_cumulative = 0;
Michael Haggerty02e8ca02012-10-28 17:50:54 +0100142 } else if (!strcmp(p, "cumulative")) {
Brandon Williams0d1e0e72017-10-31 11:19:11 -0700143 options->flags.dirstat_cumulative = 1;
Johan Herland333f3fb2011-04-29 11:36:18 +0200144 } else if (isdigit(*p)) {
145 char *end;
Johan Herland51670fc2011-04-29 11:36:22 +0200146 int permille = strtoul(p, &end, 10) * 10;
147 if (*end == '.' && isdigit(*++end)) {
Johan Herland712d2c72011-04-29 11:36:20 +0200148 /* only use first digit */
Johan Herland51670fc2011-04-29 11:36:22 +0200149 permille += *end - '0';
Johan Herland712d2c72011-04-29 11:36:20 +0200150 /* .. and ignore any further digits */
Johan Herland51670fc2011-04-29 11:36:22 +0200151 while (isdigit(*++end))
Johan Herland712d2c72011-04-29 11:36:20 +0200152 ; /* nothing */
153 }
Michael Haggerty02e8ca02012-10-28 17:50:54 +0100154 if (!*end)
Johan Herland51670fc2011-04-29 11:36:22 +0200155 options->dirstat_permille = permille;
156 else {
Michael Haggerty02e8ca02012-10-28 17:50:54 +0100157 strbuf_addf(errmsg, _(" Failed to parse dirstat cut-off percentage '%s'\n"),
158 p);
Johan Herland51670fc2011-04-29 11:36:22 +0200159 ret++;
160 }
161 } else {
Michael Haggerty02e8ca02012-10-28 17:50:54 +0100162 strbuf_addf(errmsg, _(" Unknown dirstat parameter '%s'\n"), p);
Johan Herland51670fc2011-04-29 11:36:22 +0200163 ret++;
Johan Herland333f3fb2011-04-29 11:36:18 +0200164 }
Johan Herland51670fc2011-04-29 11:36:22 +0200165
Johan Herland333f3fb2011-04-29 11:36:18 +0200166 }
Michael Haggerty02e8ca02012-10-28 17:50:54 +0100167 string_list_clear(&params, 0);
168 free(params_copy);
Johan Herland51670fc2011-04-29 11:36:22 +0200169 return ret;
Johan Herland333f3fb2011-04-29 11:36:18 +0200170}
171
Ramkumar Ramachandrac47ef572012-11-13 21:12:45 +0530172static int parse_submodule_params(struct diff_options *options, const char *value)
173{
174 if (!strcmp(value, "log"))
Jacob Keller61cfbc02016-08-31 16:27:21 -0700175 options->submodule_format = DIFF_SUBMODULE_LOG;
Ramkumar Ramachandrac47ef572012-11-13 21:12:45 +0530176 else if (!strcmp(value, "short"))
Jacob Keller61cfbc02016-08-31 16:27:21 -0700177 options->submodule_format = DIFF_SUBMODULE_SHORT;
Jacob Kellerfd47ae62016-08-31 16:27:25 -0700178 else if (!strcmp(value, "diff"))
179 options->submodule_format = DIFF_SUBMODULE_INLINE_DIFF;
Nguyễn Thái Ngọc Duy5a59a232019-02-16 18:24:41 +0700180 /*
181 * Please update $__git_diff_submodule_formats in
182 * git-completion.bash when you add new formats.
183 */
Ramkumar Ramachandrac47ef572012-11-13 21:12:45 +0530184 else
185 return -1;
186 return 0;
187}
188
Ben Peart85b46032018-05-02 16:01:14 +0000189int git_config_rename(const char *var, const char *value)
Linus Torvaldscced5fb2009-04-09 11:46:15 -0700190{
191 if (!value)
192 return DIFF_DETECT_RENAME;
193 if (!strcasecmp(value, "copies") || !strcasecmp(value, "copy"))
194 return DIFF_DETECT_COPY;
195 return git_config_bool(var,value) ? DIFF_DETECT_RENAME : 0;
196}
197
Michal Privoznik07924d42013-01-16 08:51:58 +0100198long parse_algorithm_value(const char *value)
Michal Privoznik07ab4de2013-01-16 08:51:57 +0100199{
200 if (!value)
201 return -1;
202 else if (!strcasecmp(value, "myers") || !strcasecmp(value, "default"))
203 return 0;
204 else if (!strcasecmp(value, "minimal"))
205 return XDF_NEED_MINIMAL;
206 else if (!strcasecmp(value, "patience"))
207 return XDF_PATIENCE_DIFF;
208 else if (!strcasecmp(value, "histogram"))
209 return XDF_HISTOGRAM_DIFF;
Nguyễn Thái Ngọc Duy5a59a232019-02-16 18:24:41 +0700210 /*
211 * Please update $__git_diff_algorithms in git-completion.bash
212 * when you add new algorithms.
213 */
Michal Privoznik07ab4de2013-01-16 08:51:57 +0100214 return -1;
215}
216
Junio C Hamano0b4b42e2016-10-04 15:09:18 -0700217static int parse_one_token(const char **arg, const char *token)
218{
219 const char *rest;
220 if (skip_prefix(*arg, token, &rest) && (!*rest || *rest == ',')) {
221 *arg = rest;
222 return 1;
223 }
224 return 0;
225}
226
227static int parse_ws_error_highlight(const char *arg)
228{
229 const char *orig_arg = arg;
230 unsigned val = 0;
231
232 while (*arg) {
233 if (parse_one_token(&arg, "none"))
234 val = 0;
235 else if (parse_one_token(&arg, "default"))
236 val = WSEH_NEW;
237 else if (parse_one_token(&arg, "all"))
238 val = WSEH_NEW | WSEH_OLD | WSEH_CONTEXT;
239 else if (parse_one_token(&arg, "new"))
240 val |= WSEH_NEW;
241 else if (parse_one_token(&arg, "old"))
242 val |= WSEH_OLD;
243 else if (parse_one_token(&arg, "context"))
244 val |= WSEH_CONTEXT;
245 else {
246 return -1 - (int)(arg - orig_arg);
247 }
248 if (*arg)
249 arg++;
250 }
251 return val;
252}
253
Junio C Hamanoe0e324a2007-07-07 01:49:58 -0700254/*
Junio C Hamano83ad63c2006-07-08 01:05:16 -0700255 * These are to give UI layer defaults.
256 * The core-level commands such as git-diff-files should
257 * never be affected by the setting of diff.renames
258 * the user happens to have in the configuration file.
259 */
Matthieu Moy5404c112016-02-25 09:59:21 +0100260void init_diff_ui_defaults(void)
261{
Nguyễn Thái Ngọc Duy06dba2b2017-12-27 17:18:35 +0700262 diff_detect_rename_default = DIFF_DETECT_RENAME;
Matthieu Moy5404c112016-02-25 09:59:21 +0100263}
264
Michael Haggerty5b162872016-09-05 11:44:53 +0200265int git_diff_heuristic_config(const char *var, const char *value, void *cb)
266{
Junio C Hamano3cde4e02016-12-23 12:32:22 -0800267 if (!strcmp(var, "diff.indentheuristic"))
Michael Haggerty5b162872016-09-05 11:44:53 +0200268 diff_indent_heuristic = git_config_bool(var, value);
Michael Haggerty5b162872016-09-05 11:44:53 +0200269 return 0;
270}
271
Stefan Beller2e2d5ac2017-06-30 13:53:07 -0700272static int parse_color_moved(const char *arg)
273{
274 switch (git_parse_maybe_bool(arg)) {
275 case 0:
276 return COLOR_MOVED_NO;
277 case 1:
278 return COLOR_MOVED_DEFAULT;
279 default:
280 break;
281 }
282
283 if (!strcmp(arg, "no"))
284 return COLOR_MOVED_NO;
Stefan Beller176841f2017-06-30 13:53:08 -0700285 else if (!strcmp(arg, "plain"))
286 return COLOR_MOVED_PLAIN;
Stefan Beller51da15e2018-07-16 16:05:39 -0700287 else if (!strcmp(arg, "blocks"))
288 return COLOR_MOVED_BLOCKS;
Stefan Beller2e2d5ac2017-06-30 13:53:07 -0700289 else if (!strcmp(arg, "zebra"))
290 return COLOR_MOVED_ZEBRA;
291 else if (!strcmp(arg, "default"))
292 return COLOR_MOVED_DEFAULT;
Eric Sunshinee3f2f5f2018-07-24 17:58:45 -0400293 else if (!strcmp(arg, "dimmed-zebra"))
294 return COLOR_MOVED_ZEBRA_DIM;
Stefan Beller86b452e2017-06-30 13:53:09 -0700295 else if (!strcmp(arg, "dimmed_zebra"))
296 return COLOR_MOVED_ZEBRA_DIM;
Stefan Beller2e2d5ac2017-06-30 13:53:07 -0700297 else
Junio C Hamano706b0b52018-08-15 15:08:22 -0700298 return error(_("color moved setting must be one of 'no', 'default', 'blocks', 'zebra', 'dimmed-zebra', 'plain'"));
Stefan Beller2e2d5ac2017-06-30 13:53:07 -0700299}
300
Stefan Bellerd173e792018-11-13 13:33:57 -0800301static unsigned parse_color_moved_ws(const char *arg)
Stefan Bellerb3095712018-07-16 16:05:40 -0700302{
303 int ret = 0;
304 struct string_list l = STRING_LIST_INIT_DUP;
305 struct string_list_item *i;
306
307 string_list_split(&l, arg, ',', -1);
308
309 for_each_string_list_item(i, &l) {
310 struct strbuf sb = STRBUF_INIT;
311 strbuf_addstr(&sb, i->string);
312 strbuf_trim(&sb);
313
Phillip Woodb73bcba2018-11-23 11:16:52 +0000314 if (!strcmp(sb.buf, "no"))
315 ret = 0;
316 else if (!strcmp(sb.buf, "ignore-space-change"))
Stefan Bellerb3095712018-07-16 16:05:40 -0700317 ret |= XDF_IGNORE_WHITESPACE_CHANGE;
318 else if (!strcmp(sb.buf, "ignore-space-at-eol"))
319 ret |= XDF_IGNORE_WHITESPACE_AT_EOL;
320 else if (!strcmp(sb.buf, "ignore-all-space"))
321 ret |= XDF_IGNORE_WHITESPACE;
Stefan Bellerca1f4ae2018-07-18 12:31:55 -0700322 else if (!strcmp(sb.buf, "allow-indentation-change"))
323 ret |= COLOR_MOVED_WS_ALLOW_INDENTATION_CHANGE;
Stefan Bellerd173e792018-11-13 13:33:57 -0800324 else {
325 ret |= COLOR_MOVED_WS_ERROR;
326 error(_("unknown color-moved-ws mode '%s', possible values are 'ignore-space-change', 'ignore-space-at-eol', 'ignore-all-space', 'allow-indentation-change'"), sb.buf);
327 }
Stefan Bellerb3095712018-07-16 16:05:40 -0700328
329 strbuf_release(&sb);
330 }
331
Stefan Bellerca1f4ae2018-07-18 12:31:55 -0700332 if ((ret & COLOR_MOVED_WS_ALLOW_INDENTATION_CHANGE) &&
Stefan Bellerd173e792018-11-13 13:33:57 -0800333 (ret & XDF_WHITESPACE_FLAGS)) {
Junio C Hamano15b07cb2019-01-29 12:47:53 -0800334 error(_("color-moved-ws: allow-indentation-change cannot be combined with other whitespace modes"));
Stefan Bellerd173e792018-11-13 13:33:57 -0800335 ret |= COLOR_MOVED_WS_ERROR;
336 }
Stefan Bellerca1f4ae2018-07-18 12:31:55 -0700337
Stefan Bellerb3095712018-07-16 16:05:40 -0700338 string_list_clear(&l, 0);
339
340 return ret;
Jeff Kinge269eb72011-08-17 22:03:48 -0700341}
Junio C Hamano801235c2006-06-24 04:06:23 -0700342
343int git_diff_ui_config(const char *var, const char *value, void *cb)
Jeff Muizelaar6468a4e2012-09-27 15:12:52 -0400344{
345 if (!strcmp(var, "diff.color") || !strcmp(var, "color.diff")) {
346 diff_use_color_default = git_config_colorbool(var, value);
347 return 0;
348 }
Stefan Beller2e2d5ac2017-06-30 13:53:07 -0700349 if (!strcmp(var, "diff.colormoved")) {
350 int cm = parse_color_moved(value);
351 if (cm < 0)
352 return -1;
353 diff_color_moved_default = cm;
354 return 0;
355 }
Stefan Beller626c0b52018-07-18 12:31:56 -0700356 if (!strcmp(var, "diff.colormovedws")) {
Stefan Bellerd173e792018-11-13 13:33:57 -0800357 unsigned cm = parse_color_moved_ws(value);
358 if (cm & COLOR_MOVED_WS_ERROR)
Stefan Beller626c0b52018-07-18 12:31:56 -0700359 return -1;
360 diff_color_moved_ws_default = cm;
361 return 0;
362 }
Jeff Muizelaar6468a4e2012-09-27 15:12:52 -0400363 if (!strcmp(var, "diff.context")) {
364 diff_context_default = git_config_int(var, value);
365 if (diff_context_default < 0)
366 return -1;
367 return 0;
368 }
Vegard Nossumc4888672017-01-12 13:21:11 +0100369 if (!strcmp(var, "diff.interhunkcontext")) {
370 diff_interhunk_context_default = git_config_int(var, value);
371 if (diff_interhunk_context_default < 0)
372 return -1;
373 return 0;
374 }
Eric Wongb68ea122006-07-07 04:01:23 -0700375 if (!strcmp(var, "diff.renames")) {
Linus Torvaldscced5fb2009-04-09 11:46:15 -0700376 diff_detect_rename_default = git_config_rename(var, value);
Eric Wongb68ea122006-07-07 04:01:23 -0700377 return 0;
378 }
Junio C Hamanoaecbf912007-08-31 13:13:42 -0700379 if (!strcmp(var, "diff.autorefreshindex")) {
380 diff_auto_refresh_index = git_config_bool(var, value);
381 return 0;
382 }
Junio C Hamanoa5a818e2008-08-18 20:08:09 -0700383 if (!strcmp(var, "diff.mnemonicprefix")) {
384 diff_mnemonic_prefix = git_config_bool(var, value);
385 return 0;
386 }
Eli Collinsf89504d2010-05-02 19:03:41 -0700387 if (!strcmp(var, "diff.noprefix")) {
388 diff_no_prefix = git_config_bool(var, value);
389 return 0;
390 }
Laurent Arnoudc28ded82020-05-22 12:46:18 +0200391 if (!strcmp(var, "diff.relative")) {
392 diff_relative = git_config_bool(var, value);
393 return 0;
394 }
Zbigniew Jędrzejewski-Szmekdf444832012-03-01 13:26:46 +0100395 if (!strcmp(var, "diff.statgraphwidth")) {
396 diff_stat_graph_width = git_config_int(var, value);
397 return 0;
398 }
Brian Hetrodaec8082008-07-05 01:24:43 -0400399 if (!strcmp(var, "diff.external"))
400 return git_config_string(&external_diff_cmd_cfg, var, value);
Boyd Stephen Smith Jr98a4d872009-01-20 21:46:57 -0600401 if (!strcmp(var, "diff.wordregex"))
402 return git_config_string(&diff_word_regex_cfg, var, value);
Samuel Bronson6d8940b2013-12-18 19:08:12 -0500403 if (!strcmp(var, "diff.orderfile"))
404 return git_config_pathname(&diff_order_file_cfg, var, value);
Junio C Hamanof1af60b2007-04-22 17:52:55 -0700405
Johannes Schindelinbe4f2b42010-08-05 10:49:55 +0200406 if (!strcmp(var, "diff.ignoresubmodules"))
407 handle_ignore_submodules_arg(&default_diff_options, value);
408
Ramkumar Ramachandrac47ef572012-11-13 21:12:45 +0530409 if (!strcmp(var, "diff.submodule")) {
410 if (parse_submodule_params(&default_diff_options, value))
411 warning(_("Unknown value for 'diff.submodule' config variable: '%s'"),
412 value);
413 return 0;
414 }
415
Michal Privoznik07ab4de2013-01-16 08:51:57 +0100416 if (!strcmp(var, "diff.algorithm")) {
417 diff_algorithm = parse_algorithm_value(value);
418 if (diff_algorithm < 0)
419 return -1;
420 return 0;
421 }
422
Jeff King33c643b2017-10-13 13:24:31 -0400423 if (git_color_config(var, value, cb) < 0)
424 return -1;
425
Johannes Schindelinef90d6d2008-05-14 18:46:53 +0100426 return git_diff_basic_config(var, value, cb);
Jeff King9a1805a2008-01-04 03:59:34 -0500427}
428
Johannes Schindelinef90d6d2008-05-14 18:46:53 +0100429int git_diff_basic_config(const char *var, const char *value, void *cb)
Jeff King9a1805a2008-01-04 03:59:34 -0500430{
Jeff Kingae021d82014-06-18 15:47:50 -0400431 const char *name;
432
Linus Torvalds2b6ca6d2008-08-05 11:27:30 -0700433 if (!strcmp(var, "diff.renamelimit")) {
434 diff_rename_limit_default = git_config_int(var, value);
435 return 0;
436 }
437
Jeff King6680a082012-02-07 13:23:02 -0500438 if (userdiff_config(var, value) < 0)
439 return -1;
Jeff Kingc7534ef2008-10-26 00:45:55 -0400440
Jeff Kingae021d82014-06-18 15:47:50 -0400441 if (skip_prefix(var, "diff.color.", &name) ||
442 skip_prefix(var, "color.diff.", &name)) {
443 int slot = parse_diff_color_slot(name);
Jeff King8b8e8622009-12-12 07:25:24 -0500444 if (slot < 0)
445 return 0;
Junio C Hamano64f30e92008-02-11 10:53:56 -0800446 if (!value)
447 return config_error_nonbool(var);
Jeff Kingf6c5a292014-10-07 15:33:09 -0400448 return color_parse(value, diff_colors[slot]);
Junio C Hamano801235c2006-06-24 04:06:23 -0700449 }
Junio C Hamanof1af60b2007-04-22 17:52:55 -0700450
Jeff Kingda806352020-01-31 04:57:49 -0500451 if (!strcmp(var, "diff.wserrorhighlight")) {
452 int val = parse_ws_error_highlight(value);
453 if (val < 0)
454 return -1;
455 ws_error_highlight_default = val;
456 return 0;
457 }
458
Jim Meyeringa624eaa2008-08-15 13:39:26 +0200459 /* like GNU diff's --suppress-blank-empty option */
Johannes Schindelin950db872009-01-20 22:08:33 +0100460 if (!strcmp(var, "diff.suppressblankempty") ||
461 /* for backwards compatibility */
462 !strcmp(var, "diff.suppress-blank-empty")) {
Jim Meyeringa624eaa2008-08-15 13:39:26 +0200463 diff_suppress_blank_empty = git_config_bool(var, value);
464 return 0;
465 }
466
Johan Herland2d174952011-04-29 11:36:19 +0200467 if (!strcmp(var, "diff.dirstat")) {
Johan Herland51670fc2011-04-29 11:36:22 +0200468 struct strbuf errmsg = STRBUF_INIT;
Johan Herland712d2c72011-04-29 11:36:20 +0200469 default_diff_options.dirstat_permille = diff_dirstat_permille_default;
Johan Herland51670fc2011-04-29 11:36:22 +0200470 if (parse_dirstat_params(&default_diff_options, value, &errmsg))
Johan Herland7478ac52011-04-29 11:36:23 +0200471 warning(_("Found errors in 'diff.dirstat' config variable:\n%s"),
Johan Herland51670fc2011-04-29 11:36:22 +0200472 errmsg.buf);
473 strbuf_release(&errmsg);
Johan Herland712d2c72011-04-29 11:36:20 +0200474 diff_dirstat_permille_default = default_diff_options.dirstat_permille;
Johan Herland2d174952011-04-29 11:36:19 +0200475 return 0;
476 }
477
Marc Branchaudcf5e7722017-05-08 12:03:36 -0400478 if (git_diff_heuristic_config(var, value, cb) < 0)
479 return -1;
480
Jeff King3e1dd172011-08-17 22:05:08 -0700481 return git_default_config(var, value, cb);
Junio C Hamano801235c2006-06-24 04:06:23 -0700482}
483
Junio C Hamano6973dca2006-04-21 23:57:45 -0700484static char *quote_two(const char *one, const char *two)
485{
Junio C Hamano7c37c972020-09-10 10:01:59 -0700486 int need_one = quote_c_style(one, NULL, NULL, CQUOTE_NODQ);
487 int need_two = quote_c_style(two, NULL, NULL, CQUOTE_NODQ);
Brandon Caseyf285a2d2008-10-09 14:12:12 -0500488 struct strbuf res = STRBUF_INIT;
Junio C Hamano6973dca2006-04-21 23:57:45 -0700489
490 if (need_one + need_two) {
Pierre Habouzit663af342007-09-20 00:42:15 +0200491 strbuf_addch(&res, '"');
Junio C Hamano7c37c972020-09-10 10:01:59 -0700492 quote_c_style(one, &res, NULL, CQUOTE_NODQ);
493 quote_c_style(two, &res, NULL, CQUOTE_NODQ);
Pierre Habouzit663af342007-09-20 00:42:15 +0200494 strbuf_addch(&res, '"');
495 } else {
496 strbuf_addstr(&res, one);
497 strbuf_addstr(&res, two);
Junio C Hamano6973dca2006-04-21 23:57:45 -0700498 }
Pierre Habouzitb315c5c2007-09-27 12:58:23 +0200499 return strbuf_detach(&res, NULL);
Junio C Hamano6973dca2006-04-21 23:57:45 -0700500}
501
502static const char *external_diff(void)
503{
504 static const char *external_diff_cmd = NULL;
505 static int done_preparing = 0;
506
507 if (done_preparing)
508 return external_diff_cmd;
Kim Gybels6776a842019-01-11 21:26:08 +0100509 external_diff_cmd = xstrdup_or_null(getenv("GIT_EXTERNAL_DIFF"));
Johannes Schindelincbe02102007-12-17 13:42:20 +0000510 if (!external_diff_cmd)
511 external_diff_cmd = external_diff_cmd_cfg;
Junio C Hamano6973dca2006-04-21 23:57:45 -0700512 done_preparing = 1;
513 return external_diff_cmd;
514}
515
Michael Haggerty284098f2015-08-12 19:12:01 +0200516/*
517 * Keep track of files used for diffing. Sometimes such an entry
518 * refers to a temporary file, sometimes to an existing file, and
519 * sometimes to "/dev/null".
520 */
Junio C Hamano6973dca2006-04-21 23:57:45 -0700521static struct diff_tempfile {
Michael Haggerty284098f2015-08-12 19:12:01 +0200522 /*
523 * filename external diff should read from, or NULL if this
524 * entry is currently not in use:
525 */
526 const char *name;
527
brian m. carlsondc015052017-03-26 16:01:24 +0000528 char hex[GIT_MAX_HEXSZ + 1];
Junio C Hamano6973dca2006-04-21 23:57:45 -0700529 char mode[10];
Michael Haggerty284098f2015-08-12 19:12:01 +0200530
531 /*
532 * If this diff_tempfile instance refers to a temporary file,
533 * this tempfile object is used to manage its lifetime.
534 */
Jeff King076aa2c2017-09-05 08:15:08 -0400535 struct tempfile *tempfile;
Junio C Hamano6973dca2006-04-21 23:57:45 -0700536} diff_temp[2];
537
Junio C Hamano6957eb92009-09-14 18:44:01 -0700538struct emit_callback {
Junio C Hamano6957eb92009-09-14 18:44:01 -0700539 int color_diff;
540 unsigned ws_rule;
541 int blank_at_eof_in_preimage;
542 int blank_at_eof_in_postimage;
543 int lno_in_preimage;
544 int lno_in_postimage;
Junio C Hamano6957eb92009-09-14 18:44:01 -0700545 const char **label_path;
546 struct diff_words_data *diff_words;
Bo Yanga3c158d2010-05-26 15:08:02 +0800547 struct diff_options *opt;
Greg Bacon3e97c7c2009-11-19 15:12:24 -0600548 struct strbuf *header;
Junio C Hamano6957eb92009-09-14 18:44:01 -0700549};
550
Junio C Hamano6973dca2006-04-21 23:57:45 -0700551static int count_lines(const char *data, int size)
552{
553 int count, ch, completely_empty = 1, nl_just_seen = 0;
554 count = 0;
555 while (0 < size--) {
556 ch = *data++;
557 if (ch == '\n') {
558 count++;
559 nl_just_seen = 1;
560 completely_empty = 0;
561 }
562 else {
563 nl_just_seen = 0;
564 completely_empty = 0;
565 }
566 }
567 if (completely_empty)
568 return 0;
569 if (!nl_just_seen)
570 count++; /* no trailing newline */
571 return count;
572}
573
Nguyễn Thái Ngọc Duyb78ea5f2018-09-21 17:57:19 +0200574static int fill_mmfile(struct repository *r, mmfile_t *mf,
575 struct diff_filespec *one)
Junio C Hamano6957eb92009-09-14 18:44:01 -0700576{
577 if (!DIFF_FILE_VALID(one)) {
578 mf->ptr = (char *)""; /* does not matter */
579 mf->size = 0;
580 return 0;
581 }
Jonathan Tan1c37e862020-04-07 15:11:41 -0700582 else if (diff_populate_filespec(r, one, NULL))
Junio C Hamano6957eb92009-09-14 18:44:01 -0700583 return -1;
Junio C Hamanobb35fef2009-09-15 03:38:30 -0700584
Junio C Hamano6957eb92009-09-14 18:44:01 -0700585 mf->ptr = one->data;
586 mf->size = one->size;
587 return 0;
588}
589
Jeff Kingabb371a2011-02-19 03:16:32 -0500590/* like fill_mmfile, but only for size, so we can avoid retrieving blob */
Nguyễn Thái Ngọc Duyb78ea5f2018-09-21 17:57:19 +0200591static unsigned long diff_filespec_size(struct repository *r,
592 struct diff_filespec *one)
Jeff Kingabb371a2011-02-19 03:16:32 -0500593{
Jonathan Tan1c37e862020-04-07 15:11:41 -0700594 struct diff_populate_filespec_options dpf_options = {
595 .check_size_only = 1,
596 };
597
Jeff Kingabb371a2011-02-19 03:16:32 -0500598 if (!DIFF_FILE_VALID(one))
599 return 0;
Jonathan Tan1c37e862020-04-07 15:11:41 -0700600 diff_populate_filespec(r, one, &dpf_options);
Jeff Kingabb371a2011-02-19 03:16:32 -0500601 return one->size;
602}
603
Junio C Hamano6957eb92009-09-14 18:44:01 -0700604static int count_trailing_blank(mmfile_t *mf, unsigned ws_rule)
605{
606 char *ptr = mf->ptr;
607 long size = mf->size;
608 int cnt = 0;
609
610 if (!size)
611 return cnt;
612 ptr += size - 1; /* pointing at the very end */
613 if (*ptr != '\n')
614 ; /* incomplete line */
615 else
616 ptr--; /* skip the last LF */
617 while (mf->ptr < ptr) {
618 char *prev_eol;
619 for (prev_eol = ptr; mf->ptr <= prev_eol; prev_eol--)
620 if (*prev_eol == '\n')
621 break;
622 if (!ws_blank_line(prev_eol + 1, ptr - prev_eol, ws_rule))
623 break;
624 cnt++;
625 ptr = prev_eol - 1;
626 }
627 return cnt;
628}
629
630static void check_blank_at_eof(mmfile_t *mf1, mmfile_t *mf2,
631 struct emit_callback *ecbdata)
632{
633 int l1, l2, at;
634 unsigned ws_rule = ecbdata->ws_rule;
635 l1 = count_trailing_blank(mf1, ws_rule);
636 l2 = count_trailing_blank(mf2, ws_rule);
637 if (l2 <= l1) {
638 ecbdata->blank_at_eof_in_preimage = 0;
639 ecbdata->blank_at_eof_in_postimage = 0;
640 return;
641 }
642 at = count_lines(mf1->ptr, mf1->size);
643 ecbdata->blank_at_eof_in_preimage = (at - l1) + 1;
644
645 at = count_lines(mf2->ptr, mf2->size);
646 ecbdata->blank_at_eof_in_postimage = (at - l2) + 1;
647}
648
Johannes Schindelinf7c3b4e2018-08-13 04:33:20 -0700649static void emit_line_0(struct diff_options *o,
Stefan Beller017ac452018-08-13 18:41:19 -0700650 const char *set_sign, const char *set, unsigned reverse, const char *reset,
Junio C Hamano250f7992009-09-14 18:44:01 -0700651 int first, const char *line, int len)
Junio C Hamano6957eb92009-09-14 18:44:01 -0700652{
653 int has_trailing_newline, has_trailing_carriage_return;
Stefan Beller44410672018-08-13 18:41:22 -0700654 int needs_reset = 0; /* at the end of the line */
Bo Yanga3c158d2010-05-26 15:08:02 +0800655 FILE *file = o->file;
656
Stefan Bellerf103a6f2018-08-13 18:41:21 -0700657 fputs(diff_line_prefix(o), file);
Junio C Hamano6957eb92009-09-14 18:44:01 -0700658
Stefan Beller44410672018-08-13 18:41:22 -0700659 has_trailing_newline = (len > 0 && line[len-1] == '\n');
660 if (has_trailing_newline)
661 len--;
662
663 has_trailing_carriage_return = (len > 0 && line[len-1] == '\r');
664 if (has_trailing_carriage_return)
665 len--;
666
667 if (!len && !first)
668 goto end_of_line;
669
670 if (reverse && want_color(o->use_color)) {
671 fputs(GIT_COLOR_REVERSE, file);
672 needs_reset = 1;
Junio C Hamano250f7992009-09-14 18:44:01 -0700673 }
Junio C Hamano6957eb92009-09-14 18:44:01 -0700674
Stefan Beller44410672018-08-13 18:41:22 -0700675 if (set_sign) {
676 fputs(set_sign, file);
677 needs_reset = 1;
Junio C Hamano06a47552009-11-27 22:04:10 -0800678 }
Stefan Beller44410672018-08-13 18:41:22 -0700679
Junio C Hamano6957eb92009-09-14 18:44:01 -0700680 if (first)
Stefan Beller44410672018-08-13 18:41:22 -0700681 fputc(first, file);
Junio C Hamano6957eb92009-09-14 18:44:01 -0700682
Stefan Beller44410672018-08-13 18:41:22 -0700683 if (!len)
684 goto end_of_line;
Junio C Hamano6957eb92009-09-14 18:44:01 -0700685
Stefan Beller44410672018-08-13 18:41:22 -0700686 if (set) {
687 if (set_sign && set != set_sign)
688 fputs(reset, file);
Junio C Hamano250f7992009-09-14 18:44:01 -0700689 fputs(set, file);
Stefan Beller44410672018-08-13 18:41:22 -0700690 needs_reset = 1;
Junio C Hamano6957eb92009-09-14 18:44:01 -0700691 }
Stefan Beller44410672018-08-13 18:41:22 -0700692 fwrite(line, len, 1, file);
693 needs_reset = 1; /* 'line' may contain color codes. */
694
695end_of_line:
696 if (needs_reset)
697 fputs(reset, file);
Junio C Hamano6957eb92009-09-14 18:44:01 -0700698 if (has_trailing_carriage_return)
699 fputc('\r', file);
700 if (has_trailing_newline)
701 fputc('\n', file);
702}
703
Bo Yanga3c158d2010-05-26 15:08:02 +0800704static void emit_line(struct diff_options *o, const char *set, const char *reset,
Junio C Hamano250f7992009-09-14 18:44:01 -0700705 const char *line, int len)
706{
Stefan Beller44410672018-08-13 18:41:22 -0700707 emit_line_0(o, set, NULL, 0, reset, 0, line, len);
Junio C Hamano250f7992009-09-14 18:44:01 -0700708}
709
Stefan Beller36a4cef2017-06-29 17:06:49 -0700710enum diff_symbol {
Stefan Beller4eed0eb2017-06-29 17:07:01 -0700711 DIFF_SYMBOL_BINARY_DIFF_HEADER,
712 DIFF_SYMBOL_BINARY_DIFF_HEADER_DELTA,
713 DIFF_SYMBOL_BINARY_DIFF_HEADER_LITERAL,
714 DIFF_SYMBOL_BINARY_DIFF_BODY,
715 DIFF_SYMBOL_BINARY_DIFF_FOOTER,
Stefan Beller0911c472017-06-29 17:07:02 -0700716 DIFF_SYMBOL_STATS_SUMMARY_NO_FILES,
717 DIFF_SYMBOL_STATS_SUMMARY_ABBREV,
718 DIFF_SYMBOL_STATS_SUMMARY_INSERTS_DELETES,
719 DIFF_SYMBOL_STATS_LINE,
Stefan Bellerbd033292017-06-29 17:07:03 -0700720 DIFF_SYMBOL_WORD_DIFF,
Stefan Beller30b7e1e2017-06-29 17:07:04 -0700721 DIFF_SYMBOL_STAT_SEP,
Stefan Beller146fdb02017-06-29 17:07:05 -0700722 DIFF_SYMBOL_SUMMARY,
Stefan Bellerf3597132017-06-29 17:07:00 -0700723 DIFF_SYMBOL_SUBMODULE_ADD,
724 DIFF_SYMBOL_SUBMODULE_DEL,
725 DIFF_SYMBOL_SUBMODULE_UNTRACKED,
726 DIFF_SYMBOL_SUBMODULE_MODIFIED,
727 DIFF_SYMBOL_SUBMODULE_HEADER,
728 DIFF_SYMBOL_SUBMODULE_ERROR,
729 DIFF_SYMBOL_SUBMODULE_PIPETHROUGH,
Stefan Beller5af6ea92017-06-29 17:06:59 -0700730 DIFF_SYMBOL_REWRITE_DIFF,
Stefan Beller4acaaa72017-06-29 17:06:58 -0700731 DIFF_SYMBOL_BINARY_FILES,
Stefan Bellera29b0a12017-06-29 17:06:57 -0700732 DIFF_SYMBOL_HEADER,
Stefan Beller3ee8b7b2017-06-29 17:06:56 -0700733 DIFF_SYMBOL_FILEPAIR_PLUS,
734 DIFF_SYMBOL_FILEPAIR_MINUS,
Stefan Bellerff958672017-06-29 17:06:54 -0700735 DIFF_SYMBOL_WORDS_PORCELAIN,
736 DIFF_SYMBOL_WORDS,
Stefan Beller091f8e22017-06-29 17:06:53 -0700737 DIFF_SYMBOL_CONTEXT,
Stefan Bellerf2bb1212017-06-29 17:06:55 -0700738 DIFF_SYMBOL_CONTEXT_INCOMPLETE,
Stefan Beller091f8e22017-06-29 17:06:53 -0700739 DIFF_SYMBOL_PLUS,
740 DIFF_SYMBOL_MINUS,
Stefan Bellerb9cbfde2017-06-29 17:06:52 -0700741 DIFF_SYMBOL_NO_LF_EOF,
Stefan Beller68abc6f2017-06-29 17:06:51 -0700742 DIFF_SYMBOL_CONTEXT_FRAGINFO,
Stefan Bellerc64b4202017-06-29 17:06:50 -0700743 DIFF_SYMBOL_CONTEXT_MARKER,
Stefan Beller36a4cef2017-06-29 17:06:49 -0700744 DIFF_SYMBOL_SEPARATOR
745};
Stefan Beller091f8e22017-06-29 17:06:53 -0700746/*
747 * Flags for content lines:
748 * 0..12 are whitespace rules
749 * 13-15 are WSEH_NEW | WSEH_OLD | WSEH_CONTEXT
750 * 16 is marking if the line is blank at EOF
751 */
Stefan Beller2e2d5ac2017-06-30 13:53:07 -0700752#define DIFF_SYMBOL_CONTENT_BLANK_LINE_EOF (1<<16)
753#define DIFF_SYMBOL_MOVED_LINE (1<<17)
754#define DIFF_SYMBOL_MOVED_LINE_ALT (1<<18)
Stefan Beller86b452e2017-06-30 13:53:09 -0700755#define DIFF_SYMBOL_MOVED_LINE_UNINTERESTING (1<<19)
Stefan Beller091f8e22017-06-29 17:06:53 -0700756#define DIFF_SYMBOL_CONTENT_WS_MASK (WSEH_NEW | WSEH_OLD | WSEH_CONTEXT | WS_RULE_MASK)
757
Stefan Bellere6e045f2017-06-29 17:07:06 -0700758/*
759 * This struct is used when we need to buffer the output of the diff output.
760 *
761 * NEEDSWORK: Instead of storing a copy of the line, add an offset pointer
762 * into the pre/post image file. This pointer could be a union with the
763 * line pointer. By storing an offset into the file instead of the literal line,
764 * we can decrease the memory footprint for the buffered output. At first we
765 * may want to only have indirection for the content lines, but we could also
766 * enhance the state for emitting prefabricated lines, e.g. the similarity
767 * score line or hunk/file headers would only need to store a number or path
768 * and then the output can be constructed later on depending on state.
769 */
770struct emitted_diff_symbol {
771 const char *line;
772 int len;
773 int flags;
Phillip Wood21536d02018-11-23 11:16:57 +0000774 int indent_off; /* Offset to first non-whitespace character */
775 int indent_width; /* The visual width of the indentation */
Stefan Bellere6e045f2017-06-29 17:07:06 -0700776 enum diff_symbol s;
777};
778#define EMITTED_DIFF_SYMBOL_INIT {NULL}
779
780struct emitted_diff_symbols {
781 struct emitted_diff_symbol *buf;
782 int nr, alloc;
783};
784#define EMITTED_DIFF_SYMBOLS_INIT {NULL, 0, 0}
785
786static void append_emitted_diff_symbol(struct diff_options *o,
787 struct emitted_diff_symbol *e)
788{
789 struct emitted_diff_symbol *f;
790
791 ALLOC_GROW(o->emitted_symbols->buf,
792 o->emitted_symbols->nr + 1,
793 o->emitted_symbols->alloc);
794 f = &o->emitted_symbols->buf[o->emitted_symbols->nr++];
795
796 memcpy(f, e, sizeof(struct emitted_diff_symbol));
797 f->line = e->line ? xmemdupz(e->line, e->len) : NULL;
798}
799
Stefan Beller2e2d5ac2017-06-30 13:53:07 -0700800struct moved_entry {
801 struct hashmap_entry ent;
802 const struct emitted_diff_symbol *es;
803 struct moved_entry *next_line;
804};
805
Phillip Wood74d156f2018-10-04 11:07:41 +0100806struct moved_block {
807 struct moved_entry *match;
Phillip Wood21536d02018-11-23 11:16:57 +0000808 int wsd; /* The whitespace delta of this block */
Phillip Wood74d156f2018-10-04 11:07:41 +0100809};
810
811static void moved_block_clear(struct moved_block *b)
812{
Phillip Wood21536d02018-11-23 11:16:57 +0000813 memset(b, 0, sizeof(*b));
814}
815
Phillip Wood0cd51e92018-11-23 11:16:58 +0000816#define INDENT_BLANKLINE INT_MIN
817
Phillip Wood21536d02018-11-23 11:16:57 +0000818static void fill_es_indent_data(struct emitted_diff_symbol *es)
819{
Phillip Wood0cd51e92018-11-23 11:16:58 +0000820 unsigned int off = 0, i;
Phillip Wood21536d02018-11-23 11:16:57 +0000821 int width = 0, tab_width = es->flags & WS_TAB_WIDTH_MASK;
822 const char *s = es->line;
823 const int len = es->len;
824
825 /* skip any \v \f \r at start of indentation */
826 while (s[off] == '\f' || s[off] == '\v' ||
827 (s[off] == '\r' && off < len - 1))
828 off++;
829
830 /* calculate the visual width of indentation */
831 while(1) {
832 if (s[off] == ' ') {
833 width++;
834 off++;
835 } else if (s[off] == '\t') {
836 width += tab_width - (width % tab_width);
837 while (s[++off] == '\t')
838 width += tab_width;
839 } else {
840 break;
841 }
842 }
843
Phillip Wood0cd51e92018-11-23 11:16:58 +0000844 /* check if this line is blank */
845 for (i = off; i < len; i++)
846 if (!isspace(s[i]))
847 break;
848
849 if (i == len) {
850 es->indent_width = INDENT_BLANKLINE;
851 es->indent_off = len;
852 } else {
853 es->indent_off = off;
854 es->indent_width = width;
855 }
Phillip Wood74d156f2018-10-04 11:07:41 +0100856}
857
Stefan Bellerca1f4ae2018-07-18 12:31:55 -0700858static int compute_ws_delta(const struct emitted_diff_symbol *a,
Phillip Wood21536d02018-11-23 11:16:57 +0000859 const struct emitted_diff_symbol *b,
860 int *out)
Stefan Bellerca1f4ae2018-07-18 12:31:55 -0700861{
Phillip Wood21536d02018-11-23 11:16:57 +0000862 int a_len = a->len,
863 b_len = b->len,
864 a_off = a->indent_off,
865 a_width = a->indent_width,
866 b_off = b->indent_off,
867 b_width = b->indent_width;
868 int delta;
Stefan Bellerca1f4ae2018-07-18 12:31:55 -0700869
Phillip Wood0cd51e92018-11-23 11:16:58 +0000870 if (a_width == INDENT_BLANKLINE && b_width == INDENT_BLANKLINE) {
871 *out = INDENT_BLANKLINE;
872 return 1;
873 }
874
Phillip Wood21536d02018-11-23 11:16:57 +0000875 if (a->s == DIFF_SYMBOL_PLUS)
876 delta = a_width - b_width;
877 else
878 delta = b_width - a_width;
879
880 if (a_len - a_off != b_len - b_off ||
881 memcmp(a->line + a_off, b->line + b_off, a_len - a_off))
Phillip Woodfe4516d2018-10-04 11:07:43 +0100882 return 0;
883
Phillip Wood21536d02018-11-23 11:16:57 +0000884 *out = delta;
Stefan Bellerca1f4ae2018-07-18 12:31:55 -0700885
Phillip Woodfe4516d2018-10-04 11:07:43 +0100886 return 1;
Stefan Bellerca1f4ae2018-07-18 12:31:55 -0700887}
888
889static int cmp_in_block_with_wsd(const struct diff_options *o,
890 const struct moved_entry *cur,
891 const struct moved_entry *match,
Phillip Wood74d156f2018-10-04 11:07:41 +0100892 struct moved_block *pmb,
Stefan Bellerca1f4ae2018-07-18 12:31:55 -0700893 int n)
894{
895 struct emitted_diff_symbol *l = &o->emitted_symbols->buf[n];
Phillip Wood7a4252c2018-11-23 11:16:56 +0000896 int al = cur->es->len, bl = match->es->len, cl = l->len;
Stefan Bellerca1f4ae2018-07-18 12:31:55 -0700897 const char *a = cur->es->line,
898 *b = match->es->line,
899 *c = l->line;
Phillip Wood21536d02018-11-23 11:16:57 +0000900 int a_off = cur->es->indent_off,
901 a_width = cur->es->indent_width,
902 c_off = l->indent_off,
903 c_width = l->indent_width;
904 int delta;
Stefan Bellerca1f4ae2018-07-18 12:31:55 -0700905
906 /*
Phillip Wood7a4252c2018-11-23 11:16:56 +0000907 * We need to check if 'cur' is equal to 'match'. As those
908 * are from the same (+/-) side, we do not need to adjust for
909 * indent changes. However these were found using fuzzy
910 * matching so we do have to check if they are equal. Here we
911 * just check the lengths. We delay calling memcmp() to check
912 * the contents until later as if the length comparison for a
913 * and c fails we can avoid the call all together.
Stefan Bellerca1f4ae2018-07-18 12:31:55 -0700914 */
Phillip Wood7a4252c2018-11-23 11:16:56 +0000915 if (al != bl)
Stefan Bellerca1f4ae2018-07-18 12:31:55 -0700916 return 1;
917
Phillip Wood0cd51e92018-11-23 11:16:58 +0000918 /* If 'l' and 'cur' are both blank then they match. */
919 if (a_width == INDENT_BLANKLINE && c_width == INDENT_BLANKLINE)
920 return 0;
Stefan Bellerca1f4ae2018-07-18 12:31:55 -0700921
922 /*
Phillip Wood21536d02018-11-23 11:16:57 +0000923 * The indent changes of the block are known and stored in pmb->wsd;
924 * however we need to check if the indent changes of the current line
925 * match those of the current block and that the text of 'l' and 'cur'
926 * after the indentation match.
Stefan Bellerca1f4ae2018-07-18 12:31:55 -0700927 */
Phillip Wood21536d02018-11-23 11:16:57 +0000928 if (cur->es->s == DIFF_SYMBOL_PLUS)
929 delta = a_width - c_width;
930 else
931 delta = c_width - a_width;
Stefan Bellerca1f4ae2018-07-18 12:31:55 -0700932
Phillip Wood0cd51e92018-11-23 11:16:58 +0000933 /*
934 * If the previous lines of this block were all blank then set its
935 * whitespace delta.
936 */
937 if (pmb->wsd == INDENT_BLANKLINE)
938 pmb->wsd = delta;
Stefan Bellerca1f4ae2018-07-18 12:31:55 -0700939
Phillip Wood21536d02018-11-23 11:16:57 +0000940 return !(delta == pmb->wsd && al - a_off == cl - c_off &&
941 !memcmp(a, b, al) && !
942 memcmp(a + a_off, c + c_off, al - a_off));
Stefan Bellerca1f4ae2018-07-18 12:31:55 -0700943}
944
Stefan Belleree1df662018-07-16 16:05:38 -0700945static int moved_entry_cmp(const void *hashmap_cmp_fn_data,
Eric Wong939af162019-10-06 23:30:37 +0000946 const struct hashmap_entry *eptr,
947 const struct hashmap_entry *entry_or_key,
Stefan Beller2e2d5ac2017-06-30 13:53:07 -0700948 const void *keydata)
949{
Stefan Belleree1df662018-07-16 16:05:38 -0700950 const struct diff_options *diffopt = hashmap_cmp_fn_data;
Eric Wong939af162019-10-06 23:30:37 +0000951 const struct moved_entry *a, *b;
Stefan Bellerb3095712018-07-16 16:05:40 -0700952 unsigned flags = diffopt->color_moved_ws_handling
953 & XDF_WHITESPACE_FLAGS;
Stefan Belleree1df662018-07-16 16:05:38 -0700954
Eric Wong939af162019-10-06 23:30:37 +0000955 a = container_of(eptr, const struct moved_entry, ent);
956 b = container_of(entry_or_key, const struct moved_entry, ent);
957
Stefan Bellerca1f4ae2018-07-18 12:31:55 -0700958 if (diffopt->color_moved_ws_handling &
959 COLOR_MOVED_WS_ALLOW_INDENTATION_CHANGE)
960 /*
961 * As there is not specific white space config given,
962 * we'd need to check for a new block, so ignore all
963 * white space. The setup of the white space
964 * configuration for the next block is done else where
965 */
966 flags |= XDF_IGNORE_WHITESPACE;
967
Stefan Beller01be97c2017-10-25 11:49:12 -0700968 return !xdiff_compare_lines(a->es->line, a->es->len,
969 b->es->line, b->es->len,
Stefan Bellerb3095712018-07-16 16:05:40 -0700970 flags);
Stefan Beller2e2d5ac2017-06-30 13:53:07 -0700971}
972
973static struct moved_entry *prepare_entry(struct diff_options *o,
974 int line_no)
975{
976 struct moved_entry *ret = xmalloc(sizeof(*ret));
977 struct emitted_diff_symbol *l = &o->emitted_symbols->buf[line_no];
Stefan Bellerb3095712018-07-16 16:05:40 -0700978 unsigned flags = o->color_moved_ws_handling & XDF_WHITESPACE_FLAGS;
Eric Wonge010a412019-10-06 23:30:24 +0000979 unsigned int hash = xdiff_hash_string(l->line, l->len, flags);
Stefan Beller2e2d5ac2017-06-30 13:53:07 -0700980
Eric Wonge010a412019-10-06 23:30:24 +0000981 hashmap_entry_init(&ret->ent, hash);
Stefan Beller2e2d5ac2017-06-30 13:53:07 -0700982 ret->es = l;
983 ret->next_line = NULL;
984
985 return ret;
986}
987
988static void add_lines_to_move_detection(struct diff_options *o,
989 struct hashmap *add_lines,
990 struct hashmap *del_lines)
991{
992 struct moved_entry *prev_line = NULL;
993
994 int n;
995 for (n = 0; n < o->emitted_symbols->nr; n++) {
996 struct hashmap *hm;
997 struct moved_entry *key;
998
999 switch (o->emitted_symbols->buf[n].s) {
1000 case DIFF_SYMBOL_PLUS:
1001 hm = add_lines;
1002 break;
1003 case DIFF_SYMBOL_MINUS:
1004 hm = del_lines;
1005 break;
1006 default:
1007 prev_line = NULL;
1008 continue;
1009 }
1010
Phillip Wood21536d02018-11-23 11:16:57 +00001011 if (o->color_moved_ws_handling &
1012 COLOR_MOVED_WS_ALLOW_INDENTATION_CHANGE)
1013 fill_es_indent_data(&o->emitted_symbols->buf[n]);
Stefan Beller2e2d5ac2017-06-30 13:53:07 -07001014 key = prepare_entry(o, n);
1015 if (prev_line && prev_line->es->s == o->emitted_symbols->buf[n].s)
1016 prev_line->next_line = key;
1017
Eric Wongb94e5c12019-10-06 23:30:29 +00001018 hashmap_add(hm, &key->ent);
Stefan Beller2e2d5ac2017-06-30 13:53:07 -07001019 prev_line = key;
1020 }
1021}
1022
Stefan Bellere2fe6ab2018-07-16 16:05:41 -07001023static void pmb_advance_or_null(struct diff_options *o,
1024 struct moved_entry *match,
1025 struct hashmap *hm,
Phillip Wood74d156f2018-10-04 11:07:41 +01001026 struct moved_block *pmb,
Stefan Bellere2fe6ab2018-07-16 16:05:41 -07001027 int pmb_nr)
1028{
1029 int i;
1030 for (i = 0; i < pmb_nr; i++) {
Phillip Wood74d156f2018-10-04 11:07:41 +01001031 struct moved_entry *prev = pmb[i].match;
Stefan Bellere2fe6ab2018-07-16 16:05:41 -07001032 struct moved_entry *cur = (prev && prev->next_line) ?
1033 prev->next_line : NULL;
Eric Wong939af162019-10-06 23:30:37 +00001034 if (cur && !hm->cmpfn(o, &cur->ent, &match->ent, NULL)) {
Phillip Wood74d156f2018-10-04 11:07:41 +01001035 pmb[i].match = cur;
Stefan Bellere2fe6ab2018-07-16 16:05:41 -07001036 } else {
Phillip Wood74d156f2018-10-04 11:07:41 +01001037 pmb[i].match = NULL;
Stefan Bellere2fe6ab2018-07-16 16:05:41 -07001038 }
1039 }
1040}
1041
Stefan Bellerca1f4ae2018-07-18 12:31:55 -07001042static void pmb_advance_or_null_multi_match(struct diff_options *o,
1043 struct moved_entry *match,
1044 struct hashmap *hm,
Phillip Wood74d156f2018-10-04 11:07:41 +01001045 struct moved_block *pmb,
Stefan Bellerca1f4ae2018-07-18 12:31:55 -07001046 int pmb_nr, int n)
1047{
1048 int i;
1049 char *got_match = xcalloc(1, pmb_nr);
1050
Eric Wong23dee692019-10-06 23:30:41 +00001051 hashmap_for_each_entry_from(hm, match, ent) {
Stefan Bellerca1f4ae2018-07-18 12:31:55 -07001052 for (i = 0; i < pmb_nr; i++) {
Phillip Wood74d156f2018-10-04 11:07:41 +01001053 struct moved_entry *prev = pmb[i].match;
Stefan Bellerca1f4ae2018-07-18 12:31:55 -07001054 struct moved_entry *cur = (prev && prev->next_line) ?
1055 prev->next_line : NULL;
1056 if (!cur)
1057 continue;
Phillip Wood74d156f2018-10-04 11:07:41 +01001058 if (!cmp_in_block_with_wsd(o, cur, match, &pmb[i], n))
Stefan Bellerca1f4ae2018-07-18 12:31:55 -07001059 got_match[i] |= 1;
1060 }
1061 }
1062
1063 for (i = 0; i < pmb_nr; i++) {
1064 if (got_match[i]) {
Phillip Wood74d156f2018-10-04 11:07:41 +01001065 /* Advance to the next line */
1066 pmb[i].match = pmb[i].match->next_line;
Phillip Woodfab01ec2018-09-04 14:52:58 +01001067 } else {
Phillip Wood74d156f2018-10-04 11:07:41 +01001068 moved_block_clear(&pmb[i]);
Phillip Woodfab01ec2018-09-04 14:52:58 +01001069 }
Stefan Bellerca1f4ae2018-07-18 12:31:55 -07001070 }
Phillip Wood9c1a6c22018-10-04 11:07:44 +01001071
1072 free(got_match);
Stefan Bellerca1f4ae2018-07-18 12:31:55 -07001073}
1074
Phillip Wood74d156f2018-10-04 11:07:41 +01001075static int shrink_potential_moved_blocks(struct moved_block *pmb,
Stefan Beller2e2d5ac2017-06-30 13:53:07 -07001076 int pmb_nr)
1077{
1078 int lp, rp;
1079
1080 /* Shrink the set of potential block to the remaining running */
1081 for (lp = 0, rp = pmb_nr - 1; lp <= rp;) {
Phillip Wood74d156f2018-10-04 11:07:41 +01001082 while (lp < pmb_nr && pmb[lp].match)
Stefan Beller2e2d5ac2017-06-30 13:53:07 -07001083 lp++;
1084 /* lp points at the first NULL now */
1085
Phillip Wood74d156f2018-10-04 11:07:41 +01001086 while (rp > -1 && !pmb[rp].match)
Stefan Beller2e2d5ac2017-06-30 13:53:07 -07001087 rp--;
1088 /* rp points at the last non-NULL */
1089
1090 if (lp < pmb_nr && rp > -1 && lp < rp) {
1091 pmb[lp] = pmb[rp];
Phillip Wood21536d02018-11-23 11:16:57 +00001092 memset(&pmb[rp], 0, sizeof(pmb[rp]));
Stefan Beller2e2d5ac2017-06-30 13:53:07 -07001093 rp--;
1094 lp++;
1095 }
1096 }
1097
1098 /* Remember the number of running sets */
1099 return rp + 1;
1100}
1101
Jonathan Tan09153272017-08-15 18:27:38 -07001102/*
1103 * If o->color_moved is COLOR_MOVED_PLAIN, this function does nothing.
1104 *
Jonathan Tanf0b8fb62017-08-15 18:27:39 -07001105 * Otherwise, if the last block has fewer alphanumeric characters than
1106 * COLOR_MOVED_MIN_ALNUM_COUNT, unset DIFF_SYMBOL_MOVED_LINE on all lines in
Jonathan Tan09153272017-08-15 18:27:38 -07001107 * that block.
1108 *
1109 * The last block consists of the (n - block_length)'th line up to but not
1110 * including the nth line.
Jonathan Tanf0b8fb62017-08-15 18:27:39 -07001111 *
Phillip Woodb0a2ba42018-11-23 11:16:55 +00001112 * Returns 0 if the last block is empty or is unset by this function, non zero
1113 * otherwise.
1114 *
Jonathan Tanf0b8fb62017-08-15 18:27:39 -07001115 * NEEDSWORK: This uses the same heuristic as blame_entry_score() in blame.c.
1116 * Think of a way to unify them.
Jonathan Tan09153272017-08-15 18:27:38 -07001117 */
Phillip Woodb0a2ba42018-11-23 11:16:55 +00001118static int adjust_last_block(struct diff_options *o, int n, int block_length)
Jonathan Tan09153272017-08-15 18:27:38 -07001119{
Jonathan Tanf0b8fb62017-08-15 18:27:39 -07001120 int i, alnum_count = 0;
1121 if (o->color_moved == COLOR_MOVED_PLAIN)
Phillip Woodb0a2ba42018-11-23 11:16:55 +00001122 return block_length;
Jonathan Tanf0b8fb62017-08-15 18:27:39 -07001123 for (i = 1; i < block_length + 1; i++) {
1124 const char *c = o->emitted_symbols->buf[n - i].line;
1125 for (; *c; c++) {
1126 if (!isalnum(*c))
1127 continue;
1128 alnum_count++;
1129 if (alnum_count >= COLOR_MOVED_MIN_ALNUM_COUNT)
Phillip Woodb0a2ba42018-11-23 11:16:55 +00001130 return 1;
Jonathan Tanf0b8fb62017-08-15 18:27:39 -07001131 }
1132 }
Jonathan Tan09153272017-08-15 18:27:38 -07001133 for (i = 1; i < block_length + 1; i++)
1134 o->emitted_symbols->buf[n - i].flags &= ~DIFF_SYMBOL_MOVED_LINE;
Phillip Woodb0a2ba42018-11-23 11:16:55 +00001135 return 0;
Jonathan Tan09153272017-08-15 18:27:38 -07001136}
1137
Stefan Beller2e2d5ac2017-06-30 13:53:07 -07001138/* Find blocks of moved code, delegate actual coloring decision to helper */
1139static void mark_color_as_moved(struct diff_options *o,
1140 struct hashmap *add_lines,
1141 struct hashmap *del_lines)
1142{
Phillip Wood74d156f2018-10-04 11:07:41 +01001143 struct moved_block *pmb = NULL; /* potentially moved blocks */
Stefan Beller2e2d5ac2017-06-30 13:53:07 -07001144 int pmb_nr = 0, pmb_alloc = 0;
Phillip Woodb0a2ba42018-11-23 11:16:55 +00001145 int n, flipped_block = 0, block_length = 0;
Stefan Beller2e2d5ac2017-06-30 13:53:07 -07001146
1147
1148 for (n = 0; n < o->emitted_symbols->nr; n++) {
1149 struct hashmap *hm = NULL;
1150 struct moved_entry *key;
1151 struct moved_entry *match = NULL;
1152 struct emitted_diff_symbol *l = &o->emitted_symbols->buf[n];
Phillip Woodb0a2ba42018-11-23 11:16:55 +00001153 enum diff_symbol last_symbol = 0;
Stefan Beller2e2d5ac2017-06-30 13:53:07 -07001154
1155 switch (l->s) {
1156 case DIFF_SYMBOL_PLUS:
1157 hm = del_lines;
1158 key = prepare_entry(o, n);
Eric Wong404ab782019-10-06 23:30:42 +00001159 match = hashmap_get_entry(hm, key, ent, NULL);
Stefan Beller2e2d5ac2017-06-30 13:53:07 -07001160 free(key);
1161 break;
1162 case DIFF_SYMBOL_MINUS:
1163 hm = add_lines;
1164 key = prepare_entry(o, n);
Eric Wong404ab782019-10-06 23:30:42 +00001165 match = hashmap_get_entry(hm, key, ent, NULL);
Stefan Beller2e2d5ac2017-06-30 13:53:07 -07001166 free(key);
1167 break;
1168 default:
Phillip Woodb0a2ba42018-11-23 11:16:55 +00001169 flipped_block = 0;
Stefan Beller2e2d5ac2017-06-30 13:53:07 -07001170 }
1171
1172 if (!match) {
Phillip Wood74d156f2018-10-04 11:07:41 +01001173 int i;
1174
Jonathan Tan09153272017-08-15 18:27:38 -07001175 adjust_last_block(o, n, block_length);
Phillip Wood74d156f2018-10-04 11:07:41 +01001176 for(i = 0; i < pmb_nr; i++)
1177 moved_block_clear(&pmb[i]);
Stefan Beller2e2d5ac2017-06-30 13:53:07 -07001178 pmb_nr = 0;
1179 block_length = 0;
Phillip Woodb0a2ba42018-11-23 11:16:55 +00001180 flipped_block = 0;
1181 last_symbol = l->s;
Stefan Beller2e2d5ac2017-06-30 13:53:07 -07001182 continue;
1183 }
1184
Phillip Wood2034b472018-11-23 11:16:54 +00001185 if (o->color_moved == COLOR_MOVED_PLAIN) {
Phillip Woodb0a2ba42018-11-23 11:16:55 +00001186 last_symbol = l->s;
Phillip Wood2034b472018-11-23 11:16:54 +00001187 l->flags |= DIFF_SYMBOL_MOVED_LINE;
Stefan Beller176841f2017-06-30 13:53:08 -07001188 continue;
Phillip Wood2034b472018-11-23 11:16:54 +00001189 }
Stefan Beller176841f2017-06-30 13:53:08 -07001190
Stefan Bellerca1f4ae2018-07-18 12:31:55 -07001191 if (o->color_moved_ws_handling &
1192 COLOR_MOVED_WS_ALLOW_INDENTATION_CHANGE)
1193 pmb_advance_or_null_multi_match(o, match, hm, pmb, pmb_nr, n);
1194 else
1195 pmb_advance_or_null(o, match, hm, pmb, pmb_nr);
Stefan Beller2e2d5ac2017-06-30 13:53:07 -07001196
1197 pmb_nr = shrink_potential_moved_blocks(pmb, pmb_nr);
1198
1199 if (pmb_nr == 0) {
1200 /*
1201 * The current line is the start of a new block.
1202 * Setup the set of potential blocks.
1203 */
Eric Wong23dee692019-10-06 23:30:41 +00001204 hashmap_for_each_entry_from(hm, match, ent) {
Stefan Beller2e2d5ac2017-06-30 13:53:07 -07001205 ALLOC_GROW(pmb, pmb_nr + 1, pmb_alloc);
Stefan Bellerca1f4ae2018-07-18 12:31:55 -07001206 if (o->color_moved_ws_handling &
1207 COLOR_MOVED_WS_ALLOW_INDENTATION_CHANGE) {
Phillip Wood74d156f2018-10-04 11:07:41 +01001208 if (compute_ws_delta(l, match->es,
1209 &pmb[pmb_nr].wsd))
1210 pmb[pmb_nr++].match = match;
Stefan Bellerca1f4ae2018-07-18 12:31:55 -07001211 } else {
Phillip Wood21536d02018-11-23 11:16:57 +00001212 pmb[pmb_nr].wsd = 0;
Phillip Wood74d156f2018-10-04 11:07:41 +01001213 pmb[pmb_nr++].match = match;
Stefan Bellerca1f4ae2018-07-18 12:31:55 -07001214 }
Stefan Beller2e2d5ac2017-06-30 13:53:07 -07001215 }
1216
Phillip Woodb0a2ba42018-11-23 11:16:55 +00001217 if (adjust_last_block(o, n, block_length) &&
1218 pmb_nr && last_symbol != l->s)
1219 flipped_block = (flipped_block + 1) % 2;
1220 else
1221 flipped_block = 0;
Jonathan Tanf0b8fb62017-08-15 18:27:39 -07001222
Jonathan Tanf0b8fb62017-08-15 18:27:39 -07001223 block_length = 0;
Stefan Beller2e2d5ac2017-06-30 13:53:07 -07001224 }
1225
Phillip Wood2034b472018-11-23 11:16:54 +00001226 if (pmb_nr) {
1227 block_length++;
Phillip Wood2034b472018-11-23 11:16:54 +00001228 l->flags |= DIFF_SYMBOL_MOVED_LINE;
1229 if (flipped_block && o->color_moved != COLOR_MOVED_BLOCKS)
1230 l->flags |= DIFF_SYMBOL_MOVED_LINE_ALT;
1231 }
Phillip Woodb0a2ba42018-11-23 11:16:55 +00001232 last_symbol = l->s;
Stefan Beller2e2d5ac2017-06-30 13:53:07 -07001233 }
Jonathan Tan09153272017-08-15 18:27:38 -07001234 adjust_last_block(o, n, block_length);
Stefan Beller2e2d5ac2017-06-30 13:53:07 -07001235
Phillip Wood74d156f2018-10-04 11:07:41 +01001236 for(n = 0; n < pmb_nr; n++)
1237 moved_block_clear(&pmb[n]);
Stefan Beller2e2d5ac2017-06-30 13:53:07 -07001238 free(pmb);
1239}
Stefan Bellere6e045f2017-06-29 17:07:06 -07001240
Stefan Beller86b452e2017-06-30 13:53:09 -07001241#define DIFF_SYMBOL_MOVED_LINE_ZEBRA_MASK \
1242 (DIFF_SYMBOL_MOVED_LINE | DIFF_SYMBOL_MOVED_LINE_ALT)
1243static void dim_moved_lines(struct diff_options *o)
1244{
1245 int n;
1246 for (n = 0; n < o->emitted_symbols->nr; n++) {
1247 struct emitted_diff_symbol *prev = (n != 0) ?
1248 &o->emitted_symbols->buf[n - 1] : NULL;
1249 struct emitted_diff_symbol *l = &o->emitted_symbols->buf[n];
1250 struct emitted_diff_symbol *next =
1251 (n < o->emitted_symbols->nr - 1) ?
1252 &o->emitted_symbols->buf[n + 1] : NULL;
1253
1254 /* Not a plus or minus line? */
1255 if (l->s != DIFF_SYMBOL_PLUS && l->s != DIFF_SYMBOL_MINUS)
1256 continue;
1257
1258 /* Not a moved line? */
1259 if (!(l->flags & DIFF_SYMBOL_MOVED_LINE))
1260 continue;
1261
1262 /*
1263 * If prev or next are not a plus or minus line,
1264 * pretend they don't exist
1265 */
1266 if (prev && prev->s != DIFF_SYMBOL_PLUS &&
1267 prev->s != DIFF_SYMBOL_MINUS)
1268 prev = NULL;
1269 if (next && next->s != DIFF_SYMBOL_PLUS &&
1270 next->s != DIFF_SYMBOL_MINUS)
1271 next = NULL;
1272
1273 /* Inside a block? */
1274 if ((prev &&
1275 (prev->flags & DIFF_SYMBOL_MOVED_LINE_ZEBRA_MASK) ==
1276 (l->flags & DIFF_SYMBOL_MOVED_LINE_ZEBRA_MASK)) &&
1277 (next &&
1278 (next->flags & DIFF_SYMBOL_MOVED_LINE_ZEBRA_MASK) ==
1279 (l->flags & DIFF_SYMBOL_MOVED_LINE_ZEBRA_MASK))) {
1280 l->flags |= DIFF_SYMBOL_MOVED_LINE_UNINTERESTING;
1281 continue;
1282 }
1283
1284 /* Check if we are at an interesting bound: */
1285 if (prev && (prev->flags & DIFF_SYMBOL_MOVED_LINE) &&
1286 (prev->flags & DIFF_SYMBOL_MOVED_LINE_ALT) !=
1287 (l->flags & DIFF_SYMBOL_MOVED_LINE_ALT))
1288 continue;
1289 if (next && (next->flags & DIFF_SYMBOL_MOVED_LINE) &&
1290 (next->flags & DIFF_SYMBOL_MOVED_LINE_ALT) !=
1291 (l->flags & DIFF_SYMBOL_MOVED_LINE_ALT))
1292 continue;
1293
1294 /*
1295 * The boundary to prev and next are not interesting,
1296 * so this line is not interesting as a whole
1297 */
1298 l->flags |= DIFF_SYMBOL_MOVED_LINE_UNINTERESTING;
1299 }
1300}
1301
Stefan Beller091f8e22017-06-29 17:06:53 -07001302static void emit_line_ws_markup(struct diff_options *o,
Stefan Beller9d1e16b2018-08-13 18:41:18 -07001303 const char *set_sign, const char *set,
1304 const char *reset,
Stefan Bellerbc9feb02018-10-10 16:24:59 -07001305 int sign_index, const char *line, int len,
Stefan Beller091f8e22017-06-29 17:06:53 -07001306 unsigned ws_rule, int blank_at_eof)
1307{
1308 const char *ws = NULL;
Stefan Bellerbc9feb02018-10-10 16:24:59 -07001309 int sign = o->output_indicators[sign_index];
Stefan Beller091f8e22017-06-29 17:06:53 -07001310
1311 if (o->ws_error_highlight & ws_rule) {
1312 ws = diff_get_color_opt(o, DIFF_WHITESPACE);
1313 if (!*ws)
1314 ws = NULL;
1315 }
1316
Johannes Schindelinf7c3b4e2018-08-13 04:33:20 -07001317 if (!ws && !set_sign)
Stefan Beller017ac452018-08-13 18:41:19 -07001318 emit_line_0(o, set, NULL, 0, reset, sign, line, len);
Johannes Schindelinf7c3b4e2018-08-13 04:33:20 -07001319 else if (!ws) {
Stefan Beller29ef7592018-08-13 18:41:20 -07001320 emit_line_0(o, set_sign, set, !!set_sign, reset, sign, line, len);
Johannes Schindelinf7c3b4e2018-08-13 04:33:20 -07001321 } else if (blank_at_eof)
Stefan Beller091f8e22017-06-29 17:06:53 -07001322 /* Blank line at EOF - paint '+' as well */
Stefan Beller017ac452018-08-13 18:41:19 -07001323 emit_line_0(o, ws, NULL, 0, reset, sign, line, len);
Stefan Beller091f8e22017-06-29 17:06:53 -07001324 else {
1325 /* Emit just the prefix, then the rest. */
Stefan Beller017ac452018-08-13 18:41:19 -07001326 emit_line_0(o, set_sign ? set_sign : set, NULL, !!set_sign, reset,
Johannes Schindelinf7c3b4e2018-08-13 04:33:20 -07001327 sign, "", 0);
Stefan Beller091f8e22017-06-29 17:06:53 -07001328 ws_check_emit(line, len, ws_rule,
1329 o->file, set, reset, ws);
1330 }
1331}
Stefan Beller36a4cef2017-06-29 17:06:49 -07001332
Stefan Bellere6e045f2017-06-29 17:07:06 -07001333static void emit_diff_symbol_from_struct(struct diff_options *o,
1334 struct emitted_diff_symbol *eds)
Stefan Beller36a4cef2017-06-29 17:06:49 -07001335{
Stefan Bellerb9cbfde2017-06-29 17:06:52 -07001336 static const char *nneof = " No newline at end of file\n";
Johannes Schindelinf7c3b4e2018-08-13 04:33:20 -07001337 const char *context, *reset, *set, *set_sign, *meta, *fraginfo;
Stefan Beller0911c472017-06-29 17:07:02 -07001338 struct strbuf sb = STRBUF_INIT;
Stefan Bellere6e045f2017-06-29 17:07:06 -07001339
1340 enum diff_symbol s = eds->s;
1341 const char *line = eds->line;
1342 int len = eds->len;
1343 unsigned flags = eds->flags;
1344
Stefan Beller36a4cef2017-06-29 17:06:49 -07001345 switch (s) {
Stefan Bellerb9cbfde2017-06-29 17:06:52 -07001346 case DIFF_SYMBOL_NO_LF_EOF:
1347 context = diff_get_color_opt(o, DIFF_CONTEXT);
1348 reset = diff_get_color_opt(o, DIFF_RESET);
1349 putc('\n', o->file);
Stefan Beller017ac452018-08-13 18:41:19 -07001350 emit_line_0(o, context, NULL, 0, reset, '\\',
Stefan Bellerb9cbfde2017-06-29 17:06:52 -07001351 nneof, strlen(nneof));
1352 break;
Stefan Bellerf3597132017-06-29 17:07:00 -07001353 case DIFF_SYMBOL_SUBMODULE_HEADER:
1354 case DIFF_SYMBOL_SUBMODULE_ERROR:
1355 case DIFF_SYMBOL_SUBMODULE_PIPETHROUGH:
Stefan Beller0911c472017-06-29 17:07:02 -07001356 case DIFF_SYMBOL_STATS_SUMMARY_INSERTS_DELETES:
Stefan Beller146fdb02017-06-29 17:07:05 -07001357 case DIFF_SYMBOL_SUMMARY:
Stefan Beller0911c472017-06-29 17:07:02 -07001358 case DIFF_SYMBOL_STATS_LINE:
Stefan Beller4eed0eb2017-06-29 17:07:01 -07001359 case DIFF_SYMBOL_BINARY_DIFF_BODY:
Stefan Beller68abc6f2017-06-29 17:06:51 -07001360 case DIFF_SYMBOL_CONTEXT_FRAGINFO:
1361 emit_line(o, "", "", line, len);
1362 break;
Stefan Bellerf2bb1212017-06-29 17:06:55 -07001363 case DIFF_SYMBOL_CONTEXT_INCOMPLETE:
Stefan Bellerc64b4202017-06-29 17:06:50 -07001364 case DIFF_SYMBOL_CONTEXT_MARKER:
1365 context = diff_get_color_opt(o, DIFF_CONTEXT);
1366 reset = diff_get_color_opt(o, DIFF_RESET);
1367 emit_line(o, context, reset, line, len);
1368 break;
Stefan Beller36a4cef2017-06-29 17:06:49 -07001369 case DIFF_SYMBOL_SEPARATOR:
1370 fprintf(o->file, "%s%c",
1371 diff_line_prefix(o),
1372 o->line_termination);
1373 break;
Stefan Beller091f8e22017-06-29 17:06:53 -07001374 case DIFF_SYMBOL_CONTEXT:
1375 set = diff_get_color_opt(o, DIFF_CONTEXT);
1376 reset = diff_get_color_opt(o, DIFF_RESET);
Johannes Schindelinf7c3b4e2018-08-13 04:33:20 -07001377 set_sign = NULL;
1378 if (o->flags.dual_color_diffed_diffs) {
1379 char c = !len ? 0 : line[0];
1380
1381 if (c == '+')
1382 set = diff_get_color_opt(o, DIFF_FILE_NEW);
1383 else if (c == '@')
1384 set = diff_get_color_opt(o, DIFF_FRAGINFO);
1385 else if (c == '-')
1386 set = diff_get_color_opt(o, DIFF_FILE_OLD);
1387 }
Stefan Beller7648b792018-08-17 13:43:52 -07001388 emit_line_ws_markup(o, set_sign, set, reset,
Stefan Bellerbc9feb02018-10-10 16:24:59 -07001389 OUTPUT_INDICATOR_CONTEXT, line, len,
Stefan Beller091f8e22017-06-29 17:06:53 -07001390 flags & (DIFF_SYMBOL_CONTENT_WS_MASK), 0);
1391 break;
1392 case DIFF_SYMBOL_PLUS:
Stefan Beller86b452e2017-06-30 13:53:09 -07001393 switch (flags & (DIFF_SYMBOL_MOVED_LINE |
1394 DIFF_SYMBOL_MOVED_LINE_ALT |
1395 DIFF_SYMBOL_MOVED_LINE_UNINTERESTING)) {
1396 case DIFF_SYMBOL_MOVED_LINE |
1397 DIFF_SYMBOL_MOVED_LINE_ALT |
1398 DIFF_SYMBOL_MOVED_LINE_UNINTERESTING:
1399 set = diff_get_color_opt(o, DIFF_FILE_NEW_MOVED_ALT_DIM);
1400 break;
1401 case DIFF_SYMBOL_MOVED_LINE |
1402 DIFF_SYMBOL_MOVED_LINE_ALT:
Stefan Beller2e2d5ac2017-06-30 13:53:07 -07001403 set = diff_get_color_opt(o, DIFF_FILE_NEW_MOVED_ALT);
Stefan Beller86b452e2017-06-30 13:53:09 -07001404 break;
1405 case DIFF_SYMBOL_MOVED_LINE |
1406 DIFF_SYMBOL_MOVED_LINE_UNINTERESTING:
1407 set = diff_get_color_opt(o, DIFF_FILE_NEW_MOVED_DIM);
1408 break;
1409 case DIFF_SYMBOL_MOVED_LINE:
Stefan Beller2e2d5ac2017-06-30 13:53:07 -07001410 set = diff_get_color_opt(o, DIFF_FILE_NEW_MOVED);
Stefan Beller86b452e2017-06-30 13:53:09 -07001411 break;
1412 default:
Stefan Beller2e2d5ac2017-06-30 13:53:07 -07001413 set = diff_get_color_opt(o, DIFF_FILE_NEW);
Stefan Beller86b452e2017-06-30 13:53:09 -07001414 }
Stefan Beller091f8e22017-06-29 17:06:53 -07001415 reset = diff_get_color_opt(o, DIFF_RESET);
Johannes Schindelinf7c3b4e2018-08-13 04:33:20 -07001416 if (!o->flags.dual_color_diffed_diffs)
1417 set_sign = NULL;
1418 else {
1419 char c = !len ? 0 : line[0];
1420
1421 set_sign = set;
1422 if (c == '-')
Johannes Schindelina7be92a2018-08-13 04:33:32 -07001423 set = diff_get_color_opt(o, DIFF_FILE_OLD_BOLD);
Johannes Schindelinf7c3b4e2018-08-13 04:33:20 -07001424 else if (c == '@')
1425 set = diff_get_color_opt(o, DIFF_FRAGINFO);
Johannes Schindelina7be92a2018-08-13 04:33:32 -07001426 else if (c == '+')
1427 set = diff_get_color_opt(o, DIFF_FILE_NEW_BOLD);
1428 else
1429 set = diff_get_color_opt(o, DIFF_CONTEXT_BOLD);
Johannes Schindelin0b91faa2018-08-13 04:33:24 -07001430 flags &= ~DIFF_SYMBOL_CONTENT_WS_MASK;
Johannes Schindelinf7c3b4e2018-08-13 04:33:20 -07001431 }
Stefan Beller7648b792018-08-17 13:43:52 -07001432 emit_line_ws_markup(o, set_sign, set, reset,
Stefan Bellerbc9feb02018-10-10 16:24:59 -07001433 OUTPUT_INDICATOR_NEW, line, len,
Stefan Beller091f8e22017-06-29 17:06:53 -07001434 flags & DIFF_SYMBOL_CONTENT_WS_MASK,
1435 flags & DIFF_SYMBOL_CONTENT_BLANK_LINE_EOF);
1436 break;
1437 case DIFF_SYMBOL_MINUS:
Stefan Beller86b452e2017-06-30 13:53:09 -07001438 switch (flags & (DIFF_SYMBOL_MOVED_LINE |
1439 DIFF_SYMBOL_MOVED_LINE_ALT |
1440 DIFF_SYMBOL_MOVED_LINE_UNINTERESTING)) {
1441 case DIFF_SYMBOL_MOVED_LINE |
1442 DIFF_SYMBOL_MOVED_LINE_ALT |
1443 DIFF_SYMBOL_MOVED_LINE_UNINTERESTING:
1444 set = diff_get_color_opt(o, DIFF_FILE_OLD_MOVED_ALT_DIM);
1445 break;
1446 case DIFF_SYMBOL_MOVED_LINE |
1447 DIFF_SYMBOL_MOVED_LINE_ALT:
Stefan Beller2e2d5ac2017-06-30 13:53:07 -07001448 set = diff_get_color_opt(o, DIFF_FILE_OLD_MOVED_ALT);
Stefan Beller86b452e2017-06-30 13:53:09 -07001449 break;
1450 case DIFF_SYMBOL_MOVED_LINE |
1451 DIFF_SYMBOL_MOVED_LINE_UNINTERESTING:
1452 set = diff_get_color_opt(o, DIFF_FILE_OLD_MOVED_DIM);
1453 break;
1454 case DIFF_SYMBOL_MOVED_LINE:
Stefan Beller2e2d5ac2017-06-30 13:53:07 -07001455 set = diff_get_color_opt(o, DIFF_FILE_OLD_MOVED);
Stefan Beller86b452e2017-06-30 13:53:09 -07001456 break;
1457 default:
Stefan Beller2e2d5ac2017-06-30 13:53:07 -07001458 set = diff_get_color_opt(o, DIFF_FILE_OLD);
Stefan Beller86b452e2017-06-30 13:53:09 -07001459 }
Stefan Beller091f8e22017-06-29 17:06:53 -07001460 reset = diff_get_color_opt(o, DIFF_RESET);
Johannes Schindelinf7c3b4e2018-08-13 04:33:20 -07001461 if (!o->flags.dual_color_diffed_diffs)
1462 set_sign = NULL;
1463 else {
1464 char c = !len ? 0 : line[0];
1465
1466 set_sign = set;
1467 if (c == '+')
Johannes Schindelina7be92a2018-08-13 04:33:32 -07001468 set = diff_get_color_opt(o, DIFF_FILE_NEW_DIM);
Johannes Schindelinf7c3b4e2018-08-13 04:33:20 -07001469 else if (c == '@')
1470 set = diff_get_color_opt(o, DIFF_FRAGINFO);
Johannes Schindelina7be92a2018-08-13 04:33:32 -07001471 else if (c == '-')
1472 set = diff_get_color_opt(o, DIFF_FILE_OLD_DIM);
1473 else
1474 set = diff_get_color_opt(o, DIFF_CONTEXT_DIM);
Johannes Schindelinf7c3b4e2018-08-13 04:33:20 -07001475 }
Stefan Beller7648b792018-08-17 13:43:52 -07001476 emit_line_ws_markup(o, set_sign, set, reset,
Stefan Bellerbc9feb02018-10-10 16:24:59 -07001477 OUTPUT_INDICATOR_OLD, line, len,
Stefan Beller091f8e22017-06-29 17:06:53 -07001478 flags & DIFF_SYMBOL_CONTENT_WS_MASK, 0);
1479 break;
Stefan Bellerff958672017-06-29 17:06:54 -07001480 case DIFF_SYMBOL_WORDS_PORCELAIN:
1481 context = diff_get_color_opt(o, DIFF_CONTEXT);
1482 reset = diff_get_color_opt(o, DIFF_RESET);
1483 emit_line(o, context, reset, line, len);
1484 fputs("~\n", o->file);
1485 break;
1486 case DIFF_SYMBOL_WORDS:
1487 context = diff_get_color_opt(o, DIFF_CONTEXT);
1488 reset = diff_get_color_opt(o, DIFF_RESET);
1489 /*
1490 * Skip the prefix character, if any. With
1491 * diff_suppress_blank_empty, there may be
1492 * none.
1493 */
1494 if (line[0] != '\n') {
1495 line++;
1496 len--;
1497 }
1498 emit_line(o, context, reset, line, len);
1499 break;
Stefan Beller3ee8b7b2017-06-29 17:06:56 -07001500 case DIFF_SYMBOL_FILEPAIR_PLUS:
1501 meta = diff_get_color_opt(o, DIFF_METAINFO);
1502 reset = diff_get_color_opt(o, DIFF_RESET);
1503 fprintf(o->file, "%s%s+++ %s%s%s\n", diff_line_prefix(o), meta,
1504 line, reset,
1505 strchr(line, ' ') ? "\t" : "");
1506 break;
1507 case DIFF_SYMBOL_FILEPAIR_MINUS:
1508 meta = diff_get_color_opt(o, DIFF_METAINFO);
1509 reset = diff_get_color_opt(o, DIFF_RESET);
1510 fprintf(o->file, "%s%s--- %s%s%s\n", diff_line_prefix(o), meta,
1511 line, reset,
1512 strchr(line, ' ') ? "\t" : "");
1513 break;
Stefan Beller4acaaa72017-06-29 17:06:58 -07001514 case DIFF_SYMBOL_BINARY_FILES:
Stefan Bellera29b0a12017-06-29 17:06:57 -07001515 case DIFF_SYMBOL_HEADER:
1516 fprintf(o->file, "%s", line);
1517 break;
Stefan Beller4eed0eb2017-06-29 17:07:01 -07001518 case DIFF_SYMBOL_BINARY_DIFF_HEADER:
1519 fprintf(o->file, "%sGIT binary patch\n", diff_line_prefix(o));
1520 break;
1521 case DIFF_SYMBOL_BINARY_DIFF_HEADER_DELTA:
1522 fprintf(o->file, "%sdelta %s\n", diff_line_prefix(o), line);
1523 break;
1524 case DIFF_SYMBOL_BINARY_DIFF_HEADER_LITERAL:
1525 fprintf(o->file, "%sliteral %s\n", diff_line_prefix(o), line);
1526 break;
1527 case DIFF_SYMBOL_BINARY_DIFF_FOOTER:
1528 fputs(diff_line_prefix(o), o->file);
1529 fputc('\n', o->file);
1530 break;
Stefan Beller5af6ea92017-06-29 17:06:59 -07001531 case DIFF_SYMBOL_REWRITE_DIFF:
1532 fraginfo = diff_get_color(o->use_color, DIFF_FRAGINFO);
1533 reset = diff_get_color_opt(o, DIFF_RESET);
1534 emit_line(o, fraginfo, reset, line, len);
1535 break;
Stefan Bellerf3597132017-06-29 17:07:00 -07001536 case DIFF_SYMBOL_SUBMODULE_ADD:
1537 set = diff_get_color_opt(o, DIFF_FILE_NEW);
1538 reset = diff_get_color_opt(o, DIFF_RESET);
1539 emit_line(o, set, reset, line, len);
1540 break;
1541 case DIFF_SYMBOL_SUBMODULE_DEL:
1542 set = diff_get_color_opt(o, DIFF_FILE_OLD);
1543 reset = diff_get_color_opt(o, DIFF_RESET);
1544 emit_line(o, set, reset, line, len);
1545 break;
1546 case DIFF_SYMBOL_SUBMODULE_UNTRACKED:
1547 fprintf(o->file, "%sSubmodule %s contains untracked content\n",
1548 diff_line_prefix(o), line);
1549 break;
1550 case DIFF_SYMBOL_SUBMODULE_MODIFIED:
1551 fprintf(o->file, "%sSubmodule %s contains modified content\n",
1552 diff_line_prefix(o), line);
1553 break;
Stefan Beller0911c472017-06-29 17:07:02 -07001554 case DIFF_SYMBOL_STATS_SUMMARY_NO_FILES:
1555 emit_line(o, "", "", " 0 files changed\n",
1556 strlen(" 0 files changed\n"));
1557 break;
1558 case DIFF_SYMBOL_STATS_SUMMARY_ABBREV:
1559 emit_line(o, "", "", " ...\n", strlen(" ...\n"));
1560 break;
Stefan Bellerbd033292017-06-29 17:07:03 -07001561 case DIFF_SYMBOL_WORD_DIFF:
1562 fprintf(o->file, "%.*s", len, line);
1563 break;
Stefan Beller30b7e1e2017-06-29 17:07:04 -07001564 case DIFF_SYMBOL_STAT_SEP:
1565 fputs(o->stat_sep, o->file);
1566 break;
Stefan Beller36a4cef2017-06-29 17:06:49 -07001567 default:
Johannes Schindelin033abf92018-05-02 11:38:39 +02001568 BUG("unknown diff symbol");
Stefan Beller36a4cef2017-06-29 17:06:49 -07001569 }
Stefan Beller0911c472017-06-29 17:07:02 -07001570 strbuf_release(&sb);
Stefan Beller36a4cef2017-06-29 17:06:49 -07001571}
1572
Stefan Bellere6e045f2017-06-29 17:07:06 -07001573static void emit_diff_symbol(struct diff_options *o, enum diff_symbol s,
1574 const char *line, int len, unsigned flags)
1575{
Phillip Wood21536d02018-11-23 11:16:57 +00001576 struct emitted_diff_symbol e = {line, len, flags, 0, 0, s};
Stefan Bellere6e045f2017-06-29 17:07:06 -07001577
1578 if (o->emitted_symbols)
1579 append_emitted_diff_symbol(o, &e);
1580 else
1581 emit_diff_symbol_from_struct(o, &e);
1582}
1583
Stefan Bellerf3597132017-06-29 17:07:00 -07001584void diff_emit_submodule_del(struct diff_options *o, const char *line)
1585{
1586 emit_diff_symbol(o, DIFF_SYMBOL_SUBMODULE_DEL, line, strlen(line), 0);
1587}
1588
1589void diff_emit_submodule_add(struct diff_options *o, const char *line)
1590{
1591 emit_diff_symbol(o, DIFF_SYMBOL_SUBMODULE_ADD, line, strlen(line), 0);
1592}
1593
1594void diff_emit_submodule_untracked(struct diff_options *o, const char *path)
1595{
1596 emit_diff_symbol(o, DIFF_SYMBOL_SUBMODULE_UNTRACKED,
1597 path, strlen(path), 0);
1598}
1599
1600void diff_emit_submodule_modified(struct diff_options *o, const char *path)
1601{
1602 emit_diff_symbol(o, DIFF_SYMBOL_SUBMODULE_MODIFIED,
1603 path, strlen(path), 0);
1604}
1605
1606void diff_emit_submodule_header(struct diff_options *o, const char *header)
1607{
1608 emit_diff_symbol(o, DIFF_SYMBOL_SUBMODULE_HEADER,
1609 header, strlen(header), 0);
1610}
1611
1612void diff_emit_submodule_error(struct diff_options *o, const char *err)
1613{
1614 emit_diff_symbol(o, DIFF_SYMBOL_SUBMODULE_ERROR, err, strlen(err), 0);
1615}
1616
1617void diff_emit_submodule_pipethrough(struct diff_options *o,
1618 const char *line, int len)
1619{
1620 emit_diff_symbol(o, DIFF_SYMBOL_SUBMODULE_PIPETHROUGH, line, len, 0);
1621}
1622
Junio C Hamano6957eb92009-09-14 18:44:01 -07001623static int new_blank_line_at_eof(struct emit_callback *ecbdata, const char *line, int len)
1624{
1625 if (!((ecbdata->ws_rule & WS_BLANK_AT_EOF) &&
1626 ecbdata->blank_at_eof_in_preimage &&
1627 ecbdata->blank_at_eof_in_postimage &&
1628 ecbdata->blank_at_eof_in_preimage <= ecbdata->lno_in_preimage &&
1629 ecbdata->blank_at_eof_in_postimage <= ecbdata->lno_in_postimage))
1630 return 0;
Junio C Hamano018cff72009-09-14 18:44:01 -07001631 return ws_blank_line(line, len, ecbdata->ws_rule);
Junio C Hamano6957eb92009-09-14 18:44:01 -07001632}
1633
Jeff Kinge04df612019-02-14 00:48:13 -05001634static void emit_add_line(struct emit_callback *ecbdata,
Junio C Hamano018cff72009-09-14 18:44:01 -07001635 const char *line, int len)
Junio C Hamano6957eb92009-09-14 18:44:01 -07001636{
Stefan Beller091f8e22017-06-29 17:06:53 -07001637 unsigned flags = WSEH_NEW | ecbdata->ws_rule;
1638 if (new_blank_line_at_eof(ecbdata, line, len))
1639 flags |= DIFF_SYMBOL_CONTENT_BLANK_LINE_EOF;
1640
1641 emit_diff_symbol(ecbdata->opt, DIFF_SYMBOL_PLUS, line, len, flags);
Junio C Hamano6957eb92009-09-14 18:44:01 -07001642}
Junio C Hamano6957eb92009-09-14 18:44:01 -07001643
Jeff Kinge04df612019-02-14 00:48:13 -05001644static void emit_del_line(struct emit_callback *ecbdata,
Junio C Hamano0e383e12015-05-26 09:56:33 -07001645 const char *line, int len)
1646{
Stefan Beller091f8e22017-06-29 17:06:53 -07001647 unsigned flags = WSEH_OLD | ecbdata->ws_rule;
1648 emit_diff_symbol(ecbdata->opt, DIFF_SYMBOL_MINUS, line, len, flags);
Junio C Hamano0e383e12015-05-26 09:56:33 -07001649}
1650
Jeff Kinge04df612019-02-14 00:48:13 -05001651static void emit_context_line(struct emit_callback *ecbdata,
Junio C Hamano0e383e12015-05-26 09:56:33 -07001652 const char *line, int len)
1653{
Stefan Beller091f8e22017-06-29 17:06:53 -07001654 unsigned flags = WSEH_CONTEXT | ecbdata->ws_rule;
1655 emit_diff_symbol(ecbdata->opt, DIFF_SYMBOL_CONTEXT, line, len, flags);
Junio C Hamano6957eb92009-09-14 18:44:01 -07001656}
1657
Bert Wesarg89cb73a2009-11-27 07:55:18 +01001658static void emit_hunk_header(struct emit_callback *ecbdata,
1659 const char *line, int len)
1660{
Jeff King8dbf3eb2015-05-27 16:48:46 -04001661 const char *context = diff_get_color(ecbdata->color_diff, DIFF_CONTEXT);
Bert Wesarg89cb73a2009-11-27 07:55:18 +01001662 const char *frag = diff_get_color(ecbdata->color_diff, DIFF_FRAGINFO);
1663 const char *func = diff_get_color(ecbdata->color_diff, DIFF_FUNCINFO);
1664 const char *reset = diff_get_color(ecbdata->color_diff, DIFF_RESET);
Johannes Schindelinf7c3b4e2018-08-13 04:33:20 -07001665 const char *reverse = ecbdata->color_diff ? GIT_COLOR_REVERSE : "";
Bert Wesarg89cb73a2009-11-27 07:55:18 +01001666 static const char atat[2] = { '@', '@' };
1667 const char *cp, *ep;
Bo Yang2efcc972010-05-29 23:32:05 +08001668 struct strbuf msgbuf = STRBUF_INIT;
1669 int org_len = len;
1670 int i = 1;
Bert Wesarg89cb73a2009-11-27 07:55:18 +01001671
1672 /*
1673 * As a hunk header must begin with "@@ -<old>, +<new> @@",
1674 * it always is at least 10 bytes long.
1675 */
1676 if (len < 10 ||
1677 memcmp(line, atat, 2) ||
1678 !(ep = memmem(line + 2, len - 2, atat, 2))) {
Stefan Bellerc64b4202017-06-29 17:06:50 -07001679 emit_diff_symbol(ecbdata->opt,
Stefan Beller091f8e22017-06-29 17:06:53 -07001680 DIFF_SYMBOL_CONTEXT_MARKER, line, len, 0);
Bert Wesarg89cb73a2009-11-27 07:55:18 +01001681 return;
1682 }
1683 ep += 2; /* skip over @@ */
1684
1685 /* The hunk header in fraginfo color */
Johannes Schindelinf7c3b4e2018-08-13 04:33:20 -07001686 if (ecbdata->opt->flags.dual_color_diffed_diffs)
1687 strbuf_addstr(&msgbuf, reverse);
René Scharfecedc61a2014-07-17 01:38:18 +02001688 strbuf_addstr(&msgbuf, frag);
Thomas Gummerer430be362019-07-11 17:08:48 +01001689 if (ecbdata->opt->flags.suppress_hunk_header_line_count)
1690 strbuf_add(&msgbuf, atat, sizeof(atat));
1691 else
1692 strbuf_add(&msgbuf, line, ep - line);
René Scharfecedc61a2014-07-17 01:38:18 +02001693 strbuf_addstr(&msgbuf, reset);
Bo Yang2efcc972010-05-29 23:32:05 +08001694
1695 /*
1696 * trailing "\r\n"
1697 */
1698 for ( ; i < 3; i++)
1699 if (line[len - i] == '\r' || line[len - i] == '\n')
1700 len--;
Bert Wesarg89cb73a2009-11-27 07:55:18 +01001701
1702 /* blank before the func header */
1703 for (cp = ep; ep - line < len; ep++)
1704 if (*ep != ' ' && *ep != '\t')
1705 break;
Bo Yang2efcc972010-05-29 23:32:05 +08001706 if (ep != cp) {
Jeff King8dbf3eb2015-05-27 16:48:46 -04001707 strbuf_addstr(&msgbuf, context);
Bo Yang2efcc972010-05-29 23:32:05 +08001708 strbuf_add(&msgbuf, cp, ep - cp);
René Scharfecedc61a2014-07-17 01:38:18 +02001709 strbuf_addstr(&msgbuf, reset);
Bo Yang2efcc972010-05-29 23:32:05 +08001710 }
Bert Wesarg89cb73a2009-11-27 07:55:18 +01001711
Bo Yang2efcc972010-05-29 23:32:05 +08001712 if (ep < line + len) {
René Scharfecedc61a2014-07-17 01:38:18 +02001713 strbuf_addstr(&msgbuf, func);
Bo Yang2efcc972010-05-29 23:32:05 +08001714 strbuf_add(&msgbuf, ep, line + len - ep);
René Scharfecedc61a2014-07-17 01:38:18 +02001715 strbuf_addstr(&msgbuf, reset);
Bo Yang2efcc972010-05-29 23:32:05 +08001716 }
1717
1718 strbuf_add(&msgbuf, line + len, org_len - len);
Stefan Bellerdfb77282017-06-29 17:06:47 -07001719 strbuf_complete_line(&msgbuf);
Stefan Beller68abc6f2017-06-29 17:06:51 -07001720 emit_diff_symbol(ecbdata->opt,
Stefan Beller091f8e22017-06-29 17:06:53 -07001721 DIFF_SYMBOL_CONTEXT_FRAGINFO, msgbuf.buf, msgbuf.len, 0);
Bo Yang2efcc972010-05-29 23:32:05 +08001722 strbuf_release(&msgbuf);
Bert Wesarg89cb73a2009-11-27 07:55:18 +01001723}
1724
Nguyễn Thái Ngọc Duy3b335762018-12-09 11:25:21 +01001725static struct diff_tempfile *claim_diff_tempfile(void)
1726{
Jeff King479b0ae2009-01-22 00:59:56 -05001727 int i;
1728 for (i = 0; i < ARRAY_SIZE(diff_temp); i++)
1729 if (!diff_temp[i].name)
1730 return diff_temp + i;
Johannes Schindelin033abf92018-05-02 11:38:39 +02001731 BUG("diff is failing to clean up its tempfiles");
Jeff King479b0ae2009-01-22 00:59:56 -05001732}
1733
Jeff King479b0ae2009-01-22 00:59:56 -05001734static void remove_tempfile(void)
1735{
1736 int i;
Nazri Ramliya8344ab2009-02-12 21:36:14 +08001737 for (i = 0; i < ARRAY_SIZE(diff_temp); i++) {
Jeff King076aa2c2017-09-05 08:15:08 -04001738 if (is_tempfile_active(diff_temp[i].tempfile))
Michael Haggerty284098f2015-08-12 19:12:01 +02001739 delete_tempfile(&diff_temp[i].tempfile);
Nazri Ramliya8344ab2009-02-12 21:36:14 +08001740 diff_temp[i].name = NULL;
1741 }
Jeff King479b0ae2009-01-22 00:59:56 -05001742}
1743
Stefan Beller5af6ea92017-06-29 17:06:59 -07001744static void add_line_count(struct strbuf *out, int count)
Junio C Hamano6973dca2006-04-21 23:57:45 -07001745{
1746 switch (count) {
1747 case 0:
Stefan Beller5af6ea92017-06-29 17:06:59 -07001748 strbuf_addstr(out, "0,0");
Junio C Hamano6973dca2006-04-21 23:57:45 -07001749 break;
1750 case 1:
Stefan Beller5af6ea92017-06-29 17:06:59 -07001751 strbuf_addstr(out, "1");
Junio C Hamano6973dca2006-04-21 23:57:45 -07001752 break;
1753 default:
Stefan Beller5af6ea92017-06-29 17:06:59 -07001754 strbuf_addf(out, "1,%d", count);
Junio C Hamano6973dca2006-04-21 23:57:45 -07001755 break;
1756 }
1757}
1758
Junio C Hamano7f7ee2f2009-09-14 18:44:01 -07001759static void emit_rewrite_lines(struct emit_callback *ecb,
1760 int prefix, const char *data, int size)
Junio C Hamano6973dca2006-04-21 23:57:45 -07001761{
Junio C Hamano7f7ee2f2009-09-14 18:44:01 -07001762 const char *endp = NULL;
Junio C Hamano7f7ee2f2009-09-14 18:44:01 -07001763
1764 while (0 < size) {
1765 int len;
1766
1767 endp = memchr(data, '\n', size);
1768 len = endp ? (endp - data + 1) : size;
1769 if (prefix != '+') {
1770 ecb->lno_in_preimage++;
Jeff Kinge04df612019-02-14 00:48:13 -05001771 emit_del_line(ecb, data, len);
Junio C Hamano7f7ee2f2009-09-14 18:44:01 -07001772 } else {
1773 ecb->lno_in_postimage++;
Jeff Kinge04df612019-02-14 00:48:13 -05001774 emit_add_line(ecb, data, len);
Johannes Schindelin13e36ec2007-02-20 15:08:46 +01001775 }
Junio C Hamano7f7ee2f2009-09-14 18:44:01 -07001776 size -= len;
1777 data += len;
Junio C Hamano6973dca2006-04-21 23:57:45 -07001778 }
Stefan Bellerb9cbfde2017-06-29 17:06:52 -07001779 if (!endp)
Stefan Beller091f8e22017-06-29 17:06:53 -07001780 emit_diff_symbol(ecb->opt, DIFF_SYMBOL_NO_LF_EOF, NULL, 0, 0);
Junio C Hamano6973dca2006-04-21 23:57:45 -07001781}
1782
1783static void emit_rewrite_diff(const char *name_a,
1784 const char *name_b,
1785 struct diff_filespec *one,
Johannes Schindelin13e36ec2007-02-20 15:08:46 +01001786 struct diff_filespec *two,
Jeff Kingd9bae1a2010-04-01 20:12:15 -04001787 struct userdiff_driver *textconv_one,
1788 struct userdiff_driver *textconv_two,
Johannes Schindelineab9a402007-12-18 19:32:14 +00001789 struct diff_options *o)
Junio C Hamano6973dca2006-04-21 23:57:45 -07001790{
1791 int lc_a, lc_b;
Junio C Hamanod5625092007-12-26 17:13:36 -08001792 static struct strbuf a_name = STRBUF_INIT, b_name = STRBUF_INIT;
Junio C Hamanoa5a818e2008-08-18 20:08:09 -07001793 const char *a_prefix, *b_prefix;
Jeff King840383b2010-04-01 20:09:26 -04001794 char *data_one, *data_two;
Jeff King3aa1f7c2008-12-09 03:13:21 -05001795 size_t size_one, size_two;
Junio C Hamano7f7ee2f2009-09-14 18:44:01 -07001796 struct emit_callback ecbdata;
Stefan Beller5af6ea92017-06-29 17:06:59 -07001797 struct strbuf out = STRBUF_INIT;
Junio C Hamanoa5a818e2008-08-18 20:08:09 -07001798
Brandon Williams0d1e0e72017-10-31 11:19:11 -07001799 if (diff_mnemonic_prefix && o->flags.reverse_diff) {
Junio C Hamanoa5a818e2008-08-18 20:08:09 -07001800 a_prefix = o->b_prefix;
1801 b_prefix = o->a_prefix;
1802 } else {
1803 a_prefix = o->a_prefix;
1804 b_prefix = o->b_prefix;
1805 }
Junio C Hamano1a9eb3b2006-09-22 16:17:58 -07001806
Junio C Hamano8a13bec2007-02-24 01:42:06 -08001807 name_a += (*name_a == '/');
1808 name_b += (*name_b == '/');
Junio C Hamano1a9eb3b2006-09-22 16:17:58 -07001809
Junio C Hamanod5625092007-12-26 17:13:36 -08001810 strbuf_reset(&a_name);
1811 strbuf_reset(&b_name);
Junio C Hamanoa5a818e2008-08-18 20:08:09 -07001812 quote_two_c_style(&a_name, a_prefix, name_a, 0);
1813 quote_two_c_style(&b_name, b_prefix, name_b, 0);
Junio C Hamanod5625092007-12-26 17:13:36 -08001814
Nguyễn Thái Ngọc Duy6afaf802018-09-21 17:57:22 +02001815 size_one = fill_textconv(o->repo, textconv_one, one, &data_one);
1816 size_two = fill_textconv(o->repo, textconv_two, two, &data_two);
Jeff King3aa1f7c2008-12-09 03:13:21 -05001817
Junio C Hamanod91ba8f2009-09-15 11:21:10 -07001818 memset(&ecbdata, 0, sizeof(ecbdata));
Jeff Kingdaa0c3d2011-08-17 22:04:23 -07001819 ecbdata.color_diff = want_color(o->use_color);
Nguyễn Thái Ngọc Duy26d024e2018-09-21 17:57:37 +02001820 ecbdata.ws_rule = whitespace_rule(o->repo->index, name_b);
Bo Yanga3c158d2010-05-26 15:08:02 +08001821 ecbdata.opt = o;
Junio C Hamanod91ba8f2009-09-15 11:21:10 -07001822 if (ecbdata.ws_rule & WS_BLANK_AT_EOF) {
1823 mmfile_t mf1, mf2;
1824 mf1.ptr = (char *)data_one;
1825 mf2.ptr = (char *)data_two;
1826 mf1.size = size_one;
1827 mf2.size = size_two;
1828 check_blank_at_eof(&mf1, &mf2, &ecbdata);
1829 }
1830 ecbdata.lno_in_preimage = 1;
1831 ecbdata.lno_in_postimage = 1;
1832
Jeff King3aa1f7c2008-12-09 03:13:21 -05001833 lc_a = count_lines(data_one, size_one);
1834 lc_b = count_lines(data_two, size_two);
Stefan Beller3ee8b7b2017-06-29 17:06:56 -07001835
1836 emit_diff_symbol(o, DIFF_SYMBOL_FILEPAIR_MINUS,
1837 a_name.buf, a_name.len, 0);
1838 emit_diff_symbol(o, DIFF_SYMBOL_FILEPAIR_PLUS,
1839 b_name.buf, b_name.len, 0);
1840
Stefan Beller5af6ea92017-06-29 17:06:59 -07001841 strbuf_addstr(&out, "@@ -");
Junio C Hamano467ddc12011-02-28 16:11:55 -08001842 if (!o->irreversible_delete)
Stefan Beller5af6ea92017-06-29 17:06:59 -07001843 add_line_count(&out, lc_a);
Junio C Hamano467ddc12011-02-28 16:11:55 -08001844 else
Stefan Beller5af6ea92017-06-29 17:06:59 -07001845 strbuf_addstr(&out, "?,?");
1846 strbuf_addstr(&out, " +");
1847 add_line_count(&out, lc_b);
1848 strbuf_addstr(&out, " @@\n");
1849 emit_diff_symbol(o, DIFF_SYMBOL_REWRITE_DIFF, out.buf, out.len, 0);
1850 strbuf_release(&out);
1851
Junio C Hamano467ddc12011-02-28 16:11:55 -08001852 if (lc_a && !o->irreversible_delete)
Junio C Hamanod91ba8f2009-09-15 11:21:10 -07001853 emit_rewrite_lines(&ecbdata, '-', data_one, size_one);
Junio C Hamano6973dca2006-04-21 23:57:45 -07001854 if (lc_b)
Junio C Hamanod91ba8f2009-09-15 11:21:10 -07001855 emit_rewrite_lines(&ecbdata, '+', data_two, size_two);
Jeff Kingb76c0562010-04-01 20:04:14 -04001856 if (textconv_one)
Junio C Hamanoaed6ca52010-04-08 23:30:49 -07001857 free((char *)data_one);
Jeff Kingb76c0562010-04-01 20:04:14 -04001858 if (textconv_two)
Junio C Hamanoaed6ca52010-04-08 23:30:49 -07001859 free((char *)data_two);
Junio C Hamano6973dca2006-04-21 23:57:45 -07001860}
1861
Johannes Schindelinf59a59e2006-07-28 23:56:15 +02001862struct diff_words_buffer {
1863 mmfile_t text;
Ramsay Jones071bcaa2017-09-21 17:49:38 +01001864 unsigned long alloc;
Johannes Schindelin2e5d2002009-01-17 17:29:44 +01001865 struct diff_words_orig {
1866 const char *begin, *end;
1867 } *orig;
1868 int orig_nr, orig_alloc;
Johannes Schindelinf59a59e2006-07-28 23:56:15 +02001869};
1870
1871static void diff_words_append(char *line, unsigned long len,
1872 struct diff_words_buffer *buffer)
1873{
Johannes Schindelin23c15752009-01-17 17:29:43 +01001874 ALLOC_GROW(buffer->text.ptr, buffer->text.size + len, buffer->alloc);
Johannes Schindelinf59a59e2006-07-28 23:56:15 +02001875 line++;
1876 len--;
1877 memcpy(buffer->text.ptr + buffer->text.size, line, len);
1878 buffer->text.size += len;
Johannes Schindelin2b6a5412009-01-17 17:29:45 +01001879 buffer->text.ptr[buffer->text.size] = '\0';
Johannes Schindelinf59a59e2006-07-28 23:56:15 +02001880}
1881
Jonathan Nieder9cba13c2011-03-16 02:08:34 -05001882struct diff_words_style_elem {
Thomas Rast882749a2010-04-14 17:59:06 +02001883 const char *prefix;
1884 const char *suffix;
1885 const char *color; /* NULL; filled in by the setup code if
1886 * color is enabled */
1887};
1888
Jonathan Nieder9cba13c2011-03-16 02:08:34 -05001889struct diff_words_style {
Thomas Rast882749a2010-04-14 17:59:06 +02001890 enum diff_words_type type;
Brandon Williams63a01c32018-02-14 10:59:39 -08001891 struct diff_words_style_elem new_word, old_word, ctx;
Thomas Rast882749a2010-04-14 17:59:06 +02001892 const char *newline;
1893};
1894
Stephen Boydc2e86ad2011-03-22 00:51:05 -07001895static struct diff_words_style diff_words_styles[] = {
Thomas Rast882749a2010-04-14 17:59:06 +02001896 { DIFF_WORDS_PORCELAIN, {"+", "\n"}, {"-", "\n"}, {" ", "\n"}, "~\n" },
1897 { DIFF_WORDS_PLAIN, {"{+", "+}"}, {"[-", "-]"}, {"", ""}, "\n" },
1898 { DIFF_WORDS_COLOR, {"", ""}, {"", ""}, {"", ""}, "\n" }
1899};
1900
Johannes Schindelinf59a59e2006-07-28 23:56:15 +02001901struct diff_words_data {
Johannes Schindelinf59a59e2006-07-28 23:56:15 +02001902 struct diff_words_buffer minus, plus;
Johannes Schindelin2e5d2002009-01-17 17:29:44 +01001903 const char *current_plus;
Bo Yang4297c0a2010-05-29 23:32:06 +08001904 int last_minus;
1905 struct diff_options *opt;
Johannes Schindelin2b6a5412009-01-17 17:29:45 +01001906 regex_t *word_regex;
Thomas Rast882749a2010-04-14 17:59:06 +02001907 enum diff_words_type type;
1908 struct diff_words_style *style;
Johannes Schindelinf59a59e2006-07-28 23:56:15 +02001909};
1910
Stefan Bellerbd033292017-06-29 17:07:03 -07001911static int fn_out_diff_words_write_helper(struct diff_options *o,
Thomas Rast882749a2010-04-14 17:59:06 +02001912 struct diff_words_style_elem *st_el,
1913 const char *newline,
Stefan Bellerbd033292017-06-29 17:07:03 -07001914 size_t count, const char *buf)
Thomas Rast882749a2010-04-14 17:59:06 +02001915{
Bo Yang4297c0a2010-05-29 23:32:06 +08001916 int print = 0;
Stefan Bellerbd033292017-06-29 17:07:03 -07001917 struct strbuf sb = STRBUF_INIT;
Bo Yang4297c0a2010-05-29 23:32:06 +08001918
Thomas Rast882749a2010-04-14 17:59:06 +02001919 while (count) {
1920 char *p = memchr(buf, '\n', count);
Bo Yang4297c0a2010-05-29 23:32:06 +08001921 if (print)
Stefan Bellerbd033292017-06-29 17:07:03 -07001922 strbuf_addstr(&sb, diff_line_prefix(o));
1923
Thomas Rast882749a2010-04-14 17:59:06 +02001924 if (p != buf) {
Stefan Bellerbd033292017-06-29 17:07:03 -07001925 const char *reset = st_el->color && *st_el->color ?
1926 GIT_COLOR_RESET : NULL;
1927 if (st_el->color && *st_el->color)
1928 strbuf_addstr(&sb, st_el->color);
1929 strbuf_addstr(&sb, st_el->prefix);
1930 strbuf_add(&sb, buf, p ? p - buf : count);
1931 strbuf_addstr(&sb, st_el->suffix);
1932 if (reset)
1933 strbuf_addstr(&sb, reset);
Thomas Rast882749a2010-04-14 17:59:06 +02001934 }
1935 if (!p)
Stefan Bellerbd033292017-06-29 17:07:03 -07001936 goto out;
1937
1938 strbuf_addstr(&sb, newline);
Thomas Rast882749a2010-04-14 17:59:06 +02001939 count -= p + 1 - buf;
1940 buf = p + 1;
Bo Yang4297c0a2010-05-29 23:32:06 +08001941 print = 1;
Stefan Bellerbd033292017-06-29 17:07:03 -07001942 if (count) {
1943 emit_diff_symbol(o, DIFF_SYMBOL_WORD_DIFF,
1944 sb.buf, sb.len, 0);
1945 strbuf_reset(&sb);
1946 }
Thomas Rast882749a2010-04-14 17:59:06 +02001947 }
Stefan Bellerbd033292017-06-29 17:07:03 -07001948
1949out:
1950 if (sb.len)
1951 emit_diff_symbol(o, DIFF_SYMBOL_WORD_DIFF,
1952 sb.buf, sb.len, 0);
1953 strbuf_release(&sb);
Thomas Rast882749a2010-04-14 17:59:06 +02001954 return 0;
1955}
1956
Bo Yang4297c0a2010-05-29 23:32:06 +08001957/*
1958 * '--color-words' algorithm can be described as:
1959 *
René Genz56217602017-04-30 17:48:27 +02001960 * 1. collect the minus/plus lines of a diff hunk, divided into
Bo Yang4297c0a2010-05-29 23:32:06 +08001961 * minus-lines and plus-lines;
1962 *
1963 * 2. break both minus-lines and plus-lines into words and
1964 * place them into two mmfile_t with one word for each line;
1965 *
1966 * 3. use xdiff to run diff on the two mmfile_t to get the words level diff;
1967 *
1968 * And for the common parts of the both file, we output the plus side text.
1969 * diff_words->current_plus is used to trace the current position of the plus file
1970 * which printed. diff_words->last_minus is used to trace the last minus word
1971 * printed.
1972 *
1973 * For '--graph' to work with '--color-words', we need to output the graph prefix
1974 * on each line of color words output. Generally, there are two conditions on
1975 * which we should output the prefix.
1976 *
1977 * 1. diff_words->last_minus == 0 &&
1978 * diff_words->current_plus == diff_words->plus.text.ptr
1979 *
1980 * that is: the plus text must start as a new line, and if there is no minus
1981 * word printed, a graph prefix must be printed.
1982 *
1983 * 2. diff_words->current_plus > diff_words->plus.text.ptr &&
1984 * *(diff_words->current_plus - 1) == '\n'
1985 *
1986 * that is: a graph prefix must be printed following a '\n'
1987 */
1988static int color_words_output_graph_prefix(struct diff_words_data *diff_words)
1989{
1990 if ((diff_words->last_minus == 0 &&
1991 diff_words->current_plus == diff_words->plus.text.ptr) ||
1992 (diff_words->current_plus > diff_words->plus.text.ptr &&
1993 *(diff_words->current_plus - 1) == '\n')) {
1994 return 1;
1995 } else {
1996 return 0;
1997 }
1998}
1999
Jeff King7c61e252018-11-02 02:37:18 -04002000static void fn_out_diff_words_aux(void *priv,
2001 long minus_first, long minus_len,
2002 long plus_first, long plus_len,
2003 const char *func, long funclen)
Johannes Schindelinf59a59e2006-07-28 23:56:15 +02002004{
2005 struct diff_words_data *diff_words = priv;
Thomas Rast882749a2010-04-14 17:59:06 +02002006 struct diff_words_style *style = diff_words->style;
Johannes Schindelin2e5d2002009-01-17 17:29:44 +01002007 const char *minus_begin, *minus_end, *plus_begin, *plus_end;
Bo Yang4297c0a2010-05-29 23:32:06 +08002008 struct diff_options *opt = diff_words->opt;
John Keeping30997bb2013-02-07 20:15:27 +00002009 const char *line_prefix;
Johannes Schindelinf59a59e2006-07-28 23:56:15 +02002010
Bo Yang4297c0a2010-05-29 23:32:06 +08002011 assert(opt);
John Keeping30997bb2013-02-07 20:15:27 +00002012 line_prefix = diff_line_prefix(opt);
Bo Yang4297c0a2010-05-29 23:32:06 +08002013
Johannes Schindelin2e5d2002009-01-17 17:29:44 +01002014 /* POSIX requires that first be decremented by one if len == 0... */
2015 if (minus_len) {
2016 minus_begin = diff_words->minus.orig[minus_first].begin;
2017 minus_end =
2018 diff_words->minus.orig[minus_first + minus_len - 1].end;
2019 } else
2020 minus_begin = minus_end =
2021 diff_words->minus.orig[minus_first].end;
2022
2023 if (plus_len) {
2024 plus_begin = diff_words->plus.orig[plus_first].begin;
2025 plus_end = diff_words->plus.orig[plus_first + plus_len - 1].end;
2026 } else
2027 plus_begin = plus_end = diff_words->plus.orig[plus_first].end;
2028
Bo Yang4297c0a2010-05-29 23:32:06 +08002029 if (color_words_output_graph_prefix(diff_words)) {
2030 fputs(line_prefix, diff_words->opt->file);
2031 }
2032 if (diff_words->current_plus != plus_begin) {
Stefan Bellerbd033292017-06-29 17:07:03 -07002033 fn_out_diff_words_write_helper(diff_words->opt,
Thomas Rast882749a2010-04-14 17:59:06 +02002034 &style->ctx, style->newline,
2035 plus_begin - diff_words->current_plus,
Stefan Bellerbd033292017-06-29 17:07:03 -07002036 diff_words->current_plus);
Bo Yang4297c0a2010-05-29 23:32:06 +08002037 }
2038 if (minus_begin != minus_end) {
Stefan Bellerbd033292017-06-29 17:07:03 -07002039 fn_out_diff_words_write_helper(diff_words->opt,
Brandon Williams63a01c32018-02-14 10:59:39 -08002040 &style->old_word, style->newline,
Stefan Bellerbd033292017-06-29 17:07:03 -07002041 minus_end - minus_begin, minus_begin);
Bo Yang4297c0a2010-05-29 23:32:06 +08002042 }
2043 if (plus_begin != plus_end) {
Stefan Bellerbd033292017-06-29 17:07:03 -07002044 fn_out_diff_words_write_helper(diff_words->opt,
Brandon Williams63a01c32018-02-14 10:59:39 -08002045 &style->new_word, style->newline,
Stefan Bellerbd033292017-06-29 17:07:03 -07002046 plus_end - plus_begin, plus_begin);
Bo Yang4297c0a2010-05-29 23:32:06 +08002047 }
Johannes Schindelin2e5d2002009-01-17 17:29:44 +01002048
2049 diff_words->current_plus = plus_end;
Bo Yang4297c0a2010-05-29 23:32:06 +08002050 diff_words->last_minus = minus_first;
Johannes Schindelinf59a59e2006-07-28 23:56:15 +02002051}
2052
Johannes Schindelin2b6a5412009-01-17 17:29:45 +01002053/* This function starts looking at *begin, and returns 0 iff a word was found. */
2054static int find_word_boundaries(mmfile_t *buffer, regex_t *word_regex,
2055 int *begin, int *end)
2056{
Phillip Wood0324e8f2021-05-04 09:27:34 +00002057 while (word_regex && *begin < buffer->size) {
Johannes Schindelin2b6a5412009-01-17 17:29:45 +01002058 regmatch_t match[1];
Johannes Schindelinb7d36ff2016-09-21 20:24:14 +02002059 if (!regexec_buf(word_regex, buffer->ptr + *begin,
2060 buffer->size - *begin, 1, match, 0)) {
Johannes Schindelin2b6a5412009-01-17 17:29:45 +01002061 char *p = memchr(buffer->ptr + *begin + match[0].rm_so,
2062 '\n', match[0].rm_eo - match[0].rm_so);
2063 *end = p ? p - buffer->ptr : match[0].rm_eo + *begin;
2064 *begin += match[0].rm_so;
Phillip Wood0324e8f2021-05-04 09:27:34 +00002065 if (*begin == *end)
2066 (*begin)++;
2067 else
2068 return *begin > *end;
2069 } else {
2070 return -1;
Johannes Schindelin2b6a5412009-01-17 17:29:45 +01002071 }
Johannes Schindelinf59a59e2006-07-28 23:56:15 +02002072 }
2073
Johannes Schindelin2b6a5412009-01-17 17:29:45 +01002074 /* find the next word */
2075 while (*begin < buffer->size && isspace(buffer->ptr[*begin]))
2076 (*begin)++;
2077 if (*begin >= buffer->size)
2078 return -1;
2079
2080 /* find the end of the word */
2081 *end = *begin + 1;
2082 while (*end < buffer->size && !isspace(buffer->ptr[*end]))
2083 (*end)++;
2084
2085 return 0;
2086}
2087
Johannes Schindelin23c15752009-01-17 17:29:43 +01002088/*
Johannes Schindelin2e5d2002009-01-17 17:29:44 +01002089 * This function splits the words in buffer->text, stores the list with
2090 * newline separator into out, and saves the offsets of the original words
2091 * in buffer->orig.
Johannes Schindelin23c15752009-01-17 17:29:43 +01002092 */
Johannes Schindelin2b6a5412009-01-17 17:29:45 +01002093static void diff_words_fill(struct diff_words_buffer *buffer, mmfile_t *out,
2094 regex_t *word_regex)
Johannes Schindelin23c15752009-01-17 17:29:43 +01002095{
Johannes Schindelin2e5d2002009-01-17 17:29:44 +01002096 int i, j;
Johannes Schindelin2b6a5412009-01-17 17:29:45 +01002097 long alloc = 0;
Johannes Schindelin2e5d2002009-01-17 17:29:44 +01002098
2099 out->size = 0;
Johannes Schindelin2b6a5412009-01-17 17:29:45 +01002100 out->ptr = NULL;
Johannes Schindelin2e5d2002009-01-17 17:29:44 +01002101
2102 /* fake an empty "0th" word */
2103 ALLOC_GROW(buffer->orig, 1, buffer->orig_alloc);
2104 buffer->orig[0].begin = buffer->orig[0].end = buffer->text.ptr;
2105 buffer->orig_nr = 1;
2106
2107 for (i = 0; i < buffer->text.size; i++) {
Johannes Schindelin2b6a5412009-01-17 17:29:45 +01002108 if (find_word_boundaries(&buffer->text, word_regex, &i, &j))
2109 return;
Johannes Schindelin2e5d2002009-01-17 17:29:44 +01002110
2111 /* store original boundaries */
2112 ALLOC_GROW(buffer->orig, buffer->orig_nr + 1,
2113 buffer->orig_alloc);
2114 buffer->orig[buffer->orig_nr].begin = buffer->text.ptr + i;
2115 buffer->orig[buffer->orig_nr].end = buffer->text.ptr + j;
2116 buffer->orig_nr++;
2117
2118 /* store one word */
Johannes Schindelin2b6a5412009-01-17 17:29:45 +01002119 ALLOC_GROW(out->ptr, out->size + j - i + 1, alloc);
Johannes Schindelin2e5d2002009-01-17 17:29:44 +01002120 memcpy(out->ptr + out->size, buffer->text.ptr + i, j - i);
2121 out->ptr[out->size + j - i] = '\n';
2122 out->size += j - i + 1;
2123
2124 i = j - 1;
Johannes Schindelinf59a59e2006-07-28 23:56:15 +02002125 }
2126}
2127
2128/* this executes the word diff on the accumulated buffers */
2129static void diff_words_show(struct diff_words_data *diff_words)
2130{
2131 xpparam_t xpp;
2132 xdemitconf_t xecfg;
Johannes Schindelinf59a59e2006-07-28 23:56:15 +02002133 mmfile_t minus, plus;
Thomas Rast882749a2010-04-14 17:59:06 +02002134 struct diff_words_style *style = diff_words->style;
Johannes Schindelinf59a59e2006-07-28 23:56:15 +02002135
Bo Yang4297c0a2010-05-29 23:32:06 +08002136 struct diff_options *opt = diff_words->opt;
John Keeping30997bb2013-02-07 20:15:27 +00002137 const char *line_prefix;
Bo Yang4297c0a2010-05-29 23:32:06 +08002138
2139 assert(opt);
John Keeping30997bb2013-02-07 20:15:27 +00002140 line_prefix = diff_line_prefix(opt);
Bo Yang4297c0a2010-05-29 23:32:06 +08002141
Johannes Schindelin2e5d2002009-01-17 17:29:44 +01002142 /* special case: only removal */
2143 if (!diff_words->plus.text.size) {
Stefan Bellerbd033292017-06-29 17:07:03 -07002144 emit_diff_symbol(diff_words->opt, DIFF_SYMBOL_WORD_DIFF,
2145 line_prefix, strlen(line_prefix), 0);
2146 fn_out_diff_words_write_helper(diff_words->opt,
Brandon Williams63a01c32018-02-14 10:59:39 -08002147 &style->old_word, style->newline,
Bo Yang4297c0a2010-05-29 23:32:06 +08002148 diff_words->minus.text.size,
Stefan Bellerbd033292017-06-29 17:07:03 -07002149 diff_words->minus.text.ptr);
Johannes Schindelin2e5d2002009-01-17 17:29:44 +01002150 diff_words->minus.text.size = 0;
2151 return;
2152 }
2153
2154 diff_words->current_plus = diff_words->plus.text.ptr;
Bo Yang4297c0a2010-05-29 23:32:06 +08002155 diff_words->last_minus = 0;
Johannes Schindelinf59a59e2006-07-28 23:56:15 +02002156
Brian Downing9ccd0a82008-10-25 15:30:37 +02002157 memset(&xpp, 0, sizeof(xpp));
Johannes Schindelin30b25012007-07-04 19:05:46 +01002158 memset(&xecfg, 0, sizeof(xecfg));
Johannes Schindelin2b6a5412009-01-17 17:29:45 +01002159 diff_words_fill(&diff_words->minus, &minus, diff_words->word_regex);
2160 diff_words_fill(&diff_words->plus, &plus, diff_words->word_regex);
René Scharfe582aa002010-05-02 15:04:41 +02002161 xpp.flags = 0;
Johannes Schindelin2b6a5412009-01-17 17:29:45 +01002162 /* as only the hunk header will be parsed, we need a 0-context */
Johannes Schindelin2e5d2002009-01-17 17:29:44 +01002163 xecfg.ctxlen = 0;
Jeff King7c61e252018-11-02 02:37:18 -04002164 if (xdi_diff_outf(&minus, &plus, fn_out_diff_words_aux, NULL,
Jeff King9346d6d2018-11-02 02:35:45 -04002165 diff_words, &xpp, &xecfg))
Jeff King3efb9882015-09-24 19:12:23 -04002166 die("unable to generate word diff");
Johannes Schindelinf59a59e2006-07-28 23:56:15 +02002167 free(minus.ptr);
2168 free(plus.ptr);
Johannes Schindelin2e5d2002009-01-17 17:29:44 +01002169 if (diff_words->current_plus != diff_words->plus.text.ptr +
Bo Yang4297c0a2010-05-29 23:32:06 +08002170 diff_words->plus.text.size) {
2171 if (color_words_output_graph_prefix(diff_words))
Stefan Bellerbd033292017-06-29 17:07:03 -07002172 emit_diff_symbol(diff_words->opt, DIFF_SYMBOL_WORD_DIFF,
2173 line_prefix, strlen(line_prefix), 0);
2174 fn_out_diff_words_write_helper(diff_words->opt,
Thomas Rast882749a2010-04-14 17:59:06 +02002175 &style->ctx, style->newline,
Johannes Schindelin2e5d2002009-01-17 17:29:44 +01002176 diff_words->plus.text.ptr + diff_words->plus.text.size
Stefan Bellerbd033292017-06-29 17:07:03 -07002177 - diff_words->current_plus, diff_words->current_plus);
Bo Yang4297c0a2010-05-29 23:32:06 +08002178 }
Johannes Schindelinf59a59e2006-07-28 23:56:15 +02002179 diff_words->minus.text.size = diff_words->plus.text.size = 0;
Johannes Schindelinf59a59e2006-07-28 23:56:15 +02002180}
2181
Junio C Hamano76fd2822009-10-30 10:09:06 -07002182/* In "color-words" mode, show word-diff of words accumulated in the buffer */
2183static void diff_words_flush(struct emit_callback *ecbdata)
2184{
Stefan Bellere6e045f2017-06-29 17:07:06 -07002185 struct diff_options *wo = ecbdata->diff_words->opt;
2186
Junio C Hamano76fd2822009-10-30 10:09:06 -07002187 if (ecbdata->diff_words->minus.text.size ||
2188 ecbdata->diff_words->plus.text.size)
2189 diff_words_show(ecbdata->diff_words);
Stefan Bellere6e045f2017-06-29 17:07:06 -07002190
2191 if (wo->emitted_symbols) {
2192 struct diff_options *o = ecbdata->opt;
2193 struct emitted_diff_symbols *wol = wo->emitted_symbols;
2194 int i;
2195
2196 /*
2197 * NEEDSWORK:
2198 * Instead of appending each, concat all words to a line?
2199 */
2200 for (i = 0; i < wol->nr; i++)
2201 append_emitted_diff_symbol(o, &wol->buf[i]);
2202
2203 for (i = 0; i < wol->nr; i++)
2204 free((void *)wol->buf[i].line);
2205
2206 wol->nr = 0;
2207 }
Junio C Hamano76fd2822009-10-30 10:09:06 -07002208}
2209
Nguyễn Thái Ngọc Duyacd00ea2018-09-21 17:57:33 +02002210static void diff_filespec_load_driver(struct diff_filespec *one,
2211 struct index_state *istate)
Thomas Rast77d1a522012-03-14 19:24:08 +01002212{
2213 /* Use already-loaded driver */
2214 if (one->driver)
2215 return;
2216
2217 if (S_ISREG(one->mode))
Nguyễn Thái Ngọc Duyacd00ea2018-09-21 17:57:33 +02002218 one->driver = userdiff_find_by_path(istate, one->path);
Thomas Rast77d1a522012-03-14 19:24:08 +01002219
2220 /* Fallback to default settings */
2221 if (!one->driver)
2222 one->driver = userdiff_find_by_name("default");
2223}
2224
Nguyễn Thái Ngọc Duyacd00ea2018-09-21 17:57:33 +02002225static const char *userdiff_word_regex(struct diff_filespec *one,
2226 struct index_state *istate)
Thomas Rast77d1a522012-03-14 19:24:08 +01002227{
Nguyễn Thái Ngọc Duyacd00ea2018-09-21 17:57:33 +02002228 diff_filespec_load_driver(one, istate);
Thomas Rast77d1a522012-03-14 19:24:08 +01002229 return one->driver->word_regex;
2230}
2231
2232static void init_diff_words_data(struct emit_callback *ecbdata,
Thomas Rast6440d342012-03-14 19:24:09 +01002233 struct diff_options *orig_opts,
Thomas Rast77d1a522012-03-14 19:24:08 +01002234 struct diff_filespec *one,
2235 struct diff_filespec *two)
2236{
2237 int i;
Thomas Rast6440d342012-03-14 19:24:09 +01002238 struct diff_options *o = xmalloc(sizeof(struct diff_options));
2239 memcpy(o, orig_opts, sizeof(struct diff_options));
Thomas Rast77d1a522012-03-14 19:24:08 +01002240
René Scharfeca56dad2021-03-13 17:17:22 +01002241 CALLOC_ARRAY(ecbdata->diff_words, 1);
Thomas Rast77d1a522012-03-14 19:24:08 +01002242 ecbdata->diff_words->type = o->word_diff;
2243 ecbdata->diff_words->opt = o;
Stefan Bellere6e045f2017-06-29 17:07:06 -07002244
2245 if (orig_opts->emitted_symbols)
René Scharfeca56dad2021-03-13 17:17:22 +01002246 CALLOC_ARRAY(o->emitted_symbols, 1);
Stefan Bellere6e045f2017-06-29 17:07:06 -07002247
Thomas Rast77d1a522012-03-14 19:24:08 +01002248 if (!o->word_regex)
Nguyễn Thái Ngọc Duyacd00ea2018-09-21 17:57:33 +02002249 o->word_regex = userdiff_word_regex(one, o->repo->index);
Thomas Rast77d1a522012-03-14 19:24:08 +01002250 if (!o->word_regex)
Nguyễn Thái Ngọc Duyacd00ea2018-09-21 17:57:33 +02002251 o->word_regex = userdiff_word_regex(two, o->repo->index);
Thomas Rast77d1a522012-03-14 19:24:08 +01002252 if (!o->word_regex)
2253 o->word_regex = diff_word_regex_cfg;
2254 if (o->word_regex) {
2255 ecbdata->diff_words->word_regex = (regex_t *)
2256 xmalloc(sizeof(regex_t));
2257 if (regcomp(ecbdata->diff_words->word_regex,
2258 o->word_regex,
2259 REG_EXTENDED | REG_NEWLINE))
Nguyễn Thái Ngọc Duy1a07e592018-07-21 09:49:19 +02002260 die("invalid regular expression: %s",
2261 o->word_regex);
Thomas Rast77d1a522012-03-14 19:24:08 +01002262 }
2263 for (i = 0; i < ARRAY_SIZE(diff_words_styles); i++) {
2264 if (o->word_diff == diff_words_styles[i].type) {
2265 ecbdata->diff_words->style =
2266 &diff_words_styles[i];
2267 break;
2268 }
2269 }
2270 if (want_color(o->use_color)) {
2271 struct diff_words_style *st = ecbdata->diff_words->style;
Brandon Williams63a01c32018-02-14 10:59:39 -08002272 st->old_word.color = diff_get_color_opt(o, DIFF_FILE_OLD);
2273 st->new_word.color = diff_get_color_opt(o, DIFF_FILE_NEW);
Jeff King8dbf3eb2015-05-27 16:48:46 -04002274 st->ctx.color = diff_get_color_opt(o, DIFF_CONTEXT);
Thomas Rast77d1a522012-03-14 19:24:08 +01002275 }
2276}
2277
Johannes Schindelinf59a59e2006-07-28 23:56:15 +02002278static void free_diff_words_data(struct emit_callback *ecbdata)
2279{
2280 if (ecbdata->diff_words) {
Junio C Hamano76fd2822009-10-30 10:09:06 -07002281 diff_words_flush(ecbdata);
Stefan Bellere6e045f2017-06-29 17:07:06 -07002282 free (ecbdata->diff_words->opt->emitted_symbols);
Thomas Rast6440d342012-03-14 19:24:09 +01002283 free (ecbdata->diff_words->opt);
Jim Meyering8e0f7002008-01-31 18:26:32 +01002284 free (ecbdata->diff_words->minus.text.ptr);
Johannes Schindelin2e5d2002009-01-17 17:29:44 +01002285 free (ecbdata->diff_words->minus.orig);
Jim Meyering8e0f7002008-01-31 18:26:32 +01002286 free (ecbdata->diff_words->plus.text.ptr);
Johannes Schindelin2e5d2002009-01-17 17:29:44 +01002287 free (ecbdata->diff_words->plus.orig);
Brandon Caseyef5644e2010-09-09 14:02:45 -05002288 if (ecbdata->diff_words->word_regex) {
2289 regfree(ecbdata->diff_words->word_regex);
2290 free(ecbdata->diff_words->word_regex);
2291 }
Ævar Arnfjörð Bjarmason6a83d902017-06-15 23:15:46 +00002292 FREE_AND_NULL(ecbdata->diff_words);
Johannes Schindelinf59a59e2006-07-28 23:56:15 +02002293 }
2294}
2295
Jeff Kingce436972006-07-23 05:24:18 -04002296const char *diff_get_color(int diff_use_color, enum color_diff ix)
Johannes Schindelincd112ce2006-06-13 18:45:44 +02002297{
Jeff Kingdaa0c3d2011-08-17 22:04:23 -07002298 if (want_color(diff_use_color))
Linus Torvalds50f575f2006-06-22 13:53:31 -07002299 return diff_colors[ix];
2300 return "";
Johannes Schindelincd112ce2006-06-13 18:45:44 +02002301}
2302
John Keepingf1922232013-02-07 20:15:26 +00002303const char *diff_line_prefix(struct diff_options *opt)
2304{
2305 struct strbuf *msgbuf;
2306 if (!opt->output_prefix)
2307 return "";
2308
2309 msgbuf = opt->output_prefix(opt, opt->output_prefix_data);
2310 return msgbuf->buf;
2311}
2312
Jeff King19b90462019-02-14 00:48:27 -05002313static unsigned long sane_truncate_line(char *line, unsigned long len)
Junio C Hamano23707812008-01-02 01:50:11 -08002314{
2315 const char *cp;
2316 unsigned long allot;
2317 size_t l = len;
2318
Junio C Hamano23707812008-01-02 01:50:11 -08002319 cp = line;
2320 allot = l;
2321 while (0 < l) {
2322 (void) utf8_width(&cp, &l);
2323 if (!cp)
2324 break; /* truncated in the middle? */
2325 }
2326 return allot - l;
2327}
2328
Junio C Hamanod68fe262009-09-14 22:05:57 -07002329static void find_lno(const char *line, struct emit_callback *ecbdata)
Junio C Hamano690ed842009-09-04 00:41:15 -07002330{
Junio C Hamanod68fe262009-09-14 22:05:57 -07002331 const char *p;
2332 ecbdata->lno_in_preimage = 0;
2333 ecbdata->lno_in_postimage = 0;
2334 p = strchr(line, '-');
Junio C Hamano690ed842009-09-04 00:41:15 -07002335 if (!p)
Junio C Hamanod68fe262009-09-14 22:05:57 -07002336 return; /* cannot happen */
2337 ecbdata->lno_in_preimage = strtol(p + 1, NULL, 10);
2338 p = strchr(p, '+');
2339 if (!p)
2340 return; /* cannot happen */
2341 ecbdata->lno_in_postimage = strtol(p + 1, NULL, 10);
Junio C Hamano690ed842009-09-04 00:41:15 -07002342}
2343
Ævar Arnfjörð Bjarmasona8d5eb62021-04-12 19:15:24 +02002344static int fn_out_consume(void *priv, char *line, unsigned long len)
Junio C Hamano6973dca2006-04-21 23:57:45 -07002345{
Junio C Hamano6973dca2006-04-21 23:57:45 -07002346 struct emit_callback *ecbdata = priv;
Bo Yang7be57612010-05-26 15:23:54 +08002347 struct diff_options *o = ecbdata->opt;
Junio C Hamano6973dca2006-04-21 23:57:45 -07002348
Stefan Bellerba162332016-09-07 16:36:46 -07002349 o->found_changes = 1;
2350
Greg Bacon3e97c7c2009-11-19 15:12:24 -06002351 if (ecbdata->header) {
Stefan Bellera29b0a12017-06-29 17:06:57 -07002352 emit_diff_symbol(o, DIFF_SYMBOL_HEADER,
2353 ecbdata->header->buf, ecbdata->header->len, 0);
Greg Bacon3e97c7c2009-11-19 15:12:24 -06002354 strbuf_reset(ecbdata->header);
2355 ecbdata->header = NULL;
2356 }
Johannes Schindelin34a5e1a2007-02-25 23:34:54 +01002357
Junio C Hamano6973dca2006-04-21 23:57:45 -07002358 if (ecbdata->label_path[0]) {
Stefan Beller3ee8b7b2017-06-29 17:06:56 -07002359 emit_diff_symbol(o, DIFF_SYMBOL_FILEPAIR_MINUS,
2360 ecbdata->label_path[0],
2361 strlen(ecbdata->label_path[0]), 0);
2362 emit_diff_symbol(o, DIFF_SYMBOL_FILEPAIR_PLUS,
2363 ecbdata->label_path[1],
2364 strlen(ecbdata->label_path[1]), 0);
Junio C Hamano6973dca2006-04-21 23:57:45 -07002365 ecbdata->label_path[0] = ecbdata->label_path[1] = NULL;
2366 }
Johannes Schindelincd112ce2006-06-13 18:45:44 +02002367
Jim Meyeringa624eaa2008-08-15 13:39:26 +02002368 if (diff_suppress_blank_empty
2369 && len == 2 && line[0] == ' ' && line[1] == '\n') {
2370 line[0] = '\n';
2371 len = 1;
2372 }
2373
Junio C Hamanob8d9c1a2009-09-03 23:59:25 -07002374 if (line[0] == '@') {
Junio C Hamano76fd2822009-10-30 10:09:06 -07002375 if (ecbdata->diff_words)
2376 diff_words_flush(ecbdata);
Jeff King19b90462019-02-14 00:48:27 -05002377 len = sane_truncate_line(line, len);
Junio C Hamanod68fe262009-09-14 22:05:57 -07002378 find_lno(line, ecbdata);
Bert Wesarg89cb73a2009-11-27 07:55:18 +01002379 emit_hunk_header(ecbdata, line, len);
Ævar Arnfjörð Bjarmasona8d5eb62021-04-12 19:15:24 +02002380 return 0;
Johannes Schindelincd112ce2006-06-13 18:45:44 +02002381 }
Junio C Hamano448c3ef2006-09-22 22:48:39 -07002382
Junio C Hamano448c3ef2006-09-22 22:48:39 -07002383 if (ecbdata->diff_words) {
Stefan Bellerff958672017-06-29 17:06:54 -07002384 enum diff_symbol s =
2385 ecbdata->diff_words->type == DIFF_WORDS_PORCELAIN ?
2386 DIFF_SYMBOL_WORDS_PORCELAIN : DIFF_SYMBOL_WORDS;
Junio C Hamano448c3ef2006-09-22 22:48:39 -07002387 if (line[0] == '-') {
2388 diff_words_append(line, len,
2389 &ecbdata->diff_words->minus);
Ævar Arnfjörð Bjarmasona8d5eb62021-04-12 19:15:24 +02002390 return 0;
Junio C Hamano448c3ef2006-09-22 22:48:39 -07002391 } else if (line[0] == '+') {
2392 diff_words_append(line, len,
2393 &ecbdata->diff_words->plus);
Ævar Arnfjörð Bjarmasona8d5eb62021-04-12 19:15:24 +02002394 return 0;
Christian Couder59556542013-11-30 21:55:40 +01002395 } else if (starts_with(line, "\\ ")) {
Thomas Rastc7c2bc02012-01-12 12:15:33 +01002396 /*
2397 * Eat the "no newline at eof" marker as if we
2398 * saw a "+" or "-" line with nothing on it,
2399 * and return without diff_words_flush() to
2400 * defer processing. If this is the end of
2401 * preimage, more "+" lines may come after it.
2402 */
Ævar Arnfjörð Bjarmasona8d5eb62021-04-12 19:15:24 +02002403 return 0;
Junio C Hamano448c3ef2006-09-22 22:48:39 -07002404 }
Junio C Hamano76fd2822009-10-30 10:09:06 -07002405 diff_words_flush(ecbdata);
Stefan Bellerff958672017-06-29 17:06:54 -07002406 emit_diff_symbol(o, s, line, len, 0);
Ævar Arnfjörð Bjarmasona8d5eb62021-04-12 19:15:24 +02002407 return 0;
Junio C Hamano448c3ef2006-09-22 22:48:39 -07002408 }
Junio C Hamano448c3ef2006-09-22 22:48:39 -07002409
Junio C Hamano0e383e12015-05-26 09:56:33 -07002410 switch (line[0]) {
2411 case '+':
Junio C Hamanod68fe262009-09-14 22:05:57 -07002412 ecbdata->lno_in_postimage++;
Jeff Kinge04df612019-02-14 00:48:13 -05002413 emit_add_line(ecbdata, line + 1, len - 1);
Junio C Hamano0e383e12015-05-26 09:56:33 -07002414 break;
2415 case '-':
2416 ecbdata->lno_in_preimage++;
Jeff Kinge04df612019-02-14 00:48:13 -05002417 emit_del_line(ecbdata, line + 1, len - 1);
Junio C Hamano0e383e12015-05-26 09:56:33 -07002418 break;
2419 case ' ':
2420 ecbdata->lno_in_postimage++;
2421 ecbdata->lno_in_preimage++;
Jeff Kinge04df612019-02-14 00:48:13 -05002422 emit_context_line(ecbdata, line + 1, len - 1);
Junio C Hamano0e383e12015-05-26 09:56:33 -07002423 break;
2424 default:
2425 /* incomplete line at the end */
2426 ecbdata->lno_in_preimage++;
Stefan Bellerf2bb1212017-06-29 17:06:55 -07002427 emit_diff_symbol(o, DIFF_SYMBOL_CONTEXT_INCOMPLETE,
2428 line, len, 0);
Junio C Hamano0e383e12015-05-26 09:56:33 -07002429 break;
Junio C Hamano448c3ef2006-09-22 22:48:39 -07002430 }
Ævar Arnfjörð Bjarmasona8d5eb62021-04-12 19:15:24 +02002431 return 0;
Junio C Hamano6973dca2006-04-21 23:57:45 -07002432}
2433
Nguyễn Thái Ngọc Duyc905cbc2018-02-01 20:02:20 +07002434static void pprint_rename(struct strbuf *name, const char *a, const char *b)
Junio C Hamano6973dca2006-04-21 23:57:45 -07002435{
Brandon Williams63a01c32018-02-14 10:59:39 -08002436 const char *old_name = a;
2437 const char *new_name = b;
Junio C Hamano6973dca2006-04-21 23:57:45 -07002438 int pfx_length, sfx_length;
Thomas Rastdd281f02013-02-26 21:47:01 +01002439 int pfx_adjust_for_slash;
Junio C Hamano6973dca2006-04-21 23:57:45 -07002440 int len_a = strlen(a);
2441 int len_b = strlen(b);
Pierre Habouzit663af342007-09-20 00:42:15 +02002442 int a_midlen, b_midlen;
Alexandre Julliarde5bfbf92007-02-10 15:39:00 +01002443 int qlen_a = quote_c_style(a, NULL, NULL, 0);
2444 int qlen_b = quote_c_style(b, NULL, NULL, 0);
2445
2446 if (qlen_a || qlen_b) {
Nguyễn Thái Ngọc Duyc905cbc2018-02-01 20:02:20 +07002447 quote_c_style(a, name, NULL, 0);
2448 strbuf_addstr(name, " => ");
2449 quote_c_style(b, name, NULL, 0);
2450 return;
Alexandre Julliarde5bfbf92007-02-10 15:39:00 +01002451 }
Junio C Hamano6973dca2006-04-21 23:57:45 -07002452
2453 /* Find common prefix */
2454 pfx_length = 0;
Brandon Williams63a01c32018-02-14 10:59:39 -08002455 while (*old_name && *new_name && *old_name == *new_name) {
2456 if (*old_name == '/')
2457 pfx_length = old_name - a + 1;
2458 old_name++;
2459 new_name++;
Junio C Hamano6973dca2006-04-21 23:57:45 -07002460 }
2461
2462 /* Find common suffix */
Brandon Williams63a01c32018-02-14 10:59:39 -08002463 old_name = a + len_a;
2464 new_name = b + len_b;
Junio C Hamano6973dca2006-04-21 23:57:45 -07002465 sfx_length = 0;
Antoine Pelissed020e272013-02-23 17:48:45 +01002466 /*
Thomas Rastdd281f02013-02-26 21:47:01 +01002467 * If there is a common prefix, it must end in a slash. In
2468 * that case we let this loop run 1 into the prefix to see the
2469 * same slash.
2470 *
2471 * If there is no common prefix, we cannot do this as it would
2472 * underrun the input strings.
Antoine Pelissed020e272013-02-23 17:48:45 +01002473 */
Thomas Rastdd281f02013-02-26 21:47:01 +01002474 pfx_adjust_for_slash = (pfx_length ? 1 : 0);
Brandon Williams63a01c32018-02-14 10:59:39 -08002475 while (a + pfx_length - pfx_adjust_for_slash <= old_name &&
2476 b + pfx_length - pfx_adjust_for_slash <= new_name &&
2477 *old_name == *new_name) {
2478 if (*old_name == '/')
2479 sfx_length = len_a - (old_name - a);
2480 old_name--;
2481 new_name--;
Junio C Hamano6973dca2006-04-21 23:57:45 -07002482 }
2483
2484 /*
2485 * pfx{mid-a => mid-b}sfx
2486 * {pfx-a => pfx-b}sfx
2487 * pfx{sfx-a => sfx-b}
2488 * name-a => name-b
2489 */
Pierre Habouzit663af342007-09-20 00:42:15 +02002490 a_midlen = len_a - pfx_length - sfx_length;
2491 b_midlen = len_b - pfx_length - sfx_length;
2492 if (a_midlen < 0)
2493 a_midlen = 0;
2494 if (b_midlen < 0)
2495 b_midlen = 0;
Junio C Hamanocc908b82006-05-14 22:07:28 -07002496
Nguyễn Thái Ngọc Duyc905cbc2018-02-01 20:02:20 +07002497 strbuf_grow(name, pfx_length + a_midlen + b_midlen + sfx_length + 7);
Pierre Habouzit663af342007-09-20 00:42:15 +02002498 if (pfx_length + sfx_length) {
Nguyễn Thái Ngọc Duyc905cbc2018-02-01 20:02:20 +07002499 strbuf_add(name, a, pfx_length);
2500 strbuf_addch(name, '{');
Junio C Hamano6973dca2006-04-21 23:57:45 -07002501 }
Nguyễn Thái Ngọc Duyc905cbc2018-02-01 20:02:20 +07002502 strbuf_add(name, a + pfx_length, a_midlen);
2503 strbuf_addstr(name, " => ");
2504 strbuf_add(name, b + pfx_length, b_midlen);
Pierre Habouzit663af342007-09-20 00:42:15 +02002505 if (pfx_length + sfx_length) {
Nguyễn Thái Ngọc Duyc905cbc2018-02-01 20:02:20 +07002506 strbuf_addch(name, '}');
2507 strbuf_add(name, a + len_a - sfx_length, sfx_length);
Junio C Hamano6973dca2006-04-21 23:57:45 -07002508 }
Junio C Hamano6973dca2006-04-21 23:57:45 -07002509}
2510
Junio C Hamano6973dca2006-04-21 23:57:45 -07002511static struct diffstat_file *diffstat_add(struct diffstat_t *diffstat,
2512 const char *name_a,
2513 const char *name_b)
2514{
2515 struct diffstat_file *x;
René Scharfeca56dad2021-03-13 17:17:22 +01002516 CALLOC_ARRAY(x, 1);
Dmitry S. Dolzhenko4c960a42014-03-04 02:31:53 +04002517 ALLOC_GROW(diffstat->files, diffstat->nr + 1, diffstat->alloc);
Junio C Hamano6973dca2006-04-21 23:57:45 -07002518 diffstat->files[diffstat->nr++] = x;
2519 if (name_b) {
Junio C Hamanof6046522007-12-11 23:46:30 -08002520 x->from_name = xstrdup(name_a);
2521 x->name = xstrdup(name_b);
Junio C Hamano6973dca2006-04-21 23:57:45 -07002522 x->is_renamed = 1;
2523 }
Junio C Hamanof6046522007-12-11 23:46:30 -08002524 else {
2525 x->from_name = NULL;
Shawn Pearce9befac42006-09-02 00:16:31 -04002526 x->name = xstrdup(name_a);
Junio C Hamanof6046522007-12-11 23:46:30 -08002527 }
Junio C Hamano6973dca2006-04-21 23:57:45 -07002528 return x;
2529}
2530
Ævar Arnfjörð Bjarmasona8d5eb62021-04-12 19:15:24 +02002531static int diffstat_consume(void *priv, char *line, unsigned long len)
Junio C Hamano6973dca2006-04-21 23:57:45 -07002532{
2533 struct diffstat_t *diffstat = priv;
2534 struct diffstat_file *x = diffstat->files[diffstat->nr - 1];
2535
2536 if (line[0] == '+')
2537 x->added++;
2538 else if (line[0] == '-')
2539 x->deleted++;
Ævar Arnfjörð Bjarmasona8d5eb62021-04-12 19:15:24 +02002540 return 0;
Junio C Hamano6973dca2006-04-21 23:57:45 -07002541}
2542
Johannes Schindelin698ce6f2006-05-20 15:40:29 +02002543const char mime_boundary_leader[] = "------------";
Junio C Hamano6973dca2006-04-21 23:57:45 -07002544
Junio C Hamanoa2540022006-09-26 18:53:02 -07002545static int scale_linear(int it, int width, int max_change)
2546{
Junio C Hamano2eeeef22012-02-14 13:49:11 -08002547 if (!it)
2548 return 0;
Junio C Hamanoa2540022006-09-26 18:53:02 -07002549 /*
Junio C Hamano2eeeef22012-02-14 13:49:11 -08002550 * make sure that at least one '-' or '+' is printed if
2551 * there is any change to this path. The easiest way is to
Elijah Newren15beaaa2019-11-05 17:07:23 +00002552 * scale linearly as if the allotted width is one column shorter
Junio C Hamano2eeeef22012-02-14 13:49:11 -08002553 * than it is, and then add 1 to the result.
Junio C Hamanoa2540022006-09-26 18:53:02 -07002554 */
Junio C Hamano2eeeef22012-02-14 13:49:11 -08002555 return 1 + (it * (width - 1) / max_change);
Junio C Hamanoa2540022006-09-26 18:53:02 -07002556}
2557
Stefan Beller0911c472017-06-29 17:07:02 -07002558static void show_graph(struct strbuf *out, char ch, int cnt,
2559 const char *set, const char *reset)
Junio C Hamanoa2540022006-09-26 18:53:02 -07002560{
2561 if (cnt <= 0)
2562 return;
Stefan Beller0911c472017-06-29 17:07:02 -07002563 strbuf_addstr(out, set);
2564 strbuf_addchars(out, ch, cnt);
2565 strbuf_addstr(out, reset);
Junio C Hamanoa2540022006-09-26 18:53:02 -07002566}
2567
Junio C Hamanof6046522007-12-11 23:46:30 -08002568static void fill_print_name(struct diffstat_file *file)
2569{
Nguyễn Thái Ngọc Duyc905cbc2018-02-01 20:02:20 +07002570 struct strbuf pname = STRBUF_INIT;
Junio C Hamanof6046522007-12-11 23:46:30 -08002571
2572 if (file->print_name)
2573 return;
2574
Nguyễn Thái Ngọc Duyc905cbc2018-02-01 20:02:20 +07002575 if (file->is_renamed)
2576 pprint_rename(&pname, file->from_name, file->name);
2577 else
2578 quote_c_style(file->name, &pname, NULL, 0);
2579
Nguyễn Thái Ngọc Duyddf88fa2018-02-24 21:09:59 +07002580 if (file->comments)
2581 strbuf_addf(&pname, " (%s)", file->comments);
2582
Nguyễn Thái Ngọc Duyc905cbc2018-02-01 20:02:20 +07002583 file->print_name = strbuf_detach(&pname, NULL);
Junio C Hamanof6046522007-12-11 23:46:30 -08002584}
2585
Stefan Beller0911c472017-06-29 17:07:02 -07002586static void print_stat_summary_inserts_deletes(struct diff_options *options,
2587 int files, int insertions, int deletions)
Nguyễn Thái Ngọc Duy7f814632012-02-01 19:55:07 +07002588{
2589 struct strbuf sb = STRBUF_INIT;
Nguyễn Thái Ngọc Duy7f814632012-02-01 19:55:07 +07002590
2591 if (!files) {
2592 assert(insertions == 0 && deletions == 0);
Stefan Beller0911c472017-06-29 17:07:02 -07002593 emit_diff_symbol(options, DIFF_SYMBOL_STATS_SUMMARY_NO_FILES,
2594 NULL, 0, 0);
2595 return;
Nguyễn Thái Ngọc Duy7f814632012-02-01 19:55:07 +07002596 }
2597
2598 strbuf_addf(&sb,
Nguyễn Thái Ngọc Duy218adaa2012-09-13 21:16:26 +07002599 (files == 1) ? " %d file changed" : " %d files changed",
Nguyễn Thái Ngọc Duy7f814632012-02-01 19:55:07 +07002600 files);
2601
2602 /*
2603 * For binary diff, the caller may want to print "x files
2604 * changed" with insertions == 0 && deletions == 0.
2605 *
2606 * Not omitting "0 insertions(+), 0 deletions(-)" in this case
2607 * is probably less confusing (i.e skip over "2 files changed
2608 * but nothing about added/removed lines? Is this a bug in Git?").
2609 */
2610 if (insertions || deletions == 0) {
Nguyễn Thái Ngọc Duy7f814632012-02-01 19:55:07 +07002611 strbuf_addf(&sb,
Nguyễn Thái Ngọc Duy218adaa2012-09-13 21:16:26 +07002612 (insertions == 1) ? ", %d insertion(+)" : ", %d insertions(+)",
Nguyễn Thái Ngọc Duy7f814632012-02-01 19:55:07 +07002613 insertions);
2614 }
2615
2616 if (deletions || insertions == 0) {
Nguyễn Thái Ngọc Duy7f814632012-02-01 19:55:07 +07002617 strbuf_addf(&sb,
Nguyễn Thái Ngọc Duy218adaa2012-09-13 21:16:26 +07002618 (deletions == 1) ? ", %d deletion(-)" : ", %d deletions(-)",
Nguyễn Thái Ngọc Duy7f814632012-02-01 19:55:07 +07002619 deletions);
2620 }
2621 strbuf_addch(&sb, '\n');
Stefan Beller0911c472017-06-29 17:07:02 -07002622 emit_diff_symbol(options, DIFF_SYMBOL_STATS_SUMMARY_INSERTS_DELETES,
2623 sb.buf, sb.len, 0);
Nguyễn Thái Ngọc Duy7f814632012-02-01 19:55:07 +07002624 strbuf_release(&sb);
Stefan Beller0911c472017-06-29 17:07:02 -07002625}
2626
2627void print_stat_summary(FILE *fp, int files,
2628 int insertions, int deletions)
2629{
2630 struct diff_options o;
2631 memset(&o, 0, sizeof(o));
2632 o.file = fp;
2633
2634 print_stat_summary_inserts_deletes(&o, files, insertions, deletions);
Nguyễn Thái Ngọc Duy7f814632012-02-01 19:55:07 +07002635}
2636
Felipe Contreras4b25d092009-05-01 12:06:36 +03002637static void show_stats(struct diffstat_t *data, struct diff_options *options)
Junio C Hamano6973dca2006-04-21 23:57:45 -07002638{
Benjamin Kramereb3a9dd2009-03-07 21:02:10 +01002639 int i, len, add, del, adds = 0, dels = 0;
Jeff King0974c112010-04-17 13:41:08 -04002640 uintmax_t max_change = 0, max_len = 0;
Zbigniew Jędrzejewski-Szmekdc801e72012-04-30 22:38:58 +02002641 int total_files = data->nr, count;
2642 int width, name_width, graph_width, number_width = 0, bin_width = 0;
Johannes Schindelinc0aa3352011-03-22 13:50:08 +01002643 const char *reset, *add_c, *del_c;
Junio C Hamanoe5f85df2011-05-27 21:50:39 -07002644 int extra_shown = 0;
Stefan Beller0911c472017-06-29 17:07:02 -07002645 const char *line_prefix = diff_line_prefix(options);
2646 struct strbuf out = STRBUF_INIT;
Junio C Hamano6973dca2006-04-21 23:57:45 -07002647
2648 if (data->nr == 0)
2649 return;
2650
Michael J Gruber808e1db2011-05-27 14:36:41 +02002651 count = options->stat_count ? options->stat_count : data->nr;
Junio C Hamanoa2540022006-09-26 18:53:02 -07002652
Pierre Habouzit8f67f8a2007-11-10 20:05:14 +01002653 reset = diff_get_color_opt(options, DIFF_RESET);
Pierre Habouzit8f67f8a2007-11-10 20:05:14 +01002654 add_c = diff_get_color_opt(options, DIFF_FILE_NEW);
2655 del_c = diff_get_color_opt(options, DIFF_FILE_OLD);
Junio C Hamano785f7432006-09-26 18:59:41 -07002656
Zbigniew Jędrzejewski-Szmek1b058bc2012-03-01 13:26:43 +01002657 /*
2658 * Find the longest filename and max number of changes
2659 */
Michael J Gruber808e1db2011-05-27 14:36:41 +02002660 for (i = 0; (i < count) && (i < data->nr); i++) {
Junio C Hamano6973dca2006-04-21 23:57:45 -07002661 struct diffstat_file *file = data->files[i];
Jeff King0974c112010-04-17 13:41:08 -04002662 uintmax_t change = file->added + file->deleted;
Junio C Hamanoaf0ed812012-11-27 11:24:54 -08002663
2664 if (!file->is_interesting && (change == 0)) {
Michael J Gruber808e1db2011-05-27 14:36:41 +02002665 count++; /* not shown == room for one more */
Michael J Gruber358e4602011-05-27 14:36:40 +02002666 continue;
2667 }
Junio C Hamanof6046522007-12-11 23:46:30 -08002668 fill_print_name(file);
2669 len = strlen(file->print_name);
Junio C Hamano6973dca2006-04-21 23:57:45 -07002670 if (max_len < len)
2671 max_len = len;
2672
Zbigniew Jędrzejewski-Szmekdc801e72012-04-30 22:38:58 +02002673 if (file->is_unmerged) {
2674 /* "Unmerged" is 8 characters */
2675 bin_width = bin_width < 8 ? 8 : bin_width;
Junio C Hamano6973dca2006-04-21 23:57:45 -07002676 continue;
Zbigniew Jędrzejewski-Szmekdc801e72012-04-30 22:38:58 +02002677 }
2678 if (file->is_binary) {
2679 /* "Bin XXX -> YYY bytes" */
2680 int w = 14 + decimal_width(file->added)
2681 + decimal_width(file->deleted);
2682 bin_width = bin_width < w ? w : bin_width;
2683 /* Display change counts aligned with "Bin" */
2684 number_width = 3;
2685 continue;
2686 }
2687
Junio C Hamanoa2540022006-09-26 18:53:02 -07002688 if (max_change < change)
2689 max_change = change;
Junio C Hamano6973dca2006-04-21 23:57:45 -07002690 }
Junio C Hamanoa20d3c02012-11-27 11:47:46 -08002691 count = i; /* where we can stop scanning in data->files[] */
Junio C Hamano6973dca2006-04-21 23:57:45 -07002692
Zbigniew Jędrzejewski-Szmek1b058bc2012-03-01 13:26:43 +01002693 /*
2694 * We have width = stat_width or term_columns() columns total.
2695 * We want a maximum of min(max_len, stat_name_width) for the name part.
Zbigniew Jędrzejewski-Szmek969fe572012-03-01 13:26:45 +01002696 * We want a maximum of min(max_change, stat_graph_width) for the +- part.
Zbigniew Jędrzejewski-Szmek1b058bc2012-03-01 13:26:43 +01002697 * We also need 1 for " " and 4 + decimal_width(max_change)
2698 * for " | NNNN " and one the empty column at the end, altogether
2699 * 6 + decimal_width(max_change).
Junio C Hamanoa2540022006-09-26 18:53:02 -07002700 *
Zbigniew Jędrzejewski-Szmek1b058bc2012-03-01 13:26:43 +01002701 * If there's not enough space, we will use the smaller of
2702 * stat_name_width (if set) and 5/8*width for the filename,
Zbigniew Jędrzejewski-Szmek969fe572012-03-01 13:26:45 +01002703 * and the rest for constant elements + graph part, but no more
2704 * than stat_graph_width for the graph part.
Zbigniew Jędrzejewski-Szmek1b058bc2012-03-01 13:26:43 +01002705 * (5/8 gives 50 for filename and 30 for the constant parts + graph
2706 * for the standard terminal size).
2707 *
2708 * In other words: stat_width limits the maximum width, and
2709 * stat_name_width fixes the maximum width of the filename,
2710 * and is also used to divide available columns if there
2711 * aren't enough.
Zbigniew Jędrzejewski-Szmekdc801e72012-04-30 22:38:58 +02002712 *
2713 * Binary files are displayed with "Bin XXX -> YYY bytes"
2714 * instead of the change count and graph. This part is treated
2715 * similarly to the graph part, except that it is not
Stefano Lattarini41ccfdd2013-04-12 00:36:10 +02002716 * "scaled". If total width is too small to accommodate the
Zbigniew Jędrzejewski-Szmekdc801e72012-04-30 22:38:58 +02002717 * guaranteed minimum width of the filename part and the
2718 * separators and this message, this message will "overflow"
2719 * making the line longer than the maximum width.
Junio C Hamanoa2540022006-09-26 18:53:02 -07002720 */
Junio C Hamanoa2540022006-09-26 18:53:02 -07002721
Zbigniew Jędrzejewski-Szmek1b058bc2012-03-01 13:26:43 +01002722 if (options->stat_width == -1)
Junio C Hamanocd48dad2016-08-31 16:27:19 -07002723 width = term_columns() - strlen(line_prefix);
Zbigniew Jędrzejewski-Szmek1b058bc2012-03-01 13:26:43 +01002724 else
2725 width = options->stat_width ? options->stat_width : 80;
Zbigniew Jędrzejewski-Szmekdc801e72012-04-30 22:38:58 +02002726 number_width = decimal_width(max_change) > number_width ?
2727 decimal_width(max_change) : number_width;
Zbigniew Jędrzejewski-Szmek1b058bc2012-03-01 13:26:43 +01002728
Zbigniew Jędrzejewski-Szmekdf444832012-03-01 13:26:46 +01002729 if (options->stat_graph_width == -1)
2730 options->stat_graph_width = diff_stat_graph_width;
2731
Zbigniew Jędrzejewski-Szmek1b058bc2012-03-01 13:26:43 +01002732 /*
2733 * Guarantee 3/8*16==6 for the graph part
2734 * and 5/8*16==10 for the filename part
2735 */
2736 if (width < 16 + 6 + number_width)
2737 width = 16 + 6 + number_width;
2738
2739 /*
2740 * First assign sizes that are wanted, ignoring available width.
Zbigniew Jędrzejewski-Szmekdc801e72012-04-30 22:38:58 +02002741 * strlen("Bin XXX -> YYY bytes") == bin_width, and the part
2742 * starting from "XXX" should fit in graph_width.
Zbigniew Jędrzejewski-Szmek1b058bc2012-03-01 13:26:43 +01002743 */
Zbigniew Jędrzejewski-Szmekdc801e72012-04-30 22:38:58 +02002744 graph_width = max_change + 4 > bin_width ? max_change : bin_width - 4;
2745 if (options->stat_graph_width &&
2746 options->stat_graph_width < graph_width)
2747 graph_width = options->stat_graph_width;
2748
Zbigniew Jędrzejewski-Szmek1b058bc2012-03-01 13:26:43 +01002749 name_width = (options->stat_name_width > 0 &&
2750 options->stat_name_width < max_len) ?
2751 options->stat_name_width : max_len;
2752
2753 /*
2754 * Adjust adjustable widths not to exceed maximum width
2755 */
2756 if (name_width + number_width + 6 + graph_width > width) {
Lucian Poston678c5742012-04-18 14:12:18 -07002757 if (graph_width > width * 3/8 - number_width - 6) {
Zbigniew Jędrzejewski-Szmek1b058bc2012-03-01 13:26:43 +01002758 graph_width = width * 3/8 - number_width - 6;
Lucian Poston678c5742012-04-18 14:12:18 -07002759 if (graph_width < 6)
2760 graph_width = 6;
2761 }
2762
Zbigniew Jędrzejewski-Szmek969fe572012-03-01 13:26:45 +01002763 if (options->stat_graph_width &&
2764 graph_width > options->stat_graph_width)
2765 graph_width = options->stat_graph_width;
Zbigniew Jędrzejewski-Szmek1b058bc2012-03-01 13:26:43 +01002766 if (name_width > width - number_width - 6 - graph_width)
2767 name_width = width - number_width - 6 - graph_width;
2768 else
2769 graph_width = width - number_width - 6 - name_width;
2770 }
2771
2772 /*
2773 * From here name_width is the width of the name area,
2774 * and graph_width is the width of the graph area.
2775 * max_change is used to scale graph properly.
2776 */
Michael J Gruber808e1db2011-05-27 14:36:41 +02002777 for (i = 0; i < count; i++) {
Timo Hirvonend2543b82006-06-24 20:20:32 +03002778 const char *prefix = "";
Junio C Hamanoaf0ed812012-11-27 11:24:54 -08002779 struct diffstat_file *file = data->files[i];
2780 char *name = file->print_name;
2781 uintmax_t added = file->added;
2782 uintmax_t deleted = file->deleted;
Junio C Hamanoa2540022006-09-26 18:53:02 -07002783 int name_len;
Junio C Hamano6973dca2006-04-21 23:57:45 -07002784
Junio C Hamanoa20d3c02012-11-27 11:47:46 -08002785 if (!file->is_interesting && (added + deleted == 0))
Michael J Gruber358e4602011-05-27 14:36:40 +02002786 continue;
Junio C Hamanoa20d3c02012-11-27 11:47:46 -08002787
Junio C Hamano6973dca2006-04-21 23:57:45 -07002788 /*
2789 * "scale" the filename
2790 */
Junio C Hamanoa2540022006-09-26 18:53:02 -07002791 len = name_width;
2792 name_len = strlen(name);
2793 if (name_width < name_len) {
Junio C Hamano6973dca2006-04-21 23:57:45 -07002794 char *slash;
2795 prefix = "...";
Junio C Hamanoa2540022006-09-26 18:53:02 -07002796 len -= 3;
2797 name += name_len - len;
Junio C Hamano6973dca2006-04-21 23:57:45 -07002798 slash = strchr(name, '/');
2799 if (slash)
2800 name = slash;
2801 }
Junio C Hamano6973dca2006-04-21 23:57:45 -07002802
Junio C Hamanoaf0ed812012-11-27 11:24:54 -08002803 if (file->is_binary) {
Stefan Beller0911c472017-06-29 17:07:02 -07002804 strbuf_addf(&out, " %s%-*s |", prefix, len, name);
2805 strbuf_addf(&out, " %*s", number_width, "Bin");
Zbigniew Jędrzejewski-Szmeke18872b2012-05-01 19:10:14 +02002806 if (!added && !deleted) {
Stefan Beller0911c472017-06-29 17:07:02 -07002807 strbuf_addch(&out, '\n');
2808 emit_diff_symbol(options, DIFF_SYMBOL_STATS_LINE,
2809 out.buf, out.len, 0);
2810 strbuf_reset(&out);
Zbigniew Jędrzejewski-Szmeke18872b2012-05-01 19:10:14 +02002811 continue;
2812 }
Stefan Beller0911c472017-06-29 17:07:02 -07002813 strbuf_addf(&out, " %s%"PRIuMAX"%s",
Jeff King0974c112010-04-17 13:41:08 -04002814 del_c, deleted, reset);
Stefan Beller0911c472017-06-29 17:07:02 -07002815 strbuf_addstr(&out, " -> ");
2816 strbuf_addf(&out, "%s%"PRIuMAX"%s",
Jeff King0974c112010-04-17 13:41:08 -04002817 add_c, added, reset);
Stefan Beller0911c472017-06-29 17:07:02 -07002818 strbuf_addstr(&out, " bytes\n");
2819 emit_diff_symbol(options, DIFF_SYMBOL_STATS_LINE,
2820 out.buf, out.len, 0);
2821 strbuf_reset(&out);
Junio C Hamanof6046522007-12-11 23:46:30 -08002822 continue;
Junio C Hamano6973dca2006-04-21 23:57:45 -07002823 }
Junio C Hamanoaf0ed812012-11-27 11:24:54 -08002824 else if (file->is_unmerged) {
Stefan Beller0911c472017-06-29 17:07:02 -07002825 strbuf_addf(&out, " %s%-*s |", prefix, len, name);
2826 strbuf_addstr(&out, " Unmerged\n");
2827 emit_diff_symbol(options, DIFF_SYMBOL_STATS_LINE,
2828 out.buf, out.len, 0);
2829 strbuf_reset(&out);
Junio C Hamanof6046522007-12-11 23:46:30 -08002830 continue;
Junio C Hamano6973dca2006-04-21 23:57:45 -07002831 }
Junio C Hamano6973dca2006-04-21 23:57:45 -07002832
Junio C Hamanoa2540022006-09-26 18:53:02 -07002833 /*
2834 * scale the add/delete
2835 */
Junio C Hamano6973dca2006-04-21 23:57:45 -07002836 add = added;
2837 del = deleted;
Junio C Hamano6973dca2006-04-21 23:57:45 -07002838
Zbigniew Jędrzejewski-Szmek1b058bc2012-03-01 13:26:43 +01002839 if (graph_width <= max_change) {
Stefan Bellerd3c9cf32013-07-14 23:35:49 +02002840 int total = scale_linear(add + del, graph_width, max_change);
Junio C Hamano2eeeef22012-02-14 13:49:11 -08002841 if (total < 2 && add && del)
2842 /* width >= 2 due to the sanity check */
2843 total = 2;
2844 if (add < del) {
Zbigniew Jędrzejewski-Szmek1b058bc2012-03-01 13:26:43 +01002845 add = scale_linear(add, graph_width, max_change);
Junio C Hamano2eeeef22012-02-14 13:49:11 -08002846 del = total - add;
2847 } else {
Zbigniew Jędrzejewski-Szmek1b058bc2012-03-01 13:26:43 +01002848 del = scale_linear(del, graph_width, max_change);
Junio C Hamano2eeeef22012-02-14 13:49:11 -08002849 add = total - del;
2850 }
Junio C Hamano6973dca2006-04-21 23:57:45 -07002851 }
Stefan Beller0911c472017-06-29 17:07:02 -07002852 strbuf_addf(&out, " %s%-*s |", prefix, len, name);
2853 strbuf_addf(&out, " %*"PRIuMAX"%s",
Zbigniew Jędrzejewski-Szmekdc801e72012-04-30 22:38:58 +02002854 number_width, added + deleted,
2855 added + deleted ? " " : "");
Stefan Beller0911c472017-06-29 17:07:02 -07002856 show_graph(&out, '+', add, add_c, reset);
2857 show_graph(&out, '-', del, del_c, reset);
2858 strbuf_addch(&out, '\n');
2859 emit_diff_symbol(options, DIFF_SYMBOL_STATS_LINE,
2860 out.buf, out.len, 0);
2861 strbuf_reset(&out);
Junio C Hamano6973dca2006-04-21 23:57:45 -07002862 }
Junio C Hamanoa20d3c02012-11-27 11:47:46 -08002863
2864 for (i = 0; i < data->nr; i++) {
Junio C Hamanoaf0ed812012-11-27 11:24:54 -08002865 struct diffstat_file *file = data->files[i];
2866 uintmax_t added = file->added;
2867 uintmax_t deleted = file->deleted;
Junio C Hamano82dfc2c2012-11-27 12:05:10 -08002868
2869 if (file->is_unmerged ||
2870 (!file->is_interesting && (added + deleted == 0))) {
Michael J Gruber808e1db2011-05-27 14:36:41 +02002871 total_files--;
2872 continue;
2873 }
Junio C Hamanoa20d3c02012-11-27 11:47:46 -08002874
Junio C Hamano82dfc2c2012-11-27 12:05:10 -08002875 if (!file->is_binary) {
Junio C Hamanoa20d3c02012-11-27 11:47:46 -08002876 adds += added;
2877 dels += deleted;
2878 }
2879 if (i < count)
2880 continue;
Junio C Hamanoe5f85df2011-05-27 21:50:39 -07002881 if (!extra_shown)
Stefan Beller0911c472017-06-29 17:07:02 -07002882 emit_diff_symbol(options,
2883 DIFF_SYMBOL_STATS_SUMMARY_ABBREV,
2884 NULL, 0, 0);
Junio C Hamanoe5f85df2011-05-27 21:50:39 -07002885 extra_shown = 1;
Michael J Gruber808e1db2011-05-27 14:36:41 +02002886 }
Stefan Beller0911c472017-06-29 17:07:02 -07002887
2888 print_stat_summary_inserts_deletes(options, total_files, adds, dels);
Rene Scharfe5a612012017-08-30 19:49:43 +02002889 strbuf_release(&out);
Junio C Hamano6973dca2006-04-21 23:57:45 -07002890}
2891
Felipe Contreras2775d922009-10-11 23:46:11 +03002892static void show_shortstats(struct diffstat_t *data, struct diff_options *options)
Nicolas Pitreebd124c2006-12-14 23:15:44 -05002893{
2894 int i, adds = 0, dels = 0, total_files = data->nr;
2895
2896 if (data->nr == 0)
2897 return;
2898
2899 for (i = 0; i < data->nr; i++) {
Zbigniew Jędrzejewski-Szmeke18872b2012-05-01 19:10:14 +02002900 int added = data->files[i]->added;
Stefan Beller0911c472017-06-29 17:07:02 -07002901 int deleted = data->files[i]->deleted;
Zbigniew Jędrzejewski-Szmeke18872b2012-05-01 19:10:14 +02002902
Junio C Hamano20c8cde2012-11-27 14:19:36 -08002903 if (data->files[i]->is_unmerged ||
2904 (!data->files[i]->is_interesting && (added + deleted == 0))) {
Zbigniew Jędrzejewski-Szmeke18872b2012-05-01 19:10:14 +02002905 total_files--;
Alexander Strasserde9658b2012-06-15 23:50:30 +02002906 } else if (!data->files[i]->is_binary) { /* don't count bytes */
Zbigniew Jędrzejewski-Szmeke18872b2012-05-01 19:10:14 +02002907 adds += added;
2908 dels += deleted;
Nicolas Pitreebd124c2006-12-14 23:15:44 -05002909 }
Nicolas Pitreebd124c2006-12-14 23:15:44 -05002910 }
Stefan Beller0911c472017-06-29 17:07:02 -07002911 print_stat_summary_inserts_deletes(options, total_files, adds, dels);
Nicolas Pitreebd124c2006-12-14 23:15:44 -05002912}
2913
Felipe Contreras4b25d092009-05-01 12:06:36 +03002914static void show_numstat(struct diffstat_t *data, struct diff_options *options)
Junio C Hamano74e2abe2006-10-12 03:01:00 -07002915{
2916 int i;
2917
Junio C Hamanof6046522007-12-11 23:46:30 -08002918 if (data->nr == 0)
2919 return;
2920
Junio C Hamano74e2abe2006-10-12 03:01:00 -07002921 for (i = 0; i < data->nr; i++) {
2922 struct diffstat_file *file = data->files[i];
2923
John Keeping30997bb2013-02-07 20:15:27 +00002924 fprintf(options->file, "%s", diff_line_prefix(options));
Bo Yang7be57612010-05-26 15:23:54 +08002925
Junio C Hamanobfddbc52006-12-10 13:50:59 -08002926 if (file->is_binary)
Daniel Barkalowc0c77732008-03-09 22:43:39 -04002927 fprintf(options->file, "-\t-\t");
Junio C Hamanobfddbc52006-12-10 13:50:59 -08002928 else
Daniel Barkalowc0c77732008-03-09 22:43:39 -04002929 fprintf(options->file,
Jeff King0974c112010-04-17 13:41:08 -04002930 "%"PRIuMAX"\t%"PRIuMAX"\t",
2931 file->added, file->deleted);
Junio C Hamanof6046522007-12-11 23:46:30 -08002932 if (options->line_termination) {
2933 fill_print_name(file);
2934 if (!file->is_renamed)
Daniel Barkalowc0c77732008-03-09 22:43:39 -04002935 write_name_quoted(file->name, options->file,
Junio C Hamanof6046522007-12-11 23:46:30 -08002936 options->line_termination);
2937 else {
Daniel Barkalowc0c77732008-03-09 22:43:39 -04002938 fputs(file->print_name, options->file);
2939 putc(options->line_termination, options->file);
Junio C Hamanof6046522007-12-11 23:46:30 -08002940 }
Pierre Habouzit663af342007-09-20 00:42:15 +02002941 } else {
Junio C Hamanof6046522007-12-11 23:46:30 -08002942 if (file->is_renamed) {
Daniel Barkalowc0c77732008-03-09 22:43:39 -04002943 putc('\0', options->file);
2944 write_name_quoted(file->from_name, options->file, '\0');
Junio C Hamanof6046522007-12-11 23:46:30 -08002945 }
Daniel Barkalowc0c77732008-03-09 22:43:39 -04002946 write_name_quoted(file->name, options->file, '\0');
Pierre Habouzit663af342007-09-20 00:42:15 +02002947 }
Junio C Hamano74e2abe2006-10-12 03:01:00 -07002948 }
2949}
2950
Junio C Hamanoc04a7152008-02-12 17:06:58 -08002951struct dirstat_file {
2952 const char *name;
2953 unsigned long changed;
Linus Torvalds7df7c012008-02-12 13:26:31 -08002954};
2955
Junio C Hamanoc04a7152008-02-12 17:06:58 -08002956struct dirstat_dir {
2957 struct dirstat_file *files;
Johan Herland712d2c72011-04-29 11:36:20 +02002958 int alloc, nr, permille, cumulative;
Junio C Hamanoc04a7152008-02-12 17:06:58 -08002959};
2960
Bo Yang7be57612010-05-26 15:23:54 +08002961static long gather_dirstat(struct diff_options *opt, struct dirstat_dir *dir,
2962 unsigned long changed, const char *base, int baselen)
Linus Torvalds7df7c012008-02-12 13:26:31 -08002963{
Brandon Williams585c0e22018-02-14 10:59:28 -08002964 unsigned long sum_changes = 0;
Linus Torvalds7df7c012008-02-12 13:26:31 -08002965 unsigned int sources = 0;
John Keeping30997bb2013-02-07 20:15:27 +00002966 const char *line_prefix = diff_line_prefix(opt);
Linus Torvalds7df7c012008-02-12 13:26:31 -08002967
2968 while (dir->nr) {
Junio C Hamanoc04a7152008-02-12 17:06:58 -08002969 struct dirstat_file *f = dir->files;
Linus Torvalds7df7c012008-02-12 13:26:31 -08002970 int namelen = strlen(f->name);
Brandon Williams585c0e22018-02-14 10:59:28 -08002971 unsigned long changes;
Linus Torvalds7df7c012008-02-12 13:26:31 -08002972 char *slash;
2973
2974 if (namelen < baselen)
2975 break;
2976 if (memcmp(f->name, base, baselen))
2977 break;
2978 slash = strchr(f->name + baselen, '/');
2979 if (slash) {
2980 int newbaselen = slash + 1 - f->name;
Brandon Williams585c0e22018-02-14 10:59:28 -08002981 changes = gather_dirstat(opt, dir, changed, f->name, newbaselen);
Linus Torvalds7df7c012008-02-12 13:26:31 -08002982 sources++;
2983 } else {
Brandon Williams585c0e22018-02-14 10:59:28 -08002984 changes = f->changed;
Linus Torvalds7df7c012008-02-12 13:26:31 -08002985 dir->files++;
2986 dir->nr--;
2987 sources += 2;
2988 }
Brandon Williams585c0e22018-02-14 10:59:28 -08002989 sum_changes += changes;
Linus Torvalds7df7c012008-02-12 13:26:31 -08002990 }
2991
2992 /*
2993 * We don't report dirstat's for
2994 * - the top level
2995 * - or cases where everything came from a single directory
2996 * under this directory (sources == 1).
2997 */
2998 if (baselen && sources != 1) {
Brandon Williams585c0e22018-02-14 10:59:28 -08002999 if (sum_changes) {
3000 int permille = sum_changes * 1000 / changed;
Johan Herland712d2c72011-04-29 11:36:20 +02003001 if (permille >= dir->permille) {
Bo Yang7be57612010-05-26 15:23:54 +08003002 fprintf(opt->file, "%s%4d.%01d%% %.*s\n", line_prefix,
Johan Herland712d2c72011-04-29 11:36:20 +02003003 permille / 10, permille % 10, baselen, base);
Linus Torvalds7df7c012008-02-12 13:26:31 -08003004 if (!dir->cumulative)
3005 return 0;
3006 }
3007 }
3008 }
Brandon Williams585c0e22018-02-14 10:59:28 -08003009 return sum_changes;
Linus Torvalds7df7c012008-02-12 13:26:31 -08003010}
3011
Linus Torvalds441bca02008-08-28 16:19:08 -07003012static int dirstat_compare(const void *_a, const void *_b)
3013{
3014 const struct dirstat_file *a = _a;
3015 const struct dirstat_file *b = _b;
3016 return strcmp(a->name, b->name);
3017}
3018
Junio C Hamanoc04a7152008-02-12 17:06:58 -08003019static void show_dirstat(struct diff_options *options)
Linus Torvalds7df7c012008-02-12 13:26:31 -08003020{
3021 int i;
3022 unsigned long changed;
Junio C Hamanoc04a7152008-02-12 17:06:58 -08003023 struct dirstat_dir dir;
3024 struct diff_queue_struct *q = &diff_queued_diff;
Linus Torvalds7df7c012008-02-12 13:26:31 -08003025
Junio C Hamanoc04a7152008-02-12 17:06:58 -08003026 dir.files = NULL;
3027 dir.alloc = 0;
3028 dir.nr = 0;
Johan Herland712d2c72011-04-29 11:36:20 +02003029 dir.permille = options->dirstat_permille;
Brandon Williams0d1e0e72017-10-31 11:19:11 -07003030 dir.cumulative = options->flags.dirstat_cumulative;
Junio C Hamanoc04a7152008-02-12 17:06:58 -08003031
Linus Torvalds7df7c012008-02-12 13:26:31 -08003032 changed = 0;
Junio C Hamanoc04a7152008-02-12 17:06:58 -08003033 for (i = 0; i < q->nr; i++) {
3034 struct diff_filepair *p = q->queue[i];
3035 const char *name;
3036 unsigned long copied, added, damage;
Jonathan Tan1c37e862020-04-07 15:11:41 -07003037 struct diff_populate_filespec_options dpf_options = {
3038 .check_size_only = 1,
3039 };
Junio C Hamanoc04a7152008-02-12 17:06:58 -08003040
Johan Herland2ca86712011-04-12 11:24:34 +02003041 name = p->two->path ? p->two->path : p->one->path;
Junio C Hamanoc04a7152008-02-12 17:06:58 -08003042
Jeff Kingd9f62df2018-08-28 17:23:03 -04003043 if (p->one->oid_valid && p->two->oid_valid &&
3044 oideq(&p->one->oid, &p->two->oid)) {
Johan Herland2ff3a802011-04-11 00:48:52 +02003045 /*
3046 * The SHA1 has not changed, so pre-/post-content is
3047 * identical. We can therefore skip looking at the
3048 * file contents altogether.
3049 */
3050 damage = 0;
3051 goto found_damage;
3052 }
3053
Brandon Williams0d1e0e72017-10-31 11:19:11 -07003054 if (options->flags.dirstat_by_file) {
Johan Herland0133dab2011-04-11 00:48:51 +02003055 /*
3056 * In --dirstat-by-file mode, we don't really need to
3057 * look at the actual file contents at all.
3058 * The fact that the SHA1 changed is enough for us to
3059 * add this file to the list of results
3060 * (with each file contributing equal damage).
3061 */
Johan Herland2ff3a802011-04-11 00:48:52 +02003062 damage = 1;
Johan Herland0133dab2011-04-11 00:48:51 +02003063 goto found_damage;
3064 }
Junio C Hamanoc04a7152008-02-12 17:06:58 -08003065
3066 if (DIFF_FILE_VALID(p->one) && DIFF_FILE_VALID(p->two)) {
Jonathan Tan1c37e862020-04-07 15:11:41 -07003067 diff_populate_filespec(options->repo, p->one, NULL);
3068 diff_populate_filespec(options->repo, p->two, NULL);
Nguyễn Thái Ngọc Duyb78ea5f2018-09-21 17:57:19 +02003069 diffcore_count_changes(options->repo,
3070 p->one, p->two, NULL, NULL,
Junio C Hamanoc04a7152008-02-12 17:06:58 -08003071 &copied, &added);
3072 diff_free_filespec_data(p->one);
3073 diff_free_filespec_data(p->two);
3074 } else if (DIFF_FILE_VALID(p->one)) {
Jonathan Tan1c37e862020-04-07 15:11:41 -07003075 diff_populate_filespec(options->repo, p->one, &dpf_options);
Junio C Hamanoc04a7152008-02-12 17:06:58 -08003076 copied = added = 0;
3077 diff_free_filespec_data(p->one);
3078 } else if (DIFF_FILE_VALID(p->two)) {
Jonathan Tan1c37e862020-04-07 15:11:41 -07003079 diff_populate_filespec(options->repo, p->two, &dpf_options);
Junio C Hamanoc04a7152008-02-12 17:06:58 -08003080 copied = 0;
3081 added = p->two->size;
3082 diff_free_filespec_data(p->two);
3083 } else
Junio C Hamano2b0b5512008-02-24 17:37:15 -08003084 continue;
Junio C Hamanoc04a7152008-02-12 17:06:58 -08003085
3086 /*
3087 * Original minus copied is the removed material,
3088 * added is the new material. They are both damages
Johan Herland0133dab2011-04-11 00:48:51 +02003089 * made to the preimage.
Johan Herland2ff3a802011-04-11 00:48:52 +02003090 * If the resulting damage is zero, we know that
3091 * diffcore_count_changes() considers the two entries to
Jeff Kingd9f62df2018-08-28 17:23:03 -04003092 * be identical, but since the oid changed, we
Johan Herland2ff3a802011-04-11 00:48:52 +02003093 * know that there must have been _some_ kind of change,
3094 * so we force all entries to have damage > 0.
Junio C Hamanoc04a7152008-02-12 17:06:58 -08003095 */
3096 damage = (p->one->size - copied) + added;
Johan Herland2ff3a802011-04-11 00:48:52 +02003097 if (!damage)
Heikki Orsilafd337772008-09-05 22:27:35 +03003098 damage = 1;
Junio C Hamanoc04a7152008-02-12 17:06:58 -08003099
Johan Herland0133dab2011-04-11 00:48:51 +02003100found_damage:
Junio C Hamanoc04a7152008-02-12 17:06:58 -08003101 ALLOC_GROW(dir.files, dir.nr + 1, dir.alloc);
3102 dir.files[dir.nr].name = name;
3103 dir.files[dir.nr].changed = damage;
3104 changed += damage;
3105 dir.nr++;
Linus Torvalds7df7c012008-02-12 13:26:31 -08003106 }
3107
3108 /* This can happen even with many files, if everything was renames */
3109 if (!changed)
3110 return;
3111
3112 /* Show all directories with more than x% of the changes */
René Scharfe9ed0d8d2016-09-29 17:27:31 +02003113 QSORT(dir.files, dir.nr, dirstat_compare);
Bo Yang7be57612010-05-26 15:23:54 +08003114 gather_dirstat(options, &dir, changed, "", 0);
Linus Torvalds7df7c012008-02-12 13:26:31 -08003115}
3116
Johan Herland1c57a622011-04-29 11:36:21 +02003117static void show_dirstat_by_line(struct diffstat_t *data, struct diff_options *options)
3118{
3119 int i;
3120 unsigned long changed;
3121 struct dirstat_dir dir;
3122
3123 if (data->nr == 0)
3124 return;
3125
3126 dir.files = NULL;
3127 dir.alloc = 0;
3128 dir.nr = 0;
3129 dir.permille = options->dirstat_permille;
Brandon Williams0d1e0e72017-10-31 11:19:11 -07003130 dir.cumulative = options->flags.dirstat_cumulative;
Johan Herland1c57a622011-04-29 11:36:21 +02003131
3132 changed = 0;
3133 for (i = 0; i < data->nr; i++) {
3134 struct diffstat_file *file = data->files[i];
3135 unsigned long damage = file->added + file->deleted;
3136 if (file->is_binary)
3137 /*
3138 * binary files counts bytes, not lines. Must find some
3139 * way to normalize binary bytes vs. textual lines.
3140 * The following heuristic assumes that there are 64
3141 * bytes per "line".
3142 * This is stupid and ugly, but very cheap...
3143 */
René Scharfe42c78a22017-07-08 12:35:35 +02003144 damage = DIV_ROUND_UP(damage, 64);
Johan Herland1c57a622011-04-29 11:36:21 +02003145 ALLOC_GROW(dir.files, dir.nr + 1, dir.alloc);
3146 dir.files[dir.nr].name = file->name;
3147 dir.files[dir.nr].changed = damage;
3148 changed += damage;
3149 dir.nr++;
3150 }
3151
3152 /* This can happen even with many files, if everything was renames */
3153 if (!changed)
3154 return;
3155
3156 /* Show all directories with more than x% of the changes */
René Scharfe9ed0d8d2016-09-29 17:27:31 +02003157 QSORT(dir.files, dir.nr, dirstat_compare);
Johan Herland1c57a622011-04-29 11:36:21 +02003158 gather_dirstat(options, &dir, changed, "", 0);
3159}
3160
Matthew Rogers1cf3d5d2020-08-20 00:41:32 +00003161static void free_diffstat_file(struct diffstat_file *f)
3162{
3163 free(f->print_name);
3164 free(f->name);
3165 free(f->from_name);
3166 free(f);
3167}
3168
Daniel Ferreirae4cb6592019-11-13 12:40:58 +00003169void free_diffstat_info(struct diffstat_t *diffstat)
Junio C Hamanof6046522007-12-11 23:46:30 -08003170{
3171 int i;
Matthew Rogers1cf3d5d2020-08-20 00:41:32 +00003172 for (i = 0; i < diffstat->nr; i++)
3173 free_diffstat_file(diffstat->files[i]);
Junio C Hamanof6046522007-12-11 23:46:30 -08003174 free(diffstat->files);
3175}
3176
Johannes Schindelin88246892006-05-20 23:43:13 +02003177struct checkdiff_t {
Johannes Schindelin88246892006-05-20 23:43:13 +02003178 const char *filename;
Junio C Hamano1ba111d2008-06-26 15:36:34 -07003179 int lineno;
Junio C Hamanoa757c642010-03-24 19:21:32 -07003180 int conflict_marker_size;
Junio C Hamano1ba111d2008-06-26 15:36:34 -07003181 struct diff_options *o;
Junio C Hamanocf1b7862007-12-06 00:14:14 -08003182 unsigned ws_rule;
Wincent Colaiuta62c64892007-12-13 21:24:52 +01003183 unsigned status;
Johannes Schindelin88246892006-05-20 23:43:13 +02003184};
3185
Junio C Hamanoa757c642010-03-24 19:21:32 -07003186static int is_conflict_marker(const char *line, int marker_size, unsigned long len)
Junio C Hamano04954042008-06-26 15:37:21 -07003187{
3188 char firstchar;
3189 int cnt;
3190
Junio C Hamanoa757c642010-03-24 19:21:32 -07003191 if (len < marker_size + 1)
Junio C Hamano04954042008-06-26 15:37:21 -07003192 return 0;
3193 firstchar = line[0];
3194 switch (firstchar) {
Junio C Hamanoa757c642010-03-24 19:21:32 -07003195 case '=': case '>': case '<': case '|':
Junio C Hamano04954042008-06-26 15:37:21 -07003196 break;
3197 default:
3198 return 0;
3199 }
Junio C Hamanoa757c642010-03-24 19:21:32 -07003200 for (cnt = 1; cnt < marker_size; cnt++)
Junio C Hamano04954042008-06-26 15:37:21 -07003201 if (line[cnt] != firstchar)
3202 return 0;
Elijah Newren15beaaa2019-11-05 17:07:23 +00003203 /* line[1] through line[marker_size-1] are same as firstchar */
Junio C Hamanoa757c642010-03-24 19:21:32 -07003204 if (len < marker_size + 1 || !isspace(line[marker_size]))
Junio C Hamano04954042008-06-26 15:37:21 -07003205 return 0;
Junio C Hamano04954042008-06-26 15:37:21 -07003206 return 1;
3207}
3208
Jeff King75ab7632018-11-02 02:39:03 -04003209static void checkdiff_consume_hunk(void *priv,
3210 long ob, long on, long nb, long nn,
3211 const char *func, long funclen)
3212
3213{
3214 struct checkdiff_t *data = priv;
3215 data->lineno = nb - 1;
3216}
3217
Ævar Arnfjörð Bjarmasona8d5eb62021-04-12 19:15:24 +02003218static int checkdiff_consume(void *priv, char *line, unsigned long len)
Johannes Schindelin88246892006-05-20 23:43:13 +02003219{
3220 struct checkdiff_t *data = priv;
Junio C Hamanoa757c642010-03-24 19:21:32 -07003221 int marker_size = data->conflict_marker_size;
Jeff Kingf1c96262011-08-17 22:03:12 -07003222 const char *ws = diff_get_color(data->o->use_color, DIFF_WHITESPACE);
3223 const char *reset = diff_get_color(data->o->use_color, DIFF_RESET);
3224 const char *set = diff_get_color(data->o->use_color, DIFF_FILE_NEW);
Wincent Colaiutac1795bb2007-12-13 14:32:29 +01003225 char *err;
John Keeping30997bb2013-02-07 20:15:27 +00003226 const char *line_prefix;
Bo Yang7be57612010-05-26 15:23:54 +08003227
3228 assert(data->o);
John Keeping30997bb2013-02-07 20:15:27 +00003229 line_prefix = diff_line_prefix(data->o);
Johannes Schindelin88246892006-05-20 23:43:13 +02003230
3231 if (line[0] == '+') {
Junio C Hamano18374e52008-06-26 13:16:33 -07003232 unsigned bad;
Junio C Hamano0ef617f2008-02-15 20:30:05 -08003233 data->lineno++;
Junio C Hamanoa757c642010-03-24 19:21:32 -07003234 if (is_conflict_marker(line + 1, marker_size, len - 1)) {
Junio C Hamano04954042008-06-26 15:37:21 -07003235 data->status |= 1;
3236 fprintf(data->o->file,
Bo Yang7be57612010-05-26 15:23:54 +08003237 "%s%s:%d: leftover conflict marker\n",
3238 line_prefix, data->filename, data->lineno);
Junio C Hamano04954042008-06-26 15:37:21 -07003239 }
Junio C Hamano8f8841e2008-06-26 15:35:21 -07003240 bad = ws_check(line + 1, len - 1, data->ws_rule);
Junio C Hamano18374e52008-06-26 13:16:33 -07003241 if (!bad)
Ævar Arnfjörð Bjarmasona8d5eb62021-04-12 19:15:24 +02003242 return 0;
Junio C Hamano18374e52008-06-26 13:16:33 -07003243 data->status |= bad;
3244 err = whitespace_error_string(bad);
Bo Yang7be57612010-05-26 15:23:54 +08003245 fprintf(data->o->file, "%s%s:%d: %s.\n",
3246 line_prefix, data->filename, data->lineno, err);
Wincent Colaiutac1795bb2007-12-13 14:32:29 +01003247 free(err);
Bo Yanga3c158d2010-05-26 15:08:02 +08003248 emit_line(data->o, set, reset, line, 1);
Junio C Hamano8f8841e2008-06-26 15:35:21 -07003249 ws_check_emit(line + 1, len - 1, data->ws_rule,
Junio C Hamano1ba111d2008-06-26 15:36:34 -07003250 data->o->file, set, reset, ws);
Junio C Hamano877f23c2008-06-26 15:36:59 -07003251 } else if (line[0] == ' ') {
Johannes Schindelin88246892006-05-20 23:43:13 +02003252 data->lineno++;
Johannes Schindelin88246892006-05-20 23:43:13 +02003253 }
Ævar Arnfjörð Bjarmasona8d5eb62021-04-12 19:15:24 +02003254 return 0;
Johannes Schindelin88246892006-05-20 23:43:13 +02003255}
3256
Junio C Hamano06606262006-05-05 02:41:53 -07003257static unsigned char *deflate_it(char *data,
3258 unsigned long size,
3259 unsigned long *result_size)
Junio C Hamano051308f2006-05-04 16:51:44 -07003260{
Junio C Hamano06606262006-05-05 02:41:53 -07003261 int bound;
3262 unsigned char *deflated;
Junio C Hamanoef49a7a2011-06-10 11:52:15 -07003263 git_zstream stream;
Junio C Hamano051308f2006-05-04 16:51:44 -07003264
Junio C Hamano55bb5c92011-06-10 10:55:10 -07003265 git_deflate_init(&stream, zlib_compression_level);
Junio C Hamano225a6f12011-06-10 11:18:17 -07003266 bound = git_deflate_bound(&stream, size);
Junio C Hamano06606262006-05-05 02:41:53 -07003267 deflated = xmalloc(bound);
3268 stream.next_out = deflated;
3269 stream.avail_out = bound;
Junio C Hamano051308f2006-05-04 16:51:44 -07003270
Junio C Hamano06606262006-05-05 02:41:53 -07003271 stream.next_in = (unsigned char *)data;
3272 stream.avail_in = size;
Junio C Hamano55bb5c92011-06-10 10:55:10 -07003273 while (git_deflate(&stream, Z_FINISH) == Z_OK)
Junio C Hamano06606262006-05-05 02:41:53 -07003274 ; /* nothing */
Junio C Hamano55bb5c92011-06-10 10:55:10 -07003275 git_deflate_end(&stream);
Junio C Hamano06606262006-05-05 02:41:53 -07003276 *result_size = stream.total_out;
3277 return deflated;
Junio C Hamano051308f2006-05-04 16:51:44 -07003278}
3279
Stefan Beller4eed0eb2017-06-29 17:07:01 -07003280static void emit_binary_diff_body(struct diff_options *o,
3281 mmfile_t *one, mmfile_t *two)
Junio C Hamano051308f2006-05-04 16:51:44 -07003282{
Junio C Hamano06606262006-05-05 02:41:53 -07003283 void *cp;
3284 void *delta;
3285 void *deflated;
3286 void *data;
3287 unsigned long orig_size;
Junio C Hamano051308f2006-05-04 16:51:44 -07003288 unsigned long delta_size;
Junio C Hamano06606262006-05-05 02:41:53 -07003289 unsigned long deflate_size;
3290 unsigned long data_size;
Junio C Hamano051308f2006-05-04 16:51:44 -07003291
Junio C Hamano06606262006-05-05 02:41:53 -07003292 /* We could do deflated delta, or we could do just deflated two,
3293 * whichever is smaller.
3294 */
3295 delta = NULL;
3296 deflated = deflate_it(two->ptr, two->size, &deflate_size);
3297 if (one->size && two->size) {
3298 delta = diff_delta(one->ptr, one->size,
3299 two->ptr, two->size,
3300 &delta_size, deflate_size);
3301 if (delta) {
3302 void *to_free = delta;
3303 orig_size = delta_size;
3304 delta = deflate_it(delta, delta_size, &delta_size);
3305 free(to_free);
3306 }
3307 }
Junio C Hamano051308f2006-05-04 16:51:44 -07003308
Junio C Hamano06606262006-05-05 02:41:53 -07003309 if (delta && delta_size < deflate_size) {
Torsten Bögershausenca473ce2018-11-11 08:05:04 +01003310 char *s = xstrfmt("%"PRIuMAX , (uintmax_t)orig_size);
Stefan Beller4eed0eb2017-06-29 17:07:01 -07003311 emit_diff_symbol(o, DIFF_SYMBOL_BINARY_DIFF_HEADER_DELTA,
3312 s, strlen(s), 0);
3313 free(s);
Junio C Hamano06606262006-05-05 02:41:53 -07003314 free(deflated);
3315 data = delta;
3316 data_size = delta_size;
Stefan Beller4eed0eb2017-06-29 17:07:01 -07003317 } else {
3318 char *s = xstrfmt("%lu", two->size);
3319 emit_diff_symbol(o, DIFF_SYMBOL_BINARY_DIFF_HEADER_LITERAL,
3320 s, strlen(s), 0);
3321 free(s);
Junio C Hamano06606262006-05-05 02:41:53 -07003322 free(delta);
3323 data = deflated;
3324 data_size = deflate_size;
3325 }
3326
3327 /* emit data encoded in base85 */
3328 cp = data;
3329 while (data_size) {
Stefan Beller4eed0eb2017-06-29 17:07:01 -07003330 int len;
Junio C Hamano06606262006-05-05 02:41:53 -07003331 int bytes = (52 < data_size) ? 52 : data_size;
Stefan Beller4eed0eb2017-06-29 17:07:01 -07003332 char line[71];
Junio C Hamano06606262006-05-05 02:41:53 -07003333 data_size -= bytes;
Junio C Hamano051308f2006-05-04 16:51:44 -07003334 if (bytes <= 26)
3335 line[0] = bytes + 'A' - 1;
3336 else
3337 line[0] = bytes - 26 + 'a' - 1;
3338 encode_85(line + 1, cp, bytes);
Florian Forster1d7f1712006-06-18 17:18:09 +02003339 cp = (char *) cp + bytes;
Stefan Beller4eed0eb2017-06-29 17:07:01 -07003340
3341 len = strlen(line);
3342 line[len++] = '\n';
3343 line[len] = '\0';
3344
3345 emit_diff_symbol(o, DIFF_SYMBOL_BINARY_DIFF_BODY,
3346 line, len, 0);
Junio C Hamano051308f2006-05-04 16:51:44 -07003347 }
Stefan Beller4eed0eb2017-06-29 17:07:01 -07003348 emit_diff_symbol(o, DIFF_SYMBOL_BINARY_DIFF_FOOTER, NULL, 0, 0);
Junio C Hamano06606262006-05-05 02:41:53 -07003349 free(data);
Junio C Hamano051308f2006-05-04 16:51:44 -07003350}
3351
Stefan Beller4eed0eb2017-06-29 17:07:01 -07003352static void emit_binary_diff(struct diff_options *o,
3353 mmfile_t *one, mmfile_t *two)
Junio C Hamanod4c452f2006-08-16 16:08:14 -07003354{
Stefan Beller4eed0eb2017-06-29 17:07:01 -07003355 emit_diff_symbol(o, DIFF_SYMBOL_BINARY_DIFF_HEADER, NULL, 0, 0);
3356 emit_binary_diff_body(o, one, two);
3357 emit_binary_diff_body(o, two, one);
Junio C Hamanod4c452f2006-08-16 16:08:14 -07003358}
3359
Nguyễn Thái Ngọc Duyb78ea5f2018-09-21 17:57:19 +02003360int diff_filespec_is_binary(struct repository *r,
3361 struct diff_filespec *one)
Junio C Hamano29a3eef2007-07-06 00:18:54 -07003362{
Jonathan Tan1c37e862020-04-07 15:11:41 -07003363 struct diff_populate_filespec_options dpf_options = {
3364 .check_binary = 1,
3365 };
3366
Jeff King122aa6f2008-10-05 17:43:36 -04003367 if (one->is_binary == -1) {
Nguyễn Thái Ngọc Duyacd00ea2018-09-21 17:57:33 +02003368 diff_filespec_load_driver(one, r->index);
Jeff King122aa6f2008-10-05 17:43:36 -04003369 if (one->driver->binary != -1)
3370 one->is_binary = one->driver->binary;
3371 else {
3372 if (!one->data && DIFF_FILE_VALID(one))
Jonathan Tan1c37e862020-04-07 15:11:41 -07003373 diff_populate_filespec(r, one, &dpf_options);
Nguyễn Thái Ngọc Duy6bf3b812014-08-16 10:08:05 +07003374 if (one->is_binary == -1 && one->data)
Jeff King122aa6f2008-10-05 17:43:36 -04003375 one->is_binary = buffer_is_binary(one->data,
3376 one->size);
3377 if (one->is_binary == -1)
3378 one->is_binary = 0;
3379 }
3380 }
Junio C Hamano29a3eef2007-07-06 00:18:54 -07003381 return one->is_binary;
Junio C Hamano6973dca2006-04-21 23:57:45 -07003382}
3383
Nguyễn Thái Ngọc Duyacd00ea2018-09-21 17:57:33 +02003384static const struct userdiff_funcname *
3385diff_funcname_pattern(struct diff_options *o, struct diff_filespec *one)
Junio C Hamanof2584752007-07-06 00:45:10 -07003386{
Nguyễn Thái Ngọc Duyacd00ea2018-09-21 17:57:33 +02003387 diff_filespec_load_driver(one, o->repo->index);
Jeff King122aa6f2008-10-05 17:43:36 -04003388 return one->driver->funcname.pattern ? &one->driver->funcname : NULL;
Junio C Hamanof2584752007-07-06 00:45:10 -07003389}
3390
Junio C Hamanoa5a818e2008-08-18 20:08:09 -07003391void diff_set_mnemonic_prefix(struct diff_options *options, const char *a, const char *b)
3392{
3393 if (!options->a_prefix)
3394 options->a_prefix = a;
3395 if (!options->b_prefix)
3396 options->b_prefix = b;
3397}
3398
Nguyễn Thái Ngọc Duybd7ad452018-11-10 06:49:06 +01003399struct userdiff_driver *get_textconv(struct repository *r,
Nguyễn Thái Ngọc Duyacd00ea2018-09-21 17:57:33 +02003400 struct diff_filespec *one)
Jeff King04427ac2008-10-26 00:44:53 -04003401{
3402 if (!DIFF_FILE_VALID(one))
3403 return NULL;
Jeff Kingd391c0f2010-09-21 17:01:24 -04003404
Nguyễn Thái Ngọc Duybd7ad452018-11-10 06:49:06 +01003405 diff_filespec_load_driver(one, r->index);
3406 return userdiff_get_textconv(r, one->driver);
Jeff King04427ac2008-10-26 00:44:53 -04003407}
3408
Junio C Hamano6973dca2006-04-21 23:57:45 -07003409static void builtin_diff(const char *name_a,
3410 const char *name_b,
3411 struct diff_filespec *one,
3412 struct diff_filespec *two,
3413 const char *xfrm_msg,
Christian Couder296c6bb2010-05-26 04:50:12 +02003414 int must_show_header,
Junio C Hamano051308f2006-05-04 16:51:44 -07003415 struct diff_options *o,
Junio C Hamano6973dca2006-04-21 23:57:45 -07003416 int complete_rewrite)
3417{
3418 mmfile_t mf1, mf2;
3419 const char *lbl[2];
3420 char *a_one, *b_two;
Jeff Kingd9c552f2012-11-13 21:12:46 +05303421 const char *meta = diff_get_color_opt(o, DIFF_METAINFO);
Pierre Habouzit8f67f8a2007-11-10 20:05:14 +01003422 const char *reset = diff_get_color_opt(o, DIFF_RESET);
Junio C Hamanoa5a818e2008-08-18 20:08:09 -07003423 const char *a_prefix, *b_prefix;
Jeff Kingd9bae1a2010-04-01 20:12:15 -04003424 struct userdiff_driver *textconv_one = NULL;
3425 struct userdiff_driver *textconv_two = NULL;
Greg Bacon3e97c7c2009-11-19 15:12:24 -06003426 struct strbuf header = STRBUF_INIT;
John Keeping30997bb2013-02-07 20:15:27 +00003427 const char *line_prefix = diff_line_prefix(o);
Junio C Hamano6973dca2006-04-21 23:57:45 -07003428
Junio C Hamanoa5a818e2008-08-18 20:08:09 -07003429 diff_set_mnemonic_prefix(o, "a/", "b/");
Brandon Williams0d1e0e72017-10-31 11:19:11 -07003430 if (o->flags.reverse_diff) {
Junio C Hamanoa5a818e2008-08-18 20:08:09 -07003431 a_prefix = o->b_prefix;
3432 b_prefix = o->a_prefix;
3433 } else {
3434 a_prefix = o->a_prefix;
3435 b_prefix = o->b_prefix;
3436 }
3437
Jacob Keller61cfbc02016-08-31 16:27:21 -07003438 if (o->submodule_format == DIFF_SUBMODULE_LOG &&
3439 (!one->mode || S_ISGITLINK(one->mode)) &&
3440 (!two->mode || S_ISGITLINK(two->mode))) {
Shourya Shukla180b1542020-08-13 01:14:02 +05303441 show_submodule_diff_summary(o, one->path ? one->path : two->path,
Jacob Keller602a2832016-08-31 16:27:23 -07003442 &one->oid, &two->oid,
Stefan Bellerf3597132017-06-29 17:07:00 -07003443 two->dirty_submodule);
Junio C Hamano6973dca2006-04-21 23:57:45 -07003444 return;
Jacob Kellerfd47ae62016-08-31 16:27:25 -07003445 } else if (o->submodule_format == DIFF_SUBMODULE_INLINE_DIFF &&
3446 (!one->mode || S_ISGITLINK(one->mode)) &&
3447 (!two->mode || S_ISGITLINK(two->mode))) {
Stefan Bellerf3597132017-06-29 17:07:00 -07003448 show_submodule_inline_diff(o, one->path ? one->path : two->path,
Jacob Kellerfd47ae62016-08-31 16:27:25 -07003449 &one->oid, &two->oid,
Stefan Bellerf3597132017-06-29 17:07:00 -07003450 two->dirty_submodule);
Jacob Kellerfd47ae62016-08-31 16:27:25 -07003451 return;
Junio C Hamano6973dca2006-04-21 23:57:45 -07003452 }
3453
Brandon Williams0d1e0e72017-10-31 11:19:11 -07003454 if (o->flags.allow_textconv) {
Nguyễn Thái Ngọc Duybd7ad452018-11-10 06:49:06 +01003455 textconv_one = get_textconv(o->repo, one);
3456 textconv_two = get_textconv(o->repo, two);
Junio C Hamano6973dca2006-04-21 23:57:45 -07003457 }
3458
Linus Torvalds71b989e2008-10-05 15:35:15 -04003459 /* Never use a non-valid filename anywhere if at all possible */
3460 name_a = DIFF_FILE_VALID(one) ? name_a : name_b;
3461 name_b = DIFF_FILE_VALID(two) ? name_b : name_a;
3462
Junio C Hamanoa5a818e2008-08-18 20:08:09 -07003463 a_one = quote_two(a_prefix, name_a + (*name_a == '/'));
3464 b_two = quote_two(b_prefix, name_b + (*name_b == '/'));
Junio C Hamano6973dca2006-04-21 23:57:45 -07003465 lbl[0] = DIFF_FILE_VALID(one) ? a_one : "/dev/null";
3466 lbl[1] = DIFF_FILE_VALID(two) ? b_two : "/dev/null";
Jeff Kingd9c552f2012-11-13 21:12:46 +05303467 strbuf_addf(&header, "%s%sdiff --git %s %s%s\n", line_prefix, meta, a_one, b_two, reset);
Junio C Hamano6973dca2006-04-21 23:57:45 -07003468 if (lbl[0][0] == '/') {
3469 /* /dev/null */
Jeff Kingd9c552f2012-11-13 21:12:46 +05303470 strbuf_addf(&header, "%s%snew file mode %06o%s\n", line_prefix, meta, two->mode, reset);
Bert Wesarg37466442010-05-04 00:38:07 +02003471 if (xfrm_msg)
3472 strbuf_addstr(&header, xfrm_msg);
Christian Couder296c6bb2010-05-26 04:50:12 +02003473 must_show_header = 1;
Junio C Hamano6973dca2006-04-21 23:57:45 -07003474 }
3475 else if (lbl[1][0] == '/') {
Jeff Kingd9c552f2012-11-13 21:12:46 +05303476 strbuf_addf(&header, "%s%sdeleted file mode %06o%s\n", line_prefix, meta, one->mode, reset);
Bert Wesarg37466442010-05-04 00:38:07 +02003477 if (xfrm_msg)
3478 strbuf_addstr(&header, xfrm_msg);
Christian Couder296c6bb2010-05-26 04:50:12 +02003479 must_show_header = 1;
Junio C Hamano6973dca2006-04-21 23:57:45 -07003480 }
3481 else {
3482 if (one->mode != two->mode) {
Jeff Kingd9c552f2012-11-13 21:12:46 +05303483 strbuf_addf(&header, "%s%sold mode %06o%s\n", line_prefix, meta, one->mode, reset);
3484 strbuf_addf(&header, "%s%snew mode %06o%s\n", line_prefix, meta, two->mode, reset);
Christian Couder296c6bb2010-05-26 04:50:12 +02003485 must_show_header = 1;
Junio C Hamano6973dca2006-04-21 23:57:45 -07003486 }
Bert Wesarg37466442010-05-04 00:38:07 +02003487 if (xfrm_msg)
3488 strbuf_addstr(&header, xfrm_msg);
Greg Bacon3e97c7c2009-11-19 15:12:24 -06003489
Junio C Hamano6973dca2006-04-21 23:57:45 -07003490 /*
3491 * we do not run diff between different kind
3492 * of objects.
3493 */
3494 if ((one->mode ^ two->mode) & S_IFMT)
3495 goto free_ab_and_return;
Jeff King0c018572008-12-09 03:12:28 -05003496 if (complete_rewrite &&
Nguyễn Thái Ngọc Duyb78ea5f2018-09-21 17:57:19 +02003497 (textconv_one || !diff_filespec_is_binary(o->repo, one)) &&
3498 (textconv_two || !diff_filespec_is_binary(o->repo, two))) {
Stefan Bellera29b0a12017-06-29 17:06:57 -07003499 emit_diff_symbol(o, DIFF_SYMBOL_HEADER,
3500 header.buf, header.len, 0);
Greg Bacon3e97c7c2009-11-19 15:12:24 -06003501 strbuf_reset(&header);
Jeff King3aa1f7c2008-12-09 03:13:21 -05003502 emit_rewrite_diff(name_a, name_b, one, two,
Nguyễn Thái Ngọc Duyb78ea5f2018-09-21 17:57:19 +02003503 textconv_one, textconv_two, o);
Johannes Schindelin34a5e1a2007-02-25 23:34:54 +01003504 o->found_changes = 1;
Junio C Hamano6973dca2006-04-21 23:57:45 -07003505 goto free_ab_and_return;
3506 }
3507 }
3508
Junio C Hamano467ddc12011-02-28 16:11:55 -08003509 if (o->irreversible_delete && lbl[1][0] == '/') {
Stefan Bellera29b0a12017-06-29 17:06:57 -07003510 emit_diff_symbol(o, DIFF_SYMBOL_HEADER, header.buf,
3511 header.len, 0);
Junio C Hamano467ddc12011-02-28 16:11:55 -08003512 strbuf_reset(&header);
3513 goto free_ab_and_return;
Brandon Williams0d1e0e72017-10-31 11:19:11 -07003514 } else if (!o->flags.text &&
Nguyễn Thái Ngọc Duyb78ea5f2018-09-21 17:57:19 +02003515 ( (!textconv_one && diff_filespec_is_binary(o->repo, one)) ||
3516 (!textconv_two && diff_filespec_is_binary(o->repo, two)) )) {
Stefan Beller4acaaa72017-06-29 17:06:58 -07003517 struct strbuf sb = STRBUF_INIT;
Nguyễn Thái Ngọc Duy1aaf69e2014-08-16 10:08:06 +07003518 if (!one->data && !two->data &&
3519 S_ISREG(one->mode) && S_ISREG(two->mode) &&
Brandon Williams0d1e0e72017-10-31 11:19:11 -07003520 !o->flags.binary) {
Jeff King4a7e27e2018-08-28 17:22:40 -04003521 if (oideq(&one->oid, &two->oid)) {
Nguyễn Thái Ngọc Duy1aaf69e2014-08-16 10:08:06 +07003522 if (must_show_header)
Stefan Bellera29b0a12017-06-29 17:06:57 -07003523 emit_diff_symbol(o, DIFF_SYMBOL_HEADER,
3524 header.buf, header.len,
3525 0);
Nguyễn Thái Ngọc Duy1aaf69e2014-08-16 10:08:06 +07003526 goto free_ab_and_return;
3527 }
Stefan Bellera29b0a12017-06-29 17:06:57 -07003528 emit_diff_symbol(o, DIFF_SYMBOL_HEADER,
3529 header.buf, header.len, 0);
Stefan Beller4acaaa72017-06-29 17:06:58 -07003530 strbuf_addf(&sb, "%sBinary files %s and %s differ\n",
3531 diff_line_prefix(o), lbl[0], lbl[1]);
3532 emit_diff_symbol(o, DIFF_SYMBOL_BINARY_FILES,
3533 sb.buf, sb.len, 0);
3534 strbuf_release(&sb);
Nguyễn Thái Ngọc Duy1aaf69e2014-08-16 10:08:06 +07003535 goto free_ab_and_return;
3536 }
Nguyễn Thái Ngọc Duyb78ea5f2018-09-21 17:57:19 +02003537 if (fill_mmfile(o->repo, &mf1, one) < 0 ||
3538 fill_mmfile(o->repo, &mf2, two) < 0)
Jeff Kingb3373982010-04-01 20:14:24 -04003539 die("unable to read files to diff");
Junio C Hamano06606262006-05-05 02:41:53 -07003540 /* Quite common confusing case */
3541 if (mf1.size == mf2.size &&
Christian Couder296c6bb2010-05-26 04:50:12 +02003542 !memcmp(mf1.ptr, mf2.ptr, mf1.size)) {
3543 if (must_show_header)
Stefan Bellera29b0a12017-06-29 17:06:57 -07003544 emit_diff_symbol(o, DIFF_SYMBOL_HEADER,
3545 header.buf, header.len, 0);
Junio C Hamano06606262006-05-05 02:41:53 -07003546 goto free_ab_and_return;
Christian Couder296c6bb2010-05-26 04:50:12 +02003547 }
Stefan Bellera29b0a12017-06-29 17:06:57 -07003548 emit_diff_symbol(o, DIFF_SYMBOL_HEADER, header.buf, header.len, 0);
Greg Bacon3e97c7c2009-11-19 15:12:24 -06003549 strbuf_reset(&header);
Brandon Williams0d1e0e72017-10-31 11:19:11 -07003550 if (o->flags.binary)
Stefan Beller4eed0eb2017-06-29 17:07:01 -07003551 emit_binary_diff(o, &mf1, &mf2);
Stefan Beller4acaaa72017-06-29 17:06:58 -07003552 else {
3553 strbuf_addf(&sb, "%sBinary files %s and %s differ\n",
3554 diff_line_prefix(o), lbl[0], lbl[1]);
3555 emit_diff_symbol(o, DIFF_SYMBOL_BINARY_FILES,
3556 sb.buf, sb.len, 0);
3557 strbuf_release(&sb);
3558 }
Johannes Schindelin34a5e1a2007-02-25 23:34:54 +01003559 o->found_changes = 1;
Junio C Hamano467ddc12011-02-28 16:11:55 -08003560 } else {
Junio C Hamano6973dca2006-04-21 23:57:45 -07003561 /* Crazy xdl interfaces.. */
Jeff King0da0e922019-01-11 17:17:22 -05003562 const char *diffopts;
Jeff Kingae021d82014-06-18 15:47:50 -04003563 const char *v;
Junio C Hamano6973dca2006-04-21 23:57:45 -07003564 xpparam_t xpp;
3565 xdemitconf_t xecfg;
Junio C Hamano6973dca2006-04-21 23:57:45 -07003566 struct emit_callback ecbdata;
Jeff Kingbe58e702008-10-05 17:43:21 -04003567 const struct userdiff_funcname *pe;
Junio C Hamanof2584752007-07-06 00:45:10 -07003568
Junio C Hamanob3f01ff2012-02-29 18:14:16 -08003569 if (must_show_header) {
Stefan Bellera29b0a12017-06-29 17:06:57 -07003570 emit_diff_symbol(o, DIFF_SYMBOL_HEADER,
3571 header.buf, header.len, 0);
Greg Bacon3e97c7c2009-11-19 15:12:24 -06003572 strbuf_reset(&header);
3573 }
3574
Nguyễn Thái Ngọc Duy6afaf802018-09-21 17:57:22 +02003575 mf1.size = fill_textconv(o->repo, textconv_one, one, &mf1.ptr);
3576 mf2.size = fill_textconv(o->repo, textconv_two, two, &mf2.ptr);
Jeff King04427ac2008-10-26 00:44:53 -04003577
Nguyễn Thái Ngọc Duyacd00ea2018-09-21 17:57:33 +02003578 pe = diff_funcname_pattern(o, one);
Brandon Casey45e7ca02008-09-18 17:40:48 -05003579 if (!pe)
Nguyễn Thái Ngọc Duyacd00ea2018-09-21 17:57:33 +02003580 pe = diff_funcname_pattern(o, two);
Junio C Hamano6973dca2006-04-21 23:57:45 -07003581
Brian Downing9ccd0a82008-10-25 15:30:37 +02003582 memset(&xpp, 0, sizeof(xpp));
Johannes Schindelin30b25012007-07-04 19:05:46 +01003583 memset(&xecfg, 0, sizeof(xecfg));
Johannes Schindelincd112ce2006-06-13 18:45:44 +02003584 memset(&ecbdata, 0, sizeof(ecbdata));
Johannes Schindelin1cdde292018-08-13 04:33:11 -07003585 if (o->flags.suppress_diff_headers)
3586 lbl[0] = NULL;
Junio C Hamano6973dca2006-04-21 23:57:45 -07003587 ecbdata.label_path = lbl;
Jeff Kingdaa0c3d2011-08-17 22:04:23 -07003588 ecbdata.color_diff = want_color(o->use_color);
Nguyễn Thái Ngọc Duy26d024e2018-09-21 17:57:37 +02003589 ecbdata.ws_rule = whitespace_rule(o->repo->index, name_b);
Junio C Hamano690ed842009-09-04 00:41:15 -07003590 if (ecbdata.ws_rule & WS_BLANK_AT_EOF)
Junio C Hamanod68fe262009-09-14 22:05:57 -07003591 check_blank_at_eof(&mf1, &mf2, &ecbdata);
Bo Yanga3c158d2010-05-26 15:08:02 +08003592 ecbdata.opt = o;
Johannes Schindelin1cdde292018-08-13 04:33:11 -07003593 if (header.len && !o->flags.suppress_diff_headers)
3594 ecbdata.header = &header;
René Scharfe582aa002010-05-02 15:04:41 +02003595 xpp.flags = o->xdl_opts;
Michał Kępień296d4a92020-10-20 08:48:09 +02003596 xpp.ignore_regex = o->ignore_regex;
3597 xpp.ignore_regex_nr = o->ignore_regex_nr;
Jonathan Tan2477ab22017-11-27 11:47:47 -08003598 xpp.anchors = o->anchors;
3599 xpp.anchors_nr = o->anchors_nr;
Linus Torvaldsee1e5412006-05-13 13:23:48 -07003600 xecfg.ctxlen = o->context;
René Scharfe6d0e6742008-12-28 19:45:32 +01003601 xecfg.interhunkctxlen = o->interhunkcontext;
Junio C Hamano6973dca2006-04-21 23:57:45 -07003602 xecfg.flags = XDL_EMIT_FUNCNAMES;
Brandon Williams0d1e0e72017-10-31 11:19:11 -07003603 if (o->flags.funccontext)
René Scharfe14937c22011-10-09 13:36:57 +02003604 xecfg.flags |= XDL_EMIT_FUNCCONTEXT;
Brandon Casey45e7ca02008-09-18 17:40:48 -05003605 if (pe)
Brandon Caseya0135852008-09-18 17:42:48 -05003606 xdiff_set_find_func(&xecfg, pe->pattern, pe->cflags);
Jeff King0da0e922019-01-11 17:17:22 -05003607
3608 diffopts = getenv("GIT_DIFF_OPTS");
Junio C Hamano6973dca2006-04-21 23:57:45 -07003609 if (!diffopts)
3610 ;
Jeff Kingae021d82014-06-18 15:47:50 -04003611 else if (skip_prefix(diffopts, "--unified=", &v))
3612 xecfg.ctxlen = strtoul(v, NULL, 10);
3613 else if (skip_prefix(diffopts, "-u", &v))
3614 xecfg.ctxlen = strtoul(v, NULL, 10);
Jeff King0da0e922019-01-11 17:17:22 -05003615
Thomas Rast77d1a522012-03-14 19:24:08 +01003616 if (o->word_diff)
3617 init_diff_words_data(&ecbdata, o, one, two);
Jeff King9346d6d2018-11-02 02:35:45 -04003618 if (xdi_diff_outf(&mf1, &mf2, NULL, fn_out_consume,
3619 &ecbdata, &xpp, &xecfg))
Jeff King3efb9882015-09-24 19:12:23 -04003620 die("unable to generate diff for %s", one->path);
Thomas Rast882749a2010-04-14 17:59:06 +02003621 if (o->word_diff)
Johannes Schindelinf59a59e2006-07-28 23:56:15 +02003622 free_diff_words_data(&ecbdata);
Jeff King04427ac2008-10-26 00:44:53 -04003623 if (textconv_one)
3624 free(mf1.ptr);
3625 if (textconv_two)
3626 free(mf2.ptr);
René Scharfe8cfe5f12009-07-02 00:01:43 +02003627 xdiff_clear_find_func(&xecfg);
Junio C Hamano6973dca2006-04-21 23:57:45 -07003628 }
3629
3630 free_ab_and_return:
Greg Bacon3e97c7c2009-11-19 15:12:24 -06003631 strbuf_release(&header);
Junio C Hamanofc3abdf2007-05-03 13:05:48 -07003632 diff_free_filespec_data(one);
3633 diff_free_filespec_data(two);
Junio C Hamano6973dca2006-04-21 23:57:45 -07003634 free(a_one);
3635 free(b_two);
3636 return;
3637}
3638
Nguyễn Thái Ngọc Duyddf88fa2018-02-24 21:09:59 +07003639static char *get_compact_summary(const struct diff_filepair *p, int is_renamed)
3640{
3641 if (!is_renamed) {
3642 if (p->status == DIFF_STATUS_ADDED) {
3643 if (S_ISLNK(p->two->mode))
3644 return "new +l";
3645 else if ((p->two->mode & 0777) == 0755)
3646 return "new +x";
3647 else
3648 return "new";
3649 } else if (p->status == DIFF_STATUS_DELETED)
3650 return "gone";
3651 }
3652 if (S_ISLNK(p->one->mode) && !S_ISLNK(p->two->mode))
3653 return "mode -l";
3654 else if (!S_ISLNK(p->one->mode) && S_ISLNK(p->two->mode))
3655 return "mode +l";
3656 else if ((p->one->mode & 0777) == 0644 &&
3657 (p->two->mode & 0777) == 0755)
3658 return "mode +x";
3659 else if ((p->one->mode & 0777) == 0755 &&
3660 (p->two->mode & 0777) == 0644)
3661 return "mode -x";
3662 return NULL;
3663}
3664
Junio C Hamano6973dca2006-04-21 23:57:45 -07003665static void builtin_diffstat(const char *name_a, const char *name_b,
3666 struct diff_filespec *one,
3667 struct diff_filespec *two,
Junio C Hamano710158e2006-04-25 23:40:09 -07003668 struct diffstat_t *diffstat,
Johannes Schindelin0d21efa2006-06-14 17:40:23 +02003669 struct diff_options *o,
Linus Torvalds74faaa12012-10-17 10:00:37 -07003670 struct diff_filepair *p)
Junio C Hamano6973dca2006-04-21 23:57:45 -07003671{
3672 mmfile_t mf1, mf2;
3673 struct diffstat_file *data;
Thomas Guyot-Sionnestff0c7fa2020-09-24 03:41:41 -04003674 int may_differ;
Linus Torvalds74faaa12012-10-17 10:00:37 -07003675 int complete_rewrite = 0;
3676
3677 if (!DIFF_PAIR_UNMERGED(p)) {
3678 if (p->status == DIFF_STATUS_MODIFIED && p->score)
3679 complete_rewrite = 1;
3680 }
Junio C Hamano6973dca2006-04-21 23:57:45 -07003681
3682 data = diffstat_add(diffstat, name_a, name_b);
Junio C Hamano99bfd402012-11-27 11:17:14 -08003683 data->is_interesting = p->status != DIFF_STATUS_UNKNOWN;
Nguyễn Thái Ngọc Duyddf88fa2018-02-24 21:09:59 +07003684 if (o->flags.stat_with_summary)
3685 data->comments = get_compact_summary(p, data->is_renamed);
Junio C Hamano6973dca2006-04-21 23:57:45 -07003686
3687 if (!one || !two) {
3688 data->is_unmerged = 1;
3689 return;
3690 }
Jeff Kingded0abc2011-02-19 03:04:56 -05003691
Thomas Guyot-Sionnestff0c7fa2020-09-24 03:41:41 -04003692 /* saves some reads if true, not a guarantee of diff outcome */
3693 may_differ = !(one->oid_valid && two->oid_valid &&
3694 oideq(&one->oid, &two->oid));
Zbigniew Jędrzejewski-Szmek352ca4e2012-05-01 19:10:15 +02003695
Nguyễn Thái Ngọc Duyb78ea5f2018-09-21 17:57:19 +02003696 if (diff_filespec_is_binary(o->repo, one) ||
3697 diff_filespec_is_binary(o->repo, two)) {
Jeff Kingded0abc2011-02-19 03:04:56 -05003698 data->is_binary = 1;
Thomas Guyot-Sionnestff0c7fa2020-09-24 03:41:41 -04003699 if (!may_differ) {
Zbigniew Jędrzejewski-Szmeke18872b2012-05-01 19:10:14 +02003700 data->added = 0;
3701 data->deleted = 0;
3702 } else {
Nguyễn Thái Ngọc Duyb78ea5f2018-09-21 17:57:19 +02003703 data->added = diff_filespec_size(o->repo, two);
3704 data->deleted = diff_filespec_size(o->repo, one);
Zbigniew Jędrzejewski-Szmeke18872b2012-05-01 19:10:14 +02003705 }
Jeff Kingded0abc2011-02-19 03:04:56 -05003706 }
3707
3708 else if (complete_rewrite) {
Jonathan Tan1c37e862020-04-07 15:11:41 -07003709 diff_populate_filespec(o->repo, one, NULL);
3710 diff_populate_filespec(o->repo, two, NULL);
Junio C Hamano710158e2006-04-25 23:40:09 -07003711 data->deleted = count_lines(one->data, one->size);
3712 data->added = count_lines(two->data, two->size);
Junio C Hamano710158e2006-04-25 23:40:09 -07003713 }
Junio C Hamano6973dca2006-04-21 23:57:45 -07003714
Thomas Guyot-Sionnestff0c7fa2020-09-24 03:41:41 -04003715 else if (may_differ) {
Junio C Hamano6973dca2006-04-21 23:57:45 -07003716 /* Crazy xdl interfaces.. */
3717 xpparam_t xpp;
3718 xdemitconf_t xecfg;
Junio C Hamano6973dca2006-04-21 23:57:45 -07003719
Nguyễn Thái Ngọc Duyb78ea5f2018-09-21 17:57:19 +02003720 if (fill_mmfile(o->repo, &mf1, one) < 0 ||
3721 fill_mmfile(o->repo, &mf2, two) < 0)
Jeff Kingded0abc2011-02-19 03:04:56 -05003722 die("unable to read files to diff");
3723
Brian Downing9ccd0a82008-10-25 15:30:37 +02003724 memset(&xpp, 0, sizeof(xpp));
Johannes Schindelin30b25012007-07-04 19:05:46 +01003725 memset(&xecfg, 0, sizeof(xecfg));
René Scharfe582aa002010-05-02 15:04:41 +02003726 xpp.flags = o->xdl_opts;
Michał Kępień296d4a92020-10-20 08:48:09 +02003727 xpp.ignore_regex = o->ignore_regex;
3728 xpp.ignore_regex_nr = o->ignore_regex_nr;
Jonathan Tan2477ab22017-11-27 11:47:47 -08003729 xpp.anchors = o->anchors;
3730 xpp.anchors_nr = o->anchors_nr;
Junio C Hamanof01cae92011-09-22 10:54:47 -07003731 xecfg.ctxlen = o->context;
3732 xecfg.interhunkctxlen = o->interhunkcontext;
Ævar Arnfjörð Bjarmason5d934602021-04-12 19:15:29 +02003733 xecfg.flags = XDL_EMIT_NO_HUNK_HDR;
3734 if (xdi_diff_outf(&mf1, &mf2, NULL,
Jeff King3b40a092018-11-02 02:36:06 -04003735 diffstat_consume, diffstat, &xpp, &xecfg))
Jeff King3efb9882015-09-24 19:12:23 -04003736 die("unable to generate diffstat for %s", one->path);
Matthew Rogers1cf3d5d2020-08-20 00:41:32 +00003737
3738 if (DIFF_FILE_VALID(one) && DIFF_FILE_VALID(two)) {
3739 struct diffstat_file *file =
3740 diffstat->files[diffstat->nr - 1];
3741 /*
3742 * Omit diffstats of modified files where nothing changed.
Thomas Guyot-Sionnestff0c7fa2020-09-24 03:41:41 -04003743 * Even if may_differ, this might be the case due to
Matthew Rogers1cf3d5d2020-08-20 00:41:32 +00003744 * ignoring whitespace changes, etc.
3745 *
3746 * But note that we special-case additions, deletions,
3747 * renames, and mode changes as adding an empty file,
3748 * for example is still of interest.
3749 */
3750 if ((p->status == DIFF_STATUS_MODIFIED)
3751 && !file->added
3752 && !file->deleted
3753 && one->mode == two->mode) {
3754 free_diffstat_file(file);
3755 diffstat->nr--;
3756 }
3757 }
Junio C Hamano6973dca2006-04-21 23:57:45 -07003758 }
Junio C Hamanofc3abdf2007-05-03 13:05:48 -07003759
Junio C Hamanofc3abdf2007-05-03 13:05:48 -07003760 diff_free_filespec_data(one);
3761 diff_free_filespec_data(two);
Junio C Hamano6973dca2006-04-21 23:57:45 -07003762}
3763
Johannes Schindelin88246892006-05-20 23:43:13 +02003764static void builtin_checkdiff(const char *name_a, const char *name_b,
Junio C Hamanocd676a52008-02-12 14:26:02 -08003765 const char *attr_path,
Junio C Hamano5ff10dd2008-06-26 15:34:54 -07003766 struct diff_filespec *one,
3767 struct diff_filespec *two,
3768 struct diff_options *o)
Johannes Schindelin88246892006-05-20 23:43:13 +02003769{
3770 mmfile_t mf1, mf2;
3771 struct checkdiff_t data;
3772
3773 if (!two)
3774 return;
3775
3776 memset(&data, 0, sizeof(data));
Johannes Schindelin88246892006-05-20 23:43:13 +02003777 data.filename = name_b ? name_b : name_a;
3778 data.lineno = 0;
Junio C Hamano1ba111d2008-06-26 15:36:34 -07003779 data.o = o;
Nguyễn Thái Ngọc Duy26d024e2018-09-21 17:57:37 +02003780 data.ws_rule = whitespace_rule(o->repo->index, attr_path);
Nguyễn Thái Ngọc Duy32eaa462018-09-21 17:57:27 +02003781 data.conflict_marker_size = ll_merge_marker_size(o->repo->index, attr_path);
Johannes Schindelin88246892006-05-20 23:43:13 +02003782
Nguyễn Thái Ngọc Duyb78ea5f2018-09-21 17:57:19 +02003783 if (fill_mmfile(o->repo, &mf1, one) < 0 ||
3784 fill_mmfile(o->repo, &mf2, two) < 0)
Johannes Schindelin88246892006-05-20 23:43:13 +02003785 die("unable to read files to diff");
3786
Junio C Hamano5ff10dd2008-06-26 15:34:54 -07003787 /*
3788 * All the other codepaths check both sides, but not checking
3789 * the "old" side here is deliberate. We are checking the newly
3790 * introduced changes, and as long as the "new" side is text, we
3791 * can and should check what it introduces.
3792 */
Nguyễn Thái Ngọc Duyb78ea5f2018-09-21 17:57:19 +02003793 if (diff_filespec_is_binary(o->repo, two))
Junio C Hamanofc3abdf2007-05-03 13:05:48 -07003794 goto free_and_return;
Johannes Schindelin88246892006-05-20 23:43:13 +02003795 else {
3796 /* Crazy xdl interfaces.. */
3797 xpparam_t xpp;
3798 xdemitconf_t xecfg;
Johannes Schindelin88246892006-05-20 23:43:13 +02003799
Brian Downing9ccd0a82008-10-25 15:30:37 +02003800 memset(&xpp, 0, sizeof(xpp));
Johannes Schindelin30b25012007-07-04 19:05:46 +01003801 memset(&xecfg, 0, sizeof(xecfg));
Junio C Hamanoc35539e2008-08-20 11:47:55 -07003802 xecfg.ctxlen = 1; /* at least one context line */
René Scharfe582aa002010-05-02 15:04:41 +02003803 xpp.flags = 0;
Jeff King75ab7632018-11-02 02:39:03 -04003804 if (xdi_diff_outf(&mf1, &mf2, checkdiff_consume_hunk,
3805 checkdiff_consume, &data,
Jeff King3efb9882015-09-24 19:12:23 -04003806 &xpp, &xecfg))
3807 die("unable to generate checkdiff for %s", one->path);
Junio C Hamano877f23c2008-06-26 15:36:59 -07003808
Junio C Hamano467babf2009-09-03 23:39:43 -07003809 if (data.ws_rule & WS_BLANK_AT_EOF) {
Junio C Hamanod68fe262009-09-14 22:05:57 -07003810 struct emit_callback ecbdata;
3811 int blank_at_eof;
3812
3813 ecbdata.ws_rule = data.ws_rule;
3814 check_blank_at_eof(&mf1, &mf2, &ecbdata);
Christoph Mallon8837d332010-10-10 19:24:06 +02003815 blank_at_eof = ecbdata.blank_at_eof_in_postimage;
Junio C Hamanod68fe262009-09-14 22:05:57 -07003816
Junio C Hamano467babf2009-09-03 23:39:43 -07003817 if (blank_at_eof) {
3818 static char *err;
3819 if (!err)
3820 err = whitespace_error_string(WS_BLANK_AT_EOF);
3821 fprintf(o->file, "%s:%d: %s.\n",
3822 data.filename, blank_at_eof, err);
3823 data.status = 1; /* report errors */
3824 }
Junio C Hamano877f23c2008-06-26 15:36:59 -07003825 }
Johannes Schindelin88246892006-05-20 23:43:13 +02003826 }
Junio C Hamanofc3abdf2007-05-03 13:05:48 -07003827 free_and_return:
3828 diff_free_filespec_data(one);
3829 diff_free_filespec_data(two);
Wincent Colaiuta62c64892007-12-13 21:24:52 +01003830 if (data.status)
Brandon Williams0d1e0e72017-10-31 11:19:11 -07003831 o->flags.check_failed = 1;
Johannes Schindelin88246892006-05-20 23:43:13 +02003832}
3833
Junio C Hamano6973dca2006-04-21 23:57:45 -07003834struct diff_filespec *alloc_filespec(const char *path)
3835{
Jeff King96ffc062016-02-22 17:44:32 -05003836 struct diff_filespec *spec;
Junio C Hamano6973dca2006-04-21 23:57:45 -07003837
Jeff King96ffc062016-02-22 17:44:32 -05003838 FLEXPTR_ALLOC_STR(spec, path, path);
Linus Torvalds9fb88412007-10-25 11:19:10 -07003839 spec->count = 1;
Jeff King122aa6f2008-10-05 17:43:36 -04003840 spec->is_binary = -1;
Junio C Hamano6973dca2006-04-21 23:57:45 -07003841 return spec;
3842}
3843
Linus Torvalds9fb88412007-10-25 11:19:10 -07003844void free_filespec(struct diff_filespec *spec)
3845{
3846 if (!--spec->count) {
3847 diff_free_filespec_data(spec);
3848 free(spec);
3849 }
3850}
3851
Brandon Williamsf9704c22017-05-30 10:30:50 -07003852void fill_filespec(struct diff_filespec *spec, const struct object_id *oid,
3853 int oid_valid, unsigned short mode)
Junio C Hamano6973dca2006-04-21 23:57:45 -07003854{
3855 if (mode) {
3856 spec->mode = canon_mode(mode);
Brandon Williamsf9704c22017-05-30 10:30:50 -07003857 oidcpy(&spec->oid, oid);
3858 spec->oid_valid = oid_valid;
Junio C Hamano6973dca2006-04-21 23:57:45 -07003859 }
3860}
3861
3862/*
Jakub Narebski5adf3172007-05-26 00:37:40 +02003863 * Given a name and sha1 pair, if the index tells us the file in
Junio C Hamano6973dca2006-04-21 23:57:45 -07003864 * the work tree has that object contents, return true, so that
3865 * prepare_temp_file() does not have to inflate and extract.
3866 */
Nguyễn Thái Ngọc Duyb78ea5f2018-09-21 17:57:19 +02003867static int reuse_worktree_file(struct index_state *istate,
3868 const char *name,
3869 const struct object_id *oid,
3870 int want_file)
Junio C Hamano6973dca2006-04-21 23:57:45 -07003871{
Nguyễn Thái Ngọc Duy9c5e6c82013-07-09 22:29:00 +07003872 const struct cache_entry *ce;
Junio C Hamano6973dca2006-04-21 23:57:45 -07003873 struct stat st;
3874 int pos, len;
3875
Junio C Hamano150115a2009-03-22 15:26:07 -07003876 /*
3877 * We do not read the cache ourselves here, because the
Junio C Hamano6973dca2006-04-21 23:57:45 -07003878 * benchmark with my previous version that always reads cache
3879 * shows that it makes things worse for diff-tree comparing
3880 * two linux-2.6 kernel trees in an already checked out work
3881 * tree. This is because most diff-tree comparisons deal with
3882 * only a small number of files, while reading the cache is
3883 * expensive for a large project, and its cost outweighs the
3884 * savings we get by not inflating the object to a temporary
3885 * file. Practically, this code only helps when we are used
3886 * by diff-cache --cached, which does read the cache before
3887 * calling us.
3888 */
Nguyễn Thái Ngọc Duyb78ea5f2018-09-21 17:57:19 +02003889 if (!istate->cache)
Junio C Hamano6973dca2006-04-21 23:57:45 -07003890 return 0;
3891
Shawn O. Pearce1510fea2006-12-14 06:15:57 -05003892 /* We want to avoid the working directory if our caller
3893 * doesn't need the data in a normal file, this system
3894 * is rather slow with its stat/open/mmap/close syscalls,
3895 * and the object is contained in a pack file. The pack
3896 * is probably already open and will be faster to obtain
3897 * the data through than the working directory. Loose
3898 * objects however would tend to be slower as they need
3899 * to be individually opened and inflated.
3900 */
brian m. carlson14c3c802018-05-02 00:25:33 +00003901 if (!FAST_WORKING_DIRECTORY && !want_file && has_object_pack(oid))
Shawn O. Pearce1510fea2006-12-14 06:15:57 -05003902 return 0;
3903
Jeff King06dec432016-07-22 11:27:53 -04003904 /*
3905 * Similarly, if we'd have to convert the file contents anyway, that
3906 * makes the optimization not worthwhile.
3907 */
Nguyễn Thái Ngọc Duyb78ea5f2018-09-21 17:57:19 +02003908 if (!want_file && would_convert_to_git(istate, name))
Jeff King06dec432016-07-22 11:27:53 -04003909 return 0;
3910
Derrick Stoleead90da72021-09-08 11:23:56 +00003911 /*
3912 * If this path does not match our sparse-checkout definition,
3913 * then the file will not be in the working directory.
3914 */
3915 if (!path_in_sparse_checkout(name, istate))
3916 return 0;
3917
Junio C Hamano6973dca2006-04-21 23:57:45 -07003918 len = strlen(name);
Nguyễn Thái Ngọc Duyb78ea5f2018-09-21 17:57:19 +02003919 pos = index_name_pos(istate, name, len);
Junio C Hamano6973dca2006-04-21 23:57:45 -07003920 if (pos < 0)
3921 return 0;
Nguyễn Thái Ngọc Duyb78ea5f2018-09-21 17:57:19 +02003922 ce = istate->cache[pos];
Junio C Hamanoeadb5832008-01-18 23:45:24 -08003923
3924 /*
3925 * This is not the sha1 we are looking for, or
3926 * unreusable because it is not a regular file.
Junio C Hamano6973dca2006-04-21 23:57:45 -07003927 */
Jeff King9001dc22018-08-28 17:22:48 -04003928 if (!oideq(oid, &ce->oid) || !S_ISREG(ce->ce_mode))
Junio C Hamanoeadb5832008-01-18 23:45:24 -08003929 return 0;
3930
3931 /*
Junio C Hamano150115a2009-03-22 15:26:07 -07003932 * If ce is marked as "assume unchanged", there is no
3933 * guarantee that work tree matches what we are looking for.
3934 */
Nguyễn Thái Ngọc Duyb4d16902009-08-20 20:46:58 +07003935 if ((ce->ce_flags & CE_VALID) || ce_skip_worktree(ce))
Junio C Hamano150115a2009-03-22 15:26:07 -07003936 return 0;
3937
3938 /*
Junio C Hamanoeadb5832008-01-18 23:45:24 -08003939 * If ce matches the file in the work tree, we can reuse it.
3940 */
3941 if (ce_uptodate(ce) ||
Nguyễn Thái Ngọc Duyb78ea5f2018-09-21 17:57:19 +02003942 (!lstat(name, &st) && !ie_match_stat(istate, ce, &st, 0)))
Junio C Hamanoeadb5832008-01-18 23:45:24 -08003943 return 1;
3944
3945 return 0;
Junio C Hamano6973dca2006-04-21 23:57:45 -07003946}
3947
Linus Torvalds04786752007-04-15 11:14:28 -07003948static int diff_populate_gitlink(struct diff_filespec *s, int size_only)
3949{
Jeff Kingb1ddfb92016-02-22 17:45:08 -05003950 struct strbuf buf = STRBUF_INIT;
3951 char *dirty = "";
Junio C Hamano8e08b412010-01-16 18:42:53 +01003952
3953 /* Are we looking at the work tree? */
Jens Lehmann85adbf22010-03-12 22:23:52 +01003954 if (s->dirty_submodule)
Junio C Hamano8e08b412010-01-16 18:42:53 +01003955 dirty = "-dirty";
3956
brian m. carlsona0d12c42016-06-24 23:09:23 +00003957 strbuf_addf(&buf, "Subproject commit %s%s\n",
3958 oid_to_hex(&s->oid), dirty);
Jeff Kingb1ddfb92016-02-22 17:45:08 -05003959 s->size = buf.len;
Linus Torvalds04786752007-04-15 11:14:28 -07003960 if (size_only) {
3961 s->data = NULL;
Jeff Kingb1ddfb92016-02-22 17:45:08 -05003962 strbuf_release(&buf);
3963 } else {
3964 s->data = strbuf_detach(&buf, NULL);
3965 s->should_free = 1;
Linus Torvalds04786752007-04-15 11:14:28 -07003966 }
3967 return 0;
3968}
3969
Junio C Hamano6973dca2006-04-21 23:57:45 -07003970/*
3971 * While doing rename detection and pickaxe operation, we may need to
3972 * grab the data for the blob (or file) for our own in-core comparison.
3973 * diff_filespec has data and size fields for this purpose.
3974 */
Nguyễn Thái Ngọc Duyb78ea5f2018-09-21 17:57:19 +02003975int diff_populate_filespec(struct repository *r,
3976 struct diff_filespec *s,
Jonathan Tan1c37e862020-04-07 15:11:41 -07003977 const struct diff_populate_filespec_options *options)
Junio C Hamano6973dca2006-04-21 23:57:45 -07003978{
Jonathan Tan1c37e862020-04-07 15:11:41 -07003979 int size_only = options ? options->check_size_only : 0;
3980 int check_binary = options ? options->check_binary : 0;
Junio C Hamano6973dca2006-04-21 23:57:45 -07003981 int err = 0;
Torsten Bögershausen8462ff42018-01-13 23:49:31 +01003982 int conv_flags = global_conv_flags_eol;
Junio C Hamano5430bb22013-06-24 14:35:04 -07003983 /*
3984 * demote FAIL to WARN to allow inspecting the situation
3985 * instead of refusing.
3986 */
Torsten Bögershausen8462ff42018-01-13 23:49:31 +01003987 if (conv_flags & CONV_EOL_RNDTRP_DIE)
3988 conv_flags = CONV_EOL_RNDTRP_WARN;
Junio C Hamano5430bb22013-06-24 14:35:04 -07003989
Junio C Hamano6973dca2006-04-21 23:57:45 -07003990 if (!DIFF_FILE_VALID(s))
3991 die("internal error: asking to populate invalid file.");
3992 if (S_ISDIR(s->mode))
3993 return -1;
3994
Junio C Hamano6973dca2006-04-21 23:57:45 -07003995 if (s->data)
Junio C Hamanofc3abdf2007-05-03 13:05:48 -07003996 return 0;
Linus Torvalds04786752007-04-15 11:14:28 -07003997
Junio C Hamano6e0b8ed2007-05-07 01:14:21 -07003998 if (size_only && 0 < s->size)
3999 return 0;
4000
Martin Waitz302b9282007-05-21 22:08:28 +02004001 if (S_ISGITLINK(s->mode))
Linus Torvalds04786752007-04-15 11:14:28 -07004002 return diff_populate_gitlink(s, size_only);
4003
brian m. carlson41c95602016-06-24 23:09:24 +00004004 if (!s->oid_valid ||
Nguyễn Thái Ngọc Duyb78ea5f2018-09-21 17:57:19 +02004005 reuse_worktree_file(r->index, s->path, &s->oid, 0)) {
Brandon Caseyf285a2d2008-10-09 14:12:12 -05004006 struct strbuf buf = STRBUF_INIT;
Junio C Hamano6973dca2006-04-21 23:57:45 -07004007 struct stat st;
4008 int fd;
Linus Torvalds6c510be2007-02-13 11:07:23 -08004009
Junio C Hamano6973dca2006-04-21 23:57:45 -07004010 if (lstat(s->path, &st) < 0) {
Andrey Okoshkin10e0ca82017-10-27 12:33:25 +03004011 err_empty:
4012 err = -1;
4013 empty:
4014 s->data = (char *)"";
4015 s->size = 0;
4016 return err;
Junio C Hamano6973dca2006-04-21 23:57:45 -07004017 }
Shawn O. Pearcedc49cd72007-03-06 20:44:37 -05004018 s->size = xsize_t(st.st_size);
Junio C Hamano6973dca2006-04-21 23:57:45 -07004019 if (!s->size)
4020 goto empty;
Junio C Hamano6973dca2006-04-21 23:57:45 -07004021 if (S_ISLNK(st.st_mode)) {
Linus Torvaldscf219d82008-12-17 10:26:13 -08004022 struct strbuf sb = STRBUF_INIT;
4023
4024 if (strbuf_readlink(&sb, s->path, s->size))
Junio C Hamano6973dca2006-04-21 23:57:45 -07004025 goto err_empty;
René Scharfe0956a6d2008-12-18 17:56:51 +01004026 s->size = sb.len;
4027 s->data = strbuf_detach(&sb, NULL);
Linus Torvaldscf219d82008-12-17 10:26:13 -08004028 s->should_free = 1;
Junio C Hamano6973dca2006-04-21 23:57:45 -07004029 return 0;
4030 }
Junio C Hamano12426e12017-03-01 18:04:44 +01004031
4032 /*
4033 * Even if the caller would be happy with getting
4034 * only the size, we cannot return early at this
4035 * point if the path requires us to run the content
4036 * conversion.
4037 */
Nguyễn Thái Ngọc Duyb78ea5f2018-09-21 17:57:19 +02004038 if (size_only && !would_convert_to_git(r->index, s->path))
Linus Torvaldscf219d82008-12-17 10:26:13 -08004039 return 0;
Junio C Hamano12426e12017-03-01 18:04:44 +01004040
4041 /*
4042 * Note: this check uses xsize_t(st.st_size) that may
4043 * not be the true size of the blob after it goes
4044 * through convert_to_git(). This may not strictly be
4045 * correct, but the whole point of big_file_threshold
4046 * and is_binary check being that we want to avoid
4047 * opening the file and inspecting the contents, this
4048 * is probably fine.
4049 */
Jonathan Tan1c37e862020-04-07 15:11:41 -07004050 if (check_binary &&
Nguyễn Thái Ngọc Duy6bf3b812014-08-16 10:08:05 +07004051 s->size > big_file_threshold && s->is_binary == -1) {
4052 s->is_binary = 1;
4053 return 0;
4054 }
Junio C Hamano6973dca2006-04-21 23:57:45 -07004055 fd = open(s->path, O_RDONLY);
4056 if (fd < 0)
4057 goto err_empty;
Shawn O. Pearcec4712e42006-12-24 00:47:23 -05004058 s->data = xmmap(NULL, s->size, PROT_READ, MAP_PRIVATE, fd, 0);
Junio C Hamano6973dca2006-04-21 23:57:45 -07004059 close(fd);
Junio C Hamano6973dca2006-04-21 23:57:45 -07004060 s->should_munmap = 1;
Linus Torvalds6c510be2007-02-13 11:07:23 -08004061
4062 /*
4063 * Convert from working tree format to canonical git format
4064 */
Nguyễn Thái Ngọc Duyb78ea5f2018-09-21 17:57:19 +02004065 if (convert_to_git(r->index, s->path, s->data, s->size, &buf, conv_flags)) {
René Scharfec32f7492007-10-21 11:23:49 +02004066 size_t size = 0;
Linus Torvalds6c510be2007-02-13 11:07:23 -08004067 munmap(s->data, s->size);
4068 s->should_munmap = 0;
René Scharfec32f7492007-10-21 11:23:49 +02004069 s->data = strbuf_detach(&buf, &size);
4070 s->size = size;
Linus Torvalds6c510be2007-02-13 11:07:23 -08004071 s->should_free = 1;
4072 }
Junio C Hamano6973dca2006-04-21 23:57:45 -07004073 }
4074 else {
Jonathan Tanc14b6f82020-04-07 15:11:42 -07004075 struct object_info info = {
4076 .sizep = &s->size
4077 };
4078
4079 if (!(size_only || check_binary))
4080 /*
4081 * Set contentp, since there is no chance that merely
4082 * the size is sufficient.
4083 */
4084 info.contentp = &s->data;
4085
Jonathan Tan95acf112020-04-07 15:11:43 -07004086 if (options && options->missing_object_cb) {
4087 if (!oid_object_info_extended(r, &s->oid, &info,
4088 OBJECT_INFO_LOOKUP_REPLACE |
4089 OBJECT_INFO_SKIP_FETCH_OBJECT))
4090 goto object_read;
4091 options->missing_object_cb(options->missing_object_data);
4092 }
Jonathan Tanc14b6f82020-04-07 15:11:42 -07004093 if (oid_object_info_extended(r, &s->oid, &info,
4094 OBJECT_INFO_LOOKUP_REPLACE))
4095 die("unable to read %s", oid_to_hex(&s->oid));
4096
Jonathan Tan95acf112020-04-07 15:11:43 -07004097object_read:
Jonathan Tan1c37e862020-04-07 15:11:41 -07004098 if (size_only || check_binary) {
Nguyễn Thái Ngọc Duy6bf3b812014-08-16 10:08:05 +07004099 if (size_only)
4100 return 0;
4101 if (s->size > big_file_threshold && s->is_binary == -1) {
4102 s->is_binary = 1;
4103 return 0;
4104 }
Junio C Hamano6973dca2006-04-21 23:57:45 -07004105 }
Jonathan Tanc14b6f82020-04-07 15:11:42 -07004106 if (!info.contentp) {
4107 info.contentp = &s->data;
4108 if (oid_object_info_extended(r, &s->oid, &info,
4109 OBJECT_INFO_LOOKUP_REPLACE))
4110 die("unable to read %s", oid_to_hex(&s->oid));
4111 }
Nguyễn Thái Ngọc Duy6bf3b812014-08-16 10:08:05 +07004112 s->should_free = 1;
Junio C Hamano6973dca2006-04-21 23:57:45 -07004113 }
4114 return 0;
4115}
4116
Junio C Hamano8ae92e62007-10-02 21:01:03 -07004117void diff_free_filespec_blob(struct diff_filespec *s)
Junio C Hamano6973dca2006-04-21 23:57:45 -07004118{
4119 if (s->should_free)
4120 free(s->data);
4121 else if (s->should_munmap)
4122 munmap(s->data, s->size);
Junio C Hamanofc3abdf2007-05-03 13:05:48 -07004123
4124 if (s->should_free || s->should_munmap) {
4125 s->should_free = s->should_munmap = 0;
4126 s->data = NULL;
4127 }
Jeff Kingeede7b72007-09-25 15:29:42 -04004128}
4129
4130void diff_free_filespec_data(struct diff_filespec *s)
4131{
Jinoh Kang24695932020-11-06 17:14:52 +00004132 if (!s)
4133 return;
4134
Junio C Hamano8ae92e62007-10-02 21:01:03 -07004135 diff_free_filespec_blob(s);
Ævar Arnfjörð Bjarmason6a83d902017-06-15 23:15:46 +00004136 FREE_AND_NULL(s->cnt_data);
Junio C Hamano6973dca2006-04-21 23:57:45 -07004137}
4138
Nguyễn Thái Ngọc Duyb78ea5f2018-09-21 17:57:19 +02004139static void prep_temp_blob(struct index_state *istate,
4140 const char *path, struct diff_tempfile *temp,
Junio C Hamano6973dca2006-04-21 23:57:45 -07004141 void *blob,
4142 unsigned long size,
brian m. carlson09bdff22016-06-24 23:09:29 +00004143 const struct object_id *oid,
Junio C Hamano6973dca2006-04-21 23:57:45 -07004144 int mode)
4145{
Johannes Schindelin4e218f52009-03-21 12:42:52 +01004146 struct strbuf buf = STRBUF_INIT;
Brandon Williamsc2a46a72018-02-14 10:59:54 -08004147 struct strbuf tempfile = STRBUF_INIT;
David Aguilar003b33a2009-05-31 01:35:52 -07004148 char *path_dup = xstrdup(path);
4149 const char *base = basename(path_dup);
brian m. carlsonc397aac2020-03-16 18:05:03 +00004150 struct checkout_metadata meta;
4151
4152 init_checkout_metadata(&meta, NULL, NULL, oid);
Junio C Hamano6973dca2006-04-21 23:57:45 -07004153
David Aguilar003b33a2009-05-31 01:35:52 -07004154 /* Generate "XXXXXX_basename.ext" */
Brandon Williamsc2a46a72018-02-14 10:59:54 -08004155 strbuf_addstr(&tempfile, "XXXXXX_");
4156 strbuf_addstr(&tempfile, base);
David Aguilar003b33a2009-05-31 01:35:52 -07004157
Brandon Williamsc2a46a72018-02-14 10:59:54 -08004158 temp->tempfile = mks_tempfile_ts(tempfile.buf, strlen(base) + 1);
Jeff King076aa2c2017-09-05 08:15:08 -04004159 if (!temp->tempfile)
Thomas Rastd824cbb2009-06-27 17:58:46 +02004160 die_errno("unable to create temp-file");
Nguyễn Thái Ngọc Duyb78ea5f2018-09-21 17:57:19 +02004161 if (convert_to_working_tree(istate, path,
brian m. carlsonc397aac2020-03-16 18:05:03 +00004162 (const char *)blob, (size_t)size, &buf, &meta)) {
Johannes Schindelin4e218f52009-03-21 12:42:52 +01004163 blob = buf.buf;
4164 size = buf.len;
4165 }
Junio C Hamanoc50424a2017-09-25 15:24:06 +09004166 if (write_in_full(temp->tempfile->fd, blob, size) < 0 ||
Jeff King076aa2c2017-09-05 08:15:08 -04004167 close_tempfile_gently(temp->tempfile))
Thomas Rast0721c312009-06-27 17:58:47 +02004168 die_errno("unable to write temp-file");
Jeff King076aa2c2017-09-05 08:15:08 -04004169 temp->name = get_tempfile_path(temp->tempfile);
brian m. carlson09bdff22016-06-24 23:09:29 +00004170 oid_to_hex_r(temp->hex, oid);
Jeff King5096d492015-09-24 17:06:08 -04004171 xsnprintf(temp->mode, sizeof(temp->mode), "%06o", mode);
Johannes Schindelin4e218f52009-03-21 12:42:52 +01004172 strbuf_release(&buf);
Brandon Williamsc2a46a72018-02-14 10:59:54 -08004173 strbuf_release(&tempfile);
David Aguilar003b33a2009-05-31 01:35:52 -07004174 free(path_dup);
Junio C Hamano6973dca2006-04-21 23:57:45 -07004175}
4176
Nguyễn Thái Ngọc Duyb78ea5f2018-09-21 17:57:19 +02004177static struct diff_tempfile *prepare_temp_file(struct repository *r,
4178 const char *name,
4179 struct diff_filespec *one)
Junio C Hamano6973dca2006-04-21 23:57:45 -07004180{
Jeff King479b0ae2009-01-22 00:59:56 -05004181 struct diff_tempfile *temp = claim_diff_tempfile();
4182
Junio C Hamano6973dca2006-04-21 23:57:45 -07004183 if (!DIFF_FILE_VALID(one)) {
4184 not_a_valid_file:
4185 /* A '-' entry produces this for file-2, and
4186 * a '+' entry produces this for file-1.
4187 */
4188 temp->name = "/dev/null";
Jeff King5096d492015-09-24 17:06:08 -04004189 xsnprintf(temp->hex, sizeof(temp->hex), ".");
4190 xsnprintf(temp->mode, sizeof(temp->mode), ".");
Jeff King479b0ae2009-01-22 00:59:56 -05004191 return temp;
4192 }
4193
Thomas Rastaba47272014-02-16 17:52:34 +01004194 if (!S_ISGITLINK(one->mode) &&
brian m. carlson41c95602016-06-24 23:09:24 +00004195 (!one->oid_valid ||
Nguyễn Thái Ngọc Duyb78ea5f2018-09-21 17:57:19 +02004196 reuse_worktree_file(r->index, name, &one->oid, 1))) {
Junio C Hamano6973dca2006-04-21 23:57:45 -07004197 struct stat st;
4198 if (lstat(name, &st) < 0) {
4199 if (errno == ENOENT)
4200 goto not_a_valid_file;
Thomas Rastd824cbb2009-06-27 17:58:46 +02004201 die_errno("stat(%s)", name);
Junio C Hamano6973dca2006-04-21 23:57:45 -07004202 }
4203 if (S_ISLNK(st.st_mode)) {
Jeff King3cd73882009-05-25 06:46:09 -04004204 struct strbuf sb = STRBUF_INIT;
4205 if (strbuf_readlink(&sb, name, st.st_size) < 0)
Thomas Rast0721c312009-06-27 17:58:47 +02004206 die_errno("readlink(%s)", name);
Nguyễn Thái Ngọc Duyb78ea5f2018-09-21 17:57:19 +02004207 prep_temp_blob(r->index, name, temp, sb.buf, sb.len,
brian m. carlson41c95602016-06-24 23:09:24 +00004208 (one->oid_valid ?
brian m. carlson14228442021-04-26 01:02:56 +00004209 &one->oid : null_oid()),
brian m. carlson41c95602016-06-24 23:09:24 +00004210 (one->oid_valid ?
Junio C Hamano6973dca2006-04-21 23:57:45 -07004211 one->mode : S_IFLNK));
Jeff King3cd73882009-05-25 06:46:09 -04004212 strbuf_release(&sb);
Junio C Hamano6973dca2006-04-21 23:57:45 -07004213 }
4214 else {
4215 /* we can borrow from the file in the work tree */
4216 temp->name = name;
brian m. carlson41c95602016-06-24 23:09:24 +00004217 if (!one->oid_valid)
brian m. carlson14228442021-04-26 01:02:56 +00004218 oid_to_hex_r(temp->hex, null_oid());
Junio C Hamano6973dca2006-04-21 23:57:45 -07004219 else
René Scharfe24905742017-01-28 23:03:03 +01004220 oid_to_hex_r(temp->hex, &one->oid);
Junio C Hamano6973dca2006-04-21 23:57:45 -07004221 /* Even though we may sometimes borrow the
4222 * contents from the work tree, we always want
4223 * one->mode. mode is trustworthy even when
Brandon Williams74014152017-05-30 10:30:52 -07004224 * !(one->oid_valid), as long as
Junio C Hamano6973dca2006-04-21 23:57:45 -07004225 * DIFF_FILE_VALID(one).
4226 */
Jeff King5096d492015-09-24 17:06:08 -04004227 xsnprintf(temp->mode, sizeof(temp->mode), "%06o", one->mode);
Junio C Hamano6973dca2006-04-21 23:57:45 -07004228 }
Jeff King479b0ae2009-01-22 00:59:56 -05004229 return temp;
Junio C Hamano6973dca2006-04-21 23:57:45 -07004230 }
4231 else {
Jonathan Tan1c37e862020-04-07 15:11:41 -07004232 if (diff_populate_filespec(r, one, NULL))
Junio C Hamano6973dca2006-04-21 23:57:45 -07004233 die("cannot read data blob for %s", one->path);
Nguyễn Thái Ngọc Duyb78ea5f2018-09-21 17:57:19 +02004234 prep_temp_blob(r->index, name, temp,
4235 one->data, one->size,
brian m. carlson09bdff22016-06-24 23:09:29 +00004236 &one->oid, one->mode);
Junio C Hamano6973dca2006-04-21 23:57:45 -07004237 }
Jeff King479b0ae2009-01-22 00:59:56 -05004238 return temp;
Junio C Hamano6973dca2006-04-21 23:57:45 -07004239}
4240
Nguyễn Thái Ngọc Duyb78ea5f2018-09-21 17:57:19 +02004241static void add_external_diff_name(struct repository *r,
Jeff Kingef8d7ac2020-07-28 16:24:53 -04004242 struct strvec *argv,
Jeff Kingf3efe782014-04-19 15:22:25 -04004243 const char *name,
4244 struct diff_filespec *df)
4245{
Nguyễn Thái Ngọc Duyb78ea5f2018-09-21 17:57:19 +02004246 struct diff_tempfile *temp = prepare_temp_file(r, name, df);
Jeff Kingef8d7ac2020-07-28 16:24:53 -04004247 strvec_push(argv, temp->name);
4248 strvec_push(argv, temp->hex);
4249 strvec_push(argv, temp->mode);
Jeff Kingf3efe782014-04-19 15:22:25 -04004250}
4251
Junio C Hamano6973dca2006-04-21 23:57:45 -07004252/* An external diff command takes:
4253 *
4254 * diff-cmd name infile1 infile1-sha1 infile1-mode \
4255 * infile2 infile2-sha1 infile2-mode [ rename-to ]
4256 *
4257 */
4258static void run_external_diff(const char *pgm,
4259 const char *name,
4260 const char *other,
4261 struct diff_filespec *one,
4262 struct diff_filespec *two,
4263 const char *xfrm_msg,
Zoltan Klingeree7fb0b2013-12-06 10:38:46 +11004264 struct diff_options *o)
Junio C Hamano6973dca2006-04-21 23:57:45 -07004265{
Jeff Kingef8d7ac2020-07-28 16:24:53 -04004266 struct strvec argv = STRVEC_INIT;
4267 struct strvec env = STRVEC_INIT;
Zoltan Klingeree7fb0b2013-12-06 10:38:46 +11004268 struct diff_queue_struct *q = &diff_queued_diff;
Junio C Hamano6973dca2006-04-21 23:57:45 -07004269
Jeff Kingef8d7ac2020-07-28 16:24:53 -04004270 strvec_push(&argv, pgm);
4271 strvec_push(&argv, name);
Junio C Hamano6973dca2006-04-21 23:57:45 -07004272
Junio C Hamano6973dca2006-04-21 23:57:45 -07004273 if (one && two) {
Nguyễn Thái Ngọc Duyb78ea5f2018-09-21 17:57:19 +02004274 add_external_diff_name(o->repo, &argv, name, one);
Jeff Kingf3efe782014-04-19 15:22:25 -04004275 if (!other)
Nguyễn Thái Ngọc Duyb78ea5f2018-09-21 17:57:19 +02004276 add_external_diff_name(o->repo, &argv, name, two);
Jeff Kingf3efe782014-04-19 15:22:25 -04004277 else {
Nguyễn Thái Ngọc Duyb78ea5f2018-09-21 17:57:19 +02004278 add_external_diff_name(o->repo, &argv, other, two);
Jeff Kingef8d7ac2020-07-28 16:24:53 -04004279 strvec_push(&argv, other);
4280 strvec_push(&argv, xfrm_msg);
Junio C Hamano6973dca2006-04-21 23:57:45 -07004281 }
Junio C Hamano6973dca2006-04-21 23:57:45 -07004282 }
Zoltan Klingeree7fb0b2013-12-06 10:38:46 +11004283
Jeff Kingef8d7ac2020-07-28 16:24:53 -04004284 strvec_pushf(&env, "GIT_DIFF_PATH_COUNTER=%d", ++o->diff_path_counter);
4285 strvec_pushf(&env, "GIT_DIFF_PATH_TOTAL=%d", q->nr);
Zoltan Klingeree7fb0b2013-12-06 10:38:46 +11004286
Johannes Schindelin3aef54e2019-07-11 01:23:41 -07004287 diff_free_filespec_data(one);
4288 diff_free_filespec_data(two);
Jeff Kingd70a9eb2020-07-28 20:37:20 -04004289 if (run_command_v_opt_cd_env(argv.v, RUN_USING_SHELL, NULL, env.v))
Jeff King89294d12014-04-19 15:19:19 -04004290 die(_("external diff died, stopping at %s"), name);
4291
Junio C Hamano6973dca2006-04-21 23:57:45 -07004292 remove_tempfile();
Jeff Kingef8d7ac2020-07-28 16:24:53 -04004293 strvec_clear(&argv);
4294 strvec_clear(&env);
Junio C Hamano6973dca2006-04-21 23:57:45 -07004295}
4296
Junio C Hamanob67b9612009-01-26 00:08:24 -08004297static int similarity_index(struct diff_filepair *p)
4298{
4299 return p->score * 100 / MAX_SCORE;
4300}
4301
Jeff King4f036662016-10-20 02:21:25 -04004302static const char *diff_abbrev_oid(const struct object_id *oid, int abbrev)
4303{
4304 if (startup_info->have_repository)
brian m. carlsonaab95832018-03-12 02:27:30 +00004305 return find_unique_abbrev(oid, abbrev);
Jeff King4f036662016-10-20 02:21:25 -04004306 else {
4307 char *hex = oid_to_hex(oid);
Junio C Hamano0d9c5272016-10-27 14:58:48 -07004308 if (abbrev < 0)
4309 abbrev = FALLBACK_DEFAULT_ABBREV;
brian m. carlson02afca12018-07-16 01:28:05 +00004310 if (abbrev > the_hash_algo->hexsz)
Johannes Schindelin033abf92018-05-02 11:38:39 +02004311 BUG("oid abbreviation out of range: %d", abbrev);
Jack Bates43d19482016-12-06 09:56:14 -07004312 if (abbrev)
4313 hex[abbrev] = '\0';
Jeff King4f036662016-10-20 02:21:25 -04004314 return hex;
4315 }
4316}
4317
Junio C Hamanob67b9612009-01-26 00:08:24 -08004318static void fill_metainfo(struct strbuf *msg,
4319 const char *name,
4320 const char *other,
4321 struct diff_filespec *one,
4322 struct diff_filespec *two,
4323 struct diff_options *o,
Bert Wesarg37466442010-05-04 00:38:07 +02004324 struct diff_filepair *p,
Junio C Hamano59777442010-06-18 11:16:57 -07004325 int *must_show_header,
Bert Wesarg37466442010-05-04 00:38:07 +02004326 int use_color)
Junio C Hamanob67b9612009-01-26 00:08:24 -08004327{
Bert Wesarg37466442010-05-04 00:38:07 +02004328 const char *set = diff_get_color(use_color, DIFF_METAINFO);
4329 const char *reset = diff_get_color(use_color, DIFF_RESET);
John Keeping30997bb2013-02-07 20:15:27 +00004330 const char *line_prefix = diff_line_prefix(o);
Bo Yang7be57612010-05-26 15:23:54 +08004331
Christian Couder296c6bb2010-05-26 04:50:12 +02004332 *must_show_header = 1;
Junio C Hamanob67b9612009-01-26 00:08:24 -08004333 strbuf_init(msg, PATH_MAX * 2 + 300);
4334 switch (p->status) {
4335 case DIFF_STATUS_COPIED:
Junio C Hamano98ad90f2010-06-18 11:16:57 -07004336 strbuf_addf(msg, "%s%ssimilarity index %d%%",
4337 line_prefix, set, similarity_index(p));
4338 strbuf_addf(msg, "%s\n%s%scopy from ",
4339 reset, line_prefix, set);
Junio C Hamanob67b9612009-01-26 00:08:24 -08004340 quote_c_style(name, msg, NULL, 0);
Junio C Hamano98ad90f2010-06-18 11:16:57 -07004341 strbuf_addf(msg, "%s\n%s%scopy to ", reset, line_prefix, set);
Junio C Hamanob67b9612009-01-26 00:08:24 -08004342 quote_c_style(other, msg, NULL, 0);
Bert Wesarg37466442010-05-04 00:38:07 +02004343 strbuf_addf(msg, "%s\n", reset);
Junio C Hamanob67b9612009-01-26 00:08:24 -08004344 break;
4345 case DIFF_STATUS_RENAMED:
Junio C Hamano98ad90f2010-06-18 11:16:57 -07004346 strbuf_addf(msg, "%s%ssimilarity index %d%%",
4347 line_prefix, set, similarity_index(p));
4348 strbuf_addf(msg, "%s\n%s%srename from ",
4349 reset, line_prefix, set);
Junio C Hamanob67b9612009-01-26 00:08:24 -08004350 quote_c_style(name, msg, NULL, 0);
Junio C Hamano98ad90f2010-06-18 11:16:57 -07004351 strbuf_addf(msg, "%s\n%s%srename to ",
4352 reset, line_prefix, set);
Junio C Hamanob67b9612009-01-26 00:08:24 -08004353 quote_c_style(other, msg, NULL, 0);
Bert Wesarg37466442010-05-04 00:38:07 +02004354 strbuf_addf(msg, "%s\n", reset);
Junio C Hamanob67b9612009-01-26 00:08:24 -08004355 break;
4356 case DIFF_STATUS_MODIFIED:
4357 if (p->score) {
Junio C Hamano98ad90f2010-06-18 11:16:57 -07004358 strbuf_addf(msg, "%s%sdissimilarity index %d%%%s\n",
4359 line_prefix,
Bert Wesarg37466442010-05-04 00:38:07 +02004360 set, similarity_index(p), reset);
Junio C Hamanob67b9612009-01-26 00:08:24 -08004361 break;
4362 }
4363 /* fallthru */
4364 default:
Christian Couder296c6bb2010-05-26 04:50:12 +02004365 *must_show_header = 0;
Junio C Hamanob67b9612009-01-26 00:08:24 -08004366 }
Jeff King9001dc22018-08-28 17:22:48 -04004367 if (one && two && !oideq(&one->oid, &two->oid)) {
brian m. carlsonde1d81d2018-05-02 00:25:52 +00004368 const unsigned hexsz = the_hash_algo->hexsz;
Đoàn Trần Công Danh3046c7f2020-08-21 18:51:47 +07004369 int abbrev = o->abbrev ? o->abbrev : DEFAULT_ABBREV;
4370
4371 if (o->flags.full_index)
4372 abbrev = hexsz;
Junio C Hamanob67b9612009-01-26 00:08:24 -08004373
Brandon Williams0d1e0e72017-10-31 11:19:11 -07004374 if (o->flags.binary) {
Junio C Hamanob67b9612009-01-26 00:08:24 -08004375 mmfile_t mf;
Nguyễn Thái Ngọc Duyb78ea5f2018-09-21 17:57:19 +02004376 if ((!fill_mmfile(o->repo, &mf, one) &&
4377 diff_filespec_is_binary(o->repo, one)) ||
4378 (!fill_mmfile(o->repo, &mf, two) &&
4379 diff_filespec_is_binary(o->repo, two)))
brian m. carlsonde1d81d2018-05-02 00:25:52 +00004380 abbrev = hexsz;
Junio C Hamanob67b9612009-01-26 00:08:24 -08004381 }
Jeff King4f036662016-10-20 02:21:25 -04004382 strbuf_addf(msg, "%s%sindex %s..%s", line_prefix, set,
4383 diff_abbrev_oid(&one->oid, abbrev),
4384 diff_abbrev_oid(&two->oid, abbrev));
Junio C Hamanob67b9612009-01-26 00:08:24 -08004385 if (one->mode == two->mode)
4386 strbuf_addf(msg, " %06o", one->mode);
Bert Wesarg37466442010-05-04 00:38:07 +02004387 strbuf_addf(msg, "%s\n", reset);
Junio C Hamanob67b9612009-01-26 00:08:24 -08004388 }
Junio C Hamanob67b9612009-01-26 00:08:24 -08004389}
4390
Junio C Hamano6973dca2006-04-21 23:57:45 -07004391static void run_diff_cmd(const char *pgm,
4392 const char *name,
4393 const char *other,
Junio C Hamanocd676a52008-02-12 14:26:02 -08004394 const char *attr_path,
Junio C Hamano6973dca2006-04-21 23:57:45 -07004395 struct diff_filespec *one,
4396 struct diff_filespec *two,
Junio C Hamanob67b9612009-01-26 00:08:24 -08004397 struct strbuf *msg,
Junio C Hamano051308f2006-05-04 16:51:44 -07004398 struct diff_options *o,
Junio C Hamanob67b9612009-01-26 00:08:24 -08004399 struct diff_filepair *p)
Junio C Hamano6973dca2006-04-21 23:57:45 -07004400{
Junio C Hamanob67b9612009-01-26 00:08:24 -08004401 const char *xfrm_msg = NULL;
4402 int complete_rewrite = (p->status == DIFF_STATUS_MODIFIED) && p->score;
Christian Couder296c6bb2010-05-26 04:50:12 +02004403 int must_show_header = 0;
Junio C Hamanob67b9612009-01-26 00:08:24 -08004404
Junio C Hamanobd8c1a92012-07-17 22:08:59 -07004405
Brandon Williams0d1e0e72017-10-31 11:19:11 -07004406 if (o->flags.allow_external) {
Nguyễn Thái Ngọc Duyacd00ea2018-09-21 17:57:33 +02004407 struct userdiff_driver *drv;
4408
4409 drv = userdiff_find_by_path(o->repo->index, attr_path);
Jeff Kingbe58e702008-10-05 17:43:21 -04004410 if (drv && drv->external)
4411 pgm = drv->external;
Junio C Hamanof1af60b2007-04-22 17:52:55 -07004412 }
4413
Bert Wesarg37466442010-05-04 00:38:07 +02004414 if (msg) {
4415 /*
4416 * don't use colors when the header is intended for an
4417 * external diff driver
4418 */
4419 fill_metainfo(msg, name, other, one, two, o, p,
Junio C Hamano59777442010-06-18 11:16:57 -07004420 &must_show_header,
Jeff Kingdaa0c3d2011-08-17 22:04:23 -07004421 want_color(o->use_color) && !pgm);
Bert Wesarg37466442010-05-04 00:38:07 +02004422 xfrm_msg = msg->len ? msg->buf : NULL;
4423 }
4424
Junio C Hamano6973dca2006-04-21 23:57:45 -07004425 if (pgm) {
Jeff King4bc17922019-02-14 00:49:42 -05004426 run_external_diff(pgm, name, other, one, two, xfrm_msg, o);
Junio C Hamano6973dca2006-04-21 23:57:45 -07004427 return;
4428 }
4429 if (one && two)
4430 builtin_diff(name, other ? other : name,
Christian Couder296c6bb2010-05-26 04:50:12 +02004431 one, two, xfrm_msg, must_show_header,
4432 o, complete_rewrite);
Junio C Hamano6973dca2006-04-21 23:57:45 -07004433 else
Daniel Barkalowc0c77732008-03-09 22:43:39 -04004434 fprintf(o->file, "* Unmerged path %s\n", name);
Junio C Hamano6973dca2006-04-21 23:57:45 -07004435}
4436
Nguyễn Thái Ngọc Duy58bf2a42018-09-21 17:57:31 +02004437static void diff_fill_oid_info(struct diff_filespec *one, struct index_state *istate)
Junio C Hamano6973dca2006-04-21 23:57:45 -07004438{
4439 if (DIFF_FILE_VALID(one)) {
brian m. carlson41c95602016-06-24 23:09:24 +00004440 if (!one->oid_valid) {
Junio C Hamano6973dca2006-04-21 23:57:45 -07004441 struct stat st;
Junio C Hamano4682d852012-06-27 20:14:47 -07004442 if (one->is_stdin) {
brian m. carlsona0d12c42016-06-24 23:09:23 +00004443 oidclr(&one->oid);
Johannes Schindelin5332b2a2007-02-25 23:36:10 +01004444 return;
4445 }
Junio C Hamano6973dca2006-04-21 23:57:45 -07004446 if (lstat(one->path, &st) < 0)
Thomas Rast0721c312009-06-27 17:58:47 +02004447 die_errno("stat '%s'", one->path);
Nguyễn Thái Ngọc Duy58bf2a42018-09-21 17:57:31 +02004448 if (index_path(istate, &one->oid, one->path, &st, 0))
Alexander Potashevd7530702009-01-04 21:38:41 +03004449 die("cannot hash %s", one->path);
Junio C Hamano6973dca2006-04-21 23:57:45 -07004450 }
4451 }
4452 else
brian m. carlsona0d12c42016-06-24 23:09:23 +00004453 oidclr(&one->oid);
Junio C Hamano6973dca2006-04-21 23:57:45 -07004454}
4455
Junio C Hamanocd676a52008-02-12 14:26:02 -08004456static void strip_prefix(int prefix_length, const char **namep, const char **otherp)
4457{
4458 /* Strip the prefix but do not molest /dev/null and absolute paths */
Johannes Sixtffd04e92018-10-19 18:58:07 +02004459 if (*namep && !is_absolute_path(*namep)) {
Junio C Hamanocd676a52008-02-12 14:26:02 -08004460 *namep += prefix_length;
Jakub Narebskid8faea92010-08-09 10:50:53 -04004461 if (**namep == '/')
4462 ++*namep;
4463 }
Johannes Sixtffd04e92018-10-19 18:58:07 +02004464 if (*otherp && !is_absolute_path(*otherp)) {
Junio C Hamanocd676a52008-02-12 14:26:02 -08004465 *otherp += prefix_length;
Jakub Narebskid8faea92010-08-09 10:50:53 -04004466 if (**otherp == '/')
4467 ++*otherp;
4468 }
Junio C Hamanocd676a52008-02-12 14:26:02 -08004469}
4470
Junio C Hamano6973dca2006-04-21 23:57:45 -07004471static void run_diff(struct diff_filepair *p, struct diff_options *o)
4472{
4473 const char *pgm = external_diff();
Pierre Habouzit663af342007-09-20 00:42:15 +02004474 struct strbuf msg;
Pierre Habouzit663af342007-09-20 00:42:15 +02004475 struct diff_filespec *one = p->one;
4476 struct diff_filespec *two = p->two;
Junio C Hamano6973dca2006-04-21 23:57:45 -07004477 const char *name;
4478 const char *other;
Junio C Hamanocd676a52008-02-12 14:26:02 -08004479 const char *attr_path;
Pierre Habouzit663af342007-09-20 00:42:15 +02004480
Stefan Bellerf2d2a5d2017-06-29 17:06:46 -07004481 name = one->path;
4482 other = (strcmp(name, two->path) ? two->path : NULL);
Junio C Hamanocd676a52008-02-12 14:26:02 -08004483 attr_path = name;
4484 if (o->prefix_length)
4485 strip_prefix(o->prefix_length, &name, &other);
4486
Brandon Williams0d1e0e72017-10-31 11:19:11 -07004487 if (!o->flags.allow_external)
Junio C Hamanobd8c1a92012-07-17 22:08:59 -07004488 pgm = NULL;
4489
Junio C Hamanocd676a52008-02-12 14:26:02 -08004490 if (DIFF_PAIR_UNMERGED(p)) {
4491 run_diff_cmd(pgm, name, NULL, attr_path,
Junio C Hamanob67b9612009-01-26 00:08:24 -08004492 NULL, NULL, NULL, o, p);
Junio C Hamanocd676a52008-02-12 14:26:02 -08004493 return;
4494 }
4495
Nguyễn Thái Ngọc Duy58bf2a42018-09-21 17:57:31 +02004496 diff_fill_oid_info(one, o->repo->index);
4497 diff_fill_oid_info(two, o->repo->index);
Junio C Hamano6973dca2006-04-21 23:57:45 -07004498
Junio C Hamano6973dca2006-04-21 23:57:45 -07004499 if (!pgm &&
4500 DIFF_FILE_VALID(one) && DIFF_FILE_VALID(two) &&
4501 (S_IFMT & one->mode) != (S_IFMT & two->mode)) {
Junio C Hamanob67b9612009-01-26 00:08:24 -08004502 /*
4503 * a filepair that changes between file and symlink
Junio C Hamano6973dca2006-04-21 23:57:45 -07004504 * needs to be split into deletion and creation.
4505 */
4506 struct diff_filespec *null = alloc_filespec(two->path);
Junio C Hamanocd676a52008-02-12 14:26:02 -08004507 run_diff_cmd(NULL, name, other, attr_path,
Nguyễn Thái Ngọc Duyb78ea5f2018-09-21 17:57:19 +02004508 one, null, &msg,
4509 o, p);
Junio C Hamano6973dca2006-04-21 23:57:45 -07004510 free(null);
Junio C Hamanob67b9612009-01-26 00:08:24 -08004511 strbuf_release(&msg);
4512
Junio C Hamano6973dca2006-04-21 23:57:45 -07004513 null = alloc_filespec(one->path);
Junio C Hamanocd676a52008-02-12 14:26:02 -08004514 run_diff_cmd(NULL, name, other, attr_path,
Junio C Hamanob67b9612009-01-26 00:08:24 -08004515 null, two, &msg, o, p);
Junio C Hamano6973dca2006-04-21 23:57:45 -07004516 free(null);
4517 }
4518 else
Junio C Hamanocd676a52008-02-12 14:26:02 -08004519 run_diff_cmd(pgm, name, other, attr_path,
Junio C Hamanob67b9612009-01-26 00:08:24 -08004520 one, two, &msg, o, p);
Junio C Hamano6973dca2006-04-21 23:57:45 -07004521
Pierre Habouzit663af342007-09-20 00:42:15 +02004522 strbuf_release(&msg);
Junio C Hamano6973dca2006-04-21 23:57:45 -07004523}
4524
4525static void run_diffstat(struct diff_filepair *p, struct diff_options *o,
4526 struct diffstat_t *diffstat)
4527{
4528 const char *name;
4529 const char *other;
4530
4531 if (DIFF_PAIR_UNMERGED(p)) {
4532 /* unmerged */
Nguyễn Thái Ngọc Duyb78ea5f2018-09-21 17:57:19 +02004533 builtin_diffstat(p->one->path, NULL, NULL, NULL,
4534 diffstat, o, p);
Junio C Hamano6973dca2006-04-21 23:57:45 -07004535 return;
4536 }
4537
4538 name = p->one->path;
4539 other = (strcmp(name, p->two->path) ? p->two->path : NULL);
4540
Junio C Hamanocd676a52008-02-12 14:26:02 -08004541 if (o->prefix_length)
4542 strip_prefix(o->prefix_length, &name, &other);
4543
Nguyễn Thái Ngọc Duy58bf2a42018-09-21 17:57:31 +02004544 diff_fill_oid_info(p->one, o->repo->index);
4545 diff_fill_oid_info(p->two, o->repo->index);
Junio C Hamano6973dca2006-04-21 23:57:45 -07004546
Nguyễn Thái Ngọc Duyb78ea5f2018-09-21 17:57:19 +02004547 builtin_diffstat(name, other, p->one, p->two,
4548 diffstat, o, p);
Junio C Hamano6973dca2006-04-21 23:57:45 -07004549}
4550
Johannes Schindelin88246892006-05-20 23:43:13 +02004551static void run_checkdiff(struct diff_filepair *p, struct diff_options *o)
4552{
4553 const char *name;
4554 const char *other;
Junio C Hamanocd676a52008-02-12 14:26:02 -08004555 const char *attr_path;
Johannes Schindelin88246892006-05-20 23:43:13 +02004556
4557 if (DIFF_PAIR_UNMERGED(p)) {
4558 /* unmerged */
4559 return;
4560 }
4561
4562 name = p->one->path;
4563 other = (strcmp(name, p->two->path) ? p->two->path : NULL);
Junio C Hamanocd676a52008-02-12 14:26:02 -08004564 attr_path = other ? other : name;
4565
4566 if (o->prefix_length)
4567 strip_prefix(o->prefix_length, &name, &other);
Johannes Schindelin88246892006-05-20 23:43:13 +02004568
Nguyễn Thái Ngọc Duy58bf2a42018-09-21 17:57:31 +02004569 diff_fill_oid_info(p->one, o->repo->index);
4570 diff_fill_oid_info(p->two, o->repo->index);
Johannes Schindelin88246892006-05-20 23:43:13 +02004571
Junio C Hamanocd676a52008-02-12 14:26:02 -08004572 builtin_checkdiff(name, other, attr_path, p->one, p->two, o);
Johannes Schindelin88246892006-05-20 23:43:13 +02004573}
4574
Nguyễn Thái Ngọc Duy4a288472019-01-27 07:35:31 +07004575static void prep_parse_options(struct diff_options *options);
4576
Nguyễn Thái Ngọc Duye6757652018-09-21 17:57:24 +02004577void repo_diff_setup(struct repository *r, struct diff_options *options)
Junio C Hamano6973dca2006-04-21 23:57:45 -07004578{
Johannes Schindelinbe4f2b42010-08-05 10:49:55 +02004579 memcpy(options, &default_diff_options, sizeof(*options));
Daniel Barkalowc0c77732008-03-09 22:43:39 -04004580
4581 options->file = stdout;
Nguyễn Thái Ngọc Duye6757652018-09-21 17:57:24 +02004582 options->repo = r;
Daniel Barkalowc0c77732008-03-09 22:43:39 -04004583
Stefan Beller7648b792018-08-17 13:43:52 -07004584 options->output_indicators[OUTPUT_INDICATOR_NEW] = '+';
4585 options->output_indicators[OUTPUT_INDICATOR_OLD] = '-';
4586 options->output_indicators[OUTPUT_INDICATOR_CONTEXT] = ' ';
Jack Bates43d19482016-12-06 09:56:14 -07004587 options->abbrev = DEFAULT_ABBREV;
Junio C Hamano6973dca2006-04-21 23:57:45 -07004588 options->line_termination = '\n';
4589 options->break_opt = -1;
4590 options->rename_limit = -1;
Johan Herland712d2c72011-04-29 11:36:20 +02004591 options->dirstat_permille = diff_dirstat_permille_default;
Jeff Muizelaar6468a4e2012-09-27 15:12:52 -04004592 options->context = diff_context_default;
Vegard Nossumc4888672017-01-12 13:21:11 +01004593 options->interhunkcontext = diff_interhunk_context_default;
Junio C Hamanoa17505f2016-10-04 15:26:27 -07004594 options->ws_error_highlight = ws_error_highlight_default;
Brandon Williams0d1e0e72017-10-31 11:19:11 -07004595 options->flags.rename_empty = 1;
Laurent Arnoudc28ded82020-05-22 12:46:18 +02004596 options->flags.relative_name = diff_relative;
Stefan Beller15af58c2018-01-04 14:50:42 -08004597 options->objfind = NULL;
Junio C Hamano6973dca2006-04-21 23:57:45 -07004598
Kirill Smelkov72441af2014-04-07 01:46:26 +04004599 /* pathchange left =NULL by default */
Junio C Hamano6973dca2006-04-21 23:57:45 -07004600 options->change = diff_change;
4601 options->add_remove = diff_addremove;
Jeff Kingf1c96262011-08-17 22:03:12 -07004602 options->use_color = diff_use_color_default;
Eric Wongb68ea122006-07-07 04:01:23 -07004603 options->detect_rename = diff_detect_rename_default;
Michal Privoznik07ab4de2013-01-16 08:51:57 +01004604 options->xdl_opts |= diff_algorithm;
Michael Haggerty433860f2016-09-05 11:44:51 +02004605 if (diff_indent_heuristic)
4606 DIFF_XDL_SET(options, INDENT_HEURISTIC);
Johannes Schindelineab9a402007-12-18 19:32:14 +00004607
Samuel Bronson6d8940b2013-12-18 19:08:12 -05004608 options->orderfile = diff_order_file_cfg;
4609
Sangeeta Jain8ef93122020-11-10 14:09:00 +05304610 if (!options->flags.ignore_submodule_set)
4611 options->flags.ignore_untracked_in_submodules = 1;
4612
Eli Collinsf89504d2010-05-02 19:03:41 -07004613 if (diff_no_prefix) {
4614 options->a_prefix = options->b_prefix = "";
4615 } else if (!diff_mnemonic_prefix) {
Junio C Hamanoa5a818e2008-08-18 20:08:09 -07004616 options->a_prefix = "a/";
4617 options->b_prefix = "b/";
4618 }
Stefan Beller2e2d5ac2017-06-30 13:53:07 -07004619
4620 options->color_moved = diff_color_moved_default;
Stefan Beller626c0b52018-07-18 12:31:56 -07004621 options->color_moved_ws_handling = diff_color_moved_ws_default;
Nguyễn Thái Ngọc Duy4a288472019-01-27 07:35:31 +07004622
4623 prep_parse_options(options);
Junio C Hamano6973dca2006-04-21 23:57:45 -07004624}
4625
Thomas Rast28452652012-08-03 14:16:24 +02004626void diff_setup_done(struct diff_options *options)
Junio C Hamano6973dca2006-04-21 23:57:45 -07004627{
Stefan Beller4d8c51a2018-01-04 14:50:44 -08004628 unsigned check_mask = DIFF_FORMAT_NAME |
4629 DIFF_FORMAT_NAME_STATUS |
4630 DIFF_FORMAT_CHECKDIFF |
4631 DIFF_FORMAT_NO_OUTPUT;
brian m. carlsonde1d81d2018-05-02 00:25:52 +00004632 /*
4633 * This must be signed because we're comparing against a potentially
4634 * negative value.
4635 */
4636 const int hexsz = the_hash_algo->hexsz;
Timo Hirvonend7de00f2006-06-24 20:26:49 +03004637
Junio C Hamano6c374002013-05-10 17:10:11 +02004638 if (options->set_default)
4639 options->set_default(options);
4640
Stefan Beller4d8c51a2018-01-04 14:50:44 -08004641 if (HAS_MULTI_BITS(options->output_format & check_mask))
Jean-Noël AVILAa2f05c92016-09-20 21:04:27 +02004642 die(_("--name-only, --name-status, --check and -s are mutually exclusive"));
Timo Hirvonend7de00f2006-06-24 20:26:49 +03004643
Stefan Beller5e505252018-01-04 14:50:43 -08004644 if (HAS_MULTI_BITS(options->pickaxe_opts & DIFF_PICKAXE_KINDS_MASK))
4645 die(_("-G, -S and --find-object are mutually exclusive"));
4646
Ævar Arnfjörð Bjarmason188e9e22021-04-12 19:15:14 +02004647 if (HAS_MULTI_BITS(options->pickaxe_opts & DIFF_PICKAXE_KINDS_G_REGEX_MASK))
4648 die(_("-G and --pickaxe-regex are mutually exclusive, use --pickaxe-regex with -S"));
4649
Ævar Arnfjörð Bjarmasond26ec882021-04-12 19:15:15 +02004650 if (HAS_MULTI_BITS(options->pickaxe_opts & DIFF_PICKAXE_KINDS_ALL_OBJFIND_MASK))
Bagas Sanjaya11c649b2021-08-04 19:24:20 +07004651 die(_("--pickaxe-all and --find-object are mutually exclusive, use --pickaxe-all with -G and -S"));
Ævar Arnfjörð Bjarmasond26ec882021-04-12 19:15:15 +02004652
Junio C Hamanof2451942009-05-22 12:45:29 -07004653 /*
4654 * Most of the time we can say "there are changes"
4655 * only by checking if there are changed paths, but
4656 * --ignore-whitespace* options force us to look
Jim Meyering97bf2a02009-08-30 22:27:02 +02004657 * inside contents.
Junio C Hamanof2451942009-05-22 12:45:29 -07004658 */
4659
Junio C Hamano50f04392020-12-16 17:27:13 -08004660 if ((options->xdl_opts & XDF_WHITESPACE_FLAGS) ||
4661 options->ignore_regex_nr)
Brandon Williams0d1e0e72017-10-31 11:19:11 -07004662 options->flags.diff_from_contents = 1;
Junio C Hamanof2451942009-05-22 12:45:29 -07004663 else
Brandon Williams0d1e0e72017-10-31 11:19:11 -07004664 options->flags.diff_from_contents = 0;
Junio C Hamanof2451942009-05-22 12:45:29 -07004665
Brandon Williams0d1e0e72017-10-31 11:19:11 -07004666 if (options->flags.find_copies_harder)
Junio C Hamano03b9d562006-08-09 13:17:19 -07004667 options->detect_rename = DIFF_DETECT_COPY;
4668
Brandon Williams0d1e0e72017-10-31 11:19:11 -07004669 if (!options->flags.relative_name)
Junio C Hamanocd676a52008-02-12 14:26:02 -08004670 options->prefix = NULL;
4671 if (options->prefix)
4672 options->prefix_length = strlen(options->prefix);
4673 else
4674 options->prefix_length = 0;
4675
Timo Hirvonenc6744342006-06-24 20:21:53 +03004676 if (options->output_format & (DIFF_FORMAT_NAME |
4677 DIFF_FORMAT_NAME_STATUS |
4678 DIFF_FORMAT_CHECKDIFF |
4679 DIFF_FORMAT_NO_OUTPUT))
4680 options->output_format &= ~(DIFF_FORMAT_RAW |
Junio C Hamano74e2abe2006-10-12 03:01:00 -07004681 DIFF_FORMAT_NUMSTAT |
Timo Hirvonenc6744342006-06-24 20:21:53 +03004682 DIFF_FORMAT_DIFFSTAT |
Nicolas Pitreebd124c2006-12-14 23:15:44 -05004683 DIFF_FORMAT_SHORTSTAT |
Linus Torvalds7df7c012008-02-12 13:26:31 -08004684 DIFF_FORMAT_DIRSTAT |
Timo Hirvonenc6744342006-06-24 20:21:53 +03004685 DIFF_FORMAT_SUMMARY |
4686 DIFF_FORMAT_PATCH);
4687
Junio C Hamano6973dca2006-04-21 23:57:45 -07004688 /*
4689 * These cases always need recursive; we do not drop caller-supplied
4690 * recursive bits for other formats here.
4691 */
Timo Hirvonenc6744342006-06-24 20:21:53 +03004692 if (options->output_format & (DIFF_FORMAT_PATCH |
Junio C Hamano74e2abe2006-10-12 03:01:00 -07004693 DIFF_FORMAT_NUMSTAT |
Timo Hirvonenc6744342006-06-24 20:21:53 +03004694 DIFF_FORMAT_DIFFSTAT |
Nicolas Pitreebd124c2006-12-14 23:15:44 -05004695 DIFF_FORMAT_SHORTSTAT |
Linus Torvalds7df7c012008-02-12 13:26:31 -08004696 DIFF_FORMAT_DIRSTAT |
Johannes Schindelind7014dc2006-10-03 23:09:56 +02004697 DIFF_FORMAT_SUMMARY |
Timo Hirvonenc6744342006-06-24 20:21:53 +03004698 DIFF_FORMAT_CHECKDIFF))
Brandon Williams0d1e0e72017-10-31 11:19:11 -07004699 options->flags.recursive = 1;
Junio C Hamano5e363542006-05-22 00:31:02 -07004700 /*
Junio C Hamano3969cf72006-06-27 15:08:19 -07004701 * Also pickaxe would not work very well if you do not say recursive
Junio C Hamano5e363542006-05-22 00:31:02 -07004702 */
Stefan Bellercf630512018-01-04 14:50:41 -08004703 if (options->pickaxe_opts & DIFF_PICKAXE_KINDS_MASK)
Brandon Williams0d1e0e72017-10-31 11:19:11 -07004704 options->flags.recursive = 1;
Jens Lehmannae6d5c12010-03-11 22:50:25 +01004705 /*
4706 * When patches are generated, submodules diffed against the work tree
4707 * must be checked for dirtiness too so it can be shown in the output
4708 */
4709 if (options->output_format & DIFF_FORMAT_PATCH)
Brandon Williams0d1e0e72017-10-31 11:19:11 -07004710 options->flags.dirty_submodules = 1;
Junio C Hamano5e363542006-05-22 00:31:02 -07004711
Junio C Hamano6973dca2006-04-21 23:57:45 -07004712 if (options->detect_rename && options->rename_limit < 0)
4713 options->rename_limit = diff_rename_limit_default;
brian m. carlsonde1d81d2018-05-02 00:25:52 +00004714 if (hexsz < options->abbrev)
4715 options->abbrev = hexsz; /* full */
Junio C Hamano6973dca2006-04-21 23:57:45 -07004716
Junio C Hamano68aacb22007-03-14 11:12:13 -07004717 /*
4718 * It does not make sense to show the first hit we happened
4719 * to have found. It does not make sense not to return with
4720 * exit code in such a case either.
4721 */
Brandon Williams0d1e0e72017-10-31 11:19:11 -07004722 if (options->flags.quick) {
Junio C Hamano68aacb22007-03-14 11:12:13 -07004723 options->output_format = DIFF_FORMAT_NO_OUTPUT;
Brandon Williams0d1e0e72017-10-31 11:19:11 -07004724 options->flags.exit_with_status = 1;
Junio C Hamano68aacb22007-03-14 11:12:13 -07004725 }
Zoltan Klingeree7fb0b2013-12-06 10:38:46 +11004726
Jeff King0ea7d5b2013-12-16 15:02:21 -05004727 options->diff_path_counter = 0;
Junio C Hamanob0e2c992014-06-16 10:07:09 -07004728
Brandon Williams0d1e0e72017-10-31 11:19:11 -07004729 if (options->flags.follow_renames && options->pathspec.nr != 1)
Jeff Kingdd63f162014-05-20 02:49:20 -04004730 die(_("--follow requires exactly one pathspec"));
Stefan Beller2e2d5ac2017-06-30 13:53:07 -07004731
4732 if (!options->use_color || external_diff())
4733 options->color_moved = 0;
Nguyễn Thái Ngọc Duy4a288472019-01-27 07:35:31 +07004734
4735 FREE_AND_NULL(options->parseopts);
Junio C Hamano6973dca2006-04-21 23:57:45 -07004736}
4737
Matthieu Moydea007f2010-08-05 10:22:52 +02004738int parse_long_opt(const char *opt, const char **argv,
4739 const char **optarg)
4740{
4741 const char *arg = argv[0];
Jeff King95b567c2014-06-18 15:48:29 -04004742 if (!skip_prefix(arg, "--", &arg))
Matthieu Moydea007f2010-08-05 10:22:52 +02004743 return 0;
Jeff King95b567c2014-06-18 15:48:29 -04004744 if (!skip_prefix(arg, opt, &arg))
Matthieu Moydea007f2010-08-05 10:22:52 +02004745 return 0;
Nicolas Vigierb0d12fc2013-10-31 12:08:28 +01004746 if (*arg == '=') { /* stuck form: --option=value */
Matthieu Moydea007f2010-08-05 10:22:52 +02004747 *optarg = arg + 1;
4748 return 1;
4749 }
4750 if (*arg != '\0')
4751 return 0;
4752 /* separate form: --option value */
4753 if (!argv[1])
4754 die("Option '--%s' requires a value", opt);
4755 *optarg = argv[1];
4756 return 2;
4757}
4758
Nguyễn Thái Ngọc Duy84b50892019-02-21 18:16:10 +07004759static int diff_opt_stat(const struct option *opt, const char *value, int unset)
Jonathan Nieder4d7f7a42010-08-05 10:22:53 +02004760{
Nguyễn Thái Ngọc Duy84b50892019-02-21 18:16:10 +07004761 struct diff_options *options = opt->value;
Jonathan Nieder4d7f7a42010-08-05 10:22:53 +02004762 int width = options->stat_width;
4763 int name_width = options->stat_name_width;
Zbigniew Jędrzejewski-Szmek969fe572012-03-01 13:26:45 +01004764 int graph_width = options->stat_graph_width;
Michael J Gruber808e1db2011-05-27 14:36:41 +02004765 int count = options->stat_count;
Nguyễn Thái Ngọc Duy84b50892019-02-21 18:16:10 +07004766 char *end;
Jonathan Nieder4d7f7a42010-08-05 10:22:53 +02004767
Nguyễn Thái Ngọc Duy84b50892019-02-21 18:16:10 +07004768 BUG_ON_OPT_NEG(unset);
Jonathan Nieder4d7f7a42010-08-05 10:22:53 +02004769
Nguyễn Thái Ngọc Duy84b50892019-02-21 18:16:10 +07004770 if (!strcmp(opt->long_name, "stat")) {
4771 if (value) {
4772 width = strtoul(value, &end, 10);
4773 if (*end == ',')
4774 name_width = strtoul(end+1, &end, 10);
4775 if (*end == ',')
4776 count = strtoul(end+1, &end, 10);
4777 if (*end)
4778 return error(_("invalid --stat value: %s"), value);
Matthieu Moy1e572082010-08-05 10:22:54 +02004779 }
Nguyễn Thái Ngọc Duy84b50892019-02-21 18:16:10 +07004780 } else if (!strcmp(opt->long_name, "stat-width")) {
4781 width = strtoul(value, &end, 10);
4782 if (*end)
4783 return error(_("%s expects a numerical value"),
4784 opt->long_name);
4785 } else if (!strcmp(opt->long_name, "stat-name-width")) {
4786 name_width = strtoul(value, &end, 10);
4787 if (*end)
4788 return error(_("%s expects a numerical value"),
4789 opt->long_name);
4790 } else if (!strcmp(opt->long_name, "stat-graph-width")) {
4791 graph_width = strtoul(value, &end, 10);
4792 if (*end)
4793 return error(_("%s expects a numerical value"),
4794 opt->long_name);
4795 } else if (!strcmp(opt->long_name, "stat-count")) {
4796 count = strtoul(value, &end, 10);
4797 if (*end)
4798 return error(_("%s expects a numerical value"),
4799 opt->long_name);
4800 } else
4801 BUG("%s should not get here", opt->long_name);
Jonathan Nieder4d7f7a42010-08-05 10:22:53 +02004802
Jonathan Nieder4d7f7a42010-08-05 10:22:53 +02004803 options->output_format |= DIFF_FORMAT_DIFFSTAT;
4804 options->stat_name_width = name_width;
Zbigniew Jędrzejewski-Szmek969fe572012-03-01 13:26:45 +01004805 options->stat_graph_width = graph_width;
Jonathan Nieder4d7f7a42010-08-05 10:22:53 +02004806 options->stat_width = width;
Michael J Gruber808e1db2011-05-27 14:36:41 +02004807 options->stat_count = count;
Nguyễn Thái Ngọc Duy84b50892019-02-21 18:16:10 +07004808 return 0;
Jonathan Nieder4d7f7a42010-08-05 10:22:53 +02004809}
4810
Johan Herland333f3fb2011-04-29 11:36:18 +02004811static int parse_dirstat_opt(struct diff_options *options, const char *params)
4812{
Johan Herland51670fc2011-04-29 11:36:22 +02004813 struct strbuf errmsg = STRBUF_INIT;
4814 if (parse_dirstat_params(options, params, &errmsg))
Johan Herland7478ac52011-04-29 11:36:23 +02004815 die(_("Failed to parse --dirstat/-X option parameter:\n%s"),
Johan Herland51670fc2011-04-29 11:36:22 +02004816 errmsg.buf);
4817 strbuf_release(&errmsg);
Johan Herland333f3fb2011-04-29 11:36:18 +02004818 /*
4819 * The caller knows a dirstat-related option is given from the command
4820 * line; allow it to say "return this_function();"
4821 */
4822 options->output_format |= DIFF_FORMAT_DIRSTAT;
4823 return 1;
4824}
4825
Junio C Hamano1ecc1cb2013-07-17 15:05:46 -07004826static const char diff_status_letters[] = {
4827 DIFF_STATUS_ADDED,
4828 DIFF_STATUS_COPIED,
4829 DIFF_STATUS_DELETED,
4830 DIFF_STATUS_MODIFIED,
4831 DIFF_STATUS_RENAMED,
4832 DIFF_STATUS_TYPE_CHANGED,
4833 DIFF_STATUS_UNKNOWN,
4834 DIFF_STATUS_UNMERGED,
4835 DIFF_STATUS_FILTER_AON,
4836 DIFF_STATUS_FILTER_BROKEN,
4837 '\0',
4838};
4839
4840static unsigned int filter_bit['Z' + 1];
4841
4842static void prepare_filter_bits(void)
4843{
4844 int i;
4845
4846 if (!filter_bit[DIFF_STATUS_ADDED]) {
4847 for (i = 0; diff_status_letters[i]; i++)
4848 filter_bit[(int) diff_status_letters[i]] = (1 << i);
4849 }
4850}
4851
4852static unsigned filter_bit_tst(char status, const struct diff_options *opt)
4853{
4854 return opt->filter & filter_bit[(int) status];
4855}
4856
Nguyễn Thái Ngọc Duycdb53302019-03-24 15:20:14 +07004857unsigned diff_filter_bit(char status)
Junio C Hamano1ecc1cb2013-07-17 15:05:46 -07004858{
Nguyễn Thái Ngọc Duycdb53302019-03-24 15:20:14 +07004859 prepare_filter_bits();
4860 return filter_bit[(int) status];
4861}
4862
Nguyễn Thái Ngọc Duyd2d3f272019-03-24 15:20:03 +07004863static int diff_opt_diff_filter(const struct option *option,
4864 const char *optarg, int unset)
Junio C Hamano1ecc1cb2013-07-17 15:05:46 -07004865{
Nguyễn Thái Ngọc Duyd2d3f272019-03-24 15:20:03 +07004866 struct diff_options *opt = option->value;
Junio C Hamano1ecc1cb2013-07-17 15:05:46 -07004867 int i, optch;
4868
Nguyễn Thái Ngọc Duyd2d3f272019-03-24 15:20:03 +07004869 BUG_ON_OPT_NEG(unset);
Junio C Hamano1ecc1cb2013-07-17 15:05:46 -07004870 prepare_filter_bits();
Junio C Hamano7f2ea5f2013-07-17 16:19:19 -07004871
4872 /*
4873 * If there is a negation e.g. 'd' in the input, and we haven't
4874 * initialized the filter field with another --diff-filter, start
4875 * from full set of bits, except for AON.
4876 */
4877 if (!opt->filter) {
4878 for (i = 0; (optch = optarg[i]) != '\0'; i++) {
4879 if (optch < 'a' || 'z' < optch)
4880 continue;
4881 opt->filter = (1 << (ARRAY_SIZE(diff_status_letters) - 1)) - 1;
4882 opt->filter &= ~filter_bit[DIFF_STATUS_FILTER_AON];
4883 break;
4884 }
4885 }
4886
Junio C Hamano1ecc1cb2013-07-17 15:05:46 -07004887 for (i = 0; (optch = optarg[i]) != '\0'; i++) {
4888 unsigned int bit;
Junio C Hamano7f2ea5f2013-07-17 16:19:19 -07004889 int negate;
4890
4891 if ('a' <= optch && optch <= 'z') {
4892 negate = 1;
4893 optch = toupper(optch);
4894 } else {
4895 negate = 0;
4896 }
Junio C Hamano1ecc1cb2013-07-17 15:05:46 -07004897
4898 bit = (0 <= optch && optch <= 'Z') ? filter_bit[optch] : 0;
4899 if (!bit)
Nguyễn Thái Ngọc Duyd2d3f272019-03-24 15:20:03 +07004900 return error(_("unknown change class '%c' in --diff-filter=%s"),
4901 optarg[i], optarg);
Junio C Hamano7f2ea5f2013-07-17 16:19:19 -07004902 if (negate)
4903 opt->filter &= ~bit;
4904 else
4905 opt->filter |= bit;
Junio C Hamano1ecc1cb2013-07-17 15:05:46 -07004906 }
4907 return 0;
4908}
4909
Nguyễn Thái Ngọc Duy3b335762018-12-09 11:25:21 +01004910static void enable_patch_output(int *fmt)
4911{
Matthieu Moy71482d32013-07-16 10:05:37 +02004912 *fmt &= ~DIFF_FORMAT_NO_OUTPUT;
4913 *fmt |= DIFF_FORMAT_PATCH;
4914}
4915
Nguyễn Thái Ngọc Duy5a749d92019-03-24 15:19:55 +07004916static int diff_opt_ws_error_highlight(const struct option *option,
4917 const char *arg, int unset)
Junio C Hamanob8767f72015-05-26 10:11:28 -07004918{
Nguyễn Thái Ngọc Duy5a749d92019-03-24 15:19:55 +07004919 struct diff_options *opt = option->value;
Junio C Hamano077965f2016-10-04 15:03:33 -07004920 int val = parse_ws_error_highlight(arg);
Junio C Hamanob8767f72015-05-26 10:11:28 -07004921
Nguyễn Thái Ngọc Duy5a749d92019-03-24 15:19:55 +07004922 BUG_ON_OPT_NEG(unset);
4923 if (val < 0)
4924 return error(_("unknown value after ws-error-highlight=%.*s"),
4925 -1 - val, arg);
Junio C Hamanob8767f72015-05-26 10:11:28 -07004926 opt->ws_error_highlight = val;
Nguyễn Thái Ngọc Duy5a749d92019-03-24 15:19:55 +07004927 return 0;
Junio C Hamanob8767f72015-05-26 10:11:28 -07004928}
4929
Nguyễn Thái Ngọc Duya75f28c2019-03-24 15:20:02 +07004930static int diff_opt_find_object(const struct option *option,
4931 const char *arg, int unset)
Stefan Beller15af58c2018-01-04 14:50:42 -08004932{
Nguyễn Thái Ngọc Duya75f28c2019-03-24 15:20:02 +07004933 struct diff_options *opt = option->value;
Stefan Beller15af58c2018-01-04 14:50:42 -08004934 struct object_id oid;
4935
Nguyễn Thái Ngọc Duya75f28c2019-03-24 15:20:02 +07004936 BUG_ON_OPT_NEG(unset);
Stefan Beller15af58c2018-01-04 14:50:42 -08004937 if (get_oid(arg, &oid))
Nguyễn Thái Ngọc Duya75f28c2019-03-24 15:20:02 +07004938 return error(_("unable to resolve '%s'"), arg);
Stefan Beller15af58c2018-01-04 14:50:42 -08004939
4940 if (!opt->objfind)
René Scharfeca56dad2021-03-13 17:17:22 +01004941 CALLOC_ARRAY(opt->objfind, 1);
Stefan Beller15af58c2018-01-04 14:50:42 -08004942
4943 opt->pickaxe_opts |= DIFF_PICKAXE_KIND_OBJFIND;
4944 opt->flags.recursive = 1;
4945 opt->flags.tree_in_recursive = 1;
4946 oidset_insert(opt->objfind, &oid);
Nguyễn Thái Ngọc Duya75f28c2019-03-24 15:20:02 +07004947 return 0;
Stefan Beller15af58c2018-01-04 14:50:42 -08004948}
4949
Nguyễn Thái Ngọc Duydf84a432019-03-05 19:30:11 +07004950static int diff_opt_anchored(const struct option *opt,
4951 const char *arg, int unset)
4952{
4953 struct diff_options *options = opt->value;
4954
4955 BUG_ON_OPT_NEG(unset);
4956 options->xdl_opts = DIFF_WITH_ALG(options, PATIENCE_DIFF);
4957 ALLOC_GROW(options->anchors, options->anchors_nr + 1,
4958 options->anchors_alloc);
4959 options->anchors[options->anchors_nr++] = xstrdup(arg);
4960 return 0;
4961}
4962
Nguyễn Thái Ngọc Duy6d9af6f2019-03-05 19:30:12 +07004963static int diff_opt_binary(const struct option *opt,
4964 const char *arg, int unset)
4965{
4966 struct diff_options *options = opt->value;
4967
4968 BUG_ON_OPT_NEG(unset);
4969 BUG_ON_OPT_ARG(arg);
4970 enable_patch_output(&options->output_format);
4971 options->flags.binary = 1;
4972 return 0;
4973}
4974
Nguyễn Thái Ngọc Duyced4e172019-02-21 18:16:13 +07004975static int diff_opt_break_rewrites(const struct option *opt,
4976 const char *arg, int unset)
4977{
4978 int *break_opt = opt->value;
4979 int opt1, opt2;
4980
4981 BUG_ON_OPT_NEG(unset);
4982 if (!arg)
4983 arg = "";
4984 opt1 = parse_rename_score(&arg);
4985 if (*arg == 0)
4986 opt2 = 0;
4987 else if (*arg != '/')
4988 return error(_("%s expects <n>/<m> form"), opt->long_name);
4989 else {
4990 arg++;
4991 opt2 = parse_rename_score(&arg);
4992 }
4993 if (*arg != 0)
4994 return error(_("%s expects <n>/<m> form"), opt->long_name);
4995 *break_opt = opt1 | (opt2 << 16);
4996 return 0;
4997}
4998
Nguyễn Thái Ngọc Duyaf2f3682019-02-21 18:16:12 +07004999static int diff_opt_char(const struct option *opt,
5000 const char *arg, int unset)
5001{
5002 char *value = opt->value;
5003
5004 BUG_ON_OPT_NEG(unset);
5005 if (arg[1])
5006 return error(_("%s expects a character, got '%s'"),
5007 opt->long_name, arg);
5008 *value = arg[0];
5009 return 0;
5010}
5011
Nguyễn Thái Ngọc Duy59311a92019-03-24 15:20:09 +07005012static int diff_opt_color_moved(const struct option *opt,
5013 const char *arg, int unset)
5014{
5015 struct diff_options *options = opt->value;
5016
5017 if (unset) {
5018 options->color_moved = COLOR_MOVED_NO;
5019 } else if (!arg) {
5020 if (diff_color_moved_default)
5021 options->color_moved = diff_color_moved_default;
5022 if (options->color_moved == COLOR_MOVED_NO)
5023 options->color_moved = COLOR_MOVED_DEFAULT;
5024 } else {
5025 int cm = parse_color_moved(arg);
5026 if (cm < 0)
5027 return error(_("bad --color-moved argument: %s"), arg);
5028 options->color_moved = cm;
5029 }
5030 return 0;
5031}
5032
Nguyễn Thái Ngọc Duy8ce20202019-03-24 15:20:10 +07005033static int diff_opt_color_moved_ws(const struct option *opt,
5034 const char *arg, int unset)
5035{
5036 struct diff_options *options = opt->value;
5037 unsigned cm;
5038
Nguyễn Thái Ngọc Duybb987292019-03-24 15:20:11 +07005039 if (unset) {
5040 options->color_moved_ws_handling = 0;
5041 return 0;
5042 }
5043
Nguyễn Thái Ngọc Duy8ce20202019-03-24 15:20:10 +07005044 cm = parse_color_moved_ws(arg);
5045 if (cm & COLOR_MOVED_WS_ERROR)
5046 return error(_("invalid mode '%s' in --color-moved-ws"), arg);
5047 options->color_moved_ws_handling = cm;
5048 return 0;
5049}
5050
Nguyễn Thái Ngọc Duy212db692019-03-05 19:30:20 +07005051static int diff_opt_color_words(const struct option *opt,
5052 const char *arg, int unset)
5053{
5054 struct diff_options *options = opt->value;
5055
5056 BUG_ON_OPT_NEG(unset);
5057 options->use_color = 1;
5058 options->word_diff = DIFF_WORDS_COLOR;
5059 options->word_regex = arg;
5060 return 0;
5061}
5062
Nguyễn Thái Ngọc Duy7d7942b2019-02-21 18:16:11 +07005063static int diff_opt_compact_summary(const struct option *opt,
5064 const char *arg, int unset)
5065{
5066 struct diff_options *options = opt->value;
5067
5068 BUG_ON_OPT_ARG(arg);
5069 if (unset) {
5070 options->flags.stat_with_summary = 0;
5071 } else {
5072 options->flags.stat_with_summary = 1;
5073 options->output_format |= DIFF_FORMAT_DIFFSTAT;
5074 }
5075 return 0;
5076}
5077
Nguyễn Thái Ngọc Duy10f35b12019-03-05 19:30:10 +07005078static int diff_opt_diff_algorithm(const struct option *opt,
5079 const char *arg, int unset)
5080{
5081 struct diff_options *options = opt->value;
5082 long value = parse_algorithm_value(arg);
5083
5084 BUG_ON_OPT_NEG(unset);
5085 if (value < 0)
5086 return error(_("option diff-algorithm accepts \"myers\", "
5087 "\"minimal\", \"patience\" and \"histogram\""));
5088
5089 /* clear out previous settings */
5090 DIFF_XDL_CLR(options, NEED_MINIMAL);
5091 options->xdl_opts &= ~XDF_DIFF_ALGORITHM_MASK;
5092 options->xdl_opts |= value;
5093 return 0;
5094}
5095
Nguyễn Thái Ngọc Duy4ce7aab2019-02-21 18:16:03 +07005096static int diff_opt_dirstat(const struct option *opt,
5097 const char *arg, int unset)
5098{
5099 struct diff_options *options = opt->value;
5100
5101 BUG_ON_OPT_NEG(unset);
5102 if (!strcmp(opt->long_name, "cumulative")) {
5103 if (arg)
5104 BUG("how come --cumulative take a value?");
5105 arg = "cumulative";
5106 } else if (!strcmp(opt->long_name, "dirstat-by-file"))
5107 parse_dirstat_opt(options, "files");
5108 parse_dirstat_opt(options, arg ? arg : "");
5109 return 0;
5110}
5111
Nguyễn Thái Ngọc Duy7f648502019-02-21 18:16:16 +07005112static int diff_opt_find_copies(const struct option *opt,
5113 const char *arg, int unset)
5114{
5115 struct diff_options *options = opt->value;
5116
5117 BUG_ON_OPT_NEG(unset);
5118 if (!arg)
5119 arg = "";
5120 options->rename_score = parse_rename_score(&arg);
5121 if (*arg != 0)
5122 return error(_("invalid argument to %s"), opt->long_name);
5123
5124 if (options->detect_rename == DIFF_DETECT_COPY)
5125 options->flags.find_copies_harder = 1;
5126 else
5127 options->detect_rename = DIFF_DETECT_COPY;
5128
5129 return 0;
5130}
5131
Nguyễn Thái Ngọc Duyf4763082019-02-21 18:16:14 +07005132static int diff_opt_find_renames(const struct option *opt,
5133 const char *arg, int unset)
5134{
5135 struct diff_options *options = opt->value;
5136
5137 BUG_ON_OPT_NEG(unset);
5138 if (!arg)
5139 arg = "";
5140 options->rename_score = parse_rename_score(&arg);
5141 if (*arg != 0)
5142 return error(_("invalid argument to %s"), opt->long_name);
5143
5144 options->detect_rename = DIFF_DETECT_RENAME;
5145 return 0;
5146}
5147
Nguyễn Thái Ngọc Duy1e9250b2019-03-05 19:30:16 +07005148static int diff_opt_follow(const struct option *opt,
5149 const char *arg, int unset)
5150{
5151 struct diff_options *options = opt->value;
5152
5153 BUG_ON_OPT_ARG(arg);
5154 if (unset) {
5155 options->flags.follow_renames = 0;
5156 options->flags.default_follow_renames = 0;
5157 } else {
5158 options->flags.follow_renames = 1;
5159 }
5160 return 0;
5161}
5162
Nguyễn Thái Ngọc Duyb680ee12019-03-05 19:30:25 +07005163static int diff_opt_ignore_submodules(const struct option *opt,
5164 const char *arg, int unset)
5165{
5166 struct diff_options *options = opt->value;
5167
5168 BUG_ON_OPT_NEG(unset);
5169 if (!arg)
5170 arg = "all";
5171 options->flags.override_submodule_config = 1;
5172 handle_ignore_submodules_arg(options, arg);
5173 return 0;
5174}
5175
Nguyễn Thái Ngọc Duy2f81cf92019-03-24 15:20:06 +07005176static int diff_opt_line_prefix(const struct option *opt,
5177 const char *optarg, int unset)
5178{
5179 struct diff_options *options = opt->value;
5180
5181 BUG_ON_OPT_NEG(unset);
5182 options->line_prefix = optarg;
5183 options->line_prefix_length = strlen(options->line_prefix);
5184 graph_setup_line_prefix(options);
5185 return 0;
5186}
5187
Nguyễn Thái Ngọc Duy11c659d2019-03-24 15:20:07 +07005188static int diff_opt_no_prefix(const struct option *opt,
5189 const char *optarg, int unset)
5190{
5191 struct diff_options *options = opt->value;
5192
5193 BUG_ON_OPT_NEG(unset);
5194 BUG_ON_OPT_ARG(optarg);
5195 options->a_prefix = "";
5196 options->b_prefix = "";
5197 return 0;
5198}
5199
Nguyễn Thái Ngọc Duyaf2f3682019-02-21 18:16:12 +07005200static enum parse_opt_result diff_opt_output(struct parse_opt_ctx_t *ctx,
5201 const struct option *opt,
5202 const char *arg, int unset)
5203{
5204 struct diff_options *options = opt->value;
5205 char *path;
5206
5207 BUG_ON_OPT_NEG(unset);
5208 path = prefix_filename(ctx->prefix, arg);
5209 options->file = xfopen(path, "w");
5210 options->close_file = 1;
5211 if (options->use_color != GIT_COLOR_ALWAYS)
5212 options->use_color = GIT_COLOR_NEVER;
5213 free(path);
5214 return 0;
5215}
5216
Nguyễn Thái Ngọc Duy31fd6402019-03-05 19:30:08 +07005217static int diff_opt_patience(const struct option *opt,
5218 const char *arg, int unset)
5219{
5220 struct diff_options *options = opt->value;
5221 int i;
5222
5223 BUG_ON_OPT_NEG(unset);
5224 BUG_ON_OPT_ARG(arg);
5225 options->xdl_opts = DIFF_WITH_ALG(options, PATIENCE_DIFF);
5226 /*
5227 * Both --patience and --anchored use PATIENCE_DIFF
5228 * internally, so remove any anchors previously
5229 * specified.
5230 */
5231 for (i = 0; i < options->anchors_nr; i++)
5232 free(options->anchors[i]);
5233 options->anchors_nr = 0;
5234 return 0;
5235}
5236
Michał Kępień296d4a92020-10-20 08:48:09 +02005237static int diff_opt_ignore_regex(const struct option *opt,
5238 const char *arg, int unset)
5239{
5240 struct diff_options *options = opt->value;
5241 regex_t *regex;
5242
5243 BUG_ON_OPT_NEG(unset);
5244 regex = xmalloc(sizeof(*regex));
5245 if (regcomp(regex, arg, REG_EXTENDED | REG_NEWLINE))
5246 return error(_("invalid regex given to -I: '%s'"), arg);
5247 ALLOC_GROW(options->ignore_regex, options->ignore_regex_nr + 1,
5248 options->ignore_regex_alloc);
5249 options->ignore_regex[options->ignore_regex_nr++] = regex;
5250 return 0;
5251}
5252
Nguyễn Thái Ngọc Duya41cfb32019-03-24 15:19:59 +07005253static int diff_opt_pickaxe_regex(const struct option *opt,
5254 const char *arg, int unset)
5255{
5256 struct diff_options *options = opt->value;
5257
5258 BUG_ON_OPT_NEG(unset);
5259 options->pickaxe = arg;
5260 options->pickaxe_opts |= DIFF_PICKAXE_KIND_G;
5261 return 0;
5262}
5263
5264static int diff_opt_pickaxe_string(const struct option *opt,
5265 const char *arg, int unset)
5266{
5267 struct diff_options *options = opt->value;
5268
5269 BUG_ON_OPT_NEG(unset);
5270 options->pickaxe = arg;
5271 options->pickaxe_opts |= DIFF_PICKAXE_KIND_S;
5272 return 0;
5273}
5274
Nguyễn Thái Ngọc Duy0b1c5b52019-02-21 18:16:19 +07005275static int diff_opt_relative(const struct option *opt,
5276 const char *arg, int unset)
5277{
5278 struct diff_options *options = opt->value;
5279
Laurent Arnoudc28ded82020-05-22 12:46:18 +02005280 options->flags.relative_name = !unset;
Nguyễn Thái Ngọc Duy0b1c5b52019-02-21 18:16:19 +07005281 if (arg)
5282 options->prefix = arg;
5283 return 0;
5284}
5285
Nguyễn Thái Ngọc Duy125dcea2019-03-05 19:30:26 +07005286static int diff_opt_submodule(const struct option *opt,
5287 const char *arg, int unset)
5288{
5289 struct diff_options *options = opt->value;
5290
5291 BUG_ON_OPT_NEG(unset);
5292 if (!arg)
5293 arg = "log";
5294 if (parse_submodule_params(options, arg))
5295 return error(_("failed to parse --submodule option parameter: '%s'"),
5296 arg);
5297 return 0;
5298}
5299
Nguyễn Thái Ngọc Duy8ab76972019-03-05 19:30:24 +07005300static int diff_opt_textconv(const struct option *opt,
5301 const char *arg, int unset)
5302{
5303 struct diff_options *options = opt->value;
5304
5305 BUG_ON_OPT_ARG(arg);
5306 if (unset) {
5307 options->flags.allow_textconv = 0;
5308 } else {
5309 options->flags.allow_textconv = 1;
5310 options->flags.textconv_set_via_cmdline = 1;
5311 }
5312 return 0;
5313}
5314
Nguyễn Thái Ngọc Duyd473e2e2019-01-27 07:35:33 +07005315static int diff_opt_unified(const struct option *opt,
5316 const char *arg, int unset)
5317{
5318 struct diff_options *options = opt->value;
5319 char *s;
5320
5321 BUG_ON_OPT_NEG(unset);
5322
Nguyễn Thái Ngọc Duy8ef05192019-05-29 16:11:15 +07005323 if (arg) {
5324 options->context = strtol(arg, &s, 10);
5325 if (*s)
5326 return error(_("%s expects a numerical value"), "--unified");
5327 }
Nguyễn Thái Ngọc Duyd473e2e2019-01-27 07:35:33 +07005328 enable_patch_output(&options->output_format);
5329
5330 return 0;
5331}
5332
Nguyễn Thái Ngọc Duye9fb39b2019-03-05 19:30:18 +07005333static int diff_opt_word_diff(const struct option *opt,
5334 const char *arg, int unset)
5335{
5336 struct diff_options *options = opt->value;
5337
5338 BUG_ON_OPT_NEG(unset);
5339 if (arg) {
5340 if (!strcmp(arg, "plain"))
5341 options->word_diff = DIFF_WORDS_PLAIN;
5342 else if (!strcmp(arg, "color")) {
5343 options->use_color = 1;
5344 options->word_diff = DIFF_WORDS_COLOR;
5345 }
5346 else if (!strcmp(arg, "porcelain"))
5347 options->word_diff = DIFF_WORDS_PORCELAIN;
5348 else if (!strcmp(arg, "none"))
5349 options->word_diff = DIFF_WORDS_NONE;
5350 else
5351 return error(_("bad --word-diff argument: %s"), arg);
5352 } else {
5353 if (options->word_diff == DIFF_WORDS_NONE)
5354 options->word_diff = DIFF_WORDS_PLAIN;
5355 }
5356 return 0;
5357}
5358
Nguyễn Thái Ngọc Duy797df112019-03-05 19:30:19 +07005359static int diff_opt_word_diff_regex(const struct option *opt,
5360 const char *arg, int unset)
5361{
5362 struct diff_options *options = opt->value;
5363
5364 BUG_ON_OPT_NEG(unset);
5365 if (options->word_diff == DIFF_WORDS_NONE)
5366 options->word_diff = DIFF_WORDS_PLAIN;
5367 options->word_regex = arg;
5368 return 0;
5369}
5370
Junio C Hamano1eb41362021-02-11 11:57:50 -08005371static int diff_opt_rotate_to(const struct option *opt, const char *arg, int unset)
5372{
5373 struct diff_options *options = opt->value;
5374
5375 BUG_ON_OPT_NEG(unset);
5376 if (!strcmp(opt->long_name, "skip-to"))
5377 options->skip_instead_of_rotate = 1;
5378 else
5379 options->skip_instead_of_rotate = 0;
5380 options->rotate_to = arg;
5381 return 0;
5382}
5383
Nguyễn Thái Ngọc Duy4a288472019-01-27 07:35:31 +07005384static void prep_parse_options(struct diff_options *options)
5385{
5386 struct option parseopts[] = {
Nguyễn Thái Ngọc Duycc013c22019-01-27 07:35:32 +07005387 OPT_GROUP(N_("Diff output format options")),
5388 OPT_BITOP('p', "patch", &options->output_format,
5389 N_("generate patch"),
5390 DIFF_FORMAT_PATCH, DIFF_FORMAT_NO_OUTPUT),
Nguyễn Thái Ngọc Duye01df7a2019-02-21 18:16:09 +07005391 OPT_BIT_F('s', "no-patch", &options->output_format,
5392 N_("suppress diff output"),
5393 DIFF_FORMAT_NO_OUTPUT, PARSE_OPT_NONEG),
Nguyễn Thái Ngọc Duycc013c22019-01-27 07:35:32 +07005394 OPT_BITOP('u', NULL, &options->output_format,
5395 N_("generate patch"),
5396 DIFF_FORMAT_PATCH, DIFF_FORMAT_NO_OUTPUT),
Nguyễn Thái Ngọc Duyd473e2e2019-01-27 07:35:33 +07005397 OPT_CALLBACK_F('U', "unified", options, N_("<n>"),
5398 N_("generate diffs with <n> lines context"),
Junio C Hamano20aa7c52019-05-30 10:50:44 -07005399 PARSE_OPT_NONEG | PARSE_OPT_OPTARG, diff_opt_unified),
Nguyễn Thái Ngọc Duy7fd9a1b2019-01-27 07:35:34 +07005400 OPT_BOOL('W', "function-context", &options->flags.funccontext,
5401 N_("generate diffs with <n> lines context")),
Nguyễn Thái Ngọc Duyed881482019-01-27 07:35:35 +07005402 OPT_BIT_F(0, "raw", &options->output_format,
5403 N_("generate the diff in raw format"),
5404 DIFF_FORMAT_RAW, PARSE_OPT_NONEG),
Nguyễn Thái Ngọc Duyc659f302019-02-16 18:36:35 +07005405 OPT_BITOP(0, "patch-with-raw", &options->output_format,
5406 N_("synonym for '-p --raw'"),
5407 DIFF_FORMAT_PATCH | DIFF_FORMAT_RAW,
5408 DIFF_FORMAT_NO_OUTPUT),
Nguyễn Thái Ngọc Duye550f582019-02-21 18:16:06 +07005409 OPT_BITOP(0, "patch-with-stat", &options->output_format,
5410 N_("synonym for '-p --stat'"),
5411 DIFF_FORMAT_PATCH | DIFF_FORMAT_DIFFSTAT,
5412 DIFF_FORMAT_NO_OUTPUT),
Nguyễn Thái Ngọc Duye5673622019-02-16 18:36:36 +07005413 OPT_BIT_F(0, "numstat", &options->output_format,
5414 N_("machine friendly --stat"),
5415 DIFF_FORMAT_NUMSTAT, PARSE_OPT_NONEG),
5416 OPT_BIT_F(0, "shortstat", &options->output_format,
5417 N_("output only the last line of --stat"),
5418 DIFF_FORMAT_SHORTSTAT, PARSE_OPT_NONEG),
Nguyễn Thái Ngọc Duy4ce7aab2019-02-21 18:16:03 +07005419 OPT_CALLBACK_F('X', "dirstat", options, N_("<param1,param2>..."),
5420 N_("output the distribution of relative amount of changes for each sub-directory"),
5421 PARSE_OPT_NONEG | PARSE_OPT_OPTARG,
5422 diff_opt_dirstat),
5423 OPT_CALLBACK_F(0, "cumulative", options, NULL,
5424 N_("synonym for --dirstat=cumulative"),
5425 PARSE_OPT_NONEG | PARSE_OPT_NOARG,
5426 diff_opt_dirstat),
5427 OPT_CALLBACK_F(0, "dirstat-by-file", options, N_("<param1,param2>..."),
5428 N_("synonym for --dirstat=files,param1,param2..."),
5429 PARSE_OPT_NONEG | PARSE_OPT_OPTARG,
5430 diff_opt_dirstat),
Nguyễn Thái Ngọc Duyfc6af3e2019-02-21 18:16:04 +07005431 OPT_BIT_F(0, "check", &options->output_format,
5432 N_("warn if changes introduce conflict markers or whitespace errors"),
5433 DIFF_FORMAT_CHECKDIFF, PARSE_OPT_NONEG),
Nguyễn Thái Ngọc Duy70a30412019-02-21 18:16:05 +07005434 OPT_BIT_F(0, "summary", &options->output_format,
5435 N_("condensed summary such as creations, renames and mode changes"),
5436 DIFF_FORMAT_SUMMARY, PARSE_OPT_NONEG),
Nguyễn Thái Ngọc Duy0e840e22019-02-21 18:16:07 +07005437 OPT_BIT_F(0, "name-only", &options->output_format,
5438 N_("show only names of changed files"),
5439 DIFF_FORMAT_NAME, PARSE_OPT_NONEG),
Nguyễn Thái Ngọc Duya2387472019-02-21 18:16:08 +07005440 OPT_BIT_F(0, "name-status", &options->output_format,
5441 N_("show only names and status of changed files"),
5442 DIFF_FORMAT_NAME_STATUS, PARSE_OPT_NONEG),
Nguyễn Thái Ngọc Duy84b50892019-02-21 18:16:10 +07005443 OPT_CALLBACK_F(0, "stat", options, N_("<width>[,<name-width>[,<count>]]"),
5444 N_("generate diffstat"),
5445 PARSE_OPT_NONEG | PARSE_OPT_OPTARG, diff_opt_stat),
5446 OPT_CALLBACK_F(0, "stat-width", options, N_("<width>"),
5447 N_("generate diffstat with a given width"),
5448 PARSE_OPT_NONEG, diff_opt_stat),
5449 OPT_CALLBACK_F(0, "stat-name-width", options, N_("<width>"),
5450 N_("generate diffstat with a given name width"),
5451 PARSE_OPT_NONEG, diff_opt_stat),
5452 OPT_CALLBACK_F(0, "stat-graph-width", options, N_("<width>"),
5453 N_("generate diffstat with a given graph width"),
5454 PARSE_OPT_NONEG, diff_opt_stat),
5455 OPT_CALLBACK_F(0, "stat-count", options, N_("<count>"),
5456 N_("generate diffstat with limited lines"),
5457 PARSE_OPT_NONEG, diff_opt_stat),
Nguyễn Thái Ngọc Duy7d7942b2019-02-21 18:16:11 +07005458 OPT_CALLBACK_F(0, "compact-summary", options, NULL,
5459 N_("generate compact summary in diffstat"),
5460 PARSE_OPT_NOARG, diff_opt_compact_summary),
Nguyễn Thái Ngọc Duy6d9af6f2019-03-05 19:30:12 +07005461 OPT_CALLBACK_F(0, "binary", options, NULL,
5462 N_("output a binary diff that can be applied"),
5463 PARSE_OPT_NONEG | PARSE_OPT_NOARG, diff_opt_binary),
Nguyễn Thái Ngọc Duy4fe01672019-03-05 19:30:13 +07005464 OPT_BOOL(0, "full-index", &options->flags.full_index,
5465 N_("show full pre- and post-image object names on the \"index\" lines")),
Nguyễn Thái Ngọc Duy8b81c262019-03-05 19:30:17 +07005466 OPT_COLOR_FLAG(0, "color", &options->use_color,
5467 N_("show colored diff")),
Nguyễn Thái Ngọc Duy5a749d92019-03-24 15:19:55 +07005468 OPT_CALLBACK_F(0, "ws-error-highlight", options, N_("<kind>"),
5469 N_("highlight whitespace errors in the 'context', 'old' or 'new' lines in the diff"),
5470 PARSE_OPT_NONEG, diff_opt_ws_error_highlight),
Nguyễn Thái Ngọc Duy1a1eb162019-03-24 15:19:57 +07005471 OPT_SET_INT('z', NULL, &options->line_termination,
5472 N_("do not munge pathnames and use NULs as output field terminators in --raw or --numstat"),
5473 0),
Nguyễn Thái Ngọc Duyd8774182019-03-24 15:20:04 +07005474 OPT__ABBREV(&options->abbrev),
Nguyễn Thái Ngọc Duy31fba9d2019-03-24 15:20:05 +07005475 OPT_STRING_F(0, "src-prefix", &options->a_prefix, N_("<prefix>"),
5476 N_("show the given source prefix instead of \"a/\""),
5477 PARSE_OPT_NONEG),
5478 OPT_STRING_F(0, "dst-prefix", &options->b_prefix, N_("<prefix>"),
Jiang Xin8a1569d2019-06-02 23:11:22 +08005479 N_("show the given destination prefix instead of \"b/\""),
Nguyễn Thái Ngọc Duy31fba9d2019-03-24 15:20:05 +07005480 PARSE_OPT_NONEG),
Nguyễn Thái Ngọc Duy2f81cf92019-03-24 15:20:06 +07005481 OPT_CALLBACK_F(0, "line-prefix", options, N_("<prefix>"),
5482 N_("prepend an additional prefix to every line of output"),
5483 PARSE_OPT_NONEG, diff_opt_line_prefix),
Nguyễn Thái Ngọc Duy11c659d2019-03-24 15:20:07 +07005484 OPT_CALLBACK_F(0, "no-prefix", options, NULL,
5485 N_("do not show any source or destination prefix"),
5486 PARSE_OPT_NONEG | PARSE_OPT_NOARG, diff_opt_no_prefix),
Nguyễn Thái Ngọc Duy16ed6c92019-03-24 15:20:08 +07005487 OPT_INTEGER_F(0, "inter-hunk-context", &options->interhunkcontext,
5488 N_("show context between diff hunks up to the specified number of lines"),
5489 PARSE_OPT_NONEG),
Nguyễn Thái Ngọc Duyaf2f3682019-02-21 18:16:12 +07005490 OPT_CALLBACK_F(0, "output-indicator-new",
5491 &options->output_indicators[OUTPUT_INDICATOR_NEW],
5492 N_("<char>"),
5493 N_("specify the character to indicate a new line instead of '+'"),
5494 PARSE_OPT_NONEG, diff_opt_char),
5495 OPT_CALLBACK_F(0, "output-indicator-old",
5496 &options->output_indicators[OUTPUT_INDICATOR_OLD],
5497 N_("<char>"),
5498 N_("specify the character to indicate an old line instead of '-'"),
5499 PARSE_OPT_NONEG, diff_opt_char),
5500 OPT_CALLBACK_F(0, "output-indicator-context",
5501 &options->output_indicators[OUTPUT_INDICATOR_CONTEXT],
5502 N_("<char>"),
5503 N_("specify the character to indicate a context instead of ' '"),
5504 PARSE_OPT_NONEG, diff_opt_char),
5505
Nguyễn Thái Ngọc Duyced4e172019-02-21 18:16:13 +07005506 OPT_GROUP(N_("Diff rename options")),
5507 OPT_CALLBACK_F('B', "break-rewrites", &options->break_opt, N_("<n>[/<m>]"),
5508 N_("break complete rewrite changes into pairs of delete and create"),
5509 PARSE_OPT_NONEG | PARSE_OPT_OPTARG,
5510 diff_opt_break_rewrites),
Nguyễn Thái Ngọc Duyf4763082019-02-21 18:16:14 +07005511 OPT_CALLBACK_F('M', "find-renames", options, N_("<n>"),
5512 N_("detect renames"),
5513 PARSE_OPT_NONEG | PARSE_OPT_OPTARG,
5514 diff_opt_find_renames),
Nguyễn Thái Ngọc Duy1e533292019-02-21 18:16:15 +07005515 OPT_SET_INT_F('D', "irreversible-delete", &options->irreversible_delete,
5516 N_("omit the preimage for deletes"),
5517 1, PARSE_OPT_NONEG),
Nguyễn Thái Ngọc Duy7f648502019-02-21 18:16:16 +07005518 OPT_CALLBACK_F('C', "find-copies", options, N_("<n>"),
5519 N_("detect copies"),
5520 PARSE_OPT_NONEG | PARSE_OPT_OPTARG,
5521 diff_opt_find_copies),
Nguyễn Thái Ngọc Duybdd47412019-02-21 18:16:17 +07005522 OPT_BOOL(0, "find-copies-harder", &options->flags.find_copies_harder,
5523 N_("use unmodified files as source to find copies")),
Nguyễn Thái Ngọc Duycdc43eb2019-02-21 18:16:18 +07005524 OPT_SET_INT_F(0, "no-renames", &options->detect_rename,
5525 N_("disable rename detection"),
5526 0, PARSE_OPT_NONEG),
5527 OPT_BOOL(0, "rename-empty", &options->flags.rename_empty,
5528 N_("use empty blobs as rename source")),
Nguyễn Thái Ngọc Duy1e9250b2019-03-05 19:30:16 +07005529 OPT_CALLBACK_F(0, "follow", options, NULL,
5530 N_("continue listing the history of a file beyond renames"),
5531 PARSE_OPT_NOARG, diff_opt_follow),
Nguyễn Thái Ngọc Duybffee742019-03-24 15:19:58 +07005532 OPT_INTEGER('l', NULL, &options->rename_limit,
5533 N_("prevent rename/copy detection if the number of rename/copy targets exceeds given limit")),
Nguyễn Thái Ngọc Duyced4e172019-02-21 18:16:13 +07005534
Nguyễn Thái Ngọc Duy2e75f922019-02-21 18:16:20 +07005535 OPT_GROUP(N_("Diff algorithm options")),
5536 OPT_BIT(0, "minimal", &options->xdl_opts,
5537 N_("produce the smallest possible diff"),
5538 XDF_NEED_MINIMAL),
Nguyễn Thái Ngọc Duy87649a12019-02-21 18:16:21 +07005539 OPT_BIT_F('w', "ignore-all-space", &options->xdl_opts,
5540 N_("ignore whitespace when comparing lines"),
5541 XDF_IGNORE_WHITESPACE, PARSE_OPT_NONEG),
5542 OPT_BIT_F('b', "ignore-space-change", &options->xdl_opts,
5543 N_("ignore changes in amount of whitespace"),
5544 XDF_IGNORE_WHITESPACE_CHANGE, PARSE_OPT_NONEG),
5545 OPT_BIT_F(0, "ignore-space-at-eol", &options->xdl_opts,
5546 N_("ignore changes in whitespace at EOL"),
5547 XDF_IGNORE_WHITESPACE_AT_EOL, PARSE_OPT_NONEG),
5548 OPT_BIT_F(0, "ignore-cr-at-eol", &options->xdl_opts,
5549 N_("ignore carrier-return at the end of line"),
5550 XDF_IGNORE_CR_AT_EOL, PARSE_OPT_NONEG),
5551 OPT_BIT_F(0, "ignore-blank-lines", &options->xdl_opts,
5552 N_("ignore changes whose lines are all blank"),
5553 XDF_IGNORE_BLANK_LINES, PARSE_OPT_NONEG),
Michał Kępień296d4a92020-10-20 08:48:09 +02005554 OPT_CALLBACK_F('I', "ignore-matching-lines", options, N_("<regex>"),
5555 N_("ignore changes whose all lines match <regex>"),
5556 0, diff_opt_ignore_regex),
Nguyễn Thái Ngọc Duy06f77512019-03-05 19:30:07 +07005557 OPT_BIT(0, "indent-heuristic", &options->xdl_opts,
5558 N_("heuristic to shift diff hunk boundaries for easy reading"),
5559 XDF_INDENT_HEURISTIC),
Nguyễn Thái Ngọc Duy31fd6402019-03-05 19:30:08 +07005560 OPT_CALLBACK_F(0, "patience", options, NULL,
5561 N_("generate diff using the \"patience diff\" algorithm"),
5562 PARSE_OPT_NONEG | PARSE_OPT_NOARG,
5563 diff_opt_patience),
Nguyễn Thái Ngọc Duyf1e68ef2019-03-05 19:30:09 +07005564 OPT_BITOP(0, "histogram", &options->xdl_opts,
5565 N_("generate diff using the \"histogram diff\" algorithm"),
5566 XDF_HISTOGRAM_DIFF, XDF_DIFF_ALGORITHM_MASK),
Nguyễn Thái Ngọc Duy10f35b12019-03-05 19:30:10 +07005567 OPT_CALLBACK_F(0, "diff-algorithm", options, N_("<algorithm>"),
5568 N_("choose a diff algorithm"),
5569 PARSE_OPT_NONEG, diff_opt_diff_algorithm),
Nguyễn Thái Ngọc Duydf84a432019-03-05 19:30:11 +07005570 OPT_CALLBACK_F(0, "anchored", options, N_("<text>"),
5571 N_("generate diff using the \"anchored diff\" algorithm"),
5572 PARSE_OPT_NONEG, diff_opt_anchored),
Nguyễn Thái Ngọc Duye9fb39b2019-03-05 19:30:18 +07005573 OPT_CALLBACK_F(0, "word-diff", options, N_("<mode>"),
5574 N_("show word diff, using <mode> to delimit changed words"),
5575 PARSE_OPT_NONEG | PARSE_OPT_OPTARG, diff_opt_word_diff),
Nguyễn Thái Ngọc Duy797df112019-03-05 19:30:19 +07005576 OPT_CALLBACK_F(0, "word-diff-regex", options, N_("<regex>"),
5577 N_("use <regex> to decide what a word is"),
5578 PARSE_OPT_NONEG, diff_opt_word_diff_regex),
Nguyễn Thái Ngọc Duy212db692019-03-05 19:30:20 +07005579 OPT_CALLBACK_F(0, "color-words", options, N_("<regex>"),
5580 N_("equivalent to --word-diff=color --word-diff-regex=<regex>"),
5581 PARSE_OPT_NONEG | PARSE_OPT_OPTARG, diff_opt_color_words),
Nguyễn Thái Ngọc Duy59311a92019-03-24 15:20:09 +07005582 OPT_CALLBACK_F(0, "color-moved", options, N_("<mode>"),
Jean-Noël Avila8bcd8f42019-05-17 21:26:19 +02005583 N_("moved lines of code are colored differently"),
Nguyễn Thái Ngọc Duy59311a92019-03-24 15:20:09 +07005584 PARSE_OPT_OPTARG, diff_opt_color_moved),
Nguyễn Thái Ngọc Duy8ce20202019-03-24 15:20:10 +07005585 OPT_CALLBACK_F(0, "color-moved-ws", options, N_("<mode>"),
5586 N_("how white spaces are ignored in --color-moved"),
Nguyễn Thái Ngọc Duybb987292019-03-24 15:20:11 +07005587 0, diff_opt_color_moved_ws),
Nguyễn Thái Ngọc Duy2e75f922019-02-21 18:16:20 +07005588
Jean-Noël Avila8bcd8f42019-05-17 21:26:19 +02005589 OPT_GROUP(N_("Other diff options")),
Nguyễn Thái Ngọc Duy0b1c5b52019-02-21 18:16:19 +07005590 OPT_CALLBACK_F(0, "relative", options, N_("<prefix>"),
5591 N_("when run from subdir, exclude changes outside and show relative paths"),
Laurent Arnoudc28ded82020-05-22 12:46:18 +02005592 PARSE_OPT_OPTARG,
Nguyễn Thái Ngọc Duy0b1c5b52019-02-21 18:16:19 +07005593 diff_opt_relative),
Nguyễn Thái Ngọc Duyc37d4c02019-03-05 19:30:14 +07005594 OPT_BOOL('a', "text", &options->flags.text,
5595 N_("treat all files as text")),
Nguyễn Thái Ngọc Duy4f99f292019-03-05 19:30:15 +07005596 OPT_BOOL('R', NULL, &options->flags.reverse_diff,
5597 N_("swap two inputs, reverse the diff")),
Nguyễn Thái Ngọc Duy1086ea02019-03-05 19:30:21 +07005598 OPT_BOOL(0, "exit-code", &options->flags.exit_with_status,
5599 N_("exit with 1 if there were differences, 0 otherwise")),
Nguyễn Thái Ngọc Duy9bbaf1c2019-03-05 19:30:22 +07005600 OPT_BOOL(0, "quiet", &options->flags.quick,
5601 N_("disable all output of the program")),
Nguyễn Thái Ngọc Duy0cda1002019-03-05 19:30:23 +07005602 OPT_BOOL(0, "ext-diff", &options->flags.allow_external,
5603 N_("allow an external diff helper to be executed")),
Nguyễn Thái Ngọc Duy8ab76972019-03-05 19:30:24 +07005604 OPT_CALLBACK_F(0, "textconv", options, NULL,
5605 N_("run external text conversion filters when comparing binary files"),
5606 PARSE_OPT_NOARG, diff_opt_textconv),
Nguyễn Thái Ngọc Duyb680ee12019-03-05 19:30:25 +07005607 OPT_CALLBACK_F(0, "ignore-submodules", options, N_("<when>"),
5608 N_("ignore changes to submodules in the diff generation"),
5609 PARSE_OPT_NONEG | PARSE_OPT_OPTARG,
5610 diff_opt_ignore_submodules),
Nguyễn Thái Ngọc Duy125dcea2019-03-05 19:30:26 +07005611 OPT_CALLBACK_F(0, "submodule", options, N_("<format>"),
5612 N_("specify how differences in submodules are shown"),
5613 PARSE_OPT_NONEG | PARSE_OPT_OPTARG,
5614 diff_opt_submodule),
Nguyễn Thái Ngọc Duy96860e42019-03-24 15:19:56 +07005615 OPT_SET_INT_F(0, "ita-invisible-in-index", &options->ita_invisible_in_index,
5616 N_("hide 'git add -N' entries from the index"),
5617 1, PARSE_OPT_NONEG),
5618 OPT_SET_INT_F(0, "ita-visible-in-index", &options->ita_invisible_in_index,
5619 N_("treat 'git add -N' entries as real in the index"),
5620 0, PARSE_OPT_NONEG),
Nguyễn Thái Ngọc Duya41cfb32019-03-24 15:19:59 +07005621 OPT_CALLBACK_F('S', NULL, options, N_("<string>"),
5622 N_("look for differences that change the number of occurrences of the specified string"),
5623 0, diff_opt_pickaxe_string),
5624 OPT_CALLBACK_F('G', NULL, options, N_("<regex>"),
5625 N_("look for differences that change the number of occurrences of the specified regex"),
5626 0, diff_opt_pickaxe_regex),
Nguyễn Thái Ngọc Duy85f8e882019-03-24 15:20:00 +07005627 OPT_BIT_F(0, "pickaxe-all", &options->pickaxe_opts,
5628 N_("show all changes in the changeset with -S or -G"),
5629 DIFF_PICKAXE_ALL, PARSE_OPT_NONEG),
5630 OPT_BIT_F(0, "pickaxe-regex", &options->pickaxe_opts,
5631 N_("treat <string> in -S as extended POSIX regular expression"),
5632 DIFF_PICKAXE_REGEX, PARSE_OPT_NONEG),
Nguyễn Thái Ngọc Duyf7318142019-03-24 15:20:01 +07005633 OPT_FILENAME('O', NULL, &options->orderfile,
5634 N_("control the order in which files appear in the output")),
Junio C Hamano1eb41362021-02-11 11:57:50 -08005635 OPT_CALLBACK_F(0, "rotate-to", options, N_("<path>"),
5636 N_("show the change in the specified path first"),
5637 PARSE_OPT_NONEG, diff_opt_rotate_to),
5638 OPT_CALLBACK_F(0, "skip-to", options, N_("<path>"),
5639 N_("skip the output to the specified path"),
5640 PARSE_OPT_NONEG, diff_opt_rotate_to),
Nguyễn Thái Ngọc Duya75f28c2019-03-24 15:20:02 +07005641 OPT_CALLBACK_F(0, "find-object", options, N_("<object-id>"),
5642 N_("look for differences that change the number of occurrences of the specified object"),
5643 PARSE_OPT_NONEG, diff_opt_find_object),
Nguyễn Thái Ngọc Duyd2d3f272019-03-24 15:20:03 +07005644 OPT_CALLBACK_F(0, "diff-filter", options, N_("[(A|C|D|M|R|T|U|X|B)...[*]]"),
5645 N_("select files by diff type"),
5646 PARSE_OPT_NONEG, diff_opt_diff_filter),
Nguyễn Thái Ngọc Duyaf2f3682019-02-21 18:16:12 +07005647 { OPTION_CALLBACK, 0, "output", options, N_("<file>"),
5648 N_("Output to a specific file"),
5649 PARSE_OPT_NONEG, NULL, 0, diff_opt_output },
5650
Nguyễn Thái Ngọc Duy4a288472019-01-27 07:35:31 +07005651 OPT_END()
5652 };
5653
5654 ALLOC_ARRAY(options->parseopts, ARRAY_SIZE(parseopts));
5655 memcpy(options->parseopts, parseopts, sizeof(parseopts));
5656}
5657
Duy Nguyena97262c2016-01-21 18:48:44 +07005658int diff_opt_parse(struct diff_options *options,
5659 const char **av, int ac, const char *prefix)
Junio C Hamano6973dca2006-04-21 23:57:45 -07005660{
Duy Nguyena97262c2016-01-21 18:48:44 +07005661 if (!prefix)
5662 prefix = "";
5663
Nguyễn Thái Ngọc Duy4a288472019-01-27 07:35:31 +07005664 ac = parse_options(ac, av, prefix, options->parseopts, NULL,
5665 PARSE_OPT_KEEP_DASHDASH |
5666 PARSE_OPT_KEEP_UNKNOWN |
5667 PARSE_OPT_NO_INTERNAL_HELP |
5668 PARSE_OPT_ONE_SHOT |
5669 PARSE_OPT_STOP_AT_NON_OPTION);
5670
Nguyễn Thái Ngọc Duy8ce20202019-03-24 15:20:10 +07005671 return ac;
Junio C Hamano6973dca2006-04-21 23:57:45 -07005672}
5673
Kevin Ballard10ae7522010-09-27 16:58:25 -07005674int parse_rename_score(const char **cp_p)
Junio C Hamano6973dca2006-04-21 23:57:45 -07005675{
5676 unsigned long num, scale;
5677 int ch, dot;
5678 const char *cp = *cp_p;
5679
5680 num = 0;
5681 scale = 1;
5682 dot = 0;
Brian Gianforcaroeeefa7c2009-09-01 01:35:10 -04005683 for (;;) {
Junio C Hamano6973dca2006-04-21 23:57:45 -07005684 ch = *cp;
5685 if ( !dot && ch == '.' ) {
5686 scale = 1;
5687 dot = 1;
5688 } else if ( ch == '%' ) {
5689 scale = dot ? scale*100 : 100;
5690 cp++; /* % is always at the end */
5691 break;
5692 } else if ( ch >= '0' && ch <= '9' ) {
5693 if ( scale < 100000 ) {
5694 scale *= 10;
5695 num = (num*10) + (ch-'0');
5696 }
5697 } else {
5698 break;
5699 }
5700 cp++;
5701 }
5702 *cp_p = cp;
5703
5704 /* user says num divided by scale and we say internally that
5705 * is MAX_SCORE * num / scale.
5706 */
Shawn O. Pearcedc49cd72007-03-06 20:44:37 -05005707 return (int)((num >= scale) ? MAX_SCORE : (MAX_SCORE * num / scale));
Junio C Hamano6973dca2006-04-21 23:57:45 -07005708}
5709
Junio C Hamano6973dca2006-04-21 23:57:45 -07005710struct diff_queue_struct diff_queued_diff;
5711
5712void diff_q(struct diff_queue_struct *queue, struct diff_filepair *dp)
5713{
Dmitry S. Dolzhenko4c960a42014-03-04 02:31:53 +04005714 ALLOC_GROW(queue->queue, queue->nr + 1, queue->alloc);
Junio C Hamano6973dca2006-04-21 23:57:45 -07005715 queue->queue[queue->nr++] = dp;
5716}
5717
5718struct diff_filepair *diff_queue(struct diff_queue_struct *queue,
5719 struct diff_filespec *one,
5720 struct diff_filespec *two)
5721{
Junio C Hamanoef677682006-08-03 12:01:01 -07005722 struct diff_filepair *dp = xcalloc(1, sizeof(*dp));
Junio C Hamano6973dca2006-04-21 23:57:45 -07005723 dp->one = one;
5724 dp->two = two;
Junio C Hamano6973dca2006-04-21 23:57:45 -07005725 if (queue)
5726 diff_q(queue, dp);
5727 return dp;
5728}
5729
5730void diff_free_filepair(struct diff_filepair *p)
5731{
Linus Torvalds9fb88412007-10-25 11:19:10 -07005732 free_filespec(p->one);
5733 free_filespec(p->two);
Junio C Hamano6973dca2006-04-21 23:57:45 -07005734 free(p);
5735}
5736
Jeff Kingd6cece52016-10-20 02:20:07 -04005737const char *diff_aligned_abbrev(const struct object_id *oid, int len)
Junio C Hamano6973dca2006-04-21 23:57:45 -07005738{
5739 int abblen;
5740 const char *abbrev;
Junio C Hamano6973dca2006-04-21 23:57:45 -07005741
Ann T Ropea7cb6ac12017-12-03 22:27:42 +01005742 /* Do we want all 40 hex characters? */
brian m. carlson02afca12018-07-16 01:28:05 +00005743 if (len == the_hash_algo->hexsz)
Jeff Kingd6cece52016-10-20 02:20:07 -04005744 return oid_to_hex(oid);
5745
Ann T Ropea7cb6ac12017-12-03 22:27:42 +01005746 /* An abbreviated value is fine, possibly followed by an ellipsis. */
Jeff King4f036662016-10-20 02:21:25 -04005747 abbrev = diff_abbrev_oid(oid, len);
Ann T Ropea7cb6ac12017-12-03 22:27:42 +01005748
5749 if (!print_sha1_ellipsis())
5750 return abbrev;
5751
Junio C Hamano6973dca2006-04-21 23:57:45 -07005752 abblen = strlen(abbrev);
Junio C Hamanod709f1f2016-09-30 10:42:05 -07005753
5754 /*
Ann T Ropea7cb6ac12017-12-03 22:27:42 +01005755 * In well-behaved cases, where the abbreviated result is the
Junio C Hamanod709f1f2016-09-30 10:42:05 -07005756 * same as the requested length, append three dots after the
5757 * abbreviation (hence the whole logic is limited to the case
5758 * where abblen < 37); when the actual abbreviated result is a
5759 * bit longer than the requested length, we reduce the number
5760 * of dots so that they match the well-behaved ones. However,
5761 * if the actual abbreviation is longer than the requested
5762 * length by more than three, we give up on aligning, and add
5763 * three dots anyway, to indicate that the output is not the
5764 * full object name. Yes, this may be suboptimal, but this
5765 * appears only in "diff --raw --abbrev" output and it is not
5766 * worth the effort to change it now. Note that this would
5767 * likely to work fine when the automatic sizing of default
5768 * abbreviation length is used--we would be fed -1 in "len" in
5769 * that case, and will end up always appending three-dots, but
5770 * the automatic sizing is supposed to give abblen that ensures
5771 * uniqueness across all objects (statistically speaking).
5772 */
brian m. carlson02afca12018-07-16 01:28:05 +00005773 if (abblen < the_hash_algo->hexsz - 3) {
brian m. carlsondc015052017-03-26 16:01:24 +00005774 static char hex[GIT_MAX_HEXSZ + 1];
Junio C Hamano6973dca2006-04-21 23:57:45 -07005775 if (len < abblen && abblen <= len + 2)
Jeff King5096d492015-09-24 17:06:08 -04005776 xsnprintf(hex, sizeof(hex), "%s%.*s", abbrev, len+3-abblen, "..");
Junio C Hamano6973dca2006-04-21 23:57:45 -07005777 else
Jeff King5096d492015-09-24 17:06:08 -04005778 xsnprintf(hex, sizeof(hex), "%s...", abbrev);
Junio C Hamano6973dca2006-04-21 23:57:45 -07005779 return hex;
5780 }
Jeff Kingd6cece52016-10-20 02:20:07 -04005781
5782 return oid_to_hex(oid);
Junio C Hamano6973dca2006-04-21 23:57:45 -07005783}
5784
Pierre Habouzit663af342007-09-20 00:42:15 +02005785static void diff_flush_raw(struct diff_filepair *p, struct diff_options *opt)
Junio C Hamano6973dca2006-04-21 23:57:45 -07005786{
Pierre Habouzit663af342007-09-20 00:42:15 +02005787 int line_termination = opt->line_termination;
5788 int inter_name_termination = line_termination ? '\t' : '\0';
Timo Hirvonenc6744342006-06-24 20:21:53 +03005789
John Keeping30997bb2013-02-07 20:15:27 +00005790 fprintf(opt->file, "%s", diff_line_prefix(opt));
Pierre Habouzit663af342007-09-20 00:42:15 +02005791 if (!(opt->output_format & DIFF_FORMAT_NAME_STATUS)) {
Daniel Barkalowc0c77732008-03-09 22:43:39 -04005792 fprintf(opt->file, ":%06o %06o %s ", p->one->mode, p->two->mode,
Jeff Kingd6cece52016-10-20 02:20:07 -04005793 diff_aligned_abbrev(&p->one->oid, opt->abbrev));
brian m. carlsona0d12c42016-06-24 23:09:23 +00005794 fprintf(opt->file, "%s ",
Jeff Kingd6cece52016-10-20 02:20:07 -04005795 diff_aligned_abbrev(&p->two->oid, opt->abbrev));
Pierre Habouzit663af342007-09-20 00:42:15 +02005796 }
5797 if (p->score) {
Daniel Barkalowc0c77732008-03-09 22:43:39 -04005798 fprintf(opt->file, "%c%03d%c", p->status, similarity_index(p),
5799 inter_name_termination);
Pierre Habouzit663af342007-09-20 00:42:15 +02005800 } else {
Daniel Barkalowc0c77732008-03-09 22:43:39 -04005801 fprintf(opt->file, "%c%c", p->status, inter_name_termination);
Junio C Hamano6973dca2006-04-21 23:57:45 -07005802 }
5803
Junio C Hamanocd676a52008-02-12 14:26:02 -08005804 if (p->status == DIFF_STATUS_COPIED ||
5805 p->status == DIFF_STATUS_RENAMED) {
5806 const char *name_a, *name_b;
5807 name_a = p->one->path;
5808 name_b = p->two->path;
5809 strip_prefix(opt->prefix_length, &name_a, &name_b);
Daniel Barkalowc0c77732008-03-09 22:43:39 -04005810 write_name_quoted(name_a, opt->file, inter_name_termination);
5811 write_name_quoted(name_b, opt->file, line_termination);
Pierre Habouzit663af342007-09-20 00:42:15 +02005812 } else {
Junio C Hamanocd676a52008-02-12 14:26:02 -08005813 const char *name_a, *name_b;
5814 name_a = p->one->mode ? p->one->path : p->two->path;
5815 name_b = NULL;
5816 strip_prefix(opt->prefix_length, &name_a, &name_b);
Daniel Barkalowc0c77732008-03-09 22:43:39 -04005817 write_name_quoted(name_a, opt->file, line_termination);
Junio C Hamano6973dca2006-04-21 23:57:45 -07005818 }
Junio C Hamano6973dca2006-04-21 23:57:45 -07005819}
5820
5821int diff_unmodified_pair(struct diff_filepair *p)
5822{
5823 /* This function is written stricter than necessary to support
5824 * the currently implemented transformers, but the idea is to
5825 * let transformers to produce diff_filepairs any way they want,
5826 * and filter and clean them up here before producing the output.
5827 */
Pierre Habouzit663af342007-09-20 00:42:15 +02005828 struct diff_filespec *one = p->one, *two = p->two;
Junio C Hamano6973dca2006-04-21 23:57:45 -07005829
5830 if (DIFF_PAIR_UNMERGED(p))
5831 return 0; /* unmerged is interesting */
5832
Junio C Hamano6973dca2006-04-21 23:57:45 -07005833 /* deletion, addition, mode or type change
5834 * and rename are all interesting.
5835 */
5836 if (DIFF_FILE_VALID(one) != DIFF_FILE_VALID(two) ||
5837 DIFF_PAIR_MODE_CHANGED(p) ||
5838 strcmp(one->path, two->path))
5839 return 0;
5840
5841 /* both are valid and point at the same path. that is, we are
5842 * dealing with a change.
5843 */
brian m. carlson41c95602016-06-24 23:09:24 +00005844 if (one->oid_valid && two->oid_valid &&
Jeff King4a7e27e2018-08-28 17:22:40 -04005845 oideq(&one->oid, &two->oid) &&
Jens Lehmann85adbf22010-03-12 22:23:52 +01005846 !one->dirty_submodule && !two->dirty_submodule)
Junio C Hamano6973dca2006-04-21 23:57:45 -07005847 return 1; /* no change */
brian m. carlson41c95602016-06-24 23:09:24 +00005848 if (!one->oid_valid && !two->oid_valid)
Junio C Hamano6973dca2006-04-21 23:57:45 -07005849 return 1; /* both look at the same file on the filesystem. */
5850 return 0;
5851}
5852
5853static void diff_flush_patch(struct diff_filepair *p, struct diff_options *o)
5854{
5855 if (diff_unmodified_pair(p))
5856 return;
5857
5858 if ((DIFF_FILE_VALID(p->one) && S_ISDIR(p->one->mode)) ||
5859 (DIFF_FILE_VALID(p->two) && S_ISDIR(p->two->mode)))
5860 return; /* no tree diffs in patch format */
5861
5862 run_diff(p, o);
5863}
5864
5865static void diff_flush_stat(struct diff_filepair *p, struct diff_options *o,
5866 struct diffstat_t *diffstat)
5867{
5868 if (diff_unmodified_pair(p))
5869 return;
5870
5871 if ((DIFF_FILE_VALID(p->one) && S_ISDIR(p->one->mode)) ||
5872 (DIFF_FILE_VALID(p->two) && S_ISDIR(p->two->mode)))
Yann Dirsonc3fced62010-10-24 23:03:10 +02005873 return; /* no useful stat for tree diffs */
Junio C Hamano6973dca2006-04-21 23:57:45 -07005874
5875 run_diffstat(p, o, diffstat);
5876}
5877
Johannes Schindelin88246892006-05-20 23:43:13 +02005878static void diff_flush_checkdiff(struct diff_filepair *p,
5879 struct diff_options *o)
5880{
5881 if (diff_unmodified_pair(p))
5882 return;
5883
5884 if ((DIFF_FILE_VALID(p->one) && S_ISDIR(p->one->mode)) ||
5885 (DIFF_FILE_VALID(p->two) && S_ISDIR(p->two->mode)))
Yann Dirsonc3fced62010-10-24 23:03:10 +02005886 return; /* nothing to check in tree diffs */
Johannes Schindelin88246892006-05-20 23:43:13 +02005887
5888 run_checkdiff(p, o);
5889}
5890
Junio C Hamano6973dca2006-04-21 23:57:45 -07005891int diff_queue_is_empty(void)
5892{
5893 struct diff_queue_struct *q = &diff_queued_diff;
5894 int i;
5895 for (i = 0; i < q->nr; i++)
5896 if (!diff_unmodified_pair(q->queue[i]))
5897 return 0;
5898 return 1;
5899}
5900
5901#if DIFF_DEBUG
5902void diff_debug_filespec(struct diff_filespec *s, int x, const char *one)
5903{
5904 fprintf(stderr, "queue[%d] %s (%s) %s %06o %s\n",
5905 x, one ? one : "",
5906 s->path,
5907 DIFF_FILE_VALID(s) ? "valid" : "invalid",
5908 s->mode,
brian m. carlson41c95602016-06-24 23:09:24 +00005909 s->oid_valid ? oid_to_hex(&s->oid) : "");
Jeff King428d52a2014-01-16 20:21:59 -05005910 fprintf(stderr, "queue[%d] %s size %lu\n",
Junio C Hamano6973dca2006-04-21 23:57:45 -07005911 x, one ? one : "",
Jeff King428d52a2014-01-16 20:21:59 -05005912 s->size);
Junio C Hamano6973dca2006-04-21 23:57:45 -07005913}
5914
5915void diff_debug_filepair(const struct diff_filepair *p, int i)
5916{
5917 diff_debug_filespec(p->one, i, "one");
5918 diff_debug_filespec(p->two, i, "two");
Linus Torvalds64479712007-10-25 11:20:56 -07005919 fprintf(stderr, "score %d, status %c rename_used %d broken %d\n",
Junio C Hamano6973dca2006-04-21 23:57:45 -07005920 p->score, p->status ? p->status : '?',
Linus Torvalds64479712007-10-25 11:20:56 -07005921 p->one->rename_used, p->broken_pair);
Junio C Hamano6973dca2006-04-21 23:57:45 -07005922}
5923
5924void diff_debug_queue(const char *msg, struct diff_queue_struct *q)
5925{
5926 int i;
5927 if (msg)
5928 fprintf(stderr, "%s\n", msg);
5929 fprintf(stderr, "q->nr = %d\n", q->nr);
5930 for (i = 0; i < q->nr; i++) {
5931 struct diff_filepair *p = q->queue[i];
5932 diff_debug_filepair(p, i);
5933 }
5934}
5935#endif
5936
5937static void diff_resolve_rename_copy(void)
5938{
Linus Torvalds64479712007-10-25 11:20:56 -07005939 int i;
5940 struct diff_filepair *p;
Junio C Hamano6973dca2006-04-21 23:57:45 -07005941 struct diff_queue_struct *q = &diff_queued_diff;
5942
5943 diff_debug_queue("resolve-rename-copy", q);
5944
5945 for (i = 0; i < q->nr; i++) {
5946 p = q->queue[i];
5947 p->status = 0; /* undecided */
5948 if (DIFF_PAIR_UNMERGED(p))
5949 p->status = DIFF_STATUS_UNMERGED;
5950 else if (!DIFF_FILE_VALID(p->one))
5951 p->status = DIFF_STATUS_ADDED;
5952 else if (!DIFF_FILE_VALID(p->two))
5953 p->status = DIFF_STATUS_DELETED;
5954 else if (DIFF_PAIR_TYPE_CHANGED(p))
5955 p->status = DIFF_STATUS_TYPE_CHANGED;
5956
5957 /* from this point on, we are dealing with a pair
5958 * whose both sides are valid and of the same type, i.e.
5959 * either in-place edit or rename/copy edit.
5960 */
5961 else if (DIFF_PAIR_RENAME(p)) {
Linus Torvalds64479712007-10-25 11:20:56 -07005962 /*
5963 * A rename might have re-connected a broken
5964 * pair up, causing the pathnames to be the
5965 * same again. If so, that's not a rename at
5966 * all, just a modification..
5967 *
5968 * Otherwise, see if this source was used for
5969 * multiple renames, in which case we decrement
5970 * the count, and call it a copy.
Junio C Hamano6973dca2006-04-21 23:57:45 -07005971 */
Linus Torvalds64479712007-10-25 11:20:56 -07005972 if (!strcmp(p->one->path, p->two->path))
5973 p->status = DIFF_STATUS_MODIFIED;
5974 else if (--p->one->rename_used > 0)
Junio C Hamano6973dca2006-04-21 23:57:45 -07005975 p->status = DIFF_STATUS_COPIED;
Linus Torvalds64479712007-10-25 11:20:56 -07005976 else
Junio C Hamano6973dca2006-04-21 23:57:45 -07005977 p->status = DIFF_STATUS_RENAMED;
5978 }
Jeff King9001dc22018-08-28 17:22:48 -04005979 else if (!oideq(&p->one->oid, &p->two->oid) ||
Johannes Schindelind516c2d2007-02-22 21:50:10 +01005980 p->one->mode != p->two->mode ||
Jens Lehmann85adbf22010-03-12 22:23:52 +01005981 p->one->dirty_submodule ||
5982 p->two->dirty_submodule ||
brian m. carlsona0d12c42016-06-24 23:09:23 +00005983 is_null_oid(&p->one->oid))
Junio C Hamano6973dca2006-04-21 23:57:45 -07005984 p->status = DIFF_STATUS_MODIFIED;
5985 else {
5986 /* This is a "no-change" entry and should not
5987 * happen anymore, but prepare for broken callers.
5988 */
5989 error("feeding unmodified %s to diffcore",
5990 p->one->path);
5991 p->status = DIFF_STATUS_UNKNOWN;
5992 }
5993 }
5994 diff_debug_queue("resolve-rename-copy done", q);
5995}
5996
Timo Hirvonenc6744342006-06-24 20:21:53 +03005997static int check_pair_status(struct diff_filepair *p)
Junio C Hamano6973dca2006-04-21 23:57:45 -07005998{
Junio C Hamano6973dca2006-04-21 23:57:45 -07005999 switch (p->status) {
6000 case DIFF_STATUS_UNKNOWN:
Timo Hirvonenc6744342006-06-24 20:21:53 +03006001 return 0;
Junio C Hamano6973dca2006-04-21 23:57:45 -07006002 case 0:
6003 die("internal error in diff-resolve-rename-copy");
Junio C Hamano6973dca2006-04-21 23:57:45 -07006004 default:
Timo Hirvonenc6744342006-06-24 20:21:53 +03006005 return 1;
Junio C Hamano6973dca2006-04-21 23:57:45 -07006006 }
6007}
6008
Timo Hirvonenc6744342006-06-24 20:21:53 +03006009static void flush_one_pair(struct diff_filepair *p, struct diff_options *opt)
6010{
6011 int fmt = opt->output_format;
6012
6013 if (fmt & DIFF_FORMAT_CHECKDIFF)
6014 diff_flush_checkdiff(p, opt);
6015 else if (fmt & (DIFF_FORMAT_RAW | DIFF_FORMAT_NAME_STATUS))
6016 diff_flush_raw(p, opt);
Junio C Hamanocd676a52008-02-12 14:26:02 -08006017 else if (fmt & DIFF_FORMAT_NAME) {
6018 const char *name_a, *name_b;
6019 name_a = p->two->path;
6020 name_b = NULL;
6021 strip_prefix(opt->prefix_length, &name_a, &name_b);
Jeff Kingf5022b52017-02-08 15:31:15 -05006022 fprintf(opt->file, "%s", diff_line_prefix(opt));
Daniel Barkalowc0c77732008-03-09 22:43:39 -04006023 write_name_quoted(name_a, opt->file, opt->line_termination);
Junio C Hamanocd676a52008-02-12 14:26:02 -08006024 }
Timo Hirvonenc6744342006-06-24 20:21:53 +03006025}
6026
Stefan Beller146fdb02017-06-29 17:07:05 -07006027static void show_file_mode_name(struct diff_options *opt, const char *newdelete, struct diff_filespec *fs)
Sean4bbd2612006-05-14 08:13:49 -04006028{
Stefan Beller146fdb02017-06-29 17:07:05 -07006029 struct strbuf sb = STRBUF_INIT;
Sean4bbd2612006-05-14 08:13:49 -04006030 if (fs->mode)
Stefan Beller146fdb02017-06-29 17:07:05 -07006031 strbuf_addf(&sb, " %s mode %06o ", newdelete, fs->mode);
Sean4bbd2612006-05-14 08:13:49 -04006032 else
Stefan Beller146fdb02017-06-29 17:07:05 -07006033 strbuf_addf(&sb, " %s ", newdelete);
6034
6035 quote_c_style(fs->path, &sb, NULL, 0);
6036 strbuf_addch(&sb, '\n');
6037 emit_diff_symbol(opt, DIFF_SYMBOL_SUMMARY,
6038 sb.buf, sb.len, 0);
6039 strbuf_release(&sb);
Sean4bbd2612006-05-14 08:13:49 -04006040}
6041
Stefan Beller146fdb02017-06-29 17:07:05 -07006042static void show_mode_change(struct diff_options *opt, struct diff_filepair *p,
6043 int show_name)
Sean4bbd2612006-05-14 08:13:49 -04006044{
6045 if (p->one->mode && p->two->mode && p->one->mode != p->two->mode) {
Stefan Beller146fdb02017-06-29 17:07:05 -07006046 struct strbuf sb = STRBUF_INIT;
6047 strbuf_addf(&sb, " mode change %06o => %06o",
6048 p->one->mode, p->two->mode);
Alexandre Julliard0d26a642007-02-10 15:37:48 +01006049 if (show_name) {
Stefan Beller146fdb02017-06-29 17:07:05 -07006050 strbuf_addch(&sb, ' ');
6051 quote_c_style(p->two->path, &sb, NULL, 0);
Alexandre Julliard0d26a642007-02-10 15:37:48 +01006052 }
Stefan Beller58aaced2017-09-27 15:51:26 -07006053 strbuf_addch(&sb, '\n');
Stefan Beller146fdb02017-06-29 17:07:05 -07006054 emit_diff_symbol(opt, DIFF_SYMBOL_SUMMARY,
6055 sb.buf, sb.len, 0);
6056 strbuf_release(&sb);
Sean4bbd2612006-05-14 08:13:49 -04006057 }
6058}
6059
Stefan Beller146fdb02017-06-29 17:07:05 -07006060static void show_rename_copy(struct diff_options *opt, const char *renamecopy,
6061 struct diff_filepair *p)
Sean4bbd2612006-05-14 08:13:49 -04006062{
Stefan Beller146fdb02017-06-29 17:07:05 -07006063 struct strbuf sb = STRBUF_INIT;
Nguyễn Thái Ngọc Duyc905cbc2018-02-01 20:02:20 +07006064 struct strbuf names = STRBUF_INIT;
6065
6066 pprint_rename(&names, p->one->path, p->two->path);
Stefan Beller146fdb02017-06-29 17:07:05 -07006067 strbuf_addf(&sb, " %s %s (%d%%)\n",
Nguyễn Thái Ngọc Duyc905cbc2018-02-01 20:02:20 +07006068 renamecopy, names.buf, similarity_index(p));
6069 strbuf_release(&names);
Stefan Beller146fdb02017-06-29 17:07:05 -07006070 emit_diff_symbol(opt, DIFF_SYMBOL_SUMMARY,
6071 sb.buf, sb.len, 0);
6072 show_mode_change(opt, p, 0);
Rene Scharfe348eda22017-08-30 19:49:42 +02006073 strbuf_release(&sb);
Sean4bbd2612006-05-14 08:13:49 -04006074}
6075
Bo Yang7be57612010-05-26 15:23:54 +08006076static void diff_summary(struct diff_options *opt, struct diff_filepair *p)
Sean4bbd2612006-05-14 08:13:49 -04006077{
6078 switch(p->status) {
6079 case DIFF_STATUS_DELETED:
Stefan Beller146fdb02017-06-29 17:07:05 -07006080 show_file_mode_name(opt, "delete", p->one);
Sean4bbd2612006-05-14 08:13:49 -04006081 break;
6082 case DIFF_STATUS_ADDED:
Stefan Beller146fdb02017-06-29 17:07:05 -07006083 show_file_mode_name(opt, "create", p->two);
Sean4bbd2612006-05-14 08:13:49 -04006084 break;
6085 case DIFF_STATUS_COPIED:
Stefan Beller146fdb02017-06-29 17:07:05 -07006086 show_rename_copy(opt, "copy", p);
Sean4bbd2612006-05-14 08:13:49 -04006087 break;
6088 case DIFF_STATUS_RENAMED:
Stefan Beller146fdb02017-06-29 17:07:05 -07006089 show_rename_copy(opt, "rename", p);
Sean4bbd2612006-05-14 08:13:49 -04006090 break;
6091 default:
6092 if (p->score) {
Stefan Beller146fdb02017-06-29 17:07:05 -07006093 struct strbuf sb = STRBUF_INIT;
6094 strbuf_addstr(&sb, " rewrite ");
6095 quote_c_style(p->two->path, &sb, NULL, 0);
6096 strbuf_addf(&sb, " (%d%%)\n", similarity_index(p));
6097 emit_diff_symbol(opt, DIFF_SYMBOL_SUMMARY,
6098 sb.buf, sb.len, 0);
Rene Scharfefa842d82017-08-30 19:49:41 +02006099 strbuf_release(&sb);
Pierre Habouzit663af342007-09-20 00:42:15 +02006100 }
Stefan Beller146fdb02017-06-29 17:07:05 -07006101 show_mode_change(opt, p, !p->score);
Sean4bbd2612006-05-14 08:13:49 -04006102 break;
6103 }
6104}
6105
Johannes Schindelinfcb3d0a2006-06-25 03:51:08 +02006106struct patch_id_t {
brian m. carlson36261e42019-08-18 20:04:03 +00006107 git_hash_ctx *ctx;
Johannes Schindelinfcb3d0a2006-06-25 03:51:08 +02006108 int patchlen;
6109};
6110
6111static int remove_space(char *line, int len)
6112{
6113 int i;
Pierre Habouzit663af342007-09-20 00:42:15 +02006114 char *dst = line;
6115 unsigned char c;
Johannes Schindelinfcb3d0a2006-06-25 03:51:08 +02006116
Pierre Habouzit663af342007-09-20 00:42:15 +02006117 for (i = 0; i < len; i++)
6118 if (!isspace((c = line[i])))
6119 *dst++ = c;
Johannes Schindelinfcb3d0a2006-06-25 03:51:08 +02006120
Pierre Habouzit663af342007-09-20 00:42:15 +02006121 return dst - line;
Johannes Schindelinfcb3d0a2006-06-25 03:51:08 +02006122}
6123
brian m. carlson36261e42019-08-18 20:04:03 +00006124void flush_one_hunk(struct object_id *result, git_hash_ctx *ctx)
Stephen Boyda8f68552019-04-26 16:51:57 -07006125{
6126 unsigned char hash[GIT_MAX_RAWSZ];
6127 unsigned short carry = 0;
6128 int i;
6129
brian m. carlson36261e42019-08-18 20:04:03 +00006130 the_hash_algo->final_fn(hash, ctx);
6131 the_hash_algo->init_fn(ctx);
Stephen Boyda8f68552019-04-26 16:51:57 -07006132 /* 20-byte sum, with carry */
brian m. carlson36261e42019-08-18 20:04:03 +00006133 for (i = 0; i < the_hash_algo->rawsz; ++i) {
Stephen Boyda8f68552019-04-26 16:51:57 -07006134 carry += result->hash[i] + hash[i];
6135 result->hash[i] = carry;
6136 carry >>= 8;
6137 }
6138}
6139
Ævar Arnfjörð Bjarmasona8d5eb62021-04-12 19:15:24 +02006140static int patch_id_consume(void *priv, char *line, unsigned long len)
Johannes Schindelinfcb3d0a2006-06-25 03:51:08 +02006141{
6142 struct patch_id_t *data = priv;
6143 int new_len;
6144
René Scharfe82a62012020-08-19 00:08:54 +02006145 if (len > 12 && starts_with(line, "\\ "))
Ævar Arnfjörð Bjarmasona8d5eb62021-04-12 19:15:24 +02006146 return 0;
Johannes Schindelinfcb3d0a2006-06-25 03:51:08 +02006147 new_len = remove_space(line, len);
6148
brian m. carlson36261e42019-08-18 20:04:03 +00006149 the_hash_algo->update_fn(data->ctx, line, new_len);
Johannes Schindelinfcb3d0a2006-06-25 03:51:08 +02006150 data->patchlen += new_len;
Ævar Arnfjörð Bjarmasona8d5eb62021-04-12 19:15:24 +02006151 return 0;
Johannes Schindelinfcb3d0a2006-06-25 03:51:08 +02006152}
6153
brian m. carlson36261e42019-08-18 20:04:03 +00006154static void patch_id_add_string(git_hash_ctx *ctx, const char *str)
Jeff King977db6b2017-03-30 14:26:05 -04006155{
brian m. carlson36261e42019-08-18 20:04:03 +00006156 the_hash_algo->update_fn(ctx, str, strlen(str));
Jeff King977db6b2017-03-30 14:26:05 -04006157}
6158
brian m. carlson36261e42019-08-18 20:04:03 +00006159static void patch_id_add_mode(git_hash_ctx *ctx, unsigned mode)
Jeff King977db6b2017-03-30 14:26:05 -04006160{
6161 /* large enough for 2^32 in octal */
6162 char buf[12];
6163 int len = xsnprintf(buf, sizeof(buf), "%06o", mode);
brian m. carlson36261e42019-08-18 20:04:03 +00006164 the_hash_algo->update_fn(ctx, buf, len);
Jeff King977db6b2017-03-30 14:26:05 -04006165}
6166
Stephen Boyda8f68552019-04-26 16:51:57 -07006167/* returns 0 upon success, and writes result into oid */
6168static int diff_get_patch_id(struct diff_options *options, struct object_id *oid, int diff_header_only, int stable)
Johannes Schindelinfcb3d0a2006-06-25 03:51:08 +02006169{
6170 struct diff_queue_struct *q = &diff_queued_diff;
6171 int i;
brian m. carlson36261e42019-08-18 20:04:03 +00006172 git_hash_ctx ctx;
Johannes Schindelinfcb3d0a2006-06-25 03:51:08 +02006173 struct patch_id_t data;
Johannes Schindelinfcb3d0a2006-06-25 03:51:08 +02006174
brian m. carlson36261e42019-08-18 20:04:03 +00006175 the_hash_algo->init_fn(&ctx);
Johannes Schindelinfcb3d0a2006-06-25 03:51:08 +02006176 memset(&data, 0, sizeof(struct patch_id_t));
6177 data.ctx = &ctx;
Stephen Boyda8f68552019-04-26 16:51:57 -07006178 oidclr(oid);
Johannes Schindelinfcb3d0a2006-06-25 03:51:08 +02006179
6180 for (i = 0; i < q->nr; i++) {
6181 xpparam_t xpp;
6182 xdemitconf_t xecfg;
Johannes Schindelinfcb3d0a2006-06-25 03:51:08 +02006183 mmfile_t mf1, mf2;
6184 struct diff_filepair *p = q->queue[i];
6185 int len1, len2;
6186
Brian Downing9ccd0a82008-10-25 15:30:37 +02006187 memset(&xpp, 0, sizeof(xpp));
Johannes Schindelin30b25012007-07-04 19:05:46 +01006188 memset(&xecfg, 0, sizeof(xecfg));
Johannes Schindelinfcb3d0a2006-06-25 03:51:08 +02006189 if (p->status == 0)
6190 return error("internal diff status error");
6191 if (p->status == DIFF_STATUS_UNKNOWN)
6192 continue;
6193 if (diff_unmodified_pair(p))
6194 continue;
6195 if ((DIFF_FILE_VALID(p->one) && S_ISDIR(p->one->mode)) ||
6196 (DIFF_FILE_VALID(p->two) && S_ISDIR(p->two->mode)))
6197 continue;
6198 if (DIFF_PAIR_UNMERGED(p))
6199 continue;
6200
Nguyễn Thái Ngọc Duy58bf2a42018-09-21 17:57:31 +02006201 diff_fill_oid_info(p->one, options->repo->index);
6202 diff_fill_oid_info(p->two, options->repo->index);
Johannes Schindelinfcb3d0a2006-06-25 03:51:08 +02006203
Johannes Schindelinfcb3d0a2006-06-25 03:51:08 +02006204 len1 = remove_space(p->one->path, strlen(p->one->path));
6205 len2 = remove_space(p->two->path, strlen(p->two->path));
Jeff King977db6b2017-03-30 14:26:05 -04006206 patch_id_add_string(&ctx, "diff--git");
6207 patch_id_add_string(&ctx, "a/");
brian m. carlson36261e42019-08-18 20:04:03 +00006208 the_hash_algo->update_fn(&ctx, p->one->path, len1);
Jeff King977db6b2017-03-30 14:26:05 -04006209 patch_id_add_string(&ctx, "b/");
brian m. carlson36261e42019-08-18 20:04:03 +00006210 the_hash_algo->update_fn(&ctx, p->two->path, len2);
Jeff King977db6b2017-03-30 14:26:05 -04006211
6212 if (p->one->mode == 0) {
6213 patch_id_add_string(&ctx, "newfilemode");
6214 patch_id_add_mode(&ctx, p->two->mode);
6215 patch_id_add_string(&ctx, "---/dev/null");
6216 patch_id_add_string(&ctx, "+++b/");
brian m. carlson36261e42019-08-18 20:04:03 +00006217 the_hash_algo->update_fn(&ctx, p->two->path, len2);
Jeff King977db6b2017-03-30 14:26:05 -04006218 } else if (p->two->mode == 0) {
6219 patch_id_add_string(&ctx, "deletedfilemode");
6220 patch_id_add_mode(&ctx, p->one->mode);
6221 patch_id_add_string(&ctx, "---a/");
brian m. carlson36261e42019-08-18 20:04:03 +00006222 the_hash_algo->update_fn(&ctx, p->one->path, len1);
Jeff King977db6b2017-03-30 14:26:05 -04006223 patch_id_add_string(&ctx, "+++/dev/null");
6224 } else {
6225 patch_id_add_string(&ctx, "---a/");
brian m. carlson36261e42019-08-18 20:04:03 +00006226 the_hash_algo->update_fn(&ctx, p->one->path, len1);
Jeff King977db6b2017-03-30 14:26:05 -04006227 patch_id_add_string(&ctx, "+++b/");
brian m. carlson36261e42019-08-18 20:04:03 +00006228 the_hash_algo->update_fn(&ctx, p->two->path, len2);
Jeff King977db6b2017-03-30 14:26:05 -04006229 }
Johannes Schindelinfcb3d0a2006-06-25 03:51:08 +02006230
Kevin Willford3e8e32c2016-07-29 12:19:19 -04006231 if (diff_header_only)
6232 continue;
6233
Nguyễn Thái Ngọc Duyb78ea5f2018-09-21 17:57:19 +02006234 if (fill_mmfile(options->repo, &mf1, p->one) < 0 ||
6235 fill_mmfile(options->repo, &mf2, p->two) < 0)
Kevin Willford3e8e32c2016-07-29 12:19:19 -04006236 return error("unable to read files to diff");
6237
Nguyễn Thái Ngọc Duyb78ea5f2018-09-21 17:57:19 +02006238 if (diff_filespec_is_binary(options->repo, p->one) ||
6239 diff_filespec_is_binary(options->repo, p->two)) {
brian m. carlson36261e42019-08-18 20:04:03 +00006240 the_hash_algo->update_fn(&ctx, oid_to_hex(&p->one->oid),
6241 the_hash_algo->hexsz);
6242 the_hash_algo->update_fn(&ctx, oid_to_hex(&p->two->oid),
6243 the_hash_algo->hexsz);
Clemens Buchacher34597c12010-08-15 09:20:43 +02006244 continue;
6245 }
6246
René Scharfe582aa002010-05-02 15:04:41 +02006247 xpp.flags = 0;
Johannes Schindelinfcb3d0a2006-06-25 03:51:08 +02006248 xecfg.ctxlen = 3;
Ævar Arnfjörð Bjarmason5d934602021-04-12 19:15:29 +02006249 xecfg.flags = XDL_EMIT_NO_HUNK_HDR;
6250 if (xdi_diff_outf(&mf1, &mf2, NULL,
Jeff Kingb1357392018-11-02 02:36:36 -04006251 patch_id_consume, &data, &xpp, &xecfg))
Jeff King3efb9882015-09-24 19:12:23 -04006252 return error("unable to generate patch-id diff for %s",
6253 p->one->path);
Stephen Boyda8f68552019-04-26 16:51:57 -07006254
6255 if (stable)
6256 flush_one_hunk(oid, &ctx);
Johannes Schindelinfcb3d0a2006-06-25 03:51:08 +02006257 }
6258
Stephen Boyda8f68552019-04-26 16:51:57 -07006259 if (!stable)
brian m. carlson5951bf42021-04-26 01:02:53 +00006260 the_hash_algo->final_oid_fn(oid, &ctx);
Stephen Boyda8f68552019-04-26 16:51:57 -07006261
Johannes Schindelinfcb3d0a2006-06-25 03:51:08 +02006262 return 0;
6263}
6264
Stephen Boyda8f68552019-04-26 16:51:57 -07006265int diff_flush_patch_id(struct diff_options *options, struct object_id *oid, int diff_header_only, int stable)
Johannes Schindelinfcb3d0a2006-06-25 03:51:08 +02006266{
6267 struct diff_queue_struct *q = &diff_queued_diff;
6268 int i;
Stephen Boyda8f68552019-04-26 16:51:57 -07006269 int result = diff_get_patch_id(options, oid, diff_header_only, stable);
Johannes Schindelinfcb3d0a2006-06-25 03:51:08 +02006270
6271 for (i = 0; i < q->nr; i++)
6272 diff_free_filepair(q->queue[i]);
6273
6274 free(q->queue);
Bo Yang9ca5df92010-05-06 21:52:27 -07006275 DIFF_QUEUE_CLEAR(q);
Johannes Schindelinfcb3d0a2006-06-25 03:51:08 +02006276
6277 return result;
6278}
6279
Timo Hirvonen946c3782006-06-27 15:09:17 +03006280static int is_summary_empty(const struct diff_queue_struct *q)
6281{
6282 int i;
6283
6284 for (i = 0; i < q->nr; i++) {
6285 const struct diff_filepair *p = q->queue[i];
6286
6287 switch (p->status) {
6288 case DIFF_STATUS_DELETED:
6289 case DIFF_STATUS_ADDED:
6290 case DIFF_STATUS_COPIED:
6291 case DIFF_STATUS_RENAMED:
6292 return 0;
6293 default:
6294 if (p->score)
6295 return 0;
6296 if (p->one->mode && p->two->mode &&
6297 p->one->mode != p->two->mode)
6298 return 0;
6299 break;
6300 }
6301 }
6302 return 1;
6303}
6304
Junio C Hamanof31027c2011-01-06 13:50:06 -08006305static const char rename_limit_warning[] =
Elijah Newren05d2c612021-07-15 00:45:21 +00006306N_("exhaustive rename detection was skipped due to too many files.");
Junio C Hamanof31027c2011-01-06 13:50:06 -08006307
6308static const char degrade_cc_to_c_warning[] =
Vasco Almeidadb424972016-10-17 13:15:29 +00006309N_("only found copies from modified paths due to too many files.");
Junio C Hamanof31027c2011-01-06 13:50:06 -08006310
6311static const char rename_limit_advice[] =
Vasco Almeidadb424972016-10-17 13:15:29 +00006312N_("you may want to set your %s variable to at least "
6313 "%d and retry the command.");
Junio C Hamanof31027c2011-01-06 13:50:06 -08006314
6315void diff_warn_rename_limit(const char *varname, int needed, int degraded_cc)
6316{
Nguyễn Thái Ngọc Duy4e056c92018-01-16 16:23:49 +07006317 fflush(stdout);
Junio C Hamanof31027c2011-01-06 13:50:06 -08006318 if (degraded_cc)
Vasco Almeidadb424972016-10-17 13:15:29 +00006319 warning(_(degrade_cc_to_c_warning));
Junio C Hamanof31027c2011-01-06 13:50:06 -08006320 else if (needed)
Vasco Almeidadb424972016-10-17 13:15:29 +00006321 warning(_(rename_limit_warning));
Junio C Hamanof31027c2011-01-06 13:50:06 -08006322 else
6323 return;
Elijah Newren9f7e4bf2017-11-13 12:15:59 -08006324 if (0 < needed)
Vasco Almeidadb424972016-10-17 13:15:29 +00006325 warning(_(rename_limit_advice), varname, needed);
Junio C Hamanof31027c2011-01-06 13:50:06 -08006326}
6327
Stefan Bellerec331502017-06-29 17:06:48 -07006328static void diff_flush_patch_all_file_pairs(struct diff_options *o)
6329{
6330 int i;
Stefan Bellere6e045f2017-06-29 17:07:06 -07006331 static struct emitted_diff_symbols esm = EMITTED_DIFF_SYMBOLS_INIT;
Stefan Bellerec331502017-06-29 17:06:48 -07006332 struct diff_queue_struct *q = &diff_queued_diff;
Stefan Beller091f8e22017-06-29 17:06:53 -07006333
6334 if (WSEH_NEW & WS_RULE_MASK)
Johannes Schindelin033abf92018-05-02 11:38:39 +02006335 BUG("WS rules bit mask overlaps with diff symbol flags");
Stefan Beller091f8e22017-06-29 17:06:53 -07006336
Stefan Beller2e2d5ac2017-06-30 13:53:07 -07006337 if (o->color_moved)
6338 o->emitted_symbols = &esm;
Stefan Bellere6e045f2017-06-29 17:07:06 -07006339
Stefan Bellerec331502017-06-29 17:06:48 -07006340 for (i = 0; i < q->nr; i++) {
6341 struct diff_filepair *p = q->queue[i];
6342 if (check_pair_status(p))
6343 diff_flush_patch(p, o);
6344 }
Stefan Bellere6e045f2017-06-29 17:07:06 -07006345
6346 if (o->emitted_symbols) {
Stefan Beller2e2d5ac2017-06-30 13:53:07 -07006347 if (o->color_moved) {
6348 struct hashmap add_lines, del_lines;
6349
Stefan Bellerca1f4ae2018-07-18 12:31:55 -07006350 if (o->color_moved_ws_handling &
6351 COLOR_MOVED_WS_ALLOW_INDENTATION_CHANGE)
6352 o->color_moved_ws_handling |= XDF_IGNORE_WHITESPACE;
6353
Stefan Belleree1df662018-07-16 16:05:38 -07006354 hashmap_init(&del_lines, moved_entry_cmp, o, 0);
6355 hashmap_init(&add_lines, moved_entry_cmp, o, 0);
Stefan Beller2e2d5ac2017-06-30 13:53:07 -07006356
6357 add_lines_to_move_detection(o, &add_lines, &del_lines);
6358 mark_color_as_moved(o, &add_lines, &del_lines);
Stefan Beller86b452e2017-06-30 13:53:09 -07006359 if (o->color_moved == COLOR_MOVED_ZEBRA_DIM)
6360 dim_moved_lines(o);
Stefan Beller2e2d5ac2017-06-30 13:53:07 -07006361
Elijah Newren6da1a252020-11-02 18:55:05 +00006362 hashmap_clear_and_free(&add_lines, struct moved_entry,
Eric Wongc8e424c2019-10-06 23:30:40 +00006363 ent);
Elijah Newren6da1a252020-11-02 18:55:05 +00006364 hashmap_clear_and_free(&del_lines, struct moved_entry,
Eric Wongc8e424c2019-10-06 23:30:40 +00006365 ent);
Stefan Beller2e2d5ac2017-06-30 13:53:07 -07006366 }
6367
Stefan Bellere6e045f2017-06-29 17:07:06 -07006368 for (i = 0; i < esm.nr; i++)
6369 emit_diff_symbol_from_struct(o, &esm.buf[i]);
6370
6371 for (i = 0; i < esm.nr; i++)
6372 free((void *)esm.buf[i].line);
Jeff King48edf3a2019-01-24 07:32:41 -05006373 esm.nr = 0;
6374
6375 o->emitted_symbols = NULL;
Stefan Bellere6e045f2017-06-29 17:07:06 -07006376 }
Stefan Bellerec331502017-06-29 17:06:48 -07006377}
6378
Ævar Arnfjörð Bjarmasone900d492021-02-11 11:45:34 +01006379static void diff_free_file(struct diff_options *options)
6380{
6381 if (options->close_file)
6382 fclose(options->file);
6383}
6384
Ævar Arnfjörð Bjarmasonc45dc9c2021-02-11 11:45:35 +01006385static void diff_free_ignore_regex(struct diff_options *options)
6386{
6387 int i;
6388
6389 for (i = 0; i < options->ignore_regex_nr; i++) {
6390 regfree(options->ignore_regex[i]);
6391 free(options->ignore_regex[i]);
6392 }
6393 free(options->ignore_regex);
6394}
6395
Ævar Arnfjörð Bjarmasone900d492021-02-11 11:45:34 +01006396void diff_free(struct diff_options *options)
6397{
6398 if (options->no_free)
6399 return;
6400
6401 diff_free_file(options);
Ævar Arnfjörð Bjarmasonc45dc9c2021-02-11 11:45:35 +01006402 diff_free_ignore_regex(options);
Ævar Arnfjörð Bjarmasone900d492021-02-11 11:45:34 +01006403}
6404
Junio C Hamano6973dca2006-04-21 23:57:45 -07006405void diff_flush(struct diff_options *options)
6406{
6407 struct diff_queue_struct *q = &diff_queued_diff;
Timo Hirvonenc6744342006-06-24 20:21:53 +03006408 int i, output_format = options->output_format;
Timo Hirvonen946c3782006-06-27 15:09:17 +03006409 int separator = 0;
Johan Herland1c57a622011-04-29 11:36:21 +02006410 int dirstat_by_line = 0;
Junio C Hamano6973dca2006-04-21 23:57:45 -07006411
Timo Hirvonenc6744342006-06-24 20:21:53 +03006412 /*
6413 * Order: raw, stat, summary, patch
6414 * or: name/name-status/checkdiff (other bits clear)
6415 */
Timo Hirvonen946c3782006-06-27 15:09:17 +03006416 if (!q->nr)
6417 goto free_queue;
Junio C Hamano6973dca2006-04-21 23:57:45 -07006418
Timo Hirvonenc6744342006-06-24 20:21:53 +03006419 if (output_format & (DIFF_FORMAT_RAW |
6420 DIFF_FORMAT_NAME |
6421 DIFF_FORMAT_NAME_STATUS |
6422 DIFF_FORMAT_CHECKDIFF)) {
Junio C Hamano6973dca2006-04-21 23:57:45 -07006423 for (i = 0; i < q->nr; i++) {
6424 struct diff_filepair *p = q->queue[i];
Timo Hirvonenc6744342006-06-24 20:21:53 +03006425 if (check_pair_status(p))
6426 flush_one_pair(p, options);
Junio C Hamano6973dca2006-04-21 23:57:45 -07006427 }
Timo Hirvonen946c3782006-06-27 15:09:17 +03006428 separator++;
Junio C Hamano6973dca2006-04-21 23:57:45 -07006429 }
Timo Hirvonenc6744342006-06-24 20:21:53 +03006430
Brandon Williams0d1e0e72017-10-31 11:19:11 -07006431 if (output_format & DIFF_FORMAT_DIRSTAT && options->flags.dirstat_by_line)
Johan Herland1c57a622011-04-29 11:36:21 +02006432 dirstat_by_line = 1;
6433
6434 if (output_format & (DIFF_FORMAT_DIFFSTAT|DIFF_FORMAT_SHORTSTAT|DIFF_FORMAT_NUMSTAT) ||
6435 dirstat_by_line) {
Timo Hirvonen5e2b0632006-06-25 14:28:19 +03006436 struct diffstat_t diffstat;
Timo Hirvonenc6744342006-06-24 20:21:53 +03006437
Daniel Ferreirae4cb6592019-11-13 12:40:58 +00006438 compute_diffstat(options, &diffstat, q);
Junio C Hamano74e2abe2006-10-12 03:01:00 -07006439 if (output_format & DIFF_FORMAT_NUMSTAT)
6440 show_numstat(&diffstat, options);
6441 if (output_format & DIFF_FORMAT_DIFFSTAT)
6442 show_stats(&diffstat, options);
Junio C Hamanof6046522007-12-11 23:46:30 -08006443 if (output_format & DIFF_FORMAT_SHORTSTAT)
Daniel Barkalowc0c77732008-03-09 22:43:39 -04006444 show_shortstats(&diffstat, options);
Mårten Kongstadab273892015-03-02 16:05:39 +01006445 if (output_format & DIFF_FORMAT_DIRSTAT && dirstat_by_line)
Johan Herland1c57a622011-04-29 11:36:21 +02006446 show_dirstat_by_line(&diffstat, options);
Junio C Hamanof6046522007-12-11 23:46:30 -08006447 free_diffstat_info(&diffstat);
Junio C Hamano3969cf72006-06-27 15:08:19 -07006448 separator++;
Junio C Hamano6973dca2006-04-21 23:57:45 -07006449 }
Johan Herland1c57a622011-04-29 11:36:21 +02006450 if ((output_format & DIFF_FORMAT_DIRSTAT) && !dirstat_by_line)
Junio C Hamanoc04a7152008-02-12 17:06:58 -08006451 show_dirstat(options);
Junio C Hamano6973dca2006-04-21 23:57:45 -07006452
Timo Hirvonen946c3782006-06-27 15:09:17 +03006453 if (output_format & DIFF_FORMAT_SUMMARY && !is_summary_empty(q)) {
Bo Yang7be57612010-05-26 15:23:54 +08006454 for (i = 0; i < q->nr; i++) {
6455 diff_summary(options, q->queue[i]);
6456 }
Junio C Hamano3969cf72006-06-27 15:08:19 -07006457 separator++;
Sean4bbd2612006-05-14 08:13:49 -04006458 }
6459
Larry D'Anna6977c252010-02-16 01:55:21 -05006460 if (output_format & DIFF_FORMAT_NO_OUTPUT &&
Brandon Williams0d1e0e72017-10-31 11:19:11 -07006461 options->flags.exit_with_status &&
6462 options->flags.diff_from_contents) {
Larry D'Anna6977c252010-02-16 01:55:21 -05006463 /*
6464 * run diff_flush_patch for the exit status. setting
Ondřej Bílka749f7632013-07-22 23:02:23 +02006465 * options->file to /dev/null should be safe, because we
Larry D'Anna6977c252010-02-16 01:55:21 -05006466 * aren't supposed to produce any output anyway.
6467 */
Ævar Arnfjörð Bjarmasone900d492021-02-11 11:45:34 +01006468 diff_free_file(options);
Nguyễn Thái Ngọc Duy23a9e072017-05-03 17:16:46 +07006469 options->file = xfopen("/dev/null", "w");
Larry D'Anna6977c252010-02-16 01:55:21 -05006470 options->close_file = 1;
Stefan Beller2e2d5ac2017-06-30 13:53:07 -07006471 options->color_moved = 0;
Larry D'Anna6977c252010-02-16 01:55:21 -05006472 for (i = 0; i < q->nr; i++) {
6473 struct diff_filepair *p = q->queue[i];
6474 if (check_pair_status(p))
6475 diff_flush_patch(p, options);
6476 if (options->found_changes)
6477 break;
6478 }
6479 }
6480
Timo Hirvonenc6744342006-06-24 20:21:53 +03006481 if (output_format & DIFF_FORMAT_PATCH) {
Timo Hirvonen946c3782006-06-27 15:09:17 +03006482 if (separator) {
Stefan Beller091f8e22017-06-29 17:06:53 -07006483 emit_diff_symbol(options, DIFF_SYMBOL_SEPARATOR, NULL, 0, 0);
Stefan Beller30b7e1e2017-06-29 17:07:04 -07006484 if (options->stat_sep)
Timo Hirvonen946c3782006-06-27 15:09:17 +03006485 /* attach patch instead of inline */
Stefan Beller30b7e1e2017-06-29 17:07:04 -07006486 emit_diff_symbol(options, DIFF_SYMBOL_STAT_SEP,
6487 NULL, 0, 0);
Timo Hirvonenc6744342006-06-24 20:21:53 +03006488 }
6489
Stefan Bellerec331502017-06-29 17:06:48 -07006490 diff_flush_patch_all_file_pairs(options);
Timo Hirvonenc6744342006-06-24 20:21:53 +03006491 }
6492
Jeff King04245582006-09-07 02:35:42 -04006493 if (output_format & DIFF_FORMAT_CALLBACK)
6494 options->format_callback(q, options, options->format_callback_data);
6495
Timo Hirvonenc6744342006-06-24 20:21:53 +03006496 for (i = 0; i < q->nr; i++)
6497 diff_free_filepair(q->queue[i]);
Timo Hirvonen946c3782006-06-27 15:09:17 +03006498free_queue:
Junio C Hamano6973dca2006-04-21 23:57:45 -07006499 free(q->queue);
Bo Yang9ca5df92010-05-06 21:52:27 -07006500 DIFF_QUEUE_CLEAR(q);
Ævar Arnfjörð Bjarmasone900d492021-02-11 11:45:34 +01006501 diff_free(options);
Junio C Hamanof2451942009-05-22 12:45:29 -07006502
6503 /*
Jim Meyering97bf2a02009-08-30 22:27:02 +02006504 * Report the content-level differences with HAS_CHANGES;
Junio C Hamanof2451942009-05-22 12:45:29 -07006505 * diff_addremove/diff_change does not set the bit when
6506 * DIFF_FROM_CONTENTS is in effect (e.g. with -w).
6507 */
Brandon Williams0d1e0e72017-10-31 11:19:11 -07006508 if (options->flags.diff_from_contents) {
Junio C Hamanof2451942009-05-22 12:45:29 -07006509 if (options->found_changes)
Brandon Williams0d1e0e72017-10-31 11:19:11 -07006510 options->flags.has_changes = 1;
Junio C Hamanof2451942009-05-22 12:45:29 -07006511 else
Brandon Williams0d1e0e72017-10-31 11:19:11 -07006512 options->flags.has_changes = 0;
Junio C Hamanof2451942009-05-22 12:45:29 -07006513 }
Junio C Hamano6973dca2006-04-21 23:57:45 -07006514}
6515
Junio C Hamano08578fa2013-07-17 15:09:34 -07006516static int match_filter(const struct diff_options *options, const struct diff_filepair *p)
6517{
6518 return (((p->status == DIFF_STATUS_MODIFIED) &&
6519 ((p->score &&
Junio C Hamano1ecc1cb2013-07-17 15:05:46 -07006520 filter_bit_tst(DIFF_STATUS_FILTER_BROKEN, options)) ||
Junio C Hamano08578fa2013-07-17 15:09:34 -07006521 (!p->score &&
Junio C Hamano1ecc1cb2013-07-17 15:05:46 -07006522 filter_bit_tst(DIFF_STATUS_MODIFIED, options)))) ||
Junio C Hamano08578fa2013-07-17 15:09:34 -07006523 ((p->status != DIFF_STATUS_MODIFIED) &&
Junio C Hamano1ecc1cb2013-07-17 15:05:46 -07006524 filter_bit_tst(p->status, options)));
Junio C Hamano08578fa2013-07-17 15:09:34 -07006525}
6526
Junio C Hamano949226f2013-07-17 14:19:24 -07006527static void diffcore_apply_filter(struct diff_options *options)
Junio C Hamano6973dca2006-04-21 23:57:45 -07006528{
6529 int i;
6530 struct diff_queue_struct *q = &diff_queued_diff;
6531 struct diff_queue_struct outq;
Junio C Hamano949226f2013-07-17 14:19:24 -07006532
Bo Yang9ca5df92010-05-06 21:52:27 -07006533 DIFF_QUEUE_CLEAR(&outq);
Junio C Hamano6973dca2006-04-21 23:57:45 -07006534
Junio C Hamano1ecc1cb2013-07-17 15:05:46 -07006535 if (!options->filter)
Junio C Hamano6973dca2006-04-21 23:57:45 -07006536 return;
6537
Junio C Hamano1ecc1cb2013-07-17 15:05:46 -07006538 if (filter_bit_tst(DIFF_STATUS_FILTER_AON, options)) {
Junio C Hamano6973dca2006-04-21 23:57:45 -07006539 int found;
6540 for (i = found = 0; !found && i < q->nr; i++) {
Junio C Hamano08578fa2013-07-17 15:09:34 -07006541 if (match_filter(options, q->queue[i]))
Junio C Hamano6973dca2006-04-21 23:57:45 -07006542 found++;
6543 }
6544 if (found)
6545 return;
6546
6547 /* otherwise we will clear the whole queue
6548 * by copying the empty outq at the end of this
6549 * function, but first clear the current entries
6550 * in the queue.
6551 */
6552 for (i = 0; i < q->nr; i++)
6553 diff_free_filepair(q->queue[i]);
6554 }
6555 else {
6556 /* Only the matching ones */
6557 for (i = 0; i < q->nr; i++) {
6558 struct diff_filepair *p = q->queue[i];
Junio C Hamano08578fa2013-07-17 15:09:34 -07006559 if (match_filter(options, p))
Junio C Hamano6973dca2006-04-21 23:57:45 -07006560 diff_q(&outq, p);
6561 else
6562 diff_free_filepair(p);
6563 }
6564 }
6565 free(q->queue);
6566 *q = outq;
6567}
6568
Sven Verdoolaege57011152007-09-08 12:30:22 +02006569/* Check whether two filespecs with the same mode and size are identical */
Nguyễn Thái Ngọc Duyb78ea5f2018-09-21 17:57:19 +02006570static int diff_filespec_is_identical(struct repository *r,
6571 struct diff_filespec *one,
Sven Verdoolaege57011152007-09-08 12:30:22 +02006572 struct diff_filespec *two)
6573{
Junio C Hamano2b459b42008-03-02 00:07:59 -08006574 if (S_ISGITLINK(one->mode))
6575 return 0;
Jonathan Tan1c37e862020-04-07 15:11:41 -07006576 if (diff_populate_filespec(r, one, NULL))
Sven Verdoolaege57011152007-09-08 12:30:22 +02006577 return 0;
Jonathan Tan1c37e862020-04-07 15:11:41 -07006578 if (diff_populate_filespec(r, two, NULL))
Sven Verdoolaege57011152007-09-08 12:30:22 +02006579 return 0;
6580 return !memcmp(one->data, two->data, one->size);
6581}
6582
Nguyễn Thái Ngọc Duyb78ea5f2018-09-21 17:57:19 +02006583static int diff_filespec_check_stat_unmatch(struct repository *r,
6584 struct diff_filepair *p)
Nguyễn Thái Ngọc Duyfceb9072014-01-25 13:46:49 +07006585{
Jonathan Tan1c37e862020-04-07 15:11:41 -07006586 struct diff_populate_filespec_options dpf_options = {
6587 .check_size_only = 1,
Jonathan Tan95acf112020-04-07 15:11:43 -07006588 .missing_object_cb = diff_queued_diff_prefetch,
6589 .missing_object_data = r,
Jonathan Tan1c37e862020-04-07 15:11:41 -07006590 };
6591
Nguyễn Thái Ngọc Duyf34b2052014-01-25 13:46:50 +07006592 if (p->done_skip_stat_unmatch)
6593 return p->skip_stat_unmatch_result;
6594
6595 p->done_skip_stat_unmatch = 1;
6596 p->skip_stat_unmatch_result = 0;
Nguyễn Thái Ngọc Duyfceb9072014-01-25 13:46:49 +07006597 /*
6598 * 1. Entries that come from stat info dirtiness
6599 * always have both sides (iow, not create/delete),
6600 * one side of the object name is unknown, with
6601 * the same mode and size. Keep the ones that
6602 * do not match these criteria. They have real
6603 * differences.
6604 *
6605 * 2. At this point, the file is known to be modified,
6606 * with the same mode and size, and the object
6607 * name of one side is unknown. Need to inspect
6608 * the identical contents.
6609 */
6610 if (!DIFF_FILE_VALID(p->one) || /* (1) */
6611 !DIFF_FILE_VALID(p->two) ||
brian m. carlson41c95602016-06-24 23:09:24 +00006612 (p->one->oid_valid && p->two->oid_valid) ||
Nguyễn Thái Ngọc Duyfceb9072014-01-25 13:46:49 +07006613 (p->one->mode != p->two->mode) ||
Jonathan Tan1c37e862020-04-07 15:11:41 -07006614 diff_populate_filespec(r, p->one, &dpf_options) ||
6615 diff_populate_filespec(r, p->two, &dpf_options) ||
Nguyễn Thái Ngọc Duyfceb9072014-01-25 13:46:49 +07006616 (p->one->size != p->two->size) ||
Nguyễn Thái Ngọc Duyb78ea5f2018-09-21 17:57:19 +02006617 !diff_filespec_is_identical(r, p->one, p->two)) /* (2) */
Nguyễn Thái Ngọc Duyf34b2052014-01-25 13:46:50 +07006618 p->skip_stat_unmatch_result = 1;
6619 return p->skip_stat_unmatch_result;
Nguyễn Thái Ngọc Duyfceb9072014-01-25 13:46:49 +07006620}
6621
Junio C Hamanofb132272007-08-03 13:33:31 -07006622static void diffcore_skip_stat_unmatch(struct diff_options *diffopt)
6623{
6624 int i;
6625 struct diff_queue_struct *q = &diff_queued_diff;
6626 struct diff_queue_struct outq;
Bo Yang9ca5df92010-05-06 21:52:27 -07006627 DIFF_QUEUE_CLEAR(&outq);
Junio C Hamanofb132272007-08-03 13:33:31 -07006628
6629 for (i = 0; i < q->nr; i++) {
6630 struct diff_filepair *p = q->queue[i];
6631
Nguyễn Thái Ngọc Duyb78ea5f2018-09-21 17:57:19 +02006632 if (diff_filespec_check_stat_unmatch(diffopt->repo, p))
Junio C Hamanofb132272007-08-03 13:33:31 -07006633 diff_q(&outq, p);
6634 else {
6635 /*
6636 * The caller can subtract 1 from skip_stat_unmatch
6637 * to determine how many paths were dirty only
6638 * due to stat info mismatch.
6639 */
Brandon Williams0d1e0e72017-10-31 11:19:11 -07006640 if (!diffopt->flags.no_index)
René Scharfe6d2d9e82007-08-15 00:41:00 +02006641 diffopt->skip_stat_unmatch++;
Junio C Hamanofb132272007-08-03 13:33:31 -07006642 diff_free_filepair(p);
6643 }
6644 }
6645 free(q->queue);
6646 *q = outq;
6647}
6648
Junio C Hamano730f7282009-09-20 00:03:39 -07006649static int diffnamecmp(const void *a_, const void *b_)
6650{
6651 const struct diff_filepair *a = *((const struct diff_filepair **)a_);
6652 const struct diff_filepair *b = *((const struct diff_filepair **)b_);
6653 const char *name_a, *name_b;
6654
6655 name_a = a->one ? a->one->path : a->two->path;
6656 name_b = b->one ? b->one->path : b->two->path;
6657 return strcmp(name_a, name_b);
6658}
6659
Jeff King784c0da2019-02-14 00:48:03 -05006660void diffcore_fix_diff_index(void)
Junio C Hamano730f7282009-09-20 00:03:39 -07006661{
6662 struct diff_queue_struct *q = &diff_queued_diff;
René Scharfe9ed0d8d2016-09-29 17:27:31 +02006663 QSORT(q->queue, q->nr, diffnamecmp);
Junio C Hamano730f7282009-09-20 00:03:39 -07006664}
6665
Jonathan Tan95acf112020-04-07 15:11:43 -07006666void diff_add_if_missing(struct repository *r,
6667 struct oid_array *to_fetch,
6668 const struct diff_filespec *filespec)
Jonathan Tan7fbbcb22019-04-05 10:09:34 -07006669{
6670 if (filespec && filespec->oid_valid &&
Jonathan Tana63694f2019-08-20 13:53:20 -07006671 !S_ISGITLINK(filespec->mode) &&
Jonathan Tan7fbbcb22019-04-05 10:09:34 -07006672 oid_object_info_extended(r, &filespec->oid, NULL,
6673 OBJECT_INFO_FOR_PREFETCH))
6674 oid_array_append(to_fetch, &filespec->oid);
6675}
6676
Jonathan Tan95acf112020-04-07 15:11:43 -07006677void diff_queued_diff_prefetch(void *repository)
6678{
6679 struct repository *repo = repository;
6680 int i;
6681 struct diff_queue_struct *q = &diff_queued_diff;
6682 struct oid_array to_fetch = OID_ARRAY_INIT;
6683
6684 for (i = 0; i < q->nr; i++) {
6685 struct diff_filepair *p = q->queue[i];
6686 diff_add_if_missing(repo, &to_fetch, p->one);
6687 diff_add_if_missing(repo, &to_fetch, p->two);
6688 }
6689
6690 /*
6691 * NEEDSWORK: Consider deduplicating the OIDs sent.
6692 */
6693 promisor_remote_get_direct(repo, to_fetch.oid, to_fetch.nr);
6694
6695 oid_array_clear(&to_fetch);
6696}
6697
Junio C Hamano6973dca2006-04-21 23:57:45 -07006698void diffcore_std(struct diff_options *options)
6699{
Jonathan Tan95acf112020-04-07 15:11:43 -07006700 int output_formats_to_prefetch = DIFF_FORMAT_DIFFSTAT |
6701 DIFF_FORMAT_NUMSTAT |
6702 DIFF_FORMAT_PATCH |
6703 DIFF_FORMAT_SHORTSTAT |
6704 DIFF_FORMAT_DIRSTAT;
Jonathan Tan7fbbcb22019-04-05 10:09:34 -07006705
Jonathan Tan95acf112020-04-07 15:11:43 -07006706 /*
6707 * Check if the user requested a blob-data-requiring diff output and/or
6708 * break-rewrite detection (which requires blob data). If yes, prefetch
6709 * the diff pairs.
6710 *
6711 * If no prefetching occurs, diffcore_rename() will prefetch if it
6712 * decides that it needs inexact rename detection.
6713 */
6714 if (options->repo == the_repository && has_promisor_remote() &&
6715 (options->output_format & output_formats_to_prefetch ||
6716 options->pickaxe_opts & DIFF_PICKAXE_KINDS_MASK))
6717 diff_queued_diff_prefetch(options->repo);
Jonathan Tan7fbbcb22019-04-05 10:09:34 -07006718
Kirill Smelkov7195fbf2014-02-24 20:21:51 +04006719 /* NOTE please keep the following in sync with diff_tree_combined() */
Junio C Hamano9d865352008-09-06 19:09:16 -07006720 if (options->skip_stat_unmatch)
Junio C Hamanofb132272007-08-03 13:33:31 -07006721 diffcore_skip_stat_unmatch(options);
Junio C Hamano44c48a92010-08-13 12:17:45 -07006722 if (!options->found_follow) {
6723 /* See try_to_follow_renames() in tree-diff.c */
6724 if (options->break_opt != -1)
Nguyễn Thái Ngọc Duyb78ea5f2018-09-21 17:57:19 +02006725 diffcore_break(options->repo,
6726 options->break_opt);
Junio C Hamano44c48a92010-08-13 12:17:45 -07006727 if (options->detect_rename)
6728 diffcore_rename(options);
6729 if (options->break_opt != -1)
6730 diffcore_merge_broken();
6731 }
Stefan Bellercf630512018-01-04 14:50:41 -08006732 if (options->pickaxe_opts & DIFF_PICKAXE_KINDS_MASK)
Junio C Hamano382f0132010-08-31 13:44:39 -07006733 diffcore_pickaxe(options);
Junio C Hamano6973dca2006-04-21 23:57:45 -07006734 if (options->orderfile)
6735 diffcore_order(options->orderfile);
Junio C Hamano1eb41362021-02-11 11:57:50 -08006736 if (options->rotate_to)
6737 diffcore_rotate(options);
Junio C Hamano44c48a92010-08-13 12:17:45 -07006738 if (!options->found_follow)
6739 /* See try_to_follow_renames() in tree-diff.c */
6740 diff_resolve_rename_copy();
Junio C Hamano949226f2013-07-17 14:19:24 -07006741 diffcore_apply_filter(options);
Junio C Hamano68aacb22007-03-14 11:12:13 -07006742
Brandon Williams0d1e0e72017-10-31 11:19:11 -07006743 if (diff_queued_diff.nr && !options->flags.diff_from_contents)
6744 options->flags.has_changes = 1;
Pierre Habouzit8f67f8a2007-11-10 20:05:14 +01006745 else
Brandon Williams0d1e0e72017-10-31 11:19:11 -07006746 options->flags.has_changes = 0;
Bo Yang1da61752010-05-06 21:52:28 -07006747
Junio C Hamano44c48a92010-08-13 12:17:45 -07006748 options->found_follow = 0;
Junio C Hamano6973dca2006-04-21 23:57:45 -07006749}
6750
Junio C Hamanoda31b352007-12-13 23:40:27 -08006751int diff_result_code(struct diff_options *opt, int status)
6752{
6753 int result = 0;
Junio C Hamanof31027c2011-01-06 13:50:06 -08006754
Max Nanasyc9fc4412013-03-21 12:53:38 -07006755 diff_warn_rename_limit("diff.renameLimit",
Junio C Hamanof31027c2011-01-06 13:50:06 -08006756 opt->needed_rename_limit,
6757 opt->degraded_cc_to_c);
Brandon Williams0d1e0e72017-10-31 11:19:11 -07006758 if (!opt->flags.exit_with_status &&
Junio C Hamanoda31b352007-12-13 23:40:27 -08006759 !(opt->output_format & DIFF_FORMAT_CHECKDIFF))
6760 return status;
Brandon Williams0d1e0e72017-10-31 11:19:11 -07006761 if (opt->flags.exit_with_status &&
6762 opt->flags.has_changes)
Junio C Hamanoda31b352007-12-13 23:40:27 -08006763 result |= 01;
6764 if ((opt->output_format & DIFF_FORMAT_CHECKDIFF) &&
Brandon Williams0d1e0e72017-10-31 11:19:11 -07006765 opt->flags.check_failed)
Junio C Hamanoda31b352007-12-13 23:40:27 -08006766 result |= 02;
6767 return result;
6768}
Junio C Hamano6973dca2006-04-21 23:57:45 -07006769
Junio C Hamano28b92642011-05-31 09:14:17 -07006770int diff_can_quit_early(struct diff_options *opt)
6771{
Brandon Williams0d1e0e72017-10-31 11:19:11 -07006772 return (opt->flags.quick &&
Junio C Hamano28b92642011-05-31 09:14:17 -07006773 !opt->filter &&
Brandon Williams0d1e0e72017-10-31 11:19:11 -07006774 opt->flags.has_changes);
Junio C Hamano28b92642011-05-31 09:14:17 -07006775}
6776
Jens Lehmannaee9c7d2010-08-06 00:39:25 +02006777/*
6778 * Shall changes to this submodule be ignored?
6779 *
6780 * Submodule changes can be configured to be ignored separately for each path,
6781 * but that configuration can be overridden from the command line.
6782 */
6783static int is_submodule_ignored(const char *path, struct diff_options *options)
6784{
6785 int ignored = 0;
Brandon Williams02f2f562017-10-31 11:19:05 -07006786 struct diff_flags orig_flags = options->flags;
Brandon Williams0d1e0e72017-10-31 11:19:11 -07006787 if (!options->flags.override_submodule_config)
Jens Lehmannaee9c7d2010-08-06 00:39:25 +02006788 set_diffopt_flags_from_submodule_config(options, path);
Brandon Williams0d1e0e72017-10-31 11:19:11 -07006789 if (options->flags.ignore_submodules)
Jens Lehmannaee9c7d2010-08-06 00:39:25 +02006790 ignored = 1;
6791 options->flags = orig_flags;
6792 return ignored;
6793}
6794
Daniel Ferreirae4cb6592019-11-13 12:40:58 +00006795void compute_diffstat(struct diff_options *options,
6796 struct diffstat_t *diffstat,
6797 struct diff_queue_struct *q)
6798{
6799 int i;
6800
6801 memset(diffstat, 0, sizeof(struct diffstat_t));
6802 for (i = 0; i < q->nr; i++) {
6803 struct diff_filepair *p = q->queue[i];
6804 if (check_pair_status(p))
6805 diff_flush_stat(p, options, diffstat);
6806 }
6807}
6808
Junio C Hamano6973dca2006-04-21 23:57:45 -07006809void diff_addremove(struct diff_options *options,
6810 int addremove, unsigned mode,
Brandon Williamsc26022e2017-05-30 10:30:47 -07006811 const struct object_id *oid,
6812 int oid_valid,
Jens Lehmanne3d42c42010-01-18 21:26:18 +01006813 const char *concatpath, unsigned dirty_submodule)
Junio C Hamano6973dca2006-04-21 23:57:45 -07006814{
Junio C Hamano6973dca2006-04-21 23:57:45 -07006815 struct diff_filespec *one, *two;
6816
Jens Lehmannaee9c7d2010-08-06 00:39:25 +02006817 if (S_ISGITLINK(mode) && is_submodule_ignored(concatpath, options))
Johannes Schindelin50fd9bd2008-05-14 18:03:31 +01006818 return;
6819
Junio C Hamano6973dca2006-04-21 23:57:45 -07006820 /* This may look odd, but it is a preparation for
6821 * feeding "there are unchanged files which should
6822 * not produce diffs, but when you are doing copy
6823 * detection you would need them, so here they are"
6824 * entries to the diff-core. They will be prefixed
6825 * with something like '=' or '*' (I haven't decided
6826 * which but should not make any difference).
Junio C Hamanoa6080a02007-06-07 00:04:01 -07006827 * Feeding the same new and old to diff_change()
Junio C Hamano6973dca2006-04-21 23:57:45 -07006828 * also has the same effect.
6829 * Before the final output happens, they are pruned after
6830 * merged into rename/copy pairs as appropriate.
6831 */
Brandon Williams0d1e0e72017-10-31 11:19:11 -07006832 if (options->flags.reverse_diff)
Junio C Hamano6973dca2006-04-21 23:57:45 -07006833 addremove = (addremove == '+' ? '-' :
6834 addremove == '-' ? '+' : addremove);
6835
Junio C Hamanocd676a52008-02-12 14:26:02 -08006836 if (options->prefix &&
6837 strncmp(concatpath, options->prefix, options->prefix_length))
6838 return;
6839
Junio C Hamano6973dca2006-04-21 23:57:45 -07006840 one = alloc_filespec(concatpath);
6841 two = alloc_filespec(concatpath);
6842
6843 if (addremove != '+')
Brandon Williamsf9704c22017-05-30 10:30:50 -07006844 fill_filespec(one, oid, oid_valid, mode);
Jens Lehmanne3d42c42010-01-18 21:26:18 +01006845 if (addremove != '-') {
Brandon Williamsf9704c22017-05-30 10:30:50 -07006846 fill_filespec(two, oid, oid_valid, mode);
Jens Lehmanne3d42c42010-01-18 21:26:18 +01006847 two->dirty_submodule = dirty_submodule;
6848 }
Junio C Hamano6973dca2006-04-21 23:57:45 -07006849
6850 diff_queue(&diff_queued_diff, one, two);
Brandon Williams0d1e0e72017-10-31 11:19:11 -07006851 if (!options->flags.diff_from_contents)
6852 options->flags.has_changes = 1;
Junio C Hamano6973dca2006-04-21 23:57:45 -07006853}
6854
6855void diff_change(struct diff_options *options,
6856 unsigned old_mode, unsigned new_mode,
Brandon Williams94a00972017-05-30 10:30:49 -07006857 const struct object_id *old_oid,
6858 const struct object_id *new_oid,
6859 int old_oid_valid, int new_oid_valid,
Jens Lehmanne3d42c42010-01-18 21:26:18 +01006860 const char *concatpath,
6861 unsigned old_dirty_submodule, unsigned new_dirty_submodule)
Junio C Hamano6973dca2006-04-21 23:57:45 -07006862{
Junio C Hamano6973dca2006-04-21 23:57:45 -07006863 struct diff_filespec *one, *two;
Nguyễn Thái Ngọc Duyf34b2052014-01-25 13:46:50 +07006864 struct diff_filepair *p;
Junio C Hamano6973dca2006-04-21 23:57:45 -07006865
Jens Lehmannaee9c7d2010-08-06 00:39:25 +02006866 if (S_ISGITLINK(old_mode) && S_ISGITLINK(new_mode) &&
6867 is_submodule_ignored(concatpath, options))
Johannes Schindelin50fd9bd2008-05-14 18:03:31 +01006868 return;
6869
Brandon Williams0d1e0e72017-10-31 11:19:11 -07006870 if (options->flags.reverse_diff) {
René Scharfe35d803b2017-01-28 22:40:58 +01006871 SWAP(old_mode, new_mode);
Brandon Williams94a00972017-05-30 10:30:49 -07006872 SWAP(old_oid, new_oid);
6873 SWAP(old_oid_valid, new_oid_valid);
René Scharfe35d803b2017-01-28 22:40:58 +01006874 SWAP(old_dirty_submodule, new_dirty_submodule);
Junio C Hamano6973dca2006-04-21 23:57:45 -07006875 }
Junio C Hamanocd676a52008-02-12 14:26:02 -08006876
6877 if (options->prefix &&
6878 strncmp(concatpath, options->prefix, options->prefix_length))
6879 return;
6880
Junio C Hamano6973dca2006-04-21 23:57:45 -07006881 one = alloc_filespec(concatpath);
6882 two = alloc_filespec(concatpath);
Brandon Williamsf9704c22017-05-30 10:30:50 -07006883 fill_filespec(one, old_oid, old_oid_valid, old_mode);
6884 fill_filespec(two, new_oid, new_oid_valid, new_mode);
Jens Lehmanne3d42c42010-01-18 21:26:18 +01006885 one->dirty_submodule = old_dirty_submodule;
6886 two->dirty_submodule = new_dirty_submodule;
Nguyễn Thái Ngọc Duyf34b2052014-01-25 13:46:50 +07006887 p = diff_queue(&diff_queued_diff, one, two);
Junio C Hamano6973dca2006-04-21 23:57:45 -07006888
Brandon Williams0d1e0e72017-10-31 11:19:11 -07006889 if (options->flags.diff_from_contents)
Nguyễn Thái Ngọc Duyf34b2052014-01-25 13:46:50 +07006890 return;
6891
Brandon Williams0d1e0e72017-10-31 11:19:11 -07006892 if (options->flags.quick && options->skip_stat_unmatch &&
Jeff Kingd2d7fbe2020-06-01 16:22:18 -04006893 !diff_filespec_check_stat_unmatch(options->repo, p)) {
6894 diff_free_filespec_data(p->one);
6895 diff_free_filespec_data(p->two);
Nguyễn Thái Ngọc Duyf34b2052014-01-25 13:46:50 +07006896 return;
Jeff Kingd2d7fbe2020-06-01 16:22:18 -04006897 }
Nguyễn Thái Ngọc Duyf34b2052014-01-25 13:46:50 +07006898
Brandon Williams0d1e0e72017-10-31 11:19:11 -07006899 options->flags.has_changes = 1;
Junio C Hamano6973dca2006-04-21 23:57:45 -07006900}
6901
Junio C Hamanofa7b2902011-04-22 16:05:58 -07006902struct diff_filepair *diff_unmerge(struct diff_options *options, const char *path)
Junio C Hamano6973dca2006-04-21 23:57:45 -07006903{
Junio C Hamano76399c02011-04-22 15:55:55 -07006904 struct diff_filepair *pair;
Junio C Hamano6973dca2006-04-21 23:57:45 -07006905 struct diff_filespec *one, *two;
Junio C Hamanocd676a52008-02-12 14:26:02 -08006906
6907 if (options->prefix &&
6908 strncmp(path, options->prefix, options->prefix_length))
Junio C Hamano76399c02011-04-22 15:55:55 -07006909 return NULL;
Junio C Hamanocd676a52008-02-12 14:26:02 -08006910
Junio C Hamano6973dca2006-04-21 23:57:45 -07006911 one = alloc_filespec(path);
6912 two = alloc_filespec(path);
Junio C Hamano76399c02011-04-22 15:55:55 -07006913 pair = diff_queue(&diff_queued_diff, one, two);
6914 pair->is_unmerged = 1;
6915 return pair;
Junio C Hamano6973dca2006-04-21 23:57:45 -07006916}
Jeff King9cb92c32008-10-05 17:43:45 -04006917
Nguyễn Thái Ngọc Duyb78ea5f2018-09-21 17:57:19 +02006918static char *run_textconv(struct repository *r,
6919 const char *pgm,
6920 struct diff_filespec *spec,
6921 size_t *outsize)
Jeff King9cb92c32008-10-05 17:43:45 -04006922{
Jeff King479b0ae2009-01-22 00:59:56 -05006923 struct diff_tempfile *temp;
Jeff King9cb92c32008-10-05 17:43:45 -04006924 const char *argv[3];
6925 const char **arg = argv;
René Scharfed3180272014-08-19 21:09:35 +02006926 struct child_process child = CHILD_PROCESS_INIT;
Jeff King9cb92c32008-10-05 17:43:45 -04006927 struct strbuf buf = STRBUF_INIT;
Johannes Sixtda1fbed2010-03-30 19:36:03 +02006928 int err = 0;
Jeff King9cb92c32008-10-05 17:43:45 -04006929
Nguyễn Thái Ngọc Duyb78ea5f2018-09-21 17:57:19 +02006930 temp = prepare_temp_file(r, spec->path, spec);
Jeff King9cb92c32008-10-05 17:43:45 -04006931 *arg++ = pgm;
Jeff King479b0ae2009-01-22 00:59:56 -05006932 *arg++ = temp->name;
Jeff King9cb92c32008-10-05 17:43:45 -04006933 *arg = NULL;
6934
Jeff King41a457e2009-12-30 06:01:09 -05006935 child.use_shell = 1;
Jeff King9cb92c32008-10-05 17:43:45 -04006936 child.argv = argv;
6937 child.out = -1;
Johannes Sixtda1fbed2010-03-30 19:36:03 +02006938 if (start_command(&child)) {
Jeff King479b0ae2009-01-22 00:59:56 -05006939 remove_tempfile();
Jeff King9cb92c32008-10-05 17:43:45 -04006940 return NULL;
6941 }
Johannes Sixtda1fbed2010-03-30 19:36:03 +02006942
6943 if (strbuf_read(&buf, child.out, 0) < 0)
6944 err = error("error reading from textconv command '%s'", pgm);
Jeff King70d70992009-12-30 04:02:53 -05006945 close(child.out);
Johannes Sixtda1fbed2010-03-30 19:36:03 +02006946
6947 if (finish_command(&child) || err) {
6948 strbuf_release(&buf);
6949 remove_tempfile();
6950 return NULL;
6951 }
Jeff King479b0ae2009-01-22 00:59:56 -05006952 remove_tempfile();
Jeff King9cb92c32008-10-05 17:43:45 -04006953
6954 return strbuf_detach(&buf, outsize);
6955}
Jeff King840383b2010-04-01 20:09:26 -04006956
Nguyễn Thái Ngọc Duy6afaf802018-09-21 17:57:22 +02006957size_t fill_textconv(struct repository *r,
6958 struct userdiff_driver *driver,
Axel Bonneta788d7d2010-06-07 17:23:36 +02006959 struct diff_filespec *df,
6960 char **outbuf)
Jeff King840383b2010-04-01 20:09:26 -04006961{
6962 size_t size;
6963
Jeff Kinga64e6a42016-02-22 13:28:54 -05006964 if (!driver) {
Jeff King840383b2010-04-01 20:09:26 -04006965 if (!DIFF_FILE_VALID(df)) {
6966 *outbuf = "";
6967 return 0;
6968 }
Jonathan Tan1c37e862020-04-07 15:11:41 -07006969 if (diff_populate_filespec(r, df, NULL))
Jeff King840383b2010-04-01 20:09:26 -04006970 die("unable to read files to diff");
6971 *outbuf = df->data;
6972 return df->size;
6973 }
6974
Jeff Kinga64e6a42016-02-22 13:28:54 -05006975 if (!driver->textconv)
Johannes Schindelin033abf92018-05-02 11:38:39 +02006976 BUG("fill_textconv called with non-textconv driver");
Jeff Kinga64e6a42016-02-22 13:28:54 -05006977
brian m. carlson41c95602016-06-24 23:09:24 +00006978 if (driver->textconv_cache && df->oid_valid) {
brian m. carlsona0d12c42016-06-24 23:09:23 +00006979 *outbuf = notes_cache_get(driver->textconv_cache,
brian m. carlson569aa372017-05-06 22:09:58 +00006980 &df->oid,
Jeff Kingd9bae1a2010-04-01 20:12:15 -04006981 &size);
6982 if (*outbuf)
6983 return size;
6984 }
6985
Nguyễn Thái Ngọc Duyb78ea5f2018-09-21 17:57:19 +02006986 *outbuf = run_textconv(r, driver->textconv, df, &size);
Jeff King840383b2010-04-01 20:09:26 -04006987 if (!*outbuf)
6988 die("unable to read files to diff");
Jeff Kingd9bae1a2010-04-01 20:12:15 -04006989
brian m. carlson41c95602016-06-24 23:09:24 +00006990 if (driver->textconv_cache && df->oid_valid) {
Jeff Kingd9bae1a2010-04-01 20:12:15 -04006991 /* ignore errors, as we might be in a readonly repository */
brian m. carlson569aa372017-05-06 22:09:58 +00006992 notes_cache_put(driver->textconv_cache, &df->oid, *outbuf,
Jeff Kingd9bae1a2010-04-01 20:12:15 -04006993 size);
6994 /*
6995 * we could save up changes and flush them all at the end,
6996 * but we would need an extra call after all diffing is done.
6997 * Since generating a cache entry is the slow path anyway,
6998 * this extra overhead probably isn't a big deal.
6999 */
7000 notes_cache_write(driver->textconv_cache);
7001 }
7002
Jeff King840383b2010-04-01 20:09:26 -04007003 return size;
7004}
Nguyễn Thái Ngọc Duy4914c962012-10-26 22:53:52 +07007005
Nguyễn Thái Ngọc Duy6afaf802018-09-21 17:57:22 +02007006int textconv_object(struct repository *r,
7007 const char *path,
Jeff Smith3a35cb22017-05-24 00:15:10 -05007008 unsigned mode,
7009 const struct object_id *oid,
7010 int oid_valid,
7011 char **buf,
7012 unsigned long *buf_size)
7013{
7014 struct diff_filespec *df;
7015 struct userdiff_driver *textconv;
7016
7017 df = alloc_filespec(path);
Junio C Hamanoa6f38c12017-06-19 12:38:44 -07007018 fill_filespec(df, oid, oid_valid, mode);
Nguyễn Thái Ngọc Duybd7ad452018-11-10 06:49:06 +01007019 textconv = get_textconv(r, df);
Jeff Smith3a35cb22017-05-24 00:15:10 -05007020 if (!textconv) {
7021 free_filespec(df);
7022 return 0;
7023 }
7024
Nguyễn Thái Ngọc Duy6afaf802018-09-21 17:57:22 +02007025 *buf_size = fill_textconv(r, textconv, df, buf);
Jeff Smith3a35cb22017-05-24 00:15:10 -05007026 free_filespec(df);
7027 return 1;
7028}
7029
Nguyễn Thái Ngọc Duy4914c962012-10-26 22:53:52 +07007030void setup_diff_pager(struct diff_options *opt)
7031{
7032 /*
7033 * If the user asked for our exit code, then either they want --quiet
7034 * or --exit-code. We should definitely not bother with a pager in the
7035 * former case, as we will generate no output. Since we still properly
7036 * report our exit code even when a pager is run, we _could_ run a
7037 * pager with --exit-code. But since we have not done so historically,
7038 * and because it is easy to find people oneline advising "git diff
7039 * --exit-code" in hooks and other scripts, we do not do so.
7040 */
Brandon Williams0d1e0e72017-10-31 11:19:11 -07007041 if (!opt->flags.exit_with_status &&
Nguyễn Thái Ngọc Duy4914c962012-10-26 22:53:52 +07007042 check_pager_config("diff") != 0)
7043 setup_pager();
7044}