5 #include "xdiff/xdiff.h"
6 #include "xdiff-interface.h"
10 static const char git_rerere_usage
[] =
11 "git-rerere [clear | status | diff | gc]";
13 /* these values are days */
14 static int cutoff_noresolve
= 15;
15 static int cutoff_resolve
= 60;
17 /* if rerere_enabled == -1, fall back to detection of .git/rr-cache */
18 static int rerere_enabled
= -1;
20 static char *merge_rr_path
;
22 static const char *rr_path(const char *name
, const char *file
)
24 return git_path("rr-cache/%s/%s", name
, file
);
27 static void read_rr(struct path_list
*rr
)
29 unsigned char sha1
[20];
31 FILE *in
= fopen(merge_rr_path
, "r");
34 while (fread(buf
, 40, 1, in
) == 1) {
37 if (get_sha1_hex(buf
, sha1
))
38 die("corrupt MERGE_RR");
41 if (fgetc(in
) != '\t')
42 die("corrupt MERGE_RR");
43 for (i
= 0; i
< sizeof(buf
) && (buf
[i
] = fgetc(in
)); i
++)
46 die("filename too long");
47 path_list_insert(buf
, rr
)->util
= xstrdup(name
);
52 static struct lock_file write_lock
;
54 static int write_rr(struct path_list
*rr
, int out_fd
)
57 for (i
= 0; i
< rr
->nr
; i
++) {
58 const char *path
= rr
->items
[i
].path
;
59 int length
= strlen(path
) + 1;
60 if (write_in_full(out_fd
, rr
->items
[i
].util
, 40) != 40 ||
61 write_in_full(out_fd
, "\t", 1) != 1 ||
62 write_in_full(out_fd
, path
, length
) != length
)
63 die("unable to write rerere record");
65 if (close(out_fd
) != 0)
66 die("unable to write rerere record");
67 return commit_lock_file(&write_lock
);
70 static int handle_file(const char *path
,
71 unsigned char *sha1
, const char *output
)
75 int hunk
= 0, hunk_no
= 0;
76 struct strbuf minus
, plus
;
77 struct strbuf
*one
= &minus
, *two
= &plus
;
78 FILE *f
= fopen(path
, "r");
81 strbuf_init(&minus
, 0);
82 strbuf_init(&plus
, 0);
85 return error("Could not open %s", path
);
88 out
= fopen(output
, "w");
91 return error("Could not write %s", output
);
99 while (fgets(buf
, sizeof(buf
), f
)) {
100 if (!prefixcmp(buf
, "<<<<<<< "))
102 else if (!prefixcmp(buf
, "======="))
104 else if (!prefixcmp(buf
, ">>>>>>> ")) {
105 int one_is_longer
= (one
->len
> two
->len
);
106 int common_len
= one_is_longer
? two
->len
: one
->len
;
107 int cmp
= memcmp(one
->buf
, two
->buf
, common_len
);
111 if ((cmp
> 0) || ((cmp
== 0) && one_is_longer
)) {
112 struct strbuf
*swap
= one
;
117 fputs("<<<<<<<\n", out
);
118 fwrite(one
->buf
, one
->len
, 1, out
);
119 fputs("=======\n", out
);
120 fwrite(two
->buf
, two
->len
, 1, out
);
121 fputs(">>>>>>>\n", out
);
124 SHA1_Update(&ctx
, one
->buf
, one
->len
);
125 SHA1_Update(&ctx
, "\0", 1);
126 SHA1_Update(&ctx
, two
->buf
, two
->len
);
127 SHA1_Update(&ctx
, "\0", 1);
131 } else if (hunk
== 1)
132 strbuf_addstr(one
, buf
);
134 strbuf_addstr(two
, buf
);
143 SHA1_Final(sha1
, &ctx
);
147 static int find_conflict(struct path_list
*conflict
)
150 if (read_cache() < 0)
151 return error("Could not read index");
152 for (i
= 0; i
+1 < active_nr
; i
++) {
153 struct cache_entry
*e2
= active_cache
[i
];
154 struct cache_entry
*e3
= active_cache
[i
+1];
155 if (ce_stage(e2
) == 2 &&
157 ce_same_name(e2
, e3
) &&
158 S_ISREG(ntohl(e2
->ce_mode
)) &&
159 S_ISREG(ntohl(e3
->ce_mode
))) {
160 path_list_insert((const char *)e2
->name
, conflict
);
161 i
++; /* skip over both #2 and #3 */
167 static int merge(const char *name
, const char *path
)
170 mmfile_t cur
, base
, other
;
171 mmbuffer_t result
= {NULL
, 0};
172 xpparam_t xpp
= {XDF_NEED_MINIMAL
};
174 if (handle_file(path
, NULL
, rr_path(name
, "thisimage")) < 0)
177 if (read_mmfile(&cur
, rr_path(name
, "thisimage")) ||
178 read_mmfile(&base
, rr_path(name
, "preimage")) ||
179 read_mmfile(&other
, rr_path(name
, "postimage")))
181 ret
= xdl_merge(&base
, &cur
, "", &other
, "",
182 &xpp
, XDL_MERGE_ZEALOUS
, &result
);
184 FILE *f
= fopen(path
, "w");
186 return error("Could not write to %s", path
);
187 fwrite(result
.ptr
, result
.size
, 1, f
);
199 static void unlink_rr_item(const char *name
)
201 unlink(rr_path(name
, "thisimage"));
202 unlink(rr_path(name
, "preimage"));
203 unlink(rr_path(name
, "postimage"));
204 rmdir(git_path("rr-cache/%s", name
));
207 static void garbage_collect(struct path_list
*rr
)
209 struct path_list to_remove
= { NULL
, 0, 0, 1 };
214 time_t now
= time(NULL
), then
;
216 strlcpy(buf
, git_path("rr-cache"), sizeof(buf
));
219 strcpy(buf
+ len
++, "/");
220 while ((e
= readdir(dir
))) {
221 const char *name
= e
->d_name
;
223 if (name
[0] == '.' && (name
[1] == '\0' ||
224 (name
[1] == '.' && name
[2] == '\0')))
226 i
= snprintf(buf
+ len
, sizeof(buf
) - len
, "%s", name
);
227 strlcpy(buf
+ len
+ i
, "/preimage", sizeof(buf
) - len
- i
);
231 strlcpy(buf
+ len
+ i
, "/postimage", sizeof(buf
) - len
- i
);
232 cutoff
= stat(buf
, &st
) ? cutoff_noresolve
: cutoff_resolve
;
233 if (then
< now
- cutoff
* 86400) {
235 path_list_insert(xstrdup(name
), &to_remove
);
238 for (i
= 0; i
< to_remove
.nr
; i
++)
239 unlink_rr_item(to_remove
.items
[i
].path
);
240 path_list_clear(&to_remove
, 0);
243 static int outf(void *dummy
, mmbuffer_t
*ptr
, int nbuf
)
246 for (i
= 0; i
< nbuf
; i
++)
247 if (write_in_full(1, ptr
[i
].ptr
, ptr
[i
].size
) != ptr
[i
].size
)
252 static int diff_two(const char *file1
, const char *label1
,
253 const char *file2
, const char *label2
)
258 mmfile_t minus
, plus
;
260 if (read_mmfile(&minus
, file1
) || read_mmfile(&plus
, file2
))
263 printf("--- a/%s\n+++ b/%s\n", label1
, label2
);
265 xpp
.flags
= XDF_NEED_MINIMAL
;
266 memset(&xecfg
, 0, sizeof(xecfg
));
269 xdl_diff(&minus
, &plus
, &xpp
, &xecfg
, &ecb
);
276 static int copy_file(const char *src
, const char *dest
)
282 if (!(in
= fopen(src
, "r")))
283 return error("Could not open %s", src
);
284 if (!(out
= fopen(dest
, "w")))
285 return error("Could not open %s", dest
);
286 while ((count
= fread(buffer
, 1, sizeof(buffer
), in
)))
287 fwrite(buffer
, 1, count
, out
);
293 static int do_plain_rerere(struct path_list
*rr
, int fd
)
295 struct path_list conflict
= { NULL
, 0, 0, 1 };
298 find_conflict(&conflict
);
301 * MERGE_RR records paths with conflicts immediately after merge
302 * failed. Some of the conflicted paths might have been hand resolved
303 * in the working tree since then, but the initial run would catch all
304 * and register their preimages.
307 for (i
= 0; i
< conflict
.nr
; i
++) {
308 const char *path
= conflict
.items
[i
].path
;
309 if (!path_list_has_path(rr
, path
)) {
310 unsigned char sha1
[20];
313 ret
= handle_file(path
, sha1
, NULL
);
316 hex
= xstrdup(sha1_to_hex(sha1
));
317 path_list_insert(path
, rr
)->util
= hex
;
318 if (mkdir(git_path("rr-cache/%s", hex
), 0755))
320 handle_file(path
, NULL
, rr_path(hex
, "preimage"));
321 fprintf(stderr
, "Recorded preimage for '%s'\n", path
);
326 * Now some of the paths that had conflicts earlier might have been
327 * hand resolved. Others may be similar to a conflict already that
328 * was resolved before.
331 for (i
= 0; i
< rr
->nr
; i
++) {
334 const char *path
= rr
->items
[i
].path
;
335 const char *name
= (const char *)rr
->items
[i
].util
;
337 if (!stat(rr_path(name
, "preimage"), &st
) &&
338 !stat(rr_path(name
, "postimage"), &st
)) {
339 if (!merge(name
, path
)) {
340 fprintf(stderr
, "Resolved '%s' using "
341 "previous resolution.\n", path
);
342 goto tail_optimization
;
346 /* Let's see if we have resolved it. */
347 ret
= handle_file(path
, NULL
, NULL
);
351 fprintf(stderr
, "Recorded resolution for '%s'.\n", path
);
352 copy_file(path
, rr_path(name
, "postimage"));
355 memmove(rr
->items
+ i
,
357 sizeof(rr
->items
[0]) * (rr
->nr
- i
- 1));
362 return write_rr(rr
, fd
);
365 static int git_rerere_config(const char *var
, const char *value
)
367 if (!strcmp(var
, "gc.rerereresolved"))
368 cutoff_resolve
= git_config_int(var
, value
);
369 else if (!strcmp(var
, "gc.rerereunresolved"))
370 cutoff_noresolve
= git_config_int(var
, value
);
371 else if (!strcmp(var
, "rerere.enabled"))
372 rerere_enabled
= git_config_bool(var
, value
);
374 return git_default_config(var
, value
);
378 static int is_rerere_enabled(void)
381 const char *rr_cache
;
387 rr_cache
= git_path("rr-cache");
388 rr_cache_exists
= !stat(rr_cache
, &st
) && S_ISDIR(st
.st_mode
);
389 if (rerere_enabled
< 0)
390 return rr_cache_exists
;
392 if (!rr_cache_exists
&&
393 (mkdir(rr_cache
, 0777) || adjust_shared_perm(rr_cache
)))
394 die("Could not create directory %s", rr_cache
);
398 int cmd_rerere(int argc
, const char **argv
, const char *prefix
)
400 struct path_list merge_rr
= { NULL
, 0, 0, 1 };
403 git_config(git_rerere_config
);
404 if (!is_rerere_enabled())
407 merge_rr_path
= xstrdup(git_path("rr-cache/MERGE_RR"));
408 fd
= hold_lock_file_for_update(&write_lock
, merge_rr_path
, 1);
412 return do_plain_rerere(&merge_rr
, fd
);
413 else if (!strcmp(argv
[1], "clear")) {
414 for (i
= 0; i
< merge_rr
.nr
; i
++) {
416 const char *name
= (const char *)merge_rr
.items
[i
].util
;
417 if (!stat(git_path("rr-cache/%s", name
), &st
) &&
418 S_ISDIR(st
.st_mode
) &&
419 stat(rr_path(name
, "postimage"), &st
))
420 unlink_rr_item(name
);
422 unlink(merge_rr_path
);
423 } else if (!strcmp(argv
[1], "gc"))
424 garbage_collect(&merge_rr
);
425 else if (!strcmp(argv
[1], "status"))
426 for (i
= 0; i
< merge_rr
.nr
; i
++)
427 printf("%s\n", merge_rr
.items
[i
].path
);
428 else if (!strcmp(argv
[1], "diff"))
429 for (i
= 0; i
< merge_rr
.nr
; i
++) {
430 const char *path
= merge_rr
.items
[i
].path
;
431 const char *name
= (const char *)merge_rr
.items
[i
].util
;
432 diff_two(rr_path(name
, "preimage"), path
, path
, path
);
435 usage(git_rerere_usage
);
437 path_list_clear(&merge_rr
, 1);