]>
git.ipfire.org Git - thirdparty/git.git/blob - rerere.c
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_putmem(const char *mem
, size_t sz
, struct rerere_io
*io
)
104 ferr_write(mem
, sz
, io
->output
, &io
->wrerror
);
107 struct rerere_io_file
{
112 static int rerere_file_getline(struct strbuf
*sb
, struct rerere_io
*io_
)
114 struct rerere_io_file
*io
= (struct rerere_io_file
*)io_
;
115 return strbuf_getwholeline(sb
, io
->input
, '\n');
118 static int handle_path(unsigned char *sha1
, struct rerere_io
*io
)
123 RR_CONTEXT
= 0, RR_SIDE_1
, RR_SIDE_2
, RR_ORIGINAL
,
125 struct strbuf one
= STRBUF_INIT
, two
= STRBUF_INIT
;
126 struct strbuf buf
= STRBUF_INIT
;
131 while (!io
->getline(&buf
, io
)) {
132 if (!prefixcmp(buf
.buf
, "<<<<<<< ")) {
133 if (hunk
!= RR_CONTEXT
)
136 } else if (!prefixcmp(buf
.buf
, "|||||||") && isspace(buf
.buf
[7])) {
137 if (hunk
!= RR_SIDE_1
)
140 } else if (!prefixcmp(buf
.buf
, "=======") && isspace(buf
.buf
[7])) {
141 if (hunk
!= RR_SIDE_1
&& hunk
!= RR_ORIGINAL
)
144 } else if (!prefixcmp(buf
.buf
, ">>>>>>> ")) {
145 if (hunk
!= RR_SIDE_2
)
147 if (strbuf_cmp(&one
, &two
) > 0)
148 strbuf_swap(&one
, &two
);
151 rerere_io_putstr("<<<<<<<\n", io
);
152 rerere_io_putmem(one
.buf
, one
.len
, io
);
153 rerere_io_putstr("=======\n", io
);
154 rerere_io_putmem(two
.buf
, two
.len
, io
);
155 rerere_io_putstr(">>>>>>>\n", io
);
157 git_SHA1_Update(&ctx
, one
.buf
? one
.buf
: "",
159 git_SHA1_Update(&ctx
, two
.buf
? two
.buf
: "",
164 } else if (hunk
== RR_SIDE_1
)
165 strbuf_addstr(&one
, buf
.buf
);
166 else if (hunk
== RR_ORIGINAL
)
168 else if (hunk
== RR_SIDE_2
)
169 strbuf_addstr(&two
, buf
.buf
);
171 rerere_io_putstr(buf
.buf
, io
);
174 hunk
= 99; /* force error exit */
177 strbuf_release(&one
);
178 strbuf_release(&two
);
179 strbuf_release(&buf
);
182 git_SHA1_Final(sha1
, &ctx
);
183 if (hunk
!= RR_CONTEXT
)
188 static int handle_file(const char *path
, unsigned char *sha1
, const char *output
)
191 struct rerere_io_file io
;
193 memset(&io
, 0, sizeof(io
));
194 io
.io
.getline
= rerere_file_getline
;
195 io
.input
= fopen(path
, "r");
198 return error("Could not open %s", path
);
201 io
.io
.output
= fopen(output
, "w");
204 return error("Could not write %s", output
);
208 hunk_no
= handle_path(sha1
, (struct rerere_io
*)&io
);
212 error("There were errors while writing %s (%s)",
213 path
, strerror(io
.io
.wrerror
));
214 if (io
.io
.output
&& fclose(io
.io
.output
))
215 io
.io
.wrerror
= error("Failed to flush %s: %s",
216 path
, strerror(errno
));
220 unlink_or_warn(output
);
221 return error("Could not parse conflict hunks in %s", path
);
228 struct rerere_io_mem
{
233 static int rerere_mem_getline(struct strbuf
*sb
, struct rerere_io
*io_
)
235 struct rerere_io_mem
*io
= (struct rerere_io_mem
*)io_
;
242 ep
= strchrnul(io
->input
.buf
, '\n');
245 len
= ep
- io
->input
.buf
;
246 strbuf_add(sb
, io
->input
.buf
, len
);
247 strbuf_remove(&io
->input
, 0, len
);
251 static int handle_cache(const char *path
, unsigned char *sha1
, const char *output
)
254 mmbuffer_t result
= {NULL
, 0};
255 struct cache_entry
*ce
;
256 int pos
, len
, i
, hunk_no
;
257 struct rerere_io_mem io
;
260 * Reproduce the conflicted merge in-core
263 pos
= cache_name_pos(path
, len
);
268 for (i
= 0; i
< 3; i
++) {
269 enum object_type type
;
273 mmfile
[i
].ptr
= NULL
;
274 if (active_nr
<= pos
)
276 ce
= active_cache
[pos
++];
277 if (ce_namelen(ce
) != len
|| memcmp(ce
->name
, path
, len
)
278 || ce_stage(ce
) != i
+ 1)
280 mmfile
[i
].ptr
= read_sha1_file(ce
->sha1
, &type
, &size
);
281 mmfile
[i
].size
= size
;
283 for (i
= 0; i
< 3; i
++) {
284 if (!mmfile
[i
].ptr
&& !mmfile
[i
].size
)
285 mmfile
[i
].ptr
= xstrdup("");
287 ll_merge(&result
, path
, &mmfile
[0],
289 &mmfile
[2], "theirs", 0);
290 for (i
= 0; i
< 3; i
++)
293 memset(&io
, 0, sizeof(&io
));
294 io
.io
.getline
= rerere_mem_getline
;
296 io
.io
.output
= fopen(output
, "w");
299 strbuf_init(&io
.input
, 0);
300 strbuf_attach(&io
.input
, result
.ptr
, result
.size
, result
.size
);
302 hunk_no
= handle_path(sha1
, (struct rerere_io
*)&io
);
303 strbuf_release(&io
.input
);
305 fclose(io
.io
.output
);
309 static int find_conflict(struct string_list
*conflict
)
312 if (read_cache() < 0)
313 return error("Could not read index");
314 for (i
= 0; i
+1 < active_nr
; i
++) {
315 struct cache_entry
*e2
= active_cache
[i
];
316 struct cache_entry
*e3
= active_cache
[i
+1];
317 if (ce_stage(e2
) == 2 &&
319 ce_same_name(e2
, e3
) &&
320 S_ISREG(e2
->ce_mode
) &&
321 S_ISREG(e3
->ce_mode
)) {
322 string_list_insert((const char *)e2
->name
, conflict
);
323 i
++; /* skip over both #2 and #3 */
329 static int merge(const char *name
, const char *path
)
332 mmfile_t cur
, base
, other
;
333 mmbuffer_t result
= {NULL
, 0};
335 if (handle_file(path
, NULL
, rerere_path(name
, "thisimage")) < 0)
338 if (read_mmfile(&cur
, rerere_path(name
, "thisimage")) ||
339 read_mmfile(&base
, rerere_path(name
, "preimage")) ||
340 read_mmfile(&other
, rerere_path(name
, "postimage")))
342 ret
= ll_merge(&result
, path
, &base
, &cur
, "", &other
, "", 0);
344 FILE *f
= fopen(path
, "w");
346 return error("Could not open %s: %s", path
,
348 if (fwrite(result
.ptr
, result
.size
, 1, f
) != 1)
349 error("Could not write %s: %s", path
, strerror(errno
));
351 return error("Writing %s failed: %s", path
,
363 static struct lock_file index_lock
;
365 static int update_paths(struct string_list
*update
)
368 int fd
= hold_locked_index(&index_lock
, 0);
374 for (i
= 0; i
< update
->nr
; i
++) {
375 struct string_list_item
*item
= &update
->items
[i
];
376 if (add_file_to_cache(item
->string
, ADD_CACHE_IGNORE_ERRORS
))
380 if (!status
&& active_cache_changed
) {
381 if (write_cache(fd
, active_cache
, active_nr
) ||
382 commit_locked_index(&index_lock
))
383 die("Unable to write new index file");
385 rollback_lock_file(&index_lock
);
389 static int do_plain_rerere(struct string_list
*rr
, int fd
)
391 struct string_list conflict
= { NULL
, 0, 0, 1 };
392 struct string_list update
= { NULL
, 0, 0, 1 };
395 find_conflict(&conflict
);
398 * MERGE_RR records paths with conflicts immediately after merge
399 * failed. Some of the conflicted paths might have been hand resolved
400 * in the working tree since then, but the initial run would catch all
401 * and register their preimages.
404 for (i
= 0; i
< conflict
.nr
; i
++) {
405 const char *path
= conflict
.items
[i
].string
;
406 if (!string_list_has_string(rr
, path
)) {
407 unsigned char sha1
[20];
410 ret
= handle_file(path
, sha1
, NULL
);
413 hex
= xstrdup(sha1_to_hex(sha1
));
414 string_list_insert(path
, rr
)->util
= hex
;
415 if (mkdir(git_path("rr-cache/%s", hex
), 0755))
417 handle_file(path
, NULL
, rerere_path(hex
, "preimage"));
418 fprintf(stderr
, "Recorded preimage for '%s'\n", path
);
423 * Now some of the paths that had conflicts earlier might have been
424 * hand resolved. Others may be similar to a conflict already that
425 * was resolved before.
428 for (i
= 0; i
< rr
->nr
; i
++) {
430 const char *path
= rr
->items
[i
].string
;
431 const char *name
= (const char *)rr
->items
[i
].util
;
433 if (has_rerere_resolution(name
)) {
434 if (!merge(name
, path
)) {
435 if (rerere_autoupdate
)
436 string_list_insert(path
, &update
);
438 "%s '%s' using previous resolution.\n",
440 ? "Staged" : "Resolved",
446 /* Let's see if we have resolved it. */
447 ret
= handle_file(path
, NULL
, NULL
);
451 fprintf(stderr
, "Recorded resolution for '%s'.\n", path
);
452 copy_file(rerere_path(name
, "postimage"), path
, 0666);
454 rr
->items
[i
].util
= NULL
;
458 update_paths(&update
);
460 return write_rr(rr
, fd
);
463 static int git_rerere_config(const char *var
, const char *value
, void *cb
)
465 if (!strcmp(var
, "rerere.enabled"))
466 rerere_enabled
= git_config_bool(var
, value
);
467 else if (!strcmp(var
, "rerere.autoupdate"))
468 rerere_autoupdate
= git_config_bool(var
, value
);
470 return git_default_config(var
, value
, cb
);
474 static int is_rerere_enabled(void)
476 const char *rr_cache
;
482 rr_cache
= git_path("rr-cache");
483 rr_cache_exists
= is_directory(rr_cache
);
484 if (rerere_enabled
< 0)
485 return rr_cache_exists
;
487 if (!rr_cache_exists
&&
488 (mkdir(rr_cache
, 0777) || adjust_shared_perm(rr_cache
)))
489 die("Could not create directory %s", rr_cache
);
493 int setup_rerere(struct string_list
*merge_rr
)
497 git_config(git_rerere_config
, NULL
);
498 if (!is_rerere_enabled())
501 merge_rr_path
= git_pathdup("MERGE_RR");
502 fd
= hold_lock_file_for_update(&write_lock
, merge_rr_path
,
510 struct string_list merge_rr
= { NULL
, 0, 0, 1 };
513 fd
= setup_rerere(&merge_rr
);
516 return do_plain_rerere(&merge_rr
, fd
);
519 static int rerere_forget_one_path(const char *path
, struct string_list
*rr
)
521 const char *filename
;
523 unsigned char sha1
[20];
526 ret
= handle_cache(path
, sha1
, NULL
);
528 return error("Could not parse conflict hunks in '%s'", path
);
529 hex
= xstrdup(sha1_to_hex(sha1
));
530 filename
= rerere_path(hex
, "postimage");
531 if (unlink(filename
))
532 return (errno
== ENOENT
533 ? error("no remembered resolution for %s", path
)
534 : error("cannot unlink %s: %s", filename
, strerror(errno
)));
536 handle_cache(path
, sha1
, rerere_path(hex
, "preimage"));
537 fprintf(stderr
, "Updated preimage for '%s'\n", path
);
540 string_list_insert(path
, rr
)->util
= hex
;
541 fprintf(stderr
, "Forgot resolution for %s\n", path
);
545 int rerere_forget(const char **pathspec
)
548 struct string_list conflict
= { NULL
, 0, 0, 1 };
549 struct string_list merge_rr
= { NULL
, 0, 0, 1 };
551 if (read_cache() < 0)
552 return error("Could not read index");
554 fd
= setup_rerere(&merge_rr
);
556 unmerge_cache(pathspec
);
557 find_conflict(&conflict
);
558 for (i
= 0; i
< conflict
.nr
; i
++) {
559 struct string_list_item
*it
= &conflict
.items
[i
];
560 if (!match_pathspec(pathspec
, it
->string
, strlen(it
->string
),
563 rerere_forget_one_path(it
->string
, &merge_rr
);
565 return write_rr(&merge_rr
, fd
);