| 1 | #define USE_THE_REPOSITORY_VARIABLE |
| 2 | #define DISABLE_SIGN_COMPARE_WARNINGS |
| 3 | |
| 4 | #include "git-compat-util.h" |
| 5 | #include "abspath.h" |
| 6 | #include "config.h" |
| 7 | #include "copy.h" |
| 8 | #include "environment.h" |
| 9 | #include "gettext.h" |
| 10 | #include "hex.h" |
| 11 | #include "lockfile.h" |
| 12 | #include "string-list.h" |
| 13 | #include "read-cache-ll.h" |
| 14 | #include "rerere.h" |
| 15 | #include "xdiff-interface.h" |
| 16 | #include "dir.h" |
| 17 | #include "resolve-undo.h" |
| 18 | #include "merge-ll.h" |
| 19 | #include "path.h" |
| 20 | #include "pathspec.h" |
| 21 | #include "object-file.h" |
| 22 | #include "odb.h" |
| 23 | #include "strmap.h" |
| 24 | |
| 25 | #define RESOLVED 0 |
| 26 | #define PUNTED 1 |
| 27 | #define THREE_STAGED 2 |
| 28 | void *RERERE_RESOLVED = &RERERE_RESOLVED; |
| 29 | |
| 30 | /* if rerere_enabled == -1, fall back to detection of .git/rr-cache */ |
| 31 | static int rerere_enabled = -1; |
| 32 | |
| 33 | /* automatically update cleanly resolved paths to the index */ |
| 34 | static int rerere_autoupdate; |
| 35 | |
| 36 | #define RR_HAS_POSTIMAGE 1 |
| 37 | #define RR_HAS_PREIMAGE 2 |
| 38 | struct rerere_dir { |
| 39 | int status_alloc, status_nr; |
| 40 | unsigned char *status; |
| 41 | char name[FLEX_ARRAY]; |
| 42 | }; |
| 43 | |
| 44 | static struct strmap rerere_dirs = STRMAP_INIT; |
| 45 | |
| 46 | static void free_rerere_dirs(void) |
| 47 | { |
| 48 | struct hashmap_iter iter; |
| 49 | struct strmap_entry *ent; |
| 50 | |
| 51 | strmap_for_each_entry(&rerere_dirs, &iter, ent) { |
| 52 | struct rerere_dir *rr_dir = ent->value; |
| 53 | free(rr_dir->status); |
| 54 | free(rr_dir); |
| 55 | } |
| 56 | strmap_clear(&rerere_dirs, 0); |
| 57 | } |
| 58 | |
| 59 | static void free_rerere_id(struct string_list_item *item) |
| 60 | { |
| 61 | free(item->util); |
| 62 | } |
| 63 | |
| 64 | static const char *rerere_id_hex(const struct rerere_id *id) |
| 65 | { |
| 66 | return id->collection->name; |
| 67 | } |
| 68 | |
| 69 | static void fit_variant(struct rerere_dir *rr_dir, int variant) |
| 70 | { |
| 71 | variant++; |
| 72 | ALLOC_GROW(rr_dir->status, variant, rr_dir->status_alloc); |
| 73 | if (rr_dir->status_nr < variant) { |
| 74 | memset(rr_dir->status + rr_dir->status_nr, |
| 75 | '\0', variant - rr_dir->status_nr); |
| 76 | rr_dir->status_nr = variant; |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | static void assign_variant(struct rerere_id *id) |
| 81 | { |
| 82 | int variant; |
| 83 | struct rerere_dir *rr_dir = id->collection; |
| 84 | |
| 85 | variant = id->variant; |
| 86 | if (variant < 0) { |
| 87 | for (variant = 0; variant < rr_dir->status_nr; variant++) |
| 88 | if (!rr_dir->status[variant]) |
| 89 | break; |
| 90 | } |
| 91 | fit_variant(rr_dir, variant); |
| 92 | id->variant = variant; |
| 93 | } |
| 94 | |
| 95 | const char *rerere_path(struct strbuf *buf, const struct rerere_id *id, const char *file) |
| 96 | { |
| 97 | if (!file) |
| 98 | return repo_git_path_replace(the_repository, buf, "rr-cache/%s", |
| 99 | rerere_id_hex(id)); |
| 100 | |
| 101 | if (id->variant <= 0) |
| 102 | return repo_git_path_replace(the_repository, buf, "rr-cache/%s/%s", |
| 103 | rerere_id_hex(id), file); |
| 104 | |
| 105 | return repo_git_path_replace(the_repository, buf, "rr-cache/%s/%s.%d", |
| 106 | rerere_id_hex(id), file, id->variant); |
| 107 | } |
| 108 | |
| 109 | static int is_rr_file(const char *name, const char *filename, int *variant) |
| 110 | { |
| 111 | const char *suffix; |
| 112 | char *ep; |
| 113 | |
| 114 | if (!strcmp(name, filename)) { |
| 115 | *variant = 0; |
| 116 | return 1; |
| 117 | } |
| 118 | if (!skip_prefix(name, filename, &suffix) || *suffix != '.') |
| 119 | return 0; |
| 120 | |
| 121 | errno = 0; |
| 122 | *variant = strtol(suffix + 1, &ep, 10); |
| 123 | if (errno || *ep) |
| 124 | return 0; |
| 125 | return 1; |
| 126 | } |
| 127 | |
| 128 | static void scan_rerere_dir(struct rerere_dir *rr_dir) |
| 129 | { |
| 130 | struct dirent *de; |
| 131 | char *path; |
| 132 | DIR *dir; |
| 133 | |
| 134 | path = repo_git_path(the_repository, "rr-cache/%s", rr_dir->name); |
| 135 | dir = opendir(path); |
| 136 | free(path); |
| 137 | if (!dir) |
| 138 | return; |
| 139 | while ((de = readdir(dir)) != NULL) { |
| 140 | int variant; |
| 141 | |
| 142 | if (is_rr_file(de->d_name, "postimage", &variant)) { |
| 143 | fit_variant(rr_dir, variant); |
| 144 | rr_dir->status[variant] |= RR_HAS_POSTIMAGE; |
| 145 | } else if (is_rr_file(de->d_name, "preimage", &variant)) { |
| 146 | fit_variant(rr_dir, variant); |
| 147 | rr_dir->status[variant] |= RR_HAS_PREIMAGE; |
| 148 | } |
| 149 | } |
| 150 | closedir(dir); |
| 151 | } |
| 152 | |
| 153 | static struct rerere_dir *find_rerere_dir(const char *hex) |
| 154 | { |
| 155 | struct rerere_dir *rr_dir; |
| 156 | |
| 157 | rr_dir = strmap_get(&rerere_dirs, hex); |
| 158 | if (!rr_dir) { |
| 159 | FLEX_ALLOC_STR(rr_dir, name, hex); |
| 160 | rr_dir->status = NULL; |
| 161 | rr_dir->status_nr = 0; |
| 162 | rr_dir->status_alloc = 0; |
| 163 | strmap_put(&rerere_dirs, hex, rr_dir); |
| 164 | |
| 165 | scan_rerere_dir(rr_dir); |
| 166 | } |
| 167 | return rr_dir; |
| 168 | } |
| 169 | |
| 170 | static int has_rerere_resolution(const struct rerere_id *id) |
| 171 | { |
| 172 | const int both = RR_HAS_POSTIMAGE|RR_HAS_PREIMAGE; |
| 173 | int variant = id->variant; |
| 174 | |
| 175 | if (variant < 0) |
| 176 | return 0; |
| 177 | return ((id->collection->status[variant] & both) == both); |
| 178 | } |
| 179 | |
| 180 | static struct rerere_id *new_rerere_id_hex(char *hex) |
| 181 | { |
| 182 | struct rerere_id *id = xmalloc(sizeof(*id)); |
| 183 | id->collection = find_rerere_dir(hex); |
| 184 | id->variant = -1; /* not known yet */ |
| 185 | return id; |
| 186 | } |
| 187 | |
| 188 | static struct rerere_id *new_rerere_id(unsigned char *hash) |
| 189 | { |
| 190 | return new_rerere_id_hex(hash_to_hex(hash)); |
| 191 | } |
| 192 | |
| 193 | /* |
| 194 | * $GIT_DIR/MERGE_RR file is a collection of records, each of which is |
| 195 | * "conflict ID", a HT and pathname, terminated with a NUL, and is |
| 196 | * used to keep track of the set of paths that "rerere" may need to |
| 197 | * work on (i.e. what is left by the previous invocation of "git |
| 198 | * rerere" during the current conflict resolution session). |
| 199 | */ |
| 200 | static void read_rr(struct repository *r, struct string_list *rr) |
| 201 | { |
| 202 | struct strbuf buf = STRBUF_INIT; |
| 203 | FILE *in = fopen_or_warn(git_path_merge_rr(r), "r"); |
| 204 | |
| 205 | if (!in) |
| 206 | return; |
| 207 | while (!strbuf_getwholeline(&buf, in, '\0')) { |
| 208 | char *path; |
| 209 | unsigned char hash[GIT_MAX_RAWSZ]; |
| 210 | struct rerere_id *id; |
| 211 | int variant; |
| 212 | const unsigned hexsz = the_hash_algo->hexsz; |
| 213 | |
| 214 | /* There has to be the hash, tab, path and then NUL */ |
| 215 | if (buf.len < hexsz + 2 || get_hash_hex(buf.buf, hash)) |
| 216 | die(_("corrupt MERGE_RR")); |
| 217 | |
| 218 | if (buf.buf[hexsz] != '.') { |
| 219 | variant = 0; |
| 220 | path = buf.buf + hexsz; |
| 221 | } else { |
| 222 | errno = 0; |
| 223 | variant = strtol(buf.buf + hexsz + 1, &path, 10); |
| 224 | if (errno) |
| 225 | die(_("corrupt MERGE_RR")); |
| 226 | } |
| 227 | if (*(path++) != '\t') |
| 228 | die(_("corrupt MERGE_RR")); |
| 229 | buf.buf[hexsz] = '\0'; |
| 230 | id = new_rerere_id_hex(buf.buf); |
| 231 | id->variant = variant; |
| 232 | /* |
| 233 | * make sure id->collection->status has enough space |
| 234 | * for the variant we are interested in |
| 235 | */ |
| 236 | fit_variant(id->collection, variant); |
| 237 | string_list_insert(rr, path)->util = id; |
| 238 | } |
| 239 | strbuf_release(&buf); |
| 240 | fclose(in); |
| 241 | } |
| 242 | |
| 243 | static struct lock_file write_lock; |
| 244 | |
| 245 | static int write_rr(struct string_list *rr, int out_fd) |
| 246 | { |
| 247 | int i; |
| 248 | for (i = 0; i < rr->nr; i++) { |
| 249 | struct strbuf buf = STRBUF_INIT; |
| 250 | struct rerere_id *id; |
| 251 | |
| 252 | assert(rr->items[i].util != RERERE_RESOLVED); |
| 253 | |
| 254 | id = rr->items[i].util; |
| 255 | if (!id) |
| 256 | continue; |
| 257 | assert(id->variant >= 0); |
| 258 | if (0 < id->variant) |
| 259 | strbuf_addf(&buf, "%s.%d\t%s%c", |
| 260 | rerere_id_hex(id), id->variant, |
| 261 | rr->items[i].string, 0); |
| 262 | else |
| 263 | strbuf_addf(&buf, "%s\t%s%c", |
| 264 | rerere_id_hex(id), |
| 265 | rr->items[i].string, 0); |
| 266 | |
| 267 | if (write_in_full(out_fd, buf.buf, buf.len) < 0) |
| 268 | die(_("unable to write rerere record")); |
| 269 | |
| 270 | strbuf_release(&buf); |
| 271 | } |
| 272 | if (commit_lock_file(&write_lock) != 0) |
| 273 | die(_("unable to write rerere record")); |
| 274 | return 0; |
| 275 | } |
| 276 | |
| 277 | /* |
| 278 | * "rerere" interacts with conflicted file contents using this I/O |
| 279 | * abstraction. It reads a conflicted contents from one place via |
| 280 | * "getline()" method, and optionally can write it out after |
| 281 | * normalizing the conflicted hunks to the "output". Subclasses of |
| 282 | * rerere_io embed this structure at the beginning of their own |
| 283 | * rerere_io object. |
| 284 | */ |
| 285 | struct rerere_io { |
| 286 | int (*getline)(struct strbuf *, struct rerere_io *); |
| 287 | FILE *output; |
| 288 | int wrerror; |
| 289 | /* some more stuff */ |
| 290 | }; |
| 291 | |
| 292 | static void ferr_write(const void *p, size_t count, FILE *fp, int *err) |
| 293 | { |
| 294 | if (!count || *err) |
| 295 | return; |
| 296 | if (fwrite(p, count, 1, fp) != 1) |
| 297 | *err = errno; |
| 298 | } |
| 299 | |
| 300 | static inline void ferr_puts(const char *s, FILE *fp, int *err) |
| 301 | { |
| 302 | ferr_write(s, strlen(s), fp, err); |
| 303 | } |
| 304 | |
| 305 | static void rerere_io_putstr(const char *str, struct rerere_io *io) |
| 306 | { |
| 307 | if (io->output) |
| 308 | ferr_puts(str, io->output, &io->wrerror); |
| 309 | } |
| 310 | |
| 311 | static void rerere_io_putmem(const char *mem, size_t sz, struct rerere_io *io) |
| 312 | { |
| 313 | if (io->output) |
| 314 | ferr_write(mem, sz, io->output, &io->wrerror); |
| 315 | } |
| 316 | |
| 317 | /* |
| 318 | * Subclass of rerere_io that reads from an on-disk file |
| 319 | */ |
| 320 | struct rerere_io_file { |
| 321 | struct rerere_io io; |
| 322 | FILE *input; |
| 323 | }; |
| 324 | |
| 325 | /* |
| 326 | * ... and its getline() method implementation |
| 327 | */ |
| 328 | static int rerere_file_getline(struct strbuf *sb, struct rerere_io *io_) |
| 329 | { |
| 330 | struct rerere_io_file *io = (struct rerere_io_file *)io_; |
| 331 | return strbuf_getwholeline(sb, io->input, '\n'); |
| 332 | } |
| 333 | |
| 334 | /* |
| 335 | * Require the exact number of conflict marker letters, no more, no |
| 336 | * less, followed by SP or any whitespace |
| 337 | * (including LF). |
| 338 | */ |
| 339 | static int is_cmarker(char *buf, int marker_char, int marker_size) |
| 340 | { |
| 341 | int want_sp; |
| 342 | |
| 343 | /* |
| 344 | * The beginning of our version and the end of their version |
| 345 | * always are labeled like "<<<<< ours" or ">>>>> theirs", |
| 346 | * hence we set want_sp for them. Note that the version from |
| 347 | * the common ancestor in diff3-style output is not always |
| 348 | * labelled (e.g. "||||| common" is often seen but "|||||" |
| 349 | * alone is also valid), so we do not set want_sp. |
| 350 | */ |
| 351 | want_sp = (marker_char == '<') || (marker_char == '>'); |
| 352 | |
| 353 | while (marker_size--) |
| 354 | if (*buf++ != marker_char) |
| 355 | return 0; |
| 356 | if (want_sp && *buf != ' ') |
| 357 | return 0; |
| 358 | return isspace(*buf); |
| 359 | } |
| 360 | |
| 361 | static void rerere_strbuf_putconflict(struct strbuf *buf, int ch, size_t size) |
| 362 | { |
| 363 | strbuf_addchars(buf, ch, size); |
| 364 | strbuf_addch(buf, '\n'); |
| 365 | } |
| 366 | |
| 367 | static int handle_conflict(struct strbuf *out, struct rerere_io *io, |
| 368 | int marker_size, struct git_hash_ctx *ctx) |
| 369 | { |
| 370 | enum { |
| 371 | RR_SIDE_1 = 0, RR_SIDE_2, RR_ORIGINAL |
| 372 | } hunk = RR_SIDE_1; |
| 373 | struct strbuf one = STRBUF_INIT, two = STRBUF_INIT; |
| 374 | struct strbuf buf = STRBUF_INIT, conflict = STRBUF_INIT; |
| 375 | int has_conflicts = -1; |
| 376 | |
| 377 | while (!io->getline(&buf, io)) { |
| 378 | if (is_cmarker(buf.buf, '<', marker_size)) { |
| 379 | if (handle_conflict(&conflict, io, marker_size, NULL) < 0) |
| 380 | break; |
| 381 | if (hunk == RR_SIDE_1) |
| 382 | strbuf_addbuf(&one, &conflict); |
| 383 | else |
| 384 | strbuf_addbuf(&two, &conflict); |
| 385 | strbuf_release(&conflict); |
| 386 | } else if (is_cmarker(buf.buf, '|', marker_size)) { |
| 387 | if (hunk != RR_SIDE_1) |
| 388 | break; |
| 389 | hunk = RR_ORIGINAL; |
| 390 | } else if (is_cmarker(buf.buf, '=', marker_size)) { |
| 391 | if (hunk != RR_SIDE_1 && hunk != RR_ORIGINAL) |
| 392 | break; |
| 393 | hunk = RR_SIDE_2; |
| 394 | } else if (is_cmarker(buf.buf, '>', marker_size)) { |
| 395 | if (hunk != RR_SIDE_2) |
| 396 | break; |
| 397 | if (strbuf_cmp(&one, &two) > 0) |
| 398 | strbuf_swap(&one, &two); |
| 399 | has_conflicts = 1; |
| 400 | rerere_strbuf_putconflict(out, '<', marker_size); |
| 401 | strbuf_addbuf(out, &one); |
| 402 | rerere_strbuf_putconflict(out, '=', marker_size); |
| 403 | strbuf_addbuf(out, &two); |
| 404 | rerere_strbuf_putconflict(out, '>', marker_size); |
| 405 | if (ctx) { |
| 406 | git_hash_update(ctx, one.buf, one.len + 1); |
| 407 | git_hash_update(ctx, two.buf, two.len + 1); |
| 408 | } |
| 409 | break; |
| 410 | } else if (hunk == RR_SIDE_1) |
| 411 | strbuf_addbuf(&one, &buf); |
| 412 | else if (hunk == RR_ORIGINAL) |
| 413 | ; /* discard */ |
| 414 | else if (hunk == RR_SIDE_2) |
| 415 | strbuf_addbuf(&two, &buf); |
| 416 | } |
| 417 | strbuf_release(&one); |
| 418 | strbuf_release(&two); |
| 419 | strbuf_release(&buf); |
| 420 | |
| 421 | return has_conflicts; |
| 422 | } |
| 423 | |
| 424 | /* |
| 425 | * Read contents a file with conflicts, normalize the conflicts |
| 426 | * by (1) discarding the common ancestor version in diff3-style, |
| 427 | * (2) reordering our side and their side so that whichever sorts |
| 428 | * alphabetically earlier comes before the other one, while |
| 429 | * computing the "conflict ID", which is just an SHA-1 hash of |
| 430 | * one side of the conflict, NUL, the other side of the conflict, |
| 431 | * and NUL concatenated together. |
| 432 | * |
| 433 | * Return 1 if conflict hunks are found, 0 if there are no conflict |
| 434 | * hunks and -1 if an error occurred. |
| 435 | */ |
| 436 | static int handle_path(unsigned char *hash, struct rerere_io *io, int marker_size) |
| 437 | { |
| 438 | struct git_hash_ctx ctx; |
| 439 | struct strbuf buf = STRBUF_INIT, out = STRBUF_INIT; |
| 440 | int has_conflicts = 0; |
| 441 | if (hash) |
| 442 | git_hash_init(&ctx, the_hash_algo); |
| 443 | |
| 444 | while (!io->getline(&buf, io)) { |
| 445 | if (is_cmarker(buf.buf, '<', marker_size)) { |
| 446 | has_conflicts = handle_conflict(&out, io, marker_size, |
| 447 | hash ? &ctx : NULL); |
| 448 | if (has_conflicts < 0) |
| 449 | break; |
| 450 | rerere_io_putmem(out.buf, out.len, io); |
| 451 | strbuf_reset(&out); |
| 452 | } else |
| 453 | rerere_io_putstr(buf.buf, io); |
| 454 | } |
| 455 | strbuf_release(&buf); |
| 456 | strbuf_release(&out); |
| 457 | |
| 458 | if (hash) |
| 459 | git_hash_final(hash, &ctx); |
| 460 | |
| 461 | return has_conflicts; |
| 462 | } |
| 463 | |
| 464 | /* |
| 465 | * Scan the path for conflicts, do the "handle_path()" thing above, and |
| 466 | * return the number of conflict hunks found. |
| 467 | */ |
| 468 | static int handle_file(struct index_state *istate, |
| 469 | const char *path, unsigned char *hash, const char *output) |
| 470 | { |
| 471 | int has_conflicts = 0; |
| 472 | struct rerere_io_file io; |
| 473 | int marker_size = ll_merge_marker_size(istate, path); |
| 474 | |
| 475 | memset(&io, 0, sizeof(io)); |
| 476 | io.io.getline = rerere_file_getline; |
| 477 | io.input = fopen(path, "r"); |
| 478 | io.io.wrerror = 0; |
| 479 | if (!io.input) |
| 480 | return error_errno(_("could not open '%s'"), path); |
| 481 | |
| 482 | if (output) { |
| 483 | io.io.output = fopen(output, "w"); |
| 484 | if (!io.io.output) { |
| 485 | error_errno(_("could not write '%s'"), output); |
| 486 | fclose(io.input); |
| 487 | return -1; |
| 488 | } |
| 489 | } |
| 490 | |
| 491 | has_conflicts = handle_path(hash, (struct rerere_io *)&io, marker_size); |
| 492 | |
| 493 | fclose(io.input); |
| 494 | if (io.io.wrerror) |
| 495 | error(_("there were errors while writing '%s' (%s)"), |
| 496 | path, strerror(io.io.wrerror)); |
| 497 | if (io.io.output && fclose(io.io.output)) |
| 498 | io.io.wrerror = error_errno(_("failed to flush '%s'"), path); |
| 499 | |
| 500 | if (has_conflicts < 0) { |
| 501 | if (output) |
| 502 | unlink_or_warn(output); |
| 503 | return error(_("could not parse conflict hunks in '%s'"), path); |
| 504 | } |
| 505 | if (io.io.wrerror) |
| 506 | return -1; |
| 507 | return has_conflicts; |
| 508 | } |
| 509 | |
| 510 | /* |
| 511 | * Look at a cache entry at "i" and see if it is not conflicting, |
| 512 | * conflicting and we are willing to handle, or conflicting and |
| 513 | * we are unable to handle, and return the determination in *type. |
| 514 | * Return the cache index to be looked at next, by skipping the |
| 515 | * stages we have already looked at in this invocation of this |
| 516 | * function. |
| 517 | */ |
| 518 | static int check_one_conflict(struct index_state *istate, int i, int *type) |
| 519 | { |
| 520 | const struct cache_entry *e = istate->cache[i]; |
| 521 | |
| 522 | if (!ce_stage(e)) { |
| 523 | *type = RESOLVED; |
| 524 | return i + 1; |
| 525 | } |
| 526 | |
| 527 | *type = PUNTED; |
| 528 | while (i < istate->cache_nr && ce_stage(istate->cache[i]) == 1) |
| 529 | i++; |
| 530 | |
| 531 | /* Only handle regular files with both stages #2 and #3 */ |
| 532 | if (i + 1 < istate->cache_nr) { |
| 533 | const struct cache_entry *e2 = istate->cache[i]; |
| 534 | const struct cache_entry *e3 = istate->cache[i + 1]; |
| 535 | if (ce_stage(e2) == 2 && |
| 536 | ce_stage(e3) == 3 && |
| 537 | ce_same_name(e, e3) && |
| 538 | S_ISREG(e2->ce_mode) && |
| 539 | S_ISREG(e3->ce_mode)) |
| 540 | *type = THREE_STAGED; |
| 541 | } |
| 542 | |
| 543 | /* Skip the entries with the same name */ |
| 544 | while (i < istate->cache_nr && ce_same_name(e, istate->cache[i])) |
| 545 | i++; |
| 546 | return i; |
| 547 | } |
| 548 | |
| 549 | /* |
| 550 | * Scan the index and find paths that have conflicts that rerere can |
| 551 | * handle, i.e. the ones that have both stages #2 and #3. |
| 552 | * |
| 553 | * NEEDSWORK: we do not record or replay a previous "resolve by |
| 554 | * deletion" for a delete-modify conflict, as that is inherently risky |
| 555 | * without knowing what modification is being discarded. The only |
| 556 | * safe case, i.e. both side doing the deletion and modification that |
| 557 | * are identical to the previous round, might want to be handled, |
| 558 | * though. |
| 559 | */ |
| 560 | static int find_conflict(struct repository *r, struct string_list *conflict) |
| 561 | { |
| 562 | int i; |
| 563 | |
| 564 | if (repo_read_index(r) < 0) |
| 565 | return error(_("index file corrupt")); |
| 566 | |
| 567 | for (i = 0; i < r->index->cache_nr;) { |
| 568 | int conflict_type; |
| 569 | const struct cache_entry *e = r->index->cache[i]; |
| 570 | i = check_one_conflict(r->index, i, &conflict_type); |
| 571 | if (conflict_type == THREE_STAGED) |
| 572 | string_list_insert(conflict, (const char *)e->name); |
| 573 | } |
| 574 | return 0; |
| 575 | } |
| 576 | |
| 577 | /* |
| 578 | * The merge_rr list is meant to hold outstanding conflicted paths |
| 579 | * that rerere could handle. Abuse the list by adding other types of |
| 580 | * entries to allow the caller to show "rerere remaining". |
| 581 | * |
| 582 | * - Conflicted paths that rerere does not handle are added |
| 583 | * - Conflicted paths that have been resolved are marked as such |
| 584 | * by storing RERERE_RESOLVED to .util field (where conflict ID |
| 585 | * is expected to be stored). |
| 586 | * |
| 587 | * Do *not* write MERGE_RR file out after calling this function. |
| 588 | * |
| 589 | * NEEDSWORK: we may want to fix the caller that implements "rerere |
| 590 | * remaining" to do this without abusing merge_rr. |
| 591 | */ |
| 592 | int rerere_remaining(struct repository *r, struct string_list *merge_rr) |
| 593 | { |
| 594 | int i; |
| 595 | |
| 596 | if (setup_rerere(r, merge_rr, RERERE_READONLY)) |
| 597 | return 0; |
| 598 | if (repo_read_index(r) < 0) |
| 599 | return error(_("index file corrupt")); |
| 600 | |
| 601 | for (i = 0; i < r->index->cache_nr;) { |
| 602 | int conflict_type; |
| 603 | const struct cache_entry *e = r->index->cache[i]; |
| 604 | i = check_one_conflict(r->index, i, &conflict_type); |
| 605 | if (conflict_type == PUNTED) |
| 606 | string_list_insert(merge_rr, (const char *)e->name); |
| 607 | else if (conflict_type == RESOLVED) { |
| 608 | struct string_list_item *it; |
| 609 | it = string_list_lookup(merge_rr, (const char *)e->name); |
| 610 | if (it) { |
| 611 | free_rerere_id(it); |
| 612 | it->util = RERERE_RESOLVED; |
| 613 | } |
| 614 | } |
| 615 | } |
| 616 | return 0; |
| 617 | } |
| 618 | |
| 619 | /* |
| 620 | * Try using the given conflict resolution "ID" to see |
| 621 | * if that recorded conflict resolves cleanly what we |
| 622 | * got in the "cur". |
| 623 | */ |
| 624 | static int try_merge(struct index_state *istate, |
| 625 | const struct rerere_id *id, const char *path, |
| 626 | mmfile_t *cur, mmbuffer_t *result) |
| 627 | { |
| 628 | enum ll_merge_result ret; |
| 629 | mmfile_t base = {NULL, 0}, other = {NULL, 0}; |
| 630 | struct strbuf buf = STRBUF_INIT; |
| 631 | |
| 632 | if (read_mmfile(&base, rerere_path(&buf, id, "preimage")) || |
| 633 | read_mmfile(&other, rerere_path(&buf, id, "postimage"))) { |
| 634 | ret = LL_MERGE_CONFLICT; |
| 635 | } else { |
| 636 | /* |
| 637 | * A three-way merge. Note that this honors user-customizable |
| 638 | * low-level merge driver settings. |
| 639 | */ |
| 640 | ret = ll_merge(result, path, &base, NULL, cur, "", &other, "", |
| 641 | istate, NULL); |
| 642 | } |
| 643 | |
| 644 | strbuf_release(&buf); |
| 645 | free(base.ptr); |
| 646 | free(other.ptr); |
| 647 | |
| 648 | return ret; |
| 649 | } |
| 650 | |
| 651 | /* |
| 652 | * Find the conflict identified by "id"; the change between its |
| 653 | * "preimage" (i.e. a previous contents with conflict markers) and its |
| 654 | * "postimage" (i.e. the corresponding contents with conflicts |
| 655 | * resolved) may apply cleanly to the contents stored in "path", i.e. |
| 656 | * the conflict this time around. |
| 657 | * |
| 658 | * Returns 0 for successful replay of recorded resolution, or non-zero |
| 659 | * for failure. |
| 660 | */ |
| 661 | static int merge(struct index_state *istate, const struct rerere_id *id, const char *path) |
| 662 | { |
| 663 | FILE *f; |
| 664 | int ret; |
| 665 | struct strbuf buf = STRBUF_INIT; |
| 666 | mmfile_t cur = {NULL, 0}; |
| 667 | mmbuffer_t result = {NULL, 0}; |
| 668 | |
| 669 | /* |
| 670 | * Normalize the conflicts in path and write it out to |
| 671 | * "thisimage" temporary file. |
| 672 | */ |
| 673 | if ((handle_file(istate, path, NULL, rerere_path(&buf, id, "thisimage")) < 0) || |
| 674 | read_mmfile(&cur, rerere_path(&buf, id, "thisimage"))) { |
| 675 | ret = 1; |
| 676 | goto out; |
| 677 | } |
| 678 | |
| 679 | ret = try_merge(istate, id, path, &cur, &result); |
| 680 | if (ret) |
| 681 | goto out; |
| 682 | |
| 683 | /* |
| 684 | * A successful replay of recorded resolution. |
| 685 | * Mark that "postimage" was used to help gc. |
| 686 | */ |
| 687 | if (utime(rerere_path(&buf, id, "postimage"), NULL) < 0) |
| 688 | warning_errno(_("failed utime() on '%s'"), |
| 689 | rerere_path(&buf, id, "postimage")); |
| 690 | |
| 691 | /* Update "path" with the resolution */ |
| 692 | f = fopen(path, "w"); |
| 693 | if (!f) |
| 694 | return error_errno(_("could not open '%s'"), path); |
| 695 | if (fwrite(result.ptr, result.size, 1, f) != 1) |
| 696 | error_errno(_("could not write '%s'"), path); |
| 697 | if (fclose(f)) |
| 698 | return error_errno(_("writing '%s' failed"), path); |
| 699 | |
| 700 | out: |
| 701 | free(cur.ptr); |
| 702 | free(result.ptr); |
| 703 | strbuf_release(&buf); |
| 704 | |
| 705 | return ret; |
| 706 | } |
| 707 | |
| 708 | static void update_paths(struct repository *r, struct string_list *update) |
| 709 | { |
| 710 | struct lock_file index_lock = LOCK_INIT; |
| 711 | int i; |
| 712 | |
| 713 | repo_hold_locked_index(r, &index_lock, LOCK_DIE_ON_ERROR); |
| 714 | |
| 715 | for (i = 0; i < update->nr; i++) { |
| 716 | struct string_list_item *item = &update->items[i]; |
| 717 | if (add_file_to_index(r->index, item->string, 0)) |
| 718 | exit(128); |
| 719 | fprintf_ln(stderr, _("Staged '%s' using previous resolution."), |
| 720 | item->string); |
| 721 | } |
| 722 | |
| 723 | if (write_locked_index(r->index, &index_lock, |
| 724 | COMMIT_LOCK | SKIP_IF_UNCHANGED)) |
| 725 | die(_("unable to write new index file")); |
| 726 | } |
| 727 | |
| 728 | static void remove_variant(struct rerere_id *id) |
| 729 | { |
| 730 | struct strbuf buf = STRBUF_INIT; |
| 731 | unlink_or_warn(rerere_path(&buf, id, "postimage")); |
| 732 | unlink_or_warn(rerere_path(&buf, id, "preimage")); |
| 733 | id->collection->status[id->variant] = 0; |
| 734 | strbuf_release(&buf); |
| 735 | } |
| 736 | |
| 737 | /* |
| 738 | * The path indicated by rr_item may still have conflict for which we |
| 739 | * have a recorded resolution, in which case replay it and optionally |
| 740 | * update it. Or it may have been resolved by the user and we may |
| 741 | * only have the preimage for that conflict, in which case the result |
| 742 | * needs to be recorded as a resolution in a postimage file. |
| 743 | */ |
| 744 | static void do_rerere_one_path(struct index_state *istate, |
| 745 | struct string_list_item *rr_item, |
| 746 | struct string_list *update) |
| 747 | { |
| 748 | const char *path = rr_item->string; |
| 749 | struct rerere_id *id = rr_item->util; |
| 750 | struct rerere_dir *rr_dir = id->collection; |
| 751 | struct strbuf buf = STRBUF_INIT; |
| 752 | int variant; |
| 753 | |
| 754 | variant = id->variant; |
| 755 | |
| 756 | /* Has the user resolved it already? */ |
| 757 | if (variant >= 0) { |
| 758 | if (!handle_file(istate, path, NULL, NULL)) { |
| 759 | copy_file(the_repository, rerere_path(&buf, id, "postimage"), path, 0666); |
| 760 | id->collection->status[variant] |= RR_HAS_POSTIMAGE; |
| 761 | fprintf_ln(stderr, _("Recorded resolution for '%s'."), path); |
| 762 | free_rerere_id(rr_item); |
| 763 | rr_item->util = NULL; |
| 764 | goto out; |
| 765 | } |
| 766 | /* |
| 767 | * There may be other variants that can cleanly |
| 768 | * replay. Try them and update the variant number for |
| 769 | * this one. |
| 770 | */ |
| 771 | } |
| 772 | |
| 773 | /* Does any existing resolution apply cleanly? */ |
| 774 | for (variant = 0; variant < rr_dir->status_nr; variant++) { |
| 775 | const int both = RR_HAS_PREIMAGE | RR_HAS_POSTIMAGE; |
| 776 | struct rerere_id vid = *id; |
| 777 | |
| 778 | if ((rr_dir->status[variant] & both) != both) |
| 779 | continue; |
| 780 | |
| 781 | vid.variant = variant; |
| 782 | if (merge(istate, &vid, path)) |
| 783 | continue; /* failed to replay */ |
| 784 | |
| 785 | /* |
| 786 | * If there already is a different variant that applies |
| 787 | * cleanly, there is no point maintaining our own variant. |
| 788 | */ |
| 789 | if (0 <= id->variant && id->variant != variant) |
| 790 | remove_variant(id); |
| 791 | |
| 792 | if (rerere_autoupdate) |
| 793 | string_list_insert(update, path); |
| 794 | else |
| 795 | fprintf_ln(stderr, |
| 796 | _("Resolved '%s' using previous resolution."), |
| 797 | path); |
| 798 | free_rerere_id(rr_item); |
| 799 | rr_item->util = NULL; |
| 800 | goto out; |
| 801 | } |
| 802 | |
| 803 | /* None of the existing one applies; we need a new variant */ |
| 804 | assign_variant(id); |
| 805 | |
| 806 | variant = id->variant; |
| 807 | handle_file(istate, path, NULL, rerere_path(&buf, id, "preimage")); |
| 808 | if (id->collection->status[variant] & RR_HAS_POSTIMAGE) { |
| 809 | const char *path = rerere_path(&buf, id, "postimage"); |
| 810 | if (unlink(path)) |
| 811 | die_errno(_("cannot unlink stray '%s'"), path); |
| 812 | id->collection->status[variant] &= ~RR_HAS_POSTIMAGE; |
| 813 | } |
| 814 | id->collection->status[variant] |= RR_HAS_PREIMAGE; |
| 815 | fprintf_ln(stderr, _("Recorded preimage for '%s'"), path); |
| 816 | |
| 817 | out: |
| 818 | strbuf_release(&buf); |
| 819 | } |
| 820 | |
| 821 | static int do_plain_rerere(struct repository *r, |
| 822 | struct string_list *rr, int fd) |
| 823 | { |
| 824 | struct string_list conflict = STRING_LIST_INIT_DUP; |
| 825 | struct string_list update = STRING_LIST_INIT_DUP; |
| 826 | struct strbuf buf = STRBUF_INIT; |
| 827 | int i; |
| 828 | |
| 829 | find_conflict(r, &conflict); |
| 830 | |
| 831 | /* |
| 832 | * MERGE_RR records paths with conflicts immediately after |
| 833 | * merge failed. Some of the conflicted paths might have been |
| 834 | * hand resolved in the working tree since then, but the |
| 835 | * initial run would catch all and register their preimages. |
| 836 | */ |
| 837 | for (i = 0; i < conflict.nr; i++) { |
| 838 | struct rerere_id *id; |
| 839 | unsigned char hash[GIT_MAX_RAWSZ]; |
| 840 | const char *path = conflict.items[i].string; |
| 841 | int ret; |
| 842 | |
| 843 | /* |
| 844 | * Ask handle_file() to scan and assign a |
| 845 | * conflict ID. No need to write anything out |
| 846 | * yet. |
| 847 | */ |
| 848 | ret = handle_file(r->index, path, hash, NULL); |
| 849 | if (ret != 0 && string_list_has_string(rr, path)) { |
| 850 | remove_variant(string_list_lookup(rr, path)->util); |
| 851 | string_list_remove(rr, path, 1); |
| 852 | } |
| 853 | if (ret < 1) |
| 854 | continue; |
| 855 | |
| 856 | id = new_rerere_id(hash); |
| 857 | string_list_insert(rr, path)->util = id; |
| 858 | |
| 859 | /* Ensure that the directory exists. */ |
| 860 | safe_create_dir_in_gitdir(the_repository, rerere_path(&buf, id, NULL)); |
| 861 | } |
| 862 | |
| 863 | for (i = 0; i < rr->nr; i++) |
| 864 | do_rerere_one_path(r->index, &rr->items[i], &update); |
| 865 | |
| 866 | if (update.nr) |
| 867 | update_paths(r, &update); |
| 868 | |
| 869 | string_list_clear(&conflict, 0); |
| 870 | string_list_clear(&update, 0); |
| 871 | strbuf_release(&buf); |
| 872 | return write_rr(rr, fd); |
| 873 | } |
| 874 | |
| 875 | static void git_rerere_config(void) |
| 876 | { |
| 877 | repo_config_get_bool(the_repository, "rerere.enabled", &rerere_enabled); |
| 878 | repo_config_get_bool(the_repository, "rerere.autoupdate", &rerere_autoupdate); |
| 879 | repo_config(the_repository, git_default_config, NULL); |
| 880 | } |
| 881 | |
| 882 | static GIT_PATH_FUNC(git_path_rr_cache, "rr-cache") |
| 883 | |
| 884 | static int is_rerere_enabled(void) |
| 885 | { |
| 886 | int rr_cache_exists; |
| 887 | |
| 888 | if (!rerere_enabled) |
| 889 | return 0; |
| 890 | |
| 891 | rr_cache_exists = is_directory(git_path_rr_cache()); |
| 892 | if (rerere_enabled < 0) |
| 893 | return rr_cache_exists; |
| 894 | |
| 895 | if (!rr_cache_exists && |
| 896 | safe_create_dir_in_gitdir(the_repository, git_path_rr_cache())) |
| 897 | die(_("could not create directory '%s'"), git_path_rr_cache()); |
| 898 | return 1; |
| 899 | } |
| 900 | |
| 901 | int setup_rerere(struct repository *r, struct string_list *merge_rr, int flags) |
| 902 | { |
| 903 | int fd; |
| 904 | |
| 905 | git_rerere_config(); |
| 906 | if (!is_rerere_enabled()) |
| 907 | return -1; |
| 908 | |
| 909 | if (flags & (RERERE_AUTOUPDATE|RERERE_NOAUTOUPDATE)) |
| 910 | rerere_autoupdate = !!(flags & RERERE_AUTOUPDATE); |
| 911 | if (flags & RERERE_READONLY) |
| 912 | fd = 0; |
| 913 | else |
| 914 | fd = repo_hold_lock_file_for_update(r, &write_lock, |
| 915 | git_path_merge_rr(r), |
| 916 | LOCK_DIE_ON_ERROR); |
| 917 | read_rr(r, merge_rr); |
| 918 | return fd; |
| 919 | } |
| 920 | |
| 921 | /* |
| 922 | * The main entry point that is called internally from codepaths that |
| 923 | * perform mergy operations, possibly leaving conflicted index entries |
| 924 | * and working tree files. |
| 925 | */ |
| 926 | int repo_rerere(struct repository *r, int flags) |
| 927 | { |
| 928 | struct string_list merge_rr = STRING_LIST_INIT_DUP; |
| 929 | int fd, status; |
| 930 | |
| 931 | fd = setup_rerere(r, &merge_rr, flags); |
| 932 | if (fd < 0) |
| 933 | return 0; |
| 934 | status = do_plain_rerere(r, &merge_rr, fd); |
| 935 | free_rerere_dirs(); |
| 936 | string_list_clear(&merge_rr, 1); |
| 937 | return status; |
| 938 | } |
| 939 | |
| 940 | /* |
| 941 | * Subclass of rerere_io that reads from an in-core buffer that is a |
| 942 | * strbuf |
| 943 | */ |
| 944 | struct rerere_io_mem { |
| 945 | struct rerere_io io; |
| 946 | struct strbuf input; |
| 947 | }; |
| 948 | |
| 949 | /* |
| 950 | * ... and its getline() method implementation |
| 951 | */ |
| 952 | static int rerere_mem_getline(struct strbuf *sb, struct rerere_io *io_) |
| 953 | { |
| 954 | struct rerere_io_mem *io = (struct rerere_io_mem *)io_; |
| 955 | char *ep; |
| 956 | size_t len; |
| 957 | |
| 958 | strbuf_release(sb); |
| 959 | if (!io->input.len) |
| 960 | return -1; |
| 961 | ep = memchr(io->input.buf, '\n', io->input.len); |
| 962 | if (!ep) |
| 963 | ep = io->input.buf + io->input.len; |
| 964 | else if (*ep == '\n') |
| 965 | ep++; |
| 966 | len = ep - io->input.buf; |
| 967 | strbuf_add(sb, io->input.buf, len); |
| 968 | strbuf_remove(&io->input, 0, len); |
| 969 | return 0; |
| 970 | } |
| 971 | |
| 972 | static int handle_cache(struct index_state *istate, |
| 973 | const char *path, unsigned char *hash, const char *output) |
| 974 | { |
| 975 | mmfile_t mmfile[3] = {{NULL}}; |
| 976 | mmbuffer_t result = {NULL, 0}; |
| 977 | const struct cache_entry *ce; |
| 978 | int pos, len, i, has_conflicts; |
| 979 | struct rerere_io_mem io; |
| 980 | int marker_size = ll_merge_marker_size(istate, path); |
| 981 | |
| 982 | /* |
| 983 | * Reproduce the conflicted merge in-core |
| 984 | */ |
| 985 | len = strlen(path); |
| 986 | pos = index_name_pos(istate, path, len); |
| 987 | if (0 <= pos) |
| 988 | return -1; |
| 989 | pos = -pos - 1; |
| 990 | |
| 991 | while (pos < istate->cache_nr) { |
| 992 | enum object_type type; |
| 993 | size_t size; |
| 994 | |
| 995 | ce = istate->cache[pos++]; |
| 996 | if (ce_namelen(ce) != len || memcmp(ce->name, path, len)) |
| 997 | break; |
| 998 | i = ce_stage(ce) - 1; |
| 999 | if (!mmfile[i].ptr) { |
| 1000 | mmfile[i].ptr = odb_read_object(the_repository->objects, |
| 1001 | &ce->oid, &type, &size); |
| 1002 | if (!mmfile[i].ptr) |
| 1003 | die(_("unable to read %s"), |
| 1004 | oid_to_hex(&ce->oid)); |
| 1005 | mmfile[i].size = size; |
| 1006 | } |
| 1007 | } |
| 1008 | for (i = 0; i < 3; i++) |
| 1009 | if (!mmfile[i].ptr && !mmfile[i].size) |
| 1010 | mmfile[i].ptr = xstrdup(""); |
| 1011 | |
| 1012 | /* |
| 1013 | * NEEDSWORK: handle conflicts from merges with |
| 1014 | * merge.renormalize set, too? |
| 1015 | */ |
| 1016 | ll_merge(&result, path, &mmfile[0], NULL, |
| 1017 | &mmfile[1], "ours", |
| 1018 | &mmfile[2], "theirs", |
| 1019 | istate, NULL); |
| 1020 | for (i = 0; i < 3; i++) |
| 1021 | free(mmfile[i].ptr); |
| 1022 | |
| 1023 | memset(&io, 0, sizeof(io)); |
| 1024 | io.io.getline = rerere_mem_getline; |
| 1025 | if (output) |
| 1026 | io.io.output = fopen(output, "w"); |
| 1027 | else |
| 1028 | io.io.output = NULL; |
| 1029 | strbuf_init(&io.input, 0); |
| 1030 | strbuf_attach(&io.input, result.ptr, result.size, result.size); |
| 1031 | |
| 1032 | /* |
| 1033 | * Grab the conflict ID and optionally write the original |
| 1034 | * contents with conflict markers out. |
| 1035 | */ |
| 1036 | has_conflicts = handle_path(hash, (struct rerere_io *)&io, marker_size); |
| 1037 | strbuf_release(&io.input); |
| 1038 | if (io.io.output) |
| 1039 | fclose(io.io.output); |
| 1040 | return has_conflicts; |
| 1041 | } |
| 1042 | |
| 1043 | static int rerere_forget_one_path(struct index_state *istate, |
| 1044 | const char *path, |
| 1045 | struct string_list *rr) |
| 1046 | { |
| 1047 | const char *filename; |
| 1048 | struct rerere_id *id; |
| 1049 | unsigned char hash[GIT_MAX_RAWSZ]; |
| 1050 | int ret; |
| 1051 | struct strbuf buf = STRBUF_INIT; |
| 1052 | struct string_list_item *item; |
| 1053 | |
| 1054 | /* |
| 1055 | * Recreate the original conflict from the stages in the |
| 1056 | * index and compute the conflict ID |
| 1057 | */ |
| 1058 | ret = handle_cache(istate, path, hash, NULL); |
| 1059 | if (ret < 1) |
| 1060 | return error(_("could not parse conflict hunks in '%s'"), path); |
| 1061 | |
| 1062 | /* Nuke the recorded resolution for the conflict */ |
| 1063 | id = new_rerere_id(hash); |
| 1064 | |
| 1065 | for (id->variant = 0; |
| 1066 | id->variant < id->collection->status_nr; |
| 1067 | id->variant++) { |
| 1068 | mmfile_t cur = { NULL, 0 }; |
| 1069 | mmbuffer_t result = {NULL, 0}; |
| 1070 | int cleanly_resolved; |
| 1071 | |
| 1072 | if (!has_rerere_resolution(id)) |
| 1073 | continue; |
| 1074 | |
| 1075 | handle_cache(istate, path, hash, rerere_path(&buf, id, "thisimage")); |
| 1076 | if (read_mmfile(&cur, rerere_path(&buf, id, "thisimage"))) { |
| 1077 | free(cur.ptr); |
| 1078 | error(_("failed to update conflicted state in '%s'"), path); |
| 1079 | goto fail_exit; |
| 1080 | } |
| 1081 | cleanly_resolved = !try_merge(istate, id, path, &cur, &result); |
| 1082 | free(result.ptr); |
| 1083 | free(cur.ptr); |
| 1084 | if (cleanly_resolved) |
| 1085 | break; |
| 1086 | } |
| 1087 | |
| 1088 | if (id->collection->status_nr <= id->variant) { |
| 1089 | error(_("no remembered resolution for '%s'"), path); |
| 1090 | goto fail_exit; |
| 1091 | } |
| 1092 | |
| 1093 | filename = rerere_path(&buf, id, "postimage"); |
| 1094 | if (unlink(filename)) { |
| 1095 | if (errno == ENOENT) |
| 1096 | error(_("no remembered resolution for '%s'"), path); |
| 1097 | else |
| 1098 | error_errno(_("cannot unlink '%s'"), filename); |
| 1099 | goto fail_exit; |
| 1100 | } |
| 1101 | |
| 1102 | /* |
| 1103 | * Update the preimage so that the user can resolve the |
| 1104 | * conflict in the working tree, run us again to record |
| 1105 | * the postimage. |
| 1106 | */ |
| 1107 | handle_cache(istate, path, hash, rerere_path(&buf, id, "preimage")); |
| 1108 | fprintf_ln(stderr, _("Updated preimage for '%s'"), path); |
| 1109 | |
| 1110 | /* |
| 1111 | * And remember that we can record resolution for this |
| 1112 | * conflict when the user is done. |
| 1113 | */ |
| 1114 | item = string_list_insert(rr, path); |
| 1115 | free_rerere_id(item); |
| 1116 | item->util = id; |
| 1117 | fprintf(stderr, _("Forgot resolution for '%s'\n"), path); |
| 1118 | strbuf_release(&buf); |
| 1119 | return 0; |
| 1120 | |
| 1121 | fail_exit: |
| 1122 | strbuf_release(&buf); |
| 1123 | free(id); |
| 1124 | return -1; |
| 1125 | } |
| 1126 | |
| 1127 | int rerere_forget(struct repository *r, struct pathspec *pathspec) |
| 1128 | { |
| 1129 | int i, fd, ret; |
| 1130 | struct string_list conflict = STRING_LIST_INIT_DUP; |
| 1131 | struct string_list merge_rr = STRING_LIST_INIT_DUP; |
| 1132 | |
| 1133 | if (repo_read_index(r) < 0) |
| 1134 | return error(_("index file corrupt")); |
| 1135 | |
| 1136 | fd = setup_rerere(r, &merge_rr, RERERE_NOAUTOUPDATE); |
| 1137 | if (fd < 0) |
| 1138 | return 0; |
| 1139 | |
| 1140 | /* |
| 1141 | * The paths may have been resolved (incorrectly); |
| 1142 | * recover the original conflicted state and then |
| 1143 | * find the conflicted paths. |
| 1144 | */ |
| 1145 | unmerge_index(r->index, pathspec, 0); |
| 1146 | find_conflict(r, &conflict); |
| 1147 | for (i = 0; i < conflict.nr; i++) { |
| 1148 | struct string_list_item *it = &conflict.items[i]; |
| 1149 | if (!match_pathspec(r->index, pathspec, it->string, |
| 1150 | strlen(it->string), 0, NULL, 0)) |
| 1151 | continue; |
| 1152 | rerere_forget_one_path(r->index, it->string, &merge_rr); |
| 1153 | } |
| 1154 | |
| 1155 | ret = write_rr(&merge_rr, fd); |
| 1156 | |
| 1157 | string_list_clear(&conflict, 0); |
| 1158 | string_list_clear(&merge_rr, 1); |
| 1159 | return ret; |
| 1160 | } |
| 1161 | |
| 1162 | /* |
| 1163 | * Garbage collection support |
| 1164 | */ |
| 1165 | |
| 1166 | static timestamp_t rerere_created_at(struct rerere_id *id) |
| 1167 | { |
| 1168 | struct strbuf buf = STRBUF_INIT; |
| 1169 | struct stat st; |
| 1170 | timestamp_t ret; |
| 1171 | |
| 1172 | ret = stat(rerere_path(&buf, id, "preimage"), &st) ? (time_t) 0 : st.st_mtime; |
| 1173 | |
| 1174 | strbuf_release(&buf); |
| 1175 | return ret; |
| 1176 | } |
| 1177 | |
| 1178 | static timestamp_t rerere_last_used_at(struct rerere_id *id) |
| 1179 | { |
| 1180 | struct strbuf buf = STRBUF_INIT; |
| 1181 | struct stat st; |
| 1182 | timestamp_t ret; |
| 1183 | |
| 1184 | ret = stat(rerere_path(&buf, id, "postimage"), &st) ? (time_t) 0 : st.st_mtime; |
| 1185 | |
| 1186 | strbuf_release(&buf); |
| 1187 | return ret; |
| 1188 | } |
| 1189 | |
| 1190 | /* |
| 1191 | * Remove the recorded resolution for a given conflict ID |
| 1192 | */ |
| 1193 | static void unlink_rr_item(struct rerere_id *id) |
| 1194 | { |
| 1195 | struct strbuf buf = STRBUF_INIT; |
| 1196 | unlink_or_warn(rerere_path(&buf, id, "thisimage")); |
| 1197 | remove_variant(id); |
| 1198 | id->collection->status[id->variant] = 0; |
| 1199 | strbuf_release(&buf); |
| 1200 | } |
| 1201 | |
| 1202 | static void prune_one(struct rerere_id *id, |
| 1203 | timestamp_t cutoff_resolve, timestamp_t cutoff_noresolve) |
| 1204 | { |
| 1205 | timestamp_t then; |
| 1206 | timestamp_t cutoff; |
| 1207 | |
| 1208 | then = rerere_last_used_at(id); |
| 1209 | if (then) |
| 1210 | cutoff = cutoff_resolve; |
| 1211 | else { |
| 1212 | then = rerere_created_at(id); |
| 1213 | if (!then) |
| 1214 | return; |
| 1215 | cutoff = cutoff_noresolve; |
| 1216 | } |
| 1217 | if (then < cutoff) |
| 1218 | unlink_rr_item(id); |
| 1219 | } |
| 1220 | |
| 1221 | /* Does the basename in "path" look plausibly like an rr-cache entry? */ |
| 1222 | static int is_rr_cache_dirname(const char *path) |
| 1223 | { |
| 1224 | struct object_id oid; |
| 1225 | const char *end; |
| 1226 | return !parse_oid_hex(path, &oid, &end) && !*end; |
| 1227 | } |
| 1228 | |
| 1229 | void rerere_gc(struct repository *r, struct string_list *rr) |
| 1230 | { |
| 1231 | struct string_list to_remove = STRING_LIST_INIT_DUP; |
| 1232 | DIR *dir; |
| 1233 | struct dirent *e; |
| 1234 | int i; |
| 1235 | timestamp_t now = time(NULL); |
| 1236 | timestamp_t cutoff_noresolve = now - 15 * 86400; |
| 1237 | timestamp_t cutoff_resolve = now - 60 * 86400; |
| 1238 | struct strbuf buf = STRBUF_INIT; |
| 1239 | |
| 1240 | if (setup_rerere(r, rr, 0) < 0) |
| 1241 | return; |
| 1242 | |
| 1243 | repo_config_get_expiry_in_days(the_repository, "gc.rerereresolved", |
| 1244 | &cutoff_resolve, now); |
| 1245 | repo_config_get_expiry_in_days(the_repository, "gc.rerereunresolved", |
| 1246 | &cutoff_noresolve, now); |
| 1247 | repo_config(the_repository, git_default_config, NULL); |
| 1248 | dir = opendir(repo_git_path_replace(the_repository, &buf, "rr-cache")); |
| 1249 | if (!dir) |
| 1250 | die_errno(_("unable to open rr-cache directory")); |
| 1251 | /* Collect stale conflict IDs ... */ |
| 1252 | while ((e = readdir_skip_dot_and_dotdot(dir))) { |
| 1253 | struct rerere_dir *rr_dir; |
| 1254 | struct rerere_id id; |
| 1255 | int now_empty; |
| 1256 | |
| 1257 | if (!is_rr_cache_dirname(e->d_name)) |
| 1258 | continue; /* or should we remove e->d_name? */ |
| 1259 | |
| 1260 | rr_dir = find_rerere_dir(e->d_name); |
| 1261 | |
| 1262 | now_empty = 1; |
| 1263 | for (id.variant = 0, id.collection = rr_dir; |
| 1264 | id.variant < id.collection->status_nr; |
| 1265 | id.variant++) { |
| 1266 | prune_one(&id, cutoff_resolve, cutoff_noresolve); |
| 1267 | if (id.collection->status[id.variant]) |
| 1268 | now_empty = 0; |
| 1269 | } |
| 1270 | if (now_empty) |
| 1271 | string_list_append(&to_remove, e->d_name); |
| 1272 | } |
| 1273 | closedir(dir); |
| 1274 | |
| 1275 | /* ... and then remove the empty directories */ |
| 1276 | for (i = 0; i < to_remove.nr; i++) |
| 1277 | rmdir(repo_git_path_replace(the_repository, &buf, |
| 1278 | "rr-cache/%s", to_remove.items[i].string)); |
| 1279 | |
| 1280 | string_list_clear(&to_remove, 0); |
| 1281 | rollback_lock_file(&write_lock); |
| 1282 | strbuf_release(&buf); |
| 1283 | } |
| 1284 | |
| 1285 | /* |
| 1286 | * During a conflict resolution, after "rerere" recorded the |
| 1287 | * preimages, abandon them if the user did not resolve them or |
| 1288 | * record their resolutions. And drop $GIT_DIR/MERGE_RR. |
| 1289 | * |
| 1290 | * NEEDSWORK: shouldn't we be calling this from "reset --hard"? |
| 1291 | */ |
| 1292 | void rerere_clear(struct repository *r, struct string_list *merge_rr) |
| 1293 | { |
| 1294 | int i; |
| 1295 | |
| 1296 | if (setup_rerere(r, merge_rr, 0) < 0) |
| 1297 | return; |
| 1298 | |
| 1299 | for (i = 0; i < merge_rr->nr; i++) { |
| 1300 | struct rerere_id *id = merge_rr->items[i].util; |
| 1301 | struct strbuf buf = STRBUF_INIT; |
| 1302 | |
| 1303 | if (!has_rerere_resolution(id)) { |
| 1304 | unlink_rr_item(id); |
| 1305 | rmdir(rerere_path(&buf, id, NULL)); |
| 1306 | } |
| 1307 | |
| 1308 | strbuf_release(&buf); |
| 1309 | } |
| 1310 | unlink_or_warn(git_path_merge_rr(r)); |
| 1311 | rollback_lock_file(&write_lock); |
| 1312 | } |