gc: factor out gc.pruneexpire parsing code
[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"
697cc8ef 14#include "lockfile.h"
44c637c8 15#include "parse-options.h"
6757ada4 16#include "run-command.h"
4c5baf02 17#include "sigchain.h"
234587fc 18#include "argv-array.h"
eab3296c 19#include "commit.h"
6757ada4
JB
20
21#define FAILED_RUN "failed to run %s"
22
44c637c8 23static const char * const builtin_gc_usage[] = {
6705c162 24 N_("git gc [options]"),
44c637c8
JB
25 NULL
26};
6757ada4 27
56752391 28static int pack_refs = 1;
62aad184 29static int prune_reflogs = 1;
125f8146 30static int aggressive_depth = 250;
1c192f34 31static int aggressive_window = 250;
2c3c4399 32static int gc_auto_threshold = 6700;
97063974 33static int gc_auto_pack_limit = 50;
9f673f94 34static int detach_auto = 1;
d3154b44 35static const char *prune_expire = "2.weeks.ago";
6757ada4 36
234587fc
JK
37static struct argv_array pack_refs_cmd = ARGV_ARRAY_INIT;
38static struct argv_array reflog = ARGV_ARRAY_INIT;
39static struct argv_array repack = ARGV_ARRAY_INIT;
40static struct argv_array prune = ARGV_ARRAY_INIT;
41static struct argv_array rerere = ARGV_ARRAY_INIT;
6757ada4 42
4c5baf02
JN
43static char *pidfile;
44
45static void remove_pidfile(void)
46{
47 if (pidfile)
48 unlink(pidfile);
49}
50
51static void remove_pidfile_on_signal(int signo)
52{
53 remove_pidfile();
54 sigchain_pop(signo);
55 raise(signo);
56}
57
09dbb90b
NTND
58static void git_config_date_string(const char *key, const char **output)
59{
60 if (git_config_get_string_const(key, output))
61 return;
62 if (strcmp(*output, "now")) {
63 unsigned long now = approxidate("now");
64 if (approxidate(*output) >= now)
65 git_die_config(key, _("Invalid %s: '%s'"), key, *output);
66 }
67}
68
5801d3b4 69static void gc_config(void)
6757ada4 70{
5801d3b4
TA
71 const char *value;
72
73 if (!git_config_get_value("gc.packrefs", &value)) {
c5e5a2c0 74 if (value && !strcmp(value, "notbare"))
6757ada4
JB
75 pack_refs = -1;
76 else
5801d3b4 77 pack_refs = git_config_bool("gc.packrefs", value);
17815501 78 }
5801d3b4
TA
79
80 git_config_get_int("gc.aggressivewindow", &aggressive_window);
81 git_config_get_int("gc.aggressivedepth", &aggressive_depth);
82 git_config_get_int("gc.auto", &gc_auto_threshold);
83 git_config_get_int("gc.autopacklimit", &gc_auto_pack_limit);
84 git_config_get_bool("gc.autodetach", &detach_auto);
09dbb90b 85 git_config_date_string("gc.pruneexpire", &prune_expire);
5801d3b4 86 git_config(git_default_config, NULL);
6757ada4
JB
87}
88
a087cc98 89static int too_many_loose_objects(void)
2c3c4399
JH
90{
91 /*
92 * Quickly check if a "gc" is needed, by estimating how
93 * many loose objects there are. Because SHA-1 is evenly
94 * distributed, we can check only one and get a reasonable
95 * estimate.
96 */
97 char path[PATH_MAX];
98 const char *objdir = get_object_directory();
99 DIR *dir;
100 struct dirent *ent;
101 int auto_threshold;
102 int num_loose = 0;
103 int needed = 0;
104
17815501
JH
105 if (gc_auto_threshold <= 0)
106 return 0;
107
2c3c4399 108 if (sizeof(path) <= snprintf(path, sizeof(path), "%s/17", objdir)) {
fea6128b 109 warning(_("insanely long object directory %.*s"), 50, objdir);
2c3c4399
JH
110 return 0;
111 }
112 dir = opendir(path);
113 if (!dir)
114 return 0;
115
116 auto_threshold = (gc_auto_threshold + 255) / 256;
117 while ((ent = readdir(dir)) != NULL) {
118 if (strspn(ent->d_name, "0123456789abcdef") != 38 ||
119 ent->d_name[38] != '\0')
120 continue;
121 if (++num_loose > auto_threshold) {
122 needed = 1;
123 break;
124 }
125 }
126 closedir(dir);
127 return needed;
128}
129
17815501
JH
130static int too_many_packs(void)
131{
132 struct packed_git *p;
133 int cnt;
134
135 if (gc_auto_pack_limit <= 0)
136 return 0;
137
138 prepare_packed_git();
139 for (cnt = 0, p = packed_git; p; p = p->next) {
17815501
JH
140 if (!p->pack_local)
141 continue;
01af249f 142 if (p->pack_keep)
17815501
JH
143 continue;
144 /*
145 * Perhaps check the size of the pack and count only
146 * very small ones here?
147 */
148 cnt++;
149 }
150 return gc_auto_pack_limit <= cnt;
151}
152
7e52f566
JK
153static void add_repack_all_option(void)
154{
155 if (prune_expire && !strcmp(prune_expire, "now"))
234587fc 156 argv_array_push(&repack, "-a");
7e52f566 157 else {
234587fc
JK
158 argv_array_push(&repack, "-A");
159 if (prune_expire)
160 argv_array_pushf(&repack, "--unpack-unreachable=%s", prune_expire);
7e52f566
JK
161 }
162}
163
a087cc98
JH
164static int need_to_gc(void)
165{
166 /*
b14d255b
BC
167 * Setting gc.auto to 0 or negative can disable the
168 * automatic gc.
a087cc98 169 */
b14d255b 170 if (gc_auto_threshold <= 0)
95143f9e
JH
171 return 0;
172
17815501
JH
173 /*
174 * If there are too many loose objects, but not too many
175 * packs, we run "repack -d -l". If there are too many packs,
176 * we run "repack -A -d -l". Otherwise we tell the caller
177 * there is no need.
178 */
17815501 179 if (too_many_packs())
7e52f566 180 add_repack_all_option();
17815501
JH
181 else if (!too_many_loose_objects())
182 return 0;
bde30540 183
15048f8a 184 if (run_hook_le(NULL, "pre-auto-gc", NULL))
bde30540 185 return 0;
95143f9e 186 return 1;
a087cc98
JH
187}
188
64a99eb4
NTND
189/* return NULL on success, else hostname running the gc */
190static const char *lock_repo_for_gc(int force, pid_t* ret_pid)
191{
192 static struct lock_file lock;
64a99eb4
NTND
193 char my_host[128];
194 struct strbuf sb = STRBUF_INIT;
195 struct stat st;
196 uintmax_t pid;
197 FILE *fp;
4f1c0b21 198 int fd;
64a99eb4 199
4c5baf02
JN
200 if (pidfile)
201 /* already locked */
202 return NULL;
203
64a99eb4
NTND
204 if (gethostname(my_host, sizeof(my_host)))
205 strcpy(my_host, "unknown");
206
207 fd = hold_lock_file_for_update(&lock, git_path("gc.pid"),
208 LOCK_DIE_ON_ERROR);
209 if (!force) {
4f1c0b21
EP
210 static char locking_host[128];
211 int should_exit;
64a99eb4
NTND
212 fp = fopen(git_path("gc.pid"), "r");
213 memset(locking_host, 0, sizeof(locking_host));
214 should_exit =
215 fp != NULL &&
216 !fstat(fileno(fp), &st) &&
217 /*
218 * 12 hour limit is very generous as gc should
219 * never take that long. On the other hand we
220 * don't really need a strict limit here,
221 * running gc --auto one day late is not a big
222 * problem. --force can be used in manual gc
223 * after the user verifies that no gc is
224 * running.
225 */
226 time(NULL) - st.st_mtime <= 12 * 3600 &&
227 fscanf(fp, "%"PRIuMAX" %127c", &pid, locking_host) == 2 &&
228 /* be gentle to concurrent "gc" on remote hosts */
ed7eda8b 229 (strcmp(locking_host, my_host) || !kill(pid, 0) || errno == EPERM);
64a99eb4
NTND
230 if (fp != NULL)
231 fclose(fp);
232 if (should_exit) {
233 if (fd >= 0)
234 rollback_lock_file(&lock);
235 *ret_pid = pid;
236 return locking_host;
237 }
238 }
239
240 strbuf_addf(&sb, "%"PRIuMAX" %s",
241 (uintmax_t) getpid(), my_host);
242 write_in_full(fd, sb.buf, sb.len);
243 strbuf_release(&sb);
244 commit_lock_file(&lock);
245
4c5baf02
JN
246 pidfile = git_pathdup("gc.pid");
247 sigchain_push_common(remove_pidfile_on_signal);
248 atexit(remove_pidfile);
249
64a99eb4
NTND
250 return NULL;
251}
252
62aad184
NTND
253static int gc_before_repack(void)
254{
255 if (pack_refs && run_command_v_opt(pack_refs_cmd.argv, RUN_GIT_CMD))
256 return error(FAILED_RUN, pack_refs_cmd.argv[0]);
257
258 if (prune_reflogs && run_command_v_opt(reflog.argv, RUN_GIT_CMD))
259 return error(FAILED_RUN, reflog.argv[0]);
260
261 pack_refs = 0;
262 prune_reflogs = 0;
263 return 0;
264}
265
6757ada4
JB
266int cmd_gc(int argc, const char **argv, const char *prefix)
267{
44c637c8 268 int aggressive = 0;
2c3c4399 269 int auto_gc = 0;
a0c14cbb 270 int quiet = 0;
64a99eb4
NTND
271 int force = 0;
272 const char *name;
273 pid_t pid;
6757ada4 274
44c637c8 275 struct option builtin_gc_options[] = {
6705c162
NTND
276 OPT__QUIET(&quiet, N_("suppress progress reporting")),
277 { OPTION_STRING, 0, "prune", &prune_expire, N_("date"),
278 N_("prune unreferenced objects"),
58e9d9d4 279 PARSE_OPT_OPTARG, NULL, (intptr_t)prune_expire },
d5d09d47
SB
280 OPT_BOOL(0, "aggressive", &aggressive, N_("be more thorough (increased runtime)")),
281 OPT_BOOL(0, "auto", &auto_gc, N_("enable auto-gc mode")),
64a99eb4 282 OPT_BOOL(0, "force", &force, N_("force running gc even if there may be another gc running")),
44c637c8
JB
283 OPT_END()
284 };
285
0c8151b6
NTND
286 if (argc == 2 && !strcmp(argv[1], "-h"))
287 usage_with_options(builtin_gc_usage, builtin_gc_options);
288
234587fc
JK
289 argv_array_pushl(&pack_refs_cmd, "pack-refs", "--all", "--prune", NULL);
290 argv_array_pushl(&reflog, "reflog", "expire", "--all", NULL);
291 argv_array_pushl(&repack, "repack", "-d", "-l", NULL);
2cfe2a78 292 argv_array_pushl(&prune, "prune", "--expire", NULL);
234587fc
JK
293 argv_array_pushl(&rerere, "rerere", "gc", NULL);
294
5801d3b4 295 gc_config();
6757ada4
JB
296
297 if (pack_refs < 0)
298 pack_refs = !is_bare_repository();
299
37782920
SB
300 argc = parse_options(argc, argv, prefix, builtin_gc_options,
301 builtin_gc_usage, 0);
44c637c8
JB
302 if (argc > 0)
303 usage_with_options(builtin_gc_usage, builtin_gc_options);
304
305 if (aggressive) {
234587fc 306 argv_array_push(&repack, "-f");
125f8146
NTND
307 if (aggressive_depth > 0)
308 argv_array_pushf(&repack, "--depth=%d", aggressive_depth);
234587fc
JK
309 if (aggressive_window > 0)
310 argv_array_pushf(&repack, "--window=%d", aggressive_window);
6757ada4 311 }
a0c14cbb 312 if (quiet)
234587fc 313 argv_array_push(&repack, "-q");
6757ada4 314
2c3c4399
JH
315 if (auto_gc) {
316 /*
317 * Auto-gc should be least intrusive as possible.
318 */
2c3c4399
JH
319 if (!need_to_gc())
320 return 0;
9f673f94
NTND
321 if (!quiet) {
322 if (detach_auto)
323 fprintf(stderr, _("Auto packing the repository in background for optimum performance.\n"));
324 else
325 fprintf(stderr, _("Auto packing the repository for optimum performance.\n"));
326 fprintf(stderr, _("See \"git help gc\" for manual housekeeping.\n"));
327 }
62aad184
NTND
328 if (detach_auto) {
329 if (gc_before_repack())
330 return -1;
9f673f94
NTND
331 /*
332 * failure to daemonize is ok, we'll continue
333 * in foreground
334 */
335 daemonize();
62aad184 336 }
a37cce3b 337 } else
7e52f566 338 add_repack_all_option();
2c3c4399 339
64a99eb4
NTND
340 name = lock_repo_for_gc(force, &pid);
341 if (name) {
342 if (auto_gc)
343 return 0; /* be quiet on --auto */
344 die(_("gc is already running on machine '%s' pid %"PRIuMAX" (use --force if not)"),
345 name, (uintmax_t)pid);
346 }
347
62aad184
NTND
348 if (gc_before_repack())
349 return -1;
6757ada4 350
234587fc
JK
351 if (run_command_v_opt(repack.argv, RUN_GIT_CMD))
352 return error(FAILED_RUN, repack.argv[0]);
6757ada4 353
58e9d9d4 354 if (prune_expire) {
234587fc 355 argv_array_push(&prune, prune_expire);
bf0a59b3 356 if (quiet)
234587fc
JK
357 argv_array_push(&prune, "--no-progress");
358 if (run_command_v_opt(prune.argv, RUN_GIT_CMD))
359 return error(FAILED_RUN, prune.argv[0]);
58e9d9d4 360 }
6757ada4 361
234587fc
JK
362 if (run_command_v_opt(rerere.argv, RUN_GIT_CMD))
363 return error(FAILED_RUN, rerere.argv[0]);
6757ada4 364
a087cc98 365 if (auto_gc && too_many_loose_objects())
fea6128b
ÆAB
366 warning(_("There are too many unreachable loose objects; "
367 "run 'git prune' to remove them."));
a087cc98 368
6757ada4
JB
369 return 0;
370}