git-gc: by default use safer "-A" option to repack when not --prune'ing
[git/git.git] / builtin-gc.c
CommitLineData
6757ada4
JB
1/*
2 * git gc builtin command
3 *
4 * Cleanup unreachable files and optimize the repository.
5 *
6 * Copyright (c) 2007 James Bowes
7 *
8 * Based on git-gc.sh, which is
9 *
10 * Copyright (c) 2006 Shawn O. Pearce
11 */
12
baffc0e7 13#include "builtin.h"
6757ada4
JB
14#include "cache.h"
15#include "run-command.h"
16
17#define FAILED_RUN "failed to run %s"
18
0d7566a5 19static const char builtin_gc_usage[] = "git-gc [--prune] [--aggressive]";
6757ada4 20
56752391 21static int pack_refs = 1;
0d7566a5 22static int aggressive_window = -1;
2c3c4399 23static int gc_auto_threshold = 6700;
17815501 24static int gc_auto_pack_limit = 20;
6757ada4 25
0d7566a5 26#define MAX_ADD 10
56752391 27static const char *argv_pack_refs[] = {"pack-refs", "--all", "--prune", NULL};
6757ada4 28static const char *argv_reflog[] = {"reflog", "expire", "--all", NULL};
edb0e04e 29static const char *argv_repack[MAX_ADD] = {"repack", "-d", "-l", NULL};
6757ada4
JB
30static const char *argv_prune[] = {"prune", NULL};
31static const char *argv_rerere[] = {"rerere", "gc", NULL};
32
33static int gc_config(const char *var, const char *value)
34{
35 if (!strcmp(var, "gc.packrefs")) {
36 if (!strcmp(value, "notbare"))
37 pack_refs = -1;
38 else
39 pack_refs = git_config_bool(var, value);
40 return 0;
41 }
0d7566a5
TT
42 if (!strcmp(var, "gc.aggressivewindow")) {
43 aggressive_window = git_config_int(var, value);
44 return 0;
45 }
2c3c4399
JH
46 if (!strcmp(var, "gc.auto")) {
47 gc_auto_threshold = git_config_int(var, value);
48 return 0;
49 }
17815501
JH
50 if (!strcmp(var, "gc.autopacklimit")) {
51 gc_auto_pack_limit = git_config_int(var, value);
52 return 0;
53 }
6757ada4
JB
54 return git_default_config(var, value);
55}
56
0d7566a5
TT
57static void append_option(const char **cmd, const char *opt, int max_length)
58{
59 int i;
60
61 for (i = 0; cmd[i]; i++)
62 ;
63
64 if (i + 2 >= max_length)
65 die("Too many options specified");
66 cmd[i++] = opt;
67 cmd[i] = NULL;
68}
69
a087cc98 70static int too_many_loose_objects(void)
2c3c4399
JH
71{
72 /*
73 * Quickly check if a "gc" is needed, by estimating how
74 * many loose objects there are. Because SHA-1 is evenly
75 * distributed, we can check only one and get a reasonable
76 * estimate.
77 */
78 char path[PATH_MAX];
79 const char *objdir = get_object_directory();
80 DIR *dir;
81 struct dirent *ent;
82 int auto_threshold;
83 int num_loose = 0;
84 int needed = 0;
85
17815501
JH
86 if (gc_auto_threshold <= 0)
87 return 0;
88
2c3c4399
JH
89 if (sizeof(path) <= snprintf(path, sizeof(path), "%s/17", objdir)) {
90 warning("insanely long object directory %.*s", 50, objdir);
91 return 0;
92 }
93 dir = opendir(path);
94 if (!dir)
95 return 0;
96
97 auto_threshold = (gc_auto_threshold + 255) / 256;
98 while ((ent = readdir(dir)) != NULL) {
99 if (strspn(ent->d_name, "0123456789abcdef") != 38 ||
100 ent->d_name[38] != '\0')
101 continue;
102 if (++num_loose > auto_threshold) {
103 needed = 1;
104 break;
105 }
106 }
107 closedir(dir);
108 return needed;
109}
110
17815501
JH
111static int too_many_packs(void)
112{
113 struct packed_git *p;
114 int cnt;
115
116 if (gc_auto_pack_limit <= 0)
117 return 0;
118
119 prepare_packed_git();
120 for (cnt = 0, p = packed_git; p; p = p->next) {
121 char path[PATH_MAX];
122 size_t len;
123 int keep;
124
125 if (!p->pack_local)
126 continue;
127 len = strlen(p->pack_name);
128 if (PATH_MAX <= len + 1)
129 continue; /* oops, give up */
130 memcpy(path, p->pack_name, len-5);
131 memcpy(path + len - 5, ".keep", 6);
132 keep = access(p->pack_name, F_OK) && (errno == ENOENT);
133 if (keep)
134 continue;
135 /*
136 * Perhaps check the size of the pack and count only
137 * very small ones here?
138 */
139 cnt++;
140 }
141 return gc_auto_pack_limit <= cnt;
142}
143
a087cc98
JH
144static int need_to_gc(void)
145{
95143f9e
JH
146 int ac = 0;
147
a087cc98 148 /*
17815501
JH
149 * Setting gc.auto and gc.autopacklimit to 0 or negative can
150 * disable the automatic gc.
a087cc98 151 */
17815501 152 if (gc_auto_threshold <= 0 && gc_auto_pack_limit <= 0)
95143f9e
JH
153 return 0;
154
17815501
JH
155 /*
156 * If there are too many loose objects, but not too many
157 * packs, we run "repack -d -l". If there are too many packs,
158 * we run "repack -A -d -l". Otherwise we tell the caller
159 * there is no need.
160 */
95143f9e 161 argv_repack[ac++] = "repack";
17815501
JH
162 if (too_many_packs())
163 argv_repack[ac++] = "-A";
164 else if (!too_many_loose_objects())
165 return 0;
95143f9e
JH
166 argv_repack[ac++] = "-d";
167 argv_repack[ac++] = "-l";
168 argv_repack[ac++] = NULL;
169 return 1;
a087cc98
JH
170}
171
6757ada4
JB
172int cmd_gc(int argc, const char **argv, const char *prefix)
173{
174 int i;
175 int prune = 0;
2c3c4399 176 int auto_gc = 0;
0d7566a5 177 char buf[80];
6757ada4
JB
178
179 git_config(gc_config);
180
181 if (pack_refs < 0)
182 pack_refs = !is_bare_repository();
183
184 for (i = 1; i < argc; i++) {
185 const char *arg = argv[i];
186 if (!strcmp(arg, "--prune")) {
187 prune = 1;
188 continue;
189 }
0d7566a5
TT
190 if (!strcmp(arg, "--aggressive")) {
191 append_option(argv_repack, "-f", MAX_ADD);
192 if (aggressive_window > 0) {
193 sprintf(buf, "--window=%d", aggressive_window);
194 append_option(argv_repack, buf, MAX_ADD);
195 }
196 continue;
197 }
2c3c4399 198 if (!strcmp(arg, "--auto")) {
2c3c4399
JH
199 auto_gc = 1;
200 continue;
201 }
6757ada4
JB
202 break;
203 }
204 if (i != argc)
205 usage(builtin_gc_usage);
206
2c3c4399
JH
207 if (auto_gc) {
208 /*
209 * Auto-gc should be least intrusive as possible.
210 */
211 prune = 0;
2c3c4399
JH
212 if (!need_to_gc())
213 return 0;
edb0e04e
BC
214 } else {
215 /*
216 * Use safer (for shared repos) "-A" option to
217 * repack when not pruning. Auto-gc makes its
218 * own decision.
219 */
220 if (prune)
221 append_option(argv_repack, "-a", MAX_ADD);
222 else
223 append_option(argv_repack, "-A", MAX_ADD);
2c3c4399
JH
224 }
225
6757ada4
JB
226 if (pack_refs && run_command_v_opt(argv_pack_refs, RUN_GIT_CMD))
227 return error(FAILED_RUN, argv_pack_refs[0]);
228
229 if (run_command_v_opt(argv_reflog, RUN_GIT_CMD))
230 return error(FAILED_RUN, argv_reflog[0]);
231
232 if (run_command_v_opt(argv_repack, RUN_GIT_CMD))
233 return error(FAILED_RUN, argv_repack[0]);
234
235 if (prune && run_command_v_opt(argv_prune, RUN_GIT_CMD))
236 return error(FAILED_RUN, argv_prune[0]);
237
238 if (run_command_v_opt(argv_rerere, RUN_GIT_CMD))
239 return error(FAILED_RUN, argv_rerere[0]);
240
a087cc98
JH
241 if (auto_gc && too_many_loose_objects())
242 warning("There are too many unreachable loose objects; "
243 "run 'git prune' to remove them.");
244
6757ada4
JB
245 return 0;
246}