fix openssl headers conflicting with custom SHA1 implementations
[git/git.git] / rerere.c
1 #include "cache.h"
2 #include "string-list.h"
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 */
8 static int rerere_enabled = -1;
9
10 /* automatically update cleanly resolved paths to the index */
11 static int rerere_autoupdate;
12
13 static char *merge_rr_path;
14
15 static const char *rr_path(const char *name, const char *file)
16 {
17 return git_path("rr-cache/%s/%s", name, file);
18 }
19
20 static int has_resolution(const char *name)
21 {
22 struct stat st;
23 return !stat(rr_path(name, "postimage"), &st);
24 }
25
26 static void read_rr(struct string_list *rr)
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");
46 string_list_insert(buf, rr)->util = name;
47 }
48 fclose(in);
49 }
50
51 static struct lock_file write_lock;
52
53 static int write_rr(struct string_list *rr, int out_fd)
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;
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");
67 }
68 if (commit_lock_file(&write_lock) != 0)
69 die("unable to write rerere record");
70 return 0;
71 }
72
73 static int handle_file(const char *path,
74 unsigned char *sha1, const char *output)
75 {
76 git_SHA_CTX ctx;
77 char buf[1024];
78 int hunk_no = 0;
79 enum {
80 RR_CONTEXT = 0, RR_SIDE_1, RR_SIDE_2, RR_ORIGINAL,
81 } hunk = RR_CONTEXT;
82 struct strbuf one, two;
83 FILE *f = fopen(path, "r");
84 FILE *out = NULL;
85
86 if (!f)
87 return error("Could not open %s", path);
88
89 if (output) {
90 out = fopen(output, "w");
91 if (!out) {
92 fclose(f);
93 return error("Could not write %s", output);
94 }
95 }
96
97 if (sha1)
98 git_SHA1_Init(&ctx);
99
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)
105 goto bad;
106 hunk = RR_SIDE_1;
107 } else if (!prefixcmp(buf, "|||||||") && isspace(buf[7])) {
108 if (hunk != RR_SIDE_1)
109 goto bad;
110 hunk = RR_ORIGINAL;
111 } else if (!prefixcmp(buf, "=======") && isspace(buf[7])) {
112 if (hunk != RR_SIDE_1 && hunk != RR_ORIGINAL)
113 goto bad;
114 hunk = RR_SIDE_2;
115 } else if (!prefixcmp(buf, ">>>>>>> ")) {
116 if (hunk != RR_SIDE_2)
117 goto bad;
118 if (strbuf_cmp(&one, &two) > 0)
119 strbuf_swap(&one, &two);
120 hunk_no++;
121 hunk = RR_CONTEXT;
122 if (out) {
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);
128 }
129 if (sha1) {
130 git_SHA1_Update(&ctx, one.buf ? one.buf : "",
131 one.len + 1);
132 git_SHA1_Update(&ctx, two.buf ? two.buf : "",
133 two.len + 1);
134 }
135 strbuf_reset(&one);
136 strbuf_reset(&two);
137 } else if (hunk == RR_SIDE_1)
138 strbuf_addstr(&one, buf);
139 else if (hunk == RR_ORIGINAL)
140 ; /* discard */
141 else if (hunk == RR_SIDE_2)
142 strbuf_addstr(&two, buf);
143 else if (out)
144 fputs(buf, out);
145 continue;
146 bad:
147 hunk = 99; /* force error exit */
148 break;
149 }
150 strbuf_release(&one);
151 strbuf_release(&two);
152
153 fclose(f);
154 if (out)
155 fclose(out);
156 if (sha1)
157 git_SHA1_Final(sha1, &ctx);
158 if (hunk != RR_CONTEXT) {
159 if (output)
160 unlink(output);
161 return error("Could not parse conflict hunks in %s", path);
162 }
163 return hunk_no;
164 }
165
166 static int find_conflict(struct string_list *conflict)
167 {
168 int i;
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 &&
175 ce_stage(e3) == 3 &&
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 */
181 }
182 }
183 return 0;
184 }
185
186 static int merge(const char *name, const char *path)
187 {
188 int ret;
189 mmfile_t cur, base, other;
190 mmbuffer_t result = {NULL, 0};
191 xpparam_t xpp = {XDF_NEED_MINIMAL};
192
193 if (handle_file(path, NULL, rr_path(name, "thisimage")) < 0)
194 return 1;
195
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")))
199 return 1;
200 ret = xdl_merge(&base, &cur, "", &other, "",
201 &xpp, XDL_MERGE_ZEALOUS, &result);
202 if (!ret) {
203 FILE *f = fopen(path, "w");
204 if (!f)
205 return error("Could not write to %s", path);
206 fwrite(result.ptr, result.size, 1, f);
207 fclose(f);
208 }
209
210 free(cur.ptr);
211 free(base.ptr);
212 free(other.ptr);
213 free(result.ptr);
214
215 return ret;
216 }
217
218 static struct lock_file index_lock;
219
220 static int update_paths(struct string_list *update)
221 {
222 int i;
223 int fd = hold_locked_index(&index_lock, 0);
224 int status = 0;
225
226 if (fd < 0)
227 return -1;
228
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))
232 status = -1;
233 }
234
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");
239 } else if (fd >= 0)
240 rollback_lock_file(&index_lock);
241 return status;
242 }
243
244 static int do_plain_rerere(struct string_list *rr, int fd)
245 {
246 struct string_list conflict = { NULL, 0, 0, 1 };
247 struct string_list update = { NULL, 0, 0, 1 };
248 int i;
249
250 find_conflict(&conflict);
251
252 /*
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.
257 */
258
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];
263 char *hex;
264 int ret;
265 ret = handle_file(path, sha1, NULL);
266 if (ret < 1)
267 continue;
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))
271 continue;;
272 handle_file(path, NULL, rr_path(hex, "preimage"));
273 fprintf(stderr, "Recorded preimage for '%s'\n", path);
274 }
275 }
276
277 /*
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.
281 */
282
283 for (i = 0; i < rr->nr; i++) {
284 int ret;
285 const char *path = rr->items[i].string;
286 const char *name = (const char *)rr->items[i].util;
287
288 if (has_resolution(name)) {
289 if (!merge(name, path)) {
290 if (rerere_autoupdate)
291 string_list_insert(path, &update);
292 fprintf(stderr,
293 "%s '%s' using previous resolution.\n",
294 rerere_autoupdate
295 ? "Staged" : "Resolved",
296 path);
297 goto mark_resolved;
298 }
299 }
300
301 /* Let's see if we have resolved it. */
302 ret = handle_file(path, NULL, NULL);
303 if (ret)
304 continue;
305
306 fprintf(stderr, "Recorded resolution for '%s'.\n", path);
307 copy_file(rr_path(name, "postimage"), path, 0666);
308 mark_resolved:
309 rr->items[i].util = NULL;
310 }
311
312 if (update.nr)
313 update_paths(&update);
314
315 return write_rr(rr, fd);
316 }
317
318 static int git_rerere_config(const char *var, const char *value, void *cb)
319 {
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);
324 else
325 return git_default_config(var, value, cb);
326 return 0;
327 }
328
329 static int is_rerere_enabled(void)
330 {
331 const char *rr_cache;
332 int rr_cache_exists;
333
334 if (!rerere_enabled)
335 return 0;
336
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;
341
342 if (!rr_cache_exists &&
343 (mkdir(rr_cache, 0777) || adjust_shared_perm(rr_cache)))
344 die("Could not create directory %s", rr_cache);
345 return 1;
346 }
347
348 int setup_rerere(struct string_list *merge_rr)
349 {
350 int fd;
351
352 git_config(git_rerere_config, NULL);
353 if (!is_rerere_enabled())
354 return -1;
355
356 merge_rr_path = xstrdup(git_path("MERGE_RR"));
357 fd = hold_lock_file_for_update(&write_lock, merge_rr_path, 1);
358 read_rr(merge_rr);
359 return fd;
360 }
361
362 int rerere(void)
363 {
364 struct string_list merge_rr = { NULL, 0, 0, 1 };
365 int fd;
366
367 fd = setup_rerere(&merge_rr);
368 if (fd < 0)
369 return 0;
370 return do_plain_rerere(&merge_rr, fd);
371 }