blob: 974626a6211514b25af43e8f48933c6929826aa5 [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"
Phillip Wood72962e82021-12-09 10:30:09 +000021#include "mem-pool.h"
Junio C Hamanoa757c642010-03-24 19:21:32 -070022#include "ll-merge.h"
Michael Haggerty02e8ca02012-10-28 17:50:54 +010023#include "string-list.h"
Jeff Kingdbbcd442020-07-28 16:23:39 -040024#include "strvec.h"
Jacob Keller660e1132016-08-31 16:27:20 -070025#include "graph.h"
Jonathan Tan150e3002017-08-18 15:20:36 -070026#include "packfile.h"
Nguyễn Thái Ngọc Duy4a288472019-01-27 07:35:31 +070027#include "parse-options.h"
Nguyễn Thái Ngọc Duy3ac68a92018-05-26 15:55:24 +020028#include "help.h"
Christian Couderb14ed5a2019-06-25 15:40:31 +020029#include "promisor-remote.h"
Derrick Stoleead90da72021-09-08 11:23:56 +000030#include "dir.h"
Elijah Newren95433ee2022-02-02 02:37:34 +000031#include "strmap.h"
Junio C Hamano6973dca2006-04-21 23:57:45 -070032
Shawn O. Pearce1510fea2006-12-14 06:15:57 -050033#ifdef NO_FAST_WORKING_DIRECTORY
34#define FAST_WORKING_DIRECTORY 0
35#else
36#define FAST_WORKING_DIRECTORY 1
37#endif
38
David Rientjes96f1e582006-08-15 10:23:48 -070039static int diff_detect_rename_default;
Stefan Beller33de7162017-05-08 12:03:38 -040040static int diff_indent_heuristic = 1;
Elijah Newren94b82d52021-07-15 00:45:24 +000041static int diff_rename_limit_default = 1000;
Jim Meyeringa624eaa2008-08-15 13:39:26 +020042static int diff_suppress_blank_empty;
Junio C Hamanod2aea132012-09-15 13:59:59 -070043static int diff_use_color_default = -1;
Stefan Beller2e2d5ac2017-06-30 13:53:07 -070044static int diff_color_moved_default;
Stefan Beller626c0b52018-07-18 12:31:56 -070045static int diff_color_moved_ws_default;
Jeff Muizelaar6468a4e2012-09-27 15:12:52 -040046static int diff_context_default = 3;
Vegard Nossumc4888672017-01-12 13:21:11 +010047static int diff_interhunk_context_default;
Boyd Stephen Smith Jr98a4d872009-01-20 21:46:57 -060048static const char *diff_word_regex_cfg;
Johannes Schindelincbe02102007-12-17 13:42:20 +000049static const char *external_diff_cmd_cfg;
Samuel Bronson6d8940b2013-12-18 19:08:12 -050050static const char *diff_order_file_cfg;
Junio C Hamanoaecbf912007-08-31 13:13:42 -070051int diff_auto_refresh_index = 1;
Junio C Hamanoa5a818e2008-08-18 20:08:09 -070052static int diff_mnemonic_prefix;
Eli Collinsf89504d2010-05-02 19:03:41 -070053static int diff_no_prefix;
Laurent Arnoudc28ded82020-05-22 12:46:18 +020054static int diff_relative;
Zbigniew Jędrzejewski-Szmekdf444832012-03-01 13:26:46 +010055static int diff_stat_graph_width;
Johan Herland712d2c72011-04-29 11:36:20 +020056static int diff_dirstat_permille_default = 30;
Johannes Schindelinbe4f2b42010-08-05 10:49:55 +020057static struct diff_options default_diff_options;
Michal Privoznik07ab4de2013-01-16 08:51:57 +010058static long diff_algorithm;
Junio C Hamanoa17505f2016-10-04 15:26:27 -070059static unsigned ws_error_highlight_default = WSEH_NEW;
Junio C Hamano6973dca2006-04-21 23:57:45 -070060
Jeff King7c92fe02006-09-08 04:03:18 -040061static char diff_colors[][COLOR_MAXLEN] = {
Arjen Laarhovendc6ebd42009-02-13 22:53:40 +010062 GIT_COLOR_RESET,
Jeff King8dbf3eb2015-05-27 16:48:46 -040063 GIT_COLOR_NORMAL, /* CONTEXT */
Arjen Laarhovendc6ebd42009-02-13 22:53:40 +010064 GIT_COLOR_BOLD, /* METAINFO */
65 GIT_COLOR_CYAN, /* FRAGINFO */
66 GIT_COLOR_RED, /* OLD */
67 GIT_COLOR_GREEN, /* NEW */
68 GIT_COLOR_YELLOW, /* COMMIT */
69 GIT_COLOR_BG_RED, /* WHITESPACE */
Bert Wesarg89cb73a2009-11-27 07:55:18 +010070 GIT_COLOR_NORMAL, /* FUNCINFO */
Stefan Beller86b452e2017-06-30 13:53:09 -070071 GIT_COLOR_BOLD_MAGENTA, /* OLD_MOVED */
72 GIT_COLOR_BOLD_BLUE, /* OLD_MOVED ALTERNATIVE */
73 GIT_COLOR_FAINT, /* OLD_MOVED_DIM */
74 GIT_COLOR_FAINT_ITALIC, /* OLD_MOVED_ALTERNATIVE_DIM */
75 GIT_COLOR_BOLD_CYAN, /* NEW_MOVED */
76 GIT_COLOR_BOLD_YELLOW, /* NEW_MOVED ALTERNATIVE */
77 GIT_COLOR_FAINT, /* NEW_MOVED_DIM */
78 GIT_COLOR_FAINT_ITALIC, /* NEW_MOVED_ALTERNATIVE_DIM */
Johannes Schindelina7be92a2018-08-13 04:33:32 -070079 GIT_COLOR_FAINT, /* CONTEXT_DIM */
80 GIT_COLOR_FAINT_RED, /* OLD_DIM */
81 GIT_COLOR_FAINT_GREEN, /* NEW_DIM */
82 GIT_COLOR_BOLD, /* CONTEXT_BOLD */
83 GIT_COLOR_BOLD_RED, /* OLD_BOLD */
84 GIT_COLOR_BOLD_GREEN, /* NEW_BOLD */
Johannes Schindelincd112ce2006-06-13 18:45:44 +020085};
86
Nguyễn Thái Ngọc Duya73b3682018-05-26 15:55:21 +020087static const char *color_diff_slots[] = {
88 [DIFF_CONTEXT] = "context",
89 [DIFF_METAINFO] = "meta",
90 [DIFF_FRAGINFO] = "frag",
91 [DIFF_FILE_OLD] = "old",
92 [DIFF_FILE_NEW] = "new",
93 [DIFF_COMMIT] = "commit",
94 [DIFF_WHITESPACE] = "whitespace",
95 [DIFF_FUNCINFO] = "func",
96 [DIFF_FILE_OLD_MOVED] = "oldMoved",
97 [DIFF_FILE_OLD_MOVED_ALT] = "oldMovedAlternative",
98 [DIFF_FILE_OLD_MOVED_DIM] = "oldMovedDimmed",
99 [DIFF_FILE_OLD_MOVED_ALT_DIM] = "oldMovedAlternativeDimmed",
100 [DIFF_FILE_NEW_MOVED] = "newMoved",
101 [DIFF_FILE_NEW_MOVED_ALT] = "newMovedAlternative",
102 [DIFF_FILE_NEW_MOVED_DIM] = "newMovedDimmed",
103 [DIFF_FILE_NEW_MOVED_ALT_DIM] = "newMovedAlternativeDimmed",
Johannes Schindelina7be92a2018-08-13 04:33:32 -0700104 [DIFF_CONTEXT_DIM] = "contextDimmed",
105 [DIFF_FILE_OLD_DIM] = "oldDimmed",
106 [DIFF_FILE_NEW_DIM] = "newDimmed",
107 [DIFF_CONTEXT_BOLD] = "contextBold",
108 [DIFF_FILE_OLD_BOLD] = "oldBold",
109 [DIFF_FILE_NEW_BOLD] = "newBold",
Nguyễn Thái Ngọc Duya73b3682018-05-26 15:55:21 +0200110};
111
Nguyễn Thái Ngọc Duy3ac68a92018-05-26 15:55:24 +0200112define_list_config_array_extra(color_diff_slots, {"plain"});
113
Jeff King9e1a5eb2014-06-18 15:41:50 -0400114static int parse_diff_color_slot(const char *var)
Junio C Hamano801235c2006-06-24 04:06:23 -0700115{
Nguyễn Thái Ngọc Duya73b3682018-05-26 15:55:21 +0200116 if (!strcasecmp(var, "plain"))
Jeff King8dbf3eb2015-05-27 16:48:46 -0400117 return DIFF_CONTEXT;
Nguyễn Thái Ngọc Duya73b3682018-05-26 15:55:21 +0200118 return LOOKUP_CONFIG(color_diff_slots, var);
Junio C Hamano801235c2006-06-24 04:06:23 -0700119}
120
Michael Haggerty02e8ca02012-10-28 17:50:54 +0100121static int parse_dirstat_params(struct diff_options *options, const char *params_string,
Johan Herland51670fc2011-04-29 11:36:22 +0200122 struct strbuf *errmsg)
Johan Herland333f3fb2011-04-29 11:36:18 +0200123{
Michael Haggerty02e8ca02012-10-28 17:50:54 +0100124 char *params_copy = xstrdup(params_string);
125 struct string_list params = STRING_LIST_INIT_NODUP;
126 int ret = 0;
127 int i;
Johan Herland51670fc2011-04-29 11:36:22 +0200128
Michael Haggerty02e8ca02012-10-28 17:50:54 +0100129 if (*params_copy)
130 string_list_split_in_place(&params, params_copy, ',', -1);
131 for (i = 0; i < params.nr; i++) {
132 const char *p = params.items[i].string;
133 if (!strcmp(p, "changes")) {
Brandon Williams0d1e0e72017-10-31 11:19:11 -0700134 options->flags.dirstat_by_line = 0;
135 options->flags.dirstat_by_file = 0;
Michael Haggerty02e8ca02012-10-28 17:50:54 +0100136 } else if (!strcmp(p, "lines")) {
Brandon Williams0d1e0e72017-10-31 11:19:11 -0700137 options->flags.dirstat_by_line = 1;
138 options->flags.dirstat_by_file = 0;
Michael Haggerty02e8ca02012-10-28 17:50:54 +0100139 } else if (!strcmp(p, "files")) {
Brandon Williams0d1e0e72017-10-31 11:19:11 -0700140 options->flags.dirstat_by_line = 0;
141 options->flags.dirstat_by_file = 1;
Michael Haggerty02e8ca02012-10-28 17:50:54 +0100142 } else if (!strcmp(p, "noncumulative")) {
Brandon Williams0d1e0e72017-10-31 11:19:11 -0700143 options->flags.dirstat_cumulative = 0;
Michael Haggerty02e8ca02012-10-28 17:50:54 +0100144 } else if (!strcmp(p, "cumulative")) {
Brandon Williams0d1e0e72017-10-31 11:19:11 -0700145 options->flags.dirstat_cumulative = 1;
Johan Herland333f3fb2011-04-29 11:36:18 +0200146 } else if (isdigit(*p)) {
147 char *end;
Johan Herland51670fc2011-04-29 11:36:22 +0200148 int permille = strtoul(p, &end, 10) * 10;
149 if (*end == '.' && isdigit(*++end)) {
Johan Herland712d2c72011-04-29 11:36:20 +0200150 /* only use first digit */
Johan Herland51670fc2011-04-29 11:36:22 +0200151 permille += *end - '0';
Johan Herland712d2c72011-04-29 11:36:20 +0200152 /* .. and ignore any further digits */
Johan Herland51670fc2011-04-29 11:36:22 +0200153 while (isdigit(*++end))
Johan Herland712d2c72011-04-29 11:36:20 +0200154 ; /* nothing */
155 }
Michael Haggerty02e8ca02012-10-28 17:50:54 +0100156 if (!*end)
Johan Herland51670fc2011-04-29 11:36:22 +0200157 options->dirstat_permille = permille;
158 else {
Michael Haggerty02e8ca02012-10-28 17:50:54 +0100159 strbuf_addf(errmsg, _(" Failed to parse dirstat cut-off percentage '%s'\n"),
160 p);
Johan Herland51670fc2011-04-29 11:36:22 +0200161 ret++;
162 }
163 } else {
Michael Haggerty02e8ca02012-10-28 17:50:54 +0100164 strbuf_addf(errmsg, _(" Unknown dirstat parameter '%s'\n"), p);
Johan Herland51670fc2011-04-29 11:36:22 +0200165 ret++;
Johan Herland333f3fb2011-04-29 11:36:18 +0200166 }
Johan Herland51670fc2011-04-29 11:36:22 +0200167
Johan Herland333f3fb2011-04-29 11:36:18 +0200168 }
Michael Haggerty02e8ca02012-10-28 17:50:54 +0100169 string_list_clear(&params, 0);
170 free(params_copy);
Johan Herland51670fc2011-04-29 11:36:22 +0200171 return ret;
Johan Herland333f3fb2011-04-29 11:36:18 +0200172}
173
Ramkumar Ramachandrac47ef572012-11-13 21:12:45 +0530174static int parse_submodule_params(struct diff_options *options, const char *value)
175{
176 if (!strcmp(value, "log"))
Jacob Keller61cfbc02016-08-31 16:27:21 -0700177 options->submodule_format = DIFF_SUBMODULE_LOG;
Ramkumar Ramachandrac47ef572012-11-13 21:12:45 +0530178 else if (!strcmp(value, "short"))
Jacob Keller61cfbc02016-08-31 16:27:21 -0700179 options->submodule_format = DIFF_SUBMODULE_SHORT;
Jacob Kellerfd47ae62016-08-31 16:27:25 -0700180 else if (!strcmp(value, "diff"))
181 options->submodule_format = DIFF_SUBMODULE_INLINE_DIFF;
Nguyễn Thái Ngọc Duy5a59a232019-02-16 18:24:41 +0700182 /*
183 * Please update $__git_diff_submodule_formats in
184 * git-completion.bash when you add new formats.
185 */
Ramkumar Ramachandrac47ef572012-11-13 21:12:45 +0530186 else
187 return -1;
188 return 0;
189}
190
Ben Peart85b46032018-05-02 16:01:14 +0000191int git_config_rename(const char *var, const char *value)
Linus Torvaldscced5fb2009-04-09 11:46:15 -0700192{
193 if (!value)
194 return DIFF_DETECT_RENAME;
195 if (!strcasecmp(value, "copies") || !strcasecmp(value, "copy"))
196 return DIFF_DETECT_COPY;
197 return git_config_bool(var,value) ? DIFF_DETECT_RENAME : 0;
198}
199
Michal Privoznik07924d42013-01-16 08:51:58 +0100200long parse_algorithm_value(const char *value)
Michal Privoznik07ab4de2013-01-16 08:51:57 +0100201{
202 if (!value)
203 return -1;
204 else if (!strcasecmp(value, "myers") || !strcasecmp(value, "default"))
205 return 0;
206 else if (!strcasecmp(value, "minimal"))
207 return XDF_NEED_MINIMAL;
208 else if (!strcasecmp(value, "patience"))
209 return XDF_PATIENCE_DIFF;
210 else if (!strcasecmp(value, "histogram"))
211 return XDF_HISTOGRAM_DIFF;
Nguyễn Thái Ngọc Duy5a59a232019-02-16 18:24:41 +0700212 /*
213 * Please update $__git_diff_algorithms in git-completion.bash
214 * when you add new algorithms.
215 */
Michal Privoznik07ab4de2013-01-16 08:51:57 +0100216 return -1;
217}
218
Junio C Hamano0b4b42e2016-10-04 15:09:18 -0700219static int parse_one_token(const char **arg, const char *token)
220{
221 const char *rest;
222 if (skip_prefix(*arg, token, &rest) && (!*rest || *rest == ',')) {
223 *arg = rest;
224 return 1;
225 }
226 return 0;
227}
228
229static int parse_ws_error_highlight(const char *arg)
230{
231 const char *orig_arg = arg;
232 unsigned val = 0;
233
234 while (*arg) {
235 if (parse_one_token(&arg, "none"))
236 val = 0;
237 else if (parse_one_token(&arg, "default"))
238 val = WSEH_NEW;
239 else if (parse_one_token(&arg, "all"))
240 val = WSEH_NEW | WSEH_OLD | WSEH_CONTEXT;
241 else if (parse_one_token(&arg, "new"))
242 val |= WSEH_NEW;
243 else if (parse_one_token(&arg, "old"))
244 val |= WSEH_OLD;
245 else if (parse_one_token(&arg, "context"))
246 val |= WSEH_CONTEXT;
247 else {
248 return -1 - (int)(arg - orig_arg);
249 }
250 if (*arg)
251 arg++;
252 }
253 return val;
254}
255
Junio C Hamanoe0e324a2007-07-07 01:49:58 -0700256/*
Junio C Hamano83ad63c2006-07-08 01:05:16 -0700257 * These are to give UI layer defaults.
258 * The core-level commands such as git-diff-files should
259 * never be affected by the setting of diff.renames
260 * the user happens to have in the configuration file.
261 */
Matthieu Moy5404c112016-02-25 09:59:21 +0100262void init_diff_ui_defaults(void)
263{
Nguyễn Thái Ngọc Duy06dba2b2017-12-27 17:18:35 +0700264 diff_detect_rename_default = DIFF_DETECT_RENAME;
Matthieu Moy5404c112016-02-25 09:59:21 +0100265}
266
Michael Haggerty5b162872016-09-05 11:44:53 +0200267int git_diff_heuristic_config(const char *var, const char *value, void *cb)
268{
Junio C Hamano3cde4e02016-12-23 12:32:22 -0800269 if (!strcmp(var, "diff.indentheuristic"))
Michael Haggerty5b162872016-09-05 11:44:53 +0200270 diff_indent_heuristic = git_config_bool(var, value);
Michael Haggerty5b162872016-09-05 11:44:53 +0200271 return 0;
272}
273
Stefan Beller2e2d5ac2017-06-30 13:53:07 -0700274static int parse_color_moved(const char *arg)
275{
276 switch (git_parse_maybe_bool(arg)) {
277 case 0:
278 return COLOR_MOVED_NO;
279 case 1:
280 return COLOR_MOVED_DEFAULT;
281 default:
282 break;
283 }
284
285 if (!strcmp(arg, "no"))
286 return COLOR_MOVED_NO;
Stefan Beller176841f2017-06-30 13:53:08 -0700287 else if (!strcmp(arg, "plain"))
288 return COLOR_MOVED_PLAIN;
Stefan Beller51da15e2018-07-16 16:05:39 -0700289 else if (!strcmp(arg, "blocks"))
290 return COLOR_MOVED_BLOCKS;
Stefan Beller2e2d5ac2017-06-30 13:53:07 -0700291 else if (!strcmp(arg, "zebra"))
292 return COLOR_MOVED_ZEBRA;
293 else if (!strcmp(arg, "default"))
294 return COLOR_MOVED_DEFAULT;
Eric Sunshinee3f2f5f2018-07-24 17:58:45 -0400295 else if (!strcmp(arg, "dimmed-zebra"))
296 return COLOR_MOVED_ZEBRA_DIM;
Stefan Beller86b452e2017-06-30 13:53:09 -0700297 else if (!strcmp(arg, "dimmed_zebra"))
298 return COLOR_MOVED_ZEBRA_DIM;
Stefan Beller2e2d5ac2017-06-30 13:53:07 -0700299 else
Junio C Hamano706b0b52018-08-15 15:08:22 -0700300 return error(_("color moved setting must be one of 'no', 'default', 'blocks', 'zebra', 'dimmed-zebra', 'plain'"));
Stefan Beller2e2d5ac2017-06-30 13:53:07 -0700301}
302
Stefan Bellerd173e792018-11-13 13:33:57 -0800303static unsigned parse_color_moved_ws(const char *arg)
Stefan Bellerb3095712018-07-16 16:05:40 -0700304{
305 int ret = 0;
306 struct string_list l = STRING_LIST_INIT_DUP;
307 struct string_list_item *i;
308
309 string_list_split(&l, arg, ',', -1);
310
311 for_each_string_list_item(i, &l) {
312 struct strbuf sb = STRBUF_INIT;
313 strbuf_addstr(&sb, i->string);
314 strbuf_trim(&sb);
315
Phillip Woodb73bcba2018-11-23 11:16:52 +0000316 if (!strcmp(sb.buf, "no"))
317 ret = 0;
318 else if (!strcmp(sb.buf, "ignore-space-change"))
Stefan Bellerb3095712018-07-16 16:05:40 -0700319 ret |= XDF_IGNORE_WHITESPACE_CHANGE;
320 else if (!strcmp(sb.buf, "ignore-space-at-eol"))
321 ret |= XDF_IGNORE_WHITESPACE_AT_EOL;
322 else if (!strcmp(sb.buf, "ignore-all-space"))
323 ret |= XDF_IGNORE_WHITESPACE;
Stefan Bellerca1f4ae2018-07-18 12:31:55 -0700324 else if (!strcmp(sb.buf, "allow-indentation-change"))
325 ret |= COLOR_MOVED_WS_ALLOW_INDENTATION_CHANGE;
Stefan Bellerd173e792018-11-13 13:33:57 -0800326 else {
327 ret |= COLOR_MOVED_WS_ERROR;
328 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);
329 }
Stefan Bellerb3095712018-07-16 16:05:40 -0700330
331 strbuf_release(&sb);
332 }
333
Stefan Bellerca1f4ae2018-07-18 12:31:55 -0700334 if ((ret & COLOR_MOVED_WS_ALLOW_INDENTATION_CHANGE) &&
Stefan Bellerd173e792018-11-13 13:33:57 -0800335 (ret & XDF_WHITESPACE_FLAGS)) {
Junio C Hamano15b07cb2019-01-29 12:47:53 -0800336 error(_("color-moved-ws: allow-indentation-change cannot be combined with other whitespace modes"));
Stefan Bellerd173e792018-11-13 13:33:57 -0800337 ret |= COLOR_MOVED_WS_ERROR;
338 }
Stefan Bellerca1f4ae2018-07-18 12:31:55 -0700339
Stefan Bellerb3095712018-07-16 16:05:40 -0700340 string_list_clear(&l, 0);
341
342 return ret;
Jeff Kinge269eb72011-08-17 22:03:48 -0700343}
Junio C Hamano801235c2006-06-24 04:06:23 -0700344
345int git_diff_ui_config(const char *var, const char *value, void *cb)
Jeff Muizelaar6468a4e2012-09-27 15:12:52 -0400346{
347 if (!strcmp(var, "diff.color") || !strcmp(var, "color.diff")) {
348 diff_use_color_default = git_config_colorbool(var, value);
349 return 0;
350 }
Stefan Beller2e2d5ac2017-06-30 13:53:07 -0700351 if (!strcmp(var, "diff.colormoved")) {
352 int cm = parse_color_moved(value);
353 if (cm < 0)
354 return -1;
355 diff_color_moved_default = cm;
356 return 0;
357 }
Stefan Beller626c0b52018-07-18 12:31:56 -0700358 if (!strcmp(var, "diff.colormovedws")) {
Stefan Bellerd173e792018-11-13 13:33:57 -0800359 unsigned cm = parse_color_moved_ws(value);
360 if (cm & COLOR_MOVED_WS_ERROR)
Stefan Beller626c0b52018-07-18 12:31:56 -0700361 return -1;
362 diff_color_moved_ws_default = cm;
363 return 0;
364 }
Jeff Muizelaar6468a4e2012-09-27 15:12:52 -0400365 if (!strcmp(var, "diff.context")) {
366 diff_context_default = git_config_int(var, value);
367 if (diff_context_default < 0)
368 return -1;
369 return 0;
370 }
Vegard Nossumc4888672017-01-12 13:21:11 +0100371 if (!strcmp(var, "diff.interhunkcontext")) {
372 diff_interhunk_context_default = git_config_int(var, value);
373 if (diff_interhunk_context_default < 0)
374 return -1;
375 return 0;
376 }
Eric Wongb68ea122006-07-07 04:01:23 -0700377 if (!strcmp(var, "diff.renames")) {
Linus Torvaldscced5fb2009-04-09 11:46:15 -0700378 diff_detect_rename_default = git_config_rename(var, value);
Eric Wongb68ea122006-07-07 04:01:23 -0700379 return 0;
380 }
Junio C Hamanoaecbf912007-08-31 13:13:42 -0700381 if (!strcmp(var, "diff.autorefreshindex")) {
382 diff_auto_refresh_index = git_config_bool(var, value);
383 return 0;
384 }
Junio C Hamanoa5a818e2008-08-18 20:08:09 -0700385 if (!strcmp(var, "diff.mnemonicprefix")) {
386 diff_mnemonic_prefix = git_config_bool(var, value);
387 return 0;
388 }
Eli Collinsf89504d2010-05-02 19:03:41 -0700389 if (!strcmp(var, "diff.noprefix")) {
390 diff_no_prefix = git_config_bool(var, value);
391 return 0;
392 }
Laurent Arnoudc28ded82020-05-22 12:46:18 +0200393 if (!strcmp(var, "diff.relative")) {
394 diff_relative = git_config_bool(var, value);
395 return 0;
396 }
Zbigniew Jędrzejewski-Szmekdf444832012-03-01 13:26:46 +0100397 if (!strcmp(var, "diff.statgraphwidth")) {
398 diff_stat_graph_width = git_config_int(var, value);
399 return 0;
400 }
Brian Hetrodaec8082008-07-05 01:24:43 -0400401 if (!strcmp(var, "diff.external"))
402 return git_config_string(&external_diff_cmd_cfg, var, value);
Boyd Stephen Smith Jr98a4d872009-01-20 21:46:57 -0600403 if (!strcmp(var, "diff.wordregex"))
404 return git_config_string(&diff_word_regex_cfg, var, value);
Samuel Bronson6d8940b2013-12-18 19:08:12 -0500405 if (!strcmp(var, "diff.orderfile"))
406 return git_config_pathname(&diff_order_file_cfg, var, value);
Junio C Hamanof1af60b2007-04-22 17:52:55 -0700407
Johannes Schindelinbe4f2b42010-08-05 10:49:55 +0200408 if (!strcmp(var, "diff.ignoresubmodules"))
409 handle_ignore_submodules_arg(&default_diff_options, value);
410
Ramkumar Ramachandrac47ef572012-11-13 21:12:45 +0530411 if (!strcmp(var, "diff.submodule")) {
412 if (parse_submodule_params(&default_diff_options, value))
413 warning(_("Unknown value for 'diff.submodule' config variable: '%s'"),
414 value);
415 return 0;
416 }
417
Michal Privoznik07ab4de2013-01-16 08:51:57 +0100418 if (!strcmp(var, "diff.algorithm")) {
419 diff_algorithm = parse_algorithm_value(value);
420 if (diff_algorithm < 0)
421 return -1;
422 return 0;
423 }
424
Jeff King33c643b2017-10-13 13:24:31 -0400425 if (git_color_config(var, value, cb) < 0)
426 return -1;
427
Johannes Schindelinef90d6d2008-05-14 18:46:53 +0100428 return git_diff_basic_config(var, value, cb);
Jeff King9a1805a2008-01-04 03:59:34 -0500429}
430
Johannes Schindelinef90d6d2008-05-14 18:46:53 +0100431int git_diff_basic_config(const char *var, const char *value, void *cb)
Jeff King9a1805a2008-01-04 03:59:34 -0500432{
Jeff Kingae021d82014-06-18 15:47:50 -0400433 const char *name;
434
Linus Torvalds2b6ca6d2008-08-05 11:27:30 -0700435 if (!strcmp(var, "diff.renamelimit")) {
436 diff_rename_limit_default = git_config_int(var, value);
437 return 0;
438 }
439
Jeff King6680a082012-02-07 13:23:02 -0500440 if (userdiff_config(var, value) < 0)
441 return -1;
Jeff Kingc7534ef2008-10-26 00:45:55 -0400442
Jeff Kingae021d82014-06-18 15:47:50 -0400443 if (skip_prefix(var, "diff.color.", &name) ||
444 skip_prefix(var, "color.diff.", &name)) {
445 int slot = parse_diff_color_slot(name);
Jeff King8b8e8622009-12-12 07:25:24 -0500446 if (slot < 0)
447 return 0;
Junio C Hamano64f30e92008-02-11 10:53:56 -0800448 if (!value)
449 return config_error_nonbool(var);
Jeff Kingf6c5a292014-10-07 15:33:09 -0400450 return color_parse(value, diff_colors[slot]);
Junio C Hamano801235c2006-06-24 04:06:23 -0700451 }
Junio C Hamanof1af60b2007-04-22 17:52:55 -0700452
Jeff Kingda806352020-01-31 04:57:49 -0500453 if (!strcmp(var, "diff.wserrorhighlight")) {
454 int val = parse_ws_error_highlight(value);
455 if (val < 0)
456 return -1;
457 ws_error_highlight_default = val;
458 return 0;
459 }
460
Jim Meyeringa624eaa2008-08-15 13:39:26 +0200461 /* like GNU diff's --suppress-blank-empty option */
Johannes Schindelin950db872009-01-20 22:08:33 +0100462 if (!strcmp(var, "diff.suppressblankempty") ||
463 /* for backwards compatibility */
464 !strcmp(var, "diff.suppress-blank-empty")) {
Jim Meyeringa624eaa2008-08-15 13:39:26 +0200465 diff_suppress_blank_empty = git_config_bool(var, value);
466 return 0;
467 }
468
Johan Herland2d174952011-04-29 11:36:19 +0200469 if (!strcmp(var, "diff.dirstat")) {
Johan Herland51670fc2011-04-29 11:36:22 +0200470 struct strbuf errmsg = STRBUF_INIT;
Johan Herland712d2c72011-04-29 11:36:20 +0200471 default_diff_options.dirstat_permille = diff_dirstat_permille_default;
Johan Herland51670fc2011-04-29 11:36:22 +0200472 if (parse_dirstat_params(&default_diff_options, value, &errmsg))
Johan Herland7478ac52011-04-29 11:36:23 +0200473 warning(_("Found errors in 'diff.dirstat' config variable:\n%s"),
Johan Herland51670fc2011-04-29 11:36:22 +0200474 errmsg.buf);
475 strbuf_release(&errmsg);
Johan Herland712d2c72011-04-29 11:36:20 +0200476 diff_dirstat_permille_default = default_diff_options.dirstat_permille;
Johan Herland2d174952011-04-29 11:36:19 +0200477 return 0;
478 }
479
Marc Branchaudcf5e7722017-05-08 12:03:36 -0400480 if (git_diff_heuristic_config(var, value, cb) < 0)
481 return -1;
482
Jeff King3e1dd172011-08-17 22:05:08 -0700483 return git_default_config(var, value, cb);
Junio C Hamano801235c2006-06-24 04:06:23 -0700484}
485
Junio C Hamano6973dca2006-04-21 23:57:45 -0700486static char *quote_two(const char *one, const char *two)
487{
Junio C Hamano7c37c972020-09-10 10:01:59 -0700488 int need_one = quote_c_style(one, NULL, NULL, CQUOTE_NODQ);
489 int need_two = quote_c_style(two, NULL, NULL, CQUOTE_NODQ);
Brandon Caseyf285a2d2008-10-09 14:12:12 -0500490 struct strbuf res = STRBUF_INIT;
Junio C Hamano6973dca2006-04-21 23:57:45 -0700491
492 if (need_one + need_two) {
Pierre Habouzit663af342007-09-20 00:42:15 +0200493 strbuf_addch(&res, '"');
Junio C Hamano7c37c972020-09-10 10:01:59 -0700494 quote_c_style(one, &res, NULL, CQUOTE_NODQ);
495 quote_c_style(two, &res, NULL, CQUOTE_NODQ);
Pierre Habouzit663af342007-09-20 00:42:15 +0200496 strbuf_addch(&res, '"');
497 } else {
498 strbuf_addstr(&res, one);
499 strbuf_addstr(&res, two);
Junio C Hamano6973dca2006-04-21 23:57:45 -0700500 }
Pierre Habouzitb315c5c2007-09-27 12:58:23 +0200501 return strbuf_detach(&res, NULL);
Junio C Hamano6973dca2006-04-21 23:57:45 -0700502}
503
504static const char *external_diff(void)
505{
506 static const char *external_diff_cmd = NULL;
507 static int done_preparing = 0;
508
509 if (done_preparing)
510 return external_diff_cmd;
Kim Gybels6776a842019-01-11 21:26:08 +0100511 external_diff_cmd = xstrdup_or_null(getenv("GIT_EXTERNAL_DIFF"));
Johannes Schindelincbe02102007-12-17 13:42:20 +0000512 if (!external_diff_cmd)
513 external_diff_cmd = external_diff_cmd_cfg;
Junio C Hamano6973dca2006-04-21 23:57:45 -0700514 done_preparing = 1;
515 return external_diff_cmd;
516}
517
Michael Haggerty284098f2015-08-12 19:12:01 +0200518/*
519 * Keep track of files used for diffing. Sometimes such an entry
520 * refers to a temporary file, sometimes to an existing file, and
521 * sometimes to "/dev/null".
522 */
Junio C Hamano6973dca2006-04-21 23:57:45 -0700523static struct diff_tempfile {
Michael Haggerty284098f2015-08-12 19:12:01 +0200524 /*
525 * filename external diff should read from, or NULL if this
526 * entry is currently not in use:
527 */
528 const char *name;
529
brian m. carlsondc015052017-03-26 16:01:24 +0000530 char hex[GIT_MAX_HEXSZ + 1];
Junio C Hamano6973dca2006-04-21 23:57:45 -0700531 char mode[10];
Michael Haggerty284098f2015-08-12 19:12:01 +0200532
533 /*
534 * If this diff_tempfile instance refers to a temporary file,
535 * this tempfile object is used to manage its lifetime.
536 */
Jeff King076aa2c2017-09-05 08:15:08 -0400537 struct tempfile *tempfile;
Junio C Hamano6973dca2006-04-21 23:57:45 -0700538} diff_temp[2];
539
Junio C Hamano6957eb92009-09-14 18:44:01 -0700540struct emit_callback {
Junio C Hamano6957eb92009-09-14 18:44:01 -0700541 int color_diff;
542 unsigned ws_rule;
543 int blank_at_eof_in_preimage;
544 int blank_at_eof_in_postimage;
545 int lno_in_preimage;
546 int lno_in_postimage;
Junio C Hamano6957eb92009-09-14 18:44:01 -0700547 const char **label_path;
548 struct diff_words_data *diff_words;
Bo Yanga3c158d2010-05-26 15:08:02 +0800549 struct diff_options *opt;
Greg Bacon3e97c7c2009-11-19 15:12:24 -0600550 struct strbuf *header;
Junio C Hamano6957eb92009-09-14 18:44:01 -0700551};
552
Junio C Hamano6973dca2006-04-21 23:57:45 -0700553static int count_lines(const char *data, int size)
554{
555 int count, ch, completely_empty = 1, nl_just_seen = 0;
556 count = 0;
557 while (0 < size--) {
558 ch = *data++;
559 if (ch == '\n') {
560 count++;
561 nl_just_seen = 1;
562 completely_empty = 0;
563 }
564 else {
565 nl_just_seen = 0;
566 completely_empty = 0;
567 }
568 }
569 if (completely_empty)
570 return 0;
571 if (!nl_just_seen)
572 count++; /* no trailing newline */
573 return count;
574}
575
Nguyễn Thái Ngọc Duyb78ea5f2018-09-21 17:57:19 +0200576static int fill_mmfile(struct repository *r, mmfile_t *mf,
577 struct diff_filespec *one)
Junio C Hamano6957eb92009-09-14 18:44:01 -0700578{
579 if (!DIFF_FILE_VALID(one)) {
580 mf->ptr = (char *)""; /* does not matter */
581 mf->size = 0;
582 return 0;
583 }
Jonathan Tan1c37e862020-04-07 15:11:41 -0700584 else if (diff_populate_filespec(r, one, NULL))
Junio C Hamano6957eb92009-09-14 18:44:01 -0700585 return -1;
Junio C Hamanobb35fef2009-09-15 03:38:30 -0700586
Junio C Hamano6957eb92009-09-14 18:44:01 -0700587 mf->ptr = one->data;
588 mf->size = one->size;
589 return 0;
590}
591
Jeff Kingabb371a2011-02-19 03:16:32 -0500592/* 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 +0200593static unsigned long diff_filespec_size(struct repository *r,
594 struct diff_filespec *one)
Jeff Kingabb371a2011-02-19 03:16:32 -0500595{
Jonathan Tan1c37e862020-04-07 15:11:41 -0700596 struct diff_populate_filespec_options dpf_options = {
597 .check_size_only = 1,
598 };
599
Jeff Kingabb371a2011-02-19 03:16:32 -0500600 if (!DIFF_FILE_VALID(one))
601 return 0;
Jonathan Tan1c37e862020-04-07 15:11:41 -0700602 diff_populate_filespec(r, one, &dpf_options);
Jeff Kingabb371a2011-02-19 03:16:32 -0500603 return one->size;
604}
605
Junio C Hamano6957eb92009-09-14 18:44:01 -0700606static int count_trailing_blank(mmfile_t *mf, unsigned ws_rule)
607{
608 char *ptr = mf->ptr;
609 long size = mf->size;
610 int cnt = 0;
611
612 if (!size)
613 return cnt;
614 ptr += size - 1; /* pointing at the very end */
615 if (*ptr != '\n')
616 ; /* incomplete line */
617 else
618 ptr--; /* skip the last LF */
619 while (mf->ptr < ptr) {
620 char *prev_eol;
621 for (prev_eol = ptr; mf->ptr <= prev_eol; prev_eol--)
622 if (*prev_eol == '\n')
623 break;
624 if (!ws_blank_line(prev_eol + 1, ptr - prev_eol, ws_rule))
625 break;
626 cnt++;
627 ptr = prev_eol - 1;
628 }
629 return cnt;
630}
631
632static void check_blank_at_eof(mmfile_t *mf1, mmfile_t *mf2,
633 struct emit_callback *ecbdata)
634{
635 int l1, l2, at;
636 unsigned ws_rule = ecbdata->ws_rule;
637 l1 = count_trailing_blank(mf1, ws_rule);
638 l2 = count_trailing_blank(mf2, ws_rule);
639 if (l2 <= l1) {
640 ecbdata->blank_at_eof_in_preimage = 0;
641 ecbdata->blank_at_eof_in_postimage = 0;
642 return;
643 }
644 at = count_lines(mf1->ptr, mf1->size);
645 ecbdata->blank_at_eof_in_preimage = (at - l1) + 1;
646
647 at = count_lines(mf2->ptr, mf2->size);
648 ecbdata->blank_at_eof_in_postimage = (at - l2) + 1;
649}
650
Johannes Schindelinf7c3b4e2018-08-13 04:33:20 -0700651static void emit_line_0(struct diff_options *o,
Stefan Beller017ac452018-08-13 18:41:19 -0700652 const char *set_sign, const char *set, unsigned reverse, const char *reset,
Junio C Hamano250f7992009-09-14 18:44:01 -0700653 int first, const char *line, int len)
Junio C Hamano6957eb92009-09-14 18:44:01 -0700654{
655 int has_trailing_newline, has_trailing_carriage_return;
Stefan Beller44410672018-08-13 18:41:22 -0700656 int needs_reset = 0; /* at the end of the line */
Bo Yanga3c158d2010-05-26 15:08:02 +0800657 FILE *file = o->file;
658
Stefan Bellerf103a6f2018-08-13 18:41:21 -0700659 fputs(diff_line_prefix(o), file);
Junio C Hamano6957eb92009-09-14 18:44:01 -0700660
Stefan Beller44410672018-08-13 18:41:22 -0700661 has_trailing_newline = (len > 0 && line[len-1] == '\n');
662 if (has_trailing_newline)
663 len--;
664
665 has_trailing_carriage_return = (len > 0 && line[len-1] == '\r');
666 if (has_trailing_carriage_return)
667 len--;
668
669 if (!len && !first)
670 goto end_of_line;
671
672 if (reverse && want_color(o->use_color)) {
673 fputs(GIT_COLOR_REVERSE, file);
674 needs_reset = 1;
Junio C Hamano250f7992009-09-14 18:44:01 -0700675 }
Junio C Hamano6957eb92009-09-14 18:44:01 -0700676
Stefan Beller44410672018-08-13 18:41:22 -0700677 if (set_sign) {
678 fputs(set_sign, file);
679 needs_reset = 1;
Junio C Hamano06a47552009-11-27 22:04:10 -0800680 }
Stefan Beller44410672018-08-13 18:41:22 -0700681
Junio C Hamano6957eb92009-09-14 18:44:01 -0700682 if (first)
Stefan Beller44410672018-08-13 18:41:22 -0700683 fputc(first, file);
Junio C Hamano6957eb92009-09-14 18:44:01 -0700684
Stefan Beller44410672018-08-13 18:41:22 -0700685 if (!len)
686 goto end_of_line;
Junio C Hamano6957eb92009-09-14 18:44:01 -0700687
Stefan Beller44410672018-08-13 18:41:22 -0700688 if (set) {
689 if (set_sign && set != set_sign)
690 fputs(reset, file);
Junio C Hamano250f7992009-09-14 18:44:01 -0700691 fputs(set, file);
Stefan Beller44410672018-08-13 18:41:22 -0700692 needs_reset = 1;
Junio C Hamano6957eb92009-09-14 18:44:01 -0700693 }
Stefan Beller44410672018-08-13 18:41:22 -0700694 fwrite(line, len, 1, file);
695 needs_reset = 1; /* 'line' may contain color codes. */
696
697end_of_line:
698 if (needs_reset)
699 fputs(reset, file);
Junio C Hamano6957eb92009-09-14 18:44:01 -0700700 if (has_trailing_carriage_return)
701 fputc('\r', file);
702 if (has_trailing_newline)
703 fputc('\n', file);
704}
705
Bo Yanga3c158d2010-05-26 15:08:02 +0800706static void emit_line(struct diff_options *o, const char *set, const char *reset,
Junio C Hamano250f7992009-09-14 18:44:01 -0700707 const char *line, int len)
708{
Stefan Beller44410672018-08-13 18:41:22 -0700709 emit_line_0(o, set, NULL, 0, reset, 0, line, len);
Junio C Hamano250f7992009-09-14 18:44:01 -0700710}
711
Stefan Beller36a4cef2017-06-29 17:06:49 -0700712enum diff_symbol {
Stefan Beller4eed0eb2017-06-29 17:07:01 -0700713 DIFF_SYMBOL_BINARY_DIFF_HEADER,
714 DIFF_SYMBOL_BINARY_DIFF_HEADER_DELTA,
715 DIFF_SYMBOL_BINARY_DIFF_HEADER_LITERAL,
716 DIFF_SYMBOL_BINARY_DIFF_BODY,
717 DIFF_SYMBOL_BINARY_DIFF_FOOTER,
Stefan Beller0911c472017-06-29 17:07:02 -0700718 DIFF_SYMBOL_STATS_SUMMARY_NO_FILES,
719 DIFF_SYMBOL_STATS_SUMMARY_ABBREV,
720 DIFF_SYMBOL_STATS_SUMMARY_INSERTS_DELETES,
721 DIFF_SYMBOL_STATS_LINE,
Stefan Bellerbd033292017-06-29 17:07:03 -0700722 DIFF_SYMBOL_WORD_DIFF,
Stefan Beller30b7e1e2017-06-29 17:07:04 -0700723 DIFF_SYMBOL_STAT_SEP,
Stefan Beller146fdb02017-06-29 17:07:05 -0700724 DIFF_SYMBOL_SUMMARY,
Stefan Bellerf3597132017-06-29 17:07:00 -0700725 DIFF_SYMBOL_SUBMODULE_ADD,
726 DIFF_SYMBOL_SUBMODULE_DEL,
727 DIFF_SYMBOL_SUBMODULE_UNTRACKED,
728 DIFF_SYMBOL_SUBMODULE_MODIFIED,
729 DIFF_SYMBOL_SUBMODULE_HEADER,
730 DIFF_SYMBOL_SUBMODULE_ERROR,
731 DIFF_SYMBOL_SUBMODULE_PIPETHROUGH,
Stefan Beller5af6ea92017-06-29 17:06:59 -0700732 DIFF_SYMBOL_REWRITE_DIFF,
Stefan Beller4acaaa72017-06-29 17:06:58 -0700733 DIFF_SYMBOL_BINARY_FILES,
Stefan Bellera29b0a12017-06-29 17:06:57 -0700734 DIFF_SYMBOL_HEADER,
Stefan Beller3ee8b7b2017-06-29 17:06:56 -0700735 DIFF_SYMBOL_FILEPAIR_PLUS,
736 DIFF_SYMBOL_FILEPAIR_MINUS,
Stefan Bellerff958672017-06-29 17:06:54 -0700737 DIFF_SYMBOL_WORDS_PORCELAIN,
738 DIFF_SYMBOL_WORDS,
Stefan Beller091f8e22017-06-29 17:06:53 -0700739 DIFF_SYMBOL_CONTEXT,
Stefan Bellerf2bb1212017-06-29 17:06:55 -0700740 DIFF_SYMBOL_CONTEXT_INCOMPLETE,
Stefan Beller091f8e22017-06-29 17:06:53 -0700741 DIFF_SYMBOL_PLUS,
742 DIFF_SYMBOL_MINUS,
Stefan Bellerb9cbfde2017-06-29 17:06:52 -0700743 DIFF_SYMBOL_NO_LF_EOF,
Stefan Beller68abc6f2017-06-29 17:06:51 -0700744 DIFF_SYMBOL_CONTEXT_FRAGINFO,
Stefan Bellerc64b4202017-06-29 17:06:50 -0700745 DIFF_SYMBOL_CONTEXT_MARKER,
Stefan Beller36a4cef2017-06-29 17:06:49 -0700746 DIFF_SYMBOL_SEPARATOR
747};
Stefan Beller091f8e22017-06-29 17:06:53 -0700748/*
749 * Flags for content lines:
750 * 0..12 are whitespace rules
751 * 13-15 are WSEH_NEW | WSEH_OLD | WSEH_CONTEXT
752 * 16 is marking if the line is blank at EOF
753 */
Stefan Beller2e2d5ac2017-06-30 13:53:07 -0700754#define DIFF_SYMBOL_CONTENT_BLANK_LINE_EOF (1<<16)
755#define DIFF_SYMBOL_MOVED_LINE (1<<17)
756#define DIFF_SYMBOL_MOVED_LINE_ALT (1<<18)
Stefan Beller86b452e2017-06-30 13:53:09 -0700757#define DIFF_SYMBOL_MOVED_LINE_UNINTERESTING (1<<19)
Stefan Beller091f8e22017-06-29 17:06:53 -0700758#define DIFF_SYMBOL_CONTENT_WS_MASK (WSEH_NEW | WSEH_OLD | WSEH_CONTEXT | WS_RULE_MASK)
759
Stefan Bellere6e045f2017-06-29 17:07:06 -0700760/*
761 * This struct is used when we need to buffer the output of the diff output.
762 *
763 * NEEDSWORK: Instead of storing a copy of the line, add an offset pointer
764 * into the pre/post image file. This pointer could be a union with the
765 * line pointer. By storing an offset into the file instead of the literal line,
766 * we can decrease the memory footprint for the buffered output. At first we
767 * may want to only have indirection for the content lines, but we could also
768 * enhance the state for emitting prefabricated lines, e.g. the similarity
769 * score line or hunk/file headers would only need to store a number or path
770 * and then the output can be constructed later on depending on state.
771 */
772struct emitted_diff_symbol {
773 const char *line;
774 int len;
775 int flags;
Phillip Wood21536d02018-11-23 11:16:57 +0000776 int indent_off; /* Offset to first non-whitespace character */
777 int indent_width; /* The visual width of the indentation */
Phillip Wood72962e82021-12-09 10:30:09 +0000778 unsigned id;
Stefan Bellere6e045f2017-06-29 17:07:06 -0700779 enum diff_symbol s;
780};
Ævar Arnfjörð Bjarmason9865b6e2021-09-27 14:54:25 +0200781#define EMITTED_DIFF_SYMBOL_INIT { 0 }
Stefan Bellere6e045f2017-06-29 17:07:06 -0700782
783struct emitted_diff_symbols {
784 struct emitted_diff_symbol *buf;
785 int nr, alloc;
786};
Ævar Arnfjörð Bjarmason9865b6e2021-09-27 14:54:25 +0200787#define EMITTED_DIFF_SYMBOLS_INIT { 0 }
Stefan Bellere6e045f2017-06-29 17:07:06 -0700788
789static void append_emitted_diff_symbol(struct diff_options *o,
790 struct emitted_diff_symbol *e)
791{
792 struct emitted_diff_symbol *f;
793
794 ALLOC_GROW(o->emitted_symbols->buf,
795 o->emitted_symbols->nr + 1,
796 o->emitted_symbols->alloc);
797 f = &o->emitted_symbols->buf[o->emitted_symbols->nr++];
798
799 memcpy(f, e, sizeof(struct emitted_diff_symbol));
800 f->line = e->line ? xmemdupz(e->line, e->len) : NULL;
801}
802
Ævar Arnfjörð Bjarmason77e56d52022-03-17 15:55:35 +0100803static void free_emitted_diff_symbols(struct emitted_diff_symbols *e)
804{
805 if (!e)
806 return;
807 free(e->buf);
808 free(e);
809}
810
Stefan Beller2e2d5ac2017-06-30 13:53:07 -0700811struct moved_entry {
Stefan Beller2e2d5ac2017-06-30 13:53:07 -0700812 const struct emitted_diff_symbol *es;
813 struct moved_entry *next_line;
Phillip Wood72962e82021-12-09 10:30:09 +0000814 struct moved_entry *next_match;
Stefan Beller2e2d5ac2017-06-30 13:53:07 -0700815};
816
Phillip Wood74d156f2018-10-04 11:07:41 +0100817struct moved_block {
818 struct moved_entry *match;
Phillip Wood21536d02018-11-23 11:16:57 +0000819 int wsd; /* The whitespace delta of this block */
Phillip Wood74d156f2018-10-04 11:07:41 +0100820};
821
Phillip Wood0cd51e92018-11-23 11:16:58 +0000822#define INDENT_BLANKLINE INT_MIN
823
Phillip Wood21536d02018-11-23 11:16:57 +0000824static void fill_es_indent_data(struct emitted_diff_symbol *es)
825{
Phillip Wood0cd51e92018-11-23 11:16:58 +0000826 unsigned int off = 0, i;
Phillip Wood21536d02018-11-23 11:16:57 +0000827 int width = 0, tab_width = es->flags & WS_TAB_WIDTH_MASK;
828 const char *s = es->line;
829 const int len = es->len;
830
831 /* skip any \v \f \r at start of indentation */
832 while (s[off] == '\f' || s[off] == '\v' ||
833 (s[off] == '\r' && off < len - 1))
834 off++;
835
836 /* calculate the visual width of indentation */
837 while(1) {
838 if (s[off] == ' ') {
839 width++;
840 off++;
841 } else if (s[off] == '\t') {
842 width += tab_width - (width % tab_width);
843 while (s[++off] == '\t')
844 width += tab_width;
845 } else {
846 break;
847 }
848 }
849
Phillip Wood0cd51e92018-11-23 11:16:58 +0000850 /* check if this line is blank */
851 for (i = off; i < len; i++)
852 if (!isspace(s[i]))
853 break;
854
855 if (i == len) {
856 es->indent_width = INDENT_BLANKLINE;
857 es->indent_off = len;
858 } else {
859 es->indent_off = off;
860 es->indent_width = width;
861 }
Phillip Wood74d156f2018-10-04 11:07:41 +0100862}
863
Stefan Bellerca1f4ae2018-07-18 12:31:55 -0700864static int compute_ws_delta(const struct emitted_diff_symbol *a,
Phillip Wood25e61902021-12-09 10:30:07 +0000865 const struct emitted_diff_symbol *b)
Stefan Bellerca1f4ae2018-07-18 12:31:55 -0700866{
Phillip Wood25e61902021-12-09 10:30:07 +0000867 int a_width = a->indent_width,
Phillip Wood21536d02018-11-23 11:16:57 +0000868 b_width = b->indent_width;
Stefan Bellerca1f4ae2018-07-18 12:31:55 -0700869
Phillip Wood25e61902021-12-09 10:30:07 +0000870 if (a_width == INDENT_BLANKLINE && b_width == INDENT_BLANKLINE)
871 return INDENT_BLANKLINE;
Phillip Wood0cd51e92018-11-23 11:16:58 +0000872
Phillip Wood25e61902021-12-09 10:30:07 +0000873 return a_width - b_width;
Stefan Bellerca1f4ae2018-07-18 12:31:55 -0700874}
875
Phillip Wood52d14e12021-12-09 10:30:02 +0000876static int cmp_in_block_with_wsd(const struct moved_entry *cur,
877 const struct emitted_diff_symbol *l,
878 struct moved_block *pmb)
Stefan Bellerca1f4ae2018-07-18 12:31:55 -0700879{
Phillip Wood72962e82021-12-09 10:30:09 +0000880 int a_width = cur->es->indent_width, b_width = l->indent_width;
Phillip Wood21536d02018-11-23 11:16:57 +0000881 int delta;
Stefan Bellerca1f4ae2018-07-18 12:31:55 -0700882
Phillip Wood72962e82021-12-09 10:30:09 +0000883 /* The text of each line must match */
884 if (cur->es->id != l->id)
Stefan Bellerca1f4ae2018-07-18 12:31:55 -0700885 return 1;
886
Phillip Wood72962e82021-12-09 10:30:09 +0000887 /*
888 * If 'l' and 'cur' are both blank then we don't need to check the
889 * indent. We only need to check cur as we know the strings match.
890 * */
891 if (a_width == INDENT_BLANKLINE)
Phillip Wood0cd51e92018-11-23 11:16:58 +0000892 return 0;
Stefan Bellerca1f4ae2018-07-18 12:31:55 -0700893
894 /*
Phillip Wood21536d02018-11-23 11:16:57 +0000895 * The indent changes of the block are known and stored in pmb->wsd;
896 * however we need to check if the indent changes of the current line
Phillip Wood72962e82021-12-09 10:30:09 +0000897 * match those of the current block.
Stefan Bellerca1f4ae2018-07-18 12:31:55 -0700898 */
Phillip Wood52d14e12021-12-09 10:30:02 +0000899 delta = b_width - a_width;
Stefan Bellerca1f4ae2018-07-18 12:31:55 -0700900
Phillip Wood0cd51e92018-11-23 11:16:58 +0000901 /*
902 * If the previous lines of this block were all blank then set its
903 * whitespace delta.
904 */
905 if (pmb->wsd == INDENT_BLANKLINE)
906 pmb->wsd = delta;
Stefan Bellerca1f4ae2018-07-18 12:31:55 -0700907
Phillip Wood72962e82021-12-09 10:30:09 +0000908 return delta != pmb->wsd;
Stefan Bellerca1f4ae2018-07-18 12:31:55 -0700909}
910
Phillip Wood72962e82021-12-09 10:30:09 +0000911struct interned_diff_symbol {
912 struct hashmap_entry ent;
913 struct emitted_diff_symbol *es;
914};
915
916static int interned_diff_symbol_cmp(const void *hashmap_cmp_fn_data,
917 const struct hashmap_entry *eptr,
918 const struct hashmap_entry *entry_or_key,
919 const void *keydata)
Stefan Beller2e2d5ac2017-06-30 13:53:07 -0700920{
Stefan Belleree1df662018-07-16 16:05:38 -0700921 const struct diff_options *diffopt = hashmap_cmp_fn_data;
Phillip Wood25e61902021-12-09 10:30:07 +0000922 const struct emitted_diff_symbol *a, *b;
Stefan Bellerb3095712018-07-16 16:05:40 -0700923 unsigned flags = diffopt->color_moved_ws_handling
924 & XDF_WHITESPACE_FLAGS;
Stefan Belleree1df662018-07-16 16:05:38 -0700925
Phillip Wood72962e82021-12-09 10:30:09 +0000926 a = container_of(eptr, const struct interned_diff_symbol, ent)->es;
927 b = container_of(entry_or_key, const struct interned_diff_symbol, ent)->es;
Eric Wong939af162019-10-06 23:30:37 +0000928
Phillip Wood25e61902021-12-09 10:30:07 +0000929 return !xdiff_compare_lines(a->line + a->indent_off,
930 a->len - a->indent_off,
931 b->line + b->indent_off,
932 b->len - b->indent_off, flags);
Stefan Beller2e2d5ac2017-06-30 13:53:07 -0700933}
934
Phillip Wood72962e82021-12-09 10:30:09 +0000935static void prepare_entry(struct diff_options *o, struct emitted_diff_symbol *l,
936 struct interned_diff_symbol *s)
Stefan Beller2e2d5ac2017-06-30 13:53:07 -0700937{
Stefan Bellerb3095712018-07-16 16:05:40 -0700938 unsigned flags = o->color_moved_ws_handling & XDF_WHITESPACE_FLAGS;
Phillip Wood25e61902021-12-09 10:30:07 +0000939 unsigned int hash = xdiff_hash_string(l->line + l->indent_off,
940 l->len - l->indent_off, flags);
Stefan Beller2e2d5ac2017-06-30 13:53:07 -0700941
Phillip Wood72962e82021-12-09 10:30:09 +0000942 hashmap_entry_init(&s->ent, hash);
943 s->es = l;
Stefan Beller2e2d5ac2017-06-30 13:53:07 -0700944}
945
Phillip Wood72962e82021-12-09 10:30:09 +0000946struct moved_entry_list {
947 struct moved_entry *add, *del;
948};
949
950static struct moved_entry_list *add_lines_to_move_detection(struct diff_options *o,
951 struct mem_pool *entry_mem_pool)
Stefan Beller2e2d5ac2017-06-30 13:53:07 -0700952{
953 struct moved_entry *prev_line = NULL;
Phillip Wood72962e82021-12-09 10:30:09 +0000954 struct mem_pool interned_pool;
955 struct hashmap interned_map;
956 struct moved_entry_list *entry_list = NULL;
957 size_t entry_list_alloc = 0;
958 unsigned id = 0;
Stefan Beller2e2d5ac2017-06-30 13:53:07 -0700959 int n;
Stefan Beller2e2d5ac2017-06-30 13:53:07 -0700960
Phillip Wood72962e82021-12-09 10:30:09 +0000961 hashmap_init(&interned_map, interned_diff_symbol_cmp, o, 8096);
962 mem_pool_init(&interned_pool, 1024 * 1024);
963
964 for (n = 0; n < o->emitted_symbols->nr; n++) {
965 struct interned_diff_symbol key;
966 struct emitted_diff_symbol *l = &o->emitted_symbols->buf[n];
967 struct interned_diff_symbol *s;
968 struct moved_entry *entry;
969
970 if (l->s != DIFF_SYMBOL_PLUS && l->s != DIFF_SYMBOL_MINUS) {
Stefan Beller2e2d5ac2017-06-30 13:53:07 -0700971 prev_line = NULL;
972 continue;
973 }
974
Phillip Wood21536d02018-11-23 11:16:57 +0000975 if (o->color_moved_ws_handling &
976 COLOR_MOVED_WS_ALLOW_INDENTATION_CHANGE)
Phillip Wood72962e82021-12-09 10:30:09 +0000977 fill_es_indent_data(l);
Stefan Beller2e2d5ac2017-06-30 13:53:07 -0700978
Phillip Wood72962e82021-12-09 10:30:09 +0000979 prepare_entry(o, l, &key);
980 s = hashmap_get_entry(&interned_map, &key, ent, &key.ent);
981 if (s) {
982 l->id = s->es->id;
983 } else {
984 l->id = id;
985 ALLOC_GROW_BY(entry_list, id, 1, entry_list_alloc);
986 hashmap_add(&interned_map,
987 memcpy(mem_pool_alloc(&interned_pool,
988 sizeof(key)),
989 &key, sizeof(key)));
990 }
991 entry = mem_pool_alloc(entry_mem_pool, sizeof(*entry));
992 entry->es = l;
993 entry->next_line = NULL;
994 if (prev_line && prev_line->es->s == l->s)
995 prev_line->next_line = entry;
996 prev_line = entry;
997 if (l->s == DIFF_SYMBOL_PLUS) {
998 entry->next_match = entry_list[l->id].add;
999 entry_list[l->id].add = entry;
1000 } else {
1001 entry->next_match = entry_list[l->id].del;
1002 entry_list[l->id].del = entry;
1003 }
Stefan Beller2e2d5ac2017-06-30 13:53:07 -07001004 }
Phillip Wood72962e82021-12-09 10:30:09 +00001005
1006 hashmap_clear(&interned_map);
1007 mem_pool_discard(&interned_pool, 0);
1008
1009 return entry_list;
Stefan Beller2e2d5ac2017-06-30 13:53:07 -07001010}
1011
Stefan Bellere2fe6ab2018-07-16 16:05:41 -07001012static void pmb_advance_or_null(struct diff_options *o,
Phillip Wood08fba102021-12-09 10:30:03 +00001013 struct emitted_diff_symbol *l,
Phillip Wood74d156f2018-10-04 11:07:41 +01001014 struct moved_block *pmb,
Phillip Wood0e488f12021-12-09 10:30:05 +00001015 int *pmb_nr)
Stefan Bellere2fe6ab2018-07-16 16:05:41 -07001016{
Phillip Wood0e488f12021-12-09 10:30:05 +00001017 int i, j;
Phillip Wood08fba102021-12-09 10:30:03 +00001018
Phillip Wood0e488f12021-12-09 10:30:05 +00001019 for (i = 0, j = 0; i < *pmb_nr; i++) {
Phillip Woodff046a02021-12-09 10:30:04 +00001020 int match;
Phillip Wood74d156f2018-10-04 11:07:41 +01001021 struct moved_entry *prev = pmb[i].match;
Stefan Bellere2fe6ab2018-07-16 16:05:41 -07001022 struct moved_entry *cur = (prev && prev->next_line) ?
1023 prev->next_line : NULL;
Stefan Bellere2fe6ab2018-07-16 16:05:41 -07001024
Phillip Woodff046a02021-12-09 10:30:04 +00001025 if (o->color_moved_ws_handling &
1026 COLOR_MOVED_WS_ALLOW_INDENTATION_CHANGE)
1027 match = cur &&
1028 !cmp_in_block_with_wsd(cur, l, &pmb[i]);
1029 else
Phillip Wood72962e82021-12-09 10:30:09 +00001030 match = cur && cur->es->id == l->id;
1031
Phillip Wood0e488f12021-12-09 10:30:05 +00001032 if (match) {
1033 pmb[j] = pmb[i];
1034 pmb[j++].match = cur;
Stefan Beller2e2d5ac2017-06-30 13:53:07 -07001035 }
1036 }
Phillip Wood0e488f12021-12-09 10:30:05 +00001037 *pmb_nr = j;
Stefan Beller2e2d5ac2017-06-30 13:53:07 -07001038}
1039
Phillip Wood7dfe4272021-12-09 10:29:57 +00001040static void fill_potential_moved_blocks(struct diff_options *o,
Phillip Wood7dfe4272021-12-09 10:29:57 +00001041 struct moved_entry *match,
1042 struct emitted_diff_symbol *l,
1043 struct moved_block **pmb_p,
1044 int *pmb_alloc_p, int *pmb_nr_p)
1045
Stefan Bellerca1f4ae2018-07-18 12:31:55 -07001046{
Phillip Wood7dfe4272021-12-09 10:29:57 +00001047 struct moved_block *pmb = *pmb_p;
1048 int pmb_alloc = *pmb_alloc_p, pmb_nr = *pmb_nr_p;
Stefan Bellerca1f4ae2018-07-18 12:31:55 -07001049
Phillip Wood7dfe4272021-12-09 10:29:57 +00001050 /*
1051 * The current line is the start of a new block.
1052 * Setup the set of potential blocks.
1053 */
Phillip Wood72962e82021-12-09 10:30:09 +00001054 for (; match; match = match->next_match) {
Phillip Wood7dfe4272021-12-09 10:29:57 +00001055 ALLOC_GROW(pmb, pmb_nr + 1, pmb_alloc);
1056 if (o->color_moved_ws_handling &
Phillip Wood25e61902021-12-09 10:30:07 +00001057 COLOR_MOVED_WS_ALLOW_INDENTATION_CHANGE)
1058 pmb[pmb_nr].wsd = compute_ws_delta(l, match->es);
1059 else
Phillip Wood7dfe4272021-12-09 10:29:57 +00001060 pmb[pmb_nr].wsd = 0;
Phillip Wood25e61902021-12-09 10:30:07 +00001061 pmb[pmb_nr++].match = match;
Stefan Bellerca1f4ae2018-07-18 12:31:55 -07001062 }
1063
Phillip Wood7dfe4272021-12-09 10:29:57 +00001064 *pmb_p = pmb;
1065 *pmb_alloc_p = pmb_alloc;
1066 *pmb_nr_p = pmb_nr;
Stefan Beller2e2d5ac2017-06-30 13:53:07 -07001067}
1068
Jonathan Tan09153272017-08-15 18:27:38 -07001069/*
1070 * If o->color_moved is COLOR_MOVED_PLAIN, this function does nothing.
1071 *
Jonathan Tanf0b8fb62017-08-15 18:27:39 -07001072 * Otherwise, if the last block has fewer alphanumeric characters than
1073 * COLOR_MOVED_MIN_ALNUM_COUNT, unset DIFF_SYMBOL_MOVED_LINE on all lines in
Jonathan Tan09153272017-08-15 18:27:38 -07001074 * that block.
1075 *
1076 * The last block consists of the (n - block_length)'th line up to but not
1077 * including the nth line.
Jonathan Tanf0b8fb62017-08-15 18:27:39 -07001078 *
Phillip Woodb0a2ba42018-11-23 11:16:55 +00001079 * Returns 0 if the last block is empty or is unset by this function, non zero
1080 * otherwise.
1081 *
Jonathan Tanf0b8fb62017-08-15 18:27:39 -07001082 * NEEDSWORK: This uses the same heuristic as blame_entry_score() in blame.c.
1083 * Think of a way to unify them.
Jonathan Tan09153272017-08-15 18:27:38 -07001084 */
Phillip Woodbea084b2021-12-09 10:29:56 +00001085#define DIFF_SYMBOL_MOVED_LINE_ZEBRA_MASK \
1086 (DIFF_SYMBOL_MOVED_LINE | DIFF_SYMBOL_MOVED_LINE_ALT)
Phillip Woodb0a2ba42018-11-23 11:16:55 +00001087static int adjust_last_block(struct diff_options *o, int n, int block_length)
Jonathan Tan09153272017-08-15 18:27:38 -07001088{
Jonathan Tanf0b8fb62017-08-15 18:27:39 -07001089 int i, alnum_count = 0;
1090 if (o->color_moved == COLOR_MOVED_PLAIN)
Phillip Woodb0a2ba42018-11-23 11:16:55 +00001091 return block_length;
Jonathan Tanf0b8fb62017-08-15 18:27:39 -07001092 for (i = 1; i < block_length + 1; i++) {
1093 const char *c = o->emitted_symbols->buf[n - i].line;
1094 for (; *c; c++) {
1095 if (!isalnum(*c))
1096 continue;
1097 alnum_count++;
1098 if (alnum_count >= COLOR_MOVED_MIN_ALNUM_COUNT)
Phillip Woodb0a2ba42018-11-23 11:16:55 +00001099 return 1;
Jonathan Tanf0b8fb62017-08-15 18:27:39 -07001100 }
1101 }
Jonathan Tan09153272017-08-15 18:27:38 -07001102 for (i = 1; i < block_length + 1; i++)
Phillip Woodbea084b2021-12-09 10:29:56 +00001103 o->emitted_symbols->buf[n - i].flags &= ~DIFF_SYMBOL_MOVED_LINE_ZEBRA_MASK;
Phillip Woodb0a2ba42018-11-23 11:16:55 +00001104 return 0;
Jonathan Tan09153272017-08-15 18:27:38 -07001105}
1106
Stefan Beller2e2d5ac2017-06-30 13:53:07 -07001107/* Find blocks of moved code, delegate actual coloring decision to helper */
1108static void mark_color_as_moved(struct diff_options *o,
Phillip Wood72962e82021-12-09 10:30:09 +00001109 struct moved_entry_list *entry_list)
Stefan Beller2e2d5ac2017-06-30 13:53:07 -07001110{
Phillip Wood74d156f2018-10-04 11:07:41 +01001111 struct moved_block *pmb = NULL; /* potentially moved blocks */
Stefan Beller2e2d5ac2017-06-30 13:53:07 -07001112 int pmb_nr = 0, pmb_alloc = 0;
Phillip Woodb0a2ba42018-11-23 11:16:55 +00001113 int n, flipped_block = 0, block_length = 0;
Phillip Woodeb893522021-12-09 10:30:00 +00001114 enum diff_symbol moved_symbol = DIFF_SYMBOL_BINARY_DIFF_HEADER;
Stefan Beller2e2d5ac2017-06-30 13:53:07 -07001115
1116
1117 for (n = 0; n < o->emitted_symbols->nr; n++) {
Stefan Beller2e2d5ac2017-06-30 13:53:07 -07001118 struct moved_entry *match = NULL;
1119 struct emitted_diff_symbol *l = &o->emitted_symbols->buf[n];
Stefan Beller2e2d5ac2017-06-30 13:53:07 -07001120
1121 switch (l->s) {
1122 case DIFF_SYMBOL_PLUS:
Phillip Wood72962e82021-12-09 10:30:09 +00001123 match = entry_list[l->id].del;
Stefan Beller2e2d5ac2017-06-30 13:53:07 -07001124 break;
1125 case DIFF_SYMBOL_MINUS:
Phillip Wood72962e82021-12-09 10:30:09 +00001126 match = entry_list[l->id].add;
Stefan Beller2e2d5ac2017-06-30 13:53:07 -07001127 break;
1128 default:
Phillip Woodb0a2ba42018-11-23 11:16:55 +00001129 flipped_block = 0;
Stefan Beller2e2d5ac2017-06-30 13:53:07 -07001130 }
1131
Phillip Woodeb893522021-12-09 10:30:00 +00001132 if (pmb_nr && (!match || l->s != moved_symbol)) {
Phillip Wood09906582021-12-09 10:29:58 +00001133 if (!adjust_last_block(o, n, block_length) &&
1134 block_length > 1) {
1135 /*
1136 * Rewind in case there is another match
1137 * starting at the second line of the block
1138 */
1139 match = NULL;
1140 n -= block_length;
1141 }
Stefan Beller2e2d5ac2017-06-30 13:53:07 -07001142 pmb_nr = 0;
1143 block_length = 0;
Phillip Woodb0a2ba42018-11-23 11:16:55 +00001144 flipped_block = 0;
Phillip Woodeb893522021-12-09 10:30:00 +00001145 }
1146 if (!match) {
1147 moved_symbol = DIFF_SYMBOL_BINARY_DIFF_HEADER;
Stefan Beller2e2d5ac2017-06-30 13:53:07 -07001148 continue;
1149 }
1150
Phillip Wood2034b472018-11-23 11:16:54 +00001151 if (o->color_moved == COLOR_MOVED_PLAIN) {
1152 l->flags |= DIFF_SYMBOL_MOVED_LINE;
Stefan Beller176841f2017-06-30 13:53:08 -07001153 continue;
Phillip Wood2034b472018-11-23 11:16:54 +00001154 }
Stefan Beller176841f2017-06-30 13:53:08 -07001155
Phillip Wood0e488f12021-12-09 10:30:05 +00001156 pmb_advance_or_null(o, l, pmb, &pmb_nr);
Stefan Beller2e2d5ac2017-06-30 13:53:07 -07001157
1158 if (pmb_nr == 0) {
Phillip Wood09906582021-12-09 10:29:58 +00001159 int contiguous = adjust_last_block(o, n, block_length);
Stefan Beller2e2d5ac2017-06-30 13:53:07 -07001160
Phillip Wood09906582021-12-09 10:29:58 +00001161 if (!contiguous && block_length > 1)
1162 /*
1163 * Rewind in case there is another match
1164 * starting at the second line of the block
1165 */
1166 n -= block_length;
1167 else
Phillip Wood72962e82021-12-09 10:30:09 +00001168 fill_potential_moved_blocks(o, match, l,
Phillip Wood09906582021-12-09 10:29:58 +00001169 &pmb, &pmb_alloc,
1170 &pmb_nr);
1171
Phillip Woodeb893522021-12-09 10:30:00 +00001172 if (contiguous && pmb_nr && moved_symbol == l->s)
Phillip Woodb0a2ba42018-11-23 11:16:55 +00001173 flipped_block = (flipped_block + 1) % 2;
1174 else
1175 flipped_block = 0;
Jonathan Tanf0b8fb62017-08-15 18:27:39 -07001176
Phillip Woodeb893522021-12-09 10:30:00 +00001177 if (pmb_nr)
1178 moved_symbol = l->s;
1179 else
1180 moved_symbol = DIFF_SYMBOL_BINARY_DIFF_HEADER;
1181
Jonathan Tanf0b8fb62017-08-15 18:27:39 -07001182 block_length = 0;
Stefan Beller2e2d5ac2017-06-30 13:53:07 -07001183 }
1184
Phillip Wood2034b472018-11-23 11:16:54 +00001185 if (pmb_nr) {
1186 block_length++;
Phillip Wood2034b472018-11-23 11:16:54 +00001187 l->flags |= DIFF_SYMBOL_MOVED_LINE;
1188 if (flipped_block && o->color_moved != COLOR_MOVED_BLOCKS)
1189 l->flags |= DIFF_SYMBOL_MOVED_LINE_ALT;
1190 }
Stefan Beller2e2d5ac2017-06-30 13:53:07 -07001191 }
Jonathan Tan09153272017-08-15 18:27:38 -07001192 adjust_last_block(o, n, block_length);
Stefan Beller2e2d5ac2017-06-30 13:53:07 -07001193
1194 free(pmb);
1195}
Stefan Bellere6e045f2017-06-29 17:07:06 -07001196
Stefan Beller86b452e2017-06-30 13:53:09 -07001197static void dim_moved_lines(struct diff_options *o)
1198{
1199 int n;
1200 for (n = 0; n < o->emitted_symbols->nr; n++) {
1201 struct emitted_diff_symbol *prev = (n != 0) ?
1202 &o->emitted_symbols->buf[n - 1] : NULL;
1203 struct emitted_diff_symbol *l = &o->emitted_symbols->buf[n];
1204 struct emitted_diff_symbol *next =
1205 (n < o->emitted_symbols->nr - 1) ?
1206 &o->emitted_symbols->buf[n + 1] : NULL;
1207
1208 /* Not a plus or minus line? */
1209 if (l->s != DIFF_SYMBOL_PLUS && l->s != DIFF_SYMBOL_MINUS)
1210 continue;
1211
1212 /* Not a moved line? */
1213 if (!(l->flags & DIFF_SYMBOL_MOVED_LINE))
1214 continue;
1215
1216 /*
1217 * If prev or next are not a plus or minus line,
1218 * pretend they don't exist
1219 */
1220 if (prev && prev->s != DIFF_SYMBOL_PLUS &&
1221 prev->s != DIFF_SYMBOL_MINUS)
1222 prev = NULL;
1223 if (next && next->s != DIFF_SYMBOL_PLUS &&
1224 next->s != DIFF_SYMBOL_MINUS)
1225 next = NULL;
1226
1227 /* Inside a block? */
1228 if ((prev &&
1229 (prev->flags & DIFF_SYMBOL_MOVED_LINE_ZEBRA_MASK) ==
1230 (l->flags & DIFF_SYMBOL_MOVED_LINE_ZEBRA_MASK)) &&
1231 (next &&
1232 (next->flags & DIFF_SYMBOL_MOVED_LINE_ZEBRA_MASK) ==
1233 (l->flags & DIFF_SYMBOL_MOVED_LINE_ZEBRA_MASK))) {
1234 l->flags |= DIFF_SYMBOL_MOVED_LINE_UNINTERESTING;
1235 continue;
1236 }
1237
1238 /* Check if we are at an interesting bound: */
1239 if (prev && (prev->flags & DIFF_SYMBOL_MOVED_LINE) &&
1240 (prev->flags & DIFF_SYMBOL_MOVED_LINE_ALT) !=
1241 (l->flags & DIFF_SYMBOL_MOVED_LINE_ALT))
1242 continue;
1243 if (next && (next->flags & DIFF_SYMBOL_MOVED_LINE) &&
1244 (next->flags & DIFF_SYMBOL_MOVED_LINE_ALT) !=
1245 (l->flags & DIFF_SYMBOL_MOVED_LINE_ALT))
1246 continue;
1247
1248 /*
1249 * The boundary to prev and next are not interesting,
1250 * so this line is not interesting as a whole
1251 */
1252 l->flags |= DIFF_SYMBOL_MOVED_LINE_UNINTERESTING;
1253 }
1254}
1255
Stefan Beller091f8e22017-06-29 17:06:53 -07001256static void emit_line_ws_markup(struct diff_options *o,
Stefan Beller9d1e16b2018-08-13 18:41:18 -07001257 const char *set_sign, const char *set,
1258 const char *reset,
Stefan Bellerbc9feb02018-10-10 16:24:59 -07001259 int sign_index, const char *line, int len,
Stefan Beller091f8e22017-06-29 17:06:53 -07001260 unsigned ws_rule, int blank_at_eof)
1261{
1262 const char *ws = NULL;
Stefan Bellerbc9feb02018-10-10 16:24:59 -07001263 int sign = o->output_indicators[sign_index];
Stefan Beller091f8e22017-06-29 17:06:53 -07001264
1265 if (o->ws_error_highlight & ws_rule) {
1266 ws = diff_get_color_opt(o, DIFF_WHITESPACE);
1267 if (!*ws)
1268 ws = NULL;
1269 }
1270
Johannes Schindelinf7c3b4e2018-08-13 04:33:20 -07001271 if (!ws && !set_sign)
Stefan Beller017ac452018-08-13 18:41:19 -07001272 emit_line_0(o, set, NULL, 0, reset, sign, line, len);
Johannes Schindelinf7c3b4e2018-08-13 04:33:20 -07001273 else if (!ws) {
Stefan Beller29ef7592018-08-13 18:41:20 -07001274 emit_line_0(o, set_sign, set, !!set_sign, reset, sign, line, len);
Johannes Schindelinf7c3b4e2018-08-13 04:33:20 -07001275 } else if (blank_at_eof)
Stefan Beller091f8e22017-06-29 17:06:53 -07001276 /* Blank line at EOF - paint '+' as well */
Stefan Beller017ac452018-08-13 18:41:19 -07001277 emit_line_0(o, ws, NULL, 0, reset, sign, line, len);
Stefan Beller091f8e22017-06-29 17:06:53 -07001278 else {
1279 /* Emit just the prefix, then the rest. */
Stefan Beller017ac452018-08-13 18:41:19 -07001280 emit_line_0(o, set_sign ? set_sign : set, NULL, !!set_sign, reset,
Johannes Schindelinf7c3b4e2018-08-13 04:33:20 -07001281 sign, "", 0);
Stefan Beller091f8e22017-06-29 17:06:53 -07001282 ws_check_emit(line, len, ws_rule,
1283 o->file, set, reset, ws);
1284 }
1285}
Stefan Beller36a4cef2017-06-29 17:06:49 -07001286
Stefan Bellere6e045f2017-06-29 17:07:06 -07001287static void emit_diff_symbol_from_struct(struct diff_options *o,
1288 struct emitted_diff_symbol *eds)
Stefan Beller36a4cef2017-06-29 17:06:49 -07001289{
Stefan Bellerb9cbfde2017-06-29 17:06:52 -07001290 static const char *nneof = " No newline at end of file\n";
Johannes Schindelinf7c3b4e2018-08-13 04:33:20 -07001291 const char *context, *reset, *set, *set_sign, *meta, *fraginfo;
Stefan Bellere6e045f2017-06-29 17:07:06 -07001292
1293 enum diff_symbol s = eds->s;
1294 const char *line = eds->line;
1295 int len = eds->len;
1296 unsigned flags = eds->flags;
1297
Stefan Beller36a4cef2017-06-29 17:06:49 -07001298 switch (s) {
Stefan Bellerb9cbfde2017-06-29 17:06:52 -07001299 case DIFF_SYMBOL_NO_LF_EOF:
1300 context = diff_get_color_opt(o, DIFF_CONTEXT);
1301 reset = diff_get_color_opt(o, DIFF_RESET);
1302 putc('\n', o->file);
Stefan Beller017ac452018-08-13 18:41:19 -07001303 emit_line_0(o, context, NULL, 0, reset, '\\',
Stefan Bellerb9cbfde2017-06-29 17:06:52 -07001304 nneof, strlen(nneof));
1305 break;
Stefan Bellerf3597132017-06-29 17:07:00 -07001306 case DIFF_SYMBOL_SUBMODULE_HEADER:
1307 case DIFF_SYMBOL_SUBMODULE_ERROR:
1308 case DIFF_SYMBOL_SUBMODULE_PIPETHROUGH:
Stefan Beller0911c472017-06-29 17:07:02 -07001309 case DIFF_SYMBOL_STATS_SUMMARY_INSERTS_DELETES:
Stefan Beller146fdb02017-06-29 17:07:05 -07001310 case DIFF_SYMBOL_SUMMARY:
Stefan Beller0911c472017-06-29 17:07:02 -07001311 case DIFF_SYMBOL_STATS_LINE:
Stefan Beller4eed0eb2017-06-29 17:07:01 -07001312 case DIFF_SYMBOL_BINARY_DIFF_BODY:
Stefan Beller68abc6f2017-06-29 17:06:51 -07001313 case DIFF_SYMBOL_CONTEXT_FRAGINFO:
1314 emit_line(o, "", "", line, len);
1315 break;
Stefan Bellerf2bb1212017-06-29 17:06:55 -07001316 case DIFF_SYMBOL_CONTEXT_INCOMPLETE:
Stefan Bellerc64b4202017-06-29 17:06:50 -07001317 case DIFF_SYMBOL_CONTEXT_MARKER:
1318 context = diff_get_color_opt(o, DIFF_CONTEXT);
1319 reset = diff_get_color_opt(o, DIFF_RESET);
1320 emit_line(o, context, reset, line, len);
1321 break;
Stefan Beller36a4cef2017-06-29 17:06:49 -07001322 case DIFF_SYMBOL_SEPARATOR:
1323 fprintf(o->file, "%s%c",
1324 diff_line_prefix(o),
1325 o->line_termination);
1326 break;
Stefan Beller091f8e22017-06-29 17:06:53 -07001327 case DIFF_SYMBOL_CONTEXT:
1328 set = diff_get_color_opt(o, DIFF_CONTEXT);
1329 reset = diff_get_color_opt(o, DIFF_RESET);
Johannes Schindelinf7c3b4e2018-08-13 04:33:20 -07001330 set_sign = NULL;
1331 if (o->flags.dual_color_diffed_diffs) {
1332 char c = !len ? 0 : line[0];
1333
1334 if (c == '+')
1335 set = diff_get_color_opt(o, DIFF_FILE_NEW);
1336 else if (c == '@')
1337 set = diff_get_color_opt(o, DIFF_FRAGINFO);
1338 else if (c == '-')
1339 set = diff_get_color_opt(o, DIFF_FILE_OLD);
1340 }
Stefan Beller7648b792018-08-17 13:43:52 -07001341 emit_line_ws_markup(o, set_sign, set, reset,
Stefan Bellerbc9feb02018-10-10 16:24:59 -07001342 OUTPUT_INDICATOR_CONTEXT, line, len,
Stefan Beller091f8e22017-06-29 17:06:53 -07001343 flags & (DIFF_SYMBOL_CONTENT_WS_MASK), 0);
1344 break;
1345 case DIFF_SYMBOL_PLUS:
Stefan Beller86b452e2017-06-30 13:53:09 -07001346 switch (flags & (DIFF_SYMBOL_MOVED_LINE |
1347 DIFF_SYMBOL_MOVED_LINE_ALT |
1348 DIFF_SYMBOL_MOVED_LINE_UNINTERESTING)) {
1349 case DIFF_SYMBOL_MOVED_LINE |
1350 DIFF_SYMBOL_MOVED_LINE_ALT |
1351 DIFF_SYMBOL_MOVED_LINE_UNINTERESTING:
1352 set = diff_get_color_opt(o, DIFF_FILE_NEW_MOVED_ALT_DIM);
1353 break;
1354 case DIFF_SYMBOL_MOVED_LINE |
1355 DIFF_SYMBOL_MOVED_LINE_ALT:
Stefan Beller2e2d5ac2017-06-30 13:53:07 -07001356 set = diff_get_color_opt(o, DIFF_FILE_NEW_MOVED_ALT);
Stefan Beller86b452e2017-06-30 13:53:09 -07001357 break;
1358 case DIFF_SYMBOL_MOVED_LINE |
1359 DIFF_SYMBOL_MOVED_LINE_UNINTERESTING:
1360 set = diff_get_color_opt(o, DIFF_FILE_NEW_MOVED_DIM);
1361 break;
1362 case DIFF_SYMBOL_MOVED_LINE:
Stefan Beller2e2d5ac2017-06-30 13:53:07 -07001363 set = diff_get_color_opt(o, DIFF_FILE_NEW_MOVED);
Stefan Beller86b452e2017-06-30 13:53:09 -07001364 break;
1365 default:
Stefan Beller2e2d5ac2017-06-30 13:53:07 -07001366 set = diff_get_color_opt(o, DIFF_FILE_NEW);
Stefan Beller86b452e2017-06-30 13:53:09 -07001367 }
Stefan Beller091f8e22017-06-29 17:06:53 -07001368 reset = diff_get_color_opt(o, DIFF_RESET);
Johannes Schindelinf7c3b4e2018-08-13 04:33:20 -07001369 if (!o->flags.dual_color_diffed_diffs)
1370 set_sign = NULL;
1371 else {
1372 char c = !len ? 0 : line[0];
1373
1374 set_sign = set;
1375 if (c == '-')
Johannes Schindelina7be92a2018-08-13 04:33:32 -07001376 set = diff_get_color_opt(o, DIFF_FILE_OLD_BOLD);
Johannes Schindelinf7c3b4e2018-08-13 04:33:20 -07001377 else if (c == '@')
1378 set = diff_get_color_opt(o, DIFF_FRAGINFO);
Johannes Schindelina7be92a2018-08-13 04:33:32 -07001379 else if (c == '+')
1380 set = diff_get_color_opt(o, DIFF_FILE_NEW_BOLD);
1381 else
1382 set = diff_get_color_opt(o, DIFF_CONTEXT_BOLD);
Johannes Schindelin0b91faa2018-08-13 04:33:24 -07001383 flags &= ~DIFF_SYMBOL_CONTENT_WS_MASK;
Johannes Schindelinf7c3b4e2018-08-13 04:33:20 -07001384 }
Stefan Beller7648b792018-08-17 13:43:52 -07001385 emit_line_ws_markup(o, set_sign, set, reset,
Stefan Bellerbc9feb02018-10-10 16:24:59 -07001386 OUTPUT_INDICATOR_NEW, line, len,
Stefan Beller091f8e22017-06-29 17:06:53 -07001387 flags & DIFF_SYMBOL_CONTENT_WS_MASK,
1388 flags & DIFF_SYMBOL_CONTENT_BLANK_LINE_EOF);
1389 break;
1390 case DIFF_SYMBOL_MINUS:
Stefan Beller86b452e2017-06-30 13:53:09 -07001391 switch (flags & (DIFF_SYMBOL_MOVED_LINE |
1392 DIFF_SYMBOL_MOVED_LINE_ALT |
1393 DIFF_SYMBOL_MOVED_LINE_UNINTERESTING)) {
1394 case DIFF_SYMBOL_MOVED_LINE |
1395 DIFF_SYMBOL_MOVED_LINE_ALT |
1396 DIFF_SYMBOL_MOVED_LINE_UNINTERESTING:
1397 set = diff_get_color_opt(o, DIFF_FILE_OLD_MOVED_ALT_DIM);
1398 break;
1399 case DIFF_SYMBOL_MOVED_LINE |
1400 DIFF_SYMBOL_MOVED_LINE_ALT:
Stefan Beller2e2d5ac2017-06-30 13:53:07 -07001401 set = diff_get_color_opt(o, DIFF_FILE_OLD_MOVED_ALT);
Stefan Beller86b452e2017-06-30 13:53:09 -07001402 break;
1403 case DIFF_SYMBOL_MOVED_LINE |
1404 DIFF_SYMBOL_MOVED_LINE_UNINTERESTING:
1405 set = diff_get_color_opt(o, DIFF_FILE_OLD_MOVED_DIM);
1406 break;
1407 case DIFF_SYMBOL_MOVED_LINE:
Stefan Beller2e2d5ac2017-06-30 13:53:07 -07001408 set = diff_get_color_opt(o, DIFF_FILE_OLD_MOVED);
Stefan Beller86b452e2017-06-30 13:53:09 -07001409 break;
1410 default:
Stefan Beller2e2d5ac2017-06-30 13:53:07 -07001411 set = diff_get_color_opt(o, DIFF_FILE_OLD);
Stefan Beller86b452e2017-06-30 13:53:09 -07001412 }
Stefan Beller091f8e22017-06-29 17:06:53 -07001413 reset = diff_get_color_opt(o, DIFF_RESET);
Johannes Schindelinf7c3b4e2018-08-13 04:33:20 -07001414 if (!o->flags.dual_color_diffed_diffs)
1415 set_sign = NULL;
1416 else {
1417 char c = !len ? 0 : line[0];
1418
1419 set_sign = set;
1420 if (c == '+')
Johannes Schindelina7be92a2018-08-13 04:33:32 -07001421 set = diff_get_color_opt(o, DIFF_FILE_NEW_DIM);
Johannes Schindelinf7c3b4e2018-08-13 04:33:20 -07001422 else if (c == '@')
1423 set = diff_get_color_opt(o, DIFF_FRAGINFO);
Johannes Schindelina7be92a2018-08-13 04:33:32 -07001424 else if (c == '-')
1425 set = diff_get_color_opt(o, DIFF_FILE_OLD_DIM);
1426 else
1427 set = diff_get_color_opt(o, DIFF_CONTEXT_DIM);
Johannes Schindelinf7c3b4e2018-08-13 04:33:20 -07001428 }
Stefan Beller7648b792018-08-17 13:43:52 -07001429 emit_line_ws_markup(o, set_sign, set, reset,
Stefan Bellerbc9feb02018-10-10 16:24:59 -07001430 OUTPUT_INDICATOR_OLD, line, len,
Stefan Beller091f8e22017-06-29 17:06:53 -07001431 flags & DIFF_SYMBOL_CONTENT_WS_MASK, 0);
1432 break;
Stefan Bellerff958672017-06-29 17:06:54 -07001433 case DIFF_SYMBOL_WORDS_PORCELAIN:
1434 context = diff_get_color_opt(o, DIFF_CONTEXT);
1435 reset = diff_get_color_opt(o, DIFF_RESET);
1436 emit_line(o, context, reset, line, len);
1437 fputs("~\n", o->file);
1438 break;
1439 case DIFF_SYMBOL_WORDS:
1440 context = diff_get_color_opt(o, DIFF_CONTEXT);
1441 reset = diff_get_color_opt(o, DIFF_RESET);
1442 /*
1443 * Skip the prefix character, if any. With
1444 * diff_suppress_blank_empty, there may be
1445 * none.
1446 */
1447 if (line[0] != '\n') {
1448 line++;
1449 len--;
1450 }
1451 emit_line(o, context, reset, line, len);
1452 break;
Stefan Beller3ee8b7b2017-06-29 17:06:56 -07001453 case DIFF_SYMBOL_FILEPAIR_PLUS:
1454 meta = diff_get_color_opt(o, DIFF_METAINFO);
1455 reset = diff_get_color_opt(o, DIFF_RESET);
1456 fprintf(o->file, "%s%s+++ %s%s%s\n", diff_line_prefix(o), meta,
1457 line, reset,
1458 strchr(line, ' ') ? "\t" : "");
1459 break;
1460 case DIFF_SYMBOL_FILEPAIR_MINUS:
1461 meta = diff_get_color_opt(o, DIFF_METAINFO);
1462 reset = diff_get_color_opt(o, DIFF_RESET);
1463 fprintf(o->file, "%s%s--- %s%s%s\n", diff_line_prefix(o), meta,
1464 line, reset,
1465 strchr(line, ' ') ? "\t" : "");
1466 break;
Stefan Beller4acaaa72017-06-29 17:06:58 -07001467 case DIFF_SYMBOL_BINARY_FILES:
Stefan Bellera29b0a12017-06-29 17:06:57 -07001468 case DIFF_SYMBOL_HEADER:
1469 fprintf(o->file, "%s", line);
1470 break;
Stefan Beller4eed0eb2017-06-29 17:07:01 -07001471 case DIFF_SYMBOL_BINARY_DIFF_HEADER:
1472 fprintf(o->file, "%sGIT binary patch\n", diff_line_prefix(o));
1473 break;
1474 case DIFF_SYMBOL_BINARY_DIFF_HEADER_DELTA:
1475 fprintf(o->file, "%sdelta %s\n", diff_line_prefix(o), line);
1476 break;
1477 case DIFF_SYMBOL_BINARY_DIFF_HEADER_LITERAL:
1478 fprintf(o->file, "%sliteral %s\n", diff_line_prefix(o), line);
1479 break;
1480 case DIFF_SYMBOL_BINARY_DIFF_FOOTER:
1481 fputs(diff_line_prefix(o), o->file);
1482 fputc('\n', o->file);
1483 break;
Stefan Beller5af6ea92017-06-29 17:06:59 -07001484 case DIFF_SYMBOL_REWRITE_DIFF:
1485 fraginfo = diff_get_color(o->use_color, DIFF_FRAGINFO);
1486 reset = diff_get_color_opt(o, DIFF_RESET);
1487 emit_line(o, fraginfo, reset, line, len);
1488 break;
Stefan Bellerf3597132017-06-29 17:07:00 -07001489 case DIFF_SYMBOL_SUBMODULE_ADD:
1490 set = diff_get_color_opt(o, DIFF_FILE_NEW);
1491 reset = diff_get_color_opt(o, DIFF_RESET);
1492 emit_line(o, set, reset, line, len);
1493 break;
1494 case DIFF_SYMBOL_SUBMODULE_DEL:
1495 set = diff_get_color_opt(o, DIFF_FILE_OLD);
1496 reset = diff_get_color_opt(o, DIFF_RESET);
1497 emit_line(o, set, reset, line, len);
1498 break;
1499 case DIFF_SYMBOL_SUBMODULE_UNTRACKED:
1500 fprintf(o->file, "%sSubmodule %s contains untracked content\n",
1501 diff_line_prefix(o), line);
1502 break;
1503 case DIFF_SYMBOL_SUBMODULE_MODIFIED:
1504 fprintf(o->file, "%sSubmodule %s contains modified content\n",
1505 diff_line_prefix(o), line);
1506 break;
Stefan Beller0911c472017-06-29 17:07:02 -07001507 case DIFF_SYMBOL_STATS_SUMMARY_NO_FILES:
1508 emit_line(o, "", "", " 0 files changed\n",
1509 strlen(" 0 files changed\n"));
1510 break;
1511 case DIFF_SYMBOL_STATS_SUMMARY_ABBREV:
1512 emit_line(o, "", "", " ...\n", strlen(" ...\n"));
1513 break;
Stefan Bellerbd033292017-06-29 17:07:03 -07001514 case DIFF_SYMBOL_WORD_DIFF:
1515 fprintf(o->file, "%.*s", len, line);
1516 break;
Stefan Beller30b7e1e2017-06-29 17:07:04 -07001517 case DIFF_SYMBOL_STAT_SEP:
1518 fputs(o->stat_sep, o->file);
1519 break;
Stefan Beller36a4cef2017-06-29 17:06:49 -07001520 default:
Johannes Schindelin033abf92018-05-02 11:38:39 +02001521 BUG("unknown diff symbol");
Stefan Beller36a4cef2017-06-29 17:06:49 -07001522 }
Stefan Beller36a4cef2017-06-29 17:06:49 -07001523}
1524
Stefan Bellere6e045f2017-06-29 17:07:06 -07001525static void emit_diff_symbol(struct diff_options *o, enum diff_symbol s,
1526 const char *line, int len, unsigned flags)
1527{
Phillip Woodb4a5c5c2021-12-09 10:30:08 +00001528 struct emitted_diff_symbol e = {
1529 .line = line, .len = len, .flags = flags, .s = s
1530 };
Stefan Bellere6e045f2017-06-29 17:07:06 -07001531
1532 if (o->emitted_symbols)
1533 append_emitted_diff_symbol(o, &e);
1534 else
1535 emit_diff_symbol_from_struct(o, &e);
1536}
1537
Stefan Bellerf3597132017-06-29 17:07:00 -07001538void diff_emit_submodule_del(struct diff_options *o, const char *line)
1539{
1540 emit_diff_symbol(o, DIFF_SYMBOL_SUBMODULE_DEL, line, strlen(line), 0);
1541}
1542
1543void diff_emit_submodule_add(struct diff_options *o, const char *line)
1544{
1545 emit_diff_symbol(o, DIFF_SYMBOL_SUBMODULE_ADD, line, strlen(line), 0);
1546}
1547
1548void diff_emit_submodule_untracked(struct diff_options *o, const char *path)
1549{
1550 emit_diff_symbol(o, DIFF_SYMBOL_SUBMODULE_UNTRACKED,
1551 path, strlen(path), 0);
1552}
1553
1554void diff_emit_submodule_modified(struct diff_options *o, const char *path)
1555{
1556 emit_diff_symbol(o, DIFF_SYMBOL_SUBMODULE_MODIFIED,
1557 path, strlen(path), 0);
1558}
1559
1560void diff_emit_submodule_header(struct diff_options *o, const char *header)
1561{
1562 emit_diff_symbol(o, DIFF_SYMBOL_SUBMODULE_HEADER,
1563 header, strlen(header), 0);
1564}
1565
1566void diff_emit_submodule_error(struct diff_options *o, const char *err)
1567{
1568 emit_diff_symbol(o, DIFF_SYMBOL_SUBMODULE_ERROR, err, strlen(err), 0);
1569}
1570
1571void diff_emit_submodule_pipethrough(struct diff_options *o,
1572 const char *line, int len)
1573{
1574 emit_diff_symbol(o, DIFF_SYMBOL_SUBMODULE_PIPETHROUGH, line, len, 0);
1575}
1576
Junio C Hamano6957eb92009-09-14 18:44:01 -07001577static int new_blank_line_at_eof(struct emit_callback *ecbdata, const char *line, int len)
1578{
1579 if (!((ecbdata->ws_rule & WS_BLANK_AT_EOF) &&
1580 ecbdata->blank_at_eof_in_preimage &&
1581 ecbdata->blank_at_eof_in_postimage &&
1582 ecbdata->blank_at_eof_in_preimage <= ecbdata->lno_in_preimage &&
1583 ecbdata->blank_at_eof_in_postimage <= ecbdata->lno_in_postimage))
1584 return 0;
Junio C Hamano018cff72009-09-14 18:44:01 -07001585 return ws_blank_line(line, len, ecbdata->ws_rule);
Junio C Hamano6957eb92009-09-14 18:44:01 -07001586}
1587
Jeff Kinge04df612019-02-14 00:48:13 -05001588static void emit_add_line(struct emit_callback *ecbdata,
Junio C Hamano018cff72009-09-14 18:44:01 -07001589 const char *line, int len)
Junio C Hamano6957eb92009-09-14 18:44:01 -07001590{
Stefan Beller091f8e22017-06-29 17:06:53 -07001591 unsigned flags = WSEH_NEW | ecbdata->ws_rule;
1592 if (new_blank_line_at_eof(ecbdata, line, len))
1593 flags |= DIFF_SYMBOL_CONTENT_BLANK_LINE_EOF;
1594
1595 emit_diff_symbol(ecbdata->opt, DIFF_SYMBOL_PLUS, line, len, flags);
Junio C Hamano6957eb92009-09-14 18:44:01 -07001596}
Junio C Hamano6957eb92009-09-14 18:44:01 -07001597
Jeff Kinge04df612019-02-14 00:48:13 -05001598static void emit_del_line(struct emit_callback *ecbdata,
Junio C Hamano0e383e12015-05-26 09:56:33 -07001599 const char *line, int len)
1600{
Stefan Beller091f8e22017-06-29 17:06:53 -07001601 unsigned flags = WSEH_OLD | ecbdata->ws_rule;
1602 emit_diff_symbol(ecbdata->opt, DIFF_SYMBOL_MINUS, line, len, flags);
Junio C Hamano0e383e12015-05-26 09:56:33 -07001603}
1604
Jeff Kinge04df612019-02-14 00:48:13 -05001605static void emit_context_line(struct emit_callback *ecbdata,
Junio C Hamano0e383e12015-05-26 09:56:33 -07001606 const char *line, int len)
1607{
Stefan Beller091f8e22017-06-29 17:06:53 -07001608 unsigned flags = WSEH_CONTEXT | ecbdata->ws_rule;
1609 emit_diff_symbol(ecbdata->opt, DIFF_SYMBOL_CONTEXT, line, len, flags);
Junio C Hamano6957eb92009-09-14 18:44:01 -07001610}
1611
Bert Wesarg89cb73a2009-11-27 07:55:18 +01001612static void emit_hunk_header(struct emit_callback *ecbdata,
1613 const char *line, int len)
1614{
Jeff King8dbf3eb2015-05-27 16:48:46 -04001615 const char *context = diff_get_color(ecbdata->color_diff, DIFF_CONTEXT);
Bert Wesarg89cb73a2009-11-27 07:55:18 +01001616 const char *frag = diff_get_color(ecbdata->color_diff, DIFF_FRAGINFO);
1617 const char *func = diff_get_color(ecbdata->color_diff, DIFF_FUNCINFO);
1618 const char *reset = diff_get_color(ecbdata->color_diff, DIFF_RESET);
Johannes Schindelinf7c3b4e2018-08-13 04:33:20 -07001619 const char *reverse = ecbdata->color_diff ? GIT_COLOR_REVERSE : "";
Bert Wesarg89cb73a2009-11-27 07:55:18 +01001620 static const char atat[2] = { '@', '@' };
1621 const char *cp, *ep;
Bo Yang2efcc972010-05-29 23:32:05 +08001622 struct strbuf msgbuf = STRBUF_INIT;
1623 int org_len = len;
1624 int i = 1;
Bert Wesarg89cb73a2009-11-27 07:55:18 +01001625
1626 /*
1627 * As a hunk header must begin with "@@ -<old>, +<new> @@",
1628 * it always is at least 10 bytes long.
1629 */
1630 if (len < 10 ||
1631 memcmp(line, atat, 2) ||
1632 !(ep = memmem(line + 2, len - 2, atat, 2))) {
Stefan Bellerc64b4202017-06-29 17:06:50 -07001633 emit_diff_symbol(ecbdata->opt,
Stefan Beller091f8e22017-06-29 17:06:53 -07001634 DIFF_SYMBOL_CONTEXT_MARKER, line, len, 0);
Bert Wesarg89cb73a2009-11-27 07:55:18 +01001635 return;
1636 }
1637 ep += 2; /* skip over @@ */
1638
1639 /* The hunk header in fraginfo color */
Johannes Schindelinf7c3b4e2018-08-13 04:33:20 -07001640 if (ecbdata->opt->flags.dual_color_diffed_diffs)
1641 strbuf_addstr(&msgbuf, reverse);
René Scharfecedc61a2014-07-17 01:38:18 +02001642 strbuf_addstr(&msgbuf, frag);
Thomas Gummerer430be362019-07-11 17:08:48 +01001643 if (ecbdata->opt->flags.suppress_hunk_header_line_count)
1644 strbuf_add(&msgbuf, atat, sizeof(atat));
1645 else
1646 strbuf_add(&msgbuf, line, ep - line);
René Scharfecedc61a2014-07-17 01:38:18 +02001647 strbuf_addstr(&msgbuf, reset);
Bo Yang2efcc972010-05-29 23:32:05 +08001648
1649 /*
1650 * trailing "\r\n"
1651 */
1652 for ( ; i < 3; i++)
1653 if (line[len - i] == '\r' || line[len - i] == '\n')
1654 len--;
Bert Wesarg89cb73a2009-11-27 07:55:18 +01001655
1656 /* blank before the func header */
1657 for (cp = ep; ep - line < len; ep++)
1658 if (*ep != ' ' && *ep != '\t')
1659 break;
Bo Yang2efcc972010-05-29 23:32:05 +08001660 if (ep != cp) {
Jeff King8dbf3eb2015-05-27 16:48:46 -04001661 strbuf_addstr(&msgbuf, context);
Bo Yang2efcc972010-05-29 23:32:05 +08001662 strbuf_add(&msgbuf, cp, ep - cp);
René Scharfecedc61a2014-07-17 01:38:18 +02001663 strbuf_addstr(&msgbuf, reset);
Bo Yang2efcc972010-05-29 23:32:05 +08001664 }
Bert Wesarg89cb73a2009-11-27 07:55:18 +01001665
Bo Yang2efcc972010-05-29 23:32:05 +08001666 if (ep < line + len) {
René Scharfecedc61a2014-07-17 01:38:18 +02001667 strbuf_addstr(&msgbuf, func);
Bo Yang2efcc972010-05-29 23:32:05 +08001668 strbuf_add(&msgbuf, ep, line + len - ep);
René Scharfecedc61a2014-07-17 01:38:18 +02001669 strbuf_addstr(&msgbuf, reset);
Bo Yang2efcc972010-05-29 23:32:05 +08001670 }
1671
1672 strbuf_add(&msgbuf, line + len, org_len - len);
Stefan Bellerdfb77282017-06-29 17:06:47 -07001673 strbuf_complete_line(&msgbuf);
Stefan Beller68abc6f2017-06-29 17:06:51 -07001674 emit_diff_symbol(ecbdata->opt,
Stefan Beller091f8e22017-06-29 17:06:53 -07001675 DIFF_SYMBOL_CONTEXT_FRAGINFO, msgbuf.buf, msgbuf.len, 0);
Bo Yang2efcc972010-05-29 23:32:05 +08001676 strbuf_release(&msgbuf);
Bert Wesarg89cb73a2009-11-27 07:55:18 +01001677}
1678
Nguyễn Thái Ngọc Duy3b335762018-12-09 11:25:21 +01001679static struct diff_tempfile *claim_diff_tempfile(void)
1680{
Jeff King479b0ae2009-01-22 00:59:56 -05001681 int i;
1682 for (i = 0; i < ARRAY_SIZE(diff_temp); i++)
1683 if (!diff_temp[i].name)
1684 return diff_temp + i;
Johannes Schindelin033abf92018-05-02 11:38:39 +02001685 BUG("diff is failing to clean up its tempfiles");
Jeff King479b0ae2009-01-22 00:59:56 -05001686}
1687
Jeff King479b0ae2009-01-22 00:59:56 -05001688static void remove_tempfile(void)
1689{
1690 int i;
Nazri Ramliya8344ab2009-02-12 21:36:14 +08001691 for (i = 0; i < ARRAY_SIZE(diff_temp); i++) {
Jeff King076aa2c2017-09-05 08:15:08 -04001692 if (is_tempfile_active(diff_temp[i].tempfile))
Michael Haggerty284098f2015-08-12 19:12:01 +02001693 delete_tempfile(&diff_temp[i].tempfile);
Nazri Ramliya8344ab2009-02-12 21:36:14 +08001694 diff_temp[i].name = NULL;
1695 }
Jeff King479b0ae2009-01-22 00:59:56 -05001696}
1697
Stefan Beller5af6ea92017-06-29 17:06:59 -07001698static void add_line_count(struct strbuf *out, int count)
Junio C Hamano6973dca2006-04-21 23:57:45 -07001699{
1700 switch (count) {
1701 case 0:
Stefan Beller5af6ea92017-06-29 17:06:59 -07001702 strbuf_addstr(out, "0,0");
Junio C Hamano6973dca2006-04-21 23:57:45 -07001703 break;
1704 case 1:
Stefan Beller5af6ea92017-06-29 17:06:59 -07001705 strbuf_addstr(out, "1");
Junio C Hamano6973dca2006-04-21 23:57:45 -07001706 break;
1707 default:
Stefan Beller5af6ea92017-06-29 17:06:59 -07001708 strbuf_addf(out, "1,%d", count);
Junio C Hamano6973dca2006-04-21 23:57:45 -07001709 break;
1710 }
1711}
1712
Junio C Hamano7f7ee2f2009-09-14 18:44:01 -07001713static void emit_rewrite_lines(struct emit_callback *ecb,
1714 int prefix, const char *data, int size)
Junio C Hamano6973dca2006-04-21 23:57:45 -07001715{
Junio C Hamano7f7ee2f2009-09-14 18:44:01 -07001716 const char *endp = NULL;
Junio C Hamano7f7ee2f2009-09-14 18:44:01 -07001717
1718 while (0 < size) {
1719 int len;
1720
1721 endp = memchr(data, '\n', size);
1722 len = endp ? (endp - data + 1) : size;
1723 if (prefix != '+') {
1724 ecb->lno_in_preimage++;
Jeff Kinge04df612019-02-14 00:48:13 -05001725 emit_del_line(ecb, data, len);
Junio C Hamano7f7ee2f2009-09-14 18:44:01 -07001726 } else {
1727 ecb->lno_in_postimage++;
Jeff Kinge04df612019-02-14 00:48:13 -05001728 emit_add_line(ecb, data, len);
Johannes Schindelin13e36ec2007-02-20 15:08:46 +01001729 }
Junio C Hamano7f7ee2f2009-09-14 18:44:01 -07001730 size -= len;
1731 data += len;
Junio C Hamano6973dca2006-04-21 23:57:45 -07001732 }
Stefan Bellerb9cbfde2017-06-29 17:06:52 -07001733 if (!endp)
Stefan Beller091f8e22017-06-29 17:06:53 -07001734 emit_diff_symbol(ecb->opt, DIFF_SYMBOL_NO_LF_EOF, NULL, 0, 0);
Junio C Hamano6973dca2006-04-21 23:57:45 -07001735}
1736
1737static void emit_rewrite_diff(const char *name_a,
1738 const char *name_b,
1739 struct diff_filespec *one,
Johannes Schindelin13e36ec2007-02-20 15:08:46 +01001740 struct diff_filespec *two,
Jeff Kingd9bae1a2010-04-01 20:12:15 -04001741 struct userdiff_driver *textconv_one,
1742 struct userdiff_driver *textconv_two,
Johannes Schindelineab9a402007-12-18 19:32:14 +00001743 struct diff_options *o)
Junio C Hamano6973dca2006-04-21 23:57:45 -07001744{
1745 int lc_a, lc_b;
Junio C Hamanod5625092007-12-26 17:13:36 -08001746 static struct strbuf a_name = STRBUF_INIT, b_name = STRBUF_INIT;
Junio C Hamanoa5a818e2008-08-18 20:08:09 -07001747 const char *a_prefix, *b_prefix;
Jeff King840383b2010-04-01 20:09:26 -04001748 char *data_one, *data_two;
Jeff King3aa1f7c2008-12-09 03:13:21 -05001749 size_t size_one, size_two;
Junio C Hamano7f7ee2f2009-09-14 18:44:01 -07001750 struct emit_callback ecbdata;
Stefan Beller5af6ea92017-06-29 17:06:59 -07001751 struct strbuf out = STRBUF_INIT;
Junio C Hamanoa5a818e2008-08-18 20:08:09 -07001752
Brandon Williams0d1e0e72017-10-31 11:19:11 -07001753 if (diff_mnemonic_prefix && o->flags.reverse_diff) {
Junio C Hamanoa5a818e2008-08-18 20:08:09 -07001754 a_prefix = o->b_prefix;
1755 b_prefix = o->a_prefix;
1756 } else {
1757 a_prefix = o->a_prefix;
1758 b_prefix = o->b_prefix;
1759 }
Junio C Hamano1a9eb3b2006-09-22 16:17:58 -07001760
Junio C Hamano8a13bec2007-02-24 01:42:06 -08001761 name_a += (*name_a == '/');
1762 name_b += (*name_b == '/');
Junio C Hamano1a9eb3b2006-09-22 16:17:58 -07001763
Junio C Hamanod5625092007-12-26 17:13:36 -08001764 strbuf_reset(&a_name);
1765 strbuf_reset(&b_name);
Junio C Hamanoa5a818e2008-08-18 20:08:09 -07001766 quote_two_c_style(&a_name, a_prefix, name_a, 0);
1767 quote_two_c_style(&b_name, b_prefix, name_b, 0);
Junio C Hamanod5625092007-12-26 17:13:36 -08001768
Nguyễn Thái Ngọc Duy6afaf802018-09-21 17:57:22 +02001769 size_one = fill_textconv(o->repo, textconv_one, one, &data_one);
1770 size_two = fill_textconv(o->repo, textconv_two, two, &data_two);
Jeff King3aa1f7c2008-12-09 03:13:21 -05001771
Junio C Hamanod91ba8f2009-09-15 11:21:10 -07001772 memset(&ecbdata, 0, sizeof(ecbdata));
Jeff Kingdaa0c3d2011-08-17 22:04:23 -07001773 ecbdata.color_diff = want_color(o->use_color);
Nguyễn Thái Ngọc Duy26d024e2018-09-21 17:57:37 +02001774 ecbdata.ws_rule = whitespace_rule(o->repo->index, name_b);
Bo Yanga3c158d2010-05-26 15:08:02 +08001775 ecbdata.opt = o;
Junio C Hamanod91ba8f2009-09-15 11:21:10 -07001776 if (ecbdata.ws_rule & WS_BLANK_AT_EOF) {
1777 mmfile_t mf1, mf2;
1778 mf1.ptr = (char *)data_one;
1779 mf2.ptr = (char *)data_two;
1780 mf1.size = size_one;
1781 mf2.size = size_two;
1782 check_blank_at_eof(&mf1, &mf2, &ecbdata);
1783 }
1784 ecbdata.lno_in_preimage = 1;
1785 ecbdata.lno_in_postimage = 1;
1786
Jeff King3aa1f7c2008-12-09 03:13:21 -05001787 lc_a = count_lines(data_one, size_one);
1788 lc_b = count_lines(data_two, size_two);
Stefan Beller3ee8b7b2017-06-29 17:06:56 -07001789
1790 emit_diff_symbol(o, DIFF_SYMBOL_FILEPAIR_MINUS,
1791 a_name.buf, a_name.len, 0);
1792 emit_diff_symbol(o, DIFF_SYMBOL_FILEPAIR_PLUS,
1793 b_name.buf, b_name.len, 0);
1794
Stefan Beller5af6ea92017-06-29 17:06:59 -07001795 strbuf_addstr(&out, "@@ -");
Junio C Hamano467ddc12011-02-28 16:11:55 -08001796 if (!o->irreversible_delete)
Stefan Beller5af6ea92017-06-29 17:06:59 -07001797 add_line_count(&out, lc_a);
Junio C Hamano467ddc12011-02-28 16:11:55 -08001798 else
Stefan Beller5af6ea92017-06-29 17:06:59 -07001799 strbuf_addstr(&out, "?,?");
1800 strbuf_addstr(&out, " +");
1801 add_line_count(&out, lc_b);
1802 strbuf_addstr(&out, " @@\n");
1803 emit_diff_symbol(o, DIFF_SYMBOL_REWRITE_DIFF, out.buf, out.len, 0);
1804 strbuf_release(&out);
1805
Junio C Hamano467ddc12011-02-28 16:11:55 -08001806 if (lc_a && !o->irreversible_delete)
Junio C Hamanod91ba8f2009-09-15 11:21:10 -07001807 emit_rewrite_lines(&ecbdata, '-', data_one, size_one);
Junio C Hamano6973dca2006-04-21 23:57:45 -07001808 if (lc_b)
Junio C Hamanod91ba8f2009-09-15 11:21:10 -07001809 emit_rewrite_lines(&ecbdata, '+', data_two, size_two);
Jeff Kingb76c0562010-04-01 20:04:14 -04001810 if (textconv_one)
Junio C Hamanoaed6ca52010-04-08 23:30:49 -07001811 free((char *)data_one);
Jeff Kingb76c0562010-04-01 20:04:14 -04001812 if (textconv_two)
Junio C Hamanoaed6ca52010-04-08 23:30:49 -07001813 free((char *)data_two);
Junio C Hamano6973dca2006-04-21 23:57:45 -07001814}
1815
Johannes Schindelinf59a59e2006-07-28 23:56:15 +02001816struct diff_words_buffer {
1817 mmfile_t text;
Ramsay Jones071bcaa2017-09-21 17:49:38 +01001818 unsigned long alloc;
Johannes Schindelin2e5d2002009-01-17 17:29:44 +01001819 struct diff_words_orig {
1820 const char *begin, *end;
1821 } *orig;
1822 int orig_nr, orig_alloc;
Johannes Schindelinf59a59e2006-07-28 23:56:15 +02001823};
1824
1825static void diff_words_append(char *line, unsigned long len,
1826 struct diff_words_buffer *buffer)
1827{
Johannes Schindelin23c15752009-01-17 17:29:43 +01001828 ALLOC_GROW(buffer->text.ptr, buffer->text.size + len, buffer->alloc);
Johannes Schindelinf59a59e2006-07-28 23:56:15 +02001829 line++;
1830 len--;
1831 memcpy(buffer->text.ptr + buffer->text.size, line, len);
1832 buffer->text.size += len;
Johannes Schindelin2b6a5412009-01-17 17:29:45 +01001833 buffer->text.ptr[buffer->text.size] = '\0';
Johannes Schindelinf59a59e2006-07-28 23:56:15 +02001834}
1835
Jonathan Nieder9cba13c2011-03-16 02:08:34 -05001836struct diff_words_style_elem {
Thomas Rast882749a2010-04-14 17:59:06 +02001837 const char *prefix;
1838 const char *suffix;
1839 const char *color; /* NULL; filled in by the setup code if
1840 * color is enabled */
1841};
1842
Jonathan Nieder9cba13c2011-03-16 02:08:34 -05001843struct diff_words_style {
Thomas Rast882749a2010-04-14 17:59:06 +02001844 enum diff_words_type type;
Brandon Williams63a01c32018-02-14 10:59:39 -08001845 struct diff_words_style_elem new_word, old_word, ctx;
Thomas Rast882749a2010-04-14 17:59:06 +02001846 const char *newline;
1847};
1848
Stephen Boydc2e86ad2011-03-22 00:51:05 -07001849static struct diff_words_style diff_words_styles[] = {
Thomas Rast882749a2010-04-14 17:59:06 +02001850 { DIFF_WORDS_PORCELAIN, {"+", "\n"}, {"-", "\n"}, {" ", "\n"}, "~\n" },
1851 { DIFF_WORDS_PLAIN, {"{+", "+}"}, {"[-", "-]"}, {"", ""}, "\n" },
1852 { DIFF_WORDS_COLOR, {"", ""}, {"", ""}, {"", ""}, "\n" }
1853};
1854
Johannes Schindelinf59a59e2006-07-28 23:56:15 +02001855struct diff_words_data {
Johannes Schindelinf59a59e2006-07-28 23:56:15 +02001856 struct diff_words_buffer minus, plus;
Johannes Schindelin2e5d2002009-01-17 17:29:44 +01001857 const char *current_plus;
Bo Yang4297c0a2010-05-29 23:32:06 +08001858 int last_minus;
1859 struct diff_options *opt;
Johannes Schindelin2b6a5412009-01-17 17:29:45 +01001860 regex_t *word_regex;
Thomas Rast882749a2010-04-14 17:59:06 +02001861 enum diff_words_type type;
1862 struct diff_words_style *style;
Johannes Schindelinf59a59e2006-07-28 23:56:15 +02001863};
1864
Stefan Bellerbd033292017-06-29 17:07:03 -07001865static int fn_out_diff_words_write_helper(struct diff_options *o,
Thomas Rast882749a2010-04-14 17:59:06 +02001866 struct diff_words_style_elem *st_el,
1867 const char *newline,
Stefan Bellerbd033292017-06-29 17:07:03 -07001868 size_t count, const char *buf)
Thomas Rast882749a2010-04-14 17:59:06 +02001869{
Bo Yang4297c0a2010-05-29 23:32:06 +08001870 int print = 0;
Stefan Bellerbd033292017-06-29 17:07:03 -07001871 struct strbuf sb = STRBUF_INIT;
Bo Yang4297c0a2010-05-29 23:32:06 +08001872
Thomas Rast882749a2010-04-14 17:59:06 +02001873 while (count) {
1874 char *p = memchr(buf, '\n', count);
Bo Yang4297c0a2010-05-29 23:32:06 +08001875 if (print)
Stefan Bellerbd033292017-06-29 17:07:03 -07001876 strbuf_addstr(&sb, diff_line_prefix(o));
1877
Thomas Rast882749a2010-04-14 17:59:06 +02001878 if (p != buf) {
Stefan Bellerbd033292017-06-29 17:07:03 -07001879 const char *reset = st_el->color && *st_el->color ?
1880 GIT_COLOR_RESET : NULL;
1881 if (st_el->color && *st_el->color)
1882 strbuf_addstr(&sb, st_el->color);
1883 strbuf_addstr(&sb, st_el->prefix);
1884 strbuf_add(&sb, buf, p ? p - buf : count);
1885 strbuf_addstr(&sb, st_el->suffix);
1886 if (reset)
1887 strbuf_addstr(&sb, reset);
Thomas Rast882749a2010-04-14 17:59:06 +02001888 }
1889 if (!p)
Stefan Bellerbd033292017-06-29 17:07:03 -07001890 goto out;
1891
1892 strbuf_addstr(&sb, newline);
Thomas Rast882749a2010-04-14 17:59:06 +02001893 count -= p + 1 - buf;
1894 buf = p + 1;
Bo Yang4297c0a2010-05-29 23:32:06 +08001895 print = 1;
Stefan Bellerbd033292017-06-29 17:07:03 -07001896 if (count) {
1897 emit_diff_symbol(o, DIFF_SYMBOL_WORD_DIFF,
1898 sb.buf, sb.len, 0);
1899 strbuf_reset(&sb);
1900 }
Thomas Rast882749a2010-04-14 17:59:06 +02001901 }
Stefan Bellerbd033292017-06-29 17:07:03 -07001902
1903out:
1904 if (sb.len)
1905 emit_diff_symbol(o, DIFF_SYMBOL_WORD_DIFF,
1906 sb.buf, sb.len, 0);
1907 strbuf_release(&sb);
Thomas Rast882749a2010-04-14 17:59:06 +02001908 return 0;
1909}
1910
Bo Yang4297c0a2010-05-29 23:32:06 +08001911/*
1912 * '--color-words' algorithm can be described as:
1913 *
René Genz56217602017-04-30 17:48:27 +02001914 * 1. collect the minus/plus lines of a diff hunk, divided into
Bo Yang4297c0a2010-05-29 23:32:06 +08001915 * minus-lines and plus-lines;
1916 *
1917 * 2. break both minus-lines and plus-lines into words and
1918 * place them into two mmfile_t with one word for each line;
1919 *
1920 * 3. use xdiff to run diff on the two mmfile_t to get the words level diff;
1921 *
1922 * And for the common parts of the both file, we output the plus side text.
1923 * diff_words->current_plus is used to trace the current position of the plus file
1924 * which printed. diff_words->last_minus is used to trace the last minus word
1925 * printed.
1926 *
1927 * For '--graph' to work with '--color-words', we need to output the graph prefix
1928 * on each line of color words output. Generally, there are two conditions on
1929 * which we should output the prefix.
1930 *
1931 * 1. diff_words->last_minus == 0 &&
1932 * diff_words->current_plus == diff_words->plus.text.ptr
1933 *
1934 * that is: the plus text must start as a new line, and if there is no minus
1935 * word printed, a graph prefix must be printed.
1936 *
1937 * 2. diff_words->current_plus > diff_words->plus.text.ptr &&
1938 * *(diff_words->current_plus - 1) == '\n'
1939 *
1940 * that is: a graph prefix must be printed following a '\n'
1941 */
1942static int color_words_output_graph_prefix(struct diff_words_data *diff_words)
1943{
1944 if ((diff_words->last_minus == 0 &&
1945 diff_words->current_plus == diff_words->plus.text.ptr) ||
1946 (diff_words->current_plus > diff_words->plus.text.ptr &&
1947 *(diff_words->current_plus - 1) == '\n')) {
1948 return 1;
1949 } else {
1950 return 0;
1951 }
1952}
1953
Jeff King7c61e252018-11-02 02:37:18 -04001954static void fn_out_diff_words_aux(void *priv,
1955 long minus_first, long minus_len,
1956 long plus_first, long plus_len,
1957 const char *func, long funclen)
Johannes Schindelinf59a59e2006-07-28 23:56:15 +02001958{
1959 struct diff_words_data *diff_words = priv;
Thomas Rast882749a2010-04-14 17:59:06 +02001960 struct diff_words_style *style = diff_words->style;
Johannes Schindelin2e5d2002009-01-17 17:29:44 +01001961 const char *minus_begin, *minus_end, *plus_begin, *plus_end;
Bo Yang4297c0a2010-05-29 23:32:06 +08001962 struct diff_options *opt = diff_words->opt;
John Keeping30997bb2013-02-07 20:15:27 +00001963 const char *line_prefix;
Johannes Schindelinf59a59e2006-07-28 23:56:15 +02001964
Bo Yang4297c0a2010-05-29 23:32:06 +08001965 assert(opt);
John Keeping30997bb2013-02-07 20:15:27 +00001966 line_prefix = diff_line_prefix(opt);
Bo Yang4297c0a2010-05-29 23:32:06 +08001967
Johannes Schindelin2e5d2002009-01-17 17:29:44 +01001968 /* POSIX requires that first be decremented by one if len == 0... */
1969 if (minus_len) {
1970 minus_begin = diff_words->minus.orig[minus_first].begin;
1971 minus_end =
1972 diff_words->minus.orig[minus_first + minus_len - 1].end;
1973 } else
1974 minus_begin = minus_end =
1975 diff_words->minus.orig[minus_first].end;
1976
1977 if (plus_len) {
1978 plus_begin = diff_words->plus.orig[plus_first].begin;
1979 plus_end = diff_words->plus.orig[plus_first + plus_len - 1].end;
1980 } else
1981 plus_begin = plus_end = diff_words->plus.orig[plus_first].end;
1982
Bo Yang4297c0a2010-05-29 23:32:06 +08001983 if (color_words_output_graph_prefix(diff_words)) {
1984 fputs(line_prefix, diff_words->opt->file);
1985 }
1986 if (diff_words->current_plus != plus_begin) {
Stefan Bellerbd033292017-06-29 17:07:03 -07001987 fn_out_diff_words_write_helper(diff_words->opt,
Thomas Rast882749a2010-04-14 17:59:06 +02001988 &style->ctx, style->newline,
1989 plus_begin - diff_words->current_plus,
Stefan Bellerbd033292017-06-29 17:07:03 -07001990 diff_words->current_plus);
Bo Yang4297c0a2010-05-29 23:32:06 +08001991 }
1992 if (minus_begin != minus_end) {
Stefan Bellerbd033292017-06-29 17:07:03 -07001993 fn_out_diff_words_write_helper(diff_words->opt,
Brandon Williams63a01c32018-02-14 10:59:39 -08001994 &style->old_word, style->newline,
Stefan Bellerbd033292017-06-29 17:07:03 -07001995 minus_end - minus_begin, minus_begin);
Bo Yang4297c0a2010-05-29 23:32:06 +08001996 }
1997 if (plus_begin != plus_end) {
Stefan Bellerbd033292017-06-29 17:07:03 -07001998 fn_out_diff_words_write_helper(diff_words->opt,
Brandon Williams63a01c32018-02-14 10:59:39 -08001999 &style->new_word, style->newline,
Stefan Bellerbd033292017-06-29 17:07:03 -07002000 plus_end - plus_begin, plus_begin);
Bo Yang4297c0a2010-05-29 23:32:06 +08002001 }
Johannes Schindelin2e5d2002009-01-17 17:29:44 +01002002
2003 diff_words->current_plus = plus_end;
Bo Yang4297c0a2010-05-29 23:32:06 +08002004 diff_words->last_minus = minus_first;
Johannes Schindelinf59a59e2006-07-28 23:56:15 +02002005}
2006
Johannes Schindelin2b6a5412009-01-17 17:29:45 +01002007/* This function starts looking at *begin, and returns 0 iff a word was found. */
2008static int find_word_boundaries(mmfile_t *buffer, regex_t *word_regex,
2009 int *begin, int *end)
2010{
Phillip Wood0324e8f2021-05-04 09:27:34 +00002011 while (word_regex && *begin < buffer->size) {
Johannes Schindelin2b6a5412009-01-17 17:29:45 +01002012 regmatch_t match[1];
Johannes Schindelinb7d36ff2016-09-21 20:24:14 +02002013 if (!regexec_buf(word_regex, buffer->ptr + *begin,
2014 buffer->size - *begin, 1, match, 0)) {
Johannes Schindelin2b6a5412009-01-17 17:29:45 +01002015 char *p = memchr(buffer->ptr + *begin + match[0].rm_so,
2016 '\n', match[0].rm_eo - match[0].rm_so);
2017 *end = p ? p - buffer->ptr : match[0].rm_eo + *begin;
2018 *begin += match[0].rm_so;
Phillip Wood0324e8f2021-05-04 09:27:34 +00002019 if (*begin == *end)
2020 (*begin)++;
2021 else
2022 return *begin > *end;
2023 } else {
2024 return -1;
Johannes Schindelin2b6a5412009-01-17 17:29:45 +01002025 }
Johannes Schindelinf59a59e2006-07-28 23:56:15 +02002026 }
2027
Johannes Schindelin2b6a5412009-01-17 17:29:45 +01002028 /* find the next word */
2029 while (*begin < buffer->size && isspace(buffer->ptr[*begin]))
2030 (*begin)++;
2031 if (*begin >= buffer->size)
2032 return -1;
2033
2034 /* find the end of the word */
2035 *end = *begin + 1;
2036 while (*end < buffer->size && !isspace(buffer->ptr[*end]))
2037 (*end)++;
2038
2039 return 0;
2040}
2041
Johannes Schindelin23c15752009-01-17 17:29:43 +01002042/*
Johannes Schindelin2e5d2002009-01-17 17:29:44 +01002043 * This function splits the words in buffer->text, stores the list with
2044 * newline separator into out, and saves the offsets of the original words
2045 * in buffer->orig.
Johannes Schindelin23c15752009-01-17 17:29:43 +01002046 */
Johannes Schindelin2b6a5412009-01-17 17:29:45 +01002047static void diff_words_fill(struct diff_words_buffer *buffer, mmfile_t *out,
2048 regex_t *word_regex)
Johannes Schindelin23c15752009-01-17 17:29:43 +01002049{
Johannes Schindelin2e5d2002009-01-17 17:29:44 +01002050 int i, j;
Johannes Schindelin2b6a5412009-01-17 17:29:45 +01002051 long alloc = 0;
Johannes Schindelin2e5d2002009-01-17 17:29:44 +01002052
2053 out->size = 0;
Johannes Schindelin2b6a5412009-01-17 17:29:45 +01002054 out->ptr = NULL;
Johannes Schindelin2e5d2002009-01-17 17:29:44 +01002055
2056 /* fake an empty "0th" word */
2057 ALLOC_GROW(buffer->orig, 1, buffer->orig_alloc);
2058 buffer->orig[0].begin = buffer->orig[0].end = buffer->text.ptr;
2059 buffer->orig_nr = 1;
2060
2061 for (i = 0; i < buffer->text.size; i++) {
Johannes Schindelin2b6a5412009-01-17 17:29:45 +01002062 if (find_word_boundaries(&buffer->text, word_regex, &i, &j))
2063 return;
Johannes Schindelin2e5d2002009-01-17 17:29:44 +01002064
2065 /* store original boundaries */
2066 ALLOC_GROW(buffer->orig, buffer->orig_nr + 1,
2067 buffer->orig_alloc);
2068 buffer->orig[buffer->orig_nr].begin = buffer->text.ptr + i;
2069 buffer->orig[buffer->orig_nr].end = buffer->text.ptr + j;
2070 buffer->orig_nr++;
2071
2072 /* store one word */
Johannes Schindelin2b6a5412009-01-17 17:29:45 +01002073 ALLOC_GROW(out->ptr, out->size + j - i + 1, alloc);
Johannes Schindelin2e5d2002009-01-17 17:29:44 +01002074 memcpy(out->ptr + out->size, buffer->text.ptr + i, j - i);
2075 out->ptr[out->size + j - i] = '\n';
2076 out->size += j - i + 1;
2077
2078 i = j - 1;
Johannes Schindelinf59a59e2006-07-28 23:56:15 +02002079 }
2080}
2081
2082/* this executes the word diff on the accumulated buffers */
2083static void diff_words_show(struct diff_words_data *diff_words)
2084{
2085 xpparam_t xpp;
2086 xdemitconf_t xecfg;
Johannes Schindelinf59a59e2006-07-28 23:56:15 +02002087 mmfile_t minus, plus;
Thomas Rast882749a2010-04-14 17:59:06 +02002088 struct diff_words_style *style = diff_words->style;
Johannes Schindelinf59a59e2006-07-28 23:56:15 +02002089
Bo Yang4297c0a2010-05-29 23:32:06 +08002090 struct diff_options *opt = diff_words->opt;
John Keeping30997bb2013-02-07 20:15:27 +00002091 const char *line_prefix;
Bo Yang4297c0a2010-05-29 23:32:06 +08002092
2093 assert(opt);
John Keeping30997bb2013-02-07 20:15:27 +00002094 line_prefix = diff_line_prefix(opt);
Bo Yang4297c0a2010-05-29 23:32:06 +08002095
Johannes Schindelin2e5d2002009-01-17 17:29:44 +01002096 /* special case: only removal */
2097 if (!diff_words->plus.text.size) {
Stefan Bellerbd033292017-06-29 17:07:03 -07002098 emit_diff_symbol(diff_words->opt, DIFF_SYMBOL_WORD_DIFF,
2099 line_prefix, strlen(line_prefix), 0);
2100 fn_out_diff_words_write_helper(diff_words->opt,
Brandon Williams63a01c32018-02-14 10:59:39 -08002101 &style->old_word, style->newline,
Bo Yang4297c0a2010-05-29 23:32:06 +08002102 diff_words->minus.text.size,
Stefan Bellerbd033292017-06-29 17:07:03 -07002103 diff_words->minus.text.ptr);
Johannes Schindelin2e5d2002009-01-17 17:29:44 +01002104 diff_words->minus.text.size = 0;
2105 return;
2106 }
2107
2108 diff_words->current_plus = diff_words->plus.text.ptr;
Bo Yang4297c0a2010-05-29 23:32:06 +08002109 diff_words->last_minus = 0;
Johannes Schindelinf59a59e2006-07-28 23:56:15 +02002110
Brian Downing9ccd0a82008-10-25 15:30:37 +02002111 memset(&xpp, 0, sizeof(xpp));
Johannes Schindelin30b25012007-07-04 19:05:46 +01002112 memset(&xecfg, 0, sizeof(xecfg));
Johannes Schindelin2b6a5412009-01-17 17:29:45 +01002113 diff_words_fill(&diff_words->minus, &minus, diff_words->word_regex);
2114 diff_words_fill(&diff_words->plus, &plus, diff_words->word_regex);
René Scharfe582aa002010-05-02 15:04:41 +02002115 xpp.flags = 0;
Johannes Schindelin2b6a5412009-01-17 17:29:45 +01002116 /* as only the hunk header will be parsed, we need a 0-context */
Johannes Schindelin2e5d2002009-01-17 17:29:44 +01002117 xecfg.ctxlen = 0;
Jeff King7c61e252018-11-02 02:37:18 -04002118 if (xdi_diff_outf(&minus, &plus, fn_out_diff_words_aux, NULL,
Jeff King9346d6d2018-11-02 02:35:45 -04002119 diff_words, &xpp, &xecfg))
Jeff King3efb9882015-09-24 19:12:23 -04002120 die("unable to generate word diff");
Johannes Schindelinf59a59e2006-07-28 23:56:15 +02002121 free(minus.ptr);
2122 free(plus.ptr);
Johannes Schindelin2e5d2002009-01-17 17:29:44 +01002123 if (diff_words->current_plus != diff_words->plus.text.ptr +
Bo Yang4297c0a2010-05-29 23:32:06 +08002124 diff_words->plus.text.size) {
2125 if (color_words_output_graph_prefix(diff_words))
Stefan Bellerbd033292017-06-29 17:07:03 -07002126 emit_diff_symbol(diff_words->opt, DIFF_SYMBOL_WORD_DIFF,
2127 line_prefix, strlen(line_prefix), 0);
2128 fn_out_diff_words_write_helper(diff_words->opt,
Thomas Rast882749a2010-04-14 17:59:06 +02002129 &style->ctx, style->newline,
Johannes Schindelin2e5d2002009-01-17 17:29:44 +01002130 diff_words->plus.text.ptr + diff_words->plus.text.size
Stefan Bellerbd033292017-06-29 17:07:03 -07002131 - diff_words->current_plus, diff_words->current_plus);
Bo Yang4297c0a2010-05-29 23:32:06 +08002132 }
Johannes Schindelinf59a59e2006-07-28 23:56:15 +02002133 diff_words->minus.text.size = diff_words->plus.text.size = 0;
Johannes Schindelinf59a59e2006-07-28 23:56:15 +02002134}
2135
Junio C Hamano76fd2822009-10-30 10:09:06 -07002136/* In "color-words" mode, show word-diff of words accumulated in the buffer */
2137static void diff_words_flush(struct emit_callback *ecbdata)
2138{
Stefan Bellere6e045f2017-06-29 17:07:06 -07002139 struct diff_options *wo = ecbdata->diff_words->opt;
2140
Junio C Hamano76fd2822009-10-30 10:09:06 -07002141 if (ecbdata->diff_words->minus.text.size ||
2142 ecbdata->diff_words->plus.text.size)
2143 diff_words_show(ecbdata->diff_words);
Stefan Bellere6e045f2017-06-29 17:07:06 -07002144
2145 if (wo->emitted_symbols) {
2146 struct diff_options *o = ecbdata->opt;
2147 struct emitted_diff_symbols *wol = wo->emitted_symbols;
2148 int i;
2149
2150 /*
2151 * NEEDSWORK:
2152 * Instead of appending each, concat all words to a line?
2153 */
2154 for (i = 0; i < wol->nr; i++)
2155 append_emitted_diff_symbol(o, &wol->buf[i]);
2156
2157 for (i = 0; i < wol->nr; i++)
2158 free((void *)wol->buf[i].line);
2159
2160 wol->nr = 0;
2161 }
Junio C Hamano76fd2822009-10-30 10:09:06 -07002162}
2163
Nguyễn Thái Ngọc Duyacd00ea2018-09-21 17:57:33 +02002164static void diff_filespec_load_driver(struct diff_filespec *one,
2165 struct index_state *istate)
Thomas Rast77d1a522012-03-14 19:24:08 +01002166{
2167 /* Use already-loaded driver */
2168 if (one->driver)
2169 return;
2170
2171 if (S_ISREG(one->mode))
Nguyễn Thái Ngọc Duyacd00ea2018-09-21 17:57:33 +02002172 one->driver = userdiff_find_by_path(istate, one->path);
Thomas Rast77d1a522012-03-14 19:24:08 +01002173
2174 /* Fallback to default settings */
2175 if (!one->driver)
2176 one->driver = userdiff_find_by_name("default");
2177}
2178
Nguyễn Thái Ngọc Duyacd00ea2018-09-21 17:57:33 +02002179static const char *userdiff_word_regex(struct diff_filespec *one,
2180 struct index_state *istate)
Thomas Rast77d1a522012-03-14 19:24:08 +01002181{
Nguyễn Thái Ngọc Duyacd00ea2018-09-21 17:57:33 +02002182 diff_filespec_load_driver(one, istate);
Thomas Rast77d1a522012-03-14 19:24:08 +01002183 return one->driver->word_regex;
2184}
2185
2186static void init_diff_words_data(struct emit_callback *ecbdata,
Thomas Rast6440d342012-03-14 19:24:09 +01002187 struct diff_options *orig_opts,
Thomas Rast77d1a522012-03-14 19:24:08 +01002188 struct diff_filespec *one,
2189 struct diff_filespec *two)
2190{
2191 int i;
Thomas Rast6440d342012-03-14 19:24:09 +01002192 struct diff_options *o = xmalloc(sizeof(struct diff_options));
2193 memcpy(o, orig_opts, sizeof(struct diff_options));
Thomas Rast77d1a522012-03-14 19:24:08 +01002194
René Scharfeca56dad2021-03-13 17:17:22 +01002195 CALLOC_ARRAY(ecbdata->diff_words, 1);
Thomas Rast77d1a522012-03-14 19:24:08 +01002196 ecbdata->diff_words->type = o->word_diff;
2197 ecbdata->diff_words->opt = o;
Stefan Bellere6e045f2017-06-29 17:07:06 -07002198
2199 if (orig_opts->emitted_symbols)
René Scharfeca56dad2021-03-13 17:17:22 +01002200 CALLOC_ARRAY(o->emitted_symbols, 1);
Stefan Bellere6e045f2017-06-29 17:07:06 -07002201
Thomas Rast77d1a522012-03-14 19:24:08 +01002202 if (!o->word_regex)
Nguyễn Thái Ngọc Duyacd00ea2018-09-21 17:57:33 +02002203 o->word_regex = userdiff_word_regex(one, o->repo->index);
Thomas Rast77d1a522012-03-14 19:24:08 +01002204 if (!o->word_regex)
Nguyễn Thái Ngọc Duyacd00ea2018-09-21 17:57:33 +02002205 o->word_regex = userdiff_word_regex(two, o->repo->index);
Thomas Rast77d1a522012-03-14 19:24:08 +01002206 if (!o->word_regex)
2207 o->word_regex = diff_word_regex_cfg;
2208 if (o->word_regex) {
2209 ecbdata->diff_words->word_regex = (regex_t *)
2210 xmalloc(sizeof(regex_t));
2211 if (regcomp(ecbdata->diff_words->word_regex,
2212 o->word_regex,
2213 REG_EXTENDED | REG_NEWLINE))
Nguyễn Thái Ngọc Duy1a07e592018-07-21 09:49:19 +02002214 die("invalid regular expression: %s",
2215 o->word_regex);
Thomas Rast77d1a522012-03-14 19:24:08 +01002216 }
2217 for (i = 0; i < ARRAY_SIZE(diff_words_styles); i++) {
2218 if (o->word_diff == diff_words_styles[i].type) {
2219 ecbdata->diff_words->style =
2220 &diff_words_styles[i];
2221 break;
2222 }
2223 }
2224 if (want_color(o->use_color)) {
2225 struct diff_words_style *st = ecbdata->diff_words->style;
Brandon Williams63a01c32018-02-14 10:59:39 -08002226 st->old_word.color = diff_get_color_opt(o, DIFF_FILE_OLD);
2227 st->new_word.color = diff_get_color_opt(o, DIFF_FILE_NEW);
Jeff King8dbf3eb2015-05-27 16:48:46 -04002228 st->ctx.color = diff_get_color_opt(o, DIFF_CONTEXT);
Thomas Rast77d1a522012-03-14 19:24:08 +01002229 }
2230}
2231
Johannes Schindelinf59a59e2006-07-28 23:56:15 +02002232static void free_diff_words_data(struct emit_callback *ecbdata)
2233{
2234 if (ecbdata->diff_words) {
Junio C Hamano76fd2822009-10-30 10:09:06 -07002235 diff_words_flush(ecbdata);
Ævar Arnfjörð Bjarmason77e56d52022-03-17 15:55:35 +01002236 free_emitted_diff_symbols(ecbdata->diff_words->opt->emitted_symbols);
Thomas Rast6440d342012-03-14 19:24:09 +01002237 free (ecbdata->diff_words->opt);
Jim Meyering8e0f7002008-01-31 18:26:32 +01002238 free (ecbdata->diff_words->minus.text.ptr);
Johannes Schindelin2e5d2002009-01-17 17:29:44 +01002239 free (ecbdata->diff_words->minus.orig);
Jim Meyering8e0f7002008-01-31 18:26:32 +01002240 free (ecbdata->diff_words->plus.text.ptr);
Johannes Schindelin2e5d2002009-01-17 17:29:44 +01002241 free (ecbdata->diff_words->plus.orig);
Brandon Caseyef5644e2010-09-09 14:02:45 -05002242 if (ecbdata->diff_words->word_regex) {
2243 regfree(ecbdata->diff_words->word_regex);
2244 free(ecbdata->diff_words->word_regex);
2245 }
Ævar Arnfjörð Bjarmason6a83d902017-06-15 23:15:46 +00002246 FREE_AND_NULL(ecbdata->diff_words);
Johannes Schindelinf59a59e2006-07-28 23:56:15 +02002247 }
2248}
2249
Jeff Kingce436972006-07-23 05:24:18 -04002250const char *diff_get_color(int diff_use_color, enum color_diff ix)
Johannes Schindelincd112ce2006-06-13 18:45:44 +02002251{
Jeff Kingdaa0c3d2011-08-17 22:04:23 -07002252 if (want_color(diff_use_color))
Linus Torvalds50f575f2006-06-22 13:53:31 -07002253 return diff_colors[ix];
2254 return "";
Johannes Schindelincd112ce2006-06-13 18:45:44 +02002255}
2256
John Keepingf1922232013-02-07 20:15:26 +00002257const char *diff_line_prefix(struct diff_options *opt)
2258{
2259 struct strbuf *msgbuf;
2260 if (!opt->output_prefix)
2261 return "";
2262
2263 msgbuf = opt->output_prefix(opt, opt->output_prefix_data);
2264 return msgbuf->buf;
2265}
2266
Jeff King19b90462019-02-14 00:48:27 -05002267static unsigned long sane_truncate_line(char *line, unsigned long len)
Junio C Hamano23707812008-01-02 01:50:11 -08002268{
2269 const char *cp;
2270 unsigned long allot;
2271 size_t l = len;
2272
Junio C Hamano23707812008-01-02 01:50:11 -08002273 cp = line;
2274 allot = l;
2275 while (0 < l) {
2276 (void) utf8_width(&cp, &l);
2277 if (!cp)
2278 break; /* truncated in the middle? */
2279 }
2280 return allot - l;
2281}
2282
Junio C Hamanod68fe262009-09-14 22:05:57 -07002283static void find_lno(const char *line, struct emit_callback *ecbdata)
Junio C Hamano690ed842009-09-04 00:41:15 -07002284{
Junio C Hamanod68fe262009-09-14 22:05:57 -07002285 const char *p;
2286 ecbdata->lno_in_preimage = 0;
2287 ecbdata->lno_in_postimage = 0;
2288 p = strchr(line, '-');
Junio C Hamano690ed842009-09-04 00:41:15 -07002289 if (!p)
Junio C Hamanod68fe262009-09-14 22:05:57 -07002290 return; /* cannot happen */
2291 ecbdata->lno_in_preimage = strtol(p + 1, NULL, 10);
2292 p = strchr(p, '+');
2293 if (!p)
2294 return; /* cannot happen */
2295 ecbdata->lno_in_postimage = strtol(p + 1, NULL, 10);
Junio C Hamano690ed842009-09-04 00:41:15 -07002296}
2297
Ævar Arnfjörð Bjarmasona8d5eb62021-04-12 19:15:24 +02002298static int fn_out_consume(void *priv, char *line, unsigned long len)
Junio C Hamano6973dca2006-04-21 23:57:45 -07002299{
Junio C Hamano6973dca2006-04-21 23:57:45 -07002300 struct emit_callback *ecbdata = priv;
Bo Yang7be57612010-05-26 15:23:54 +08002301 struct diff_options *o = ecbdata->opt;
Junio C Hamano6973dca2006-04-21 23:57:45 -07002302
Stefan Bellerba162332016-09-07 16:36:46 -07002303 o->found_changes = 1;
2304
Greg Bacon3e97c7c2009-11-19 15:12:24 -06002305 if (ecbdata->header) {
Stefan Bellera29b0a12017-06-29 17:06:57 -07002306 emit_diff_symbol(o, DIFF_SYMBOL_HEADER,
2307 ecbdata->header->buf, ecbdata->header->len, 0);
Greg Bacon3e97c7c2009-11-19 15:12:24 -06002308 strbuf_reset(ecbdata->header);
2309 ecbdata->header = NULL;
2310 }
Johannes Schindelin34a5e1a2007-02-25 23:34:54 +01002311
Junio C Hamano6973dca2006-04-21 23:57:45 -07002312 if (ecbdata->label_path[0]) {
Stefan Beller3ee8b7b2017-06-29 17:06:56 -07002313 emit_diff_symbol(o, DIFF_SYMBOL_FILEPAIR_MINUS,
2314 ecbdata->label_path[0],
2315 strlen(ecbdata->label_path[0]), 0);
2316 emit_diff_symbol(o, DIFF_SYMBOL_FILEPAIR_PLUS,
2317 ecbdata->label_path[1],
2318 strlen(ecbdata->label_path[1]), 0);
Junio C Hamano6973dca2006-04-21 23:57:45 -07002319 ecbdata->label_path[0] = ecbdata->label_path[1] = NULL;
2320 }
Johannes Schindelincd112ce2006-06-13 18:45:44 +02002321
Jim Meyeringa624eaa2008-08-15 13:39:26 +02002322 if (diff_suppress_blank_empty
2323 && len == 2 && line[0] == ' ' && line[1] == '\n') {
2324 line[0] = '\n';
2325 len = 1;
2326 }
2327
Junio C Hamanob8d9c1a2009-09-03 23:59:25 -07002328 if (line[0] == '@') {
Junio C Hamano76fd2822009-10-30 10:09:06 -07002329 if (ecbdata->diff_words)
2330 diff_words_flush(ecbdata);
Jeff King19b90462019-02-14 00:48:27 -05002331 len = sane_truncate_line(line, len);
Junio C Hamanod68fe262009-09-14 22:05:57 -07002332 find_lno(line, ecbdata);
Bert Wesarg89cb73a2009-11-27 07:55:18 +01002333 emit_hunk_header(ecbdata, line, len);
Ævar Arnfjörð Bjarmasona8d5eb62021-04-12 19:15:24 +02002334 return 0;
Johannes Schindelincd112ce2006-06-13 18:45:44 +02002335 }
Junio C Hamano448c3ef2006-09-22 22:48:39 -07002336
Junio C Hamano448c3ef2006-09-22 22:48:39 -07002337 if (ecbdata->diff_words) {
Stefan Bellerff958672017-06-29 17:06:54 -07002338 enum diff_symbol s =
2339 ecbdata->diff_words->type == DIFF_WORDS_PORCELAIN ?
2340 DIFF_SYMBOL_WORDS_PORCELAIN : DIFF_SYMBOL_WORDS;
Junio C Hamano448c3ef2006-09-22 22:48:39 -07002341 if (line[0] == '-') {
2342 diff_words_append(line, len,
2343 &ecbdata->diff_words->minus);
Ævar Arnfjörð Bjarmasona8d5eb62021-04-12 19:15:24 +02002344 return 0;
Junio C Hamano448c3ef2006-09-22 22:48:39 -07002345 } else if (line[0] == '+') {
2346 diff_words_append(line, len,
2347 &ecbdata->diff_words->plus);
Ævar Arnfjörð Bjarmasona8d5eb62021-04-12 19:15:24 +02002348 return 0;
Christian Couder59556542013-11-30 21:55:40 +01002349 } else if (starts_with(line, "\\ ")) {
Thomas Rastc7c2bc02012-01-12 12:15:33 +01002350 /*
2351 * Eat the "no newline at eof" marker as if we
2352 * saw a "+" or "-" line with nothing on it,
2353 * and return without diff_words_flush() to
2354 * defer processing. If this is the end of
2355 * preimage, more "+" lines may come after it.
2356 */
Ævar Arnfjörð Bjarmasona8d5eb62021-04-12 19:15:24 +02002357 return 0;
Junio C Hamano448c3ef2006-09-22 22:48:39 -07002358 }
Junio C Hamano76fd2822009-10-30 10:09:06 -07002359 diff_words_flush(ecbdata);
Stefan Bellerff958672017-06-29 17:06:54 -07002360 emit_diff_symbol(o, s, line, len, 0);
Ævar Arnfjörð Bjarmasona8d5eb62021-04-12 19:15:24 +02002361 return 0;
Junio C Hamano448c3ef2006-09-22 22:48:39 -07002362 }
Junio C Hamano448c3ef2006-09-22 22:48:39 -07002363
Junio C Hamano0e383e12015-05-26 09:56:33 -07002364 switch (line[0]) {
2365 case '+':
Junio C Hamanod68fe262009-09-14 22:05:57 -07002366 ecbdata->lno_in_postimage++;
Jeff Kinge04df612019-02-14 00:48:13 -05002367 emit_add_line(ecbdata, line + 1, len - 1);
Junio C Hamano0e383e12015-05-26 09:56:33 -07002368 break;
2369 case '-':
2370 ecbdata->lno_in_preimage++;
Jeff Kinge04df612019-02-14 00:48:13 -05002371 emit_del_line(ecbdata, line + 1, len - 1);
Junio C Hamano0e383e12015-05-26 09:56:33 -07002372 break;
2373 case ' ':
2374 ecbdata->lno_in_postimage++;
2375 ecbdata->lno_in_preimage++;
Jeff Kinge04df612019-02-14 00:48:13 -05002376 emit_context_line(ecbdata, line + 1, len - 1);
Junio C Hamano0e383e12015-05-26 09:56:33 -07002377 break;
2378 default:
2379 /* incomplete line at the end */
2380 ecbdata->lno_in_preimage++;
Stefan Bellerf2bb1212017-06-29 17:06:55 -07002381 emit_diff_symbol(o, DIFF_SYMBOL_CONTEXT_INCOMPLETE,
2382 line, len, 0);
Junio C Hamano0e383e12015-05-26 09:56:33 -07002383 break;
Junio C Hamano448c3ef2006-09-22 22:48:39 -07002384 }
Ævar Arnfjörð Bjarmasona8d5eb62021-04-12 19:15:24 +02002385 return 0;
Junio C Hamano6973dca2006-04-21 23:57:45 -07002386}
2387
Nguyễn Thái Ngọc Duyc905cbc2018-02-01 20:02:20 +07002388static void pprint_rename(struct strbuf *name, const char *a, const char *b)
Junio C Hamano6973dca2006-04-21 23:57:45 -07002389{
Brandon Williams63a01c32018-02-14 10:59:39 -08002390 const char *old_name = a;
2391 const char *new_name = b;
Junio C Hamano6973dca2006-04-21 23:57:45 -07002392 int pfx_length, sfx_length;
Thomas Rastdd281f02013-02-26 21:47:01 +01002393 int pfx_adjust_for_slash;
Junio C Hamano6973dca2006-04-21 23:57:45 -07002394 int len_a = strlen(a);
2395 int len_b = strlen(b);
Pierre Habouzit663af342007-09-20 00:42:15 +02002396 int a_midlen, b_midlen;
Alexandre Julliarde5bfbf92007-02-10 15:39:00 +01002397 int qlen_a = quote_c_style(a, NULL, NULL, 0);
2398 int qlen_b = quote_c_style(b, NULL, NULL, 0);
2399
2400 if (qlen_a || qlen_b) {
Nguyễn Thái Ngọc Duyc905cbc2018-02-01 20:02:20 +07002401 quote_c_style(a, name, NULL, 0);
2402 strbuf_addstr(name, " => ");
2403 quote_c_style(b, name, NULL, 0);
2404 return;
Alexandre Julliarde5bfbf92007-02-10 15:39:00 +01002405 }
Junio C Hamano6973dca2006-04-21 23:57:45 -07002406
2407 /* Find common prefix */
2408 pfx_length = 0;
Brandon Williams63a01c32018-02-14 10:59:39 -08002409 while (*old_name && *new_name && *old_name == *new_name) {
2410 if (*old_name == '/')
2411 pfx_length = old_name - a + 1;
2412 old_name++;
2413 new_name++;
Junio C Hamano6973dca2006-04-21 23:57:45 -07002414 }
2415
2416 /* Find common suffix */
Brandon Williams63a01c32018-02-14 10:59:39 -08002417 old_name = a + len_a;
2418 new_name = b + len_b;
Junio C Hamano6973dca2006-04-21 23:57:45 -07002419 sfx_length = 0;
Antoine Pelissed020e272013-02-23 17:48:45 +01002420 /*
Thomas Rastdd281f02013-02-26 21:47:01 +01002421 * If there is a common prefix, it must end in a slash. In
2422 * that case we let this loop run 1 into the prefix to see the
2423 * same slash.
2424 *
2425 * If there is no common prefix, we cannot do this as it would
2426 * underrun the input strings.
Antoine Pelissed020e272013-02-23 17:48:45 +01002427 */
Thomas Rastdd281f02013-02-26 21:47:01 +01002428 pfx_adjust_for_slash = (pfx_length ? 1 : 0);
Brandon Williams63a01c32018-02-14 10:59:39 -08002429 while (a + pfx_length - pfx_adjust_for_slash <= old_name &&
2430 b + pfx_length - pfx_adjust_for_slash <= new_name &&
2431 *old_name == *new_name) {
2432 if (*old_name == '/')
2433 sfx_length = len_a - (old_name - a);
2434 old_name--;
2435 new_name--;
Junio C Hamano6973dca2006-04-21 23:57:45 -07002436 }
2437
2438 /*
2439 * pfx{mid-a => mid-b}sfx
2440 * {pfx-a => pfx-b}sfx
2441 * pfx{sfx-a => sfx-b}
2442 * name-a => name-b
2443 */
Pierre Habouzit663af342007-09-20 00:42:15 +02002444 a_midlen = len_a - pfx_length - sfx_length;
2445 b_midlen = len_b - pfx_length - sfx_length;
2446 if (a_midlen < 0)
2447 a_midlen = 0;
2448 if (b_midlen < 0)
2449 b_midlen = 0;
Junio C Hamanocc908b82006-05-14 22:07:28 -07002450
Nguyễn Thái Ngọc Duyc905cbc2018-02-01 20:02:20 +07002451 strbuf_grow(name, pfx_length + a_midlen + b_midlen + sfx_length + 7);
Pierre Habouzit663af342007-09-20 00:42:15 +02002452 if (pfx_length + sfx_length) {
Nguyễn Thái Ngọc Duyc905cbc2018-02-01 20:02:20 +07002453 strbuf_add(name, a, pfx_length);
2454 strbuf_addch(name, '{');
Junio C Hamano6973dca2006-04-21 23:57:45 -07002455 }
Nguyễn Thái Ngọc Duyc905cbc2018-02-01 20:02:20 +07002456 strbuf_add(name, a + pfx_length, a_midlen);
2457 strbuf_addstr(name, " => ");
2458 strbuf_add(name, b + pfx_length, b_midlen);
Pierre Habouzit663af342007-09-20 00:42:15 +02002459 if (pfx_length + sfx_length) {
Nguyễn Thái Ngọc Duyc905cbc2018-02-01 20:02:20 +07002460 strbuf_addch(name, '}');
2461 strbuf_add(name, a + len_a - sfx_length, sfx_length);
Junio C Hamano6973dca2006-04-21 23:57:45 -07002462 }
Junio C Hamano6973dca2006-04-21 23:57:45 -07002463}
2464
Junio C Hamano6973dca2006-04-21 23:57:45 -07002465static struct diffstat_file *diffstat_add(struct diffstat_t *diffstat,
2466 const char *name_a,
2467 const char *name_b)
2468{
2469 struct diffstat_file *x;
René Scharfeca56dad2021-03-13 17:17:22 +01002470 CALLOC_ARRAY(x, 1);
Dmitry S. Dolzhenko4c960a42014-03-04 02:31:53 +04002471 ALLOC_GROW(diffstat->files, diffstat->nr + 1, diffstat->alloc);
Junio C Hamano6973dca2006-04-21 23:57:45 -07002472 diffstat->files[diffstat->nr++] = x;
2473 if (name_b) {
Junio C Hamanof6046522007-12-11 23:46:30 -08002474 x->from_name = xstrdup(name_a);
2475 x->name = xstrdup(name_b);
Junio C Hamano6973dca2006-04-21 23:57:45 -07002476 x->is_renamed = 1;
2477 }
Junio C Hamanof6046522007-12-11 23:46:30 -08002478 else {
2479 x->from_name = NULL;
Shawn Pearce9befac42006-09-02 00:16:31 -04002480 x->name = xstrdup(name_a);
Junio C Hamanof6046522007-12-11 23:46:30 -08002481 }
Junio C Hamano6973dca2006-04-21 23:57:45 -07002482 return x;
2483}
2484
Ævar Arnfjörð Bjarmasona8d5eb62021-04-12 19:15:24 +02002485static int diffstat_consume(void *priv, char *line, unsigned long len)
Junio C Hamano6973dca2006-04-21 23:57:45 -07002486{
2487 struct diffstat_t *diffstat = priv;
2488 struct diffstat_file *x = diffstat->files[diffstat->nr - 1];
2489
2490 if (line[0] == '+')
2491 x->added++;
2492 else if (line[0] == '-')
2493 x->deleted++;
Ævar Arnfjörð Bjarmasona8d5eb62021-04-12 19:15:24 +02002494 return 0;
Junio C Hamano6973dca2006-04-21 23:57:45 -07002495}
2496
Johannes Schindelin698ce6f2006-05-20 15:40:29 +02002497const char mime_boundary_leader[] = "------------";
Junio C Hamano6973dca2006-04-21 23:57:45 -07002498
Junio C Hamanoa2540022006-09-26 18:53:02 -07002499static int scale_linear(int it, int width, int max_change)
2500{
Junio C Hamano2eeeef22012-02-14 13:49:11 -08002501 if (!it)
2502 return 0;
Junio C Hamanoa2540022006-09-26 18:53:02 -07002503 /*
Junio C Hamano2eeeef22012-02-14 13:49:11 -08002504 * make sure that at least one '-' or '+' is printed if
2505 * there is any change to this path. The easiest way is to
Elijah Newren15beaaa2019-11-05 17:07:23 +00002506 * scale linearly as if the allotted width is one column shorter
Junio C Hamano2eeeef22012-02-14 13:49:11 -08002507 * than it is, and then add 1 to the result.
Junio C Hamanoa2540022006-09-26 18:53:02 -07002508 */
Junio C Hamano2eeeef22012-02-14 13:49:11 -08002509 return 1 + (it * (width - 1) / max_change);
Junio C Hamanoa2540022006-09-26 18:53:02 -07002510}
2511
Stefan Beller0911c472017-06-29 17:07:02 -07002512static void show_graph(struct strbuf *out, char ch, int cnt,
2513 const char *set, const char *reset)
Junio C Hamanoa2540022006-09-26 18:53:02 -07002514{
2515 if (cnt <= 0)
2516 return;
Stefan Beller0911c472017-06-29 17:07:02 -07002517 strbuf_addstr(out, set);
2518 strbuf_addchars(out, ch, cnt);
2519 strbuf_addstr(out, reset);
Junio C Hamanoa2540022006-09-26 18:53:02 -07002520}
2521
Junio C Hamanof6046522007-12-11 23:46:30 -08002522static void fill_print_name(struct diffstat_file *file)
2523{
Nguyễn Thái Ngọc Duyc905cbc2018-02-01 20:02:20 +07002524 struct strbuf pname = STRBUF_INIT;
Junio C Hamanof6046522007-12-11 23:46:30 -08002525
2526 if (file->print_name)
2527 return;
2528
Nguyễn Thái Ngọc Duyc905cbc2018-02-01 20:02:20 +07002529 if (file->is_renamed)
2530 pprint_rename(&pname, file->from_name, file->name);
2531 else
2532 quote_c_style(file->name, &pname, NULL, 0);
2533
Nguyễn Thái Ngọc Duyddf88fa2018-02-24 21:09:59 +07002534 if (file->comments)
2535 strbuf_addf(&pname, " (%s)", file->comments);
2536
Nguyễn Thái Ngọc Duyc905cbc2018-02-01 20:02:20 +07002537 file->print_name = strbuf_detach(&pname, NULL);
Junio C Hamanof6046522007-12-11 23:46:30 -08002538}
2539
Stefan Beller0911c472017-06-29 17:07:02 -07002540static void print_stat_summary_inserts_deletes(struct diff_options *options,
2541 int files, int insertions, int deletions)
Nguyễn Thái Ngọc Duy7f814632012-02-01 19:55:07 +07002542{
2543 struct strbuf sb = STRBUF_INIT;
Nguyễn Thái Ngọc Duy7f814632012-02-01 19:55:07 +07002544
2545 if (!files) {
2546 assert(insertions == 0 && deletions == 0);
Stefan Beller0911c472017-06-29 17:07:02 -07002547 emit_diff_symbol(options, DIFF_SYMBOL_STATS_SUMMARY_NO_FILES,
2548 NULL, 0, 0);
2549 return;
Nguyễn Thái Ngọc Duy7f814632012-02-01 19:55:07 +07002550 }
2551
2552 strbuf_addf(&sb,
Nguyễn Thái Ngọc Duy218adaa2012-09-13 21:16:26 +07002553 (files == 1) ? " %d file changed" : " %d files changed",
Nguyễn Thái Ngọc Duy7f814632012-02-01 19:55:07 +07002554 files);
2555
2556 /*
2557 * For binary diff, the caller may want to print "x files
2558 * changed" with insertions == 0 && deletions == 0.
2559 *
2560 * Not omitting "0 insertions(+), 0 deletions(-)" in this case
2561 * is probably less confusing (i.e skip over "2 files changed
2562 * but nothing about added/removed lines? Is this a bug in Git?").
2563 */
2564 if (insertions || deletions == 0) {
Nguyễn Thái Ngọc Duy7f814632012-02-01 19:55:07 +07002565 strbuf_addf(&sb,
Nguyễn Thái Ngọc Duy218adaa2012-09-13 21:16:26 +07002566 (insertions == 1) ? ", %d insertion(+)" : ", %d insertions(+)",
Nguyễn Thái Ngọc Duy7f814632012-02-01 19:55:07 +07002567 insertions);
2568 }
2569
2570 if (deletions || insertions == 0) {
Nguyễn Thái Ngọc Duy7f814632012-02-01 19:55:07 +07002571 strbuf_addf(&sb,
Nguyễn Thái Ngọc Duy218adaa2012-09-13 21:16:26 +07002572 (deletions == 1) ? ", %d deletion(-)" : ", %d deletions(-)",
Nguyễn Thái Ngọc Duy7f814632012-02-01 19:55:07 +07002573 deletions);
2574 }
2575 strbuf_addch(&sb, '\n');
Stefan Beller0911c472017-06-29 17:07:02 -07002576 emit_diff_symbol(options, DIFF_SYMBOL_STATS_SUMMARY_INSERTS_DELETES,
2577 sb.buf, sb.len, 0);
Nguyễn Thái Ngọc Duy7f814632012-02-01 19:55:07 +07002578 strbuf_release(&sb);
Stefan Beller0911c472017-06-29 17:07:02 -07002579}
2580
2581void print_stat_summary(FILE *fp, int files,
2582 int insertions, int deletions)
2583{
2584 struct diff_options o;
2585 memset(&o, 0, sizeof(o));
2586 o.file = fp;
2587
2588 print_stat_summary_inserts_deletes(&o, files, insertions, deletions);
Nguyễn Thái Ngọc Duy7f814632012-02-01 19:55:07 +07002589}
2590
Felipe Contreras4b25d092009-05-01 12:06:36 +03002591static void show_stats(struct diffstat_t *data, struct diff_options *options)
Junio C Hamano6973dca2006-04-21 23:57:45 -07002592{
Benjamin Kramereb3a9dd2009-03-07 21:02:10 +01002593 int i, len, add, del, adds = 0, dels = 0;
Jeff King0974c112010-04-17 13:41:08 -04002594 uintmax_t max_change = 0, max_len = 0;
Zbigniew Jędrzejewski-Szmekdc801e72012-04-30 22:38:58 +02002595 int total_files = data->nr, count;
2596 int width, name_width, graph_width, number_width = 0, bin_width = 0;
Johannes Schindelinc0aa3352011-03-22 13:50:08 +01002597 const char *reset, *add_c, *del_c;
Junio C Hamanoe5f85df2011-05-27 21:50:39 -07002598 int extra_shown = 0;
Stefan Beller0911c472017-06-29 17:07:02 -07002599 const char *line_prefix = diff_line_prefix(options);
2600 struct strbuf out = STRBUF_INIT;
Junio C Hamano6973dca2006-04-21 23:57:45 -07002601
2602 if (data->nr == 0)
2603 return;
2604
Michael J Gruber808e1db2011-05-27 14:36:41 +02002605 count = options->stat_count ? options->stat_count : data->nr;
Junio C Hamanoa2540022006-09-26 18:53:02 -07002606
Pierre Habouzit8f67f8a2007-11-10 20:05:14 +01002607 reset = diff_get_color_opt(options, DIFF_RESET);
Pierre Habouzit8f67f8a2007-11-10 20:05:14 +01002608 add_c = diff_get_color_opt(options, DIFF_FILE_NEW);
2609 del_c = diff_get_color_opt(options, DIFF_FILE_OLD);
Junio C Hamano785f7432006-09-26 18:59:41 -07002610
Zbigniew Jędrzejewski-Szmek1b058bc2012-03-01 13:26:43 +01002611 /*
2612 * Find the longest filename and max number of changes
2613 */
Michael J Gruber808e1db2011-05-27 14:36:41 +02002614 for (i = 0; (i < count) && (i < data->nr); i++) {
Junio C Hamano6973dca2006-04-21 23:57:45 -07002615 struct diffstat_file *file = data->files[i];
Jeff King0974c112010-04-17 13:41:08 -04002616 uintmax_t change = file->added + file->deleted;
Junio C Hamanoaf0ed812012-11-27 11:24:54 -08002617
2618 if (!file->is_interesting && (change == 0)) {
Michael J Gruber808e1db2011-05-27 14:36:41 +02002619 count++; /* not shown == room for one more */
Michael J Gruber358e4602011-05-27 14:36:40 +02002620 continue;
2621 }
Junio C Hamanof6046522007-12-11 23:46:30 -08002622 fill_print_name(file);
2623 len = strlen(file->print_name);
Junio C Hamano6973dca2006-04-21 23:57:45 -07002624 if (max_len < len)
2625 max_len = len;
2626
Zbigniew Jędrzejewski-Szmekdc801e72012-04-30 22:38:58 +02002627 if (file->is_unmerged) {
2628 /* "Unmerged" is 8 characters */
2629 bin_width = bin_width < 8 ? 8 : bin_width;
Junio C Hamano6973dca2006-04-21 23:57:45 -07002630 continue;
Zbigniew Jędrzejewski-Szmekdc801e72012-04-30 22:38:58 +02002631 }
2632 if (file->is_binary) {
2633 /* "Bin XXX -> YYY bytes" */
2634 int w = 14 + decimal_width(file->added)
2635 + decimal_width(file->deleted);
2636 bin_width = bin_width < w ? w : bin_width;
2637 /* Display change counts aligned with "Bin" */
2638 number_width = 3;
2639 continue;
2640 }
2641
Junio C Hamanoa2540022006-09-26 18:53:02 -07002642 if (max_change < change)
2643 max_change = change;
Junio C Hamano6973dca2006-04-21 23:57:45 -07002644 }
Junio C Hamanoa20d3c02012-11-27 11:47:46 -08002645 count = i; /* where we can stop scanning in data->files[] */
Junio C Hamano6973dca2006-04-21 23:57:45 -07002646
Zbigniew Jędrzejewski-Szmek1b058bc2012-03-01 13:26:43 +01002647 /*
2648 * We have width = stat_width or term_columns() columns total.
2649 * We want a maximum of min(max_len, stat_name_width) for the name part.
Zbigniew Jędrzejewski-Szmek969fe572012-03-01 13:26:45 +01002650 * We want a maximum of min(max_change, stat_graph_width) for the +- part.
Zbigniew Jędrzejewski-Szmek1b058bc2012-03-01 13:26:43 +01002651 * We also need 1 for " " and 4 + decimal_width(max_change)
2652 * for " | NNNN " and one the empty column at the end, altogether
2653 * 6 + decimal_width(max_change).
Junio C Hamanoa2540022006-09-26 18:53:02 -07002654 *
Zbigniew Jędrzejewski-Szmek1b058bc2012-03-01 13:26:43 +01002655 * If there's not enough space, we will use the smaller of
2656 * stat_name_width (if set) and 5/8*width for the filename,
Zbigniew Jędrzejewski-Szmek969fe572012-03-01 13:26:45 +01002657 * and the rest for constant elements + graph part, but no more
2658 * than stat_graph_width for the graph part.
Zbigniew Jędrzejewski-Szmek1b058bc2012-03-01 13:26:43 +01002659 * (5/8 gives 50 for filename and 30 for the constant parts + graph
2660 * for the standard terminal size).
2661 *
2662 * In other words: stat_width limits the maximum width, and
2663 * stat_name_width fixes the maximum width of the filename,
2664 * and is also used to divide available columns if there
2665 * aren't enough.
Zbigniew Jędrzejewski-Szmekdc801e72012-04-30 22:38:58 +02002666 *
2667 * Binary files are displayed with "Bin XXX -> YYY bytes"
2668 * instead of the change count and graph. This part is treated
2669 * similarly to the graph part, except that it is not
Stefano Lattarini41ccfdd2013-04-12 00:36:10 +02002670 * "scaled". If total width is too small to accommodate the
Zbigniew Jędrzejewski-Szmekdc801e72012-04-30 22:38:58 +02002671 * guaranteed minimum width of the filename part and the
2672 * separators and this message, this message will "overflow"
2673 * making the line longer than the maximum width.
Junio C Hamanoa2540022006-09-26 18:53:02 -07002674 */
Junio C Hamanoa2540022006-09-26 18:53:02 -07002675
Zbigniew Jędrzejewski-Szmek1b058bc2012-03-01 13:26:43 +01002676 if (options->stat_width == -1)
Junio C Hamanocd48dad2016-08-31 16:27:19 -07002677 width = term_columns() - strlen(line_prefix);
Zbigniew Jędrzejewski-Szmek1b058bc2012-03-01 13:26:43 +01002678 else
2679 width = options->stat_width ? options->stat_width : 80;
Zbigniew Jędrzejewski-Szmekdc801e72012-04-30 22:38:58 +02002680 number_width = decimal_width(max_change) > number_width ?
2681 decimal_width(max_change) : number_width;
Zbigniew Jędrzejewski-Szmek1b058bc2012-03-01 13:26:43 +01002682
Zbigniew Jędrzejewski-Szmekdf444832012-03-01 13:26:46 +01002683 if (options->stat_graph_width == -1)
2684 options->stat_graph_width = diff_stat_graph_width;
2685
Zbigniew Jędrzejewski-Szmek1b058bc2012-03-01 13:26:43 +01002686 /*
2687 * Guarantee 3/8*16==6 for the graph part
2688 * and 5/8*16==10 for the filename part
2689 */
2690 if (width < 16 + 6 + number_width)
2691 width = 16 + 6 + number_width;
2692
2693 /*
2694 * First assign sizes that are wanted, ignoring available width.
Zbigniew Jędrzejewski-Szmekdc801e72012-04-30 22:38:58 +02002695 * strlen("Bin XXX -> YYY bytes") == bin_width, and the part
2696 * starting from "XXX" should fit in graph_width.
Zbigniew Jędrzejewski-Szmek1b058bc2012-03-01 13:26:43 +01002697 */
Zbigniew Jędrzejewski-Szmekdc801e72012-04-30 22:38:58 +02002698 graph_width = max_change + 4 > bin_width ? max_change : bin_width - 4;
2699 if (options->stat_graph_width &&
2700 options->stat_graph_width < graph_width)
2701 graph_width = options->stat_graph_width;
2702
Zbigniew Jędrzejewski-Szmek1b058bc2012-03-01 13:26:43 +01002703 name_width = (options->stat_name_width > 0 &&
2704 options->stat_name_width < max_len) ?
2705 options->stat_name_width : max_len;
2706
2707 /*
2708 * Adjust adjustable widths not to exceed maximum width
2709 */
2710 if (name_width + number_width + 6 + graph_width > width) {
Lucian Poston678c5742012-04-18 14:12:18 -07002711 if (graph_width > width * 3/8 - number_width - 6) {
Zbigniew Jędrzejewski-Szmek1b058bc2012-03-01 13:26:43 +01002712 graph_width = width * 3/8 - number_width - 6;
Lucian Poston678c5742012-04-18 14:12:18 -07002713 if (graph_width < 6)
2714 graph_width = 6;
2715 }
2716
Zbigniew Jędrzejewski-Szmek969fe572012-03-01 13:26:45 +01002717 if (options->stat_graph_width &&
2718 graph_width > options->stat_graph_width)
2719 graph_width = options->stat_graph_width;
Zbigniew Jędrzejewski-Szmek1b058bc2012-03-01 13:26:43 +01002720 if (name_width > width - number_width - 6 - graph_width)
2721 name_width = width - number_width - 6 - graph_width;
2722 else
2723 graph_width = width - number_width - 6 - name_width;
2724 }
2725
2726 /*
2727 * From here name_width is the width of the name area,
2728 * and graph_width is the width of the graph area.
2729 * max_change is used to scale graph properly.
2730 */
Michael J Gruber808e1db2011-05-27 14:36:41 +02002731 for (i = 0; i < count; i++) {
Timo Hirvonend2543b82006-06-24 20:20:32 +03002732 const char *prefix = "";
Junio C Hamanoaf0ed812012-11-27 11:24:54 -08002733 struct diffstat_file *file = data->files[i];
2734 char *name = file->print_name;
2735 uintmax_t added = file->added;
2736 uintmax_t deleted = file->deleted;
Junio C Hamanoa2540022006-09-26 18:53:02 -07002737 int name_len;
Junio C Hamano6973dca2006-04-21 23:57:45 -07002738
Junio C Hamanoa20d3c02012-11-27 11:47:46 -08002739 if (!file->is_interesting && (added + deleted == 0))
Michael J Gruber358e4602011-05-27 14:36:40 +02002740 continue;
Junio C Hamanoa20d3c02012-11-27 11:47:46 -08002741
Junio C Hamano6973dca2006-04-21 23:57:45 -07002742 /*
2743 * "scale" the filename
2744 */
Junio C Hamanoa2540022006-09-26 18:53:02 -07002745 len = name_width;
2746 name_len = strlen(name);
2747 if (name_width < name_len) {
Junio C Hamano6973dca2006-04-21 23:57:45 -07002748 char *slash;
2749 prefix = "...";
Junio C Hamanoa2540022006-09-26 18:53:02 -07002750 len -= 3;
2751 name += name_len - len;
Junio C Hamano6973dca2006-04-21 23:57:45 -07002752 slash = strchr(name, '/');
2753 if (slash)
2754 name = slash;
2755 }
Junio C Hamano6973dca2006-04-21 23:57:45 -07002756
Junio C Hamanoaf0ed812012-11-27 11:24:54 -08002757 if (file->is_binary) {
Stefan Beller0911c472017-06-29 17:07:02 -07002758 strbuf_addf(&out, " %s%-*s |", prefix, len, name);
2759 strbuf_addf(&out, " %*s", number_width, "Bin");
Zbigniew Jędrzejewski-Szmeke18872b2012-05-01 19:10:14 +02002760 if (!added && !deleted) {
Stefan Beller0911c472017-06-29 17:07:02 -07002761 strbuf_addch(&out, '\n');
2762 emit_diff_symbol(options, DIFF_SYMBOL_STATS_LINE,
2763 out.buf, out.len, 0);
2764 strbuf_reset(&out);
Zbigniew Jędrzejewski-Szmeke18872b2012-05-01 19:10:14 +02002765 continue;
2766 }
Stefan Beller0911c472017-06-29 17:07:02 -07002767 strbuf_addf(&out, " %s%"PRIuMAX"%s",
Jeff King0974c112010-04-17 13:41:08 -04002768 del_c, deleted, reset);
Stefan Beller0911c472017-06-29 17:07:02 -07002769 strbuf_addstr(&out, " -> ");
2770 strbuf_addf(&out, "%s%"PRIuMAX"%s",
Jeff King0974c112010-04-17 13:41:08 -04002771 add_c, added, reset);
Stefan Beller0911c472017-06-29 17:07:02 -07002772 strbuf_addstr(&out, " bytes\n");
2773 emit_diff_symbol(options, DIFF_SYMBOL_STATS_LINE,
2774 out.buf, out.len, 0);
2775 strbuf_reset(&out);
Junio C Hamanof6046522007-12-11 23:46:30 -08002776 continue;
Junio C Hamano6973dca2006-04-21 23:57:45 -07002777 }
Junio C Hamanoaf0ed812012-11-27 11:24:54 -08002778 else if (file->is_unmerged) {
Stefan Beller0911c472017-06-29 17:07:02 -07002779 strbuf_addf(&out, " %s%-*s |", prefix, len, name);
2780 strbuf_addstr(&out, " Unmerged\n");
2781 emit_diff_symbol(options, DIFF_SYMBOL_STATS_LINE,
2782 out.buf, out.len, 0);
2783 strbuf_reset(&out);
Junio C Hamanof6046522007-12-11 23:46:30 -08002784 continue;
Junio C Hamano6973dca2006-04-21 23:57:45 -07002785 }
Junio C Hamano6973dca2006-04-21 23:57:45 -07002786
Junio C Hamanoa2540022006-09-26 18:53:02 -07002787 /*
2788 * scale the add/delete
2789 */
Junio C Hamano6973dca2006-04-21 23:57:45 -07002790 add = added;
2791 del = deleted;
Junio C Hamano6973dca2006-04-21 23:57:45 -07002792
Zbigniew Jędrzejewski-Szmek1b058bc2012-03-01 13:26:43 +01002793 if (graph_width <= max_change) {
Stefan Bellerd3c9cf32013-07-14 23:35:49 +02002794 int total = scale_linear(add + del, graph_width, max_change);
Junio C Hamano2eeeef22012-02-14 13:49:11 -08002795 if (total < 2 && add && del)
2796 /* width >= 2 due to the sanity check */
2797 total = 2;
2798 if (add < del) {
Zbigniew Jędrzejewski-Szmek1b058bc2012-03-01 13:26:43 +01002799 add = scale_linear(add, graph_width, max_change);
Junio C Hamano2eeeef22012-02-14 13:49:11 -08002800 del = total - add;
2801 } else {
Zbigniew Jędrzejewski-Szmek1b058bc2012-03-01 13:26:43 +01002802 del = scale_linear(del, graph_width, max_change);
Junio C Hamano2eeeef22012-02-14 13:49:11 -08002803 add = total - del;
2804 }
Junio C Hamano6973dca2006-04-21 23:57:45 -07002805 }
Stefan Beller0911c472017-06-29 17:07:02 -07002806 strbuf_addf(&out, " %s%-*s |", prefix, len, name);
2807 strbuf_addf(&out, " %*"PRIuMAX"%s",
Zbigniew Jędrzejewski-Szmekdc801e72012-04-30 22:38:58 +02002808 number_width, added + deleted,
2809 added + deleted ? " " : "");
Stefan Beller0911c472017-06-29 17:07:02 -07002810 show_graph(&out, '+', add, add_c, reset);
2811 show_graph(&out, '-', del, del_c, reset);
2812 strbuf_addch(&out, '\n');
2813 emit_diff_symbol(options, DIFF_SYMBOL_STATS_LINE,
2814 out.buf, out.len, 0);
2815 strbuf_reset(&out);
Junio C Hamano6973dca2006-04-21 23:57:45 -07002816 }
Junio C Hamanoa20d3c02012-11-27 11:47:46 -08002817
2818 for (i = 0; i < data->nr; i++) {
Junio C Hamanoaf0ed812012-11-27 11:24:54 -08002819 struct diffstat_file *file = data->files[i];
2820 uintmax_t added = file->added;
2821 uintmax_t deleted = file->deleted;
Junio C Hamano82dfc2c2012-11-27 12:05:10 -08002822
2823 if (file->is_unmerged ||
2824 (!file->is_interesting && (added + deleted == 0))) {
Michael J Gruber808e1db2011-05-27 14:36:41 +02002825 total_files--;
2826 continue;
2827 }
Junio C Hamanoa20d3c02012-11-27 11:47:46 -08002828
Junio C Hamano82dfc2c2012-11-27 12:05:10 -08002829 if (!file->is_binary) {
Junio C Hamanoa20d3c02012-11-27 11:47:46 -08002830 adds += added;
2831 dels += deleted;
2832 }
2833 if (i < count)
2834 continue;
Junio C Hamanoe5f85df2011-05-27 21:50:39 -07002835 if (!extra_shown)
Stefan Beller0911c472017-06-29 17:07:02 -07002836 emit_diff_symbol(options,
2837 DIFF_SYMBOL_STATS_SUMMARY_ABBREV,
2838 NULL, 0, 0);
Junio C Hamanoe5f85df2011-05-27 21:50:39 -07002839 extra_shown = 1;
Michael J Gruber808e1db2011-05-27 14:36:41 +02002840 }
Stefan Beller0911c472017-06-29 17:07:02 -07002841
2842 print_stat_summary_inserts_deletes(options, total_files, adds, dels);
Rene Scharfe5a612012017-08-30 19:49:43 +02002843 strbuf_release(&out);
Junio C Hamano6973dca2006-04-21 23:57:45 -07002844}
2845
Felipe Contreras2775d922009-10-11 23:46:11 +03002846static void show_shortstats(struct diffstat_t *data, struct diff_options *options)
Nicolas Pitreebd124c2006-12-14 23:15:44 -05002847{
2848 int i, adds = 0, dels = 0, total_files = data->nr;
2849
2850 if (data->nr == 0)
2851 return;
2852
2853 for (i = 0; i < data->nr; i++) {
Zbigniew Jędrzejewski-Szmeke18872b2012-05-01 19:10:14 +02002854 int added = data->files[i]->added;
Stefan Beller0911c472017-06-29 17:07:02 -07002855 int deleted = data->files[i]->deleted;
Zbigniew Jędrzejewski-Szmeke18872b2012-05-01 19:10:14 +02002856
Junio C Hamano20c8cde2012-11-27 14:19:36 -08002857 if (data->files[i]->is_unmerged ||
2858 (!data->files[i]->is_interesting && (added + deleted == 0))) {
Zbigniew Jędrzejewski-Szmeke18872b2012-05-01 19:10:14 +02002859 total_files--;
Alexander Strasserde9658b2012-06-15 23:50:30 +02002860 } else if (!data->files[i]->is_binary) { /* don't count bytes */
Zbigniew Jędrzejewski-Szmeke18872b2012-05-01 19:10:14 +02002861 adds += added;
2862 dels += deleted;
Nicolas Pitreebd124c2006-12-14 23:15:44 -05002863 }
Nicolas Pitreebd124c2006-12-14 23:15:44 -05002864 }
Stefan Beller0911c472017-06-29 17:07:02 -07002865 print_stat_summary_inserts_deletes(options, total_files, adds, dels);
Nicolas Pitreebd124c2006-12-14 23:15:44 -05002866}
2867
Felipe Contreras4b25d092009-05-01 12:06:36 +03002868static void show_numstat(struct diffstat_t *data, struct diff_options *options)
Junio C Hamano74e2abe2006-10-12 03:01:00 -07002869{
2870 int i;
2871
Junio C Hamanof6046522007-12-11 23:46:30 -08002872 if (data->nr == 0)
2873 return;
2874
Junio C Hamano74e2abe2006-10-12 03:01:00 -07002875 for (i = 0; i < data->nr; i++) {
2876 struct diffstat_file *file = data->files[i];
2877
John Keeping30997bb2013-02-07 20:15:27 +00002878 fprintf(options->file, "%s", diff_line_prefix(options));
Bo Yang7be57612010-05-26 15:23:54 +08002879
Junio C Hamanobfddbc52006-12-10 13:50:59 -08002880 if (file->is_binary)
Daniel Barkalowc0c77732008-03-09 22:43:39 -04002881 fprintf(options->file, "-\t-\t");
Junio C Hamanobfddbc52006-12-10 13:50:59 -08002882 else
Daniel Barkalowc0c77732008-03-09 22:43:39 -04002883 fprintf(options->file,
Jeff King0974c112010-04-17 13:41:08 -04002884 "%"PRIuMAX"\t%"PRIuMAX"\t",
2885 file->added, file->deleted);
Junio C Hamanof6046522007-12-11 23:46:30 -08002886 if (options->line_termination) {
2887 fill_print_name(file);
2888 if (!file->is_renamed)
Daniel Barkalowc0c77732008-03-09 22:43:39 -04002889 write_name_quoted(file->name, options->file,
Junio C Hamanof6046522007-12-11 23:46:30 -08002890 options->line_termination);
2891 else {
Daniel Barkalowc0c77732008-03-09 22:43:39 -04002892 fputs(file->print_name, options->file);
2893 putc(options->line_termination, options->file);
Junio C Hamanof6046522007-12-11 23:46:30 -08002894 }
Pierre Habouzit663af342007-09-20 00:42:15 +02002895 } else {
Junio C Hamanof6046522007-12-11 23:46:30 -08002896 if (file->is_renamed) {
Daniel Barkalowc0c77732008-03-09 22:43:39 -04002897 putc('\0', options->file);
2898 write_name_quoted(file->from_name, options->file, '\0');
Junio C Hamanof6046522007-12-11 23:46:30 -08002899 }
Daniel Barkalowc0c77732008-03-09 22:43:39 -04002900 write_name_quoted(file->name, options->file, '\0');
Pierre Habouzit663af342007-09-20 00:42:15 +02002901 }
Junio C Hamano74e2abe2006-10-12 03:01:00 -07002902 }
2903}
2904
Junio C Hamanoc04a7152008-02-12 17:06:58 -08002905struct dirstat_file {
2906 const char *name;
2907 unsigned long changed;
Linus Torvalds7df7c012008-02-12 13:26:31 -08002908};
2909
Junio C Hamanoc04a7152008-02-12 17:06:58 -08002910struct dirstat_dir {
2911 struct dirstat_file *files;
Johan Herland712d2c72011-04-29 11:36:20 +02002912 int alloc, nr, permille, cumulative;
Junio C Hamanoc04a7152008-02-12 17:06:58 -08002913};
2914
Bo Yang7be57612010-05-26 15:23:54 +08002915static long gather_dirstat(struct diff_options *opt, struct dirstat_dir *dir,
2916 unsigned long changed, const char *base, int baselen)
Linus Torvalds7df7c012008-02-12 13:26:31 -08002917{
Brandon Williams585c0e22018-02-14 10:59:28 -08002918 unsigned long sum_changes = 0;
Linus Torvalds7df7c012008-02-12 13:26:31 -08002919 unsigned int sources = 0;
John Keeping30997bb2013-02-07 20:15:27 +00002920 const char *line_prefix = diff_line_prefix(opt);
Linus Torvalds7df7c012008-02-12 13:26:31 -08002921
2922 while (dir->nr) {
Junio C Hamanoc04a7152008-02-12 17:06:58 -08002923 struct dirstat_file *f = dir->files;
Linus Torvalds7df7c012008-02-12 13:26:31 -08002924 int namelen = strlen(f->name);
Brandon Williams585c0e22018-02-14 10:59:28 -08002925 unsigned long changes;
Linus Torvalds7df7c012008-02-12 13:26:31 -08002926 char *slash;
2927
2928 if (namelen < baselen)
2929 break;
2930 if (memcmp(f->name, base, baselen))
2931 break;
2932 slash = strchr(f->name + baselen, '/');
2933 if (slash) {
2934 int newbaselen = slash + 1 - f->name;
Brandon Williams585c0e22018-02-14 10:59:28 -08002935 changes = gather_dirstat(opt, dir, changed, f->name, newbaselen);
Linus Torvalds7df7c012008-02-12 13:26:31 -08002936 sources++;
2937 } else {
Brandon Williams585c0e22018-02-14 10:59:28 -08002938 changes = f->changed;
Linus Torvalds7df7c012008-02-12 13:26:31 -08002939 dir->files++;
2940 dir->nr--;
2941 sources += 2;
2942 }
Brandon Williams585c0e22018-02-14 10:59:28 -08002943 sum_changes += changes;
Linus Torvalds7df7c012008-02-12 13:26:31 -08002944 }
2945
2946 /*
2947 * We don't report dirstat's for
2948 * - the top level
2949 * - or cases where everything came from a single directory
2950 * under this directory (sources == 1).
2951 */
2952 if (baselen && sources != 1) {
Brandon Williams585c0e22018-02-14 10:59:28 -08002953 if (sum_changes) {
2954 int permille = sum_changes * 1000 / changed;
Johan Herland712d2c72011-04-29 11:36:20 +02002955 if (permille >= dir->permille) {
Bo Yang7be57612010-05-26 15:23:54 +08002956 fprintf(opt->file, "%s%4d.%01d%% %.*s\n", line_prefix,
Johan Herland712d2c72011-04-29 11:36:20 +02002957 permille / 10, permille % 10, baselen, base);
Linus Torvalds7df7c012008-02-12 13:26:31 -08002958 if (!dir->cumulative)
2959 return 0;
2960 }
2961 }
2962 }
Brandon Williams585c0e22018-02-14 10:59:28 -08002963 return sum_changes;
Linus Torvalds7df7c012008-02-12 13:26:31 -08002964}
2965
Linus Torvalds441bca02008-08-28 16:19:08 -07002966static int dirstat_compare(const void *_a, const void *_b)
2967{
2968 const struct dirstat_file *a = _a;
2969 const struct dirstat_file *b = _b;
2970 return strcmp(a->name, b->name);
2971}
2972
Junio C Hamanoc04a7152008-02-12 17:06:58 -08002973static void show_dirstat(struct diff_options *options)
Linus Torvalds7df7c012008-02-12 13:26:31 -08002974{
2975 int i;
2976 unsigned long changed;
Junio C Hamanoc04a7152008-02-12 17:06:58 -08002977 struct dirstat_dir dir;
2978 struct diff_queue_struct *q = &diff_queued_diff;
Linus Torvalds7df7c012008-02-12 13:26:31 -08002979
Junio C Hamanoc04a7152008-02-12 17:06:58 -08002980 dir.files = NULL;
2981 dir.alloc = 0;
2982 dir.nr = 0;
Johan Herland712d2c72011-04-29 11:36:20 +02002983 dir.permille = options->dirstat_permille;
Brandon Williams0d1e0e72017-10-31 11:19:11 -07002984 dir.cumulative = options->flags.dirstat_cumulative;
Junio C Hamanoc04a7152008-02-12 17:06:58 -08002985
Linus Torvalds7df7c012008-02-12 13:26:31 -08002986 changed = 0;
Junio C Hamanoc04a7152008-02-12 17:06:58 -08002987 for (i = 0; i < q->nr; i++) {
2988 struct diff_filepair *p = q->queue[i];
2989 const char *name;
2990 unsigned long copied, added, damage;
Jonathan Tan1c37e862020-04-07 15:11:41 -07002991 struct diff_populate_filespec_options dpf_options = {
2992 .check_size_only = 1,
2993 };
Junio C Hamanoc04a7152008-02-12 17:06:58 -08002994
Johan Herland2ca86712011-04-12 11:24:34 +02002995 name = p->two->path ? p->two->path : p->one->path;
Junio C Hamanoc04a7152008-02-12 17:06:58 -08002996
Jeff Kingd9f62df2018-08-28 17:23:03 -04002997 if (p->one->oid_valid && p->two->oid_valid &&
2998 oideq(&p->one->oid, &p->two->oid)) {
Johan Herland2ff3a802011-04-11 00:48:52 +02002999 /*
3000 * The SHA1 has not changed, so pre-/post-content is
3001 * identical. We can therefore skip looking at the
3002 * file contents altogether.
3003 */
3004 damage = 0;
3005 goto found_damage;
3006 }
3007
Brandon Williams0d1e0e72017-10-31 11:19:11 -07003008 if (options->flags.dirstat_by_file) {
Johan Herland0133dab2011-04-11 00:48:51 +02003009 /*
3010 * In --dirstat-by-file mode, we don't really need to
3011 * look at the actual file contents at all.
3012 * The fact that the SHA1 changed is enough for us to
3013 * add this file to the list of results
3014 * (with each file contributing equal damage).
3015 */
Johan Herland2ff3a802011-04-11 00:48:52 +02003016 damage = 1;
Johan Herland0133dab2011-04-11 00:48:51 +02003017 goto found_damage;
3018 }
Junio C Hamanoc04a7152008-02-12 17:06:58 -08003019
3020 if (DIFF_FILE_VALID(p->one) && DIFF_FILE_VALID(p->two)) {
Jonathan Tan1c37e862020-04-07 15:11:41 -07003021 diff_populate_filespec(options->repo, p->one, NULL);
3022 diff_populate_filespec(options->repo, p->two, NULL);
Nguyễn Thái Ngọc Duyb78ea5f2018-09-21 17:57:19 +02003023 diffcore_count_changes(options->repo,
3024 p->one, p->two, NULL, NULL,
Junio C Hamanoc04a7152008-02-12 17:06:58 -08003025 &copied, &added);
3026 diff_free_filespec_data(p->one);
3027 diff_free_filespec_data(p->two);
3028 } else if (DIFF_FILE_VALID(p->one)) {
Jonathan Tan1c37e862020-04-07 15:11:41 -07003029 diff_populate_filespec(options->repo, p->one, &dpf_options);
Junio C Hamanoc04a7152008-02-12 17:06:58 -08003030 copied = added = 0;
3031 diff_free_filespec_data(p->one);
3032 } else if (DIFF_FILE_VALID(p->two)) {
Jonathan Tan1c37e862020-04-07 15:11:41 -07003033 diff_populate_filespec(options->repo, p->two, &dpf_options);
Junio C Hamanoc04a7152008-02-12 17:06:58 -08003034 copied = 0;
3035 added = p->two->size;
3036 diff_free_filespec_data(p->two);
3037 } else
Junio C Hamano2b0b5512008-02-24 17:37:15 -08003038 continue;
Junio C Hamanoc04a7152008-02-12 17:06:58 -08003039
3040 /*
3041 * Original minus copied is the removed material,
3042 * added is the new material. They are both damages
Johan Herland0133dab2011-04-11 00:48:51 +02003043 * made to the preimage.
Johan Herland2ff3a802011-04-11 00:48:52 +02003044 * If the resulting damage is zero, we know that
3045 * diffcore_count_changes() considers the two entries to
Jeff Kingd9f62df2018-08-28 17:23:03 -04003046 * be identical, but since the oid changed, we
Johan Herland2ff3a802011-04-11 00:48:52 +02003047 * know that there must have been _some_ kind of change,
3048 * so we force all entries to have damage > 0.
Junio C Hamanoc04a7152008-02-12 17:06:58 -08003049 */
3050 damage = (p->one->size - copied) + added;
Johan Herland2ff3a802011-04-11 00:48:52 +02003051 if (!damage)
Heikki Orsilafd337772008-09-05 22:27:35 +03003052 damage = 1;
Junio C Hamanoc04a7152008-02-12 17:06:58 -08003053
Johan Herland0133dab2011-04-11 00:48:51 +02003054found_damage:
Junio C Hamanoc04a7152008-02-12 17:06:58 -08003055 ALLOC_GROW(dir.files, dir.nr + 1, dir.alloc);
3056 dir.files[dir.nr].name = name;
3057 dir.files[dir.nr].changed = damage;
3058 changed += damage;
3059 dir.nr++;
Linus Torvalds7df7c012008-02-12 13:26:31 -08003060 }
3061
3062 /* This can happen even with many files, if everything was renames */
3063 if (!changed)
3064 return;
3065
3066 /* Show all directories with more than x% of the changes */
René Scharfe9ed0d8d2016-09-29 17:27:31 +02003067 QSORT(dir.files, dir.nr, dirstat_compare);
Bo Yang7be57612010-05-26 15:23:54 +08003068 gather_dirstat(options, &dir, changed, "", 0);
Linus Torvalds7df7c012008-02-12 13:26:31 -08003069}
3070
Johan Herland1c57a622011-04-29 11:36:21 +02003071static void show_dirstat_by_line(struct diffstat_t *data, struct diff_options *options)
3072{
3073 int i;
3074 unsigned long changed;
3075 struct dirstat_dir dir;
3076
3077 if (data->nr == 0)
3078 return;
3079
3080 dir.files = NULL;
3081 dir.alloc = 0;
3082 dir.nr = 0;
3083 dir.permille = options->dirstat_permille;
Brandon Williams0d1e0e72017-10-31 11:19:11 -07003084 dir.cumulative = options->flags.dirstat_cumulative;
Johan Herland1c57a622011-04-29 11:36:21 +02003085
3086 changed = 0;
3087 for (i = 0; i < data->nr; i++) {
3088 struct diffstat_file *file = data->files[i];
3089 unsigned long damage = file->added + file->deleted;
3090 if (file->is_binary)
3091 /*
3092 * binary files counts bytes, not lines. Must find some
3093 * way to normalize binary bytes vs. textual lines.
3094 * The following heuristic assumes that there are 64
3095 * bytes per "line".
3096 * This is stupid and ugly, but very cheap...
3097 */
René Scharfe42c78a22017-07-08 12:35:35 +02003098 damage = DIV_ROUND_UP(damage, 64);
Johan Herland1c57a622011-04-29 11:36:21 +02003099 ALLOC_GROW(dir.files, dir.nr + 1, dir.alloc);
3100 dir.files[dir.nr].name = file->name;
3101 dir.files[dir.nr].changed = damage;
3102 changed += damage;
3103 dir.nr++;
3104 }
3105
3106 /* This can happen even with many files, if everything was renames */
3107 if (!changed)
3108 return;
3109
3110 /* Show all directories with more than x% of the changes */
René Scharfe9ed0d8d2016-09-29 17:27:31 +02003111 QSORT(dir.files, dir.nr, dirstat_compare);
Johan Herland1c57a622011-04-29 11:36:21 +02003112 gather_dirstat(options, &dir, changed, "", 0);
3113}
3114
Matthew Rogers1cf3d5d2020-08-20 00:41:32 +00003115static void free_diffstat_file(struct diffstat_file *f)
3116{
3117 free(f->print_name);
3118 free(f->name);
3119 free(f->from_name);
3120 free(f);
3121}
3122
Daniel Ferreirae4cb6592019-11-13 12:40:58 +00003123void free_diffstat_info(struct diffstat_t *diffstat)
Junio C Hamanof6046522007-12-11 23:46:30 -08003124{
3125 int i;
Matthew Rogers1cf3d5d2020-08-20 00:41:32 +00003126 for (i = 0; i < diffstat->nr; i++)
3127 free_diffstat_file(diffstat->files[i]);
Junio C Hamanof6046522007-12-11 23:46:30 -08003128 free(diffstat->files);
3129}
3130
Johannes Schindelin88246892006-05-20 23:43:13 +02003131struct checkdiff_t {
Johannes Schindelin88246892006-05-20 23:43:13 +02003132 const char *filename;
Junio C Hamano1ba111d2008-06-26 15:36:34 -07003133 int lineno;
Junio C Hamanoa757c642010-03-24 19:21:32 -07003134 int conflict_marker_size;
Junio C Hamano1ba111d2008-06-26 15:36:34 -07003135 struct diff_options *o;
Junio C Hamanocf1b7862007-12-06 00:14:14 -08003136 unsigned ws_rule;
Wincent Colaiuta62c64892007-12-13 21:24:52 +01003137 unsigned status;
Johannes Schindelin88246892006-05-20 23:43:13 +02003138};
3139
Junio C Hamanoa757c642010-03-24 19:21:32 -07003140static int is_conflict_marker(const char *line, int marker_size, unsigned long len)
Junio C Hamano04954042008-06-26 15:37:21 -07003141{
3142 char firstchar;
3143 int cnt;
3144
Junio C Hamanoa757c642010-03-24 19:21:32 -07003145 if (len < marker_size + 1)
Junio C Hamano04954042008-06-26 15:37:21 -07003146 return 0;
3147 firstchar = line[0];
3148 switch (firstchar) {
Junio C Hamanoa757c642010-03-24 19:21:32 -07003149 case '=': case '>': case '<': case '|':
Junio C Hamano04954042008-06-26 15:37:21 -07003150 break;
3151 default:
3152 return 0;
3153 }
Junio C Hamanoa757c642010-03-24 19:21:32 -07003154 for (cnt = 1; cnt < marker_size; cnt++)
Junio C Hamano04954042008-06-26 15:37:21 -07003155 if (line[cnt] != firstchar)
3156 return 0;
Elijah Newren15beaaa2019-11-05 17:07:23 +00003157 /* line[1] through line[marker_size-1] are same as firstchar */
Junio C Hamanoa757c642010-03-24 19:21:32 -07003158 if (len < marker_size + 1 || !isspace(line[marker_size]))
Junio C Hamano04954042008-06-26 15:37:21 -07003159 return 0;
Junio C Hamano04954042008-06-26 15:37:21 -07003160 return 1;
3161}
3162
Jeff King75ab7632018-11-02 02:39:03 -04003163static void checkdiff_consume_hunk(void *priv,
3164 long ob, long on, long nb, long nn,
3165 const char *func, long funclen)
3166
3167{
3168 struct checkdiff_t *data = priv;
3169 data->lineno = nb - 1;
3170}
3171
Ævar Arnfjörð Bjarmasona8d5eb62021-04-12 19:15:24 +02003172static int checkdiff_consume(void *priv, char *line, unsigned long len)
Johannes Schindelin88246892006-05-20 23:43:13 +02003173{
3174 struct checkdiff_t *data = priv;
Junio C Hamanoa757c642010-03-24 19:21:32 -07003175 int marker_size = data->conflict_marker_size;
Jeff Kingf1c96262011-08-17 22:03:12 -07003176 const char *ws = diff_get_color(data->o->use_color, DIFF_WHITESPACE);
3177 const char *reset = diff_get_color(data->o->use_color, DIFF_RESET);
3178 const char *set = diff_get_color(data->o->use_color, DIFF_FILE_NEW);
Wincent Colaiutac1795bb2007-12-13 14:32:29 +01003179 char *err;
John Keeping30997bb2013-02-07 20:15:27 +00003180 const char *line_prefix;
Bo Yang7be57612010-05-26 15:23:54 +08003181
3182 assert(data->o);
John Keeping30997bb2013-02-07 20:15:27 +00003183 line_prefix = diff_line_prefix(data->o);
Johannes Schindelin88246892006-05-20 23:43:13 +02003184
3185 if (line[0] == '+') {
Junio C Hamano18374e52008-06-26 13:16:33 -07003186 unsigned bad;
Junio C Hamano0ef617f2008-02-15 20:30:05 -08003187 data->lineno++;
Junio C Hamanoa757c642010-03-24 19:21:32 -07003188 if (is_conflict_marker(line + 1, marker_size, len - 1)) {
Junio C Hamano04954042008-06-26 15:37:21 -07003189 data->status |= 1;
3190 fprintf(data->o->file,
Bo Yang7be57612010-05-26 15:23:54 +08003191 "%s%s:%d: leftover conflict marker\n",
3192 line_prefix, data->filename, data->lineno);
Junio C Hamano04954042008-06-26 15:37:21 -07003193 }
Junio C Hamano8f8841e2008-06-26 15:35:21 -07003194 bad = ws_check(line + 1, len - 1, data->ws_rule);
Junio C Hamano18374e52008-06-26 13:16:33 -07003195 if (!bad)
Ævar Arnfjörð Bjarmasona8d5eb62021-04-12 19:15:24 +02003196 return 0;
Junio C Hamano18374e52008-06-26 13:16:33 -07003197 data->status |= bad;
3198 err = whitespace_error_string(bad);
Bo Yang7be57612010-05-26 15:23:54 +08003199 fprintf(data->o->file, "%s%s:%d: %s.\n",
3200 line_prefix, data->filename, data->lineno, err);
Wincent Colaiutac1795bb2007-12-13 14:32:29 +01003201 free(err);
Bo Yanga3c158d2010-05-26 15:08:02 +08003202 emit_line(data->o, set, reset, line, 1);
Junio C Hamano8f8841e2008-06-26 15:35:21 -07003203 ws_check_emit(line + 1, len - 1, data->ws_rule,
Junio C Hamano1ba111d2008-06-26 15:36:34 -07003204 data->o->file, set, reset, ws);
Junio C Hamano877f23c2008-06-26 15:36:59 -07003205 } else if (line[0] == ' ') {
Johannes Schindelin88246892006-05-20 23:43:13 +02003206 data->lineno++;
Johannes Schindelin88246892006-05-20 23:43:13 +02003207 }
Ævar Arnfjörð Bjarmasona8d5eb62021-04-12 19:15:24 +02003208 return 0;
Johannes Schindelin88246892006-05-20 23:43:13 +02003209}
3210
Junio C Hamano06606262006-05-05 02:41:53 -07003211static unsigned char *deflate_it(char *data,
3212 unsigned long size,
3213 unsigned long *result_size)
Junio C Hamano051308f2006-05-04 16:51:44 -07003214{
Junio C Hamano06606262006-05-05 02:41:53 -07003215 int bound;
3216 unsigned char *deflated;
Junio C Hamanoef49a7a2011-06-10 11:52:15 -07003217 git_zstream stream;
Junio C Hamano051308f2006-05-04 16:51:44 -07003218
Junio C Hamano55bb5c92011-06-10 10:55:10 -07003219 git_deflate_init(&stream, zlib_compression_level);
Junio C Hamano225a6f12011-06-10 11:18:17 -07003220 bound = git_deflate_bound(&stream, size);
Junio C Hamano06606262006-05-05 02:41:53 -07003221 deflated = xmalloc(bound);
3222 stream.next_out = deflated;
3223 stream.avail_out = bound;
Junio C Hamano051308f2006-05-04 16:51:44 -07003224
Junio C Hamano06606262006-05-05 02:41:53 -07003225 stream.next_in = (unsigned char *)data;
3226 stream.avail_in = size;
Junio C Hamano55bb5c92011-06-10 10:55:10 -07003227 while (git_deflate(&stream, Z_FINISH) == Z_OK)
Junio C Hamano06606262006-05-05 02:41:53 -07003228 ; /* nothing */
Junio C Hamano55bb5c92011-06-10 10:55:10 -07003229 git_deflate_end(&stream);
Junio C Hamano06606262006-05-05 02:41:53 -07003230 *result_size = stream.total_out;
3231 return deflated;
Junio C Hamano051308f2006-05-04 16:51:44 -07003232}
3233
Stefan Beller4eed0eb2017-06-29 17:07:01 -07003234static void emit_binary_diff_body(struct diff_options *o,
3235 mmfile_t *one, mmfile_t *two)
Junio C Hamano051308f2006-05-04 16:51:44 -07003236{
Junio C Hamano06606262006-05-05 02:41:53 -07003237 void *cp;
3238 void *delta;
3239 void *deflated;
3240 void *data;
3241 unsigned long orig_size;
Junio C Hamano051308f2006-05-04 16:51:44 -07003242 unsigned long delta_size;
Junio C Hamano06606262006-05-05 02:41:53 -07003243 unsigned long deflate_size;
3244 unsigned long data_size;
Junio C Hamano051308f2006-05-04 16:51:44 -07003245
Junio C Hamano06606262006-05-05 02:41:53 -07003246 /* We could do deflated delta, or we could do just deflated two,
3247 * whichever is smaller.
3248 */
3249 delta = NULL;
3250 deflated = deflate_it(two->ptr, two->size, &deflate_size);
3251 if (one->size && two->size) {
3252 delta = diff_delta(one->ptr, one->size,
3253 two->ptr, two->size,
3254 &delta_size, deflate_size);
3255 if (delta) {
3256 void *to_free = delta;
3257 orig_size = delta_size;
3258 delta = deflate_it(delta, delta_size, &delta_size);
3259 free(to_free);
3260 }
3261 }
Junio C Hamano051308f2006-05-04 16:51:44 -07003262
Junio C Hamano06606262006-05-05 02:41:53 -07003263 if (delta && delta_size < deflate_size) {
Torsten Bögershausenca473ce2018-11-11 08:05:04 +01003264 char *s = xstrfmt("%"PRIuMAX , (uintmax_t)orig_size);
Stefan Beller4eed0eb2017-06-29 17:07:01 -07003265 emit_diff_symbol(o, DIFF_SYMBOL_BINARY_DIFF_HEADER_DELTA,
3266 s, strlen(s), 0);
3267 free(s);
Junio C Hamano06606262006-05-05 02:41:53 -07003268 free(deflated);
3269 data = delta;
3270 data_size = delta_size;
Stefan Beller4eed0eb2017-06-29 17:07:01 -07003271 } else {
3272 char *s = xstrfmt("%lu", two->size);
3273 emit_diff_symbol(o, DIFF_SYMBOL_BINARY_DIFF_HEADER_LITERAL,
3274 s, strlen(s), 0);
3275 free(s);
Junio C Hamano06606262006-05-05 02:41:53 -07003276 free(delta);
3277 data = deflated;
3278 data_size = deflate_size;
3279 }
3280
3281 /* emit data encoded in base85 */
3282 cp = data;
3283 while (data_size) {
Stefan Beller4eed0eb2017-06-29 17:07:01 -07003284 int len;
Junio C Hamano06606262006-05-05 02:41:53 -07003285 int bytes = (52 < data_size) ? 52 : data_size;
Stefan Beller4eed0eb2017-06-29 17:07:01 -07003286 char line[71];
Junio C Hamano06606262006-05-05 02:41:53 -07003287 data_size -= bytes;
Junio C Hamano051308f2006-05-04 16:51:44 -07003288 if (bytes <= 26)
3289 line[0] = bytes + 'A' - 1;
3290 else
3291 line[0] = bytes - 26 + 'a' - 1;
3292 encode_85(line + 1, cp, bytes);
Florian Forster1d7f1712006-06-18 17:18:09 +02003293 cp = (char *) cp + bytes;
Stefan Beller4eed0eb2017-06-29 17:07:01 -07003294
3295 len = strlen(line);
3296 line[len++] = '\n';
3297 line[len] = '\0';
3298
3299 emit_diff_symbol(o, DIFF_SYMBOL_BINARY_DIFF_BODY,
3300 line, len, 0);
Junio C Hamano051308f2006-05-04 16:51:44 -07003301 }
Stefan Beller4eed0eb2017-06-29 17:07:01 -07003302 emit_diff_symbol(o, DIFF_SYMBOL_BINARY_DIFF_FOOTER, NULL, 0, 0);
Junio C Hamano06606262006-05-05 02:41:53 -07003303 free(data);
Junio C Hamano051308f2006-05-04 16:51:44 -07003304}
3305
Stefan Beller4eed0eb2017-06-29 17:07:01 -07003306static void emit_binary_diff(struct diff_options *o,
3307 mmfile_t *one, mmfile_t *two)
Junio C Hamanod4c452f2006-08-16 16:08:14 -07003308{
Stefan Beller4eed0eb2017-06-29 17:07:01 -07003309 emit_diff_symbol(o, DIFF_SYMBOL_BINARY_DIFF_HEADER, NULL, 0, 0);
3310 emit_binary_diff_body(o, one, two);
3311 emit_binary_diff_body(o, two, one);
Junio C Hamanod4c452f2006-08-16 16:08:14 -07003312}
3313
Nguyễn Thái Ngọc Duyb78ea5f2018-09-21 17:57:19 +02003314int diff_filespec_is_binary(struct repository *r,
3315 struct diff_filespec *one)
Junio C Hamano29a3eef2007-07-06 00:18:54 -07003316{
Jonathan Tan1c37e862020-04-07 15:11:41 -07003317 struct diff_populate_filespec_options dpf_options = {
3318 .check_binary = 1,
3319 };
3320
Jeff King122aa6f2008-10-05 17:43:36 -04003321 if (one->is_binary == -1) {
Nguyễn Thái Ngọc Duyacd00ea2018-09-21 17:57:33 +02003322 diff_filespec_load_driver(one, r->index);
Jeff King122aa6f2008-10-05 17:43:36 -04003323 if (one->driver->binary != -1)
3324 one->is_binary = one->driver->binary;
3325 else {
3326 if (!one->data && DIFF_FILE_VALID(one))
Jonathan Tan1c37e862020-04-07 15:11:41 -07003327 diff_populate_filespec(r, one, &dpf_options);
Nguyễn Thái Ngọc Duy6bf3b812014-08-16 10:08:05 +07003328 if (one->is_binary == -1 && one->data)
Jeff King122aa6f2008-10-05 17:43:36 -04003329 one->is_binary = buffer_is_binary(one->data,
3330 one->size);
3331 if (one->is_binary == -1)
3332 one->is_binary = 0;
3333 }
3334 }
Junio C Hamano29a3eef2007-07-06 00:18:54 -07003335 return one->is_binary;
Junio C Hamano6973dca2006-04-21 23:57:45 -07003336}
3337
Nguyễn Thái Ngọc Duyacd00ea2018-09-21 17:57:33 +02003338static const struct userdiff_funcname *
3339diff_funcname_pattern(struct diff_options *o, struct diff_filespec *one)
Junio C Hamanof2584752007-07-06 00:45:10 -07003340{
Nguyễn Thái Ngọc Duyacd00ea2018-09-21 17:57:33 +02003341 diff_filespec_load_driver(one, o->repo->index);
Jeff King122aa6f2008-10-05 17:43:36 -04003342 return one->driver->funcname.pattern ? &one->driver->funcname : NULL;
Junio C Hamanof2584752007-07-06 00:45:10 -07003343}
3344
Junio C Hamanoa5a818e2008-08-18 20:08:09 -07003345void diff_set_mnemonic_prefix(struct diff_options *options, const char *a, const char *b)
3346{
3347 if (!options->a_prefix)
3348 options->a_prefix = a;
3349 if (!options->b_prefix)
3350 options->b_prefix = b;
3351}
3352
Nguyễn Thái Ngọc Duybd7ad452018-11-10 06:49:06 +01003353struct userdiff_driver *get_textconv(struct repository *r,
Nguyễn Thái Ngọc Duyacd00ea2018-09-21 17:57:33 +02003354 struct diff_filespec *one)
Jeff King04427ac2008-10-26 00:44:53 -04003355{
3356 if (!DIFF_FILE_VALID(one))
3357 return NULL;
Jeff Kingd391c0f2010-09-21 17:01:24 -04003358
Nguyễn Thái Ngọc Duybd7ad452018-11-10 06:49:06 +01003359 diff_filespec_load_driver(one, r->index);
3360 return userdiff_get_textconv(r, one->driver);
Jeff King04427ac2008-10-26 00:44:53 -04003361}
3362
Johannes Schindelin2715e8a2022-06-18 00:20:55 +00003363static struct string_list *additional_headers(struct diff_options *o,
3364 const char *path)
Elijah Newren95433ee2022-02-02 02:37:34 +00003365{
3366 if (!o->additional_path_headers)
3367 return NULL;
3368 return strmap_get(o->additional_path_headers, path);
3369}
3370
Johannes Schindelin2715e8a2022-06-18 00:20:55 +00003371static void add_formatted_header(struct strbuf *msg,
3372 const char *header,
Elijah Newren95433ee2022-02-02 02:37:34 +00003373 const char *line_prefix,
3374 const char *meta,
3375 const char *reset)
3376{
Johannes Schindelin2715e8a2022-06-18 00:20:55 +00003377 const char *next, *newline;
Elijah Newren95433ee2022-02-02 02:37:34 +00003378
Johannes Schindelin2715e8a2022-06-18 00:20:55 +00003379 for (next = header; *next; next = newline) {
Elijah Newren95433ee2022-02-02 02:37:34 +00003380 newline = strchrnul(next, '\n');
3381 strbuf_addf(msg, "%s%s%.*s%s\n", line_prefix, meta,
3382 (int)(newline - next), next, reset);
3383 if (*newline)
3384 newline++;
3385 }
3386}
3387
Johannes Schindelin2715e8a2022-06-18 00:20:55 +00003388static void add_formatted_headers(struct strbuf *msg,
3389 struct string_list *more_headers,
3390 const char *line_prefix,
3391 const char *meta,
3392 const char *reset)
3393{
3394 int i;
3395
3396 for (i = 0; i < more_headers->nr; i++)
3397 add_formatted_header(msg, more_headers->items[i].string,
3398 line_prefix, meta, reset);
3399}
3400
Junio C Hamano6973dca2006-04-21 23:57:45 -07003401static void builtin_diff(const char *name_a,
3402 const char *name_b,
3403 struct diff_filespec *one,
3404 struct diff_filespec *two,
3405 const char *xfrm_msg,
Christian Couder296c6bb2010-05-26 04:50:12 +02003406 int must_show_header,
Junio C Hamano051308f2006-05-04 16:51:44 -07003407 struct diff_options *o,
Junio C Hamano6973dca2006-04-21 23:57:45 -07003408 int complete_rewrite)
3409{
3410 mmfile_t mf1, mf2;
3411 const char *lbl[2];
3412 char *a_one, *b_two;
Jeff Kingd9c552f2012-11-13 21:12:46 +05303413 const char *meta = diff_get_color_opt(o, DIFF_METAINFO);
Pierre Habouzit8f67f8a2007-11-10 20:05:14 +01003414 const char *reset = diff_get_color_opt(o, DIFF_RESET);
Junio C Hamanoa5a818e2008-08-18 20:08:09 -07003415 const char *a_prefix, *b_prefix;
Jeff Kingd9bae1a2010-04-01 20:12:15 -04003416 struct userdiff_driver *textconv_one = NULL;
3417 struct userdiff_driver *textconv_two = NULL;
Greg Bacon3e97c7c2009-11-19 15:12:24 -06003418 struct strbuf header = STRBUF_INIT;
John Keeping30997bb2013-02-07 20:15:27 +00003419 const char *line_prefix = diff_line_prefix(o);
Junio C Hamano6973dca2006-04-21 23:57:45 -07003420
Junio C Hamanoa5a818e2008-08-18 20:08:09 -07003421 diff_set_mnemonic_prefix(o, "a/", "b/");
Brandon Williams0d1e0e72017-10-31 11:19:11 -07003422 if (o->flags.reverse_diff) {
Junio C Hamanoa5a818e2008-08-18 20:08:09 -07003423 a_prefix = o->b_prefix;
3424 b_prefix = o->a_prefix;
3425 } else {
3426 a_prefix = o->a_prefix;
3427 b_prefix = o->b_prefix;
3428 }
3429
Jacob Keller61cfbc02016-08-31 16:27:21 -07003430 if (o->submodule_format == DIFF_SUBMODULE_LOG &&
3431 (!one->mode || S_ISGITLINK(one->mode)) &&
3432 (!two->mode || S_ISGITLINK(two->mode))) {
Shourya Shukla180b1542020-08-13 01:14:02 +05303433 show_submodule_diff_summary(o, one->path ? one->path : two->path,
Jacob Keller602a2832016-08-31 16:27:23 -07003434 &one->oid, &two->oid,
Stefan Bellerf3597132017-06-29 17:07:00 -07003435 two->dirty_submodule);
Junio C Hamano6973dca2006-04-21 23:57:45 -07003436 return;
Jacob Kellerfd47ae62016-08-31 16:27:25 -07003437 } else if (o->submodule_format == DIFF_SUBMODULE_INLINE_DIFF &&
3438 (!one->mode || S_ISGITLINK(one->mode)) &&
3439 (!two->mode || S_ISGITLINK(two->mode))) {
Stefan Bellerf3597132017-06-29 17:07:00 -07003440 show_submodule_inline_diff(o, one->path ? one->path : two->path,
Jacob Kellerfd47ae62016-08-31 16:27:25 -07003441 &one->oid, &two->oid,
Stefan Bellerf3597132017-06-29 17:07:00 -07003442 two->dirty_submodule);
Jacob Kellerfd47ae62016-08-31 16:27:25 -07003443 return;
Junio C Hamano6973dca2006-04-21 23:57:45 -07003444 }
3445
Brandon Williams0d1e0e72017-10-31 11:19:11 -07003446 if (o->flags.allow_textconv) {
Nguyễn Thái Ngọc Duybd7ad452018-11-10 06:49:06 +01003447 textconv_one = get_textconv(o->repo, one);
3448 textconv_two = get_textconv(o->repo, two);
Junio C Hamano6973dca2006-04-21 23:57:45 -07003449 }
3450
Linus Torvalds71b989e2008-10-05 15:35:15 -04003451 /* Never use a non-valid filename anywhere if at all possible */
3452 name_a = DIFF_FILE_VALID(one) ? name_a : name_b;
3453 name_b = DIFF_FILE_VALID(two) ? name_b : name_a;
3454
Junio C Hamanoa5a818e2008-08-18 20:08:09 -07003455 a_one = quote_two(a_prefix, name_a + (*name_a == '/'));
3456 b_two = quote_two(b_prefix, name_b + (*name_b == '/'));
Junio C Hamano6973dca2006-04-21 23:57:45 -07003457 lbl[0] = DIFF_FILE_VALID(one) ? a_one : "/dev/null";
3458 lbl[1] = DIFF_FILE_VALID(two) ? b_two : "/dev/null";
Elijah Newren95433ee2022-02-02 02:37:34 +00003459 if (!DIFF_FILE_VALID(one) && !DIFF_FILE_VALID(two)) {
3460 /*
3461 * We should only reach this point for pairs from
3462 * create_filepairs_for_header_only_notifications(). For
3463 * these, we should avoid the "/dev/null" special casing
3464 * above, meaning we avoid showing such pairs as either
3465 * "new file" or "deleted file" below.
3466 */
3467 lbl[0] = a_one;
3468 lbl[1] = b_two;
3469 }
Jeff Kingd9c552f2012-11-13 21:12:46 +05303470 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 -07003471 if (lbl[0][0] == '/') {
3472 /* /dev/null */
Jeff Kingd9c552f2012-11-13 21:12:46 +05303473 strbuf_addf(&header, "%s%snew file mode %06o%s\n", line_prefix, meta, two->mode, reset);
Bert Wesarg37466442010-05-04 00:38:07 +02003474 if (xfrm_msg)
3475 strbuf_addstr(&header, xfrm_msg);
Christian Couder296c6bb2010-05-26 04:50:12 +02003476 must_show_header = 1;
Junio C Hamano6973dca2006-04-21 23:57:45 -07003477 }
3478 else if (lbl[1][0] == '/') {
Jeff Kingd9c552f2012-11-13 21:12:46 +05303479 strbuf_addf(&header, "%s%sdeleted file mode %06o%s\n", line_prefix, meta, one->mode, reset);
Bert Wesarg37466442010-05-04 00:38:07 +02003480 if (xfrm_msg)
3481 strbuf_addstr(&header, xfrm_msg);
Christian Couder296c6bb2010-05-26 04:50:12 +02003482 must_show_header = 1;
Junio C Hamano6973dca2006-04-21 23:57:45 -07003483 }
3484 else {
3485 if (one->mode != two->mode) {
Jeff Kingd9c552f2012-11-13 21:12:46 +05303486 strbuf_addf(&header, "%s%sold mode %06o%s\n", line_prefix, meta, one->mode, reset);
3487 strbuf_addf(&header, "%s%snew mode %06o%s\n", line_prefix, meta, two->mode, reset);
Christian Couder296c6bb2010-05-26 04:50:12 +02003488 must_show_header = 1;
Junio C Hamano6973dca2006-04-21 23:57:45 -07003489 }
Bert Wesarg37466442010-05-04 00:38:07 +02003490 if (xfrm_msg)
3491 strbuf_addstr(&header, xfrm_msg);
Greg Bacon3e97c7c2009-11-19 15:12:24 -06003492
Junio C Hamano6973dca2006-04-21 23:57:45 -07003493 /*
3494 * we do not run diff between different kind
3495 * of objects.
3496 */
3497 if ((one->mode ^ two->mode) & S_IFMT)
3498 goto free_ab_and_return;
Jeff King0c018572008-12-09 03:12:28 -05003499 if (complete_rewrite &&
Nguyễn Thái Ngọc Duyb78ea5f2018-09-21 17:57:19 +02003500 (textconv_one || !diff_filespec_is_binary(o->repo, one)) &&
3501 (textconv_two || !diff_filespec_is_binary(o->repo, two))) {
Stefan Bellera29b0a12017-06-29 17:06:57 -07003502 emit_diff_symbol(o, DIFF_SYMBOL_HEADER,
3503 header.buf, header.len, 0);
Greg Bacon3e97c7c2009-11-19 15:12:24 -06003504 strbuf_reset(&header);
Jeff King3aa1f7c2008-12-09 03:13:21 -05003505 emit_rewrite_diff(name_a, name_b, one, two,
Nguyễn Thái Ngọc Duyb78ea5f2018-09-21 17:57:19 +02003506 textconv_one, textconv_two, o);
Johannes Schindelin34a5e1a2007-02-25 23:34:54 +01003507 o->found_changes = 1;
Junio C Hamano6973dca2006-04-21 23:57:45 -07003508 goto free_ab_and_return;
3509 }
3510 }
3511
Junio C Hamano467ddc12011-02-28 16:11:55 -08003512 if (o->irreversible_delete && lbl[1][0] == '/') {
Stefan Bellera29b0a12017-06-29 17:06:57 -07003513 emit_diff_symbol(o, DIFF_SYMBOL_HEADER, header.buf,
3514 header.len, 0);
Junio C Hamano467ddc12011-02-28 16:11:55 -08003515 strbuf_reset(&header);
3516 goto free_ab_and_return;
Brandon Williams0d1e0e72017-10-31 11:19:11 -07003517 } else if (!o->flags.text &&
Nguyễn Thái Ngọc Duyb78ea5f2018-09-21 17:57:19 +02003518 ( (!textconv_one && diff_filespec_is_binary(o->repo, one)) ||
3519 (!textconv_two && diff_filespec_is_binary(o->repo, two)) )) {
Stefan Beller4acaaa72017-06-29 17:06:58 -07003520 struct strbuf sb = STRBUF_INIT;
Nguyễn Thái Ngọc Duy1aaf69e2014-08-16 10:08:06 +07003521 if (!one->data && !two->data &&
3522 S_ISREG(one->mode) && S_ISREG(two->mode) &&
Brandon Williams0d1e0e72017-10-31 11:19:11 -07003523 !o->flags.binary) {
Jeff King4a7e27e2018-08-28 17:22:40 -04003524 if (oideq(&one->oid, &two->oid)) {
Nguyễn Thái Ngọc Duy1aaf69e2014-08-16 10:08:06 +07003525 if (must_show_header)
Stefan Bellera29b0a12017-06-29 17:06:57 -07003526 emit_diff_symbol(o, DIFF_SYMBOL_HEADER,
3527 header.buf, header.len,
3528 0);
Nguyễn Thái Ngọc Duy1aaf69e2014-08-16 10:08:06 +07003529 goto free_ab_and_return;
3530 }
Stefan Bellera29b0a12017-06-29 17:06:57 -07003531 emit_diff_symbol(o, DIFF_SYMBOL_HEADER,
3532 header.buf, header.len, 0);
Stefan Beller4acaaa72017-06-29 17:06:58 -07003533 strbuf_addf(&sb, "%sBinary files %s and %s differ\n",
3534 diff_line_prefix(o), lbl[0], lbl[1]);
3535 emit_diff_symbol(o, DIFF_SYMBOL_BINARY_FILES,
3536 sb.buf, sb.len, 0);
3537 strbuf_release(&sb);
Nguyễn Thái Ngọc Duy1aaf69e2014-08-16 10:08:06 +07003538 goto free_ab_and_return;
3539 }
Nguyễn Thái Ngọc Duyb78ea5f2018-09-21 17:57:19 +02003540 if (fill_mmfile(o->repo, &mf1, one) < 0 ||
3541 fill_mmfile(o->repo, &mf2, two) < 0)
Jeff Kingb3373982010-04-01 20:14:24 -04003542 die("unable to read files to diff");
Junio C Hamano06606262006-05-05 02:41:53 -07003543 /* Quite common confusing case */
3544 if (mf1.size == mf2.size &&
Christian Couder296c6bb2010-05-26 04:50:12 +02003545 !memcmp(mf1.ptr, mf2.ptr, mf1.size)) {
3546 if (must_show_header)
Stefan Bellera29b0a12017-06-29 17:06:57 -07003547 emit_diff_symbol(o, DIFF_SYMBOL_HEADER,
3548 header.buf, header.len, 0);
Junio C Hamano06606262006-05-05 02:41:53 -07003549 goto free_ab_and_return;
Christian Couder296c6bb2010-05-26 04:50:12 +02003550 }
Stefan Bellera29b0a12017-06-29 17:06:57 -07003551 emit_diff_symbol(o, DIFF_SYMBOL_HEADER, header.buf, header.len, 0);
Greg Bacon3e97c7c2009-11-19 15:12:24 -06003552 strbuf_reset(&header);
Brandon Williams0d1e0e72017-10-31 11:19:11 -07003553 if (o->flags.binary)
Stefan Beller4eed0eb2017-06-29 17:07:01 -07003554 emit_binary_diff(o, &mf1, &mf2);
Stefan Beller4acaaa72017-06-29 17:06:58 -07003555 else {
3556 strbuf_addf(&sb, "%sBinary files %s and %s differ\n",
3557 diff_line_prefix(o), lbl[0], lbl[1]);
3558 emit_diff_symbol(o, DIFF_SYMBOL_BINARY_FILES,
3559 sb.buf, sb.len, 0);
3560 strbuf_release(&sb);
3561 }
Johannes Schindelin34a5e1a2007-02-25 23:34:54 +01003562 o->found_changes = 1;
Junio C Hamano467ddc12011-02-28 16:11:55 -08003563 } else {
Junio C Hamano6973dca2006-04-21 23:57:45 -07003564 /* Crazy xdl interfaces.. */
Jeff King0da0e922019-01-11 17:17:22 -05003565 const char *diffopts;
Jeff Kingae021d82014-06-18 15:47:50 -04003566 const char *v;
Junio C Hamano6973dca2006-04-21 23:57:45 -07003567 xpparam_t xpp;
3568 xdemitconf_t xecfg;
Junio C Hamano6973dca2006-04-21 23:57:45 -07003569 struct emit_callback ecbdata;
Jeff Kingbe58e702008-10-05 17:43:21 -04003570 const struct userdiff_funcname *pe;
Junio C Hamanof2584752007-07-06 00:45:10 -07003571
Junio C Hamanob3f01ff2012-02-29 18:14:16 -08003572 if (must_show_header) {
Stefan Bellera29b0a12017-06-29 17:06:57 -07003573 emit_diff_symbol(o, DIFF_SYMBOL_HEADER,
3574 header.buf, header.len, 0);
Greg Bacon3e97c7c2009-11-19 15:12:24 -06003575 strbuf_reset(&header);
3576 }
3577
Nguyễn Thái Ngọc Duy6afaf802018-09-21 17:57:22 +02003578 mf1.size = fill_textconv(o->repo, textconv_one, one, &mf1.ptr);
3579 mf2.size = fill_textconv(o->repo, textconv_two, two, &mf2.ptr);
Jeff King04427ac2008-10-26 00:44:53 -04003580
Nguyễn Thái Ngọc Duyacd00ea2018-09-21 17:57:33 +02003581 pe = diff_funcname_pattern(o, one);
Brandon Casey45e7ca02008-09-18 17:40:48 -05003582 if (!pe)
Nguyễn Thái Ngọc Duyacd00ea2018-09-21 17:57:33 +02003583 pe = diff_funcname_pattern(o, two);
Junio C Hamano6973dca2006-04-21 23:57:45 -07003584
Brian Downing9ccd0a82008-10-25 15:30:37 +02003585 memset(&xpp, 0, sizeof(xpp));
Johannes Schindelin30b25012007-07-04 19:05:46 +01003586 memset(&xecfg, 0, sizeof(xecfg));
Johannes Schindelincd112ce2006-06-13 18:45:44 +02003587 memset(&ecbdata, 0, sizeof(ecbdata));
Johannes Schindelin1cdde292018-08-13 04:33:11 -07003588 if (o->flags.suppress_diff_headers)
3589 lbl[0] = NULL;
Junio C Hamano6973dca2006-04-21 23:57:45 -07003590 ecbdata.label_path = lbl;
Jeff Kingdaa0c3d2011-08-17 22:04:23 -07003591 ecbdata.color_diff = want_color(o->use_color);
Nguyễn Thái Ngọc Duy26d024e2018-09-21 17:57:37 +02003592 ecbdata.ws_rule = whitespace_rule(o->repo->index, name_b);
Junio C Hamano690ed842009-09-04 00:41:15 -07003593 if (ecbdata.ws_rule & WS_BLANK_AT_EOF)
Junio C Hamanod68fe262009-09-14 22:05:57 -07003594 check_blank_at_eof(&mf1, &mf2, &ecbdata);
Bo Yanga3c158d2010-05-26 15:08:02 +08003595 ecbdata.opt = o;
Johannes Schindelin1cdde292018-08-13 04:33:11 -07003596 if (header.len && !o->flags.suppress_diff_headers)
3597 ecbdata.header = &header;
René Scharfe582aa002010-05-02 15:04:41 +02003598 xpp.flags = o->xdl_opts;
Michał Kępień296d4a92020-10-20 08:48:09 +02003599 xpp.ignore_regex = o->ignore_regex;
3600 xpp.ignore_regex_nr = o->ignore_regex_nr;
Jonathan Tan2477ab22017-11-27 11:47:47 -08003601 xpp.anchors = o->anchors;
3602 xpp.anchors_nr = o->anchors_nr;
Linus Torvaldsee1e5412006-05-13 13:23:48 -07003603 xecfg.ctxlen = o->context;
René Scharfe6d0e6742008-12-28 19:45:32 +01003604 xecfg.interhunkctxlen = o->interhunkcontext;
Junio C Hamano6973dca2006-04-21 23:57:45 -07003605 xecfg.flags = XDL_EMIT_FUNCNAMES;
Brandon Williams0d1e0e72017-10-31 11:19:11 -07003606 if (o->flags.funccontext)
René Scharfe14937c22011-10-09 13:36:57 +02003607 xecfg.flags |= XDL_EMIT_FUNCCONTEXT;
Brandon Casey45e7ca02008-09-18 17:40:48 -05003608 if (pe)
Brandon Caseya0135852008-09-18 17:42:48 -05003609 xdiff_set_find_func(&xecfg, pe->pattern, pe->cflags);
Jeff King0da0e922019-01-11 17:17:22 -05003610
3611 diffopts = getenv("GIT_DIFF_OPTS");
Junio C Hamano6973dca2006-04-21 23:57:45 -07003612 if (!diffopts)
3613 ;
Jeff Kingae021d82014-06-18 15:47:50 -04003614 else if (skip_prefix(diffopts, "--unified=", &v))
3615 xecfg.ctxlen = strtoul(v, NULL, 10);
3616 else if (skip_prefix(diffopts, "-u", &v))
3617 xecfg.ctxlen = strtoul(v, NULL, 10);
Jeff King0da0e922019-01-11 17:17:22 -05003618
Thomas Rast77d1a522012-03-14 19:24:08 +01003619 if (o->word_diff)
3620 init_diff_words_data(&ecbdata, o, one, two);
Jeff King9346d6d2018-11-02 02:35:45 -04003621 if (xdi_diff_outf(&mf1, &mf2, NULL, fn_out_consume,
3622 &ecbdata, &xpp, &xecfg))
Jeff King3efb9882015-09-24 19:12:23 -04003623 die("unable to generate diff for %s", one->path);
Thomas Rast882749a2010-04-14 17:59:06 +02003624 if (o->word_diff)
Johannes Schindelinf59a59e2006-07-28 23:56:15 +02003625 free_diff_words_data(&ecbdata);
Jeff King04427ac2008-10-26 00:44:53 -04003626 if (textconv_one)
3627 free(mf1.ptr);
3628 if (textconv_two)
3629 free(mf2.ptr);
René Scharfe8cfe5f12009-07-02 00:01:43 +02003630 xdiff_clear_find_func(&xecfg);
Junio C Hamano6973dca2006-04-21 23:57:45 -07003631 }
3632
3633 free_ab_and_return:
Greg Bacon3e97c7c2009-11-19 15:12:24 -06003634 strbuf_release(&header);
Junio C Hamanofc3abdf2007-05-03 13:05:48 -07003635 diff_free_filespec_data(one);
3636 diff_free_filespec_data(two);
Junio C Hamano6973dca2006-04-21 23:57:45 -07003637 free(a_one);
3638 free(b_two);
3639 return;
3640}
3641
Nguyễn Thái Ngọc Duyddf88fa2018-02-24 21:09:59 +07003642static char *get_compact_summary(const struct diff_filepair *p, int is_renamed)
3643{
3644 if (!is_renamed) {
3645 if (p->status == DIFF_STATUS_ADDED) {
3646 if (S_ISLNK(p->two->mode))
3647 return "new +l";
3648 else if ((p->two->mode & 0777) == 0755)
3649 return "new +x";
3650 else
3651 return "new";
3652 } else if (p->status == DIFF_STATUS_DELETED)
3653 return "gone";
3654 }
3655 if (S_ISLNK(p->one->mode) && !S_ISLNK(p->two->mode))
3656 return "mode -l";
3657 else if (!S_ISLNK(p->one->mode) && S_ISLNK(p->two->mode))
3658 return "mode +l";
3659 else if ((p->one->mode & 0777) == 0644 &&
3660 (p->two->mode & 0777) == 0755)
3661 return "mode +x";
3662 else if ((p->one->mode & 0777) == 0755 &&
3663 (p->two->mode & 0777) == 0644)
3664 return "mode -x";
3665 return NULL;
3666}
3667
Junio C Hamano6973dca2006-04-21 23:57:45 -07003668static void builtin_diffstat(const char *name_a, const char *name_b,
3669 struct diff_filespec *one,
3670 struct diff_filespec *two,
Junio C Hamano710158e2006-04-25 23:40:09 -07003671 struct diffstat_t *diffstat,
Johannes Schindelin0d21efa2006-06-14 17:40:23 +02003672 struct diff_options *o,
Linus Torvalds74faaa12012-10-17 10:00:37 -07003673 struct diff_filepair *p)
Junio C Hamano6973dca2006-04-21 23:57:45 -07003674{
3675 mmfile_t mf1, mf2;
3676 struct diffstat_file *data;
Thomas Guyot-Sionnestff0c7fa2020-09-24 03:41:41 -04003677 int may_differ;
Linus Torvalds74faaa12012-10-17 10:00:37 -07003678 int complete_rewrite = 0;
3679
3680 if (!DIFF_PAIR_UNMERGED(p)) {
3681 if (p->status == DIFF_STATUS_MODIFIED && p->score)
3682 complete_rewrite = 1;
3683 }
Junio C Hamano6973dca2006-04-21 23:57:45 -07003684
3685 data = diffstat_add(diffstat, name_a, name_b);
Junio C Hamano99bfd402012-11-27 11:17:14 -08003686 data->is_interesting = p->status != DIFF_STATUS_UNKNOWN;
Nguyễn Thái Ngọc Duyddf88fa2018-02-24 21:09:59 +07003687 if (o->flags.stat_with_summary)
3688 data->comments = get_compact_summary(p, data->is_renamed);
Junio C Hamano6973dca2006-04-21 23:57:45 -07003689
3690 if (!one || !two) {
3691 data->is_unmerged = 1;
3692 return;
3693 }
Jeff Kingded0abc2011-02-19 03:04:56 -05003694
Thomas Guyot-Sionnestff0c7fa2020-09-24 03:41:41 -04003695 /* saves some reads if true, not a guarantee of diff outcome */
3696 may_differ = !(one->oid_valid && two->oid_valid &&
3697 oideq(&one->oid, &two->oid));
Zbigniew Jędrzejewski-Szmek352ca4e2012-05-01 19:10:15 +02003698
Nguyễn Thái Ngọc Duyb78ea5f2018-09-21 17:57:19 +02003699 if (diff_filespec_is_binary(o->repo, one) ||
3700 diff_filespec_is_binary(o->repo, two)) {
Jeff Kingded0abc2011-02-19 03:04:56 -05003701 data->is_binary = 1;
Thomas Guyot-Sionnestff0c7fa2020-09-24 03:41:41 -04003702 if (!may_differ) {
Zbigniew Jędrzejewski-Szmeke18872b2012-05-01 19:10:14 +02003703 data->added = 0;
3704 data->deleted = 0;
3705 } else {
Nguyễn Thái Ngọc Duyb78ea5f2018-09-21 17:57:19 +02003706 data->added = diff_filespec_size(o->repo, two);
3707 data->deleted = diff_filespec_size(o->repo, one);
Zbigniew Jędrzejewski-Szmeke18872b2012-05-01 19:10:14 +02003708 }
Jeff Kingded0abc2011-02-19 03:04:56 -05003709 }
3710
3711 else if (complete_rewrite) {
Jonathan Tan1c37e862020-04-07 15:11:41 -07003712 diff_populate_filespec(o->repo, one, NULL);
3713 diff_populate_filespec(o->repo, two, NULL);
Junio C Hamano710158e2006-04-25 23:40:09 -07003714 data->deleted = count_lines(one->data, one->size);
3715 data->added = count_lines(two->data, two->size);
Junio C Hamano710158e2006-04-25 23:40:09 -07003716 }
Junio C Hamano6973dca2006-04-21 23:57:45 -07003717
Thomas Guyot-Sionnestff0c7fa2020-09-24 03:41:41 -04003718 else if (may_differ) {
Junio C Hamano6973dca2006-04-21 23:57:45 -07003719 /* Crazy xdl interfaces.. */
3720 xpparam_t xpp;
3721 xdemitconf_t xecfg;
Junio C Hamano6973dca2006-04-21 23:57:45 -07003722
Nguyễn Thái Ngọc Duyb78ea5f2018-09-21 17:57:19 +02003723 if (fill_mmfile(o->repo, &mf1, one) < 0 ||
3724 fill_mmfile(o->repo, &mf2, two) < 0)
Jeff Kingded0abc2011-02-19 03:04:56 -05003725 die("unable to read files to diff");
3726
Brian Downing9ccd0a82008-10-25 15:30:37 +02003727 memset(&xpp, 0, sizeof(xpp));
Johannes Schindelin30b25012007-07-04 19:05:46 +01003728 memset(&xecfg, 0, sizeof(xecfg));
René Scharfe582aa002010-05-02 15:04:41 +02003729 xpp.flags = o->xdl_opts;
Michał Kępień296d4a92020-10-20 08:48:09 +02003730 xpp.ignore_regex = o->ignore_regex;
3731 xpp.ignore_regex_nr = o->ignore_regex_nr;
Jonathan Tan2477ab22017-11-27 11:47:47 -08003732 xpp.anchors = o->anchors;
3733 xpp.anchors_nr = o->anchors_nr;
Junio C Hamanof01cae92011-09-22 10:54:47 -07003734 xecfg.ctxlen = o->context;
3735 xecfg.interhunkctxlen = o->interhunkcontext;
Ævar Arnfjörð Bjarmason5d934602021-04-12 19:15:29 +02003736 xecfg.flags = XDL_EMIT_NO_HUNK_HDR;
3737 if (xdi_diff_outf(&mf1, &mf2, NULL,
Jeff King3b40a092018-11-02 02:36:06 -04003738 diffstat_consume, diffstat, &xpp, &xecfg))
Jeff King3efb9882015-09-24 19:12:23 -04003739 die("unable to generate diffstat for %s", one->path);
Matthew Rogers1cf3d5d2020-08-20 00:41:32 +00003740
3741 if (DIFF_FILE_VALID(one) && DIFF_FILE_VALID(two)) {
3742 struct diffstat_file *file =
3743 diffstat->files[diffstat->nr - 1];
3744 /*
3745 * Omit diffstats of modified files where nothing changed.
Thomas Guyot-Sionnestff0c7fa2020-09-24 03:41:41 -04003746 * Even if may_differ, this might be the case due to
Matthew Rogers1cf3d5d2020-08-20 00:41:32 +00003747 * ignoring whitespace changes, etc.
3748 *
3749 * But note that we special-case additions, deletions,
3750 * renames, and mode changes as adding an empty file,
3751 * for example is still of interest.
3752 */
3753 if ((p->status == DIFF_STATUS_MODIFIED)
3754 && !file->added
3755 && !file->deleted
3756 && one->mode == two->mode) {
3757 free_diffstat_file(file);
3758 diffstat->nr--;
3759 }
3760 }
Junio C Hamano6973dca2006-04-21 23:57:45 -07003761 }
Junio C Hamanofc3abdf2007-05-03 13:05:48 -07003762
Junio C Hamanofc3abdf2007-05-03 13:05:48 -07003763 diff_free_filespec_data(one);
3764 diff_free_filespec_data(two);
Junio C Hamano6973dca2006-04-21 23:57:45 -07003765}
3766
Johannes Schindelin88246892006-05-20 23:43:13 +02003767static void builtin_checkdiff(const char *name_a, const char *name_b,
Junio C Hamanocd676a52008-02-12 14:26:02 -08003768 const char *attr_path,
Junio C Hamano5ff10dd2008-06-26 15:34:54 -07003769 struct diff_filespec *one,
3770 struct diff_filespec *two,
3771 struct diff_options *o)
Johannes Schindelin88246892006-05-20 23:43:13 +02003772{
3773 mmfile_t mf1, mf2;
3774 struct checkdiff_t data;
3775
3776 if (!two)
3777 return;
3778
3779 memset(&data, 0, sizeof(data));
Johannes Schindelin88246892006-05-20 23:43:13 +02003780 data.filename = name_b ? name_b : name_a;
3781 data.lineno = 0;
Junio C Hamano1ba111d2008-06-26 15:36:34 -07003782 data.o = o;
Nguyễn Thái Ngọc Duy26d024e2018-09-21 17:57:37 +02003783 data.ws_rule = whitespace_rule(o->repo->index, attr_path);
Nguyễn Thái Ngọc Duy32eaa462018-09-21 17:57:27 +02003784 data.conflict_marker_size = ll_merge_marker_size(o->repo->index, attr_path);
Johannes Schindelin88246892006-05-20 23:43:13 +02003785
Nguyễn Thái Ngọc Duyb78ea5f2018-09-21 17:57:19 +02003786 if (fill_mmfile(o->repo, &mf1, one) < 0 ||
3787 fill_mmfile(o->repo, &mf2, two) < 0)
Johannes Schindelin88246892006-05-20 23:43:13 +02003788 die("unable to read files to diff");
3789
Junio C Hamano5ff10dd2008-06-26 15:34:54 -07003790 /*
3791 * All the other codepaths check both sides, but not checking
3792 * the "old" side here is deliberate. We are checking the newly
3793 * introduced changes, and as long as the "new" side is text, we
3794 * can and should check what it introduces.
3795 */
Nguyễn Thái Ngọc Duyb78ea5f2018-09-21 17:57:19 +02003796 if (diff_filespec_is_binary(o->repo, two))
Junio C Hamanofc3abdf2007-05-03 13:05:48 -07003797 goto free_and_return;
Johannes Schindelin88246892006-05-20 23:43:13 +02003798 else {
3799 /* Crazy xdl interfaces.. */
3800 xpparam_t xpp;
3801 xdemitconf_t xecfg;
Johannes Schindelin88246892006-05-20 23:43:13 +02003802
Brian Downing9ccd0a82008-10-25 15:30:37 +02003803 memset(&xpp, 0, sizeof(xpp));
Johannes Schindelin30b25012007-07-04 19:05:46 +01003804 memset(&xecfg, 0, sizeof(xecfg));
Junio C Hamanoc35539e2008-08-20 11:47:55 -07003805 xecfg.ctxlen = 1; /* at least one context line */
René Scharfe582aa002010-05-02 15:04:41 +02003806 xpp.flags = 0;
Jeff King75ab7632018-11-02 02:39:03 -04003807 if (xdi_diff_outf(&mf1, &mf2, checkdiff_consume_hunk,
3808 checkdiff_consume, &data,
Jeff King3efb9882015-09-24 19:12:23 -04003809 &xpp, &xecfg))
3810 die("unable to generate checkdiff for %s", one->path);
Junio C Hamano877f23c2008-06-26 15:36:59 -07003811
Junio C Hamano467babf2009-09-03 23:39:43 -07003812 if (data.ws_rule & WS_BLANK_AT_EOF) {
Junio C Hamanod68fe262009-09-14 22:05:57 -07003813 struct emit_callback ecbdata;
3814 int blank_at_eof;
3815
3816 ecbdata.ws_rule = data.ws_rule;
3817 check_blank_at_eof(&mf1, &mf2, &ecbdata);
Christoph Mallon8837d332010-10-10 19:24:06 +02003818 blank_at_eof = ecbdata.blank_at_eof_in_postimage;
Junio C Hamanod68fe262009-09-14 22:05:57 -07003819
Junio C Hamano467babf2009-09-03 23:39:43 -07003820 if (blank_at_eof) {
3821 static char *err;
3822 if (!err)
3823 err = whitespace_error_string(WS_BLANK_AT_EOF);
3824 fprintf(o->file, "%s:%d: %s.\n",
3825 data.filename, blank_at_eof, err);
3826 data.status = 1; /* report errors */
3827 }
Junio C Hamano877f23c2008-06-26 15:36:59 -07003828 }
Johannes Schindelin88246892006-05-20 23:43:13 +02003829 }
Junio C Hamanofc3abdf2007-05-03 13:05:48 -07003830 free_and_return:
3831 diff_free_filespec_data(one);
3832 diff_free_filespec_data(two);
Wincent Colaiuta62c64892007-12-13 21:24:52 +01003833 if (data.status)
Brandon Williams0d1e0e72017-10-31 11:19:11 -07003834 o->flags.check_failed = 1;
Johannes Schindelin88246892006-05-20 23:43:13 +02003835}
3836
Junio C Hamano6973dca2006-04-21 23:57:45 -07003837struct diff_filespec *alloc_filespec(const char *path)
3838{
Jeff King96ffc062016-02-22 17:44:32 -05003839 struct diff_filespec *spec;
Junio C Hamano6973dca2006-04-21 23:57:45 -07003840
Jeff King96ffc062016-02-22 17:44:32 -05003841 FLEXPTR_ALLOC_STR(spec, path, path);
Linus Torvalds9fb88412007-10-25 11:19:10 -07003842 spec->count = 1;
Jeff King122aa6f2008-10-05 17:43:36 -04003843 spec->is_binary = -1;
Junio C Hamano6973dca2006-04-21 23:57:45 -07003844 return spec;
3845}
3846
Linus Torvalds9fb88412007-10-25 11:19:10 -07003847void free_filespec(struct diff_filespec *spec)
3848{
3849 if (!--spec->count) {
3850 diff_free_filespec_data(spec);
3851 free(spec);
3852 }
3853}
3854
Brandon Williamsf9704c22017-05-30 10:30:50 -07003855void fill_filespec(struct diff_filespec *spec, const struct object_id *oid,
3856 int oid_valid, unsigned short mode)
Junio C Hamano6973dca2006-04-21 23:57:45 -07003857{
3858 if (mode) {
3859 spec->mode = canon_mode(mode);
Brandon Williamsf9704c22017-05-30 10:30:50 -07003860 oidcpy(&spec->oid, oid);
3861 spec->oid_valid = oid_valid;
Junio C Hamano6973dca2006-04-21 23:57:45 -07003862 }
3863}
3864
3865/*
Jakub Narebski5adf3172007-05-26 00:37:40 +02003866 * Given a name and sha1 pair, if the index tells us the file in
Junio C Hamano6973dca2006-04-21 23:57:45 -07003867 * the work tree has that object contents, return true, so that
3868 * prepare_temp_file() does not have to inflate and extract.
3869 */
Nguyễn Thái Ngọc Duyb78ea5f2018-09-21 17:57:19 +02003870static int reuse_worktree_file(struct index_state *istate,
3871 const char *name,
3872 const struct object_id *oid,
3873 int want_file)
Junio C Hamano6973dca2006-04-21 23:57:45 -07003874{
Nguyễn Thái Ngọc Duy9c5e6c82013-07-09 22:29:00 +07003875 const struct cache_entry *ce;
Junio C Hamano6973dca2006-04-21 23:57:45 -07003876 struct stat st;
3877 int pos, len;
3878
Junio C Hamano150115a2009-03-22 15:26:07 -07003879 /*
3880 * We do not read the cache ourselves here, because the
Junio C Hamano6973dca2006-04-21 23:57:45 -07003881 * benchmark with my previous version that always reads cache
3882 * shows that it makes things worse for diff-tree comparing
3883 * two linux-2.6 kernel trees in an already checked out work
3884 * tree. This is because most diff-tree comparisons deal with
3885 * only a small number of files, while reading the cache is
3886 * expensive for a large project, and its cost outweighs the
3887 * savings we get by not inflating the object to a temporary
3888 * file. Practically, this code only helps when we are used
3889 * by diff-cache --cached, which does read the cache before
3890 * calling us.
3891 */
Nguyễn Thái Ngọc Duyb78ea5f2018-09-21 17:57:19 +02003892 if (!istate->cache)
Junio C Hamano6973dca2006-04-21 23:57:45 -07003893 return 0;
3894
Shawn O. Pearce1510fea2006-12-14 06:15:57 -05003895 /* We want to avoid the working directory if our caller
3896 * doesn't need the data in a normal file, this system
3897 * is rather slow with its stat/open/mmap/close syscalls,
3898 * and the object is contained in a pack file. The pack
3899 * is probably already open and will be faster to obtain
3900 * the data through than the working directory. Loose
3901 * objects however would tend to be slower as they need
3902 * to be individually opened and inflated.
3903 */
brian m. carlson14c3c802018-05-02 00:25:33 +00003904 if (!FAST_WORKING_DIRECTORY && !want_file && has_object_pack(oid))
Shawn O. Pearce1510fea2006-12-14 06:15:57 -05003905 return 0;
3906
Jeff King06dec432016-07-22 11:27:53 -04003907 /*
3908 * Similarly, if we'd have to convert the file contents anyway, that
3909 * makes the optimization not worthwhile.
3910 */
Nguyễn Thái Ngọc Duyb78ea5f2018-09-21 17:57:19 +02003911 if (!want_file && would_convert_to_git(istate, name))
Jeff King06dec432016-07-22 11:27:53 -04003912 return 0;
3913
Derrick Stoleead90da72021-09-08 11:23:56 +00003914 /*
3915 * If this path does not match our sparse-checkout definition,
3916 * then the file will not be in the working directory.
3917 */
3918 if (!path_in_sparse_checkout(name, istate))
3919 return 0;
3920
Junio C Hamano6973dca2006-04-21 23:57:45 -07003921 len = strlen(name);
Nguyễn Thái Ngọc Duyb78ea5f2018-09-21 17:57:19 +02003922 pos = index_name_pos(istate, name, len);
Junio C Hamano6973dca2006-04-21 23:57:45 -07003923 if (pos < 0)
3924 return 0;
Nguyễn Thái Ngọc Duyb78ea5f2018-09-21 17:57:19 +02003925 ce = istate->cache[pos];
Junio C Hamanoeadb5832008-01-18 23:45:24 -08003926
3927 /*
3928 * This is not the sha1 we are looking for, or
3929 * unreusable because it is not a regular file.
Junio C Hamano6973dca2006-04-21 23:57:45 -07003930 */
Jeff King9001dc22018-08-28 17:22:48 -04003931 if (!oideq(oid, &ce->oid) || !S_ISREG(ce->ce_mode))
Junio C Hamanoeadb5832008-01-18 23:45:24 -08003932 return 0;
3933
3934 /*
Junio C Hamano150115a2009-03-22 15:26:07 -07003935 * If ce is marked as "assume unchanged", there is no
3936 * guarantee that work tree matches what we are looking for.
3937 */
Nguyễn Thái Ngọc Duyb4d16902009-08-20 20:46:58 +07003938 if ((ce->ce_flags & CE_VALID) || ce_skip_worktree(ce))
Junio C Hamano150115a2009-03-22 15:26:07 -07003939 return 0;
3940
3941 /*
Junio C Hamanoeadb5832008-01-18 23:45:24 -08003942 * If ce matches the file in the work tree, we can reuse it.
3943 */
3944 if (ce_uptodate(ce) ||
Nguyễn Thái Ngọc Duyb78ea5f2018-09-21 17:57:19 +02003945 (!lstat(name, &st) && !ie_match_stat(istate, ce, &st, 0)))
Junio C Hamanoeadb5832008-01-18 23:45:24 -08003946 return 1;
3947
3948 return 0;
Junio C Hamano6973dca2006-04-21 23:57:45 -07003949}
3950
Linus Torvalds04786752007-04-15 11:14:28 -07003951static int diff_populate_gitlink(struct diff_filespec *s, int size_only)
3952{
Jeff Kingb1ddfb92016-02-22 17:45:08 -05003953 struct strbuf buf = STRBUF_INIT;
3954 char *dirty = "";
Junio C Hamano8e08b412010-01-16 18:42:53 +01003955
3956 /* Are we looking at the work tree? */
Jens Lehmann85adbf22010-03-12 22:23:52 +01003957 if (s->dirty_submodule)
Junio C Hamano8e08b412010-01-16 18:42:53 +01003958 dirty = "-dirty";
3959
brian m. carlsona0d12c42016-06-24 23:09:23 +00003960 strbuf_addf(&buf, "Subproject commit %s%s\n",
3961 oid_to_hex(&s->oid), dirty);
Jeff Kingb1ddfb92016-02-22 17:45:08 -05003962 s->size = buf.len;
Linus Torvalds04786752007-04-15 11:14:28 -07003963 if (size_only) {
3964 s->data = NULL;
Jeff Kingb1ddfb92016-02-22 17:45:08 -05003965 strbuf_release(&buf);
3966 } else {
3967 s->data = strbuf_detach(&buf, NULL);
3968 s->should_free = 1;
Linus Torvalds04786752007-04-15 11:14:28 -07003969 }
3970 return 0;
3971}
3972
Junio C Hamano6973dca2006-04-21 23:57:45 -07003973/*
3974 * While doing rename detection and pickaxe operation, we may need to
3975 * grab the data for the blob (or file) for our own in-core comparison.
3976 * diff_filespec has data and size fields for this purpose.
3977 */
Nguyễn Thái Ngọc Duyb78ea5f2018-09-21 17:57:19 +02003978int diff_populate_filespec(struct repository *r,
3979 struct diff_filespec *s,
Jonathan Tan1c37e862020-04-07 15:11:41 -07003980 const struct diff_populate_filespec_options *options)
Junio C Hamano6973dca2006-04-21 23:57:45 -07003981{
Jonathan Tan1c37e862020-04-07 15:11:41 -07003982 int size_only = options ? options->check_size_only : 0;
3983 int check_binary = options ? options->check_binary : 0;
Junio C Hamano6973dca2006-04-21 23:57:45 -07003984 int err = 0;
Torsten Bögershausen8462ff42018-01-13 23:49:31 +01003985 int conv_flags = global_conv_flags_eol;
Junio C Hamano5430bb22013-06-24 14:35:04 -07003986 /*
3987 * demote FAIL to WARN to allow inspecting the situation
3988 * instead of refusing.
3989 */
Torsten Bögershausen8462ff42018-01-13 23:49:31 +01003990 if (conv_flags & CONV_EOL_RNDTRP_DIE)
3991 conv_flags = CONV_EOL_RNDTRP_WARN;
Junio C Hamano5430bb22013-06-24 14:35:04 -07003992
Junio C Hamano6973dca2006-04-21 23:57:45 -07003993 if (!DIFF_FILE_VALID(s))
3994 die("internal error: asking to populate invalid file.");
3995 if (S_ISDIR(s->mode))
3996 return -1;
3997
Junio C Hamano6973dca2006-04-21 23:57:45 -07003998 if (s->data)
Junio C Hamanofc3abdf2007-05-03 13:05:48 -07003999 return 0;
Linus Torvalds04786752007-04-15 11:14:28 -07004000
Junio C Hamano6e0b8ed2007-05-07 01:14:21 -07004001 if (size_only && 0 < s->size)
4002 return 0;
4003
Martin Waitz302b9282007-05-21 22:08:28 +02004004 if (S_ISGITLINK(s->mode))
Linus Torvalds04786752007-04-15 11:14:28 -07004005 return diff_populate_gitlink(s, size_only);
4006
brian m. carlson41c95602016-06-24 23:09:24 +00004007 if (!s->oid_valid ||
Nguyễn Thái Ngọc Duyb78ea5f2018-09-21 17:57:19 +02004008 reuse_worktree_file(r->index, s->path, &s->oid, 0)) {
Brandon Caseyf285a2d2008-10-09 14:12:12 -05004009 struct strbuf buf = STRBUF_INIT;
Junio C Hamano6973dca2006-04-21 23:57:45 -07004010 struct stat st;
4011 int fd;
Linus Torvalds6c510be2007-02-13 11:07:23 -08004012
Junio C Hamano6973dca2006-04-21 23:57:45 -07004013 if (lstat(s->path, &st) < 0) {
Andrey Okoshkin10e0ca82017-10-27 12:33:25 +03004014 err_empty:
4015 err = -1;
4016 empty:
4017 s->data = (char *)"";
4018 s->size = 0;
4019 return err;
Junio C Hamano6973dca2006-04-21 23:57:45 -07004020 }
Shawn O. Pearcedc49cd72007-03-06 20:44:37 -05004021 s->size = xsize_t(st.st_size);
Junio C Hamano6973dca2006-04-21 23:57:45 -07004022 if (!s->size)
4023 goto empty;
Junio C Hamano6973dca2006-04-21 23:57:45 -07004024 if (S_ISLNK(st.st_mode)) {
Linus Torvaldscf219d82008-12-17 10:26:13 -08004025 struct strbuf sb = STRBUF_INIT;
4026
4027 if (strbuf_readlink(&sb, s->path, s->size))
Junio C Hamano6973dca2006-04-21 23:57:45 -07004028 goto err_empty;
René Scharfe0956a6d2008-12-18 17:56:51 +01004029 s->size = sb.len;
4030 s->data = strbuf_detach(&sb, NULL);
Linus Torvaldscf219d82008-12-17 10:26:13 -08004031 s->should_free = 1;
Junio C Hamano6973dca2006-04-21 23:57:45 -07004032 return 0;
4033 }
Junio C Hamano12426e12017-03-01 18:04:44 +01004034
4035 /*
4036 * Even if the caller would be happy with getting
4037 * only the size, we cannot return early at this
4038 * point if the path requires us to run the content
4039 * conversion.
4040 */
Nguyễn Thái Ngọc Duyb78ea5f2018-09-21 17:57:19 +02004041 if (size_only && !would_convert_to_git(r->index, s->path))
Linus Torvaldscf219d82008-12-17 10:26:13 -08004042 return 0;
Junio C Hamano12426e12017-03-01 18:04:44 +01004043
4044 /*
4045 * Note: this check uses xsize_t(st.st_size) that may
4046 * not be the true size of the blob after it goes
4047 * through convert_to_git(). This may not strictly be
4048 * correct, but the whole point of big_file_threshold
4049 * and is_binary check being that we want to avoid
4050 * opening the file and inspecting the contents, this
4051 * is probably fine.
4052 */
Jonathan Tan1c37e862020-04-07 15:11:41 -07004053 if (check_binary &&
Nguyễn Thái Ngọc Duy6bf3b812014-08-16 10:08:05 +07004054 s->size > big_file_threshold && s->is_binary == -1) {
4055 s->is_binary = 1;
4056 return 0;
4057 }
Junio C Hamano6973dca2006-04-21 23:57:45 -07004058 fd = open(s->path, O_RDONLY);
4059 if (fd < 0)
4060 goto err_empty;
Shawn O. Pearcec4712e42006-12-24 00:47:23 -05004061 s->data = xmmap(NULL, s->size, PROT_READ, MAP_PRIVATE, fd, 0);
Junio C Hamano6973dca2006-04-21 23:57:45 -07004062 close(fd);
Junio C Hamano6973dca2006-04-21 23:57:45 -07004063 s->should_munmap = 1;
Linus Torvalds6c510be2007-02-13 11:07:23 -08004064
4065 /*
4066 * Convert from working tree format to canonical git format
4067 */
Nguyễn Thái Ngọc Duyb78ea5f2018-09-21 17:57:19 +02004068 if (convert_to_git(r->index, s->path, s->data, s->size, &buf, conv_flags)) {
René Scharfec32f7492007-10-21 11:23:49 +02004069 size_t size = 0;
Linus Torvalds6c510be2007-02-13 11:07:23 -08004070 munmap(s->data, s->size);
4071 s->should_munmap = 0;
René Scharfec32f7492007-10-21 11:23:49 +02004072 s->data = strbuf_detach(&buf, &size);
4073 s->size = size;
Linus Torvalds6c510be2007-02-13 11:07:23 -08004074 s->should_free = 1;
4075 }
Junio C Hamano6973dca2006-04-21 23:57:45 -07004076 }
4077 else {
Jonathan Tanc14b6f82020-04-07 15:11:42 -07004078 struct object_info info = {
4079 .sizep = &s->size
4080 };
4081
4082 if (!(size_only || check_binary))
4083 /*
4084 * Set contentp, since there is no chance that merely
4085 * the size is sufficient.
4086 */
4087 info.contentp = &s->data;
4088
Jonathan Tan95acf112020-04-07 15:11:43 -07004089 if (options && options->missing_object_cb) {
4090 if (!oid_object_info_extended(r, &s->oid, &info,
4091 OBJECT_INFO_LOOKUP_REPLACE |
4092 OBJECT_INFO_SKIP_FETCH_OBJECT))
4093 goto object_read;
4094 options->missing_object_cb(options->missing_object_data);
4095 }
Jonathan Tanc14b6f82020-04-07 15:11:42 -07004096 if (oid_object_info_extended(r, &s->oid, &info,
4097 OBJECT_INFO_LOOKUP_REPLACE))
4098 die("unable to read %s", oid_to_hex(&s->oid));
4099
Jonathan Tan95acf112020-04-07 15:11:43 -07004100object_read:
Jonathan Tan1c37e862020-04-07 15:11:41 -07004101 if (size_only || check_binary) {
Nguyễn Thái Ngọc Duy6bf3b812014-08-16 10:08:05 +07004102 if (size_only)
4103 return 0;
4104 if (s->size > big_file_threshold && s->is_binary == -1) {
4105 s->is_binary = 1;
4106 return 0;
4107 }
Junio C Hamano6973dca2006-04-21 23:57:45 -07004108 }
Jonathan Tanc14b6f82020-04-07 15:11:42 -07004109 if (!info.contentp) {
4110 info.contentp = &s->data;
4111 if (oid_object_info_extended(r, &s->oid, &info,
4112 OBJECT_INFO_LOOKUP_REPLACE))
4113 die("unable to read %s", oid_to_hex(&s->oid));
4114 }
Nguyễn Thái Ngọc Duy6bf3b812014-08-16 10:08:05 +07004115 s->should_free = 1;
Junio C Hamano6973dca2006-04-21 23:57:45 -07004116 }
4117 return 0;
4118}
4119
Junio C Hamano8ae92e62007-10-02 21:01:03 -07004120void diff_free_filespec_blob(struct diff_filespec *s)
Junio C Hamano6973dca2006-04-21 23:57:45 -07004121{
4122 if (s->should_free)
4123 free(s->data);
4124 else if (s->should_munmap)
4125 munmap(s->data, s->size);
Junio C Hamanofc3abdf2007-05-03 13:05:48 -07004126
4127 if (s->should_free || s->should_munmap) {
4128 s->should_free = s->should_munmap = 0;
4129 s->data = NULL;
4130 }
Jeff Kingeede7b72007-09-25 15:29:42 -04004131}
4132
4133void diff_free_filespec_data(struct diff_filespec *s)
4134{
Jinoh Kang24695932020-11-06 17:14:52 +00004135 if (!s)
4136 return;
4137
Junio C Hamano8ae92e62007-10-02 21:01:03 -07004138 diff_free_filespec_blob(s);
Ævar Arnfjörð Bjarmason6a83d902017-06-15 23:15:46 +00004139 FREE_AND_NULL(s->cnt_data);
Junio C Hamano6973dca2006-04-21 23:57:45 -07004140}
4141
Nguyễn Thái Ngọc Duyb78ea5f2018-09-21 17:57:19 +02004142static void prep_temp_blob(struct index_state *istate,
4143 const char *path, struct diff_tempfile *temp,
Junio C Hamano6973dca2006-04-21 23:57:45 -07004144 void *blob,
4145 unsigned long size,
brian m. carlson09bdff22016-06-24 23:09:29 +00004146 const struct object_id *oid,
Junio C Hamano6973dca2006-04-21 23:57:45 -07004147 int mode)
4148{
Johannes Schindelin4e218f52009-03-21 12:42:52 +01004149 struct strbuf buf = STRBUF_INIT;
David Aguilar003b33a2009-05-31 01:35:52 -07004150 char *path_dup = xstrdup(path);
4151 const char *base = basename(path_dup);
brian m. carlsonc397aac2020-03-16 18:05:03 +00004152 struct checkout_metadata meta;
4153
4154 init_checkout_metadata(&meta, NULL, NULL, oid);
Junio C Hamano6973dca2006-04-21 23:57:45 -07004155
René Scharfe8af75932022-04-20 22:30:10 +02004156 temp->tempfile = mks_tempfile_dt("git-blob-XXXXXX", base);
Jeff King076aa2c2017-09-05 08:15:08 -04004157 if (!temp->tempfile)
Thomas Rastd824cbb2009-06-27 17:58:46 +02004158 die_errno("unable to create temp-file");
Nguyễn Thái Ngọc Duyb78ea5f2018-09-21 17:57:19 +02004159 if (convert_to_working_tree(istate, path,
brian m. carlsonc397aac2020-03-16 18:05:03 +00004160 (const char *)blob, (size_t)size, &buf, &meta)) {
Johannes Schindelin4e218f52009-03-21 12:42:52 +01004161 blob = buf.buf;
4162 size = buf.len;
4163 }
Junio C Hamanoc50424a2017-09-25 15:24:06 +09004164 if (write_in_full(temp->tempfile->fd, blob, size) < 0 ||
Jeff King076aa2c2017-09-05 08:15:08 -04004165 close_tempfile_gently(temp->tempfile))
Thomas Rast0721c312009-06-27 17:58:47 +02004166 die_errno("unable to write temp-file");
Jeff King076aa2c2017-09-05 08:15:08 -04004167 temp->name = get_tempfile_path(temp->tempfile);
brian m. carlson09bdff22016-06-24 23:09:29 +00004168 oid_to_hex_r(temp->hex, oid);
Jeff King5096d492015-09-24 17:06:08 -04004169 xsnprintf(temp->mode, sizeof(temp->mode), "%06o", mode);
Johannes Schindelin4e218f52009-03-21 12:42:52 +01004170 strbuf_release(&buf);
David Aguilar003b33a2009-05-31 01:35:52 -07004171 free(path_dup);
Junio C Hamano6973dca2006-04-21 23:57:45 -07004172}
4173
Nguyễn Thái Ngọc Duyb78ea5f2018-09-21 17:57:19 +02004174static struct diff_tempfile *prepare_temp_file(struct repository *r,
4175 const char *name,
4176 struct diff_filespec *one)
Junio C Hamano6973dca2006-04-21 23:57:45 -07004177{
Jeff King479b0ae2009-01-22 00:59:56 -05004178 struct diff_tempfile *temp = claim_diff_tempfile();
4179
Junio C Hamano6973dca2006-04-21 23:57:45 -07004180 if (!DIFF_FILE_VALID(one)) {
4181 not_a_valid_file:
4182 /* A '-' entry produces this for file-2, and
4183 * a '+' entry produces this for file-1.
4184 */
4185 temp->name = "/dev/null";
Jeff King5096d492015-09-24 17:06:08 -04004186 xsnprintf(temp->hex, sizeof(temp->hex), ".");
4187 xsnprintf(temp->mode, sizeof(temp->mode), ".");
Jeff King479b0ae2009-01-22 00:59:56 -05004188 return temp;
4189 }
4190
Thomas Rastaba47272014-02-16 17:52:34 +01004191 if (!S_ISGITLINK(one->mode) &&
brian m. carlson41c95602016-06-24 23:09:24 +00004192 (!one->oid_valid ||
Nguyễn Thái Ngọc Duyb78ea5f2018-09-21 17:57:19 +02004193 reuse_worktree_file(r->index, name, &one->oid, 1))) {
Junio C Hamano6973dca2006-04-21 23:57:45 -07004194 struct stat st;
4195 if (lstat(name, &st) < 0) {
4196 if (errno == ENOENT)
4197 goto not_a_valid_file;
Thomas Rastd824cbb2009-06-27 17:58:46 +02004198 die_errno("stat(%s)", name);
Junio C Hamano6973dca2006-04-21 23:57:45 -07004199 }
4200 if (S_ISLNK(st.st_mode)) {
Jeff King3cd73882009-05-25 06:46:09 -04004201 struct strbuf sb = STRBUF_INIT;
4202 if (strbuf_readlink(&sb, name, st.st_size) < 0)
Thomas Rast0721c312009-06-27 17:58:47 +02004203 die_errno("readlink(%s)", name);
Nguyễn Thái Ngọc Duyb78ea5f2018-09-21 17:57:19 +02004204 prep_temp_blob(r->index, name, temp, sb.buf, sb.len,
brian m. carlson41c95602016-06-24 23:09:24 +00004205 (one->oid_valid ?
brian m. carlson14228442021-04-26 01:02:56 +00004206 &one->oid : null_oid()),
brian m. carlson41c95602016-06-24 23:09:24 +00004207 (one->oid_valid ?
Junio C Hamano6973dca2006-04-21 23:57:45 -07004208 one->mode : S_IFLNK));
Jeff King3cd73882009-05-25 06:46:09 -04004209 strbuf_release(&sb);
Junio C Hamano6973dca2006-04-21 23:57:45 -07004210 }
4211 else {
4212 /* we can borrow from the file in the work tree */
4213 temp->name = name;
brian m. carlson41c95602016-06-24 23:09:24 +00004214 if (!one->oid_valid)
brian m. carlson14228442021-04-26 01:02:56 +00004215 oid_to_hex_r(temp->hex, null_oid());
Junio C Hamano6973dca2006-04-21 23:57:45 -07004216 else
René Scharfe24905742017-01-28 23:03:03 +01004217 oid_to_hex_r(temp->hex, &one->oid);
Junio C Hamano6973dca2006-04-21 23:57:45 -07004218 /* Even though we may sometimes borrow the
4219 * contents from the work tree, we always want
4220 * one->mode. mode is trustworthy even when
Brandon Williams74014152017-05-30 10:30:52 -07004221 * !(one->oid_valid), as long as
Junio C Hamano6973dca2006-04-21 23:57:45 -07004222 * DIFF_FILE_VALID(one).
4223 */
Jeff King5096d492015-09-24 17:06:08 -04004224 xsnprintf(temp->mode, sizeof(temp->mode), "%06o", one->mode);
Junio C Hamano6973dca2006-04-21 23:57:45 -07004225 }
Jeff King479b0ae2009-01-22 00:59:56 -05004226 return temp;
Junio C Hamano6973dca2006-04-21 23:57:45 -07004227 }
4228 else {
Jonathan Tan1c37e862020-04-07 15:11:41 -07004229 if (diff_populate_filespec(r, one, NULL))
Junio C Hamano6973dca2006-04-21 23:57:45 -07004230 die("cannot read data blob for %s", one->path);
Nguyễn Thái Ngọc Duyb78ea5f2018-09-21 17:57:19 +02004231 prep_temp_blob(r->index, name, temp,
4232 one->data, one->size,
brian m. carlson09bdff22016-06-24 23:09:29 +00004233 &one->oid, one->mode);
Junio C Hamano6973dca2006-04-21 23:57:45 -07004234 }
Jeff King479b0ae2009-01-22 00:59:56 -05004235 return temp;
Junio C Hamano6973dca2006-04-21 23:57:45 -07004236}
4237
Nguyễn Thái Ngọc Duyb78ea5f2018-09-21 17:57:19 +02004238static void add_external_diff_name(struct repository *r,
Jeff Kingef8d7ac2020-07-28 16:24:53 -04004239 struct strvec *argv,
Jeff Kingf3efe782014-04-19 15:22:25 -04004240 const char *name,
4241 struct diff_filespec *df)
4242{
Nguyễn Thái Ngọc Duyb78ea5f2018-09-21 17:57:19 +02004243 struct diff_tempfile *temp = prepare_temp_file(r, name, df);
Jeff Kingef8d7ac2020-07-28 16:24:53 -04004244 strvec_push(argv, temp->name);
4245 strvec_push(argv, temp->hex);
4246 strvec_push(argv, temp->mode);
Jeff Kingf3efe782014-04-19 15:22:25 -04004247}
4248
Junio C Hamano6973dca2006-04-21 23:57:45 -07004249/* An external diff command takes:
4250 *
4251 * diff-cmd name infile1 infile1-sha1 infile1-mode \
4252 * infile2 infile2-sha1 infile2-mode [ rename-to ]
4253 *
4254 */
4255static void run_external_diff(const char *pgm,
4256 const char *name,
4257 const char *other,
4258 struct diff_filespec *one,
4259 struct diff_filespec *two,
4260 const char *xfrm_msg,
Zoltan Klingeree7fb0b2013-12-06 10:38:46 +11004261 struct diff_options *o)
Junio C Hamano6973dca2006-04-21 23:57:45 -07004262{
Jeff Kingef8d7ac2020-07-28 16:24:53 -04004263 struct strvec argv = STRVEC_INIT;
4264 struct strvec env = STRVEC_INIT;
Zoltan Klingeree7fb0b2013-12-06 10:38:46 +11004265 struct diff_queue_struct *q = &diff_queued_diff;
Junio C Hamano6973dca2006-04-21 23:57:45 -07004266
Jeff Kingef8d7ac2020-07-28 16:24:53 -04004267 strvec_push(&argv, pgm);
4268 strvec_push(&argv, name);
Junio C Hamano6973dca2006-04-21 23:57:45 -07004269
Junio C Hamano6973dca2006-04-21 23:57:45 -07004270 if (one && two) {
Nguyễn Thái Ngọc Duyb78ea5f2018-09-21 17:57:19 +02004271 add_external_diff_name(o->repo, &argv, name, one);
Jeff Kingf3efe782014-04-19 15:22:25 -04004272 if (!other)
Nguyễn Thái Ngọc Duyb78ea5f2018-09-21 17:57:19 +02004273 add_external_diff_name(o->repo, &argv, name, two);
Jeff Kingf3efe782014-04-19 15:22:25 -04004274 else {
Nguyễn Thái Ngọc Duyb78ea5f2018-09-21 17:57:19 +02004275 add_external_diff_name(o->repo, &argv, other, two);
Jeff Kingef8d7ac2020-07-28 16:24:53 -04004276 strvec_push(&argv, other);
4277 strvec_push(&argv, xfrm_msg);
Junio C Hamano6973dca2006-04-21 23:57:45 -07004278 }
Junio C Hamano6973dca2006-04-21 23:57:45 -07004279 }
Zoltan Klingeree7fb0b2013-12-06 10:38:46 +11004280
Jeff Kingef8d7ac2020-07-28 16:24:53 -04004281 strvec_pushf(&env, "GIT_DIFF_PATH_COUNTER=%d", ++o->diff_path_counter);
4282 strvec_pushf(&env, "GIT_DIFF_PATH_TOTAL=%d", q->nr);
Zoltan Klingeree7fb0b2013-12-06 10:38:46 +11004283
Johannes Schindelin3aef54e2019-07-11 01:23:41 -07004284 diff_free_filespec_data(one);
4285 diff_free_filespec_data(two);
Jeff Kingd70a9eb2020-07-28 20:37:20 -04004286 if (run_command_v_opt_cd_env(argv.v, RUN_USING_SHELL, NULL, env.v))
Jeff King89294d12014-04-19 15:19:19 -04004287 die(_("external diff died, stopping at %s"), name);
4288
Junio C Hamano6973dca2006-04-21 23:57:45 -07004289 remove_tempfile();
Jeff Kingef8d7ac2020-07-28 16:24:53 -04004290 strvec_clear(&argv);
4291 strvec_clear(&env);
Junio C Hamano6973dca2006-04-21 23:57:45 -07004292}
4293
Junio C Hamanob67b9612009-01-26 00:08:24 -08004294static int similarity_index(struct diff_filepair *p)
4295{
4296 return p->score * 100 / MAX_SCORE;
4297}
4298
Jeff King4f036662016-10-20 02:21:25 -04004299static const char *diff_abbrev_oid(const struct object_id *oid, int abbrev)
4300{
4301 if (startup_info->have_repository)
brian m. carlsonaab95832018-03-12 02:27:30 +00004302 return find_unique_abbrev(oid, abbrev);
Jeff King4f036662016-10-20 02:21:25 -04004303 else {
4304 char *hex = oid_to_hex(oid);
Junio C Hamano0d9c5272016-10-27 14:58:48 -07004305 if (abbrev < 0)
4306 abbrev = FALLBACK_DEFAULT_ABBREV;
brian m. carlson02afca12018-07-16 01:28:05 +00004307 if (abbrev > the_hash_algo->hexsz)
Johannes Schindelin033abf92018-05-02 11:38:39 +02004308 BUG("oid abbreviation out of range: %d", abbrev);
Jack Bates43d19482016-12-06 09:56:14 -07004309 if (abbrev)
4310 hex[abbrev] = '\0';
Jeff King4f036662016-10-20 02:21:25 -04004311 return hex;
4312 }
4313}
4314
Junio C Hamanob67b9612009-01-26 00:08:24 -08004315static void fill_metainfo(struct strbuf *msg,
4316 const char *name,
4317 const char *other,
4318 struct diff_filespec *one,
4319 struct diff_filespec *two,
4320 struct diff_options *o,
Bert Wesarg37466442010-05-04 00:38:07 +02004321 struct diff_filepair *p,
Junio C Hamano59777442010-06-18 11:16:57 -07004322 int *must_show_header,
Bert Wesarg37466442010-05-04 00:38:07 +02004323 int use_color)
Junio C Hamanob67b9612009-01-26 00:08:24 -08004324{
Bert Wesarg37466442010-05-04 00:38:07 +02004325 const char *set = diff_get_color(use_color, DIFF_METAINFO);
4326 const char *reset = diff_get_color(use_color, DIFF_RESET);
John Keeping30997bb2013-02-07 20:15:27 +00004327 const char *line_prefix = diff_line_prefix(o);
Johannes Schindelin2715e8a2022-06-18 00:20:55 +00004328 struct string_list *more_headers = NULL;
Bo Yang7be57612010-05-26 15:23:54 +08004329
Christian Couder296c6bb2010-05-26 04:50:12 +02004330 *must_show_header = 1;
Junio C Hamanob67b9612009-01-26 00:08:24 -08004331 strbuf_init(msg, PATH_MAX * 2 + 300);
4332 switch (p->status) {
4333 case DIFF_STATUS_COPIED:
Junio C Hamano98ad90f2010-06-18 11:16:57 -07004334 strbuf_addf(msg, "%s%ssimilarity index %d%%",
4335 line_prefix, set, similarity_index(p));
4336 strbuf_addf(msg, "%s\n%s%scopy from ",
4337 reset, line_prefix, set);
Junio C Hamanob67b9612009-01-26 00:08:24 -08004338 quote_c_style(name, msg, NULL, 0);
Junio C Hamano98ad90f2010-06-18 11:16:57 -07004339 strbuf_addf(msg, "%s\n%s%scopy to ", reset, line_prefix, set);
Junio C Hamanob67b9612009-01-26 00:08:24 -08004340 quote_c_style(other, msg, NULL, 0);
Bert Wesarg37466442010-05-04 00:38:07 +02004341 strbuf_addf(msg, "%s\n", reset);
Junio C Hamanob67b9612009-01-26 00:08:24 -08004342 break;
4343 case DIFF_STATUS_RENAMED:
Junio C Hamano98ad90f2010-06-18 11:16:57 -07004344 strbuf_addf(msg, "%s%ssimilarity index %d%%",
4345 line_prefix, set, similarity_index(p));
4346 strbuf_addf(msg, "%s\n%s%srename from ",
4347 reset, line_prefix, set);
Junio C Hamanob67b9612009-01-26 00:08:24 -08004348 quote_c_style(name, msg, NULL, 0);
Junio C Hamano98ad90f2010-06-18 11:16:57 -07004349 strbuf_addf(msg, "%s\n%s%srename to ",
4350 reset, line_prefix, set);
Junio C Hamanob67b9612009-01-26 00:08:24 -08004351 quote_c_style(other, msg, NULL, 0);
Bert Wesarg37466442010-05-04 00:38:07 +02004352 strbuf_addf(msg, "%s\n", reset);
Junio C Hamanob67b9612009-01-26 00:08:24 -08004353 break;
4354 case DIFF_STATUS_MODIFIED:
4355 if (p->score) {
Junio C Hamano98ad90f2010-06-18 11:16:57 -07004356 strbuf_addf(msg, "%s%sdissimilarity index %d%%%s\n",
4357 line_prefix,
Bert Wesarg37466442010-05-04 00:38:07 +02004358 set, similarity_index(p), reset);
Junio C Hamanob67b9612009-01-26 00:08:24 -08004359 break;
4360 }
4361 /* fallthru */
4362 default:
Christian Couder296c6bb2010-05-26 04:50:12 +02004363 *must_show_header = 0;
Junio C Hamanob67b9612009-01-26 00:08:24 -08004364 }
Elijah Newren95433ee2022-02-02 02:37:34 +00004365 if ((more_headers = additional_headers(o, name))) {
4366 add_formatted_headers(msg, more_headers,
4367 line_prefix, set, reset);
4368 *must_show_header = 1;
4369 }
Jeff King9001dc22018-08-28 17:22:48 -04004370 if (one && two && !oideq(&one->oid, &two->oid)) {
brian m. carlsonde1d81d2018-05-02 00:25:52 +00004371 const unsigned hexsz = the_hash_algo->hexsz;
Đoàn Trần Công Danh3046c7f2020-08-21 18:51:47 +07004372 int abbrev = o->abbrev ? o->abbrev : DEFAULT_ABBREV;
4373
4374 if (o->flags.full_index)
4375 abbrev = hexsz;
Junio C Hamanob67b9612009-01-26 00:08:24 -08004376
Brandon Williams0d1e0e72017-10-31 11:19:11 -07004377 if (o->flags.binary) {
Junio C Hamanob67b9612009-01-26 00:08:24 -08004378 mmfile_t mf;
Nguyễn Thái Ngọc Duyb78ea5f2018-09-21 17:57:19 +02004379 if ((!fill_mmfile(o->repo, &mf, one) &&
4380 diff_filespec_is_binary(o->repo, one)) ||
4381 (!fill_mmfile(o->repo, &mf, two) &&
4382 diff_filespec_is_binary(o->repo, two)))
brian m. carlsonde1d81d2018-05-02 00:25:52 +00004383 abbrev = hexsz;
Junio C Hamanob67b9612009-01-26 00:08:24 -08004384 }
Jeff King4f036662016-10-20 02:21:25 -04004385 strbuf_addf(msg, "%s%sindex %s..%s", line_prefix, set,
4386 diff_abbrev_oid(&one->oid, abbrev),
4387 diff_abbrev_oid(&two->oid, abbrev));
Junio C Hamanob67b9612009-01-26 00:08:24 -08004388 if (one->mode == two->mode)
4389 strbuf_addf(msg, " %06o", one->mode);
Bert Wesarg37466442010-05-04 00:38:07 +02004390 strbuf_addf(msg, "%s\n", reset);
Junio C Hamanob67b9612009-01-26 00:08:24 -08004391 }
Junio C Hamanob67b9612009-01-26 00:08:24 -08004392}
4393
Junio C Hamano6973dca2006-04-21 23:57:45 -07004394static void run_diff_cmd(const char *pgm,
4395 const char *name,
4396 const char *other,
Junio C Hamanocd676a52008-02-12 14:26:02 -08004397 const char *attr_path,
Junio C Hamano6973dca2006-04-21 23:57:45 -07004398 struct diff_filespec *one,
4399 struct diff_filespec *two,
Junio C Hamanob67b9612009-01-26 00:08:24 -08004400 struct strbuf *msg,
Junio C Hamano051308f2006-05-04 16:51:44 -07004401 struct diff_options *o,
Junio C Hamanob67b9612009-01-26 00:08:24 -08004402 struct diff_filepair *p)
Junio C Hamano6973dca2006-04-21 23:57:45 -07004403{
Junio C Hamanob67b9612009-01-26 00:08:24 -08004404 const char *xfrm_msg = NULL;
4405 int complete_rewrite = (p->status == DIFF_STATUS_MODIFIED) && p->score;
Christian Couder296c6bb2010-05-26 04:50:12 +02004406 int must_show_header = 0;
Junio C Hamanob67b9612009-01-26 00:08:24 -08004407
Junio C Hamanobd8c1a92012-07-17 22:08:59 -07004408
Brandon Williams0d1e0e72017-10-31 11:19:11 -07004409 if (o->flags.allow_external) {
Nguyễn Thái Ngọc Duyacd00ea2018-09-21 17:57:33 +02004410 struct userdiff_driver *drv;
4411
4412 drv = userdiff_find_by_path(o->repo->index, attr_path);
Jeff Kingbe58e702008-10-05 17:43:21 -04004413 if (drv && drv->external)
4414 pgm = drv->external;
Junio C Hamanof1af60b2007-04-22 17:52:55 -07004415 }
4416
Bert Wesarg37466442010-05-04 00:38:07 +02004417 if (msg) {
4418 /*
4419 * don't use colors when the header is intended for an
4420 * external diff driver
4421 */
4422 fill_metainfo(msg, name, other, one, two, o, p,
Junio C Hamano59777442010-06-18 11:16:57 -07004423 &must_show_header,
Jeff Kingdaa0c3d2011-08-17 22:04:23 -07004424 want_color(o->use_color) && !pgm);
Bert Wesarg37466442010-05-04 00:38:07 +02004425 xfrm_msg = msg->len ? msg->buf : NULL;
4426 }
4427
Junio C Hamano6973dca2006-04-21 23:57:45 -07004428 if (pgm) {
Jeff King4bc17922019-02-14 00:49:42 -05004429 run_external_diff(pgm, name, other, one, two, xfrm_msg, o);
Junio C Hamano6973dca2006-04-21 23:57:45 -07004430 return;
4431 }
4432 if (one && two)
4433 builtin_diff(name, other ? other : name,
Christian Couder296c6bb2010-05-26 04:50:12 +02004434 one, two, xfrm_msg, must_show_header,
4435 o, complete_rewrite);
Junio C Hamano6973dca2006-04-21 23:57:45 -07004436 else
Daniel Barkalowc0c77732008-03-09 22:43:39 -04004437 fprintf(o->file, "* Unmerged path %s\n", name);
Junio C Hamano6973dca2006-04-21 23:57:45 -07004438}
4439
Nguyễn Thái Ngọc Duy58bf2a42018-09-21 17:57:31 +02004440static void diff_fill_oid_info(struct diff_filespec *one, struct index_state *istate)
Junio C Hamano6973dca2006-04-21 23:57:45 -07004441{
4442 if (DIFF_FILE_VALID(one)) {
brian m. carlson41c95602016-06-24 23:09:24 +00004443 if (!one->oid_valid) {
Junio C Hamano6973dca2006-04-21 23:57:45 -07004444 struct stat st;
Junio C Hamano4682d852012-06-27 20:14:47 -07004445 if (one->is_stdin) {
brian m. carlsona0d12c42016-06-24 23:09:23 +00004446 oidclr(&one->oid);
Johannes Schindelin5332b2a2007-02-25 23:36:10 +01004447 return;
4448 }
Junio C Hamano6973dca2006-04-21 23:57:45 -07004449 if (lstat(one->path, &st) < 0)
Thomas Rast0721c312009-06-27 17:58:47 +02004450 die_errno("stat '%s'", one->path);
Nguyễn Thái Ngọc Duy58bf2a42018-09-21 17:57:31 +02004451 if (index_path(istate, &one->oid, one->path, &st, 0))
Alexander Potashevd7530702009-01-04 21:38:41 +03004452 die("cannot hash %s", one->path);
Junio C Hamano6973dca2006-04-21 23:57:45 -07004453 }
4454 }
4455 else
brian m. carlsona0d12c42016-06-24 23:09:23 +00004456 oidclr(&one->oid);
Junio C Hamano6973dca2006-04-21 23:57:45 -07004457}
4458
Junio C Hamanocd676a52008-02-12 14:26:02 -08004459static void strip_prefix(int prefix_length, const char **namep, const char **otherp)
4460{
4461 /* Strip the prefix but do not molest /dev/null and absolute paths */
Johannes Sixtffd04e92018-10-19 18:58:07 +02004462 if (*namep && !is_absolute_path(*namep)) {
Junio C Hamanocd676a52008-02-12 14:26:02 -08004463 *namep += prefix_length;
Jakub Narebskid8faea92010-08-09 10:50:53 -04004464 if (**namep == '/')
4465 ++*namep;
4466 }
Johannes Sixtffd04e92018-10-19 18:58:07 +02004467 if (*otherp && !is_absolute_path(*otherp)) {
Junio C Hamanocd676a52008-02-12 14:26:02 -08004468 *otherp += prefix_length;
Jakub Narebskid8faea92010-08-09 10:50:53 -04004469 if (**otherp == '/')
4470 ++*otherp;
4471 }
Junio C Hamanocd676a52008-02-12 14:26:02 -08004472}
4473
Junio C Hamano6973dca2006-04-21 23:57:45 -07004474static void run_diff(struct diff_filepair *p, struct diff_options *o)
4475{
4476 const char *pgm = external_diff();
Pierre Habouzit663af342007-09-20 00:42:15 +02004477 struct strbuf msg;
Pierre Habouzit663af342007-09-20 00:42:15 +02004478 struct diff_filespec *one = p->one;
4479 struct diff_filespec *two = p->two;
Junio C Hamano6973dca2006-04-21 23:57:45 -07004480 const char *name;
4481 const char *other;
Junio C Hamanocd676a52008-02-12 14:26:02 -08004482 const char *attr_path;
Pierre Habouzit663af342007-09-20 00:42:15 +02004483
Stefan Bellerf2d2a5d2017-06-29 17:06:46 -07004484 name = one->path;
4485 other = (strcmp(name, two->path) ? two->path : NULL);
Junio C Hamanocd676a52008-02-12 14:26:02 -08004486 attr_path = name;
4487 if (o->prefix_length)
4488 strip_prefix(o->prefix_length, &name, &other);
4489
Brandon Williams0d1e0e72017-10-31 11:19:11 -07004490 if (!o->flags.allow_external)
Junio C Hamanobd8c1a92012-07-17 22:08:59 -07004491 pgm = NULL;
4492
Junio C Hamanocd676a52008-02-12 14:26:02 -08004493 if (DIFF_PAIR_UNMERGED(p)) {
4494 run_diff_cmd(pgm, name, NULL, attr_path,
Junio C Hamanob67b9612009-01-26 00:08:24 -08004495 NULL, NULL, NULL, o, p);
Junio C Hamanocd676a52008-02-12 14:26:02 -08004496 return;
4497 }
4498
Nguyễn Thái Ngọc Duy58bf2a42018-09-21 17:57:31 +02004499 diff_fill_oid_info(one, o->repo->index);
4500 diff_fill_oid_info(two, o->repo->index);
Junio C Hamano6973dca2006-04-21 23:57:45 -07004501
Junio C Hamano6973dca2006-04-21 23:57:45 -07004502 if (!pgm &&
4503 DIFF_FILE_VALID(one) && DIFF_FILE_VALID(two) &&
4504 (S_IFMT & one->mode) != (S_IFMT & two->mode)) {
Junio C Hamanob67b9612009-01-26 00:08:24 -08004505 /*
4506 * a filepair that changes between file and symlink
Junio C Hamano6973dca2006-04-21 23:57:45 -07004507 * needs to be split into deletion and creation.
4508 */
4509 struct diff_filespec *null = alloc_filespec(two->path);
Junio C Hamanocd676a52008-02-12 14:26:02 -08004510 run_diff_cmd(NULL, name, other, attr_path,
Nguyễn Thái Ngọc Duyb78ea5f2018-09-21 17:57:19 +02004511 one, null, &msg,
4512 o, p);
Junio C Hamano6973dca2006-04-21 23:57:45 -07004513 free(null);
Junio C Hamanob67b9612009-01-26 00:08:24 -08004514 strbuf_release(&msg);
4515
Junio C Hamano6973dca2006-04-21 23:57:45 -07004516 null = alloc_filespec(one->path);
Junio C Hamanocd676a52008-02-12 14:26:02 -08004517 run_diff_cmd(NULL, name, other, attr_path,
Junio C Hamanob67b9612009-01-26 00:08:24 -08004518 null, two, &msg, o, p);
Junio C Hamano6973dca2006-04-21 23:57:45 -07004519 free(null);
4520 }
4521 else
Junio C Hamanocd676a52008-02-12 14:26:02 -08004522 run_diff_cmd(pgm, name, other, attr_path,
Junio C Hamanob67b9612009-01-26 00:08:24 -08004523 one, two, &msg, o, p);
Junio C Hamano6973dca2006-04-21 23:57:45 -07004524
Pierre Habouzit663af342007-09-20 00:42:15 +02004525 strbuf_release(&msg);
Junio C Hamano6973dca2006-04-21 23:57:45 -07004526}
4527
4528static void run_diffstat(struct diff_filepair *p, struct diff_options *o,
4529 struct diffstat_t *diffstat)
4530{
4531 const char *name;
4532 const char *other;
4533
4534 if (DIFF_PAIR_UNMERGED(p)) {
4535 /* unmerged */
Nguyễn Thái Ngọc Duyb78ea5f2018-09-21 17:57:19 +02004536 builtin_diffstat(p->one->path, NULL, NULL, NULL,
4537 diffstat, o, p);
Junio C Hamano6973dca2006-04-21 23:57:45 -07004538 return;
4539 }
4540
4541 name = p->one->path;
4542 other = (strcmp(name, p->two->path) ? p->two->path : NULL);
4543
Junio C Hamanocd676a52008-02-12 14:26:02 -08004544 if (o->prefix_length)
4545 strip_prefix(o->prefix_length, &name, &other);
4546
Nguyễn Thái Ngọc Duy58bf2a42018-09-21 17:57:31 +02004547 diff_fill_oid_info(p->one, o->repo->index);
4548 diff_fill_oid_info(p->two, o->repo->index);
Junio C Hamano6973dca2006-04-21 23:57:45 -07004549
Nguyễn Thái Ngọc Duyb78ea5f2018-09-21 17:57:19 +02004550 builtin_diffstat(name, other, p->one, p->two,
4551 diffstat, o, p);
Junio C Hamano6973dca2006-04-21 23:57:45 -07004552}
4553
Johannes Schindelin88246892006-05-20 23:43:13 +02004554static void run_checkdiff(struct diff_filepair *p, struct diff_options *o)
4555{
4556 const char *name;
4557 const char *other;
Junio C Hamanocd676a52008-02-12 14:26:02 -08004558 const char *attr_path;
Johannes Schindelin88246892006-05-20 23:43:13 +02004559
4560 if (DIFF_PAIR_UNMERGED(p)) {
4561 /* unmerged */
4562 return;
4563 }
4564
4565 name = p->one->path;
4566 other = (strcmp(name, p->two->path) ? p->two->path : NULL);
Junio C Hamanocd676a52008-02-12 14:26:02 -08004567 attr_path = other ? other : name;
4568
4569 if (o->prefix_length)
4570 strip_prefix(o->prefix_length, &name, &other);
Johannes Schindelin88246892006-05-20 23:43:13 +02004571
Nguyễn Thái Ngọc Duy58bf2a42018-09-21 17:57:31 +02004572 diff_fill_oid_info(p->one, o->repo->index);
4573 diff_fill_oid_info(p->two, o->repo->index);
Johannes Schindelin88246892006-05-20 23:43:13 +02004574
Junio C Hamanocd676a52008-02-12 14:26:02 -08004575 builtin_checkdiff(name, other, attr_path, p->one, p->two, o);
Johannes Schindelin88246892006-05-20 23:43:13 +02004576}
4577
Nguyễn Thái Ngọc Duy4a288472019-01-27 07:35:31 +07004578static void prep_parse_options(struct diff_options *options);
4579
Nguyễn Thái Ngọc Duye6757652018-09-21 17:57:24 +02004580void repo_diff_setup(struct repository *r, struct diff_options *options)
Junio C Hamano6973dca2006-04-21 23:57:45 -07004581{
Johannes Schindelinbe4f2b42010-08-05 10:49:55 +02004582 memcpy(options, &default_diff_options, sizeof(*options));
Daniel Barkalowc0c77732008-03-09 22:43:39 -04004583
4584 options->file = stdout;
Nguyễn Thái Ngọc Duye6757652018-09-21 17:57:24 +02004585 options->repo = r;
Daniel Barkalowc0c77732008-03-09 22:43:39 -04004586
Stefan Beller7648b792018-08-17 13:43:52 -07004587 options->output_indicators[OUTPUT_INDICATOR_NEW] = '+';
4588 options->output_indicators[OUTPUT_INDICATOR_OLD] = '-';
4589 options->output_indicators[OUTPUT_INDICATOR_CONTEXT] = ' ';
Jack Bates43d19482016-12-06 09:56:14 -07004590 options->abbrev = DEFAULT_ABBREV;
Junio C Hamano6973dca2006-04-21 23:57:45 -07004591 options->line_termination = '\n';
4592 options->break_opt = -1;
4593 options->rename_limit = -1;
Johan Herland712d2c72011-04-29 11:36:20 +02004594 options->dirstat_permille = diff_dirstat_permille_default;
Jeff Muizelaar6468a4e2012-09-27 15:12:52 -04004595 options->context = diff_context_default;
Vegard Nossumc4888672017-01-12 13:21:11 +01004596 options->interhunkcontext = diff_interhunk_context_default;
Junio C Hamanoa17505f2016-10-04 15:26:27 -07004597 options->ws_error_highlight = ws_error_highlight_default;
Brandon Williams0d1e0e72017-10-31 11:19:11 -07004598 options->flags.rename_empty = 1;
Laurent Arnoudc28ded82020-05-22 12:46:18 +02004599 options->flags.relative_name = diff_relative;
Stefan Beller15af58c2018-01-04 14:50:42 -08004600 options->objfind = NULL;
Junio C Hamano6973dca2006-04-21 23:57:45 -07004601
Kirill Smelkov72441af2014-04-07 01:46:26 +04004602 /* pathchange left =NULL by default */
Junio C Hamano6973dca2006-04-21 23:57:45 -07004603 options->change = diff_change;
4604 options->add_remove = diff_addremove;
Jeff Kingf1c96262011-08-17 22:03:12 -07004605 options->use_color = diff_use_color_default;
Eric Wongb68ea122006-07-07 04:01:23 -07004606 options->detect_rename = diff_detect_rename_default;
Michal Privoznik07ab4de2013-01-16 08:51:57 +01004607 options->xdl_opts |= diff_algorithm;
Michael Haggerty433860f2016-09-05 11:44:51 +02004608 if (diff_indent_heuristic)
4609 DIFF_XDL_SET(options, INDENT_HEURISTIC);
Johannes Schindelineab9a402007-12-18 19:32:14 +00004610
Samuel Bronson6d8940b2013-12-18 19:08:12 -05004611 options->orderfile = diff_order_file_cfg;
4612
Sangeeta Jain8ef93122020-11-10 14:09:00 +05304613 if (!options->flags.ignore_submodule_set)
4614 options->flags.ignore_untracked_in_submodules = 1;
4615
Eli Collinsf89504d2010-05-02 19:03:41 -07004616 if (diff_no_prefix) {
4617 options->a_prefix = options->b_prefix = "";
4618 } else if (!diff_mnemonic_prefix) {
Junio C Hamanoa5a818e2008-08-18 20:08:09 -07004619 options->a_prefix = "a/";
4620 options->b_prefix = "b/";
4621 }
Stefan Beller2e2d5ac2017-06-30 13:53:07 -07004622
4623 options->color_moved = diff_color_moved_default;
Stefan Beller626c0b52018-07-18 12:31:56 -07004624 options->color_moved_ws_handling = diff_color_moved_ws_default;
Nguyễn Thái Ngọc Duy4a288472019-01-27 07:35:31 +07004625
4626 prep_parse_options(options);
Junio C Hamano6973dca2006-04-21 23:57:45 -07004627}
4628
Johannes Schindelin4d4d4ea2022-01-28 12:02:49 +00004629static const char diff_status_letters[] = {
4630 DIFF_STATUS_ADDED,
4631 DIFF_STATUS_COPIED,
4632 DIFF_STATUS_DELETED,
4633 DIFF_STATUS_MODIFIED,
4634 DIFF_STATUS_RENAMED,
4635 DIFF_STATUS_TYPE_CHANGED,
4636 DIFF_STATUS_UNKNOWN,
4637 DIFF_STATUS_UNMERGED,
4638 DIFF_STATUS_FILTER_AON,
4639 DIFF_STATUS_FILTER_BROKEN,
4640 '\0',
4641};
4642
4643static unsigned int filter_bit['Z' + 1];
4644
4645static void prepare_filter_bits(void)
4646{
4647 int i;
4648
4649 if (!filter_bit[DIFF_STATUS_ADDED]) {
4650 for (i = 0; diff_status_letters[i]; i++)
4651 filter_bit[(int) diff_status_letters[i]] = (1 << i);
4652 }
4653}
4654
4655static unsigned filter_bit_tst(char status, const struct diff_options *opt)
4656{
4657 return opt->filter & filter_bit[(int) status];
4658}
4659
4660unsigned diff_filter_bit(char status)
4661{
4662 prepare_filter_bits();
4663 return filter_bit[(int) status];
4664}
4665
Thomas Rast28452652012-08-03 14:16:24 +02004666void diff_setup_done(struct diff_options *options)
Junio C Hamano6973dca2006-04-21 23:57:45 -07004667{
Stefan Beller4d8c51a2018-01-04 14:50:44 -08004668 unsigned check_mask = DIFF_FORMAT_NAME |
4669 DIFF_FORMAT_NAME_STATUS |
4670 DIFF_FORMAT_CHECKDIFF |
4671 DIFF_FORMAT_NO_OUTPUT;
brian m. carlsonde1d81d2018-05-02 00:25:52 +00004672 /*
4673 * This must be signed because we're comparing against a potentially
4674 * negative value.
4675 */
4676 const int hexsz = the_hash_algo->hexsz;
Timo Hirvonend7de00f2006-06-24 20:26:49 +03004677
Junio C Hamano6c374002013-05-10 17:10:11 +02004678 if (options->set_default)
4679 options->set_default(options);
4680
Stefan Beller4d8c51a2018-01-04 14:50:44 -08004681 if (HAS_MULTI_BITS(options->output_format & check_mask))
Jean-Noël Avila246cac82022-01-05 20:02:24 +00004682 die(_("options '%s', '%s', '%s', and '%s' cannot be used together"),
4683 "--name-only", "--name-status", "--check", "-s");
Timo Hirvonend7de00f2006-06-24 20:26:49 +03004684
Stefan Beller5e505252018-01-04 14:50:43 -08004685 if (HAS_MULTI_BITS(options->pickaxe_opts & DIFF_PICKAXE_KINDS_MASK))
Jean-Noël Avila246cac82022-01-05 20:02:24 +00004686 die(_("options '%s', '%s', and '%s' cannot be used together"),
4687 "-G", "-S", "--find-object");
Stefan Beller5e505252018-01-04 14:50:43 -08004688
Ævar Arnfjörð Bjarmason188e9e22021-04-12 19:15:14 +02004689 if (HAS_MULTI_BITS(options->pickaxe_opts & DIFF_PICKAXE_KINDS_G_REGEX_MASK))
Jean-Noël Avila246cac82022-01-05 20:02:24 +00004690 die(_("options '%s' and '%s' cannot be used together, use '%s' with '%s'"),
4691 "-G", "--pickaxe-regex", "--pickaxe-regex", "-S");
Ævar Arnfjörð Bjarmason188e9e22021-04-12 19:15:14 +02004692
Ævar Arnfjörð Bjarmasond26ec882021-04-12 19:15:15 +02004693 if (HAS_MULTI_BITS(options->pickaxe_opts & DIFF_PICKAXE_KINDS_ALL_OBJFIND_MASK))
Jean-Noël Avila246cac82022-01-05 20:02:24 +00004694 die(_("options '%s' and '%s' cannot be used together, use '%s' with '%s' and '%s'"),
4695 "--pickaxe-all", "--find-object", "--pickaxe-all", "-G", "-S");
Ævar Arnfjörð Bjarmasond26ec882021-04-12 19:15:15 +02004696
Junio C Hamanof2451942009-05-22 12:45:29 -07004697 /*
4698 * Most of the time we can say "there are changes"
4699 * only by checking if there are changed paths, but
4700 * --ignore-whitespace* options force us to look
Jim Meyering97bf2a02009-08-30 22:27:02 +02004701 * inside contents.
Junio C Hamanof2451942009-05-22 12:45:29 -07004702 */
4703
Junio C Hamano50f04392020-12-16 17:27:13 -08004704 if ((options->xdl_opts & XDF_WHITESPACE_FLAGS) ||
4705 options->ignore_regex_nr)
Brandon Williams0d1e0e72017-10-31 11:19:11 -07004706 options->flags.diff_from_contents = 1;
Junio C Hamanof2451942009-05-22 12:45:29 -07004707 else
Brandon Williams0d1e0e72017-10-31 11:19:11 -07004708 options->flags.diff_from_contents = 0;
Junio C Hamanof2451942009-05-22 12:45:29 -07004709
Brandon Williams0d1e0e72017-10-31 11:19:11 -07004710 if (options->flags.find_copies_harder)
Junio C Hamano03b9d562006-08-09 13:17:19 -07004711 options->detect_rename = DIFF_DETECT_COPY;
4712
Brandon Williams0d1e0e72017-10-31 11:19:11 -07004713 if (!options->flags.relative_name)
Junio C Hamanocd676a52008-02-12 14:26:02 -08004714 options->prefix = NULL;
4715 if (options->prefix)
4716 options->prefix_length = strlen(options->prefix);
4717 else
4718 options->prefix_length = 0;
4719
Timo Hirvonenc6744342006-06-24 20:21:53 +03004720 if (options->output_format & (DIFF_FORMAT_NAME |
4721 DIFF_FORMAT_NAME_STATUS |
4722 DIFF_FORMAT_CHECKDIFF |
4723 DIFF_FORMAT_NO_OUTPUT))
4724 options->output_format &= ~(DIFF_FORMAT_RAW |
Junio C Hamano74e2abe2006-10-12 03:01:00 -07004725 DIFF_FORMAT_NUMSTAT |
Timo Hirvonenc6744342006-06-24 20:21:53 +03004726 DIFF_FORMAT_DIFFSTAT |
Nicolas Pitreebd124c2006-12-14 23:15:44 -05004727 DIFF_FORMAT_SHORTSTAT |
Linus Torvalds7df7c012008-02-12 13:26:31 -08004728 DIFF_FORMAT_DIRSTAT |
Timo Hirvonenc6744342006-06-24 20:21:53 +03004729 DIFF_FORMAT_SUMMARY |
4730 DIFF_FORMAT_PATCH);
4731
Junio C Hamano6973dca2006-04-21 23:57:45 -07004732 /*
4733 * These cases always need recursive; we do not drop caller-supplied
4734 * recursive bits for other formats here.
4735 */
Timo Hirvonenc6744342006-06-24 20:21:53 +03004736 if (options->output_format & (DIFF_FORMAT_PATCH |
Junio C Hamano74e2abe2006-10-12 03:01:00 -07004737 DIFF_FORMAT_NUMSTAT |
Timo Hirvonenc6744342006-06-24 20:21:53 +03004738 DIFF_FORMAT_DIFFSTAT |
Nicolas Pitreebd124c2006-12-14 23:15:44 -05004739 DIFF_FORMAT_SHORTSTAT |
Linus Torvalds7df7c012008-02-12 13:26:31 -08004740 DIFF_FORMAT_DIRSTAT |
Johannes Schindelind7014dc2006-10-03 23:09:56 +02004741 DIFF_FORMAT_SUMMARY |
Timo Hirvonenc6744342006-06-24 20:21:53 +03004742 DIFF_FORMAT_CHECKDIFF))
Brandon Williams0d1e0e72017-10-31 11:19:11 -07004743 options->flags.recursive = 1;
Junio C Hamano5e363542006-05-22 00:31:02 -07004744 /*
Junio C Hamano3969cf72006-06-27 15:08:19 -07004745 * Also pickaxe would not work very well if you do not say recursive
Junio C Hamano5e363542006-05-22 00:31:02 -07004746 */
Stefan Bellercf630512018-01-04 14:50:41 -08004747 if (options->pickaxe_opts & DIFF_PICKAXE_KINDS_MASK)
Brandon Williams0d1e0e72017-10-31 11:19:11 -07004748 options->flags.recursive = 1;
Jens Lehmannae6d5c12010-03-11 22:50:25 +01004749 /*
4750 * When patches are generated, submodules diffed against the work tree
4751 * must be checked for dirtiness too so it can be shown in the output
4752 */
4753 if (options->output_format & DIFF_FORMAT_PATCH)
Brandon Williams0d1e0e72017-10-31 11:19:11 -07004754 options->flags.dirty_submodules = 1;
Junio C Hamano5e363542006-05-22 00:31:02 -07004755
Junio C Hamano6973dca2006-04-21 23:57:45 -07004756 if (options->detect_rename && options->rename_limit < 0)
4757 options->rename_limit = diff_rename_limit_default;
brian m. carlsonde1d81d2018-05-02 00:25:52 +00004758 if (hexsz < options->abbrev)
4759 options->abbrev = hexsz; /* full */
Junio C Hamano6973dca2006-04-21 23:57:45 -07004760
Junio C Hamano68aacb22007-03-14 11:12:13 -07004761 /*
4762 * It does not make sense to show the first hit we happened
4763 * to have found. It does not make sense not to return with
4764 * exit code in such a case either.
4765 */
Brandon Williams0d1e0e72017-10-31 11:19:11 -07004766 if (options->flags.quick) {
Junio C Hamano68aacb22007-03-14 11:12:13 -07004767 options->output_format = DIFF_FORMAT_NO_OUTPUT;
Brandon Williams0d1e0e72017-10-31 11:19:11 -07004768 options->flags.exit_with_status = 1;
Junio C Hamano68aacb22007-03-14 11:12:13 -07004769 }
Zoltan Klingeree7fb0b2013-12-06 10:38:46 +11004770
Jeff King0ea7d5b2013-12-16 15:02:21 -05004771 options->diff_path_counter = 0;
Junio C Hamanob0e2c992014-06-16 10:07:09 -07004772
Brandon Williams0d1e0e72017-10-31 11:19:11 -07004773 if (options->flags.follow_renames && options->pathspec.nr != 1)
Jeff Kingdd63f162014-05-20 02:49:20 -04004774 die(_("--follow requires exactly one pathspec"));
Stefan Beller2e2d5ac2017-06-30 13:53:07 -07004775
4776 if (!options->use_color || external_diff())
4777 options->color_moved = 0;
Nguyễn Thái Ngọc Duy4a288472019-01-27 07:35:31 +07004778
Johannes Schindelin75408ca2022-01-28 12:02:50 +00004779 if (options->filter_not) {
4780 if (!options->filter)
4781 options->filter = ~filter_bit[DIFF_STATUS_FILTER_AON];
4782 options->filter &= ~options->filter_not;
4783 }
4784
Nguyễn Thái Ngọc Duy4a288472019-01-27 07:35:31 +07004785 FREE_AND_NULL(options->parseopts);
Junio C Hamano6973dca2006-04-21 23:57:45 -07004786}
4787
Matthieu Moydea007f2010-08-05 10:22:52 +02004788int parse_long_opt(const char *opt, const char **argv,
4789 const char **optarg)
4790{
4791 const char *arg = argv[0];
Jeff King95b567c2014-06-18 15:48:29 -04004792 if (!skip_prefix(arg, "--", &arg))
Matthieu Moydea007f2010-08-05 10:22:52 +02004793 return 0;
Jeff King95b567c2014-06-18 15:48:29 -04004794 if (!skip_prefix(arg, opt, &arg))
Matthieu Moydea007f2010-08-05 10:22:52 +02004795 return 0;
Nicolas Vigierb0d12fc2013-10-31 12:08:28 +01004796 if (*arg == '=') { /* stuck form: --option=value */
Matthieu Moydea007f2010-08-05 10:22:52 +02004797 *optarg = arg + 1;
4798 return 1;
4799 }
4800 if (*arg != '\0')
4801 return 0;
4802 /* separate form: --option value */
4803 if (!argv[1])
4804 die("Option '--%s' requires a value", opt);
4805 *optarg = argv[1];
4806 return 2;
4807}
4808
Nguyễn Thái Ngọc Duy84b50892019-02-21 18:16:10 +07004809static int diff_opt_stat(const struct option *opt, const char *value, int unset)
Jonathan Nieder4d7f7a42010-08-05 10:22:53 +02004810{
Nguyễn Thái Ngọc Duy84b50892019-02-21 18:16:10 +07004811 struct diff_options *options = opt->value;
Jonathan Nieder4d7f7a42010-08-05 10:22:53 +02004812 int width = options->stat_width;
4813 int name_width = options->stat_name_width;
Zbigniew Jędrzejewski-Szmek969fe572012-03-01 13:26:45 +01004814 int graph_width = options->stat_graph_width;
Michael J Gruber808e1db2011-05-27 14:36:41 +02004815 int count = options->stat_count;
Nguyễn Thái Ngọc Duy84b50892019-02-21 18:16:10 +07004816 char *end;
Jonathan Nieder4d7f7a42010-08-05 10:22:53 +02004817
Nguyễn Thái Ngọc Duy84b50892019-02-21 18:16:10 +07004818 BUG_ON_OPT_NEG(unset);
Jonathan Nieder4d7f7a42010-08-05 10:22:53 +02004819
Nguyễn Thái Ngọc Duy84b50892019-02-21 18:16:10 +07004820 if (!strcmp(opt->long_name, "stat")) {
4821 if (value) {
4822 width = strtoul(value, &end, 10);
4823 if (*end == ',')
4824 name_width = strtoul(end+1, &end, 10);
4825 if (*end == ',')
4826 count = strtoul(end+1, &end, 10);
4827 if (*end)
4828 return error(_("invalid --stat value: %s"), value);
Matthieu Moy1e572082010-08-05 10:22:54 +02004829 }
Nguyễn Thái Ngọc Duy84b50892019-02-21 18:16:10 +07004830 } else if (!strcmp(opt->long_name, "stat-width")) {
4831 width = strtoul(value, &end, 10);
4832 if (*end)
4833 return error(_("%s expects a numerical value"),
4834 opt->long_name);
4835 } else if (!strcmp(opt->long_name, "stat-name-width")) {
4836 name_width = strtoul(value, &end, 10);
4837 if (*end)
4838 return error(_("%s expects a numerical value"),
4839 opt->long_name);
4840 } else if (!strcmp(opt->long_name, "stat-graph-width")) {
4841 graph_width = strtoul(value, &end, 10);
4842 if (*end)
4843 return error(_("%s expects a numerical value"),
4844 opt->long_name);
4845 } else if (!strcmp(opt->long_name, "stat-count")) {
4846 count = strtoul(value, &end, 10);
4847 if (*end)
4848 return error(_("%s expects a numerical value"),
4849 opt->long_name);
4850 } else
4851 BUG("%s should not get here", opt->long_name);
Jonathan Nieder4d7f7a42010-08-05 10:22:53 +02004852
Jonathan Nieder4d7f7a42010-08-05 10:22:53 +02004853 options->output_format |= DIFF_FORMAT_DIFFSTAT;
4854 options->stat_name_width = name_width;
Zbigniew Jędrzejewski-Szmek969fe572012-03-01 13:26:45 +01004855 options->stat_graph_width = graph_width;
Jonathan Nieder4d7f7a42010-08-05 10:22:53 +02004856 options->stat_width = width;
Michael J Gruber808e1db2011-05-27 14:36:41 +02004857 options->stat_count = count;
Nguyễn Thái Ngọc Duy84b50892019-02-21 18:16:10 +07004858 return 0;
Jonathan Nieder4d7f7a42010-08-05 10:22:53 +02004859}
4860
Johan Herland333f3fb2011-04-29 11:36:18 +02004861static int parse_dirstat_opt(struct diff_options *options, const char *params)
4862{
Johan Herland51670fc2011-04-29 11:36:22 +02004863 struct strbuf errmsg = STRBUF_INIT;
4864 if (parse_dirstat_params(options, params, &errmsg))
Johan Herland7478ac52011-04-29 11:36:23 +02004865 die(_("Failed to parse --dirstat/-X option parameter:\n%s"),
Johan Herland51670fc2011-04-29 11:36:22 +02004866 errmsg.buf);
4867 strbuf_release(&errmsg);
Johan Herland333f3fb2011-04-29 11:36:18 +02004868 /*
4869 * The caller knows a dirstat-related option is given from the command
4870 * line; allow it to say "return this_function();"
4871 */
4872 options->output_format |= DIFF_FORMAT_DIRSTAT;
4873 return 1;
4874}
4875
Nguyễn Thái Ngọc Duyd2d3f272019-03-24 15:20:03 +07004876static int diff_opt_diff_filter(const struct option *option,
4877 const char *optarg, int unset)
Junio C Hamano1ecc1cb2013-07-17 15:05:46 -07004878{
Nguyễn Thái Ngọc Duyd2d3f272019-03-24 15:20:03 +07004879 struct diff_options *opt = option->value;
Junio C Hamano1ecc1cb2013-07-17 15:05:46 -07004880 int i, optch;
4881
Nguyễn Thái Ngọc Duyd2d3f272019-03-24 15:20:03 +07004882 BUG_ON_OPT_NEG(unset);
Junio C Hamano1ecc1cb2013-07-17 15:05:46 -07004883 prepare_filter_bits();
Junio C Hamano7f2ea5f2013-07-17 16:19:19 -07004884
Junio C Hamano1ecc1cb2013-07-17 15:05:46 -07004885 for (i = 0; (optch = optarg[i]) != '\0'; i++) {
4886 unsigned int bit;
Junio C Hamano7f2ea5f2013-07-17 16:19:19 -07004887 int negate;
4888
4889 if ('a' <= optch && optch <= 'z') {
4890 negate = 1;
4891 optch = toupper(optch);
4892 } else {
4893 negate = 0;
4894 }
Junio C Hamano1ecc1cb2013-07-17 15:05:46 -07004895
4896 bit = (0 <= optch && optch <= 'Z') ? filter_bit[optch] : 0;
4897 if (!bit)
Nguyễn Thái Ngọc Duyd2d3f272019-03-24 15:20:03 +07004898 return error(_("unknown change class '%c' in --diff-filter=%s"),
4899 optarg[i], optarg);
Junio C Hamano7f2ea5f2013-07-17 16:19:19 -07004900 if (negate)
Johannes Schindelin75408ca2022-01-28 12:02:50 +00004901 opt->filter_not |= bit;
Junio C Hamano7f2ea5f2013-07-17 16:19:19 -07004902 else
4903 opt->filter |= bit;
Junio C Hamano1ecc1cb2013-07-17 15:05:46 -07004904 }
4905 return 0;
4906}
4907
Nguyễn Thái Ngọc Duy3b335762018-12-09 11:25:21 +01004908static void enable_patch_output(int *fmt)
4909{
Matthieu Moy71482d32013-07-16 10:05:37 +02004910 *fmt &= ~DIFF_FORMAT_NO_OUTPUT;
4911 *fmt |= DIFF_FORMAT_PATCH;
4912}
4913
Nguyễn Thái Ngọc Duy5a749d92019-03-24 15:19:55 +07004914static int diff_opt_ws_error_highlight(const struct option *option,
4915 const char *arg, int unset)
Junio C Hamanob8767f72015-05-26 10:11:28 -07004916{
Nguyễn Thái Ngọc Duy5a749d92019-03-24 15:19:55 +07004917 struct diff_options *opt = option->value;
Junio C Hamano077965f2016-10-04 15:03:33 -07004918 int val = parse_ws_error_highlight(arg);
Junio C Hamanob8767f72015-05-26 10:11:28 -07004919
Nguyễn Thái Ngọc Duy5a749d92019-03-24 15:19:55 +07004920 BUG_ON_OPT_NEG(unset);
4921 if (val < 0)
4922 return error(_("unknown value after ws-error-highlight=%.*s"),
4923 -1 - val, arg);
Junio C Hamanob8767f72015-05-26 10:11:28 -07004924 opt->ws_error_highlight = val;
Nguyễn Thái Ngọc Duy5a749d92019-03-24 15:19:55 +07004925 return 0;
Junio C Hamanob8767f72015-05-26 10:11:28 -07004926}
4927
Nguyễn Thái Ngọc Duya75f28c2019-03-24 15:20:02 +07004928static int diff_opt_find_object(const struct option *option,
4929 const char *arg, int unset)
Stefan Beller15af58c2018-01-04 14:50:42 -08004930{
Nguyễn Thái Ngọc Duya75f28c2019-03-24 15:20:02 +07004931 struct diff_options *opt = option->value;
Stefan Beller15af58c2018-01-04 14:50:42 -08004932 struct object_id oid;
4933
Nguyễn Thái Ngọc Duya75f28c2019-03-24 15:20:02 +07004934 BUG_ON_OPT_NEG(unset);
Stefan Beller15af58c2018-01-04 14:50:42 -08004935 if (get_oid(arg, &oid))
Nguyễn Thái Ngọc Duya75f28c2019-03-24 15:20:02 +07004936 return error(_("unable to resolve '%s'"), arg);
Stefan Beller15af58c2018-01-04 14:50:42 -08004937
4938 if (!opt->objfind)
René Scharfeca56dad2021-03-13 17:17:22 +01004939 CALLOC_ARRAY(opt->objfind, 1);
Stefan Beller15af58c2018-01-04 14:50:42 -08004940
4941 opt->pickaxe_opts |= DIFF_PICKAXE_KIND_OBJFIND;
4942 opt->flags.recursive = 1;
4943 opt->flags.tree_in_recursive = 1;
4944 oidset_insert(opt->objfind, &oid);
Nguyễn Thái Ngọc Duya75f28c2019-03-24 15:20:02 +07004945 return 0;
Stefan Beller15af58c2018-01-04 14:50:42 -08004946}
4947
Nguyễn Thái Ngọc Duydf84a432019-03-05 19:30:11 +07004948static int diff_opt_anchored(const struct option *opt,
4949 const char *arg, int unset)
4950{
4951 struct diff_options *options = opt->value;
4952
4953 BUG_ON_OPT_NEG(unset);
4954 options->xdl_opts = DIFF_WITH_ALG(options, PATIENCE_DIFF);
4955 ALLOC_GROW(options->anchors, options->anchors_nr + 1,
4956 options->anchors_alloc);
4957 options->anchors[options->anchors_nr++] = xstrdup(arg);
4958 return 0;
4959}
4960
Nguyễn Thái Ngọc Duy6d9af6f2019-03-05 19:30:12 +07004961static int diff_opt_binary(const struct option *opt,
4962 const char *arg, int unset)
4963{
4964 struct diff_options *options = opt->value;
4965
4966 BUG_ON_OPT_NEG(unset);
4967 BUG_ON_OPT_ARG(arg);
4968 enable_patch_output(&options->output_format);
4969 options->flags.binary = 1;
4970 return 0;
4971}
4972
Nguyễn Thái Ngọc Duyced4e172019-02-21 18:16:13 +07004973static int diff_opt_break_rewrites(const struct option *opt,
4974 const char *arg, int unset)
4975{
4976 int *break_opt = opt->value;
4977 int opt1, opt2;
4978
4979 BUG_ON_OPT_NEG(unset);
4980 if (!arg)
4981 arg = "";
4982 opt1 = parse_rename_score(&arg);
4983 if (*arg == 0)
4984 opt2 = 0;
4985 else if (*arg != '/')
4986 return error(_("%s expects <n>/<m> form"), opt->long_name);
4987 else {
4988 arg++;
4989 opt2 = parse_rename_score(&arg);
4990 }
4991 if (*arg != 0)
4992 return error(_("%s expects <n>/<m> form"), opt->long_name);
4993 *break_opt = opt1 | (opt2 << 16);
4994 return 0;
4995}
4996
Nguyễn Thái Ngọc Duyaf2f3682019-02-21 18:16:12 +07004997static int diff_opt_char(const struct option *opt,
4998 const char *arg, int unset)
4999{
5000 char *value = opt->value;
5001
5002 BUG_ON_OPT_NEG(unset);
5003 if (arg[1])
5004 return error(_("%s expects a character, got '%s'"),
5005 opt->long_name, arg);
5006 *value = arg[0];
5007 return 0;
5008}
5009
Nguyễn Thái Ngọc Duy59311a92019-03-24 15:20:09 +07005010static int diff_opt_color_moved(const struct option *opt,
5011 const char *arg, int unset)
5012{
5013 struct diff_options *options = opt->value;
5014
5015 if (unset) {
5016 options->color_moved = COLOR_MOVED_NO;
5017 } else if (!arg) {
5018 if (diff_color_moved_default)
5019 options->color_moved = diff_color_moved_default;
5020 if (options->color_moved == COLOR_MOVED_NO)
5021 options->color_moved = COLOR_MOVED_DEFAULT;
5022 } else {
5023 int cm = parse_color_moved(arg);
5024 if (cm < 0)
5025 return error(_("bad --color-moved argument: %s"), arg);
5026 options->color_moved = cm;
5027 }
5028 return 0;
5029}
5030
Nguyễn Thái Ngọc Duy8ce20202019-03-24 15:20:10 +07005031static int diff_opt_color_moved_ws(const struct option *opt,
5032 const char *arg, int unset)
5033{
5034 struct diff_options *options = opt->value;
5035 unsigned cm;
5036
Nguyễn Thái Ngọc Duybb987292019-03-24 15:20:11 +07005037 if (unset) {
5038 options->color_moved_ws_handling = 0;
5039 return 0;
5040 }
5041
Nguyễn Thái Ngọc Duy8ce20202019-03-24 15:20:10 +07005042 cm = parse_color_moved_ws(arg);
5043 if (cm & COLOR_MOVED_WS_ERROR)
5044 return error(_("invalid mode '%s' in --color-moved-ws"), arg);
5045 options->color_moved_ws_handling = cm;
5046 return 0;
5047}
5048
Nguyễn Thái Ngọc Duy212db692019-03-05 19:30:20 +07005049static int diff_opt_color_words(const struct option *opt,
5050 const char *arg, int unset)
5051{
5052 struct diff_options *options = opt->value;
5053
5054 BUG_ON_OPT_NEG(unset);
5055 options->use_color = 1;
5056 options->word_diff = DIFF_WORDS_COLOR;
5057 options->word_regex = arg;
5058 return 0;
5059}
5060
Nguyễn Thái Ngọc Duy7d7942b2019-02-21 18:16:11 +07005061static int diff_opt_compact_summary(const struct option *opt,
5062 const char *arg, int unset)
5063{
5064 struct diff_options *options = opt->value;
5065
5066 BUG_ON_OPT_ARG(arg);
5067 if (unset) {
5068 options->flags.stat_with_summary = 0;
5069 } else {
5070 options->flags.stat_with_summary = 1;
5071 options->output_format |= DIFF_FORMAT_DIFFSTAT;
5072 }
5073 return 0;
5074}
5075
Nguyễn Thái Ngọc Duy10f35b12019-03-05 19:30:10 +07005076static int diff_opt_diff_algorithm(const struct option *opt,
5077 const char *arg, int unset)
5078{
5079 struct diff_options *options = opt->value;
5080 long value = parse_algorithm_value(arg);
5081
5082 BUG_ON_OPT_NEG(unset);
5083 if (value < 0)
5084 return error(_("option diff-algorithm accepts \"myers\", "
5085 "\"minimal\", \"patience\" and \"histogram\""));
5086
5087 /* clear out previous settings */
5088 DIFF_XDL_CLR(options, NEED_MINIMAL);
5089 options->xdl_opts &= ~XDF_DIFF_ALGORITHM_MASK;
5090 options->xdl_opts |= value;
5091 return 0;
5092}
5093
Nguyễn Thái Ngọc Duy4ce7aab2019-02-21 18:16:03 +07005094static int diff_opt_dirstat(const struct option *opt,
5095 const char *arg, int unset)
5096{
5097 struct diff_options *options = opt->value;
5098
5099 BUG_ON_OPT_NEG(unset);
5100 if (!strcmp(opt->long_name, "cumulative")) {
5101 if (arg)
5102 BUG("how come --cumulative take a value?");
5103 arg = "cumulative";
5104 } else if (!strcmp(opt->long_name, "dirstat-by-file"))
5105 parse_dirstat_opt(options, "files");
5106 parse_dirstat_opt(options, arg ? arg : "");
5107 return 0;
5108}
5109
Nguyễn Thái Ngọc Duy7f648502019-02-21 18:16:16 +07005110static int diff_opt_find_copies(const struct option *opt,
5111 const char *arg, int unset)
5112{
5113 struct diff_options *options = opt->value;
5114
5115 BUG_ON_OPT_NEG(unset);
5116 if (!arg)
5117 arg = "";
5118 options->rename_score = parse_rename_score(&arg);
5119 if (*arg != 0)
5120 return error(_("invalid argument to %s"), opt->long_name);
5121
5122 if (options->detect_rename == DIFF_DETECT_COPY)
5123 options->flags.find_copies_harder = 1;
5124 else
5125 options->detect_rename = DIFF_DETECT_COPY;
5126
5127 return 0;
5128}
5129
Nguyễn Thái Ngọc Duyf4763082019-02-21 18:16:14 +07005130static int diff_opt_find_renames(const struct option *opt,
5131 const char *arg, int unset)
5132{
5133 struct diff_options *options = opt->value;
5134
5135 BUG_ON_OPT_NEG(unset);
5136 if (!arg)
5137 arg = "";
5138 options->rename_score = parse_rename_score(&arg);
5139 if (*arg != 0)
5140 return error(_("invalid argument to %s"), opt->long_name);
5141
5142 options->detect_rename = DIFF_DETECT_RENAME;
5143 return 0;
5144}
5145
Nguyễn Thái Ngọc Duy1e9250b2019-03-05 19:30:16 +07005146static int diff_opt_follow(const struct option *opt,
5147 const char *arg, int unset)
5148{
5149 struct diff_options *options = opt->value;
5150
5151 BUG_ON_OPT_ARG(arg);
5152 if (unset) {
5153 options->flags.follow_renames = 0;
5154 options->flags.default_follow_renames = 0;
5155 } else {
5156 options->flags.follow_renames = 1;
5157 }
5158 return 0;
5159}
5160
Nguyễn Thái Ngọc Duyb680ee12019-03-05 19:30:25 +07005161static int diff_opt_ignore_submodules(const struct option *opt,
5162 const char *arg, int unset)
5163{
5164 struct diff_options *options = opt->value;
5165
5166 BUG_ON_OPT_NEG(unset);
5167 if (!arg)
5168 arg = "all";
5169 options->flags.override_submodule_config = 1;
5170 handle_ignore_submodules_arg(options, arg);
5171 return 0;
5172}
5173
Nguyễn Thái Ngọc Duy2f81cf92019-03-24 15:20:06 +07005174static int diff_opt_line_prefix(const struct option *opt,
5175 const char *optarg, int unset)
5176{
5177 struct diff_options *options = opt->value;
5178
5179 BUG_ON_OPT_NEG(unset);
5180 options->line_prefix = optarg;
5181 options->line_prefix_length = strlen(options->line_prefix);
5182 graph_setup_line_prefix(options);
5183 return 0;
5184}
5185
Nguyễn Thái Ngọc Duy11c659d2019-03-24 15:20:07 +07005186static int diff_opt_no_prefix(const struct option *opt,
5187 const char *optarg, int unset)
5188{
5189 struct diff_options *options = opt->value;
5190
5191 BUG_ON_OPT_NEG(unset);
5192 BUG_ON_OPT_ARG(optarg);
5193 options->a_prefix = "";
5194 options->b_prefix = "";
5195 return 0;
5196}
5197
Nguyễn Thái Ngọc Duyaf2f3682019-02-21 18:16:12 +07005198static enum parse_opt_result diff_opt_output(struct parse_opt_ctx_t *ctx,
5199 const struct option *opt,
5200 const char *arg, int unset)
5201{
5202 struct diff_options *options = opt->value;
5203 char *path;
5204
5205 BUG_ON_OPT_NEG(unset);
5206 path = prefix_filename(ctx->prefix, arg);
5207 options->file = xfopen(path, "w");
5208 options->close_file = 1;
5209 if (options->use_color != GIT_COLOR_ALWAYS)
5210 options->use_color = GIT_COLOR_NEVER;
5211 free(path);
5212 return 0;
5213}
5214
Nguyễn Thái Ngọc Duy31fd6402019-03-05 19:30:08 +07005215static int diff_opt_patience(const struct option *opt,
5216 const char *arg, int unset)
5217{
5218 struct diff_options *options = opt->value;
5219 int i;
5220
5221 BUG_ON_OPT_NEG(unset);
5222 BUG_ON_OPT_ARG(arg);
5223 options->xdl_opts = DIFF_WITH_ALG(options, PATIENCE_DIFF);
5224 /*
5225 * Both --patience and --anchored use PATIENCE_DIFF
5226 * internally, so remove any anchors previously
5227 * specified.
5228 */
5229 for (i = 0; i < options->anchors_nr; i++)
5230 free(options->anchors[i]);
5231 options->anchors_nr = 0;
5232 return 0;
5233}
5234
Michał Kępień296d4a92020-10-20 08:48:09 +02005235static int diff_opt_ignore_regex(const struct option *opt,
5236 const char *arg, int unset)
5237{
5238 struct diff_options *options = opt->value;
5239 regex_t *regex;
5240
5241 BUG_ON_OPT_NEG(unset);
5242 regex = xmalloc(sizeof(*regex));
5243 if (regcomp(regex, arg, REG_EXTENDED | REG_NEWLINE))
5244 return error(_("invalid regex given to -I: '%s'"), arg);
5245 ALLOC_GROW(options->ignore_regex, options->ignore_regex_nr + 1,
5246 options->ignore_regex_alloc);
5247 options->ignore_regex[options->ignore_regex_nr++] = regex;
5248 return 0;
5249}
5250
Nguyễn Thái Ngọc Duya41cfb32019-03-24 15:19:59 +07005251static int diff_opt_pickaxe_regex(const struct option *opt,
5252 const char *arg, int unset)
5253{
5254 struct diff_options *options = opt->value;
5255
5256 BUG_ON_OPT_NEG(unset);
5257 options->pickaxe = arg;
5258 options->pickaxe_opts |= DIFF_PICKAXE_KIND_G;
5259 return 0;
5260}
5261
5262static int diff_opt_pickaxe_string(const struct option *opt,
5263 const char *arg, int unset)
5264{
5265 struct diff_options *options = opt->value;
5266
5267 BUG_ON_OPT_NEG(unset);
5268 options->pickaxe = arg;
5269 options->pickaxe_opts |= DIFF_PICKAXE_KIND_S;
5270 return 0;
5271}
5272
Nguyễn Thái Ngọc Duy0b1c5b52019-02-21 18:16:19 +07005273static int diff_opt_relative(const struct option *opt,
5274 const char *arg, int unset)
5275{
5276 struct diff_options *options = opt->value;
5277
Laurent Arnoudc28ded82020-05-22 12:46:18 +02005278 options->flags.relative_name = !unset;
Nguyễn Thái Ngọc Duy0b1c5b52019-02-21 18:16:19 +07005279 if (arg)
5280 options->prefix = arg;
5281 return 0;
5282}
5283
Nguyễn Thái Ngọc Duy125dcea2019-03-05 19:30:26 +07005284static int diff_opt_submodule(const struct option *opt,
5285 const char *arg, int unset)
5286{
5287 struct diff_options *options = opt->value;
5288
5289 BUG_ON_OPT_NEG(unset);
5290 if (!arg)
5291 arg = "log";
5292 if (parse_submodule_params(options, arg))
5293 return error(_("failed to parse --submodule option parameter: '%s'"),
5294 arg);
5295 return 0;
5296}
5297
Nguyễn Thái Ngọc Duy8ab76972019-03-05 19:30:24 +07005298static int diff_opt_textconv(const struct option *opt,
5299 const char *arg, int unset)
5300{
5301 struct diff_options *options = opt->value;
5302
5303 BUG_ON_OPT_ARG(arg);
5304 if (unset) {
5305 options->flags.allow_textconv = 0;
5306 } else {
5307 options->flags.allow_textconv = 1;
5308 options->flags.textconv_set_via_cmdline = 1;
5309 }
5310 return 0;
5311}
5312
Nguyễn Thái Ngọc Duyd473e2e2019-01-27 07:35:33 +07005313static int diff_opt_unified(const struct option *opt,
5314 const char *arg, int unset)
5315{
5316 struct diff_options *options = opt->value;
5317 char *s;
5318
5319 BUG_ON_OPT_NEG(unset);
5320
Nguyễn Thái Ngọc Duy8ef05192019-05-29 16:11:15 +07005321 if (arg) {
5322 options->context = strtol(arg, &s, 10);
5323 if (*s)
5324 return error(_("%s expects a numerical value"), "--unified");
5325 }
Nguyễn Thái Ngọc Duyd473e2e2019-01-27 07:35:33 +07005326 enable_patch_output(&options->output_format);
5327
5328 return 0;
5329}
5330
Nguyễn Thái Ngọc Duye9fb39b2019-03-05 19:30:18 +07005331static int diff_opt_word_diff(const struct option *opt,
5332 const char *arg, int unset)
5333{
5334 struct diff_options *options = opt->value;
5335
5336 BUG_ON_OPT_NEG(unset);
5337 if (arg) {
5338 if (!strcmp(arg, "plain"))
5339 options->word_diff = DIFF_WORDS_PLAIN;
5340 else if (!strcmp(arg, "color")) {
5341 options->use_color = 1;
5342 options->word_diff = DIFF_WORDS_COLOR;
5343 }
5344 else if (!strcmp(arg, "porcelain"))
5345 options->word_diff = DIFF_WORDS_PORCELAIN;
5346 else if (!strcmp(arg, "none"))
5347 options->word_diff = DIFF_WORDS_NONE;
5348 else
5349 return error(_("bad --word-diff argument: %s"), arg);
5350 } else {
5351 if (options->word_diff == DIFF_WORDS_NONE)
5352 options->word_diff = DIFF_WORDS_PLAIN;
5353 }
5354 return 0;
5355}
5356
Nguyễn Thái Ngọc Duy797df112019-03-05 19:30:19 +07005357static int diff_opt_word_diff_regex(const struct option *opt,
5358 const char *arg, int unset)
5359{
5360 struct diff_options *options = opt->value;
5361
5362 BUG_ON_OPT_NEG(unset);
5363 if (options->word_diff == DIFF_WORDS_NONE)
5364 options->word_diff = DIFF_WORDS_PLAIN;
5365 options->word_regex = arg;
5366 return 0;
5367}
5368
Junio C Hamano1eb41362021-02-11 11:57:50 -08005369static int diff_opt_rotate_to(const struct option *opt, const char *arg, int unset)
5370{
5371 struct diff_options *options = opt->value;
5372
5373 BUG_ON_OPT_NEG(unset);
5374 if (!strcmp(opt->long_name, "skip-to"))
5375 options->skip_instead_of_rotate = 1;
5376 else
5377 options->skip_instead_of_rotate = 0;
5378 options->rotate_to = arg;
5379 return 0;
5380}
5381
Nguyễn Thái Ngọc Duy4a288472019-01-27 07:35:31 +07005382static void prep_parse_options(struct diff_options *options)
5383{
5384 struct option parseopts[] = {
Nguyễn Thái Ngọc Duycc013c22019-01-27 07:35:32 +07005385 OPT_GROUP(N_("Diff output format options")),
5386 OPT_BITOP('p', "patch", &options->output_format,
5387 N_("generate patch"),
5388 DIFF_FORMAT_PATCH, DIFF_FORMAT_NO_OUTPUT),
Nguyễn Thái Ngọc Duye01df7a2019-02-21 18:16:09 +07005389 OPT_BIT_F('s', "no-patch", &options->output_format,
5390 N_("suppress diff output"),
5391 DIFF_FORMAT_NO_OUTPUT, PARSE_OPT_NONEG),
Nguyễn Thái Ngọc Duycc013c22019-01-27 07:35:32 +07005392 OPT_BITOP('u', NULL, &options->output_format,
5393 N_("generate patch"),
5394 DIFF_FORMAT_PATCH, DIFF_FORMAT_NO_OUTPUT),
Nguyễn Thái Ngọc Duyd473e2e2019-01-27 07:35:33 +07005395 OPT_CALLBACK_F('U', "unified", options, N_("<n>"),
5396 N_("generate diffs with <n> lines context"),
Junio C Hamano20aa7c52019-05-30 10:50:44 -07005397 PARSE_OPT_NONEG | PARSE_OPT_OPTARG, diff_opt_unified),
Nguyễn Thái Ngọc Duy7fd9a1b2019-01-27 07:35:34 +07005398 OPT_BOOL('W', "function-context", &options->flags.funccontext,
5399 N_("generate diffs with <n> lines context")),
Nguyễn Thái Ngọc Duyed881482019-01-27 07:35:35 +07005400 OPT_BIT_F(0, "raw", &options->output_format,
5401 N_("generate the diff in raw format"),
5402 DIFF_FORMAT_RAW, PARSE_OPT_NONEG),
Nguyễn Thái Ngọc Duyc659f302019-02-16 18:36:35 +07005403 OPT_BITOP(0, "patch-with-raw", &options->output_format,
5404 N_("synonym for '-p --raw'"),
5405 DIFF_FORMAT_PATCH | DIFF_FORMAT_RAW,
5406 DIFF_FORMAT_NO_OUTPUT),
Nguyễn Thái Ngọc Duye550f582019-02-21 18:16:06 +07005407 OPT_BITOP(0, "patch-with-stat", &options->output_format,
5408 N_("synonym for '-p --stat'"),
5409 DIFF_FORMAT_PATCH | DIFF_FORMAT_DIFFSTAT,
5410 DIFF_FORMAT_NO_OUTPUT),
Nguyễn Thái Ngọc Duye5673622019-02-16 18:36:36 +07005411 OPT_BIT_F(0, "numstat", &options->output_format,
5412 N_("machine friendly --stat"),
5413 DIFF_FORMAT_NUMSTAT, PARSE_OPT_NONEG),
5414 OPT_BIT_F(0, "shortstat", &options->output_format,
5415 N_("output only the last line of --stat"),
5416 DIFF_FORMAT_SHORTSTAT, PARSE_OPT_NONEG),
Nguyễn Thái Ngọc Duy4ce7aab2019-02-21 18:16:03 +07005417 OPT_CALLBACK_F('X', "dirstat", options, N_("<param1,param2>..."),
5418 N_("output the distribution of relative amount of changes for each sub-directory"),
5419 PARSE_OPT_NONEG | PARSE_OPT_OPTARG,
5420 diff_opt_dirstat),
5421 OPT_CALLBACK_F(0, "cumulative", options, NULL,
5422 N_("synonym for --dirstat=cumulative"),
5423 PARSE_OPT_NONEG | PARSE_OPT_NOARG,
5424 diff_opt_dirstat),
5425 OPT_CALLBACK_F(0, "dirstat-by-file", options, N_("<param1,param2>..."),
5426 N_("synonym for --dirstat=files,param1,param2..."),
5427 PARSE_OPT_NONEG | PARSE_OPT_OPTARG,
5428 diff_opt_dirstat),
Nguyễn Thái Ngọc Duyfc6af3e2019-02-21 18:16:04 +07005429 OPT_BIT_F(0, "check", &options->output_format,
5430 N_("warn if changes introduce conflict markers or whitespace errors"),
5431 DIFF_FORMAT_CHECKDIFF, PARSE_OPT_NONEG),
Nguyễn Thái Ngọc Duy70a30412019-02-21 18:16:05 +07005432 OPT_BIT_F(0, "summary", &options->output_format,
5433 N_("condensed summary such as creations, renames and mode changes"),
5434 DIFF_FORMAT_SUMMARY, PARSE_OPT_NONEG),
Nguyễn Thái Ngọc Duy0e840e22019-02-21 18:16:07 +07005435 OPT_BIT_F(0, "name-only", &options->output_format,
5436 N_("show only names of changed files"),
5437 DIFF_FORMAT_NAME, PARSE_OPT_NONEG),
Nguyễn Thái Ngọc Duya2387472019-02-21 18:16:08 +07005438 OPT_BIT_F(0, "name-status", &options->output_format,
5439 N_("show only names and status of changed files"),
5440 DIFF_FORMAT_NAME_STATUS, PARSE_OPT_NONEG),
Nguyễn Thái Ngọc Duy84b50892019-02-21 18:16:10 +07005441 OPT_CALLBACK_F(0, "stat", options, N_("<width>[,<name-width>[,<count>]]"),
5442 N_("generate diffstat"),
5443 PARSE_OPT_NONEG | PARSE_OPT_OPTARG, diff_opt_stat),
5444 OPT_CALLBACK_F(0, "stat-width", options, N_("<width>"),
5445 N_("generate diffstat with a given width"),
5446 PARSE_OPT_NONEG, diff_opt_stat),
5447 OPT_CALLBACK_F(0, "stat-name-width", options, N_("<width>"),
5448 N_("generate diffstat with a given name width"),
5449 PARSE_OPT_NONEG, diff_opt_stat),
5450 OPT_CALLBACK_F(0, "stat-graph-width", options, N_("<width>"),
5451 N_("generate diffstat with a given graph width"),
5452 PARSE_OPT_NONEG, diff_opt_stat),
5453 OPT_CALLBACK_F(0, "stat-count", options, N_("<count>"),
5454 N_("generate diffstat with limited lines"),
5455 PARSE_OPT_NONEG, diff_opt_stat),
Nguyễn Thái Ngọc Duy7d7942b2019-02-21 18:16:11 +07005456 OPT_CALLBACK_F(0, "compact-summary", options, NULL,
5457 N_("generate compact summary in diffstat"),
5458 PARSE_OPT_NOARG, diff_opt_compact_summary),
Nguyễn Thái Ngọc Duy6d9af6f2019-03-05 19:30:12 +07005459 OPT_CALLBACK_F(0, "binary", options, NULL,
5460 N_("output a binary diff that can be applied"),
5461 PARSE_OPT_NONEG | PARSE_OPT_NOARG, diff_opt_binary),
Nguyễn Thái Ngọc Duy4fe01672019-03-05 19:30:13 +07005462 OPT_BOOL(0, "full-index", &options->flags.full_index,
5463 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 +07005464 OPT_COLOR_FLAG(0, "color", &options->use_color,
5465 N_("show colored diff")),
Nguyễn Thái Ngọc Duy5a749d92019-03-24 15:19:55 +07005466 OPT_CALLBACK_F(0, "ws-error-highlight", options, N_("<kind>"),
5467 N_("highlight whitespace errors in the 'context', 'old' or 'new' lines in the diff"),
5468 PARSE_OPT_NONEG, diff_opt_ws_error_highlight),
Nguyễn Thái Ngọc Duy1a1eb162019-03-24 15:19:57 +07005469 OPT_SET_INT('z', NULL, &options->line_termination,
5470 N_("do not munge pathnames and use NULs as output field terminators in --raw or --numstat"),
5471 0),
Nguyễn Thái Ngọc Duyd8774182019-03-24 15:20:04 +07005472 OPT__ABBREV(&options->abbrev),
Nguyễn Thái Ngọc Duy31fba9d2019-03-24 15:20:05 +07005473 OPT_STRING_F(0, "src-prefix", &options->a_prefix, N_("<prefix>"),
5474 N_("show the given source prefix instead of \"a/\""),
5475 PARSE_OPT_NONEG),
5476 OPT_STRING_F(0, "dst-prefix", &options->b_prefix, N_("<prefix>"),
Jiang Xin8a1569d2019-06-02 23:11:22 +08005477 N_("show the given destination prefix instead of \"b/\""),
Nguyễn Thái Ngọc Duy31fba9d2019-03-24 15:20:05 +07005478 PARSE_OPT_NONEG),
Nguyễn Thái Ngọc Duy2f81cf92019-03-24 15:20:06 +07005479 OPT_CALLBACK_F(0, "line-prefix", options, N_("<prefix>"),
5480 N_("prepend an additional prefix to every line of output"),
5481 PARSE_OPT_NONEG, diff_opt_line_prefix),
Nguyễn Thái Ngọc Duy11c659d2019-03-24 15:20:07 +07005482 OPT_CALLBACK_F(0, "no-prefix", options, NULL,
5483 N_("do not show any source or destination prefix"),
5484 PARSE_OPT_NONEG | PARSE_OPT_NOARG, diff_opt_no_prefix),
Nguyễn Thái Ngọc Duy16ed6c92019-03-24 15:20:08 +07005485 OPT_INTEGER_F(0, "inter-hunk-context", &options->interhunkcontext,
5486 N_("show context between diff hunks up to the specified number of lines"),
5487 PARSE_OPT_NONEG),
Nguyễn Thái Ngọc Duyaf2f3682019-02-21 18:16:12 +07005488 OPT_CALLBACK_F(0, "output-indicator-new",
5489 &options->output_indicators[OUTPUT_INDICATOR_NEW],
5490 N_("<char>"),
5491 N_("specify the character to indicate a new line instead of '+'"),
5492 PARSE_OPT_NONEG, diff_opt_char),
5493 OPT_CALLBACK_F(0, "output-indicator-old",
5494 &options->output_indicators[OUTPUT_INDICATOR_OLD],
5495 N_("<char>"),
5496 N_("specify the character to indicate an old line instead of '-'"),
5497 PARSE_OPT_NONEG, diff_opt_char),
5498 OPT_CALLBACK_F(0, "output-indicator-context",
5499 &options->output_indicators[OUTPUT_INDICATOR_CONTEXT],
5500 N_("<char>"),
5501 N_("specify the character to indicate a context instead of ' '"),
5502 PARSE_OPT_NONEG, diff_opt_char),
5503
Nguyễn Thái Ngọc Duyced4e172019-02-21 18:16:13 +07005504 OPT_GROUP(N_("Diff rename options")),
5505 OPT_CALLBACK_F('B', "break-rewrites", &options->break_opt, N_("<n>[/<m>]"),
5506 N_("break complete rewrite changes into pairs of delete and create"),
5507 PARSE_OPT_NONEG | PARSE_OPT_OPTARG,
5508 diff_opt_break_rewrites),
Nguyễn Thái Ngọc Duyf4763082019-02-21 18:16:14 +07005509 OPT_CALLBACK_F('M', "find-renames", options, N_("<n>"),
5510 N_("detect renames"),
5511 PARSE_OPT_NONEG | PARSE_OPT_OPTARG,
5512 diff_opt_find_renames),
Nguyễn Thái Ngọc Duy1e533292019-02-21 18:16:15 +07005513 OPT_SET_INT_F('D', "irreversible-delete", &options->irreversible_delete,
5514 N_("omit the preimage for deletes"),
5515 1, PARSE_OPT_NONEG),
Nguyễn Thái Ngọc Duy7f648502019-02-21 18:16:16 +07005516 OPT_CALLBACK_F('C', "find-copies", options, N_("<n>"),
5517 N_("detect copies"),
5518 PARSE_OPT_NONEG | PARSE_OPT_OPTARG,
5519 diff_opt_find_copies),
Nguyễn Thái Ngọc Duybdd47412019-02-21 18:16:17 +07005520 OPT_BOOL(0, "find-copies-harder", &options->flags.find_copies_harder,
5521 N_("use unmodified files as source to find copies")),
Nguyễn Thái Ngọc Duycdc43eb2019-02-21 18:16:18 +07005522 OPT_SET_INT_F(0, "no-renames", &options->detect_rename,
5523 N_("disable rename detection"),
5524 0, PARSE_OPT_NONEG),
5525 OPT_BOOL(0, "rename-empty", &options->flags.rename_empty,
5526 N_("use empty blobs as rename source")),
Nguyễn Thái Ngọc Duy1e9250b2019-03-05 19:30:16 +07005527 OPT_CALLBACK_F(0, "follow", options, NULL,
5528 N_("continue listing the history of a file beyond renames"),
5529 PARSE_OPT_NOARG, diff_opt_follow),
Nguyễn Thái Ngọc Duybffee742019-03-24 15:19:58 +07005530 OPT_INTEGER('l', NULL, &options->rename_limit,
5531 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 +07005532
Nguyễn Thái Ngọc Duy2e75f922019-02-21 18:16:20 +07005533 OPT_GROUP(N_("Diff algorithm options")),
5534 OPT_BIT(0, "minimal", &options->xdl_opts,
5535 N_("produce the smallest possible diff"),
5536 XDF_NEED_MINIMAL),
Nguyễn Thái Ngọc Duy87649a12019-02-21 18:16:21 +07005537 OPT_BIT_F('w', "ignore-all-space", &options->xdl_opts,
5538 N_("ignore whitespace when comparing lines"),
5539 XDF_IGNORE_WHITESPACE, PARSE_OPT_NONEG),
5540 OPT_BIT_F('b', "ignore-space-change", &options->xdl_opts,
5541 N_("ignore changes in amount of whitespace"),
5542 XDF_IGNORE_WHITESPACE_CHANGE, PARSE_OPT_NONEG),
5543 OPT_BIT_F(0, "ignore-space-at-eol", &options->xdl_opts,
5544 N_("ignore changes in whitespace at EOL"),
5545 XDF_IGNORE_WHITESPACE_AT_EOL, PARSE_OPT_NONEG),
5546 OPT_BIT_F(0, "ignore-cr-at-eol", &options->xdl_opts,
5547 N_("ignore carrier-return at the end of line"),
5548 XDF_IGNORE_CR_AT_EOL, PARSE_OPT_NONEG),
5549 OPT_BIT_F(0, "ignore-blank-lines", &options->xdl_opts,
5550 N_("ignore changes whose lines are all blank"),
5551 XDF_IGNORE_BLANK_LINES, PARSE_OPT_NONEG),
Michał Kępień296d4a92020-10-20 08:48:09 +02005552 OPT_CALLBACK_F('I', "ignore-matching-lines", options, N_("<regex>"),
5553 N_("ignore changes whose all lines match <regex>"),
5554 0, diff_opt_ignore_regex),
Nguyễn Thái Ngọc Duy06f77512019-03-05 19:30:07 +07005555 OPT_BIT(0, "indent-heuristic", &options->xdl_opts,
5556 N_("heuristic to shift diff hunk boundaries for easy reading"),
5557 XDF_INDENT_HEURISTIC),
Nguyễn Thái Ngọc Duy31fd6402019-03-05 19:30:08 +07005558 OPT_CALLBACK_F(0, "patience", options, NULL,
5559 N_("generate diff using the \"patience diff\" algorithm"),
5560 PARSE_OPT_NONEG | PARSE_OPT_NOARG,
5561 diff_opt_patience),
Nguyễn Thái Ngọc Duyf1e68ef2019-03-05 19:30:09 +07005562 OPT_BITOP(0, "histogram", &options->xdl_opts,
5563 N_("generate diff using the \"histogram diff\" algorithm"),
5564 XDF_HISTOGRAM_DIFF, XDF_DIFF_ALGORITHM_MASK),
Nguyễn Thái Ngọc Duy10f35b12019-03-05 19:30:10 +07005565 OPT_CALLBACK_F(0, "diff-algorithm", options, N_("<algorithm>"),
5566 N_("choose a diff algorithm"),
5567 PARSE_OPT_NONEG, diff_opt_diff_algorithm),
Nguyễn Thái Ngọc Duydf84a432019-03-05 19:30:11 +07005568 OPT_CALLBACK_F(0, "anchored", options, N_("<text>"),
5569 N_("generate diff using the \"anchored diff\" algorithm"),
5570 PARSE_OPT_NONEG, diff_opt_anchored),
Nguyễn Thái Ngọc Duye9fb39b2019-03-05 19:30:18 +07005571 OPT_CALLBACK_F(0, "word-diff", options, N_("<mode>"),
5572 N_("show word diff, using <mode> to delimit changed words"),
5573 PARSE_OPT_NONEG | PARSE_OPT_OPTARG, diff_opt_word_diff),
Nguyễn Thái Ngọc Duy797df112019-03-05 19:30:19 +07005574 OPT_CALLBACK_F(0, "word-diff-regex", options, N_("<regex>"),
5575 N_("use <regex> to decide what a word is"),
5576 PARSE_OPT_NONEG, diff_opt_word_diff_regex),
Nguyễn Thái Ngọc Duy212db692019-03-05 19:30:20 +07005577 OPT_CALLBACK_F(0, "color-words", options, N_("<regex>"),
5578 N_("equivalent to --word-diff=color --word-diff-regex=<regex>"),
5579 PARSE_OPT_NONEG | PARSE_OPT_OPTARG, diff_opt_color_words),
Nguyễn Thái Ngọc Duy59311a92019-03-24 15:20:09 +07005580 OPT_CALLBACK_F(0, "color-moved", options, N_("<mode>"),
Jean-Noël Avila8bcd8f42019-05-17 21:26:19 +02005581 N_("moved lines of code are colored differently"),
Nguyễn Thái Ngọc Duy59311a92019-03-24 15:20:09 +07005582 PARSE_OPT_OPTARG, diff_opt_color_moved),
Nguyễn Thái Ngọc Duy8ce20202019-03-24 15:20:10 +07005583 OPT_CALLBACK_F(0, "color-moved-ws", options, N_("<mode>"),
5584 N_("how white spaces are ignored in --color-moved"),
Nguyễn Thái Ngọc Duybb987292019-03-24 15:20:11 +07005585 0, diff_opt_color_moved_ws),
Nguyễn Thái Ngọc Duy2e75f922019-02-21 18:16:20 +07005586
Jean-Noël Avila8bcd8f42019-05-17 21:26:19 +02005587 OPT_GROUP(N_("Other diff options")),
Nguyễn Thái Ngọc Duy0b1c5b52019-02-21 18:16:19 +07005588 OPT_CALLBACK_F(0, "relative", options, N_("<prefix>"),
5589 N_("when run from subdir, exclude changes outside and show relative paths"),
Laurent Arnoudc28ded82020-05-22 12:46:18 +02005590 PARSE_OPT_OPTARG,
Nguyễn Thái Ngọc Duy0b1c5b52019-02-21 18:16:19 +07005591 diff_opt_relative),
Nguyễn Thái Ngọc Duyc37d4c02019-03-05 19:30:14 +07005592 OPT_BOOL('a', "text", &options->flags.text,
5593 N_("treat all files as text")),
Nguyễn Thái Ngọc Duy4f99f292019-03-05 19:30:15 +07005594 OPT_BOOL('R', NULL, &options->flags.reverse_diff,
5595 N_("swap two inputs, reverse the diff")),
Nguyễn Thái Ngọc Duy1086ea02019-03-05 19:30:21 +07005596 OPT_BOOL(0, "exit-code", &options->flags.exit_with_status,
5597 N_("exit with 1 if there were differences, 0 otherwise")),
Nguyễn Thái Ngọc Duy9bbaf1c2019-03-05 19:30:22 +07005598 OPT_BOOL(0, "quiet", &options->flags.quick,
5599 N_("disable all output of the program")),
Nguyễn Thái Ngọc Duy0cda1002019-03-05 19:30:23 +07005600 OPT_BOOL(0, "ext-diff", &options->flags.allow_external,
5601 N_("allow an external diff helper to be executed")),
Nguyễn Thái Ngọc Duy8ab76972019-03-05 19:30:24 +07005602 OPT_CALLBACK_F(0, "textconv", options, NULL,
5603 N_("run external text conversion filters when comparing binary files"),
5604 PARSE_OPT_NOARG, diff_opt_textconv),
Nguyễn Thái Ngọc Duyb680ee12019-03-05 19:30:25 +07005605 OPT_CALLBACK_F(0, "ignore-submodules", options, N_("<when>"),
5606 N_("ignore changes to submodules in the diff generation"),
5607 PARSE_OPT_NONEG | PARSE_OPT_OPTARG,
5608 diff_opt_ignore_submodules),
Nguyễn Thái Ngọc Duy125dcea2019-03-05 19:30:26 +07005609 OPT_CALLBACK_F(0, "submodule", options, N_("<format>"),
5610 N_("specify how differences in submodules are shown"),
5611 PARSE_OPT_NONEG | PARSE_OPT_OPTARG,
5612 diff_opt_submodule),
Nguyễn Thái Ngọc Duy96860e42019-03-24 15:19:56 +07005613 OPT_SET_INT_F(0, "ita-invisible-in-index", &options->ita_invisible_in_index,
5614 N_("hide 'git add -N' entries from the index"),
5615 1, PARSE_OPT_NONEG),
5616 OPT_SET_INT_F(0, "ita-visible-in-index", &options->ita_invisible_in_index,
5617 N_("treat 'git add -N' entries as real in the index"),
5618 0, PARSE_OPT_NONEG),
Nguyễn Thái Ngọc Duya41cfb32019-03-24 15:19:59 +07005619 OPT_CALLBACK_F('S', NULL, options, N_("<string>"),
5620 N_("look for differences that change the number of occurrences of the specified string"),
5621 0, diff_opt_pickaxe_string),
5622 OPT_CALLBACK_F('G', NULL, options, N_("<regex>"),
5623 N_("look for differences that change the number of occurrences of the specified regex"),
5624 0, diff_opt_pickaxe_regex),
Nguyễn Thái Ngọc Duy85f8e882019-03-24 15:20:00 +07005625 OPT_BIT_F(0, "pickaxe-all", &options->pickaxe_opts,
5626 N_("show all changes in the changeset with -S or -G"),
5627 DIFF_PICKAXE_ALL, PARSE_OPT_NONEG),
5628 OPT_BIT_F(0, "pickaxe-regex", &options->pickaxe_opts,
5629 N_("treat <string> in -S as extended POSIX regular expression"),
5630 DIFF_PICKAXE_REGEX, PARSE_OPT_NONEG),
Nguyễn Thái Ngọc Duyf7318142019-03-24 15:20:01 +07005631 OPT_FILENAME('O', NULL, &options->orderfile,
5632 N_("control the order in which files appear in the output")),
Junio C Hamano1eb41362021-02-11 11:57:50 -08005633 OPT_CALLBACK_F(0, "rotate-to", options, N_("<path>"),
5634 N_("show the change in the specified path first"),
5635 PARSE_OPT_NONEG, diff_opt_rotate_to),
5636 OPT_CALLBACK_F(0, "skip-to", options, N_("<path>"),
5637 N_("skip the output to the specified path"),
5638 PARSE_OPT_NONEG, diff_opt_rotate_to),
Nguyễn Thái Ngọc Duya75f28c2019-03-24 15:20:02 +07005639 OPT_CALLBACK_F(0, "find-object", options, N_("<object-id>"),
5640 N_("look for differences that change the number of occurrences of the specified object"),
5641 PARSE_OPT_NONEG, diff_opt_find_object),
Nguyễn Thái Ngọc Duyd2d3f272019-03-24 15:20:03 +07005642 OPT_CALLBACK_F(0, "diff-filter", options, N_("[(A|C|D|M|R|T|U|X|B)...[*]]"),
5643 N_("select files by diff type"),
5644 PARSE_OPT_NONEG, diff_opt_diff_filter),
Nguyễn Thái Ngọc Duyaf2f3682019-02-21 18:16:12 +07005645 { OPTION_CALLBACK, 0, "output", options, N_("<file>"),
Abhradeep Chakraborty9e1f22c2022-02-23 14:27:34 +00005646 N_("output to a specific file"),
Nguyễn Thái Ngọc Duyaf2f3682019-02-21 18:16:12 +07005647 PARSE_OPT_NONEG, NULL, 0, diff_opt_output },
5648
Nguyễn Thái Ngọc Duy4a288472019-01-27 07:35:31 +07005649 OPT_END()
5650 };
5651
5652 ALLOC_ARRAY(options->parseopts, ARRAY_SIZE(parseopts));
5653 memcpy(options->parseopts, parseopts, sizeof(parseopts));
5654}
5655
Duy Nguyena97262c2016-01-21 18:48:44 +07005656int diff_opt_parse(struct diff_options *options,
5657 const char **av, int ac, const char *prefix)
Junio C Hamano6973dca2006-04-21 23:57:45 -07005658{
Duy Nguyena97262c2016-01-21 18:48:44 +07005659 if (!prefix)
5660 prefix = "";
5661
Nguyễn Thái Ngọc Duy4a288472019-01-27 07:35:31 +07005662 ac = parse_options(ac, av, prefix, options->parseopts, NULL,
5663 PARSE_OPT_KEEP_DASHDASH |
5664 PARSE_OPT_KEEP_UNKNOWN |
5665 PARSE_OPT_NO_INTERNAL_HELP |
5666 PARSE_OPT_ONE_SHOT |
5667 PARSE_OPT_STOP_AT_NON_OPTION);
5668
Nguyễn Thái Ngọc Duy8ce20202019-03-24 15:20:10 +07005669 return ac;
Junio C Hamano6973dca2006-04-21 23:57:45 -07005670}
5671
Kevin Ballard10ae7522010-09-27 16:58:25 -07005672int parse_rename_score(const char **cp_p)
Junio C Hamano6973dca2006-04-21 23:57:45 -07005673{
5674 unsigned long num, scale;
5675 int ch, dot;
5676 const char *cp = *cp_p;
5677
5678 num = 0;
5679 scale = 1;
5680 dot = 0;
Brian Gianforcaroeeefa7c2009-09-01 01:35:10 -04005681 for (;;) {
Junio C Hamano6973dca2006-04-21 23:57:45 -07005682 ch = *cp;
5683 if ( !dot && ch == '.' ) {
5684 scale = 1;
5685 dot = 1;
5686 } else if ( ch == '%' ) {
5687 scale = dot ? scale*100 : 100;
5688 cp++; /* % is always at the end */
5689 break;
5690 } else if ( ch >= '0' && ch <= '9' ) {
5691 if ( scale < 100000 ) {
5692 scale *= 10;
5693 num = (num*10) + (ch-'0');
5694 }
5695 } else {
5696 break;
5697 }
5698 cp++;
5699 }
5700 *cp_p = cp;
5701
5702 /* user says num divided by scale and we say internally that
5703 * is MAX_SCORE * num / scale.
5704 */
Shawn O. Pearcedc49cd72007-03-06 20:44:37 -05005705 return (int)((num >= scale) ? MAX_SCORE : (MAX_SCORE * num / scale));
Junio C Hamano6973dca2006-04-21 23:57:45 -07005706}
5707
Junio C Hamano6973dca2006-04-21 23:57:45 -07005708struct diff_queue_struct diff_queued_diff;
5709
5710void diff_q(struct diff_queue_struct *queue, struct diff_filepair *dp)
5711{
Dmitry S. Dolzhenko4c960a42014-03-04 02:31:53 +04005712 ALLOC_GROW(queue->queue, queue->nr + 1, queue->alloc);
Junio C Hamano6973dca2006-04-21 23:57:45 -07005713 queue->queue[queue->nr++] = dp;
5714}
5715
5716struct diff_filepair *diff_queue(struct diff_queue_struct *queue,
5717 struct diff_filespec *one,
5718 struct diff_filespec *two)
5719{
Junio C Hamanoef677682006-08-03 12:01:01 -07005720 struct diff_filepair *dp = xcalloc(1, sizeof(*dp));
Junio C Hamano6973dca2006-04-21 23:57:45 -07005721 dp->one = one;
5722 dp->two = two;
Junio C Hamano6973dca2006-04-21 23:57:45 -07005723 if (queue)
5724 diff_q(queue, dp);
5725 return dp;
5726}
5727
5728void diff_free_filepair(struct diff_filepair *p)
5729{
Linus Torvalds9fb88412007-10-25 11:19:10 -07005730 free_filespec(p->one);
5731 free_filespec(p->two);
Junio C Hamano6973dca2006-04-21 23:57:45 -07005732 free(p);
5733}
5734
Jeff Kingd6cece52016-10-20 02:20:07 -04005735const char *diff_aligned_abbrev(const struct object_id *oid, int len)
Junio C Hamano6973dca2006-04-21 23:57:45 -07005736{
5737 int abblen;
5738 const char *abbrev;
Junio C Hamano6973dca2006-04-21 23:57:45 -07005739
Ann T Ropea7cb6ac12017-12-03 22:27:42 +01005740 /* Do we want all 40 hex characters? */
brian m. carlson02afca12018-07-16 01:28:05 +00005741 if (len == the_hash_algo->hexsz)
Jeff Kingd6cece52016-10-20 02:20:07 -04005742 return oid_to_hex(oid);
5743
Ann T Ropea7cb6ac12017-12-03 22:27:42 +01005744 /* An abbreviated value is fine, possibly followed by an ellipsis. */
Jeff King4f036662016-10-20 02:21:25 -04005745 abbrev = diff_abbrev_oid(oid, len);
Ann T Ropea7cb6ac12017-12-03 22:27:42 +01005746
5747 if (!print_sha1_ellipsis())
5748 return abbrev;
5749
Junio C Hamano6973dca2006-04-21 23:57:45 -07005750 abblen = strlen(abbrev);
Junio C Hamanod709f1f2016-09-30 10:42:05 -07005751
5752 /*
Ann T Ropea7cb6ac12017-12-03 22:27:42 +01005753 * In well-behaved cases, where the abbreviated result is the
Junio C Hamanod709f1f2016-09-30 10:42:05 -07005754 * same as the requested length, append three dots after the
5755 * abbreviation (hence the whole logic is limited to the case
5756 * where abblen < 37); when the actual abbreviated result is a
5757 * bit longer than the requested length, we reduce the number
5758 * of dots so that they match the well-behaved ones. However,
5759 * if the actual abbreviation is longer than the requested
5760 * length by more than three, we give up on aligning, and add
5761 * three dots anyway, to indicate that the output is not the
5762 * full object name. Yes, this may be suboptimal, but this
5763 * appears only in "diff --raw --abbrev" output and it is not
5764 * worth the effort to change it now. Note that this would
5765 * likely to work fine when the automatic sizing of default
5766 * abbreviation length is used--we would be fed -1 in "len" in
5767 * that case, and will end up always appending three-dots, but
5768 * the automatic sizing is supposed to give abblen that ensures
5769 * uniqueness across all objects (statistically speaking).
5770 */
brian m. carlson02afca12018-07-16 01:28:05 +00005771 if (abblen < the_hash_algo->hexsz - 3) {
brian m. carlsondc015052017-03-26 16:01:24 +00005772 static char hex[GIT_MAX_HEXSZ + 1];
Junio C Hamano6973dca2006-04-21 23:57:45 -07005773 if (len < abblen && abblen <= len + 2)
Jeff King5096d492015-09-24 17:06:08 -04005774 xsnprintf(hex, sizeof(hex), "%s%.*s", abbrev, len+3-abblen, "..");
Junio C Hamano6973dca2006-04-21 23:57:45 -07005775 else
Jeff King5096d492015-09-24 17:06:08 -04005776 xsnprintf(hex, sizeof(hex), "%s...", abbrev);
Junio C Hamano6973dca2006-04-21 23:57:45 -07005777 return hex;
5778 }
Jeff Kingd6cece52016-10-20 02:20:07 -04005779
5780 return oid_to_hex(oid);
Junio C Hamano6973dca2006-04-21 23:57:45 -07005781}
5782
Pierre Habouzit663af342007-09-20 00:42:15 +02005783static void diff_flush_raw(struct diff_filepair *p, struct diff_options *opt)
Junio C Hamano6973dca2006-04-21 23:57:45 -07005784{
Pierre Habouzit663af342007-09-20 00:42:15 +02005785 int line_termination = opt->line_termination;
5786 int inter_name_termination = line_termination ? '\t' : '\0';
Timo Hirvonenc6744342006-06-24 20:21:53 +03005787
John Keeping30997bb2013-02-07 20:15:27 +00005788 fprintf(opt->file, "%s", diff_line_prefix(opt));
Pierre Habouzit663af342007-09-20 00:42:15 +02005789 if (!(opt->output_format & DIFF_FORMAT_NAME_STATUS)) {
Daniel Barkalowc0c77732008-03-09 22:43:39 -04005790 fprintf(opt->file, ":%06o %06o %s ", p->one->mode, p->two->mode,
Jeff Kingd6cece52016-10-20 02:20:07 -04005791 diff_aligned_abbrev(&p->one->oid, opt->abbrev));
brian m. carlsona0d12c42016-06-24 23:09:23 +00005792 fprintf(opt->file, "%s ",
Jeff Kingd6cece52016-10-20 02:20:07 -04005793 diff_aligned_abbrev(&p->two->oid, opt->abbrev));
Pierre Habouzit663af342007-09-20 00:42:15 +02005794 }
5795 if (p->score) {
Daniel Barkalowc0c77732008-03-09 22:43:39 -04005796 fprintf(opt->file, "%c%03d%c", p->status, similarity_index(p),
5797 inter_name_termination);
Pierre Habouzit663af342007-09-20 00:42:15 +02005798 } else {
Daniel Barkalowc0c77732008-03-09 22:43:39 -04005799 fprintf(opt->file, "%c%c", p->status, inter_name_termination);
Junio C Hamano6973dca2006-04-21 23:57:45 -07005800 }
5801
Junio C Hamanocd676a52008-02-12 14:26:02 -08005802 if (p->status == DIFF_STATUS_COPIED ||
5803 p->status == DIFF_STATUS_RENAMED) {
5804 const char *name_a, *name_b;
5805 name_a = p->one->path;
5806 name_b = p->two->path;
5807 strip_prefix(opt->prefix_length, &name_a, &name_b);
Daniel Barkalowc0c77732008-03-09 22:43:39 -04005808 write_name_quoted(name_a, opt->file, inter_name_termination);
5809 write_name_quoted(name_b, opt->file, line_termination);
Pierre Habouzit663af342007-09-20 00:42:15 +02005810 } else {
Junio C Hamanocd676a52008-02-12 14:26:02 -08005811 const char *name_a, *name_b;
5812 name_a = p->one->mode ? p->one->path : p->two->path;
5813 name_b = NULL;
5814 strip_prefix(opt->prefix_length, &name_a, &name_b);
Daniel Barkalowc0c77732008-03-09 22:43:39 -04005815 write_name_quoted(name_a, opt->file, line_termination);
Junio C Hamano6973dca2006-04-21 23:57:45 -07005816 }
Junio C Hamano6973dca2006-04-21 23:57:45 -07005817}
5818
5819int diff_unmodified_pair(struct diff_filepair *p)
5820{
5821 /* This function is written stricter than necessary to support
5822 * the currently implemented transformers, but the idea is to
5823 * let transformers to produce diff_filepairs any way they want,
5824 * and filter and clean them up here before producing the output.
5825 */
Pierre Habouzit663af342007-09-20 00:42:15 +02005826 struct diff_filespec *one = p->one, *two = p->two;
Junio C Hamano6973dca2006-04-21 23:57:45 -07005827
5828 if (DIFF_PAIR_UNMERGED(p))
5829 return 0; /* unmerged is interesting */
5830
Junio C Hamano6973dca2006-04-21 23:57:45 -07005831 /* deletion, addition, mode or type change
5832 * and rename are all interesting.
5833 */
5834 if (DIFF_FILE_VALID(one) != DIFF_FILE_VALID(two) ||
5835 DIFF_PAIR_MODE_CHANGED(p) ||
5836 strcmp(one->path, two->path))
5837 return 0;
5838
5839 /* both are valid and point at the same path. that is, we are
5840 * dealing with a change.
5841 */
brian m. carlson41c95602016-06-24 23:09:24 +00005842 if (one->oid_valid && two->oid_valid &&
Jeff King4a7e27e2018-08-28 17:22:40 -04005843 oideq(&one->oid, &two->oid) &&
Jens Lehmann85adbf22010-03-12 22:23:52 +01005844 !one->dirty_submodule && !two->dirty_submodule)
Junio C Hamano6973dca2006-04-21 23:57:45 -07005845 return 1; /* no change */
brian m. carlson41c95602016-06-24 23:09:24 +00005846 if (!one->oid_valid && !two->oid_valid)
Junio C Hamano6973dca2006-04-21 23:57:45 -07005847 return 1; /* both look at the same file on the filesystem. */
5848 return 0;
5849}
5850
5851static void diff_flush_patch(struct diff_filepair *p, struct diff_options *o)
5852{
Elijah Newren95433ee2022-02-02 02:37:34 +00005853 int include_conflict_headers =
5854 (additional_headers(o, p->one->path) &&
5855 (!o->filter || filter_bit_tst(DIFF_STATUS_UNMERGED, o)));
5856
5857 /*
5858 * Check if we can return early without showing a diff. Note that
5859 * diff_filepair only stores {oid, path, mode, is_valid}
5860 * information for each path, and thus diff_unmodified_pair() only
5861 * considers those bits of info. However, we do not want pairs
5862 * created by create_filepairs_for_header_only_notifications()
5863 * (which always look like unmodified pairs) to be ignored, so
5864 * return early if both p is unmodified AND we don't want to
5865 * include_conflict_headers.
5866 */
5867 if (diff_unmodified_pair(p) && !include_conflict_headers)
Junio C Hamano6973dca2006-04-21 23:57:45 -07005868 return;
5869
Elijah Newren95433ee2022-02-02 02:37:34 +00005870 /* Actually, we can also return early to avoid showing tree diffs */
Junio C Hamano6973dca2006-04-21 23:57:45 -07005871 if ((DIFF_FILE_VALID(p->one) && S_ISDIR(p->one->mode)) ||
5872 (DIFF_FILE_VALID(p->two) && S_ISDIR(p->two->mode)))
Elijah Newren95433ee2022-02-02 02:37:34 +00005873 return;
Junio C Hamano6973dca2006-04-21 23:57:45 -07005874
5875 run_diff(p, o);
5876}
5877
5878static void diff_flush_stat(struct diff_filepair *p, struct diff_options *o,
5879 struct diffstat_t *diffstat)
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; /* no useful stat for tree diffs */
Junio C Hamano6973dca2006-04-21 23:57:45 -07005887
5888 run_diffstat(p, o, diffstat);
5889}
5890
Johannes Schindelin88246892006-05-20 23:43:13 +02005891static void diff_flush_checkdiff(struct diff_filepair *p,
5892 struct diff_options *o)
5893{
5894 if (diff_unmodified_pair(p))
5895 return;
5896
5897 if ((DIFF_FILE_VALID(p->one) && S_ISDIR(p->one->mode)) ||
5898 (DIFF_FILE_VALID(p->two) && S_ISDIR(p->two->mode)))
Yann Dirsonc3fced62010-10-24 23:03:10 +02005899 return; /* nothing to check in tree diffs */
Johannes Schindelin88246892006-05-20 23:43:13 +02005900
5901 run_checkdiff(p, o);
5902}
5903
Elijah Newren95433ee2022-02-02 02:37:34 +00005904int diff_queue_is_empty(struct diff_options *o)
Junio C Hamano6973dca2006-04-21 23:57:45 -07005905{
5906 struct diff_queue_struct *q = &diff_queued_diff;
5907 int i;
Elijah Newren95433ee2022-02-02 02:37:34 +00005908 int include_conflict_headers =
5909 (o->additional_path_headers &&
5910 (!o->filter || filter_bit_tst(DIFF_STATUS_UNMERGED, o)));
5911
5912 if (include_conflict_headers)
5913 return 0;
5914
Junio C Hamano6973dca2006-04-21 23:57:45 -07005915 for (i = 0; i < q->nr; i++)
5916 if (!diff_unmodified_pair(q->queue[i]))
5917 return 0;
5918 return 1;
5919}
5920
5921#if DIFF_DEBUG
5922void diff_debug_filespec(struct diff_filespec *s, int x, const char *one)
5923{
5924 fprintf(stderr, "queue[%d] %s (%s) %s %06o %s\n",
5925 x, one ? one : "",
5926 s->path,
5927 DIFF_FILE_VALID(s) ? "valid" : "invalid",
5928 s->mode,
brian m. carlson41c95602016-06-24 23:09:24 +00005929 s->oid_valid ? oid_to_hex(&s->oid) : "");
Jeff King428d52a2014-01-16 20:21:59 -05005930 fprintf(stderr, "queue[%d] %s size %lu\n",
Junio C Hamano6973dca2006-04-21 23:57:45 -07005931 x, one ? one : "",
Jeff King428d52a2014-01-16 20:21:59 -05005932 s->size);
Junio C Hamano6973dca2006-04-21 23:57:45 -07005933}
5934
5935void diff_debug_filepair(const struct diff_filepair *p, int i)
5936{
5937 diff_debug_filespec(p->one, i, "one");
5938 diff_debug_filespec(p->two, i, "two");
Linus Torvalds64479712007-10-25 11:20:56 -07005939 fprintf(stderr, "score %d, status %c rename_used %d broken %d\n",
Junio C Hamano6973dca2006-04-21 23:57:45 -07005940 p->score, p->status ? p->status : '?',
Linus Torvalds64479712007-10-25 11:20:56 -07005941 p->one->rename_used, p->broken_pair);
Junio C Hamano6973dca2006-04-21 23:57:45 -07005942}
5943
5944void diff_debug_queue(const char *msg, struct diff_queue_struct *q)
5945{
5946 int i;
5947 if (msg)
5948 fprintf(stderr, "%s\n", msg);
5949 fprintf(stderr, "q->nr = %d\n", q->nr);
5950 for (i = 0; i < q->nr; i++) {
5951 struct diff_filepair *p = q->queue[i];
5952 diff_debug_filepair(p, i);
5953 }
5954}
5955#endif
5956
5957static void diff_resolve_rename_copy(void)
5958{
Linus Torvalds64479712007-10-25 11:20:56 -07005959 int i;
5960 struct diff_filepair *p;
Junio C Hamano6973dca2006-04-21 23:57:45 -07005961 struct diff_queue_struct *q = &diff_queued_diff;
5962
5963 diff_debug_queue("resolve-rename-copy", q);
5964
5965 for (i = 0; i < q->nr; i++) {
5966 p = q->queue[i];
5967 p->status = 0; /* undecided */
5968 if (DIFF_PAIR_UNMERGED(p))
5969 p->status = DIFF_STATUS_UNMERGED;
5970 else if (!DIFF_FILE_VALID(p->one))
5971 p->status = DIFF_STATUS_ADDED;
5972 else if (!DIFF_FILE_VALID(p->two))
5973 p->status = DIFF_STATUS_DELETED;
5974 else if (DIFF_PAIR_TYPE_CHANGED(p))
5975 p->status = DIFF_STATUS_TYPE_CHANGED;
5976
5977 /* from this point on, we are dealing with a pair
5978 * whose both sides are valid and of the same type, i.e.
5979 * either in-place edit or rename/copy edit.
5980 */
5981 else if (DIFF_PAIR_RENAME(p)) {
Linus Torvalds64479712007-10-25 11:20:56 -07005982 /*
5983 * A rename might have re-connected a broken
5984 * pair up, causing the pathnames to be the
5985 * same again. If so, that's not a rename at
5986 * all, just a modification..
5987 *
5988 * Otherwise, see if this source was used for
5989 * multiple renames, in which case we decrement
5990 * the count, and call it a copy.
Junio C Hamano6973dca2006-04-21 23:57:45 -07005991 */
Linus Torvalds64479712007-10-25 11:20:56 -07005992 if (!strcmp(p->one->path, p->two->path))
5993 p->status = DIFF_STATUS_MODIFIED;
5994 else if (--p->one->rename_used > 0)
Junio C Hamano6973dca2006-04-21 23:57:45 -07005995 p->status = DIFF_STATUS_COPIED;
Linus Torvalds64479712007-10-25 11:20:56 -07005996 else
Junio C Hamano6973dca2006-04-21 23:57:45 -07005997 p->status = DIFF_STATUS_RENAMED;
5998 }
Jeff King9001dc22018-08-28 17:22:48 -04005999 else if (!oideq(&p->one->oid, &p->two->oid) ||
Johannes Schindelind516c2d2007-02-22 21:50:10 +01006000 p->one->mode != p->two->mode ||
Jens Lehmann85adbf22010-03-12 22:23:52 +01006001 p->one->dirty_submodule ||
6002 p->two->dirty_submodule ||
brian m. carlsona0d12c42016-06-24 23:09:23 +00006003 is_null_oid(&p->one->oid))
Junio C Hamano6973dca2006-04-21 23:57:45 -07006004 p->status = DIFF_STATUS_MODIFIED;
6005 else {
6006 /* This is a "no-change" entry and should not
6007 * happen anymore, but prepare for broken callers.
6008 */
6009 error("feeding unmodified %s to diffcore",
6010 p->one->path);
6011 p->status = DIFF_STATUS_UNKNOWN;
6012 }
6013 }
6014 diff_debug_queue("resolve-rename-copy done", q);
6015}
6016
Timo Hirvonenc6744342006-06-24 20:21:53 +03006017static int check_pair_status(struct diff_filepair *p)
Junio C Hamano6973dca2006-04-21 23:57:45 -07006018{
Junio C Hamano6973dca2006-04-21 23:57:45 -07006019 switch (p->status) {
6020 case DIFF_STATUS_UNKNOWN:
Timo Hirvonenc6744342006-06-24 20:21:53 +03006021 return 0;
Junio C Hamano6973dca2006-04-21 23:57:45 -07006022 case 0:
6023 die("internal error in diff-resolve-rename-copy");
Junio C Hamano6973dca2006-04-21 23:57:45 -07006024 default:
Timo Hirvonenc6744342006-06-24 20:21:53 +03006025 return 1;
Junio C Hamano6973dca2006-04-21 23:57:45 -07006026 }
6027}
6028
Timo Hirvonenc6744342006-06-24 20:21:53 +03006029static void flush_one_pair(struct diff_filepair *p, struct diff_options *opt)
6030{
6031 int fmt = opt->output_format;
6032
6033 if (fmt & DIFF_FORMAT_CHECKDIFF)
6034 diff_flush_checkdiff(p, opt);
6035 else if (fmt & (DIFF_FORMAT_RAW | DIFF_FORMAT_NAME_STATUS))
6036 diff_flush_raw(p, opt);
Junio C Hamanocd676a52008-02-12 14:26:02 -08006037 else if (fmt & DIFF_FORMAT_NAME) {
6038 const char *name_a, *name_b;
6039 name_a = p->two->path;
6040 name_b = NULL;
6041 strip_prefix(opt->prefix_length, &name_a, &name_b);
Jeff Kingf5022b52017-02-08 15:31:15 -05006042 fprintf(opt->file, "%s", diff_line_prefix(opt));
Daniel Barkalowc0c77732008-03-09 22:43:39 -04006043 write_name_quoted(name_a, opt->file, opt->line_termination);
Junio C Hamanocd676a52008-02-12 14:26:02 -08006044 }
Timo Hirvonenc6744342006-06-24 20:21:53 +03006045}
6046
Stefan Beller146fdb02017-06-29 17:07:05 -07006047static void show_file_mode_name(struct diff_options *opt, const char *newdelete, struct diff_filespec *fs)
Sean4bbd2612006-05-14 08:13:49 -04006048{
Stefan Beller146fdb02017-06-29 17:07:05 -07006049 struct strbuf sb = STRBUF_INIT;
Sean4bbd2612006-05-14 08:13:49 -04006050 if (fs->mode)
Stefan Beller146fdb02017-06-29 17:07:05 -07006051 strbuf_addf(&sb, " %s mode %06o ", newdelete, fs->mode);
Sean4bbd2612006-05-14 08:13:49 -04006052 else
Stefan Beller146fdb02017-06-29 17:07:05 -07006053 strbuf_addf(&sb, " %s ", newdelete);
6054
6055 quote_c_style(fs->path, &sb, NULL, 0);
6056 strbuf_addch(&sb, '\n');
6057 emit_diff_symbol(opt, DIFF_SYMBOL_SUMMARY,
6058 sb.buf, sb.len, 0);
6059 strbuf_release(&sb);
Sean4bbd2612006-05-14 08:13:49 -04006060}
6061
Stefan Beller146fdb02017-06-29 17:07:05 -07006062static void show_mode_change(struct diff_options *opt, struct diff_filepair *p,
6063 int show_name)
Sean4bbd2612006-05-14 08:13:49 -04006064{
6065 if (p->one->mode && p->two->mode && p->one->mode != p->two->mode) {
Stefan Beller146fdb02017-06-29 17:07:05 -07006066 struct strbuf sb = STRBUF_INIT;
6067 strbuf_addf(&sb, " mode change %06o => %06o",
6068 p->one->mode, p->two->mode);
Alexandre Julliard0d26a642007-02-10 15:37:48 +01006069 if (show_name) {
Stefan Beller146fdb02017-06-29 17:07:05 -07006070 strbuf_addch(&sb, ' ');
6071 quote_c_style(p->two->path, &sb, NULL, 0);
Alexandre Julliard0d26a642007-02-10 15:37:48 +01006072 }
Stefan Beller58aaced2017-09-27 15:51:26 -07006073 strbuf_addch(&sb, '\n');
Stefan Beller146fdb02017-06-29 17:07:05 -07006074 emit_diff_symbol(opt, DIFF_SYMBOL_SUMMARY,
6075 sb.buf, sb.len, 0);
6076 strbuf_release(&sb);
Sean4bbd2612006-05-14 08:13:49 -04006077 }
6078}
6079
Stefan Beller146fdb02017-06-29 17:07:05 -07006080static void show_rename_copy(struct diff_options *opt, const char *renamecopy,
6081 struct diff_filepair *p)
Sean4bbd2612006-05-14 08:13:49 -04006082{
Stefan Beller146fdb02017-06-29 17:07:05 -07006083 struct strbuf sb = STRBUF_INIT;
Nguyễn Thái Ngọc Duyc905cbc2018-02-01 20:02:20 +07006084 struct strbuf names = STRBUF_INIT;
6085
6086 pprint_rename(&names, p->one->path, p->two->path);
Stefan Beller146fdb02017-06-29 17:07:05 -07006087 strbuf_addf(&sb, " %s %s (%d%%)\n",
Nguyễn Thái Ngọc Duyc905cbc2018-02-01 20:02:20 +07006088 renamecopy, names.buf, similarity_index(p));
6089 strbuf_release(&names);
Stefan Beller146fdb02017-06-29 17:07:05 -07006090 emit_diff_symbol(opt, DIFF_SYMBOL_SUMMARY,
6091 sb.buf, sb.len, 0);
6092 show_mode_change(opt, p, 0);
Rene Scharfe348eda22017-08-30 19:49:42 +02006093 strbuf_release(&sb);
Sean4bbd2612006-05-14 08:13:49 -04006094}
6095
Bo Yang7be57612010-05-26 15:23:54 +08006096static void diff_summary(struct diff_options *opt, struct diff_filepair *p)
Sean4bbd2612006-05-14 08:13:49 -04006097{
6098 switch(p->status) {
6099 case DIFF_STATUS_DELETED:
Stefan Beller146fdb02017-06-29 17:07:05 -07006100 show_file_mode_name(opt, "delete", p->one);
Sean4bbd2612006-05-14 08:13:49 -04006101 break;
6102 case DIFF_STATUS_ADDED:
Stefan Beller146fdb02017-06-29 17:07:05 -07006103 show_file_mode_name(opt, "create", p->two);
Sean4bbd2612006-05-14 08:13:49 -04006104 break;
6105 case DIFF_STATUS_COPIED:
Stefan Beller146fdb02017-06-29 17:07:05 -07006106 show_rename_copy(opt, "copy", p);
Sean4bbd2612006-05-14 08:13:49 -04006107 break;
6108 case DIFF_STATUS_RENAMED:
Stefan Beller146fdb02017-06-29 17:07:05 -07006109 show_rename_copy(opt, "rename", p);
Sean4bbd2612006-05-14 08:13:49 -04006110 break;
6111 default:
6112 if (p->score) {
Stefan Beller146fdb02017-06-29 17:07:05 -07006113 struct strbuf sb = STRBUF_INIT;
6114 strbuf_addstr(&sb, " rewrite ");
6115 quote_c_style(p->two->path, &sb, NULL, 0);
6116 strbuf_addf(&sb, " (%d%%)\n", similarity_index(p));
6117 emit_diff_symbol(opt, DIFF_SYMBOL_SUMMARY,
6118 sb.buf, sb.len, 0);
Rene Scharfefa842d82017-08-30 19:49:41 +02006119 strbuf_release(&sb);
Pierre Habouzit663af342007-09-20 00:42:15 +02006120 }
Stefan Beller146fdb02017-06-29 17:07:05 -07006121 show_mode_change(opt, p, !p->score);
Sean4bbd2612006-05-14 08:13:49 -04006122 break;
6123 }
6124}
6125
Johannes Schindelinfcb3d0a2006-06-25 03:51:08 +02006126struct patch_id_t {
brian m. carlson36261e42019-08-18 20:04:03 +00006127 git_hash_ctx *ctx;
Johannes Schindelinfcb3d0a2006-06-25 03:51:08 +02006128 int patchlen;
6129};
6130
6131static int remove_space(char *line, int len)
6132{
6133 int i;
Pierre Habouzit663af342007-09-20 00:42:15 +02006134 char *dst = line;
6135 unsigned char c;
Johannes Schindelinfcb3d0a2006-06-25 03:51:08 +02006136
Pierre Habouzit663af342007-09-20 00:42:15 +02006137 for (i = 0; i < len; i++)
6138 if (!isspace((c = line[i])))
6139 *dst++ = c;
Johannes Schindelinfcb3d0a2006-06-25 03:51:08 +02006140
Pierre Habouzit663af342007-09-20 00:42:15 +02006141 return dst - line;
Johannes Schindelinfcb3d0a2006-06-25 03:51:08 +02006142}
6143
brian m. carlson36261e42019-08-18 20:04:03 +00006144void flush_one_hunk(struct object_id *result, git_hash_ctx *ctx)
Stephen Boyda8f68552019-04-26 16:51:57 -07006145{
6146 unsigned char hash[GIT_MAX_RAWSZ];
6147 unsigned short carry = 0;
6148 int i;
6149
brian m. carlson36261e42019-08-18 20:04:03 +00006150 the_hash_algo->final_fn(hash, ctx);
6151 the_hash_algo->init_fn(ctx);
Stephen Boyda8f68552019-04-26 16:51:57 -07006152 /* 20-byte sum, with carry */
brian m. carlson36261e42019-08-18 20:04:03 +00006153 for (i = 0; i < the_hash_algo->rawsz; ++i) {
Stephen Boyda8f68552019-04-26 16:51:57 -07006154 carry += result->hash[i] + hash[i];
6155 result->hash[i] = carry;
6156 carry >>= 8;
6157 }
6158}
6159
Ævar Arnfjörð Bjarmasona8d5eb62021-04-12 19:15:24 +02006160static int patch_id_consume(void *priv, char *line, unsigned long len)
Johannes Schindelinfcb3d0a2006-06-25 03:51:08 +02006161{
6162 struct patch_id_t *data = priv;
6163 int new_len;
6164
René Scharfe82a62012020-08-19 00:08:54 +02006165 if (len > 12 && starts_with(line, "\\ "))
Ævar Arnfjörð Bjarmasona8d5eb62021-04-12 19:15:24 +02006166 return 0;
Johannes Schindelinfcb3d0a2006-06-25 03:51:08 +02006167 new_len = remove_space(line, len);
6168
brian m. carlson36261e42019-08-18 20:04:03 +00006169 the_hash_algo->update_fn(data->ctx, line, new_len);
Johannes Schindelinfcb3d0a2006-06-25 03:51:08 +02006170 data->patchlen += new_len;
Ævar Arnfjörð Bjarmasona8d5eb62021-04-12 19:15:24 +02006171 return 0;
Johannes Schindelinfcb3d0a2006-06-25 03:51:08 +02006172}
6173
brian m. carlson36261e42019-08-18 20:04:03 +00006174static void patch_id_add_string(git_hash_ctx *ctx, const char *str)
Jeff King977db6b2017-03-30 14:26:05 -04006175{
brian m. carlson36261e42019-08-18 20:04:03 +00006176 the_hash_algo->update_fn(ctx, str, strlen(str));
Jeff King977db6b2017-03-30 14:26:05 -04006177}
6178
brian m. carlson36261e42019-08-18 20:04:03 +00006179static void patch_id_add_mode(git_hash_ctx *ctx, unsigned mode)
Jeff King977db6b2017-03-30 14:26:05 -04006180{
6181 /* large enough for 2^32 in octal */
6182 char buf[12];
6183 int len = xsnprintf(buf, sizeof(buf), "%06o", mode);
brian m. carlson36261e42019-08-18 20:04:03 +00006184 the_hash_algo->update_fn(ctx, buf, len);
Jeff King977db6b2017-03-30 14:26:05 -04006185}
6186
Stephen Boyda8f68552019-04-26 16:51:57 -07006187/* returns 0 upon success, and writes result into oid */
6188static 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 +02006189{
6190 struct diff_queue_struct *q = &diff_queued_diff;
6191 int i;
brian m. carlson36261e42019-08-18 20:04:03 +00006192 git_hash_ctx ctx;
Johannes Schindelinfcb3d0a2006-06-25 03:51:08 +02006193 struct patch_id_t data;
Johannes Schindelinfcb3d0a2006-06-25 03:51:08 +02006194
brian m. carlson36261e42019-08-18 20:04:03 +00006195 the_hash_algo->init_fn(&ctx);
Johannes Schindelinfcb3d0a2006-06-25 03:51:08 +02006196 memset(&data, 0, sizeof(struct patch_id_t));
6197 data.ctx = &ctx;
Stephen Boyda8f68552019-04-26 16:51:57 -07006198 oidclr(oid);
Johannes Schindelinfcb3d0a2006-06-25 03:51:08 +02006199
6200 for (i = 0; i < q->nr; i++) {
6201 xpparam_t xpp;
6202 xdemitconf_t xecfg;
Johannes Schindelinfcb3d0a2006-06-25 03:51:08 +02006203 mmfile_t mf1, mf2;
6204 struct diff_filepair *p = q->queue[i];
6205 int len1, len2;
6206
Brian Downing9ccd0a82008-10-25 15:30:37 +02006207 memset(&xpp, 0, sizeof(xpp));
Johannes Schindelin30b25012007-07-04 19:05:46 +01006208 memset(&xecfg, 0, sizeof(xecfg));
Johannes Schindelinfcb3d0a2006-06-25 03:51:08 +02006209 if (p->status == 0)
6210 return error("internal diff status error");
6211 if (p->status == DIFF_STATUS_UNKNOWN)
6212 continue;
6213 if (diff_unmodified_pair(p))
6214 continue;
6215 if ((DIFF_FILE_VALID(p->one) && S_ISDIR(p->one->mode)) ||
6216 (DIFF_FILE_VALID(p->two) && S_ISDIR(p->two->mode)))
6217 continue;
6218 if (DIFF_PAIR_UNMERGED(p))
6219 continue;
6220
Nguyễn Thái Ngọc Duy58bf2a42018-09-21 17:57:31 +02006221 diff_fill_oid_info(p->one, options->repo->index);
6222 diff_fill_oid_info(p->two, options->repo->index);
Johannes Schindelinfcb3d0a2006-06-25 03:51:08 +02006223
Johannes Schindelinfcb3d0a2006-06-25 03:51:08 +02006224 len1 = remove_space(p->one->path, strlen(p->one->path));
6225 len2 = remove_space(p->two->path, strlen(p->two->path));
Jeff King977db6b2017-03-30 14:26:05 -04006226 patch_id_add_string(&ctx, "diff--git");
6227 patch_id_add_string(&ctx, "a/");
brian m. carlson36261e42019-08-18 20:04:03 +00006228 the_hash_algo->update_fn(&ctx, p->one->path, len1);
Jeff King977db6b2017-03-30 14:26:05 -04006229 patch_id_add_string(&ctx, "b/");
brian m. carlson36261e42019-08-18 20:04:03 +00006230 the_hash_algo->update_fn(&ctx, p->two->path, len2);
Jeff King977db6b2017-03-30 14:26:05 -04006231
6232 if (p->one->mode == 0) {
6233 patch_id_add_string(&ctx, "newfilemode");
6234 patch_id_add_mode(&ctx, p->two->mode);
6235 patch_id_add_string(&ctx, "---/dev/null");
6236 patch_id_add_string(&ctx, "+++b/");
brian m. carlson36261e42019-08-18 20:04:03 +00006237 the_hash_algo->update_fn(&ctx, p->two->path, len2);
Jeff King977db6b2017-03-30 14:26:05 -04006238 } else if (p->two->mode == 0) {
6239 patch_id_add_string(&ctx, "deletedfilemode");
6240 patch_id_add_mode(&ctx, p->one->mode);
6241 patch_id_add_string(&ctx, "---a/");
brian m. carlson36261e42019-08-18 20:04:03 +00006242 the_hash_algo->update_fn(&ctx, p->one->path, len1);
Jeff King977db6b2017-03-30 14:26:05 -04006243 patch_id_add_string(&ctx, "+++/dev/null");
6244 } else {
6245 patch_id_add_string(&ctx, "---a/");
brian m. carlson36261e42019-08-18 20:04:03 +00006246 the_hash_algo->update_fn(&ctx, p->one->path, len1);
Jeff King977db6b2017-03-30 14:26:05 -04006247 patch_id_add_string(&ctx, "+++b/");
brian m. carlson36261e42019-08-18 20:04:03 +00006248 the_hash_algo->update_fn(&ctx, p->two->path, len2);
Jeff King977db6b2017-03-30 14:26:05 -04006249 }
Johannes Schindelinfcb3d0a2006-06-25 03:51:08 +02006250
Kevin Willford3e8e32c2016-07-29 12:19:19 -04006251 if (diff_header_only)
6252 continue;
6253
Nguyễn Thái Ngọc Duyb78ea5f2018-09-21 17:57:19 +02006254 if (fill_mmfile(options->repo, &mf1, p->one) < 0 ||
6255 fill_mmfile(options->repo, &mf2, p->two) < 0)
Kevin Willford3e8e32c2016-07-29 12:19:19 -04006256 return error("unable to read files to diff");
6257
Nguyễn Thái Ngọc Duyb78ea5f2018-09-21 17:57:19 +02006258 if (diff_filespec_is_binary(options->repo, p->one) ||
6259 diff_filespec_is_binary(options->repo, p->two)) {
brian m. carlson36261e42019-08-18 20:04:03 +00006260 the_hash_algo->update_fn(&ctx, oid_to_hex(&p->one->oid),
6261 the_hash_algo->hexsz);
6262 the_hash_algo->update_fn(&ctx, oid_to_hex(&p->two->oid),
6263 the_hash_algo->hexsz);
Clemens Buchacher34597c12010-08-15 09:20:43 +02006264 continue;
6265 }
6266
René Scharfe582aa002010-05-02 15:04:41 +02006267 xpp.flags = 0;
Johannes Schindelinfcb3d0a2006-06-25 03:51:08 +02006268 xecfg.ctxlen = 3;
Ævar Arnfjörð Bjarmason5d934602021-04-12 19:15:29 +02006269 xecfg.flags = XDL_EMIT_NO_HUNK_HDR;
6270 if (xdi_diff_outf(&mf1, &mf2, NULL,
Jeff Kingb1357392018-11-02 02:36:36 -04006271 patch_id_consume, &data, &xpp, &xecfg))
Jeff King3efb9882015-09-24 19:12:23 -04006272 return error("unable to generate patch-id diff for %s",
6273 p->one->path);
Stephen Boyda8f68552019-04-26 16:51:57 -07006274
6275 if (stable)
6276 flush_one_hunk(oid, &ctx);
Johannes Schindelinfcb3d0a2006-06-25 03:51:08 +02006277 }
6278
Stephen Boyda8f68552019-04-26 16:51:57 -07006279 if (!stable)
brian m. carlson5951bf42021-04-26 01:02:53 +00006280 the_hash_algo->final_oid_fn(oid, &ctx);
Stephen Boyda8f68552019-04-26 16:51:57 -07006281
Johannes Schindelinfcb3d0a2006-06-25 03:51:08 +02006282 return 0;
6283}
6284
Stephen Boyda8f68552019-04-26 16:51:57 -07006285int 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 +02006286{
6287 struct diff_queue_struct *q = &diff_queued_diff;
6288 int i;
Stephen Boyda8f68552019-04-26 16:51:57 -07006289 int result = diff_get_patch_id(options, oid, diff_header_only, stable);
Johannes Schindelinfcb3d0a2006-06-25 03:51:08 +02006290
6291 for (i = 0; i < q->nr; i++)
6292 diff_free_filepair(q->queue[i]);
6293
6294 free(q->queue);
Bo Yang9ca5df92010-05-06 21:52:27 -07006295 DIFF_QUEUE_CLEAR(q);
Johannes Schindelinfcb3d0a2006-06-25 03:51:08 +02006296
6297 return result;
6298}
6299
Timo Hirvonen946c3782006-06-27 15:09:17 +03006300static int is_summary_empty(const struct diff_queue_struct *q)
6301{
6302 int i;
6303
6304 for (i = 0; i < q->nr; i++) {
6305 const struct diff_filepair *p = q->queue[i];
6306
6307 switch (p->status) {
6308 case DIFF_STATUS_DELETED:
6309 case DIFF_STATUS_ADDED:
6310 case DIFF_STATUS_COPIED:
6311 case DIFF_STATUS_RENAMED:
6312 return 0;
6313 default:
6314 if (p->score)
6315 return 0;
6316 if (p->one->mode && p->two->mode &&
6317 p->one->mode != p->two->mode)
6318 return 0;
6319 break;
6320 }
6321 }
6322 return 1;
6323}
6324
Junio C Hamanof31027c2011-01-06 13:50:06 -08006325static const char rename_limit_warning[] =
Elijah Newren05d2c612021-07-15 00:45:21 +00006326N_("exhaustive rename detection was skipped due to too many files.");
Junio C Hamanof31027c2011-01-06 13:50:06 -08006327
6328static const char degrade_cc_to_c_warning[] =
Vasco Almeidadb424972016-10-17 13:15:29 +00006329N_("only found copies from modified paths due to too many files.");
Junio C Hamanof31027c2011-01-06 13:50:06 -08006330
6331static const char rename_limit_advice[] =
Vasco Almeidadb424972016-10-17 13:15:29 +00006332N_("you may want to set your %s variable to at least "
6333 "%d and retry the command.");
Junio C Hamanof31027c2011-01-06 13:50:06 -08006334
6335void diff_warn_rename_limit(const char *varname, int needed, int degraded_cc)
6336{
Nguyễn Thái Ngọc Duy4e056c92018-01-16 16:23:49 +07006337 fflush(stdout);
Junio C Hamanof31027c2011-01-06 13:50:06 -08006338 if (degraded_cc)
Vasco Almeidadb424972016-10-17 13:15:29 +00006339 warning(_(degrade_cc_to_c_warning));
Junio C Hamanof31027c2011-01-06 13:50:06 -08006340 else if (needed)
Vasco Almeidadb424972016-10-17 13:15:29 +00006341 warning(_(rename_limit_warning));
Junio C Hamanof31027c2011-01-06 13:50:06 -08006342 else
6343 return;
Elijah Newren9f7e4bf2017-11-13 12:15:59 -08006344 if (0 < needed)
Vasco Almeidadb424972016-10-17 13:15:29 +00006345 warning(_(rename_limit_advice), varname, needed);
Junio C Hamanof31027c2011-01-06 13:50:06 -08006346}
6347
Elijah Newren95433ee2022-02-02 02:37:34 +00006348static void create_filepairs_for_header_only_notifications(struct diff_options *o)
6349{
6350 struct strset present;
6351 struct diff_queue_struct *q = &diff_queued_diff;
6352 struct hashmap_iter iter;
6353 struct strmap_entry *e;
6354 int i;
6355
6356 strset_init_with_options(&present, /*pool*/ NULL, /*strdup*/ 0);
6357
6358 /*
6359 * Find out which paths exist in diff_queued_diff, preferring
6360 * one->path for any pair that has multiple paths.
6361 */
6362 for (i = 0; i < q->nr; i++) {
6363 struct diff_filepair *p = q->queue[i];
6364 char *path = p->one->path ? p->one->path : p->two->path;
6365
6366 if (strmap_contains(o->additional_path_headers, path))
6367 strset_add(&present, path);
6368 }
6369
6370 /*
6371 * Loop over paths in additional_path_headers; for each NOT already
6372 * in diff_queued_diff, create a synthetic filepair and insert that
6373 * into diff_queued_diff.
6374 */
6375 strmap_for_each_entry(o->additional_path_headers, &iter, e) {
6376 if (!strset_contains(&present, e->key)) {
6377 struct diff_filespec *one, *two;
6378 struct diff_filepair *p;
6379
6380 one = alloc_filespec(e->key);
6381 two = alloc_filespec(e->key);
6382 fill_filespec(one, null_oid(), 0, 0);
6383 fill_filespec(two, null_oid(), 0, 0);
6384 p = diff_queue(q, one, two);
6385 p->status = DIFF_STATUS_MODIFIED;
6386 }
6387 }
6388
6389 /* Re-sort the filepairs */
6390 diffcore_fix_diff_index();
6391
6392 /* Cleanup */
6393 strset_clear(&present);
6394}
6395
Stefan Bellerec331502017-06-29 17:06:48 -07006396static void diff_flush_patch_all_file_pairs(struct diff_options *o)
6397{
6398 int i;
Stefan Bellere6e045f2017-06-29 17:07:06 -07006399 static struct emitted_diff_symbols esm = EMITTED_DIFF_SYMBOLS_INIT;
Stefan Bellerec331502017-06-29 17:06:48 -07006400 struct diff_queue_struct *q = &diff_queued_diff;
Stefan Beller091f8e22017-06-29 17:06:53 -07006401
6402 if (WSEH_NEW & WS_RULE_MASK)
Johannes Schindelin033abf92018-05-02 11:38:39 +02006403 BUG("WS rules bit mask overlaps with diff symbol flags");
Stefan Beller091f8e22017-06-29 17:06:53 -07006404
Stefan Beller2e2d5ac2017-06-30 13:53:07 -07006405 if (o->color_moved)
6406 o->emitted_symbols = &esm;
Stefan Bellere6e045f2017-06-29 17:07:06 -07006407
Elijah Newren95433ee2022-02-02 02:37:34 +00006408 if (o->additional_path_headers)
6409 create_filepairs_for_header_only_notifications(o);
6410
Stefan Bellerec331502017-06-29 17:06:48 -07006411 for (i = 0; i < q->nr; i++) {
6412 struct diff_filepair *p = q->queue[i];
6413 if (check_pair_status(p))
6414 diff_flush_patch(p, o);
6415 }
Stefan Bellere6e045f2017-06-29 17:07:06 -07006416
6417 if (o->emitted_symbols) {
Stefan Beller2e2d5ac2017-06-30 13:53:07 -07006418 if (o->color_moved) {
Phillip Wood72962e82021-12-09 10:30:09 +00006419 struct mem_pool entry_pool;
6420 struct moved_entry_list *entry_list;
Stefan Beller2e2d5ac2017-06-30 13:53:07 -07006421
Phillip Wood72962e82021-12-09 10:30:09 +00006422 mem_pool_init(&entry_pool, 1024 * 1024);
6423 entry_list = add_lines_to_move_detection(o,
6424 &entry_pool);
6425 mark_color_as_moved(o, entry_list);
Stefan Beller86b452e2017-06-30 13:53:09 -07006426 if (o->color_moved == COLOR_MOVED_ZEBRA_DIM)
6427 dim_moved_lines(o);
Stefan Beller2e2d5ac2017-06-30 13:53:07 -07006428
Phillip Wood72962e82021-12-09 10:30:09 +00006429 mem_pool_discard(&entry_pool, 0);
6430 free(entry_list);
Stefan Beller2e2d5ac2017-06-30 13:53:07 -07006431 }
6432
Stefan Bellere6e045f2017-06-29 17:07:06 -07006433 for (i = 0; i < esm.nr; i++)
6434 emit_diff_symbol_from_struct(o, &esm.buf[i]);
6435
6436 for (i = 0; i < esm.nr; i++)
6437 free((void *)esm.buf[i].line);
Jeff King48edf3a2019-01-24 07:32:41 -05006438 esm.nr = 0;
6439
6440 o->emitted_symbols = NULL;
Stefan Bellere6e045f2017-06-29 17:07:06 -07006441 }
Stefan Bellerec331502017-06-29 17:06:48 -07006442}
6443
Ævar Arnfjörð Bjarmasone900d492021-02-11 11:45:34 +01006444static void diff_free_file(struct diff_options *options)
6445{
6446 if (options->close_file)
6447 fclose(options->file);
6448}
6449
Ævar Arnfjörð Bjarmasonc45dc9c2021-02-11 11:45:35 +01006450static void diff_free_ignore_regex(struct diff_options *options)
6451{
6452 int i;
6453
6454 for (i = 0; i < options->ignore_regex_nr; i++) {
6455 regfree(options->ignore_regex[i]);
6456 free(options->ignore_regex[i]);
6457 }
6458 free(options->ignore_regex);
6459}
6460
Ævar Arnfjörð Bjarmasone900d492021-02-11 11:45:34 +01006461void diff_free(struct diff_options *options)
6462{
6463 if (options->no_free)
6464 return;
6465
6466 diff_free_file(options);
Ævar Arnfjörð Bjarmasonc45dc9c2021-02-11 11:45:35 +01006467 diff_free_ignore_regex(options);
Ævar Arnfjörð Bjarmason244c2722022-02-16 11:56:28 +01006468 clear_pathspec(&options->pathspec);
Ævar Arnfjörð Bjarmason6ee36362022-02-16 11:56:29 +01006469 FREE_AND_NULL(options->parseopts);
Ævar Arnfjörð Bjarmasone900d492021-02-11 11:45:34 +01006470}
6471
Junio C Hamano6973dca2006-04-21 23:57:45 -07006472void diff_flush(struct diff_options *options)
6473{
6474 struct diff_queue_struct *q = &diff_queued_diff;
Timo Hirvonenc6744342006-06-24 20:21:53 +03006475 int i, output_format = options->output_format;
Timo Hirvonen946c3782006-06-27 15:09:17 +03006476 int separator = 0;
Johan Herland1c57a622011-04-29 11:36:21 +02006477 int dirstat_by_line = 0;
Junio C Hamano6973dca2006-04-21 23:57:45 -07006478
Timo Hirvonenc6744342006-06-24 20:21:53 +03006479 /*
6480 * Order: raw, stat, summary, patch
6481 * or: name/name-status/checkdiff (other bits clear)
6482 */
Elijah Newren95433ee2022-02-02 02:37:34 +00006483 if (!q->nr && !options->additional_path_headers)
Timo Hirvonen946c3782006-06-27 15:09:17 +03006484 goto free_queue;
Junio C Hamano6973dca2006-04-21 23:57:45 -07006485
Timo Hirvonenc6744342006-06-24 20:21:53 +03006486 if (output_format & (DIFF_FORMAT_RAW |
6487 DIFF_FORMAT_NAME |
6488 DIFF_FORMAT_NAME_STATUS |
6489 DIFF_FORMAT_CHECKDIFF)) {
Junio C Hamano6973dca2006-04-21 23:57:45 -07006490 for (i = 0; i < q->nr; i++) {
6491 struct diff_filepair *p = q->queue[i];
Timo Hirvonenc6744342006-06-24 20:21:53 +03006492 if (check_pair_status(p))
6493 flush_one_pair(p, options);
Junio C Hamano6973dca2006-04-21 23:57:45 -07006494 }
Timo Hirvonen946c3782006-06-27 15:09:17 +03006495 separator++;
Junio C Hamano6973dca2006-04-21 23:57:45 -07006496 }
Timo Hirvonenc6744342006-06-24 20:21:53 +03006497
Brandon Williams0d1e0e72017-10-31 11:19:11 -07006498 if (output_format & DIFF_FORMAT_DIRSTAT && options->flags.dirstat_by_line)
Johan Herland1c57a622011-04-29 11:36:21 +02006499 dirstat_by_line = 1;
6500
6501 if (output_format & (DIFF_FORMAT_DIFFSTAT|DIFF_FORMAT_SHORTSTAT|DIFF_FORMAT_NUMSTAT) ||
6502 dirstat_by_line) {
Timo Hirvonen5e2b0632006-06-25 14:28:19 +03006503 struct diffstat_t diffstat;
Timo Hirvonenc6744342006-06-24 20:21:53 +03006504
Daniel Ferreirae4cb6592019-11-13 12:40:58 +00006505 compute_diffstat(options, &diffstat, q);
Junio C Hamano74e2abe2006-10-12 03:01:00 -07006506 if (output_format & DIFF_FORMAT_NUMSTAT)
6507 show_numstat(&diffstat, options);
6508 if (output_format & DIFF_FORMAT_DIFFSTAT)
6509 show_stats(&diffstat, options);
Junio C Hamanof6046522007-12-11 23:46:30 -08006510 if (output_format & DIFF_FORMAT_SHORTSTAT)
Daniel Barkalowc0c77732008-03-09 22:43:39 -04006511 show_shortstats(&diffstat, options);
Mårten Kongstadab273892015-03-02 16:05:39 +01006512 if (output_format & DIFF_FORMAT_DIRSTAT && dirstat_by_line)
Johan Herland1c57a622011-04-29 11:36:21 +02006513 show_dirstat_by_line(&diffstat, options);
Junio C Hamanof6046522007-12-11 23:46:30 -08006514 free_diffstat_info(&diffstat);
Junio C Hamano3969cf72006-06-27 15:08:19 -07006515 separator++;
Junio C Hamano6973dca2006-04-21 23:57:45 -07006516 }
Johan Herland1c57a622011-04-29 11:36:21 +02006517 if ((output_format & DIFF_FORMAT_DIRSTAT) && !dirstat_by_line)
Junio C Hamanoc04a7152008-02-12 17:06:58 -08006518 show_dirstat(options);
Junio C Hamano6973dca2006-04-21 23:57:45 -07006519
Timo Hirvonen946c3782006-06-27 15:09:17 +03006520 if (output_format & DIFF_FORMAT_SUMMARY && !is_summary_empty(q)) {
Bo Yang7be57612010-05-26 15:23:54 +08006521 for (i = 0; i < q->nr; i++) {
6522 diff_summary(options, q->queue[i]);
6523 }
Junio C Hamano3969cf72006-06-27 15:08:19 -07006524 separator++;
Sean4bbd2612006-05-14 08:13:49 -04006525 }
6526
Larry D'Anna6977c252010-02-16 01:55:21 -05006527 if (output_format & DIFF_FORMAT_NO_OUTPUT &&
Brandon Williams0d1e0e72017-10-31 11:19:11 -07006528 options->flags.exit_with_status &&
6529 options->flags.diff_from_contents) {
Larry D'Anna6977c252010-02-16 01:55:21 -05006530 /*
6531 * run diff_flush_patch for the exit status. setting
Ondřej Bílka749f7632013-07-22 23:02:23 +02006532 * options->file to /dev/null should be safe, because we
Larry D'Anna6977c252010-02-16 01:55:21 -05006533 * aren't supposed to produce any output anyway.
6534 */
Ævar Arnfjörð Bjarmasone900d492021-02-11 11:45:34 +01006535 diff_free_file(options);
Nguyễn Thái Ngọc Duy23a9e072017-05-03 17:16:46 +07006536 options->file = xfopen("/dev/null", "w");
Larry D'Anna6977c252010-02-16 01:55:21 -05006537 options->close_file = 1;
Stefan Beller2e2d5ac2017-06-30 13:53:07 -07006538 options->color_moved = 0;
Larry D'Anna6977c252010-02-16 01:55:21 -05006539 for (i = 0; i < q->nr; i++) {
6540 struct diff_filepair *p = q->queue[i];
6541 if (check_pair_status(p))
6542 diff_flush_patch(p, options);
6543 if (options->found_changes)
6544 break;
6545 }
6546 }
6547
Timo Hirvonenc6744342006-06-24 20:21:53 +03006548 if (output_format & DIFF_FORMAT_PATCH) {
Timo Hirvonen946c3782006-06-27 15:09:17 +03006549 if (separator) {
Stefan Beller091f8e22017-06-29 17:06:53 -07006550 emit_diff_symbol(options, DIFF_SYMBOL_SEPARATOR, NULL, 0, 0);
Stefan Beller30b7e1e2017-06-29 17:07:04 -07006551 if (options->stat_sep)
Timo Hirvonen946c3782006-06-27 15:09:17 +03006552 /* attach patch instead of inline */
Stefan Beller30b7e1e2017-06-29 17:07:04 -07006553 emit_diff_symbol(options, DIFF_SYMBOL_STAT_SEP,
6554 NULL, 0, 0);
Timo Hirvonenc6744342006-06-24 20:21:53 +03006555 }
6556
Stefan Bellerec331502017-06-29 17:06:48 -07006557 diff_flush_patch_all_file_pairs(options);
Timo Hirvonenc6744342006-06-24 20:21:53 +03006558 }
6559
Jeff King04245582006-09-07 02:35:42 -04006560 if (output_format & DIFF_FORMAT_CALLBACK)
6561 options->format_callback(q, options, options->format_callback_data);
6562
Timo Hirvonenc6744342006-06-24 20:21:53 +03006563 for (i = 0; i < q->nr; i++)
6564 diff_free_filepair(q->queue[i]);
Timo Hirvonen946c3782006-06-27 15:09:17 +03006565free_queue:
Junio C Hamano6973dca2006-04-21 23:57:45 -07006566 free(q->queue);
Bo Yang9ca5df92010-05-06 21:52:27 -07006567 DIFF_QUEUE_CLEAR(q);
Ævar Arnfjörð Bjarmasone900d492021-02-11 11:45:34 +01006568 diff_free(options);
Junio C Hamanof2451942009-05-22 12:45:29 -07006569
6570 /*
Jim Meyering97bf2a02009-08-30 22:27:02 +02006571 * Report the content-level differences with HAS_CHANGES;
Junio C Hamanof2451942009-05-22 12:45:29 -07006572 * diff_addremove/diff_change does not set the bit when
6573 * DIFF_FROM_CONTENTS is in effect (e.g. with -w).
6574 */
Brandon Williams0d1e0e72017-10-31 11:19:11 -07006575 if (options->flags.diff_from_contents) {
Junio C Hamanof2451942009-05-22 12:45:29 -07006576 if (options->found_changes)
Brandon Williams0d1e0e72017-10-31 11:19:11 -07006577 options->flags.has_changes = 1;
Junio C Hamanof2451942009-05-22 12:45:29 -07006578 else
Brandon Williams0d1e0e72017-10-31 11:19:11 -07006579 options->flags.has_changes = 0;
Junio C Hamanof2451942009-05-22 12:45:29 -07006580 }
Junio C Hamano6973dca2006-04-21 23:57:45 -07006581}
6582
Junio C Hamano08578fa2013-07-17 15:09:34 -07006583static int match_filter(const struct diff_options *options, const struct diff_filepair *p)
6584{
6585 return (((p->status == DIFF_STATUS_MODIFIED) &&
6586 ((p->score &&
Junio C Hamano1ecc1cb2013-07-17 15:05:46 -07006587 filter_bit_tst(DIFF_STATUS_FILTER_BROKEN, options)) ||
Junio C Hamano08578fa2013-07-17 15:09:34 -07006588 (!p->score &&
Junio C Hamano1ecc1cb2013-07-17 15:05:46 -07006589 filter_bit_tst(DIFF_STATUS_MODIFIED, options)))) ||
Junio C Hamano08578fa2013-07-17 15:09:34 -07006590 ((p->status != DIFF_STATUS_MODIFIED) &&
Junio C Hamano1ecc1cb2013-07-17 15:05:46 -07006591 filter_bit_tst(p->status, options)));
Junio C Hamano08578fa2013-07-17 15:09:34 -07006592}
6593
Junio C Hamano949226f2013-07-17 14:19:24 -07006594static void diffcore_apply_filter(struct diff_options *options)
Junio C Hamano6973dca2006-04-21 23:57:45 -07006595{
6596 int i;
6597 struct diff_queue_struct *q = &diff_queued_diff;
6598 struct diff_queue_struct outq;
Junio C Hamano949226f2013-07-17 14:19:24 -07006599
Bo Yang9ca5df92010-05-06 21:52:27 -07006600 DIFF_QUEUE_CLEAR(&outq);
Junio C Hamano6973dca2006-04-21 23:57:45 -07006601
Junio C Hamano1ecc1cb2013-07-17 15:05:46 -07006602 if (!options->filter)
Junio C Hamano6973dca2006-04-21 23:57:45 -07006603 return;
6604
Junio C Hamano1ecc1cb2013-07-17 15:05:46 -07006605 if (filter_bit_tst(DIFF_STATUS_FILTER_AON, options)) {
Junio C Hamano6973dca2006-04-21 23:57:45 -07006606 int found;
6607 for (i = found = 0; !found && i < q->nr; i++) {
Junio C Hamano08578fa2013-07-17 15:09:34 -07006608 if (match_filter(options, q->queue[i]))
Junio C Hamano6973dca2006-04-21 23:57:45 -07006609 found++;
6610 }
6611 if (found)
6612 return;
6613
6614 /* otherwise we will clear the whole queue
6615 * by copying the empty outq at the end of this
6616 * function, but first clear the current entries
6617 * in the queue.
6618 */
6619 for (i = 0; i < q->nr; i++)
6620 diff_free_filepair(q->queue[i]);
6621 }
6622 else {
6623 /* Only the matching ones */
6624 for (i = 0; i < q->nr; i++) {
6625 struct diff_filepair *p = q->queue[i];
Junio C Hamano08578fa2013-07-17 15:09:34 -07006626 if (match_filter(options, p))
Junio C Hamano6973dca2006-04-21 23:57:45 -07006627 diff_q(&outq, p);
6628 else
6629 diff_free_filepair(p);
6630 }
6631 }
6632 free(q->queue);
6633 *q = outq;
6634}
6635
Sven Verdoolaege57011152007-09-08 12:30:22 +02006636/* Check whether two filespecs with the same mode and size are identical */
Nguyễn Thái Ngọc Duyb78ea5f2018-09-21 17:57:19 +02006637static int diff_filespec_is_identical(struct repository *r,
6638 struct diff_filespec *one,
Sven Verdoolaege57011152007-09-08 12:30:22 +02006639 struct diff_filespec *two)
6640{
Junio C Hamano2b459b42008-03-02 00:07:59 -08006641 if (S_ISGITLINK(one->mode))
6642 return 0;
Jonathan Tan1c37e862020-04-07 15:11:41 -07006643 if (diff_populate_filespec(r, one, NULL))
Sven Verdoolaege57011152007-09-08 12:30:22 +02006644 return 0;
Jonathan Tan1c37e862020-04-07 15:11:41 -07006645 if (diff_populate_filespec(r, two, NULL))
Sven Verdoolaege57011152007-09-08 12:30:22 +02006646 return 0;
6647 return !memcmp(one->data, two->data, one->size);
6648}
6649
Nguyễn Thái Ngọc Duyb78ea5f2018-09-21 17:57:19 +02006650static int diff_filespec_check_stat_unmatch(struct repository *r,
6651 struct diff_filepair *p)
Nguyễn Thái Ngọc Duyfceb9072014-01-25 13:46:49 +07006652{
Jonathan Tan1c37e862020-04-07 15:11:41 -07006653 struct diff_populate_filespec_options dpf_options = {
6654 .check_size_only = 1,
Jonathan Tan95acf112020-04-07 15:11:43 -07006655 .missing_object_cb = diff_queued_diff_prefetch,
6656 .missing_object_data = r,
Jonathan Tan1c37e862020-04-07 15:11:41 -07006657 };
6658
Nguyễn Thái Ngọc Duyf34b2052014-01-25 13:46:50 +07006659 if (p->done_skip_stat_unmatch)
6660 return p->skip_stat_unmatch_result;
6661
6662 p->done_skip_stat_unmatch = 1;
6663 p->skip_stat_unmatch_result = 0;
Nguyễn Thái Ngọc Duyfceb9072014-01-25 13:46:49 +07006664 /*
6665 * 1. Entries that come from stat info dirtiness
6666 * always have both sides (iow, not create/delete),
6667 * one side of the object name is unknown, with
6668 * the same mode and size. Keep the ones that
6669 * do not match these criteria. They have real
6670 * differences.
6671 *
6672 * 2. At this point, the file is known to be modified,
6673 * with the same mode and size, and the object
6674 * name of one side is unknown. Need to inspect
6675 * the identical contents.
6676 */
6677 if (!DIFF_FILE_VALID(p->one) || /* (1) */
6678 !DIFF_FILE_VALID(p->two) ||
brian m. carlson41c95602016-06-24 23:09:24 +00006679 (p->one->oid_valid && p->two->oid_valid) ||
Nguyễn Thái Ngọc Duyfceb9072014-01-25 13:46:49 +07006680 (p->one->mode != p->two->mode) ||
Jonathan Tan1c37e862020-04-07 15:11:41 -07006681 diff_populate_filespec(r, p->one, &dpf_options) ||
6682 diff_populate_filespec(r, p->two, &dpf_options) ||
Nguyễn Thái Ngọc Duyfceb9072014-01-25 13:46:49 +07006683 (p->one->size != p->two->size) ||
Nguyễn Thái Ngọc Duyb78ea5f2018-09-21 17:57:19 +02006684 !diff_filespec_is_identical(r, p->one, p->two)) /* (2) */
Nguyễn Thái Ngọc Duyf34b2052014-01-25 13:46:50 +07006685 p->skip_stat_unmatch_result = 1;
6686 return p->skip_stat_unmatch_result;
Nguyễn Thái Ngọc Duyfceb9072014-01-25 13:46:49 +07006687}
6688
Junio C Hamanofb132272007-08-03 13:33:31 -07006689static void diffcore_skip_stat_unmatch(struct diff_options *diffopt)
6690{
6691 int i;
6692 struct diff_queue_struct *q = &diff_queued_diff;
6693 struct diff_queue_struct outq;
Bo Yang9ca5df92010-05-06 21:52:27 -07006694 DIFF_QUEUE_CLEAR(&outq);
Junio C Hamanofb132272007-08-03 13:33:31 -07006695
6696 for (i = 0; i < q->nr; i++) {
6697 struct diff_filepair *p = q->queue[i];
6698
Nguyễn Thái Ngọc Duyb78ea5f2018-09-21 17:57:19 +02006699 if (diff_filespec_check_stat_unmatch(diffopt->repo, p))
Junio C Hamanofb132272007-08-03 13:33:31 -07006700 diff_q(&outq, p);
6701 else {
6702 /*
6703 * The caller can subtract 1 from skip_stat_unmatch
6704 * to determine how many paths were dirty only
6705 * due to stat info mismatch.
6706 */
Brandon Williams0d1e0e72017-10-31 11:19:11 -07006707 if (!diffopt->flags.no_index)
René Scharfe6d2d9e82007-08-15 00:41:00 +02006708 diffopt->skip_stat_unmatch++;
Junio C Hamanofb132272007-08-03 13:33:31 -07006709 diff_free_filepair(p);
6710 }
6711 }
6712 free(q->queue);
6713 *q = outq;
6714}
6715
Junio C Hamano730f7282009-09-20 00:03:39 -07006716static int diffnamecmp(const void *a_, const void *b_)
6717{
6718 const struct diff_filepair *a = *((const struct diff_filepair **)a_);
6719 const struct diff_filepair *b = *((const struct diff_filepair **)b_);
6720 const char *name_a, *name_b;
6721
6722 name_a = a->one ? a->one->path : a->two->path;
6723 name_b = b->one ? b->one->path : b->two->path;
6724 return strcmp(name_a, name_b);
6725}
6726
Jeff King784c0da2019-02-14 00:48:03 -05006727void diffcore_fix_diff_index(void)
Junio C Hamano730f7282009-09-20 00:03:39 -07006728{
6729 struct diff_queue_struct *q = &diff_queued_diff;
René Scharfe9ed0d8d2016-09-29 17:27:31 +02006730 QSORT(q->queue, q->nr, diffnamecmp);
Junio C Hamano730f7282009-09-20 00:03:39 -07006731}
6732
Jonathan Tan95acf112020-04-07 15:11:43 -07006733void diff_add_if_missing(struct repository *r,
6734 struct oid_array *to_fetch,
6735 const struct diff_filespec *filespec)
Jonathan Tan7fbbcb22019-04-05 10:09:34 -07006736{
6737 if (filespec && filespec->oid_valid &&
Jonathan Tana63694f2019-08-20 13:53:20 -07006738 !S_ISGITLINK(filespec->mode) &&
Jonathan Tan7fbbcb22019-04-05 10:09:34 -07006739 oid_object_info_extended(r, &filespec->oid, NULL,
6740 OBJECT_INFO_FOR_PREFETCH))
6741 oid_array_append(to_fetch, &filespec->oid);
6742}
6743
Jonathan Tan95acf112020-04-07 15:11:43 -07006744void diff_queued_diff_prefetch(void *repository)
6745{
6746 struct repository *repo = repository;
6747 int i;
6748 struct diff_queue_struct *q = &diff_queued_diff;
6749 struct oid_array to_fetch = OID_ARRAY_INIT;
6750
6751 for (i = 0; i < q->nr; i++) {
6752 struct diff_filepair *p = q->queue[i];
6753 diff_add_if_missing(repo, &to_fetch, p->one);
6754 diff_add_if_missing(repo, &to_fetch, p->two);
6755 }
6756
6757 /*
6758 * NEEDSWORK: Consider deduplicating the OIDs sent.
6759 */
6760 promisor_remote_get_direct(repo, to_fetch.oid, to_fetch.nr);
6761
6762 oid_array_clear(&to_fetch);
6763}
6764
Junio C Hamano6973dca2006-04-21 23:57:45 -07006765void diffcore_std(struct diff_options *options)
6766{
Jonathan Tan95acf112020-04-07 15:11:43 -07006767 int output_formats_to_prefetch = DIFF_FORMAT_DIFFSTAT |
6768 DIFF_FORMAT_NUMSTAT |
6769 DIFF_FORMAT_PATCH |
6770 DIFF_FORMAT_SHORTSTAT |
6771 DIFF_FORMAT_DIRSTAT;
Jonathan Tan7fbbcb22019-04-05 10:09:34 -07006772
Jonathan Tan95acf112020-04-07 15:11:43 -07006773 /*
6774 * Check if the user requested a blob-data-requiring diff output and/or
6775 * break-rewrite detection (which requires blob data). If yes, prefetch
6776 * the diff pairs.
6777 *
6778 * If no prefetching occurs, diffcore_rename() will prefetch if it
6779 * decides that it needs inexact rename detection.
6780 */
6781 if (options->repo == the_repository && has_promisor_remote() &&
6782 (options->output_format & output_formats_to_prefetch ||
6783 options->pickaxe_opts & DIFF_PICKAXE_KINDS_MASK))
6784 diff_queued_diff_prefetch(options->repo);
Jonathan Tan7fbbcb22019-04-05 10:09:34 -07006785
Kirill Smelkov7195fbf2014-02-24 20:21:51 +04006786 /* NOTE please keep the following in sync with diff_tree_combined() */
Junio C Hamano9d865352008-09-06 19:09:16 -07006787 if (options->skip_stat_unmatch)
Junio C Hamanofb132272007-08-03 13:33:31 -07006788 diffcore_skip_stat_unmatch(options);
Junio C Hamano44c48a92010-08-13 12:17:45 -07006789 if (!options->found_follow) {
6790 /* See try_to_follow_renames() in tree-diff.c */
6791 if (options->break_opt != -1)
Nguyễn Thái Ngọc Duyb78ea5f2018-09-21 17:57:19 +02006792 diffcore_break(options->repo,
6793 options->break_opt);
Junio C Hamano44c48a92010-08-13 12:17:45 -07006794 if (options->detect_rename)
6795 diffcore_rename(options);
6796 if (options->break_opt != -1)
6797 diffcore_merge_broken();
6798 }
Stefan Bellercf630512018-01-04 14:50:41 -08006799 if (options->pickaxe_opts & DIFF_PICKAXE_KINDS_MASK)
Junio C Hamano382f0132010-08-31 13:44:39 -07006800 diffcore_pickaxe(options);
Junio C Hamano6973dca2006-04-21 23:57:45 -07006801 if (options->orderfile)
6802 diffcore_order(options->orderfile);
Junio C Hamano1eb41362021-02-11 11:57:50 -08006803 if (options->rotate_to)
6804 diffcore_rotate(options);
Junio C Hamano44c48a92010-08-13 12:17:45 -07006805 if (!options->found_follow)
6806 /* See try_to_follow_renames() in tree-diff.c */
6807 diff_resolve_rename_copy();
Junio C Hamano949226f2013-07-17 14:19:24 -07006808 diffcore_apply_filter(options);
Junio C Hamano68aacb22007-03-14 11:12:13 -07006809
Brandon Williams0d1e0e72017-10-31 11:19:11 -07006810 if (diff_queued_diff.nr && !options->flags.diff_from_contents)
6811 options->flags.has_changes = 1;
Pierre Habouzit8f67f8a2007-11-10 20:05:14 +01006812 else
Brandon Williams0d1e0e72017-10-31 11:19:11 -07006813 options->flags.has_changes = 0;
Bo Yang1da61752010-05-06 21:52:28 -07006814
Junio C Hamano44c48a92010-08-13 12:17:45 -07006815 options->found_follow = 0;
Junio C Hamano6973dca2006-04-21 23:57:45 -07006816}
6817
Junio C Hamanoda31b352007-12-13 23:40:27 -08006818int diff_result_code(struct diff_options *opt, int status)
6819{
6820 int result = 0;
Junio C Hamanof31027c2011-01-06 13:50:06 -08006821
Max Nanasyc9fc4412013-03-21 12:53:38 -07006822 diff_warn_rename_limit("diff.renameLimit",
Junio C Hamanof31027c2011-01-06 13:50:06 -08006823 opt->needed_rename_limit,
6824 opt->degraded_cc_to_c);
Brandon Williams0d1e0e72017-10-31 11:19:11 -07006825 if (!opt->flags.exit_with_status &&
Junio C Hamanoda31b352007-12-13 23:40:27 -08006826 !(opt->output_format & DIFF_FORMAT_CHECKDIFF))
6827 return status;
Brandon Williams0d1e0e72017-10-31 11:19:11 -07006828 if (opt->flags.exit_with_status &&
6829 opt->flags.has_changes)
Junio C Hamanoda31b352007-12-13 23:40:27 -08006830 result |= 01;
6831 if ((opt->output_format & DIFF_FORMAT_CHECKDIFF) &&
Brandon Williams0d1e0e72017-10-31 11:19:11 -07006832 opt->flags.check_failed)
Junio C Hamanoda31b352007-12-13 23:40:27 -08006833 result |= 02;
6834 return result;
6835}
Junio C Hamano6973dca2006-04-21 23:57:45 -07006836
Junio C Hamano28b92642011-05-31 09:14:17 -07006837int diff_can_quit_early(struct diff_options *opt)
6838{
Brandon Williams0d1e0e72017-10-31 11:19:11 -07006839 return (opt->flags.quick &&
Junio C Hamano28b92642011-05-31 09:14:17 -07006840 !opt->filter &&
Brandon Williams0d1e0e72017-10-31 11:19:11 -07006841 opt->flags.has_changes);
Junio C Hamano28b92642011-05-31 09:14:17 -07006842}
6843
Jens Lehmannaee9c7d2010-08-06 00:39:25 +02006844/*
6845 * Shall changes to this submodule be ignored?
6846 *
6847 * Submodule changes can be configured to be ignored separately for each path,
6848 * but that configuration can be overridden from the command line.
6849 */
6850static int is_submodule_ignored(const char *path, struct diff_options *options)
6851{
6852 int ignored = 0;
Brandon Williams02f2f562017-10-31 11:19:05 -07006853 struct diff_flags orig_flags = options->flags;
Brandon Williams0d1e0e72017-10-31 11:19:11 -07006854 if (!options->flags.override_submodule_config)
Jens Lehmannaee9c7d2010-08-06 00:39:25 +02006855 set_diffopt_flags_from_submodule_config(options, path);
Brandon Williams0d1e0e72017-10-31 11:19:11 -07006856 if (options->flags.ignore_submodules)
Jens Lehmannaee9c7d2010-08-06 00:39:25 +02006857 ignored = 1;
6858 options->flags = orig_flags;
6859 return ignored;
6860}
6861
Daniel Ferreirae4cb6592019-11-13 12:40:58 +00006862void compute_diffstat(struct diff_options *options,
6863 struct diffstat_t *diffstat,
6864 struct diff_queue_struct *q)
6865{
6866 int i;
6867
6868 memset(diffstat, 0, sizeof(struct diffstat_t));
6869 for (i = 0; i < q->nr; i++) {
6870 struct diff_filepair *p = q->queue[i];
6871 if (check_pair_status(p))
6872 diff_flush_stat(p, options, diffstat);
6873 }
6874}
6875
Junio C Hamano6973dca2006-04-21 23:57:45 -07006876void diff_addremove(struct diff_options *options,
6877 int addremove, unsigned mode,
Brandon Williamsc26022e2017-05-30 10:30:47 -07006878 const struct object_id *oid,
6879 int oid_valid,
Jens Lehmanne3d42c42010-01-18 21:26:18 +01006880 const char *concatpath, unsigned dirty_submodule)
Junio C Hamano6973dca2006-04-21 23:57:45 -07006881{
Junio C Hamano6973dca2006-04-21 23:57:45 -07006882 struct diff_filespec *one, *two;
6883
Jens Lehmannaee9c7d2010-08-06 00:39:25 +02006884 if (S_ISGITLINK(mode) && is_submodule_ignored(concatpath, options))
Johannes Schindelin50fd9bd2008-05-14 18:03:31 +01006885 return;
6886
Junio C Hamano6973dca2006-04-21 23:57:45 -07006887 /* This may look odd, but it is a preparation for
6888 * feeding "there are unchanged files which should
6889 * not produce diffs, but when you are doing copy
6890 * detection you would need them, so here they are"
6891 * entries to the diff-core. They will be prefixed
6892 * with something like '=' or '*' (I haven't decided
6893 * which but should not make any difference).
Junio C Hamanoa6080a02007-06-07 00:04:01 -07006894 * Feeding the same new and old to diff_change()
Junio C Hamano6973dca2006-04-21 23:57:45 -07006895 * also has the same effect.
6896 * Before the final output happens, they are pruned after
6897 * merged into rename/copy pairs as appropriate.
6898 */
Brandon Williams0d1e0e72017-10-31 11:19:11 -07006899 if (options->flags.reverse_diff)
Junio C Hamano6973dca2006-04-21 23:57:45 -07006900 addremove = (addremove == '+' ? '-' :
6901 addremove == '-' ? '+' : addremove);
6902
Junio C Hamanocd676a52008-02-12 14:26:02 -08006903 if (options->prefix &&
6904 strncmp(concatpath, options->prefix, options->prefix_length))
6905 return;
6906
Junio C Hamano6973dca2006-04-21 23:57:45 -07006907 one = alloc_filespec(concatpath);
6908 two = alloc_filespec(concatpath);
6909
6910 if (addremove != '+')
Brandon Williamsf9704c22017-05-30 10:30:50 -07006911 fill_filespec(one, oid, oid_valid, mode);
Jens Lehmanne3d42c42010-01-18 21:26:18 +01006912 if (addremove != '-') {
Brandon Williamsf9704c22017-05-30 10:30:50 -07006913 fill_filespec(two, oid, oid_valid, mode);
Jens Lehmanne3d42c42010-01-18 21:26:18 +01006914 two->dirty_submodule = dirty_submodule;
6915 }
Junio C Hamano6973dca2006-04-21 23:57:45 -07006916
6917 diff_queue(&diff_queued_diff, one, two);
Brandon Williams0d1e0e72017-10-31 11:19:11 -07006918 if (!options->flags.diff_from_contents)
6919 options->flags.has_changes = 1;
Junio C Hamano6973dca2006-04-21 23:57:45 -07006920}
6921
6922void diff_change(struct diff_options *options,
6923 unsigned old_mode, unsigned new_mode,
Brandon Williams94a00972017-05-30 10:30:49 -07006924 const struct object_id *old_oid,
6925 const struct object_id *new_oid,
6926 int old_oid_valid, int new_oid_valid,
Jens Lehmanne3d42c42010-01-18 21:26:18 +01006927 const char *concatpath,
6928 unsigned old_dirty_submodule, unsigned new_dirty_submodule)
Junio C Hamano6973dca2006-04-21 23:57:45 -07006929{
Junio C Hamano6973dca2006-04-21 23:57:45 -07006930 struct diff_filespec *one, *two;
Nguyễn Thái Ngọc Duyf34b2052014-01-25 13:46:50 +07006931 struct diff_filepair *p;
Junio C Hamano6973dca2006-04-21 23:57:45 -07006932
Jens Lehmannaee9c7d2010-08-06 00:39:25 +02006933 if (S_ISGITLINK(old_mode) && S_ISGITLINK(new_mode) &&
6934 is_submodule_ignored(concatpath, options))
Johannes Schindelin50fd9bd2008-05-14 18:03:31 +01006935 return;
6936
Brandon Williams0d1e0e72017-10-31 11:19:11 -07006937 if (options->flags.reverse_diff) {
René Scharfe35d803b2017-01-28 22:40:58 +01006938 SWAP(old_mode, new_mode);
Brandon Williams94a00972017-05-30 10:30:49 -07006939 SWAP(old_oid, new_oid);
6940 SWAP(old_oid_valid, new_oid_valid);
René Scharfe35d803b2017-01-28 22:40:58 +01006941 SWAP(old_dirty_submodule, new_dirty_submodule);
Junio C Hamano6973dca2006-04-21 23:57:45 -07006942 }
Junio C Hamanocd676a52008-02-12 14:26:02 -08006943
6944 if (options->prefix &&
6945 strncmp(concatpath, options->prefix, options->prefix_length))
6946 return;
6947
Junio C Hamano6973dca2006-04-21 23:57:45 -07006948 one = alloc_filespec(concatpath);
6949 two = alloc_filespec(concatpath);
Brandon Williamsf9704c22017-05-30 10:30:50 -07006950 fill_filespec(one, old_oid, old_oid_valid, old_mode);
6951 fill_filespec(two, new_oid, new_oid_valid, new_mode);
Jens Lehmanne3d42c42010-01-18 21:26:18 +01006952 one->dirty_submodule = old_dirty_submodule;
6953 two->dirty_submodule = new_dirty_submodule;
Nguyễn Thái Ngọc Duyf34b2052014-01-25 13:46:50 +07006954 p = diff_queue(&diff_queued_diff, one, two);
Junio C Hamano6973dca2006-04-21 23:57:45 -07006955
Brandon Williams0d1e0e72017-10-31 11:19:11 -07006956 if (options->flags.diff_from_contents)
Nguyễn Thái Ngọc Duyf34b2052014-01-25 13:46:50 +07006957 return;
6958
Brandon Williams0d1e0e72017-10-31 11:19:11 -07006959 if (options->flags.quick && options->skip_stat_unmatch &&
Jeff Kingd2d7fbe2020-06-01 16:22:18 -04006960 !diff_filespec_check_stat_unmatch(options->repo, p)) {
6961 diff_free_filespec_data(p->one);
6962 diff_free_filespec_data(p->two);
Nguyễn Thái Ngọc Duyf34b2052014-01-25 13:46:50 +07006963 return;
Jeff Kingd2d7fbe2020-06-01 16:22:18 -04006964 }
Nguyễn Thái Ngọc Duyf34b2052014-01-25 13:46:50 +07006965
Brandon Williams0d1e0e72017-10-31 11:19:11 -07006966 options->flags.has_changes = 1;
Junio C Hamano6973dca2006-04-21 23:57:45 -07006967}
6968
Junio C Hamanofa7b2902011-04-22 16:05:58 -07006969struct diff_filepair *diff_unmerge(struct diff_options *options, const char *path)
Junio C Hamano6973dca2006-04-21 23:57:45 -07006970{
Junio C Hamano76399c02011-04-22 15:55:55 -07006971 struct diff_filepair *pair;
Junio C Hamano6973dca2006-04-21 23:57:45 -07006972 struct diff_filespec *one, *two;
Junio C Hamanocd676a52008-02-12 14:26:02 -08006973
6974 if (options->prefix &&
6975 strncmp(path, options->prefix, options->prefix_length))
Junio C Hamano76399c02011-04-22 15:55:55 -07006976 return NULL;
Junio C Hamanocd676a52008-02-12 14:26:02 -08006977
Junio C Hamano6973dca2006-04-21 23:57:45 -07006978 one = alloc_filespec(path);
6979 two = alloc_filespec(path);
Junio C Hamano76399c02011-04-22 15:55:55 -07006980 pair = diff_queue(&diff_queued_diff, one, two);
6981 pair->is_unmerged = 1;
6982 return pair;
Junio C Hamano6973dca2006-04-21 23:57:45 -07006983}
Jeff King9cb92c32008-10-05 17:43:45 -04006984
Nguyễn Thái Ngọc Duyb78ea5f2018-09-21 17:57:19 +02006985static char *run_textconv(struct repository *r,
6986 const char *pgm,
6987 struct diff_filespec *spec,
6988 size_t *outsize)
Jeff King9cb92c32008-10-05 17:43:45 -04006989{
Jeff King479b0ae2009-01-22 00:59:56 -05006990 struct diff_tempfile *temp;
René Scharfed3180272014-08-19 21:09:35 +02006991 struct child_process child = CHILD_PROCESS_INIT;
Jeff King9cb92c32008-10-05 17:43:45 -04006992 struct strbuf buf = STRBUF_INIT;
Johannes Sixtda1fbed2010-03-30 19:36:03 +02006993 int err = 0;
Jeff King9cb92c32008-10-05 17:43:45 -04006994
Nguyễn Thái Ngọc Duyb78ea5f2018-09-21 17:57:19 +02006995 temp = prepare_temp_file(r, spec->path, spec);
Ævar Arnfjörð Bjarmason7f146092021-11-25 23:52:21 +01006996 strvec_push(&child.args, pgm);
6997 strvec_push(&child.args, temp->name);
Jeff King9cb92c32008-10-05 17:43:45 -04006998
Jeff King41a457e2009-12-30 06:01:09 -05006999 child.use_shell = 1;
Jeff King9cb92c32008-10-05 17:43:45 -04007000 child.out = -1;
Johannes Sixtda1fbed2010-03-30 19:36:03 +02007001 if (start_command(&child)) {
Jeff King479b0ae2009-01-22 00:59:56 -05007002 remove_tempfile();
Jeff King9cb92c32008-10-05 17:43:45 -04007003 return NULL;
7004 }
Johannes Sixtda1fbed2010-03-30 19:36:03 +02007005
7006 if (strbuf_read(&buf, child.out, 0) < 0)
7007 err = error("error reading from textconv command '%s'", pgm);
Jeff King70d70992009-12-30 04:02:53 -05007008 close(child.out);
Johannes Sixtda1fbed2010-03-30 19:36:03 +02007009
7010 if (finish_command(&child) || err) {
7011 strbuf_release(&buf);
7012 remove_tempfile();
7013 return NULL;
7014 }
Jeff King479b0ae2009-01-22 00:59:56 -05007015 remove_tempfile();
Jeff King9cb92c32008-10-05 17:43:45 -04007016
7017 return strbuf_detach(&buf, outsize);
7018}
Jeff King840383b2010-04-01 20:09:26 -04007019
Nguyễn Thái Ngọc Duy6afaf802018-09-21 17:57:22 +02007020size_t fill_textconv(struct repository *r,
7021 struct userdiff_driver *driver,
Axel Bonneta788d7d2010-06-07 17:23:36 +02007022 struct diff_filespec *df,
7023 char **outbuf)
Jeff King840383b2010-04-01 20:09:26 -04007024{
7025 size_t size;
7026
Jeff Kinga64e6a42016-02-22 13:28:54 -05007027 if (!driver) {
Jeff King840383b2010-04-01 20:09:26 -04007028 if (!DIFF_FILE_VALID(df)) {
7029 *outbuf = "";
7030 return 0;
7031 }
Jonathan Tan1c37e862020-04-07 15:11:41 -07007032 if (diff_populate_filespec(r, df, NULL))
Jeff King840383b2010-04-01 20:09:26 -04007033 die("unable to read files to diff");
7034 *outbuf = df->data;
7035 return df->size;
7036 }
7037
Jeff Kinga64e6a42016-02-22 13:28:54 -05007038 if (!driver->textconv)
Johannes Schindelin033abf92018-05-02 11:38:39 +02007039 BUG("fill_textconv called with non-textconv driver");
Jeff Kinga64e6a42016-02-22 13:28:54 -05007040
brian m. carlson41c95602016-06-24 23:09:24 +00007041 if (driver->textconv_cache && df->oid_valid) {
brian m. carlsona0d12c42016-06-24 23:09:23 +00007042 *outbuf = notes_cache_get(driver->textconv_cache,
brian m. carlson569aa372017-05-06 22:09:58 +00007043 &df->oid,
Jeff Kingd9bae1a2010-04-01 20:12:15 -04007044 &size);
7045 if (*outbuf)
7046 return size;
7047 }
7048
Nguyễn Thái Ngọc Duyb78ea5f2018-09-21 17:57:19 +02007049 *outbuf = run_textconv(r, driver->textconv, df, &size);
Jeff King840383b2010-04-01 20:09:26 -04007050 if (!*outbuf)
7051 die("unable to read files to diff");
Jeff Kingd9bae1a2010-04-01 20:12:15 -04007052
brian m. carlson41c95602016-06-24 23:09:24 +00007053 if (driver->textconv_cache && df->oid_valid) {
Jeff Kingd9bae1a2010-04-01 20:12:15 -04007054 /* ignore errors, as we might be in a readonly repository */
brian m. carlson569aa372017-05-06 22:09:58 +00007055 notes_cache_put(driver->textconv_cache, &df->oid, *outbuf,
Jeff Kingd9bae1a2010-04-01 20:12:15 -04007056 size);
7057 /*
7058 * we could save up changes and flush them all at the end,
7059 * but we would need an extra call after all diffing is done.
7060 * Since generating a cache entry is the slow path anyway,
7061 * this extra overhead probably isn't a big deal.
7062 */
7063 notes_cache_write(driver->textconv_cache);
7064 }
7065
Jeff King840383b2010-04-01 20:09:26 -04007066 return size;
7067}
Nguyễn Thái Ngọc Duy4914c962012-10-26 22:53:52 +07007068
Nguyễn Thái Ngọc Duy6afaf802018-09-21 17:57:22 +02007069int textconv_object(struct repository *r,
7070 const char *path,
Jeff Smith3a35cb22017-05-24 00:15:10 -05007071 unsigned mode,
7072 const struct object_id *oid,
7073 int oid_valid,
7074 char **buf,
7075 unsigned long *buf_size)
7076{
7077 struct diff_filespec *df;
7078 struct userdiff_driver *textconv;
7079
7080 df = alloc_filespec(path);
Junio C Hamanoa6f38c12017-06-19 12:38:44 -07007081 fill_filespec(df, oid, oid_valid, mode);
Nguyễn Thái Ngọc Duybd7ad452018-11-10 06:49:06 +01007082 textconv = get_textconv(r, df);
Jeff Smith3a35cb22017-05-24 00:15:10 -05007083 if (!textconv) {
7084 free_filespec(df);
7085 return 0;
7086 }
7087
Nguyễn Thái Ngọc Duy6afaf802018-09-21 17:57:22 +02007088 *buf_size = fill_textconv(r, textconv, df, buf);
Jeff Smith3a35cb22017-05-24 00:15:10 -05007089 free_filespec(df);
7090 return 1;
7091}
7092
Nguyễn Thái Ngọc Duy4914c962012-10-26 22:53:52 +07007093void setup_diff_pager(struct diff_options *opt)
7094{
7095 /*
7096 * If the user asked for our exit code, then either they want --quiet
7097 * or --exit-code. We should definitely not bother with a pager in the
7098 * former case, as we will generate no output. Since we still properly
7099 * report our exit code even when a pager is run, we _could_ run a
7100 * pager with --exit-code. But since we have not done so historically,
7101 * and because it is easy to find people oneline advising "git diff
7102 * --exit-code" in hooks and other scripts, we do not do so.
7103 */
Brandon Williams0d1e0e72017-10-31 11:19:11 -07007104 if (!opt->flags.exit_with_status &&
Nguyễn Thái Ngọc Duy4914c962012-10-26 22:53:52 +07007105 check_pager_config("diff") != 0)
7106 setup_pager();
7107}