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" |
Stephan Beyer | 5b2fd95 | 2008-07-09 14:58:57 +0200 | [diff] [blame] | 4 | #include "xdiff-interface.h" |
Junio C Hamano | dea4562 | 2009-12-25 15:51:32 -0800 | [diff] [blame] | 5 | #include "dir.h" |
| 6 | #include "resolve-undo.h" |
| 7 | #include "ll-merge.h" |
Junio C Hamano | 8588567 | 2010-01-16 23:28:46 -0800 | [diff] [blame] | 8 | #include "attr.h" |
Stephan Beyer | 5b2fd95 | 2008-07-09 14:58:57 +0200 | [diff] [blame] | 9 | |
Martin von Zweigbergk | ac49f5c | 2011-02-16 05:47:44 -0500 | [diff] [blame] | 10 | #define RESOLVED 0 |
| 11 | #define PUNTED 1 |
| 12 | #define THREE_STAGED 2 |
| 13 | void *RERERE_RESOLVED = &RERERE_RESOLVED; |
| 14 | |
Stephan Beyer | 5b2fd95 | 2008-07-09 14:58:57 +0200 | [diff] [blame] | 15 | /* if rerere_enabled == -1, fall back to detection of .git/rr-cache */ |
| 16 | static int rerere_enabled = -1; |
| 17 | |
| 18 | /* automatically update cleanly resolved paths to the index */ |
| 19 | static int rerere_autoupdate; |
| 20 | |
| 21 | static char *merge_rr_path; |
| 22 | |
SZEDER Gábor | 9005696 | 2009-02-14 23:21:04 +0100 | [diff] [blame] | 23 | const char *rerere_path(const char *hex, const char *file) |
Stephan Beyer | 5b2fd95 | 2008-07-09 14:58:57 +0200 | [diff] [blame] | 24 | { |
SZEDER Gábor | 9005696 | 2009-02-14 23:21:04 +0100 | [diff] [blame] | 25 | return git_path("rr-cache/%s/%s", hex, file); |
Stephan Beyer | 5b2fd95 | 2008-07-09 14:58:57 +0200 | [diff] [blame] | 26 | } |
| 27 | |
Junio C Hamano | 7e0d4ab | 2012-09-15 14:06:00 -0700 | [diff] [blame] | 28 | static int has_rerere_resolution(const char *hex) |
Stephan Beyer | 5b2fd95 | 2008-07-09 14:58:57 +0200 | [diff] [blame] | 29 | { |
| 30 | struct stat st; |
SZEDER Gábor | 9005696 | 2009-02-14 23:21:04 +0100 | [diff] [blame] | 31 | return !stat(rerere_path(hex, "postimage"), &st); |
Stephan Beyer | 5b2fd95 | 2008-07-09 14:58:57 +0200 | [diff] [blame] | 32 | } |
| 33 | |
Johannes Schindelin | c455c87 | 2008-07-21 19:03:49 +0100 | [diff] [blame] | 34 | static void read_rr(struct string_list *rr) |
Stephan Beyer | 5b2fd95 | 2008-07-09 14:58:57 +0200 | [diff] [blame] | 35 | { |
| 36 | unsigned char sha1[20]; |
| 37 | char buf[PATH_MAX]; |
| 38 | FILE *in = fopen(merge_rr_path, "r"); |
| 39 | if (!in) |
| 40 | return; |
| 41 | while (fread(buf, 40, 1, in) == 1) { |
| 42 | int i; |
| 43 | char *name; |
| 44 | if (get_sha1_hex(buf, sha1)) |
| 45 | die("corrupt MERGE_RR"); |
| 46 | buf[40] = '\0'; |
| 47 | name = xstrdup(buf); |
| 48 | if (fgetc(in) != '\t') |
| 49 | die("corrupt MERGE_RR"); |
Jim Meyering | 5743350 | 2011-05-26 15:54:18 +0200 | [diff] [blame] | 50 | for (i = 0; i < sizeof(buf); i++) { |
| 51 | int c = fgetc(in); |
| 52 | if (c < 0) |
| 53 | die("corrupt MERGE_RR"); |
| 54 | buf[i] = c; |
| 55 | if (c == 0) |
| 56 | break; |
| 57 | } |
Stephan Beyer | 5b2fd95 | 2008-07-09 14:58:57 +0200 | [diff] [blame] | 58 | if (i == sizeof(buf)) |
| 59 | die("filename too long"); |
Julian Phillips | 78a395d | 2010-06-26 00:41:35 +0100 | [diff] [blame] | 60 | string_list_insert(rr, buf)->util = name; |
Stephan Beyer | 5b2fd95 | 2008-07-09 14:58:57 +0200 | [diff] [blame] | 61 | } |
| 62 | fclose(in); |
| 63 | } |
| 64 | |
| 65 | static struct lock_file write_lock; |
| 66 | |
Johannes Schindelin | c455c87 | 2008-07-21 19:03:49 +0100 | [diff] [blame] | 67 | static int write_rr(struct string_list *rr, int out_fd) |
Stephan Beyer | 5b2fd95 | 2008-07-09 14:58:57 +0200 | [diff] [blame] | 68 | { |
| 69 | int i; |
| 70 | for (i = 0; i < rr->nr; i++) { |
| 71 | const char *path; |
| 72 | int length; |
| 73 | if (!rr->items[i].util) |
| 74 | continue; |
Johannes Schindelin | c455c87 | 2008-07-21 19:03:49 +0100 | [diff] [blame] | 75 | path = rr->items[i].string; |
Stephan Beyer | 5b2fd95 | 2008-07-09 14:58:57 +0200 | [diff] [blame] | 76 | length = strlen(path) + 1; |
| 77 | if (write_in_full(out_fd, rr->items[i].util, 40) != 40 || |
Jim Meyering | 2b7ca83 | 2009-09-12 10:54:32 +0200 | [diff] [blame] | 78 | write_str_in_full(out_fd, "\t") != 1 || |
Stephan Beyer | 5b2fd95 | 2008-07-09 14:58:57 +0200 | [diff] [blame] | 79 | write_in_full(out_fd, path, length) != length) |
| 80 | die("unable to write rerere record"); |
| 81 | } |
| 82 | if (commit_lock_file(&write_lock) != 0) |
| 83 | die("unable to write rerere record"); |
| 84 | return 0; |
| 85 | } |
| 86 | |
Alex Riesen | 47d32af | 2008-12-05 01:35:48 +0100 | [diff] [blame] | 87 | static void ferr_write(const void *p, size_t count, FILE *fp, int *err) |
| 88 | { |
| 89 | if (!count || *err) |
| 90 | return; |
| 91 | if (fwrite(p, count, 1, fp) != 1) |
| 92 | *err = errno; |
| 93 | } |
| 94 | |
| 95 | static inline void ferr_puts(const char *s, FILE *fp, int *err) |
| 96 | { |
| 97 | ferr_write(s, strlen(s), fp, err); |
| 98 | } |
| 99 | |
Junio C Hamano | 27d6b08 | 2009-12-25 14:34:53 -0800 | [diff] [blame] | 100 | struct rerere_io { |
| 101 | int (*getline)(struct strbuf *, struct rerere_io *); |
| 102 | FILE *output; |
| 103 | int wrerror; |
| 104 | /* some more stuff */ |
| 105 | }; |
| 106 | |
| 107 | static void rerere_io_putstr(const char *str, struct rerere_io *io) |
| 108 | { |
| 109 | if (io->output) |
| 110 | ferr_puts(str, io->output, &io->wrerror); |
| 111 | } |
| 112 | |
Junio C Hamano | 191f2417 | 2010-01-16 23:06:45 -0800 | [diff] [blame] | 113 | static void rerere_io_putconflict(int ch, int size, struct rerere_io *io) |
| 114 | { |
| 115 | char buf[64]; |
| 116 | |
| 117 | while (size) { |
| 118 | if (size < sizeof(buf) - 2) { |
| 119 | memset(buf, ch, size); |
| 120 | buf[size] = '\n'; |
| 121 | buf[size + 1] = '\0'; |
| 122 | size = 0; |
| 123 | } else { |
| 124 | int sz = sizeof(buf) - 1; |
| 125 | if (size <= sz) |
| 126 | sz -= (sz - size) + 1; |
| 127 | memset(buf, ch, sz); |
| 128 | buf[sz] = '\0'; |
| 129 | size -= sz; |
| 130 | } |
| 131 | rerere_io_putstr(buf, io); |
| 132 | } |
| 133 | } |
| 134 | |
Junio C Hamano | 27d6b08 | 2009-12-25 14:34:53 -0800 | [diff] [blame] | 135 | static void rerere_io_putmem(const char *mem, size_t sz, struct rerere_io *io) |
| 136 | { |
| 137 | if (io->output) |
| 138 | ferr_write(mem, sz, io->output, &io->wrerror); |
| 139 | } |
| 140 | |
| 141 | struct rerere_io_file { |
| 142 | struct rerere_io io; |
| 143 | FILE *input; |
| 144 | }; |
| 145 | |
| 146 | static int rerere_file_getline(struct strbuf *sb, struct rerere_io *io_) |
| 147 | { |
| 148 | struct rerere_io_file *io = (struct rerere_io_file *)io_; |
| 149 | return strbuf_getwholeline(sb, io->input, '\n'); |
| 150 | } |
| 151 | |
Junio C Hamano | 191f2417 | 2010-01-16 23:06:45 -0800 | [diff] [blame] | 152 | static int is_cmarker(char *buf, int marker_char, int marker_size, int want_sp) |
| 153 | { |
| 154 | while (marker_size--) |
| 155 | if (*buf++ != marker_char) |
| 156 | return 0; |
| 157 | if (want_sp && *buf != ' ') |
| 158 | return 0; |
| 159 | return isspace(*buf); |
| 160 | } |
| 161 | |
| 162 | static int handle_path(unsigned char *sha1, struct rerere_io *io, int marker_size) |
Stephan Beyer | 5b2fd95 | 2008-07-09 14:58:57 +0200 | [diff] [blame] | 163 | { |
Nicolas Pitre | 9126f00 | 2008-10-01 14:05:20 -0400 | [diff] [blame] | 164 | git_SHA_CTX ctx; |
Junio C Hamano | cc58d7d | 2008-08-29 10:12:23 -0700 | [diff] [blame] | 165 | int hunk_no = 0; |
| 166 | enum { |
Gary V. Vaughan | 4b05548 | 2010-05-14 09:31:35 +0000 | [diff] [blame] | 167 | RR_CONTEXT = 0, RR_SIDE_1, RR_SIDE_2, RR_ORIGINAL |
Junio C Hamano | cc58d7d | 2008-08-29 10:12:23 -0700 | [diff] [blame] | 168 | } hunk = RR_CONTEXT; |
Brandon Casey | f285a2d | 2008-10-09 14:12:12 -0500 | [diff] [blame] | 169 | struct strbuf one = STRBUF_INIT, two = STRBUF_INIT; |
Junio C Hamano | d58ee6d | 2009-12-25 13:55:29 -0800 | [diff] [blame] | 170 | struct strbuf buf = STRBUF_INIT; |
Stephan Beyer | 5b2fd95 | 2008-07-09 14:58:57 +0200 | [diff] [blame] | 171 | |
| 172 | if (sha1) |
Nicolas Pitre | 9126f00 | 2008-10-01 14:05:20 -0400 | [diff] [blame] | 173 | git_SHA1_Init(&ctx); |
Stephan Beyer | 5b2fd95 | 2008-07-09 14:58:57 +0200 | [diff] [blame] | 174 | |
Junio C Hamano | 27d6b08 | 2009-12-25 14:34:53 -0800 | [diff] [blame] | 175 | while (!io->getline(&buf, io)) { |
Junio C Hamano | 191f2417 | 2010-01-16 23:06:45 -0800 | [diff] [blame] | 176 | if (is_cmarker(buf.buf, '<', marker_size, 1)) { |
Junio C Hamano | cc58d7d | 2008-08-29 10:12:23 -0700 | [diff] [blame] | 177 | if (hunk != RR_CONTEXT) |
Stephan Beyer | 5b2fd95 | 2008-07-09 14:58:57 +0200 | [diff] [blame] | 178 | goto bad; |
Junio C Hamano | cc58d7d | 2008-08-29 10:12:23 -0700 | [diff] [blame] | 179 | hunk = RR_SIDE_1; |
Junio C Hamano | 191f2417 | 2010-01-16 23:06:45 -0800 | [diff] [blame] | 180 | } else if (is_cmarker(buf.buf, '|', marker_size, 0)) { |
Junio C Hamano | cc58d7d | 2008-08-29 10:12:23 -0700 | [diff] [blame] | 181 | if (hunk != RR_SIDE_1) |
Stephan Beyer | 5b2fd95 | 2008-07-09 14:58:57 +0200 | [diff] [blame] | 182 | goto bad; |
Junio C Hamano | 387c9d4 | 2008-08-29 10:24:45 -0700 | [diff] [blame] | 183 | hunk = RR_ORIGINAL; |
Junio C Hamano | 191f2417 | 2010-01-16 23:06:45 -0800 | [diff] [blame] | 184 | } else if (is_cmarker(buf.buf, '=', marker_size, 0)) { |
Junio C Hamano | 387c9d4 | 2008-08-29 10:24:45 -0700 | [diff] [blame] | 185 | if (hunk != RR_SIDE_1 && hunk != RR_ORIGINAL) |
Stephan Beyer | 5b2fd95 | 2008-07-09 14:58:57 +0200 | [diff] [blame] | 186 | goto bad; |
Junio C Hamano | cc58d7d | 2008-08-29 10:12:23 -0700 | [diff] [blame] | 187 | hunk = RR_SIDE_2; |
Junio C Hamano | 191f2417 | 2010-01-16 23:06:45 -0800 | [diff] [blame] | 188 | } else if (is_cmarker(buf.buf, '>', marker_size, 1)) { |
Junio C Hamano | cc58d7d | 2008-08-29 10:12:23 -0700 | [diff] [blame] | 189 | if (hunk != RR_SIDE_2) |
Stephan Beyer | 5b2fd95 | 2008-07-09 14:58:57 +0200 | [diff] [blame] | 190 | goto bad; |
| 191 | if (strbuf_cmp(&one, &two) > 0) |
| 192 | strbuf_swap(&one, &two); |
| 193 | hunk_no++; |
Junio C Hamano | cc58d7d | 2008-08-29 10:12:23 -0700 | [diff] [blame] | 194 | hunk = RR_CONTEXT; |
Junio C Hamano | 191f2417 | 2010-01-16 23:06:45 -0800 | [diff] [blame] | 195 | rerere_io_putconflict('<', marker_size, io); |
Junio C Hamano | 27d6b08 | 2009-12-25 14:34:53 -0800 | [diff] [blame] | 196 | rerere_io_putmem(one.buf, one.len, io); |
Junio C Hamano | 191f2417 | 2010-01-16 23:06:45 -0800 | [diff] [blame] | 197 | rerere_io_putconflict('=', marker_size, io); |
Junio C Hamano | 27d6b08 | 2009-12-25 14:34:53 -0800 | [diff] [blame] | 198 | rerere_io_putmem(two.buf, two.len, io); |
Junio C Hamano | 191f2417 | 2010-01-16 23:06:45 -0800 | [diff] [blame] | 199 | rerere_io_putconflict('>', marker_size, io); |
Stephan Beyer | 5b2fd95 | 2008-07-09 14:58:57 +0200 | [diff] [blame] | 200 | if (sha1) { |
Nicolas Pitre | 9126f00 | 2008-10-01 14:05:20 -0400 | [diff] [blame] | 201 | git_SHA1_Update(&ctx, one.buf ? one.buf : "", |
Stephan Beyer | 5b2fd95 | 2008-07-09 14:58:57 +0200 | [diff] [blame] | 202 | one.len + 1); |
Nicolas Pitre | 9126f00 | 2008-10-01 14:05:20 -0400 | [diff] [blame] | 203 | git_SHA1_Update(&ctx, two.buf ? two.buf : "", |
Stephan Beyer | 5b2fd95 | 2008-07-09 14:58:57 +0200 | [diff] [blame] | 204 | two.len + 1); |
| 205 | } |
| 206 | strbuf_reset(&one); |
| 207 | strbuf_reset(&two); |
Junio C Hamano | cc58d7d | 2008-08-29 10:12:23 -0700 | [diff] [blame] | 208 | } else if (hunk == RR_SIDE_1) |
Junio C Hamano | d58ee6d | 2009-12-25 13:55:29 -0800 | [diff] [blame] | 209 | strbuf_addstr(&one, buf.buf); |
Junio C Hamano | 387c9d4 | 2008-08-29 10:24:45 -0700 | [diff] [blame] | 210 | else if (hunk == RR_ORIGINAL) |
| 211 | ; /* discard */ |
Junio C Hamano | cc58d7d | 2008-08-29 10:12:23 -0700 | [diff] [blame] | 212 | else if (hunk == RR_SIDE_2) |
Junio C Hamano | d58ee6d | 2009-12-25 13:55:29 -0800 | [diff] [blame] | 213 | strbuf_addstr(&two, buf.buf); |
Junio C Hamano | 27d6b08 | 2009-12-25 14:34:53 -0800 | [diff] [blame] | 214 | else |
| 215 | rerere_io_putstr(buf.buf, io); |
Stephan Beyer | 5b2fd95 | 2008-07-09 14:58:57 +0200 | [diff] [blame] | 216 | continue; |
| 217 | bad: |
| 218 | hunk = 99; /* force error exit */ |
| 219 | break; |
| 220 | } |
| 221 | strbuf_release(&one); |
| 222 | strbuf_release(&two); |
Junio C Hamano | d58ee6d | 2009-12-25 13:55:29 -0800 | [diff] [blame] | 223 | strbuf_release(&buf); |
Stephan Beyer | 5b2fd95 | 2008-07-09 14:58:57 +0200 | [diff] [blame] | 224 | |
Stephan Beyer | 5b2fd95 | 2008-07-09 14:58:57 +0200 | [diff] [blame] | 225 | if (sha1) |
Nicolas Pitre | 9126f00 | 2008-10-01 14:05:20 -0400 | [diff] [blame] | 226 | git_SHA1_Final(sha1, &ctx); |
Junio C Hamano | 27d6b08 | 2009-12-25 14:34:53 -0800 | [diff] [blame] | 227 | if (hunk != RR_CONTEXT) |
| 228 | return -1; |
| 229 | return hunk_no; |
| 230 | } |
| 231 | |
| 232 | static int handle_file(const char *path, unsigned char *sha1, const char *output) |
| 233 | { |
| 234 | int hunk_no = 0; |
| 235 | struct rerere_io_file io; |
Junio C Hamano | 8588567 | 2010-01-16 23:28:46 -0800 | [diff] [blame] | 236 | int marker_size = ll_merge_marker_size(path); |
Junio C Hamano | 27d6b08 | 2009-12-25 14:34:53 -0800 | [diff] [blame] | 237 | |
| 238 | memset(&io, 0, sizeof(io)); |
| 239 | io.io.getline = rerere_file_getline; |
| 240 | io.input = fopen(path, "r"); |
| 241 | io.io.wrerror = 0; |
| 242 | if (!io.input) |
| 243 | return error("Could not open %s", path); |
| 244 | |
| 245 | if (output) { |
| 246 | io.io.output = fopen(output, "w"); |
| 247 | if (!io.io.output) { |
| 248 | fclose(io.input); |
| 249 | return error("Could not write %s", output); |
| 250 | } |
| 251 | } |
| 252 | |
Junio C Hamano | 191f2417 | 2010-01-16 23:06:45 -0800 | [diff] [blame] | 253 | hunk_no = handle_path(sha1, (struct rerere_io *)&io, marker_size); |
Junio C Hamano | 27d6b08 | 2009-12-25 14:34:53 -0800 | [diff] [blame] | 254 | |
| 255 | fclose(io.input); |
| 256 | if (io.io.wrerror) |
| 257 | error("There were errors while writing %s (%s)", |
| 258 | path, strerror(io.io.wrerror)); |
| 259 | if (io.io.output && fclose(io.io.output)) |
| 260 | io.io.wrerror = error("Failed to flush %s: %s", |
| 261 | path, strerror(errno)); |
| 262 | |
| 263 | if (hunk_no < 0) { |
Stephan Beyer | 5b2fd95 | 2008-07-09 14:58:57 +0200 | [diff] [blame] | 264 | if (output) |
Alex Riesen | 691f1a2 | 2009-04-29 23:22:56 +0200 | [diff] [blame] | 265 | unlink_or_warn(output); |
Stephan Beyer | 5b2fd95 | 2008-07-09 14:58:57 +0200 | [diff] [blame] | 266 | return error("Could not parse conflict hunks in %s", path); |
| 267 | } |
Junio C Hamano | 27d6b08 | 2009-12-25 14:34:53 -0800 | [diff] [blame] | 268 | if (io.io.wrerror) |
Alex Riesen | 47d32af | 2008-12-05 01:35:48 +0100 | [diff] [blame] | 269 | return -1; |
Stephan Beyer | 5b2fd95 | 2008-07-09 14:58:57 +0200 | [diff] [blame] | 270 | return hunk_no; |
| 271 | } |
| 272 | |
Junio C Hamano | dea4562 | 2009-12-25 15:51:32 -0800 | [diff] [blame] | 273 | struct rerere_io_mem { |
| 274 | struct rerere_io io; |
| 275 | struct strbuf input; |
| 276 | }; |
| 277 | |
| 278 | static int rerere_mem_getline(struct strbuf *sb, struct rerere_io *io_) |
| 279 | { |
| 280 | struct rerere_io_mem *io = (struct rerere_io_mem *)io_; |
| 281 | char *ep; |
| 282 | size_t len; |
| 283 | |
| 284 | strbuf_release(sb); |
| 285 | if (!io->input.len) |
| 286 | return -1; |
Johannes Sixt | 53d8afa | 2013-04-01 23:36:36 +0200 | [diff] [blame] | 287 | ep = memchr(io->input.buf, '\n', io->input.len); |
| 288 | if (!ep) |
| 289 | ep = io->input.buf + io->input.len; |
| 290 | else if (*ep == '\n') |
Junio C Hamano | dea4562 | 2009-12-25 15:51:32 -0800 | [diff] [blame] | 291 | ep++; |
| 292 | len = ep - io->input.buf; |
| 293 | strbuf_add(sb, io->input.buf, len); |
| 294 | strbuf_remove(&io->input, 0, len); |
| 295 | return 0; |
| 296 | } |
| 297 | |
| 298 | static int handle_cache(const char *path, unsigned char *sha1, const char *output) |
| 299 | { |
Johannes Sixt | b9e31f5 | 2013-04-04 20:41:43 +0200 | [diff] [blame] | 300 | mmfile_t mmfile[3] = {{NULL}}; |
Junio C Hamano | dea4562 | 2009-12-25 15:51:32 -0800 | [diff] [blame] | 301 | mmbuffer_t result = {NULL, 0}; |
| 302 | struct cache_entry *ce; |
| 303 | int pos, len, i, hunk_no; |
| 304 | struct rerere_io_mem io; |
Junio C Hamano | 8588567 | 2010-01-16 23:28:46 -0800 | [diff] [blame] | 305 | int marker_size = ll_merge_marker_size(path); |
Junio C Hamano | dea4562 | 2009-12-25 15:51:32 -0800 | [diff] [blame] | 306 | |
| 307 | /* |
| 308 | * Reproduce the conflicted merge in-core |
| 309 | */ |
| 310 | len = strlen(path); |
| 311 | pos = cache_name_pos(path, len); |
| 312 | if (0 <= pos) |
| 313 | return -1; |
| 314 | pos = -pos - 1; |
| 315 | |
| 316 | for (i = 0; i < 3; i++) { |
| 317 | enum object_type type; |
| 318 | unsigned long size; |
Johannes Sixt | b9e31f5 | 2013-04-04 20:41:43 +0200 | [diff] [blame] | 319 | int j; |
Junio C Hamano | dea4562 | 2009-12-25 15:51:32 -0800 | [diff] [blame] | 320 | |
Junio C Hamano | dea4562 | 2009-12-25 15:51:32 -0800 | [diff] [blame] | 321 | if (active_nr <= pos) |
| 322 | break; |
| 323 | ce = active_cache[pos++]; |
Johannes Sixt | b9e31f5 | 2013-04-04 20:41:43 +0200 | [diff] [blame] | 324 | if (ce_namelen(ce) != len || memcmp(ce->name, path, len)) |
| 325 | continue; |
| 326 | j = ce_stage(ce) - 1; |
| 327 | mmfile[j].ptr = read_sha1_file(ce->sha1, &type, &size); |
| 328 | mmfile[j].size = size; |
Junio C Hamano | dea4562 | 2009-12-25 15:51:32 -0800 | [diff] [blame] | 329 | } |
| 330 | for (i = 0; i < 3; i++) { |
| 331 | if (!mmfile[i].ptr && !mmfile[i].size) |
| 332 | mmfile[i].ptr = xstrdup(""); |
| 333 | } |
Jonathan Nieder | 18b037a | 2010-08-05 06:24:58 -0500 | [diff] [blame] | 334 | /* |
| 335 | * NEEDSWORK: handle conflicts from merges with |
| 336 | * merge.renormalize set, too |
| 337 | */ |
Jonathan Nieder | f01de62 | 2010-03-20 19:38:58 -0500 | [diff] [blame] | 338 | ll_merge(&result, path, &mmfile[0], NULL, |
Junio C Hamano | dea4562 | 2009-12-25 15:51:32 -0800 | [diff] [blame] | 339 | &mmfile[1], "ours", |
Jonathan Nieder | 712516b | 2010-08-26 00:49:53 -0500 | [diff] [blame] | 340 | &mmfile[2], "theirs", NULL); |
Junio C Hamano | dea4562 | 2009-12-25 15:51:32 -0800 | [diff] [blame] | 341 | for (i = 0; i < 3; i++) |
| 342 | free(mmfile[i].ptr); |
| 343 | |
Jeff King | af86deb | 2010-01-28 09:52:16 -0500 | [diff] [blame] | 344 | memset(&io, 0, sizeof(io)); |
Junio C Hamano | dea4562 | 2009-12-25 15:51:32 -0800 | [diff] [blame] | 345 | io.io.getline = rerere_mem_getline; |
| 346 | if (output) |
| 347 | io.io.output = fopen(output, "w"); |
| 348 | else |
| 349 | io.io.output = NULL; |
| 350 | strbuf_init(&io.input, 0); |
| 351 | strbuf_attach(&io.input, result.ptr, result.size, result.size); |
| 352 | |
Junio C Hamano | 191f2417 | 2010-01-16 23:06:45 -0800 | [diff] [blame] | 353 | hunk_no = handle_path(sha1, (struct rerere_io *)&io, marker_size); |
Junio C Hamano | dea4562 | 2009-12-25 15:51:32 -0800 | [diff] [blame] | 354 | strbuf_release(&io.input); |
| 355 | if (io.io.output) |
| 356 | fclose(io.io.output); |
| 357 | return hunk_no; |
| 358 | } |
| 359 | |
Martin von Zweigbergk | ac49f5c | 2011-02-16 05:47:44 -0500 | [diff] [blame] | 360 | static int check_one_conflict(int i, int *type) |
| 361 | { |
| 362 | struct cache_entry *e = active_cache[i]; |
| 363 | |
| 364 | if (!ce_stage(e)) { |
| 365 | *type = RESOLVED; |
| 366 | return i + 1; |
| 367 | } |
| 368 | |
| 369 | *type = PUNTED; |
| 370 | if (ce_stage(e) == 1) { |
| 371 | if (active_nr <= ++i) |
| 372 | return i + 1; |
| 373 | } |
| 374 | |
| 375 | /* Only handle regular files with both stages #2 and #3 */ |
| 376 | if (i + 1 < active_nr) { |
| 377 | struct cache_entry *e2 = active_cache[i]; |
| 378 | struct cache_entry *e3 = active_cache[i + 1]; |
| 379 | if (ce_stage(e2) == 2 && |
| 380 | ce_stage(e3) == 3 && |
| 381 | ce_same_name(e, e3) && |
| 382 | S_ISREG(e2->ce_mode) && |
| 383 | S_ISREG(e3->ce_mode)) |
| 384 | *type = THREE_STAGED; |
| 385 | } |
| 386 | |
| 387 | /* Skip the entries with the same name */ |
| 388 | while (i < active_nr && ce_same_name(e, active_cache[i])) |
| 389 | i++; |
| 390 | return i; |
| 391 | } |
| 392 | |
Johannes Schindelin | c455c87 | 2008-07-21 19:03:49 +0100 | [diff] [blame] | 393 | static int find_conflict(struct string_list *conflict) |
Stephan Beyer | 5b2fd95 | 2008-07-09 14:58:57 +0200 | [diff] [blame] | 394 | { |
| 395 | int i; |
| 396 | if (read_cache() < 0) |
| 397 | return error("Could not read index"); |
Martin von Zweigbergk | ac49f5c | 2011-02-16 05:47:44 -0500 | [diff] [blame] | 398 | |
| 399 | for (i = 0; i < active_nr;) { |
| 400 | int conflict_type; |
| 401 | struct cache_entry *e = active_cache[i]; |
| 402 | i = check_one_conflict(i, &conflict_type); |
| 403 | if (conflict_type == THREE_STAGED) |
| 404 | string_list_insert(conflict, (const char *)e->name); |
| 405 | } |
| 406 | return 0; |
| 407 | } |
| 408 | |
| 409 | int rerere_remaining(struct string_list *merge_rr) |
| 410 | { |
| 411 | int i; |
| 412 | if (read_cache() < 0) |
| 413 | return error("Could not read index"); |
| 414 | |
| 415 | for (i = 0; i < active_nr;) { |
| 416 | int conflict_type; |
| 417 | struct cache_entry *e = active_cache[i]; |
| 418 | i = check_one_conflict(i, &conflict_type); |
| 419 | if (conflict_type == PUNTED) |
| 420 | string_list_insert(merge_rr, (const char *)e->name); |
| 421 | else if (conflict_type == RESOLVED) { |
| 422 | struct string_list_item *it; |
| 423 | it = string_list_lookup(merge_rr, (const char *)e->name); |
| 424 | if (it != NULL) { |
| 425 | free(it->util); |
| 426 | it->util = RERERE_RESOLVED; |
| 427 | } |
Stephan Beyer | 5b2fd95 | 2008-07-09 14:58:57 +0200 | [diff] [blame] | 428 | } |
| 429 | } |
| 430 | return 0; |
| 431 | } |
| 432 | |
| 433 | static int merge(const char *name, const char *path) |
| 434 | { |
| 435 | int ret; |
Bert Wesarg | 689b8c2 | 2010-02-23 21:11:53 +0100 | [diff] [blame] | 436 | mmfile_t cur = {NULL, 0}, base = {NULL, 0}, other = {NULL, 0}; |
Stephan Beyer | 5b2fd95 | 2008-07-09 14:58:57 +0200 | [diff] [blame] | 437 | mmbuffer_t result = {NULL, 0}; |
Stephan Beyer | 5b2fd95 | 2008-07-09 14:58:57 +0200 | [diff] [blame] | 438 | |
SZEDER Gábor | 9005696 | 2009-02-14 23:21:04 +0100 | [diff] [blame] | 439 | if (handle_file(path, NULL, rerere_path(name, "thisimage")) < 0) |
Stephan Beyer | 5b2fd95 | 2008-07-09 14:58:57 +0200 | [diff] [blame] | 440 | return 1; |
| 441 | |
SZEDER Gábor | 9005696 | 2009-02-14 23:21:04 +0100 | [diff] [blame] | 442 | if (read_mmfile(&cur, rerere_path(name, "thisimage")) || |
| 443 | read_mmfile(&base, rerere_path(name, "preimage")) || |
Bert Wesarg | 689b8c2 | 2010-02-23 21:11:53 +0100 | [diff] [blame] | 444 | read_mmfile(&other, rerere_path(name, "postimage"))) { |
| 445 | ret = 1; |
| 446 | goto out; |
| 447 | } |
Stephen Boyd | 1e4cd68 | 2011-04-03 00:06:54 -0700 | [diff] [blame] | 448 | ret = ll_merge(&result, path, &base, NULL, &cur, "", &other, "", NULL); |
Stephan Beyer | 5b2fd95 | 2008-07-09 14:58:57 +0200 | [diff] [blame] | 449 | if (!ret) { |
SZEDER Gábor | 7d7ff15 | 2010-07-13 01:42:04 +0200 | [diff] [blame] | 450 | FILE *f; |
| 451 | |
| 452 | if (utime(rerere_path(name, "postimage"), NULL) < 0) |
| 453 | warning("failed utime() on %s: %s", |
| 454 | rerere_path(name, "postimage"), |
| 455 | strerror(errno)); |
| 456 | f = fopen(path, "w"); |
Stephan Beyer | 5b2fd95 | 2008-07-09 14:58:57 +0200 | [diff] [blame] | 457 | if (!f) |
Alex Riesen | 47d32af | 2008-12-05 01:35:48 +0100 | [diff] [blame] | 458 | return error("Could not open %s: %s", path, |
| 459 | strerror(errno)); |
| 460 | if (fwrite(result.ptr, result.size, 1, f) != 1) |
| 461 | error("Could not write %s: %s", path, strerror(errno)); |
| 462 | if (fclose(f)) |
| 463 | return error("Writing %s failed: %s", path, |
| 464 | strerror(errno)); |
Stephan Beyer | 5b2fd95 | 2008-07-09 14:58:57 +0200 | [diff] [blame] | 465 | } |
| 466 | |
Bert Wesarg | 689b8c2 | 2010-02-23 21:11:53 +0100 | [diff] [blame] | 467 | out: |
Stephan Beyer | 5b2fd95 | 2008-07-09 14:58:57 +0200 | [diff] [blame] | 468 | free(cur.ptr); |
| 469 | free(base.ptr); |
| 470 | free(other.ptr); |
| 471 | free(result.ptr); |
| 472 | |
| 473 | return ret; |
| 474 | } |
| 475 | |
| 476 | static struct lock_file index_lock; |
| 477 | |
Johannes Schindelin | c455c87 | 2008-07-21 19:03:49 +0100 | [diff] [blame] | 478 | static int update_paths(struct string_list *update) |
Stephan Beyer | 5b2fd95 | 2008-07-09 14:58:57 +0200 | [diff] [blame] | 479 | { |
| 480 | int i; |
| 481 | int fd = hold_locked_index(&index_lock, 0); |
| 482 | int status = 0; |
| 483 | |
| 484 | if (fd < 0) |
| 485 | return -1; |
| 486 | |
| 487 | for (i = 0; i < update->nr; i++) { |
Johannes Schindelin | c455c87 | 2008-07-21 19:03:49 +0100 | [diff] [blame] | 488 | struct string_list_item *item = &update->items[i]; |
| 489 | if (add_file_to_cache(item->string, ADD_CACHE_IGNORE_ERRORS)) |
Stephan Beyer | 5b2fd95 | 2008-07-09 14:58:57 +0200 | [diff] [blame] | 490 | status = -1; |
| 491 | } |
| 492 | |
| 493 | if (!status && active_cache_changed) { |
| 494 | if (write_cache(fd, active_cache, active_nr) || |
| 495 | commit_locked_index(&index_lock)) |
| 496 | die("Unable to write new index file"); |
| 497 | } else if (fd >= 0) |
| 498 | rollback_lock_file(&index_lock); |
| 499 | return status; |
| 500 | } |
| 501 | |
Johannes Schindelin | c455c87 | 2008-07-21 19:03:49 +0100 | [diff] [blame] | 502 | static int do_plain_rerere(struct string_list *rr, int fd) |
Stephan Beyer | 5b2fd95 | 2008-07-09 14:58:57 +0200 | [diff] [blame] | 503 | { |
Thiago Farina | 183113a | 2010-07-04 16:46:19 -0300 | [diff] [blame] | 504 | struct string_list conflict = STRING_LIST_INIT_DUP; |
| 505 | struct string_list update = STRING_LIST_INIT_DUP; |
Stephan Beyer | 5b2fd95 | 2008-07-09 14:58:57 +0200 | [diff] [blame] | 506 | int i; |
| 507 | |
| 508 | find_conflict(&conflict); |
| 509 | |
| 510 | /* |
| 511 | * MERGE_RR records paths with conflicts immediately after merge |
| 512 | * failed. Some of the conflicted paths might have been hand resolved |
| 513 | * in the working tree since then, but the initial run would catch all |
| 514 | * and register their preimages. |
| 515 | */ |
| 516 | |
| 517 | for (i = 0; i < conflict.nr; i++) { |
Johannes Schindelin | c455c87 | 2008-07-21 19:03:49 +0100 | [diff] [blame] | 518 | const char *path = conflict.items[i].string; |
| 519 | if (!string_list_has_string(rr, path)) { |
Stephan Beyer | 5b2fd95 | 2008-07-09 14:58:57 +0200 | [diff] [blame] | 520 | unsigned char sha1[20]; |
| 521 | char *hex; |
| 522 | int ret; |
| 523 | ret = handle_file(path, sha1, NULL); |
| 524 | if (ret < 1) |
| 525 | continue; |
| 526 | hex = xstrdup(sha1_to_hex(sha1)); |
Julian Phillips | 78a395d | 2010-06-26 00:41:35 +0100 | [diff] [blame] | 527 | string_list_insert(rr, path)->util = hex; |
Junio C Hamano | fd95633 | 2012-07-09 16:27:49 -0700 | [diff] [blame] | 528 | if (mkdir_in_gitdir(git_path("rr-cache/%s", hex))) |
Junio C Hamano | ba19a80 | 2009-02-10 17:42:04 -0800 | [diff] [blame] | 529 | continue; |
SZEDER Gábor | 9005696 | 2009-02-14 23:21:04 +0100 | [diff] [blame] | 530 | handle_file(path, NULL, rerere_path(hex, "preimage")); |
Stephan Beyer | 5b2fd95 | 2008-07-09 14:58:57 +0200 | [diff] [blame] | 531 | fprintf(stderr, "Recorded preimage for '%s'\n", path); |
| 532 | } |
| 533 | } |
| 534 | |
| 535 | /* |
| 536 | * Now some of the paths that had conflicts earlier might have been |
| 537 | * hand resolved. Others may be similar to a conflict already that |
| 538 | * was resolved before. |
| 539 | */ |
| 540 | |
| 541 | for (i = 0; i < rr->nr; i++) { |
| 542 | int ret; |
Johannes Schindelin | c455c87 | 2008-07-21 19:03:49 +0100 | [diff] [blame] | 543 | const char *path = rr->items[i].string; |
Stephan Beyer | 5b2fd95 | 2008-07-09 14:58:57 +0200 | [diff] [blame] | 544 | const char *name = (const char *)rr->items[i].util; |
| 545 | |
SZEDER Gábor | 9005696 | 2009-02-14 23:21:04 +0100 | [diff] [blame] | 546 | if (has_rerere_resolution(name)) { |
Stephan Beyer | 5b2fd95 | 2008-07-09 14:58:57 +0200 | [diff] [blame] | 547 | if (!merge(name, path)) { |
Nguyễn Thái Ngọc Duy | 72a23e6 | 2012-06-07 19:05:14 +0700 | [diff] [blame] | 548 | const char *msg; |
| 549 | if (rerere_autoupdate) { |
Julian Phillips | 78a395d | 2010-06-26 00:41:35 +0100 | [diff] [blame] | 550 | string_list_insert(&update, path); |
Nguyễn Thái Ngọc Duy | 72a23e6 | 2012-06-07 19:05:14 +0700 | [diff] [blame] | 551 | msg = "Staged '%s' using previous resolution.\n"; |
| 552 | } else |
| 553 | msg = "Resolved '%s' using previous resolution.\n"; |
| 554 | fprintf(stderr, msg, path); |
Stephan Beyer | 5b2fd95 | 2008-07-09 14:58:57 +0200 | [diff] [blame] | 555 | goto mark_resolved; |
| 556 | } |
| 557 | } |
| 558 | |
| 559 | /* Let's see if we have resolved it. */ |
| 560 | ret = handle_file(path, NULL, NULL); |
| 561 | if (ret) |
| 562 | continue; |
| 563 | |
| 564 | fprintf(stderr, "Recorded resolution for '%s'.\n", path); |
SZEDER Gábor | 9005696 | 2009-02-14 23:21:04 +0100 | [diff] [blame] | 565 | copy_file(rerere_path(name, "postimage"), path, 0666); |
Stephan Beyer | 5b2fd95 | 2008-07-09 14:58:57 +0200 | [diff] [blame] | 566 | mark_resolved: |
| 567 | rr->items[i].util = NULL; |
| 568 | } |
| 569 | |
| 570 | if (update.nr) |
| 571 | update_paths(&update); |
| 572 | |
| 573 | return write_rr(rr, fd); |
| 574 | } |
| 575 | |
| 576 | static int git_rerere_config(const char *var, const char *value, void *cb) |
| 577 | { |
| 578 | if (!strcmp(var, "rerere.enabled")) |
| 579 | rerere_enabled = git_config_bool(var, value); |
| 580 | else if (!strcmp(var, "rerere.autoupdate")) |
| 581 | rerere_autoupdate = git_config_bool(var, value); |
| 582 | else |
| 583 | return git_default_config(var, value, cb); |
| 584 | return 0; |
| 585 | } |
| 586 | |
| 587 | static int is_rerere_enabled(void) |
| 588 | { |
Stephan Beyer | 5b2fd95 | 2008-07-09 14:58:57 +0200 | [diff] [blame] | 589 | const char *rr_cache; |
| 590 | int rr_cache_exists; |
| 591 | |
| 592 | if (!rerere_enabled) |
| 593 | return 0; |
| 594 | |
| 595 | rr_cache = git_path("rr-cache"); |
Junio C Hamano | 90b4a71 | 2008-09-09 01:27:07 -0700 | [diff] [blame] | 596 | rr_cache_exists = is_directory(rr_cache); |
Stephan Beyer | 5b2fd95 | 2008-07-09 14:58:57 +0200 | [diff] [blame] | 597 | if (rerere_enabled < 0) |
| 598 | return rr_cache_exists; |
| 599 | |
Junio C Hamano | 90a6464 | 2011-03-10 16:02:50 -0800 | [diff] [blame] | 600 | if (!rr_cache_exists && mkdir_in_gitdir(rr_cache)) |
Stephan Beyer | 5b2fd95 | 2008-07-09 14:58:57 +0200 | [diff] [blame] | 601 | die("Could not create directory %s", rr_cache); |
| 602 | return 1; |
| 603 | } |
| 604 | |
Junio C Hamano | cb6020b | 2009-12-04 00:20:48 -0800 | [diff] [blame] | 605 | int setup_rerere(struct string_list *merge_rr, int flags) |
Stephan Beyer | 5b2fd95 | 2008-07-09 14:58:57 +0200 | [diff] [blame] | 606 | { |
| 607 | int fd; |
| 608 | |
| 609 | git_config(git_rerere_config, NULL); |
| 610 | if (!is_rerere_enabled()) |
| 611 | return -1; |
| 612 | |
Junio C Hamano | cb6020b | 2009-12-04 00:20:48 -0800 | [diff] [blame] | 613 | if (flags & (RERERE_AUTOUPDATE|RERERE_NOAUTOUPDATE)) |
| 614 | rerere_autoupdate = !!(flags & RERERE_AUTOUPDATE); |
Alex Riesen | a4f34cb | 2008-10-27 11:22:09 +0100 | [diff] [blame] | 615 | merge_rr_path = git_pathdup("MERGE_RR"); |
Junio C Hamano | acd3b9e | 2008-10-17 15:44:39 -0700 | [diff] [blame] | 616 | fd = hold_lock_file_for_update(&write_lock, merge_rr_path, |
| 617 | LOCK_DIE_ON_ERROR); |
Stephan Beyer | 5b2fd95 | 2008-07-09 14:58:57 +0200 | [diff] [blame] | 618 | read_rr(merge_rr); |
| 619 | return fd; |
| 620 | } |
| 621 | |
Junio C Hamano | cb6020b | 2009-12-04 00:20:48 -0800 | [diff] [blame] | 622 | int rerere(int flags) |
Stephan Beyer | 5b2fd95 | 2008-07-09 14:58:57 +0200 | [diff] [blame] | 623 | { |
Thiago Farina | 183113a | 2010-07-04 16:46:19 -0300 | [diff] [blame] | 624 | struct string_list merge_rr = STRING_LIST_INIT_DUP; |
Stephan Beyer | 5b2fd95 | 2008-07-09 14:58:57 +0200 | [diff] [blame] | 625 | int fd; |
| 626 | |
Junio C Hamano | cb6020b | 2009-12-04 00:20:48 -0800 | [diff] [blame] | 627 | fd = setup_rerere(&merge_rr, flags); |
Stephan Beyer | 5b2fd95 | 2008-07-09 14:58:57 +0200 | [diff] [blame] | 628 | if (fd < 0) |
| 629 | return 0; |
| 630 | return do_plain_rerere(&merge_rr, fd); |
| 631 | } |
Junio C Hamano | dea4562 | 2009-12-25 15:51:32 -0800 | [diff] [blame] | 632 | |
| 633 | static int rerere_forget_one_path(const char *path, struct string_list *rr) |
| 634 | { |
| 635 | const char *filename; |
| 636 | char *hex; |
| 637 | unsigned char sha1[20]; |
| 638 | int ret; |
| 639 | |
| 640 | ret = handle_cache(path, sha1, NULL); |
| 641 | if (ret < 1) |
| 642 | return error("Could not parse conflict hunks in '%s'", path); |
| 643 | hex = xstrdup(sha1_to_hex(sha1)); |
| 644 | filename = rerere_path(hex, "postimage"); |
| 645 | if (unlink(filename)) |
| 646 | return (errno == ENOENT |
| 647 | ? error("no remembered resolution for %s", path) |
| 648 | : error("cannot unlink %s: %s", filename, strerror(errno))); |
| 649 | |
| 650 | handle_cache(path, sha1, rerere_path(hex, "preimage")); |
| 651 | fprintf(stderr, "Updated preimage for '%s'\n", path); |
| 652 | |
| 653 | |
Julian Phillips | 78a395d | 2010-06-26 00:41:35 +0100 | [diff] [blame] | 654 | string_list_insert(rr, path)->util = hex; |
Junio C Hamano | dea4562 | 2009-12-25 15:51:32 -0800 | [diff] [blame] | 655 | fprintf(stderr, "Forgot resolution for %s\n", path); |
| 656 | return 0; |
| 657 | } |
| 658 | |
| 659 | int rerere_forget(const char **pathspec) |
| 660 | { |
| 661 | int i, fd; |
Thiago Farina | 183113a | 2010-07-04 16:46:19 -0300 | [diff] [blame] | 662 | struct string_list conflict = STRING_LIST_INIT_DUP; |
| 663 | struct string_list merge_rr = STRING_LIST_INIT_DUP; |
Junio C Hamano | dea4562 | 2009-12-25 15:51:32 -0800 | [diff] [blame] | 664 | |
| 665 | if (read_cache() < 0) |
| 666 | return error("Could not read index"); |
| 667 | |
Junio C Hamano | 6751e04 | 2010-01-20 14:44:31 -0800 | [diff] [blame] | 668 | fd = setup_rerere(&merge_rr, RERERE_NOAUTOUPDATE); |
Junio C Hamano | dea4562 | 2009-12-25 15:51:32 -0800 | [diff] [blame] | 669 | |
| 670 | unmerge_cache(pathspec); |
| 671 | find_conflict(&conflict); |
| 672 | for (i = 0; i < conflict.nr; i++) { |
| 673 | struct string_list_item *it = &conflict.items[i]; |
| 674 | if (!match_pathspec(pathspec, it->string, strlen(it->string), |
| 675 | 0, NULL)) |
| 676 | continue; |
| 677 | rerere_forget_one_path(it->string, &merge_rr); |
| 678 | } |
| 679 | return write_rr(&merge_rr, fd); |
| 680 | } |
Junio C Hamano | 0f891e7 | 2011-05-08 12:55:34 -0700 | [diff] [blame] | 681 | |
| 682 | static time_t rerere_created_at(const char *name) |
| 683 | { |
| 684 | struct stat st; |
| 685 | return stat(rerere_path(name, "preimage"), &st) ? (time_t) 0 : st.st_mtime; |
| 686 | } |
| 687 | |
| 688 | static time_t rerere_last_used_at(const char *name) |
| 689 | { |
| 690 | struct stat st; |
| 691 | return stat(rerere_path(name, "postimage"), &st) ? (time_t) 0 : st.st_mtime; |
| 692 | } |
| 693 | |
| 694 | static void unlink_rr_item(const char *name) |
| 695 | { |
| 696 | unlink(rerere_path(name, "thisimage")); |
| 697 | unlink(rerere_path(name, "preimage")); |
| 698 | unlink(rerere_path(name, "postimage")); |
| 699 | rmdir(git_path("rr-cache/%s", name)); |
| 700 | } |
| 701 | |
| 702 | struct rerere_gc_config_cb { |
| 703 | int cutoff_noresolve; |
| 704 | int cutoff_resolve; |
| 705 | }; |
| 706 | |
| 707 | static int git_rerere_gc_config(const char *var, const char *value, void *cb) |
| 708 | { |
| 709 | struct rerere_gc_config_cb *cf = cb; |
| 710 | |
| 711 | if (!strcmp(var, "gc.rerereresolved")) |
| 712 | cf->cutoff_resolve = git_config_int(var, value); |
| 713 | else if (!strcmp(var, "gc.rerereunresolved")) |
| 714 | cf->cutoff_noresolve = git_config_int(var, value); |
| 715 | else |
| 716 | return git_default_config(var, value, cb); |
| 717 | return 0; |
| 718 | } |
| 719 | |
| 720 | void rerere_gc(struct string_list *rr) |
| 721 | { |
| 722 | struct string_list to_remove = STRING_LIST_INIT_DUP; |
| 723 | DIR *dir; |
| 724 | struct dirent *e; |
| 725 | int i, cutoff; |
| 726 | time_t now = time(NULL), then; |
| 727 | struct rerere_gc_config_cb cf = { 15, 60 }; |
| 728 | |
| 729 | git_config(git_rerere_gc_config, &cf); |
| 730 | dir = opendir(git_path("rr-cache")); |
| 731 | if (!dir) |
| 732 | die_errno("unable to open rr-cache directory"); |
| 733 | while ((e = readdir(dir))) { |
| 734 | if (is_dot_or_dotdot(e->d_name)) |
| 735 | continue; |
| 736 | |
| 737 | then = rerere_last_used_at(e->d_name); |
| 738 | if (then) { |
| 739 | cutoff = cf.cutoff_resolve; |
| 740 | } else { |
| 741 | then = rerere_created_at(e->d_name); |
| 742 | if (!then) |
| 743 | continue; |
| 744 | cutoff = cf.cutoff_noresolve; |
| 745 | } |
| 746 | if (then < now - cutoff * 86400) |
| 747 | string_list_append(&to_remove, e->d_name); |
| 748 | } |
Jim Meyering | a9930e3 | 2011-05-26 15:55:50 +0200 | [diff] [blame] | 749 | closedir(dir); |
Junio C Hamano | 0f891e7 | 2011-05-08 12:55:34 -0700 | [diff] [blame] | 750 | for (i = 0; i < to_remove.nr; i++) |
| 751 | unlink_rr_item(to_remove.items[i].string); |
| 752 | string_list_clear(&to_remove, 0); |
| 753 | } |
| 754 | |
| 755 | void rerere_clear(struct string_list *merge_rr) |
| 756 | { |
| 757 | int i; |
| 758 | |
| 759 | for (i = 0; i < merge_rr->nr; i++) { |
| 760 | const char *name = (const char *)merge_rr->items[i].util; |
| 761 | if (!has_rerere_resolution(name)) |
| 762 | unlink_rr_item(name); |
| 763 | } |
| 764 | unlink_or_warn(git_path("MERGE_RR")); |
| 765 | } |