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