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