]> git.ipfire.org Git - thirdparty/git.git/blame - builtin-rerere.c
Documentation/git-archive: spell --worktree-attributes correctly
[thirdparty/git.git] / builtin-rerere.c
CommitLineData
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 9static const char git_rerere_usage[] =
1b1dd23f 10"git rerere [clear | status | diff | gc]";
658f3650
JS
11
12/* these values are days */
13static int cutoff_noresolve = 15;
14static int cutoff_resolve = 60;
15
9022a495
JH
16static time_t rerere_created_at(const char *name)
17{
18 struct stat st;
90056966 19 return stat(rerere_path(name, "preimage"), &st) ? (time_t) 0 : st.st_mtime;
9022a495
JH
20}
21
658f3650
JS
22static void unlink_rr_item(const char *name)
23{
90056966
SG
24 unlink(rerere_path(name, "thisimage"));
25 unlink(rerere_path(name, "preimage"));
26 unlink(rerere_path(name, "postimage"));
658f3650
JS
27 rmdir(git_path("rr-cache/%s", name));
28}
29
5b2fd956
SB
30static int git_rerere_gc_config(const char *var, const char *value, void *cb)
31{
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);
36 else
37 return git_default_config(var, value, cb);
38 return 0;
39}
40
c455c87c 41static void garbage_collect(struct string_list *rr)
658f3650 42{
c455c87c 43 struct string_list to_remove = { NULL, 0, 0, 1 };
658f3650
JS
44 DIR *dir;
45 struct dirent *e;
9022a495 46 int i, cutoff;
658f3650
JS
47 time_t now = time(NULL), then;
48
5b2fd956 49 git_config(git_rerere_gc_config, NULL);
9022a495 50 dir = opendir(git_path("rr-cache"));
e5f59172
JK
51 if (!dir)
52 die_errno("unable to open rr-cache directory");
658f3650 53 while ((e = readdir(dir))) {
8ca12c0d 54 if (is_dot_or_dotdot(e->d_name))
658f3650 55 continue;
8ca12c0d 56 then = rerere_created_at(e->d_name);
9022a495 57 if (!then)
658f3650 58 continue;
90056966 59 cutoff = (has_rerere_resolution(e->d_name)
9022a495
JH
60 ? cutoff_resolve : cutoff_noresolve);
61 if (then < now - cutoff * 86400)
8ca12c0d 62 string_list_append(e->d_name, &to_remove);
658f3650
JS
63 }
64 for (i = 0; i < to_remove.nr; i++)
c455c87c
JS
65 unlink_rr_item(to_remove.items[i].string);
66 string_list_clear(&to_remove, 0);
658f3650
JS
67}
68
69static int outf(void *dummy, mmbuffer_t *ptr, int nbuf)
70{
71 int i;
72 for (i = 0; i < nbuf; i++)
93822c22
AW
73 if (write_in_full(1, ptr[i].ptr, ptr[i].size) != ptr[i].size)
74 return -1;
658f3650
JS
75 return 0;
76}
77
78static int diff_two(const char *file1, const char *label1,
79 const char *file2, const char *label2)
80{
81 xpparam_t xpp;
82 xdemitconf_t xecfg;
83 xdemitcb_t ecb;
84 mmfile_t minus, plus;
85
86 if (read_mmfile(&minus, file1) || read_mmfile(&plus, file2))
87 return 1;
88
89 printf("--- a/%s\n+++ b/%s\n", label1, label2);
90 fflush(stdout);
9ccd0a88 91 memset(&xpp, 0, sizeof(xpp));
658f3650 92 xpp.flags = XDF_NEED_MINIMAL;
30b25010 93 memset(&xecfg, 0, sizeof(xecfg));
658f3650 94 xecfg.ctxlen = 3;
658f3650 95 ecb.outf = outf;
c279d7e9 96 xdi_diff(&minus, &plus, &xpp, &xecfg, &ecb);
658f3650
JS
97
98 free(minus.ptr);
99 free(plus.ptr);
100 return 0;
101}
102
d8b7db0a
KH
103int cmd_rerere(int argc, const char **argv, const char *prefix)
104{
c455c87c 105 struct string_list merge_rr = { NULL, 0, 0, 1 };
cb6020bb
JH
106 int i, fd, flags = 0;
107
108 if (2 < argc) {
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;
115 if (flags) {
116 argc--;
117 argv++;
118 }
119 }
5b2fd956 120 if (argc < 2)
cb6020bb 121 return rerere(flags);
99caeed0 122
5a9f0395
JS
123 if (!strcmp(argv[1], "forget")) {
124 const char **pathspec = get_pathspec(prefix, argv + 2);
125 return rerere_forget(pathspec);
126 }
99caeed0 127
cb6020bb 128 fd = setup_rerere(&merge_rr, flags);
d8b7db0a
KH
129 if (fd < 0)
130 return 0;
658f3650 131
5b2fd956 132 if (!strcmp(argv[1], "clear")) {
658f3650
JS
133 for (i = 0; i < merge_rr.nr; i++) {
134 const char *name = (const char *)merge_rr.items[i].util;
90056966 135 if (!has_rerere_resolution(name))
658f3650
JS
136 unlink_rr_item(name);
137 }
691f1a28 138 unlink_or_warn(git_path("rr-cache/MERGE_RR"));
658f3650
JS
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++)
c455c87c 143 printf("%s\n", merge_rr.items[i].string);
658f3650
JS
144 else if (!strcmp(argv[1], "diff"))
145 for (i = 0; i < merge_rr.nr; i++) {
c455c87c 146 const char *path = merge_rr.items[i].string;
658f3650 147 const char *name = (const char *)merge_rr.items[i].util;
90056966 148 diff_two(rerere_path(name, "preimage"), path, path, path);
658f3650
JS
149 }
150 else
151 usage(git_rerere_usage);
152
c455c87c 153 string_list_clear(&merge_rr, 1);
658f3650
JS
154 return 0;
155}