blob: 69f03570adf6cf2c3642aea1fb0dba5011ef966f [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")) {
Johan Herland1c57a622011-04-29 11:36:21 +0200127 DIFF_OPT_CLR(options, DIRSTAT_BY_LINE);
128 DIFF_OPT_CLR(options, DIRSTAT_BY_FILE);
Michael Haggerty02e8ca02012-10-28 17:50:54 +0100129 } else if (!strcmp(p, "lines")) {
Johan Herland1c57a622011-04-29 11:36:21 +0200130 DIFF_OPT_SET(options, DIRSTAT_BY_LINE);
Johan Herland333f3fb2011-04-29 11:36:18 +0200131 DIFF_OPT_CLR(options, DIRSTAT_BY_FILE);
Michael Haggerty02e8ca02012-10-28 17:50:54 +0100132 } else if (!strcmp(p, "files")) {
Johan Herland1c57a622011-04-29 11:36:21 +0200133 DIFF_OPT_CLR(options, DIRSTAT_BY_LINE);
Johan Herland333f3fb2011-04-29 11:36:18 +0200134 DIFF_OPT_SET(options, DIRSTAT_BY_FILE);
Michael Haggerty02e8ca02012-10-28 17:50:54 +0100135 } else if (!strcmp(p, "noncumulative")) {
Johan Herland333f3fb2011-04-29 11:36:18 +0200136 DIFF_OPT_CLR(options, DIRSTAT_CUMULATIVE);
Michael Haggerty02e8ca02012-10-28 17:50:54 +0100137 } else if (!strcmp(p, "cumulative")) {
Johan Herland333f3fb2011-04-29 11:36:18 +0200138 DIFF_OPT_SET(options, DIRSTAT_CUMULATIVE);
139 } 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
Johannes Schindelinef90d6d2008-05-14 18:46:53 +0100361 return git_diff_basic_config(var, value, cb);
Jeff King9a1805a2008-01-04 03:59:34 -0500362}
363
Johannes Schindelinef90d6d2008-05-14 18:46:53 +0100364int git_diff_basic_config(const char *var, const char *value, void *cb)
Jeff King9a1805a2008-01-04 03:59:34 -0500365{
Jeff Kingae021d82014-06-18 15:47:50 -0400366 const char *name;
367
Linus Torvalds2b6ca6d2008-08-05 11:27:30 -0700368 if (!strcmp(var, "diff.renamelimit")) {
369 diff_rename_limit_default = git_config_int(var, value);
370 return 0;
371 }
372
Jeff King6680a082012-02-07 13:23:02 -0500373 if (userdiff_config(var, value) < 0)
374 return -1;
Jeff Kingc7534ef2008-10-26 00:45:55 -0400375
Jeff Kingae021d82014-06-18 15:47:50 -0400376 if (skip_prefix(var, "diff.color.", &name) ||
377 skip_prefix(var, "color.diff.", &name)) {
378 int slot = parse_diff_color_slot(name);
Jeff King8b8e8622009-12-12 07:25:24 -0500379 if (slot < 0)
380 return 0;
Junio C Hamano64f30e92008-02-11 10:53:56 -0800381 if (!value)
382 return config_error_nonbool(var);
Jeff Kingf6c5a292014-10-07 15:33:09 -0400383 return color_parse(value, diff_colors[slot]);
Junio C Hamano801235c2006-06-24 04:06:23 -0700384 }
Junio C Hamanof1af60b2007-04-22 17:52:55 -0700385
Jim Meyeringa624eaa2008-08-15 13:39:26 +0200386 /* like GNU diff's --suppress-blank-empty option */
Johannes Schindelin950db872009-01-20 22:08:33 +0100387 if (!strcmp(var, "diff.suppressblankempty") ||
388 /* for backwards compatibility */
389 !strcmp(var, "diff.suppress-blank-empty")) {
Jim Meyeringa624eaa2008-08-15 13:39:26 +0200390 diff_suppress_blank_empty = git_config_bool(var, value);
391 return 0;
392 }
393
Johan Herland2d174952011-04-29 11:36:19 +0200394 if (!strcmp(var, "diff.dirstat")) {
Johan Herland51670fc2011-04-29 11:36:22 +0200395 struct strbuf errmsg = STRBUF_INIT;
Johan Herland712d2c72011-04-29 11:36:20 +0200396 default_diff_options.dirstat_permille = diff_dirstat_permille_default;
Johan Herland51670fc2011-04-29 11:36:22 +0200397 if (parse_dirstat_params(&default_diff_options, value, &errmsg))
Johan Herland7478ac52011-04-29 11:36:23 +0200398 warning(_("Found errors in 'diff.dirstat' config variable:\n%s"),
Johan Herland51670fc2011-04-29 11:36:22 +0200399 errmsg.buf);
400 strbuf_release(&errmsg);
Johan Herland712d2c72011-04-29 11:36:20 +0200401 diff_dirstat_permille_default = default_diff_options.dirstat_permille;
Johan Herland2d174952011-04-29 11:36:19 +0200402 return 0;
403 }
404
Marc Branchaudcf5e7722017-05-08 12:03:36 -0400405 if (git_diff_heuristic_config(var, value, cb) < 0)
406 return -1;
407
Jeff King3e1dd172011-08-17 22:05:08 -0700408 return git_default_config(var, value, cb);
Junio C Hamano801235c2006-06-24 04:06:23 -0700409}
410
Junio C Hamano6973dca2006-04-21 23:57:45 -0700411static char *quote_two(const char *one, const char *two)
412{
413 int need_one = quote_c_style(one, NULL, NULL, 1);
414 int need_two = quote_c_style(two, NULL, NULL, 1);
Brandon Caseyf285a2d2008-10-09 14:12:12 -0500415 struct strbuf res = STRBUF_INIT;
Junio C Hamano6973dca2006-04-21 23:57:45 -0700416
417 if (need_one + need_two) {
Pierre Habouzit663af342007-09-20 00:42:15 +0200418 strbuf_addch(&res, '"');
419 quote_c_style(one, &res, NULL, 1);
420 quote_c_style(two, &res, NULL, 1);
421 strbuf_addch(&res, '"');
422 } else {
423 strbuf_addstr(&res, one);
424 strbuf_addstr(&res, two);
Junio C Hamano6973dca2006-04-21 23:57:45 -0700425 }
Pierre Habouzitb315c5c2007-09-27 12:58:23 +0200426 return strbuf_detach(&res, NULL);
Junio C Hamano6973dca2006-04-21 23:57:45 -0700427}
428
429static const char *external_diff(void)
430{
431 static const char *external_diff_cmd = NULL;
432 static int done_preparing = 0;
433
434 if (done_preparing)
435 return external_diff_cmd;
436 external_diff_cmd = getenv("GIT_EXTERNAL_DIFF");
Johannes Schindelincbe02102007-12-17 13:42:20 +0000437 if (!external_diff_cmd)
438 external_diff_cmd = external_diff_cmd_cfg;
Junio C Hamano6973dca2006-04-21 23:57:45 -0700439 done_preparing = 1;
440 return external_diff_cmd;
441}
442
Michael Haggerty284098f2015-08-12 19:12:01 +0200443/*
444 * Keep track of files used for diffing. Sometimes such an entry
445 * refers to a temporary file, sometimes to an existing file, and
446 * sometimes to "/dev/null".
447 */
Junio C Hamano6973dca2006-04-21 23:57:45 -0700448static struct diff_tempfile {
Michael Haggerty284098f2015-08-12 19:12:01 +0200449 /*
450 * filename external diff should read from, or NULL if this
451 * entry is currently not in use:
452 */
453 const char *name;
454
brian m. carlsondc015052017-03-26 16:01:24 +0000455 char hex[GIT_MAX_HEXSZ + 1];
Junio C Hamano6973dca2006-04-21 23:57:45 -0700456 char mode[10];
Michael Haggerty284098f2015-08-12 19:12:01 +0200457
458 /*
459 * If this diff_tempfile instance refers to a temporary file,
460 * this tempfile object is used to manage its lifetime.
461 */
Jeff King076aa2c2017-09-05 08:15:08 -0400462 struct tempfile *tempfile;
Junio C Hamano6973dca2006-04-21 23:57:45 -0700463} diff_temp[2];
464
Junio C Hamano6957eb92009-09-14 18:44:01 -0700465struct emit_callback {
Junio C Hamano6957eb92009-09-14 18:44:01 -0700466 int color_diff;
467 unsigned ws_rule;
468 int blank_at_eof_in_preimage;
469 int blank_at_eof_in_postimage;
470 int lno_in_preimage;
471 int lno_in_postimage;
Junio C Hamano6957eb92009-09-14 18:44:01 -0700472 const char **label_path;
473 struct diff_words_data *diff_words;
Bo Yanga3c158d2010-05-26 15:08:02 +0800474 struct diff_options *opt;
Greg Bacon3e97c7c2009-11-19 15:12:24 -0600475 struct strbuf *header;
Junio C Hamano6957eb92009-09-14 18:44:01 -0700476};
477
Junio C Hamano6973dca2006-04-21 23:57:45 -0700478static int count_lines(const char *data, int size)
479{
480 int count, ch, completely_empty = 1, nl_just_seen = 0;
481 count = 0;
482 while (0 < size--) {
483 ch = *data++;
484 if (ch == '\n') {
485 count++;
486 nl_just_seen = 1;
487 completely_empty = 0;
488 }
489 else {
490 nl_just_seen = 0;
491 completely_empty = 0;
492 }
493 }
494 if (completely_empty)
495 return 0;
496 if (!nl_just_seen)
497 count++; /* no trailing newline */
498 return count;
499}
500
Junio C Hamano6957eb92009-09-14 18:44:01 -0700501static int fill_mmfile(mmfile_t *mf, struct diff_filespec *one)
502{
503 if (!DIFF_FILE_VALID(one)) {
504 mf->ptr = (char *)""; /* does not matter */
505 mf->size = 0;
506 return 0;
507 }
508 else if (diff_populate_filespec(one, 0))
509 return -1;
Junio C Hamanobb35fef2009-09-15 03:38:30 -0700510
Junio C Hamano6957eb92009-09-14 18:44:01 -0700511 mf->ptr = one->data;
512 mf->size = one->size;
513 return 0;
514}
515
Jeff Kingabb371a2011-02-19 03:16:32 -0500516/* like fill_mmfile, but only for size, so we can avoid retrieving blob */
517static unsigned long diff_filespec_size(struct diff_filespec *one)
518{
519 if (!DIFF_FILE_VALID(one))
520 return 0;
Nguyễn Thái Ngọc Duy8e5dd3d2014-08-16 10:08:04 +0700521 diff_populate_filespec(one, CHECK_SIZE_ONLY);
Jeff Kingabb371a2011-02-19 03:16:32 -0500522 return one->size;
523}
524
Junio C Hamano6957eb92009-09-14 18:44:01 -0700525static int count_trailing_blank(mmfile_t *mf, unsigned ws_rule)
526{
527 char *ptr = mf->ptr;
528 long size = mf->size;
529 int cnt = 0;
530
531 if (!size)
532 return cnt;
533 ptr += size - 1; /* pointing at the very end */
534 if (*ptr != '\n')
535 ; /* incomplete line */
536 else
537 ptr--; /* skip the last LF */
538 while (mf->ptr < ptr) {
539 char *prev_eol;
540 for (prev_eol = ptr; mf->ptr <= prev_eol; prev_eol--)
541 if (*prev_eol == '\n')
542 break;
543 if (!ws_blank_line(prev_eol + 1, ptr - prev_eol, ws_rule))
544 break;
545 cnt++;
546 ptr = prev_eol - 1;
547 }
548 return cnt;
549}
550
551static void check_blank_at_eof(mmfile_t *mf1, mmfile_t *mf2,
552 struct emit_callback *ecbdata)
553{
554 int l1, l2, at;
555 unsigned ws_rule = ecbdata->ws_rule;
556 l1 = count_trailing_blank(mf1, ws_rule);
557 l2 = count_trailing_blank(mf2, ws_rule);
558 if (l2 <= l1) {
559 ecbdata->blank_at_eof_in_preimage = 0;
560 ecbdata->blank_at_eof_in_postimage = 0;
561 return;
562 }
563 at = count_lines(mf1->ptr, mf1->size);
564 ecbdata->blank_at_eof_in_preimage = (at - l1) + 1;
565
566 at = count_lines(mf2->ptr, mf2->size);
567 ecbdata->blank_at_eof_in_postimage = (at - l2) + 1;
568}
569
Bo Yanga3c158d2010-05-26 15:08:02 +0800570static void emit_line_0(struct diff_options *o, const char *set, const char *reset,
Junio C Hamano250f7992009-09-14 18:44:01 -0700571 int first, const char *line, int len)
Junio C Hamano6957eb92009-09-14 18:44:01 -0700572{
573 int has_trailing_newline, has_trailing_carriage_return;
Junio C Hamano250f7992009-09-14 18:44:01 -0700574 int nofirst;
Bo Yanga3c158d2010-05-26 15:08:02 +0800575 FILE *file = o->file;
576
John Keeping30997bb2013-02-07 20:15:27 +0000577 fputs(diff_line_prefix(o), file);
Junio C Hamano6957eb92009-09-14 18:44:01 -0700578
Junio C Hamano250f7992009-09-14 18:44:01 -0700579 if (len == 0) {
580 has_trailing_newline = (first == '\n');
581 has_trailing_carriage_return = (!has_trailing_newline &&
582 (first == '\r'));
583 nofirst = has_trailing_newline || has_trailing_carriage_return;
584 } else {
585 has_trailing_newline = (len > 0 && line[len-1] == '\n');
586 if (has_trailing_newline)
587 len--;
588 has_trailing_carriage_return = (len > 0 && line[len-1] == '\r');
589 if (has_trailing_carriage_return)
590 len--;
591 nofirst = 0;
592 }
Junio C Hamano6957eb92009-09-14 18:44:01 -0700593
Junio C Hamano06a47552009-11-27 22:04:10 -0800594 if (len || !nofirst) {
595 fputs(set, file);
596 if (!nofirst)
597 fputc(first, file);
598 fwrite(line, len, 1, file);
599 fputs(reset, file);
600 }
Junio C Hamano6957eb92009-09-14 18:44:01 -0700601 if (has_trailing_carriage_return)
602 fputc('\r', file);
603 if (has_trailing_newline)
604 fputc('\n', file);
605}
606
Bo Yanga3c158d2010-05-26 15:08:02 +0800607static void emit_line(struct diff_options *o, const char *set, const char *reset,
Junio C Hamano250f7992009-09-14 18:44:01 -0700608 const char *line, int len)
609{
Bo Yanga3c158d2010-05-26 15:08:02 +0800610 emit_line_0(o, set, reset, line[0], line+1, len-1);
Junio C Hamano250f7992009-09-14 18:44:01 -0700611}
612
Stefan Beller36a4cef2017-06-29 17:06:49 -0700613enum diff_symbol {
Stefan Beller4eed0eb2017-06-29 17:07:01 -0700614 DIFF_SYMBOL_BINARY_DIFF_HEADER,
615 DIFF_SYMBOL_BINARY_DIFF_HEADER_DELTA,
616 DIFF_SYMBOL_BINARY_DIFF_HEADER_LITERAL,
617 DIFF_SYMBOL_BINARY_DIFF_BODY,
618 DIFF_SYMBOL_BINARY_DIFF_FOOTER,
Stefan Beller0911c472017-06-29 17:07:02 -0700619 DIFF_SYMBOL_STATS_SUMMARY_NO_FILES,
620 DIFF_SYMBOL_STATS_SUMMARY_ABBREV,
621 DIFF_SYMBOL_STATS_SUMMARY_INSERTS_DELETES,
622 DIFF_SYMBOL_STATS_LINE,
Stefan Bellerbd033292017-06-29 17:07:03 -0700623 DIFF_SYMBOL_WORD_DIFF,
Stefan Beller30b7e1e2017-06-29 17:07:04 -0700624 DIFF_SYMBOL_STAT_SEP,
Stefan Beller146fdb02017-06-29 17:07:05 -0700625 DIFF_SYMBOL_SUMMARY,
Stefan Bellerf3597132017-06-29 17:07:00 -0700626 DIFF_SYMBOL_SUBMODULE_ADD,
627 DIFF_SYMBOL_SUBMODULE_DEL,
628 DIFF_SYMBOL_SUBMODULE_UNTRACKED,
629 DIFF_SYMBOL_SUBMODULE_MODIFIED,
630 DIFF_SYMBOL_SUBMODULE_HEADER,
631 DIFF_SYMBOL_SUBMODULE_ERROR,
632 DIFF_SYMBOL_SUBMODULE_PIPETHROUGH,
Stefan Beller5af6ea92017-06-29 17:06:59 -0700633 DIFF_SYMBOL_REWRITE_DIFF,
Stefan Beller4acaaa72017-06-29 17:06:58 -0700634 DIFF_SYMBOL_BINARY_FILES,
Stefan Bellera29b0a12017-06-29 17:06:57 -0700635 DIFF_SYMBOL_HEADER,
Stefan Beller3ee8b7b2017-06-29 17:06:56 -0700636 DIFF_SYMBOL_FILEPAIR_PLUS,
637 DIFF_SYMBOL_FILEPAIR_MINUS,
Stefan Bellerff958672017-06-29 17:06:54 -0700638 DIFF_SYMBOL_WORDS_PORCELAIN,
639 DIFF_SYMBOL_WORDS,
Stefan Beller091f8e22017-06-29 17:06:53 -0700640 DIFF_SYMBOL_CONTEXT,
Stefan Bellerf2bb1212017-06-29 17:06:55 -0700641 DIFF_SYMBOL_CONTEXT_INCOMPLETE,
Stefan Beller091f8e22017-06-29 17:06:53 -0700642 DIFF_SYMBOL_PLUS,
643 DIFF_SYMBOL_MINUS,
Stefan Bellerb9cbfde2017-06-29 17:06:52 -0700644 DIFF_SYMBOL_NO_LF_EOF,
Stefan Beller68abc6f2017-06-29 17:06:51 -0700645 DIFF_SYMBOL_CONTEXT_FRAGINFO,
Stefan Bellerc64b4202017-06-29 17:06:50 -0700646 DIFF_SYMBOL_CONTEXT_MARKER,
Stefan Beller36a4cef2017-06-29 17:06:49 -0700647 DIFF_SYMBOL_SEPARATOR
648};
Stefan Beller091f8e22017-06-29 17:06:53 -0700649/*
650 * Flags for content lines:
651 * 0..12 are whitespace rules
652 * 13-15 are WSEH_NEW | WSEH_OLD | WSEH_CONTEXT
653 * 16 is marking if the line is blank at EOF
654 */
Stefan Beller2e2d5ac2017-06-30 13:53:07 -0700655#define DIFF_SYMBOL_CONTENT_BLANK_LINE_EOF (1<<16)
656#define DIFF_SYMBOL_MOVED_LINE (1<<17)
657#define DIFF_SYMBOL_MOVED_LINE_ALT (1<<18)
Stefan Beller86b452e2017-06-30 13:53:09 -0700658#define DIFF_SYMBOL_MOVED_LINE_UNINTERESTING (1<<19)
Stefan Beller091f8e22017-06-29 17:06:53 -0700659#define DIFF_SYMBOL_CONTENT_WS_MASK (WSEH_NEW | WSEH_OLD | WSEH_CONTEXT | WS_RULE_MASK)
660
Stefan Bellere6e045f2017-06-29 17:07:06 -0700661/*
662 * This struct is used when we need to buffer the output of the diff output.
663 *
664 * NEEDSWORK: Instead of storing a copy of the line, add an offset pointer
665 * into the pre/post image file. This pointer could be a union with the
666 * line pointer. By storing an offset into the file instead of the literal line,
667 * we can decrease the memory footprint for the buffered output. At first we
668 * may want to only have indirection for the content lines, but we could also
669 * enhance the state for emitting prefabricated lines, e.g. the similarity
670 * score line or hunk/file headers would only need to store a number or path
671 * and then the output can be constructed later on depending on state.
672 */
673struct emitted_diff_symbol {
674 const char *line;
675 int len;
676 int flags;
677 enum diff_symbol s;
678};
679#define EMITTED_DIFF_SYMBOL_INIT {NULL}
680
681struct emitted_diff_symbols {
682 struct emitted_diff_symbol *buf;
683 int nr, alloc;
684};
685#define EMITTED_DIFF_SYMBOLS_INIT {NULL, 0, 0}
686
687static void append_emitted_diff_symbol(struct diff_options *o,
688 struct emitted_diff_symbol *e)
689{
690 struct emitted_diff_symbol *f;
691
692 ALLOC_GROW(o->emitted_symbols->buf,
693 o->emitted_symbols->nr + 1,
694 o->emitted_symbols->alloc);
695 f = &o->emitted_symbols->buf[o->emitted_symbols->nr++];
696
697 memcpy(f, e, sizeof(struct emitted_diff_symbol));
698 f->line = e->line ? xmemdupz(e->line, e->len) : NULL;
699}
700
Stefan Beller2e2d5ac2017-06-30 13:53:07 -0700701struct moved_entry {
702 struct hashmap_entry ent;
703 const struct emitted_diff_symbol *es;
704 struct moved_entry *next_line;
705};
706
707static int next_byte(const char **cp, const char **endp,
708 const struct diff_options *diffopt)
709{
710 int retval;
711
712 if (*cp > *endp)
713 return -1;
714
715 if (DIFF_XDL_TST(diffopt, IGNORE_WHITESPACE_CHANGE)) {
716 while (*cp < *endp && isspace(**cp))
717 (*cp)++;
718 /*
719 * After skipping a couple of whitespaces, we still have to
720 * account for one space.
721 */
722 return (int)' ';
723 }
724
725 if (DIFF_XDL_TST(diffopt, IGNORE_WHITESPACE)) {
726 while (*cp < *endp && isspace(**cp))
727 (*cp)++;
728 /* return the first non-ws character via the usual below */
729 }
730
731 retval = (unsigned char)(**cp);
732 (*cp)++;
733 return retval;
734}
735
736static int moved_entry_cmp(const struct diff_options *diffopt,
737 const struct moved_entry *a,
738 const struct moved_entry *b,
739 const void *keydata)
740{
741 const char *ap = a->es->line, *ae = a->es->line + a->es->len;
742 const char *bp = b->es->line, *be = b->es->line + b->es->len;
743
744 if (!(diffopt->xdl_opts & XDF_WHITESPACE_FLAGS))
745 return a->es->len != b->es->len || memcmp(ap, bp, a->es->len);
746
747 if (DIFF_XDL_TST(diffopt, IGNORE_WHITESPACE_AT_EOL)) {
748 while (ae > ap && isspace(*ae))
749 ae--;
750 while (be > bp && isspace(*be))
751 be--;
752 }
753
754 while (1) {
755 int ca, cb;
756 ca = next_byte(&ap, &ae, diffopt);
757 cb = next_byte(&bp, &be, diffopt);
758 if (ca != cb)
759 return 1;
760 if (ca < 0)
761 return 0;
762 }
763}
764
765static unsigned get_string_hash(struct emitted_diff_symbol *es, struct diff_options *o)
766{
767 if (o->xdl_opts & XDF_WHITESPACE_FLAGS) {
768 static struct strbuf sb = STRBUF_INIT;
769 const char *ap = es->line, *ae = es->line + es->len;
770 int c;
771
772 strbuf_reset(&sb);
773 while (ae > ap && isspace(*ae))
774 ae--;
775 while ((c = next_byte(&ap, &ae, o)) > 0)
776 strbuf_addch(&sb, c);
777
778 return memhash(sb.buf, sb.len);
779 } else {
780 return memhash(es->line, es->len);
781 }
782}
783
784static struct moved_entry *prepare_entry(struct diff_options *o,
785 int line_no)
786{
787 struct moved_entry *ret = xmalloc(sizeof(*ret));
788 struct emitted_diff_symbol *l = &o->emitted_symbols->buf[line_no];
789
790 ret->ent.hash = get_string_hash(l, o);
791 ret->es = l;
792 ret->next_line = NULL;
793
794 return ret;
795}
796
797static void add_lines_to_move_detection(struct diff_options *o,
798 struct hashmap *add_lines,
799 struct hashmap *del_lines)
800{
801 struct moved_entry *prev_line = NULL;
802
803 int n;
804 for (n = 0; n < o->emitted_symbols->nr; n++) {
805 struct hashmap *hm;
806 struct moved_entry *key;
807
808 switch (o->emitted_symbols->buf[n].s) {
809 case DIFF_SYMBOL_PLUS:
810 hm = add_lines;
811 break;
812 case DIFF_SYMBOL_MINUS:
813 hm = del_lines;
814 break;
815 default:
816 prev_line = NULL;
817 continue;
818 }
819
820 key = prepare_entry(o, n);
821 if (prev_line && prev_line->es->s == o->emitted_symbols->buf[n].s)
822 prev_line->next_line = key;
823
824 hashmap_add(hm, key);
825 prev_line = key;
826 }
827}
828
829static int shrink_potential_moved_blocks(struct moved_entry **pmb,
830 int pmb_nr)
831{
832 int lp, rp;
833
834 /* Shrink the set of potential block to the remaining running */
835 for (lp = 0, rp = pmb_nr - 1; lp <= rp;) {
836 while (lp < pmb_nr && pmb[lp])
837 lp++;
838 /* lp points at the first NULL now */
839
840 while (rp > -1 && !pmb[rp])
841 rp--;
842 /* rp points at the last non-NULL */
843
844 if (lp < pmb_nr && rp > -1 && lp < rp) {
845 pmb[lp] = pmb[rp];
846 pmb[rp] = NULL;
847 rp--;
848 lp++;
849 }
850 }
851
852 /* Remember the number of running sets */
853 return rp + 1;
854}
855
Jonathan Tan09153272017-08-15 18:27:38 -0700856/*
857 * If o->color_moved is COLOR_MOVED_PLAIN, this function does nothing.
858 *
Jonathan Tanf0b8fb62017-08-15 18:27:39 -0700859 * Otherwise, if the last block has fewer alphanumeric characters than
860 * COLOR_MOVED_MIN_ALNUM_COUNT, unset DIFF_SYMBOL_MOVED_LINE on all lines in
Jonathan Tan09153272017-08-15 18:27:38 -0700861 * that block.
862 *
863 * The last block consists of the (n - block_length)'th line up to but not
864 * including the nth line.
Jonathan Tanf0b8fb62017-08-15 18:27:39 -0700865 *
866 * NEEDSWORK: This uses the same heuristic as blame_entry_score() in blame.c.
867 * Think of a way to unify them.
Jonathan Tan09153272017-08-15 18:27:38 -0700868 */
869static void adjust_last_block(struct diff_options *o, int n, int block_length)
870{
Jonathan Tanf0b8fb62017-08-15 18:27:39 -0700871 int i, alnum_count = 0;
872 if (o->color_moved == COLOR_MOVED_PLAIN)
Jonathan Tan09153272017-08-15 18:27:38 -0700873 return;
Jonathan Tanf0b8fb62017-08-15 18:27:39 -0700874 for (i = 1; i < block_length + 1; i++) {
875 const char *c = o->emitted_symbols->buf[n - i].line;
876 for (; *c; c++) {
877 if (!isalnum(*c))
878 continue;
879 alnum_count++;
880 if (alnum_count >= COLOR_MOVED_MIN_ALNUM_COUNT)
881 return;
882 }
883 }
Jonathan Tan09153272017-08-15 18:27:38 -0700884 for (i = 1; i < block_length + 1; i++)
885 o->emitted_symbols->buf[n - i].flags &= ~DIFF_SYMBOL_MOVED_LINE;
886}
887
Stefan Beller2e2d5ac2017-06-30 13:53:07 -0700888/* Find blocks of moved code, delegate actual coloring decision to helper */
889static void mark_color_as_moved(struct diff_options *o,
890 struct hashmap *add_lines,
891 struct hashmap *del_lines)
892{
893 struct moved_entry **pmb = NULL; /* potentially moved blocks */
894 int pmb_nr = 0, pmb_alloc = 0;
895 int n, flipped_block = 1, block_length = 0;
896
897
898 for (n = 0; n < o->emitted_symbols->nr; n++) {
899 struct hashmap *hm = NULL;
900 struct moved_entry *key;
901 struct moved_entry *match = NULL;
902 struct emitted_diff_symbol *l = &o->emitted_symbols->buf[n];
903 int i;
904
905 switch (l->s) {
906 case DIFF_SYMBOL_PLUS:
907 hm = del_lines;
908 key = prepare_entry(o, n);
909 match = hashmap_get(hm, key, o);
910 free(key);
911 break;
912 case DIFF_SYMBOL_MINUS:
913 hm = add_lines;
914 key = prepare_entry(o, n);
915 match = hashmap_get(hm, key, o);
916 free(key);
917 break;
918 default:
919 flipped_block = 1;
920 }
921
922 if (!match) {
Jonathan Tan09153272017-08-15 18:27:38 -0700923 adjust_last_block(o, n, block_length);
Stefan Beller2e2d5ac2017-06-30 13:53:07 -0700924 pmb_nr = 0;
925 block_length = 0;
926 continue;
927 }
928
929 l->flags |= DIFF_SYMBOL_MOVED_LINE;
Stefan Beller2e2d5ac2017-06-30 13:53:07 -0700930
Stefan Beller176841f2017-06-30 13:53:08 -0700931 if (o->color_moved == COLOR_MOVED_PLAIN)
932 continue;
933
Stefan Beller2e2d5ac2017-06-30 13:53:07 -0700934 /* Check any potential block runs, advance each or nullify */
935 for (i = 0; i < pmb_nr; i++) {
936 struct moved_entry *p = pmb[i];
937 struct moved_entry *pnext = (p && p->next_line) ?
938 p->next_line : NULL;
939 if (pnext && !hm->cmpfn(o, pnext, match, NULL)) {
940 pmb[i] = p->next_line;
941 } else {
942 pmb[i] = NULL;
943 }
944 }
945
946 pmb_nr = shrink_potential_moved_blocks(pmb, pmb_nr);
947
948 if (pmb_nr == 0) {
949 /*
950 * The current line is the start of a new block.
951 * Setup the set of potential blocks.
952 */
953 for (; match; match = hashmap_get_next(hm, match)) {
954 ALLOC_GROW(pmb, pmb_nr + 1, pmb_alloc);
955 pmb[pmb_nr++] = match;
956 }
957
958 flipped_block = (flipped_block + 1) % 2;
Jonathan Tanf0b8fb62017-08-15 18:27:39 -0700959
960 adjust_last_block(o, n, block_length);
961 block_length = 0;
Stefan Beller2e2d5ac2017-06-30 13:53:07 -0700962 }
963
Jonathan Tanf0b8fb62017-08-15 18:27:39 -0700964 block_length++;
965
Stefan Beller2e2d5ac2017-06-30 13:53:07 -0700966 if (flipped_block)
967 l->flags |= DIFF_SYMBOL_MOVED_LINE_ALT;
968 }
Jonathan Tan09153272017-08-15 18:27:38 -0700969 adjust_last_block(o, n, block_length);
Stefan Beller2e2d5ac2017-06-30 13:53:07 -0700970
971 free(pmb);
972}
Stefan Bellere6e045f2017-06-29 17:07:06 -0700973
Stefan Beller86b452e2017-06-30 13:53:09 -0700974#define DIFF_SYMBOL_MOVED_LINE_ZEBRA_MASK \
975 (DIFF_SYMBOL_MOVED_LINE | DIFF_SYMBOL_MOVED_LINE_ALT)
976static void dim_moved_lines(struct diff_options *o)
977{
978 int n;
979 for (n = 0; n < o->emitted_symbols->nr; n++) {
980 struct emitted_diff_symbol *prev = (n != 0) ?
981 &o->emitted_symbols->buf[n - 1] : NULL;
982 struct emitted_diff_symbol *l = &o->emitted_symbols->buf[n];
983 struct emitted_diff_symbol *next =
984 (n < o->emitted_symbols->nr - 1) ?
985 &o->emitted_symbols->buf[n + 1] : NULL;
986
987 /* Not a plus or minus line? */
988 if (l->s != DIFF_SYMBOL_PLUS && l->s != DIFF_SYMBOL_MINUS)
989 continue;
990
991 /* Not a moved line? */
992 if (!(l->flags & DIFF_SYMBOL_MOVED_LINE))
993 continue;
994
995 /*
996 * If prev or next are not a plus or minus line,
997 * pretend they don't exist
998 */
999 if (prev && prev->s != DIFF_SYMBOL_PLUS &&
1000 prev->s != DIFF_SYMBOL_MINUS)
1001 prev = NULL;
1002 if (next && next->s != DIFF_SYMBOL_PLUS &&
1003 next->s != DIFF_SYMBOL_MINUS)
1004 next = NULL;
1005
1006 /* Inside a block? */
1007 if ((prev &&
1008 (prev->flags & DIFF_SYMBOL_MOVED_LINE_ZEBRA_MASK) ==
1009 (l->flags & DIFF_SYMBOL_MOVED_LINE_ZEBRA_MASK)) &&
1010 (next &&
1011 (next->flags & DIFF_SYMBOL_MOVED_LINE_ZEBRA_MASK) ==
1012 (l->flags & DIFF_SYMBOL_MOVED_LINE_ZEBRA_MASK))) {
1013 l->flags |= DIFF_SYMBOL_MOVED_LINE_UNINTERESTING;
1014 continue;
1015 }
1016
1017 /* Check if we are at an interesting bound: */
1018 if (prev && (prev->flags & DIFF_SYMBOL_MOVED_LINE) &&
1019 (prev->flags & DIFF_SYMBOL_MOVED_LINE_ALT) !=
1020 (l->flags & DIFF_SYMBOL_MOVED_LINE_ALT))
1021 continue;
1022 if (next && (next->flags & DIFF_SYMBOL_MOVED_LINE) &&
1023 (next->flags & DIFF_SYMBOL_MOVED_LINE_ALT) !=
1024 (l->flags & DIFF_SYMBOL_MOVED_LINE_ALT))
1025 continue;
1026
1027 /*
1028 * The boundary to prev and next are not interesting,
1029 * so this line is not interesting as a whole
1030 */
1031 l->flags |= DIFF_SYMBOL_MOVED_LINE_UNINTERESTING;
1032 }
1033}
1034
Stefan Beller091f8e22017-06-29 17:06:53 -07001035static void emit_line_ws_markup(struct diff_options *o,
1036 const char *set, const char *reset,
1037 const char *line, int len, char sign,
1038 unsigned ws_rule, int blank_at_eof)
1039{
1040 const char *ws = NULL;
1041
1042 if (o->ws_error_highlight & ws_rule) {
1043 ws = diff_get_color_opt(o, DIFF_WHITESPACE);
1044 if (!*ws)
1045 ws = NULL;
1046 }
1047
1048 if (!ws)
1049 emit_line_0(o, set, reset, sign, line, len);
1050 else if (blank_at_eof)
1051 /* Blank line at EOF - paint '+' as well */
1052 emit_line_0(o, ws, reset, sign, line, len);
1053 else {
1054 /* Emit just the prefix, then the rest. */
1055 emit_line_0(o, set, reset, sign, "", 0);
1056 ws_check_emit(line, len, ws_rule,
1057 o->file, set, reset, ws);
1058 }
1059}
Stefan Beller36a4cef2017-06-29 17:06:49 -07001060
Stefan Bellere6e045f2017-06-29 17:07:06 -07001061static void emit_diff_symbol_from_struct(struct diff_options *o,
1062 struct emitted_diff_symbol *eds)
Stefan Beller36a4cef2017-06-29 17:06:49 -07001063{
Stefan Bellerb9cbfde2017-06-29 17:06:52 -07001064 static const char *nneof = " No newline at end of file\n";
Stefan Beller5af6ea92017-06-29 17:06:59 -07001065 const char *context, *reset, *set, *meta, *fraginfo;
Stefan Beller0911c472017-06-29 17:07:02 -07001066 struct strbuf sb = STRBUF_INIT;
Stefan Bellere6e045f2017-06-29 17:07:06 -07001067
1068 enum diff_symbol s = eds->s;
1069 const char *line = eds->line;
1070 int len = eds->len;
1071 unsigned flags = eds->flags;
1072
Stefan Beller36a4cef2017-06-29 17:06:49 -07001073 switch (s) {
Stefan Bellerb9cbfde2017-06-29 17:06:52 -07001074 case DIFF_SYMBOL_NO_LF_EOF:
1075 context = diff_get_color_opt(o, DIFF_CONTEXT);
1076 reset = diff_get_color_opt(o, DIFF_RESET);
1077 putc('\n', o->file);
1078 emit_line_0(o, context, reset, '\\',
1079 nneof, strlen(nneof));
1080 break;
Stefan Bellerf3597132017-06-29 17:07:00 -07001081 case DIFF_SYMBOL_SUBMODULE_HEADER:
1082 case DIFF_SYMBOL_SUBMODULE_ERROR:
1083 case DIFF_SYMBOL_SUBMODULE_PIPETHROUGH:
Stefan Beller0911c472017-06-29 17:07:02 -07001084 case DIFF_SYMBOL_STATS_SUMMARY_INSERTS_DELETES:
Stefan Beller146fdb02017-06-29 17:07:05 -07001085 case DIFF_SYMBOL_SUMMARY:
Stefan Beller0911c472017-06-29 17:07:02 -07001086 case DIFF_SYMBOL_STATS_LINE:
Stefan Beller4eed0eb2017-06-29 17:07:01 -07001087 case DIFF_SYMBOL_BINARY_DIFF_BODY:
Stefan Beller68abc6f2017-06-29 17:06:51 -07001088 case DIFF_SYMBOL_CONTEXT_FRAGINFO:
1089 emit_line(o, "", "", line, len);
1090 break;
Stefan Bellerf2bb1212017-06-29 17:06:55 -07001091 case DIFF_SYMBOL_CONTEXT_INCOMPLETE:
Stefan Bellerc64b4202017-06-29 17:06:50 -07001092 case DIFF_SYMBOL_CONTEXT_MARKER:
1093 context = diff_get_color_opt(o, DIFF_CONTEXT);
1094 reset = diff_get_color_opt(o, DIFF_RESET);
1095 emit_line(o, context, reset, line, len);
1096 break;
Stefan Beller36a4cef2017-06-29 17:06:49 -07001097 case DIFF_SYMBOL_SEPARATOR:
1098 fprintf(o->file, "%s%c",
1099 diff_line_prefix(o),
1100 o->line_termination);
1101 break;
Stefan Beller091f8e22017-06-29 17:06:53 -07001102 case DIFF_SYMBOL_CONTEXT:
1103 set = diff_get_color_opt(o, DIFF_CONTEXT);
1104 reset = diff_get_color_opt(o, DIFF_RESET);
1105 emit_line_ws_markup(o, set, reset, line, len, ' ',
1106 flags & (DIFF_SYMBOL_CONTENT_WS_MASK), 0);
1107 break;
1108 case DIFF_SYMBOL_PLUS:
Stefan Beller86b452e2017-06-30 13:53:09 -07001109 switch (flags & (DIFF_SYMBOL_MOVED_LINE |
1110 DIFF_SYMBOL_MOVED_LINE_ALT |
1111 DIFF_SYMBOL_MOVED_LINE_UNINTERESTING)) {
1112 case DIFF_SYMBOL_MOVED_LINE |
1113 DIFF_SYMBOL_MOVED_LINE_ALT |
1114 DIFF_SYMBOL_MOVED_LINE_UNINTERESTING:
1115 set = diff_get_color_opt(o, DIFF_FILE_NEW_MOVED_ALT_DIM);
1116 break;
1117 case DIFF_SYMBOL_MOVED_LINE |
1118 DIFF_SYMBOL_MOVED_LINE_ALT:
Stefan Beller2e2d5ac2017-06-30 13:53:07 -07001119 set = diff_get_color_opt(o, DIFF_FILE_NEW_MOVED_ALT);
Stefan Beller86b452e2017-06-30 13:53:09 -07001120 break;
1121 case DIFF_SYMBOL_MOVED_LINE |
1122 DIFF_SYMBOL_MOVED_LINE_UNINTERESTING:
1123 set = diff_get_color_opt(o, DIFF_FILE_NEW_MOVED_DIM);
1124 break;
1125 case DIFF_SYMBOL_MOVED_LINE:
Stefan Beller2e2d5ac2017-06-30 13:53:07 -07001126 set = diff_get_color_opt(o, DIFF_FILE_NEW_MOVED);
Stefan Beller86b452e2017-06-30 13:53:09 -07001127 break;
1128 default:
Stefan Beller2e2d5ac2017-06-30 13:53:07 -07001129 set = diff_get_color_opt(o, DIFF_FILE_NEW);
Stefan Beller86b452e2017-06-30 13:53:09 -07001130 }
Stefan Beller091f8e22017-06-29 17:06:53 -07001131 reset = diff_get_color_opt(o, DIFF_RESET);
1132 emit_line_ws_markup(o, set, reset, line, len, '+',
1133 flags & DIFF_SYMBOL_CONTENT_WS_MASK,
1134 flags & DIFF_SYMBOL_CONTENT_BLANK_LINE_EOF);
1135 break;
1136 case DIFF_SYMBOL_MINUS:
Stefan Beller86b452e2017-06-30 13:53:09 -07001137 switch (flags & (DIFF_SYMBOL_MOVED_LINE |
1138 DIFF_SYMBOL_MOVED_LINE_ALT |
1139 DIFF_SYMBOL_MOVED_LINE_UNINTERESTING)) {
1140 case DIFF_SYMBOL_MOVED_LINE |
1141 DIFF_SYMBOL_MOVED_LINE_ALT |
1142 DIFF_SYMBOL_MOVED_LINE_UNINTERESTING:
1143 set = diff_get_color_opt(o, DIFF_FILE_OLD_MOVED_ALT_DIM);
1144 break;
1145 case DIFF_SYMBOL_MOVED_LINE |
1146 DIFF_SYMBOL_MOVED_LINE_ALT:
Stefan Beller2e2d5ac2017-06-30 13:53:07 -07001147 set = diff_get_color_opt(o, DIFF_FILE_OLD_MOVED_ALT);
Stefan Beller86b452e2017-06-30 13:53:09 -07001148 break;
1149 case DIFF_SYMBOL_MOVED_LINE |
1150 DIFF_SYMBOL_MOVED_LINE_UNINTERESTING:
1151 set = diff_get_color_opt(o, DIFF_FILE_OLD_MOVED_DIM);
1152 break;
1153 case DIFF_SYMBOL_MOVED_LINE:
Stefan Beller2e2d5ac2017-06-30 13:53:07 -07001154 set = diff_get_color_opt(o, DIFF_FILE_OLD_MOVED);
Stefan Beller86b452e2017-06-30 13:53:09 -07001155 break;
1156 default:
Stefan Beller2e2d5ac2017-06-30 13:53:07 -07001157 set = diff_get_color_opt(o, DIFF_FILE_OLD);
Stefan Beller86b452e2017-06-30 13:53:09 -07001158 }
Stefan Beller091f8e22017-06-29 17:06:53 -07001159 reset = diff_get_color_opt(o, DIFF_RESET);
1160 emit_line_ws_markup(o, set, reset, line, len, '-',
1161 flags & DIFF_SYMBOL_CONTENT_WS_MASK, 0);
1162 break;
Stefan Bellerff958672017-06-29 17:06:54 -07001163 case DIFF_SYMBOL_WORDS_PORCELAIN:
1164 context = diff_get_color_opt(o, DIFF_CONTEXT);
1165 reset = diff_get_color_opt(o, DIFF_RESET);
1166 emit_line(o, context, reset, line, len);
1167 fputs("~\n", o->file);
1168 break;
1169 case DIFF_SYMBOL_WORDS:
1170 context = diff_get_color_opt(o, DIFF_CONTEXT);
1171 reset = diff_get_color_opt(o, DIFF_RESET);
1172 /*
1173 * Skip the prefix character, if any. With
1174 * diff_suppress_blank_empty, there may be
1175 * none.
1176 */
1177 if (line[0] != '\n') {
1178 line++;
1179 len--;
1180 }
1181 emit_line(o, context, reset, line, len);
1182 break;
Stefan Beller3ee8b7b2017-06-29 17:06:56 -07001183 case DIFF_SYMBOL_FILEPAIR_PLUS:
1184 meta = diff_get_color_opt(o, DIFF_METAINFO);
1185 reset = diff_get_color_opt(o, DIFF_RESET);
1186 fprintf(o->file, "%s%s+++ %s%s%s\n", diff_line_prefix(o), meta,
1187 line, reset,
1188 strchr(line, ' ') ? "\t" : "");
1189 break;
1190 case DIFF_SYMBOL_FILEPAIR_MINUS:
1191 meta = diff_get_color_opt(o, DIFF_METAINFO);
1192 reset = diff_get_color_opt(o, DIFF_RESET);
1193 fprintf(o->file, "%s%s--- %s%s%s\n", diff_line_prefix(o), meta,
1194 line, reset,
1195 strchr(line, ' ') ? "\t" : "");
1196 break;
Stefan Beller4acaaa72017-06-29 17:06:58 -07001197 case DIFF_SYMBOL_BINARY_FILES:
Stefan Bellera29b0a12017-06-29 17:06:57 -07001198 case DIFF_SYMBOL_HEADER:
1199 fprintf(o->file, "%s", line);
1200 break;
Stefan Beller4eed0eb2017-06-29 17:07:01 -07001201 case DIFF_SYMBOL_BINARY_DIFF_HEADER:
1202 fprintf(o->file, "%sGIT binary patch\n", diff_line_prefix(o));
1203 break;
1204 case DIFF_SYMBOL_BINARY_DIFF_HEADER_DELTA:
1205 fprintf(o->file, "%sdelta %s\n", diff_line_prefix(o), line);
1206 break;
1207 case DIFF_SYMBOL_BINARY_DIFF_HEADER_LITERAL:
1208 fprintf(o->file, "%sliteral %s\n", diff_line_prefix(o), line);
1209 break;
1210 case DIFF_SYMBOL_BINARY_DIFF_FOOTER:
1211 fputs(diff_line_prefix(o), o->file);
1212 fputc('\n', o->file);
1213 break;
Stefan Beller5af6ea92017-06-29 17:06:59 -07001214 case DIFF_SYMBOL_REWRITE_DIFF:
1215 fraginfo = diff_get_color(o->use_color, DIFF_FRAGINFO);
1216 reset = diff_get_color_opt(o, DIFF_RESET);
1217 emit_line(o, fraginfo, reset, line, len);
1218 break;
Stefan Bellerf3597132017-06-29 17:07:00 -07001219 case DIFF_SYMBOL_SUBMODULE_ADD:
1220 set = diff_get_color_opt(o, DIFF_FILE_NEW);
1221 reset = diff_get_color_opt(o, DIFF_RESET);
1222 emit_line(o, set, reset, line, len);
1223 break;
1224 case DIFF_SYMBOL_SUBMODULE_DEL:
1225 set = diff_get_color_opt(o, DIFF_FILE_OLD);
1226 reset = diff_get_color_opt(o, DIFF_RESET);
1227 emit_line(o, set, reset, line, len);
1228 break;
1229 case DIFF_SYMBOL_SUBMODULE_UNTRACKED:
1230 fprintf(o->file, "%sSubmodule %s contains untracked content\n",
1231 diff_line_prefix(o), line);
1232 break;
1233 case DIFF_SYMBOL_SUBMODULE_MODIFIED:
1234 fprintf(o->file, "%sSubmodule %s contains modified content\n",
1235 diff_line_prefix(o), line);
1236 break;
Stefan Beller0911c472017-06-29 17:07:02 -07001237 case DIFF_SYMBOL_STATS_SUMMARY_NO_FILES:
1238 emit_line(o, "", "", " 0 files changed\n",
1239 strlen(" 0 files changed\n"));
1240 break;
1241 case DIFF_SYMBOL_STATS_SUMMARY_ABBREV:
1242 emit_line(o, "", "", " ...\n", strlen(" ...\n"));
1243 break;
Stefan Bellerbd033292017-06-29 17:07:03 -07001244 case DIFF_SYMBOL_WORD_DIFF:
1245 fprintf(o->file, "%.*s", len, line);
1246 break;
Stefan Beller30b7e1e2017-06-29 17:07:04 -07001247 case DIFF_SYMBOL_STAT_SEP:
1248 fputs(o->stat_sep, o->file);
1249 break;
Stefan Beller36a4cef2017-06-29 17:06:49 -07001250 default:
1251 die("BUG: unknown diff symbol");
1252 }
Stefan Beller0911c472017-06-29 17:07:02 -07001253 strbuf_release(&sb);
Stefan Beller36a4cef2017-06-29 17:06:49 -07001254}
1255
Stefan Bellere6e045f2017-06-29 17:07:06 -07001256static void emit_diff_symbol(struct diff_options *o, enum diff_symbol s,
1257 const char *line, int len, unsigned flags)
1258{
1259 struct emitted_diff_symbol e = {line, len, flags, s};
1260
1261 if (o->emitted_symbols)
1262 append_emitted_diff_symbol(o, &e);
1263 else
1264 emit_diff_symbol_from_struct(o, &e);
1265}
1266
Stefan Bellerf3597132017-06-29 17:07:00 -07001267void diff_emit_submodule_del(struct diff_options *o, const char *line)
1268{
1269 emit_diff_symbol(o, DIFF_SYMBOL_SUBMODULE_DEL, line, strlen(line), 0);
1270}
1271
1272void diff_emit_submodule_add(struct diff_options *o, const char *line)
1273{
1274 emit_diff_symbol(o, DIFF_SYMBOL_SUBMODULE_ADD, line, strlen(line), 0);
1275}
1276
1277void diff_emit_submodule_untracked(struct diff_options *o, const char *path)
1278{
1279 emit_diff_symbol(o, DIFF_SYMBOL_SUBMODULE_UNTRACKED,
1280 path, strlen(path), 0);
1281}
1282
1283void diff_emit_submodule_modified(struct diff_options *o, const char *path)
1284{
1285 emit_diff_symbol(o, DIFF_SYMBOL_SUBMODULE_MODIFIED,
1286 path, strlen(path), 0);
1287}
1288
1289void diff_emit_submodule_header(struct diff_options *o, const char *header)
1290{
1291 emit_diff_symbol(o, DIFF_SYMBOL_SUBMODULE_HEADER,
1292 header, strlen(header), 0);
1293}
1294
1295void diff_emit_submodule_error(struct diff_options *o, const char *err)
1296{
1297 emit_diff_symbol(o, DIFF_SYMBOL_SUBMODULE_ERROR, err, strlen(err), 0);
1298}
1299
1300void diff_emit_submodule_pipethrough(struct diff_options *o,
1301 const char *line, int len)
1302{
1303 emit_diff_symbol(o, DIFF_SYMBOL_SUBMODULE_PIPETHROUGH, line, len, 0);
1304}
1305
Junio C Hamano6957eb92009-09-14 18:44:01 -07001306static int new_blank_line_at_eof(struct emit_callback *ecbdata, const char *line, int len)
1307{
1308 if (!((ecbdata->ws_rule & WS_BLANK_AT_EOF) &&
1309 ecbdata->blank_at_eof_in_preimage &&
1310 ecbdata->blank_at_eof_in_postimage &&
1311 ecbdata->blank_at_eof_in_preimage <= ecbdata->lno_in_preimage &&
1312 ecbdata->blank_at_eof_in_postimage <= ecbdata->lno_in_postimage))
1313 return 0;
Junio C Hamano018cff72009-09-14 18:44:01 -07001314 return ws_blank_line(line, len, ecbdata->ws_rule);
Junio C Hamano6957eb92009-09-14 18:44:01 -07001315}
1316
Junio C Hamano018cff72009-09-14 18:44:01 -07001317static void emit_add_line(const char *reset,
1318 struct emit_callback *ecbdata,
1319 const char *line, int len)
Junio C Hamano6957eb92009-09-14 18:44:01 -07001320{
Stefan Beller091f8e22017-06-29 17:06:53 -07001321 unsigned flags = WSEH_NEW | ecbdata->ws_rule;
1322 if (new_blank_line_at_eof(ecbdata, line, len))
1323 flags |= DIFF_SYMBOL_CONTENT_BLANK_LINE_EOF;
1324
1325 emit_diff_symbol(ecbdata->opt, DIFF_SYMBOL_PLUS, line, len, flags);
Junio C Hamano6957eb92009-09-14 18:44:01 -07001326}
Junio C Hamano6957eb92009-09-14 18:44:01 -07001327
Junio C Hamano0e383e12015-05-26 09:56:33 -07001328static void emit_del_line(const char *reset,
1329 struct emit_callback *ecbdata,
1330 const char *line, int len)
1331{
Stefan Beller091f8e22017-06-29 17:06:53 -07001332 unsigned flags = WSEH_OLD | ecbdata->ws_rule;
1333 emit_diff_symbol(ecbdata->opt, DIFF_SYMBOL_MINUS, line, len, flags);
Junio C Hamano0e383e12015-05-26 09:56:33 -07001334}
1335
1336static void emit_context_line(const char *reset,
1337 struct emit_callback *ecbdata,
1338 const char *line, int len)
1339{
Stefan Beller091f8e22017-06-29 17:06:53 -07001340 unsigned flags = WSEH_CONTEXT | ecbdata->ws_rule;
1341 emit_diff_symbol(ecbdata->opt, DIFF_SYMBOL_CONTEXT, line, len, flags);
Junio C Hamano6957eb92009-09-14 18:44:01 -07001342}
1343
Bert Wesarg89cb73a2009-11-27 07:55:18 +01001344static void emit_hunk_header(struct emit_callback *ecbdata,
1345 const char *line, int len)
1346{
Jeff King8dbf3eb2015-05-27 16:48:46 -04001347 const char *context = diff_get_color(ecbdata->color_diff, DIFF_CONTEXT);
Bert Wesarg89cb73a2009-11-27 07:55:18 +01001348 const char *frag = diff_get_color(ecbdata->color_diff, DIFF_FRAGINFO);
1349 const char *func = diff_get_color(ecbdata->color_diff, DIFF_FUNCINFO);
1350 const char *reset = diff_get_color(ecbdata->color_diff, DIFF_RESET);
1351 static const char atat[2] = { '@', '@' };
1352 const char *cp, *ep;
Bo Yang2efcc972010-05-29 23:32:05 +08001353 struct strbuf msgbuf = STRBUF_INIT;
1354 int org_len = len;
1355 int i = 1;
Bert Wesarg89cb73a2009-11-27 07:55:18 +01001356
1357 /*
1358 * As a hunk header must begin with "@@ -<old>, +<new> @@",
1359 * it always is at least 10 bytes long.
1360 */
1361 if (len < 10 ||
1362 memcmp(line, atat, 2) ||
1363 !(ep = memmem(line + 2, len - 2, atat, 2))) {
Stefan Bellerc64b4202017-06-29 17:06:50 -07001364 emit_diff_symbol(ecbdata->opt,
Stefan Beller091f8e22017-06-29 17:06:53 -07001365 DIFF_SYMBOL_CONTEXT_MARKER, line, len, 0);
Bert Wesarg89cb73a2009-11-27 07:55:18 +01001366 return;
1367 }
1368 ep += 2; /* skip over @@ */
1369
1370 /* The hunk header in fraginfo color */
René Scharfecedc61a2014-07-17 01:38:18 +02001371 strbuf_addstr(&msgbuf, frag);
Bo Yang2efcc972010-05-29 23:32:05 +08001372 strbuf_add(&msgbuf, line, ep - line);
René Scharfecedc61a2014-07-17 01:38:18 +02001373 strbuf_addstr(&msgbuf, reset);
Bo Yang2efcc972010-05-29 23:32:05 +08001374
1375 /*
1376 * trailing "\r\n"
1377 */
1378 for ( ; i < 3; i++)
1379 if (line[len - i] == '\r' || line[len - i] == '\n')
1380 len--;
Bert Wesarg89cb73a2009-11-27 07:55:18 +01001381
1382 /* blank before the func header */
1383 for (cp = ep; ep - line < len; ep++)
1384 if (*ep != ' ' && *ep != '\t')
1385 break;
Bo Yang2efcc972010-05-29 23:32:05 +08001386 if (ep != cp) {
Jeff King8dbf3eb2015-05-27 16:48:46 -04001387 strbuf_addstr(&msgbuf, context);
Bo Yang2efcc972010-05-29 23:32:05 +08001388 strbuf_add(&msgbuf, cp, ep - cp);
René Scharfecedc61a2014-07-17 01:38:18 +02001389 strbuf_addstr(&msgbuf, reset);
Bo Yang2efcc972010-05-29 23:32:05 +08001390 }
Bert Wesarg89cb73a2009-11-27 07:55:18 +01001391
Bo Yang2efcc972010-05-29 23:32:05 +08001392 if (ep < line + len) {
René Scharfecedc61a2014-07-17 01:38:18 +02001393 strbuf_addstr(&msgbuf, func);
Bo Yang2efcc972010-05-29 23:32:05 +08001394 strbuf_add(&msgbuf, ep, line + len - ep);
René Scharfecedc61a2014-07-17 01:38:18 +02001395 strbuf_addstr(&msgbuf, reset);
Bo Yang2efcc972010-05-29 23:32:05 +08001396 }
1397
1398 strbuf_add(&msgbuf, line + len, org_len - len);
Stefan Bellerdfb77282017-06-29 17:06:47 -07001399 strbuf_complete_line(&msgbuf);
Stefan Beller68abc6f2017-06-29 17:06:51 -07001400 emit_diff_symbol(ecbdata->opt,
Stefan Beller091f8e22017-06-29 17:06:53 -07001401 DIFF_SYMBOL_CONTEXT_FRAGINFO, msgbuf.buf, msgbuf.len, 0);
Bo Yang2efcc972010-05-29 23:32:05 +08001402 strbuf_release(&msgbuf);
Bert Wesarg89cb73a2009-11-27 07:55:18 +01001403}
1404
Jeff King479b0ae2009-01-22 00:59:56 -05001405static struct diff_tempfile *claim_diff_tempfile(void) {
1406 int i;
1407 for (i = 0; i < ARRAY_SIZE(diff_temp); i++)
1408 if (!diff_temp[i].name)
1409 return diff_temp + i;
1410 die("BUG: diff is failing to clean up its tempfiles");
1411}
1412
Jeff King479b0ae2009-01-22 00:59:56 -05001413static void remove_tempfile(void)
1414{
1415 int i;
Nazri Ramliya8344ab2009-02-12 21:36:14 +08001416 for (i = 0; i < ARRAY_SIZE(diff_temp); i++) {
Jeff King076aa2c2017-09-05 08:15:08 -04001417 if (is_tempfile_active(diff_temp[i].tempfile))
Michael Haggerty284098f2015-08-12 19:12:01 +02001418 delete_tempfile(&diff_temp[i].tempfile);
Nazri Ramliya8344ab2009-02-12 21:36:14 +08001419 diff_temp[i].name = NULL;
1420 }
Jeff King479b0ae2009-01-22 00:59:56 -05001421}
1422
Stefan Beller5af6ea92017-06-29 17:06:59 -07001423static void add_line_count(struct strbuf *out, int count)
Junio C Hamano6973dca2006-04-21 23:57:45 -07001424{
1425 switch (count) {
1426 case 0:
Stefan Beller5af6ea92017-06-29 17:06:59 -07001427 strbuf_addstr(out, "0,0");
Junio C Hamano6973dca2006-04-21 23:57:45 -07001428 break;
1429 case 1:
Stefan Beller5af6ea92017-06-29 17:06:59 -07001430 strbuf_addstr(out, "1");
Junio C Hamano6973dca2006-04-21 23:57:45 -07001431 break;
1432 default:
Stefan Beller5af6ea92017-06-29 17:06:59 -07001433 strbuf_addf(out, "1,%d", count);
Junio C Hamano6973dca2006-04-21 23:57:45 -07001434 break;
1435 }
1436}
1437
Junio C Hamano7f7ee2f2009-09-14 18:44:01 -07001438static void emit_rewrite_lines(struct emit_callback *ecb,
1439 int prefix, const char *data, int size)
Junio C Hamano6973dca2006-04-21 23:57:45 -07001440{
Junio C Hamano7f7ee2f2009-09-14 18:44:01 -07001441 const char *endp = NULL;
Junio C Hamano7f7ee2f2009-09-14 18:44:01 -07001442 const char *reset = diff_get_color(ecb->color_diff, DIFF_RESET);
1443
1444 while (0 < size) {
1445 int len;
1446
1447 endp = memchr(data, '\n', size);
1448 len = endp ? (endp - data + 1) : size;
1449 if (prefix != '+') {
1450 ecb->lno_in_preimage++;
Junio C Hamano0e383e12015-05-26 09:56:33 -07001451 emit_del_line(reset, ecb, data, len);
Junio C Hamano7f7ee2f2009-09-14 18:44:01 -07001452 } else {
1453 ecb->lno_in_postimage++;
1454 emit_add_line(reset, ecb, data, len);
Johannes Schindelin13e36ec2007-02-20 15:08:46 +01001455 }
Junio C Hamano7f7ee2f2009-09-14 18:44:01 -07001456 size -= len;
1457 data += len;
Junio C Hamano6973dca2006-04-21 23:57:45 -07001458 }
Stefan Bellerb9cbfde2017-06-29 17:06:52 -07001459 if (!endp)
Stefan Beller091f8e22017-06-29 17:06:53 -07001460 emit_diff_symbol(ecb->opt, DIFF_SYMBOL_NO_LF_EOF, NULL, 0, 0);
Junio C Hamano6973dca2006-04-21 23:57:45 -07001461}
1462
1463static void emit_rewrite_diff(const char *name_a,
1464 const char *name_b,
1465 struct diff_filespec *one,
Johannes Schindelin13e36ec2007-02-20 15:08:46 +01001466 struct diff_filespec *two,
Jeff Kingd9bae1a2010-04-01 20:12:15 -04001467 struct userdiff_driver *textconv_one,
1468 struct userdiff_driver *textconv_two,
Johannes Schindelineab9a402007-12-18 19:32:14 +00001469 struct diff_options *o)
Junio C Hamano6973dca2006-04-21 23:57:45 -07001470{
1471 int lc_a, lc_b;
Junio C Hamanod5625092007-12-26 17:13:36 -08001472 static struct strbuf a_name = STRBUF_INIT, b_name = STRBUF_INIT;
Junio C Hamanoa5a818e2008-08-18 20:08:09 -07001473 const char *a_prefix, *b_prefix;
Jeff King840383b2010-04-01 20:09:26 -04001474 char *data_one, *data_two;
Jeff King3aa1f7c2008-12-09 03:13:21 -05001475 size_t size_one, size_two;
Junio C Hamano7f7ee2f2009-09-14 18:44:01 -07001476 struct emit_callback ecbdata;
Stefan Beller5af6ea92017-06-29 17:06:59 -07001477 struct strbuf out = STRBUF_INIT;
Junio C Hamanoa5a818e2008-08-18 20:08:09 -07001478
1479 if (diff_mnemonic_prefix && DIFF_OPT_TST(o, REVERSE_DIFF)) {
1480 a_prefix = o->b_prefix;
1481 b_prefix = o->a_prefix;
1482 } else {
1483 a_prefix = o->a_prefix;
1484 b_prefix = o->b_prefix;
1485 }
Junio C Hamano1a9eb3b2006-09-22 16:17:58 -07001486
Junio C Hamano8a13bec2007-02-24 01:42:06 -08001487 name_a += (*name_a == '/');
1488 name_b += (*name_b == '/');
Junio C Hamano1a9eb3b2006-09-22 16:17:58 -07001489
Junio C Hamanod5625092007-12-26 17:13:36 -08001490 strbuf_reset(&a_name);
1491 strbuf_reset(&b_name);
Junio C Hamanoa5a818e2008-08-18 20:08:09 -07001492 quote_two_c_style(&a_name, a_prefix, name_a, 0);
1493 quote_two_c_style(&b_name, b_prefix, name_b, 0);
Junio C Hamanod5625092007-12-26 17:13:36 -08001494
Jeff King840383b2010-04-01 20:09:26 -04001495 size_one = fill_textconv(textconv_one, one, &data_one);
1496 size_two = fill_textconv(textconv_two, two, &data_two);
Jeff King3aa1f7c2008-12-09 03:13:21 -05001497
Junio C Hamanod91ba8f2009-09-15 11:21:10 -07001498 memset(&ecbdata, 0, sizeof(ecbdata));
Jeff Kingdaa0c3d2011-08-17 22:04:23 -07001499 ecbdata.color_diff = want_color(o->use_color);
Stefan Bellerc189c4f2013-08-08 20:31:44 +02001500 ecbdata.ws_rule = whitespace_rule(name_b);
Bo Yanga3c158d2010-05-26 15:08:02 +08001501 ecbdata.opt = o;
Junio C Hamanod91ba8f2009-09-15 11:21:10 -07001502 if (ecbdata.ws_rule & WS_BLANK_AT_EOF) {
1503 mmfile_t mf1, mf2;
1504 mf1.ptr = (char *)data_one;
1505 mf2.ptr = (char *)data_two;
1506 mf1.size = size_one;
1507 mf2.size = size_two;
1508 check_blank_at_eof(&mf1, &mf2, &ecbdata);
1509 }
1510 ecbdata.lno_in_preimage = 1;
1511 ecbdata.lno_in_postimage = 1;
1512
Jeff King3aa1f7c2008-12-09 03:13:21 -05001513 lc_a = count_lines(data_one, size_one);
1514 lc_b = count_lines(data_two, size_two);
Stefan Beller3ee8b7b2017-06-29 17:06:56 -07001515
1516 emit_diff_symbol(o, DIFF_SYMBOL_FILEPAIR_MINUS,
1517 a_name.buf, a_name.len, 0);
1518 emit_diff_symbol(o, DIFF_SYMBOL_FILEPAIR_PLUS,
1519 b_name.buf, b_name.len, 0);
1520
Stefan Beller5af6ea92017-06-29 17:06:59 -07001521 strbuf_addstr(&out, "@@ -");
Junio C Hamano467ddc12011-02-28 16:11:55 -08001522 if (!o->irreversible_delete)
Stefan Beller5af6ea92017-06-29 17:06:59 -07001523 add_line_count(&out, lc_a);
Junio C Hamano467ddc12011-02-28 16:11:55 -08001524 else
Stefan Beller5af6ea92017-06-29 17:06:59 -07001525 strbuf_addstr(&out, "?,?");
1526 strbuf_addstr(&out, " +");
1527 add_line_count(&out, lc_b);
1528 strbuf_addstr(&out, " @@\n");
1529 emit_diff_symbol(o, DIFF_SYMBOL_REWRITE_DIFF, out.buf, out.len, 0);
1530 strbuf_release(&out);
1531
Junio C Hamano467ddc12011-02-28 16:11:55 -08001532 if (lc_a && !o->irreversible_delete)
Junio C Hamanod91ba8f2009-09-15 11:21:10 -07001533 emit_rewrite_lines(&ecbdata, '-', data_one, size_one);
Junio C Hamano6973dca2006-04-21 23:57:45 -07001534 if (lc_b)
Junio C Hamanod91ba8f2009-09-15 11:21:10 -07001535 emit_rewrite_lines(&ecbdata, '+', data_two, size_two);
Jeff Kingb76c0562010-04-01 20:04:14 -04001536 if (textconv_one)
Junio C Hamanoaed6ca52010-04-08 23:30:49 -07001537 free((char *)data_one);
Jeff Kingb76c0562010-04-01 20:04:14 -04001538 if (textconv_two)
Junio C Hamanoaed6ca52010-04-08 23:30:49 -07001539 free((char *)data_two);
Junio C Hamano6973dca2006-04-21 23:57:45 -07001540}
1541
Johannes Schindelinf59a59e2006-07-28 23:56:15 +02001542struct diff_words_buffer {
1543 mmfile_t text;
Ramsay Jones071bcaa2017-09-21 17:49:38 +01001544 unsigned long alloc;
Johannes Schindelin2e5d2002009-01-17 17:29:44 +01001545 struct diff_words_orig {
1546 const char *begin, *end;
1547 } *orig;
1548 int orig_nr, orig_alloc;
Johannes Schindelinf59a59e2006-07-28 23:56:15 +02001549};
1550
1551static void diff_words_append(char *line, unsigned long len,
1552 struct diff_words_buffer *buffer)
1553{
Johannes Schindelin23c15752009-01-17 17:29:43 +01001554 ALLOC_GROW(buffer->text.ptr, buffer->text.size + len, buffer->alloc);
Johannes Schindelinf59a59e2006-07-28 23:56:15 +02001555 line++;
1556 len--;
1557 memcpy(buffer->text.ptr + buffer->text.size, line, len);
1558 buffer->text.size += len;
Johannes Schindelin2b6a5412009-01-17 17:29:45 +01001559 buffer->text.ptr[buffer->text.size] = '\0';
Johannes Schindelinf59a59e2006-07-28 23:56:15 +02001560}
1561
Jonathan Nieder9cba13c2011-03-16 02:08:34 -05001562struct diff_words_style_elem {
Thomas Rast882749a2010-04-14 17:59:06 +02001563 const char *prefix;
1564 const char *suffix;
1565 const char *color; /* NULL; filled in by the setup code if
1566 * color is enabled */
1567};
1568
Jonathan Nieder9cba13c2011-03-16 02:08:34 -05001569struct diff_words_style {
Thomas Rast882749a2010-04-14 17:59:06 +02001570 enum diff_words_type type;
1571 struct diff_words_style_elem new, old, ctx;
1572 const char *newline;
1573};
1574
Stephen Boydc2e86ad2011-03-22 00:51:05 -07001575static struct diff_words_style diff_words_styles[] = {
Thomas Rast882749a2010-04-14 17:59:06 +02001576 { DIFF_WORDS_PORCELAIN, {"+", "\n"}, {"-", "\n"}, {" ", "\n"}, "~\n" },
1577 { DIFF_WORDS_PLAIN, {"{+", "+}"}, {"[-", "-]"}, {"", ""}, "\n" },
1578 { DIFF_WORDS_COLOR, {"", ""}, {"", ""}, {"", ""}, "\n" }
1579};
1580
Johannes Schindelinf59a59e2006-07-28 23:56:15 +02001581struct diff_words_data {
Johannes Schindelinf59a59e2006-07-28 23:56:15 +02001582 struct diff_words_buffer minus, plus;
Johannes Schindelin2e5d2002009-01-17 17:29:44 +01001583 const char *current_plus;
Bo Yang4297c0a2010-05-29 23:32:06 +08001584 int last_minus;
1585 struct diff_options *opt;
Johannes Schindelin2b6a5412009-01-17 17:29:45 +01001586 regex_t *word_regex;
Thomas Rast882749a2010-04-14 17:59:06 +02001587 enum diff_words_type type;
1588 struct diff_words_style *style;
Johannes Schindelinf59a59e2006-07-28 23:56:15 +02001589};
1590
Stefan Bellerbd033292017-06-29 17:07:03 -07001591static int fn_out_diff_words_write_helper(struct diff_options *o,
Thomas Rast882749a2010-04-14 17:59:06 +02001592 struct diff_words_style_elem *st_el,
1593 const char *newline,
Stefan Bellerbd033292017-06-29 17:07:03 -07001594 size_t count, const char *buf)
Thomas Rast882749a2010-04-14 17:59:06 +02001595{
Bo Yang4297c0a2010-05-29 23:32:06 +08001596 int print = 0;
Stefan Bellerbd033292017-06-29 17:07:03 -07001597 struct strbuf sb = STRBUF_INIT;
Bo Yang4297c0a2010-05-29 23:32:06 +08001598
Thomas Rast882749a2010-04-14 17:59:06 +02001599 while (count) {
1600 char *p = memchr(buf, '\n', count);
Bo Yang4297c0a2010-05-29 23:32:06 +08001601 if (print)
Stefan Bellerbd033292017-06-29 17:07:03 -07001602 strbuf_addstr(&sb, diff_line_prefix(o));
1603
Thomas Rast882749a2010-04-14 17:59:06 +02001604 if (p != buf) {
Stefan Bellerbd033292017-06-29 17:07:03 -07001605 const char *reset = st_el->color && *st_el->color ?
1606 GIT_COLOR_RESET : NULL;
1607 if (st_el->color && *st_el->color)
1608 strbuf_addstr(&sb, st_el->color);
1609 strbuf_addstr(&sb, st_el->prefix);
1610 strbuf_add(&sb, buf, p ? p - buf : count);
1611 strbuf_addstr(&sb, st_el->suffix);
1612 if (reset)
1613 strbuf_addstr(&sb, reset);
Thomas Rast882749a2010-04-14 17:59:06 +02001614 }
1615 if (!p)
Stefan Bellerbd033292017-06-29 17:07:03 -07001616 goto out;
1617
1618 strbuf_addstr(&sb, newline);
Thomas Rast882749a2010-04-14 17:59:06 +02001619 count -= p + 1 - buf;
1620 buf = p + 1;
Bo Yang4297c0a2010-05-29 23:32:06 +08001621 print = 1;
Stefan Bellerbd033292017-06-29 17:07:03 -07001622 if (count) {
1623 emit_diff_symbol(o, DIFF_SYMBOL_WORD_DIFF,
1624 sb.buf, sb.len, 0);
1625 strbuf_reset(&sb);
1626 }
Thomas Rast882749a2010-04-14 17:59:06 +02001627 }
Stefan Bellerbd033292017-06-29 17:07:03 -07001628
1629out:
1630 if (sb.len)
1631 emit_diff_symbol(o, DIFF_SYMBOL_WORD_DIFF,
1632 sb.buf, sb.len, 0);
1633 strbuf_release(&sb);
Thomas Rast882749a2010-04-14 17:59:06 +02001634 return 0;
1635}
1636
Bo Yang4297c0a2010-05-29 23:32:06 +08001637/*
1638 * '--color-words' algorithm can be described as:
1639 *
René Genz56217602017-04-30 17:48:27 +02001640 * 1. collect the minus/plus lines of a diff hunk, divided into
Bo Yang4297c0a2010-05-29 23:32:06 +08001641 * minus-lines and plus-lines;
1642 *
1643 * 2. break both minus-lines and plus-lines into words and
1644 * place them into two mmfile_t with one word for each line;
1645 *
1646 * 3. use xdiff to run diff on the two mmfile_t to get the words level diff;
1647 *
1648 * And for the common parts of the both file, we output the plus side text.
1649 * diff_words->current_plus is used to trace the current position of the plus file
1650 * which printed. diff_words->last_minus is used to trace the last minus word
1651 * printed.
1652 *
1653 * For '--graph' to work with '--color-words', we need to output the graph prefix
1654 * on each line of color words output. Generally, there are two conditions on
1655 * which we should output the prefix.
1656 *
1657 * 1. diff_words->last_minus == 0 &&
1658 * diff_words->current_plus == diff_words->plus.text.ptr
1659 *
1660 * that is: the plus text must start as a new line, and if there is no minus
1661 * word printed, a graph prefix must be printed.
1662 *
1663 * 2. diff_words->current_plus > diff_words->plus.text.ptr &&
1664 * *(diff_words->current_plus - 1) == '\n'
1665 *
1666 * that is: a graph prefix must be printed following a '\n'
1667 */
1668static int color_words_output_graph_prefix(struct diff_words_data *diff_words)
1669{
1670 if ((diff_words->last_minus == 0 &&
1671 diff_words->current_plus == diff_words->plus.text.ptr) ||
1672 (diff_words->current_plus > diff_words->plus.text.ptr &&
1673 *(diff_words->current_plus - 1) == '\n')) {
1674 return 1;
1675 } else {
1676 return 0;
1677 }
1678}
1679
Johannes Schindelinf59a59e2006-07-28 23:56:15 +02001680static void fn_out_diff_words_aux(void *priv, char *line, unsigned long len)
1681{
1682 struct diff_words_data *diff_words = priv;
Thomas Rast882749a2010-04-14 17:59:06 +02001683 struct diff_words_style *style = diff_words->style;
Johannes Schindelin2e5d2002009-01-17 17:29:44 +01001684 int minus_first, minus_len, plus_first, plus_len;
1685 const char *minus_begin, *minus_end, *plus_begin, *plus_end;
Bo Yang4297c0a2010-05-29 23:32:06 +08001686 struct diff_options *opt = diff_words->opt;
John Keeping30997bb2013-02-07 20:15:27 +00001687 const char *line_prefix;
Johannes Schindelinf59a59e2006-07-28 23:56:15 +02001688
Johannes Schindelin2e5d2002009-01-17 17:29:44 +01001689 if (line[0] != '@' || parse_hunk_header(line, len,
1690 &minus_first, &minus_len, &plus_first, &plus_len))
1691 return;
Johannes Schindelinf59a59e2006-07-28 23:56:15 +02001692
Bo Yang4297c0a2010-05-29 23:32:06 +08001693 assert(opt);
John Keeping30997bb2013-02-07 20:15:27 +00001694 line_prefix = diff_line_prefix(opt);
Bo Yang4297c0a2010-05-29 23:32:06 +08001695
Johannes Schindelin2e5d2002009-01-17 17:29:44 +01001696 /* POSIX requires that first be decremented by one if len == 0... */
1697 if (minus_len) {
1698 minus_begin = diff_words->minus.orig[minus_first].begin;
1699 minus_end =
1700 diff_words->minus.orig[minus_first + minus_len - 1].end;
1701 } else
1702 minus_begin = minus_end =
1703 diff_words->minus.orig[minus_first].end;
1704
1705 if (plus_len) {
1706 plus_begin = diff_words->plus.orig[plus_first].begin;
1707 plus_end = diff_words->plus.orig[plus_first + plus_len - 1].end;
1708 } else
1709 plus_begin = plus_end = diff_words->plus.orig[plus_first].end;
1710
Bo Yang4297c0a2010-05-29 23:32:06 +08001711 if (color_words_output_graph_prefix(diff_words)) {
1712 fputs(line_prefix, diff_words->opt->file);
1713 }
1714 if (diff_words->current_plus != plus_begin) {
Stefan Bellerbd033292017-06-29 17:07:03 -07001715 fn_out_diff_words_write_helper(diff_words->opt,
Thomas Rast882749a2010-04-14 17:59:06 +02001716 &style->ctx, style->newline,
1717 plus_begin - diff_words->current_plus,
Stefan Bellerbd033292017-06-29 17:07:03 -07001718 diff_words->current_plus);
Bo Yang4297c0a2010-05-29 23:32:06 +08001719 }
1720 if (minus_begin != minus_end) {
Stefan Bellerbd033292017-06-29 17:07:03 -07001721 fn_out_diff_words_write_helper(diff_words->opt,
Thomas Rast882749a2010-04-14 17:59:06 +02001722 &style->old, style->newline,
Stefan Bellerbd033292017-06-29 17:07:03 -07001723 minus_end - minus_begin, minus_begin);
Bo Yang4297c0a2010-05-29 23:32:06 +08001724 }
1725 if (plus_begin != plus_end) {
Stefan Bellerbd033292017-06-29 17:07:03 -07001726 fn_out_diff_words_write_helper(diff_words->opt,
Thomas Rast882749a2010-04-14 17:59:06 +02001727 &style->new, style->newline,
Stefan Bellerbd033292017-06-29 17:07:03 -07001728 plus_end - plus_begin, plus_begin);
Bo Yang4297c0a2010-05-29 23:32:06 +08001729 }
Johannes Schindelin2e5d2002009-01-17 17:29:44 +01001730
1731 diff_words->current_plus = plus_end;
Bo Yang4297c0a2010-05-29 23:32:06 +08001732 diff_words->last_minus = minus_first;
Johannes Schindelinf59a59e2006-07-28 23:56:15 +02001733}
1734
Johannes Schindelin2b6a5412009-01-17 17:29:45 +01001735/* This function starts looking at *begin, and returns 0 iff a word was found. */
1736static int find_word_boundaries(mmfile_t *buffer, regex_t *word_regex,
1737 int *begin, int *end)
1738{
1739 if (word_regex && *begin < buffer->size) {
1740 regmatch_t match[1];
Johannes Schindelinb7d36ff2016-09-21 20:24:14 +02001741 if (!regexec_buf(word_regex, buffer->ptr + *begin,
1742 buffer->size - *begin, 1, match, 0)) {
Johannes Schindelin2b6a5412009-01-17 17:29:45 +01001743 char *p = memchr(buffer->ptr + *begin + match[0].rm_so,
1744 '\n', match[0].rm_eo - match[0].rm_so);
1745 *end = p ? p - buffer->ptr : match[0].rm_eo + *begin;
1746 *begin += match[0].rm_so;
1747 return *begin >= *end;
1748 }
1749 return -1;
Johannes Schindelinf59a59e2006-07-28 23:56:15 +02001750 }
1751
Johannes Schindelin2b6a5412009-01-17 17:29:45 +01001752 /* find the next word */
1753 while (*begin < buffer->size && isspace(buffer->ptr[*begin]))
1754 (*begin)++;
1755 if (*begin >= buffer->size)
1756 return -1;
1757
1758 /* find the end of the word */
1759 *end = *begin + 1;
1760 while (*end < buffer->size && !isspace(buffer->ptr[*end]))
1761 (*end)++;
1762
1763 return 0;
1764}
1765
Johannes Schindelin23c15752009-01-17 17:29:43 +01001766/*
Johannes Schindelin2e5d2002009-01-17 17:29:44 +01001767 * This function splits the words in buffer->text, stores the list with
1768 * newline separator into out, and saves the offsets of the original words
1769 * in buffer->orig.
Johannes Schindelin23c15752009-01-17 17:29:43 +01001770 */
Johannes Schindelin2b6a5412009-01-17 17:29:45 +01001771static void diff_words_fill(struct diff_words_buffer *buffer, mmfile_t *out,
1772 regex_t *word_regex)
Johannes Schindelin23c15752009-01-17 17:29:43 +01001773{
Johannes Schindelin2e5d2002009-01-17 17:29:44 +01001774 int i, j;
Johannes Schindelin2b6a5412009-01-17 17:29:45 +01001775 long alloc = 0;
Johannes Schindelin2e5d2002009-01-17 17:29:44 +01001776
1777 out->size = 0;
Johannes Schindelin2b6a5412009-01-17 17:29:45 +01001778 out->ptr = NULL;
Johannes Schindelin2e5d2002009-01-17 17:29:44 +01001779
1780 /* fake an empty "0th" word */
1781 ALLOC_GROW(buffer->orig, 1, buffer->orig_alloc);
1782 buffer->orig[0].begin = buffer->orig[0].end = buffer->text.ptr;
1783 buffer->orig_nr = 1;
1784
1785 for (i = 0; i < buffer->text.size; i++) {
Johannes Schindelin2b6a5412009-01-17 17:29:45 +01001786 if (find_word_boundaries(&buffer->text, word_regex, &i, &j))
1787 return;
Johannes Schindelin2e5d2002009-01-17 17:29:44 +01001788
1789 /* store original boundaries */
1790 ALLOC_GROW(buffer->orig, buffer->orig_nr + 1,
1791 buffer->orig_alloc);
1792 buffer->orig[buffer->orig_nr].begin = buffer->text.ptr + i;
1793 buffer->orig[buffer->orig_nr].end = buffer->text.ptr + j;
1794 buffer->orig_nr++;
1795
1796 /* store one word */
Johannes Schindelin2b6a5412009-01-17 17:29:45 +01001797 ALLOC_GROW(out->ptr, out->size + j - i + 1, alloc);
Johannes Schindelin2e5d2002009-01-17 17:29:44 +01001798 memcpy(out->ptr + out->size, buffer->text.ptr + i, j - i);
1799 out->ptr[out->size + j - i] = '\n';
1800 out->size += j - i + 1;
1801
1802 i = j - 1;
Johannes Schindelinf59a59e2006-07-28 23:56:15 +02001803 }
1804}
1805
1806/* this executes the word diff on the accumulated buffers */
1807static void diff_words_show(struct diff_words_data *diff_words)
1808{
1809 xpparam_t xpp;
1810 xdemitconf_t xecfg;
Johannes Schindelinf59a59e2006-07-28 23:56:15 +02001811 mmfile_t minus, plus;
Thomas Rast882749a2010-04-14 17:59:06 +02001812 struct diff_words_style *style = diff_words->style;
Johannes Schindelinf59a59e2006-07-28 23:56:15 +02001813
Bo Yang4297c0a2010-05-29 23:32:06 +08001814 struct diff_options *opt = diff_words->opt;
John Keeping30997bb2013-02-07 20:15:27 +00001815 const char *line_prefix;
Bo Yang4297c0a2010-05-29 23:32:06 +08001816
1817 assert(opt);
John Keeping30997bb2013-02-07 20:15:27 +00001818 line_prefix = diff_line_prefix(opt);
Bo Yang4297c0a2010-05-29 23:32:06 +08001819
Johannes Schindelin2e5d2002009-01-17 17:29:44 +01001820 /* special case: only removal */
1821 if (!diff_words->plus.text.size) {
Stefan Bellerbd033292017-06-29 17:07:03 -07001822 emit_diff_symbol(diff_words->opt, DIFF_SYMBOL_WORD_DIFF,
1823 line_prefix, strlen(line_prefix), 0);
1824 fn_out_diff_words_write_helper(diff_words->opt,
Thomas Rast882749a2010-04-14 17:59:06 +02001825 &style->old, style->newline,
Bo Yang4297c0a2010-05-29 23:32:06 +08001826 diff_words->minus.text.size,
Stefan Bellerbd033292017-06-29 17:07:03 -07001827 diff_words->minus.text.ptr);
Johannes Schindelin2e5d2002009-01-17 17:29:44 +01001828 diff_words->minus.text.size = 0;
1829 return;
1830 }
1831
1832 diff_words->current_plus = diff_words->plus.text.ptr;
Bo Yang4297c0a2010-05-29 23:32:06 +08001833 diff_words->last_minus = 0;
Johannes Schindelinf59a59e2006-07-28 23:56:15 +02001834
Brian Downing9ccd0a82008-10-25 15:30:37 +02001835 memset(&xpp, 0, sizeof(xpp));
Johannes Schindelin30b25012007-07-04 19:05:46 +01001836 memset(&xecfg, 0, sizeof(xecfg));
Johannes Schindelin2b6a5412009-01-17 17:29:45 +01001837 diff_words_fill(&diff_words->minus, &minus, diff_words->word_regex);
1838 diff_words_fill(&diff_words->plus, &plus, diff_words->word_regex);
René Scharfe582aa002010-05-02 15:04:41 +02001839 xpp.flags = 0;
Johannes Schindelin2b6a5412009-01-17 17:29:45 +01001840 /* as only the hunk header will be parsed, we need a 0-context */
Johannes Schindelin2e5d2002009-01-17 17:29:44 +01001841 xecfg.ctxlen = 0;
Jeff King3efb9882015-09-24 19:12:23 -04001842 if (xdi_diff_outf(&minus, &plus, fn_out_diff_words_aux, diff_words,
1843 &xpp, &xecfg))
1844 die("unable to generate word diff");
Johannes Schindelinf59a59e2006-07-28 23:56:15 +02001845 free(minus.ptr);
1846 free(plus.ptr);
Johannes Schindelin2e5d2002009-01-17 17:29:44 +01001847 if (diff_words->current_plus != diff_words->plus.text.ptr +
Bo Yang4297c0a2010-05-29 23:32:06 +08001848 diff_words->plus.text.size) {
1849 if (color_words_output_graph_prefix(diff_words))
Stefan Bellerbd033292017-06-29 17:07:03 -07001850 emit_diff_symbol(diff_words->opt, DIFF_SYMBOL_WORD_DIFF,
1851 line_prefix, strlen(line_prefix), 0);
1852 fn_out_diff_words_write_helper(diff_words->opt,
Thomas Rast882749a2010-04-14 17:59:06 +02001853 &style->ctx, style->newline,
Johannes Schindelin2e5d2002009-01-17 17:29:44 +01001854 diff_words->plus.text.ptr + diff_words->plus.text.size
Stefan Bellerbd033292017-06-29 17:07:03 -07001855 - diff_words->current_plus, diff_words->current_plus);
Bo Yang4297c0a2010-05-29 23:32:06 +08001856 }
Johannes Schindelinf59a59e2006-07-28 23:56:15 +02001857 diff_words->minus.text.size = diff_words->plus.text.size = 0;
Johannes Schindelinf59a59e2006-07-28 23:56:15 +02001858}
1859
Junio C Hamano76fd2822009-10-30 10:09:06 -07001860/* In "color-words" mode, show word-diff of words accumulated in the buffer */
1861static void diff_words_flush(struct emit_callback *ecbdata)
1862{
Stefan Bellere6e045f2017-06-29 17:07:06 -07001863 struct diff_options *wo = ecbdata->diff_words->opt;
1864
Junio C Hamano76fd2822009-10-30 10:09:06 -07001865 if (ecbdata->diff_words->minus.text.size ||
1866 ecbdata->diff_words->plus.text.size)
1867 diff_words_show(ecbdata->diff_words);
Stefan Bellere6e045f2017-06-29 17:07:06 -07001868
1869 if (wo->emitted_symbols) {
1870 struct diff_options *o = ecbdata->opt;
1871 struct emitted_diff_symbols *wol = wo->emitted_symbols;
1872 int i;
1873
1874 /*
1875 * NEEDSWORK:
1876 * Instead of appending each, concat all words to a line?
1877 */
1878 for (i = 0; i < wol->nr; i++)
1879 append_emitted_diff_symbol(o, &wol->buf[i]);
1880
1881 for (i = 0; i < wol->nr; i++)
1882 free((void *)wol->buf[i].line);
1883
1884 wol->nr = 0;
1885 }
Junio C Hamano76fd2822009-10-30 10:09:06 -07001886}
1887
Thomas Rast77d1a522012-03-14 19:24:08 +01001888static void diff_filespec_load_driver(struct diff_filespec *one)
1889{
1890 /* Use already-loaded driver */
1891 if (one->driver)
1892 return;
1893
1894 if (S_ISREG(one->mode))
1895 one->driver = userdiff_find_by_path(one->path);
1896
1897 /* Fallback to default settings */
1898 if (!one->driver)
1899 one->driver = userdiff_find_by_name("default");
1900}
1901
1902static const char *userdiff_word_regex(struct diff_filespec *one)
1903{
1904 diff_filespec_load_driver(one);
1905 return one->driver->word_regex;
1906}
1907
1908static void init_diff_words_data(struct emit_callback *ecbdata,
Thomas Rast6440d342012-03-14 19:24:09 +01001909 struct diff_options *orig_opts,
Thomas Rast77d1a522012-03-14 19:24:08 +01001910 struct diff_filespec *one,
1911 struct diff_filespec *two)
1912{
1913 int i;
Thomas Rast6440d342012-03-14 19:24:09 +01001914 struct diff_options *o = xmalloc(sizeof(struct diff_options));
1915 memcpy(o, orig_opts, sizeof(struct diff_options));
Thomas Rast77d1a522012-03-14 19:24:08 +01001916
1917 ecbdata->diff_words =
1918 xcalloc(1, sizeof(struct diff_words_data));
1919 ecbdata->diff_words->type = o->word_diff;
1920 ecbdata->diff_words->opt = o;
Stefan Bellere6e045f2017-06-29 17:07:06 -07001921
1922 if (orig_opts->emitted_symbols)
1923 o->emitted_symbols =
1924 xcalloc(1, sizeof(struct emitted_diff_symbols));
1925
Thomas Rast77d1a522012-03-14 19:24:08 +01001926 if (!o->word_regex)
1927 o->word_regex = userdiff_word_regex(one);
1928 if (!o->word_regex)
1929 o->word_regex = userdiff_word_regex(two);
1930 if (!o->word_regex)
1931 o->word_regex = diff_word_regex_cfg;
1932 if (o->word_regex) {
1933 ecbdata->diff_words->word_regex = (regex_t *)
1934 xmalloc(sizeof(regex_t));
1935 if (regcomp(ecbdata->diff_words->word_regex,
1936 o->word_regex,
1937 REG_EXTENDED | REG_NEWLINE))
1938 die ("Invalid regular expression: %s",
1939 o->word_regex);
1940 }
1941 for (i = 0; i < ARRAY_SIZE(diff_words_styles); i++) {
1942 if (o->word_diff == diff_words_styles[i].type) {
1943 ecbdata->diff_words->style =
1944 &diff_words_styles[i];
1945 break;
1946 }
1947 }
1948 if (want_color(o->use_color)) {
1949 struct diff_words_style *st = ecbdata->diff_words->style;
1950 st->old.color = diff_get_color_opt(o, DIFF_FILE_OLD);
1951 st->new.color = diff_get_color_opt(o, DIFF_FILE_NEW);
Jeff King8dbf3eb2015-05-27 16:48:46 -04001952 st->ctx.color = diff_get_color_opt(o, DIFF_CONTEXT);
Thomas Rast77d1a522012-03-14 19:24:08 +01001953 }
1954}
1955
Johannes Schindelinf59a59e2006-07-28 23:56:15 +02001956static void free_diff_words_data(struct emit_callback *ecbdata)
1957{
1958 if (ecbdata->diff_words) {
Junio C Hamano76fd2822009-10-30 10:09:06 -07001959 diff_words_flush(ecbdata);
Stefan Bellere6e045f2017-06-29 17:07:06 -07001960 free (ecbdata->diff_words->opt->emitted_symbols);
Thomas Rast6440d342012-03-14 19:24:09 +01001961 free (ecbdata->diff_words->opt);
Jim Meyering8e0f7002008-01-31 18:26:32 +01001962 free (ecbdata->diff_words->minus.text.ptr);
Johannes Schindelin2e5d2002009-01-17 17:29:44 +01001963 free (ecbdata->diff_words->minus.orig);
Jim Meyering8e0f7002008-01-31 18:26:32 +01001964 free (ecbdata->diff_words->plus.text.ptr);
Johannes Schindelin2e5d2002009-01-17 17:29:44 +01001965 free (ecbdata->diff_words->plus.orig);
Brandon Caseyef5644e2010-09-09 14:02:45 -05001966 if (ecbdata->diff_words->word_regex) {
1967 regfree(ecbdata->diff_words->word_regex);
1968 free(ecbdata->diff_words->word_regex);
1969 }
Ævar Arnfjörð Bjarmason6a83d902017-06-15 23:15:46 +00001970 FREE_AND_NULL(ecbdata->diff_words);
Johannes Schindelinf59a59e2006-07-28 23:56:15 +02001971 }
1972}
1973
Jeff Kingce436972006-07-23 05:24:18 -04001974const char *diff_get_color(int diff_use_color, enum color_diff ix)
Johannes Schindelincd112ce2006-06-13 18:45:44 +02001975{
Jeff Kingdaa0c3d2011-08-17 22:04:23 -07001976 if (want_color(diff_use_color))
Linus Torvalds50f575f2006-06-22 13:53:31 -07001977 return diff_colors[ix];
1978 return "";
Johannes Schindelincd112ce2006-06-13 18:45:44 +02001979}
1980
John Keepingf1922232013-02-07 20:15:26 +00001981const char *diff_line_prefix(struct diff_options *opt)
1982{
1983 struct strbuf *msgbuf;
1984 if (!opt->output_prefix)
1985 return "";
1986
1987 msgbuf = opt->output_prefix(opt, opt->output_prefix_data);
1988 return msgbuf->buf;
1989}
1990
Junio C Hamano23707812008-01-02 01:50:11 -08001991static unsigned long sane_truncate_line(struct emit_callback *ecb, char *line, unsigned long len)
1992{
1993 const char *cp;
1994 unsigned long allot;
1995 size_t l = len;
1996
Junio C Hamano23707812008-01-02 01:50:11 -08001997 cp = line;
1998 allot = l;
1999 while (0 < l) {
2000 (void) utf8_width(&cp, &l);
2001 if (!cp)
2002 break; /* truncated in the middle? */
2003 }
2004 return allot - l;
2005}
2006
Junio C Hamanod68fe262009-09-14 22:05:57 -07002007static void find_lno(const char *line, struct emit_callback *ecbdata)
Junio C Hamano690ed842009-09-04 00:41:15 -07002008{
Junio C Hamanod68fe262009-09-14 22:05:57 -07002009 const char *p;
2010 ecbdata->lno_in_preimage = 0;
2011 ecbdata->lno_in_postimage = 0;
2012 p = strchr(line, '-');
Junio C Hamano690ed842009-09-04 00:41:15 -07002013 if (!p)
Junio C Hamanod68fe262009-09-14 22:05:57 -07002014 return; /* cannot happen */
2015 ecbdata->lno_in_preimage = strtol(p + 1, NULL, 10);
2016 p = strchr(p, '+');
2017 if (!p)
2018 return; /* cannot happen */
2019 ecbdata->lno_in_postimage = strtol(p + 1, NULL, 10);
Junio C Hamano690ed842009-09-04 00:41:15 -07002020}
2021
Johannes Schindelincd112ce2006-06-13 18:45:44 +02002022static void fn_out_consume(void *priv, char *line, unsigned long len)
Junio C Hamano6973dca2006-04-21 23:57:45 -07002023{
Junio C Hamano6973dca2006-04-21 23:57:45 -07002024 struct emit_callback *ecbdata = priv;
Jeff Kingce436972006-07-23 05:24:18 -04002025 const char *reset = diff_get_color(ecbdata->color_diff, DIFF_RESET);
Bo Yang7be57612010-05-26 15:23:54 +08002026 struct diff_options *o = ecbdata->opt;
Junio C Hamano6973dca2006-04-21 23:57:45 -07002027
Stefan Bellerba162332016-09-07 16:36:46 -07002028 o->found_changes = 1;
2029
Greg Bacon3e97c7c2009-11-19 15:12:24 -06002030 if (ecbdata->header) {
Stefan Bellera29b0a12017-06-29 17:06:57 -07002031 emit_diff_symbol(o, DIFF_SYMBOL_HEADER,
2032 ecbdata->header->buf, ecbdata->header->len, 0);
Greg Bacon3e97c7c2009-11-19 15:12:24 -06002033 strbuf_reset(ecbdata->header);
2034 ecbdata->header = NULL;
2035 }
Johannes Schindelin34a5e1a2007-02-25 23:34:54 +01002036
Junio C Hamano6973dca2006-04-21 23:57:45 -07002037 if (ecbdata->label_path[0]) {
Stefan Beller3ee8b7b2017-06-29 17:06:56 -07002038 emit_diff_symbol(o, DIFF_SYMBOL_FILEPAIR_MINUS,
2039 ecbdata->label_path[0],
2040 strlen(ecbdata->label_path[0]), 0);
2041 emit_diff_symbol(o, DIFF_SYMBOL_FILEPAIR_PLUS,
2042 ecbdata->label_path[1],
2043 strlen(ecbdata->label_path[1]), 0);
Junio C Hamano6973dca2006-04-21 23:57:45 -07002044 ecbdata->label_path[0] = ecbdata->label_path[1] = NULL;
2045 }
Johannes Schindelincd112ce2006-06-13 18:45:44 +02002046
Jim Meyeringa624eaa2008-08-15 13:39:26 +02002047 if (diff_suppress_blank_empty
2048 && len == 2 && line[0] == ' ' && line[1] == '\n') {
2049 line[0] = '\n';
2050 len = 1;
2051 }
2052
Junio C Hamanob8d9c1a2009-09-03 23:59:25 -07002053 if (line[0] == '@') {
Junio C Hamano76fd2822009-10-30 10:09:06 -07002054 if (ecbdata->diff_words)
2055 diff_words_flush(ecbdata);
Junio C Hamano23707812008-01-02 01:50:11 -08002056 len = sane_truncate_line(ecbdata, line, len);
Junio C Hamanod68fe262009-09-14 22:05:57 -07002057 find_lno(line, ecbdata);
Bert Wesarg89cb73a2009-11-27 07:55:18 +01002058 emit_hunk_header(ecbdata, line, len);
Junio C Hamano448c3ef2006-09-22 22:48:39 -07002059 return;
Johannes Schindelincd112ce2006-06-13 18:45:44 +02002060 }
Junio C Hamano448c3ef2006-09-22 22:48:39 -07002061
Junio C Hamano448c3ef2006-09-22 22:48:39 -07002062 if (ecbdata->diff_words) {
Stefan Bellerff958672017-06-29 17:06:54 -07002063 enum diff_symbol s =
2064 ecbdata->diff_words->type == DIFF_WORDS_PORCELAIN ?
2065 DIFF_SYMBOL_WORDS_PORCELAIN : DIFF_SYMBOL_WORDS;
Junio C Hamano448c3ef2006-09-22 22:48:39 -07002066 if (line[0] == '-') {
2067 diff_words_append(line, len,
2068 &ecbdata->diff_words->minus);
2069 return;
2070 } else if (line[0] == '+') {
2071 diff_words_append(line, len,
2072 &ecbdata->diff_words->plus);
2073 return;
Christian Couder59556542013-11-30 21:55:40 +01002074 } else if (starts_with(line, "\\ ")) {
Thomas Rastc7c2bc02012-01-12 12:15:33 +01002075 /*
2076 * Eat the "no newline at eof" marker as if we
2077 * saw a "+" or "-" line with nothing on it,
2078 * and return without diff_words_flush() to
2079 * defer processing. If this is the end of
2080 * preimage, more "+" lines may come after it.
2081 */
2082 return;
Junio C Hamano448c3ef2006-09-22 22:48:39 -07002083 }
Junio C Hamano76fd2822009-10-30 10:09:06 -07002084 diff_words_flush(ecbdata);
Stefan Bellerff958672017-06-29 17:06:54 -07002085 emit_diff_symbol(o, s, line, len, 0);
Junio C Hamano448c3ef2006-09-22 22:48:39 -07002086 return;
2087 }
Junio C Hamano448c3ef2006-09-22 22:48:39 -07002088
Junio C Hamano0e383e12015-05-26 09:56:33 -07002089 switch (line[0]) {
2090 case '+':
Junio C Hamanod68fe262009-09-14 22:05:57 -07002091 ecbdata->lno_in_postimage++;
Junio C Hamano018cff72009-09-14 18:44:01 -07002092 emit_add_line(reset, ecbdata, line + 1, len - 1);
Junio C Hamano0e383e12015-05-26 09:56:33 -07002093 break;
2094 case '-':
2095 ecbdata->lno_in_preimage++;
2096 emit_del_line(reset, ecbdata, line + 1, len - 1);
2097 break;
2098 case ' ':
2099 ecbdata->lno_in_postimage++;
2100 ecbdata->lno_in_preimage++;
2101 emit_context_line(reset, ecbdata, line + 1, len - 1);
2102 break;
2103 default:
2104 /* incomplete line at the end */
2105 ecbdata->lno_in_preimage++;
Stefan Bellerf2bb1212017-06-29 17:06:55 -07002106 emit_diff_symbol(o, DIFF_SYMBOL_CONTEXT_INCOMPLETE,
2107 line, len, 0);
Junio C Hamano0e383e12015-05-26 09:56:33 -07002108 break;
Junio C Hamano448c3ef2006-09-22 22:48:39 -07002109 }
Junio C Hamano6973dca2006-04-21 23:57:45 -07002110}
2111
2112static char *pprint_rename(const char *a, const char *b)
2113{
2114 const char *old = a;
2115 const char *new = b;
Brandon Caseyf285a2d2008-10-09 14:12:12 -05002116 struct strbuf name = STRBUF_INIT;
Junio C Hamano6973dca2006-04-21 23:57:45 -07002117 int pfx_length, sfx_length;
Thomas Rastdd281f02013-02-26 21:47:01 +01002118 int pfx_adjust_for_slash;
Junio C Hamano6973dca2006-04-21 23:57:45 -07002119 int len_a = strlen(a);
2120 int len_b = strlen(b);
Pierre Habouzit663af342007-09-20 00:42:15 +02002121 int a_midlen, b_midlen;
Alexandre Julliarde5bfbf92007-02-10 15:39:00 +01002122 int qlen_a = quote_c_style(a, NULL, NULL, 0);
2123 int qlen_b = quote_c_style(b, NULL, NULL, 0);
2124
2125 if (qlen_a || qlen_b) {
Pierre Habouzit663af342007-09-20 00:42:15 +02002126 quote_c_style(a, &name, NULL, 0);
2127 strbuf_addstr(&name, " => ");
2128 quote_c_style(b, &name, NULL, 0);
Pierre Habouzitb315c5c2007-09-27 12:58:23 +02002129 return strbuf_detach(&name, NULL);
Alexandre Julliarde5bfbf92007-02-10 15:39:00 +01002130 }
Junio C Hamano6973dca2006-04-21 23:57:45 -07002131
2132 /* Find common prefix */
2133 pfx_length = 0;
2134 while (*old && *new && *old == *new) {
2135 if (*old == '/')
2136 pfx_length = old - a + 1;
2137 old++;
2138 new++;
2139 }
2140
2141 /* Find common suffix */
2142 old = a + len_a;
2143 new = b + len_b;
2144 sfx_length = 0;
Antoine Pelissed020e272013-02-23 17:48:45 +01002145 /*
Thomas Rastdd281f02013-02-26 21:47:01 +01002146 * If there is a common prefix, it must end in a slash. In
2147 * that case we let this loop run 1 into the prefix to see the
2148 * same slash.
2149 *
2150 * If there is no common prefix, we cannot do this as it would
2151 * underrun the input strings.
Antoine Pelissed020e272013-02-23 17:48:45 +01002152 */
Thomas Rastdd281f02013-02-26 21:47:01 +01002153 pfx_adjust_for_slash = (pfx_length ? 1 : 0);
2154 while (a + pfx_length - pfx_adjust_for_slash <= old &&
2155 b + pfx_length - pfx_adjust_for_slash <= new &&
Antoine Pelissed020e272013-02-23 17:48:45 +01002156 *old == *new) {
Junio C Hamano6973dca2006-04-21 23:57:45 -07002157 if (*old == '/')
2158 sfx_length = len_a - (old - a);
2159 old--;
2160 new--;
2161 }
2162
2163 /*
2164 * pfx{mid-a => mid-b}sfx
2165 * {pfx-a => pfx-b}sfx
2166 * pfx{sfx-a => sfx-b}
2167 * name-a => name-b
2168 */
Pierre Habouzit663af342007-09-20 00:42:15 +02002169 a_midlen = len_a - pfx_length - sfx_length;
2170 b_midlen = len_b - pfx_length - sfx_length;
2171 if (a_midlen < 0)
2172 a_midlen = 0;
2173 if (b_midlen < 0)
2174 b_midlen = 0;
Junio C Hamanocc908b82006-05-14 22:07:28 -07002175
Pierre Habouzit663af342007-09-20 00:42:15 +02002176 strbuf_grow(&name, pfx_length + a_midlen + b_midlen + sfx_length + 7);
2177 if (pfx_length + sfx_length) {
2178 strbuf_add(&name, a, pfx_length);
2179 strbuf_addch(&name, '{');
Junio C Hamano6973dca2006-04-21 23:57:45 -07002180 }
Pierre Habouzit663af342007-09-20 00:42:15 +02002181 strbuf_add(&name, a + pfx_length, a_midlen);
2182 strbuf_addstr(&name, " => ");
2183 strbuf_add(&name, b + pfx_length, b_midlen);
2184 if (pfx_length + sfx_length) {
2185 strbuf_addch(&name, '}');
2186 strbuf_add(&name, a + len_a - sfx_length, sfx_length);
Junio C Hamano6973dca2006-04-21 23:57:45 -07002187 }
Pierre Habouzitb315c5c2007-09-27 12:58:23 +02002188 return strbuf_detach(&name, NULL);
Junio C Hamano6973dca2006-04-21 23:57:45 -07002189}
2190
2191struct diffstat_t {
Junio C Hamano6973dca2006-04-21 23:57:45 -07002192 int nr;
2193 int alloc;
2194 struct diffstat_file {
Junio C Hamanof6046522007-12-11 23:46:30 -08002195 char *from_name;
Junio C Hamano6973dca2006-04-21 23:57:45 -07002196 char *name;
Junio C Hamanof6046522007-12-11 23:46:30 -08002197 char *print_name;
Junio C Hamano6973dca2006-04-21 23:57:45 -07002198 unsigned is_unmerged:1;
2199 unsigned is_binary:1;
2200 unsigned is_renamed:1;
Linus Torvalds74faaa12012-10-17 10:00:37 -07002201 unsigned is_interesting:1;
Jeff King0974c112010-04-17 13:41:08 -04002202 uintmax_t added, deleted;
Junio C Hamano6973dca2006-04-21 23:57:45 -07002203 } **files;
2204};
2205
2206static struct diffstat_file *diffstat_add(struct diffstat_t *diffstat,
2207 const char *name_a,
2208 const char *name_b)
2209{
2210 struct diffstat_file *x;
Brian Gesiak1a4927c2014-05-27 00:33:47 +09002211 x = xcalloc(1, sizeof(*x));
Dmitry S. Dolzhenko4c960a42014-03-04 02:31:53 +04002212 ALLOC_GROW(diffstat->files, diffstat->nr + 1, diffstat->alloc);
Junio C Hamano6973dca2006-04-21 23:57:45 -07002213 diffstat->files[diffstat->nr++] = x;
2214 if (name_b) {
Junio C Hamanof6046522007-12-11 23:46:30 -08002215 x->from_name = xstrdup(name_a);
2216 x->name = xstrdup(name_b);
Junio C Hamano6973dca2006-04-21 23:57:45 -07002217 x->is_renamed = 1;
2218 }
Junio C Hamanof6046522007-12-11 23:46:30 -08002219 else {
2220 x->from_name = NULL;
Shawn Pearce9befac42006-09-02 00:16:31 -04002221 x->name = xstrdup(name_a);
Junio C Hamanof6046522007-12-11 23:46:30 -08002222 }
Junio C Hamano6973dca2006-04-21 23:57:45 -07002223 return x;
2224}
2225
2226static void diffstat_consume(void *priv, char *line, unsigned long len)
2227{
2228 struct diffstat_t *diffstat = priv;
2229 struct diffstat_file *x = diffstat->files[diffstat->nr - 1];
2230
2231 if (line[0] == '+')
2232 x->added++;
2233 else if (line[0] == '-')
2234 x->deleted++;
2235}
2236
Johannes Schindelin698ce6f2006-05-20 15:40:29 +02002237const char mime_boundary_leader[] = "------------";
Junio C Hamano6973dca2006-04-21 23:57:45 -07002238
Junio C Hamanoa2540022006-09-26 18:53:02 -07002239static int scale_linear(int it, int width, int max_change)
2240{
Junio C Hamano2eeeef22012-02-14 13:49:11 -08002241 if (!it)
2242 return 0;
Junio C Hamanoa2540022006-09-26 18:53:02 -07002243 /*
Junio C Hamano2eeeef22012-02-14 13:49:11 -08002244 * make sure that at least one '-' or '+' is printed if
2245 * there is any change to this path. The easiest way is to
2246 * scale linearly as if the alloted width is one column shorter
2247 * than it is, and then add 1 to the result.
Junio C Hamanoa2540022006-09-26 18:53:02 -07002248 */
Junio C Hamano2eeeef22012-02-14 13:49:11 -08002249 return 1 + (it * (width - 1) / max_change);
Junio C Hamanoa2540022006-09-26 18:53:02 -07002250}
2251
Stefan Beller0911c472017-06-29 17:07:02 -07002252static void show_graph(struct strbuf *out, char ch, int cnt,
2253 const char *set, const char *reset)
Junio C Hamanoa2540022006-09-26 18:53:02 -07002254{
2255 if (cnt <= 0)
2256 return;
Stefan Beller0911c472017-06-29 17:07:02 -07002257 strbuf_addstr(out, set);
2258 strbuf_addchars(out, ch, cnt);
2259 strbuf_addstr(out, reset);
Junio C Hamanoa2540022006-09-26 18:53:02 -07002260}
2261
Junio C Hamanof6046522007-12-11 23:46:30 -08002262static void fill_print_name(struct diffstat_file *file)
2263{
2264 char *pname;
2265
2266 if (file->print_name)
2267 return;
2268
2269 if (!file->is_renamed) {
Brandon Caseyf285a2d2008-10-09 14:12:12 -05002270 struct strbuf buf = STRBUF_INIT;
Junio C Hamanof6046522007-12-11 23:46:30 -08002271 if (quote_c_style(file->name, &buf, NULL, 0)) {
2272 pname = strbuf_detach(&buf, NULL);
2273 } else {
2274 pname = file->name;
2275 strbuf_release(&buf);
2276 }
2277 } else {
2278 pname = pprint_rename(file->from_name, file->name);
2279 }
2280 file->print_name = pname;
2281}
2282
Stefan Beller0911c472017-06-29 17:07:02 -07002283static void print_stat_summary_inserts_deletes(struct diff_options *options,
2284 int files, int insertions, int deletions)
Nguyễn Thái Ngọc Duy7f814632012-02-01 19:55:07 +07002285{
2286 struct strbuf sb = STRBUF_INIT;
Nguyễn Thái Ngọc Duy7f814632012-02-01 19:55:07 +07002287
2288 if (!files) {
2289 assert(insertions == 0 && deletions == 0);
Stefan Beller0911c472017-06-29 17:07:02 -07002290 emit_diff_symbol(options, DIFF_SYMBOL_STATS_SUMMARY_NO_FILES,
2291 NULL, 0, 0);
2292 return;
Nguyễn Thái Ngọc Duy7f814632012-02-01 19:55:07 +07002293 }
2294
2295 strbuf_addf(&sb,
Nguyễn Thái Ngọc Duy218adaa2012-09-13 21:16:26 +07002296 (files == 1) ? " %d file changed" : " %d files changed",
Nguyễn Thái Ngọc Duy7f814632012-02-01 19:55:07 +07002297 files);
2298
2299 /*
2300 * For binary diff, the caller may want to print "x files
2301 * changed" with insertions == 0 && deletions == 0.
2302 *
2303 * Not omitting "0 insertions(+), 0 deletions(-)" in this case
2304 * is probably less confusing (i.e skip over "2 files changed
2305 * but nothing about added/removed lines? Is this a bug in Git?").
2306 */
2307 if (insertions || deletions == 0) {
Nguyễn Thái Ngọc Duy7f814632012-02-01 19:55:07 +07002308 strbuf_addf(&sb,
Nguyễn Thái Ngọc Duy218adaa2012-09-13 21:16:26 +07002309 (insertions == 1) ? ", %d insertion(+)" : ", %d insertions(+)",
Nguyễn Thái Ngọc Duy7f814632012-02-01 19:55:07 +07002310 insertions);
2311 }
2312
2313 if (deletions || insertions == 0) {
Nguyễn Thái Ngọc Duy7f814632012-02-01 19:55:07 +07002314 strbuf_addf(&sb,
Nguyễn Thái Ngọc Duy218adaa2012-09-13 21:16:26 +07002315 (deletions == 1) ? ", %d deletion(-)" : ", %d deletions(-)",
Nguyễn Thái Ngọc Duy7f814632012-02-01 19:55:07 +07002316 deletions);
2317 }
2318 strbuf_addch(&sb, '\n');
Stefan Beller0911c472017-06-29 17:07:02 -07002319 emit_diff_symbol(options, DIFF_SYMBOL_STATS_SUMMARY_INSERTS_DELETES,
2320 sb.buf, sb.len, 0);
Nguyễn Thái Ngọc Duy7f814632012-02-01 19:55:07 +07002321 strbuf_release(&sb);
Stefan Beller0911c472017-06-29 17:07:02 -07002322}
2323
2324void print_stat_summary(FILE *fp, int files,
2325 int insertions, int deletions)
2326{
2327 struct diff_options o;
2328 memset(&o, 0, sizeof(o));
2329 o.file = fp;
2330
2331 print_stat_summary_inserts_deletes(&o, files, insertions, deletions);
Nguyễn Thái Ngọc Duy7f814632012-02-01 19:55:07 +07002332}
2333
Felipe Contreras4b25d092009-05-01 12:06:36 +03002334static void show_stats(struct diffstat_t *data, struct diff_options *options)
Junio C Hamano6973dca2006-04-21 23:57:45 -07002335{
Benjamin Kramereb3a9dd2009-03-07 21:02:10 +01002336 int i, len, add, del, adds = 0, dels = 0;
Jeff King0974c112010-04-17 13:41:08 -04002337 uintmax_t max_change = 0, max_len = 0;
Zbigniew Jędrzejewski-Szmekdc801e72012-04-30 22:38:58 +02002338 int total_files = data->nr, count;
2339 int width, name_width, graph_width, number_width = 0, bin_width = 0;
Johannes Schindelinc0aa3352011-03-22 13:50:08 +01002340 const char *reset, *add_c, *del_c;
Junio C Hamanoe5f85df2011-05-27 21:50:39 -07002341 int extra_shown = 0;
Stefan Beller0911c472017-06-29 17:07:02 -07002342 const char *line_prefix = diff_line_prefix(options);
2343 struct strbuf out = STRBUF_INIT;
Junio C Hamano6973dca2006-04-21 23:57:45 -07002344
2345 if (data->nr == 0)
2346 return;
2347
Michael J Gruber808e1db2011-05-27 14:36:41 +02002348 count = options->stat_count ? options->stat_count : data->nr;
Junio C Hamanoa2540022006-09-26 18:53:02 -07002349
Pierre Habouzit8f67f8a2007-11-10 20:05:14 +01002350 reset = diff_get_color_opt(options, DIFF_RESET);
Pierre Habouzit8f67f8a2007-11-10 20:05:14 +01002351 add_c = diff_get_color_opt(options, DIFF_FILE_NEW);
2352 del_c = diff_get_color_opt(options, DIFF_FILE_OLD);
Junio C Hamano785f7432006-09-26 18:59:41 -07002353
Zbigniew Jędrzejewski-Szmek1b058bc2012-03-01 13:26:43 +01002354 /*
2355 * Find the longest filename and max number of changes
2356 */
Michael J Gruber808e1db2011-05-27 14:36:41 +02002357 for (i = 0; (i < count) && (i < data->nr); i++) {
Junio C Hamano6973dca2006-04-21 23:57:45 -07002358 struct diffstat_file *file = data->files[i];
Jeff King0974c112010-04-17 13:41:08 -04002359 uintmax_t change = file->added + file->deleted;
Junio C Hamanoaf0ed812012-11-27 11:24:54 -08002360
2361 if (!file->is_interesting && (change == 0)) {
Michael J Gruber808e1db2011-05-27 14:36:41 +02002362 count++; /* not shown == room for one more */
Michael J Gruber358e4602011-05-27 14:36:40 +02002363 continue;
2364 }
Junio C Hamanof6046522007-12-11 23:46:30 -08002365 fill_print_name(file);
2366 len = strlen(file->print_name);
Junio C Hamano6973dca2006-04-21 23:57:45 -07002367 if (max_len < len)
2368 max_len = len;
2369
Zbigniew Jędrzejewski-Szmekdc801e72012-04-30 22:38:58 +02002370 if (file->is_unmerged) {
2371 /* "Unmerged" is 8 characters */
2372 bin_width = bin_width < 8 ? 8 : bin_width;
Junio C Hamano6973dca2006-04-21 23:57:45 -07002373 continue;
Zbigniew Jędrzejewski-Szmekdc801e72012-04-30 22:38:58 +02002374 }
2375 if (file->is_binary) {
2376 /* "Bin XXX -> YYY bytes" */
2377 int w = 14 + decimal_width(file->added)
2378 + decimal_width(file->deleted);
2379 bin_width = bin_width < w ? w : bin_width;
2380 /* Display change counts aligned with "Bin" */
2381 number_width = 3;
2382 continue;
2383 }
2384
Junio C Hamanoa2540022006-09-26 18:53:02 -07002385 if (max_change < change)
2386 max_change = change;
Junio C Hamano6973dca2006-04-21 23:57:45 -07002387 }
Junio C Hamanoa20d3c02012-11-27 11:47:46 -08002388 count = i; /* where we can stop scanning in data->files[] */
Junio C Hamano6973dca2006-04-21 23:57:45 -07002389
Zbigniew Jędrzejewski-Szmek1b058bc2012-03-01 13:26:43 +01002390 /*
2391 * We have width = stat_width or term_columns() columns total.
2392 * We want a maximum of min(max_len, stat_name_width) for the name part.
Zbigniew Jędrzejewski-Szmek969fe572012-03-01 13:26:45 +01002393 * We want a maximum of min(max_change, stat_graph_width) for the +- part.
Zbigniew Jędrzejewski-Szmek1b058bc2012-03-01 13:26:43 +01002394 * We also need 1 for " " and 4 + decimal_width(max_change)
2395 * for " | NNNN " and one the empty column at the end, altogether
2396 * 6 + decimal_width(max_change).
Junio C Hamanoa2540022006-09-26 18:53:02 -07002397 *
Zbigniew Jędrzejewski-Szmek1b058bc2012-03-01 13:26:43 +01002398 * If there's not enough space, we will use the smaller of
2399 * stat_name_width (if set) and 5/8*width for the filename,
Zbigniew Jędrzejewski-Szmek969fe572012-03-01 13:26:45 +01002400 * and the rest for constant elements + graph part, but no more
2401 * than stat_graph_width for the graph part.
Zbigniew Jędrzejewski-Szmek1b058bc2012-03-01 13:26:43 +01002402 * (5/8 gives 50 for filename and 30 for the constant parts + graph
2403 * for the standard terminal size).
2404 *
2405 * In other words: stat_width limits the maximum width, and
2406 * stat_name_width fixes the maximum width of the filename,
2407 * and is also used to divide available columns if there
2408 * aren't enough.
Zbigniew Jędrzejewski-Szmekdc801e72012-04-30 22:38:58 +02002409 *
2410 * Binary files are displayed with "Bin XXX -> YYY bytes"
2411 * instead of the change count and graph. This part is treated
2412 * similarly to the graph part, except that it is not
Stefano Lattarini41ccfdd2013-04-12 00:36:10 +02002413 * "scaled". If total width is too small to accommodate the
Zbigniew Jędrzejewski-Szmekdc801e72012-04-30 22:38:58 +02002414 * guaranteed minimum width of the filename part and the
2415 * separators and this message, this message will "overflow"
2416 * making the line longer than the maximum width.
Junio C Hamanoa2540022006-09-26 18:53:02 -07002417 */
Junio C Hamanoa2540022006-09-26 18:53:02 -07002418
Zbigniew Jędrzejewski-Szmek1b058bc2012-03-01 13:26:43 +01002419 if (options->stat_width == -1)
Junio C Hamanocd48dad2016-08-31 16:27:19 -07002420 width = term_columns() - strlen(line_prefix);
Zbigniew Jędrzejewski-Szmek1b058bc2012-03-01 13:26:43 +01002421 else
2422 width = options->stat_width ? options->stat_width : 80;
Zbigniew Jędrzejewski-Szmekdc801e72012-04-30 22:38:58 +02002423 number_width = decimal_width(max_change) > number_width ?
2424 decimal_width(max_change) : number_width;
Zbigniew Jędrzejewski-Szmek1b058bc2012-03-01 13:26:43 +01002425
Zbigniew Jędrzejewski-Szmekdf444832012-03-01 13:26:46 +01002426 if (options->stat_graph_width == -1)
2427 options->stat_graph_width = diff_stat_graph_width;
2428
Zbigniew Jędrzejewski-Szmek1b058bc2012-03-01 13:26:43 +01002429 /*
2430 * Guarantee 3/8*16==6 for the graph part
2431 * and 5/8*16==10 for the filename part
2432 */
2433 if (width < 16 + 6 + number_width)
2434 width = 16 + 6 + number_width;
2435
2436 /*
2437 * First assign sizes that are wanted, ignoring available width.
Zbigniew Jędrzejewski-Szmekdc801e72012-04-30 22:38:58 +02002438 * strlen("Bin XXX -> YYY bytes") == bin_width, and the part
2439 * starting from "XXX" should fit in graph_width.
Zbigniew Jędrzejewski-Szmek1b058bc2012-03-01 13:26:43 +01002440 */
Zbigniew Jędrzejewski-Szmekdc801e72012-04-30 22:38:58 +02002441 graph_width = max_change + 4 > bin_width ? max_change : bin_width - 4;
2442 if (options->stat_graph_width &&
2443 options->stat_graph_width < graph_width)
2444 graph_width = options->stat_graph_width;
2445
Zbigniew Jędrzejewski-Szmek1b058bc2012-03-01 13:26:43 +01002446 name_width = (options->stat_name_width > 0 &&
2447 options->stat_name_width < max_len) ?
2448 options->stat_name_width : max_len;
2449
2450 /*
2451 * Adjust adjustable widths not to exceed maximum width
2452 */
2453 if (name_width + number_width + 6 + graph_width > width) {
Lucian Poston678c5742012-04-18 14:12:18 -07002454 if (graph_width > width * 3/8 - number_width - 6) {
Zbigniew Jędrzejewski-Szmek1b058bc2012-03-01 13:26:43 +01002455 graph_width = width * 3/8 - number_width - 6;
Lucian Poston678c5742012-04-18 14:12:18 -07002456 if (graph_width < 6)
2457 graph_width = 6;
2458 }
2459
Zbigniew Jędrzejewski-Szmek969fe572012-03-01 13:26:45 +01002460 if (options->stat_graph_width &&
2461 graph_width > options->stat_graph_width)
2462 graph_width = options->stat_graph_width;
Zbigniew Jędrzejewski-Szmek1b058bc2012-03-01 13:26:43 +01002463 if (name_width > width - number_width - 6 - graph_width)
2464 name_width = width - number_width - 6 - graph_width;
2465 else
2466 graph_width = width - number_width - 6 - name_width;
2467 }
2468
2469 /*
2470 * From here name_width is the width of the name area,
2471 * and graph_width is the width of the graph area.
2472 * max_change is used to scale graph properly.
2473 */
Michael J Gruber808e1db2011-05-27 14:36:41 +02002474 for (i = 0; i < count; i++) {
Timo Hirvonend2543b82006-06-24 20:20:32 +03002475 const char *prefix = "";
Junio C Hamanoaf0ed812012-11-27 11:24:54 -08002476 struct diffstat_file *file = data->files[i];
2477 char *name = file->print_name;
2478 uintmax_t added = file->added;
2479 uintmax_t deleted = file->deleted;
Junio C Hamanoa2540022006-09-26 18:53:02 -07002480 int name_len;
Junio C Hamano6973dca2006-04-21 23:57:45 -07002481
Junio C Hamanoa20d3c02012-11-27 11:47:46 -08002482 if (!file->is_interesting && (added + deleted == 0))
Michael J Gruber358e4602011-05-27 14:36:40 +02002483 continue;
Junio C Hamanoa20d3c02012-11-27 11:47:46 -08002484
Junio C Hamano6973dca2006-04-21 23:57:45 -07002485 /*
2486 * "scale" the filename
2487 */
Junio C Hamanoa2540022006-09-26 18:53:02 -07002488 len = name_width;
2489 name_len = strlen(name);
2490 if (name_width < name_len) {
Junio C Hamano6973dca2006-04-21 23:57:45 -07002491 char *slash;
2492 prefix = "...";
Junio C Hamanoa2540022006-09-26 18:53:02 -07002493 len -= 3;
2494 name += name_len - len;
Junio C Hamano6973dca2006-04-21 23:57:45 -07002495 slash = strchr(name, '/');
2496 if (slash)
2497 name = slash;
2498 }
Junio C Hamano6973dca2006-04-21 23:57:45 -07002499
Junio C Hamanoaf0ed812012-11-27 11:24:54 -08002500 if (file->is_binary) {
Stefan Beller0911c472017-06-29 17:07:02 -07002501 strbuf_addf(&out, " %s%-*s |", prefix, len, name);
2502 strbuf_addf(&out, " %*s", number_width, "Bin");
Zbigniew Jędrzejewski-Szmeke18872b2012-05-01 19:10:14 +02002503 if (!added && !deleted) {
Stefan Beller0911c472017-06-29 17:07:02 -07002504 strbuf_addch(&out, '\n');
2505 emit_diff_symbol(options, DIFF_SYMBOL_STATS_LINE,
2506 out.buf, out.len, 0);
2507 strbuf_reset(&out);
Zbigniew Jędrzejewski-Szmeke18872b2012-05-01 19:10:14 +02002508 continue;
2509 }
Stefan Beller0911c472017-06-29 17:07:02 -07002510 strbuf_addf(&out, " %s%"PRIuMAX"%s",
Jeff King0974c112010-04-17 13:41:08 -04002511 del_c, deleted, reset);
Stefan Beller0911c472017-06-29 17:07:02 -07002512 strbuf_addstr(&out, " -> ");
2513 strbuf_addf(&out, "%s%"PRIuMAX"%s",
Jeff King0974c112010-04-17 13:41:08 -04002514 add_c, added, reset);
Stefan Beller0911c472017-06-29 17:07:02 -07002515 strbuf_addstr(&out, " bytes\n");
2516 emit_diff_symbol(options, DIFF_SYMBOL_STATS_LINE,
2517 out.buf, out.len, 0);
2518 strbuf_reset(&out);
Junio C Hamanof6046522007-12-11 23:46:30 -08002519 continue;
Junio C Hamano6973dca2006-04-21 23:57:45 -07002520 }
Junio C Hamanoaf0ed812012-11-27 11:24:54 -08002521 else if (file->is_unmerged) {
Stefan Beller0911c472017-06-29 17:07:02 -07002522 strbuf_addf(&out, " %s%-*s |", prefix, len, name);
2523 strbuf_addstr(&out, " Unmerged\n");
2524 emit_diff_symbol(options, DIFF_SYMBOL_STATS_LINE,
2525 out.buf, out.len, 0);
2526 strbuf_reset(&out);
Junio C Hamanof6046522007-12-11 23:46:30 -08002527 continue;
Junio C Hamano6973dca2006-04-21 23:57:45 -07002528 }
Junio C Hamano6973dca2006-04-21 23:57:45 -07002529
Junio C Hamanoa2540022006-09-26 18:53:02 -07002530 /*
2531 * scale the add/delete
2532 */
Junio C Hamano6973dca2006-04-21 23:57:45 -07002533 add = added;
2534 del = deleted;
Junio C Hamano6973dca2006-04-21 23:57:45 -07002535
Zbigniew Jędrzejewski-Szmek1b058bc2012-03-01 13:26:43 +01002536 if (graph_width <= max_change) {
Stefan Bellerd3c9cf32013-07-14 23:35:49 +02002537 int total = scale_linear(add + del, graph_width, max_change);
Junio C Hamano2eeeef22012-02-14 13:49:11 -08002538 if (total < 2 && add && del)
2539 /* width >= 2 due to the sanity check */
2540 total = 2;
2541 if (add < del) {
Zbigniew Jędrzejewski-Szmek1b058bc2012-03-01 13:26:43 +01002542 add = scale_linear(add, graph_width, max_change);
Junio C Hamano2eeeef22012-02-14 13:49:11 -08002543 del = total - add;
2544 } else {
Zbigniew Jędrzejewski-Szmek1b058bc2012-03-01 13:26:43 +01002545 del = scale_linear(del, graph_width, max_change);
Junio C Hamano2eeeef22012-02-14 13:49:11 -08002546 add = total - del;
2547 }
Junio C Hamano6973dca2006-04-21 23:57:45 -07002548 }
Stefan Beller0911c472017-06-29 17:07:02 -07002549 strbuf_addf(&out, " %s%-*s |", prefix, len, name);
2550 strbuf_addf(&out, " %*"PRIuMAX"%s",
Zbigniew Jędrzejewski-Szmekdc801e72012-04-30 22:38:58 +02002551 number_width, added + deleted,
2552 added + deleted ? " " : "");
Stefan Beller0911c472017-06-29 17:07:02 -07002553 show_graph(&out, '+', add, add_c, reset);
2554 show_graph(&out, '-', del, del_c, reset);
2555 strbuf_addch(&out, '\n');
2556 emit_diff_symbol(options, DIFF_SYMBOL_STATS_LINE,
2557 out.buf, out.len, 0);
2558 strbuf_reset(&out);
Junio C Hamano6973dca2006-04-21 23:57:45 -07002559 }
Junio C Hamanoa20d3c02012-11-27 11:47:46 -08002560
2561 for (i = 0; i < data->nr; i++) {
Junio C Hamanoaf0ed812012-11-27 11:24:54 -08002562 struct diffstat_file *file = data->files[i];
2563 uintmax_t added = file->added;
2564 uintmax_t deleted = file->deleted;
Junio C Hamano82dfc2c2012-11-27 12:05:10 -08002565
2566 if (file->is_unmerged ||
2567 (!file->is_interesting && (added + deleted == 0))) {
Michael J Gruber808e1db2011-05-27 14:36:41 +02002568 total_files--;
2569 continue;
2570 }
Junio C Hamanoa20d3c02012-11-27 11:47:46 -08002571
Junio C Hamano82dfc2c2012-11-27 12:05:10 -08002572 if (!file->is_binary) {
Junio C Hamanoa20d3c02012-11-27 11:47:46 -08002573 adds += added;
2574 dels += deleted;
2575 }
2576 if (i < count)
2577 continue;
Junio C Hamanoe5f85df2011-05-27 21:50:39 -07002578 if (!extra_shown)
Stefan Beller0911c472017-06-29 17:07:02 -07002579 emit_diff_symbol(options,
2580 DIFF_SYMBOL_STATS_SUMMARY_ABBREV,
2581 NULL, 0, 0);
Junio C Hamanoe5f85df2011-05-27 21:50:39 -07002582 extra_shown = 1;
Michael J Gruber808e1db2011-05-27 14:36:41 +02002583 }
Stefan Beller0911c472017-06-29 17:07:02 -07002584
2585 print_stat_summary_inserts_deletes(options, total_files, adds, dels);
Rene Scharfe5a612012017-08-30 19:49:43 +02002586 strbuf_release(&out);
Junio C Hamano6973dca2006-04-21 23:57:45 -07002587}
2588
Felipe Contreras2775d922009-10-11 23:46:11 +03002589static void show_shortstats(struct diffstat_t *data, struct diff_options *options)
Nicolas Pitreebd124c2006-12-14 23:15:44 -05002590{
2591 int i, adds = 0, dels = 0, total_files = data->nr;
2592
2593 if (data->nr == 0)
2594 return;
2595
2596 for (i = 0; i < data->nr; i++) {
Zbigniew Jędrzejewski-Szmeke18872b2012-05-01 19:10:14 +02002597 int added = data->files[i]->added;
Stefan Beller0911c472017-06-29 17:07:02 -07002598 int deleted = data->files[i]->deleted;
Zbigniew Jędrzejewski-Szmeke18872b2012-05-01 19:10:14 +02002599
Junio C Hamano20c8cde2012-11-27 14:19:36 -08002600 if (data->files[i]->is_unmerged ||
2601 (!data->files[i]->is_interesting && (added + deleted == 0))) {
Zbigniew Jędrzejewski-Szmeke18872b2012-05-01 19:10:14 +02002602 total_files--;
Alexander Strasserde9658b2012-06-15 23:50:30 +02002603 } else if (!data->files[i]->is_binary) { /* don't count bytes */
Zbigniew Jędrzejewski-Szmeke18872b2012-05-01 19:10:14 +02002604 adds += added;
2605 dels += deleted;
Nicolas Pitreebd124c2006-12-14 23:15:44 -05002606 }
Nicolas Pitreebd124c2006-12-14 23:15:44 -05002607 }
Stefan Beller0911c472017-06-29 17:07:02 -07002608 print_stat_summary_inserts_deletes(options, total_files, adds, dels);
Nicolas Pitreebd124c2006-12-14 23:15:44 -05002609}
2610
Felipe Contreras4b25d092009-05-01 12:06:36 +03002611static void show_numstat(struct diffstat_t *data, struct diff_options *options)
Junio C Hamano74e2abe2006-10-12 03:01:00 -07002612{
2613 int i;
2614
Junio C Hamanof6046522007-12-11 23:46:30 -08002615 if (data->nr == 0)
2616 return;
2617
Junio C Hamano74e2abe2006-10-12 03:01:00 -07002618 for (i = 0; i < data->nr; i++) {
2619 struct diffstat_file *file = data->files[i];
2620
John Keeping30997bb2013-02-07 20:15:27 +00002621 fprintf(options->file, "%s", diff_line_prefix(options));
Bo Yang7be57612010-05-26 15:23:54 +08002622
Junio C Hamanobfddbc52006-12-10 13:50:59 -08002623 if (file->is_binary)
Daniel Barkalowc0c77732008-03-09 22:43:39 -04002624 fprintf(options->file, "-\t-\t");
Junio C Hamanobfddbc52006-12-10 13:50:59 -08002625 else
Daniel Barkalowc0c77732008-03-09 22:43:39 -04002626 fprintf(options->file,
Jeff King0974c112010-04-17 13:41:08 -04002627 "%"PRIuMAX"\t%"PRIuMAX"\t",
2628 file->added, file->deleted);
Junio C Hamanof6046522007-12-11 23:46:30 -08002629 if (options->line_termination) {
2630 fill_print_name(file);
2631 if (!file->is_renamed)
Daniel Barkalowc0c77732008-03-09 22:43:39 -04002632 write_name_quoted(file->name, options->file,
Junio C Hamanof6046522007-12-11 23:46:30 -08002633 options->line_termination);
2634 else {
Daniel Barkalowc0c77732008-03-09 22:43:39 -04002635 fputs(file->print_name, options->file);
2636 putc(options->line_termination, options->file);
Junio C Hamanof6046522007-12-11 23:46:30 -08002637 }
Pierre Habouzit663af342007-09-20 00:42:15 +02002638 } else {
Junio C Hamanof6046522007-12-11 23:46:30 -08002639 if (file->is_renamed) {
Daniel Barkalowc0c77732008-03-09 22:43:39 -04002640 putc('\0', options->file);
2641 write_name_quoted(file->from_name, options->file, '\0');
Junio C Hamanof6046522007-12-11 23:46:30 -08002642 }
Daniel Barkalowc0c77732008-03-09 22:43:39 -04002643 write_name_quoted(file->name, options->file, '\0');
Pierre Habouzit663af342007-09-20 00:42:15 +02002644 }
Junio C Hamano74e2abe2006-10-12 03:01:00 -07002645 }
2646}
2647
Junio C Hamanoc04a7152008-02-12 17:06:58 -08002648struct dirstat_file {
2649 const char *name;
2650 unsigned long changed;
Linus Torvalds7df7c012008-02-12 13:26:31 -08002651};
2652
Junio C Hamanoc04a7152008-02-12 17:06:58 -08002653struct dirstat_dir {
2654 struct dirstat_file *files;
Johan Herland712d2c72011-04-29 11:36:20 +02002655 int alloc, nr, permille, cumulative;
Junio C Hamanoc04a7152008-02-12 17:06:58 -08002656};
2657
Bo Yang7be57612010-05-26 15:23:54 +08002658static long gather_dirstat(struct diff_options *opt, struct dirstat_dir *dir,
2659 unsigned long changed, const char *base, int baselen)
Linus Torvalds7df7c012008-02-12 13:26:31 -08002660{
2661 unsigned long this_dir = 0;
2662 unsigned int sources = 0;
John Keeping30997bb2013-02-07 20:15:27 +00002663 const char *line_prefix = diff_line_prefix(opt);
Linus Torvalds7df7c012008-02-12 13:26:31 -08002664
2665 while (dir->nr) {
Junio C Hamanoc04a7152008-02-12 17:06:58 -08002666 struct dirstat_file *f = dir->files;
Linus Torvalds7df7c012008-02-12 13:26:31 -08002667 int namelen = strlen(f->name);
2668 unsigned long this;
2669 char *slash;
2670
2671 if (namelen < baselen)
2672 break;
2673 if (memcmp(f->name, base, baselen))
2674 break;
2675 slash = strchr(f->name + baselen, '/');
2676 if (slash) {
2677 int newbaselen = slash + 1 - f->name;
Bo Yang7be57612010-05-26 15:23:54 +08002678 this = gather_dirstat(opt, dir, changed, f->name, newbaselen);
Linus Torvalds7df7c012008-02-12 13:26:31 -08002679 sources++;
2680 } else {
Junio C Hamanoc04a7152008-02-12 17:06:58 -08002681 this = f->changed;
Linus Torvalds7df7c012008-02-12 13:26:31 -08002682 dir->files++;
2683 dir->nr--;
2684 sources += 2;
2685 }
2686 this_dir += this;
2687 }
2688
2689 /*
2690 * We don't report dirstat's for
2691 * - the top level
2692 * - or cases where everything came from a single directory
2693 * under this directory (sources == 1).
2694 */
2695 if (baselen && sources != 1) {
Johan Herland58a87562011-04-29 11:36:17 +02002696 if (this_dir) {
2697 int permille = this_dir * 1000 / changed;
Johan Herland712d2c72011-04-29 11:36:20 +02002698 if (permille >= dir->permille) {
Bo Yang7be57612010-05-26 15:23:54 +08002699 fprintf(opt->file, "%s%4d.%01d%% %.*s\n", line_prefix,
Johan Herland712d2c72011-04-29 11:36:20 +02002700 permille / 10, permille % 10, baselen, base);
Linus Torvalds7df7c012008-02-12 13:26:31 -08002701 if (!dir->cumulative)
2702 return 0;
2703 }
2704 }
2705 }
2706 return this_dir;
2707}
2708
Linus Torvalds441bca02008-08-28 16:19:08 -07002709static int dirstat_compare(const void *_a, const void *_b)
2710{
2711 const struct dirstat_file *a = _a;
2712 const struct dirstat_file *b = _b;
2713 return strcmp(a->name, b->name);
2714}
2715
Junio C Hamanoc04a7152008-02-12 17:06:58 -08002716static void show_dirstat(struct diff_options *options)
Linus Torvalds7df7c012008-02-12 13:26:31 -08002717{
2718 int i;
2719 unsigned long changed;
Junio C Hamanoc04a7152008-02-12 17:06:58 -08002720 struct dirstat_dir dir;
2721 struct diff_queue_struct *q = &diff_queued_diff;
Linus Torvalds7df7c012008-02-12 13:26:31 -08002722
Junio C Hamanoc04a7152008-02-12 17:06:58 -08002723 dir.files = NULL;
2724 dir.alloc = 0;
2725 dir.nr = 0;
Johan Herland712d2c72011-04-29 11:36:20 +02002726 dir.permille = options->dirstat_permille;
Junio C Hamanof88d2252008-09-02 17:28:59 -07002727 dir.cumulative = DIFF_OPT_TST(options, DIRSTAT_CUMULATIVE);
Junio C Hamanoc04a7152008-02-12 17:06:58 -08002728
Linus Torvalds7df7c012008-02-12 13:26:31 -08002729 changed = 0;
Junio C Hamanoc04a7152008-02-12 17:06:58 -08002730 for (i = 0; i < q->nr; i++) {
2731 struct diff_filepair *p = q->queue[i];
2732 const char *name;
2733 unsigned long copied, added, damage;
Johan Herland0133dab2011-04-11 00:48:51 +02002734 int content_changed;
Junio C Hamanoc04a7152008-02-12 17:06:58 -08002735
Johan Herland2ca86712011-04-12 11:24:34 +02002736 name = p->two->path ? p->two->path : p->one->path;
Junio C Hamanoc04a7152008-02-12 17:06:58 -08002737
brian m. carlson41c95602016-06-24 23:09:24 +00002738 if (p->one->oid_valid && p->two->oid_valid)
brian m. carlsona0d12c42016-06-24 23:09:23 +00002739 content_changed = oidcmp(&p->one->oid, &p->two->oid);
Johan Herland0133dab2011-04-11 00:48:51 +02002740 else
2741 content_changed = 1;
2742
Johan Herland2ff3a802011-04-11 00:48:52 +02002743 if (!content_changed) {
2744 /*
2745 * The SHA1 has not changed, so pre-/post-content is
2746 * identical. We can therefore skip looking at the
2747 * file contents altogether.
2748 */
2749 damage = 0;
2750 goto found_damage;
2751 }
2752
Johan Herland0133dab2011-04-11 00:48:51 +02002753 if (DIFF_OPT_TST(options, DIRSTAT_BY_FILE)) {
2754 /*
2755 * In --dirstat-by-file mode, we don't really need to
2756 * look at the actual file contents at all.
2757 * The fact that the SHA1 changed is enough for us to
2758 * add this file to the list of results
2759 * (with each file contributing equal damage).
2760 */
Johan Herland2ff3a802011-04-11 00:48:52 +02002761 damage = 1;
Johan Herland0133dab2011-04-11 00:48:51 +02002762 goto found_damage;
2763 }
Junio C Hamanoc04a7152008-02-12 17:06:58 -08002764
2765 if (DIFF_FILE_VALID(p->one) && DIFF_FILE_VALID(p->two)) {
2766 diff_populate_filespec(p->one, 0);
2767 diff_populate_filespec(p->two, 0);
Tobias Klauser974e0042016-11-14 14:39:05 +01002768 diffcore_count_changes(p->one, p->two, NULL, NULL,
Junio C Hamanoc04a7152008-02-12 17:06:58 -08002769 &copied, &added);
2770 diff_free_filespec_data(p->one);
2771 diff_free_filespec_data(p->two);
2772 } else if (DIFF_FILE_VALID(p->one)) {
Nguyễn Thái Ngọc Duy8e5dd3d2014-08-16 10:08:04 +07002773 diff_populate_filespec(p->one, CHECK_SIZE_ONLY);
Junio C Hamanoc04a7152008-02-12 17:06:58 -08002774 copied = added = 0;
2775 diff_free_filespec_data(p->one);
2776 } else if (DIFF_FILE_VALID(p->two)) {
Nguyễn Thái Ngọc Duy8e5dd3d2014-08-16 10:08:04 +07002777 diff_populate_filespec(p->two, CHECK_SIZE_ONLY);
Junio C Hamanoc04a7152008-02-12 17:06:58 -08002778 copied = 0;
2779 added = p->two->size;
2780 diff_free_filespec_data(p->two);
2781 } else
Junio C Hamano2b0b5512008-02-24 17:37:15 -08002782 continue;
Junio C Hamanoc04a7152008-02-12 17:06:58 -08002783
2784 /*
2785 * Original minus copied is the removed material,
2786 * added is the new material. They are both damages
Johan Herland0133dab2011-04-11 00:48:51 +02002787 * made to the preimage.
Johan Herland2ff3a802011-04-11 00:48:52 +02002788 * If the resulting damage is zero, we know that
2789 * diffcore_count_changes() considers the two entries to
2790 * be identical, but since content_changed is true, we
2791 * know that there must have been _some_ kind of change,
2792 * so we force all entries to have damage > 0.
Junio C Hamanoc04a7152008-02-12 17:06:58 -08002793 */
2794 damage = (p->one->size - copied) + added;
Johan Herland2ff3a802011-04-11 00:48:52 +02002795 if (!damage)
Heikki Orsilafd337772008-09-05 22:27:35 +03002796 damage = 1;
Junio C Hamanoc04a7152008-02-12 17:06:58 -08002797
Johan Herland0133dab2011-04-11 00:48:51 +02002798found_damage:
Junio C Hamanoc04a7152008-02-12 17:06:58 -08002799 ALLOC_GROW(dir.files, dir.nr + 1, dir.alloc);
2800 dir.files[dir.nr].name = name;
2801 dir.files[dir.nr].changed = damage;
2802 changed += damage;
2803 dir.nr++;
Linus Torvalds7df7c012008-02-12 13:26:31 -08002804 }
2805
2806 /* This can happen even with many files, if everything was renames */
2807 if (!changed)
2808 return;
2809
2810 /* Show all directories with more than x% of the changes */
René Scharfe9ed0d8d2016-09-29 17:27:31 +02002811 QSORT(dir.files, dir.nr, dirstat_compare);
Bo Yang7be57612010-05-26 15:23:54 +08002812 gather_dirstat(options, &dir, changed, "", 0);
Linus Torvalds7df7c012008-02-12 13:26:31 -08002813}
2814
Johan Herland1c57a622011-04-29 11:36:21 +02002815static void show_dirstat_by_line(struct diffstat_t *data, struct diff_options *options)
2816{
2817 int i;
2818 unsigned long changed;
2819 struct dirstat_dir dir;
2820
2821 if (data->nr == 0)
2822 return;
2823
2824 dir.files = NULL;
2825 dir.alloc = 0;
2826 dir.nr = 0;
2827 dir.permille = options->dirstat_permille;
2828 dir.cumulative = DIFF_OPT_TST(options, DIRSTAT_CUMULATIVE);
2829
2830 changed = 0;
2831 for (i = 0; i < data->nr; i++) {
2832 struct diffstat_file *file = data->files[i];
2833 unsigned long damage = file->added + file->deleted;
2834 if (file->is_binary)
2835 /*
2836 * binary files counts bytes, not lines. Must find some
2837 * way to normalize binary bytes vs. textual lines.
2838 * The following heuristic assumes that there are 64
2839 * bytes per "line".
2840 * This is stupid and ugly, but very cheap...
2841 */
René Scharfe42c78a22017-07-08 12:35:35 +02002842 damage = DIV_ROUND_UP(damage, 64);
Johan Herland1c57a622011-04-29 11:36:21 +02002843 ALLOC_GROW(dir.files, dir.nr + 1, dir.alloc);
2844 dir.files[dir.nr].name = file->name;
2845 dir.files[dir.nr].changed = damage;
2846 changed += damage;
2847 dir.nr++;
2848 }
2849
2850 /* This can happen even with many files, if everything was renames */
2851 if (!changed)
2852 return;
2853
2854 /* Show all directories with more than x% of the changes */
René Scharfe9ed0d8d2016-09-29 17:27:31 +02002855 QSORT(dir.files, dir.nr, dirstat_compare);
Johan Herland1c57a622011-04-29 11:36:21 +02002856 gather_dirstat(options, &dir, changed, "", 0);
2857}
2858
Junio C Hamanof6046522007-12-11 23:46:30 -08002859static void free_diffstat_info(struct diffstat_t *diffstat)
2860{
2861 int i;
2862 for (i = 0; i < diffstat->nr; i++) {
2863 struct diffstat_file *f = diffstat->files[i];
2864 if (f->name != f->print_name)
2865 free(f->print_name);
2866 free(f->name);
2867 free(f->from_name);
2868 free(f);
2869 }
2870 free(diffstat->files);
2871}
2872
Johannes Schindelin88246892006-05-20 23:43:13 +02002873struct checkdiff_t {
Johannes Schindelin88246892006-05-20 23:43:13 +02002874 const char *filename;
Junio C Hamano1ba111d2008-06-26 15:36:34 -07002875 int lineno;
Junio C Hamanoa757c642010-03-24 19:21:32 -07002876 int conflict_marker_size;
Junio C Hamano1ba111d2008-06-26 15:36:34 -07002877 struct diff_options *o;
Junio C Hamanocf1b7862007-12-06 00:14:14 -08002878 unsigned ws_rule;
Wincent Colaiuta62c64892007-12-13 21:24:52 +01002879 unsigned status;
Johannes Schindelin88246892006-05-20 23:43:13 +02002880};
2881
Junio C Hamanoa757c642010-03-24 19:21:32 -07002882static int is_conflict_marker(const char *line, int marker_size, unsigned long len)
Junio C Hamano04954042008-06-26 15:37:21 -07002883{
2884 char firstchar;
2885 int cnt;
2886
Junio C Hamanoa757c642010-03-24 19:21:32 -07002887 if (len < marker_size + 1)
Junio C Hamano04954042008-06-26 15:37:21 -07002888 return 0;
2889 firstchar = line[0];
2890 switch (firstchar) {
Junio C Hamanoa757c642010-03-24 19:21:32 -07002891 case '=': case '>': case '<': case '|':
Junio C Hamano04954042008-06-26 15:37:21 -07002892 break;
2893 default:
2894 return 0;
2895 }
Junio C Hamanoa757c642010-03-24 19:21:32 -07002896 for (cnt = 1; cnt < marker_size; cnt++)
Junio C Hamano04954042008-06-26 15:37:21 -07002897 if (line[cnt] != firstchar)
2898 return 0;
Junio C Hamanoa757c642010-03-24 19:21:32 -07002899 /* line[1] thru line[marker_size-1] are same as firstchar */
2900 if (len < marker_size + 1 || !isspace(line[marker_size]))
Junio C Hamano04954042008-06-26 15:37:21 -07002901 return 0;
Junio C Hamano04954042008-06-26 15:37:21 -07002902 return 1;
2903}
2904
Johannes Schindelin88246892006-05-20 23:43:13 +02002905static void checkdiff_consume(void *priv, char *line, unsigned long len)
2906{
2907 struct checkdiff_t *data = priv;
Junio C Hamanoa757c642010-03-24 19:21:32 -07002908 int marker_size = data->conflict_marker_size;
Jeff Kingf1c96262011-08-17 22:03:12 -07002909 const char *ws = diff_get_color(data->o->use_color, DIFF_WHITESPACE);
2910 const char *reset = diff_get_color(data->o->use_color, DIFF_RESET);
2911 const char *set = diff_get_color(data->o->use_color, DIFF_FILE_NEW);
Wincent Colaiutac1795bb2007-12-13 14:32:29 +01002912 char *err;
John Keeping30997bb2013-02-07 20:15:27 +00002913 const char *line_prefix;
Bo Yang7be57612010-05-26 15:23:54 +08002914
2915 assert(data->o);
John Keeping30997bb2013-02-07 20:15:27 +00002916 line_prefix = diff_line_prefix(data->o);
Johannes Schindelin88246892006-05-20 23:43:13 +02002917
2918 if (line[0] == '+') {
Junio C Hamano18374e52008-06-26 13:16:33 -07002919 unsigned bad;
Junio C Hamano0ef617f2008-02-15 20:30:05 -08002920 data->lineno++;
Junio C Hamanoa757c642010-03-24 19:21:32 -07002921 if (is_conflict_marker(line + 1, marker_size, len - 1)) {
Junio C Hamano04954042008-06-26 15:37:21 -07002922 data->status |= 1;
2923 fprintf(data->o->file,
Bo Yang7be57612010-05-26 15:23:54 +08002924 "%s%s:%d: leftover conflict marker\n",
2925 line_prefix, data->filename, data->lineno);
Junio C Hamano04954042008-06-26 15:37:21 -07002926 }
Junio C Hamano8f8841e2008-06-26 15:35:21 -07002927 bad = ws_check(line + 1, len - 1, data->ws_rule);
Junio C Hamano18374e52008-06-26 13:16:33 -07002928 if (!bad)
Wincent Colaiutac1795bb2007-12-13 14:32:29 +01002929 return;
Junio C Hamano18374e52008-06-26 13:16:33 -07002930 data->status |= bad;
2931 err = whitespace_error_string(bad);
Bo Yang7be57612010-05-26 15:23:54 +08002932 fprintf(data->o->file, "%s%s:%d: %s.\n",
2933 line_prefix, data->filename, data->lineno, err);
Wincent Colaiutac1795bb2007-12-13 14:32:29 +01002934 free(err);
Bo Yanga3c158d2010-05-26 15:08:02 +08002935 emit_line(data->o, set, reset, line, 1);
Junio C Hamano8f8841e2008-06-26 15:35:21 -07002936 ws_check_emit(line + 1, len - 1, data->ws_rule,
Junio C Hamano1ba111d2008-06-26 15:36:34 -07002937 data->o->file, set, reset, ws);
Junio C Hamano877f23c2008-06-26 15:36:59 -07002938 } else if (line[0] == ' ') {
Johannes Schindelin88246892006-05-20 23:43:13 +02002939 data->lineno++;
Junio C Hamano877f23c2008-06-26 15:36:59 -07002940 } else if (line[0] == '@') {
Johannes Schindelin88246892006-05-20 23:43:13 +02002941 char *plus = strchr(line, '+');
2942 if (plus)
Junio C Hamano0ef617f2008-02-15 20:30:05 -08002943 data->lineno = strtol(plus, NULL, 10) - 1;
Johannes Schindelin88246892006-05-20 23:43:13 +02002944 else
2945 die("invalid diff");
2946 }
2947}
2948
Junio C Hamano06606262006-05-05 02:41:53 -07002949static unsigned char *deflate_it(char *data,
2950 unsigned long size,
2951 unsigned long *result_size)
Junio C Hamano051308f2006-05-04 16:51:44 -07002952{
Junio C Hamano06606262006-05-05 02:41:53 -07002953 int bound;
2954 unsigned char *deflated;
Junio C Hamanoef49a7a2011-06-10 11:52:15 -07002955 git_zstream stream;
Junio C Hamano051308f2006-05-04 16:51:44 -07002956
Junio C Hamano55bb5c92011-06-10 10:55:10 -07002957 git_deflate_init(&stream, zlib_compression_level);
Junio C Hamano225a6f12011-06-10 11:18:17 -07002958 bound = git_deflate_bound(&stream, size);
Junio C Hamano06606262006-05-05 02:41:53 -07002959 deflated = xmalloc(bound);
2960 stream.next_out = deflated;
2961 stream.avail_out = bound;
Junio C Hamano051308f2006-05-04 16:51:44 -07002962
Junio C Hamano06606262006-05-05 02:41:53 -07002963 stream.next_in = (unsigned char *)data;
2964 stream.avail_in = size;
Junio C Hamano55bb5c92011-06-10 10:55:10 -07002965 while (git_deflate(&stream, Z_FINISH) == Z_OK)
Junio C Hamano06606262006-05-05 02:41:53 -07002966 ; /* nothing */
Junio C Hamano55bb5c92011-06-10 10:55:10 -07002967 git_deflate_end(&stream);
Junio C Hamano06606262006-05-05 02:41:53 -07002968 *result_size = stream.total_out;
2969 return deflated;
Junio C Hamano051308f2006-05-04 16:51:44 -07002970}
2971
Stefan Beller4eed0eb2017-06-29 17:07:01 -07002972static void emit_binary_diff_body(struct diff_options *o,
2973 mmfile_t *one, mmfile_t *two)
Junio C Hamano051308f2006-05-04 16:51:44 -07002974{
Junio C Hamano06606262006-05-05 02:41:53 -07002975 void *cp;
2976 void *delta;
2977 void *deflated;
2978 void *data;
2979 unsigned long orig_size;
Junio C Hamano051308f2006-05-04 16:51:44 -07002980 unsigned long delta_size;
Junio C Hamano06606262006-05-05 02:41:53 -07002981 unsigned long deflate_size;
2982 unsigned long data_size;
Junio C Hamano051308f2006-05-04 16:51:44 -07002983
Junio C Hamano06606262006-05-05 02:41:53 -07002984 /* We could do deflated delta, or we could do just deflated two,
2985 * whichever is smaller.
2986 */
2987 delta = NULL;
2988 deflated = deflate_it(two->ptr, two->size, &deflate_size);
2989 if (one->size && two->size) {
2990 delta = diff_delta(one->ptr, one->size,
2991 two->ptr, two->size,
2992 &delta_size, deflate_size);
2993 if (delta) {
2994 void *to_free = delta;
2995 orig_size = delta_size;
2996 delta = deflate_it(delta, delta_size, &delta_size);
2997 free(to_free);
2998 }
2999 }
Junio C Hamano051308f2006-05-04 16:51:44 -07003000
Junio C Hamano06606262006-05-05 02:41:53 -07003001 if (delta && delta_size < deflate_size) {
Stefan Beller4eed0eb2017-06-29 17:07:01 -07003002 char *s = xstrfmt("%lu", orig_size);
3003 emit_diff_symbol(o, DIFF_SYMBOL_BINARY_DIFF_HEADER_DELTA,
3004 s, strlen(s), 0);
3005 free(s);
Junio C Hamano06606262006-05-05 02:41:53 -07003006 free(deflated);
3007 data = delta;
3008 data_size = delta_size;
Stefan Beller4eed0eb2017-06-29 17:07:01 -07003009 } else {
3010 char *s = xstrfmt("%lu", two->size);
3011 emit_diff_symbol(o, DIFF_SYMBOL_BINARY_DIFF_HEADER_LITERAL,
3012 s, strlen(s), 0);
3013 free(s);
Junio C Hamano06606262006-05-05 02:41:53 -07003014 free(delta);
3015 data = deflated;
3016 data_size = deflate_size;
3017 }
3018
3019 /* emit data encoded in base85 */
3020 cp = data;
3021 while (data_size) {
Stefan Beller4eed0eb2017-06-29 17:07:01 -07003022 int len;
Junio C Hamano06606262006-05-05 02:41:53 -07003023 int bytes = (52 < data_size) ? 52 : data_size;
Stefan Beller4eed0eb2017-06-29 17:07:01 -07003024 char line[71];
Junio C Hamano06606262006-05-05 02:41:53 -07003025 data_size -= bytes;
Junio C Hamano051308f2006-05-04 16:51:44 -07003026 if (bytes <= 26)
3027 line[0] = bytes + 'A' - 1;
3028 else
3029 line[0] = bytes - 26 + 'a' - 1;
3030 encode_85(line + 1, cp, bytes);
Florian Forster1d7f1712006-06-18 17:18:09 +02003031 cp = (char *) cp + bytes;
Stefan Beller4eed0eb2017-06-29 17:07:01 -07003032
3033 len = strlen(line);
3034 line[len++] = '\n';
3035 line[len] = '\0';
3036
3037 emit_diff_symbol(o, DIFF_SYMBOL_BINARY_DIFF_BODY,
3038 line, len, 0);
Junio C Hamano051308f2006-05-04 16:51:44 -07003039 }
Stefan Beller4eed0eb2017-06-29 17:07:01 -07003040 emit_diff_symbol(o, DIFF_SYMBOL_BINARY_DIFF_FOOTER, NULL, 0, 0);
Junio C Hamano06606262006-05-05 02:41:53 -07003041 free(data);
Junio C Hamano051308f2006-05-04 16:51:44 -07003042}
3043
Stefan Beller4eed0eb2017-06-29 17:07:01 -07003044static void emit_binary_diff(struct diff_options *o,
3045 mmfile_t *one, mmfile_t *two)
Junio C Hamanod4c452f2006-08-16 16:08:14 -07003046{
Stefan Beller4eed0eb2017-06-29 17:07:01 -07003047 emit_diff_symbol(o, DIFF_SYMBOL_BINARY_DIFF_HEADER, NULL, 0, 0);
3048 emit_binary_diff_body(o, one, two);
3049 emit_binary_diff_body(o, two, one);
Junio C Hamanod4c452f2006-08-16 16:08:14 -07003050}
3051
Junio C Hamano29a3eef2007-07-06 00:18:54 -07003052int diff_filespec_is_binary(struct diff_filespec *one)
3053{
Jeff King122aa6f2008-10-05 17:43:36 -04003054 if (one->is_binary == -1) {
3055 diff_filespec_load_driver(one);
3056 if (one->driver->binary != -1)
3057 one->is_binary = one->driver->binary;
3058 else {
3059 if (!one->data && DIFF_FILE_VALID(one))
Nguyễn Thái Ngọc Duy6bf3b812014-08-16 10:08:05 +07003060 diff_populate_filespec(one, CHECK_BINARY);
3061 if (one->is_binary == -1 && one->data)
Jeff King122aa6f2008-10-05 17:43:36 -04003062 one->is_binary = buffer_is_binary(one->data,
3063 one->size);
3064 if (one->is_binary == -1)
3065 one->is_binary = 0;
3066 }
3067 }
Junio C Hamano29a3eef2007-07-06 00:18:54 -07003068 return one->is_binary;
Junio C Hamano6973dca2006-04-21 23:57:45 -07003069}
3070
Jeff Kingbe58e702008-10-05 17:43:21 -04003071static const struct userdiff_funcname *diff_funcname_pattern(struct diff_filespec *one)
Junio C Hamanof2584752007-07-06 00:45:10 -07003072{
Jeff King122aa6f2008-10-05 17:43:36 -04003073 diff_filespec_load_driver(one);
3074 return one->driver->funcname.pattern ? &one->driver->funcname : NULL;
Junio C Hamanof2584752007-07-06 00:45:10 -07003075}
3076
Junio C Hamanoa5a818e2008-08-18 20:08:09 -07003077void diff_set_mnemonic_prefix(struct diff_options *options, const char *a, const char *b)
3078{
3079 if (!options->a_prefix)
3080 options->a_prefix = a;
3081 if (!options->b_prefix)
3082 options->b_prefix = b;
3083}
3084
Axel Bonneta788d7d2010-06-07 17:23:36 +02003085struct userdiff_driver *get_textconv(struct diff_filespec *one)
Jeff King04427ac2008-10-26 00:44:53 -04003086{
3087 if (!DIFF_FILE_VALID(one))
3088 return NULL;
Jeff Kingd391c0f2010-09-21 17:01:24 -04003089
Jeff King04427ac2008-10-26 00:44:53 -04003090 diff_filespec_load_driver(one);
Jeff King3813e692011-05-23 16:30:14 -04003091 return userdiff_get_textconv(one->driver);
Jeff King04427ac2008-10-26 00:44:53 -04003092}
3093
Junio C Hamano6973dca2006-04-21 23:57:45 -07003094static void builtin_diff(const char *name_a,
3095 const char *name_b,
3096 struct diff_filespec *one,
3097 struct diff_filespec *two,
3098 const char *xfrm_msg,
Christian Couder296c6bb2010-05-26 04:50:12 +02003099 int must_show_header,
Junio C Hamano051308f2006-05-04 16:51:44 -07003100 struct diff_options *o,
Junio C Hamano6973dca2006-04-21 23:57:45 -07003101 int complete_rewrite)
3102{
3103 mmfile_t mf1, mf2;
3104 const char *lbl[2];
3105 char *a_one, *b_two;
Jeff Kingd9c552f2012-11-13 21:12:46 +05303106 const char *meta = diff_get_color_opt(o, DIFF_METAINFO);
Pierre Habouzit8f67f8a2007-11-10 20:05:14 +01003107 const char *reset = diff_get_color_opt(o, DIFF_RESET);
Junio C Hamanoa5a818e2008-08-18 20:08:09 -07003108 const char *a_prefix, *b_prefix;
Jeff Kingd9bae1a2010-04-01 20:12:15 -04003109 struct userdiff_driver *textconv_one = NULL;
3110 struct userdiff_driver *textconv_two = NULL;
Greg Bacon3e97c7c2009-11-19 15:12:24 -06003111 struct strbuf header = STRBUF_INIT;
John Keeping30997bb2013-02-07 20:15:27 +00003112 const char *line_prefix = diff_line_prefix(o);
Junio C Hamano6973dca2006-04-21 23:57:45 -07003113
Junio C Hamanoa5a818e2008-08-18 20:08:09 -07003114 diff_set_mnemonic_prefix(o, "a/", "b/");
3115 if (DIFF_OPT_TST(o, REVERSE_DIFF)) {
3116 a_prefix = o->b_prefix;
3117 b_prefix = o->a_prefix;
3118 } else {
3119 a_prefix = o->a_prefix;
3120 b_prefix = o->b_prefix;
3121 }
3122
Jacob Keller61cfbc02016-08-31 16:27:21 -07003123 if (o->submodule_format == DIFF_SUBMODULE_LOG &&
3124 (!one->mode || S_ISGITLINK(one->mode)) &&
3125 (!two->mode || S_ISGITLINK(two->mode))) {
Stefan Bellerf3597132017-06-29 17:07:00 -07003126 show_submodule_summary(o, one->path ? one->path : two->path,
Jacob Keller602a2832016-08-31 16:27:23 -07003127 &one->oid, &two->oid,
Stefan Bellerf3597132017-06-29 17:07:00 -07003128 two->dirty_submodule);
Junio C Hamano6973dca2006-04-21 23:57:45 -07003129 return;
Jacob Kellerfd47ae62016-08-31 16:27:25 -07003130 } else if (o->submodule_format == DIFF_SUBMODULE_INLINE_DIFF &&
3131 (!one->mode || S_ISGITLINK(one->mode)) &&
3132 (!two->mode || S_ISGITLINK(two->mode))) {
Stefan Bellerf3597132017-06-29 17:07:00 -07003133 show_submodule_inline_diff(o, one->path ? one->path : two->path,
Jacob Kellerfd47ae62016-08-31 16:27:25 -07003134 &one->oid, &two->oid,
Stefan Bellerf3597132017-06-29 17:07:00 -07003135 two->dirty_submodule);
Jacob Kellerfd47ae62016-08-31 16:27:25 -07003136 return;
Junio C Hamano6973dca2006-04-21 23:57:45 -07003137 }
3138
3139 if (DIFF_OPT_TST(o, ALLOW_TEXTCONV)) {
3140 textconv_one = get_textconv(one);
3141 textconv_two = get_textconv(two);
3142 }
3143
Linus Torvalds71b989e2008-10-05 15:35:15 -04003144 /* Never use a non-valid filename anywhere if at all possible */
3145 name_a = DIFF_FILE_VALID(one) ? name_a : name_b;
3146 name_b = DIFF_FILE_VALID(two) ? name_b : name_a;
3147
Junio C Hamanoa5a818e2008-08-18 20:08:09 -07003148 a_one = quote_two(a_prefix, name_a + (*name_a == '/'));
3149 b_two = quote_two(b_prefix, name_b + (*name_b == '/'));
Junio C Hamano6973dca2006-04-21 23:57:45 -07003150 lbl[0] = DIFF_FILE_VALID(one) ? a_one : "/dev/null";
3151 lbl[1] = DIFF_FILE_VALID(two) ? b_two : "/dev/null";
Jeff Kingd9c552f2012-11-13 21:12:46 +05303152 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 -07003153 if (lbl[0][0] == '/') {
3154 /* /dev/null */
Jeff Kingd9c552f2012-11-13 21:12:46 +05303155 strbuf_addf(&header, "%s%snew file mode %06o%s\n", line_prefix, meta, two->mode, reset);
Bert Wesarg37466442010-05-04 00:38:07 +02003156 if (xfrm_msg)
3157 strbuf_addstr(&header, xfrm_msg);
Christian Couder296c6bb2010-05-26 04:50:12 +02003158 must_show_header = 1;
Junio C Hamano6973dca2006-04-21 23:57:45 -07003159 }
3160 else if (lbl[1][0] == '/') {
Jeff Kingd9c552f2012-11-13 21:12:46 +05303161 strbuf_addf(&header, "%s%sdeleted file mode %06o%s\n", line_prefix, meta, one->mode, reset);
Bert Wesarg37466442010-05-04 00:38:07 +02003162 if (xfrm_msg)
3163 strbuf_addstr(&header, xfrm_msg);
Christian Couder296c6bb2010-05-26 04:50:12 +02003164 must_show_header = 1;
Junio C Hamano6973dca2006-04-21 23:57:45 -07003165 }
3166 else {
3167 if (one->mode != two->mode) {
Jeff Kingd9c552f2012-11-13 21:12:46 +05303168 strbuf_addf(&header, "%s%sold mode %06o%s\n", line_prefix, meta, one->mode, reset);
3169 strbuf_addf(&header, "%s%snew mode %06o%s\n", line_prefix, meta, two->mode, reset);
Christian Couder296c6bb2010-05-26 04:50:12 +02003170 must_show_header = 1;
Junio C Hamano6973dca2006-04-21 23:57:45 -07003171 }
Bert Wesarg37466442010-05-04 00:38:07 +02003172 if (xfrm_msg)
3173 strbuf_addstr(&header, xfrm_msg);
Greg Bacon3e97c7c2009-11-19 15:12:24 -06003174
Junio C Hamano6973dca2006-04-21 23:57:45 -07003175 /*
3176 * we do not run diff between different kind
3177 * of objects.
3178 */
3179 if ((one->mode ^ two->mode) & S_IFMT)
3180 goto free_ab_and_return;
Jeff King0c018572008-12-09 03:12:28 -05003181 if (complete_rewrite &&
Jeff King3aa1f7c2008-12-09 03:13:21 -05003182 (textconv_one || !diff_filespec_is_binary(one)) &&
3183 (textconv_two || !diff_filespec_is_binary(two))) {
Stefan Bellera29b0a12017-06-29 17:06:57 -07003184 emit_diff_symbol(o, DIFF_SYMBOL_HEADER,
3185 header.buf, header.len, 0);
Greg Bacon3e97c7c2009-11-19 15:12:24 -06003186 strbuf_reset(&header);
Jeff King3aa1f7c2008-12-09 03:13:21 -05003187 emit_rewrite_diff(name_a, name_b, one, two,
3188 textconv_one, textconv_two, o);
Johannes Schindelin34a5e1a2007-02-25 23:34:54 +01003189 o->found_changes = 1;
Junio C Hamano6973dca2006-04-21 23:57:45 -07003190 goto free_ab_and_return;
3191 }
3192 }
3193
Junio C Hamano467ddc12011-02-28 16:11:55 -08003194 if (o->irreversible_delete && lbl[1][0] == '/') {
Stefan Bellera29b0a12017-06-29 17:06:57 -07003195 emit_diff_symbol(o, DIFF_SYMBOL_HEADER, header.buf,
3196 header.len, 0);
Junio C Hamano467ddc12011-02-28 16:11:55 -08003197 strbuf_reset(&header);
3198 goto free_ab_and_return;
3199 } else if (!DIFF_OPT_TST(o, TEXT) &&
Jeff Kingb3373982010-04-01 20:14:24 -04003200 ( (!textconv_one && diff_filespec_is_binary(one)) ||
3201 (!textconv_two && diff_filespec_is_binary(two)) )) {
Stefan Beller4acaaa72017-06-29 17:06:58 -07003202 struct strbuf sb = STRBUF_INIT;
Nguyễn Thái Ngọc Duy1aaf69e2014-08-16 10:08:06 +07003203 if (!one->data && !two->data &&
3204 S_ISREG(one->mode) && S_ISREG(two->mode) &&
3205 !DIFF_OPT_TST(o, BINARY)) {
brian m. carlsona0d12c42016-06-24 23:09:23 +00003206 if (!oidcmp(&one->oid, &two->oid)) {
Nguyễn Thái Ngọc Duy1aaf69e2014-08-16 10:08:06 +07003207 if (must_show_header)
Stefan Bellera29b0a12017-06-29 17:06:57 -07003208 emit_diff_symbol(o, DIFF_SYMBOL_HEADER,
3209 header.buf, header.len,
3210 0);
Nguyễn Thái Ngọc Duy1aaf69e2014-08-16 10:08:06 +07003211 goto free_ab_and_return;
3212 }
Stefan Bellera29b0a12017-06-29 17:06:57 -07003213 emit_diff_symbol(o, DIFF_SYMBOL_HEADER,
3214 header.buf, header.len, 0);
Stefan Beller4acaaa72017-06-29 17:06:58 -07003215 strbuf_addf(&sb, "%sBinary files %s and %s differ\n",
3216 diff_line_prefix(o), lbl[0], lbl[1]);
3217 emit_diff_symbol(o, DIFF_SYMBOL_BINARY_FILES,
3218 sb.buf, sb.len, 0);
3219 strbuf_release(&sb);
Nguyễn Thái Ngọc Duy1aaf69e2014-08-16 10:08:06 +07003220 goto free_ab_and_return;
3221 }
Jeff Kingb3373982010-04-01 20:14:24 -04003222 if (fill_mmfile(&mf1, one) < 0 || fill_mmfile(&mf2, two) < 0)
3223 die("unable to read files to diff");
Junio C Hamano06606262006-05-05 02:41:53 -07003224 /* Quite common confusing case */
3225 if (mf1.size == mf2.size &&
Christian Couder296c6bb2010-05-26 04:50:12 +02003226 !memcmp(mf1.ptr, mf2.ptr, mf1.size)) {
3227 if (must_show_header)
Stefan Bellera29b0a12017-06-29 17:06:57 -07003228 emit_diff_symbol(o, DIFF_SYMBOL_HEADER,
3229 header.buf, header.len, 0);
Junio C Hamano06606262006-05-05 02:41:53 -07003230 goto free_ab_and_return;
Christian Couder296c6bb2010-05-26 04:50:12 +02003231 }
Stefan Bellera29b0a12017-06-29 17:06:57 -07003232 emit_diff_symbol(o, DIFF_SYMBOL_HEADER, header.buf, header.len, 0);
Greg Bacon3e97c7c2009-11-19 15:12:24 -06003233 strbuf_reset(&header);
Pierre Habouzit8f67f8a2007-11-10 20:05:14 +01003234 if (DIFF_OPT_TST(o, BINARY))
Stefan Beller4eed0eb2017-06-29 17:07:01 -07003235 emit_binary_diff(o, &mf1, &mf2);
Stefan Beller4acaaa72017-06-29 17:06:58 -07003236 else {
3237 strbuf_addf(&sb, "%sBinary files %s and %s differ\n",
3238 diff_line_prefix(o), lbl[0], lbl[1]);
3239 emit_diff_symbol(o, DIFF_SYMBOL_BINARY_FILES,
3240 sb.buf, sb.len, 0);
3241 strbuf_release(&sb);
3242 }
Johannes Schindelin34a5e1a2007-02-25 23:34:54 +01003243 o->found_changes = 1;
Junio C Hamano467ddc12011-02-28 16:11:55 -08003244 } else {
Junio C Hamano6973dca2006-04-21 23:57:45 -07003245 /* Crazy xdl interfaces.. */
3246 const char *diffopts = getenv("GIT_DIFF_OPTS");
Jeff Kingae021d82014-06-18 15:47:50 -04003247 const char *v;
Junio C Hamano6973dca2006-04-21 23:57:45 -07003248 xpparam_t xpp;
3249 xdemitconf_t xecfg;
Junio C Hamano6973dca2006-04-21 23:57:45 -07003250 struct emit_callback ecbdata;
Jeff Kingbe58e702008-10-05 17:43:21 -04003251 const struct userdiff_funcname *pe;
Junio C Hamanof2584752007-07-06 00:45:10 -07003252
Junio C Hamanob3f01ff2012-02-29 18:14:16 -08003253 if (must_show_header) {
Stefan Bellera29b0a12017-06-29 17:06:57 -07003254 emit_diff_symbol(o, DIFF_SYMBOL_HEADER,
3255 header.buf, header.len, 0);
Greg Bacon3e97c7c2009-11-19 15:12:24 -06003256 strbuf_reset(&header);
3257 }
3258
Jeff King840383b2010-04-01 20:09:26 -04003259 mf1.size = fill_textconv(textconv_one, one, &mf1.ptr);
3260 mf2.size = fill_textconv(textconv_two, two, &mf2.ptr);
Jeff King04427ac2008-10-26 00:44:53 -04003261
Brandon Casey45e7ca02008-09-18 17:40:48 -05003262 pe = diff_funcname_pattern(one);
3263 if (!pe)
3264 pe = diff_funcname_pattern(two);
Junio C Hamano6973dca2006-04-21 23:57:45 -07003265
Brian Downing9ccd0a82008-10-25 15:30:37 +02003266 memset(&xpp, 0, sizeof(xpp));
Johannes Schindelin30b25012007-07-04 19:05:46 +01003267 memset(&xecfg, 0, sizeof(xecfg));
Johannes Schindelincd112ce2006-06-13 18:45:44 +02003268 memset(&ecbdata, 0, sizeof(ecbdata));
Junio C Hamano6973dca2006-04-21 23:57:45 -07003269 ecbdata.label_path = lbl;
Jeff Kingdaa0c3d2011-08-17 22:04:23 -07003270 ecbdata.color_diff = want_color(o->use_color);
Stefan Bellerc189c4f2013-08-08 20:31:44 +02003271 ecbdata.ws_rule = whitespace_rule(name_b);
Junio C Hamano690ed842009-09-04 00:41:15 -07003272 if (ecbdata.ws_rule & WS_BLANK_AT_EOF)
Junio C Hamanod68fe262009-09-14 22:05:57 -07003273 check_blank_at_eof(&mf1, &mf2, &ecbdata);
Bo Yanga3c158d2010-05-26 15:08:02 +08003274 ecbdata.opt = o;
Greg Bacon3e97c7c2009-11-19 15:12:24 -06003275 ecbdata.header = header.len ? &header : NULL;
René Scharfe582aa002010-05-02 15:04:41 +02003276 xpp.flags = o->xdl_opts;
Linus Torvaldsee1e5412006-05-13 13:23:48 -07003277 xecfg.ctxlen = o->context;
René Scharfe6d0e6742008-12-28 19:45:32 +01003278 xecfg.interhunkctxlen = o->interhunkcontext;
Junio C Hamano6973dca2006-04-21 23:57:45 -07003279 xecfg.flags = XDL_EMIT_FUNCNAMES;
René Scharfe14937c22011-10-09 13:36:57 +02003280 if (DIFF_OPT_TST(o, FUNCCONTEXT))
3281 xecfg.flags |= XDL_EMIT_FUNCCONTEXT;
Brandon Casey45e7ca02008-09-18 17:40:48 -05003282 if (pe)
Brandon Caseya0135852008-09-18 17:42:48 -05003283 xdiff_set_find_func(&xecfg, pe->pattern, pe->cflags);
Junio C Hamano6973dca2006-04-21 23:57:45 -07003284 if (!diffopts)
3285 ;
Jeff Kingae021d82014-06-18 15:47:50 -04003286 else if (skip_prefix(diffopts, "--unified=", &v))
3287 xecfg.ctxlen = strtoul(v, NULL, 10);
3288 else if (skip_prefix(diffopts, "-u", &v))
3289 xecfg.ctxlen = strtoul(v, NULL, 10);
Thomas Rast77d1a522012-03-14 19:24:08 +01003290 if (o->word_diff)
3291 init_diff_words_data(&ecbdata, o, one, two);
Jeff King3efb9882015-09-24 19:12:23 -04003292 if (xdi_diff_outf(&mf1, &mf2, fn_out_consume, &ecbdata,
3293 &xpp, &xecfg))
3294 die("unable to generate diff for %s", one->path);
Thomas Rast882749a2010-04-14 17:59:06 +02003295 if (o->word_diff)
Johannes Schindelinf59a59e2006-07-28 23:56:15 +02003296 free_diff_words_data(&ecbdata);
Jeff King04427ac2008-10-26 00:44:53 -04003297 if (textconv_one)
3298 free(mf1.ptr);
3299 if (textconv_two)
3300 free(mf2.ptr);
René Scharfe8cfe5f12009-07-02 00:01:43 +02003301 xdiff_clear_find_func(&xecfg);
Junio C Hamano6973dca2006-04-21 23:57:45 -07003302 }
3303
3304 free_ab_and_return:
Greg Bacon3e97c7c2009-11-19 15:12:24 -06003305 strbuf_release(&header);
Junio C Hamanofc3abdf2007-05-03 13:05:48 -07003306 diff_free_filespec_data(one);
3307 diff_free_filespec_data(two);
Junio C Hamano6973dca2006-04-21 23:57:45 -07003308 free(a_one);
3309 free(b_two);
3310 return;
3311}
3312
3313static void builtin_diffstat(const char *name_a, const char *name_b,
3314 struct diff_filespec *one,
3315 struct diff_filespec *two,
Junio C Hamano710158e2006-04-25 23:40:09 -07003316 struct diffstat_t *diffstat,
Johannes Schindelin0d21efa2006-06-14 17:40:23 +02003317 struct diff_options *o,
Linus Torvalds74faaa12012-10-17 10:00:37 -07003318 struct diff_filepair *p)
Junio C Hamano6973dca2006-04-21 23:57:45 -07003319{
3320 mmfile_t mf1, mf2;
3321 struct diffstat_file *data;
Zbigniew Jędrzejewski-Szmek352ca4e2012-05-01 19:10:15 +02003322 int same_contents;
Linus Torvalds74faaa12012-10-17 10:00:37 -07003323 int complete_rewrite = 0;
3324
3325 if (!DIFF_PAIR_UNMERGED(p)) {
3326 if (p->status == DIFF_STATUS_MODIFIED && p->score)
3327 complete_rewrite = 1;
3328 }
Junio C Hamano6973dca2006-04-21 23:57:45 -07003329
3330 data = diffstat_add(diffstat, name_a, name_b);
Junio C Hamano99bfd402012-11-27 11:17:14 -08003331 data->is_interesting = p->status != DIFF_STATUS_UNKNOWN;
Junio C Hamano6973dca2006-04-21 23:57:45 -07003332
3333 if (!one || !two) {
3334 data->is_unmerged = 1;
3335 return;
3336 }
Jeff Kingded0abc2011-02-19 03:04:56 -05003337
brian m. carlsona0d12c42016-06-24 23:09:23 +00003338 same_contents = !oidcmp(&one->oid, &two->oid);
Zbigniew Jędrzejewski-Szmek352ca4e2012-05-01 19:10:15 +02003339
Jeff Kingded0abc2011-02-19 03:04:56 -05003340 if (diff_filespec_is_binary(one) || diff_filespec_is_binary(two)) {
Jeff Kingded0abc2011-02-19 03:04:56 -05003341 data->is_binary = 1;
Zbigniew Jędrzejewski-Szmek352ca4e2012-05-01 19:10:15 +02003342 if (same_contents) {
Zbigniew Jędrzejewski-Szmeke18872b2012-05-01 19:10:14 +02003343 data->added = 0;
3344 data->deleted = 0;
3345 } else {
3346 data->added = diff_filespec_size(two);
3347 data->deleted = diff_filespec_size(one);
3348 }
Jeff Kingded0abc2011-02-19 03:04:56 -05003349 }
3350
3351 else if (complete_rewrite) {
Junio C Hamano710158e2006-04-25 23:40:09 -07003352 diff_populate_filespec(one, 0);
3353 diff_populate_filespec(two, 0);
3354 data->deleted = count_lines(one->data, one->size);
3355 data->added = count_lines(two->data, two->size);
Junio C Hamano710158e2006-04-25 23:40:09 -07003356 }
Junio C Hamano6973dca2006-04-21 23:57:45 -07003357
Zbigniew Jędrzejewski-Szmek352ca4e2012-05-01 19:10:15 +02003358 else if (!same_contents) {
Junio C Hamano6973dca2006-04-21 23:57:45 -07003359 /* Crazy xdl interfaces.. */
3360 xpparam_t xpp;
3361 xdemitconf_t xecfg;
Junio C Hamano6973dca2006-04-21 23:57:45 -07003362
Jeff Kingded0abc2011-02-19 03:04:56 -05003363 if (fill_mmfile(&mf1, one) < 0 || fill_mmfile(&mf2, two) < 0)
3364 die("unable to read files to diff");
3365
Brian Downing9ccd0a82008-10-25 15:30:37 +02003366 memset(&xpp, 0, sizeof(xpp));
Johannes Schindelin30b25012007-07-04 19:05:46 +01003367 memset(&xecfg, 0, sizeof(xecfg));
René Scharfe582aa002010-05-02 15:04:41 +02003368 xpp.flags = o->xdl_opts;
Junio C Hamanof01cae92011-09-22 10:54:47 -07003369 xecfg.ctxlen = o->context;
3370 xecfg.interhunkctxlen = o->interhunkcontext;
Jeff King3efb9882015-09-24 19:12:23 -04003371 if (xdi_diff_outf(&mf1, &mf2, diffstat_consume, diffstat,
3372 &xpp, &xecfg))
3373 die("unable to generate diffstat for %s", one->path);
Junio C Hamano6973dca2006-04-21 23:57:45 -07003374 }
Junio C Hamanofc3abdf2007-05-03 13:05:48 -07003375
Junio C Hamanofc3abdf2007-05-03 13:05:48 -07003376 diff_free_filespec_data(one);
3377 diff_free_filespec_data(two);
Junio C Hamano6973dca2006-04-21 23:57:45 -07003378}
3379
Johannes Schindelin88246892006-05-20 23:43:13 +02003380static void builtin_checkdiff(const char *name_a, const char *name_b,
Junio C Hamanocd676a52008-02-12 14:26:02 -08003381 const char *attr_path,
Junio C Hamano5ff10dd2008-06-26 15:34:54 -07003382 struct diff_filespec *one,
3383 struct diff_filespec *two,
3384 struct diff_options *o)
Johannes Schindelin88246892006-05-20 23:43:13 +02003385{
3386 mmfile_t mf1, mf2;
3387 struct checkdiff_t data;
3388
3389 if (!two)
3390 return;
3391
3392 memset(&data, 0, sizeof(data));
Johannes Schindelin88246892006-05-20 23:43:13 +02003393 data.filename = name_b ? name_b : name_a;
3394 data.lineno = 0;
Junio C Hamano1ba111d2008-06-26 15:36:34 -07003395 data.o = o;
Junio C Hamanocd676a52008-02-12 14:26:02 -08003396 data.ws_rule = whitespace_rule(attr_path);
Junio C Hamanoa757c642010-03-24 19:21:32 -07003397 data.conflict_marker_size = ll_merge_marker_size(attr_path);
Johannes Schindelin88246892006-05-20 23:43:13 +02003398
3399 if (fill_mmfile(&mf1, one) < 0 || fill_mmfile(&mf2, two) < 0)
3400 die("unable to read files to diff");
3401
Junio C Hamano5ff10dd2008-06-26 15:34:54 -07003402 /*
3403 * All the other codepaths check both sides, but not checking
3404 * the "old" side here is deliberate. We are checking the newly
3405 * introduced changes, and as long as the "new" side is text, we
3406 * can and should check what it introduces.
3407 */
Junio C Hamano29a3eef2007-07-06 00:18:54 -07003408 if (diff_filespec_is_binary(two))
Junio C Hamanofc3abdf2007-05-03 13:05:48 -07003409 goto free_and_return;
Johannes Schindelin88246892006-05-20 23:43:13 +02003410 else {
3411 /* Crazy xdl interfaces.. */
3412 xpparam_t xpp;
3413 xdemitconf_t xecfg;
Johannes Schindelin88246892006-05-20 23:43:13 +02003414
Brian Downing9ccd0a82008-10-25 15:30:37 +02003415 memset(&xpp, 0, sizeof(xpp));
Johannes Schindelin30b25012007-07-04 19:05:46 +01003416 memset(&xecfg, 0, sizeof(xecfg));
Junio C Hamanoc35539e2008-08-20 11:47:55 -07003417 xecfg.ctxlen = 1; /* at least one context line */
René Scharfe582aa002010-05-02 15:04:41 +02003418 xpp.flags = 0;
Jeff King3efb9882015-09-24 19:12:23 -04003419 if (xdi_diff_outf(&mf1, &mf2, checkdiff_consume, &data,
3420 &xpp, &xecfg))
3421 die("unable to generate checkdiff for %s", one->path);
Junio C Hamano877f23c2008-06-26 15:36:59 -07003422
Junio C Hamano467babf2009-09-03 23:39:43 -07003423 if (data.ws_rule & WS_BLANK_AT_EOF) {
Junio C Hamanod68fe262009-09-14 22:05:57 -07003424 struct emit_callback ecbdata;
3425 int blank_at_eof;
3426
3427 ecbdata.ws_rule = data.ws_rule;
3428 check_blank_at_eof(&mf1, &mf2, &ecbdata);
Christoph Mallon8837d332010-10-10 19:24:06 +02003429 blank_at_eof = ecbdata.blank_at_eof_in_postimage;
Junio C Hamanod68fe262009-09-14 22:05:57 -07003430
Junio C Hamano467babf2009-09-03 23:39:43 -07003431 if (blank_at_eof) {
3432 static char *err;
3433 if (!err)
3434 err = whitespace_error_string(WS_BLANK_AT_EOF);
3435 fprintf(o->file, "%s:%d: %s.\n",
3436 data.filename, blank_at_eof, err);
3437 data.status = 1; /* report errors */
3438 }
Junio C Hamano877f23c2008-06-26 15:36:59 -07003439 }
Johannes Schindelin88246892006-05-20 23:43:13 +02003440 }
Junio C Hamanofc3abdf2007-05-03 13:05:48 -07003441 free_and_return:
3442 diff_free_filespec_data(one);
3443 diff_free_filespec_data(two);
Wincent Colaiuta62c64892007-12-13 21:24:52 +01003444 if (data.status)
3445 DIFF_OPT_SET(o, CHECK_FAILED);
Johannes Schindelin88246892006-05-20 23:43:13 +02003446}
3447
Junio C Hamano6973dca2006-04-21 23:57:45 -07003448struct diff_filespec *alloc_filespec(const char *path)
3449{
Jeff King96ffc062016-02-22 17:44:32 -05003450 struct diff_filespec *spec;
Junio C Hamano6973dca2006-04-21 23:57:45 -07003451
Jeff King96ffc062016-02-22 17:44:32 -05003452 FLEXPTR_ALLOC_STR(spec, path, path);
Linus Torvalds9fb88412007-10-25 11:19:10 -07003453 spec->count = 1;
Jeff King122aa6f2008-10-05 17:43:36 -04003454 spec->is_binary = -1;
Junio C Hamano6973dca2006-04-21 23:57:45 -07003455 return spec;
3456}
3457
Linus Torvalds9fb88412007-10-25 11:19:10 -07003458void free_filespec(struct diff_filespec *spec)
3459{
3460 if (!--spec->count) {
3461 diff_free_filespec_data(spec);
3462 free(spec);
3463 }
3464}
3465
Brandon Williamsf9704c22017-05-30 10:30:50 -07003466void fill_filespec(struct diff_filespec *spec, const struct object_id *oid,
3467 int oid_valid, unsigned short mode)
Junio C Hamano6973dca2006-04-21 23:57:45 -07003468{
3469 if (mode) {
3470 spec->mode = canon_mode(mode);
Brandon Williamsf9704c22017-05-30 10:30:50 -07003471 oidcpy(&spec->oid, oid);
3472 spec->oid_valid = oid_valid;
Junio C Hamano6973dca2006-04-21 23:57:45 -07003473 }
3474}
3475
3476/*
Jakub Narebski5adf3172007-05-26 00:37:40 +02003477 * Given a name and sha1 pair, if the index tells us the file in
Junio C Hamano6973dca2006-04-21 23:57:45 -07003478 * the work tree has that object contents, return true, so that
3479 * prepare_temp_file() does not have to inflate and extract.
3480 */
Brandon Williamsfb4a1c02017-05-30 10:30:51 -07003481static int reuse_worktree_file(const char *name, const struct object_id *oid, int want_file)
Junio C Hamano6973dca2006-04-21 23:57:45 -07003482{
Nguyễn Thái Ngọc Duy9c5e6c82013-07-09 22:29:00 +07003483 const struct cache_entry *ce;
Junio C Hamano6973dca2006-04-21 23:57:45 -07003484 struct stat st;
3485 int pos, len;
3486
Junio C Hamano150115a2009-03-22 15:26:07 -07003487 /*
3488 * We do not read the cache ourselves here, because the
Junio C Hamano6973dca2006-04-21 23:57:45 -07003489 * benchmark with my previous version that always reads cache
3490 * shows that it makes things worse for diff-tree comparing
3491 * two linux-2.6 kernel trees in an already checked out work
3492 * tree. This is because most diff-tree comparisons deal with
3493 * only a small number of files, while reading the cache is
3494 * expensive for a large project, and its cost outweighs the
3495 * savings we get by not inflating the object to a temporary
3496 * file. Practically, this code only helps when we are used
3497 * by diff-cache --cached, which does read the cache before
3498 * calling us.
3499 */
3500 if (!active_cache)
3501 return 0;
3502
Shawn O. Pearce1510fea2006-12-14 06:15:57 -05003503 /* We want to avoid the working directory if our caller
3504 * doesn't need the data in a normal file, this system
3505 * is rather slow with its stat/open/mmap/close syscalls,
3506 * and the object is contained in a pack file. The pack
3507 * is probably already open and will be faster to obtain
3508 * the data through than the working directory. Loose
3509 * objects however would tend to be slower as they need
3510 * to be individually opened and inflated.
3511 */
Brandon Williamsfb4a1c02017-05-30 10:30:51 -07003512 if (!FAST_WORKING_DIRECTORY && !want_file && has_sha1_pack(oid->hash))
Shawn O. Pearce1510fea2006-12-14 06:15:57 -05003513 return 0;
3514
Jeff King06dec432016-07-22 11:27:53 -04003515 /*
3516 * Similarly, if we'd have to convert the file contents anyway, that
3517 * makes the optimization not worthwhile.
3518 */
Brandon Williams82b474e2017-06-12 15:13:55 -07003519 if (!want_file && would_convert_to_git(&the_index, name))
Jeff King06dec432016-07-22 11:27:53 -04003520 return 0;
3521
Junio C Hamano6973dca2006-04-21 23:57:45 -07003522 len = strlen(name);
3523 pos = cache_name_pos(name, len);
3524 if (pos < 0)
3525 return 0;
3526 ce = active_cache[pos];
Junio C Hamanoeadb5832008-01-18 23:45:24 -08003527
3528 /*
3529 * This is not the sha1 we are looking for, or
3530 * unreusable because it is not a regular file.
Junio C Hamano6973dca2006-04-21 23:57:45 -07003531 */
Brandon Williamsfb4a1c02017-05-30 10:30:51 -07003532 if (oidcmp(oid, &ce->oid) || !S_ISREG(ce->ce_mode))
Junio C Hamanoeadb5832008-01-18 23:45:24 -08003533 return 0;
3534
3535 /*
Junio C Hamano150115a2009-03-22 15:26:07 -07003536 * If ce is marked as "assume unchanged", there is no
3537 * guarantee that work tree matches what we are looking for.
3538 */
Nguyễn Thái Ngọc Duyb4d16902009-08-20 20:46:58 +07003539 if ((ce->ce_flags & CE_VALID) || ce_skip_worktree(ce))
Junio C Hamano150115a2009-03-22 15:26:07 -07003540 return 0;
3541
3542 /*
Junio C Hamanoeadb5832008-01-18 23:45:24 -08003543 * If ce matches the file in the work tree, we can reuse it.
3544 */
3545 if (ce_uptodate(ce) ||
3546 (!lstat(name, &st) && !ce_match_stat(ce, &st, 0)))
3547 return 1;
3548
3549 return 0;
Junio C Hamano6973dca2006-04-21 23:57:45 -07003550}
3551
Linus Torvalds04786752007-04-15 11:14:28 -07003552static int diff_populate_gitlink(struct diff_filespec *s, int size_only)
3553{
Jeff Kingb1ddfb92016-02-22 17:45:08 -05003554 struct strbuf buf = STRBUF_INIT;
3555 char *dirty = "";
Junio C Hamano8e08b412010-01-16 18:42:53 +01003556
3557 /* Are we looking at the work tree? */
Jens Lehmann85adbf22010-03-12 22:23:52 +01003558 if (s->dirty_submodule)
Junio C Hamano8e08b412010-01-16 18:42:53 +01003559 dirty = "-dirty";
3560
brian m. carlsona0d12c42016-06-24 23:09:23 +00003561 strbuf_addf(&buf, "Subproject commit %s%s\n",
3562 oid_to_hex(&s->oid), dirty);
Jeff Kingb1ddfb92016-02-22 17:45:08 -05003563 s->size = buf.len;
Linus Torvalds04786752007-04-15 11:14:28 -07003564 if (size_only) {
3565 s->data = NULL;
Jeff Kingb1ddfb92016-02-22 17:45:08 -05003566 strbuf_release(&buf);
3567 } else {
3568 s->data = strbuf_detach(&buf, NULL);
3569 s->should_free = 1;
Linus Torvalds04786752007-04-15 11:14:28 -07003570 }
3571 return 0;
3572}
3573
Junio C Hamano6973dca2006-04-21 23:57:45 -07003574/*
3575 * While doing rename detection and pickaxe operation, we may need to
3576 * grab the data for the blob (or file) for our own in-core comparison.
3577 * diff_filespec has data and size fields for this purpose.
3578 */
Nguyễn Thái Ngọc Duy8e5dd3d2014-08-16 10:08:04 +07003579int diff_populate_filespec(struct diff_filespec *s, unsigned int flags)
Junio C Hamano6973dca2006-04-21 23:57:45 -07003580{
Nguyễn Thái Ngọc Duy8e5dd3d2014-08-16 10:08:04 +07003581 int size_only = flags & CHECK_SIZE_ONLY;
Junio C Hamano6973dca2006-04-21 23:57:45 -07003582 int err = 0;
Junio C Hamano5430bb22013-06-24 14:35:04 -07003583 /*
3584 * demote FAIL to WARN to allow inspecting the situation
3585 * instead of refusing.
3586 */
3587 enum safe_crlf crlf_warn = (safe_crlf == SAFE_CRLF_FAIL
3588 ? SAFE_CRLF_WARN
3589 : safe_crlf);
3590
Junio C Hamano6973dca2006-04-21 23:57:45 -07003591 if (!DIFF_FILE_VALID(s))
3592 die("internal error: asking to populate invalid file.");
3593 if (S_ISDIR(s->mode))
3594 return -1;
3595
Junio C Hamano6973dca2006-04-21 23:57:45 -07003596 if (s->data)
Junio C Hamanofc3abdf2007-05-03 13:05:48 -07003597 return 0;
Linus Torvalds04786752007-04-15 11:14:28 -07003598
Junio C Hamano6e0b8ed2007-05-07 01:14:21 -07003599 if (size_only && 0 < s->size)
3600 return 0;
3601
Martin Waitz302b9282007-05-21 22:08:28 +02003602 if (S_ISGITLINK(s->mode))
Linus Torvalds04786752007-04-15 11:14:28 -07003603 return diff_populate_gitlink(s, size_only);
3604
brian m. carlson41c95602016-06-24 23:09:24 +00003605 if (!s->oid_valid ||
Brandon Williamsfb4a1c02017-05-30 10:30:51 -07003606 reuse_worktree_file(s->path, &s->oid, 0)) {
Brandon Caseyf285a2d2008-10-09 14:12:12 -05003607 struct strbuf buf = STRBUF_INIT;
Junio C Hamano6973dca2006-04-21 23:57:45 -07003608 struct stat st;
3609 int fd;
Linus Torvalds6c510be2007-02-13 11:07:23 -08003610
Junio C Hamano6973dca2006-04-21 23:57:45 -07003611 if (lstat(s->path, &st) < 0) {
3612 if (errno == ENOENT) {
3613 err_empty:
3614 err = -1;
3615 empty:
Timo Hirvonend2543b82006-06-24 20:20:32 +03003616 s->data = (char *)"";
Junio C Hamano6973dca2006-04-21 23:57:45 -07003617 s->size = 0;
3618 return err;
3619 }
3620 }
Shawn O. Pearcedc49cd72007-03-06 20:44:37 -05003621 s->size = xsize_t(st.st_size);
Junio C Hamano6973dca2006-04-21 23:57:45 -07003622 if (!s->size)
3623 goto empty;
Junio C Hamano6973dca2006-04-21 23:57:45 -07003624 if (S_ISLNK(st.st_mode)) {
Linus Torvaldscf219d82008-12-17 10:26:13 -08003625 struct strbuf sb = STRBUF_INIT;
3626
3627 if (strbuf_readlink(&sb, s->path, s->size))
Junio C Hamano6973dca2006-04-21 23:57:45 -07003628 goto err_empty;
René Scharfe0956a6d2008-12-18 17:56:51 +01003629 s->size = sb.len;
3630 s->data = strbuf_detach(&sb, NULL);
Linus Torvaldscf219d82008-12-17 10:26:13 -08003631 s->should_free = 1;
Junio C Hamano6973dca2006-04-21 23:57:45 -07003632 return 0;
3633 }
Junio C Hamano12426e12017-03-01 18:04:44 +01003634
3635 /*
3636 * Even if the caller would be happy with getting
3637 * only the size, we cannot return early at this
3638 * point if the path requires us to run the content
3639 * conversion.
3640 */
Brandon Williams82b474e2017-06-12 15:13:55 -07003641 if (size_only && !would_convert_to_git(&the_index, s->path))
Linus Torvaldscf219d82008-12-17 10:26:13 -08003642 return 0;
Junio C Hamano12426e12017-03-01 18:04:44 +01003643
3644 /*
3645 * Note: this check uses xsize_t(st.st_size) that may
3646 * not be the true size of the blob after it goes
3647 * through convert_to_git(). This may not strictly be
3648 * correct, but the whole point of big_file_threshold
3649 * and is_binary check being that we want to avoid
3650 * opening the file and inspecting the contents, this
3651 * is probably fine.
3652 */
Nguyễn Thái Ngọc Duy6bf3b812014-08-16 10:08:05 +07003653 if ((flags & CHECK_BINARY) &&
3654 s->size > big_file_threshold && s->is_binary == -1) {
3655 s->is_binary = 1;
3656 return 0;
3657 }
Junio C Hamano6973dca2006-04-21 23:57:45 -07003658 fd = open(s->path, O_RDONLY);
3659 if (fd < 0)
3660 goto err_empty;
Shawn O. Pearcec4712e42006-12-24 00:47:23 -05003661 s->data = xmmap(NULL, s->size, PROT_READ, MAP_PRIVATE, fd, 0);
Junio C Hamano6973dca2006-04-21 23:57:45 -07003662 close(fd);
Junio C Hamano6973dca2006-04-21 23:57:45 -07003663 s->should_munmap = 1;
Linus Torvalds6c510be2007-02-13 11:07:23 -08003664
3665 /*
3666 * Convert from working tree format to canonical git format
3667 */
Brandon Williams82b474e2017-06-12 15:13:55 -07003668 if (convert_to_git(&the_index, s->path, s->data, s->size, &buf, crlf_warn)) {
René Scharfec32f7492007-10-21 11:23:49 +02003669 size_t size = 0;
Linus Torvalds6c510be2007-02-13 11:07:23 -08003670 munmap(s->data, s->size);
3671 s->should_munmap = 0;
René Scharfec32f7492007-10-21 11:23:49 +02003672 s->data = strbuf_detach(&buf, &size);
3673 s->size = size;
Linus Torvalds6c510be2007-02-13 11:07:23 -08003674 s->should_free = 1;
3675 }
Junio C Hamano6973dca2006-04-21 23:57:45 -07003676 }
3677 else {
Nicolas Pitre21666f12007-02-26 14:55:59 -05003678 enum object_type type;
Nguyễn Thái Ngọc Duy6bf3b812014-08-16 10:08:05 +07003679 if (size_only || (flags & CHECK_BINARY)) {
brian m. carlsona0d12c42016-06-24 23:09:23 +00003680 type = sha1_object_info(s->oid.hash, &s->size);
Nicolas Pitrec50c4312010-10-22 00:02:27 -04003681 if (type < 0)
brian m. carlsona0d12c42016-06-24 23:09:23 +00003682 die("unable to read %s",
3683 oid_to_hex(&s->oid));
Nguyễn Thái Ngọc Duy6bf3b812014-08-16 10:08:05 +07003684 if (size_only)
3685 return 0;
3686 if (s->size > big_file_threshold && s->is_binary == -1) {
3687 s->is_binary = 1;
3688 return 0;
3689 }
Junio C Hamano6973dca2006-04-21 23:57:45 -07003690 }
brian m. carlsona0d12c42016-06-24 23:09:23 +00003691 s->data = read_sha1_file(s->oid.hash, &type, &s->size);
Nguyễn Thái Ngọc Duy6bf3b812014-08-16 10:08:05 +07003692 if (!s->data)
brian m. carlsona0d12c42016-06-24 23:09:23 +00003693 die("unable to read %s", oid_to_hex(&s->oid));
Nguyễn Thái Ngọc Duy6bf3b812014-08-16 10:08:05 +07003694 s->should_free = 1;
Junio C Hamano6973dca2006-04-21 23:57:45 -07003695 }
3696 return 0;
3697}
3698
Junio C Hamano8ae92e62007-10-02 21:01:03 -07003699void diff_free_filespec_blob(struct diff_filespec *s)
Junio C Hamano6973dca2006-04-21 23:57:45 -07003700{
3701 if (s->should_free)
3702 free(s->data);
3703 else if (s->should_munmap)
3704 munmap(s->data, s->size);
Junio C Hamanofc3abdf2007-05-03 13:05:48 -07003705
3706 if (s->should_free || s->should_munmap) {
3707 s->should_free = s->should_munmap = 0;
3708 s->data = NULL;
3709 }
Jeff Kingeede7b72007-09-25 15:29:42 -04003710}
3711
3712void diff_free_filespec_data(struct diff_filespec *s)
3713{
Junio C Hamano8ae92e62007-10-02 21:01:03 -07003714 diff_free_filespec_blob(s);
Ævar Arnfjörð Bjarmason6a83d902017-06-15 23:15:46 +00003715 FREE_AND_NULL(s->cnt_data);
Junio C Hamano6973dca2006-04-21 23:57:45 -07003716}
3717
Johannes Schindelin4e218f52009-03-21 12:42:52 +01003718static void prep_temp_blob(const char *path, struct diff_tempfile *temp,
Junio C Hamano6973dca2006-04-21 23:57:45 -07003719 void *blob,
3720 unsigned long size,
brian m. carlson09bdff22016-06-24 23:09:29 +00003721 const struct object_id *oid,
Junio C Hamano6973dca2006-04-21 23:57:45 -07003722 int mode)
3723{
Johannes Schindelin4e218f52009-03-21 12:42:52 +01003724 struct strbuf buf = STRBUF_INIT;
David Aguilar003b33a2009-05-31 01:35:52 -07003725 struct strbuf template = STRBUF_INIT;
3726 char *path_dup = xstrdup(path);
3727 const char *base = basename(path_dup);
Junio C Hamano6973dca2006-04-21 23:57:45 -07003728
David Aguilar003b33a2009-05-31 01:35:52 -07003729 /* Generate "XXXXXX_basename.ext" */
3730 strbuf_addstr(&template, "XXXXXX_");
3731 strbuf_addstr(&template, base);
3732
Jeff King076aa2c2017-09-05 08:15:08 -04003733 temp->tempfile = mks_tempfile_ts(template.buf, strlen(base) + 1);
3734 if (!temp->tempfile)
Thomas Rastd824cbb2009-06-27 17:58:46 +02003735 die_errno("unable to create temp-file");
Johannes Schindelin4e218f52009-03-21 12:42:52 +01003736 if (convert_to_working_tree(path,
3737 (const char *)blob, (size_t)size, &buf)) {
3738 blob = buf.buf;
3739 size = buf.len;
3740 }
Junio C Hamanoc50424a2017-09-25 15:24:06 +09003741 if (write_in_full(temp->tempfile->fd, blob, size) < 0 ||
Jeff King076aa2c2017-09-05 08:15:08 -04003742 close_tempfile_gently(temp->tempfile))
Thomas Rast0721c312009-06-27 17:58:47 +02003743 die_errno("unable to write temp-file");
Jeff King076aa2c2017-09-05 08:15:08 -04003744 temp->name = get_tempfile_path(temp->tempfile);
brian m. carlson09bdff22016-06-24 23:09:29 +00003745 oid_to_hex_r(temp->hex, oid);
Jeff King5096d492015-09-24 17:06:08 -04003746 xsnprintf(temp->mode, sizeof(temp->mode), "%06o", mode);
Johannes Schindelin4e218f52009-03-21 12:42:52 +01003747 strbuf_release(&buf);
David Aguilar003b33a2009-05-31 01:35:52 -07003748 strbuf_release(&template);
3749 free(path_dup);
Junio C Hamano6973dca2006-04-21 23:57:45 -07003750}
3751
Jeff King479b0ae2009-01-22 00:59:56 -05003752static struct diff_tempfile *prepare_temp_file(const char *name,
3753 struct diff_filespec *one)
Junio C Hamano6973dca2006-04-21 23:57:45 -07003754{
Jeff King479b0ae2009-01-22 00:59:56 -05003755 struct diff_tempfile *temp = claim_diff_tempfile();
3756
Junio C Hamano6973dca2006-04-21 23:57:45 -07003757 if (!DIFF_FILE_VALID(one)) {
3758 not_a_valid_file:
3759 /* A '-' entry produces this for file-2, and
3760 * a '+' entry produces this for file-1.
3761 */
3762 temp->name = "/dev/null";
Jeff King5096d492015-09-24 17:06:08 -04003763 xsnprintf(temp->hex, sizeof(temp->hex), ".");
3764 xsnprintf(temp->mode, sizeof(temp->mode), ".");
Jeff King479b0ae2009-01-22 00:59:56 -05003765 return temp;
3766 }
3767
Thomas Rastaba47272014-02-16 17:52:34 +01003768 if (!S_ISGITLINK(one->mode) &&
brian m. carlson41c95602016-06-24 23:09:24 +00003769 (!one->oid_valid ||
Brandon Williamsfb4a1c02017-05-30 10:30:51 -07003770 reuse_worktree_file(name, &one->oid, 1))) {
Junio C Hamano6973dca2006-04-21 23:57:45 -07003771 struct stat st;
3772 if (lstat(name, &st) < 0) {
3773 if (errno == ENOENT)
3774 goto not_a_valid_file;
Thomas Rastd824cbb2009-06-27 17:58:46 +02003775 die_errno("stat(%s)", name);
Junio C Hamano6973dca2006-04-21 23:57:45 -07003776 }
3777 if (S_ISLNK(st.st_mode)) {
Jeff King3cd73882009-05-25 06:46:09 -04003778 struct strbuf sb = STRBUF_INIT;
3779 if (strbuf_readlink(&sb, name, st.st_size) < 0)
Thomas Rast0721c312009-06-27 17:58:47 +02003780 die_errno("readlink(%s)", name);
Jeff King3cd73882009-05-25 06:46:09 -04003781 prep_temp_blob(name, temp, sb.buf, sb.len,
brian m. carlson41c95602016-06-24 23:09:24 +00003782 (one->oid_valid ?
brian m. carlson09bdff22016-06-24 23:09:29 +00003783 &one->oid : &null_oid),
brian m. carlson41c95602016-06-24 23:09:24 +00003784 (one->oid_valid ?
Junio C Hamano6973dca2006-04-21 23:57:45 -07003785 one->mode : S_IFLNK));
Jeff King3cd73882009-05-25 06:46:09 -04003786 strbuf_release(&sb);
Junio C Hamano6973dca2006-04-21 23:57:45 -07003787 }
3788 else {
3789 /* we can borrow from the file in the work tree */
3790 temp->name = name;
brian m. carlson41c95602016-06-24 23:09:24 +00003791 if (!one->oid_valid)
Brandon Williams74014152017-05-30 10:30:52 -07003792 oid_to_hex_r(temp->hex, &null_oid);
Junio C Hamano6973dca2006-04-21 23:57:45 -07003793 else
René Scharfe24905742017-01-28 23:03:03 +01003794 oid_to_hex_r(temp->hex, &one->oid);
Junio C Hamano6973dca2006-04-21 23:57:45 -07003795 /* Even though we may sometimes borrow the
3796 * contents from the work tree, we always want
3797 * one->mode. mode is trustworthy even when
Brandon Williams74014152017-05-30 10:30:52 -07003798 * !(one->oid_valid), as long as
Junio C Hamano6973dca2006-04-21 23:57:45 -07003799 * DIFF_FILE_VALID(one).
3800 */
Jeff King5096d492015-09-24 17:06:08 -04003801 xsnprintf(temp->mode, sizeof(temp->mode), "%06o", one->mode);
Junio C Hamano6973dca2006-04-21 23:57:45 -07003802 }
Jeff King479b0ae2009-01-22 00:59:56 -05003803 return temp;
Junio C Hamano6973dca2006-04-21 23:57:45 -07003804 }
3805 else {
3806 if (diff_populate_filespec(one, 0))
3807 die("cannot read data blob for %s", one->path);
Johannes Schindelin4e218f52009-03-21 12:42:52 +01003808 prep_temp_blob(name, temp, one->data, one->size,
brian m. carlson09bdff22016-06-24 23:09:29 +00003809 &one->oid, one->mode);
Junio C Hamano6973dca2006-04-21 23:57:45 -07003810 }
Jeff King479b0ae2009-01-22 00:59:56 -05003811 return temp;
Junio C Hamano6973dca2006-04-21 23:57:45 -07003812}
3813
Jeff Kingf3efe782014-04-19 15:22:25 -04003814static void add_external_diff_name(struct argv_array *argv,
3815 const char *name,
3816 struct diff_filespec *df)
3817{
3818 struct diff_tempfile *temp = prepare_temp_file(name, df);
3819 argv_array_push(argv, temp->name);
3820 argv_array_push(argv, temp->hex);
3821 argv_array_push(argv, temp->mode);
3822}
3823
Junio C Hamano6973dca2006-04-21 23:57:45 -07003824/* An external diff command takes:
3825 *
3826 * diff-cmd name infile1 infile1-sha1 infile1-mode \
3827 * infile2 infile2-sha1 infile2-mode [ rename-to ]
3828 *
3829 */
3830static void run_external_diff(const char *pgm,
3831 const char *name,
3832 const char *other,
3833 struct diff_filespec *one,
3834 struct diff_filespec *two,
3835 const char *xfrm_msg,
Zoltan Klingeree7fb0b2013-12-06 10:38:46 +11003836 int complete_rewrite,
3837 struct diff_options *o)
Junio C Hamano6973dca2006-04-21 23:57:45 -07003838{
Jeff King82fbf262014-04-19 15:17:06 -04003839 struct argv_array argv = ARGV_ARRAY_INIT;
Jeff Kingae049c92014-04-19 15:17:25 -04003840 struct argv_array env = ARGV_ARRAY_INIT;
Zoltan Klingeree7fb0b2013-12-06 10:38:46 +11003841 struct diff_queue_struct *q = &diff_queued_diff;
Junio C Hamano6973dca2006-04-21 23:57:45 -07003842
Jeff King0d4217d2014-04-19 15:20:46 -04003843 argv_array_push(&argv, pgm);
3844 argv_array_push(&argv, name);
Junio C Hamano6973dca2006-04-21 23:57:45 -07003845
Junio C Hamano6973dca2006-04-21 23:57:45 -07003846 if (one && two) {
Jeff Kingf3efe782014-04-19 15:22:25 -04003847 add_external_diff_name(&argv, name, one);
3848 if (!other)
3849 add_external_diff_name(&argv, name, two);
3850 else {
3851 add_external_diff_name(&argv, other, two);
Jeff King82fbf262014-04-19 15:17:06 -04003852 argv_array_push(&argv, other);
3853 argv_array_push(&argv, xfrm_msg);
Junio C Hamano6973dca2006-04-21 23:57:45 -07003854 }
Junio C Hamano6973dca2006-04-21 23:57:45 -07003855 }
Zoltan Klingeree7fb0b2013-12-06 10:38:46 +11003856
Jeff Kingae049c92014-04-19 15:17:25 -04003857 argv_array_pushf(&env, "GIT_DIFF_PATH_COUNTER=%d", ++o->diff_path_counter);
3858 argv_array_pushf(&env, "GIT_DIFF_PATH_TOTAL=%d", q->nr);
Zoltan Klingeree7fb0b2013-12-06 10:38:46 +11003859
Jeff King89294d12014-04-19 15:19:19 -04003860 if (run_command_v_opt_cd_env(argv.argv, RUN_USING_SHELL, NULL, env.argv))
3861 die(_("external diff died, stopping at %s"), name);
3862
Junio C Hamano6973dca2006-04-21 23:57:45 -07003863 remove_tempfile();
Jeff King82fbf262014-04-19 15:17:06 -04003864 argv_array_clear(&argv);
Jeff Kingae049c92014-04-19 15:17:25 -04003865 argv_array_clear(&env);
Junio C Hamano6973dca2006-04-21 23:57:45 -07003866}
3867
Junio C Hamanob67b9612009-01-26 00:08:24 -08003868static int similarity_index(struct diff_filepair *p)
3869{
3870 return p->score * 100 / MAX_SCORE;
3871}
3872
Jeff King4f036662016-10-20 02:21:25 -04003873static const char *diff_abbrev_oid(const struct object_id *oid, int abbrev)
3874{
3875 if (startup_info->have_repository)
3876 return find_unique_abbrev(oid->hash, abbrev);
3877 else {
3878 char *hex = oid_to_hex(oid);
Junio C Hamano0d9c5272016-10-27 14:58:48 -07003879 if (abbrev < 0)
3880 abbrev = FALLBACK_DEFAULT_ABBREV;
3881 if (abbrev > GIT_SHA1_HEXSZ)
Jeff King4f036662016-10-20 02:21:25 -04003882 die("BUG: oid abbreviation out of range: %d", abbrev);
Jack Bates43d19482016-12-06 09:56:14 -07003883 if (abbrev)
3884 hex[abbrev] = '\0';
Jeff King4f036662016-10-20 02:21:25 -04003885 return hex;
3886 }
3887}
3888
Junio C Hamanob67b9612009-01-26 00:08:24 -08003889static void fill_metainfo(struct strbuf *msg,
3890 const char *name,
3891 const char *other,
3892 struct diff_filespec *one,
3893 struct diff_filespec *two,
3894 struct diff_options *o,
Bert Wesarg37466442010-05-04 00:38:07 +02003895 struct diff_filepair *p,
Junio C Hamano59777442010-06-18 11:16:57 -07003896 int *must_show_header,
Bert Wesarg37466442010-05-04 00:38:07 +02003897 int use_color)
Junio C Hamanob67b9612009-01-26 00:08:24 -08003898{
Bert Wesarg37466442010-05-04 00:38:07 +02003899 const char *set = diff_get_color(use_color, DIFF_METAINFO);
3900 const char *reset = diff_get_color(use_color, DIFF_RESET);
John Keeping30997bb2013-02-07 20:15:27 +00003901 const char *line_prefix = diff_line_prefix(o);
Bo Yang7be57612010-05-26 15:23:54 +08003902
Christian Couder296c6bb2010-05-26 04:50:12 +02003903 *must_show_header = 1;
Junio C Hamanob67b9612009-01-26 00:08:24 -08003904 strbuf_init(msg, PATH_MAX * 2 + 300);
3905 switch (p->status) {
3906 case DIFF_STATUS_COPIED:
Junio C Hamano98ad90f2010-06-18 11:16:57 -07003907 strbuf_addf(msg, "%s%ssimilarity index %d%%",
3908 line_prefix, set, similarity_index(p));
3909 strbuf_addf(msg, "%s\n%s%scopy from ",
3910 reset, line_prefix, set);
Junio C Hamanob67b9612009-01-26 00:08:24 -08003911 quote_c_style(name, msg, NULL, 0);
Junio C Hamano98ad90f2010-06-18 11:16:57 -07003912 strbuf_addf(msg, "%s\n%s%scopy to ", reset, line_prefix, set);
Junio C Hamanob67b9612009-01-26 00:08:24 -08003913 quote_c_style(other, msg, NULL, 0);
Bert Wesarg37466442010-05-04 00:38:07 +02003914 strbuf_addf(msg, "%s\n", reset);
Junio C Hamanob67b9612009-01-26 00:08:24 -08003915 break;
3916 case DIFF_STATUS_RENAMED:
Junio C Hamano98ad90f2010-06-18 11:16:57 -07003917 strbuf_addf(msg, "%s%ssimilarity index %d%%",
3918 line_prefix, set, similarity_index(p));
3919 strbuf_addf(msg, "%s\n%s%srename from ",
3920 reset, line_prefix, set);
Junio C Hamanob67b9612009-01-26 00:08:24 -08003921 quote_c_style(name, msg, NULL, 0);
Junio C Hamano98ad90f2010-06-18 11:16:57 -07003922 strbuf_addf(msg, "%s\n%s%srename to ",
3923 reset, line_prefix, set);
Junio C Hamanob67b9612009-01-26 00:08:24 -08003924 quote_c_style(other, msg, NULL, 0);
Bert Wesarg37466442010-05-04 00:38:07 +02003925 strbuf_addf(msg, "%s\n", reset);
Junio C Hamanob67b9612009-01-26 00:08:24 -08003926 break;
3927 case DIFF_STATUS_MODIFIED:
3928 if (p->score) {
Junio C Hamano98ad90f2010-06-18 11:16:57 -07003929 strbuf_addf(msg, "%s%sdissimilarity index %d%%%s\n",
3930 line_prefix,
Bert Wesarg37466442010-05-04 00:38:07 +02003931 set, similarity_index(p), reset);
Junio C Hamanob67b9612009-01-26 00:08:24 -08003932 break;
3933 }
3934 /* fallthru */
3935 default:
Christian Couder296c6bb2010-05-26 04:50:12 +02003936 *must_show_header = 0;
Junio C Hamanob67b9612009-01-26 00:08:24 -08003937 }
brian m. carlsona0d12c42016-06-24 23:09:23 +00003938 if (one && two && oidcmp(&one->oid, &two->oid)) {
Junio C Hamanob67b9612009-01-26 00:08:24 -08003939 int abbrev = DIFF_OPT_TST(o, FULL_INDEX) ? 40 : DEFAULT_ABBREV;
3940
3941 if (DIFF_OPT_TST(o, BINARY)) {
3942 mmfile_t mf;
3943 if ((!fill_mmfile(&mf, one) && diff_filespec_is_binary(one)) ||
3944 (!fill_mmfile(&mf, two) && diff_filespec_is_binary(two)))
3945 abbrev = 40;
3946 }
Jeff King4f036662016-10-20 02:21:25 -04003947 strbuf_addf(msg, "%s%sindex %s..%s", line_prefix, set,
3948 diff_abbrev_oid(&one->oid, abbrev),
3949 diff_abbrev_oid(&two->oid, abbrev));
Junio C Hamanob67b9612009-01-26 00:08:24 -08003950 if (one->mode == two->mode)
3951 strbuf_addf(msg, " %06o", one->mode);
Bert Wesarg37466442010-05-04 00:38:07 +02003952 strbuf_addf(msg, "%s\n", reset);
Junio C Hamanob67b9612009-01-26 00:08:24 -08003953 }
Junio C Hamanob67b9612009-01-26 00:08:24 -08003954}
3955
Junio C Hamano6973dca2006-04-21 23:57:45 -07003956static void run_diff_cmd(const char *pgm,
3957 const char *name,
3958 const char *other,
Junio C Hamanocd676a52008-02-12 14:26:02 -08003959 const char *attr_path,
Junio C Hamano6973dca2006-04-21 23:57:45 -07003960 struct diff_filespec *one,
3961 struct diff_filespec *two,
Junio C Hamanob67b9612009-01-26 00:08:24 -08003962 struct strbuf *msg,
Junio C Hamano051308f2006-05-04 16:51:44 -07003963 struct diff_options *o,
Junio C Hamanob67b9612009-01-26 00:08:24 -08003964 struct diff_filepair *p)
Junio C Hamano6973dca2006-04-21 23:57:45 -07003965{
Junio C Hamanob67b9612009-01-26 00:08:24 -08003966 const char *xfrm_msg = NULL;
3967 int complete_rewrite = (p->status == DIFF_STATUS_MODIFIED) && p->score;
Christian Couder296c6bb2010-05-26 04:50:12 +02003968 int must_show_header = 0;
Junio C Hamanob67b9612009-01-26 00:08:24 -08003969
Junio C Hamanobd8c1a92012-07-17 22:08:59 -07003970
3971 if (DIFF_OPT_TST(o, ALLOW_EXTERNAL)) {
Jeff Kingbe58e702008-10-05 17:43:21 -04003972 struct userdiff_driver *drv = userdiff_find_by_path(attr_path);
3973 if (drv && drv->external)
3974 pgm = drv->external;
Junio C Hamanof1af60b2007-04-22 17:52:55 -07003975 }
3976
Bert Wesarg37466442010-05-04 00:38:07 +02003977 if (msg) {
3978 /*
3979 * don't use colors when the header is intended for an
3980 * external diff driver
3981 */
3982 fill_metainfo(msg, name, other, one, two, o, p,
Junio C Hamano59777442010-06-18 11:16:57 -07003983 &must_show_header,
Jeff Kingdaa0c3d2011-08-17 22:04:23 -07003984 want_color(o->use_color) && !pgm);
Bert Wesarg37466442010-05-04 00:38:07 +02003985 xfrm_msg = msg->len ? msg->buf : NULL;
3986 }
3987
Junio C Hamano6973dca2006-04-21 23:57:45 -07003988 if (pgm) {
3989 run_external_diff(pgm, name, other, one, two, xfrm_msg,
Zoltan Klingeree7fb0b2013-12-06 10:38:46 +11003990 complete_rewrite, o);
Junio C Hamano6973dca2006-04-21 23:57:45 -07003991 return;
3992 }
3993 if (one && two)
3994 builtin_diff(name, other ? other : name,
Christian Couder296c6bb2010-05-26 04:50:12 +02003995 one, two, xfrm_msg, must_show_header,
3996 o, complete_rewrite);
Junio C Hamano6973dca2006-04-21 23:57:45 -07003997 else
Daniel Barkalowc0c77732008-03-09 22:43:39 -04003998 fprintf(o->file, "* Unmerged path %s\n", name);
Junio C Hamano6973dca2006-04-21 23:57:45 -07003999}
4000
Brandon Williams94e327e2017-05-30 10:31:09 -07004001static void diff_fill_oid_info(struct diff_filespec *one)
Junio C Hamano6973dca2006-04-21 23:57:45 -07004002{
4003 if (DIFF_FILE_VALID(one)) {
brian m. carlson41c95602016-06-24 23:09:24 +00004004 if (!one->oid_valid) {
Junio C Hamano6973dca2006-04-21 23:57:45 -07004005 struct stat st;
Junio C Hamano4682d852012-06-27 20:14:47 -07004006 if (one->is_stdin) {
brian m. carlsona0d12c42016-06-24 23:09:23 +00004007 oidclr(&one->oid);
Johannes Schindelin5332b2a2007-02-25 23:36:10 +01004008 return;
4009 }
Junio C Hamano6973dca2006-04-21 23:57:45 -07004010 if (lstat(one->path, &st) < 0)
Thomas Rast0721c312009-06-27 17:58:47 +02004011 die_errno("stat '%s'", one->path);
Patryk Obara98e019b2017-08-20 22:09:28 +02004012 if (index_path(&one->oid, one->path, &st, 0))
Alexander Potashevd7530702009-01-04 21:38:41 +03004013 die("cannot hash %s", one->path);
Junio C Hamano6973dca2006-04-21 23:57:45 -07004014 }
4015 }
4016 else
brian m. carlsona0d12c42016-06-24 23:09:23 +00004017 oidclr(&one->oid);
Junio C Hamano6973dca2006-04-21 23:57:45 -07004018}
4019
Junio C Hamanocd676a52008-02-12 14:26:02 -08004020static void strip_prefix(int prefix_length, const char **namep, const char **otherp)
4021{
4022 /* Strip the prefix but do not molest /dev/null and absolute paths */
Jakub Narebskid8faea92010-08-09 10:50:53 -04004023 if (*namep && **namep != '/') {
Junio C Hamanocd676a52008-02-12 14:26:02 -08004024 *namep += prefix_length;
Jakub Narebskid8faea92010-08-09 10:50:53 -04004025 if (**namep == '/')
4026 ++*namep;
4027 }
4028 if (*otherp && **otherp != '/') {
Junio C Hamanocd676a52008-02-12 14:26:02 -08004029 *otherp += prefix_length;
Jakub Narebskid8faea92010-08-09 10:50:53 -04004030 if (**otherp == '/')
4031 ++*otherp;
4032 }
Junio C Hamanocd676a52008-02-12 14:26:02 -08004033}
4034
Junio C Hamano6973dca2006-04-21 23:57:45 -07004035static void run_diff(struct diff_filepair *p, struct diff_options *o)
4036{
4037 const char *pgm = external_diff();
Pierre Habouzit663af342007-09-20 00:42:15 +02004038 struct strbuf msg;
Pierre Habouzit663af342007-09-20 00:42:15 +02004039 struct diff_filespec *one = p->one;
4040 struct diff_filespec *two = p->two;
Junio C Hamano6973dca2006-04-21 23:57:45 -07004041 const char *name;
4042 const char *other;
Junio C Hamanocd676a52008-02-12 14:26:02 -08004043 const char *attr_path;
Pierre Habouzit663af342007-09-20 00:42:15 +02004044
Stefan Bellerf2d2a5d2017-06-29 17:06:46 -07004045 name = one->path;
4046 other = (strcmp(name, two->path) ? two->path : NULL);
Junio C Hamanocd676a52008-02-12 14:26:02 -08004047 attr_path = name;
4048 if (o->prefix_length)
4049 strip_prefix(o->prefix_length, &name, &other);
4050
Junio C Hamanobd8c1a92012-07-17 22:08:59 -07004051 if (!DIFF_OPT_TST(o, ALLOW_EXTERNAL))
4052 pgm = NULL;
4053
Junio C Hamanocd676a52008-02-12 14:26:02 -08004054 if (DIFF_PAIR_UNMERGED(p)) {
4055 run_diff_cmd(pgm, name, NULL, attr_path,
Junio C Hamanob67b9612009-01-26 00:08:24 -08004056 NULL, NULL, NULL, o, p);
Junio C Hamanocd676a52008-02-12 14:26:02 -08004057 return;
4058 }
4059
Brandon Williams94e327e2017-05-30 10:31:09 -07004060 diff_fill_oid_info(one);
4061 diff_fill_oid_info(two);
Junio C Hamano6973dca2006-04-21 23:57:45 -07004062
Junio C Hamano6973dca2006-04-21 23:57:45 -07004063 if (!pgm &&
4064 DIFF_FILE_VALID(one) && DIFF_FILE_VALID(two) &&
4065 (S_IFMT & one->mode) != (S_IFMT & two->mode)) {
Junio C Hamanob67b9612009-01-26 00:08:24 -08004066 /*
4067 * a filepair that changes between file and symlink
Junio C Hamano6973dca2006-04-21 23:57:45 -07004068 * needs to be split into deletion and creation.
4069 */
4070 struct diff_filespec *null = alloc_filespec(two->path);
Junio C Hamanocd676a52008-02-12 14:26:02 -08004071 run_diff_cmd(NULL, name, other, attr_path,
Junio C Hamanob67b9612009-01-26 00:08:24 -08004072 one, null, &msg, o, p);
Junio C Hamano6973dca2006-04-21 23:57:45 -07004073 free(null);
Junio C Hamanob67b9612009-01-26 00:08:24 -08004074 strbuf_release(&msg);
4075
Junio C Hamano6973dca2006-04-21 23:57:45 -07004076 null = alloc_filespec(one->path);
Junio C Hamanocd676a52008-02-12 14:26:02 -08004077 run_diff_cmd(NULL, name, other, attr_path,
Junio C Hamanob67b9612009-01-26 00:08:24 -08004078 null, two, &msg, o, p);
Junio C Hamano6973dca2006-04-21 23:57:45 -07004079 free(null);
4080 }
4081 else
Junio C Hamanocd676a52008-02-12 14:26:02 -08004082 run_diff_cmd(pgm, name, other, attr_path,
Junio C Hamanob67b9612009-01-26 00:08:24 -08004083 one, two, &msg, o, p);
Junio C Hamano6973dca2006-04-21 23:57:45 -07004084
Pierre Habouzit663af342007-09-20 00:42:15 +02004085 strbuf_release(&msg);
Junio C Hamano6973dca2006-04-21 23:57:45 -07004086}
4087
4088static void run_diffstat(struct diff_filepair *p, struct diff_options *o,
4089 struct diffstat_t *diffstat)
4090{
4091 const char *name;
4092 const char *other;
4093
4094 if (DIFF_PAIR_UNMERGED(p)) {
4095 /* unmerged */
Linus Torvalds74faaa12012-10-17 10:00:37 -07004096 builtin_diffstat(p->one->path, NULL, NULL, NULL, diffstat, o, p);
Junio C Hamano6973dca2006-04-21 23:57:45 -07004097 return;
4098 }
4099
4100 name = p->one->path;
4101 other = (strcmp(name, p->two->path) ? p->two->path : NULL);
4102
Junio C Hamanocd676a52008-02-12 14:26:02 -08004103 if (o->prefix_length)
4104 strip_prefix(o->prefix_length, &name, &other);
4105
Brandon Williams94e327e2017-05-30 10:31:09 -07004106 diff_fill_oid_info(p->one);
4107 diff_fill_oid_info(p->two);
Junio C Hamano6973dca2006-04-21 23:57:45 -07004108
Linus Torvalds74faaa12012-10-17 10:00:37 -07004109 builtin_diffstat(name, other, p->one, p->two, diffstat, o, p);
Junio C Hamano6973dca2006-04-21 23:57:45 -07004110}
4111
Johannes Schindelin88246892006-05-20 23:43:13 +02004112static void run_checkdiff(struct diff_filepair *p, struct diff_options *o)
4113{
4114 const char *name;
4115 const char *other;
Junio C Hamanocd676a52008-02-12 14:26:02 -08004116 const char *attr_path;
Johannes Schindelin88246892006-05-20 23:43:13 +02004117
4118 if (DIFF_PAIR_UNMERGED(p)) {
4119 /* unmerged */
4120 return;
4121 }
4122
4123 name = p->one->path;
4124 other = (strcmp(name, p->two->path) ? p->two->path : NULL);
Junio C Hamanocd676a52008-02-12 14:26:02 -08004125 attr_path = other ? other : name;
4126
4127 if (o->prefix_length)
4128 strip_prefix(o->prefix_length, &name, &other);
Johannes Schindelin88246892006-05-20 23:43:13 +02004129
Brandon Williams94e327e2017-05-30 10:31:09 -07004130 diff_fill_oid_info(p->one);
4131 diff_fill_oid_info(p->two);
Johannes Schindelin88246892006-05-20 23:43:13 +02004132
Junio C Hamanocd676a52008-02-12 14:26:02 -08004133 builtin_checkdiff(name, other, attr_path, p->one, p->two, o);
Johannes Schindelin88246892006-05-20 23:43:13 +02004134}
4135
Junio C Hamano6973dca2006-04-21 23:57:45 -07004136void diff_setup(struct diff_options *options)
4137{
Johannes Schindelinbe4f2b42010-08-05 10:49:55 +02004138 memcpy(options, &default_diff_options, sizeof(*options));
Daniel Barkalowc0c77732008-03-09 22:43:39 -04004139
4140 options->file = stdout;
4141
Jack Bates43d19482016-12-06 09:56:14 -07004142 options->abbrev = DEFAULT_ABBREV;
Junio C Hamano6973dca2006-04-21 23:57:45 -07004143 options->line_termination = '\n';
4144 options->break_opt = -1;
4145 options->rename_limit = -1;
Johan Herland712d2c72011-04-29 11:36:20 +02004146 options->dirstat_permille = diff_dirstat_permille_default;
Jeff Muizelaar6468a4e2012-09-27 15:12:52 -04004147 options->context = diff_context_default;
Vegard Nossumc4888672017-01-12 13:21:11 +01004148 options->interhunkcontext = diff_interhunk_context_default;
Junio C Hamanoa17505f2016-10-04 15:26:27 -07004149 options->ws_error_highlight = ws_error_highlight_default;
Jeff King90d43b02012-03-22 18:52:13 -04004150 DIFF_OPT_SET(options, RENAME_EMPTY);
Junio C Hamano6973dca2006-04-21 23:57:45 -07004151
Kirill Smelkov72441af2014-04-07 01:46:26 +04004152 /* pathchange left =NULL by default */
Junio C Hamano6973dca2006-04-21 23:57:45 -07004153 options->change = diff_change;
4154 options->add_remove = diff_addremove;
Jeff Kingf1c96262011-08-17 22:03:12 -07004155 options->use_color = diff_use_color_default;
Eric Wongb68ea122006-07-07 04:01:23 -07004156 options->detect_rename = diff_detect_rename_default;
Michal Privoznik07ab4de2013-01-16 08:51:57 +01004157 options->xdl_opts |= diff_algorithm;
Michael Haggerty433860f2016-09-05 11:44:51 +02004158 if (diff_indent_heuristic)
4159 DIFF_XDL_SET(options, INDENT_HEURISTIC);
Johannes Schindelineab9a402007-12-18 19:32:14 +00004160
Samuel Bronson6d8940b2013-12-18 19:08:12 -05004161 options->orderfile = diff_order_file_cfg;
4162
Eli Collinsf89504d2010-05-02 19:03:41 -07004163 if (diff_no_prefix) {
4164 options->a_prefix = options->b_prefix = "";
4165 } else if (!diff_mnemonic_prefix) {
Junio C Hamanoa5a818e2008-08-18 20:08:09 -07004166 options->a_prefix = "a/";
4167 options->b_prefix = "b/";
4168 }
Stefan Beller2e2d5ac2017-06-30 13:53:07 -07004169
4170 options->color_moved = diff_color_moved_default;
Junio C Hamano6973dca2006-04-21 23:57:45 -07004171}
4172
Thomas Rast28452652012-08-03 14:16:24 +02004173void diff_setup_done(struct diff_options *options)
Junio C Hamano6973dca2006-04-21 23:57:45 -07004174{
Timo Hirvonend7de00f2006-06-24 20:26:49 +03004175 int count = 0;
4176
Junio C Hamano6c374002013-05-10 17:10:11 +02004177 if (options->set_default)
4178 options->set_default(options);
4179
Timo Hirvonend7de00f2006-06-24 20:26:49 +03004180 if (options->output_format & DIFF_FORMAT_NAME)
4181 count++;
4182 if (options->output_format & DIFF_FORMAT_NAME_STATUS)
4183 count++;
4184 if (options->output_format & DIFF_FORMAT_CHECKDIFF)
4185 count++;
4186 if (options->output_format & DIFF_FORMAT_NO_OUTPUT)
4187 count++;
4188 if (count > 1)
Jean-Noël AVILAa2f05c92016-09-20 21:04:27 +02004189 die(_("--name-only, --name-status, --check and -s are mutually exclusive"));
Timo Hirvonend7de00f2006-06-24 20:26:49 +03004190
Junio C Hamanof2451942009-05-22 12:45:29 -07004191 /*
4192 * Most of the time we can say "there are changes"
4193 * only by checking if there are changed paths, but
4194 * --ignore-whitespace* options force us to look
Jim Meyering97bf2a02009-08-30 22:27:02 +02004195 * inside contents.
Junio C Hamanof2451942009-05-22 12:45:29 -07004196 */
4197
4198 if (DIFF_XDL_TST(options, IGNORE_WHITESPACE) ||
4199 DIFF_XDL_TST(options, IGNORE_WHITESPACE_CHANGE) ||
4200 DIFF_XDL_TST(options, IGNORE_WHITESPACE_AT_EOL))
4201 DIFF_OPT_SET(options, DIFF_FROM_CONTENTS);
4202 else
4203 DIFF_OPT_CLR(options, DIFF_FROM_CONTENTS);
4204
Pierre Habouzit8f67f8a2007-11-10 20:05:14 +01004205 if (DIFF_OPT_TST(options, FIND_COPIES_HARDER))
Junio C Hamano03b9d562006-08-09 13:17:19 -07004206 options->detect_rename = DIFF_DETECT_COPY;
4207
Junio C Hamanocd676a52008-02-12 14:26:02 -08004208 if (!DIFF_OPT_TST(options, RELATIVE_NAME))
4209 options->prefix = NULL;
4210 if (options->prefix)
4211 options->prefix_length = strlen(options->prefix);
4212 else
4213 options->prefix_length = 0;
4214
Timo Hirvonenc6744342006-06-24 20:21:53 +03004215 if (options->output_format & (DIFF_FORMAT_NAME |
4216 DIFF_FORMAT_NAME_STATUS |
4217 DIFF_FORMAT_CHECKDIFF |
4218 DIFF_FORMAT_NO_OUTPUT))
4219 options->output_format &= ~(DIFF_FORMAT_RAW |
Junio C Hamano74e2abe2006-10-12 03:01:00 -07004220 DIFF_FORMAT_NUMSTAT |
Timo Hirvonenc6744342006-06-24 20:21:53 +03004221 DIFF_FORMAT_DIFFSTAT |
Nicolas Pitreebd124c2006-12-14 23:15:44 -05004222 DIFF_FORMAT_SHORTSTAT |
Linus Torvalds7df7c012008-02-12 13:26:31 -08004223 DIFF_FORMAT_DIRSTAT |
Timo Hirvonenc6744342006-06-24 20:21:53 +03004224 DIFF_FORMAT_SUMMARY |
4225 DIFF_FORMAT_PATCH);
4226
Junio C Hamano6973dca2006-04-21 23:57:45 -07004227 /*
4228 * These cases always need recursive; we do not drop caller-supplied
4229 * recursive bits for other formats here.
4230 */
Timo Hirvonenc6744342006-06-24 20:21:53 +03004231 if (options->output_format & (DIFF_FORMAT_PATCH |
Junio C Hamano74e2abe2006-10-12 03:01:00 -07004232 DIFF_FORMAT_NUMSTAT |
Timo Hirvonenc6744342006-06-24 20:21:53 +03004233 DIFF_FORMAT_DIFFSTAT |
Nicolas Pitreebd124c2006-12-14 23:15:44 -05004234 DIFF_FORMAT_SHORTSTAT |
Linus Torvalds7df7c012008-02-12 13:26:31 -08004235 DIFF_FORMAT_DIRSTAT |
Johannes Schindelind7014dc2006-10-03 23:09:56 +02004236 DIFF_FORMAT_SUMMARY |
Timo Hirvonenc6744342006-06-24 20:21:53 +03004237 DIFF_FORMAT_CHECKDIFF))
Pierre Habouzit8f67f8a2007-11-10 20:05:14 +01004238 DIFF_OPT_SET(options, RECURSIVE);
Junio C Hamano5e363542006-05-22 00:31:02 -07004239 /*
Junio C Hamano3969cf72006-06-27 15:08:19 -07004240 * Also pickaxe would not work very well if you do not say recursive
Junio C Hamano5e363542006-05-22 00:31:02 -07004241 */
Junio C Hamano3969cf72006-06-27 15:08:19 -07004242 if (options->pickaxe)
Pierre Habouzit8f67f8a2007-11-10 20:05:14 +01004243 DIFF_OPT_SET(options, RECURSIVE);
Jens Lehmannae6d5c12010-03-11 22:50:25 +01004244 /*
4245 * When patches are generated, submodules diffed against the work tree
4246 * must be checked for dirtiness too so it can be shown in the output
4247 */
4248 if (options->output_format & DIFF_FORMAT_PATCH)
4249 DIFF_OPT_SET(options, DIRTY_SUBMODULES);
Junio C Hamano5e363542006-05-22 00:31:02 -07004250
Junio C Hamano6973dca2006-04-21 23:57:45 -07004251 if (options->detect_rename && options->rename_limit < 0)
4252 options->rename_limit = diff_rename_limit_default;
4253 if (options->setup & DIFF_SETUP_USE_CACHE) {
4254 if (!active_cache)
4255 /* read-cache does not die even when it fails
4256 * so it is safe for us to do this here. Also
4257 * it does not smudge active_cache or active_nr
4258 * when it fails, so we do not have to worry about
4259 * cleaning it up ourselves either.
4260 */
4261 read_cache();
4262 }
Junio C Hamano7b5b7722016-09-30 17:19:36 -07004263 if (40 < options->abbrev)
Junio C Hamano6973dca2006-04-21 23:57:45 -07004264 options->abbrev = 40; /* full */
4265
Junio C Hamano68aacb22007-03-14 11:12:13 -07004266 /*
4267 * It does not make sense to show the first hit we happened
4268 * to have found. It does not make sense not to return with
4269 * exit code in such a case either.
4270 */
Junio C Hamano90b19942009-05-23 01:15:35 -07004271 if (DIFF_OPT_TST(options, QUICK)) {
Junio C Hamano68aacb22007-03-14 11:12:13 -07004272 options->output_format = DIFF_FORMAT_NO_OUTPUT;
Pierre Habouzit8f67f8a2007-11-10 20:05:14 +01004273 DIFF_OPT_SET(options, EXIT_WITH_STATUS);
Junio C Hamano68aacb22007-03-14 11:12:13 -07004274 }
Zoltan Klingeree7fb0b2013-12-06 10:38:46 +11004275
Jeff King0ea7d5b2013-12-16 15:02:21 -05004276 options->diff_path_counter = 0;
Junio C Hamanob0e2c992014-06-16 10:07:09 -07004277
Jeff Kingdd63f162014-05-20 02:49:20 -04004278 if (DIFF_OPT_TST(options, FOLLOW_RENAMES) && options->pathspec.nr != 1)
4279 die(_("--follow requires exactly one pathspec"));
Stefan Beller2e2d5ac2017-06-30 13:53:07 -07004280
4281 if (!options->use_color || external_diff())
4282 options->color_moved = 0;
Junio C Hamano6973dca2006-04-21 23:57:45 -07004283}
4284
Timo Hirvonend2543b82006-06-24 20:20:32 +03004285static int opt_arg(const char *arg, int arg_short, const char *arg_long, int *val)
Linus Torvaldsee1e5412006-05-13 13:23:48 -07004286{
4287 char c, *eq;
4288 int len;
4289
4290 if (*arg != '-')
4291 return 0;
4292 c = *++arg;
4293 if (!c)
4294 return 0;
4295 if (c == arg_short) {
4296 c = *++arg;
4297 if (!c)
4298 return 1;
4299 if (val && isdigit(c)) {
4300 char *end;
4301 int n = strtoul(arg, &end, 10);
4302 if (*end)
4303 return 0;
4304 *val = n;
4305 return 1;
4306 }
4307 return 0;
4308 }
4309 if (c != '-')
4310 return 0;
4311 arg++;
Rohit Mani2c5495f2014-03-07 22:48:31 -08004312 eq = strchrnul(arg, '=');
4313 len = eq - arg;
Linus Torvaldsee1e5412006-05-13 13:23:48 -07004314 if (!len || strncmp(arg, arg_long, len))
4315 return 0;
Rohit Mani2c5495f2014-03-07 22:48:31 -08004316 if (*eq) {
Linus Torvaldsee1e5412006-05-13 13:23:48 -07004317 int n;
4318 char *end;
4319 if (!isdigit(*++eq))
4320 return 0;
4321 n = strtoul(eq, &end, 10);
4322 if (*end)
4323 return 0;
4324 *val = n;
4325 }
4326 return 1;
4327}
4328
Junio C Hamano16befb82007-06-08 02:54:57 -07004329static int diff_scoreopt_parse(const char *opt);
4330
Matthieu Moydea007f2010-08-05 10:22:52 +02004331static inline int short_opt(char opt, const char **argv,
4332 const char **optarg)
4333{
4334 const char *arg = argv[0];
4335 if (arg[0] != '-' || arg[1] != opt)
4336 return 0;
4337 if (arg[2] != '\0') {
4338 *optarg = arg + 2;
4339 return 1;
4340 }
4341 if (!argv[1])
4342 die("Option '%c' requires a value", opt);
4343 *optarg = argv[1];
4344 return 2;
4345}
4346
4347int parse_long_opt(const char *opt, const char **argv,
4348 const char **optarg)
4349{
4350 const char *arg = argv[0];
Jeff King95b567c2014-06-18 15:48:29 -04004351 if (!skip_prefix(arg, "--", &arg))
Matthieu Moydea007f2010-08-05 10:22:52 +02004352 return 0;
Jeff King95b567c2014-06-18 15:48:29 -04004353 if (!skip_prefix(arg, opt, &arg))
Matthieu Moydea007f2010-08-05 10:22:52 +02004354 return 0;
Nicolas Vigierb0d12fc2013-10-31 12:08:28 +01004355 if (*arg == '=') { /* stuck form: --option=value */
Matthieu Moydea007f2010-08-05 10:22:52 +02004356 *optarg = arg + 1;
4357 return 1;
4358 }
4359 if (*arg != '\0')
4360 return 0;
4361 /* separate form: --option value */
4362 if (!argv[1])
4363 die("Option '--%s' requires a value", opt);
4364 *optarg = argv[1];
4365 return 2;
4366}
4367
Jonathan Nieder4d7f7a42010-08-05 10:22:53 +02004368static int stat_opt(struct diff_options *options, const char **av)
4369{
4370 const char *arg = av[0];
4371 char *end;
4372 int width = options->stat_width;
4373 int name_width = options->stat_name_width;
Zbigniew Jędrzejewski-Szmek969fe572012-03-01 13:26:45 +01004374 int graph_width = options->stat_graph_width;
Michael J Gruber808e1db2011-05-27 14:36:41 +02004375 int count = options->stat_count;
Matthieu Moy1e572082010-08-05 10:22:54 +02004376 int argcount = 1;
Jonathan Nieder4d7f7a42010-08-05 10:22:53 +02004377
Jeff King0539cc02014-06-18 15:51:17 -04004378 if (!skip_prefix(arg, "--stat", &arg))
4379 die("BUG: stat option does not begin with --stat: %s", arg);
Jonathan Nieder4d7f7a42010-08-05 10:22:53 +02004380 end = (char *)arg;
4381
4382 switch (*arg) {
4383 case '-':
Jeff King95b567c2014-06-18 15:48:29 -04004384 if (skip_prefix(arg, "-width", &arg)) {
Matthieu Moy1e572082010-08-05 10:22:54 +02004385 if (*arg == '=')
4386 width = strtoul(arg + 1, &end, 10);
4387 else if (!*arg && !av[1])
Jean-Noël AVILAa2f05c92016-09-20 21:04:27 +02004388 die_want_option("--stat-width");
Matthieu Moy1e572082010-08-05 10:22:54 +02004389 else if (!*arg) {
4390 width = strtoul(av[1], &end, 10);
4391 argcount = 2;
4392 }
Jeff King95b567c2014-06-18 15:48:29 -04004393 } else if (skip_prefix(arg, "-name-width", &arg)) {
Matthieu Moy1e572082010-08-05 10:22:54 +02004394 if (*arg == '=')
4395 name_width = strtoul(arg + 1, &end, 10);
4396 else if (!*arg && !av[1])
Jean-Noël AVILAa2f05c92016-09-20 21:04:27 +02004397 die_want_option("--stat-name-width");
Matthieu Moy1e572082010-08-05 10:22:54 +02004398 else if (!*arg) {
4399 name_width = strtoul(av[1], &end, 10);
4400 argcount = 2;
4401 }
Jeff King95b567c2014-06-18 15:48:29 -04004402 } else if (skip_prefix(arg, "-graph-width", &arg)) {
Zbigniew Jędrzejewski-Szmek969fe572012-03-01 13:26:45 +01004403 if (*arg == '=')
4404 graph_width = strtoul(arg + 1, &end, 10);
4405 else if (!*arg && !av[1])
Jean-Noël AVILAa2f05c92016-09-20 21:04:27 +02004406 die_want_option("--stat-graph-width");
Zbigniew Jędrzejewski-Szmek969fe572012-03-01 13:26:45 +01004407 else if (!*arg) {
4408 graph_width = strtoul(av[1], &end, 10);
4409 argcount = 2;
4410 }
Jeff King95b567c2014-06-18 15:48:29 -04004411 } else if (skip_prefix(arg, "-count", &arg)) {
Michael J Gruber808e1db2011-05-27 14:36:41 +02004412 if (*arg == '=')
4413 count = strtoul(arg + 1, &end, 10);
4414 else if (!*arg && !av[1])
Jean-Noël AVILAa2f05c92016-09-20 21:04:27 +02004415 die_want_option("--stat-count");
Michael J Gruber808e1db2011-05-27 14:36:41 +02004416 else if (!*arg) {
4417 count = strtoul(av[1], &end, 10);
4418 argcount = 2;
4419 }
Matthieu Moy1e572082010-08-05 10:22:54 +02004420 }
Jonathan Nieder4d7f7a42010-08-05 10:22:53 +02004421 break;
4422 case '=':
4423 width = strtoul(arg+1, &end, 10);
4424 if (*end == ',')
4425 name_width = strtoul(end+1, &end, 10);
Michael J Gruber808e1db2011-05-27 14:36:41 +02004426 if (*end == ',')
4427 count = strtoul(end+1, &end, 10);
Jonathan Nieder4d7f7a42010-08-05 10:22:53 +02004428 }
4429
4430 /* Important! This checks all the error cases! */
4431 if (*end)
4432 return 0;
4433 options->output_format |= DIFF_FORMAT_DIFFSTAT;
4434 options->stat_name_width = name_width;
Zbigniew Jędrzejewski-Szmek969fe572012-03-01 13:26:45 +01004435 options->stat_graph_width = graph_width;
Jonathan Nieder4d7f7a42010-08-05 10:22:53 +02004436 options->stat_width = width;
Michael J Gruber808e1db2011-05-27 14:36:41 +02004437 options->stat_count = count;
Matthieu Moy1e572082010-08-05 10:22:54 +02004438 return argcount;
Jonathan Nieder4d7f7a42010-08-05 10:22:53 +02004439}
4440
Johan Herland333f3fb2011-04-29 11:36:18 +02004441static int parse_dirstat_opt(struct diff_options *options, const char *params)
4442{
Johan Herland51670fc2011-04-29 11:36:22 +02004443 struct strbuf errmsg = STRBUF_INIT;
4444 if (parse_dirstat_params(options, params, &errmsg))
Johan Herland7478ac52011-04-29 11:36:23 +02004445 die(_("Failed to parse --dirstat/-X option parameter:\n%s"),
Johan Herland51670fc2011-04-29 11:36:22 +02004446 errmsg.buf);
4447 strbuf_release(&errmsg);
Johan Herland333f3fb2011-04-29 11:36:18 +02004448 /*
4449 * The caller knows a dirstat-related option is given from the command
4450 * line; allow it to say "return this_function();"
4451 */
4452 options->output_format |= DIFF_FORMAT_DIRSTAT;
4453 return 1;
4454}
4455
Ramkumar Ramachandrac47ef572012-11-13 21:12:45 +05304456static int parse_submodule_opt(struct diff_options *options, const char *value)
4457{
4458 if (parse_submodule_params(options, value))
4459 die(_("Failed to parse --submodule option parameter: '%s'"),
4460 value);
4461 return 1;
4462}
4463
Junio C Hamano1ecc1cb2013-07-17 15:05:46 -07004464static const char diff_status_letters[] = {
4465 DIFF_STATUS_ADDED,
4466 DIFF_STATUS_COPIED,
4467 DIFF_STATUS_DELETED,
4468 DIFF_STATUS_MODIFIED,
4469 DIFF_STATUS_RENAMED,
4470 DIFF_STATUS_TYPE_CHANGED,
4471 DIFF_STATUS_UNKNOWN,
4472 DIFF_STATUS_UNMERGED,
4473 DIFF_STATUS_FILTER_AON,
4474 DIFF_STATUS_FILTER_BROKEN,
4475 '\0',
4476};
4477
4478static unsigned int filter_bit['Z' + 1];
4479
4480static void prepare_filter_bits(void)
4481{
4482 int i;
4483
4484 if (!filter_bit[DIFF_STATUS_ADDED]) {
4485 for (i = 0; diff_status_letters[i]; i++)
4486 filter_bit[(int) diff_status_letters[i]] = (1 << i);
4487 }
4488}
4489
4490static unsigned filter_bit_tst(char status, const struct diff_options *opt)
4491{
4492 return opt->filter & filter_bit[(int) status];
4493}
4494
4495static int parse_diff_filter_opt(const char *optarg, struct diff_options *opt)
4496{
4497 int i, optch;
4498
4499 prepare_filter_bits();
Junio C Hamano7f2ea5f2013-07-17 16:19:19 -07004500
4501 /*
4502 * If there is a negation e.g. 'd' in the input, and we haven't
4503 * initialized the filter field with another --diff-filter, start
4504 * from full set of bits, except for AON.
4505 */
4506 if (!opt->filter) {
4507 for (i = 0; (optch = optarg[i]) != '\0'; i++) {
4508 if (optch < 'a' || 'z' < optch)
4509 continue;
4510 opt->filter = (1 << (ARRAY_SIZE(diff_status_letters) - 1)) - 1;
4511 opt->filter &= ~filter_bit[DIFF_STATUS_FILTER_AON];
4512 break;
4513 }
4514 }
4515
Junio C Hamano1ecc1cb2013-07-17 15:05:46 -07004516 for (i = 0; (optch = optarg[i]) != '\0'; i++) {
4517 unsigned int bit;
Junio C Hamano7f2ea5f2013-07-17 16:19:19 -07004518 int negate;
4519
4520 if ('a' <= optch && optch <= 'z') {
4521 negate = 1;
4522 optch = toupper(optch);
4523 } else {
4524 negate = 0;
4525 }
Junio C Hamano1ecc1cb2013-07-17 15:05:46 -07004526
4527 bit = (0 <= optch && optch <= 'Z') ? filter_bit[optch] : 0;
4528 if (!bit)
Junio C Hamanobf142ec2013-07-17 15:27:19 -07004529 return optarg[i];
Junio C Hamano7f2ea5f2013-07-17 16:19:19 -07004530 if (negate)
4531 opt->filter &= ~bit;
4532 else
4533 opt->filter |= bit;
Junio C Hamano1ecc1cb2013-07-17 15:05:46 -07004534 }
4535 return 0;
4536}
4537
Matthieu Moy71482d32013-07-16 10:05:37 +02004538static void enable_patch_output(int *fmt) {
4539 *fmt &= ~DIFF_FORMAT_NO_OUTPUT;
4540 *fmt |= DIFF_FORMAT_PATCH;
4541}
4542
Junio C Hamano077965f2016-10-04 15:03:33 -07004543static int parse_ws_error_highlight_opt(struct diff_options *opt, const char *arg)
Junio C Hamanob8767f72015-05-26 10:11:28 -07004544{
Junio C Hamano077965f2016-10-04 15:03:33 -07004545 int val = parse_ws_error_highlight(arg);
Junio C Hamanob8767f72015-05-26 10:11:28 -07004546
Junio C Hamano077965f2016-10-04 15:03:33 -07004547 if (val < 0) {
4548 error("unknown value after ws-error-highlight=%.*s",
4549 -1 - val, arg);
4550 return 0;
Junio C Hamanob8767f72015-05-26 10:11:28 -07004551 }
4552 opt->ws_error_highlight = val;
4553 return 1;
4554}
4555
Duy Nguyena97262c2016-01-21 18:48:44 +07004556int diff_opt_parse(struct diff_options *options,
4557 const char **av, int ac, const char *prefix)
Junio C Hamano6973dca2006-04-21 23:57:45 -07004558{
4559 const char *arg = av[0];
Matthieu Moydea007f2010-08-05 10:22:52 +02004560 const char *optarg;
4561 int argcount;
Pierre Habouzitd0546802007-11-07 11:20:32 +01004562
Duy Nguyena97262c2016-01-21 18:48:44 +07004563 if (!prefix)
4564 prefix = "";
4565
Pierre Habouzitd0546802007-11-07 11:20:32 +01004566 /* Output format options */
Matthieu Moy71482d32013-07-16 10:05:37 +02004567 if (!strcmp(arg, "-p") || !strcmp(arg, "-u") || !strcmp(arg, "--patch")
4568 || opt_arg(arg, 'U', "unified", &options->context))
4569 enable_patch_output(&options->output_format);
Timo Hirvonena6107862006-06-24 20:23:06 +03004570 else if (!strcmp(arg, "--raw"))
4571 options->output_format |= DIFF_FORMAT_RAW;
Matthieu Moy71482d32013-07-16 10:05:37 +02004572 else if (!strcmp(arg, "--patch-with-raw")) {
4573 enable_patch_output(&options->output_format);
4574 options->output_format |= DIFF_FORMAT_RAW;
4575 } else if (!strcmp(arg, "--numstat"))
Junio C Hamano74e2abe2006-10-12 03:01:00 -07004576 options->output_format |= DIFF_FORMAT_NUMSTAT;
Pierre Habouzit8f67f8a2007-11-10 20:05:14 +01004577 else if (!strcmp(arg, "--shortstat"))
Nicolas Pitreebd124c2006-12-14 23:15:44 -05004578 options->output_format |= DIFF_FORMAT_SHORTSTAT;
Johan Herland333f3fb2011-04-29 11:36:18 +02004579 else if (!strcmp(arg, "-X") || !strcmp(arg, "--dirstat"))
4580 return parse_dirstat_opt(options, "");
Jeff Kingae021d82014-06-18 15:47:50 -04004581 else if (skip_prefix(arg, "-X", &arg))
4582 return parse_dirstat_opt(options, arg);
4583 else if (skip_prefix(arg, "--dirstat=", &arg))
4584 return parse_dirstat_opt(options, arg);
Johan Herland333f3fb2011-04-29 11:36:18 +02004585 else if (!strcmp(arg, "--cumulative"))
4586 return parse_dirstat_opt(options, "cumulative");
4587 else if (!strcmp(arg, "--dirstat-by-file"))
4588 return parse_dirstat_opt(options, "files");
Jeff Kingae021d82014-06-18 15:47:50 -04004589 else if (skip_prefix(arg, "--dirstat-by-file=", &arg)) {
Johan Herland333f3fb2011-04-29 11:36:18 +02004590 parse_dirstat_opt(options, "files");
Jeff Kingae021d82014-06-18 15:47:50 -04004591 return parse_dirstat_opt(options, arg);
Junio C Hamanof88d2252008-09-02 17:28:59 -07004592 }
Pierre Habouzitd0546802007-11-07 11:20:32 +01004593 else if (!strcmp(arg, "--check"))
4594 options->output_format |= DIFF_FORMAT_CHECKDIFF;
4595 else if (!strcmp(arg, "--summary"))
4596 options->output_format |= DIFF_FORMAT_SUMMARY;
Matthieu Moy71482d32013-07-16 10:05:37 +02004597 else if (!strcmp(arg, "--patch-with-stat")) {
4598 enable_patch_output(&options->output_format);
4599 options->output_format |= DIFF_FORMAT_DIFFSTAT;
4600 } else if (!strcmp(arg, "--name-only"))
Pierre Habouzitd0546802007-11-07 11:20:32 +01004601 options->output_format |= DIFF_FORMAT_NAME;
4602 else if (!strcmp(arg, "--name-status"))
4603 options->output_format |= DIFF_FORMAT_NAME_STATUS;
Matthieu Moyd09cd152013-07-16 10:05:36 +02004604 else if (!strcmp(arg, "-s") || !strcmp(arg, "--no-patch"))
Pierre Habouzitd0546802007-11-07 11:20:32 +01004605 options->output_format |= DIFF_FORMAT_NO_OUTPUT;
Christian Couder59556542013-11-30 21:55:40 +01004606 else if (starts_with(arg, "--stat"))
Michael J Gruber808e1db2011-05-27 14:36:41 +02004607 /* --stat, --stat-width, --stat-name-width, or --stat-count */
Jonathan Nieder4d7f7a42010-08-05 10:22:53 +02004608 return stat_opt(options, av);
Pierre Habouzitd0546802007-11-07 11:20:32 +01004609
4610 /* renames options */
Christian Couder59556542013-11-30 21:55:40 +01004611 else if (starts_with(arg, "-B") || starts_with(arg, "--break-rewrites=") ||
Kevin Ballard37ab5152010-09-27 16:58:26 -07004612 !strcmp(arg, "--break-rewrites")) {
Pierre Habouzit8f67f8a2007-11-10 20:05:14 +01004613 if ((options->break_opt = diff_scoreopt_parse(arg)) == -1)
Jeff King07cd7262010-10-21 10:49:47 -04004614 return error("invalid argument to -B: %s", arg+2);
Junio C Hamano6973dca2006-04-21 23:57:45 -07004615 }
Christian Couder59556542013-11-30 21:55:40 +01004616 else if (starts_with(arg, "-M") || starts_with(arg, "--find-renames=") ||
Yann Dirsonf611ddc2010-11-10 21:27:12 +01004617 !strcmp(arg, "--find-renames")) {
Pierre Habouzit8f67f8a2007-11-10 20:05:14 +01004618 if ((options->rename_score = diff_scoreopt_parse(arg)) == -1)
Jeff King07cd7262010-10-21 10:49:47 -04004619 return error("invalid argument to -M: %s", arg+2);
Junio C Hamano6973dca2006-04-21 23:57:45 -07004620 options->detect_rename = DIFF_DETECT_RENAME;
4621 }
Junio C Hamano467ddc12011-02-28 16:11:55 -08004622 else if (!strcmp(arg, "-D") || !strcmp(arg, "--irreversible-delete")) {
4623 options->irreversible_delete = 1;
4624 }
Christian Couder59556542013-11-30 21:55:40 +01004625 else if (starts_with(arg, "-C") || starts_with(arg, "--find-copies=") ||
Yann Dirsonf611ddc2010-11-10 21:27:12 +01004626 !strcmp(arg, "--find-copies")) {
Johannes Schindelinca6c0972007-06-11 21:12:19 +01004627 if (options->detect_rename == DIFF_DETECT_COPY)
Pierre Habouzit8f67f8a2007-11-10 20:05:14 +01004628 DIFF_OPT_SET(options, FIND_COPIES_HARDER);
4629 if ((options->rename_score = diff_scoreopt_parse(arg)) == -1)
Jeff King07cd7262010-10-21 10:49:47 -04004630 return error("invalid argument to -C: %s", arg+2);
Junio C Hamano6973dca2006-04-21 23:57:45 -07004631 options->detect_rename = DIFF_DETECT_COPY;
4632 }
Pierre Habouzitd0546802007-11-07 11:20:32 +01004633 else if (!strcmp(arg, "--no-renames"))
4634 options->detect_rename = 0;
Jeff King90d43b02012-03-22 18:52:13 -04004635 else if (!strcmp(arg, "--rename-empty"))
4636 DIFF_OPT_SET(options, RENAME_EMPTY);
4637 else if (!strcmp(arg, "--no-rename-empty"))
4638 DIFF_OPT_CLR(options, RENAME_EMPTY);
Junio C Hamanocd676a52008-02-12 14:26:02 -08004639 else if (!strcmp(arg, "--relative"))
4640 DIFF_OPT_SET(options, RELATIVE_NAME);
Jeff Kingae021d82014-06-18 15:47:50 -04004641 else if (skip_prefix(arg, "--relative=", &arg)) {
Junio C Hamanoc0cb4a02008-02-13 00:34:39 -08004642 DIFF_OPT_SET(options, RELATIVE_NAME);
Jeff Kingae021d82014-06-18 15:47:50 -04004643 options->prefix = arg;
Junio C Hamanoc0cb4a02008-02-13 00:34:39 -08004644 }
Pierre Habouzitd0546802007-11-07 11:20:32 +01004645
4646 /* xdiff options */
Junio C Hamano81b568c2011-10-01 21:56:28 -07004647 else if (!strcmp(arg, "--minimal"))
4648 DIFF_XDL_SET(options, NEED_MINIMAL);
4649 else if (!strcmp(arg, "--no-minimal"))
4650 DIFF_XDL_CLR(options, NEED_MINIMAL);
Pierre Habouzitd0546802007-11-07 11:20:32 +01004651 else if (!strcmp(arg, "-w") || !strcmp(arg, "--ignore-all-space"))
Keith Cascio628d5c22009-02-16 19:26:49 -08004652 DIFF_XDL_SET(options, IGNORE_WHITESPACE);
Pierre Habouzitd0546802007-11-07 11:20:32 +01004653 else if (!strcmp(arg, "-b") || !strcmp(arg, "--ignore-space-change"))
Keith Cascio628d5c22009-02-16 19:26:49 -08004654 DIFF_XDL_SET(options, IGNORE_WHITESPACE_CHANGE);
Pierre Habouzitd0546802007-11-07 11:20:32 +01004655 else if (!strcmp(arg, "--ignore-space-at-eol"))
Keith Cascio628d5c22009-02-16 19:26:49 -08004656 DIFF_XDL_SET(options, IGNORE_WHITESPACE_AT_EOL);
Antoine Pelisse36617af2013-06-19 20:46:07 +02004657 else if (!strcmp(arg, "--ignore-blank-lines"))
4658 DIFF_XDL_SET(options, IGNORE_BLANK_LINES);
Junio C Hamano3cde4e02016-12-23 12:32:22 -08004659 else if (!strcmp(arg, "--indent-heuristic"))
Michael Haggerty433860f2016-09-05 11:44:51 +02004660 DIFF_XDL_SET(options, INDENT_HEURISTIC);
Junio C Hamano3cde4e02016-12-23 12:32:22 -08004661 else if (!strcmp(arg, "--no-indent-heuristic"))
Michael Haggerty433860f2016-09-05 11:44:51 +02004662 DIFF_XDL_CLR(options, INDENT_HEURISTIC);
Johannes Schindelin34292bd2009-01-01 17:39:17 +01004663 else if (!strcmp(arg, "--patience"))
Junio C Hamano307ab202012-02-19 15:36:55 -08004664 options->xdl_opts = DIFF_WITH_ALG(options, PATIENCE_DIFF);
Tay Ray Chuan8c912ee2011-07-12 14:10:25 +08004665 else if (!strcmp(arg, "--histogram"))
Junio C Hamano307ab202012-02-19 15:36:55 -08004666 options->xdl_opts = DIFF_WITH_ALG(options, HISTOGRAM_DIFF);
John Keeping0895c6d2013-04-05 13:19:14 +01004667 else if ((argcount = parse_long_opt("diff-algorithm", av, &optarg))) {
4668 long value = parse_algorithm_value(optarg);
Michal Privoznik07924d42013-01-16 08:51:58 +01004669 if (value < 0)
4670 return error("option diff-algorithm accepts \"myers\", "
4671 "\"minimal\", \"patience\" and \"histogram\"");
4672 /* clear out previous settings */
4673 DIFF_XDL_CLR(options, NEED_MINIMAL);
4674 options->xdl_opts &= ~XDF_DIFF_ALGORITHM_MASK;
4675 options->xdl_opts |= value;
John Keeping0895c6d2013-04-05 13:19:14 +01004676 return argcount;
Michal Privoznik07924d42013-01-16 08:51:58 +01004677 }
Pierre Habouzitd0546802007-11-07 11:20:32 +01004678
4679 /* flags options */
4680 else if (!strcmp(arg, "--binary")) {
Matthieu Moy71482d32013-07-16 10:05:37 +02004681 enable_patch_output(&options->output_format);
Pierre Habouzitd0546802007-11-07 11:20:32 +01004682 DIFF_OPT_SET(options, BINARY);
4683 }
4684 else if (!strcmp(arg, "--full-index"))
4685 DIFF_OPT_SET(options, FULL_INDEX);
4686 else if (!strcmp(arg, "-a") || !strcmp(arg, "--text"))
4687 DIFF_OPT_SET(options, TEXT);
4688 else if (!strcmp(arg, "-R"))
4689 DIFF_OPT_SET(options, REVERSE_DIFF);
Junio C Hamano6973dca2006-04-21 23:57:45 -07004690 else if (!strcmp(arg, "--find-copies-harder"))
Pierre Habouzit8f67f8a2007-11-10 20:05:14 +01004691 DIFF_OPT_SET(options, FIND_COPIES_HARDER);
Linus Torvalds750f7b62007-06-19 14:22:46 -07004692 else if (!strcmp(arg, "--follow"))
Pierre Habouzit8f67f8a2007-11-10 20:05:14 +01004693 DIFF_OPT_SET(options, FOLLOW_RENAMES);
David Turner076c9832015-07-07 21:29:34 -04004694 else if (!strcmp(arg, "--no-follow")) {
Junio C Hamanoaebbcf52012-09-21 13:49:18 -07004695 DIFF_OPT_CLR(options, FOLLOW_RENAMES);
David Turner076c9832015-07-07 21:29:34 -04004696 DIFF_OPT_CLR(options, DEFAULT_FOLLOW_RENAMES);
4697 } else if (!strcmp(arg, "--color"))
Jeff Kingf1c96262011-08-17 22:03:12 -07004698 options->use_color = 1;
Jeff Kingae021d82014-06-18 15:47:50 -04004699 else if (skip_prefix(arg, "--color=", &arg)) {
4700 int value = git_config_colorbool(NULL, arg);
Jeff Kingdaa0c3d2011-08-17 22:04:23 -07004701 if (value < 0)
Mark Lodato73e9da02010-02-16 23:55:58 -05004702 return error("option `color' expects \"always\", \"auto\", or \"never\"");
Jeff Kingdaa0c3d2011-08-17 22:04:23 -07004703 options->use_color = value;
Mark Lodato73e9da02010-02-16 23:55:58 -05004704 }
Pierre Habouzitd0546802007-11-07 11:20:32 +01004705 else if (!strcmp(arg, "--no-color"))
Jeff Kingf1c96262011-08-17 22:03:12 -07004706 options->use_color = 0;
Stefan Beller2e2d5ac2017-06-30 13:53:07 -07004707 else if (!strcmp(arg, "--color-moved")) {
4708 if (diff_color_moved_default)
4709 options->color_moved = diff_color_moved_default;
4710 if (options->color_moved == COLOR_MOVED_NO)
4711 options->color_moved = COLOR_MOVED_DEFAULT;
4712 } else if (!strcmp(arg, "--no-color-moved"))
4713 options->color_moved = COLOR_MOVED_NO;
4714 else if (skip_prefix(arg, "--color-moved=", &arg)) {
4715 int cm = parse_color_moved(arg);
4716 if (cm < 0)
4717 die("bad --color-moved argument: %s", arg);
4718 options->color_moved = cm;
4719 } else if (!strcmp(arg, "--color-words")) {
Jeff Kingf1c96262011-08-17 22:03:12 -07004720 options->use_color = 1;
Thomas Rast882749a2010-04-14 17:59:06 +02004721 options->word_diff = DIFF_WORDS_COLOR;
Keith Cascio628d5c22009-02-16 19:26:49 -08004722 }
Jeff Kingae021d82014-06-18 15:47:50 -04004723 else if (skip_prefix(arg, "--color-words=", &arg)) {
Jeff Kingf1c96262011-08-17 22:03:12 -07004724 options->use_color = 1;
Thomas Rast882749a2010-04-14 17:59:06 +02004725 options->word_diff = DIFF_WORDS_COLOR;
Jeff Kingae021d82014-06-18 15:47:50 -04004726 options->word_regex = arg;
Johannes Schindelin2b6a5412009-01-17 17:29:45 +01004727 }
Thomas Rast882749a2010-04-14 17:59:06 +02004728 else if (!strcmp(arg, "--word-diff")) {
4729 if (options->word_diff == DIFF_WORDS_NONE)
4730 options->word_diff = DIFF_WORDS_PLAIN;
4731 }
Jeff Kingae021d82014-06-18 15:47:50 -04004732 else if (skip_prefix(arg, "--word-diff=", &arg)) {
4733 if (!strcmp(arg, "plain"))
Thomas Rast882749a2010-04-14 17:59:06 +02004734 options->word_diff = DIFF_WORDS_PLAIN;
Jeff Kingae021d82014-06-18 15:47:50 -04004735 else if (!strcmp(arg, "color")) {
Jeff Kingf1c96262011-08-17 22:03:12 -07004736 options->use_color = 1;
Thomas Rast882749a2010-04-14 17:59:06 +02004737 options->word_diff = DIFF_WORDS_COLOR;
4738 }
Jeff Kingae021d82014-06-18 15:47:50 -04004739 else if (!strcmp(arg, "porcelain"))
Thomas Rast882749a2010-04-14 17:59:06 +02004740 options->word_diff = DIFF_WORDS_PORCELAIN;
Jeff Kingae021d82014-06-18 15:47:50 -04004741 else if (!strcmp(arg, "none"))
Thomas Rast882749a2010-04-14 17:59:06 +02004742 options->word_diff = DIFF_WORDS_NONE;
4743 else
Jeff Kingae021d82014-06-18 15:47:50 -04004744 die("bad --word-diff argument: %s", arg);
Thomas Rast882749a2010-04-14 17:59:06 +02004745 }
Matthieu Moydea007f2010-08-05 10:22:52 +02004746 else if ((argcount = parse_long_opt("word-diff-regex", av, &optarg))) {
Thomas Rast882749a2010-04-14 17:59:06 +02004747 if (options->word_diff == DIFF_WORDS_NONE)
4748 options->word_diff = DIFF_WORDS_PLAIN;
Matthieu Moydea007f2010-08-05 10:22:52 +02004749 options->word_regex = optarg;
4750 return argcount;
Thomas Rast882749a2010-04-14 17:59:06 +02004751 }
Pierre Habouzitd0546802007-11-07 11:20:32 +01004752 else if (!strcmp(arg, "--exit-code"))
4753 DIFF_OPT_SET(options, EXIT_WITH_STATUS);
4754 else if (!strcmp(arg, "--quiet"))
Junio C Hamano90b19942009-05-23 01:15:35 -07004755 DIFF_OPT_SET(options, QUICK);
Pierre Habouzitd0546802007-11-07 11:20:32 +01004756 else if (!strcmp(arg, "--ext-diff"))
4757 DIFF_OPT_SET(options, ALLOW_EXTERNAL);
4758 else if (!strcmp(arg, "--no-ext-diff"))
4759 DIFF_OPT_CLR(options, ALLOW_EXTERNAL);
Jeff Kinge10ea812008-12-07 21:57:01 -05004760 else if (!strcmp(arg, "--textconv"))
4761 DIFF_OPT_SET(options, ALLOW_TEXTCONV);
4762 else if (!strcmp(arg, "--no-textconv"))
4763 DIFF_OPT_CLR(options, ALLOW_TEXTCONV);
Jens Lehmannaee9c7d2010-08-06 00:39:25 +02004764 else if (!strcmp(arg, "--ignore-submodules")) {
4765 DIFF_OPT_SET(options, OVERRIDE_SUBMODULE_CONFIG);
Jens Lehmann46a958b2010-06-25 16:56:47 +02004766 handle_ignore_submodules_arg(options, "all");
Jeff Kingae021d82014-06-18 15:47:50 -04004767 } else if (skip_prefix(arg, "--ignore-submodules=", &arg)) {
Jens Lehmannaee9c7d2010-08-06 00:39:25 +02004768 DIFF_OPT_SET(options, OVERRIDE_SUBMODULE_CONFIG);
Jeff Kingae021d82014-06-18 15:47:50 -04004769 handle_ignore_submodules_arg(options, arg);
Jens Lehmannaee9c7d2010-08-06 00:39:25 +02004770 } else if (!strcmp(arg, "--submodule"))
Jacob Keller61cfbc02016-08-31 16:27:21 -07004771 options->submodule_format = DIFF_SUBMODULE_LOG;
Jeff Kingae021d82014-06-18 15:47:50 -04004772 else if (skip_prefix(arg, "--submodule=", &arg))
4773 return parse_submodule_opt(options, arg);
Junio C Hamanob8767f72015-05-26 10:11:28 -07004774 else if (skip_prefix(arg, "--ws-error-highlight=", &arg))
Junio C Hamano077965f2016-10-04 15:03:33 -07004775 return parse_ws_error_highlight_opt(options, arg);
Nguyễn Thái Ngọc Duyb42b4512016-10-24 17:42:20 +07004776 else if (!strcmp(arg, "--ita-invisible-in-index"))
4777 options->ita_invisible_in_index = 1;
4778 else if (!strcmp(arg, "--ita-visible-in-index"))
4779 options->ita_invisible_in_index = 0;
Pierre Habouzitd0546802007-11-07 11:20:32 +01004780
4781 /* misc options */
4782 else if (!strcmp(arg, "-z"))
4783 options->line_termination = 0;
Matthieu Moydea007f2010-08-05 10:22:52 +02004784 else if ((argcount = short_opt('l', av, &optarg))) {
4785 options->rename_limit = strtoul(optarg, NULL, 10);
4786 return argcount;
4787 }
4788 else if ((argcount = short_opt('S', av, &optarg))) {
4789 options->pickaxe = optarg;
Junio C Hamanof506b8e2010-08-23 10:17:03 -07004790 options->pickaxe_opts |= DIFF_PICKAXE_KIND_S;
4791 return argcount;
4792 } else if ((argcount = short_opt('G', av, &optarg))) {
4793 options->pickaxe = optarg;
4794 options->pickaxe_opts |= DIFF_PICKAXE_KIND_G;
Matthieu Moydea007f2010-08-05 10:22:52 +02004795 return argcount;
4796 }
Pierre Habouzitd0546802007-11-07 11:20:32 +01004797 else if (!strcmp(arg, "--pickaxe-all"))
Junio C Hamanof506b8e2010-08-23 10:17:03 -07004798 options->pickaxe_opts |= DIFF_PICKAXE_ALL;
Pierre Habouzitd0546802007-11-07 11:20:32 +01004799 else if (!strcmp(arg, "--pickaxe-regex"))
Junio C Hamanof506b8e2010-08-23 10:17:03 -07004800 options->pickaxe_opts |= DIFF_PICKAXE_REGEX;
Matthieu Moydea007f2010-08-05 10:22:52 +02004801 else if ((argcount = short_opt('O', av, &optarg))) {
Jeff Kinge4da43b2017-03-20 21:28:49 -04004802 options->orderfile = prefix_filename(prefix, optarg);
Matthieu Moydea007f2010-08-05 10:22:52 +02004803 return argcount;
4804 }
4805 else if ((argcount = parse_long_opt("diff-filter", av, &optarg))) {
Junio C Hamano1ecc1cb2013-07-17 15:05:46 -07004806 int offending = parse_diff_filter_opt(optarg, options);
4807 if (offending)
4808 die("unknown change class '%c' in --diff-filter=%s",
4809 offending, optarg);
Matthieu Moydea007f2010-08-05 10:22:52 +02004810 return argcount;
4811 }
Jack Bates43d19482016-12-06 09:56:14 -07004812 else if (!strcmp(arg, "--no-abbrev"))
4813 options->abbrev = 0;
Junio C Hamano6973dca2006-04-21 23:57:45 -07004814 else if (!strcmp(arg, "--abbrev"))
4815 options->abbrev = DEFAULT_ABBREV;
Jeff Kingae021d82014-06-18 15:47:50 -04004816 else if (skip_prefix(arg, "--abbrev=", &arg)) {
4817 options->abbrev = strtoul(arg, NULL, 10);
Junio C Hamano6973dca2006-04-21 23:57:45 -07004818 if (options->abbrev < MINIMUM_ABBREV)
4819 options->abbrev = MINIMUM_ABBREV;
4820 else if (40 < options->abbrev)
4821 options->abbrev = 40;
4822 }
Matthieu Moydea007f2010-08-05 10:22:52 +02004823 else if ((argcount = parse_long_opt("src-prefix", av, &optarg))) {
4824 options->a_prefix = optarg;
4825 return argcount;
4826 }
Jacob Keller660e1132016-08-31 16:27:20 -07004827 else if ((argcount = parse_long_opt("line-prefix", av, &optarg))) {
4828 options->line_prefix = optarg;
4829 options->line_prefix_length = strlen(options->line_prefix);
4830 graph_setup_line_prefix(options);
4831 return argcount;
4832 }
Matthieu Moydea007f2010-08-05 10:22:52 +02004833 else if ((argcount = parse_long_opt("dst-prefix", av, &optarg))) {
4834 options->b_prefix = optarg;
4835 return argcount;
4836 }
Johannes Schindelineab9a402007-12-18 19:32:14 +00004837 else if (!strcmp(arg, "--no-prefix"))
4838 options->a_prefix = options->b_prefix = "";
René Scharfe6d0e6742008-12-28 19:45:32 +01004839 else if (opt_arg(arg, '\0', "inter-hunk-context",
4840 &options->interhunkcontext))
4841 ;
René Scharfe14937c22011-10-09 13:36:57 +02004842 else if (!strcmp(arg, "-W"))
4843 DIFF_OPT_SET(options, FUNCCONTEXT);
4844 else if (!strcmp(arg, "--function-context"))
4845 DIFF_OPT_SET(options, FUNCCONTEXT);
4846 else if (!strcmp(arg, "--no-function-context"))
4847 DIFF_OPT_CLR(options, FUNCCONTEXT);
Matthieu Moydea007f2010-08-05 10:22:52 +02004848 else if ((argcount = parse_long_opt("output", av, &optarg))) {
Jeff Kinge4da43b2017-03-20 21:28:49 -04004849 char *path = prefix_filename(prefix, optarg);
Nguyễn Thái Ngọc Duy23a9e072017-05-03 17:16:46 +07004850 options->file = xfopen(path, "w");
Daniel Barkalowc0c77732008-03-09 22:43:39 -04004851 options->close_file = 1;
Johannes Schindelinafc676f2016-06-22 16:41:07 +02004852 if (options->use_color != GIT_COLOR_ALWAYS)
4853 options->use_color = GIT_COLOR_NEVER;
Jeff Kinge4da43b2017-03-20 21:28:49 -04004854 free(path);
Matthieu Moydea007f2010-08-05 10:22:52 +02004855 return argcount;
Daniel Barkalowc0c77732008-03-09 22:43:39 -04004856 } else
Junio C Hamano6973dca2006-04-21 23:57:45 -07004857 return 0;
4858 return 1;
4859}
4860
Kevin Ballard10ae7522010-09-27 16:58:25 -07004861int parse_rename_score(const char **cp_p)
Junio C Hamano6973dca2006-04-21 23:57:45 -07004862{
4863 unsigned long num, scale;
4864 int ch, dot;
4865 const char *cp = *cp_p;
4866
4867 num = 0;
4868 scale = 1;
4869 dot = 0;
Brian Gianforcaroeeefa7c2009-09-01 01:35:10 -04004870 for (;;) {
Junio C Hamano6973dca2006-04-21 23:57:45 -07004871 ch = *cp;
4872 if ( !dot && ch == '.' ) {
4873 scale = 1;
4874 dot = 1;
4875 } else if ( ch == '%' ) {
4876 scale = dot ? scale*100 : 100;
4877 cp++; /* % is always at the end */
4878 break;
4879 } else if ( ch >= '0' && ch <= '9' ) {
4880 if ( scale < 100000 ) {
4881 scale *= 10;
4882 num = (num*10) + (ch-'0');
4883 }
4884 } else {
4885 break;
4886 }
4887 cp++;
4888 }
4889 *cp_p = cp;
4890
4891 /* user says num divided by scale and we say internally that
4892 * is MAX_SCORE * num / scale.
4893 */
Shawn O. Pearcedc49cd72007-03-06 20:44:37 -05004894 return (int)((num >= scale) ? MAX_SCORE : (MAX_SCORE * num / scale));
Junio C Hamano6973dca2006-04-21 23:57:45 -07004895}
4896
Junio C Hamano16befb82007-06-08 02:54:57 -07004897static int diff_scoreopt_parse(const char *opt)
Junio C Hamano6973dca2006-04-21 23:57:45 -07004898{
4899 int opt1, opt2, cmd;
4900
4901 if (*opt++ != '-')
4902 return -1;
4903 cmd = *opt++;
Kevin Ballard37ab5152010-09-27 16:58:26 -07004904 if (cmd == '-') {
4905 /* convert the long-form arguments into short-form versions */
Jeff King95b567c2014-06-18 15:48:29 -04004906 if (skip_prefix(opt, "break-rewrites", &opt)) {
Kevin Ballard37ab5152010-09-27 16:58:26 -07004907 if (*opt == 0 || *opt++ == '=')
4908 cmd = 'B';
Jeff King95b567c2014-06-18 15:48:29 -04004909 } else if (skip_prefix(opt, "find-copies", &opt)) {
Kevin Ballard37ab5152010-09-27 16:58:26 -07004910 if (*opt == 0 || *opt++ == '=')
4911 cmd = 'C';
Jeff King95b567c2014-06-18 15:48:29 -04004912 } else if (skip_prefix(opt, "find-renames", &opt)) {
Kevin Ballard37ab5152010-09-27 16:58:26 -07004913 if (*opt == 0 || *opt++ == '=')
4914 cmd = 'M';
4915 }
4916 }
Junio C Hamano6973dca2006-04-21 23:57:45 -07004917 if (cmd != 'M' && cmd != 'C' && cmd != 'B')
Justin Lebar01689902014-03-31 15:11:46 -07004918 return -1; /* that is not a -M, -C, or -B option */
Junio C Hamano6973dca2006-04-21 23:57:45 -07004919
Kevin Ballard10ae7522010-09-27 16:58:25 -07004920 opt1 = parse_rename_score(&opt);
Junio C Hamano6973dca2006-04-21 23:57:45 -07004921 if (cmd != 'B')
4922 opt2 = 0;
4923 else {
4924 if (*opt == 0)
4925 opt2 = 0;
4926 else if (*opt != '/')
4927 return -1; /* we expect -B80/99 or -B80 */
4928 else {
4929 opt++;
Kevin Ballard10ae7522010-09-27 16:58:25 -07004930 opt2 = parse_rename_score(&opt);
Junio C Hamano6973dca2006-04-21 23:57:45 -07004931 }
4932 }
4933 if (*opt != 0)
4934 return -1;
4935 return opt1 | (opt2 << 16);
4936}
4937
4938struct diff_queue_struct diff_queued_diff;
4939
4940void diff_q(struct diff_queue_struct *queue, struct diff_filepair *dp)
4941{
Dmitry S. Dolzhenko4c960a42014-03-04 02:31:53 +04004942 ALLOC_GROW(queue->queue, queue->nr + 1, queue->alloc);
Junio C Hamano6973dca2006-04-21 23:57:45 -07004943 queue->queue[queue->nr++] = dp;
4944}
4945
4946struct diff_filepair *diff_queue(struct diff_queue_struct *queue,
4947 struct diff_filespec *one,
4948 struct diff_filespec *two)
4949{
Junio C Hamanoef677682006-08-03 12:01:01 -07004950 struct diff_filepair *dp = xcalloc(1, sizeof(*dp));
Junio C Hamano6973dca2006-04-21 23:57:45 -07004951 dp->one = one;
4952 dp->two = two;
Junio C Hamano6973dca2006-04-21 23:57:45 -07004953 if (queue)
4954 diff_q(queue, dp);
4955 return dp;
4956}
4957
4958void diff_free_filepair(struct diff_filepair *p)
4959{
Linus Torvalds9fb88412007-10-25 11:19:10 -07004960 free_filespec(p->one);
4961 free_filespec(p->two);
Junio C Hamano6973dca2006-04-21 23:57:45 -07004962 free(p);
4963}
4964
Jeff Kingd6cece52016-10-20 02:20:07 -04004965const char *diff_aligned_abbrev(const struct object_id *oid, int len)
Junio C Hamano6973dca2006-04-21 23:57:45 -07004966{
4967 int abblen;
4968 const char *abbrev;
Junio C Hamano6973dca2006-04-21 23:57:45 -07004969
Jeff Kingd6cece52016-10-20 02:20:07 -04004970 if (len == GIT_SHA1_HEXSZ)
4971 return oid_to_hex(oid);
4972
Jeff King4f036662016-10-20 02:21:25 -04004973 abbrev = diff_abbrev_oid(oid, len);
Junio C Hamano6973dca2006-04-21 23:57:45 -07004974 abblen = strlen(abbrev);
Junio C Hamanod709f1f2016-09-30 10:42:05 -07004975
4976 /*
4977 * In well-behaved cases, where the abbbreviated result is the
4978 * same as the requested length, append three dots after the
4979 * abbreviation (hence the whole logic is limited to the case
4980 * where abblen < 37); when the actual abbreviated result is a
4981 * bit longer than the requested length, we reduce the number
4982 * of dots so that they match the well-behaved ones. However,
4983 * if the actual abbreviation is longer than the requested
4984 * length by more than three, we give up on aligning, and add
4985 * three dots anyway, to indicate that the output is not the
4986 * full object name. Yes, this may be suboptimal, but this
4987 * appears only in "diff --raw --abbrev" output and it is not
4988 * worth the effort to change it now. Note that this would
4989 * likely to work fine when the automatic sizing of default
4990 * abbreviation length is used--we would be fed -1 in "len" in
4991 * that case, and will end up always appending three-dots, but
4992 * the automatic sizing is supposed to give abblen that ensures
4993 * uniqueness across all objects (statistically speaking).
4994 */
Jeff Kingd6cece52016-10-20 02:20:07 -04004995 if (abblen < GIT_SHA1_HEXSZ - 3) {
brian m. carlsondc015052017-03-26 16:01:24 +00004996 static char hex[GIT_MAX_HEXSZ + 1];
Junio C Hamano6973dca2006-04-21 23:57:45 -07004997 if (len < abblen && abblen <= len + 2)
Jeff King5096d492015-09-24 17:06:08 -04004998 xsnprintf(hex, sizeof(hex), "%s%.*s", abbrev, len+3-abblen, "..");
Junio C Hamano6973dca2006-04-21 23:57:45 -07004999 else
Jeff King5096d492015-09-24 17:06:08 -04005000 xsnprintf(hex, sizeof(hex), "%s...", abbrev);
Junio C Hamano6973dca2006-04-21 23:57:45 -07005001 return hex;
5002 }
Jeff Kingd6cece52016-10-20 02:20:07 -04005003
5004 return oid_to_hex(oid);
Junio C Hamano6973dca2006-04-21 23:57:45 -07005005}
5006
Pierre Habouzit663af342007-09-20 00:42:15 +02005007static void diff_flush_raw(struct diff_filepair *p, struct diff_options *opt)
Junio C Hamano6973dca2006-04-21 23:57:45 -07005008{
Pierre Habouzit663af342007-09-20 00:42:15 +02005009 int line_termination = opt->line_termination;
5010 int inter_name_termination = line_termination ? '\t' : '\0';
Timo Hirvonenc6744342006-06-24 20:21:53 +03005011
John Keeping30997bb2013-02-07 20:15:27 +00005012 fprintf(opt->file, "%s", diff_line_prefix(opt));
Pierre Habouzit663af342007-09-20 00:42:15 +02005013 if (!(opt->output_format & DIFF_FORMAT_NAME_STATUS)) {
Daniel Barkalowc0c77732008-03-09 22:43:39 -04005014 fprintf(opt->file, ":%06o %06o %s ", p->one->mode, p->two->mode,
Jeff Kingd6cece52016-10-20 02:20:07 -04005015 diff_aligned_abbrev(&p->one->oid, opt->abbrev));
brian m. carlsona0d12c42016-06-24 23:09:23 +00005016 fprintf(opt->file, "%s ",
Jeff Kingd6cece52016-10-20 02:20:07 -04005017 diff_aligned_abbrev(&p->two->oid, opt->abbrev));
Pierre Habouzit663af342007-09-20 00:42:15 +02005018 }
5019 if (p->score) {
Daniel Barkalowc0c77732008-03-09 22:43:39 -04005020 fprintf(opt->file, "%c%03d%c", p->status, similarity_index(p),
5021 inter_name_termination);
Pierre Habouzit663af342007-09-20 00:42:15 +02005022 } else {
Daniel Barkalowc0c77732008-03-09 22:43:39 -04005023 fprintf(opt->file, "%c%c", p->status, inter_name_termination);
Junio C Hamano6973dca2006-04-21 23:57:45 -07005024 }
5025
Junio C Hamanocd676a52008-02-12 14:26:02 -08005026 if (p->status == DIFF_STATUS_COPIED ||
5027 p->status == DIFF_STATUS_RENAMED) {
5028 const char *name_a, *name_b;
5029 name_a = p->one->path;
5030 name_b = p->two->path;
5031 strip_prefix(opt->prefix_length, &name_a, &name_b);
Daniel Barkalowc0c77732008-03-09 22:43:39 -04005032 write_name_quoted(name_a, opt->file, inter_name_termination);
5033 write_name_quoted(name_b, opt->file, line_termination);
Pierre Habouzit663af342007-09-20 00:42:15 +02005034 } else {
Junio C Hamanocd676a52008-02-12 14:26:02 -08005035 const char *name_a, *name_b;
5036 name_a = p->one->mode ? p->one->path : p->two->path;
5037 name_b = NULL;
5038 strip_prefix(opt->prefix_length, &name_a, &name_b);
Daniel Barkalowc0c77732008-03-09 22:43:39 -04005039 write_name_quoted(name_a, opt->file, line_termination);
Junio C Hamano6973dca2006-04-21 23:57:45 -07005040 }
Junio C Hamano6973dca2006-04-21 23:57:45 -07005041}
5042
5043int diff_unmodified_pair(struct diff_filepair *p)
5044{
5045 /* This function is written stricter than necessary to support
5046 * the currently implemented transformers, but the idea is to
5047 * let transformers to produce diff_filepairs any way they want,
5048 * and filter and clean them up here before producing the output.
5049 */
Pierre Habouzit663af342007-09-20 00:42:15 +02005050 struct diff_filespec *one = p->one, *two = p->two;
Junio C Hamano6973dca2006-04-21 23:57:45 -07005051
5052 if (DIFF_PAIR_UNMERGED(p))
5053 return 0; /* unmerged is interesting */
5054
Junio C Hamano6973dca2006-04-21 23:57:45 -07005055 /* deletion, addition, mode or type change
5056 * and rename are all interesting.
5057 */
5058 if (DIFF_FILE_VALID(one) != DIFF_FILE_VALID(two) ||
5059 DIFF_PAIR_MODE_CHANGED(p) ||
5060 strcmp(one->path, two->path))
5061 return 0;
5062
5063 /* both are valid and point at the same path. that is, we are
5064 * dealing with a change.
5065 */
brian m. carlson41c95602016-06-24 23:09:24 +00005066 if (one->oid_valid && two->oid_valid &&
brian m. carlsona0d12c42016-06-24 23:09:23 +00005067 !oidcmp(&one->oid, &two->oid) &&
Jens Lehmann85adbf22010-03-12 22:23:52 +01005068 !one->dirty_submodule && !two->dirty_submodule)
Junio C Hamano6973dca2006-04-21 23:57:45 -07005069 return 1; /* no change */
brian m. carlson41c95602016-06-24 23:09:24 +00005070 if (!one->oid_valid && !two->oid_valid)
Junio C Hamano6973dca2006-04-21 23:57:45 -07005071 return 1; /* both look at the same file on the filesystem. */
5072 return 0;
5073}
5074
5075static void diff_flush_patch(struct diff_filepair *p, struct diff_options *o)
5076{
5077 if (diff_unmodified_pair(p))
5078 return;
5079
5080 if ((DIFF_FILE_VALID(p->one) && S_ISDIR(p->one->mode)) ||
5081 (DIFF_FILE_VALID(p->two) && S_ISDIR(p->two->mode)))
5082 return; /* no tree diffs in patch format */
5083
5084 run_diff(p, o);
5085}
5086
5087static void diff_flush_stat(struct diff_filepair *p, struct diff_options *o,
5088 struct diffstat_t *diffstat)
5089{
5090 if (diff_unmodified_pair(p))
5091 return;
5092
5093 if ((DIFF_FILE_VALID(p->one) && S_ISDIR(p->one->mode)) ||
5094 (DIFF_FILE_VALID(p->two) && S_ISDIR(p->two->mode)))
Yann Dirsonc3fced62010-10-24 23:03:10 +02005095 return; /* no useful stat for tree diffs */
Junio C Hamano6973dca2006-04-21 23:57:45 -07005096
5097 run_diffstat(p, o, diffstat);
5098}
5099
Johannes Schindelin88246892006-05-20 23:43:13 +02005100static void diff_flush_checkdiff(struct diff_filepair *p,
5101 struct diff_options *o)
5102{
5103 if (diff_unmodified_pair(p))
5104 return;
5105
5106 if ((DIFF_FILE_VALID(p->one) && S_ISDIR(p->one->mode)) ||
5107 (DIFF_FILE_VALID(p->two) && S_ISDIR(p->two->mode)))
Yann Dirsonc3fced62010-10-24 23:03:10 +02005108 return; /* nothing to check in tree diffs */
Johannes Schindelin88246892006-05-20 23:43:13 +02005109
5110 run_checkdiff(p, o);
5111}
5112
Junio C Hamano6973dca2006-04-21 23:57:45 -07005113int diff_queue_is_empty(void)
5114{
5115 struct diff_queue_struct *q = &diff_queued_diff;
5116 int i;
5117 for (i = 0; i < q->nr; i++)
5118 if (!diff_unmodified_pair(q->queue[i]))
5119 return 0;
5120 return 1;
5121}
5122
5123#if DIFF_DEBUG
5124void diff_debug_filespec(struct diff_filespec *s, int x, const char *one)
5125{
5126 fprintf(stderr, "queue[%d] %s (%s) %s %06o %s\n",
5127 x, one ? one : "",
5128 s->path,
5129 DIFF_FILE_VALID(s) ? "valid" : "invalid",
5130 s->mode,
brian m. carlson41c95602016-06-24 23:09:24 +00005131 s->oid_valid ? oid_to_hex(&s->oid) : "");
Jeff King428d52a2014-01-16 20:21:59 -05005132 fprintf(stderr, "queue[%d] %s size %lu\n",
Junio C Hamano6973dca2006-04-21 23:57:45 -07005133 x, one ? one : "",
Jeff King428d52a2014-01-16 20:21:59 -05005134 s->size);
Junio C Hamano6973dca2006-04-21 23:57:45 -07005135}
5136
5137void diff_debug_filepair(const struct diff_filepair *p, int i)
5138{
5139 diff_debug_filespec(p->one, i, "one");
5140 diff_debug_filespec(p->two, i, "two");
Linus Torvalds64479712007-10-25 11:20:56 -07005141 fprintf(stderr, "score %d, status %c rename_used %d broken %d\n",
Junio C Hamano6973dca2006-04-21 23:57:45 -07005142 p->score, p->status ? p->status : '?',
Linus Torvalds64479712007-10-25 11:20:56 -07005143 p->one->rename_used, p->broken_pair);
Junio C Hamano6973dca2006-04-21 23:57:45 -07005144}
5145
5146void diff_debug_queue(const char *msg, struct diff_queue_struct *q)
5147{
5148 int i;
5149 if (msg)
5150 fprintf(stderr, "%s\n", msg);
5151 fprintf(stderr, "q->nr = %d\n", q->nr);
5152 for (i = 0; i < q->nr; i++) {
5153 struct diff_filepair *p = q->queue[i];
5154 diff_debug_filepair(p, i);
5155 }
5156}
5157#endif
5158
5159static void diff_resolve_rename_copy(void)
5160{
Linus Torvalds64479712007-10-25 11:20:56 -07005161 int i;
5162 struct diff_filepair *p;
Junio C Hamano6973dca2006-04-21 23:57:45 -07005163 struct diff_queue_struct *q = &diff_queued_diff;
5164
5165 diff_debug_queue("resolve-rename-copy", q);
5166
5167 for (i = 0; i < q->nr; i++) {
5168 p = q->queue[i];
5169 p->status = 0; /* undecided */
5170 if (DIFF_PAIR_UNMERGED(p))
5171 p->status = DIFF_STATUS_UNMERGED;
5172 else if (!DIFF_FILE_VALID(p->one))
5173 p->status = DIFF_STATUS_ADDED;
5174 else if (!DIFF_FILE_VALID(p->two))
5175 p->status = DIFF_STATUS_DELETED;
5176 else if (DIFF_PAIR_TYPE_CHANGED(p))
5177 p->status = DIFF_STATUS_TYPE_CHANGED;
5178
5179 /* from this point on, we are dealing with a pair
5180 * whose both sides are valid and of the same type, i.e.
5181 * either in-place edit or rename/copy edit.
5182 */
5183 else if (DIFF_PAIR_RENAME(p)) {
Linus Torvalds64479712007-10-25 11:20:56 -07005184 /*
5185 * A rename might have re-connected a broken
5186 * pair up, causing the pathnames to be the
5187 * same again. If so, that's not a rename at
5188 * all, just a modification..
5189 *
5190 * Otherwise, see if this source was used for
5191 * multiple renames, in which case we decrement
5192 * the count, and call it a copy.
Junio C Hamano6973dca2006-04-21 23:57:45 -07005193 */
Linus Torvalds64479712007-10-25 11:20:56 -07005194 if (!strcmp(p->one->path, p->two->path))
5195 p->status = DIFF_STATUS_MODIFIED;
5196 else if (--p->one->rename_used > 0)
Junio C Hamano6973dca2006-04-21 23:57:45 -07005197 p->status = DIFF_STATUS_COPIED;
Linus Torvalds64479712007-10-25 11:20:56 -07005198 else
Junio C Hamano6973dca2006-04-21 23:57:45 -07005199 p->status = DIFF_STATUS_RENAMED;
5200 }
brian m. carlsona0d12c42016-06-24 23:09:23 +00005201 else if (oidcmp(&p->one->oid, &p->two->oid) ||
Johannes Schindelind516c2d2007-02-22 21:50:10 +01005202 p->one->mode != p->two->mode ||
Jens Lehmann85adbf22010-03-12 22:23:52 +01005203 p->one->dirty_submodule ||
5204 p->two->dirty_submodule ||
brian m. carlsona0d12c42016-06-24 23:09:23 +00005205 is_null_oid(&p->one->oid))
Junio C Hamano6973dca2006-04-21 23:57:45 -07005206 p->status = DIFF_STATUS_MODIFIED;
5207 else {
5208 /* This is a "no-change" entry and should not
5209 * happen anymore, but prepare for broken callers.
5210 */
5211 error("feeding unmodified %s to diffcore",
5212 p->one->path);
5213 p->status = DIFF_STATUS_UNKNOWN;
5214 }
5215 }
5216 diff_debug_queue("resolve-rename-copy done", q);
5217}
5218
Timo Hirvonenc6744342006-06-24 20:21:53 +03005219static int check_pair_status(struct diff_filepair *p)
Junio C Hamano6973dca2006-04-21 23:57:45 -07005220{
Junio C Hamano6973dca2006-04-21 23:57:45 -07005221 switch (p->status) {
5222 case DIFF_STATUS_UNKNOWN:
Timo Hirvonenc6744342006-06-24 20:21:53 +03005223 return 0;
Junio C Hamano6973dca2006-04-21 23:57:45 -07005224 case 0:
5225 die("internal error in diff-resolve-rename-copy");
Junio C Hamano6973dca2006-04-21 23:57:45 -07005226 default:
Timo Hirvonenc6744342006-06-24 20:21:53 +03005227 return 1;
Junio C Hamano6973dca2006-04-21 23:57:45 -07005228 }
5229}
5230
Timo Hirvonenc6744342006-06-24 20:21:53 +03005231static void flush_one_pair(struct diff_filepair *p, struct diff_options *opt)
5232{
5233 int fmt = opt->output_format;
5234
5235 if (fmt & DIFF_FORMAT_CHECKDIFF)
5236 diff_flush_checkdiff(p, opt);
5237 else if (fmt & (DIFF_FORMAT_RAW | DIFF_FORMAT_NAME_STATUS))
5238 diff_flush_raw(p, opt);
Junio C Hamanocd676a52008-02-12 14:26:02 -08005239 else if (fmt & DIFF_FORMAT_NAME) {
5240 const char *name_a, *name_b;
5241 name_a = p->two->path;
5242 name_b = NULL;
5243 strip_prefix(opt->prefix_length, &name_a, &name_b);
Jeff Kingf5022b52017-02-08 15:31:15 -05005244 fprintf(opt->file, "%s", diff_line_prefix(opt));
Daniel Barkalowc0c77732008-03-09 22:43:39 -04005245 write_name_quoted(name_a, opt->file, opt->line_termination);
Junio C Hamanocd676a52008-02-12 14:26:02 -08005246 }
Timo Hirvonenc6744342006-06-24 20:21:53 +03005247}
5248
Stefan Beller146fdb02017-06-29 17:07:05 -07005249static void show_file_mode_name(struct diff_options *opt, const char *newdelete, struct diff_filespec *fs)
Sean4bbd2612006-05-14 08:13:49 -04005250{
Stefan Beller146fdb02017-06-29 17:07:05 -07005251 struct strbuf sb = STRBUF_INIT;
Sean4bbd2612006-05-14 08:13:49 -04005252 if (fs->mode)
Stefan Beller146fdb02017-06-29 17:07:05 -07005253 strbuf_addf(&sb, " %s mode %06o ", newdelete, fs->mode);
Sean4bbd2612006-05-14 08:13:49 -04005254 else
Stefan Beller146fdb02017-06-29 17:07:05 -07005255 strbuf_addf(&sb, " %s ", newdelete);
5256
5257 quote_c_style(fs->path, &sb, NULL, 0);
5258 strbuf_addch(&sb, '\n');
5259 emit_diff_symbol(opt, DIFF_SYMBOL_SUMMARY,
5260 sb.buf, sb.len, 0);
5261 strbuf_release(&sb);
Sean4bbd2612006-05-14 08:13:49 -04005262}
5263
Stefan Beller146fdb02017-06-29 17:07:05 -07005264static void show_mode_change(struct diff_options *opt, struct diff_filepair *p,
5265 int show_name)
Sean4bbd2612006-05-14 08:13:49 -04005266{
5267 if (p->one->mode && p->two->mode && p->one->mode != p->two->mode) {
Stefan Beller146fdb02017-06-29 17:07:05 -07005268 struct strbuf sb = STRBUF_INIT;
5269 strbuf_addf(&sb, " mode change %06o => %06o",
5270 p->one->mode, p->two->mode);
Alexandre Julliard0d26a642007-02-10 15:37:48 +01005271 if (show_name) {
Stefan Beller146fdb02017-06-29 17:07:05 -07005272 strbuf_addch(&sb, ' ');
5273 quote_c_style(p->two->path, &sb, NULL, 0);
Alexandre Julliard0d26a642007-02-10 15:37:48 +01005274 }
Stefan Beller58aaced2017-09-27 15:51:26 -07005275 strbuf_addch(&sb, '\n');
Stefan Beller146fdb02017-06-29 17:07:05 -07005276 emit_diff_symbol(opt, DIFF_SYMBOL_SUMMARY,
5277 sb.buf, sb.len, 0);
5278 strbuf_release(&sb);
Sean4bbd2612006-05-14 08:13:49 -04005279 }
5280}
5281
Stefan Beller146fdb02017-06-29 17:07:05 -07005282static void show_rename_copy(struct diff_options *opt, const char *renamecopy,
5283 struct diff_filepair *p)
Sean4bbd2612006-05-14 08:13:49 -04005284{
Stefan Beller146fdb02017-06-29 17:07:05 -07005285 struct strbuf sb = STRBUF_INIT;
Alexandre Julliardb9f44162007-02-10 15:36:47 +01005286 char *names = pprint_rename(p->one->path, p->two->path);
Stefan Beller146fdb02017-06-29 17:07:05 -07005287 strbuf_addf(&sb, " %s %s (%d%%)\n",
5288 renamecopy, names, similarity_index(p));
Alexandre Julliardb9f44162007-02-10 15:36:47 +01005289 free(names);
Stefan Beller146fdb02017-06-29 17:07:05 -07005290 emit_diff_symbol(opt, DIFF_SYMBOL_SUMMARY,
5291 sb.buf, sb.len, 0);
5292 show_mode_change(opt, p, 0);
Rene Scharfe348eda22017-08-30 19:49:42 +02005293 strbuf_release(&sb);
Sean4bbd2612006-05-14 08:13:49 -04005294}
5295
Bo Yang7be57612010-05-26 15:23:54 +08005296static void diff_summary(struct diff_options *opt, struct diff_filepair *p)
Sean4bbd2612006-05-14 08:13:49 -04005297{
5298 switch(p->status) {
5299 case DIFF_STATUS_DELETED:
Stefan Beller146fdb02017-06-29 17:07:05 -07005300 show_file_mode_name(opt, "delete", p->one);
Sean4bbd2612006-05-14 08:13:49 -04005301 break;
5302 case DIFF_STATUS_ADDED:
Stefan Beller146fdb02017-06-29 17:07:05 -07005303 show_file_mode_name(opt, "create", p->two);
Sean4bbd2612006-05-14 08:13:49 -04005304 break;
5305 case DIFF_STATUS_COPIED:
Stefan Beller146fdb02017-06-29 17:07:05 -07005306 show_rename_copy(opt, "copy", p);
Sean4bbd2612006-05-14 08:13:49 -04005307 break;
5308 case DIFF_STATUS_RENAMED:
Stefan Beller146fdb02017-06-29 17:07:05 -07005309 show_rename_copy(opt, "rename", p);
Sean4bbd2612006-05-14 08:13:49 -04005310 break;
5311 default:
5312 if (p->score) {
Stefan Beller146fdb02017-06-29 17:07:05 -07005313 struct strbuf sb = STRBUF_INIT;
5314 strbuf_addstr(&sb, " rewrite ");
5315 quote_c_style(p->two->path, &sb, NULL, 0);
5316 strbuf_addf(&sb, " (%d%%)\n", similarity_index(p));
5317 emit_diff_symbol(opt, DIFF_SYMBOL_SUMMARY,
5318 sb.buf, sb.len, 0);
Rene Scharfefa842d82017-08-30 19:49:41 +02005319 strbuf_release(&sb);
Pierre Habouzit663af342007-09-20 00:42:15 +02005320 }
Stefan Beller146fdb02017-06-29 17:07:05 -07005321 show_mode_change(opt, p, !p->score);
Sean4bbd2612006-05-14 08:13:49 -04005322 break;
5323 }
5324}
5325
Johannes Schindelinfcb3d0a2006-06-25 03:51:08 +02005326struct patch_id_t {
Nicolas Pitre9126f002008-10-01 14:05:20 -04005327 git_SHA_CTX *ctx;
Johannes Schindelinfcb3d0a2006-06-25 03:51:08 +02005328 int patchlen;
5329};
5330
5331static int remove_space(char *line, int len)
5332{
5333 int i;
Pierre Habouzit663af342007-09-20 00:42:15 +02005334 char *dst = line;
5335 unsigned char c;
Johannes Schindelinfcb3d0a2006-06-25 03:51:08 +02005336
Pierre Habouzit663af342007-09-20 00:42:15 +02005337 for (i = 0; i < len; i++)
5338 if (!isspace((c = line[i])))
5339 *dst++ = c;
Johannes Schindelinfcb3d0a2006-06-25 03:51:08 +02005340
Pierre Habouzit663af342007-09-20 00:42:15 +02005341 return dst - line;
Johannes Schindelinfcb3d0a2006-06-25 03:51:08 +02005342}
5343
5344static void patch_id_consume(void *priv, char *line, unsigned long len)
5345{
5346 struct patch_id_t *data = priv;
5347 int new_len;
5348
5349 /* Ignore line numbers when computing the SHA1 of the patch */
Christian Couder59556542013-11-30 21:55:40 +01005350 if (starts_with(line, "@@ -"))
Johannes Schindelinfcb3d0a2006-06-25 03:51:08 +02005351 return;
5352
5353 new_len = remove_space(line, len);
5354
Nicolas Pitre9126f002008-10-01 14:05:20 -04005355 git_SHA1_Update(data->ctx, line, new_len);
Johannes Schindelinfcb3d0a2006-06-25 03:51:08 +02005356 data->patchlen += new_len;
5357}
5358
Jeff King977db6b2017-03-30 14:26:05 -04005359static void patch_id_add_string(git_SHA_CTX *ctx, const char *str)
5360{
5361 git_SHA1_Update(ctx, str, strlen(str));
5362}
5363
5364static void patch_id_add_mode(git_SHA_CTX *ctx, unsigned mode)
5365{
5366 /* large enough for 2^32 in octal */
5367 char buf[12];
5368 int len = xsnprintf(buf, sizeof(buf), "%06o", mode);
5369 git_SHA1_Update(ctx, buf, len);
5370}
5371
Johannes Schindelinfcb3d0a2006-06-25 03:51:08 +02005372/* returns 0 upon success, and writes result into sha1 */
Brandon Williamsbd25f282017-05-30 10:30:54 -07005373static int diff_get_patch_id(struct diff_options *options, struct object_id *oid, int diff_header_only)
Johannes Schindelinfcb3d0a2006-06-25 03:51:08 +02005374{
5375 struct diff_queue_struct *q = &diff_queued_diff;
5376 int i;
Nicolas Pitre9126f002008-10-01 14:05:20 -04005377 git_SHA_CTX ctx;
Johannes Schindelinfcb3d0a2006-06-25 03:51:08 +02005378 struct patch_id_t data;
Johannes Schindelinfcb3d0a2006-06-25 03:51:08 +02005379
Nicolas Pitre9126f002008-10-01 14:05:20 -04005380 git_SHA1_Init(&ctx);
Johannes Schindelinfcb3d0a2006-06-25 03:51:08 +02005381 memset(&data, 0, sizeof(struct patch_id_t));
5382 data.ctx = &ctx;
Johannes Schindelinfcb3d0a2006-06-25 03:51:08 +02005383
5384 for (i = 0; i < q->nr; i++) {
5385 xpparam_t xpp;
5386 xdemitconf_t xecfg;
Johannes Schindelinfcb3d0a2006-06-25 03:51:08 +02005387 mmfile_t mf1, mf2;
5388 struct diff_filepair *p = q->queue[i];
5389 int len1, len2;
5390
Brian Downing9ccd0a82008-10-25 15:30:37 +02005391 memset(&xpp, 0, sizeof(xpp));
Johannes Schindelin30b25012007-07-04 19:05:46 +01005392 memset(&xecfg, 0, sizeof(xecfg));
Johannes Schindelinfcb3d0a2006-06-25 03:51:08 +02005393 if (p->status == 0)
5394 return error("internal diff status error");
5395 if (p->status == DIFF_STATUS_UNKNOWN)
5396 continue;
5397 if (diff_unmodified_pair(p))
5398 continue;
5399 if ((DIFF_FILE_VALID(p->one) && S_ISDIR(p->one->mode)) ||
5400 (DIFF_FILE_VALID(p->two) && S_ISDIR(p->two->mode)))
5401 continue;
5402 if (DIFF_PAIR_UNMERGED(p))
5403 continue;
5404
Brandon Williams94e327e2017-05-30 10:31:09 -07005405 diff_fill_oid_info(p->one);
5406 diff_fill_oid_info(p->two);
Johannes Schindelinfcb3d0a2006-06-25 03:51:08 +02005407
Johannes Schindelinfcb3d0a2006-06-25 03:51:08 +02005408 len1 = remove_space(p->one->path, strlen(p->one->path));
5409 len2 = remove_space(p->two->path, strlen(p->two->path));
Jeff King977db6b2017-03-30 14:26:05 -04005410 patch_id_add_string(&ctx, "diff--git");
5411 patch_id_add_string(&ctx, "a/");
5412 git_SHA1_Update(&ctx, p->one->path, len1);
5413 patch_id_add_string(&ctx, "b/");
5414 git_SHA1_Update(&ctx, p->two->path, len2);
5415
5416 if (p->one->mode == 0) {
5417 patch_id_add_string(&ctx, "newfilemode");
5418 patch_id_add_mode(&ctx, p->two->mode);
5419 patch_id_add_string(&ctx, "---/dev/null");
5420 patch_id_add_string(&ctx, "+++b/");
5421 git_SHA1_Update(&ctx, p->two->path, len2);
5422 } else if (p->two->mode == 0) {
5423 patch_id_add_string(&ctx, "deletedfilemode");
5424 patch_id_add_mode(&ctx, p->one->mode);
5425 patch_id_add_string(&ctx, "---a/");
5426 git_SHA1_Update(&ctx, p->one->path, len1);
5427 patch_id_add_string(&ctx, "+++/dev/null");
5428 } else {
5429 patch_id_add_string(&ctx, "---a/");
5430 git_SHA1_Update(&ctx, p->one->path, len1);
5431 patch_id_add_string(&ctx, "+++b/");
5432 git_SHA1_Update(&ctx, p->two->path, len2);
5433 }
Johannes Schindelinfcb3d0a2006-06-25 03:51:08 +02005434
Kevin Willford3e8e32c2016-07-29 12:19:19 -04005435 if (diff_header_only)
5436 continue;
5437
5438 if (fill_mmfile(&mf1, p->one) < 0 ||
5439 fill_mmfile(&mf2, p->two) < 0)
5440 return error("unable to read files to diff");
5441
Clemens Buchacher34597c12010-08-15 09:20:43 +02005442 if (diff_filespec_is_binary(p->one) ||
5443 diff_filespec_is_binary(p->two)) {
brian m. carlsona0d12c42016-06-24 23:09:23 +00005444 git_SHA1_Update(&ctx, oid_to_hex(&p->one->oid),
Brandon Williamsbd25f282017-05-30 10:30:54 -07005445 GIT_SHA1_HEXSZ);
brian m. carlsona0d12c42016-06-24 23:09:23 +00005446 git_SHA1_Update(&ctx, oid_to_hex(&p->two->oid),
Brandon Williamsbd25f282017-05-30 10:30:54 -07005447 GIT_SHA1_HEXSZ);
Clemens Buchacher34597c12010-08-15 09:20:43 +02005448 continue;
5449 }
5450
René Scharfe582aa002010-05-02 15:04:41 +02005451 xpp.flags = 0;
Johannes Schindelinfcb3d0a2006-06-25 03:51:08 +02005452 xecfg.ctxlen = 3;
Clemens Buchacherad14b452010-09-19 11:59:28 +02005453 xecfg.flags = 0;
Jeff King3efb9882015-09-24 19:12:23 -04005454 if (xdi_diff_outf(&mf1, &mf2, patch_id_consume, &data,
5455 &xpp, &xecfg))
5456 return error("unable to generate patch-id diff for %s",
5457 p->one->path);
Johannes Schindelinfcb3d0a2006-06-25 03:51:08 +02005458 }
5459
Brandon Williamsbd25f282017-05-30 10:30:54 -07005460 git_SHA1_Final(oid->hash, &ctx);
Johannes Schindelinfcb3d0a2006-06-25 03:51:08 +02005461 return 0;
5462}
5463
Brandon Williamsbd25f282017-05-30 10:30:54 -07005464int diff_flush_patch_id(struct diff_options *options, struct object_id *oid, int diff_header_only)
Johannes Schindelinfcb3d0a2006-06-25 03:51:08 +02005465{
5466 struct diff_queue_struct *q = &diff_queued_diff;
5467 int i;
Brandon Williamsbd25f282017-05-30 10:30:54 -07005468 int result = diff_get_patch_id(options, oid, diff_header_only);
Johannes Schindelinfcb3d0a2006-06-25 03:51:08 +02005469
5470 for (i = 0; i < q->nr; i++)
5471 diff_free_filepair(q->queue[i]);
5472
5473 free(q->queue);
Bo Yang9ca5df92010-05-06 21:52:27 -07005474 DIFF_QUEUE_CLEAR(q);
Johannes Schindelinfcb3d0a2006-06-25 03:51:08 +02005475
5476 return result;
5477}
5478
Timo Hirvonen946c3782006-06-27 15:09:17 +03005479static int is_summary_empty(const struct diff_queue_struct *q)
5480{
5481 int i;
5482
5483 for (i = 0; i < q->nr; i++) {
5484 const struct diff_filepair *p = q->queue[i];
5485
5486 switch (p->status) {
5487 case DIFF_STATUS_DELETED:
5488 case DIFF_STATUS_ADDED:
5489 case DIFF_STATUS_COPIED:
5490 case DIFF_STATUS_RENAMED:
5491 return 0;
5492 default:
5493 if (p->score)
5494 return 0;
5495 if (p->one->mode && p->two->mode &&
5496 p->one->mode != p->two->mode)
5497 return 0;
5498 break;
5499 }
5500 }
5501 return 1;
5502}
5503
Junio C Hamanof31027c2011-01-06 13:50:06 -08005504static const char rename_limit_warning[] =
Vasco Almeidadb424972016-10-17 13:15:29 +00005505N_("inexact rename detection was skipped due to too many files.");
Junio C Hamanof31027c2011-01-06 13:50:06 -08005506
5507static const char degrade_cc_to_c_warning[] =
Vasco Almeidadb424972016-10-17 13:15:29 +00005508N_("only found copies from modified paths due to too many files.");
Junio C Hamanof31027c2011-01-06 13:50:06 -08005509
5510static const char rename_limit_advice[] =
Vasco Almeidadb424972016-10-17 13:15:29 +00005511N_("you may want to set your %s variable to at least "
5512 "%d and retry the command.");
Junio C Hamanof31027c2011-01-06 13:50:06 -08005513
5514void diff_warn_rename_limit(const char *varname, int needed, int degraded_cc)
5515{
5516 if (degraded_cc)
Vasco Almeidadb424972016-10-17 13:15:29 +00005517 warning(_(degrade_cc_to_c_warning));
Junio C Hamanof31027c2011-01-06 13:50:06 -08005518 else if (needed)
Vasco Almeidadb424972016-10-17 13:15:29 +00005519 warning(_(rename_limit_warning));
Junio C Hamanof31027c2011-01-06 13:50:06 -08005520 else
5521 return;
5522 if (0 < needed && needed < 32767)
Vasco Almeidadb424972016-10-17 13:15:29 +00005523 warning(_(rename_limit_advice), varname, needed);
Junio C Hamanof31027c2011-01-06 13:50:06 -08005524}
5525
Stefan Bellerec331502017-06-29 17:06:48 -07005526static void diff_flush_patch_all_file_pairs(struct diff_options *o)
5527{
5528 int i;
Stefan Bellere6e045f2017-06-29 17:07:06 -07005529 static struct emitted_diff_symbols esm = EMITTED_DIFF_SYMBOLS_INIT;
Stefan Bellerec331502017-06-29 17:06:48 -07005530 struct diff_queue_struct *q = &diff_queued_diff;
Stefan Beller091f8e22017-06-29 17:06:53 -07005531
5532 if (WSEH_NEW & WS_RULE_MASK)
5533 die("BUG: WS rules bit mask overlaps with diff symbol flags");
5534
Stefan Beller2e2d5ac2017-06-30 13:53:07 -07005535 if (o->color_moved)
5536 o->emitted_symbols = &esm;
Stefan Bellere6e045f2017-06-29 17:07:06 -07005537
Stefan Bellerec331502017-06-29 17:06:48 -07005538 for (i = 0; i < q->nr; i++) {
5539 struct diff_filepair *p = q->queue[i];
5540 if (check_pair_status(p))
5541 diff_flush_patch(p, o);
5542 }
Stefan Bellere6e045f2017-06-29 17:07:06 -07005543
5544 if (o->emitted_symbols) {
Stefan Beller2e2d5ac2017-06-30 13:53:07 -07005545 if (o->color_moved) {
5546 struct hashmap add_lines, del_lines;
5547
5548 hashmap_init(&del_lines,
5549 (hashmap_cmp_fn)moved_entry_cmp, o, 0);
5550 hashmap_init(&add_lines,
5551 (hashmap_cmp_fn)moved_entry_cmp, o, 0);
5552
5553 add_lines_to_move_detection(o, &add_lines, &del_lines);
5554 mark_color_as_moved(o, &add_lines, &del_lines);
Stefan Beller86b452e2017-06-30 13:53:09 -07005555 if (o->color_moved == COLOR_MOVED_ZEBRA_DIM)
5556 dim_moved_lines(o);
Stefan Beller2e2d5ac2017-06-30 13:53:07 -07005557
5558 hashmap_free(&add_lines, 0);
5559 hashmap_free(&del_lines, 0);
5560 }
5561
Stefan Bellere6e045f2017-06-29 17:07:06 -07005562 for (i = 0; i < esm.nr; i++)
5563 emit_diff_symbol_from_struct(o, &esm.buf[i]);
5564
5565 for (i = 0; i < esm.nr; i++)
5566 free((void *)esm.buf[i].line);
5567 }
5568 esm.nr = 0;
Stefan Bellerec331502017-06-29 17:06:48 -07005569}
5570
Junio C Hamano6973dca2006-04-21 23:57:45 -07005571void diff_flush(struct diff_options *options)
5572{
5573 struct diff_queue_struct *q = &diff_queued_diff;
Timo Hirvonenc6744342006-06-24 20:21:53 +03005574 int i, output_format = options->output_format;
Timo Hirvonen946c3782006-06-27 15:09:17 +03005575 int separator = 0;
Johan Herland1c57a622011-04-29 11:36:21 +02005576 int dirstat_by_line = 0;
Junio C Hamano6973dca2006-04-21 23:57:45 -07005577
Timo Hirvonenc6744342006-06-24 20:21:53 +03005578 /*
5579 * Order: raw, stat, summary, patch
5580 * or: name/name-status/checkdiff (other bits clear)
5581 */
Timo Hirvonen946c3782006-06-27 15:09:17 +03005582 if (!q->nr)
5583 goto free_queue;
Junio C Hamano6973dca2006-04-21 23:57:45 -07005584
Timo Hirvonenc6744342006-06-24 20:21:53 +03005585 if (output_format & (DIFF_FORMAT_RAW |
5586 DIFF_FORMAT_NAME |
5587 DIFF_FORMAT_NAME_STATUS |
5588 DIFF_FORMAT_CHECKDIFF)) {
Junio C Hamano6973dca2006-04-21 23:57:45 -07005589 for (i = 0; i < q->nr; i++) {
5590 struct diff_filepair *p = q->queue[i];
Timo Hirvonenc6744342006-06-24 20:21:53 +03005591 if (check_pair_status(p))
5592 flush_one_pair(p, options);
Junio C Hamano6973dca2006-04-21 23:57:45 -07005593 }
Timo Hirvonen946c3782006-06-27 15:09:17 +03005594 separator++;
Junio C Hamano6973dca2006-04-21 23:57:45 -07005595 }
Timo Hirvonenc6744342006-06-24 20:21:53 +03005596
Johan Herland1c57a622011-04-29 11:36:21 +02005597 if (output_format & DIFF_FORMAT_DIRSTAT && DIFF_OPT_TST(options, DIRSTAT_BY_LINE))
5598 dirstat_by_line = 1;
5599
5600 if (output_format & (DIFF_FORMAT_DIFFSTAT|DIFF_FORMAT_SHORTSTAT|DIFF_FORMAT_NUMSTAT) ||
5601 dirstat_by_line) {
Timo Hirvonen5e2b0632006-06-25 14:28:19 +03005602 struct diffstat_t diffstat;
Timo Hirvonenc6744342006-06-24 20:21:53 +03005603
Timo Hirvonen5e2b0632006-06-25 14:28:19 +03005604 memset(&diffstat, 0, sizeof(struct diffstat_t));
Junio C Hamano6973dca2006-04-21 23:57:45 -07005605 for (i = 0; i < q->nr; i++) {
5606 struct diff_filepair *p = q->queue[i];
Timo Hirvonenc6744342006-06-24 20:21:53 +03005607 if (check_pair_status(p))
Timo Hirvonen5e2b0632006-06-25 14:28:19 +03005608 diff_flush_stat(p, options, &diffstat);
Junio C Hamano6973dca2006-04-21 23:57:45 -07005609 }
Junio C Hamano74e2abe2006-10-12 03:01:00 -07005610 if (output_format & DIFF_FORMAT_NUMSTAT)
5611 show_numstat(&diffstat, options);
5612 if (output_format & DIFF_FORMAT_DIFFSTAT)
5613 show_stats(&diffstat, options);
Junio C Hamanof6046522007-12-11 23:46:30 -08005614 if (output_format & DIFF_FORMAT_SHORTSTAT)
Daniel Barkalowc0c77732008-03-09 22:43:39 -04005615 show_shortstats(&diffstat, options);
Mårten Kongstadab273892015-03-02 16:05:39 +01005616 if (output_format & DIFF_FORMAT_DIRSTAT && dirstat_by_line)
Johan Herland1c57a622011-04-29 11:36:21 +02005617 show_dirstat_by_line(&diffstat, options);
Junio C Hamanof6046522007-12-11 23:46:30 -08005618 free_diffstat_info(&diffstat);
Junio C Hamano3969cf72006-06-27 15:08:19 -07005619 separator++;
Junio C Hamano6973dca2006-04-21 23:57:45 -07005620 }
Johan Herland1c57a622011-04-29 11:36:21 +02005621 if ((output_format & DIFF_FORMAT_DIRSTAT) && !dirstat_by_line)
Junio C Hamanoc04a7152008-02-12 17:06:58 -08005622 show_dirstat(options);
Junio C Hamano6973dca2006-04-21 23:57:45 -07005623
Timo Hirvonen946c3782006-06-27 15:09:17 +03005624 if (output_format & DIFF_FORMAT_SUMMARY && !is_summary_empty(q)) {
Bo Yang7be57612010-05-26 15:23:54 +08005625 for (i = 0; i < q->nr; i++) {
5626 diff_summary(options, q->queue[i]);
5627 }
Junio C Hamano3969cf72006-06-27 15:08:19 -07005628 separator++;
Sean4bbd2612006-05-14 08:13:49 -04005629 }
5630
Larry D'Anna6977c252010-02-16 01:55:21 -05005631 if (output_format & DIFF_FORMAT_NO_OUTPUT &&
5632 DIFF_OPT_TST(options, EXIT_WITH_STATUS) &&
5633 DIFF_OPT_TST(options, DIFF_FROM_CONTENTS)) {
5634 /*
5635 * run diff_flush_patch for the exit status. setting
Ondřej Bílka749f7632013-07-22 23:02:23 +02005636 * options->file to /dev/null should be safe, because we
Larry D'Anna6977c252010-02-16 01:55:21 -05005637 * aren't supposed to produce any output anyway.
5638 */
5639 if (options->close_file)
5640 fclose(options->file);
Nguyễn Thái Ngọc Duy23a9e072017-05-03 17:16:46 +07005641 options->file = xfopen("/dev/null", "w");
Larry D'Anna6977c252010-02-16 01:55:21 -05005642 options->close_file = 1;
Stefan Beller2e2d5ac2017-06-30 13:53:07 -07005643 options->color_moved = 0;
Larry D'Anna6977c252010-02-16 01:55:21 -05005644 for (i = 0; i < q->nr; i++) {
5645 struct diff_filepair *p = q->queue[i];
5646 if (check_pair_status(p))
5647 diff_flush_patch(p, options);
5648 if (options->found_changes)
5649 break;
5650 }
5651 }
5652
Timo Hirvonenc6744342006-06-24 20:21:53 +03005653 if (output_format & DIFF_FORMAT_PATCH) {
Timo Hirvonen946c3782006-06-27 15:09:17 +03005654 if (separator) {
Stefan Beller091f8e22017-06-29 17:06:53 -07005655 emit_diff_symbol(options, DIFF_SYMBOL_SEPARATOR, NULL, 0, 0);
Stefan Beller30b7e1e2017-06-29 17:07:04 -07005656 if (options->stat_sep)
Timo Hirvonen946c3782006-06-27 15:09:17 +03005657 /* attach patch instead of inline */
Stefan Beller30b7e1e2017-06-29 17:07:04 -07005658 emit_diff_symbol(options, DIFF_SYMBOL_STAT_SEP,
5659 NULL, 0, 0);
Timo Hirvonenc6744342006-06-24 20:21:53 +03005660 }
5661
Stefan Bellerec331502017-06-29 17:06:48 -07005662 diff_flush_patch_all_file_pairs(options);
Timo Hirvonenc6744342006-06-24 20:21:53 +03005663 }
5664
Jeff King04245582006-09-07 02:35:42 -04005665 if (output_format & DIFF_FORMAT_CALLBACK)
5666 options->format_callback(q, options, options->format_callback_data);
5667
Timo Hirvonenc6744342006-06-24 20:21:53 +03005668 for (i = 0; i < q->nr; i++)
5669 diff_free_filepair(q->queue[i]);
Timo Hirvonen946c3782006-06-27 15:09:17 +03005670free_queue:
Junio C Hamano6973dca2006-04-21 23:57:45 -07005671 free(q->queue);
Bo Yang9ca5df92010-05-06 21:52:27 -07005672 DIFF_QUEUE_CLEAR(q);
Daniel Barkalowc0c77732008-03-09 22:43:39 -04005673 if (options->close_file)
5674 fclose(options->file);
Junio C Hamanof2451942009-05-22 12:45:29 -07005675
5676 /*
Jim Meyering97bf2a02009-08-30 22:27:02 +02005677 * Report the content-level differences with HAS_CHANGES;
Junio C Hamanof2451942009-05-22 12:45:29 -07005678 * diff_addremove/diff_change does not set the bit when
5679 * DIFF_FROM_CONTENTS is in effect (e.g. with -w).
5680 */
5681 if (DIFF_OPT_TST(options, DIFF_FROM_CONTENTS)) {
5682 if (options->found_changes)
5683 DIFF_OPT_SET(options, HAS_CHANGES);
5684 else
5685 DIFF_OPT_CLR(options, HAS_CHANGES);
5686 }
Junio C Hamano6973dca2006-04-21 23:57:45 -07005687}
5688
Junio C Hamano08578fa2013-07-17 15:09:34 -07005689static int match_filter(const struct diff_options *options, const struct diff_filepair *p)
5690{
5691 return (((p->status == DIFF_STATUS_MODIFIED) &&
5692 ((p->score &&
Junio C Hamano1ecc1cb2013-07-17 15:05:46 -07005693 filter_bit_tst(DIFF_STATUS_FILTER_BROKEN, options)) ||
Junio C Hamano08578fa2013-07-17 15:09:34 -07005694 (!p->score &&
Junio C Hamano1ecc1cb2013-07-17 15:05:46 -07005695 filter_bit_tst(DIFF_STATUS_MODIFIED, options)))) ||
Junio C Hamano08578fa2013-07-17 15:09:34 -07005696 ((p->status != DIFF_STATUS_MODIFIED) &&
Junio C Hamano1ecc1cb2013-07-17 15:05:46 -07005697 filter_bit_tst(p->status, options)));
Junio C Hamano08578fa2013-07-17 15:09:34 -07005698}
5699
Junio C Hamano949226f2013-07-17 14:19:24 -07005700static void diffcore_apply_filter(struct diff_options *options)
Junio C Hamano6973dca2006-04-21 23:57:45 -07005701{
5702 int i;
5703 struct diff_queue_struct *q = &diff_queued_diff;
5704 struct diff_queue_struct outq;
Junio C Hamano949226f2013-07-17 14:19:24 -07005705
Bo Yang9ca5df92010-05-06 21:52:27 -07005706 DIFF_QUEUE_CLEAR(&outq);
Junio C Hamano6973dca2006-04-21 23:57:45 -07005707
Junio C Hamano1ecc1cb2013-07-17 15:05:46 -07005708 if (!options->filter)
Junio C Hamano6973dca2006-04-21 23:57:45 -07005709 return;
5710
Junio C Hamano1ecc1cb2013-07-17 15:05:46 -07005711 if (filter_bit_tst(DIFF_STATUS_FILTER_AON, options)) {
Junio C Hamano6973dca2006-04-21 23:57:45 -07005712 int found;
5713 for (i = found = 0; !found && i < q->nr; i++) {
Junio C Hamano08578fa2013-07-17 15:09:34 -07005714 if (match_filter(options, q->queue[i]))
Junio C Hamano6973dca2006-04-21 23:57:45 -07005715 found++;
5716 }
5717 if (found)
5718 return;
5719
5720 /* otherwise we will clear the whole queue
5721 * by copying the empty outq at the end of this
5722 * function, but first clear the current entries
5723 * in the queue.
5724 */
5725 for (i = 0; i < q->nr; i++)
5726 diff_free_filepair(q->queue[i]);
5727 }
5728 else {
5729 /* Only the matching ones */
5730 for (i = 0; i < q->nr; i++) {
5731 struct diff_filepair *p = q->queue[i];
Junio C Hamano08578fa2013-07-17 15:09:34 -07005732 if (match_filter(options, p))
Junio C Hamano6973dca2006-04-21 23:57:45 -07005733 diff_q(&outq, p);
5734 else
5735 diff_free_filepair(p);
5736 }
5737 }
5738 free(q->queue);
5739 *q = outq;
5740}
5741
Sven Verdoolaege57011152007-09-08 12:30:22 +02005742/* Check whether two filespecs with the same mode and size are identical */
5743static int diff_filespec_is_identical(struct diff_filespec *one,
5744 struct diff_filespec *two)
5745{
Junio C Hamano2b459b42008-03-02 00:07:59 -08005746 if (S_ISGITLINK(one->mode))
5747 return 0;
Sven Verdoolaege57011152007-09-08 12:30:22 +02005748 if (diff_populate_filespec(one, 0))
5749 return 0;
5750 if (diff_populate_filespec(two, 0))
5751 return 0;
5752 return !memcmp(one->data, two->data, one->size);
5753}
5754
Nguyễn Thái Ngọc Duyfceb9072014-01-25 13:46:49 +07005755static int diff_filespec_check_stat_unmatch(struct diff_filepair *p)
5756{
Nguyễn Thái Ngọc Duyf34b2052014-01-25 13:46:50 +07005757 if (p->done_skip_stat_unmatch)
5758 return p->skip_stat_unmatch_result;
5759
5760 p->done_skip_stat_unmatch = 1;
5761 p->skip_stat_unmatch_result = 0;
Nguyễn Thái Ngọc Duyfceb9072014-01-25 13:46:49 +07005762 /*
5763 * 1. Entries that come from stat info dirtiness
5764 * always have both sides (iow, not create/delete),
5765 * one side of the object name is unknown, with
5766 * the same mode and size. Keep the ones that
5767 * do not match these criteria. They have real
5768 * differences.
5769 *
5770 * 2. At this point, the file is known to be modified,
5771 * with the same mode and size, and the object
5772 * name of one side is unknown. Need to inspect
5773 * the identical contents.
5774 */
5775 if (!DIFF_FILE_VALID(p->one) || /* (1) */
5776 !DIFF_FILE_VALID(p->two) ||
brian m. carlson41c95602016-06-24 23:09:24 +00005777 (p->one->oid_valid && p->two->oid_valid) ||
Nguyễn Thái Ngọc Duyfceb9072014-01-25 13:46:49 +07005778 (p->one->mode != p->two->mode) ||
Nguyễn Thái Ngọc Duy8e5dd3d2014-08-16 10:08:04 +07005779 diff_populate_filespec(p->one, CHECK_SIZE_ONLY) ||
5780 diff_populate_filespec(p->two, CHECK_SIZE_ONLY) ||
Nguyễn Thái Ngọc Duyfceb9072014-01-25 13:46:49 +07005781 (p->one->size != p->two->size) ||
5782 !diff_filespec_is_identical(p->one, p->two)) /* (2) */
Nguyễn Thái Ngọc Duyf34b2052014-01-25 13:46:50 +07005783 p->skip_stat_unmatch_result = 1;
5784 return p->skip_stat_unmatch_result;
Nguyễn Thái Ngọc Duyfceb9072014-01-25 13:46:49 +07005785}
5786
Junio C Hamanofb132272007-08-03 13:33:31 -07005787static void diffcore_skip_stat_unmatch(struct diff_options *diffopt)
5788{
5789 int i;
5790 struct diff_queue_struct *q = &diff_queued_diff;
5791 struct diff_queue_struct outq;
Bo Yang9ca5df92010-05-06 21:52:27 -07005792 DIFF_QUEUE_CLEAR(&outq);
Junio C Hamanofb132272007-08-03 13:33:31 -07005793
5794 for (i = 0; i < q->nr; i++) {
5795 struct diff_filepair *p = q->queue[i];
5796
Nguyễn Thái Ngọc Duyfceb9072014-01-25 13:46:49 +07005797 if (diff_filespec_check_stat_unmatch(p))
Junio C Hamanofb132272007-08-03 13:33:31 -07005798 diff_q(&outq, p);
5799 else {
5800 /*
5801 * The caller can subtract 1 from skip_stat_unmatch
5802 * to determine how many paths were dirty only
5803 * due to stat info mismatch.
5804 */
Pierre Habouzit8f67f8a2007-11-10 20:05:14 +01005805 if (!DIFF_OPT_TST(diffopt, NO_INDEX))
René Scharfe6d2d9e82007-08-15 00:41:00 +02005806 diffopt->skip_stat_unmatch++;
Junio C Hamanofb132272007-08-03 13:33:31 -07005807 diff_free_filepair(p);
5808 }
5809 }
5810 free(q->queue);
5811 *q = outq;
5812}
5813
Junio C Hamano730f7282009-09-20 00:03:39 -07005814static int diffnamecmp(const void *a_, const void *b_)
5815{
5816 const struct diff_filepair *a = *((const struct diff_filepair **)a_);
5817 const struct diff_filepair *b = *((const struct diff_filepair **)b_);
5818 const char *name_a, *name_b;
5819
5820 name_a = a->one ? a->one->path : a->two->path;
5821 name_b = b->one ? b->one->path : b->two->path;
5822 return strcmp(name_a, name_b);
5823}
5824
5825void diffcore_fix_diff_index(struct diff_options *options)
5826{
5827 struct diff_queue_struct *q = &diff_queued_diff;
René Scharfe9ed0d8d2016-09-29 17:27:31 +02005828 QSORT(q->queue, q->nr, diffnamecmp);
Junio C Hamano730f7282009-09-20 00:03:39 -07005829}
5830
Junio C Hamano6973dca2006-04-21 23:57:45 -07005831void diffcore_std(struct diff_options *options)
5832{
Kirill Smelkov7195fbf2014-02-24 20:21:51 +04005833 /* NOTE please keep the following in sync with diff_tree_combined() */
Junio C Hamano9d865352008-09-06 19:09:16 -07005834 if (options->skip_stat_unmatch)
Junio C Hamanofb132272007-08-03 13:33:31 -07005835 diffcore_skip_stat_unmatch(options);
Junio C Hamano44c48a92010-08-13 12:17:45 -07005836 if (!options->found_follow) {
5837 /* See try_to_follow_renames() in tree-diff.c */
5838 if (options->break_opt != -1)
5839 diffcore_break(options->break_opt);
5840 if (options->detect_rename)
5841 diffcore_rename(options);
5842 if (options->break_opt != -1)
5843 diffcore_merge_broken();
5844 }
Junio C Hamano6973dca2006-04-21 23:57:45 -07005845 if (options->pickaxe)
Junio C Hamano382f0132010-08-31 13:44:39 -07005846 diffcore_pickaxe(options);
Junio C Hamano6973dca2006-04-21 23:57:45 -07005847 if (options->orderfile)
5848 diffcore_order(options->orderfile);
Junio C Hamano44c48a92010-08-13 12:17:45 -07005849 if (!options->found_follow)
5850 /* See try_to_follow_renames() in tree-diff.c */
5851 diff_resolve_rename_copy();
Junio C Hamano949226f2013-07-17 14:19:24 -07005852 diffcore_apply_filter(options);
Junio C Hamano68aacb22007-03-14 11:12:13 -07005853
Junio C Hamanof2451942009-05-22 12:45:29 -07005854 if (diff_queued_diff.nr && !DIFF_OPT_TST(options, DIFF_FROM_CONTENTS))
Pierre Habouzit8f67f8a2007-11-10 20:05:14 +01005855 DIFF_OPT_SET(options, HAS_CHANGES);
5856 else
5857 DIFF_OPT_CLR(options, HAS_CHANGES);
Bo Yang1da61752010-05-06 21:52:28 -07005858
Junio C Hamano44c48a92010-08-13 12:17:45 -07005859 options->found_follow = 0;
Junio C Hamano6973dca2006-04-21 23:57:45 -07005860}
5861
Junio C Hamanoda31b352007-12-13 23:40:27 -08005862int diff_result_code(struct diff_options *opt, int status)
5863{
5864 int result = 0;
Junio C Hamanof31027c2011-01-06 13:50:06 -08005865
Max Nanasyc9fc4412013-03-21 12:53:38 -07005866 diff_warn_rename_limit("diff.renameLimit",
Junio C Hamanof31027c2011-01-06 13:50:06 -08005867 opt->needed_rename_limit,
5868 opt->degraded_cc_to_c);
Junio C Hamanoda31b352007-12-13 23:40:27 -08005869 if (!DIFF_OPT_TST(opt, EXIT_WITH_STATUS) &&
5870 !(opt->output_format & DIFF_FORMAT_CHECKDIFF))
5871 return status;
5872 if (DIFF_OPT_TST(opt, EXIT_WITH_STATUS) &&
5873 DIFF_OPT_TST(opt, HAS_CHANGES))
5874 result |= 01;
5875 if ((opt->output_format & DIFF_FORMAT_CHECKDIFF) &&
5876 DIFF_OPT_TST(opt, CHECK_FAILED))
5877 result |= 02;
5878 return result;
5879}
Junio C Hamano6973dca2006-04-21 23:57:45 -07005880
Junio C Hamano28b92642011-05-31 09:14:17 -07005881int diff_can_quit_early(struct diff_options *opt)
5882{
5883 return (DIFF_OPT_TST(opt, QUICK) &&
5884 !opt->filter &&
5885 DIFF_OPT_TST(opt, HAS_CHANGES));
5886}
5887
Jens Lehmannaee9c7d2010-08-06 00:39:25 +02005888/*
5889 * Shall changes to this submodule be ignored?
5890 *
5891 * Submodule changes can be configured to be ignored separately for each path,
5892 * but that configuration can be overridden from the command line.
5893 */
5894static int is_submodule_ignored(const char *path, struct diff_options *options)
5895{
5896 int ignored = 0;
5897 unsigned orig_flags = options->flags;
5898 if (!DIFF_OPT_TST(options, OVERRIDE_SUBMODULE_CONFIG))
5899 set_diffopt_flags_from_submodule_config(options, path);
5900 if (DIFF_OPT_TST(options, IGNORE_SUBMODULES))
5901 ignored = 1;
5902 options->flags = orig_flags;
5903 return ignored;
5904}
5905
Junio C Hamano6973dca2006-04-21 23:57:45 -07005906void diff_addremove(struct diff_options *options,
5907 int addremove, unsigned mode,
Brandon Williamsc26022e2017-05-30 10:30:47 -07005908 const struct object_id *oid,
5909 int oid_valid,
Jens Lehmanne3d42c42010-01-18 21:26:18 +01005910 const char *concatpath, unsigned dirty_submodule)
Junio C Hamano6973dca2006-04-21 23:57:45 -07005911{
Junio C Hamano6973dca2006-04-21 23:57:45 -07005912 struct diff_filespec *one, *two;
5913
Jens Lehmannaee9c7d2010-08-06 00:39:25 +02005914 if (S_ISGITLINK(mode) && is_submodule_ignored(concatpath, options))
Johannes Schindelin50fd9bd2008-05-14 18:03:31 +01005915 return;
5916
Junio C Hamano6973dca2006-04-21 23:57:45 -07005917 /* This may look odd, but it is a preparation for
5918 * feeding "there are unchanged files which should
5919 * not produce diffs, but when you are doing copy
5920 * detection you would need them, so here they are"
5921 * entries to the diff-core. They will be prefixed
5922 * with something like '=' or '*' (I haven't decided
5923 * which but should not make any difference).
Junio C Hamanoa6080a02007-06-07 00:04:01 -07005924 * Feeding the same new and old to diff_change()
Junio C Hamano6973dca2006-04-21 23:57:45 -07005925 * also has the same effect.
5926 * Before the final output happens, they are pruned after
5927 * merged into rename/copy pairs as appropriate.
5928 */
Pierre Habouzit8f67f8a2007-11-10 20:05:14 +01005929 if (DIFF_OPT_TST(options, REVERSE_DIFF))
Junio C Hamano6973dca2006-04-21 23:57:45 -07005930 addremove = (addremove == '+' ? '-' :
5931 addremove == '-' ? '+' : addremove);
5932
Junio C Hamanocd676a52008-02-12 14:26:02 -08005933 if (options->prefix &&
5934 strncmp(concatpath, options->prefix, options->prefix_length))
5935 return;
5936
Junio C Hamano6973dca2006-04-21 23:57:45 -07005937 one = alloc_filespec(concatpath);
5938 two = alloc_filespec(concatpath);
5939
5940 if (addremove != '+')
Brandon Williamsf9704c22017-05-30 10:30:50 -07005941 fill_filespec(one, oid, oid_valid, mode);
Jens Lehmanne3d42c42010-01-18 21:26:18 +01005942 if (addremove != '-') {
Brandon Williamsf9704c22017-05-30 10:30:50 -07005943 fill_filespec(two, oid, oid_valid, mode);
Jens Lehmanne3d42c42010-01-18 21:26:18 +01005944 two->dirty_submodule = dirty_submodule;
5945 }
Junio C Hamano6973dca2006-04-21 23:57:45 -07005946
5947 diff_queue(&diff_queued_diff, one, two);
Junio C Hamanof2451942009-05-22 12:45:29 -07005948 if (!DIFF_OPT_TST(options, DIFF_FROM_CONTENTS))
5949 DIFF_OPT_SET(options, HAS_CHANGES);
Junio C Hamano6973dca2006-04-21 23:57:45 -07005950}
5951
5952void diff_change(struct diff_options *options,
5953 unsigned old_mode, unsigned new_mode,
Brandon Williams94a00972017-05-30 10:30:49 -07005954 const struct object_id *old_oid,
5955 const struct object_id *new_oid,
5956 int old_oid_valid, int new_oid_valid,
Jens Lehmanne3d42c42010-01-18 21:26:18 +01005957 const char *concatpath,
5958 unsigned old_dirty_submodule, unsigned new_dirty_submodule)
Junio C Hamano6973dca2006-04-21 23:57:45 -07005959{
Junio C Hamano6973dca2006-04-21 23:57:45 -07005960 struct diff_filespec *one, *two;
Nguyễn Thái Ngọc Duyf34b2052014-01-25 13:46:50 +07005961 struct diff_filepair *p;
Junio C Hamano6973dca2006-04-21 23:57:45 -07005962
Jens Lehmannaee9c7d2010-08-06 00:39:25 +02005963 if (S_ISGITLINK(old_mode) && S_ISGITLINK(new_mode) &&
5964 is_submodule_ignored(concatpath, options))
Johannes Schindelin50fd9bd2008-05-14 18:03:31 +01005965 return;
5966
Pierre Habouzit8f67f8a2007-11-10 20:05:14 +01005967 if (DIFF_OPT_TST(options, REVERSE_DIFF)) {
René Scharfe35d803b2017-01-28 22:40:58 +01005968 SWAP(old_mode, new_mode);
Brandon Williams94a00972017-05-30 10:30:49 -07005969 SWAP(old_oid, new_oid);
5970 SWAP(old_oid_valid, new_oid_valid);
René Scharfe35d803b2017-01-28 22:40:58 +01005971 SWAP(old_dirty_submodule, new_dirty_submodule);
Junio C Hamano6973dca2006-04-21 23:57:45 -07005972 }
Junio C Hamanocd676a52008-02-12 14:26:02 -08005973
5974 if (options->prefix &&
5975 strncmp(concatpath, options->prefix, options->prefix_length))
5976 return;
5977
Junio C Hamano6973dca2006-04-21 23:57:45 -07005978 one = alloc_filespec(concatpath);
5979 two = alloc_filespec(concatpath);
Brandon Williamsf9704c22017-05-30 10:30:50 -07005980 fill_filespec(one, old_oid, old_oid_valid, old_mode);
5981 fill_filespec(two, new_oid, new_oid_valid, new_mode);
Jens Lehmanne3d42c42010-01-18 21:26:18 +01005982 one->dirty_submodule = old_dirty_submodule;
5983 two->dirty_submodule = new_dirty_submodule;
Nguyễn Thái Ngọc Duyf34b2052014-01-25 13:46:50 +07005984 p = diff_queue(&diff_queued_diff, one, two);
Junio C Hamano6973dca2006-04-21 23:57:45 -07005985
Nguyễn Thái Ngọc Duyf34b2052014-01-25 13:46:50 +07005986 if (DIFF_OPT_TST(options, DIFF_FROM_CONTENTS))
5987 return;
5988
5989 if (DIFF_OPT_TST(options, QUICK) && options->skip_stat_unmatch &&
5990 !diff_filespec_check_stat_unmatch(p))
5991 return;
5992
5993 DIFF_OPT_SET(options, HAS_CHANGES);
Junio C Hamano6973dca2006-04-21 23:57:45 -07005994}
5995
Junio C Hamanofa7b2902011-04-22 16:05:58 -07005996struct diff_filepair *diff_unmerge(struct diff_options *options, const char *path)
Junio C Hamano6973dca2006-04-21 23:57:45 -07005997{
Junio C Hamano76399c02011-04-22 15:55:55 -07005998 struct diff_filepair *pair;
Junio C Hamano6973dca2006-04-21 23:57:45 -07005999 struct diff_filespec *one, *two;
Junio C Hamanocd676a52008-02-12 14:26:02 -08006000
6001 if (options->prefix &&
6002 strncmp(path, options->prefix, options->prefix_length))
Junio C Hamano76399c02011-04-22 15:55:55 -07006003 return NULL;
Junio C Hamanocd676a52008-02-12 14:26:02 -08006004
Junio C Hamano6973dca2006-04-21 23:57:45 -07006005 one = alloc_filespec(path);
6006 two = alloc_filespec(path);
Junio C Hamano76399c02011-04-22 15:55:55 -07006007 pair = diff_queue(&diff_queued_diff, one, two);
6008 pair->is_unmerged = 1;
6009 return pair;
Junio C Hamano6973dca2006-04-21 23:57:45 -07006010}
Jeff King9cb92c32008-10-05 17:43:45 -04006011
6012static char *run_textconv(const char *pgm, struct diff_filespec *spec,
6013 size_t *outsize)
6014{
Jeff King479b0ae2009-01-22 00:59:56 -05006015 struct diff_tempfile *temp;
Jeff King9cb92c32008-10-05 17:43:45 -04006016 const char *argv[3];
6017 const char **arg = argv;
René Scharfed3180272014-08-19 21:09:35 +02006018 struct child_process child = CHILD_PROCESS_INIT;
Jeff King9cb92c32008-10-05 17:43:45 -04006019 struct strbuf buf = STRBUF_INIT;
Johannes Sixtda1fbed2010-03-30 19:36:03 +02006020 int err = 0;
Jeff King9cb92c32008-10-05 17:43:45 -04006021
Jeff King479b0ae2009-01-22 00:59:56 -05006022 temp = prepare_temp_file(spec->path, spec);
Jeff King9cb92c32008-10-05 17:43:45 -04006023 *arg++ = pgm;
Jeff King479b0ae2009-01-22 00:59:56 -05006024 *arg++ = temp->name;
Jeff King9cb92c32008-10-05 17:43:45 -04006025 *arg = NULL;
6026
Jeff King41a457e2009-12-30 06:01:09 -05006027 child.use_shell = 1;
Jeff King9cb92c32008-10-05 17:43:45 -04006028 child.argv = argv;
6029 child.out = -1;
Johannes Sixtda1fbed2010-03-30 19:36:03 +02006030 if (start_command(&child)) {
Jeff King479b0ae2009-01-22 00:59:56 -05006031 remove_tempfile();
Jeff King9cb92c32008-10-05 17:43:45 -04006032 return NULL;
6033 }
Johannes Sixtda1fbed2010-03-30 19:36:03 +02006034
6035 if (strbuf_read(&buf, child.out, 0) < 0)
6036 err = error("error reading from textconv command '%s'", pgm);
Jeff King70d70992009-12-30 04:02:53 -05006037 close(child.out);
Johannes Sixtda1fbed2010-03-30 19:36:03 +02006038
6039 if (finish_command(&child) || err) {
6040 strbuf_release(&buf);
6041 remove_tempfile();
6042 return NULL;
6043 }
Jeff King479b0ae2009-01-22 00:59:56 -05006044 remove_tempfile();
Jeff King9cb92c32008-10-05 17:43:45 -04006045
6046 return strbuf_detach(&buf, outsize);
6047}
Jeff King840383b2010-04-01 20:09:26 -04006048
Axel Bonneta788d7d2010-06-07 17:23:36 +02006049size_t fill_textconv(struct userdiff_driver *driver,
6050 struct diff_filespec *df,
6051 char **outbuf)
Jeff King840383b2010-04-01 20:09:26 -04006052{
6053 size_t size;
6054
Jeff Kinga64e6a42016-02-22 13:28:54 -05006055 if (!driver) {
Jeff King840383b2010-04-01 20:09:26 -04006056 if (!DIFF_FILE_VALID(df)) {
6057 *outbuf = "";
6058 return 0;
6059 }
6060 if (diff_populate_filespec(df, 0))
6061 die("unable to read files to diff");
6062 *outbuf = df->data;
6063 return df->size;
6064 }
6065
Jeff Kinga64e6a42016-02-22 13:28:54 -05006066 if (!driver->textconv)
6067 die("BUG: fill_textconv called with non-textconv driver");
6068
brian m. carlson41c95602016-06-24 23:09:24 +00006069 if (driver->textconv_cache && df->oid_valid) {
brian m. carlsona0d12c42016-06-24 23:09:23 +00006070 *outbuf = notes_cache_get(driver->textconv_cache,
brian m. carlson569aa372017-05-06 22:09:58 +00006071 &df->oid,
Jeff Kingd9bae1a2010-04-01 20:12:15 -04006072 &size);
6073 if (*outbuf)
6074 return size;
6075 }
6076
6077 *outbuf = run_textconv(driver->textconv, df, &size);
Jeff King840383b2010-04-01 20:09:26 -04006078 if (!*outbuf)
6079 die("unable to read files to diff");
Jeff Kingd9bae1a2010-04-01 20:12:15 -04006080
brian m. carlson41c95602016-06-24 23:09:24 +00006081 if (driver->textconv_cache && df->oid_valid) {
Jeff Kingd9bae1a2010-04-01 20:12:15 -04006082 /* ignore errors, as we might be in a readonly repository */
brian m. carlson569aa372017-05-06 22:09:58 +00006083 notes_cache_put(driver->textconv_cache, &df->oid, *outbuf,
Jeff Kingd9bae1a2010-04-01 20:12:15 -04006084 size);
6085 /*
6086 * we could save up changes and flush them all at the end,
6087 * but we would need an extra call after all diffing is done.
6088 * Since generating a cache entry is the slow path anyway,
6089 * this extra overhead probably isn't a big deal.
6090 */
6091 notes_cache_write(driver->textconv_cache);
6092 }
6093
Jeff King840383b2010-04-01 20:09:26 -04006094 return size;
6095}
Nguyễn Thái Ngọc Duy4914c962012-10-26 22:53:52 +07006096
Jeff Smith3a35cb22017-05-24 00:15:10 -05006097int textconv_object(const char *path,
6098 unsigned mode,
6099 const struct object_id *oid,
6100 int oid_valid,
6101 char **buf,
6102 unsigned long *buf_size)
6103{
6104 struct diff_filespec *df;
6105 struct userdiff_driver *textconv;
6106
6107 df = alloc_filespec(path);
Junio C Hamanoa6f38c12017-06-19 12:38:44 -07006108 fill_filespec(df, oid, oid_valid, mode);
Jeff Smith3a35cb22017-05-24 00:15:10 -05006109 textconv = get_textconv(df);
6110 if (!textconv) {
6111 free_filespec(df);
6112 return 0;
6113 }
6114
6115 *buf_size = fill_textconv(textconv, df, buf);
6116 free_filespec(df);
6117 return 1;
6118}
6119
Nguyễn Thái Ngọc Duy4914c962012-10-26 22:53:52 +07006120void setup_diff_pager(struct diff_options *opt)
6121{
6122 /*
6123 * If the user asked for our exit code, then either they want --quiet
6124 * or --exit-code. We should definitely not bother with a pager in the
6125 * former case, as we will generate no output. Since we still properly
6126 * report our exit code even when a pager is run, we _could_ run a
6127 * pager with --exit-code. But since we have not done so historically,
6128 * and because it is easy to find people oneline advising "git diff
6129 * --exit-code" in hooks and other scripts, we do not do so.
6130 */
6131 if (!DIFF_OPT_TST(opt, EXIT_WITH_STATUS) &&
6132 check_pager_config("diff") != 0)
6133 setup_pager();
6134}