]> git.ipfire.org Git - thirdparty/git.git/blame - rerere.c
rerere: remove silly 1024-byte line limit
[thirdparty/git.git] / rerere.c
CommitLineData
5b2fd956 1#include "cache.h"
c455c87c 2#include "string-list.h"
5b2fd956
SB
3#include "rerere.h"
4#include "xdiff/xdiff.h"
5#include "xdiff-interface.h"
6
7/* if rerere_enabled == -1, fall back to detection of .git/rr-cache */
8static int rerere_enabled = -1;
9
10/* automatically update cleanly resolved paths to the index */
11static int rerere_autoupdate;
12
13static char *merge_rr_path;
14
90056966 15const char *rerere_path(const char *hex, const char *file)
5b2fd956 16{
90056966 17 return git_path("rr-cache/%s/%s", hex, file);
5b2fd956
SB
18}
19
90056966 20int has_rerere_resolution(const char *hex)
5b2fd956
SB
21{
22 struct stat st;
90056966 23 return !stat(rerere_path(hex, "postimage"), &st);
5b2fd956
SB
24}
25
c455c87c 26static void read_rr(struct string_list *rr)
5b2fd956
SB
27{
28 unsigned char sha1[20];
29 char buf[PATH_MAX];
30 FILE *in = fopen(merge_rr_path, "r");
31 if (!in)
32 return;
33 while (fread(buf, 40, 1, in) == 1) {
34 int i;
35 char *name;
36 if (get_sha1_hex(buf, sha1))
37 die("corrupt MERGE_RR");
38 buf[40] = '\0';
39 name = xstrdup(buf);
40 if (fgetc(in) != '\t')
41 die("corrupt MERGE_RR");
42 for (i = 0; i < sizeof(buf) && (buf[i] = fgetc(in)); i++)
43 ; /* do nothing */
44 if (i == sizeof(buf))
45 die("filename too long");
c455c87c 46 string_list_insert(buf, rr)->util = name;
5b2fd956
SB
47 }
48 fclose(in);
49}
50
51static struct lock_file write_lock;
52
c455c87c 53static int write_rr(struct string_list *rr, int out_fd)
5b2fd956
SB
54{
55 int i;
56 for (i = 0; i < rr->nr; i++) {
57 const char *path;
58 int length;
59 if (!rr->items[i].util)
60 continue;
c455c87c 61 path = rr->items[i].string;
5b2fd956
SB
62 length = strlen(path) + 1;
63 if (write_in_full(out_fd, rr->items[i].util, 40) != 40 ||
2b7ca830 64 write_str_in_full(out_fd, "\t") != 1 ||
5b2fd956
SB
65 write_in_full(out_fd, path, length) != length)
66 die("unable to write rerere record");
67 }
68 if (commit_lock_file(&write_lock) != 0)
69 die("unable to write rerere record");
70 return 0;
71}
72
47d32af2
AR
73static void ferr_write(const void *p, size_t count, FILE *fp, int *err)
74{
75 if (!count || *err)
76 return;
77 if (fwrite(p, count, 1, fp) != 1)
78 *err = errno;
79}
80
81static inline void ferr_puts(const char *s, FILE *fp, int *err)
82{
83 ferr_write(s, strlen(s), fp, err);
84}
85
5b2fd956
SB
86static int handle_file(const char *path,
87 unsigned char *sha1, const char *output)
88{
9126f009 89 git_SHA_CTX ctx;
cc58d7df
JH
90 int hunk_no = 0;
91 enum {
387c9d49 92 RR_CONTEXT = 0, RR_SIDE_1, RR_SIDE_2, RR_ORIGINAL,
cc58d7df 93 } hunk = RR_CONTEXT;
f285a2d7 94 struct strbuf one = STRBUF_INIT, two = STRBUF_INIT;
d58ee6db 95 struct strbuf buf = STRBUF_INIT;
5b2fd956
SB
96 FILE *f = fopen(path, "r");
97 FILE *out = NULL;
47d32af2 98 int wrerror = 0;
5b2fd956
SB
99
100 if (!f)
101 return error("Could not open %s", path);
102
103 if (output) {
104 out = fopen(output, "w");
105 if (!out) {
106 fclose(f);
107 return error("Could not write %s", output);
108 }
109 }
110
111 if (sha1)
9126f009 112 git_SHA1_Init(&ctx);
5b2fd956 113
d58ee6db
JH
114 while (!strbuf_getwholeline(&buf, f, '\n')) {
115 if (!prefixcmp(buf.buf, "<<<<<<< ")) {
cc58d7df 116 if (hunk != RR_CONTEXT)
5b2fd956 117 goto bad;
cc58d7df 118 hunk = RR_SIDE_1;
d58ee6db 119 } else if (!prefixcmp(buf.buf, "|||||||") && isspace(buf.buf[7])) {
cc58d7df 120 if (hunk != RR_SIDE_1)
5b2fd956 121 goto bad;
387c9d49 122 hunk = RR_ORIGINAL;
d58ee6db 123 } else if (!prefixcmp(buf.buf, "=======") && isspace(buf.buf[7])) {
387c9d49 124 if (hunk != RR_SIDE_1 && hunk != RR_ORIGINAL)
5b2fd956 125 goto bad;
cc58d7df 126 hunk = RR_SIDE_2;
d58ee6db 127 } else if (!prefixcmp(buf.buf, ">>>>>>> ")) {
cc58d7df 128 if (hunk != RR_SIDE_2)
5b2fd956
SB
129 goto bad;
130 if (strbuf_cmp(&one, &two) > 0)
131 strbuf_swap(&one, &two);
132 hunk_no++;
cc58d7df 133 hunk = RR_CONTEXT;
5b2fd956 134 if (out) {
47d32af2
AR
135 ferr_puts("<<<<<<<\n", out, &wrerror);
136 ferr_write(one.buf, one.len, out, &wrerror);
137 ferr_puts("=======\n", out, &wrerror);
138 ferr_write(two.buf, two.len, out, &wrerror);
139 ferr_puts(">>>>>>>\n", out, &wrerror);
5b2fd956
SB
140 }
141 if (sha1) {
9126f009 142 git_SHA1_Update(&ctx, one.buf ? one.buf : "",
5b2fd956 143 one.len + 1);
9126f009 144 git_SHA1_Update(&ctx, two.buf ? two.buf : "",
5b2fd956
SB
145 two.len + 1);
146 }
147 strbuf_reset(&one);
148 strbuf_reset(&two);
cc58d7df 149 } else if (hunk == RR_SIDE_1)
d58ee6db 150 strbuf_addstr(&one, buf.buf);
387c9d49
JH
151 else if (hunk == RR_ORIGINAL)
152 ; /* discard */
cc58d7df 153 else if (hunk == RR_SIDE_2)
d58ee6db 154 strbuf_addstr(&two, buf.buf);
5b2fd956 155 else if (out)
d58ee6db 156 ferr_puts(buf.buf, out, &wrerror);
5b2fd956
SB
157 continue;
158 bad:
159 hunk = 99; /* force error exit */
160 break;
161 }
162 strbuf_release(&one);
163 strbuf_release(&two);
d58ee6db 164 strbuf_release(&buf);
5b2fd956
SB
165
166 fclose(f);
47d32af2
AR
167 if (wrerror)
168 error("There were errors while writing %s (%s)",
169 path, strerror(wrerror));
170 if (out && fclose(out))
171 wrerror = error("Failed to flush %s: %s",
172 path, strerror(errno));
5b2fd956 173 if (sha1)
9126f009 174 git_SHA1_Final(sha1, &ctx);
cc58d7df 175 if (hunk != RR_CONTEXT) {
5b2fd956 176 if (output)
691f1a28 177 unlink_or_warn(output);
5b2fd956
SB
178 return error("Could not parse conflict hunks in %s", path);
179 }
47d32af2
AR
180 if (wrerror)
181 return -1;
5b2fd956
SB
182 return hunk_no;
183}
184
c455c87c 185static int find_conflict(struct string_list *conflict)
5b2fd956
SB
186{
187 int i;
188 if (read_cache() < 0)
189 return error("Could not read index");
190 for (i = 0; i+1 < active_nr; i++) {
191 struct cache_entry *e2 = active_cache[i];
192 struct cache_entry *e3 = active_cache[i+1];
193 if (ce_stage(e2) == 2 &&
194 ce_stage(e3) == 3 &&
195 ce_same_name(e2, e3) &&
196 S_ISREG(e2->ce_mode) &&
197 S_ISREG(e3->ce_mode)) {
c455c87c 198 string_list_insert((const char *)e2->name, conflict);
5b2fd956
SB
199 i++; /* skip over both #2 and #3 */
200 }
201 }
202 return 0;
203}
204
205static int merge(const char *name, const char *path)
206{
207 int ret;
208 mmfile_t cur, base, other;
209 mmbuffer_t result = {NULL, 0};
210 xpparam_t xpp = {XDF_NEED_MINIMAL};
211
90056966 212 if (handle_file(path, NULL, rerere_path(name, "thisimage")) < 0)
5b2fd956
SB
213 return 1;
214
90056966
SG
215 if (read_mmfile(&cur, rerere_path(name, "thisimage")) ||
216 read_mmfile(&base, rerere_path(name, "preimage")) ||
217 read_mmfile(&other, rerere_path(name, "postimage")))
5b2fd956
SB
218 return 1;
219 ret = xdl_merge(&base, &cur, "", &other, "",
220 &xpp, XDL_MERGE_ZEALOUS, &result);
221 if (!ret) {
222 FILE *f = fopen(path, "w");
223 if (!f)
47d32af2
AR
224 return error("Could not open %s: %s", path,
225 strerror(errno));
226 if (fwrite(result.ptr, result.size, 1, f) != 1)
227 error("Could not write %s: %s", path, strerror(errno));
228 if (fclose(f))
229 return error("Writing %s failed: %s", path,
230 strerror(errno));
5b2fd956
SB
231 }
232
233 free(cur.ptr);
234 free(base.ptr);
235 free(other.ptr);
236 free(result.ptr);
237
238 return ret;
239}
240
241static struct lock_file index_lock;
242
c455c87c 243static int update_paths(struct string_list *update)
5b2fd956
SB
244{
245 int i;
246 int fd = hold_locked_index(&index_lock, 0);
247 int status = 0;
248
249 if (fd < 0)
250 return -1;
251
252 for (i = 0; i < update->nr; i++) {
c455c87c
JS
253 struct string_list_item *item = &update->items[i];
254 if (add_file_to_cache(item->string, ADD_CACHE_IGNORE_ERRORS))
5b2fd956
SB
255 status = -1;
256 }
257
258 if (!status && active_cache_changed) {
259 if (write_cache(fd, active_cache, active_nr) ||
260 commit_locked_index(&index_lock))
261 die("Unable to write new index file");
262 } else if (fd >= 0)
263 rollback_lock_file(&index_lock);
264 return status;
265}
266
c455c87c 267static int do_plain_rerere(struct string_list *rr, int fd)
5b2fd956 268{
c455c87c
JS
269 struct string_list conflict = { NULL, 0, 0, 1 };
270 struct string_list update = { NULL, 0, 0, 1 };
5b2fd956
SB
271 int i;
272
273 find_conflict(&conflict);
274
275 /*
276 * MERGE_RR records paths with conflicts immediately after merge
277 * failed. Some of the conflicted paths might have been hand resolved
278 * in the working tree since then, but the initial run would catch all
279 * and register their preimages.
280 */
281
282 for (i = 0; i < conflict.nr; i++) {
c455c87c
JS
283 const char *path = conflict.items[i].string;
284 if (!string_list_has_string(rr, path)) {
5b2fd956
SB
285 unsigned char sha1[20];
286 char *hex;
287 int ret;
288 ret = handle_file(path, sha1, NULL);
289 if (ret < 1)
290 continue;
291 hex = xstrdup(sha1_to_hex(sha1));
c455c87c 292 string_list_insert(path, rr)->util = hex;
5b2fd956 293 if (mkdir(git_path("rr-cache/%s", hex), 0755))
ba19a808 294 continue;
90056966 295 handle_file(path, NULL, rerere_path(hex, "preimage"));
5b2fd956
SB
296 fprintf(stderr, "Recorded preimage for '%s'\n", path);
297 }
298 }
299
300 /*
301 * Now some of the paths that had conflicts earlier might have been
302 * hand resolved. Others may be similar to a conflict already that
303 * was resolved before.
304 */
305
306 for (i = 0; i < rr->nr; i++) {
307 int ret;
c455c87c 308 const char *path = rr->items[i].string;
5b2fd956
SB
309 const char *name = (const char *)rr->items[i].util;
310
90056966 311 if (has_rerere_resolution(name)) {
5b2fd956 312 if (!merge(name, path)) {
5b2fd956 313 if (rerere_autoupdate)
c455c87c 314 string_list_insert(path, &update);
9196e825
JH
315 fprintf(stderr,
316 "%s '%s' using previous resolution.\n",
317 rerere_autoupdate
318 ? "Staged" : "Resolved",
319 path);
5b2fd956
SB
320 goto mark_resolved;
321 }
322 }
323
324 /* Let's see if we have resolved it. */
325 ret = handle_file(path, NULL, NULL);
326 if (ret)
327 continue;
328
329 fprintf(stderr, "Recorded resolution for '%s'.\n", path);
90056966 330 copy_file(rerere_path(name, "postimage"), path, 0666);
5b2fd956
SB
331 mark_resolved:
332 rr->items[i].util = NULL;
333 }
334
335 if (update.nr)
336 update_paths(&update);
337
338 return write_rr(rr, fd);
339}
340
341static int git_rerere_config(const char *var, const char *value, void *cb)
342{
343 if (!strcmp(var, "rerere.enabled"))
344 rerere_enabled = git_config_bool(var, value);
345 else if (!strcmp(var, "rerere.autoupdate"))
346 rerere_autoupdate = git_config_bool(var, value);
347 else
348 return git_default_config(var, value, cb);
349 return 0;
350}
351
352static int is_rerere_enabled(void)
353{
5b2fd956
SB
354 const char *rr_cache;
355 int rr_cache_exists;
356
357 if (!rerere_enabled)
358 return 0;
359
360 rr_cache = git_path("rr-cache");
90b4a71c 361 rr_cache_exists = is_directory(rr_cache);
5b2fd956
SB
362 if (rerere_enabled < 0)
363 return rr_cache_exists;
364
365 if (!rr_cache_exists &&
366 (mkdir(rr_cache, 0777) || adjust_shared_perm(rr_cache)))
367 die("Could not create directory %s", rr_cache);
368 return 1;
369}
370
c455c87c 371int setup_rerere(struct string_list *merge_rr)
5b2fd956
SB
372{
373 int fd;
374
375 git_config(git_rerere_config, NULL);
376 if (!is_rerere_enabled())
377 return -1;
378
a4f34cbb 379 merge_rr_path = git_pathdup("MERGE_RR");
acd3b9ec
JH
380 fd = hold_lock_file_for_update(&write_lock, merge_rr_path,
381 LOCK_DIE_ON_ERROR);
5b2fd956
SB
382 read_rr(merge_rr);
383 return fd;
384}
385
386int rerere(void)
387{
c455c87c 388 struct string_list merge_rr = { NULL, 0, 0, 1 };
5b2fd956
SB
389 int fd;
390
391 fd = setup_rerere(&merge_rr);
392 if (fd < 0)
393 return 0;
394 return do_plain_rerere(&merge_rr, fd);
395}