blob: 35cca04f259804476d758f871cba60bae7fe940a [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"
Jeff Kingbe58e702008-10-05 17:43:21 -040016#include "userdiff.h"
Heiko Voigt851e18c2015-08-17 17:21:59 -070017#include "submodule-config.h"
Johannes Schindelin752c0c22009-10-19 14:38:32 +020018#include "submodule.h"
Stefan Beller2e2d5ac2017-06-30 13:53:07 -070019#include "hashmap.h"
Junio C Hamanoa757c642010-03-24 19:21:32 -070020#include "ll-merge.h"
Michael Haggerty02e8ca02012-10-28 17:50:54 +010021#include "string-list.h"
Jeff King82fbf262014-04-19 15:17:06 -040022#include "argv-array.h"
Jacob Keller660e1132016-08-31 16:27:20 -070023#include "graph.h"
Jonathan Tan150e3002017-08-18 15:20:36 -070024#include "packfile.h"
Junio C Hamano6973dca2006-04-21 23:57:45 -070025
Shawn O. Pearce1510fea2006-12-14 06:15:57 -050026#ifdef NO_FAST_WORKING_DIRECTORY
27#define FAST_WORKING_DIRECTORY 0
28#else
29#define FAST_WORKING_DIRECTORY 1
30#endif
31
David Rientjes96f1e582006-08-15 10:23:48 -070032static int diff_detect_rename_default;
Stefan Beller33de7162017-05-08 12:03:38 -040033static int diff_indent_heuristic = 1;
Jeff King92c57e52011-02-19 05:21:28 -050034static int diff_rename_limit_default = 400;
Jim Meyeringa624eaa2008-08-15 13:39:26 +020035static int diff_suppress_blank_empty;
Junio C Hamanod2aea132012-09-15 13:59:59 -070036static int diff_use_color_default = -1;
Stefan Beller2e2d5ac2017-06-30 13:53:07 -070037static int diff_color_moved_default;
Jeff Muizelaar6468a4e2012-09-27 15:12:52 -040038static int diff_context_default = 3;
Vegard Nossumc4888672017-01-12 13:21:11 +010039static int diff_interhunk_context_default;
Boyd Stephen Smith Jr98a4d872009-01-20 21:46:57 -060040static const char *diff_word_regex_cfg;
Johannes Schindelincbe02102007-12-17 13:42:20 +000041static const char *external_diff_cmd_cfg;
Samuel Bronson6d8940b2013-12-18 19:08:12 -050042static const char *diff_order_file_cfg;
Junio C Hamanoaecbf912007-08-31 13:13:42 -070043int diff_auto_refresh_index = 1;
Junio C Hamanoa5a818e2008-08-18 20:08:09 -070044static int diff_mnemonic_prefix;
Eli Collinsf89504d2010-05-02 19:03:41 -070045static int diff_no_prefix;
Zbigniew Jędrzejewski-Szmekdf444832012-03-01 13:26:46 +010046static int diff_stat_graph_width;
Johan Herland712d2c72011-04-29 11:36:20 +020047static int diff_dirstat_permille_default = 30;
Johannes Schindelinbe4f2b42010-08-05 10:49:55 +020048static struct diff_options default_diff_options;
Michal Privoznik07ab4de2013-01-16 08:51:57 +010049static long diff_algorithm;
Junio C Hamanoa17505f2016-10-04 15:26:27 -070050static unsigned ws_error_highlight_default = WSEH_NEW;
Junio C Hamano6973dca2006-04-21 23:57:45 -070051
Jeff King7c92fe02006-09-08 04:03:18 -040052static char diff_colors[][COLOR_MAXLEN] = {
Arjen Laarhovendc6ebd42009-02-13 22:53:40 +010053 GIT_COLOR_RESET,
Jeff King8dbf3eb2015-05-27 16:48:46 -040054 GIT_COLOR_NORMAL, /* CONTEXT */
Arjen Laarhovendc6ebd42009-02-13 22:53:40 +010055 GIT_COLOR_BOLD, /* METAINFO */
56 GIT_COLOR_CYAN, /* FRAGINFO */
57 GIT_COLOR_RED, /* OLD */
58 GIT_COLOR_GREEN, /* NEW */
59 GIT_COLOR_YELLOW, /* COMMIT */
60 GIT_COLOR_BG_RED, /* WHITESPACE */
Bert Wesarg89cb73a2009-11-27 07:55:18 +010061 GIT_COLOR_NORMAL, /* FUNCINFO */
Stefan Beller86b452e2017-06-30 13:53:09 -070062 GIT_COLOR_BOLD_MAGENTA, /* OLD_MOVED */
63 GIT_COLOR_BOLD_BLUE, /* OLD_MOVED ALTERNATIVE */
64 GIT_COLOR_FAINT, /* OLD_MOVED_DIM */
65 GIT_COLOR_FAINT_ITALIC, /* OLD_MOVED_ALTERNATIVE_DIM */
66 GIT_COLOR_BOLD_CYAN, /* NEW_MOVED */
67 GIT_COLOR_BOLD_YELLOW, /* NEW_MOVED ALTERNATIVE */
68 GIT_COLOR_FAINT, /* NEW_MOVED_DIM */
69 GIT_COLOR_FAINT_ITALIC, /* NEW_MOVED_ALTERNATIVE_DIM */
Johannes Schindelincd112ce2006-06-13 18:45:44 +020070};
71
Jean-Noël AVILAa2f05c92016-09-20 21:04:27 +020072static NORETURN void die_want_option(const char *option_name)
73{
74 die(_("option '%s' requires a value"), option_name);
75}
76
Jeff King9e1a5eb2014-06-18 15:41:50 -040077static int parse_diff_color_slot(const char *var)
Junio C Hamano801235c2006-06-24 04:06:23 -070078{
Jeff King74b15bf2015-05-27 03:22:19 -040079 if (!strcasecmp(var, "context") || !strcasecmp(var, "plain"))
Jeff King8dbf3eb2015-05-27 16:48:46 -040080 return DIFF_CONTEXT;
Jeff King9e1a5eb2014-06-18 15:41:50 -040081 if (!strcasecmp(var, "meta"))
Junio C Hamano801235c2006-06-24 04:06:23 -070082 return DIFF_METAINFO;
Jeff King9e1a5eb2014-06-18 15:41:50 -040083 if (!strcasecmp(var, "frag"))
Junio C Hamano801235c2006-06-24 04:06:23 -070084 return DIFF_FRAGINFO;
Jeff King9e1a5eb2014-06-18 15:41:50 -040085 if (!strcasecmp(var, "old"))
Junio C Hamano801235c2006-06-24 04:06:23 -070086 return DIFF_FILE_OLD;
Jeff King9e1a5eb2014-06-18 15:41:50 -040087 if (!strcasecmp(var, "new"))
Junio C Hamano801235c2006-06-24 04:06:23 -070088 return DIFF_FILE_NEW;
Jeff King9e1a5eb2014-06-18 15:41:50 -040089 if (!strcasecmp(var, "commit"))
Jeff Kingce436972006-07-23 05:24:18 -040090 return DIFF_COMMIT;
Jeff King9e1a5eb2014-06-18 15:41:50 -040091 if (!strcasecmp(var, "whitespace"))
Junio C Hamano448c3ef2006-09-22 22:48:39 -070092 return DIFF_WHITESPACE;
Jeff King9e1a5eb2014-06-18 15:41:50 -040093 if (!strcasecmp(var, "func"))
Bert Wesarg89cb73a2009-11-27 07:55:18 +010094 return DIFF_FUNCINFO;
Stefan Beller2e2d5ac2017-06-30 13:53:07 -070095 if (!strcasecmp(var, "oldmoved"))
96 return DIFF_FILE_OLD_MOVED;
97 if (!strcasecmp(var, "oldmovedalternative"))
98 return DIFF_FILE_OLD_MOVED_ALT;
Stefan Beller86b452e2017-06-30 13:53:09 -070099 if (!strcasecmp(var, "oldmoveddimmed"))
100 return DIFF_FILE_OLD_MOVED_DIM;
101 if (!strcasecmp(var, "oldmovedalternativedimmed"))
102 return DIFF_FILE_OLD_MOVED_ALT_DIM;
Stefan Beller2e2d5ac2017-06-30 13:53:07 -0700103 if (!strcasecmp(var, "newmoved"))
104 return DIFF_FILE_NEW_MOVED;
105 if (!strcasecmp(var, "newmovedalternative"))
106 return DIFF_FILE_NEW_MOVED_ALT;
Stefan Beller86b452e2017-06-30 13:53:09 -0700107 if (!strcasecmp(var, "newmoveddimmed"))
108 return DIFF_FILE_NEW_MOVED_DIM;
109 if (!strcasecmp(var, "newmovedalternativedimmed"))
110 return DIFF_FILE_NEW_MOVED_ALT_DIM;
Jeff King8b8e8622009-12-12 07:25:24 -0500111 return -1;
Junio C Hamano801235c2006-06-24 04:06:23 -0700112}
113
Michael Haggerty02e8ca02012-10-28 17:50:54 +0100114static int parse_dirstat_params(struct diff_options *options, const char *params_string,
Johan Herland51670fc2011-04-29 11:36:22 +0200115 struct strbuf *errmsg)
Johan Herland333f3fb2011-04-29 11:36:18 +0200116{
Michael Haggerty02e8ca02012-10-28 17:50:54 +0100117 char *params_copy = xstrdup(params_string);
118 struct string_list params = STRING_LIST_INIT_NODUP;
119 int ret = 0;
120 int i;
Johan Herland51670fc2011-04-29 11:36:22 +0200121
Michael Haggerty02e8ca02012-10-28 17:50:54 +0100122 if (*params_copy)
123 string_list_split_in_place(&params, params_copy, ',', -1);
124 for (i = 0; i < params.nr; i++) {
125 const char *p = params.items[i].string;
126 if (!strcmp(p, "changes")) {
Brandon Williams0d1e0e72017-10-31 11:19:11 -0700127 options->flags.dirstat_by_line = 0;
128 options->flags.dirstat_by_file = 0;
Michael Haggerty02e8ca02012-10-28 17:50:54 +0100129 } else if (!strcmp(p, "lines")) {
Brandon Williams0d1e0e72017-10-31 11:19:11 -0700130 options->flags.dirstat_by_line = 1;
131 options->flags.dirstat_by_file = 0;
Michael Haggerty02e8ca02012-10-28 17:50:54 +0100132 } else if (!strcmp(p, "files")) {
Brandon Williams0d1e0e72017-10-31 11:19:11 -0700133 options->flags.dirstat_by_line = 0;
134 options->flags.dirstat_by_file = 1;
Michael Haggerty02e8ca02012-10-28 17:50:54 +0100135 } else if (!strcmp(p, "noncumulative")) {
Brandon Williams0d1e0e72017-10-31 11:19:11 -0700136 options->flags.dirstat_cumulative = 0;
Michael Haggerty02e8ca02012-10-28 17:50:54 +0100137 } else if (!strcmp(p, "cumulative")) {
Brandon Williams0d1e0e72017-10-31 11:19:11 -0700138 options->flags.dirstat_cumulative = 1;
Johan Herland333f3fb2011-04-29 11:36:18 +0200139 } else if (isdigit(*p)) {
140 char *end;
Johan Herland51670fc2011-04-29 11:36:22 +0200141 int permille = strtoul(p, &end, 10) * 10;
142 if (*end == '.' && isdigit(*++end)) {
Johan Herland712d2c72011-04-29 11:36:20 +0200143 /* only use first digit */
Johan Herland51670fc2011-04-29 11:36:22 +0200144 permille += *end - '0';
Johan Herland712d2c72011-04-29 11:36:20 +0200145 /* .. and ignore any further digits */
Johan Herland51670fc2011-04-29 11:36:22 +0200146 while (isdigit(*++end))
Johan Herland712d2c72011-04-29 11:36:20 +0200147 ; /* nothing */
148 }
Michael Haggerty02e8ca02012-10-28 17:50:54 +0100149 if (!*end)
Johan Herland51670fc2011-04-29 11:36:22 +0200150 options->dirstat_permille = permille;
151 else {
Michael Haggerty02e8ca02012-10-28 17:50:54 +0100152 strbuf_addf(errmsg, _(" Failed to parse dirstat cut-off percentage '%s'\n"),
153 p);
Johan Herland51670fc2011-04-29 11:36:22 +0200154 ret++;
155 }
156 } else {
Michael Haggerty02e8ca02012-10-28 17:50:54 +0100157 strbuf_addf(errmsg, _(" Unknown dirstat parameter '%s'\n"), p);
Johan Herland51670fc2011-04-29 11:36:22 +0200158 ret++;
Johan Herland333f3fb2011-04-29 11:36:18 +0200159 }
Johan Herland51670fc2011-04-29 11:36:22 +0200160
Johan Herland333f3fb2011-04-29 11:36:18 +0200161 }
Michael Haggerty02e8ca02012-10-28 17:50:54 +0100162 string_list_clear(&params, 0);
163 free(params_copy);
Johan Herland51670fc2011-04-29 11:36:22 +0200164 return ret;
Johan Herland333f3fb2011-04-29 11:36:18 +0200165}
166
Ramkumar Ramachandrac47ef572012-11-13 21:12:45 +0530167static int parse_submodule_params(struct diff_options *options, const char *value)
168{
169 if (!strcmp(value, "log"))
Jacob Keller61cfbc02016-08-31 16:27:21 -0700170 options->submodule_format = DIFF_SUBMODULE_LOG;
Ramkumar Ramachandrac47ef572012-11-13 21:12:45 +0530171 else if (!strcmp(value, "short"))
Jacob Keller61cfbc02016-08-31 16:27:21 -0700172 options->submodule_format = DIFF_SUBMODULE_SHORT;
Jacob Kellerfd47ae62016-08-31 16:27:25 -0700173 else if (!strcmp(value, "diff"))
174 options->submodule_format = DIFF_SUBMODULE_INLINE_DIFF;
Ramkumar Ramachandrac47ef572012-11-13 21:12:45 +0530175 else
176 return -1;
177 return 0;
178}
179
Linus Torvaldscced5fb2009-04-09 11:46:15 -0700180static int git_config_rename(const char *var, const char *value)
181{
182 if (!value)
183 return DIFF_DETECT_RENAME;
184 if (!strcasecmp(value, "copies") || !strcasecmp(value, "copy"))
185 return DIFF_DETECT_COPY;
186 return git_config_bool(var,value) ? DIFF_DETECT_RENAME : 0;
187}
188
Michal Privoznik07924d42013-01-16 08:51:58 +0100189long parse_algorithm_value(const char *value)
Michal Privoznik07ab4de2013-01-16 08:51:57 +0100190{
191 if (!value)
192 return -1;
193 else if (!strcasecmp(value, "myers") || !strcasecmp(value, "default"))
194 return 0;
195 else if (!strcasecmp(value, "minimal"))
196 return XDF_NEED_MINIMAL;
197 else if (!strcasecmp(value, "patience"))
198 return XDF_PATIENCE_DIFF;
199 else if (!strcasecmp(value, "histogram"))
200 return XDF_HISTOGRAM_DIFF;
201 return -1;
202}
203
Junio C Hamano0b4b42e2016-10-04 15:09:18 -0700204static int parse_one_token(const char **arg, const char *token)
205{
206 const char *rest;
207 if (skip_prefix(*arg, token, &rest) && (!*rest || *rest == ',')) {
208 *arg = rest;
209 return 1;
210 }
211 return 0;
212}
213
214static int parse_ws_error_highlight(const char *arg)
215{
216 const char *orig_arg = arg;
217 unsigned val = 0;
218
219 while (*arg) {
220 if (parse_one_token(&arg, "none"))
221 val = 0;
222 else if (parse_one_token(&arg, "default"))
223 val = WSEH_NEW;
224 else if (parse_one_token(&arg, "all"))
225 val = WSEH_NEW | WSEH_OLD | WSEH_CONTEXT;
226 else if (parse_one_token(&arg, "new"))
227 val |= WSEH_NEW;
228 else if (parse_one_token(&arg, "old"))
229 val |= WSEH_OLD;
230 else if (parse_one_token(&arg, "context"))
231 val |= WSEH_CONTEXT;
232 else {
233 return -1 - (int)(arg - orig_arg);
234 }
235 if (*arg)
236 arg++;
237 }
238 return val;
239}
240
Junio C Hamanoe0e324a2007-07-07 01:49:58 -0700241/*
Junio C Hamano83ad63c2006-07-08 01:05:16 -0700242 * These are to give UI layer defaults.
243 * The core-level commands such as git-diff-files should
244 * never be affected by the setting of diff.renames
245 * the user happens to have in the configuration file.
246 */
Matthieu Moy5404c112016-02-25 09:59:21 +0100247void init_diff_ui_defaults(void)
248{
249 diff_detect_rename_default = 1;
250}
251
Michael Haggerty5b162872016-09-05 11:44:53 +0200252int git_diff_heuristic_config(const char *var, const char *value, void *cb)
253{
Junio C Hamano3cde4e02016-12-23 12:32:22 -0800254 if (!strcmp(var, "diff.indentheuristic"))
Michael Haggerty5b162872016-09-05 11:44:53 +0200255 diff_indent_heuristic = git_config_bool(var, value);
Michael Haggerty5b162872016-09-05 11:44:53 +0200256 return 0;
257}
258
Stefan Beller2e2d5ac2017-06-30 13:53:07 -0700259static int parse_color_moved(const char *arg)
260{
261 switch (git_parse_maybe_bool(arg)) {
262 case 0:
263 return COLOR_MOVED_NO;
264 case 1:
265 return COLOR_MOVED_DEFAULT;
266 default:
267 break;
268 }
269
270 if (!strcmp(arg, "no"))
271 return COLOR_MOVED_NO;
Stefan Beller176841f2017-06-30 13:53:08 -0700272 else if (!strcmp(arg, "plain"))
273 return COLOR_MOVED_PLAIN;
Stefan Beller2e2d5ac2017-06-30 13:53:07 -0700274 else if (!strcmp(arg, "zebra"))
275 return COLOR_MOVED_ZEBRA;
276 else if (!strcmp(arg, "default"))
277 return COLOR_MOVED_DEFAULT;
Stefan Beller86b452e2017-06-30 13:53:09 -0700278 else if (!strcmp(arg, "dimmed_zebra"))
279 return COLOR_MOVED_ZEBRA_DIM;
Stefan Beller2e2d5ac2017-06-30 13:53:07 -0700280 else
Stefan Beller86b452e2017-06-30 13:53:09 -0700281 return error(_("color moved setting must be one of 'no', 'default', 'zebra', 'dimmed_zebra', 'plain'"));
Stefan Beller2e2d5ac2017-06-30 13:53:07 -0700282}
283
Johannes Schindelinef90d6d2008-05-14 18:46:53 +0100284int git_diff_ui_config(const char *var, const char *value, void *cb)
Junio C Hamano801235c2006-06-24 04:06:23 -0700285{
Andy Parkinsa159ca02006-12-13 09:13:28 +0000286 if (!strcmp(var, "diff.color") || !strcmp(var, "color.diff")) {
Jeff Kinge269eb72011-08-17 22:03:48 -0700287 diff_use_color_default = git_config_colorbool(var, value);
Junio C Hamano801235c2006-06-24 04:06:23 -0700288 return 0;
289 }
Stefan Beller2e2d5ac2017-06-30 13:53:07 -0700290 if (!strcmp(var, "diff.colormoved")) {
291 int cm = parse_color_moved(value);
292 if (cm < 0)
293 return -1;
294 diff_color_moved_default = cm;
295 return 0;
296 }
Jeff Muizelaar6468a4e2012-09-27 15:12:52 -0400297 if (!strcmp(var, "diff.context")) {
298 diff_context_default = git_config_int(var, value);
299 if (diff_context_default < 0)
300 return -1;
301 return 0;
302 }
Vegard Nossumc4888672017-01-12 13:21:11 +0100303 if (!strcmp(var, "diff.interhunkcontext")) {
304 diff_interhunk_context_default = git_config_int(var, value);
305 if (diff_interhunk_context_default < 0)
306 return -1;
307 return 0;
308 }
Eric Wongb68ea122006-07-07 04:01:23 -0700309 if (!strcmp(var, "diff.renames")) {
Linus Torvaldscced5fb2009-04-09 11:46:15 -0700310 diff_detect_rename_default = git_config_rename(var, value);
Eric Wongb68ea122006-07-07 04:01:23 -0700311 return 0;
312 }
Junio C Hamanoaecbf912007-08-31 13:13:42 -0700313 if (!strcmp(var, "diff.autorefreshindex")) {
314 diff_auto_refresh_index = git_config_bool(var, value);
315 return 0;
316 }
Junio C Hamanoa5a818e2008-08-18 20:08:09 -0700317 if (!strcmp(var, "diff.mnemonicprefix")) {
318 diff_mnemonic_prefix = git_config_bool(var, value);
319 return 0;
320 }
Eli Collinsf89504d2010-05-02 19:03:41 -0700321 if (!strcmp(var, "diff.noprefix")) {
322 diff_no_prefix = git_config_bool(var, value);
323 return 0;
324 }
Zbigniew Jędrzejewski-Szmekdf444832012-03-01 13:26:46 +0100325 if (!strcmp(var, "diff.statgraphwidth")) {
326 diff_stat_graph_width = git_config_int(var, value);
327 return 0;
328 }
Brian Hetrodaec8082008-07-05 01:24:43 -0400329 if (!strcmp(var, "diff.external"))
330 return git_config_string(&external_diff_cmd_cfg, var, value);
Boyd Stephen Smith Jr98a4d872009-01-20 21:46:57 -0600331 if (!strcmp(var, "diff.wordregex"))
332 return git_config_string(&diff_word_regex_cfg, var, value);
Samuel Bronson6d8940b2013-12-18 19:08:12 -0500333 if (!strcmp(var, "diff.orderfile"))
334 return git_config_pathname(&diff_order_file_cfg, var, value);
Junio C Hamanof1af60b2007-04-22 17:52:55 -0700335
Johannes Schindelinbe4f2b42010-08-05 10:49:55 +0200336 if (!strcmp(var, "diff.ignoresubmodules"))
337 handle_ignore_submodules_arg(&default_diff_options, value);
338
Ramkumar Ramachandrac47ef572012-11-13 21:12:45 +0530339 if (!strcmp(var, "diff.submodule")) {
340 if (parse_submodule_params(&default_diff_options, value))
341 warning(_("Unknown value for 'diff.submodule' config variable: '%s'"),
342 value);
343 return 0;
344 }
345
Michal Privoznik07ab4de2013-01-16 08:51:57 +0100346 if (!strcmp(var, "diff.algorithm")) {
347 diff_algorithm = parse_algorithm_value(value);
348 if (diff_algorithm < 0)
349 return -1;
350 return 0;
351 }
352
Junio C Hamanoa17505f2016-10-04 15:26:27 -0700353 if (!strcmp(var, "diff.wserrorhighlight")) {
354 int val = parse_ws_error_highlight(value);
355 if (val < 0)
356 return -1;
357 ws_error_highlight_default = val;
358 return 0;
359 }
360
Jeff King33c643b2017-10-13 13:24:31 -0400361 if (git_color_config(var, value, cb) < 0)
362 return -1;
363
Johannes Schindelinef90d6d2008-05-14 18:46:53 +0100364 return git_diff_basic_config(var, value, cb);
Jeff King9a1805a2008-01-04 03:59:34 -0500365}
366
Johannes Schindelinef90d6d2008-05-14 18:46:53 +0100367int git_diff_basic_config(const char *var, const char *value, void *cb)
Jeff King9a1805a2008-01-04 03:59:34 -0500368{
Jeff Kingae021d82014-06-18 15:47:50 -0400369 const char *name;
370
Linus Torvalds2b6ca6d2008-08-05 11:27:30 -0700371 if (!strcmp(var, "diff.renamelimit")) {
372 diff_rename_limit_default = git_config_int(var, value);
373 return 0;
374 }
375
Jeff King6680a082012-02-07 13:23:02 -0500376 if (userdiff_config(var, value) < 0)
377 return -1;
Jeff Kingc7534ef2008-10-26 00:45:55 -0400378
Jeff Kingae021d82014-06-18 15:47:50 -0400379 if (skip_prefix(var, "diff.color.", &name) ||
380 skip_prefix(var, "color.diff.", &name)) {
381 int slot = parse_diff_color_slot(name);
Jeff King8b8e8622009-12-12 07:25:24 -0500382 if (slot < 0)
383 return 0;
Junio C Hamano64f30e92008-02-11 10:53:56 -0800384 if (!value)
385 return config_error_nonbool(var);
Jeff Kingf6c5a292014-10-07 15:33:09 -0400386 return color_parse(value, diff_colors[slot]);
Junio C Hamano801235c2006-06-24 04:06:23 -0700387 }
Junio C Hamanof1af60b2007-04-22 17:52:55 -0700388
Jim Meyeringa624eaa2008-08-15 13:39:26 +0200389 /* like GNU diff's --suppress-blank-empty option */
Johannes Schindelin950db872009-01-20 22:08:33 +0100390 if (!strcmp(var, "diff.suppressblankempty") ||
391 /* for backwards compatibility */
392 !strcmp(var, "diff.suppress-blank-empty")) {
Jim Meyeringa624eaa2008-08-15 13:39:26 +0200393 diff_suppress_blank_empty = git_config_bool(var, value);
394 return 0;
395 }
396
Johan Herland2d174952011-04-29 11:36:19 +0200397 if (!strcmp(var, "diff.dirstat")) {
Johan Herland51670fc2011-04-29 11:36:22 +0200398 struct strbuf errmsg = STRBUF_INIT;
Johan Herland712d2c72011-04-29 11:36:20 +0200399 default_diff_options.dirstat_permille = diff_dirstat_permille_default;
Johan Herland51670fc2011-04-29 11:36:22 +0200400 if (parse_dirstat_params(&default_diff_options, value, &errmsg))
Johan Herland7478ac52011-04-29 11:36:23 +0200401 warning(_("Found errors in 'diff.dirstat' config variable:\n%s"),
Johan Herland51670fc2011-04-29 11:36:22 +0200402 errmsg.buf);
403 strbuf_release(&errmsg);
Johan Herland712d2c72011-04-29 11:36:20 +0200404 diff_dirstat_permille_default = default_diff_options.dirstat_permille;
Johan Herland2d174952011-04-29 11:36:19 +0200405 return 0;
406 }
407
Marc Branchaudcf5e7722017-05-08 12:03:36 -0400408 if (git_diff_heuristic_config(var, value, cb) < 0)
409 return -1;
410
Jeff King3e1dd172011-08-17 22:05:08 -0700411 return git_default_config(var, value, cb);
Junio C Hamano801235c2006-06-24 04:06:23 -0700412}
413
Junio C Hamano6973dca2006-04-21 23:57:45 -0700414static char *quote_two(const char *one, const char *two)
415{
416 int need_one = quote_c_style(one, NULL, NULL, 1);
417 int need_two = quote_c_style(two, NULL, NULL, 1);
Brandon Caseyf285a2d2008-10-09 14:12:12 -0500418 struct strbuf res = STRBUF_INIT;
Junio C Hamano6973dca2006-04-21 23:57:45 -0700419
420 if (need_one + need_two) {
Pierre Habouzit663af342007-09-20 00:42:15 +0200421 strbuf_addch(&res, '"');
422 quote_c_style(one, &res, NULL, 1);
423 quote_c_style(two, &res, NULL, 1);
424 strbuf_addch(&res, '"');
425 } else {
426 strbuf_addstr(&res, one);
427 strbuf_addstr(&res, two);
Junio C Hamano6973dca2006-04-21 23:57:45 -0700428 }
Pierre Habouzitb315c5c2007-09-27 12:58:23 +0200429 return strbuf_detach(&res, NULL);
Junio C Hamano6973dca2006-04-21 23:57:45 -0700430}
431
432static const char *external_diff(void)
433{
434 static const char *external_diff_cmd = NULL;
435 static int done_preparing = 0;
436
437 if (done_preparing)
438 return external_diff_cmd;
439 external_diff_cmd = getenv("GIT_EXTERNAL_DIFF");
Johannes Schindelincbe02102007-12-17 13:42:20 +0000440 if (!external_diff_cmd)
441 external_diff_cmd = external_diff_cmd_cfg;
Junio C Hamano6973dca2006-04-21 23:57:45 -0700442 done_preparing = 1;
443 return external_diff_cmd;
444}
445
Michael Haggerty284098f2015-08-12 19:12:01 +0200446/*
447 * Keep track of files used for diffing. Sometimes such an entry
448 * refers to a temporary file, sometimes to an existing file, and
449 * sometimes to "/dev/null".
450 */
Junio C Hamano6973dca2006-04-21 23:57:45 -0700451static struct diff_tempfile {
Michael Haggerty284098f2015-08-12 19:12:01 +0200452 /*
453 * filename external diff should read from, or NULL if this
454 * entry is currently not in use:
455 */
456 const char *name;
457
brian m. carlsondc015052017-03-26 16:01:24 +0000458 char hex[GIT_MAX_HEXSZ + 1];
Junio C Hamano6973dca2006-04-21 23:57:45 -0700459 char mode[10];
Michael Haggerty284098f2015-08-12 19:12:01 +0200460
461 /*
462 * If this diff_tempfile instance refers to a temporary file,
463 * this tempfile object is used to manage its lifetime.
464 */
Jeff King076aa2c2017-09-05 08:15:08 -0400465 struct tempfile *tempfile;
Junio C Hamano6973dca2006-04-21 23:57:45 -0700466} diff_temp[2];
467
Junio C Hamano6957eb92009-09-14 18:44:01 -0700468struct emit_callback {
Junio C Hamano6957eb92009-09-14 18:44:01 -0700469 int color_diff;
470 unsigned ws_rule;
471 int blank_at_eof_in_preimage;
472 int blank_at_eof_in_postimage;
473 int lno_in_preimage;
474 int lno_in_postimage;
Junio C Hamano6957eb92009-09-14 18:44:01 -0700475 const char **label_path;
476 struct diff_words_data *diff_words;
Bo Yanga3c158d2010-05-26 15:08:02 +0800477 struct diff_options *opt;
Greg Bacon3e97c7c2009-11-19 15:12:24 -0600478 struct strbuf *header;
Junio C Hamano6957eb92009-09-14 18:44:01 -0700479};
480
Junio C Hamano6973dca2006-04-21 23:57:45 -0700481static int count_lines(const char *data, int size)
482{
483 int count, ch, completely_empty = 1, nl_just_seen = 0;
484 count = 0;
485 while (0 < size--) {
486 ch = *data++;
487 if (ch == '\n') {
488 count++;
489 nl_just_seen = 1;
490 completely_empty = 0;
491 }
492 else {
493 nl_just_seen = 0;
494 completely_empty = 0;
495 }
496 }
497 if (completely_empty)
498 return 0;
499 if (!nl_just_seen)
500 count++; /* no trailing newline */
501 return count;
502}
503
Junio C Hamano6957eb92009-09-14 18:44:01 -0700504static int fill_mmfile(mmfile_t *mf, struct diff_filespec *one)
505{
506 if (!DIFF_FILE_VALID(one)) {
507 mf->ptr = (char *)""; /* does not matter */
508 mf->size = 0;
509 return 0;
510 }
511 else if (diff_populate_filespec(one, 0))
512 return -1;
Junio C Hamanobb35fef2009-09-15 03:38:30 -0700513
Junio C Hamano6957eb92009-09-14 18:44:01 -0700514 mf->ptr = one->data;
515 mf->size = one->size;
516 return 0;
517}
518
Jeff Kingabb371a2011-02-19 03:16:32 -0500519/* like fill_mmfile, but only for size, so we can avoid retrieving blob */
520static unsigned long diff_filespec_size(struct diff_filespec *one)
521{
522 if (!DIFF_FILE_VALID(one))
523 return 0;
Nguyễn Thái Ngọc Duy8e5dd3d2014-08-16 10:08:04 +0700524 diff_populate_filespec(one, CHECK_SIZE_ONLY);
Jeff Kingabb371a2011-02-19 03:16:32 -0500525 return one->size;
526}
527
Junio C Hamano6957eb92009-09-14 18:44:01 -0700528static int count_trailing_blank(mmfile_t *mf, unsigned ws_rule)
529{
530 char *ptr = mf->ptr;
531 long size = mf->size;
532 int cnt = 0;
533
534 if (!size)
535 return cnt;
536 ptr += size - 1; /* pointing at the very end */
537 if (*ptr != '\n')
538 ; /* incomplete line */
539 else
540 ptr--; /* skip the last LF */
541 while (mf->ptr < ptr) {
542 char *prev_eol;
543 for (prev_eol = ptr; mf->ptr <= prev_eol; prev_eol--)
544 if (*prev_eol == '\n')
545 break;
546 if (!ws_blank_line(prev_eol + 1, ptr - prev_eol, ws_rule))
547 break;
548 cnt++;
549 ptr = prev_eol - 1;
550 }
551 return cnt;
552}
553
554static void check_blank_at_eof(mmfile_t *mf1, mmfile_t *mf2,
555 struct emit_callback *ecbdata)
556{
557 int l1, l2, at;
558 unsigned ws_rule = ecbdata->ws_rule;
559 l1 = count_trailing_blank(mf1, ws_rule);
560 l2 = count_trailing_blank(mf2, ws_rule);
561 if (l2 <= l1) {
562 ecbdata->blank_at_eof_in_preimage = 0;
563 ecbdata->blank_at_eof_in_postimage = 0;
564 return;
565 }
566 at = count_lines(mf1->ptr, mf1->size);
567 ecbdata->blank_at_eof_in_preimage = (at - l1) + 1;
568
569 at = count_lines(mf2->ptr, mf2->size);
570 ecbdata->blank_at_eof_in_postimage = (at - l2) + 1;
571}
572
Bo Yanga3c158d2010-05-26 15:08:02 +0800573static void emit_line_0(struct diff_options *o, const char *set, const char *reset,
Junio C Hamano250f7992009-09-14 18:44:01 -0700574 int first, const char *line, int len)
Junio C Hamano6957eb92009-09-14 18:44:01 -0700575{
576 int has_trailing_newline, has_trailing_carriage_return;
Junio C Hamano250f7992009-09-14 18:44:01 -0700577 int nofirst;
Bo Yanga3c158d2010-05-26 15:08:02 +0800578 FILE *file = o->file;
579
John Keeping30997bb2013-02-07 20:15:27 +0000580 fputs(diff_line_prefix(o), file);
Junio C Hamano6957eb92009-09-14 18:44:01 -0700581
Junio C Hamano250f7992009-09-14 18:44:01 -0700582 if (len == 0) {
583 has_trailing_newline = (first == '\n');
584 has_trailing_carriage_return = (!has_trailing_newline &&
585 (first == '\r'));
586 nofirst = has_trailing_newline || has_trailing_carriage_return;
587 } else {
588 has_trailing_newline = (len > 0 && line[len-1] == '\n');
589 if (has_trailing_newline)
590 len--;
591 has_trailing_carriage_return = (len > 0 && line[len-1] == '\r');
592 if (has_trailing_carriage_return)
593 len--;
594 nofirst = 0;
595 }
Junio C Hamano6957eb92009-09-14 18:44:01 -0700596
Junio C Hamano06a47552009-11-27 22:04:10 -0800597 if (len || !nofirst) {
598 fputs(set, file);
599 if (!nofirst)
600 fputc(first, file);
601 fwrite(line, len, 1, file);
602 fputs(reset, file);
603 }
Junio C Hamano6957eb92009-09-14 18:44:01 -0700604 if (has_trailing_carriage_return)
605 fputc('\r', file);
606 if (has_trailing_newline)
607 fputc('\n', file);
608}
609
Bo Yanga3c158d2010-05-26 15:08:02 +0800610static void emit_line(struct diff_options *o, const char *set, const char *reset,
Junio C Hamano250f7992009-09-14 18:44:01 -0700611 const char *line, int len)
612{
Bo Yanga3c158d2010-05-26 15:08:02 +0800613 emit_line_0(o, set, reset, line[0], line+1, len-1);
Junio C Hamano250f7992009-09-14 18:44:01 -0700614}
615
Stefan Beller36a4cef2017-06-29 17:06:49 -0700616enum diff_symbol {
Stefan Beller4eed0eb2017-06-29 17:07:01 -0700617 DIFF_SYMBOL_BINARY_DIFF_HEADER,
618 DIFF_SYMBOL_BINARY_DIFF_HEADER_DELTA,
619 DIFF_SYMBOL_BINARY_DIFF_HEADER_LITERAL,
620 DIFF_SYMBOL_BINARY_DIFF_BODY,
621 DIFF_SYMBOL_BINARY_DIFF_FOOTER,
Stefan Beller0911c472017-06-29 17:07:02 -0700622 DIFF_SYMBOL_STATS_SUMMARY_NO_FILES,
623 DIFF_SYMBOL_STATS_SUMMARY_ABBREV,
624 DIFF_SYMBOL_STATS_SUMMARY_INSERTS_DELETES,
625 DIFF_SYMBOL_STATS_LINE,
Stefan Bellerbd033292017-06-29 17:07:03 -0700626 DIFF_SYMBOL_WORD_DIFF,
Stefan Beller30b7e1e2017-06-29 17:07:04 -0700627 DIFF_SYMBOL_STAT_SEP,
Stefan Beller146fdb02017-06-29 17:07:05 -0700628 DIFF_SYMBOL_SUMMARY,
Stefan Bellerf3597132017-06-29 17:07:00 -0700629 DIFF_SYMBOL_SUBMODULE_ADD,
630 DIFF_SYMBOL_SUBMODULE_DEL,
631 DIFF_SYMBOL_SUBMODULE_UNTRACKED,
632 DIFF_SYMBOL_SUBMODULE_MODIFIED,
633 DIFF_SYMBOL_SUBMODULE_HEADER,
634 DIFF_SYMBOL_SUBMODULE_ERROR,
635 DIFF_SYMBOL_SUBMODULE_PIPETHROUGH,
Stefan Beller5af6ea92017-06-29 17:06:59 -0700636 DIFF_SYMBOL_REWRITE_DIFF,
Stefan Beller4acaaa72017-06-29 17:06:58 -0700637 DIFF_SYMBOL_BINARY_FILES,
Stefan Bellera29b0a12017-06-29 17:06:57 -0700638 DIFF_SYMBOL_HEADER,
Stefan Beller3ee8b7b2017-06-29 17:06:56 -0700639 DIFF_SYMBOL_FILEPAIR_PLUS,
640 DIFF_SYMBOL_FILEPAIR_MINUS,
Stefan Bellerff958672017-06-29 17:06:54 -0700641 DIFF_SYMBOL_WORDS_PORCELAIN,
642 DIFF_SYMBOL_WORDS,
Stefan Beller091f8e22017-06-29 17:06:53 -0700643 DIFF_SYMBOL_CONTEXT,
Stefan Bellerf2bb1212017-06-29 17:06:55 -0700644 DIFF_SYMBOL_CONTEXT_INCOMPLETE,
Stefan Beller091f8e22017-06-29 17:06:53 -0700645 DIFF_SYMBOL_PLUS,
646 DIFF_SYMBOL_MINUS,
Stefan Bellerb9cbfde2017-06-29 17:06:52 -0700647 DIFF_SYMBOL_NO_LF_EOF,
Stefan Beller68abc6f2017-06-29 17:06:51 -0700648 DIFF_SYMBOL_CONTEXT_FRAGINFO,
Stefan Bellerc64b4202017-06-29 17:06:50 -0700649 DIFF_SYMBOL_CONTEXT_MARKER,
Stefan Beller36a4cef2017-06-29 17:06:49 -0700650 DIFF_SYMBOL_SEPARATOR
651};
Stefan Beller091f8e22017-06-29 17:06:53 -0700652/*
653 * Flags for content lines:
654 * 0..12 are whitespace rules
655 * 13-15 are WSEH_NEW | WSEH_OLD | WSEH_CONTEXT
656 * 16 is marking if the line is blank at EOF
657 */
Stefan Beller2e2d5ac2017-06-30 13:53:07 -0700658#define DIFF_SYMBOL_CONTENT_BLANK_LINE_EOF (1<<16)
659#define DIFF_SYMBOL_MOVED_LINE (1<<17)
660#define DIFF_SYMBOL_MOVED_LINE_ALT (1<<18)
Stefan Beller86b452e2017-06-30 13:53:09 -0700661#define DIFF_SYMBOL_MOVED_LINE_UNINTERESTING (1<<19)
Stefan Beller091f8e22017-06-29 17:06:53 -0700662#define DIFF_SYMBOL_CONTENT_WS_MASK (WSEH_NEW | WSEH_OLD | WSEH_CONTEXT | WS_RULE_MASK)
663
Stefan Bellere6e045f2017-06-29 17:07:06 -0700664/*
665 * This struct is used when we need to buffer the output of the diff output.
666 *
667 * NEEDSWORK: Instead of storing a copy of the line, add an offset pointer
668 * into the pre/post image file. This pointer could be a union with the
669 * line pointer. By storing an offset into the file instead of the literal line,
670 * we can decrease the memory footprint for the buffered output. At first we
671 * may want to only have indirection for the content lines, but we could also
672 * enhance the state for emitting prefabricated lines, e.g. the similarity
673 * score line or hunk/file headers would only need to store a number or path
674 * and then the output can be constructed later on depending on state.
675 */
676struct emitted_diff_symbol {
677 const char *line;
678 int len;
679 int flags;
680 enum diff_symbol s;
681};
682#define EMITTED_DIFF_SYMBOL_INIT {NULL}
683
684struct emitted_diff_symbols {
685 struct emitted_diff_symbol *buf;
686 int nr, alloc;
687};
688#define EMITTED_DIFF_SYMBOLS_INIT {NULL, 0, 0}
689
690static void append_emitted_diff_symbol(struct diff_options *o,
691 struct emitted_diff_symbol *e)
692{
693 struct emitted_diff_symbol *f;
694
695 ALLOC_GROW(o->emitted_symbols->buf,
696 o->emitted_symbols->nr + 1,
697 o->emitted_symbols->alloc);
698 f = &o->emitted_symbols->buf[o->emitted_symbols->nr++];
699
700 memcpy(f, e, sizeof(struct emitted_diff_symbol));
701 f->line = e->line ? xmemdupz(e->line, e->len) : NULL;
702}
703
Stefan Beller2e2d5ac2017-06-30 13:53:07 -0700704struct moved_entry {
705 struct hashmap_entry ent;
706 const struct emitted_diff_symbol *es;
707 struct moved_entry *next_line;
708};
709
Stefan Beller2e2d5ac2017-06-30 13:53:07 -0700710static int moved_entry_cmp(const struct diff_options *diffopt,
711 const struct moved_entry *a,
712 const struct moved_entry *b,
713 const void *keydata)
714{
Stefan Beller01be97c2017-10-25 11:49:12 -0700715 return !xdiff_compare_lines(a->es->line, a->es->len,
716 b->es->line, b->es->len,
717 diffopt->xdl_opts);
Stefan Beller2e2d5ac2017-06-30 13:53:07 -0700718}
719
720static struct moved_entry *prepare_entry(struct diff_options *o,
721 int line_no)
722{
723 struct moved_entry *ret = xmalloc(sizeof(*ret));
724 struct emitted_diff_symbol *l = &o->emitted_symbols->buf[line_no];
725
Stefan Beller01be97c2017-10-25 11:49:12 -0700726 ret->ent.hash = xdiff_hash_string(l->line, l->len, o->xdl_opts);
Stefan Beller2e2d5ac2017-06-30 13:53:07 -0700727 ret->es = l;
728 ret->next_line = NULL;
729
730 return ret;
731}
732
733static void add_lines_to_move_detection(struct diff_options *o,
734 struct hashmap *add_lines,
735 struct hashmap *del_lines)
736{
737 struct moved_entry *prev_line = NULL;
738
739 int n;
740 for (n = 0; n < o->emitted_symbols->nr; n++) {
741 struct hashmap *hm;
742 struct moved_entry *key;
743
744 switch (o->emitted_symbols->buf[n].s) {
745 case DIFF_SYMBOL_PLUS:
746 hm = add_lines;
747 break;
748 case DIFF_SYMBOL_MINUS:
749 hm = del_lines;
750 break;
751 default:
752 prev_line = NULL;
753 continue;
754 }
755
756 key = prepare_entry(o, n);
757 if (prev_line && prev_line->es->s == o->emitted_symbols->buf[n].s)
758 prev_line->next_line = key;
759
760 hashmap_add(hm, key);
761 prev_line = key;
762 }
763}
764
765static int shrink_potential_moved_blocks(struct moved_entry **pmb,
766 int pmb_nr)
767{
768 int lp, rp;
769
770 /* Shrink the set of potential block to the remaining running */
771 for (lp = 0, rp = pmb_nr - 1; lp <= rp;) {
772 while (lp < pmb_nr && pmb[lp])
773 lp++;
774 /* lp points at the first NULL now */
775
776 while (rp > -1 && !pmb[rp])
777 rp--;
778 /* rp points at the last non-NULL */
779
780 if (lp < pmb_nr && rp > -1 && lp < rp) {
781 pmb[lp] = pmb[rp];
782 pmb[rp] = NULL;
783 rp--;
784 lp++;
785 }
786 }
787
788 /* Remember the number of running sets */
789 return rp + 1;
790}
791
Jonathan Tan09153272017-08-15 18:27:38 -0700792/*
793 * If o->color_moved is COLOR_MOVED_PLAIN, this function does nothing.
794 *
Jonathan Tanf0b8fb62017-08-15 18:27:39 -0700795 * Otherwise, if the last block has fewer alphanumeric characters than
796 * COLOR_MOVED_MIN_ALNUM_COUNT, unset DIFF_SYMBOL_MOVED_LINE on all lines in
Jonathan Tan09153272017-08-15 18:27:38 -0700797 * that block.
798 *
799 * The last block consists of the (n - block_length)'th line up to but not
800 * including the nth line.
Jonathan Tanf0b8fb62017-08-15 18:27:39 -0700801 *
802 * NEEDSWORK: This uses the same heuristic as blame_entry_score() in blame.c.
803 * Think of a way to unify them.
Jonathan Tan09153272017-08-15 18:27:38 -0700804 */
805static void adjust_last_block(struct diff_options *o, int n, int block_length)
806{
Jonathan Tanf0b8fb62017-08-15 18:27:39 -0700807 int i, alnum_count = 0;
808 if (o->color_moved == COLOR_MOVED_PLAIN)
Jonathan Tan09153272017-08-15 18:27:38 -0700809 return;
Jonathan Tanf0b8fb62017-08-15 18:27:39 -0700810 for (i = 1; i < block_length + 1; i++) {
811 const char *c = o->emitted_symbols->buf[n - i].line;
812 for (; *c; c++) {
813 if (!isalnum(*c))
814 continue;
815 alnum_count++;
816 if (alnum_count >= COLOR_MOVED_MIN_ALNUM_COUNT)
817 return;
818 }
819 }
Jonathan Tan09153272017-08-15 18:27:38 -0700820 for (i = 1; i < block_length + 1; i++)
821 o->emitted_symbols->buf[n - i].flags &= ~DIFF_SYMBOL_MOVED_LINE;
822}
823
Stefan Beller2e2d5ac2017-06-30 13:53:07 -0700824/* Find blocks of moved code, delegate actual coloring decision to helper */
825static void mark_color_as_moved(struct diff_options *o,
826 struct hashmap *add_lines,
827 struct hashmap *del_lines)
828{
829 struct moved_entry **pmb = NULL; /* potentially moved blocks */
830 int pmb_nr = 0, pmb_alloc = 0;
831 int n, flipped_block = 1, block_length = 0;
832
833
834 for (n = 0; n < o->emitted_symbols->nr; n++) {
835 struct hashmap *hm = NULL;
836 struct moved_entry *key;
837 struct moved_entry *match = NULL;
838 struct emitted_diff_symbol *l = &o->emitted_symbols->buf[n];
839 int i;
840
841 switch (l->s) {
842 case DIFF_SYMBOL_PLUS:
843 hm = del_lines;
844 key = prepare_entry(o, n);
845 match = hashmap_get(hm, key, o);
846 free(key);
847 break;
848 case DIFF_SYMBOL_MINUS:
849 hm = add_lines;
850 key = prepare_entry(o, n);
851 match = hashmap_get(hm, key, o);
852 free(key);
853 break;
854 default:
855 flipped_block = 1;
856 }
857
858 if (!match) {
Jonathan Tan09153272017-08-15 18:27:38 -0700859 adjust_last_block(o, n, block_length);
Stefan Beller2e2d5ac2017-06-30 13:53:07 -0700860 pmb_nr = 0;
861 block_length = 0;
862 continue;
863 }
864
865 l->flags |= DIFF_SYMBOL_MOVED_LINE;
Stefan Beller2e2d5ac2017-06-30 13:53:07 -0700866
Stefan Beller176841f2017-06-30 13:53:08 -0700867 if (o->color_moved == COLOR_MOVED_PLAIN)
868 continue;
869
Stefan Beller2e2d5ac2017-06-30 13:53:07 -0700870 /* Check any potential block runs, advance each or nullify */
871 for (i = 0; i < pmb_nr; i++) {
872 struct moved_entry *p = pmb[i];
873 struct moved_entry *pnext = (p && p->next_line) ?
874 p->next_line : NULL;
875 if (pnext && !hm->cmpfn(o, pnext, match, NULL)) {
876 pmb[i] = p->next_line;
877 } else {
878 pmb[i] = NULL;
879 }
880 }
881
882 pmb_nr = shrink_potential_moved_blocks(pmb, pmb_nr);
883
884 if (pmb_nr == 0) {
885 /*
886 * The current line is the start of a new block.
887 * Setup the set of potential blocks.
888 */
889 for (; match; match = hashmap_get_next(hm, match)) {
890 ALLOC_GROW(pmb, pmb_nr + 1, pmb_alloc);
891 pmb[pmb_nr++] = match;
892 }
893
894 flipped_block = (flipped_block + 1) % 2;
Jonathan Tanf0b8fb62017-08-15 18:27:39 -0700895
896 adjust_last_block(o, n, block_length);
897 block_length = 0;
Stefan Beller2e2d5ac2017-06-30 13:53:07 -0700898 }
899
Jonathan Tanf0b8fb62017-08-15 18:27:39 -0700900 block_length++;
901
Stefan Beller2e2d5ac2017-06-30 13:53:07 -0700902 if (flipped_block)
903 l->flags |= DIFF_SYMBOL_MOVED_LINE_ALT;
904 }
Jonathan Tan09153272017-08-15 18:27:38 -0700905 adjust_last_block(o, n, block_length);
Stefan Beller2e2d5ac2017-06-30 13:53:07 -0700906
907 free(pmb);
908}
Stefan Bellere6e045f2017-06-29 17:07:06 -0700909
Stefan Beller86b452e2017-06-30 13:53:09 -0700910#define DIFF_SYMBOL_MOVED_LINE_ZEBRA_MASK \
911 (DIFF_SYMBOL_MOVED_LINE | DIFF_SYMBOL_MOVED_LINE_ALT)
912static void dim_moved_lines(struct diff_options *o)
913{
914 int n;
915 for (n = 0; n < o->emitted_symbols->nr; n++) {
916 struct emitted_diff_symbol *prev = (n != 0) ?
917 &o->emitted_symbols->buf[n - 1] : NULL;
918 struct emitted_diff_symbol *l = &o->emitted_symbols->buf[n];
919 struct emitted_diff_symbol *next =
920 (n < o->emitted_symbols->nr - 1) ?
921 &o->emitted_symbols->buf[n + 1] : NULL;
922
923 /* Not a plus or minus line? */
924 if (l->s != DIFF_SYMBOL_PLUS && l->s != DIFF_SYMBOL_MINUS)
925 continue;
926
927 /* Not a moved line? */
928 if (!(l->flags & DIFF_SYMBOL_MOVED_LINE))
929 continue;
930
931 /*
932 * If prev or next are not a plus or minus line,
933 * pretend they don't exist
934 */
935 if (prev && prev->s != DIFF_SYMBOL_PLUS &&
936 prev->s != DIFF_SYMBOL_MINUS)
937 prev = NULL;
938 if (next && next->s != DIFF_SYMBOL_PLUS &&
939 next->s != DIFF_SYMBOL_MINUS)
940 next = NULL;
941
942 /* Inside a block? */
943 if ((prev &&
944 (prev->flags & DIFF_SYMBOL_MOVED_LINE_ZEBRA_MASK) ==
945 (l->flags & DIFF_SYMBOL_MOVED_LINE_ZEBRA_MASK)) &&
946 (next &&
947 (next->flags & DIFF_SYMBOL_MOVED_LINE_ZEBRA_MASK) ==
948 (l->flags & DIFF_SYMBOL_MOVED_LINE_ZEBRA_MASK))) {
949 l->flags |= DIFF_SYMBOL_MOVED_LINE_UNINTERESTING;
950 continue;
951 }
952
953 /* Check if we are at an interesting bound: */
954 if (prev && (prev->flags & DIFF_SYMBOL_MOVED_LINE) &&
955 (prev->flags & DIFF_SYMBOL_MOVED_LINE_ALT) !=
956 (l->flags & DIFF_SYMBOL_MOVED_LINE_ALT))
957 continue;
958 if (next && (next->flags & DIFF_SYMBOL_MOVED_LINE) &&
959 (next->flags & DIFF_SYMBOL_MOVED_LINE_ALT) !=
960 (l->flags & DIFF_SYMBOL_MOVED_LINE_ALT))
961 continue;
962
963 /*
964 * The boundary to prev and next are not interesting,
965 * so this line is not interesting as a whole
966 */
967 l->flags |= DIFF_SYMBOL_MOVED_LINE_UNINTERESTING;
968 }
969}
970
Stefan Beller091f8e22017-06-29 17:06:53 -0700971static void emit_line_ws_markup(struct diff_options *o,
972 const char *set, const char *reset,
973 const char *line, int len, char sign,
974 unsigned ws_rule, int blank_at_eof)
975{
976 const char *ws = NULL;
977
978 if (o->ws_error_highlight & ws_rule) {
979 ws = diff_get_color_opt(o, DIFF_WHITESPACE);
980 if (!*ws)
981 ws = NULL;
982 }
983
984 if (!ws)
985 emit_line_0(o, set, reset, sign, line, len);
986 else if (blank_at_eof)
987 /* Blank line at EOF - paint '+' as well */
988 emit_line_0(o, ws, reset, sign, line, len);
989 else {
990 /* Emit just the prefix, then the rest. */
991 emit_line_0(o, set, reset, sign, "", 0);
992 ws_check_emit(line, len, ws_rule,
993 o->file, set, reset, ws);
994 }
995}
Stefan Beller36a4cef2017-06-29 17:06:49 -0700996
Stefan Bellere6e045f2017-06-29 17:07:06 -0700997static void emit_diff_symbol_from_struct(struct diff_options *o,
998 struct emitted_diff_symbol *eds)
Stefan Beller36a4cef2017-06-29 17:06:49 -0700999{
Stefan Bellerb9cbfde2017-06-29 17:06:52 -07001000 static const char *nneof = " No newline at end of file\n";
Stefan Beller5af6ea92017-06-29 17:06:59 -07001001 const char *context, *reset, *set, *meta, *fraginfo;
Stefan Beller0911c472017-06-29 17:07:02 -07001002 struct strbuf sb = STRBUF_INIT;
Stefan Bellere6e045f2017-06-29 17:07:06 -07001003
1004 enum diff_symbol s = eds->s;
1005 const char *line = eds->line;
1006 int len = eds->len;
1007 unsigned flags = eds->flags;
1008
Stefan Beller36a4cef2017-06-29 17:06:49 -07001009 switch (s) {
Stefan Bellerb9cbfde2017-06-29 17:06:52 -07001010 case DIFF_SYMBOL_NO_LF_EOF:
1011 context = diff_get_color_opt(o, DIFF_CONTEXT);
1012 reset = diff_get_color_opt(o, DIFF_RESET);
1013 putc('\n', o->file);
1014 emit_line_0(o, context, reset, '\\',
1015 nneof, strlen(nneof));
1016 break;
Stefan Bellerf3597132017-06-29 17:07:00 -07001017 case DIFF_SYMBOL_SUBMODULE_HEADER:
1018 case DIFF_SYMBOL_SUBMODULE_ERROR:
1019 case DIFF_SYMBOL_SUBMODULE_PIPETHROUGH:
Stefan Beller0911c472017-06-29 17:07:02 -07001020 case DIFF_SYMBOL_STATS_SUMMARY_INSERTS_DELETES:
Stefan Beller146fdb02017-06-29 17:07:05 -07001021 case DIFF_SYMBOL_SUMMARY:
Stefan Beller0911c472017-06-29 17:07:02 -07001022 case DIFF_SYMBOL_STATS_LINE:
Stefan Beller4eed0eb2017-06-29 17:07:01 -07001023 case DIFF_SYMBOL_BINARY_DIFF_BODY:
Stefan Beller68abc6f2017-06-29 17:06:51 -07001024 case DIFF_SYMBOL_CONTEXT_FRAGINFO:
1025 emit_line(o, "", "", line, len);
1026 break;
Stefan Bellerf2bb1212017-06-29 17:06:55 -07001027 case DIFF_SYMBOL_CONTEXT_INCOMPLETE:
Stefan Bellerc64b4202017-06-29 17:06:50 -07001028 case DIFF_SYMBOL_CONTEXT_MARKER:
1029 context = diff_get_color_opt(o, DIFF_CONTEXT);
1030 reset = diff_get_color_opt(o, DIFF_RESET);
1031 emit_line(o, context, reset, line, len);
1032 break;
Stefan Beller36a4cef2017-06-29 17:06:49 -07001033 case DIFF_SYMBOL_SEPARATOR:
1034 fprintf(o->file, "%s%c",
1035 diff_line_prefix(o),
1036 o->line_termination);
1037 break;
Stefan Beller091f8e22017-06-29 17:06:53 -07001038 case DIFF_SYMBOL_CONTEXT:
1039 set = diff_get_color_opt(o, DIFF_CONTEXT);
1040 reset = diff_get_color_opt(o, DIFF_RESET);
1041 emit_line_ws_markup(o, set, reset, line, len, ' ',
1042 flags & (DIFF_SYMBOL_CONTENT_WS_MASK), 0);
1043 break;
1044 case DIFF_SYMBOL_PLUS:
Stefan Beller86b452e2017-06-30 13:53:09 -07001045 switch (flags & (DIFF_SYMBOL_MOVED_LINE |
1046 DIFF_SYMBOL_MOVED_LINE_ALT |
1047 DIFF_SYMBOL_MOVED_LINE_UNINTERESTING)) {
1048 case DIFF_SYMBOL_MOVED_LINE |
1049 DIFF_SYMBOL_MOVED_LINE_ALT |
1050 DIFF_SYMBOL_MOVED_LINE_UNINTERESTING:
1051 set = diff_get_color_opt(o, DIFF_FILE_NEW_MOVED_ALT_DIM);
1052 break;
1053 case DIFF_SYMBOL_MOVED_LINE |
1054 DIFF_SYMBOL_MOVED_LINE_ALT:
Stefan Beller2e2d5ac2017-06-30 13:53:07 -07001055 set = diff_get_color_opt(o, DIFF_FILE_NEW_MOVED_ALT);
Stefan Beller86b452e2017-06-30 13:53:09 -07001056 break;
1057 case DIFF_SYMBOL_MOVED_LINE |
1058 DIFF_SYMBOL_MOVED_LINE_UNINTERESTING:
1059 set = diff_get_color_opt(o, DIFF_FILE_NEW_MOVED_DIM);
1060 break;
1061 case DIFF_SYMBOL_MOVED_LINE:
Stefan Beller2e2d5ac2017-06-30 13:53:07 -07001062 set = diff_get_color_opt(o, DIFF_FILE_NEW_MOVED);
Stefan Beller86b452e2017-06-30 13:53:09 -07001063 break;
1064 default:
Stefan Beller2e2d5ac2017-06-30 13:53:07 -07001065 set = diff_get_color_opt(o, DIFF_FILE_NEW);
Stefan Beller86b452e2017-06-30 13:53:09 -07001066 }
Stefan Beller091f8e22017-06-29 17:06:53 -07001067 reset = diff_get_color_opt(o, DIFF_RESET);
1068 emit_line_ws_markup(o, set, reset, line, len, '+',
1069 flags & DIFF_SYMBOL_CONTENT_WS_MASK,
1070 flags & DIFF_SYMBOL_CONTENT_BLANK_LINE_EOF);
1071 break;
1072 case DIFF_SYMBOL_MINUS:
Stefan Beller86b452e2017-06-30 13:53:09 -07001073 switch (flags & (DIFF_SYMBOL_MOVED_LINE |
1074 DIFF_SYMBOL_MOVED_LINE_ALT |
1075 DIFF_SYMBOL_MOVED_LINE_UNINTERESTING)) {
1076 case DIFF_SYMBOL_MOVED_LINE |
1077 DIFF_SYMBOL_MOVED_LINE_ALT |
1078 DIFF_SYMBOL_MOVED_LINE_UNINTERESTING:
1079 set = diff_get_color_opt(o, DIFF_FILE_OLD_MOVED_ALT_DIM);
1080 break;
1081 case DIFF_SYMBOL_MOVED_LINE |
1082 DIFF_SYMBOL_MOVED_LINE_ALT:
Stefan Beller2e2d5ac2017-06-30 13:53:07 -07001083 set = diff_get_color_opt(o, DIFF_FILE_OLD_MOVED_ALT);
Stefan Beller86b452e2017-06-30 13:53:09 -07001084 break;
1085 case DIFF_SYMBOL_MOVED_LINE |
1086 DIFF_SYMBOL_MOVED_LINE_UNINTERESTING:
1087 set = diff_get_color_opt(o, DIFF_FILE_OLD_MOVED_DIM);
1088 break;
1089 case DIFF_SYMBOL_MOVED_LINE:
Stefan Beller2e2d5ac2017-06-30 13:53:07 -07001090 set = diff_get_color_opt(o, DIFF_FILE_OLD_MOVED);
Stefan Beller86b452e2017-06-30 13:53:09 -07001091 break;
1092 default:
Stefan Beller2e2d5ac2017-06-30 13:53:07 -07001093 set = diff_get_color_opt(o, DIFF_FILE_OLD);
Stefan Beller86b452e2017-06-30 13:53:09 -07001094 }
Stefan Beller091f8e22017-06-29 17:06:53 -07001095 reset = diff_get_color_opt(o, DIFF_RESET);
1096 emit_line_ws_markup(o, set, reset, line, len, '-',
1097 flags & DIFF_SYMBOL_CONTENT_WS_MASK, 0);
1098 break;
Stefan Bellerff958672017-06-29 17:06:54 -07001099 case DIFF_SYMBOL_WORDS_PORCELAIN:
1100 context = diff_get_color_opt(o, DIFF_CONTEXT);
1101 reset = diff_get_color_opt(o, DIFF_RESET);
1102 emit_line(o, context, reset, line, len);
1103 fputs("~\n", o->file);
1104 break;
1105 case DIFF_SYMBOL_WORDS:
1106 context = diff_get_color_opt(o, DIFF_CONTEXT);
1107 reset = diff_get_color_opt(o, DIFF_RESET);
1108 /*
1109 * Skip the prefix character, if any. With
1110 * diff_suppress_blank_empty, there may be
1111 * none.
1112 */
1113 if (line[0] != '\n') {
1114 line++;
1115 len--;
1116 }
1117 emit_line(o, context, reset, line, len);
1118 break;
Stefan Beller3ee8b7b2017-06-29 17:06:56 -07001119 case DIFF_SYMBOL_FILEPAIR_PLUS:
1120 meta = diff_get_color_opt(o, DIFF_METAINFO);
1121 reset = diff_get_color_opt(o, DIFF_RESET);
1122 fprintf(o->file, "%s%s+++ %s%s%s\n", diff_line_prefix(o), meta,
1123 line, reset,
1124 strchr(line, ' ') ? "\t" : "");
1125 break;
1126 case DIFF_SYMBOL_FILEPAIR_MINUS:
1127 meta = diff_get_color_opt(o, DIFF_METAINFO);
1128 reset = diff_get_color_opt(o, DIFF_RESET);
1129 fprintf(o->file, "%s%s--- %s%s%s\n", diff_line_prefix(o), meta,
1130 line, reset,
1131 strchr(line, ' ') ? "\t" : "");
1132 break;
Stefan Beller4acaaa72017-06-29 17:06:58 -07001133 case DIFF_SYMBOL_BINARY_FILES:
Stefan Bellera29b0a12017-06-29 17:06:57 -07001134 case DIFF_SYMBOL_HEADER:
1135 fprintf(o->file, "%s", line);
1136 break;
Stefan Beller4eed0eb2017-06-29 17:07:01 -07001137 case DIFF_SYMBOL_BINARY_DIFF_HEADER:
1138 fprintf(o->file, "%sGIT binary patch\n", diff_line_prefix(o));
1139 break;
1140 case DIFF_SYMBOL_BINARY_DIFF_HEADER_DELTA:
1141 fprintf(o->file, "%sdelta %s\n", diff_line_prefix(o), line);
1142 break;
1143 case DIFF_SYMBOL_BINARY_DIFF_HEADER_LITERAL:
1144 fprintf(o->file, "%sliteral %s\n", diff_line_prefix(o), line);
1145 break;
1146 case DIFF_SYMBOL_BINARY_DIFF_FOOTER:
1147 fputs(diff_line_prefix(o), o->file);
1148 fputc('\n', o->file);
1149 break;
Stefan Beller5af6ea92017-06-29 17:06:59 -07001150 case DIFF_SYMBOL_REWRITE_DIFF:
1151 fraginfo = diff_get_color(o->use_color, DIFF_FRAGINFO);
1152 reset = diff_get_color_opt(o, DIFF_RESET);
1153 emit_line(o, fraginfo, reset, line, len);
1154 break;
Stefan Bellerf3597132017-06-29 17:07:00 -07001155 case DIFF_SYMBOL_SUBMODULE_ADD:
1156 set = diff_get_color_opt(o, DIFF_FILE_NEW);
1157 reset = diff_get_color_opt(o, DIFF_RESET);
1158 emit_line(o, set, reset, line, len);
1159 break;
1160 case DIFF_SYMBOL_SUBMODULE_DEL:
1161 set = diff_get_color_opt(o, DIFF_FILE_OLD);
1162 reset = diff_get_color_opt(o, DIFF_RESET);
1163 emit_line(o, set, reset, line, len);
1164 break;
1165 case DIFF_SYMBOL_SUBMODULE_UNTRACKED:
1166 fprintf(o->file, "%sSubmodule %s contains untracked content\n",
1167 diff_line_prefix(o), line);
1168 break;
1169 case DIFF_SYMBOL_SUBMODULE_MODIFIED:
1170 fprintf(o->file, "%sSubmodule %s contains modified content\n",
1171 diff_line_prefix(o), line);
1172 break;
Stefan Beller0911c472017-06-29 17:07:02 -07001173 case DIFF_SYMBOL_STATS_SUMMARY_NO_FILES:
1174 emit_line(o, "", "", " 0 files changed\n",
1175 strlen(" 0 files changed\n"));
1176 break;
1177 case DIFF_SYMBOL_STATS_SUMMARY_ABBREV:
1178 emit_line(o, "", "", " ...\n", strlen(" ...\n"));
1179 break;
Stefan Bellerbd033292017-06-29 17:07:03 -07001180 case DIFF_SYMBOL_WORD_DIFF:
1181 fprintf(o->file, "%.*s", len, line);
1182 break;
Stefan Beller30b7e1e2017-06-29 17:07:04 -07001183 case DIFF_SYMBOL_STAT_SEP:
1184 fputs(o->stat_sep, o->file);
1185 break;
Stefan Beller36a4cef2017-06-29 17:06:49 -07001186 default:
1187 die("BUG: unknown diff symbol");
1188 }
Stefan Beller0911c472017-06-29 17:07:02 -07001189 strbuf_release(&sb);
Stefan Beller36a4cef2017-06-29 17:06:49 -07001190}
1191
Stefan Bellere6e045f2017-06-29 17:07:06 -07001192static void emit_diff_symbol(struct diff_options *o, enum diff_symbol s,
1193 const char *line, int len, unsigned flags)
1194{
1195 struct emitted_diff_symbol e = {line, len, flags, s};
1196
1197 if (o->emitted_symbols)
1198 append_emitted_diff_symbol(o, &e);
1199 else
1200 emit_diff_symbol_from_struct(o, &e);
1201}
1202
Stefan Bellerf3597132017-06-29 17:07:00 -07001203void diff_emit_submodule_del(struct diff_options *o, const char *line)
1204{
1205 emit_diff_symbol(o, DIFF_SYMBOL_SUBMODULE_DEL, line, strlen(line), 0);
1206}
1207
1208void diff_emit_submodule_add(struct diff_options *o, const char *line)
1209{
1210 emit_diff_symbol(o, DIFF_SYMBOL_SUBMODULE_ADD, line, strlen(line), 0);
1211}
1212
1213void diff_emit_submodule_untracked(struct diff_options *o, const char *path)
1214{
1215 emit_diff_symbol(o, DIFF_SYMBOL_SUBMODULE_UNTRACKED,
1216 path, strlen(path), 0);
1217}
1218
1219void diff_emit_submodule_modified(struct diff_options *o, const char *path)
1220{
1221 emit_diff_symbol(o, DIFF_SYMBOL_SUBMODULE_MODIFIED,
1222 path, strlen(path), 0);
1223}
1224
1225void diff_emit_submodule_header(struct diff_options *o, const char *header)
1226{
1227 emit_diff_symbol(o, DIFF_SYMBOL_SUBMODULE_HEADER,
1228 header, strlen(header), 0);
1229}
1230
1231void diff_emit_submodule_error(struct diff_options *o, const char *err)
1232{
1233 emit_diff_symbol(o, DIFF_SYMBOL_SUBMODULE_ERROR, err, strlen(err), 0);
1234}
1235
1236void diff_emit_submodule_pipethrough(struct diff_options *o,
1237 const char *line, int len)
1238{
1239 emit_diff_symbol(o, DIFF_SYMBOL_SUBMODULE_PIPETHROUGH, line, len, 0);
1240}
1241
Junio C Hamano6957eb92009-09-14 18:44:01 -07001242static int new_blank_line_at_eof(struct emit_callback *ecbdata, const char *line, int len)
1243{
1244 if (!((ecbdata->ws_rule & WS_BLANK_AT_EOF) &&
1245 ecbdata->blank_at_eof_in_preimage &&
1246 ecbdata->blank_at_eof_in_postimage &&
1247 ecbdata->blank_at_eof_in_preimage <= ecbdata->lno_in_preimage &&
1248 ecbdata->blank_at_eof_in_postimage <= ecbdata->lno_in_postimage))
1249 return 0;
Junio C Hamano018cff72009-09-14 18:44:01 -07001250 return ws_blank_line(line, len, ecbdata->ws_rule);
Junio C Hamano6957eb92009-09-14 18:44:01 -07001251}
1252
Junio C Hamano018cff72009-09-14 18:44:01 -07001253static void emit_add_line(const char *reset,
1254 struct emit_callback *ecbdata,
1255 const char *line, int len)
Junio C Hamano6957eb92009-09-14 18:44:01 -07001256{
Stefan Beller091f8e22017-06-29 17:06:53 -07001257 unsigned flags = WSEH_NEW | ecbdata->ws_rule;
1258 if (new_blank_line_at_eof(ecbdata, line, len))
1259 flags |= DIFF_SYMBOL_CONTENT_BLANK_LINE_EOF;
1260
1261 emit_diff_symbol(ecbdata->opt, DIFF_SYMBOL_PLUS, line, len, flags);
Junio C Hamano6957eb92009-09-14 18:44:01 -07001262}
Junio C Hamano6957eb92009-09-14 18:44:01 -07001263
Junio C Hamano0e383e12015-05-26 09:56:33 -07001264static void emit_del_line(const char *reset,
1265 struct emit_callback *ecbdata,
1266 const char *line, int len)
1267{
Stefan Beller091f8e22017-06-29 17:06:53 -07001268 unsigned flags = WSEH_OLD | ecbdata->ws_rule;
1269 emit_diff_symbol(ecbdata->opt, DIFF_SYMBOL_MINUS, line, len, flags);
Junio C Hamano0e383e12015-05-26 09:56:33 -07001270}
1271
1272static void emit_context_line(const char *reset,
1273 struct emit_callback *ecbdata,
1274 const char *line, int len)
1275{
Stefan Beller091f8e22017-06-29 17:06:53 -07001276 unsigned flags = WSEH_CONTEXT | ecbdata->ws_rule;
1277 emit_diff_symbol(ecbdata->opt, DIFF_SYMBOL_CONTEXT, line, len, flags);
Junio C Hamano6957eb92009-09-14 18:44:01 -07001278}
1279
Bert Wesarg89cb73a2009-11-27 07:55:18 +01001280static void emit_hunk_header(struct emit_callback *ecbdata,
1281 const char *line, int len)
1282{
Jeff King8dbf3eb2015-05-27 16:48:46 -04001283 const char *context = diff_get_color(ecbdata->color_diff, DIFF_CONTEXT);
Bert Wesarg89cb73a2009-11-27 07:55:18 +01001284 const char *frag = diff_get_color(ecbdata->color_diff, DIFF_FRAGINFO);
1285 const char *func = diff_get_color(ecbdata->color_diff, DIFF_FUNCINFO);
1286 const char *reset = diff_get_color(ecbdata->color_diff, DIFF_RESET);
1287 static const char atat[2] = { '@', '@' };
1288 const char *cp, *ep;
Bo Yang2efcc972010-05-29 23:32:05 +08001289 struct strbuf msgbuf = STRBUF_INIT;
1290 int org_len = len;
1291 int i = 1;
Bert Wesarg89cb73a2009-11-27 07:55:18 +01001292
1293 /*
1294 * As a hunk header must begin with "@@ -<old>, +<new> @@",
1295 * it always is at least 10 bytes long.
1296 */
1297 if (len < 10 ||
1298 memcmp(line, atat, 2) ||
1299 !(ep = memmem(line + 2, len - 2, atat, 2))) {
Stefan Bellerc64b4202017-06-29 17:06:50 -07001300 emit_diff_symbol(ecbdata->opt,
Stefan Beller091f8e22017-06-29 17:06:53 -07001301 DIFF_SYMBOL_CONTEXT_MARKER, line, len, 0);
Bert Wesarg89cb73a2009-11-27 07:55:18 +01001302 return;
1303 }
1304 ep += 2; /* skip over @@ */
1305
1306 /* The hunk header in fraginfo color */
René Scharfecedc61a2014-07-17 01:38:18 +02001307 strbuf_addstr(&msgbuf, frag);
Bo Yang2efcc972010-05-29 23:32:05 +08001308 strbuf_add(&msgbuf, line, ep - line);
René Scharfecedc61a2014-07-17 01:38:18 +02001309 strbuf_addstr(&msgbuf, reset);
Bo Yang2efcc972010-05-29 23:32:05 +08001310
1311 /*
1312 * trailing "\r\n"
1313 */
1314 for ( ; i < 3; i++)
1315 if (line[len - i] == '\r' || line[len - i] == '\n')
1316 len--;
Bert Wesarg89cb73a2009-11-27 07:55:18 +01001317
1318 /* blank before the func header */
1319 for (cp = ep; ep - line < len; ep++)
1320 if (*ep != ' ' && *ep != '\t')
1321 break;
Bo Yang2efcc972010-05-29 23:32:05 +08001322 if (ep != cp) {
Jeff King8dbf3eb2015-05-27 16:48:46 -04001323 strbuf_addstr(&msgbuf, context);
Bo Yang2efcc972010-05-29 23:32:05 +08001324 strbuf_add(&msgbuf, cp, ep - cp);
René Scharfecedc61a2014-07-17 01:38:18 +02001325 strbuf_addstr(&msgbuf, reset);
Bo Yang2efcc972010-05-29 23:32:05 +08001326 }
Bert Wesarg89cb73a2009-11-27 07:55:18 +01001327
Bo Yang2efcc972010-05-29 23:32:05 +08001328 if (ep < line + len) {
René Scharfecedc61a2014-07-17 01:38:18 +02001329 strbuf_addstr(&msgbuf, func);
Bo Yang2efcc972010-05-29 23:32:05 +08001330 strbuf_add(&msgbuf, ep, line + len - ep);
René Scharfecedc61a2014-07-17 01:38:18 +02001331 strbuf_addstr(&msgbuf, reset);
Bo Yang2efcc972010-05-29 23:32:05 +08001332 }
1333
1334 strbuf_add(&msgbuf, line + len, org_len - len);
Stefan Bellerdfb77282017-06-29 17:06:47 -07001335 strbuf_complete_line(&msgbuf);
Stefan Beller68abc6f2017-06-29 17:06:51 -07001336 emit_diff_symbol(ecbdata->opt,
Stefan Beller091f8e22017-06-29 17:06:53 -07001337 DIFF_SYMBOL_CONTEXT_FRAGINFO, msgbuf.buf, msgbuf.len, 0);
Bo Yang2efcc972010-05-29 23:32:05 +08001338 strbuf_release(&msgbuf);
Bert Wesarg89cb73a2009-11-27 07:55:18 +01001339}
1340
Jeff King479b0ae2009-01-22 00:59:56 -05001341static struct diff_tempfile *claim_diff_tempfile(void) {
1342 int i;
1343 for (i = 0; i < ARRAY_SIZE(diff_temp); i++)
1344 if (!diff_temp[i].name)
1345 return diff_temp + i;
1346 die("BUG: diff is failing to clean up its tempfiles");
1347}
1348
Jeff King479b0ae2009-01-22 00:59:56 -05001349static void remove_tempfile(void)
1350{
1351 int i;
Nazri Ramliya8344ab2009-02-12 21:36:14 +08001352 for (i = 0; i < ARRAY_SIZE(diff_temp); i++) {
Jeff King076aa2c2017-09-05 08:15:08 -04001353 if (is_tempfile_active(diff_temp[i].tempfile))
Michael Haggerty284098f2015-08-12 19:12:01 +02001354 delete_tempfile(&diff_temp[i].tempfile);
Nazri Ramliya8344ab2009-02-12 21:36:14 +08001355 diff_temp[i].name = NULL;
1356 }
Jeff King479b0ae2009-01-22 00:59:56 -05001357}
1358
Stefan Beller5af6ea92017-06-29 17:06:59 -07001359static void add_line_count(struct strbuf *out, int count)
Junio C Hamano6973dca2006-04-21 23:57:45 -07001360{
1361 switch (count) {
1362 case 0:
Stefan Beller5af6ea92017-06-29 17:06:59 -07001363 strbuf_addstr(out, "0,0");
Junio C Hamano6973dca2006-04-21 23:57:45 -07001364 break;
1365 case 1:
Stefan Beller5af6ea92017-06-29 17:06:59 -07001366 strbuf_addstr(out, "1");
Junio C Hamano6973dca2006-04-21 23:57:45 -07001367 break;
1368 default:
Stefan Beller5af6ea92017-06-29 17:06:59 -07001369 strbuf_addf(out, "1,%d", count);
Junio C Hamano6973dca2006-04-21 23:57:45 -07001370 break;
1371 }
1372}
1373
Junio C Hamano7f7ee2f2009-09-14 18:44:01 -07001374static void emit_rewrite_lines(struct emit_callback *ecb,
1375 int prefix, const char *data, int size)
Junio C Hamano6973dca2006-04-21 23:57:45 -07001376{
Junio C Hamano7f7ee2f2009-09-14 18:44:01 -07001377 const char *endp = NULL;
Junio C Hamano7f7ee2f2009-09-14 18:44:01 -07001378 const char *reset = diff_get_color(ecb->color_diff, DIFF_RESET);
1379
1380 while (0 < size) {
1381 int len;
1382
1383 endp = memchr(data, '\n', size);
1384 len = endp ? (endp - data + 1) : size;
1385 if (prefix != '+') {
1386 ecb->lno_in_preimage++;
Junio C Hamano0e383e12015-05-26 09:56:33 -07001387 emit_del_line(reset, ecb, data, len);
Junio C Hamano7f7ee2f2009-09-14 18:44:01 -07001388 } else {
1389 ecb->lno_in_postimage++;
1390 emit_add_line(reset, ecb, data, len);
Johannes Schindelin13e36ec2007-02-20 15:08:46 +01001391 }
Junio C Hamano7f7ee2f2009-09-14 18:44:01 -07001392 size -= len;
1393 data += len;
Junio C Hamano6973dca2006-04-21 23:57:45 -07001394 }
Stefan Bellerb9cbfde2017-06-29 17:06:52 -07001395 if (!endp)
Stefan Beller091f8e22017-06-29 17:06:53 -07001396 emit_diff_symbol(ecb->opt, DIFF_SYMBOL_NO_LF_EOF, NULL, 0, 0);
Junio C Hamano6973dca2006-04-21 23:57:45 -07001397}
1398
1399static void emit_rewrite_diff(const char *name_a,
1400 const char *name_b,
1401 struct diff_filespec *one,
Johannes Schindelin13e36ec2007-02-20 15:08:46 +01001402 struct diff_filespec *two,
Jeff Kingd9bae1a2010-04-01 20:12:15 -04001403 struct userdiff_driver *textconv_one,
1404 struct userdiff_driver *textconv_two,
Johannes Schindelineab9a402007-12-18 19:32:14 +00001405 struct diff_options *o)
Junio C Hamano6973dca2006-04-21 23:57:45 -07001406{
1407 int lc_a, lc_b;
Junio C Hamanod5625092007-12-26 17:13:36 -08001408 static struct strbuf a_name = STRBUF_INIT, b_name = STRBUF_INIT;
Junio C Hamanoa5a818e2008-08-18 20:08:09 -07001409 const char *a_prefix, *b_prefix;
Jeff King840383b2010-04-01 20:09:26 -04001410 char *data_one, *data_two;
Jeff King3aa1f7c2008-12-09 03:13:21 -05001411 size_t size_one, size_two;
Junio C Hamano7f7ee2f2009-09-14 18:44:01 -07001412 struct emit_callback ecbdata;
Stefan Beller5af6ea92017-06-29 17:06:59 -07001413 struct strbuf out = STRBUF_INIT;
Junio C Hamanoa5a818e2008-08-18 20:08:09 -07001414
Brandon Williams0d1e0e72017-10-31 11:19:11 -07001415 if (diff_mnemonic_prefix && o->flags.reverse_diff) {
Junio C Hamanoa5a818e2008-08-18 20:08:09 -07001416 a_prefix = o->b_prefix;
1417 b_prefix = o->a_prefix;
1418 } else {
1419 a_prefix = o->a_prefix;
1420 b_prefix = o->b_prefix;
1421 }
Junio C Hamano1a9eb3b2006-09-22 16:17:58 -07001422
Junio C Hamano8a13bec2007-02-24 01:42:06 -08001423 name_a += (*name_a == '/');
1424 name_b += (*name_b == '/');
Junio C Hamano1a9eb3b2006-09-22 16:17:58 -07001425
Junio C Hamanod5625092007-12-26 17:13:36 -08001426 strbuf_reset(&a_name);
1427 strbuf_reset(&b_name);
Junio C Hamanoa5a818e2008-08-18 20:08:09 -07001428 quote_two_c_style(&a_name, a_prefix, name_a, 0);
1429 quote_two_c_style(&b_name, b_prefix, name_b, 0);
Junio C Hamanod5625092007-12-26 17:13:36 -08001430
Jeff King840383b2010-04-01 20:09:26 -04001431 size_one = fill_textconv(textconv_one, one, &data_one);
1432 size_two = fill_textconv(textconv_two, two, &data_two);
Jeff King3aa1f7c2008-12-09 03:13:21 -05001433
Junio C Hamanod91ba8f2009-09-15 11:21:10 -07001434 memset(&ecbdata, 0, sizeof(ecbdata));
Jeff Kingdaa0c3d2011-08-17 22:04:23 -07001435 ecbdata.color_diff = want_color(o->use_color);
Stefan Bellerc189c4f2013-08-08 20:31:44 +02001436 ecbdata.ws_rule = whitespace_rule(name_b);
Bo Yanga3c158d2010-05-26 15:08:02 +08001437 ecbdata.opt = o;
Junio C Hamanod91ba8f2009-09-15 11:21:10 -07001438 if (ecbdata.ws_rule & WS_BLANK_AT_EOF) {
1439 mmfile_t mf1, mf2;
1440 mf1.ptr = (char *)data_one;
1441 mf2.ptr = (char *)data_two;
1442 mf1.size = size_one;
1443 mf2.size = size_two;
1444 check_blank_at_eof(&mf1, &mf2, &ecbdata);
1445 }
1446 ecbdata.lno_in_preimage = 1;
1447 ecbdata.lno_in_postimage = 1;
1448
Jeff King3aa1f7c2008-12-09 03:13:21 -05001449 lc_a = count_lines(data_one, size_one);
1450 lc_b = count_lines(data_two, size_two);
Stefan Beller3ee8b7b2017-06-29 17:06:56 -07001451
1452 emit_diff_symbol(o, DIFF_SYMBOL_FILEPAIR_MINUS,
1453 a_name.buf, a_name.len, 0);
1454 emit_diff_symbol(o, DIFF_SYMBOL_FILEPAIR_PLUS,
1455 b_name.buf, b_name.len, 0);
1456
Stefan Beller5af6ea92017-06-29 17:06:59 -07001457 strbuf_addstr(&out, "@@ -");
Junio C Hamano467ddc12011-02-28 16:11:55 -08001458 if (!o->irreversible_delete)
Stefan Beller5af6ea92017-06-29 17:06:59 -07001459 add_line_count(&out, lc_a);
Junio C Hamano467ddc12011-02-28 16:11:55 -08001460 else
Stefan Beller5af6ea92017-06-29 17:06:59 -07001461 strbuf_addstr(&out, "?,?");
1462 strbuf_addstr(&out, " +");
1463 add_line_count(&out, lc_b);
1464 strbuf_addstr(&out, " @@\n");
1465 emit_diff_symbol(o, DIFF_SYMBOL_REWRITE_DIFF, out.buf, out.len, 0);
1466 strbuf_release(&out);
1467
Junio C Hamano467ddc12011-02-28 16:11:55 -08001468 if (lc_a && !o->irreversible_delete)
Junio C Hamanod91ba8f2009-09-15 11:21:10 -07001469 emit_rewrite_lines(&ecbdata, '-', data_one, size_one);
Junio C Hamano6973dca2006-04-21 23:57:45 -07001470 if (lc_b)
Junio C Hamanod91ba8f2009-09-15 11:21:10 -07001471 emit_rewrite_lines(&ecbdata, '+', data_two, size_two);
Jeff Kingb76c0562010-04-01 20:04:14 -04001472 if (textconv_one)
Junio C Hamanoaed6ca52010-04-08 23:30:49 -07001473 free((char *)data_one);
Jeff Kingb76c0562010-04-01 20:04:14 -04001474 if (textconv_two)
Junio C Hamanoaed6ca52010-04-08 23:30:49 -07001475 free((char *)data_two);
Junio C Hamano6973dca2006-04-21 23:57:45 -07001476}
1477
Johannes Schindelinf59a59e2006-07-28 23:56:15 +02001478struct diff_words_buffer {
1479 mmfile_t text;
Ramsay Jones071bcaa2017-09-21 17:49:38 +01001480 unsigned long alloc;
Johannes Schindelin2e5d2002009-01-17 17:29:44 +01001481 struct diff_words_orig {
1482 const char *begin, *end;
1483 } *orig;
1484 int orig_nr, orig_alloc;
Johannes Schindelinf59a59e2006-07-28 23:56:15 +02001485};
1486
1487static void diff_words_append(char *line, unsigned long len,
1488 struct diff_words_buffer *buffer)
1489{
Johannes Schindelin23c15752009-01-17 17:29:43 +01001490 ALLOC_GROW(buffer->text.ptr, buffer->text.size + len, buffer->alloc);
Johannes Schindelinf59a59e2006-07-28 23:56:15 +02001491 line++;
1492 len--;
1493 memcpy(buffer->text.ptr + buffer->text.size, line, len);
1494 buffer->text.size += len;
Johannes Schindelin2b6a5412009-01-17 17:29:45 +01001495 buffer->text.ptr[buffer->text.size] = '\0';
Johannes Schindelinf59a59e2006-07-28 23:56:15 +02001496}
1497
Jonathan Nieder9cba13c2011-03-16 02:08:34 -05001498struct diff_words_style_elem {
Thomas Rast882749a2010-04-14 17:59:06 +02001499 const char *prefix;
1500 const char *suffix;
1501 const char *color; /* NULL; filled in by the setup code if
1502 * color is enabled */
1503};
1504
Jonathan Nieder9cba13c2011-03-16 02:08:34 -05001505struct diff_words_style {
Thomas Rast882749a2010-04-14 17:59:06 +02001506 enum diff_words_type type;
1507 struct diff_words_style_elem new, old, ctx;
1508 const char *newline;
1509};
1510
Stephen Boydc2e86ad2011-03-22 00:51:05 -07001511static struct diff_words_style diff_words_styles[] = {
Thomas Rast882749a2010-04-14 17:59:06 +02001512 { DIFF_WORDS_PORCELAIN, {"+", "\n"}, {"-", "\n"}, {" ", "\n"}, "~\n" },
1513 { DIFF_WORDS_PLAIN, {"{+", "+}"}, {"[-", "-]"}, {"", ""}, "\n" },
1514 { DIFF_WORDS_COLOR, {"", ""}, {"", ""}, {"", ""}, "\n" }
1515};
1516
Johannes Schindelinf59a59e2006-07-28 23:56:15 +02001517struct diff_words_data {
Johannes Schindelinf59a59e2006-07-28 23:56:15 +02001518 struct diff_words_buffer minus, plus;
Johannes Schindelin2e5d2002009-01-17 17:29:44 +01001519 const char *current_plus;
Bo Yang4297c0a2010-05-29 23:32:06 +08001520 int last_minus;
1521 struct diff_options *opt;
Johannes Schindelin2b6a5412009-01-17 17:29:45 +01001522 regex_t *word_regex;
Thomas Rast882749a2010-04-14 17:59:06 +02001523 enum diff_words_type type;
1524 struct diff_words_style *style;
Johannes Schindelinf59a59e2006-07-28 23:56:15 +02001525};
1526
Stefan Bellerbd033292017-06-29 17:07:03 -07001527static int fn_out_diff_words_write_helper(struct diff_options *o,
Thomas Rast882749a2010-04-14 17:59:06 +02001528 struct diff_words_style_elem *st_el,
1529 const char *newline,
Stefan Bellerbd033292017-06-29 17:07:03 -07001530 size_t count, const char *buf)
Thomas Rast882749a2010-04-14 17:59:06 +02001531{
Bo Yang4297c0a2010-05-29 23:32:06 +08001532 int print = 0;
Stefan Bellerbd033292017-06-29 17:07:03 -07001533 struct strbuf sb = STRBUF_INIT;
Bo Yang4297c0a2010-05-29 23:32:06 +08001534
Thomas Rast882749a2010-04-14 17:59:06 +02001535 while (count) {
1536 char *p = memchr(buf, '\n', count);
Bo Yang4297c0a2010-05-29 23:32:06 +08001537 if (print)
Stefan Bellerbd033292017-06-29 17:07:03 -07001538 strbuf_addstr(&sb, diff_line_prefix(o));
1539
Thomas Rast882749a2010-04-14 17:59:06 +02001540 if (p != buf) {
Stefan Bellerbd033292017-06-29 17:07:03 -07001541 const char *reset = st_el->color && *st_el->color ?
1542 GIT_COLOR_RESET : NULL;
1543 if (st_el->color && *st_el->color)
1544 strbuf_addstr(&sb, st_el->color);
1545 strbuf_addstr(&sb, st_el->prefix);
1546 strbuf_add(&sb, buf, p ? p - buf : count);
1547 strbuf_addstr(&sb, st_el->suffix);
1548 if (reset)
1549 strbuf_addstr(&sb, reset);
Thomas Rast882749a2010-04-14 17:59:06 +02001550 }
1551 if (!p)
Stefan Bellerbd033292017-06-29 17:07:03 -07001552 goto out;
1553
1554 strbuf_addstr(&sb, newline);
Thomas Rast882749a2010-04-14 17:59:06 +02001555 count -= p + 1 - buf;
1556 buf = p + 1;
Bo Yang4297c0a2010-05-29 23:32:06 +08001557 print = 1;
Stefan Bellerbd033292017-06-29 17:07:03 -07001558 if (count) {
1559 emit_diff_symbol(o, DIFF_SYMBOL_WORD_DIFF,
1560 sb.buf, sb.len, 0);
1561 strbuf_reset(&sb);
1562 }
Thomas Rast882749a2010-04-14 17:59:06 +02001563 }
Stefan Bellerbd033292017-06-29 17:07:03 -07001564
1565out:
1566 if (sb.len)
1567 emit_diff_symbol(o, DIFF_SYMBOL_WORD_DIFF,
1568 sb.buf, sb.len, 0);
1569 strbuf_release(&sb);
Thomas Rast882749a2010-04-14 17:59:06 +02001570 return 0;
1571}
1572
Bo Yang4297c0a2010-05-29 23:32:06 +08001573/*
1574 * '--color-words' algorithm can be described as:
1575 *
René Genz56217602017-04-30 17:48:27 +02001576 * 1. collect the minus/plus lines of a diff hunk, divided into
Bo Yang4297c0a2010-05-29 23:32:06 +08001577 * minus-lines and plus-lines;
1578 *
1579 * 2. break both minus-lines and plus-lines into words and
1580 * place them into two mmfile_t with one word for each line;
1581 *
1582 * 3. use xdiff to run diff on the two mmfile_t to get the words level diff;
1583 *
1584 * And for the common parts of the both file, we output the plus side text.
1585 * diff_words->current_plus is used to trace the current position of the plus file
1586 * which printed. diff_words->last_minus is used to trace the last minus word
1587 * printed.
1588 *
1589 * For '--graph' to work with '--color-words', we need to output the graph prefix
1590 * on each line of color words output. Generally, there are two conditions on
1591 * which we should output the prefix.
1592 *
1593 * 1. diff_words->last_minus == 0 &&
1594 * diff_words->current_plus == diff_words->plus.text.ptr
1595 *
1596 * that is: the plus text must start as a new line, and if there is no minus
1597 * word printed, a graph prefix must be printed.
1598 *
1599 * 2. diff_words->current_plus > diff_words->plus.text.ptr &&
1600 * *(diff_words->current_plus - 1) == '\n'
1601 *
1602 * that is: a graph prefix must be printed following a '\n'
1603 */
1604static int color_words_output_graph_prefix(struct diff_words_data *diff_words)
1605{
1606 if ((diff_words->last_minus == 0 &&
1607 diff_words->current_plus == diff_words->plus.text.ptr) ||
1608 (diff_words->current_plus > diff_words->plus.text.ptr &&
1609 *(diff_words->current_plus - 1) == '\n')) {
1610 return 1;
1611 } else {
1612 return 0;
1613 }
1614}
1615
Johannes Schindelinf59a59e2006-07-28 23:56:15 +02001616static void fn_out_diff_words_aux(void *priv, char *line, unsigned long len)
1617{
1618 struct diff_words_data *diff_words = priv;
Thomas Rast882749a2010-04-14 17:59:06 +02001619 struct diff_words_style *style = diff_words->style;
Johannes Schindelin2e5d2002009-01-17 17:29:44 +01001620 int minus_first, minus_len, plus_first, plus_len;
1621 const char *minus_begin, *minus_end, *plus_begin, *plus_end;
Bo Yang4297c0a2010-05-29 23:32:06 +08001622 struct diff_options *opt = diff_words->opt;
John Keeping30997bb2013-02-07 20:15:27 +00001623 const char *line_prefix;
Johannes Schindelinf59a59e2006-07-28 23:56:15 +02001624
Johannes Schindelin2e5d2002009-01-17 17:29:44 +01001625 if (line[0] != '@' || parse_hunk_header(line, len,
1626 &minus_first, &minus_len, &plus_first, &plus_len))
1627 return;
Johannes Schindelinf59a59e2006-07-28 23:56:15 +02001628
Bo Yang4297c0a2010-05-29 23:32:06 +08001629 assert(opt);
John Keeping30997bb2013-02-07 20:15:27 +00001630 line_prefix = diff_line_prefix(opt);
Bo Yang4297c0a2010-05-29 23:32:06 +08001631
Johannes Schindelin2e5d2002009-01-17 17:29:44 +01001632 /* POSIX requires that first be decremented by one if len == 0... */
1633 if (minus_len) {
1634 minus_begin = diff_words->minus.orig[minus_first].begin;
1635 minus_end =
1636 diff_words->minus.orig[minus_first + minus_len - 1].end;
1637 } else
1638 minus_begin = minus_end =
1639 diff_words->minus.orig[minus_first].end;
1640
1641 if (plus_len) {
1642 plus_begin = diff_words->plus.orig[plus_first].begin;
1643 plus_end = diff_words->plus.orig[plus_first + plus_len - 1].end;
1644 } else
1645 plus_begin = plus_end = diff_words->plus.orig[plus_first].end;
1646
Bo Yang4297c0a2010-05-29 23:32:06 +08001647 if (color_words_output_graph_prefix(diff_words)) {
1648 fputs(line_prefix, diff_words->opt->file);
1649 }
1650 if (diff_words->current_plus != plus_begin) {
Stefan Bellerbd033292017-06-29 17:07:03 -07001651 fn_out_diff_words_write_helper(diff_words->opt,
Thomas Rast882749a2010-04-14 17:59:06 +02001652 &style->ctx, style->newline,
1653 plus_begin - diff_words->current_plus,
Stefan Bellerbd033292017-06-29 17:07:03 -07001654 diff_words->current_plus);
Bo Yang4297c0a2010-05-29 23:32:06 +08001655 }
1656 if (minus_begin != minus_end) {
Stefan Bellerbd033292017-06-29 17:07:03 -07001657 fn_out_diff_words_write_helper(diff_words->opt,
Thomas Rast882749a2010-04-14 17:59:06 +02001658 &style->old, style->newline,
Stefan Bellerbd033292017-06-29 17:07:03 -07001659 minus_end - minus_begin, minus_begin);
Bo Yang4297c0a2010-05-29 23:32:06 +08001660 }
1661 if (plus_begin != plus_end) {
Stefan Bellerbd033292017-06-29 17:07:03 -07001662 fn_out_diff_words_write_helper(diff_words->opt,
Thomas Rast882749a2010-04-14 17:59:06 +02001663 &style->new, style->newline,
Stefan Bellerbd033292017-06-29 17:07:03 -07001664 plus_end - plus_begin, plus_begin);
Bo Yang4297c0a2010-05-29 23:32:06 +08001665 }
Johannes Schindelin2e5d2002009-01-17 17:29:44 +01001666
1667 diff_words->current_plus = plus_end;
Bo Yang4297c0a2010-05-29 23:32:06 +08001668 diff_words->last_minus = minus_first;
Johannes Schindelinf59a59e2006-07-28 23:56:15 +02001669}
1670
Johannes Schindelin2b6a5412009-01-17 17:29:45 +01001671/* This function starts looking at *begin, and returns 0 iff a word was found. */
1672static int find_word_boundaries(mmfile_t *buffer, regex_t *word_regex,
1673 int *begin, int *end)
1674{
1675 if (word_regex && *begin < buffer->size) {
1676 regmatch_t match[1];
Johannes Schindelinb7d36ff2016-09-21 20:24:14 +02001677 if (!regexec_buf(word_regex, buffer->ptr + *begin,
1678 buffer->size - *begin, 1, match, 0)) {
Johannes Schindelin2b6a5412009-01-17 17:29:45 +01001679 char *p = memchr(buffer->ptr + *begin + match[0].rm_so,
1680 '\n', match[0].rm_eo - match[0].rm_so);
1681 *end = p ? p - buffer->ptr : match[0].rm_eo + *begin;
1682 *begin += match[0].rm_so;
1683 return *begin >= *end;
1684 }
1685 return -1;
Johannes Schindelinf59a59e2006-07-28 23:56:15 +02001686 }
1687
Johannes Schindelin2b6a5412009-01-17 17:29:45 +01001688 /* find the next word */
1689 while (*begin < buffer->size && isspace(buffer->ptr[*begin]))
1690 (*begin)++;
1691 if (*begin >= buffer->size)
1692 return -1;
1693
1694 /* find the end of the word */
1695 *end = *begin + 1;
1696 while (*end < buffer->size && !isspace(buffer->ptr[*end]))
1697 (*end)++;
1698
1699 return 0;
1700}
1701
Johannes Schindelin23c15752009-01-17 17:29:43 +01001702/*
Johannes Schindelin2e5d2002009-01-17 17:29:44 +01001703 * This function splits the words in buffer->text, stores the list with
1704 * newline separator into out, and saves the offsets of the original words
1705 * in buffer->orig.
Johannes Schindelin23c15752009-01-17 17:29:43 +01001706 */
Johannes Schindelin2b6a5412009-01-17 17:29:45 +01001707static void diff_words_fill(struct diff_words_buffer *buffer, mmfile_t *out,
1708 regex_t *word_regex)
Johannes Schindelin23c15752009-01-17 17:29:43 +01001709{
Johannes Schindelin2e5d2002009-01-17 17:29:44 +01001710 int i, j;
Johannes Schindelin2b6a5412009-01-17 17:29:45 +01001711 long alloc = 0;
Johannes Schindelin2e5d2002009-01-17 17:29:44 +01001712
1713 out->size = 0;
Johannes Schindelin2b6a5412009-01-17 17:29:45 +01001714 out->ptr = NULL;
Johannes Schindelin2e5d2002009-01-17 17:29:44 +01001715
1716 /* fake an empty "0th" word */
1717 ALLOC_GROW(buffer->orig, 1, buffer->orig_alloc);
1718 buffer->orig[0].begin = buffer->orig[0].end = buffer->text.ptr;
1719 buffer->orig_nr = 1;
1720
1721 for (i = 0; i < buffer->text.size; i++) {
Johannes Schindelin2b6a5412009-01-17 17:29:45 +01001722 if (find_word_boundaries(&buffer->text, word_regex, &i, &j))
1723 return;
Johannes Schindelin2e5d2002009-01-17 17:29:44 +01001724
1725 /* store original boundaries */
1726 ALLOC_GROW(buffer->orig, buffer->orig_nr + 1,
1727 buffer->orig_alloc);
1728 buffer->orig[buffer->orig_nr].begin = buffer->text.ptr + i;
1729 buffer->orig[buffer->orig_nr].end = buffer->text.ptr + j;
1730 buffer->orig_nr++;
1731
1732 /* store one word */
Johannes Schindelin2b6a5412009-01-17 17:29:45 +01001733 ALLOC_GROW(out->ptr, out->size + j - i + 1, alloc);
Johannes Schindelin2e5d2002009-01-17 17:29:44 +01001734 memcpy(out->ptr + out->size, buffer->text.ptr + i, j - i);
1735 out->ptr[out->size + j - i] = '\n';
1736 out->size += j - i + 1;
1737
1738 i = j - 1;
Johannes Schindelinf59a59e2006-07-28 23:56:15 +02001739 }
1740}
1741
1742/* this executes the word diff on the accumulated buffers */
1743static void diff_words_show(struct diff_words_data *diff_words)
1744{
1745 xpparam_t xpp;
1746 xdemitconf_t xecfg;
Johannes Schindelinf59a59e2006-07-28 23:56:15 +02001747 mmfile_t minus, plus;
Thomas Rast882749a2010-04-14 17:59:06 +02001748 struct diff_words_style *style = diff_words->style;
Johannes Schindelinf59a59e2006-07-28 23:56:15 +02001749
Bo Yang4297c0a2010-05-29 23:32:06 +08001750 struct diff_options *opt = diff_words->opt;
John Keeping30997bb2013-02-07 20:15:27 +00001751 const char *line_prefix;
Bo Yang4297c0a2010-05-29 23:32:06 +08001752
1753 assert(opt);
John Keeping30997bb2013-02-07 20:15:27 +00001754 line_prefix = diff_line_prefix(opt);
Bo Yang4297c0a2010-05-29 23:32:06 +08001755
Johannes Schindelin2e5d2002009-01-17 17:29:44 +01001756 /* special case: only removal */
1757 if (!diff_words->plus.text.size) {
Stefan Bellerbd033292017-06-29 17:07:03 -07001758 emit_diff_symbol(diff_words->opt, DIFF_SYMBOL_WORD_DIFF,
1759 line_prefix, strlen(line_prefix), 0);
1760 fn_out_diff_words_write_helper(diff_words->opt,
Thomas Rast882749a2010-04-14 17:59:06 +02001761 &style->old, style->newline,
Bo Yang4297c0a2010-05-29 23:32:06 +08001762 diff_words->minus.text.size,
Stefan Bellerbd033292017-06-29 17:07:03 -07001763 diff_words->minus.text.ptr);
Johannes Schindelin2e5d2002009-01-17 17:29:44 +01001764 diff_words->minus.text.size = 0;
1765 return;
1766 }
1767
1768 diff_words->current_plus = diff_words->plus.text.ptr;
Bo Yang4297c0a2010-05-29 23:32:06 +08001769 diff_words->last_minus = 0;
Johannes Schindelinf59a59e2006-07-28 23:56:15 +02001770
Brian Downing9ccd0a82008-10-25 15:30:37 +02001771 memset(&xpp, 0, sizeof(xpp));
Johannes Schindelin30b25012007-07-04 19:05:46 +01001772 memset(&xecfg, 0, sizeof(xecfg));
Johannes Schindelin2b6a5412009-01-17 17:29:45 +01001773 diff_words_fill(&diff_words->minus, &minus, diff_words->word_regex);
1774 diff_words_fill(&diff_words->plus, &plus, diff_words->word_regex);
René Scharfe582aa002010-05-02 15:04:41 +02001775 xpp.flags = 0;
Johannes Schindelin2b6a5412009-01-17 17:29:45 +01001776 /* as only the hunk header will be parsed, we need a 0-context */
Johannes Schindelin2e5d2002009-01-17 17:29:44 +01001777 xecfg.ctxlen = 0;
Jeff King3efb9882015-09-24 19:12:23 -04001778 if (xdi_diff_outf(&minus, &plus, fn_out_diff_words_aux, diff_words,
1779 &xpp, &xecfg))
1780 die("unable to generate word diff");
Johannes Schindelinf59a59e2006-07-28 23:56:15 +02001781 free(minus.ptr);
1782 free(plus.ptr);
Johannes Schindelin2e5d2002009-01-17 17:29:44 +01001783 if (diff_words->current_plus != diff_words->plus.text.ptr +
Bo Yang4297c0a2010-05-29 23:32:06 +08001784 diff_words->plus.text.size) {
1785 if (color_words_output_graph_prefix(diff_words))
Stefan Bellerbd033292017-06-29 17:07:03 -07001786 emit_diff_symbol(diff_words->opt, DIFF_SYMBOL_WORD_DIFF,
1787 line_prefix, strlen(line_prefix), 0);
1788 fn_out_diff_words_write_helper(diff_words->opt,
Thomas Rast882749a2010-04-14 17:59:06 +02001789 &style->ctx, style->newline,
Johannes Schindelin2e5d2002009-01-17 17:29:44 +01001790 diff_words->plus.text.ptr + diff_words->plus.text.size
Stefan Bellerbd033292017-06-29 17:07:03 -07001791 - diff_words->current_plus, diff_words->current_plus);
Bo Yang4297c0a2010-05-29 23:32:06 +08001792 }
Johannes Schindelinf59a59e2006-07-28 23:56:15 +02001793 diff_words->minus.text.size = diff_words->plus.text.size = 0;
Johannes Schindelinf59a59e2006-07-28 23:56:15 +02001794}
1795
Junio C Hamano76fd2822009-10-30 10:09:06 -07001796/* In "color-words" mode, show word-diff of words accumulated in the buffer */
1797static void diff_words_flush(struct emit_callback *ecbdata)
1798{
Stefan Bellere6e045f2017-06-29 17:07:06 -07001799 struct diff_options *wo = ecbdata->diff_words->opt;
1800
Junio C Hamano76fd2822009-10-30 10:09:06 -07001801 if (ecbdata->diff_words->minus.text.size ||
1802 ecbdata->diff_words->plus.text.size)
1803 diff_words_show(ecbdata->diff_words);
Stefan Bellere6e045f2017-06-29 17:07:06 -07001804
1805 if (wo->emitted_symbols) {
1806 struct diff_options *o = ecbdata->opt;
1807 struct emitted_diff_symbols *wol = wo->emitted_symbols;
1808 int i;
1809
1810 /*
1811 * NEEDSWORK:
1812 * Instead of appending each, concat all words to a line?
1813 */
1814 for (i = 0; i < wol->nr; i++)
1815 append_emitted_diff_symbol(o, &wol->buf[i]);
1816
1817 for (i = 0; i < wol->nr; i++)
1818 free((void *)wol->buf[i].line);
1819
1820 wol->nr = 0;
1821 }
Junio C Hamano76fd2822009-10-30 10:09:06 -07001822}
1823
Thomas Rast77d1a522012-03-14 19:24:08 +01001824static void diff_filespec_load_driver(struct diff_filespec *one)
1825{
1826 /* Use already-loaded driver */
1827 if (one->driver)
1828 return;
1829
1830 if (S_ISREG(one->mode))
1831 one->driver = userdiff_find_by_path(one->path);
1832
1833 /* Fallback to default settings */
1834 if (!one->driver)
1835 one->driver = userdiff_find_by_name("default");
1836}
1837
1838static const char *userdiff_word_regex(struct diff_filespec *one)
1839{
1840 diff_filespec_load_driver(one);
1841 return one->driver->word_regex;
1842}
1843
1844static void init_diff_words_data(struct emit_callback *ecbdata,
Thomas Rast6440d342012-03-14 19:24:09 +01001845 struct diff_options *orig_opts,
Thomas Rast77d1a522012-03-14 19:24:08 +01001846 struct diff_filespec *one,
1847 struct diff_filespec *two)
1848{
1849 int i;
Thomas Rast6440d342012-03-14 19:24:09 +01001850 struct diff_options *o = xmalloc(sizeof(struct diff_options));
1851 memcpy(o, orig_opts, sizeof(struct diff_options));
Thomas Rast77d1a522012-03-14 19:24:08 +01001852
1853 ecbdata->diff_words =
1854 xcalloc(1, sizeof(struct diff_words_data));
1855 ecbdata->diff_words->type = o->word_diff;
1856 ecbdata->diff_words->opt = o;
Stefan Bellere6e045f2017-06-29 17:07:06 -07001857
1858 if (orig_opts->emitted_symbols)
1859 o->emitted_symbols =
1860 xcalloc(1, sizeof(struct emitted_diff_symbols));
1861
Thomas Rast77d1a522012-03-14 19:24:08 +01001862 if (!o->word_regex)
1863 o->word_regex = userdiff_word_regex(one);
1864 if (!o->word_regex)
1865 o->word_regex = userdiff_word_regex(two);
1866 if (!o->word_regex)
1867 o->word_regex = diff_word_regex_cfg;
1868 if (o->word_regex) {
1869 ecbdata->diff_words->word_regex = (regex_t *)
1870 xmalloc(sizeof(regex_t));
1871 if (regcomp(ecbdata->diff_words->word_regex,
1872 o->word_regex,
1873 REG_EXTENDED | REG_NEWLINE))
1874 die ("Invalid regular expression: %s",
1875 o->word_regex);
1876 }
1877 for (i = 0; i < ARRAY_SIZE(diff_words_styles); i++) {
1878 if (o->word_diff == diff_words_styles[i].type) {
1879 ecbdata->diff_words->style =
1880 &diff_words_styles[i];
1881 break;
1882 }
1883 }
1884 if (want_color(o->use_color)) {
1885 struct diff_words_style *st = ecbdata->diff_words->style;
1886 st->old.color = diff_get_color_opt(o, DIFF_FILE_OLD);
1887 st->new.color = diff_get_color_opt(o, DIFF_FILE_NEW);
Jeff King8dbf3eb2015-05-27 16:48:46 -04001888 st->ctx.color = diff_get_color_opt(o, DIFF_CONTEXT);
Thomas Rast77d1a522012-03-14 19:24:08 +01001889 }
1890}
1891
Johannes Schindelinf59a59e2006-07-28 23:56:15 +02001892static void free_diff_words_data(struct emit_callback *ecbdata)
1893{
1894 if (ecbdata->diff_words) {
Junio C Hamano76fd2822009-10-30 10:09:06 -07001895 diff_words_flush(ecbdata);
Stefan Bellere6e045f2017-06-29 17:07:06 -07001896 free (ecbdata->diff_words->opt->emitted_symbols);
Thomas Rast6440d342012-03-14 19:24:09 +01001897 free (ecbdata->diff_words->opt);
Jim Meyering8e0f7002008-01-31 18:26:32 +01001898 free (ecbdata->diff_words->minus.text.ptr);
Johannes Schindelin2e5d2002009-01-17 17:29:44 +01001899 free (ecbdata->diff_words->minus.orig);
Jim Meyering8e0f7002008-01-31 18:26:32 +01001900 free (ecbdata->diff_words->plus.text.ptr);
Johannes Schindelin2e5d2002009-01-17 17:29:44 +01001901 free (ecbdata->diff_words->plus.orig);
Brandon Caseyef5644e2010-09-09 14:02:45 -05001902 if (ecbdata->diff_words->word_regex) {
1903 regfree(ecbdata->diff_words->word_regex);
1904 free(ecbdata->diff_words->word_regex);
1905 }
Ævar Arnfjörð Bjarmason6a83d902017-06-15 23:15:46 +00001906 FREE_AND_NULL(ecbdata->diff_words);
Johannes Schindelinf59a59e2006-07-28 23:56:15 +02001907 }
1908}
1909
Jeff Kingce436972006-07-23 05:24:18 -04001910const char *diff_get_color(int diff_use_color, enum color_diff ix)
Johannes Schindelincd112ce2006-06-13 18:45:44 +02001911{
Jeff Kingdaa0c3d2011-08-17 22:04:23 -07001912 if (want_color(diff_use_color))
Linus Torvalds50f575f2006-06-22 13:53:31 -07001913 return diff_colors[ix];
1914 return "";
Johannes Schindelincd112ce2006-06-13 18:45:44 +02001915}
1916
John Keepingf1922232013-02-07 20:15:26 +00001917const char *diff_line_prefix(struct diff_options *opt)
1918{
1919 struct strbuf *msgbuf;
1920 if (!opt->output_prefix)
1921 return "";
1922
1923 msgbuf = opt->output_prefix(opt, opt->output_prefix_data);
1924 return msgbuf->buf;
1925}
1926
Junio C Hamano23707812008-01-02 01:50:11 -08001927static unsigned long sane_truncate_line(struct emit_callback *ecb, char *line, unsigned long len)
1928{
1929 const char *cp;
1930 unsigned long allot;
1931 size_t l = len;
1932
Junio C Hamano23707812008-01-02 01:50:11 -08001933 cp = line;
1934 allot = l;
1935 while (0 < l) {
1936 (void) utf8_width(&cp, &l);
1937 if (!cp)
1938 break; /* truncated in the middle? */
1939 }
1940 return allot - l;
1941}
1942
Junio C Hamanod68fe262009-09-14 22:05:57 -07001943static void find_lno(const char *line, struct emit_callback *ecbdata)
Junio C Hamano690ed842009-09-04 00:41:15 -07001944{
Junio C Hamanod68fe262009-09-14 22:05:57 -07001945 const char *p;
1946 ecbdata->lno_in_preimage = 0;
1947 ecbdata->lno_in_postimage = 0;
1948 p = strchr(line, '-');
Junio C Hamano690ed842009-09-04 00:41:15 -07001949 if (!p)
Junio C Hamanod68fe262009-09-14 22:05:57 -07001950 return; /* cannot happen */
1951 ecbdata->lno_in_preimage = strtol(p + 1, NULL, 10);
1952 p = strchr(p, '+');
1953 if (!p)
1954 return; /* cannot happen */
1955 ecbdata->lno_in_postimage = strtol(p + 1, NULL, 10);
Junio C Hamano690ed842009-09-04 00:41:15 -07001956}
1957
Johannes Schindelincd112ce2006-06-13 18:45:44 +02001958static void fn_out_consume(void *priv, char *line, unsigned long len)
Junio C Hamano6973dca2006-04-21 23:57:45 -07001959{
Junio C Hamano6973dca2006-04-21 23:57:45 -07001960 struct emit_callback *ecbdata = priv;
Jeff Kingce436972006-07-23 05:24:18 -04001961 const char *reset = diff_get_color(ecbdata->color_diff, DIFF_RESET);
Bo Yang7be57612010-05-26 15:23:54 +08001962 struct diff_options *o = ecbdata->opt;
Junio C Hamano6973dca2006-04-21 23:57:45 -07001963
Stefan Bellerba162332016-09-07 16:36:46 -07001964 o->found_changes = 1;
1965
Greg Bacon3e97c7c2009-11-19 15:12:24 -06001966 if (ecbdata->header) {
Stefan Bellera29b0a12017-06-29 17:06:57 -07001967 emit_diff_symbol(o, DIFF_SYMBOL_HEADER,
1968 ecbdata->header->buf, ecbdata->header->len, 0);
Greg Bacon3e97c7c2009-11-19 15:12:24 -06001969 strbuf_reset(ecbdata->header);
1970 ecbdata->header = NULL;
1971 }
Johannes Schindelin34a5e1a2007-02-25 23:34:54 +01001972
Junio C Hamano6973dca2006-04-21 23:57:45 -07001973 if (ecbdata->label_path[0]) {
Stefan Beller3ee8b7b2017-06-29 17:06:56 -07001974 emit_diff_symbol(o, DIFF_SYMBOL_FILEPAIR_MINUS,
1975 ecbdata->label_path[0],
1976 strlen(ecbdata->label_path[0]), 0);
1977 emit_diff_symbol(o, DIFF_SYMBOL_FILEPAIR_PLUS,
1978 ecbdata->label_path[1],
1979 strlen(ecbdata->label_path[1]), 0);
Junio C Hamano6973dca2006-04-21 23:57:45 -07001980 ecbdata->label_path[0] = ecbdata->label_path[1] = NULL;
1981 }
Johannes Schindelincd112ce2006-06-13 18:45:44 +02001982
Jim Meyeringa624eaa2008-08-15 13:39:26 +02001983 if (diff_suppress_blank_empty
1984 && len == 2 && line[0] == ' ' && line[1] == '\n') {
1985 line[0] = '\n';
1986 len = 1;
1987 }
1988
Junio C Hamanob8d9c1a2009-09-03 23:59:25 -07001989 if (line[0] == '@') {
Junio C Hamano76fd2822009-10-30 10:09:06 -07001990 if (ecbdata->diff_words)
1991 diff_words_flush(ecbdata);
Junio C Hamano23707812008-01-02 01:50:11 -08001992 len = sane_truncate_line(ecbdata, line, len);
Junio C Hamanod68fe262009-09-14 22:05:57 -07001993 find_lno(line, ecbdata);
Bert Wesarg89cb73a2009-11-27 07:55:18 +01001994 emit_hunk_header(ecbdata, line, len);
Junio C Hamano448c3ef2006-09-22 22:48:39 -07001995 return;
Johannes Schindelincd112ce2006-06-13 18:45:44 +02001996 }
Junio C Hamano448c3ef2006-09-22 22:48:39 -07001997
Junio C Hamano448c3ef2006-09-22 22:48:39 -07001998 if (ecbdata->diff_words) {
Stefan Bellerff958672017-06-29 17:06:54 -07001999 enum diff_symbol s =
2000 ecbdata->diff_words->type == DIFF_WORDS_PORCELAIN ?
2001 DIFF_SYMBOL_WORDS_PORCELAIN : DIFF_SYMBOL_WORDS;
Junio C Hamano448c3ef2006-09-22 22:48:39 -07002002 if (line[0] == '-') {
2003 diff_words_append(line, len,
2004 &ecbdata->diff_words->minus);
2005 return;
2006 } else if (line[0] == '+') {
2007 diff_words_append(line, len,
2008 &ecbdata->diff_words->plus);
2009 return;
Christian Couder59556542013-11-30 21:55:40 +01002010 } else if (starts_with(line, "\\ ")) {
Thomas Rastc7c2bc02012-01-12 12:15:33 +01002011 /*
2012 * Eat the "no newline at eof" marker as if we
2013 * saw a "+" or "-" line with nothing on it,
2014 * and return without diff_words_flush() to
2015 * defer processing. If this is the end of
2016 * preimage, more "+" lines may come after it.
2017 */
2018 return;
Junio C Hamano448c3ef2006-09-22 22:48:39 -07002019 }
Junio C Hamano76fd2822009-10-30 10:09:06 -07002020 diff_words_flush(ecbdata);
Stefan Bellerff958672017-06-29 17:06:54 -07002021 emit_diff_symbol(o, s, line, len, 0);
Junio C Hamano448c3ef2006-09-22 22:48:39 -07002022 return;
2023 }
Junio C Hamano448c3ef2006-09-22 22:48:39 -07002024
Junio C Hamano0e383e12015-05-26 09:56:33 -07002025 switch (line[0]) {
2026 case '+':
Junio C Hamanod68fe262009-09-14 22:05:57 -07002027 ecbdata->lno_in_postimage++;
Junio C Hamano018cff72009-09-14 18:44:01 -07002028 emit_add_line(reset, ecbdata, line + 1, len - 1);
Junio C Hamano0e383e12015-05-26 09:56:33 -07002029 break;
2030 case '-':
2031 ecbdata->lno_in_preimage++;
2032 emit_del_line(reset, ecbdata, line + 1, len - 1);
2033 break;
2034 case ' ':
2035 ecbdata->lno_in_postimage++;
2036 ecbdata->lno_in_preimage++;
2037 emit_context_line(reset, ecbdata, line + 1, len - 1);
2038 break;
2039 default:
2040 /* incomplete line at the end */
2041 ecbdata->lno_in_preimage++;
Stefan Bellerf2bb1212017-06-29 17:06:55 -07002042 emit_diff_symbol(o, DIFF_SYMBOL_CONTEXT_INCOMPLETE,
2043 line, len, 0);
Junio C Hamano0e383e12015-05-26 09:56:33 -07002044 break;
Junio C Hamano448c3ef2006-09-22 22:48:39 -07002045 }
Junio C Hamano6973dca2006-04-21 23:57:45 -07002046}
2047
2048static char *pprint_rename(const char *a, const char *b)
2049{
2050 const char *old = a;
2051 const char *new = b;
Brandon Caseyf285a2d2008-10-09 14:12:12 -05002052 struct strbuf name = STRBUF_INIT;
Junio C Hamano6973dca2006-04-21 23:57:45 -07002053 int pfx_length, sfx_length;
Thomas Rastdd281f02013-02-26 21:47:01 +01002054 int pfx_adjust_for_slash;
Junio C Hamano6973dca2006-04-21 23:57:45 -07002055 int len_a = strlen(a);
2056 int len_b = strlen(b);
Pierre Habouzit663af342007-09-20 00:42:15 +02002057 int a_midlen, b_midlen;
Alexandre Julliarde5bfbf92007-02-10 15:39:00 +01002058 int qlen_a = quote_c_style(a, NULL, NULL, 0);
2059 int qlen_b = quote_c_style(b, NULL, NULL, 0);
2060
2061 if (qlen_a || qlen_b) {
Pierre Habouzit663af342007-09-20 00:42:15 +02002062 quote_c_style(a, &name, NULL, 0);
2063 strbuf_addstr(&name, " => ");
2064 quote_c_style(b, &name, NULL, 0);
Pierre Habouzitb315c5c2007-09-27 12:58:23 +02002065 return strbuf_detach(&name, NULL);
Alexandre Julliarde5bfbf92007-02-10 15:39:00 +01002066 }
Junio C Hamano6973dca2006-04-21 23:57:45 -07002067
2068 /* Find common prefix */
2069 pfx_length = 0;
2070 while (*old && *new && *old == *new) {
2071 if (*old == '/')
2072 pfx_length = old - a + 1;
2073 old++;
2074 new++;
2075 }
2076
2077 /* Find common suffix */
2078 old = a + len_a;
2079 new = b + len_b;
2080 sfx_length = 0;
Antoine Pelissed020e272013-02-23 17:48:45 +01002081 /*
Thomas Rastdd281f02013-02-26 21:47:01 +01002082 * If there is a common prefix, it must end in a slash. In
2083 * that case we let this loop run 1 into the prefix to see the
2084 * same slash.
2085 *
2086 * If there is no common prefix, we cannot do this as it would
2087 * underrun the input strings.
Antoine Pelissed020e272013-02-23 17:48:45 +01002088 */
Thomas Rastdd281f02013-02-26 21:47:01 +01002089 pfx_adjust_for_slash = (pfx_length ? 1 : 0);
2090 while (a + pfx_length - pfx_adjust_for_slash <= old &&
2091 b + pfx_length - pfx_adjust_for_slash <= new &&
Antoine Pelissed020e272013-02-23 17:48:45 +01002092 *old == *new) {
Junio C Hamano6973dca2006-04-21 23:57:45 -07002093 if (*old == '/')
2094 sfx_length = len_a - (old - a);
2095 old--;
2096 new--;
2097 }
2098
2099 /*
2100 * pfx{mid-a => mid-b}sfx
2101 * {pfx-a => pfx-b}sfx
2102 * pfx{sfx-a => sfx-b}
2103 * name-a => name-b
2104 */
Pierre Habouzit663af342007-09-20 00:42:15 +02002105 a_midlen = len_a - pfx_length - sfx_length;
2106 b_midlen = len_b - pfx_length - sfx_length;
2107 if (a_midlen < 0)
2108 a_midlen = 0;
2109 if (b_midlen < 0)
2110 b_midlen = 0;
Junio C Hamanocc908b82006-05-14 22:07:28 -07002111
Pierre Habouzit663af342007-09-20 00:42:15 +02002112 strbuf_grow(&name, pfx_length + a_midlen + b_midlen + sfx_length + 7);
2113 if (pfx_length + sfx_length) {
2114 strbuf_add(&name, a, pfx_length);
2115 strbuf_addch(&name, '{');
Junio C Hamano6973dca2006-04-21 23:57:45 -07002116 }
Pierre Habouzit663af342007-09-20 00:42:15 +02002117 strbuf_add(&name, a + pfx_length, a_midlen);
2118 strbuf_addstr(&name, " => ");
2119 strbuf_add(&name, b + pfx_length, b_midlen);
2120 if (pfx_length + sfx_length) {
2121 strbuf_addch(&name, '}');
2122 strbuf_add(&name, a + len_a - sfx_length, sfx_length);
Junio C Hamano6973dca2006-04-21 23:57:45 -07002123 }
Pierre Habouzitb315c5c2007-09-27 12:58:23 +02002124 return strbuf_detach(&name, NULL);
Junio C Hamano6973dca2006-04-21 23:57:45 -07002125}
2126
2127struct diffstat_t {
Junio C Hamano6973dca2006-04-21 23:57:45 -07002128 int nr;
2129 int alloc;
2130 struct diffstat_file {
Junio C Hamanof6046522007-12-11 23:46:30 -08002131 char *from_name;
Junio C Hamano6973dca2006-04-21 23:57:45 -07002132 char *name;
Junio C Hamanof6046522007-12-11 23:46:30 -08002133 char *print_name;
Junio C Hamano6973dca2006-04-21 23:57:45 -07002134 unsigned is_unmerged:1;
2135 unsigned is_binary:1;
2136 unsigned is_renamed:1;
Linus Torvalds74faaa12012-10-17 10:00:37 -07002137 unsigned is_interesting:1;
Jeff King0974c112010-04-17 13:41:08 -04002138 uintmax_t added, deleted;
Junio C Hamano6973dca2006-04-21 23:57:45 -07002139 } **files;
2140};
2141
2142static struct diffstat_file *diffstat_add(struct diffstat_t *diffstat,
2143 const char *name_a,
2144 const char *name_b)
2145{
2146 struct diffstat_file *x;
Brian Gesiak1a4927c2014-05-27 00:33:47 +09002147 x = xcalloc(1, sizeof(*x));
Dmitry S. Dolzhenko4c960a42014-03-04 02:31:53 +04002148 ALLOC_GROW(diffstat->files, diffstat->nr + 1, diffstat->alloc);
Junio C Hamano6973dca2006-04-21 23:57:45 -07002149 diffstat->files[diffstat->nr++] = x;
2150 if (name_b) {
Junio C Hamanof6046522007-12-11 23:46:30 -08002151 x->from_name = xstrdup(name_a);
2152 x->name = xstrdup(name_b);
Junio C Hamano6973dca2006-04-21 23:57:45 -07002153 x->is_renamed = 1;
2154 }
Junio C Hamanof6046522007-12-11 23:46:30 -08002155 else {
2156 x->from_name = NULL;
Shawn Pearce9befac42006-09-02 00:16:31 -04002157 x->name = xstrdup(name_a);
Junio C Hamanof6046522007-12-11 23:46:30 -08002158 }
Junio C Hamano6973dca2006-04-21 23:57:45 -07002159 return x;
2160}
2161
2162static void diffstat_consume(void *priv, char *line, unsigned long len)
2163{
2164 struct diffstat_t *diffstat = priv;
2165 struct diffstat_file *x = diffstat->files[diffstat->nr - 1];
2166
2167 if (line[0] == '+')
2168 x->added++;
2169 else if (line[0] == '-')
2170 x->deleted++;
2171}
2172
Johannes Schindelin698ce6f2006-05-20 15:40:29 +02002173const char mime_boundary_leader[] = "------------";
Junio C Hamano6973dca2006-04-21 23:57:45 -07002174
Junio C Hamanoa2540022006-09-26 18:53:02 -07002175static int scale_linear(int it, int width, int max_change)
2176{
Junio C Hamano2eeeef22012-02-14 13:49:11 -08002177 if (!it)
2178 return 0;
Junio C Hamanoa2540022006-09-26 18:53:02 -07002179 /*
Junio C Hamano2eeeef22012-02-14 13:49:11 -08002180 * make sure that at least one '-' or '+' is printed if
2181 * there is any change to this path. The easiest way is to
2182 * scale linearly as if the alloted width is one column shorter
2183 * than it is, and then add 1 to the result.
Junio C Hamanoa2540022006-09-26 18:53:02 -07002184 */
Junio C Hamano2eeeef22012-02-14 13:49:11 -08002185 return 1 + (it * (width - 1) / max_change);
Junio C Hamanoa2540022006-09-26 18:53:02 -07002186}
2187
Stefan Beller0911c472017-06-29 17:07:02 -07002188static void show_graph(struct strbuf *out, char ch, int cnt,
2189 const char *set, const char *reset)
Junio C Hamanoa2540022006-09-26 18:53:02 -07002190{
2191 if (cnt <= 0)
2192 return;
Stefan Beller0911c472017-06-29 17:07:02 -07002193 strbuf_addstr(out, set);
2194 strbuf_addchars(out, ch, cnt);
2195 strbuf_addstr(out, reset);
Junio C Hamanoa2540022006-09-26 18:53:02 -07002196}
2197
Junio C Hamanof6046522007-12-11 23:46:30 -08002198static void fill_print_name(struct diffstat_file *file)
2199{
2200 char *pname;
2201
2202 if (file->print_name)
2203 return;
2204
2205 if (!file->is_renamed) {
Brandon Caseyf285a2d2008-10-09 14:12:12 -05002206 struct strbuf buf = STRBUF_INIT;
Junio C Hamanof6046522007-12-11 23:46:30 -08002207 if (quote_c_style(file->name, &buf, NULL, 0)) {
2208 pname = strbuf_detach(&buf, NULL);
2209 } else {
2210 pname = file->name;
2211 strbuf_release(&buf);
2212 }
2213 } else {
2214 pname = pprint_rename(file->from_name, file->name);
2215 }
2216 file->print_name = pname;
2217}
2218
Stefan Beller0911c472017-06-29 17:07:02 -07002219static void print_stat_summary_inserts_deletes(struct diff_options *options,
2220 int files, int insertions, int deletions)
Nguyễn Thái Ngọc Duy7f814632012-02-01 19:55:07 +07002221{
2222 struct strbuf sb = STRBUF_INIT;
Nguyễn Thái Ngọc Duy7f814632012-02-01 19:55:07 +07002223
2224 if (!files) {
2225 assert(insertions == 0 && deletions == 0);
Stefan Beller0911c472017-06-29 17:07:02 -07002226 emit_diff_symbol(options, DIFF_SYMBOL_STATS_SUMMARY_NO_FILES,
2227 NULL, 0, 0);
2228 return;
Nguyễn Thái Ngọc Duy7f814632012-02-01 19:55:07 +07002229 }
2230
2231 strbuf_addf(&sb,
Nguyễn Thái Ngọc Duy218adaa2012-09-13 21:16:26 +07002232 (files == 1) ? " %d file changed" : " %d files changed",
Nguyễn Thái Ngọc Duy7f814632012-02-01 19:55:07 +07002233 files);
2234
2235 /*
2236 * For binary diff, the caller may want to print "x files
2237 * changed" with insertions == 0 && deletions == 0.
2238 *
2239 * Not omitting "0 insertions(+), 0 deletions(-)" in this case
2240 * is probably less confusing (i.e skip over "2 files changed
2241 * but nothing about added/removed lines? Is this a bug in Git?").
2242 */
2243 if (insertions || deletions == 0) {
Nguyễn Thái Ngọc Duy7f814632012-02-01 19:55:07 +07002244 strbuf_addf(&sb,
Nguyễn Thái Ngọc Duy218adaa2012-09-13 21:16:26 +07002245 (insertions == 1) ? ", %d insertion(+)" : ", %d insertions(+)",
Nguyễn Thái Ngọc Duy7f814632012-02-01 19:55:07 +07002246 insertions);
2247 }
2248
2249 if (deletions || insertions == 0) {
Nguyễn Thái Ngọc Duy7f814632012-02-01 19:55:07 +07002250 strbuf_addf(&sb,
Nguyễn Thái Ngọc Duy218adaa2012-09-13 21:16:26 +07002251 (deletions == 1) ? ", %d deletion(-)" : ", %d deletions(-)",
Nguyễn Thái Ngọc Duy7f814632012-02-01 19:55:07 +07002252 deletions);
2253 }
2254 strbuf_addch(&sb, '\n');
Stefan Beller0911c472017-06-29 17:07:02 -07002255 emit_diff_symbol(options, DIFF_SYMBOL_STATS_SUMMARY_INSERTS_DELETES,
2256 sb.buf, sb.len, 0);
Nguyễn Thái Ngọc Duy7f814632012-02-01 19:55:07 +07002257 strbuf_release(&sb);
Stefan Beller0911c472017-06-29 17:07:02 -07002258}
2259
2260void print_stat_summary(FILE *fp, int files,
2261 int insertions, int deletions)
2262{
2263 struct diff_options o;
2264 memset(&o, 0, sizeof(o));
2265 o.file = fp;
2266
2267 print_stat_summary_inserts_deletes(&o, files, insertions, deletions);
Nguyễn Thái Ngọc Duy7f814632012-02-01 19:55:07 +07002268}
2269
Felipe Contreras4b25d092009-05-01 12:06:36 +03002270static void show_stats(struct diffstat_t *data, struct diff_options *options)
Junio C Hamano6973dca2006-04-21 23:57:45 -07002271{
Benjamin Kramereb3a9dd2009-03-07 21:02:10 +01002272 int i, len, add, del, adds = 0, dels = 0;
Jeff King0974c112010-04-17 13:41:08 -04002273 uintmax_t max_change = 0, max_len = 0;
Zbigniew Jędrzejewski-Szmekdc801e72012-04-30 22:38:58 +02002274 int total_files = data->nr, count;
2275 int width, name_width, graph_width, number_width = 0, bin_width = 0;
Johannes Schindelinc0aa3352011-03-22 13:50:08 +01002276 const char *reset, *add_c, *del_c;
Junio C Hamanoe5f85df2011-05-27 21:50:39 -07002277 int extra_shown = 0;
Stefan Beller0911c472017-06-29 17:07:02 -07002278 const char *line_prefix = diff_line_prefix(options);
2279 struct strbuf out = STRBUF_INIT;
Junio C Hamano6973dca2006-04-21 23:57:45 -07002280
2281 if (data->nr == 0)
2282 return;
2283
Michael J Gruber808e1db2011-05-27 14:36:41 +02002284 count = options->stat_count ? options->stat_count : data->nr;
Junio C Hamanoa2540022006-09-26 18:53:02 -07002285
Pierre Habouzit8f67f8a2007-11-10 20:05:14 +01002286 reset = diff_get_color_opt(options, DIFF_RESET);
Pierre Habouzit8f67f8a2007-11-10 20:05:14 +01002287 add_c = diff_get_color_opt(options, DIFF_FILE_NEW);
2288 del_c = diff_get_color_opt(options, DIFF_FILE_OLD);
Junio C Hamano785f7432006-09-26 18:59:41 -07002289
Zbigniew Jędrzejewski-Szmek1b058bc2012-03-01 13:26:43 +01002290 /*
2291 * Find the longest filename and max number of changes
2292 */
Michael J Gruber808e1db2011-05-27 14:36:41 +02002293 for (i = 0; (i < count) && (i < data->nr); i++) {
Junio C Hamano6973dca2006-04-21 23:57:45 -07002294 struct diffstat_file *file = data->files[i];
Jeff King0974c112010-04-17 13:41:08 -04002295 uintmax_t change = file->added + file->deleted;
Junio C Hamanoaf0ed812012-11-27 11:24:54 -08002296
2297 if (!file->is_interesting && (change == 0)) {
Michael J Gruber808e1db2011-05-27 14:36:41 +02002298 count++; /* not shown == room for one more */
Michael J Gruber358e4602011-05-27 14:36:40 +02002299 continue;
2300 }
Junio C Hamanof6046522007-12-11 23:46:30 -08002301 fill_print_name(file);
2302 len = strlen(file->print_name);
Junio C Hamano6973dca2006-04-21 23:57:45 -07002303 if (max_len < len)
2304 max_len = len;
2305
Zbigniew Jędrzejewski-Szmekdc801e72012-04-30 22:38:58 +02002306 if (file->is_unmerged) {
2307 /* "Unmerged" is 8 characters */
2308 bin_width = bin_width < 8 ? 8 : bin_width;
Junio C Hamano6973dca2006-04-21 23:57:45 -07002309 continue;
Zbigniew Jędrzejewski-Szmekdc801e72012-04-30 22:38:58 +02002310 }
2311 if (file->is_binary) {
2312 /* "Bin XXX -> YYY bytes" */
2313 int w = 14 + decimal_width(file->added)
2314 + decimal_width(file->deleted);
2315 bin_width = bin_width < w ? w : bin_width;
2316 /* Display change counts aligned with "Bin" */
2317 number_width = 3;
2318 continue;
2319 }
2320
Junio C Hamanoa2540022006-09-26 18:53:02 -07002321 if (max_change < change)
2322 max_change = change;
Junio C Hamano6973dca2006-04-21 23:57:45 -07002323 }
Junio C Hamanoa20d3c02012-11-27 11:47:46 -08002324 count = i; /* where we can stop scanning in data->files[] */
Junio C Hamano6973dca2006-04-21 23:57:45 -07002325
Zbigniew Jędrzejewski-Szmek1b058bc2012-03-01 13:26:43 +01002326 /*
2327 * We have width = stat_width or term_columns() columns total.
2328 * We want a maximum of min(max_len, stat_name_width) for the name part.
Zbigniew Jędrzejewski-Szmek969fe572012-03-01 13:26:45 +01002329 * We want a maximum of min(max_change, stat_graph_width) for the +- part.
Zbigniew Jędrzejewski-Szmek1b058bc2012-03-01 13:26:43 +01002330 * We also need 1 for " " and 4 + decimal_width(max_change)
2331 * for " | NNNN " and one the empty column at the end, altogether
2332 * 6 + decimal_width(max_change).
Junio C Hamanoa2540022006-09-26 18:53:02 -07002333 *
Zbigniew Jędrzejewski-Szmek1b058bc2012-03-01 13:26:43 +01002334 * If there's not enough space, we will use the smaller of
2335 * stat_name_width (if set) and 5/8*width for the filename,
Zbigniew Jędrzejewski-Szmek969fe572012-03-01 13:26:45 +01002336 * and the rest for constant elements + graph part, but no more
2337 * than stat_graph_width for the graph part.
Zbigniew Jędrzejewski-Szmek1b058bc2012-03-01 13:26:43 +01002338 * (5/8 gives 50 for filename and 30 for the constant parts + graph
2339 * for the standard terminal size).
2340 *
2341 * In other words: stat_width limits the maximum width, and
2342 * stat_name_width fixes the maximum width of the filename,
2343 * and is also used to divide available columns if there
2344 * aren't enough.
Zbigniew Jędrzejewski-Szmekdc801e72012-04-30 22:38:58 +02002345 *
2346 * Binary files are displayed with "Bin XXX -> YYY bytes"
2347 * instead of the change count and graph. This part is treated
2348 * similarly to the graph part, except that it is not
Stefano Lattarini41ccfdd2013-04-12 00:36:10 +02002349 * "scaled". If total width is too small to accommodate the
Zbigniew Jędrzejewski-Szmekdc801e72012-04-30 22:38:58 +02002350 * guaranteed minimum width of the filename part and the
2351 * separators and this message, this message will "overflow"
2352 * making the line longer than the maximum width.
Junio C Hamanoa2540022006-09-26 18:53:02 -07002353 */
Junio C Hamanoa2540022006-09-26 18:53:02 -07002354
Zbigniew Jędrzejewski-Szmek1b058bc2012-03-01 13:26:43 +01002355 if (options->stat_width == -1)
Junio C Hamanocd48dad2016-08-31 16:27:19 -07002356 width = term_columns() - strlen(line_prefix);
Zbigniew Jędrzejewski-Szmek1b058bc2012-03-01 13:26:43 +01002357 else
2358 width = options->stat_width ? options->stat_width : 80;
Zbigniew Jędrzejewski-Szmekdc801e72012-04-30 22:38:58 +02002359 number_width = decimal_width(max_change) > number_width ?
2360 decimal_width(max_change) : number_width;
Zbigniew Jędrzejewski-Szmek1b058bc2012-03-01 13:26:43 +01002361
Zbigniew Jędrzejewski-Szmekdf444832012-03-01 13:26:46 +01002362 if (options->stat_graph_width == -1)
2363 options->stat_graph_width = diff_stat_graph_width;
2364
Zbigniew Jędrzejewski-Szmek1b058bc2012-03-01 13:26:43 +01002365 /*
2366 * Guarantee 3/8*16==6 for the graph part
2367 * and 5/8*16==10 for the filename part
2368 */
2369 if (width < 16 + 6 + number_width)
2370 width = 16 + 6 + number_width;
2371
2372 /*
2373 * First assign sizes that are wanted, ignoring available width.
Zbigniew Jędrzejewski-Szmekdc801e72012-04-30 22:38:58 +02002374 * strlen("Bin XXX -> YYY bytes") == bin_width, and the part
2375 * starting from "XXX" should fit in graph_width.
Zbigniew Jędrzejewski-Szmek1b058bc2012-03-01 13:26:43 +01002376 */
Zbigniew Jędrzejewski-Szmekdc801e72012-04-30 22:38:58 +02002377 graph_width = max_change + 4 > bin_width ? max_change : bin_width - 4;
2378 if (options->stat_graph_width &&
2379 options->stat_graph_width < graph_width)
2380 graph_width = options->stat_graph_width;
2381
Zbigniew Jędrzejewski-Szmek1b058bc2012-03-01 13:26:43 +01002382 name_width = (options->stat_name_width > 0 &&
2383 options->stat_name_width < max_len) ?
2384 options->stat_name_width : max_len;
2385
2386 /*
2387 * Adjust adjustable widths not to exceed maximum width
2388 */
2389 if (name_width + number_width + 6 + graph_width > width) {
Lucian Poston678c5742012-04-18 14:12:18 -07002390 if (graph_width > width * 3/8 - number_width - 6) {
Zbigniew Jędrzejewski-Szmek1b058bc2012-03-01 13:26:43 +01002391 graph_width = width * 3/8 - number_width - 6;
Lucian Poston678c5742012-04-18 14:12:18 -07002392 if (graph_width < 6)
2393 graph_width = 6;
2394 }
2395
Zbigniew Jędrzejewski-Szmek969fe572012-03-01 13:26:45 +01002396 if (options->stat_graph_width &&
2397 graph_width > options->stat_graph_width)
2398 graph_width = options->stat_graph_width;
Zbigniew Jędrzejewski-Szmek1b058bc2012-03-01 13:26:43 +01002399 if (name_width > width - number_width - 6 - graph_width)
2400 name_width = width - number_width - 6 - graph_width;
2401 else
2402 graph_width = width - number_width - 6 - name_width;
2403 }
2404
2405 /*
2406 * From here name_width is the width of the name area,
2407 * and graph_width is the width of the graph area.
2408 * max_change is used to scale graph properly.
2409 */
Michael J Gruber808e1db2011-05-27 14:36:41 +02002410 for (i = 0; i < count; i++) {
Timo Hirvonend2543b82006-06-24 20:20:32 +03002411 const char *prefix = "";
Junio C Hamanoaf0ed812012-11-27 11:24:54 -08002412 struct diffstat_file *file = data->files[i];
2413 char *name = file->print_name;
2414 uintmax_t added = file->added;
2415 uintmax_t deleted = file->deleted;
Junio C Hamanoa2540022006-09-26 18:53:02 -07002416 int name_len;
Junio C Hamano6973dca2006-04-21 23:57:45 -07002417
Junio C Hamanoa20d3c02012-11-27 11:47:46 -08002418 if (!file->is_interesting && (added + deleted == 0))
Michael J Gruber358e4602011-05-27 14:36:40 +02002419 continue;
Junio C Hamanoa20d3c02012-11-27 11:47:46 -08002420
Junio C Hamano6973dca2006-04-21 23:57:45 -07002421 /*
2422 * "scale" the filename
2423 */
Junio C Hamanoa2540022006-09-26 18:53:02 -07002424 len = name_width;
2425 name_len = strlen(name);
2426 if (name_width < name_len) {
Junio C Hamano6973dca2006-04-21 23:57:45 -07002427 char *slash;
2428 prefix = "...";
Junio C Hamanoa2540022006-09-26 18:53:02 -07002429 len -= 3;
2430 name += name_len - len;
Junio C Hamano6973dca2006-04-21 23:57:45 -07002431 slash = strchr(name, '/');
2432 if (slash)
2433 name = slash;
2434 }
Junio C Hamano6973dca2006-04-21 23:57:45 -07002435
Junio C Hamanoaf0ed812012-11-27 11:24:54 -08002436 if (file->is_binary) {
Stefan Beller0911c472017-06-29 17:07:02 -07002437 strbuf_addf(&out, " %s%-*s |", prefix, len, name);
2438 strbuf_addf(&out, " %*s", number_width, "Bin");
Zbigniew Jędrzejewski-Szmeke18872b2012-05-01 19:10:14 +02002439 if (!added && !deleted) {
Stefan Beller0911c472017-06-29 17:07:02 -07002440 strbuf_addch(&out, '\n');
2441 emit_diff_symbol(options, DIFF_SYMBOL_STATS_LINE,
2442 out.buf, out.len, 0);
2443 strbuf_reset(&out);
Zbigniew Jędrzejewski-Szmeke18872b2012-05-01 19:10:14 +02002444 continue;
2445 }
Stefan Beller0911c472017-06-29 17:07:02 -07002446 strbuf_addf(&out, " %s%"PRIuMAX"%s",
Jeff King0974c112010-04-17 13:41:08 -04002447 del_c, deleted, reset);
Stefan Beller0911c472017-06-29 17:07:02 -07002448 strbuf_addstr(&out, " -> ");
2449 strbuf_addf(&out, "%s%"PRIuMAX"%s",
Jeff King0974c112010-04-17 13:41:08 -04002450 add_c, added, reset);
Stefan Beller0911c472017-06-29 17:07:02 -07002451 strbuf_addstr(&out, " bytes\n");
2452 emit_diff_symbol(options, DIFF_SYMBOL_STATS_LINE,
2453 out.buf, out.len, 0);
2454 strbuf_reset(&out);
Junio C Hamanof6046522007-12-11 23:46:30 -08002455 continue;
Junio C Hamano6973dca2006-04-21 23:57:45 -07002456 }
Junio C Hamanoaf0ed812012-11-27 11:24:54 -08002457 else if (file->is_unmerged) {
Stefan Beller0911c472017-06-29 17:07:02 -07002458 strbuf_addf(&out, " %s%-*s |", prefix, len, name);
2459 strbuf_addstr(&out, " Unmerged\n");
2460 emit_diff_symbol(options, DIFF_SYMBOL_STATS_LINE,
2461 out.buf, out.len, 0);
2462 strbuf_reset(&out);
Junio C Hamanof6046522007-12-11 23:46:30 -08002463 continue;
Junio C Hamano6973dca2006-04-21 23:57:45 -07002464 }
Junio C Hamano6973dca2006-04-21 23:57:45 -07002465
Junio C Hamanoa2540022006-09-26 18:53:02 -07002466 /*
2467 * scale the add/delete
2468 */
Junio C Hamano6973dca2006-04-21 23:57:45 -07002469 add = added;
2470 del = deleted;
Junio C Hamano6973dca2006-04-21 23:57:45 -07002471
Zbigniew Jędrzejewski-Szmek1b058bc2012-03-01 13:26:43 +01002472 if (graph_width <= max_change) {
Stefan Bellerd3c9cf32013-07-14 23:35:49 +02002473 int total = scale_linear(add + del, graph_width, max_change);
Junio C Hamano2eeeef22012-02-14 13:49:11 -08002474 if (total < 2 && add && del)
2475 /* width >= 2 due to the sanity check */
2476 total = 2;
2477 if (add < del) {
Zbigniew Jędrzejewski-Szmek1b058bc2012-03-01 13:26:43 +01002478 add = scale_linear(add, graph_width, max_change);
Junio C Hamano2eeeef22012-02-14 13:49:11 -08002479 del = total - add;
2480 } else {
Zbigniew Jędrzejewski-Szmek1b058bc2012-03-01 13:26:43 +01002481 del = scale_linear(del, graph_width, max_change);
Junio C Hamano2eeeef22012-02-14 13:49:11 -08002482 add = total - del;
2483 }
Junio C Hamano6973dca2006-04-21 23:57:45 -07002484 }
Stefan Beller0911c472017-06-29 17:07:02 -07002485 strbuf_addf(&out, " %s%-*s |", prefix, len, name);
2486 strbuf_addf(&out, " %*"PRIuMAX"%s",
Zbigniew Jędrzejewski-Szmekdc801e72012-04-30 22:38:58 +02002487 number_width, added + deleted,
2488 added + deleted ? " " : "");
Stefan Beller0911c472017-06-29 17:07:02 -07002489 show_graph(&out, '+', add, add_c, reset);
2490 show_graph(&out, '-', del, del_c, reset);
2491 strbuf_addch(&out, '\n');
2492 emit_diff_symbol(options, DIFF_SYMBOL_STATS_LINE,
2493 out.buf, out.len, 0);
2494 strbuf_reset(&out);
Junio C Hamano6973dca2006-04-21 23:57:45 -07002495 }
Junio C Hamanoa20d3c02012-11-27 11:47:46 -08002496
2497 for (i = 0; i < data->nr; i++) {
Junio C Hamanoaf0ed812012-11-27 11:24:54 -08002498 struct diffstat_file *file = data->files[i];
2499 uintmax_t added = file->added;
2500 uintmax_t deleted = file->deleted;
Junio C Hamano82dfc2c2012-11-27 12:05:10 -08002501
2502 if (file->is_unmerged ||
2503 (!file->is_interesting && (added + deleted == 0))) {
Michael J Gruber808e1db2011-05-27 14:36:41 +02002504 total_files--;
2505 continue;
2506 }
Junio C Hamanoa20d3c02012-11-27 11:47:46 -08002507
Junio C Hamano82dfc2c2012-11-27 12:05:10 -08002508 if (!file->is_binary) {
Junio C Hamanoa20d3c02012-11-27 11:47:46 -08002509 adds += added;
2510 dels += deleted;
2511 }
2512 if (i < count)
2513 continue;
Junio C Hamanoe5f85df2011-05-27 21:50:39 -07002514 if (!extra_shown)
Stefan Beller0911c472017-06-29 17:07:02 -07002515 emit_diff_symbol(options,
2516 DIFF_SYMBOL_STATS_SUMMARY_ABBREV,
2517 NULL, 0, 0);
Junio C Hamanoe5f85df2011-05-27 21:50:39 -07002518 extra_shown = 1;
Michael J Gruber808e1db2011-05-27 14:36:41 +02002519 }
Stefan Beller0911c472017-06-29 17:07:02 -07002520
2521 print_stat_summary_inserts_deletes(options, total_files, adds, dels);
Rene Scharfe5a612012017-08-30 19:49:43 +02002522 strbuf_release(&out);
Junio C Hamano6973dca2006-04-21 23:57:45 -07002523}
2524
Felipe Contreras2775d922009-10-11 23:46:11 +03002525static void show_shortstats(struct diffstat_t *data, struct diff_options *options)
Nicolas Pitreebd124c2006-12-14 23:15:44 -05002526{
2527 int i, adds = 0, dels = 0, total_files = data->nr;
2528
2529 if (data->nr == 0)
2530 return;
2531
2532 for (i = 0; i < data->nr; i++) {
Zbigniew Jędrzejewski-Szmeke18872b2012-05-01 19:10:14 +02002533 int added = data->files[i]->added;
Stefan Beller0911c472017-06-29 17:07:02 -07002534 int deleted = data->files[i]->deleted;
Zbigniew Jędrzejewski-Szmeke18872b2012-05-01 19:10:14 +02002535
Junio C Hamano20c8cde2012-11-27 14:19:36 -08002536 if (data->files[i]->is_unmerged ||
2537 (!data->files[i]->is_interesting && (added + deleted == 0))) {
Zbigniew Jędrzejewski-Szmeke18872b2012-05-01 19:10:14 +02002538 total_files--;
Alexander Strasserde9658b2012-06-15 23:50:30 +02002539 } else if (!data->files[i]->is_binary) { /* don't count bytes */
Zbigniew Jędrzejewski-Szmeke18872b2012-05-01 19:10:14 +02002540 adds += added;
2541 dels += deleted;
Nicolas Pitreebd124c2006-12-14 23:15:44 -05002542 }
Nicolas Pitreebd124c2006-12-14 23:15:44 -05002543 }
Stefan Beller0911c472017-06-29 17:07:02 -07002544 print_stat_summary_inserts_deletes(options, total_files, adds, dels);
Nicolas Pitreebd124c2006-12-14 23:15:44 -05002545}
2546
Felipe Contreras4b25d092009-05-01 12:06:36 +03002547static void show_numstat(struct diffstat_t *data, struct diff_options *options)
Junio C Hamano74e2abe2006-10-12 03:01:00 -07002548{
2549 int i;
2550
Junio C Hamanof6046522007-12-11 23:46:30 -08002551 if (data->nr == 0)
2552 return;
2553
Junio C Hamano74e2abe2006-10-12 03:01:00 -07002554 for (i = 0; i < data->nr; i++) {
2555 struct diffstat_file *file = data->files[i];
2556
John Keeping30997bb2013-02-07 20:15:27 +00002557 fprintf(options->file, "%s", diff_line_prefix(options));
Bo Yang7be57612010-05-26 15:23:54 +08002558
Junio C Hamanobfddbc52006-12-10 13:50:59 -08002559 if (file->is_binary)
Daniel Barkalowc0c77732008-03-09 22:43:39 -04002560 fprintf(options->file, "-\t-\t");
Junio C Hamanobfddbc52006-12-10 13:50:59 -08002561 else
Daniel Barkalowc0c77732008-03-09 22:43:39 -04002562 fprintf(options->file,
Jeff King0974c112010-04-17 13:41:08 -04002563 "%"PRIuMAX"\t%"PRIuMAX"\t",
2564 file->added, file->deleted);
Junio C Hamanof6046522007-12-11 23:46:30 -08002565 if (options->line_termination) {
2566 fill_print_name(file);
2567 if (!file->is_renamed)
Daniel Barkalowc0c77732008-03-09 22:43:39 -04002568 write_name_quoted(file->name, options->file,
Junio C Hamanof6046522007-12-11 23:46:30 -08002569 options->line_termination);
2570 else {
Daniel Barkalowc0c77732008-03-09 22:43:39 -04002571 fputs(file->print_name, options->file);
2572 putc(options->line_termination, options->file);
Junio C Hamanof6046522007-12-11 23:46:30 -08002573 }
Pierre Habouzit663af342007-09-20 00:42:15 +02002574 } else {
Junio C Hamanof6046522007-12-11 23:46:30 -08002575 if (file->is_renamed) {
Daniel Barkalowc0c77732008-03-09 22:43:39 -04002576 putc('\0', options->file);
2577 write_name_quoted(file->from_name, options->file, '\0');
Junio C Hamanof6046522007-12-11 23:46:30 -08002578 }
Daniel Barkalowc0c77732008-03-09 22:43:39 -04002579 write_name_quoted(file->name, options->file, '\0');
Pierre Habouzit663af342007-09-20 00:42:15 +02002580 }
Junio C Hamano74e2abe2006-10-12 03:01:00 -07002581 }
2582}
2583
Junio C Hamanoc04a7152008-02-12 17:06:58 -08002584struct dirstat_file {
2585 const char *name;
2586 unsigned long changed;
Linus Torvalds7df7c012008-02-12 13:26:31 -08002587};
2588
Junio C Hamanoc04a7152008-02-12 17:06:58 -08002589struct dirstat_dir {
2590 struct dirstat_file *files;
Johan Herland712d2c72011-04-29 11:36:20 +02002591 int alloc, nr, permille, cumulative;
Junio C Hamanoc04a7152008-02-12 17:06:58 -08002592};
2593
Bo Yang7be57612010-05-26 15:23:54 +08002594static long gather_dirstat(struct diff_options *opt, struct dirstat_dir *dir,
2595 unsigned long changed, const char *base, int baselen)
Linus Torvalds7df7c012008-02-12 13:26:31 -08002596{
2597 unsigned long this_dir = 0;
2598 unsigned int sources = 0;
John Keeping30997bb2013-02-07 20:15:27 +00002599 const char *line_prefix = diff_line_prefix(opt);
Linus Torvalds7df7c012008-02-12 13:26:31 -08002600
2601 while (dir->nr) {
Junio C Hamanoc04a7152008-02-12 17:06:58 -08002602 struct dirstat_file *f = dir->files;
Linus Torvalds7df7c012008-02-12 13:26:31 -08002603 int namelen = strlen(f->name);
2604 unsigned long this;
2605 char *slash;
2606
2607 if (namelen < baselen)
2608 break;
2609 if (memcmp(f->name, base, baselen))
2610 break;
2611 slash = strchr(f->name + baselen, '/');
2612 if (slash) {
2613 int newbaselen = slash + 1 - f->name;
Bo Yang7be57612010-05-26 15:23:54 +08002614 this = gather_dirstat(opt, dir, changed, f->name, newbaselen);
Linus Torvalds7df7c012008-02-12 13:26:31 -08002615 sources++;
2616 } else {
Junio C Hamanoc04a7152008-02-12 17:06:58 -08002617 this = f->changed;
Linus Torvalds7df7c012008-02-12 13:26:31 -08002618 dir->files++;
2619 dir->nr--;
2620 sources += 2;
2621 }
2622 this_dir += this;
2623 }
2624
2625 /*
2626 * We don't report dirstat's for
2627 * - the top level
2628 * - or cases where everything came from a single directory
2629 * under this directory (sources == 1).
2630 */
2631 if (baselen && sources != 1) {
Johan Herland58a87562011-04-29 11:36:17 +02002632 if (this_dir) {
2633 int permille = this_dir * 1000 / changed;
Johan Herland712d2c72011-04-29 11:36:20 +02002634 if (permille >= dir->permille) {
Bo Yang7be57612010-05-26 15:23:54 +08002635 fprintf(opt->file, "%s%4d.%01d%% %.*s\n", line_prefix,
Johan Herland712d2c72011-04-29 11:36:20 +02002636 permille / 10, permille % 10, baselen, base);
Linus Torvalds7df7c012008-02-12 13:26:31 -08002637 if (!dir->cumulative)
2638 return 0;
2639 }
2640 }
2641 }
2642 return this_dir;
2643}
2644
Linus Torvalds441bca02008-08-28 16:19:08 -07002645static int dirstat_compare(const void *_a, const void *_b)
2646{
2647 const struct dirstat_file *a = _a;
2648 const struct dirstat_file *b = _b;
2649 return strcmp(a->name, b->name);
2650}
2651
Junio C Hamanoc04a7152008-02-12 17:06:58 -08002652static void show_dirstat(struct diff_options *options)
Linus Torvalds7df7c012008-02-12 13:26:31 -08002653{
2654 int i;
2655 unsigned long changed;
Junio C Hamanoc04a7152008-02-12 17:06:58 -08002656 struct dirstat_dir dir;
2657 struct diff_queue_struct *q = &diff_queued_diff;
Linus Torvalds7df7c012008-02-12 13:26:31 -08002658
Junio C Hamanoc04a7152008-02-12 17:06:58 -08002659 dir.files = NULL;
2660 dir.alloc = 0;
2661 dir.nr = 0;
Johan Herland712d2c72011-04-29 11:36:20 +02002662 dir.permille = options->dirstat_permille;
Brandon Williams0d1e0e72017-10-31 11:19:11 -07002663 dir.cumulative = options->flags.dirstat_cumulative;
Junio C Hamanoc04a7152008-02-12 17:06:58 -08002664
Linus Torvalds7df7c012008-02-12 13:26:31 -08002665 changed = 0;
Junio C Hamanoc04a7152008-02-12 17:06:58 -08002666 for (i = 0; i < q->nr; i++) {
2667 struct diff_filepair *p = q->queue[i];
2668 const char *name;
2669 unsigned long copied, added, damage;
Johan Herland0133dab2011-04-11 00:48:51 +02002670 int content_changed;
Junio C Hamanoc04a7152008-02-12 17:06:58 -08002671
Johan Herland2ca86712011-04-12 11:24:34 +02002672 name = p->two->path ? p->two->path : p->one->path;
Junio C Hamanoc04a7152008-02-12 17:06:58 -08002673
brian m. carlson41c95602016-06-24 23:09:24 +00002674 if (p->one->oid_valid && p->two->oid_valid)
brian m. carlsona0d12c42016-06-24 23:09:23 +00002675 content_changed = oidcmp(&p->one->oid, &p->two->oid);
Johan Herland0133dab2011-04-11 00:48:51 +02002676 else
2677 content_changed = 1;
2678
Johan Herland2ff3a802011-04-11 00:48:52 +02002679 if (!content_changed) {
2680 /*
2681 * The SHA1 has not changed, so pre-/post-content is
2682 * identical. We can therefore skip looking at the
2683 * file contents altogether.
2684 */
2685 damage = 0;
2686 goto found_damage;
2687 }
2688
Brandon Williams0d1e0e72017-10-31 11:19:11 -07002689 if (options->flags.dirstat_by_file) {
Johan Herland0133dab2011-04-11 00:48:51 +02002690 /*
2691 * In --dirstat-by-file mode, we don't really need to
2692 * look at the actual file contents at all.
2693 * The fact that the SHA1 changed is enough for us to
2694 * add this file to the list of results
2695 * (with each file contributing equal damage).
2696 */
Johan Herland2ff3a802011-04-11 00:48:52 +02002697 damage = 1;
Johan Herland0133dab2011-04-11 00:48:51 +02002698 goto found_damage;
2699 }
Junio C Hamanoc04a7152008-02-12 17:06:58 -08002700
2701 if (DIFF_FILE_VALID(p->one) && DIFF_FILE_VALID(p->two)) {
2702 diff_populate_filespec(p->one, 0);
2703 diff_populate_filespec(p->two, 0);
Tobias Klauser974e0042016-11-14 14:39:05 +01002704 diffcore_count_changes(p->one, p->two, NULL, NULL,
Junio C Hamanoc04a7152008-02-12 17:06:58 -08002705 &copied, &added);
2706 diff_free_filespec_data(p->one);
2707 diff_free_filespec_data(p->two);
2708 } else if (DIFF_FILE_VALID(p->one)) {
Nguyễn Thái Ngọc Duy8e5dd3d2014-08-16 10:08:04 +07002709 diff_populate_filespec(p->one, CHECK_SIZE_ONLY);
Junio C Hamanoc04a7152008-02-12 17:06:58 -08002710 copied = added = 0;
2711 diff_free_filespec_data(p->one);
2712 } else if (DIFF_FILE_VALID(p->two)) {
Nguyễn Thái Ngọc Duy8e5dd3d2014-08-16 10:08:04 +07002713 diff_populate_filespec(p->two, CHECK_SIZE_ONLY);
Junio C Hamanoc04a7152008-02-12 17:06:58 -08002714 copied = 0;
2715 added = p->two->size;
2716 diff_free_filespec_data(p->two);
2717 } else
Junio C Hamano2b0b5512008-02-24 17:37:15 -08002718 continue;
Junio C Hamanoc04a7152008-02-12 17:06:58 -08002719
2720 /*
2721 * Original minus copied is the removed material,
2722 * added is the new material. They are both damages
Johan Herland0133dab2011-04-11 00:48:51 +02002723 * made to the preimage.
Johan Herland2ff3a802011-04-11 00:48:52 +02002724 * If the resulting damage is zero, we know that
2725 * diffcore_count_changes() considers the two entries to
2726 * be identical, but since content_changed is true, we
2727 * know that there must have been _some_ kind of change,
2728 * so we force all entries to have damage > 0.
Junio C Hamanoc04a7152008-02-12 17:06:58 -08002729 */
2730 damage = (p->one->size - copied) + added;
Johan Herland2ff3a802011-04-11 00:48:52 +02002731 if (!damage)
Heikki Orsilafd337772008-09-05 22:27:35 +03002732 damage = 1;
Junio C Hamanoc04a7152008-02-12 17:06:58 -08002733
Johan Herland0133dab2011-04-11 00:48:51 +02002734found_damage:
Junio C Hamanoc04a7152008-02-12 17:06:58 -08002735 ALLOC_GROW(dir.files, dir.nr + 1, dir.alloc);
2736 dir.files[dir.nr].name = name;
2737 dir.files[dir.nr].changed = damage;
2738 changed += damage;
2739 dir.nr++;
Linus Torvalds7df7c012008-02-12 13:26:31 -08002740 }
2741
2742 /* This can happen even with many files, if everything was renames */
2743 if (!changed)
2744 return;
2745
2746 /* Show all directories with more than x% of the changes */
René Scharfe9ed0d8d2016-09-29 17:27:31 +02002747 QSORT(dir.files, dir.nr, dirstat_compare);
Bo Yang7be57612010-05-26 15:23:54 +08002748 gather_dirstat(options, &dir, changed, "", 0);
Linus Torvalds7df7c012008-02-12 13:26:31 -08002749}
2750
Johan Herland1c57a622011-04-29 11:36:21 +02002751static void show_dirstat_by_line(struct diffstat_t *data, struct diff_options *options)
2752{
2753 int i;
2754 unsigned long changed;
2755 struct dirstat_dir dir;
2756
2757 if (data->nr == 0)
2758 return;
2759
2760 dir.files = NULL;
2761 dir.alloc = 0;
2762 dir.nr = 0;
2763 dir.permille = options->dirstat_permille;
Brandon Williams0d1e0e72017-10-31 11:19:11 -07002764 dir.cumulative = options->flags.dirstat_cumulative;
Johan Herland1c57a622011-04-29 11:36:21 +02002765
2766 changed = 0;
2767 for (i = 0; i < data->nr; i++) {
2768 struct diffstat_file *file = data->files[i];
2769 unsigned long damage = file->added + file->deleted;
2770 if (file->is_binary)
2771 /*
2772 * binary files counts bytes, not lines. Must find some
2773 * way to normalize binary bytes vs. textual lines.
2774 * The following heuristic assumes that there are 64
2775 * bytes per "line".
2776 * This is stupid and ugly, but very cheap...
2777 */
René Scharfe42c78a22017-07-08 12:35:35 +02002778 damage = DIV_ROUND_UP(damage, 64);
Johan Herland1c57a622011-04-29 11:36:21 +02002779 ALLOC_GROW(dir.files, dir.nr + 1, dir.alloc);
2780 dir.files[dir.nr].name = file->name;
2781 dir.files[dir.nr].changed = damage;
2782 changed += damage;
2783 dir.nr++;
2784 }
2785
2786 /* This can happen even with many files, if everything was renames */
2787 if (!changed)
2788 return;
2789
2790 /* Show all directories with more than x% of the changes */
René Scharfe9ed0d8d2016-09-29 17:27:31 +02002791 QSORT(dir.files, dir.nr, dirstat_compare);
Johan Herland1c57a622011-04-29 11:36:21 +02002792 gather_dirstat(options, &dir, changed, "", 0);
2793}
2794
Junio C Hamanof6046522007-12-11 23:46:30 -08002795static void free_diffstat_info(struct diffstat_t *diffstat)
2796{
2797 int i;
2798 for (i = 0; i < diffstat->nr; i++) {
2799 struct diffstat_file *f = diffstat->files[i];
2800 if (f->name != f->print_name)
2801 free(f->print_name);
2802 free(f->name);
2803 free(f->from_name);
2804 free(f);
2805 }
2806 free(diffstat->files);
2807}
2808
Johannes Schindelin88246892006-05-20 23:43:13 +02002809struct checkdiff_t {
Johannes Schindelin88246892006-05-20 23:43:13 +02002810 const char *filename;
Junio C Hamano1ba111d2008-06-26 15:36:34 -07002811 int lineno;
Junio C Hamanoa757c642010-03-24 19:21:32 -07002812 int conflict_marker_size;
Junio C Hamano1ba111d2008-06-26 15:36:34 -07002813 struct diff_options *o;
Junio C Hamanocf1b7862007-12-06 00:14:14 -08002814 unsigned ws_rule;
Wincent Colaiuta62c64892007-12-13 21:24:52 +01002815 unsigned status;
Johannes Schindelin88246892006-05-20 23:43:13 +02002816};
2817
Junio C Hamanoa757c642010-03-24 19:21:32 -07002818static int is_conflict_marker(const char *line, int marker_size, unsigned long len)
Junio C Hamano04954042008-06-26 15:37:21 -07002819{
2820 char firstchar;
2821 int cnt;
2822
Junio C Hamanoa757c642010-03-24 19:21:32 -07002823 if (len < marker_size + 1)
Junio C Hamano04954042008-06-26 15:37:21 -07002824 return 0;
2825 firstchar = line[0];
2826 switch (firstchar) {
Junio C Hamanoa757c642010-03-24 19:21:32 -07002827 case '=': case '>': case '<': case '|':
Junio C Hamano04954042008-06-26 15:37:21 -07002828 break;
2829 default:
2830 return 0;
2831 }
Junio C Hamanoa757c642010-03-24 19:21:32 -07002832 for (cnt = 1; cnt < marker_size; cnt++)
Junio C Hamano04954042008-06-26 15:37:21 -07002833 if (line[cnt] != firstchar)
2834 return 0;
Junio C Hamanoa757c642010-03-24 19:21:32 -07002835 /* line[1] thru line[marker_size-1] are same as firstchar */
2836 if (len < marker_size + 1 || !isspace(line[marker_size]))
Junio C Hamano04954042008-06-26 15:37:21 -07002837 return 0;
Junio C Hamano04954042008-06-26 15:37:21 -07002838 return 1;
2839}
2840
Johannes Schindelin88246892006-05-20 23:43:13 +02002841static void checkdiff_consume(void *priv, char *line, unsigned long len)
2842{
2843 struct checkdiff_t *data = priv;
Junio C Hamanoa757c642010-03-24 19:21:32 -07002844 int marker_size = data->conflict_marker_size;
Jeff Kingf1c96262011-08-17 22:03:12 -07002845 const char *ws = diff_get_color(data->o->use_color, DIFF_WHITESPACE);
2846 const char *reset = diff_get_color(data->o->use_color, DIFF_RESET);
2847 const char *set = diff_get_color(data->o->use_color, DIFF_FILE_NEW);
Wincent Colaiutac1795bb2007-12-13 14:32:29 +01002848 char *err;
John Keeping30997bb2013-02-07 20:15:27 +00002849 const char *line_prefix;
Bo Yang7be57612010-05-26 15:23:54 +08002850
2851 assert(data->o);
John Keeping30997bb2013-02-07 20:15:27 +00002852 line_prefix = diff_line_prefix(data->o);
Johannes Schindelin88246892006-05-20 23:43:13 +02002853
2854 if (line[0] == '+') {
Junio C Hamano18374e52008-06-26 13:16:33 -07002855 unsigned bad;
Junio C Hamano0ef617f2008-02-15 20:30:05 -08002856 data->lineno++;
Junio C Hamanoa757c642010-03-24 19:21:32 -07002857 if (is_conflict_marker(line + 1, marker_size, len - 1)) {
Junio C Hamano04954042008-06-26 15:37:21 -07002858 data->status |= 1;
2859 fprintf(data->o->file,
Bo Yang7be57612010-05-26 15:23:54 +08002860 "%s%s:%d: leftover conflict marker\n",
2861 line_prefix, data->filename, data->lineno);
Junio C Hamano04954042008-06-26 15:37:21 -07002862 }
Junio C Hamano8f8841e2008-06-26 15:35:21 -07002863 bad = ws_check(line + 1, len - 1, data->ws_rule);
Junio C Hamano18374e52008-06-26 13:16:33 -07002864 if (!bad)
Wincent Colaiutac1795bb2007-12-13 14:32:29 +01002865 return;
Junio C Hamano18374e52008-06-26 13:16:33 -07002866 data->status |= bad;
2867 err = whitespace_error_string(bad);
Bo Yang7be57612010-05-26 15:23:54 +08002868 fprintf(data->o->file, "%s%s:%d: %s.\n",
2869 line_prefix, data->filename, data->lineno, err);
Wincent Colaiutac1795bb2007-12-13 14:32:29 +01002870 free(err);
Bo Yanga3c158d2010-05-26 15:08:02 +08002871 emit_line(data->o, set, reset, line, 1);
Junio C Hamano8f8841e2008-06-26 15:35:21 -07002872 ws_check_emit(line + 1, len - 1, data->ws_rule,
Junio C Hamano1ba111d2008-06-26 15:36:34 -07002873 data->o->file, set, reset, ws);
Junio C Hamano877f23c2008-06-26 15:36:59 -07002874 } else if (line[0] == ' ') {
Johannes Schindelin88246892006-05-20 23:43:13 +02002875 data->lineno++;
Junio C Hamano877f23c2008-06-26 15:36:59 -07002876 } else if (line[0] == '@') {
Johannes Schindelin88246892006-05-20 23:43:13 +02002877 char *plus = strchr(line, '+');
2878 if (plus)
Junio C Hamano0ef617f2008-02-15 20:30:05 -08002879 data->lineno = strtol(plus, NULL, 10) - 1;
Johannes Schindelin88246892006-05-20 23:43:13 +02002880 else
2881 die("invalid diff");
2882 }
2883}
2884
Junio C Hamano06606262006-05-05 02:41:53 -07002885static unsigned char *deflate_it(char *data,
2886 unsigned long size,
2887 unsigned long *result_size)
Junio C Hamano051308f2006-05-04 16:51:44 -07002888{
Junio C Hamano06606262006-05-05 02:41:53 -07002889 int bound;
2890 unsigned char *deflated;
Junio C Hamanoef49a7a2011-06-10 11:52:15 -07002891 git_zstream stream;
Junio C Hamano051308f2006-05-04 16:51:44 -07002892
Junio C Hamano55bb5c92011-06-10 10:55:10 -07002893 git_deflate_init(&stream, zlib_compression_level);
Junio C Hamano225a6f12011-06-10 11:18:17 -07002894 bound = git_deflate_bound(&stream, size);
Junio C Hamano06606262006-05-05 02:41:53 -07002895 deflated = xmalloc(bound);
2896 stream.next_out = deflated;
2897 stream.avail_out = bound;
Junio C Hamano051308f2006-05-04 16:51:44 -07002898
Junio C Hamano06606262006-05-05 02:41:53 -07002899 stream.next_in = (unsigned char *)data;
2900 stream.avail_in = size;
Junio C Hamano55bb5c92011-06-10 10:55:10 -07002901 while (git_deflate(&stream, Z_FINISH) == Z_OK)
Junio C Hamano06606262006-05-05 02:41:53 -07002902 ; /* nothing */
Junio C Hamano55bb5c92011-06-10 10:55:10 -07002903 git_deflate_end(&stream);
Junio C Hamano06606262006-05-05 02:41:53 -07002904 *result_size = stream.total_out;
2905 return deflated;
Junio C Hamano051308f2006-05-04 16:51:44 -07002906}
2907
Stefan Beller4eed0eb2017-06-29 17:07:01 -07002908static void emit_binary_diff_body(struct diff_options *o,
2909 mmfile_t *one, mmfile_t *two)
Junio C Hamano051308f2006-05-04 16:51:44 -07002910{
Junio C Hamano06606262006-05-05 02:41:53 -07002911 void *cp;
2912 void *delta;
2913 void *deflated;
2914 void *data;
2915 unsigned long orig_size;
Junio C Hamano051308f2006-05-04 16:51:44 -07002916 unsigned long delta_size;
Junio C Hamano06606262006-05-05 02:41:53 -07002917 unsigned long deflate_size;
2918 unsigned long data_size;
Junio C Hamano051308f2006-05-04 16:51:44 -07002919
Junio C Hamano06606262006-05-05 02:41:53 -07002920 /* We could do deflated delta, or we could do just deflated two,
2921 * whichever is smaller.
2922 */
2923 delta = NULL;
2924 deflated = deflate_it(two->ptr, two->size, &deflate_size);
2925 if (one->size && two->size) {
2926 delta = diff_delta(one->ptr, one->size,
2927 two->ptr, two->size,
2928 &delta_size, deflate_size);
2929 if (delta) {
2930 void *to_free = delta;
2931 orig_size = delta_size;
2932 delta = deflate_it(delta, delta_size, &delta_size);
2933 free(to_free);
2934 }
2935 }
Junio C Hamano051308f2006-05-04 16:51:44 -07002936
Junio C Hamano06606262006-05-05 02:41:53 -07002937 if (delta && delta_size < deflate_size) {
Stefan Beller4eed0eb2017-06-29 17:07:01 -07002938 char *s = xstrfmt("%lu", orig_size);
2939 emit_diff_symbol(o, DIFF_SYMBOL_BINARY_DIFF_HEADER_DELTA,
2940 s, strlen(s), 0);
2941 free(s);
Junio C Hamano06606262006-05-05 02:41:53 -07002942 free(deflated);
2943 data = delta;
2944 data_size = delta_size;
Stefan Beller4eed0eb2017-06-29 17:07:01 -07002945 } else {
2946 char *s = xstrfmt("%lu", two->size);
2947 emit_diff_symbol(o, DIFF_SYMBOL_BINARY_DIFF_HEADER_LITERAL,
2948 s, strlen(s), 0);
2949 free(s);
Junio C Hamano06606262006-05-05 02:41:53 -07002950 free(delta);
2951 data = deflated;
2952 data_size = deflate_size;
2953 }
2954
2955 /* emit data encoded in base85 */
2956 cp = data;
2957 while (data_size) {
Stefan Beller4eed0eb2017-06-29 17:07:01 -07002958 int len;
Junio C Hamano06606262006-05-05 02:41:53 -07002959 int bytes = (52 < data_size) ? 52 : data_size;
Stefan Beller4eed0eb2017-06-29 17:07:01 -07002960 char line[71];
Junio C Hamano06606262006-05-05 02:41:53 -07002961 data_size -= bytes;
Junio C Hamano051308f2006-05-04 16:51:44 -07002962 if (bytes <= 26)
2963 line[0] = bytes + 'A' - 1;
2964 else
2965 line[0] = bytes - 26 + 'a' - 1;
2966 encode_85(line + 1, cp, bytes);
Florian Forster1d7f1712006-06-18 17:18:09 +02002967 cp = (char *) cp + bytes;
Stefan Beller4eed0eb2017-06-29 17:07:01 -07002968
2969 len = strlen(line);
2970 line[len++] = '\n';
2971 line[len] = '\0';
2972
2973 emit_diff_symbol(o, DIFF_SYMBOL_BINARY_DIFF_BODY,
2974 line, len, 0);
Junio C Hamano051308f2006-05-04 16:51:44 -07002975 }
Stefan Beller4eed0eb2017-06-29 17:07:01 -07002976 emit_diff_symbol(o, DIFF_SYMBOL_BINARY_DIFF_FOOTER, NULL, 0, 0);
Junio C Hamano06606262006-05-05 02:41:53 -07002977 free(data);
Junio C Hamano051308f2006-05-04 16:51:44 -07002978}
2979
Stefan Beller4eed0eb2017-06-29 17:07:01 -07002980static void emit_binary_diff(struct diff_options *o,
2981 mmfile_t *one, mmfile_t *two)
Junio C Hamanod4c452f2006-08-16 16:08:14 -07002982{
Stefan Beller4eed0eb2017-06-29 17:07:01 -07002983 emit_diff_symbol(o, DIFF_SYMBOL_BINARY_DIFF_HEADER, NULL, 0, 0);
2984 emit_binary_diff_body(o, one, two);
2985 emit_binary_diff_body(o, two, one);
Junio C Hamanod4c452f2006-08-16 16:08:14 -07002986}
2987
Junio C Hamano29a3eef2007-07-06 00:18:54 -07002988int diff_filespec_is_binary(struct diff_filespec *one)
2989{
Jeff King122aa6f2008-10-05 17:43:36 -04002990 if (one->is_binary == -1) {
2991 diff_filespec_load_driver(one);
2992 if (one->driver->binary != -1)
2993 one->is_binary = one->driver->binary;
2994 else {
2995 if (!one->data && DIFF_FILE_VALID(one))
Nguyễn Thái Ngọc Duy6bf3b812014-08-16 10:08:05 +07002996 diff_populate_filespec(one, CHECK_BINARY);
2997 if (one->is_binary == -1 && one->data)
Jeff King122aa6f2008-10-05 17:43:36 -04002998 one->is_binary = buffer_is_binary(one->data,
2999 one->size);
3000 if (one->is_binary == -1)
3001 one->is_binary = 0;
3002 }
3003 }
Junio C Hamano29a3eef2007-07-06 00:18:54 -07003004 return one->is_binary;
Junio C Hamano6973dca2006-04-21 23:57:45 -07003005}
3006
Jeff Kingbe58e702008-10-05 17:43:21 -04003007static const struct userdiff_funcname *diff_funcname_pattern(struct diff_filespec *one)
Junio C Hamanof2584752007-07-06 00:45:10 -07003008{
Jeff King122aa6f2008-10-05 17:43:36 -04003009 diff_filespec_load_driver(one);
3010 return one->driver->funcname.pattern ? &one->driver->funcname : NULL;
Junio C Hamanof2584752007-07-06 00:45:10 -07003011}
3012
Junio C Hamanoa5a818e2008-08-18 20:08:09 -07003013void diff_set_mnemonic_prefix(struct diff_options *options, const char *a, const char *b)
3014{
3015 if (!options->a_prefix)
3016 options->a_prefix = a;
3017 if (!options->b_prefix)
3018 options->b_prefix = b;
3019}
3020
Axel Bonneta788d7d2010-06-07 17:23:36 +02003021struct userdiff_driver *get_textconv(struct diff_filespec *one)
Jeff King04427ac2008-10-26 00:44:53 -04003022{
3023 if (!DIFF_FILE_VALID(one))
3024 return NULL;
Jeff Kingd391c0f2010-09-21 17:01:24 -04003025
Jeff King04427ac2008-10-26 00:44:53 -04003026 diff_filespec_load_driver(one);
Jeff King3813e692011-05-23 16:30:14 -04003027 return userdiff_get_textconv(one->driver);
Jeff King04427ac2008-10-26 00:44:53 -04003028}
3029
Junio C Hamano6973dca2006-04-21 23:57:45 -07003030static void builtin_diff(const char *name_a,
3031 const char *name_b,
3032 struct diff_filespec *one,
3033 struct diff_filespec *two,
3034 const char *xfrm_msg,
Christian Couder296c6bb2010-05-26 04:50:12 +02003035 int must_show_header,
Junio C Hamano051308f2006-05-04 16:51:44 -07003036 struct diff_options *o,
Junio C Hamano6973dca2006-04-21 23:57:45 -07003037 int complete_rewrite)
3038{
3039 mmfile_t mf1, mf2;
3040 const char *lbl[2];
3041 char *a_one, *b_two;
Jeff Kingd9c552f2012-11-13 21:12:46 +05303042 const char *meta = diff_get_color_opt(o, DIFF_METAINFO);
Pierre Habouzit8f67f8a2007-11-10 20:05:14 +01003043 const char *reset = diff_get_color_opt(o, DIFF_RESET);
Junio C Hamanoa5a818e2008-08-18 20:08:09 -07003044 const char *a_prefix, *b_prefix;
Jeff Kingd9bae1a2010-04-01 20:12:15 -04003045 struct userdiff_driver *textconv_one = NULL;
3046 struct userdiff_driver *textconv_two = NULL;
Greg Bacon3e97c7c2009-11-19 15:12:24 -06003047 struct strbuf header = STRBUF_INIT;
John Keeping30997bb2013-02-07 20:15:27 +00003048 const char *line_prefix = diff_line_prefix(o);
Junio C Hamano6973dca2006-04-21 23:57:45 -07003049
Junio C Hamanoa5a818e2008-08-18 20:08:09 -07003050 diff_set_mnemonic_prefix(o, "a/", "b/");
Brandon Williams0d1e0e72017-10-31 11:19:11 -07003051 if (o->flags.reverse_diff) {
Junio C Hamanoa5a818e2008-08-18 20:08:09 -07003052 a_prefix = o->b_prefix;
3053 b_prefix = o->a_prefix;
3054 } else {
3055 a_prefix = o->a_prefix;
3056 b_prefix = o->b_prefix;
3057 }
3058
Jacob Keller61cfbc02016-08-31 16:27:21 -07003059 if (o->submodule_format == DIFF_SUBMODULE_LOG &&
3060 (!one->mode || S_ISGITLINK(one->mode)) &&
3061 (!two->mode || S_ISGITLINK(two->mode))) {
Stefan Bellerf3597132017-06-29 17:07:00 -07003062 show_submodule_summary(o, one->path ? one->path : two->path,
Jacob Keller602a2832016-08-31 16:27:23 -07003063 &one->oid, &two->oid,
Stefan Bellerf3597132017-06-29 17:07:00 -07003064 two->dirty_submodule);
Junio C Hamano6973dca2006-04-21 23:57:45 -07003065 return;
Jacob Kellerfd47ae62016-08-31 16:27:25 -07003066 } else if (o->submodule_format == DIFF_SUBMODULE_INLINE_DIFF &&
3067 (!one->mode || S_ISGITLINK(one->mode)) &&
3068 (!two->mode || S_ISGITLINK(two->mode))) {
Stefan Bellerf3597132017-06-29 17:07:00 -07003069 show_submodule_inline_diff(o, one->path ? one->path : two->path,
Jacob Kellerfd47ae62016-08-31 16:27:25 -07003070 &one->oid, &two->oid,
Stefan Bellerf3597132017-06-29 17:07:00 -07003071 two->dirty_submodule);
Jacob Kellerfd47ae62016-08-31 16:27:25 -07003072 return;
Junio C Hamano6973dca2006-04-21 23:57:45 -07003073 }
3074
Brandon Williams0d1e0e72017-10-31 11:19:11 -07003075 if (o->flags.allow_textconv) {
Junio C Hamano6973dca2006-04-21 23:57:45 -07003076 textconv_one = get_textconv(one);
3077 textconv_two = get_textconv(two);
3078 }
3079
Linus Torvalds71b989e2008-10-05 15:35:15 -04003080 /* Never use a non-valid filename anywhere if at all possible */
3081 name_a = DIFF_FILE_VALID(one) ? name_a : name_b;
3082 name_b = DIFF_FILE_VALID(two) ? name_b : name_a;
3083
Junio C Hamanoa5a818e2008-08-18 20:08:09 -07003084 a_one = quote_two(a_prefix, name_a + (*name_a == '/'));
3085 b_two = quote_two(b_prefix, name_b + (*name_b == '/'));
Junio C Hamano6973dca2006-04-21 23:57:45 -07003086 lbl[0] = DIFF_FILE_VALID(one) ? a_one : "/dev/null";
3087 lbl[1] = DIFF_FILE_VALID(two) ? b_two : "/dev/null";
Jeff Kingd9c552f2012-11-13 21:12:46 +05303088 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 -07003089 if (lbl[0][0] == '/') {
3090 /* /dev/null */
Jeff Kingd9c552f2012-11-13 21:12:46 +05303091 strbuf_addf(&header, "%s%snew file mode %06o%s\n", line_prefix, meta, two->mode, reset);
Bert Wesarg37466442010-05-04 00:38:07 +02003092 if (xfrm_msg)
3093 strbuf_addstr(&header, xfrm_msg);
Christian Couder296c6bb2010-05-26 04:50:12 +02003094 must_show_header = 1;
Junio C Hamano6973dca2006-04-21 23:57:45 -07003095 }
3096 else if (lbl[1][0] == '/') {
Jeff Kingd9c552f2012-11-13 21:12:46 +05303097 strbuf_addf(&header, "%s%sdeleted file mode %06o%s\n", line_prefix, meta, one->mode, reset);
Bert Wesarg37466442010-05-04 00:38:07 +02003098 if (xfrm_msg)
3099 strbuf_addstr(&header, xfrm_msg);
Christian Couder296c6bb2010-05-26 04:50:12 +02003100 must_show_header = 1;
Junio C Hamano6973dca2006-04-21 23:57:45 -07003101 }
3102 else {
3103 if (one->mode != two->mode) {
Jeff Kingd9c552f2012-11-13 21:12:46 +05303104 strbuf_addf(&header, "%s%sold mode %06o%s\n", line_prefix, meta, one->mode, reset);
3105 strbuf_addf(&header, "%s%snew mode %06o%s\n", line_prefix, meta, two->mode, reset);
Christian Couder296c6bb2010-05-26 04:50:12 +02003106 must_show_header = 1;
Junio C Hamano6973dca2006-04-21 23:57:45 -07003107 }
Bert Wesarg37466442010-05-04 00:38:07 +02003108 if (xfrm_msg)
3109 strbuf_addstr(&header, xfrm_msg);
Greg Bacon3e97c7c2009-11-19 15:12:24 -06003110
Junio C Hamano6973dca2006-04-21 23:57:45 -07003111 /*
3112 * we do not run diff between different kind
3113 * of objects.
3114 */
3115 if ((one->mode ^ two->mode) & S_IFMT)
3116 goto free_ab_and_return;
Jeff King0c018572008-12-09 03:12:28 -05003117 if (complete_rewrite &&
Jeff King3aa1f7c2008-12-09 03:13:21 -05003118 (textconv_one || !diff_filespec_is_binary(one)) &&
3119 (textconv_two || !diff_filespec_is_binary(two))) {
Stefan Bellera29b0a12017-06-29 17:06:57 -07003120 emit_diff_symbol(o, DIFF_SYMBOL_HEADER,
3121 header.buf, header.len, 0);
Greg Bacon3e97c7c2009-11-19 15:12:24 -06003122 strbuf_reset(&header);
Jeff King3aa1f7c2008-12-09 03:13:21 -05003123 emit_rewrite_diff(name_a, name_b, one, two,
3124 textconv_one, textconv_two, o);
Johannes Schindelin34a5e1a2007-02-25 23:34:54 +01003125 o->found_changes = 1;
Junio C Hamano6973dca2006-04-21 23:57:45 -07003126 goto free_ab_and_return;
3127 }
3128 }
3129
Junio C Hamano467ddc12011-02-28 16:11:55 -08003130 if (o->irreversible_delete && lbl[1][0] == '/') {
Stefan Bellera29b0a12017-06-29 17:06:57 -07003131 emit_diff_symbol(o, DIFF_SYMBOL_HEADER, header.buf,
3132 header.len, 0);
Junio C Hamano467ddc12011-02-28 16:11:55 -08003133 strbuf_reset(&header);
3134 goto free_ab_and_return;
Brandon Williams0d1e0e72017-10-31 11:19:11 -07003135 } else if (!o->flags.text &&
Jeff Kingb3373982010-04-01 20:14:24 -04003136 ( (!textconv_one && diff_filespec_is_binary(one)) ||
3137 (!textconv_two && diff_filespec_is_binary(two)) )) {
Stefan Beller4acaaa72017-06-29 17:06:58 -07003138 struct strbuf sb = STRBUF_INIT;
Nguyễn Thái Ngọc Duy1aaf69e2014-08-16 10:08:06 +07003139 if (!one->data && !two->data &&
3140 S_ISREG(one->mode) && S_ISREG(two->mode) &&
Brandon Williams0d1e0e72017-10-31 11:19:11 -07003141 !o->flags.binary) {
brian m. carlsona0d12c42016-06-24 23:09:23 +00003142 if (!oidcmp(&one->oid, &two->oid)) {
Nguyễn Thái Ngọc Duy1aaf69e2014-08-16 10:08:06 +07003143 if (must_show_header)
Stefan Bellera29b0a12017-06-29 17:06:57 -07003144 emit_diff_symbol(o, DIFF_SYMBOL_HEADER,
3145 header.buf, header.len,
3146 0);
Nguyễn Thái Ngọc Duy1aaf69e2014-08-16 10:08:06 +07003147 goto free_ab_and_return;
3148 }
Stefan Bellera29b0a12017-06-29 17:06:57 -07003149 emit_diff_symbol(o, DIFF_SYMBOL_HEADER,
3150 header.buf, header.len, 0);
Stefan Beller4acaaa72017-06-29 17:06:58 -07003151 strbuf_addf(&sb, "%sBinary files %s and %s differ\n",
3152 diff_line_prefix(o), lbl[0], lbl[1]);
3153 emit_diff_symbol(o, DIFF_SYMBOL_BINARY_FILES,
3154 sb.buf, sb.len, 0);
3155 strbuf_release(&sb);
Nguyễn Thái Ngọc Duy1aaf69e2014-08-16 10:08:06 +07003156 goto free_ab_and_return;
3157 }
Jeff Kingb3373982010-04-01 20:14:24 -04003158 if (fill_mmfile(&mf1, one) < 0 || fill_mmfile(&mf2, two) < 0)
3159 die("unable to read files to diff");
Junio C Hamano06606262006-05-05 02:41:53 -07003160 /* Quite common confusing case */
3161 if (mf1.size == mf2.size &&
Christian Couder296c6bb2010-05-26 04:50:12 +02003162 !memcmp(mf1.ptr, mf2.ptr, mf1.size)) {
3163 if (must_show_header)
Stefan Bellera29b0a12017-06-29 17:06:57 -07003164 emit_diff_symbol(o, DIFF_SYMBOL_HEADER,
3165 header.buf, header.len, 0);
Junio C Hamano06606262006-05-05 02:41:53 -07003166 goto free_ab_and_return;
Christian Couder296c6bb2010-05-26 04:50:12 +02003167 }
Stefan Bellera29b0a12017-06-29 17:06:57 -07003168 emit_diff_symbol(o, DIFF_SYMBOL_HEADER, header.buf, header.len, 0);
Greg Bacon3e97c7c2009-11-19 15:12:24 -06003169 strbuf_reset(&header);
Brandon Williams0d1e0e72017-10-31 11:19:11 -07003170 if (o->flags.binary)
Stefan Beller4eed0eb2017-06-29 17:07:01 -07003171 emit_binary_diff(o, &mf1, &mf2);
Stefan Beller4acaaa72017-06-29 17:06:58 -07003172 else {
3173 strbuf_addf(&sb, "%sBinary files %s and %s differ\n",
3174 diff_line_prefix(o), lbl[0], lbl[1]);
3175 emit_diff_symbol(o, DIFF_SYMBOL_BINARY_FILES,
3176 sb.buf, sb.len, 0);
3177 strbuf_release(&sb);
3178 }
Johannes Schindelin34a5e1a2007-02-25 23:34:54 +01003179 o->found_changes = 1;
Junio C Hamano467ddc12011-02-28 16:11:55 -08003180 } else {
Junio C Hamano6973dca2006-04-21 23:57:45 -07003181 /* Crazy xdl interfaces.. */
3182 const char *diffopts = getenv("GIT_DIFF_OPTS");
Jeff Kingae021d82014-06-18 15:47:50 -04003183 const char *v;
Junio C Hamano6973dca2006-04-21 23:57:45 -07003184 xpparam_t xpp;
3185 xdemitconf_t xecfg;
Junio C Hamano6973dca2006-04-21 23:57:45 -07003186 struct emit_callback ecbdata;
Jeff Kingbe58e702008-10-05 17:43:21 -04003187 const struct userdiff_funcname *pe;
Junio C Hamanof2584752007-07-06 00:45:10 -07003188
Junio C Hamanob3f01ff2012-02-29 18:14:16 -08003189 if (must_show_header) {
Stefan Bellera29b0a12017-06-29 17:06:57 -07003190 emit_diff_symbol(o, DIFF_SYMBOL_HEADER,
3191 header.buf, header.len, 0);
Greg Bacon3e97c7c2009-11-19 15:12:24 -06003192 strbuf_reset(&header);
3193 }
3194
Jeff King840383b2010-04-01 20:09:26 -04003195 mf1.size = fill_textconv(textconv_one, one, &mf1.ptr);
3196 mf2.size = fill_textconv(textconv_two, two, &mf2.ptr);
Jeff King04427ac2008-10-26 00:44:53 -04003197
Brandon Casey45e7ca02008-09-18 17:40:48 -05003198 pe = diff_funcname_pattern(one);
3199 if (!pe)
3200 pe = diff_funcname_pattern(two);
Junio C Hamano6973dca2006-04-21 23:57:45 -07003201
Brian Downing9ccd0a82008-10-25 15:30:37 +02003202 memset(&xpp, 0, sizeof(xpp));
Johannes Schindelin30b25012007-07-04 19:05:46 +01003203 memset(&xecfg, 0, sizeof(xecfg));
Johannes Schindelincd112ce2006-06-13 18:45:44 +02003204 memset(&ecbdata, 0, sizeof(ecbdata));
Junio C Hamano6973dca2006-04-21 23:57:45 -07003205 ecbdata.label_path = lbl;
Jeff Kingdaa0c3d2011-08-17 22:04:23 -07003206 ecbdata.color_diff = want_color(o->use_color);
Stefan Bellerc189c4f2013-08-08 20:31:44 +02003207 ecbdata.ws_rule = whitespace_rule(name_b);
Junio C Hamano690ed842009-09-04 00:41:15 -07003208 if (ecbdata.ws_rule & WS_BLANK_AT_EOF)
Junio C Hamanod68fe262009-09-14 22:05:57 -07003209 check_blank_at_eof(&mf1, &mf2, &ecbdata);
Bo Yanga3c158d2010-05-26 15:08:02 +08003210 ecbdata.opt = o;
Greg Bacon3e97c7c2009-11-19 15:12:24 -06003211 ecbdata.header = header.len ? &header : NULL;
René Scharfe582aa002010-05-02 15:04:41 +02003212 xpp.flags = o->xdl_opts;
Jonathan Tan2477ab22017-11-27 11:47:47 -08003213 xpp.anchors = o->anchors;
3214 xpp.anchors_nr = o->anchors_nr;
Linus Torvaldsee1e5412006-05-13 13:23:48 -07003215 xecfg.ctxlen = o->context;
René Scharfe6d0e6742008-12-28 19:45:32 +01003216 xecfg.interhunkctxlen = o->interhunkcontext;
Junio C Hamano6973dca2006-04-21 23:57:45 -07003217 xecfg.flags = XDL_EMIT_FUNCNAMES;
Brandon Williams0d1e0e72017-10-31 11:19:11 -07003218 if (o->flags.funccontext)
René Scharfe14937c22011-10-09 13:36:57 +02003219 xecfg.flags |= XDL_EMIT_FUNCCONTEXT;
Brandon Casey45e7ca02008-09-18 17:40:48 -05003220 if (pe)
Brandon Caseya0135852008-09-18 17:42:48 -05003221 xdiff_set_find_func(&xecfg, pe->pattern, pe->cflags);
Junio C Hamano6973dca2006-04-21 23:57:45 -07003222 if (!diffopts)
3223 ;
Jeff Kingae021d82014-06-18 15:47:50 -04003224 else if (skip_prefix(diffopts, "--unified=", &v))
3225 xecfg.ctxlen = strtoul(v, NULL, 10);
3226 else if (skip_prefix(diffopts, "-u", &v))
3227 xecfg.ctxlen = strtoul(v, NULL, 10);
Thomas Rast77d1a522012-03-14 19:24:08 +01003228 if (o->word_diff)
3229 init_diff_words_data(&ecbdata, o, one, two);
Jeff King3efb9882015-09-24 19:12:23 -04003230 if (xdi_diff_outf(&mf1, &mf2, fn_out_consume, &ecbdata,
3231 &xpp, &xecfg))
3232 die("unable to generate diff for %s", one->path);
Thomas Rast882749a2010-04-14 17:59:06 +02003233 if (o->word_diff)
Johannes Schindelinf59a59e2006-07-28 23:56:15 +02003234 free_diff_words_data(&ecbdata);
Jeff King04427ac2008-10-26 00:44:53 -04003235 if (textconv_one)
3236 free(mf1.ptr);
3237 if (textconv_two)
3238 free(mf2.ptr);
René Scharfe8cfe5f12009-07-02 00:01:43 +02003239 xdiff_clear_find_func(&xecfg);
Junio C Hamano6973dca2006-04-21 23:57:45 -07003240 }
3241
3242 free_ab_and_return:
Greg Bacon3e97c7c2009-11-19 15:12:24 -06003243 strbuf_release(&header);
Junio C Hamanofc3abdf2007-05-03 13:05:48 -07003244 diff_free_filespec_data(one);
3245 diff_free_filespec_data(two);
Junio C Hamano6973dca2006-04-21 23:57:45 -07003246 free(a_one);
3247 free(b_two);
3248 return;
3249}
3250
3251static void builtin_diffstat(const char *name_a, const char *name_b,
3252 struct diff_filespec *one,
3253 struct diff_filespec *two,
Junio C Hamano710158e2006-04-25 23:40:09 -07003254 struct diffstat_t *diffstat,
Johannes Schindelin0d21efa2006-06-14 17:40:23 +02003255 struct diff_options *o,
Linus Torvalds74faaa12012-10-17 10:00:37 -07003256 struct diff_filepair *p)
Junio C Hamano6973dca2006-04-21 23:57:45 -07003257{
3258 mmfile_t mf1, mf2;
3259 struct diffstat_file *data;
Zbigniew Jędrzejewski-Szmek352ca4e2012-05-01 19:10:15 +02003260 int same_contents;
Linus Torvalds74faaa12012-10-17 10:00:37 -07003261 int complete_rewrite = 0;
3262
3263 if (!DIFF_PAIR_UNMERGED(p)) {
3264 if (p->status == DIFF_STATUS_MODIFIED && p->score)
3265 complete_rewrite = 1;
3266 }
Junio C Hamano6973dca2006-04-21 23:57:45 -07003267
3268 data = diffstat_add(diffstat, name_a, name_b);
Junio C Hamano99bfd402012-11-27 11:17:14 -08003269 data->is_interesting = p->status != DIFF_STATUS_UNKNOWN;
Junio C Hamano6973dca2006-04-21 23:57:45 -07003270
3271 if (!one || !two) {
3272 data->is_unmerged = 1;
3273 return;
3274 }
Jeff Kingded0abc2011-02-19 03:04:56 -05003275
brian m. carlsona0d12c42016-06-24 23:09:23 +00003276 same_contents = !oidcmp(&one->oid, &two->oid);
Zbigniew Jędrzejewski-Szmek352ca4e2012-05-01 19:10:15 +02003277
Jeff Kingded0abc2011-02-19 03:04:56 -05003278 if (diff_filespec_is_binary(one) || diff_filespec_is_binary(two)) {
Jeff Kingded0abc2011-02-19 03:04:56 -05003279 data->is_binary = 1;
Zbigniew Jędrzejewski-Szmek352ca4e2012-05-01 19:10:15 +02003280 if (same_contents) {
Zbigniew Jędrzejewski-Szmeke18872b2012-05-01 19:10:14 +02003281 data->added = 0;
3282 data->deleted = 0;
3283 } else {
3284 data->added = diff_filespec_size(two);
3285 data->deleted = diff_filespec_size(one);
3286 }
Jeff Kingded0abc2011-02-19 03:04:56 -05003287 }
3288
3289 else if (complete_rewrite) {
Junio C Hamano710158e2006-04-25 23:40:09 -07003290 diff_populate_filespec(one, 0);
3291 diff_populate_filespec(two, 0);
3292 data->deleted = count_lines(one->data, one->size);
3293 data->added = count_lines(two->data, two->size);
Junio C Hamano710158e2006-04-25 23:40:09 -07003294 }
Junio C Hamano6973dca2006-04-21 23:57:45 -07003295
Zbigniew Jędrzejewski-Szmek352ca4e2012-05-01 19:10:15 +02003296 else if (!same_contents) {
Junio C Hamano6973dca2006-04-21 23:57:45 -07003297 /* Crazy xdl interfaces.. */
3298 xpparam_t xpp;
3299 xdemitconf_t xecfg;
Junio C Hamano6973dca2006-04-21 23:57:45 -07003300
Jeff Kingded0abc2011-02-19 03:04:56 -05003301 if (fill_mmfile(&mf1, one) < 0 || fill_mmfile(&mf2, two) < 0)
3302 die("unable to read files to diff");
3303
Brian Downing9ccd0a82008-10-25 15:30:37 +02003304 memset(&xpp, 0, sizeof(xpp));
Johannes Schindelin30b25012007-07-04 19:05:46 +01003305 memset(&xecfg, 0, sizeof(xecfg));
René Scharfe582aa002010-05-02 15:04:41 +02003306 xpp.flags = o->xdl_opts;
Jonathan Tan2477ab22017-11-27 11:47:47 -08003307 xpp.anchors = o->anchors;
3308 xpp.anchors_nr = o->anchors_nr;
Junio C Hamanof01cae92011-09-22 10:54:47 -07003309 xecfg.ctxlen = o->context;
3310 xecfg.interhunkctxlen = o->interhunkcontext;
Jeff King3efb9882015-09-24 19:12:23 -04003311 if (xdi_diff_outf(&mf1, &mf2, diffstat_consume, diffstat,
3312 &xpp, &xecfg))
3313 die("unable to generate diffstat for %s", one->path);
Junio C Hamano6973dca2006-04-21 23:57:45 -07003314 }
Junio C Hamanofc3abdf2007-05-03 13:05:48 -07003315
Junio C Hamanofc3abdf2007-05-03 13:05:48 -07003316 diff_free_filespec_data(one);
3317 diff_free_filespec_data(two);
Junio C Hamano6973dca2006-04-21 23:57:45 -07003318}
3319
Johannes Schindelin88246892006-05-20 23:43:13 +02003320static void builtin_checkdiff(const char *name_a, const char *name_b,
Junio C Hamanocd676a52008-02-12 14:26:02 -08003321 const char *attr_path,
Junio C Hamano5ff10dd2008-06-26 15:34:54 -07003322 struct diff_filespec *one,
3323 struct diff_filespec *two,
3324 struct diff_options *o)
Johannes Schindelin88246892006-05-20 23:43:13 +02003325{
3326 mmfile_t mf1, mf2;
3327 struct checkdiff_t data;
3328
3329 if (!two)
3330 return;
3331
3332 memset(&data, 0, sizeof(data));
Johannes Schindelin88246892006-05-20 23:43:13 +02003333 data.filename = name_b ? name_b : name_a;
3334 data.lineno = 0;
Junio C Hamano1ba111d2008-06-26 15:36:34 -07003335 data.o = o;
Junio C Hamanocd676a52008-02-12 14:26:02 -08003336 data.ws_rule = whitespace_rule(attr_path);
Junio C Hamanoa757c642010-03-24 19:21:32 -07003337 data.conflict_marker_size = ll_merge_marker_size(attr_path);
Johannes Schindelin88246892006-05-20 23:43:13 +02003338
3339 if (fill_mmfile(&mf1, one) < 0 || fill_mmfile(&mf2, two) < 0)
3340 die("unable to read files to diff");
3341
Junio C Hamano5ff10dd2008-06-26 15:34:54 -07003342 /*
3343 * All the other codepaths check both sides, but not checking
3344 * the "old" side here is deliberate. We are checking the newly
3345 * introduced changes, and as long as the "new" side is text, we
3346 * can and should check what it introduces.
3347 */
Junio C Hamano29a3eef2007-07-06 00:18:54 -07003348 if (diff_filespec_is_binary(two))
Junio C Hamanofc3abdf2007-05-03 13:05:48 -07003349 goto free_and_return;
Johannes Schindelin88246892006-05-20 23:43:13 +02003350 else {
3351 /* Crazy xdl interfaces.. */
3352 xpparam_t xpp;
3353 xdemitconf_t xecfg;
Johannes Schindelin88246892006-05-20 23:43:13 +02003354
Brian Downing9ccd0a82008-10-25 15:30:37 +02003355 memset(&xpp, 0, sizeof(xpp));
Johannes Schindelin30b25012007-07-04 19:05:46 +01003356 memset(&xecfg, 0, sizeof(xecfg));
Junio C Hamanoc35539e2008-08-20 11:47:55 -07003357 xecfg.ctxlen = 1; /* at least one context line */
René Scharfe582aa002010-05-02 15:04:41 +02003358 xpp.flags = 0;
Jeff King3efb9882015-09-24 19:12:23 -04003359 if (xdi_diff_outf(&mf1, &mf2, checkdiff_consume, &data,
3360 &xpp, &xecfg))
3361 die("unable to generate checkdiff for %s", one->path);
Junio C Hamano877f23c2008-06-26 15:36:59 -07003362
Junio C Hamano467babf2009-09-03 23:39:43 -07003363 if (data.ws_rule & WS_BLANK_AT_EOF) {
Junio C Hamanod68fe262009-09-14 22:05:57 -07003364 struct emit_callback ecbdata;
3365 int blank_at_eof;
3366
3367 ecbdata.ws_rule = data.ws_rule;
3368 check_blank_at_eof(&mf1, &mf2, &ecbdata);
Christoph Mallon8837d332010-10-10 19:24:06 +02003369 blank_at_eof = ecbdata.blank_at_eof_in_postimage;
Junio C Hamanod68fe262009-09-14 22:05:57 -07003370
Junio C Hamano467babf2009-09-03 23:39:43 -07003371 if (blank_at_eof) {
3372 static char *err;
3373 if (!err)
3374 err = whitespace_error_string(WS_BLANK_AT_EOF);
3375 fprintf(o->file, "%s:%d: %s.\n",
3376 data.filename, blank_at_eof, err);
3377 data.status = 1; /* report errors */
3378 }
Junio C Hamano877f23c2008-06-26 15:36:59 -07003379 }
Johannes Schindelin88246892006-05-20 23:43:13 +02003380 }
Junio C Hamanofc3abdf2007-05-03 13:05:48 -07003381 free_and_return:
3382 diff_free_filespec_data(one);
3383 diff_free_filespec_data(two);
Wincent Colaiuta62c64892007-12-13 21:24:52 +01003384 if (data.status)
Brandon Williams0d1e0e72017-10-31 11:19:11 -07003385 o->flags.check_failed = 1;
Johannes Schindelin88246892006-05-20 23:43:13 +02003386}
3387
Junio C Hamano6973dca2006-04-21 23:57:45 -07003388struct diff_filespec *alloc_filespec(const char *path)
3389{
Jeff King96ffc062016-02-22 17:44:32 -05003390 struct diff_filespec *spec;
Junio C Hamano6973dca2006-04-21 23:57:45 -07003391
Jeff King96ffc062016-02-22 17:44:32 -05003392 FLEXPTR_ALLOC_STR(spec, path, path);
Linus Torvalds9fb88412007-10-25 11:19:10 -07003393 spec->count = 1;
Jeff King122aa6f2008-10-05 17:43:36 -04003394 spec->is_binary = -1;
Junio C Hamano6973dca2006-04-21 23:57:45 -07003395 return spec;
3396}
3397
Linus Torvalds9fb88412007-10-25 11:19:10 -07003398void free_filespec(struct diff_filespec *spec)
3399{
3400 if (!--spec->count) {
3401 diff_free_filespec_data(spec);
3402 free(spec);
3403 }
3404}
3405
Brandon Williamsf9704c22017-05-30 10:30:50 -07003406void fill_filespec(struct diff_filespec *spec, const struct object_id *oid,
3407 int oid_valid, unsigned short mode)
Junio C Hamano6973dca2006-04-21 23:57:45 -07003408{
3409 if (mode) {
3410 spec->mode = canon_mode(mode);
Brandon Williamsf9704c22017-05-30 10:30:50 -07003411 oidcpy(&spec->oid, oid);
3412 spec->oid_valid = oid_valid;
Junio C Hamano6973dca2006-04-21 23:57:45 -07003413 }
3414}
3415
3416/*
Jakub Narebski5adf3172007-05-26 00:37:40 +02003417 * Given a name and sha1 pair, if the index tells us the file in
Junio C Hamano6973dca2006-04-21 23:57:45 -07003418 * the work tree has that object contents, return true, so that
3419 * prepare_temp_file() does not have to inflate and extract.
3420 */
Brandon Williamsfb4a1c02017-05-30 10:30:51 -07003421static int reuse_worktree_file(const char *name, const struct object_id *oid, int want_file)
Junio C Hamano6973dca2006-04-21 23:57:45 -07003422{
Nguyễn Thái Ngọc Duy9c5e6c82013-07-09 22:29:00 +07003423 const struct cache_entry *ce;
Junio C Hamano6973dca2006-04-21 23:57:45 -07003424 struct stat st;
3425 int pos, len;
3426
Junio C Hamano150115a2009-03-22 15:26:07 -07003427 /*
3428 * We do not read the cache ourselves here, because the
Junio C Hamano6973dca2006-04-21 23:57:45 -07003429 * benchmark with my previous version that always reads cache
3430 * shows that it makes things worse for diff-tree comparing
3431 * two linux-2.6 kernel trees in an already checked out work
3432 * tree. This is because most diff-tree comparisons deal with
3433 * only a small number of files, while reading the cache is
3434 * expensive for a large project, and its cost outweighs the
3435 * savings we get by not inflating the object to a temporary
3436 * file. Practically, this code only helps when we are used
3437 * by diff-cache --cached, which does read the cache before
3438 * calling us.
3439 */
3440 if (!active_cache)
3441 return 0;
3442
Shawn O. Pearce1510fea2006-12-14 06:15:57 -05003443 /* We want to avoid the working directory if our caller
3444 * doesn't need the data in a normal file, this system
3445 * is rather slow with its stat/open/mmap/close syscalls,
3446 * and the object is contained in a pack file. The pack
3447 * is probably already open and will be faster to obtain
3448 * the data through than the working directory. Loose
3449 * objects however would tend to be slower as they need
3450 * to be individually opened and inflated.
3451 */
Brandon Williamsfb4a1c02017-05-30 10:30:51 -07003452 if (!FAST_WORKING_DIRECTORY && !want_file && has_sha1_pack(oid->hash))
Shawn O. Pearce1510fea2006-12-14 06:15:57 -05003453 return 0;
3454
Jeff King06dec432016-07-22 11:27:53 -04003455 /*
3456 * Similarly, if we'd have to convert the file contents anyway, that
3457 * makes the optimization not worthwhile.
3458 */
Brandon Williams82b474e2017-06-12 15:13:55 -07003459 if (!want_file && would_convert_to_git(&the_index, name))
Jeff King06dec432016-07-22 11:27:53 -04003460 return 0;
3461
Junio C Hamano6973dca2006-04-21 23:57:45 -07003462 len = strlen(name);
3463 pos = cache_name_pos(name, len);
3464 if (pos < 0)
3465 return 0;
3466 ce = active_cache[pos];
Junio C Hamanoeadb5832008-01-18 23:45:24 -08003467
3468 /*
3469 * This is not the sha1 we are looking for, or
3470 * unreusable because it is not a regular file.
Junio C Hamano6973dca2006-04-21 23:57:45 -07003471 */
Brandon Williamsfb4a1c02017-05-30 10:30:51 -07003472 if (oidcmp(oid, &ce->oid) || !S_ISREG(ce->ce_mode))
Junio C Hamanoeadb5832008-01-18 23:45:24 -08003473 return 0;
3474
3475 /*
Junio C Hamano150115a2009-03-22 15:26:07 -07003476 * If ce is marked as "assume unchanged", there is no
3477 * guarantee that work tree matches what we are looking for.
3478 */
Nguyễn Thái Ngọc Duyb4d16902009-08-20 20:46:58 +07003479 if ((ce->ce_flags & CE_VALID) || ce_skip_worktree(ce))
Junio C Hamano150115a2009-03-22 15:26:07 -07003480 return 0;
3481
3482 /*
Junio C Hamanoeadb5832008-01-18 23:45:24 -08003483 * If ce matches the file in the work tree, we can reuse it.
3484 */
3485 if (ce_uptodate(ce) ||
3486 (!lstat(name, &st) && !ce_match_stat(ce, &st, 0)))
3487 return 1;
3488
3489 return 0;
Junio C Hamano6973dca2006-04-21 23:57:45 -07003490}
3491
Linus Torvalds04786752007-04-15 11:14:28 -07003492static int diff_populate_gitlink(struct diff_filespec *s, int size_only)
3493{
Jeff Kingb1ddfb92016-02-22 17:45:08 -05003494 struct strbuf buf = STRBUF_INIT;
3495 char *dirty = "";
Junio C Hamano8e08b412010-01-16 18:42:53 +01003496
3497 /* Are we looking at the work tree? */
Jens Lehmann85adbf22010-03-12 22:23:52 +01003498 if (s->dirty_submodule)
Junio C Hamano8e08b412010-01-16 18:42:53 +01003499 dirty = "-dirty";
3500
brian m. carlsona0d12c42016-06-24 23:09:23 +00003501 strbuf_addf(&buf, "Subproject commit %s%s\n",
3502 oid_to_hex(&s->oid), dirty);
Jeff Kingb1ddfb92016-02-22 17:45:08 -05003503 s->size = buf.len;
Linus Torvalds04786752007-04-15 11:14:28 -07003504 if (size_only) {
3505 s->data = NULL;
Jeff Kingb1ddfb92016-02-22 17:45:08 -05003506 strbuf_release(&buf);
3507 } else {
3508 s->data = strbuf_detach(&buf, NULL);
3509 s->should_free = 1;
Linus Torvalds04786752007-04-15 11:14:28 -07003510 }
3511 return 0;
3512}
3513
Junio C Hamano6973dca2006-04-21 23:57:45 -07003514/*
3515 * While doing rename detection and pickaxe operation, we may need to
3516 * grab the data for the blob (or file) for our own in-core comparison.
3517 * diff_filespec has data and size fields for this purpose.
3518 */
Nguyễn Thái Ngọc Duy8e5dd3d2014-08-16 10:08:04 +07003519int diff_populate_filespec(struct diff_filespec *s, unsigned int flags)
Junio C Hamano6973dca2006-04-21 23:57:45 -07003520{
Nguyễn Thái Ngọc Duy8e5dd3d2014-08-16 10:08:04 +07003521 int size_only = flags & CHECK_SIZE_ONLY;
Junio C Hamano6973dca2006-04-21 23:57:45 -07003522 int err = 0;
Torsten Bögershausen8462ff42018-01-13 23:49:31 +01003523 int conv_flags = global_conv_flags_eol;
Junio C Hamano5430bb22013-06-24 14:35:04 -07003524 /*
3525 * demote FAIL to WARN to allow inspecting the situation
3526 * instead of refusing.
3527 */
Torsten Bögershausen8462ff42018-01-13 23:49:31 +01003528 if (conv_flags & CONV_EOL_RNDTRP_DIE)
3529 conv_flags = CONV_EOL_RNDTRP_WARN;
Junio C Hamano5430bb22013-06-24 14:35:04 -07003530
Junio C Hamano6973dca2006-04-21 23:57:45 -07003531 if (!DIFF_FILE_VALID(s))
3532 die("internal error: asking to populate invalid file.");
3533 if (S_ISDIR(s->mode))
3534 return -1;
3535
Junio C Hamano6973dca2006-04-21 23:57:45 -07003536 if (s->data)
Junio C Hamanofc3abdf2007-05-03 13:05:48 -07003537 return 0;
Linus Torvalds04786752007-04-15 11:14:28 -07003538
Junio C Hamano6e0b8ed2007-05-07 01:14:21 -07003539 if (size_only && 0 < s->size)
3540 return 0;
3541
Martin Waitz302b9282007-05-21 22:08:28 +02003542 if (S_ISGITLINK(s->mode))
Linus Torvalds04786752007-04-15 11:14:28 -07003543 return diff_populate_gitlink(s, size_only);
3544
brian m. carlson41c95602016-06-24 23:09:24 +00003545 if (!s->oid_valid ||
Brandon Williamsfb4a1c02017-05-30 10:30:51 -07003546 reuse_worktree_file(s->path, &s->oid, 0)) {
Brandon Caseyf285a2d2008-10-09 14:12:12 -05003547 struct strbuf buf = STRBUF_INIT;
Junio C Hamano6973dca2006-04-21 23:57:45 -07003548 struct stat st;
3549 int fd;
Linus Torvalds6c510be2007-02-13 11:07:23 -08003550
Junio C Hamano6973dca2006-04-21 23:57:45 -07003551 if (lstat(s->path, &st) < 0) {
Andrey Okoshkin10e0ca82017-10-27 12:33:25 +03003552 err_empty:
3553 err = -1;
3554 empty:
3555 s->data = (char *)"";
3556 s->size = 0;
3557 return err;
Junio C Hamano6973dca2006-04-21 23:57:45 -07003558 }
Shawn O. Pearcedc49cd72007-03-06 20:44:37 -05003559 s->size = xsize_t(st.st_size);
Junio C Hamano6973dca2006-04-21 23:57:45 -07003560 if (!s->size)
3561 goto empty;
Junio C Hamano6973dca2006-04-21 23:57:45 -07003562 if (S_ISLNK(st.st_mode)) {
Linus Torvaldscf219d82008-12-17 10:26:13 -08003563 struct strbuf sb = STRBUF_INIT;
3564
3565 if (strbuf_readlink(&sb, s->path, s->size))
Junio C Hamano6973dca2006-04-21 23:57:45 -07003566 goto err_empty;
René Scharfe0956a6d2008-12-18 17:56:51 +01003567 s->size = sb.len;
3568 s->data = strbuf_detach(&sb, NULL);
Linus Torvaldscf219d82008-12-17 10:26:13 -08003569 s->should_free = 1;
Junio C Hamano6973dca2006-04-21 23:57:45 -07003570 return 0;
3571 }
Junio C Hamano12426e12017-03-01 18:04:44 +01003572
3573 /*
3574 * Even if the caller would be happy with getting
3575 * only the size, we cannot return early at this
3576 * point if the path requires us to run the content
3577 * conversion.
3578 */
Brandon Williams82b474e2017-06-12 15:13:55 -07003579 if (size_only && !would_convert_to_git(&the_index, s->path))
Linus Torvaldscf219d82008-12-17 10:26:13 -08003580 return 0;
Junio C Hamano12426e12017-03-01 18:04:44 +01003581
3582 /*
3583 * Note: this check uses xsize_t(st.st_size) that may
3584 * not be the true size of the blob after it goes
3585 * through convert_to_git(). This may not strictly be
3586 * correct, but the whole point of big_file_threshold
3587 * and is_binary check being that we want to avoid
3588 * opening the file and inspecting the contents, this
3589 * is probably fine.
3590 */
Nguyễn Thái Ngọc Duy6bf3b812014-08-16 10:08:05 +07003591 if ((flags & CHECK_BINARY) &&
3592 s->size > big_file_threshold && s->is_binary == -1) {
3593 s->is_binary = 1;
3594 return 0;
3595 }
Junio C Hamano6973dca2006-04-21 23:57:45 -07003596 fd = open(s->path, O_RDONLY);
3597 if (fd < 0)
3598 goto err_empty;
Shawn O. Pearcec4712e42006-12-24 00:47:23 -05003599 s->data = xmmap(NULL, s->size, PROT_READ, MAP_PRIVATE, fd, 0);
Junio C Hamano6973dca2006-04-21 23:57:45 -07003600 close(fd);
Junio C Hamano6973dca2006-04-21 23:57:45 -07003601 s->should_munmap = 1;
Linus Torvalds6c510be2007-02-13 11:07:23 -08003602
3603 /*
3604 * Convert from working tree format to canonical git format
3605 */
Torsten Bögershausen8462ff42018-01-13 23:49:31 +01003606 if (convert_to_git(&the_index, s->path, s->data, s->size, &buf, conv_flags)) {
René Scharfec32f7492007-10-21 11:23:49 +02003607 size_t size = 0;
Linus Torvalds6c510be2007-02-13 11:07:23 -08003608 munmap(s->data, s->size);
3609 s->should_munmap = 0;
René Scharfec32f7492007-10-21 11:23:49 +02003610 s->data = strbuf_detach(&buf, &size);
3611 s->size = size;
Linus Torvalds6c510be2007-02-13 11:07:23 -08003612 s->should_free = 1;
3613 }
Junio C Hamano6973dca2006-04-21 23:57:45 -07003614 }
3615 else {
Nicolas Pitre21666f12007-02-26 14:55:59 -05003616 enum object_type type;
Nguyễn Thái Ngọc Duy6bf3b812014-08-16 10:08:05 +07003617 if (size_only || (flags & CHECK_BINARY)) {
brian m. carlsona0d12c42016-06-24 23:09:23 +00003618 type = sha1_object_info(s->oid.hash, &s->size);
Nicolas Pitrec50c4312010-10-22 00:02:27 -04003619 if (type < 0)
brian m. carlsona0d12c42016-06-24 23:09:23 +00003620 die("unable to read %s",
3621 oid_to_hex(&s->oid));
Nguyễn Thái Ngọc Duy6bf3b812014-08-16 10:08:05 +07003622 if (size_only)
3623 return 0;
3624 if (s->size > big_file_threshold && s->is_binary == -1) {
3625 s->is_binary = 1;
3626 return 0;
3627 }
Junio C Hamano6973dca2006-04-21 23:57:45 -07003628 }
brian m. carlsona0d12c42016-06-24 23:09:23 +00003629 s->data = read_sha1_file(s->oid.hash, &type, &s->size);
Nguyễn Thái Ngọc Duy6bf3b812014-08-16 10:08:05 +07003630 if (!s->data)
brian m. carlsona0d12c42016-06-24 23:09:23 +00003631 die("unable to read %s", oid_to_hex(&s->oid));
Nguyễn Thái Ngọc Duy6bf3b812014-08-16 10:08:05 +07003632 s->should_free = 1;
Junio C Hamano6973dca2006-04-21 23:57:45 -07003633 }
3634 return 0;
3635}
3636
Junio C Hamano8ae92e62007-10-02 21:01:03 -07003637void diff_free_filespec_blob(struct diff_filespec *s)
Junio C Hamano6973dca2006-04-21 23:57:45 -07003638{
3639 if (s->should_free)
3640 free(s->data);
3641 else if (s->should_munmap)
3642 munmap(s->data, s->size);
Junio C Hamanofc3abdf2007-05-03 13:05:48 -07003643
3644 if (s->should_free || s->should_munmap) {
3645 s->should_free = s->should_munmap = 0;
3646 s->data = NULL;
3647 }
Jeff Kingeede7b72007-09-25 15:29:42 -04003648}
3649
3650void diff_free_filespec_data(struct diff_filespec *s)
3651{
Junio C Hamano8ae92e62007-10-02 21:01:03 -07003652 diff_free_filespec_blob(s);
Ævar Arnfjörð Bjarmason6a83d902017-06-15 23:15:46 +00003653 FREE_AND_NULL(s->cnt_data);
Junio C Hamano6973dca2006-04-21 23:57:45 -07003654}
3655
Johannes Schindelin4e218f52009-03-21 12:42:52 +01003656static void prep_temp_blob(const char *path, struct diff_tempfile *temp,
Junio C Hamano6973dca2006-04-21 23:57:45 -07003657 void *blob,
3658 unsigned long size,
brian m. carlson09bdff22016-06-24 23:09:29 +00003659 const struct object_id *oid,
Junio C Hamano6973dca2006-04-21 23:57:45 -07003660 int mode)
3661{
Johannes Schindelin4e218f52009-03-21 12:42:52 +01003662 struct strbuf buf = STRBUF_INIT;
David Aguilar003b33a2009-05-31 01:35:52 -07003663 struct strbuf template = STRBUF_INIT;
3664 char *path_dup = xstrdup(path);
3665 const char *base = basename(path_dup);
Junio C Hamano6973dca2006-04-21 23:57:45 -07003666
David Aguilar003b33a2009-05-31 01:35:52 -07003667 /* Generate "XXXXXX_basename.ext" */
3668 strbuf_addstr(&template, "XXXXXX_");
3669 strbuf_addstr(&template, base);
3670
Jeff King076aa2c2017-09-05 08:15:08 -04003671 temp->tempfile = mks_tempfile_ts(template.buf, strlen(base) + 1);
3672 if (!temp->tempfile)
Thomas Rastd824cbb2009-06-27 17:58:46 +02003673 die_errno("unable to create temp-file");
Johannes Schindelin4e218f52009-03-21 12:42:52 +01003674 if (convert_to_working_tree(path,
3675 (const char *)blob, (size_t)size, &buf)) {
3676 blob = buf.buf;
3677 size = buf.len;
3678 }
Junio C Hamanoc50424a2017-09-25 15:24:06 +09003679 if (write_in_full(temp->tempfile->fd, blob, size) < 0 ||
Jeff King076aa2c2017-09-05 08:15:08 -04003680 close_tempfile_gently(temp->tempfile))
Thomas Rast0721c312009-06-27 17:58:47 +02003681 die_errno("unable to write temp-file");
Jeff King076aa2c2017-09-05 08:15:08 -04003682 temp->name = get_tempfile_path(temp->tempfile);
brian m. carlson09bdff22016-06-24 23:09:29 +00003683 oid_to_hex_r(temp->hex, oid);
Jeff King5096d492015-09-24 17:06:08 -04003684 xsnprintf(temp->mode, sizeof(temp->mode), "%06o", mode);
Johannes Schindelin4e218f52009-03-21 12:42:52 +01003685 strbuf_release(&buf);
David Aguilar003b33a2009-05-31 01:35:52 -07003686 strbuf_release(&template);
3687 free(path_dup);
Junio C Hamano6973dca2006-04-21 23:57:45 -07003688}
3689
Jeff King479b0ae2009-01-22 00:59:56 -05003690static struct diff_tempfile *prepare_temp_file(const char *name,
3691 struct diff_filespec *one)
Junio C Hamano6973dca2006-04-21 23:57:45 -07003692{
Jeff King479b0ae2009-01-22 00:59:56 -05003693 struct diff_tempfile *temp = claim_diff_tempfile();
3694
Junio C Hamano6973dca2006-04-21 23:57:45 -07003695 if (!DIFF_FILE_VALID(one)) {
3696 not_a_valid_file:
3697 /* A '-' entry produces this for file-2, and
3698 * a '+' entry produces this for file-1.
3699 */
3700 temp->name = "/dev/null";
Jeff King5096d492015-09-24 17:06:08 -04003701 xsnprintf(temp->hex, sizeof(temp->hex), ".");
3702 xsnprintf(temp->mode, sizeof(temp->mode), ".");
Jeff King479b0ae2009-01-22 00:59:56 -05003703 return temp;
3704 }
3705
Thomas Rastaba47272014-02-16 17:52:34 +01003706 if (!S_ISGITLINK(one->mode) &&
brian m. carlson41c95602016-06-24 23:09:24 +00003707 (!one->oid_valid ||
Brandon Williamsfb4a1c02017-05-30 10:30:51 -07003708 reuse_worktree_file(name, &one->oid, 1))) {
Junio C Hamano6973dca2006-04-21 23:57:45 -07003709 struct stat st;
3710 if (lstat(name, &st) < 0) {
3711 if (errno == ENOENT)
3712 goto not_a_valid_file;
Thomas Rastd824cbb2009-06-27 17:58:46 +02003713 die_errno("stat(%s)", name);
Junio C Hamano6973dca2006-04-21 23:57:45 -07003714 }
3715 if (S_ISLNK(st.st_mode)) {
Jeff King3cd73882009-05-25 06:46:09 -04003716 struct strbuf sb = STRBUF_INIT;
3717 if (strbuf_readlink(&sb, name, st.st_size) < 0)
Thomas Rast0721c312009-06-27 17:58:47 +02003718 die_errno("readlink(%s)", name);
Jeff King3cd73882009-05-25 06:46:09 -04003719 prep_temp_blob(name, temp, sb.buf, sb.len,
brian m. carlson41c95602016-06-24 23:09:24 +00003720 (one->oid_valid ?
brian m. carlson09bdff22016-06-24 23:09:29 +00003721 &one->oid : &null_oid),
brian m. carlson41c95602016-06-24 23:09:24 +00003722 (one->oid_valid ?
Junio C Hamano6973dca2006-04-21 23:57:45 -07003723 one->mode : S_IFLNK));
Jeff King3cd73882009-05-25 06:46:09 -04003724 strbuf_release(&sb);
Junio C Hamano6973dca2006-04-21 23:57:45 -07003725 }
3726 else {
3727 /* we can borrow from the file in the work tree */
3728 temp->name = name;
brian m. carlson41c95602016-06-24 23:09:24 +00003729 if (!one->oid_valid)
Brandon Williams74014152017-05-30 10:30:52 -07003730 oid_to_hex_r(temp->hex, &null_oid);
Junio C Hamano6973dca2006-04-21 23:57:45 -07003731 else
René Scharfe24905742017-01-28 23:03:03 +01003732 oid_to_hex_r(temp->hex, &one->oid);
Junio C Hamano6973dca2006-04-21 23:57:45 -07003733 /* Even though we may sometimes borrow the
3734 * contents from the work tree, we always want
3735 * one->mode. mode is trustworthy even when
Brandon Williams74014152017-05-30 10:30:52 -07003736 * !(one->oid_valid), as long as
Junio C Hamano6973dca2006-04-21 23:57:45 -07003737 * DIFF_FILE_VALID(one).
3738 */
Jeff King5096d492015-09-24 17:06:08 -04003739 xsnprintf(temp->mode, sizeof(temp->mode), "%06o", one->mode);
Junio C Hamano6973dca2006-04-21 23:57:45 -07003740 }
Jeff King479b0ae2009-01-22 00:59:56 -05003741 return temp;
Junio C Hamano6973dca2006-04-21 23:57:45 -07003742 }
3743 else {
3744 if (diff_populate_filespec(one, 0))
3745 die("cannot read data blob for %s", one->path);
Johannes Schindelin4e218f52009-03-21 12:42:52 +01003746 prep_temp_blob(name, temp, one->data, one->size,
brian m. carlson09bdff22016-06-24 23:09:29 +00003747 &one->oid, one->mode);
Junio C Hamano6973dca2006-04-21 23:57:45 -07003748 }
Jeff King479b0ae2009-01-22 00:59:56 -05003749 return temp;
Junio C Hamano6973dca2006-04-21 23:57:45 -07003750}
3751
Jeff Kingf3efe782014-04-19 15:22:25 -04003752static void add_external_diff_name(struct argv_array *argv,
3753 const char *name,
3754 struct diff_filespec *df)
3755{
3756 struct diff_tempfile *temp = prepare_temp_file(name, df);
3757 argv_array_push(argv, temp->name);
3758 argv_array_push(argv, temp->hex);
3759 argv_array_push(argv, temp->mode);
3760}
3761
Junio C Hamano6973dca2006-04-21 23:57:45 -07003762/* An external diff command takes:
3763 *
3764 * diff-cmd name infile1 infile1-sha1 infile1-mode \
3765 * infile2 infile2-sha1 infile2-mode [ rename-to ]
3766 *
3767 */
3768static void run_external_diff(const char *pgm,
3769 const char *name,
3770 const char *other,
3771 struct diff_filespec *one,
3772 struct diff_filespec *two,
3773 const char *xfrm_msg,
Zoltan Klingeree7fb0b2013-12-06 10:38:46 +11003774 int complete_rewrite,
3775 struct diff_options *o)
Junio C Hamano6973dca2006-04-21 23:57:45 -07003776{
Jeff King82fbf262014-04-19 15:17:06 -04003777 struct argv_array argv = ARGV_ARRAY_INIT;
Jeff Kingae049c92014-04-19 15:17:25 -04003778 struct argv_array env = ARGV_ARRAY_INIT;
Zoltan Klingeree7fb0b2013-12-06 10:38:46 +11003779 struct diff_queue_struct *q = &diff_queued_diff;
Junio C Hamano6973dca2006-04-21 23:57:45 -07003780
Jeff King0d4217d2014-04-19 15:20:46 -04003781 argv_array_push(&argv, pgm);
3782 argv_array_push(&argv, name);
Junio C Hamano6973dca2006-04-21 23:57:45 -07003783
Junio C Hamano6973dca2006-04-21 23:57:45 -07003784 if (one && two) {
Jeff Kingf3efe782014-04-19 15:22:25 -04003785 add_external_diff_name(&argv, name, one);
3786 if (!other)
3787 add_external_diff_name(&argv, name, two);
3788 else {
3789 add_external_diff_name(&argv, other, two);
Jeff King82fbf262014-04-19 15:17:06 -04003790 argv_array_push(&argv, other);
3791 argv_array_push(&argv, xfrm_msg);
Junio C Hamano6973dca2006-04-21 23:57:45 -07003792 }
Junio C Hamano6973dca2006-04-21 23:57:45 -07003793 }
Zoltan Klingeree7fb0b2013-12-06 10:38:46 +11003794
Jeff Kingae049c92014-04-19 15:17:25 -04003795 argv_array_pushf(&env, "GIT_DIFF_PATH_COUNTER=%d", ++o->diff_path_counter);
3796 argv_array_pushf(&env, "GIT_DIFF_PATH_TOTAL=%d", q->nr);
Zoltan Klingeree7fb0b2013-12-06 10:38:46 +11003797
Jeff King89294d12014-04-19 15:19:19 -04003798 if (run_command_v_opt_cd_env(argv.argv, RUN_USING_SHELL, NULL, env.argv))
3799 die(_("external diff died, stopping at %s"), name);
3800
Junio C Hamano6973dca2006-04-21 23:57:45 -07003801 remove_tempfile();
Jeff King82fbf262014-04-19 15:17:06 -04003802 argv_array_clear(&argv);
Jeff Kingae049c92014-04-19 15:17:25 -04003803 argv_array_clear(&env);
Junio C Hamano6973dca2006-04-21 23:57:45 -07003804}
3805
Junio C Hamanob67b9612009-01-26 00:08:24 -08003806static int similarity_index(struct diff_filepair *p)
3807{
3808 return p->score * 100 / MAX_SCORE;
3809}
3810
Jeff King4f036662016-10-20 02:21:25 -04003811static const char *diff_abbrev_oid(const struct object_id *oid, int abbrev)
3812{
3813 if (startup_info->have_repository)
3814 return find_unique_abbrev(oid->hash, abbrev);
3815 else {
3816 char *hex = oid_to_hex(oid);
Junio C Hamano0d9c5272016-10-27 14:58:48 -07003817 if (abbrev < 0)
3818 abbrev = FALLBACK_DEFAULT_ABBREV;
3819 if (abbrev > GIT_SHA1_HEXSZ)
Jeff King4f036662016-10-20 02:21:25 -04003820 die("BUG: oid abbreviation out of range: %d", abbrev);
Jack Bates43d19482016-12-06 09:56:14 -07003821 if (abbrev)
3822 hex[abbrev] = '\0';
Jeff King4f036662016-10-20 02:21:25 -04003823 return hex;
3824 }
3825}
3826
Junio C Hamanob67b9612009-01-26 00:08:24 -08003827static void fill_metainfo(struct strbuf *msg,
3828 const char *name,
3829 const char *other,
3830 struct diff_filespec *one,
3831 struct diff_filespec *two,
3832 struct diff_options *o,
Bert Wesarg37466442010-05-04 00:38:07 +02003833 struct diff_filepair *p,
Junio C Hamano59777442010-06-18 11:16:57 -07003834 int *must_show_header,
Bert Wesarg37466442010-05-04 00:38:07 +02003835 int use_color)
Junio C Hamanob67b9612009-01-26 00:08:24 -08003836{
Bert Wesarg37466442010-05-04 00:38:07 +02003837 const char *set = diff_get_color(use_color, DIFF_METAINFO);
3838 const char *reset = diff_get_color(use_color, DIFF_RESET);
John Keeping30997bb2013-02-07 20:15:27 +00003839 const char *line_prefix = diff_line_prefix(o);
Bo Yang7be57612010-05-26 15:23:54 +08003840
Christian Couder296c6bb2010-05-26 04:50:12 +02003841 *must_show_header = 1;
Junio C Hamanob67b9612009-01-26 00:08:24 -08003842 strbuf_init(msg, PATH_MAX * 2 + 300);
3843 switch (p->status) {
3844 case DIFF_STATUS_COPIED:
Junio C Hamano98ad90f2010-06-18 11:16:57 -07003845 strbuf_addf(msg, "%s%ssimilarity index %d%%",
3846 line_prefix, set, similarity_index(p));
3847 strbuf_addf(msg, "%s\n%s%scopy from ",
3848 reset, line_prefix, set);
Junio C Hamanob67b9612009-01-26 00:08:24 -08003849 quote_c_style(name, msg, NULL, 0);
Junio C Hamano98ad90f2010-06-18 11:16:57 -07003850 strbuf_addf(msg, "%s\n%s%scopy to ", reset, line_prefix, set);
Junio C Hamanob67b9612009-01-26 00:08:24 -08003851 quote_c_style(other, msg, NULL, 0);
Bert Wesarg37466442010-05-04 00:38:07 +02003852 strbuf_addf(msg, "%s\n", reset);
Junio C Hamanob67b9612009-01-26 00:08:24 -08003853 break;
3854 case DIFF_STATUS_RENAMED:
Junio C Hamano98ad90f2010-06-18 11:16:57 -07003855 strbuf_addf(msg, "%s%ssimilarity index %d%%",
3856 line_prefix, set, similarity_index(p));
3857 strbuf_addf(msg, "%s\n%s%srename from ",
3858 reset, line_prefix, set);
Junio C Hamanob67b9612009-01-26 00:08:24 -08003859 quote_c_style(name, msg, NULL, 0);
Junio C Hamano98ad90f2010-06-18 11:16:57 -07003860 strbuf_addf(msg, "%s\n%s%srename to ",
3861 reset, line_prefix, set);
Junio C Hamanob67b9612009-01-26 00:08:24 -08003862 quote_c_style(other, msg, NULL, 0);
Bert Wesarg37466442010-05-04 00:38:07 +02003863 strbuf_addf(msg, "%s\n", reset);
Junio C Hamanob67b9612009-01-26 00:08:24 -08003864 break;
3865 case DIFF_STATUS_MODIFIED:
3866 if (p->score) {
Junio C Hamano98ad90f2010-06-18 11:16:57 -07003867 strbuf_addf(msg, "%s%sdissimilarity index %d%%%s\n",
3868 line_prefix,
Bert Wesarg37466442010-05-04 00:38:07 +02003869 set, similarity_index(p), reset);
Junio C Hamanob67b9612009-01-26 00:08:24 -08003870 break;
3871 }
3872 /* fallthru */
3873 default:
Christian Couder296c6bb2010-05-26 04:50:12 +02003874 *must_show_header = 0;
Junio C Hamanob67b9612009-01-26 00:08:24 -08003875 }
brian m. carlsona0d12c42016-06-24 23:09:23 +00003876 if (one && two && oidcmp(&one->oid, &two->oid)) {
Brandon Williams0d1e0e72017-10-31 11:19:11 -07003877 int abbrev = o->flags.full_index ? 40 : DEFAULT_ABBREV;
Junio C Hamanob67b9612009-01-26 00:08:24 -08003878
Brandon Williams0d1e0e72017-10-31 11:19:11 -07003879 if (o->flags.binary) {
Junio C Hamanob67b9612009-01-26 00:08:24 -08003880 mmfile_t mf;
3881 if ((!fill_mmfile(&mf, one) && diff_filespec_is_binary(one)) ||
3882 (!fill_mmfile(&mf, two) && diff_filespec_is_binary(two)))
3883 abbrev = 40;
3884 }
Jeff King4f036662016-10-20 02:21:25 -04003885 strbuf_addf(msg, "%s%sindex %s..%s", line_prefix, set,
3886 diff_abbrev_oid(&one->oid, abbrev),
3887 diff_abbrev_oid(&two->oid, abbrev));
Junio C Hamanob67b9612009-01-26 00:08:24 -08003888 if (one->mode == two->mode)
3889 strbuf_addf(msg, " %06o", one->mode);
Bert Wesarg37466442010-05-04 00:38:07 +02003890 strbuf_addf(msg, "%s\n", reset);
Junio C Hamanob67b9612009-01-26 00:08:24 -08003891 }
Junio C Hamanob67b9612009-01-26 00:08:24 -08003892}
3893
Junio C Hamano6973dca2006-04-21 23:57:45 -07003894static void run_diff_cmd(const char *pgm,
3895 const char *name,
3896 const char *other,
Junio C Hamanocd676a52008-02-12 14:26:02 -08003897 const char *attr_path,
Junio C Hamano6973dca2006-04-21 23:57:45 -07003898 struct diff_filespec *one,
3899 struct diff_filespec *two,
Junio C Hamanob67b9612009-01-26 00:08:24 -08003900 struct strbuf *msg,
Junio C Hamano051308f2006-05-04 16:51:44 -07003901 struct diff_options *o,
Junio C Hamanob67b9612009-01-26 00:08:24 -08003902 struct diff_filepair *p)
Junio C Hamano6973dca2006-04-21 23:57:45 -07003903{
Junio C Hamanob67b9612009-01-26 00:08:24 -08003904 const char *xfrm_msg = NULL;
3905 int complete_rewrite = (p->status == DIFF_STATUS_MODIFIED) && p->score;
Christian Couder296c6bb2010-05-26 04:50:12 +02003906 int must_show_header = 0;
Junio C Hamanob67b9612009-01-26 00:08:24 -08003907
Junio C Hamanobd8c1a92012-07-17 22:08:59 -07003908
Brandon Williams0d1e0e72017-10-31 11:19:11 -07003909 if (o->flags.allow_external) {
Jeff Kingbe58e702008-10-05 17:43:21 -04003910 struct userdiff_driver *drv = userdiff_find_by_path(attr_path);
3911 if (drv && drv->external)
3912 pgm = drv->external;
Junio C Hamanof1af60b2007-04-22 17:52:55 -07003913 }
3914
Bert Wesarg37466442010-05-04 00:38:07 +02003915 if (msg) {
3916 /*
3917 * don't use colors when the header is intended for an
3918 * external diff driver
3919 */
3920 fill_metainfo(msg, name, other, one, two, o, p,
Junio C Hamano59777442010-06-18 11:16:57 -07003921 &must_show_header,
Jeff Kingdaa0c3d2011-08-17 22:04:23 -07003922 want_color(o->use_color) && !pgm);
Bert Wesarg37466442010-05-04 00:38:07 +02003923 xfrm_msg = msg->len ? msg->buf : NULL;
3924 }
3925
Junio C Hamano6973dca2006-04-21 23:57:45 -07003926 if (pgm) {
3927 run_external_diff(pgm, name, other, one, two, xfrm_msg,
Zoltan Klingeree7fb0b2013-12-06 10:38:46 +11003928 complete_rewrite, o);
Junio C Hamano6973dca2006-04-21 23:57:45 -07003929 return;
3930 }
3931 if (one && two)
3932 builtin_diff(name, other ? other : name,
Christian Couder296c6bb2010-05-26 04:50:12 +02003933 one, two, xfrm_msg, must_show_header,
3934 o, complete_rewrite);
Junio C Hamano6973dca2006-04-21 23:57:45 -07003935 else
Daniel Barkalowc0c77732008-03-09 22:43:39 -04003936 fprintf(o->file, "* Unmerged path %s\n", name);
Junio C Hamano6973dca2006-04-21 23:57:45 -07003937}
3938
Brandon Williams94e327e2017-05-30 10:31:09 -07003939static void diff_fill_oid_info(struct diff_filespec *one)
Junio C Hamano6973dca2006-04-21 23:57:45 -07003940{
3941 if (DIFF_FILE_VALID(one)) {
brian m. carlson41c95602016-06-24 23:09:24 +00003942 if (!one->oid_valid) {
Junio C Hamano6973dca2006-04-21 23:57:45 -07003943 struct stat st;
Junio C Hamano4682d852012-06-27 20:14:47 -07003944 if (one->is_stdin) {
brian m. carlsona0d12c42016-06-24 23:09:23 +00003945 oidclr(&one->oid);
Johannes Schindelin5332b2a2007-02-25 23:36:10 +01003946 return;
3947 }
Junio C Hamano6973dca2006-04-21 23:57:45 -07003948 if (lstat(one->path, &st) < 0)
Thomas Rast0721c312009-06-27 17:58:47 +02003949 die_errno("stat '%s'", one->path);
Patryk Obara98e019b2017-08-20 22:09:28 +02003950 if (index_path(&one->oid, one->path, &st, 0))
Alexander Potashevd7530702009-01-04 21:38:41 +03003951 die("cannot hash %s", one->path);
Junio C Hamano6973dca2006-04-21 23:57:45 -07003952 }
3953 }
3954 else
brian m. carlsona0d12c42016-06-24 23:09:23 +00003955 oidclr(&one->oid);
Junio C Hamano6973dca2006-04-21 23:57:45 -07003956}
3957
Junio C Hamanocd676a52008-02-12 14:26:02 -08003958static void strip_prefix(int prefix_length, const char **namep, const char **otherp)
3959{
3960 /* Strip the prefix but do not molest /dev/null and absolute paths */
Jakub Narebskid8faea92010-08-09 10:50:53 -04003961 if (*namep && **namep != '/') {
Junio C Hamanocd676a52008-02-12 14:26:02 -08003962 *namep += prefix_length;
Jakub Narebskid8faea92010-08-09 10:50:53 -04003963 if (**namep == '/')
3964 ++*namep;
3965 }
3966 if (*otherp && **otherp != '/') {
Junio C Hamanocd676a52008-02-12 14:26:02 -08003967 *otherp += prefix_length;
Jakub Narebskid8faea92010-08-09 10:50:53 -04003968 if (**otherp == '/')
3969 ++*otherp;
3970 }
Junio C Hamanocd676a52008-02-12 14:26:02 -08003971}
3972
Junio C Hamano6973dca2006-04-21 23:57:45 -07003973static void run_diff(struct diff_filepair *p, struct diff_options *o)
3974{
3975 const char *pgm = external_diff();
Pierre Habouzit663af342007-09-20 00:42:15 +02003976 struct strbuf msg;
Pierre Habouzit663af342007-09-20 00:42:15 +02003977 struct diff_filespec *one = p->one;
3978 struct diff_filespec *two = p->two;
Junio C Hamano6973dca2006-04-21 23:57:45 -07003979 const char *name;
3980 const char *other;
Junio C Hamanocd676a52008-02-12 14:26:02 -08003981 const char *attr_path;
Pierre Habouzit663af342007-09-20 00:42:15 +02003982
Stefan Bellerf2d2a5d2017-06-29 17:06:46 -07003983 name = one->path;
3984 other = (strcmp(name, two->path) ? two->path : NULL);
Junio C Hamanocd676a52008-02-12 14:26:02 -08003985 attr_path = name;
3986 if (o->prefix_length)
3987 strip_prefix(o->prefix_length, &name, &other);
3988
Brandon Williams0d1e0e72017-10-31 11:19:11 -07003989 if (!o->flags.allow_external)
Junio C Hamanobd8c1a92012-07-17 22:08:59 -07003990 pgm = NULL;
3991
Junio C Hamanocd676a52008-02-12 14:26:02 -08003992 if (DIFF_PAIR_UNMERGED(p)) {
3993 run_diff_cmd(pgm, name, NULL, attr_path,
Junio C Hamanob67b9612009-01-26 00:08:24 -08003994 NULL, NULL, NULL, o, p);
Junio C Hamanocd676a52008-02-12 14:26:02 -08003995 return;
3996 }
3997
Brandon Williams94e327e2017-05-30 10:31:09 -07003998 diff_fill_oid_info(one);
3999 diff_fill_oid_info(two);
Junio C Hamano6973dca2006-04-21 23:57:45 -07004000
Junio C Hamano6973dca2006-04-21 23:57:45 -07004001 if (!pgm &&
4002 DIFF_FILE_VALID(one) && DIFF_FILE_VALID(two) &&
4003 (S_IFMT & one->mode) != (S_IFMT & two->mode)) {
Junio C Hamanob67b9612009-01-26 00:08:24 -08004004 /*
4005 * a filepair that changes between file and symlink
Junio C Hamano6973dca2006-04-21 23:57:45 -07004006 * needs to be split into deletion and creation.
4007 */
4008 struct diff_filespec *null = alloc_filespec(two->path);
Junio C Hamanocd676a52008-02-12 14:26:02 -08004009 run_diff_cmd(NULL, name, other, attr_path,
Junio C Hamanob67b9612009-01-26 00:08:24 -08004010 one, null, &msg, o, p);
Junio C Hamano6973dca2006-04-21 23:57:45 -07004011 free(null);
Junio C Hamanob67b9612009-01-26 00:08:24 -08004012 strbuf_release(&msg);
4013
Junio C Hamano6973dca2006-04-21 23:57:45 -07004014 null = alloc_filespec(one->path);
Junio C Hamanocd676a52008-02-12 14:26:02 -08004015 run_diff_cmd(NULL, name, other, attr_path,
Junio C Hamanob67b9612009-01-26 00:08:24 -08004016 null, two, &msg, o, p);
Junio C Hamano6973dca2006-04-21 23:57:45 -07004017 free(null);
4018 }
4019 else
Junio C Hamanocd676a52008-02-12 14:26:02 -08004020 run_diff_cmd(pgm, name, other, attr_path,
Junio C Hamanob67b9612009-01-26 00:08:24 -08004021 one, two, &msg, o, p);
Junio C Hamano6973dca2006-04-21 23:57:45 -07004022
Pierre Habouzit663af342007-09-20 00:42:15 +02004023 strbuf_release(&msg);
Junio C Hamano6973dca2006-04-21 23:57:45 -07004024}
4025
4026static void run_diffstat(struct diff_filepair *p, struct diff_options *o,
4027 struct diffstat_t *diffstat)
4028{
4029 const char *name;
4030 const char *other;
4031
4032 if (DIFF_PAIR_UNMERGED(p)) {
4033 /* unmerged */
Linus Torvalds74faaa12012-10-17 10:00:37 -07004034 builtin_diffstat(p->one->path, NULL, NULL, NULL, diffstat, o, p);
Junio C Hamano6973dca2006-04-21 23:57:45 -07004035 return;
4036 }
4037
4038 name = p->one->path;
4039 other = (strcmp(name, p->two->path) ? p->two->path : NULL);
4040
Junio C Hamanocd676a52008-02-12 14:26:02 -08004041 if (o->prefix_length)
4042 strip_prefix(o->prefix_length, &name, &other);
4043
Brandon Williams94e327e2017-05-30 10:31:09 -07004044 diff_fill_oid_info(p->one);
4045 diff_fill_oid_info(p->two);
Junio C Hamano6973dca2006-04-21 23:57:45 -07004046
Linus Torvalds74faaa12012-10-17 10:00:37 -07004047 builtin_diffstat(name, other, p->one, p->two, diffstat, o, p);
Junio C Hamano6973dca2006-04-21 23:57:45 -07004048}
4049
Johannes Schindelin88246892006-05-20 23:43:13 +02004050static void run_checkdiff(struct diff_filepair *p, struct diff_options *o)
4051{
4052 const char *name;
4053 const char *other;
Junio C Hamanocd676a52008-02-12 14:26:02 -08004054 const char *attr_path;
Johannes Schindelin88246892006-05-20 23:43:13 +02004055
4056 if (DIFF_PAIR_UNMERGED(p)) {
4057 /* unmerged */
4058 return;
4059 }
4060
4061 name = p->one->path;
4062 other = (strcmp(name, p->two->path) ? p->two->path : NULL);
Junio C Hamanocd676a52008-02-12 14:26:02 -08004063 attr_path = other ? other : name;
4064
4065 if (o->prefix_length)
4066 strip_prefix(o->prefix_length, &name, &other);
Johannes Schindelin88246892006-05-20 23:43:13 +02004067
Brandon Williams94e327e2017-05-30 10:31:09 -07004068 diff_fill_oid_info(p->one);
4069 diff_fill_oid_info(p->two);
Johannes Schindelin88246892006-05-20 23:43:13 +02004070
Junio C Hamanocd676a52008-02-12 14:26:02 -08004071 builtin_checkdiff(name, other, attr_path, p->one, p->two, o);
Johannes Schindelin88246892006-05-20 23:43:13 +02004072}
4073
Junio C Hamano6973dca2006-04-21 23:57:45 -07004074void diff_setup(struct diff_options *options)
4075{
Johannes Schindelinbe4f2b42010-08-05 10:49:55 +02004076 memcpy(options, &default_diff_options, sizeof(*options));
Daniel Barkalowc0c77732008-03-09 22:43:39 -04004077
4078 options->file = stdout;
4079
Jack Bates43d19482016-12-06 09:56:14 -07004080 options->abbrev = DEFAULT_ABBREV;
Junio C Hamano6973dca2006-04-21 23:57:45 -07004081 options->line_termination = '\n';
4082 options->break_opt = -1;
4083 options->rename_limit = -1;
Johan Herland712d2c72011-04-29 11:36:20 +02004084 options->dirstat_permille = diff_dirstat_permille_default;
Jeff Muizelaar6468a4e2012-09-27 15:12:52 -04004085 options->context = diff_context_default;
Vegard Nossumc4888672017-01-12 13:21:11 +01004086 options->interhunkcontext = diff_interhunk_context_default;
Junio C Hamanoa17505f2016-10-04 15:26:27 -07004087 options->ws_error_highlight = ws_error_highlight_default;
Brandon Williams0d1e0e72017-10-31 11:19:11 -07004088 options->flags.rename_empty = 1;
Junio C Hamano6973dca2006-04-21 23:57:45 -07004089
Kirill Smelkov72441af2014-04-07 01:46:26 +04004090 /* pathchange left =NULL by default */
Junio C Hamano6973dca2006-04-21 23:57:45 -07004091 options->change = diff_change;
4092 options->add_remove = diff_addremove;
Jeff Kingf1c96262011-08-17 22:03:12 -07004093 options->use_color = diff_use_color_default;
Eric Wongb68ea122006-07-07 04:01:23 -07004094 options->detect_rename = diff_detect_rename_default;
Michal Privoznik07ab4de2013-01-16 08:51:57 +01004095 options->xdl_opts |= diff_algorithm;
Michael Haggerty433860f2016-09-05 11:44:51 +02004096 if (diff_indent_heuristic)
4097 DIFF_XDL_SET(options, INDENT_HEURISTIC);
Johannes Schindelineab9a402007-12-18 19:32:14 +00004098
Samuel Bronson6d8940b2013-12-18 19:08:12 -05004099 options->orderfile = diff_order_file_cfg;
4100
Eli Collinsf89504d2010-05-02 19:03:41 -07004101 if (diff_no_prefix) {
4102 options->a_prefix = options->b_prefix = "";
4103 } else if (!diff_mnemonic_prefix) {
Junio C Hamanoa5a818e2008-08-18 20:08:09 -07004104 options->a_prefix = "a/";
4105 options->b_prefix = "b/";
4106 }
Stefan Beller2e2d5ac2017-06-30 13:53:07 -07004107
4108 options->color_moved = diff_color_moved_default;
Junio C Hamano6973dca2006-04-21 23:57:45 -07004109}
4110
Thomas Rast28452652012-08-03 14:16:24 +02004111void diff_setup_done(struct diff_options *options)
Junio C Hamano6973dca2006-04-21 23:57:45 -07004112{
Timo Hirvonend7de00f2006-06-24 20:26:49 +03004113 int count = 0;
4114
Junio C Hamano6c374002013-05-10 17:10:11 +02004115 if (options->set_default)
4116 options->set_default(options);
4117
Timo Hirvonend7de00f2006-06-24 20:26:49 +03004118 if (options->output_format & DIFF_FORMAT_NAME)
4119 count++;
4120 if (options->output_format & DIFF_FORMAT_NAME_STATUS)
4121 count++;
4122 if (options->output_format & DIFF_FORMAT_CHECKDIFF)
4123 count++;
4124 if (options->output_format & DIFF_FORMAT_NO_OUTPUT)
4125 count++;
4126 if (count > 1)
Jean-Noël AVILAa2f05c92016-09-20 21:04:27 +02004127 die(_("--name-only, --name-status, --check and -s are mutually exclusive"));
Timo Hirvonend7de00f2006-06-24 20:26:49 +03004128
Junio C Hamanof2451942009-05-22 12:45:29 -07004129 /*
4130 * Most of the time we can say "there are changes"
4131 * only by checking if there are changed paths, but
4132 * --ignore-whitespace* options force us to look
Jim Meyering97bf2a02009-08-30 22:27:02 +02004133 * inside contents.
Junio C Hamanof2451942009-05-22 12:45:29 -07004134 */
4135
Junio C Hamano446d12c2017-10-26 15:15:51 +09004136 if ((options->xdl_opts & XDF_WHITESPACE_FLAGS))
Brandon Williams0d1e0e72017-10-31 11:19:11 -07004137 options->flags.diff_from_contents = 1;
Junio C Hamanof2451942009-05-22 12:45:29 -07004138 else
Brandon Williams0d1e0e72017-10-31 11:19:11 -07004139 options->flags.diff_from_contents = 0;
Junio C Hamanof2451942009-05-22 12:45:29 -07004140
Brandon Williams0d1e0e72017-10-31 11:19:11 -07004141 if (options->flags.find_copies_harder)
Junio C Hamano03b9d562006-08-09 13:17:19 -07004142 options->detect_rename = DIFF_DETECT_COPY;
4143
Brandon Williams0d1e0e72017-10-31 11:19:11 -07004144 if (!options->flags.relative_name)
Junio C Hamanocd676a52008-02-12 14:26:02 -08004145 options->prefix = NULL;
4146 if (options->prefix)
4147 options->prefix_length = strlen(options->prefix);
4148 else
4149 options->prefix_length = 0;
4150
Timo Hirvonenc6744342006-06-24 20:21:53 +03004151 if (options->output_format & (DIFF_FORMAT_NAME |
4152 DIFF_FORMAT_NAME_STATUS |
4153 DIFF_FORMAT_CHECKDIFF |
4154 DIFF_FORMAT_NO_OUTPUT))
4155 options->output_format &= ~(DIFF_FORMAT_RAW |
Junio C Hamano74e2abe2006-10-12 03:01:00 -07004156 DIFF_FORMAT_NUMSTAT |
Timo Hirvonenc6744342006-06-24 20:21:53 +03004157 DIFF_FORMAT_DIFFSTAT |
Nicolas Pitreebd124c2006-12-14 23:15:44 -05004158 DIFF_FORMAT_SHORTSTAT |
Linus Torvalds7df7c012008-02-12 13:26:31 -08004159 DIFF_FORMAT_DIRSTAT |
Timo Hirvonenc6744342006-06-24 20:21:53 +03004160 DIFF_FORMAT_SUMMARY |
4161 DIFF_FORMAT_PATCH);
4162
Junio C Hamano6973dca2006-04-21 23:57:45 -07004163 /*
4164 * These cases always need recursive; we do not drop caller-supplied
4165 * recursive bits for other formats here.
4166 */
Timo Hirvonenc6744342006-06-24 20:21:53 +03004167 if (options->output_format & (DIFF_FORMAT_PATCH |
Junio C Hamano74e2abe2006-10-12 03:01:00 -07004168 DIFF_FORMAT_NUMSTAT |
Timo Hirvonenc6744342006-06-24 20:21:53 +03004169 DIFF_FORMAT_DIFFSTAT |
Nicolas Pitreebd124c2006-12-14 23:15:44 -05004170 DIFF_FORMAT_SHORTSTAT |
Linus Torvalds7df7c012008-02-12 13:26:31 -08004171 DIFF_FORMAT_DIRSTAT |
Johannes Schindelind7014dc2006-10-03 23:09:56 +02004172 DIFF_FORMAT_SUMMARY |
Timo Hirvonenc6744342006-06-24 20:21:53 +03004173 DIFF_FORMAT_CHECKDIFF))
Brandon Williams0d1e0e72017-10-31 11:19:11 -07004174 options->flags.recursive = 1;
Junio C Hamano5e363542006-05-22 00:31:02 -07004175 /*
Junio C Hamano3969cf72006-06-27 15:08:19 -07004176 * Also pickaxe would not work very well if you do not say recursive
Junio C Hamano5e363542006-05-22 00:31:02 -07004177 */
Junio C Hamano3969cf72006-06-27 15:08:19 -07004178 if (options->pickaxe)
Brandon Williams0d1e0e72017-10-31 11:19:11 -07004179 options->flags.recursive = 1;
Jens Lehmannae6d5c12010-03-11 22:50:25 +01004180 /*
4181 * When patches are generated, submodules diffed against the work tree
4182 * must be checked for dirtiness too so it can be shown in the output
4183 */
4184 if (options->output_format & DIFF_FORMAT_PATCH)
Brandon Williams0d1e0e72017-10-31 11:19:11 -07004185 options->flags.dirty_submodules = 1;
Junio C Hamano5e363542006-05-22 00:31:02 -07004186
Junio C Hamano6973dca2006-04-21 23:57:45 -07004187 if (options->detect_rename && options->rename_limit < 0)
4188 options->rename_limit = diff_rename_limit_default;
4189 if (options->setup & DIFF_SETUP_USE_CACHE) {
4190 if (!active_cache)
4191 /* read-cache does not die even when it fails
4192 * so it is safe for us to do this here. Also
4193 * it does not smudge active_cache or active_nr
4194 * when it fails, so we do not have to worry about
4195 * cleaning it up ourselves either.
4196 */
4197 read_cache();
4198 }
Junio C Hamano7b5b7722016-09-30 17:19:36 -07004199 if (40 < options->abbrev)
Junio C Hamano6973dca2006-04-21 23:57:45 -07004200 options->abbrev = 40; /* full */
4201
Junio C Hamano68aacb22007-03-14 11:12:13 -07004202 /*
4203 * It does not make sense to show the first hit we happened
4204 * to have found. It does not make sense not to return with
4205 * exit code in such a case either.
4206 */
Brandon Williams0d1e0e72017-10-31 11:19:11 -07004207 if (options->flags.quick) {
Junio C Hamano68aacb22007-03-14 11:12:13 -07004208 options->output_format = DIFF_FORMAT_NO_OUTPUT;
Brandon Williams0d1e0e72017-10-31 11:19:11 -07004209 options->flags.exit_with_status = 1;
Junio C Hamano68aacb22007-03-14 11:12:13 -07004210 }
Zoltan Klingeree7fb0b2013-12-06 10:38:46 +11004211
Jeff King0ea7d5b2013-12-16 15:02:21 -05004212 options->diff_path_counter = 0;
Junio C Hamanob0e2c992014-06-16 10:07:09 -07004213
Brandon Williams0d1e0e72017-10-31 11:19:11 -07004214 if (options->flags.follow_renames && options->pathspec.nr != 1)
Jeff Kingdd63f162014-05-20 02:49:20 -04004215 die(_("--follow requires exactly one pathspec"));
Stefan Beller2e2d5ac2017-06-30 13:53:07 -07004216
4217 if (!options->use_color || external_diff())
4218 options->color_moved = 0;
Junio C Hamano6973dca2006-04-21 23:57:45 -07004219}
4220
Timo Hirvonend2543b82006-06-24 20:20:32 +03004221static int opt_arg(const char *arg, int arg_short, const char *arg_long, int *val)
Linus Torvaldsee1e5412006-05-13 13:23:48 -07004222{
4223 char c, *eq;
4224 int len;
4225
4226 if (*arg != '-')
4227 return 0;
4228 c = *++arg;
4229 if (!c)
4230 return 0;
4231 if (c == arg_short) {
4232 c = *++arg;
4233 if (!c)
4234 return 1;
4235 if (val && isdigit(c)) {
4236 char *end;
4237 int n = strtoul(arg, &end, 10);
4238 if (*end)
4239 return 0;
4240 *val = n;
4241 return 1;
4242 }
4243 return 0;
4244 }
4245 if (c != '-')
4246 return 0;
4247 arg++;
Rohit Mani2c5495f2014-03-07 22:48:31 -08004248 eq = strchrnul(arg, '=');
4249 len = eq - arg;
Linus Torvaldsee1e5412006-05-13 13:23:48 -07004250 if (!len || strncmp(arg, arg_long, len))
4251 return 0;
Rohit Mani2c5495f2014-03-07 22:48:31 -08004252 if (*eq) {
Linus Torvaldsee1e5412006-05-13 13:23:48 -07004253 int n;
4254 char *end;
4255 if (!isdigit(*++eq))
4256 return 0;
4257 n = strtoul(eq, &end, 10);
4258 if (*end)
4259 return 0;
4260 *val = n;
4261 }
4262 return 1;
4263}
4264
Junio C Hamano16befb82007-06-08 02:54:57 -07004265static int diff_scoreopt_parse(const char *opt);
4266
Matthieu Moydea007f2010-08-05 10:22:52 +02004267static inline int short_opt(char opt, const char **argv,
4268 const char **optarg)
4269{
4270 const char *arg = argv[0];
4271 if (arg[0] != '-' || arg[1] != opt)
4272 return 0;
4273 if (arg[2] != '\0') {
4274 *optarg = arg + 2;
4275 return 1;
4276 }
4277 if (!argv[1])
4278 die("Option '%c' requires a value", opt);
4279 *optarg = argv[1];
4280 return 2;
4281}
4282
4283int parse_long_opt(const char *opt, const char **argv,
4284 const char **optarg)
4285{
4286 const char *arg = argv[0];
Jeff King95b567c2014-06-18 15:48:29 -04004287 if (!skip_prefix(arg, "--", &arg))
Matthieu Moydea007f2010-08-05 10:22:52 +02004288 return 0;
Jeff King95b567c2014-06-18 15:48:29 -04004289 if (!skip_prefix(arg, opt, &arg))
Matthieu Moydea007f2010-08-05 10:22:52 +02004290 return 0;
Nicolas Vigierb0d12fc2013-10-31 12:08:28 +01004291 if (*arg == '=') { /* stuck form: --option=value */
Matthieu Moydea007f2010-08-05 10:22:52 +02004292 *optarg = arg + 1;
4293 return 1;
4294 }
4295 if (*arg != '\0')
4296 return 0;
4297 /* separate form: --option value */
4298 if (!argv[1])
4299 die("Option '--%s' requires a value", opt);
4300 *optarg = argv[1];
4301 return 2;
4302}
4303
Jonathan Nieder4d7f7a42010-08-05 10:22:53 +02004304static int stat_opt(struct diff_options *options, const char **av)
4305{
4306 const char *arg = av[0];
4307 char *end;
4308 int width = options->stat_width;
4309 int name_width = options->stat_name_width;
Zbigniew Jędrzejewski-Szmek969fe572012-03-01 13:26:45 +01004310 int graph_width = options->stat_graph_width;
Michael J Gruber808e1db2011-05-27 14:36:41 +02004311 int count = options->stat_count;
Matthieu Moy1e572082010-08-05 10:22:54 +02004312 int argcount = 1;
Jonathan Nieder4d7f7a42010-08-05 10:22:53 +02004313
Jeff King0539cc02014-06-18 15:51:17 -04004314 if (!skip_prefix(arg, "--stat", &arg))
4315 die("BUG: stat option does not begin with --stat: %s", arg);
Jonathan Nieder4d7f7a42010-08-05 10:22:53 +02004316 end = (char *)arg;
4317
4318 switch (*arg) {
4319 case '-':
Jeff King95b567c2014-06-18 15:48:29 -04004320 if (skip_prefix(arg, "-width", &arg)) {
Matthieu Moy1e572082010-08-05 10:22:54 +02004321 if (*arg == '=')
4322 width = strtoul(arg + 1, &end, 10);
4323 else if (!*arg && !av[1])
Jean-Noël AVILAa2f05c92016-09-20 21:04:27 +02004324 die_want_option("--stat-width");
Matthieu Moy1e572082010-08-05 10:22:54 +02004325 else if (!*arg) {
4326 width = strtoul(av[1], &end, 10);
4327 argcount = 2;
4328 }
Jeff King95b567c2014-06-18 15:48:29 -04004329 } else if (skip_prefix(arg, "-name-width", &arg)) {
Matthieu Moy1e572082010-08-05 10:22:54 +02004330 if (*arg == '=')
4331 name_width = strtoul(arg + 1, &end, 10);
4332 else if (!*arg && !av[1])
Jean-Noël AVILAa2f05c92016-09-20 21:04:27 +02004333 die_want_option("--stat-name-width");
Matthieu Moy1e572082010-08-05 10:22:54 +02004334 else if (!*arg) {
4335 name_width = strtoul(av[1], &end, 10);
4336 argcount = 2;
4337 }
Jeff King95b567c2014-06-18 15:48:29 -04004338 } else if (skip_prefix(arg, "-graph-width", &arg)) {
Zbigniew Jędrzejewski-Szmek969fe572012-03-01 13:26:45 +01004339 if (*arg == '=')
4340 graph_width = strtoul(arg + 1, &end, 10);
4341 else if (!*arg && !av[1])
Jean-Noël AVILAa2f05c92016-09-20 21:04:27 +02004342 die_want_option("--stat-graph-width");
Zbigniew Jędrzejewski-Szmek969fe572012-03-01 13:26:45 +01004343 else if (!*arg) {
4344 graph_width = strtoul(av[1], &end, 10);
4345 argcount = 2;
4346 }
Jeff King95b567c2014-06-18 15:48:29 -04004347 } else if (skip_prefix(arg, "-count", &arg)) {
Michael J Gruber808e1db2011-05-27 14:36:41 +02004348 if (*arg == '=')
4349 count = strtoul(arg + 1, &end, 10);
4350 else if (!*arg && !av[1])
Jean-Noël AVILAa2f05c92016-09-20 21:04:27 +02004351 die_want_option("--stat-count");
Michael J Gruber808e1db2011-05-27 14:36:41 +02004352 else if (!*arg) {
4353 count = strtoul(av[1], &end, 10);
4354 argcount = 2;
4355 }
Matthieu Moy1e572082010-08-05 10:22:54 +02004356 }
Jonathan Nieder4d7f7a42010-08-05 10:22:53 +02004357 break;
4358 case '=':
4359 width = strtoul(arg+1, &end, 10);
4360 if (*end == ',')
4361 name_width = strtoul(end+1, &end, 10);
Michael J Gruber808e1db2011-05-27 14:36:41 +02004362 if (*end == ',')
4363 count = strtoul(end+1, &end, 10);
Jonathan Nieder4d7f7a42010-08-05 10:22:53 +02004364 }
4365
4366 /* Important! This checks all the error cases! */
4367 if (*end)
4368 return 0;
4369 options->output_format |= DIFF_FORMAT_DIFFSTAT;
4370 options->stat_name_width = name_width;
Zbigniew Jędrzejewski-Szmek969fe572012-03-01 13:26:45 +01004371 options->stat_graph_width = graph_width;
Jonathan Nieder4d7f7a42010-08-05 10:22:53 +02004372 options->stat_width = width;
Michael J Gruber808e1db2011-05-27 14:36:41 +02004373 options->stat_count = count;
Matthieu Moy1e572082010-08-05 10:22:54 +02004374 return argcount;
Jonathan Nieder4d7f7a42010-08-05 10:22:53 +02004375}
4376
Johan Herland333f3fb2011-04-29 11:36:18 +02004377static int parse_dirstat_opt(struct diff_options *options, const char *params)
4378{
Johan Herland51670fc2011-04-29 11:36:22 +02004379 struct strbuf errmsg = STRBUF_INIT;
4380 if (parse_dirstat_params(options, params, &errmsg))
Johan Herland7478ac52011-04-29 11:36:23 +02004381 die(_("Failed to parse --dirstat/-X option parameter:\n%s"),
Johan Herland51670fc2011-04-29 11:36:22 +02004382 errmsg.buf);
4383 strbuf_release(&errmsg);
Johan Herland333f3fb2011-04-29 11:36:18 +02004384 /*
4385 * The caller knows a dirstat-related option is given from the command
4386 * line; allow it to say "return this_function();"
4387 */
4388 options->output_format |= DIFF_FORMAT_DIRSTAT;
4389 return 1;
4390}
4391
Ramkumar Ramachandrac47ef572012-11-13 21:12:45 +05304392static int parse_submodule_opt(struct diff_options *options, const char *value)
4393{
4394 if (parse_submodule_params(options, value))
4395 die(_("Failed to parse --submodule option parameter: '%s'"),
4396 value);
4397 return 1;
4398}
4399
Junio C Hamano1ecc1cb2013-07-17 15:05:46 -07004400static const char diff_status_letters[] = {
4401 DIFF_STATUS_ADDED,
4402 DIFF_STATUS_COPIED,
4403 DIFF_STATUS_DELETED,
4404 DIFF_STATUS_MODIFIED,
4405 DIFF_STATUS_RENAMED,
4406 DIFF_STATUS_TYPE_CHANGED,
4407 DIFF_STATUS_UNKNOWN,
4408 DIFF_STATUS_UNMERGED,
4409 DIFF_STATUS_FILTER_AON,
4410 DIFF_STATUS_FILTER_BROKEN,
4411 '\0',
4412};
4413
4414static unsigned int filter_bit['Z' + 1];
4415
4416static void prepare_filter_bits(void)
4417{
4418 int i;
4419
4420 if (!filter_bit[DIFF_STATUS_ADDED]) {
4421 for (i = 0; diff_status_letters[i]; i++)
4422 filter_bit[(int) diff_status_letters[i]] = (1 << i);
4423 }
4424}
4425
4426static unsigned filter_bit_tst(char status, const struct diff_options *opt)
4427{
4428 return opt->filter & filter_bit[(int) status];
4429}
4430
4431static int parse_diff_filter_opt(const char *optarg, struct diff_options *opt)
4432{
4433 int i, optch;
4434
4435 prepare_filter_bits();
Junio C Hamano7f2ea5f2013-07-17 16:19:19 -07004436
4437 /*
4438 * If there is a negation e.g. 'd' in the input, and we haven't
4439 * initialized the filter field with another --diff-filter, start
4440 * from full set of bits, except for AON.
4441 */
4442 if (!opt->filter) {
4443 for (i = 0; (optch = optarg[i]) != '\0'; i++) {
4444 if (optch < 'a' || 'z' < optch)
4445 continue;
4446 opt->filter = (1 << (ARRAY_SIZE(diff_status_letters) - 1)) - 1;
4447 opt->filter &= ~filter_bit[DIFF_STATUS_FILTER_AON];
4448 break;
4449 }
4450 }
4451
Junio C Hamano1ecc1cb2013-07-17 15:05:46 -07004452 for (i = 0; (optch = optarg[i]) != '\0'; i++) {
4453 unsigned int bit;
Junio C Hamano7f2ea5f2013-07-17 16:19:19 -07004454 int negate;
4455
4456 if ('a' <= optch && optch <= 'z') {
4457 negate = 1;
4458 optch = toupper(optch);
4459 } else {
4460 negate = 0;
4461 }
Junio C Hamano1ecc1cb2013-07-17 15:05:46 -07004462
4463 bit = (0 <= optch && optch <= 'Z') ? filter_bit[optch] : 0;
4464 if (!bit)
Junio C Hamanobf142ec2013-07-17 15:27:19 -07004465 return optarg[i];
Junio C Hamano7f2ea5f2013-07-17 16:19:19 -07004466 if (negate)
4467 opt->filter &= ~bit;
4468 else
4469 opt->filter |= bit;
Junio C Hamano1ecc1cb2013-07-17 15:05:46 -07004470 }
4471 return 0;
4472}
4473
Matthieu Moy71482d32013-07-16 10:05:37 +02004474static void enable_patch_output(int *fmt) {
4475 *fmt &= ~DIFF_FORMAT_NO_OUTPUT;
4476 *fmt |= DIFF_FORMAT_PATCH;
4477}
4478
Junio C Hamano077965f2016-10-04 15:03:33 -07004479static int parse_ws_error_highlight_opt(struct diff_options *opt, const char *arg)
Junio C Hamanob8767f72015-05-26 10:11:28 -07004480{
Junio C Hamano077965f2016-10-04 15:03:33 -07004481 int val = parse_ws_error_highlight(arg);
Junio C Hamanob8767f72015-05-26 10:11:28 -07004482
Junio C Hamano077965f2016-10-04 15:03:33 -07004483 if (val < 0) {
4484 error("unknown value after ws-error-highlight=%.*s",
4485 -1 - val, arg);
4486 return 0;
Junio C Hamanob8767f72015-05-26 10:11:28 -07004487 }
4488 opt->ws_error_highlight = val;
4489 return 1;
4490}
4491
Duy Nguyena97262c2016-01-21 18:48:44 +07004492int diff_opt_parse(struct diff_options *options,
4493 const char **av, int ac, const char *prefix)
Junio C Hamano6973dca2006-04-21 23:57:45 -07004494{
4495 const char *arg = av[0];
Matthieu Moydea007f2010-08-05 10:22:52 +02004496 const char *optarg;
4497 int argcount;
Pierre Habouzitd0546802007-11-07 11:20:32 +01004498
Duy Nguyena97262c2016-01-21 18:48:44 +07004499 if (!prefix)
4500 prefix = "";
4501
Pierre Habouzitd0546802007-11-07 11:20:32 +01004502 /* Output format options */
Matthieu Moy71482d32013-07-16 10:05:37 +02004503 if (!strcmp(arg, "-p") || !strcmp(arg, "-u") || !strcmp(arg, "--patch")
4504 || opt_arg(arg, 'U', "unified", &options->context))
4505 enable_patch_output(&options->output_format);
Timo Hirvonena6107862006-06-24 20:23:06 +03004506 else if (!strcmp(arg, "--raw"))
4507 options->output_format |= DIFF_FORMAT_RAW;
Matthieu Moy71482d32013-07-16 10:05:37 +02004508 else if (!strcmp(arg, "--patch-with-raw")) {
4509 enable_patch_output(&options->output_format);
4510 options->output_format |= DIFF_FORMAT_RAW;
4511 } else if (!strcmp(arg, "--numstat"))
Junio C Hamano74e2abe2006-10-12 03:01:00 -07004512 options->output_format |= DIFF_FORMAT_NUMSTAT;
Pierre Habouzit8f67f8a2007-11-10 20:05:14 +01004513 else if (!strcmp(arg, "--shortstat"))
Nicolas Pitreebd124c2006-12-14 23:15:44 -05004514 options->output_format |= DIFF_FORMAT_SHORTSTAT;
Christian Couder948cbe62017-12-09 21:40:09 +01004515 else if (skip_prefix(arg, "-X", &arg) ||
4516 skip_to_optional_arg(arg, "--dirstat", &arg))
Jeff Kingae021d82014-06-18 15:47:50 -04004517 return parse_dirstat_opt(options, arg);
Johan Herland333f3fb2011-04-29 11:36:18 +02004518 else if (!strcmp(arg, "--cumulative"))
4519 return parse_dirstat_opt(options, "cumulative");
Christian Couder948cbe62017-12-09 21:40:09 +01004520 else if (skip_to_optional_arg(arg, "--dirstat-by-file", &arg)) {
Johan Herland333f3fb2011-04-29 11:36:18 +02004521 parse_dirstat_opt(options, "files");
Jeff Kingae021d82014-06-18 15:47:50 -04004522 return parse_dirstat_opt(options, arg);
Junio C Hamanof88d2252008-09-02 17:28:59 -07004523 }
Pierre Habouzitd0546802007-11-07 11:20:32 +01004524 else if (!strcmp(arg, "--check"))
4525 options->output_format |= DIFF_FORMAT_CHECKDIFF;
4526 else if (!strcmp(arg, "--summary"))
4527 options->output_format |= DIFF_FORMAT_SUMMARY;
Matthieu Moy71482d32013-07-16 10:05:37 +02004528 else if (!strcmp(arg, "--patch-with-stat")) {
4529 enable_patch_output(&options->output_format);
4530 options->output_format |= DIFF_FORMAT_DIFFSTAT;
4531 } else if (!strcmp(arg, "--name-only"))
Pierre Habouzitd0546802007-11-07 11:20:32 +01004532 options->output_format |= DIFF_FORMAT_NAME;
4533 else if (!strcmp(arg, "--name-status"))
4534 options->output_format |= DIFF_FORMAT_NAME_STATUS;
Matthieu Moyd09cd152013-07-16 10:05:36 +02004535 else if (!strcmp(arg, "-s") || !strcmp(arg, "--no-patch"))
Pierre Habouzitd0546802007-11-07 11:20:32 +01004536 options->output_format |= DIFF_FORMAT_NO_OUTPUT;
Christian Couder59556542013-11-30 21:55:40 +01004537 else if (starts_with(arg, "--stat"))
Michael J Gruber808e1db2011-05-27 14:36:41 +02004538 /* --stat, --stat-width, --stat-name-width, or --stat-count */
Jonathan Nieder4d7f7a42010-08-05 10:22:53 +02004539 return stat_opt(options, av);
Pierre Habouzitd0546802007-11-07 11:20:32 +01004540
4541 /* renames options */
Christian Couder948cbe62017-12-09 21:40:09 +01004542 else if (starts_with(arg, "-B") ||
4543 skip_to_optional_arg(arg, "--break-rewrites", NULL)) {
Pierre Habouzit8f67f8a2007-11-10 20:05:14 +01004544 if ((options->break_opt = diff_scoreopt_parse(arg)) == -1)
Jeff King07cd7262010-10-21 10:49:47 -04004545 return error("invalid argument to -B: %s", arg+2);
Junio C Hamano6973dca2006-04-21 23:57:45 -07004546 }
Christian Couder948cbe62017-12-09 21:40:09 +01004547 else if (starts_with(arg, "-M") ||
4548 skip_to_optional_arg(arg, "--find-renames", NULL)) {
Pierre Habouzit8f67f8a2007-11-10 20:05:14 +01004549 if ((options->rename_score = diff_scoreopt_parse(arg)) == -1)
Jeff King07cd7262010-10-21 10:49:47 -04004550 return error("invalid argument to -M: %s", arg+2);
Junio C Hamano6973dca2006-04-21 23:57:45 -07004551 options->detect_rename = DIFF_DETECT_RENAME;
4552 }
Junio C Hamano467ddc12011-02-28 16:11:55 -08004553 else if (!strcmp(arg, "-D") || !strcmp(arg, "--irreversible-delete")) {
4554 options->irreversible_delete = 1;
4555 }
Christian Couder948cbe62017-12-09 21:40:09 +01004556 else if (starts_with(arg, "-C") ||
4557 skip_to_optional_arg(arg, "--find-copies", NULL)) {
Johannes Schindelinca6c0972007-06-11 21:12:19 +01004558 if (options->detect_rename == DIFF_DETECT_COPY)
Brandon Williams0d1e0e72017-10-31 11:19:11 -07004559 options->flags.find_copies_harder = 1;
Pierre Habouzit8f67f8a2007-11-10 20:05:14 +01004560 if ((options->rename_score = diff_scoreopt_parse(arg)) == -1)
Jeff King07cd7262010-10-21 10:49:47 -04004561 return error("invalid argument to -C: %s", arg+2);
Junio C Hamano6973dca2006-04-21 23:57:45 -07004562 options->detect_rename = DIFF_DETECT_COPY;
4563 }
Pierre Habouzitd0546802007-11-07 11:20:32 +01004564 else if (!strcmp(arg, "--no-renames"))
4565 options->detect_rename = 0;
Jeff King90d43b02012-03-22 18:52:13 -04004566 else if (!strcmp(arg, "--rename-empty"))
Brandon Williams0d1e0e72017-10-31 11:19:11 -07004567 options->flags.rename_empty = 1;
Jeff King90d43b02012-03-22 18:52:13 -04004568 else if (!strcmp(arg, "--no-rename-empty"))
Brandon Williams0d1e0e72017-10-31 11:19:11 -07004569 options->flags.rename_empty = 0;
Junio C Hamano1efad512017-12-09 21:40:11 +01004570 else if (skip_to_optional_arg_default(arg, "--relative", &arg, NULL)) {
Brandon Williams0d1e0e72017-10-31 11:19:11 -07004571 options->flags.relative_name = 1;
Junio C Hamano1efad512017-12-09 21:40:11 +01004572 if (arg)
4573 options->prefix = arg;
Junio C Hamanoc0cb4a02008-02-13 00:34:39 -08004574 }
Pierre Habouzitd0546802007-11-07 11:20:32 +01004575
4576 /* xdiff options */
Junio C Hamano81b568c2011-10-01 21:56:28 -07004577 else if (!strcmp(arg, "--minimal"))
4578 DIFF_XDL_SET(options, NEED_MINIMAL);
4579 else if (!strcmp(arg, "--no-minimal"))
4580 DIFF_XDL_CLR(options, NEED_MINIMAL);
Pierre Habouzitd0546802007-11-07 11:20:32 +01004581 else if (!strcmp(arg, "-w") || !strcmp(arg, "--ignore-all-space"))
Keith Cascio628d5c22009-02-16 19:26:49 -08004582 DIFF_XDL_SET(options, IGNORE_WHITESPACE);
Pierre Habouzitd0546802007-11-07 11:20:32 +01004583 else if (!strcmp(arg, "-b") || !strcmp(arg, "--ignore-space-change"))
Keith Cascio628d5c22009-02-16 19:26:49 -08004584 DIFF_XDL_SET(options, IGNORE_WHITESPACE_CHANGE);
Pierre Habouzitd0546802007-11-07 11:20:32 +01004585 else if (!strcmp(arg, "--ignore-space-at-eol"))
Keith Cascio628d5c22009-02-16 19:26:49 -08004586 DIFF_XDL_SET(options, IGNORE_WHITESPACE_AT_EOL);
Junio C Hamanoe9282f02017-10-26 15:32:27 +09004587 else if (!strcmp(arg, "--ignore-cr-at-eol"))
4588 DIFF_XDL_SET(options, IGNORE_CR_AT_EOL);
Antoine Pelisse36617af2013-06-19 20:46:07 +02004589 else if (!strcmp(arg, "--ignore-blank-lines"))
4590 DIFF_XDL_SET(options, IGNORE_BLANK_LINES);
Junio C Hamano3cde4e02016-12-23 12:32:22 -08004591 else if (!strcmp(arg, "--indent-heuristic"))
Michael Haggerty433860f2016-09-05 11:44:51 +02004592 DIFF_XDL_SET(options, INDENT_HEURISTIC);
Junio C Hamano3cde4e02016-12-23 12:32:22 -08004593 else if (!strcmp(arg, "--no-indent-heuristic"))
Michael Haggerty433860f2016-09-05 11:44:51 +02004594 DIFF_XDL_CLR(options, INDENT_HEURISTIC);
Jonathan Tan2477ab22017-11-27 11:47:47 -08004595 else if (!strcmp(arg, "--patience")) {
4596 int i;
Junio C Hamano307ab202012-02-19 15:36:55 -08004597 options->xdl_opts = DIFF_WITH_ALG(options, PATIENCE_DIFF);
Jonathan Tan2477ab22017-11-27 11:47:47 -08004598 /*
4599 * Both --patience and --anchored use PATIENCE_DIFF
4600 * internally, so remove any anchors previously
4601 * specified.
4602 */
4603 for (i = 0; i < options->anchors_nr; i++)
4604 free(options->anchors[i]);
4605 options->anchors_nr = 0;
4606 } else if (!strcmp(arg, "--histogram"))
Junio C Hamano307ab202012-02-19 15:36:55 -08004607 options->xdl_opts = DIFF_WITH_ALG(options, HISTOGRAM_DIFF);
John Keeping0895c6d2013-04-05 13:19:14 +01004608 else if ((argcount = parse_long_opt("diff-algorithm", av, &optarg))) {
4609 long value = parse_algorithm_value(optarg);
Michal Privoznik07924d42013-01-16 08:51:58 +01004610 if (value < 0)
4611 return error("option diff-algorithm accepts \"myers\", "
4612 "\"minimal\", \"patience\" and \"histogram\"");
4613 /* clear out previous settings */
4614 DIFF_XDL_CLR(options, NEED_MINIMAL);
4615 options->xdl_opts &= ~XDF_DIFF_ALGORITHM_MASK;
4616 options->xdl_opts |= value;
John Keeping0895c6d2013-04-05 13:19:14 +01004617 return argcount;
Jonathan Tan2477ab22017-11-27 11:47:47 -08004618 } else if (skip_prefix(arg, "--anchored=", &arg)) {
4619 options->xdl_opts = DIFF_WITH_ALG(options, PATIENCE_DIFF);
4620 ALLOC_GROW(options->anchors, options->anchors_nr + 1,
4621 options->anchors_alloc);
4622 options->anchors[options->anchors_nr++] = xstrdup(arg);
Michal Privoznik07924d42013-01-16 08:51:58 +01004623 }
Pierre Habouzitd0546802007-11-07 11:20:32 +01004624
4625 /* flags options */
4626 else if (!strcmp(arg, "--binary")) {
Matthieu Moy71482d32013-07-16 10:05:37 +02004627 enable_patch_output(&options->output_format);
Brandon Williams0d1e0e72017-10-31 11:19:11 -07004628 options->flags.binary = 1;
Pierre Habouzitd0546802007-11-07 11:20:32 +01004629 }
4630 else if (!strcmp(arg, "--full-index"))
Brandon Williams0d1e0e72017-10-31 11:19:11 -07004631 options->flags.full_index = 1;
Pierre Habouzitd0546802007-11-07 11:20:32 +01004632 else if (!strcmp(arg, "-a") || !strcmp(arg, "--text"))
Brandon Williams0d1e0e72017-10-31 11:19:11 -07004633 options->flags.text = 1;
Pierre Habouzitd0546802007-11-07 11:20:32 +01004634 else if (!strcmp(arg, "-R"))
Brandon Williams0d1e0e72017-10-31 11:19:11 -07004635 options->flags.reverse_diff = 1;
Junio C Hamano6973dca2006-04-21 23:57:45 -07004636 else if (!strcmp(arg, "--find-copies-harder"))
Brandon Williams0d1e0e72017-10-31 11:19:11 -07004637 options->flags.find_copies_harder = 1;
Linus Torvalds750f7b62007-06-19 14:22:46 -07004638 else if (!strcmp(arg, "--follow"))
Brandon Williams0d1e0e72017-10-31 11:19:11 -07004639 options->flags.follow_renames = 1;
David Turner076c9832015-07-07 21:29:34 -04004640 else if (!strcmp(arg, "--no-follow")) {
Brandon Williams0d1e0e72017-10-31 11:19:11 -07004641 options->flags.follow_renames = 0;
4642 options->flags.default_follow_renames = 0;
Christian Coudercf81f942017-12-09 21:40:10 +01004643 } else if (skip_to_optional_arg_default(arg, "--color", &arg, "always")) {
Jeff Kingae021d82014-06-18 15:47:50 -04004644 int value = git_config_colorbool(NULL, arg);
Jeff Kingdaa0c3d2011-08-17 22:04:23 -07004645 if (value < 0)
Mark Lodato73e9da02010-02-16 23:55:58 -05004646 return error("option `color' expects \"always\", \"auto\", or \"never\"");
Jeff Kingdaa0c3d2011-08-17 22:04:23 -07004647 options->use_color = value;
Mark Lodato73e9da02010-02-16 23:55:58 -05004648 }
Pierre Habouzitd0546802007-11-07 11:20:32 +01004649 else if (!strcmp(arg, "--no-color"))
Jeff Kingf1c96262011-08-17 22:03:12 -07004650 options->use_color = 0;
Stefan Beller2e2d5ac2017-06-30 13:53:07 -07004651 else if (!strcmp(arg, "--color-moved")) {
4652 if (diff_color_moved_default)
4653 options->color_moved = diff_color_moved_default;
4654 if (options->color_moved == COLOR_MOVED_NO)
4655 options->color_moved = COLOR_MOVED_DEFAULT;
4656 } else if (!strcmp(arg, "--no-color-moved"))
4657 options->color_moved = COLOR_MOVED_NO;
4658 else if (skip_prefix(arg, "--color-moved=", &arg)) {
4659 int cm = parse_color_moved(arg);
4660 if (cm < 0)
4661 die("bad --color-moved argument: %s", arg);
4662 options->color_moved = cm;
Christian Coudercf81f942017-12-09 21:40:10 +01004663 } else if (skip_to_optional_arg_default(arg, "--color-words", &options->word_regex, NULL)) {
Jeff Kingf1c96262011-08-17 22:03:12 -07004664 options->use_color = 1;
Thomas Rast882749a2010-04-14 17:59:06 +02004665 options->word_diff = DIFF_WORDS_COLOR;
Keith Cascio628d5c22009-02-16 19:26:49 -08004666 }
Thomas Rast882749a2010-04-14 17:59:06 +02004667 else if (!strcmp(arg, "--word-diff")) {
4668 if (options->word_diff == DIFF_WORDS_NONE)
4669 options->word_diff = DIFF_WORDS_PLAIN;
4670 }
Jeff Kingae021d82014-06-18 15:47:50 -04004671 else if (skip_prefix(arg, "--word-diff=", &arg)) {
4672 if (!strcmp(arg, "plain"))
Thomas Rast882749a2010-04-14 17:59:06 +02004673 options->word_diff = DIFF_WORDS_PLAIN;
Jeff Kingae021d82014-06-18 15:47:50 -04004674 else if (!strcmp(arg, "color")) {
Jeff Kingf1c96262011-08-17 22:03:12 -07004675 options->use_color = 1;
Thomas Rast882749a2010-04-14 17:59:06 +02004676 options->word_diff = DIFF_WORDS_COLOR;
4677 }
Jeff Kingae021d82014-06-18 15:47:50 -04004678 else if (!strcmp(arg, "porcelain"))
Thomas Rast882749a2010-04-14 17:59:06 +02004679 options->word_diff = DIFF_WORDS_PORCELAIN;
Jeff Kingae021d82014-06-18 15:47:50 -04004680 else if (!strcmp(arg, "none"))
Thomas Rast882749a2010-04-14 17:59:06 +02004681 options->word_diff = DIFF_WORDS_NONE;
4682 else
Jeff Kingae021d82014-06-18 15:47:50 -04004683 die("bad --word-diff argument: %s", arg);
Thomas Rast882749a2010-04-14 17:59:06 +02004684 }
Matthieu Moydea007f2010-08-05 10:22:52 +02004685 else if ((argcount = parse_long_opt("word-diff-regex", av, &optarg))) {
Thomas Rast882749a2010-04-14 17:59:06 +02004686 if (options->word_diff == DIFF_WORDS_NONE)
4687 options->word_diff = DIFF_WORDS_PLAIN;
Matthieu Moydea007f2010-08-05 10:22:52 +02004688 options->word_regex = optarg;
4689 return argcount;
Thomas Rast882749a2010-04-14 17:59:06 +02004690 }
Pierre Habouzitd0546802007-11-07 11:20:32 +01004691 else if (!strcmp(arg, "--exit-code"))
Brandon Williams0d1e0e72017-10-31 11:19:11 -07004692 options->flags.exit_with_status = 1;
Pierre Habouzitd0546802007-11-07 11:20:32 +01004693 else if (!strcmp(arg, "--quiet"))
Brandon Williams0d1e0e72017-10-31 11:19:11 -07004694 options->flags.quick = 1;
Pierre Habouzitd0546802007-11-07 11:20:32 +01004695 else if (!strcmp(arg, "--ext-diff"))
Brandon Williams0d1e0e72017-10-31 11:19:11 -07004696 options->flags.allow_external = 1;
Pierre Habouzitd0546802007-11-07 11:20:32 +01004697 else if (!strcmp(arg, "--no-ext-diff"))
Brandon Williams0d1e0e72017-10-31 11:19:11 -07004698 options->flags.allow_external = 0;
Brandon Williamsafa73c52017-10-31 11:19:06 -07004699 else if (!strcmp(arg, "--textconv")) {
Brandon Williams0d1e0e72017-10-31 11:19:11 -07004700 options->flags.allow_textconv = 1;
4701 options->flags.textconv_set_via_cmdline = 1;
Brandon Williamsafa73c52017-10-31 11:19:06 -07004702 } else if (!strcmp(arg, "--no-textconv"))
Brandon Williams0d1e0e72017-10-31 11:19:11 -07004703 options->flags.allow_textconv = 0;
Christian Coudercf81f942017-12-09 21:40:10 +01004704 else if (skip_to_optional_arg_default(arg, "--ignore-submodules", &arg, "all")) {
Brandon Williams0d1e0e72017-10-31 11:19:11 -07004705 options->flags.override_submodule_config = 1;
Jeff Kingae021d82014-06-18 15:47:50 -04004706 handle_ignore_submodules_arg(options, arg);
Christian Coudercf81f942017-12-09 21:40:10 +01004707 } else if (skip_to_optional_arg_default(arg, "--submodule", &arg, "log"))
Jeff Kingae021d82014-06-18 15:47:50 -04004708 return parse_submodule_opt(options, arg);
Junio C Hamanob8767f72015-05-26 10:11:28 -07004709 else if (skip_prefix(arg, "--ws-error-highlight=", &arg))
Junio C Hamano077965f2016-10-04 15:03:33 -07004710 return parse_ws_error_highlight_opt(options, arg);
Nguyễn Thái Ngọc Duyb42b4512016-10-24 17:42:20 +07004711 else if (!strcmp(arg, "--ita-invisible-in-index"))
4712 options->ita_invisible_in_index = 1;
4713 else if (!strcmp(arg, "--ita-visible-in-index"))
4714 options->ita_invisible_in_index = 0;
Pierre Habouzitd0546802007-11-07 11:20:32 +01004715
4716 /* misc options */
4717 else if (!strcmp(arg, "-z"))
4718 options->line_termination = 0;
Matthieu Moydea007f2010-08-05 10:22:52 +02004719 else if ((argcount = short_opt('l', av, &optarg))) {
4720 options->rename_limit = strtoul(optarg, NULL, 10);
4721 return argcount;
4722 }
4723 else if ((argcount = short_opt('S', av, &optarg))) {
4724 options->pickaxe = optarg;
Junio C Hamanof506b8e2010-08-23 10:17:03 -07004725 options->pickaxe_opts |= DIFF_PICKAXE_KIND_S;
4726 return argcount;
4727 } else if ((argcount = short_opt('G', av, &optarg))) {
4728 options->pickaxe = optarg;
4729 options->pickaxe_opts |= DIFF_PICKAXE_KIND_G;
Matthieu Moydea007f2010-08-05 10:22:52 +02004730 return argcount;
4731 }
Pierre Habouzitd0546802007-11-07 11:20:32 +01004732 else if (!strcmp(arg, "--pickaxe-all"))
Junio C Hamanof506b8e2010-08-23 10:17:03 -07004733 options->pickaxe_opts |= DIFF_PICKAXE_ALL;
Pierre Habouzitd0546802007-11-07 11:20:32 +01004734 else if (!strcmp(arg, "--pickaxe-regex"))
Junio C Hamanof506b8e2010-08-23 10:17:03 -07004735 options->pickaxe_opts |= DIFF_PICKAXE_REGEX;
Matthieu Moydea007f2010-08-05 10:22:52 +02004736 else if ((argcount = short_opt('O', av, &optarg))) {
Jeff Kinge4da43b2017-03-20 21:28:49 -04004737 options->orderfile = prefix_filename(prefix, optarg);
Matthieu Moydea007f2010-08-05 10:22:52 +02004738 return argcount;
4739 }
4740 else if ((argcount = parse_long_opt("diff-filter", av, &optarg))) {
Junio C Hamano1ecc1cb2013-07-17 15:05:46 -07004741 int offending = parse_diff_filter_opt(optarg, options);
4742 if (offending)
4743 die("unknown change class '%c' in --diff-filter=%s",
4744 offending, optarg);
Matthieu Moydea007f2010-08-05 10:22:52 +02004745 return argcount;
4746 }
Jack Bates43d19482016-12-06 09:56:14 -07004747 else if (!strcmp(arg, "--no-abbrev"))
4748 options->abbrev = 0;
Junio C Hamano6973dca2006-04-21 23:57:45 -07004749 else if (!strcmp(arg, "--abbrev"))
4750 options->abbrev = DEFAULT_ABBREV;
Jeff Kingae021d82014-06-18 15:47:50 -04004751 else if (skip_prefix(arg, "--abbrev=", &arg)) {
4752 options->abbrev = strtoul(arg, NULL, 10);
Junio C Hamano6973dca2006-04-21 23:57:45 -07004753 if (options->abbrev < MINIMUM_ABBREV)
4754 options->abbrev = MINIMUM_ABBREV;
4755 else if (40 < options->abbrev)
4756 options->abbrev = 40;
4757 }
Matthieu Moydea007f2010-08-05 10:22:52 +02004758 else if ((argcount = parse_long_opt("src-prefix", av, &optarg))) {
4759 options->a_prefix = optarg;
4760 return argcount;
4761 }
Jacob Keller660e1132016-08-31 16:27:20 -07004762 else if ((argcount = parse_long_opt("line-prefix", av, &optarg))) {
4763 options->line_prefix = optarg;
4764 options->line_prefix_length = strlen(options->line_prefix);
4765 graph_setup_line_prefix(options);
4766 return argcount;
4767 }
Matthieu Moydea007f2010-08-05 10:22:52 +02004768 else if ((argcount = parse_long_opt("dst-prefix", av, &optarg))) {
4769 options->b_prefix = optarg;
4770 return argcount;
4771 }
Johannes Schindelineab9a402007-12-18 19:32:14 +00004772 else if (!strcmp(arg, "--no-prefix"))
4773 options->a_prefix = options->b_prefix = "";
René Scharfe6d0e6742008-12-28 19:45:32 +01004774 else if (opt_arg(arg, '\0', "inter-hunk-context",
4775 &options->interhunkcontext))
4776 ;
René Scharfe14937c22011-10-09 13:36:57 +02004777 else if (!strcmp(arg, "-W"))
Brandon Williams0d1e0e72017-10-31 11:19:11 -07004778 options->flags.funccontext = 1;
René Scharfe14937c22011-10-09 13:36:57 +02004779 else if (!strcmp(arg, "--function-context"))
Brandon Williams0d1e0e72017-10-31 11:19:11 -07004780 options->flags.funccontext = 1;
René Scharfe14937c22011-10-09 13:36:57 +02004781 else if (!strcmp(arg, "--no-function-context"))
Brandon Williams0d1e0e72017-10-31 11:19:11 -07004782 options->flags.funccontext = 0;
Matthieu Moydea007f2010-08-05 10:22:52 +02004783 else if ((argcount = parse_long_opt("output", av, &optarg))) {
Jeff Kinge4da43b2017-03-20 21:28:49 -04004784 char *path = prefix_filename(prefix, optarg);
Nguyễn Thái Ngọc Duy23a9e072017-05-03 17:16:46 +07004785 options->file = xfopen(path, "w");
Daniel Barkalowc0c77732008-03-09 22:43:39 -04004786 options->close_file = 1;
Johannes Schindelinafc676f2016-06-22 16:41:07 +02004787 if (options->use_color != GIT_COLOR_ALWAYS)
4788 options->use_color = GIT_COLOR_NEVER;
Jeff Kinge4da43b2017-03-20 21:28:49 -04004789 free(path);
Matthieu Moydea007f2010-08-05 10:22:52 +02004790 return argcount;
Daniel Barkalowc0c77732008-03-09 22:43:39 -04004791 } else
Junio C Hamano6973dca2006-04-21 23:57:45 -07004792 return 0;
4793 return 1;
4794}
4795
Kevin Ballard10ae7522010-09-27 16:58:25 -07004796int parse_rename_score(const char **cp_p)
Junio C Hamano6973dca2006-04-21 23:57:45 -07004797{
4798 unsigned long num, scale;
4799 int ch, dot;
4800 const char *cp = *cp_p;
4801
4802 num = 0;
4803 scale = 1;
4804 dot = 0;
Brian Gianforcaroeeefa7c2009-09-01 01:35:10 -04004805 for (;;) {
Junio C Hamano6973dca2006-04-21 23:57:45 -07004806 ch = *cp;
4807 if ( !dot && ch == '.' ) {
4808 scale = 1;
4809 dot = 1;
4810 } else if ( ch == '%' ) {
4811 scale = dot ? scale*100 : 100;
4812 cp++; /* % is always at the end */
4813 break;
4814 } else if ( ch >= '0' && ch <= '9' ) {
4815 if ( scale < 100000 ) {
4816 scale *= 10;
4817 num = (num*10) + (ch-'0');
4818 }
4819 } else {
4820 break;
4821 }
4822 cp++;
4823 }
4824 *cp_p = cp;
4825
4826 /* user says num divided by scale and we say internally that
4827 * is MAX_SCORE * num / scale.
4828 */
Shawn O. Pearcedc49cd72007-03-06 20:44:37 -05004829 return (int)((num >= scale) ? MAX_SCORE : (MAX_SCORE * num / scale));
Junio C Hamano6973dca2006-04-21 23:57:45 -07004830}
4831
Junio C Hamano16befb82007-06-08 02:54:57 -07004832static int diff_scoreopt_parse(const char *opt)
Junio C Hamano6973dca2006-04-21 23:57:45 -07004833{
4834 int opt1, opt2, cmd;
4835
4836 if (*opt++ != '-')
4837 return -1;
4838 cmd = *opt++;
Kevin Ballard37ab5152010-09-27 16:58:26 -07004839 if (cmd == '-') {
4840 /* convert the long-form arguments into short-form versions */
Jeff King95b567c2014-06-18 15:48:29 -04004841 if (skip_prefix(opt, "break-rewrites", &opt)) {
Kevin Ballard37ab5152010-09-27 16:58:26 -07004842 if (*opt == 0 || *opt++ == '=')
4843 cmd = 'B';
Jeff King95b567c2014-06-18 15:48:29 -04004844 } else if (skip_prefix(opt, "find-copies", &opt)) {
Kevin Ballard37ab5152010-09-27 16:58:26 -07004845 if (*opt == 0 || *opt++ == '=')
4846 cmd = 'C';
Jeff King95b567c2014-06-18 15:48:29 -04004847 } else if (skip_prefix(opt, "find-renames", &opt)) {
Kevin Ballard37ab5152010-09-27 16:58:26 -07004848 if (*opt == 0 || *opt++ == '=')
4849 cmd = 'M';
4850 }
4851 }
Junio C Hamano6973dca2006-04-21 23:57:45 -07004852 if (cmd != 'M' && cmd != 'C' && cmd != 'B')
Justin Lebar01689902014-03-31 15:11:46 -07004853 return -1; /* that is not a -M, -C, or -B option */
Junio C Hamano6973dca2006-04-21 23:57:45 -07004854
Kevin Ballard10ae7522010-09-27 16:58:25 -07004855 opt1 = parse_rename_score(&opt);
Junio C Hamano6973dca2006-04-21 23:57:45 -07004856 if (cmd != 'B')
4857 opt2 = 0;
4858 else {
4859 if (*opt == 0)
4860 opt2 = 0;
4861 else if (*opt != '/')
4862 return -1; /* we expect -B80/99 or -B80 */
4863 else {
4864 opt++;
Kevin Ballard10ae7522010-09-27 16:58:25 -07004865 opt2 = parse_rename_score(&opt);
Junio C Hamano6973dca2006-04-21 23:57:45 -07004866 }
4867 }
4868 if (*opt != 0)
4869 return -1;
4870 return opt1 | (opt2 << 16);
4871}
4872
4873struct diff_queue_struct diff_queued_diff;
4874
4875void diff_q(struct diff_queue_struct *queue, struct diff_filepair *dp)
4876{
Dmitry S. Dolzhenko4c960a42014-03-04 02:31:53 +04004877 ALLOC_GROW(queue->queue, queue->nr + 1, queue->alloc);
Junio C Hamano6973dca2006-04-21 23:57:45 -07004878 queue->queue[queue->nr++] = dp;
4879}
4880
4881struct diff_filepair *diff_queue(struct diff_queue_struct *queue,
4882 struct diff_filespec *one,
4883 struct diff_filespec *two)
4884{
Junio C Hamanoef677682006-08-03 12:01:01 -07004885 struct diff_filepair *dp = xcalloc(1, sizeof(*dp));
Junio C Hamano6973dca2006-04-21 23:57:45 -07004886 dp->one = one;
4887 dp->two = two;
Junio C Hamano6973dca2006-04-21 23:57:45 -07004888 if (queue)
4889 diff_q(queue, dp);
4890 return dp;
4891}
4892
4893void diff_free_filepair(struct diff_filepair *p)
4894{
Linus Torvalds9fb88412007-10-25 11:19:10 -07004895 free_filespec(p->one);
4896 free_filespec(p->two);
Junio C Hamano6973dca2006-04-21 23:57:45 -07004897 free(p);
4898}
4899
Jeff Kingd6cece52016-10-20 02:20:07 -04004900const char *diff_aligned_abbrev(const struct object_id *oid, int len)
Junio C Hamano6973dca2006-04-21 23:57:45 -07004901{
4902 int abblen;
4903 const char *abbrev;
Junio C Hamano6973dca2006-04-21 23:57:45 -07004904
Ann T Ropea7cb6ac12017-12-03 22:27:42 +01004905 /* Do we want all 40 hex characters? */
Jeff Kingd6cece52016-10-20 02:20:07 -04004906 if (len == GIT_SHA1_HEXSZ)
4907 return oid_to_hex(oid);
4908
Ann T Ropea7cb6ac12017-12-03 22:27:42 +01004909 /* An abbreviated value is fine, possibly followed by an ellipsis. */
Jeff King4f036662016-10-20 02:21:25 -04004910 abbrev = diff_abbrev_oid(oid, len);
Ann T Ropea7cb6ac12017-12-03 22:27:42 +01004911
4912 if (!print_sha1_ellipsis())
4913 return abbrev;
4914
Junio C Hamano6973dca2006-04-21 23:57:45 -07004915 abblen = strlen(abbrev);
Junio C Hamanod709f1f2016-09-30 10:42:05 -07004916
4917 /*
Ann T Ropea7cb6ac12017-12-03 22:27:42 +01004918 * In well-behaved cases, where the abbreviated result is the
Junio C Hamanod709f1f2016-09-30 10:42:05 -07004919 * same as the requested length, append three dots after the
4920 * abbreviation (hence the whole logic is limited to the case
4921 * where abblen < 37); when the actual abbreviated result is a
4922 * bit longer than the requested length, we reduce the number
4923 * of dots so that they match the well-behaved ones. However,
4924 * if the actual abbreviation is longer than the requested
4925 * length by more than three, we give up on aligning, and add
4926 * three dots anyway, to indicate that the output is not the
4927 * full object name. Yes, this may be suboptimal, but this
4928 * appears only in "diff --raw --abbrev" output and it is not
4929 * worth the effort to change it now. Note that this would
4930 * likely to work fine when the automatic sizing of default
4931 * abbreviation length is used--we would be fed -1 in "len" in
4932 * that case, and will end up always appending three-dots, but
4933 * the automatic sizing is supposed to give abblen that ensures
4934 * uniqueness across all objects (statistically speaking).
4935 */
Jeff Kingd6cece52016-10-20 02:20:07 -04004936 if (abblen < GIT_SHA1_HEXSZ - 3) {
brian m. carlsondc015052017-03-26 16:01:24 +00004937 static char hex[GIT_MAX_HEXSZ + 1];
Junio C Hamano6973dca2006-04-21 23:57:45 -07004938 if (len < abblen && abblen <= len + 2)
Jeff King5096d492015-09-24 17:06:08 -04004939 xsnprintf(hex, sizeof(hex), "%s%.*s", abbrev, len+3-abblen, "..");
Junio C Hamano6973dca2006-04-21 23:57:45 -07004940 else
Jeff King5096d492015-09-24 17:06:08 -04004941 xsnprintf(hex, sizeof(hex), "%s...", abbrev);
Junio C Hamano6973dca2006-04-21 23:57:45 -07004942 return hex;
4943 }
Jeff Kingd6cece52016-10-20 02:20:07 -04004944
4945 return oid_to_hex(oid);
Junio C Hamano6973dca2006-04-21 23:57:45 -07004946}
4947
Pierre Habouzit663af342007-09-20 00:42:15 +02004948static void diff_flush_raw(struct diff_filepair *p, struct diff_options *opt)
Junio C Hamano6973dca2006-04-21 23:57:45 -07004949{
Pierre Habouzit663af342007-09-20 00:42:15 +02004950 int line_termination = opt->line_termination;
4951 int inter_name_termination = line_termination ? '\t' : '\0';
Timo Hirvonenc6744342006-06-24 20:21:53 +03004952
John Keeping30997bb2013-02-07 20:15:27 +00004953 fprintf(opt->file, "%s", diff_line_prefix(opt));
Pierre Habouzit663af342007-09-20 00:42:15 +02004954 if (!(opt->output_format & DIFF_FORMAT_NAME_STATUS)) {
Daniel Barkalowc0c77732008-03-09 22:43:39 -04004955 fprintf(opt->file, ":%06o %06o %s ", p->one->mode, p->two->mode,
Jeff Kingd6cece52016-10-20 02:20:07 -04004956 diff_aligned_abbrev(&p->one->oid, opt->abbrev));
brian m. carlsona0d12c42016-06-24 23:09:23 +00004957 fprintf(opt->file, "%s ",
Jeff Kingd6cece52016-10-20 02:20:07 -04004958 diff_aligned_abbrev(&p->two->oid, opt->abbrev));
Pierre Habouzit663af342007-09-20 00:42:15 +02004959 }
4960 if (p->score) {
Daniel Barkalowc0c77732008-03-09 22:43:39 -04004961 fprintf(opt->file, "%c%03d%c", p->status, similarity_index(p),
4962 inter_name_termination);
Pierre Habouzit663af342007-09-20 00:42:15 +02004963 } else {
Daniel Barkalowc0c77732008-03-09 22:43:39 -04004964 fprintf(opt->file, "%c%c", p->status, inter_name_termination);
Junio C Hamano6973dca2006-04-21 23:57:45 -07004965 }
4966
Junio C Hamanocd676a52008-02-12 14:26:02 -08004967 if (p->status == DIFF_STATUS_COPIED ||
4968 p->status == DIFF_STATUS_RENAMED) {
4969 const char *name_a, *name_b;
4970 name_a = p->one->path;
4971 name_b = p->two->path;
4972 strip_prefix(opt->prefix_length, &name_a, &name_b);
Daniel Barkalowc0c77732008-03-09 22:43:39 -04004973 write_name_quoted(name_a, opt->file, inter_name_termination);
4974 write_name_quoted(name_b, opt->file, line_termination);
Pierre Habouzit663af342007-09-20 00:42:15 +02004975 } else {
Junio C Hamanocd676a52008-02-12 14:26:02 -08004976 const char *name_a, *name_b;
4977 name_a = p->one->mode ? p->one->path : p->two->path;
4978 name_b = NULL;
4979 strip_prefix(opt->prefix_length, &name_a, &name_b);
Daniel Barkalowc0c77732008-03-09 22:43:39 -04004980 write_name_quoted(name_a, opt->file, line_termination);
Junio C Hamano6973dca2006-04-21 23:57:45 -07004981 }
Junio C Hamano6973dca2006-04-21 23:57:45 -07004982}
4983
4984int diff_unmodified_pair(struct diff_filepair *p)
4985{
4986 /* This function is written stricter than necessary to support
4987 * the currently implemented transformers, but the idea is to
4988 * let transformers to produce diff_filepairs any way they want,
4989 * and filter and clean them up here before producing the output.
4990 */
Pierre Habouzit663af342007-09-20 00:42:15 +02004991 struct diff_filespec *one = p->one, *two = p->two;
Junio C Hamano6973dca2006-04-21 23:57:45 -07004992
4993 if (DIFF_PAIR_UNMERGED(p))
4994 return 0; /* unmerged is interesting */
4995
Junio C Hamano6973dca2006-04-21 23:57:45 -07004996 /* deletion, addition, mode or type change
4997 * and rename are all interesting.
4998 */
4999 if (DIFF_FILE_VALID(one) != DIFF_FILE_VALID(two) ||
5000 DIFF_PAIR_MODE_CHANGED(p) ||
5001 strcmp(one->path, two->path))
5002 return 0;
5003
5004 /* both are valid and point at the same path. that is, we are
5005 * dealing with a change.
5006 */
brian m. carlson41c95602016-06-24 23:09:24 +00005007 if (one->oid_valid && two->oid_valid &&
brian m. carlsona0d12c42016-06-24 23:09:23 +00005008 !oidcmp(&one->oid, &two->oid) &&
Jens Lehmann85adbf22010-03-12 22:23:52 +01005009 !one->dirty_submodule && !two->dirty_submodule)
Junio C Hamano6973dca2006-04-21 23:57:45 -07005010 return 1; /* no change */
brian m. carlson41c95602016-06-24 23:09:24 +00005011 if (!one->oid_valid && !two->oid_valid)
Junio C Hamano6973dca2006-04-21 23:57:45 -07005012 return 1; /* both look at the same file on the filesystem. */
5013 return 0;
5014}
5015
5016static void diff_flush_patch(struct diff_filepair *p, struct diff_options *o)
5017{
5018 if (diff_unmodified_pair(p))
5019 return;
5020
5021 if ((DIFF_FILE_VALID(p->one) && S_ISDIR(p->one->mode)) ||
5022 (DIFF_FILE_VALID(p->two) && S_ISDIR(p->two->mode)))
5023 return; /* no tree diffs in patch format */
5024
5025 run_diff(p, o);
5026}
5027
5028static void diff_flush_stat(struct diff_filepair *p, struct diff_options *o,
5029 struct diffstat_t *diffstat)
5030{
5031 if (diff_unmodified_pair(p))
5032 return;
5033
5034 if ((DIFF_FILE_VALID(p->one) && S_ISDIR(p->one->mode)) ||
5035 (DIFF_FILE_VALID(p->two) && S_ISDIR(p->two->mode)))
Yann Dirsonc3fced62010-10-24 23:03:10 +02005036 return; /* no useful stat for tree diffs */
Junio C Hamano6973dca2006-04-21 23:57:45 -07005037
5038 run_diffstat(p, o, diffstat);
5039}
5040
Johannes Schindelin88246892006-05-20 23:43:13 +02005041static void diff_flush_checkdiff(struct diff_filepair *p,
5042 struct diff_options *o)
5043{
5044 if (diff_unmodified_pair(p))
5045 return;
5046
5047 if ((DIFF_FILE_VALID(p->one) && S_ISDIR(p->one->mode)) ||
5048 (DIFF_FILE_VALID(p->two) && S_ISDIR(p->two->mode)))
Yann Dirsonc3fced62010-10-24 23:03:10 +02005049 return; /* nothing to check in tree diffs */
Johannes Schindelin88246892006-05-20 23:43:13 +02005050
5051 run_checkdiff(p, o);
5052}
5053
Junio C Hamano6973dca2006-04-21 23:57:45 -07005054int diff_queue_is_empty(void)
5055{
5056 struct diff_queue_struct *q = &diff_queued_diff;
5057 int i;
5058 for (i = 0; i < q->nr; i++)
5059 if (!diff_unmodified_pair(q->queue[i]))
5060 return 0;
5061 return 1;
5062}
5063
5064#if DIFF_DEBUG
5065void diff_debug_filespec(struct diff_filespec *s, int x, const char *one)
5066{
5067 fprintf(stderr, "queue[%d] %s (%s) %s %06o %s\n",
5068 x, one ? one : "",
5069 s->path,
5070 DIFF_FILE_VALID(s) ? "valid" : "invalid",
5071 s->mode,
brian m. carlson41c95602016-06-24 23:09:24 +00005072 s->oid_valid ? oid_to_hex(&s->oid) : "");
Jeff King428d52a2014-01-16 20:21:59 -05005073 fprintf(stderr, "queue[%d] %s size %lu\n",
Junio C Hamano6973dca2006-04-21 23:57:45 -07005074 x, one ? one : "",
Jeff King428d52a2014-01-16 20:21:59 -05005075 s->size);
Junio C Hamano6973dca2006-04-21 23:57:45 -07005076}
5077
5078void diff_debug_filepair(const struct diff_filepair *p, int i)
5079{
5080 diff_debug_filespec(p->one, i, "one");
5081 diff_debug_filespec(p->two, i, "two");
Linus Torvalds64479712007-10-25 11:20:56 -07005082 fprintf(stderr, "score %d, status %c rename_used %d broken %d\n",
Junio C Hamano6973dca2006-04-21 23:57:45 -07005083 p->score, p->status ? p->status : '?',
Linus Torvalds64479712007-10-25 11:20:56 -07005084 p->one->rename_used, p->broken_pair);
Junio C Hamano6973dca2006-04-21 23:57:45 -07005085}
5086
5087void diff_debug_queue(const char *msg, struct diff_queue_struct *q)
5088{
5089 int i;
5090 if (msg)
5091 fprintf(stderr, "%s\n", msg);
5092 fprintf(stderr, "q->nr = %d\n", q->nr);
5093 for (i = 0; i < q->nr; i++) {
5094 struct diff_filepair *p = q->queue[i];
5095 diff_debug_filepair(p, i);
5096 }
5097}
5098#endif
5099
5100static void diff_resolve_rename_copy(void)
5101{
Linus Torvalds64479712007-10-25 11:20:56 -07005102 int i;
5103 struct diff_filepair *p;
Junio C Hamano6973dca2006-04-21 23:57:45 -07005104 struct diff_queue_struct *q = &diff_queued_diff;
5105
5106 diff_debug_queue("resolve-rename-copy", q);
5107
5108 for (i = 0; i < q->nr; i++) {
5109 p = q->queue[i];
5110 p->status = 0; /* undecided */
5111 if (DIFF_PAIR_UNMERGED(p))
5112 p->status = DIFF_STATUS_UNMERGED;
5113 else if (!DIFF_FILE_VALID(p->one))
5114 p->status = DIFF_STATUS_ADDED;
5115 else if (!DIFF_FILE_VALID(p->two))
5116 p->status = DIFF_STATUS_DELETED;
5117 else if (DIFF_PAIR_TYPE_CHANGED(p))
5118 p->status = DIFF_STATUS_TYPE_CHANGED;
5119
5120 /* from this point on, we are dealing with a pair
5121 * whose both sides are valid and of the same type, i.e.
5122 * either in-place edit or rename/copy edit.
5123 */
5124 else if (DIFF_PAIR_RENAME(p)) {
Linus Torvalds64479712007-10-25 11:20:56 -07005125 /*
5126 * A rename might have re-connected a broken
5127 * pair up, causing the pathnames to be the
5128 * same again. If so, that's not a rename at
5129 * all, just a modification..
5130 *
5131 * Otherwise, see if this source was used for
5132 * multiple renames, in which case we decrement
5133 * the count, and call it a copy.
Junio C Hamano6973dca2006-04-21 23:57:45 -07005134 */
Linus Torvalds64479712007-10-25 11:20:56 -07005135 if (!strcmp(p->one->path, p->two->path))
5136 p->status = DIFF_STATUS_MODIFIED;
5137 else if (--p->one->rename_used > 0)
Junio C Hamano6973dca2006-04-21 23:57:45 -07005138 p->status = DIFF_STATUS_COPIED;
Linus Torvalds64479712007-10-25 11:20:56 -07005139 else
Junio C Hamano6973dca2006-04-21 23:57:45 -07005140 p->status = DIFF_STATUS_RENAMED;
5141 }
brian m. carlsona0d12c42016-06-24 23:09:23 +00005142 else if (oidcmp(&p->one->oid, &p->two->oid) ||
Johannes Schindelind516c2d2007-02-22 21:50:10 +01005143 p->one->mode != p->two->mode ||
Jens Lehmann85adbf22010-03-12 22:23:52 +01005144 p->one->dirty_submodule ||
5145 p->two->dirty_submodule ||
brian m. carlsona0d12c42016-06-24 23:09:23 +00005146 is_null_oid(&p->one->oid))
Junio C Hamano6973dca2006-04-21 23:57:45 -07005147 p->status = DIFF_STATUS_MODIFIED;
5148 else {
5149 /* This is a "no-change" entry and should not
5150 * happen anymore, but prepare for broken callers.
5151 */
5152 error("feeding unmodified %s to diffcore",
5153 p->one->path);
5154 p->status = DIFF_STATUS_UNKNOWN;
5155 }
5156 }
5157 diff_debug_queue("resolve-rename-copy done", q);
5158}
5159
Timo Hirvonenc6744342006-06-24 20:21:53 +03005160static int check_pair_status(struct diff_filepair *p)
Junio C Hamano6973dca2006-04-21 23:57:45 -07005161{
Junio C Hamano6973dca2006-04-21 23:57:45 -07005162 switch (p->status) {
5163 case DIFF_STATUS_UNKNOWN:
Timo Hirvonenc6744342006-06-24 20:21:53 +03005164 return 0;
Junio C Hamano6973dca2006-04-21 23:57:45 -07005165 case 0:
5166 die("internal error in diff-resolve-rename-copy");
Junio C Hamano6973dca2006-04-21 23:57:45 -07005167 default:
Timo Hirvonenc6744342006-06-24 20:21:53 +03005168 return 1;
Junio C Hamano6973dca2006-04-21 23:57:45 -07005169 }
5170}
5171
Timo Hirvonenc6744342006-06-24 20:21:53 +03005172static void flush_one_pair(struct diff_filepair *p, struct diff_options *opt)
5173{
5174 int fmt = opt->output_format;
5175
5176 if (fmt & DIFF_FORMAT_CHECKDIFF)
5177 diff_flush_checkdiff(p, opt);
5178 else if (fmt & (DIFF_FORMAT_RAW | DIFF_FORMAT_NAME_STATUS))
5179 diff_flush_raw(p, opt);
Junio C Hamanocd676a52008-02-12 14:26:02 -08005180 else if (fmt & DIFF_FORMAT_NAME) {
5181 const char *name_a, *name_b;
5182 name_a = p->two->path;
5183 name_b = NULL;
5184 strip_prefix(opt->prefix_length, &name_a, &name_b);
Jeff Kingf5022b52017-02-08 15:31:15 -05005185 fprintf(opt->file, "%s", diff_line_prefix(opt));
Daniel Barkalowc0c77732008-03-09 22:43:39 -04005186 write_name_quoted(name_a, opt->file, opt->line_termination);
Junio C Hamanocd676a52008-02-12 14:26:02 -08005187 }
Timo Hirvonenc6744342006-06-24 20:21:53 +03005188}
5189
Stefan Beller146fdb02017-06-29 17:07:05 -07005190static void show_file_mode_name(struct diff_options *opt, const char *newdelete, struct diff_filespec *fs)
Sean4bbd2612006-05-14 08:13:49 -04005191{
Stefan Beller146fdb02017-06-29 17:07:05 -07005192 struct strbuf sb = STRBUF_INIT;
Sean4bbd2612006-05-14 08:13:49 -04005193 if (fs->mode)
Stefan Beller146fdb02017-06-29 17:07:05 -07005194 strbuf_addf(&sb, " %s mode %06o ", newdelete, fs->mode);
Sean4bbd2612006-05-14 08:13:49 -04005195 else
Stefan Beller146fdb02017-06-29 17:07:05 -07005196 strbuf_addf(&sb, " %s ", newdelete);
5197
5198 quote_c_style(fs->path, &sb, NULL, 0);
5199 strbuf_addch(&sb, '\n');
5200 emit_diff_symbol(opt, DIFF_SYMBOL_SUMMARY,
5201 sb.buf, sb.len, 0);
5202 strbuf_release(&sb);
Sean4bbd2612006-05-14 08:13:49 -04005203}
5204
Stefan Beller146fdb02017-06-29 17:07:05 -07005205static void show_mode_change(struct diff_options *opt, struct diff_filepair *p,
5206 int show_name)
Sean4bbd2612006-05-14 08:13:49 -04005207{
5208 if (p->one->mode && p->two->mode && p->one->mode != p->two->mode) {
Stefan Beller146fdb02017-06-29 17:07:05 -07005209 struct strbuf sb = STRBUF_INIT;
5210 strbuf_addf(&sb, " mode change %06o => %06o",
5211 p->one->mode, p->two->mode);
Alexandre Julliard0d26a642007-02-10 15:37:48 +01005212 if (show_name) {
Stefan Beller146fdb02017-06-29 17:07:05 -07005213 strbuf_addch(&sb, ' ');
5214 quote_c_style(p->two->path, &sb, NULL, 0);
Alexandre Julliard0d26a642007-02-10 15:37:48 +01005215 }
Stefan Beller58aaced2017-09-27 15:51:26 -07005216 strbuf_addch(&sb, '\n');
Stefan Beller146fdb02017-06-29 17:07:05 -07005217 emit_diff_symbol(opt, DIFF_SYMBOL_SUMMARY,
5218 sb.buf, sb.len, 0);
5219 strbuf_release(&sb);
Sean4bbd2612006-05-14 08:13:49 -04005220 }
5221}
5222
Stefan Beller146fdb02017-06-29 17:07:05 -07005223static void show_rename_copy(struct diff_options *opt, const char *renamecopy,
5224 struct diff_filepair *p)
Sean4bbd2612006-05-14 08:13:49 -04005225{
Stefan Beller146fdb02017-06-29 17:07:05 -07005226 struct strbuf sb = STRBUF_INIT;
Alexandre Julliardb9f44162007-02-10 15:36:47 +01005227 char *names = pprint_rename(p->one->path, p->two->path);
Stefan Beller146fdb02017-06-29 17:07:05 -07005228 strbuf_addf(&sb, " %s %s (%d%%)\n",
5229 renamecopy, names, similarity_index(p));
Alexandre Julliardb9f44162007-02-10 15:36:47 +01005230 free(names);
Stefan Beller146fdb02017-06-29 17:07:05 -07005231 emit_diff_symbol(opt, DIFF_SYMBOL_SUMMARY,
5232 sb.buf, sb.len, 0);
5233 show_mode_change(opt, p, 0);
Rene Scharfe348eda22017-08-30 19:49:42 +02005234 strbuf_release(&sb);
Sean4bbd2612006-05-14 08:13:49 -04005235}
5236
Bo Yang7be57612010-05-26 15:23:54 +08005237static void diff_summary(struct diff_options *opt, struct diff_filepair *p)
Sean4bbd2612006-05-14 08:13:49 -04005238{
5239 switch(p->status) {
5240 case DIFF_STATUS_DELETED:
Stefan Beller146fdb02017-06-29 17:07:05 -07005241 show_file_mode_name(opt, "delete", p->one);
Sean4bbd2612006-05-14 08:13:49 -04005242 break;
5243 case DIFF_STATUS_ADDED:
Stefan Beller146fdb02017-06-29 17:07:05 -07005244 show_file_mode_name(opt, "create", p->two);
Sean4bbd2612006-05-14 08:13:49 -04005245 break;
5246 case DIFF_STATUS_COPIED:
Stefan Beller146fdb02017-06-29 17:07:05 -07005247 show_rename_copy(opt, "copy", p);
Sean4bbd2612006-05-14 08:13:49 -04005248 break;
5249 case DIFF_STATUS_RENAMED:
Stefan Beller146fdb02017-06-29 17:07:05 -07005250 show_rename_copy(opt, "rename", p);
Sean4bbd2612006-05-14 08:13:49 -04005251 break;
5252 default:
5253 if (p->score) {
Stefan Beller146fdb02017-06-29 17:07:05 -07005254 struct strbuf sb = STRBUF_INIT;
5255 strbuf_addstr(&sb, " rewrite ");
5256 quote_c_style(p->two->path, &sb, NULL, 0);
5257 strbuf_addf(&sb, " (%d%%)\n", similarity_index(p));
5258 emit_diff_symbol(opt, DIFF_SYMBOL_SUMMARY,
5259 sb.buf, sb.len, 0);
Rene Scharfefa842d82017-08-30 19:49:41 +02005260 strbuf_release(&sb);
Pierre Habouzit663af342007-09-20 00:42:15 +02005261 }
Stefan Beller146fdb02017-06-29 17:07:05 -07005262 show_mode_change(opt, p, !p->score);
Sean4bbd2612006-05-14 08:13:49 -04005263 break;
5264 }
5265}
5266
Johannes Schindelinfcb3d0a2006-06-25 03:51:08 +02005267struct patch_id_t {
Nicolas Pitre9126f002008-10-01 14:05:20 -04005268 git_SHA_CTX *ctx;
Johannes Schindelinfcb3d0a2006-06-25 03:51:08 +02005269 int patchlen;
5270};
5271
5272static int remove_space(char *line, int len)
5273{
5274 int i;
Pierre Habouzit663af342007-09-20 00:42:15 +02005275 char *dst = line;
5276 unsigned char c;
Johannes Schindelinfcb3d0a2006-06-25 03:51:08 +02005277
Pierre Habouzit663af342007-09-20 00:42:15 +02005278 for (i = 0; i < len; i++)
5279 if (!isspace((c = line[i])))
5280 *dst++ = c;
Johannes Schindelinfcb3d0a2006-06-25 03:51:08 +02005281
Pierre Habouzit663af342007-09-20 00:42:15 +02005282 return dst - line;
Johannes Schindelinfcb3d0a2006-06-25 03:51:08 +02005283}
5284
5285static void patch_id_consume(void *priv, char *line, unsigned long len)
5286{
5287 struct patch_id_t *data = priv;
5288 int new_len;
5289
5290 /* Ignore line numbers when computing the SHA1 of the patch */
Christian Couder59556542013-11-30 21:55:40 +01005291 if (starts_with(line, "@@ -"))
Johannes Schindelinfcb3d0a2006-06-25 03:51:08 +02005292 return;
5293
5294 new_len = remove_space(line, len);
5295
Nicolas Pitre9126f002008-10-01 14:05:20 -04005296 git_SHA1_Update(data->ctx, line, new_len);
Johannes Schindelinfcb3d0a2006-06-25 03:51:08 +02005297 data->patchlen += new_len;
5298}
5299
Jeff King977db6b2017-03-30 14:26:05 -04005300static void patch_id_add_string(git_SHA_CTX *ctx, const char *str)
5301{
5302 git_SHA1_Update(ctx, str, strlen(str));
5303}
5304
5305static void patch_id_add_mode(git_SHA_CTX *ctx, unsigned mode)
5306{
5307 /* large enough for 2^32 in octal */
5308 char buf[12];
5309 int len = xsnprintf(buf, sizeof(buf), "%06o", mode);
5310 git_SHA1_Update(ctx, buf, len);
5311}
5312
Johannes Schindelinfcb3d0a2006-06-25 03:51:08 +02005313/* returns 0 upon success, and writes result into sha1 */
Brandon Williamsbd25f282017-05-30 10:30:54 -07005314static int diff_get_patch_id(struct diff_options *options, struct object_id *oid, int diff_header_only)
Johannes Schindelinfcb3d0a2006-06-25 03:51:08 +02005315{
5316 struct diff_queue_struct *q = &diff_queued_diff;
5317 int i;
Nicolas Pitre9126f002008-10-01 14:05:20 -04005318 git_SHA_CTX ctx;
Johannes Schindelinfcb3d0a2006-06-25 03:51:08 +02005319 struct patch_id_t data;
Johannes Schindelinfcb3d0a2006-06-25 03:51:08 +02005320
Nicolas Pitre9126f002008-10-01 14:05:20 -04005321 git_SHA1_Init(&ctx);
Johannes Schindelinfcb3d0a2006-06-25 03:51:08 +02005322 memset(&data, 0, sizeof(struct patch_id_t));
5323 data.ctx = &ctx;
Johannes Schindelinfcb3d0a2006-06-25 03:51:08 +02005324
5325 for (i = 0; i < q->nr; i++) {
5326 xpparam_t xpp;
5327 xdemitconf_t xecfg;
Johannes Schindelinfcb3d0a2006-06-25 03:51:08 +02005328 mmfile_t mf1, mf2;
5329 struct diff_filepair *p = q->queue[i];
5330 int len1, len2;
5331
Brian Downing9ccd0a82008-10-25 15:30:37 +02005332 memset(&xpp, 0, sizeof(xpp));
Johannes Schindelin30b25012007-07-04 19:05:46 +01005333 memset(&xecfg, 0, sizeof(xecfg));
Johannes Schindelinfcb3d0a2006-06-25 03:51:08 +02005334 if (p->status == 0)
5335 return error("internal diff status error");
5336 if (p->status == DIFF_STATUS_UNKNOWN)
5337 continue;
5338 if (diff_unmodified_pair(p))
5339 continue;
5340 if ((DIFF_FILE_VALID(p->one) && S_ISDIR(p->one->mode)) ||
5341 (DIFF_FILE_VALID(p->two) && S_ISDIR(p->two->mode)))
5342 continue;
5343 if (DIFF_PAIR_UNMERGED(p))
5344 continue;
5345
Brandon Williams94e327e2017-05-30 10:31:09 -07005346 diff_fill_oid_info(p->one);
5347 diff_fill_oid_info(p->two);
Johannes Schindelinfcb3d0a2006-06-25 03:51:08 +02005348
Johannes Schindelinfcb3d0a2006-06-25 03:51:08 +02005349 len1 = remove_space(p->one->path, strlen(p->one->path));
5350 len2 = remove_space(p->two->path, strlen(p->two->path));
Jeff King977db6b2017-03-30 14:26:05 -04005351 patch_id_add_string(&ctx, "diff--git");
5352 patch_id_add_string(&ctx, "a/");
5353 git_SHA1_Update(&ctx, p->one->path, len1);
5354 patch_id_add_string(&ctx, "b/");
5355 git_SHA1_Update(&ctx, p->two->path, len2);
5356
5357 if (p->one->mode == 0) {
5358 patch_id_add_string(&ctx, "newfilemode");
5359 patch_id_add_mode(&ctx, p->two->mode);
5360 patch_id_add_string(&ctx, "---/dev/null");
5361 patch_id_add_string(&ctx, "+++b/");
5362 git_SHA1_Update(&ctx, p->two->path, len2);
5363 } else if (p->two->mode == 0) {
5364 patch_id_add_string(&ctx, "deletedfilemode");
5365 patch_id_add_mode(&ctx, p->one->mode);
5366 patch_id_add_string(&ctx, "---a/");
5367 git_SHA1_Update(&ctx, p->one->path, len1);
5368 patch_id_add_string(&ctx, "+++/dev/null");
5369 } else {
5370 patch_id_add_string(&ctx, "---a/");
5371 git_SHA1_Update(&ctx, p->one->path, len1);
5372 patch_id_add_string(&ctx, "+++b/");
5373 git_SHA1_Update(&ctx, p->two->path, len2);
5374 }
Johannes Schindelinfcb3d0a2006-06-25 03:51:08 +02005375
Kevin Willford3e8e32c2016-07-29 12:19:19 -04005376 if (diff_header_only)
5377 continue;
5378
5379 if (fill_mmfile(&mf1, p->one) < 0 ||
5380 fill_mmfile(&mf2, p->two) < 0)
5381 return error("unable to read files to diff");
5382
Clemens Buchacher34597c12010-08-15 09:20:43 +02005383 if (diff_filespec_is_binary(p->one) ||
5384 diff_filespec_is_binary(p->two)) {
brian m. carlsona0d12c42016-06-24 23:09:23 +00005385 git_SHA1_Update(&ctx, oid_to_hex(&p->one->oid),
Brandon Williamsbd25f282017-05-30 10:30:54 -07005386 GIT_SHA1_HEXSZ);
brian m. carlsona0d12c42016-06-24 23:09:23 +00005387 git_SHA1_Update(&ctx, oid_to_hex(&p->two->oid),
Brandon Williamsbd25f282017-05-30 10:30:54 -07005388 GIT_SHA1_HEXSZ);
Clemens Buchacher34597c12010-08-15 09:20:43 +02005389 continue;
5390 }
5391
René Scharfe582aa002010-05-02 15:04:41 +02005392 xpp.flags = 0;
Johannes Schindelinfcb3d0a2006-06-25 03:51:08 +02005393 xecfg.ctxlen = 3;
Clemens Buchacherad14b452010-09-19 11:59:28 +02005394 xecfg.flags = 0;
Jeff King3efb9882015-09-24 19:12:23 -04005395 if (xdi_diff_outf(&mf1, &mf2, patch_id_consume, &data,
5396 &xpp, &xecfg))
5397 return error("unable to generate patch-id diff for %s",
5398 p->one->path);
Johannes Schindelinfcb3d0a2006-06-25 03:51:08 +02005399 }
5400
Brandon Williamsbd25f282017-05-30 10:30:54 -07005401 git_SHA1_Final(oid->hash, &ctx);
Johannes Schindelinfcb3d0a2006-06-25 03:51:08 +02005402 return 0;
5403}
5404
Brandon Williamsbd25f282017-05-30 10:30:54 -07005405int diff_flush_patch_id(struct diff_options *options, struct object_id *oid, int diff_header_only)
Johannes Schindelinfcb3d0a2006-06-25 03:51:08 +02005406{
5407 struct diff_queue_struct *q = &diff_queued_diff;
5408 int i;
Brandon Williamsbd25f282017-05-30 10:30:54 -07005409 int result = diff_get_patch_id(options, oid, diff_header_only);
Johannes Schindelinfcb3d0a2006-06-25 03:51:08 +02005410
5411 for (i = 0; i < q->nr; i++)
5412 diff_free_filepair(q->queue[i]);
5413
5414 free(q->queue);
Bo Yang9ca5df92010-05-06 21:52:27 -07005415 DIFF_QUEUE_CLEAR(q);
Johannes Schindelinfcb3d0a2006-06-25 03:51:08 +02005416
5417 return result;
5418}
5419
Timo Hirvonen946c3782006-06-27 15:09:17 +03005420static int is_summary_empty(const struct diff_queue_struct *q)
5421{
5422 int i;
5423
5424 for (i = 0; i < q->nr; i++) {
5425 const struct diff_filepair *p = q->queue[i];
5426
5427 switch (p->status) {
5428 case DIFF_STATUS_DELETED:
5429 case DIFF_STATUS_ADDED:
5430 case DIFF_STATUS_COPIED:
5431 case DIFF_STATUS_RENAMED:
5432 return 0;
5433 default:
5434 if (p->score)
5435 return 0;
5436 if (p->one->mode && p->two->mode &&
5437 p->one->mode != p->two->mode)
5438 return 0;
5439 break;
5440 }
5441 }
5442 return 1;
5443}
5444
Junio C Hamanof31027c2011-01-06 13:50:06 -08005445static const char rename_limit_warning[] =
Vasco Almeidadb424972016-10-17 13:15:29 +00005446N_("inexact rename detection was skipped due to too many files.");
Junio C Hamanof31027c2011-01-06 13:50:06 -08005447
5448static const char degrade_cc_to_c_warning[] =
Vasco Almeidadb424972016-10-17 13:15:29 +00005449N_("only found copies from modified paths due to too many files.");
Junio C Hamanof31027c2011-01-06 13:50:06 -08005450
5451static const char rename_limit_advice[] =
Vasco Almeidadb424972016-10-17 13:15:29 +00005452N_("you may want to set your %s variable to at least "
5453 "%d and retry the command.");
Junio C Hamanof31027c2011-01-06 13:50:06 -08005454
5455void diff_warn_rename_limit(const char *varname, int needed, int degraded_cc)
5456{
5457 if (degraded_cc)
Vasco Almeidadb424972016-10-17 13:15:29 +00005458 warning(_(degrade_cc_to_c_warning));
Junio C Hamanof31027c2011-01-06 13:50:06 -08005459 else if (needed)
Vasco Almeidadb424972016-10-17 13:15:29 +00005460 warning(_(rename_limit_warning));
Junio C Hamanof31027c2011-01-06 13:50:06 -08005461 else
5462 return;
Elijah Newren9f7e4bf2017-11-13 12:15:59 -08005463 if (0 < needed)
Vasco Almeidadb424972016-10-17 13:15:29 +00005464 warning(_(rename_limit_advice), varname, needed);
Junio C Hamanof31027c2011-01-06 13:50:06 -08005465}
5466
Stefan Bellerec331502017-06-29 17:06:48 -07005467static void diff_flush_patch_all_file_pairs(struct diff_options *o)
5468{
5469 int i;
Stefan Bellere6e045f2017-06-29 17:07:06 -07005470 static struct emitted_diff_symbols esm = EMITTED_DIFF_SYMBOLS_INIT;
Stefan Bellerec331502017-06-29 17:06:48 -07005471 struct diff_queue_struct *q = &diff_queued_diff;
Stefan Beller091f8e22017-06-29 17:06:53 -07005472
5473 if (WSEH_NEW & WS_RULE_MASK)
5474 die("BUG: WS rules bit mask overlaps with diff symbol flags");
5475
Stefan Beller2e2d5ac2017-06-30 13:53:07 -07005476 if (o->color_moved)
5477 o->emitted_symbols = &esm;
Stefan Bellere6e045f2017-06-29 17:07:06 -07005478
Stefan Bellerec331502017-06-29 17:06:48 -07005479 for (i = 0; i < q->nr; i++) {
5480 struct diff_filepair *p = q->queue[i];
5481 if (check_pair_status(p))
5482 diff_flush_patch(p, o);
5483 }
Stefan Bellere6e045f2017-06-29 17:07:06 -07005484
5485 if (o->emitted_symbols) {
Stefan Beller2e2d5ac2017-06-30 13:53:07 -07005486 if (o->color_moved) {
5487 struct hashmap add_lines, del_lines;
5488
5489 hashmap_init(&del_lines,
5490 (hashmap_cmp_fn)moved_entry_cmp, o, 0);
5491 hashmap_init(&add_lines,
5492 (hashmap_cmp_fn)moved_entry_cmp, o, 0);
5493
5494 add_lines_to_move_detection(o, &add_lines, &del_lines);
5495 mark_color_as_moved(o, &add_lines, &del_lines);
Stefan Beller86b452e2017-06-30 13:53:09 -07005496 if (o->color_moved == COLOR_MOVED_ZEBRA_DIM)
5497 dim_moved_lines(o);
Stefan Beller2e2d5ac2017-06-30 13:53:07 -07005498
5499 hashmap_free(&add_lines, 0);
5500 hashmap_free(&del_lines, 0);
5501 }
5502
Stefan Bellere6e045f2017-06-29 17:07:06 -07005503 for (i = 0; i < esm.nr; i++)
5504 emit_diff_symbol_from_struct(o, &esm.buf[i]);
5505
5506 for (i = 0; i < esm.nr; i++)
5507 free((void *)esm.buf[i].line);
5508 }
5509 esm.nr = 0;
Stefan Bellerec331502017-06-29 17:06:48 -07005510}
5511
Junio C Hamano6973dca2006-04-21 23:57:45 -07005512void diff_flush(struct diff_options *options)
5513{
5514 struct diff_queue_struct *q = &diff_queued_diff;
Timo Hirvonenc6744342006-06-24 20:21:53 +03005515 int i, output_format = options->output_format;
Timo Hirvonen946c3782006-06-27 15:09:17 +03005516 int separator = 0;
Johan Herland1c57a622011-04-29 11:36:21 +02005517 int dirstat_by_line = 0;
Junio C Hamano6973dca2006-04-21 23:57:45 -07005518
Timo Hirvonenc6744342006-06-24 20:21:53 +03005519 /*
5520 * Order: raw, stat, summary, patch
5521 * or: name/name-status/checkdiff (other bits clear)
5522 */
Timo Hirvonen946c3782006-06-27 15:09:17 +03005523 if (!q->nr)
5524 goto free_queue;
Junio C Hamano6973dca2006-04-21 23:57:45 -07005525
Timo Hirvonenc6744342006-06-24 20:21:53 +03005526 if (output_format & (DIFF_FORMAT_RAW |
5527 DIFF_FORMAT_NAME |
5528 DIFF_FORMAT_NAME_STATUS |
5529 DIFF_FORMAT_CHECKDIFF)) {
Junio C Hamano6973dca2006-04-21 23:57:45 -07005530 for (i = 0; i < q->nr; i++) {
5531 struct diff_filepair *p = q->queue[i];
Timo Hirvonenc6744342006-06-24 20:21:53 +03005532 if (check_pair_status(p))
5533 flush_one_pair(p, options);
Junio C Hamano6973dca2006-04-21 23:57:45 -07005534 }
Timo Hirvonen946c3782006-06-27 15:09:17 +03005535 separator++;
Junio C Hamano6973dca2006-04-21 23:57:45 -07005536 }
Timo Hirvonenc6744342006-06-24 20:21:53 +03005537
Brandon Williams0d1e0e72017-10-31 11:19:11 -07005538 if (output_format & DIFF_FORMAT_DIRSTAT && options->flags.dirstat_by_line)
Johan Herland1c57a622011-04-29 11:36:21 +02005539 dirstat_by_line = 1;
5540
5541 if (output_format & (DIFF_FORMAT_DIFFSTAT|DIFF_FORMAT_SHORTSTAT|DIFF_FORMAT_NUMSTAT) ||
5542 dirstat_by_line) {
Timo Hirvonen5e2b0632006-06-25 14:28:19 +03005543 struct diffstat_t diffstat;
Timo Hirvonenc6744342006-06-24 20:21:53 +03005544
Timo Hirvonen5e2b0632006-06-25 14:28:19 +03005545 memset(&diffstat, 0, sizeof(struct diffstat_t));
Junio C Hamano6973dca2006-04-21 23:57:45 -07005546 for (i = 0; i < q->nr; i++) {
5547 struct diff_filepair *p = q->queue[i];
Timo Hirvonenc6744342006-06-24 20:21:53 +03005548 if (check_pair_status(p))
Timo Hirvonen5e2b0632006-06-25 14:28:19 +03005549 diff_flush_stat(p, options, &diffstat);
Junio C Hamano6973dca2006-04-21 23:57:45 -07005550 }
Junio C Hamano74e2abe2006-10-12 03:01:00 -07005551 if (output_format & DIFF_FORMAT_NUMSTAT)
5552 show_numstat(&diffstat, options);
5553 if (output_format & DIFF_FORMAT_DIFFSTAT)
5554 show_stats(&diffstat, options);
Junio C Hamanof6046522007-12-11 23:46:30 -08005555 if (output_format & DIFF_FORMAT_SHORTSTAT)
Daniel Barkalowc0c77732008-03-09 22:43:39 -04005556 show_shortstats(&diffstat, options);
Mårten Kongstadab273892015-03-02 16:05:39 +01005557 if (output_format & DIFF_FORMAT_DIRSTAT && dirstat_by_line)
Johan Herland1c57a622011-04-29 11:36:21 +02005558 show_dirstat_by_line(&diffstat, options);
Junio C Hamanof6046522007-12-11 23:46:30 -08005559 free_diffstat_info(&diffstat);
Junio C Hamano3969cf72006-06-27 15:08:19 -07005560 separator++;
Junio C Hamano6973dca2006-04-21 23:57:45 -07005561 }
Johan Herland1c57a622011-04-29 11:36:21 +02005562 if ((output_format & DIFF_FORMAT_DIRSTAT) && !dirstat_by_line)
Junio C Hamanoc04a7152008-02-12 17:06:58 -08005563 show_dirstat(options);
Junio C Hamano6973dca2006-04-21 23:57:45 -07005564
Timo Hirvonen946c3782006-06-27 15:09:17 +03005565 if (output_format & DIFF_FORMAT_SUMMARY && !is_summary_empty(q)) {
Bo Yang7be57612010-05-26 15:23:54 +08005566 for (i = 0; i < q->nr; i++) {
5567 diff_summary(options, q->queue[i]);
5568 }
Junio C Hamano3969cf72006-06-27 15:08:19 -07005569 separator++;
Sean4bbd2612006-05-14 08:13:49 -04005570 }
5571
Larry D'Anna6977c252010-02-16 01:55:21 -05005572 if (output_format & DIFF_FORMAT_NO_OUTPUT &&
Brandon Williams0d1e0e72017-10-31 11:19:11 -07005573 options->flags.exit_with_status &&
5574 options->flags.diff_from_contents) {
Larry D'Anna6977c252010-02-16 01:55:21 -05005575 /*
5576 * run diff_flush_patch for the exit status. setting
Ondřej Bílka749f7632013-07-22 23:02:23 +02005577 * options->file to /dev/null should be safe, because we
Larry D'Anna6977c252010-02-16 01:55:21 -05005578 * aren't supposed to produce any output anyway.
5579 */
5580 if (options->close_file)
5581 fclose(options->file);
Nguyễn Thái Ngọc Duy23a9e072017-05-03 17:16:46 +07005582 options->file = xfopen("/dev/null", "w");
Larry D'Anna6977c252010-02-16 01:55:21 -05005583 options->close_file = 1;
Stefan Beller2e2d5ac2017-06-30 13:53:07 -07005584 options->color_moved = 0;
Larry D'Anna6977c252010-02-16 01:55:21 -05005585 for (i = 0; i < q->nr; i++) {
5586 struct diff_filepair *p = q->queue[i];
5587 if (check_pair_status(p))
5588 diff_flush_patch(p, options);
5589 if (options->found_changes)
5590 break;
5591 }
5592 }
5593
Timo Hirvonenc6744342006-06-24 20:21:53 +03005594 if (output_format & DIFF_FORMAT_PATCH) {
Timo Hirvonen946c3782006-06-27 15:09:17 +03005595 if (separator) {
Stefan Beller091f8e22017-06-29 17:06:53 -07005596 emit_diff_symbol(options, DIFF_SYMBOL_SEPARATOR, NULL, 0, 0);
Stefan Beller30b7e1e2017-06-29 17:07:04 -07005597 if (options->stat_sep)
Timo Hirvonen946c3782006-06-27 15:09:17 +03005598 /* attach patch instead of inline */
Stefan Beller30b7e1e2017-06-29 17:07:04 -07005599 emit_diff_symbol(options, DIFF_SYMBOL_STAT_SEP,
5600 NULL, 0, 0);
Timo Hirvonenc6744342006-06-24 20:21:53 +03005601 }
5602
Stefan Bellerec331502017-06-29 17:06:48 -07005603 diff_flush_patch_all_file_pairs(options);
Timo Hirvonenc6744342006-06-24 20:21:53 +03005604 }
5605
Jeff King04245582006-09-07 02:35:42 -04005606 if (output_format & DIFF_FORMAT_CALLBACK)
5607 options->format_callback(q, options, options->format_callback_data);
5608
Timo Hirvonenc6744342006-06-24 20:21:53 +03005609 for (i = 0; i < q->nr; i++)
5610 diff_free_filepair(q->queue[i]);
Timo Hirvonen946c3782006-06-27 15:09:17 +03005611free_queue:
Junio C Hamano6973dca2006-04-21 23:57:45 -07005612 free(q->queue);
Bo Yang9ca5df92010-05-06 21:52:27 -07005613 DIFF_QUEUE_CLEAR(q);
Daniel Barkalowc0c77732008-03-09 22:43:39 -04005614 if (options->close_file)
5615 fclose(options->file);
Junio C Hamanof2451942009-05-22 12:45:29 -07005616
5617 /*
Jim Meyering97bf2a02009-08-30 22:27:02 +02005618 * Report the content-level differences with HAS_CHANGES;
Junio C Hamanof2451942009-05-22 12:45:29 -07005619 * diff_addremove/diff_change does not set the bit when
5620 * DIFF_FROM_CONTENTS is in effect (e.g. with -w).
5621 */
Brandon Williams0d1e0e72017-10-31 11:19:11 -07005622 if (options->flags.diff_from_contents) {
Junio C Hamanof2451942009-05-22 12:45:29 -07005623 if (options->found_changes)
Brandon Williams0d1e0e72017-10-31 11:19:11 -07005624 options->flags.has_changes = 1;
Junio C Hamanof2451942009-05-22 12:45:29 -07005625 else
Brandon Williams0d1e0e72017-10-31 11:19:11 -07005626 options->flags.has_changes = 0;
Junio C Hamanof2451942009-05-22 12:45:29 -07005627 }
Junio C Hamano6973dca2006-04-21 23:57:45 -07005628}
5629
Junio C Hamano08578fa2013-07-17 15:09:34 -07005630static int match_filter(const struct diff_options *options, const struct diff_filepair *p)
5631{
5632 return (((p->status == DIFF_STATUS_MODIFIED) &&
5633 ((p->score &&
Junio C Hamano1ecc1cb2013-07-17 15:05:46 -07005634 filter_bit_tst(DIFF_STATUS_FILTER_BROKEN, options)) ||
Junio C Hamano08578fa2013-07-17 15:09:34 -07005635 (!p->score &&
Junio C Hamano1ecc1cb2013-07-17 15:05:46 -07005636 filter_bit_tst(DIFF_STATUS_MODIFIED, options)))) ||
Junio C Hamano08578fa2013-07-17 15:09:34 -07005637 ((p->status != DIFF_STATUS_MODIFIED) &&
Junio C Hamano1ecc1cb2013-07-17 15:05:46 -07005638 filter_bit_tst(p->status, options)));
Junio C Hamano08578fa2013-07-17 15:09:34 -07005639}
5640
Junio C Hamano949226f2013-07-17 14:19:24 -07005641static void diffcore_apply_filter(struct diff_options *options)
Junio C Hamano6973dca2006-04-21 23:57:45 -07005642{
5643 int i;
5644 struct diff_queue_struct *q = &diff_queued_diff;
5645 struct diff_queue_struct outq;
Junio C Hamano949226f2013-07-17 14:19:24 -07005646
Bo Yang9ca5df92010-05-06 21:52:27 -07005647 DIFF_QUEUE_CLEAR(&outq);
Junio C Hamano6973dca2006-04-21 23:57:45 -07005648
Junio C Hamano1ecc1cb2013-07-17 15:05:46 -07005649 if (!options->filter)
Junio C Hamano6973dca2006-04-21 23:57:45 -07005650 return;
5651
Junio C Hamano1ecc1cb2013-07-17 15:05:46 -07005652 if (filter_bit_tst(DIFF_STATUS_FILTER_AON, options)) {
Junio C Hamano6973dca2006-04-21 23:57:45 -07005653 int found;
5654 for (i = found = 0; !found && i < q->nr; i++) {
Junio C Hamano08578fa2013-07-17 15:09:34 -07005655 if (match_filter(options, q->queue[i]))
Junio C Hamano6973dca2006-04-21 23:57:45 -07005656 found++;
5657 }
5658 if (found)
5659 return;
5660
5661 /* otherwise we will clear the whole queue
5662 * by copying the empty outq at the end of this
5663 * function, but first clear the current entries
5664 * in the queue.
5665 */
5666 for (i = 0; i < q->nr; i++)
5667 diff_free_filepair(q->queue[i]);
5668 }
5669 else {
5670 /* Only the matching ones */
5671 for (i = 0; i < q->nr; i++) {
5672 struct diff_filepair *p = q->queue[i];
Junio C Hamano08578fa2013-07-17 15:09:34 -07005673 if (match_filter(options, p))
Junio C Hamano6973dca2006-04-21 23:57:45 -07005674 diff_q(&outq, p);
5675 else
5676 diff_free_filepair(p);
5677 }
5678 }
5679 free(q->queue);
5680 *q = outq;
5681}
5682
Sven Verdoolaege57011152007-09-08 12:30:22 +02005683/* Check whether two filespecs with the same mode and size are identical */
5684static int diff_filespec_is_identical(struct diff_filespec *one,
5685 struct diff_filespec *two)
5686{
Junio C Hamano2b459b42008-03-02 00:07:59 -08005687 if (S_ISGITLINK(one->mode))
5688 return 0;
Sven Verdoolaege57011152007-09-08 12:30:22 +02005689 if (diff_populate_filespec(one, 0))
5690 return 0;
5691 if (diff_populate_filespec(two, 0))
5692 return 0;
5693 return !memcmp(one->data, two->data, one->size);
5694}
5695
Nguyễn Thái Ngọc Duyfceb9072014-01-25 13:46:49 +07005696static int diff_filespec_check_stat_unmatch(struct diff_filepair *p)
5697{
Nguyễn Thái Ngọc Duyf34b2052014-01-25 13:46:50 +07005698 if (p->done_skip_stat_unmatch)
5699 return p->skip_stat_unmatch_result;
5700
5701 p->done_skip_stat_unmatch = 1;
5702 p->skip_stat_unmatch_result = 0;
Nguyễn Thái Ngọc Duyfceb9072014-01-25 13:46:49 +07005703 /*
5704 * 1. Entries that come from stat info dirtiness
5705 * always have both sides (iow, not create/delete),
5706 * one side of the object name is unknown, with
5707 * the same mode and size. Keep the ones that
5708 * do not match these criteria. They have real
5709 * differences.
5710 *
5711 * 2. At this point, the file is known to be modified,
5712 * with the same mode and size, and the object
5713 * name of one side is unknown. Need to inspect
5714 * the identical contents.
5715 */
5716 if (!DIFF_FILE_VALID(p->one) || /* (1) */
5717 !DIFF_FILE_VALID(p->two) ||
brian m. carlson41c95602016-06-24 23:09:24 +00005718 (p->one->oid_valid && p->two->oid_valid) ||
Nguyễn Thái Ngọc Duyfceb9072014-01-25 13:46:49 +07005719 (p->one->mode != p->two->mode) ||
Nguyễn Thái Ngọc Duy8e5dd3d2014-08-16 10:08:04 +07005720 diff_populate_filespec(p->one, CHECK_SIZE_ONLY) ||
5721 diff_populate_filespec(p->two, CHECK_SIZE_ONLY) ||
Nguyễn Thái Ngọc Duyfceb9072014-01-25 13:46:49 +07005722 (p->one->size != p->two->size) ||
5723 !diff_filespec_is_identical(p->one, p->two)) /* (2) */
Nguyễn Thái Ngọc Duyf34b2052014-01-25 13:46:50 +07005724 p->skip_stat_unmatch_result = 1;
5725 return p->skip_stat_unmatch_result;
Nguyễn Thái Ngọc Duyfceb9072014-01-25 13:46:49 +07005726}
5727
Junio C Hamanofb132272007-08-03 13:33:31 -07005728static void diffcore_skip_stat_unmatch(struct diff_options *diffopt)
5729{
5730 int i;
5731 struct diff_queue_struct *q = &diff_queued_diff;
5732 struct diff_queue_struct outq;
Bo Yang9ca5df92010-05-06 21:52:27 -07005733 DIFF_QUEUE_CLEAR(&outq);
Junio C Hamanofb132272007-08-03 13:33:31 -07005734
5735 for (i = 0; i < q->nr; i++) {
5736 struct diff_filepair *p = q->queue[i];
5737
Nguyễn Thái Ngọc Duyfceb9072014-01-25 13:46:49 +07005738 if (diff_filespec_check_stat_unmatch(p))
Junio C Hamanofb132272007-08-03 13:33:31 -07005739 diff_q(&outq, p);
5740 else {
5741 /*
5742 * The caller can subtract 1 from skip_stat_unmatch
5743 * to determine how many paths were dirty only
5744 * due to stat info mismatch.
5745 */
Brandon Williams0d1e0e72017-10-31 11:19:11 -07005746 if (!diffopt->flags.no_index)
René Scharfe6d2d9e82007-08-15 00:41:00 +02005747 diffopt->skip_stat_unmatch++;
Junio C Hamanofb132272007-08-03 13:33:31 -07005748 diff_free_filepair(p);
5749 }
5750 }
5751 free(q->queue);
5752 *q = outq;
5753}
5754
Junio C Hamano730f7282009-09-20 00:03:39 -07005755static int diffnamecmp(const void *a_, const void *b_)
5756{
5757 const struct diff_filepair *a = *((const struct diff_filepair **)a_);
5758 const struct diff_filepair *b = *((const struct diff_filepair **)b_);
5759 const char *name_a, *name_b;
5760
5761 name_a = a->one ? a->one->path : a->two->path;
5762 name_b = b->one ? b->one->path : b->two->path;
5763 return strcmp(name_a, name_b);
5764}
5765
5766void diffcore_fix_diff_index(struct diff_options *options)
5767{
5768 struct diff_queue_struct *q = &diff_queued_diff;
René Scharfe9ed0d8d2016-09-29 17:27:31 +02005769 QSORT(q->queue, q->nr, diffnamecmp);
Junio C Hamano730f7282009-09-20 00:03:39 -07005770}
5771
Junio C Hamano6973dca2006-04-21 23:57:45 -07005772void diffcore_std(struct diff_options *options)
5773{
Kirill Smelkov7195fbf2014-02-24 20:21:51 +04005774 /* NOTE please keep the following in sync with diff_tree_combined() */
Junio C Hamano9d865352008-09-06 19:09:16 -07005775 if (options->skip_stat_unmatch)
Junio C Hamanofb132272007-08-03 13:33:31 -07005776 diffcore_skip_stat_unmatch(options);
Junio C Hamano44c48a92010-08-13 12:17:45 -07005777 if (!options->found_follow) {
5778 /* See try_to_follow_renames() in tree-diff.c */
5779 if (options->break_opt != -1)
5780 diffcore_break(options->break_opt);
5781 if (options->detect_rename)
5782 diffcore_rename(options);
5783 if (options->break_opt != -1)
5784 diffcore_merge_broken();
5785 }
Junio C Hamano6973dca2006-04-21 23:57:45 -07005786 if (options->pickaxe)
Junio C Hamano382f0132010-08-31 13:44:39 -07005787 diffcore_pickaxe(options);
Junio C Hamano6973dca2006-04-21 23:57:45 -07005788 if (options->orderfile)
5789 diffcore_order(options->orderfile);
Junio C Hamano44c48a92010-08-13 12:17:45 -07005790 if (!options->found_follow)
5791 /* See try_to_follow_renames() in tree-diff.c */
5792 diff_resolve_rename_copy();
Junio C Hamano949226f2013-07-17 14:19:24 -07005793 diffcore_apply_filter(options);
Junio C Hamano68aacb22007-03-14 11:12:13 -07005794
Brandon Williams0d1e0e72017-10-31 11:19:11 -07005795 if (diff_queued_diff.nr && !options->flags.diff_from_contents)
5796 options->flags.has_changes = 1;
Pierre Habouzit8f67f8a2007-11-10 20:05:14 +01005797 else
Brandon Williams0d1e0e72017-10-31 11:19:11 -07005798 options->flags.has_changes = 0;
Bo Yang1da61752010-05-06 21:52:28 -07005799
Junio C Hamano44c48a92010-08-13 12:17:45 -07005800 options->found_follow = 0;
Junio C Hamano6973dca2006-04-21 23:57:45 -07005801}
5802
Junio C Hamanoda31b352007-12-13 23:40:27 -08005803int diff_result_code(struct diff_options *opt, int status)
5804{
5805 int result = 0;
Junio C Hamanof31027c2011-01-06 13:50:06 -08005806
Max Nanasyc9fc4412013-03-21 12:53:38 -07005807 diff_warn_rename_limit("diff.renameLimit",
Junio C Hamanof31027c2011-01-06 13:50:06 -08005808 opt->needed_rename_limit,
5809 opt->degraded_cc_to_c);
Brandon Williams0d1e0e72017-10-31 11:19:11 -07005810 if (!opt->flags.exit_with_status &&
Junio C Hamanoda31b352007-12-13 23:40:27 -08005811 !(opt->output_format & DIFF_FORMAT_CHECKDIFF))
5812 return status;
Brandon Williams0d1e0e72017-10-31 11:19:11 -07005813 if (opt->flags.exit_with_status &&
5814 opt->flags.has_changes)
Junio C Hamanoda31b352007-12-13 23:40:27 -08005815 result |= 01;
5816 if ((opt->output_format & DIFF_FORMAT_CHECKDIFF) &&
Brandon Williams0d1e0e72017-10-31 11:19:11 -07005817 opt->flags.check_failed)
Junio C Hamanoda31b352007-12-13 23:40:27 -08005818 result |= 02;
5819 return result;
5820}
Junio C Hamano6973dca2006-04-21 23:57:45 -07005821
Junio C Hamano28b92642011-05-31 09:14:17 -07005822int diff_can_quit_early(struct diff_options *opt)
5823{
Brandon Williams0d1e0e72017-10-31 11:19:11 -07005824 return (opt->flags.quick &&
Junio C Hamano28b92642011-05-31 09:14:17 -07005825 !opt->filter &&
Brandon Williams0d1e0e72017-10-31 11:19:11 -07005826 opt->flags.has_changes);
Junio C Hamano28b92642011-05-31 09:14:17 -07005827}
5828
Jens Lehmannaee9c7d2010-08-06 00:39:25 +02005829/*
5830 * Shall changes to this submodule be ignored?
5831 *
5832 * Submodule changes can be configured to be ignored separately for each path,
5833 * but that configuration can be overridden from the command line.
5834 */
5835static int is_submodule_ignored(const char *path, struct diff_options *options)
5836{
5837 int ignored = 0;
Brandon Williams02f2f562017-10-31 11:19:05 -07005838 struct diff_flags orig_flags = options->flags;
Brandon Williams0d1e0e72017-10-31 11:19:11 -07005839 if (!options->flags.override_submodule_config)
Jens Lehmannaee9c7d2010-08-06 00:39:25 +02005840 set_diffopt_flags_from_submodule_config(options, path);
Brandon Williams0d1e0e72017-10-31 11:19:11 -07005841 if (options->flags.ignore_submodules)
Jens Lehmannaee9c7d2010-08-06 00:39:25 +02005842 ignored = 1;
5843 options->flags = orig_flags;
5844 return ignored;
5845}
5846
Junio C Hamano6973dca2006-04-21 23:57:45 -07005847void diff_addremove(struct diff_options *options,
5848 int addremove, unsigned mode,
Brandon Williamsc26022e2017-05-30 10:30:47 -07005849 const struct object_id *oid,
5850 int oid_valid,
Jens Lehmanne3d42c42010-01-18 21:26:18 +01005851 const char *concatpath, unsigned dirty_submodule)
Junio C Hamano6973dca2006-04-21 23:57:45 -07005852{
Junio C Hamano6973dca2006-04-21 23:57:45 -07005853 struct diff_filespec *one, *two;
5854
Jens Lehmannaee9c7d2010-08-06 00:39:25 +02005855 if (S_ISGITLINK(mode) && is_submodule_ignored(concatpath, options))
Johannes Schindelin50fd9bd2008-05-14 18:03:31 +01005856 return;
5857
Junio C Hamano6973dca2006-04-21 23:57:45 -07005858 /* This may look odd, but it is a preparation for
5859 * feeding "there are unchanged files which should
5860 * not produce diffs, but when you are doing copy
5861 * detection you would need them, so here they are"
5862 * entries to the diff-core. They will be prefixed
5863 * with something like '=' or '*' (I haven't decided
5864 * which but should not make any difference).
Junio C Hamanoa6080a02007-06-07 00:04:01 -07005865 * Feeding the same new and old to diff_change()
Junio C Hamano6973dca2006-04-21 23:57:45 -07005866 * also has the same effect.
5867 * Before the final output happens, they are pruned after
5868 * merged into rename/copy pairs as appropriate.
5869 */
Brandon Williams0d1e0e72017-10-31 11:19:11 -07005870 if (options->flags.reverse_diff)
Junio C Hamano6973dca2006-04-21 23:57:45 -07005871 addremove = (addremove == '+' ? '-' :
5872 addremove == '-' ? '+' : addremove);
5873
Junio C Hamanocd676a52008-02-12 14:26:02 -08005874 if (options->prefix &&
5875 strncmp(concatpath, options->prefix, options->prefix_length))
5876 return;
5877
Junio C Hamano6973dca2006-04-21 23:57:45 -07005878 one = alloc_filespec(concatpath);
5879 two = alloc_filespec(concatpath);
5880
5881 if (addremove != '+')
Brandon Williamsf9704c22017-05-30 10:30:50 -07005882 fill_filespec(one, oid, oid_valid, mode);
Jens Lehmanne3d42c42010-01-18 21:26:18 +01005883 if (addremove != '-') {
Brandon Williamsf9704c22017-05-30 10:30:50 -07005884 fill_filespec(two, oid, oid_valid, mode);
Jens Lehmanne3d42c42010-01-18 21:26:18 +01005885 two->dirty_submodule = dirty_submodule;
5886 }
Junio C Hamano6973dca2006-04-21 23:57:45 -07005887
5888 diff_queue(&diff_queued_diff, one, two);
Brandon Williams0d1e0e72017-10-31 11:19:11 -07005889 if (!options->flags.diff_from_contents)
5890 options->flags.has_changes = 1;
Junio C Hamano6973dca2006-04-21 23:57:45 -07005891}
5892
5893void diff_change(struct diff_options *options,
5894 unsigned old_mode, unsigned new_mode,
Brandon Williams94a00972017-05-30 10:30:49 -07005895 const struct object_id *old_oid,
5896 const struct object_id *new_oid,
5897 int old_oid_valid, int new_oid_valid,
Jens Lehmanne3d42c42010-01-18 21:26:18 +01005898 const char *concatpath,
5899 unsigned old_dirty_submodule, unsigned new_dirty_submodule)
Junio C Hamano6973dca2006-04-21 23:57:45 -07005900{
Junio C Hamano6973dca2006-04-21 23:57:45 -07005901 struct diff_filespec *one, *two;
Nguyễn Thái Ngọc Duyf34b2052014-01-25 13:46:50 +07005902 struct diff_filepair *p;
Junio C Hamano6973dca2006-04-21 23:57:45 -07005903
Jens Lehmannaee9c7d2010-08-06 00:39:25 +02005904 if (S_ISGITLINK(old_mode) && S_ISGITLINK(new_mode) &&
5905 is_submodule_ignored(concatpath, options))
Johannes Schindelin50fd9bd2008-05-14 18:03:31 +01005906 return;
5907
Brandon Williams0d1e0e72017-10-31 11:19:11 -07005908 if (options->flags.reverse_diff) {
René Scharfe35d803b2017-01-28 22:40:58 +01005909 SWAP(old_mode, new_mode);
Brandon Williams94a00972017-05-30 10:30:49 -07005910 SWAP(old_oid, new_oid);
5911 SWAP(old_oid_valid, new_oid_valid);
René Scharfe35d803b2017-01-28 22:40:58 +01005912 SWAP(old_dirty_submodule, new_dirty_submodule);
Junio C Hamano6973dca2006-04-21 23:57:45 -07005913 }
Junio C Hamanocd676a52008-02-12 14:26:02 -08005914
5915 if (options->prefix &&
5916 strncmp(concatpath, options->prefix, options->prefix_length))
5917 return;
5918
Junio C Hamano6973dca2006-04-21 23:57:45 -07005919 one = alloc_filespec(concatpath);
5920 two = alloc_filespec(concatpath);
Brandon Williamsf9704c22017-05-30 10:30:50 -07005921 fill_filespec(one, old_oid, old_oid_valid, old_mode);
5922 fill_filespec(two, new_oid, new_oid_valid, new_mode);
Jens Lehmanne3d42c42010-01-18 21:26:18 +01005923 one->dirty_submodule = old_dirty_submodule;
5924 two->dirty_submodule = new_dirty_submodule;
Nguyễn Thái Ngọc Duyf34b2052014-01-25 13:46:50 +07005925 p = diff_queue(&diff_queued_diff, one, two);
Junio C Hamano6973dca2006-04-21 23:57:45 -07005926
Brandon Williams0d1e0e72017-10-31 11:19:11 -07005927 if (options->flags.diff_from_contents)
Nguyễn Thái Ngọc Duyf34b2052014-01-25 13:46:50 +07005928 return;
5929
Brandon Williams0d1e0e72017-10-31 11:19:11 -07005930 if (options->flags.quick && options->skip_stat_unmatch &&
Nguyễn Thái Ngọc Duyf34b2052014-01-25 13:46:50 +07005931 !diff_filespec_check_stat_unmatch(p))
5932 return;
5933
Brandon Williams0d1e0e72017-10-31 11:19:11 -07005934 options->flags.has_changes = 1;
Junio C Hamano6973dca2006-04-21 23:57:45 -07005935}
5936
Junio C Hamanofa7b2902011-04-22 16:05:58 -07005937struct diff_filepair *diff_unmerge(struct diff_options *options, const char *path)
Junio C Hamano6973dca2006-04-21 23:57:45 -07005938{
Junio C Hamano76399c02011-04-22 15:55:55 -07005939 struct diff_filepair *pair;
Junio C Hamano6973dca2006-04-21 23:57:45 -07005940 struct diff_filespec *one, *two;
Junio C Hamanocd676a52008-02-12 14:26:02 -08005941
5942 if (options->prefix &&
5943 strncmp(path, options->prefix, options->prefix_length))
Junio C Hamano76399c02011-04-22 15:55:55 -07005944 return NULL;
Junio C Hamanocd676a52008-02-12 14:26:02 -08005945
Junio C Hamano6973dca2006-04-21 23:57:45 -07005946 one = alloc_filespec(path);
5947 two = alloc_filespec(path);
Junio C Hamano76399c02011-04-22 15:55:55 -07005948 pair = diff_queue(&diff_queued_diff, one, two);
5949 pair->is_unmerged = 1;
5950 return pair;
Junio C Hamano6973dca2006-04-21 23:57:45 -07005951}
Jeff King9cb92c32008-10-05 17:43:45 -04005952
5953static char *run_textconv(const char *pgm, struct diff_filespec *spec,
5954 size_t *outsize)
5955{
Jeff King479b0ae2009-01-22 00:59:56 -05005956 struct diff_tempfile *temp;
Jeff King9cb92c32008-10-05 17:43:45 -04005957 const char *argv[3];
5958 const char **arg = argv;
René Scharfed3180272014-08-19 21:09:35 +02005959 struct child_process child = CHILD_PROCESS_INIT;
Jeff King9cb92c32008-10-05 17:43:45 -04005960 struct strbuf buf = STRBUF_INIT;
Johannes Sixtda1fbed2010-03-30 19:36:03 +02005961 int err = 0;
Jeff King9cb92c32008-10-05 17:43:45 -04005962
Jeff King479b0ae2009-01-22 00:59:56 -05005963 temp = prepare_temp_file(spec->path, spec);
Jeff King9cb92c32008-10-05 17:43:45 -04005964 *arg++ = pgm;
Jeff King479b0ae2009-01-22 00:59:56 -05005965 *arg++ = temp->name;
Jeff King9cb92c32008-10-05 17:43:45 -04005966 *arg = NULL;
5967
Jeff King41a457e2009-12-30 06:01:09 -05005968 child.use_shell = 1;
Jeff King9cb92c32008-10-05 17:43:45 -04005969 child.argv = argv;
5970 child.out = -1;
Johannes Sixtda1fbed2010-03-30 19:36:03 +02005971 if (start_command(&child)) {
Jeff King479b0ae2009-01-22 00:59:56 -05005972 remove_tempfile();
Jeff King9cb92c32008-10-05 17:43:45 -04005973 return NULL;
5974 }
Johannes Sixtda1fbed2010-03-30 19:36:03 +02005975
5976 if (strbuf_read(&buf, child.out, 0) < 0)
5977 err = error("error reading from textconv command '%s'", pgm);
Jeff King70d70992009-12-30 04:02:53 -05005978 close(child.out);
Johannes Sixtda1fbed2010-03-30 19:36:03 +02005979
5980 if (finish_command(&child) || err) {
5981 strbuf_release(&buf);
5982 remove_tempfile();
5983 return NULL;
5984 }
Jeff King479b0ae2009-01-22 00:59:56 -05005985 remove_tempfile();
Jeff King9cb92c32008-10-05 17:43:45 -04005986
5987 return strbuf_detach(&buf, outsize);
5988}
Jeff King840383b2010-04-01 20:09:26 -04005989
Axel Bonneta788d7d2010-06-07 17:23:36 +02005990size_t fill_textconv(struct userdiff_driver *driver,
5991 struct diff_filespec *df,
5992 char **outbuf)
Jeff King840383b2010-04-01 20:09:26 -04005993{
5994 size_t size;
5995
Jeff Kinga64e6a42016-02-22 13:28:54 -05005996 if (!driver) {
Jeff King840383b2010-04-01 20:09:26 -04005997 if (!DIFF_FILE_VALID(df)) {
5998 *outbuf = "";
5999 return 0;
6000 }
6001 if (diff_populate_filespec(df, 0))
6002 die("unable to read files to diff");
6003 *outbuf = df->data;
6004 return df->size;
6005 }
6006
Jeff Kinga64e6a42016-02-22 13:28:54 -05006007 if (!driver->textconv)
6008 die("BUG: fill_textconv called with non-textconv driver");
6009
brian m. carlson41c95602016-06-24 23:09:24 +00006010 if (driver->textconv_cache && df->oid_valid) {
brian m. carlsona0d12c42016-06-24 23:09:23 +00006011 *outbuf = notes_cache_get(driver->textconv_cache,
brian m. carlson569aa372017-05-06 22:09:58 +00006012 &df->oid,
Jeff Kingd9bae1a2010-04-01 20:12:15 -04006013 &size);
6014 if (*outbuf)
6015 return size;
6016 }
6017
6018 *outbuf = run_textconv(driver->textconv, df, &size);
Jeff King840383b2010-04-01 20:09:26 -04006019 if (!*outbuf)
6020 die("unable to read files to diff");
Jeff Kingd9bae1a2010-04-01 20:12:15 -04006021
brian m. carlson41c95602016-06-24 23:09:24 +00006022 if (driver->textconv_cache && df->oid_valid) {
Jeff Kingd9bae1a2010-04-01 20:12:15 -04006023 /* ignore errors, as we might be in a readonly repository */
brian m. carlson569aa372017-05-06 22:09:58 +00006024 notes_cache_put(driver->textconv_cache, &df->oid, *outbuf,
Jeff Kingd9bae1a2010-04-01 20:12:15 -04006025 size);
6026 /*
6027 * we could save up changes and flush them all at the end,
6028 * but we would need an extra call after all diffing is done.
6029 * Since generating a cache entry is the slow path anyway,
6030 * this extra overhead probably isn't a big deal.
6031 */
6032 notes_cache_write(driver->textconv_cache);
6033 }
6034
Jeff King840383b2010-04-01 20:09:26 -04006035 return size;
6036}
Nguyễn Thái Ngọc Duy4914c962012-10-26 22:53:52 +07006037
Jeff Smith3a35cb22017-05-24 00:15:10 -05006038int textconv_object(const char *path,
6039 unsigned mode,
6040 const struct object_id *oid,
6041 int oid_valid,
6042 char **buf,
6043 unsigned long *buf_size)
6044{
6045 struct diff_filespec *df;
6046 struct userdiff_driver *textconv;
6047
6048 df = alloc_filespec(path);
Junio C Hamanoa6f38c12017-06-19 12:38:44 -07006049 fill_filespec(df, oid, oid_valid, mode);
Jeff Smith3a35cb22017-05-24 00:15:10 -05006050 textconv = get_textconv(df);
6051 if (!textconv) {
6052 free_filespec(df);
6053 return 0;
6054 }
6055
6056 *buf_size = fill_textconv(textconv, df, buf);
6057 free_filespec(df);
6058 return 1;
6059}
6060
Nguyễn Thái Ngọc Duy4914c962012-10-26 22:53:52 +07006061void setup_diff_pager(struct diff_options *opt)
6062{
6063 /*
6064 * If the user asked for our exit code, then either they want --quiet
6065 * or --exit-code. We should definitely not bother with a pager in the
6066 * former case, as we will generate no output. Since we still properly
6067 * report our exit code even when a pager is run, we _could_ run a
6068 * pager with --exit-code. But since we have not done so historically,
6069 * and because it is easy to find people oneline advising "git diff
6070 * --exit-code" in hooks and other scripts, we do not do so.
6071 */
Brandon Williams0d1e0e72017-10-31 11:19:11 -07006072 if (!opt->flags.exit_with_status &&
Nguyễn Thái Ngọc Duy4914c962012-10-26 22:53:52 +07006073 check_pager_config("diff") != 0)
6074 setup_pager();
6075}