Avoid using non-portable `echo -n` in tests.
[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 = STRBUF_INIT, two = STRBUF_INIT;
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         while (fgets(buf, sizeof(buf), f)) {
101                 if (!prefixcmp(buf, "<<<<<<< ")) {
102                         if (hunk != RR_CONTEXT)
103                                 goto bad;
104                         hunk = RR_SIDE_1;
105                 } else if (!prefixcmp(buf, "|||||||") && isspace(buf[7])) {
106                         if (hunk != RR_SIDE_1)
107                                 goto bad;
108                         hunk = RR_ORIGINAL;
109                 } else if (!prefixcmp(buf, "=======") && isspace(buf[7])) {
110                         if (hunk != RR_SIDE_1 && hunk != RR_ORIGINAL)
111                                 goto bad;
112                         hunk = RR_SIDE_2;
113                 } else if (!prefixcmp(buf, ">>>>>>> ")) {
114                         if (hunk != RR_SIDE_2)
115                                 goto bad;
116                         if (strbuf_cmp(&one, &two) > 0)
117                                 strbuf_swap(&one, &two);
118                         hunk_no++;
119                         hunk = RR_CONTEXT;
120                         if (out) {
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);
126                         }
127                         if (sha1) {
128                                 git_SHA1_Update(&ctx, one.buf ? one.buf : "",
129                                             one.len + 1);
130                                 git_SHA1_Update(&ctx, two.buf ? two.buf : "",
131                                             two.len + 1);
132                         }
133                         strbuf_reset(&one);
134                         strbuf_reset(&two);
135                 } else if (hunk == RR_SIDE_1)
136                         strbuf_addstr(&one, buf);
137                 else if (hunk == RR_ORIGINAL)
138                         ; /* discard */
139                 else if (hunk == RR_SIDE_2)
140                         strbuf_addstr(&two, buf);
141                 else if (out)
142                         fputs(buf, out);
143                 continue;
144         bad:
145                 hunk = 99; /* force error exit */
146                 break;
147         }
148         strbuf_release(&one);
149         strbuf_release(&two);
150
151         fclose(f);
152         if (out)
153                 fclose(out);
154         if (sha1)
155                 git_SHA1_Final(sha1, &ctx);
156         if (hunk != RR_CONTEXT) {
157                 if (output)
158                         unlink(output);
159                 return error("Could not parse conflict hunks in %s", path);
160         }
161         return hunk_no;
162 }
163
164 static int find_conflict(struct string_list *conflict)
165 {
166         int i;
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 &&
173                     ce_stage(e3) == 3 &&
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 */
179                 }
180         }
181         return 0;
182 }
183
184 static int merge(const char *name, const char *path)
185 {
186         int ret;
187         mmfile_t cur, base, other;
188         mmbuffer_t result = {NULL, 0};
189         xpparam_t xpp = {XDF_NEED_MINIMAL};
190
191         if (handle_file(path, NULL, rr_path(name, "thisimage")) < 0)
192                 return 1;
193
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")))
197                 return 1;
198         ret = xdl_merge(&base, &cur, "", &other, "",
199                         &xpp, XDL_MERGE_ZEALOUS, &result);
200         if (!ret) {
201                 FILE *f = fopen(path, "w");
202                 if (!f)
203                         return error("Could not write to %s", path);
204                 fwrite(result.ptr, result.size, 1, f);
205                 fclose(f);
206         }
207
208         free(cur.ptr);
209         free(base.ptr);
210         free(other.ptr);
211         free(result.ptr);
212
213         return ret;
214 }
215
216 static struct lock_file index_lock;
217
218 static int update_paths(struct string_list *update)
219 {
220         int i;
221         int fd = hold_locked_index(&index_lock, 0);
222         int status = 0;
223
224         if (fd < 0)
225                 return -1;
226
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))
230                         status = -1;
231         }
232
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");
237         } else if (fd >= 0)
238                 rollback_lock_file(&index_lock);
239         return status;
240 }
241
242 static int do_plain_rerere(struct string_list *rr, int fd)
243 {
244         struct string_list conflict = { NULL, 0, 0, 1 };
245         struct string_list update = { NULL, 0, 0, 1 };
246         int i;
247
248         find_conflict(&conflict);
249
250         /*
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.
255          */
256
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];
261                         char *hex;
262                         int ret;
263                         ret = handle_file(path, sha1, NULL);
264                         if (ret < 1)
265                                 continue;
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))
269                                 continue;;
270                         handle_file(path, NULL, rr_path(hex, "preimage"));
271                         fprintf(stderr, "Recorded preimage for '%s'\n", path);
272                 }
273         }
274
275         /*
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.
279          */
280
281         for (i = 0; i < rr->nr; i++) {
282                 int ret;
283                 const char *path = rr->items[i].string;
284                 const char *name = (const char *)rr->items[i].util;
285
286                 if (has_resolution(name)) {
287                         if (!merge(name, path)) {
288                                 if (rerere_autoupdate)
289                                         string_list_insert(path, &update);
290                                 fprintf(stderr,
291                                         "%s '%s' using previous resolution.\n",
292                                         rerere_autoupdate
293                                         ? "Staged" : "Resolved",
294                                         path);
295                                 goto mark_resolved;
296                         }
297                 }
298
299                 /* Let's see if we have resolved it. */
300                 ret = handle_file(path, NULL, NULL);
301                 if (ret)
302                         continue;
303
304                 fprintf(stderr, "Recorded resolution for '%s'.\n", path);
305                 copy_file(rr_path(name, "postimage"), path, 0666);
306         mark_resolved:
307                 rr->items[i].util = NULL;
308         }
309
310         if (update.nr)
311                 update_paths(&update);
312
313         return write_rr(rr, fd);
314 }
315
316 static int git_rerere_config(const char *var, const char *value, void *cb)
317 {
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);
322         else
323                 return git_default_config(var, value, cb);
324         return 0;
325 }
326
327 static int is_rerere_enabled(void)
328 {
329         const char *rr_cache;
330         int rr_cache_exists;
331
332         if (!rerere_enabled)
333                 return 0;
334
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;
339
340         if (!rr_cache_exists &&
341             (mkdir(rr_cache, 0777) || adjust_shared_perm(rr_cache)))
342                 die("Could not create directory %s", rr_cache);
343         return 1;
344 }
345
346 int setup_rerere(struct string_list *merge_rr)
347 {
348         int fd;
349
350         git_config(git_rerere_config, NULL);
351         if (!is_rerere_enabled())
352                 return -1;
353
354         merge_rr_path = xstrdup(git_path("MERGE_RR"));
355         fd = hold_lock_file_for_update(&write_lock, merge_rr_path,
356                                        LOCK_DIE_ON_ERROR);
357         read_rr(merge_rr);
358         return fd;
359 }
360
361 int rerere(void)
362 {
363         struct string_list merge_rr = { NULL, 0, 0, 1 };
364         int fd;
365
366         fd = setup_rerere(&merge_rr);
367         if (fd < 0)
368                 return 0;
369         return do_plain_rerere(&merge_rr, fd);
370 }