| 1 | /* |
| 2 | * Copyright (C) 2005 Junio C Hamano |
| 3 | */ |
| 4 | |
| 5 | #define USE_THE_REPOSITORY_VARIABLE |
| 6 | #define DISABLE_SIGN_COMPARE_WARNINGS |
| 7 | |
| 8 | #include "git-compat-util.h" |
| 9 | #include "abspath.h" |
| 10 | #include "base85.h" |
| 11 | #include "config.h" |
| 12 | #include "convert.h" |
| 13 | #include "environment.h" |
| 14 | #include "gettext.h" |
| 15 | #include "tempfile.h" |
| 16 | #include "revision.h" |
| 17 | #include "quote.h" |
| 18 | #include "diff.h" |
| 19 | #include "diffcore.h" |
| 20 | #include "delta.h" |
| 21 | #include "hex.h" |
| 22 | #include "xdiff-interface.h" |
| 23 | #include "color.h" |
| 24 | #include "run-command.h" |
| 25 | #include "utf8.h" |
| 26 | #include "odb.h" |
| 27 | #include "userdiff.h" |
| 28 | #include "submodule.h" |
| 29 | #include "hashmap.h" |
| 30 | #include "mem-pool.h" |
| 31 | #include "merge-ll.h" |
| 32 | #include "string-list.h" |
| 33 | #include "strvec.h" |
| 34 | #include "tmp-objdir.h" |
| 35 | #include "graph.h" |
| 36 | #include "oid-array.h" |
| 37 | #include "packfile.h" |
| 38 | #include "pager.h" |
| 39 | #include "parse-options.h" |
| 40 | #include "help.h" |
| 41 | #include "promisor-remote.h" |
| 42 | #include "dir.h" |
| 43 | #include "object-file.h" |
| 44 | #include "object-name.h" |
| 45 | #include "read-cache-ll.h" |
| 46 | #include "setup.h" |
| 47 | #include "strmap.h" |
| 48 | #include "ws.h" |
| 49 | |
| 50 | #ifdef NO_FAST_WORKING_DIRECTORY |
| 51 | #define FAST_WORKING_DIRECTORY 0 |
| 52 | #else |
| 53 | #define FAST_WORKING_DIRECTORY 1 |
| 54 | #endif |
| 55 | |
| 56 | static int diff_detect_rename_default; |
| 57 | static int diff_indent_heuristic = 1; |
| 58 | static int diff_rename_limit_default = 1000; |
| 59 | static int diff_suppress_blank_empty; |
| 60 | static enum git_colorbool diff_use_color_default = GIT_COLOR_UNKNOWN; |
| 61 | static int diff_color_moved_default; |
| 62 | static int diff_color_moved_ws_default; |
| 63 | static unsigned int diff_context_default = 3; |
| 64 | static unsigned int diff_interhunk_context_default; |
| 65 | static char *diff_word_regex_cfg; |
| 66 | static struct external_diff external_diff_cfg; |
| 67 | static char *diff_order_file_cfg; |
| 68 | int diff_auto_refresh_index = 1; |
| 69 | static int diff_mnemonic_prefix; |
| 70 | static int diff_no_prefix; |
| 71 | static char *diff_src_prefix; |
| 72 | static char *diff_dst_prefix; |
| 73 | static int diff_relative; |
| 74 | static int diff_stat_name_width; |
| 75 | static int diff_stat_graph_width; |
| 76 | static int diff_dirstat_permille_default = 30; |
| 77 | static struct diff_options default_diff_options; |
| 78 | static long diff_algorithm; |
| 79 | static unsigned ws_error_highlight_default = WSEH_NEW; |
| 80 | |
| 81 | static char diff_colors[][COLOR_MAXLEN] = { |
| 82 | GIT_COLOR_RESET, |
| 83 | GIT_COLOR_NORMAL, /* CONTEXT */ |
| 84 | GIT_COLOR_BOLD, /* METAINFO */ |
| 85 | GIT_COLOR_CYAN, /* FRAGINFO */ |
| 86 | GIT_COLOR_RED, /* OLD */ |
| 87 | GIT_COLOR_GREEN, /* NEW */ |
| 88 | GIT_COLOR_YELLOW, /* COMMIT */ |
| 89 | GIT_COLOR_BG_RED, /* WHITESPACE */ |
| 90 | GIT_COLOR_NORMAL, /* FUNCINFO */ |
| 91 | GIT_COLOR_BOLD_MAGENTA, /* OLD_MOVED */ |
| 92 | GIT_COLOR_BOLD_BLUE, /* OLD_MOVED ALTERNATIVE */ |
| 93 | GIT_COLOR_FAINT, /* OLD_MOVED_DIM */ |
| 94 | GIT_COLOR_FAINT_ITALIC, /* OLD_MOVED_ALTERNATIVE_DIM */ |
| 95 | GIT_COLOR_BOLD_CYAN, /* NEW_MOVED */ |
| 96 | GIT_COLOR_BOLD_YELLOW, /* NEW_MOVED ALTERNATIVE */ |
| 97 | GIT_COLOR_FAINT, /* NEW_MOVED_DIM */ |
| 98 | GIT_COLOR_FAINT_ITALIC, /* NEW_MOVED_ALTERNATIVE_DIM */ |
| 99 | GIT_COLOR_FAINT, /* CONTEXT_DIM */ |
| 100 | GIT_COLOR_FAINT_RED, /* OLD_DIM */ |
| 101 | GIT_COLOR_FAINT_GREEN, /* NEW_DIM */ |
| 102 | GIT_COLOR_BOLD, /* CONTEXT_BOLD */ |
| 103 | GIT_COLOR_BOLD_RED, /* OLD_BOLD */ |
| 104 | GIT_COLOR_BOLD_GREEN, /* NEW_BOLD */ |
| 105 | }; |
| 106 | |
| 107 | static const char *color_diff_slots[] = { |
| 108 | [DIFF_CONTEXT] = "context", |
| 109 | [DIFF_METAINFO] = "meta", |
| 110 | [DIFF_FRAGINFO] = "frag", |
| 111 | [DIFF_FILE_OLD] = "old", |
| 112 | [DIFF_FILE_NEW] = "new", |
| 113 | [DIFF_COMMIT] = "commit", |
| 114 | [DIFF_WHITESPACE] = "whitespace", |
| 115 | [DIFF_FUNCINFO] = "func", |
| 116 | [DIFF_FILE_OLD_MOVED] = "oldMoved", |
| 117 | [DIFF_FILE_OLD_MOVED_ALT] = "oldMovedAlternative", |
| 118 | [DIFF_FILE_OLD_MOVED_DIM] = "oldMovedDimmed", |
| 119 | [DIFF_FILE_OLD_MOVED_ALT_DIM] = "oldMovedAlternativeDimmed", |
| 120 | [DIFF_FILE_NEW_MOVED] = "newMoved", |
| 121 | [DIFF_FILE_NEW_MOVED_ALT] = "newMovedAlternative", |
| 122 | [DIFF_FILE_NEW_MOVED_DIM] = "newMovedDimmed", |
| 123 | [DIFF_FILE_NEW_MOVED_ALT_DIM] = "newMovedAlternativeDimmed", |
| 124 | [DIFF_CONTEXT_DIM] = "contextDimmed", |
| 125 | [DIFF_FILE_OLD_DIM] = "oldDimmed", |
| 126 | [DIFF_FILE_NEW_DIM] = "newDimmed", |
| 127 | [DIFF_CONTEXT_BOLD] = "contextBold", |
| 128 | [DIFF_FILE_OLD_BOLD] = "oldBold", |
| 129 | [DIFF_FILE_NEW_BOLD] = "newBold", |
| 130 | }; |
| 131 | |
| 132 | define_list_config_array_extra(color_diff_slots, {"plain"}); |
| 133 | |
| 134 | static int parse_diff_color_slot(const char *var) |
| 135 | { |
| 136 | if (!strcasecmp(var, "plain")) |
| 137 | return DIFF_CONTEXT; |
| 138 | return LOOKUP_CONFIG(color_diff_slots, var); |
| 139 | } |
| 140 | |
| 141 | static int parse_dirstat_params(struct diff_options *options, const char *params_string, |
| 142 | struct strbuf *errmsg) |
| 143 | { |
| 144 | char *params_copy = xstrdup(params_string); |
| 145 | struct string_list params = STRING_LIST_INIT_NODUP; |
| 146 | int ret = 0; |
| 147 | int i; |
| 148 | |
| 149 | if (*params_copy) |
| 150 | string_list_split_in_place(¶ms, params_copy, ",", -1); |
| 151 | for (i = 0; i < params.nr; i++) { |
| 152 | const char *p = params.items[i].string; |
| 153 | if (!strcmp(p, "changes")) { |
| 154 | options->flags.dirstat_by_line = 0; |
| 155 | options->flags.dirstat_by_file = 0; |
| 156 | } else if (!strcmp(p, "lines")) { |
| 157 | options->flags.dirstat_by_line = 1; |
| 158 | options->flags.dirstat_by_file = 0; |
| 159 | } else if (!strcmp(p, "files")) { |
| 160 | options->flags.dirstat_by_line = 0; |
| 161 | options->flags.dirstat_by_file = 1; |
| 162 | } else if (!strcmp(p, "noncumulative")) { |
| 163 | options->flags.dirstat_cumulative = 0; |
| 164 | } else if (!strcmp(p, "cumulative")) { |
| 165 | options->flags.dirstat_cumulative = 1; |
| 166 | } else if (isdigit(*p)) { |
| 167 | char *end; |
| 168 | int permille = strtoul(p, &end, 10) * 10; |
| 169 | if (*end == '.' && isdigit(*++end)) { |
| 170 | /* only use first digit */ |
| 171 | permille += *end - '0'; |
| 172 | /* .. and ignore any further digits */ |
| 173 | while (isdigit(*++end)) |
| 174 | ; /* nothing */ |
| 175 | } |
| 176 | if (!*end) |
| 177 | options->dirstat_permille = permille; |
| 178 | else { |
| 179 | strbuf_addf(errmsg, _(" Failed to parse dirstat cut-off percentage '%s'\n"), |
| 180 | p); |
| 181 | ret++; |
| 182 | } |
| 183 | } else { |
| 184 | strbuf_addf(errmsg, _(" Unknown dirstat parameter '%s'\n"), p); |
| 185 | ret++; |
| 186 | } |
| 187 | |
| 188 | } |
| 189 | string_list_clear(¶ms, 0); |
| 190 | free(params_copy); |
| 191 | return ret; |
| 192 | } |
| 193 | |
| 194 | static int parse_submodule_params(struct diff_options *options, const char *value) |
| 195 | { |
| 196 | if (!strcmp(value, "log")) |
| 197 | options->submodule_format = DIFF_SUBMODULE_LOG; |
| 198 | else if (!strcmp(value, "short")) |
| 199 | options->submodule_format = DIFF_SUBMODULE_SHORT; |
| 200 | else if (!strcmp(value, "diff")) |
| 201 | options->submodule_format = DIFF_SUBMODULE_INLINE_DIFF; |
| 202 | /* |
| 203 | * Please update $__git_diff_submodule_formats in |
| 204 | * git-completion.bash when you add new formats. |
| 205 | */ |
| 206 | else |
| 207 | return -1; |
| 208 | return 0; |
| 209 | } |
| 210 | |
| 211 | int git_config_rename(const char *var, const char *value) |
| 212 | { |
| 213 | if (!value) |
| 214 | return DIFF_DETECT_RENAME; |
| 215 | if (!strcasecmp(value, "copies") || !strcasecmp(value, "copy")) |
| 216 | return DIFF_DETECT_COPY; |
| 217 | return git_config_bool(var,value) ? DIFF_DETECT_RENAME : 0; |
| 218 | } |
| 219 | |
| 220 | long parse_algorithm_value(const char *value) |
| 221 | { |
| 222 | if (!value) |
| 223 | return -1; |
| 224 | else if (!strcasecmp(value, "myers") || !strcasecmp(value, "default")) |
| 225 | return 0; |
| 226 | else if (!strcasecmp(value, "minimal")) |
| 227 | return XDF_NEED_MINIMAL; |
| 228 | else if (!strcasecmp(value, "patience")) |
| 229 | return XDF_PATIENCE_DIFF; |
| 230 | else if (!strcasecmp(value, "histogram")) |
| 231 | return XDF_HISTOGRAM_DIFF; |
| 232 | /* |
| 233 | * Please update $__git_diff_algorithms in git-completion.bash |
| 234 | * when you add new algorithms. |
| 235 | */ |
| 236 | return -1; |
| 237 | } |
| 238 | |
| 239 | static int parse_one_token(const char **arg, const char *token) |
| 240 | { |
| 241 | const char *rest; |
| 242 | if (skip_prefix(*arg, token, &rest) && (!*rest || *rest == ',')) { |
| 243 | *arg = rest; |
| 244 | return 1; |
| 245 | } |
| 246 | return 0; |
| 247 | } |
| 248 | |
| 249 | static int parse_ws_error_highlight(const char *arg) |
| 250 | { |
| 251 | const char *orig_arg = arg; |
| 252 | unsigned val = 0; |
| 253 | |
| 254 | while (*arg) { |
| 255 | if (parse_one_token(&arg, "none")) |
| 256 | val = 0; |
| 257 | else if (parse_one_token(&arg, "default")) |
| 258 | val = WSEH_NEW; |
| 259 | else if (parse_one_token(&arg, "all")) |
| 260 | val = WSEH_NEW | WSEH_OLD | WSEH_CONTEXT; |
| 261 | else if (parse_one_token(&arg, "new")) |
| 262 | val |= WSEH_NEW; |
| 263 | else if (parse_one_token(&arg, "old")) |
| 264 | val |= WSEH_OLD; |
| 265 | else if (parse_one_token(&arg, "context")) |
| 266 | val |= WSEH_CONTEXT; |
| 267 | else { |
| 268 | return -1 - (int)(arg - orig_arg); |
| 269 | } |
| 270 | if (*arg) |
| 271 | arg++; |
| 272 | } |
| 273 | return val; |
| 274 | } |
| 275 | |
| 276 | /* |
| 277 | * These are to give UI layer defaults. |
| 278 | * The core-level commands such as git-diff-files should |
| 279 | * never be affected by the setting of diff.renames |
| 280 | * the user happens to have in the configuration file. |
| 281 | */ |
| 282 | void init_diff_ui_defaults(void) |
| 283 | { |
| 284 | diff_detect_rename_default = DIFF_DETECT_RENAME; |
| 285 | } |
| 286 | |
| 287 | int git_diff_heuristic_config(const char *var, const char *value, |
| 288 | void *cb UNUSED) |
| 289 | { |
| 290 | if (!strcmp(var, "diff.indentheuristic")) |
| 291 | diff_indent_heuristic = git_config_bool(var, value); |
| 292 | return 0; |
| 293 | } |
| 294 | |
| 295 | static int parse_color_moved(const char *arg) |
| 296 | { |
| 297 | switch (git_parse_maybe_bool(arg)) { |
| 298 | case 0: |
| 299 | return COLOR_MOVED_NO; |
| 300 | case 1: |
| 301 | return COLOR_MOVED_DEFAULT; |
| 302 | default: |
| 303 | break; |
| 304 | } |
| 305 | |
| 306 | if (!strcmp(arg, "no")) |
| 307 | return COLOR_MOVED_NO; |
| 308 | else if (!strcmp(arg, "plain")) |
| 309 | return COLOR_MOVED_PLAIN; |
| 310 | else if (!strcmp(arg, "blocks")) |
| 311 | return COLOR_MOVED_BLOCKS; |
| 312 | else if (!strcmp(arg, "zebra")) |
| 313 | return COLOR_MOVED_ZEBRA; |
| 314 | else if (!strcmp(arg, "default")) |
| 315 | return COLOR_MOVED_DEFAULT; |
| 316 | else if (!strcmp(arg, "dimmed-zebra")) |
| 317 | return COLOR_MOVED_ZEBRA_DIM; |
| 318 | else if (!strcmp(arg, "dimmed_zebra")) |
| 319 | return COLOR_MOVED_ZEBRA_DIM; |
| 320 | else |
| 321 | return error(_("color moved setting must be one of 'no', 'default', 'blocks', 'zebra', 'dimmed-zebra', 'plain'")); |
| 322 | } |
| 323 | |
| 324 | static unsigned parse_color_moved_ws(const char *arg) |
| 325 | { |
| 326 | int ret = 0; |
| 327 | struct string_list l = STRING_LIST_INIT_DUP; |
| 328 | struct string_list_item *i; |
| 329 | |
| 330 | string_list_split_f(&l, arg, ",", -1, STRING_LIST_SPLIT_TRIM); |
| 331 | |
| 332 | for_each_string_list_item(i, &l) { |
| 333 | if (!strcmp(i->string, "no")) |
| 334 | ret = 0; |
| 335 | else if (!strcmp(i->string, "ignore-space-change")) |
| 336 | ret |= XDF_IGNORE_WHITESPACE_CHANGE; |
| 337 | else if (!strcmp(i->string, "ignore-space-at-eol")) |
| 338 | ret |= XDF_IGNORE_WHITESPACE_AT_EOL; |
| 339 | else if (!strcmp(i->string, "ignore-all-space")) |
| 340 | ret |= XDF_IGNORE_WHITESPACE; |
| 341 | else if (!strcmp(i->string, "allow-indentation-change")) |
| 342 | ret |= COLOR_MOVED_WS_ALLOW_INDENTATION_CHANGE; |
| 343 | else { |
| 344 | ret |= COLOR_MOVED_WS_ERROR; |
| 345 | error(_("unknown color-moved-ws mode '%s', possible values are 'ignore-space-change', 'ignore-space-at-eol', 'ignore-all-space', 'allow-indentation-change'"), i->string); |
| 346 | } |
| 347 | } |
| 348 | |
| 349 | if ((ret & COLOR_MOVED_WS_ALLOW_INDENTATION_CHANGE) && |
| 350 | (ret & XDF_WHITESPACE_FLAGS)) { |
| 351 | error(_("color-moved-ws: allow-indentation-change cannot be combined with other whitespace modes")); |
| 352 | ret |= COLOR_MOVED_WS_ERROR; |
| 353 | } |
| 354 | |
| 355 | string_list_clear(&l, 0); |
| 356 | |
| 357 | return ret; |
| 358 | } |
| 359 | |
| 360 | int git_diff_ui_config(const char *var, const char *value, |
| 361 | const struct config_context *ctx, void *cb) |
| 362 | { |
| 363 | if (!strcmp(var, "diff.color") || !strcmp(var, "color.diff")) { |
| 364 | diff_use_color_default = git_config_colorbool(var, value); |
| 365 | return 0; |
| 366 | } |
| 367 | if (!strcmp(var, "diff.colormoved")) { |
| 368 | int cm = parse_color_moved(value); |
| 369 | if (cm < 0) |
| 370 | return -1; |
| 371 | diff_color_moved_default = cm; |
| 372 | return 0; |
| 373 | } |
| 374 | if (!strcmp(var, "diff.colormovedws")) { |
| 375 | unsigned cm; |
| 376 | if (!value) |
| 377 | return config_error_nonbool(var); |
| 378 | cm = parse_color_moved_ws(value); |
| 379 | if (cm & COLOR_MOVED_WS_ERROR) |
| 380 | return -1; |
| 381 | diff_color_moved_ws_default = cm; |
| 382 | return 0; |
| 383 | } |
| 384 | if (!strcmp(var, "diff.context")) { |
| 385 | int val = git_config_int(var, value, ctx->kvi); |
| 386 | if (val < 0) |
| 387 | return -1; |
| 388 | diff_context_default = val; |
| 389 | return 0; |
| 390 | } |
| 391 | if (!strcmp(var, "diff.interhunkcontext")) { |
| 392 | int val = git_config_int(var, value, ctx->kvi); |
| 393 | if (val < 0) |
| 394 | return -1; |
| 395 | diff_interhunk_context_default = val; |
| 396 | return 0; |
| 397 | } |
| 398 | if (!strcmp(var, "diff.renames")) { |
| 399 | diff_detect_rename_default = git_config_rename(var, value); |
| 400 | return 0; |
| 401 | } |
| 402 | if (!strcmp(var, "diff.autorefreshindex")) { |
| 403 | diff_auto_refresh_index = git_config_bool(var, value); |
| 404 | return 0; |
| 405 | } |
| 406 | if (!strcmp(var, "diff.mnemonicprefix")) { |
| 407 | diff_mnemonic_prefix = git_config_bool(var, value); |
| 408 | return 0; |
| 409 | } |
| 410 | if (!strcmp(var, "diff.noprefix")) { |
| 411 | diff_no_prefix = git_config_bool(var, value); |
| 412 | return 0; |
| 413 | } |
| 414 | if (!strcmp(var, "diff.srcprefix")) { |
| 415 | FREE_AND_NULL(diff_src_prefix); |
| 416 | return git_config_string(&diff_src_prefix, var, value); |
| 417 | } |
| 418 | if (!strcmp(var, "diff.dstprefix")) { |
| 419 | FREE_AND_NULL(diff_dst_prefix); |
| 420 | return git_config_string(&diff_dst_prefix, var, value); |
| 421 | } |
| 422 | if (!strcmp(var, "diff.relative")) { |
| 423 | diff_relative = git_config_bool(var, value); |
| 424 | return 0; |
| 425 | } |
| 426 | if (!strcmp(var, "diff.statnamewidth")) { |
| 427 | diff_stat_name_width = git_config_int(var, value, ctx->kvi); |
| 428 | return 0; |
| 429 | } |
| 430 | if (!strcmp(var, "diff.statgraphwidth")) { |
| 431 | diff_stat_graph_width = git_config_int(var, value, ctx->kvi); |
| 432 | return 0; |
| 433 | } |
| 434 | if (!strcmp(var, "diff.external")) |
| 435 | return git_config_string(&external_diff_cfg.cmd, var, value); |
| 436 | if (!strcmp(var, "diff.trustexitcode")) { |
| 437 | external_diff_cfg.trust_exit_code = git_config_bool(var, value); |
| 438 | return 0; |
| 439 | } |
| 440 | if (!strcmp(var, "diff.wordregex")) |
| 441 | return git_config_string(&diff_word_regex_cfg, var, value); |
| 442 | if (!strcmp(var, "diff.orderfile")) { |
| 443 | FREE_AND_NULL(diff_order_file_cfg); |
| 444 | return git_config_pathname(&diff_order_file_cfg, var, value); |
| 445 | } |
| 446 | |
| 447 | if (!strcmp(var, "diff.ignoresubmodules")) { |
| 448 | if (!value) |
| 449 | return config_error_nonbool(var); |
| 450 | handle_ignore_submodules_arg(&default_diff_options, value); |
| 451 | } |
| 452 | |
| 453 | if (!strcmp(var, "diff.submodule")) { |
| 454 | if (!value) |
| 455 | return config_error_nonbool(var); |
| 456 | if (parse_submodule_params(&default_diff_options, value)) |
| 457 | warning(_("Unknown value for 'diff.submodule' config variable: '%s'"), |
| 458 | value); |
| 459 | return 0; |
| 460 | } |
| 461 | |
| 462 | if (!strcmp(var, "diff.algorithm")) { |
| 463 | if (!value) |
| 464 | return config_error_nonbool(var); |
| 465 | diff_algorithm = parse_algorithm_value(value); |
| 466 | if (diff_algorithm < 0) |
| 467 | return error(_("unknown value for config '%s': %s"), |
| 468 | var, value); |
| 469 | return 0; |
| 470 | } |
| 471 | |
| 472 | if (git_color_config(var, value, cb) < 0) |
| 473 | return -1; |
| 474 | |
| 475 | return git_diff_basic_config(var, value, ctx, cb); |
| 476 | } |
| 477 | |
| 478 | int git_diff_basic_config(const char *var, const char *value, |
| 479 | const struct config_context *ctx, void *cb) |
| 480 | { |
| 481 | const char *name; |
| 482 | |
| 483 | if (!strcmp(var, "diff.renamelimit")) { |
| 484 | diff_rename_limit_default = git_config_int(var, value, ctx->kvi); |
| 485 | return 0; |
| 486 | } |
| 487 | |
| 488 | if (userdiff_config(var, value) < 0) |
| 489 | return -1; |
| 490 | |
| 491 | if (skip_prefix(var, "diff.color.", &name) || |
| 492 | skip_prefix(var, "color.diff.", &name)) { |
| 493 | int slot = parse_diff_color_slot(name); |
| 494 | if (slot < 0) |
| 495 | return 0; |
| 496 | if (!value) |
| 497 | return config_error_nonbool(var); |
| 498 | return color_parse(value, diff_colors[slot]); |
| 499 | } |
| 500 | |
| 501 | if (!strcmp(var, "diff.wserrorhighlight")) { |
| 502 | int val; |
| 503 | if (!value) |
| 504 | return config_error_nonbool(var); |
| 505 | val = parse_ws_error_highlight(value); |
| 506 | if (val < 0) |
| 507 | return error(_("unknown value for config '%s': %s"), |
| 508 | var, value); |
| 509 | ws_error_highlight_default = val; |
| 510 | return 0; |
| 511 | } |
| 512 | |
| 513 | /* like GNU diff's --suppress-blank-empty option */ |
| 514 | if (!strcmp(var, "diff.suppressblankempty") || |
| 515 | /* for backwards compatibility */ |
| 516 | !strcmp(var, "diff.suppress-blank-empty")) { |
| 517 | diff_suppress_blank_empty = git_config_bool(var, value); |
| 518 | return 0; |
| 519 | } |
| 520 | |
| 521 | if (!strcmp(var, "diff.dirstat")) { |
| 522 | struct strbuf errmsg = STRBUF_INIT; |
| 523 | if (!value) |
| 524 | return config_error_nonbool(var); |
| 525 | default_diff_options.dirstat_permille = diff_dirstat_permille_default; |
| 526 | if (parse_dirstat_params(&default_diff_options, value, &errmsg)) |
| 527 | warning(_("Found errors in 'diff.dirstat' config variable:\n%s"), |
| 528 | errmsg.buf); |
| 529 | strbuf_release(&errmsg); |
| 530 | diff_dirstat_permille_default = default_diff_options.dirstat_permille; |
| 531 | return 0; |
| 532 | } |
| 533 | |
| 534 | if (git_diff_heuristic_config(var, value, cb) < 0) |
| 535 | return -1; |
| 536 | |
| 537 | return git_default_config(var, value, ctx, cb); |
| 538 | } |
| 539 | |
| 540 | static char *quote_two(const char *one, const char *two) |
| 541 | { |
| 542 | int need_one = quote_c_style(one, NULL, NULL, CQUOTE_NODQ); |
| 543 | int need_two = quote_c_style(two, NULL, NULL, CQUOTE_NODQ); |
| 544 | struct strbuf res = STRBUF_INIT; |
| 545 | |
| 546 | if (need_one + need_two) { |
| 547 | strbuf_addch(&res, '"'); |
| 548 | quote_c_style(one, &res, NULL, CQUOTE_NODQ); |
| 549 | quote_c_style(two, &res, NULL, CQUOTE_NODQ); |
| 550 | strbuf_addch(&res, '"'); |
| 551 | } else { |
| 552 | strbuf_addstr(&res, one); |
| 553 | strbuf_addstr(&res, two); |
| 554 | } |
| 555 | return strbuf_detach(&res, NULL); |
| 556 | } |
| 557 | |
| 558 | static const struct external_diff *external_diff(void) |
| 559 | { |
| 560 | static struct external_diff external_diff_env, *external_diff_ptr; |
| 561 | static int done_preparing = 0; |
| 562 | |
| 563 | if (done_preparing) |
| 564 | return external_diff_ptr; |
| 565 | external_diff_env.cmd = xstrdup_or_null(getenv("GIT_EXTERNAL_DIFF")); |
| 566 | if (git_env_bool("GIT_EXTERNAL_DIFF_TRUST_EXIT_CODE", 0)) |
| 567 | external_diff_env.trust_exit_code = 1; |
| 568 | if (external_diff_env.cmd) |
| 569 | external_diff_ptr = &external_diff_env; |
| 570 | else if (external_diff_cfg.cmd) |
| 571 | external_diff_ptr = &external_diff_cfg; |
| 572 | done_preparing = 1; |
| 573 | return external_diff_ptr; |
| 574 | } |
| 575 | |
| 576 | /* |
| 577 | * Keep track of files used for diffing. Sometimes such an entry |
| 578 | * refers to a temporary file, sometimes to an existing file, and |
| 579 | * sometimes to "/dev/null". |
| 580 | */ |
| 581 | static struct diff_tempfile { |
| 582 | /* |
| 583 | * filename external diff should read from, or NULL if this |
| 584 | * entry is currently not in use: |
| 585 | */ |
| 586 | const char *name; |
| 587 | |
| 588 | char hex[GIT_MAX_HEXSZ + 1]; |
| 589 | char mode[10]; |
| 590 | |
| 591 | /* |
| 592 | * If this diff_tempfile instance refers to a temporary file, |
| 593 | * this tempfile object is used to manage its lifetime. |
| 594 | */ |
| 595 | struct tempfile *tempfile; |
| 596 | } diff_temp[2]; |
| 597 | |
| 598 | struct emit_callback { |
| 599 | int color_diff; |
| 600 | unsigned ws_rule; |
| 601 | int blank_at_eof_in_preimage; |
| 602 | int blank_at_eof_in_postimage; |
| 603 | int lno_in_preimage; |
| 604 | int lno_in_postimage; |
| 605 | int last_line_kind; |
| 606 | const char **label_path; |
| 607 | struct diff_words_data *diff_words; |
| 608 | struct diff_options *opt; |
| 609 | struct strbuf *header; |
| 610 | }; |
| 611 | |
| 612 | /* |
| 613 | * State for the line-range callback wrappers that sit between |
| 614 | * xdi_diff_outf() and fn_out_consume(). xdiff produces a normal, |
| 615 | * unfiltered diff; the wrappers intercept each hunk header and line, |
| 616 | * track post-image position, and forward only lines that fall within |
| 617 | * the requested ranges. Contiguous in-range lines are collected into |
| 618 | * range hunks and flushed with a synthetic @@ header so that |
| 619 | * fn_out_consume() sees well-formed unified-diff fragments. |
| 620 | * |
| 621 | * Removal lines ('-') cannot be classified by post-image position, so |
| 622 | * they are buffered in pending_rm until the next '+' or ' ' line |
| 623 | * reveals whether they precede an in-range line (flush into range hunk) or |
| 624 | * an out-of-range line (discard). |
| 625 | */ |
| 626 | struct line_range_callback { |
| 627 | xdiff_emit_line_fn orig_line_fn; |
| 628 | void *orig_cb_data; |
| 629 | const struct range_set *ranges; /* 0-based [start, end) */ |
| 630 | unsigned int cur_range; /* index into the range_set */ |
| 631 | |
| 632 | /* Post/pre-image line counters (1-based, set from hunk headers) */ |
| 633 | long lno_post; |
| 634 | long lno_pre; |
| 635 | |
| 636 | /* |
| 637 | * Function name from most recent xdiff hunk header; |
| 638 | * size matches struct func_line.buf in xdiff/xemit.c. |
| 639 | */ |
| 640 | char func[80]; |
| 641 | long funclen; |
| 642 | |
| 643 | /* Range hunk being accumulated for the current range */ |
| 644 | struct strbuf rhunk; |
| 645 | long rhunk_old_begin, rhunk_old_count; |
| 646 | long rhunk_new_begin, rhunk_new_count; |
| 647 | int rhunk_active; |
| 648 | int rhunk_has_changes; /* any '+' or '-' lines? */ |
| 649 | |
| 650 | /* Removal lines not yet known to be in-range */ |
| 651 | struct strbuf pending_rm; |
| 652 | int pending_rm_count; |
| 653 | long pending_rm_pre_begin; /* pre-image line of first pending */ |
| 654 | |
| 655 | int ret; /* latched error from orig_line_fn */ |
| 656 | }; |
| 657 | |
| 658 | static int count_lines(const char *data, int size) |
| 659 | { |
| 660 | int count, ch, completely_empty = 1, nl_just_seen = 0; |
| 661 | count = 0; |
| 662 | while (0 < size--) { |
| 663 | ch = *data++; |
| 664 | if (ch == '\n') { |
| 665 | count++; |
| 666 | nl_just_seen = 1; |
| 667 | completely_empty = 0; |
| 668 | } |
| 669 | else { |
| 670 | nl_just_seen = 0; |
| 671 | completely_empty = 0; |
| 672 | } |
| 673 | } |
| 674 | if (completely_empty) |
| 675 | return 0; |
| 676 | if (!nl_just_seen) |
| 677 | count++; /* no trailing newline */ |
| 678 | return count; |
| 679 | } |
| 680 | |
| 681 | static int fill_mmfile(struct repository *r, mmfile_t *mf, |
| 682 | struct diff_filespec *one) |
| 683 | { |
| 684 | if (!DIFF_FILE_VALID(one)) { |
| 685 | mf->ptr = (char *)""; /* does not matter */ |
| 686 | mf->size = 0; |
| 687 | return 0; |
| 688 | } |
| 689 | else if (diff_populate_filespec(r, one, NULL)) |
| 690 | return -1; |
| 691 | |
| 692 | mf->ptr = one->data; |
| 693 | mf->size = one->size; |
| 694 | return 0; |
| 695 | } |
| 696 | |
| 697 | /* like fill_mmfile, but only for size, so we can avoid retrieving blob */ |
| 698 | static unsigned long diff_filespec_size(struct repository *r, |
| 699 | struct diff_filespec *one) |
| 700 | { |
| 701 | struct diff_populate_filespec_options dpf_options = { |
| 702 | .check_size_only = 1, |
| 703 | }; |
| 704 | |
| 705 | if (!DIFF_FILE_VALID(one)) |
| 706 | return 0; |
| 707 | diff_populate_filespec(r, one, &dpf_options); |
| 708 | return one->size; |
| 709 | } |
| 710 | |
| 711 | static int count_trailing_blank(mmfile_t *mf) |
| 712 | { |
| 713 | char *ptr = mf->ptr; |
| 714 | long size = mf->size; |
| 715 | int cnt = 0; |
| 716 | |
| 717 | if (!size) |
| 718 | return cnt; |
| 719 | ptr += size - 1; /* pointing at the very end */ |
| 720 | if (*ptr != '\n') |
| 721 | ; /* incomplete line */ |
| 722 | else |
| 723 | ptr--; /* skip the last LF */ |
| 724 | while (mf->ptr < ptr) { |
| 725 | char *prev_eol; |
| 726 | for (prev_eol = ptr; mf->ptr <= prev_eol; prev_eol--) |
| 727 | if (*prev_eol == '\n') |
| 728 | break; |
| 729 | if (!ws_blank_line(prev_eol + 1, ptr - prev_eol)) |
| 730 | break; |
| 731 | cnt++; |
| 732 | ptr = prev_eol - 1; |
| 733 | } |
| 734 | return cnt; |
| 735 | } |
| 736 | |
| 737 | static void check_blank_at_eof(mmfile_t *mf1, mmfile_t *mf2, |
| 738 | struct emit_callback *ecbdata) |
| 739 | { |
| 740 | int l1, l2, at; |
| 741 | l1 = count_trailing_blank(mf1); |
| 742 | l2 = count_trailing_blank(mf2); |
| 743 | if (l2 <= l1) { |
| 744 | ecbdata->blank_at_eof_in_preimage = 0; |
| 745 | ecbdata->blank_at_eof_in_postimage = 0; |
| 746 | return; |
| 747 | } |
| 748 | at = count_lines(mf1->ptr, mf1->size); |
| 749 | ecbdata->blank_at_eof_in_preimage = (at - l1) + 1; |
| 750 | |
| 751 | at = count_lines(mf2->ptr, mf2->size); |
| 752 | ecbdata->blank_at_eof_in_postimage = (at - l2) + 1; |
| 753 | } |
| 754 | |
| 755 | static void emit_line_0(struct diff_options *o, |
| 756 | const char *set_sign, const char *set, unsigned reverse, const char *reset, |
| 757 | int first, const char *line, int len) |
| 758 | { |
| 759 | int has_trailing_newline, has_trailing_carriage_return; |
| 760 | int needs_reset = 0; /* at the end of the line */ |
| 761 | FILE *file = o->file; |
| 762 | |
| 763 | fputs(diff_line_prefix(o), file); |
| 764 | |
| 765 | has_trailing_newline = (len > 0 && line[len-1] == '\n'); |
| 766 | if (has_trailing_newline) |
| 767 | len--; |
| 768 | |
| 769 | has_trailing_carriage_return = (len > 0 && line[len-1] == '\r'); |
| 770 | if (has_trailing_carriage_return) |
| 771 | len--; |
| 772 | |
| 773 | if (!len && !first) |
| 774 | goto end_of_line; |
| 775 | |
| 776 | if (reverse && want_color(o->use_color)) { |
| 777 | fputs(GIT_COLOR_REVERSE, file); |
| 778 | needs_reset = 1; |
| 779 | } |
| 780 | |
| 781 | if (set_sign) { |
| 782 | fputs(set_sign, file); |
| 783 | needs_reset = 1; |
| 784 | } |
| 785 | |
| 786 | if (first) |
| 787 | fputc(first, file); |
| 788 | |
| 789 | if (!len) |
| 790 | goto end_of_line; |
| 791 | |
| 792 | if (set) { |
| 793 | if (set_sign && set != set_sign) |
| 794 | fputs(reset, file); |
| 795 | fputs(set, file); |
| 796 | needs_reset = 1; |
| 797 | } |
| 798 | fwrite(line, len, 1, file); |
| 799 | needs_reset = 1; /* 'line' may contain color codes. */ |
| 800 | |
| 801 | end_of_line: |
| 802 | if (needs_reset) |
| 803 | fputs(reset, file); |
| 804 | if (has_trailing_carriage_return) |
| 805 | fputc('\r', file); |
| 806 | if (has_trailing_newline) |
| 807 | fputc('\n', file); |
| 808 | } |
| 809 | |
| 810 | static void emit_line(struct diff_options *o, const char *set, const char *reset, |
| 811 | const char *line, int len) |
| 812 | { |
| 813 | emit_line_0(o, set, NULL, 0, reset, 0, line, len); |
| 814 | } |
| 815 | |
| 816 | enum diff_symbol { |
| 817 | DIFF_SYMBOL_BINARY_DIFF_HEADER, |
| 818 | DIFF_SYMBOL_BINARY_DIFF_HEADER_DELTA, |
| 819 | DIFF_SYMBOL_BINARY_DIFF_HEADER_LITERAL, |
| 820 | DIFF_SYMBOL_BINARY_DIFF_BODY, |
| 821 | DIFF_SYMBOL_BINARY_DIFF_FOOTER, |
| 822 | DIFF_SYMBOL_STATS_SUMMARY_NO_FILES, |
| 823 | DIFF_SYMBOL_STATS_SUMMARY_ABBREV, |
| 824 | DIFF_SYMBOL_STATS_SUMMARY_INSERTS_DELETES, |
| 825 | DIFF_SYMBOL_STATS_LINE, |
| 826 | DIFF_SYMBOL_WORD_DIFF, |
| 827 | DIFF_SYMBOL_STAT_SEP, |
| 828 | DIFF_SYMBOL_SUMMARY, |
| 829 | DIFF_SYMBOL_SUBMODULE_ADD, |
| 830 | DIFF_SYMBOL_SUBMODULE_DEL, |
| 831 | DIFF_SYMBOL_SUBMODULE_UNTRACKED, |
| 832 | DIFF_SYMBOL_SUBMODULE_MODIFIED, |
| 833 | DIFF_SYMBOL_SUBMODULE_HEADER, |
| 834 | DIFF_SYMBOL_SUBMODULE_ERROR, |
| 835 | DIFF_SYMBOL_SUBMODULE_PIPETHROUGH, |
| 836 | DIFF_SYMBOL_REWRITE_DIFF, |
| 837 | DIFF_SYMBOL_BINARY_FILES, |
| 838 | DIFF_SYMBOL_HEADER, |
| 839 | DIFF_SYMBOL_FILEPAIR_PLUS, |
| 840 | DIFF_SYMBOL_FILEPAIR_MINUS, |
| 841 | DIFF_SYMBOL_WORDS_PORCELAIN, |
| 842 | DIFF_SYMBOL_WORDS, |
| 843 | DIFF_SYMBOL_CONTEXT, |
| 844 | DIFF_SYMBOL_CONTEXT_INCOMPLETE, |
| 845 | DIFF_SYMBOL_PLUS, |
| 846 | DIFF_SYMBOL_MINUS, |
| 847 | DIFF_SYMBOL_CONTEXT_FRAGINFO, |
| 848 | DIFF_SYMBOL_CONTEXT_MARKER, |
| 849 | DIFF_SYMBOL_SEPARATOR |
| 850 | }; |
| 851 | |
| 852 | /* |
| 853 | * Flags for content lines: |
| 854 | * 0..15 are whitespace rules (see ws.h) |
| 855 | * 16..18 are WSEH_NEW | WSEH_CONTEXT | WSEH_OLD |
| 856 | * 19 is marking if the line is blank at EOF |
| 857 | * 20..22 are used for color-moved. |
| 858 | */ |
| 859 | #define DIFF_SYMBOL_CONTENT_BLANK_LINE_EOF (1<<19) |
| 860 | #define DIFF_SYMBOL_MOVED_LINE (1<<20) |
| 861 | #define DIFF_SYMBOL_MOVED_LINE_ALT (1<<21) |
| 862 | #define DIFF_SYMBOL_MOVED_LINE_UNINTERESTING (1<<22) |
| 863 | |
| 864 | #define DIFF_SYMBOL_CONTENT_WS_MASK (WSEH_NEW | WSEH_OLD | WSEH_CONTEXT | WS_RULE_MASK) |
| 865 | |
| 866 | /* |
| 867 | * This struct is used when we need to buffer the output of the diff output. |
| 868 | * |
| 869 | * NEEDSWORK: Instead of storing a copy of the line, add an offset pointer |
| 870 | * into the pre/post image file. This pointer could be a union with the |
| 871 | * line pointer. By storing an offset into the file instead of the literal line, |
| 872 | * we can decrease the memory footprint for the buffered output. At first we |
| 873 | * may want to only have indirection for the content lines, but we could also |
| 874 | * enhance the state for emitting prefabricated lines, e.g. the similarity |
| 875 | * score line or hunk/file headers would only need to store a number or path |
| 876 | * and then the output can be constructed later on depending on state. |
| 877 | */ |
| 878 | struct emitted_diff_symbol { |
| 879 | const char *line; |
| 880 | int len; |
| 881 | int flags; |
| 882 | int indent_off; /* Offset to first non-whitespace character */ |
| 883 | int indent_width; /* The visual width of the indentation */ |
| 884 | unsigned id; |
| 885 | enum diff_symbol s; |
| 886 | }; |
| 887 | #define EMITTED_DIFF_SYMBOL_INIT { 0 } |
| 888 | |
| 889 | struct emitted_diff_symbols { |
| 890 | struct emitted_diff_symbol *buf; |
| 891 | int nr, alloc; |
| 892 | }; |
| 893 | #define EMITTED_DIFF_SYMBOLS_INIT { 0 } |
| 894 | |
| 895 | static void append_emitted_diff_symbol(struct diff_options *o, |
| 896 | struct emitted_diff_symbol *e) |
| 897 | { |
| 898 | struct emitted_diff_symbol *f; |
| 899 | |
| 900 | ALLOC_GROW(o->emitted_symbols->buf, |
| 901 | o->emitted_symbols->nr + 1, |
| 902 | o->emitted_symbols->alloc); |
| 903 | f = &o->emitted_symbols->buf[o->emitted_symbols->nr++]; |
| 904 | |
| 905 | memcpy(f, e, sizeof(struct emitted_diff_symbol)); |
| 906 | f->line = e->line ? xmemdupz(e->line, e->len) : NULL; |
| 907 | } |
| 908 | |
| 909 | static void free_emitted_diff_symbols(struct emitted_diff_symbols *e) |
| 910 | { |
| 911 | if (!e) |
| 912 | return; |
| 913 | free(e->buf); |
| 914 | free(e); |
| 915 | } |
| 916 | |
| 917 | struct moved_entry { |
| 918 | const struct emitted_diff_symbol *es; |
| 919 | struct moved_entry *next_line; |
| 920 | struct moved_entry *next_match; |
| 921 | }; |
| 922 | |
| 923 | struct moved_block { |
| 924 | struct moved_entry *match; |
| 925 | int wsd; /* The whitespace delta of this block */ |
| 926 | }; |
| 927 | |
| 928 | #define INDENT_BLANKLINE INT_MIN |
| 929 | |
| 930 | static void fill_es_indent_data(struct emitted_diff_symbol *es) |
| 931 | { |
| 932 | unsigned int off = 0, i; |
| 933 | int width = 0, tab_width = es->flags & WS_TAB_WIDTH_MASK; |
| 934 | const char *s = es->line; |
| 935 | const int len = es->len; |
| 936 | |
| 937 | /* skip any \v \f \r at start of indentation */ |
| 938 | while (s[off] == '\f' || s[off] == '\v' || |
| 939 | (off < len - 1 && s[off] == '\r')) |
| 940 | off++; |
| 941 | |
| 942 | /* calculate the visual width of indentation */ |
| 943 | while(1) { |
| 944 | if (s[off] == ' ') { |
| 945 | width++; |
| 946 | off++; |
| 947 | } else if (s[off] == '\t') { |
| 948 | width += tab_width - (width % tab_width); |
| 949 | while (s[++off] == '\t') |
| 950 | width += tab_width; |
| 951 | } else { |
| 952 | break; |
| 953 | } |
| 954 | } |
| 955 | |
| 956 | /* check if this line is blank */ |
| 957 | for (i = off; i < len; i++) |
| 958 | if (!isspace(s[i])) |
| 959 | break; |
| 960 | |
| 961 | if (i == len) { |
| 962 | es->indent_width = INDENT_BLANKLINE; |
| 963 | es->indent_off = len; |
| 964 | } else { |
| 965 | es->indent_off = off; |
| 966 | es->indent_width = width; |
| 967 | } |
| 968 | } |
| 969 | |
| 970 | static int compute_ws_delta(const struct emitted_diff_symbol *a, |
| 971 | const struct emitted_diff_symbol *b) |
| 972 | { |
| 973 | int a_width = a->indent_width, |
| 974 | b_width = b->indent_width; |
| 975 | |
| 976 | if (a_width == INDENT_BLANKLINE && b_width == INDENT_BLANKLINE) |
| 977 | return INDENT_BLANKLINE; |
| 978 | |
| 979 | return a_width - b_width; |
| 980 | } |
| 981 | |
| 982 | static int cmp_in_block_with_wsd(const struct moved_entry *cur, |
| 983 | const struct emitted_diff_symbol *l, |
| 984 | struct moved_block *pmb) |
| 985 | { |
| 986 | int a_width = cur->es->indent_width, b_width = l->indent_width; |
| 987 | int delta; |
| 988 | |
| 989 | /* The text of each line must match */ |
| 990 | if (cur->es->id != l->id) |
| 991 | return 1; |
| 992 | |
| 993 | /* |
| 994 | * If 'l' and 'cur' are both blank then we don't need to check the |
| 995 | * indent. We only need to check cur as we know the strings match. |
| 996 | * */ |
| 997 | if (a_width == INDENT_BLANKLINE) |
| 998 | return 0; |
| 999 | |
| 1000 | /* |
| 1001 | * The indent changes of the block are known and stored in pmb->wsd; |
| 1002 | * however we need to check if the indent changes of the current line |
| 1003 | * match those of the current block. |
| 1004 | */ |
| 1005 | delta = b_width - a_width; |
| 1006 | |
| 1007 | /* |
| 1008 | * If the previous lines of this block were all blank then set its |
| 1009 | * whitespace delta. |
| 1010 | */ |
| 1011 | if (pmb->wsd == INDENT_BLANKLINE) |
| 1012 | pmb->wsd = delta; |
| 1013 | |
| 1014 | return delta != pmb->wsd; |
| 1015 | } |
| 1016 | |
| 1017 | struct interned_diff_symbol { |
| 1018 | struct hashmap_entry ent; |
| 1019 | struct emitted_diff_symbol *es; |
| 1020 | }; |
| 1021 | |
| 1022 | static int interned_diff_symbol_cmp(const void *hashmap_cmp_fn_data, |
| 1023 | const struct hashmap_entry *eptr, |
| 1024 | const struct hashmap_entry *entry_or_key, |
| 1025 | const void *keydata UNUSED) |
| 1026 | { |
| 1027 | const struct diff_options *diffopt = hashmap_cmp_fn_data; |
| 1028 | const struct emitted_diff_symbol *a, *b; |
| 1029 | unsigned flags = diffopt->color_moved_ws_handling |
| 1030 | & XDF_WHITESPACE_FLAGS; |
| 1031 | |
| 1032 | a = container_of(eptr, const struct interned_diff_symbol, ent)->es; |
| 1033 | b = container_of(entry_or_key, const struct interned_diff_symbol, ent)->es; |
| 1034 | |
| 1035 | return !xdiff_compare_lines(a->line + a->indent_off, |
| 1036 | a->len - a->indent_off, |
| 1037 | b->line + b->indent_off, |
| 1038 | b->len - b->indent_off, flags); |
| 1039 | } |
| 1040 | |
| 1041 | static void prepare_entry(struct diff_options *o, struct emitted_diff_symbol *l, |
| 1042 | struct interned_diff_symbol *s) |
| 1043 | { |
| 1044 | unsigned flags = o->color_moved_ws_handling & XDF_WHITESPACE_FLAGS; |
| 1045 | unsigned int hash = xdiff_hash_string(l->line + l->indent_off, |
| 1046 | l->len - l->indent_off, flags); |
| 1047 | |
| 1048 | hashmap_entry_init(&s->ent, hash); |
| 1049 | s->es = l; |
| 1050 | } |
| 1051 | |
| 1052 | struct moved_entry_list { |
| 1053 | struct moved_entry *add, *del; |
| 1054 | }; |
| 1055 | |
| 1056 | static struct moved_entry_list *add_lines_to_move_detection(struct diff_options *o, |
| 1057 | struct mem_pool *entry_mem_pool) |
| 1058 | { |
| 1059 | struct moved_entry *prev_line = NULL; |
| 1060 | struct mem_pool interned_pool; |
| 1061 | struct hashmap interned_map; |
| 1062 | struct moved_entry_list *entry_list = NULL; |
| 1063 | size_t entry_list_alloc = 0; |
| 1064 | unsigned id = 0; |
| 1065 | int n; |
| 1066 | |
| 1067 | hashmap_init(&interned_map, interned_diff_symbol_cmp, o, 8096); |
| 1068 | mem_pool_init(&interned_pool, 1024 * 1024); |
| 1069 | |
| 1070 | for (n = 0; n < o->emitted_symbols->nr; n++) { |
| 1071 | struct interned_diff_symbol key; |
| 1072 | struct emitted_diff_symbol *l = &o->emitted_symbols->buf[n]; |
| 1073 | struct interned_diff_symbol *s; |
| 1074 | struct moved_entry *entry; |
| 1075 | |
| 1076 | if (l->s != DIFF_SYMBOL_PLUS && l->s != DIFF_SYMBOL_MINUS) { |
| 1077 | prev_line = NULL; |
| 1078 | continue; |
| 1079 | } |
| 1080 | |
| 1081 | if (o->color_moved_ws_handling & |
| 1082 | COLOR_MOVED_WS_ALLOW_INDENTATION_CHANGE) |
| 1083 | fill_es_indent_data(l); |
| 1084 | |
| 1085 | prepare_entry(o, l, &key); |
| 1086 | s = hashmap_get_entry(&interned_map, &key, ent, &key.ent); |
| 1087 | if (s) { |
| 1088 | l->id = s->es->id; |
| 1089 | } else { |
| 1090 | l->id = id; |
| 1091 | ALLOC_GROW_BY(entry_list, id, 1, entry_list_alloc); |
| 1092 | hashmap_add(&interned_map, |
| 1093 | memcpy(mem_pool_alloc(&interned_pool, |
| 1094 | sizeof(key)), |
| 1095 | &key, sizeof(key))); |
| 1096 | } |
| 1097 | entry = mem_pool_alloc(entry_mem_pool, sizeof(*entry)); |
| 1098 | entry->es = l; |
| 1099 | entry->next_line = NULL; |
| 1100 | if (prev_line && prev_line->es->s == l->s) |
| 1101 | prev_line->next_line = entry; |
| 1102 | prev_line = entry; |
| 1103 | if (l->s == DIFF_SYMBOL_PLUS) { |
| 1104 | entry->next_match = entry_list[l->id].add; |
| 1105 | entry_list[l->id].add = entry; |
| 1106 | } else { |
| 1107 | entry->next_match = entry_list[l->id].del; |
| 1108 | entry_list[l->id].del = entry; |
| 1109 | } |
| 1110 | } |
| 1111 | |
| 1112 | hashmap_clear(&interned_map); |
| 1113 | mem_pool_discard(&interned_pool, 0); |
| 1114 | |
| 1115 | return entry_list; |
| 1116 | } |
| 1117 | |
| 1118 | static void pmb_advance_or_null(struct diff_options *o, |
| 1119 | struct emitted_diff_symbol *l, |
| 1120 | struct moved_block *pmb, |
| 1121 | int *pmb_nr) |
| 1122 | { |
| 1123 | int i, j; |
| 1124 | |
| 1125 | for (i = 0, j = 0; i < *pmb_nr; i++) { |
| 1126 | int match; |
| 1127 | struct moved_entry *prev = pmb[i].match; |
| 1128 | struct moved_entry *cur = (prev && prev->next_line) ? |
| 1129 | prev->next_line : NULL; |
| 1130 | |
| 1131 | if (o->color_moved_ws_handling & |
| 1132 | COLOR_MOVED_WS_ALLOW_INDENTATION_CHANGE) |
| 1133 | match = cur && |
| 1134 | !cmp_in_block_with_wsd(cur, l, &pmb[i]); |
| 1135 | else |
| 1136 | match = cur && cur->es->id == l->id; |
| 1137 | |
| 1138 | if (match) { |
| 1139 | pmb[j] = pmb[i]; |
| 1140 | pmb[j++].match = cur; |
| 1141 | } |
| 1142 | } |
| 1143 | *pmb_nr = j; |
| 1144 | } |
| 1145 | |
| 1146 | static void fill_potential_moved_blocks(struct diff_options *o, |
| 1147 | struct moved_entry *match, |
| 1148 | struct emitted_diff_symbol *l, |
| 1149 | struct moved_block **pmb_p, |
| 1150 | int *pmb_alloc_p, int *pmb_nr_p) |
| 1151 | |
| 1152 | { |
| 1153 | struct moved_block *pmb = *pmb_p; |
| 1154 | int pmb_alloc = *pmb_alloc_p, pmb_nr = *pmb_nr_p; |
| 1155 | |
| 1156 | /* |
| 1157 | * The current line is the start of a new block. |
| 1158 | * Setup the set of potential blocks. |
| 1159 | */ |
| 1160 | for (; match; match = match->next_match) { |
| 1161 | ALLOC_GROW(pmb, pmb_nr + 1, pmb_alloc); |
| 1162 | if (o->color_moved_ws_handling & |
| 1163 | COLOR_MOVED_WS_ALLOW_INDENTATION_CHANGE) |
| 1164 | pmb[pmb_nr].wsd = compute_ws_delta(l, match->es); |
| 1165 | else |
| 1166 | pmb[pmb_nr].wsd = 0; |
| 1167 | pmb[pmb_nr++].match = match; |
| 1168 | } |
| 1169 | |
| 1170 | *pmb_p = pmb; |
| 1171 | *pmb_alloc_p = pmb_alloc; |
| 1172 | *pmb_nr_p = pmb_nr; |
| 1173 | } |
| 1174 | |
| 1175 | /* |
| 1176 | * If o->color_moved is COLOR_MOVED_PLAIN, this function does nothing. |
| 1177 | * |
| 1178 | * Otherwise, if the last block has fewer alphanumeric characters than |
| 1179 | * COLOR_MOVED_MIN_ALNUM_COUNT, unset DIFF_SYMBOL_MOVED_LINE on all lines in |
| 1180 | * that block. |
| 1181 | * |
| 1182 | * The last block consists of the (n - block_length)'th line up to but not |
| 1183 | * including the nth line. |
| 1184 | * |
| 1185 | * Returns 0 if the last block is empty or is unset by this function, non zero |
| 1186 | * otherwise. |
| 1187 | * |
| 1188 | * NEEDSWORK: This uses the same heuristic as blame_entry_score() in blame.c. |
| 1189 | * Think of a way to unify them. |
| 1190 | */ |
| 1191 | #define DIFF_SYMBOL_MOVED_LINE_ZEBRA_MASK \ |
| 1192 | (DIFF_SYMBOL_MOVED_LINE | DIFF_SYMBOL_MOVED_LINE_ALT) |
| 1193 | static int adjust_last_block(struct diff_options *o, int n, int block_length) |
| 1194 | { |
| 1195 | int i, alnum_count = 0; |
| 1196 | if (o->color_moved == COLOR_MOVED_PLAIN) |
| 1197 | return block_length; |
| 1198 | for (i = 1; i < block_length + 1; i++) { |
| 1199 | const char *c = o->emitted_symbols->buf[n - i].line; |
| 1200 | for (; *c; c++) { |
| 1201 | if (!isalnum(*c)) |
| 1202 | continue; |
| 1203 | alnum_count++; |
| 1204 | if (alnum_count >= COLOR_MOVED_MIN_ALNUM_COUNT) |
| 1205 | return 1; |
| 1206 | } |
| 1207 | } |
| 1208 | for (i = 1; i < block_length + 1; i++) |
| 1209 | o->emitted_symbols->buf[n - i].flags &= ~DIFF_SYMBOL_MOVED_LINE_ZEBRA_MASK; |
| 1210 | return 0; |
| 1211 | } |
| 1212 | |
| 1213 | /* Find blocks of moved code, delegate actual coloring decision to helper */ |
| 1214 | static void mark_color_as_moved(struct diff_options *o, |
| 1215 | struct moved_entry_list *entry_list) |
| 1216 | { |
| 1217 | struct moved_block *pmb = NULL; /* potentially moved blocks */ |
| 1218 | int pmb_nr = 0, pmb_alloc = 0; |
| 1219 | int n, flipped_block = 0, block_length = 0; |
| 1220 | enum diff_symbol moved_symbol = DIFF_SYMBOL_BINARY_DIFF_HEADER; |
| 1221 | |
| 1222 | |
| 1223 | for (n = 0; n < o->emitted_symbols->nr; n++) { |
| 1224 | struct moved_entry *match = NULL; |
| 1225 | struct emitted_diff_symbol *l = &o->emitted_symbols->buf[n]; |
| 1226 | |
| 1227 | switch (l->s) { |
| 1228 | case DIFF_SYMBOL_PLUS: |
| 1229 | match = entry_list[l->id].del; |
| 1230 | break; |
| 1231 | case DIFF_SYMBOL_MINUS: |
| 1232 | match = entry_list[l->id].add; |
| 1233 | break; |
| 1234 | default: |
| 1235 | flipped_block = 0; |
| 1236 | } |
| 1237 | |
| 1238 | if (pmb_nr && (!match || l->s != moved_symbol)) { |
| 1239 | if (!adjust_last_block(o, n, block_length) && |
| 1240 | block_length > 1) { |
| 1241 | /* |
| 1242 | * Rewind in case there is another match |
| 1243 | * starting at the second line of the block |
| 1244 | */ |
| 1245 | match = NULL; |
| 1246 | n -= block_length; |
| 1247 | } |
| 1248 | pmb_nr = 0; |
| 1249 | block_length = 0; |
| 1250 | flipped_block = 0; |
| 1251 | } |
| 1252 | if (!match) { |
| 1253 | moved_symbol = DIFF_SYMBOL_BINARY_DIFF_HEADER; |
| 1254 | continue; |
| 1255 | } |
| 1256 | |
| 1257 | if (o->color_moved == COLOR_MOVED_PLAIN) { |
| 1258 | l->flags |= DIFF_SYMBOL_MOVED_LINE; |
| 1259 | continue; |
| 1260 | } |
| 1261 | |
| 1262 | pmb_advance_or_null(o, l, pmb, &pmb_nr); |
| 1263 | |
| 1264 | if (pmb_nr == 0) { |
| 1265 | int contiguous = adjust_last_block(o, n, block_length); |
| 1266 | |
| 1267 | if (!contiguous && block_length > 1) |
| 1268 | /* |
| 1269 | * Rewind in case there is another match |
| 1270 | * starting at the second line of the block |
| 1271 | */ |
| 1272 | n -= block_length; |
| 1273 | else |
| 1274 | fill_potential_moved_blocks(o, match, l, |
| 1275 | &pmb, &pmb_alloc, |
| 1276 | &pmb_nr); |
| 1277 | |
| 1278 | if (contiguous && pmb_nr && moved_symbol == l->s) |
| 1279 | flipped_block = (flipped_block + 1) % 2; |
| 1280 | else |
| 1281 | flipped_block = 0; |
| 1282 | |
| 1283 | if (pmb_nr) |
| 1284 | moved_symbol = l->s; |
| 1285 | else |
| 1286 | moved_symbol = DIFF_SYMBOL_BINARY_DIFF_HEADER; |
| 1287 | |
| 1288 | block_length = 0; |
| 1289 | } |
| 1290 | |
| 1291 | if (pmb_nr) { |
| 1292 | block_length++; |
| 1293 | l->flags |= DIFF_SYMBOL_MOVED_LINE; |
| 1294 | if (flipped_block && o->color_moved != COLOR_MOVED_BLOCKS) |
| 1295 | l->flags |= DIFF_SYMBOL_MOVED_LINE_ALT; |
| 1296 | } |
| 1297 | } |
| 1298 | adjust_last_block(o, n, block_length); |
| 1299 | |
| 1300 | free(pmb); |
| 1301 | } |
| 1302 | |
| 1303 | static void dim_moved_lines(struct diff_options *o) |
| 1304 | { |
| 1305 | int n; |
| 1306 | for (n = 0; n < o->emitted_symbols->nr; n++) { |
| 1307 | struct emitted_diff_symbol *prev = (n != 0) ? |
| 1308 | &o->emitted_symbols->buf[n - 1] : NULL; |
| 1309 | struct emitted_diff_symbol *l = &o->emitted_symbols->buf[n]; |
| 1310 | struct emitted_diff_symbol *next = |
| 1311 | (n < o->emitted_symbols->nr - 1) ? |
| 1312 | &o->emitted_symbols->buf[n + 1] : NULL; |
| 1313 | |
| 1314 | /* Not a plus or minus line? */ |
| 1315 | if (l->s != DIFF_SYMBOL_PLUS && l->s != DIFF_SYMBOL_MINUS) |
| 1316 | continue; |
| 1317 | |
| 1318 | /* Not a moved line? */ |
| 1319 | if (!(l->flags & DIFF_SYMBOL_MOVED_LINE)) |
| 1320 | continue; |
| 1321 | |
| 1322 | /* |
| 1323 | * If prev or next are not a plus or minus line, |
| 1324 | * pretend they don't exist |
| 1325 | */ |
| 1326 | if (prev && prev->s != DIFF_SYMBOL_PLUS && |
| 1327 | prev->s != DIFF_SYMBOL_MINUS) |
| 1328 | prev = NULL; |
| 1329 | if (next && next->s != DIFF_SYMBOL_PLUS && |
| 1330 | next->s != DIFF_SYMBOL_MINUS) |
| 1331 | next = NULL; |
| 1332 | |
| 1333 | /* Inside a block? */ |
| 1334 | if ((prev && |
| 1335 | (prev->flags & DIFF_SYMBOL_MOVED_LINE_ZEBRA_MASK) == |
| 1336 | (l->flags & DIFF_SYMBOL_MOVED_LINE_ZEBRA_MASK)) && |
| 1337 | (next && |
| 1338 | (next->flags & DIFF_SYMBOL_MOVED_LINE_ZEBRA_MASK) == |
| 1339 | (l->flags & DIFF_SYMBOL_MOVED_LINE_ZEBRA_MASK))) { |
| 1340 | l->flags |= DIFF_SYMBOL_MOVED_LINE_UNINTERESTING; |
| 1341 | continue; |
| 1342 | } |
| 1343 | |
| 1344 | /* Check if we are at an interesting bound: */ |
| 1345 | if (prev && (prev->flags & DIFF_SYMBOL_MOVED_LINE) && |
| 1346 | (prev->flags & DIFF_SYMBOL_MOVED_LINE_ALT) != |
| 1347 | (l->flags & DIFF_SYMBOL_MOVED_LINE_ALT)) |
| 1348 | continue; |
| 1349 | if (next && (next->flags & DIFF_SYMBOL_MOVED_LINE) && |
| 1350 | (next->flags & DIFF_SYMBOL_MOVED_LINE_ALT) != |
| 1351 | (l->flags & DIFF_SYMBOL_MOVED_LINE_ALT)) |
| 1352 | continue; |
| 1353 | |
| 1354 | /* |
| 1355 | * The boundary to prev and next are not interesting, |
| 1356 | * so this line is not interesting as a whole |
| 1357 | */ |
| 1358 | l->flags |= DIFF_SYMBOL_MOVED_LINE_UNINTERESTING; |
| 1359 | } |
| 1360 | } |
| 1361 | |
| 1362 | static void emit_line_ws_markup(struct diff_options *o, |
| 1363 | const char *set_sign, const char *set, |
| 1364 | const char *reset, |
| 1365 | int sign_index, const char *line, int len, |
| 1366 | unsigned ws_rule, int blank_at_eof) |
| 1367 | { |
| 1368 | const char *ws = NULL; |
| 1369 | int sign = o->output_indicators[sign_index]; |
| 1370 | |
| 1371 | if (diff_suppress_blank_empty && |
| 1372 | sign_index == OUTPUT_INDICATOR_CONTEXT && |
| 1373 | len == 1 && line[0] == '\n') |
| 1374 | sign = 0; |
| 1375 | |
| 1376 | if (o->ws_error_highlight & ws_rule) { |
| 1377 | ws = diff_get_color_opt(o, DIFF_WHITESPACE); |
| 1378 | if (!*ws) |
| 1379 | ws = NULL; |
| 1380 | } |
| 1381 | |
| 1382 | if (!ws && !set_sign) { |
| 1383 | emit_line_0(o, set, NULL, 0, reset, sign, line, len); |
| 1384 | } else if (!ws) { |
| 1385 | emit_line_0(o, set_sign, set, !!set_sign, reset, sign, line, len); |
| 1386 | } else if (blank_at_eof) { |
| 1387 | /* Blank line at EOF - paint '+' as well */ |
| 1388 | emit_line_0(o, ws, NULL, 0, reset, sign, line, len); |
| 1389 | } else { |
| 1390 | /* Emit just the prefix, then the rest. */ |
| 1391 | emit_line_0(o, set_sign ? set_sign : set, NULL, !!set_sign, reset, |
| 1392 | sign, "", 0); |
| 1393 | ws_check_emit(line, len, ws_rule, |
| 1394 | o->file, set, reset, ws); |
| 1395 | } |
| 1396 | } |
| 1397 | |
| 1398 | static void emit_diff_symbol_from_struct(struct diff_options *o, |
| 1399 | struct emitted_diff_symbol *eds) |
| 1400 | { |
| 1401 | const char *context, *reset, *set, *set_sign, *meta, *fraginfo; |
| 1402 | |
| 1403 | enum diff_symbol s = eds->s; |
| 1404 | const char *line = eds->line; |
| 1405 | int len = eds->len; |
| 1406 | unsigned flags = eds->flags; |
| 1407 | |
| 1408 | if (!o->file) |
| 1409 | return; |
| 1410 | |
| 1411 | switch (s) { |
| 1412 | case DIFF_SYMBOL_SUBMODULE_HEADER: |
| 1413 | case DIFF_SYMBOL_SUBMODULE_ERROR: |
| 1414 | case DIFF_SYMBOL_SUBMODULE_PIPETHROUGH: |
| 1415 | case DIFF_SYMBOL_STATS_SUMMARY_INSERTS_DELETES: |
| 1416 | case DIFF_SYMBOL_SUMMARY: |
| 1417 | case DIFF_SYMBOL_STATS_LINE: |
| 1418 | case DIFF_SYMBOL_BINARY_DIFF_BODY: |
| 1419 | case DIFF_SYMBOL_CONTEXT_FRAGINFO: |
| 1420 | emit_line(o, "", "", line, len); |
| 1421 | break; |
| 1422 | case DIFF_SYMBOL_CONTEXT_INCOMPLETE: |
| 1423 | if ((flags & WS_INCOMPLETE_LINE) && |
| 1424 | (flags & o->ws_error_highlight)) |
| 1425 | set = diff_get_color_opt(o, DIFF_WHITESPACE); |
| 1426 | else |
| 1427 | set = diff_get_color_opt(o, DIFF_CONTEXT); |
| 1428 | reset = diff_get_color_opt(o, DIFF_RESET); |
| 1429 | emit_line(o, set, reset, line, len); |
| 1430 | break; |
| 1431 | case DIFF_SYMBOL_CONTEXT_MARKER: |
| 1432 | context = diff_get_color_opt(o, DIFF_CONTEXT); |
| 1433 | reset = diff_get_color_opt(o, DIFF_RESET); |
| 1434 | emit_line(o, context, reset, line, len); |
| 1435 | break; |
| 1436 | case DIFF_SYMBOL_SEPARATOR: |
| 1437 | fprintf(o->file, "%s%c", |
| 1438 | diff_line_prefix(o), |
| 1439 | o->line_termination); |
| 1440 | break; |
| 1441 | case DIFF_SYMBOL_CONTEXT: |
| 1442 | set = diff_get_color_opt(o, DIFF_CONTEXT); |
| 1443 | reset = diff_get_color_opt(o, DIFF_RESET); |
| 1444 | set_sign = NULL; |
| 1445 | if (o->flags.dual_color_diffed_diffs) { |
| 1446 | char c = !len ? 0 : line[0]; |
| 1447 | |
| 1448 | if (c == '+') |
| 1449 | set = diff_get_color_opt(o, DIFF_FILE_NEW); |
| 1450 | else if (c == '@') |
| 1451 | set = diff_get_color_opt(o, DIFF_FRAGINFO); |
| 1452 | else if (c == '-') |
| 1453 | set = diff_get_color_opt(o, DIFF_FILE_OLD); |
| 1454 | } |
| 1455 | emit_line_ws_markup(o, set_sign, set, reset, |
| 1456 | OUTPUT_INDICATOR_CONTEXT, line, len, |
| 1457 | flags & (DIFF_SYMBOL_CONTENT_WS_MASK), 0); |
| 1458 | break; |
| 1459 | case DIFF_SYMBOL_PLUS: |
| 1460 | switch (flags & (DIFF_SYMBOL_MOVED_LINE | |
| 1461 | DIFF_SYMBOL_MOVED_LINE_ALT | |
| 1462 | DIFF_SYMBOL_MOVED_LINE_UNINTERESTING)) { |
| 1463 | case DIFF_SYMBOL_MOVED_LINE | |
| 1464 | DIFF_SYMBOL_MOVED_LINE_ALT | |
| 1465 | DIFF_SYMBOL_MOVED_LINE_UNINTERESTING: |
| 1466 | set = diff_get_color_opt(o, DIFF_FILE_NEW_MOVED_ALT_DIM); |
| 1467 | break; |
| 1468 | case DIFF_SYMBOL_MOVED_LINE | |
| 1469 | DIFF_SYMBOL_MOVED_LINE_ALT: |
| 1470 | set = diff_get_color_opt(o, DIFF_FILE_NEW_MOVED_ALT); |
| 1471 | break; |
| 1472 | case DIFF_SYMBOL_MOVED_LINE | |
| 1473 | DIFF_SYMBOL_MOVED_LINE_UNINTERESTING: |
| 1474 | set = diff_get_color_opt(o, DIFF_FILE_NEW_MOVED_DIM); |
| 1475 | break; |
| 1476 | case DIFF_SYMBOL_MOVED_LINE: |
| 1477 | set = diff_get_color_opt(o, DIFF_FILE_NEW_MOVED); |
| 1478 | break; |
| 1479 | default: |
| 1480 | set = diff_get_color_opt(o, DIFF_FILE_NEW); |
| 1481 | } |
| 1482 | reset = diff_get_color_opt(o, DIFF_RESET); |
| 1483 | if (!o->flags.dual_color_diffed_diffs) |
| 1484 | set_sign = NULL; |
| 1485 | else { |
| 1486 | char c = !len ? 0 : line[0]; |
| 1487 | |
| 1488 | set_sign = set; |
| 1489 | if (c == '-') |
| 1490 | set = diff_get_color_opt(o, DIFF_FILE_OLD_BOLD); |
| 1491 | else if (c == '@') |
| 1492 | set = diff_get_color_opt(o, DIFF_FRAGINFO); |
| 1493 | else if (c == '+') |
| 1494 | set = diff_get_color_opt(o, DIFF_FILE_NEW_BOLD); |
| 1495 | else |
| 1496 | set = diff_get_color_opt(o, DIFF_CONTEXT_BOLD); |
| 1497 | flags &= ~DIFF_SYMBOL_CONTENT_WS_MASK; |
| 1498 | } |
| 1499 | emit_line_ws_markup(o, set_sign, set, reset, |
| 1500 | OUTPUT_INDICATOR_NEW, line, len, |
| 1501 | flags & DIFF_SYMBOL_CONTENT_WS_MASK, |
| 1502 | flags & DIFF_SYMBOL_CONTENT_BLANK_LINE_EOF); |
| 1503 | break; |
| 1504 | case DIFF_SYMBOL_MINUS: |
| 1505 | switch (flags & (DIFF_SYMBOL_MOVED_LINE | |
| 1506 | DIFF_SYMBOL_MOVED_LINE_ALT | |
| 1507 | DIFF_SYMBOL_MOVED_LINE_UNINTERESTING)) { |
| 1508 | case DIFF_SYMBOL_MOVED_LINE | |
| 1509 | DIFF_SYMBOL_MOVED_LINE_ALT | |
| 1510 | DIFF_SYMBOL_MOVED_LINE_UNINTERESTING: |
| 1511 | set = diff_get_color_opt(o, DIFF_FILE_OLD_MOVED_ALT_DIM); |
| 1512 | break; |
| 1513 | case DIFF_SYMBOL_MOVED_LINE | |
| 1514 | DIFF_SYMBOL_MOVED_LINE_ALT: |
| 1515 | set = diff_get_color_opt(o, DIFF_FILE_OLD_MOVED_ALT); |
| 1516 | break; |
| 1517 | case DIFF_SYMBOL_MOVED_LINE | |
| 1518 | DIFF_SYMBOL_MOVED_LINE_UNINTERESTING: |
| 1519 | set = diff_get_color_opt(o, DIFF_FILE_OLD_MOVED_DIM); |
| 1520 | break; |
| 1521 | case DIFF_SYMBOL_MOVED_LINE: |
| 1522 | set = diff_get_color_opt(o, DIFF_FILE_OLD_MOVED); |
| 1523 | break; |
| 1524 | default: |
| 1525 | set = diff_get_color_opt(o, DIFF_FILE_OLD); |
| 1526 | } |
| 1527 | reset = diff_get_color_opt(o, DIFF_RESET); |
| 1528 | if (!o->flags.dual_color_diffed_diffs) |
| 1529 | set_sign = NULL; |
| 1530 | else { |
| 1531 | char c = !len ? 0 : line[0]; |
| 1532 | |
| 1533 | set_sign = set; |
| 1534 | if (c == '+') |
| 1535 | set = diff_get_color_opt(o, DIFF_FILE_NEW_DIM); |
| 1536 | else if (c == '@') |
| 1537 | set = diff_get_color_opt(o, DIFF_FRAGINFO); |
| 1538 | else if (c == '-') |
| 1539 | set = diff_get_color_opt(o, DIFF_FILE_OLD_DIM); |
| 1540 | else |
| 1541 | set = diff_get_color_opt(o, DIFF_CONTEXT_DIM); |
| 1542 | } |
| 1543 | emit_line_ws_markup(o, set_sign, set, reset, |
| 1544 | OUTPUT_INDICATOR_OLD, line, len, |
| 1545 | flags & DIFF_SYMBOL_CONTENT_WS_MASK, 0); |
| 1546 | break; |
| 1547 | case DIFF_SYMBOL_WORDS_PORCELAIN: |
| 1548 | context = diff_get_color_opt(o, DIFF_CONTEXT); |
| 1549 | reset = diff_get_color_opt(o, DIFF_RESET); |
| 1550 | emit_line(o, context, reset, line, len); |
| 1551 | fputs("~\n", o->file); |
| 1552 | break; |
| 1553 | case DIFF_SYMBOL_WORDS: |
| 1554 | context = diff_get_color_opt(o, DIFF_CONTEXT); |
| 1555 | reset = diff_get_color_opt(o, DIFF_RESET); |
| 1556 | |
| 1557 | /* Skip the prefix character */ |
| 1558 | line++; len--; |
| 1559 | emit_line(o, context, reset, line, len); |
| 1560 | break; |
| 1561 | case DIFF_SYMBOL_FILEPAIR_PLUS: |
| 1562 | meta = diff_get_color_opt(o, DIFF_METAINFO); |
| 1563 | reset = diff_get_color_opt(o, DIFF_RESET); |
| 1564 | fprintf(o->file, "%s%s+++ %s%s%s\n", diff_line_prefix(o), meta, |
| 1565 | line, reset, |
| 1566 | strchr(line, ' ') ? "\t" : ""); |
| 1567 | break; |
| 1568 | case DIFF_SYMBOL_FILEPAIR_MINUS: |
| 1569 | meta = diff_get_color_opt(o, DIFF_METAINFO); |
| 1570 | reset = diff_get_color_opt(o, DIFF_RESET); |
| 1571 | fprintf(o->file, "%s%s--- %s%s%s\n", diff_line_prefix(o), meta, |
| 1572 | line, reset, |
| 1573 | strchr(line, ' ') ? "\t" : ""); |
| 1574 | break; |
| 1575 | case DIFF_SYMBOL_BINARY_FILES: |
| 1576 | case DIFF_SYMBOL_HEADER: |
| 1577 | fprintf(o->file, "%s", line); |
| 1578 | break; |
| 1579 | case DIFF_SYMBOL_BINARY_DIFF_HEADER: |
| 1580 | fprintf(o->file, "%sGIT binary patch\n", diff_line_prefix(o)); |
| 1581 | break; |
| 1582 | case DIFF_SYMBOL_BINARY_DIFF_HEADER_DELTA: |
| 1583 | fprintf(o->file, "%sdelta %s\n", diff_line_prefix(o), line); |
| 1584 | break; |
| 1585 | case DIFF_SYMBOL_BINARY_DIFF_HEADER_LITERAL: |
| 1586 | fprintf(o->file, "%sliteral %s\n", diff_line_prefix(o), line); |
| 1587 | break; |
| 1588 | case DIFF_SYMBOL_BINARY_DIFF_FOOTER: |
| 1589 | fputs(diff_line_prefix(o), o->file); |
| 1590 | fputc('\n', o->file); |
| 1591 | break; |
| 1592 | case DIFF_SYMBOL_REWRITE_DIFF: |
| 1593 | fraginfo = diff_get_color(o->use_color, DIFF_FRAGINFO); |
| 1594 | reset = diff_get_color_opt(o, DIFF_RESET); |
| 1595 | emit_line(o, fraginfo, reset, line, len); |
| 1596 | break; |
| 1597 | case DIFF_SYMBOL_SUBMODULE_ADD: |
| 1598 | set = diff_get_color_opt(o, DIFF_FILE_NEW); |
| 1599 | reset = diff_get_color_opt(o, DIFF_RESET); |
| 1600 | emit_line(o, set, reset, line, len); |
| 1601 | break; |
| 1602 | case DIFF_SYMBOL_SUBMODULE_DEL: |
| 1603 | set = diff_get_color_opt(o, DIFF_FILE_OLD); |
| 1604 | reset = diff_get_color_opt(o, DIFF_RESET); |
| 1605 | emit_line(o, set, reset, line, len); |
| 1606 | break; |
| 1607 | case DIFF_SYMBOL_SUBMODULE_UNTRACKED: |
| 1608 | fprintf(o->file, "%sSubmodule %s contains untracked content\n", |
| 1609 | diff_line_prefix(o), line); |
| 1610 | break; |
| 1611 | case DIFF_SYMBOL_SUBMODULE_MODIFIED: |
| 1612 | fprintf(o->file, "%sSubmodule %s contains modified content\n", |
| 1613 | diff_line_prefix(o), line); |
| 1614 | break; |
| 1615 | case DIFF_SYMBOL_STATS_SUMMARY_NO_FILES: |
| 1616 | emit_line(o, "", "", " 0 files changed\n", |
| 1617 | strlen(" 0 files changed\n")); |
| 1618 | break; |
| 1619 | case DIFF_SYMBOL_STATS_SUMMARY_ABBREV: |
| 1620 | emit_line(o, "", "", " ...\n", strlen(" ...\n")); |
| 1621 | break; |
| 1622 | case DIFF_SYMBOL_WORD_DIFF: |
| 1623 | fprintf(o->file, "%.*s", len, line); |
| 1624 | break; |
| 1625 | case DIFF_SYMBOL_STAT_SEP: |
| 1626 | fputs(o->stat_sep, o->file); |
| 1627 | break; |
| 1628 | default: |
| 1629 | BUG("unknown diff symbol"); |
| 1630 | } |
| 1631 | } |
| 1632 | |
| 1633 | static void emit_diff_symbol(struct diff_options *o, enum diff_symbol s, |
| 1634 | const char *line, int len, unsigned flags) |
| 1635 | { |
| 1636 | struct emitted_diff_symbol e = { |
| 1637 | .line = line, .len = len, .flags = flags, .s = s |
| 1638 | }; |
| 1639 | |
| 1640 | if (o->emitted_symbols) |
| 1641 | append_emitted_diff_symbol(o, &e); |
| 1642 | else |
| 1643 | emit_diff_symbol_from_struct(o, &e); |
| 1644 | } |
| 1645 | |
| 1646 | void diff_emit_submodule_del(struct diff_options *o, const char *line) |
| 1647 | { |
| 1648 | emit_diff_symbol(o, DIFF_SYMBOL_SUBMODULE_DEL, line, strlen(line), 0); |
| 1649 | } |
| 1650 | |
| 1651 | void diff_emit_submodule_add(struct diff_options *o, const char *line) |
| 1652 | { |
| 1653 | emit_diff_symbol(o, DIFF_SYMBOL_SUBMODULE_ADD, line, strlen(line), 0); |
| 1654 | } |
| 1655 | |
| 1656 | void diff_emit_submodule_untracked(struct diff_options *o, const char *path) |
| 1657 | { |
| 1658 | emit_diff_symbol(o, DIFF_SYMBOL_SUBMODULE_UNTRACKED, |
| 1659 | path, strlen(path), 0); |
| 1660 | } |
| 1661 | |
| 1662 | void diff_emit_submodule_modified(struct diff_options *o, const char *path) |
| 1663 | { |
| 1664 | emit_diff_symbol(o, DIFF_SYMBOL_SUBMODULE_MODIFIED, |
| 1665 | path, strlen(path), 0); |
| 1666 | } |
| 1667 | |
| 1668 | void diff_emit_submodule_header(struct diff_options *o, const char *header) |
| 1669 | { |
| 1670 | emit_diff_symbol(o, DIFF_SYMBOL_SUBMODULE_HEADER, |
| 1671 | header, strlen(header), 0); |
| 1672 | } |
| 1673 | |
| 1674 | void diff_emit_submodule_error(struct diff_options *o, const char *err) |
| 1675 | { |
| 1676 | emit_diff_symbol(o, DIFF_SYMBOL_SUBMODULE_ERROR, err, strlen(err), 0); |
| 1677 | } |
| 1678 | |
| 1679 | void diff_emit_submodule_pipethrough(struct diff_options *o, |
| 1680 | const char *line, int len) |
| 1681 | { |
| 1682 | emit_diff_symbol(o, DIFF_SYMBOL_SUBMODULE_PIPETHROUGH, line, len, 0); |
| 1683 | } |
| 1684 | |
| 1685 | static int new_blank_line_at_eof(struct emit_callback *ecbdata, const char *line, int len) |
| 1686 | { |
| 1687 | if (!((ecbdata->ws_rule & WS_BLANK_AT_EOF) && |
| 1688 | ecbdata->blank_at_eof_in_preimage && |
| 1689 | ecbdata->blank_at_eof_in_postimage && |
| 1690 | ecbdata->blank_at_eof_in_preimage <= ecbdata->lno_in_preimage && |
| 1691 | ecbdata->blank_at_eof_in_postimage <= ecbdata->lno_in_postimage)) |
| 1692 | return 0; |
| 1693 | return ws_blank_line(line, len); |
| 1694 | } |
| 1695 | |
| 1696 | static void emit_add_line(struct emit_callback *ecbdata, |
| 1697 | const char *line, int len) |
| 1698 | { |
| 1699 | unsigned flags = WSEH_NEW | ecbdata->ws_rule; |
| 1700 | if (new_blank_line_at_eof(ecbdata, line, len)) |
| 1701 | flags |= DIFF_SYMBOL_CONTENT_BLANK_LINE_EOF; |
| 1702 | |
| 1703 | emit_diff_symbol(ecbdata->opt, DIFF_SYMBOL_PLUS, line, len, flags); |
| 1704 | } |
| 1705 | |
| 1706 | static void emit_del_line(struct emit_callback *ecbdata, |
| 1707 | const char *line, int len) |
| 1708 | { |
| 1709 | unsigned flags = WSEH_OLD | ecbdata->ws_rule; |
| 1710 | emit_diff_symbol(ecbdata->opt, DIFF_SYMBOL_MINUS, line, len, flags); |
| 1711 | } |
| 1712 | |
| 1713 | static void emit_context_line(struct emit_callback *ecbdata, |
| 1714 | const char *line, int len) |
| 1715 | { |
| 1716 | unsigned flags = WSEH_CONTEXT | ecbdata->ws_rule; |
| 1717 | emit_diff_symbol(ecbdata->opt, DIFF_SYMBOL_CONTEXT, line, len, flags); |
| 1718 | } |
| 1719 | |
| 1720 | static void emit_incomplete_line_marker(struct emit_callback *ecbdata, |
| 1721 | const char *line, int len) |
| 1722 | { |
| 1723 | int last_line_kind = ecbdata->last_line_kind; |
| 1724 | unsigned flags = (last_line_kind == '+' |
| 1725 | ? WSEH_NEW |
| 1726 | : last_line_kind == '-' |
| 1727 | ? WSEH_OLD |
| 1728 | : WSEH_CONTEXT) | ecbdata->ws_rule; |
| 1729 | emit_diff_symbol(ecbdata->opt, DIFF_SYMBOL_CONTEXT_INCOMPLETE, |
| 1730 | line, len, flags); |
| 1731 | } |
| 1732 | |
| 1733 | static void emit_hunk_header(struct emit_callback *ecbdata, |
| 1734 | const char *line, int len) |
| 1735 | { |
| 1736 | const char *context = diff_get_color(ecbdata->color_diff, DIFF_CONTEXT); |
| 1737 | const char *frag = diff_get_color(ecbdata->color_diff, DIFF_FRAGINFO); |
| 1738 | const char *func = diff_get_color(ecbdata->color_diff, DIFF_FUNCINFO); |
| 1739 | const char *reset = diff_get_color(ecbdata->color_diff, DIFF_RESET); |
| 1740 | const char *reverse = want_color(ecbdata->color_diff) ? GIT_COLOR_REVERSE : ""; |
| 1741 | static const char atat[2] = { '@', '@' }; |
| 1742 | const char *cp, *ep; |
| 1743 | struct strbuf msgbuf = STRBUF_INIT; |
| 1744 | int org_len = len; |
| 1745 | int i = 1; |
| 1746 | |
| 1747 | /* |
| 1748 | * As a hunk header must begin with "@@ -<old>, +<new> @@", |
| 1749 | * it always is at least 10 bytes long. |
| 1750 | */ |
| 1751 | if (len < 10 || |
| 1752 | memcmp(line, atat, 2) || |
| 1753 | !(ep = memmem(line + 2, len - 2, atat, 2))) { |
| 1754 | emit_diff_symbol(ecbdata->opt, |
| 1755 | DIFF_SYMBOL_CONTEXT_MARKER, line, len, 0); |
| 1756 | return; |
| 1757 | } |
| 1758 | ep += 2; /* skip over @@ */ |
| 1759 | |
| 1760 | /* The hunk header in fraginfo color */ |
| 1761 | if (ecbdata->opt->flags.dual_color_diffed_diffs) |
| 1762 | strbuf_addstr(&msgbuf, reverse); |
| 1763 | strbuf_addstr(&msgbuf, frag); |
| 1764 | if (ecbdata->opt->flags.suppress_hunk_header_line_count) |
| 1765 | strbuf_add(&msgbuf, atat, sizeof(atat)); |
| 1766 | else |
| 1767 | strbuf_add(&msgbuf, line, ep - line); |
| 1768 | strbuf_addstr(&msgbuf, reset); |
| 1769 | |
| 1770 | /* |
| 1771 | * trailing "\r\n" |
| 1772 | */ |
| 1773 | for ( ; i < 3; i++) |
| 1774 | if (line[len - i] == '\r' || line[len - i] == '\n') |
| 1775 | len--; |
| 1776 | |
| 1777 | /* blank before the func header */ |
| 1778 | for (cp = ep; ep - line < len; ep++) |
| 1779 | if (*ep != ' ' && *ep != '\t') |
| 1780 | break; |
| 1781 | if (ep != cp) { |
| 1782 | strbuf_addstr(&msgbuf, context); |
| 1783 | strbuf_add(&msgbuf, cp, ep - cp); |
| 1784 | strbuf_addstr(&msgbuf, reset); |
| 1785 | } |
| 1786 | |
| 1787 | if (ep < line + len) { |
| 1788 | strbuf_addstr(&msgbuf, func); |
| 1789 | strbuf_add(&msgbuf, ep, line + len - ep); |
| 1790 | strbuf_addstr(&msgbuf, reset); |
| 1791 | } |
| 1792 | |
| 1793 | strbuf_add(&msgbuf, line + len, org_len - len); |
| 1794 | strbuf_complete_line(&msgbuf); |
| 1795 | emit_diff_symbol(ecbdata->opt, |
| 1796 | DIFF_SYMBOL_CONTEXT_FRAGINFO, msgbuf.buf, msgbuf.len, 0); |
| 1797 | strbuf_release(&msgbuf); |
| 1798 | } |
| 1799 | |
| 1800 | static struct diff_tempfile *claim_diff_tempfile(void) |
| 1801 | { |
| 1802 | int i; |
| 1803 | for (i = 0; i < ARRAY_SIZE(diff_temp); i++) |
| 1804 | if (!diff_temp[i].name) |
| 1805 | return diff_temp + i; |
| 1806 | BUG("diff is failing to clean up its tempfiles"); |
| 1807 | } |
| 1808 | |
| 1809 | static void remove_tempfile(void) |
| 1810 | { |
| 1811 | int i; |
| 1812 | for (i = 0; i < ARRAY_SIZE(diff_temp); i++) { |
| 1813 | if (is_tempfile_active(diff_temp[i].tempfile)) |
| 1814 | delete_tempfile(&diff_temp[i].tempfile); |
| 1815 | diff_temp[i].name = NULL; |
| 1816 | } |
| 1817 | } |
| 1818 | |
| 1819 | static void add_line_count(struct strbuf *out, int count) |
| 1820 | { |
| 1821 | switch (count) { |
| 1822 | case 0: |
| 1823 | strbuf_addstr(out, "0,0"); |
| 1824 | break; |
| 1825 | case 1: |
| 1826 | strbuf_addstr(out, "1"); |
| 1827 | break; |
| 1828 | default: |
| 1829 | strbuf_addf(out, "1,%d", count); |
| 1830 | break; |
| 1831 | } |
| 1832 | } |
| 1833 | |
| 1834 | static void emit_rewrite_lines(struct emit_callback *ecbdata, |
| 1835 | int prefix, const char *data, int size) |
| 1836 | { |
| 1837 | const char *endp = NULL; |
| 1838 | |
| 1839 | while (0 < size) { |
| 1840 | int len, plen; |
| 1841 | char *pdata = NULL; |
| 1842 | |
| 1843 | endp = memchr(data, '\n', size); |
| 1844 | |
| 1845 | if (endp) { |
| 1846 | len = endp - data + 1; |
| 1847 | plen = len; |
| 1848 | } else { |
| 1849 | len = size; |
| 1850 | plen = len + 1; |
| 1851 | pdata = xmalloc(plen + 2); |
| 1852 | memcpy(pdata, data, len); |
| 1853 | pdata[len] = '\n'; |
| 1854 | pdata[len + 1] = '\0'; |
| 1855 | } |
| 1856 | if (prefix != '+') { |
| 1857 | ecbdata->lno_in_preimage++; |
| 1858 | emit_del_line(ecbdata, pdata ? pdata : data, plen); |
| 1859 | } else { |
| 1860 | ecbdata->lno_in_postimage++; |
| 1861 | emit_add_line(ecbdata, pdata ? pdata : data, plen); |
| 1862 | } |
| 1863 | free(pdata); |
| 1864 | size -= len; |
| 1865 | data += len; |
| 1866 | } |
| 1867 | if (!endp) { |
| 1868 | static const char nneof[] = "\\ No newline at end of file\n"; |
| 1869 | ecbdata->last_line_kind = prefix; |
| 1870 | emit_incomplete_line_marker(ecbdata, nneof, sizeof(nneof) - 1); |
| 1871 | } |
| 1872 | } |
| 1873 | |
| 1874 | static void emit_rewrite_diff(const char *name_a, |
| 1875 | const char *name_b, |
| 1876 | struct diff_filespec *one, |
| 1877 | struct diff_filespec *two, |
| 1878 | struct userdiff_driver *textconv_one, |
| 1879 | struct userdiff_driver *textconv_two, |
| 1880 | struct diff_options *o) |
| 1881 | { |
| 1882 | int lc_a, lc_b; |
| 1883 | static struct strbuf a_name = STRBUF_INIT, b_name = STRBUF_INIT; |
| 1884 | const char *a_prefix, *b_prefix; |
| 1885 | char *data_one, *data_two; |
| 1886 | size_t size_one, size_two; |
| 1887 | unsigned ws_rule; |
| 1888 | struct emit_callback ecbdata; |
| 1889 | struct strbuf out = STRBUF_INIT; |
| 1890 | |
| 1891 | if (diff_mnemonic_prefix && o->flags.reverse_diff) { |
| 1892 | a_prefix = o->b_prefix; |
| 1893 | b_prefix = o->a_prefix; |
| 1894 | } else { |
| 1895 | a_prefix = o->a_prefix; |
| 1896 | b_prefix = o->b_prefix; |
| 1897 | } |
| 1898 | |
| 1899 | name_a += (*name_a == '/'); |
| 1900 | name_b += (*name_b == '/'); |
| 1901 | |
| 1902 | strbuf_reset(&a_name); |
| 1903 | strbuf_reset(&b_name); |
| 1904 | quote_two_c_style(&a_name, a_prefix, name_a, 0); |
| 1905 | quote_two_c_style(&b_name, b_prefix, name_b, 0); |
| 1906 | |
| 1907 | size_one = fill_textconv(o->repo, textconv_one, one, &data_one); |
| 1908 | size_two = fill_textconv(o->repo, textconv_two, two, &data_two); |
| 1909 | |
| 1910 | ws_rule = whitespace_rule(o->repo->index, name_b); |
| 1911 | |
| 1912 | /* symlink being an incomplete line is not a news */ |
| 1913 | if (DIFF_FILE_VALID(two) && S_ISLNK(two->mode)) |
| 1914 | ws_rule &= ~WS_INCOMPLETE_LINE; |
| 1915 | |
| 1916 | memset(&ecbdata, 0, sizeof(ecbdata)); |
| 1917 | ecbdata.color_diff = o->use_color; |
| 1918 | ecbdata.ws_rule = ws_rule; |
| 1919 | ecbdata.opt = o; |
| 1920 | if (ecbdata.ws_rule & WS_BLANK_AT_EOF) { |
| 1921 | mmfile_t mf1, mf2; |
| 1922 | mf1.ptr = (char *)data_one; |
| 1923 | mf2.ptr = (char *)data_two; |
| 1924 | mf1.size = size_one; |
| 1925 | mf2.size = size_two; |
| 1926 | check_blank_at_eof(&mf1, &mf2, &ecbdata); |
| 1927 | } |
| 1928 | ecbdata.lno_in_preimage = 1; |
| 1929 | ecbdata.lno_in_postimage = 1; |
| 1930 | |
| 1931 | lc_a = count_lines(data_one, size_one); |
| 1932 | lc_b = count_lines(data_two, size_two); |
| 1933 | |
| 1934 | emit_diff_symbol(o, DIFF_SYMBOL_FILEPAIR_MINUS, |
| 1935 | a_name.buf, a_name.len, 0); |
| 1936 | emit_diff_symbol(o, DIFF_SYMBOL_FILEPAIR_PLUS, |
| 1937 | b_name.buf, b_name.len, 0); |
| 1938 | |
| 1939 | strbuf_addstr(&out, "@@ -"); |
| 1940 | if (!o->irreversible_delete) |
| 1941 | add_line_count(&out, lc_a); |
| 1942 | else |
| 1943 | strbuf_addstr(&out, "?,?"); |
| 1944 | strbuf_addstr(&out, " +"); |
| 1945 | add_line_count(&out, lc_b); |
| 1946 | strbuf_addstr(&out, " @@\n"); |
| 1947 | emit_diff_symbol(o, DIFF_SYMBOL_REWRITE_DIFF, out.buf, out.len, 0); |
| 1948 | strbuf_release(&out); |
| 1949 | |
| 1950 | if (lc_a && !o->irreversible_delete) |
| 1951 | emit_rewrite_lines(&ecbdata, '-', data_one, size_one); |
| 1952 | if (lc_b) |
| 1953 | emit_rewrite_lines(&ecbdata, '+', data_two, size_two); |
| 1954 | if (textconv_one) |
| 1955 | free((char *)data_one); |
| 1956 | if (textconv_two) |
| 1957 | free((char *)data_two); |
| 1958 | } |
| 1959 | |
| 1960 | struct diff_words_buffer { |
| 1961 | mmfile_t text; |
| 1962 | unsigned long alloc; |
| 1963 | struct diff_words_orig { |
| 1964 | const char *begin, *end; |
| 1965 | } *orig; |
| 1966 | int orig_nr, orig_alloc; |
| 1967 | }; |
| 1968 | |
| 1969 | static void diff_words_append(char *line, unsigned long len, |
| 1970 | struct diff_words_buffer *buffer) |
| 1971 | { |
| 1972 | ALLOC_GROW(buffer->text.ptr, buffer->text.size + len, buffer->alloc); |
| 1973 | line++; |
| 1974 | len--; |
| 1975 | memcpy(buffer->text.ptr + buffer->text.size, line, len); |
| 1976 | buffer->text.size += len; |
| 1977 | buffer->text.ptr[buffer->text.size] = '\0'; |
| 1978 | } |
| 1979 | |
| 1980 | struct diff_words_style_elem { |
| 1981 | const char *prefix; |
| 1982 | const char *suffix; |
| 1983 | const char *color; /* NULL; filled in by the setup code if |
| 1984 | * color is enabled */ |
| 1985 | }; |
| 1986 | |
| 1987 | struct diff_words_style { |
| 1988 | enum diff_words_type type; |
| 1989 | struct diff_words_style_elem new_word, old_word, ctx; |
| 1990 | const char *newline; |
| 1991 | }; |
| 1992 | |
| 1993 | static struct diff_words_style diff_words_styles[] = { |
| 1994 | { DIFF_WORDS_PORCELAIN, {"+", "\n"}, {"-", "\n"}, {" ", "\n"}, "~\n" }, |
| 1995 | { DIFF_WORDS_PLAIN, {"{+", "+}"}, {"[-", "-]"}, {"", ""}, "\n" }, |
| 1996 | { DIFF_WORDS_COLOR, {"", ""}, {"", ""}, {"", ""}, "\n" } |
| 1997 | }; |
| 1998 | |
| 1999 | struct diff_words_data { |
| 2000 | struct diff_words_buffer minus, plus; |
| 2001 | const char *current_plus; |
| 2002 | int last_minus; |
| 2003 | struct diff_options *opt; |
| 2004 | regex_t *word_regex; |
| 2005 | enum diff_words_type type; |
| 2006 | struct diff_words_style *style; |
| 2007 | }; |
| 2008 | |
| 2009 | static int fn_out_diff_words_write_helper(struct diff_options *o, |
| 2010 | struct diff_words_style_elem *st_el, |
| 2011 | const char *newline, |
| 2012 | size_t count, const char *buf) |
| 2013 | { |
| 2014 | int print = 0; |
| 2015 | struct strbuf sb = STRBUF_INIT; |
| 2016 | |
| 2017 | while (count) { |
| 2018 | const char *p = memchr(buf, '\n', count); |
| 2019 | if (print) |
| 2020 | strbuf_addstr(&sb, diff_line_prefix(o)); |
| 2021 | |
| 2022 | if (p != buf) { |
| 2023 | const char *reset = st_el->color && *st_el->color ? |
| 2024 | GIT_COLOR_RESET : NULL; |
| 2025 | if (st_el->color && *st_el->color) |
| 2026 | strbuf_addstr(&sb, st_el->color); |
| 2027 | strbuf_addstr(&sb, st_el->prefix); |
| 2028 | strbuf_add(&sb, buf, p ? p - buf : count); |
| 2029 | strbuf_addstr(&sb, st_el->suffix); |
| 2030 | if (reset) |
| 2031 | strbuf_addstr(&sb, reset); |
| 2032 | } |
| 2033 | if (!p) |
| 2034 | goto out; |
| 2035 | |
| 2036 | strbuf_addstr(&sb, newline); |
| 2037 | count -= p + 1 - buf; |
| 2038 | buf = p + 1; |
| 2039 | print = 1; |
| 2040 | if (count) { |
| 2041 | emit_diff_symbol(o, DIFF_SYMBOL_WORD_DIFF, |
| 2042 | sb.buf, sb.len, 0); |
| 2043 | strbuf_reset(&sb); |
| 2044 | } |
| 2045 | } |
| 2046 | |
| 2047 | out: |
| 2048 | if (sb.len) |
| 2049 | emit_diff_symbol(o, DIFF_SYMBOL_WORD_DIFF, |
| 2050 | sb.buf, sb.len, 0); |
| 2051 | strbuf_release(&sb); |
| 2052 | return 0; |
| 2053 | } |
| 2054 | |
| 2055 | /* |
| 2056 | * '--color-words' algorithm can be described as: |
| 2057 | * |
| 2058 | * 1. collect the minus/plus lines of a diff hunk, divided into |
| 2059 | * minus-lines and plus-lines; |
| 2060 | * |
| 2061 | * 2. break both minus-lines and plus-lines into words and |
| 2062 | * place them into two mmfile_t with one word for each line; |
| 2063 | * |
| 2064 | * 3. use xdiff to run diff on the two mmfile_t to get the words level diff; |
| 2065 | * |
| 2066 | * And for the common parts of the both file, we output the plus side text. |
| 2067 | * diff_words->current_plus is used to trace the current position of the plus file |
| 2068 | * which printed. diff_words->last_minus is used to trace the last minus word |
| 2069 | * printed. |
| 2070 | * |
| 2071 | * For '--graph' to work with '--color-words', we need to output the graph prefix |
| 2072 | * on each line of color words output. Generally, there are two conditions on |
| 2073 | * which we should output the prefix. |
| 2074 | * |
| 2075 | * 1. diff_words->last_minus == 0 && |
| 2076 | * diff_words->current_plus == diff_words->plus.text.ptr |
| 2077 | * |
| 2078 | * that is: the plus text must start as a new line, and if there is no minus |
| 2079 | * word printed, a graph prefix must be printed. |
| 2080 | * |
| 2081 | * 2. diff_words->current_plus > diff_words->plus.text.ptr && |
| 2082 | * *(diff_words->current_plus - 1) == '\n' |
| 2083 | * |
| 2084 | * that is: a graph prefix must be printed following a '\n' |
| 2085 | */ |
| 2086 | static int color_words_output_graph_prefix(struct diff_words_data *diff_words) |
| 2087 | { |
| 2088 | if ((diff_words->last_minus == 0 && |
| 2089 | diff_words->current_plus == diff_words->plus.text.ptr) || |
| 2090 | (diff_words->current_plus > diff_words->plus.text.ptr && |
| 2091 | *(diff_words->current_plus - 1) == '\n')) { |
| 2092 | return 1; |
| 2093 | } else { |
| 2094 | return 0; |
| 2095 | } |
| 2096 | } |
| 2097 | |
| 2098 | static void fn_out_diff_words_aux(void *priv, |
| 2099 | long minus_first, long minus_len, |
| 2100 | long plus_first, long plus_len, |
| 2101 | const char *func UNUSED, long funclen UNUSED) |
| 2102 | { |
| 2103 | struct diff_words_data *diff_words = priv; |
| 2104 | struct diff_words_style *style = diff_words->style; |
| 2105 | const char *minus_begin, *minus_end, *plus_begin, *plus_end; |
| 2106 | struct diff_options *opt = diff_words->opt; |
| 2107 | const char *line_prefix; |
| 2108 | |
| 2109 | assert(opt); |
| 2110 | line_prefix = diff_line_prefix(opt); |
| 2111 | |
| 2112 | /* POSIX requires that first be decremented by one if len == 0... */ |
| 2113 | if (minus_len) { |
| 2114 | minus_begin = diff_words->minus.orig[minus_first].begin; |
| 2115 | minus_end = |
| 2116 | diff_words->minus.orig[minus_first + minus_len - 1].end; |
| 2117 | } else |
| 2118 | minus_begin = minus_end = |
| 2119 | diff_words->minus.orig[minus_first].end; |
| 2120 | |
| 2121 | if (plus_len) { |
| 2122 | plus_begin = diff_words->plus.orig[plus_first].begin; |
| 2123 | plus_end = diff_words->plus.orig[plus_first + plus_len - 1].end; |
| 2124 | } else |
| 2125 | plus_begin = plus_end = diff_words->plus.orig[plus_first].end; |
| 2126 | |
| 2127 | if (color_words_output_graph_prefix(diff_words)) { |
| 2128 | fputs(line_prefix, diff_words->opt->file); |
| 2129 | } |
| 2130 | if (diff_words->current_plus != plus_begin) { |
| 2131 | fn_out_diff_words_write_helper(diff_words->opt, |
| 2132 | &style->ctx, style->newline, |
| 2133 | plus_begin - diff_words->current_plus, |
| 2134 | diff_words->current_plus); |
| 2135 | } |
| 2136 | if (minus_begin != minus_end) { |
| 2137 | fn_out_diff_words_write_helper(diff_words->opt, |
| 2138 | &style->old_word, style->newline, |
| 2139 | minus_end - minus_begin, minus_begin); |
| 2140 | } |
| 2141 | if (plus_begin != plus_end) { |
| 2142 | fn_out_diff_words_write_helper(diff_words->opt, |
| 2143 | &style->new_word, style->newline, |
| 2144 | plus_end - plus_begin, plus_begin); |
| 2145 | } |
| 2146 | |
| 2147 | diff_words->current_plus = plus_end; |
| 2148 | diff_words->last_minus = minus_first; |
| 2149 | } |
| 2150 | |
| 2151 | /* This function starts looking at *begin, and returns 0 iff a word was found. */ |
| 2152 | static int find_word_boundaries(mmfile_t *buffer, regex_t *word_regex, |
| 2153 | int *begin, int *end) |
| 2154 | { |
| 2155 | while (word_regex && *begin < buffer->size) { |
| 2156 | regmatch_t match[1]; |
| 2157 | if (!regexec_buf(word_regex, buffer->ptr + *begin, |
| 2158 | buffer->size - *begin, 1, match, 0)) { |
| 2159 | char *p = memchr(buffer->ptr + *begin + match[0].rm_so, |
| 2160 | '\n', match[0].rm_eo - match[0].rm_so); |
| 2161 | *end = p ? p - buffer->ptr : match[0].rm_eo + *begin; |
| 2162 | *begin += match[0].rm_so; |
| 2163 | if (*begin == *end) |
| 2164 | (*begin)++; |
| 2165 | else |
| 2166 | return *begin > *end; |
| 2167 | } else { |
| 2168 | return -1; |
| 2169 | } |
| 2170 | } |
| 2171 | |
| 2172 | /* find the next word */ |
| 2173 | while (*begin < buffer->size && isspace(buffer->ptr[*begin])) |
| 2174 | (*begin)++; |
| 2175 | if (*begin >= buffer->size) |
| 2176 | return -1; |
| 2177 | |
| 2178 | /* find the end of the word */ |
| 2179 | *end = *begin + 1; |
| 2180 | while (*end < buffer->size && !isspace(buffer->ptr[*end])) |
| 2181 | (*end)++; |
| 2182 | |
| 2183 | return 0; |
| 2184 | } |
| 2185 | |
| 2186 | /* |
| 2187 | * This function splits the words in buffer->text, stores the list with |
| 2188 | * newline separator into out, and saves the offsets of the original words |
| 2189 | * in buffer->orig. |
| 2190 | */ |
| 2191 | static void diff_words_fill(struct diff_words_buffer *buffer, mmfile_t *out, |
| 2192 | regex_t *word_regex) |
| 2193 | { |
| 2194 | int i, j; |
| 2195 | long alloc = 0; |
| 2196 | |
| 2197 | out->size = 0; |
| 2198 | out->ptr = NULL; |
| 2199 | |
| 2200 | /* fake an empty "0th" word */ |
| 2201 | ALLOC_GROW(buffer->orig, 1, buffer->orig_alloc); |
| 2202 | buffer->orig[0].begin = buffer->orig[0].end = buffer->text.ptr; |
| 2203 | buffer->orig_nr = 1; |
| 2204 | |
| 2205 | for (i = 0; i < buffer->text.size; i++) { |
| 2206 | if (find_word_boundaries(&buffer->text, word_regex, &i, &j)) |
| 2207 | return; |
| 2208 | |
| 2209 | /* store original boundaries */ |
| 2210 | ALLOC_GROW(buffer->orig, buffer->orig_nr + 1, |
| 2211 | buffer->orig_alloc); |
| 2212 | buffer->orig[buffer->orig_nr].begin = buffer->text.ptr + i; |
| 2213 | buffer->orig[buffer->orig_nr].end = buffer->text.ptr + j; |
| 2214 | buffer->orig_nr++; |
| 2215 | |
| 2216 | /* store one word */ |
| 2217 | ALLOC_GROW(out->ptr, out->size + j - i + 1, alloc); |
| 2218 | memcpy(out->ptr + out->size, buffer->text.ptr + i, j - i); |
| 2219 | out->ptr[out->size + j - i] = '\n'; |
| 2220 | out->size += j - i + 1; |
| 2221 | |
| 2222 | i = j - 1; |
| 2223 | } |
| 2224 | } |
| 2225 | |
| 2226 | /* this executes the word diff on the accumulated buffers */ |
| 2227 | static void diff_words_show(struct diff_words_data *diff_words) |
| 2228 | { |
| 2229 | xpparam_t xpp; |
| 2230 | xdemitconf_t xecfg; |
| 2231 | mmfile_t minus, plus; |
| 2232 | struct diff_words_style *style = diff_words->style; |
| 2233 | |
| 2234 | struct diff_options *opt = diff_words->opt; |
| 2235 | const char *line_prefix; |
| 2236 | |
| 2237 | assert(opt); |
| 2238 | line_prefix = diff_line_prefix(opt); |
| 2239 | |
| 2240 | /* special case: only removal */ |
| 2241 | if (!diff_words->plus.text.size) { |
| 2242 | emit_diff_symbol(diff_words->opt, DIFF_SYMBOL_WORD_DIFF, |
| 2243 | line_prefix, strlen(line_prefix), 0); |
| 2244 | fn_out_diff_words_write_helper(diff_words->opt, |
| 2245 | &style->old_word, style->newline, |
| 2246 | diff_words->minus.text.size, |
| 2247 | diff_words->minus.text.ptr); |
| 2248 | diff_words->minus.text.size = 0; |
| 2249 | return; |
| 2250 | } |
| 2251 | |
| 2252 | diff_words->current_plus = diff_words->plus.text.ptr; |
| 2253 | diff_words->last_minus = 0; |
| 2254 | |
| 2255 | memset(&xpp, 0, sizeof(xpp)); |
| 2256 | memset(&xecfg, 0, sizeof(xecfg)); |
| 2257 | diff_words_fill(&diff_words->minus, &minus, diff_words->word_regex); |
| 2258 | diff_words_fill(&diff_words->plus, &plus, diff_words->word_regex); |
| 2259 | xpp.flags = 0; |
| 2260 | /* as only the hunk header will be parsed, we need a 0-context */ |
| 2261 | xecfg.ctxlen = 0; |
| 2262 | if (xdi_diff_outf(&minus, &plus, fn_out_diff_words_aux, NULL, |
| 2263 | diff_words, &xpp, &xecfg)) |
| 2264 | die("unable to generate word diff"); |
| 2265 | free(minus.ptr); |
| 2266 | free(plus.ptr); |
| 2267 | if (diff_words->current_plus != diff_words->plus.text.ptr + |
| 2268 | diff_words->plus.text.size) { |
| 2269 | if (color_words_output_graph_prefix(diff_words)) |
| 2270 | emit_diff_symbol(diff_words->opt, DIFF_SYMBOL_WORD_DIFF, |
| 2271 | line_prefix, strlen(line_prefix), 0); |
| 2272 | fn_out_diff_words_write_helper(diff_words->opt, |
| 2273 | &style->ctx, style->newline, |
| 2274 | diff_words->plus.text.ptr + diff_words->plus.text.size |
| 2275 | - diff_words->current_plus, diff_words->current_plus); |
| 2276 | } |
| 2277 | diff_words->minus.text.size = diff_words->plus.text.size = 0; |
| 2278 | } |
| 2279 | |
| 2280 | /* In "color-words" mode, show word-diff of words accumulated in the buffer */ |
| 2281 | static void diff_words_flush(struct emit_callback *ecbdata) |
| 2282 | { |
| 2283 | struct diff_options *wo = ecbdata->diff_words->opt; |
| 2284 | |
| 2285 | if (ecbdata->diff_words->minus.text.size || |
| 2286 | ecbdata->diff_words->plus.text.size) |
| 2287 | diff_words_show(ecbdata->diff_words); |
| 2288 | |
| 2289 | if (wo->emitted_symbols) { |
| 2290 | struct diff_options *o = ecbdata->opt; |
| 2291 | struct emitted_diff_symbols *wol = wo->emitted_symbols; |
| 2292 | int i; |
| 2293 | |
| 2294 | /* |
| 2295 | * NEEDSWORK: |
| 2296 | * Instead of appending each, concat all words to a line? |
| 2297 | */ |
| 2298 | for (i = 0; i < wol->nr; i++) |
| 2299 | append_emitted_diff_symbol(o, &wol->buf[i]); |
| 2300 | |
| 2301 | for (i = 0; i < wol->nr; i++) |
| 2302 | free((void *)wol->buf[i].line); |
| 2303 | |
| 2304 | wol->nr = 0; |
| 2305 | } |
| 2306 | } |
| 2307 | |
| 2308 | static void diff_filespec_load_driver(struct diff_filespec *one, |
| 2309 | struct index_state *istate) |
| 2310 | { |
| 2311 | /* Use already-loaded driver */ |
| 2312 | if (one->driver) |
| 2313 | return; |
| 2314 | |
| 2315 | if (S_ISREG(one->mode)) |
| 2316 | one->driver = userdiff_find_by_path(istate, one->path); |
| 2317 | |
| 2318 | /* Fallback to default settings */ |
| 2319 | if (!one->driver) |
| 2320 | one->driver = userdiff_find_by_name("default"); |
| 2321 | } |
| 2322 | |
| 2323 | static const char *userdiff_word_regex(struct diff_filespec *one, |
| 2324 | struct index_state *istate) |
| 2325 | { |
| 2326 | diff_filespec_load_driver(one, istate); |
| 2327 | return one->driver->word_regex; |
| 2328 | } |
| 2329 | |
| 2330 | static void init_diff_words_data(struct emit_callback *ecbdata, |
| 2331 | struct diff_options *orig_opts, |
| 2332 | struct diff_filespec *one, |
| 2333 | struct diff_filespec *two) |
| 2334 | { |
| 2335 | int i; |
| 2336 | struct diff_options *o = xmalloc(sizeof(struct diff_options)); |
| 2337 | memcpy(o, orig_opts, sizeof(struct diff_options)); |
| 2338 | |
| 2339 | CALLOC_ARRAY(ecbdata->diff_words, 1); |
| 2340 | ecbdata->diff_words->type = o->word_diff; |
| 2341 | ecbdata->diff_words->opt = o; |
| 2342 | |
| 2343 | if (orig_opts->emitted_symbols) |
| 2344 | CALLOC_ARRAY(o->emitted_symbols, 1); |
| 2345 | |
| 2346 | if (!o->word_regex) |
| 2347 | o->word_regex = userdiff_word_regex(one, o->repo->index); |
| 2348 | if (!o->word_regex) |
| 2349 | o->word_regex = userdiff_word_regex(two, o->repo->index); |
| 2350 | if (!o->word_regex) |
| 2351 | o->word_regex = diff_word_regex_cfg; |
| 2352 | if (o->word_regex) { |
| 2353 | ecbdata->diff_words->word_regex = (regex_t *) |
| 2354 | xmalloc(sizeof(regex_t)); |
| 2355 | if (regcomp(ecbdata->diff_words->word_regex, |
| 2356 | o->word_regex, |
| 2357 | REG_EXTENDED | REG_NEWLINE)) |
| 2358 | die("invalid regular expression: %s", |
| 2359 | o->word_regex); |
| 2360 | } |
| 2361 | for (i = 0; i < ARRAY_SIZE(diff_words_styles); i++) { |
| 2362 | if (o->word_diff == diff_words_styles[i].type) { |
| 2363 | ecbdata->diff_words->style = |
| 2364 | &diff_words_styles[i]; |
| 2365 | break; |
| 2366 | } |
| 2367 | } |
| 2368 | if (want_color(o->use_color)) { |
| 2369 | struct diff_words_style *st = ecbdata->diff_words->style; |
| 2370 | st->old_word.color = diff_get_color_opt(o, DIFF_FILE_OLD); |
| 2371 | st->new_word.color = diff_get_color_opt(o, DIFF_FILE_NEW); |
| 2372 | st->ctx.color = diff_get_color_opt(o, DIFF_CONTEXT); |
| 2373 | } |
| 2374 | } |
| 2375 | |
| 2376 | static void free_diff_words_data(struct emit_callback *ecbdata) |
| 2377 | { |
| 2378 | if (ecbdata->diff_words) { |
| 2379 | diff_words_flush(ecbdata); |
| 2380 | free_emitted_diff_symbols(ecbdata->diff_words->opt->emitted_symbols); |
| 2381 | free (ecbdata->diff_words->opt); |
| 2382 | free (ecbdata->diff_words->minus.text.ptr); |
| 2383 | free (ecbdata->diff_words->minus.orig); |
| 2384 | free (ecbdata->diff_words->plus.text.ptr); |
| 2385 | free (ecbdata->diff_words->plus.orig); |
| 2386 | if (ecbdata->diff_words->word_regex) { |
| 2387 | regfree(ecbdata->diff_words->word_regex); |
| 2388 | free(ecbdata->diff_words->word_regex); |
| 2389 | } |
| 2390 | FREE_AND_NULL(ecbdata->diff_words); |
| 2391 | } |
| 2392 | } |
| 2393 | |
| 2394 | const char *diff_get_color(enum git_colorbool diff_use_color, enum color_diff ix) |
| 2395 | { |
| 2396 | if (want_color(diff_use_color)) |
| 2397 | return diff_colors[ix]; |
| 2398 | return ""; |
| 2399 | } |
| 2400 | |
| 2401 | const char *diff_line_prefix(struct diff_options *opt) |
| 2402 | { |
| 2403 | return opt->output_prefix ? |
| 2404 | opt->output_prefix(opt, opt->output_prefix_data) : |
| 2405 | ""; |
| 2406 | } |
| 2407 | |
| 2408 | static unsigned long sane_truncate_line(char *line, unsigned long len) |
| 2409 | { |
| 2410 | const char *cp; |
| 2411 | unsigned long allot; |
| 2412 | size_t l = len; |
| 2413 | |
| 2414 | cp = line; |
| 2415 | allot = l; |
| 2416 | while (0 < l) { |
| 2417 | (void) utf8_width(&cp, &l); |
| 2418 | if (!cp) |
| 2419 | break; /* truncated in the middle? */ |
| 2420 | } |
| 2421 | return allot - l; |
| 2422 | } |
| 2423 | |
| 2424 | static void find_lno(const char *line, struct emit_callback *ecbdata) |
| 2425 | { |
| 2426 | const char *p; |
| 2427 | ecbdata->lno_in_preimage = 0; |
| 2428 | ecbdata->lno_in_postimage = 0; |
| 2429 | p = strchr(line, '-'); |
| 2430 | if (!p) |
| 2431 | return; /* cannot happen */ |
| 2432 | ecbdata->lno_in_preimage = strtol(p + 1, NULL, 10); |
| 2433 | p = strchr(p, '+'); |
| 2434 | if (!p) |
| 2435 | return; /* cannot happen */ |
| 2436 | ecbdata->lno_in_postimage = strtol(p + 1, NULL, 10); |
| 2437 | } |
| 2438 | |
| 2439 | static int fn_out_consume(void *priv, char *line, unsigned long len) |
| 2440 | { |
| 2441 | struct emit_callback *ecbdata = priv; |
| 2442 | struct diff_options *o = ecbdata->opt; |
| 2443 | |
| 2444 | o->found_changes = 1; |
| 2445 | |
| 2446 | if (ecbdata->header) { |
| 2447 | emit_diff_symbol(o, DIFF_SYMBOL_HEADER, |
| 2448 | ecbdata->header->buf, ecbdata->header->len, 0); |
| 2449 | strbuf_reset(ecbdata->header); |
| 2450 | ecbdata->header = NULL; |
| 2451 | } |
| 2452 | |
| 2453 | if (ecbdata->label_path[0]) { |
| 2454 | emit_diff_symbol(o, DIFF_SYMBOL_FILEPAIR_MINUS, |
| 2455 | ecbdata->label_path[0], |
| 2456 | strlen(ecbdata->label_path[0]), 0); |
| 2457 | emit_diff_symbol(o, DIFF_SYMBOL_FILEPAIR_PLUS, |
| 2458 | ecbdata->label_path[1], |
| 2459 | strlen(ecbdata->label_path[1]), 0); |
| 2460 | ecbdata->label_path[0] = ecbdata->label_path[1] = NULL; |
| 2461 | } |
| 2462 | |
| 2463 | if (line[0] == '@') { |
| 2464 | if (ecbdata->diff_words) |
| 2465 | diff_words_flush(ecbdata); |
| 2466 | len = sane_truncate_line(line, len); |
| 2467 | find_lno(line, ecbdata); |
| 2468 | emit_hunk_header(ecbdata, line, len); |
| 2469 | return 0; |
| 2470 | } |
| 2471 | |
| 2472 | if (ecbdata->diff_words) { |
| 2473 | enum diff_symbol s = |
| 2474 | ecbdata->diff_words->type == DIFF_WORDS_PORCELAIN ? |
| 2475 | DIFF_SYMBOL_WORDS_PORCELAIN : DIFF_SYMBOL_WORDS; |
| 2476 | if (line[0] == '-') { |
| 2477 | diff_words_append(line, len, |
| 2478 | &ecbdata->diff_words->minus); |
| 2479 | return 0; |
| 2480 | } else if (line[0] == '+') { |
| 2481 | diff_words_append(line, len, |
| 2482 | &ecbdata->diff_words->plus); |
| 2483 | return 0; |
| 2484 | } else if (starts_with(line, "\\ ")) { |
| 2485 | /* |
| 2486 | * Eat the "no newline at eof" marker as if we |
| 2487 | * saw a "+" or "-" line with nothing on it, |
| 2488 | * and return without diff_words_flush() to |
| 2489 | * defer processing. If this is the end of |
| 2490 | * preimage, more "+" lines may come after it. |
| 2491 | */ |
| 2492 | return 0; |
| 2493 | } |
| 2494 | diff_words_flush(ecbdata); |
| 2495 | emit_diff_symbol(o, s, line, len, 0); |
| 2496 | return 0; |
| 2497 | } |
| 2498 | |
| 2499 | switch (line[0]) { |
| 2500 | case '+': |
| 2501 | ecbdata->lno_in_postimage++; |
| 2502 | emit_add_line(ecbdata, line + 1, len - 1); |
| 2503 | break; |
| 2504 | case '-': |
| 2505 | ecbdata->lno_in_preimage++; |
| 2506 | emit_del_line(ecbdata, line + 1, len - 1); |
| 2507 | break; |
| 2508 | case ' ': |
| 2509 | ecbdata->lno_in_postimage++; |
| 2510 | ecbdata->lno_in_preimage++; |
| 2511 | emit_context_line(ecbdata, line + 1, len - 1); |
| 2512 | break; |
| 2513 | case '\\': |
| 2514 | /* incomplete line at the end */ |
| 2515 | switch (ecbdata->last_line_kind) { |
| 2516 | case '+': |
| 2517 | case '-': |
| 2518 | case ' ': |
| 2519 | break; |
| 2520 | default: |
| 2521 | BUG("fn_out_consume: '\\No newline' after unknown line (%c)", |
| 2522 | ecbdata->last_line_kind); |
| 2523 | } |
| 2524 | ecbdata->lno_in_preimage++; |
| 2525 | emit_incomplete_line_marker(ecbdata, line, len); |
| 2526 | break; |
| 2527 | default: |
| 2528 | BUG("fn_out_consume: unknown line '%s'", line); |
| 2529 | } |
| 2530 | ecbdata->last_line_kind = line[0]; |
| 2531 | return 0; |
| 2532 | } |
| 2533 | |
| 2534 | static int quick_consume(void *priv, char *line UNUSED, unsigned long len UNUSED) |
| 2535 | { |
| 2536 | struct emit_callback *ecbdata = priv; |
| 2537 | struct diff_options *o = ecbdata->opt; |
| 2538 | |
| 2539 | o->found_changes = 1; |
| 2540 | return 1; |
| 2541 | } |
| 2542 | |
| 2543 | static void discard_pending_rm(struct line_range_callback *s) |
| 2544 | { |
| 2545 | strbuf_reset(&s->pending_rm); |
| 2546 | s->pending_rm_count = 0; |
| 2547 | } |
| 2548 | |
| 2549 | static void flush_rhunk(struct line_range_callback *s) |
| 2550 | { |
| 2551 | struct strbuf hdr = STRBUF_INIT; |
| 2552 | const char *p, *end; |
| 2553 | |
| 2554 | if (!s->rhunk_active || s->ret) |
| 2555 | return; |
| 2556 | |
| 2557 | /* Drain any pending removal lines into the range hunk */ |
| 2558 | if (s->pending_rm_count) { |
| 2559 | strbuf_addbuf(&s->rhunk, &s->pending_rm); |
| 2560 | s->rhunk_old_count += s->pending_rm_count; |
| 2561 | s->rhunk_has_changes = 1; |
| 2562 | discard_pending_rm(s); |
| 2563 | } |
| 2564 | |
| 2565 | /* |
| 2566 | * Suppress context-only hunks: they contain no actual changes |
| 2567 | * and would just be noise. This can happen when the inflated |
| 2568 | * ctxlen causes xdiff to emit context covering a range that |
| 2569 | * has no changes in this commit. |
| 2570 | */ |
| 2571 | if (!s->rhunk_has_changes) { |
| 2572 | s->rhunk_active = 0; |
| 2573 | strbuf_reset(&s->rhunk); |
| 2574 | return; |
| 2575 | } |
| 2576 | |
| 2577 | strbuf_addf(&hdr, "@@ -%ld,%ld +%ld,%ld @@", |
| 2578 | s->rhunk_old_begin, s->rhunk_old_count, |
| 2579 | s->rhunk_new_begin, s->rhunk_new_count); |
| 2580 | if (s->funclen > 0) { |
| 2581 | strbuf_addch(&hdr, ' '); |
| 2582 | strbuf_add(&hdr, s->func, s->funclen); |
| 2583 | } |
| 2584 | strbuf_addch(&hdr, '\n'); |
| 2585 | |
| 2586 | s->ret = s->orig_line_fn(s->orig_cb_data, hdr.buf, hdr.len); |
| 2587 | strbuf_release(&hdr); |
| 2588 | |
| 2589 | /* |
| 2590 | * Replay buffered lines one at a time through fn_out_consume. |
| 2591 | * The cast discards const because xdiff_emit_line_fn takes |
| 2592 | * char *, though fn_out_consume does not modify the buffer. |
| 2593 | */ |
| 2594 | p = s->rhunk.buf; |
| 2595 | end = p + s->rhunk.len; |
| 2596 | while (!s->ret && p < end) { |
| 2597 | const char *eol = memchr(p, '\n', end - p); |
| 2598 | unsigned long line_len = eol ? (unsigned long)(eol - p + 1) |
| 2599 | : (unsigned long)(end - p); |
| 2600 | s->ret = s->orig_line_fn(s->orig_cb_data, (char *)p, line_len); |
| 2601 | p += line_len; |
| 2602 | } |
| 2603 | |
| 2604 | s->rhunk_active = 0; |
| 2605 | strbuf_reset(&s->rhunk); |
| 2606 | } |
| 2607 | |
| 2608 | static void line_range_hunk_fn(void *data, |
| 2609 | long old_begin, long old_nr UNUSED, |
| 2610 | long new_begin, long new_nr UNUSED, |
| 2611 | const char *func, long funclen) |
| 2612 | { |
| 2613 | struct line_range_callback *s = data; |
| 2614 | |
| 2615 | /* |
| 2616 | * When count > 0, begin is 1-based. When count == 0, begin is |
| 2617 | * adjusted down by 1 by xdl_emit_hunk_hdr(), but no lines of |
| 2618 | * that type will arrive, so the value is unused. |
| 2619 | * |
| 2620 | * Any pending removal lines from the previous xdiff hunk are |
| 2621 | * intentionally left in pending_rm: the line callback will |
| 2622 | * flush or discard them when the next content line reveals |
| 2623 | * whether the removals precede in-range content. |
| 2624 | */ |
| 2625 | s->lno_post = new_begin; |
| 2626 | s->lno_pre = old_begin; |
| 2627 | |
| 2628 | if (funclen > 0) { |
| 2629 | if (funclen > (long)sizeof(s->func)) |
| 2630 | funclen = sizeof(s->func); |
| 2631 | memcpy(s->func, func, funclen); |
| 2632 | } |
| 2633 | s->funclen = funclen; |
| 2634 | } |
| 2635 | |
| 2636 | static int line_range_line_fn(void *priv, char *line, unsigned long len) |
| 2637 | { |
| 2638 | struct line_range_callback *s = priv; |
| 2639 | const struct range *cur; |
| 2640 | long lno_0, cur_pre; |
| 2641 | |
| 2642 | if (s->ret) |
| 2643 | return s->ret; |
| 2644 | |
| 2645 | if (line[0] == '-') { |
| 2646 | if (!s->pending_rm_count) |
| 2647 | s->pending_rm_pre_begin = s->lno_pre; |
| 2648 | s->lno_pre++; |
| 2649 | strbuf_add(&s->pending_rm, line, len); |
| 2650 | s->pending_rm_count++; |
| 2651 | return s->ret; |
| 2652 | } |
| 2653 | |
| 2654 | if (line[0] == '\\') { |
| 2655 | if (s->pending_rm_count) |
| 2656 | strbuf_add(&s->pending_rm, line, len); |
| 2657 | else if (s->rhunk_active) |
| 2658 | strbuf_add(&s->rhunk, line, len); |
| 2659 | /* otherwise outside tracked range; drop silently */ |
| 2660 | return s->ret; |
| 2661 | } |
| 2662 | |
| 2663 | if (line[0] != '+' && line[0] != ' ') |
| 2664 | BUG("unexpected diff line type '%c'", line[0]); |
| 2665 | |
| 2666 | lno_0 = s->lno_post - 1; |
| 2667 | cur_pre = s->lno_pre; /* save before advancing for context lines */ |
| 2668 | s->lno_post++; |
| 2669 | if (line[0] == ' ') |
| 2670 | s->lno_pre++; |
| 2671 | |
| 2672 | /* Advance past ranges we've passed */ |
| 2673 | while (s->cur_range < s->ranges->nr && |
| 2674 | lno_0 >= s->ranges->ranges[s->cur_range].end) { |
| 2675 | if (s->rhunk_active) |
| 2676 | flush_rhunk(s); |
| 2677 | discard_pending_rm(s); |
| 2678 | s->cur_range++; |
| 2679 | } |
| 2680 | |
| 2681 | /* Past all ranges */ |
| 2682 | if (s->cur_range >= s->ranges->nr) { |
| 2683 | discard_pending_rm(s); |
| 2684 | return s->ret; |
| 2685 | } |
| 2686 | |
| 2687 | cur = &s->ranges->ranges[s->cur_range]; |
| 2688 | |
| 2689 | /* Before current range */ |
| 2690 | if (lno_0 < cur->start) { |
| 2691 | discard_pending_rm(s); |
| 2692 | return s->ret; |
| 2693 | } |
| 2694 | |
| 2695 | /* In range so start a new range hunk if needed */ |
| 2696 | if (!s->rhunk_active) { |
| 2697 | s->rhunk_active = 1; |
| 2698 | s->rhunk_has_changes = 0; |
| 2699 | s->rhunk_new_begin = lno_0 + 1; |
| 2700 | s->rhunk_old_begin = s->pending_rm_count |
| 2701 | ? s->pending_rm_pre_begin : cur_pre; |
| 2702 | s->rhunk_old_count = 0; |
| 2703 | s->rhunk_new_count = 0; |
| 2704 | strbuf_reset(&s->rhunk); |
| 2705 | } |
| 2706 | |
| 2707 | /* Flush pending removals into range hunk */ |
| 2708 | if (s->pending_rm_count) { |
| 2709 | strbuf_addbuf(&s->rhunk, &s->pending_rm); |
| 2710 | s->rhunk_old_count += s->pending_rm_count; |
| 2711 | s->rhunk_has_changes = 1; |
| 2712 | discard_pending_rm(s); |
| 2713 | } |
| 2714 | |
| 2715 | strbuf_add(&s->rhunk, line, len); |
| 2716 | s->rhunk_new_count++; |
| 2717 | if (line[0] == '+') |
| 2718 | s->rhunk_has_changes = 1; |
| 2719 | else |
| 2720 | s->rhunk_old_count++; |
| 2721 | |
| 2722 | return s->ret; |
| 2723 | } |
| 2724 | |
| 2725 | static void pprint_rename(struct strbuf *name, const char *a, const char *b) |
| 2726 | { |
| 2727 | const char *old_name = a; |
| 2728 | const char *new_name = b; |
| 2729 | int pfx_length, sfx_length; |
| 2730 | int pfx_adjust_for_slash; |
| 2731 | int len_a = strlen(a); |
| 2732 | int len_b = strlen(b); |
| 2733 | int a_midlen, b_midlen; |
| 2734 | int qlen_a = quote_c_style(a, NULL, NULL, 0); |
| 2735 | int qlen_b = quote_c_style(b, NULL, NULL, 0); |
| 2736 | |
| 2737 | if (qlen_a || qlen_b) { |
| 2738 | quote_c_style(a, name, NULL, 0); |
| 2739 | strbuf_addstr(name, " => "); |
| 2740 | quote_c_style(b, name, NULL, 0); |
| 2741 | return; |
| 2742 | } |
| 2743 | |
| 2744 | /* Find common prefix */ |
| 2745 | pfx_length = 0; |
| 2746 | while (*old_name && *new_name && *old_name == *new_name) { |
| 2747 | if (*old_name == '/') |
| 2748 | pfx_length = old_name - a + 1; |
| 2749 | old_name++; |
| 2750 | new_name++; |
| 2751 | } |
| 2752 | |
| 2753 | /* Find common suffix */ |
| 2754 | old_name = a + len_a; |
| 2755 | new_name = b + len_b; |
| 2756 | sfx_length = 0; |
| 2757 | /* |
| 2758 | * If there is a common prefix, it must end in a slash. In |
| 2759 | * that case we let this loop run 1 into the prefix to see the |
| 2760 | * same slash. |
| 2761 | * |
| 2762 | * If there is no common prefix, we cannot do this as it would |
| 2763 | * underrun the input strings. |
| 2764 | */ |
| 2765 | pfx_adjust_for_slash = (pfx_length ? 1 : 0); |
| 2766 | while (a + pfx_length - pfx_adjust_for_slash <= old_name && |
| 2767 | b + pfx_length - pfx_adjust_for_slash <= new_name && |
| 2768 | *old_name == *new_name) { |
| 2769 | if (*old_name == '/') |
| 2770 | sfx_length = len_a - (old_name - a); |
| 2771 | old_name--; |
| 2772 | new_name--; |
| 2773 | } |
| 2774 | |
| 2775 | /* |
| 2776 | * pfx{mid-a => mid-b}sfx |
| 2777 | * {pfx-a => pfx-b}sfx |
| 2778 | * pfx{sfx-a => sfx-b} |
| 2779 | * name-a => name-b |
| 2780 | */ |
| 2781 | a_midlen = len_a - pfx_length - sfx_length; |
| 2782 | b_midlen = len_b - pfx_length - sfx_length; |
| 2783 | if (a_midlen < 0) |
| 2784 | a_midlen = 0; |
| 2785 | if (b_midlen < 0) |
| 2786 | b_midlen = 0; |
| 2787 | |
| 2788 | strbuf_grow(name, pfx_length + a_midlen + b_midlen + sfx_length + 7); |
| 2789 | if (pfx_length + sfx_length) { |
| 2790 | strbuf_add(name, a, pfx_length); |
| 2791 | strbuf_addch(name, '{'); |
| 2792 | } |
| 2793 | strbuf_add(name, a + pfx_length, a_midlen); |
| 2794 | strbuf_addstr(name, " => "); |
| 2795 | strbuf_add(name, b + pfx_length, b_midlen); |
| 2796 | if (pfx_length + sfx_length) { |
| 2797 | strbuf_addch(name, '}'); |
| 2798 | strbuf_add(name, a + len_a - sfx_length, sfx_length); |
| 2799 | } |
| 2800 | } |
| 2801 | |
| 2802 | static struct diffstat_file *diffstat_add(struct diffstat_t *diffstat, |
| 2803 | const char *name_a, |
| 2804 | const char *name_b) |
| 2805 | { |
| 2806 | struct diffstat_file *x; |
| 2807 | CALLOC_ARRAY(x, 1); |
| 2808 | ALLOC_GROW(diffstat->files, diffstat->nr + 1, diffstat->alloc); |
| 2809 | diffstat->files[diffstat->nr++] = x; |
| 2810 | if (name_b) { |
| 2811 | x->from_name = xstrdup(name_a); |
| 2812 | x->name = xstrdup(name_b); |
| 2813 | x->is_renamed = 1; |
| 2814 | } |
| 2815 | else { |
| 2816 | x->from_name = NULL; |
| 2817 | x->name = xstrdup(name_a); |
| 2818 | } |
| 2819 | return x; |
| 2820 | } |
| 2821 | |
| 2822 | static int diffstat_consume(void *priv, char *line, unsigned long len) |
| 2823 | { |
| 2824 | struct diffstat_t *diffstat = priv; |
| 2825 | struct diffstat_file *x = diffstat->files[diffstat->nr - 1]; |
| 2826 | |
| 2827 | if (!len) |
| 2828 | BUG("xdiff fed us an empty line"); |
| 2829 | |
| 2830 | if (line[0] == '+') |
| 2831 | x->added++; |
| 2832 | else if (line[0] == '-') |
| 2833 | x->deleted++; |
| 2834 | return 0; |
| 2835 | } |
| 2836 | |
| 2837 | const char mime_boundary_leader[] = "------------"; |
| 2838 | |
| 2839 | static int scale_linear(int it, int width, int max_change) |
| 2840 | { |
| 2841 | if (!it) |
| 2842 | return 0; |
| 2843 | /* |
| 2844 | * make sure that at least one '-' or '+' is printed if |
| 2845 | * there is any change to this path. The easiest way is to |
| 2846 | * scale linearly as if the allotted width is one column shorter |
| 2847 | * than it is, and then add 1 to the result. |
| 2848 | */ |
| 2849 | return 1 + (it * (width - 1) / max_change); |
| 2850 | } |
| 2851 | |
| 2852 | static void show_graph(struct strbuf *out, char ch, int cnt, |
| 2853 | const char *set, const char *reset) |
| 2854 | { |
| 2855 | if (cnt <= 0) |
| 2856 | return; |
| 2857 | strbuf_addstr(out, set); |
| 2858 | strbuf_addchars(out, ch, cnt); |
| 2859 | strbuf_addstr(out, reset); |
| 2860 | } |
| 2861 | |
| 2862 | static void fill_print_name(struct diffstat_file *file) |
| 2863 | { |
| 2864 | struct strbuf pname = STRBUF_INIT; |
| 2865 | |
| 2866 | if (file->print_name) |
| 2867 | return; |
| 2868 | |
| 2869 | if (file->is_renamed) |
| 2870 | pprint_rename(&pname, file->from_name, file->name); |
| 2871 | else |
| 2872 | quote_c_style(file->name, &pname, NULL, 0); |
| 2873 | |
| 2874 | if (file->comments) |
| 2875 | strbuf_addf(&pname, " (%s)", file->comments); |
| 2876 | |
| 2877 | file->print_name = strbuf_detach(&pname, NULL); |
| 2878 | } |
| 2879 | |
| 2880 | static void print_stat_summary_inserts_deletes(struct diff_options *options, |
| 2881 | int files, int insertions, int deletions) |
| 2882 | { |
| 2883 | struct strbuf sb = STRBUF_INIT; |
| 2884 | |
| 2885 | if (!files) { |
| 2886 | assert(insertions == 0 && deletions == 0); |
| 2887 | emit_diff_symbol(options, DIFF_SYMBOL_STATS_SUMMARY_NO_FILES, |
| 2888 | NULL, 0, 0); |
| 2889 | return; |
| 2890 | } |
| 2891 | |
| 2892 | strbuf_addf(&sb, |
| 2893 | (files == 1) ? " %d file changed" : " %d files changed", |
| 2894 | files); |
| 2895 | |
| 2896 | /* |
| 2897 | * For binary diff, the caller may want to print "x files |
| 2898 | * changed" with insertions == 0 && deletions == 0. |
| 2899 | * |
| 2900 | * Not omitting "0 insertions(+), 0 deletions(-)" in this case |
| 2901 | * is probably less confusing (i.e skip over "2 files changed |
| 2902 | * but nothing about added/removed lines? Is this a bug in Git?"). |
| 2903 | */ |
| 2904 | if (insertions || deletions == 0) { |
| 2905 | strbuf_addf(&sb, |
| 2906 | (insertions == 1) ? ", %d insertion(+)" : ", %d insertions(+)", |
| 2907 | insertions); |
| 2908 | } |
| 2909 | |
| 2910 | if (deletions || insertions == 0) { |
| 2911 | strbuf_addf(&sb, |
| 2912 | (deletions == 1) ? ", %d deletion(-)" : ", %d deletions(-)", |
| 2913 | deletions); |
| 2914 | } |
| 2915 | strbuf_addch(&sb, '\n'); |
| 2916 | emit_diff_symbol(options, DIFF_SYMBOL_STATS_SUMMARY_INSERTS_DELETES, |
| 2917 | sb.buf, sb.len, 0); |
| 2918 | strbuf_release(&sb); |
| 2919 | } |
| 2920 | |
| 2921 | void print_stat_summary(FILE *fp, int files, |
| 2922 | int insertions, int deletions) |
| 2923 | { |
| 2924 | struct diff_options o; |
| 2925 | memset(&o, 0, sizeof(o)); |
| 2926 | o.file = fp; |
| 2927 | |
| 2928 | print_stat_summary_inserts_deletes(&o, files, insertions, deletions); |
| 2929 | } |
| 2930 | |
| 2931 | /* |
| 2932 | * Like utf8_width(), but guaranteed safe for use in loops that subtract |
| 2933 | * per-character widths: |
| 2934 | * |
| 2935 | * - utf8_width() sets *start to NULL on invalid UTF-8 and returns 0; |
| 2936 | * we restore the pointer and advance by one byte, returning width 1 |
| 2937 | * (matching the strlen()-based fallback in utf8_strwidth()). |
| 2938 | * |
| 2939 | * - utf8_width() returns -1 for control characters; we return 0 |
| 2940 | * (matching utf8_strnwidth() which skips them). |
| 2941 | */ |
| 2942 | static int utf8_ish_width(const char **start) |
| 2943 | { |
| 2944 | const char *old = *start; |
| 2945 | int w = utf8_width(start, NULL); |
| 2946 | if (!*start) { |
| 2947 | *start = old + 1; |
| 2948 | return 1; |
| 2949 | } |
| 2950 | return (w < 0) ? 0 : w; |
| 2951 | } |
| 2952 | |
| 2953 | static void show_stats(struct diffstat_t *data, struct diff_options *options) |
| 2954 | { |
| 2955 | int i, len, add, del, adds = 0, dels = 0; |
| 2956 | uintmax_t max_change = 0, max_len = 0; |
| 2957 | int total_files = data->nr, count; |
| 2958 | int width, name_width, graph_width, number_width = 0, bin_width = 0; |
| 2959 | const char *reset, *add_c, *del_c; |
| 2960 | int extra_shown = 0; |
| 2961 | const char *line_prefix = diff_line_prefix(options); |
| 2962 | struct strbuf out = STRBUF_INIT; |
| 2963 | |
| 2964 | if (data->nr == 0) |
| 2965 | return; |
| 2966 | |
| 2967 | count = options->stat_count ? options->stat_count : data->nr; |
| 2968 | |
| 2969 | reset = diff_get_color_opt(options, DIFF_RESET); |
| 2970 | add_c = diff_get_color_opt(options, DIFF_FILE_NEW); |
| 2971 | del_c = diff_get_color_opt(options, DIFF_FILE_OLD); |
| 2972 | |
| 2973 | /* |
| 2974 | * Find the longest filename and max number of changes |
| 2975 | */ |
| 2976 | for (i = 0; (i < count) && (i < data->nr); i++) { |
| 2977 | struct diffstat_file *file = data->files[i]; |
| 2978 | uintmax_t change = file->added + file->deleted; |
| 2979 | |
| 2980 | if (!file->is_interesting && (change == 0)) { |
| 2981 | count++; /* not shown == room for one more */ |
| 2982 | continue; |
| 2983 | } |
| 2984 | fill_print_name(file); |
| 2985 | len = utf8_strwidth(file->print_name); |
| 2986 | if (max_len < len) |
| 2987 | max_len = len; |
| 2988 | |
| 2989 | if (file->is_unmerged) { |
| 2990 | /* "Unmerged" is 8 characters */ |
| 2991 | bin_width = bin_width < 8 ? 8 : bin_width; |
| 2992 | continue; |
| 2993 | } |
| 2994 | if (file->is_binary) { |
| 2995 | /* "Bin XXX -> YYY bytes" */ |
| 2996 | int w = 14 + decimal_width(file->added) |
| 2997 | + decimal_width(file->deleted); |
| 2998 | bin_width = bin_width < w ? w : bin_width; |
| 2999 | /* Display change counts aligned with "Bin" */ |
| 3000 | number_width = 3; |
| 3001 | continue; |
| 3002 | } |
| 3003 | |
| 3004 | if (max_change < change) |
| 3005 | max_change = change; |
| 3006 | } |
| 3007 | count = i; /* where we can stop scanning in data->files[] */ |
| 3008 | |
| 3009 | /* |
| 3010 | * We have width = stat_width or term_columns() columns total minus the |
| 3011 | * length of line_prefix skipping ANSI escape codes to get the display |
| 3012 | * width (e.g., skip ANSI-colored strings in "log --graph --stat"). |
| 3013 | * We want a maximum of min(max_len, stat_name_width) for the name part. |
| 3014 | * We want a maximum of min(max_change, stat_graph_width) for the +- part. |
| 3015 | * We also need 1 for " " and 4 + decimal_width(max_change) |
| 3016 | * for " | NNNN " and one the empty column at the end, altogether |
| 3017 | * 6 + decimal_width(max_change). |
| 3018 | * |
| 3019 | * If there's not enough space, we will use the smaller of |
| 3020 | * stat_name_width (if set) and 5/8*width for the filename, |
| 3021 | * and the rest for constant elements + graph part, but no more |
| 3022 | * than stat_graph_width for the graph part. |
| 3023 | * (5/8 gives 50 for filename and 30 for the constant parts + graph |
| 3024 | * for the standard terminal size). |
| 3025 | * |
| 3026 | * In other words: stat_width limits the maximum width, and |
| 3027 | * stat_name_width fixes the maximum width of the filename, |
| 3028 | * and is also used to divide available columns if there |
| 3029 | * aren't enough. |
| 3030 | * |
| 3031 | * Binary files are displayed with "Bin XXX -> YYY bytes" |
| 3032 | * instead of the change count and graph. This part is treated |
| 3033 | * similarly to the graph part, except that it is not |
| 3034 | * "scaled". If total width is too small to accommodate the |
| 3035 | * guaranteed minimum width of the filename part and the |
| 3036 | * separators and this message, this message will "overflow" |
| 3037 | * making the line longer than the maximum width. |
| 3038 | */ |
| 3039 | if (options->stat_width == -1) |
| 3040 | width = term_columns() - utf8_strnwidth(line_prefix, strlen(line_prefix), 1); |
| 3041 | else |
| 3042 | width = options->stat_width ? options->stat_width : 80; |
| 3043 | number_width = decimal_width(max_change) > number_width ? |
| 3044 | decimal_width(max_change) : number_width; |
| 3045 | |
| 3046 | if (options->stat_name_width == -1) |
| 3047 | options->stat_name_width = diff_stat_name_width; |
| 3048 | if (options->stat_graph_width == -1) |
| 3049 | options->stat_graph_width = diff_stat_graph_width; |
| 3050 | |
| 3051 | /* |
| 3052 | * Guarantee 3/8*16 == 6 for the graph part |
| 3053 | * and 5/8*16 == 10 for the filename part |
| 3054 | */ |
| 3055 | if (width < 16 + 6 + number_width) |
| 3056 | width = 16 + 6 + number_width; |
| 3057 | |
| 3058 | /* |
| 3059 | * First assign sizes that are wanted, ignoring available width. |
| 3060 | * strlen("Bin XXX -> YYY bytes") == bin_width, and the part |
| 3061 | * starting from "XXX" should fit in graph_width. |
| 3062 | */ |
| 3063 | graph_width = max_change + 4 > bin_width ? max_change : bin_width - 4; |
| 3064 | if (options->stat_graph_width && |
| 3065 | options->stat_graph_width < graph_width) |
| 3066 | graph_width = options->stat_graph_width; |
| 3067 | |
| 3068 | name_width = (options->stat_name_width > 0 && |
| 3069 | options->stat_name_width < max_len) ? |
| 3070 | options->stat_name_width : max_len; |
| 3071 | |
| 3072 | /* |
| 3073 | * Adjust adjustable widths not to exceed maximum width |
| 3074 | */ |
| 3075 | if (name_width + number_width + 6 + graph_width > width) { |
| 3076 | if (graph_width > width * 3/8 - number_width - 6) { |
| 3077 | graph_width = width * 3/8 - number_width - 6; |
| 3078 | if (graph_width < 6) |
| 3079 | graph_width = 6; |
| 3080 | } |
| 3081 | |
| 3082 | if (options->stat_graph_width && |
| 3083 | graph_width > options->stat_graph_width) |
| 3084 | graph_width = options->stat_graph_width; |
| 3085 | if (name_width > width - number_width - 6 - graph_width) |
| 3086 | name_width = width - number_width - 6 - graph_width; |
| 3087 | else |
| 3088 | graph_width = width - number_width - 6 - name_width; |
| 3089 | } |
| 3090 | |
| 3091 | /* |
| 3092 | * From here name_width is the width of the name area, |
| 3093 | * and graph_width is the width of the graph area. |
| 3094 | * max_change is used to scale graph properly. |
| 3095 | */ |
| 3096 | for (i = 0; i < count; i++) { |
| 3097 | const char *prefix = ""; |
| 3098 | struct diffstat_file *file = data->files[i]; |
| 3099 | char *name = file->print_name; |
| 3100 | uintmax_t added = file->added; |
| 3101 | uintmax_t deleted = file->deleted; |
| 3102 | int name_len, padding; |
| 3103 | |
| 3104 | if (!file->is_interesting && (added + deleted == 0)) |
| 3105 | continue; |
| 3106 | |
| 3107 | /* |
| 3108 | * "scale" the filename |
| 3109 | */ |
| 3110 | len = name_width; |
| 3111 | name_len = utf8_strwidth(name); |
| 3112 | if (name_width < name_len) { |
| 3113 | char *slash; |
| 3114 | prefix = "..."; |
| 3115 | len -= 3; |
| 3116 | if (len < 0) |
| 3117 | len = 0; |
| 3118 | |
| 3119 | while (name_len > len && *name) |
| 3120 | name_len -= utf8_ish_width((const char**)&name); |
| 3121 | |
| 3122 | slash = strchr(name, '/'); |
| 3123 | if (slash) |
| 3124 | name = slash; |
| 3125 | } |
| 3126 | padding = len - utf8_strwidth(name); |
| 3127 | if (padding < 0) |
| 3128 | padding = 0; |
| 3129 | |
| 3130 | if (file->is_binary) { |
| 3131 | strbuf_addf(&out, " %s%s%*s | %*s", |
| 3132 | prefix, name, padding, "", |
| 3133 | number_width, "Bin"); |
| 3134 | if (!added && !deleted) { |
| 3135 | strbuf_addch(&out, '\n'); |
| 3136 | emit_diff_symbol(options, DIFF_SYMBOL_STATS_LINE, |
| 3137 | out.buf, out.len, 0); |
| 3138 | strbuf_reset(&out); |
| 3139 | continue; |
| 3140 | } |
| 3141 | strbuf_addf(&out, " %s%"PRIuMAX"%s", |
| 3142 | del_c, deleted, reset); |
| 3143 | strbuf_addstr(&out, " -> "); |
| 3144 | strbuf_addf(&out, "%s%"PRIuMAX"%s", |
| 3145 | add_c, added, reset); |
| 3146 | strbuf_addstr(&out, " bytes\n"); |
| 3147 | emit_diff_symbol(options, DIFF_SYMBOL_STATS_LINE, |
| 3148 | out.buf, out.len, 0); |
| 3149 | strbuf_reset(&out); |
| 3150 | continue; |
| 3151 | } |
| 3152 | else if (file->is_unmerged) { |
| 3153 | strbuf_addf(&out, " %s%s%*s | %*s", |
| 3154 | prefix, name, padding, "", |
| 3155 | number_width, "Unmerged\n"); |
| 3156 | emit_diff_symbol(options, DIFF_SYMBOL_STATS_LINE, |
| 3157 | out.buf, out.len, 0); |
| 3158 | strbuf_reset(&out); |
| 3159 | continue; |
| 3160 | } |
| 3161 | |
| 3162 | /* |
| 3163 | * scale the add/delete |
| 3164 | */ |
| 3165 | add = added; |
| 3166 | del = deleted; |
| 3167 | |
| 3168 | if (graph_width <= max_change) { |
| 3169 | int total = scale_linear(add + del, graph_width, max_change); |
| 3170 | if (total < 2 && add && del) |
| 3171 | /* width >= 2 due to the sanity check */ |
| 3172 | total = 2; |
| 3173 | if (add < del) { |
| 3174 | add = scale_linear(add, graph_width, max_change); |
| 3175 | del = total - add; |
| 3176 | } else { |
| 3177 | del = scale_linear(del, graph_width, max_change); |
| 3178 | add = total - del; |
| 3179 | } |
| 3180 | } |
| 3181 | strbuf_addf(&out, " %s%s%*s | %*"PRIuMAX"%s", |
| 3182 | prefix, name, padding, "", |
| 3183 | number_width, added + deleted, |
| 3184 | added + deleted ? " " : ""); |
| 3185 | show_graph(&out, '+', add, add_c, reset); |
| 3186 | show_graph(&out, '-', del, del_c, reset); |
| 3187 | strbuf_addch(&out, '\n'); |
| 3188 | emit_diff_symbol(options, DIFF_SYMBOL_STATS_LINE, |
| 3189 | out.buf, out.len, 0); |
| 3190 | strbuf_reset(&out); |
| 3191 | } |
| 3192 | |
| 3193 | for (i = 0; i < data->nr; i++) { |
| 3194 | struct diffstat_file *file = data->files[i]; |
| 3195 | uintmax_t added = file->added; |
| 3196 | uintmax_t deleted = file->deleted; |
| 3197 | |
| 3198 | if (file->is_unmerged || |
| 3199 | (!file->is_interesting && (added + deleted == 0))) { |
| 3200 | total_files--; |
| 3201 | continue; |
| 3202 | } |
| 3203 | |
| 3204 | if (!file->is_binary) { |
| 3205 | adds += added; |
| 3206 | dels += deleted; |
| 3207 | } |
| 3208 | if (i < count) |
| 3209 | continue; |
| 3210 | if (!extra_shown) |
| 3211 | emit_diff_symbol(options, |
| 3212 | DIFF_SYMBOL_STATS_SUMMARY_ABBREV, |
| 3213 | NULL, 0, 0); |
| 3214 | extra_shown = 1; |
| 3215 | } |
| 3216 | |
| 3217 | print_stat_summary_inserts_deletes(options, total_files, adds, dels); |
| 3218 | strbuf_release(&out); |
| 3219 | } |
| 3220 | |
| 3221 | static void show_shortstats(struct diffstat_t *data, struct diff_options *options) |
| 3222 | { |
| 3223 | int i, adds = 0, dels = 0, total_files = data->nr; |
| 3224 | |
| 3225 | if (data->nr == 0) |
| 3226 | return; |
| 3227 | |
| 3228 | for (i = 0; i < data->nr; i++) { |
| 3229 | int added = data->files[i]->added; |
| 3230 | int deleted = data->files[i]->deleted; |
| 3231 | |
| 3232 | if (data->files[i]->is_unmerged || |
| 3233 | (!data->files[i]->is_interesting && (added + deleted == 0))) { |
| 3234 | total_files--; |
| 3235 | } else if (!data->files[i]->is_binary) { /* don't count bytes */ |
| 3236 | adds += added; |
| 3237 | dels += deleted; |
| 3238 | } |
| 3239 | } |
| 3240 | print_stat_summary_inserts_deletes(options, total_files, adds, dels); |
| 3241 | } |
| 3242 | |
| 3243 | static void show_numstat(struct diffstat_t *data, struct diff_options *options) |
| 3244 | { |
| 3245 | int i; |
| 3246 | |
| 3247 | if (data->nr == 0) |
| 3248 | return; |
| 3249 | |
| 3250 | for (i = 0; i < data->nr; i++) { |
| 3251 | struct diffstat_file *file = data->files[i]; |
| 3252 | |
| 3253 | fprintf(options->file, "%s", diff_line_prefix(options)); |
| 3254 | |
| 3255 | if (file->is_binary) |
| 3256 | fprintf(options->file, "-\t-\t"); |
| 3257 | else |
| 3258 | fprintf(options->file, |
| 3259 | "%"PRIuMAX"\t%"PRIuMAX"\t", |
| 3260 | file->added, file->deleted); |
| 3261 | if (options->line_termination) { |
| 3262 | fill_print_name(file); |
| 3263 | if (!file->is_renamed) |
| 3264 | write_name_quoted(file->name, options->file, |
| 3265 | options->line_termination); |
| 3266 | else { |
| 3267 | fputs(file->print_name, options->file); |
| 3268 | putc(options->line_termination, options->file); |
| 3269 | } |
| 3270 | } else { |
| 3271 | if (file->is_renamed) { |
| 3272 | putc('\0', options->file); |
| 3273 | write_name_quoted(file->from_name, options->file, '\0'); |
| 3274 | } |
| 3275 | write_name_quoted(file->name, options->file, '\0'); |
| 3276 | } |
| 3277 | } |
| 3278 | } |
| 3279 | |
| 3280 | struct dirstat_file { |
| 3281 | const char *name; |
| 3282 | unsigned long changed; |
| 3283 | }; |
| 3284 | |
| 3285 | struct dirstat_dir { |
| 3286 | struct dirstat_file *files; |
| 3287 | int alloc, nr, permille, cumulative; |
| 3288 | }; |
| 3289 | |
| 3290 | static long gather_dirstat(struct diff_options *opt, struct dirstat_dir *dir, |
| 3291 | unsigned long changed, const char *base, int baselen) |
| 3292 | { |
| 3293 | unsigned long sum_changes = 0; |
| 3294 | unsigned int sources = 0; |
| 3295 | const char *line_prefix = diff_line_prefix(opt); |
| 3296 | |
| 3297 | while (dir->nr) { |
| 3298 | struct dirstat_file *f = dir->files; |
| 3299 | int namelen = strlen(f->name); |
| 3300 | unsigned long changes; |
| 3301 | const char *slash; |
| 3302 | |
| 3303 | if (namelen < baselen) |
| 3304 | break; |
| 3305 | if (memcmp(f->name, base, baselen)) |
| 3306 | break; |
| 3307 | slash = strchr(f->name + baselen, '/'); |
| 3308 | if (slash) { |
| 3309 | int newbaselen = slash + 1 - f->name; |
| 3310 | changes = gather_dirstat(opt, dir, changed, f->name, newbaselen); |
| 3311 | sources++; |
| 3312 | } else { |
| 3313 | changes = f->changed; |
| 3314 | dir->files++; |
| 3315 | dir->nr--; |
| 3316 | sources += 2; |
| 3317 | } |
| 3318 | sum_changes += changes; |
| 3319 | } |
| 3320 | |
| 3321 | /* |
| 3322 | * We don't report dirstat's for |
| 3323 | * - the top level |
| 3324 | * - or cases where everything came from a single directory |
| 3325 | * under this directory (sources == 1). |
| 3326 | */ |
| 3327 | if (baselen && sources != 1) { |
| 3328 | if (sum_changes) { |
| 3329 | int permille = sum_changes * 1000 / changed; |
| 3330 | if (permille >= dir->permille) { |
| 3331 | fprintf(opt->file, "%s%4d.%01d%% %.*s\n", line_prefix, |
| 3332 | permille / 10, permille % 10, baselen, base); |
| 3333 | if (!dir->cumulative) |
| 3334 | return 0; |
| 3335 | } |
| 3336 | } |
| 3337 | } |
| 3338 | return sum_changes; |
| 3339 | } |
| 3340 | |
| 3341 | static int dirstat_compare(const void *_a, const void *_b) |
| 3342 | { |
| 3343 | const struct dirstat_file *a = _a; |
| 3344 | const struct dirstat_file *b = _b; |
| 3345 | return strcmp(a->name, b->name); |
| 3346 | } |
| 3347 | |
| 3348 | static void conclude_dirstat(struct diff_options *options, |
| 3349 | struct dirstat_dir *dir, |
| 3350 | unsigned long changed) |
| 3351 | { |
| 3352 | struct dirstat_file *to_free = dir->files; |
| 3353 | |
| 3354 | if (!changed) { |
| 3355 | /* This can happen even with many files, if everything was renames */ |
| 3356 | ; |
| 3357 | } else { |
| 3358 | /* Show all directories with more than x% of the changes */ |
| 3359 | QSORT(dir->files, dir->nr, dirstat_compare); |
| 3360 | gather_dirstat(options, dir, changed, "", 0); |
| 3361 | } |
| 3362 | |
| 3363 | free(to_free); |
| 3364 | } |
| 3365 | |
| 3366 | static void show_dirstat(struct diff_options *options) |
| 3367 | { |
| 3368 | int i; |
| 3369 | unsigned long changed; |
| 3370 | struct dirstat_dir dir; |
| 3371 | struct diff_queue_struct *q = &diff_queued_diff; |
| 3372 | |
| 3373 | dir.files = NULL; |
| 3374 | dir.alloc = 0; |
| 3375 | dir.nr = 0; |
| 3376 | dir.permille = options->dirstat_permille; |
| 3377 | dir.cumulative = options->flags.dirstat_cumulative; |
| 3378 | |
| 3379 | changed = 0; |
| 3380 | for (i = 0; i < q->nr; i++) { |
| 3381 | struct diff_filepair *p = q->queue[i]; |
| 3382 | const char *name; |
| 3383 | unsigned long copied, added, damage; |
| 3384 | struct diff_populate_filespec_options dpf_options = { |
| 3385 | .check_size_only = 1, |
| 3386 | }; |
| 3387 | |
| 3388 | name = p->two->path ? p->two->path : p->one->path; |
| 3389 | |
| 3390 | if (p->one->oid_valid && p->two->oid_valid && |
| 3391 | oideq(&p->one->oid, &p->two->oid)) { |
| 3392 | /* |
| 3393 | * The SHA1 has not changed, so pre-/post-content is |
| 3394 | * identical. We can therefore skip looking at the |
| 3395 | * file contents altogether. |
| 3396 | */ |
| 3397 | damage = 0; |
| 3398 | goto found_damage; |
| 3399 | } |
| 3400 | |
| 3401 | if (options->flags.dirstat_by_file) { |
| 3402 | /* |
| 3403 | * In --dirstat-by-file mode, we don't really need to |
| 3404 | * look at the actual file contents at all. |
| 3405 | * The fact that the SHA1 changed is enough for us to |
| 3406 | * add this file to the list of results |
| 3407 | * (with each file contributing equal damage). |
| 3408 | */ |
| 3409 | damage = 1; |
| 3410 | goto found_damage; |
| 3411 | } |
| 3412 | |
| 3413 | if (DIFF_FILE_VALID(p->one) && DIFF_FILE_VALID(p->two)) { |
| 3414 | diff_populate_filespec(options->repo, p->one, NULL); |
| 3415 | diff_populate_filespec(options->repo, p->two, NULL); |
| 3416 | diffcore_count_changes(options->repo, |
| 3417 | p->one, p->two, NULL, NULL, |
| 3418 | &copied, &added); |
| 3419 | diff_free_filespec_data(p->one); |
| 3420 | diff_free_filespec_data(p->two); |
| 3421 | } else if (DIFF_FILE_VALID(p->one)) { |
| 3422 | diff_populate_filespec(options->repo, p->one, &dpf_options); |
| 3423 | copied = added = 0; |
| 3424 | diff_free_filespec_data(p->one); |
| 3425 | } else if (DIFF_FILE_VALID(p->two)) { |
| 3426 | diff_populate_filespec(options->repo, p->two, &dpf_options); |
| 3427 | copied = 0; |
| 3428 | added = p->two->size; |
| 3429 | diff_free_filespec_data(p->two); |
| 3430 | } else |
| 3431 | continue; |
| 3432 | |
| 3433 | /* |
| 3434 | * Original minus copied is the removed material, |
| 3435 | * added is the new material. They are both damages |
| 3436 | * made to the preimage. |
| 3437 | * If the resulting damage is zero, we know that |
| 3438 | * diffcore_count_changes() considers the two entries to |
| 3439 | * be identical, but since the oid changed, we |
| 3440 | * know that there must have been _some_ kind of change, |
| 3441 | * so we force all entries to have damage > 0. |
| 3442 | */ |
| 3443 | damage = (p->one->size - copied) + added; |
| 3444 | if (!damage) |
| 3445 | damage = 1; |
| 3446 | |
| 3447 | found_damage: |
| 3448 | ALLOC_GROW(dir.files, dir.nr + 1, dir.alloc); |
| 3449 | dir.files[dir.nr].name = name; |
| 3450 | dir.files[dir.nr].changed = damage; |
| 3451 | changed += damage; |
| 3452 | dir.nr++; |
| 3453 | } |
| 3454 | |
| 3455 | conclude_dirstat(options, &dir, changed); |
| 3456 | } |
| 3457 | |
| 3458 | static void show_dirstat_by_line(struct diffstat_t *data, struct diff_options *options) |
| 3459 | { |
| 3460 | int i; |
| 3461 | unsigned long changed; |
| 3462 | struct dirstat_dir dir; |
| 3463 | |
| 3464 | if (data->nr == 0) |
| 3465 | return; |
| 3466 | |
| 3467 | dir.files = NULL; |
| 3468 | dir.alloc = 0; |
| 3469 | dir.nr = 0; |
| 3470 | dir.permille = options->dirstat_permille; |
| 3471 | dir.cumulative = options->flags.dirstat_cumulative; |
| 3472 | |
| 3473 | changed = 0; |
| 3474 | for (i = 0; i < data->nr; i++) { |
| 3475 | struct diffstat_file *file = data->files[i]; |
| 3476 | unsigned long damage = file->added + file->deleted; |
| 3477 | if (file->is_binary) |
| 3478 | /* |
| 3479 | * binary files counts bytes, not lines. Must find some |
| 3480 | * way to normalize binary bytes vs. textual lines. |
| 3481 | * The following heuristic assumes that there are 64 |
| 3482 | * bytes per "line". |
| 3483 | * This is stupid and ugly, but very cheap... |
| 3484 | */ |
| 3485 | damage = DIV_ROUND_UP(damage, 64); |
| 3486 | ALLOC_GROW(dir.files, dir.nr + 1, dir.alloc); |
| 3487 | dir.files[dir.nr].name = file->name; |
| 3488 | dir.files[dir.nr].changed = damage; |
| 3489 | changed += damage; |
| 3490 | dir.nr++; |
| 3491 | } |
| 3492 | |
| 3493 | conclude_dirstat(options, &dir, changed); |
| 3494 | } |
| 3495 | |
| 3496 | static void free_diffstat_file(struct diffstat_file *f) |
| 3497 | { |
| 3498 | free(f->print_name); |
| 3499 | free(f->name); |
| 3500 | free(f->from_name); |
| 3501 | free(f); |
| 3502 | } |
| 3503 | |
| 3504 | void free_diffstat_info(struct diffstat_t *diffstat) |
| 3505 | { |
| 3506 | int i; |
| 3507 | for (i = 0; i < diffstat->nr; i++) |
| 3508 | free_diffstat_file(diffstat->files[i]); |
| 3509 | free(diffstat->files); |
| 3510 | } |
| 3511 | |
| 3512 | struct checkdiff_t { |
| 3513 | const char *filename; |
| 3514 | int lineno; |
| 3515 | int conflict_marker_size; |
| 3516 | struct diff_options *o; |
| 3517 | unsigned ws_rule; |
| 3518 | unsigned status; |
| 3519 | int last_line_kind; |
| 3520 | }; |
| 3521 | |
| 3522 | static int is_conflict_marker(const char *line, int marker_size, unsigned long len) |
| 3523 | { |
| 3524 | char firstchar; |
| 3525 | int cnt; |
| 3526 | |
| 3527 | if (len < marker_size + 1) |
| 3528 | return 0; |
| 3529 | firstchar = line[0]; |
| 3530 | switch (firstchar) { |
| 3531 | case '=': case '>': case '<': case '|': |
| 3532 | break; |
| 3533 | default: |
| 3534 | return 0; |
| 3535 | } |
| 3536 | for (cnt = 1; cnt < marker_size; cnt++) |
| 3537 | if (line[cnt] != firstchar) |
| 3538 | return 0; |
| 3539 | /* line[1] through line[marker_size-1] are same as firstchar */ |
| 3540 | if (len < marker_size + 1 || !isspace(line[marker_size])) |
| 3541 | return 0; |
| 3542 | return 1; |
| 3543 | } |
| 3544 | |
| 3545 | static void checkdiff_consume_hunk(void *priv, |
| 3546 | long ob UNUSED, long on UNUSED, |
| 3547 | long nb, long nn UNUSED, |
| 3548 | const char *func UNUSED, long funclen UNUSED) |
| 3549 | |
| 3550 | { |
| 3551 | struct checkdiff_t *data = priv; |
| 3552 | data->lineno = nb - 1; |
| 3553 | } |
| 3554 | |
| 3555 | static int checkdiff_consume(void *priv, char *line, unsigned long len) |
| 3556 | { |
| 3557 | struct checkdiff_t *data = priv; |
| 3558 | int last_line_kind; |
| 3559 | int marker_size = data->conflict_marker_size; |
| 3560 | const char *ws = diff_get_color(data->o->use_color, DIFF_WHITESPACE); |
| 3561 | const char *reset = diff_get_color(data->o->use_color, DIFF_RESET); |
| 3562 | const char *set = diff_get_color(data->o->use_color, DIFF_FILE_NEW); |
| 3563 | char *err; |
| 3564 | const char *line_prefix; |
| 3565 | |
| 3566 | assert(data->o); |
| 3567 | line_prefix = diff_line_prefix(data->o); |
| 3568 | |
| 3569 | last_line_kind = data->last_line_kind; |
| 3570 | data->last_line_kind = line[0]; |
| 3571 | if (line[0] == '+') { |
| 3572 | unsigned bad; |
| 3573 | data->lineno++; |
| 3574 | if (is_conflict_marker(line + 1, marker_size, len - 1)) { |
| 3575 | data->status |= 1; |
| 3576 | fprintf(data->o->file, |
| 3577 | "%s%s:%d: leftover conflict marker\n", |
| 3578 | line_prefix, data->filename, data->lineno); |
| 3579 | } |
| 3580 | bad = ws_check(line + 1, len - 1, data->ws_rule); |
| 3581 | if (!bad) |
| 3582 | return 0; |
| 3583 | data->status |= bad; |
| 3584 | err = whitespace_error_string(bad); |
| 3585 | fprintf(data->o->file, "%s%s:%d: %s.\n", |
| 3586 | line_prefix, data->filename, data->lineno, err); |
| 3587 | free(err); |
| 3588 | emit_line(data->o, set, reset, line, 1); |
| 3589 | ws_check_emit(line + 1, len - 1, data->ws_rule, |
| 3590 | data->o->file, set, reset, ws); |
| 3591 | } else if (line[0] == ' ') { |
| 3592 | data->lineno++; |
| 3593 | } else if (line[0] == '\\') { |
| 3594 | /* no newline at the end of the line */ |
| 3595 | if ((data->ws_rule & WS_INCOMPLETE_LINE) && |
| 3596 | (last_line_kind == '+')) { |
| 3597 | unsigned bad = WS_INCOMPLETE_LINE; |
| 3598 | data->status |= bad; |
| 3599 | err = whitespace_error_string(bad); |
| 3600 | fprintf(data->o->file, "%s%s:%d: %s.\n", |
| 3601 | line_prefix, data->filename, data->lineno, err); |
| 3602 | free(err); |
| 3603 | } |
| 3604 | } |
| 3605 | return 0; |
| 3606 | } |
| 3607 | |
| 3608 | static unsigned char *deflate_it(char *data, |
| 3609 | unsigned long size, |
| 3610 | unsigned long *result_size) |
| 3611 | { |
| 3612 | int bound; |
| 3613 | unsigned char *deflated; |
| 3614 | git_zstream stream; |
| 3615 | struct repo_config_values *cfg = repo_config_values(the_repository); |
| 3616 | |
| 3617 | git_deflate_init(&stream, cfg->zlib_compression_level); |
| 3618 | bound = git_deflate_bound(&stream, size); |
| 3619 | deflated = xmalloc(bound); |
| 3620 | stream.next_out = deflated; |
| 3621 | stream.avail_out = bound; |
| 3622 | |
| 3623 | stream.next_in = (unsigned char *)data; |
| 3624 | stream.avail_in = size; |
| 3625 | while (git_deflate(&stream, Z_FINISH) == Z_OK) |
| 3626 | ; /* nothing */ |
| 3627 | git_deflate_end(&stream); |
| 3628 | *result_size = stream.total_out; |
| 3629 | return deflated; |
| 3630 | } |
| 3631 | |
| 3632 | static void emit_binary_diff_body(struct diff_options *o, |
| 3633 | mmfile_t *one, mmfile_t *two) |
| 3634 | { |
| 3635 | void *cp; |
| 3636 | void *delta; |
| 3637 | void *deflated; |
| 3638 | void *data; |
| 3639 | unsigned long orig_size; |
| 3640 | unsigned long delta_size; |
| 3641 | unsigned long deflate_size; |
| 3642 | unsigned long data_size; |
| 3643 | |
| 3644 | /* We could do deflated delta, or we could do just deflated two, |
| 3645 | * whichever is smaller. |
| 3646 | */ |
| 3647 | delta = NULL; |
| 3648 | deflated = deflate_it(two->ptr, two->size, &deflate_size); |
| 3649 | if (one->size && two->size) { |
| 3650 | delta = diff_delta(one->ptr, one->size, |
| 3651 | two->ptr, two->size, |
| 3652 | &delta_size, deflate_size); |
| 3653 | if (delta) { |
| 3654 | void *to_free = delta; |
| 3655 | orig_size = delta_size; |
| 3656 | delta = deflate_it(delta, delta_size, &delta_size); |
| 3657 | free(to_free); |
| 3658 | } |
| 3659 | } |
| 3660 | |
| 3661 | if (delta && delta_size < deflate_size) { |
| 3662 | char *s = xstrfmt("%"PRIuMAX , (uintmax_t)orig_size); |
| 3663 | emit_diff_symbol(o, DIFF_SYMBOL_BINARY_DIFF_HEADER_DELTA, |
| 3664 | s, strlen(s), 0); |
| 3665 | free(s); |
| 3666 | free(deflated); |
| 3667 | data = delta; |
| 3668 | data_size = delta_size; |
| 3669 | } else { |
| 3670 | char *s = xstrfmt("%lu", two->size); |
| 3671 | emit_diff_symbol(o, DIFF_SYMBOL_BINARY_DIFF_HEADER_LITERAL, |
| 3672 | s, strlen(s), 0); |
| 3673 | free(s); |
| 3674 | free(delta); |
| 3675 | data = deflated; |
| 3676 | data_size = deflate_size; |
| 3677 | } |
| 3678 | |
| 3679 | /* emit data encoded in base85 */ |
| 3680 | cp = data; |
| 3681 | while (data_size) { |
| 3682 | int len; |
| 3683 | int bytes = (52 < data_size) ? 52 : data_size; |
| 3684 | char line[71]; |
| 3685 | data_size -= bytes; |
| 3686 | if (bytes <= 26) |
| 3687 | line[0] = bytes + 'A' - 1; |
| 3688 | else |
| 3689 | line[0] = bytes - 26 + 'a' - 1; |
| 3690 | encode_85(line + 1, cp, bytes); |
| 3691 | cp = (char *) cp + bytes; |
| 3692 | |
| 3693 | len = strlen(line); |
| 3694 | line[len++] = '\n'; |
| 3695 | line[len] = '\0'; |
| 3696 | |
| 3697 | emit_diff_symbol(o, DIFF_SYMBOL_BINARY_DIFF_BODY, |
| 3698 | line, len, 0); |
| 3699 | } |
| 3700 | emit_diff_symbol(o, DIFF_SYMBOL_BINARY_DIFF_FOOTER, NULL, 0, 0); |
| 3701 | free(data); |
| 3702 | } |
| 3703 | |
| 3704 | static void emit_binary_diff(struct diff_options *o, |
| 3705 | mmfile_t *one, mmfile_t *two) |
| 3706 | { |
| 3707 | emit_diff_symbol(o, DIFF_SYMBOL_BINARY_DIFF_HEADER, NULL, 0, 0); |
| 3708 | emit_binary_diff_body(o, one, two); |
| 3709 | emit_binary_diff_body(o, two, one); |
| 3710 | } |
| 3711 | |
| 3712 | int diff_filespec_is_binary(struct repository *r, |
| 3713 | struct diff_filespec *one) |
| 3714 | { |
| 3715 | struct diff_populate_filespec_options dpf_options = { |
| 3716 | .check_binary = 1, |
| 3717 | }; |
| 3718 | |
| 3719 | if (one->is_binary == -1) { |
| 3720 | diff_filespec_load_driver(one, r->index); |
| 3721 | if (one->driver->binary != -1) |
| 3722 | one->is_binary = one->driver->binary; |
| 3723 | else { |
| 3724 | if (!one->data && DIFF_FILE_VALID(one)) |
| 3725 | diff_populate_filespec(r, one, &dpf_options); |
| 3726 | if (one->is_binary == -1 && one->data) |
| 3727 | one->is_binary = buffer_is_binary(one->data, |
| 3728 | one->size); |
| 3729 | if (one->is_binary == -1) |
| 3730 | one->is_binary = 0; |
| 3731 | } |
| 3732 | } |
| 3733 | return one->is_binary; |
| 3734 | } |
| 3735 | |
| 3736 | static const struct userdiff_funcname * |
| 3737 | diff_funcname_pattern(struct diff_options *o, struct diff_filespec *one) |
| 3738 | { |
| 3739 | diff_filespec_load_driver(one, o->repo->index); |
| 3740 | return one->driver->funcname.pattern ? &one->driver->funcname : NULL; |
| 3741 | } |
| 3742 | |
| 3743 | void diff_set_mnemonic_prefix(struct diff_options *options, const char *a, const char *b) |
| 3744 | { |
| 3745 | if (!options->a_prefix) |
| 3746 | options->a_prefix = a; |
| 3747 | if (!options->b_prefix) |
| 3748 | options->b_prefix = b; |
| 3749 | } |
| 3750 | |
| 3751 | void diff_set_noprefix(struct diff_options *options) |
| 3752 | { |
| 3753 | options->a_prefix = options->b_prefix = ""; |
| 3754 | } |
| 3755 | |
| 3756 | void diff_set_default_prefix(struct diff_options *options) |
| 3757 | { |
| 3758 | options->a_prefix = diff_src_prefix ? diff_src_prefix : "a/"; |
| 3759 | options->b_prefix = diff_dst_prefix ? diff_dst_prefix : "b/"; |
| 3760 | } |
| 3761 | |
| 3762 | struct userdiff_driver *get_textconv(struct repository *r, |
| 3763 | struct diff_filespec *one) |
| 3764 | { |
| 3765 | if (!DIFF_FILE_VALID(one)) |
| 3766 | return NULL; |
| 3767 | |
| 3768 | diff_filespec_load_driver(one, r->index); |
| 3769 | return userdiff_get_textconv(r, one->driver); |
| 3770 | } |
| 3771 | |
| 3772 | static struct string_list *additional_headers(struct diff_options *o, |
| 3773 | const char *path) |
| 3774 | { |
| 3775 | if (!o->additional_path_headers) |
| 3776 | return NULL; |
| 3777 | return strmap_get(o->additional_path_headers, path); |
| 3778 | } |
| 3779 | |
| 3780 | static void add_formatted_header(struct strbuf *msg, |
| 3781 | const char *header, |
| 3782 | const char *line_prefix, |
| 3783 | const char *meta, |
| 3784 | const char *reset) |
| 3785 | { |
| 3786 | const char *next, *newline; |
| 3787 | |
| 3788 | for (next = header; *next; next = newline) { |
| 3789 | newline = strchrnul(next, '\n'); |
| 3790 | strbuf_addf(msg, "%s%s%.*s%s\n", line_prefix, meta, |
| 3791 | (int)(newline - next), next, reset); |
| 3792 | if (*newline) |
| 3793 | newline++; |
| 3794 | } |
| 3795 | } |
| 3796 | |
| 3797 | static void add_formatted_headers(struct strbuf *msg, |
| 3798 | struct string_list *more_headers, |
| 3799 | const char *line_prefix, |
| 3800 | const char *meta, |
| 3801 | const char *reset) |
| 3802 | { |
| 3803 | int i; |
| 3804 | |
| 3805 | for (i = 0; i < more_headers->nr; i++) |
| 3806 | add_formatted_header(msg, more_headers->items[i].string, |
| 3807 | line_prefix, meta, reset); |
| 3808 | } |
| 3809 | |
| 3810 | static int diff_filepair_is_phoney(struct diff_filespec *one, |
| 3811 | struct diff_filespec *two) |
| 3812 | { |
| 3813 | /* |
| 3814 | * This function specifically looks for pairs injected by |
| 3815 | * create_filepairs_for_header_only_notifications(). Such |
| 3816 | * pairs are "phoney" in that they do not represent any |
| 3817 | * content or even mode difference, but were inserted because |
| 3818 | * diff_queued_diff previously had no pair associated with |
| 3819 | * that path but we needed some pair to avoid losing the |
| 3820 | * "remerge CONFLICT" header associated with the path. |
| 3821 | */ |
| 3822 | return !DIFF_FILE_VALID(one) && !DIFF_FILE_VALID(two); |
| 3823 | } |
| 3824 | |
| 3825 | static int set_diff_algorithm(struct diff_options *opts, |
| 3826 | const char *alg) |
| 3827 | { |
| 3828 | long value = parse_algorithm_value(alg); |
| 3829 | |
| 3830 | if (value < 0) |
| 3831 | return -1; |
| 3832 | |
| 3833 | /* clear out previous settings */ |
| 3834 | opts->xdl_opts &= ~XDF_DIFF_ALGORITHM_MASK; |
| 3835 | opts->xdl_opts |= value; |
| 3836 | |
| 3837 | return 0; |
| 3838 | } |
| 3839 | |
| 3840 | static void builtin_diff(const char *name_a, |
| 3841 | const char *name_b, |
| 3842 | struct diff_filespec *one, |
| 3843 | struct diff_filespec *two, |
| 3844 | const char *xfrm_msg, |
| 3845 | int must_show_header, |
| 3846 | struct diff_options *o, |
| 3847 | int complete_rewrite, |
| 3848 | const struct range_set *line_ranges) |
| 3849 | { |
| 3850 | mmfile_t mf1, mf2; |
| 3851 | const char *lbl[2]; |
| 3852 | char *a_one, *b_two; |
| 3853 | const char *meta = diff_get_color_opt(o, DIFF_METAINFO); |
| 3854 | const char *reset = diff_get_color_opt(o, DIFF_RESET); |
| 3855 | const char *a_prefix, *b_prefix; |
| 3856 | struct userdiff_driver *textconv_one = NULL; |
| 3857 | struct userdiff_driver *textconv_two = NULL; |
| 3858 | struct strbuf header = STRBUF_INIT; |
| 3859 | const char *line_prefix = diff_line_prefix(o); |
| 3860 | |
| 3861 | diff_set_mnemonic_prefix(o, "a/", "b/"); |
| 3862 | if (o->flags.reverse_diff) { |
| 3863 | a_prefix = o->b_prefix; |
| 3864 | b_prefix = o->a_prefix; |
| 3865 | } else { |
| 3866 | a_prefix = o->a_prefix; |
| 3867 | b_prefix = o->b_prefix; |
| 3868 | } |
| 3869 | |
| 3870 | if (o->submodule_format == DIFF_SUBMODULE_LOG && |
| 3871 | (!one->mode || S_ISGITLINK(one->mode)) && |
| 3872 | (!two->mode || S_ISGITLINK(two->mode)) && |
| 3873 | (!diff_filepair_is_phoney(one, two))) { |
| 3874 | show_submodule_diff_summary(o, one->path ? one->path : two->path, |
| 3875 | &one->oid, &two->oid, |
| 3876 | two->dirty_submodule); |
| 3877 | o->found_changes = 1; |
| 3878 | return; |
| 3879 | } else if (o->submodule_format == DIFF_SUBMODULE_INLINE_DIFF && |
| 3880 | (!one->mode || S_ISGITLINK(one->mode)) && |
| 3881 | (!two->mode || S_ISGITLINK(two->mode)) && |
| 3882 | (!diff_filepair_is_phoney(one, two))) { |
| 3883 | show_submodule_inline_diff(o, one->path ? one->path : two->path, |
| 3884 | &one->oid, &two->oid, |
| 3885 | two->dirty_submodule); |
| 3886 | o->found_changes = 1; |
| 3887 | return; |
| 3888 | } |
| 3889 | |
| 3890 | if (o->flags.allow_textconv) { |
| 3891 | textconv_one = get_textconv(o->repo, one); |
| 3892 | textconv_two = get_textconv(o->repo, two); |
| 3893 | } |
| 3894 | |
| 3895 | /* Never use a non-valid filename anywhere if at all possible */ |
| 3896 | name_a = DIFF_FILE_VALID(one) ? name_a : name_b; |
| 3897 | name_b = DIFF_FILE_VALID(two) ? name_b : name_a; |
| 3898 | |
| 3899 | a_one = quote_two(a_prefix, name_a + (*name_a == '/')); |
| 3900 | b_two = quote_two(b_prefix, name_b + (*name_b == '/')); |
| 3901 | lbl[0] = DIFF_FILE_VALID(one) ? a_one : "/dev/null"; |
| 3902 | lbl[1] = DIFF_FILE_VALID(two) ? b_two : "/dev/null"; |
| 3903 | if (diff_filepair_is_phoney(one, two)) { |
| 3904 | /* |
| 3905 | * We should only reach this point for pairs generated from |
| 3906 | * create_filepairs_for_header_only_notifications(). For |
| 3907 | * these, we want to avoid the "/dev/null" special casing |
| 3908 | * above, because we do not want such pairs shown as either |
| 3909 | * "new file" or "deleted file" below. |
| 3910 | */ |
| 3911 | lbl[0] = a_one; |
| 3912 | lbl[1] = b_two; |
| 3913 | } |
| 3914 | strbuf_addf(&header, "%s%sdiff --git %s %s%s\n", line_prefix, meta, a_one, b_two, reset); |
| 3915 | if (lbl[0][0] == '/') { |
| 3916 | /* /dev/null */ |
| 3917 | strbuf_addf(&header, "%s%snew file mode %06o%s\n", line_prefix, meta, two->mode, reset); |
| 3918 | if (xfrm_msg) |
| 3919 | strbuf_addstr(&header, xfrm_msg); |
| 3920 | o->found_changes = 1; |
| 3921 | must_show_header = 1; |
| 3922 | } |
| 3923 | else if (lbl[1][0] == '/') { |
| 3924 | strbuf_addf(&header, "%s%sdeleted file mode %06o%s\n", line_prefix, meta, one->mode, reset); |
| 3925 | if (xfrm_msg) |
| 3926 | strbuf_addstr(&header, xfrm_msg); |
| 3927 | o->found_changes = 1; |
| 3928 | must_show_header = 1; |
| 3929 | } |
| 3930 | else { |
| 3931 | if (one->mode != two->mode) { |
| 3932 | strbuf_addf(&header, "%s%sold mode %06o%s\n", line_prefix, meta, one->mode, reset); |
| 3933 | strbuf_addf(&header, "%s%snew mode %06o%s\n", line_prefix, meta, two->mode, reset); |
| 3934 | o->found_changes = 1; |
| 3935 | must_show_header = 1; |
| 3936 | } |
| 3937 | if (xfrm_msg) |
| 3938 | strbuf_addstr(&header, xfrm_msg); |
| 3939 | |
| 3940 | /* |
| 3941 | * we do not run diff between different kind |
| 3942 | * of objects. |
| 3943 | */ |
| 3944 | if ((one->mode ^ two->mode) & S_IFMT) |
| 3945 | goto free_ab_and_return; |
| 3946 | if (complete_rewrite && |
| 3947 | (textconv_one || !diff_filespec_is_binary(o->repo, one)) && |
| 3948 | (textconv_two || !diff_filespec_is_binary(o->repo, two))) { |
| 3949 | emit_diff_symbol(o, DIFF_SYMBOL_HEADER, |
| 3950 | header.buf, header.len, 0); |
| 3951 | strbuf_reset(&header); |
| 3952 | emit_rewrite_diff(name_a, name_b, one, two, |
| 3953 | textconv_one, textconv_two, o); |
| 3954 | o->found_changes = 1; |
| 3955 | goto free_ab_and_return; |
| 3956 | } |
| 3957 | } |
| 3958 | |
| 3959 | if (o->irreversible_delete && lbl[1][0] == '/') { |
| 3960 | emit_diff_symbol(o, DIFF_SYMBOL_HEADER, header.buf, |
| 3961 | header.len, 0); |
| 3962 | strbuf_reset(&header); |
| 3963 | goto free_ab_and_return; |
| 3964 | } else if (!o->flags.text && |
| 3965 | ( (!textconv_one && diff_filespec_is_binary(o->repo, one)) || |
| 3966 | (!textconv_two && diff_filespec_is_binary(o->repo, two)) )) { |
| 3967 | struct strbuf sb = STRBUF_INIT; |
| 3968 | if (!one->data && !two->data && |
| 3969 | S_ISREG(one->mode) && S_ISREG(two->mode) && |
| 3970 | !o->flags.binary) { |
| 3971 | if (oideq(&one->oid, &two->oid)) { |
| 3972 | if (must_show_header) |
| 3973 | emit_diff_symbol(o, DIFF_SYMBOL_HEADER, |
| 3974 | header.buf, header.len, |
| 3975 | 0); |
| 3976 | goto free_ab_and_return; |
| 3977 | } |
| 3978 | emit_diff_symbol(o, DIFF_SYMBOL_HEADER, |
| 3979 | header.buf, header.len, 0); |
| 3980 | strbuf_addf(&sb, "%sBinary files %s and %s differ\n", |
| 3981 | diff_line_prefix(o), lbl[0], lbl[1]); |
| 3982 | emit_diff_symbol(o, DIFF_SYMBOL_BINARY_FILES, |
| 3983 | sb.buf, sb.len, 0); |
| 3984 | strbuf_release(&sb); |
| 3985 | o->found_changes = 1; |
| 3986 | goto free_ab_and_return; |
| 3987 | } |
| 3988 | if (fill_mmfile(o->repo, &mf1, one) < 0 || |
| 3989 | fill_mmfile(o->repo, &mf2, two) < 0) |
| 3990 | die("unable to read files to diff"); |
| 3991 | /* Quite common confusing case */ |
| 3992 | if (mf1.size == mf2.size && |
| 3993 | !memcmp(mf1.ptr, mf2.ptr, mf1.size)) { |
| 3994 | if (must_show_header) |
| 3995 | emit_diff_symbol(o, DIFF_SYMBOL_HEADER, |
| 3996 | header.buf, header.len, 0); |
| 3997 | goto free_ab_and_return; |
| 3998 | } |
| 3999 | emit_diff_symbol(o, DIFF_SYMBOL_HEADER, header.buf, header.len, 0); |
| 4000 | strbuf_reset(&header); |
| 4001 | if (o->flags.binary) |
| 4002 | emit_binary_diff(o, &mf1, &mf2); |
| 4003 | else { |
| 4004 | strbuf_addf(&sb, "%sBinary files %s and %s differ\n", |
| 4005 | diff_line_prefix(o), lbl[0], lbl[1]); |
| 4006 | emit_diff_symbol(o, DIFF_SYMBOL_BINARY_FILES, |
| 4007 | sb.buf, sb.len, 0); |
| 4008 | strbuf_release(&sb); |
| 4009 | } |
| 4010 | o->found_changes = 1; |
| 4011 | } else { |
| 4012 | /* Crazy xdl interfaces.. */ |
| 4013 | const char *diffopts; |
| 4014 | const char *v; |
| 4015 | xpparam_t xpp; |
| 4016 | xdemitconf_t xecfg; |
| 4017 | struct emit_callback ecbdata; |
| 4018 | unsigned ws_rule; |
| 4019 | const struct userdiff_funcname *pe; |
| 4020 | |
| 4021 | if (must_show_header) { |
| 4022 | emit_diff_symbol(o, DIFF_SYMBOL_HEADER, |
| 4023 | header.buf, header.len, 0); |
| 4024 | strbuf_reset(&header); |
| 4025 | } |
| 4026 | |
| 4027 | mf1.size = fill_textconv(o->repo, textconv_one, one, &mf1.ptr); |
| 4028 | mf2.size = fill_textconv(o->repo, textconv_two, two, &mf2.ptr); |
| 4029 | |
| 4030 | ws_rule = whitespace_rule(o->repo->index, name_b); |
| 4031 | |
| 4032 | /* symlink being an incomplete line is not a news */ |
| 4033 | if (DIFF_FILE_VALID(two) && S_ISLNK(two->mode)) |
| 4034 | ws_rule &= ~WS_INCOMPLETE_LINE; |
| 4035 | |
| 4036 | pe = diff_funcname_pattern(o, one); |
| 4037 | if (!pe) |
| 4038 | pe = diff_funcname_pattern(o, two); |
| 4039 | |
| 4040 | memset(&xpp, 0, sizeof(xpp)); |
| 4041 | memset(&xecfg, 0, sizeof(xecfg)); |
| 4042 | memset(&ecbdata, 0, sizeof(ecbdata)); |
| 4043 | if (o->flags.suppress_diff_headers) |
| 4044 | lbl[0] = NULL; |
| 4045 | ecbdata.label_path = lbl; |
| 4046 | ecbdata.color_diff = o->use_color; |
| 4047 | ecbdata.ws_rule = ws_rule; |
| 4048 | if (ecbdata.ws_rule & WS_BLANK_AT_EOF) |
| 4049 | check_blank_at_eof(&mf1, &mf2, &ecbdata); |
| 4050 | ecbdata.opt = o; |
| 4051 | if (header.len && !o->flags.suppress_diff_headers) |
| 4052 | ecbdata.header = &header; |
| 4053 | xpp.flags = o->xdl_opts; |
| 4054 | xpp.ignore_regex = o->ignore_regex; |
| 4055 | xpp.ignore_regex_nr = o->ignore_regex_nr; |
| 4056 | xpp.anchors = o->anchors; |
| 4057 | xpp.anchors_nr = o->anchors_nr; |
| 4058 | xecfg.ctxlen = o->context; |
| 4059 | xecfg.interhunkctxlen = o->interhunkcontext; |
| 4060 | xecfg.flags = XDL_EMIT_FUNCNAMES; |
| 4061 | if (o->flags.funccontext) |
| 4062 | xecfg.flags |= XDL_EMIT_FUNCCONTEXT; |
| 4063 | if (pe) |
| 4064 | xdiff_set_find_func(&xecfg, pe->pattern, pe->cflags); |
| 4065 | |
| 4066 | diffopts = getenv("GIT_DIFF_OPTS"); |
| 4067 | if (!diffopts) |
| 4068 | ; |
| 4069 | else if (skip_prefix(diffopts, "--unified=", &v)) |
| 4070 | xecfg.ctxlen = strtoul(v, NULL, 10); |
| 4071 | else if (skip_prefix(diffopts, "-u", &v)) |
| 4072 | xecfg.ctxlen = strtoul(v, NULL, 10); |
| 4073 | |
| 4074 | if (o->word_diff) |
| 4075 | init_diff_words_data(&ecbdata, o, one, two); |
| 4076 | if (!o->file) { |
| 4077 | /* |
| 4078 | * Unlike the normal output case, we need to ignore the |
| 4079 | * return value from xdi_diff_outf() here, because |
| 4080 | * xdi_diff_outf() takes non-zero return from its |
| 4081 | * callback function as a sign of error and returns |
| 4082 | * early (which is why we return non-zero from our |
| 4083 | * callback, quick_consume()). Unfortunately, |
| 4084 | * xdi_diff_outf() signals an error by returning |
| 4085 | * non-zero. |
| 4086 | */ |
| 4087 | xdi_diff_outf(&mf1, &mf2, NULL, quick_consume, |
| 4088 | &ecbdata, &xpp, &xecfg); |
| 4089 | } else if (line_ranges) { |
| 4090 | struct line_range_callback lr_state; |
| 4091 | unsigned int i; |
| 4092 | long max_span = 0; |
| 4093 | |
| 4094 | memset(&lr_state, 0, sizeof(lr_state)); |
| 4095 | lr_state.orig_line_fn = fn_out_consume; |
| 4096 | lr_state.orig_cb_data = &ecbdata; |
| 4097 | lr_state.ranges = line_ranges; |
| 4098 | strbuf_init(&lr_state.rhunk, 0); |
| 4099 | strbuf_init(&lr_state.pending_rm, 0); |
| 4100 | |
| 4101 | /* |
| 4102 | * Inflate ctxlen so that all changes within |
| 4103 | * any single range are merged into one xdiff |
| 4104 | * hunk and the inter-change context is emitted. |
| 4105 | * The callback clips back to range boundaries. |
| 4106 | * |
| 4107 | * The optimal ctxlen depends on where changes |
| 4108 | * fall within the range, which is only known |
| 4109 | * after xdiff runs; the max range span is the |
| 4110 | * upper bound that guarantees correctness in a |
| 4111 | * single pass. |
| 4112 | */ |
| 4113 | for (i = 0; i < line_ranges->nr; i++) { |
| 4114 | long span = line_ranges->ranges[i].end - |
| 4115 | line_ranges->ranges[i].start; |
| 4116 | if (span > max_span) |
| 4117 | max_span = span; |
| 4118 | } |
| 4119 | if (max_span > xecfg.ctxlen) |
| 4120 | xecfg.ctxlen = max_span; |
| 4121 | |
| 4122 | if (xdi_diff_outf(&mf1, &mf2, |
| 4123 | line_range_hunk_fn, |
| 4124 | line_range_line_fn, |
| 4125 | &lr_state, &xpp, &xecfg)) |
| 4126 | die("unable to generate diff for %s", |
| 4127 | one->path); |
| 4128 | |
| 4129 | flush_rhunk(&lr_state); |
| 4130 | if (lr_state.ret) |
| 4131 | die("unable to generate diff for %s", |
| 4132 | one->path); |
| 4133 | strbuf_release(&lr_state.rhunk); |
| 4134 | strbuf_release(&lr_state.pending_rm); |
| 4135 | } else if (xdi_diff_outf(&mf1, &mf2, NULL, fn_out_consume, |
| 4136 | &ecbdata, &xpp, &xecfg)) |
| 4137 | die("unable to generate diff for %s", one->path); |
| 4138 | if (o->word_diff) |
| 4139 | free_diff_words_data(&ecbdata); |
| 4140 | if (textconv_one) |
| 4141 | free(mf1.ptr); |
| 4142 | if (textconv_two) |
| 4143 | free(mf2.ptr); |
| 4144 | xdiff_clear_find_func(&xecfg); |
| 4145 | } |
| 4146 | |
| 4147 | free_ab_and_return: |
| 4148 | strbuf_release(&header); |
| 4149 | diff_free_filespec_data(one); |
| 4150 | diff_free_filespec_data(two); |
| 4151 | free(a_one); |
| 4152 | free(b_two); |
| 4153 | return; |
| 4154 | } |
| 4155 | |
| 4156 | static const char *get_compact_summary(const struct diff_filepair *p, int is_renamed) |
| 4157 | { |
| 4158 | if (!is_renamed) { |
| 4159 | if (p->status == DIFF_STATUS_ADDED) { |
| 4160 | if (S_ISLNK(p->two->mode)) |
| 4161 | return "new +l"; |
| 4162 | else if ((p->two->mode & 0777) == 0755) |
| 4163 | return "new +x"; |
| 4164 | else |
| 4165 | return "new"; |
| 4166 | } else if (p->status == DIFF_STATUS_DELETED) |
| 4167 | return "gone"; |
| 4168 | } |
| 4169 | if (S_ISLNK(p->one->mode) && !S_ISLNK(p->two->mode)) |
| 4170 | return "mode -l"; |
| 4171 | else if (!S_ISLNK(p->one->mode) && S_ISLNK(p->two->mode)) |
| 4172 | return "mode +l"; |
| 4173 | else if ((p->one->mode & 0777) == 0644 && |
| 4174 | (p->two->mode & 0777) == 0755) |
| 4175 | return "mode +x"; |
| 4176 | else if ((p->one->mode & 0777) == 0755 && |
| 4177 | (p->two->mode & 0777) == 0644) |
| 4178 | return "mode -x"; |
| 4179 | return NULL; |
| 4180 | } |
| 4181 | |
| 4182 | static void builtin_diffstat(const char *name_a, const char *name_b, |
| 4183 | struct diff_filespec *one, |
| 4184 | struct diff_filespec *two, |
| 4185 | struct diffstat_t *diffstat, |
| 4186 | struct diff_options *o, |
| 4187 | struct diff_filepair *p) |
| 4188 | { |
| 4189 | mmfile_t mf1, mf2; |
| 4190 | struct diffstat_file *data; |
| 4191 | int may_differ; |
| 4192 | int complete_rewrite = 0; |
| 4193 | |
| 4194 | if (!DIFF_PAIR_UNMERGED(p)) { |
| 4195 | if (p->status == DIFF_STATUS_MODIFIED && p->score) |
| 4196 | complete_rewrite = 1; |
| 4197 | } |
| 4198 | |
| 4199 | data = diffstat_add(diffstat, name_a, name_b); |
| 4200 | data->is_interesting = p->status != DIFF_STATUS_UNKNOWN; |
| 4201 | if (o->flags.stat_with_summary) |
| 4202 | data->comments = get_compact_summary(p, data->is_renamed); |
| 4203 | |
| 4204 | if (!one || !two) { |
| 4205 | data->is_unmerged = 1; |
| 4206 | return; |
| 4207 | } |
| 4208 | |
| 4209 | /* saves some reads if true, not a guarantee of diff outcome */ |
| 4210 | may_differ = !(one->oid_valid && two->oid_valid && |
| 4211 | oideq(&one->oid, &two->oid)); |
| 4212 | |
| 4213 | if (diff_filespec_is_binary(o->repo, one) || |
| 4214 | diff_filespec_is_binary(o->repo, two)) { |
| 4215 | data->is_binary = 1; |
| 4216 | if (!may_differ) { |
| 4217 | data->added = 0; |
| 4218 | data->deleted = 0; |
| 4219 | } else { |
| 4220 | data->added = diff_filespec_size(o->repo, two); |
| 4221 | data->deleted = diff_filespec_size(o->repo, one); |
| 4222 | } |
| 4223 | } |
| 4224 | |
| 4225 | else if (complete_rewrite) { |
| 4226 | diff_populate_filespec(o->repo, one, NULL); |
| 4227 | diff_populate_filespec(o->repo, two, NULL); |
| 4228 | data->deleted = count_lines(one->data, one->size); |
| 4229 | data->added = count_lines(two->data, two->size); |
| 4230 | } |
| 4231 | |
| 4232 | else if (may_differ) { |
| 4233 | /* Crazy xdl interfaces.. */ |
| 4234 | xpparam_t xpp; |
| 4235 | xdemitconf_t xecfg; |
| 4236 | |
| 4237 | if (fill_mmfile(o->repo, &mf1, one) < 0 || |
| 4238 | fill_mmfile(o->repo, &mf2, two) < 0) |
| 4239 | die("unable to read files to diff"); |
| 4240 | |
| 4241 | memset(&xpp, 0, sizeof(xpp)); |
| 4242 | memset(&xecfg, 0, sizeof(xecfg)); |
| 4243 | xpp.flags = o->xdl_opts; |
| 4244 | xpp.ignore_regex = o->ignore_regex; |
| 4245 | xpp.ignore_regex_nr = o->ignore_regex_nr; |
| 4246 | xpp.anchors = o->anchors; |
| 4247 | xpp.anchors_nr = o->anchors_nr; |
| 4248 | xecfg.ctxlen = o->context; |
| 4249 | xecfg.interhunkctxlen = o->interhunkcontext; |
| 4250 | xecfg.flags = XDL_EMIT_NO_HUNK_HDR; |
| 4251 | if (xdi_diff_outf(&mf1, &mf2, NULL, |
| 4252 | diffstat_consume, diffstat, &xpp, &xecfg)) |
| 4253 | die("unable to generate diffstat for %s", one->path); |
| 4254 | |
| 4255 | if (DIFF_FILE_VALID(one) && DIFF_FILE_VALID(two)) { |
| 4256 | struct diffstat_file *file = |
| 4257 | diffstat->files[diffstat->nr - 1]; |
| 4258 | /* |
| 4259 | * Omit diffstats of modified files where nothing changed. |
| 4260 | * Even if may_differ, this might be the case due to |
| 4261 | * ignoring whitespace changes, etc. |
| 4262 | * |
| 4263 | * But note that we special-case additions, deletions, |
| 4264 | * renames, and mode changes as adding an empty file, |
| 4265 | * for example is still of interest. |
| 4266 | */ |
| 4267 | if ((p->status == DIFF_STATUS_MODIFIED) |
| 4268 | && !file->added |
| 4269 | && !file->deleted |
| 4270 | && one->mode == two->mode) { |
| 4271 | free_diffstat_file(file); |
| 4272 | diffstat->nr--; |
| 4273 | } |
| 4274 | } |
| 4275 | } |
| 4276 | |
| 4277 | diff_free_filespec_data(one); |
| 4278 | diff_free_filespec_data(two); |
| 4279 | } |
| 4280 | |
| 4281 | static void builtin_checkdiff(const char *name_a, const char *name_b, |
| 4282 | const char *attr_path, |
| 4283 | struct diff_filespec *one, |
| 4284 | struct diff_filespec *two, |
| 4285 | struct diff_options *o) |
| 4286 | { |
| 4287 | mmfile_t mf1, mf2; |
| 4288 | struct checkdiff_t data; |
| 4289 | |
| 4290 | if (!two) |
| 4291 | return; |
| 4292 | |
| 4293 | memset(&data, 0, sizeof(data)); |
| 4294 | data.filename = name_b ? name_b : name_a; |
| 4295 | data.lineno = 0; |
| 4296 | data.o = o; |
| 4297 | data.ws_rule = whitespace_rule(o->repo->index, attr_path); |
| 4298 | data.conflict_marker_size = ll_merge_marker_size(o->repo->index, attr_path); |
| 4299 | |
| 4300 | /* symlink being an incomplete line is not a news */ |
| 4301 | if (DIFF_FILE_VALID(two) && S_ISLNK(two->mode)) |
| 4302 | data.ws_rule &= ~WS_INCOMPLETE_LINE; |
| 4303 | |
| 4304 | if (fill_mmfile(o->repo, &mf1, one) < 0 || |
| 4305 | fill_mmfile(o->repo, &mf2, two) < 0) |
| 4306 | die("unable to read files to diff"); |
| 4307 | |
| 4308 | /* |
| 4309 | * All the other codepaths check both sides, but not checking |
| 4310 | * the "old" side here is deliberate. We are checking the newly |
| 4311 | * introduced changes, and as long as the "new" side is text, we |
| 4312 | * can and should check what it introduces. |
| 4313 | */ |
| 4314 | if (diff_filespec_is_binary(o->repo, two)) |
| 4315 | goto free_and_return; |
| 4316 | else { |
| 4317 | /* Crazy xdl interfaces.. */ |
| 4318 | xpparam_t xpp; |
| 4319 | xdemitconf_t xecfg; |
| 4320 | |
| 4321 | memset(&xpp, 0, sizeof(xpp)); |
| 4322 | memset(&xecfg, 0, sizeof(xecfg)); |
| 4323 | xecfg.ctxlen = 1; /* at least one context line */ |
| 4324 | xpp.flags = 0; |
| 4325 | if (xdi_diff_outf(&mf1, &mf2, checkdiff_consume_hunk, |
| 4326 | checkdiff_consume, &data, |
| 4327 | &xpp, &xecfg)) |
| 4328 | die("unable to generate checkdiff for %s", one->path); |
| 4329 | |
| 4330 | if (data.ws_rule & WS_BLANK_AT_EOF) { |
| 4331 | struct emit_callback ecbdata; |
| 4332 | int blank_at_eof; |
| 4333 | |
| 4334 | ecbdata.ws_rule = data.ws_rule; |
| 4335 | check_blank_at_eof(&mf1, &mf2, &ecbdata); |
| 4336 | blank_at_eof = ecbdata.blank_at_eof_in_postimage; |
| 4337 | |
| 4338 | if (blank_at_eof) { |
| 4339 | static char *err; |
| 4340 | if (!err) |
| 4341 | err = whitespace_error_string(WS_BLANK_AT_EOF); |
| 4342 | fprintf(o->file, "%s:%d: %s.\n", |
| 4343 | data.filename, blank_at_eof, err); |
| 4344 | data.status = 1; /* report errors */ |
| 4345 | } |
| 4346 | } |
| 4347 | } |
| 4348 | free_and_return: |
| 4349 | diff_free_filespec_data(one); |
| 4350 | diff_free_filespec_data(two); |
| 4351 | if (data.status) |
| 4352 | o->flags.check_failed = 1; |
| 4353 | } |
| 4354 | |
| 4355 | struct diff_filespec *alloc_filespec(const char *path) |
| 4356 | { |
| 4357 | struct diff_filespec *spec; |
| 4358 | |
| 4359 | FLEXPTR_ALLOC_STR(spec, path, path); |
| 4360 | spec->count = 1; |
| 4361 | spec->is_binary = -1; |
| 4362 | return spec; |
| 4363 | } |
| 4364 | |
| 4365 | void free_filespec(struct diff_filespec *spec) |
| 4366 | { |
| 4367 | if (!--spec->count) { |
| 4368 | diff_free_filespec_data(spec); |
| 4369 | free(spec); |
| 4370 | } |
| 4371 | } |
| 4372 | |
| 4373 | void fill_filespec(struct diff_filespec *spec, const struct object_id *oid, |
| 4374 | int oid_valid, unsigned short mode) |
| 4375 | { |
| 4376 | if (mode) { |
| 4377 | spec->mode = canon_mode(mode); |
| 4378 | oidcpy(&spec->oid, oid); |
| 4379 | spec->oid_valid = oid_valid; |
| 4380 | } |
| 4381 | } |
| 4382 | |
| 4383 | /* |
| 4384 | * Given a name and sha1 pair, if the index tells us the file in |
| 4385 | * the work tree has that object contents, return true, so that |
| 4386 | * prepare_temp_file() does not have to inflate and extract. |
| 4387 | */ |
| 4388 | static int reuse_worktree_file(struct index_state *istate, |
| 4389 | const char *name, |
| 4390 | const struct object_id *oid, |
| 4391 | int want_file) |
| 4392 | { |
| 4393 | const struct cache_entry *ce; |
| 4394 | struct stat st; |
| 4395 | int pos, len; |
| 4396 | |
| 4397 | /* |
| 4398 | * We do not read the cache ourselves here, because the |
| 4399 | * benchmark with my previous version that always reads cache |
| 4400 | * shows that it makes things worse for diff-tree comparing |
| 4401 | * two linux-2.6 kernel trees in an already checked out work |
| 4402 | * tree. This is because most diff-tree comparisons deal with |
| 4403 | * only a small number of files, while reading the cache is |
| 4404 | * expensive for a large project, and its cost outweighs the |
| 4405 | * savings we get by not inflating the object to a temporary |
| 4406 | * file. Practically, this code only helps when we are used |
| 4407 | * by diff-cache --cached, which does read the cache before |
| 4408 | * calling us. |
| 4409 | */ |
| 4410 | if (!istate->cache) |
| 4411 | return 0; |
| 4412 | |
| 4413 | /* We want to avoid the working directory if our caller |
| 4414 | * doesn't need the data in a normal file, this system |
| 4415 | * is rather slow with its stat/open/mmap/close syscalls, |
| 4416 | * and the object is contained in a pack file. The pack |
| 4417 | * is probably already open and will be faster to obtain |
| 4418 | * the data through than the working directory. Loose |
| 4419 | * objects however would tend to be slower as they need |
| 4420 | * to be individually opened and inflated. |
| 4421 | */ |
| 4422 | if (!FAST_WORKING_DIRECTORY && !want_file && |
| 4423 | has_object_pack(istate->repo, oid)) |
| 4424 | return 0; |
| 4425 | |
| 4426 | /* |
| 4427 | * Similarly, if we'd have to convert the file contents anyway, that |
| 4428 | * makes the optimization not worthwhile. |
| 4429 | */ |
| 4430 | if (!want_file && would_convert_to_git(istate, name)) |
| 4431 | return 0; |
| 4432 | |
| 4433 | /* |
| 4434 | * If this path does not match our sparse-checkout definition, |
| 4435 | * then the file will not be in the working directory. |
| 4436 | */ |
| 4437 | if (!path_in_sparse_checkout(name, istate)) |
| 4438 | return 0; |
| 4439 | |
| 4440 | len = strlen(name); |
| 4441 | pos = index_name_pos(istate, name, len); |
| 4442 | if (pos < 0) |
| 4443 | return 0; |
| 4444 | ce = istate->cache[pos]; |
| 4445 | |
| 4446 | /* |
| 4447 | * This is not the sha1 we are looking for, or |
| 4448 | * unreusable because it is not a regular file. |
| 4449 | */ |
| 4450 | if (!oideq(oid, &ce->oid) || !S_ISREG(ce->ce_mode)) |
| 4451 | return 0; |
| 4452 | |
| 4453 | /* |
| 4454 | * If ce is marked as "assume unchanged", there is no |
| 4455 | * guarantee that work tree matches what we are looking for. |
| 4456 | */ |
| 4457 | if ((ce->ce_flags & CE_VALID) || ce_skip_worktree(ce)) |
| 4458 | return 0; |
| 4459 | |
| 4460 | /* |
| 4461 | * If ce matches the file in the work tree, we can reuse it. |
| 4462 | */ |
| 4463 | if (ce_uptodate(ce) || |
| 4464 | (!lstat(name, &st) && !ie_match_stat(istate, ce, &st, 0))) |
| 4465 | return 1; |
| 4466 | |
| 4467 | return 0; |
| 4468 | } |
| 4469 | |
| 4470 | static int diff_populate_gitlink(struct diff_filespec *s, int size_only) |
| 4471 | { |
| 4472 | struct strbuf buf = STRBUF_INIT; |
| 4473 | const char *dirty = ""; |
| 4474 | |
| 4475 | /* Are we looking at the work tree? */ |
| 4476 | if (s->dirty_submodule) |
| 4477 | dirty = "-dirty"; |
| 4478 | |
| 4479 | strbuf_addf(&buf, "Subproject commit %s%s\n", |
| 4480 | oid_to_hex(&s->oid), dirty); |
| 4481 | s->size = buf.len; |
| 4482 | if (size_only) { |
| 4483 | s->data = NULL; |
| 4484 | strbuf_release(&buf); |
| 4485 | } else { |
| 4486 | s->data = strbuf_detach(&buf, NULL); |
| 4487 | s->should_free = 1; |
| 4488 | } |
| 4489 | return 0; |
| 4490 | } |
| 4491 | |
| 4492 | /* |
| 4493 | * While doing rename detection and pickaxe operation, we may need to |
| 4494 | * grab the data for the blob (or file) for our own in-core comparison. |
| 4495 | * diff_filespec has data and size fields for this purpose. |
| 4496 | */ |
| 4497 | int diff_populate_filespec(struct repository *r, |
| 4498 | struct diff_filespec *s, |
| 4499 | const struct diff_populate_filespec_options *options) |
| 4500 | { |
| 4501 | int size_only = options ? options->check_size_only : 0; |
| 4502 | int check_binary = options ? options->check_binary : 0; |
| 4503 | int err = 0; |
| 4504 | int conv_flags = global_conv_flags_eol; |
| 4505 | /* |
| 4506 | * demote FAIL to WARN to allow inspecting the situation |
| 4507 | * instead of refusing. |
| 4508 | */ |
| 4509 | if (conv_flags & CONV_EOL_RNDTRP_DIE) |
| 4510 | conv_flags = CONV_EOL_RNDTRP_WARN; |
| 4511 | |
| 4512 | if (!DIFF_FILE_VALID(s)) |
| 4513 | die("internal error: asking to populate invalid file."); |
| 4514 | if (S_ISDIR(s->mode)) |
| 4515 | return -1; |
| 4516 | |
| 4517 | if (s->data) |
| 4518 | return 0; |
| 4519 | |
| 4520 | if (size_only && 0 < s->size) |
| 4521 | return 0; |
| 4522 | |
| 4523 | if (S_ISGITLINK(s->mode)) |
| 4524 | return diff_populate_gitlink(s, size_only); |
| 4525 | |
| 4526 | if (!s->oid_valid || |
| 4527 | reuse_worktree_file(r->index, s->path, &s->oid, 0)) { |
| 4528 | struct strbuf buf = STRBUF_INIT; |
| 4529 | struct stat st; |
| 4530 | int fd; |
| 4531 | |
| 4532 | if (lstat(s->path, &st) < 0) { |
| 4533 | err_empty: |
| 4534 | err = -1; |
| 4535 | empty: |
| 4536 | s->data = (char *)""; |
| 4537 | s->size = 0; |
| 4538 | return err; |
| 4539 | } |
| 4540 | s->size = xsize_t(st.st_size); |
| 4541 | if (!s->size) |
| 4542 | goto empty; |
| 4543 | if (S_ISLNK(st.st_mode)) { |
| 4544 | struct strbuf sb = STRBUF_INIT; |
| 4545 | |
| 4546 | if (strbuf_readlink(&sb, s->path, s->size)) |
| 4547 | goto err_empty; |
| 4548 | s->size = sb.len; |
| 4549 | s->data = strbuf_detach(&sb, NULL); |
| 4550 | s->should_free = 1; |
| 4551 | return 0; |
| 4552 | } |
| 4553 | |
| 4554 | /* |
| 4555 | * Even if the caller would be happy with getting |
| 4556 | * only the size, we cannot return early at this |
| 4557 | * point if the path requires us to run the content |
| 4558 | * conversion. |
| 4559 | */ |
| 4560 | if (size_only && !would_convert_to_git(r->index, s->path)) |
| 4561 | return 0; |
| 4562 | |
| 4563 | /* |
| 4564 | * Note: this check uses xsize_t(st.st_size) that may |
| 4565 | * not be the true size of the blob after it goes |
| 4566 | * through convert_to_git(). This may not strictly be |
| 4567 | * correct, but the whole point of big_file_threshold |
| 4568 | * and is_binary check being that we want to avoid |
| 4569 | * opening the file and inspecting the contents, this |
| 4570 | * is probably fine. |
| 4571 | */ |
| 4572 | if (check_binary && |
| 4573 | s->size > repo_settings_get_big_file_threshold(the_repository) && |
| 4574 | s->is_binary == -1) { |
| 4575 | s->is_binary = 1; |
| 4576 | return 0; |
| 4577 | } |
| 4578 | fd = open(s->path, O_RDONLY); |
| 4579 | if (fd < 0) |
| 4580 | goto err_empty; |
| 4581 | s->data = xmmap(NULL, s->size, PROT_READ, MAP_PRIVATE, fd, 0); |
| 4582 | close(fd); |
| 4583 | s->should_munmap = 1; |
| 4584 | |
| 4585 | /* |
| 4586 | * Convert from working tree format to canonical git format |
| 4587 | */ |
| 4588 | if (convert_to_git(r->index, s->path, s->data, s->size, &buf, conv_flags)) { |
| 4589 | size_t size = 0; |
| 4590 | munmap(s->data, s->size); |
| 4591 | s->should_munmap = 0; |
| 4592 | s->data = strbuf_detach(&buf, &size); |
| 4593 | s->size = size; |
| 4594 | s->should_free = 1; |
| 4595 | } |
| 4596 | } |
| 4597 | else { |
| 4598 | size_t size_st = 0; |
| 4599 | struct object_info info = { |
| 4600 | .sizep = &size_st |
| 4601 | }; |
| 4602 | |
| 4603 | if (!(size_only || check_binary)) |
| 4604 | /* |
| 4605 | * Set contentp, since there is no chance that merely |
| 4606 | * the size is sufficient. |
| 4607 | */ |
| 4608 | info.contentp = &s->data; |
| 4609 | |
| 4610 | if (options && options->missing_object_cb) { |
| 4611 | if (!odb_read_object_info_extended(r->objects, &s->oid, &info, |
| 4612 | OBJECT_INFO_LOOKUP_REPLACE | |
| 4613 | OBJECT_INFO_SKIP_FETCH_OBJECT)) |
| 4614 | goto object_read; |
| 4615 | options->missing_object_cb(options->missing_object_data); |
| 4616 | } |
| 4617 | if (odb_read_object_info_extended(r->objects, &s->oid, &info, |
| 4618 | OBJECT_INFO_LOOKUP_REPLACE)) |
| 4619 | die("unable to read %s", oid_to_hex(&s->oid)); |
| 4620 | |
| 4621 | object_read: |
| 4622 | s->size = cast_size_t_to_ulong(size_st); |
| 4623 | if (size_only || check_binary) { |
| 4624 | if (size_only) |
| 4625 | return 0; |
| 4626 | if (s->size > repo_settings_get_big_file_threshold(the_repository) && |
| 4627 | s->is_binary == -1) { |
| 4628 | s->is_binary = 1; |
| 4629 | return 0; |
| 4630 | } |
| 4631 | } |
| 4632 | if (!info.contentp) { |
| 4633 | info.contentp = &s->data; |
| 4634 | if (odb_read_object_info_extended(r->objects, &s->oid, &info, |
| 4635 | OBJECT_INFO_LOOKUP_REPLACE)) |
| 4636 | die("unable to read %s", oid_to_hex(&s->oid)); |
| 4637 | s->size = cast_size_t_to_ulong(size_st); |
| 4638 | } |
| 4639 | s->should_free = 1; |
| 4640 | } |
| 4641 | return 0; |
| 4642 | } |
| 4643 | |
| 4644 | void diff_free_filespec_blob(struct diff_filespec *s) |
| 4645 | { |
| 4646 | if (s->should_free) |
| 4647 | free(s->data); |
| 4648 | else if (s->should_munmap) |
| 4649 | munmap(s->data, s->size); |
| 4650 | |
| 4651 | if (s->should_free || s->should_munmap) { |
| 4652 | s->should_free = s->should_munmap = 0; |
| 4653 | s->data = NULL; |
| 4654 | } |
| 4655 | } |
| 4656 | |
| 4657 | void diff_free_filespec_data(struct diff_filespec *s) |
| 4658 | { |
| 4659 | if (!s) |
| 4660 | return; |
| 4661 | |
| 4662 | diff_free_filespec_blob(s); |
| 4663 | FREE_AND_NULL(s->cnt_data); |
| 4664 | } |
| 4665 | |
| 4666 | static void prep_temp_blob(struct index_state *istate, |
| 4667 | const char *path, struct diff_tempfile *temp, |
| 4668 | void *blob, |
| 4669 | unsigned long size, |
| 4670 | const struct object_id *oid, |
| 4671 | int mode) |
| 4672 | { |
| 4673 | struct strbuf buf = STRBUF_INIT; |
| 4674 | char *path_dup = xstrdup(path); |
| 4675 | const char *base = basename(path_dup); |
| 4676 | struct checkout_metadata meta; |
| 4677 | |
| 4678 | init_checkout_metadata(&meta, NULL, NULL, oid); |
| 4679 | |
| 4680 | temp->tempfile = mks_tempfile_dt("git-blob-XXXXXX", base); |
| 4681 | if (!temp->tempfile) |
| 4682 | die_errno("unable to create temp-file"); |
| 4683 | if (convert_to_working_tree(istate, path, |
| 4684 | (const char *)blob, (size_t)size, &buf, &meta)) { |
| 4685 | blob = buf.buf; |
| 4686 | size = buf.len; |
| 4687 | } |
| 4688 | if (write_in_full(temp->tempfile->fd, blob, size) < 0 || |
| 4689 | close_tempfile_gently(temp->tempfile)) |
| 4690 | die_errno("unable to write temp-file"); |
| 4691 | temp->name = get_tempfile_path(temp->tempfile); |
| 4692 | oid_to_hex_r(temp->hex, oid); |
| 4693 | xsnprintf(temp->mode, sizeof(temp->mode), "%06o", mode); |
| 4694 | strbuf_release(&buf); |
| 4695 | free(path_dup); |
| 4696 | } |
| 4697 | |
| 4698 | static struct diff_tempfile *prepare_temp_file(struct repository *r, |
| 4699 | struct diff_filespec *one) |
| 4700 | { |
| 4701 | struct diff_tempfile *temp = claim_diff_tempfile(); |
| 4702 | |
| 4703 | if (!DIFF_FILE_VALID(one)) { |
| 4704 | not_a_valid_file: |
| 4705 | /* A '-' entry produces this for file-2, and |
| 4706 | * a '+' entry produces this for file-1. |
| 4707 | */ |
| 4708 | temp->name = "/dev/null"; |
| 4709 | xsnprintf(temp->hex, sizeof(temp->hex), "."); |
| 4710 | xsnprintf(temp->mode, sizeof(temp->mode), "."); |
| 4711 | return temp; |
| 4712 | } |
| 4713 | |
| 4714 | if (!S_ISGITLINK(one->mode) && |
| 4715 | (!one->oid_valid || |
| 4716 | reuse_worktree_file(r->index, one->path, &one->oid, 1))) { |
| 4717 | struct stat st; |
| 4718 | if (lstat(one->path, &st) < 0) { |
| 4719 | if (errno == ENOENT) |
| 4720 | goto not_a_valid_file; |
| 4721 | die_errno("stat(%s)", one->path); |
| 4722 | } |
| 4723 | if (S_ISLNK(st.st_mode)) { |
| 4724 | struct strbuf sb = STRBUF_INIT; |
| 4725 | if (strbuf_readlink(&sb, one->path, st.st_size) < 0) |
| 4726 | die_errno("readlink(%s)", one->path); |
| 4727 | prep_temp_blob(r->index, one->path, temp, sb.buf, sb.len, |
| 4728 | (one->oid_valid ? |
| 4729 | &one->oid : null_oid(the_hash_algo)), |
| 4730 | (one->oid_valid ? |
| 4731 | one->mode : S_IFLNK)); |
| 4732 | strbuf_release(&sb); |
| 4733 | } |
| 4734 | else { |
| 4735 | /* we can borrow from the file in the work tree */ |
| 4736 | temp->name = one->path; |
| 4737 | if (!one->oid_valid) |
| 4738 | oid_to_hex_r(temp->hex, null_oid(the_hash_algo)); |
| 4739 | else |
| 4740 | oid_to_hex_r(temp->hex, &one->oid); |
| 4741 | /* Even though we may sometimes borrow the |
| 4742 | * contents from the work tree, we always want |
| 4743 | * one->mode. mode is trustworthy even when |
| 4744 | * !(one->oid_valid), as long as |
| 4745 | * DIFF_FILE_VALID(one). |
| 4746 | */ |
| 4747 | xsnprintf(temp->mode, sizeof(temp->mode), "%06o", one->mode); |
| 4748 | } |
| 4749 | return temp; |
| 4750 | } |
| 4751 | else { |
| 4752 | if (diff_populate_filespec(r, one, NULL)) |
| 4753 | die("cannot read data blob for %s", one->path); |
| 4754 | prep_temp_blob(r->index, one->path, temp, |
| 4755 | one->data, one->size, |
| 4756 | &one->oid, one->mode); |
| 4757 | } |
| 4758 | return temp; |
| 4759 | } |
| 4760 | |
| 4761 | static void add_external_diff_name(struct repository *r, |
| 4762 | struct strvec *argv, |
| 4763 | struct diff_filespec *df) |
| 4764 | { |
| 4765 | struct diff_tempfile *temp = prepare_temp_file(r, df); |
| 4766 | strvec_push(argv, temp->name); |
| 4767 | strvec_push(argv, temp->hex); |
| 4768 | strvec_push(argv, temp->mode); |
| 4769 | } |
| 4770 | |
| 4771 | /* An external diff command takes: |
| 4772 | * |
| 4773 | * diff-cmd name infile1 infile1-sha1 infile1-mode \ |
| 4774 | * infile2 infile2-sha1 infile2-mode [ rename-to ] |
| 4775 | * |
| 4776 | */ |
| 4777 | static void run_external_diff(const struct external_diff *pgm, |
| 4778 | const char *name, |
| 4779 | const char *other, |
| 4780 | struct diff_filespec *one, |
| 4781 | struct diff_filespec *two, |
| 4782 | const char *xfrm_msg, |
| 4783 | struct diff_options *o) |
| 4784 | { |
| 4785 | struct child_process cmd = CHILD_PROCESS_INIT; |
| 4786 | struct diff_queue_struct *q = &diff_queued_diff; |
| 4787 | int rc; |
| 4788 | |
| 4789 | /* |
| 4790 | * Trivial equality is handled by diff_unmodified_pair() before |
| 4791 | * we get here. If we don't need to show the diff and the |
| 4792 | * external diff program lacks the ability to tell us whether |
| 4793 | * it's empty then we consider it non-empty without even asking. |
| 4794 | */ |
| 4795 | if (!pgm->trust_exit_code && !o->file) { |
| 4796 | o->found_changes = 1; |
| 4797 | return; |
| 4798 | } |
| 4799 | |
| 4800 | strvec_push(&cmd.args, pgm->cmd); |
| 4801 | strvec_push(&cmd.args, name); |
| 4802 | |
| 4803 | if (one && two) { |
| 4804 | add_external_diff_name(o->repo, &cmd.args, one); |
| 4805 | add_external_diff_name(o->repo, &cmd.args, two); |
| 4806 | if (other) { |
| 4807 | strvec_push(&cmd.args, other); |
| 4808 | if (xfrm_msg) |
| 4809 | strvec_push(&cmd.args, xfrm_msg); |
| 4810 | } |
| 4811 | } |
| 4812 | |
| 4813 | strvec_pushf(&cmd.env, "GIT_DIFF_PATH_COUNTER=%d", |
| 4814 | ++o->diff_path_counter); |
| 4815 | strvec_pushf(&cmd.env, "GIT_DIFF_PATH_TOTAL=%d", q->nr); |
| 4816 | |
| 4817 | diff_free_filespec_data(one); |
| 4818 | diff_free_filespec_data(two); |
| 4819 | cmd.use_shell = 1; |
| 4820 | if (!o->file) |
| 4821 | cmd.no_stdout = 1; |
| 4822 | else if (o->file != stdout) |
| 4823 | cmd.out = xdup(fileno(o->file)); |
| 4824 | rc = run_command(&cmd); |
| 4825 | if (!pgm->trust_exit_code && rc == 0) |
| 4826 | o->found_changes = 1; |
| 4827 | else if (pgm->trust_exit_code && rc == 0) |
| 4828 | ; /* nothing */ |
| 4829 | else if (pgm->trust_exit_code && rc == 1) |
| 4830 | o->found_changes = 1; |
| 4831 | else |
| 4832 | die(_("external diff died, stopping at %s"), name); |
| 4833 | |
| 4834 | remove_tempfile(); |
| 4835 | } |
| 4836 | |
| 4837 | static int similarity_index(struct diff_filepair *p) |
| 4838 | { |
| 4839 | return p->score * 100 / MAX_SCORE; |
| 4840 | } |
| 4841 | |
| 4842 | static const char *diff_abbrev_oid(const struct object_id *oid, int abbrev) |
| 4843 | { |
| 4844 | if (startup_info->have_repository) |
| 4845 | return repo_find_unique_abbrev(the_repository, oid, abbrev); |
| 4846 | else { |
| 4847 | char *hex = oid_to_hex(oid); |
| 4848 | if (abbrev < 0) |
| 4849 | abbrev = FALLBACK_DEFAULT_ABBREV; |
| 4850 | if (abbrev > the_hash_algo->hexsz) |
| 4851 | BUG("oid abbreviation out of range: %d", abbrev); |
| 4852 | if (abbrev) |
| 4853 | hex[abbrev] = '\0'; |
| 4854 | return hex; |
| 4855 | } |
| 4856 | } |
| 4857 | |
| 4858 | static void fill_metainfo(struct strbuf *msg, |
| 4859 | const char *name, |
| 4860 | const char *other, |
| 4861 | struct diff_filespec *one, |
| 4862 | struct diff_filespec *two, |
| 4863 | struct diff_options *o, |
| 4864 | struct diff_filepair *p, |
| 4865 | int *must_show_header, |
| 4866 | enum git_colorbool use_color) |
| 4867 | { |
| 4868 | const char *set = diff_get_color(use_color, DIFF_METAINFO); |
| 4869 | const char *reset = diff_get_color(use_color, DIFF_RESET); |
| 4870 | const char *line_prefix = diff_line_prefix(o); |
| 4871 | struct string_list *more_headers = NULL; |
| 4872 | |
| 4873 | *must_show_header = 1; |
| 4874 | strbuf_init(msg, PATH_MAX * 2 + 300); |
| 4875 | switch (p->status) { |
| 4876 | case DIFF_STATUS_COPIED: |
| 4877 | strbuf_addf(msg, "%s%ssimilarity index %d%%", |
| 4878 | line_prefix, set, similarity_index(p)); |
| 4879 | strbuf_addf(msg, "%s\n%s%scopy from ", |
| 4880 | reset, line_prefix, set); |
| 4881 | quote_c_style(name, msg, NULL, 0); |
| 4882 | strbuf_addf(msg, "%s\n%s%scopy to ", reset, line_prefix, set); |
| 4883 | quote_c_style(other, msg, NULL, 0); |
| 4884 | strbuf_addf(msg, "%s\n", reset); |
| 4885 | break; |
| 4886 | case DIFF_STATUS_RENAMED: |
| 4887 | strbuf_addf(msg, "%s%ssimilarity index %d%%", |
| 4888 | line_prefix, set, similarity_index(p)); |
| 4889 | strbuf_addf(msg, "%s\n%s%srename from ", |
| 4890 | reset, line_prefix, set); |
| 4891 | quote_c_style(name, msg, NULL, 0); |
| 4892 | strbuf_addf(msg, "%s\n%s%srename to ", |
| 4893 | reset, line_prefix, set); |
| 4894 | quote_c_style(other, msg, NULL, 0); |
| 4895 | strbuf_addf(msg, "%s\n", reset); |
| 4896 | break; |
| 4897 | case DIFF_STATUS_MODIFIED: |
| 4898 | if (p->score) { |
| 4899 | strbuf_addf(msg, "%s%sdissimilarity index %d%%%s\n", |
| 4900 | line_prefix, |
| 4901 | set, similarity_index(p), reset); |
| 4902 | break; |
| 4903 | } |
| 4904 | /* fallthru */ |
| 4905 | default: |
| 4906 | *must_show_header = 0; |
| 4907 | } |
| 4908 | if ((more_headers = additional_headers(o, name))) { |
| 4909 | add_formatted_headers(msg, more_headers, |
| 4910 | line_prefix, set, reset); |
| 4911 | *must_show_header = 1; |
| 4912 | } |
| 4913 | if (one && two && !oideq(&one->oid, &two->oid)) { |
| 4914 | const unsigned hexsz = the_hash_algo->hexsz; |
| 4915 | int abbrev = o->abbrev ? o->abbrev : DEFAULT_ABBREV; |
| 4916 | |
| 4917 | if (o->flags.full_index) |
| 4918 | abbrev = hexsz; |
| 4919 | |
| 4920 | if (o->flags.binary) { |
| 4921 | mmfile_t mf; |
| 4922 | if ((!fill_mmfile(o->repo, &mf, one) && |
| 4923 | diff_filespec_is_binary(o->repo, one)) || |
| 4924 | (!fill_mmfile(o->repo, &mf, two) && |
| 4925 | diff_filespec_is_binary(o->repo, two))) |
| 4926 | abbrev = hexsz; |
| 4927 | } |
| 4928 | strbuf_addf(msg, "%s%sindex %s..%s", line_prefix, set, |
| 4929 | diff_abbrev_oid(&one->oid, abbrev), |
| 4930 | diff_abbrev_oid(&two->oid, abbrev)); |
| 4931 | if (one->mode == two->mode) |
| 4932 | strbuf_addf(msg, " %06o", one->mode); |
| 4933 | strbuf_addf(msg, "%s\n", reset); |
| 4934 | } |
| 4935 | } |
| 4936 | |
| 4937 | static void run_diff_cmd(const struct external_diff *pgm, |
| 4938 | const char *name, |
| 4939 | const char *other, |
| 4940 | const char *attr_path, |
| 4941 | struct diff_filespec *one, |
| 4942 | struct diff_filespec *two, |
| 4943 | struct strbuf *msg, |
| 4944 | struct diff_options *o, |
| 4945 | struct diff_filepair *p) |
| 4946 | { |
| 4947 | const char *xfrm_msg = NULL; |
| 4948 | int complete_rewrite = (p->status == DIFF_STATUS_MODIFIED) && p->score; |
| 4949 | int must_show_header = 0; |
| 4950 | struct userdiff_driver *drv = NULL; |
| 4951 | |
| 4952 | if (o->flags.allow_external || !o->ignore_driver_algorithm) |
| 4953 | drv = userdiff_find_by_path(o->repo->index, attr_path); |
| 4954 | |
| 4955 | if (o->flags.allow_external && drv && drv->external.cmd) |
| 4956 | pgm = &drv->external; |
| 4957 | |
| 4958 | if (msg) { |
| 4959 | /* |
| 4960 | * don't use colors when the header is intended for an |
| 4961 | * external diff driver |
| 4962 | */ |
| 4963 | fill_metainfo(msg, name, other, one, two, o, p, |
| 4964 | &must_show_header, |
| 4965 | pgm ? GIT_COLOR_NEVER : o->use_color); |
| 4966 | xfrm_msg = msg->len ? msg->buf : NULL; |
| 4967 | } |
| 4968 | |
| 4969 | if (pgm) { |
| 4970 | run_external_diff(pgm, name, other, one, two, xfrm_msg, o); |
| 4971 | return; |
| 4972 | } |
| 4973 | if (one && two) { |
| 4974 | if (!o->ignore_driver_algorithm && drv && drv->algorithm) |
| 4975 | set_diff_algorithm(o, drv->algorithm); |
| 4976 | |
| 4977 | builtin_diff(name, other ? other : name, |
| 4978 | one, two, xfrm_msg, must_show_header, |
| 4979 | o, complete_rewrite, p->line_ranges); |
| 4980 | if (p->status == DIFF_STATUS_COPIED || |
| 4981 | p->status == DIFF_STATUS_RENAMED) |
| 4982 | o->found_changes = 1; |
| 4983 | } else { |
| 4984 | if (o->file) |
| 4985 | fprintf(o->file, "* Unmerged path %s\n", name); |
| 4986 | o->found_changes = 1; |
| 4987 | } |
| 4988 | } |
| 4989 | |
| 4990 | static void diff_fill_oid_info(struct diff_filespec *one, struct index_state *istate) |
| 4991 | { |
| 4992 | if (DIFF_FILE_VALID(one)) { |
| 4993 | if (!one->oid_valid) { |
| 4994 | struct stat st; |
| 4995 | if (one->is_stdin) { |
| 4996 | oidclr(&one->oid, the_repository->hash_algo); |
| 4997 | return; |
| 4998 | } |
| 4999 | if (lstat(one->path, &st) < 0) |
| 5000 | die_errno("stat '%s'", one->path); |
Showing first 5,000 of 7,881 lines.
View raw