]>
Commit | Line | Data |
---|---|---|
5b2fd956 | 1 | #include "cache.h" |
697cc8ef | 2 | #include "lockfile.h" |
c455c87c | 3 | #include "string-list.h" |
5b2fd956 | 4 | #include "rerere.h" |
5b2fd956 | 5 | #include "xdiff-interface.h" |
dea4562b JH |
6 | #include "dir.h" |
7 | #include "resolve-undo.h" | |
8 | #include "ll-merge.h" | |
8588567c | 9 | #include "attr.h" |
01a10b0a | 10 | #include "pathspec.h" |
1869bbe1 | 11 | #include "sha1-lookup.h" |
5b2fd956 | 12 | |
ac49f5ca MZ |
13 | #define RESOLVED 0 |
14 | #define PUNTED 1 | |
15 | #define THREE_STAGED 2 | |
16 | void *RERERE_RESOLVED = &RERERE_RESOLVED; | |
17 | ||
5b2fd956 SB |
18 | /* if rerere_enabled == -1, fall back to detection of .git/rr-cache */ |
19 | static int rerere_enabled = -1; | |
20 | ||
21 | /* automatically update cleanly resolved paths to the index */ | |
22 | static int rerere_autoupdate; | |
23 | ||
24 | static char *merge_rr_path; | |
25 | ||
1869bbe1 JH |
26 | static int rerere_dir_nr; |
27 | static int rerere_dir_alloc; | |
28 | ||
2c7929b1 JH |
29 | #define RR_HAS_POSTIMAGE 1 |
30 | #define RR_HAS_PREIMAGE 2 | |
1869bbe1 JH |
31 | static struct rerere_dir { |
32 | unsigned char sha1[20]; | |
2c7929b1 | 33 | unsigned char status; |
1869bbe1 JH |
34 | } **rerere_dir; |
35 | ||
36 | static void free_rerere_dirs(void) | |
37 | { | |
38 | int i; | |
39 | for (i = 0; i < rerere_dir_nr; i++) | |
40 | free(rerere_dir[i]); | |
41 | free(rerere_dir); | |
42 | rerere_dir_nr = rerere_dir_alloc = 0; | |
43 | rerere_dir = NULL; | |
44 | } | |
45 | ||
1d51eced | 46 | static void free_rerere_id(struct string_list_item *item) |
5b2fd956 | 47 | { |
1d51eced JH |
48 | free(item->util); |
49 | } | |
50 | ||
51 | static const char *rerere_id_hex(const struct rerere_id *id) | |
52 | { | |
1869bbe1 | 53 | return sha1_to_hex(id->collection->sha1); |
1d51eced JH |
54 | } |
55 | ||
56 | const char *rerere_path(const struct rerere_id *id, const char *file) | |
57 | { | |
58 | if (!file) | |
59 | return git_path("rr-cache/%s", rerere_id_hex(id)); | |
60 | ||
61 | return git_path("rr-cache/%s/%s", rerere_id_hex(id), file); | |
5b2fd956 SB |
62 | } |
63 | ||
2c7929b1 JH |
64 | static int is_rr_file(const char *name, const char *filename) |
65 | { | |
66 | return !strcmp(name, filename); | |
67 | } | |
68 | ||
69 | static void scan_rerere_dir(struct rerere_dir *rr_dir) | |
70 | { | |
71 | struct dirent *de; | |
72 | DIR *dir = opendir(git_path("rr-cache/%s", sha1_to_hex(rr_dir->sha1))); | |
73 | ||
74 | if (!dir) | |
75 | return; | |
76 | while ((de = readdir(dir)) != NULL) { | |
77 | if (is_rr_file(de->d_name, "postimage")) | |
78 | rr_dir->status |= RR_HAS_POSTIMAGE; | |
79 | else if (is_rr_file(de->d_name, "preimage")) | |
80 | rr_dir->status |= RR_HAS_PREIMAGE; | |
81 | } | |
82 | closedir(dir); | |
83 | } | |
84 | ||
1869bbe1 JH |
85 | static const unsigned char *rerere_dir_sha1(size_t i, void *table) |
86 | { | |
87 | struct rerere_dir **rr_dir = table; | |
88 | return rr_dir[i]->sha1; | |
89 | } | |
90 | ||
91 | static struct rerere_dir *find_rerere_dir(const char *hex) | |
92 | { | |
93 | unsigned char sha1[20]; | |
94 | struct rerere_dir *rr_dir; | |
95 | int pos; | |
96 | ||
97 | if (get_sha1_hex(hex, sha1)) | |
98 | return NULL; /* BUG */ | |
99 | pos = sha1_pos(sha1, rerere_dir, rerere_dir_nr, rerere_dir_sha1); | |
100 | if (pos < 0) { | |
101 | rr_dir = xmalloc(sizeof(*rr_dir)); | |
102 | hashcpy(rr_dir->sha1, sha1); | |
2c7929b1 | 103 | rr_dir->status = 0; |
1869bbe1 JH |
104 | pos = -1 - pos; |
105 | ||
106 | /* Make sure the array is big enough ... */ | |
107 | ALLOC_GROW(rerere_dir, rerere_dir_nr + 1, rerere_dir_alloc); | |
108 | /* ... and add it in. */ | |
109 | rerere_dir_nr++; | |
110 | memmove(rerere_dir + pos + 1, rerere_dir + pos, | |
111 | (rerere_dir_nr - pos - 1) * sizeof(*rerere_dir)); | |
112 | rerere_dir[pos] = rr_dir; | |
2c7929b1 | 113 | scan_rerere_dir(rr_dir); |
1869bbe1 JH |
114 | } |
115 | return rerere_dir[pos]; | |
116 | } | |
117 | ||
1d51eced | 118 | static int has_rerere_resolution(const struct rerere_id *id) |
5b2fd956 | 119 | { |
2c7929b1 | 120 | return (id->collection->status & RR_HAS_POSTIMAGE); |
5b2fd956 SB |
121 | } |
122 | ||
1d51eced JH |
123 | static struct rerere_id *new_rerere_id_hex(char *hex) |
124 | { | |
125 | struct rerere_id *id = xmalloc(sizeof(*id)); | |
1869bbe1 | 126 | id->collection = find_rerere_dir(hex); |
1d51eced JH |
127 | return id; |
128 | } | |
129 | ||
130 | static struct rerere_id *new_rerere_id(unsigned char *sha1) | |
131 | { | |
132 | return new_rerere_id_hex(sha1_to_hex(sha1)); | |
133 | } | |
134 | ||
4b68c2a0 JH |
135 | /* |
136 | * $GIT_DIR/MERGE_RR file is a collection of records, each of which is | |
137 | * "conflict ID", a HT and pathname, terminated with a NUL, and is | |
138 | * used to keep track of the set of paths that "rerere" may need to | |
139 | * work on (i.e. what is left by the previous invocation of "git | |
140 | * rerere" during the current conflict resolution session). | |
141 | */ | |
c455c87c | 142 | static void read_rr(struct string_list *rr) |
5b2fd956 | 143 | { |
f5800f6a | 144 | struct strbuf buf = STRBUF_INIT; |
5b2fd956 | 145 | FILE *in = fopen(merge_rr_path, "r"); |
f5800f6a | 146 | |
5b2fd956 SB |
147 | if (!in) |
148 | return; | |
f5800f6a JH |
149 | while (!strbuf_getwholeline(&buf, in, '\0')) { |
150 | char *path; | |
151 | unsigned char sha1[20]; | |
1d51eced | 152 | struct rerere_id *id; |
f5800f6a JH |
153 | |
154 | /* There has to be the hash, tab, path and then NUL */ | |
155 | if (buf.len < 42 || get_sha1_hex(buf.buf, sha1)) | |
5b2fd956 | 156 | die("corrupt MERGE_RR"); |
f5800f6a JH |
157 | |
158 | if (buf.buf[40] != '\t') | |
5b2fd956 | 159 | die("corrupt MERGE_RR"); |
f5800f6a JH |
160 | buf.buf[40] = '\0'; |
161 | path = buf.buf + 41; | |
1d51eced JH |
162 | id = new_rerere_id_hex(buf.buf); |
163 | string_list_insert(rr, path)->util = id; | |
5b2fd956 | 164 | } |
f5800f6a | 165 | strbuf_release(&buf); |
5b2fd956 SB |
166 | fclose(in); |
167 | } | |
168 | ||
169 | static struct lock_file write_lock; | |
170 | ||
c455c87c | 171 | static int write_rr(struct string_list *rr, int out_fd) |
5b2fd956 SB |
172 | { |
173 | int i; | |
174 | for (i = 0; i < rr->nr; i++) { | |
e2cb6a95 | 175 | struct strbuf buf = STRBUF_INIT; |
1d51eced | 176 | struct rerere_id *id; |
e2cb6a95 JH |
177 | |
178 | assert(rr->items[i].util != RERERE_RESOLVED); | |
1d51eced JH |
179 | |
180 | id = rr->items[i].util; | |
181 | if (!id) | |
5b2fd956 | 182 | continue; |
e2cb6a95 | 183 | strbuf_addf(&buf, "%s\t%s%c", |
1d51eced | 184 | rerere_id_hex(id), |
e2cb6a95 JH |
185 | rr->items[i].string, 0); |
186 | if (write_in_full(out_fd, buf.buf, buf.len) != buf.len) | |
5b2fd956 | 187 | die("unable to write rerere record"); |
e2cb6a95 JH |
188 | |
189 | strbuf_release(&buf); | |
5b2fd956 SB |
190 | } |
191 | if (commit_lock_file(&write_lock) != 0) | |
192 | die("unable to write rerere record"); | |
193 | return 0; | |
194 | } | |
195 | ||
a96847cc JH |
196 | /* |
197 | * "rerere" interacts with conflicted file contents using this I/O | |
198 | * abstraction. It reads a conflicted contents from one place via | |
199 | * "getline()" method, and optionally can write it out after | |
200 | * normalizing the conflicted hunks to the "output". Subclasses of | |
201 | * rerere_io embed this structure at the beginning of their own | |
202 | * rerere_io object. | |
203 | */ | |
204 | struct rerere_io { | |
205 | int (*getline)(struct strbuf *, struct rerere_io *); | |
206 | FILE *output; | |
207 | int wrerror; | |
208 | /* some more stuff */ | |
209 | }; | |
210 | ||
47d32af2 AR |
211 | static void ferr_write(const void *p, size_t count, FILE *fp, int *err) |
212 | { | |
213 | if (!count || *err) | |
214 | return; | |
215 | if (fwrite(p, count, 1, fp) != 1) | |
216 | *err = errno; | |
217 | } | |
218 | ||
219 | static inline void ferr_puts(const char *s, FILE *fp, int *err) | |
220 | { | |
221 | ferr_write(s, strlen(s), fp, err); | |
222 | } | |
223 | ||
27d6b085 JH |
224 | static void rerere_io_putstr(const char *str, struct rerere_io *io) |
225 | { | |
226 | if (io->output) | |
227 | ferr_puts(str, io->output, &io->wrerror); | |
228 | } | |
229 | ||
a96847cc JH |
230 | /* |
231 | * Write a conflict marker to io->output (if defined). | |
232 | */ | |
191f2417 JH |
233 | static void rerere_io_putconflict(int ch, int size, struct rerere_io *io) |
234 | { | |
235 | char buf[64]; | |
236 | ||
237 | while (size) { | |
d3c2749d | 238 | if (size <= sizeof(buf) - 2) { |
191f2417 JH |
239 | memset(buf, ch, size); |
240 | buf[size] = '\n'; | |
241 | buf[size + 1] = '\0'; | |
242 | size = 0; | |
243 | } else { | |
244 | int sz = sizeof(buf) - 1; | |
d3c2749d JH |
245 | |
246 | /* | |
247 | * Make sure we will not write everything out | |
248 | * in this round by leaving at least 1 byte | |
249 | * for the next round, giving the next round | |
250 | * a chance to add the terminating LF. Yuck. | |
251 | */ | |
191f2417 JH |
252 | if (size <= sz) |
253 | sz -= (sz - size) + 1; | |
254 | memset(buf, ch, sz); | |
255 | buf[sz] = '\0'; | |
256 | size -= sz; | |
257 | } | |
258 | rerere_io_putstr(buf, io); | |
259 | } | |
260 | } | |
261 | ||
27d6b085 JH |
262 | static void rerere_io_putmem(const char *mem, size_t sz, struct rerere_io *io) |
263 | { | |
264 | if (io->output) | |
265 | ferr_write(mem, sz, io->output, &io->wrerror); | |
266 | } | |
267 | ||
a96847cc JH |
268 | /* |
269 | * Subclass of rerere_io that reads from an on-disk file | |
270 | */ | |
27d6b085 JH |
271 | struct rerere_io_file { |
272 | struct rerere_io io; | |
273 | FILE *input; | |
274 | }; | |
275 | ||
a96847cc JH |
276 | /* |
277 | * ... and its getline() method implementation | |
278 | */ | |
27d6b085 JH |
279 | static int rerere_file_getline(struct strbuf *sb, struct rerere_io *io_) |
280 | { | |
281 | struct rerere_io_file *io = (struct rerere_io_file *)io_; | |
282 | return strbuf_getwholeline(sb, io->input, '\n'); | |
283 | } | |
284 | ||
67711cdc JH |
285 | /* |
286 | * Require the exact number of conflict marker letters, no more, no | |
287 | * less, followed by SP or any whitespace | |
288 | * (including LF). | |
289 | */ | |
290 | static int is_cmarker(char *buf, int marker_char, int marker_size) | |
191f2417 | 291 | { |
67711cdc JH |
292 | int want_sp; |
293 | ||
294 | /* | |
295 | * The beginning of our version and the end of their version | |
296 | * always are labeled like "<<<<< ours" or ">>>>> theirs", | |
297 | * hence we set want_sp for them. Note that the version from | |
298 | * the common ancestor in diff3-style output is not always | |
299 | * labelled (e.g. "||||| common" is often seen but "|||||" | |
300 | * alone is also valid), so we do not set want_sp. | |
301 | */ | |
302 | want_sp = (marker_char == '<') || (marker_char == '>'); | |
303 | ||
191f2417 JH |
304 | while (marker_size--) |
305 | if (*buf++ != marker_char) | |
306 | return 0; | |
307 | if (want_sp && *buf != ' ') | |
308 | return 0; | |
309 | return isspace(*buf); | |
310 | } | |
311 | ||
cc899eca JH |
312 | /* |
313 | * Read contents a file with conflicts, normalize the conflicts | |
314 | * by (1) discarding the common ancestor version in diff3-style, | |
315 | * (2) reordering our side and their side so that whichever sorts | |
316 | * alphabetically earlier comes before the other one, while | |
317 | * computing the "conflict ID", which is just an SHA-1 hash of | |
318 | * one side of the conflict, NUL, the other side of the conflict, | |
319 | * and NUL concatenated together. | |
320 | * | |
321 | * Return the number of conflict hunks found. | |
322 | * | |
323 | * NEEDSWORK: the logic and theory of operation behind this conflict | |
324 | * normalization may deserve to be documented somewhere, perhaps in | |
325 | * Documentation/technical/rerere.txt. | |
326 | */ | |
191f2417 | 327 | static int handle_path(unsigned char *sha1, struct rerere_io *io, int marker_size) |
5b2fd956 | 328 | { |
9126f009 | 329 | git_SHA_CTX ctx; |
cc58d7df JH |
330 | int hunk_no = 0; |
331 | enum { | |
4b05548f | 332 | RR_CONTEXT = 0, RR_SIDE_1, RR_SIDE_2, RR_ORIGINAL |
cc58d7df | 333 | } hunk = RR_CONTEXT; |
f285a2d7 | 334 | struct strbuf one = STRBUF_INIT, two = STRBUF_INIT; |
d58ee6db | 335 | struct strbuf buf = STRBUF_INIT; |
5b2fd956 SB |
336 | |
337 | if (sha1) | |
9126f009 | 338 | git_SHA1_Init(&ctx); |
5b2fd956 | 339 | |
27d6b085 | 340 | while (!io->getline(&buf, io)) { |
67711cdc | 341 | if (is_cmarker(buf.buf, '<', marker_size)) { |
cc58d7df | 342 | if (hunk != RR_CONTEXT) |
5b2fd956 | 343 | goto bad; |
cc58d7df | 344 | hunk = RR_SIDE_1; |
67711cdc | 345 | } else if (is_cmarker(buf.buf, '|', marker_size)) { |
cc58d7df | 346 | if (hunk != RR_SIDE_1) |
5b2fd956 | 347 | goto bad; |
387c9d49 | 348 | hunk = RR_ORIGINAL; |
67711cdc | 349 | } else if (is_cmarker(buf.buf, '=', marker_size)) { |
387c9d49 | 350 | if (hunk != RR_SIDE_1 && hunk != RR_ORIGINAL) |
5b2fd956 | 351 | goto bad; |
cc58d7df | 352 | hunk = RR_SIDE_2; |
67711cdc | 353 | } else if (is_cmarker(buf.buf, '>', marker_size)) { |
cc58d7df | 354 | if (hunk != RR_SIDE_2) |
5b2fd956 SB |
355 | goto bad; |
356 | if (strbuf_cmp(&one, &two) > 0) | |
357 | strbuf_swap(&one, &two); | |
358 | hunk_no++; | |
cc58d7df | 359 | hunk = RR_CONTEXT; |
191f2417 | 360 | rerere_io_putconflict('<', marker_size, io); |
27d6b085 | 361 | rerere_io_putmem(one.buf, one.len, io); |
191f2417 | 362 | rerere_io_putconflict('=', marker_size, io); |
27d6b085 | 363 | rerere_io_putmem(two.buf, two.len, io); |
191f2417 | 364 | rerere_io_putconflict('>', marker_size, io); |
5b2fd956 | 365 | if (sha1) { |
9126f009 | 366 | git_SHA1_Update(&ctx, one.buf ? one.buf : "", |
5b2fd956 | 367 | one.len + 1); |
9126f009 | 368 | git_SHA1_Update(&ctx, two.buf ? two.buf : "", |
5b2fd956 SB |
369 | two.len + 1); |
370 | } | |
371 | strbuf_reset(&one); | |
372 | strbuf_reset(&two); | |
cc58d7df | 373 | } else if (hunk == RR_SIDE_1) |
e992d1eb | 374 | strbuf_addbuf(&one, &buf); |
387c9d49 JH |
375 | else if (hunk == RR_ORIGINAL) |
376 | ; /* discard */ | |
cc58d7df | 377 | else if (hunk == RR_SIDE_2) |
e992d1eb | 378 | strbuf_addbuf(&two, &buf); |
27d6b085 JH |
379 | else |
380 | rerere_io_putstr(buf.buf, io); | |
5b2fd956 SB |
381 | continue; |
382 | bad: | |
383 | hunk = 99; /* force error exit */ | |
384 | break; | |
385 | } | |
386 | strbuf_release(&one); | |
387 | strbuf_release(&two); | |
d58ee6db | 388 | strbuf_release(&buf); |
5b2fd956 | 389 | |
5b2fd956 | 390 | if (sha1) |
9126f009 | 391 | git_SHA1_Final(sha1, &ctx); |
27d6b085 JH |
392 | if (hunk != RR_CONTEXT) |
393 | return -1; | |
394 | return hunk_no; | |
395 | } | |
396 | ||
cc899eca JH |
397 | /* |
398 | * Scan the path for conflicts, do the "handle_path()" thing above, and | |
399 | * return the number of conflict hunks found. | |
400 | */ | |
27d6b085 JH |
401 | static int handle_file(const char *path, unsigned char *sha1, const char *output) |
402 | { | |
403 | int hunk_no = 0; | |
404 | struct rerere_io_file io; | |
8588567c | 405 | int marker_size = ll_merge_marker_size(path); |
27d6b085 JH |
406 | |
407 | memset(&io, 0, sizeof(io)); | |
408 | io.io.getline = rerere_file_getline; | |
409 | io.input = fopen(path, "r"); | |
410 | io.io.wrerror = 0; | |
411 | if (!io.input) | |
412 | return error("Could not open %s", path); | |
413 | ||
414 | if (output) { | |
415 | io.io.output = fopen(output, "w"); | |
416 | if (!io.io.output) { | |
417 | fclose(io.input); | |
418 | return error("Could not write %s", output); | |
419 | } | |
420 | } | |
421 | ||
191f2417 | 422 | hunk_no = handle_path(sha1, (struct rerere_io *)&io, marker_size); |
27d6b085 JH |
423 | |
424 | fclose(io.input); | |
425 | if (io.io.wrerror) | |
426 | error("There were errors while writing %s (%s)", | |
427 | path, strerror(io.io.wrerror)); | |
428 | if (io.io.output && fclose(io.io.output)) | |
429 | io.io.wrerror = error("Failed to flush %s: %s", | |
430 | path, strerror(errno)); | |
431 | ||
432 | if (hunk_no < 0) { | |
5b2fd956 | 433 | if (output) |
691f1a28 | 434 | unlink_or_warn(output); |
5b2fd956 SB |
435 | return error("Could not parse conflict hunks in %s", path); |
436 | } | |
27d6b085 | 437 | if (io.io.wrerror) |
47d32af2 | 438 | return -1; |
5b2fd956 SB |
439 | return hunk_no; |
440 | } | |
441 | ||
a96847cc JH |
442 | /* |
443 | * Subclass of rerere_io that reads from an in-core buffer that is a | |
444 | * strbuf | |
445 | */ | |
dea4562b JH |
446 | struct rerere_io_mem { |
447 | struct rerere_io io; | |
448 | struct strbuf input; | |
449 | }; | |
450 | ||
a96847cc JH |
451 | /* |
452 | * ... and its getline() method implementation | |
453 | */ | |
dea4562b JH |
454 | static int rerere_mem_getline(struct strbuf *sb, struct rerere_io *io_) |
455 | { | |
456 | struct rerere_io_mem *io = (struct rerere_io_mem *)io_; | |
457 | char *ep; | |
458 | size_t len; | |
459 | ||
460 | strbuf_release(sb); | |
461 | if (!io->input.len) | |
462 | return -1; | |
53d8afaf JS |
463 | ep = memchr(io->input.buf, '\n', io->input.len); |
464 | if (!ep) | |
465 | ep = io->input.buf + io->input.len; | |
466 | else if (*ep == '\n') | |
dea4562b JH |
467 | ep++; |
468 | len = ep - io->input.buf; | |
469 | strbuf_add(sb, io->input.buf, len); | |
470 | strbuf_remove(&io->input, 0, len); | |
471 | return 0; | |
472 | } | |
473 | ||
474 | static int handle_cache(const char *path, unsigned char *sha1, const char *output) | |
475 | { | |
b9e31f59 | 476 | mmfile_t mmfile[3] = {{NULL}}; |
dea4562b | 477 | mmbuffer_t result = {NULL, 0}; |
9c5e6c80 | 478 | const struct cache_entry *ce; |
dea4562b JH |
479 | int pos, len, i, hunk_no; |
480 | struct rerere_io_mem io; | |
8588567c | 481 | int marker_size = ll_merge_marker_size(path); |
dea4562b JH |
482 | |
483 | /* | |
484 | * Reproduce the conflicted merge in-core | |
485 | */ | |
486 | len = strlen(path); | |
487 | pos = cache_name_pos(path, len); | |
488 | if (0 <= pos) | |
47d32af2 | 489 | return -1; |
dea4562b JH |
490 | pos = -pos - 1; |
491 | ||
74444d4e | 492 | while (pos < active_nr) { |
dea4562b JH |
493 | enum object_type type; |
494 | unsigned long size; | |
495 | ||
dea4562b | 496 | ce = active_cache[pos++]; |
b9e31f59 | 497 | if (ce_namelen(ce) != len || memcmp(ce->name, path, len)) |
74444d4e JH |
498 | break; |
499 | i = ce_stage(ce) - 1; | |
7d4053b6 JH |
500 | if (!mmfile[i].ptr) { |
501 | mmfile[i].ptr = read_sha1_file(ce->sha1, &type, &size); | |
502 | mmfile[i].size = size; | |
503 | } | |
dea4562b | 504 | } |
74444d4e | 505 | for (i = 0; i < 3; i++) |
dea4562b JH |
506 | if (!mmfile[i].ptr && !mmfile[i].size) |
507 | mmfile[i].ptr = xstrdup(""); | |
74444d4e | 508 | |
18b037a5 JN |
509 | /* |
510 | * NEEDSWORK: handle conflicts from merges with | |
511 | * merge.renormalize set, too | |
512 | */ | |
f01de62e | 513 | ll_merge(&result, path, &mmfile[0], NULL, |
dea4562b | 514 | &mmfile[1], "ours", |
712516bc | 515 | &mmfile[2], "theirs", NULL); |
dea4562b JH |
516 | for (i = 0; i < 3; i++) |
517 | free(mmfile[i].ptr); | |
518 | ||
af86debc | 519 | memset(&io, 0, sizeof(io)); |
dea4562b JH |
520 | io.io.getline = rerere_mem_getline; |
521 | if (output) | |
522 | io.io.output = fopen(output, "w"); | |
523 | else | |
524 | io.io.output = NULL; | |
525 | strbuf_init(&io.input, 0); | |
526 | strbuf_attach(&io.input, result.ptr, result.size, result.size); | |
527 | ||
963ec003 JH |
528 | /* |
529 | * Grab the conflict ID and optionally write the original | |
530 | * contents with conflict markers out. | |
531 | */ | |
191f2417 | 532 | hunk_no = handle_path(sha1, (struct rerere_io *)&io, marker_size); |
dea4562b JH |
533 | strbuf_release(&io.input); |
534 | if (io.io.output) | |
535 | fclose(io.io.output); | |
5b2fd956 SB |
536 | return hunk_no; |
537 | } | |
538 | ||
4b68c2a0 JH |
539 | /* |
540 | * Look at a cache entry at "i" and see if it is not conflicting, | |
541 | * conflicting and we are willing to handle, or conflicting and | |
542 | * we are unable to handle, and return the determination in *type. | |
543 | * Return the cache index to be looked at next, by skipping the | |
544 | * stages we have already looked at in this invocation of this | |
545 | * function. | |
546 | */ | |
ac49f5ca | 547 | static int check_one_conflict(int i, int *type) |
5b2fd956 | 548 | { |
9c5e6c80 | 549 | const struct cache_entry *e = active_cache[i]; |
ac49f5ca MZ |
550 | |
551 | if (!ce_stage(e)) { | |
552 | *type = RESOLVED; | |
553 | return i + 1; | |
554 | } | |
555 | ||
556 | *type = PUNTED; | |
5eda906b | 557 | while (ce_stage(active_cache[i]) == 1) |
fb70a06d | 558 | i++; |
ac49f5ca MZ |
559 | |
560 | /* Only handle regular files with both stages #2 and #3 */ | |
561 | if (i + 1 < active_nr) { | |
9c5e6c80 NTND |
562 | const struct cache_entry *e2 = active_cache[i]; |
563 | const struct cache_entry *e3 = active_cache[i + 1]; | |
5b2fd956 SB |
564 | if (ce_stage(e2) == 2 && |
565 | ce_stage(e3) == 3 && | |
ac49f5ca | 566 | ce_same_name(e, e3) && |
5b2fd956 | 567 | S_ISREG(e2->ce_mode) && |
ac49f5ca MZ |
568 | S_ISREG(e3->ce_mode)) |
569 | *type = THREE_STAGED; | |
570 | } | |
571 | ||
572 | /* Skip the entries with the same name */ | |
573 | while (i < active_nr && ce_same_name(e, active_cache[i])) | |
574 | i++; | |
575 | return i; | |
576 | } | |
577 | ||
4b68c2a0 JH |
578 | /* |
579 | * Scan the index and find paths that have conflicts that rerere can | |
580 | * handle, i.e. the ones that has both stages #2 and #3. | |
581 | * | |
582 | * NEEDSWORK: we do not record or replay a previous "resolve by | |
583 | * deletion" for a delete-modify conflict, as that is inherently risky | |
584 | * without knowing what modification is being discarded. The only | |
585 | * safe case, i.e. both side doing the deletion and modification that | |
586 | * are identical to the previous round, might want to be handled, | |
587 | * though. | |
588 | */ | |
ac49f5ca MZ |
589 | static int find_conflict(struct string_list *conflict) |
590 | { | |
591 | int i; | |
592 | if (read_cache() < 0) | |
593 | return error("Could not read index"); | |
594 | ||
595 | for (i = 0; i < active_nr;) { | |
596 | int conflict_type; | |
9c5e6c80 | 597 | const struct cache_entry *e = active_cache[i]; |
ac49f5ca MZ |
598 | i = check_one_conflict(i, &conflict_type); |
599 | if (conflict_type == THREE_STAGED) | |
600 | string_list_insert(conflict, (const char *)e->name); | |
601 | } | |
602 | return 0; | |
603 | } | |
604 | ||
4b68c2a0 JH |
605 | /* |
606 | * The merge_rr list is meant to hold outstanding conflicted paths | |
607 | * that rerere could handle. Abuse the list by adding other types of | |
608 | * entries to allow the caller to show "rerere remaining". | |
609 | * | |
610 | * - Conflicted paths that rerere does not handle are added | |
611 | * - Conflicted paths that have been resolved are marked as such | |
612 | * by storing RERERE_RESOLVED to .util field (where conflict ID | |
613 | * is expected to be stored). | |
614 | * | |
615 | * Do *not* write MERGE_RR file out after calling this function. | |
616 | * | |
617 | * NEEDSWORK: we may want to fix the caller that implements "rerere | |
618 | * remaining" to do this without abusing merge_rr. | |
619 | */ | |
ac49f5ca MZ |
620 | int rerere_remaining(struct string_list *merge_rr) |
621 | { | |
622 | int i; | |
623 | if (read_cache() < 0) | |
624 | return error("Could not read index"); | |
625 | ||
626 | for (i = 0; i < active_nr;) { | |
627 | int conflict_type; | |
9c5e6c80 | 628 | const struct cache_entry *e = active_cache[i]; |
ac49f5ca MZ |
629 | i = check_one_conflict(i, &conflict_type); |
630 | if (conflict_type == PUNTED) | |
631 | string_list_insert(merge_rr, (const char *)e->name); | |
632 | else if (conflict_type == RESOLVED) { | |
633 | struct string_list_item *it; | |
634 | it = string_list_lookup(merge_rr, (const char *)e->name); | |
635 | if (it != NULL) { | |
1d51eced | 636 | free_rerere_id(it); |
ac49f5ca MZ |
637 | it->util = RERERE_RESOLVED; |
638 | } | |
5b2fd956 SB |
639 | } |
640 | } | |
641 | return 0; | |
642 | } | |
643 | ||
cc899eca | 644 | /* |
18bb9934 | 645 | * Find the conflict identified by "id"; the change between its |
cc899eca JH |
646 | * "preimage" (i.e. a previous contents with conflict markers) and its |
647 | * "postimage" (i.e. the corresponding contents with conflicts | |
648 | * resolved) may apply cleanly to the contents stored in "path", i.e. | |
649 | * the conflict this time around. | |
650 | * | |
651 | * Returns 0 for successful replay of recorded resolution, or non-zero | |
652 | * for failure. | |
653 | */ | |
1d51eced | 654 | static int merge(const struct rerere_id *id, const char *path) |
5b2fd956 | 655 | { |
15ed07d5 | 656 | FILE *f; |
5b2fd956 | 657 | int ret; |
689b8c29 | 658 | mmfile_t cur = {NULL, 0}, base = {NULL, 0}, other = {NULL, 0}; |
5b2fd956 | 659 | mmbuffer_t result = {NULL, 0}; |
5b2fd956 | 660 | |
cc899eca JH |
661 | /* |
662 | * Normalize the conflicts in path and write it out to | |
663 | * "thisimage" temporary file. | |
664 | */ | |
15ed07d5 JH |
665 | if (handle_file(path, NULL, rerere_path(id, "thisimage")) < 0) { |
666 | ret = 1; | |
667 | goto out; | |
668 | } | |
5b2fd956 | 669 | |
18bb9934 JH |
670 | if (read_mmfile(&cur, rerere_path(id, "thisimage")) || |
671 | read_mmfile(&base, rerere_path(id, "preimage")) || | |
672 | read_mmfile(&other, rerere_path(id, "postimage"))) { | |
689b8c29 BW |
673 | ret = 1; |
674 | goto out; | |
675 | } | |
cc899eca JH |
676 | |
677 | /* | |
678 | * A three-way merge. Note that this honors user-customizable | |
679 | * low-level merge driver settings. | |
680 | */ | |
1e4cd68c | 681 | ret = ll_merge(&result, path, &base, NULL, &cur, "", &other, "", NULL); |
15ed07d5 JH |
682 | if (ret) |
683 | goto out; | |
684 | ||
685 | /* | |
686 | * A successful replay of recorded resolution. | |
687 | * Mark that "postimage" was used to help gc. | |
688 | */ | |
689 | if (utime(rerere_path(id, "postimage"), NULL) < 0) | |
690 | warning("failed utime() on %s: %s", | |
691 | rerere_path(id, "postimage"), | |
692 | strerror(errno)); | |
693 | ||
694 | /* Update "path" with the resolution */ | |
695 | f = fopen(path, "w"); | |
696 | if (!f) | |
697 | return error("Could not open %s: %s", path, | |
698 | strerror(errno)); | |
699 | if (fwrite(result.ptr, result.size, 1, f) != 1) | |
700 | error("Could not write %s: %s", path, strerror(errno)); | |
701 | if (fclose(f)) | |
702 | return error("Writing %s failed: %s", path, | |
703 | strerror(errno)); | |
5b2fd956 | 704 | |
689b8c29 | 705 | out: |
5b2fd956 SB |
706 | free(cur.ptr); |
707 | free(base.ptr); | |
708 | free(other.ptr); | |
709 | free(result.ptr); | |
710 | ||
711 | return ret; | |
712 | } | |
713 | ||
714 | static struct lock_file index_lock; | |
715 | ||
89ea9035 | 716 | static void update_paths(struct string_list *update) |
5b2fd956 SB |
717 | { |
718 | int i; | |
5b2fd956 | 719 | |
89ea9035 | 720 | hold_locked_index(&index_lock, 1); |
5b2fd956 SB |
721 | |
722 | for (i = 0; i < update->nr; i++) { | |
c455c87c | 723 | struct string_list_item *item = &update->items[i]; |
89ea9035 JN |
724 | if (add_file_to_cache(item->string, 0)) |
725 | exit(128); | |
a14c7ab8 JH |
726 | fprintf(stderr, "Staged '%s' using previous resolution.\n", |
727 | item->string); | |
5b2fd956 SB |
728 | } |
729 | ||
89ea9035 | 730 | if (active_cache_changed) { |
03b86647 | 731 | if (write_locked_index(&the_index, &index_lock, COMMIT_LOCK)) |
5b2fd956 | 732 | die("Unable to write new index file"); |
89ea9035 | 733 | } else |
5b2fd956 | 734 | rollback_lock_file(&index_lock); |
5b2fd956 SB |
735 | } |
736 | ||
8e7768b2 JH |
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 string_list_item *rr_item, | |
745 | struct string_list *update) | |
746 | { | |
747 | const char *path = rr_item->string; | |
1d51eced | 748 | const struct rerere_id *id = rr_item->util; |
8e7768b2 JH |
749 | |
750 | /* Is there a recorded resolution we could attempt to apply? */ | |
18bb9934 JH |
751 | if (has_rerere_resolution(id)) { |
752 | if (merge(id, path)) | |
8e7768b2 JH |
753 | return; /* failed to replay */ |
754 | ||
755 | if (rerere_autoupdate) | |
756 | string_list_insert(update, path); | |
757 | else | |
758 | fprintf(stderr, | |
759 | "Resolved '%s' using previous resolution.\n", | |
760 | path); | |
925d73c4 JH |
761 | } else if (!handle_file(path, NULL, NULL)) { |
762 | /* The user has resolved it. */ | |
18bb9934 | 763 | copy_file(rerere_path(id, "postimage"), path, 0666); |
2c7929b1 | 764 | id->collection->status |= RR_HAS_POSTIMAGE; |
925d73c4 JH |
765 | fprintf(stderr, "Recorded resolution for '%s'.\n", path); |
766 | } else { | |
767 | return; | |
8e7768b2 | 768 | } |
1d51eced | 769 | free_rerere_id(rr_item); |
8e7768b2 JH |
770 | rr_item->util = NULL; |
771 | } | |
772 | ||
c455c87c | 773 | static int do_plain_rerere(struct string_list *rr, int fd) |
5b2fd956 | 774 | { |
183113a5 TF |
775 | struct string_list conflict = STRING_LIST_INIT_DUP; |
776 | struct string_list update = STRING_LIST_INIT_DUP; | |
5b2fd956 SB |
777 | int i; |
778 | ||
779 | find_conflict(&conflict); | |
780 | ||
781 | /* | |
cc899eca JH |
782 | * MERGE_RR records paths with conflicts immediately after |
783 | * merge failed. Some of the conflicted paths might have been | |
784 | * hand resolved in the working tree since then, but the | |
785 | * initial run would catch all and register their preimages. | |
5b2fd956 | 786 | */ |
5b2fd956 | 787 | for (i = 0; i < conflict.nr; i++) { |
1d51eced | 788 | struct rerere_id *id; |
c7a25d37 | 789 | unsigned char sha1[20]; |
c455c87c | 790 | const char *path = conflict.items[i].string; |
1d51eced | 791 | int ret; |
cc899eca | 792 | |
c7a25d37 JH |
793 | if (string_list_has_string(rr, path)) |
794 | continue; | |
cc899eca | 795 | |
c7a25d37 JH |
796 | /* |
797 | * Ask handle_file() to scan and assign a | |
798 | * conflict ID. No need to write anything out | |
799 | * yet. | |
800 | */ | |
801 | ret = handle_file(path, sha1, NULL); | |
802 | if (ret < 1) | |
803 | continue; | |
1d51eced JH |
804 | |
805 | id = new_rerere_id(sha1); | |
18bb9934 | 806 | string_list_insert(rr, path)->util = id; |
cc899eca | 807 | |
c7a25d37 JH |
808 | /* |
809 | * If the directory does not exist, create | |
810 | * it. mkdir_in_gitdir() will fail with | |
811 | * EEXIST if there already is one. | |
812 | * | |
813 | * NEEDSWORK: make sure "gc" does not remove | |
814 | * preimage without removing the directory. | |
815 | */ | |
1d51eced | 816 | if (mkdir_in_gitdir(rerere_path(id, NULL))) |
c7a25d37 JH |
817 | continue; |
818 | ||
819 | /* | |
820 | * We are the first to encounter this | |
821 | * conflict. Ask handle_file() to write the | |
822 | * normalized contents to the "preimage" file. | |
823 | */ | |
18bb9934 | 824 | handle_file(path, NULL, rerere_path(id, "preimage")); |
2c7929b1 | 825 | id->collection->status |= RR_HAS_PREIMAGE; |
c7a25d37 | 826 | fprintf(stderr, "Recorded preimage for '%s'\n", path); |
5b2fd956 SB |
827 | } |
828 | ||
8e7768b2 JH |
829 | for (i = 0; i < rr->nr; i++) |
830 | do_rerere_one_path(&rr->items[i], &update); | |
5b2fd956 SB |
831 | |
832 | if (update.nr) | |
833 | update_paths(&update); | |
834 | ||
835 | return write_rr(rr, fd); | |
836 | } | |
837 | ||
633e5ad3 | 838 | static void git_rerere_config(void) |
5b2fd956 | 839 | { |
633e5ad3 TA |
840 | git_config_get_bool("rerere.enabled", &rerere_enabled); |
841 | git_config_get_bool("rerere.autoupdate", &rerere_autoupdate); | |
842 | git_config(git_default_config, NULL); | |
5b2fd956 SB |
843 | } |
844 | ||
845 | static int is_rerere_enabled(void) | |
846 | { | |
5b2fd956 SB |
847 | const char *rr_cache; |
848 | int rr_cache_exists; | |
849 | ||
850 | if (!rerere_enabled) | |
851 | return 0; | |
852 | ||
853 | rr_cache = git_path("rr-cache"); | |
90b4a71c | 854 | rr_cache_exists = is_directory(rr_cache); |
5b2fd956 SB |
855 | if (rerere_enabled < 0) |
856 | return rr_cache_exists; | |
857 | ||
90a6464b | 858 | if (!rr_cache_exists && mkdir_in_gitdir(rr_cache)) |
5b2fd956 SB |
859 | die("Could not create directory %s", rr_cache); |
860 | return 1; | |
861 | } | |
862 | ||
cb6020bb | 863 | int setup_rerere(struct string_list *merge_rr, int flags) |
5b2fd956 SB |
864 | { |
865 | int fd; | |
866 | ||
633e5ad3 | 867 | git_rerere_config(); |
5b2fd956 SB |
868 | if (!is_rerere_enabled()) |
869 | return -1; | |
870 | ||
cb6020bb JH |
871 | if (flags & (RERERE_AUTOUPDATE|RERERE_NOAUTOUPDATE)) |
872 | rerere_autoupdate = !!(flags & RERERE_AUTOUPDATE); | |
a4f34cbb | 873 | merge_rr_path = git_pathdup("MERGE_RR"); |
acd3b9ec JH |
874 | fd = hold_lock_file_for_update(&write_lock, merge_rr_path, |
875 | LOCK_DIE_ON_ERROR); | |
5b2fd956 SB |
876 | read_rr(merge_rr); |
877 | return fd; | |
878 | } | |
879 | ||
cc899eca JH |
880 | /* |
881 | * The main entry point that is called internally from codepaths that | |
882 | * perform mergy operations, possibly leaving conflicted index entries | |
883 | * and working tree files. | |
884 | */ | |
cb6020bb | 885 | int rerere(int flags) |
5b2fd956 | 886 | { |
183113a5 | 887 | struct string_list merge_rr = STRING_LIST_INIT_DUP; |
1869bbe1 | 888 | int fd, status; |
5b2fd956 | 889 | |
cb6020bb | 890 | fd = setup_rerere(&merge_rr, flags); |
5b2fd956 SB |
891 | if (fd < 0) |
892 | return 0; | |
1869bbe1 JH |
893 | status = do_plain_rerere(&merge_rr, fd); |
894 | free_rerere_dirs(); | |
895 | return status; | |
5b2fd956 | 896 | } |
dea4562b JH |
897 | |
898 | static int rerere_forget_one_path(const char *path, struct string_list *rr) | |
899 | { | |
900 | const char *filename; | |
1d51eced | 901 | struct rerere_id *id; |
dea4562b JH |
902 | unsigned char sha1[20]; |
903 | int ret; | |
8d9b5a4a | 904 | struct string_list_item *item; |
dea4562b | 905 | |
963ec003 JH |
906 | /* |
907 | * Recreate the original conflict from the stages in the | |
908 | * index and compute the conflict ID | |
909 | */ | |
dea4562b JH |
910 | ret = handle_cache(path, sha1, NULL); |
911 | if (ret < 1) | |
912 | return error("Could not parse conflict hunks in '%s'", path); | |
963ec003 JH |
913 | |
914 | /* Nuke the recorded resolution for the conflict */ | |
1d51eced | 915 | id = new_rerere_id(sha1); |
18bb9934 | 916 | filename = rerere_path(id, "postimage"); |
dea4562b JH |
917 | if (unlink(filename)) |
918 | return (errno == ENOENT | |
919 | ? error("no remembered resolution for %s", path) | |
920 | : error("cannot unlink %s: %s", filename, strerror(errno))); | |
921 | ||
963ec003 JH |
922 | /* |
923 | * Update the preimage so that the user can resolve the | |
924 | * conflict in the working tree, run us again to record | |
925 | * the postimage. | |
926 | */ | |
18bb9934 | 927 | handle_cache(path, sha1, rerere_path(id, "preimage")); |
dea4562b JH |
928 | fprintf(stderr, "Updated preimage for '%s'\n", path); |
929 | ||
963ec003 JH |
930 | /* |
931 | * And remember that we can record resolution for this | |
932 | * conflict when the user is done. | |
933 | */ | |
8d9b5a4a | 934 | item = string_list_insert(rr, path); |
1d51eced | 935 | free_rerere_id(item); |
18bb9934 | 936 | item->util = id; |
dea4562b JH |
937 | fprintf(stderr, "Forgot resolution for %s\n", path); |
938 | return 0; | |
939 | } | |
940 | ||
01a10b0a | 941 | int rerere_forget(struct pathspec *pathspec) |
dea4562b JH |
942 | { |
943 | int i, fd; | |
183113a5 TF |
944 | struct string_list conflict = STRING_LIST_INIT_DUP; |
945 | struct string_list merge_rr = STRING_LIST_INIT_DUP; | |
dea4562b JH |
946 | |
947 | if (read_cache() < 0) | |
948 | return error("Could not read index"); | |
949 | ||
6751e047 | 950 | fd = setup_rerere(&merge_rr, RERERE_NOAUTOUPDATE); |
dea4562b | 951 | |
963ec003 JH |
952 | /* |
953 | * The paths may have been resolved (incorrectly); | |
954 | * recover the original conflicted state and then | |
955 | * find the conflicted paths. | |
956 | */ | |
dea4562b JH |
957 | unmerge_cache(pathspec); |
958 | find_conflict(&conflict); | |
959 | for (i = 0; i < conflict.nr; i++) { | |
960 | struct string_list_item *it = &conflict.items[i]; | |
854b0959 | 961 | if (!match_pathspec(pathspec, it->string, |
ae8d0824 | 962 | strlen(it->string), 0, NULL, 0)) |
dea4562b JH |
963 | continue; |
964 | rerere_forget_one_path(it->string, &merge_rr); | |
965 | } | |
966 | return write_rr(&merge_rr, fd); | |
967 | } | |
0f891e7d | 968 | |
e828de82 JH |
969 | /* |
970 | * Garbage collection support | |
971 | */ | |
1d51eced JH |
972 | |
973 | /* | |
974 | * Note that this is not reentrant but is used only one-at-a-time | |
975 | * so it does not matter right now. | |
976 | */ | |
977 | static struct rerere_id *dirname_to_id(const char *name) | |
978 | { | |
979 | static struct rerere_id id; | |
1869bbe1 | 980 | id.collection = find_rerere_dir(name); |
1d51eced JH |
981 | return &id; |
982 | } | |
983 | ||
984 | static time_t rerere_created_at(const char *dir_name) | |
0f891e7d JH |
985 | { |
986 | struct stat st; | |
1d51eced JH |
987 | struct rerere_id *id = dirname_to_id(dir_name); |
988 | ||
18bb9934 | 989 | return stat(rerere_path(id, "preimage"), &st) ? (time_t) 0 : st.st_mtime; |
0f891e7d JH |
990 | } |
991 | ||
1d51eced | 992 | static time_t rerere_last_used_at(const char *dir_name) |
0f891e7d JH |
993 | { |
994 | struct stat st; | |
1d51eced JH |
995 | struct rerere_id *id = dirname_to_id(dir_name); |
996 | ||
18bb9934 | 997 | return stat(rerere_path(id, "postimage"), &st) ? (time_t) 0 : st.st_mtime; |
0f891e7d JH |
998 | } |
999 | ||
e828de82 JH |
1000 | /* |
1001 | * Remove the recorded resolution for a given conflict ID | |
1002 | */ | |
1d51eced | 1003 | static void unlink_rr_item(struct rerere_id *id) |
0f891e7d | 1004 | { |
18bb9934 JH |
1005 | unlink(rerere_path(id, "thisimage")); |
1006 | unlink(rerere_path(id, "preimage")); | |
1007 | unlink(rerere_path(id, "postimage")); | |
e828de82 JH |
1008 | /* |
1009 | * NEEDSWORK: what if this rmdir() fails? Wouldn't we then | |
1010 | * assume that we already have preimage recorded in | |
1011 | * do_plain_rerere()? | |
1012 | */ | |
1d51eced | 1013 | rmdir(rerere_path(id, NULL)); |
0f891e7d JH |
1014 | } |
1015 | ||
0f891e7d JH |
1016 | void rerere_gc(struct string_list *rr) |
1017 | { | |
1018 | struct string_list to_remove = STRING_LIST_INIT_DUP; | |
1019 | DIR *dir; | |
1020 | struct dirent *e; | |
1021 | int i, cutoff; | |
1022 | time_t now = time(NULL), then; | |
633e5ad3 TA |
1023 | int cutoff_noresolve = 15; |
1024 | int cutoff_resolve = 60; | |
0f891e7d | 1025 | |
633e5ad3 TA |
1026 | git_config_get_int("gc.rerereresolved", &cutoff_resolve); |
1027 | git_config_get_int("gc.rerereunresolved", &cutoff_noresolve); | |
1028 | git_config(git_default_config, NULL); | |
0f891e7d JH |
1029 | dir = opendir(git_path("rr-cache")); |
1030 | if (!dir) | |
1031 | die_errno("unable to open rr-cache directory"); | |
e828de82 | 1032 | /* Collect stale conflict IDs ... */ |
0f891e7d JH |
1033 | while ((e = readdir(dir))) { |
1034 | if (is_dot_or_dotdot(e->d_name)) | |
1035 | continue; | |
1036 | ||
1037 | then = rerere_last_used_at(e->d_name); | |
1038 | if (then) { | |
633e5ad3 | 1039 | cutoff = cutoff_resolve; |
0f891e7d JH |
1040 | } else { |
1041 | then = rerere_created_at(e->d_name); | |
1042 | if (!then) | |
1043 | continue; | |
633e5ad3 | 1044 | cutoff = cutoff_noresolve; |
0f891e7d JH |
1045 | } |
1046 | if (then < now - cutoff * 86400) | |
1047 | string_list_append(&to_remove, e->d_name); | |
1048 | } | |
a9930e35 | 1049 | closedir(dir); |
e828de82 | 1050 | /* ... and then remove them one-by-one */ |
0f891e7d | 1051 | for (i = 0; i < to_remove.nr; i++) |
1d51eced | 1052 | unlink_rr_item(dirname_to_id(to_remove.items[i].string)); |
0f891e7d JH |
1053 | string_list_clear(&to_remove, 0); |
1054 | } | |
1055 | ||
e828de82 JH |
1056 | /* |
1057 | * During a conflict resolution, after "rerere" recorded the | |
1058 | * preimages, abandon them if the user did not resolve them or | |
1059 | * record their resolutions. And drop $GIT_DIR/MERGE_RR. | |
1060 | * | |
1061 | * NEEDSWORK: shouldn't we be calling this from "reset --hard"? | |
1062 | */ | |
0f891e7d JH |
1063 | void rerere_clear(struct string_list *merge_rr) |
1064 | { | |
1065 | int i; | |
1066 | ||
1067 | for (i = 0; i < merge_rr->nr; i++) { | |
1d51eced | 1068 | struct rerere_id *id = merge_rr->items[i].util; |
18bb9934 JH |
1069 | if (!has_rerere_resolution(id)) |
1070 | unlink_rr_item(id); | |
0f891e7d JH |
1071 | } |
1072 | unlink_or_warn(git_path("MERGE_RR")); | |
1073 | } |