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
);
74 static void append_line(struct buffer
*buffer
, const char *line
)
76 int len
= strlen(line
);
78 if (buffer
->nr
+ len
> buffer
->alloc
) {
79 buffer
->alloc
= alloc_nr(buffer
->nr
+ len
);
80 buffer
->ptr
= xrealloc(buffer
->ptr
, buffer
->alloc
);
82 memcpy(buffer
->ptr
+ buffer
->nr
, line
, len
);
86 static void clear_buffer(struct buffer
*buffer
)
90 buffer
->nr
= buffer
->alloc
= 0;
93 static int handle_file(const char *path
,
94 unsigned char *sha1
, const char *output
)
98 int hunk
= 0, hunk_no
= 0;
99 struct buffer minus
= { NULL
, 0, 0 }, plus
= { NULL
, 0, 0 };
100 struct buffer
*one
= &minus
, *two
= &plus
;
101 FILE *f
= fopen(path
, "r");
105 return error("Could not open %s", path
);
108 out
= fopen(output
, "w");
111 return error("Could not write %s", output
);
119 while (fgets(buf
, sizeof(buf
), f
)) {
120 if (!prefixcmp(buf
, "<<<<<<< "))
122 else if (!prefixcmp(buf
, "======="))
124 else if (!prefixcmp(buf
, ">>>>>>> ")) {
125 int one_is_longer
= (one
->nr
> two
->nr
);
126 int common_len
= one_is_longer
? two
->nr
: one
->nr
;
127 int cmp
= memcmp(one
->ptr
, two
->ptr
, common_len
);
131 if ((cmp
> 0) || ((cmp
== 0) && one_is_longer
)) {
132 struct buffer
*swap
= one
;
137 fputs("<<<<<<<\n", out
);
138 fwrite(one
->ptr
, one
->nr
, 1, out
);
139 fputs("=======\n", out
);
140 fwrite(two
->ptr
, two
->nr
, 1, out
);
141 fputs(">>>>>>>\n", out
);
144 SHA1_Update(&ctx
, one
->ptr
, one
->nr
);
145 SHA1_Update(&ctx
, "\0", 1);
146 SHA1_Update(&ctx
, two
->ptr
, two
->nr
);
147 SHA1_Update(&ctx
, "\0", 1);
151 } else if (hunk
== 1)
152 append_line(one
, buf
);
154 append_line(two
, buf
);
163 SHA1_Final(sha1
, &ctx
);
167 static int find_conflict(struct path_list
*conflict
)
170 if (read_cache() < 0)
171 return error("Could not read index");
172 for (i
= 0; i
+1 < active_nr
; i
++) {
173 struct cache_entry
*e2
= active_cache
[i
];
174 struct cache_entry
*e3
= active_cache
[i
+1];
175 if (ce_stage(e2
) == 2 &&
177 ce_same_name(e2
, e3
) &&
178 S_ISREG(ntohl(e2
->ce_mode
)) &&
179 S_ISREG(ntohl(e3
->ce_mode
))) {
180 path_list_insert((const char *)e2
->name
, conflict
);
181 i
++; /* skip over both #2 and #3 */
187 static int merge(const char *name
, const char *path
)
190 mmfile_t cur
, base
, other
;
191 mmbuffer_t result
= {NULL
, 0};
192 xpparam_t xpp
= {XDF_NEED_MINIMAL
};
194 if (handle_file(path
, NULL
, rr_path(name
, "thisimage")) < 0)
197 if (read_mmfile(&cur
, rr_path(name
, "thisimage")) ||
198 read_mmfile(&base
, rr_path(name
, "preimage")) ||
199 read_mmfile(&other
, rr_path(name
, "postimage")))
201 ret
= xdl_merge(&base
, &cur
, "", &other
, "",
202 &xpp
, XDL_MERGE_ZEALOUS
, &result
);
204 FILE *f
= fopen(path
, "w");
206 return error("Could not write to %s", path
);
207 fwrite(result
.ptr
, result
.size
, 1, f
);
219 static void unlink_rr_item(const char *name
)
221 unlink(rr_path(name
, "thisimage"));
222 unlink(rr_path(name
, "preimage"));
223 unlink(rr_path(name
, "postimage"));
224 rmdir(git_path("rr-cache/%s", name
));
227 static void garbage_collect(struct path_list
*rr
)
229 struct path_list to_remove
= { NULL
, 0, 0, 1 };
234 time_t now
= time(NULL
), then
;
236 strlcpy(buf
, git_path("rr-cache"), sizeof(buf
));
239 strcpy(buf
+ len
++, "/");
240 while ((e
= readdir(dir
))) {
241 const char *name
= e
->d_name
;
243 if (name
[0] == '.' && (name
[1] == '\0' ||
244 (name
[1] == '.' && name
[2] == '\0')))
246 i
= snprintf(buf
+ len
, sizeof(buf
) - len
, "%s", name
);
247 strlcpy(buf
+ len
+ i
, "/preimage", sizeof(buf
) - len
- i
);
251 strlcpy(buf
+ len
+ i
, "/postimage", sizeof(buf
) - len
- i
);
252 cutoff
= stat(buf
, &st
) ? cutoff_noresolve
: cutoff_resolve
;
253 if (then
< now
- cutoff
* 86400) {
255 path_list_insert(xstrdup(name
), &to_remove
);
258 for (i
= 0; i
< to_remove
.nr
; i
++)
259 unlink_rr_item(to_remove
.items
[i
].path
);
260 path_list_clear(&to_remove
, 0);
263 static int outf(void *dummy
, mmbuffer_t
*ptr
, int nbuf
)
266 for (i
= 0; i
< nbuf
; i
++)
267 if (write_in_full(1, ptr
[i
].ptr
, ptr
[i
].size
) != ptr
[i
].size
)
272 static int diff_two(const char *file1
, const char *label1
,
273 const char *file2
, const char *label2
)
278 mmfile_t minus
, plus
;
280 if (read_mmfile(&minus
, file1
) || read_mmfile(&plus
, file2
))
283 printf("--- a/%s\n+++ b/%s\n", label1
, label2
);
285 xpp
.flags
= XDF_NEED_MINIMAL
;
286 memset(&xecfg
, 0, sizeof(xecfg
));
289 xdl_diff(&minus
, &plus
, &xpp
, &xecfg
, &ecb
);
296 static int copy_file(const char *src
, const char *dest
)
302 if (!(in
= fopen(src
, "r")))
303 return error("Could not open %s", src
);
304 if (!(out
= fopen(dest
, "w")))
305 return error("Could not open %s", dest
);
306 while ((count
= fread(buffer
, 1, sizeof(buffer
), in
)))
307 fwrite(buffer
, 1, count
, out
);
313 static int do_plain_rerere(struct path_list
*rr
, int fd
)
315 struct path_list conflict
= { NULL
, 0, 0, 1 };
318 find_conflict(&conflict
);
321 * MERGE_RR records paths with conflicts immediately after merge
322 * failed. Some of the conflicted paths might have been hand resolved
323 * in the working tree since then, but the initial run would catch all
324 * and register their preimages.
327 for (i
= 0; i
< conflict
.nr
; i
++) {
328 const char *path
= conflict
.items
[i
].path
;
329 if (!path_list_has_path(rr
, path
)) {
330 unsigned char sha1
[20];
333 ret
= handle_file(path
, sha1
, NULL
);
336 hex
= xstrdup(sha1_to_hex(sha1
));
337 path_list_insert(path
, rr
)->util
= hex
;
338 if (mkdir(git_path("rr-cache/%s", hex
), 0755))
340 handle_file(path
, NULL
, rr_path(hex
, "preimage"));
341 fprintf(stderr
, "Recorded preimage for '%s'\n", path
);
346 * Now some of the paths that had conflicts earlier might have been
347 * hand resolved. Others may be similar to a conflict already that
348 * was resolved before.
351 for (i
= 0; i
< rr
->nr
; i
++) {
354 const char *path
= rr
->items
[i
].path
;
355 const char *name
= (const char *)rr
->items
[i
].util
;
357 if (!stat(rr_path(name
, "preimage"), &st
) &&
358 !stat(rr_path(name
, "postimage"), &st
)) {
359 if (!merge(name
, path
)) {
360 fprintf(stderr
, "Resolved '%s' using "
361 "previous resolution.\n", path
);
362 goto tail_optimization
;
366 /* Let's see if we have resolved it. */
367 ret
= handle_file(path
, NULL
, NULL
);
371 fprintf(stderr
, "Recorded resolution for '%s'.\n", path
);
372 copy_file(path
, rr_path(name
, "postimage"));
375 memmove(rr
->items
+ i
,
377 sizeof(rr
->items
[0]) * (rr
->nr
- i
- 1));
382 return write_rr(rr
, fd
);
385 static int git_rerere_config(const char *var
, const char *value
)
387 if (!strcmp(var
, "gc.rerereresolved"))
388 cutoff_resolve
= git_config_int(var
, value
);
389 else if (!strcmp(var
, "gc.rerereunresolved"))
390 cutoff_noresolve
= git_config_int(var
, value
);
391 else if (!strcmp(var
, "rerere.enabled"))
392 rerere_enabled
= git_config_bool(var
, value
);
394 return git_default_config(var
, value
);
398 static int is_rerere_enabled(void)
401 const char *rr_cache
;
407 rr_cache
= git_path("rr-cache");
408 rr_cache_exists
= !stat(rr_cache
, &st
) && S_ISDIR(st
.st_mode
);
409 if (rerere_enabled
< 0)
410 return rr_cache_exists
;
412 if (!rr_cache_exists
&&
413 (mkdir(rr_cache
, 0777) || adjust_shared_perm(rr_cache
)))
414 die("Could not create directory %s", rr_cache
);
418 int cmd_rerere(int argc
, const char **argv
, const char *prefix
)
420 struct path_list merge_rr
= { NULL
, 0, 0, 1 };
423 git_config(git_rerere_config
);
424 if (!is_rerere_enabled())
427 merge_rr_path
= xstrdup(git_path("rr-cache/MERGE_RR"));
428 fd
= hold_lock_file_for_update(&write_lock
, merge_rr_path
, 1);
432 return do_plain_rerere(&merge_rr
, fd
);
433 else if (!strcmp(argv
[1], "clear")) {
434 for (i
= 0; i
< merge_rr
.nr
; i
++) {
436 const char *name
= (const char *)merge_rr
.items
[i
].util
;
437 if (!stat(git_path("rr-cache/%s", name
), &st
) &&
438 S_ISDIR(st
.st_mode
) &&
439 stat(rr_path(name
, "postimage"), &st
))
440 unlink_rr_item(name
);
442 unlink(merge_rr_path
);
443 } else if (!strcmp(argv
[1], "gc"))
444 garbage_collect(&merge_rr
);
445 else if (!strcmp(argv
[1], "status"))
446 for (i
= 0; i
< merge_rr
.nr
; i
++)
447 printf("%s\n", merge_rr
.items
[i
].path
);
448 else if (!strcmp(argv
[1], "diff"))
449 for (i
= 0; i
< merge_rr
.nr
; i
++) {
450 const char *path
= merge_rr
.items
[i
].path
;
451 const char *name
= (const char *)merge_rr
.items
[i
].util
;
452 diff_two(rr_path(name
, "preimage"), path
, path
, path
);
455 usage(git_rerere_usage
);
457 path_list_clear(&merge_rr
, 1);