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 time_t rerere_last_used_at(const char *name
)
25 return stat(rerere_path(name
, "postimage"), &st
) ? (time_t) 0 : st
.st_mtime
;
28 static void unlink_rr_item(const char *name
)
30 unlink(rerere_path(name
, "thisimage"));
31 unlink(rerere_path(name
, "preimage"));
32 unlink(rerere_path(name
, "postimage"));
33 rmdir(git_path("rr-cache/%s", name
));
36 static int git_rerere_gc_config(const char *var
, const char *value
, void *cb
)
38 if (!strcmp(var
, "gc.rerereresolved"))
39 cutoff_resolve
= git_config_int(var
, value
);
40 else if (!strcmp(var
, "gc.rerereunresolved"))
41 cutoff_noresolve
= git_config_int(var
, value
);
43 return git_default_config(var
, value
, cb
);
47 static void garbage_collect(struct string_list
*rr
)
49 struct string_list to_remove
= { NULL
, 0, 0, 1 };
53 time_t now
= time(NULL
), then
;
55 git_config(git_rerere_gc_config
, NULL
);
56 dir
= opendir(git_path("rr-cache"));
58 die_errno("unable to open rr-cache directory");
59 while ((e
= readdir(dir
))) {
60 if (is_dot_or_dotdot(e
->d_name
))
63 then
= rerere_last_used_at(e
->d_name
);
65 cutoff
= cutoff_resolve
;
67 then
= rerere_created_at(e
->d_name
);
70 cutoff
= cutoff_noresolve
;
72 if (then
< now
- cutoff
* 86400)
73 string_list_append(&to_remove
, e
->d_name
);
75 for (i
= 0; i
< to_remove
.nr
; i
++)
76 unlink_rr_item(to_remove
.items
[i
].string
);
77 string_list_clear(&to_remove
, 0);
80 static int outf(void *dummy
, mmbuffer_t
*ptr
, int nbuf
)
83 for (i
= 0; i
< nbuf
; i
++)
84 if (write_in_full(1, ptr
[i
].ptr
, ptr
[i
].size
) != ptr
[i
].size
)
89 static int diff_two(const char *file1
, const char *label1
,
90 const char *file2
, const char *label2
)
97 if (read_mmfile(&minus
, file1
) || read_mmfile(&plus
, file2
))
100 printf("--- a/%s\n+++ b/%s\n", label1
, label2
);
102 memset(&xpp
, 0, sizeof(xpp
));
104 memset(&xecfg
, 0, sizeof(xecfg
));
107 xdi_diff(&minus
, &plus
, &xpp
, &xecfg
, &ecb
);
114 int cmd_rerere(int argc
, const char **argv
, const char *prefix
)
116 struct string_list merge_rr
= { NULL
, 0, 0, 1 };
117 int i
, fd
, flags
= 0;
120 if (!strcmp(argv
[1], "-h"))
121 usage(git_rerere_usage
);
122 if (!strcmp(argv
[1], "--rerere-autoupdate"))
123 flags
= RERERE_AUTOUPDATE
;
124 else if (!strcmp(argv
[1], "--no-rerere-autoupdate"))
125 flags
= RERERE_NOAUTOUPDATE
;
132 return rerere(flags
);
134 if (!strcmp(argv
[1], "forget")) {
135 const char **pathspec
= get_pathspec(prefix
, argv
+ 2);
136 return rerere_forget(pathspec
);
139 fd
= setup_rerere(&merge_rr
, flags
);
143 if (!strcmp(argv
[1], "clear")) {
144 for (i
= 0; i
< merge_rr
.nr
; i
++) {
145 const char *name
= (const char *)merge_rr
.items
[i
].util
;
146 if (!has_rerere_resolution(name
))
147 unlink_rr_item(name
);
149 unlink_or_warn(git_path("MERGE_RR"));
150 } else if (!strcmp(argv
[1], "gc"))
151 garbage_collect(&merge_rr
);
152 else if (!strcmp(argv
[1], "status"))
153 for (i
= 0; i
< merge_rr
.nr
; i
++)
154 printf("%s\n", merge_rr
.items
[i
].string
);
155 else if (!strcmp(argv
[1], "diff"))
156 for (i
= 0; i
< merge_rr
.nr
; i
++) {
157 const char *path
= merge_rr
.items
[i
].string
;
158 const char *name
= (const char *)merge_rr
.items
[i
].util
;
159 diff_two(rerere_path(name
, "preimage"), path
, path
, path
);
162 usage(git_rerere_usage
);
164 string_list_clear(&merge_rr
, 1);