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 (close(out_fd
) != 0)
65 die("unable to write rerere record");
66 return commit_lock_file(&write_lock
);
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 minus
, plus
;
76 struct strbuf
*one
= &minus
, *two
= &plus
;
77 FILE *f
= fopen(path
, "r");
80 strbuf_init(&minus
, 0);
81 strbuf_init(&plus
, 0);
84 return error("Could not open %s", path
);
87 out
= fopen(output
, "w");
90 return error("Could not write %s", output
);
98 while (fgets(buf
, sizeof(buf
), f
)) {
99 if (!prefixcmp(buf
, "<<<<<<< "))
101 else if (!prefixcmp(buf
, "======="))
103 else if (!prefixcmp(buf
, ">>>>>>> ")) {
104 int one_is_longer
= (one
->len
> two
->len
);
105 int common_len
= one_is_longer
? two
->len
: one
->len
;
106 int cmp
= memcmp(one
->buf
, two
->buf
, common_len
);
110 if ((cmp
> 0) || ((cmp
== 0) && one_is_longer
)) {
111 struct strbuf
*swap
= one
;
116 fputs("<<<<<<<\n", out
);
117 fwrite(one
->buf
, one
->len
, 1, out
);
118 fputs("=======\n", out
);
119 fwrite(two
->buf
, two
->len
, 1, out
);
120 fputs(">>>>>>>\n", out
);
123 SHA1_Update(&ctx
, one
->buf
, one
->len
);
124 SHA1_Update(&ctx
, "\0", 1);
125 SHA1_Update(&ctx
, two
->buf
, two
->len
);
126 SHA1_Update(&ctx
, "\0", 1);
130 } else if (hunk
== 1)
131 strbuf_addstr(one
, buf
);
133 strbuf_addstr(two
, buf
);
142 SHA1_Final(sha1
, &ctx
);
146 static int find_conflict(struct path_list
*conflict
)
149 if (read_cache() < 0)
150 return error("Could not read index");
151 for (i
= 0; i
+1 < active_nr
; i
++) {
152 struct cache_entry
*e2
= active_cache
[i
];
153 struct cache_entry
*e3
= active_cache
[i
+1];
154 if (ce_stage(e2
) == 2 &&
156 ce_same_name(e2
, e3
) &&
157 S_ISREG(ntohl(e2
->ce_mode
)) &&
158 S_ISREG(ntohl(e3
->ce_mode
))) {
159 path_list_insert((const char *)e2
->name
, conflict
);
160 i
++; /* skip over both #2 and #3 */
166 static int merge(const char *name
, const char *path
)
169 mmfile_t cur
, base
, other
;
170 mmbuffer_t result
= {NULL
, 0};
171 xpparam_t xpp
= {XDF_NEED_MINIMAL
};
173 if (handle_file(path
, NULL
, rr_path(name
, "thisimage")) < 0)
176 if (read_mmfile(&cur
, rr_path(name
, "thisimage")) ||
177 read_mmfile(&base
, rr_path(name
, "preimage")) ||
178 read_mmfile(&other
, rr_path(name
, "postimage")))
180 ret
= xdl_merge(&base
, &cur
, "", &other
, "",
181 &xpp
, XDL_MERGE_ZEALOUS
, &result
);
183 FILE *f
= fopen(path
, "w");
185 return error("Could not write to %s", path
);
186 fwrite(result
.ptr
, result
.size
, 1, f
);
198 static void unlink_rr_item(const char *name
)
200 unlink(rr_path(name
, "thisimage"));
201 unlink(rr_path(name
, "preimage"));
202 unlink(rr_path(name
, "postimage"));
203 rmdir(git_path("rr-cache/%s", name
));
206 static void garbage_collect(struct path_list
*rr
)
208 struct path_list to_remove
= { NULL
, 0, 0, 1 };
213 time_t now
= time(NULL
), then
;
215 strlcpy(buf
, git_path("rr-cache"), sizeof(buf
));
218 strcpy(buf
+ len
++, "/");
219 while ((e
= readdir(dir
))) {
220 const char *name
= e
->d_name
;
222 if (name
[0] == '.' && (name
[1] == '\0' ||
223 (name
[1] == '.' && name
[2] == '\0')))
225 i
= snprintf(buf
+ len
, sizeof(buf
) - len
, "%s", name
);
226 strlcpy(buf
+ len
+ i
, "/preimage", sizeof(buf
) - len
- i
);
230 strlcpy(buf
+ len
+ i
, "/postimage", sizeof(buf
) - len
- i
);
231 cutoff
= stat(buf
, &st
) ? cutoff_noresolve
: cutoff_resolve
;
232 if (then
< now
- cutoff
* 86400) {
234 path_list_insert(xstrdup(name
), &to_remove
);
237 for (i
= 0; i
< to_remove
.nr
; i
++)
238 unlink_rr_item(to_remove
.items
[i
].path
);
239 path_list_clear(&to_remove
, 0);
242 static int outf(void *dummy
, mmbuffer_t
*ptr
, int nbuf
)
245 for (i
= 0; i
< nbuf
; i
++)
246 if (write_in_full(1, ptr
[i
].ptr
, ptr
[i
].size
) != ptr
[i
].size
)
251 static int diff_two(const char *file1
, const char *label1
,
252 const char *file2
, const char *label2
)
257 mmfile_t minus
, plus
;
259 if (read_mmfile(&minus
, file1
) || read_mmfile(&plus
, file2
))
262 printf("--- a/%s\n+++ b/%s\n", label1
, label2
);
264 xpp
.flags
= XDF_NEED_MINIMAL
;
265 memset(&xecfg
, 0, sizeof(xecfg
));
268 xdl_diff(&minus
, &plus
, &xpp
, &xecfg
, &ecb
);
275 static int copy_file(const char *src
, const char *dest
)
281 if (!(in
= fopen(src
, "r")))
282 return error("Could not open %s", src
);
283 if (!(out
= fopen(dest
, "w")))
284 return error("Could not open %s", dest
);
285 while ((count
= fread(buffer
, 1, sizeof(buffer
), in
)))
286 fwrite(buffer
, 1, count
, out
);
292 static int do_plain_rerere(struct path_list
*rr
, int fd
)
294 struct path_list conflict
= { NULL
, 0, 0, 1 };
297 find_conflict(&conflict
);
300 * MERGE_RR records paths with conflicts immediately after merge
301 * failed. Some of the conflicted paths might have been hand resolved
302 * in the working tree since then, but the initial run would catch all
303 * and register their preimages.
306 for (i
= 0; i
< conflict
.nr
; i
++) {
307 const char *path
= conflict
.items
[i
].path
;
308 if (!path_list_has_path(rr
, path
)) {
309 unsigned char sha1
[20];
312 ret
= handle_file(path
, sha1
, NULL
);
315 hex
= xstrdup(sha1_to_hex(sha1
));
316 path_list_insert(path
, rr
)->util
= hex
;
317 if (mkdir(git_path("rr-cache/%s", hex
), 0755))
319 handle_file(path
, NULL
, rr_path(hex
, "preimage"));
320 fprintf(stderr
, "Recorded preimage for '%s'\n", path
);
325 * Now some of the paths that had conflicts earlier might have been
326 * hand resolved. Others may be similar to a conflict already that
327 * was resolved before.
330 for (i
= 0; i
< rr
->nr
; i
++) {
333 const char *path
= rr
->items
[i
].path
;
334 const char *name
= (const char *)rr
->items
[i
].util
;
336 if (!stat(rr_path(name
, "preimage"), &st
) &&
337 !stat(rr_path(name
, "postimage"), &st
)) {
338 if (!merge(name
, path
)) {
339 fprintf(stderr
, "Resolved '%s' using "
340 "previous resolution.\n", path
);
341 goto tail_optimization
;
345 /* Let's see if we have resolved it. */
346 ret
= handle_file(path
, NULL
, NULL
);
350 fprintf(stderr
, "Recorded resolution for '%s'.\n", path
);
351 copy_file(path
, rr_path(name
, "postimage"));
354 memmove(rr
->items
+ i
,
356 sizeof(rr
->items
[0]) * (rr
->nr
- i
- 1));
361 return write_rr(rr
, fd
);
364 static int git_rerere_config(const char *var
, const char *value
)
366 if (!strcmp(var
, "gc.rerereresolved"))
367 cutoff_resolve
= git_config_int(var
, value
);
368 else if (!strcmp(var
, "gc.rerereunresolved"))
369 cutoff_noresolve
= git_config_int(var
, value
);
370 else if (!strcmp(var
, "rerere.enabled"))
371 rerere_enabled
= git_config_bool(var
, value
);
373 return git_default_config(var
, value
);
377 static int is_rerere_enabled(void)
380 const char *rr_cache
;
386 rr_cache
= git_path("rr-cache");
387 rr_cache_exists
= !stat(rr_cache
, &st
) && S_ISDIR(st
.st_mode
);
388 if (rerere_enabled
< 0)
389 return rr_cache_exists
;
391 if (!rr_cache_exists
&&
392 (mkdir(rr_cache
, 0777) || adjust_shared_perm(rr_cache
)))
393 die("Could not create directory %s", rr_cache
);
397 int cmd_rerere(int argc
, const char **argv
, const char *prefix
)
399 struct path_list merge_rr
= { NULL
, 0, 0, 1 };
402 git_config(git_rerere_config
);
403 if (!is_rerere_enabled())
406 merge_rr_path
= xstrdup(git_path("rr-cache/MERGE_RR"));
407 fd
= hold_lock_file_for_update(&write_lock
, merge_rr_path
, 1);
411 return do_plain_rerere(&merge_rr
, fd
);
412 else if (!strcmp(argv
[1], "clear")) {
413 for (i
= 0; i
< merge_rr
.nr
; i
++) {
415 const char *name
= (const char *)merge_rr
.items
[i
].util
;
416 if (!stat(git_path("rr-cache/%s", name
), &st
) &&
417 S_ISDIR(st
.st_mode
) &&
418 stat(rr_path(name
, "postimage"), &st
))
419 unlink_rr_item(name
);
421 unlink(merge_rr_path
);
422 } else if (!strcmp(argv
[1], "gc"))
423 garbage_collect(&merge_rr
);
424 else if (!strcmp(argv
[1], "status"))
425 for (i
= 0; i
< merge_rr
.nr
; i
++)
426 printf("%s\n", merge_rr
.items
[i
].path
);
427 else if (!strcmp(argv
[1], "diff"))
428 for (i
= 0; i
< merge_rr
.nr
; i
++) {
429 const char *path
= merge_rr
.items
[i
].path
;
430 const char *name
= (const char *)merge_rr
.items
[i
].util
;
431 diff_two(rr_path(name
, "preimage"), path
, path
, path
);
434 usage(git_rerere_usage
);
436 path_list_clear(&merge_rr
, 1);