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 static const char *rr_path(const char *name
, const char *file
)
17 return git_path("rr-cache/%s/%s", name
, file
);
20 static int has_resolution(const char *name
)
23 return !stat(rr_path(name
, "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_in_full(out_fd
, "\t", 1) != 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 int handle_file(const char *path
,
74 unsigned char *sha1
, const char *output
)
80 RR_CONTEXT
= 0, RR_SIDE_1
, RR_SIDE_2
, RR_ORIGINAL
,
82 struct strbuf one
= STRBUF_INIT
, two
= STRBUF_INIT
;
83 FILE *f
= fopen(path
, "r");
87 return error("Could not open %s", path
);
90 out
= fopen(output
, "w");
93 return error("Could not write %s", output
);
100 while (fgets(buf
, sizeof(buf
), f
)) {
101 if (!prefixcmp(buf
, "<<<<<<< ")) {
102 if (hunk
!= RR_CONTEXT
)
105 } else if (!prefixcmp(buf
, "|||||||") && isspace(buf
[7])) {
106 if (hunk
!= RR_SIDE_1
)
109 } else if (!prefixcmp(buf
, "=======") && isspace(buf
[7])) {
110 if (hunk
!= RR_SIDE_1
&& hunk
!= RR_ORIGINAL
)
113 } else if (!prefixcmp(buf
, ">>>>>>> ")) {
114 if (hunk
!= RR_SIDE_2
)
116 if (strbuf_cmp(&one
, &two
) > 0)
117 strbuf_swap(&one
, &two
);
121 fputs("<<<<<<<\n", out
);
122 fwrite(one
.buf
, one
.len
, 1, out
);
123 fputs("=======\n", out
);
124 fwrite(two
.buf
, two
.len
, 1, out
);
125 fputs(">>>>>>>\n", out
);
128 git_SHA1_Update(&ctx
, one
.buf
? one
.buf
: "",
130 git_SHA1_Update(&ctx
, two
.buf
? two
.buf
: "",
135 } else if (hunk
== RR_SIDE_1
)
136 strbuf_addstr(&one
, buf
);
137 else if (hunk
== RR_ORIGINAL
)
139 else if (hunk
== RR_SIDE_2
)
140 strbuf_addstr(&two
, buf
);
145 hunk
= 99; /* force error exit */
148 strbuf_release(&one
);
149 strbuf_release(&two
);
155 git_SHA1_Final(sha1
, &ctx
);
156 if (hunk
!= RR_CONTEXT
) {
159 return error("Could not parse conflict hunks in %s", path
);
164 static int find_conflict(struct string_list
*conflict
)
167 if (read_cache() < 0)
168 return error("Could not read index");
169 for (i
= 0; i
+1 < active_nr
; i
++) {
170 struct cache_entry
*e2
= active_cache
[i
];
171 struct cache_entry
*e3
= active_cache
[i
+1];
172 if (ce_stage(e2
) == 2 &&
174 ce_same_name(e2
, e3
) &&
175 S_ISREG(e2
->ce_mode
) &&
176 S_ISREG(e3
->ce_mode
)) {
177 string_list_insert((const char *)e2
->name
, conflict
);
178 i
++; /* skip over both #2 and #3 */
184 static int merge(const char *name
, const char *path
)
187 mmfile_t cur
, base
, other
;
188 mmbuffer_t result
= {NULL
, 0};
189 xpparam_t xpp
= {XDF_NEED_MINIMAL
};
191 if (handle_file(path
, NULL
, rr_path(name
, "thisimage")) < 0)
194 if (read_mmfile(&cur
, rr_path(name
, "thisimage")) ||
195 read_mmfile(&base
, rr_path(name
, "preimage")) ||
196 read_mmfile(&other
, rr_path(name
, "postimage")))
198 ret
= xdl_merge(&base
, &cur
, "", &other
, "",
199 &xpp
, XDL_MERGE_ZEALOUS
, &result
);
201 FILE *f
= fopen(path
, "w");
203 return error("Could not write to %s", path
);
204 fwrite(result
.ptr
, result
.size
, 1, f
);
216 static struct lock_file index_lock
;
218 static int update_paths(struct string_list
*update
)
221 int fd
= hold_locked_index(&index_lock
, 0);
227 for (i
= 0; i
< update
->nr
; i
++) {
228 struct string_list_item
*item
= &update
->items
[i
];
229 if (add_file_to_cache(item
->string
, ADD_CACHE_IGNORE_ERRORS
))
233 if (!status
&& active_cache_changed
) {
234 if (write_cache(fd
, active_cache
, active_nr
) ||
235 commit_locked_index(&index_lock
))
236 die("Unable to write new index file");
238 rollback_lock_file(&index_lock
);
242 static int do_plain_rerere(struct string_list
*rr
, int fd
)
244 struct string_list conflict
= { NULL
, 0, 0, 1 };
245 struct string_list update
= { NULL
, 0, 0, 1 };
248 find_conflict(&conflict
);
251 * MERGE_RR records paths with conflicts immediately after merge
252 * failed. Some of the conflicted paths might have been hand resolved
253 * in the working tree since then, but the initial run would catch all
254 * and register their preimages.
257 for (i
= 0; i
< conflict
.nr
; i
++) {
258 const char *path
= conflict
.items
[i
].string
;
259 if (!string_list_has_string(rr
, path
)) {
260 unsigned char sha1
[20];
263 ret
= handle_file(path
, sha1
, NULL
);
266 hex
= xstrdup(sha1_to_hex(sha1
));
267 string_list_insert(path
, rr
)->util
= hex
;
268 if (mkdir(git_path("rr-cache/%s", hex
), 0755))
270 handle_file(path
, NULL
, rr_path(hex
, "preimage"));
271 fprintf(stderr
, "Recorded preimage for '%s'\n", path
);
276 * Now some of the paths that had conflicts earlier might have been
277 * hand resolved. Others may be similar to a conflict already that
278 * was resolved before.
281 for (i
= 0; i
< rr
->nr
; i
++) {
283 const char *path
= rr
->items
[i
].string
;
284 const char *name
= (const char *)rr
->items
[i
].util
;
286 if (has_resolution(name
)) {
287 if (!merge(name
, path
)) {
288 if (rerere_autoupdate
)
289 string_list_insert(path
, &update
);
291 "%s '%s' using previous resolution.\n",
293 ? "Staged" : "Resolved",
299 /* Let's see if we have resolved it. */
300 ret
= handle_file(path
, NULL
, NULL
);
304 fprintf(stderr
, "Recorded resolution for '%s'.\n", path
);
305 copy_file(rr_path(name
, "postimage"), path
, 0666);
307 rr
->items
[i
].util
= NULL
;
311 update_paths(&update
);
313 return write_rr(rr
, fd
);
316 static int git_rerere_config(const char *var
, const char *value
, void *cb
)
318 if (!strcmp(var
, "rerere.enabled"))
319 rerere_enabled
= git_config_bool(var
, value
);
320 else if (!strcmp(var
, "rerere.autoupdate"))
321 rerere_autoupdate
= git_config_bool(var
, value
);
323 return git_default_config(var
, value
, cb
);
327 static int is_rerere_enabled(void)
329 const char *rr_cache
;
335 rr_cache
= git_path("rr-cache");
336 rr_cache_exists
= is_directory(rr_cache
);
337 if (rerere_enabled
< 0)
338 return rr_cache_exists
;
340 if (!rr_cache_exists
&&
341 (mkdir(rr_cache
, 0777) || adjust_shared_perm(rr_cache
)))
342 die("Could not create directory %s", rr_cache
);
346 int setup_rerere(struct string_list
*merge_rr
)
350 git_config(git_rerere_config
, NULL
);
351 if (!is_rerere_enabled())
354 merge_rr_path
= git_pathdup("MERGE_RR");
355 fd
= hold_lock_file_for_update(&write_lock
, merge_rr_path
,
363 struct string_list merge_rr
= { NULL
, 0, 0, 1 };
366 fd
= setup_rerere(&merge_rr
);
369 return do_plain_rerere(&merge_rr
, fd
);