3 #include "xdiff/xdiff.h"
4 #include "xdiff-interface.h"
8 static const char git_rerere_usage
[] =
9 "git-rerere [clear | status | diff | gc]";
11 /* these values are days */
12 static int cutoff_noresolve
= 15;
13 static int cutoff_resolve
= 60;
15 /* if rerere_enabled == -1, fall back to detection of .git/rr-cache */
16 static int rerere_enabled
= -1;
18 static char *merge_rr_path
;
20 static const char *rr_path(const char *name
, const char *file
)
22 return git_path("rr-cache/%s/%s", name
, file
);
25 static void read_rr(struct path_list
*rr
)
27 unsigned char sha1
[20];
29 FILE *in
= fopen(merge_rr_path
, "r");
32 while (fread(buf
, 40, 1, in
) == 1) {
35 if (get_sha1_hex(buf
, sha1
))
36 die("corrupt MERGE_RR");
39 if (fgetc(in
) != '\t')
40 die("corrupt MERGE_RR");
41 for (i
= 0; i
< sizeof(buf
) && (buf
[i
] = fgetc(in
)); i
++)
44 die("filename too long");
45 path_list_insert(buf
, rr
)->util
= xstrdup(name
);
50 static struct lock_file write_lock
;
52 static int write_rr(struct path_list
*rr
, int out_fd
)
55 for (i
= 0; i
< rr
->nr
; i
++) {
56 const char *path
= rr
->items
[i
].path
;
57 int length
= strlen(path
) + 1;
58 if (write_in_full(out_fd
, rr
->items
[i
].util
, 40) != 40 ||
59 write_in_full(out_fd
, "\t", 1) != 1 ||
60 write_in_full(out_fd
, path
, length
) != length
)
61 die("unable to write rerere record");
63 if (close(out_fd
) != 0)
64 die("unable to write rerere record");
65 return commit_lock_file(&write_lock
);
73 static void append_line(struct buffer
*buffer
, const char *line
)
75 int len
= strlen(line
);
77 if (buffer
->nr
+ len
> buffer
->alloc
) {
78 buffer
->alloc
= alloc_nr(buffer
->nr
+ len
);
79 buffer
->ptr
= xrealloc(buffer
->ptr
, buffer
->alloc
);
81 memcpy(buffer
->ptr
+ buffer
->nr
, line
, len
);
85 static void clear_buffer(struct buffer
*buffer
)
89 buffer
->nr
= buffer
->alloc
= 0;
92 static int handle_file(const char *path
,
93 unsigned char *sha1
, const char *output
)
97 int hunk
= 0, hunk_no
= 0;
98 struct buffer minus
= { NULL
, 0, 0 }, plus
= { NULL
, 0, 0 };
99 struct buffer
*one
= &minus
, *two
= &plus
;
100 FILE *f
= fopen(path
, "r");
104 return error("Could not open %s", path
);
107 out
= fopen(output
, "w");
110 return error("Could not write %s", output
);
118 while (fgets(buf
, sizeof(buf
), f
)) {
119 if (!prefixcmp(buf
, "<<<<<<< "))
121 else if (!prefixcmp(buf
, "======="))
123 else if (!prefixcmp(buf
, ">>>>>>> ")) {
124 int one_is_longer
= (one
->nr
> two
->nr
);
125 int common_len
= one_is_longer
? two
->nr
: one
->nr
;
126 int cmp
= memcmp(one
->ptr
, two
->ptr
, common_len
);
130 if ((cmp
> 0) || ((cmp
== 0) && one_is_longer
)) {
131 struct buffer
*swap
= one
;
136 fputs("<<<<<<<\n", out
);
137 fwrite(one
->ptr
, one
->nr
, 1, out
);
138 fputs("=======\n", out
);
139 fwrite(two
->ptr
, two
->nr
, 1, out
);
140 fputs(">>>>>>>\n", out
);
143 SHA1_Update(&ctx
, one
->ptr
, one
->nr
);
144 SHA1_Update(&ctx
, "\0", 1);
145 SHA1_Update(&ctx
, two
->ptr
, two
->nr
);
146 SHA1_Update(&ctx
, "\0", 1);
150 } else if (hunk
== 1)
151 append_line(one
, buf
);
153 append_line(two
, buf
);
162 SHA1_Final(sha1
, &ctx
);
166 static int find_conflict(struct path_list
*conflict
)
169 if (read_cache() < 0)
170 return error("Could not read index");
171 for (i
= 0; i
+1 < active_nr
; i
++) {
172 struct cache_entry
*e2
= active_cache
[i
];
173 struct cache_entry
*e3
= active_cache
[i
+1];
174 if (ce_stage(e2
) == 2 &&
176 ce_same_name(e2
, e3
) &&
177 S_ISREG(ntohl(e2
->ce_mode
)) &&
178 S_ISREG(ntohl(e3
->ce_mode
))) {
179 path_list_insert((const char *)e2
->name
, conflict
);
180 i
++; /* skip over both #2 and #3 */
186 static int merge(const char *name
, const char *path
)
189 mmfile_t cur
, base
, other
;
190 mmbuffer_t result
= {NULL
, 0};
191 xpparam_t xpp
= {XDF_NEED_MINIMAL
};
193 if (handle_file(path
, NULL
, rr_path(name
, "thisimage")) < 0)
196 if (read_mmfile(&cur
, rr_path(name
, "thisimage")) ||
197 read_mmfile(&base
, rr_path(name
, "preimage")) ||
198 read_mmfile(&other
, rr_path(name
, "postimage")))
200 ret
= xdl_merge(&base
, &cur
, "", &other
, "",
201 &xpp
, XDL_MERGE_ZEALOUS
, &result
);
203 FILE *f
= fopen(path
, "w");
205 return error("Could not write to %s", path
);
206 fwrite(result
.ptr
, result
.size
, 1, f
);
218 static void unlink_rr_item(const char *name
)
220 unlink(rr_path(name
, "thisimage"));
221 unlink(rr_path(name
, "preimage"));
222 unlink(rr_path(name
, "postimage"));
223 rmdir(git_path("rr-cache/%s", name
));
226 static void garbage_collect(struct path_list
*rr
)
228 struct path_list to_remove
= { NULL
, 0, 0, 1 };
233 time_t now
= time(NULL
), then
;
235 strlcpy(buf
, git_path("rr-cache"), sizeof(buf
));
238 strcpy(buf
+ len
++, "/");
239 while ((e
= readdir(dir
))) {
240 const char *name
= e
->d_name
;
242 if (name
[0] == '.' && (name
[1] == '\0' ||
243 (name
[1] == '.' && name
[2] == '\0')))
245 i
= snprintf(buf
+ len
, sizeof(buf
) - len
, "%s", name
);
246 strlcpy(buf
+ len
+ i
, "/preimage", sizeof(buf
) - len
- i
);
250 strlcpy(buf
+ len
+ i
, "/postimage", sizeof(buf
) - len
- i
);
251 cutoff
= stat(buf
, &st
) ? cutoff_noresolve
: cutoff_resolve
;
252 if (then
< now
- cutoff
* 86400) {
254 path_list_insert(xstrdup(name
), &to_remove
);
257 for (i
= 0; i
< to_remove
.nr
; i
++)
258 unlink_rr_item(to_remove
.items
[i
].path
);
259 path_list_clear(&to_remove
, 0);
262 static int outf(void *dummy
, mmbuffer_t
*ptr
, int nbuf
)
265 for (i
= 0; i
< nbuf
; i
++)
266 if (write_in_full(1, ptr
[i
].ptr
, ptr
[i
].size
) != ptr
[i
].size
)
271 static int diff_two(const char *file1
, const char *label1
,
272 const char *file2
, const char *label2
)
277 mmfile_t minus
, plus
;
279 if (read_mmfile(&minus
, file1
) || read_mmfile(&plus
, file2
))
282 printf("--- a/%s\n+++ b/%s\n", label1
, label2
);
284 xpp
.flags
= XDF_NEED_MINIMAL
;
285 memset(&xecfg
, 0, sizeof(xecfg
));
288 xdl_diff(&minus
, &plus
, &xpp
, &xecfg
, &ecb
);
295 static int copy_file(const char *src
, const char *dest
)
301 if (!(in
= fopen(src
, "r")))
302 return error("Could not open %s", src
);
303 if (!(out
= fopen(dest
, "w")))
304 return error("Could not open %s", dest
);
305 while ((count
= fread(buffer
, 1, sizeof(buffer
), in
)))
306 fwrite(buffer
, 1, count
, out
);
312 static int do_plain_rerere(struct path_list
*rr
, int fd
)
314 struct path_list conflict
= { NULL
, 0, 0, 1 };
317 find_conflict(&conflict
);
320 * MERGE_RR records paths with conflicts immediately after merge
321 * failed. Some of the conflicted paths might have been hand resolved
322 * in the working tree since then, but the initial run would catch all
323 * and register their preimages.
326 for (i
= 0; i
< conflict
.nr
; i
++) {
327 const char *path
= conflict
.items
[i
].path
;
328 if (!path_list_has_path(rr
, path
)) {
329 unsigned char sha1
[20];
332 ret
= handle_file(path
, sha1
, NULL
);
335 hex
= xstrdup(sha1_to_hex(sha1
));
336 path_list_insert(path
, rr
)->util
= hex
;
337 if (mkdir(git_path("rr-cache/%s", hex
), 0755))
339 handle_file(path
, NULL
, rr_path(hex
, "preimage"));
340 fprintf(stderr
, "Recorded preimage for '%s'\n", path
);
345 * Now some of the paths that had conflicts earlier might have been
346 * hand resolved. Others may be similar to a conflict already that
347 * was resolved before.
350 for (i
= 0; i
< rr
->nr
; i
++) {
353 const char *path
= rr
->items
[i
].path
;
354 const char *name
= (const char *)rr
->items
[i
].util
;
356 if (!stat(rr_path(name
, "preimage"), &st
) &&
357 !stat(rr_path(name
, "postimage"), &st
)) {
358 if (!merge(name
, path
)) {
359 fprintf(stderr
, "Resolved '%s' using "
360 "previous resolution.\n", path
);
361 goto tail_optimization
;
365 /* Let's see if we have resolved it. */
366 ret
= handle_file(path
, NULL
, NULL
);
370 fprintf(stderr
, "Recorded resolution for '%s'.\n", path
);
371 copy_file(path
, rr_path(name
, "postimage"));
374 memmove(rr
->items
+ i
,
376 sizeof(rr
->items
[0]) * (rr
->nr
- i
- 1));
381 return write_rr(rr
, fd
);
384 static int git_rerere_config(const char *var
, const char *value
)
386 if (!strcmp(var
, "gc.rerereresolved"))
387 cutoff_resolve
= git_config_int(var
, value
);
388 else if (!strcmp(var
, "gc.rerereunresolved"))
389 cutoff_noresolve
= git_config_int(var
, value
);
390 else if (!strcmp(var
, "rerere.enabled"))
391 rerere_enabled
= git_config_bool(var
, value
);
393 return git_default_config(var
, value
);
397 static int is_rerere_enabled(void)
400 const char *rr_cache
;
406 rr_cache
= git_path("rr-cache");
407 rr_cache_exists
= !stat(rr_cache
, &st
) && S_ISDIR(st
.st_mode
);
408 if (rerere_enabled
< 0)
409 return rr_cache_exists
;
411 if (!rr_cache_exists
&&
412 (mkdir(rr_cache
, 0777) || adjust_shared_perm(rr_cache
)))
413 die("Could not create directory %s", rr_cache
);
417 int cmd_rerere(int argc
, const char **argv
, const char *prefix
)
419 struct path_list merge_rr
= { NULL
, 0, 0, 1 };
422 git_config(git_rerere_config
);
423 if (!is_rerere_enabled())
426 merge_rr_path
= xstrdup(git_path("rr-cache/MERGE_RR"));
427 fd
= hold_lock_file_for_update(&write_lock
, merge_rr_path
, 1);
431 return do_plain_rerere(&merge_rr
, fd
);
432 else if (!strcmp(argv
[1], "clear")) {
433 for (i
= 0; i
< merge_rr
.nr
; i
++) {
435 const char *name
= (const char *)merge_rr
.items
[i
].util
;
436 if (!stat(git_path("rr-cache/%s", name
), &st
) &&
437 S_ISDIR(st
.st_mode
) &&
438 stat(rr_path(name
, "postimage"), &st
))
439 unlink_rr_item(name
);
441 unlink(merge_rr_path
);
442 } else if (!strcmp(argv
[1], "gc"))
443 garbage_collect(&merge_rr
);
444 else if (!strcmp(argv
[1], "status"))
445 for (i
= 0; i
< merge_rr
.nr
; i
++)
446 printf("%s\n", merge_rr
.items
[i
].path
);
447 else if (!strcmp(argv
[1], "diff"))
448 for (i
= 0; i
< merge_rr
.nr
; i
++) {
449 const char *path
= merge_rr
.items
[i
].path
;
450 const char *name
= (const char *)merge_rr
.items
[i
].util
;
451 diff_two(rr_path(name
, "preimage"), path
, path
, path
);
454 usage(git_rerere_usage
);
456 path_list_clear(&merge_rr
, 1);