2 #include "string-list.h"
4 #include "xdiff-interface.h"
6 #include "resolve-undo.h"
9 /* if rerere_enabled == -1, fall back to detection of .git/rr-cache */
10 static int rerere_enabled
= -1;
12 /* automatically update cleanly resolved paths to the index */
13 static int rerere_autoupdate
;
15 static char *merge_rr_path
;
17 const char *rerere_path(const char *hex
, const char *file
)
19 return git_path("rr-cache/%s/%s", hex
, file
);
22 int has_rerere_resolution(const char *hex
)
25 return !stat(rerere_path(hex
, "postimage"), &st
);
28 static void read_rr(struct string_list
*rr
)
30 unsigned char sha1
[20];
32 FILE *in
= fopen(merge_rr_path
, "r");
35 while (fread(buf
, 40, 1, in
) == 1) {
38 if (get_sha1_hex(buf
, sha1
))
39 die("corrupt MERGE_RR");
42 if (fgetc(in
) != '\t')
43 die("corrupt MERGE_RR");
44 for (i
= 0; i
< sizeof(buf
) && (buf
[i
] = fgetc(in
)); i
++)
47 die("filename too long");
48 string_list_insert(buf
, rr
)->util
= name
;
53 static struct lock_file write_lock
;
55 static int write_rr(struct string_list
*rr
, int out_fd
)
58 for (i
= 0; i
< rr
->nr
; i
++) {
61 if (!rr
->items
[i
].util
)
63 path
= rr
->items
[i
].string
;
64 length
= strlen(path
) + 1;
65 if (write_in_full(out_fd
, rr
->items
[i
].util
, 40) != 40 ||
66 write_str_in_full(out_fd
, "\t") != 1 ||
67 write_in_full(out_fd
, path
, length
) != length
)
68 die("unable to write rerere record");
70 if (commit_lock_file(&write_lock
) != 0)
71 die("unable to write rerere record");
75 static void ferr_write(const void *p
, size_t count
, FILE *fp
, int *err
)
79 if (fwrite(p
, count
, 1, fp
) != 1)
83 static inline void ferr_puts(const char *s
, FILE *fp
, int *err
)
85 ferr_write(s
, strlen(s
), fp
, err
);
89 int (*getline
)(struct strbuf
*, struct rerere_io
*);
95 static void rerere_io_putstr(const char *str
, struct rerere_io
*io
)
98 ferr_puts(str
, io
->output
, &io
->wrerror
);
101 static void rerere_io_putconflict(int ch
, int size
, struct rerere_io
*io
)
106 if (size
< sizeof(buf
) - 2) {
107 memset(buf
, ch
, size
);
109 buf
[size
+ 1] = '\0';
112 int sz
= sizeof(buf
) - 1;
114 sz
-= (sz
- size
) + 1;
119 rerere_io_putstr(buf
, io
);
123 static void rerere_io_putmem(const char *mem
, size_t sz
, struct rerere_io
*io
)
126 ferr_write(mem
, sz
, io
->output
, &io
->wrerror
);
129 struct rerere_io_file
{
134 static int rerere_file_getline(struct strbuf
*sb
, struct rerere_io
*io_
)
136 struct rerere_io_file
*io
= (struct rerere_io_file
*)io_
;
137 return strbuf_getwholeline(sb
, io
->input
, '\n');
140 static int is_cmarker(char *buf
, int marker_char
, int marker_size
, int want_sp
)
142 while (marker_size
--)
143 if (*buf
++ != marker_char
)
145 if (want_sp
&& *buf
!= ' ')
147 return isspace(*buf
);
150 static int handle_path(unsigned char *sha1
, struct rerere_io
*io
, int marker_size
)
155 RR_CONTEXT
= 0, RR_SIDE_1
, RR_SIDE_2
, RR_ORIGINAL
,
157 struct strbuf one
= STRBUF_INIT
, two
= STRBUF_INIT
;
158 struct strbuf buf
= STRBUF_INIT
;
163 while (!io
->getline(&buf
, io
)) {
164 if (is_cmarker(buf
.buf
, '<', marker_size
, 1)) {
165 if (hunk
!= RR_CONTEXT
)
168 } else if (is_cmarker(buf
.buf
, '|', marker_size
, 0)) {
169 if (hunk
!= RR_SIDE_1
)
172 } else if (is_cmarker(buf
.buf
, '=', marker_size
, 0)) {
173 if (hunk
!= RR_SIDE_1
&& hunk
!= RR_ORIGINAL
)
176 } else if (is_cmarker(buf
.buf
, '>', marker_size
, 1)) {
177 if (hunk
!= RR_SIDE_2
)
179 if (strbuf_cmp(&one
, &two
) > 0)
180 strbuf_swap(&one
, &two
);
183 rerere_io_putconflict('<', marker_size
, io
);
184 rerere_io_putmem(one
.buf
, one
.len
, io
);
185 rerere_io_putconflict('=', marker_size
, io
);
186 rerere_io_putmem(two
.buf
, two
.len
, io
);
187 rerere_io_putconflict('>', marker_size
, io
);
189 git_SHA1_Update(&ctx
, one
.buf
? one
.buf
: "",
191 git_SHA1_Update(&ctx
, two
.buf
? two
.buf
: "",
196 } else if (hunk
== RR_SIDE_1
)
197 strbuf_addstr(&one
, buf
.buf
);
198 else if (hunk
== RR_ORIGINAL
)
200 else if (hunk
== RR_SIDE_2
)
201 strbuf_addstr(&two
, buf
.buf
);
203 rerere_io_putstr(buf
.buf
, io
);
206 hunk
= 99; /* force error exit */
209 strbuf_release(&one
);
210 strbuf_release(&two
);
211 strbuf_release(&buf
);
214 git_SHA1_Final(sha1
, &ctx
);
215 if (hunk
!= RR_CONTEXT
)
220 static int handle_file(const char *path
, unsigned char *sha1
, const char *output
)
223 struct rerere_io_file io
;
226 memset(&io
, 0, sizeof(io
));
227 io
.io
.getline
= rerere_file_getline
;
228 io
.input
= fopen(path
, "r");
231 return error("Could not open %s", path
);
234 io
.io
.output
= fopen(output
, "w");
237 return error("Could not write %s", output
);
241 hunk_no
= handle_path(sha1
, (struct rerere_io
*)&io
, marker_size
);
245 error("There were errors while writing %s (%s)",
246 path
, strerror(io
.io
.wrerror
));
247 if (io
.io
.output
&& fclose(io
.io
.output
))
248 io
.io
.wrerror
= error("Failed to flush %s: %s",
249 path
, strerror(errno
));
253 unlink_or_warn(output
);
254 return error("Could not parse conflict hunks in %s", path
);
261 struct rerere_io_mem
{
266 static int rerere_mem_getline(struct strbuf
*sb
, struct rerere_io
*io_
)
268 struct rerere_io_mem
*io
= (struct rerere_io_mem
*)io_
;
275 ep
= strchrnul(io
->input
.buf
, '\n');
278 len
= ep
- io
->input
.buf
;
279 strbuf_add(sb
, io
->input
.buf
, len
);
280 strbuf_remove(&io
->input
, 0, len
);
284 static int handle_cache(const char *path
, unsigned char *sha1
, const char *output
)
287 mmbuffer_t result
= {NULL
, 0};
288 struct cache_entry
*ce
;
289 int pos
, len
, i
, hunk_no
;
290 struct rerere_io_mem io
;
294 * Reproduce the conflicted merge in-core
297 pos
= cache_name_pos(path
, len
);
302 for (i
= 0; i
< 3; i
++) {
303 enum object_type type
;
307 mmfile
[i
].ptr
= NULL
;
308 if (active_nr
<= pos
)
310 ce
= active_cache
[pos
++];
311 if (ce_namelen(ce
) != len
|| memcmp(ce
->name
, path
, len
)
312 || ce_stage(ce
) != i
+ 1)
314 mmfile
[i
].ptr
= read_sha1_file(ce
->sha1
, &type
, &size
);
315 mmfile
[i
].size
= size
;
317 for (i
= 0; i
< 3; i
++) {
318 if (!mmfile
[i
].ptr
&& !mmfile
[i
].size
)
319 mmfile
[i
].ptr
= xstrdup("");
321 ll_merge(&result
, path
, &mmfile
[0],
323 &mmfile
[2], "theirs", 0);
324 for (i
= 0; i
< 3; i
++)
327 memset(&io
, 0, sizeof(&io
));
328 io
.io
.getline
= rerere_mem_getline
;
330 io
.io
.output
= fopen(output
, "w");
333 strbuf_init(&io
.input
, 0);
334 strbuf_attach(&io
.input
, result
.ptr
, result
.size
, result
.size
);
336 hunk_no
= handle_path(sha1
, (struct rerere_io
*)&io
, marker_size
);
337 strbuf_release(&io
.input
);
339 fclose(io
.io
.output
);
343 static int find_conflict(struct string_list
*conflict
)
346 if (read_cache() < 0)
347 return error("Could not read index");
348 for (i
= 0; i
+1 < active_nr
; i
++) {
349 struct cache_entry
*e2
= active_cache
[i
];
350 struct cache_entry
*e3
= active_cache
[i
+1];
351 if (ce_stage(e2
) == 2 &&
353 ce_same_name(e2
, e3
) &&
354 S_ISREG(e2
->ce_mode
) &&
355 S_ISREG(e3
->ce_mode
)) {
356 string_list_insert((const char *)e2
->name
, conflict
);
357 i
++; /* skip over both #2 and #3 */
363 static int merge(const char *name
, const char *path
)
366 mmfile_t cur
, base
, other
;
367 mmbuffer_t result
= {NULL
, 0};
369 if (handle_file(path
, NULL
, rerere_path(name
, "thisimage")) < 0)
372 if (read_mmfile(&cur
, rerere_path(name
, "thisimage")) ||
373 read_mmfile(&base
, rerere_path(name
, "preimage")) ||
374 read_mmfile(&other
, rerere_path(name
, "postimage")))
376 ret
= ll_merge(&result
, path
, &base
, &cur
, "", &other
, "", 0);
378 FILE *f
= fopen(path
, "w");
380 return error("Could not open %s: %s", path
,
382 if (fwrite(result
.ptr
, result
.size
, 1, f
) != 1)
383 error("Could not write %s: %s", path
, strerror(errno
));
385 return error("Writing %s failed: %s", path
,
397 static struct lock_file index_lock
;
399 static int update_paths(struct string_list
*update
)
402 int fd
= hold_locked_index(&index_lock
, 0);
408 for (i
= 0; i
< update
->nr
; i
++) {
409 struct string_list_item
*item
= &update
->items
[i
];
410 if (add_file_to_cache(item
->string
, ADD_CACHE_IGNORE_ERRORS
))
414 if (!status
&& active_cache_changed
) {
415 if (write_cache(fd
, active_cache
, active_nr
) ||
416 commit_locked_index(&index_lock
))
417 die("Unable to write new index file");
419 rollback_lock_file(&index_lock
);
423 static int do_plain_rerere(struct string_list
*rr
, int fd
)
425 struct string_list conflict
= { NULL
, 0, 0, 1 };
426 struct string_list update
= { NULL
, 0, 0, 1 };
429 find_conflict(&conflict
);
432 * MERGE_RR records paths with conflicts immediately after merge
433 * failed. Some of the conflicted paths might have been hand resolved
434 * in the working tree since then, but the initial run would catch all
435 * and register their preimages.
438 for (i
= 0; i
< conflict
.nr
; i
++) {
439 const char *path
= conflict
.items
[i
].string
;
440 if (!string_list_has_string(rr
, path
)) {
441 unsigned char sha1
[20];
444 ret
= handle_file(path
, sha1
, NULL
);
447 hex
= xstrdup(sha1_to_hex(sha1
));
448 string_list_insert(path
, rr
)->util
= hex
;
449 if (mkdir(git_path("rr-cache/%s", hex
), 0755))
451 handle_file(path
, NULL
, rerere_path(hex
, "preimage"));
452 fprintf(stderr
, "Recorded preimage for '%s'\n", path
);
457 * Now some of the paths that had conflicts earlier might have been
458 * hand resolved. Others may be similar to a conflict already that
459 * was resolved before.
462 for (i
= 0; i
< rr
->nr
; i
++) {
464 const char *path
= rr
->items
[i
].string
;
465 const char *name
= (const char *)rr
->items
[i
].util
;
467 if (has_rerere_resolution(name
)) {
468 if (!merge(name
, path
)) {
469 if (rerere_autoupdate
)
470 string_list_insert(path
, &update
);
472 "%s '%s' using previous resolution.\n",
474 ? "Staged" : "Resolved",
480 /* Let's see if we have resolved it. */
481 ret
= handle_file(path
, NULL
, NULL
);
485 fprintf(stderr
, "Recorded resolution for '%s'.\n", path
);
486 copy_file(rerere_path(name
, "postimage"), path
, 0666);
488 rr
->items
[i
].util
= NULL
;
492 update_paths(&update
);
494 return write_rr(rr
, fd
);
497 static int git_rerere_config(const char *var
, const char *value
, void *cb
)
499 if (!strcmp(var
, "rerere.enabled"))
500 rerere_enabled
= git_config_bool(var
, value
);
501 else if (!strcmp(var
, "rerere.autoupdate"))
502 rerere_autoupdate
= git_config_bool(var
, value
);
504 return git_default_config(var
, value
, cb
);
508 static int is_rerere_enabled(void)
510 const char *rr_cache
;
516 rr_cache
= git_path("rr-cache");
517 rr_cache_exists
= is_directory(rr_cache
);
518 if (rerere_enabled
< 0)
519 return rr_cache_exists
;
521 if (!rr_cache_exists
&&
522 (mkdir(rr_cache
, 0777) || adjust_shared_perm(rr_cache
)))
523 die("Could not create directory %s", rr_cache
);
527 int setup_rerere(struct string_list
*merge_rr
)
531 git_config(git_rerere_config
, NULL
);
532 if (!is_rerere_enabled())
535 merge_rr_path
= git_pathdup("MERGE_RR");
536 fd
= hold_lock_file_for_update(&write_lock
, merge_rr_path
,
544 struct string_list merge_rr
= { NULL
, 0, 0, 1 };
547 fd
= setup_rerere(&merge_rr
);
550 return do_plain_rerere(&merge_rr
, fd
);
553 static int rerere_forget_one_path(const char *path
, struct string_list
*rr
)
555 const char *filename
;
557 unsigned char sha1
[20];
560 ret
= handle_cache(path
, sha1
, NULL
);
562 return error("Could not parse conflict hunks in '%s'", path
);
563 hex
= xstrdup(sha1_to_hex(sha1
));
564 filename
= rerere_path(hex
, "postimage");
565 if (unlink(filename
))
566 return (errno
== ENOENT
567 ? error("no remembered resolution for %s", path
)
568 : error("cannot unlink %s: %s", filename
, strerror(errno
)));
570 handle_cache(path
, sha1
, rerere_path(hex
, "preimage"));
571 fprintf(stderr
, "Updated preimage for '%s'\n", path
);
574 string_list_insert(path
, rr
)->util
= hex
;
575 fprintf(stderr
, "Forgot resolution for %s\n", path
);
579 int rerere_forget(const char **pathspec
)
582 struct string_list conflict
= { NULL
, 0, 0, 1 };
583 struct string_list merge_rr
= { NULL
, 0, 0, 1 };
585 if (read_cache() < 0)
586 return error("Could not read index");
588 fd
= setup_rerere(&merge_rr
);
590 unmerge_cache(pathspec
);
591 find_conflict(&conflict
);
592 for (i
= 0; i
< conflict
.nr
; i
++) {
593 struct string_list_item
*it
= &conflict
.items
[i
];
594 if (!match_pathspec(pathspec
, it
->string
, strlen(it
->string
),
597 rerere_forget_one_path(it
->string
, &merge_rr
);
599 return write_rr(&merge_rr
, fd
);