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