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
)
78 int hunk
= 0, hunk_no
= 0;
79 struct strbuf one
, two
;
80 FILE *f
= fopen(path
, "r");
84 return error("Could not open %s", path
);
87 out
= fopen(output
, "w");
90 return error("Could not write %s", output
);
99 while (fgets(buf
, sizeof(buf
), f
)) {
100 if (!prefixcmp(buf
, "<<<<<<< ")) {
104 } else if (!prefixcmp(buf
, "=======") && isspace(buf
[7])) {
108 } else if (!prefixcmp(buf
, ">>>>>>> ")) {
111 if (strbuf_cmp(&one
, &two
) > 0)
112 strbuf_swap(&one
, &two
);
116 fputs("<<<<<<<\n", out
);
117 fwrite(one
.buf
, one
.len
, 1, out
);
118 fputs("=======\n", out
);
119 fwrite(two
.buf
, two
.len
, 1, out
);
120 fputs(">>>>>>>\n", out
);
123 SHA1_Update(&ctx
, one
.buf
? one
.buf
: "",
125 SHA1_Update(&ctx
, two
.buf
? two
.buf
: "",
130 } else if (hunk
== 1)
131 strbuf_addstr(&one
, buf
);
133 strbuf_addstr(&two
, buf
);
138 hunk
= 99; /* force error exit */
141 strbuf_release(&one
);
142 strbuf_release(&two
);
148 SHA1_Final(sha1
, &ctx
);
152 return error("Could not parse conflict hunks in %s", path
);
157 static int find_conflict(struct string_list
*conflict
)
160 if (read_cache() < 0)
161 return error("Could not read index");
162 for (i
= 0; i
+1 < active_nr
; i
++) {
163 struct cache_entry
*e2
= active_cache
[i
];
164 struct cache_entry
*e3
= active_cache
[i
+1];
165 if (ce_stage(e2
) == 2 &&
167 ce_same_name(e2
, e3
) &&
168 S_ISREG(e2
->ce_mode
) &&
169 S_ISREG(e3
->ce_mode
)) {
170 string_list_insert((const char *)e2
->name
, conflict
);
171 i
++; /* skip over both #2 and #3 */
177 static int merge(const char *name
, const char *path
)
180 mmfile_t cur
, base
, other
;
181 mmbuffer_t result
= {NULL
, 0};
182 xpparam_t xpp
= {XDF_NEED_MINIMAL
};
184 if (handle_file(path
, NULL
, rr_path(name
, "thisimage")) < 0)
187 if (read_mmfile(&cur
, rr_path(name
, "thisimage")) ||
188 read_mmfile(&base
, rr_path(name
, "preimage")) ||
189 read_mmfile(&other
, rr_path(name
, "postimage")))
191 ret
= xdl_merge(&base
, &cur
, "", &other
, "",
192 &xpp
, XDL_MERGE_ZEALOUS
, &result
);
194 FILE *f
= fopen(path
, "w");
196 return error("Could not write to %s", path
);
197 fwrite(result
.ptr
, result
.size
, 1, f
);
209 static struct lock_file index_lock
;
211 static int update_paths(struct string_list
*update
)
214 int fd
= hold_locked_index(&index_lock
, 0);
220 for (i
= 0; i
< update
->nr
; i
++) {
221 struct string_list_item
*item
= &update
->items
[i
];
222 if (add_file_to_cache(item
->string
, ADD_CACHE_IGNORE_ERRORS
))
226 if (!status
&& active_cache_changed
) {
227 if (write_cache(fd
, active_cache
, active_nr
) ||
228 commit_locked_index(&index_lock
))
229 die("Unable to write new index file");
231 rollback_lock_file(&index_lock
);
235 static int do_plain_rerere(struct string_list
*rr
, int fd
)
237 struct string_list conflict
= { NULL
, 0, 0, 1 };
238 struct string_list update
= { NULL
, 0, 0, 1 };
241 find_conflict(&conflict
);
244 * MERGE_RR records paths with conflicts immediately after merge
245 * failed. Some of the conflicted paths might have been hand resolved
246 * in the working tree since then, but the initial run would catch all
247 * and register their preimages.
250 for (i
= 0; i
< conflict
.nr
; i
++) {
251 const char *path
= conflict
.items
[i
].string
;
252 if (!string_list_has_string(rr
, path
)) {
253 unsigned char sha1
[20];
256 ret
= handle_file(path
, sha1
, NULL
);
259 hex
= xstrdup(sha1_to_hex(sha1
));
260 string_list_insert(path
, rr
)->util
= hex
;
261 if (mkdir(git_path("rr-cache/%s", hex
), 0755))
263 handle_file(path
, NULL
, rr_path(hex
, "preimage"));
264 fprintf(stderr
, "Recorded preimage for '%s'\n", path
);
269 * Now some of the paths that had conflicts earlier might have been
270 * hand resolved. Others may be similar to a conflict already that
271 * was resolved before.
274 for (i
= 0; i
< rr
->nr
; i
++) {
276 const char *path
= rr
->items
[i
].string
;
277 const char *name
= (const char *)rr
->items
[i
].util
;
279 if (has_resolution(name
)) {
280 if (!merge(name
, path
)) {
281 if (rerere_autoupdate
)
282 string_list_insert(path
, &update
);
284 "%s '%s' using previous resolution.\n",
286 ? "Staged" : "Resolved",
292 /* Let's see if we have resolved it. */
293 ret
= handle_file(path
, NULL
, NULL
);
297 fprintf(stderr
, "Recorded resolution for '%s'.\n", path
);
298 copy_file(rr_path(name
, "postimage"), path
, 0666);
300 rr
->items
[i
].util
= NULL
;
304 update_paths(&update
);
306 return write_rr(rr
, fd
);
309 static int git_rerere_config(const char *var
, const char *value
, void *cb
)
311 if (!strcmp(var
, "rerere.enabled"))
312 rerere_enabled
= git_config_bool(var
, value
);
313 else if (!strcmp(var
, "rerere.autoupdate"))
314 rerere_autoupdate
= git_config_bool(var
, value
);
316 return git_default_config(var
, value
, cb
);
320 static int is_rerere_enabled(void)
323 const char *rr_cache
;
329 rr_cache
= git_path("rr-cache");
330 rr_cache_exists
= !stat(rr_cache
, &st
) && S_ISDIR(st
.st_mode
);
331 if (rerere_enabled
< 0)
332 return rr_cache_exists
;
334 if (!rr_cache_exists
&&
335 (mkdir(rr_cache
, 0777) || adjust_shared_perm(rr_cache
)))
336 die("Could not create directory %s", rr_cache
);
340 int setup_rerere(struct string_list
*merge_rr
)
344 git_config(git_rerere_config
, NULL
);
345 if (!is_rerere_enabled())
348 merge_rr_path
= git_pathdup("MERGE_RR");
349 fd
= hold_lock_file_for_update(&write_lock
, merge_rr_path
,
357 struct string_list merge_rr
= { NULL
, 0, 0, 1 };
360 fd
= setup_rerere(&merge_rr
);
363 return do_plain_rerere(&merge_rr
, fd
);