]> git.ipfire.org Git - thirdparty/git.git/blame - builtin/gc.c
maintenance: use launchctl on macOS
[thirdparty/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"
a80d72db 14#include "repository.h"
b2141fc1 15#include "config.h"
ebebeaea 16#include "tempfile.h"
697cc8ef 17#include "lockfile.h"
44c637c8 18#include "parse-options.h"
6757ada4 19#include "run-command.h"
4c5baf02 20#include "sigchain.h"
dbbcd44f 21#include "strvec.h"
eab3296c 22#include "commit.h"
d5d5d7b6 23#include "commit-graph.h"
0abe14f6 24#include "packfile.h"
a80d72db 25#include "object-store.h"
9806f5a7
NTND
26#include "pack.h"
27#include "pack-objects.h"
28#include "blob.h"
29#include "tree.h"
b14ed5ad 30#include "promisor-remote.h"
4ddc79b2 31#include "refs.h"
28cb5e66 32#include "remote.h"
e841a79a 33#include "object-store.h"
2fec604f 34#include "exec-cmd.h"
6757ada4
JB
35
36#define FAILED_RUN "failed to run %s"
37
44c637c8 38static const char * const builtin_gc_usage[] = {
9c9b4f2f 39 N_("git gc [<options>]"),
44c637c8
JB
40 NULL
41};
6757ada4 42
56752391 43static int pack_refs = 1;
62aad184 44static int prune_reflogs = 1;
07e7dbf0 45static int aggressive_depth = 50;
1c192f34 46static int aggressive_window = 250;
2c3c4399 47static int gc_auto_threshold = 6700;
97063974 48static int gc_auto_pack_limit = 50;
9f673f94 49static int detach_auto = 1;
dddbad72 50static timestamp_t gc_log_expire_time;
a831c06a 51static const char *gc_log_expire = "1.day.ago";
d3154b44 52static const char *prune_expire = "2.weeks.ago";
e3df33bb 53static const char *prune_worktrees_expire = "3.months.ago";
55dfe13d 54static unsigned long big_pack_threshold;
9806f5a7 55static unsigned long max_delta_cache_size = DEFAULT_DELTA_CACHE_SIZE;
6757ada4 56
22f9b7f3
JK
57static struct strvec pack_refs_cmd = STRVEC_INIT;
58static struct strvec reflog = STRVEC_INIT;
59static struct strvec repack = STRVEC_INIT;
60static struct strvec prune = STRVEC_INIT;
61static struct strvec prune_worktrees = STRVEC_INIT;
62static struct strvec rerere = STRVEC_INIT;
6757ada4 63
076aa2cb 64static struct tempfile *pidfile;
329e6e87 65static struct lock_file log_lock;
4c5baf02 66
478f34d2
DK
67static struct string_list pack_garbage = STRING_LIST_INIT_DUP;
68
69static void clean_pack_garbage(void)
70{
71 int i;
72 for (i = 0; i < pack_garbage.nr; i++)
73 unlink_or_warn(pack_garbage.items[i].string);
74 string_list_clear(&pack_garbage, 0);
75}
76
77static void report_pack_garbage(unsigned seen_bits, const char *path)
78{
79 if (seen_bits == PACKDIR_FILE_IDX)
80 string_list_append(&pack_garbage, path);
81}
82
329e6e87
NTND
83static void process_log_file(void)
84{
85 struct stat st;
a831c06a
DT
86 if (fstat(get_lock_file_fd(&log_lock), &st)) {
87 /*
88 * Perhaps there was an i/o error or another
89 * unlikely situation. Try to make a note of
90 * this in gc.log along with any existing
91 * messages.
92 */
93 int saved_errno = errno;
94 fprintf(stderr, _("Failed to fstat %s: %s"),
076aa2cb 95 get_tempfile_path(log_lock.tempfile),
a831c06a
DT
96 strerror(saved_errno));
97 fflush(stderr);
329e6e87 98 commit_lock_file(&log_lock);
a831c06a
DT
99 errno = saved_errno;
100 } else if (st.st_size) {
101 /* There was some error recorded in the lock file */
102 commit_lock_file(&log_lock);
103 } else {
104 /* No error, clean up any old gc.log */
105 unlink(git_path("gc.log"));
329e6e87 106 rollback_lock_file(&log_lock);
a831c06a 107 }
329e6e87
NTND
108}
109
110static void process_log_file_at_exit(void)
111{
112 fflush(stderr);
113 process_log_file();
114}
115
116static void process_log_file_on_signal(int signo)
117{
118 process_log_file();
119 sigchain_pop(signo);
120 raise(signo);
121}
122
bf3d70fe
ÆAB
123static int gc_config_is_timestamp_never(const char *var)
124{
125 const char *value;
126 timestamp_t expire;
127
128 if (!git_config_get_value(var, &value) && value) {
129 if (parse_expiry_date(value, &expire))
130 die(_("failed to parse '%s' value '%s'"), var, value);
131 return expire == 0;
132 }
133 return 0;
134}
135
5801d3b4 136static void gc_config(void)
6757ada4 137{
5801d3b4
TA
138 const char *value;
139
140 if (!git_config_get_value("gc.packrefs", &value)) {
c5e5a2c0 141 if (value && !strcmp(value, "notbare"))
6757ada4
JB
142 pack_refs = -1;
143 else
5801d3b4 144 pack_refs = git_config_bool("gc.packrefs", value);
17815501 145 }
5801d3b4 146
bf3d70fe
ÆAB
147 if (gc_config_is_timestamp_never("gc.reflogexpire") &&
148 gc_config_is_timestamp_never("gc.reflogexpireunreachable"))
149 prune_reflogs = 0;
150
5801d3b4
TA
151 git_config_get_int("gc.aggressivewindow", &aggressive_window);
152 git_config_get_int("gc.aggressivedepth", &aggressive_depth);
153 git_config_get_int("gc.auto", &gc_auto_threshold);
154 git_config_get_int("gc.autopacklimit", &gc_auto_pack_limit);
155 git_config_get_bool("gc.autodetach", &detach_auto);
77d67977
CC
156 git_config_get_expiry("gc.pruneexpire", &prune_expire);
157 git_config_get_expiry("gc.worktreepruneexpire", &prune_worktrees_expire);
94c9b5af 158 git_config_get_expiry("gc.logexpiry", &gc_log_expire);
a831c06a 159
55dfe13d 160 git_config_get_ulong("gc.bigpackthreshold", &big_pack_threshold);
9806f5a7 161 git_config_get_ulong("pack.deltacachesize", &max_delta_cache_size);
55dfe13d 162
5801d3b4 163 git_config(git_default_config, NULL);
6757ada4
JB
164}
165
a087cc98 166static int too_many_loose_objects(void)
2c3c4399
JH
167{
168 /*
169 * Quickly check if a "gc" is needed, by estimating how
170 * many loose objects there are. Because SHA-1 is evenly
171 * distributed, we can check only one and get a reasonable
172 * estimate.
173 */
2c3c4399
JH
174 DIR *dir;
175 struct dirent *ent;
176 int auto_threshold;
177 int num_loose = 0;
178 int needed = 0;
e5cdbd5f 179 const unsigned hexsz_loose = the_hash_algo->hexsz - 2;
2c3c4399 180
07af8891 181 dir = opendir(git_path("objects/17"));
2c3c4399
JH
182 if (!dir)
183 return 0;
184
42c78a21 185 auto_threshold = DIV_ROUND_UP(gc_auto_threshold, 256);
2c3c4399 186 while ((ent = readdir(dir)) != NULL) {
e5cdbd5f
ÆAB
187 if (strspn(ent->d_name, "0123456789abcdef") != hexsz_loose ||
188 ent->d_name[hexsz_loose] != '\0')
2c3c4399
JH
189 continue;
190 if (++num_loose > auto_threshold) {
191 needed = 1;
192 break;
193 }
194 }
195 closedir(dir);
196 return needed;
197}
198
9806f5a7
NTND
199static struct packed_git *find_base_packs(struct string_list *packs,
200 unsigned long limit)
ae4e89e5
NTND
201{
202 struct packed_git *p, *base = NULL;
203
454ea2e4 204 for (p = get_all_packs(the_repository); p; p = p->next) {
ae4e89e5
NTND
205 if (!p->pack_local)
206 continue;
55dfe13d
NTND
207 if (limit) {
208 if (p->pack_size >= limit)
209 string_list_append(packs, p->pack_name);
210 } else if (!base || base->pack_size < p->pack_size) {
ae4e89e5
NTND
211 base = p;
212 }
213 }
214
215 if (base)
216 string_list_append(packs, base->pack_name);
9806f5a7
NTND
217
218 return base;
ae4e89e5
NTND
219}
220
17815501
JH
221static int too_many_packs(void)
222{
223 struct packed_git *p;
224 int cnt;
225
226 if (gc_auto_pack_limit <= 0)
227 return 0;
228
454ea2e4 229 for (cnt = 0, p = get_all_packs(the_repository); p; p = p->next) {
17815501
JH
230 if (!p->pack_local)
231 continue;
01af249f 232 if (p->pack_keep)
17815501
JH
233 continue;
234 /*
235 * Perhaps check the size of the pack and count only
236 * very small ones here?
237 */
238 cnt++;
239 }
5f4e3bf5 240 return gc_auto_pack_limit < cnt;
17815501
JH
241}
242
9806f5a7
NTND
243static uint64_t total_ram(void)
244{
245#if defined(HAVE_SYSINFO)
246 struct sysinfo si;
247
248 if (!sysinfo(&si))
249 return si.totalram;
250#elif defined(HAVE_BSD_SYSCTL) && (defined(HW_MEMSIZE) || defined(HW_PHYSMEM))
251 int64_t physical_memory;
252 int mib[2];
253 size_t length;
254
255 mib[0] = CTL_HW;
256# if defined(HW_MEMSIZE)
257 mib[1] = HW_MEMSIZE;
258# else
259 mib[1] = HW_PHYSMEM;
260# endif
261 length = sizeof(int64_t);
262 if (!sysctl(mib, 2, &physical_memory, &length, NULL, 0))
263 return physical_memory;
264#elif defined(GIT_WINDOWS_NATIVE)
265 MEMORYSTATUSEX memInfo;
266
267 memInfo.dwLength = sizeof(MEMORYSTATUSEX);
268 if (GlobalMemoryStatusEx(&memInfo))
269 return memInfo.ullTotalPhys;
270#endif
271 return 0;
272}
273
274static uint64_t estimate_repack_memory(struct packed_git *pack)
275{
276 unsigned long nr_objects = approximate_object_count();
277 size_t os_cache, heap;
278
279 if (!pack || !nr_objects)
280 return 0;
281
282 /*
283 * First we have to scan through at least one pack.
284 * Assume enough room in OS file cache to keep the entire pack
285 * or we may accidentally evict data of other processes from
286 * the cache.
287 */
288 os_cache = pack->pack_size + pack->index_size;
289 /* then pack-objects needs lots more for book keeping */
290 heap = sizeof(struct object_entry) * nr_objects;
291 /*
292 * internal rev-list --all --objects takes up some memory too,
293 * let's say half of it is for blobs
294 */
295 heap += sizeof(struct blob) * nr_objects / 2;
296 /*
297 * and the other half is for trees (commits and tags are
298 * usually insignificant)
299 */
300 heap += sizeof(struct tree) * nr_objects / 2;
301 /* and then obj_hash[], underestimated in fact */
302 heap += sizeof(struct object *) * nr_objects;
303 /* revindex is used also */
304 heap += sizeof(struct revindex_entry) * nr_objects;
305 /*
306 * read_sha1_file() (either at delta calculation phase, or
307 * writing phase) also fills up the delta base cache
308 */
309 heap += delta_base_cache_limit;
310 /* and of course pack-objects has its own delta cache */
311 heap += max_delta_cache_size;
312
313 return os_cache + heap;
314}
315
ae4e89e5
NTND
316static int keep_one_pack(struct string_list_item *item, void *data)
317{
22f9b7f3 318 strvec_pushf(&repack, "--keep-pack=%s", basename(item->string));
ae4e89e5
NTND
319 return 0;
320}
321
322static void add_repack_all_option(struct string_list *keep_pack)
7e52f566
JK
323{
324 if (prune_expire && !strcmp(prune_expire, "now"))
22f9b7f3 325 strvec_push(&repack, "-a");
7e52f566 326 else {
22f9b7f3 327 strvec_push(&repack, "-A");
234587fc 328 if (prune_expire)
22f9b7f3 329 strvec_pushf(&repack, "--unpack-unreachable=%s", prune_expire);
7e52f566 330 }
ae4e89e5
NTND
331
332 if (keep_pack)
333 for_each_string_list(keep_pack, keep_one_pack, NULL);
7e52f566
JK
334}
335
bdf56de8
DT
336static void add_repack_incremental_option(void)
337{
22f9b7f3 338 strvec_push(&repack, "--no-write-bitmap-index");
bdf56de8
DT
339}
340
a087cc98
JH
341static int need_to_gc(void)
342{
343 /*
b14d255b
BC
344 * Setting gc.auto to 0 or negative can disable the
345 * automatic gc.
a087cc98 346 */
b14d255b 347 if (gc_auto_threshold <= 0)
95143f9e
JH
348 return 0;
349
17815501
JH
350 /*
351 * If there are too many loose objects, but not too many
352 * packs, we run "repack -d -l". If there are too many packs,
353 * we run "repack -A -d -l". Otherwise we tell the caller
354 * there is no need.
355 */
55dfe13d
NTND
356 if (too_many_packs()) {
357 struct string_list keep_pack = STRING_LIST_INIT_NODUP;
358
8fc67762 359 if (big_pack_threshold) {
55dfe13d 360 find_base_packs(&keep_pack, big_pack_threshold);
8fc67762
NTND
361 if (keep_pack.nr >= gc_auto_pack_limit) {
362 big_pack_threshold = 0;
363 string_list_clear(&keep_pack, 0);
364 find_base_packs(&keep_pack, 0);
365 }
9806f5a7
NTND
366 } else {
367 struct packed_git *p = find_base_packs(&keep_pack, 0);
368 uint64_t mem_have, mem_want;
369
370 mem_have = total_ram();
371 mem_want = estimate_repack_memory(p);
372
373 /*
374 * Only allow 1/2 of memory for pack-objects, leave
375 * the rest for the OS and other processes in the
376 * system.
377 */
378 if (!mem_have || mem_want < mem_have / 2)
379 string_list_clear(&keep_pack, 0);
8fc67762 380 }
55dfe13d
NTND
381
382 add_repack_all_option(&keep_pack);
383 string_list_clear(&keep_pack, 0);
384 } else if (too_many_loose_objects())
bdf56de8
DT
385 add_repack_incremental_option();
386 else
17815501 387 return 0;
bde30540 388
15048f8a 389 if (run_hook_le(NULL, "pre-auto-gc", NULL))
bde30540 390 return 0;
95143f9e 391 return 1;
a087cc98
JH
392}
393
64a99eb4
NTND
394/* return NULL on success, else hostname running the gc */
395static const char *lock_repo_for_gc(int force, pid_t* ret_pid)
396{
b2275868 397 struct lock_file lock = LOCK_INIT;
da25bdb7 398 char my_host[HOST_NAME_MAX + 1];
64a99eb4
NTND
399 struct strbuf sb = STRBUF_INIT;
400 struct stat st;
401 uintmax_t pid;
402 FILE *fp;
4f1c0b21 403 int fd;
00539cef 404 char *pidfile_path;
64a99eb4 405
076aa2cb 406 if (is_tempfile_active(pidfile))
4c5baf02
JN
407 /* already locked */
408 return NULL;
409
5781a9a2 410 if (xgethostname(my_host, sizeof(my_host)))
5096d490 411 xsnprintf(my_host, sizeof(my_host), "unknown");
64a99eb4 412
00539cef
MH
413 pidfile_path = git_pathdup("gc.pid");
414 fd = hold_lock_file_for_update(&lock, pidfile_path,
64a99eb4
NTND
415 LOCK_DIE_ON_ERROR);
416 if (!force) {
da25bdb7
RS
417 static char locking_host[HOST_NAME_MAX + 1];
418 static char *scan_fmt;
4f1c0b21 419 int should_exit;
da25bdb7
RS
420
421 if (!scan_fmt)
afe2fab7 422 scan_fmt = xstrfmt("%s %%%ds", "%"SCNuMAX, HOST_NAME_MAX);
00539cef 423 fp = fopen(pidfile_path, "r");
64a99eb4
NTND
424 memset(locking_host, 0, sizeof(locking_host));
425 should_exit =
426 fp != NULL &&
427 !fstat(fileno(fp), &st) &&
428 /*
429 * 12 hour limit is very generous as gc should
430 * never take that long. On the other hand we
431 * don't really need a strict limit here,
432 * running gc --auto one day late is not a big
433 * problem. --force can be used in manual gc
434 * after the user verifies that no gc is
435 * running.
436 */
437 time(NULL) - st.st_mtime <= 12 * 3600 &&
da25bdb7 438 fscanf(fp, scan_fmt, &pid, locking_host) == 2 &&
64a99eb4 439 /* be gentle to concurrent "gc" on remote hosts */
ed7eda8b 440 (strcmp(locking_host, my_host) || !kill(pid, 0) || errno == EPERM);
64a99eb4
NTND
441 if (fp != NULL)
442 fclose(fp);
443 if (should_exit) {
444 if (fd >= 0)
445 rollback_lock_file(&lock);
446 *ret_pid = pid;
00539cef 447 free(pidfile_path);
64a99eb4
NTND
448 return locking_host;
449 }
450 }
451
452 strbuf_addf(&sb, "%"PRIuMAX" %s",
453 (uintmax_t) getpid(), my_host);
454 write_in_full(fd, sb.buf, sb.len);
455 strbuf_release(&sb);
456 commit_lock_file(&lock);
076aa2cb 457 pidfile = register_tempfile(pidfile_path);
ebebeaea 458 free(pidfile_path);
64a99eb4
NTND
459 return NULL;
460}
461
30299702
JN
462/*
463 * Returns 0 if there was no previous error and gc can proceed, 1 if
464 * gc should not proceed due to an error in the last run. Prints a
15beaaa3 465 * message and returns -1 if an error occurred while reading gc.log
30299702
JN
466 */
467static int report_last_gc_error(void)
329e6e87
NTND
468{
469 struct strbuf sb = STRBUF_INIT;
30299702 470 int ret = 0;
3c426ecc 471 ssize_t len;
a831c06a
DT
472 struct stat st;
473 char *gc_log_path = git_pathdup("gc.log");
329e6e87 474
a831c06a
DT
475 if (stat(gc_log_path, &st)) {
476 if (errno == ENOENT)
477 goto done;
478
30299702
JN
479 ret = error_errno(_("cannot stat '%s'"), gc_log_path);
480 goto done;
a831c06a
DT
481 }
482
483 if (st.st_mtime < gc_log_expire_time)
484 goto done;
485
3c426ecc
JN
486 len = strbuf_read_file(&sb, gc_log_path, 0);
487 if (len < 0)
30299702
JN
488 ret = error_errno(_("cannot read '%s'"), gc_log_path);
489 else if (len > 0) {
490 /*
491 * A previous gc failed. Report the error, and don't
492 * bother with an automatic gc run since it is likely
493 * to fail in the same way.
494 */
495 warning(_("The last gc run reported the following. "
329e6e87
NTND
496 "Please correct the root cause\n"
497 "and remove %s.\n"
498 "Automatic cleanup will not be performed "
499 "until the file is removed.\n\n"
500 "%s"),
a831c06a 501 gc_log_path, sb.buf);
30299702
JN
502 ret = 1;
503 }
329e6e87 504 strbuf_release(&sb);
a831c06a
DT
505done:
506 free(gc_log_path);
30299702 507 return ret;
329e6e87
NTND
508}
509
fec2ed21 510static void gc_before_repack(void)
62aad184 511{
cd8eb3a0
ÆAB
512 /*
513 * We may be called twice, as both the pre- and
514 * post-daemonized phases will call us, but running these
515 * commands more than once is pointless and wasteful.
516 */
517 static int done = 0;
518 if (done++)
519 return;
520
d70a9eb6
JK
521 if (pack_refs && run_command_v_opt(pack_refs_cmd.v, RUN_GIT_CMD))
522 die(FAILED_RUN, pack_refs_cmd.v[0]);
62aad184 523
d70a9eb6
JK
524 if (prune_reflogs && run_command_v_opt(reflog.v, RUN_GIT_CMD))
525 die(FAILED_RUN, reflog.v[0]);
62aad184
NTND
526}
527
6757ada4
JB
528int cmd_gc(int argc, const char **argv, const char *prefix)
529{
44c637c8 530 int aggressive = 0;
2c3c4399 531 int auto_gc = 0;
a0c14cbb 532 int quiet = 0;
64a99eb4
NTND
533 int force = 0;
534 const char *name;
535 pid_t pid;
329e6e87 536 int daemonized = 0;
ae4e89e5 537 int keep_base_pack = -1;
8ab5aa4b 538 timestamp_t dummy;
6757ada4 539
44c637c8 540 struct option builtin_gc_options[] = {
6705c162
NTND
541 OPT__QUIET(&quiet, N_("suppress progress reporting")),
542 { OPTION_STRING, 0, "prune", &prune_expire, N_("date"),
543 N_("prune unreferenced objects"),
58e9d9d4 544 PARSE_OPT_OPTARG, NULL, (intptr_t)prune_expire },
d5d09d47 545 OPT_BOOL(0, "aggressive", &aggressive, N_("be more thorough (increased runtime)")),
7e1eeaa4
NTND
546 OPT_BOOL_F(0, "auto", &auto_gc, N_("enable auto-gc mode"),
547 PARSE_OPT_NOCOMPLETE),
548 OPT_BOOL_F(0, "force", &force,
549 N_("force running gc even if there may be another gc running"),
550 PARSE_OPT_NOCOMPLETE),
ae4e89e5
NTND
551 OPT_BOOL(0, "keep-largest-pack", &keep_base_pack,
552 N_("repack all other packs except the largest pack")),
44c637c8
JB
553 OPT_END()
554 };
555
0c8151b6
NTND
556 if (argc == 2 && !strcmp(argv[1], "-h"))
557 usage_with_options(builtin_gc_usage, builtin_gc_options);
558
22f9b7f3
JK
559 strvec_pushl(&pack_refs_cmd, "pack-refs", "--all", "--prune", NULL);
560 strvec_pushl(&reflog, "reflog", "expire", "--all", NULL);
561 strvec_pushl(&repack, "repack", "-d", "-l", NULL);
562 strvec_pushl(&prune, "prune", "--expire", NULL);
563 strvec_pushl(&prune_worktrees, "worktree", "prune", "--expire", NULL);
564 strvec_pushl(&rerere, "rerere", "gc", NULL);
234587fc 565
a831c06a 566 /* default expiry time, overwritten in gc_config */
5801d3b4 567 gc_config();
a831c06a 568 if (parse_expiry_date(gc_log_expire, &gc_log_expire_time))
96913c9d 569 die(_("failed to parse gc.logexpiry value %s"), gc_log_expire);
6757ada4
JB
570
571 if (pack_refs < 0)
572 pack_refs = !is_bare_repository();
573
37782920
SB
574 argc = parse_options(argc, argv, prefix, builtin_gc_options,
575 builtin_gc_usage, 0);
44c637c8
JB
576 if (argc > 0)
577 usage_with_options(builtin_gc_usage, builtin_gc_options);
578
8ab5aa4b
JH
579 if (prune_expire && parse_expiry_date(prune_expire, &dummy))
580 die(_("failed to parse prune expiry value %s"), prune_expire);
581
44c637c8 582 if (aggressive) {
22f9b7f3 583 strvec_push(&repack, "-f");
125f8146 584 if (aggressive_depth > 0)
22f9b7f3 585 strvec_pushf(&repack, "--depth=%d", aggressive_depth);
234587fc 586 if (aggressive_window > 0)
22f9b7f3 587 strvec_pushf(&repack, "--window=%d", aggressive_window);
6757ada4 588 }
a0c14cbb 589 if (quiet)
22f9b7f3 590 strvec_push(&repack, "-q");
6757ada4 591
2c3c4399
JH
592 if (auto_gc) {
593 /*
594 * Auto-gc should be least intrusive as possible.
595 */
2c3c4399
JH
596 if (!need_to_gc())
597 return 0;
9f673f94
NTND
598 if (!quiet) {
599 if (detach_auto)
600 fprintf(stderr, _("Auto packing the repository in background for optimum performance.\n"));
601 else
602 fprintf(stderr, _("Auto packing the repository for optimum performance.\n"));
603 fprintf(stderr, _("See \"git help gc\" for manual housekeeping.\n"));
604 }
62aad184 605 if (detach_auto) {
30299702
JN
606 int ret = report_last_gc_error();
607 if (ret < 0)
15beaaa3 608 /* an I/O error occurred, already reported */
30299702
JN
609 exit(128);
610 if (ret == 1)
611 /* Last gc --auto failed. Skip this one. */
612 return 0;
329e6e87 613
c45af94d
JK
614 if (lock_repo_for_gc(force, &pid))
615 return 0;
fec2ed21 616 gc_before_repack(); /* dies on failure */
c45af94d
JK
617 delete_tempfile(&pidfile);
618
9f673f94
NTND
619 /*
620 * failure to daemonize is ok, we'll continue
621 * in foreground
622 */
329e6e87 623 daemonized = !daemonize();
62aad184 624 }
ae4e89e5
NTND
625 } else {
626 struct string_list keep_pack = STRING_LIST_INIT_NODUP;
627
628 if (keep_base_pack != -1) {
629 if (keep_base_pack)
55dfe13d
NTND
630 find_base_packs(&keep_pack, 0);
631 } else if (big_pack_threshold) {
632 find_base_packs(&keep_pack, big_pack_threshold);
ae4e89e5
NTND
633 }
634
635 add_repack_all_option(&keep_pack);
636 string_list_clear(&keep_pack, 0);
637 }
2c3c4399 638
64a99eb4
NTND
639 name = lock_repo_for_gc(force, &pid);
640 if (name) {
641 if (auto_gc)
642 return 0; /* be quiet on --auto */
643 die(_("gc is already running on machine '%s' pid %"PRIuMAX" (use --force if not)"),
644 name, (uintmax_t)pid);
645 }
646
329e6e87
NTND
647 if (daemonized) {
648 hold_lock_file_for_update(&log_lock,
649 git_path("gc.log"),
650 LOCK_DIE_ON_ERROR);
076c8278 651 dup2(get_lock_file_fd(&log_lock), 2);
329e6e87
NTND
652 sigchain_push_common(process_log_file_on_signal);
653 atexit(process_log_file_at_exit);
654 }
655
fec2ed21 656 gc_before_repack();
6757ada4 657
067fbd41 658 if (!repository_format_precious_objects) {
2d511cfc 659 close_object_store(the_repository->objects);
d70a9eb6
JK
660 if (run_command_v_opt(repack.v, RUN_GIT_CMD))
661 die(FAILED_RUN, repack.v[0]);
067fbd41
JK
662
663 if (prune_expire) {
22f9b7f3 664 strvec_push(&prune, prune_expire);
067fbd41 665 if (quiet)
22f9b7f3 666 strvec_push(&prune, "--no-progress");
b14ed5ad 667 if (has_promisor_remote())
22f9b7f3 668 strvec_push(&prune,
f6d8942b 669 "--exclude-promisor-objects");
d70a9eb6
JK
670 if (run_command_v_opt(prune.v, RUN_GIT_CMD))
671 die(FAILED_RUN, prune.v[0]);
067fbd41 672 }
58e9d9d4 673 }
6757ada4 674
e3df33bb 675 if (prune_worktrees_expire) {
22f9b7f3 676 strvec_push(&prune_worktrees, prune_worktrees_expire);
d70a9eb6
JK
677 if (run_command_v_opt(prune_worktrees.v, RUN_GIT_CMD))
678 die(FAILED_RUN, prune_worktrees.v[0]);
e3df33bb
NTND
679 }
680
d70a9eb6
JK
681 if (run_command_v_opt(rerere.v, RUN_GIT_CMD))
682 die(FAILED_RUN, rerere.v[0]);
6757ada4 683
478f34d2 684 report_garbage = report_pack_garbage;
a49d2834 685 reprepare_packed_git(the_repository);
5bdece0d 686 if (pack_garbage.nr > 0) {
2d511cfc 687 close_object_store(the_repository->objects);
478f34d2 688 clean_pack_garbage();
5bdece0d 689 }
478f34d2 690
7211b9e7
DS
691 prepare_repo_settings(the_repository);
692 if (the_repository->settings.gc_write_commit_graph == 1)
0bd52e27 693 write_commit_graph_reachable(the_repository->objects->odb,
f4f8dfe1 694 !quiet && !daemonized ? COMMIT_GRAPH_WRITE_PROGRESS : 0,
7211b9e7 695 NULL);
d5d5d7b6 696
a087cc98 697 if (auto_gc && too_many_loose_objects())
fea6128b
ÆAB
698 warning(_("There are too many unreachable loose objects; "
699 "run 'git prune' to remove them."));
a087cc98 700
a831c06a
DT
701 if (!daemonized)
702 unlink(git_path("gc.log"));
703
6757ada4
JB
704 return 0;
705}
2057d750 706
b08ff1fe
DS
707static const char *const builtin_maintenance_run_usage[] = {
708 N_("git maintenance run [--auto] [--[no-]quiet] [--task=<task>] [--schedule]"),
2057d750
DS
709 NULL
710};
711
b08ff1fe
DS
712enum schedule_priority {
713 SCHEDULE_NONE = 0,
714 SCHEDULE_WEEKLY = 1,
715 SCHEDULE_DAILY = 2,
716 SCHEDULE_HOURLY = 3,
717};
718
719static enum schedule_priority parse_schedule(const char *value)
720{
721 if (!value)
722 return SCHEDULE_NONE;
723 if (!strcasecmp(value, "hourly"))
724 return SCHEDULE_HOURLY;
725 if (!strcasecmp(value, "daily"))
726 return SCHEDULE_DAILY;
727 if (!strcasecmp(value, "weekly"))
728 return SCHEDULE_WEEKLY;
729 return SCHEDULE_NONE;
730}
731
732static int maintenance_opt_schedule(const struct option *opt, const char *arg,
733 int unset)
734{
735 enum schedule_priority *priority = opt->value;
736
737 if (unset)
738 die(_("--no-schedule is not allowed"));
739
740 *priority = parse_schedule(arg);
741
742 if (!*priority)
743 die(_("unrecognized --schedule argument '%s'"), arg);
744
745 return 0;
746}
747
2057d750
DS
748struct maintenance_run_opts {
749 int auto_flag;
3ddaad0e 750 int quiet;
b08ff1fe 751 enum schedule_priority schedule;
2057d750
DS
752};
753
4ddc79b2
DS
754/* Remember to update object flag allocation in object.h */
755#define SEEN (1u<<0)
756
757struct cg_auto_data {
758 int num_not_in_graph;
759 int limit;
760};
761
762static int dfs_on_ref(const char *refname,
763 const struct object_id *oid, int flags,
764 void *cb_data)
765{
766 struct cg_auto_data *data = (struct cg_auto_data *)cb_data;
767 int result = 0;
768 struct object_id peeled;
769 struct commit_list *stack = NULL;
770 struct commit *commit;
771
772 if (!peel_ref(refname, &peeled))
773 oid = &peeled;
774 if (oid_object_info(the_repository, oid, NULL) != OBJ_COMMIT)
775 return 0;
776
777 commit = lookup_commit(the_repository, oid);
778 if (!commit)
779 return 0;
780 if (parse_commit(commit))
781 return 0;
782
783 commit_list_append(commit, &stack);
784
785 while (!result && stack) {
786 struct commit_list *parent;
787
788 commit = pop_commit(&stack);
789
790 for (parent = commit->parents; parent; parent = parent->next) {
791 if (parse_commit(parent->item) ||
792 commit_graph_position(parent->item) != COMMIT_NOT_FROM_GRAPH ||
793 parent->item->object.flags & SEEN)
794 continue;
795
796 parent->item->object.flags |= SEEN;
797 data->num_not_in_graph++;
798
799 if (data->num_not_in_graph >= data->limit) {
800 result = 1;
801 break;
802 }
803
804 commit_list_append(parent->item, &stack);
805 }
806 }
807
808 free_commit_list(stack);
809 return result;
810}
811
812static int should_write_commit_graph(void)
813{
814 int result;
815 struct cg_auto_data data;
816
817 data.num_not_in_graph = 0;
818 data.limit = 100;
819 git_config_get_int("maintenance.commit-graph.auto",
820 &data.limit);
821
822 if (!data.limit)
823 return 0;
824 if (data.limit < 0)
825 return 1;
826
827 result = for_each_ref(dfs_on_ref, &data);
828
829 clear_commit_marks_all(SEEN);
830
831 return result;
832}
833
663b2b1b
DS
834static int run_write_commit_graph(struct maintenance_run_opts *opts)
835{
836 struct child_process child = CHILD_PROCESS_INIT;
837
838 child.git_cmd = 1;
839 strvec_pushl(&child.args, "commit-graph", "write",
840 "--split", "--reachable", NULL);
841
842 if (opts->quiet)
843 strvec_push(&child.args, "--no-progress");
844
845 return !!run_command(&child);
846}
847
848static int maintenance_task_commit_graph(struct maintenance_run_opts *opts)
849{
850 close_object_store(the_repository->objects);
851 if (run_write_commit_graph(opts)) {
852 error(_("failed to write commit-graph"));
853 return 1;
854 }
855
856 return 0;
857}
858
28cb5e66
DS
859static int fetch_remote(const char *remote, struct maintenance_run_opts *opts)
860{
861 struct child_process child = CHILD_PROCESS_INIT;
862
863 child.git_cmd = 1;
864 strvec_pushl(&child.args, "fetch", remote, "--prune", "--no-tags",
865 "--no-write-fetch-head", "--recurse-submodules=no",
866 "--refmap=", NULL);
867
868 if (opts->quiet)
869 strvec_push(&child.args, "--quiet");
870
871 strvec_pushf(&child.args, "+refs/heads/*:refs/prefetch/%s/*", remote);
872
873 return !!run_command(&child);
874}
875
876static int append_remote(struct remote *remote, void *cbdata)
877{
878 struct string_list *remotes = (struct string_list *)cbdata;
879
880 string_list_append(remotes, remote->name);
881 return 0;
882}
883
884static int maintenance_task_prefetch(struct maintenance_run_opts *opts)
885{
886 int result = 0;
887 struct string_list_item *item;
888 struct string_list remotes = STRING_LIST_INIT_DUP;
889
890 if (for_each_remote(append_remote, &remotes)) {
891 error(_("failed to fill remotes"));
892 result = 1;
893 goto cleanup;
894 }
895
896 for_each_string_list_item(item, &remotes)
897 result |= fetch_remote(item->string, opts);
898
899cleanup:
900 string_list_clear(&remotes, 0);
901 return result;
902}
903
2057d750
DS
904static int maintenance_task_gc(struct maintenance_run_opts *opts)
905{
906 struct child_process child = CHILD_PROCESS_INIT;
907
908 child.git_cmd = 1;
909 strvec_push(&child.args, "gc");
910
911 if (opts->auto_flag)
912 strvec_push(&child.args, "--auto");
3ddaad0e
DS
913 if (opts->quiet)
914 strvec_push(&child.args, "--quiet");
915 else
916 strvec_push(&child.args, "--no-quiet");
2057d750
DS
917
918 close_object_store(the_repository->objects);
919 return run_command(&child);
920}
921
252cfb7c
DS
922static int prune_packed(struct maintenance_run_opts *opts)
923{
924 struct child_process child = CHILD_PROCESS_INIT;
925
926 child.git_cmd = 1;
927 strvec_push(&child.args, "prune-packed");
928
929 if (opts->quiet)
930 strvec_push(&child.args, "--quiet");
931
932 return !!run_command(&child);
933}
934
935struct write_loose_object_data {
936 FILE *in;
937 int count;
938 int batch_size;
939};
940
3e220e60
DS
941static int loose_object_auto_limit = 100;
942
943static int loose_object_count(const struct object_id *oid,
944 const char *path,
945 void *data)
946{
947 int *count = (int*)data;
948 if (++(*count) >= loose_object_auto_limit)
949 return 1;
950 return 0;
951}
952
953static int loose_object_auto_condition(void)
954{
955 int count = 0;
956
957 git_config_get_int("maintenance.loose-objects.auto",
958 &loose_object_auto_limit);
959
960 if (!loose_object_auto_limit)
961 return 0;
962 if (loose_object_auto_limit < 0)
963 return 1;
964
965 return for_each_loose_file_in_objdir(the_repository->objects->odb->path,
966 loose_object_count,
967 NULL, NULL, &count);
968}
969
252cfb7c
DS
970static int bail_on_loose(const struct object_id *oid,
971 const char *path,
972 void *data)
973{
974 return 1;
975}
976
977static int write_loose_object_to_stdin(const struct object_id *oid,
978 const char *path,
979 void *data)
980{
981 struct write_loose_object_data *d = (struct write_loose_object_data *)data;
982
983 fprintf(d->in, "%s\n", oid_to_hex(oid));
984
985 return ++(d->count) > d->batch_size;
986}
987
988static int pack_loose(struct maintenance_run_opts *opts)
989{
990 struct repository *r = the_repository;
991 int result = 0;
992 struct write_loose_object_data data;
993 struct child_process pack_proc = CHILD_PROCESS_INIT;
994
995 /*
996 * Do not start pack-objects process
997 * if there are no loose objects.
998 */
999 if (!for_each_loose_file_in_objdir(r->objects->odb->path,
1000 bail_on_loose,
1001 NULL, NULL, NULL))
1002 return 0;
1003
1004 pack_proc.git_cmd = 1;
1005
1006 strvec_push(&pack_proc.args, "pack-objects");
1007 if (opts->quiet)
1008 strvec_push(&pack_proc.args, "--quiet");
1009 strvec_pushf(&pack_proc.args, "%s/pack/loose", r->objects->odb->path);
1010
1011 pack_proc.in = -1;
1012
1013 if (start_command(&pack_proc)) {
1014 error(_("failed to start 'git pack-objects' process"));
1015 return 1;
1016 }
1017
1018 data.in = xfdopen(pack_proc.in, "w");
1019 data.count = 0;
1020 data.batch_size = 50000;
1021
1022 for_each_loose_file_in_objdir(r->objects->odb->path,
1023 write_loose_object_to_stdin,
1024 NULL,
1025 NULL,
1026 &data);
1027
1028 fclose(data.in);
1029
1030 if (finish_command(&pack_proc)) {
1031 error(_("failed to finish 'git pack-objects' process"));
1032 result = 1;
1033 }
1034
1035 return result;
1036}
1037
1038static int maintenance_task_loose_objects(struct maintenance_run_opts *opts)
1039{
1040 return prune_packed(opts) || pack_loose(opts);
1041}
1042
e841a79a
DS
1043static int incremental_repack_auto_condition(void)
1044{
1045 struct packed_git *p;
1046 int enabled;
1047 int incremental_repack_auto_limit = 10;
1048 int count = 0;
1049
1050 if (git_config_get_bool("core.multiPackIndex", &enabled) ||
1051 !enabled)
1052 return 0;
1053
1054 git_config_get_int("maintenance.incremental-repack.auto",
1055 &incremental_repack_auto_limit);
1056
1057 if (!incremental_repack_auto_limit)
1058 return 0;
1059 if (incremental_repack_auto_limit < 0)
1060 return 1;
1061
1062 for (p = get_packed_git(the_repository);
1063 count < incremental_repack_auto_limit && p;
1064 p = p->next) {
1065 if (!p->multi_pack_index)
1066 count++;
1067 }
1068
1069 return count >= incremental_repack_auto_limit;
1070}
1071
52fe41ff
DS
1072static int multi_pack_index_write(struct maintenance_run_opts *opts)
1073{
1074 struct child_process child = CHILD_PROCESS_INIT;
1075
1076 child.git_cmd = 1;
1077 strvec_pushl(&child.args, "multi-pack-index", "write", NULL);
1078
1079 if (opts->quiet)
1080 strvec_push(&child.args, "--no-progress");
1081
1082 if (run_command(&child))
1083 return error(_("failed to write multi-pack-index"));
1084
1085 return 0;
1086}
1087
1088static int multi_pack_index_expire(struct maintenance_run_opts *opts)
1089{
1090 struct child_process child = CHILD_PROCESS_INIT;
1091
1092 child.git_cmd = 1;
1093 strvec_pushl(&child.args, "multi-pack-index", "expire", NULL);
1094
1095 if (opts->quiet)
1096 strvec_push(&child.args, "--no-progress");
1097
1098 close_object_store(the_repository->objects);
1099
1100 if (run_command(&child))
1101 return error(_("'git multi-pack-index expire' failed"));
1102
1103 return 0;
1104}
1105
a13e3d0e
DS
1106#define TWO_GIGABYTES (INT32_MAX)
1107
1108static off_t get_auto_pack_size(void)
1109{
1110 /*
1111 * The "auto" value is special: we optimize for
1112 * one large pack-file (i.e. from a clone) and
1113 * expect the rest to be small and they can be
1114 * repacked quickly.
1115 *
1116 * The strategy we select here is to select a
1117 * size that is one more than the second largest
1118 * pack-file. This ensures that we will repack
1119 * at least two packs if there are three or more
1120 * packs.
1121 */
1122 off_t max_size = 0;
1123 off_t second_largest_size = 0;
1124 off_t result_size;
1125 struct packed_git *p;
1126 struct repository *r = the_repository;
1127
1128 reprepare_packed_git(r);
1129 for (p = get_all_packs(r); p; p = p->next) {
1130 if (p->pack_size > max_size) {
1131 second_largest_size = max_size;
1132 max_size = p->pack_size;
1133 } else if (p->pack_size > second_largest_size)
1134 second_largest_size = p->pack_size;
1135 }
1136
1137 result_size = second_largest_size + 1;
1138
1139 /* But limit ourselves to a batch size of 2g */
1140 if (result_size > TWO_GIGABYTES)
1141 result_size = TWO_GIGABYTES;
1142
1143 return result_size;
1144}
1145
52fe41ff
DS
1146static int multi_pack_index_repack(struct maintenance_run_opts *opts)
1147{
1148 struct child_process child = CHILD_PROCESS_INIT;
1149
1150 child.git_cmd = 1;
1151 strvec_pushl(&child.args, "multi-pack-index", "repack", NULL);
1152
1153 if (opts->quiet)
1154 strvec_push(&child.args, "--no-progress");
1155
a13e3d0e
DS
1156 strvec_pushf(&child.args, "--batch-size=%"PRIuMAX,
1157 (uintmax_t)get_auto_pack_size());
52fe41ff
DS
1158
1159 close_object_store(the_repository->objects);
1160
1161 if (run_command(&child))
1162 return error(_("'git multi-pack-index repack' failed"));
1163
1164 return 0;
1165}
1166
1167static int maintenance_task_incremental_repack(struct maintenance_run_opts *opts)
1168{
1169 prepare_repo_settings(the_repository);
1170 if (!the_repository->settings.core_multi_pack_index) {
1171 warning(_("skipping incremental-repack task because core.multiPackIndex is disabled"));
1172 return 0;
1173 }
1174
1175 if (multi_pack_index_write(opts))
1176 return 1;
1177 if (multi_pack_index_expire(opts))
1178 return 1;
1179 if (multi_pack_index_repack(opts))
1180 return 1;
1181 return 0;
1182}
1183
3103e984
DS
1184typedef int maintenance_task_fn(struct maintenance_run_opts *opts);
1185
916d0626
DS
1186/*
1187 * An auto condition function returns 1 if the task should run
1188 * and 0 if the task should NOT run. See needs_to_gc() for an
1189 * example.
1190 */
1191typedef int maintenance_auto_fn(void);
1192
3103e984
DS
1193struct maintenance_task {
1194 const char *name;
1195 maintenance_task_fn *fn;
916d0626 1196 maintenance_auto_fn *auto_condition;
3103e984 1197 unsigned enabled:1;
090511bc 1198
b08ff1fe
DS
1199 enum schedule_priority schedule;
1200
090511bc
DS
1201 /* -1 if not selected. */
1202 int selected_order;
3103e984
DS
1203};
1204
1205enum maintenance_task_label {
28cb5e66 1206 TASK_PREFETCH,
252cfb7c 1207 TASK_LOOSE_OBJECTS,
52fe41ff 1208 TASK_INCREMENTAL_REPACK,
3103e984 1209 TASK_GC,
663b2b1b 1210 TASK_COMMIT_GRAPH,
3103e984
DS
1211
1212 /* Leave as final value */
1213 TASK__COUNT
1214};
1215
1216static struct maintenance_task tasks[] = {
28cb5e66
DS
1217 [TASK_PREFETCH] = {
1218 "prefetch",
1219 maintenance_task_prefetch,
1220 },
252cfb7c
DS
1221 [TASK_LOOSE_OBJECTS] = {
1222 "loose-objects",
1223 maintenance_task_loose_objects,
3e220e60 1224 loose_object_auto_condition,
252cfb7c 1225 },
52fe41ff
DS
1226 [TASK_INCREMENTAL_REPACK] = {
1227 "incremental-repack",
1228 maintenance_task_incremental_repack,
e841a79a 1229 incremental_repack_auto_condition,
52fe41ff 1230 },
3103e984
DS
1231 [TASK_GC] = {
1232 "gc",
1233 maintenance_task_gc,
916d0626 1234 need_to_gc,
3103e984
DS
1235 1,
1236 },
663b2b1b
DS
1237 [TASK_COMMIT_GRAPH] = {
1238 "commit-graph",
1239 maintenance_task_commit_graph,
4ddc79b2 1240 should_write_commit_graph,
663b2b1b 1241 },
3103e984
DS
1242};
1243
090511bc
DS
1244static int compare_tasks_by_selection(const void *a_, const void *b_)
1245{
1246 const struct maintenance_task *a, *b;
1247
1248 a = (const struct maintenance_task *)&a_;
1249 b = (const struct maintenance_task *)&b_;
1250
1251 return b->selected_order - a->selected_order;
1252}
1253
3103e984
DS
1254static int maintenance_run_tasks(struct maintenance_run_opts *opts)
1255{
090511bc 1256 int i, found_selected = 0;
3103e984 1257 int result = 0;
d7514f6e
DS
1258 struct lock_file lk;
1259 struct repository *r = the_repository;
1260 char *lock_path = xstrfmt("%s/maintenance", r->objects->odb->path);
1261
1262 if (hold_lock_file_for_update(&lk, lock_path, LOCK_NO_DEREF) < 0) {
1263 /*
1264 * Another maintenance command is running.
1265 *
1266 * If --auto was provided, then it is likely due to a
1267 * recursive process stack. Do not report an error in
1268 * that case.
1269 */
1270 if (!opts->auto_flag && !opts->quiet)
1271 warning(_("lock file '%s' exists, skipping maintenance"),
1272 lock_path);
1273 free(lock_path);
1274 return 0;
1275 }
1276 free(lock_path);
3103e984 1277
090511bc
DS
1278 for (i = 0; !found_selected && i < TASK__COUNT; i++)
1279 found_selected = tasks[i].selected_order >= 0;
1280
1281 if (found_selected)
1282 QSORT(tasks, TASK__COUNT, compare_tasks_by_selection);
1283
3103e984 1284 for (i = 0; i < TASK__COUNT; i++) {
090511bc
DS
1285 if (found_selected && tasks[i].selected_order < 0)
1286 continue;
1287
1288 if (!found_selected && !tasks[i].enabled)
3103e984
DS
1289 continue;
1290
916d0626
DS
1291 if (opts->auto_flag &&
1292 (!tasks[i].auto_condition ||
1293 !tasks[i].auto_condition()))
1294 continue;
1295
b08ff1fe
DS
1296 if (opts->schedule && tasks[i].schedule < opts->schedule)
1297 continue;
1298
25914c4f 1299 trace2_region_enter("maintenance", tasks[i].name, r);
3103e984
DS
1300 if (tasks[i].fn(opts)) {
1301 error(_("task '%s' failed"), tasks[i].name);
1302 result = 1;
1303 }
25914c4f 1304 trace2_region_leave("maintenance", tasks[i].name, r);
3103e984
DS
1305 }
1306
d7514f6e 1307 rollback_lock_file(&lk);
3103e984
DS
1308 return result;
1309}
1310
a4cb1a23
DS
1311static void initialize_maintenance_strategy(void)
1312{
1313 char *config_str;
1314
1315 if (git_config_get_string("maintenance.strategy", &config_str))
1316 return;
1317
1318 if (!strcasecmp(config_str, "incremental")) {
1319 tasks[TASK_GC].schedule = SCHEDULE_NONE;
1320 tasks[TASK_COMMIT_GRAPH].enabled = 1;
1321 tasks[TASK_COMMIT_GRAPH].schedule = SCHEDULE_HOURLY;
1322 tasks[TASK_PREFETCH].enabled = 1;
1323 tasks[TASK_PREFETCH].schedule = SCHEDULE_HOURLY;
1324 tasks[TASK_INCREMENTAL_REPACK].enabled = 1;
1325 tasks[TASK_INCREMENTAL_REPACK].schedule = SCHEDULE_DAILY;
1326 tasks[TASK_LOOSE_OBJECTS].enabled = 1;
1327 tasks[TASK_LOOSE_OBJECTS].schedule = SCHEDULE_DAILY;
1328 }
1329}
1330
1331static void initialize_task_config(int schedule)
65d655b5
DS
1332{
1333 int i;
1334 struct strbuf config_name = STRBUF_INIT;
916d0626
DS
1335 gc_config();
1336
a4cb1a23
DS
1337 if (schedule)
1338 initialize_maintenance_strategy();
1339
65d655b5
DS
1340 for (i = 0; i < TASK__COUNT; i++) {
1341 int config_value;
b08ff1fe 1342 char *config_str;
65d655b5 1343
b08ff1fe 1344 strbuf_reset(&config_name);
65d655b5
DS
1345 strbuf_addf(&config_name, "maintenance.%s.enabled",
1346 tasks[i].name);
1347
1348 if (!git_config_get_bool(config_name.buf, &config_value))
1349 tasks[i].enabled = config_value;
b08ff1fe
DS
1350
1351 strbuf_reset(&config_name);
1352 strbuf_addf(&config_name, "maintenance.%s.schedule",
1353 tasks[i].name);
1354
1355 if (!git_config_get_string(config_name.buf, &config_str)) {
1356 tasks[i].schedule = parse_schedule(config_str);
1357 free(config_str);
1358 }
65d655b5
DS
1359 }
1360
1361 strbuf_release(&config_name);
1362}
1363
090511bc
DS
1364static int task_option_parse(const struct option *opt,
1365 const char *arg, int unset)
1366{
1367 int i, num_selected = 0;
1368 struct maintenance_task *task = NULL;
1369
1370 BUG_ON_OPT_NEG(unset);
1371
1372 for (i = 0; i < TASK__COUNT; i++) {
1373 if (tasks[i].selected_order >= 0)
1374 num_selected++;
1375 if (!strcasecmp(tasks[i].name, arg)) {
1376 task = &tasks[i];
1377 }
1378 }
1379
1380 if (!task) {
1381 error(_("'%s' is not a valid task"), arg);
1382 return 1;
1383 }
1384
1385 if (task->selected_order >= 0) {
1386 error(_("task '%s' cannot be selected multiple times"), arg);
1387 return 1;
1388 }
1389
1390 task->selected_order = num_selected + 1;
1391
1392 return 0;
1393}
1394
2057d750
DS
1395static int maintenance_run(int argc, const char **argv, const char *prefix)
1396{
090511bc 1397 int i;
2057d750
DS
1398 struct maintenance_run_opts opts;
1399 struct option builtin_maintenance_run_options[] = {
1400 OPT_BOOL(0, "auto", &opts.auto_flag,
1401 N_("run tasks based on the state of the repository")),
b08ff1fe
DS
1402 OPT_CALLBACK(0, "schedule", &opts.schedule, N_("frequency"),
1403 N_("run tasks based on frequency"),
1404 maintenance_opt_schedule),
3ddaad0e
DS
1405 OPT_BOOL(0, "quiet", &opts.quiet,
1406 N_("do not report progress or other information over stderr")),
090511bc
DS
1407 OPT_CALLBACK_F(0, "task", NULL, N_("task"),
1408 N_("run a specific task"),
1409 PARSE_OPT_NONEG, task_option_parse),
2057d750
DS
1410 OPT_END()
1411 };
1412 memset(&opts, 0, sizeof(opts));
1413
3ddaad0e
DS
1414 opts.quiet = !isatty(2);
1415
090511bc
DS
1416 for (i = 0; i < TASK__COUNT; i++)
1417 tasks[i].selected_order = -1;
1418
2057d750
DS
1419 argc = parse_options(argc, argv, prefix,
1420 builtin_maintenance_run_options,
1421 builtin_maintenance_run_usage,
1422 PARSE_OPT_STOP_AT_NON_OPTION);
1423
b08ff1fe
DS
1424 if (opts.auto_flag && opts.schedule)
1425 die(_("use at most one of --auto and --schedule=<frequency>"));
1426
a4cb1a23
DS
1427 initialize_task_config(opts.schedule);
1428
2057d750
DS
1429 if (argc != 0)
1430 usage_with_options(builtin_maintenance_run_usage,
1431 builtin_maintenance_run_options);
3103e984 1432 return maintenance_run_tasks(&opts);
2057d750
DS
1433}
1434
0c18b700
DS
1435static int maintenance_register(void)
1436{
61f7a383 1437 char *config_value;
0c18b700
DS
1438 struct child_process config_set = CHILD_PROCESS_INIT;
1439 struct child_process config_get = CHILD_PROCESS_INIT;
1440
1441 /* There is no current repository, so skip registering it */
1442 if (!the_repository || !the_repository->gitdir)
1443 return 0;
1444
61f7a383
DS
1445 /* Disable foreground maintenance */
1446 git_config_set("maintenance.auto", "false");
1447
1448 /* Set maintenance strategy, if unset */
1449 if (!git_config_get_string("maintenance.strategy", &config_value))
1450 free(config_value);
1451 else
1452 git_config_set("maintenance.strategy", "incremental");
1453
0c18b700
DS
1454 config_get.git_cmd = 1;
1455 strvec_pushl(&config_get.args, "config", "--global", "--get", "maintenance.repo",
1456 the_repository->worktree ? the_repository->worktree
1457 : the_repository->gitdir,
1458 NULL);
1459 config_get.out = -1;
1460
1461 if (start_command(&config_get))
1462 return error(_("failed to run 'git config'"));
1463
1464 /* We already have this value in our config! */
1465 if (!finish_command(&config_get))
1466 return 0;
1467
1468 config_set.git_cmd = 1;
1469 strvec_pushl(&config_set.args, "config", "--add", "--global", "maintenance.repo",
1470 the_repository->worktree ? the_repository->worktree
1471 : the_repository->gitdir,
1472 NULL);
1473
1474 return run_command(&config_set);
1475}
1476
1477static int maintenance_unregister(void)
1478{
1479 struct child_process config_unset = CHILD_PROCESS_INIT;
1480
1481 if (!the_repository || !the_repository->gitdir)
1482 return error(_("no current repository to unregister"));
1483
1484 config_unset.git_cmd = 1;
1485 strvec_pushl(&config_unset.args, "config", "--global", "--unset",
1486 "maintenance.repo",
1487 the_repository->worktree ? the_repository->worktree
1488 : the_repository->gitdir,
1489 NULL);
1490
1491 return run_command(&config_unset);
1492}
1493
2afe7e35
DS
1494static const char *get_frequency(enum schedule_priority schedule)
1495{
1496 switch (schedule) {
1497 case SCHEDULE_HOURLY:
1498 return "hourly";
1499 case SCHEDULE_DAILY:
1500 return "daily";
1501 case SCHEDULE_WEEKLY:
1502 return "weekly";
1503 default:
1504 BUG("invalid schedule %d", schedule);
1505 }
1506}
1507
1508static char *launchctl_service_name(const char *frequency)
1509{
1510 struct strbuf label = STRBUF_INIT;
1511 strbuf_addf(&label, "org.git-scm.git.%s", frequency);
1512 return strbuf_detach(&label, NULL);
1513}
1514
1515static char *launchctl_service_filename(const char *name)
1516{
1517 char *expanded;
1518 struct strbuf filename = STRBUF_INIT;
1519 strbuf_addf(&filename, "~/Library/LaunchAgents/%s.plist", name);
1520
1521 expanded = expand_user_path(filename.buf, 1);
1522 if (!expanded)
1523 die(_("failed to expand path '%s'"), filename.buf);
1524
1525 strbuf_release(&filename);
1526 return expanded;
1527}
1528
1529static char *launchctl_get_uid(void)
1530{
1531 return xstrfmt("gui/%d", getuid());
1532}
1533
1534static int launchctl_boot_plist(int enable, const char *filename, const char *cmd)
1535{
1536 int result;
1537 struct child_process child = CHILD_PROCESS_INIT;
1538 char *uid = launchctl_get_uid();
1539
1540 strvec_split(&child.args, cmd);
1541 if (enable)
1542 strvec_push(&child.args, "bootstrap");
1543 else
1544 strvec_push(&child.args, "bootout");
1545 strvec_push(&child.args, uid);
1546 strvec_push(&child.args, filename);
1547
1548 child.no_stderr = 1;
1549 child.no_stdout = 1;
1550
1551 if (start_command(&child))
1552 die(_("failed to start launchctl"));
1553
1554 result = finish_command(&child);
1555
1556 free(uid);
1557 return result;
1558}
1559
1560static int launchctl_remove_plist(enum schedule_priority schedule, const char *cmd)
1561{
1562 const char *frequency = get_frequency(schedule);
1563 char *name = launchctl_service_name(frequency);
1564 char *filename = launchctl_service_filename(name);
1565 int result = launchctl_boot_plist(0, filename, cmd);
1566 unlink(filename);
1567 free(filename);
1568 free(name);
1569 return result;
1570}
1571
1572static int launchctl_remove_plists(const char *cmd)
1573{
1574 return launchctl_remove_plist(SCHEDULE_HOURLY, cmd) ||
1575 launchctl_remove_plist(SCHEDULE_DAILY, cmd) ||
1576 launchctl_remove_plist(SCHEDULE_WEEKLY, cmd);
1577}
1578
1579static int launchctl_schedule_plist(const char *exec_path, enum schedule_priority schedule, const char *cmd)
1580{
1581 FILE *plist;
1582 int i;
1583 const char *preamble, *repeat;
1584 const char *frequency = get_frequency(schedule);
1585 char *name = launchctl_service_name(frequency);
1586 char *filename = launchctl_service_filename(name);
1587
1588 if (safe_create_leading_directories(filename))
1589 die(_("failed to create directories for '%s'"), filename);
1590 plist = xfopen(filename, "w");
1591
1592 preamble = "<?xml version=\"1.0\" encoding=\"US-ASCII\"?>\n"
1593 "<!DOCTYPE plist PUBLIC \"-//Apple//DTD PLIST 1.0//EN\" \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">\n"
1594 "<plist version=\"1.0\">"
1595 "<dict>\n"
1596 "<key>Label</key><string>%s</string>\n"
1597 "<key>ProgramArguments</key>\n"
1598 "<array>\n"
1599 "<string>%s/git</string>\n"
1600 "<string>--exec-path=%s</string>\n"
1601 "<string>for-each-repo</string>\n"
1602 "<string>--config=maintenance.repo</string>\n"
1603 "<string>maintenance</string>\n"
1604 "<string>run</string>\n"
1605 "<string>--schedule=%s</string>\n"
1606 "</array>\n"
1607 "<key>StartCalendarInterval</key>\n"
1608 "<array>\n";
1609 fprintf(plist, preamble, name, exec_path, exec_path, frequency);
1610
1611 switch (schedule) {
1612 case SCHEDULE_HOURLY:
1613 repeat = "<dict>\n"
1614 "<key>Hour</key><integer>%d</integer>\n"
1615 "<key>Minute</key><integer>0</integer>\n"
1616 "</dict>\n";
1617 for (i = 1; i <= 23; i++)
1618 fprintf(plist, repeat, i);
1619 break;
1620
1621 case SCHEDULE_DAILY:
1622 repeat = "<dict>\n"
1623 "<key>Day</key><integer>%d</integer>\n"
1624 "<key>Hour</key><integer>0</integer>\n"
1625 "<key>Minute</key><integer>0</integer>\n"
1626 "</dict>\n";
1627 for (i = 1; i <= 6; i++)
1628 fprintf(plist, repeat, i);
1629 break;
1630
1631 case SCHEDULE_WEEKLY:
1632 fprintf(plist,
1633 "<dict>\n"
1634 "<key>Day</key><integer>0</integer>\n"
1635 "<key>Hour</key><integer>0</integer>\n"
1636 "<key>Minute</key><integer>0</integer>\n"
1637 "</dict>\n");
1638 break;
1639
1640 default:
1641 /* unreachable */
1642 break;
1643 }
1644 fprintf(plist, "</array>\n</dict>\n</plist>\n");
1645 fclose(plist);
1646
1647 /* bootout might fail if not already running, so ignore */
1648 launchctl_boot_plist(0, filename, cmd);
1649 if (launchctl_boot_plist(1, filename, cmd))
1650 die(_("failed to bootstrap service %s"), filename);
1651
1652 free(filename);
1653 free(name);
1654 return 0;
1655}
1656
1657static int launchctl_add_plists(const char *cmd)
1658{
1659 const char *exec_path = git_exec_path();
1660
1661 return launchctl_schedule_plist(exec_path, SCHEDULE_HOURLY, cmd) ||
1662 launchctl_schedule_plist(exec_path, SCHEDULE_DAILY, cmd) ||
1663 launchctl_schedule_plist(exec_path, SCHEDULE_WEEKLY, cmd);
1664}
1665
1666static int launchctl_update_schedule(int run_maintenance, int fd, const char *cmd)
1667{
1668 if (run_maintenance)
1669 return launchctl_add_plists(cmd);
1670 else
1671 return launchctl_remove_plists(cmd);
1672}
1673
2fec604f
DS
1674#define BEGIN_LINE "# BEGIN GIT MAINTENANCE SCHEDULE"
1675#define END_LINE "# END GIT MAINTENANCE SCHEDULE"
1676
31345d55 1677static int crontab_update_schedule(int run_maintenance, int fd, const char *cmd)
2fec604f
DS
1678{
1679 int result = 0;
1680 int in_old_region = 0;
1681 struct child_process crontab_list = CHILD_PROCESS_INIT;
1682 struct child_process crontab_edit = CHILD_PROCESS_INIT;
1683 FILE *cron_list, *cron_in;
2fec604f 1684 struct strbuf line = STRBUF_INIT;
2fec604f 1685
31345d55 1686 strvec_split(&crontab_list.args, cmd);
2fec604f
DS
1687 strvec_push(&crontab_list.args, "-l");
1688 crontab_list.in = -1;
31345d55 1689 crontab_list.out = dup(fd);
2fec604f
DS
1690 crontab_list.git_cmd = 0;
1691
31345d55
DS
1692 if (start_command(&crontab_list))
1693 return error(_("failed to run 'crontab -l'; your system might not support 'cron'"));
2fec604f
DS
1694
1695 /* Ignore exit code, as an empty crontab will return error. */
1696 finish_command(&crontab_list);
1697
1698 /*
1699 * Read from the .lock file, filtering out the old
1700 * schedule while appending the new schedule.
1701 */
31345d55 1702 cron_list = fdopen(fd, "r");
2fec604f
DS
1703 rewind(cron_list);
1704
31345d55 1705 strvec_split(&crontab_edit.args, cmd);
2fec604f
DS
1706 crontab_edit.in = -1;
1707 crontab_edit.git_cmd = 0;
1708
31345d55
DS
1709 if (start_command(&crontab_edit))
1710 return error(_("failed to run 'crontab'; your system might not support 'cron'"));
2fec604f
DS
1711
1712 cron_in = fdopen(crontab_edit.in, "w");
1713 if (!cron_in) {
1714 result = error(_("failed to open stdin of 'crontab'"));
1715 goto done_editing;
1716 }
1717
1718 while (!strbuf_getline_lf(&line, cron_list)) {
1719 if (!in_old_region && !strcmp(line.buf, BEGIN_LINE))
1720 in_old_region = 1;
1721 if (in_old_region)
1722 continue;
1723 fprintf(cron_in, "%s\n", line.buf);
1724 if (in_old_region && !strcmp(line.buf, END_LINE))
1725 in_old_region = 0;
1726 }
1727
1728 if (run_maintenance) {
1729 struct strbuf line_format = STRBUF_INIT;
1730 const char *exec_path = git_exec_path();
1731
1732 fprintf(cron_in, "%s\n", BEGIN_LINE);
1733 fprintf(cron_in,
1734 "# The following schedule was created by Git\n");
1735 fprintf(cron_in, "# Any edits made in this region might be\n");
1736 fprintf(cron_in,
1737 "# replaced in the future by a Git command.\n\n");
1738
1739 strbuf_addf(&line_format,
1740 "%%s %%s * * %%s \"%s/git\" --exec-path=\"%s\" for-each-repo --config=maintenance.repo maintenance run --schedule=%%s\n",
1741 exec_path, exec_path);
1742 fprintf(cron_in, line_format.buf, "0", "1-23", "*", "hourly");
1743 fprintf(cron_in, line_format.buf, "0", "0", "1-6", "daily");
1744 fprintf(cron_in, line_format.buf, "0", "0", "0", "weekly");
1745 strbuf_release(&line_format);
1746
1747 fprintf(cron_in, "\n%s\n", END_LINE);
1748 }
1749
1750 fflush(cron_in);
1751 fclose(cron_in);
1752 close(crontab_edit.in);
1753
1754done_editing:
31345d55 1755 if (finish_command(&crontab_edit))
2fec604f 1756 result = error(_("'crontab' died"));
31345d55
DS
1757 else
1758 fclose(cron_list);
1759 return result;
1760}
1761
2afe7e35
DS
1762#if defined(__APPLE__)
1763static const char platform_scheduler[] = "launchctl";
1764#else
31345d55 1765static const char platform_scheduler[] = "crontab";
2afe7e35 1766#endif
31345d55
DS
1767
1768static int update_background_schedule(int enable)
1769{
1770 int result;
1771 const char *scheduler = platform_scheduler;
1772 const char *cmd = scheduler;
1773 char *testing;
1774 struct lock_file lk;
1775 char *lock_path = xstrfmt("%s/schedule", the_repository->objects->odb->path);
1776
1777 testing = xstrdup_or_null(getenv("GIT_TEST_MAINT_SCHEDULER"));
1778 if (testing) {
1779 char *sep = strchr(testing, ':');
1780 if (!sep)
1781 die("GIT_TEST_MAINT_SCHEDULER unparseable: %s", testing);
1782 *sep = '\0';
1783 scheduler = testing;
1784 cmd = sep + 1;
2fec604f 1785 }
2fec604f 1786
31345d55
DS
1787 if (hold_lock_file_for_update(&lk, lock_path, LOCK_NO_DEREF) < 0)
1788 return error(_("another process is scheduling background maintenance"));
1789
2afe7e35
DS
1790 if (!strcmp(scheduler, "launchctl"))
1791 result = launchctl_update_schedule(enable, lk.tempfile->fd, cmd);
1792 else if (!strcmp(scheduler, "crontab"))
31345d55
DS
1793 result = crontab_update_schedule(enable, lk.tempfile->fd, cmd);
1794 else
1795 die("unknown background scheduler: %s", scheduler);
1796
2fec604f 1797 rollback_lock_file(&lk);
31345d55 1798 free(testing);
2fec604f
DS
1799 return result;
1800}
1801
1802static int maintenance_start(void)
1803{
1804 if (maintenance_register())
1805 warning(_("failed to add repo to global config"));
1806
1807 return update_background_schedule(1);
1808}
1809
1810static int maintenance_stop(void)
1811{
1812 return update_background_schedule(0);
1813}
1814
0c18b700 1815static const char builtin_maintenance_usage[] = N_("git maintenance <subcommand> [<options>]");
2057d750
DS
1816
1817int cmd_maintenance(int argc, const char **argv, const char *prefix)
1818{
1819 if (argc < 2 ||
1820 (argc == 2 && !strcmp(argv[1], "-h")))
1821 usage(builtin_maintenance_usage);
1822
1823 if (!strcmp(argv[1], "run"))
1824 return maintenance_run(argc - 1, argv + 1, prefix);
2fec604f
DS
1825 if (!strcmp(argv[1], "start"))
1826 return maintenance_start();
1827 if (!strcmp(argv[1], "stop"))
1828 return maintenance_stop();
0c18b700
DS
1829 if (!strcmp(argv[1], "register"))
1830 return maintenance_register();
1831 if (!strcmp(argv[1], "unregister"))
1832 return maintenance_unregister();
2057d750
DS
1833
1834 die(_("invalid subcommand: %s"), argv[1]);
1835}