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 static char *merge_rr_path
;
21 static const char *rr_path(const char *name
, const char *file
)
23 return git_path("rr-cache/%s/%s", name
, file
);
26 static void read_rr(struct path_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 path_list_insert(buf
, rr
)->util
= xstrdup(name
);
51 static struct lock_file write_lock
;
53 static int write_rr(struct path_list
*rr
, int out_fd
)
56 for (i
= 0; i
< rr
->nr
; i
++) {
57 const char *path
= rr
->items
[i
].path
;
58 int length
= strlen(path
) + 1;
59 if (write_in_full(out_fd
, rr
->items
[i
].util
, 40) != 40 ||
60 write_in_full(out_fd
, "\t", 1) != 1 ||
61 write_in_full(out_fd
, path
, length
) != length
)
62 die("unable to write rerere record");
64 if (commit_lock_file(&write_lock
) != 0)
65 die("unable to write rerere record");
69 static int handle_file(const char *path
,
70 unsigned char *sha1
, const char *output
)
74 int hunk
= 0, hunk_no
= 0;
75 struct strbuf one
, two
;
76 FILE *f
= fopen(path
, "r");
80 return error("Could not open %s", path
);
83 out
= fopen(output
, "w");
86 return error("Could not write %s", output
);
95 while (fgets(buf
, sizeof(buf
), f
)) {
96 if (!prefixcmp(buf
, "<<<<<<< "))
98 else if (!prefixcmp(buf
, "======="))
100 else if (!prefixcmp(buf
, ">>>>>>> ")) {
101 int cmp
= strbuf_cmp(&one
, &two
);
106 strbuf_swap(&one
, &two
);
109 fputs("<<<<<<<\n", out
);
110 fwrite(one
.buf
, one
.len
, 1, out
);
111 fputs("=======\n", out
);
112 fwrite(two
.buf
, two
.len
, 1, out
);
113 fputs(">>>>>>>\n", out
);
116 SHA1_Update(&ctx
, one
.buf
? one
.buf
: "",
118 SHA1_Update(&ctx
, two
.buf
? two
.buf
: "",
123 } else if (hunk
== 1)
124 strbuf_addstr(&one
, buf
);
126 strbuf_addstr(&two
, buf
);
130 strbuf_release(&one
);
131 strbuf_release(&two
);
137 SHA1_Final(sha1
, &ctx
);
141 static int find_conflict(struct path_list
*conflict
)
144 if (read_cache() < 0)
145 return error("Could not read index");
146 for (i
= 0; i
+1 < active_nr
; i
++) {
147 struct cache_entry
*e2
= active_cache
[i
];
148 struct cache_entry
*e3
= active_cache
[i
+1];
149 if (ce_stage(e2
) == 2 &&
151 ce_same_name(e2
, e3
) &&
152 S_ISREG(ntohl(e2
->ce_mode
)) &&
153 S_ISREG(ntohl(e3
->ce_mode
))) {
154 path_list_insert((const char *)e2
->name
, conflict
);
155 i
++; /* skip over both #2 and #3 */
161 static int merge(const char *name
, const char *path
)
164 mmfile_t cur
, base
, other
;
165 mmbuffer_t result
= {NULL
, 0};
166 xpparam_t xpp
= {XDF_NEED_MINIMAL
};
168 if (handle_file(path
, NULL
, rr_path(name
, "thisimage")) < 0)
171 if (read_mmfile(&cur
, rr_path(name
, "thisimage")) ||
172 read_mmfile(&base
, rr_path(name
, "preimage")) ||
173 read_mmfile(&other
, rr_path(name
, "postimage")))
175 ret
= xdl_merge(&base
, &cur
, "", &other
, "",
176 &xpp
, XDL_MERGE_ZEALOUS
, &result
);
178 FILE *f
= fopen(path
, "w");
180 return error("Could not write to %s", path
);
181 fwrite(result
.ptr
, result
.size
, 1, f
);
193 static void unlink_rr_item(const char *name
)
195 unlink(rr_path(name
, "thisimage"));
196 unlink(rr_path(name
, "preimage"));
197 unlink(rr_path(name
, "postimage"));
198 rmdir(git_path("rr-cache/%s", name
));
201 static void garbage_collect(struct path_list
*rr
)
203 struct path_list to_remove
= { NULL
, 0, 0, 1 };
208 time_t now
= time(NULL
), then
;
210 strlcpy(buf
, git_path("rr-cache"), sizeof(buf
));
213 strcpy(buf
+ len
++, "/");
214 while ((e
= readdir(dir
))) {
215 const char *name
= e
->d_name
;
217 if (name
[0] == '.' && (name
[1] == '\0' ||
218 (name
[1] == '.' && name
[2] == '\0')))
220 i
= snprintf(buf
+ len
, sizeof(buf
) - len
, "%s", name
);
221 strlcpy(buf
+ len
+ i
, "/preimage", sizeof(buf
) - len
- i
);
225 strlcpy(buf
+ len
+ i
, "/postimage", sizeof(buf
) - len
- i
);
226 cutoff
= stat(buf
, &st
) ? cutoff_noresolve
: cutoff_resolve
;
227 if (then
< now
- cutoff
* 86400) {
229 path_list_insert(xstrdup(name
), &to_remove
);
232 for (i
= 0; i
< to_remove
.nr
; i
++)
233 unlink_rr_item(to_remove
.items
[i
].path
);
234 path_list_clear(&to_remove
, 0);
237 static int outf(void *dummy
, mmbuffer_t
*ptr
, int nbuf
)
240 for (i
= 0; i
< nbuf
; i
++)
241 if (write_in_full(1, ptr
[i
].ptr
, ptr
[i
].size
) != ptr
[i
].size
)
246 static int diff_two(const char *file1
, const char *label1
,
247 const char *file2
, const char *label2
)
252 mmfile_t minus
, plus
;
254 if (read_mmfile(&minus
, file1
) || read_mmfile(&plus
, file2
))
257 printf("--- a/%s\n+++ b/%s\n", label1
, label2
);
259 xpp
.flags
= XDF_NEED_MINIMAL
;
260 memset(&xecfg
, 0, sizeof(xecfg
));
263 xdi_diff(&minus
, &plus
, &xpp
, &xecfg
, &ecb
);
270 static int do_plain_rerere(struct path_list
*rr
, int fd
)
272 struct path_list conflict
= { NULL
, 0, 0, 1 };
275 find_conflict(&conflict
);
278 * MERGE_RR records paths with conflicts immediately after merge
279 * failed. Some of the conflicted paths might have been hand resolved
280 * in the working tree since then, but the initial run would catch all
281 * and register their preimages.
284 for (i
= 0; i
< conflict
.nr
; i
++) {
285 const char *path
= conflict
.items
[i
].path
;
286 if (!path_list_has_path(rr
, path
)) {
287 unsigned char sha1
[20];
290 ret
= handle_file(path
, sha1
, NULL
);
293 hex
= xstrdup(sha1_to_hex(sha1
));
294 path_list_insert(path
, rr
)->util
= hex
;
295 if (mkdir(git_path("rr-cache/%s", hex
), 0755))
297 handle_file(path
, NULL
, rr_path(hex
, "preimage"));
298 fprintf(stderr
, "Recorded preimage for '%s'\n", path
);
303 * Now some of the paths that had conflicts earlier might have been
304 * hand resolved. Others may be similar to a conflict already that
305 * was resolved before.
308 for (i
= 0; i
< rr
->nr
; i
++) {
311 const char *path
= rr
->items
[i
].path
;
312 const char *name
= (const char *)rr
->items
[i
].util
;
314 if (!stat(rr_path(name
, "preimage"), &st
) &&
315 !stat(rr_path(name
, "postimage"), &st
)) {
316 if (!merge(name
, path
)) {
317 fprintf(stderr
, "Resolved '%s' using "
318 "previous resolution.\n", path
);
319 goto tail_optimization
;
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(rr_path(name
, "postimage"), path
, 0666);
332 memmove(rr
->items
+ i
,
334 sizeof(rr
->items
[0]) * (rr
->nr
- i
- 1));
339 return write_rr(rr
, fd
);
342 static int git_rerere_config(const char *var
, const char *value
)
344 if (!strcmp(var
, "gc.rerereresolved"))
345 cutoff_resolve
= git_config_int(var
, value
);
346 else if (!strcmp(var
, "gc.rerereunresolved"))
347 cutoff_noresolve
= git_config_int(var
, value
);
348 else if (!strcmp(var
, "rerere.enabled"))
349 rerere_enabled
= git_config_bool(var
, value
);
351 return git_default_config(var
, value
);
355 static int is_rerere_enabled(void)
358 const char *rr_cache
;
364 rr_cache
= git_path("rr-cache");
365 rr_cache_exists
= !stat(rr_cache
, &st
) && S_ISDIR(st
.st_mode
);
366 if (rerere_enabled
< 0)
367 return rr_cache_exists
;
369 if (!rr_cache_exists
&&
370 (mkdir(rr_cache
, 0777) || adjust_shared_perm(rr_cache
)))
371 die("Could not create directory %s", rr_cache
);
375 static int setup_rerere(struct path_list
*merge_rr
)
379 git_config(git_rerere_config
);
380 if (!is_rerere_enabled())
383 merge_rr_path
= xstrdup(git_path("rr-cache/MERGE_RR"));
384 fd
= hold_lock_file_for_update(&write_lock
, merge_rr_path
, 1);
391 struct path_list merge_rr
= { NULL
, 0, 0, 1 };
394 fd
= setup_rerere(&merge_rr
);
397 return do_plain_rerere(&merge_rr
, fd
);
400 int cmd_rerere(int argc
, const char **argv
, const char *prefix
)
402 struct path_list merge_rr
= { NULL
, 0, 0, 1 };
405 fd
= setup_rerere(&merge_rr
);
410 return do_plain_rerere(&merge_rr
, fd
);
411 else if (!strcmp(argv
[1], "clear")) {
412 for (i
= 0; i
< merge_rr
.nr
; i
++) {
414 const char *name
= (const char *)merge_rr
.items
[i
].util
;
415 if (!stat(git_path("rr-cache/%s", name
), &st
) &&
416 S_ISDIR(st
.st_mode
) &&
417 stat(rr_path(name
, "postimage"), &st
))
418 unlink_rr_item(name
);
420 unlink(merge_rr_path
);
421 } else if (!strcmp(argv
[1], "gc"))
422 garbage_collect(&merge_rr
);
423 else if (!strcmp(argv
[1], "status"))
424 for (i
= 0; i
< merge_rr
.nr
; i
++)
425 printf("%s\n", merge_rr
.items
[i
].path
);
426 else if (!strcmp(argv
[1], "diff"))
427 for (i
= 0; i
< merge_rr
.nr
; i
++) {
428 const char *path
= merge_rr
.items
[i
].path
;
429 const char *name
= (const char *)merge_rr
.items
[i
].util
;
430 diff_two(rr_path(name
, "preimage"), path
, path
, path
);
433 usage(git_rerere_usage
);
435 path_list_clear(&merge_rr
, 1);