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