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