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
, two
;
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 strbuf_init(&one
, 0);
101 strbuf_init(&two
, 0);
102 while (fgets(buf
, sizeof(buf
), f
)) {
103 if (!prefixcmp(buf
, "<<<<<<< ")) {
104 if (hunk
!= RR_CONTEXT
)
107 } else if (!prefixcmp(buf
, "|||||||") && isspace(buf
[7])) {
108 if (hunk
!= RR_SIDE_1
)
111 } else if (!prefixcmp(buf
, "=======") && isspace(buf
[7])) {
112 if (hunk
!= RR_SIDE_1
&& hunk
!= RR_ORIGINAL
)
115 } else if (!prefixcmp(buf
, ">>>>>>> ")) {
116 if (hunk
!= RR_SIDE_2
)
118 if (strbuf_cmp(&one
, &two
) > 0)
119 strbuf_swap(&one
, &two
);
123 fputs("<<<<<<<\n", out
);
124 fwrite(one
.buf
, one
.len
, 1, out
);
125 fputs("=======\n", out
);
126 fwrite(two
.buf
, two
.len
, 1, out
);
127 fputs(">>>>>>>\n", out
);
130 SHA1_Update(&ctx
, one
.buf
? one
.buf
: "",
132 SHA1_Update(&ctx
, two
.buf
? two
.buf
: "",
137 } else if (hunk
== RR_SIDE_1
)
138 strbuf_addstr(&one
, buf
);
139 else if (hunk
== RR_ORIGINAL
)
141 else if (hunk
== RR_SIDE_2
)
142 strbuf_addstr(&two
, buf
);
147 hunk
= 99; /* force error exit */
150 strbuf_release(&one
);
151 strbuf_release(&two
);
157 SHA1_Final(sha1
, &ctx
);
158 if (hunk
!= RR_CONTEXT
) {
161 return error("Could not parse conflict hunks in %s", path
);
166 static int find_conflict(struct string_list
*conflict
)
169 if (read_cache() < 0)
170 return error("Could not read index");
171 for (i
= 0; i
+1 < active_nr
; i
++) {
172 struct cache_entry
*e2
= active_cache
[i
];
173 struct cache_entry
*e3
= active_cache
[i
+1];
174 if (ce_stage(e2
) == 2 &&
176 ce_same_name(e2
, e3
) &&
177 S_ISREG(e2
->ce_mode
) &&
178 S_ISREG(e3
->ce_mode
)) {
179 string_list_insert((const char *)e2
->name
, conflict
);
180 i
++; /* skip over both #2 and #3 */
186 static int merge(const char *name
, const char *path
)
189 mmfile_t cur
, base
, other
;
190 mmbuffer_t result
= {NULL
, 0};
191 xpparam_t xpp
= {XDF_NEED_MINIMAL
};
193 if (handle_file(path
, NULL
, rr_path(name
, "thisimage")) < 0)
196 if (read_mmfile(&cur
, rr_path(name
, "thisimage")) ||
197 read_mmfile(&base
, rr_path(name
, "preimage")) ||
198 read_mmfile(&other
, rr_path(name
, "postimage")))
200 ret
= xdl_merge(&base
, &cur
, "", &other
, "",
201 &xpp
, XDL_MERGE_ZEALOUS
, &result
);
203 FILE *f
= fopen(path
, "w");
205 return error("Could not write to %s", path
);
206 fwrite(result
.ptr
, result
.size
, 1, f
);
218 static struct lock_file index_lock
;
220 static int update_paths(struct string_list
*update
)
223 int fd
= hold_locked_index(&index_lock
, 0);
229 for (i
= 0; i
< update
->nr
; i
++) {
230 struct string_list_item
*item
= &update
->items
[i
];
231 if (add_file_to_cache(item
->string
, ADD_CACHE_IGNORE_ERRORS
))
235 if (!status
&& active_cache_changed
) {
236 if (write_cache(fd
, active_cache
, active_nr
) ||
237 commit_locked_index(&index_lock
))
238 die("Unable to write new index file");
240 rollback_lock_file(&index_lock
);
244 static int do_plain_rerere(struct string_list
*rr
, int fd
)
246 struct string_list conflict
= { NULL
, 0, 0, 1 };
247 struct string_list update
= { NULL
, 0, 0, 1 };
250 find_conflict(&conflict
);
253 * MERGE_RR records paths with conflicts immediately after merge
254 * failed. Some of the conflicted paths might have been hand resolved
255 * in the working tree since then, but the initial run would catch all
256 * and register their preimages.
259 for (i
= 0; i
< conflict
.nr
; i
++) {
260 const char *path
= conflict
.items
[i
].string
;
261 if (!string_list_has_string(rr
, path
)) {
262 unsigned char sha1
[20];
265 ret
= handle_file(path
, sha1
, NULL
);
268 hex
= xstrdup(sha1_to_hex(sha1
));
269 string_list_insert(path
, rr
)->util
= hex
;
270 if (mkdir(git_path("rr-cache/%s", hex
), 0755))
272 handle_file(path
, NULL
, rr_path(hex
, "preimage"));
273 fprintf(stderr
, "Recorded preimage for '%s'\n", path
);
278 * Now some of the paths that had conflicts earlier might have been
279 * hand resolved. Others may be similar to a conflict already that
280 * was resolved before.
283 for (i
= 0; i
< rr
->nr
; i
++) {
285 const char *path
= rr
->items
[i
].string
;
286 const char *name
= (const char *)rr
->items
[i
].util
;
288 if (has_resolution(name
)) {
289 if (!merge(name
, path
)) {
290 if (rerere_autoupdate
)
291 string_list_insert(path
, &update
);
293 "%s '%s' using previous resolution.\n",
295 ? "Staged" : "Resolved",
301 /* Let's see if we have resolved it. */
302 ret
= handle_file(path
, NULL
, NULL
);
306 fprintf(stderr
, "Recorded resolution for '%s'.\n", path
);
307 copy_file(rr_path(name
, "postimage"), path
, 0666);
309 rr
->items
[i
].util
= NULL
;
313 update_paths(&update
);
315 return write_rr(rr
, fd
);
318 static int git_rerere_config(const char *var
, const char *value
, void *cb
)
320 if (!strcmp(var
, "rerere.enabled"))
321 rerere_enabled
= git_config_bool(var
, value
);
322 else if (!strcmp(var
, "rerere.autoupdate"))
323 rerere_autoupdate
= git_config_bool(var
, value
);
325 return git_default_config(var
, value
, cb
);
329 static int is_rerere_enabled(void)
331 const char *rr_cache
;
337 rr_cache
= git_path("rr-cache");
338 rr_cache_exists
= is_directory(rr_cache
);
339 if (rerere_enabled
< 0)
340 return rr_cache_exists
;
342 if (!rr_cache_exists
&&
343 (mkdir(rr_cache
, 0777) || adjust_shared_perm(rr_cache
)))
344 die("Could not create directory %s", rr_cache
);
348 int setup_rerere(struct string_list
*merge_rr
)
352 git_config(git_rerere_config
, NULL
);
353 if (!is_rerere_enabled())
356 merge_rr_path
= xstrdup(git_path("MERGE_RR"));
357 fd
= hold_lock_file_for_update(&write_lock
, merge_rr_path
, 1);
364 struct string_list merge_rr
= { NULL
, 0, 0, 1 };
367 fd
= setup_rerere(&merge_rr
);
370 return do_plain_rerere(&merge_rr
, fd
);