]>
Commit | Line | Data |
---|---|---|
5b2fd956 | 1 | #include "cache.h" |
c455c87c | 2 | #include "string-list.h" |
5b2fd956 | 3 | #include "rerere.h" |
5b2fd956 | 4 | #include "xdiff-interface.h" |
dea4562b JH |
5 | #include "dir.h" |
6 | #include "resolve-undo.h" | |
7 | #include "ll-merge.h" | |
8588567c | 8 | #include "attr.h" |
5b2fd956 | 9 | |
ac49f5ca MZ |
10 | #define RESOLVED 0 |
11 | #define PUNTED 1 | |
12 | #define THREE_STAGED 2 | |
13 | void *RERERE_RESOLVED = &RERERE_RESOLVED; | |
14 | ||
5b2fd956 SB |
15 | /* if rerere_enabled == -1, fall back to detection of .git/rr-cache */ |
16 | static int rerere_enabled = -1; | |
17 | ||
18 | /* automatically update cleanly resolved paths to the index */ | |
19 | static int rerere_autoupdate; | |
20 | ||
21 | static char *merge_rr_path; | |
22 | ||
90056966 | 23 | const char *rerere_path(const char *hex, const char *file) |
5b2fd956 | 24 | { |
90056966 | 25 | return git_path("rr-cache/%s/%s", hex, file); |
5b2fd956 SB |
26 | } |
27 | ||
90056966 | 28 | int has_rerere_resolution(const char *hex) |
5b2fd956 SB |
29 | { |
30 | struct stat st; | |
90056966 | 31 | return !stat(rerere_path(hex, "postimage"), &st); |
5b2fd956 SB |
32 | } |
33 | ||
c455c87c | 34 | static void read_rr(struct string_list *rr) |
5b2fd956 SB |
35 | { |
36 | unsigned char sha1[20]; | |
37 | char buf[PATH_MAX]; | |
38 | FILE *in = fopen(merge_rr_path, "r"); | |
39 | if (!in) | |
40 | return; | |
41 | while (fread(buf, 40, 1, in) == 1) { | |
42 | int i; | |
43 | char *name; | |
44 | if (get_sha1_hex(buf, sha1)) | |
45 | die("corrupt MERGE_RR"); | |
46 | buf[40] = '\0'; | |
47 | name = xstrdup(buf); | |
48 | if (fgetc(in) != '\t') | |
49 | die("corrupt MERGE_RR"); | |
50 | for (i = 0; i < sizeof(buf) && (buf[i] = fgetc(in)); i++) | |
51 | ; /* do nothing */ | |
52 | if (i == sizeof(buf)) | |
53 | die("filename too long"); | |
78a395d3 | 54 | string_list_insert(rr, buf)->util = name; |
5b2fd956 SB |
55 | } |
56 | fclose(in); | |
57 | } | |
58 | ||
59 | static struct lock_file write_lock; | |
60 | ||
c455c87c | 61 | static int write_rr(struct string_list *rr, int out_fd) |
5b2fd956 SB |
62 | { |
63 | int i; | |
64 | for (i = 0; i < rr->nr; i++) { | |
65 | const char *path; | |
66 | int length; | |
67 | if (!rr->items[i].util) | |
68 | continue; | |
c455c87c | 69 | path = rr->items[i].string; |
5b2fd956 SB |
70 | length = strlen(path) + 1; |
71 | if (write_in_full(out_fd, rr->items[i].util, 40) != 40 || | |
2b7ca830 | 72 | write_str_in_full(out_fd, "\t") != 1 || |
5b2fd956 SB |
73 | write_in_full(out_fd, path, length) != length) |
74 | die("unable to write rerere record"); | |
75 | } | |
76 | if (commit_lock_file(&write_lock) != 0) | |
77 | die("unable to write rerere record"); | |
78 | return 0; | |
79 | } | |
80 | ||
47d32af2 AR |
81 | static void ferr_write(const void *p, size_t count, FILE *fp, int *err) |
82 | { | |
83 | if (!count || *err) | |
84 | return; | |
85 | if (fwrite(p, count, 1, fp) != 1) | |
86 | *err = errno; | |
87 | } | |
88 | ||
89 | static inline void ferr_puts(const char *s, FILE *fp, int *err) | |
90 | { | |
91 | ferr_write(s, strlen(s), fp, err); | |
92 | } | |
93 | ||
27d6b085 JH |
94 | struct rerere_io { |
95 | int (*getline)(struct strbuf *, struct rerere_io *); | |
96 | FILE *output; | |
97 | int wrerror; | |
98 | /* some more stuff */ | |
99 | }; | |
100 | ||
101 | static void rerere_io_putstr(const char *str, struct rerere_io *io) | |
102 | { | |
103 | if (io->output) | |
104 | ferr_puts(str, io->output, &io->wrerror); | |
105 | } | |
106 | ||
191f2417 JH |
107 | static void rerere_io_putconflict(int ch, int size, struct rerere_io *io) |
108 | { | |
109 | char buf[64]; | |
110 | ||
111 | while (size) { | |
112 | if (size < sizeof(buf) - 2) { | |
113 | memset(buf, ch, size); | |
114 | buf[size] = '\n'; | |
115 | buf[size + 1] = '\0'; | |
116 | size = 0; | |
117 | } else { | |
118 | int sz = sizeof(buf) - 1; | |
119 | if (size <= sz) | |
120 | sz -= (sz - size) + 1; | |
121 | memset(buf, ch, sz); | |
122 | buf[sz] = '\0'; | |
123 | size -= sz; | |
124 | } | |
125 | rerere_io_putstr(buf, io); | |
126 | } | |
127 | } | |
128 | ||
27d6b085 JH |
129 | static void rerere_io_putmem(const char *mem, size_t sz, struct rerere_io *io) |
130 | { | |
131 | if (io->output) | |
132 | ferr_write(mem, sz, io->output, &io->wrerror); | |
133 | } | |
134 | ||
135 | struct rerere_io_file { | |
136 | struct rerere_io io; | |
137 | FILE *input; | |
138 | }; | |
139 | ||
140 | static int rerere_file_getline(struct strbuf *sb, struct rerere_io *io_) | |
141 | { | |
142 | struct rerere_io_file *io = (struct rerere_io_file *)io_; | |
143 | return strbuf_getwholeline(sb, io->input, '\n'); | |
144 | } | |
145 | ||
191f2417 JH |
146 | static int is_cmarker(char *buf, int marker_char, int marker_size, int want_sp) |
147 | { | |
148 | while (marker_size--) | |
149 | if (*buf++ != marker_char) | |
150 | return 0; | |
151 | if (want_sp && *buf != ' ') | |
152 | return 0; | |
153 | return isspace(*buf); | |
154 | } | |
155 | ||
156 | static int handle_path(unsigned char *sha1, struct rerere_io *io, int marker_size) | |
5b2fd956 | 157 | { |
9126f009 | 158 | git_SHA_CTX ctx; |
cc58d7df JH |
159 | int hunk_no = 0; |
160 | enum { | |
4b05548f | 161 | RR_CONTEXT = 0, RR_SIDE_1, RR_SIDE_2, RR_ORIGINAL |
cc58d7df | 162 | } hunk = RR_CONTEXT; |
f285a2d7 | 163 | struct strbuf one = STRBUF_INIT, two = STRBUF_INIT; |
d58ee6db | 164 | struct strbuf buf = STRBUF_INIT; |
5b2fd956 SB |
165 | |
166 | if (sha1) | |
9126f009 | 167 | git_SHA1_Init(&ctx); |
5b2fd956 | 168 | |
27d6b085 | 169 | while (!io->getline(&buf, io)) { |
191f2417 | 170 | if (is_cmarker(buf.buf, '<', marker_size, 1)) { |
cc58d7df | 171 | if (hunk != RR_CONTEXT) |
5b2fd956 | 172 | goto bad; |
cc58d7df | 173 | hunk = RR_SIDE_1; |
191f2417 | 174 | } else if (is_cmarker(buf.buf, '|', marker_size, 0)) { |
cc58d7df | 175 | if (hunk != RR_SIDE_1) |
5b2fd956 | 176 | goto bad; |
387c9d49 | 177 | hunk = RR_ORIGINAL; |
191f2417 | 178 | } else if (is_cmarker(buf.buf, '=', marker_size, 0)) { |
387c9d49 | 179 | if (hunk != RR_SIDE_1 && hunk != RR_ORIGINAL) |
5b2fd956 | 180 | goto bad; |
cc58d7df | 181 | hunk = RR_SIDE_2; |
191f2417 | 182 | } else if (is_cmarker(buf.buf, '>', marker_size, 1)) { |
cc58d7df | 183 | if (hunk != RR_SIDE_2) |
5b2fd956 SB |
184 | goto bad; |
185 | if (strbuf_cmp(&one, &two) > 0) | |
186 | strbuf_swap(&one, &two); | |
187 | hunk_no++; | |
cc58d7df | 188 | hunk = RR_CONTEXT; |
191f2417 | 189 | rerere_io_putconflict('<', marker_size, io); |
27d6b085 | 190 | rerere_io_putmem(one.buf, one.len, io); |
191f2417 | 191 | rerere_io_putconflict('=', marker_size, io); |
27d6b085 | 192 | rerere_io_putmem(two.buf, two.len, io); |
191f2417 | 193 | rerere_io_putconflict('>', marker_size, io); |
5b2fd956 | 194 | if (sha1) { |
9126f009 | 195 | git_SHA1_Update(&ctx, one.buf ? one.buf : "", |
5b2fd956 | 196 | one.len + 1); |
9126f009 | 197 | git_SHA1_Update(&ctx, two.buf ? two.buf : "", |
5b2fd956 SB |
198 | two.len + 1); |
199 | } | |
200 | strbuf_reset(&one); | |
201 | strbuf_reset(&two); | |
cc58d7df | 202 | } else if (hunk == RR_SIDE_1) |
d58ee6db | 203 | strbuf_addstr(&one, buf.buf); |
387c9d49 JH |
204 | else if (hunk == RR_ORIGINAL) |
205 | ; /* discard */ | |
cc58d7df | 206 | else if (hunk == RR_SIDE_2) |
d58ee6db | 207 | strbuf_addstr(&two, buf.buf); |
27d6b085 JH |
208 | else |
209 | rerere_io_putstr(buf.buf, io); | |
5b2fd956 SB |
210 | continue; |
211 | bad: | |
212 | hunk = 99; /* force error exit */ | |
213 | break; | |
214 | } | |
215 | strbuf_release(&one); | |
216 | strbuf_release(&two); | |
d58ee6db | 217 | strbuf_release(&buf); |
5b2fd956 | 218 | |
5b2fd956 | 219 | if (sha1) |
9126f009 | 220 | git_SHA1_Final(sha1, &ctx); |
27d6b085 JH |
221 | if (hunk != RR_CONTEXT) |
222 | return -1; | |
223 | return hunk_no; | |
224 | } | |
225 | ||
226 | static int handle_file(const char *path, unsigned char *sha1, const char *output) | |
227 | { | |
228 | int hunk_no = 0; | |
229 | struct rerere_io_file io; | |
8588567c | 230 | int marker_size = ll_merge_marker_size(path); |
27d6b085 JH |
231 | |
232 | memset(&io, 0, sizeof(io)); | |
233 | io.io.getline = rerere_file_getline; | |
234 | io.input = fopen(path, "r"); | |
235 | io.io.wrerror = 0; | |
236 | if (!io.input) | |
237 | return error("Could not open %s", path); | |
238 | ||
239 | if (output) { | |
240 | io.io.output = fopen(output, "w"); | |
241 | if (!io.io.output) { | |
242 | fclose(io.input); | |
243 | return error("Could not write %s", output); | |
244 | } | |
245 | } | |
246 | ||
191f2417 | 247 | hunk_no = handle_path(sha1, (struct rerere_io *)&io, marker_size); |
27d6b085 JH |
248 | |
249 | fclose(io.input); | |
250 | if (io.io.wrerror) | |
251 | error("There were errors while writing %s (%s)", | |
252 | path, strerror(io.io.wrerror)); | |
253 | if (io.io.output && fclose(io.io.output)) | |
254 | io.io.wrerror = error("Failed to flush %s: %s", | |
255 | path, strerror(errno)); | |
256 | ||
257 | if (hunk_no < 0) { | |
5b2fd956 | 258 | if (output) |
691f1a28 | 259 | unlink_or_warn(output); |
5b2fd956 SB |
260 | return error("Could not parse conflict hunks in %s", path); |
261 | } | |
27d6b085 | 262 | if (io.io.wrerror) |
47d32af2 | 263 | return -1; |
5b2fd956 SB |
264 | return hunk_no; |
265 | } | |
266 | ||
dea4562b JH |
267 | struct rerere_io_mem { |
268 | struct rerere_io io; | |
269 | struct strbuf input; | |
270 | }; | |
271 | ||
272 | static int rerere_mem_getline(struct strbuf *sb, struct rerere_io *io_) | |
273 | { | |
274 | struct rerere_io_mem *io = (struct rerere_io_mem *)io_; | |
275 | char *ep; | |
276 | size_t len; | |
277 | ||
278 | strbuf_release(sb); | |
279 | if (!io->input.len) | |
280 | return -1; | |
281 | ep = strchrnul(io->input.buf, '\n'); | |
282 | if (*ep == '\n') | |
283 | ep++; | |
284 | len = ep - io->input.buf; | |
285 | strbuf_add(sb, io->input.buf, len); | |
286 | strbuf_remove(&io->input, 0, len); | |
287 | return 0; | |
288 | } | |
289 | ||
290 | static int handle_cache(const char *path, unsigned char *sha1, const char *output) | |
291 | { | |
292 | mmfile_t mmfile[3]; | |
293 | mmbuffer_t result = {NULL, 0}; | |
294 | struct cache_entry *ce; | |
295 | int pos, len, i, hunk_no; | |
296 | struct rerere_io_mem io; | |
8588567c | 297 | int marker_size = ll_merge_marker_size(path); |
dea4562b JH |
298 | |
299 | /* | |
300 | * Reproduce the conflicted merge in-core | |
301 | */ | |
302 | len = strlen(path); | |
303 | pos = cache_name_pos(path, len); | |
304 | if (0 <= pos) | |
47d32af2 | 305 | return -1; |
dea4562b JH |
306 | pos = -pos - 1; |
307 | ||
308 | for (i = 0; i < 3; i++) { | |
309 | enum object_type type; | |
310 | unsigned long size; | |
311 | ||
312 | mmfile[i].size = 0; | |
313 | mmfile[i].ptr = NULL; | |
314 | if (active_nr <= pos) | |
315 | break; | |
316 | ce = active_cache[pos++]; | |
317 | if (ce_namelen(ce) != len || memcmp(ce->name, path, len) | |
318 | || ce_stage(ce) != i + 1) | |
319 | break; | |
320 | mmfile[i].ptr = read_sha1_file(ce->sha1, &type, &size); | |
321 | mmfile[i].size = size; | |
322 | } | |
323 | for (i = 0; i < 3; i++) { | |
324 | if (!mmfile[i].ptr && !mmfile[i].size) | |
325 | mmfile[i].ptr = xstrdup(""); | |
326 | } | |
18b037a5 JN |
327 | /* |
328 | * NEEDSWORK: handle conflicts from merges with | |
329 | * merge.renormalize set, too | |
330 | */ | |
f01de62e | 331 | ll_merge(&result, path, &mmfile[0], NULL, |
dea4562b | 332 | &mmfile[1], "ours", |
712516bc | 333 | &mmfile[2], "theirs", NULL); |
dea4562b JH |
334 | for (i = 0; i < 3; i++) |
335 | free(mmfile[i].ptr); | |
336 | ||
af86debc | 337 | memset(&io, 0, sizeof(io)); |
dea4562b JH |
338 | io.io.getline = rerere_mem_getline; |
339 | if (output) | |
340 | io.io.output = fopen(output, "w"); | |
341 | else | |
342 | io.io.output = NULL; | |
343 | strbuf_init(&io.input, 0); | |
344 | strbuf_attach(&io.input, result.ptr, result.size, result.size); | |
345 | ||
191f2417 | 346 | hunk_no = handle_path(sha1, (struct rerere_io *)&io, marker_size); |
dea4562b JH |
347 | strbuf_release(&io.input); |
348 | if (io.io.output) | |
349 | fclose(io.io.output); | |
5b2fd956 SB |
350 | return hunk_no; |
351 | } | |
352 | ||
ac49f5ca | 353 | static int check_one_conflict(int i, int *type) |
5b2fd956 | 354 | { |
ac49f5ca MZ |
355 | struct cache_entry *e = active_cache[i]; |
356 | ||
357 | if (!ce_stage(e)) { | |
358 | *type = RESOLVED; | |
359 | return i + 1; | |
360 | } | |
361 | ||
362 | *type = PUNTED; | |
363 | if (ce_stage(e) == 1) { | |
364 | if (active_nr <= ++i) | |
365 | return i + 1; | |
366 | } | |
367 | ||
368 | /* Only handle regular files with both stages #2 and #3 */ | |
369 | if (i + 1 < active_nr) { | |
5b2fd956 | 370 | struct cache_entry *e2 = active_cache[i]; |
ac49f5ca | 371 | struct cache_entry *e3 = active_cache[i + 1]; |
5b2fd956 SB |
372 | if (ce_stage(e2) == 2 && |
373 | ce_stage(e3) == 3 && | |
ac49f5ca | 374 | ce_same_name(e, e3) && |
5b2fd956 | 375 | S_ISREG(e2->ce_mode) && |
ac49f5ca MZ |
376 | S_ISREG(e3->ce_mode)) |
377 | *type = THREE_STAGED; | |
378 | } | |
379 | ||
380 | /* Skip the entries with the same name */ | |
381 | while (i < active_nr && ce_same_name(e, active_cache[i])) | |
382 | i++; | |
383 | return i; | |
384 | } | |
385 | ||
386 | static int find_conflict(struct string_list *conflict) | |
387 | { | |
388 | int i; | |
389 | if (read_cache() < 0) | |
390 | return error("Could not read index"); | |
391 | ||
392 | for (i = 0; i < active_nr;) { | |
393 | int conflict_type; | |
394 | struct cache_entry *e = active_cache[i]; | |
395 | i = check_one_conflict(i, &conflict_type); | |
396 | if (conflict_type == THREE_STAGED) | |
397 | string_list_insert(conflict, (const char *)e->name); | |
398 | } | |
399 | return 0; | |
400 | } | |
401 | ||
402 | int rerere_remaining(struct string_list *merge_rr) | |
403 | { | |
404 | int i; | |
405 | if (read_cache() < 0) | |
406 | return error("Could not read index"); | |
407 | ||
408 | for (i = 0; i < active_nr;) { | |
409 | int conflict_type; | |
410 | struct cache_entry *e = active_cache[i]; | |
411 | i = check_one_conflict(i, &conflict_type); | |
412 | if (conflict_type == PUNTED) | |
413 | string_list_insert(merge_rr, (const char *)e->name); | |
414 | else if (conflict_type == RESOLVED) { | |
415 | struct string_list_item *it; | |
416 | it = string_list_lookup(merge_rr, (const char *)e->name); | |
417 | if (it != NULL) { | |
418 | free(it->util); | |
419 | it->util = RERERE_RESOLVED; | |
420 | } | |
5b2fd956 SB |
421 | } |
422 | } | |
423 | return 0; | |
424 | } | |
425 | ||
426 | static int merge(const char *name, const char *path) | |
427 | { | |
428 | int ret; | |
689b8c29 | 429 | mmfile_t cur = {NULL, 0}, base = {NULL, 0}, other = {NULL, 0}; |
5b2fd956 | 430 | mmbuffer_t result = {NULL, 0}; |
5b2fd956 | 431 | |
90056966 | 432 | if (handle_file(path, NULL, rerere_path(name, "thisimage")) < 0) |
5b2fd956 SB |
433 | return 1; |
434 | ||
90056966 SG |
435 | if (read_mmfile(&cur, rerere_path(name, "thisimage")) || |
436 | read_mmfile(&base, rerere_path(name, "preimage")) || | |
689b8c29 BW |
437 | read_mmfile(&other, rerere_path(name, "postimage"))) { |
438 | ret = 1; | |
439 | goto out; | |
440 | } | |
f01de62e | 441 | ret = ll_merge(&result, path, &base, NULL, &cur, "", &other, "", 0); |
5b2fd956 | 442 | if (!ret) { |
7d7ff15b SG |
443 | FILE *f; |
444 | ||
445 | if (utime(rerere_path(name, "postimage"), NULL) < 0) | |
446 | warning("failed utime() on %s: %s", | |
447 | rerere_path(name, "postimage"), | |
448 | strerror(errno)); | |
449 | f = fopen(path, "w"); | |
5b2fd956 | 450 | if (!f) |
47d32af2 AR |
451 | return error("Could not open %s: %s", path, |
452 | strerror(errno)); | |
453 | if (fwrite(result.ptr, result.size, 1, f) != 1) | |
454 | error("Could not write %s: %s", path, strerror(errno)); | |
455 | if (fclose(f)) | |
456 | return error("Writing %s failed: %s", path, | |
457 | strerror(errno)); | |
5b2fd956 SB |
458 | } |
459 | ||
689b8c29 | 460 | out: |
5b2fd956 SB |
461 | free(cur.ptr); |
462 | free(base.ptr); | |
463 | free(other.ptr); | |
464 | free(result.ptr); | |
465 | ||
466 | return ret; | |
467 | } | |
468 | ||
469 | static struct lock_file index_lock; | |
470 | ||
c455c87c | 471 | static int update_paths(struct string_list *update) |
5b2fd956 SB |
472 | { |
473 | int i; | |
474 | int fd = hold_locked_index(&index_lock, 0); | |
475 | int status = 0; | |
476 | ||
477 | if (fd < 0) | |
478 | return -1; | |
479 | ||
480 | for (i = 0; i < update->nr; i++) { | |
c455c87c JS |
481 | struct string_list_item *item = &update->items[i]; |
482 | if (add_file_to_cache(item->string, ADD_CACHE_IGNORE_ERRORS)) | |
5b2fd956 SB |
483 | status = -1; |
484 | } | |
485 | ||
486 | if (!status && active_cache_changed) { | |
487 | if (write_cache(fd, active_cache, active_nr) || | |
488 | commit_locked_index(&index_lock)) | |
489 | die("Unable to write new index file"); | |
490 | } else if (fd >= 0) | |
491 | rollback_lock_file(&index_lock); | |
492 | return status; | |
493 | } | |
494 | ||
c455c87c | 495 | static int do_plain_rerere(struct string_list *rr, int fd) |
5b2fd956 | 496 | { |
183113a5 TF |
497 | struct string_list conflict = STRING_LIST_INIT_DUP; |
498 | struct string_list update = STRING_LIST_INIT_DUP; | |
5b2fd956 SB |
499 | int i; |
500 | ||
501 | find_conflict(&conflict); | |
502 | ||
503 | /* | |
504 | * MERGE_RR records paths with conflicts immediately after merge | |
505 | * failed. Some of the conflicted paths might have been hand resolved | |
506 | * in the working tree since then, but the initial run would catch all | |
507 | * and register their preimages. | |
508 | */ | |
509 | ||
510 | for (i = 0; i < conflict.nr; i++) { | |
c455c87c JS |
511 | const char *path = conflict.items[i].string; |
512 | if (!string_list_has_string(rr, path)) { | |
5b2fd956 SB |
513 | unsigned char sha1[20]; |
514 | char *hex; | |
515 | int ret; | |
516 | ret = handle_file(path, sha1, NULL); | |
517 | if (ret < 1) | |
518 | continue; | |
519 | hex = xstrdup(sha1_to_hex(sha1)); | |
78a395d3 | 520 | string_list_insert(rr, path)->util = hex; |
5b2fd956 | 521 | if (mkdir(git_path("rr-cache/%s", hex), 0755)) |
ba19a808 | 522 | continue; |
90056966 | 523 | handle_file(path, NULL, rerere_path(hex, "preimage")); |
5b2fd956 SB |
524 | fprintf(stderr, "Recorded preimage for '%s'\n", path); |
525 | } | |
526 | } | |
527 | ||
528 | /* | |
529 | * Now some of the paths that had conflicts earlier might have been | |
530 | * hand resolved. Others may be similar to a conflict already that | |
531 | * was resolved before. | |
532 | */ | |
533 | ||
534 | for (i = 0; i < rr->nr; i++) { | |
535 | int ret; | |
c455c87c | 536 | const char *path = rr->items[i].string; |
5b2fd956 SB |
537 | const char *name = (const char *)rr->items[i].util; |
538 | ||
90056966 | 539 | if (has_rerere_resolution(name)) { |
5b2fd956 | 540 | if (!merge(name, path)) { |
5b2fd956 | 541 | if (rerere_autoupdate) |
78a395d3 | 542 | string_list_insert(&update, path); |
9196e825 JH |
543 | fprintf(stderr, |
544 | "%s '%s' using previous resolution.\n", | |
545 | rerere_autoupdate | |
546 | ? "Staged" : "Resolved", | |
547 | path); | |
5b2fd956 SB |
548 | goto mark_resolved; |
549 | } | |
550 | } | |
551 | ||
552 | /* Let's see if we have resolved it. */ | |
553 | ret = handle_file(path, NULL, NULL); | |
554 | if (ret) | |
555 | continue; | |
556 | ||
557 | fprintf(stderr, "Recorded resolution for '%s'.\n", path); | |
90056966 | 558 | copy_file(rerere_path(name, "postimage"), path, 0666); |
5b2fd956 SB |
559 | mark_resolved: |
560 | rr->items[i].util = NULL; | |
561 | } | |
562 | ||
563 | if (update.nr) | |
564 | update_paths(&update); | |
565 | ||
566 | return write_rr(rr, fd); | |
567 | } | |
568 | ||
569 | static int git_rerere_config(const char *var, const char *value, void *cb) | |
570 | { | |
571 | if (!strcmp(var, "rerere.enabled")) | |
572 | rerere_enabled = git_config_bool(var, value); | |
573 | else if (!strcmp(var, "rerere.autoupdate")) | |
574 | rerere_autoupdate = git_config_bool(var, value); | |
575 | else | |
576 | return git_default_config(var, value, cb); | |
577 | return 0; | |
578 | } | |
579 | ||
580 | static int is_rerere_enabled(void) | |
581 | { | |
5b2fd956 SB |
582 | const char *rr_cache; |
583 | int rr_cache_exists; | |
584 | ||
585 | if (!rerere_enabled) | |
586 | return 0; | |
587 | ||
588 | rr_cache = git_path("rr-cache"); | |
90b4a71c | 589 | rr_cache_exists = is_directory(rr_cache); |
5b2fd956 SB |
590 | if (rerere_enabled < 0) |
591 | return rr_cache_exists; | |
592 | ||
90a6464b | 593 | if (!rr_cache_exists && mkdir_in_gitdir(rr_cache)) |
5b2fd956 SB |
594 | die("Could not create directory %s", rr_cache); |
595 | return 1; | |
596 | } | |
597 | ||
cb6020bb | 598 | int setup_rerere(struct string_list *merge_rr, int flags) |
5b2fd956 SB |
599 | { |
600 | int fd; | |
601 | ||
602 | git_config(git_rerere_config, NULL); | |
603 | if (!is_rerere_enabled()) | |
604 | return -1; | |
605 | ||
cb6020bb JH |
606 | if (flags & (RERERE_AUTOUPDATE|RERERE_NOAUTOUPDATE)) |
607 | rerere_autoupdate = !!(flags & RERERE_AUTOUPDATE); | |
a4f34cbb | 608 | merge_rr_path = git_pathdup("MERGE_RR"); |
acd3b9ec JH |
609 | fd = hold_lock_file_for_update(&write_lock, merge_rr_path, |
610 | LOCK_DIE_ON_ERROR); | |
5b2fd956 SB |
611 | read_rr(merge_rr); |
612 | return fd; | |
613 | } | |
614 | ||
cb6020bb | 615 | int rerere(int flags) |
5b2fd956 | 616 | { |
183113a5 | 617 | struct string_list merge_rr = STRING_LIST_INIT_DUP; |
5b2fd956 SB |
618 | int fd; |
619 | ||
cb6020bb | 620 | fd = setup_rerere(&merge_rr, flags); |
5b2fd956 SB |
621 | if (fd < 0) |
622 | return 0; | |
623 | return do_plain_rerere(&merge_rr, fd); | |
624 | } | |
dea4562b JH |
625 | |
626 | static int rerere_forget_one_path(const char *path, struct string_list *rr) | |
627 | { | |
628 | const char *filename; | |
629 | char *hex; | |
630 | unsigned char sha1[20]; | |
631 | int ret; | |
632 | ||
633 | ret = handle_cache(path, sha1, NULL); | |
634 | if (ret < 1) | |
635 | return error("Could not parse conflict hunks in '%s'", path); | |
636 | hex = xstrdup(sha1_to_hex(sha1)); | |
637 | filename = rerere_path(hex, "postimage"); | |
638 | if (unlink(filename)) | |
639 | return (errno == ENOENT | |
640 | ? error("no remembered resolution for %s", path) | |
641 | : error("cannot unlink %s: %s", filename, strerror(errno))); | |
642 | ||
643 | handle_cache(path, sha1, rerere_path(hex, "preimage")); | |
644 | fprintf(stderr, "Updated preimage for '%s'\n", path); | |
645 | ||
646 | ||
78a395d3 | 647 | string_list_insert(rr, path)->util = hex; |
dea4562b JH |
648 | fprintf(stderr, "Forgot resolution for %s\n", path); |
649 | return 0; | |
650 | } | |
651 | ||
652 | int rerere_forget(const char **pathspec) | |
653 | { | |
654 | int i, fd; | |
183113a5 TF |
655 | struct string_list conflict = STRING_LIST_INIT_DUP; |
656 | struct string_list merge_rr = STRING_LIST_INIT_DUP; | |
dea4562b JH |
657 | |
658 | if (read_cache() < 0) | |
659 | return error("Could not read index"); | |
660 | ||
6751e047 | 661 | fd = setup_rerere(&merge_rr, RERERE_NOAUTOUPDATE); |
dea4562b JH |
662 | |
663 | unmerge_cache(pathspec); | |
664 | find_conflict(&conflict); | |
665 | for (i = 0; i < conflict.nr; i++) { | |
666 | struct string_list_item *it = &conflict.items[i]; | |
667 | if (!match_pathspec(pathspec, it->string, strlen(it->string), | |
668 | 0, NULL)) | |
669 | continue; | |
670 | rerere_forget_one_path(it->string, &merge_rr); | |
671 | } | |
672 | return write_rr(&merge_rr, fd); | |
673 | } |