]> git.ipfire.org Git - thirdparty/git.git/blame - builtin/rerere.c
Merge branch 'tr/diff-words-test'
[thirdparty/git.git] / builtin / rerere.c
CommitLineData
baffc0e7 1#include "builtin.h"
658f3650 2#include "cache.h"
8ca12c0d 3#include "dir.h"
672d1b78 4#include "parse-options.h"
c455c87c 5#include "string-list.h"
5b2fd956 6#include "rerere.h"
658f3650
JS
7#include "xdiff/xdiff.h"
8#include "xdiff-interface.h"
9
672d1b78
JN
10static const char * const rerere_usage[] = {
11 "git rerere [clear | status | diff | gc]",
12 NULL,
13};
658f3650
JS
14
15/* these values are days */
16static int cutoff_noresolve = 15;
17static int cutoff_resolve = 60;
18
9022a495
JH
19static time_t rerere_created_at(const char *name)
20{
21 struct stat st;
90056966 22 return stat(rerere_path(name, "preimage"), &st) ? (time_t) 0 : st.st_mtime;
9022a495
JH
23}
24
7d7ff15b
SG
25static time_t rerere_last_used_at(const char *name)
26{
27 struct stat st;
28 return stat(rerere_path(name, "postimage"), &st) ? (time_t) 0 : st.st_mtime;
29}
30
658f3650
JS
31static void unlink_rr_item(const char *name)
32{
90056966
SG
33 unlink(rerere_path(name, "thisimage"));
34 unlink(rerere_path(name, "preimage"));
35 unlink(rerere_path(name, "postimage"));
658f3650
JS
36 rmdir(git_path("rr-cache/%s", name));
37}
38
5b2fd956
SB
39static int git_rerere_gc_config(const char *var, const char *value, void *cb)
40{
41 if (!strcmp(var, "gc.rerereresolved"))
42 cutoff_resolve = git_config_int(var, value);
43 else if (!strcmp(var, "gc.rerereunresolved"))
44 cutoff_noresolve = git_config_int(var, value);
45 else
46 return git_default_config(var, value, cb);
47 return 0;
48}
49
c455c87c 50static void garbage_collect(struct string_list *rr)
658f3650 51{
183113a5 52 struct string_list to_remove = STRING_LIST_INIT_DUP;
658f3650
JS
53 DIR *dir;
54 struct dirent *e;
9022a495 55 int i, cutoff;
658f3650
JS
56 time_t now = time(NULL), then;
57
5b2fd956 58 git_config(git_rerere_gc_config, NULL);
9022a495 59 dir = opendir(git_path("rr-cache"));
e5f59172
JK
60 if (!dir)
61 die_errno("unable to open rr-cache directory");
658f3650 62 while ((e = readdir(dir))) {
8ca12c0d 63 if (is_dot_or_dotdot(e->d_name))
658f3650 64 continue;
7d7ff15b
SG
65
66 then = rerere_last_used_at(e->d_name);
67 if (then) {
68 cutoff = cutoff_resolve;
69 } else {
70 then = rerere_created_at(e->d_name);
71 if (!then)
72 continue;
73 cutoff = cutoff_noresolve;
74 }
9022a495 75 if (then < now - cutoff * 86400)
1d2f80fa 76 string_list_append(&to_remove, e->d_name);
658f3650
JS
77 }
78 for (i = 0; i < to_remove.nr; i++)
c455c87c
JS
79 unlink_rr_item(to_remove.items[i].string);
80 string_list_clear(&to_remove, 0);
658f3650
JS
81}
82
83static int outf(void *dummy, mmbuffer_t *ptr, int nbuf)
84{
85 int i;
86 for (i = 0; i < nbuf; i++)
93822c22
AW
87 if (write_in_full(1, ptr[i].ptr, ptr[i].size) != ptr[i].size)
88 return -1;
658f3650
JS
89 return 0;
90}
91
92static int diff_two(const char *file1, const char *label1,
93 const char *file2, const char *label2)
94{
95 xpparam_t xpp;
96 xdemitconf_t xecfg;
97 xdemitcb_t ecb;
98 mmfile_t minus, plus;
99
100 if (read_mmfile(&minus, file1) || read_mmfile(&plus, file2))
101 return 1;
102
103 printf("--- a/%s\n+++ b/%s\n", label1, label2);
104 fflush(stdout);
9ccd0a88 105 memset(&xpp, 0, sizeof(xpp));
582aa00b 106 xpp.flags = 0;
30b25010 107 memset(&xecfg, 0, sizeof(xecfg));
658f3650 108 xecfg.ctxlen = 3;
658f3650 109 ecb.outf = outf;
c279d7e9 110 xdi_diff(&minus, &plus, &xpp, &xecfg, &ecb);
658f3650
JS
111
112 free(minus.ptr);
113 free(plus.ptr);
114 return 0;
115}
116
d8b7db0a
KH
117int cmd_rerere(int argc, const char **argv, const char *prefix)
118{
183113a5 119 struct string_list merge_rr = STRING_LIST_INIT_DUP;
672d1b78
JN
120 int i, fd, autoupdate = -1, flags = 0;
121
122 struct option options[] = {
123 OPT_SET_INT(0, "rerere-autoupdate", &autoupdate,
124 "register clean resolutions in index", 1),
125 OPT_END(),
126 };
127
128 argc = parse_options(argc, argv, prefix, options, rerere_usage, 0);
129
130 if (autoupdate == 1)
131 flags = RERERE_AUTOUPDATE;
132 if (autoupdate == 0)
133 flags = RERERE_NOAUTOUPDATE;
134
135 if (argc < 1)
cb6020bb 136 return rerere(flags);
99caeed0 137
672d1b78
JN
138 if (!strcmp(argv[0], "forget")) {
139 const char **pathspec = get_pathspec(prefix, argv + 1);
5a9f0395
JS
140 return rerere_forget(pathspec);
141 }
99caeed0 142
cb6020bb 143 fd = setup_rerere(&merge_rr, flags);
d8b7db0a
KH
144 if (fd < 0)
145 return 0;
658f3650 146
672d1b78 147 if (!strcmp(argv[0], "clear")) {
658f3650
JS
148 for (i = 0; i < merge_rr.nr; i++) {
149 const char *name = (const char *)merge_rr.items[i].util;
90056966 150 if (!has_rerere_resolution(name))
658f3650
JS
151 unlink_rr_item(name);
152 }
3ca399d4 153 unlink_or_warn(git_path("MERGE_RR"));
672d1b78 154 } else if (!strcmp(argv[0], "gc"))
658f3650 155 garbage_collect(&merge_rr);
672d1b78 156 else if (!strcmp(argv[0], "status"))
658f3650 157 for (i = 0; i < merge_rr.nr; i++)
c455c87c 158 printf("%s\n", merge_rr.items[i].string);
672d1b78 159 else if (!strcmp(argv[0], "diff"))
658f3650 160 for (i = 0; i < merge_rr.nr; i++) {
c455c87c 161 const char *path = merge_rr.items[i].string;
658f3650 162 const char *name = (const char *)merge_rr.items[i].util;
90056966 163 diff_two(rerere_path(name, "preimage"), path, path, path);
658f3650
JS
164 }
165 else
672d1b78 166 usage_with_options(rerere_usage, options);
658f3650 167
c455c87c 168 string_list_clear(&merge_rr, 1);
658f3650
JS
169 return 0;
170}