Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/git/blob/master/merge-ll.c # git [Ported from github.com/git/git] Sync with Git 2.56.1 Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `master` Branches Tags History `git / merge-ll.c` 530 lines ยท 14204 bytes Raw Blame History 1 /* 2 * Low level 3-way in-core file merge. 3 * 4 * Copyright (c) 2007 Junio C Hamano 5 */ 6 7 #define USE_THE_REPOSITORY_VARIABLE 8 #define DISABLE_SIGN_COMPARE_WARNINGS 9 10 #include "git-compat-util.h" 11 #include "config.h" 12 #include "convert.h" 13 #include "attr.h" 14 #include "xdiff-interface.h" 15 #include "run-command.h" 16 #include "merge-ll.h" 17 #include "quote.h" 18 #include "strbuf.h" 19 #include "gettext.h" 20 #include "tempfile.h" 21 22 struct ll_merge_driver; 23 24 typedef enum ll_merge_result (*ll_merge_fn)(const struct ll_merge_driver *, 25 mmfile_t *result, 26 const char *path, 27 mmfile_t *orig, const char *orig_name, 28 mmfile_t *src1, const char *name1, 29 mmfile_t *src2, const char *name2, 30 const struct ll_merge_options *opts, 31 int marker_size); 32 33 struct ll_merge_driver { 34 const char *name; 35 const char *description; 36 ll_merge_fn fn; 37 char *recursive; 38 struct ll_merge_driver *next; 39 char *cmdline; 40 }; 41 42 static struct attr_check *merge_attributes; 43 static struct attr_check *load_merge_attributes(void) 44 { 45 if (!merge_attributes) 46 merge_attributes = attr_check_initl("merge", "conflict-marker-size", NULL); 47 return merge_attributes; 48 } 49 50 void reset_merge_attributes(void) 51 { 52 attr_check_free(merge_attributes); 53 merge_attributes = NULL; 54 } 55 56 /* 57 * Built-in low-levels 58 */ 59 static enum ll_merge_result ll_binary_merge(const struct ll_merge_driver *drv UNUSED, 60 mmfile_t *result, 61 const char *path UNUSED, 62 mmfile_t *orig, const char *orig_name UNUSED, 63 mmfile_t *src1, const char *name1 UNUSED, 64 mmfile_t *src2, const char *name2 UNUSED, 65 const struct ll_merge_options *opts, 66 int marker_size UNUSED) 67 { 68 enum ll_merge_result ret; 69 mmfile_t *stolen; 70 assert(opts); 71 72 /* 73 * The tentative merge result is the common ancestor for an 74 * internal merge. For the final merge, it is "ours" by 75 * default but -Xours/-Xtheirs can tweak the choice. 76 */ 77 if (opts->virtual_ancestor) { 78 stolen = orig; 79 ret = LL_MERGE_OK; 80 } else { 81 switch (opts->variant) { 82 default: 83 ret = LL_MERGE_BINARY_CONFLICT; 84 stolen = src1; 85 break; 86 case XDL_MERGE_FAVOR_OURS: 87 ret = LL_MERGE_OK; 88 stolen = src1; 89 break; 90 case XDL_MERGE_FAVOR_THEIRS: 91 ret = LL_MERGE_OK; 92 stolen = src2; 93 break; 94 } 95 } 96 97 result->ptr = stolen->ptr; 98 result->size = stolen->size; 99 stolen->ptr = NULL; 100 101 return ret; 102 } 103 104 static enum ll_merge_result ll_xdl_merge(const struct ll_merge_driver *drv_unused, 105 mmfile_t *result, 106 const char *path, 107 mmfile_t *orig, const char *orig_name, 108 mmfile_t *src1, const char *name1, 109 mmfile_t *src2, const char *name2, 110 const struct ll_merge_options *opts, 111 int marker_size) 112 { 113 enum ll_merge_result ret; 114 xmparam_t xmp; 115 int status; 116 assert(opts); 117 118 if (orig->size > MAX_XDIFF_SIZE || 119 src1->size > MAX_XDIFF_SIZE || 120 src2->size > MAX_XDIFF_SIZE || 121 buffer_is_binary(orig->ptr, orig->size) || 122 buffer_is_binary(src1->ptr, src1->size) || 123 buffer_is_binary(src2->ptr, src2->size)) { 124 return ll_binary_merge(drv_unused, result, 125 path, 126 orig, orig_name, 127 src1, name1, 128 src2, name2, 129 opts, marker_size); 130 } 131 132 memset(&xmp, 0, sizeof(xmp)); 133 xmp.level = XDL_MERGE_ZEALOUS; 134 xmp.favor = opts->variant; 135 xmp.xpp.flags = opts->xdl_opts; 136 if (opts->conflict_style >= 0) 137 xmp.style = opts->conflict_style; 138 else if (git_xmerge_style >= 0) 139 xmp.style = git_xmerge_style; 140 if (marker_size > 0) 141 xmp.marker_size = marker_size; 142 xmp.ancestor = orig_name; 143 xmp.file1 = name1; 144 xmp.file2 = name2; 145 status = xdl_merge(orig, src1, src2, &xmp, result); 146 ret = (status > 0) ? LL_MERGE_CONFLICT : status; 147 return ret; 148 } 149 150 static enum ll_merge_result ll_union_merge(const struct ll_merge_driver *drv_unused, 151 mmfile_t *result, 152 const char *path, 153 mmfile_t *orig, const char *orig_name, 154 mmfile_t *src1, const char *name1, 155 mmfile_t *src2, const char *name2, 156 const struct ll_merge_options *opts, 157 int marker_size) 158 { 159 /* Use union favor */ 160 struct ll_merge_options o; 161 assert(opts); 162 o = *opts; 163 o.variant = XDL_MERGE_FAVOR_UNION; 164 return ll_xdl_merge(drv_unused, result, path, 165 orig, orig_name, src1, name1, src2, name2, 166 &o, marker_size); 167 } 168 169 #define LL_BINARY_MERGE 0 170 #define LL_TEXT_MERGE 1 171 #define LL_UNION_MERGE 2 172 static struct ll_merge_driver ll_merge_drv[] = { 173 { "binary", "built-in binary merge", ll_binary_merge }, 174 { "text", "built-in 3-way text merge", ll_xdl_merge }, 175 { "union", "built-in union merge", ll_union_merge }, 176 }; 177 178 static struct tempfile *create_temp(mmfile_t *src) 179 { 180 struct tempfile *t = xmks_tempfile(".merge_file_XXXXXX"); 181 if (write_in_full(t->fd, src->ptr, src->size) < 0) 182 die_errno(_("unable to write %s"), get_tempfile_path(t)); 183 if (close_tempfile_gently(t) < 0) 184 die_errno(_("unable to close %s"), get_tempfile_path(t)); 185 return t; 186 } 187 188 static const char *temp_path_basename(struct tempfile *t) 189 { 190 /* 191 * basename() takes a non-const pointer because it can 192 * modify the input string to remove trailing directory 193 * separators. We know that we don't have any because 194 * this is a clean path generated from our vanilla 195 * tempfile template. 196 * 197 * So casting away the const here is safe, albeit gross. 198 */ 199 return basename((char *)get_tempfile_path(t)); 200 } 201 202 /* 203 * User defined low-level merge driver support. 204 */ 205 static enum ll_merge_result ll_ext_merge(const struct ll_merge_driver *fn, 206 mmfile_t *result, 207 const char *path, 208 mmfile_t *orig, const char *orig_name, 209 mmfile_t *src1, const char *name1, 210 mmfile_t *src2, const char *name2, 211 const struct ll_merge_options *opts, 212 int marker_size) 213 { 214 struct tempfile *tmp_o, *tmp_a, *tmp_b; 215 struct strbuf cmd = STRBUF_INIT; 216 const char *format = fn->cmdline; 217 struct child_process child = CHILD_PROCESS_INIT; 218 int status; 219 enum ll_merge_result ret; 220 assert(opts); 221 222 if (!fn->cmdline) 223 die("custom merge driver %s lacks command line.", fn->name); 224 225 result->ptr = NULL; 226 result->size = 0; 227 tmp_o = create_temp(orig); 228 tmp_a = create_temp(src1); 229 tmp_b = create_temp(src2); 230 231 while (strbuf_expand_step(&cmd, &format)) { 232 if (skip_prefix(format, "%", &format)) 233 strbuf_addch(&cmd, '%'); 234 else if (skip_prefix(format, "O", &format)) 235 strbuf_addstr(&cmd, temp_path_basename(tmp_o)); 236 else if (skip_prefix(format, "A", &format)) 237 strbuf_addstr(&cmd, temp_path_basename(tmp_a)); 238 else if (skip_prefix(format, "B", &format)) 239 strbuf_addstr(&cmd, temp_path_basename(tmp_b)); 240 else if (skip_prefix(format, "L", &format)) 241 strbuf_addf(&cmd, "%d", marker_size); 242 else if (skip_prefix(format, "P", &format)) 243 sq_quote_buf(&cmd, path); 244 else if (skip_prefix(format, "S", &format)) 245 sq_quote_buf(&cmd, orig_name ? orig_name : ""); 246 else if (skip_prefix(format, "X", &format)) 247 sq_quote_buf(&cmd, name1 ? name1 : ""); 248 else if (skip_prefix(format, "Y", &format)) 249 sq_quote_buf(&cmd, name2 ? name2 : ""); 250 else 251 strbuf_addch(&cmd, '%'); 252 } 253 254 child.use_shell = 1; 255 strvec_push(&child.args, cmd.buf); 256 status = run_command(&child); 257 if (!status) 258 ret = LL_MERGE_OK; 259 else if (status <= 128) 260 ret = LL_MERGE_CONFLICT; 261 else 262 /* died due to a signal: WTERMSIG(status) + 128 */ 263 ret = LL_MERGE_ERROR; 264 265 if (read_mmfile(result, temp_path_basename(tmp_a)) < 0) 266 ret = LL_MERGE_ERROR; 267 268 delete_tempfile(&tmp_o); 269 delete_tempfile(&tmp_a); 270 delete_tempfile(&tmp_b); 271 strbuf_release(&cmd); 272 return ret; 273 } 274 275 /* 276 * merge.default and merge.driver configuration items 277 */ 278 static struct ll_merge_driver *ll_user_merge, **ll_user_merge_tail; 279 static char *default_ll_merge; 280 281 static int read_merge_config(const char *var, const char *value, 282 const struct config_context *ctx UNUSED, 283 void *cb UNUSED) 284 { 285 struct ll_merge_driver *fn; 286 const char *key, *name; 287 size_t namelen; 288 289 if (!strcmp(var, "merge.default")) 290 return git_config_string(&default_ll_merge, var, value); 291 292 /* 293 * We are not interested in anything but "merge..variable"; 294 * especially, we do not want to look at variables such as 295 * "merge.summary", "merge.tool", and "merge.verbosity". 296 */ 297 if (parse_config_key(var, "merge", &name, &namelen, &key) < 0 || !name) 298 return 0; 299 300 /* 301 * Find existing one as we might be processing merge..var2 302 * after seeing merge..var1. 303 */ 304 for (fn = ll_user_merge; fn; fn = fn->next) 305 if (!xstrncmpz(fn->name, name, namelen)) 306 break; 307 if (!fn) { 308 CALLOC_ARRAY(fn, 1); 309 fn->name = xmemdupz(name, namelen); 310 fn->fn = ll_ext_merge; 311 *ll_user_merge_tail = fn; 312 ll_user_merge_tail = &(fn->next); 313 } 314 315 if (!strcmp("name", key)) { 316 /* 317 * The description is leaking, but that's okay as we want to 318 * keep around the merge drivers anyway. 319 */ 320 return git_config_string((char **) &fn->description, var, value); 321 } 322 323 if (!strcmp("driver", key)) { 324 if (!value) 325 return config_error_nonbool(var); 326 /* 327 * merge..driver specifies the command line: 328 * 329 * command-line 330 * 331 * The command-line will be interpolated with the following 332 * tokens and is given to the shell: 333 * 334 * %O - temporary file name for the merge base. 335 * %A - temporary file name for our version. 336 * %B - temporary file name for the other branches' version. 337 * %L - conflict marker length 338 * %P - the original path (safely quoted for the shell) 339 * %S - the revision for the merge base 340 * %X - the revision for our version 341 * %Y - the revision for their version 342 * 343 * If the file is not named identically in all versions, then each 344 * revision is joined with the corresponding path, separated by a colon. 345 * The external merge driver should write the results in the 346 * file named by %A, and signal that it has done with zero exit 347 * status. 348 */ 349 fn->cmdline = xstrdup(value); 350 return 0; 351 } 352 353 if (!strcmp("recursive", key)) 354 return git_config_string(&fn->recursive, var, value); 355 356 return 0; 357 } 358 359 static void initialize_ll_merge(void) 360 { 361 if (ll_user_merge_tail) 362 return; 363 ll_user_merge_tail = &ll_user_merge; 364 repo_config(the_repository, read_merge_config, NULL); 365 } 366 367 static const struct ll_merge_driver *find_ll_merge_driver(const char *merge_attr) 368 { 369 struct ll_merge_driver *fn; 370 const char *name; 371 int i; 372 373 initialize_ll_merge(); 374 375 if (ATTR_TRUE(merge_attr)) 376 return &ll_merge_drv[LL_TEXT_MERGE]; 377 else if (ATTR_FALSE(merge_attr)) 378 return &ll_merge_drv[LL_BINARY_MERGE]; 379 else if (ATTR_UNSET(merge_attr)) { 380 if (!default_ll_merge) 381 return &ll_merge_drv[LL_TEXT_MERGE]; 382 else 383 name = default_ll_merge; 384 } 385 else 386 name = merge_attr; 387 388 for (fn = ll_user_merge; fn; fn = fn->next) 389 if (!strcmp(fn->name, name)) 390 return fn; 391 392 for (i = 0; i < ARRAY_SIZE(ll_merge_drv); i++) 393 if (!strcmp(ll_merge_drv[i].name, name)) 394 return &ll_merge_drv[i]; 395 396 /* default to the 3-way */ 397 return &ll_merge_drv[LL_TEXT_MERGE]; 398 } 399 400 static void normalize_file(mmfile_t *mm, const char *path, struct index_state *istate) 401 { 402 struct strbuf strbuf = STRBUF_INIT; 403 if (renormalize_buffer(istate, path, mm->ptr, mm->size, &strbuf)) { 404 free(mm->ptr); 405 mm->size = strbuf.len; 406 mm->ptr = strbuf_detach(&strbuf, NULL); 407 } 408 } 409 410 enum ll_merge_result ll_merge(mmfile_t *result_buf, 411 const char *path, 412 mmfile_t *ancestor, const char *ancestor_label, 413 mmfile_t *ours, const char *our_label, 414 mmfile_t *theirs, const char *their_label, 415 struct index_state *istate, 416 const struct ll_merge_options *opts) 417 { 418 struct attr_check *check = load_merge_attributes(); 419 static const struct ll_merge_options default_opts = LL_MERGE_OPTIONS_INIT; 420 const char *ll_driver_name = NULL; 421 int marker_size = DEFAULT_CONFLICT_MARKER_SIZE; 422 const struct ll_merge_driver *driver; 423 424 if (!opts) 425 opts = &default_opts; 426 427 if (opts->renormalize) { 428 normalize_file(ancestor, path, istate); 429 normalize_file(ours, path, istate); 430 normalize_file(theirs, path, istate); 431 } 432 433 git_check_attr(istate, path, check); 434 ll_driver_name = check->items[0].value; 435 if (check->items[1].value) { 436 if (strtol_i(check->items[1].value, 10, &marker_size)) { 437 marker_size = DEFAULT_CONFLICT_MARKER_SIZE; 438 warning(_("invalid marker-size '%s', expecting an integer"), check->items[1].value); 439 } 440 if (marker_size <= 0) 441 marker_size = DEFAULT_CONFLICT_MARKER_SIZE; 442 } 443 driver = find_ll_merge_driver(ll_driver_name); 444 445 if (opts->virtual_ancestor) { 446 if (driver->recursive) 447 driver = find_ll_merge_driver(driver->recursive); 448 } 449 if (opts->extra_marker_size) { 450 marker_size += opts->extra_marker_size; 451 } 452 return driver->fn(driver, result_buf, path, ancestor, ancestor_label, 453 ours, our_label, theirs, their_label, 454 opts, marker_size); 455 } 456 457 int ll_merge_marker_size(struct index_state *istate, const char *path) 458 { 459 static struct attr_check *check; 460 int marker_size = DEFAULT_CONFLICT_MARKER_SIZE; 461 462 if (!check) 463 check = attr_check_initl("conflict-marker-size", NULL); 464 git_check_attr(istate, path, check); 465 if (check->items[0].value) { 466 if (strtol_i(check->items[0].value, 10, &marker_size)) { 467 marker_size = DEFAULT_CONFLICT_MARKER_SIZE; 468 warning(_("invalid marker-size '%s', expecting an integer"), check->items[0].value); 469 } 470 if (marker_size <= 0) 471 marker_size = DEFAULT_CONFLICT_MARKER_SIZE; 472 } 473 return marker_size; 474 } 475 476 int is_conflict_marker_line(const char *line, unsigned long len, int marker_size) 477 { 478 char firstchar; 479 int cnt; 480 481 if (len < marker_size + 1) 482 return 0; 483 484 firstchar = line[0]; 485 switch (firstchar) { 486 case '=': case '>': case '<': case '|': 487 break; 488 default: 489 return 0; 490 } 491 492 for (cnt = 1; cnt < marker_size; cnt++) { 493 if (line[cnt] != firstchar) 494 return 0; 495 } 496 497 if (((firstchar == '<') || (firstchar == '>')) && 498 line[marker_size] != ' ') 499 return 0; 500 501 if (!isspace((unsigned char)line[marker_size])) 502 return 0; 503 504 return firstchar; 505 } 506 507 int has_conflict_markers(struct index_state *istate, const char *path) 508 { 509 FILE *f; 510 struct strbuf sb = STRBUF_INIT; 511 int marker_size = ll_merge_marker_size(istate, path); 512 int has_markers = 0; 513 514 f = fopen(path, "r"); 515 if (!f) 516 return 0; 517 518 while (strbuf_getwholeline(&sb, f, '\n') != EOF) { 519 if (is_conflict_marker_line(sb.buf, sb.len, marker_size)) { 520 has_markers = 1; 521 break; 522 } 523 if (buffer_is_binary(sb.buf, 524 ULONG_MAX <= sb.len ? ULONG_MAX : sb.len)) 525 break; 526 } 527 fclose(f); 528 strbuf_release(&sb); 529 return has_markers; 530 }