2 #include "string-list.h"
4 #include "xdiff-interface.h"
6 #include "resolve-undo.h"
10 /* if rerere_enabled == -1, fall back to detection of .git/rr-cache */
11 static int rerere_enabled
= -1;
13 /* automatically update cleanly resolved paths to the index */
14 static int rerere_autoupdate
;
16 static char *merge_rr_path
;
18 const char *rerere_path(const char *hex
, const char *file
)
20 return git_path("rr-cache/%s/%s", hex
, file
);
23 int has_rerere_resolution(const char *hex
)
26 return !stat(rerere_path(hex
, "postimage"), &st
);
29 static void read_rr(struct string_list
*rr
)
31 unsigned char sha1
[20];
33 FILE *in
= fopen(merge_rr_path
, "r");
36 while (fread(buf
, 40, 1, in
) == 1) {
39 if (get_sha1_hex(buf
, sha1
))
40 die("corrupt MERGE_RR");
43 if (fgetc(in
) != '\t')
44 die("corrupt MERGE_RR");
45 for (i
= 0; i
< sizeof(buf
); i
++) {
48 die("corrupt MERGE_RR");
54 die("filename too long");
55 string_list_insert(rr
, buf
)->util
= name
;
60 static struct lock_file write_lock
;
62 static int write_rr(struct string_list
*rr
, int out_fd
)
65 for (i
= 0; i
< rr
->nr
; i
++) {
68 if (!rr
->items
[i
].util
)
70 path
= rr
->items
[i
].string
;
71 length
= strlen(path
) + 1;
72 if (write_in_full(out_fd
, rr
->items
[i
].util
, 40) != 40 ||
73 write_str_in_full(out_fd
, "\t") != 1 ||
74 write_in_full(out_fd
, path
, length
) != length
)
75 die("unable to write rerere record");
77 if (commit_lock_file(&write_lock
) != 0)
78 die("unable to write rerere record");
82 static void ferr_write(const void *p
, size_t count
, FILE *fp
, int *err
)
86 if (fwrite(p
, count
, 1, fp
) != 1)
90 static inline void ferr_puts(const char *s
, FILE *fp
, int *err
)
92 ferr_write(s
, strlen(s
), fp
, err
);
96 int (*getline
)(struct strbuf
*, struct rerere_io
*);
102 static void rerere_io_putstr(const char *str
, struct rerere_io
*io
)
105 ferr_puts(str
, io
->output
, &io
->wrerror
);
108 static void rerere_io_putconflict(int ch
, int size
, struct rerere_io
*io
)
113 if (size
< sizeof(buf
) - 2) {
114 memset(buf
, ch
, size
);
116 buf
[size
+ 1] = '\0';
119 int sz
= sizeof(buf
) - 1;
121 sz
-= (sz
- size
) + 1;
126 rerere_io_putstr(buf
, io
);
130 static void rerere_io_putmem(const char *mem
, size_t sz
, struct rerere_io
*io
)
133 ferr_write(mem
, sz
, io
->output
, &io
->wrerror
);
136 struct rerere_io_file
{
141 static int rerere_file_getline(struct strbuf
*sb
, struct rerere_io
*io_
)
143 struct rerere_io_file
*io
= (struct rerere_io_file
*)io_
;
144 return strbuf_getwholeline(sb
, io
->input
, '\n');
147 static int is_cmarker(char *buf
, int marker_char
, int marker_size
, int want_sp
)
149 while (marker_size
--)
150 if (*buf
++ != marker_char
)
152 if (want_sp
&& *buf
!= ' ')
154 return isspace(*buf
);
157 static int handle_path(unsigned char *sha1
, struct rerere_io
*io
, int marker_size
)
162 RR_CONTEXT
= 0, RR_SIDE_1
, RR_SIDE_2
, RR_ORIGINAL
164 struct strbuf one
= STRBUF_INIT
, two
= STRBUF_INIT
;
165 struct strbuf buf
= STRBUF_INIT
;
170 while (!io
->getline(&buf
, io
)) {
171 if (is_cmarker(buf
.buf
, '<', marker_size
, 1)) {
172 if (hunk
!= RR_CONTEXT
)
175 } else if (is_cmarker(buf
.buf
, '|', marker_size
, 0)) {
176 if (hunk
!= RR_SIDE_1
)
179 } else if (is_cmarker(buf
.buf
, '=', marker_size
, 0)) {
180 if (hunk
!= RR_SIDE_1
&& hunk
!= RR_ORIGINAL
)
183 } else if (is_cmarker(buf
.buf
, '>', marker_size
, 1)) {
184 if (hunk
!= RR_SIDE_2
)
186 if (strbuf_cmp(&one
, &two
) > 0)
187 strbuf_swap(&one
, &two
);
190 rerere_io_putconflict('<', marker_size
, io
);
191 rerere_io_putmem(one
.buf
, one
.len
, io
);
192 rerere_io_putconflict('=', marker_size
, io
);
193 rerere_io_putmem(two
.buf
, two
.len
, io
);
194 rerere_io_putconflict('>', marker_size
, io
);
196 git_SHA1_Update(&ctx
, one
.buf
? one
.buf
: "",
198 git_SHA1_Update(&ctx
, two
.buf
? two
.buf
: "",
203 } else if (hunk
== RR_SIDE_1
)
204 strbuf_addstr(&one
, buf
.buf
);
205 else if (hunk
== RR_ORIGINAL
)
207 else if (hunk
== RR_SIDE_2
)
208 strbuf_addstr(&two
, buf
.buf
);
210 rerere_io_putstr(buf
.buf
, io
);
213 hunk
= 99; /* force error exit */
216 strbuf_release(&one
);
217 strbuf_release(&two
);
218 strbuf_release(&buf
);
221 git_SHA1_Final(sha1
, &ctx
);
222 if (hunk
!= RR_CONTEXT
)
227 static int handle_file(const char *path
, unsigned char *sha1
, const char *output
)
230 struct rerere_io_file io
;
231 int marker_size
= ll_merge_marker_size(path
);
233 memset(&io
, 0, sizeof(io
));
234 io
.io
.getline
= rerere_file_getline
;
235 io
.input
= fopen(path
, "r");
238 return error("Could not open %s", path
);
241 io
.io
.output
= fopen(output
, "w");
244 return error("Could not write %s", output
);
248 hunk_no
= handle_path(sha1
, (struct rerere_io
*)&io
, marker_size
);
252 error("There were errors while writing %s (%s)",
253 path
, strerror(io
.io
.wrerror
));
254 if (io
.io
.output
&& fclose(io
.io
.output
))
255 io
.io
.wrerror
= error("Failed to flush %s: %s",
256 path
, strerror(errno
));
260 unlink_or_warn(output
);
261 return error("Could not parse conflict hunks in %s", path
);
268 struct rerere_io_mem
{
273 static int rerere_mem_getline(struct strbuf
*sb
, struct rerere_io
*io_
)
275 struct rerere_io_mem
*io
= (struct rerere_io_mem
*)io_
;
282 ep
= strchrnul(io
->input
.buf
, '\n');
285 len
= ep
- io
->input
.buf
;
286 strbuf_add(sb
, io
->input
.buf
, len
);
287 strbuf_remove(&io
->input
, 0, len
);
291 static int handle_cache(const char *path
, unsigned char *sha1
, const char *output
)
294 mmbuffer_t result
= {NULL
, 0};
295 struct cache_entry
*ce
;
296 int pos
, len
, i
, hunk_no
;
297 struct rerere_io_mem io
;
298 int marker_size
= ll_merge_marker_size(path
);
301 * Reproduce the conflicted merge in-core
304 pos
= cache_name_pos(path
, len
);
309 for (i
= 0; i
< 3; i
++) {
310 enum object_type type
;
314 mmfile
[i
].ptr
= NULL
;
315 if (active_nr
<= pos
)
317 ce
= active_cache
[pos
++];
318 if (ce_namelen(ce
) != len
|| memcmp(ce
->name
, path
, len
)
319 || ce_stage(ce
) != i
+ 1)
321 mmfile
[i
].ptr
= read_sha1_file(ce
->sha1
, &type
, &size
);
322 mmfile
[i
].size
= size
;
324 for (i
= 0; i
< 3; i
++) {
325 if (!mmfile
[i
].ptr
&& !mmfile
[i
].size
)
326 mmfile
[i
].ptr
= xstrdup("");
329 * NEEDSWORK: handle conflicts from merges with
330 * merge.renormalize set, too
332 ll_merge(&result
, path
, &mmfile
[0], NULL
,
334 &mmfile
[2], "theirs", NULL
);
335 for (i
= 0; i
< 3; i
++)
338 memset(&io
, 0, sizeof(io
));
339 io
.io
.getline
= rerere_mem_getline
;
341 io
.io
.output
= fopen(output
, "w");
344 strbuf_init(&io
.input
, 0);
345 strbuf_attach(&io
.input
, result
.ptr
, result
.size
, result
.size
);
347 hunk_no
= handle_path(sha1
, (struct rerere_io
*)&io
, marker_size
);
348 strbuf_release(&io
.input
);
350 fclose(io
.io
.output
);
354 static int find_conflict(struct string_list
*conflict
)
357 if (read_cache() < 0)
358 return error("Could not read index");
359 for (i
= 0; i
+1 < active_nr
; i
++) {
360 struct cache_entry
*e2
= active_cache
[i
];
361 struct cache_entry
*e3
= active_cache
[i
+1];
362 if (ce_stage(e2
) == 2 &&
364 ce_same_name(e2
, e3
) &&
365 S_ISREG(e2
->ce_mode
) &&
366 S_ISREG(e3
->ce_mode
)) {
367 string_list_insert(conflict
, (const char *)e2
->name
);
368 i
++; /* skip over both #2 and #3 */
374 static int merge(const char *name
, const char *path
)
377 mmfile_t cur
= {NULL
, 0}, base
= {NULL
, 0}, other
= {NULL
, 0};
378 mmbuffer_t result
= {NULL
, 0};
380 if (handle_file(path
, NULL
, rerere_path(name
, "thisimage")) < 0)
383 if (read_mmfile(&cur
, rerere_path(name
, "thisimage")) ||
384 read_mmfile(&base
, rerere_path(name
, "preimage")) ||
385 read_mmfile(&other
, rerere_path(name
, "postimage"))) {
389 ret
= ll_merge(&result
, path
, &base
, NULL
, &cur
, "", &other
, "", 0);
393 if (utime(rerere_path(name
, "postimage"), NULL
) < 0)
394 warning("failed utime() on %s: %s",
395 rerere_path(name
, "postimage"),
397 f
= fopen(path
, "w");
399 return error("Could not open %s: %s", path
,
401 if (fwrite(result
.ptr
, result
.size
, 1, f
) != 1)
402 error("Could not write %s: %s", path
, strerror(errno
));
404 return error("Writing %s failed: %s", path
,
417 static struct lock_file index_lock
;
419 static int update_paths(struct string_list
*update
)
422 int fd
= hold_locked_index(&index_lock
, 0);
428 for (i
= 0; i
< update
->nr
; i
++) {
429 struct string_list_item
*item
= &update
->items
[i
];
430 if (add_file_to_cache(item
->string
, ADD_CACHE_IGNORE_ERRORS
))
434 if (!status
&& active_cache_changed
) {
435 if (write_cache(fd
, active_cache
, active_nr
) ||
436 commit_locked_index(&index_lock
))
437 die("Unable to write new index file");
439 rollback_lock_file(&index_lock
);
443 static int do_plain_rerere(struct string_list
*rr
, int fd
)
445 struct string_list conflict
= STRING_LIST_INIT_DUP
;
446 struct string_list update
= STRING_LIST_INIT_DUP
;
449 find_conflict(&conflict
);
452 * MERGE_RR records paths with conflicts immediately after merge
453 * failed. Some of the conflicted paths might have been hand resolved
454 * in the working tree since then, but the initial run would catch all
455 * and register their preimages.
458 for (i
= 0; i
< conflict
.nr
; i
++) {
459 const char *path
= conflict
.items
[i
].string
;
460 if (!string_list_has_string(rr
, path
)) {
461 unsigned char sha1
[20];
464 ret
= handle_file(path
, sha1
, NULL
);
467 hex
= xstrdup(sha1_to_hex(sha1
));
468 string_list_insert(rr
, path
)->util
= hex
;
469 if (mkdir(git_path("rr-cache/%s", hex
), 0755))
471 handle_file(path
, NULL
, rerere_path(hex
, "preimage"));
472 fprintf(stderr
, "Recorded preimage for '%s'\n", path
);
477 * Now some of the paths that had conflicts earlier might have been
478 * hand resolved. Others may be similar to a conflict already that
479 * was resolved before.
482 for (i
= 0; i
< rr
->nr
; i
++) {
484 const char *path
= rr
->items
[i
].string
;
485 const char *name
= (const char *)rr
->items
[i
].util
;
487 if (has_rerere_resolution(name
)) {
488 if (!merge(name
, path
)) {
489 if (rerere_autoupdate
)
490 string_list_insert(&update
, path
);
492 "%s '%s' using previous resolution.\n",
494 ? "Staged" : "Resolved",
500 /* Let's see if we have resolved it. */
501 ret
= handle_file(path
, NULL
, NULL
);
505 fprintf(stderr
, "Recorded resolution for '%s'.\n", path
);
506 copy_file(rerere_path(name
, "postimage"), path
, 0666);
508 rr
->items
[i
].util
= NULL
;
512 update_paths(&update
);
514 return write_rr(rr
, fd
);
517 static int git_rerere_config(const char *var
, const char *value
, void *cb
)
519 if (!strcmp(var
, "rerere.enabled"))
520 rerere_enabled
= git_config_bool(var
, value
);
521 else if (!strcmp(var
, "rerere.autoupdate"))
522 rerere_autoupdate
= git_config_bool(var
, value
);
524 return git_default_config(var
, value
, cb
);
528 static int is_rerere_enabled(void)
530 const char *rr_cache
;
536 rr_cache
= git_path("rr-cache");
537 rr_cache_exists
= is_directory(rr_cache
);
538 if (rerere_enabled
< 0)
539 return rr_cache_exists
;
541 if (!rr_cache_exists
&&
542 (mkdir(rr_cache
, 0777) || adjust_shared_perm(rr_cache
)))
543 die("Could not create directory %s", rr_cache
);
547 int setup_rerere(struct string_list
*merge_rr
, int flags
)
551 git_config(git_rerere_config
, NULL
);
552 if (!is_rerere_enabled())
555 if (flags
& (RERERE_AUTOUPDATE
|RERERE_NOAUTOUPDATE
))
556 rerere_autoupdate
= !!(flags
& RERERE_AUTOUPDATE
);
557 merge_rr_path
= git_pathdup("MERGE_RR");
558 fd
= hold_lock_file_for_update(&write_lock
, merge_rr_path
,
564 int rerere(int flags
)
566 struct string_list merge_rr
= STRING_LIST_INIT_DUP
;
569 fd
= setup_rerere(&merge_rr
, flags
);
572 return do_plain_rerere(&merge_rr
, fd
);
575 static int rerere_forget_one_path(const char *path
, struct string_list
*rr
)
577 const char *filename
;
579 unsigned char sha1
[20];
582 ret
= handle_cache(path
, sha1
, NULL
);
584 return error("Could not parse conflict hunks in '%s'", path
);
585 hex
= xstrdup(sha1_to_hex(sha1
));
586 filename
= rerere_path(hex
, "postimage");
587 if (unlink(filename
))
588 return (errno
== ENOENT
589 ? error("no remembered resolution for %s", path
)
590 : error("cannot unlink %s: %s", filename
, strerror(errno
)));
592 handle_cache(path
, sha1
, rerere_path(hex
, "preimage"));
593 fprintf(stderr
, "Updated preimage for '%s'\n", path
);
596 string_list_insert(rr
, path
)->util
= hex
;
597 fprintf(stderr
, "Forgot resolution for %s\n", path
);
601 int rerere_forget(const char **pathspec
)
604 struct string_list conflict
= STRING_LIST_INIT_DUP
;
605 struct string_list merge_rr
= STRING_LIST_INIT_DUP
;
607 if (read_cache() < 0)
608 return error("Could not read index");
610 fd
= setup_rerere(&merge_rr
, RERERE_NOAUTOUPDATE
);
612 unmerge_cache(pathspec
);
613 find_conflict(&conflict
);
614 for (i
= 0; i
< conflict
.nr
; i
++) {
615 struct string_list_item
*it
= &conflict
.items
[i
];
616 if (!match_pathspec(pathspec
, it
->string
, strlen(it
->string
),
619 rerere_forget_one_path(it
->string
, &merge_rr
);
621 return write_rr(&merge_rr
, fd
);