2 #include "string-list.h"
4 #include "xdiff/xdiff.h"
5 #include "xdiff-interface.h"
7 /* if rerere_enabled == -1, fall back to detection of .git/rr-cache */
8 static int rerere_enabled
= -1;
10 /* automatically update cleanly resolved paths to the index */
11 static int rerere_autoupdate
;
13 static char *merge_rr_path
;
15 const char *rerere_path(const char *hex
, const char *file
)
17 return git_path("rr-cache/%s/%s", hex
, file
);
20 int has_rerere_resolution(const char *hex
)
23 return !stat(rerere_path(hex
, "postimage"), &st
);
26 static void read_rr(struct string_list
*rr
)
28 unsigned char sha1
[20];
30 FILE *in
= fopen(merge_rr_path
, "r");
33 while (fread(buf
, 40, 1, in
) == 1) {
36 if (get_sha1_hex(buf
, sha1
))
37 die("corrupt MERGE_RR");
40 if (fgetc(in
) != '\t')
41 die("corrupt MERGE_RR");
42 for (i
= 0; i
< sizeof(buf
) && (buf
[i
] = fgetc(in
)); i
++)
45 die("filename too long");
46 string_list_insert(buf
, rr
)->util
= name
;
51 static struct lock_file write_lock
;
53 static int write_rr(struct string_list
*rr
, int out_fd
)
56 for (i
= 0; i
< rr
->nr
; i
++) {
59 if (!rr
->items
[i
].util
)
61 path
= rr
->items
[i
].string
;
62 length
= strlen(path
) + 1;
63 if (write_in_full(out_fd
, rr
->items
[i
].util
, 40) != 40 ||
64 write_str_in_full(out_fd
, "\t") != 1 ||
65 write_in_full(out_fd
, path
, length
) != length
)
66 die("unable to write rerere record");
68 if (commit_lock_file(&write_lock
) != 0)
69 die("unable to write rerere record");
73 static void ferr_write(const void *p
, size_t count
, FILE *fp
, int *err
)
77 if (fwrite(p
, count
, 1, fp
) != 1)
81 static inline void ferr_puts(const char *s
, FILE *fp
, int *err
)
83 ferr_write(s
, strlen(s
), fp
, err
);
87 int (*getline
)(struct strbuf
*, struct rerere_io
*);
93 static void rerere_io_putstr(const char *str
, struct rerere_io
*io
)
96 ferr_puts(str
, io
->output
, &io
->wrerror
);
99 static void rerere_io_putmem(const char *mem
, size_t sz
, struct rerere_io
*io
)
102 ferr_write(mem
, sz
, io
->output
, &io
->wrerror
);
105 struct rerere_io_file
{
110 static int rerere_file_getline(struct strbuf
*sb
, struct rerere_io
*io_
)
112 struct rerere_io_file
*io
= (struct rerere_io_file
*)io_
;
113 return strbuf_getwholeline(sb
, io
->input
, '\n');
116 static int handle_path(unsigned char *sha1
, struct rerere_io
*io
)
121 RR_CONTEXT
= 0, RR_SIDE_1
, RR_SIDE_2
, RR_ORIGINAL
,
123 struct strbuf one
= STRBUF_INIT
, two
= STRBUF_INIT
;
124 struct strbuf buf
= STRBUF_INIT
;
129 while (!io
->getline(&buf
, io
)) {
130 if (!prefixcmp(buf
.buf
, "<<<<<<< ")) {
131 if (hunk
!= RR_CONTEXT
)
134 } else if (!prefixcmp(buf
.buf
, "|||||||") && isspace(buf
.buf
[7])) {
135 if (hunk
!= RR_SIDE_1
)
138 } else if (!prefixcmp(buf
.buf
, "=======") && isspace(buf
.buf
[7])) {
139 if (hunk
!= RR_SIDE_1
&& hunk
!= RR_ORIGINAL
)
142 } else if (!prefixcmp(buf
.buf
, ">>>>>>> ")) {
143 if (hunk
!= RR_SIDE_2
)
145 if (strbuf_cmp(&one
, &two
) > 0)
146 strbuf_swap(&one
, &two
);
149 rerere_io_putstr("<<<<<<<\n", io
);
150 rerere_io_putmem(one
.buf
, one
.len
, io
);
151 rerere_io_putstr("=======\n", io
);
152 rerere_io_putmem(two
.buf
, two
.len
, io
);
153 rerere_io_putstr(">>>>>>>\n", io
);
155 git_SHA1_Update(&ctx
, one
.buf
? one
.buf
: "",
157 git_SHA1_Update(&ctx
, two
.buf
? two
.buf
: "",
162 } else if (hunk
== RR_SIDE_1
)
163 strbuf_addstr(&one
, buf
.buf
);
164 else if (hunk
== RR_ORIGINAL
)
166 else if (hunk
== RR_SIDE_2
)
167 strbuf_addstr(&two
, buf
.buf
);
169 rerere_io_putstr(buf
.buf
, io
);
172 hunk
= 99; /* force error exit */
175 strbuf_release(&one
);
176 strbuf_release(&two
);
177 strbuf_release(&buf
);
180 git_SHA1_Final(sha1
, &ctx
);
181 if (hunk
!= RR_CONTEXT
)
186 static int handle_file(const char *path
, unsigned char *sha1
, const char *output
)
189 struct rerere_io_file io
;
191 memset(&io
, 0, sizeof(io
));
192 io
.io
.getline
= rerere_file_getline
;
193 io
.input
= fopen(path
, "r");
196 return error("Could not open %s", path
);
199 io
.io
.output
= fopen(output
, "w");
202 return error("Could not write %s", output
);
206 hunk_no
= handle_path(sha1
, (struct rerere_io
*)&io
);
210 error("There were errors while writing %s (%s)",
211 path
, strerror(io
.io
.wrerror
));
212 if (io
.io
.output
&& fclose(io
.io
.output
))
213 io
.io
.wrerror
= error("Failed to flush %s: %s",
214 path
, strerror(errno
));
218 unlink_or_warn(output
);
219 return error("Could not parse conflict hunks in %s", path
);
226 static int find_conflict(struct string_list
*conflict
)
229 if (read_cache() < 0)
230 return error("Could not read index");
231 for (i
= 0; i
+1 < active_nr
; i
++) {
232 struct cache_entry
*e2
= active_cache
[i
];
233 struct cache_entry
*e3
= active_cache
[i
+1];
234 if (ce_stage(e2
) == 2 &&
236 ce_same_name(e2
, e3
) &&
237 S_ISREG(e2
->ce_mode
) &&
238 S_ISREG(e3
->ce_mode
)) {
239 string_list_insert((const char *)e2
->name
, conflict
);
240 i
++; /* skip over both #2 and #3 */
246 static int merge(const char *name
, const char *path
)
249 mmfile_t cur
, base
, other
;
250 mmbuffer_t result
= {NULL
, 0};
251 xpparam_t xpp
= {XDF_NEED_MINIMAL
};
253 if (handle_file(path
, NULL
, rerere_path(name
, "thisimage")) < 0)
256 if (read_mmfile(&cur
, rerere_path(name
, "thisimage")) ||
257 read_mmfile(&base
, rerere_path(name
, "preimage")) ||
258 read_mmfile(&other
, rerere_path(name
, "postimage")))
260 ret
= xdl_merge(&base
, &cur
, "", &other
, "",
261 &xpp
, XDL_MERGE_ZEALOUS
, &result
);
263 FILE *f
= fopen(path
, "w");
265 return error("Could not open %s: %s", path
,
267 if (fwrite(result
.ptr
, result
.size
, 1, f
) != 1)
268 error("Could not write %s: %s", path
, strerror(errno
));
270 return error("Writing %s failed: %s", path
,
282 static struct lock_file index_lock
;
284 static int update_paths(struct string_list
*update
)
287 int fd
= hold_locked_index(&index_lock
, 0);
293 for (i
= 0; i
< update
->nr
; i
++) {
294 struct string_list_item
*item
= &update
->items
[i
];
295 if (add_file_to_cache(item
->string
, ADD_CACHE_IGNORE_ERRORS
))
299 if (!status
&& active_cache_changed
) {
300 if (write_cache(fd
, active_cache
, active_nr
) ||
301 commit_locked_index(&index_lock
))
302 die("Unable to write new index file");
304 rollback_lock_file(&index_lock
);
308 static int do_plain_rerere(struct string_list
*rr
, int fd
)
310 struct string_list conflict
= { NULL
, 0, 0, 1 };
311 struct string_list update
= { NULL
, 0, 0, 1 };
314 find_conflict(&conflict
);
317 * MERGE_RR records paths with conflicts immediately after merge
318 * failed. Some of the conflicted paths might have been hand resolved
319 * in the working tree since then, but the initial run would catch all
320 * and register their preimages.
323 for (i
= 0; i
< conflict
.nr
; i
++) {
324 const char *path
= conflict
.items
[i
].string
;
325 if (!string_list_has_string(rr
, path
)) {
326 unsigned char sha1
[20];
329 ret
= handle_file(path
, sha1
, NULL
);
332 hex
= xstrdup(sha1_to_hex(sha1
));
333 string_list_insert(path
, rr
)->util
= hex
;
334 if (mkdir(git_path("rr-cache/%s", hex
), 0755))
336 handle_file(path
, NULL
, rerere_path(hex
, "preimage"));
337 fprintf(stderr
, "Recorded preimage for '%s'\n", path
);
342 * Now some of the paths that had conflicts earlier might have been
343 * hand resolved. Others may be similar to a conflict already that
344 * was resolved before.
347 for (i
= 0; i
< rr
->nr
; i
++) {
349 const char *path
= rr
->items
[i
].string
;
350 const char *name
= (const char *)rr
->items
[i
].util
;
352 if (has_rerere_resolution(name
)) {
353 if (!merge(name
, path
)) {
354 if (rerere_autoupdate
)
355 string_list_insert(path
, &update
);
357 "%s '%s' using previous resolution.\n",
359 ? "Staged" : "Resolved",
365 /* Let's see if we have resolved it. */
366 ret
= handle_file(path
, NULL
, NULL
);
370 fprintf(stderr
, "Recorded resolution for '%s'.\n", path
);
371 copy_file(rerere_path(name
, "postimage"), path
, 0666);
373 rr
->items
[i
].util
= NULL
;
377 update_paths(&update
);
379 return write_rr(rr
, fd
);
382 static int git_rerere_config(const char *var
, const char *value
, void *cb
)
384 if (!strcmp(var
, "rerere.enabled"))
385 rerere_enabled
= git_config_bool(var
, value
);
386 else if (!strcmp(var
, "rerere.autoupdate"))
387 rerere_autoupdate
= git_config_bool(var
, value
);
389 return git_default_config(var
, value
, cb
);
393 static int is_rerere_enabled(void)
395 const char *rr_cache
;
401 rr_cache
= git_path("rr-cache");
402 rr_cache_exists
= is_directory(rr_cache
);
403 if (rerere_enabled
< 0)
404 return rr_cache_exists
;
406 if (!rr_cache_exists
&&
407 (mkdir(rr_cache
, 0777) || adjust_shared_perm(rr_cache
)))
408 die("Could not create directory %s", rr_cache
);
412 int setup_rerere(struct string_list
*merge_rr
)
416 git_config(git_rerere_config
, NULL
);
417 if (!is_rerere_enabled())
420 merge_rr_path
= git_pathdup("MERGE_RR");
421 fd
= hold_lock_file_for_update(&write_lock
, merge_rr_path
,
429 struct string_list merge_rr
= { NULL
, 0, 0, 1 };
432 fd
= setup_rerere(&merge_rr
);
435 return do_plain_rerere(&merge_rr
, fd
);