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