Stephan Beyer | 5b2fd95 | 2008-07-09 14:58:57 +0200 | [diff] [blame] | 1 | #include "cache.h" |
Johannes Schindelin | c455c87 | 2008-07-21 19:03:49 +0100 | [diff] [blame] | 2 | #include "string-list.h" |
Stephan Beyer | 5b2fd95 | 2008-07-09 14:58:57 +0200 | [diff] [blame] | 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 | |
Johannes Schindelin | c455c87 | 2008-07-21 19:03:49 +0100 | [diff] [blame] | 26 | static void read_rr(struct string_list *rr) |
Stephan Beyer | 5b2fd95 | 2008-07-09 14:58:57 +0200 | [diff] [blame] | 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"); |
Johannes Schindelin | c455c87 | 2008-07-21 19:03:49 +0100 | [diff] [blame] | 46 | string_list_insert(buf, rr)->util = name; |
Stephan Beyer | 5b2fd95 | 2008-07-09 14:58:57 +0200 | [diff] [blame] | 47 | } |
| 48 | fclose(in); |
| 49 | } |
| 50 | |
| 51 | static struct lock_file write_lock; |
| 52 | |
Johannes Schindelin | c455c87 | 2008-07-21 19:03:49 +0100 | [diff] [blame] | 53 | static int write_rr(struct string_list *rr, int out_fd) |
Stephan Beyer | 5b2fd95 | 2008-07-09 14:58:57 +0200 | [diff] [blame] | 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; |
Johannes Schindelin | c455c87 | 2008-07-21 19:03:49 +0100 | [diff] [blame] | 61 | path = rr->items[i].string; |
Stephan Beyer | 5b2fd95 | 2008-07-09 14:58:57 +0200 | [diff] [blame] | 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 | |
Alex Riesen | 47d32af | 2008-12-05 01:35:48 +0100 | [diff] [blame] | 73 | static 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 | |
| 81 | static inline void ferr_puts(const char *s, FILE *fp, int *err) |
| 82 | { |
| 83 | ferr_write(s, strlen(s), fp, err); |
| 84 | } |
| 85 | |
Stephan Beyer | 5b2fd95 | 2008-07-09 14:58:57 +0200 | [diff] [blame] | 86 | static int handle_file(const char *path, |
| 87 | unsigned char *sha1, const char *output) |
| 88 | { |
Nicolas Pitre | 9126f00 | 2008-10-01 14:05:20 -0400 | [diff] [blame] | 89 | git_SHA_CTX ctx; |
Stephan Beyer | 5b2fd95 | 2008-07-09 14:58:57 +0200 | [diff] [blame] | 90 | char buf[1024]; |
Junio C Hamano | cc58d7d | 2008-08-29 10:12:23 -0700 | [diff] [blame] | 91 | int hunk_no = 0; |
| 92 | enum { |
Junio C Hamano | 387c9d4 | 2008-08-29 10:24:45 -0700 | [diff] [blame] | 93 | RR_CONTEXT = 0, RR_SIDE_1, RR_SIDE_2, RR_ORIGINAL, |
Junio C Hamano | cc58d7d | 2008-08-29 10:12:23 -0700 | [diff] [blame] | 94 | } hunk = RR_CONTEXT; |
Brandon Casey | f285a2d | 2008-10-09 14:12:12 -0500 | [diff] [blame] | 95 | struct strbuf one = STRBUF_INIT, two = STRBUF_INIT; |
Stephan Beyer | 5b2fd95 | 2008-07-09 14:58:57 +0200 | [diff] [blame] | 96 | FILE *f = fopen(path, "r"); |
| 97 | FILE *out = NULL; |
Alex Riesen | 47d32af | 2008-12-05 01:35:48 +0100 | [diff] [blame] | 98 | int wrerror = 0; |
Stephan Beyer | 5b2fd95 | 2008-07-09 14:58:57 +0200 | [diff] [blame] | 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) |
Nicolas Pitre | 9126f00 | 2008-10-01 14:05:20 -0400 | [diff] [blame] | 112 | git_SHA1_Init(&ctx); |
Stephan Beyer | 5b2fd95 | 2008-07-09 14:58:57 +0200 | [diff] [blame] | 113 | |
Stephan Beyer | 5b2fd95 | 2008-07-09 14:58:57 +0200 | [diff] [blame] | 114 | while (fgets(buf, sizeof(buf), f)) { |
| 115 | if (!prefixcmp(buf, "<<<<<<< ")) { |
Junio C Hamano | cc58d7d | 2008-08-29 10:12:23 -0700 | [diff] [blame] | 116 | if (hunk != RR_CONTEXT) |
Stephan Beyer | 5b2fd95 | 2008-07-09 14:58:57 +0200 | [diff] [blame] | 117 | goto bad; |
Junio C Hamano | cc58d7d | 2008-08-29 10:12:23 -0700 | [diff] [blame] | 118 | hunk = RR_SIDE_1; |
Junio C Hamano | 387c9d4 | 2008-08-29 10:24:45 -0700 | [diff] [blame] | 119 | } else if (!prefixcmp(buf, "|||||||") && isspace(buf[7])) { |
Junio C Hamano | cc58d7d | 2008-08-29 10:12:23 -0700 | [diff] [blame] | 120 | if (hunk != RR_SIDE_1) |
Stephan Beyer | 5b2fd95 | 2008-07-09 14:58:57 +0200 | [diff] [blame] | 121 | goto bad; |
Junio C Hamano | 387c9d4 | 2008-08-29 10:24:45 -0700 | [diff] [blame] | 122 | hunk = RR_ORIGINAL; |
Stephan Beyer | 5b2fd95 | 2008-07-09 14:58:57 +0200 | [diff] [blame] | 123 | } else if (!prefixcmp(buf, "=======") && isspace(buf[7])) { |
Junio C Hamano | 387c9d4 | 2008-08-29 10:24:45 -0700 | [diff] [blame] | 124 | if (hunk != RR_SIDE_1 && hunk != RR_ORIGINAL) |
Stephan Beyer | 5b2fd95 | 2008-07-09 14:58:57 +0200 | [diff] [blame] | 125 | goto bad; |
Junio C Hamano | cc58d7d | 2008-08-29 10:12:23 -0700 | [diff] [blame] | 126 | hunk = RR_SIDE_2; |
Stephan Beyer | 5b2fd95 | 2008-07-09 14:58:57 +0200 | [diff] [blame] | 127 | } else if (!prefixcmp(buf, ">>>>>>> ")) { |
Junio C Hamano | cc58d7d | 2008-08-29 10:12:23 -0700 | [diff] [blame] | 128 | if (hunk != RR_SIDE_2) |
Stephan Beyer | 5b2fd95 | 2008-07-09 14:58:57 +0200 | [diff] [blame] | 129 | goto bad; |
| 130 | if (strbuf_cmp(&one, &two) > 0) |
| 131 | strbuf_swap(&one, &two); |
| 132 | hunk_no++; |
Junio C Hamano | cc58d7d | 2008-08-29 10:12:23 -0700 | [diff] [blame] | 133 | hunk = RR_CONTEXT; |
Stephan Beyer | 5b2fd95 | 2008-07-09 14:58:57 +0200 | [diff] [blame] | 134 | if (out) { |
Alex Riesen | 47d32af | 2008-12-05 01:35:48 +0100 | [diff] [blame] | 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); |
Stephan Beyer | 5b2fd95 | 2008-07-09 14:58:57 +0200 | [diff] [blame] | 140 | } |
| 141 | if (sha1) { |
Nicolas Pitre | 9126f00 | 2008-10-01 14:05:20 -0400 | [diff] [blame] | 142 | git_SHA1_Update(&ctx, one.buf ? one.buf : "", |
Stephan Beyer | 5b2fd95 | 2008-07-09 14:58:57 +0200 | [diff] [blame] | 143 | one.len + 1); |
Nicolas Pitre | 9126f00 | 2008-10-01 14:05:20 -0400 | [diff] [blame] | 144 | git_SHA1_Update(&ctx, two.buf ? two.buf : "", |
Stephan Beyer | 5b2fd95 | 2008-07-09 14:58:57 +0200 | [diff] [blame] | 145 | two.len + 1); |
| 146 | } |
| 147 | strbuf_reset(&one); |
| 148 | strbuf_reset(&two); |
Junio C Hamano | cc58d7d | 2008-08-29 10:12:23 -0700 | [diff] [blame] | 149 | } else if (hunk == RR_SIDE_1) |
Stephan Beyer | 5b2fd95 | 2008-07-09 14:58:57 +0200 | [diff] [blame] | 150 | strbuf_addstr(&one, buf); |
Junio C Hamano | 387c9d4 | 2008-08-29 10:24:45 -0700 | [diff] [blame] | 151 | else if (hunk == RR_ORIGINAL) |
| 152 | ; /* discard */ |
Junio C Hamano | cc58d7d | 2008-08-29 10:12:23 -0700 | [diff] [blame] | 153 | else if (hunk == RR_SIDE_2) |
Stephan Beyer | 5b2fd95 | 2008-07-09 14:58:57 +0200 | [diff] [blame] | 154 | strbuf_addstr(&two, buf); |
| 155 | else if (out) |
Alex Riesen | 47d32af | 2008-12-05 01:35:48 +0100 | [diff] [blame] | 156 | ferr_puts(buf, out, &wrerror); |
Stephan Beyer | 5b2fd95 | 2008-07-09 14:58:57 +0200 | [diff] [blame] | 157 | continue; |
| 158 | bad: |
| 159 | hunk = 99; /* force error exit */ |
| 160 | break; |
| 161 | } |
| 162 | strbuf_release(&one); |
| 163 | strbuf_release(&two); |
| 164 | |
| 165 | fclose(f); |
Alex Riesen | 47d32af | 2008-12-05 01:35:48 +0100 | [diff] [blame] | 166 | if (wrerror) |
| 167 | error("There were errors while writing %s (%s)", |
| 168 | path, strerror(wrerror)); |
| 169 | if (out && fclose(out)) |
| 170 | wrerror = error("Failed to flush %s: %s", |
| 171 | path, strerror(errno)); |
Stephan Beyer | 5b2fd95 | 2008-07-09 14:58:57 +0200 | [diff] [blame] | 172 | if (sha1) |
Nicolas Pitre | 9126f00 | 2008-10-01 14:05:20 -0400 | [diff] [blame] | 173 | git_SHA1_Final(sha1, &ctx); |
Junio C Hamano | cc58d7d | 2008-08-29 10:12:23 -0700 | [diff] [blame] | 174 | if (hunk != RR_CONTEXT) { |
Stephan Beyer | 5b2fd95 | 2008-07-09 14:58:57 +0200 | [diff] [blame] | 175 | if (output) |
| 176 | unlink(output); |
| 177 | return error("Could not parse conflict hunks in %s", path); |
| 178 | } |
Alex Riesen | 47d32af | 2008-12-05 01:35:48 +0100 | [diff] [blame] | 179 | if (wrerror) |
| 180 | return -1; |
Stephan Beyer | 5b2fd95 | 2008-07-09 14:58:57 +0200 | [diff] [blame] | 181 | return hunk_no; |
| 182 | } |
| 183 | |
Johannes Schindelin | c455c87 | 2008-07-21 19:03:49 +0100 | [diff] [blame] | 184 | static int find_conflict(struct string_list *conflict) |
Stephan Beyer | 5b2fd95 | 2008-07-09 14:58:57 +0200 | [diff] [blame] | 185 | { |
| 186 | int i; |
| 187 | if (read_cache() < 0) |
| 188 | return error("Could not read index"); |
| 189 | for (i = 0; i+1 < active_nr; i++) { |
| 190 | struct cache_entry *e2 = active_cache[i]; |
| 191 | struct cache_entry *e3 = active_cache[i+1]; |
| 192 | if (ce_stage(e2) == 2 && |
| 193 | ce_stage(e3) == 3 && |
| 194 | ce_same_name(e2, e3) && |
| 195 | S_ISREG(e2->ce_mode) && |
| 196 | S_ISREG(e3->ce_mode)) { |
Johannes Schindelin | c455c87 | 2008-07-21 19:03:49 +0100 | [diff] [blame] | 197 | string_list_insert((const char *)e2->name, conflict); |
Stephan Beyer | 5b2fd95 | 2008-07-09 14:58:57 +0200 | [diff] [blame] | 198 | i++; /* skip over both #2 and #3 */ |
| 199 | } |
| 200 | } |
| 201 | return 0; |
| 202 | } |
| 203 | |
| 204 | static int merge(const char *name, const char *path) |
| 205 | { |
| 206 | int ret; |
| 207 | mmfile_t cur, base, other; |
| 208 | mmbuffer_t result = {NULL, 0}; |
| 209 | xpparam_t xpp = {XDF_NEED_MINIMAL}; |
| 210 | |
| 211 | if (handle_file(path, NULL, rr_path(name, "thisimage")) < 0) |
| 212 | return 1; |
| 213 | |
| 214 | if (read_mmfile(&cur, rr_path(name, "thisimage")) || |
| 215 | read_mmfile(&base, rr_path(name, "preimage")) || |
| 216 | read_mmfile(&other, rr_path(name, "postimage"))) |
| 217 | return 1; |
| 218 | ret = xdl_merge(&base, &cur, "", &other, "", |
| 219 | &xpp, XDL_MERGE_ZEALOUS, &result); |
| 220 | if (!ret) { |
| 221 | FILE *f = fopen(path, "w"); |
| 222 | if (!f) |
Alex Riesen | 47d32af | 2008-12-05 01:35:48 +0100 | [diff] [blame] | 223 | return error("Could not open %s: %s", path, |
| 224 | strerror(errno)); |
| 225 | if (fwrite(result.ptr, result.size, 1, f) != 1) |
| 226 | error("Could not write %s: %s", path, strerror(errno)); |
| 227 | if (fclose(f)) |
| 228 | return error("Writing %s failed: %s", path, |
| 229 | strerror(errno)); |
Stephan Beyer | 5b2fd95 | 2008-07-09 14:58:57 +0200 | [diff] [blame] | 230 | } |
| 231 | |
| 232 | free(cur.ptr); |
| 233 | free(base.ptr); |
| 234 | free(other.ptr); |
| 235 | free(result.ptr); |
| 236 | |
| 237 | return ret; |
| 238 | } |
| 239 | |
| 240 | static struct lock_file index_lock; |
| 241 | |
Johannes Schindelin | c455c87 | 2008-07-21 19:03:49 +0100 | [diff] [blame] | 242 | static int update_paths(struct string_list *update) |
Stephan Beyer | 5b2fd95 | 2008-07-09 14:58:57 +0200 | [diff] [blame] | 243 | { |
| 244 | int i; |
| 245 | int fd = hold_locked_index(&index_lock, 0); |
| 246 | int status = 0; |
| 247 | |
| 248 | if (fd < 0) |
| 249 | return -1; |
| 250 | |
| 251 | for (i = 0; i < update->nr; i++) { |
Johannes Schindelin | c455c87 | 2008-07-21 19:03:49 +0100 | [diff] [blame] | 252 | struct string_list_item *item = &update->items[i]; |
| 253 | if (add_file_to_cache(item->string, ADD_CACHE_IGNORE_ERRORS)) |
Stephan Beyer | 5b2fd95 | 2008-07-09 14:58:57 +0200 | [diff] [blame] | 254 | status = -1; |
| 255 | } |
| 256 | |
| 257 | if (!status && active_cache_changed) { |
| 258 | if (write_cache(fd, active_cache, active_nr) || |
| 259 | commit_locked_index(&index_lock)) |
| 260 | die("Unable to write new index file"); |
| 261 | } else if (fd >= 0) |
| 262 | rollback_lock_file(&index_lock); |
| 263 | return status; |
| 264 | } |
| 265 | |
Johannes Schindelin | c455c87 | 2008-07-21 19:03:49 +0100 | [diff] [blame] | 266 | static int do_plain_rerere(struct string_list *rr, int fd) |
Stephan Beyer | 5b2fd95 | 2008-07-09 14:58:57 +0200 | [diff] [blame] | 267 | { |
Johannes Schindelin | c455c87 | 2008-07-21 19:03:49 +0100 | [diff] [blame] | 268 | struct string_list conflict = { NULL, 0, 0, 1 }; |
| 269 | struct string_list update = { NULL, 0, 0, 1 }; |
Stephan Beyer | 5b2fd95 | 2008-07-09 14:58:57 +0200 | [diff] [blame] | 270 | int i; |
| 271 | |
| 272 | find_conflict(&conflict); |
| 273 | |
| 274 | /* |
| 275 | * MERGE_RR records paths with conflicts immediately after merge |
| 276 | * failed. Some of the conflicted paths might have been hand resolved |
| 277 | * in the working tree since then, but the initial run would catch all |
| 278 | * and register their preimages. |
| 279 | */ |
| 280 | |
| 281 | for (i = 0; i < conflict.nr; i++) { |
Johannes Schindelin | c455c87 | 2008-07-21 19:03:49 +0100 | [diff] [blame] | 282 | const char *path = conflict.items[i].string; |
| 283 | if (!string_list_has_string(rr, path)) { |
Stephan Beyer | 5b2fd95 | 2008-07-09 14:58:57 +0200 | [diff] [blame] | 284 | unsigned char sha1[20]; |
| 285 | char *hex; |
| 286 | int ret; |
| 287 | ret = handle_file(path, sha1, NULL); |
| 288 | if (ret < 1) |
| 289 | continue; |
| 290 | hex = xstrdup(sha1_to_hex(sha1)); |
Johannes Schindelin | c455c87 | 2008-07-21 19:03:49 +0100 | [diff] [blame] | 291 | string_list_insert(path, rr)->util = hex; |
Stephan Beyer | 5b2fd95 | 2008-07-09 14:58:57 +0200 | [diff] [blame] | 292 | if (mkdir(git_path("rr-cache/%s", hex), 0755)) |
Junio C Hamano | ba19a80 | 2009-02-10 17:42:04 -0800 | [diff] [blame] | 293 | continue; |
Stephan Beyer | 5b2fd95 | 2008-07-09 14:58:57 +0200 | [diff] [blame] | 294 | handle_file(path, NULL, rr_path(hex, "preimage")); |
| 295 | fprintf(stderr, "Recorded preimage for '%s'\n", path); |
| 296 | } |
| 297 | } |
| 298 | |
| 299 | /* |
| 300 | * Now some of the paths that had conflicts earlier might have been |
| 301 | * hand resolved. Others may be similar to a conflict already that |
| 302 | * was resolved before. |
| 303 | */ |
| 304 | |
| 305 | for (i = 0; i < rr->nr; i++) { |
| 306 | int ret; |
Johannes Schindelin | c455c87 | 2008-07-21 19:03:49 +0100 | [diff] [blame] | 307 | const char *path = rr->items[i].string; |
Stephan Beyer | 5b2fd95 | 2008-07-09 14:58:57 +0200 | [diff] [blame] | 308 | const char *name = (const char *)rr->items[i].util; |
| 309 | |
| 310 | if (has_resolution(name)) { |
| 311 | if (!merge(name, path)) { |
Stephan Beyer | 5b2fd95 | 2008-07-09 14:58:57 +0200 | [diff] [blame] | 312 | if (rerere_autoupdate) |
Johannes Schindelin | c455c87 | 2008-07-21 19:03:49 +0100 | [diff] [blame] | 313 | string_list_insert(path, &update); |
Junio C Hamano | 9196e82 | 2008-07-16 20:25:18 -0700 | [diff] [blame] | 314 | fprintf(stderr, |
| 315 | "%s '%s' using previous resolution.\n", |
| 316 | rerere_autoupdate |
| 317 | ? "Staged" : "Resolved", |
| 318 | path); |
Stephan Beyer | 5b2fd95 | 2008-07-09 14:58:57 +0200 | [diff] [blame] | 319 | goto mark_resolved; |
| 320 | } |
| 321 | } |
| 322 | |
| 323 | /* Let's see if we have resolved it. */ |
| 324 | ret = handle_file(path, NULL, NULL); |
| 325 | if (ret) |
| 326 | continue; |
| 327 | |
| 328 | fprintf(stderr, "Recorded resolution for '%s'.\n", path); |
| 329 | copy_file(rr_path(name, "postimage"), path, 0666); |
| 330 | mark_resolved: |
| 331 | rr->items[i].util = NULL; |
| 332 | } |
| 333 | |
| 334 | if (update.nr) |
| 335 | update_paths(&update); |
| 336 | |
| 337 | return write_rr(rr, fd); |
| 338 | } |
| 339 | |
| 340 | static int git_rerere_config(const char *var, const char *value, void *cb) |
| 341 | { |
| 342 | if (!strcmp(var, "rerere.enabled")) |
| 343 | rerere_enabled = git_config_bool(var, value); |
| 344 | else if (!strcmp(var, "rerere.autoupdate")) |
| 345 | rerere_autoupdate = git_config_bool(var, value); |
| 346 | else |
| 347 | return git_default_config(var, value, cb); |
| 348 | return 0; |
| 349 | } |
| 350 | |
| 351 | static int is_rerere_enabled(void) |
| 352 | { |
Stephan Beyer | 5b2fd95 | 2008-07-09 14:58:57 +0200 | [diff] [blame] | 353 | const char *rr_cache; |
| 354 | int rr_cache_exists; |
| 355 | |
| 356 | if (!rerere_enabled) |
| 357 | return 0; |
| 358 | |
| 359 | rr_cache = git_path("rr-cache"); |
Junio C Hamano | 90b4a71 | 2008-09-09 01:27:07 -0700 | [diff] [blame] | 360 | rr_cache_exists = is_directory(rr_cache); |
Stephan Beyer | 5b2fd95 | 2008-07-09 14:58:57 +0200 | [diff] [blame] | 361 | if (rerere_enabled < 0) |
| 362 | return rr_cache_exists; |
| 363 | |
| 364 | if (!rr_cache_exists && |
| 365 | (mkdir(rr_cache, 0777) || adjust_shared_perm(rr_cache))) |
| 366 | die("Could not create directory %s", rr_cache); |
| 367 | return 1; |
| 368 | } |
| 369 | |
Johannes Schindelin | c455c87 | 2008-07-21 19:03:49 +0100 | [diff] [blame] | 370 | int setup_rerere(struct string_list *merge_rr) |
Stephan Beyer | 5b2fd95 | 2008-07-09 14:58:57 +0200 | [diff] [blame] | 371 | { |
| 372 | int fd; |
| 373 | |
| 374 | git_config(git_rerere_config, NULL); |
| 375 | if (!is_rerere_enabled()) |
| 376 | return -1; |
| 377 | |
Alex Riesen | a4f34cb | 2008-10-27 11:22:09 +0100 | [diff] [blame] | 378 | merge_rr_path = git_pathdup("MERGE_RR"); |
Junio C Hamano | acd3b9e | 2008-10-17 15:44:39 -0700 | [diff] [blame] | 379 | fd = hold_lock_file_for_update(&write_lock, merge_rr_path, |
| 380 | LOCK_DIE_ON_ERROR); |
Stephan Beyer | 5b2fd95 | 2008-07-09 14:58:57 +0200 | [diff] [blame] | 381 | read_rr(merge_rr); |
| 382 | return fd; |
| 383 | } |
| 384 | |
| 385 | int rerere(void) |
| 386 | { |
Johannes Schindelin | c455c87 | 2008-07-21 19:03:49 +0100 | [diff] [blame] | 387 | struct string_list merge_rr = { NULL, 0, 0, 1 }; |
Stephan Beyer | 5b2fd95 | 2008-07-09 14:58:57 +0200 | [diff] [blame] | 388 | int fd; |
| 389 | |
| 390 | fd = setup_rerere(&merge_rr); |
| 391 | if (fd < 0) |
| 392 | return 0; |
| 393 | return do_plain_rerere(&merge_rr, fd); |
| 394 | } |