2 #include "string-list.h"
4 #include "xdiff/xdiff.h"
5 #include "xdiff-interface.h"
7 /* if rerere_enabled == -1, fall back to detection of .git/rr-cache */
8 static int rerere_enabled
= -1;
10 /* automatically update cleanly resolved paths to the index */
11 static int rerere_autoupdate
;
13 static char *merge_rr_path
;
15 const char *rerere_path(const char *hex
, const char *file
)
17 return git_path("rr-cache/%s/%s", hex
, file
);
20 int has_rerere_resolution(const char *hex
)
23 return !stat(rerere_path(hex
, "postimage"), &st
);
26 static void read_rr(struct string_list
*rr
)
28 unsigned char sha1
[20];
30 FILE *in
= fopen(merge_rr_path
, "r");
33 while (fread(buf
, 40, 1, in
) == 1) {
36 if (get_sha1_hex(buf
, sha1
))
37 die("corrupt MERGE_RR");
40 if (fgetc(in
) != '\t')
41 die("corrupt MERGE_RR");
42 for (i
= 0; i
< sizeof(buf
) && (buf
[i
] = fgetc(in
)); i
++)
45 die("filename too long");
46 string_list_insert(buf
, rr
)->util
= name
;
51 static struct lock_file write_lock
;
53 static int write_rr(struct string_list
*rr
, int out_fd
)
56 for (i
= 0; i
< rr
->nr
; i
++) {
59 if (!rr
->items
[i
].util
)
61 path
= rr
->items
[i
].string
;
62 length
= strlen(path
) + 1;
63 if (write_in_full(out_fd
, rr
->items
[i
].util
, 40) != 40 ||
64 write_str_in_full(out_fd
, "\t") != 1 ||
65 write_in_full(out_fd
, path
, length
) != length
)
66 die("unable to write rerere record");
68 if (commit_lock_file(&write_lock
) != 0)
69 die("unable to write rerere record");
73 static void ferr_write(const void *p
, size_t count
, FILE *fp
, int *err
)
77 if (fwrite(p
, count
, 1, fp
) != 1)
81 static inline void ferr_puts(const char *s
, FILE *fp
, int *err
)
83 ferr_write(s
, strlen(s
), fp
, err
);
86 static int handle_file(const char *path
,
87 unsigned char *sha1
, const char *output
)
93 RR_CONTEXT
= 0, RR_SIDE_1
, RR_SIDE_2
, RR_ORIGINAL
,
95 struct strbuf one
= STRBUF_INIT
, two
= STRBUF_INIT
;
96 FILE *f
= fopen(path
, "r");
101 return error("Could not open %s", path
);
104 out
= fopen(output
, "w");
107 return error("Could not write %s", output
);
114 while (fgets(buf
, sizeof(buf
), f
)) {
115 if (!prefixcmp(buf
, "<<<<<<< ")) {
116 if (hunk
!= RR_CONTEXT
)
119 } else if (!prefixcmp(buf
, "|||||||") && isspace(buf
[7])) {
120 if (hunk
!= RR_SIDE_1
)
123 } else if (!prefixcmp(buf
, "=======") && isspace(buf
[7])) {
124 if (hunk
!= RR_SIDE_1
&& hunk
!= RR_ORIGINAL
)
127 } else if (!prefixcmp(buf
, ">>>>>>> ")) {
128 if (hunk
!= RR_SIDE_2
)
130 if (strbuf_cmp(&one
, &two
) > 0)
131 strbuf_swap(&one
, &two
);
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
);
142 git_SHA1_Update(&ctx
, one
.buf
? one
.buf
: "",
144 git_SHA1_Update(&ctx
, two
.buf
? two
.buf
: "",
149 } else if (hunk
== RR_SIDE_1
)
150 strbuf_addstr(&one
, buf
);
151 else if (hunk
== RR_ORIGINAL
)
153 else if (hunk
== RR_SIDE_2
)
154 strbuf_addstr(&two
, buf
);
156 ferr_puts(buf
, out
, &wrerror
);
159 hunk
= 99; /* force error exit */
162 strbuf_release(&one
);
163 strbuf_release(&two
);
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
));
173 git_SHA1_Final(sha1
, &ctx
);
174 if (hunk
!= RR_CONTEXT
) {
176 unlink_or_warn(output
);
177 return error("Could not parse conflict hunks in %s", path
);
184 static int find_conflict(struct string_list
*conflict
)
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 &&
194 ce_same_name(e2
, e3
) &&
195 S_ISREG(e2
->ce_mode
) &&
196 S_ISREG(e3
->ce_mode
)) {
197 string_list_insert((const char *)e2
->name
, conflict
);
198 i
++; /* skip over both #2 and #3 */
204 static int merge(const char *name
, const char *path
)
207 mmfile_t cur
, base
, other
;
208 mmbuffer_t result
= {NULL
, 0};
209 xpparam_t xpp
= {XDF_NEED_MINIMAL
};
211 if (handle_file(path
, NULL
, rerere_path(name
, "thisimage")) < 0)
214 if (read_mmfile(&cur
, rerere_path(name
, "thisimage")) ||
215 read_mmfile(&base
, rerere_path(name
, "preimage")) ||
216 read_mmfile(&other
, rerere_path(name
, "postimage")))
218 ret
= xdl_merge(&base
, &cur
, "", &other
, "",
219 &xpp
, XDL_MERGE_ZEALOUS
, &result
);
221 FILE *f
= fopen(path
, "w");
223 return error("Could not open %s: %s", path
,
225 if (fwrite(result
.ptr
, result
.size
, 1, f
) != 1)
226 error("Could not write %s: %s", path
, strerror(errno
));
228 return error("Writing %s failed: %s", path
,
240 static struct lock_file index_lock
;
242 static int update_paths(struct string_list
*update
)
245 int fd
= hold_locked_index(&index_lock
, 0);
251 for (i
= 0; i
< update
->nr
; i
++) {
252 struct string_list_item
*item
= &update
->items
[i
];
253 if (add_file_to_cache(item
->string
, ADD_CACHE_IGNORE_ERRORS
))
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");
262 rollback_lock_file(&index_lock
);
266 static int do_plain_rerere(struct string_list
*rr
, int fd
)
268 struct string_list conflict
= { NULL
, 0, 0, 1 };
269 struct string_list update
= { NULL
, 0, 0, 1 };
272 find_conflict(&conflict
);
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.
281 for (i
= 0; i
< conflict
.nr
; i
++) {
282 const char *path
= conflict
.items
[i
].string
;
283 if (!string_list_has_string(rr
, path
)) {
284 unsigned char sha1
[20];
287 ret
= handle_file(path
, sha1
, NULL
);
290 hex
= xstrdup(sha1_to_hex(sha1
));
291 string_list_insert(path
, rr
)->util
= hex
;
292 if (mkdir(git_path("rr-cache/%s", hex
), 0755))
294 handle_file(path
, NULL
, rerere_path(hex
, "preimage"));
295 fprintf(stderr
, "Recorded preimage for '%s'\n", path
);
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.
305 for (i
= 0; i
< rr
->nr
; i
++) {
307 const char *path
= rr
->items
[i
].string
;
308 const char *name
= (const char *)rr
->items
[i
].util
;
310 if (has_rerere_resolution(name
)) {
311 if (!merge(name
, path
)) {
312 if (rerere_autoupdate
)
313 string_list_insert(path
, &update
);
315 "%s '%s' using previous resolution.\n",
317 ? "Staged" : "Resolved",
323 /* Let's see if we have resolved it. */
324 ret
= handle_file(path
, NULL
, NULL
);
328 fprintf(stderr
, "Recorded resolution for '%s'.\n", path
);
329 copy_file(rerere_path(name
, "postimage"), path
, 0666);
331 rr
->items
[i
].util
= NULL
;
335 update_paths(&update
);
337 return write_rr(rr
, fd
);
340 static int git_rerere_config(const char *var
, const char *value
, void *cb
)
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
);
347 return git_default_config(var
, value
, cb
);
351 static int is_rerere_enabled(void)
353 const char *rr_cache
;
359 rr_cache
= git_path("rr-cache");
360 rr_cache_exists
= is_directory(rr_cache
);
361 if (rerere_enabled
< 0)
362 return rr_cache_exists
;
364 if (!rr_cache_exists
&&
365 (mkdir(rr_cache
, 0777) || adjust_shared_perm(rr_cache
)))
366 die("Could not create directory %s", rr_cache
);
370 int setup_rerere(struct string_list
*merge_rr
, int flags
)
374 git_config(git_rerere_config
, NULL
);
375 if (!is_rerere_enabled())
378 if (flags
& (RERERE_AUTOUPDATE
|RERERE_NOAUTOUPDATE
))
379 rerere_autoupdate
= !!(flags
& RERERE_AUTOUPDATE
);
380 merge_rr_path
= git_pathdup("MERGE_RR");
381 fd
= hold_lock_file_for_update(&write_lock
, merge_rr_path
,
387 int rerere(int flags
)
389 struct string_list merge_rr
= { NULL
, 0, 0, 1 };
392 fd
= setup_rerere(&merge_rr
, flags
);
395 return do_plain_rerere(&merge_rr
, fd
);