Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2005 Junio C Hamano |
| 3 | */ |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 4 | #include "cache.h" |
Brandon Williams | b2141fc | 2017-06-14 11:07:36 -0700 | [diff] [blame] | 5 | #include "config.h" |
Michael Haggerty | 284098f | 2015-08-12 19:12:01 +0200 | [diff] [blame] | 6 | #include "tempfile.h" |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 7 | #include "quote.h" |
| 8 | #include "diff.h" |
| 9 | #include "diffcore.h" |
Junio C Hamano | 051308f | 2006-05-04 16:51:44 -0700 | [diff] [blame] | 10 | #include "delta.h" |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 11 | #include "xdiff-interface.h" |
Jeff King | 7c92fe0 | 2006-09-08 04:03:18 -0400 | [diff] [blame] | 12 | #include "color.h" |
Junio C Hamano | 8c70124 | 2007-04-12 23:05:29 -0700 | [diff] [blame] | 13 | #include "attr.h" |
Johannes Sixt | d5535ec | 2007-10-19 21:47:56 +0200 | [diff] [blame] | 14 | #include "run-command.h" |
Junio C Hamano | 2370781 | 2008-01-02 01:50:11 -0800 | [diff] [blame] | 15 | #include "utf8.h" |
Stefan Beller | cbd53a2 | 2018-05-15 16:42:15 -0700 | [diff] [blame] | 16 | #include "object-store.h" |
Jeff King | be58e70 | 2008-10-05 17:43:21 -0400 | [diff] [blame] | 17 | #include "userdiff.h" |
Heiko Voigt | 851e18c | 2015-08-17 17:21:59 -0700 | [diff] [blame] | 18 | #include "submodule-config.h" |
Johannes Schindelin | 752c0c2 | 2009-10-19 14:38:32 +0200 | [diff] [blame] | 19 | #include "submodule.h" |
Stefan Beller | 2e2d5ac | 2017-06-30 13:53:07 -0700 | [diff] [blame] | 20 | #include "hashmap.h" |
Junio C Hamano | a757c64 | 2010-03-24 19:21:32 -0700 | [diff] [blame] | 21 | #include "ll-merge.h" |
Michael Haggerty | 02e8ca0 | 2012-10-28 17:50:54 +0100 | [diff] [blame] | 22 | #include "string-list.h" |
Jeff King | dbbcd44 | 2020-07-28 16:23:39 -0400 | [diff] [blame] | 23 | #include "strvec.h" |
Jacob Keller | 660e113 | 2016-08-31 16:27:20 -0700 | [diff] [blame] | 24 | #include "graph.h" |
Jonathan Tan | 150e300 | 2017-08-18 15:20:36 -0700 | [diff] [blame] | 25 | #include "packfile.h" |
Nguyễn Thái Ngọc Duy | 4a28847 | 2019-01-27 07:35:31 +0700 | [diff] [blame] | 26 | #include "parse-options.h" |
Nguyễn Thái Ngọc Duy | 3ac68a9 | 2018-05-26 15:55:24 +0200 | [diff] [blame] | 27 | #include "help.h" |
Christian Couder | b14ed5a | 2019-06-25 15:40:31 +0200 | [diff] [blame] | 28 | #include "promisor-remote.h" |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 29 | |
Shawn O. Pearce | 1510fea | 2006-12-14 06:15:57 -0500 | [diff] [blame] | 30 | #ifdef NO_FAST_WORKING_DIRECTORY |
| 31 | #define FAST_WORKING_DIRECTORY 0 |
| 32 | #else |
| 33 | #define FAST_WORKING_DIRECTORY 1 |
| 34 | #endif |
| 35 | |
David Rientjes | 96f1e58 | 2006-08-15 10:23:48 -0700 | [diff] [blame] | 36 | static int diff_detect_rename_default; |
Stefan Beller | 33de716 | 2017-05-08 12:03:38 -0400 | [diff] [blame] | 37 | static int diff_indent_heuristic = 1; |
Jeff King | 92c57e5 | 2011-02-19 05:21:28 -0500 | [diff] [blame] | 38 | static int diff_rename_limit_default = 400; |
Jim Meyering | a624eaa | 2008-08-15 13:39:26 +0200 | [diff] [blame] | 39 | static int diff_suppress_blank_empty; |
Junio C Hamano | d2aea13 | 2012-09-15 13:59:59 -0700 | [diff] [blame] | 40 | static int diff_use_color_default = -1; |
Stefan Beller | 2e2d5ac | 2017-06-30 13:53:07 -0700 | [diff] [blame] | 41 | static int diff_color_moved_default; |
Stefan Beller | 626c0b5 | 2018-07-18 12:31:56 -0700 | [diff] [blame] | 42 | static int diff_color_moved_ws_default; |
Jeff Muizelaar | 6468a4e | 2012-09-27 15:12:52 -0400 | [diff] [blame] | 43 | static int diff_context_default = 3; |
Vegard Nossum | c488867 | 2017-01-12 13:21:11 +0100 | [diff] [blame] | 44 | static int diff_interhunk_context_default; |
Boyd Stephen Smith Jr | 98a4d87 | 2009-01-20 21:46:57 -0600 | [diff] [blame] | 45 | static const char *diff_word_regex_cfg; |
Johannes Schindelin | cbe0210 | 2007-12-17 13:42:20 +0000 | [diff] [blame] | 46 | static const char *external_diff_cmd_cfg; |
Samuel Bronson | 6d8940b | 2013-12-18 19:08:12 -0500 | [diff] [blame] | 47 | static const char *diff_order_file_cfg; |
Junio C Hamano | aecbf91 | 2007-08-31 13:13:42 -0700 | [diff] [blame] | 48 | int diff_auto_refresh_index = 1; |
Junio C Hamano | a5a818e | 2008-08-18 20:08:09 -0700 | [diff] [blame] | 49 | static int diff_mnemonic_prefix; |
Eli Collins | f89504d | 2010-05-02 19:03:41 -0700 | [diff] [blame] | 50 | static int diff_no_prefix; |
Laurent Arnoud | c28ded8 | 2020-05-22 12:46:18 +0200 | [diff] [blame] | 51 | static int diff_relative; |
Zbigniew Jędrzejewski-Szmek | df44483 | 2012-03-01 13:26:46 +0100 | [diff] [blame] | 52 | static int diff_stat_graph_width; |
Johan Herland | 712d2c7 | 2011-04-29 11:36:20 +0200 | [diff] [blame] | 53 | static int diff_dirstat_permille_default = 30; |
Johannes Schindelin | be4f2b4 | 2010-08-05 10:49:55 +0200 | [diff] [blame] | 54 | static struct diff_options default_diff_options; |
Michal Privoznik | 07ab4de | 2013-01-16 08:51:57 +0100 | [diff] [blame] | 55 | static long diff_algorithm; |
Junio C Hamano | a17505f | 2016-10-04 15:26:27 -0700 | [diff] [blame] | 56 | static unsigned ws_error_highlight_default = WSEH_NEW; |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 57 | |
Jeff King | 7c92fe0 | 2006-09-08 04:03:18 -0400 | [diff] [blame] | 58 | static char diff_colors[][COLOR_MAXLEN] = { |
Arjen Laarhoven | dc6ebd4 | 2009-02-13 22:53:40 +0100 | [diff] [blame] | 59 | GIT_COLOR_RESET, |
Jeff King | 8dbf3eb | 2015-05-27 16:48:46 -0400 | [diff] [blame] | 60 | GIT_COLOR_NORMAL, /* CONTEXT */ |
Arjen Laarhoven | dc6ebd4 | 2009-02-13 22:53:40 +0100 | [diff] [blame] | 61 | GIT_COLOR_BOLD, /* METAINFO */ |
| 62 | GIT_COLOR_CYAN, /* FRAGINFO */ |
| 63 | GIT_COLOR_RED, /* OLD */ |
| 64 | GIT_COLOR_GREEN, /* NEW */ |
| 65 | GIT_COLOR_YELLOW, /* COMMIT */ |
| 66 | GIT_COLOR_BG_RED, /* WHITESPACE */ |
Bert Wesarg | 89cb73a | 2009-11-27 07:55:18 +0100 | [diff] [blame] | 67 | GIT_COLOR_NORMAL, /* FUNCINFO */ |
Stefan Beller | 86b452e | 2017-06-30 13:53:09 -0700 | [diff] [blame] | 68 | GIT_COLOR_BOLD_MAGENTA, /* OLD_MOVED */ |
| 69 | GIT_COLOR_BOLD_BLUE, /* OLD_MOVED ALTERNATIVE */ |
| 70 | GIT_COLOR_FAINT, /* OLD_MOVED_DIM */ |
| 71 | GIT_COLOR_FAINT_ITALIC, /* OLD_MOVED_ALTERNATIVE_DIM */ |
| 72 | GIT_COLOR_BOLD_CYAN, /* NEW_MOVED */ |
| 73 | GIT_COLOR_BOLD_YELLOW, /* NEW_MOVED ALTERNATIVE */ |
| 74 | GIT_COLOR_FAINT, /* NEW_MOVED_DIM */ |
| 75 | GIT_COLOR_FAINT_ITALIC, /* NEW_MOVED_ALTERNATIVE_DIM */ |
Johannes Schindelin | a7be92a | 2018-08-13 04:33:32 -0700 | [diff] [blame] | 76 | GIT_COLOR_FAINT, /* CONTEXT_DIM */ |
| 77 | GIT_COLOR_FAINT_RED, /* OLD_DIM */ |
| 78 | GIT_COLOR_FAINT_GREEN, /* NEW_DIM */ |
| 79 | GIT_COLOR_BOLD, /* CONTEXT_BOLD */ |
| 80 | GIT_COLOR_BOLD_RED, /* OLD_BOLD */ |
| 81 | GIT_COLOR_BOLD_GREEN, /* NEW_BOLD */ |
Johannes Schindelin | cd112ce | 2006-06-13 18:45:44 +0200 | [diff] [blame] | 82 | }; |
| 83 | |
Nguyễn Thái Ngọc Duy | a73b368 | 2018-05-26 15:55:21 +0200 | [diff] [blame] | 84 | static const char *color_diff_slots[] = { |
| 85 | [DIFF_CONTEXT] = "context", |
| 86 | [DIFF_METAINFO] = "meta", |
| 87 | [DIFF_FRAGINFO] = "frag", |
| 88 | [DIFF_FILE_OLD] = "old", |
| 89 | [DIFF_FILE_NEW] = "new", |
| 90 | [DIFF_COMMIT] = "commit", |
| 91 | [DIFF_WHITESPACE] = "whitespace", |
| 92 | [DIFF_FUNCINFO] = "func", |
| 93 | [DIFF_FILE_OLD_MOVED] = "oldMoved", |
| 94 | [DIFF_FILE_OLD_MOVED_ALT] = "oldMovedAlternative", |
| 95 | [DIFF_FILE_OLD_MOVED_DIM] = "oldMovedDimmed", |
| 96 | [DIFF_FILE_OLD_MOVED_ALT_DIM] = "oldMovedAlternativeDimmed", |
| 97 | [DIFF_FILE_NEW_MOVED] = "newMoved", |
| 98 | [DIFF_FILE_NEW_MOVED_ALT] = "newMovedAlternative", |
| 99 | [DIFF_FILE_NEW_MOVED_DIM] = "newMovedDimmed", |
| 100 | [DIFF_FILE_NEW_MOVED_ALT_DIM] = "newMovedAlternativeDimmed", |
Johannes Schindelin | a7be92a | 2018-08-13 04:33:32 -0700 | [diff] [blame] | 101 | [DIFF_CONTEXT_DIM] = "contextDimmed", |
| 102 | [DIFF_FILE_OLD_DIM] = "oldDimmed", |
| 103 | [DIFF_FILE_NEW_DIM] = "newDimmed", |
| 104 | [DIFF_CONTEXT_BOLD] = "contextBold", |
| 105 | [DIFF_FILE_OLD_BOLD] = "oldBold", |
| 106 | [DIFF_FILE_NEW_BOLD] = "newBold", |
Nguyễn Thái Ngọc Duy | a73b368 | 2018-05-26 15:55:21 +0200 | [diff] [blame] | 107 | }; |
| 108 | |
Nguyễn Thái Ngọc Duy | 3ac68a9 | 2018-05-26 15:55:24 +0200 | [diff] [blame] | 109 | define_list_config_array_extra(color_diff_slots, {"plain"}); |
| 110 | |
Jeff King | 9e1a5eb | 2014-06-18 15:41:50 -0400 | [diff] [blame] | 111 | static int parse_diff_color_slot(const char *var) |
Junio C Hamano | 801235c | 2006-06-24 04:06:23 -0700 | [diff] [blame] | 112 | { |
Nguyễn Thái Ngọc Duy | a73b368 | 2018-05-26 15:55:21 +0200 | [diff] [blame] | 113 | if (!strcasecmp(var, "plain")) |
Jeff King | 8dbf3eb | 2015-05-27 16:48:46 -0400 | [diff] [blame] | 114 | return DIFF_CONTEXT; |
Nguyễn Thái Ngọc Duy | a73b368 | 2018-05-26 15:55:21 +0200 | [diff] [blame] | 115 | return LOOKUP_CONFIG(color_diff_slots, var); |
Junio C Hamano | 801235c | 2006-06-24 04:06:23 -0700 | [diff] [blame] | 116 | } |
| 117 | |
Michael Haggerty | 02e8ca0 | 2012-10-28 17:50:54 +0100 | [diff] [blame] | 118 | static int parse_dirstat_params(struct diff_options *options, const char *params_string, |
Johan Herland | 51670fc | 2011-04-29 11:36:22 +0200 | [diff] [blame] | 119 | struct strbuf *errmsg) |
Johan Herland | 333f3fb | 2011-04-29 11:36:18 +0200 | [diff] [blame] | 120 | { |
Michael Haggerty | 02e8ca0 | 2012-10-28 17:50:54 +0100 | [diff] [blame] | 121 | char *params_copy = xstrdup(params_string); |
| 122 | struct string_list params = STRING_LIST_INIT_NODUP; |
| 123 | int ret = 0; |
| 124 | int i; |
Johan Herland | 51670fc | 2011-04-29 11:36:22 +0200 | [diff] [blame] | 125 | |
Michael Haggerty | 02e8ca0 | 2012-10-28 17:50:54 +0100 | [diff] [blame] | 126 | if (*params_copy) |
| 127 | string_list_split_in_place(¶ms, params_copy, ',', -1); |
| 128 | for (i = 0; i < params.nr; i++) { |
| 129 | const char *p = params.items[i].string; |
| 130 | if (!strcmp(p, "changes")) { |
Brandon Williams | 0d1e0e7 | 2017-10-31 11:19:11 -0700 | [diff] [blame] | 131 | options->flags.dirstat_by_line = 0; |
| 132 | options->flags.dirstat_by_file = 0; |
Michael Haggerty | 02e8ca0 | 2012-10-28 17:50:54 +0100 | [diff] [blame] | 133 | } else if (!strcmp(p, "lines")) { |
Brandon Williams | 0d1e0e7 | 2017-10-31 11:19:11 -0700 | [diff] [blame] | 134 | options->flags.dirstat_by_line = 1; |
| 135 | options->flags.dirstat_by_file = 0; |
Michael Haggerty | 02e8ca0 | 2012-10-28 17:50:54 +0100 | [diff] [blame] | 136 | } else if (!strcmp(p, "files")) { |
Brandon Williams | 0d1e0e7 | 2017-10-31 11:19:11 -0700 | [diff] [blame] | 137 | options->flags.dirstat_by_line = 0; |
| 138 | options->flags.dirstat_by_file = 1; |
Michael Haggerty | 02e8ca0 | 2012-10-28 17:50:54 +0100 | [diff] [blame] | 139 | } else if (!strcmp(p, "noncumulative")) { |
Brandon Williams | 0d1e0e7 | 2017-10-31 11:19:11 -0700 | [diff] [blame] | 140 | options->flags.dirstat_cumulative = 0; |
Michael Haggerty | 02e8ca0 | 2012-10-28 17:50:54 +0100 | [diff] [blame] | 141 | } else if (!strcmp(p, "cumulative")) { |
Brandon Williams | 0d1e0e7 | 2017-10-31 11:19:11 -0700 | [diff] [blame] | 142 | options->flags.dirstat_cumulative = 1; |
Johan Herland | 333f3fb | 2011-04-29 11:36:18 +0200 | [diff] [blame] | 143 | } else if (isdigit(*p)) { |
| 144 | char *end; |
Johan Herland | 51670fc | 2011-04-29 11:36:22 +0200 | [diff] [blame] | 145 | int permille = strtoul(p, &end, 10) * 10; |
| 146 | if (*end == '.' && isdigit(*++end)) { |
Johan Herland | 712d2c7 | 2011-04-29 11:36:20 +0200 | [diff] [blame] | 147 | /* only use first digit */ |
Johan Herland | 51670fc | 2011-04-29 11:36:22 +0200 | [diff] [blame] | 148 | permille += *end - '0'; |
Johan Herland | 712d2c7 | 2011-04-29 11:36:20 +0200 | [diff] [blame] | 149 | /* .. and ignore any further digits */ |
Johan Herland | 51670fc | 2011-04-29 11:36:22 +0200 | [diff] [blame] | 150 | while (isdigit(*++end)) |
Johan Herland | 712d2c7 | 2011-04-29 11:36:20 +0200 | [diff] [blame] | 151 | ; /* nothing */ |
| 152 | } |
Michael Haggerty | 02e8ca0 | 2012-10-28 17:50:54 +0100 | [diff] [blame] | 153 | if (!*end) |
Johan Herland | 51670fc | 2011-04-29 11:36:22 +0200 | [diff] [blame] | 154 | options->dirstat_permille = permille; |
| 155 | else { |
Michael Haggerty | 02e8ca0 | 2012-10-28 17:50:54 +0100 | [diff] [blame] | 156 | strbuf_addf(errmsg, _(" Failed to parse dirstat cut-off percentage '%s'\n"), |
| 157 | p); |
Johan Herland | 51670fc | 2011-04-29 11:36:22 +0200 | [diff] [blame] | 158 | ret++; |
| 159 | } |
| 160 | } else { |
Michael Haggerty | 02e8ca0 | 2012-10-28 17:50:54 +0100 | [diff] [blame] | 161 | strbuf_addf(errmsg, _(" Unknown dirstat parameter '%s'\n"), p); |
Johan Herland | 51670fc | 2011-04-29 11:36:22 +0200 | [diff] [blame] | 162 | ret++; |
Johan Herland | 333f3fb | 2011-04-29 11:36:18 +0200 | [diff] [blame] | 163 | } |
Johan Herland | 51670fc | 2011-04-29 11:36:22 +0200 | [diff] [blame] | 164 | |
Johan Herland | 333f3fb | 2011-04-29 11:36:18 +0200 | [diff] [blame] | 165 | } |
Michael Haggerty | 02e8ca0 | 2012-10-28 17:50:54 +0100 | [diff] [blame] | 166 | string_list_clear(¶ms, 0); |
| 167 | free(params_copy); |
Johan Herland | 51670fc | 2011-04-29 11:36:22 +0200 | [diff] [blame] | 168 | return ret; |
Johan Herland | 333f3fb | 2011-04-29 11:36:18 +0200 | [diff] [blame] | 169 | } |
| 170 | |
Ramkumar Ramachandra | c47ef57 | 2012-11-13 21:12:45 +0530 | [diff] [blame] | 171 | static int parse_submodule_params(struct diff_options *options, const char *value) |
| 172 | { |
| 173 | if (!strcmp(value, "log")) |
Jacob Keller | 61cfbc0 | 2016-08-31 16:27:21 -0700 | [diff] [blame] | 174 | options->submodule_format = DIFF_SUBMODULE_LOG; |
Ramkumar Ramachandra | c47ef57 | 2012-11-13 21:12:45 +0530 | [diff] [blame] | 175 | else if (!strcmp(value, "short")) |
Jacob Keller | 61cfbc0 | 2016-08-31 16:27:21 -0700 | [diff] [blame] | 176 | options->submodule_format = DIFF_SUBMODULE_SHORT; |
Jacob Keller | fd47ae6 | 2016-08-31 16:27:25 -0700 | [diff] [blame] | 177 | else if (!strcmp(value, "diff")) |
| 178 | options->submodule_format = DIFF_SUBMODULE_INLINE_DIFF; |
Nguyễn Thái Ngọc Duy | 5a59a23 | 2019-02-16 18:24:41 +0700 | [diff] [blame] | 179 | /* |
| 180 | * Please update $__git_diff_submodule_formats in |
| 181 | * git-completion.bash when you add new formats. |
| 182 | */ |
Ramkumar Ramachandra | c47ef57 | 2012-11-13 21:12:45 +0530 | [diff] [blame] | 183 | else |
| 184 | return -1; |
| 185 | return 0; |
| 186 | } |
| 187 | |
Ben Peart | 85b4603 | 2018-05-02 16:01:14 +0000 | [diff] [blame] | 188 | int git_config_rename(const char *var, const char *value) |
Linus Torvalds | cced5fb | 2009-04-09 11:46:15 -0700 | [diff] [blame] | 189 | { |
| 190 | if (!value) |
| 191 | return DIFF_DETECT_RENAME; |
| 192 | if (!strcasecmp(value, "copies") || !strcasecmp(value, "copy")) |
| 193 | return DIFF_DETECT_COPY; |
| 194 | return git_config_bool(var,value) ? DIFF_DETECT_RENAME : 0; |
| 195 | } |
| 196 | |
Michal Privoznik | 07924d4 | 2013-01-16 08:51:58 +0100 | [diff] [blame] | 197 | long parse_algorithm_value(const char *value) |
Michal Privoznik | 07ab4de | 2013-01-16 08:51:57 +0100 | [diff] [blame] | 198 | { |
| 199 | if (!value) |
| 200 | return -1; |
| 201 | else if (!strcasecmp(value, "myers") || !strcasecmp(value, "default")) |
| 202 | return 0; |
| 203 | else if (!strcasecmp(value, "minimal")) |
| 204 | return XDF_NEED_MINIMAL; |
| 205 | else if (!strcasecmp(value, "patience")) |
| 206 | return XDF_PATIENCE_DIFF; |
| 207 | else if (!strcasecmp(value, "histogram")) |
| 208 | return XDF_HISTOGRAM_DIFF; |
Nguyễn Thái Ngọc Duy | 5a59a23 | 2019-02-16 18:24:41 +0700 | [diff] [blame] | 209 | /* |
| 210 | * Please update $__git_diff_algorithms in git-completion.bash |
| 211 | * when you add new algorithms. |
| 212 | */ |
Michal Privoznik | 07ab4de | 2013-01-16 08:51:57 +0100 | [diff] [blame] | 213 | return -1; |
| 214 | } |
| 215 | |
Junio C Hamano | 0b4b42e | 2016-10-04 15:09:18 -0700 | [diff] [blame] | 216 | static int parse_one_token(const char **arg, const char *token) |
| 217 | { |
| 218 | const char *rest; |
| 219 | if (skip_prefix(*arg, token, &rest) && (!*rest || *rest == ',')) { |
| 220 | *arg = rest; |
| 221 | return 1; |
| 222 | } |
| 223 | return 0; |
| 224 | } |
| 225 | |
| 226 | static int parse_ws_error_highlight(const char *arg) |
| 227 | { |
| 228 | const char *orig_arg = arg; |
| 229 | unsigned val = 0; |
| 230 | |
| 231 | while (*arg) { |
| 232 | if (parse_one_token(&arg, "none")) |
| 233 | val = 0; |
| 234 | else if (parse_one_token(&arg, "default")) |
| 235 | val = WSEH_NEW; |
| 236 | else if (parse_one_token(&arg, "all")) |
| 237 | val = WSEH_NEW | WSEH_OLD | WSEH_CONTEXT; |
| 238 | else if (parse_one_token(&arg, "new")) |
| 239 | val |= WSEH_NEW; |
| 240 | else if (parse_one_token(&arg, "old")) |
| 241 | val |= WSEH_OLD; |
| 242 | else if (parse_one_token(&arg, "context")) |
| 243 | val |= WSEH_CONTEXT; |
| 244 | else { |
| 245 | return -1 - (int)(arg - orig_arg); |
| 246 | } |
| 247 | if (*arg) |
| 248 | arg++; |
| 249 | } |
| 250 | return val; |
| 251 | } |
| 252 | |
Junio C Hamano | e0e324a | 2007-07-07 01:49:58 -0700 | [diff] [blame] | 253 | /* |
Junio C Hamano | 83ad63c | 2006-07-08 01:05:16 -0700 | [diff] [blame] | 254 | * These are to give UI layer defaults. |
| 255 | * The core-level commands such as git-diff-files should |
| 256 | * never be affected by the setting of diff.renames |
| 257 | * the user happens to have in the configuration file. |
| 258 | */ |
Matthieu Moy | 5404c11 | 2016-02-25 09:59:21 +0100 | [diff] [blame] | 259 | void init_diff_ui_defaults(void) |
| 260 | { |
Nguyễn Thái Ngọc Duy | 06dba2b | 2017-12-27 17:18:35 +0700 | [diff] [blame] | 261 | diff_detect_rename_default = DIFF_DETECT_RENAME; |
Matthieu Moy | 5404c11 | 2016-02-25 09:59:21 +0100 | [diff] [blame] | 262 | } |
| 263 | |
Michael Haggerty | 5b16287 | 2016-09-05 11:44:53 +0200 | [diff] [blame] | 264 | int git_diff_heuristic_config(const char *var, const char *value, void *cb) |
| 265 | { |
Junio C Hamano | 3cde4e0 | 2016-12-23 12:32:22 -0800 | [diff] [blame] | 266 | if (!strcmp(var, "diff.indentheuristic")) |
Michael Haggerty | 5b16287 | 2016-09-05 11:44:53 +0200 | [diff] [blame] | 267 | diff_indent_heuristic = git_config_bool(var, value); |
Michael Haggerty | 5b16287 | 2016-09-05 11:44:53 +0200 | [diff] [blame] | 268 | return 0; |
| 269 | } |
| 270 | |
Stefan Beller | 2e2d5ac | 2017-06-30 13:53:07 -0700 | [diff] [blame] | 271 | static int parse_color_moved(const char *arg) |
| 272 | { |
| 273 | switch (git_parse_maybe_bool(arg)) { |
| 274 | case 0: |
| 275 | return COLOR_MOVED_NO; |
| 276 | case 1: |
| 277 | return COLOR_MOVED_DEFAULT; |
| 278 | default: |
| 279 | break; |
| 280 | } |
| 281 | |
| 282 | if (!strcmp(arg, "no")) |
| 283 | return COLOR_MOVED_NO; |
Stefan Beller | 176841f | 2017-06-30 13:53:08 -0700 | [diff] [blame] | 284 | else if (!strcmp(arg, "plain")) |
| 285 | return COLOR_MOVED_PLAIN; |
Stefan Beller | 51da15e | 2018-07-16 16:05:39 -0700 | [diff] [blame] | 286 | else if (!strcmp(arg, "blocks")) |
| 287 | return COLOR_MOVED_BLOCKS; |
Stefan Beller | 2e2d5ac | 2017-06-30 13:53:07 -0700 | [diff] [blame] | 288 | else if (!strcmp(arg, "zebra")) |
| 289 | return COLOR_MOVED_ZEBRA; |
| 290 | else if (!strcmp(arg, "default")) |
| 291 | return COLOR_MOVED_DEFAULT; |
Eric Sunshine | e3f2f5f | 2018-07-24 17:58:45 -0400 | [diff] [blame] | 292 | else if (!strcmp(arg, "dimmed-zebra")) |
| 293 | return COLOR_MOVED_ZEBRA_DIM; |
Stefan Beller | 86b452e | 2017-06-30 13:53:09 -0700 | [diff] [blame] | 294 | else if (!strcmp(arg, "dimmed_zebra")) |
| 295 | return COLOR_MOVED_ZEBRA_DIM; |
Stefan Beller | 2e2d5ac | 2017-06-30 13:53:07 -0700 | [diff] [blame] | 296 | else |
Junio C Hamano | 706b0b5 | 2018-08-15 15:08:22 -0700 | [diff] [blame] | 297 | return error(_("color moved setting must be one of 'no', 'default', 'blocks', 'zebra', 'dimmed-zebra', 'plain'")); |
Stefan Beller | 2e2d5ac | 2017-06-30 13:53:07 -0700 | [diff] [blame] | 298 | } |
| 299 | |
Stefan Beller | d173e79 | 2018-11-13 13:33:57 -0800 | [diff] [blame] | 300 | static unsigned parse_color_moved_ws(const char *arg) |
Stefan Beller | b309571 | 2018-07-16 16:05:40 -0700 | [diff] [blame] | 301 | { |
| 302 | int ret = 0; |
| 303 | struct string_list l = STRING_LIST_INIT_DUP; |
| 304 | struct string_list_item *i; |
| 305 | |
| 306 | string_list_split(&l, arg, ',', -1); |
| 307 | |
| 308 | for_each_string_list_item(i, &l) { |
| 309 | struct strbuf sb = STRBUF_INIT; |
| 310 | strbuf_addstr(&sb, i->string); |
| 311 | strbuf_trim(&sb); |
| 312 | |
Phillip Wood | b73bcba | 2018-11-23 11:16:52 +0000 | [diff] [blame] | 313 | if (!strcmp(sb.buf, "no")) |
| 314 | ret = 0; |
| 315 | else if (!strcmp(sb.buf, "ignore-space-change")) |
Stefan Beller | b309571 | 2018-07-16 16:05:40 -0700 | [diff] [blame] | 316 | ret |= XDF_IGNORE_WHITESPACE_CHANGE; |
| 317 | else if (!strcmp(sb.buf, "ignore-space-at-eol")) |
| 318 | ret |= XDF_IGNORE_WHITESPACE_AT_EOL; |
| 319 | else if (!strcmp(sb.buf, "ignore-all-space")) |
| 320 | ret |= XDF_IGNORE_WHITESPACE; |
Stefan Beller | ca1f4ae | 2018-07-18 12:31:55 -0700 | [diff] [blame] | 321 | else if (!strcmp(sb.buf, "allow-indentation-change")) |
| 322 | ret |= COLOR_MOVED_WS_ALLOW_INDENTATION_CHANGE; |
Stefan Beller | d173e79 | 2018-11-13 13:33:57 -0800 | [diff] [blame] | 323 | else { |
| 324 | ret |= COLOR_MOVED_WS_ERROR; |
| 325 | error(_("unknown color-moved-ws mode '%s', possible values are 'ignore-space-change', 'ignore-space-at-eol', 'ignore-all-space', 'allow-indentation-change'"), sb.buf); |
| 326 | } |
Stefan Beller | b309571 | 2018-07-16 16:05:40 -0700 | [diff] [blame] | 327 | |
| 328 | strbuf_release(&sb); |
| 329 | } |
| 330 | |
Stefan Beller | ca1f4ae | 2018-07-18 12:31:55 -0700 | [diff] [blame] | 331 | if ((ret & COLOR_MOVED_WS_ALLOW_INDENTATION_CHANGE) && |
Stefan Beller | d173e79 | 2018-11-13 13:33:57 -0800 | [diff] [blame] | 332 | (ret & XDF_WHITESPACE_FLAGS)) { |
Junio C Hamano | 15b07cb | 2019-01-29 12:47:53 -0800 | [diff] [blame] | 333 | error(_("color-moved-ws: allow-indentation-change cannot be combined with other whitespace modes")); |
Stefan Beller | d173e79 | 2018-11-13 13:33:57 -0800 | [diff] [blame] | 334 | ret |= COLOR_MOVED_WS_ERROR; |
| 335 | } |
Stefan Beller | ca1f4ae | 2018-07-18 12:31:55 -0700 | [diff] [blame] | 336 | |
Stefan Beller | b309571 | 2018-07-16 16:05:40 -0700 | [diff] [blame] | 337 | string_list_clear(&l, 0); |
| 338 | |
| 339 | return ret; |
Jeff King | e269eb7 | 2011-08-17 22:03:48 -0700 | [diff] [blame] | 340 | } |
Junio C Hamano | 801235c | 2006-06-24 04:06:23 -0700 | [diff] [blame] | 341 | |
| 342 | int git_diff_ui_config(const char *var, const char *value, void *cb) |
Jeff Muizelaar | 6468a4e | 2012-09-27 15:12:52 -0400 | [diff] [blame] | 343 | { |
| 344 | if (!strcmp(var, "diff.color") || !strcmp(var, "color.diff")) { |
| 345 | diff_use_color_default = git_config_colorbool(var, value); |
| 346 | return 0; |
| 347 | } |
Stefan Beller | 2e2d5ac | 2017-06-30 13:53:07 -0700 | [diff] [blame] | 348 | if (!strcmp(var, "diff.colormoved")) { |
| 349 | int cm = parse_color_moved(value); |
| 350 | if (cm < 0) |
| 351 | return -1; |
| 352 | diff_color_moved_default = cm; |
| 353 | return 0; |
| 354 | } |
Stefan Beller | 626c0b5 | 2018-07-18 12:31:56 -0700 | [diff] [blame] | 355 | if (!strcmp(var, "diff.colormovedws")) { |
Stefan Beller | d173e79 | 2018-11-13 13:33:57 -0800 | [diff] [blame] | 356 | unsigned cm = parse_color_moved_ws(value); |
| 357 | if (cm & COLOR_MOVED_WS_ERROR) |
Stefan Beller | 626c0b5 | 2018-07-18 12:31:56 -0700 | [diff] [blame] | 358 | return -1; |
| 359 | diff_color_moved_ws_default = cm; |
| 360 | return 0; |
| 361 | } |
Jeff Muizelaar | 6468a4e | 2012-09-27 15:12:52 -0400 | [diff] [blame] | 362 | if (!strcmp(var, "diff.context")) { |
| 363 | diff_context_default = git_config_int(var, value); |
| 364 | if (diff_context_default < 0) |
| 365 | return -1; |
| 366 | return 0; |
| 367 | } |
Vegard Nossum | c488867 | 2017-01-12 13:21:11 +0100 | [diff] [blame] | 368 | if (!strcmp(var, "diff.interhunkcontext")) { |
| 369 | diff_interhunk_context_default = git_config_int(var, value); |
| 370 | if (diff_interhunk_context_default < 0) |
| 371 | return -1; |
| 372 | return 0; |
| 373 | } |
Eric Wong | b68ea12 | 2006-07-07 04:01:23 -0700 | [diff] [blame] | 374 | if (!strcmp(var, "diff.renames")) { |
Linus Torvalds | cced5fb | 2009-04-09 11:46:15 -0700 | [diff] [blame] | 375 | diff_detect_rename_default = git_config_rename(var, value); |
Eric Wong | b68ea12 | 2006-07-07 04:01:23 -0700 | [diff] [blame] | 376 | return 0; |
| 377 | } |
Junio C Hamano | aecbf91 | 2007-08-31 13:13:42 -0700 | [diff] [blame] | 378 | if (!strcmp(var, "diff.autorefreshindex")) { |
| 379 | diff_auto_refresh_index = git_config_bool(var, value); |
| 380 | return 0; |
| 381 | } |
Junio C Hamano | a5a818e | 2008-08-18 20:08:09 -0700 | [diff] [blame] | 382 | if (!strcmp(var, "diff.mnemonicprefix")) { |
| 383 | diff_mnemonic_prefix = git_config_bool(var, value); |
| 384 | return 0; |
| 385 | } |
Eli Collins | f89504d | 2010-05-02 19:03:41 -0700 | [diff] [blame] | 386 | if (!strcmp(var, "diff.noprefix")) { |
| 387 | diff_no_prefix = git_config_bool(var, value); |
| 388 | return 0; |
| 389 | } |
Laurent Arnoud | c28ded8 | 2020-05-22 12:46:18 +0200 | [diff] [blame] | 390 | if (!strcmp(var, "diff.relative")) { |
| 391 | diff_relative = git_config_bool(var, value); |
| 392 | return 0; |
| 393 | } |
Zbigniew Jędrzejewski-Szmek | df44483 | 2012-03-01 13:26:46 +0100 | [diff] [blame] | 394 | if (!strcmp(var, "diff.statgraphwidth")) { |
| 395 | diff_stat_graph_width = git_config_int(var, value); |
| 396 | return 0; |
| 397 | } |
Brian Hetro | daec808 | 2008-07-05 01:24:43 -0400 | [diff] [blame] | 398 | if (!strcmp(var, "diff.external")) |
| 399 | return git_config_string(&external_diff_cmd_cfg, var, value); |
Boyd Stephen Smith Jr | 98a4d87 | 2009-01-20 21:46:57 -0600 | [diff] [blame] | 400 | if (!strcmp(var, "diff.wordregex")) |
| 401 | return git_config_string(&diff_word_regex_cfg, var, value); |
Samuel Bronson | 6d8940b | 2013-12-18 19:08:12 -0500 | [diff] [blame] | 402 | if (!strcmp(var, "diff.orderfile")) |
| 403 | return git_config_pathname(&diff_order_file_cfg, var, value); |
Junio C Hamano | f1af60b | 2007-04-22 17:52:55 -0700 | [diff] [blame] | 404 | |
Johannes Schindelin | be4f2b4 | 2010-08-05 10:49:55 +0200 | [diff] [blame] | 405 | if (!strcmp(var, "diff.ignoresubmodules")) |
| 406 | handle_ignore_submodules_arg(&default_diff_options, value); |
| 407 | |
Ramkumar Ramachandra | c47ef57 | 2012-11-13 21:12:45 +0530 | [diff] [blame] | 408 | if (!strcmp(var, "diff.submodule")) { |
| 409 | if (parse_submodule_params(&default_diff_options, value)) |
| 410 | warning(_("Unknown value for 'diff.submodule' config variable: '%s'"), |
| 411 | value); |
| 412 | return 0; |
| 413 | } |
| 414 | |
Michal Privoznik | 07ab4de | 2013-01-16 08:51:57 +0100 | [diff] [blame] | 415 | if (!strcmp(var, "diff.algorithm")) { |
| 416 | diff_algorithm = parse_algorithm_value(value); |
| 417 | if (diff_algorithm < 0) |
| 418 | return -1; |
| 419 | return 0; |
| 420 | } |
| 421 | |
Jeff King | 33c643b | 2017-10-13 13:24:31 -0400 | [diff] [blame] | 422 | if (git_color_config(var, value, cb) < 0) |
| 423 | return -1; |
| 424 | |
Johannes Schindelin | ef90d6d | 2008-05-14 18:46:53 +0100 | [diff] [blame] | 425 | return git_diff_basic_config(var, value, cb); |
Jeff King | 9a1805a | 2008-01-04 03:59:34 -0500 | [diff] [blame] | 426 | } |
| 427 | |
Johannes Schindelin | ef90d6d | 2008-05-14 18:46:53 +0100 | [diff] [blame] | 428 | int git_diff_basic_config(const char *var, const char *value, void *cb) |
Jeff King | 9a1805a | 2008-01-04 03:59:34 -0500 | [diff] [blame] | 429 | { |
Jeff King | ae021d8 | 2014-06-18 15:47:50 -0400 | [diff] [blame] | 430 | const char *name; |
| 431 | |
Linus Torvalds | 2b6ca6d | 2008-08-05 11:27:30 -0700 | [diff] [blame] | 432 | if (!strcmp(var, "diff.renamelimit")) { |
| 433 | diff_rename_limit_default = git_config_int(var, value); |
| 434 | return 0; |
| 435 | } |
| 436 | |
Jeff King | 6680a08 | 2012-02-07 13:23:02 -0500 | [diff] [blame] | 437 | if (userdiff_config(var, value) < 0) |
| 438 | return -1; |
Jeff King | c7534ef | 2008-10-26 00:45:55 -0400 | [diff] [blame] | 439 | |
Jeff King | ae021d8 | 2014-06-18 15:47:50 -0400 | [diff] [blame] | 440 | if (skip_prefix(var, "diff.color.", &name) || |
| 441 | skip_prefix(var, "color.diff.", &name)) { |
| 442 | int slot = parse_diff_color_slot(name); |
Jeff King | 8b8e862 | 2009-12-12 07:25:24 -0500 | [diff] [blame] | 443 | if (slot < 0) |
| 444 | return 0; |
Junio C Hamano | 64f30e9 | 2008-02-11 10:53:56 -0800 | [diff] [blame] | 445 | if (!value) |
| 446 | return config_error_nonbool(var); |
Jeff King | f6c5a29 | 2014-10-07 15:33:09 -0400 | [diff] [blame] | 447 | return color_parse(value, diff_colors[slot]); |
Junio C Hamano | 801235c | 2006-06-24 04:06:23 -0700 | [diff] [blame] | 448 | } |
Junio C Hamano | f1af60b | 2007-04-22 17:52:55 -0700 | [diff] [blame] | 449 | |
Jeff King | da80635 | 2020-01-31 04:57:49 -0500 | [diff] [blame] | 450 | if (!strcmp(var, "diff.wserrorhighlight")) { |
| 451 | int val = parse_ws_error_highlight(value); |
| 452 | if (val < 0) |
| 453 | return -1; |
| 454 | ws_error_highlight_default = val; |
| 455 | return 0; |
| 456 | } |
| 457 | |
Jim Meyering | a624eaa | 2008-08-15 13:39:26 +0200 | [diff] [blame] | 458 | /* like GNU diff's --suppress-blank-empty option */ |
Johannes Schindelin | 950db87 | 2009-01-20 22:08:33 +0100 | [diff] [blame] | 459 | if (!strcmp(var, "diff.suppressblankempty") || |
| 460 | /* for backwards compatibility */ |
| 461 | !strcmp(var, "diff.suppress-blank-empty")) { |
Jim Meyering | a624eaa | 2008-08-15 13:39:26 +0200 | [diff] [blame] | 462 | diff_suppress_blank_empty = git_config_bool(var, value); |
| 463 | return 0; |
| 464 | } |
| 465 | |
Johan Herland | 2d17495 | 2011-04-29 11:36:19 +0200 | [diff] [blame] | 466 | if (!strcmp(var, "diff.dirstat")) { |
Johan Herland | 51670fc | 2011-04-29 11:36:22 +0200 | [diff] [blame] | 467 | struct strbuf errmsg = STRBUF_INIT; |
Johan Herland | 712d2c7 | 2011-04-29 11:36:20 +0200 | [diff] [blame] | 468 | default_diff_options.dirstat_permille = diff_dirstat_permille_default; |
Johan Herland | 51670fc | 2011-04-29 11:36:22 +0200 | [diff] [blame] | 469 | if (parse_dirstat_params(&default_diff_options, value, &errmsg)) |
Johan Herland | 7478ac5 | 2011-04-29 11:36:23 +0200 | [diff] [blame] | 470 | warning(_("Found errors in 'diff.dirstat' config variable:\n%s"), |
Johan Herland | 51670fc | 2011-04-29 11:36:22 +0200 | [diff] [blame] | 471 | errmsg.buf); |
| 472 | strbuf_release(&errmsg); |
Johan Herland | 712d2c7 | 2011-04-29 11:36:20 +0200 | [diff] [blame] | 473 | diff_dirstat_permille_default = default_diff_options.dirstat_permille; |
Johan Herland | 2d17495 | 2011-04-29 11:36:19 +0200 | [diff] [blame] | 474 | return 0; |
| 475 | } |
| 476 | |
Marc Branchaud | cf5e772 | 2017-05-08 12:03:36 -0400 | [diff] [blame] | 477 | if (git_diff_heuristic_config(var, value, cb) < 0) |
| 478 | return -1; |
| 479 | |
Jeff King | 3e1dd17 | 2011-08-17 22:05:08 -0700 | [diff] [blame] | 480 | return git_default_config(var, value, cb); |
Junio C Hamano | 801235c | 2006-06-24 04:06:23 -0700 | [diff] [blame] | 481 | } |
| 482 | |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 483 | static char *quote_two(const char *one, const char *two) |
| 484 | { |
| 485 | int need_one = quote_c_style(one, NULL, NULL, 1); |
| 486 | int need_two = quote_c_style(two, NULL, NULL, 1); |
Brandon Casey | f285a2d | 2008-10-09 14:12:12 -0500 | [diff] [blame] | 487 | struct strbuf res = STRBUF_INIT; |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 488 | |
| 489 | if (need_one + need_two) { |
Pierre Habouzit | 663af34 | 2007-09-20 00:42:15 +0200 | [diff] [blame] | 490 | strbuf_addch(&res, '"'); |
| 491 | quote_c_style(one, &res, NULL, 1); |
| 492 | quote_c_style(two, &res, NULL, 1); |
| 493 | strbuf_addch(&res, '"'); |
| 494 | } else { |
| 495 | strbuf_addstr(&res, one); |
| 496 | strbuf_addstr(&res, two); |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 497 | } |
Pierre Habouzit | b315c5c | 2007-09-27 12:58:23 +0200 | [diff] [blame] | 498 | return strbuf_detach(&res, NULL); |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 499 | } |
| 500 | |
| 501 | static const char *external_diff(void) |
| 502 | { |
| 503 | static const char *external_diff_cmd = NULL; |
| 504 | static int done_preparing = 0; |
| 505 | |
| 506 | if (done_preparing) |
| 507 | return external_diff_cmd; |
Kim Gybels | 6776a84 | 2019-01-11 21:26:08 +0100 | [diff] [blame] | 508 | external_diff_cmd = xstrdup_or_null(getenv("GIT_EXTERNAL_DIFF")); |
Johannes Schindelin | cbe0210 | 2007-12-17 13:42:20 +0000 | [diff] [blame] | 509 | if (!external_diff_cmd) |
| 510 | external_diff_cmd = external_diff_cmd_cfg; |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 511 | done_preparing = 1; |
| 512 | return external_diff_cmd; |
| 513 | } |
| 514 | |
Michael Haggerty | 284098f | 2015-08-12 19:12:01 +0200 | [diff] [blame] | 515 | /* |
| 516 | * Keep track of files used for diffing. Sometimes such an entry |
| 517 | * refers to a temporary file, sometimes to an existing file, and |
| 518 | * sometimes to "/dev/null". |
| 519 | */ |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 520 | static struct diff_tempfile { |
Michael Haggerty | 284098f | 2015-08-12 19:12:01 +0200 | [diff] [blame] | 521 | /* |
| 522 | * filename external diff should read from, or NULL if this |
| 523 | * entry is currently not in use: |
| 524 | */ |
| 525 | const char *name; |
| 526 | |
brian m. carlson | dc01505 | 2017-03-26 16:01:24 +0000 | [diff] [blame] | 527 | char hex[GIT_MAX_HEXSZ + 1]; |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 528 | char mode[10]; |
Michael Haggerty | 284098f | 2015-08-12 19:12:01 +0200 | [diff] [blame] | 529 | |
| 530 | /* |
| 531 | * If this diff_tempfile instance refers to a temporary file, |
| 532 | * this tempfile object is used to manage its lifetime. |
| 533 | */ |
Jeff King | 076aa2c | 2017-09-05 08:15:08 -0400 | [diff] [blame] | 534 | struct tempfile *tempfile; |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 535 | } diff_temp[2]; |
| 536 | |
Junio C Hamano | 6957eb9 | 2009-09-14 18:44:01 -0700 | [diff] [blame] | 537 | struct emit_callback { |
Junio C Hamano | 6957eb9 | 2009-09-14 18:44:01 -0700 | [diff] [blame] | 538 | int color_diff; |
| 539 | unsigned ws_rule; |
| 540 | int blank_at_eof_in_preimage; |
| 541 | int blank_at_eof_in_postimage; |
| 542 | int lno_in_preimage; |
| 543 | int lno_in_postimage; |
Junio C Hamano | 6957eb9 | 2009-09-14 18:44:01 -0700 | [diff] [blame] | 544 | const char **label_path; |
| 545 | struct diff_words_data *diff_words; |
Bo Yang | a3c158d | 2010-05-26 15:08:02 +0800 | [diff] [blame] | 546 | struct diff_options *opt; |
Greg Bacon | 3e97c7c | 2009-11-19 15:12:24 -0600 | [diff] [blame] | 547 | struct strbuf *header; |
Junio C Hamano | 6957eb9 | 2009-09-14 18:44:01 -0700 | [diff] [blame] | 548 | }; |
| 549 | |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 550 | static int count_lines(const char *data, int size) |
| 551 | { |
| 552 | int count, ch, completely_empty = 1, nl_just_seen = 0; |
| 553 | count = 0; |
| 554 | while (0 < size--) { |
| 555 | ch = *data++; |
| 556 | if (ch == '\n') { |
| 557 | count++; |
| 558 | nl_just_seen = 1; |
| 559 | completely_empty = 0; |
| 560 | } |
| 561 | else { |
| 562 | nl_just_seen = 0; |
| 563 | completely_empty = 0; |
| 564 | } |
| 565 | } |
| 566 | if (completely_empty) |
| 567 | return 0; |
| 568 | if (!nl_just_seen) |
| 569 | count++; /* no trailing newline */ |
| 570 | return count; |
| 571 | } |
| 572 | |
Nguyễn Thái Ngọc Duy | b78ea5f | 2018-09-21 17:57:19 +0200 | [diff] [blame] | 573 | static int fill_mmfile(struct repository *r, mmfile_t *mf, |
| 574 | struct diff_filespec *one) |
Junio C Hamano | 6957eb9 | 2009-09-14 18:44:01 -0700 | [diff] [blame] | 575 | { |
| 576 | if (!DIFF_FILE_VALID(one)) { |
| 577 | mf->ptr = (char *)""; /* does not matter */ |
| 578 | mf->size = 0; |
| 579 | return 0; |
| 580 | } |
Jonathan Tan | 1c37e86 | 2020-04-07 15:11:41 -0700 | [diff] [blame] | 581 | else if (diff_populate_filespec(r, one, NULL)) |
Junio C Hamano | 6957eb9 | 2009-09-14 18:44:01 -0700 | [diff] [blame] | 582 | return -1; |
Junio C Hamano | bb35fef | 2009-09-15 03:38:30 -0700 | [diff] [blame] | 583 | |
Junio C Hamano | 6957eb9 | 2009-09-14 18:44:01 -0700 | [diff] [blame] | 584 | mf->ptr = one->data; |
| 585 | mf->size = one->size; |
| 586 | return 0; |
| 587 | } |
| 588 | |
Jeff King | abb371a | 2011-02-19 03:16:32 -0500 | [diff] [blame] | 589 | /* like fill_mmfile, but only for size, so we can avoid retrieving blob */ |
Nguyễn Thái Ngọc Duy | b78ea5f | 2018-09-21 17:57:19 +0200 | [diff] [blame] | 590 | static unsigned long diff_filespec_size(struct repository *r, |
| 591 | struct diff_filespec *one) |
Jeff King | abb371a | 2011-02-19 03:16:32 -0500 | [diff] [blame] | 592 | { |
Jonathan Tan | 1c37e86 | 2020-04-07 15:11:41 -0700 | [diff] [blame] | 593 | struct diff_populate_filespec_options dpf_options = { |
| 594 | .check_size_only = 1, |
| 595 | }; |
| 596 | |
Jeff King | abb371a | 2011-02-19 03:16:32 -0500 | [diff] [blame] | 597 | if (!DIFF_FILE_VALID(one)) |
| 598 | return 0; |
Jonathan Tan | 1c37e86 | 2020-04-07 15:11:41 -0700 | [diff] [blame] | 599 | diff_populate_filespec(r, one, &dpf_options); |
Jeff King | abb371a | 2011-02-19 03:16:32 -0500 | [diff] [blame] | 600 | return one->size; |
| 601 | } |
| 602 | |
Junio C Hamano | 6957eb9 | 2009-09-14 18:44:01 -0700 | [diff] [blame] | 603 | static int count_trailing_blank(mmfile_t *mf, unsigned ws_rule) |
| 604 | { |
| 605 | char *ptr = mf->ptr; |
| 606 | long size = mf->size; |
| 607 | int cnt = 0; |
| 608 | |
| 609 | if (!size) |
| 610 | return cnt; |
| 611 | ptr += size - 1; /* pointing at the very end */ |
| 612 | if (*ptr != '\n') |
| 613 | ; /* incomplete line */ |
| 614 | else |
| 615 | ptr--; /* skip the last LF */ |
| 616 | while (mf->ptr < ptr) { |
| 617 | char *prev_eol; |
| 618 | for (prev_eol = ptr; mf->ptr <= prev_eol; prev_eol--) |
| 619 | if (*prev_eol == '\n') |
| 620 | break; |
| 621 | if (!ws_blank_line(prev_eol + 1, ptr - prev_eol, ws_rule)) |
| 622 | break; |
| 623 | cnt++; |
| 624 | ptr = prev_eol - 1; |
| 625 | } |
| 626 | return cnt; |
| 627 | } |
| 628 | |
| 629 | static void check_blank_at_eof(mmfile_t *mf1, mmfile_t *mf2, |
| 630 | struct emit_callback *ecbdata) |
| 631 | { |
| 632 | int l1, l2, at; |
| 633 | unsigned ws_rule = ecbdata->ws_rule; |
| 634 | l1 = count_trailing_blank(mf1, ws_rule); |
| 635 | l2 = count_trailing_blank(mf2, ws_rule); |
| 636 | if (l2 <= l1) { |
| 637 | ecbdata->blank_at_eof_in_preimage = 0; |
| 638 | ecbdata->blank_at_eof_in_postimage = 0; |
| 639 | return; |
| 640 | } |
| 641 | at = count_lines(mf1->ptr, mf1->size); |
| 642 | ecbdata->blank_at_eof_in_preimage = (at - l1) + 1; |
| 643 | |
| 644 | at = count_lines(mf2->ptr, mf2->size); |
| 645 | ecbdata->blank_at_eof_in_postimage = (at - l2) + 1; |
| 646 | } |
| 647 | |
Johannes Schindelin | f7c3b4e | 2018-08-13 04:33:20 -0700 | [diff] [blame] | 648 | static void emit_line_0(struct diff_options *o, |
Stefan Beller | 017ac45 | 2018-08-13 18:41:19 -0700 | [diff] [blame] | 649 | const char *set_sign, const char *set, unsigned reverse, const char *reset, |
Junio C Hamano | 250f799 | 2009-09-14 18:44:01 -0700 | [diff] [blame] | 650 | int first, const char *line, int len) |
Junio C Hamano | 6957eb9 | 2009-09-14 18:44:01 -0700 | [diff] [blame] | 651 | { |
| 652 | int has_trailing_newline, has_trailing_carriage_return; |
Stefan Beller | 4441067 | 2018-08-13 18:41:22 -0700 | [diff] [blame] | 653 | int needs_reset = 0; /* at the end of the line */ |
Bo Yang | a3c158d | 2010-05-26 15:08:02 +0800 | [diff] [blame] | 654 | FILE *file = o->file; |
| 655 | |
Stefan Beller | f103a6f | 2018-08-13 18:41:21 -0700 | [diff] [blame] | 656 | fputs(diff_line_prefix(o), file); |
Junio C Hamano | 6957eb9 | 2009-09-14 18:44:01 -0700 | [diff] [blame] | 657 | |
Stefan Beller | 4441067 | 2018-08-13 18:41:22 -0700 | [diff] [blame] | 658 | has_trailing_newline = (len > 0 && line[len-1] == '\n'); |
| 659 | if (has_trailing_newline) |
| 660 | len--; |
| 661 | |
| 662 | has_trailing_carriage_return = (len > 0 && line[len-1] == '\r'); |
| 663 | if (has_trailing_carriage_return) |
| 664 | len--; |
| 665 | |
| 666 | if (!len && !first) |
| 667 | goto end_of_line; |
| 668 | |
| 669 | if (reverse && want_color(o->use_color)) { |
| 670 | fputs(GIT_COLOR_REVERSE, file); |
| 671 | needs_reset = 1; |
Junio C Hamano | 250f799 | 2009-09-14 18:44:01 -0700 | [diff] [blame] | 672 | } |
Junio C Hamano | 6957eb9 | 2009-09-14 18:44:01 -0700 | [diff] [blame] | 673 | |
Stefan Beller | 4441067 | 2018-08-13 18:41:22 -0700 | [diff] [blame] | 674 | if (set_sign) { |
| 675 | fputs(set_sign, file); |
| 676 | needs_reset = 1; |
Junio C Hamano | 06a4755 | 2009-11-27 22:04:10 -0800 | [diff] [blame] | 677 | } |
Stefan Beller | 4441067 | 2018-08-13 18:41:22 -0700 | [diff] [blame] | 678 | |
Junio C Hamano | 6957eb9 | 2009-09-14 18:44:01 -0700 | [diff] [blame] | 679 | if (first) |
Stefan Beller | 4441067 | 2018-08-13 18:41:22 -0700 | [diff] [blame] | 680 | fputc(first, file); |
Junio C Hamano | 6957eb9 | 2009-09-14 18:44:01 -0700 | [diff] [blame] | 681 | |
Stefan Beller | 4441067 | 2018-08-13 18:41:22 -0700 | [diff] [blame] | 682 | if (!len) |
| 683 | goto end_of_line; |
Junio C Hamano | 6957eb9 | 2009-09-14 18:44:01 -0700 | [diff] [blame] | 684 | |
Stefan Beller | 4441067 | 2018-08-13 18:41:22 -0700 | [diff] [blame] | 685 | if (set) { |
| 686 | if (set_sign && set != set_sign) |
| 687 | fputs(reset, file); |
Junio C Hamano | 250f799 | 2009-09-14 18:44:01 -0700 | [diff] [blame] | 688 | fputs(set, file); |
Stefan Beller | 4441067 | 2018-08-13 18:41:22 -0700 | [diff] [blame] | 689 | needs_reset = 1; |
Junio C Hamano | 6957eb9 | 2009-09-14 18:44:01 -0700 | [diff] [blame] | 690 | } |
Stefan Beller | 4441067 | 2018-08-13 18:41:22 -0700 | [diff] [blame] | 691 | fwrite(line, len, 1, file); |
| 692 | needs_reset = 1; /* 'line' may contain color codes. */ |
| 693 | |
| 694 | end_of_line: |
| 695 | if (needs_reset) |
| 696 | fputs(reset, file); |
Junio C Hamano | 6957eb9 | 2009-09-14 18:44:01 -0700 | [diff] [blame] | 697 | if (has_trailing_carriage_return) |
| 698 | fputc('\r', file); |
| 699 | if (has_trailing_newline) |
| 700 | fputc('\n', file); |
| 701 | } |
| 702 | |
Bo Yang | a3c158d | 2010-05-26 15:08:02 +0800 | [diff] [blame] | 703 | static void emit_line(struct diff_options *o, const char *set, const char *reset, |
Junio C Hamano | 250f799 | 2009-09-14 18:44:01 -0700 | [diff] [blame] | 704 | const char *line, int len) |
| 705 | { |
Stefan Beller | 4441067 | 2018-08-13 18:41:22 -0700 | [diff] [blame] | 706 | emit_line_0(o, set, NULL, 0, reset, 0, line, len); |
Junio C Hamano | 250f799 | 2009-09-14 18:44:01 -0700 | [diff] [blame] | 707 | } |
| 708 | |
Stefan Beller | 36a4cef | 2017-06-29 17:06:49 -0700 | [diff] [blame] | 709 | enum diff_symbol { |
Stefan Beller | 4eed0eb | 2017-06-29 17:07:01 -0700 | [diff] [blame] | 710 | DIFF_SYMBOL_BINARY_DIFF_HEADER, |
| 711 | DIFF_SYMBOL_BINARY_DIFF_HEADER_DELTA, |
| 712 | DIFF_SYMBOL_BINARY_DIFF_HEADER_LITERAL, |
| 713 | DIFF_SYMBOL_BINARY_DIFF_BODY, |
| 714 | DIFF_SYMBOL_BINARY_DIFF_FOOTER, |
Stefan Beller | 0911c47 | 2017-06-29 17:07:02 -0700 | [diff] [blame] | 715 | DIFF_SYMBOL_STATS_SUMMARY_NO_FILES, |
| 716 | DIFF_SYMBOL_STATS_SUMMARY_ABBREV, |
| 717 | DIFF_SYMBOL_STATS_SUMMARY_INSERTS_DELETES, |
| 718 | DIFF_SYMBOL_STATS_LINE, |
Stefan Beller | bd03329 | 2017-06-29 17:07:03 -0700 | [diff] [blame] | 719 | DIFF_SYMBOL_WORD_DIFF, |
Stefan Beller | 30b7e1e | 2017-06-29 17:07:04 -0700 | [diff] [blame] | 720 | DIFF_SYMBOL_STAT_SEP, |
Stefan Beller | 146fdb0 | 2017-06-29 17:07:05 -0700 | [diff] [blame] | 721 | DIFF_SYMBOL_SUMMARY, |
Stefan Beller | f359713 | 2017-06-29 17:07:00 -0700 | [diff] [blame] | 722 | DIFF_SYMBOL_SUBMODULE_ADD, |
| 723 | DIFF_SYMBOL_SUBMODULE_DEL, |
| 724 | DIFF_SYMBOL_SUBMODULE_UNTRACKED, |
| 725 | DIFF_SYMBOL_SUBMODULE_MODIFIED, |
| 726 | DIFF_SYMBOL_SUBMODULE_HEADER, |
| 727 | DIFF_SYMBOL_SUBMODULE_ERROR, |
| 728 | DIFF_SYMBOL_SUBMODULE_PIPETHROUGH, |
Stefan Beller | 5af6ea9 | 2017-06-29 17:06:59 -0700 | [diff] [blame] | 729 | DIFF_SYMBOL_REWRITE_DIFF, |
Stefan Beller | 4acaaa7 | 2017-06-29 17:06:58 -0700 | [diff] [blame] | 730 | DIFF_SYMBOL_BINARY_FILES, |
Stefan Beller | a29b0a1 | 2017-06-29 17:06:57 -0700 | [diff] [blame] | 731 | DIFF_SYMBOL_HEADER, |
Stefan Beller | 3ee8b7b | 2017-06-29 17:06:56 -0700 | [diff] [blame] | 732 | DIFF_SYMBOL_FILEPAIR_PLUS, |
| 733 | DIFF_SYMBOL_FILEPAIR_MINUS, |
Stefan Beller | ff95867 | 2017-06-29 17:06:54 -0700 | [diff] [blame] | 734 | DIFF_SYMBOL_WORDS_PORCELAIN, |
| 735 | DIFF_SYMBOL_WORDS, |
Stefan Beller | 091f8e2 | 2017-06-29 17:06:53 -0700 | [diff] [blame] | 736 | DIFF_SYMBOL_CONTEXT, |
Stefan Beller | f2bb121 | 2017-06-29 17:06:55 -0700 | [diff] [blame] | 737 | DIFF_SYMBOL_CONTEXT_INCOMPLETE, |
Stefan Beller | 091f8e2 | 2017-06-29 17:06:53 -0700 | [diff] [blame] | 738 | DIFF_SYMBOL_PLUS, |
| 739 | DIFF_SYMBOL_MINUS, |
Stefan Beller | b9cbfde | 2017-06-29 17:06:52 -0700 | [diff] [blame] | 740 | DIFF_SYMBOL_NO_LF_EOF, |
Stefan Beller | 68abc6f | 2017-06-29 17:06:51 -0700 | [diff] [blame] | 741 | DIFF_SYMBOL_CONTEXT_FRAGINFO, |
Stefan Beller | c64b420 | 2017-06-29 17:06:50 -0700 | [diff] [blame] | 742 | DIFF_SYMBOL_CONTEXT_MARKER, |
Stefan Beller | 36a4cef | 2017-06-29 17:06:49 -0700 | [diff] [blame] | 743 | DIFF_SYMBOL_SEPARATOR |
| 744 | }; |
Stefan Beller | 091f8e2 | 2017-06-29 17:06:53 -0700 | [diff] [blame] | 745 | /* |
| 746 | * Flags for content lines: |
| 747 | * 0..12 are whitespace rules |
| 748 | * 13-15 are WSEH_NEW | WSEH_OLD | WSEH_CONTEXT |
| 749 | * 16 is marking if the line is blank at EOF |
| 750 | */ |
Stefan Beller | 2e2d5ac | 2017-06-30 13:53:07 -0700 | [diff] [blame] | 751 | #define DIFF_SYMBOL_CONTENT_BLANK_LINE_EOF (1<<16) |
| 752 | #define DIFF_SYMBOL_MOVED_LINE (1<<17) |
| 753 | #define DIFF_SYMBOL_MOVED_LINE_ALT (1<<18) |
Stefan Beller | 86b452e | 2017-06-30 13:53:09 -0700 | [diff] [blame] | 754 | #define DIFF_SYMBOL_MOVED_LINE_UNINTERESTING (1<<19) |
Stefan Beller | 091f8e2 | 2017-06-29 17:06:53 -0700 | [diff] [blame] | 755 | #define DIFF_SYMBOL_CONTENT_WS_MASK (WSEH_NEW | WSEH_OLD | WSEH_CONTEXT | WS_RULE_MASK) |
| 756 | |
Stefan Beller | e6e045f | 2017-06-29 17:07:06 -0700 | [diff] [blame] | 757 | /* |
| 758 | * This struct is used when we need to buffer the output of the diff output. |
| 759 | * |
| 760 | * NEEDSWORK: Instead of storing a copy of the line, add an offset pointer |
| 761 | * into the pre/post image file. This pointer could be a union with the |
| 762 | * line pointer. By storing an offset into the file instead of the literal line, |
| 763 | * we can decrease the memory footprint for the buffered output. At first we |
| 764 | * may want to only have indirection for the content lines, but we could also |
| 765 | * enhance the state for emitting prefabricated lines, e.g. the similarity |
| 766 | * score line or hunk/file headers would only need to store a number or path |
| 767 | * and then the output can be constructed later on depending on state. |
| 768 | */ |
| 769 | struct emitted_diff_symbol { |
| 770 | const char *line; |
| 771 | int len; |
| 772 | int flags; |
Phillip Wood | 21536d0 | 2018-11-23 11:16:57 +0000 | [diff] [blame] | 773 | int indent_off; /* Offset to first non-whitespace character */ |
| 774 | int indent_width; /* The visual width of the indentation */ |
Stefan Beller | e6e045f | 2017-06-29 17:07:06 -0700 | [diff] [blame] | 775 | enum diff_symbol s; |
| 776 | }; |
| 777 | #define EMITTED_DIFF_SYMBOL_INIT {NULL} |
| 778 | |
| 779 | struct emitted_diff_symbols { |
| 780 | struct emitted_diff_symbol *buf; |
| 781 | int nr, alloc; |
| 782 | }; |
| 783 | #define EMITTED_DIFF_SYMBOLS_INIT {NULL, 0, 0} |
| 784 | |
| 785 | static void append_emitted_diff_symbol(struct diff_options *o, |
| 786 | struct emitted_diff_symbol *e) |
| 787 | { |
| 788 | struct emitted_diff_symbol *f; |
| 789 | |
| 790 | ALLOC_GROW(o->emitted_symbols->buf, |
| 791 | o->emitted_symbols->nr + 1, |
| 792 | o->emitted_symbols->alloc); |
| 793 | f = &o->emitted_symbols->buf[o->emitted_symbols->nr++]; |
| 794 | |
| 795 | memcpy(f, e, sizeof(struct emitted_diff_symbol)); |
| 796 | f->line = e->line ? xmemdupz(e->line, e->len) : NULL; |
| 797 | } |
| 798 | |
Stefan Beller | 2e2d5ac | 2017-06-30 13:53:07 -0700 | [diff] [blame] | 799 | struct moved_entry { |
| 800 | struct hashmap_entry ent; |
| 801 | const struct emitted_diff_symbol *es; |
| 802 | struct moved_entry *next_line; |
| 803 | }; |
| 804 | |
Phillip Wood | 74d156f | 2018-10-04 11:07:41 +0100 | [diff] [blame] | 805 | struct moved_block { |
| 806 | struct moved_entry *match; |
Phillip Wood | 21536d0 | 2018-11-23 11:16:57 +0000 | [diff] [blame] | 807 | int wsd; /* The whitespace delta of this block */ |
Phillip Wood | 74d156f | 2018-10-04 11:07:41 +0100 | [diff] [blame] | 808 | }; |
| 809 | |
| 810 | static void moved_block_clear(struct moved_block *b) |
| 811 | { |
Phillip Wood | 21536d0 | 2018-11-23 11:16:57 +0000 | [diff] [blame] | 812 | memset(b, 0, sizeof(*b)); |
| 813 | } |
| 814 | |
Phillip Wood | 0cd51e9 | 2018-11-23 11:16:58 +0000 | [diff] [blame] | 815 | #define INDENT_BLANKLINE INT_MIN |
| 816 | |
Phillip Wood | 21536d0 | 2018-11-23 11:16:57 +0000 | [diff] [blame] | 817 | static void fill_es_indent_data(struct emitted_diff_symbol *es) |
| 818 | { |
Phillip Wood | 0cd51e9 | 2018-11-23 11:16:58 +0000 | [diff] [blame] | 819 | unsigned int off = 0, i; |
Phillip Wood | 21536d0 | 2018-11-23 11:16:57 +0000 | [diff] [blame] | 820 | int width = 0, tab_width = es->flags & WS_TAB_WIDTH_MASK; |
| 821 | const char *s = es->line; |
| 822 | const int len = es->len; |
| 823 | |
| 824 | /* skip any \v \f \r at start of indentation */ |
| 825 | while (s[off] == '\f' || s[off] == '\v' || |
| 826 | (s[off] == '\r' && off < len - 1)) |
| 827 | off++; |
| 828 | |
| 829 | /* calculate the visual width of indentation */ |
| 830 | while(1) { |
| 831 | if (s[off] == ' ') { |
| 832 | width++; |
| 833 | off++; |
| 834 | } else if (s[off] == '\t') { |
| 835 | width += tab_width - (width % tab_width); |
| 836 | while (s[++off] == '\t') |
| 837 | width += tab_width; |
| 838 | } else { |
| 839 | break; |
| 840 | } |
| 841 | } |
| 842 | |
Phillip Wood | 0cd51e9 | 2018-11-23 11:16:58 +0000 | [diff] [blame] | 843 | /* check if this line is blank */ |
| 844 | for (i = off; i < len; i++) |
| 845 | if (!isspace(s[i])) |
| 846 | break; |
| 847 | |
| 848 | if (i == len) { |
| 849 | es->indent_width = INDENT_BLANKLINE; |
| 850 | es->indent_off = len; |
| 851 | } else { |
| 852 | es->indent_off = off; |
| 853 | es->indent_width = width; |
| 854 | } |
Phillip Wood | 74d156f | 2018-10-04 11:07:41 +0100 | [diff] [blame] | 855 | } |
| 856 | |
Stefan Beller | ca1f4ae | 2018-07-18 12:31:55 -0700 | [diff] [blame] | 857 | static int compute_ws_delta(const struct emitted_diff_symbol *a, |
Phillip Wood | 21536d0 | 2018-11-23 11:16:57 +0000 | [diff] [blame] | 858 | const struct emitted_diff_symbol *b, |
| 859 | int *out) |
Stefan Beller | ca1f4ae | 2018-07-18 12:31:55 -0700 | [diff] [blame] | 860 | { |
Phillip Wood | 21536d0 | 2018-11-23 11:16:57 +0000 | [diff] [blame] | 861 | int a_len = a->len, |
| 862 | b_len = b->len, |
| 863 | a_off = a->indent_off, |
| 864 | a_width = a->indent_width, |
| 865 | b_off = b->indent_off, |
| 866 | b_width = b->indent_width; |
| 867 | int delta; |
Stefan Beller | ca1f4ae | 2018-07-18 12:31:55 -0700 | [diff] [blame] | 868 | |
Phillip Wood | 0cd51e9 | 2018-11-23 11:16:58 +0000 | [diff] [blame] | 869 | if (a_width == INDENT_BLANKLINE && b_width == INDENT_BLANKLINE) { |
| 870 | *out = INDENT_BLANKLINE; |
| 871 | return 1; |
| 872 | } |
| 873 | |
Phillip Wood | 21536d0 | 2018-11-23 11:16:57 +0000 | [diff] [blame] | 874 | if (a->s == DIFF_SYMBOL_PLUS) |
| 875 | delta = a_width - b_width; |
| 876 | else |
| 877 | delta = b_width - a_width; |
| 878 | |
| 879 | if (a_len - a_off != b_len - b_off || |
| 880 | memcmp(a->line + a_off, b->line + b_off, a_len - a_off)) |
Phillip Wood | fe4516d | 2018-10-04 11:07:43 +0100 | [diff] [blame] | 881 | return 0; |
| 882 | |
Phillip Wood | 21536d0 | 2018-11-23 11:16:57 +0000 | [diff] [blame] | 883 | *out = delta; |
Stefan Beller | ca1f4ae | 2018-07-18 12:31:55 -0700 | [diff] [blame] | 884 | |
Phillip Wood | fe4516d | 2018-10-04 11:07:43 +0100 | [diff] [blame] | 885 | return 1; |
Stefan Beller | ca1f4ae | 2018-07-18 12:31:55 -0700 | [diff] [blame] | 886 | } |
| 887 | |
| 888 | static int cmp_in_block_with_wsd(const struct diff_options *o, |
| 889 | const struct moved_entry *cur, |
| 890 | const struct moved_entry *match, |
Phillip Wood | 74d156f | 2018-10-04 11:07:41 +0100 | [diff] [blame] | 891 | struct moved_block *pmb, |
Stefan Beller | ca1f4ae | 2018-07-18 12:31:55 -0700 | [diff] [blame] | 892 | int n) |
| 893 | { |
| 894 | struct emitted_diff_symbol *l = &o->emitted_symbols->buf[n]; |
Phillip Wood | 7a4252c | 2018-11-23 11:16:56 +0000 | [diff] [blame] | 895 | int al = cur->es->len, bl = match->es->len, cl = l->len; |
Stefan Beller | ca1f4ae | 2018-07-18 12:31:55 -0700 | [diff] [blame] | 896 | const char *a = cur->es->line, |
| 897 | *b = match->es->line, |
| 898 | *c = l->line; |
Phillip Wood | 21536d0 | 2018-11-23 11:16:57 +0000 | [diff] [blame] | 899 | int a_off = cur->es->indent_off, |
| 900 | a_width = cur->es->indent_width, |
| 901 | c_off = l->indent_off, |
| 902 | c_width = l->indent_width; |
| 903 | int delta; |
Stefan Beller | ca1f4ae | 2018-07-18 12:31:55 -0700 | [diff] [blame] | 904 | |
| 905 | /* |
Phillip Wood | 7a4252c | 2018-11-23 11:16:56 +0000 | [diff] [blame] | 906 | * We need to check if 'cur' is equal to 'match'. As those |
| 907 | * are from the same (+/-) side, we do not need to adjust for |
| 908 | * indent changes. However these were found using fuzzy |
| 909 | * matching so we do have to check if they are equal. Here we |
| 910 | * just check the lengths. We delay calling memcmp() to check |
| 911 | * the contents until later as if the length comparison for a |
| 912 | * and c fails we can avoid the call all together. |
Stefan Beller | ca1f4ae | 2018-07-18 12:31:55 -0700 | [diff] [blame] | 913 | */ |
Phillip Wood | 7a4252c | 2018-11-23 11:16:56 +0000 | [diff] [blame] | 914 | if (al != bl) |
Stefan Beller | ca1f4ae | 2018-07-18 12:31:55 -0700 | [diff] [blame] | 915 | return 1; |
| 916 | |
Phillip Wood | 0cd51e9 | 2018-11-23 11:16:58 +0000 | [diff] [blame] | 917 | /* If 'l' and 'cur' are both blank then they match. */ |
| 918 | if (a_width == INDENT_BLANKLINE && c_width == INDENT_BLANKLINE) |
| 919 | return 0; |
Stefan Beller | ca1f4ae | 2018-07-18 12:31:55 -0700 | [diff] [blame] | 920 | |
| 921 | /* |
Phillip Wood | 21536d0 | 2018-11-23 11:16:57 +0000 | [diff] [blame] | 922 | * The indent changes of the block are known and stored in pmb->wsd; |
| 923 | * however we need to check if the indent changes of the current line |
| 924 | * match those of the current block and that the text of 'l' and 'cur' |
| 925 | * after the indentation match. |
Stefan Beller | ca1f4ae | 2018-07-18 12:31:55 -0700 | [diff] [blame] | 926 | */ |
Phillip Wood | 21536d0 | 2018-11-23 11:16:57 +0000 | [diff] [blame] | 927 | if (cur->es->s == DIFF_SYMBOL_PLUS) |
| 928 | delta = a_width - c_width; |
| 929 | else |
| 930 | delta = c_width - a_width; |
Stefan Beller | ca1f4ae | 2018-07-18 12:31:55 -0700 | [diff] [blame] | 931 | |
Phillip Wood | 0cd51e9 | 2018-11-23 11:16:58 +0000 | [diff] [blame] | 932 | /* |
| 933 | * If the previous lines of this block were all blank then set its |
| 934 | * whitespace delta. |
| 935 | */ |
| 936 | if (pmb->wsd == INDENT_BLANKLINE) |
| 937 | pmb->wsd = delta; |
Stefan Beller | ca1f4ae | 2018-07-18 12:31:55 -0700 | [diff] [blame] | 938 | |
Phillip Wood | 21536d0 | 2018-11-23 11:16:57 +0000 | [diff] [blame] | 939 | return !(delta == pmb->wsd && al - a_off == cl - c_off && |
| 940 | !memcmp(a, b, al) && ! |
| 941 | memcmp(a + a_off, c + c_off, al - a_off)); |
Stefan Beller | ca1f4ae | 2018-07-18 12:31:55 -0700 | [diff] [blame] | 942 | } |
| 943 | |
Stefan Beller | ee1df66 | 2018-07-16 16:05:38 -0700 | [diff] [blame] | 944 | static int moved_entry_cmp(const void *hashmap_cmp_fn_data, |
Eric Wong | 939af16 | 2019-10-06 23:30:37 +0000 | [diff] [blame] | 945 | const struct hashmap_entry *eptr, |
| 946 | const struct hashmap_entry *entry_or_key, |
Stefan Beller | 2e2d5ac | 2017-06-30 13:53:07 -0700 | [diff] [blame] | 947 | const void *keydata) |
| 948 | { |
Stefan Beller | ee1df66 | 2018-07-16 16:05:38 -0700 | [diff] [blame] | 949 | const struct diff_options *diffopt = hashmap_cmp_fn_data; |
Eric Wong | 939af16 | 2019-10-06 23:30:37 +0000 | [diff] [blame] | 950 | const struct moved_entry *a, *b; |
Stefan Beller | b309571 | 2018-07-16 16:05:40 -0700 | [diff] [blame] | 951 | unsigned flags = diffopt->color_moved_ws_handling |
| 952 | & XDF_WHITESPACE_FLAGS; |
Stefan Beller | ee1df66 | 2018-07-16 16:05:38 -0700 | [diff] [blame] | 953 | |
Eric Wong | 939af16 | 2019-10-06 23:30:37 +0000 | [diff] [blame] | 954 | a = container_of(eptr, const struct moved_entry, ent); |
| 955 | b = container_of(entry_or_key, const struct moved_entry, ent); |
| 956 | |
Stefan Beller | ca1f4ae | 2018-07-18 12:31:55 -0700 | [diff] [blame] | 957 | if (diffopt->color_moved_ws_handling & |
| 958 | COLOR_MOVED_WS_ALLOW_INDENTATION_CHANGE) |
| 959 | /* |
| 960 | * As there is not specific white space config given, |
| 961 | * we'd need to check for a new block, so ignore all |
| 962 | * white space. The setup of the white space |
| 963 | * configuration for the next block is done else where |
| 964 | */ |
| 965 | flags |= XDF_IGNORE_WHITESPACE; |
| 966 | |
Stefan Beller | 01be97c | 2017-10-25 11:49:12 -0700 | [diff] [blame] | 967 | return !xdiff_compare_lines(a->es->line, a->es->len, |
| 968 | b->es->line, b->es->len, |
Stefan Beller | b309571 | 2018-07-16 16:05:40 -0700 | [diff] [blame] | 969 | flags); |
Stefan Beller | 2e2d5ac | 2017-06-30 13:53:07 -0700 | [diff] [blame] | 970 | } |
| 971 | |
| 972 | static struct moved_entry *prepare_entry(struct diff_options *o, |
| 973 | int line_no) |
| 974 | { |
| 975 | struct moved_entry *ret = xmalloc(sizeof(*ret)); |
| 976 | struct emitted_diff_symbol *l = &o->emitted_symbols->buf[line_no]; |
Stefan Beller | b309571 | 2018-07-16 16:05:40 -0700 | [diff] [blame] | 977 | unsigned flags = o->color_moved_ws_handling & XDF_WHITESPACE_FLAGS; |
Eric Wong | e010a41 | 2019-10-06 23:30:24 +0000 | [diff] [blame] | 978 | unsigned int hash = xdiff_hash_string(l->line, l->len, flags); |
Stefan Beller | 2e2d5ac | 2017-06-30 13:53:07 -0700 | [diff] [blame] | 979 | |
Eric Wong | e010a41 | 2019-10-06 23:30:24 +0000 | [diff] [blame] | 980 | hashmap_entry_init(&ret->ent, hash); |
Stefan Beller | 2e2d5ac | 2017-06-30 13:53:07 -0700 | [diff] [blame] | 981 | ret->es = l; |
| 982 | ret->next_line = NULL; |
| 983 | |
| 984 | return ret; |
| 985 | } |
| 986 | |
| 987 | static void add_lines_to_move_detection(struct diff_options *o, |
| 988 | struct hashmap *add_lines, |
| 989 | struct hashmap *del_lines) |
| 990 | { |
| 991 | struct moved_entry *prev_line = NULL; |
| 992 | |
| 993 | int n; |
| 994 | for (n = 0; n < o->emitted_symbols->nr; n++) { |
| 995 | struct hashmap *hm; |
| 996 | struct moved_entry *key; |
| 997 | |
| 998 | switch (o->emitted_symbols->buf[n].s) { |
| 999 | case DIFF_SYMBOL_PLUS: |
| 1000 | hm = add_lines; |
| 1001 | break; |
| 1002 | case DIFF_SYMBOL_MINUS: |
| 1003 | hm = del_lines; |
| 1004 | break; |
| 1005 | default: |
| 1006 | prev_line = NULL; |
| 1007 | continue; |
| 1008 | } |
| 1009 | |
Phillip Wood | 21536d0 | 2018-11-23 11:16:57 +0000 | [diff] [blame] | 1010 | if (o->color_moved_ws_handling & |
| 1011 | COLOR_MOVED_WS_ALLOW_INDENTATION_CHANGE) |
| 1012 | fill_es_indent_data(&o->emitted_symbols->buf[n]); |
Stefan Beller | 2e2d5ac | 2017-06-30 13:53:07 -0700 | [diff] [blame] | 1013 | key = prepare_entry(o, n); |
| 1014 | if (prev_line && prev_line->es->s == o->emitted_symbols->buf[n].s) |
| 1015 | prev_line->next_line = key; |
| 1016 | |
Eric Wong | b94e5c1 | 2019-10-06 23:30:29 +0000 | [diff] [blame] | 1017 | hashmap_add(hm, &key->ent); |
Stefan Beller | 2e2d5ac | 2017-06-30 13:53:07 -0700 | [diff] [blame] | 1018 | prev_line = key; |
| 1019 | } |
| 1020 | } |
| 1021 | |
Stefan Beller | e2fe6ab | 2018-07-16 16:05:41 -0700 | [diff] [blame] | 1022 | static void pmb_advance_or_null(struct diff_options *o, |
| 1023 | struct moved_entry *match, |
| 1024 | struct hashmap *hm, |
Phillip Wood | 74d156f | 2018-10-04 11:07:41 +0100 | [diff] [blame] | 1025 | struct moved_block *pmb, |
Stefan Beller | e2fe6ab | 2018-07-16 16:05:41 -0700 | [diff] [blame] | 1026 | int pmb_nr) |
| 1027 | { |
| 1028 | int i; |
| 1029 | for (i = 0; i < pmb_nr; i++) { |
Phillip Wood | 74d156f | 2018-10-04 11:07:41 +0100 | [diff] [blame] | 1030 | struct moved_entry *prev = pmb[i].match; |
Stefan Beller | e2fe6ab | 2018-07-16 16:05:41 -0700 | [diff] [blame] | 1031 | struct moved_entry *cur = (prev && prev->next_line) ? |
| 1032 | prev->next_line : NULL; |
Eric Wong | 939af16 | 2019-10-06 23:30:37 +0000 | [diff] [blame] | 1033 | if (cur && !hm->cmpfn(o, &cur->ent, &match->ent, NULL)) { |
Phillip Wood | 74d156f | 2018-10-04 11:07:41 +0100 | [diff] [blame] | 1034 | pmb[i].match = cur; |
Stefan Beller | e2fe6ab | 2018-07-16 16:05:41 -0700 | [diff] [blame] | 1035 | } else { |
Phillip Wood | 74d156f | 2018-10-04 11:07:41 +0100 | [diff] [blame] | 1036 | pmb[i].match = NULL; |
Stefan Beller | e2fe6ab | 2018-07-16 16:05:41 -0700 | [diff] [blame] | 1037 | } |
| 1038 | } |
| 1039 | } |
| 1040 | |
Stefan Beller | ca1f4ae | 2018-07-18 12:31:55 -0700 | [diff] [blame] | 1041 | static void pmb_advance_or_null_multi_match(struct diff_options *o, |
| 1042 | struct moved_entry *match, |
| 1043 | struct hashmap *hm, |
Phillip Wood | 74d156f | 2018-10-04 11:07:41 +0100 | [diff] [blame] | 1044 | struct moved_block *pmb, |
Stefan Beller | ca1f4ae | 2018-07-18 12:31:55 -0700 | [diff] [blame] | 1045 | int pmb_nr, int n) |
| 1046 | { |
| 1047 | int i; |
| 1048 | char *got_match = xcalloc(1, pmb_nr); |
| 1049 | |
Eric Wong | 23dee69 | 2019-10-06 23:30:41 +0000 | [diff] [blame] | 1050 | hashmap_for_each_entry_from(hm, match, ent) { |
Stefan Beller | ca1f4ae | 2018-07-18 12:31:55 -0700 | [diff] [blame] | 1051 | for (i = 0; i < pmb_nr; i++) { |
Phillip Wood | 74d156f | 2018-10-04 11:07:41 +0100 | [diff] [blame] | 1052 | struct moved_entry *prev = pmb[i].match; |
Stefan Beller | ca1f4ae | 2018-07-18 12:31:55 -0700 | [diff] [blame] | 1053 | struct moved_entry *cur = (prev && prev->next_line) ? |
| 1054 | prev->next_line : NULL; |
| 1055 | if (!cur) |
| 1056 | continue; |
Phillip Wood | 74d156f | 2018-10-04 11:07:41 +0100 | [diff] [blame] | 1057 | if (!cmp_in_block_with_wsd(o, cur, match, &pmb[i], n)) |
Stefan Beller | ca1f4ae | 2018-07-18 12:31:55 -0700 | [diff] [blame] | 1058 | got_match[i] |= 1; |
| 1059 | } |
| 1060 | } |
| 1061 | |
| 1062 | for (i = 0; i < pmb_nr; i++) { |
| 1063 | if (got_match[i]) { |
Phillip Wood | 74d156f | 2018-10-04 11:07:41 +0100 | [diff] [blame] | 1064 | /* Advance to the next line */ |
| 1065 | pmb[i].match = pmb[i].match->next_line; |
Phillip Wood | fab01ec | 2018-09-04 14:52:58 +0100 | [diff] [blame] | 1066 | } else { |
Phillip Wood | 74d156f | 2018-10-04 11:07:41 +0100 | [diff] [blame] | 1067 | moved_block_clear(&pmb[i]); |
Phillip Wood | fab01ec | 2018-09-04 14:52:58 +0100 | [diff] [blame] | 1068 | } |
Stefan Beller | ca1f4ae | 2018-07-18 12:31:55 -0700 | [diff] [blame] | 1069 | } |
Phillip Wood | 9c1a6c2 | 2018-10-04 11:07:44 +0100 | [diff] [blame] | 1070 | |
| 1071 | free(got_match); |
Stefan Beller | ca1f4ae | 2018-07-18 12:31:55 -0700 | [diff] [blame] | 1072 | } |
| 1073 | |
Phillip Wood | 74d156f | 2018-10-04 11:07:41 +0100 | [diff] [blame] | 1074 | static int shrink_potential_moved_blocks(struct moved_block *pmb, |
Stefan Beller | 2e2d5ac | 2017-06-30 13:53:07 -0700 | [diff] [blame] | 1075 | int pmb_nr) |
| 1076 | { |
| 1077 | int lp, rp; |
| 1078 | |
| 1079 | /* Shrink the set of potential block to the remaining running */ |
| 1080 | for (lp = 0, rp = pmb_nr - 1; lp <= rp;) { |
Phillip Wood | 74d156f | 2018-10-04 11:07:41 +0100 | [diff] [blame] | 1081 | while (lp < pmb_nr && pmb[lp].match) |
Stefan Beller | 2e2d5ac | 2017-06-30 13:53:07 -0700 | [diff] [blame] | 1082 | lp++; |
| 1083 | /* lp points at the first NULL now */ |
| 1084 | |
Phillip Wood | 74d156f | 2018-10-04 11:07:41 +0100 | [diff] [blame] | 1085 | while (rp > -1 && !pmb[rp].match) |
Stefan Beller | 2e2d5ac | 2017-06-30 13:53:07 -0700 | [diff] [blame] | 1086 | rp--; |
| 1087 | /* rp points at the last non-NULL */ |
| 1088 | |
| 1089 | if (lp < pmb_nr && rp > -1 && lp < rp) { |
| 1090 | pmb[lp] = pmb[rp]; |
Phillip Wood | 21536d0 | 2018-11-23 11:16:57 +0000 | [diff] [blame] | 1091 | memset(&pmb[rp], 0, sizeof(pmb[rp])); |
Stefan Beller | 2e2d5ac | 2017-06-30 13:53:07 -0700 | [diff] [blame] | 1092 | rp--; |
| 1093 | lp++; |
| 1094 | } |
| 1095 | } |
| 1096 | |
| 1097 | /* Remember the number of running sets */ |
| 1098 | return rp + 1; |
| 1099 | } |
| 1100 | |
Jonathan Tan | 0915327 | 2017-08-15 18:27:38 -0700 | [diff] [blame] | 1101 | /* |
| 1102 | * If o->color_moved is COLOR_MOVED_PLAIN, this function does nothing. |
| 1103 | * |
Jonathan Tan | f0b8fb6 | 2017-08-15 18:27:39 -0700 | [diff] [blame] | 1104 | * Otherwise, if the last block has fewer alphanumeric characters than |
| 1105 | * COLOR_MOVED_MIN_ALNUM_COUNT, unset DIFF_SYMBOL_MOVED_LINE on all lines in |
Jonathan Tan | 0915327 | 2017-08-15 18:27:38 -0700 | [diff] [blame] | 1106 | * that block. |
| 1107 | * |
| 1108 | * The last block consists of the (n - block_length)'th line up to but not |
| 1109 | * including the nth line. |
Jonathan Tan | f0b8fb6 | 2017-08-15 18:27:39 -0700 | [diff] [blame] | 1110 | * |
Phillip Wood | b0a2ba4 | 2018-11-23 11:16:55 +0000 | [diff] [blame] | 1111 | * Returns 0 if the last block is empty or is unset by this function, non zero |
| 1112 | * otherwise. |
| 1113 | * |
Jonathan Tan | f0b8fb6 | 2017-08-15 18:27:39 -0700 | [diff] [blame] | 1114 | * NEEDSWORK: This uses the same heuristic as blame_entry_score() in blame.c. |
| 1115 | * Think of a way to unify them. |
Jonathan Tan | 0915327 | 2017-08-15 18:27:38 -0700 | [diff] [blame] | 1116 | */ |
Phillip Wood | b0a2ba4 | 2018-11-23 11:16:55 +0000 | [diff] [blame] | 1117 | static int adjust_last_block(struct diff_options *o, int n, int block_length) |
Jonathan Tan | 0915327 | 2017-08-15 18:27:38 -0700 | [diff] [blame] | 1118 | { |
Jonathan Tan | f0b8fb6 | 2017-08-15 18:27:39 -0700 | [diff] [blame] | 1119 | int i, alnum_count = 0; |
| 1120 | if (o->color_moved == COLOR_MOVED_PLAIN) |
Phillip Wood | b0a2ba4 | 2018-11-23 11:16:55 +0000 | [diff] [blame] | 1121 | return block_length; |
Jonathan Tan | f0b8fb6 | 2017-08-15 18:27:39 -0700 | [diff] [blame] | 1122 | for (i = 1; i < block_length + 1; i++) { |
| 1123 | const char *c = o->emitted_symbols->buf[n - i].line; |
| 1124 | for (; *c; c++) { |
| 1125 | if (!isalnum(*c)) |
| 1126 | continue; |
| 1127 | alnum_count++; |
| 1128 | if (alnum_count >= COLOR_MOVED_MIN_ALNUM_COUNT) |
Phillip Wood | b0a2ba4 | 2018-11-23 11:16:55 +0000 | [diff] [blame] | 1129 | return 1; |
Jonathan Tan | f0b8fb6 | 2017-08-15 18:27:39 -0700 | [diff] [blame] | 1130 | } |
| 1131 | } |
Jonathan Tan | 0915327 | 2017-08-15 18:27:38 -0700 | [diff] [blame] | 1132 | for (i = 1; i < block_length + 1; i++) |
| 1133 | o->emitted_symbols->buf[n - i].flags &= ~DIFF_SYMBOL_MOVED_LINE; |
Phillip Wood | b0a2ba4 | 2018-11-23 11:16:55 +0000 | [diff] [blame] | 1134 | return 0; |
Jonathan Tan | 0915327 | 2017-08-15 18:27:38 -0700 | [diff] [blame] | 1135 | } |
| 1136 | |
Stefan Beller | 2e2d5ac | 2017-06-30 13:53:07 -0700 | [diff] [blame] | 1137 | /* Find blocks of moved code, delegate actual coloring decision to helper */ |
| 1138 | static void mark_color_as_moved(struct diff_options *o, |
| 1139 | struct hashmap *add_lines, |
| 1140 | struct hashmap *del_lines) |
| 1141 | { |
Phillip Wood | 74d156f | 2018-10-04 11:07:41 +0100 | [diff] [blame] | 1142 | struct moved_block *pmb = NULL; /* potentially moved blocks */ |
Stefan Beller | 2e2d5ac | 2017-06-30 13:53:07 -0700 | [diff] [blame] | 1143 | int pmb_nr = 0, pmb_alloc = 0; |
Phillip Wood | b0a2ba4 | 2018-11-23 11:16:55 +0000 | [diff] [blame] | 1144 | int n, flipped_block = 0, block_length = 0; |
Stefan Beller | 2e2d5ac | 2017-06-30 13:53:07 -0700 | [diff] [blame] | 1145 | |
| 1146 | |
| 1147 | for (n = 0; n < o->emitted_symbols->nr; n++) { |
| 1148 | struct hashmap *hm = NULL; |
| 1149 | struct moved_entry *key; |
| 1150 | struct moved_entry *match = NULL; |
| 1151 | struct emitted_diff_symbol *l = &o->emitted_symbols->buf[n]; |
Phillip Wood | b0a2ba4 | 2018-11-23 11:16:55 +0000 | [diff] [blame] | 1152 | enum diff_symbol last_symbol = 0; |
Stefan Beller | 2e2d5ac | 2017-06-30 13:53:07 -0700 | [diff] [blame] | 1153 | |
| 1154 | switch (l->s) { |
| 1155 | case DIFF_SYMBOL_PLUS: |
| 1156 | hm = del_lines; |
| 1157 | key = prepare_entry(o, n); |
Eric Wong | 404ab78 | 2019-10-06 23:30:42 +0000 | [diff] [blame] | 1158 | match = hashmap_get_entry(hm, key, ent, NULL); |
Stefan Beller | 2e2d5ac | 2017-06-30 13:53:07 -0700 | [diff] [blame] | 1159 | free(key); |
| 1160 | break; |
| 1161 | case DIFF_SYMBOL_MINUS: |
| 1162 | hm = add_lines; |
| 1163 | key = prepare_entry(o, n); |
Eric Wong | 404ab78 | 2019-10-06 23:30:42 +0000 | [diff] [blame] | 1164 | match = hashmap_get_entry(hm, key, ent, NULL); |
Stefan Beller | 2e2d5ac | 2017-06-30 13:53:07 -0700 | [diff] [blame] | 1165 | free(key); |
| 1166 | break; |
| 1167 | default: |
Phillip Wood | b0a2ba4 | 2018-11-23 11:16:55 +0000 | [diff] [blame] | 1168 | flipped_block = 0; |
Stefan Beller | 2e2d5ac | 2017-06-30 13:53:07 -0700 | [diff] [blame] | 1169 | } |
| 1170 | |
| 1171 | if (!match) { |
Phillip Wood | 74d156f | 2018-10-04 11:07:41 +0100 | [diff] [blame] | 1172 | int i; |
| 1173 | |
Jonathan Tan | 0915327 | 2017-08-15 18:27:38 -0700 | [diff] [blame] | 1174 | adjust_last_block(o, n, block_length); |
Phillip Wood | 74d156f | 2018-10-04 11:07:41 +0100 | [diff] [blame] | 1175 | for(i = 0; i < pmb_nr; i++) |
| 1176 | moved_block_clear(&pmb[i]); |
Stefan Beller | 2e2d5ac | 2017-06-30 13:53:07 -0700 | [diff] [blame] | 1177 | pmb_nr = 0; |
| 1178 | block_length = 0; |
Phillip Wood | b0a2ba4 | 2018-11-23 11:16:55 +0000 | [diff] [blame] | 1179 | flipped_block = 0; |
| 1180 | last_symbol = l->s; |
Stefan Beller | 2e2d5ac | 2017-06-30 13:53:07 -0700 | [diff] [blame] | 1181 | continue; |
| 1182 | } |
| 1183 | |
Phillip Wood | 2034b47 | 2018-11-23 11:16:54 +0000 | [diff] [blame] | 1184 | if (o->color_moved == COLOR_MOVED_PLAIN) { |
Phillip Wood | b0a2ba4 | 2018-11-23 11:16:55 +0000 | [diff] [blame] | 1185 | last_symbol = l->s; |
Phillip Wood | 2034b47 | 2018-11-23 11:16:54 +0000 | [diff] [blame] | 1186 | l->flags |= DIFF_SYMBOL_MOVED_LINE; |
Stefan Beller | 176841f | 2017-06-30 13:53:08 -0700 | [diff] [blame] | 1187 | continue; |
Phillip Wood | 2034b47 | 2018-11-23 11:16:54 +0000 | [diff] [blame] | 1188 | } |
Stefan Beller | 176841f | 2017-06-30 13:53:08 -0700 | [diff] [blame] | 1189 | |
Stefan Beller | ca1f4ae | 2018-07-18 12:31:55 -0700 | [diff] [blame] | 1190 | if (o->color_moved_ws_handling & |
| 1191 | COLOR_MOVED_WS_ALLOW_INDENTATION_CHANGE) |
| 1192 | pmb_advance_or_null_multi_match(o, match, hm, pmb, pmb_nr, n); |
| 1193 | else |
| 1194 | pmb_advance_or_null(o, match, hm, pmb, pmb_nr); |
Stefan Beller | 2e2d5ac | 2017-06-30 13:53:07 -0700 | [diff] [blame] | 1195 | |
| 1196 | pmb_nr = shrink_potential_moved_blocks(pmb, pmb_nr); |
| 1197 | |
| 1198 | if (pmb_nr == 0) { |
| 1199 | /* |
| 1200 | * The current line is the start of a new block. |
| 1201 | * Setup the set of potential blocks. |
| 1202 | */ |
Eric Wong | 23dee69 | 2019-10-06 23:30:41 +0000 | [diff] [blame] | 1203 | hashmap_for_each_entry_from(hm, match, ent) { |
Stefan Beller | 2e2d5ac | 2017-06-30 13:53:07 -0700 | [diff] [blame] | 1204 | ALLOC_GROW(pmb, pmb_nr + 1, pmb_alloc); |
Stefan Beller | ca1f4ae | 2018-07-18 12:31:55 -0700 | [diff] [blame] | 1205 | if (o->color_moved_ws_handling & |
| 1206 | COLOR_MOVED_WS_ALLOW_INDENTATION_CHANGE) { |
Phillip Wood | 74d156f | 2018-10-04 11:07:41 +0100 | [diff] [blame] | 1207 | if (compute_ws_delta(l, match->es, |
| 1208 | &pmb[pmb_nr].wsd)) |
| 1209 | pmb[pmb_nr++].match = match; |
Stefan Beller | ca1f4ae | 2018-07-18 12:31:55 -0700 | [diff] [blame] | 1210 | } else { |
Phillip Wood | 21536d0 | 2018-11-23 11:16:57 +0000 | [diff] [blame] | 1211 | pmb[pmb_nr].wsd = 0; |
Phillip Wood | 74d156f | 2018-10-04 11:07:41 +0100 | [diff] [blame] | 1212 | pmb[pmb_nr++].match = match; |
Stefan Beller | ca1f4ae | 2018-07-18 12:31:55 -0700 | [diff] [blame] | 1213 | } |
Stefan Beller | 2e2d5ac | 2017-06-30 13:53:07 -0700 | [diff] [blame] | 1214 | } |
| 1215 | |
Phillip Wood | b0a2ba4 | 2018-11-23 11:16:55 +0000 | [diff] [blame] | 1216 | if (adjust_last_block(o, n, block_length) && |
| 1217 | pmb_nr && last_symbol != l->s) |
| 1218 | flipped_block = (flipped_block + 1) % 2; |
| 1219 | else |
| 1220 | flipped_block = 0; |
Jonathan Tan | f0b8fb6 | 2017-08-15 18:27:39 -0700 | [diff] [blame] | 1221 | |
Jonathan Tan | f0b8fb6 | 2017-08-15 18:27:39 -0700 | [diff] [blame] | 1222 | block_length = 0; |
Stefan Beller | 2e2d5ac | 2017-06-30 13:53:07 -0700 | [diff] [blame] | 1223 | } |
| 1224 | |
Phillip Wood | 2034b47 | 2018-11-23 11:16:54 +0000 | [diff] [blame] | 1225 | if (pmb_nr) { |
| 1226 | block_length++; |
Phillip Wood | 2034b47 | 2018-11-23 11:16:54 +0000 | [diff] [blame] | 1227 | l->flags |= DIFF_SYMBOL_MOVED_LINE; |
| 1228 | if (flipped_block && o->color_moved != COLOR_MOVED_BLOCKS) |
| 1229 | l->flags |= DIFF_SYMBOL_MOVED_LINE_ALT; |
| 1230 | } |
Phillip Wood | b0a2ba4 | 2018-11-23 11:16:55 +0000 | [diff] [blame] | 1231 | last_symbol = l->s; |
Stefan Beller | 2e2d5ac | 2017-06-30 13:53:07 -0700 | [diff] [blame] | 1232 | } |
Jonathan Tan | 0915327 | 2017-08-15 18:27:38 -0700 | [diff] [blame] | 1233 | adjust_last_block(o, n, block_length); |
Stefan Beller | 2e2d5ac | 2017-06-30 13:53:07 -0700 | [diff] [blame] | 1234 | |
Phillip Wood | 74d156f | 2018-10-04 11:07:41 +0100 | [diff] [blame] | 1235 | for(n = 0; n < pmb_nr; n++) |
| 1236 | moved_block_clear(&pmb[n]); |
Stefan Beller | 2e2d5ac | 2017-06-30 13:53:07 -0700 | [diff] [blame] | 1237 | free(pmb); |
| 1238 | } |
Stefan Beller | e6e045f | 2017-06-29 17:07:06 -0700 | [diff] [blame] | 1239 | |
Stefan Beller | 86b452e | 2017-06-30 13:53:09 -0700 | [diff] [blame] | 1240 | #define DIFF_SYMBOL_MOVED_LINE_ZEBRA_MASK \ |
| 1241 | (DIFF_SYMBOL_MOVED_LINE | DIFF_SYMBOL_MOVED_LINE_ALT) |
| 1242 | static void dim_moved_lines(struct diff_options *o) |
| 1243 | { |
| 1244 | int n; |
| 1245 | for (n = 0; n < o->emitted_symbols->nr; n++) { |
| 1246 | struct emitted_diff_symbol *prev = (n != 0) ? |
| 1247 | &o->emitted_symbols->buf[n - 1] : NULL; |
| 1248 | struct emitted_diff_symbol *l = &o->emitted_symbols->buf[n]; |
| 1249 | struct emitted_diff_symbol *next = |
| 1250 | (n < o->emitted_symbols->nr - 1) ? |
| 1251 | &o->emitted_symbols->buf[n + 1] : NULL; |
| 1252 | |
| 1253 | /* Not a plus or minus line? */ |
| 1254 | if (l->s != DIFF_SYMBOL_PLUS && l->s != DIFF_SYMBOL_MINUS) |
| 1255 | continue; |
| 1256 | |
| 1257 | /* Not a moved line? */ |
| 1258 | if (!(l->flags & DIFF_SYMBOL_MOVED_LINE)) |
| 1259 | continue; |
| 1260 | |
| 1261 | /* |
| 1262 | * If prev or next are not a plus or minus line, |
| 1263 | * pretend they don't exist |
| 1264 | */ |
| 1265 | if (prev && prev->s != DIFF_SYMBOL_PLUS && |
| 1266 | prev->s != DIFF_SYMBOL_MINUS) |
| 1267 | prev = NULL; |
| 1268 | if (next && next->s != DIFF_SYMBOL_PLUS && |
| 1269 | next->s != DIFF_SYMBOL_MINUS) |
| 1270 | next = NULL; |
| 1271 | |
| 1272 | /* Inside a block? */ |
| 1273 | if ((prev && |
| 1274 | (prev->flags & DIFF_SYMBOL_MOVED_LINE_ZEBRA_MASK) == |
| 1275 | (l->flags & DIFF_SYMBOL_MOVED_LINE_ZEBRA_MASK)) && |
| 1276 | (next && |
| 1277 | (next->flags & DIFF_SYMBOL_MOVED_LINE_ZEBRA_MASK) == |
| 1278 | (l->flags & DIFF_SYMBOL_MOVED_LINE_ZEBRA_MASK))) { |
| 1279 | l->flags |= DIFF_SYMBOL_MOVED_LINE_UNINTERESTING; |
| 1280 | continue; |
| 1281 | } |
| 1282 | |
| 1283 | /* Check if we are at an interesting bound: */ |
| 1284 | if (prev && (prev->flags & DIFF_SYMBOL_MOVED_LINE) && |
| 1285 | (prev->flags & DIFF_SYMBOL_MOVED_LINE_ALT) != |
| 1286 | (l->flags & DIFF_SYMBOL_MOVED_LINE_ALT)) |
| 1287 | continue; |
| 1288 | if (next && (next->flags & DIFF_SYMBOL_MOVED_LINE) && |
| 1289 | (next->flags & DIFF_SYMBOL_MOVED_LINE_ALT) != |
| 1290 | (l->flags & DIFF_SYMBOL_MOVED_LINE_ALT)) |
| 1291 | continue; |
| 1292 | |
| 1293 | /* |
| 1294 | * The boundary to prev and next are not interesting, |
| 1295 | * so this line is not interesting as a whole |
| 1296 | */ |
| 1297 | l->flags |= DIFF_SYMBOL_MOVED_LINE_UNINTERESTING; |
| 1298 | } |
| 1299 | } |
| 1300 | |
Stefan Beller | 091f8e2 | 2017-06-29 17:06:53 -0700 | [diff] [blame] | 1301 | static void emit_line_ws_markup(struct diff_options *o, |
Stefan Beller | 9d1e16b | 2018-08-13 18:41:18 -0700 | [diff] [blame] | 1302 | const char *set_sign, const char *set, |
| 1303 | const char *reset, |
Stefan Beller | bc9feb0 | 2018-10-10 16:24:59 -0700 | [diff] [blame] | 1304 | int sign_index, const char *line, int len, |
Stefan Beller | 091f8e2 | 2017-06-29 17:06:53 -0700 | [diff] [blame] | 1305 | unsigned ws_rule, int blank_at_eof) |
| 1306 | { |
| 1307 | const char *ws = NULL; |
Stefan Beller | bc9feb0 | 2018-10-10 16:24:59 -0700 | [diff] [blame] | 1308 | int sign = o->output_indicators[sign_index]; |
Stefan Beller | 091f8e2 | 2017-06-29 17:06:53 -0700 | [diff] [blame] | 1309 | |
| 1310 | if (o->ws_error_highlight & ws_rule) { |
| 1311 | ws = diff_get_color_opt(o, DIFF_WHITESPACE); |
| 1312 | if (!*ws) |
| 1313 | ws = NULL; |
| 1314 | } |
| 1315 | |
Johannes Schindelin | f7c3b4e | 2018-08-13 04:33:20 -0700 | [diff] [blame] | 1316 | if (!ws && !set_sign) |
Stefan Beller | 017ac45 | 2018-08-13 18:41:19 -0700 | [diff] [blame] | 1317 | emit_line_0(o, set, NULL, 0, reset, sign, line, len); |
Johannes Schindelin | f7c3b4e | 2018-08-13 04:33:20 -0700 | [diff] [blame] | 1318 | else if (!ws) { |
Stefan Beller | 29ef759 | 2018-08-13 18:41:20 -0700 | [diff] [blame] | 1319 | emit_line_0(o, set_sign, set, !!set_sign, reset, sign, line, len); |
Johannes Schindelin | f7c3b4e | 2018-08-13 04:33:20 -0700 | [diff] [blame] | 1320 | } else if (blank_at_eof) |
Stefan Beller | 091f8e2 | 2017-06-29 17:06:53 -0700 | [diff] [blame] | 1321 | /* Blank line at EOF - paint '+' as well */ |
Stefan Beller | 017ac45 | 2018-08-13 18:41:19 -0700 | [diff] [blame] | 1322 | emit_line_0(o, ws, NULL, 0, reset, sign, line, len); |
Stefan Beller | 091f8e2 | 2017-06-29 17:06:53 -0700 | [diff] [blame] | 1323 | else { |
| 1324 | /* Emit just the prefix, then the rest. */ |
Stefan Beller | 017ac45 | 2018-08-13 18:41:19 -0700 | [diff] [blame] | 1325 | emit_line_0(o, set_sign ? set_sign : set, NULL, !!set_sign, reset, |
Johannes Schindelin | f7c3b4e | 2018-08-13 04:33:20 -0700 | [diff] [blame] | 1326 | sign, "", 0); |
Stefan Beller | 091f8e2 | 2017-06-29 17:06:53 -0700 | [diff] [blame] | 1327 | ws_check_emit(line, len, ws_rule, |
| 1328 | o->file, set, reset, ws); |
| 1329 | } |
| 1330 | } |
Stefan Beller | 36a4cef | 2017-06-29 17:06:49 -0700 | [diff] [blame] | 1331 | |
Stefan Beller | e6e045f | 2017-06-29 17:07:06 -0700 | [diff] [blame] | 1332 | static void emit_diff_symbol_from_struct(struct diff_options *o, |
| 1333 | struct emitted_diff_symbol *eds) |
Stefan Beller | 36a4cef | 2017-06-29 17:06:49 -0700 | [diff] [blame] | 1334 | { |
Stefan Beller | b9cbfde | 2017-06-29 17:06:52 -0700 | [diff] [blame] | 1335 | static const char *nneof = " No newline at end of file\n"; |
Johannes Schindelin | f7c3b4e | 2018-08-13 04:33:20 -0700 | [diff] [blame] | 1336 | const char *context, *reset, *set, *set_sign, *meta, *fraginfo; |
Stefan Beller | 0911c47 | 2017-06-29 17:07:02 -0700 | [diff] [blame] | 1337 | struct strbuf sb = STRBUF_INIT; |
Stefan Beller | e6e045f | 2017-06-29 17:07:06 -0700 | [diff] [blame] | 1338 | |
| 1339 | enum diff_symbol s = eds->s; |
| 1340 | const char *line = eds->line; |
| 1341 | int len = eds->len; |
| 1342 | unsigned flags = eds->flags; |
| 1343 | |
Stefan Beller | 36a4cef | 2017-06-29 17:06:49 -0700 | [diff] [blame] | 1344 | switch (s) { |
Stefan Beller | b9cbfde | 2017-06-29 17:06:52 -0700 | [diff] [blame] | 1345 | case DIFF_SYMBOL_NO_LF_EOF: |
| 1346 | context = diff_get_color_opt(o, DIFF_CONTEXT); |
| 1347 | reset = diff_get_color_opt(o, DIFF_RESET); |
| 1348 | putc('\n', o->file); |
Stefan Beller | 017ac45 | 2018-08-13 18:41:19 -0700 | [diff] [blame] | 1349 | emit_line_0(o, context, NULL, 0, reset, '\\', |
Stefan Beller | b9cbfde | 2017-06-29 17:06:52 -0700 | [diff] [blame] | 1350 | nneof, strlen(nneof)); |
| 1351 | break; |
Stefan Beller | f359713 | 2017-06-29 17:07:00 -0700 | [diff] [blame] | 1352 | case DIFF_SYMBOL_SUBMODULE_HEADER: |
| 1353 | case DIFF_SYMBOL_SUBMODULE_ERROR: |
| 1354 | case DIFF_SYMBOL_SUBMODULE_PIPETHROUGH: |
Stefan Beller | 0911c47 | 2017-06-29 17:07:02 -0700 | [diff] [blame] | 1355 | case DIFF_SYMBOL_STATS_SUMMARY_INSERTS_DELETES: |
Stefan Beller | 146fdb0 | 2017-06-29 17:07:05 -0700 | [diff] [blame] | 1356 | case DIFF_SYMBOL_SUMMARY: |
Stefan Beller | 0911c47 | 2017-06-29 17:07:02 -0700 | [diff] [blame] | 1357 | case DIFF_SYMBOL_STATS_LINE: |
Stefan Beller | 4eed0eb | 2017-06-29 17:07:01 -0700 | [diff] [blame] | 1358 | case DIFF_SYMBOL_BINARY_DIFF_BODY: |
Stefan Beller | 68abc6f | 2017-06-29 17:06:51 -0700 | [diff] [blame] | 1359 | case DIFF_SYMBOL_CONTEXT_FRAGINFO: |
| 1360 | emit_line(o, "", "", line, len); |
| 1361 | break; |
Stefan Beller | f2bb121 | 2017-06-29 17:06:55 -0700 | [diff] [blame] | 1362 | case DIFF_SYMBOL_CONTEXT_INCOMPLETE: |
Stefan Beller | c64b420 | 2017-06-29 17:06:50 -0700 | [diff] [blame] | 1363 | case DIFF_SYMBOL_CONTEXT_MARKER: |
| 1364 | context = diff_get_color_opt(o, DIFF_CONTEXT); |
| 1365 | reset = diff_get_color_opt(o, DIFF_RESET); |
| 1366 | emit_line(o, context, reset, line, len); |
| 1367 | break; |
Stefan Beller | 36a4cef | 2017-06-29 17:06:49 -0700 | [diff] [blame] | 1368 | case DIFF_SYMBOL_SEPARATOR: |
| 1369 | fprintf(o->file, "%s%c", |
| 1370 | diff_line_prefix(o), |
| 1371 | o->line_termination); |
| 1372 | break; |
Stefan Beller | 091f8e2 | 2017-06-29 17:06:53 -0700 | [diff] [blame] | 1373 | case DIFF_SYMBOL_CONTEXT: |
| 1374 | set = diff_get_color_opt(o, DIFF_CONTEXT); |
| 1375 | reset = diff_get_color_opt(o, DIFF_RESET); |
Johannes Schindelin | f7c3b4e | 2018-08-13 04:33:20 -0700 | [diff] [blame] | 1376 | set_sign = NULL; |
| 1377 | if (o->flags.dual_color_diffed_diffs) { |
| 1378 | char c = !len ? 0 : line[0]; |
| 1379 | |
| 1380 | if (c == '+') |
| 1381 | set = diff_get_color_opt(o, DIFF_FILE_NEW); |
| 1382 | else if (c == '@') |
| 1383 | set = diff_get_color_opt(o, DIFF_FRAGINFO); |
| 1384 | else if (c == '-') |
| 1385 | set = diff_get_color_opt(o, DIFF_FILE_OLD); |
| 1386 | } |
Stefan Beller | 7648b79 | 2018-08-17 13:43:52 -0700 | [diff] [blame] | 1387 | emit_line_ws_markup(o, set_sign, set, reset, |
Stefan Beller | bc9feb0 | 2018-10-10 16:24:59 -0700 | [diff] [blame] | 1388 | OUTPUT_INDICATOR_CONTEXT, line, len, |
Stefan Beller | 091f8e2 | 2017-06-29 17:06:53 -0700 | [diff] [blame] | 1389 | flags & (DIFF_SYMBOL_CONTENT_WS_MASK), 0); |
| 1390 | break; |
| 1391 | case DIFF_SYMBOL_PLUS: |
Stefan Beller | 86b452e | 2017-06-30 13:53:09 -0700 | [diff] [blame] | 1392 | switch (flags & (DIFF_SYMBOL_MOVED_LINE | |
| 1393 | DIFF_SYMBOL_MOVED_LINE_ALT | |
| 1394 | DIFF_SYMBOL_MOVED_LINE_UNINTERESTING)) { |
| 1395 | case DIFF_SYMBOL_MOVED_LINE | |
| 1396 | DIFF_SYMBOL_MOVED_LINE_ALT | |
| 1397 | DIFF_SYMBOL_MOVED_LINE_UNINTERESTING: |
| 1398 | set = diff_get_color_opt(o, DIFF_FILE_NEW_MOVED_ALT_DIM); |
| 1399 | break; |
| 1400 | case DIFF_SYMBOL_MOVED_LINE | |
| 1401 | DIFF_SYMBOL_MOVED_LINE_ALT: |
Stefan Beller | 2e2d5ac | 2017-06-30 13:53:07 -0700 | [diff] [blame] | 1402 | set = diff_get_color_opt(o, DIFF_FILE_NEW_MOVED_ALT); |
Stefan Beller | 86b452e | 2017-06-30 13:53:09 -0700 | [diff] [blame] | 1403 | break; |
| 1404 | case DIFF_SYMBOL_MOVED_LINE | |
| 1405 | DIFF_SYMBOL_MOVED_LINE_UNINTERESTING: |
| 1406 | set = diff_get_color_opt(o, DIFF_FILE_NEW_MOVED_DIM); |
| 1407 | break; |
| 1408 | case DIFF_SYMBOL_MOVED_LINE: |
Stefan Beller | 2e2d5ac | 2017-06-30 13:53:07 -0700 | [diff] [blame] | 1409 | set = diff_get_color_opt(o, DIFF_FILE_NEW_MOVED); |
Stefan Beller | 86b452e | 2017-06-30 13:53:09 -0700 | [diff] [blame] | 1410 | break; |
| 1411 | default: |
Stefan Beller | 2e2d5ac | 2017-06-30 13:53:07 -0700 | [diff] [blame] | 1412 | set = diff_get_color_opt(o, DIFF_FILE_NEW); |
Stefan Beller | 86b452e | 2017-06-30 13:53:09 -0700 | [diff] [blame] | 1413 | } |
Stefan Beller | 091f8e2 | 2017-06-29 17:06:53 -0700 | [diff] [blame] | 1414 | reset = diff_get_color_opt(o, DIFF_RESET); |
Johannes Schindelin | f7c3b4e | 2018-08-13 04:33:20 -0700 | [diff] [blame] | 1415 | if (!o->flags.dual_color_diffed_diffs) |
| 1416 | set_sign = NULL; |
| 1417 | else { |
| 1418 | char c = !len ? 0 : line[0]; |
| 1419 | |
| 1420 | set_sign = set; |
| 1421 | if (c == '-') |
Johannes Schindelin | a7be92a | 2018-08-13 04:33:32 -0700 | [diff] [blame] | 1422 | set = diff_get_color_opt(o, DIFF_FILE_OLD_BOLD); |
Johannes Schindelin | f7c3b4e | 2018-08-13 04:33:20 -0700 | [diff] [blame] | 1423 | else if (c == '@') |
| 1424 | set = diff_get_color_opt(o, DIFF_FRAGINFO); |
Johannes Schindelin | a7be92a | 2018-08-13 04:33:32 -0700 | [diff] [blame] | 1425 | else if (c == '+') |
| 1426 | set = diff_get_color_opt(o, DIFF_FILE_NEW_BOLD); |
| 1427 | else |
| 1428 | set = diff_get_color_opt(o, DIFF_CONTEXT_BOLD); |
Johannes Schindelin | 0b91faa | 2018-08-13 04:33:24 -0700 | [diff] [blame] | 1429 | flags &= ~DIFF_SYMBOL_CONTENT_WS_MASK; |
Johannes Schindelin | f7c3b4e | 2018-08-13 04:33:20 -0700 | [diff] [blame] | 1430 | } |
Stefan Beller | 7648b79 | 2018-08-17 13:43:52 -0700 | [diff] [blame] | 1431 | emit_line_ws_markup(o, set_sign, set, reset, |
Stefan Beller | bc9feb0 | 2018-10-10 16:24:59 -0700 | [diff] [blame] | 1432 | OUTPUT_INDICATOR_NEW, line, len, |
Stefan Beller | 091f8e2 | 2017-06-29 17:06:53 -0700 | [diff] [blame] | 1433 | flags & DIFF_SYMBOL_CONTENT_WS_MASK, |
| 1434 | flags & DIFF_SYMBOL_CONTENT_BLANK_LINE_EOF); |
| 1435 | break; |
| 1436 | case DIFF_SYMBOL_MINUS: |
Stefan Beller | 86b452e | 2017-06-30 13:53:09 -0700 | [diff] [blame] | 1437 | switch (flags & (DIFF_SYMBOL_MOVED_LINE | |
| 1438 | DIFF_SYMBOL_MOVED_LINE_ALT | |
| 1439 | DIFF_SYMBOL_MOVED_LINE_UNINTERESTING)) { |
| 1440 | case DIFF_SYMBOL_MOVED_LINE | |
| 1441 | DIFF_SYMBOL_MOVED_LINE_ALT | |
| 1442 | DIFF_SYMBOL_MOVED_LINE_UNINTERESTING: |
| 1443 | set = diff_get_color_opt(o, DIFF_FILE_OLD_MOVED_ALT_DIM); |
| 1444 | break; |
| 1445 | case DIFF_SYMBOL_MOVED_LINE | |
| 1446 | DIFF_SYMBOL_MOVED_LINE_ALT: |
Stefan Beller | 2e2d5ac | 2017-06-30 13:53:07 -0700 | [diff] [blame] | 1447 | set = diff_get_color_opt(o, DIFF_FILE_OLD_MOVED_ALT); |
Stefan Beller | 86b452e | 2017-06-30 13:53:09 -0700 | [diff] [blame] | 1448 | break; |
| 1449 | case DIFF_SYMBOL_MOVED_LINE | |
| 1450 | DIFF_SYMBOL_MOVED_LINE_UNINTERESTING: |
| 1451 | set = diff_get_color_opt(o, DIFF_FILE_OLD_MOVED_DIM); |
| 1452 | break; |
| 1453 | case DIFF_SYMBOL_MOVED_LINE: |
Stefan Beller | 2e2d5ac | 2017-06-30 13:53:07 -0700 | [diff] [blame] | 1454 | set = diff_get_color_opt(o, DIFF_FILE_OLD_MOVED); |
Stefan Beller | 86b452e | 2017-06-30 13:53:09 -0700 | [diff] [blame] | 1455 | break; |
| 1456 | default: |
Stefan Beller | 2e2d5ac | 2017-06-30 13:53:07 -0700 | [diff] [blame] | 1457 | set = diff_get_color_opt(o, DIFF_FILE_OLD); |
Stefan Beller | 86b452e | 2017-06-30 13:53:09 -0700 | [diff] [blame] | 1458 | } |
Stefan Beller | 091f8e2 | 2017-06-29 17:06:53 -0700 | [diff] [blame] | 1459 | reset = diff_get_color_opt(o, DIFF_RESET); |
Johannes Schindelin | f7c3b4e | 2018-08-13 04:33:20 -0700 | [diff] [blame] | 1460 | if (!o->flags.dual_color_diffed_diffs) |
| 1461 | set_sign = NULL; |
| 1462 | else { |
| 1463 | char c = !len ? 0 : line[0]; |
| 1464 | |
| 1465 | set_sign = set; |
| 1466 | if (c == '+') |
Johannes Schindelin | a7be92a | 2018-08-13 04:33:32 -0700 | [diff] [blame] | 1467 | set = diff_get_color_opt(o, DIFF_FILE_NEW_DIM); |
Johannes Schindelin | f7c3b4e | 2018-08-13 04:33:20 -0700 | [diff] [blame] | 1468 | else if (c == '@') |
| 1469 | set = diff_get_color_opt(o, DIFF_FRAGINFO); |
Johannes Schindelin | a7be92a | 2018-08-13 04:33:32 -0700 | [diff] [blame] | 1470 | else if (c == '-') |
| 1471 | set = diff_get_color_opt(o, DIFF_FILE_OLD_DIM); |
| 1472 | else |
| 1473 | set = diff_get_color_opt(o, DIFF_CONTEXT_DIM); |
Johannes Schindelin | f7c3b4e | 2018-08-13 04:33:20 -0700 | [diff] [blame] | 1474 | } |
Stefan Beller | 7648b79 | 2018-08-17 13:43:52 -0700 | [diff] [blame] | 1475 | emit_line_ws_markup(o, set_sign, set, reset, |
Stefan Beller | bc9feb0 | 2018-10-10 16:24:59 -0700 | [diff] [blame] | 1476 | OUTPUT_INDICATOR_OLD, line, len, |
Stefan Beller | 091f8e2 | 2017-06-29 17:06:53 -0700 | [diff] [blame] | 1477 | flags & DIFF_SYMBOL_CONTENT_WS_MASK, 0); |
| 1478 | break; |
Stefan Beller | ff95867 | 2017-06-29 17:06:54 -0700 | [diff] [blame] | 1479 | case DIFF_SYMBOL_WORDS_PORCELAIN: |
| 1480 | context = diff_get_color_opt(o, DIFF_CONTEXT); |
| 1481 | reset = diff_get_color_opt(o, DIFF_RESET); |
| 1482 | emit_line(o, context, reset, line, len); |
| 1483 | fputs("~\n", o->file); |
| 1484 | break; |
| 1485 | case DIFF_SYMBOL_WORDS: |
| 1486 | context = diff_get_color_opt(o, DIFF_CONTEXT); |
| 1487 | reset = diff_get_color_opt(o, DIFF_RESET); |
| 1488 | /* |
| 1489 | * Skip the prefix character, if any. With |
| 1490 | * diff_suppress_blank_empty, there may be |
| 1491 | * none. |
| 1492 | */ |
| 1493 | if (line[0] != '\n') { |
| 1494 | line++; |
| 1495 | len--; |
| 1496 | } |
| 1497 | emit_line(o, context, reset, line, len); |
| 1498 | break; |
Stefan Beller | 3ee8b7b | 2017-06-29 17:06:56 -0700 | [diff] [blame] | 1499 | case DIFF_SYMBOL_FILEPAIR_PLUS: |
| 1500 | meta = diff_get_color_opt(o, DIFF_METAINFO); |
| 1501 | reset = diff_get_color_opt(o, DIFF_RESET); |
| 1502 | fprintf(o->file, "%s%s+++ %s%s%s\n", diff_line_prefix(o), meta, |
| 1503 | line, reset, |
| 1504 | strchr(line, ' ') ? "\t" : ""); |
| 1505 | break; |
| 1506 | case DIFF_SYMBOL_FILEPAIR_MINUS: |
| 1507 | meta = diff_get_color_opt(o, DIFF_METAINFO); |
| 1508 | reset = diff_get_color_opt(o, DIFF_RESET); |
| 1509 | fprintf(o->file, "%s%s--- %s%s%s\n", diff_line_prefix(o), meta, |
| 1510 | line, reset, |
| 1511 | strchr(line, ' ') ? "\t" : ""); |
| 1512 | break; |
Stefan Beller | 4acaaa7 | 2017-06-29 17:06:58 -0700 | [diff] [blame] | 1513 | case DIFF_SYMBOL_BINARY_FILES: |
Stefan Beller | a29b0a1 | 2017-06-29 17:06:57 -0700 | [diff] [blame] | 1514 | case DIFF_SYMBOL_HEADER: |
| 1515 | fprintf(o->file, "%s", line); |
| 1516 | break; |
Stefan Beller | 4eed0eb | 2017-06-29 17:07:01 -0700 | [diff] [blame] | 1517 | case DIFF_SYMBOL_BINARY_DIFF_HEADER: |
| 1518 | fprintf(o->file, "%sGIT binary patch\n", diff_line_prefix(o)); |
| 1519 | break; |
| 1520 | case DIFF_SYMBOL_BINARY_DIFF_HEADER_DELTA: |
| 1521 | fprintf(o->file, "%sdelta %s\n", diff_line_prefix(o), line); |
| 1522 | break; |
| 1523 | case DIFF_SYMBOL_BINARY_DIFF_HEADER_LITERAL: |
| 1524 | fprintf(o->file, "%sliteral %s\n", diff_line_prefix(o), line); |
| 1525 | break; |
| 1526 | case DIFF_SYMBOL_BINARY_DIFF_FOOTER: |
| 1527 | fputs(diff_line_prefix(o), o->file); |
| 1528 | fputc('\n', o->file); |
| 1529 | break; |
Stefan Beller | 5af6ea9 | 2017-06-29 17:06:59 -0700 | [diff] [blame] | 1530 | case DIFF_SYMBOL_REWRITE_DIFF: |
| 1531 | fraginfo = diff_get_color(o->use_color, DIFF_FRAGINFO); |
| 1532 | reset = diff_get_color_opt(o, DIFF_RESET); |
| 1533 | emit_line(o, fraginfo, reset, line, len); |
| 1534 | break; |
Stefan Beller | f359713 | 2017-06-29 17:07:00 -0700 | [diff] [blame] | 1535 | case DIFF_SYMBOL_SUBMODULE_ADD: |
| 1536 | set = diff_get_color_opt(o, DIFF_FILE_NEW); |
| 1537 | reset = diff_get_color_opt(o, DIFF_RESET); |
| 1538 | emit_line(o, set, reset, line, len); |
| 1539 | break; |
| 1540 | case DIFF_SYMBOL_SUBMODULE_DEL: |
| 1541 | set = diff_get_color_opt(o, DIFF_FILE_OLD); |
| 1542 | reset = diff_get_color_opt(o, DIFF_RESET); |
| 1543 | emit_line(o, set, reset, line, len); |
| 1544 | break; |
| 1545 | case DIFF_SYMBOL_SUBMODULE_UNTRACKED: |
| 1546 | fprintf(o->file, "%sSubmodule %s contains untracked content\n", |
| 1547 | diff_line_prefix(o), line); |
| 1548 | break; |
| 1549 | case DIFF_SYMBOL_SUBMODULE_MODIFIED: |
| 1550 | fprintf(o->file, "%sSubmodule %s contains modified content\n", |
| 1551 | diff_line_prefix(o), line); |
| 1552 | break; |
Stefan Beller | 0911c47 | 2017-06-29 17:07:02 -0700 | [diff] [blame] | 1553 | case DIFF_SYMBOL_STATS_SUMMARY_NO_FILES: |
| 1554 | emit_line(o, "", "", " 0 files changed\n", |
| 1555 | strlen(" 0 files changed\n")); |
| 1556 | break; |
| 1557 | case DIFF_SYMBOL_STATS_SUMMARY_ABBREV: |
| 1558 | emit_line(o, "", "", " ...\n", strlen(" ...\n")); |
| 1559 | break; |
Stefan Beller | bd03329 | 2017-06-29 17:07:03 -0700 | [diff] [blame] | 1560 | case DIFF_SYMBOL_WORD_DIFF: |
| 1561 | fprintf(o->file, "%.*s", len, line); |
| 1562 | break; |
Stefan Beller | 30b7e1e | 2017-06-29 17:07:04 -0700 | [diff] [blame] | 1563 | case DIFF_SYMBOL_STAT_SEP: |
| 1564 | fputs(o->stat_sep, o->file); |
| 1565 | break; |
Stefan Beller | 36a4cef | 2017-06-29 17:06:49 -0700 | [diff] [blame] | 1566 | default: |
Johannes Schindelin | 033abf9 | 2018-05-02 11:38:39 +0200 | [diff] [blame] | 1567 | BUG("unknown diff symbol"); |
Stefan Beller | 36a4cef | 2017-06-29 17:06:49 -0700 | [diff] [blame] | 1568 | } |
Stefan Beller | 0911c47 | 2017-06-29 17:07:02 -0700 | [diff] [blame] | 1569 | strbuf_release(&sb); |
Stefan Beller | 36a4cef | 2017-06-29 17:06:49 -0700 | [diff] [blame] | 1570 | } |
| 1571 | |
Stefan Beller | e6e045f | 2017-06-29 17:07:06 -0700 | [diff] [blame] | 1572 | static void emit_diff_symbol(struct diff_options *o, enum diff_symbol s, |
| 1573 | const char *line, int len, unsigned flags) |
| 1574 | { |
Phillip Wood | 21536d0 | 2018-11-23 11:16:57 +0000 | [diff] [blame] | 1575 | struct emitted_diff_symbol e = {line, len, flags, 0, 0, s}; |
Stefan Beller | e6e045f | 2017-06-29 17:07:06 -0700 | [diff] [blame] | 1576 | |
| 1577 | if (o->emitted_symbols) |
| 1578 | append_emitted_diff_symbol(o, &e); |
| 1579 | else |
| 1580 | emit_diff_symbol_from_struct(o, &e); |
| 1581 | } |
| 1582 | |
Stefan Beller | f359713 | 2017-06-29 17:07:00 -0700 | [diff] [blame] | 1583 | void diff_emit_submodule_del(struct diff_options *o, const char *line) |
| 1584 | { |
| 1585 | emit_diff_symbol(o, DIFF_SYMBOL_SUBMODULE_DEL, line, strlen(line), 0); |
| 1586 | } |
| 1587 | |
| 1588 | void diff_emit_submodule_add(struct diff_options *o, const char *line) |
| 1589 | { |
| 1590 | emit_diff_symbol(o, DIFF_SYMBOL_SUBMODULE_ADD, line, strlen(line), 0); |
| 1591 | } |
| 1592 | |
| 1593 | void diff_emit_submodule_untracked(struct diff_options *o, const char *path) |
| 1594 | { |
| 1595 | emit_diff_symbol(o, DIFF_SYMBOL_SUBMODULE_UNTRACKED, |
| 1596 | path, strlen(path), 0); |
| 1597 | } |
| 1598 | |
| 1599 | void diff_emit_submodule_modified(struct diff_options *o, const char *path) |
| 1600 | { |
| 1601 | emit_diff_symbol(o, DIFF_SYMBOL_SUBMODULE_MODIFIED, |
| 1602 | path, strlen(path), 0); |
| 1603 | } |
| 1604 | |
| 1605 | void diff_emit_submodule_header(struct diff_options *o, const char *header) |
| 1606 | { |
| 1607 | emit_diff_symbol(o, DIFF_SYMBOL_SUBMODULE_HEADER, |
| 1608 | header, strlen(header), 0); |
| 1609 | } |
| 1610 | |
| 1611 | void diff_emit_submodule_error(struct diff_options *o, const char *err) |
| 1612 | { |
| 1613 | emit_diff_symbol(o, DIFF_SYMBOL_SUBMODULE_ERROR, err, strlen(err), 0); |
| 1614 | } |
| 1615 | |
| 1616 | void diff_emit_submodule_pipethrough(struct diff_options *o, |
| 1617 | const char *line, int len) |
| 1618 | { |
| 1619 | emit_diff_symbol(o, DIFF_SYMBOL_SUBMODULE_PIPETHROUGH, line, len, 0); |
| 1620 | } |
| 1621 | |
Junio C Hamano | 6957eb9 | 2009-09-14 18:44:01 -0700 | [diff] [blame] | 1622 | static int new_blank_line_at_eof(struct emit_callback *ecbdata, const char *line, int len) |
| 1623 | { |
| 1624 | if (!((ecbdata->ws_rule & WS_BLANK_AT_EOF) && |
| 1625 | ecbdata->blank_at_eof_in_preimage && |
| 1626 | ecbdata->blank_at_eof_in_postimage && |
| 1627 | ecbdata->blank_at_eof_in_preimage <= ecbdata->lno_in_preimage && |
| 1628 | ecbdata->blank_at_eof_in_postimage <= ecbdata->lno_in_postimage)) |
| 1629 | return 0; |
Junio C Hamano | 018cff7 | 2009-09-14 18:44:01 -0700 | [diff] [blame] | 1630 | return ws_blank_line(line, len, ecbdata->ws_rule); |
Junio C Hamano | 6957eb9 | 2009-09-14 18:44:01 -0700 | [diff] [blame] | 1631 | } |
| 1632 | |
Jeff King | e04df61 | 2019-02-14 00:48:13 -0500 | [diff] [blame] | 1633 | static void emit_add_line(struct emit_callback *ecbdata, |
Junio C Hamano | 018cff7 | 2009-09-14 18:44:01 -0700 | [diff] [blame] | 1634 | const char *line, int len) |
Junio C Hamano | 6957eb9 | 2009-09-14 18:44:01 -0700 | [diff] [blame] | 1635 | { |
Stefan Beller | 091f8e2 | 2017-06-29 17:06:53 -0700 | [diff] [blame] | 1636 | unsigned flags = WSEH_NEW | ecbdata->ws_rule; |
| 1637 | if (new_blank_line_at_eof(ecbdata, line, len)) |
| 1638 | flags |= DIFF_SYMBOL_CONTENT_BLANK_LINE_EOF; |
| 1639 | |
| 1640 | emit_diff_symbol(ecbdata->opt, DIFF_SYMBOL_PLUS, line, len, flags); |
Junio C Hamano | 6957eb9 | 2009-09-14 18:44:01 -0700 | [diff] [blame] | 1641 | } |
Junio C Hamano | 6957eb9 | 2009-09-14 18:44:01 -0700 | [diff] [blame] | 1642 | |
Jeff King | e04df61 | 2019-02-14 00:48:13 -0500 | [diff] [blame] | 1643 | static void emit_del_line(struct emit_callback *ecbdata, |
Junio C Hamano | 0e383e1 | 2015-05-26 09:56:33 -0700 | [diff] [blame] | 1644 | const char *line, int len) |
| 1645 | { |
Stefan Beller | 091f8e2 | 2017-06-29 17:06:53 -0700 | [diff] [blame] | 1646 | unsigned flags = WSEH_OLD | ecbdata->ws_rule; |
| 1647 | emit_diff_symbol(ecbdata->opt, DIFF_SYMBOL_MINUS, line, len, flags); |
Junio C Hamano | 0e383e1 | 2015-05-26 09:56:33 -0700 | [diff] [blame] | 1648 | } |
| 1649 | |
Jeff King | e04df61 | 2019-02-14 00:48:13 -0500 | [diff] [blame] | 1650 | static void emit_context_line(struct emit_callback *ecbdata, |
Junio C Hamano | 0e383e1 | 2015-05-26 09:56:33 -0700 | [diff] [blame] | 1651 | const char *line, int len) |
| 1652 | { |
Stefan Beller | 091f8e2 | 2017-06-29 17:06:53 -0700 | [diff] [blame] | 1653 | unsigned flags = WSEH_CONTEXT | ecbdata->ws_rule; |
| 1654 | emit_diff_symbol(ecbdata->opt, DIFF_SYMBOL_CONTEXT, line, len, flags); |
Junio C Hamano | 6957eb9 | 2009-09-14 18:44:01 -0700 | [diff] [blame] | 1655 | } |
| 1656 | |
Bert Wesarg | 89cb73a | 2009-11-27 07:55:18 +0100 | [diff] [blame] | 1657 | static void emit_hunk_header(struct emit_callback *ecbdata, |
| 1658 | const char *line, int len) |
| 1659 | { |
Jeff King | 8dbf3eb | 2015-05-27 16:48:46 -0400 | [diff] [blame] | 1660 | const char *context = diff_get_color(ecbdata->color_diff, DIFF_CONTEXT); |
Bert Wesarg | 89cb73a | 2009-11-27 07:55:18 +0100 | [diff] [blame] | 1661 | const char *frag = diff_get_color(ecbdata->color_diff, DIFF_FRAGINFO); |
| 1662 | const char *func = diff_get_color(ecbdata->color_diff, DIFF_FUNCINFO); |
| 1663 | const char *reset = diff_get_color(ecbdata->color_diff, DIFF_RESET); |
Johannes Schindelin | f7c3b4e | 2018-08-13 04:33:20 -0700 | [diff] [blame] | 1664 | const char *reverse = ecbdata->color_diff ? GIT_COLOR_REVERSE : ""; |
Bert Wesarg | 89cb73a | 2009-11-27 07:55:18 +0100 | [diff] [blame] | 1665 | static const char atat[2] = { '@', '@' }; |
| 1666 | const char *cp, *ep; |
Bo Yang | 2efcc97 | 2010-05-29 23:32:05 +0800 | [diff] [blame] | 1667 | struct strbuf msgbuf = STRBUF_INIT; |
| 1668 | int org_len = len; |
| 1669 | int i = 1; |
Bert Wesarg | 89cb73a | 2009-11-27 07:55:18 +0100 | [diff] [blame] | 1670 | |
| 1671 | /* |
| 1672 | * As a hunk header must begin with "@@ -<old>, +<new> @@", |
| 1673 | * it always is at least 10 bytes long. |
| 1674 | */ |
| 1675 | if (len < 10 || |
| 1676 | memcmp(line, atat, 2) || |
| 1677 | !(ep = memmem(line + 2, len - 2, atat, 2))) { |
Stefan Beller | c64b420 | 2017-06-29 17:06:50 -0700 | [diff] [blame] | 1678 | emit_diff_symbol(ecbdata->opt, |
Stefan Beller | 091f8e2 | 2017-06-29 17:06:53 -0700 | [diff] [blame] | 1679 | DIFF_SYMBOL_CONTEXT_MARKER, line, len, 0); |
Bert Wesarg | 89cb73a | 2009-11-27 07:55:18 +0100 | [diff] [blame] | 1680 | return; |
| 1681 | } |
| 1682 | ep += 2; /* skip over @@ */ |
| 1683 | |
| 1684 | /* The hunk header in fraginfo color */ |
Johannes Schindelin | f7c3b4e | 2018-08-13 04:33:20 -0700 | [diff] [blame] | 1685 | if (ecbdata->opt->flags.dual_color_diffed_diffs) |
| 1686 | strbuf_addstr(&msgbuf, reverse); |
René Scharfe | cedc61a | 2014-07-17 01:38:18 +0200 | [diff] [blame] | 1687 | strbuf_addstr(&msgbuf, frag); |
Thomas Gummerer | 430be36 | 2019-07-11 17:08:48 +0100 | [diff] [blame] | 1688 | if (ecbdata->opt->flags.suppress_hunk_header_line_count) |
| 1689 | strbuf_add(&msgbuf, atat, sizeof(atat)); |
| 1690 | else |
| 1691 | strbuf_add(&msgbuf, line, ep - line); |
René Scharfe | cedc61a | 2014-07-17 01:38:18 +0200 | [diff] [blame] | 1692 | strbuf_addstr(&msgbuf, reset); |
Bo Yang | 2efcc97 | 2010-05-29 23:32:05 +0800 | [diff] [blame] | 1693 | |
| 1694 | /* |
| 1695 | * trailing "\r\n" |
| 1696 | */ |
| 1697 | for ( ; i < 3; i++) |
| 1698 | if (line[len - i] == '\r' || line[len - i] == '\n') |
| 1699 | len--; |
Bert Wesarg | 89cb73a | 2009-11-27 07:55:18 +0100 | [diff] [blame] | 1700 | |
| 1701 | /* blank before the func header */ |
| 1702 | for (cp = ep; ep - line < len; ep++) |
| 1703 | if (*ep != ' ' && *ep != '\t') |
| 1704 | break; |
Bo Yang | 2efcc97 | 2010-05-29 23:32:05 +0800 | [diff] [blame] | 1705 | if (ep != cp) { |
Jeff King | 8dbf3eb | 2015-05-27 16:48:46 -0400 | [diff] [blame] | 1706 | strbuf_addstr(&msgbuf, context); |
Bo Yang | 2efcc97 | 2010-05-29 23:32:05 +0800 | [diff] [blame] | 1707 | strbuf_add(&msgbuf, cp, ep - cp); |
René Scharfe | cedc61a | 2014-07-17 01:38:18 +0200 | [diff] [blame] | 1708 | strbuf_addstr(&msgbuf, reset); |
Bo Yang | 2efcc97 | 2010-05-29 23:32:05 +0800 | [diff] [blame] | 1709 | } |
Bert Wesarg | 89cb73a | 2009-11-27 07:55:18 +0100 | [diff] [blame] | 1710 | |
Bo Yang | 2efcc97 | 2010-05-29 23:32:05 +0800 | [diff] [blame] | 1711 | if (ep < line + len) { |
René Scharfe | cedc61a | 2014-07-17 01:38:18 +0200 | [diff] [blame] | 1712 | strbuf_addstr(&msgbuf, func); |
Bo Yang | 2efcc97 | 2010-05-29 23:32:05 +0800 | [diff] [blame] | 1713 | strbuf_add(&msgbuf, ep, line + len - ep); |
René Scharfe | cedc61a | 2014-07-17 01:38:18 +0200 | [diff] [blame] | 1714 | strbuf_addstr(&msgbuf, reset); |
Bo Yang | 2efcc97 | 2010-05-29 23:32:05 +0800 | [diff] [blame] | 1715 | } |
| 1716 | |
| 1717 | strbuf_add(&msgbuf, line + len, org_len - len); |
Stefan Beller | dfb7728 | 2017-06-29 17:06:47 -0700 | [diff] [blame] | 1718 | strbuf_complete_line(&msgbuf); |
Stefan Beller | 68abc6f | 2017-06-29 17:06:51 -0700 | [diff] [blame] | 1719 | emit_diff_symbol(ecbdata->opt, |
Stefan Beller | 091f8e2 | 2017-06-29 17:06:53 -0700 | [diff] [blame] | 1720 | DIFF_SYMBOL_CONTEXT_FRAGINFO, msgbuf.buf, msgbuf.len, 0); |
Bo Yang | 2efcc97 | 2010-05-29 23:32:05 +0800 | [diff] [blame] | 1721 | strbuf_release(&msgbuf); |
Bert Wesarg | 89cb73a | 2009-11-27 07:55:18 +0100 | [diff] [blame] | 1722 | } |
| 1723 | |
Nguyễn Thái Ngọc Duy | 3b33576 | 2018-12-09 11:25:21 +0100 | [diff] [blame] | 1724 | static struct diff_tempfile *claim_diff_tempfile(void) |
| 1725 | { |
Jeff King | 479b0ae | 2009-01-22 00:59:56 -0500 | [diff] [blame] | 1726 | int i; |
| 1727 | for (i = 0; i < ARRAY_SIZE(diff_temp); i++) |
| 1728 | if (!diff_temp[i].name) |
| 1729 | return diff_temp + i; |
Johannes Schindelin | 033abf9 | 2018-05-02 11:38:39 +0200 | [diff] [blame] | 1730 | BUG("diff is failing to clean up its tempfiles"); |
Jeff King | 479b0ae | 2009-01-22 00:59:56 -0500 | [diff] [blame] | 1731 | } |
| 1732 | |
Jeff King | 479b0ae | 2009-01-22 00:59:56 -0500 | [diff] [blame] | 1733 | static void remove_tempfile(void) |
| 1734 | { |
| 1735 | int i; |
Nazri Ramliy | a8344ab | 2009-02-12 21:36:14 +0800 | [diff] [blame] | 1736 | for (i = 0; i < ARRAY_SIZE(diff_temp); i++) { |
Jeff King | 076aa2c | 2017-09-05 08:15:08 -0400 | [diff] [blame] | 1737 | if (is_tempfile_active(diff_temp[i].tempfile)) |
Michael Haggerty | 284098f | 2015-08-12 19:12:01 +0200 | [diff] [blame] | 1738 | delete_tempfile(&diff_temp[i].tempfile); |
Nazri Ramliy | a8344ab | 2009-02-12 21:36:14 +0800 | [diff] [blame] | 1739 | diff_temp[i].name = NULL; |
| 1740 | } |
Jeff King | 479b0ae | 2009-01-22 00:59:56 -0500 | [diff] [blame] | 1741 | } |
| 1742 | |
Stefan Beller | 5af6ea9 | 2017-06-29 17:06:59 -0700 | [diff] [blame] | 1743 | static void add_line_count(struct strbuf *out, int count) |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 1744 | { |
| 1745 | switch (count) { |
| 1746 | case 0: |
Stefan Beller | 5af6ea9 | 2017-06-29 17:06:59 -0700 | [diff] [blame] | 1747 | strbuf_addstr(out, "0,0"); |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 1748 | break; |
| 1749 | case 1: |
Stefan Beller | 5af6ea9 | 2017-06-29 17:06:59 -0700 | [diff] [blame] | 1750 | strbuf_addstr(out, "1"); |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 1751 | break; |
| 1752 | default: |
Stefan Beller | 5af6ea9 | 2017-06-29 17:06:59 -0700 | [diff] [blame] | 1753 | strbuf_addf(out, "1,%d", count); |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 1754 | break; |
| 1755 | } |
| 1756 | } |
| 1757 | |
Junio C Hamano | 7f7ee2f | 2009-09-14 18:44:01 -0700 | [diff] [blame] | 1758 | static void emit_rewrite_lines(struct emit_callback *ecb, |
| 1759 | int prefix, const char *data, int size) |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 1760 | { |
Junio C Hamano | 7f7ee2f | 2009-09-14 18:44:01 -0700 | [diff] [blame] | 1761 | const char *endp = NULL; |
Junio C Hamano | 7f7ee2f | 2009-09-14 18:44:01 -0700 | [diff] [blame] | 1762 | |
| 1763 | while (0 < size) { |
| 1764 | int len; |
| 1765 | |
| 1766 | endp = memchr(data, '\n', size); |
| 1767 | len = endp ? (endp - data + 1) : size; |
| 1768 | if (prefix != '+') { |
| 1769 | ecb->lno_in_preimage++; |
Jeff King | e04df61 | 2019-02-14 00:48:13 -0500 | [diff] [blame] | 1770 | emit_del_line(ecb, data, len); |
Junio C Hamano | 7f7ee2f | 2009-09-14 18:44:01 -0700 | [diff] [blame] | 1771 | } else { |
| 1772 | ecb->lno_in_postimage++; |
Jeff King | e04df61 | 2019-02-14 00:48:13 -0500 | [diff] [blame] | 1773 | emit_add_line(ecb, data, len); |
Johannes Schindelin | 13e36ec | 2007-02-20 15:08:46 +0100 | [diff] [blame] | 1774 | } |
Junio C Hamano | 7f7ee2f | 2009-09-14 18:44:01 -0700 | [diff] [blame] | 1775 | size -= len; |
| 1776 | data += len; |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 1777 | } |
Stefan Beller | b9cbfde | 2017-06-29 17:06:52 -0700 | [diff] [blame] | 1778 | if (!endp) |
Stefan Beller | 091f8e2 | 2017-06-29 17:06:53 -0700 | [diff] [blame] | 1779 | emit_diff_symbol(ecb->opt, DIFF_SYMBOL_NO_LF_EOF, NULL, 0, 0); |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 1780 | } |
| 1781 | |
| 1782 | static void emit_rewrite_diff(const char *name_a, |
| 1783 | const char *name_b, |
| 1784 | struct diff_filespec *one, |
Johannes Schindelin | 13e36ec | 2007-02-20 15:08:46 +0100 | [diff] [blame] | 1785 | struct diff_filespec *two, |
Jeff King | d9bae1a | 2010-04-01 20:12:15 -0400 | [diff] [blame] | 1786 | struct userdiff_driver *textconv_one, |
| 1787 | struct userdiff_driver *textconv_two, |
Johannes Schindelin | eab9a40 | 2007-12-18 19:32:14 +0000 | [diff] [blame] | 1788 | struct diff_options *o) |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 1789 | { |
| 1790 | int lc_a, lc_b; |
Junio C Hamano | d562509 | 2007-12-26 17:13:36 -0800 | [diff] [blame] | 1791 | static struct strbuf a_name = STRBUF_INIT, b_name = STRBUF_INIT; |
Junio C Hamano | a5a818e | 2008-08-18 20:08:09 -0700 | [diff] [blame] | 1792 | const char *a_prefix, *b_prefix; |
Jeff King | 840383b | 2010-04-01 20:09:26 -0400 | [diff] [blame] | 1793 | char *data_one, *data_two; |
Jeff King | 3aa1f7c | 2008-12-09 03:13:21 -0500 | [diff] [blame] | 1794 | size_t size_one, size_two; |
Junio C Hamano | 7f7ee2f | 2009-09-14 18:44:01 -0700 | [diff] [blame] | 1795 | struct emit_callback ecbdata; |
Stefan Beller | 5af6ea9 | 2017-06-29 17:06:59 -0700 | [diff] [blame] | 1796 | struct strbuf out = STRBUF_INIT; |
Junio C Hamano | a5a818e | 2008-08-18 20:08:09 -0700 | [diff] [blame] | 1797 | |
Brandon Williams | 0d1e0e7 | 2017-10-31 11:19:11 -0700 | [diff] [blame] | 1798 | if (diff_mnemonic_prefix && o->flags.reverse_diff) { |
Junio C Hamano | a5a818e | 2008-08-18 20:08:09 -0700 | [diff] [blame] | 1799 | a_prefix = o->b_prefix; |
| 1800 | b_prefix = o->a_prefix; |
| 1801 | } else { |
| 1802 | a_prefix = o->a_prefix; |
| 1803 | b_prefix = o->b_prefix; |
| 1804 | } |
Junio C Hamano | 1a9eb3b | 2006-09-22 16:17:58 -0700 | [diff] [blame] | 1805 | |
Junio C Hamano | 8a13bec | 2007-02-24 01:42:06 -0800 | [diff] [blame] | 1806 | name_a += (*name_a == '/'); |
| 1807 | name_b += (*name_b == '/'); |
Junio C Hamano | 1a9eb3b | 2006-09-22 16:17:58 -0700 | [diff] [blame] | 1808 | |
Junio C Hamano | d562509 | 2007-12-26 17:13:36 -0800 | [diff] [blame] | 1809 | strbuf_reset(&a_name); |
| 1810 | strbuf_reset(&b_name); |
Junio C Hamano | a5a818e | 2008-08-18 20:08:09 -0700 | [diff] [blame] | 1811 | quote_two_c_style(&a_name, a_prefix, name_a, 0); |
| 1812 | quote_two_c_style(&b_name, b_prefix, name_b, 0); |
Junio C Hamano | d562509 | 2007-12-26 17:13:36 -0800 | [diff] [blame] | 1813 | |
Nguyễn Thái Ngọc Duy | 6afaf80 | 2018-09-21 17:57:22 +0200 | [diff] [blame] | 1814 | size_one = fill_textconv(o->repo, textconv_one, one, &data_one); |
| 1815 | size_two = fill_textconv(o->repo, textconv_two, two, &data_two); |
Jeff King | 3aa1f7c | 2008-12-09 03:13:21 -0500 | [diff] [blame] | 1816 | |
Junio C Hamano | d91ba8f | 2009-09-15 11:21:10 -0700 | [diff] [blame] | 1817 | memset(&ecbdata, 0, sizeof(ecbdata)); |
Jeff King | daa0c3d | 2011-08-17 22:04:23 -0700 | [diff] [blame] | 1818 | ecbdata.color_diff = want_color(o->use_color); |
Nguyễn Thái Ngọc Duy | 26d024e | 2018-09-21 17:57:37 +0200 | [diff] [blame] | 1819 | ecbdata.ws_rule = whitespace_rule(o->repo->index, name_b); |
Bo Yang | a3c158d | 2010-05-26 15:08:02 +0800 | [diff] [blame] | 1820 | ecbdata.opt = o; |
Junio C Hamano | d91ba8f | 2009-09-15 11:21:10 -0700 | [diff] [blame] | 1821 | if (ecbdata.ws_rule & WS_BLANK_AT_EOF) { |
| 1822 | mmfile_t mf1, mf2; |
| 1823 | mf1.ptr = (char *)data_one; |
| 1824 | mf2.ptr = (char *)data_two; |
| 1825 | mf1.size = size_one; |
| 1826 | mf2.size = size_two; |
| 1827 | check_blank_at_eof(&mf1, &mf2, &ecbdata); |
| 1828 | } |
| 1829 | ecbdata.lno_in_preimage = 1; |
| 1830 | ecbdata.lno_in_postimage = 1; |
| 1831 | |
Jeff King | 3aa1f7c | 2008-12-09 03:13:21 -0500 | [diff] [blame] | 1832 | lc_a = count_lines(data_one, size_one); |
| 1833 | lc_b = count_lines(data_two, size_two); |
Stefan Beller | 3ee8b7b | 2017-06-29 17:06:56 -0700 | [diff] [blame] | 1834 | |
| 1835 | emit_diff_symbol(o, DIFF_SYMBOL_FILEPAIR_MINUS, |
| 1836 | a_name.buf, a_name.len, 0); |
| 1837 | emit_diff_symbol(o, DIFF_SYMBOL_FILEPAIR_PLUS, |
| 1838 | b_name.buf, b_name.len, 0); |
| 1839 | |
Stefan Beller | 5af6ea9 | 2017-06-29 17:06:59 -0700 | [diff] [blame] | 1840 | strbuf_addstr(&out, "@@ -"); |
Junio C Hamano | 467ddc1 | 2011-02-28 16:11:55 -0800 | [diff] [blame] | 1841 | if (!o->irreversible_delete) |
Stefan Beller | 5af6ea9 | 2017-06-29 17:06:59 -0700 | [diff] [blame] | 1842 | add_line_count(&out, lc_a); |
Junio C Hamano | 467ddc1 | 2011-02-28 16:11:55 -0800 | [diff] [blame] | 1843 | else |
Stefan Beller | 5af6ea9 | 2017-06-29 17:06:59 -0700 | [diff] [blame] | 1844 | strbuf_addstr(&out, "?,?"); |
| 1845 | strbuf_addstr(&out, " +"); |
| 1846 | add_line_count(&out, lc_b); |
| 1847 | strbuf_addstr(&out, " @@\n"); |
| 1848 | emit_diff_symbol(o, DIFF_SYMBOL_REWRITE_DIFF, out.buf, out.len, 0); |
| 1849 | strbuf_release(&out); |
| 1850 | |
Junio C Hamano | 467ddc1 | 2011-02-28 16:11:55 -0800 | [diff] [blame] | 1851 | if (lc_a && !o->irreversible_delete) |
Junio C Hamano | d91ba8f | 2009-09-15 11:21:10 -0700 | [diff] [blame] | 1852 | emit_rewrite_lines(&ecbdata, '-', data_one, size_one); |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 1853 | if (lc_b) |
Junio C Hamano | d91ba8f | 2009-09-15 11:21:10 -0700 | [diff] [blame] | 1854 | emit_rewrite_lines(&ecbdata, '+', data_two, size_two); |
Jeff King | b76c056 | 2010-04-01 20:04:14 -0400 | [diff] [blame] | 1855 | if (textconv_one) |
Junio C Hamano | aed6ca5 | 2010-04-08 23:30:49 -0700 | [diff] [blame] | 1856 | free((char *)data_one); |
Jeff King | b76c056 | 2010-04-01 20:04:14 -0400 | [diff] [blame] | 1857 | if (textconv_two) |
Junio C Hamano | aed6ca5 | 2010-04-08 23:30:49 -0700 | [diff] [blame] | 1858 | free((char *)data_two); |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 1859 | } |
| 1860 | |
Johannes Schindelin | f59a59e | 2006-07-28 23:56:15 +0200 | [diff] [blame] | 1861 | struct diff_words_buffer { |
| 1862 | mmfile_t text; |
Ramsay Jones | 071bcaa | 2017-09-21 17:49:38 +0100 | [diff] [blame] | 1863 | unsigned long alloc; |
Johannes Schindelin | 2e5d200 | 2009-01-17 17:29:44 +0100 | [diff] [blame] | 1864 | struct diff_words_orig { |
| 1865 | const char *begin, *end; |
| 1866 | } *orig; |
| 1867 | int orig_nr, orig_alloc; |
Johannes Schindelin | f59a59e | 2006-07-28 23:56:15 +0200 | [diff] [blame] | 1868 | }; |
| 1869 | |
| 1870 | static void diff_words_append(char *line, unsigned long len, |
| 1871 | struct diff_words_buffer *buffer) |
| 1872 | { |
Johannes Schindelin | 23c1575 | 2009-01-17 17:29:43 +0100 | [diff] [blame] | 1873 | ALLOC_GROW(buffer->text.ptr, buffer->text.size + len, buffer->alloc); |
Johannes Schindelin | f59a59e | 2006-07-28 23:56:15 +0200 | [diff] [blame] | 1874 | line++; |
| 1875 | len--; |
| 1876 | memcpy(buffer->text.ptr + buffer->text.size, line, len); |
| 1877 | buffer->text.size += len; |
Johannes Schindelin | 2b6a541 | 2009-01-17 17:29:45 +0100 | [diff] [blame] | 1878 | buffer->text.ptr[buffer->text.size] = '\0'; |
Johannes Schindelin | f59a59e | 2006-07-28 23:56:15 +0200 | [diff] [blame] | 1879 | } |
| 1880 | |
Jonathan Nieder | 9cba13c | 2011-03-16 02:08:34 -0500 | [diff] [blame] | 1881 | struct diff_words_style_elem { |
Thomas Rast | 882749a | 2010-04-14 17:59:06 +0200 | [diff] [blame] | 1882 | const char *prefix; |
| 1883 | const char *suffix; |
| 1884 | const char *color; /* NULL; filled in by the setup code if |
| 1885 | * color is enabled */ |
| 1886 | }; |
| 1887 | |
Jonathan Nieder | 9cba13c | 2011-03-16 02:08:34 -0500 | [diff] [blame] | 1888 | struct diff_words_style { |
Thomas Rast | 882749a | 2010-04-14 17:59:06 +0200 | [diff] [blame] | 1889 | enum diff_words_type type; |
Brandon Williams | 63a01c3 | 2018-02-14 10:59:39 -0800 | [diff] [blame] | 1890 | struct diff_words_style_elem new_word, old_word, ctx; |
Thomas Rast | 882749a | 2010-04-14 17:59:06 +0200 | [diff] [blame] | 1891 | const char *newline; |
| 1892 | }; |
| 1893 | |
Stephen Boyd | c2e86ad | 2011-03-22 00:51:05 -0700 | [diff] [blame] | 1894 | static struct diff_words_style diff_words_styles[] = { |
Thomas Rast | 882749a | 2010-04-14 17:59:06 +0200 | [diff] [blame] | 1895 | { DIFF_WORDS_PORCELAIN, {"+", "\n"}, {"-", "\n"}, {" ", "\n"}, "~\n" }, |
| 1896 | { DIFF_WORDS_PLAIN, {"{+", "+}"}, {"[-", "-]"}, {"", ""}, "\n" }, |
| 1897 | { DIFF_WORDS_COLOR, {"", ""}, {"", ""}, {"", ""}, "\n" } |
| 1898 | }; |
| 1899 | |
Johannes Schindelin | f59a59e | 2006-07-28 23:56:15 +0200 | [diff] [blame] | 1900 | struct diff_words_data { |
Johannes Schindelin | f59a59e | 2006-07-28 23:56:15 +0200 | [diff] [blame] | 1901 | struct diff_words_buffer minus, plus; |
Johannes Schindelin | 2e5d200 | 2009-01-17 17:29:44 +0100 | [diff] [blame] | 1902 | const char *current_plus; |
Bo Yang | 4297c0a | 2010-05-29 23:32:06 +0800 | [diff] [blame] | 1903 | int last_minus; |
| 1904 | struct diff_options *opt; |
Johannes Schindelin | 2b6a541 | 2009-01-17 17:29:45 +0100 | [diff] [blame] | 1905 | regex_t *word_regex; |
Thomas Rast | 882749a | 2010-04-14 17:59:06 +0200 | [diff] [blame] | 1906 | enum diff_words_type type; |
| 1907 | struct diff_words_style *style; |
Johannes Schindelin | f59a59e | 2006-07-28 23:56:15 +0200 | [diff] [blame] | 1908 | }; |
| 1909 | |
Stefan Beller | bd03329 | 2017-06-29 17:07:03 -0700 | [diff] [blame] | 1910 | static int fn_out_diff_words_write_helper(struct diff_options *o, |
Thomas Rast | 882749a | 2010-04-14 17:59:06 +0200 | [diff] [blame] | 1911 | struct diff_words_style_elem *st_el, |
| 1912 | const char *newline, |
Stefan Beller | bd03329 | 2017-06-29 17:07:03 -0700 | [diff] [blame] | 1913 | size_t count, const char *buf) |
Thomas Rast | 882749a | 2010-04-14 17:59:06 +0200 | [diff] [blame] | 1914 | { |
Bo Yang | 4297c0a | 2010-05-29 23:32:06 +0800 | [diff] [blame] | 1915 | int print = 0; |
Stefan Beller | bd03329 | 2017-06-29 17:07:03 -0700 | [diff] [blame] | 1916 | struct strbuf sb = STRBUF_INIT; |
Bo Yang | 4297c0a | 2010-05-29 23:32:06 +0800 | [diff] [blame] | 1917 | |
Thomas Rast | 882749a | 2010-04-14 17:59:06 +0200 | [diff] [blame] | 1918 | while (count) { |
| 1919 | char *p = memchr(buf, '\n', count); |
Bo Yang | 4297c0a | 2010-05-29 23:32:06 +0800 | [diff] [blame] | 1920 | if (print) |
Stefan Beller | bd03329 | 2017-06-29 17:07:03 -0700 | [diff] [blame] | 1921 | strbuf_addstr(&sb, diff_line_prefix(o)); |
| 1922 | |
Thomas Rast | 882749a | 2010-04-14 17:59:06 +0200 | [diff] [blame] | 1923 | if (p != buf) { |
Stefan Beller | bd03329 | 2017-06-29 17:07:03 -0700 | [diff] [blame] | 1924 | const char *reset = st_el->color && *st_el->color ? |
| 1925 | GIT_COLOR_RESET : NULL; |
| 1926 | if (st_el->color && *st_el->color) |
| 1927 | strbuf_addstr(&sb, st_el->color); |
| 1928 | strbuf_addstr(&sb, st_el->prefix); |
| 1929 | strbuf_add(&sb, buf, p ? p - buf : count); |
| 1930 | strbuf_addstr(&sb, st_el->suffix); |
| 1931 | if (reset) |
| 1932 | strbuf_addstr(&sb, reset); |
Thomas Rast | 882749a | 2010-04-14 17:59:06 +0200 | [diff] [blame] | 1933 | } |
| 1934 | if (!p) |
Stefan Beller | bd03329 | 2017-06-29 17:07:03 -0700 | [diff] [blame] | 1935 | goto out; |
| 1936 | |
| 1937 | strbuf_addstr(&sb, newline); |
Thomas Rast | 882749a | 2010-04-14 17:59:06 +0200 | [diff] [blame] | 1938 | count -= p + 1 - buf; |
| 1939 | buf = p + 1; |
Bo Yang | 4297c0a | 2010-05-29 23:32:06 +0800 | [diff] [blame] | 1940 | print = 1; |
Stefan Beller | bd03329 | 2017-06-29 17:07:03 -0700 | [diff] [blame] | 1941 | if (count) { |
| 1942 | emit_diff_symbol(o, DIFF_SYMBOL_WORD_DIFF, |
| 1943 | sb.buf, sb.len, 0); |
| 1944 | strbuf_reset(&sb); |
| 1945 | } |
Thomas Rast | 882749a | 2010-04-14 17:59:06 +0200 | [diff] [blame] | 1946 | } |
Stefan Beller | bd03329 | 2017-06-29 17:07:03 -0700 | [diff] [blame] | 1947 | |
| 1948 | out: |
| 1949 | if (sb.len) |
| 1950 | emit_diff_symbol(o, DIFF_SYMBOL_WORD_DIFF, |
| 1951 | sb.buf, sb.len, 0); |
| 1952 | strbuf_release(&sb); |
Thomas Rast | 882749a | 2010-04-14 17:59:06 +0200 | [diff] [blame] | 1953 | return 0; |
| 1954 | } |
| 1955 | |
Bo Yang | 4297c0a | 2010-05-29 23:32:06 +0800 | [diff] [blame] | 1956 | /* |
| 1957 | * '--color-words' algorithm can be described as: |
| 1958 | * |
René Genz | 5621760 | 2017-04-30 17:48:27 +0200 | [diff] [blame] | 1959 | * 1. collect the minus/plus lines of a diff hunk, divided into |
Bo Yang | 4297c0a | 2010-05-29 23:32:06 +0800 | [diff] [blame] | 1960 | * minus-lines and plus-lines; |
| 1961 | * |
| 1962 | * 2. break both minus-lines and plus-lines into words and |
| 1963 | * place them into two mmfile_t with one word for each line; |
| 1964 | * |
| 1965 | * 3. use xdiff to run diff on the two mmfile_t to get the words level diff; |
| 1966 | * |
| 1967 | * And for the common parts of the both file, we output the plus side text. |
| 1968 | * diff_words->current_plus is used to trace the current position of the plus file |
| 1969 | * which printed. diff_words->last_minus is used to trace the last minus word |
| 1970 | * printed. |
| 1971 | * |
| 1972 | * For '--graph' to work with '--color-words', we need to output the graph prefix |
| 1973 | * on each line of color words output. Generally, there are two conditions on |
| 1974 | * which we should output the prefix. |
| 1975 | * |
| 1976 | * 1. diff_words->last_minus == 0 && |
| 1977 | * diff_words->current_plus == diff_words->plus.text.ptr |
| 1978 | * |
| 1979 | * that is: the plus text must start as a new line, and if there is no minus |
| 1980 | * word printed, a graph prefix must be printed. |
| 1981 | * |
| 1982 | * 2. diff_words->current_plus > diff_words->plus.text.ptr && |
| 1983 | * *(diff_words->current_plus - 1) == '\n' |
| 1984 | * |
| 1985 | * that is: a graph prefix must be printed following a '\n' |
| 1986 | */ |
| 1987 | static int color_words_output_graph_prefix(struct diff_words_data *diff_words) |
| 1988 | { |
| 1989 | if ((diff_words->last_minus == 0 && |
| 1990 | diff_words->current_plus == diff_words->plus.text.ptr) || |
| 1991 | (diff_words->current_plus > diff_words->plus.text.ptr && |
| 1992 | *(diff_words->current_plus - 1) == '\n')) { |
| 1993 | return 1; |
| 1994 | } else { |
| 1995 | return 0; |
| 1996 | } |
| 1997 | } |
| 1998 | |
Jeff King | 7c61e25 | 2018-11-02 02:37:18 -0400 | [diff] [blame] | 1999 | static void fn_out_diff_words_aux(void *priv, |
| 2000 | long minus_first, long minus_len, |
| 2001 | long plus_first, long plus_len, |
| 2002 | const char *func, long funclen) |
Johannes Schindelin | f59a59e | 2006-07-28 23:56:15 +0200 | [diff] [blame] | 2003 | { |
| 2004 | struct diff_words_data *diff_words = priv; |
Thomas Rast | 882749a | 2010-04-14 17:59:06 +0200 | [diff] [blame] | 2005 | struct diff_words_style *style = diff_words->style; |
Johannes Schindelin | 2e5d200 | 2009-01-17 17:29:44 +0100 | [diff] [blame] | 2006 | const char *minus_begin, *minus_end, *plus_begin, *plus_end; |
Bo Yang | 4297c0a | 2010-05-29 23:32:06 +0800 | [diff] [blame] | 2007 | struct diff_options *opt = diff_words->opt; |
John Keeping | 30997bb | 2013-02-07 20:15:27 +0000 | [diff] [blame] | 2008 | const char *line_prefix; |
Johannes Schindelin | f59a59e | 2006-07-28 23:56:15 +0200 | [diff] [blame] | 2009 | |
Bo Yang | 4297c0a | 2010-05-29 23:32:06 +0800 | [diff] [blame] | 2010 | assert(opt); |
John Keeping | 30997bb | 2013-02-07 20:15:27 +0000 | [diff] [blame] | 2011 | line_prefix = diff_line_prefix(opt); |
Bo Yang | 4297c0a | 2010-05-29 23:32:06 +0800 | [diff] [blame] | 2012 | |
Johannes Schindelin | 2e5d200 | 2009-01-17 17:29:44 +0100 | [diff] [blame] | 2013 | /* POSIX requires that first be decremented by one if len == 0... */ |
| 2014 | if (minus_len) { |
| 2015 | minus_begin = diff_words->minus.orig[minus_first].begin; |
| 2016 | minus_end = |
| 2017 | diff_words->minus.orig[minus_first + minus_len - 1].end; |
| 2018 | } else |
| 2019 | minus_begin = minus_end = |
| 2020 | diff_words->minus.orig[minus_first].end; |
| 2021 | |
| 2022 | if (plus_len) { |
| 2023 | plus_begin = diff_words->plus.orig[plus_first].begin; |
| 2024 | plus_end = diff_words->plus.orig[plus_first + plus_len - 1].end; |
| 2025 | } else |
| 2026 | plus_begin = plus_end = diff_words->plus.orig[plus_first].end; |
| 2027 | |
Bo Yang | 4297c0a | 2010-05-29 23:32:06 +0800 | [diff] [blame] | 2028 | if (color_words_output_graph_prefix(diff_words)) { |
| 2029 | fputs(line_prefix, diff_words->opt->file); |
| 2030 | } |
| 2031 | if (diff_words->current_plus != plus_begin) { |
Stefan Beller | bd03329 | 2017-06-29 17:07:03 -0700 | [diff] [blame] | 2032 | fn_out_diff_words_write_helper(diff_words->opt, |
Thomas Rast | 882749a | 2010-04-14 17:59:06 +0200 | [diff] [blame] | 2033 | &style->ctx, style->newline, |
| 2034 | plus_begin - diff_words->current_plus, |
Stefan Beller | bd03329 | 2017-06-29 17:07:03 -0700 | [diff] [blame] | 2035 | diff_words->current_plus); |
Bo Yang | 4297c0a | 2010-05-29 23:32:06 +0800 | [diff] [blame] | 2036 | } |
| 2037 | if (minus_begin != minus_end) { |
Stefan Beller | bd03329 | 2017-06-29 17:07:03 -0700 | [diff] [blame] | 2038 | fn_out_diff_words_write_helper(diff_words->opt, |
Brandon Williams | 63a01c3 | 2018-02-14 10:59:39 -0800 | [diff] [blame] | 2039 | &style->old_word, style->newline, |
Stefan Beller | bd03329 | 2017-06-29 17:07:03 -0700 | [diff] [blame] | 2040 | minus_end - minus_begin, minus_begin); |
Bo Yang | 4297c0a | 2010-05-29 23:32:06 +0800 | [diff] [blame] | 2041 | } |
| 2042 | if (plus_begin != plus_end) { |
Stefan Beller | bd03329 | 2017-06-29 17:07:03 -0700 | [diff] [blame] | 2043 | fn_out_diff_words_write_helper(diff_words->opt, |
Brandon Williams | 63a01c3 | 2018-02-14 10:59:39 -0800 | [diff] [blame] | 2044 | &style->new_word, style->newline, |
Stefan Beller | bd03329 | 2017-06-29 17:07:03 -0700 | [diff] [blame] | 2045 | plus_end - plus_begin, plus_begin); |
Bo Yang | 4297c0a | 2010-05-29 23:32:06 +0800 | [diff] [blame] | 2046 | } |
Johannes Schindelin | 2e5d200 | 2009-01-17 17:29:44 +0100 | [diff] [blame] | 2047 | |
| 2048 | diff_words->current_plus = plus_end; |
Bo Yang | 4297c0a | 2010-05-29 23:32:06 +0800 | [diff] [blame] | 2049 | diff_words->last_minus = minus_first; |
Johannes Schindelin | f59a59e | 2006-07-28 23:56:15 +0200 | [diff] [blame] | 2050 | } |
| 2051 | |
Johannes Schindelin | 2b6a541 | 2009-01-17 17:29:45 +0100 | [diff] [blame] | 2052 | /* This function starts looking at *begin, and returns 0 iff a word was found. */ |
| 2053 | static int find_word_boundaries(mmfile_t *buffer, regex_t *word_regex, |
| 2054 | int *begin, int *end) |
| 2055 | { |
| 2056 | if (word_regex && *begin < buffer->size) { |
| 2057 | regmatch_t match[1]; |
Johannes Schindelin | b7d36ff | 2016-09-21 20:24:14 +0200 | [diff] [blame] | 2058 | if (!regexec_buf(word_regex, buffer->ptr + *begin, |
| 2059 | buffer->size - *begin, 1, match, 0)) { |
Johannes Schindelin | 2b6a541 | 2009-01-17 17:29:45 +0100 | [diff] [blame] | 2060 | char *p = memchr(buffer->ptr + *begin + match[0].rm_so, |
| 2061 | '\n', match[0].rm_eo - match[0].rm_so); |
| 2062 | *end = p ? p - buffer->ptr : match[0].rm_eo + *begin; |
| 2063 | *begin += match[0].rm_so; |
| 2064 | return *begin >= *end; |
| 2065 | } |
| 2066 | return -1; |
Johannes Schindelin | f59a59e | 2006-07-28 23:56:15 +0200 | [diff] [blame] | 2067 | } |
| 2068 | |
Johannes Schindelin | 2b6a541 | 2009-01-17 17:29:45 +0100 | [diff] [blame] | 2069 | /* find the next word */ |
| 2070 | while (*begin < buffer->size && isspace(buffer->ptr[*begin])) |
| 2071 | (*begin)++; |
| 2072 | if (*begin >= buffer->size) |
| 2073 | return -1; |
| 2074 | |
| 2075 | /* find the end of the word */ |
| 2076 | *end = *begin + 1; |
| 2077 | while (*end < buffer->size && !isspace(buffer->ptr[*end])) |
| 2078 | (*end)++; |
| 2079 | |
| 2080 | return 0; |
| 2081 | } |
| 2082 | |
Johannes Schindelin | 23c1575 | 2009-01-17 17:29:43 +0100 | [diff] [blame] | 2083 | /* |
Johannes Schindelin | 2e5d200 | 2009-01-17 17:29:44 +0100 | [diff] [blame] | 2084 | * This function splits the words in buffer->text, stores the list with |
| 2085 | * newline separator into out, and saves the offsets of the original words |
| 2086 | * in buffer->orig. |
Johannes Schindelin | 23c1575 | 2009-01-17 17:29:43 +0100 | [diff] [blame] | 2087 | */ |
Johannes Schindelin | 2b6a541 | 2009-01-17 17:29:45 +0100 | [diff] [blame] | 2088 | static void diff_words_fill(struct diff_words_buffer *buffer, mmfile_t *out, |
| 2089 | regex_t *word_regex) |
Johannes Schindelin | 23c1575 | 2009-01-17 17:29:43 +0100 | [diff] [blame] | 2090 | { |
Johannes Schindelin | 2e5d200 | 2009-01-17 17:29:44 +0100 | [diff] [blame] | 2091 | int i, j; |
Johannes Schindelin | 2b6a541 | 2009-01-17 17:29:45 +0100 | [diff] [blame] | 2092 | long alloc = 0; |
Johannes Schindelin | 2e5d200 | 2009-01-17 17:29:44 +0100 | [diff] [blame] | 2093 | |
| 2094 | out->size = 0; |
Johannes Schindelin | 2b6a541 | 2009-01-17 17:29:45 +0100 | [diff] [blame] | 2095 | out->ptr = NULL; |
Johannes Schindelin | 2e5d200 | 2009-01-17 17:29:44 +0100 | [diff] [blame] | 2096 | |
| 2097 | /* fake an empty "0th" word */ |
| 2098 | ALLOC_GROW(buffer->orig, 1, buffer->orig_alloc); |
| 2099 | buffer->orig[0].begin = buffer->orig[0].end = buffer->text.ptr; |
| 2100 | buffer->orig_nr = 1; |
| 2101 | |
| 2102 | for (i = 0; i < buffer->text.size; i++) { |
Johannes Schindelin | 2b6a541 | 2009-01-17 17:29:45 +0100 | [diff] [blame] | 2103 | if (find_word_boundaries(&buffer->text, word_regex, &i, &j)) |
| 2104 | return; |
Johannes Schindelin | 2e5d200 | 2009-01-17 17:29:44 +0100 | [diff] [blame] | 2105 | |
| 2106 | /* store original boundaries */ |
| 2107 | ALLOC_GROW(buffer->orig, buffer->orig_nr + 1, |
| 2108 | buffer->orig_alloc); |
| 2109 | buffer->orig[buffer->orig_nr].begin = buffer->text.ptr + i; |
| 2110 | buffer->orig[buffer->orig_nr].end = buffer->text.ptr + j; |
| 2111 | buffer->orig_nr++; |
| 2112 | |
| 2113 | /* store one word */ |
Johannes Schindelin | 2b6a541 | 2009-01-17 17:29:45 +0100 | [diff] [blame] | 2114 | ALLOC_GROW(out->ptr, out->size + j - i + 1, alloc); |
Johannes Schindelin | 2e5d200 | 2009-01-17 17:29:44 +0100 | [diff] [blame] | 2115 | memcpy(out->ptr + out->size, buffer->text.ptr + i, j - i); |
| 2116 | out->ptr[out->size + j - i] = '\n'; |
| 2117 | out->size += j - i + 1; |
| 2118 | |
| 2119 | i = j - 1; |
Johannes Schindelin | f59a59e | 2006-07-28 23:56:15 +0200 | [diff] [blame] | 2120 | } |
| 2121 | } |
| 2122 | |
| 2123 | /* this executes the word diff on the accumulated buffers */ |
| 2124 | static void diff_words_show(struct diff_words_data *diff_words) |
| 2125 | { |
| 2126 | xpparam_t xpp; |
| 2127 | xdemitconf_t xecfg; |
Johannes Schindelin | f59a59e | 2006-07-28 23:56:15 +0200 | [diff] [blame] | 2128 | mmfile_t minus, plus; |
Thomas Rast | 882749a | 2010-04-14 17:59:06 +0200 | [diff] [blame] | 2129 | struct diff_words_style *style = diff_words->style; |
Johannes Schindelin | f59a59e | 2006-07-28 23:56:15 +0200 | [diff] [blame] | 2130 | |
Bo Yang | 4297c0a | 2010-05-29 23:32:06 +0800 | [diff] [blame] | 2131 | struct diff_options *opt = diff_words->opt; |
John Keeping | 30997bb | 2013-02-07 20:15:27 +0000 | [diff] [blame] | 2132 | const char *line_prefix; |
Bo Yang | 4297c0a | 2010-05-29 23:32:06 +0800 | [diff] [blame] | 2133 | |
| 2134 | assert(opt); |
John Keeping | 30997bb | 2013-02-07 20:15:27 +0000 | [diff] [blame] | 2135 | line_prefix = diff_line_prefix(opt); |
Bo Yang | 4297c0a | 2010-05-29 23:32:06 +0800 | [diff] [blame] | 2136 | |
Johannes Schindelin | 2e5d200 | 2009-01-17 17:29:44 +0100 | [diff] [blame] | 2137 | /* special case: only removal */ |
| 2138 | if (!diff_words->plus.text.size) { |
Stefan Beller | bd03329 | 2017-06-29 17:07:03 -0700 | [diff] [blame] | 2139 | emit_diff_symbol(diff_words->opt, DIFF_SYMBOL_WORD_DIFF, |
| 2140 | line_prefix, strlen(line_prefix), 0); |
| 2141 | fn_out_diff_words_write_helper(diff_words->opt, |
Brandon Williams | 63a01c3 | 2018-02-14 10:59:39 -0800 | [diff] [blame] | 2142 | &style->old_word, style->newline, |
Bo Yang | 4297c0a | 2010-05-29 23:32:06 +0800 | [diff] [blame] | 2143 | diff_words->minus.text.size, |
Stefan Beller | bd03329 | 2017-06-29 17:07:03 -0700 | [diff] [blame] | 2144 | diff_words->minus.text.ptr); |
Johannes Schindelin | 2e5d200 | 2009-01-17 17:29:44 +0100 | [diff] [blame] | 2145 | diff_words->minus.text.size = 0; |
| 2146 | return; |
| 2147 | } |
| 2148 | |
| 2149 | diff_words->current_plus = diff_words->plus.text.ptr; |
Bo Yang | 4297c0a | 2010-05-29 23:32:06 +0800 | [diff] [blame] | 2150 | diff_words->last_minus = 0; |
Johannes Schindelin | f59a59e | 2006-07-28 23:56:15 +0200 | [diff] [blame] | 2151 | |
Brian Downing | 9ccd0a8 | 2008-10-25 15:30:37 +0200 | [diff] [blame] | 2152 | memset(&xpp, 0, sizeof(xpp)); |
Johannes Schindelin | 30b2501 | 2007-07-04 19:05:46 +0100 | [diff] [blame] | 2153 | memset(&xecfg, 0, sizeof(xecfg)); |
Johannes Schindelin | 2b6a541 | 2009-01-17 17:29:45 +0100 | [diff] [blame] | 2154 | diff_words_fill(&diff_words->minus, &minus, diff_words->word_regex); |
| 2155 | diff_words_fill(&diff_words->plus, &plus, diff_words->word_regex); |
René Scharfe | 582aa00 | 2010-05-02 15:04:41 +0200 | [diff] [blame] | 2156 | xpp.flags = 0; |
Johannes Schindelin | 2b6a541 | 2009-01-17 17:29:45 +0100 | [diff] [blame] | 2157 | /* as only the hunk header will be parsed, we need a 0-context */ |
Johannes Schindelin | 2e5d200 | 2009-01-17 17:29:44 +0100 | [diff] [blame] | 2158 | xecfg.ctxlen = 0; |
Jeff King | 7c61e25 | 2018-11-02 02:37:18 -0400 | [diff] [blame] | 2159 | if (xdi_diff_outf(&minus, &plus, fn_out_diff_words_aux, NULL, |
Jeff King | 9346d6d | 2018-11-02 02:35:45 -0400 | [diff] [blame] | 2160 | diff_words, &xpp, &xecfg)) |
Jeff King | 3efb988 | 2015-09-24 19:12:23 -0400 | [diff] [blame] | 2161 | die("unable to generate word diff"); |
Johannes Schindelin | f59a59e | 2006-07-28 23:56:15 +0200 | [diff] [blame] | 2162 | free(minus.ptr); |
| 2163 | free(plus.ptr); |
Johannes Schindelin | 2e5d200 | 2009-01-17 17:29:44 +0100 | [diff] [blame] | 2164 | if (diff_words->current_plus != diff_words->plus.text.ptr + |
Bo Yang | 4297c0a | 2010-05-29 23:32:06 +0800 | [diff] [blame] | 2165 | diff_words->plus.text.size) { |
| 2166 | if (color_words_output_graph_prefix(diff_words)) |
Stefan Beller | bd03329 | 2017-06-29 17:07:03 -0700 | [diff] [blame] | 2167 | emit_diff_symbol(diff_words->opt, DIFF_SYMBOL_WORD_DIFF, |
| 2168 | line_prefix, strlen(line_prefix), 0); |
| 2169 | fn_out_diff_words_write_helper(diff_words->opt, |
Thomas Rast | 882749a | 2010-04-14 17:59:06 +0200 | [diff] [blame] | 2170 | &style->ctx, style->newline, |
Johannes Schindelin | 2e5d200 | 2009-01-17 17:29:44 +0100 | [diff] [blame] | 2171 | diff_words->plus.text.ptr + diff_words->plus.text.size |
Stefan Beller | bd03329 | 2017-06-29 17:07:03 -0700 | [diff] [blame] | 2172 | - diff_words->current_plus, diff_words->current_plus); |
Bo Yang | 4297c0a | 2010-05-29 23:32:06 +0800 | [diff] [blame] | 2173 | } |
Johannes Schindelin | f59a59e | 2006-07-28 23:56:15 +0200 | [diff] [blame] | 2174 | diff_words->minus.text.size = diff_words->plus.text.size = 0; |
Johannes Schindelin | f59a59e | 2006-07-28 23:56:15 +0200 | [diff] [blame] | 2175 | } |
| 2176 | |
Junio C Hamano | 76fd282 | 2009-10-30 10:09:06 -0700 | [diff] [blame] | 2177 | /* In "color-words" mode, show word-diff of words accumulated in the buffer */ |
| 2178 | static void diff_words_flush(struct emit_callback *ecbdata) |
| 2179 | { |
Stefan Beller | e6e045f | 2017-06-29 17:07:06 -0700 | [diff] [blame] | 2180 | struct diff_options *wo = ecbdata->diff_words->opt; |
| 2181 | |
Junio C Hamano | 76fd282 | 2009-10-30 10:09:06 -0700 | [diff] [blame] | 2182 | if (ecbdata->diff_words->minus.text.size || |
| 2183 | ecbdata->diff_words->plus.text.size) |
| 2184 | diff_words_show(ecbdata->diff_words); |
Stefan Beller | e6e045f | 2017-06-29 17:07:06 -0700 | [diff] [blame] | 2185 | |
| 2186 | if (wo->emitted_symbols) { |
| 2187 | struct diff_options *o = ecbdata->opt; |
| 2188 | struct emitted_diff_symbols *wol = wo->emitted_symbols; |
| 2189 | int i; |
| 2190 | |
| 2191 | /* |
| 2192 | * NEEDSWORK: |
| 2193 | * Instead of appending each, concat all words to a line? |
| 2194 | */ |
| 2195 | for (i = 0; i < wol->nr; i++) |
| 2196 | append_emitted_diff_symbol(o, &wol->buf[i]); |
| 2197 | |
| 2198 | for (i = 0; i < wol->nr; i++) |
| 2199 | free((void *)wol->buf[i].line); |
| 2200 | |
| 2201 | wol->nr = 0; |
| 2202 | } |
Junio C Hamano | 76fd282 | 2009-10-30 10:09:06 -0700 | [diff] [blame] | 2203 | } |
| 2204 | |
Nguyễn Thái Ngọc Duy | acd00ea | 2018-09-21 17:57:33 +0200 | [diff] [blame] | 2205 | static void diff_filespec_load_driver(struct diff_filespec *one, |
| 2206 | struct index_state *istate) |
Thomas Rast | 77d1a52 | 2012-03-14 19:24:08 +0100 | [diff] [blame] | 2207 | { |
| 2208 | /* Use already-loaded driver */ |
| 2209 | if (one->driver) |
| 2210 | return; |
| 2211 | |
| 2212 | if (S_ISREG(one->mode)) |
Nguyễn Thái Ngọc Duy | acd00ea | 2018-09-21 17:57:33 +0200 | [diff] [blame] | 2213 | one->driver = userdiff_find_by_path(istate, one->path); |
Thomas Rast | 77d1a52 | 2012-03-14 19:24:08 +0100 | [diff] [blame] | 2214 | |
| 2215 | /* Fallback to default settings */ |
| 2216 | if (!one->driver) |
| 2217 | one->driver = userdiff_find_by_name("default"); |
| 2218 | } |
| 2219 | |
Nguyễn Thái Ngọc Duy | acd00ea | 2018-09-21 17:57:33 +0200 | [diff] [blame] | 2220 | static const char *userdiff_word_regex(struct diff_filespec *one, |
| 2221 | struct index_state *istate) |
Thomas Rast | 77d1a52 | 2012-03-14 19:24:08 +0100 | [diff] [blame] | 2222 | { |
Nguyễn Thái Ngọc Duy | acd00ea | 2018-09-21 17:57:33 +0200 | [diff] [blame] | 2223 | diff_filespec_load_driver(one, istate); |
Thomas Rast | 77d1a52 | 2012-03-14 19:24:08 +0100 | [diff] [blame] | 2224 | return one->driver->word_regex; |
| 2225 | } |
| 2226 | |
| 2227 | static void init_diff_words_data(struct emit_callback *ecbdata, |
Thomas Rast | 6440d34 | 2012-03-14 19:24:09 +0100 | [diff] [blame] | 2228 | struct diff_options *orig_opts, |
Thomas Rast | 77d1a52 | 2012-03-14 19:24:08 +0100 | [diff] [blame] | 2229 | struct diff_filespec *one, |
| 2230 | struct diff_filespec *two) |
| 2231 | { |
| 2232 | int i; |
Thomas Rast | 6440d34 | 2012-03-14 19:24:09 +0100 | [diff] [blame] | 2233 | struct diff_options *o = xmalloc(sizeof(struct diff_options)); |
| 2234 | memcpy(o, orig_opts, sizeof(struct diff_options)); |
Thomas Rast | 77d1a52 | 2012-03-14 19:24:08 +0100 | [diff] [blame] | 2235 | |
| 2236 | ecbdata->diff_words = |
| 2237 | xcalloc(1, sizeof(struct diff_words_data)); |
| 2238 | ecbdata->diff_words->type = o->word_diff; |
| 2239 | ecbdata->diff_words->opt = o; |
Stefan Beller | e6e045f | 2017-06-29 17:07:06 -0700 | [diff] [blame] | 2240 | |
| 2241 | if (orig_opts->emitted_symbols) |
| 2242 | o->emitted_symbols = |
| 2243 | xcalloc(1, sizeof(struct emitted_diff_symbols)); |
| 2244 | |
Thomas Rast | 77d1a52 | 2012-03-14 19:24:08 +0100 | [diff] [blame] | 2245 | if (!o->word_regex) |
Nguyễn Thái Ngọc Duy | acd00ea | 2018-09-21 17:57:33 +0200 | [diff] [blame] | 2246 | o->word_regex = userdiff_word_regex(one, o->repo->index); |
Thomas Rast | 77d1a52 | 2012-03-14 19:24:08 +0100 | [diff] [blame] | 2247 | if (!o->word_regex) |
Nguyễn Thái Ngọc Duy | acd00ea | 2018-09-21 17:57:33 +0200 | [diff] [blame] | 2248 | o->word_regex = userdiff_word_regex(two, o->repo->index); |
Thomas Rast | 77d1a52 | 2012-03-14 19:24:08 +0100 | [diff] [blame] | 2249 | if (!o->word_regex) |
| 2250 | o->word_regex = diff_word_regex_cfg; |
| 2251 | if (o->word_regex) { |
| 2252 | ecbdata->diff_words->word_regex = (regex_t *) |
| 2253 | xmalloc(sizeof(regex_t)); |
| 2254 | if (regcomp(ecbdata->diff_words->word_regex, |
| 2255 | o->word_regex, |
| 2256 | REG_EXTENDED | REG_NEWLINE)) |
Nguyễn Thái Ngọc Duy | 1a07e59 | 2018-07-21 09:49:19 +0200 | [diff] [blame] | 2257 | die("invalid regular expression: %s", |
| 2258 | o->word_regex); |
Thomas Rast | 77d1a52 | 2012-03-14 19:24:08 +0100 | [diff] [blame] | 2259 | } |
| 2260 | for (i = 0; i < ARRAY_SIZE(diff_words_styles); i++) { |
| 2261 | if (o->word_diff == diff_words_styles[i].type) { |
| 2262 | ecbdata->diff_words->style = |
| 2263 | &diff_words_styles[i]; |
| 2264 | break; |
| 2265 | } |
| 2266 | } |
| 2267 | if (want_color(o->use_color)) { |
| 2268 | struct diff_words_style *st = ecbdata->diff_words->style; |
Brandon Williams | 63a01c3 | 2018-02-14 10:59:39 -0800 | [diff] [blame] | 2269 | st->old_word.color = diff_get_color_opt(o, DIFF_FILE_OLD); |
| 2270 | st->new_word.color = diff_get_color_opt(o, DIFF_FILE_NEW); |
Jeff King | 8dbf3eb | 2015-05-27 16:48:46 -0400 | [diff] [blame] | 2271 | st->ctx.color = diff_get_color_opt(o, DIFF_CONTEXT); |
Thomas Rast | 77d1a52 | 2012-03-14 19:24:08 +0100 | [diff] [blame] | 2272 | } |
| 2273 | } |
| 2274 | |
Johannes Schindelin | f59a59e | 2006-07-28 23:56:15 +0200 | [diff] [blame] | 2275 | static void free_diff_words_data(struct emit_callback *ecbdata) |
| 2276 | { |
| 2277 | if (ecbdata->diff_words) { |
Junio C Hamano | 76fd282 | 2009-10-30 10:09:06 -0700 | [diff] [blame] | 2278 | diff_words_flush(ecbdata); |
Stefan Beller | e6e045f | 2017-06-29 17:07:06 -0700 | [diff] [blame] | 2279 | free (ecbdata->diff_words->opt->emitted_symbols); |
Thomas Rast | 6440d34 | 2012-03-14 19:24:09 +0100 | [diff] [blame] | 2280 | free (ecbdata->diff_words->opt); |
Jim Meyering | 8e0f700 | 2008-01-31 18:26:32 +0100 | [diff] [blame] | 2281 | free (ecbdata->diff_words->minus.text.ptr); |
Johannes Schindelin | 2e5d200 | 2009-01-17 17:29:44 +0100 | [diff] [blame] | 2282 | free (ecbdata->diff_words->minus.orig); |
Jim Meyering | 8e0f700 | 2008-01-31 18:26:32 +0100 | [diff] [blame] | 2283 | free (ecbdata->diff_words->plus.text.ptr); |
Johannes Schindelin | 2e5d200 | 2009-01-17 17:29:44 +0100 | [diff] [blame] | 2284 | free (ecbdata->diff_words->plus.orig); |
Brandon Casey | ef5644e | 2010-09-09 14:02:45 -0500 | [diff] [blame] | 2285 | if (ecbdata->diff_words->word_regex) { |
| 2286 | regfree(ecbdata->diff_words->word_regex); |
| 2287 | free(ecbdata->diff_words->word_regex); |
| 2288 | } |
Ævar Arnfjörð Bjarmason | 6a83d90 | 2017-06-15 23:15:46 +0000 | [diff] [blame] | 2289 | FREE_AND_NULL(ecbdata->diff_words); |
Johannes Schindelin | f59a59e | 2006-07-28 23:56:15 +0200 | [diff] [blame] | 2290 | } |
| 2291 | } |
| 2292 | |
Jeff King | ce43697 | 2006-07-23 05:24:18 -0400 | [diff] [blame] | 2293 | const char *diff_get_color(int diff_use_color, enum color_diff ix) |
Johannes Schindelin | cd112ce | 2006-06-13 18:45:44 +0200 | [diff] [blame] | 2294 | { |
Jeff King | daa0c3d | 2011-08-17 22:04:23 -0700 | [diff] [blame] | 2295 | if (want_color(diff_use_color)) |
Linus Torvalds | 50f575f | 2006-06-22 13:53:31 -0700 | [diff] [blame] | 2296 | return diff_colors[ix]; |
| 2297 | return ""; |
Johannes Schindelin | cd112ce | 2006-06-13 18:45:44 +0200 | [diff] [blame] | 2298 | } |
| 2299 | |
John Keeping | f192223 | 2013-02-07 20:15:26 +0000 | [diff] [blame] | 2300 | const char *diff_line_prefix(struct diff_options *opt) |
| 2301 | { |
| 2302 | struct strbuf *msgbuf; |
| 2303 | if (!opt->output_prefix) |
| 2304 | return ""; |
| 2305 | |
| 2306 | msgbuf = opt->output_prefix(opt, opt->output_prefix_data); |
| 2307 | return msgbuf->buf; |
| 2308 | } |
| 2309 | |
Jeff King | 19b9046 | 2019-02-14 00:48:27 -0500 | [diff] [blame] | 2310 | static unsigned long sane_truncate_line(char *line, unsigned long len) |
Junio C Hamano | 2370781 | 2008-01-02 01:50:11 -0800 | [diff] [blame] | 2311 | { |
| 2312 | const char *cp; |
| 2313 | unsigned long allot; |
| 2314 | size_t l = len; |
| 2315 | |
Junio C Hamano | 2370781 | 2008-01-02 01:50:11 -0800 | [diff] [blame] | 2316 | cp = line; |
| 2317 | allot = l; |
| 2318 | while (0 < l) { |
| 2319 | (void) utf8_width(&cp, &l); |
| 2320 | if (!cp) |
| 2321 | break; /* truncated in the middle? */ |
| 2322 | } |
| 2323 | return allot - l; |
| 2324 | } |
| 2325 | |
Junio C Hamano | d68fe26 | 2009-09-14 22:05:57 -0700 | [diff] [blame] | 2326 | static void find_lno(const char *line, struct emit_callback *ecbdata) |
Junio C Hamano | 690ed84 | 2009-09-04 00:41:15 -0700 | [diff] [blame] | 2327 | { |
Junio C Hamano | d68fe26 | 2009-09-14 22:05:57 -0700 | [diff] [blame] | 2328 | const char *p; |
| 2329 | ecbdata->lno_in_preimage = 0; |
| 2330 | ecbdata->lno_in_postimage = 0; |
| 2331 | p = strchr(line, '-'); |
Junio C Hamano | 690ed84 | 2009-09-04 00:41:15 -0700 | [diff] [blame] | 2332 | if (!p) |
Junio C Hamano | d68fe26 | 2009-09-14 22:05:57 -0700 | [diff] [blame] | 2333 | return; /* cannot happen */ |
| 2334 | ecbdata->lno_in_preimage = strtol(p + 1, NULL, 10); |
| 2335 | p = strchr(p, '+'); |
| 2336 | if (!p) |
| 2337 | return; /* cannot happen */ |
| 2338 | ecbdata->lno_in_postimage = strtol(p + 1, NULL, 10); |
Junio C Hamano | 690ed84 | 2009-09-04 00:41:15 -0700 | [diff] [blame] | 2339 | } |
| 2340 | |
Johannes Schindelin | cd112ce | 2006-06-13 18:45:44 +0200 | [diff] [blame] | 2341 | static void fn_out_consume(void *priv, char *line, unsigned long len) |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 2342 | { |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 2343 | struct emit_callback *ecbdata = priv; |
Bo Yang | 7be5761 | 2010-05-26 15:23:54 +0800 | [diff] [blame] | 2344 | struct diff_options *o = ecbdata->opt; |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 2345 | |
Stefan Beller | ba16233 | 2016-09-07 16:36:46 -0700 | [diff] [blame] | 2346 | o->found_changes = 1; |
| 2347 | |
Greg Bacon | 3e97c7c | 2009-11-19 15:12:24 -0600 | [diff] [blame] | 2348 | if (ecbdata->header) { |
Stefan Beller | a29b0a1 | 2017-06-29 17:06:57 -0700 | [diff] [blame] | 2349 | emit_diff_symbol(o, DIFF_SYMBOL_HEADER, |
| 2350 | ecbdata->header->buf, ecbdata->header->len, 0); |
Greg Bacon | 3e97c7c | 2009-11-19 15:12:24 -0600 | [diff] [blame] | 2351 | strbuf_reset(ecbdata->header); |
| 2352 | ecbdata->header = NULL; |
| 2353 | } |
Johannes Schindelin | 34a5e1a | 2007-02-25 23:34:54 +0100 | [diff] [blame] | 2354 | |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 2355 | if (ecbdata->label_path[0]) { |
Stefan Beller | 3ee8b7b | 2017-06-29 17:06:56 -0700 | [diff] [blame] | 2356 | emit_diff_symbol(o, DIFF_SYMBOL_FILEPAIR_MINUS, |
| 2357 | ecbdata->label_path[0], |
| 2358 | strlen(ecbdata->label_path[0]), 0); |
| 2359 | emit_diff_symbol(o, DIFF_SYMBOL_FILEPAIR_PLUS, |
| 2360 | ecbdata->label_path[1], |
| 2361 | strlen(ecbdata->label_path[1]), 0); |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 2362 | ecbdata->label_path[0] = ecbdata->label_path[1] = NULL; |
| 2363 | } |
Johannes Schindelin | cd112ce | 2006-06-13 18:45:44 +0200 | [diff] [blame] | 2364 | |
Jim Meyering | a624eaa | 2008-08-15 13:39:26 +0200 | [diff] [blame] | 2365 | if (diff_suppress_blank_empty |
| 2366 | && len == 2 && line[0] == ' ' && line[1] == '\n') { |
| 2367 | line[0] = '\n'; |
| 2368 | len = 1; |
| 2369 | } |
| 2370 | |
Junio C Hamano | b8d9c1a | 2009-09-03 23:59:25 -0700 | [diff] [blame] | 2371 | if (line[0] == '@') { |
Junio C Hamano | 76fd282 | 2009-10-30 10:09:06 -0700 | [diff] [blame] | 2372 | if (ecbdata->diff_words) |
| 2373 | diff_words_flush(ecbdata); |
Jeff King | 19b9046 | 2019-02-14 00:48:27 -0500 | [diff] [blame] | 2374 | len = sane_truncate_line(line, len); |
Junio C Hamano | d68fe26 | 2009-09-14 22:05:57 -0700 | [diff] [blame] | 2375 | find_lno(line, ecbdata); |
Bert Wesarg | 89cb73a | 2009-11-27 07:55:18 +0100 | [diff] [blame] | 2376 | emit_hunk_header(ecbdata, line, len); |
Junio C Hamano | 448c3ef | 2006-09-22 22:48:39 -0700 | [diff] [blame] | 2377 | return; |
Johannes Schindelin | cd112ce | 2006-06-13 18:45:44 +0200 | [diff] [blame] | 2378 | } |
Junio C Hamano | 448c3ef | 2006-09-22 22:48:39 -0700 | [diff] [blame] | 2379 | |
Junio C Hamano | 448c3ef | 2006-09-22 22:48:39 -0700 | [diff] [blame] | 2380 | if (ecbdata->diff_words) { |
Stefan Beller | ff95867 | 2017-06-29 17:06:54 -0700 | [diff] [blame] | 2381 | enum diff_symbol s = |
| 2382 | ecbdata->diff_words->type == DIFF_WORDS_PORCELAIN ? |
| 2383 | DIFF_SYMBOL_WORDS_PORCELAIN : DIFF_SYMBOL_WORDS; |
Junio C Hamano | 448c3ef | 2006-09-22 22:48:39 -0700 | [diff] [blame] | 2384 | if (line[0] == '-') { |
| 2385 | diff_words_append(line, len, |
| 2386 | &ecbdata->diff_words->minus); |
| 2387 | return; |
| 2388 | } else if (line[0] == '+') { |
| 2389 | diff_words_append(line, len, |
| 2390 | &ecbdata->diff_words->plus); |
| 2391 | return; |
Christian Couder | 5955654 | 2013-11-30 21:55:40 +0100 | [diff] [blame] | 2392 | } else if (starts_with(line, "\\ ")) { |
Thomas Rast | c7c2bc0 | 2012-01-12 12:15:33 +0100 | [diff] [blame] | 2393 | /* |
| 2394 | * Eat the "no newline at eof" marker as if we |
| 2395 | * saw a "+" or "-" line with nothing on it, |
| 2396 | * and return without diff_words_flush() to |
| 2397 | * defer processing. If this is the end of |
| 2398 | * preimage, more "+" lines may come after it. |
| 2399 | */ |
| 2400 | return; |
Junio C Hamano | 448c3ef | 2006-09-22 22:48:39 -0700 | [diff] [blame] | 2401 | } |
Junio C Hamano | 76fd282 | 2009-10-30 10:09:06 -0700 | [diff] [blame] | 2402 | diff_words_flush(ecbdata); |
Stefan Beller | ff95867 | 2017-06-29 17:06:54 -0700 | [diff] [blame] | 2403 | emit_diff_symbol(o, s, line, len, 0); |
Junio C Hamano | 448c3ef | 2006-09-22 22:48:39 -0700 | [diff] [blame] | 2404 | return; |
| 2405 | } |
Junio C Hamano | 448c3ef | 2006-09-22 22:48:39 -0700 | [diff] [blame] | 2406 | |
Junio C Hamano | 0e383e1 | 2015-05-26 09:56:33 -0700 | [diff] [blame] | 2407 | switch (line[0]) { |
| 2408 | case '+': |
Junio C Hamano | d68fe26 | 2009-09-14 22:05:57 -0700 | [diff] [blame] | 2409 | ecbdata->lno_in_postimage++; |
Jeff King | e04df61 | 2019-02-14 00:48:13 -0500 | [diff] [blame] | 2410 | emit_add_line(ecbdata, line + 1, len - 1); |
Junio C Hamano | 0e383e1 | 2015-05-26 09:56:33 -0700 | [diff] [blame] | 2411 | break; |
| 2412 | case '-': |
| 2413 | ecbdata->lno_in_preimage++; |
Jeff King | e04df61 | 2019-02-14 00:48:13 -0500 | [diff] [blame] | 2414 | emit_del_line(ecbdata, line + 1, len - 1); |
Junio C Hamano | 0e383e1 | 2015-05-26 09:56:33 -0700 | [diff] [blame] | 2415 | break; |
| 2416 | case ' ': |
| 2417 | ecbdata->lno_in_postimage++; |
| 2418 | ecbdata->lno_in_preimage++; |
Jeff King | e04df61 | 2019-02-14 00:48:13 -0500 | [diff] [blame] | 2419 | emit_context_line(ecbdata, line + 1, len - 1); |
Junio C Hamano | 0e383e1 | 2015-05-26 09:56:33 -0700 | [diff] [blame] | 2420 | break; |
| 2421 | default: |
| 2422 | /* incomplete line at the end */ |
| 2423 | ecbdata->lno_in_preimage++; |
Stefan Beller | f2bb121 | 2017-06-29 17:06:55 -0700 | [diff] [blame] | 2424 | emit_diff_symbol(o, DIFF_SYMBOL_CONTEXT_INCOMPLETE, |
| 2425 | line, len, 0); |
Junio C Hamano | 0e383e1 | 2015-05-26 09:56:33 -0700 | [diff] [blame] | 2426 | break; |
Junio C Hamano | 448c3ef | 2006-09-22 22:48:39 -0700 | [diff] [blame] | 2427 | } |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 2428 | } |
| 2429 | |
Nguyễn Thái Ngọc Duy | c905cbc | 2018-02-01 20:02:20 +0700 | [diff] [blame] | 2430 | static void pprint_rename(struct strbuf *name, const char *a, const char *b) |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 2431 | { |
Brandon Williams | 63a01c3 | 2018-02-14 10:59:39 -0800 | [diff] [blame] | 2432 | const char *old_name = a; |
| 2433 | const char *new_name = b; |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 2434 | int pfx_length, sfx_length; |
Thomas Rast | dd281f0 | 2013-02-26 21:47:01 +0100 | [diff] [blame] | 2435 | int pfx_adjust_for_slash; |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 2436 | int len_a = strlen(a); |
| 2437 | int len_b = strlen(b); |
Pierre Habouzit | 663af34 | 2007-09-20 00:42:15 +0200 | [diff] [blame] | 2438 | int a_midlen, b_midlen; |
Alexandre Julliard | e5bfbf9 | 2007-02-10 15:39:00 +0100 | [diff] [blame] | 2439 | int qlen_a = quote_c_style(a, NULL, NULL, 0); |
| 2440 | int qlen_b = quote_c_style(b, NULL, NULL, 0); |
| 2441 | |
| 2442 | if (qlen_a || qlen_b) { |
Nguyễn Thái Ngọc Duy | c905cbc | 2018-02-01 20:02:20 +0700 | [diff] [blame] | 2443 | quote_c_style(a, name, NULL, 0); |
| 2444 | strbuf_addstr(name, " => "); |
| 2445 | quote_c_style(b, name, NULL, 0); |
| 2446 | return; |
Alexandre Julliard | e5bfbf9 | 2007-02-10 15:39:00 +0100 | [diff] [blame] | 2447 | } |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 2448 | |
| 2449 | /* Find common prefix */ |
| 2450 | pfx_length = 0; |
Brandon Williams | 63a01c3 | 2018-02-14 10:59:39 -0800 | [diff] [blame] | 2451 | while (*old_name && *new_name && *old_name == *new_name) { |
| 2452 | if (*old_name == '/') |
| 2453 | pfx_length = old_name - a + 1; |
| 2454 | old_name++; |
| 2455 | new_name++; |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 2456 | } |
| 2457 | |
| 2458 | /* Find common suffix */ |
Brandon Williams | 63a01c3 | 2018-02-14 10:59:39 -0800 | [diff] [blame] | 2459 | old_name = a + len_a; |
| 2460 | new_name = b + len_b; |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 2461 | sfx_length = 0; |
Antoine Pelisse | d020e27 | 2013-02-23 17:48:45 +0100 | [diff] [blame] | 2462 | /* |
Thomas Rast | dd281f0 | 2013-02-26 21:47:01 +0100 | [diff] [blame] | 2463 | * If there is a common prefix, it must end in a slash. In |
| 2464 | * that case we let this loop run 1 into the prefix to see the |
| 2465 | * same slash. |
| 2466 | * |
| 2467 | * If there is no common prefix, we cannot do this as it would |
| 2468 | * underrun the input strings. |
Antoine Pelisse | d020e27 | 2013-02-23 17:48:45 +0100 | [diff] [blame] | 2469 | */ |
Thomas Rast | dd281f0 | 2013-02-26 21:47:01 +0100 | [diff] [blame] | 2470 | pfx_adjust_for_slash = (pfx_length ? 1 : 0); |
Brandon Williams | 63a01c3 | 2018-02-14 10:59:39 -0800 | [diff] [blame] | 2471 | while (a + pfx_length - pfx_adjust_for_slash <= old_name && |
| 2472 | b + pfx_length - pfx_adjust_for_slash <= new_name && |
| 2473 | *old_name == *new_name) { |
| 2474 | if (*old_name == '/') |
| 2475 | sfx_length = len_a - (old_name - a); |
| 2476 | old_name--; |
| 2477 | new_name--; |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 2478 | } |
| 2479 | |
| 2480 | /* |
| 2481 | * pfx{mid-a => mid-b}sfx |
| 2482 | * {pfx-a => pfx-b}sfx |
| 2483 | * pfx{sfx-a => sfx-b} |
| 2484 | * name-a => name-b |
| 2485 | */ |
Pierre Habouzit | 663af34 | 2007-09-20 00:42:15 +0200 | [diff] [blame] | 2486 | a_midlen = len_a - pfx_length - sfx_length; |
| 2487 | b_midlen = len_b - pfx_length - sfx_length; |
| 2488 | if (a_midlen < 0) |
| 2489 | a_midlen = 0; |
| 2490 | if (b_midlen < 0) |
| 2491 | b_midlen = 0; |
Junio C Hamano | cc908b8 | 2006-05-14 22:07:28 -0700 | [diff] [blame] | 2492 | |
Nguyễn Thái Ngọc Duy | c905cbc | 2018-02-01 20:02:20 +0700 | [diff] [blame] | 2493 | strbuf_grow(name, pfx_length + a_midlen + b_midlen + sfx_length + 7); |
Pierre Habouzit | 663af34 | 2007-09-20 00:42:15 +0200 | [diff] [blame] | 2494 | if (pfx_length + sfx_length) { |
Nguyễn Thái Ngọc Duy | c905cbc | 2018-02-01 20:02:20 +0700 | [diff] [blame] | 2495 | strbuf_add(name, a, pfx_length); |
| 2496 | strbuf_addch(name, '{'); |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 2497 | } |
Nguyễn Thái Ngọc Duy | c905cbc | 2018-02-01 20:02:20 +0700 | [diff] [blame] | 2498 | strbuf_add(name, a + pfx_length, a_midlen); |
| 2499 | strbuf_addstr(name, " => "); |
| 2500 | strbuf_add(name, b + pfx_length, b_midlen); |
Pierre Habouzit | 663af34 | 2007-09-20 00:42:15 +0200 | [diff] [blame] | 2501 | if (pfx_length + sfx_length) { |
Nguyễn Thái Ngọc Duy | c905cbc | 2018-02-01 20:02:20 +0700 | [diff] [blame] | 2502 | strbuf_addch(name, '}'); |
| 2503 | strbuf_add(name, a + len_a - sfx_length, sfx_length); |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 2504 | } |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 2505 | } |
| 2506 | |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 2507 | static struct diffstat_file *diffstat_add(struct diffstat_t *diffstat, |
| 2508 | const char *name_a, |
| 2509 | const char *name_b) |
| 2510 | { |
| 2511 | struct diffstat_file *x; |
Brian Gesiak | 1a4927c | 2014-05-27 00:33:47 +0900 | [diff] [blame] | 2512 | x = xcalloc(1, sizeof(*x)); |
Dmitry S. Dolzhenko | 4c960a4 | 2014-03-04 02:31:53 +0400 | [diff] [blame] | 2513 | ALLOC_GROW(diffstat->files, diffstat->nr + 1, diffstat->alloc); |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 2514 | diffstat->files[diffstat->nr++] = x; |
| 2515 | if (name_b) { |
Junio C Hamano | f604652 | 2007-12-11 23:46:30 -0800 | [diff] [blame] | 2516 | x->from_name = xstrdup(name_a); |
| 2517 | x->name = xstrdup(name_b); |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 2518 | x->is_renamed = 1; |
| 2519 | } |
Junio C Hamano | f604652 | 2007-12-11 23:46:30 -0800 | [diff] [blame] | 2520 | else { |
| 2521 | x->from_name = NULL; |
Shawn Pearce | 9befac4 | 2006-09-02 00:16:31 -0400 | [diff] [blame] | 2522 | x->name = xstrdup(name_a); |
Junio C Hamano | f604652 | 2007-12-11 23:46:30 -0800 | [diff] [blame] | 2523 | } |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 2524 | return x; |
| 2525 | } |
| 2526 | |
| 2527 | static void diffstat_consume(void *priv, char *line, unsigned long len) |
| 2528 | { |
| 2529 | struct diffstat_t *diffstat = priv; |
| 2530 | struct diffstat_file *x = diffstat->files[diffstat->nr - 1]; |
| 2531 | |
| 2532 | if (line[0] == '+') |
| 2533 | x->added++; |
| 2534 | else if (line[0] == '-') |
| 2535 | x->deleted++; |
| 2536 | } |
| 2537 | |
Johannes Schindelin | 698ce6f | 2006-05-20 15:40:29 +0200 | [diff] [blame] | 2538 | const char mime_boundary_leader[] = "------------"; |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 2539 | |
Junio C Hamano | a254002 | 2006-09-26 18:53:02 -0700 | [diff] [blame] | 2540 | static int scale_linear(int it, int width, int max_change) |
| 2541 | { |
Junio C Hamano | 2eeeef2 | 2012-02-14 13:49:11 -0800 | [diff] [blame] | 2542 | if (!it) |
| 2543 | return 0; |
Junio C Hamano | a254002 | 2006-09-26 18:53:02 -0700 | [diff] [blame] | 2544 | /* |
Junio C Hamano | 2eeeef2 | 2012-02-14 13:49:11 -0800 | [diff] [blame] | 2545 | * make sure that at least one '-' or '+' is printed if |
| 2546 | * there is any change to this path. The easiest way is to |
Elijah Newren | 15beaaa | 2019-11-05 17:07:23 +0000 | [diff] [blame] | 2547 | * scale linearly as if the allotted width is one column shorter |
Junio C Hamano | 2eeeef2 | 2012-02-14 13:49:11 -0800 | [diff] [blame] | 2548 | * than it is, and then add 1 to the result. |
Junio C Hamano | a254002 | 2006-09-26 18:53:02 -0700 | [diff] [blame] | 2549 | */ |
Junio C Hamano | 2eeeef2 | 2012-02-14 13:49:11 -0800 | [diff] [blame] | 2550 | return 1 + (it * (width - 1) / max_change); |
Junio C Hamano | a254002 | 2006-09-26 18:53:02 -0700 | [diff] [blame] | 2551 | } |
| 2552 | |
Stefan Beller | 0911c47 | 2017-06-29 17:07:02 -0700 | [diff] [blame] | 2553 | static void show_graph(struct strbuf *out, char ch, int cnt, |
| 2554 | const char *set, const char *reset) |
Junio C Hamano | a254002 | 2006-09-26 18:53:02 -0700 | [diff] [blame] | 2555 | { |
| 2556 | if (cnt <= 0) |
| 2557 | return; |
Stefan Beller | 0911c47 | 2017-06-29 17:07:02 -0700 | [diff] [blame] | 2558 | strbuf_addstr(out, set); |
| 2559 | strbuf_addchars(out, ch, cnt); |
| 2560 | strbuf_addstr(out, reset); |
Junio C Hamano | a254002 | 2006-09-26 18:53:02 -0700 | [diff] [blame] | 2561 | } |
| 2562 | |
Junio C Hamano | f604652 | 2007-12-11 23:46:30 -0800 | [diff] [blame] | 2563 | static void fill_print_name(struct diffstat_file *file) |
| 2564 | { |
Nguyễn Thái Ngọc Duy | c905cbc | 2018-02-01 20:02:20 +0700 | [diff] [blame] | 2565 | struct strbuf pname = STRBUF_INIT; |
Junio C Hamano | f604652 | 2007-12-11 23:46:30 -0800 | [diff] [blame] | 2566 | |
| 2567 | if (file->print_name) |
| 2568 | return; |
| 2569 | |
Nguyễn Thái Ngọc Duy | c905cbc | 2018-02-01 20:02:20 +0700 | [diff] [blame] | 2570 | if (file->is_renamed) |
| 2571 | pprint_rename(&pname, file->from_name, file->name); |
| 2572 | else |
| 2573 | quote_c_style(file->name, &pname, NULL, 0); |
| 2574 | |
Nguyễn Thái Ngọc Duy | ddf88fa | 2018-02-24 21:09:59 +0700 | [diff] [blame] | 2575 | if (file->comments) |
| 2576 | strbuf_addf(&pname, " (%s)", file->comments); |
| 2577 | |
Nguyễn Thái Ngọc Duy | c905cbc | 2018-02-01 20:02:20 +0700 | [diff] [blame] | 2578 | file->print_name = strbuf_detach(&pname, NULL); |
Junio C Hamano | f604652 | 2007-12-11 23:46:30 -0800 | [diff] [blame] | 2579 | } |
| 2580 | |
Stefan Beller | 0911c47 | 2017-06-29 17:07:02 -0700 | [diff] [blame] | 2581 | static void print_stat_summary_inserts_deletes(struct diff_options *options, |
| 2582 | int files, int insertions, int deletions) |
Nguyễn Thái Ngọc Duy | 7f81463 | 2012-02-01 19:55:07 +0700 | [diff] [blame] | 2583 | { |
| 2584 | struct strbuf sb = STRBUF_INIT; |
Nguyễn Thái Ngọc Duy | 7f81463 | 2012-02-01 19:55:07 +0700 | [diff] [blame] | 2585 | |
| 2586 | if (!files) { |
| 2587 | assert(insertions == 0 && deletions == 0); |
Stefan Beller | 0911c47 | 2017-06-29 17:07:02 -0700 | [diff] [blame] | 2588 | emit_diff_symbol(options, DIFF_SYMBOL_STATS_SUMMARY_NO_FILES, |
| 2589 | NULL, 0, 0); |
| 2590 | return; |
Nguyễn Thái Ngọc Duy | 7f81463 | 2012-02-01 19:55:07 +0700 | [diff] [blame] | 2591 | } |
| 2592 | |
| 2593 | strbuf_addf(&sb, |
Nguyễn Thái Ngọc Duy | 218adaa | 2012-09-13 21:16:26 +0700 | [diff] [blame] | 2594 | (files == 1) ? " %d file changed" : " %d files changed", |
Nguyễn Thái Ngọc Duy | 7f81463 | 2012-02-01 19:55:07 +0700 | [diff] [blame] | 2595 | files); |
| 2596 | |
| 2597 | /* |
| 2598 | * For binary diff, the caller may want to print "x files |
| 2599 | * changed" with insertions == 0 && deletions == 0. |
| 2600 | * |
| 2601 | * Not omitting "0 insertions(+), 0 deletions(-)" in this case |
| 2602 | * is probably less confusing (i.e skip over "2 files changed |
| 2603 | * but nothing about added/removed lines? Is this a bug in Git?"). |
| 2604 | */ |
| 2605 | if (insertions || deletions == 0) { |
Nguyễn Thái Ngọc Duy | 7f81463 | 2012-02-01 19:55:07 +0700 | [diff] [blame] | 2606 | strbuf_addf(&sb, |
Nguyễn Thái Ngọc Duy | 218adaa | 2012-09-13 21:16:26 +0700 | [diff] [blame] | 2607 | (insertions == 1) ? ", %d insertion(+)" : ", %d insertions(+)", |
Nguyễn Thái Ngọc Duy | 7f81463 | 2012-02-01 19:55:07 +0700 | [diff] [blame] | 2608 | insertions); |
| 2609 | } |
| 2610 | |
| 2611 | if (deletions || insertions == 0) { |
Nguyễn Thái Ngọc Duy | 7f81463 | 2012-02-01 19:55:07 +0700 | [diff] [blame] | 2612 | strbuf_addf(&sb, |
Nguyễn Thái Ngọc Duy | 218adaa | 2012-09-13 21:16:26 +0700 | [diff] [blame] | 2613 | (deletions == 1) ? ", %d deletion(-)" : ", %d deletions(-)", |
Nguyễn Thái Ngọc Duy | 7f81463 | 2012-02-01 19:55:07 +0700 | [diff] [blame] | 2614 | deletions); |
| 2615 | } |
| 2616 | strbuf_addch(&sb, '\n'); |
Stefan Beller | 0911c47 | 2017-06-29 17:07:02 -0700 | [diff] [blame] | 2617 | emit_diff_symbol(options, DIFF_SYMBOL_STATS_SUMMARY_INSERTS_DELETES, |
| 2618 | sb.buf, sb.len, 0); |
Nguyễn Thái Ngọc Duy | 7f81463 | 2012-02-01 19:55:07 +0700 | [diff] [blame] | 2619 | strbuf_release(&sb); |
Stefan Beller | 0911c47 | 2017-06-29 17:07:02 -0700 | [diff] [blame] | 2620 | } |
| 2621 | |
| 2622 | void print_stat_summary(FILE *fp, int files, |
| 2623 | int insertions, int deletions) |
| 2624 | { |
| 2625 | struct diff_options o; |
| 2626 | memset(&o, 0, sizeof(o)); |
| 2627 | o.file = fp; |
| 2628 | |
| 2629 | print_stat_summary_inserts_deletes(&o, files, insertions, deletions); |
Nguyễn Thái Ngọc Duy | 7f81463 | 2012-02-01 19:55:07 +0700 | [diff] [blame] | 2630 | } |
| 2631 | |
Felipe Contreras | 4b25d09 | 2009-05-01 12:06:36 +0300 | [diff] [blame] | 2632 | static void show_stats(struct diffstat_t *data, struct diff_options *options) |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 2633 | { |
Benjamin Kramer | eb3a9dd | 2009-03-07 21:02:10 +0100 | [diff] [blame] | 2634 | int i, len, add, del, adds = 0, dels = 0; |
Jeff King | 0974c11 | 2010-04-17 13:41:08 -0400 | [diff] [blame] | 2635 | uintmax_t max_change = 0, max_len = 0; |
Zbigniew Jędrzejewski-Szmek | dc801e7 | 2012-04-30 22:38:58 +0200 | [diff] [blame] | 2636 | int total_files = data->nr, count; |
| 2637 | int width, name_width, graph_width, number_width = 0, bin_width = 0; |
Johannes Schindelin | c0aa335 | 2011-03-22 13:50:08 +0100 | [diff] [blame] | 2638 | const char *reset, *add_c, *del_c; |
Junio C Hamano | e5f85df | 2011-05-27 21:50:39 -0700 | [diff] [blame] | 2639 | int extra_shown = 0; |
Stefan Beller | 0911c47 | 2017-06-29 17:07:02 -0700 | [diff] [blame] | 2640 | const char *line_prefix = diff_line_prefix(options); |
| 2641 | struct strbuf out = STRBUF_INIT; |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 2642 | |
| 2643 | if (data->nr == 0) |
| 2644 | return; |
| 2645 | |
Michael J Gruber | 808e1db | 2011-05-27 14:36:41 +0200 | [diff] [blame] | 2646 | count = options->stat_count ? options->stat_count : data->nr; |
Junio C Hamano | a254002 | 2006-09-26 18:53:02 -0700 | [diff] [blame] | 2647 | |
Pierre Habouzit | 8f67f8a | 2007-11-10 20:05:14 +0100 | [diff] [blame] | 2648 | reset = diff_get_color_opt(options, DIFF_RESET); |
Pierre Habouzit | 8f67f8a | 2007-11-10 20:05:14 +0100 | [diff] [blame] | 2649 | add_c = diff_get_color_opt(options, DIFF_FILE_NEW); |
| 2650 | del_c = diff_get_color_opt(options, DIFF_FILE_OLD); |
Junio C Hamano | 785f743 | 2006-09-26 18:59:41 -0700 | [diff] [blame] | 2651 | |
Zbigniew Jędrzejewski-Szmek | 1b058bc | 2012-03-01 13:26:43 +0100 | [diff] [blame] | 2652 | /* |
| 2653 | * Find the longest filename and max number of changes |
| 2654 | */ |
Michael J Gruber | 808e1db | 2011-05-27 14:36:41 +0200 | [diff] [blame] | 2655 | for (i = 0; (i < count) && (i < data->nr); i++) { |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 2656 | struct diffstat_file *file = data->files[i]; |
Jeff King | 0974c11 | 2010-04-17 13:41:08 -0400 | [diff] [blame] | 2657 | uintmax_t change = file->added + file->deleted; |
Junio C Hamano | af0ed81 | 2012-11-27 11:24:54 -0800 | [diff] [blame] | 2658 | |
| 2659 | if (!file->is_interesting && (change == 0)) { |
Michael J Gruber | 808e1db | 2011-05-27 14:36:41 +0200 | [diff] [blame] | 2660 | count++; /* not shown == room for one more */ |
Michael J Gruber | 358e460 | 2011-05-27 14:36:40 +0200 | [diff] [blame] | 2661 | continue; |
| 2662 | } |
Junio C Hamano | f604652 | 2007-12-11 23:46:30 -0800 | [diff] [blame] | 2663 | fill_print_name(file); |
| 2664 | len = strlen(file->print_name); |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 2665 | if (max_len < len) |
| 2666 | max_len = len; |
| 2667 | |
Zbigniew Jędrzejewski-Szmek | dc801e7 | 2012-04-30 22:38:58 +0200 | [diff] [blame] | 2668 | if (file->is_unmerged) { |
| 2669 | /* "Unmerged" is 8 characters */ |
| 2670 | bin_width = bin_width < 8 ? 8 : bin_width; |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 2671 | continue; |
Zbigniew Jędrzejewski-Szmek | dc801e7 | 2012-04-30 22:38:58 +0200 | [diff] [blame] | 2672 | } |
| 2673 | if (file->is_binary) { |
| 2674 | /* "Bin XXX -> YYY bytes" */ |
| 2675 | int w = 14 + decimal_width(file->added) |
| 2676 | + decimal_width(file->deleted); |
| 2677 | bin_width = bin_width < w ? w : bin_width; |
| 2678 | /* Display change counts aligned with "Bin" */ |
| 2679 | number_width = 3; |
| 2680 | continue; |
| 2681 | } |
| 2682 | |
Junio C Hamano | a254002 | 2006-09-26 18:53:02 -0700 | [diff] [blame] | 2683 | if (max_change < change) |
| 2684 | max_change = change; |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 2685 | } |
Junio C Hamano | a20d3c0 | 2012-11-27 11:47:46 -0800 | [diff] [blame] | 2686 | count = i; /* where we can stop scanning in data->files[] */ |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 2687 | |
Zbigniew Jędrzejewski-Szmek | 1b058bc | 2012-03-01 13:26:43 +0100 | [diff] [blame] | 2688 | /* |
| 2689 | * We have width = stat_width or term_columns() columns total. |
| 2690 | * We want a maximum of min(max_len, stat_name_width) for the name part. |
Zbigniew Jędrzejewski-Szmek | 969fe57 | 2012-03-01 13:26:45 +0100 | [diff] [blame] | 2691 | * We want a maximum of min(max_change, stat_graph_width) for the +- part. |
Zbigniew Jędrzejewski-Szmek | 1b058bc | 2012-03-01 13:26:43 +0100 | [diff] [blame] | 2692 | * We also need 1 for " " and 4 + decimal_width(max_change) |
| 2693 | * for " | NNNN " and one the empty column at the end, altogether |
| 2694 | * 6 + decimal_width(max_change). |
Junio C Hamano | a254002 | 2006-09-26 18:53:02 -0700 | [diff] [blame] | 2695 | * |
Zbigniew Jędrzejewski-Szmek | 1b058bc | 2012-03-01 13:26:43 +0100 | [diff] [blame] | 2696 | * If there's not enough space, we will use the smaller of |
| 2697 | * stat_name_width (if set) and 5/8*width for the filename, |
Zbigniew Jędrzejewski-Szmek | 969fe57 | 2012-03-01 13:26:45 +0100 | [diff] [blame] | 2698 | * and the rest for constant elements + graph part, but no more |
| 2699 | * than stat_graph_width for the graph part. |
Zbigniew Jędrzejewski-Szmek | 1b058bc | 2012-03-01 13:26:43 +0100 | [diff] [blame] | 2700 | * (5/8 gives 50 for filename and 30 for the constant parts + graph |
| 2701 | * for the standard terminal size). |
| 2702 | * |
| 2703 | * In other words: stat_width limits the maximum width, and |
| 2704 | * stat_name_width fixes the maximum width of the filename, |
| 2705 | * and is also used to divide available columns if there |
| 2706 | * aren't enough. |
Zbigniew Jędrzejewski-Szmek | dc801e7 | 2012-04-30 22:38:58 +0200 | [diff] [blame] | 2707 | * |
| 2708 | * Binary files are displayed with "Bin XXX -> YYY bytes" |
| 2709 | * instead of the change count and graph. This part is treated |
| 2710 | * similarly to the graph part, except that it is not |
Stefano Lattarini | 41ccfdd | 2013-04-12 00:36:10 +0200 | [diff] [blame] | 2711 | * "scaled". If total width is too small to accommodate the |
Zbigniew Jędrzejewski-Szmek | dc801e7 | 2012-04-30 22:38:58 +0200 | [diff] [blame] | 2712 | * guaranteed minimum width of the filename part and the |
| 2713 | * separators and this message, this message will "overflow" |
| 2714 | * making the line longer than the maximum width. |
Junio C Hamano | a254002 | 2006-09-26 18:53:02 -0700 | [diff] [blame] | 2715 | */ |
Junio C Hamano | a254002 | 2006-09-26 18:53:02 -0700 | [diff] [blame] | 2716 | |
Zbigniew Jędrzejewski-Szmek | 1b058bc | 2012-03-01 13:26:43 +0100 | [diff] [blame] | 2717 | if (options->stat_width == -1) |
Junio C Hamano | cd48dad | 2016-08-31 16:27:19 -0700 | [diff] [blame] | 2718 | width = term_columns() - strlen(line_prefix); |
Zbigniew Jędrzejewski-Szmek | 1b058bc | 2012-03-01 13:26:43 +0100 | [diff] [blame] | 2719 | else |
| 2720 | width = options->stat_width ? options->stat_width : 80; |
Zbigniew Jędrzejewski-Szmek | dc801e7 | 2012-04-30 22:38:58 +0200 | [diff] [blame] | 2721 | number_width = decimal_width(max_change) > number_width ? |
| 2722 | decimal_width(max_change) : number_width; |
Zbigniew Jędrzejewski-Szmek | 1b058bc | 2012-03-01 13:26:43 +0100 | [diff] [blame] | 2723 | |
Zbigniew Jędrzejewski-Szmek | df44483 | 2012-03-01 13:26:46 +0100 | [diff] [blame] | 2724 | if (options->stat_graph_width == -1) |
| 2725 | options->stat_graph_width = diff_stat_graph_width; |
| 2726 | |
Zbigniew Jędrzejewski-Szmek | 1b058bc | 2012-03-01 13:26:43 +0100 | [diff] [blame] | 2727 | /* |
| 2728 | * Guarantee 3/8*16==6 for the graph part |
| 2729 | * and 5/8*16==10 for the filename part |
| 2730 | */ |
| 2731 | if (width < 16 + 6 + number_width) |
| 2732 | width = 16 + 6 + number_width; |
| 2733 | |
| 2734 | /* |
| 2735 | * First assign sizes that are wanted, ignoring available width. |
Zbigniew Jędrzejewski-Szmek | dc801e7 | 2012-04-30 22:38:58 +0200 | [diff] [blame] | 2736 | * strlen("Bin XXX -> YYY bytes") == bin_width, and the part |
| 2737 | * starting from "XXX" should fit in graph_width. |
Zbigniew Jędrzejewski-Szmek | 1b058bc | 2012-03-01 13:26:43 +0100 | [diff] [blame] | 2738 | */ |
Zbigniew Jędrzejewski-Szmek | dc801e7 | 2012-04-30 22:38:58 +0200 | [diff] [blame] | 2739 | graph_width = max_change + 4 > bin_width ? max_change : bin_width - 4; |
| 2740 | if (options->stat_graph_width && |
| 2741 | options->stat_graph_width < graph_width) |
| 2742 | graph_width = options->stat_graph_width; |
| 2743 | |
Zbigniew Jędrzejewski-Szmek | 1b058bc | 2012-03-01 13:26:43 +0100 | [diff] [blame] | 2744 | name_width = (options->stat_name_width > 0 && |
| 2745 | options->stat_name_width < max_len) ? |
| 2746 | options->stat_name_width : max_len; |
| 2747 | |
| 2748 | /* |
| 2749 | * Adjust adjustable widths not to exceed maximum width |
| 2750 | */ |
| 2751 | if (name_width + number_width + 6 + graph_width > width) { |
Lucian Poston | 678c574 | 2012-04-18 14:12:18 -0700 | [diff] [blame] | 2752 | if (graph_width > width * 3/8 - number_width - 6) { |
Zbigniew Jędrzejewski-Szmek | 1b058bc | 2012-03-01 13:26:43 +0100 | [diff] [blame] | 2753 | graph_width = width * 3/8 - number_width - 6; |
Lucian Poston | 678c574 | 2012-04-18 14:12:18 -0700 | [diff] [blame] | 2754 | if (graph_width < 6) |
| 2755 | graph_width = 6; |
| 2756 | } |
| 2757 | |
Zbigniew Jędrzejewski-Szmek | 969fe57 | 2012-03-01 13:26:45 +0100 | [diff] [blame] | 2758 | if (options->stat_graph_width && |
| 2759 | graph_width > options->stat_graph_width) |
| 2760 | graph_width = options->stat_graph_width; |
Zbigniew Jędrzejewski-Szmek | 1b058bc | 2012-03-01 13:26:43 +0100 | [diff] [blame] | 2761 | if (name_width > width - number_width - 6 - graph_width) |
| 2762 | name_width = width - number_width - 6 - graph_width; |
| 2763 | else |
| 2764 | graph_width = width - number_width - 6 - name_width; |
| 2765 | } |
| 2766 | |
| 2767 | /* |
| 2768 | * From here name_width is the width of the name area, |
| 2769 | * and graph_width is the width of the graph area. |
| 2770 | * max_change is used to scale graph properly. |
| 2771 | */ |
Michael J Gruber | 808e1db | 2011-05-27 14:36:41 +0200 | [diff] [blame] | 2772 | for (i = 0; i < count; i++) { |
Timo Hirvonen | d2543b8 | 2006-06-24 20:20:32 +0300 | [diff] [blame] | 2773 | const char *prefix = ""; |
Junio C Hamano | af0ed81 | 2012-11-27 11:24:54 -0800 | [diff] [blame] | 2774 | struct diffstat_file *file = data->files[i]; |
| 2775 | char *name = file->print_name; |
| 2776 | uintmax_t added = file->added; |
| 2777 | uintmax_t deleted = file->deleted; |
Junio C Hamano | a254002 | 2006-09-26 18:53:02 -0700 | [diff] [blame] | 2778 | int name_len; |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 2779 | |
Junio C Hamano | a20d3c0 | 2012-11-27 11:47:46 -0800 | [diff] [blame] | 2780 | if (!file->is_interesting && (added + deleted == 0)) |
Michael J Gruber | 358e460 | 2011-05-27 14:36:40 +0200 | [diff] [blame] | 2781 | continue; |
Junio C Hamano | a20d3c0 | 2012-11-27 11:47:46 -0800 | [diff] [blame] | 2782 | |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 2783 | /* |
| 2784 | * "scale" the filename |
| 2785 | */ |
Junio C Hamano | a254002 | 2006-09-26 18:53:02 -0700 | [diff] [blame] | 2786 | len = name_width; |
| 2787 | name_len = strlen(name); |
| 2788 | if (name_width < name_len) { |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 2789 | char *slash; |
| 2790 | prefix = "..."; |
Junio C Hamano | a254002 | 2006-09-26 18:53:02 -0700 | [diff] [blame] | 2791 | len -= 3; |
| 2792 | name += name_len - len; |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 2793 | slash = strchr(name, '/'); |
| 2794 | if (slash) |
| 2795 | name = slash; |
| 2796 | } |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 2797 | |
Junio C Hamano | af0ed81 | 2012-11-27 11:24:54 -0800 | [diff] [blame] | 2798 | if (file->is_binary) { |
Stefan Beller | 0911c47 | 2017-06-29 17:07:02 -0700 | [diff] [blame] | 2799 | strbuf_addf(&out, " %s%-*s |", prefix, len, name); |
| 2800 | strbuf_addf(&out, " %*s", number_width, "Bin"); |
Zbigniew Jędrzejewski-Szmek | e18872b | 2012-05-01 19:10:14 +0200 | [diff] [blame] | 2801 | if (!added && !deleted) { |
Stefan Beller | 0911c47 | 2017-06-29 17:07:02 -0700 | [diff] [blame] | 2802 | strbuf_addch(&out, '\n'); |
| 2803 | emit_diff_symbol(options, DIFF_SYMBOL_STATS_LINE, |
| 2804 | out.buf, out.len, 0); |
| 2805 | strbuf_reset(&out); |
Zbigniew Jędrzejewski-Szmek | e18872b | 2012-05-01 19:10:14 +0200 | [diff] [blame] | 2806 | continue; |
| 2807 | } |
Stefan Beller | 0911c47 | 2017-06-29 17:07:02 -0700 | [diff] [blame] | 2808 | strbuf_addf(&out, " %s%"PRIuMAX"%s", |
Jeff King | 0974c11 | 2010-04-17 13:41:08 -0400 | [diff] [blame] | 2809 | del_c, deleted, reset); |
Stefan Beller | 0911c47 | 2017-06-29 17:07:02 -0700 | [diff] [blame] | 2810 | strbuf_addstr(&out, " -> "); |
| 2811 | strbuf_addf(&out, "%s%"PRIuMAX"%s", |
Jeff King | 0974c11 | 2010-04-17 13:41:08 -0400 | [diff] [blame] | 2812 | add_c, added, reset); |
Stefan Beller | 0911c47 | 2017-06-29 17:07:02 -0700 | [diff] [blame] | 2813 | strbuf_addstr(&out, " bytes\n"); |
| 2814 | emit_diff_symbol(options, DIFF_SYMBOL_STATS_LINE, |
| 2815 | out.buf, out.len, 0); |
| 2816 | strbuf_reset(&out); |
Junio C Hamano | f604652 | 2007-12-11 23:46:30 -0800 | [diff] [blame] | 2817 | continue; |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 2818 | } |
Junio C Hamano | af0ed81 | 2012-11-27 11:24:54 -0800 | [diff] [blame] | 2819 | else if (file->is_unmerged) { |
Stefan Beller | 0911c47 | 2017-06-29 17:07:02 -0700 | [diff] [blame] | 2820 | strbuf_addf(&out, " %s%-*s |", prefix, len, name); |
| 2821 | strbuf_addstr(&out, " Unmerged\n"); |
| 2822 | emit_diff_symbol(options, DIFF_SYMBOL_STATS_LINE, |
| 2823 | out.buf, out.len, 0); |
| 2824 | strbuf_reset(&out); |
Junio C Hamano | f604652 | 2007-12-11 23:46:30 -0800 | [diff] [blame] | 2825 | continue; |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 2826 | } |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 2827 | |
Junio C Hamano | a254002 | 2006-09-26 18:53:02 -0700 | [diff] [blame] | 2828 | /* |
| 2829 | * scale the add/delete |
| 2830 | */ |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 2831 | add = added; |
| 2832 | del = deleted; |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 2833 | |
Zbigniew Jędrzejewski-Szmek | 1b058bc | 2012-03-01 13:26:43 +0100 | [diff] [blame] | 2834 | if (graph_width <= max_change) { |
Stefan Beller | d3c9cf3 | 2013-07-14 23:35:49 +0200 | [diff] [blame] | 2835 | int total = scale_linear(add + del, graph_width, max_change); |
Junio C Hamano | 2eeeef2 | 2012-02-14 13:49:11 -0800 | [diff] [blame] | 2836 | if (total < 2 && add && del) |
| 2837 | /* width >= 2 due to the sanity check */ |
| 2838 | total = 2; |
| 2839 | if (add < del) { |
Zbigniew Jędrzejewski-Szmek | 1b058bc | 2012-03-01 13:26:43 +0100 | [diff] [blame] | 2840 | add = scale_linear(add, graph_width, max_change); |
Junio C Hamano | 2eeeef2 | 2012-02-14 13:49:11 -0800 | [diff] [blame] | 2841 | del = total - add; |
| 2842 | } else { |
Zbigniew Jędrzejewski-Szmek | 1b058bc | 2012-03-01 13:26:43 +0100 | [diff] [blame] | 2843 | del = scale_linear(del, graph_width, max_change); |
Junio C Hamano | 2eeeef2 | 2012-02-14 13:49:11 -0800 | [diff] [blame] | 2844 | add = total - del; |
| 2845 | } |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 2846 | } |
Stefan Beller | 0911c47 | 2017-06-29 17:07:02 -0700 | [diff] [blame] | 2847 | strbuf_addf(&out, " %s%-*s |", prefix, len, name); |
| 2848 | strbuf_addf(&out, " %*"PRIuMAX"%s", |
Zbigniew Jędrzejewski-Szmek | dc801e7 | 2012-04-30 22:38:58 +0200 | [diff] [blame] | 2849 | number_width, added + deleted, |
| 2850 | added + deleted ? " " : ""); |
Stefan Beller | 0911c47 | 2017-06-29 17:07:02 -0700 | [diff] [blame] | 2851 | show_graph(&out, '+', add, add_c, reset); |
| 2852 | show_graph(&out, '-', del, del_c, reset); |
| 2853 | strbuf_addch(&out, '\n'); |
| 2854 | emit_diff_symbol(options, DIFF_SYMBOL_STATS_LINE, |
| 2855 | out.buf, out.len, 0); |
| 2856 | strbuf_reset(&out); |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 2857 | } |
Junio C Hamano | a20d3c0 | 2012-11-27 11:47:46 -0800 | [diff] [blame] | 2858 | |
| 2859 | for (i = 0; i < data->nr; i++) { |
Junio C Hamano | af0ed81 | 2012-11-27 11:24:54 -0800 | [diff] [blame] | 2860 | struct diffstat_file *file = data->files[i]; |
| 2861 | uintmax_t added = file->added; |
| 2862 | uintmax_t deleted = file->deleted; |
Junio C Hamano | 82dfc2c | 2012-11-27 12:05:10 -0800 | [diff] [blame] | 2863 | |
| 2864 | if (file->is_unmerged || |
| 2865 | (!file->is_interesting && (added + deleted == 0))) { |
Michael J Gruber | 808e1db | 2011-05-27 14:36:41 +0200 | [diff] [blame] | 2866 | total_files--; |
| 2867 | continue; |
| 2868 | } |
Junio C Hamano | a20d3c0 | 2012-11-27 11:47:46 -0800 | [diff] [blame] | 2869 | |
Junio C Hamano | 82dfc2c | 2012-11-27 12:05:10 -0800 | [diff] [blame] | 2870 | if (!file->is_binary) { |
Junio C Hamano | a20d3c0 | 2012-11-27 11:47:46 -0800 | [diff] [blame] | 2871 | adds += added; |
| 2872 | dels += deleted; |
| 2873 | } |
| 2874 | if (i < count) |
| 2875 | continue; |
Junio C Hamano | e5f85df | 2011-05-27 21:50:39 -0700 | [diff] [blame] | 2876 | if (!extra_shown) |
Stefan Beller | 0911c47 | 2017-06-29 17:07:02 -0700 | [diff] [blame] | 2877 | emit_diff_symbol(options, |
| 2878 | DIFF_SYMBOL_STATS_SUMMARY_ABBREV, |
| 2879 | NULL, 0, 0); |
Junio C Hamano | e5f85df | 2011-05-27 21:50:39 -0700 | [diff] [blame] | 2880 | extra_shown = 1; |
Michael J Gruber | 808e1db | 2011-05-27 14:36:41 +0200 | [diff] [blame] | 2881 | } |
Stefan Beller | 0911c47 | 2017-06-29 17:07:02 -0700 | [diff] [blame] | 2882 | |
| 2883 | print_stat_summary_inserts_deletes(options, total_files, adds, dels); |
Rene Scharfe | 5a61201 | 2017-08-30 19:49:43 +0200 | [diff] [blame] | 2884 | strbuf_release(&out); |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 2885 | } |
| 2886 | |
Felipe Contreras | 2775d92 | 2009-10-11 23:46:11 +0300 | [diff] [blame] | 2887 | static void show_shortstats(struct diffstat_t *data, struct diff_options *options) |
Nicolas Pitre | ebd124c | 2006-12-14 23:15:44 -0500 | [diff] [blame] | 2888 | { |
| 2889 | int i, adds = 0, dels = 0, total_files = data->nr; |
| 2890 | |
| 2891 | if (data->nr == 0) |
| 2892 | return; |
| 2893 | |
| 2894 | for (i = 0; i < data->nr; i++) { |
Zbigniew Jędrzejewski-Szmek | e18872b | 2012-05-01 19:10:14 +0200 | [diff] [blame] | 2895 | int added = data->files[i]->added; |
Stefan Beller | 0911c47 | 2017-06-29 17:07:02 -0700 | [diff] [blame] | 2896 | int deleted = data->files[i]->deleted; |
Zbigniew Jędrzejewski-Szmek | e18872b | 2012-05-01 19:10:14 +0200 | [diff] [blame] | 2897 | |
Junio C Hamano | 20c8cde | 2012-11-27 14:19:36 -0800 | [diff] [blame] | 2898 | if (data->files[i]->is_unmerged || |
| 2899 | (!data->files[i]->is_interesting && (added + deleted == 0))) { |
Zbigniew Jędrzejewski-Szmek | e18872b | 2012-05-01 19:10:14 +0200 | [diff] [blame] | 2900 | total_files--; |
Alexander Strasser | de9658b | 2012-06-15 23:50:30 +0200 | [diff] [blame] | 2901 | } else if (!data->files[i]->is_binary) { /* don't count bytes */ |
Zbigniew Jędrzejewski-Szmek | e18872b | 2012-05-01 19:10:14 +0200 | [diff] [blame] | 2902 | adds += added; |
| 2903 | dels += deleted; |
Nicolas Pitre | ebd124c | 2006-12-14 23:15:44 -0500 | [diff] [blame] | 2904 | } |
Nicolas Pitre | ebd124c | 2006-12-14 23:15:44 -0500 | [diff] [blame] | 2905 | } |
Stefan Beller | 0911c47 | 2017-06-29 17:07:02 -0700 | [diff] [blame] | 2906 | print_stat_summary_inserts_deletes(options, total_files, adds, dels); |
Nicolas Pitre | ebd124c | 2006-12-14 23:15:44 -0500 | [diff] [blame] | 2907 | } |
| 2908 | |
Felipe Contreras | 4b25d09 | 2009-05-01 12:06:36 +0300 | [diff] [blame] | 2909 | static void show_numstat(struct diffstat_t *data, struct diff_options *options) |
Junio C Hamano | 74e2abe | 2006-10-12 03:01:00 -0700 | [diff] [blame] | 2910 | { |
| 2911 | int i; |
| 2912 | |
Junio C Hamano | f604652 | 2007-12-11 23:46:30 -0800 | [diff] [blame] | 2913 | if (data->nr == 0) |
| 2914 | return; |
| 2915 | |
Junio C Hamano | 74e2abe | 2006-10-12 03:01:00 -0700 | [diff] [blame] | 2916 | for (i = 0; i < data->nr; i++) { |
| 2917 | struct diffstat_file *file = data->files[i]; |
| 2918 | |
John Keeping | 30997bb | 2013-02-07 20:15:27 +0000 | [diff] [blame] | 2919 | fprintf(options->file, "%s", diff_line_prefix(options)); |
Bo Yang | 7be5761 | 2010-05-26 15:23:54 +0800 | [diff] [blame] | 2920 | |
Junio C Hamano | bfddbc5 | 2006-12-10 13:50:59 -0800 | [diff] [blame] | 2921 | if (file->is_binary) |
Daniel Barkalow | c0c7773 | 2008-03-09 22:43:39 -0400 | [diff] [blame] | 2922 | fprintf(options->file, "-\t-\t"); |
Junio C Hamano | bfddbc5 | 2006-12-10 13:50:59 -0800 | [diff] [blame] | 2923 | else |
Daniel Barkalow | c0c7773 | 2008-03-09 22:43:39 -0400 | [diff] [blame] | 2924 | fprintf(options->file, |
Jeff King | 0974c11 | 2010-04-17 13:41:08 -0400 | [diff] [blame] | 2925 | "%"PRIuMAX"\t%"PRIuMAX"\t", |
| 2926 | file->added, file->deleted); |
Junio C Hamano | f604652 | 2007-12-11 23:46:30 -0800 | [diff] [blame] | 2927 | if (options->line_termination) { |
| 2928 | fill_print_name(file); |
| 2929 | if (!file->is_renamed) |
Daniel Barkalow | c0c7773 | 2008-03-09 22:43:39 -0400 | [diff] [blame] | 2930 | write_name_quoted(file->name, options->file, |
Junio C Hamano | f604652 | 2007-12-11 23:46:30 -0800 | [diff] [blame] | 2931 | options->line_termination); |
| 2932 | else { |
Daniel Barkalow | c0c7773 | 2008-03-09 22:43:39 -0400 | [diff] [blame] | 2933 | fputs(file->print_name, options->file); |
| 2934 | putc(options->line_termination, options->file); |
Junio C Hamano | f604652 | 2007-12-11 23:46:30 -0800 | [diff] [blame] | 2935 | } |
Pierre Habouzit | 663af34 | 2007-09-20 00:42:15 +0200 | [diff] [blame] | 2936 | } else { |
Junio C Hamano | f604652 | 2007-12-11 23:46:30 -0800 | [diff] [blame] | 2937 | if (file->is_renamed) { |
Daniel Barkalow | c0c7773 | 2008-03-09 22:43:39 -0400 | [diff] [blame] | 2938 | putc('\0', options->file); |
| 2939 | write_name_quoted(file->from_name, options->file, '\0'); |
Junio C Hamano | f604652 | 2007-12-11 23:46:30 -0800 | [diff] [blame] | 2940 | } |
Daniel Barkalow | c0c7773 | 2008-03-09 22:43:39 -0400 | [diff] [blame] | 2941 | write_name_quoted(file->name, options->file, '\0'); |
Pierre Habouzit | 663af34 | 2007-09-20 00:42:15 +0200 | [diff] [blame] | 2942 | } |
Junio C Hamano | 74e2abe | 2006-10-12 03:01:00 -0700 | [diff] [blame] | 2943 | } |
| 2944 | } |
| 2945 | |
Junio C Hamano | c04a715 | 2008-02-12 17:06:58 -0800 | [diff] [blame] | 2946 | struct dirstat_file { |
| 2947 | const char *name; |
| 2948 | unsigned long changed; |
Linus Torvalds | 7df7c01 | 2008-02-12 13:26:31 -0800 | [diff] [blame] | 2949 | }; |
| 2950 | |
Junio C Hamano | c04a715 | 2008-02-12 17:06:58 -0800 | [diff] [blame] | 2951 | struct dirstat_dir { |
| 2952 | struct dirstat_file *files; |
Johan Herland | 712d2c7 | 2011-04-29 11:36:20 +0200 | [diff] [blame] | 2953 | int alloc, nr, permille, cumulative; |
Junio C Hamano | c04a715 | 2008-02-12 17:06:58 -0800 | [diff] [blame] | 2954 | }; |
| 2955 | |
Bo Yang | 7be5761 | 2010-05-26 15:23:54 +0800 | [diff] [blame] | 2956 | static long gather_dirstat(struct diff_options *opt, struct dirstat_dir *dir, |
| 2957 | unsigned long changed, const char *base, int baselen) |
Linus Torvalds | 7df7c01 | 2008-02-12 13:26:31 -0800 | [diff] [blame] | 2958 | { |
Brandon Williams | 585c0e2 | 2018-02-14 10:59:28 -0800 | [diff] [blame] | 2959 | unsigned long sum_changes = 0; |
Linus Torvalds | 7df7c01 | 2008-02-12 13:26:31 -0800 | [diff] [blame] | 2960 | unsigned int sources = 0; |
John Keeping | 30997bb | 2013-02-07 20:15:27 +0000 | [diff] [blame] | 2961 | const char *line_prefix = diff_line_prefix(opt); |
Linus Torvalds | 7df7c01 | 2008-02-12 13:26:31 -0800 | [diff] [blame] | 2962 | |
| 2963 | while (dir->nr) { |
Junio C Hamano | c04a715 | 2008-02-12 17:06:58 -0800 | [diff] [blame] | 2964 | struct dirstat_file *f = dir->files; |
Linus Torvalds | 7df7c01 | 2008-02-12 13:26:31 -0800 | [diff] [blame] | 2965 | int namelen = strlen(f->name); |
Brandon Williams | 585c0e2 | 2018-02-14 10:59:28 -0800 | [diff] [blame] | 2966 | unsigned long changes; |
Linus Torvalds | 7df7c01 | 2008-02-12 13:26:31 -0800 | [diff] [blame] | 2967 | char *slash; |
| 2968 | |
| 2969 | if (namelen < baselen) |
| 2970 | break; |
| 2971 | if (memcmp(f->name, base, baselen)) |
| 2972 | break; |
| 2973 | slash = strchr(f->name + baselen, '/'); |
| 2974 | if (slash) { |
| 2975 | int newbaselen = slash + 1 - f->name; |
Brandon Williams | 585c0e2 | 2018-02-14 10:59:28 -0800 | [diff] [blame] | 2976 | changes = gather_dirstat(opt, dir, changed, f->name, newbaselen); |
Linus Torvalds | 7df7c01 | 2008-02-12 13:26:31 -0800 | [diff] [blame] | 2977 | sources++; |
| 2978 | } else { |
Brandon Williams | 585c0e2 | 2018-02-14 10:59:28 -0800 | [diff] [blame] | 2979 | changes = f->changed; |
Linus Torvalds | 7df7c01 | 2008-02-12 13:26:31 -0800 | [diff] [blame] | 2980 | dir->files++; |
| 2981 | dir->nr--; |
| 2982 | sources += 2; |
| 2983 | } |
Brandon Williams | 585c0e2 | 2018-02-14 10:59:28 -0800 | [diff] [blame] | 2984 | sum_changes += changes; |
Linus Torvalds | 7df7c01 | 2008-02-12 13:26:31 -0800 | [diff] [blame] | 2985 | } |
| 2986 | |
| 2987 | /* |
| 2988 | * We don't report dirstat's for |
| 2989 | * - the top level |
| 2990 | * - or cases where everything came from a single directory |
| 2991 | * under this directory (sources == 1). |
| 2992 | */ |
| 2993 | if (baselen && sources != 1) { |
Brandon Williams | 585c0e2 | 2018-02-14 10:59:28 -0800 | [diff] [blame] | 2994 | if (sum_changes) { |
| 2995 | int permille = sum_changes * 1000 / changed; |
Johan Herland | 712d2c7 | 2011-04-29 11:36:20 +0200 | [diff] [blame] | 2996 | if (permille >= dir->permille) { |
Bo Yang | 7be5761 | 2010-05-26 15:23:54 +0800 | [diff] [blame] | 2997 | fprintf(opt->file, "%s%4d.%01d%% %.*s\n", line_prefix, |
Johan Herland | 712d2c7 | 2011-04-29 11:36:20 +0200 | [diff] [blame] | 2998 | permille / 10, permille % 10, baselen, base); |
Linus Torvalds | 7df7c01 | 2008-02-12 13:26:31 -0800 | [diff] [blame] | 2999 | if (!dir->cumulative) |
| 3000 | return 0; |
| 3001 | } |
| 3002 | } |
| 3003 | } |
Brandon Williams | 585c0e2 | 2018-02-14 10:59:28 -0800 | [diff] [blame] | 3004 | return sum_changes; |
Linus Torvalds | 7df7c01 | 2008-02-12 13:26:31 -0800 | [diff] [blame] | 3005 | } |
| 3006 | |
Linus Torvalds | 441bca0 | 2008-08-28 16:19:08 -0700 | [diff] [blame] | 3007 | static int dirstat_compare(const void *_a, const void *_b) |
| 3008 | { |
| 3009 | const struct dirstat_file *a = _a; |
| 3010 | const struct dirstat_file *b = _b; |
| 3011 | return strcmp(a->name, b->name); |
| 3012 | } |
| 3013 | |
Junio C Hamano | c04a715 | 2008-02-12 17:06:58 -0800 | [diff] [blame] | 3014 | static void show_dirstat(struct diff_options *options) |
Linus Torvalds | 7df7c01 | 2008-02-12 13:26:31 -0800 | [diff] [blame] | 3015 | { |
| 3016 | int i; |
| 3017 | unsigned long changed; |
Junio C Hamano | c04a715 | 2008-02-12 17:06:58 -0800 | [diff] [blame] | 3018 | struct dirstat_dir dir; |
| 3019 | struct diff_queue_struct *q = &diff_queued_diff; |
Linus Torvalds | 7df7c01 | 2008-02-12 13:26:31 -0800 | [diff] [blame] | 3020 | |
Junio C Hamano | c04a715 | 2008-02-12 17:06:58 -0800 | [diff] [blame] | 3021 | dir.files = NULL; |
| 3022 | dir.alloc = 0; |
| 3023 | dir.nr = 0; |
Johan Herland | 712d2c7 | 2011-04-29 11:36:20 +0200 | [diff] [blame] | 3024 | dir.permille = options->dirstat_permille; |
Brandon Williams | 0d1e0e7 | 2017-10-31 11:19:11 -0700 | [diff] [blame] | 3025 | dir.cumulative = options->flags.dirstat_cumulative; |
Junio C Hamano | c04a715 | 2008-02-12 17:06:58 -0800 | [diff] [blame] | 3026 | |
Linus Torvalds | 7df7c01 | 2008-02-12 13:26:31 -0800 | [diff] [blame] | 3027 | changed = 0; |
Junio C Hamano | c04a715 | 2008-02-12 17:06:58 -0800 | [diff] [blame] | 3028 | for (i = 0; i < q->nr; i++) { |
| 3029 | struct diff_filepair *p = q->queue[i]; |
| 3030 | const char *name; |
| 3031 | unsigned long copied, added, damage; |
Jonathan Tan | 1c37e86 | 2020-04-07 15:11:41 -0700 | [diff] [blame] | 3032 | struct diff_populate_filespec_options dpf_options = { |
| 3033 | .check_size_only = 1, |
| 3034 | }; |
Junio C Hamano | c04a715 | 2008-02-12 17:06:58 -0800 | [diff] [blame] | 3035 | |
Johan Herland | 2ca8671 | 2011-04-12 11:24:34 +0200 | [diff] [blame] | 3036 | name = p->two->path ? p->two->path : p->one->path; |
Junio C Hamano | c04a715 | 2008-02-12 17:06:58 -0800 | [diff] [blame] | 3037 | |
Jeff King | d9f62df | 2018-08-28 17:23:03 -0400 | [diff] [blame] | 3038 | if (p->one->oid_valid && p->two->oid_valid && |
| 3039 | oideq(&p->one->oid, &p->two->oid)) { |
Johan Herland | 2ff3a80 | 2011-04-11 00:48:52 +0200 | [diff] [blame] | 3040 | /* |
| 3041 | * The SHA1 has not changed, so pre-/post-content is |
| 3042 | * identical. We can therefore skip looking at the |
| 3043 | * file contents altogether. |
| 3044 | */ |
| 3045 | damage = 0; |
| 3046 | goto found_damage; |
| 3047 | } |
| 3048 | |
Brandon Williams | 0d1e0e7 | 2017-10-31 11:19:11 -0700 | [diff] [blame] | 3049 | if (options->flags.dirstat_by_file) { |
Johan Herland | 0133dab | 2011-04-11 00:48:51 +0200 | [diff] [blame] | 3050 | /* |
| 3051 | * In --dirstat-by-file mode, we don't really need to |
| 3052 | * look at the actual file contents at all. |
| 3053 | * The fact that the SHA1 changed is enough for us to |
| 3054 | * add this file to the list of results |
| 3055 | * (with each file contributing equal damage). |
| 3056 | */ |
Johan Herland | 2ff3a80 | 2011-04-11 00:48:52 +0200 | [diff] [blame] | 3057 | damage = 1; |
Johan Herland | 0133dab | 2011-04-11 00:48:51 +0200 | [diff] [blame] | 3058 | goto found_damage; |
| 3059 | } |
Junio C Hamano | c04a715 | 2008-02-12 17:06:58 -0800 | [diff] [blame] | 3060 | |
| 3061 | if (DIFF_FILE_VALID(p->one) && DIFF_FILE_VALID(p->two)) { |
Jonathan Tan | 1c37e86 | 2020-04-07 15:11:41 -0700 | [diff] [blame] | 3062 | diff_populate_filespec(options->repo, p->one, NULL); |
| 3063 | diff_populate_filespec(options->repo, p->two, NULL); |
Nguyễn Thái Ngọc Duy | b78ea5f | 2018-09-21 17:57:19 +0200 | [diff] [blame] | 3064 | diffcore_count_changes(options->repo, |
| 3065 | p->one, p->two, NULL, NULL, |
Junio C Hamano | c04a715 | 2008-02-12 17:06:58 -0800 | [diff] [blame] | 3066 | &copied, &added); |
| 3067 | diff_free_filespec_data(p->one); |
| 3068 | diff_free_filespec_data(p->two); |
| 3069 | } else if (DIFF_FILE_VALID(p->one)) { |
Jonathan Tan | 1c37e86 | 2020-04-07 15:11:41 -0700 | [diff] [blame] | 3070 | diff_populate_filespec(options->repo, p->one, &dpf_options); |
Junio C Hamano | c04a715 | 2008-02-12 17:06:58 -0800 | [diff] [blame] | 3071 | copied = added = 0; |
| 3072 | diff_free_filespec_data(p->one); |
| 3073 | } else if (DIFF_FILE_VALID(p->two)) { |
Jonathan Tan | 1c37e86 | 2020-04-07 15:11:41 -0700 | [diff] [blame] | 3074 | diff_populate_filespec(options->repo, p->two, &dpf_options); |
Junio C Hamano | c04a715 | 2008-02-12 17:06:58 -0800 | [diff] [blame] | 3075 | copied = 0; |
| 3076 | added = p->two->size; |
| 3077 | diff_free_filespec_data(p->two); |
| 3078 | } else |
Junio C Hamano | 2b0b551 | 2008-02-24 17:37:15 -0800 | [diff] [blame] | 3079 | continue; |
Junio C Hamano | c04a715 | 2008-02-12 17:06:58 -0800 | [diff] [blame] | 3080 | |
| 3081 | /* |
| 3082 | * Original minus copied is the removed material, |
| 3083 | * added is the new material. They are both damages |
Johan Herland | 0133dab | 2011-04-11 00:48:51 +0200 | [diff] [blame] | 3084 | * made to the preimage. |
Johan Herland | 2ff3a80 | 2011-04-11 00:48:52 +0200 | [diff] [blame] | 3085 | * If the resulting damage is zero, we know that |
| 3086 | * diffcore_count_changes() considers the two entries to |
Jeff King | d9f62df | 2018-08-28 17:23:03 -0400 | [diff] [blame] | 3087 | * be identical, but since the oid changed, we |
Johan Herland | 2ff3a80 | 2011-04-11 00:48:52 +0200 | [diff] [blame] | 3088 | * know that there must have been _some_ kind of change, |
| 3089 | * so we force all entries to have damage > 0. |
Junio C Hamano | c04a715 | 2008-02-12 17:06:58 -0800 | [diff] [blame] | 3090 | */ |
| 3091 | damage = (p->one->size - copied) + added; |
Johan Herland | 2ff3a80 | 2011-04-11 00:48:52 +0200 | [diff] [blame] | 3092 | if (!damage) |
Heikki Orsila | fd33777 | 2008-09-05 22:27:35 +0300 | [diff] [blame] | 3093 | damage = 1; |
Junio C Hamano | c04a715 | 2008-02-12 17:06:58 -0800 | [diff] [blame] | 3094 | |
Johan Herland | 0133dab | 2011-04-11 00:48:51 +0200 | [diff] [blame] | 3095 | found_damage: |
Junio C Hamano | c04a715 | 2008-02-12 17:06:58 -0800 | [diff] [blame] | 3096 | ALLOC_GROW(dir.files, dir.nr + 1, dir.alloc); |
| 3097 | dir.files[dir.nr].name = name; |
| 3098 | dir.files[dir.nr].changed = damage; |
| 3099 | changed += damage; |
| 3100 | dir.nr++; |
Linus Torvalds | 7df7c01 | 2008-02-12 13:26:31 -0800 | [diff] [blame] | 3101 | } |
| 3102 | |
| 3103 | /* This can happen even with many files, if everything was renames */ |
| 3104 | if (!changed) |
| 3105 | return; |
| 3106 | |
| 3107 | /* Show all directories with more than x% of the changes */ |
René Scharfe | 9ed0d8d | 2016-09-29 17:27:31 +0200 | [diff] [blame] | 3108 | QSORT(dir.files, dir.nr, dirstat_compare); |
Bo Yang | 7be5761 | 2010-05-26 15:23:54 +0800 | [diff] [blame] | 3109 | gather_dirstat(options, &dir, changed, "", 0); |
Linus Torvalds | 7df7c01 | 2008-02-12 13:26:31 -0800 | [diff] [blame] | 3110 | } |
| 3111 | |
Johan Herland | 1c57a62 | 2011-04-29 11:36:21 +0200 | [diff] [blame] | 3112 | static void show_dirstat_by_line(struct diffstat_t *data, struct diff_options *options) |
| 3113 | { |
| 3114 | int i; |
| 3115 | unsigned long changed; |
| 3116 | struct dirstat_dir dir; |
| 3117 | |
| 3118 | if (data->nr == 0) |
| 3119 | return; |
| 3120 | |
| 3121 | dir.files = NULL; |
| 3122 | dir.alloc = 0; |
| 3123 | dir.nr = 0; |
| 3124 | dir.permille = options->dirstat_permille; |
Brandon Williams | 0d1e0e7 | 2017-10-31 11:19:11 -0700 | [diff] [blame] | 3125 | dir.cumulative = options->flags.dirstat_cumulative; |
Johan Herland | 1c57a62 | 2011-04-29 11:36:21 +0200 | [diff] [blame] | 3126 | |
| 3127 | changed = 0; |
| 3128 | for (i = 0; i < data->nr; i++) { |
| 3129 | struct diffstat_file *file = data->files[i]; |
| 3130 | unsigned long damage = file->added + file->deleted; |
| 3131 | if (file->is_binary) |
| 3132 | /* |
| 3133 | * binary files counts bytes, not lines. Must find some |
| 3134 | * way to normalize binary bytes vs. textual lines. |
| 3135 | * The following heuristic assumes that there are 64 |
| 3136 | * bytes per "line". |
| 3137 | * This is stupid and ugly, but very cheap... |
| 3138 | */ |
René Scharfe | 42c78a2 | 2017-07-08 12:35:35 +0200 | [diff] [blame] | 3139 | damage = DIV_ROUND_UP(damage, 64); |
Johan Herland | 1c57a62 | 2011-04-29 11:36:21 +0200 | [diff] [blame] | 3140 | ALLOC_GROW(dir.files, dir.nr + 1, dir.alloc); |
| 3141 | dir.files[dir.nr].name = file->name; |
| 3142 | dir.files[dir.nr].changed = damage; |
| 3143 | changed += damage; |
| 3144 | dir.nr++; |
| 3145 | } |
| 3146 | |
| 3147 | /* This can happen even with many files, if everything was renames */ |
| 3148 | if (!changed) |
| 3149 | return; |
| 3150 | |
| 3151 | /* Show all directories with more than x% of the changes */ |
René Scharfe | 9ed0d8d | 2016-09-29 17:27:31 +0200 | [diff] [blame] | 3152 | QSORT(dir.files, dir.nr, dirstat_compare); |
Johan Herland | 1c57a62 | 2011-04-29 11:36:21 +0200 | [diff] [blame] | 3153 | gather_dirstat(options, &dir, changed, "", 0); |
| 3154 | } |
| 3155 | |
Matthew Rogers | 1cf3d5d | 2020-08-20 00:41:32 +0000 | [diff] [blame] | 3156 | static void free_diffstat_file(struct diffstat_file *f) |
| 3157 | { |
| 3158 | free(f->print_name); |
| 3159 | free(f->name); |
| 3160 | free(f->from_name); |
| 3161 | free(f); |
| 3162 | } |
| 3163 | |
Daniel Ferreira | e4cb659 | 2019-11-13 12:40:58 +0000 | [diff] [blame] | 3164 | void free_diffstat_info(struct diffstat_t *diffstat) |
Junio C Hamano | f604652 | 2007-12-11 23:46:30 -0800 | [diff] [blame] | 3165 | { |
| 3166 | int i; |
Matthew Rogers | 1cf3d5d | 2020-08-20 00:41:32 +0000 | [diff] [blame] | 3167 | for (i = 0; i < diffstat->nr; i++) |
| 3168 | free_diffstat_file(diffstat->files[i]); |
Junio C Hamano | f604652 | 2007-12-11 23:46:30 -0800 | [diff] [blame] | 3169 | free(diffstat->files); |
| 3170 | } |
| 3171 | |
Johannes Schindelin | 8824689 | 2006-05-20 23:43:13 +0200 | [diff] [blame] | 3172 | struct checkdiff_t { |
Johannes Schindelin | 8824689 | 2006-05-20 23:43:13 +0200 | [diff] [blame] | 3173 | const char *filename; |
Junio C Hamano | 1ba111d | 2008-06-26 15:36:34 -0700 | [diff] [blame] | 3174 | int lineno; |
Junio C Hamano | a757c64 | 2010-03-24 19:21:32 -0700 | [diff] [blame] | 3175 | int conflict_marker_size; |
Junio C Hamano | 1ba111d | 2008-06-26 15:36:34 -0700 | [diff] [blame] | 3176 | struct diff_options *o; |
Junio C Hamano | cf1b786 | 2007-12-06 00:14:14 -0800 | [diff] [blame] | 3177 | unsigned ws_rule; |
Wincent Colaiuta | 62c6489 | 2007-12-13 21:24:52 +0100 | [diff] [blame] | 3178 | unsigned status; |
Johannes Schindelin | 8824689 | 2006-05-20 23:43:13 +0200 | [diff] [blame] | 3179 | }; |
| 3180 | |
Junio C Hamano | a757c64 | 2010-03-24 19:21:32 -0700 | [diff] [blame] | 3181 | static int is_conflict_marker(const char *line, int marker_size, unsigned long len) |
Junio C Hamano | 0495404 | 2008-06-26 15:37:21 -0700 | [diff] [blame] | 3182 | { |
| 3183 | char firstchar; |
| 3184 | int cnt; |
| 3185 | |
Junio C Hamano | a757c64 | 2010-03-24 19:21:32 -0700 | [diff] [blame] | 3186 | if (len < marker_size + 1) |
Junio C Hamano | 0495404 | 2008-06-26 15:37:21 -0700 | [diff] [blame] | 3187 | return 0; |
| 3188 | firstchar = line[0]; |
| 3189 | switch (firstchar) { |
Junio C Hamano | a757c64 | 2010-03-24 19:21:32 -0700 | [diff] [blame] | 3190 | case '=': case '>': case '<': case '|': |
Junio C Hamano | 0495404 | 2008-06-26 15:37:21 -0700 | [diff] [blame] | 3191 | break; |
| 3192 | default: |
| 3193 | return 0; |
| 3194 | } |
Junio C Hamano | a757c64 | 2010-03-24 19:21:32 -0700 | [diff] [blame] | 3195 | for (cnt = 1; cnt < marker_size; cnt++) |
Junio C Hamano | 0495404 | 2008-06-26 15:37:21 -0700 | [diff] [blame] | 3196 | if (line[cnt] != firstchar) |
| 3197 | return 0; |
Elijah Newren | 15beaaa | 2019-11-05 17:07:23 +0000 | [diff] [blame] | 3198 | /* line[1] through line[marker_size-1] are same as firstchar */ |
Junio C Hamano | a757c64 | 2010-03-24 19:21:32 -0700 | [diff] [blame] | 3199 | if (len < marker_size + 1 || !isspace(line[marker_size])) |
Junio C Hamano | 0495404 | 2008-06-26 15:37:21 -0700 | [diff] [blame] | 3200 | return 0; |
Junio C Hamano | 0495404 | 2008-06-26 15:37:21 -0700 | [diff] [blame] | 3201 | return 1; |
| 3202 | } |
| 3203 | |
Jeff King | 75ab763 | 2018-11-02 02:39:03 -0400 | [diff] [blame] | 3204 | static void checkdiff_consume_hunk(void *priv, |
| 3205 | long ob, long on, long nb, long nn, |
| 3206 | const char *func, long funclen) |
| 3207 | |
| 3208 | { |
| 3209 | struct checkdiff_t *data = priv; |
| 3210 | data->lineno = nb - 1; |
| 3211 | } |
| 3212 | |
Johannes Schindelin | 8824689 | 2006-05-20 23:43:13 +0200 | [diff] [blame] | 3213 | static void checkdiff_consume(void *priv, char *line, unsigned long len) |
| 3214 | { |
| 3215 | struct checkdiff_t *data = priv; |
Junio C Hamano | a757c64 | 2010-03-24 19:21:32 -0700 | [diff] [blame] | 3216 | int marker_size = data->conflict_marker_size; |
Jeff King | f1c9626 | 2011-08-17 22:03:12 -0700 | [diff] [blame] | 3217 | const char *ws = diff_get_color(data->o->use_color, DIFF_WHITESPACE); |
| 3218 | const char *reset = diff_get_color(data->o->use_color, DIFF_RESET); |
| 3219 | const char *set = diff_get_color(data->o->use_color, DIFF_FILE_NEW); |
Wincent Colaiuta | c1795bb | 2007-12-13 14:32:29 +0100 | [diff] [blame] | 3220 | char *err; |
John Keeping | 30997bb | 2013-02-07 20:15:27 +0000 | [diff] [blame] | 3221 | const char *line_prefix; |
Bo Yang | 7be5761 | 2010-05-26 15:23:54 +0800 | [diff] [blame] | 3222 | |
| 3223 | assert(data->o); |
John Keeping | 30997bb | 2013-02-07 20:15:27 +0000 | [diff] [blame] | 3224 | line_prefix = diff_line_prefix(data->o); |
Johannes Schindelin | 8824689 | 2006-05-20 23:43:13 +0200 | [diff] [blame] | 3225 | |
| 3226 | if (line[0] == '+') { |
Junio C Hamano | 18374e5 | 2008-06-26 13:16:33 -0700 | [diff] [blame] | 3227 | unsigned bad; |
Junio C Hamano | 0ef617f | 2008-02-15 20:30:05 -0800 | [diff] [blame] | 3228 | data->lineno++; |
Junio C Hamano | a757c64 | 2010-03-24 19:21:32 -0700 | [diff] [blame] | 3229 | if (is_conflict_marker(line + 1, marker_size, len - 1)) { |
Junio C Hamano | 0495404 | 2008-06-26 15:37:21 -0700 | [diff] [blame] | 3230 | data->status |= 1; |
| 3231 | fprintf(data->o->file, |
Bo Yang | 7be5761 | 2010-05-26 15:23:54 +0800 | [diff] [blame] | 3232 | "%s%s:%d: leftover conflict marker\n", |
| 3233 | line_prefix, data->filename, data->lineno); |
Junio C Hamano | 0495404 | 2008-06-26 15:37:21 -0700 | [diff] [blame] | 3234 | } |
Junio C Hamano | 8f8841e | 2008-06-26 15:35:21 -0700 | [diff] [blame] | 3235 | bad = ws_check(line + 1, len - 1, data->ws_rule); |
Junio C Hamano | 18374e5 | 2008-06-26 13:16:33 -0700 | [diff] [blame] | 3236 | if (!bad) |
Wincent Colaiuta | c1795bb | 2007-12-13 14:32:29 +0100 | [diff] [blame] | 3237 | return; |
Junio C Hamano | 18374e5 | 2008-06-26 13:16:33 -0700 | [diff] [blame] | 3238 | data->status |= bad; |
| 3239 | err = whitespace_error_string(bad); |
Bo Yang | 7be5761 | 2010-05-26 15:23:54 +0800 | [diff] [blame] | 3240 | fprintf(data->o->file, "%s%s:%d: %s.\n", |
| 3241 | line_prefix, data->filename, data->lineno, err); |
Wincent Colaiuta | c1795bb | 2007-12-13 14:32:29 +0100 | [diff] [blame] | 3242 | free(err); |
Bo Yang | a3c158d | 2010-05-26 15:08:02 +0800 | [diff] [blame] | 3243 | emit_line(data->o, set, reset, line, 1); |
Junio C Hamano | 8f8841e | 2008-06-26 15:35:21 -0700 | [diff] [blame] | 3244 | ws_check_emit(line + 1, len - 1, data->ws_rule, |
Junio C Hamano | 1ba111d | 2008-06-26 15:36:34 -0700 | [diff] [blame] | 3245 | data->o->file, set, reset, ws); |
Junio C Hamano | 877f23c | 2008-06-26 15:36:59 -0700 | [diff] [blame] | 3246 | } else if (line[0] == ' ') { |
Johannes Schindelin | 8824689 | 2006-05-20 23:43:13 +0200 | [diff] [blame] | 3247 | data->lineno++; |
Johannes Schindelin | 8824689 | 2006-05-20 23:43:13 +0200 | [diff] [blame] | 3248 | } |
| 3249 | } |
| 3250 | |
Junio C Hamano | 0660626 | 2006-05-05 02:41:53 -0700 | [diff] [blame] | 3251 | static unsigned char *deflate_it(char *data, |
| 3252 | unsigned long size, |
| 3253 | unsigned long *result_size) |
Junio C Hamano | 051308f | 2006-05-04 16:51:44 -0700 | [diff] [blame] | 3254 | { |
Junio C Hamano | 0660626 | 2006-05-05 02:41:53 -0700 | [diff] [blame] | 3255 | int bound; |
| 3256 | unsigned char *deflated; |
Junio C Hamano | ef49a7a | 2011-06-10 11:52:15 -0700 | [diff] [blame] | 3257 | git_zstream stream; |
Junio C Hamano | 051308f | 2006-05-04 16:51:44 -0700 | [diff] [blame] | 3258 | |
Junio C Hamano | 55bb5c9 | 2011-06-10 10:55:10 -0700 | [diff] [blame] | 3259 | git_deflate_init(&stream, zlib_compression_level); |
Junio C Hamano | 225a6f1 | 2011-06-10 11:18:17 -0700 | [diff] [blame] | 3260 | bound = git_deflate_bound(&stream, size); |
Junio C Hamano | 0660626 | 2006-05-05 02:41:53 -0700 | [diff] [blame] | 3261 | deflated = xmalloc(bound); |
| 3262 | stream.next_out = deflated; |
| 3263 | stream.avail_out = bound; |
Junio C Hamano | 051308f | 2006-05-04 16:51:44 -0700 | [diff] [blame] | 3264 | |
Junio C Hamano | 0660626 | 2006-05-05 02:41:53 -0700 | [diff] [blame] | 3265 | stream.next_in = (unsigned char *)data; |
| 3266 | stream.avail_in = size; |
Junio C Hamano | 55bb5c9 | 2011-06-10 10:55:10 -0700 | [diff] [blame] | 3267 | while (git_deflate(&stream, Z_FINISH) == Z_OK) |
Junio C Hamano | 0660626 | 2006-05-05 02:41:53 -0700 | [diff] [blame] | 3268 | ; /* nothing */ |
Junio C Hamano | 55bb5c9 | 2011-06-10 10:55:10 -0700 | [diff] [blame] | 3269 | git_deflate_end(&stream); |
Junio C Hamano | 0660626 | 2006-05-05 02:41:53 -0700 | [diff] [blame] | 3270 | *result_size = stream.total_out; |
| 3271 | return deflated; |
Junio C Hamano | 051308f | 2006-05-04 16:51:44 -0700 | [diff] [blame] | 3272 | } |
| 3273 | |
Stefan Beller | 4eed0eb | 2017-06-29 17:07:01 -0700 | [diff] [blame] | 3274 | static void emit_binary_diff_body(struct diff_options *o, |
| 3275 | mmfile_t *one, mmfile_t *two) |
Junio C Hamano | 051308f | 2006-05-04 16:51:44 -0700 | [diff] [blame] | 3276 | { |
Junio C Hamano | 0660626 | 2006-05-05 02:41:53 -0700 | [diff] [blame] | 3277 | void *cp; |
| 3278 | void *delta; |
| 3279 | void *deflated; |
| 3280 | void *data; |
| 3281 | unsigned long orig_size; |
Junio C Hamano | 051308f | 2006-05-04 16:51:44 -0700 | [diff] [blame] | 3282 | unsigned long delta_size; |
Junio C Hamano | 0660626 | 2006-05-05 02:41:53 -0700 | [diff] [blame] | 3283 | unsigned long deflate_size; |
| 3284 | unsigned long data_size; |
Junio C Hamano | 051308f | 2006-05-04 16:51:44 -0700 | [diff] [blame] | 3285 | |
Junio C Hamano | 0660626 | 2006-05-05 02:41:53 -0700 | [diff] [blame] | 3286 | /* We could do deflated delta, or we could do just deflated two, |
| 3287 | * whichever is smaller. |
| 3288 | */ |
| 3289 | delta = NULL; |
| 3290 | deflated = deflate_it(two->ptr, two->size, &deflate_size); |
| 3291 | if (one->size && two->size) { |
| 3292 | delta = diff_delta(one->ptr, one->size, |
| 3293 | two->ptr, two->size, |
| 3294 | &delta_size, deflate_size); |
| 3295 | if (delta) { |
| 3296 | void *to_free = delta; |
| 3297 | orig_size = delta_size; |
| 3298 | delta = deflate_it(delta, delta_size, &delta_size); |
| 3299 | free(to_free); |
| 3300 | } |
| 3301 | } |
Junio C Hamano | 051308f | 2006-05-04 16:51:44 -0700 | [diff] [blame] | 3302 | |
Junio C Hamano | 0660626 | 2006-05-05 02:41:53 -0700 | [diff] [blame] | 3303 | if (delta && delta_size < deflate_size) { |
Torsten Bögershausen | ca473ce | 2018-11-11 08:05:04 +0100 | [diff] [blame] | 3304 | char *s = xstrfmt("%"PRIuMAX , (uintmax_t)orig_size); |
Stefan Beller | 4eed0eb | 2017-06-29 17:07:01 -0700 | [diff] [blame] | 3305 | emit_diff_symbol(o, DIFF_SYMBOL_BINARY_DIFF_HEADER_DELTA, |
| 3306 | s, strlen(s), 0); |
| 3307 | free(s); |
Junio C Hamano | 0660626 | 2006-05-05 02:41:53 -0700 | [diff] [blame] | 3308 | free(deflated); |
| 3309 | data = delta; |
| 3310 | data_size = delta_size; |
Stefan Beller | 4eed0eb | 2017-06-29 17:07:01 -0700 | [diff] [blame] | 3311 | } else { |
| 3312 | char *s = xstrfmt("%lu", two->size); |
| 3313 | emit_diff_symbol(o, DIFF_SYMBOL_BINARY_DIFF_HEADER_LITERAL, |
| 3314 | s, strlen(s), 0); |
| 3315 | free(s); |
Junio C Hamano | 0660626 | 2006-05-05 02:41:53 -0700 | [diff] [blame] | 3316 | free(delta); |
| 3317 | data = deflated; |
| 3318 | data_size = deflate_size; |
| 3319 | } |
| 3320 | |
| 3321 | /* emit data encoded in base85 */ |
| 3322 | cp = data; |
| 3323 | while (data_size) { |
Stefan Beller | 4eed0eb | 2017-06-29 17:07:01 -0700 | [diff] [blame] | 3324 | int len; |
Junio C Hamano | 0660626 | 2006-05-05 02:41:53 -0700 | [diff] [blame] | 3325 | int bytes = (52 < data_size) ? 52 : data_size; |
Stefan Beller | 4eed0eb | 2017-06-29 17:07:01 -0700 | [diff] [blame] | 3326 | char line[71]; |
Junio C Hamano | 0660626 | 2006-05-05 02:41:53 -0700 | [diff] [blame] | 3327 | data_size -= bytes; |
Junio C Hamano | 051308f | 2006-05-04 16:51:44 -0700 | [diff] [blame] | 3328 | if (bytes <= 26) |
| 3329 | line[0] = bytes + 'A' - 1; |
| 3330 | else |
| 3331 | line[0] = bytes - 26 + 'a' - 1; |
| 3332 | encode_85(line + 1, cp, bytes); |
Florian Forster | 1d7f171 | 2006-06-18 17:18:09 +0200 | [diff] [blame] | 3333 | cp = (char *) cp + bytes; |
Stefan Beller | 4eed0eb | 2017-06-29 17:07:01 -0700 | [diff] [blame] | 3334 | |
| 3335 | len = strlen(line); |
| 3336 | line[len++] = '\n'; |
| 3337 | line[len] = '\0'; |
| 3338 | |
| 3339 | emit_diff_symbol(o, DIFF_SYMBOL_BINARY_DIFF_BODY, |
| 3340 | line, len, 0); |
Junio C Hamano | 051308f | 2006-05-04 16:51:44 -0700 | [diff] [blame] | 3341 | } |
Stefan Beller | 4eed0eb | 2017-06-29 17:07:01 -0700 | [diff] [blame] | 3342 | emit_diff_symbol(o, DIFF_SYMBOL_BINARY_DIFF_FOOTER, NULL, 0, 0); |
Junio C Hamano | 0660626 | 2006-05-05 02:41:53 -0700 | [diff] [blame] | 3343 | free(data); |
Junio C Hamano | 051308f | 2006-05-04 16:51:44 -0700 | [diff] [blame] | 3344 | } |
| 3345 | |
Stefan Beller | 4eed0eb | 2017-06-29 17:07:01 -0700 | [diff] [blame] | 3346 | static void emit_binary_diff(struct diff_options *o, |
| 3347 | mmfile_t *one, mmfile_t *two) |
Junio C Hamano | d4c452f | 2006-08-16 16:08:14 -0700 | [diff] [blame] | 3348 | { |
Stefan Beller | 4eed0eb | 2017-06-29 17:07:01 -0700 | [diff] [blame] | 3349 | emit_diff_symbol(o, DIFF_SYMBOL_BINARY_DIFF_HEADER, NULL, 0, 0); |
| 3350 | emit_binary_diff_body(o, one, two); |
| 3351 | emit_binary_diff_body(o, two, one); |
Junio C Hamano | d4c452f | 2006-08-16 16:08:14 -0700 | [diff] [blame] | 3352 | } |
| 3353 | |
Nguyễn Thái Ngọc Duy | b78ea5f | 2018-09-21 17:57:19 +0200 | [diff] [blame] | 3354 | int diff_filespec_is_binary(struct repository *r, |
| 3355 | struct diff_filespec *one) |
Junio C Hamano | 29a3eef | 2007-07-06 00:18:54 -0700 | [diff] [blame] | 3356 | { |
Jonathan Tan | 1c37e86 | 2020-04-07 15:11:41 -0700 | [diff] [blame] | 3357 | struct diff_populate_filespec_options dpf_options = { |
| 3358 | .check_binary = 1, |
| 3359 | }; |
| 3360 | |
Jeff King | 122aa6f | 2008-10-05 17:43:36 -0400 | [diff] [blame] | 3361 | if (one->is_binary == -1) { |
Nguyễn Thái Ngọc Duy | acd00ea | 2018-09-21 17:57:33 +0200 | [diff] [blame] | 3362 | diff_filespec_load_driver(one, r->index); |
Jeff King | 122aa6f | 2008-10-05 17:43:36 -0400 | [diff] [blame] | 3363 | if (one->driver->binary != -1) |
| 3364 | one->is_binary = one->driver->binary; |
| 3365 | else { |
| 3366 | if (!one->data && DIFF_FILE_VALID(one)) |
Jonathan Tan | 1c37e86 | 2020-04-07 15:11:41 -0700 | [diff] [blame] | 3367 | diff_populate_filespec(r, one, &dpf_options); |
Nguyễn Thái Ngọc Duy | 6bf3b81 | 2014-08-16 10:08:05 +0700 | [diff] [blame] | 3368 | if (one->is_binary == -1 && one->data) |
Jeff King | 122aa6f | 2008-10-05 17:43:36 -0400 | [diff] [blame] | 3369 | one->is_binary = buffer_is_binary(one->data, |
| 3370 | one->size); |
| 3371 | if (one->is_binary == -1) |
| 3372 | one->is_binary = 0; |
| 3373 | } |
| 3374 | } |
Junio C Hamano | 29a3eef | 2007-07-06 00:18:54 -0700 | [diff] [blame] | 3375 | return one->is_binary; |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 3376 | } |
| 3377 | |
Nguyễn Thái Ngọc Duy | acd00ea | 2018-09-21 17:57:33 +0200 | [diff] [blame] | 3378 | static const struct userdiff_funcname * |
| 3379 | diff_funcname_pattern(struct diff_options *o, struct diff_filespec *one) |
Junio C Hamano | f258475 | 2007-07-06 00:45:10 -0700 | [diff] [blame] | 3380 | { |
Nguyễn Thái Ngọc Duy | acd00ea | 2018-09-21 17:57:33 +0200 | [diff] [blame] | 3381 | diff_filespec_load_driver(one, o->repo->index); |
Jeff King | 122aa6f | 2008-10-05 17:43:36 -0400 | [diff] [blame] | 3382 | return one->driver->funcname.pattern ? &one->driver->funcname : NULL; |
Junio C Hamano | f258475 | 2007-07-06 00:45:10 -0700 | [diff] [blame] | 3383 | } |
| 3384 | |
Junio C Hamano | a5a818e | 2008-08-18 20:08:09 -0700 | [diff] [blame] | 3385 | void diff_set_mnemonic_prefix(struct diff_options *options, const char *a, const char *b) |
| 3386 | { |
| 3387 | if (!options->a_prefix) |
| 3388 | options->a_prefix = a; |
| 3389 | if (!options->b_prefix) |
| 3390 | options->b_prefix = b; |
| 3391 | } |
| 3392 | |
Nguyễn Thái Ngọc Duy | bd7ad45 | 2018-11-10 06:49:06 +0100 | [diff] [blame] | 3393 | struct userdiff_driver *get_textconv(struct repository *r, |
Nguyễn Thái Ngọc Duy | acd00ea | 2018-09-21 17:57:33 +0200 | [diff] [blame] | 3394 | struct diff_filespec *one) |
Jeff King | 04427ac | 2008-10-26 00:44:53 -0400 | [diff] [blame] | 3395 | { |
| 3396 | if (!DIFF_FILE_VALID(one)) |
| 3397 | return NULL; |
Jeff King | d391c0f | 2010-09-21 17:01:24 -0400 | [diff] [blame] | 3398 | |
Nguyễn Thái Ngọc Duy | bd7ad45 | 2018-11-10 06:49:06 +0100 | [diff] [blame] | 3399 | diff_filespec_load_driver(one, r->index); |
| 3400 | return userdiff_get_textconv(r, one->driver); |
Jeff King | 04427ac | 2008-10-26 00:44:53 -0400 | [diff] [blame] | 3401 | } |
| 3402 | |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 3403 | static void builtin_diff(const char *name_a, |
| 3404 | const char *name_b, |
| 3405 | struct diff_filespec *one, |
| 3406 | struct diff_filespec *two, |
| 3407 | const char *xfrm_msg, |
Christian Couder | 296c6bb | 2010-05-26 04:50:12 +0200 | [diff] [blame] | 3408 | int must_show_header, |
Junio C Hamano | 051308f | 2006-05-04 16:51:44 -0700 | [diff] [blame] | 3409 | struct diff_options *o, |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 3410 | int complete_rewrite) |
| 3411 | { |
| 3412 | mmfile_t mf1, mf2; |
| 3413 | const char *lbl[2]; |
| 3414 | char *a_one, *b_two; |
Jeff King | d9c552f | 2012-11-13 21:12:46 +0530 | [diff] [blame] | 3415 | const char *meta = diff_get_color_opt(o, DIFF_METAINFO); |
Pierre Habouzit | 8f67f8a | 2007-11-10 20:05:14 +0100 | [diff] [blame] | 3416 | const char *reset = diff_get_color_opt(o, DIFF_RESET); |
Junio C Hamano | a5a818e | 2008-08-18 20:08:09 -0700 | [diff] [blame] | 3417 | const char *a_prefix, *b_prefix; |
Jeff King | d9bae1a | 2010-04-01 20:12:15 -0400 | [diff] [blame] | 3418 | struct userdiff_driver *textconv_one = NULL; |
| 3419 | struct userdiff_driver *textconv_two = NULL; |
Greg Bacon | 3e97c7c | 2009-11-19 15:12:24 -0600 | [diff] [blame] | 3420 | struct strbuf header = STRBUF_INIT; |
John Keeping | 30997bb | 2013-02-07 20:15:27 +0000 | [diff] [blame] | 3421 | const char *line_prefix = diff_line_prefix(o); |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 3422 | |
Junio C Hamano | a5a818e | 2008-08-18 20:08:09 -0700 | [diff] [blame] | 3423 | diff_set_mnemonic_prefix(o, "a/", "b/"); |
Brandon Williams | 0d1e0e7 | 2017-10-31 11:19:11 -0700 | [diff] [blame] | 3424 | if (o->flags.reverse_diff) { |
Junio C Hamano | a5a818e | 2008-08-18 20:08:09 -0700 | [diff] [blame] | 3425 | a_prefix = o->b_prefix; |
| 3426 | b_prefix = o->a_prefix; |
| 3427 | } else { |
| 3428 | a_prefix = o->a_prefix; |
| 3429 | b_prefix = o->b_prefix; |
| 3430 | } |
| 3431 | |
Jacob Keller | 61cfbc0 | 2016-08-31 16:27:21 -0700 | [diff] [blame] | 3432 | if (o->submodule_format == DIFF_SUBMODULE_LOG && |
| 3433 | (!one->mode || S_ISGITLINK(one->mode)) && |
| 3434 | (!two->mode || S_ISGITLINK(two->mode))) { |
Stefan Beller | f359713 | 2017-06-29 17:07:00 -0700 | [diff] [blame] | 3435 | show_submodule_summary(o, one->path ? one->path : two->path, |
Jacob Keller | 602a283 | 2016-08-31 16:27:23 -0700 | [diff] [blame] | 3436 | &one->oid, &two->oid, |
Stefan Beller | f359713 | 2017-06-29 17:07:00 -0700 | [diff] [blame] | 3437 | two->dirty_submodule); |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 3438 | return; |
Jacob Keller | fd47ae6 | 2016-08-31 16:27:25 -0700 | [diff] [blame] | 3439 | } else if (o->submodule_format == DIFF_SUBMODULE_INLINE_DIFF && |
| 3440 | (!one->mode || S_ISGITLINK(one->mode)) && |
| 3441 | (!two->mode || S_ISGITLINK(two->mode))) { |
Stefan Beller | f359713 | 2017-06-29 17:07:00 -0700 | [diff] [blame] | 3442 | show_submodule_inline_diff(o, one->path ? one->path : two->path, |
Jacob Keller | fd47ae6 | 2016-08-31 16:27:25 -0700 | [diff] [blame] | 3443 | &one->oid, &two->oid, |
Stefan Beller | f359713 | 2017-06-29 17:07:00 -0700 | [diff] [blame] | 3444 | two->dirty_submodule); |
Jacob Keller | fd47ae6 | 2016-08-31 16:27:25 -0700 | [diff] [blame] | 3445 | return; |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 3446 | } |
| 3447 | |
Brandon Williams | 0d1e0e7 | 2017-10-31 11:19:11 -0700 | [diff] [blame] | 3448 | if (o->flags.allow_textconv) { |
Nguyễn Thái Ngọc Duy | bd7ad45 | 2018-11-10 06:49:06 +0100 | [diff] [blame] | 3449 | textconv_one = get_textconv(o->repo, one); |
| 3450 | textconv_two = get_textconv(o->repo, two); |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 3451 | } |
| 3452 | |
Linus Torvalds | 71b989e | 2008-10-05 15:35:15 -0400 | [diff] [blame] | 3453 | /* Never use a non-valid filename anywhere if at all possible */ |
| 3454 | name_a = DIFF_FILE_VALID(one) ? name_a : name_b; |
| 3455 | name_b = DIFF_FILE_VALID(two) ? name_b : name_a; |
| 3456 | |
Junio C Hamano | a5a818e | 2008-08-18 20:08:09 -0700 | [diff] [blame] | 3457 | a_one = quote_two(a_prefix, name_a + (*name_a == '/')); |
| 3458 | b_two = quote_two(b_prefix, name_b + (*name_b == '/')); |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 3459 | lbl[0] = DIFF_FILE_VALID(one) ? a_one : "/dev/null"; |
| 3460 | lbl[1] = DIFF_FILE_VALID(two) ? b_two : "/dev/null"; |
Jeff King | d9c552f | 2012-11-13 21:12:46 +0530 | [diff] [blame] | 3461 | strbuf_addf(&header, "%s%sdiff --git %s %s%s\n", line_prefix, meta, a_one, b_two, reset); |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 3462 | if (lbl[0][0] == '/') { |
| 3463 | /* /dev/null */ |
Jeff King | d9c552f | 2012-11-13 21:12:46 +0530 | [diff] [blame] | 3464 | strbuf_addf(&header, "%s%snew file mode %06o%s\n", line_prefix, meta, two->mode, reset); |
Bert Wesarg | 3746644 | 2010-05-04 00:38:07 +0200 | [diff] [blame] | 3465 | if (xfrm_msg) |
| 3466 | strbuf_addstr(&header, xfrm_msg); |
Christian Couder | 296c6bb | 2010-05-26 04:50:12 +0200 | [diff] [blame] | 3467 | must_show_header = 1; |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 3468 | } |
| 3469 | else if (lbl[1][0] == '/') { |
Jeff King | d9c552f | 2012-11-13 21:12:46 +0530 | [diff] [blame] | 3470 | strbuf_addf(&header, "%s%sdeleted file mode %06o%s\n", line_prefix, meta, one->mode, reset); |
Bert Wesarg | 3746644 | 2010-05-04 00:38:07 +0200 | [diff] [blame] | 3471 | if (xfrm_msg) |
| 3472 | strbuf_addstr(&header, xfrm_msg); |
Christian Couder | 296c6bb | 2010-05-26 04:50:12 +0200 | [diff] [blame] | 3473 | must_show_header = 1; |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 3474 | } |
| 3475 | else { |
| 3476 | if (one->mode != two->mode) { |
Jeff King | d9c552f | 2012-11-13 21:12:46 +0530 | [diff] [blame] | 3477 | strbuf_addf(&header, "%s%sold mode %06o%s\n", line_prefix, meta, one->mode, reset); |
| 3478 | strbuf_addf(&header, "%s%snew mode %06o%s\n", line_prefix, meta, two->mode, reset); |
Christian Couder | 296c6bb | 2010-05-26 04:50:12 +0200 | [diff] [blame] | 3479 | must_show_header = 1; |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 3480 | } |
Bert Wesarg | 3746644 | 2010-05-04 00:38:07 +0200 | [diff] [blame] | 3481 | if (xfrm_msg) |
| 3482 | strbuf_addstr(&header, xfrm_msg); |
Greg Bacon | 3e97c7c | 2009-11-19 15:12:24 -0600 | [diff] [blame] | 3483 | |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 3484 | /* |
| 3485 | * we do not run diff between different kind |
| 3486 | * of objects. |
| 3487 | */ |
| 3488 | if ((one->mode ^ two->mode) & S_IFMT) |
| 3489 | goto free_ab_and_return; |
Jeff King | 0c01857 | 2008-12-09 03:12:28 -0500 | [diff] [blame] | 3490 | if (complete_rewrite && |
Nguyễn Thái Ngọc Duy | b78ea5f | 2018-09-21 17:57:19 +0200 | [diff] [blame] | 3491 | (textconv_one || !diff_filespec_is_binary(o->repo, one)) && |
| 3492 | (textconv_two || !diff_filespec_is_binary(o->repo, two))) { |
Stefan Beller | a29b0a1 | 2017-06-29 17:06:57 -0700 | [diff] [blame] | 3493 | emit_diff_symbol(o, DIFF_SYMBOL_HEADER, |
| 3494 | header.buf, header.len, 0); |
Greg Bacon | 3e97c7c | 2009-11-19 15:12:24 -0600 | [diff] [blame] | 3495 | strbuf_reset(&header); |
Jeff King | 3aa1f7c | 2008-12-09 03:13:21 -0500 | [diff] [blame] | 3496 | emit_rewrite_diff(name_a, name_b, one, two, |
Nguyễn Thái Ngọc Duy | b78ea5f | 2018-09-21 17:57:19 +0200 | [diff] [blame] | 3497 | textconv_one, textconv_two, o); |
Johannes Schindelin | 34a5e1a | 2007-02-25 23:34:54 +0100 | [diff] [blame] | 3498 | o->found_changes = 1; |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 3499 | goto free_ab_and_return; |
| 3500 | } |
| 3501 | } |
| 3502 | |
Junio C Hamano | 467ddc1 | 2011-02-28 16:11:55 -0800 | [diff] [blame] | 3503 | if (o->irreversible_delete && lbl[1][0] == '/') { |
Stefan Beller | a29b0a1 | 2017-06-29 17:06:57 -0700 | [diff] [blame] | 3504 | emit_diff_symbol(o, DIFF_SYMBOL_HEADER, header.buf, |
| 3505 | header.len, 0); |
Junio C Hamano | 467ddc1 | 2011-02-28 16:11:55 -0800 | [diff] [blame] | 3506 | strbuf_reset(&header); |
| 3507 | goto free_ab_and_return; |
Brandon Williams | 0d1e0e7 | 2017-10-31 11:19:11 -0700 | [diff] [blame] | 3508 | } else if (!o->flags.text && |
Nguyễn Thái Ngọc Duy | b78ea5f | 2018-09-21 17:57:19 +0200 | [diff] [blame] | 3509 | ( (!textconv_one && diff_filespec_is_binary(o->repo, one)) || |
| 3510 | (!textconv_two && diff_filespec_is_binary(o->repo, two)) )) { |
Stefan Beller | 4acaaa7 | 2017-06-29 17:06:58 -0700 | [diff] [blame] | 3511 | struct strbuf sb = STRBUF_INIT; |
Nguyễn Thái Ngọc Duy | 1aaf69e | 2014-08-16 10:08:06 +0700 | [diff] [blame] | 3512 | if (!one->data && !two->data && |
| 3513 | S_ISREG(one->mode) && S_ISREG(two->mode) && |
Brandon Williams | 0d1e0e7 | 2017-10-31 11:19:11 -0700 | [diff] [blame] | 3514 | !o->flags.binary) { |
Jeff King | 4a7e27e | 2018-08-28 17:22:40 -0400 | [diff] [blame] | 3515 | if (oideq(&one->oid, &two->oid)) { |
Nguyễn Thái Ngọc Duy | 1aaf69e | 2014-08-16 10:08:06 +0700 | [diff] [blame] | 3516 | if (must_show_header) |
Stefan Beller | a29b0a1 | 2017-06-29 17:06:57 -0700 | [diff] [blame] | 3517 | emit_diff_symbol(o, DIFF_SYMBOL_HEADER, |
| 3518 | header.buf, header.len, |
| 3519 | 0); |
Nguyễn Thái Ngọc Duy | 1aaf69e | 2014-08-16 10:08:06 +0700 | [diff] [blame] | 3520 | goto free_ab_and_return; |
| 3521 | } |
Stefan Beller | a29b0a1 | 2017-06-29 17:06:57 -0700 | [diff] [blame] | 3522 | emit_diff_symbol(o, DIFF_SYMBOL_HEADER, |
| 3523 | header.buf, header.len, 0); |
Stefan Beller | 4acaaa7 | 2017-06-29 17:06:58 -0700 | [diff] [blame] | 3524 | strbuf_addf(&sb, "%sBinary files %s and %s differ\n", |
| 3525 | diff_line_prefix(o), lbl[0], lbl[1]); |
| 3526 | emit_diff_symbol(o, DIFF_SYMBOL_BINARY_FILES, |
| 3527 | sb.buf, sb.len, 0); |
| 3528 | strbuf_release(&sb); |
Nguyễn Thái Ngọc Duy | 1aaf69e | 2014-08-16 10:08:06 +0700 | [diff] [blame] | 3529 | goto free_ab_and_return; |
| 3530 | } |
Nguyễn Thái Ngọc Duy | b78ea5f | 2018-09-21 17:57:19 +0200 | [diff] [blame] | 3531 | if (fill_mmfile(o->repo, &mf1, one) < 0 || |
| 3532 | fill_mmfile(o->repo, &mf2, two) < 0) |
Jeff King | b337398 | 2010-04-01 20:14:24 -0400 | [diff] [blame] | 3533 | die("unable to read files to diff"); |
Junio C Hamano | 0660626 | 2006-05-05 02:41:53 -0700 | [diff] [blame] | 3534 | /* Quite common confusing case */ |
| 3535 | if (mf1.size == mf2.size && |
Christian Couder | 296c6bb | 2010-05-26 04:50:12 +0200 | [diff] [blame] | 3536 | !memcmp(mf1.ptr, mf2.ptr, mf1.size)) { |
| 3537 | if (must_show_header) |
Stefan Beller | a29b0a1 | 2017-06-29 17:06:57 -0700 | [diff] [blame] | 3538 | emit_diff_symbol(o, DIFF_SYMBOL_HEADER, |
| 3539 | header.buf, header.len, 0); |
Junio C Hamano | 0660626 | 2006-05-05 02:41:53 -0700 | [diff] [blame] | 3540 | goto free_ab_and_return; |
Christian Couder | 296c6bb | 2010-05-26 04:50:12 +0200 | [diff] [blame] | 3541 | } |
Stefan Beller | a29b0a1 | 2017-06-29 17:06:57 -0700 | [diff] [blame] | 3542 | emit_diff_symbol(o, DIFF_SYMBOL_HEADER, header.buf, header.len, 0); |
Greg Bacon | 3e97c7c | 2009-11-19 15:12:24 -0600 | [diff] [blame] | 3543 | strbuf_reset(&header); |
Brandon Williams | 0d1e0e7 | 2017-10-31 11:19:11 -0700 | [diff] [blame] | 3544 | if (o->flags.binary) |
Stefan Beller | 4eed0eb | 2017-06-29 17:07:01 -0700 | [diff] [blame] | 3545 | emit_binary_diff(o, &mf1, &mf2); |
Stefan Beller | 4acaaa7 | 2017-06-29 17:06:58 -0700 | [diff] [blame] | 3546 | else { |
| 3547 | strbuf_addf(&sb, "%sBinary files %s and %s differ\n", |
| 3548 | diff_line_prefix(o), lbl[0], lbl[1]); |
| 3549 | emit_diff_symbol(o, DIFF_SYMBOL_BINARY_FILES, |
| 3550 | sb.buf, sb.len, 0); |
| 3551 | strbuf_release(&sb); |
| 3552 | } |
Johannes Schindelin | 34a5e1a | 2007-02-25 23:34:54 +0100 | [diff] [blame] | 3553 | o->found_changes = 1; |
Junio C Hamano | 467ddc1 | 2011-02-28 16:11:55 -0800 | [diff] [blame] | 3554 | } else { |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 3555 | /* Crazy xdl interfaces.. */ |
Jeff King | 0da0e92 | 2019-01-11 17:17:22 -0500 | [diff] [blame] | 3556 | const char *diffopts; |
Jeff King | ae021d8 | 2014-06-18 15:47:50 -0400 | [diff] [blame] | 3557 | const char *v; |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 3558 | xpparam_t xpp; |
| 3559 | xdemitconf_t xecfg; |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 3560 | struct emit_callback ecbdata; |
Jeff King | be58e70 | 2008-10-05 17:43:21 -0400 | [diff] [blame] | 3561 | const struct userdiff_funcname *pe; |
Junio C Hamano | f258475 | 2007-07-06 00:45:10 -0700 | [diff] [blame] | 3562 | |
Junio C Hamano | b3f01ff | 2012-02-29 18:14:16 -0800 | [diff] [blame] | 3563 | if (must_show_header) { |
Stefan Beller | a29b0a1 | 2017-06-29 17:06:57 -0700 | [diff] [blame] | 3564 | emit_diff_symbol(o, DIFF_SYMBOL_HEADER, |
| 3565 | header.buf, header.len, 0); |
Greg Bacon | 3e97c7c | 2009-11-19 15:12:24 -0600 | [diff] [blame] | 3566 | strbuf_reset(&header); |
| 3567 | } |
| 3568 | |
Nguyễn Thái Ngọc Duy | 6afaf80 | 2018-09-21 17:57:22 +0200 | [diff] [blame] | 3569 | mf1.size = fill_textconv(o->repo, textconv_one, one, &mf1.ptr); |
| 3570 | mf2.size = fill_textconv(o->repo, textconv_two, two, &mf2.ptr); |
Jeff King | 04427ac | 2008-10-26 00:44:53 -0400 | [diff] [blame] | 3571 | |
Nguyễn Thái Ngọc Duy | acd00ea | 2018-09-21 17:57:33 +0200 | [diff] [blame] | 3572 | pe = diff_funcname_pattern(o, one); |
Brandon Casey | 45e7ca0 | 2008-09-18 17:40:48 -0500 | [diff] [blame] | 3573 | if (!pe) |
Nguyễn Thái Ngọc Duy | acd00ea | 2018-09-21 17:57:33 +0200 | [diff] [blame] | 3574 | pe = diff_funcname_pattern(o, two); |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 3575 | |
Brian Downing | 9ccd0a8 | 2008-10-25 15:30:37 +0200 | [diff] [blame] | 3576 | memset(&xpp, 0, sizeof(xpp)); |
Johannes Schindelin | 30b2501 | 2007-07-04 19:05:46 +0100 | [diff] [blame] | 3577 | memset(&xecfg, 0, sizeof(xecfg)); |
Johannes Schindelin | cd112ce | 2006-06-13 18:45:44 +0200 | [diff] [blame] | 3578 | memset(&ecbdata, 0, sizeof(ecbdata)); |
Johannes Schindelin | 1cdde29 | 2018-08-13 04:33:11 -0700 | [diff] [blame] | 3579 | if (o->flags.suppress_diff_headers) |
| 3580 | lbl[0] = NULL; |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 3581 | ecbdata.label_path = lbl; |
Jeff King | daa0c3d | 2011-08-17 22:04:23 -0700 | [diff] [blame] | 3582 | ecbdata.color_diff = want_color(o->use_color); |
Nguyễn Thái Ngọc Duy | 26d024e | 2018-09-21 17:57:37 +0200 | [diff] [blame] | 3583 | ecbdata.ws_rule = whitespace_rule(o->repo->index, name_b); |
Junio C Hamano | 690ed84 | 2009-09-04 00:41:15 -0700 | [diff] [blame] | 3584 | if (ecbdata.ws_rule & WS_BLANK_AT_EOF) |
Junio C Hamano | d68fe26 | 2009-09-14 22:05:57 -0700 | [diff] [blame] | 3585 | check_blank_at_eof(&mf1, &mf2, &ecbdata); |
Bo Yang | a3c158d | 2010-05-26 15:08:02 +0800 | [diff] [blame] | 3586 | ecbdata.opt = o; |
Johannes Schindelin | 1cdde29 | 2018-08-13 04:33:11 -0700 | [diff] [blame] | 3587 | if (header.len && !o->flags.suppress_diff_headers) |
| 3588 | ecbdata.header = &header; |
René Scharfe | 582aa00 | 2010-05-02 15:04:41 +0200 | [diff] [blame] | 3589 | xpp.flags = o->xdl_opts; |
Jonathan Tan | 2477ab2 | 2017-11-27 11:47:47 -0800 | [diff] [blame] | 3590 | xpp.anchors = o->anchors; |
| 3591 | xpp.anchors_nr = o->anchors_nr; |
Linus Torvalds | ee1e541 | 2006-05-13 13:23:48 -0700 | [diff] [blame] | 3592 | xecfg.ctxlen = o->context; |
René Scharfe | 6d0e674 | 2008-12-28 19:45:32 +0100 | [diff] [blame] | 3593 | xecfg.interhunkctxlen = o->interhunkcontext; |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 3594 | xecfg.flags = XDL_EMIT_FUNCNAMES; |
Brandon Williams | 0d1e0e7 | 2017-10-31 11:19:11 -0700 | [diff] [blame] | 3595 | if (o->flags.funccontext) |
René Scharfe | 14937c2 | 2011-10-09 13:36:57 +0200 | [diff] [blame] | 3596 | xecfg.flags |= XDL_EMIT_FUNCCONTEXT; |
Brandon Casey | 45e7ca0 | 2008-09-18 17:40:48 -0500 | [diff] [blame] | 3597 | if (pe) |
Brandon Casey | a013585 | 2008-09-18 17:42:48 -0500 | [diff] [blame] | 3598 | xdiff_set_find_func(&xecfg, pe->pattern, pe->cflags); |
Jeff King | 0da0e92 | 2019-01-11 17:17:22 -0500 | [diff] [blame] | 3599 | |
| 3600 | diffopts = getenv("GIT_DIFF_OPTS"); |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 3601 | if (!diffopts) |
| 3602 | ; |
Jeff King | ae021d8 | 2014-06-18 15:47:50 -0400 | [diff] [blame] | 3603 | else if (skip_prefix(diffopts, "--unified=", &v)) |
| 3604 | xecfg.ctxlen = strtoul(v, NULL, 10); |
| 3605 | else if (skip_prefix(diffopts, "-u", &v)) |
| 3606 | xecfg.ctxlen = strtoul(v, NULL, 10); |
Jeff King | 0da0e92 | 2019-01-11 17:17:22 -0500 | [diff] [blame] | 3607 | |
Thomas Rast | 77d1a52 | 2012-03-14 19:24:08 +0100 | [diff] [blame] | 3608 | if (o->word_diff) |
| 3609 | init_diff_words_data(&ecbdata, o, one, two); |
Jeff King | 9346d6d | 2018-11-02 02:35:45 -0400 | [diff] [blame] | 3610 | if (xdi_diff_outf(&mf1, &mf2, NULL, fn_out_consume, |
| 3611 | &ecbdata, &xpp, &xecfg)) |
Jeff King | 3efb988 | 2015-09-24 19:12:23 -0400 | [diff] [blame] | 3612 | die("unable to generate diff for %s", one->path); |
Thomas Rast | 882749a | 2010-04-14 17:59:06 +0200 | [diff] [blame] | 3613 | if (o->word_diff) |
Johannes Schindelin | f59a59e | 2006-07-28 23:56:15 +0200 | [diff] [blame] | 3614 | free_diff_words_data(&ecbdata); |
Jeff King | 04427ac | 2008-10-26 00:44:53 -0400 | [diff] [blame] | 3615 | if (textconv_one) |
| 3616 | free(mf1.ptr); |
| 3617 | if (textconv_two) |
| 3618 | free(mf2.ptr); |
René Scharfe | 8cfe5f1 | 2009-07-02 00:01:43 +0200 | [diff] [blame] | 3619 | xdiff_clear_find_func(&xecfg); |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 3620 | } |
| 3621 | |
| 3622 | free_ab_and_return: |
Greg Bacon | 3e97c7c | 2009-11-19 15:12:24 -0600 | [diff] [blame] | 3623 | strbuf_release(&header); |
Junio C Hamano | fc3abdf | 2007-05-03 13:05:48 -0700 | [diff] [blame] | 3624 | diff_free_filespec_data(one); |
| 3625 | diff_free_filespec_data(two); |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 3626 | free(a_one); |
| 3627 | free(b_two); |
| 3628 | return; |
| 3629 | } |
| 3630 | |
Nguyễn Thái Ngọc Duy | ddf88fa | 2018-02-24 21:09:59 +0700 | [diff] [blame] | 3631 | static char *get_compact_summary(const struct diff_filepair *p, int is_renamed) |
| 3632 | { |
| 3633 | if (!is_renamed) { |
| 3634 | if (p->status == DIFF_STATUS_ADDED) { |
| 3635 | if (S_ISLNK(p->two->mode)) |
| 3636 | return "new +l"; |
| 3637 | else if ((p->two->mode & 0777) == 0755) |
| 3638 | return "new +x"; |
| 3639 | else |
| 3640 | return "new"; |
| 3641 | } else if (p->status == DIFF_STATUS_DELETED) |
| 3642 | return "gone"; |
| 3643 | } |
| 3644 | if (S_ISLNK(p->one->mode) && !S_ISLNK(p->two->mode)) |
| 3645 | return "mode -l"; |
| 3646 | else if (!S_ISLNK(p->one->mode) && S_ISLNK(p->two->mode)) |
| 3647 | return "mode +l"; |
| 3648 | else if ((p->one->mode & 0777) == 0644 && |
| 3649 | (p->two->mode & 0777) == 0755) |
| 3650 | return "mode +x"; |
| 3651 | else if ((p->one->mode & 0777) == 0755 && |
| 3652 | (p->two->mode & 0777) == 0644) |
| 3653 | return "mode -x"; |
| 3654 | return NULL; |
| 3655 | } |
| 3656 | |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 3657 | static void builtin_diffstat(const char *name_a, const char *name_b, |
| 3658 | struct diff_filespec *one, |
| 3659 | struct diff_filespec *two, |
Junio C Hamano | 710158e | 2006-04-25 23:40:09 -0700 | [diff] [blame] | 3660 | struct diffstat_t *diffstat, |
Johannes Schindelin | 0d21efa | 2006-06-14 17:40:23 +0200 | [diff] [blame] | 3661 | struct diff_options *o, |
Linus Torvalds | 74faaa1 | 2012-10-17 10:00:37 -0700 | [diff] [blame] | 3662 | struct diff_filepair *p) |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 3663 | { |
| 3664 | mmfile_t mf1, mf2; |
| 3665 | struct diffstat_file *data; |
Zbigniew Jędrzejewski-Szmek | 352ca4e | 2012-05-01 19:10:15 +0200 | [diff] [blame] | 3666 | int same_contents; |
Linus Torvalds | 74faaa1 | 2012-10-17 10:00:37 -0700 | [diff] [blame] | 3667 | int complete_rewrite = 0; |
| 3668 | |
| 3669 | if (!DIFF_PAIR_UNMERGED(p)) { |
| 3670 | if (p->status == DIFF_STATUS_MODIFIED && p->score) |
| 3671 | complete_rewrite = 1; |
| 3672 | } |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 3673 | |
| 3674 | data = diffstat_add(diffstat, name_a, name_b); |
Junio C Hamano | 99bfd40 | 2012-11-27 11:17:14 -0800 | [diff] [blame] | 3675 | data->is_interesting = p->status != DIFF_STATUS_UNKNOWN; |
Nguyễn Thái Ngọc Duy | ddf88fa | 2018-02-24 21:09:59 +0700 | [diff] [blame] | 3676 | if (o->flags.stat_with_summary) |
| 3677 | data->comments = get_compact_summary(p, data->is_renamed); |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 3678 | |
| 3679 | if (!one || !two) { |
| 3680 | data->is_unmerged = 1; |
| 3681 | return; |
| 3682 | } |
Jeff King | ded0abc | 2011-02-19 03:04:56 -0500 | [diff] [blame] | 3683 | |
Jeff King | 4a7e27e | 2018-08-28 17:22:40 -0400 | [diff] [blame] | 3684 | same_contents = oideq(&one->oid, &two->oid); |
Zbigniew Jędrzejewski-Szmek | 352ca4e | 2012-05-01 19:10:15 +0200 | [diff] [blame] | 3685 | |
Nguyễn Thái Ngọc Duy | b78ea5f | 2018-09-21 17:57:19 +0200 | [diff] [blame] | 3686 | if (diff_filespec_is_binary(o->repo, one) || |
| 3687 | diff_filespec_is_binary(o->repo, two)) { |
Jeff King | ded0abc | 2011-02-19 03:04:56 -0500 | [diff] [blame] | 3688 | data->is_binary = 1; |
Zbigniew Jędrzejewski-Szmek | 352ca4e | 2012-05-01 19:10:15 +0200 | [diff] [blame] | 3689 | if (same_contents) { |
Zbigniew Jędrzejewski-Szmek | e18872b | 2012-05-01 19:10:14 +0200 | [diff] [blame] | 3690 | data->added = 0; |
| 3691 | data->deleted = 0; |
| 3692 | } else { |
Nguyễn Thái Ngọc Duy | b78ea5f | 2018-09-21 17:57:19 +0200 | [diff] [blame] | 3693 | data->added = diff_filespec_size(o->repo, two); |
| 3694 | data->deleted = diff_filespec_size(o->repo, one); |
Zbigniew Jędrzejewski-Szmek | e18872b | 2012-05-01 19:10:14 +0200 | [diff] [blame] | 3695 | } |
Jeff King | ded0abc | 2011-02-19 03:04:56 -0500 | [diff] [blame] | 3696 | } |
| 3697 | |
| 3698 | else if (complete_rewrite) { |
Jonathan Tan | 1c37e86 | 2020-04-07 15:11:41 -0700 | [diff] [blame] | 3699 | diff_populate_filespec(o->repo, one, NULL); |
| 3700 | diff_populate_filespec(o->repo, two, NULL); |
Junio C Hamano | 710158e | 2006-04-25 23:40:09 -0700 | [diff] [blame] | 3701 | data->deleted = count_lines(one->data, one->size); |
| 3702 | data->added = count_lines(two->data, two->size); |
Junio C Hamano | 710158e | 2006-04-25 23:40:09 -0700 | [diff] [blame] | 3703 | } |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 3704 | |
Zbigniew Jędrzejewski-Szmek | 352ca4e | 2012-05-01 19:10:15 +0200 | [diff] [blame] | 3705 | else if (!same_contents) { |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 3706 | /* Crazy xdl interfaces.. */ |
| 3707 | xpparam_t xpp; |
| 3708 | xdemitconf_t xecfg; |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 3709 | |
Nguyễn Thái Ngọc Duy | b78ea5f | 2018-09-21 17:57:19 +0200 | [diff] [blame] | 3710 | if (fill_mmfile(o->repo, &mf1, one) < 0 || |
| 3711 | fill_mmfile(o->repo, &mf2, two) < 0) |
Jeff King | ded0abc | 2011-02-19 03:04:56 -0500 | [diff] [blame] | 3712 | die("unable to read files to diff"); |
| 3713 | |
Brian Downing | 9ccd0a8 | 2008-10-25 15:30:37 +0200 | [diff] [blame] | 3714 | memset(&xpp, 0, sizeof(xpp)); |
Johannes Schindelin | 30b2501 | 2007-07-04 19:05:46 +0100 | [diff] [blame] | 3715 | memset(&xecfg, 0, sizeof(xecfg)); |
René Scharfe | 582aa00 | 2010-05-02 15:04:41 +0200 | [diff] [blame] | 3716 | xpp.flags = o->xdl_opts; |
Jonathan Tan | 2477ab2 | 2017-11-27 11:47:47 -0800 | [diff] [blame] | 3717 | xpp.anchors = o->anchors; |
| 3718 | xpp.anchors_nr = o->anchors_nr; |
Junio C Hamano | f01cae9 | 2011-09-22 10:54:47 -0700 | [diff] [blame] | 3719 | xecfg.ctxlen = o->context; |
| 3720 | xecfg.interhunkctxlen = o->interhunkcontext; |
Jeff King | 3b40a09 | 2018-11-02 02:36:06 -0400 | [diff] [blame] | 3721 | if (xdi_diff_outf(&mf1, &mf2, discard_hunk_line, |
| 3722 | diffstat_consume, diffstat, &xpp, &xecfg)) |
Jeff King | 3efb988 | 2015-09-24 19:12:23 -0400 | [diff] [blame] | 3723 | die("unable to generate diffstat for %s", one->path); |
Matthew Rogers | 1cf3d5d | 2020-08-20 00:41:32 +0000 | [diff] [blame] | 3724 | |
| 3725 | if (DIFF_FILE_VALID(one) && DIFF_FILE_VALID(two)) { |
| 3726 | struct diffstat_file *file = |
| 3727 | diffstat->files[diffstat->nr - 1]; |
| 3728 | /* |
| 3729 | * Omit diffstats of modified files where nothing changed. |
| 3730 | * Even if !same_contents, this might be the case due to |
| 3731 | * ignoring whitespace changes, etc. |
| 3732 | * |
| 3733 | * But note that we special-case additions, deletions, |
| 3734 | * renames, and mode changes as adding an empty file, |
| 3735 | * for example is still of interest. |
| 3736 | */ |
| 3737 | if ((p->status == DIFF_STATUS_MODIFIED) |
| 3738 | && !file->added |
| 3739 | && !file->deleted |
| 3740 | && one->mode == two->mode) { |
| 3741 | free_diffstat_file(file); |
| 3742 | diffstat->nr--; |
| 3743 | } |
| 3744 | } |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 3745 | } |
Junio C Hamano | fc3abdf | 2007-05-03 13:05:48 -0700 | [diff] [blame] | 3746 | |
Junio C Hamano | fc3abdf | 2007-05-03 13:05:48 -0700 | [diff] [blame] | 3747 | diff_free_filespec_data(one); |
| 3748 | diff_free_filespec_data(two); |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 3749 | } |
| 3750 | |
Johannes Schindelin | 8824689 | 2006-05-20 23:43:13 +0200 | [diff] [blame] | 3751 | static void builtin_checkdiff(const char *name_a, const char *name_b, |
Junio C Hamano | cd676a5 | 2008-02-12 14:26:02 -0800 | [diff] [blame] | 3752 | const char *attr_path, |
Junio C Hamano | 5ff10dd | 2008-06-26 15:34:54 -0700 | [diff] [blame] | 3753 | struct diff_filespec *one, |
| 3754 | struct diff_filespec *two, |
| 3755 | struct diff_options *o) |
Johannes Schindelin | 8824689 | 2006-05-20 23:43:13 +0200 | [diff] [blame] | 3756 | { |
| 3757 | mmfile_t mf1, mf2; |
| 3758 | struct checkdiff_t data; |
| 3759 | |
| 3760 | if (!two) |
| 3761 | return; |
| 3762 | |
| 3763 | memset(&data, 0, sizeof(data)); |
Johannes Schindelin | 8824689 | 2006-05-20 23:43:13 +0200 | [diff] [blame] | 3764 | data.filename = name_b ? name_b : name_a; |
| 3765 | data.lineno = 0; |
Junio C Hamano | 1ba111d | 2008-06-26 15:36:34 -0700 | [diff] [blame] | 3766 | data.o = o; |
Nguyễn Thái Ngọc Duy | 26d024e | 2018-09-21 17:57:37 +0200 | [diff] [blame] | 3767 | data.ws_rule = whitespace_rule(o->repo->index, attr_path); |
Nguyễn Thái Ngọc Duy | 32eaa46 | 2018-09-21 17:57:27 +0200 | [diff] [blame] | 3768 | data.conflict_marker_size = ll_merge_marker_size(o->repo->index, attr_path); |
Johannes Schindelin | 8824689 | 2006-05-20 23:43:13 +0200 | [diff] [blame] | 3769 | |
Nguyễn Thái Ngọc Duy | b78ea5f | 2018-09-21 17:57:19 +0200 | [diff] [blame] | 3770 | if (fill_mmfile(o->repo, &mf1, one) < 0 || |
| 3771 | fill_mmfile(o->repo, &mf2, two) < 0) |
Johannes Schindelin | 8824689 | 2006-05-20 23:43:13 +0200 | [diff] [blame] | 3772 | die("unable to read files to diff"); |
| 3773 | |
Junio C Hamano | 5ff10dd | 2008-06-26 15:34:54 -0700 | [diff] [blame] | 3774 | /* |
| 3775 | * All the other codepaths check both sides, but not checking |
| 3776 | * the "old" side here is deliberate. We are checking the newly |
| 3777 | * introduced changes, and as long as the "new" side is text, we |
| 3778 | * can and should check what it introduces. |
| 3779 | */ |
Nguyễn Thái Ngọc Duy | b78ea5f | 2018-09-21 17:57:19 +0200 | [diff] [blame] | 3780 | if (diff_filespec_is_binary(o->repo, two)) |
Junio C Hamano | fc3abdf | 2007-05-03 13:05:48 -0700 | [diff] [blame] | 3781 | goto free_and_return; |
Johannes Schindelin | 8824689 | 2006-05-20 23:43:13 +0200 | [diff] [blame] | 3782 | else { |
| 3783 | /* Crazy xdl interfaces.. */ |
| 3784 | xpparam_t xpp; |
| 3785 | xdemitconf_t xecfg; |
Johannes Schindelin | 8824689 | 2006-05-20 23:43:13 +0200 | [diff] [blame] | 3786 | |
Brian Downing | 9ccd0a8 | 2008-10-25 15:30:37 +0200 | [diff] [blame] | 3787 | memset(&xpp, 0, sizeof(xpp)); |
Johannes Schindelin | 30b2501 | 2007-07-04 19:05:46 +0100 | [diff] [blame] | 3788 | memset(&xecfg, 0, sizeof(xecfg)); |
Junio C Hamano | c35539e | 2008-08-20 11:47:55 -0700 | [diff] [blame] | 3789 | xecfg.ctxlen = 1; /* at least one context line */ |
René Scharfe | 582aa00 | 2010-05-02 15:04:41 +0200 | [diff] [blame] | 3790 | xpp.flags = 0; |
Jeff King | 75ab763 | 2018-11-02 02:39:03 -0400 | [diff] [blame] | 3791 | if (xdi_diff_outf(&mf1, &mf2, checkdiff_consume_hunk, |
| 3792 | checkdiff_consume, &data, |
Jeff King | 3efb988 | 2015-09-24 19:12:23 -0400 | [diff] [blame] | 3793 | &xpp, &xecfg)) |
| 3794 | die("unable to generate checkdiff for %s", one->path); |
Junio C Hamano | 877f23c | 2008-06-26 15:36:59 -0700 | [diff] [blame] | 3795 | |
Junio C Hamano | 467babf | 2009-09-03 23:39:43 -0700 | [diff] [blame] | 3796 | if (data.ws_rule & WS_BLANK_AT_EOF) { |
Junio C Hamano | d68fe26 | 2009-09-14 22:05:57 -0700 | [diff] [blame] | 3797 | struct emit_callback ecbdata; |
| 3798 | int blank_at_eof; |
| 3799 | |
| 3800 | ecbdata.ws_rule = data.ws_rule; |
| 3801 | check_blank_at_eof(&mf1, &mf2, &ecbdata); |
Christoph Mallon | 8837d33 | 2010-10-10 19:24:06 +0200 | [diff] [blame] | 3802 | blank_at_eof = ecbdata.blank_at_eof_in_postimage; |
Junio C Hamano | d68fe26 | 2009-09-14 22:05:57 -0700 | [diff] [blame] | 3803 | |
Junio C Hamano | 467babf | 2009-09-03 23:39:43 -0700 | [diff] [blame] | 3804 | if (blank_at_eof) { |
| 3805 | static char *err; |
| 3806 | if (!err) |
| 3807 | err = whitespace_error_string(WS_BLANK_AT_EOF); |
| 3808 | fprintf(o->file, "%s:%d: %s.\n", |
| 3809 | data.filename, blank_at_eof, err); |
| 3810 | data.status = 1; /* report errors */ |
| 3811 | } |
Junio C Hamano | 877f23c | 2008-06-26 15:36:59 -0700 | [diff] [blame] | 3812 | } |
Johannes Schindelin | 8824689 | 2006-05-20 23:43:13 +0200 | [diff] [blame] | 3813 | } |
Junio C Hamano | fc3abdf | 2007-05-03 13:05:48 -0700 | [diff] [blame] | 3814 | free_and_return: |
| 3815 | diff_free_filespec_data(one); |
| 3816 | diff_free_filespec_data(two); |
Wincent Colaiuta | 62c6489 | 2007-12-13 21:24:52 +0100 | [diff] [blame] | 3817 | if (data.status) |
Brandon Williams | 0d1e0e7 | 2017-10-31 11:19:11 -0700 | [diff] [blame] | 3818 | o->flags.check_failed = 1; |
Johannes Schindelin | 8824689 | 2006-05-20 23:43:13 +0200 | [diff] [blame] | 3819 | } |
| 3820 | |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 3821 | struct diff_filespec *alloc_filespec(const char *path) |
| 3822 | { |
Jeff King | 96ffc06 | 2016-02-22 17:44:32 -0500 | [diff] [blame] | 3823 | struct diff_filespec *spec; |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 3824 | |
Jeff King | 96ffc06 | 2016-02-22 17:44:32 -0500 | [diff] [blame] | 3825 | FLEXPTR_ALLOC_STR(spec, path, path); |
Linus Torvalds | 9fb8841 | 2007-10-25 11:19:10 -0700 | [diff] [blame] | 3826 | spec->count = 1; |
Jeff King | 122aa6f | 2008-10-05 17:43:36 -0400 | [diff] [blame] | 3827 | spec->is_binary = -1; |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 3828 | return spec; |
| 3829 | } |
| 3830 | |
Linus Torvalds | 9fb8841 | 2007-10-25 11:19:10 -0700 | [diff] [blame] | 3831 | void free_filespec(struct diff_filespec *spec) |
| 3832 | { |
| 3833 | if (!--spec->count) { |
| 3834 | diff_free_filespec_data(spec); |
| 3835 | free(spec); |
| 3836 | } |
| 3837 | } |
| 3838 | |
Brandon Williams | f9704c2 | 2017-05-30 10:30:50 -0700 | [diff] [blame] | 3839 | void fill_filespec(struct diff_filespec *spec, const struct object_id *oid, |
| 3840 | int oid_valid, unsigned short mode) |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 3841 | { |
| 3842 | if (mode) { |
| 3843 | spec->mode = canon_mode(mode); |
Brandon Williams | f9704c2 | 2017-05-30 10:30:50 -0700 | [diff] [blame] | 3844 | oidcpy(&spec->oid, oid); |
| 3845 | spec->oid_valid = oid_valid; |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 3846 | } |
| 3847 | } |
| 3848 | |
| 3849 | /* |
Jakub Narebski | 5adf317 | 2007-05-26 00:37:40 +0200 | [diff] [blame] | 3850 | * Given a name and sha1 pair, if the index tells us the file in |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 3851 | * the work tree has that object contents, return true, so that |
| 3852 | * prepare_temp_file() does not have to inflate and extract. |
| 3853 | */ |
Nguyễn Thái Ngọc Duy | b78ea5f | 2018-09-21 17:57:19 +0200 | [diff] [blame] | 3854 | static int reuse_worktree_file(struct index_state *istate, |
| 3855 | const char *name, |
| 3856 | const struct object_id *oid, |
| 3857 | int want_file) |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 3858 | { |
Nguyễn Thái Ngọc Duy | 9c5e6c8 | 2013-07-09 22:29:00 +0700 | [diff] [blame] | 3859 | const struct cache_entry *ce; |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 3860 | struct stat st; |
| 3861 | int pos, len; |
| 3862 | |
Junio C Hamano | 150115a | 2009-03-22 15:26:07 -0700 | [diff] [blame] | 3863 | /* |
| 3864 | * We do not read the cache ourselves here, because the |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 3865 | * benchmark with my previous version that always reads cache |
| 3866 | * shows that it makes things worse for diff-tree comparing |
| 3867 | * two linux-2.6 kernel trees in an already checked out work |
| 3868 | * tree. This is because most diff-tree comparisons deal with |
| 3869 | * only a small number of files, while reading the cache is |
| 3870 | * expensive for a large project, and its cost outweighs the |
| 3871 | * savings we get by not inflating the object to a temporary |
| 3872 | * file. Practically, this code only helps when we are used |
| 3873 | * by diff-cache --cached, which does read the cache before |
| 3874 | * calling us. |
| 3875 | */ |
Nguyễn Thái Ngọc Duy | b78ea5f | 2018-09-21 17:57:19 +0200 | [diff] [blame] | 3876 | if (!istate->cache) |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 3877 | return 0; |
| 3878 | |
Shawn O. Pearce | 1510fea | 2006-12-14 06:15:57 -0500 | [diff] [blame] | 3879 | /* We want to avoid the working directory if our caller |
| 3880 | * doesn't need the data in a normal file, this system |
| 3881 | * is rather slow with its stat/open/mmap/close syscalls, |
| 3882 | * and the object is contained in a pack file. The pack |
| 3883 | * is probably already open and will be faster to obtain |
| 3884 | * the data through than the working directory. Loose |
| 3885 | * objects however would tend to be slower as they need |
| 3886 | * to be individually opened and inflated. |
| 3887 | */ |
brian m. carlson | 14c3c80 | 2018-05-02 00:25:33 +0000 | [diff] [blame] | 3888 | if (!FAST_WORKING_DIRECTORY && !want_file && has_object_pack(oid)) |
Shawn O. Pearce | 1510fea | 2006-12-14 06:15:57 -0500 | [diff] [blame] | 3889 | return 0; |
| 3890 | |
Jeff King | 06dec43 | 2016-07-22 11:27:53 -0400 | [diff] [blame] | 3891 | /* |
| 3892 | * Similarly, if we'd have to convert the file contents anyway, that |
| 3893 | * makes the optimization not worthwhile. |
| 3894 | */ |
Nguyễn Thái Ngọc Duy | b78ea5f | 2018-09-21 17:57:19 +0200 | [diff] [blame] | 3895 | if (!want_file && would_convert_to_git(istate, name)) |
Jeff King | 06dec43 | 2016-07-22 11:27:53 -0400 | [diff] [blame] | 3896 | return 0; |
| 3897 | |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 3898 | len = strlen(name); |
Nguyễn Thái Ngọc Duy | b78ea5f | 2018-09-21 17:57:19 +0200 | [diff] [blame] | 3899 | pos = index_name_pos(istate, name, len); |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 3900 | if (pos < 0) |
| 3901 | return 0; |
Nguyễn Thái Ngọc Duy | b78ea5f | 2018-09-21 17:57:19 +0200 | [diff] [blame] | 3902 | ce = istate->cache[pos]; |
Junio C Hamano | eadb583 | 2008-01-18 23:45:24 -0800 | [diff] [blame] | 3903 | |
| 3904 | /* |
| 3905 | * This is not the sha1 we are looking for, or |
| 3906 | * unreusable because it is not a regular file. |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 3907 | */ |
Jeff King | 9001dc2 | 2018-08-28 17:22:48 -0400 | [diff] [blame] | 3908 | if (!oideq(oid, &ce->oid) || !S_ISREG(ce->ce_mode)) |
Junio C Hamano | eadb583 | 2008-01-18 23:45:24 -0800 | [diff] [blame] | 3909 | return 0; |
| 3910 | |
| 3911 | /* |
Junio C Hamano | 150115a | 2009-03-22 15:26:07 -0700 | [diff] [blame] | 3912 | * If ce is marked as "assume unchanged", there is no |
| 3913 | * guarantee that work tree matches what we are looking for. |
| 3914 | */ |
Nguyễn Thái Ngọc Duy | b4d1690 | 2009-08-20 20:46:58 +0700 | [diff] [blame] | 3915 | if ((ce->ce_flags & CE_VALID) || ce_skip_worktree(ce)) |
Junio C Hamano | 150115a | 2009-03-22 15:26:07 -0700 | [diff] [blame] | 3916 | return 0; |
| 3917 | |
| 3918 | /* |
Junio C Hamano | eadb583 | 2008-01-18 23:45:24 -0800 | [diff] [blame] | 3919 | * If ce matches the file in the work tree, we can reuse it. |
| 3920 | */ |
| 3921 | if (ce_uptodate(ce) || |
Nguyễn Thái Ngọc Duy | b78ea5f | 2018-09-21 17:57:19 +0200 | [diff] [blame] | 3922 | (!lstat(name, &st) && !ie_match_stat(istate, ce, &st, 0))) |
Junio C Hamano | eadb583 | 2008-01-18 23:45:24 -0800 | [diff] [blame] | 3923 | return 1; |
| 3924 | |
| 3925 | return 0; |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 3926 | } |
| 3927 | |
Linus Torvalds | 0478675 | 2007-04-15 11:14:28 -0700 | [diff] [blame] | 3928 | static int diff_populate_gitlink(struct diff_filespec *s, int size_only) |
| 3929 | { |
Jeff King | b1ddfb9 | 2016-02-22 17:45:08 -0500 | [diff] [blame] | 3930 | struct strbuf buf = STRBUF_INIT; |
| 3931 | char *dirty = ""; |
Junio C Hamano | 8e08b41 | 2010-01-16 18:42:53 +0100 | [diff] [blame] | 3932 | |
| 3933 | /* Are we looking at the work tree? */ |
Jens Lehmann | 85adbf2 | 2010-03-12 22:23:52 +0100 | [diff] [blame] | 3934 | if (s->dirty_submodule) |
Junio C Hamano | 8e08b41 | 2010-01-16 18:42:53 +0100 | [diff] [blame] | 3935 | dirty = "-dirty"; |
| 3936 | |
brian m. carlson | a0d12c4 | 2016-06-24 23:09:23 +0000 | [diff] [blame] | 3937 | strbuf_addf(&buf, "Subproject commit %s%s\n", |
| 3938 | oid_to_hex(&s->oid), dirty); |
Jeff King | b1ddfb9 | 2016-02-22 17:45:08 -0500 | [diff] [blame] | 3939 | s->size = buf.len; |
Linus Torvalds | 0478675 | 2007-04-15 11:14:28 -0700 | [diff] [blame] | 3940 | if (size_only) { |
| 3941 | s->data = NULL; |
Jeff King | b1ddfb9 | 2016-02-22 17:45:08 -0500 | [diff] [blame] | 3942 | strbuf_release(&buf); |
| 3943 | } else { |
| 3944 | s->data = strbuf_detach(&buf, NULL); |
| 3945 | s->should_free = 1; |
Linus Torvalds | 0478675 | 2007-04-15 11:14:28 -0700 | [diff] [blame] | 3946 | } |
| 3947 | return 0; |
| 3948 | } |
| 3949 | |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 3950 | /* |
| 3951 | * While doing rename detection and pickaxe operation, we may need to |
| 3952 | * grab the data for the blob (or file) for our own in-core comparison. |
| 3953 | * diff_filespec has data and size fields for this purpose. |
| 3954 | */ |
Nguyễn Thái Ngọc Duy | b78ea5f | 2018-09-21 17:57:19 +0200 | [diff] [blame] | 3955 | int diff_populate_filespec(struct repository *r, |
| 3956 | struct diff_filespec *s, |
Jonathan Tan | 1c37e86 | 2020-04-07 15:11:41 -0700 | [diff] [blame] | 3957 | const struct diff_populate_filespec_options *options) |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 3958 | { |
Jonathan Tan | 1c37e86 | 2020-04-07 15:11:41 -0700 | [diff] [blame] | 3959 | int size_only = options ? options->check_size_only : 0; |
| 3960 | int check_binary = options ? options->check_binary : 0; |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 3961 | int err = 0; |
Torsten Bögershausen | 8462ff4 | 2018-01-13 23:49:31 +0100 | [diff] [blame] | 3962 | int conv_flags = global_conv_flags_eol; |
Junio C Hamano | 5430bb2 | 2013-06-24 14:35:04 -0700 | [diff] [blame] | 3963 | /* |
| 3964 | * demote FAIL to WARN to allow inspecting the situation |
| 3965 | * instead of refusing. |
| 3966 | */ |
Torsten Bögershausen | 8462ff4 | 2018-01-13 23:49:31 +0100 | [diff] [blame] | 3967 | if (conv_flags & CONV_EOL_RNDTRP_DIE) |
| 3968 | conv_flags = CONV_EOL_RNDTRP_WARN; |
Junio C Hamano | 5430bb2 | 2013-06-24 14:35:04 -0700 | [diff] [blame] | 3969 | |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 3970 | if (!DIFF_FILE_VALID(s)) |
| 3971 | die("internal error: asking to populate invalid file."); |
| 3972 | if (S_ISDIR(s->mode)) |
| 3973 | return -1; |
| 3974 | |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 3975 | if (s->data) |
Junio C Hamano | fc3abdf | 2007-05-03 13:05:48 -0700 | [diff] [blame] | 3976 | return 0; |
Linus Torvalds | 0478675 | 2007-04-15 11:14:28 -0700 | [diff] [blame] | 3977 | |
Junio C Hamano | 6e0b8ed | 2007-05-07 01:14:21 -0700 | [diff] [blame] | 3978 | if (size_only && 0 < s->size) |
| 3979 | return 0; |
| 3980 | |
Martin Waitz | 302b928 | 2007-05-21 22:08:28 +0200 | [diff] [blame] | 3981 | if (S_ISGITLINK(s->mode)) |
Linus Torvalds | 0478675 | 2007-04-15 11:14:28 -0700 | [diff] [blame] | 3982 | return diff_populate_gitlink(s, size_only); |
| 3983 | |
brian m. carlson | 41c9560 | 2016-06-24 23:09:24 +0000 | [diff] [blame] | 3984 | if (!s->oid_valid || |
Nguyễn Thái Ngọc Duy | b78ea5f | 2018-09-21 17:57:19 +0200 | [diff] [blame] | 3985 | reuse_worktree_file(r->index, s->path, &s->oid, 0)) { |
Brandon Casey | f285a2d | 2008-10-09 14:12:12 -0500 | [diff] [blame] | 3986 | struct strbuf buf = STRBUF_INIT; |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 3987 | struct stat st; |
| 3988 | int fd; |
Linus Torvalds | 6c510be | 2007-02-13 11:07:23 -0800 | [diff] [blame] | 3989 | |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 3990 | if (lstat(s->path, &st) < 0) { |
Andrey Okoshkin | 10e0ca8 | 2017-10-27 12:33:25 +0300 | [diff] [blame] | 3991 | err_empty: |
| 3992 | err = -1; |
| 3993 | empty: |
| 3994 | s->data = (char *)""; |
| 3995 | s->size = 0; |
| 3996 | return err; |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 3997 | } |
Shawn O. Pearce | dc49cd7 | 2007-03-06 20:44:37 -0500 | [diff] [blame] | 3998 | s->size = xsize_t(st.st_size); |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 3999 | if (!s->size) |
| 4000 | goto empty; |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 4001 | if (S_ISLNK(st.st_mode)) { |
Linus Torvalds | cf219d8 | 2008-12-17 10:26:13 -0800 | [diff] [blame] | 4002 | struct strbuf sb = STRBUF_INIT; |
| 4003 | |
| 4004 | if (strbuf_readlink(&sb, s->path, s->size)) |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 4005 | goto err_empty; |
René Scharfe | 0956a6d | 2008-12-18 17:56:51 +0100 | [diff] [blame] | 4006 | s->size = sb.len; |
| 4007 | s->data = strbuf_detach(&sb, NULL); |
Linus Torvalds | cf219d8 | 2008-12-17 10:26:13 -0800 | [diff] [blame] | 4008 | s->should_free = 1; |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 4009 | return 0; |
| 4010 | } |
Junio C Hamano | 12426e1 | 2017-03-01 18:04:44 +0100 | [diff] [blame] | 4011 | |
| 4012 | /* |
| 4013 | * Even if the caller would be happy with getting |
| 4014 | * only the size, we cannot return early at this |
| 4015 | * point if the path requires us to run the content |
| 4016 | * conversion. |
| 4017 | */ |
Nguyễn Thái Ngọc Duy | b78ea5f | 2018-09-21 17:57:19 +0200 | [diff] [blame] | 4018 | if (size_only && !would_convert_to_git(r->index, s->path)) |
Linus Torvalds | cf219d8 | 2008-12-17 10:26:13 -0800 | [diff] [blame] | 4019 | return 0; |
Junio C Hamano | 12426e1 | 2017-03-01 18:04:44 +0100 | [diff] [blame] | 4020 | |
| 4021 | /* |
| 4022 | * Note: this check uses xsize_t(st.st_size) that may |
| 4023 | * not be the true size of the blob after it goes |
| 4024 | * through convert_to_git(). This may not strictly be |
| 4025 | * correct, but the whole point of big_file_threshold |
| 4026 | * and is_binary check being that we want to avoid |
| 4027 | * opening the file and inspecting the contents, this |
| 4028 | * is probably fine. |
| 4029 | */ |
Jonathan Tan | 1c37e86 | 2020-04-07 15:11:41 -0700 | [diff] [blame] | 4030 | if (check_binary && |
Nguyễn Thái Ngọc Duy | 6bf3b81 | 2014-08-16 10:08:05 +0700 | [diff] [blame] | 4031 | s->size > big_file_threshold && s->is_binary == -1) { |
| 4032 | s->is_binary = 1; |
| 4033 | return 0; |
| 4034 | } |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 4035 | fd = open(s->path, O_RDONLY); |
| 4036 | if (fd < 0) |
| 4037 | goto err_empty; |
Shawn O. Pearce | c4712e4 | 2006-12-24 00:47:23 -0500 | [diff] [blame] | 4038 | s->data = xmmap(NULL, s->size, PROT_READ, MAP_PRIVATE, fd, 0); |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 4039 | close(fd); |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 4040 | s->should_munmap = 1; |
Linus Torvalds | 6c510be | 2007-02-13 11:07:23 -0800 | [diff] [blame] | 4041 | |
| 4042 | /* |
| 4043 | * Convert from working tree format to canonical git format |
| 4044 | */ |
Nguyễn Thái Ngọc Duy | b78ea5f | 2018-09-21 17:57:19 +0200 | [diff] [blame] | 4045 | if (convert_to_git(r->index, s->path, s->data, s->size, &buf, conv_flags)) { |
René Scharfe | c32f749 | 2007-10-21 11:23:49 +0200 | [diff] [blame] | 4046 | size_t size = 0; |
Linus Torvalds | 6c510be | 2007-02-13 11:07:23 -0800 | [diff] [blame] | 4047 | munmap(s->data, s->size); |
| 4048 | s->should_munmap = 0; |
René Scharfe | c32f749 | 2007-10-21 11:23:49 +0200 | [diff] [blame] | 4049 | s->data = strbuf_detach(&buf, &size); |
| 4050 | s->size = size; |
Linus Torvalds | 6c510be | 2007-02-13 11:07:23 -0800 | [diff] [blame] | 4051 | s->should_free = 1; |
| 4052 | } |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 4053 | } |
| 4054 | else { |
Jonathan Tan | c14b6f8 | 2020-04-07 15:11:42 -0700 | [diff] [blame] | 4055 | struct object_info info = { |
| 4056 | .sizep = &s->size |
| 4057 | }; |
| 4058 | |
| 4059 | if (!(size_only || check_binary)) |
| 4060 | /* |
| 4061 | * Set contentp, since there is no chance that merely |
| 4062 | * the size is sufficient. |
| 4063 | */ |
| 4064 | info.contentp = &s->data; |
| 4065 | |
Jonathan Tan | 95acf11 | 2020-04-07 15:11:43 -0700 | [diff] [blame] | 4066 | if (options && options->missing_object_cb) { |
| 4067 | if (!oid_object_info_extended(r, &s->oid, &info, |
| 4068 | OBJECT_INFO_LOOKUP_REPLACE | |
| 4069 | OBJECT_INFO_SKIP_FETCH_OBJECT)) |
| 4070 | goto object_read; |
| 4071 | options->missing_object_cb(options->missing_object_data); |
| 4072 | } |
Jonathan Tan | c14b6f8 | 2020-04-07 15:11:42 -0700 | [diff] [blame] | 4073 | if (oid_object_info_extended(r, &s->oid, &info, |
| 4074 | OBJECT_INFO_LOOKUP_REPLACE)) |
| 4075 | die("unable to read %s", oid_to_hex(&s->oid)); |
| 4076 | |
Jonathan Tan | 95acf11 | 2020-04-07 15:11:43 -0700 | [diff] [blame] | 4077 | object_read: |
Jonathan Tan | 1c37e86 | 2020-04-07 15:11:41 -0700 | [diff] [blame] | 4078 | if (size_only || check_binary) { |
Nguyễn Thái Ngọc Duy | 6bf3b81 | 2014-08-16 10:08:05 +0700 | [diff] [blame] | 4079 | if (size_only) |
| 4080 | return 0; |
| 4081 | if (s->size > big_file_threshold && s->is_binary == -1) { |
| 4082 | s->is_binary = 1; |
| 4083 | return 0; |
| 4084 | } |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 4085 | } |
Jonathan Tan | c14b6f8 | 2020-04-07 15:11:42 -0700 | [diff] [blame] | 4086 | if (!info.contentp) { |
| 4087 | info.contentp = &s->data; |
| 4088 | if (oid_object_info_extended(r, &s->oid, &info, |
| 4089 | OBJECT_INFO_LOOKUP_REPLACE)) |
| 4090 | die("unable to read %s", oid_to_hex(&s->oid)); |
| 4091 | } |
Nguyễn Thái Ngọc Duy | 6bf3b81 | 2014-08-16 10:08:05 +0700 | [diff] [blame] | 4092 | s->should_free = 1; |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 4093 | } |
| 4094 | return 0; |
| 4095 | } |
| 4096 | |
Junio C Hamano | 8ae92e6 | 2007-10-02 21:01:03 -0700 | [diff] [blame] | 4097 | void diff_free_filespec_blob(struct diff_filespec *s) |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 4098 | { |
| 4099 | if (s->should_free) |
| 4100 | free(s->data); |
| 4101 | else if (s->should_munmap) |
| 4102 | munmap(s->data, s->size); |
Junio C Hamano | fc3abdf | 2007-05-03 13:05:48 -0700 | [diff] [blame] | 4103 | |
| 4104 | if (s->should_free || s->should_munmap) { |
| 4105 | s->should_free = s->should_munmap = 0; |
| 4106 | s->data = NULL; |
| 4107 | } |
Jeff King | eede7b7 | 2007-09-25 15:29:42 -0400 | [diff] [blame] | 4108 | } |
| 4109 | |
| 4110 | void diff_free_filespec_data(struct diff_filespec *s) |
| 4111 | { |
Junio C Hamano | 8ae92e6 | 2007-10-02 21:01:03 -0700 | [diff] [blame] | 4112 | diff_free_filespec_blob(s); |
Ævar Arnfjörð Bjarmason | 6a83d90 | 2017-06-15 23:15:46 +0000 | [diff] [blame] | 4113 | FREE_AND_NULL(s->cnt_data); |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 4114 | } |
| 4115 | |
Nguyễn Thái Ngọc Duy | b78ea5f | 2018-09-21 17:57:19 +0200 | [diff] [blame] | 4116 | static void prep_temp_blob(struct index_state *istate, |
| 4117 | const char *path, struct diff_tempfile *temp, |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 4118 | void *blob, |
| 4119 | unsigned long size, |
brian m. carlson | 09bdff2 | 2016-06-24 23:09:29 +0000 | [diff] [blame] | 4120 | const struct object_id *oid, |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 4121 | int mode) |
| 4122 | { |
Johannes Schindelin | 4e218f5 | 2009-03-21 12:42:52 +0100 | [diff] [blame] | 4123 | struct strbuf buf = STRBUF_INIT; |
Brandon Williams | c2a46a7 | 2018-02-14 10:59:54 -0800 | [diff] [blame] | 4124 | struct strbuf tempfile = STRBUF_INIT; |
David Aguilar | 003b33a | 2009-05-31 01:35:52 -0700 | [diff] [blame] | 4125 | char *path_dup = xstrdup(path); |
| 4126 | const char *base = basename(path_dup); |
brian m. carlson | c397aac | 2020-03-16 18:05:03 +0000 | [diff] [blame] | 4127 | struct checkout_metadata meta; |
| 4128 | |
| 4129 | init_checkout_metadata(&meta, NULL, NULL, oid); |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 4130 | |
David Aguilar | 003b33a | 2009-05-31 01:35:52 -0700 | [diff] [blame] | 4131 | /* Generate "XXXXXX_basename.ext" */ |
Brandon Williams | c2a46a7 | 2018-02-14 10:59:54 -0800 | [diff] [blame] | 4132 | strbuf_addstr(&tempfile, "XXXXXX_"); |
| 4133 | strbuf_addstr(&tempfile, base); |
David Aguilar | 003b33a | 2009-05-31 01:35:52 -0700 | [diff] [blame] | 4134 | |
Brandon Williams | c2a46a7 | 2018-02-14 10:59:54 -0800 | [diff] [blame] | 4135 | temp->tempfile = mks_tempfile_ts(tempfile.buf, strlen(base) + 1); |
Jeff King | 076aa2c | 2017-09-05 08:15:08 -0400 | [diff] [blame] | 4136 | if (!temp->tempfile) |
Thomas Rast | d824cbb | 2009-06-27 17:58:46 +0200 | [diff] [blame] | 4137 | die_errno("unable to create temp-file"); |
Nguyễn Thái Ngọc Duy | b78ea5f | 2018-09-21 17:57:19 +0200 | [diff] [blame] | 4138 | if (convert_to_working_tree(istate, path, |
brian m. carlson | c397aac | 2020-03-16 18:05:03 +0000 | [diff] [blame] | 4139 | (const char *)blob, (size_t)size, &buf, &meta)) { |
Johannes Schindelin | 4e218f5 | 2009-03-21 12:42:52 +0100 | [diff] [blame] | 4140 | blob = buf.buf; |
| 4141 | size = buf.len; |
| 4142 | } |
Junio C Hamano | c50424a | 2017-09-25 15:24:06 +0900 | [diff] [blame] | 4143 | if (write_in_full(temp->tempfile->fd, blob, size) < 0 || |
Jeff King | 076aa2c | 2017-09-05 08:15:08 -0400 | [diff] [blame] | 4144 | close_tempfile_gently(temp->tempfile)) |
Thomas Rast | 0721c31 | 2009-06-27 17:58:47 +0200 | [diff] [blame] | 4145 | die_errno("unable to write temp-file"); |
Jeff King | 076aa2c | 2017-09-05 08:15:08 -0400 | [diff] [blame] | 4146 | temp->name = get_tempfile_path(temp->tempfile); |
brian m. carlson | 09bdff2 | 2016-06-24 23:09:29 +0000 | [diff] [blame] | 4147 | oid_to_hex_r(temp->hex, oid); |
Jeff King | 5096d49 | 2015-09-24 17:06:08 -0400 | [diff] [blame] | 4148 | xsnprintf(temp->mode, sizeof(temp->mode), "%06o", mode); |
Johannes Schindelin | 4e218f5 | 2009-03-21 12:42:52 +0100 | [diff] [blame] | 4149 | strbuf_release(&buf); |
Brandon Williams | c2a46a7 | 2018-02-14 10:59:54 -0800 | [diff] [blame] | 4150 | strbuf_release(&tempfile); |
David Aguilar | 003b33a | 2009-05-31 01:35:52 -0700 | [diff] [blame] | 4151 | free(path_dup); |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 4152 | } |
| 4153 | |
Nguyễn Thái Ngọc Duy | b78ea5f | 2018-09-21 17:57:19 +0200 | [diff] [blame] | 4154 | static struct diff_tempfile *prepare_temp_file(struct repository *r, |
| 4155 | const char *name, |
| 4156 | struct diff_filespec *one) |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 4157 | { |
Jeff King | 479b0ae | 2009-01-22 00:59:56 -0500 | [diff] [blame] | 4158 | struct diff_tempfile *temp = claim_diff_tempfile(); |
| 4159 | |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 4160 | if (!DIFF_FILE_VALID(one)) { |
| 4161 | not_a_valid_file: |
| 4162 | /* A '-' entry produces this for file-2, and |
| 4163 | * a '+' entry produces this for file-1. |
| 4164 | */ |
| 4165 | temp->name = "/dev/null"; |
Jeff King | 5096d49 | 2015-09-24 17:06:08 -0400 | [diff] [blame] | 4166 | xsnprintf(temp->hex, sizeof(temp->hex), "."); |
| 4167 | xsnprintf(temp->mode, sizeof(temp->mode), "."); |
Jeff King | 479b0ae | 2009-01-22 00:59:56 -0500 | [diff] [blame] | 4168 | return temp; |
| 4169 | } |
| 4170 | |
Thomas Rast | aba4727 | 2014-02-16 17:52:34 +0100 | [diff] [blame] | 4171 | if (!S_ISGITLINK(one->mode) && |
brian m. carlson | 41c9560 | 2016-06-24 23:09:24 +0000 | [diff] [blame] | 4172 | (!one->oid_valid || |
Nguyễn Thái Ngọc Duy | b78ea5f | 2018-09-21 17:57:19 +0200 | [diff] [blame] | 4173 | reuse_worktree_file(r->index, name, &one->oid, 1))) { |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 4174 | struct stat st; |
| 4175 | if (lstat(name, &st) < 0) { |
| 4176 | if (errno == ENOENT) |
| 4177 | goto not_a_valid_file; |
Thomas Rast | d824cbb | 2009-06-27 17:58:46 +0200 | [diff] [blame] | 4178 | die_errno("stat(%s)", name); |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 4179 | } |
| 4180 | if (S_ISLNK(st.st_mode)) { |
Jeff King | 3cd7388 | 2009-05-25 06:46:09 -0400 | [diff] [blame] | 4181 | struct strbuf sb = STRBUF_INIT; |
| 4182 | if (strbuf_readlink(&sb, name, st.st_size) < 0) |
Thomas Rast | 0721c31 | 2009-06-27 17:58:47 +0200 | [diff] [blame] | 4183 | die_errno("readlink(%s)", name); |
Nguyễn Thái Ngọc Duy | b78ea5f | 2018-09-21 17:57:19 +0200 | [diff] [blame] | 4184 | prep_temp_blob(r->index, name, temp, sb.buf, sb.len, |
brian m. carlson | 41c9560 | 2016-06-24 23:09:24 +0000 | [diff] [blame] | 4185 | (one->oid_valid ? |
brian m. carlson | 09bdff2 | 2016-06-24 23:09:29 +0000 | [diff] [blame] | 4186 | &one->oid : &null_oid), |
brian m. carlson | 41c9560 | 2016-06-24 23:09:24 +0000 | [diff] [blame] | 4187 | (one->oid_valid ? |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 4188 | one->mode : S_IFLNK)); |
Jeff King | 3cd7388 | 2009-05-25 06:46:09 -0400 | [diff] [blame] | 4189 | strbuf_release(&sb); |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 4190 | } |
| 4191 | else { |
| 4192 | /* we can borrow from the file in the work tree */ |
| 4193 | temp->name = name; |
brian m. carlson | 41c9560 | 2016-06-24 23:09:24 +0000 | [diff] [blame] | 4194 | if (!one->oid_valid) |
Brandon Williams | 7401415 | 2017-05-30 10:30:52 -0700 | [diff] [blame] | 4195 | oid_to_hex_r(temp->hex, &null_oid); |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 4196 | else |
René Scharfe | 2490574 | 2017-01-28 23:03:03 +0100 | [diff] [blame] | 4197 | oid_to_hex_r(temp->hex, &one->oid); |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 4198 | /* Even though we may sometimes borrow the |
| 4199 | * contents from the work tree, we always want |
| 4200 | * one->mode. mode is trustworthy even when |
Brandon Williams | 7401415 | 2017-05-30 10:30:52 -0700 | [diff] [blame] | 4201 | * !(one->oid_valid), as long as |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 4202 | * DIFF_FILE_VALID(one). |
| 4203 | */ |
Jeff King | 5096d49 | 2015-09-24 17:06:08 -0400 | [diff] [blame] | 4204 | xsnprintf(temp->mode, sizeof(temp->mode), "%06o", one->mode); |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 4205 | } |
Jeff King | 479b0ae | 2009-01-22 00:59:56 -0500 | [diff] [blame] | 4206 | return temp; |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 4207 | } |
| 4208 | else { |
Jonathan Tan | 1c37e86 | 2020-04-07 15:11:41 -0700 | [diff] [blame] | 4209 | if (diff_populate_filespec(r, one, NULL)) |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 4210 | die("cannot read data blob for %s", one->path); |
Nguyễn Thái Ngọc Duy | b78ea5f | 2018-09-21 17:57:19 +0200 | [diff] [blame] | 4211 | prep_temp_blob(r->index, name, temp, |
| 4212 | one->data, one->size, |
brian m. carlson | 09bdff2 | 2016-06-24 23:09:29 +0000 | [diff] [blame] | 4213 | &one->oid, one->mode); |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 4214 | } |
Jeff King | 479b0ae | 2009-01-22 00:59:56 -0500 | [diff] [blame] | 4215 | return temp; |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 4216 | } |
| 4217 | |
Nguyễn Thái Ngọc Duy | b78ea5f | 2018-09-21 17:57:19 +0200 | [diff] [blame] | 4218 | static void add_external_diff_name(struct repository *r, |
Jeff King | ef8d7ac | 2020-07-28 16:24:53 -0400 | [diff] [blame] | 4219 | struct strvec *argv, |
Jeff King | f3efe78 | 2014-04-19 15:22:25 -0400 | [diff] [blame] | 4220 | const char *name, |
| 4221 | struct diff_filespec *df) |
| 4222 | { |
Nguyễn Thái Ngọc Duy | b78ea5f | 2018-09-21 17:57:19 +0200 | [diff] [blame] | 4223 | struct diff_tempfile *temp = prepare_temp_file(r, name, df); |
Jeff King | ef8d7ac | 2020-07-28 16:24:53 -0400 | [diff] [blame] | 4224 | strvec_push(argv, temp->name); |
| 4225 | strvec_push(argv, temp->hex); |
| 4226 | strvec_push(argv, temp->mode); |
Jeff King | f3efe78 | 2014-04-19 15:22:25 -0400 | [diff] [blame] | 4227 | } |
| 4228 | |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 4229 | /* An external diff command takes: |
| 4230 | * |
| 4231 | * diff-cmd name infile1 infile1-sha1 infile1-mode \ |
| 4232 | * infile2 infile2-sha1 infile2-mode [ rename-to ] |
| 4233 | * |
| 4234 | */ |
| 4235 | static void run_external_diff(const char *pgm, |
| 4236 | const char *name, |
| 4237 | const char *other, |
| 4238 | struct diff_filespec *one, |
| 4239 | struct diff_filespec *two, |
| 4240 | const char *xfrm_msg, |
Zoltan Klinger | ee7fb0b | 2013-12-06 10:38:46 +1100 | [diff] [blame] | 4241 | struct diff_options *o) |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 4242 | { |
Jeff King | ef8d7ac | 2020-07-28 16:24:53 -0400 | [diff] [blame] | 4243 | struct strvec argv = STRVEC_INIT; |
| 4244 | struct strvec env = STRVEC_INIT; |
Zoltan Klinger | ee7fb0b | 2013-12-06 10:38:46 +1100 | [diff] [blame] | 4245 | struct diff_queue_struct *q = &diff_queued_diff; |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 4246 | |
Jeff King | ef8d7ac | 2020-07-28 16:24:53 -0400 | [diff] [blame] | 4247 | strvec_push(&argv, pgm); |
| 4248 | strvec_push(&argv, name); |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 4249 | |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 4250 | if (one && two) { |
Nguyễn Thái Ngọc Duy | b78ea5f | 2018-09-21 17:57:19 +0200 | [diff] [blame] | 4251 | add_external_diff_name(o->repo, &argv, name, one); |
Jeff King | f3efe78 | 2014-04-19 15:22:25 -0400 | [diff] [blame] | 4252 | if (!other) |
Nguyễn Thái Ngọc Duy | b78ea5f | 2018-09-21 17:57:19 +0200 | [diff] [blame] | 4253 | add_external_diff_name(o->repo, &argv, name, two); |
Jeff King | f3efe78 | 2014-04-19 15:22:25 -0400 | [diff] [blame] | 4254 | else { |
Nguyễn Thái Ngọc Duy | b78ea5f | 2018-09-21 17:57:19 +0200 | [diff] [blame] | 4255 | add_external_diff_name(o->repo, &argv, other, two); |
Jeff King | ef8d7ac | 2020-07-28 16:24:53 -0400 | [diff] [blame] | 4256 | strvec_push(&argv, other); |
| 4257 | strvec_push(&argv, xfrm_msg); |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 4258 | } |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 4259 | } |
Zoltan Klinger | ee7fb0b | 2013-12-06 10:38:46 +1100 | [diff] [blame] | 4260 | |
Jeff King | ef8d7ac | 2020-07-28 16:24:53 -0400 | [diff] [blame] | 4261 | strvec_pushf(&env, "GIT_DIFF_PATH_COUNTER=%d", ++o->diff_path_counter); |
| 4262 | strvec_pushf(&env, "GIT_DIFF_PATH_TOTAL=%d", q->nr); |
Zoltan Klinger | ee7fb0b | 2013-12-06 10:38:46 +1100 | [diff] [blame] | 4263 | |
Johannes Schindelin | 3aef54e | 2019-07-11 01:23:41 -0700 | [diff] [blame] | 4264 | diff_free_filespec_data(one); |
| 4265 | diff_free_filespec_data(two); |
Jeff King | d70a9eb | 2020-07-28 20:37:20 -0400 | [diff] [blame] | 4266 | if (run_command_v_opt_cd_env(argv.v, RUN_USING_SHELL, NULL, env.v)) |
Jeff King | 89294d1 | 2014-04-19 15:19:19 -0400 | [diff] [blame] | 4267 | die(_("external diff died, stopping at %s"), name); |
| 4268 | |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 4269 | remove_tempfile(); |
Jeff King | ef8d7ac | 2020-07-28 16:24:53 -0400 | [diff] [blame] | 4270 | strvec_clear(&argv); |
| 4271 | strvec_clear(&env); |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 4272 | } |
| 4273 | |
Junio C Hamano | b67b961 | 2009-01-26 00:08:24 -0800 | [diff] [blame] | 4274 | static int similarity_index(struct diff_filepair *p) |
| 4275 | { |
| 4276 | return p->score * 100 / MAX_SCORE; |
| 4277 | } |
| 4278 | |
Jeff King | 4f03666 | 2016-10-20 02:21:25 -0400 | [diff] [blame] | 4279 | static const char *diff_abbrev_oid(const struct object_id *oid, int abbrev) |
| 4280 | { |
| 4281 | if (startup_info->have_repository) |
brian m. carlson | aab9583 | 2018-03-12 02:27:30 +0000 | [diff] [blame] | 4282 | return find_unique_abbrev(oid, abbrev); |
Jeff King | 4f03666 | 2016-10-20 02:21:25 -0400 | [diff] [blame] | 4283 | else { |
| 4284 | char *hex = oid_to_hex(oid); |
Junio C Hamano | 0d9c527 | 2016-10-27 14:58:48 -0700 | [diff] [blame] | 4285 | if (abbrev < 0) |
| 4286 | abbrev = FALLBACK_DEFAULT_ABBREV; |
brian m. carlson | 02afca1 | 2018-07-16 01:28:05 +0000 | [diff] [blame] | 4287 | if (abbrev > the_hash_algo->hexsz) |
Johannes Schindelin | 033abf9 | 2018-05-02 11:38:39 +0200 | [diff] [blame] | 4288 | BUG("oid abbreviation out of range: %d", abbrev); |
Jack Bates | 43d1948 | 2016-12-06 09:56:14 -0700 | [diff] [blame] | 4289 | if (abbrev) |
| 4290 | hex[abbrev] = '\0'; |
Jeff King | 4f03666 | 2016-10-20 02:21:25 -0400 | [diff] [blame] | 4291 | return hex; |
| 4292 | } |
| 4293 | } |
| 4294 | |
Junio C Hamano | b67b961 | 2009-01-26 00:08:24 -0800 | [diff] [blame] | 4295 | static void fill_metainfo(struct strbuf *msg, |
| 4296 | const char *name, |
| 4297 | const char *other, |
| 4298 | struct diff_filespec *one, |
| 4299 | struct diff_filespec *two, |
| 4300 | struct diff_options *o, |
Bert Wesarg | 3746644 | 2010-05-04 00:38:07 +0200 | [diff] [blame] | 4301 | struct diff_filepair *p, |
Junio C Hamano | 5977744 | 2010-06-18 11:16:57 -0700 | [diff] [blame] | 4302 | int *must_show_header, |
Bert Wesarg | 3746644 | 2010-05-04 00:38:07 +0200 | [diff] [blame] | 4303 | int use_color) |
Junio C Hamano | b67b961 | 2009-01-26 00:08:24 -0800 | [diff] [blame] | 4304 | { |
Bert Wesarg | 3746644 | 2010-05-04 00:38:07 +0200 | [diff] [blame] | 4305 | const char *set = diff_get_color(use_color, DIFF_METAINFO); |
| 4306 | const char *reset = diff_get_color(use_color, DIFF_RESET); |
John Keeping | 30997bb | 2013-02-07 20:15:27 +0000 | [diff] [blame] | 4307 | const char *line_prefix = diff_line_prefix(o); |
Bo Yang | 7be5761 | 2010-05-26 15:23:54 +0800 | [diff] [blame] | 4308 | |
Christian Couder | 296c6bb | 2010-05-26 04:50:12 +0200 | [diff] [blame] | 4309 | *must_show_header = 1; |
Junio C Hamano | b67b961 | 2009-01-26 00:08:24 -0800 | [diff] [blame] | 4310 | strbuf_init(msg, PATH_MAX * 2 + 300); |
| 4311 | switch (p->status) { |
| 4312 | case DIFF_STATUS_COPIED: |
Junio C Hamano | 98ad90f | 2010-06-18 11:16:57 -0700 | [diff] [blame] | 4313 | strbuf_addf(msg, "%s%ssimilarity index %d%%", |
| 4314 | line_prefix, set, similarity_index(p)); |
| 4315 | strbuf_addf(msg, "%s\n%s%scopy from ", |
| 4316 | reset, line_prefix, set); |
Junio C Hamano | b67b961 | 2009-01-26 00:08:24 -0800 | [diff] [blame] | 4317 | quote_c_style(name, msg, NULL, 0); |
Junio C Hamano | 98ad90f | 2010-06-18 11:16:57 -0700 | [diff] [blame] | 4318 | strbuf_addf(msg, "%s\n%s%scopy to ", reset, line_prefix, set); |
Junio C Hamano | b67b961 | 2009-01-26 00:08:24 -0800 | [diff] [blame] | 4319 | quote_c_style(other, msg, NULL, 0); |
Bert Wesarg | 3746644 | 2010-05-04 00:38:07 +0200 | [diff] [blame] | 4320 | strbuf_addf(msg, "%s\n", reset); |
Junio C Hamano | b67b961 | 2009-01-26 00:08:24 -0800 | [diff] [blame] | 4321 | break; |
| 4322 | case DIFF_STATUS_RENAMED: |
Junio C Hamano | 98ad90f | 2010-06-18 11:16:57 -0700 | [diff] [blame] | 4323 | strbuf_addf(msg, "%s%ssimilarity index %d%%", |
| 4324 | line_prefix, set, similarity_index(p)); |
| 4325 | strbuf_addf(msg, "%s\n%s%srename from ", |
| 4326 | reset, line_prefix, set); |
Junio C Hamano | b67b961 | 2009-01-26 00:08:24 -0800 | [diff] [blame] | 4327 | quote_c_style(name, msg, NULL, 0); |
Junio C Hamano | 98ad90f | 2010-06-18 11:16:57 -0700 | [diff] [blame] | 4328 | strbuf_addf(msg, "%s\n%s%srename to ", |
| 4329 | reset, line_prefix, set); |
Junio C Hamano | b67b961 | 2009-01-26 00:08:24 -0800 | [diff] [blame] | 4330 | quote_c_style(other, msg, NULL, 0); |
Bert Wesarg | 3746644 | 2010-05-04 00:38:07 +0200 | [diff] [blame] | 4331 | strbuf_addf(msg, "%s\n", reset); |
Junio C Hamano | b67b961 | 2009-01-26 00:08:24 -0800 | [diff] [blame] | 4332 | break; |
| 4333 | case DIFF_STATUS_MODIFIED: |
| 4334 | if (p->score) { |
Junio C Hamano | 98ad90f | 2010-06-18 11:16:57 -0700 | [diff] [blame] | 4335 | strbuf_addf(msg, "%s%sdissimilarity index %d%%%s\n", |
| 4336 | line_prefix, |
Bert Wesarg | 3746644 | 2010-05-04 00:38:07 +0200 | [diff] [blame] | 4337 | set, similarity_index(p), reset); |
Junio C Hamano | b67b961 | 2009-01-26 00:08:24 -0800 | [diff] [blame] | 4338 | break; |
| 4339 | } |
| 4340 | /* fallthru */ |
| 4341 | default: |
Christian Couder | 296c6bb | 2010-05-26 04:50:12 +0200 | [diff] [blame] | 4342 | *must_show_header = 0; |
Junio C Hamano | b67b961 | 2009-01-26 00:08:24 -0800 | [diff] [blame] | 4343 | } |
Jeff King | 9001dc2 | 2018-08-28 17:22:48 -0400 | [diff] [blame] | 4344 | if (one && two && !oideq(&one->oid, &two->oid)) { |
brian m. carlson | de1d81d | 2018-05-02 00:25:52 +0000 | [diff] [blame] | 4345 | const unsigned hexsz = the_hash_algo->hexsz; |
Đoàn Trần Công Danh | 3046c7f | 2020-08-21 18:51:47 +0700 | [diff] [blame] | 4346 | int abbrev = o->abbrev ? o->abbrev : DEFAULT_ABBREV; |
| 4347 | |
| 4348 | if (o->flags.full_index) |
| 4349 | abbrev = hexsz; |
Junio C Hamano | b67b961 | 2009-01-26 00:08:24 -0800 | [diff] [blame] | 4350 | |
Brandon Williams | 0d1e0e7 | 2017-10-31 11:19:11 -0700 | [diff] [blame] | 4351 | if (o->flags.binary) { |
Junio C Hamano | b67b961 | 2009-01-26 00:08:24 -0800 | [diff] [blame] | 4352 | mmfile_t mf; |
Nguyễn Thái Ngọc Duy | b78ea5f | 2018-09-21 17:57:19 +0200 | [diff] [blame] | 4353 | if ((!fill_mmfile(o->repo, &mf, one) && |
| 4354 | diff_filespec_is_binary(o->repo, one)) || |
| 4355 | (!fill_mmfile(o->repo, &mf, two) && |
| 4356 | diff_filespec_is_binary(o->repo, two))) |
brian m. carlson | de1d81d | 2018-05-02 00:25:52 +0000 | [diff] [blame] | 4357 | abbrev = hexsz; |
Junio C Hamano | b67b961 | 2009-01-26 00:08:24 -0800 | [diff] [blame] | 4358 | } |
Jeff King | 4f03666 | 2016-10-20 02:21:25 -0400 | [diff] [blame] | 4359 | strbuf_addf(msg, "%s%sindex %s..%s", line_prefix, set, |
| 4360 | diff_abbrev_oid(&one->oid, abbrev), |
| 4361 | diff_abbrev_oid(&two->oid, abbrev)); |
Junio C Hamano | b67b961 | 2009-01-26 00:08:24 -0800 | [diff] [blame] | 4362 | if (one->mode == two->mode) |
| 4363 | strbuf_addf(msg, " %06o", one->mode); |
Bert Wesarg | 3746644 | 2010-05-04 00:38:07 +0200 | [diff] [blame] | 4364 | strbuf_addf(msg, "%s\n", reset); |
Junio C Hamano | b67b961 | 2009-01-26 00:08:24 -0800 | [diff] [blame] | 4365 | } |
Junio C Hamano | b67b961 | 2009-01-26 00:08:24 -0800 | [diff] [blame] | 4366 | } |
| 4367 | |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 4368 | static void run_diff_cmd(const char *pgm, |
| 4369 | const char *name, |
| 4370 | const char *other, |
Junio C Hamano | cd676a5 | 2008-02-12 14:26:02 -0800 | [diff] [blame] | 4371 | const char *attr_path, |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 4372 | struct diff_filespec *one, |
| 4373 | struct diff_filespec *two, |
Junio C Hamano | b67b961 | 2009-01-26 00:08:24 -0800 | [diff] [blame] | 4374 | struct strbuf *msg, |
Junio C Hamano | 051308f | 2006-05-04 16:51:44 -0700 | [diff] [blame] | 4375 | struct diff_options *o, |
Junio C Hamano | b67b961 | 2009-01-26 00:08:24 -0800 | [diff] [blame] | 4376 | struct diff_filepair *p) |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 4377 | { |
Junio C Hamano | b67b961 | 2009-01-26 00:08:24 -0800 | [diff] [blame] | 4378 | const char *xfrm_msg = NULL; |
| 4379 | int complete_rewrite = (p->status == DIFF_STATUS_MODIFIED) && p->score; |
Christian Couder | 296c6bb | 2010-05-26 04:50:12 +0200 | [diff] [blame] | 4380 | int must_show_header = 0; |
Junio C Hamano | b67b961 | 2009-01-26 00:08:24 -0800 | [diff] [blame] | 4381 | |
Junio C Hamano | bd8c1a9 | 2012-07-17 22:08:59 -0700 | [diff] [blame] | 4382 | |
Brandon Williams | 0d1e0e7 | 2017-10-31 11:19:11 -0700 | [diff] [blame] | 4383 | if (o->flags.allow_external) { |
Nguyễn Thái Ngọc Duy | acd00ea | 2018-09-21 17:57:33 +0200 | [diff] [blame] | 4384 | struct userdiff_driver *drv; |
| 4385 | |
| 4386 | drv = userdiff_find_by_path(o->repo->index, attr_path); |
Jeff King | be58e70 | 2008-10-05 17:43:21 -0400 | [diff] [blame] | 4387 | if (drv && drv->external) |
| 4388 | pgm = drv->external; |
Junio C Hamano | f1af60b | 2007-04-22 17:52:55 -0700 | [diff] [blame] | 4389 | } |
| 4390 | |
Bert Wesarg | 3746644 | 2010-05-04 00:38:07 +0200 | [diff] [blame] | 4391 | if (msg) { |
| 4392 | /* |
| 4393 | * don't use colors when the header is intended for an |
| 4394 | * external diff driver |
| 4395 | */ |
| 4396 | fill_metainfo(msg, name, other, one, two, o, p, |
Junio C Hamano | 5977744 | 2010-06-18 11:16:57 -0700 | [diff] [blame] | 4397 | &must_show_header, |
Jeff King | daa0c3d | 2011-08-17 22:04:23 -0700 | [diff] [blame] | 4398 | want_color(o->use_color) && !pgm); |
Bert Wesarg | 3746644 | 2010-05-04 00:38:07 +0200 | [diff] [blame] | 4399 | xfrm_msg = msg->len ? msg->buf : NULL; |
| 4400 | } |
| 4401 | |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 4402 | if (pgm) { |
Jeff King | 4bc1792 | 2019-02-14 00:49:42 -0500 | [diff] [blame] | 4403 | run_external_diff(pgm, name, other, one, two, xfrm_msg, o); |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 4404 | return; |
| 4405 | } |
| 4406 | if (one && two) |
| 4407 | builtin_diff(name, other ? other : name, |
Christian Couder | 296c6bb | 2010-05-26 04:50:12 +0200 | [diff] [blame] | 4408 | one, two, xfrm_msg, must_show_header, |
| 4409 | o, complete_rewrite); |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 4410 | else |
Daniel Barkalow | c0c7773 | 2008-03-09 22:43:39 -0400 | [diff] [blame] | 4411 | fprintf(o->file, "* Unmerged path %s\n", name); |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 4412 | } |
| 4413 | |
Nguyễn Thái Ngọc Duy | 58bf2a4 | 2018-09-21 17:57:31 +0200 | [diff] [blame] | 4414 | static void diff_fill_oid_info(struct diff_filespec *one, struct index_state *istate) |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 4415 | { |
| 4416 | if (DIFF_FILE_VALID(one)) { |
brian m. carlson | 41c9560 | 2016-06-24 23:09:24 +0000 | [diff] [blame] | 4417 | if (!one->oid_valid) { |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 4418 | struct stat st; |
Junio C Hamano | 4682d85 | 2012-06-27 20:14:47 -0700 | [diff] [blame] | 4419 | if (one->is_stdin) { |
brian m. carlson | a0d12c4 | 2016-06-24 23:09:23 +0000 | [diff] [blame] | 4420 | oidclr(&one->oid); |
Johannes Schindelin | 5332b2a | 2007-02-25 23:36:10 +0100 | [diff] [blame] | 4421 | return; |
| 4422 | } |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 4423 | if (lstat(one->path, &st) < 0) |
Thomas Rast | 0721c31 | 2009-06-27 17:58:47 +0200 | [diff] [blame] | 4424 | die_errno("stat '%s'", one->path); |
Nguyễn Thái Ngọc Duy | 58bf2a4 | 2018-09-21 17:57:31 +0200 | [diff] [blame] | 4425 | if (index_path(istate, &one->oid, one->path, &st, 0)) |
Alexander Potashev | d753070 | 2009-01-04 21:38:41 +0300 | [diff] [blame] | 4426 | die("cannot hash %s", one->path); |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 4427 | } |
| 4428 | } |
| 4429 | else |
brian m. carlson | a0d12c4 | 2016-06-24 23:09:23 +0000 | [diff] [blame] | 4430 | oidclr(&one->oid); |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 4431 | } |
| 4432 | |
Junio C Hamano | cd676a5 | 2008-02-12 14:26:02 -0800 | [diff] [blame] | 4433 | static void strip_prefix(int prefix_length, const char **namep, const char **otherp) |
| 4434 | { |
| 4435 | /* Strip the prefix but do not molest /dev/null and absolute paths */ |
Johannes Sixt | ffd04e9 | 2018-10-19 18:58:07 +0200 | [diff] [blame] | 4436 | if (*namep && !is_absolute_path(*namep)) { |
Junio C Hamano | cd676a5 | 2008-02-12 14:26:02 -0800 | [diff] [blame] | 4437 | *namep += prefix_length; |
Jakub Narebski | d8faea9 | 2010-08-09 10:50:53 -0400 | [diff] [blame] | 4438 | if (**namep == '/') |
| 4439 | ++*namep; |
| 4440 | } |
Johannes Sixt | ffd04e9 | 2018-10-19 18:58:07 +0200 | [diff] [blame] | 4441 | if (*otherp && !is_absolute_path(*otherp)) { |
Junio C Hamano | cd676a5 | 2008-02-12 14:26:02 -0800 | [diff] [blame] | 4442 | *otherp += prefix_length; |
Jakub Narebski | d8faea9 | 2010-08-09 10:50:53 -0400 | [diff] [blame] | 4443 | if (**otherp == '/') |
| 4444 | ++*otherp; |
| 4445 | } |
Junio C Hamano | cd676a5 | 2008-02-12 14:26:02 -0800 | [diff] [blame] | 4446 | } |
| 4447 | |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 4448 | static void run_diff(struct diff_filepair *p, struct diff_options *o) |
| 4449 | { |
| 4450 | const char *pgm = external_diff(); |
Pierre Habouzit | 663af34 | 2007-09-20 00:42:15 +0200 | [diff] [blame] | 4451 | struct strbuf msg; |
Pierre Habouzit | 663af34 | 2007-09-20 00:42:15 +0200 | [diff] [blame] | 4452 | struct diff_filespec *one = p->one; |
| 4453 | struct diff_filespec *two = p->two; |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 4454 | const char *name; |
| 4455 | const char *other; |
Junio C Hamano | cd676a5 | 2008-02-12 14:26:02 -0800 | [diff] [blame] | 4456 | const char *attr_path; |
Pierre Habouzit | 663af34 | 2007-09-20 00:42:15 +0200 | [diff] [blame] | 4457 | |
Stefan Beller | f2d2a5d | 2017-06-29 17:06:46 -0700 | [diff] [blame] | 4458 | name = one->path; |
| 4459 | other = (strcmp(name, two->path) ? two->path : NULL); |
Junio C Hamano | cd676a5 | 2008-02-12 14:26:02 -0800 | [diff] [blame] | 4460 | attr_path = name; |
| 4461 | if (o->prefix_length) |
| 4462 | strip_prefix(o->prefix_length, &name, &other); |
| 4463 | |
Brandon Williams | 0d1e0e7 | 2017-10-31 11:19:11 -0700 | [diff] [blame] | 4464 | if (!o->flags.allow_external) |
Junio C Hamano | bd8c1a9 | 2012-07-17 22:08:59 -0700 | [diff] [blame] | 4465 | pgm = NULL; |
| 4466 | |
Junio C Hamano | cd676a5 | 2008-02-12 14:26:02 -0800 | [diff] [blame] | 4467 | if (DIFF_PAIR_UNMERGED(p)) { |
| 4468 | run_diff_cmd(pgm, name, NULL, attr_path, |
Junio C Hamano | b67b961 | 2009-01-26 00:08:24 -0800 | [diff] [blame] | 4469 | NULL, NULL, NULL, o, p); |
Junio C Hamano | cd676a5 | 2008-02-12 14:26:02 -0800 | [diff] [blame] | 4470 | return; |
| 4471 | } |
| 4472 | |
Nguyễn Thái Ngọc Duy | 58bf2a4 | 2018-09-21 17:57:31 +0200 | [diff] [blame] | 4473 | diff_fill_oid_info(one, o->repo->index); |
| 4474 | diff_fill_oid_info(two, o->repo->index); |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 4475 | |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 4476 | if (!pgm && |
| 4477 | DIFF_FILE_VALID(one) && DIFF_FILE_VALID(two) && |
| 4478 | (S_IFMT & one->mode) != (S_IFMT & two->mode)) { |
Junio C Hamano | b67b961 | 2009-01-26 00:08:24 -0800 | [diff] [blame] | 4479 | /* |
| 4480 | * a filepair that changes between file and symlink |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 4481 | * needs to be split into deletion and creation. |
| 4482 | */ |
| 4483 | struct diff_filespec *null = alloc_filespec(two->path); |
Junio C Hamano | cd676a5 | 2008-02-12 14:26:02 -0800 | [diff] [blame] | 4484 | run_diff_cmd(NULL, name, other, attr_path, |
Nguyễn Thái Ngọc Duy | b78ea5f | 2018-09-21 17:57:19 +0200 | [diff] [blame] | 4485 | one, null, &msg, |
| 4486 | o, p); |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 4487 | free(null); |
Junio C Hamano | b67b961 | 2009-01-26 00:08:24 -0800 | [diff] [blame] | 4488 | strbuf_release(&msg); |
| 4489 | |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 4490 | null = alloc_filespec(one->path); |
Junio C Hamano | cd676a5 | 2008-02-12 14:26:02 -0800 | [diff] [blame] | 4491 | run_diff_cmd(NULL, name, other, attr_path, |
Junio C Hamano | b67b961 | 2009-01-26 00:08:24 -0800 | [diff] [blame] | 4492 | null, two, &msg, o, p); |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 4493 | free(null); |
| 4494 | } |
| 4495 | else |
Junio C Hamano | cd676a5 | 2008-02-12 14:26:02 -0800 | [diff] [blame] | 4496 | run_diff_cmd(pgm, name, other, attr_path, |
Junio C Hamano | b67b961 | 2009-01-26 00:08:24 -0800 | [diff] [blame] | 4497 | one, two, &msg, o, p); |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 4498 | |
Pierre Habouzit | 663af34 | 2007-09-20 00:42:15 +0200 | [diff] [blame] | 4499 | strbuf_release(&msg); |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 4500 | } |
| 4501 | |
| 4502 | static void run_diffstat(struct diff_filepair *p, struct diff_options *o, |
| 4503 | struct diffstat_t *diffstat) |
| 4504 | { |
| 4505 | const char *name; |
| 4506 | const char *other; |
| 4507 | |
| 4508 | if (DIFF_PAIR_UNMERGED(p)) { |
| 4509 | /* unmerged */ |
Nguyễn Thái Ngọc Duy | b78ea5f | 2018-09-21 17:57:19 +0200 | [diff] [blame] | 4510 | builtin_diffstat(p->one->path, NULL, NULL, NULL, |
| 4511 | diffstat, o, p); |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 4512 | return; |
| 4513 | } |
| 4514 | |
| 4515 | name = p->one->path; |
| 4516 | other = (strcmp(name, p->two->path) ? p->two->path : NULL); |
| 4517 | |
Junio C Hamano | cd676a5 | 2008-02-12 14:26:02 -0800 | [diff] [blame] | 4518 | if (o->prefix_length) |
| 4519 | strip_prefix(o->prefix_length, &name, &other); |
| 4520 | |
Nguyễn Thái Ngọc Duy | 58bf2a4 | 2018-09-21 17:57:31 +0200 | [diff] [blame] | 4521 | diff_fill_oid_info(p->one, o->repo->index); |
| 4522 | diff_fill_oid_info(p->two, o->repo->index); |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 4523 | |
Nguyễn Thái Ngọc Duy | b78ea5f | 2018-09-21 17:57:19 +0200 | [diff] [blame] | 4524 | builtin_diffstat(name, other, p->one, p->two, |
| 4525 | diffstat, o, p); |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 4526 | } |
| 4527 | |
Johannes Schindelin | 8824689 | 2006-05-20 23:43:13 +0200 | [diff] [blame] | 4528 | static void run_checkdiff(struct diff_filepair *p, struct diff_options *o) |
| 4529 | { |
| 4530 | const char *name; |
| 4531 | const char *other; |
Junio C Hamano | cd676a5 | 2008-02-12 14:26:02 -0800 | [diff] [blame] | 4532 | const char *attr_path; |
Johannes Schindelin | 8824689 | 2006-05-20 23:43:13 +0200 | [diff] [blame] | 4533 | |
| 4534 | if (DIFF_PAIR_UNMERGED(p)) { |
| 4535 | /* unmerged */ |
| 4536 | return; |
| 4537 | } |
| 4538 | |
| 4539 | name = p->one->path; |
| 4540 | other = (strcmp(name, p->two->path) ? p->two->path : NULL); |
Junio C Hamano | cd676a5 | 2008-02-12 14:26:02 -0800 | [diff] [blame] | 4541 | attr_path = other ? other : name; |
| 4542 | |
| 4543 | if (o->prefix_length) |
| 4544 | strip_prefix(o->prefix_length, &name, &other); |
Johannes Schindelin | 8824689 | 2006-05-20 23:43:13 +0200 | [diff] [blame] | 4545 | |
Nguyễn Thái Ngọc Duy | 58bf2a4 | 2018-09-21 17:57:31 +0200 | [diff] [blame] | 4546 | diff_fill_oid_info(p->one, o->repo->index); |
| 4547 | diff_fill_oid_info(p->two, o->repo->index); |
Johannes Schindelin | 8824689 | 2006-05-20 23:43:13 +0200 | [diff] [blame] | 4548 | |
Junio C Hamano | cd676a5 | 2008-02-12 14:26:02 -0800 | [diff] [blame] | 4549 | builtin_checkdiff(name, other, attr_path, p->one, p->two, o); |
Johannes Schindelin | 8824689 | 2006-05-20 23:43:13 +0200 | [diff] [blame] | 4550 | } |
| 4551 | |
Nguyễn Thái Ngọc Duy | 4a28847 | 2019-01-27 07:35:31 +0700 | [diff] [blame] | 4552 | static void prep_parse_options(struct diff_options *options); |
| 4553 | |
Nguyễn Thái Ngọc Duy | e675765 | 2018-09-21 17:57:24 +0200 | [diff] [blame] | 4554 | void repo_diff_setup(struct repository *r, struct diff_options *options) |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 4555 | { |
Johannes Schindelin | be4f2b4 | 2010-08-05 10:49:55 +0200 | [diff] [blame] | 4556 | memcpy(options, &default_diff_options, sizeof(*options)); |
Daniel Barkalow | c0c7773 | 2008-03-09 22:43:39 -0400 | [diff] [blame] | 4557 | |
| 4558 | options->file = stdout; |
Nguyễn Thái Ngọc Duy | e675765 | 2018-09-21 17:57:24 +0200 | [diff] [blame] | 4559 | options->repo = r; |
Daniel Barkalow | c0c7773 | 2008-03-09 22:43:39 -0400 | [diff] [blame] | 4560 | |
Stefan Beller | 7648b79 | 2018-08-17 13:43:52 -0700 | [diff] [blame] | 4561 | options->output_indicators[OUTPUT_INDICATOR_NEW] = '+'; |
| 4562 | options->output_indicators[OUTPUT_INDICATOR_OLD] = '-'; |
| 4563 | options->output_indicators[OUTPUT_INDICATOR_CONTEXT] = ' '; |
Jack Bates | 43d1948 | 2016-12-06 09:56:14 -0700 | [diff] [blame] | 4564 | options->abbrev = DEFAULT_ABBREV; |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 4565 | options->line_termination = '\n'; |
| 4566 | options->break_opt = -1; |
| 4567 | options->rename_limit = -1; |
Johan Herland | 712d2c7 | 2011-04-29 11:36:20 +0200 | [diff] [blame] | 4568 | options->dirstat_permille = diff_dirstat_permille_default; |
Jeff Muizelaar | 6468a4e | 2012-09-27 15:12:52 -0400 | [diff] [blame] | 4569 | options->context = diff_context_default; |
Vegard Nossum | c488867 | 2017-01-12 13:21:11 +0100 | [diff] [blame] | 4570 | options->interhunkcontext = diff_interhunk_context_default; |
Junio C Hamano | a17505f | 2016-10-04 15:26:27 -0700 | [diff] [blame] | 4571 | options->ws_error_highlight = ws_error_highlight_default; |
Brandon Williams | 0d1e0e7 | 2017-10-31 11:19:11 -0700 | [diff] [blame] | 4572 | options->flags.rename_empty = 1; |
Laurent Arnoud | c28ded8 | 2020-05-22 12:46:18 +0200 | [diff] [blame] | 4573 | options->flags.relative_name = diff_relative; |
Stefan Beller | 15af58c | 2018-01-04 14:50:42 -0800 | [diff] [blame] | 4574 | options->objfind = NULL; |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 4575 | |
Kirill Smelkov | 72441af | 2014-04-07 01:46:26 +0400 | [diff] [blame] | 4576 | /* pathchange left =NULL by default */ |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 4577 | options->change = diff_change; |
| 4578 | options->add_remove = diff_addremove; |
Jeff King | f1c9626 | 2011-08-17 22:03:12 -0700 | [diff] [blame] | 4579 | options->use_color = diff_use_color_default; |
Eric Wong | b68ea12 | 2006-07-07 04:01:23 -0700 | [diff] [blame] | 4580 | options->detect_rename = diff_detect_rename_default; |
Michal Privoznik | 07ab4de | 2013-01-16 08:51:57 +0100 | [diff] [blame] | 4581 | options->xdl_opts |= diff_algorithm; |
Michael Haggerty | 433860f | 2016-09-05 11:44:51 +0200 | [diff] [blame] | 4582 | if (diff_indent_heuristic) |
| 4583 | DIFF_XDL_SET(options, INDENT_HEURISTIC); |
Johannes Schindelin | eab9a40 | 2007-12-18 19:32:14 +0000 | [diff] [blame] | 4584 | |
Samuel Bronson | 6d8940b | 2013-12-18 19:08:12 -0500 | [diff] [blame] | 4585 | options->orderfile = diff_order_file_cfg; |
| 4586 | |
Eli Collins | f89504d | 2010-05-02 19:03:41 -0700 | [diff] [blame] | 4587 | if (diff_no_prefix) { |
| 4588 | options->a_prefix = options->b_prefix = ""; |
| 4589 | } else if (!diff_mnemonic_prefix) { |
Junio C Hamano | a5a818e | 2008-08-18 20:08:09 -0700 | [diff] [blame] | 4590 | options->a_prefix = "a/"; |
| 4591 | options->b_prefix = "b/"; |
| 4592 | } |
Stefan Beller | 2e2d5ac | 2017-06-30 13:53:07 -0700 | [diff] [blame] | 4593 | |
| 4594 | options->color_moved = diff_color_moved_default; |
Stefan Beller | 626c0b5 | 2018-07-18 12:31:56 -0700 | [diff] [blame] | 4595 | options->color_moved_ws_handling = diff_color_moved_ws_default; |
Nguyễn Thái Ngọc Duy | 4a28847 | 2019-01-27 07:35:31 +0700 | [diff] [blame] | 4596 | |
| 4597 | prep_parse_options(options); |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 4598 | } |
| 4599 | |
Thomas Rast | 2845265 | 2012-08-03 14:16:24 +0200 | [diff] [blame] | 4600 | void diff_setup_done(struct diff_options *options) |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 4601 | { |
Stefan Beller | 4d8c51a | 2018-01-04 14:50:44 -0800 | [diff] [blame] | 4602 | unsigned check_mask = DIFF_FORMAT_NAME | |
| 4603 | DIFF_FORMAT_NAME_STATUS | |
| 4604 | DIFF_FORMAT_CHECKDIFF | |
| 4605 | DIFF_FORMAT_NO_OUTPUT; |
brian m. carlson | de1d81d | 2018-05-02 00:25:52 +0000 | [diff] [blame] | 4606 | /* |
| 4607 | * This must be signed because we're comparing against a potentially |
| 4608 | * negative value. |
| 4609 | */ |
| 4610 | const int hexsz = the_hash_algo->hexsz; |
Timo Hirvonen | d7de00f | 2006-06-24 20:26:49 +0300 | [diff] [blame] | 4611 | |
Junio C Hamano | 6c37400 | 2013-05-10 17:10:11 +0200 | [diff] [blame] | 4612 | if (options->set_default) |
| 4613 | options->set_default(options); |
| 4614 | |
Stefan Beller | 4d8c51a | 2018-01-04 14:50:44 -0800 | [diff] [blame] | 4615 | if (HAS_MULTI_BITS(options->output_format & check_mask)) |
Jean-Noël AVILA | a2f05c9 | 2016-09-20 21:04:27 +0200 | [diff] [blame] | 4616 | die(_("--name-only, --name-status, --check and -s are mutually exclusive")); |
Timo Hirvonen | d7de00f | 2006-06-24 20:26:49 +0300 | [diff] [blame] | 4617 | |
Stefan Beller | 5e50525 | 2018-01-04 14:50:43 -0800 | [diff] [blame] | 4618 | if (HAS_MULTI_BITS(options->pickaxe_opts & DIFF_PICKAXE_KINDS_MASK)) |
| 4619 | die(_("-G, -S and --find-object are mutually exclusive")); |
| 4620 | |
Junio C Hamano | f245194 | 2009-05-22 12:45:29 -0700 | [diff] [blame] | 4621 | /* |
| 4622 | * Most of the time we can say "there are changes" |
| 4623 | * only by checking if there are changed paths, but |
| 4624 | * --ignore-whitespace* options force us to look |
Jim Meyering | 97bf2a0 | 2009-08-30 22:27:02 +0200 | [diff] [blame] | 4625 | * inside contents. |
Junio C Hamano | f245194 | 2009-05-22 12:45:29 -0700 | [diff] [blame] | 4626 | */ |
| 4627 | |
Junio C Hamano | 446d12c | 2017-10-26 15:15:51 +0900 | [diff] [blame] | 4628 | if ((options->xdl_opts & XDF_WHITESPACE_FLAGS)) |
Brandon Williams | 0d1e0e7 | 2017-10-31 11:19:11 -0700 | [diff] [blame] | 4629 | options->flags.diff_from_contents = 1; |
Junio C Hamano | f245194 | 2009-05-22 12:45:29 -0700 | [diff] [blame] | 4630 | else |
Brandon Williams | 0d1e0e7 | 2017-10-31 11:19:11 -0700 | [diff] [blame] | 4631 | options->flags.diff_from_contents = 0; |
Junio C Hamano | f245194 | 2009-05-22 12:45:29 -0700 | [diff] [blame] | 4632 | |
Brandon Williams | 0d1e0e7 | 2017-10-31 11:19:11 -0700 | [diff] [blame] | 4633 | if (options->flags.find_copies_harder) |
Junio C Hamano | 03b9d56 | 2006-08-09 13:17:19 -0700 | [diff] [blame] | 4634 | options->detect_rename = DIFF_DETECT_COPY; |
| 4635 | |
Brandon Williams | 0d1e0e7 | 2017-10-31 11:19:11 -0700 | [diff] [blame] | 4636 | if (!options->flags.relative_name) |
Junio C Hamano | cd676a5 | 2008-02-12 14:26:02 -0800 | [diff] [blame] | 4637 | options->prefix = NULL; |
| 4638 | if (options->prefix) |
| 4639 | options->prefix_length = strlen(options->prefix); |
| 4640 | else |
| 4641 | options->prefix_length = 0; |
| 4642 | |
Timo Hirvonen | c674434 | 2006-06-24 20:21:53 +0300 | [diff] [blame] | 4643 | if (options->output_format & (DIFF_FORMAT_NAME | |
| 4644 | DIFF_FORMAT_NAME_STATUS | |
| 4645 | DIFF_FORMAT_CHECKDIFF | |
| 4646 | DIFF_FORMAT_NO_OUTPUT)) |
| 4647 | options->output_format &= ~(DIFF_FORMAT_RAW | |
Junio C Hamano | 74e2abe | 2006-10-12 03:01:00 -0700 | [diff] [blame] | 4648 | DIFF_FORMAT_NUMSTAT | |
Timo Hirvonen | c674434 | 2006-06-24 20:21:53 +0300 | [diff] [blame] | 4649 | DIFF_FORMAT_DIFFSTAT | |
Nicolas Pitre | ebd124c | 2006-12-14 23:15:44 -0500 | [diff] [blame] | 4650 | DIFF_FORMAT_SHORTSTAT | |
Linus Torvalds | 7df7c01 | 2008-02-12 13:26:31 -0800 | [diff] [blame] | 4651 | DIFF_FORMAT_DIRSTAT | |
Timo Hirvonen | c674434 | 2006-06-24 20:21:53 +0300 | [diff] [blame] | 4652 | DIFF_FORMAT_SUMMARY | |
| 4653 | DIFF_FORMAT_PATCH); |
| 4654 | |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 4655 | /* |
| 4656 | * These cases always need recursive; we do not drop caller-supplied |
| 4657 | * recursive bits for other formats here. |
| 4658 | */ |
Timo Hirvonen | c674434 | 2006-06-24 20:21:53 +0300 | [diff] [blame] | 4659 | if (options->output_format & (DIFF_FORMAT_PATCH | |
Junio C Hamano | 74e2abe | 2006-10-12 03:01:00 -0700 | [diff] [blame] | 4660 | DIFF_FORMAT_NUMSTAT | |
Timo Hirvonen | c674434 | 2006-06-24 20:21:53 +0300 | [diff] [blame] | 4661 | DIFF_FORMAT_DIFFSTAT | |
Nicolas Pitre | ebd124c | 2006-12-14 23:15:44 -0500 | [diff] [blame] | 4662 | DIFF_FORMAT_SHORTSTAT | |
Linus Torvalds | 7df7c01 | 2008-02-12 13:26:31 -0800 | [diff] [blame] | 4663 | DIFF_FORMAT_DIRSTAT | |
Johannes Schindelin | d7014dc | 2006-10-03 23:09:56 +0200 | [diff] [blame] | 4664 | DIFF_FORMAT_SUMMARY | |
Timo Hirvonen | c674434 | 2006-06-24 20:21:53 +0300 | [diff] [blame] | 4665 | DIFF_FORMAT_CHECKDIFF)) |
Brandon Williams | 0d1e0e7 | 2017-10-31 11:19:11 -0700 | [diff] [blame] | 4666 | options->flags.recursive = 1; |
Junio C Hamano | 5e36354 | 2006-05-22 00:31:02 -0700 | [diff] [blame] | 4667 | /* |
Junio C Hamano | 3969cf7 | 2006-06-27 15:08:19 -0700 | [diff] [blame] | 4668 | * Also pickaxe would not work very well if you do not say recursive |
Junio C Hamano | 5e36354 | 2006-05-22 00:31:02 -0700 | [diff] [blame] | 4669 | */ |
Stefan Beller | cf63051 | 2018-01-04 14:50:41 -0800 | [diff] [blame] | 4670 | if (options->pickaxe_opts & DIFF_PICKAXE_KINDS_MASK) |
Brandon Williams | 0d1e0e7 | 2017-10-31 11:19:11 -0700 | [diff] [blame] | 4671 | options->flags.recursive = 1; |
Jens Lehmann | ae6d5c1 | 2010-03-11 22:50:25 +0100 | [diff] [blame] | 4672 | /* |
| 4673 | * When patches are generated, submodules diffed against the work tree |
| 4674 | * must be checked for dirtiness too so it can be shown in the output |
| 4675 | */ |
| 4676 | if (options->output_format & DIFF_FORMAT_PATCH) |
Brandon Williams | 0d1e0e7 | 2017-10-31 11:19:11 -0700 | [diff] [blame] | 4677 | options->flags.dirty_submodules = 1; |
Junio C Hamano | 5e36354 | 2006-05-22 00:31:02 -0700 | [diff] [blame] | 4678 | |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 4679 | if (options->detect_rename && options->rename_limit < 0) |
| 4680 | options->rename_limit = diff_rename_limit_default; |
brian m. carlson | de1d81d | 2018-05-02 00:25:52 +0000 | [diff] [blame] | 4681 | if (hexsz < options->abbrev) |
| 4682 | options->abbrev = hexsz; /* full */ |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 4683 | |
Junio C Hamano | 68aacb2 | 2007-03-14 11:12:13 -0700 | [diff] [blame] | 4684 | /* |
| 4685 | * It does not make sense to show the first hit we happened |
| 4686 | * to have found. It does not make sense not to return with |
| 4687 | * exit code in such a case either. |
| 4688 | */ |
Brandon Williams | 0d1e0e7 | 2017-10-31 11:19:11 -0700 | [diff] [blame] | 4689 | if (options->flags.quick) { |
Junio C Hamano | 68aacb2 | 2007-03-14 11:12:13 -0700 | [diff] [blame] | 4690 | options->output_format = DIFF_FORMAT_NO_OUTPUT; |
Brandon Williams | 0d1e0e7 | 2017-10-31 11:19:11 -0700 | [diff] [blame] | 4691 | options->flags.exit_with_status = 1; |
Junio C Hamano | 68aacb2 | 2007-03-14 11:12:13 -0700 | [diff] [blame] | 4692 | } |
Zoltan Klinger | ee7fb0b | 2013-12-06 10:38:46 +1100 | [diff] [blame] | 4693 | |
Jeff King | 0ea7d5b | 2013-12-16 15:02:21 -0500 | [diff] [blame] | 4694 | options->diff_path_counter = 0; |
Junio C Hamano | b0e2c99 | 2014-06-16 10:07:09 -0700 | [diff] [blame] | 4695 | |
Brandon Williams | 0d1e0e7 | 2017-10-31 11:19:11 -0700 | [diff] [blame] | 4696 | if (options->flags.follow_renames && options->pathspec.nr != 1) |
Jeff King | dd63f16 | 2014-05-20 02:49:20 -0400 | [diff] [blame] | 4697 | die(_("--follow requires exactly one pathspec")); |
Stefan Beller | 2e2d5ac | 2017-06-30 13:53:07 -0700 | [diff] [blame] | 4698 | |
| 4699 | if (!options->use_color || external_diff()) |
| 4700 | options->color_moved = 0; |
Nguyễn Thái Ngọc Duy | 4a28847 | 2019-01-27 07:35:31 +0700 | [diff] [blame] | 4701 | |
| 4702 | FREE_AND_NULL(options->parseopts); |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 4703 | } |
| 4704 | |
Matthieu Moy | dea007f | 2010-08-05 10:22:52 +0200 | [diff] [blame] | 4705 | int parse_long_opt(const char *opt, const char **argv, |
| 4706 | const char **optarg) |
| 4707 | { |
| 4708 | const char *arg = argv[0]; |
Jeff King | 95b567c | 2014-06-18 15:48:29 -0400 | [diff] [blame] | 4709 | if (!skip_prefix(arg, "--", &arg)) |
Matthieu Moy | dea007f | 2010-08-05 10:22:52 +0200 | [diff] [blame] | 4710 | return 0; |
Jeff King | 95b567c | 2014-06-18 15:48:29 -0400 | [diff] [blame] | 4711 | if (!skip_prefix(arg, opt, &arg)) |
Matthieu Moy | dea007f | 2010-08-05 10:22:52 +0200 | [diff] [blame] | 4712 | return 0; |
Nicolas Vigier | b0d12fc | 2013-10-31 12:08:28 +0100 | [diff] [blame] | 4713 | if (*arg == '=') { /* stuck form: --option=value */ |
Matthieu Moy | dea007f | 2010-08-05 10:22:52 +0200 | [diff] [blame] | 4714 | *optarg = arg + 1; |
| 4715 | return 1; |
| 4716 | } |
| 4717 | if (*arg != '\0') |
| 4718 | return 0; |
| 4719 | /* separate form: --option value */ |
| 4720 | if (!argv[1]) |
| 4721 | die("Option '--%s' requires a value", opt); |
| 4722 | *optarg = argv[1]; |
| 4723 | return 2; |
| 4724 | } |
| 4725 | |
Nguyễn Thái Ngọc Duy | 84b5089 | 2019-02-21 18:16:10 +0700 | [diff] [blame] | 4726 | static int diff_opt_stat(const struct option *opt, const char *value, int unset) |
Jonathan Nieder | 4d7f7a4 | 2010-08-05 10:22:53 +0200 | [diff] [blame] | 4727 | { |
Nguyễn Thái Ngọc Duy | 84b5089 | 2019-02-21 18:16:10 +0700 | [diff] [blame] | 4728 | struct diff_options *options = opt->value; |
Jonathan Nieder | 4d7f7a4 | 2010-08-05 10:22:53 +0200 | [diff] [blame] | 4729 | int width = options->stat_width; |
| 4730 | int name_width = options->stat_name_width; |
Zbigniew Jędrzejewski-Szmek | 969fe57 | 2012-03-01 13:26:45 +0100 | [diff] [blame] | 4731 | int graph_width = options->stat_graph_width; |
Michael J Gruber | 808e1db | 2011-05-27 14:36:41 +0200 | [diff] [blame] | 4732 | int count = options->stat_count; |
Nguyễn Thái Ngọc Duy | 84b5089 | 2019-02-21 18:16:10 +0700 | [diff] [blame] | 4733 | char *end; |
Jonathan Nieder | 4d7f7a4 | 2010-08-05 10:22:53 +0200 | [diff] [blame] | 4734 | |
Nguyễn Thái Ngọc Duy | 84b5089 | 2019-02-21 18:16:10 +0700 | [diff] [blame] | 4735 | BUG_ON_OPT_NEG(unset); |
Jonathan Nieder | 4d7f7a4 | 2010-08-05 10:22:53 +0200 | [diff] [blame] | 4736 | |
Nguyễn Thái Ngọc Duy | 84b5089 | 2019-02-21 18:16:10 +0700 | [diff] [blame] | 4737 | if (!strcmp(opt->long_name, "stat")) { |
| 4738 | if (value) { |
| 4739 | width = strtoul(value, &end, 10); |
| 4740 | if (*end == ',') |
| 4741 | name_width = strtoul(end+1, &end, 10); |
| 4742 | if (*end == ',') |
| 4743 | count = strtoul(end+1, &end, 10); |
| 4744 | if (*end) |
| 4745 | return error(_("invalid --stat value: %s"), value); |
Matthieu Moy | 1e57208 | 2010-08-05 10:22:54 +0200 | [diff] [blame] | 4746 | } |
Nguyễn Thái Ngọc Duy | 84b5089 | 2019-02-21 18:16:10 +0700 | [diff] [blame] | 4747 | } else if (!strcmp(opt->long_name, "stat-width")) { |
| 4748 | width = strtoul(value, &end, 10); |
| 4749 | if (*end) |
| 4750 | return error(_("%s expects a numerical value"), |
| 4751 | opt->long_name); |
| 4752 | } else if (!strcmp(opt->long_name, "stat-name-width")) { |
| 4753 | name_width = strtoul(value, &end, 10); |
| 4754 | if (*end) |
| 4755 | return error(_("%s expects a numerical value"), |
| 4756 | opt->long_name); |
| 4757 | } else if (!strcmp(opt->long_name, "stat-graph-width")) { |
| 4758 | graph_width = strtoul(value, &end, 10); |
| 4759 | if (*end) |
| 4760 | return error(_("%s expects a numerical value"), |
| 4761 | opt->long_name); |
| 4762 | } else if (!strcmp(opt->long_name, "stat-count")) { |
| 4763 | count = strtoul(value, &end, 10); |
| 4764 | if (*end) |
| 4765 | return error(_("%s expects a numerical value"), |
| 4766 | opt->long_name); |
| 4767 | } else |
| 4768 | BUG("%s should not get here", opt->long_name); |
Jonathan Nieder | 4d7f7a4 | 2010-08-05 10:22:53 +0200 | [diff] [blame] | 4769 | |
Jonathan Nieder | 4d7f7a4 | 2010-08-05 10:22:53 +0200 | [diff] [blame] | 4770 | options->output_format |= DIFF_FORMAT_DIFFSTAT; |
| 4771 | options->stat_name_width = name_width; |
Zbigniew Jędrzejewski-Szmek | 969fe57 | 2012-03-01 13:26:45 +0100 | [diff] [blame] | 4772 | options->stat_graph_width = graph_width; |
Jonathan Nieder | 4d7f7a4 | 2010-08-05 10:22:53 +0200 | [diff] [blame] | 4773 | options->stat_width = width; |
Michael J Gruber | 808e1db | 2011-05-27 14:36:41 +0200 | [diff] [blame] | 4774 | options->stat_count = count; |
Nguyễn Thái Ngọc Duy | 84b5089 | 2019-02-21 18:16:10 +0700 | [diff] [blame] | 4775 | return 0; |
Jonathan Nieder | 4d7f7a4 | 2010-08-05 10:22:53 +0200 | [diff] [blame] | 4776 | } |
| 4777 | |
Johan Herland | 333f3fb | 2011-04-29 11:36:18 +0200 | [diff] [blame] | 4778 | static int parse_dirstat_opt(struct diff_options *options, const char *params) |
| 4779 | { |
Johan Herland | 51670fc | 2011-04-29 11:36:22 +0200 | [diff] [blame] | 4780 | struct strbuf errmsg = STRBUF_INIT; |
| 4781 | if (parse_dirstat_params(options, params, &errmsg)) |
Johan Herland | 7478ac5 | 2011-04-29 11:36:23 +0200 | [diff] [blame] | 4782 | die(_("Failed to parse --dirstat/-X option parameter:\n%s"), |
Johan Herland | 51670fc | 2011-04-29 11:36:22 +0200 | [diff] [blame] | 4783 | errmsg.buf); |
| 4784 | strbuf_release(&errmsg); |
Johan Herland | 333f3fb | 2011-04-29 11:36:18 +0200 | [diff] [blame] | 4785 | /* |
| 4786 | * The caller knows a dirstat-related option is given from the command |
| 4787 | * line; allow it to say "return this_function();" |
| 4788 | */ |
| 4789 | options->output_format |= DIFF_FORMAT_DIRSTAT; |
| 4790 | return 1; |
| 4791 | } |
| 4792 | |
Junio C Hamano | 1ecc1cb | 2013-07-17 15:05:46 -0700 | [diff] [blame] | 4793 | static const char diff_status_letters[] = { |
| 4794 | DIFF_STATUS_ADDED, |
| 4795 | DIFF_STATUS_COPIED, |
| 4796 | DIFF_STATUS_DELETED, |
| 4797 | DIFF_STATUS_MODIFIED, |
| 4798 | DIFF_STATUS_RENAMED, |
| 4799 | DIFF_STATUS_TYPE_CHANGED, |
| 4800 | DIFF_STATUS_UNKNOWN, |
| 4801 | DIFF_STATUS_UNMERGED, |
| 4802 | DIFF_STATUS_FILTER_AON, |
| 4803 | DIFF_STATUS_FILTER_BROKEN, |
| 4804 | '\0', |
| 4805 | }; |
| 4806 | |
| 4807 | static unsigned int filter_bit['Z' + 1]; |
| 4808 | |
| 4809 | static void prepare_filter_bits(void) |
| 4810 | { |
| 4811 | int i; |
| 4812 | |
| 4813 | if (!filter_bit[DIFF_STATUS_ADDED]) { |
| 4814 | for (i = 0; diff_status_letters[i]; i++) |
| 4815 | filter_bit[(int) diff_status_letters[i]] = (1 << i); |
| 4816 | } |
| 4817 | } |
| 4818 | |
| 4819 | static unsigned filter_bit_tst(char status, const struct diff_options *opt) |
| 4820 | { |
| 4821 | return opt->filter & filter_bit[(int) status]; |
| 4822 | } |
| 4823 | |
Nguyễn Thái Ngọc Duy | cdb5330 | 2019-03-24 15:20:14 +0700 | [diff] [blame] | 4824 | unsigned diff_filter_bit(char status) |
Junio C Hamano | 1ecc1cb | 2013-07-17 15:05:46 -0700 | [diff] [blame] | 4825 | { |
Nguyễn Thái Ngọc Duy | cdb5330 | 2019-03-24 15:20:14 +0700 | [diff] [blame] | 4826 | prepare_filter_bits(); |
| 4827 | return filter_bit[(int) status]; |
| 4828 | } |
| 4829 | |
Nguyễn Thái Ngọc Duy | d2d3f27 | 2019-03-24 15:20:03 +0700 | [diff] [blame] | 4830 | static int diff_opt_diff_filter(const struct option *option, |
| 4831 | const char *optarg, int unset) |
Junio C Hamano | 1ecc1cb | 2013-07-17 15:05:46 -0700 | [diff] [blame] | 4832 | { |
Nguyễn Thái Ngọc Duy | d2d3f27 | 2019-03-24 15:20:03 +0700 | [diff] [blame] | 4833 | struct diff_options *opt = option->value; |
Junio C Hamano | 1ecc1cb | 2013-07-17 15:05:46 -0700 | [diff] [blame] | 4834 | int i, optch; |
| 4835 | |
Nguyễn Thái Ngọc Duy | d2d3f27 | 2019-03-24 15:20:03 +0700 | [diff] [blame] | 4836 | BUG_ON_OPT_NEG(unset); |
Junio C Hamano | 1ecc1cb | 2013-07-17 15:05:46 -0700 | [diff] [blame] | 4837 | prepare_filter_bits(); |
Junio C Hamano | 7f2ea5f | 2013-07-17 16:19:19 -0700 | [diff] [blame] | 4838 | |
| 4839 | /* |
| 4840 | * If there is a negation e.g. 'd' in the input, and we haven't |
| 4841 | * initialized the filter field with another --diff-filter, start |
| 4842 | * from full set of bits, except for AON. |
| 4843 | */ |
| 4844 | if (!opt->filter) { |
| 4845 | for (i = 0; (optch = optarg[i]) != '\0'; i++) { |
| 4846 | if (optch < 'a' || 'z' < optch) |
| 4847 | continue; |
| 4848 | opt->filter = (1 << (ARRAY_SIZE(diff_status_letters) - 1)) - 1; |
| 4849 | opt->filter &= ~filter_bit[DIFF_STATUS_FILTER_AON]; |
| 4850 | break; |
| 4851 | } |
| 4852 | } |
| 4853 | |
Junio C Hamano | 1ecc1cb | 2013-07-17 15:05:46 -0700 | [diff] [blame] | 4854 | for (i = 0; (optch = optarg[i]) != '\0'; i++) { |
| 4855 | unsigned int bit; |
Junio C Hamano | 7f2ea5f | 2013-07-17 16:19:19 -0700 | [diff] [blame] | 4856 | int negate; |
| 4857 | |
| 4858 | if ('a' <= optch && optch <= 'z') { |
| 4859 | negate = 1; |
| 4860 | optch = toupper(optch); |
| 4861 | } else { |
| 4862 | negate = 0; |
| 4863 | } |
Junio C Hamano | 1ecc1cb | 2013-07-17 15:05:46 -0700 | [diff] [blame] | 4864 | |
| 4865 | bit = (0 <= optch && optch <= 'Z') ? filter_bit[optch] : 0; |
| 4866 | if (!bit) |
Nguyễn Thái Ngọc Duy | d2d3f27 | 2019-03-24 15:20:03 +0700 | [diff] [blame] | 4867 | return error(_("unknown change class '%c' in --diff-filter=%s"), |
| 4868 | optarg[i], optarg); |
Junio C Hamano | 7f2ea5f | 2013-07-17 16:19:19 -0700 | [diff] [blame] | 4869 | if (negate) |
| 4870 | opt->filter &= ~bit; |
| 4871 | else |
| 4872 | opt->filter |= bit; |
Junio C Hamano | 1ecc1cb | 2013-07-17 15:05:46 -0700 | [diff] [blame] | 4873 | } |
| 4874 | return 0; |
| 4875 | } |
| 4876 | |
Nguyễn Thái Ngọc Duy | 3b33576 | 2018-12-09 11:25:21 +0100 | [diff] [blame] | 4877 | static void enable_patch_output(int *fmt) |
| 4878 | { |
Matthieu Moy | 71482d3 | 2013-07-16 10:05:37 +0200 | [diff] [blame] | 4879 | *fmt &= ~DIFF_FORMAT_NO_OUTPUT; |
| 4880 | *fmt |= DIFF_FORMAT_PATCH; |
| 4881 | } |
| 4882 | |
Nguyễn Thái Ngọc Duy | 5a749d9 | 2019-03-24 15:19:55 +0700 | [diff] [blame] | 4883 | static int diff_opt_ws_error_highlight(const struct option *option, |
| 4884 | const char *arg, int unset) |
Junio C Hamano | b8767f7 | 2015-05-26 10:11:28 -0700 | [diff] [blame] | 4885 | { |
Nguyễn Thái Ngọc Duy | 5a749d9 | 2019-03-24 15:19:55 +0700 | [diff] [blame] | 4886 | struct diff_options *opt = option->value; |
Junio C Hamano | 077965f | 2016-10-04 15:03:33 -0700 | [diff] [blame] | 4887 | int val = parse_ws_error_highlight(arg); |
Junio C Hamano | b8767f7 | 2015-05-26 10:11:28 -0700 | [diff] [blame] | 4888 | |
Nguyễn Thái Ngọc Duy | 5a749d9 | 2019-03-24 15:19:55 +0700 | [diff] [blame] | 4889 | BUG_ON_OPT_NEG(unset); |
| 4890 | if (val < 0) |
| 4891 | return error(_("unknown value after ws-error-highlight=%.*s"), |
| 4892 | -1 - val, arg); |
Junio C Hamano | b8767f7 | 2015-05-26 10:11:28 -0700 | [diff] [blame] | 4893 | opt->ws_error_highlight = val; |
Nguyễn Thái Ngọc Duy | 5a749d9 | 2019-03-24 15:19:55 +0700 | [diff] [blame] | 4894 | return 0; |
Junio C Hamano | b8767f7 | 2015-05-26 10:11:28 -0700 | [diff] [blame] | 4895 | } |
| 4896 | |
Nguyễn Thái Ngọc Duy | a75f28c | 2019-03-24 15:20:02 +0700 | [diff] [blame] | 4897 | static int diff_opt_find_object(const struct option *option, |
| 4898 | const char *arg, int unset) |
Stefan Beller | 15af58c | 2018-01-04 14:50:42 -0800 | [diff] [blame] | 4899 | { |
Nguyễn Thái Ngọc Duy | a75f28c | 2019-03-24 15:20:02 +0700 | [diff] [blame] | 4900 | struct diff_options *opt = option->value; |
Stefan Beller | 15af58c | 2018-01-04 14:50:42 -0800 | [diff] [blame] | 4901 | struct object_id oid; |
| 4902 | |
Nguyễn Thái Ngọc Duy | a75f28c | 2019-03-24 15:20:02 +0700 | [diff] [blame] | 4903 | BUG_ON_OPT_NEG(unset); |
Stefan Beller | 15af58c | 2018-01-04 14:50:42 -0800 | [diff] [blame] | 4904 | if (get_oid(arg, &oid)) |
Nguyễn Thái Ngọc Duy | a75f28c | 2019-03-24 15:20:02 +0700 | [diff] [blame] | 4905 | return error(_("unable to resolve '%s'"), arg); |
Stefan Beller | 15af58c | 2018-01-04 14:50:42 -0800 | [diff] [blame] | 4906 | |
| 4907 | if (!opt->objfind) |
| 4908 | opt->objfind = xcalloc(1, sizeof(*opt->objfind)); |
| 4909 | |
| 4910 | opt->pickaxe_opts |= DIFF_PICKAXE_KIND_OBJFIND; |
| 4911 | opt->flags.recursive = 1; |
| 4912 | opt->flags.tree_in_recursive = 1; |
| 4913 | oidset_insert(opt->objfind, &oid); |
Nguyễn Thái Ngọc Duy | a75f28c | 2019-03-24 15:20:02 +0700 | [diff] [blame] | 4914 | return 0; |
Stefan Beller | 15af58c | 2018-01-04 14:50:42 -0800 | [diff] [blame] | 4915 | } |
| 4916 | |
Nguyễn Thái Ngọc Duy | df84a43 | 2019-03-05 19:30:11 +0700 | [diff] [blame] | 4917 | static int diff_opt_anchored(const struct option *opt, |
| 4918 | const char *arg, int unset) |
| 4919 | { |
| 4920 | struct diff_options *options = opt->value; |
| 4921 | |
| 4922 | BUG_ON_OPT_NEG(unset); |
| 4923 | options->xdl_opts = DIFF_WITH_ALG(options, PATIENCE_DIFF); |
| 4924 | ALLOC_GROW(options->anchors, options->anchors_nr + 1, |
| 4925 | options->anchors_alloc); |
| 4926 | options->anchors[options->anchors_nr++] = xstrdup(arg); |
| 4927 | return 0; |
| 4928 | } |
| 4929 | |
Nguyễn Thái Ngọc Duy | 6d9af6f | 2019-03-05 19:30:12 +0700 | [diff] [blame] | 4930 | static int diff_opt_binary(const struct option *opt, |
| 4931 | const char *arg, int unset) |
| 4932 | { |
| 4933 | struct diff_options *options = opt->value; |
| 4934 | |
| 4935 | BUG_ON_OPT_NEG(unset); |
| 4936 | BUG_ON_OPT_ARG(arg); |
| 4937 | enable_patch_output(&options->output_format); |
| 4938 | options->flags.binary = 1; |
| 4939 | return 0; |
| 4940 | } |
| 4941 | |
Nguyễn Thái Ngọc Duy | ced4e17 | 2019-02-21 18:16:13 +0700 | [diff] [blame] | 4942 | static int diff_opt_break_rewrites(const struct option *opt, |
| 4943 | const char *arg, int unset) |
| 4944 | { |
| 4945 | int *break_opt = opt->value; |
| 4946 | int opt1, opt2; |
| 4947 | |
| 4948 | BUG_ON_OPT_NEG(unset); |
| 4949 | if (!arg) |
| 4950 | arg = ""; |
| 4951 | opt1 = parse_rename_score(&arg); |
| 4952 | if (*arg == 0) |
| 4953 | opt2 = 0; |
| 4954 | else if (*arg != '/') |
| 4955 | return error(_("%s expects <n>/<m> form"), opt->long_name); |
| 4956 | else { |
| 4957 | arg++; |
| 4958 | opt2 = parse_rename_score(&arg); |
| 4959 | } |
| 4960 | if (*arg != 0) |
| 4961 | return error(_("%s expects <n>/<m> form"), opt->long_name); |
| 4962 | *break_opt = opt1 | (opt2 << 16); |
| 4963 | return 0; |
| 4964 | } |
| 4965 | |
Nguyễn Thái Ngọc Duy | af2f368 | 2019-02-21 18:16:12 +0700 | [diff] [blame] | 4966 | static int diff_opt_char(const struct option *opt, |
| 4967 | const char *arg, int unset) |
| 4968 | { |
| 4969 | char *value = opt->value; |
| 4970 | |
| 4971 | BUG_ON_OPT_NEG(unset); |
| 4972 | if (arg[1]) |
| 4973 | return error(_("%s expects a character, got '%s'"), |
| 4974 | opt->long_name, arg); |
| 4975 | *value = arg[0]; |
| 4976 | return 0; |
| 4977 | } |
| 4978 | |
Nguyễn Thái Ngọc Duy | 59311a9 | 2019-03-24 15:20:09 +0700 | [diff] [blame] | 4979 | static int diff_opt_color_moved(const struct option *opt, |
| 4980 | const char *arg, int unset) |
| 4981 | { |
| 4982 | struct diff_options *options = opt->value; |
| 4983 | |
| 4984 | if (unset) { |
| 4985 | options->color_moved = COLOR_MOVED_NO; |
| 4986 | } else if (!arg) { |
| 4987 | if (diff_color_moved_default) |
| 4988 | options->color_moved = diff_color_moved_default; |
| 4989 | if (options->color_moved == COLOR_MOVED_NO) |
| 4990 | options->color_moved = COLOR_MOVED_DEFAULT; |
| 4991 | } else { |
| 4992 | int cm = parse_color_moved(arg); |
| 4993 | if (cm < 0) |
| 4994 | return error(_("bad --color-moved argument: %s"), arg); |
| 4995 | options->color_moved = cm; |
| 4996 | } |
| 4997 | return 0; |
| 4998 | } |
| 4999 | |
Nguyễn Thái Ngọc Duy | 8ce2020 | 2019-03-24 15:20:10 +0700 | [diff] [blame] | 5000 | static int diff_opt_color_moved_ws(const struct option *opt, |
| 5001 | const char *arg, int unset) |
| 5002 | { |
| 5003 | struct diff_options *options = opt->value; |
| 5004 | unsigned cm; |
| 5005 | |
Nguyễn Thái Ngọc Duy | bb98729 | 2019-03-24 15:20:11 +0700 | [diff] [blame] | 5006 | if (unset) { |
| 5007 | options->color_moved_ws_handling = 0; |
| 5008 | return 0; |
| 5009 | } |
| 5010 | |
Nguyễn Thái Ngọc Duy | 8ce2020 | 2019-03-24 15:20:10 +0700 | [diff] [blame] | 5011 | cm = parse_color_moved_ws(arg); |
| 5012 | if (cm & COLOR_MOVED_WS_ERROR) |
| 5013 | return error(_("invalid mode '%s' in --color-moved-ws"), arg); |
| 5014 | options->color_moved_ws_handling = cm; |
| 5015 | return 0; |
| 5016 | } |
| 5017 | |
Nguyễn Thái Ngọc Duy | 212db69 | 2019-03-05 19:30:20 +0700 | [diff] [blame] | 5018 | static int diff_opt_color_words(const struct option *opt, |
| 5019 | const char *arg, int unset) |
| 5020 | { |
| 5021 | struct diff_options *options = opt->value; |
| 5022 | |
| 5023 | BUG_ON_OPT_NEG(unset); |
| 5024 | options->use_color = 1; |
| 5025 | options->word_diff = DIFF_WORDS_COLOR; |
| 5026 | options->word_regex = arg; |
| 5027 | return 0; |
| 5028 | } |
| 5029 | |
Nguyễn Thái Ngọc Duy | 7d7942b | 2019-02-21 18:16:11 +0700 | [diff] [blame] | 5030 | static int diff_opt_compact_summary(const struct option *opt, |
| 5031 | const char *arg, int unset) |
| 5032 | { |
| 5033 | struct diff_options *options = opt->value; |
| 5034 | |
| 5035 | BUG_ON_OPT_ARG(arg); |
| 5036 | if (unset) { |
| 5037 | options->flags.stat_with_summary = 0; |
| 5038 | } else { |
| 5039 | options->flags.stat_with_summary = 1; |
| 5040 | options->output_format |= DIFF_FORMAT_DIFFSTAT; |
| 5041 | } |
| 5042 | return 0; |
| 5043 | } |
| 5044 | |
Nguyễn Thái Ngọc Duy | 10f35b1 | 2019-03-05 19:30:10 +0700 | [diff] [blame] | 5045 | static int diff_opt_diff_algorithm(const struct option *opt, |
| 5046 | const char *arg, int unset) |
| 5047 | { |
| 5048 | struct diff_options *options = opt->value; |
| 5049 | long value = parse_algorithm_value(arg); |
| 5050 | |
| 5051 | BUG_ON_OPT_NEG(unset); |
| 5052 | if (value < 0) |
| 5053 | return error(_("option diff-algorithm accepts \"myers\", " |
| 5054 | "\"minimal\", \"patience\" and \"histogram\"")); |
| 5055 | |
| 5056 | /* clear out previous settings */ |
| 5057 | DIFF_XDL_CLR(options, NEED_MINIMAL); |
| 5058 | options->xdl_opts &= ~XDF_DIFF_ALGORITHM_MASK; |
| 5059 | options->xdl_opts |= value; |
| 5060 | return 0; |
| 5061 | } |
| 5062 | |
Nguyễn Thái Ngọc Duy | 4ce7aab | 2019-02-21 18:16:03 +0700 | [diff] [blame] | 5063 | static int diff_opt_dirstat(const struct option *opt, |
| 5064 | const char *arg, int unset) |
| 5065 | { |
| 5066 | struct diff_options *options = opt->value; |
| 5067 | |
| 5068 | BUG_ON_OPT_NEG(unset); |
| 5069 | if (!strcmp(opt->long_name, "cumulative")) { |
| 5070 | if (arg) |
| 5071 | BUG("how come --cumulative take a value?"); |
| 5072 | arg = "cumulative"; |
| 5073 | } else if (!strcmp(opt->long_name, "dirstat-by-file")) |
| 5074 | parse_dirstat_opt(options, "files"); |
| 5075 | parse_dirstat_opt(options, arg ? arg : ""); |
| 5076 | return 0; |
| 5077 | } |
| 5078 | |
Nguyễn Thái Ngọc Duy | 7f64850 | 2019-02-21 18:16:16 +0700 | [diff] [blame] | 5079 | static int diff_opt_find_copies(const struct option *opt, |
| 5080 | const char *arg, int unset) |
| 5081 | { |
| 5082 | struct diff_options *options = opt->value; |
| 5083 | |
| 5084 | BUG_ON_OPT_NEG(unset); |
| 5085 | if (!arg) |
| 5086 | arg = ""; |
| 5087 | options->rename_score = parse_rename_score(&arg); |
| 5088 | if (*arg != 0) |
| 5089 | return error(_("invalid argument to %s"), opt->long_name); |
| 5090 | |
| 5091 | if (options->detect_rename == DIFF_DETECT_COPY) |
| 5092 | options->flags.find_copies_harder = 1; |
| 5093 | else |
| 5094 | options->detect_rename = DIFF_DETECT_COPY; |
| 5095 | |
| 5096 | return 0; |
| 5097 | } |
| 5098 | |
Nguyễn Thái Ngọc Duy | f476308 | 2019-02-21 18:16:14 +0700 | [diff] [blame] | 5099 | static int diff_opt_find_renames(const struct option *opt, |
| 5100 | const char *arg, int unset) |
| 5101 | { |
| 5102 | struct diff_options *options = opt->value; |
| 5103 | |
| 5104 | BUG_ON_OPT_NEG(unset); |
| 5105 | if (!arg) |
| 5106 | arg = ""; |
| 5107 | options->rename_score = parse_rename_score(&arg); |
| 5108 | if (*arg != 0) |
| 5109 | return error(_("invalid argument to %s"), opt->long_name); |
| 5110 | |
| 5111 | options->detect_rename = DIFF_DETECT_RENAME; |
| 5112 | return 0; |
| 5113 | } |
| 5114 | |
Nguyễn Thái Ngọc Duy | 1e9250b | 2019-03-05 19:30:16 +0700 | [diff] [blame] | 5115 | static int diff_opt_follow(const struct option *opt, |
| 5116 | const char *arg, int unset) |
| 5117 | { |
| 5118 | struct diff_options *options = opt->value; |
| 5119 | |
| 5120 | BUG_ON_OPT_ARG(arg); |
| 5121 | if (unset) { |
| 5122 | options->flags.follow_renames = 0; |
| 5123 | options->flags.default_follow_renames = 0; |
| 5124 | } else { |
| 5125 | options->flags.follow_renames = 1; |
| 5126 | } |
| 5127 | return 0; |
| 5128 | } |
| 5129 | |
Nguyễn Thái Ngọc Duy | b680ee1 | 2019-03-05 19:30:25 +0700 | [diff] [blame] | 5130 | static int diff_opt_ignore_submodules(const struct option *opt, |
| 5131 | const char *arg, int unset) |
| 5132 | { |
| 5133 | struct diff_options *options = opt->value; |
| 5134 | |
| 5135 | BUG_ON_OPT_NEG(unset); |
| 5136 | if (!arg) |
| 5137 | arg = "all"; |
| 5138 | options->flags.override_submodule_config = 1; |
| 5139 | handle_ignore_submodules_arg(options, arg); |
| 5140 | return 0; |
| 5141 | } |
| 5142 | |
Nguyễn Thái Ngọc Duy | 2f81cf9 | 2019-03-24 15:20:06 +0700 | [diff] [blame] | 5143 | static int diff_opt_line_prefix(const struct option *opt, |
| 5144 | const char *optarg, int unset) |
| 5145 | { |
| 5146 | struct diff_options *options = opt->value; |
| 5147 | |
| 5148 | BUG_ON_OPT_NEG(unset); |
| 5149 | options->line_prefix = optarg; |
| 5150 | options->line_prefix_length = strlen(options->line_prefix); |
| 5151 | graph_setup_line_prefix(options); |
| 5152 | return 0; |
| 5153 | } |
| 5154 | |
Nguyễn Thái Ngọc Duy | 11c659d | 2019-03-24 15:20:07 +0700 | [diff] [blame] | 5155 | static int diff_opt_no_prefix(const struct option *opt, |
| 5156 | const char *optarg, int unset) |
| 5157 | { |
| 5158 | struct diff_options *options = opt->value; |
| 5159 | |
| 5160 | BUG_ON_OPT_NEG(unset); |
| 5161 | BUG_ON_OPT_ARG(optarg); |
| 5162 | options->a_prefix = ""; |
| 5163 | options->b_prefix = ""; |
| 5164 | return 0; |
| 5165 | } |
| 5166 | |
Nguyễn Thái Ngọc Duy | af2f368 | 2019-02-21 18:16:12 +0700 | [diff] [blame] | 5167 | static enum parse_opt_result diff_opt_output(struct parse_opt_ctx_t *ctx, |
| 5168 | const struct option *opt, |
| 5169 | const char *arg, int unset) |
| 5170 | { |
| 5171 | struct diff_options *options = opt->value; |
| 5172 | char *path; |
| 5173 | |
| 5174 | BUG_ON_OPT_NEG(unset); |
| 5175 | path = prefix_filename(ctx->prefix, arg); |
| 5176 | options->file = xfopen(path, "w"); |
| 5177 | options->close_file = 1; |
| 5178 | if (options->use_color != GIT_COLOR_ALWAYS) |
| 5179 | options->use_color = GIT_COLOR_NEVER; |
| 5180 | free(path); |
| 5181 | return 0; |
| 5182 | } |
| 5183 | |
Nguyễn Thái Ngọc Duy | 31fd640 | 2019-03-05 19:30:08 +0700 | [diff] [blame] | 5184 | static int diff_opt_patience(const struct option *opt, |
| 5185 | const char *arg, int unset) |
| 5186 | { |
| 5187 | struct diff_options *options = opt->value; |
| 5188 | int i; |
| 5189 | |
| 5190 | BUG_ON_OPT_NEG(unset); |
| 5191 | BUG_ON_OPT_ARG(arg); |
| 5192 | options->xdl_opts = DIFF_WITH_ALG(options, PATIENCE_DIFF); |
| 5193 | /* |
| 5194 | * Both --patience and --anchored use PATIENCE_DIFF |
| 5195 | * internally, so remove any anchors previously |
| 5196 | * specified. |
| 5197 | */ |
| 5198 | for (i = 0; i < options->anchors_nr; i++) |
| 5199 | free(options->anchors[i]); |
| 5200 | options->anchors_nr = 0; |
| 5201 | return 0; |
| 5202 | } |
| 5203 | |
Nguyễn Thái Ngọc Duy | a41cfb3 | 2019-03-24 15:19:59 +0700 | [diff] [blame] | 5204 | static int diff_opt_pickaxe_regex(const struct option *opt, |
| 5205 | const char *arg, int unset) |
| 5206 | { |
| 5207 | struct diff_options *options = opt->value; |
| 5208 | |
| 5209 | BUG_ON_OPT_NEG(unset); |
| 5210 | options->pickaxe = arg; |
| 5211 | options->pickaxe_opts |= DIFF_PICKAXE_KIND_G; |
| 5212 | return 0; |
| 5213 | } |
| 5214 | |
| 5215 | static int diff_opt_pickaxe_string(const struct option *opt, |
| 5216 | const char *arg, int unset) |
| 5217 | { |
| 5218 | struct diff_options *options = opt->value; |
| 5219 | |
| 5220 | BUG_ON_OPT_NEG(unset); |
| 5221 | options->pickaxe = arg; |
| 5222 | options->pickaxe_opts |= DIFF_PICKAXE_KIND_S; |
| 5223 | return 0; |
| 5224 | } |
| 5225 | |
Nguyễn Thái Ngọc Duy | 0b1c5b5 | 2019-02-21 18:16:19 +0700 | [diff] [blame] | 5226 | static int diff_opt_relative(const struct option *opt, |
| 5227 | const char *arg, int unset) |
| 5228 | { |
| 5229 | struct diff_options *options = opt->value; |
| 5230 | |
Laurent Arnoud | c28ded8 | 2020-05-22 12:46:18 +0200 | [diff] [blame] | 5231 | options->flags.relative_name = !unset; |
Nguyễn Thái Ngọc Duy | 0b1c5b5 | 2019-02-21 18:16:19 +0700 | [diff] [blame] | 5232 | if (arg) |
| 5233 | options->prefix = arg; |
| 5234 | return 0; |
| 5235 | } |
| 5236 | |
Nguyễn Thái Ngọc Duy | 125dcea | 2019-03-05 19:30:26 +0700 | [diff] [blame] | 5237 | static int diff_opt_submodule(const struct option *opt, |
| 5238 | const char *arg, int unset) |
| 5239 | { |
| 5240 | struct diff_options *options = opt->value; |
| 5241 | |
| 5242 | BUG_ON_OPT_NEG(unset); |
| 5243 | if (!arg) |
| 5244 | arg = "log"; |
| 5245 | if (parse_submodule_params(options, arg)) |
| 5246 | return error(_("failed to parse --submodule option parameter: '%s'"), |
| 5247 | arg); |
| 5248 | return 0; |
| 5249 | } |
| 5250 | |
Nguyễn Thái Ngọc Duy | 8ab7697 | 2019-03-05 19:30:24 +0700 | [diff] [blame] | 5251 | static int diff_opt_textconv(const struct option *opt, |
| 5252 | const char *arg, int unset) |
| 5253 | { |
| 5254 | struct diff_options *options = opt->value; |
| 5255 | |
| 5256 | BUG_ON_OPT_ARG(arg); |
| 5257 | if (unset) { |
| 5258 | options->flags.allow_textconv = 0; |
| 5259 | } else { |
| 5260 | options->flags.allow_textconv = 1; |
| 5261 | options->flags.textconv_set_via_cmdline = 1; |
| 5262 | } |
| 5263 | return 0; |
| 5264 | } |
| 5265 | |
Nguyễn Thái Ngọc Duy | d473e2e | 2019-01-27 07:35:33 +0700 | [diff] [blame] | 5266 | static int diff_opt_unified(const struct option *opt, |
| 5267 | const char *arg, int unset) |
| 5268 | { |
| 5269 | struct diff_options *options = opt->value; |
| 5270 | char *s; |
| 5271 | |
| 5272 | BUG_ON_OPT_NEG(unset); |
| 5273 | |
Nguyễn Thái Ngọc Duy | 8ef0519 | 2019-05-29 16:11:15 +0700 | [diff] [blame] | 5274 | if (arg) { |
| 5275 | options->context = strtol(arg, &s, 10); |
| 5276 | if (*s) |
| 5277 | return error(_("%s expects a numerical value"), "--unified"); |
| 5278 | } |
Nguyễn Thái Ngọc Duy | d473e2e | 2019-01-27 07:35:33 +0700 | [diff] [blame] | 5279 | enable_patch_output(&options->output_format); |
| 5280 | |
| 5281 | return 0; |
| 5282 | } |
| 5283 | |
Nguyễn Thái Ngọc Duy | e9fb39b | 2019-03-05 19:30:18 +0700 | [diff] [blame] | 5284 | static int diff_opt_word_diff(const struct option *opt, |
| 5285 | const char *arg, int unset) |
| 5286 | { |
| 5287 | struct diff_options *options = opt->value; |
| 5288 | |
| 5289 | BUG_ON_OPT_NEG(unset); |
| 5290 | if (arg) { |
| 5291 | if (!strcmp(arg, "plain")) |
| 5292 | options->word_diff = DIFF_WORDS_PLAIN; |
| 5293 | else if (!strcmp(arg, "color")) { |
| 5294 | options->use_color = 1; |
| 5295 | options->word_diff = DIFF_WORDS_COLOR; |
| 5296 | } |
| 5297 | else if (!strcmp(arg, "porcelain")) |
| 5298 | options->word_diff = DIFF_WORDS_PORCELAIN; |
| 5299 | else if (!strcmp(arg, "none")) |
| 5300 | options->word_diff = DIFF_WORDS_NONE; |
| 5301 | else |
| 5302 | return error(_("bad --word-diff argument: %s"), arg); |
| 5303 | } else { |
| 5304 | if (options->word_diff == DIFF_WORDS_NONE) |
| 5305 | options->word_diff = DIFF_WORDS_PLAIN; |
| 5306 | } |
| 5307 | return 0; |
| 5308 | } |
| 5309 | |
Nguyễn Thái Ngọc Duy | 797df11 | 2019-03-05 19:30:19 +0700 | [diff] [blame] | 5310 | static int diff_opt_word_diff_regex(const struct option *opt, |
| 5311 | const char *arg, int unset) |
| 5312 | { |
| 5313 | struct diff_options *options = opt->value; |
| 5314 | |
| 5315 | BUG_ON_OPT_NEG(unset); |
| 5316 | if (options->word_diff == DIFF_WORDS_NONE) |
| 5317 | options->word_diff = DIFF_WORDS_PLAIN; |
| 5318 | options->word_regex = arg; |
| 5319 | return 0; |
| 5320 | } |
| 5321 | |
Nguyễn Thái Ngọc Duy | 4a28847 | 2019-01-27 07:35:31 +0700 | [diff] [blame] | 5322 | static void prep_parse_options(struct diff_options *options) |
| 5323 | { |
| 5324 | struct option parseopts[] = { |
Nguyễn Thái Ngọc Duy | cc013c2 | 2019-01-27 07:35:32 +0700 | [diff] [blame] | 5325 | OPT_GROUP(N_("Diff output format options")), |
| 5326 | OPT_BITOP('p', "patch", &options->output_format, |
| 5327 | N_("generate patch"), |
| 5328 | DIFF_FORMAT_PATCH, DIFF_FORMAT_NO_OUTPUT), |
Nguyễn Thái Ngọc Duy | e01df7a | 2019-02-21 18:16:09 +0700 | [diff] [blame] | 5329 | OPT_BIT_F('s', "no-patch", &options->output_format, |
| 5330 | N_("suppress diff output"), |
| 5331 | DIFF_FORMAT_NO_OUTPUT, PARSE_OPT_NONEG), |
Nguyễn Thái Ngọc Duy | cc013c2 | 2019-01-27 07:35:32 +0700 | [diff] [blame] | 5332 | OPT_BITOP('u', NULL, &options->output_format, |
| 5333 | N_("generate patch"), |
| 5334 | DIFF_FORMAT_PATCH, DIFF_FORMAT_NO_OUTPUT), |
Nguyễn Thái Ngọc Duy | d473e2e | 2019-01-27 07:35:33 +0700 | [diff] [blame] | 5335 | OPT_CALLBACK_F('U', "unified", options, N_("<n>"), |
| 5336 | N_("generate diffs with <n> lines context"), |
Junio C Hamano | 20aa7c5 | 2019-05-30 10:50:44 -0700 | [diff] [blame] | 5337 | PARSE_OPT_NONEG | PARSE_OPT_OPTARG, diff_opt_unified), |
Nguyễn Thái Ngọc Duy | 7fd9a1b | 2019-01-27 07:35:34 +0700 | [diff] [blame] | 5338 | OPT_BOOL('W', "function-context", &options->flags.funccontext, |
| 5339 | N_("generate diffs with <n> lines context")), |
Nguyễn Thái Ngọc Duy | ed88148 | 2019-01-27 07:35:35 +0700 | [diff] [blame] | 5340 | OPT_BIT_F(0, "raw", &options->output_format, |
| 5341 | N_("generate the diff in raw format"), |
| 5342 | DIFF_FORMAT_RAW, PARSE_OPT_NONEG), |
Nguyễn Thái Ngọc Duy | c659f30 | 2019-02-16 18:36:35 +0700 | [diff] [blame] | 5343 | OPT_BITOP(0, "patch-with-raw", &options->output_format, |
| 5344 | N_("synonym for '-p --raw'"), |
| 5345 | DIFF_FORMAT_PATCH | DIFF_FORMAT_RAW, |
| 5346 | DIFF_FORMAT_NO_OUTPUT), |
Nguyễn Thái Ngọc Duy | e550f58 | 2019-02-21 18:16:06 +0700 | [diff] [blame] | 5347 | OPT_BITOP(0, "patch-with-stat", &options->output_format, |
| 5348 | N_("synonym for '-p --stat'"), |
| 5349 | DIFF_FORMAT_PATCH | DIFF_FORMAT_DIFFSTAT, |
| 5350 | DIFF_FORMAT_NO_OUTPUT), |
Nguyễn Thái Ngọc Duy | e567362 | 2019-02-16 18:36:36 +0700 | [diff] [blame] | 5351 | OPT_BIT_F(0, "numstat", &options->output_format, |
| 5352 | N_("machine friendly --stat"), |
| 5353 | DIFF_FORMAT_NUMSTAT, PARSE_OPT_NONEG), |
| 5354 | OPT_BIT_F(0, "shortstat", &options->output_format, |
| 5355 | N_("output only the last line of --stat"), |
| 5356 | DIFF_FORMAT_SHORTSTAT, PARSE_OPT_NONEG), |
Nguyễn Thái Ngọc Duy | 4ce7aab | 2019-02-21 18:16:03 +0700 | [diff] [blame] | 5357 | OPT_CALLBACK_F('X', "dirstat", options, N_("<param1,param2>..."), |
| 5358 | N_("output the distribution of relative amount of changes for each sub-directory"), |
| 5359 | PARSE_OPT_NONEG | PARSE_OPT_OPTARG, |
| 5360 | diff_opt_dirstat), |
| 5361 | OPT_CALLBACK_F(0, "cumulative", options, NULL, |
| 5362 | N_("synonym for --dirstat=cumulative"), |
| 5363 | PARSE_OPT_NONEG | PARSE_OPT_NOARG, |
| 5364 | diff_opt_dirstat), |
| 5365 | OPT_CALLBACK_F(0, "dirstat-by-file", options, N_("<param1,param2>..."), |
| 5366 | N_("synonym for --dirstat=files,param1,param2..."), |
| 5367 | PARSE_OPT_NONEG | PARSE_OPT_OPTARG, |
| 5368 | diff_opt_dirstat), |
Nguyễn Thái Ngọc Duy | fc6af3e | 2019-02-21 18:16:04 +0700 | [diff] [blame] | 5369 | OPT_BIT_F(0, "check", &options->output_format, |
| 5370 | N_("warn if changes introduce conflict markers or whitespace errors"), |
| 5371 | DIFF_FORMAT_CHECKDIFF, PARSE_OPT_NONEG), |
Nguyễn Thái Ngọc Duy | 70a3041 | 2019-02-21 18:16:05 +0700 | [diff] [blame] | 5372 | OPT_BIT_F(0, "summary", &options->output_format, |
| 5373 | N_("condensed summary such as creations, renames and mode changes"), |
| 5374 | DIFF_FORMAT_SUMMARY, PARSE_OPT_NONEG), |
Nguyễn Thái Ngọc Duy | 0e840e2 | 2019-02-21 18:16:07 +0700 | [diff] [blame] | 5375 | OPT_BIT_F(0, "name-only", &options->output_format, |
| 5376 | N_("show only names of changed files"), |
| 5377 | DIFF_FORMAT_NAME, PARSE_OPT_NONEG), |
Nguyễn Thái Ngọc Duy | a238747 | 2019-02-21 18:16:08 +0700 | [diff] [blame] | 5378 | OPT_BIT_F(0, "name-status", &options->output_format, |
| 5379 | N_("show only names and status of changed files"), |
| 5380 | DIFF_FORMAT_NAME_STATUS, PARSE_OPT_NONEG), |
Nguyễn Thái Ngọc Duy | 84b5089 | 2019-02-21 18:16:10 +0700 | [diff] [blame] | 5381 | OPT_CALLBACK_F(0, "stat", options, N_("<width>[,<name-width>[,<count>]]"), |
| 5382 | N_("generate diffstat"), |
| 5383 | PARSE_OPT_NONEG | PARSE_OPT_OPTARG, diff_opt_stat), |
| 5384 | OPT_CALLBACK_F(0, "stat-width", options, N_("<width>"), |
| 5385 | N_("generate diffstat with a given width"), |
| 5386 | PARSE_OPT_NONEG, diff_opt_stat), |
| 5387 | OPT_CALLBACK_F(0, "stat-name-width", options, N_("<width>"), |
| 5388 | N_("generate diffstat with a given name width"), |
| 5389 | PARSE_OPT_NONEG, diff_opt_stat), |
| 5390 | OPT_CALLBACK_F(0, "stat-graph-width", options, N_("<width>"), |
| 5391 | N_("generate diffstat with a given graph width"), |
| 5392 | PARSE_OPT_NONEG, diff_opt_stat), |
| 5393 | OPT_CALLBACK_F(0, "stat-count", options, N_("<count>"), |
| 5394 | N_("generate diffstat with limited lines"), |
| 5395 | PARSE_OPT_NONEG, diff_opt_stat), |
Nguyễn Thái Ngọc Duy | 7d7942b | 2019-02-21 18:16:11 +0700 | [diff] [blame] | 5396 | OPT_CALLBACK_F(0, "compact-summary", options, NULL, |
| 5397 | N_("generate compact summary in diffstat"), |
| 5398 | PARSE_OPT_NOARG, diff_opt_compact_summary), |
Nguyễn Thái Ngọc Duy | 6d9af6f | 2019-03-05 19:30:12 +0700 | [diff] [blame] | 5399 | OPT_CALLBACK_F(0, "binary", options, NULL, |
| 5400 | N_("output a binary diff that can be applied"), |
| 5401 | PARSE_OPT_NONEG | PARSE_OPT_NOARG, diff_opt_binary), |
Nguyễn Thái Ngọc Duy | 4fe0167 | 2019-03-05 19:30:13 +0700 | [diff] [blame] | 5402 | OPT_BOOL(0, "full-index", &options->flags.full_index, |
| 5403 | N_("show full pre- and post-image object names on the \"index\" lines")), |
Nguyễn Thái Ngọc Duy | 8b81c26 | 2019-03-05 19:30:17 +0700 | [diff] [blame] | 5404 | OPT_COLOR_FLAG(0, "color", &options->use_color, |
| 5405 | N_("show colored diff")), |
Nguyễn Thái Ngọc Duy | 5a749d9 | 2019-03-24 15:19:55 +0700 | [diff] [blame] | 5406 | OPT_CALLBACK_F(0, "ws-error-highlight", options, N_("<kind>"), |
| 5407 | N_("highlight whitespace errors in the 'context', 'old' or 'new' lines in the diff"), |
| 5408 | PARSE_OPT_NONEG, diff_opt_ws_error_highlight), |
Nguyễn Thái Ngọc Duy | 1a1eb16 | 2019-03-24 15:19:57 +0700 | [diff] [blame] | 5409 | OPT_SET_INT('z', NULL, &options->line_termination, |
| 5410 | N_("do not munge pathnames and use NULs as output field terminators in --raw or --numstat"), |
| 5411 | 0), |
Nguyễn Thái Ngọc Duy | d877418 | 2019-03-24 15:20:04 +0700 | [diff] [blame] | 5412 | OPT__ABBREV(&options->abbrev), |
Nguyễn Thái Ngọc Duy | 31fba9d | 2019-03-24 15:20:05 +0700 | [diff] [blame] | 5413 | OPT_STRING_F(0, "src-prefix", &options->a_prefix, N_("<prefix>"), |
| 5414 | N_("show the given source prefix instead of \"a/\""), |
| 5415 | PARSE_OPT_NONEG), |
| 5416 | OPT_STRING_F(0, "dst-prefix", &options->b_prefix, N_("<prefix>"), |
Jiang Xin | 8a1569d | 2019-06-02 23:11:22 +0800 | [diff] [blame] | 5417 | N_("show the given destination prefix instead of \"b/\""), |
Nguyễn Thái Ngọc Duy | 31fba9d | 2019-03-24 15:20:05 +0700 | [diff] [blame] | 5418 | PARSE_OPT_NONEG), |
Nguyễn Thái Ngọc Duy | 2f81cf9 | 2019-03-24 15:20:06 +0700 | [diff] [blame] | 5419 | OPT_CALLBACK_F(0, "line-prefix", options, N_("<prefix>"), |
| 5420 | N_("prepend an additional prefix to every line of output"), |
| 5421 | PARSE_OPT_NONEG, diff_opt_line_prefix), |
Nguyễn Thái Ngọc Duy | 11c659d | 2019-03-24 15:20:07 +0700 | [diff] [blame] | 5422 | OPT_CALLBACK_F(0, "no-prefix", options, NULL, |
| 5423 | N_("do not show any source or destination prefix"), |
| 5424 | PARSE_OPT_NONEG | PARSE_OPT_NOARG, diff_opt_no_prefix), |
Nguyễn Thái Ngọc Duy | 16ed6c9 | 2019-03-24 15:20:08 +0700 | [diff] [blame] | 5425 | OPT_INTEGER_F(0, "inter-hunk-context", &options->interhunkcontext, |
| 5426 | N_("show context between diff hunks up to the specified number of lines"), |
| 5427 | PARSE_OPT_NONEG), |
Nguyễn Thái Ngọc Duy | af2f368 | 2019-02-21 18:16:12 +0700 | [diff] [blame] | 5428 | OPT_CALLBACK_F(0, "output-indicator-new", |
| 5429 | &options->output_indicators[OUTPUT_INDICATOR_NEW], |
| 5430 | N_("<char>"), |
| 5431 | N_("specify the character to indicate a new line instead of '+'"), |
| 5432 | PARSE_OPT_NONEG, diff_opt_char), |
| 5433 | OPT_CALLBACK_F(0, "output-indicator-old", |
| 5434 | &options->output_indicators[OUTPUT_INDICATOR_OLD], |
| 5435 | N_("<char>"), |
| 5436 | N_("specify the character to indicate an old line instead of '-'"), |
| 5437 | PARSE_OPT_NONEG, diff_opt_char), |
| 5438 | OPT_CALLBACK_F(0, "output-indicator-context", |
| 5439 | &options->output_indicators[OUTPUT_INDICATOR_CONTEXT], |
| 5440 | N_("<char>"), |
| 5441 | N_("specify the character to indicate a context instead of ' '"), |
| 5442 | PARSE_OPT_NONEG, diff_opt_char), |
| 5443 | |
Nguyễn Thái Ngọc Duy | ced4e17 | 2019-02-21 18:16:13 +0700 | [diff] [blame] | 5444 | OPT_GROUP(N_("Diff rename options")), |
| 5445 | OPT_CALLBACK_F('B', "break-rewrites", &options->break_opt, N_("<n>[/<m>]"), |
| 5446 | N_("break complete rewrite changes into pairs of delete and create"), |
| 5447 | PARSE_OPT_NONEG | PARSE_OPT_OPTARG, |
| 5448 | diff_opt_break_rewrites), |
Nguyễn Thái Ngọc Duy | f476308 | 2019-02-21 18:16:14 +0700 | [diff] [blame] | 5449 | OPT_CALLBACK_F('M', "find-renames", options, N_("<n>"), |
| 5450 | N_("detect renames"), |
| 5451 | PARSE_OPT_NONEG | PARSE_OPT_OPTARG, |
| 5452 | diff_opt_find_renames), |
Nguyễn Thái Ngọc Duy | 1e53329 | 2019-02-21 18:16:15 +0700 | [diff] [blame] | 5453 | OPT_SET_INT_F('D', "irreversible-delete", &options->irreversible_delete, |
| 5454 | N_("omit the preimage for deletes"), |
| 5455 | 1, PARSE_OPT_NONEG), |
Nguyễn Thái Ngọc Duy | 7f64850 | 2019-02-21 18:16:16 +0700 | [diff] [blame] | 5456 | OPT_CALLBACK_F('C', "find-copies", options, N_("<n>"), |
| 5457 | N_("detect copies"), |
| 5458 | PARSE_OPT_NONEG | PARSE_OPT_OPTARG, |
| 5459 | diff_opt_find_copies), |
Nguyễn Thái Ngọc Duy | bdd4741 | 2019-02-21 18:16:17 +0700 | [diff] [blame] | 5460 | OPT_BOOL(0, "find-copies-harder", &options->flags.find_copies_harder, |
| 5461 | N_("use unmodified files as source to find copies")), |
Nguyễn Thái Ngọc Duy | cdc43eb | 2019-02-21 18:16:18 +0700 | [diff] [blame] | 5462 | OPT_SET_INT_F(0, "no-renames", &options->detect_rename, |
| 5463 | N_("disable rename detection"), |
| 5464 | 0, PARSE_OPT_NONEG), |
| 5465 | OPT_BOOL(0, "rename-empty", &options->flags.rename_empty, |
| 5466 | N_("use empty blobs as rename source")), |
Nguyễn Thái Ngọc Duy | 1e9250b | 2019-03-05 19:30:16 +0700 | [diff] [blame] | 5467 | OPT_CALLBACK_F(0, "follow", options, NULL, |
| 5468 | N_("continue listing the history of a file beyond renames"), |
| 5469 | PARSE_OPT_NOARG, diff_opt_follow), |
Nguyễn Thái Ngọc Duy | bffee74 | 2019-03-24 15:19:58 +0700 | [diff] [blame] | 5470 | OPT_INTEGER('l', NULL, &options->rename_limit, |
| 5471 | N_("prevent rename/copy detection if the number of rename/copy targets exceeds given limit")), |
Nguyễn Thái Ngọc Duy | ced4e17 | 2019-02-21 18:16:13 +0700 | [diff] [blame] | 5472 | |
Nguyễn Thái Ngọc Duy | 2e75f92 | 2019-02-21 18:16:20 +0700 | [diff] [blame] | 5473 | OPT_GROUP(N_("Diff algorithm options")), |
| 5474 | OPT_BIT(0, "minimal", &options->xdl_opts, |
| 5475 | N_("produce the smallest possible diff"), |
| 5476 | XDF_NEED_MINIMAL), |
Nguyễn Thái Ngọc Duy | 87649a1 | 2019-02-21 18:16:21 +0700 | [diff] [blame] | 5477 | OPT_BIT_F('w', "ignore-all-space", &options->xdl_opts, |
| 5478 | N_("ignore whitespace when comparing lines"), |
| 5479 | XDF_IGNORE_WHITESPACE, PARSE_OPT_NONEG), |
| 5480 | OPT_BIT_F('b', "ignore-space-change", &options->xdl_opts, |
| 5481 | N_("ignore changes in amount of whitespace"), |
| 5482 | XDF_IGNORE_WHITESPACE_CHANGE, PARSE_OPT_NONEG), |
| 5483 | OPT_BIT_F(0, "ignore-space-at-eol", &options->xdl_opts, |
| 5484 | N_("ignore changes in whitespace at EOL"), |
| 5485 | XDF_IGNORE_WHITESPACE_AT_EOL, PARSE_OPT_NONEG), |
| 5486 | OPT_BIT_F(0, "ignore-cr-at-eol", &options->xdl_opts, |
| 5487 | N_("ignore carrier-return at the end of line"), |
| 5488 | XDF_IGNORE_CR_AT_EOL, PARSE_OPT_NONEG), |
| 5489 | OPT_BIT_F(0, "ignore-blank-lines", &options->xdl_opts, |
| 5490 | N_("ignore changes whose lines are all blank"), |
| 5491 | XDF_IGNORE_BLANK_LINES, PARSE_OPT_NONEG), |
Nguyễn Thái Ngọc Duy | 06f7751 | 2019-03-05 19:30:07 +0700 | [diff] [blame] | 5492 | OPT_BIT(0, "indent-heuristic", &options->xdl_opts, |
| 5493 | N_("heuristic to shift diff hunk boundaries for easy reading"), |
| 5494 | XDF_INDENT_HEURISTIC), |
Nguyễn Thái Ngọc Duy | 31fd640 | 2019-03-05 19:30:08 +0700 | [diff] [blame] | 5495 | OPT_CALLBACK_F(0, "patience", options, NULL, |
| 5496 | N_("generate diff using the \"patience diff\" algorithm"), |
| 5497 | PARSE_OPT_NONEG | PARSE_OPT_NOARG, |
| 5498 | diff_opt_patience), |
Nguyễn Thái Ngọc Duy | f1e68ef | 2019-03-05 19:30:09 +0700 | [diff] [blame] | 5499 | OPT_BITOP(0, "histogram", &options->xdl_opts, |
| 5500 | N_("generate diff using the \"histogram diff\" algorithm"), |
| 5501 | XDF_HISTOGRAM_DIFF, XDF_DIFF_ALGORITHM_MASK), |
Nguyễn Thái Ngọc Duy | 10f35b1 | 2019-03-05 19:30:10 +0700 | [diff] [blame] | 5502 | OPT_CALLBACK_F(0, "diff-algorithm", options, N_("<algorithm>"), |
| 5503 | N_("choose a diff algorithm"), |
| 5504 | PARSE_OPT_NONEG, diff_opt_diff_algorithm), |
Nguyễn Thái Ngọc Duy | df84a43 | 2019-03-05 19:30:11 +0700 | [diff] [blame] | 5505 | OPT_CALLBACK_F(0, "anchored", options, N_("<text>"), |
| 5506 | N_("generate diff using the \"anchored diff\" algorithm"), |
| 5507 | PARSE_OPT_NONEG, diff_opt_anchored), |
Nguyễn Thái Ngọc Duy | e9fb39b | 2019-03-05 19:30:18 +0700 | [diff] [blame] | 5508 | OPT_CALLBACK_F(0, "word-diff", options, N_("<mode>"), |
| 5509 | N_("show word diff, using <mode> to delimit changed words"), |
| 5510 | PARSE_OPT_NONEG | PARSE_OPT_OPTARG, diff_opt_word_diff), |
Nguyễn Thái Ngọc Duy | 797df11 | 2019-03-05 19:30:19 +0700 | [diff] [blame] | 5511 | OPT_CALLBACK_F(0, "word-diff-regex", options, N_("<regex>"), |
| 5512 | N_("use <regex> to decide what a word is"), |
| 5513 | PARSE_OPT_NONEG, diff_opt_word_diff_regex), |
Nguyễn Thái Ngọc Duy | 212db69 | 2019-03-05 19:30:20 +0700 | [diff] [blame] | 5514 | OPT_CALLBACK_F(0, "color-words", options, N_("<regex>"), |
| 5515 | N_("equivalent to --word-diff=color --word-diff-regex=<regex>"), |
| 5516 | PARSE_OPT_NONEG | PARSE_OPT_OPTARG, diff_opt_color_words), |
Nguyễn Thái Ngọc Duy | 59311a9 | 2019-03-24 15:20:09 +0700 | [diff] [blame] | 5517 | OPT_CALLBACK_F(0, "color-moved", options, N_("<mode>"), |
Jean-Noël Avila | 8bcd8f4 | 2019-05-17 21:26:19 +0200 | [diff] [blame] | 5518 | N_("moved lines of code are colored differently"), |
Nguyễn Thái Ngọc Duy | 59311a9 | 2019-03-24 15:20:09 +0700 | [diff] [blame] | 5519 | PARSE_OPT_OPTARG, diff_opt_color_moved), |
Nguyễn Thái Ngọc Duy | 8ce2020 | 2019-03-24 15:20:10 +0700 | [diff] [blame] | 5520 | OPT_CALLBACK_F(0, "color-moved-ws", options, N_("<mode>"), |
| 5521 | N_("how white spaces are ignored in --color-moved"), |
Nguyễn Thái Ngọc Duy | bb98729 | 2019-03-24 15:20:11 +0700 | [diff] [blame] | 5522 | 0, diff_opt_color_moved_ws), |
Nguyễn Thái Ngọc Duy | 2e75f92 | 2019-02-21 18:16:20 +0700 | [diff] [blame] | 5523 | |
Jean-Noël Avila | 8bcd8f4 | 2019-05-17 21:26:19 +0200 | [diff] [blame] | 5524 | OPT_GROUP(N_("Other diff options")), |
Nguyễn Thái Ngọc Duy | 0b1c5b5 | 2019-02-21 18:16:19 +0700 | [diff] [blame] | 5525 | OPT_CALLBACK_F(0, "relative", options, N_("<prefix>"), |
| 5526 | N_("when run from subdir, exclude changes outside and show relative paths"), |
Laurent Arnoud | c28ded8 | 2020-05-22 12:46:18 +0200 | [diff] [blame] | 5527 | PARSE_OPT_OPTARG, |
Nguyễn Thái Ngọc Duy | 0b1c5b5 | 2019-02-21 18:16:19 +0700 | [diff] [blame] | 5528 | diff_opt_relative), |
Nguyễn Thái Ngọc Duy | c37d4c0 | 2019-03-05 19:30:14 +0700 | [diff] [blame] | 5529 | OPT_BOOL('a', "text", &options->flags.text, |
| 5530 | N_("treat all files as text")), |
Nguyễn Thái Ngọc Duy | 4f99f29 | 2019-03-05 19:30:15 +0700 | [diff] [blame] | 5531 | OPT_BOOL('R', NULL, &options->flags.reverse_diff, |
| 5532 | N_("swap two inputs, reverse the diff")), |
Nguyễn Thái Ngọc Duy | 1086ea0 | 2019-03-05 19:30:21 +0700 | [diff] [blame] | 5533 | OPT_BOOL(0, "exit-code", &options->flags.exit_with_status, |
| 5534 | N_("exit with 1 if there were differences, 0 otherwise")), |
Nguyễn Thái Ngọc Duy | 9bbaf1c | 2019-03-05 19:30:22 +0700 | [diff] [blame] | 5535 | OPT_BOOL(0, "quiet", &options->flags.quick, |
| 5536 | N_("disable all output of the program")), |
Nguyễn Thái Ngọc Duy | 0cda100 | 2019-03-05 19:30:23 +0700 | [diff] [blame] | 5537 | OPT_BOOL(0, "ext-diff", &options->flags.allow_external, |
| 5538 | N_("allow an external diff helper to be executed")), |
Nguyễn Thái Ngọc Duy | 8ab7697 | 2019-03-05 19:30:24 +0700 | [diff] [blame] | 5539 | OPT_CALLBACK_F(0, "textconv", options, NULL, |
| 5540 | N_("run external text conversion filters when comparing binary files"), |
| 5541 | PARSE_OPT_NOARG, diff_opt_textconv), |
Nguyễn Thái Ngọc Duy | b680ee1 | 2019-03-05 19:30:25 +0700 | [diff] [blame] | 5542 | OPT_CALLBACK_F(0, "ignore-submodules", options, N_("<when>"), |
| 5543 | N_("ignore changes to submodules in the diff generation"), |
| 5544 | PARSE_OPT_NONEG | PARSE_OPT_OPTARG, |
| 5545 | diff_opt_ignore_submodules), |
Nguyễn Thái Ngọc Duy | 125dcea | 2019-03-05 19:30:26 +0700 | [diff] [blame] | 5546 | OPT_CALLBACK_F(0, "submodule", options, N_("<format>"), |
| 5547 | N_("specify how differences in submodules are shown"), |
| 5548 | PARSE_OPT_NONEG | PARSE_OPT_OPTARG, |
| 5549 | diff_opt_submodule), |
Nguyễn Thái Ngọc Duy | 96860e4 | 2019-03-24 15:19:56 +0700 | [diff] [blame] | 5550 | OPT_SET_INT_F(0, "ita-invisible-in-index", &options->ita_invisible_in_index, |
| 5551 | N_("hide 'git add -N' entries from the index"), |
| 5552 | 1, PARSE_OPT_NONEG), |
| 5553 | OPT_SET_INT_F(0, "ita-visible-in-index", &options->ita_invisible_in_index, |
| 5554 | N_("treat 'git add -N' entries as real in the index"), |
| 5555 | 0, PARSE_OPT_NONEG), |
Nguyễn Thái Ngọc Duy | a41cfb3 | 2019-03-24 15:19:59 +0700 | [diff] [blame] | 5556 | OPT_CALLBACK_F('S', NULL, options, N_("<string>"), |
| 5557 | N_("look for differences that change the number of occurrences of the specified string"), |
| 5558 | 0, diff_opt_pickaxe_string), |
| 5559 | OPT_CALLBACK_F('G', NULL, options, N_("<regex>"), |
| 5560 | N_("look for differences that change the number of occurrences of the specified regex"), |
| 5561 | 0, diff_opt_pickaxe_regex), |
Nguyễn Thái Ngọc Duy | 85f8e88 | 2019-03-24 15:20:00 +0700 | [diff] [blame] | 5562 | OPT_BIT_F(0, "pickaxe-all", &options->pickaxe_opts, |
| 5563 | N_("show all changes in the changeset with -S or -G"), |
| 5564 | DIFF_PICKAXE_ALL, PARSE_OPT_NONEG), |
| 5565 | OPT_BIT_F(0, "pickaxe-regex", &options->pickaxe_opts, |
| 5566 | N_("treat <string> in -S as extended POSIX regular expression"), |
| 5567 | DIFF_PICKAXE_REGEX, PARSE_OPT_NONEG), |
Nguyễn Thái Ngọc Duy | f731814 | 2019-03-24 15:20:01 +0700 | [diff] [blame] | 5568 | OPT_FILENAME('O', NULL, &options->orderfile, |
| 5569 | N_("control the order in which files appear in the output")), |
Nguyễn Thái Ngọc Duy | a75f28c | 2019-03-24 15:20:02 +0700 | [diff] [blame] | 5570 | OPT_CALLBACK_F(0, "find-object", options, N_("<object-id>"), |
| 5571 | N_("look for differences that change the number of occurrences of the specified object"), |
| 5572 | PARSE_OPT_NONEG, diff_opt_find_object), |
Nguyễn Thái Ngọc Duy | d2d3f27 | 2019-03-24 15:20:03 +0700 | [diff] [blame] | 5573 | OPT_CALLBACK_F(0, "diff-filter", options, N_("[(A|C|D|M|R|T|U|X|B)...[*]]"), |
| 5574 | N_("select files by diff type"), |
| 5575 | PARSE_OPT_NONEG, diff_opt_diff_filter), |
Nguyễn Thái Ngọc Duy | af2f368 | 2019-02-21 18:16:12 +0700 | [diff] [blame] | 5576 | { OPTION_CALLBACK, 0, "output", options, N_("<file>"), |
| 5577 | N_("Output to a specific file"), |
| 5578 | PARSE_OPT_NONEG, NULL, 0, diff_opt_output }, |
| 5579 | |
Nguyễn Thái Ngọc Duy | 4a28847 | 2019-01-27 07:35:31 +0700 | [diff] [blame] | 5580 | OPT_END() |
| 5581 | }; |
| 5582 | |
| 5583 | ALLOC_ARRAY(options->parseopts, ARRAY_SIZE(parseopts)); |
| 5584 | memcpy(options->parseopts, parseopts, sizeof(parseopts)); |
| 5585 | } |
| 5586 | |
Duy Nguyen | a97262c | 2016-01-21 18:48:44 +0700 | [diff] [blame] | 5587 | int diff_opt_parse(struct diff_options *options, |
| 5588 | const char **av, int ac, const char *prefix) |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 5589 | { |
Duy Nguyen | a97262c | 2016-01-21 18:48:44 +0700 | [diff] [blame] | 5590 | if (!prefix) |
| 5591 | prefix = ""; |
| 5592 | |
Nguyễn Thái Ngọc Duy | 4a28847 | 2019-01-27 07:35:31 +0700 | [diff] [blame] | 5593 | ac = parse_options(ac, av, prefix, options->parseopts, NULL, |
| 5594 | PARSE_OPT_KEEP_DASHDASH | |
| 5595 | PARSE_OPT_KEEP_UNKNOWN | |
| 5596 | PARSE_OPT_NO_INTERNAL_HELP | |
| 5597 | PARSE_OPT_ONE_SHOT | |
| 5598 | PARSE_OPT_STOP_AT_NON_OPTION); |
| 5599 | |
Nguyễn Thái Ngọc Duy | 8ce2020 | 2019-03-24 15:20:10 +0700 | [diff] [blame] | 5600 | return ac; |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 5601 | } |
| 5602 | |
Kevin Ballard | 10ae752 | 2010-09-27 16:58:25 -0700 | [diff] [blame] | 5603 | int parse_rename_score(const char **cp_p) |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 5604 | { |
| 5605 | unsigned long num, scale; |
| 5606 | int ch, dot; |
| 5607 | const char *cp = *cp_p; |
| 5608 | |
| 5609 | num = 0; |
| 5610 | scale = 1; |
| 5611 | dot = 0; |
Brian Gianforcaro | eeefa7c | 2009-09-01 01:35:10 -0400 | [diff] [blame] | 5612 | for (;;) { |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 5613 | ch = *cp; |
| 5614 | if ( !dot && ch == '.' ) { |
| 5615 | scale = 1; |
| 5616 | dot = 1; |
| 5617 | } else if ( ch == '%' ) { |
| 5618 | scale = dot ? scale*100 : 100; |
| 5619 | cp++; /* % is always at the end */ |
| 5620 | break; |
| 5621 | } else if ( ch >= '0' && ch <= '9' ) { |
| 5622 | if ( scale < 100000 ) { |
| 5623 | scale *= 10; |
| 5624 | num = (num*10) + (ch-'0'); |
| 5625 | } |
| 5626 | } else { |
| 5627 | break; |
| 5628 | } |
| 5629 | cp++; |
| 5630 | } |
| 5631 | *cp_p = cp; |
| 5632 | |
| 5633 | /* user says num divided by scale and we say internally that |
| 5634 | * is MAX_SCORE * num / scale. |
| 5635 | */ |
Shawn O. Pearce | dc49cd7 | 2007-03-06 20:44:37 -0500 | [diff] [blame] | 5636 | return (int)((num >= scale) ? MAX_SCORE : (MAX_SCORE * num / scale)); |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 5637 | } |
| 5638 | |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 5639 | struct diff_queue_struct diff_queued_diff; |
| 5640 | |
| 5641 | void diff_q(struct diff_queue_struct *queue, struct diff_filepair *dp) |
| 5642 | { |
Dmitry S. Dolzhenko | 4c960a4 | 2014-03-04 02:31:53 +0400 | [diff] [blame] | 5643 | ALLOC_GROW(queue->queue, queue->nr + 1, queue->alloc); |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 5644 | queue->queue[queue->nr++] = dp; |
| 5645 | } |
| 5646 | |
| 5647 | struct diff_filepair *diff_queue(struct diff_queue_struct *queue, |
| 5648 | struct diff_filespec *one, |
| 5649 | struct diff_filespec *two) |
| 5650 | { |
Junio C Hamano | ef67768 | 2006-08-03 12:01:01 -0700 | [diff] [blame] | 5651 | struct diff_filepair *dp = xcalloc(1, sizeof(*dp)); |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 5652 | dp->one = one; |
| 5653 | dp->two = two; |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 5654 | if (queue) |
| 5655 | diff_q(queue, dp); |
| 5656 | return dp; |
| 5657 | } |
| 5658 | |
| 5659 | void diff_free_filepair(struct diff_filepair *p) |
| 5660 | { |
Linus Torvalds | 9fb8841 | 2007-10-25 11:19:10 -0700 | [diff] [blame] | 5661 | free_filespec(p->one); |
| 5662 | free_filespec(p->two); |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 5663 | free(p); |
| 5664 | } |
| 5665 | |
Jeff King | d6cece5 | 2016-10-20 02:20:07 -0400 | [diff] [blame] | 5666 | const char *diff_aligned_abbrev(const struct object_id *oid, int len) |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 5667 | { |
| 5668 | int abblen; |
| 5669 | const char *abbrev; |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 5670 | |
Ann T Ropea | 7cb6ac1 | 2017-12-03 22:27:42 +0100 | [diff] [blame] | 5671 | /* Do we want all 40 hex characters? */ |
brian m. carlson | 02afca1 | 2018-07-16 01:28:05 +0000 | [diff] [blame] | 5672 | if (len == the_hash_algo->hexsz) |
Jeff King | d6cece5 | 2016-10-20 02:20:07 -0400 | [diff] [blame] | 5673 | return oid_to_hex(oid); |
| 5674 | |
Ann T Ropea | 7cb6ac1 | 2017-12-03 22:27:42 +0100 | [diff] [blame] | 5675 | /* An abbreviated value is fine, possibly followed by an ellipsis. */ |
Jeff King | 4f03666 | 2016-10-20 02:21:25 -0400 | [diff] [blame] | 5676 | abbrev = diff_abbrev_oid(oid, len); |
Ann T Ropea | 7cb6ac1 | 2017-12-03 22:27:42 +0100 | [diff] [blame] | 5677 | |
| 5678 | if (!print_sha1_ellipsis()) |
| 5679 | return abbrev; |
| 5680 | |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 5681 | abblen = strlen(abbrev); |
Junio C Hamano | d709f1f | 2016-09-30 10:42:05 -0700 | [diff] [blame] | 5682 | |
| 5683 | /* |
Ann T Ropea | 7cb6ac1 | 2017-12-03 22:27:42 +0100 | [diff] [blame] | 5684 | * In well-behaved cases, where the abbreviated result is the |
Junio C Hamano | d709f1f | 2016-09-30 10:42:05 -0700 | [diff] [blame] | 5685 | * same as the requested length, append three dots after the |
| 5686 | * abbreviation (hence the whole logic is limited to the case |
| 5687 | * where abblen < 37); when the actual abbreviated result is a |
| 5688 | * bit longer than the requested length, we reduce the number |
| 5689 | * of dots so that they match the well-behaved ones. However, |
| 5690 | * if the actual abbreviation is longer than the requested |
| 5691 | * length by more than three, we give up on aligning, and add |
| 5692 | * three dots anyway, to indicate that the output is not the |
| 5693 | * full object name. Yes, this may be suboptimal, but this |
| 5694 | * appears only in "diff --raw --abbrev" output and it is not |
| 5695 | * worth the effort to change it now. Note that this would |
| 5696 | * likely to work fine when the automatic sizing of default |
| 5697 | * abbreviation length is used--we would be fed -1 in "len" in |
| 5698 | * that case, and will end up always appending three-dots, but |
| 5699 | * the automatic sizing is supposed to give abblen that ensures |
| 5700 | * uniqueness across all objects (statistically speaking). |
| 5701 | */ |
brian m. carlson | 02afca1 | 2018-07-16 01:28:05 +0000 | [diff] [blame] | 5702 | if (abblen < the_hash_algo->hexsz - 3) { |
brian m. carlson | dc01505 | 2017-03-26 16:01:24 +0000 | [diff] [blame] | 5703 | static char hex[GIT_MAX_HEXSZ + 1]; |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 5704 | if (len < abblen && abblen <= len + 2) |
Jeff King | 5096d49 | 2015-09-24 17:06:08 -0400 | [diff] [blame] | 5705 | xsnprintf(hex, sizeof(hex), "%s%.*s", abbrev, len+3-abblen, ".."); |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 5706 | else |
Jeff King | 5096d49 | 2015-09-24 17:06:08 -0400 | [diff] [blame] | 5707 | xsnprintf(hex, sizeof(hex), "%s...", abbrev); |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 5708 | return hex; |
| 5709 | } |
Jeff King | d6cece5 | 2016-10-20 02:20:07 -0400 | [diff] [blame] | 5710 | |
| 5711 | return oid_to_hex(oid); |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 5712 | } |
| 5713 | |
Pierre Habouzit | 663af34 | 2007-09-20 00:42:15 +0200 | [diff] [blame] | 5714 | static void diff_flush_raw(struct diff_filepair *p, struct diff_options *opt) |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 5715 | { |
Pierre Habouzit | 663af34 | 2007-09-20 00:42:15 +0200 | [diff] [blame] | 5716 | int line_termination = opt->line_termination; |
| 5717 | int inter_name_termination = line_termination ? '\t' : '\0'; |
Timo Hirvonen | c674434 | 2006-06-24 20:21:53 +0300 | [diff] [blame] | 5718 | |
John Keeping | 30997bb | 2013-02-07 20:15:27 +0000 | [diff] [blame] | 5719 | fprintf(opt->file, "%s", diff_line_prefix(opt)); |
Pierre Habouzit | 663af34 | 2007-09-20 00:42:15 +0200 | [diff] [blame] | 5720 | if (!(opt->output_format & DIFF_FORMAT_NAME_STATUS)) { |
Daniel Barkalow | c0c7773 | 2008-03-09 22:43:39 -0400 | [diff] [blame] | 5721 | fprintf(opt->file, ":%06o %06o %s ", p->one->mode, p->two->mode, |
Jeff King | d6cece5 | 2016-10-20 02:20:07 -0400 | [diff] [blame] | 5722 | diff_aligned_abbrev(&p->one->oid, opt->abbrev)); |
brian m. carlson | a0d12c4 | 2016-06-24 23:09:23 +0000 | [diff] [blame] | 5723 | fprintf(opt->file, "%s ", |
Jeff King | d6cece5 | 2016-10-20 02:20:07 -0400 | [diff] [blame] | 5724 | diff_aligned_abbrev(&p->two->oid, opt->abbrev)); |
Pierre Habouzit | 663af34 | 2007-09-20 00:42:15 +0200 | [diff] [blame] | 5725 | } |
| 5726 | if (p->score) { |
Daniel Barkalow | c0c7773 | 2008-03-09 22:43:39 -0400 | [diff] [blame] | 5727 | fprintf(opt->file, "%c%03d%c", p->status, similarity_index(p), |
| 5728 | inter_name_termination); |
Pierre Habouzit | 663af34 | 2007-09-20 00:42:15 +0200 | [diff] [blame] | 5729 | } else { |
Daniel Barkalow | c0c7773 | 2008-03-09 22:43:39 -0400 | [diff] [blame] | 5730 | fprintf(opt->file, "%c%c", p->status, inter_name_termination); |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 5731 | } |
| 5732 | |
Junio C Hamano | cd676a5 | 2008-02-12 14:26:02 -0800 | [diff] [blame] | 5733 | if (p->status == DIFF_STATUS_COPIED || |
| 5734 | p->status == DIFF_STATUS_RENAMED) { |
| 5735 | const char *name_a, *name_b; |
| 5736 | name_a = p->one->path; |
| 5737 | name_b = p->two->path; |
| 5738 | strip_prefix(opt->prefix_length, &name_a, &name_b); |
Daniel Barkalow | c0c7773 | 2008-03-09 22:43:39 -0400 | [diff] [blame] | 5739 | write_name_quoted(name_a, opt->file, inter_name_termination); |
| 5740 | write_name_quoted(name_b, opt->file, line_termination); |
Pierre Habouzit | 663af34 | 2007-09-20 00:42:15 +0200 | [diff] [blame] | 5741 | } else { |
Junio C Hamano | cd676a5 | 2008-02-12 14:26:02 -0800 | [diff] [blame] | 5742 | const char *name_a, *name_b; |
| 5743 | name_a = p->one->mode ? p->one->path : p->two->path; |
| 5744 | name_b = NULL; |
| 5745 | strip_prefix(opt->prefix_length, &name_a, &name_b); |
Daniel Barkalow | c0c7773 | 2008-03-09 22:43:39 -0400 | [diff] [blame] | 5746 | write_name_quoted(name_a, opt->file, line_termination); |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 5747 | } |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 5748 | } |
| 5749 | |
| 5750 | int diff_unmodified_pair(struct diff_filepair *p) |
| 5751 | { |
| 5752 | /* This function is written stricter than necessary to support |
| 5753 | * the currently implemented transformers, but the idea is to |
| 5754 | * let transformers to produce diff_filepairs any way they want, |
| 5755 | * and filter and clean them up here before producing the output. |
| 5756 | */ |
Pierre Habouzit | 663af34 | 2007-09-20 00:42:15 +0200 | [diff] [blame] | 5757 | struct diff_filespec *one = p->one, *two = p->two; |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 5758 | |
| 5759 | if (DIFF_PAIR_UNMERGED(p)) |
| 5760 | return 0; /* unmerged is interesting */ |
| 5761 | |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 5762 | /* deletion, addition, mode or type change |
| 5763 | * and rename are all interesting. |
| 5764 | */ |
| 5765 | if (DIFF_FILE_VALID(one) != DIFF_FILE_VALID(two) || |
| 5766 | DIFF_PAIR_MODE_CHANGED(p) || |
| 5767 | strcmp(one->path, two->path)) |
| 5768 | return 0; |
| 5769 | |
| 5770 | /* both are valid and point at the same path. that is, we are |
| 5771 | * dealing with a change. |
| 5772 | */ |
brian m. carlson | 41c9560 | 2016-06-24 23:09:24 +0000 | [diff] [blame] | 5773 | if (one->oid_valid && two->oid_valid && |
Jeff King | 4a7e27e | 2018-08-28 17:22:40 -0400 | [diff] [blame] | 5774 | oideq(&one->oid, &two->oid) && |
Jens Lehmann | 85adbf2 | 2010-03-12 22:23:52 +0100 | [diff] [blame] | 5775 | !one->dirty_submodule && !two->dirty_submodule) |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 5776 | return 1; /* no change */ |
brian m. carlson | 41c9560 | 2016-06-24 23:09:24 +0000 | [diff] [blame] | 5777 | if (!one->oid_valid && !two->oid_valid) |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 5778 | return 1; /* both look at the same file on the filesystem. */ |
| 5779 | return 0; |
| 5780 | } |
| 5781 | |
| 5782 | static void diff_flush_patch(struct diff_filepair *p, struct diff_options *o) |
| 5783 | { |
| 5784 | if (diff_unmodified_pair(p)) |
| 5785 | return; |
| 5786 | |
| 5787 | if ((DIFF_FILE_VALID(p->one) && S_ISDIR(p->one->mode)) || |
| 5788 | (DIFF_FILE_VALID(p->two) && S_ISDIR(p->two->mode))) |
| 5789 | return; /* no tree diffs in patch format */ |
| 5790 | |
| 5791 | run_diff(p, o); |
| 5792 | } |
| 5793 | |
| 5794 | static void diff_flush_stat(struct diff_filepair *p, struct diff_options *o, |
| 5795 | struct diffstat_t *diffstat) |
| 5796 | { |
| 5797 | if (diff_unmodified_pair(p)) |
| 5798 | return; |
| 5799 | |
| 5800 | if ((DIFF_FILE_VALID(p->one) && S_ISDIR(p->one->mode)) || |
| 5801 | (DIFF_FILE_VALID(p->two) && S_ISDIR(p->two->mode))) |
Yann Dirson | c3fced6 | 2010-10-24 23:03:10 +0200 | [diff] [blame] | 5802 | return; /* no useful stat for tree diffs */ |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 5803 | |
| 5804 | run_diffstat(p, o, diffstat); |
| 5805 | } |
| 5806 | |
Johannes Schindelin | 8824689 | 2006-05-20 23:43:13 +0200 | [diff] [blame] | 5807 | static void diff_flush_checkdiff(struct diff_filepair *p, |
| 5808 | struct diff_options *o) |
| 5809 | { |
| 5810 | if (diff_unmodified_pair(p)) |
| 5811 | return; |
| 5812 | |
| 5813 | if ((DIFF_FILE_VALID(p->one) && S_ISDIR(p->one->mode)) || |
| 5814 | (DIFF_FILE_VALID(p->two) && S_ISDIR(p->two->mode))) |
Yann Dirson | c3fced6 | 2010-10-24 23:03:10 +0200 | [diff] [blame] | 5815 | return; /* nothing to check in tree diffs */ |
Johannes Schindelin | 8824689 | 2006-05-20 23:43:13 +0200 | [diff] [blame] | 5816 | |
| 5817 | run_checkdiff(p, o); |
| 5818 | } |
| 5819 | |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 5820 | int diff_queue_is_empty(void) |
| 5821 | { |
| 5822 | struct diff_queue_struct *q = &diff_queued_diff; |
| 5823 | int i; |
| 5824 | for (i = 0; i < q->nr; i++) |
| 5825 | if (!diff_unmodified_pair(q->queue[i])) |
| 5826 | return 0; |
| 5827 | return 1; |
| 5828 | } |
| 5829 | |
| 5830 | #if DIFF_DEBUG |
| 5831 | void diff_debug_filespec(struct diff_filespec *s, int x, const char *one) |
| 5832 | { |
| 5833 | fprintf(stderr, "queue[%d] %s (%s) %s %06o %s\n", |
| 5834 | x, one ? one : "", |
| 5835 | s->path, |
| 5836 | DIFF_FILE_VALID(s) ? "valid" : "invalid", |
| 5837 | s->mode, |
brian m. carlson | 41c9560 | 2016-06-24 23:09:24 +0000 | [diff] [blame] | 5838 | s->oid_valid ? oid_to_hex(&s->oid) : ""); |
Jeff King | 428d52a | 2014-01-16 20:21:59 -0500 | [diff] [blame] | 5839 | fprintf(stderr, "queue[%d] %s size %lu\n", |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 5840 | x, one ? one : "", |
Jeff King | 428d52a | 2014-01-16 20:21:59 -0500 | [diff] [blame] | 5841 | s->size); |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 5842 | } |
| 5843 | |
| 5844 | void diff_debug_filepair(const struct diff_filepair *p, int i) |
| 5845 | { |
| 5846 | diff_debug_filespec(p->one, i, "one"); |
| 5847 | diff_debug_filespec(p->two, i, "two"); |
Linus Torvalds | 6447971 | 2007-10-25 11:20:56 -0700 | [diff] [blame] | 5848 | fprintf(stderr, "score %d, status %c rename_used %d broken %d\n", |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 5849 | p->score, p->status ? p->status : '?', |
Linus Torvalds | 6447971 | 2007-10-25 11:20:56 -0700 | [diff] [blame] | 5850 | p->one->rename_used, p->broken_pair); |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 5851 | } |
| 5852 | |
| 5853 | void diff_debug_queue(const char *msg, struct diff_queue_struct *q) |
| 5854 | { |
| 5855 | int i; |
| 5856 | if (msg) |
| 5857 | fprintf(stderr, "%s\n", msg); |
| 5858 | fprintf(stderr, "q->nr = %d\n", q->nr); |
| 5859 | for (i = 0; i < q->nr; i++) { |
| 5860 | struct diff_filepair *p = q->queue[i]; |
| 5861 | diff_debug_filepair(p, i); |
| 5862 | } |
| 5863 | } |
| 5864 | #endif |
| 5865 | |
| 5866 | static void diff_resolve_rename_copy(void) |
| 5867 | { |
Linus Torvalds | 6447971 | 2007-10-25 11:20:56 -0700 | [diff] [blame] | 5868 | int i; |
| 5869 | struct diff_filepair *p; |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 5870 | struct diff_queue_struct *q = &diff_queued_diff; |
| 5871 | |
| 5872 | diff_debug_queue("resolve-rename-copy", q); |
| 5873 | |
| 5874 | for (i = 0; i < q->nr; i++) { |
| 5875 | p = q->queue[i]; |
| 5876 | p->status = 0; /* undecided */ |
| 5877 | if (DIFF_PAIR_UNMERGED(p)) |
| 5878 | p->status = DIFF_STATUS_UNMERGED; |
| 5879 | else if (!DIFF_FILE_VALID(p->one)) |
| 5880 | p->status = DIFF_STATUS_ADDED; |
| 5881 | else if (!DIFF_FILE_VALID(p->two)) |
| 5882 | p->status = DIFF_STATUS_DELETED; |
| 5883 | else if (DIFF_PAIR_TYPE_CHANGED(p)) |
| 5884 | p->status = DIFF_STATUS_TYPE_CHANGED; |
| 5885 | |
| 5886 | /* from this point on, we are dealing with a pair |
| 5887 | * whose both sides are valid and of the same type, i.e. |
| 5888 | * either in-place edit or rename/copy edit. |
| 5889 | */ |
| 5890 | else if (DIFF_PAIR_RENAME(p)) { |
Linus Torvalds | 6447971 | 2007-10-25 11:20:56 -0700 | [diff] [blame] | 5891 | /* |
| 5892 | * A rename might have re-connected a broken |
| 5893 | * pair up, causing the pathnames to be the |
| 5894 | * same again. If so, that's not a rename at |
| 5895 | * all, just a modification.. |
| 5896 | * |
| 5897 | * Otherwise, see if this source was used for |
| 5898 | * multiple renames, in which case we decrement |
| 5899 | * the count, and call it a copy. |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 5900 | */ |
Linus Torvalds | 6447971 | 2007-10-25 11:20:56 -0700 | [diff] [blame] | 5901 | if (!strcmp(p->one->path, p->two->path)) |
| 5902 | p->status = DIFF_STATUS_MODIFIED; |
| 5903 | else if (--p->one->rename_used > 0) |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 5904 | p->status = DIFF_STATUS_COPIED; |
Linus Torvalds | 6447971 | 2007-10-25 11:20:56 -0700 | [diff] [blame] | 5905 | else |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 5906 | p->status = DIFF_STATUS_RENAMED; |
| 5907 | } |
Jeff King | 9001dc2 | 2018-08-28 17:22:48 -0400 | [diff] [blame] | 5908 | else if (!oideq(&p->one->oid, &p->two->oid) || |
Johannes Schindelin | d516c2d | 2007-02-22 21:50:10 +0100 | [diff] [blame] | 5909 | p->one->mode != p->two->mode || |
Jens Lehmann | 85adbf2 | 2010-03-12 22:23:52 +0100 | [diff] [blame] | 5910 | p->one->dirty_submodule || |
| 5911 | p->two->dirty_submodule || |
brian m. carlson | a0d12c4 | 2016-06-24 23:09:23 +0000 | [diff] [blame] | 5912 | is_null_oid(&p->one->oid)) |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 5913 | p->status = DIFF_STATUS_MODIFIED; |
| 5914 | else { |
| 5915 | /* This is a "no-change" entry and should not |
| 5916 | * happen anymore, but prepare for broken callers. |
| 5917 | */ |
| 5918 | error("feeding unmodified %s to diffcore", |
| 5919 | p->one->path); |
| 5920 | p->status = DIFF_STATUS_UNKNOWN; |
| 5921 | } |
| 5922 | } |
| 5923 | diff_debug_queue("resolve-rename-copy done", q); |
| 5924 | } |
| 5925 | |
Timo Hirvonen | c674434 | 2006-06-24 20:21:53 +0300 | [diff] [blame] | 5926 | static int check_pair_status(struct diff_filepair *p) |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 5927 | { |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 5928 | switch (p->status) { |
| 5929 | case DIFF_STATUS_UNKNOWN: |
Timo Hirvonen | c674434 | 2006-06-24 20:21:53 +0300 | [diff] [blame] | 5930 | return 0; |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 5931 | case 0: |
| 5932 | die("internal error in diff-resolve-rename-copy"); |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 5933 | default: |
Timo Hirvonen | c674434 | 2006-06-24 20:21:53 +0300 | [diff] [blame] | 5934 | return 1; |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 5935 | } |
| 5936 | } |
| 5937 | |
Timo Hirvonen | c674434 | 2006-06-24 20:21:53 +0300 | [diff] [blame] | 5938 | static void flush_one_pair(struct diff_filepair *p, struct diff_options *opt) |
| 5939 | { |
| 5940 | int fmt = opt->output_format; |
| 5941 | |
| 5942 | if (fmt & DIFF_FORMAT_CHECKDIFF) |
| 5943 | diff_flush_checkdiff(p, opt); |
| 5944 | else if (fmt & (DIFF_FORMAT_RAW | DIFF_FORMAT_NAME_STATUS)) |
| 5945 | diff_flush_raw(p, opt); |
Junio C Hamano | cd676a5 | 2008-02-12 14:26:02 -0800 | [diff] [blame] | 5946 | else if (fmt & DIFF_FORMAT_NAME) { |
| 5947 | const char *name_a, *name_b; |
| 5948 | name_a = p->two->path; |
| 5949 | name_b = NULL; |
| 5950 | strip_prefix(opt->prefix_length, &name_a, &name_b); |
Jeff King | f5022b5 | 2017-02-08 15:31:15 -0500 | [diff] [blame] | 5951 | fprintf(opt->file, "%s", diff_line_prefix(opt)); |
Daniel Barkalow | c0c7773 | 2008-03-09 22:43:39 -0400 | [diff] [blame] | 5952 | write_name_quoted(name_a, opt->file, opt->line_termination); |
Junio C Hamano | cd676a5 | 2008-02-12 14:26:02 -0800 | [diff] [blame] | 5953 | } |
Timo Hirvonen | c674434 | 2006-06-24 20:21:53 +0300 | [diff] [blame] | 5954 | } |
| 5955 | |
Stefan Beller | 146fdb0 | 2017-06-29 17:07:05 -0700 | [diff] [blame] | 5956 | static void show_file_mode_name(struct diff_options *opt, const char *newdelete, struct diff_filespec *fs) |
Sean | 4bbd261 | 2006-05-14 08:13:49 -0400 | [diff] [blame] | 5957 | { |
Stefan Beller | 146fdb0 | 2017-06-29 17:07:05 -0700 | [diff] [blame] | 5958 | struct strbuf sb = STRBUF_INIT; |
Sean | 4bbd261 | 2006-05-14 08:13:49 -0400 | [diff] [blame] | 5959 | if (fs->mode) |
Stefan Beller | 146fdb0 | 2017-06-29 17:07:05 -0700 | [diff] [blame] | 5960 | strbuf_addf(&sb, " %s mode %06o ", newdelete, fs->mode); |
Sean | 4bbd261 | 2006-05-14 08:13:49 -0400 | [diff] [blame] | 5961 | else |
Stefan Beller | 146fdb0 | 2017-06-29 17:07:05 -0700 | [diff] [blame] | 5962 | strbuf_addf(&sb, " %s ", newdelete); |
| 5963 | |
| 5964 | quote_c_style(fs->path, &sb, NULL, 0); |
| 5965 | strbuf_addch(&sb, '\n'); |
| 5966 | emit_diff_symbol(opt, DIFF_SYMBOL_SUMMARY, |
| 5967 | sb.buf, sb.len, 0); |
| 5968 | strbuf_release(&sb); |
Sean | 4bbd261 | 2006-05-14 08:13:49 -0400 | [diff] [blame] | 5969 | } |
| 5970 | |
Stefan Beller | 146fdb0 | 2017-06-29 17:07:05 -0700 | [diff] [blame] | 5971 | static void show_mode_change(struct diff_options *opt, struct diff_filepair *p, |
| 5972 | int show_name) |
Sean | 4bbd261 | 2006-05-14 08:13:49 -0400 | [diff] [blame] | 5973 | { |
| 5974 | if (p->one->mode && p->two->mode && p->one->mode != p->two->mode) { |
Stefan Beller | 146fdb0 | 2017-06-29 17:07:05 -0700 | [diff] [blame] | 5975 | struct strbuf sb = STRBUF_INIT; |
| 5976 | strbuf_addf(&sb, " mode change %06o => %06o", |
| 5977 | p->one->mode, p->two->mode); |
Alexandre Julliard | 0d26a64 | 2007-02-10 15:37:48 +0100 | [diff] [blame] | 5978 | if (show_name) { |
Stefan Beller | 146fdb0 | 2017-06-29 17:07:05 -0700 | [diff] [blame] | 5979 | strbuf_addch(&sb, ' '); |
| 5980 | quote_c_style(p->two->path, &sb, NULL, 0); |
Alexandre Julliard | 0d26a64 | 2007-02-10 15:37:48 +0100 | [diff] [blame] | 5981 | } |
Stefan Beller | 58aaced | 2017-09-27 15:51:26 -0700 | [diff] [blame] | 5982 | strbuf_addch(&sb, '\n'); |
Stefan Beller | 146fdb0 | 2017-06-29 17:07:05 -0700 | [diff] [blame] | 5983 | emit_diff_symbol(opt, DIFF_SYMBOL_SUMMARY, |
| 5984 | sb.buf, sb.len, 0); |
| 5985 | strbuf_release(&sb); |
Sean | 4bbd261 | 2006-05-14 08:13:49 -0400 | [diff] [blame] | 5986 | } |
| 5987 | } |
| 5988 | |
Stefan Beller | 146fdb0 | 2017-06-29 17:07:05 -0700 | [diff] [blame] | 5989 | static void show_rename_copy(struct diff_options *opt, const char *renamecopy, |
| 5990 | struct diff_filepair *p) |
Sean | 4bbd261 | 2006-05-14 08:13:49 -0400 | [diff] [blame] | 5991 | { |
Stefan Beller | 146fdb0 | 2017-06-29 17:07:05 -0700 | [diff] [blame] | 5992 | struct strbuf sb = STRBUF_INIT; |
Nguyễn Thái Ngọc Duy | c905cbc | 2018-02-01 20:02:20 +0700 | [diff] [blame] | 5993 | struct strbuf names = STRBUF_INIT; |
| 5994 | |
| 5995 | pprint_rename(&names, p->one->path, p->two->path); |
Stefan Beller | 146fdb0 | 2017-06-29 17:07:05 -0700 | [diff] [blame] | 5996 | strbuf_addf(&sb, " %s %s (%d%%)\n", |
Nguyễn Thái Ngọc Duy | c905cbc | 2018-02-01 20:02:20 +0700 | [diff] [blame] | 5997 | renamecopy, names.buf, similarity_index(p)); |
| 5998 | strbuf_release(&names); |
Stefan Beller | 146fdb0 | 2017-06-29 17:07:05 -0700 | [diff] [blame] | 5999 | emit_diff_symbol(opt, DIFF_SYMBOL_SUMMARY, |
| 6000 | sb.buf, sb.len, 0); |
| 6001 | show_mode_change(opt, p, 0); |
Rene Scharfe | 348eda2 | 2017-08-30 19:49:42 +0200 | [diff] [blame] | 6002 | strbuf_release(&sb); |
Sean | 4bbd261 | 2006-05-14 08:13:49 -0400 | [diff] [blame] | 6003 | } |
| 6004 | |
Bo Yang | 7be5761 | 2010-05-26 15:23:54 +0800 | [diff] [blame] | 6005 | static void diff_summary(struct diff_options *opt, struct diff_filepair *p) |
Sean | 4bbd261 | 2006-05-14 08:13:49 -0400 | [diff] [blame] | 6006 | { |
| 6007 | switch(p->status) { |
| 6008 | case DIFF_STATUS_DELETED: |
Stefan Beller | 146fdb0 | 2017-06-29 17:07:05 -0700 | [diff] [blame] | 6009 | show_file_mode_name(opt, "delete", p->one); |
Sean | 4bbd261 | 2006-05-14 08:13:49 -0400 | [diff] [blame] | 6010 | break; |
| 6011 | case DIFF_STATUS_ADDED: |
Stefan Beller | 146fdb0 | 2017-06-29 17:07:05 -0700 | [diff] [blame] | 6012 | show_file_mode_name(opt, "create", p->two); |
Sean | 4bbd261 | 2006-05-14 08:13:49 -0400 | [diff] [blame] | 6013 | break; |
| 6014 | case DIFF_STATUS_COPIED: |
Stefan Beller | 146fdb0 | 2017-06-29 17:07:05 -0700 | [diff] [blame] | 6015 | show_rename_copy(opt, "copy", p); |
Sean | 4bbd261 | 2006-05-14 08:13:49 -0400 | [diff] [blame] | 6016 | break; |
| 6017 | case DIFF_STATUS_RENAMED: |
Stefan Beller | 146fdb0 | 2017-06-29 17:07:05 -0700 | [diff] [blame] | 6018 | show_rename_copy(opt, "rename", p); |
Sean | 4bbd261 | 2006-05-14 08:13:49 -0400 | [diff] [blame] | 6019 | break; |
| 6020 | default: |
| 6021 | if (p->score) { |
Stefan Beller | 146fdb0 | 2017-06-29 17:07:05 -0700 | [diff] [blame] | 6022 | struct strbuf sb = STRBUF_INIT; |
| 6023 | strbuf_addstr(&sb, " rewrite "); |
| 6024 | quote_c_style(p->two->path, &sb, NULL, 0); |
| 6025 | strbuf_addf(&sb, " (%d%%)\n", similarity_index(p)); |
| 6026 | emit_diff_symbol(opt, DIFF_SYMBOL_SUMMARY, |
| 6027 | sb.buf, sb.len, 0); |
Rene Scharfe | fa842d8 | 2017-08-30 19:49:41 +0200 | [diff] [blame] | 6028 | strbuf_release(&sb); |
Pierre Habouzit | 663af34 | 2007-09-20 00:42:15 +0200 | [diff] [blame] | 6029 | } |
Stefan Beller | 146fdb0 | 2017-06-29 17:07:05 -0700 | [diff] [blame] | 6030 | show_mode_change(opt, p, !p->score); |
Sean | 4bbd261 | 2006-05-14 08:13:49 -0400 | [diff] [blame] | 6031 | break; |
| 6032 | } |
| 6033 | } |
| 6034 | |
Johannes Schindelin | fcb3d0a | 2006-06-25 03:51:08 +0200 | [diff] [blame] | 6035 | struct patch_id_t { |
brian m. carlson | 36261e4 | 2019-08-18 20:04:03 +0000 | [diff] [blame] | 6036 | git_hash_ctx *ctx; |
Johannes Schindelin | fcb3d0a | 2006-06-25 03:51:08 +0200 | [diff] [blame] | 6037 | int patchlen; |
| 6038 | }; |
| 6039 | |
| 6040 | static int remove_space(char *line, int len) |
| 6041 | { |
| 6042 | int i; |
Pierre Habouzit | 663af34 | 2007-09-20 00:42:15 +0200 | [diff] [blame] | 6043 | char *dst = line; |
| 6044 | unsigned char c; |
Johannes Schindelin | fcb3d0a | 2006-06-25 03:51:08 +0200 | [diff] [blame] | 6045 | |
Pierre Habouzit | 663af34 | 2007-09-20 00:42:15 +0200 | [diff] [blame] | 6046 | for (i = 0; i < len; i++) |
| 6047 | if (!isspace((c = line[i]))) |
| 6048 | *dst++ = c; |
Johannes Schindelin | fcb3d0a | 2006-06-25 03:51:08 +0200 | [diff] [blame] | 6049 | |
Pierre Habouzit | 663af34 | 2007-09-20 00:42:15 +0200 | [diff] [blame] | 6050 | return dst - line; |
Johannes Schindelin | fcb3d0a | 2006-06-25 03:51:08 +0200 | [diff] [blame] | 6051 | } |
| 6052 | |
brian m. carlson | 36261e4 | 2019-08-18 20:04:03 +0000 | [diff] [blame] | 6053 | void flush_one_hunk(struct object_id *result, git_hash_ctx *ctx) |
Stephen Boyd | a8f6855 | 2019-04-26 16:51:57 -0700 | [diff] [blame] | 6054 | { |
| 6055 | unsigned char hash[GIT_MAX_RAWSZ]; |
| 6056 | unsigned short carry = 0; |
| 6057 | int i; |
| 6058 | |
brian m. carlson | 36261e4 | 2019-08-18 20:04:03 +0000 | [diff] [blame] | 6059 | the_hash_algo->final_fn(hash, ctx); |
| 6060 | the_hash_algo->init_fn(ctx); |
Stephen Boyd | a8f6855 | 2019-04-26 16:51:57 -0700 | [diff] [blame] | 6061 | /* 20-byte sum, with carry */ |
brian m. carlson | 36261e4 | 2019-08-18 20:04:03 +0000 | [diff] [blame] | 6062 | for (i = 0; i < the_hash_algo->rawsz; ++i) { |
Stephen Boyd | a8f6855 | 2019-04-26 16:51:57 -0700 | [diff] [blame] | 6063 | carry += result->hash[i] + hash[i]; |
| 6064 | result->hash[i] = carry; |
| 6065 | carry >>= 8; |
| 6066 | } |
| 6067 | } |
| 6068 | |
Johannes Schindelin | fcb3d0a | 2006-06-25 03:51:08 +0200 | [diff] [blame] | 6069 | static void patch_id_consume(void *priv, char *line, unsigned long len) |
| 6070 | { |
| 6071 | struct patch_id_t *data = priv; |
| 6072 | int new_len; |
| 6073 | |
René Scharfe | 82a6201 | 2020-08-19 00:08:54 +0200 | [diff] [blame] | 6074 | if (len > 12 && starts_with(line, "\\ ")) |
| 6075 | return; |
Johannes Schindelin | fcb3d0a | 2006-06-25 03:51:08 +0200 | [diff] [blame] | 6076 | new_len = remove_space(line, len); |
| 6077 | |
brian m. carlson | 36261e4 | 2019-08-18 20:04:03 +0000 | [diff] [blame] | 6078 | the_hash_algo->update_fn(data->ctx, line, new_len); |
Johannes Schindelin | fcb3d0a | 2006-06-25 03:51:08 +0200 | [diff] [blame] | 6079 | data->patchlen += new_len; |
| 6080 | } |
| 6081 | |
brian m. carlson | 36261e4 | 2019-08-18 20:04:03 +0000 | [diff] [blame] | 6082 | static void patch_id_add_string(git_hash_ctx *ctx, const char *str) |
Jeff King | 977db6b | 2017-03-30 14:26:05 -0400 | [diff] [blame] | 6083 | { |
brian m. carlson | 36261e4 | 2019-08-18 20:04:03 +0000 | [diff] [blame] | 6084 | the_hash_algo->update_fn(ctx, str, strlen(str)); |
Jeff King | 977db6b | 2017-03-30 14:26:05 -0400 | [diff] [blame] | 6085 | } |
| 6086 | |
brian m. carlson | 36261e4 | 2019-08-18 20:04:03 +0000 | [diff] [blame] | 6087 | static void patch_id_add_mode(git_hash_ctx *ctx, unsigned mode) |
Jeff King | 977db6b | 2017-03-30 14:26:05 -0400 | [diff] [blame] | 6088 | { |
| 6089 | /* large enough for 2^32 in octal */ |
| 6090 | char buf[12]; |
| 6091 | int len = xsnprintf(buf, sizeof(buf), "%06o", mode); |
brian m. carlson | 36261e4 | 2019-08-18 20:04:03 +0000 | [diff] [blame] | 6092 | the_hash_algo->update_fn(ctx, buf, len); |
Jeff King | 977db6b | 2017-03-30 14:26:05 -0400 | [diff] [blame] | 6093 | } |
| 6094 | |
Stephen Boyd | a8f6855 | 2019-04-26 16:51:57 -0700 | [diff] [blame] | 6095 | /* returns 0 upon success, and writes result into oid */ |
| 6096 | static int diff_get_patch_id(struct diff_options *options, struct object_id *oid, int diff_header_only, int stable) |
Johannes Schindelin | fcb3d0a | 2006-06-25 03:51:08 +0200 | [diff] [blame] | 6097 | { |
| 6098 | struct diff_queue_struct *q = &diff_queued_diff; |
| 6099 | int i; |
brian m. carlson | 36261e4 | 2019-08-18 20:04:03 +0000 | [diff] [blame] | 6100 | git_hash_ctx ctx; |
Johannes Schindelin | fcb3d0a | 2006-06-25 03:51:08 +0200 | [diff] [blame] | 6101 | struct patch_id_t data; |
Johannes Schindelin | fcb3d0a | 2006-06-25 03:51:08 +0200 | [diff] [blame] | 6102 | |
brian m. carlson | 36261e4 | 2019-08-18 20:04:03 +0000 | [diff] [blame] | 6103 | the_hash_algo->init_fn(&ctx); |
Johannes Schindelin | fcb3d0a | 2006-06-25 03:51:08 +0200 | [diff] [blame] | 6104 | memset(&data, 0, sizeof(struct patch_id_t)); |
| 6105 | data.ctx = &ctx; |
Stephen Boyd | a8f6855 | 2019-04-26 16:51:57 -0700 | [diff] [blame] | 6106 | oidclr(oid); |
Johannes Schindelin | fcb3d0a | 2006-06-25 03:51:08 +0200 | [diff] [blame] | 6107 | |
| 6108 | for (i = 0; i < q->nr; i++) { |
| 6109 | xpparam_t xpp; |
| 6110 | xdemitconf_t xecfg; |
Johannes Schindelin | fcb3d0a | 2006-06-25 03:51:08 +0200 | [diff] [blame] | 6111 | mmfile_t mf1, mf2; |
| 6112 | struct diff_filepair *p = q->queue[i]; |
| 6113 | int len1, len2; |
| 6114 | |
Brian Downing | 9ccd0a8 | 2008-10-25 15:30:37 +0200 | [diff] [blame] | 6115 | memset(&xpp, 0, sizeof(xpp)); |
Johannes Schindelin | 30b2501 | 2007-07-04 19:05:46 +0100 | [diff] [blame] | 6116 | memset(&xecfg, 0, sizeof(xecfg)); |
Johannes Schindelin | fcb3d0a | 2006-06-25 03:51:08 +0200 | [diff] [blame] | 6117 | if (p->status == 0) |
| 6118 | return error("internal diff status error"); |
| 6119 | if (p->status == DIFF_STATUS_UNKNOWN) |
| 6120 | continue; |
| 6121 | if (diff_unmodified_pair(p)) |
| 6122 | continue; |
| 6123 | if ((DIFF_FILE_VALID(p->one) && S_ISDIR(p->one->mode)) || |
| 6124 | (DIFF_FILE_VALID(p->two) && S_ISDIR(p->two->mode))) |
| 6125 | continue; |
| 6126 | if (DIFF_PAIR_UNMERGED(p)) |
| 6127 | continue; |
| 6128 | |
Nguyễn Thái Ngọc Duy | 58bf2a4 | 2018-09-21 17:57:31 +0200 | [diff] [blame] | 6129 | diff_fill_oid_info(p->one, options->repo->index); |
| 6130 | diff_fill_oid_info(p->two, options->repo->index); |
Johannes Schindelin | fcb3d0a | 2006-06-25 03:51:08 +0200 | [diff] [blame] | 6131 | |
Johannes Schindelin | fcb3d0a | 2006-06-25 03:51:08 +0200 | [diff] [blame] | 6132 | len1 = remove_space(p->one->path, strlen(p->one->path)); |
| 6133 | len2 = remove_space(p->two->path, strlen(p->two->path)); |
Jeff King | 977db6b | 2017-03-30 14:26:05 -0400 | [diff] [blame] | 6134 | patch_id_add_string(&ctx, "diff--git"); |
| 6135 | patch_id_add_string(&ctx, "a/"); |
brian m. carlson | 36261e4 | 2019-08-18 20:04:03 +0000 | [diff] [blame] | 6136 | the_hash_algo->update_fn(&ctx, p->one->path, len1); |
Jeff King | 977db6b | 2017-03-30 14:26:05 -0400 | [diff] [blame] | 6137 | patch_id_add_string(&ctx, "b/"); |
brian m. carlson | 36261e4 | 2019-08-18 20:04:03 +0000 | [diff] [blame] | 6138 | the_hash_algo->update_fn(&ctx, p->two->path, len2); |
Jeff King | 977db6b | 2017-03-30 14:26:05 -0400 | [diff] [blame] | 6139 | |
| 6140 | if (p->one->mode == 0) { |
| 6141 | patch_id_add_string(&ctx, "newfilemode"); |
| 6142 | patch_id_add_mode(&ctx, p->two->mode); |
| 6143 | patch_id_add_string(&ctx, "---/dev/null"); |
| 6144 | patch_id_add_string(&ctx, "+++b/"); |
brian m. carlson | 36261e4 | 2019-08-18 20:04:03 +0000 | [diff] [blame] | 6145 | the_hash_algo->update_fn(&ctx, p->two->path, len2); |
Jeff King | 977db6b | 2017-03-30 14:26:05 -0400 | [diff] [blame] | 6146 | } else if (p->two->mode == 0) { |
| 6147 | patch_id_add_string(&ctx, "deletedfilemode"); |
| 6148 | patch_id_add_mode(&ctx, p->one->mode); |
| 6149 | patch_id_add_string(&ctx, "---a/"); |
brian m. carlson | 36261e4 | 2019-08-18 20:04:03 +0000 | [diff] [blame] | 6150 | the_hash_algo->update_fn(&ctx, p->one->path, len1); |
Jeff King | 977db6b | 2017-03-30 14:26:05 -0400 | [diff] [blame] | 6151 | patch_id_add_string(&ctx, "+++/dev/null"); |
| 6152 | } else { |
| 6153 | patch_id_add_string(&ctx, "---a/"); |
brian m. carlson | 36261e4 | 2019-08-18 20:04:03 +0000 | [diff] [blame] | 6154 | the_hash_algo->update_fn(&ctx, p->one->path, len1); |
Jeff King | 977db6b | 2017-03-30 14:26:05 -0400 | [diff] [blame] | 6155 | patch_id_add_string(&ctx, "+++b/"); |
brian m. carlson | 36261e4 | 2019-08-18 20:04:03 +0000 | [diff] [blame] | 6156 | the_hash_algo->update_fn(&ctx, p->two->path, len2); |
Jeff King | 977db6b | 2017-03-30 14:26:05 -0400 | [diff] [blame] | 6157 | } |
Johannes Schindelin | fcb3d0a | 2006-06-25 03:51:08 +0200 | [diff] [blame] | 6158 | |
Kevin Willford | 3e8e32c | 2016-07-29 12:19:19 -0400 | [diff] [blame] | 6159 | if (diff_header_only) |
| 6160 | continue; |
| 6161 | |
Nguyễn Thái Ngọc Duy | b78ea5f | 2018-09-21 17:57:19 +0200 | [diff] [blame] | 6162 | if (fill_mmfile(options->repo, &mf1, p->one) < 0 || |
| 6163 | fill_mmfile(options->repo, &mf2, p->two) < 0) |
Kevin Willford | 3e8e32c | 2016-07-29 12:19:19 -0400 | [diff] [blame] | 6164 | return error("unable to read files to diff"); |
| 6165 | |
Nguyễn Thái Ngọc Duy | b78ea5f | 2018-09-21 17:57:19 +0200 | [diff] [blame] | 6166 | if (diff_filespec_is_binary(options->repo, p->one) || |
| 6167 | diff_filespec_is_binary(options->repo, p->two)) { |
brian m. carlson | 36261e4 | 2019-08-18 20:04:03 +0000 | [diff] [blame] | 6168 | the_hash_algo->update_fn(&ctx, oid_to_hex(&p->one->oid), |
| 6169 | the_hash_algo->hexsz); |
| 6170 | the_hash_algo->update_fn(&ctx, oid_to_hex(&p->two->oid), |
| 6171 | the_hash_algo->hexsz); |
Clemens Buchacher | 34597c1 | 2010-08-15 09:20:43 +0200 | [diff] [blame] | 6172 | continue; |
| 6173 | } |
| 6174 | |
René Scharfe | 582aa00 | 2010-05-02 15:04:41 +0200 | [diff] [blame] | 6175 | xpp.flags = 0; |
Johannes Schindelin | fcb3d0a | 2006-06-25 03:51:08 +0200 | [diff] [blame] | 6176 | xecfg.ctxlen = 3; |
Clemens Buchacher | ad14b45 | 2010-09-19 11:59:28 +0200 | [diff] [blame] | 6177 | xecfg.flags = 0; |
Jeff King | b135739 | 2018-11-02 02:36:36 -0400 | [diff] [blame] | 6178 | if (xdi_diff_outf(&mf1, &mf2, discard_hunk_line, |
| 6179 | patch_id_consume, &data, &xpp, &xecfg)) |
Jeff King | 3efb988 | 2015-09-24 19:12:23 -0400 | [diff] [blame] | 6180 | return error("unable to generate patch-id diff for %s", |
| 6181 | p->one->path); |
Stephen Boyd | a8f6855 | 2019-04-26 16:51:57 -0700 | [diff] [blame] | 6182 | |
| 6183 | if (stable) |
| 6184 | flush_one_hunk(oid, &ctx); |
Johannes Schindelin | fcb3d0a | 2006-06-25 03:51:08 +0200 | [diff] [blame] | 6185 | } |
| 6186 | |
Stephen Boyd | a8f6855 | 2019-04-26 16:51:57 -0700 | [diff] [blame] | 6187 | if (!stable) |
brian m. carlson | 36261e4 | 2019-08-18 20:04:03 +0000 | [diff] [blame] | 6188 | the_hash_algo->final_fn(oid->hash, &ctx); |
Stephen Boyd | a8f6855 | 2019-04-26 16:51:57 -0700 | [diff] [blame] | 6189 | |
Johannes Schindelin | fcb3d0a | 2006-06-25 03:51:08 +0200 | [diff] [blame] | 6190 | return 0; |
| 6191 | } |
| 6192 | |
Stephen Boyd | a8f6855 | 2019-04-26 16:51:57 -0700 | [diff] [blame] | 6193 | int diff_flush_patch_id(struct diff_options *options, struct object_id *oid, int diff_header_only, int stable) |
Johannes Schindelin | fcb3d0a | 2006-06-25 03:51:08 +0200 | [diff] [blame] | 6194 | { |
| 6195 | struct diff_queue_struct *q = &diff_queued_diff; |
| 6196 | int i; |
Stephen Boyd | a8f6855 | 2019-04-26 16:51:57 -0700 | [diff] [blame] | 6197 | int result = diff_get_patch_id(options, oid, diff_header_only, stable); |
Johannes Schindelin | fcb3d0a | 2006-06-25 03:51:08 +0200 | [diff] [blame] | 6198 | |
| 6199 | for (i = 0; i < q->nr; i++) |
| 6200 | diff_free_filepair(q->queue[i]); |
| 6201 | |
| 6202 | free(q->queue); |
Bo Yang | 9ca5df9 | 2010-05-06 21:52:27 -0700 | [diff] [blame] | 6203 | DIFF_QUEUE_CLEAR(q); |
Johannes Schindelin | fcb3d0a | 2006-06-25 03:51:08 +0200 | [diff] [blame] | 6204 | |
| 6205 | return result; |
| 6206 | } |
| 6207 | |
Timo Hirvonen | 946c378 | 2006-06-27 15:09:17 +0300 | [diff] [blame] | 6208 | static int is_summary_empty(const struct diff_queue_struct *q) |
| 6209 | { |
| 6210 | int i; |
| 6211 | |
| 6212 | for (i = 0; i < q->nr; i++) { |
| 6213 | const struct diff_filepair *p = q->queue[i]; |
| 6214 | |
| 6215 | switch (p->status) { |
| 6216 | case DIFF_STATUS_DELETED: |
| 6217 | case DIFF_STATUS_ADDED: |
| 6218 | case DIFF_STATUS_COPIED: |
| 6219 | case DIFF_STATUS_RENAMED: |
| 6220 | return 0; |
| 6221 | default: |
| 6222 | if (p->score) |
| 6223 | return 0; |
| 6224 | if (p->one->mode && p->two->mode && |
| 6225 | p->one->mode != p->two->mode) |
| 6226 | return 0; |
| 6227 | break; |
| 6228 | } |
| 6229 | } |
| 6230 | return 1; |
| 6231 | } |
| 6232 | |
Junio C Hamano | f31027c | 2011-01-06 13:50:06 -0800 | [diff] [blame] | 6233 | static const char rename_limit_warning[] = |
Vasco Almeida | db42497 | 2016-10-17 13:15:29 +0000 | [diff] [blame] | 6234 | N_("inexact rename detection was skipped due to too many files."); |
Junio C Hamano | f31027c | 2011-01-06 13:50:06 -0800 | [diff] [blame] | 6235 | |
| 6236 | static const char degrade_cc_to_c_warning[] = |
Vasco Almeida | db42497 | 2016-10-17 13:15:29 +0000 | [diff] [blame] | 6237 | N_("only found copies from modified paths due to too many files."); |
Junio C Hamano | f31027c | 2011-01-06 13:50:06 -0800 | [diff] [blame] | 6238 | |
| 6239 | static const char rename_limit_advice[] = |
Vasco Almeida | db42497 | 2016-10-17 13:15:29 +0000 | [diff] [blame] | 6240 | N_("you may want to set your %s variable to at least " |
| 6241 | "%d and retry the command."); |
Junio C Hamano | f31027c | 2011-01-06 13:50:06 -0800 | [diff] [blame] | 6242 | |
| 6243 | void diff_warn_rename_limit(const char *varname, int needed, int degraded_cc) |
| 6244 | { |
Nguyễn Thái Ngọc Duy | 4e056c9 | 2018-01-16 16:23:49 +0700 | [diff] [blame] | 6245 | fflush(stdout); |
Junio C Hamano | f31027c | 2011-01-06 13:50:06 -0800 | [diff] [blame] | 6246 | if (degraded_cc) |
Vasco Almeida | db42497 | 2016-10-17 13:15:29 +0000 | [diff] [blame] | 6247 | warning(_(degrade_cc_to_c_warning)); |
Junio C Hamano | f31027c | 2011-01-06 13:50:06 -0800 | [diff] [blame] | 6248 | else if (needed) |
Vasco Almeida | db42497 | 2016-10-17 13:15:29 +0000 | [diff] [blame] | 6249 | warning(_(rename_limit_warning)); |
Junio C Hamano | f31027c | 2011-01-06 13:50:06 -0800 | [diff] [blame] | 6250 | else |
| 6251 | return; |
Elijah Newren | 9f7e4bf | 2017-11-13 12:15:59 -0800 | [diff] [blame] | 6252 | if (0 < needed) |
Vasco Almeida | db42497 | 2016-10-17 13:15:29 +0000 | [diff] [blame] | 6253 | warning(_(rename_limit_advice), varname, needed); |
Junio C Hamano | f31027c | 2011-01-06 13:50:06 -0800 | [diff] [blame] | 6254 | } |
| 6255 | |
Stefan Beller | ec33150 | 2017-06-29 17:06:48 -0700 | [diff] [blame] | 6256 | static void diff_flush_patch_all_file_pairs(struct diff_options *o) |
| 6257 | { |
| 6258 | int i; |
Stefan Beller | e6e045f | 2017-06-29 17:07:06 -0700 | [diff] [blame] | 6259 | static struct emitted_diff_symbols esm = EMITTED_DIFF_SYMBOLS_INIT; |
Stefan Beller | ec33150 | 2017-06-29 17:06:48 -0700 | [diff] [blame] | 6260 | struct diff_queue_struct *q = &diff_queued_diff; |
Stefan Beller | 091f8e2 | 2017-06-29 17:06:53 -0700 | [diff] [blame] | 6261 | |
| 6262 | if (WSEH_NEW & WS_RULE_MASK) |
Johannes Schindelin | 033abf9 | 2018-05-02 11:38:39 +0200 | [diff] [blame] | 6263 | BUG("WS rules bit mask overlaps with diff symbol flags"); |
Stefan Beller | 091f8e2 | 2017-06-29 17:06:53 -0700 | [diff] [blame] | 6264 | |
Stefan Beller | 2e2d5ac | 2017-06-30 13:53:07 -0700 | [diff] [blame] | 6265 | if (o->color_moved) |
| 6266 | o->emitted_symbols = &esm; |
Stefan Beller | e6e045f | 2017-06-29 17:07:06 -0700 | [diff] [blame] | 6267 | |
Stefan Beller | ec33150 | 2017-06-29 17:06:48 -0700 | [diff] [blame] | 6268 | for (i = 0; i < q->nr; i++) { |
| 6269 | struct diff_filepair *p = q->queue[i]; |
| 6270 | if (check_pair_status(p)) |
| 6271 | diff_flush_patch(p, o); |
| 6272 | } |
Stefan Beller | e6e045f | 2017-06-29 17:07:06 -0700 | [diff] [blame] | 6273 | |
| 6274 | if (o->emitted_symbols) { |
Stefan Beller | 2e2d5ac | 2017-06-30 13:53:07 -0700 | [diff] [blame] | 6275 | if (o->color_moved) { |
| 6276 | struct hashmap add_lines, del_lines; |
| 6277 | |
Stefan Beller | ca1f4ae | 2018-07-18 12:31:55 -0700 | [diff] [blame] | 6278 | if (o->color_moved_ws_handling & |
| 6279 | COLOR_MOVED_WS_ALLOW_INDENTATION_CHANGE) |
| 6280 | o->color_moved_ws_handling |= XDF_IGNORE_WHITESPACE; |
| 6281 | |
Stefan Beller | ee1df66 | 2018-07-16 16:05:38 -0700 | [diff] [blame] | 6282 | hashmap_init(&del_lines, moved_entry_cmp, o, 0); |
| 6283 | hashmap_init(&add_lines, moved_entry_cmp, o, 0); |
Stefan Beller | 2e2d5ac | 2017-06-30 13:53:07 -0700 | [diff] [blame] | 6284 | |
| 6285 | add_lines_to_move_detection(o, &add_lines, &del_lines); |
| 6286 | mark_color_as_moved(o, &add_lines, &del_lines); |
Stefan Beller | 86b452e | 2017-06-30 13:53:09 -0700 | [diff] [blame] | 6287 | if (o->color_moved == COLOR_MOVED_ZEBRA_DIM) |
| 6288 | dim_moved_lines(o); |
Stefan Beller | 2e2d5ac | 2017-06-30 13:53:07 -0700 | [diff] [blame] | 6289 | |
Eric Wong | c8e424c | 2019-10-06 23:30:40 +0000 | [diff] [blame] | 6290 | hashmap_free_entries(&add_lines, struct moved_entry, |
| 6291 | ent); |
| 6292 | hashmap_free_entries(&del_lines, struct moved_entry, |
| 6293 | ent); |
Stefan Beller | 2e2d5ac | 2017-06-30 13:53:07 -0700 | [diff] [blame] | 6294 | } |
| 6295 | |
Stefan Beller | e6e045f | 2017-06-29 17:07:06 -0700 | [diff] [blame] | 6296 | for (i = 0; i < esm.nr; i++) |
| 6297 | emit_diff_symbol_from_struct(o, &esm.buf[i]); |
| 6298 | |
| 6299 | for (i = 0; i < esm.nr; i++) |
| 6300 | free((void *)esm.buf[i].line); |
Jeff King | 48edf3a | 2019-01-24 07:32:41 -0500 | [diff] [blame] | 6301 | esm.nr = 0; |
| 6302 | |
| 6303 | o->emitted_symbols = NULL; |
Stefan Beller | e6e045f | 2017-06-29 17:07:06 -0700 | [diff] [blame] | 6304 | } |
Stefan Beller | ec33150 | 2017-06-29 17:06:48 -0700 | [diff] [blame] | 6305 | } |
| 6306 | |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 6307 | void diff_flush(struct diff_options *options) |
| 6308 | { |
| 6309 | struct diff_queue_struct *q = &diff_queued_diff; |
Timo Hirvonen | c674434 | 2006-06-24 20:21:53 +0300 | [diff] [blame] | 6310 | int i, output_format = options->output_format; |
Timo Hirvonen | 946c378 | 2006-06-27 15:09:17 +0300 | [diff] [blame] | 6311 | int separator = 0; |
Johan Herland | 1c57a62 | 2011-04-29 11:36:21 +0200 | [diff] [blame] | 6312 | int dirstat_by_line = 0; |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 6313 | |
Timo Hirvonen | c674434 | 2006-06-24 20:21:53 +0300 | [diff] [blame] | 6314 | /* |
| 6315 | * Order: raw, stat, summary, patch |
| 6316 | * or: name/name-status/checkdiff (other bits clear) |
| 6317 | */ |
Timo Hirvonen | 946c378 | 2006-06-27 15:09:17 +0300 | [diff] [blame] | 6318 | if (!q->nr) |
| 6319 | goto free_queue; |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 6320 | |
Timo Hirvonen | c674434 | 2006-06-24 20:21:53 +0300 | [diff] [blame] | 6321 | if (output_format & (DIFF_FORMAT_RAW | |
| 6322 | DIFF_FORMAT_NAME | |
| 6323 | DIFF_FORMAT_NAME_STATUS | |
| 6324 | DIFF_FORMAT_CHECKDIFF)) { |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 6325 | for (i = 0; i < q->nr; i++) { |
| 6326 | struct diff_filepair *p = q->queue[i]; |
Timo Hirvonen | c674434 | 2006-06-24 20:21:53 +0300 | [diff] [blame] | 6327 | if (check_pair_status(p)) |
| 6328 | flush_one_pair(p, options); |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 6329 | } |
Timo Hirvonen | 946c378 | 2006-06-27 15:09:17 +0300 | [diff] [blame] | 6330 | separator++; |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 6331 | } |
Timo Hirvonen | c674434 | 2006-06-24 20:21:53 +0300 | [diff] [blame] | 6332 | |
Brandon Williams | 0d1e0e7 | 2017-10-31 11:19:11 -0700 | [diff] [blame] | 6333 | if (output_format & DIFF_FORMAT_DIRSTAT && options->flags.dirstat_by_line) |
Johan Herland | 1c57a62 | 2011-04-29 11:36:21 +0200 | [diff] [blame] | 6334 | dirstat_by_line = 1; |
| 6335 | |
| 6336 | if (output_format & (DIFF_FORMAT_DIFFSTAT|DIFF_FORMAT_SHORTSTAT|DIFF_FORMAT_NUMSTAT) || |
| 6337 | dirstat_by_line) { |
Timo Hirvonen | 5e2b063 | 2006-06-25 14:28:19 +0300 | [diff] [blame] | 6338 | struct diffstat_t diffstat; |
Timo Hirvonen | c674434 | 2006-06-24 20:21:53 +0300 | [diff] [blame] | 6339 | |
Daniel Ferreira | e4cb659 | 2019-11-13 12:40:58 +0000 | [diff] [blame] | 6340 | compute_diffstat(options, &diffstat, q); |
Junio C Hamano | 74e2abe | 2006-10-12 03:01:00 -0700 | [diff] [blame] | 6341 | if (output_format & DIFF_FORMAT_NUMSTAT) |
| 6342 | show_numstat(&diffstat, options); |
| 6343 | if (output_format & DIFF_FORMAT_DIFFSTAT) |
| 6344 | show_stats(&diffstat, options); |
Junio C Hamano | f604652 | 2007-12-11 23:46:30 -0800 | [diff] [blame] | 6345 | if (output_format & DIFF_FORMAT_SHORTSTAT) |
Daniel Barkalow | c0c7773 | 2008-03-09 22:43:39 -0400 | [diff] [blame] | 6346 | show_shortstats(&diffstat, options); |
Mårten Kongstad | ab27389 | 2015-03-02 16:05:39 +0100 | [diff] [blame] | 6347 | if (output_format & DIFF_FORMAT_DIRSTAT && dirstat_by_line) |
Johan Herland | 1c57a62 | 2011-04-29 11:36:21 +0200 | [diff] [blame] | 6348 | show_dirstat_by_line(&diffstat, options); |
Junio C Hamano | f604652 | 2007-12-11 23:46:30 -0800 | [diff] [blame] | 6349 | free_diffstat_info(&diffstat); |
Junio C Hamano | 3969cf7 | 2006-06-27 15:08:19 -0700 | [diff] [blame] | 6350 | separator++; |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 6351 | } |
Johan Herland | 1c57a62 | 2011-04-29 11:36:21 +0200 | [diff] [blame] | 6352 | if ((output_format & DIFF_FORMAT_DIRSTAT) && !dirstat_by_line) |
Junio C Hamano | c04a715 | 2008-02-12 17:06:58 -0800 | [diff] [blame] | 6353 | show_dirstat(options); |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 6354 | |
Timo Hirvonen | 946c378 | 2006-06-27 15:09:17 +0300 | [diff] [blame] | 6355 | if (output_format & DIFF_FORMAT_SUMMARY && !is_summary_empty(q)) { |
Bo Yang | 7be5761 | 2010-05-26 15:23:54 +0800 | [diff] [blame] | 6356 | for (i = 0; i < q->nr; i++) { |
| 6357 | diff_summary(options, q->queue[i]); |
| 6358 | } |
Junio C Hamano | 3969cf7 | 2006-06-27 15:08:19 -0700 | [diff] [blame] | 6359 | separator++; |
Sean | 4bbd261 | 2006-05-14 08:13:49 -0400 | [diff] [blame] | 6360 | } |
| 6361 | |
Larry D'Anna | 6977c25 | 2010-02-16 01:55:21 -0500 | [diff] [blame] | 6362 | if (output_format & DIFF_FORMAT_NO_OUTPUT && |
Brandon Williams | 0d1e0e7 | 2017-10-31 11:19:11 -0700 | [diff] [blame] | 6363 | options->flags.exit_with_status && |
| 6364 | options->flags.diff_from_contents) { |
Larry D'Anna | 6977c25 | 2010-02-16 01:55:21 -0500 | [diff] [blame] | 6365 | /* |
| 6366 | * run diff_flush_patch for the exit status. setting |
Ondřej Bílka | 749f763 | 2013-07-22 23:02:23 +0200 | [diff] [blame] | 6367 | * options->file to /dev/null should be safe, because we |
Larry D'Anna | 6977c25 | 2010-02-16 01:55:21 -0500 | [diff] [blame] | 6368 | * aren't supposed to produce any output anyway. |
| 6369 | */ |
| 6370 | if (options->close_file) |
| 6371 | fclose(options->file); |
Nguyễn Thái Ngọc Duy | 23a9e07 | 2017-05-03 17:16:46 +0700 | [diff] [blame] | 6372 | options->file = xfopen("/dev/null", "w"); |
Larry D'Anna | 6977c25 | 2010-02-16 01:55:21 -0500 | [diff] [blame] | 6373 | options->close_file = 1; |
Stefan Beller | 2e2d5ac | 2017-06-30 13:53:07 -0700 | [diff] [blame] | 6374 | options->color_moved = 0; |
Larry D'Anna | 6977c25 | 2010-02-16 01:55:21 -0500 | [diff] [blame] | 6375 | for (i = 0; i < q->nr; i++) { |
| 6376 | struct diff_filepair *p = q->queue[i]; |
| 6377 | if (check_pair_status(p)) |
| 6378 | diff_flush_patch(p, options); |
| 6379 | if (options->found_changes) |
| 6380 | break; |
| 6381 | } |
| 6382 | } |
| 6383 | |
Timo Hirvonen | c674434 | 2006-06-24 20:21:53 +0300 | [diff] [blame] | 6384 | if (output_format & DIFF_FORMAT_PATCH) { |
Timo Hirvonen | 946c378 | 2006-06-27 15:09:17 +0300 | [diff] [blame] | 6385 | if (separator) { |
Stefan Beller | 091f8e2 | 2017-06-29 17:06:53 -0700 | [diff] [blame] | 6386 | emit_diff_symbol(options, DIFF_SYMBOL_SEPARATOR, NULL, 0, 0); |
Stefan Beller | 30b7e1e | 2017-06-29 17:07:04 -0700 | [diff] [blame] | 6387 | if (options->stat_sep) |
Timo Hirvonen | 946c378 | 2006-06-27 15:09:17 +0300 | [diff] [blame] | 6388 | /* attach patch instead of inline */ |
Stefan Beller | 30b7e1e | 2017-06-29 17:07:04 -0700 | [diff] [blame] | 6389 | emit_diff_symbol(options, DIFF_SYMBOL_STAT_SEP, |
| 6390 | NULL, 0, 0); |
Timo Hirvonen | c674434 | 2006-06-24 20:21:53 +0300 | [diff] [blame] | 6391 | } |
| 6392 | |
Stefan Beller | ec33150 | 2017-06-29 17:06:48 -0700 | [diff] [blame] | 6393 | diff_flush_patch_all_file_pairs(options); |
Timo Hirvonen | c674434 | 2006-06-24 20:21:53 +0300 | [diff] [blame] | 6394 | } |
| 6395 | |
Jeff King | 0424558 | 2006-09-07 02:35:42 -0400 | [diff] [blame] | 6396 | if (output_format & DIFF_FORMAT_CALLBACK) |
| 6397 | options->format_callback(q, options, options->format_callback_data); |
| 6398 | |
Timo Hirvonen | c674434 | 2006-06-24 20:21:53 +0300 | [diff] [blame] | 6399 | for (i = 0; i < q->nr; i++) |
| 6400 | diff_free_filepair(q->queue[i]); |
Timo Hirvonen | 946c378 | 2006-06-27 15:09:17 +0300 | [diff] [blame] | 6401 | free_queue: |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 6402 | free(q->queue); |
Bo Yang | 9ca5df9 | 2010-05-06 21:52:27 -0700 | [diff] [blame] | 6403 | DIFF_QUEUE_CLEAR(q); |
Daniel Barkalow | c0c7773 | 2008-03-09 22:43:39 -0400 | [diff] [blame] | 6404 | if (options->close_file) |
| 6405 | fclose(options->file); |
Junio C Hamano | f245194 | 2009-05-22 12:45:29 -0700 | [diff] [blame] | 6406 | |
| 6407 | /* |
Jim Meyering | 97bf2a0 | 2009-08-30 22:27:02 +0200 | [diff] [blame] | 6408 | * Report the content-level differences with HAS_CHANGES; |
Junio C Hamano | f245194 | 2009-05-22 12:45:29 -0700 | [diff] [blame] | 6409 | * diff_addremove/diff_change does not set the bit when |
| 6410 | * DIFF_FROM_CONTENTS is in effect (e.g. with -w). |
| 6411 | */ |
Brandon Williams | 0d1e0e7 | 2017-10-31 11:19:11 -0700 | [diff] [blame] | 6412 | if (options->flags.diff_from_contents) { |
Junio C Hamano | f245194 | 2009-05-22 12:45:29 -0700 | [diff] [blame] | 6413 | if (options->found_changes) |
Brandon Williams | 0d1e0e7 | 2017-10-31 11:19:11 -0700 | [diff] [blame] | 6414 | options->flags.has_changes = 1; |
Junio C Hamano | f245194 | 2009-05-22 12:45:29 -0700 | [diff] [blame] | 6415 | else |
Brandon Williams | 0d1e0e7 | 2017-10-31 11:19:11 -0700 | [diff] [blame] | 6416 | options->flags.has_changes = 0; |
Junio C Hamano | f245194 | 2009-05-22 12:45:29 -0700 | [diff] [blame] | 6417 | } |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 6418 | } |
| 6419 | |
Junio C Hamano | 08578fa | 2013-07-17 15:09:34 -0700 | [diff] [blame] | 6420 | static int match_filter(const struct diff_options *options, const struct diff_filepair *p) |
| 6421 | { |
| 6422 | return (((p->status == DIFF_STATUS_MODIFIED) && |
| 6423 | ((p->score && |
Junio C Hamano | 1ecc1cb | 2013-07-17 15:05:46 -0700 | [diff] [blame] | 6424 | filter_bit_tst(DIFF_STATUS_FILTER_BROKEN, options)) || |
Junio C Hamano | 08578fa | 2013-07-17 15:09:34 -0700 | [diff] [blame] | 6425 | (!p->score && |
Junio C Hamano | 1ecc1cb | 2013-07-17 15:05:46 -0700 | [diff] [blame] | 6426 | filter_bit_tst(DIFF_STATUS_MODIFIED, options)))) || |
Junio C Hamano | 08578fa | 2013-07-17 15:09:34 -0700 | [diff] [blame] | 6427 | ((p->status != DIFF_STATUS_MODIFIED) && |
Junio C Hamano | 1ecc1cb | 2013-07-17 15:05:46 -0700 | [diff] [blame] | 6428 | filter_bit_tst(p->status, options))); |
Junio C Hamano | 08578fa | 2013-07-17 15:09:34 -0700 | [diff] [blame] | 6429 | } |
| 6430 | |
Junio C Hamano | 949226f | 2013-07-17 14:19:24 -0700 | [diff] [blame] | 6431 | static void diffcore_apply_filter(struct diff_options *options) |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 6432 | { |
| 6433 | int i; |
| 6434 | struct diff_queue_struct *q = &diff_queued_diff; |
| 6435 | struct diff_queue_struct outq; |
Junio C Hamano | 949226f | 2013-07-17 14:19:24 -0700 | [diff] [blame] | 6436 | |
Bo Yang | 9ca5df9 | 2010-05-06 21:52:27 -0700 | [diff] [blame] | 6437 | DIFF_QUEUE_CLEAR(&outq); |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 6438 | |
Junio C Hamano | 1ecc1cb | 2013-07-17 15:05:46 -0700 | [diff] [blame] | 6439 | if (!options->filter) |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 6440 | return; |
| 6441 | |
Junio C Hamano | 1ecc1cb | 2013-07-17 15:05:46 -0700 | [diff] [blame] | 6442 | if (filter_bit_tst(DIFF_STATUS_FILTER_AON, options)) { |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 6443 | int found; |
| 6444 | for (i = found = 0; !found && i < q->nr; i++) { |
Junio C Hamano | 08578fa | 2013-07-17 15:09:34 -0700 | [diff] [blame] | 6445 | if (match_filter(options, q->queue[i])) |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 6446 | found++; |
| 6447 | } |
| 6448 | if (found) |
| 6449 | return; |
| 6450 | |
| 6451 | /* otherwise we will clear the whole queue |
| 6452 | * by copying the empty outq at the end of this |
| 6453 | * function, but first clear the current entries |
| 6454 | * in the queue. |
| 6455 | */ |
| 6456 | for (i = 0; i < q->nr; i++) |
| 6457 | diff_free_filepair(q->queue[i]); |
| 6458 | } |
| 6459 | else { |
| 6460 | /* Only the matching ones */ |
| 6461 | for (i = 0; i < q->nr; i++) { |
| 6462 | struct diff_filepair *p = q->queue[i]; |
Junio C Hamano | 08578fa | 2013-07-17 15:09:34 -0700 | [diff] [blame] | 6463 | if (match_filter(options, p)) |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 6464 | diff_q(&outq, p); |
| 6465 | else |
| 6466 | diff_free_filepair(p); |
| 6467 | } |
| 6468 | } |
| 6469 | free(q->queue); |
| 6470 | *q = outq; |
| 6471 | } |
| 6472 | |
Sven Verdoolaege | 5701115 | 2007-09-08 12:30:22 +0200 | [diff] [blame] | 6473 | /* Check whether two filespecs with the same mode and size are identical */ |
Nguyễn Thái Ngọc Duy | b78ea5f | 2018-09-21 17:57:19 +0200 | [diff] [blame] | 6474 | static int diff_filespec_is_identical(struct repository *r, |
| 6475 | struct diff_filespec *one, |
Sven Verdoolaege | 5701115 | 2007-09-08 12:30:22 +0200 | [diff] [blame] | 6476 | struct diff_filespec *two) |
| 6477 | { |
Junio C Hamano | 2b459b4 | 2008-03-02 00:07:59 -0800 | [diff] [blame] | 6478 | if (S_ISGITLINK(one->mode)) |
| 6479 | return 0; |
Jonathan Tan | 1c37e86 | 2020-04-07 15:11:41 -0700 | [diff] [blame] | 6480 | if (diff_populate_filespec(r, one, NULL)) |
Sven Verdoolaege | 5701115 | 2007-09-08 12:30:22 +0200 | [diff] [blame] | 6481 | return 0; |
Jonathan Tan | 1c37e86 | 2020-04-07 15:11:41 -0700 | [diff] [blame] | 6482 | if (diff_populate_filespec(r, two, NULL)) |
Sven Verdoolaege | 5701115 | 2007-09-08 12:30:22 +0200 | [diff] [blame] | 6483 | return 0; |
| 6484 | return !memcmp(one->data, two->data, one->size); |
| 6485 | } |
| 6486 | |
Nguyễn Thái Ngọc Duy | b78ea5f | 2018-09-21 17:57:19 +0200 | [diff] [blame] | 6487 | static int diff_filespec_check_stat_unmatch(struct repository *r, |
| 6488 | struct diff_filepair *p) |
Nguyễn Thái Ngọc Duy | fceb907 | 2014-01-25 13:46:49 +0700 | [diff] [blame] | 6489 | { |
Jonathan Tan | 1c37e86 | 2020-04-07 15:11:41 -0700 | [diff] [blame] | 6490 | struct diff_populate_filespec_options dpf_options = { |
| 6491 | .check_size_only = 1, |
Jonathan Tan | 95acf11 | 2020-04-07 15:11:43 -0700 | [diff] [blame] | 6492 | .missing_object_cb = diff_queued_diff_prefetch, |
| 6493 | .missing_object_data = r, |
Jonathan Tan | 1c37e86 | 2020-04-07 15:11:41 -0700 | [diff] [blame] | 6494 | }; |
| 6495 | |
Nguyễn Thái Ngọc Duy | f34b205 | 2014-01-25 13:46:50 +0700 | [diff] [blame] | 6496 | if (p->done_skip_stat_unmatch) |
| 6497 | return p->skip_stat_unmatch_result; |
| 6498 | |
| 6499 | p->done_skip_stat_unmatch = 1; |
| 6500 | p->skip_stat_unmatch_result = 0; |
Nguyễn Thái Ngọc Duy | fceb907 | 2014-01-25 13:46:49 +0700 | [diff] [blame] | 6501 | /* |
| 6502 | * 1. Entries that come from stat info dirtiness |
| 6503 | * always have both sides (iow, not create/delete), |
| 6504 | * one side of the object name is unknown, with |
| 6505 | * the same mode and size. Keep the ones that |
| 6506 | * do not match these criteria. They have real |
| 6507 | * differences. |
| 6508 | * |
| 6509 | * 2. At this point, the file is known to be modified, |
| 6510 | * with the same mode and size, and the object |
| 6511 | * name of one side is unknown. Need to inspect |
| 6512 | * the identical contents. |
| 6513 | */ |
| 6514 | if (!DIFF_FILE_VALID(p->one) || /* (1) */ |
| 6515 | !DIFF_FILE_VALID(p->two) || |
brian m. carlson | 41c9560 | 2016-06-24 23:09:24 +0000 | [diff] [blame] | 6516 | (p->one->oid_valid && p->two->oid_valid) || |
Nguyễn Thái Ngọc Duy | fceb907 | 2014-01-25 13:46:49 +0700 | [diff] [blame] | 6517 | (p->one->mode != p->two->mode) || |
Jonathan Tan | 1c37e86 | 2020-04-07 15:11:41 -0700 | [diff] [blame] | 6518 | diff_populate_filespec(r, p->one, &dpf_options) || |
| 6519 | diff_populate_filespec(r, p->two, &dpf_options) || |
Nguyễn Thái Ngọc Duy | fceb907 | 2014-01-25 13:46:49 +0700 | [diff] [blame] | 6520 | (p->one->size != p->two->size) || |
Nguyễn Thái Ngọc Duy | b78ea5f | 2018-09-21 17:57:19 +0200 | [diff] [blame] | 6521 | !diff_filespec_is_identical(r, p->one, p->two)) /* (2) */ |
Nguyễn Thái Ngọc Duy | f34b205 | 2014-01-25 13:46:50 +0700 | [diff] [blame] | 6522 | p->skip_stat_unmatch_result = 1; |
| 6523 | return p->skip_stat_unmatch_result; |
Nguyễn Thái Ngọc Duy | fceb907 | 2014-01-25 13:46:49 +0700 | [diff] [blame] | 6524 | } |
| 6525 | |
Junio C Hamano | fb13227 | 2007-08-03 13:33:31 -0700 | [diff] [blame] | 6526 | static void diffcore_skip_stat_unmatch(struct diff_options *diffopt) |
| 6527 | { |
| 6528 | int i; |
| 6529 | struct diff_queue_struct *q = &diff_queued_diff; |
| 6530 | struct diff_queue_struct outq; |
Bo Yang | 9ca5df9 | 2010-05-06 21:52:27 -0700 | [diff] [blame] | 6531 | DIFF_QUEUE_CLEAR(&outq); |
Junio C Hamano | fb13227 | 2007-08-03 13:33:31 -0700 | [diff] [blame] | 6532 | |
| 6533 | for (i = 0; i < q->nr; i++) { |
| 6534 | struct diff_filepair *p = q->queue[i]; |
| 6535 | |
Nguyễn Thái Ngọc Duy | b78ea5f | 2018-09-21 17:57:19 +0200 | [diff] [blame] | 6536 | if (diff_filespec_check_stat_unmatch(diffopt->repo, p)) |
Junio C Hamano | fb13227 | 2007-08-03 13:33:31 -0700 | [diff] [blame] | 6537 | diff_q(&outq, p); |
| 6538 | else { |
| 6539 | /* |
| 6540 | * The caller can subtract 1 from skip_stat_unmatch |
| 6541 | * to determine how many paths were dirty only |
| 6542 | * due to stat info mismatch. |
| 6543 | */ |
Brandon Williams | 0d1e0e7 | 2017-10-31 11:19:11 -0700 | [diff] [blame] | 6544 | if (!diffopt->flags.no_index) |
René Scharfe | 6d2d9e8 | 2007-08-15 00:41:00 +0200 | [diff] [blame] | 6545 | diffopt->skip_stat_unmatch++; |
Junio C Hamano | fb13227 | 2007-08-03 13:33:31 -0700 | [diff] [blame] | 6546 | diff_free_filepair(p); |
| 6547 | } |
| 6548 | } |
| 6549 | free(q->queue); |
| 6550 | *q = outq; |
| 6551 | } |
| 6552 | |
Junio C Hamano | 730f728 | 2009-09-20 00:03:39 -0700 | [diff] [blame] | 6553 | static int diffnamecmp(const void *a_, const void *b_) |
| 6554 | { |
| 6555 | const struct diff_filepair *a = *((const struct diff_filepair **)a_); |
| 6556 | const struct diff_filepair *b = *((const struct diff_filepair **)b_); |
| 6557 | const char *name_a, *name_b; |
| 6558 | |
| 6559 | name_a = a->one ? a->one->path : a->two->path; |
| 6560 | name_b = b->one ? b->one->path : b->two->path; |
| 6561 | return strcmp(name_a, name_b); |
| 6562 | } |
| 6563 | |
Jeff King | 784c0da | 2019-02-14 00:48:03 -0500 | [diff] [blame] | 6564 | void diffcore_fix_diff_index(void) |
Junio C Hamano | 730f728 | 2009-09-20 00:03:39 -0700 | [diff] [blame] | 6565 | { |
| 6566 | struct diff_queue_struct *q = &diff_queued_diff; |
René Scharfe | 9ed0d8d | 2016-09-29 17:27:31 +0200 | [diff] [blame] | 6567 | QSORT(q->queue, q->nr, diffnamecmp); |
Junio C Hamano | 730f728 | 2009-09-20 00:03:39 -0700 | [diff] [blame] | 6568 | } |
| 6569 | |
Jonathan Tan | 95acf11 | 2020-04-07 15:11:43 -0700 | [diff] [blame] | 6570 | void diff_add_if_missing(struct repository *r, |
| 6571 | struct oid_array *to_fetch, |
| 6572 | const struct diff_filespec *filespec) |
Jonathan Tan | 7fbbcb2 | 2019-04-05 10:09:34 -0700 | [diff] [blame] | 6573 | { |
| 6574 | if (filespec && filespec->oid_valid && |
Jonathan Tan | a63694f | 2019-08-20 13:53:20 -0700 | [diff] [blame] | 6575 | !S_ISGITLINK(filespec->mode) && |
Jonathan Tan | 7fbbcb2 | 2019-04-05 10:09:34 -0700 | [diff] [blame] | 6576 | oid_object_info_extended(r, &filespec->oid, NULL, |
| 6577 | OBJECT_INFO_FOR_PREFETCH)) |
| 6578 | oid_array_append(to_fetch, &filespec->oid); |
| 6579 | } |
| 6580 | |
Jonathan Tan | 95acf11 | 2020-04-07 15:11:43 -0700 | [diff] [blame] | 6581 | void diff_queued_diff_prefetch(void *repository) |
| 6582 | { |
| 6583 | struct repository *repo = repository; |
| 6584 | int i; |
| 6585 | struct diff_queue_struct *q = &diff_queued_diff; |
| 6586 | struct oid_array to_fetch = OID_ARRAY_INIT; |
| 6587 | |
| 6588 | for (i = 0; i < q->nr; i++) { |
| 6589 | struct diff_filepair *p = q->queue[i]; |
| 6590 | diff_add_if_missing(repo, &to_fetch, p->one); |
| 6591 | diff_add_if_missing(repo, &to_fetch, p->two); |
| 6592 | } |
| 6593 | |
| 6594 | /* |
| 6595 | * NEEDSWORK: Consider deduplicating the OIDs sent. |
| 6596 | */ |
| 6597 | promisor_remote_get_direct(repo, to_fetch.oid, to_fetch.nr); |
| 6598 | |
| 6599 | oid_array_clear(&to_fetch); |
| 6600 | } |
| 6601 | |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 6602 | void diffcore_std(struct diff_options *options) |
| 6603 | { |
Jonathan Tan | 95acf11 | 2020-04-07 15:11:43 -0700 | [diff] [blame] | 6604 | int output_formats_to_prefetch = DIFF_FORMAT_DIFFSTAT | |
| 6605 | DIFF_FORMAT_NUMSTAT | |
| 6606 | DIFF_FORMAT_PATCH | |
| 6607 | DIFF_FORMAT_SHORTSTAT | |
| 6608 | DIFF_FORMAT_DIRSTAT; |
Jonathan Tan | 7fbbcb2 | 2019-04-05 10:09:34 -0700 | [diff] [blame] | 6609 | |
Jonathan Tan | 95acf11 | 2020-04-07 15:11:43 -0700 | [diff] [blame] | 6610 | /* |
| 6611 | * Check if the user requested a blob-data-requiring diff output and/or |
| 6612 | * break-rewrite detection (which requires blob data). If yes, prefetch |
| 6613 | * the diff pairs. |
| 6614 | * |
| 6615 | * If no prefetching occurs, diffcore_rename() will prefetch if it |
| 6616 | * decides that it needs inexact rename detection. |
| 6617 | */ |
| 6618 | if (options->repo == the_repository && has_promisor_remote() && |
| 6619 | (options->output_format & output_formats_to_prefetch || |
| 6620 | options->pickaxe_opts & DIFF_PICKAXE_KINDS_MASK)) |
| 6621 | diff_queued_diff_prefetch(options->repo); |
Jonathan Tan | 7fbbcb2 | 2019-04-05 10:09:34 -0700 | [diff] [blame] | 6622 | |
Kirill Smelkov | 7195fbf | 2014-02-24 20:21:51 +0400 | [diff] [blame] | 6623 | /* NOTE please keep the following in sync with diff_tree_combined() */ |
Junio C Hamano | 9d86535 | 2008-09-06 19:09:16 -0700 | [diff] [blame] | 6624 | if (options->skip_stat_unmatch) |
Junio C Hamano | fb13227 | 2007-08-03 13:33:31 -0700 | [diff] [blame] | 6625 | diffcore_skip_stat_unmatch(options); |
Junio C Hamano | 44c48a9 | 2010-08-13 12:17:45 -0700 | [diff] [blame] | 6626 | if (!options->found_follow) { |
| 6627 | /* See try_to_follow_renames() in tree-diff.c */ |
| 6628 | if (options->break_opt != -1) |
Nguyễn Thái Ngọc Duy | b78ea5f | 2018-09-21 17:57:19 +0200 | [diff] [blame] | 6629 | diffcore_break(options->repo, |
| 6630 | options->break_opt); |
Junio C Hamano | 44c48a9 | 2010-08-13 12:17:45 -0700 | [diff] [blame] | 6631 | if (options->detect_rename) |
| 6632 | diffcore_rename(options); |
| 6633 | if (options->break_opt != -1) |
| 6634 | diffcore_merge_broken(); |
| 6635 | } |
Stefan Beller | cf63051 | 2018-01-04 14:50:41 -0800 | [diff] [blame] | 6636 | if (options->pickaxe_opts & DIFF_PICKAXE_KINDS_MASK) |
Junio C Hamano | 382f013 | 2010-08-31 13:44:39 -0700 | [diff] [blame] | 6637 | diffcore_pickaxe(options); |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 6638 | if (options->orderfile) |
| 6639 | diffcore_order(options->orderfile); |
Junio C Hamano | 44c48a9 | 2010-08-13 12:17:45 -0700 | [diff] [blame] | 6640 | if (!options->found_follow) |
| 6641 | /* See try_to_follow_renames() in tree-diff.c */ |
| 6642 | diff_resolve_rename_copy(); |
Junio C Hamano | 949226f | 2013-07-17 14:19:24 -0700 | [diff] [blame] | 6643 | diffcore_apply_filter(options); |
Junio C Hamano | 68aacb2 | 2007-03-14 11:12:13 -0700 | [diff] [blame] | 6644 | |
Brandon Williams | 0d1e0e7 | 2017-10-31 11:19:11 -0700 | [diff] [blame] | 6645 | if (diff_queued_diff.nr && !options->flags.diff_from_contents) |
| 6646 | options->flags.has_changes = 1; |
Pierre Habouzit | 8f67f8a | 2007-11-10 20:05:14 +0100 | [diff] [blame] | 6647 | else |
Brandon Williams | 0d1e0e7 | 2017-10-31 11:19:11 -0700 | [diff] [blame] | 6648 | options->flags.has_changes = 0; |
Bo Yang | 1da6175 | 2010-05-06 21:52:28 -0700 | [diff] [blame] | 6649 | |
Junio C Hamano | 44c48a9 | 2010-08-13 12:17:45 -0700 | [diff] [blame] | 6650 | options->found_follow = 0; |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 6651 | } |
| 6652 | |
Junio C Hamano | da31b35 | 2007-12-13 23:40:27 -0800 | [diff] [blame] | 6653 | int diff_result_code(struct diff_options *opt, int status) |
| 6654 | { |
| 6655 | int result = 0; |
Junio C Hamano | f31027c | 2011-01-06 13:50:06 -0800 | [diff] [blame] | 6656 | |
Max Nanasy | c9fc441 | 2013-03-21 12:53:38 -0700 | [diff] [blame] | 6657 | diff_warn_rename_limit("diff.renameLimit", |
Junio C Hamano | f31027c | 2011-01-06 13:50:06 -0800 | [diff] [blame] | 6658 | opt->needed_rename_limit, |
| 6659 | opt->degraded_cc_to_c); |
Brandon Williams | 0d1e0e7 | 2017-10-31 11:19:11 -0700 | [diff] [blame] | 6660 | if (!opt->flags.exit_with_status && |
Junio C Hamano | da31b35 | 2007-12-13 23:40:27 -0800 | [diff] [blame] | 6661 | !(opt->output_format & DIFF_FORMAT_CHECKDIFF)) |
| 6662 | return status; |
Brandon Williams | 0d1e0e7 | 2017-10-31 11:19:11 -0700 | [diff] [blame] | 6663 | if (opt->flags.exit_with_status && |
| 6664 | opt->flags.has_changes) |
Junio C Hamano | da31b35 | 2007-12-13 23:40:27 -0800 | [diff] [blame] | 6665 | result |= 01; |
| 6666 | if ((opt->output_format & DIFF_FORMAT_CHECKDIFF) && |
Brandon Williams | 0d1e0e7 | 2017-10-31 11:19:11 -0700 | [diff] [blame] | 6667 | opt->flags.check_failed) |
Junio C Hamano | da31b35 | 2007-12-13 23:40:27 -0800 | [diff] [blame] | 6668 | result |= 02; |
| 6669 | return result; |
| 6670 | } |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 6671 | |
Junio C Hamano | 28b9264 | 2011-05-31 09:14:17 -0700 | [diff] [blame] | 6672 | int diff_can_quit_early(struct diff_options *opt) |
| 6673 | { |
Brandon Williams | 0d1e0e7 | 2017-10-31 11:19:11 -0700 | [diff] [blame] | 6674 | return (opt->flags.quick && |
Junio C Hamano | 28b9264 | 2011-05-31 09:14:17 -0700 | [diff] [blame] | 6675 | !opt->filter && |
Brandon Williams | 0d1e0e7 | 2017-10-31 11:19:11 -0700 | [diff] [blame] | 6676 | opt->flags.has_changes); |
Junio C Hamano | 28b9264 | 2011-05-31 09:14:17 -0700 | [diff] [blame] | 6677 | } |
| 6678 | |
Jens Lehmann | aee9c7d | 2010-08-06 00:39:25 +0200 | [diff] [blame] | 6679 | /* |
| 6680 | * Shall changes to this submodule be ignored? |
| 6681 | * |
| 6682 | * Submodule changes can be configured to be ignored separately for each path, |
| 6683 | * but that configuration can be overridden from the command line. |
| 6684 | */ |
| 6685 | static int is_submodule_ignored(const char *path, struct diff_options *options) |
| 6686 | { |
| 6687 | int ignored = 0; |
Brandon Williams | 02f2f56 | 2017-10-31 11:19:05 -0700 | [diff] [blame] | 6688 | struct diff_flags orig_flags = options->flags; |
Brandon Williams | 0d1e0e7 | 2017-10-31 11:19:11 -0700 | [diff] [blame] | 6689 | if (!options->flags.override_submodule_config) |
Jens Lehmann | aee9c7d | 2010-08-06 00:39:25 +0200 | [diff] [blame] | 6690 | set_diffopt_flags_from_submodule_config(options, path); |
Brandon Williams | 0d1e0e7 | 2017-10-31 11:19:11 -0700 | [diff] [blame] | 6691 | if (options->flags.ignore_submodules) |
Jens Lehmann | aee9c7d | 2010-08-06 00:39:25 +0200 | [diff] [blame] | 6692 | ignored = 1; |
| 6693 | options->flags = orig_flags; |
| 6694 | return ignored; |
| 6695 | } |
| 6696 | |
Daniel Ferreira | e4cb659 | 2019-11-13 12:40:58 +0000 | [diff] [blame] | 6697 | void compute_diffstat(struct diff_options *options, |
| 6698 | struct diffstat_t *diffstat, |
| 6699 | struct diff_queue_struct *q) |
| 6700 | { |
| 6701 | int i; |
| 6702 | |
| 6703 | memset(diffstat, 0, sizeof(struct diffstat_t)); |
| 6704 | for (i = 0; i < q->nr; i++) { |
| 6705 | struct diff_filepair *p = q->queue[i]; |
| 6706 | if (check_pair_status(p)) |
| 6707 | diff_flush_stat(p, options, diffstat); |
| 6708 | } |
| 6709 | } |
| 6710 | |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 6711 | void diff_addremove(struct diff_options *options, |
| 6712 | int addremove, unsigned mode, |
Brandon Williams | c26022e | 2017-05-30 10:30:47 -0700 | [diff] [blame] | 6713 | const struct object_id *oid, |
| 6714 | int oid_valid, |
Jens Lehmann | e3d42c4 | 2010-01-18 21:26:18 +0100 | [diff] [blame] | 6715 | const char *concatpath, unsigned dirty_submodule) |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 6716 | { |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 6717 | struct diff_filespec *one, *two; |
| 6718 | |
Jens Lehmann | aee9c7d | 2010-08-06 00:39:25 +0200 | [diff] [blame] | 6719 | if (S_ISGITLINK(mode) && is_submodule_ignored(concatpath, options)) |
Johannes Schindelin | 50fd9bd | 2008-05-14 18:03:31 +0100 | [diff] [blame] | 6720 | return; |
| 6721 | |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 6722 | /* This may look odd, but it is a preparation for |
| 6723 | * feeding "there are unchanged files which should |
| 6724 | * not produce diffs, but when you are doing copy |
| 6725 | * detection you would need them, so here they are" |
| 6726 | * entries to the diff-core. They will be prefixed |
| 6727 | * with something like '=' or '*' (I haven't decided |
| 6728 | * which but should not make any difference). |
Junio C Hamano | a6080a0 | 2007-06-07 00:04:01 -0700 | [diff] [blame] | 6729 | * Feeding the same new and old to diff_change() |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 6730 | * also has the same effect. |
| 6731 | * Before the final output happens, they are pruned after |
| 6732 | * merged into rename/copy pairs as appropriate. |
| 6733 | */ |
Brandon Williams | 0d1e0e7 | 2017-10-31 11:19:11 -0700 | [diff] [blame] | 6734 | if (options->flags.reverse_diff) |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 6735 | addremove = (addremove == '+' ? '-' : |
| 6736 | addremove == '-' ? '+' : addremove); |
| 6737 | |
Junio C Hamano | cd676a5 | 2008-02-12 14:26:02 -0800 | [diff] [blame] | 6738 | if (options->prefix && |
| 6739 | strncmp(concatpath, options->prefix, options->prefix_length)) |
| 6740 | return; |
| 6741 | |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 6742 | one = alloc_filespec(concatpath); |
| 6743 | two = alloc_filespec(concatpath); |
| 6744 | |
| 6745 | if (addremove != '+') |
Brandon Williams | f9704c2 | 2017-05-30 10:30:50 -0700 | [diff] [blame] | 6746 | fill_filespec(one, oid, oid_valid, mode); |
Jens Lehmann | e3d42c4 | 2010-01-18 21:26:18 +0100 | [diff] [blame] | 6747 | if (addremove != '-') { |
Brandon Williams | f9704c2 | 2017-05-30 10:30:50 -0700 | [diff] [blame] | 6748 | fill_filespec(two, oid, oid_valid, mode); |
Jens Lehmann | e3d42c4 | 2010-01-18 21:26:18 +0100 | [diff] [blame] | 6749 | two->dirty_submodule = dirty_submodule; |
| 6750 | } |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 6751 | |
| 6752 | diff_queue(&diff_queued_diff, one, two); |
Brandon Williams | 0d1e0e7 | 2017-10-31 11:19:11 -0700 | [diff] [blame] | 6753 | if (!options->flags.diff_from_contents) |
| 6754 | options->flags.has_changes = 1; |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 6755 | } |
| 6756 | |
| 6757 | void diff_change(struct diff_options *options, |
| 6758 | unsigned old_mode, unsigned new_mode, |
Brandon Williams | 94a0097 | 2017-05-30 10:30:49 -0700 | [diff] [blame] | 6759 | const struct object_id *old_oid, |
| 6760 | const struct object_id *new_oid, |
| 6761 | int old_oid_valid, int new_oid_valid, |
Jens Lehmann | e3d42c4 | 2010-01-18 21:26:18 +0100 | [diff] [blame] | 6762 | const char *concatpath, |
| 6763 | unsigned old_dirty_submodule, unsigned new_dirty_submodule) |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 6764 | { |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 6765 | struct diff_filespec *one, *two; |
Nguyễn Thái Ngọc Duy | f34b205 | 2014-01-25 13:46:50 +0700 | [diff] [blame] | 6766 | struct diff_filepair *p; |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 6767 | |
Jens Lehmann | aee9c7d | 2010-08-06 00:39:25 +0200 | [diff] [blame] | 6768 | if (S_ISGITLINK(old_mode) && S_ISGITLINK(new_mode) && |
| 6769 | is_submodule_ignored(concatpath, options)) |
Johannes Schindelin | 50fd9bd | 2008-05-14 18:03:31 +0100 | [diff] [blame] | 6770 | return; |
| 6771 | |
Brandon Williams | 0d1e0e7 | 2017-10-31 11:19:11 -0700 | [diff] [blame] | 6772 | if (options->flags.reverse_diff) { |
René Scharfe | 35d803b | 2017-01-28 22:40:58 +0100 | [diff] [blame] | 6773 | SWAP(old_mode, new_mode); |
Brandon Williams | 94a0097 | 2017-05-30 10:30:49 -0700 | [diff] [blame] | 6774 | SWAP(old_oid, new_oid); |
| 6775 | SWAP(old_oid_valid, new_oid_valid); |
René Scharfe | 35d803b | 2017-01-28 22:40:58 +0100 | [diff] [blame] | 6776 | SWAP(old_dirty_submodule, new_dirty_submodule); |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 6777 | } |
Junio C Hamano | cd676a5 | 2008-02-12 14:26:02 -0800 | [diff] [blame] | 6778 | |
| 6779 | if (options->prefix && |
| 6780 | strncmp(concatpath, options->prefix, options->prefix_length)) |
| 6781 | return; |
| 6782 | |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 6783 | one = alloc_filespec(concatpath); |
| 6784 | two = alloc_filespec(concatpath); |
Brandon Williams | f9704c2 | 2017-05-30 10:30:50 -0700 | [diff] [blame] | 6785 | fill_filespec(one, old_oid, old_oid_valid, old_mode); |
| 6786 | fill_filespec(two, new_oid, new_oid_valid, new_mode); |
Jens Lehmann | e3d42c4 | 2010-01-18 21:26:18 +0100 | [diff] [blame] | 6787 | one->dirty_submodule = old_dirty_submodule; |
| 6788 | two->dirty_submodule = new_dirty_submodule; |
Nguyễn Thái Ngọc Duy | f34b205 | 2014-01-25 13:46:50 +0700 | [diff] [blame] | 6789 | p = diff_queue(&diff_queued_diff, one, two); |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 6790 | |
Brandon Williams | 0d1e0e7 | 2017-10-31 11:19:11 -0700 | [diff] [blame] | 6791 | if (options->flags.diff_from_contents) |
Nguyễn Thái Ngọc Duy | f34b205 | 2014-01-25 13:46:50 +0700 | [diff] [blame] | 6792 | return; |
| 6793 | |
Brandon Williams | 0d1e0e7 | 2017-10-31 11:19:11 -0700 | [diff] [blame] | 6794 | if (options->flags.quick && options->skip_stat_unmatch && |
Jeff King | d2d7fbe | 2020-06-01 16:22:18 -0400 | [diff] [blame] | 6795 | !diff_filespec_check_stat_unmatch(options->repo, p)) { |
| 6796 | diff_free_filespec_data(p->one); |
| 6797 | diff_free_filespec_data(p->two); |
Nguyễn Thái Ngọc Duy | f34b205 | 2014-01-25 13:46:50 +0700 | [diff] [blame] | 6798 | return; |
Jeff King | d2d7fbe | 2020-06-01 16:22:18 -0400 | [diff] [blame] | 6799 | } |
Nguyễn Thái Ngọc Duy | f34b205 | 2014-01-25 13:46:50 +0700 | [diff] [blame] | 6800 | |
Brandon Williams | 0d1e0e7 | 2017-10-31 11:19:11 -0700 | [diff] [blame] | 6801 | options->flags.has_changes = 1; |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 6802 | } |
| 6803 | |
Junio C Hamano | fa7b290 | 2011-04-22 16:05:58 -0700 | [diff] [blame] | 6804 | struct diff_filepair *diff_unmerge(struct diff_options *options, const char *path) |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 6805 | { |
Junio C Hamano | 76399c0 | 2011-04-22 15:55:55 -0700 | [diff] [blame] | 6806 | struct diff_filepair *pair; |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 6807 | struct diff_filespec *one, *two; |
Junio C Hamano | cd676a5 | 2008-02-12 14:26:02 -0800 | [diff] [blame] | 6808 | |
| 6809 | if (options->prefix && |
| 6810 | strncmp(path, options->prefix, options->prefix_length)) |
Junio C Hamano | 76399c0 | 2011-04-22 15:55:55 -0700 | [diff] [blame] | 6811 | return NULL; |
Junio C Hamano | cd676a5 | 2008-02-12 14:26:02 -0800 | [diff] [blame] | 6812 | |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 6813 | one = alloc_filespec(path); |
| 6814 | two = alloc_filespec(path); |
Junio C Hamano | 76399c0 | 2011-04-22 15:55:55 -0700 | [diff] [blame] | 6815 | pair = diff_queue(&diff_queued_diff, one, two); |
| 6816 | pair->is_unmerged = 1; |
| 6817 | return pair; |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 6818 | } |
Jeff King | 9cb92c3 | 2008-10-05 17:43:45 -0400 | [diff] [blame] | 6819 | |
Nguyễn Thái Ngọc Duy | b78ea5f | 2018-09-21 17:57:19 +0200 | [diff] [blame] | 6820 | static char *run_textconv(struct repository *r, |
| 6821 | const char *pgm, |
| 6822 | struct diff_filespec *spec, |
| 6823 | size_t *outsize) |
Jeff King | 9cb92c3 | 2008-10-05 17:43:45 -0400 | [diff] [blame] | 6824 | { |
Jeff King | 479b0ae | 2009-01-22 00:59:56 -0500 | [diff] [blame] | 6825 | struct diff_tempfile *temp; |
Jeff King | 9cb92c3 | 2008-10-05 17:43:45 -0400 | [diff] [blame] | 6826 | const char *argv[3]; |
| 6827 | const char **arg = argv; |
René Scharfe | d318027 | 2014-08-19 21:09:35 +0200 | [diff] [blame] | 6828 | struct child_process child = CHILD_PROCESS_INIT; |
Jeff King | 9cb92c3 | 2008-10-05 17:43:45 -0400 | [diff] [blame] | 6829 | struct strbuf buf = STRBUF_INIT; |
Johannes Sixt | da1fbed | 2010-03-30 19:36:03 +0200 | [diff] [blame] | 6830 | int err = 0; |
Jeff King | 9cb92c3 | 2008-10-05 17:43:45 -0400 | [diff] [blame] | 6831 | |
Nguyễn Thái Ngọc Duy | b78ea5f | 2018-09-21 17:57:19 +0200 | [diff] [blame] | 6832 | temp = prepare_temp_file(r, spec->path, spec); |
Jeff King | 9cb92c3 | 2008-10-05 17:43:45 -0400 | [diff] [blame] | 6833 | *arg++ = pgm; |
Jeff King | 479b0ae | 2009-01-22 00:59:56 -0500 | [diff] [blame] | 6834 | *arg++ = temp->name; |
Jeff King | 9cb92c3 | 2008-10-05 17:43:45 -0400 | [diff] [blame] | 6835 | *arg = NULL; |
| 6836 | |
Jeff King | 41a457e | 2009-12-30 06:01:09 -0500 | [diff] [blame] | 6837 | child.use_shell = 1; |
Jeff King | 9cb92c3 | 2008-10-05 17:43:45 -0400 | [diff] [blame] | 6838 | child.argv = argv; |
| 6839 | child.out = -1; |
Johannes Sixt | da1fbed | 2010-03-30 19:36:03 +0200 | [diff] [blame] | 6840 | if (start_command(&child)) { |
Jeff King | 479b0ae | 2009-01-22 00:59:56 -0500 | [diff] [blame] | 6841 | remove_tempfile(); |
Jeff King | 9cb92c3 | 2008-10-05 17:43:45 -0400 | [diff] [blame] | 6842 | return NULL; |
| 6843 | } |
Johannes Sixt | da1fbed | 2010-03-30 19:36:03 +0200 | [diff] [blame] | 6844 | |
| 6845 | if (strbuf_read(&buf, child.out, 0) < 0) |
| 6846 | err = error("error reading from textconv command '%s'", pgm); |
Jeff King | 70d7099 | 2009-12-30 04:02:53 -0500 | [diff] [blame] | 6847 | close(child.out); |
Johannes Sixt | da1fbed | 2010-03-30 19:36:03 +0200 | [diff] [blame] | 6848 | |
| 6849 | if (finish_command(&child) || err) { |
| 6850 | strbuf_release(&buf); |
| 6851 | remove_tempfile(); |
| 6852 | return NULL; |
| 6853 | } |
Jeff King | 479b0ae | 2009-01-22 00:59:56 -0500 | [diff] [blame] | 6854 | remove_tempfile(); |
Jeff King | 9cb92c3 | 2008-10-05 17:43:45 -0400 | [diff] [blame] | 6855 | |
| 6856 | return strbuf_detach(&buf, outsize); |
| 6857 | } |
Jeff King | 840383b | 2010-04-01 20:09:26 -0400 | [diff] [blame] | 6858 | |
Nguyễn Thái Ngọc Duy | 6afaf80 | 2018-09-21 17:57:22 +0200 | [diff] [blame] | 6859 | size_t fill_textconv(struct repository *r, |
| 6860 | struct userdiff_driver *driver, |
Axel Bonnet | a788d7d | 2010-06-07 17:23:36 +0200 | [diff] [blame] | 6861 | struct diff_filespec *df, |
| 6862 | char **outbuf) |
Jeff King | 840383b | 2010-04-01 20:09:26 -0400 | [diff] [blame] | 6863 | { |
| 6864 | size_t size; |
| 6865 | |
Jeff King | a64e6a4 | 2016-02-22 13:28:54 -0500 | [diff] [blame] | 6866 | if (!driver) { |
Jeff King | 840383b | 2010-04-01 20:09:26 -0400 | [diff] [blame] | 6867 | if (!DIFF_FILE_VALID(df)) { |
| 6868 | *outbuf = ""; |
| 6869 | return 0; |
| 6870 | } |
Jonathan Tan | 1c37e86 | 2020-04-07 15:11:41 -0700 | [diff] [blame] | 6871 | if (diff_populate_filespec(r, df, NULL)) |
Jeff King | 840383b | 2010-04-01 20:09:26 -0400 | [diff] [blame] | 6872 | die("unable to read files to diff"); |
| 6873 | *outbuf = df->data; |
| 6874 | return df->size; |
| 6875 | } |
| 6876 | |
Jeff King | a64e6a4 | 2016-02-22 13:28:54 -0500 | [diff] [blame] | 6877 | if (!driver->textconv) |
Johannes Schindelin | 033abf9 | 2018-05-02 11:38:39 +0200 | [diff] [blame] | 6878 | BUG("fill_textconv called with non-textconv driver"); |
Jeff King | a64e6a4 | 2016-02-22 13:28:54 -0500 | [diff] [blame] | 6879 | |
brian m. carlson | 41c9560 | 2016-06-24 23:09:24 +0000 | [diff] [blame] | 6880 | if (driver->textconv_cache && df->oid_valid) { |
brian m. carlson | a0d12c4 | 2016-06-24 23:09:23 +0000 | [diff] [blame] | 6881 | *outbuf = notes_cache_get(driver->textconv_cache, |
brian m. carlson | 569aa37 | 2017-05-06 22:09:58 +0000 | [diff] [blame] | 6882 | &df->oid, |
Jeff King | d9bae1a | 2010-04-01 20:12:15 -0400 | [diff] [blame] | 6883 | &size); |
| 6884 | if (*outbuf) |
| 6885 | return size; |
| 6886 | } |
| 6887 | |
Nguyễn Thái Ngọc Duy | b78ea5f | 2018-09-21 17:57:19 +0200 | [diff] [blame] | 6888 | *outbuf = run_textconv(r, driver->textconv, df, &size); |
Jeff King | 840383b | 2010-04-01 20:09:26 -0400 | [diff] [blame] | 6889 | if (!*outbuf) |
| 6890 | die("unable to read files to diff"); |
Jeff King | d9bae1a | 2010-04-01 20:12:15 -0400 | [diff] [blame] | 6891 | |
brian m. carlson | 41c9560 | 2016-06-24 23:09:24 +0000 | [diff] [blame] | 6892 | if (driver->textconv_cache && df->oid_valid) { |
Jeff King | d9bae1a | 2010-04-01 20:12:15 -0400 | [diff] [blame] | 6893 | /* ignore errors, as we might be in a readonly repository */ |
brian m. carlson | 569aa37 | 2017-05-06 22:09:58 +0000 | [diff] [blame] | 6894 | notes_cache_put(driver->textconv_cache, &df->oid, *outbuf, |
Jeff King | d9bae1a | 2010-04-01 20:12:15 -0400 | [diff] [blame] | 6895 | size); |
| 6896 | /* |
| 6897 | * we could save up changes and flush them all at the end, |
| 6898 | * but we would need an extra call after all diffing is done. |
| 6899 | * Since generating a cache entry is the slow path anyway, |
| 6900 | * this extra overhead probably isn't a big deal. |
| 6901 | */ |
| 6902 | notes_cache_write(driver->textconv_cache); |
| 6903 | } |
| 6904 | |
Jeff King | 840383b | 2010-04-01 20:09:26 -0400 | [diff] [blame] | 6905 | return size; |
| 6906 | } |
Nguyễn Thái Ngọc Duy | 4914c96 | 2012-10-26 22:53:52 +0700 | [diff] [blame] | 6907 | |
Nguyễn Thái Ngọc Duy | 6afaf80 | 2018-09-21 17:57:22 +0200 | [diff] [blame] | 6908 | int textconv_object(struct repository *r, |
| 6909 | const char *path, |
Jeff Smith | 3a35cb2 | 2017-05-24 00:15:10 -0500 | [diff] [blame] | 6910 | unsigned mode, |
| 6911 | const struct object_id *oid, |
| 6912 | int oid_valid, |
| 6913 | char **buf, |
| 6914 | unsigned long *buf_size) |
| 6915 | { |
| 6916 | struct diff_filespec *df; |
| 6917 | struct userdiff_driver *textconv; |
| 6918 | |
| 6919 | df = alloc_filespec(path); |
Junio C Hamano | a6f38c1 | 2017-06-19 12:38:44 -0700 | [diff] [blame] | 6920 | fill_filespec(df, oid, oid_valid, mode); |
Nguyễn Thái Ngọc Duy | bd7ad45 | 2018-11-10 06:49:06 +0100 | [diff] [blame] | 6921 | textconv = get_textconv(r, df); |
Jeff Smith | 3a35cb2 | 2017-05-24 00:15:10 -0500 | [diff] [blame] | 6922 | if (!textconv) { |
| 6923 | free_filespec(df); |
| 6924 | return 0; |
| 6925 | } |
| 6926 | |
Nguyễn Thái Ngọc Duy | 6afaf80 | 2018-09-21 17:57:22 +0200 | [diff] [blame] | 6927 | *buf_size = fill_textconv(r, textconv, df, buf); |
Jeff Smith | 3a35cb2 | 2017-05-24 00:15:10 -0500 | [diff] [blame] | 6928 | free_filespec(df); |
| 6929 | return 1; |
| 6930 | } |
| 6931 | |
Nguyễn Thái Ngọc Duy | 4914c96 | 2012-10-26 22:53:52 +0700 | [diff] [blame] | 6932 | void setup_diff_pager(struct diff_options *opt) |
| 6933 | { |
| 6934 | /* |
| 6935 | * If the user asked for our exit code, then either they want --quiet |
| 6936 | * or --exit-code. We should definitely not bother with a pager in the |
| 6937 | * former case, as we will generate no output. Since we still properly |
| 6938 | * report our exit code even when a pager is run, we _could_ run a |
| 6939 | * pager with --exit-code. But since we have not done so historically, |
| 6940 | * and because it is easy to find people oneline advising "git diff |
| 6941 | * --exit-code" in hooks and other scripts, we do not do so. |
| 6942 | */ |
Brandon Williams | 0d1e0e7 | 2017-10-31 11:19:11 -0700 | [diff] [blame] | 6943 | if (!opt->flags.exit_with_status && |
Nguyễn Thái Ngọc Duy | 4914c96 | 2012-10-26 22:53:52 +0700 | [diff] [blame] | 6944 | check_pager_config("diff") != 0) |
| 6945 | setup_pager(); |
| 6946 | } |