]>
Commit | Line | Data |
---|---|---|
baffc0e7 | 1 | #include "builtin.h" |
658f3650 | 2 | #include "cache.h" |
8ca12c0d | 3 | #include "dir.h" |
c455c87c | 4 | #include "string-list.h" |
5b2fd956 | 5 | #include "rerere.h" |
658f3650 JS |
6 | #include "xdiff/xdiff.h" |
7 | #include "xdiff-interface.h" | |
8 | ||
658f3650 | 9 | static const char git_rerere_usage[] = |
1b1dd23f | 10 | "git rerere [clear | status | diff | gc]"; |
658f3650 JS |
11 | |
12 | /* these values are days */ | |
13 | static int cutoff_noresolve = 15; | |
14 | static int cutoff_resolve = 60; | |
15 | ||
658f3650 JS |
16 | static const char *rr_path(const char *name, const char *file) |
17 | { | |
18 | return git_path("rr-cache/%s/%s", name, file); | |
19 | } | |
20 | ||
9022a495 JH |
21 | static time_t rerere_created_at(const char *name) |
22 | { | |
23 | struct stat st; | |
24 | return stat(rr_path(name, "preimage"), &st) ? (time_t) 0 : st.st_mtime; | |
25 | } | |
26 | ||
27 | static int has_resolution(const char *name) | |
28 | { | |
29 | struct stat st; | |
30 | return !stat(rr_path(name, "postimage"), &st); | |
31 | } | |
32 | ||
658f3650 JS |
33 | static void unlink_rr_item(const char *name) |
34 | { | |
35 | unlink(rr_path(name, "thisimage")); | |
36 | unlink(rr_path(name, "preimage")); | |
37 | unlink(rr_path(name, "postimage")); | |
38 | rmdir(git_path("rr-cache/%s", name)); | |
39 | } | |
40 | ||
5b2fd956 SB |
41 | static int git_rerere_gc_config(const char *var, const char *value, void *cb) |
42 | { | |
43 | if (!strcmp(var, "gc.rerereresolved")) | |
44 | cutoff_resolve = git_config_int(var, value); | |
45 | else if (!strcmp(var, "gc.rerereunresolved")) | |
46 | cutoff_noresolve = git_config_int(var, value); | |
47 | else | |
48 | return git_default_config(var, value, cb); | |
49 | return 0; | |
50 | } | |
51 | ||
c455c87c | 52 | static void garbage_collect(struct string_list *rr) |
658f3650 | 53 | { |
c455c87c | 54 | struct string_list to_remove = { NULL, 0, 0, 1 }; |
658f3650 JS |
55 | DIR *dir; |
56 | struct dirent *e; | |
9022a495 | 57 | int i, cutoff; |
658f3650 JS |
58 | time_t now = time(NULL), then; |
59 | ||
5b2fd956 | 60 | git_config(git_rerere_gc_config, NULL); |
9022a495 | 61 | dir = opendir(git_path("rr-cache")); |
658f3650 | 62 | while ((e = readdir(dir))) { |
8ca12c0d | 63 | if (is_dot_or_dotdot(e->d_name)) |
658f3650 | 64 | continue; |
8ca12c0d | 65 | then = rerere_created_at(e->d_name); |
9022a495 | 66 | if (!then) |
658f3650 | 67 | continue; |
8ca12c0d | 68 | cutoff = (has_resolution(e->d_name) |
9022a495 JH |
69 | ? cutoff_resolve : cutoff_noresolve); |
70 | if (then < now - cutoff * 86400) | |
8ca12c0d | 71 | string_list_append(e->d_name, &to_remove); |
658f3650 JS |
72 | } |
73 | for (i = 0; i < to_remove.nr; i++) | |
c455c87c JS |
74 | unlink_rr_item(to_remove.items[i].string); |
75 | string_list_clear(&to_remove, 0); | |
658f3650 JS |
76 | } |
77 | ||
78 | static int outf(void *dummy, mmbuffer_t *ptr, int nbuf) | |
79 | { | |
80 | int i; | |
81 | for (i = 0; i < nbuf; i++) | |
93822c22 AW |
82 | if (write_in_full(1, ptr[i].ptr, ptr[i].size) != ptr[i].size) |
83 | return -1; | |
658f3650 JS |
84 | return 0; |
85 | } | |
86 | ||
87 | static int diff_two(const char *file1, const char *label1, | |
88 | const char *file2, const char *label2) | |
89 | { | |
90 | xpparam_t xpp; | |
91 | xdemitconf_t xecfg; | |
92 | xdemitcb_t ecb; | |
93 | mmfile_t minus, plus; | |
94 | ||
95 | if (read_mmfile(&minus, file1) || read_mmfile(&plus, file2)) | |
96 | return 1; | |
97 | ||
98 | printf("--- a/%s\n+++ b/%s\n", label1, label2); | |
99 | fflush(stdout); | |
9ccd0a88 | 100 | memset(&xpp, 0, sizeof(xpp)); |
658f3650 | 101 | xpp.flags = XDF_NEED_MINIMAL; |
30b25010 | 102 | memset(&xecfg, 0, sizeof(xecfg)); |
658f3650 | 103 | xecfg.ctxlen = 3; |
658f3650 | 104 | ecb.outf = outf; |
c279d7e9 | 105 | xdi_diff(&minus, &plus, &xpp, &xecfg, &ecb); |
658f3650 JS |
106 | |
107 | free(minus.ptr); | |
108 | free(plus.ptr); | |
109 | return 0; | |
110 | } | |
111 | ||
d8b7db0a KH |
112 | int cmd_rerere(int argc, const char **argv, const char *prefix) |
113 | { | |
c455c87c | 114 | struct string_list merge_rr = { NULL, 0, 0, 1 }; |
d8b7db0a KH |
115 | int i, fd; |
116 | ||
5b2fd956 SB |
117 | if (argc < 2) |
118 | return rerere(); | |
119 | ||
d8b7db0a KH |
120 | fd = setup_rerere(&merge_rr); |
121 | if (fd < 0) | |
122 | return 0; | |
658f3650 | 123 | |
5b2fd956 | 124 | if (!strcmp(argv[1], "clear")) { |
658f3650 JS |
125 | for (i = 0; i < merge_rr.nr; i++) { |
126 | const char *name = (const char *)merge_rr.items[i].util; | |
9022a495 | 127 | if (!has_resolution(name)) |
658f3650 JS |
128 | unlink_rr_item(name); |
129 | } | |
5b2fd956 | 130 | unlink(git_path("rr-cache/MERGE_RR")); |
658f3650 JS |
131 | } else if (!strcmp(argv[1], "gc")) |
132 | garbage_collect(&merge_rr); | |
133 | else if (!strcmp(argv[1], "status")) | |
134 | for (i = 0; i < merge_rr.nr; i++) | |
c455c87c | 135 | printf("%s\n", merge_rr.items[i].string); |
658f3650 JS |
136 | else if (!strcmp(argv[1], "diff")) |
137 | for (i = 0; i < merge_rr.nr; i++) { | |
c455c87c | 138 | const char *path = merge_rr.items[i].string; |
658f3650 JS |
139 | const char *name = (const char *)merge_rr.items[i].util; |
140 | diff_two(rr_path(name, "preimage"), path, path, path); | |
141 | } | |
142 | else | |
143 | usage(git_rerere_usage); | |
144 | ||
c455c87c | 145 | string_list_clear(&merge_rr, 1); |
658f3650 JS |
146 | return 0; |
147 | } |