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