3 #include "string-list.h"
5 #include "xdiff/xdiff.h"
6 #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 static const char *rr_path(const char *name
, const char *file
)
17 return git_path("rr-cache/%s/%s", name
, file
);
20 static time_t rerere_created_at(const char *name
)
23 return stat(rr_path(name
, "preimage"), &st
) ? (time_t) 0 : st
.st_mtime
;
26 static int has_resolution(const char *name
)
29 return !stat(rr_path(name
, "postimage"), &st
);
32 static void unlink_rr_item(const char *name
)
34 unlink(rr_path(name
, "thisimage"));
35 unlink(rr_path(name
, "preimage"));
36 unlink(rr_path(name
, "postimage"));
37 rmdir(git_path("rr-cache/%s", name
));
40 static int git_rerere_gc_config(const char *var
, const char *value
, void *cb
)
42 if (!strcmp(var
, "gc.rerereresolved"))
43 cutoff_resolve
= git_config_int(var
, value
);
44 else if (!strcmp(var
, "gc.rerereunresolved"))
45 cutoff_noresolve
= git_config_int(var
, value
);
47 return git_default_config(var
, value
, cb
);
51 static void garbage_collect(struct string_list
*rr
)
53 struct string_list to_remove
= { NULL
, 0, 0, 1 };
57 time_t now
= time(NULL
), then
;
59 git_config(git_rerere_gc_config
, NULL
);
60 dir
= opendir(git_path("rr-cache"));
61 while ((e
= readdir(dir
))) {
62 const char *name
= e
->d_name
;
64 (name
[1] == '\0' || (name
[1] == '.' && name
[2] == '\0')))
66 then
= rerere_created_at(name
);
69 cutoff
= (has_resolution(name
)
70 ? cutoff_resolve
: cutoff_noresolve
);
71 if (then
< now
- cutoff
* 86400)
72 string_list_append(name
, &to_remove
);
74 for (i
= 0; i
< to_remove
.nr
; i
++)
75 unlink_rr_item(to_remove
.items
[i
].string
);
76 string_list_clear(&to_remove
, 0);
79 static int outf(void *dummy
, mmbuffer_t
*ptr
, int nbuf
)
82 for (i
= 0; i
< nbuf
; i
++)
83 if (write_in_full(1, ptr
[i
].ptr
, ptr
[i
].size
) != ptr
[i
].size
)
88 static int diff_two(const char *file1
, const char *label1
,
89 const char *file2
, const char *label2
)
96 if (read_mmfile(&minus
, file1
) || read_mmfile(&plus
, file2
))
99 printf("--- a/%s\n+++ b/%s\n", label1
, label2
);
101 memset(&xpp
, 0, sizeof(xpp
));
102 xpp
.flags
= XDF_NEED_MINIMAL
;
103 memset(&xecfg
, 0, sizeof(xecfg
));
106 xdi_diff(&minus
, &plus
, &xpp
, &xecfg
, &ecb
);
113 int cmd_rerere(int argc
, const char **argv
, const char *prefix
)
115 struct string_list merge_rr
= { NULL
, 0, 0, 1 };
121 fd
= setup_rerere(&merge_rr
);
125 if (!strcmp(argv
[1], "clear")) {
126 for (i
= 0; i
< merge_rr
.nr
; i
++) {
127 const char *name
= (const char *)merge_rr
.items
[i
].util
;
128 if (!has_resolution(name
))
129 unlink_rr_item(name
);
131 unlink(git_path("rr-cache/MERGE_RR"));
132 } else if (!strcmp(argv
[1], "gc"))
133 garbage_collect(&merge_rr
);
134 else if (!strcmp(argv
[1], "status"))
135 for (i
= 0; i
< merge_rr
.nr
; i
++)
136 printf("%s\n", merge_rr
.items
[i
].string
);
137 else if (!strcmp(argv
[1], "diff"))
138 for (i
= 0; i
< merge_rr
.nr
; i
++) {
139 const char *path
= merge_rr
.items
[i
].string
;
140 const char *name
= (const char *)merge_rr
.items
[i
].util
;
141 diff_two(rr_path(name
, "preimage"), path
, path
, path
);
144 usage(git_rerere_usage
);
146 string_list_clear(&merge_rr
, 1);