4 #include "xdiff/xdiff.h"
5 #include "xdiff-interface.h"
9 static const char git_rerere_usage
[] =
10 "git-rerere [clear | status | diff | gc]";
12 /* these values are days */
13 static int cutoff_noresolve
= 15;
14 static int cutoff_resolve
= 60;
16 /* if rerere_enabled == -1, fall back to detection of .git/rr-cache */
17 static int rerere_enabled
= -1;
19 /* automatically update cleanly resolved paths to the index */
20 static int rerere_autoupdate
;
22 static char *merge_rr_path
;
24 static const char *rr_path(const char *name
, const char *file
)
26 return git_path("rr-cache/%s/%s", name
, file
);
29 static time_t rerere_created_at(const char *name
)
32 return stat(rr_path(name
, "preimage"), &st
) ? (time_t) 0 : st
.st_mtime
;
35 static int has_resolution(const char *name
)
38 return !stat(rr_path(name
, "postimage"), &st
);
41 static void read_rr(struct path_list
*rr
)
43 unsigned char sha1
[20];
45 FILE *in
= fopen(merge_rr_path
, "r");
48 while (fread(buf
, 40, 1, in
) == 1) {
51 if (get_sha1_hex(buf
, sha1
))
52 die("corrupt MERGE_RR");
55 if (fgetc(in
) != '\t')
56 die("corrupt MERGE_RR");
57 for (i
= 0; i
< sizeof(buf
) && (buf
[i
] = fgetc(in
)); i
++)
60 die("filename too long");
61 path_list_insert(buf
, rr
)->util
= name
;
66 static struct lock_file write_lock
;
68 static int write_rr(struct path_list
*rr
, int out_fd
)
71 for (i
= 0; i
< rr
->nr
; i
++) {
74 if (!rr
->items
[i
].util
)
76 path
= rr
->items
[i
].path
;
77 length
= strlen(path
) + 1;
78 if (write_in_full(out_fd
, rr
->items
[i
].util
, 40) != 40 ||
79 write_in_full(out_fd
, "\t", 1) != 1 ||
80 write_in_full(out_fd
, path
, length
) != length
)
81 die("unable to write rerere record");
83 if (commit_lock_file(&write_lock
) != 0)
84 die("unable to write rerere record");
88 static int handle_file(const char *path
,
89 unsigned char *sha1
, const char *output
)
93 int hunk
= 0, hunk_no
= 0;
94 struct strbuf one
, two
;
95 FILE *f
= fopen(path
, "r");
99 return error("Could not open %s", path
);
102 out
= fopen(output
, "w");
105 return error("Could not write %s", output
);
112 strbuf_init(&one
, 0);
113 strbuf_init(&two
, 0);
114 while (fgets(buf
, sizeof(buf
), f
)) {
115 if (!prefixcmp(buf
, "<<<<<<< ")) {
119 } else if (!prefixcmp(buf
, "=======") && isspace(buf
[7])) {
123 } else if (!prefixcmp(buf
, ">>>>>>> ")) {
126 if (strbuf_cmp(&one
, &two
) > 0)
127 strbuf_swap(&one
, &two
);
131 fputs("<<<<<<<\n", out
);
132 fwrite(one
.buf
, one
.len
, 1, out
);
133 fputs("=======\n", out
);
134 fwrite(two
.buf
, two
.len
, 1, out
);
135 fputs(">>>>>>>\n", out
);
138 SHA1_Update(&ctx
, one
.buf
? one
.buf
: "",
140 SHA1_Update(&ctx
, two
.buf
? two
.buf
: "",
145 } else if (hunk
== 1)
146 strbuf_addstr(&one
, buf
);
148 strbuf_addstr(&two
, buf
);
153 hunk
= 99; /* force error exit */
156 strbuf_release(&one
);
157 strbuf_release(&two
);
163 SHA1_Final(sha1
, &ctx
);
167 return error("Could not parse conflict hunks in %s", path
);
172 static int find_conflict(struct path_list
*conflict
)
175 if (read_cache() < 0)
176 return error("Could not read index");
177 for (i
= 0; i
+1 < active_nr
; i
++) {
178 struct cache_entry
*e2
= active_cache
[i
];
179 struct cache_entry
*e3
= active_cache
[i
+1];
180 if (ce_stage(e2
) == 2 &&
182 ce_same_name(e2
, e3
) &&
183 S_ISREG(e2
->ce_mode
) &&
184 S_ISREG(e3
->ce_mode
)) {
185 path_list_insert((const char *)e2
->name
, conflict
);
186 i
++; /* skip over both #2 and #3 */
192 static int merge(const char *name
, const char *path
)
195 mmfile_t cur
, base
, other
;
196 mmbuffer_t result
= {NULL
, 0};
197 xpparam_t xpp
= {XDF_NEED_MINIMAL
};
199 if (handle_file(path
, NULL
, rr_path(name
, "thisimage")) < 0)
202 if (read_mmfile(&cur
, rr_path(name
, "thisimage")) ||
203 read_mmfile(&base
, rr_path(name
, "preimage")) ||
204 read_mmfile(&other
, rr_path(name
, "postimage")))
206 ret
= xdl_merge(&base
, &cur
, "", &other
, "",
207 &xpp
, XDL_MERGE_ZEALOUS
, &result
);
209 FILE *f
= fopen(path
, "w");
211 return error("Could not write to %s", path
);
212 fwrite(result
.ptr
, result
.size
, 1, f
);
224 static void unlink_rr_item(const char *name
)
226 unlink(rr_path(name
, "thisimage"));
227 unlink(rr_path(name
, "preimage"));
228 unlink(rr_path(name
, "postimage"));
229 rmdir(git_path("rr-cache/%s", name
));
232 static void garbage_collect(struct path_list
*rr
)
234 struct path_list to_remove
= { NULL
, 0, 0, 1 };
238 time_t now
= time(NULL
), then
;
240 dir
= opendir(git_path("rr-cache"));
241 while ((e
= readdir(dir
))) {
242 const char *name
= e
->d_name
;
243 if (name
[0] == '.' &&
244 (name
[1] == '\0' || (name
[1] == '.' && name
[2] == '\0')))
246 then
= rerere_created_at(name
);
249 cutoff
= (has_resolution(name
)
250 ? cutoff_resolve
: cutoff_noresolve
);
251 if (then
< now
- cutoff
* 86400)
252 path_list_append(name
, &to_remove
);
254 for (i
= 0; i
< to_remove
.nr
; i
++)
255 unlink_rr_item(to_remove
.items
[i
].path
);
256 path_list_clear(&to_remove
, 0);
259 static int outf(void *dummy
, mmbuffer_t
*ptr
, int nbuf
)
262 for (i
= 0; i
< nbuf
; i
++)
263 if (write_in_full(1, ptr
[i
].ptr
, ptr
[i
].size
) != ptr
[i
].size
)
268 static int diff_two(const char *file1
, const char *label1
,
269 const char *file2
, const char *label2
)
274 mmfile_t minus
, plus
;
276 if (read_mmfile(&minus
, file1
) || read_mmfile(&plus
, file2
))
279 printf("--- a/%s\n+++ b/%s\n", label1
, label2
);
281 xpp
.flags
= XDF_NEED_MINIMAL
;
282 memset(&xecfg
, 0, sizeof(xecfg
));
285 xdi_diff(&minus
, &plus
, &xpp
, &xecfg
, &ecb
);
292 static struct lock_file index_lock
;
294 static int update_paths(struct path_list
*update
)
297 int fd
= hold_locked_index(&index_lock
, 0);
303 for (i
= 0; i
< update
->nr
; i
++) {
304 struct path_list_item
*item
= &update
->items
[i
];
305 if (add_file_to_cache(item
->path
, ADD_CACHE_IGNORE_ERRORS
))
309 if (!status
&& active_cache_changed
) {
310 if (write_cache(fd
, active_cache
, active_nr
) ||
311 commit_locked_index(&index_lock
))
312 die("Unable to write new index file");
314 rollback_lock_file(&index_lock
);
318 static int do_plain_rerere(struct path_list
*rr
, int fd
)
320 struct path_list conflict
= { NULL
, 0, 0, 1 };
321 struct path_list update
= { NULL
, 0, 0, 1 };
324 find_conflict(&conflict
);
327 * MERGE_RR records paths with conflicts immediately after merge
328 * failed. Some of the conflicted paths might have been hand resolved
329 * in the working tree since then, but the initial run would catch all
330 * and register their preimages.
333 for (i
= 0; i
< conflict
.nr
; i
++) {
334 const char *path
= conflict
.items
[i
].path
;
335 if (!path_list_has_path(rr
, path
)) {
336 unsigned char sha1
[20];
339 ret
= handle_file(path
, sha1
, NULL
);
342 hex
= xstrdup(sha1_to_hex(sha1
));
343 path_list_insert(path
, rr
)->util
= hex
;
344 if (mkdir(git_path("rr-cache/%s", hex
), 0755))
346 handle_file(path
, NULL
, rr_path(hex
, "preimage"));
347 fprintf(stderr
, "Recorded preimage for '%s'\n", path
);
352 * Now some of the paths that had conflicts earlier might have been
353 * hand resolved. Others may be similar to a conflict already that
354 * was resolved before.
357 for (i
= 0; i
< rr
->nr
; i
++) {
359 const char *path
= rr
->items
[i
].path
;
360 const char *name
= (const char *)rr
->items
[i
].util
;
362 if (has_resolution(name
)) {
363 if (!merge(name
, path
)) {
364 fprintf(stderr
, "Resolved '%s' using "
365 "previous resolution.\n", path
);
366 if (rerere_autoupdate
)
367 path_list_insert(path
, &update
);
372 /* Let's see if we have resolved it. */
373 ret
= handle_file(path
, NULL
, NULL
);
377 fprintf(stderr
, "Recorded resolution for '%s'.\n", path
);
378 copy_file(rr_path(name
, "postimage"), path
, 0666);
380 rr
->items
[i
].util
= NULL
;
384 update_paths(&update
);
386 return write_rr(rr
, fd
);
389 static int git_rerere_config(const char *var
, const char *value
, void *cb
)
391 if (!strcmp(var
, "gc.rerereresolved"))
392 cutoff_resolve
= git_config_int(var
, value
);
393 else if (!strcmp(var
, "gc.rerereunresolved"))
394 cutoff_noresolve
= git_config_int(var
, value
);
395 else if (!strcmp(var
, "rerere.enabled"))
396 rerere_enabled
= git_config_bool(var
, value
);
397 else if (!strcmp(var
, "rerere.autoupdate"))
398 rerere_autoupdate
= git_config_bool(var
, value
);
400 return git_default_config(var
, value
, cb
);
404 static int is_rerere_enabled(void)
407 const char *rr_cache
;
413 rr_cache
= git_path("rr-cache");
414 rr_cache_exists
= !stat(rr_cache
, &st
) && S_ISDIR(st
.st_mode
);
415 if (rerere_enabled
< 0)
416 return rr_cache_exists
;
418 if (!rr_cache_exists
&&
419 (mkdir(rr_cache
, 0777) || adjust_shared_perm(rr_cache
)))
420 die("Could not create directory %s", rr_cache
);
424 static int setup_rerere(struct path_list
*merge_rr
)
428 git_config(git_rerere_config
, NULL
);
429 if (!is_rerere_enabled())
432 merge_rr_path
= xstrdup(git_path("rr-cache/MERGE_RR"));
433 fd
= hold_lock_file_for_update(&write_lock
, merge_rr_path
, 1);
440 struct path_list merge_rr
= { NULL
, 0, 0, 1 };
443 fd
= setup_rerere(&merge_rr
);
446 return do_plain_rerere(&merge_rr
, fd
);
449 int cmd_rerere(int argc
, const char **argv
, const char *prefix
)
451 struct path_list merge_rr
= { NULL
, 0, 0, 1 };
454 fd
= setup_rerere(&merge_rr
);
459 return do_plain_rerere(&merge_rr
, fd
);
460 else if (!strcmp(argv
[1], "clear")) {
461 for (i
= 0; i
< merge_rr
.nr
; i
++) {
462 const char *name
= (const char *)merge_rr
.items
[i
].util
;
463 if (!has_resolution(name
))
464 unlink_rr_item(name
);
466 unlink(merge_rr_path
);
467 } else if (!strcmp(argv
[1], "gc"))
468 garbage_collect(&merge_rr
);
469 else if (!strcmp(argv
[1], "status"))
470 for (i
= 0; i
< merge_rr
.nr
; i
++)
471 printf("%s\n", merge_rr
.items
[i
].path
);
472 else if (!strcmp(argv
[1], "diff"))
473 for (i
= 0; i
< merge_rr
.nr
; i
++) {
474 const char *path
= merge_rr
.items
[i
].path
;
475 const char *name
= (const char *)merge_rr
.items
[i
].util
;
476 diff_two(rr_path(name
, "preimage"), path
, path
, path
);
479 usage(git_rerere_usage
);
481 path_list_clear(&merge_rr
, 1);