4 #include "string-list.h"
6 #include "xdiff/xdiff.h"
7 #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 static time_t rerere_created_at(const char *name
)
19 return stat(rerere_path(name
, "preimage"), &st
) ? (time_t) 0 : st
.st_mtime
;
22 static void unlink_rr_item(const char *name
)
24 unlink(rerere_path(name
, "thisimage"));
25 unlink(rerere_path(name
, "preimage"));
26 unlink(rerere_path(name
, "postimage"));
27 rmdir(git_path("rr-cache/%s", name
));
30 static int git_rerere_gc_config(const char *var
, const char *value
, void *cb
)
32 if (!strcmp(var
, "gc.rerereresolved"))
33 cutoff_resolve
= git_config_int(var
, value
);
34 else if (!strcmp(var
, "gc.rerereunresolved"))
35 cutoff_noresolve
= git_config_int(var
, value
);
37 return git_default_config(var
, value
, cb
);
41 static void garbage_collect(struct string_list
*rr
)
43 struct string_list to_remove
= { NULL
, 0, 0, 1 };
47 time_t now
= time(NULL
), then
;
49 git_config(git_rerere_gc_config
, NULL
);
50 dir
= opendir(git_path("rr-cache"));
52 die_errno("unable to open rr-cache directory");
53 while ((e
= readdir(dir
))) {
54 if (is_dot_or_dotdot(e
->d_name
))
56 then
= rerere_created_at(e
->d_name
);
59 cutoff
= (has_rerere_resolution(e
->d_name
)
60 ? cutoff_resolve
: cutoff_noresolve
);
61 if (then
< now
- cutoff
* 86400)
62 string_list_append(e
->d_name
, &to_remove
);
64 for (i
= 0; i
< to_remove
.nr
; i
++)
65 unlink_rr_item(to_remove
.items
[i
].string
);
66 string_list_clear(&to_remove
, 0);
69 static int outf(void *dummy
, mmbuffer_t
*ptr
, int nbuf
)
72 for (i
= 0; i
< nbuf
; i
++)
73 if (write_in_full(1, ptr
[i
].ptr
, ptr
[i
].size
) != ptr
[i
].size
)
78 static int diff_two(const char *file1
, const char *label1
,
79 const char *file2
, const char *label2
)
86 if (read_mmfile(&minus
, file1
) || read_mmfile(&plus
, file2
))
89 printf("--- a/%s\n+++ b/%s\n", label1
, label2
);
91 memset(&xpp
, 0, sizeof(xpp
));
93 memset(&xecfg
, 0, sizeof(xecfg
));
96 xdi_diff(&minus
, &plus
, &xpp
, &xecfg
, &ecb
);
103 int cmd_rerere(int argc
, const char **argv
, const char *prefix
)
105 struct string_list merge_rr
= { NULL
, 0, 0, 1 };
106 int i
, fd
, flags
= 0;
109 if (!strcmp(argv
[1], "-h"))
110 usage(git_rerere_usage
);
111 if (!strcmp(argv
[1], "--rerere-autoupdate"))
112 flags
= RERERE_AUTOUPDATE
;
113 else if (!strcmp(argv
[1], "--no-rerere-autoupdate"))
114 flags
= RERERE_NOAUTOUPDATE
;
121 return rerere(flags
);
123 if (!strcmp(argv
[1], "forget")) {
124 const char **pathspec
= get_pathspec(prefix
, argv
+ 2);
125 return rerere_forget(pathspec
);
128 fd
= setup_rerere(&merge_rr
, flags
);
132 if (!strcmp(argv
[1], "clear")) {
133 for (i
= 0; i
< merge_rr
.nr
; i
++) {
134 const char *name
= (const char *)merge_rr
.items
[i
].util
;
135 if (!has_rerere_resolution(name
))
136 unlink_rr_item(name
);
138 unlink_or_warn(git_path("rr-cache/MERGE_RR"));
139 } else if (!strcmp(argv
[1], "gc"))
140 garbage_collect(&merge_rr
);
141 else if (!strcmp(argv
[1], "status"))
142 for (i
= 0; i
< merge_rr
.nr
; i
++)
143 printf("%s\n", merge_rr
.items
[i
].string
);
144 else if (!strcmp(argv
[1], "diff"))
145 for (i
= 0; i
< merge_rr
.nr
; i
++) {
146 const char *path
= merge_rr
.items
[i
].string
;
147 const char *name
= (const char *)merge_rr
.items
[i
].util
;
148 diff_two(rerere_path(name
, "preimage"), path
, path
, path
);
151 usage(git_rerere_usage
);
153 string_list_clear(&merge_rr
, 1);