Merge branch 'tb/enable-cruft-packs-by-default'
[git.git] / builtin / gc.c
blobc9f855733515b7eb13cbf1863fb4fb1eacd3fcd9
1 /*
2 * git gc builtin command
4 * Cleanup unreachable files and optimize the repository.
6 * Copyright (c) 2007 James Bowes
8 * Based on git-gc.sh, which is
10 * Copyright (c) 2006 Shawn O. Pearce
13 #include "builtin.h"
14 #include "abspath.h"
15 #include "environment.h"
16 #include "hex.h"
17 #include "repository.h"
18 #include "config.h"
19 #include "tempfile.h"
20 #include "lockfile.h"
21 #include "parse-options.h"
22 #include "run-command.h"
23 #include "sigchain.h"
24 #include "strvec.h"
25 #include "commit.h"
26 #include "commit-graph.h"
27 #include "packfile.h"
28 #include "object-file.h"
29 #include "object-store.h"
30 #include "pack.h"
31 #include "pack-objects.h"
32 #include "blob.h"
33 #include "tree.h"
34 #include "promisor-remote.h"
35 #include "refs.h"
36 #include "remote.h"
37 #include "exec-cmd.h"
38 #include "gettext.h"
39 #include "hook.h"
40 #include "setup.h"
41 #include "trace2.h"
42 #include "wrapper.h"
44 #define FAILED_RUN "failed to run %s"
46 static const char * const builtin_gc_usage[] = {
47 N_("git gc [<options>]"),
48 NULL
51 static int pack_refs = 1;
52 static int prune_reflogs = 1;
53 static int cruft_packs = 1;
54 static int aggressive_depth = 50;
55 static int aggressive_window = 250;
56 static int gc_auto_threshold = 6700;
57 static int gc_auto_pack_limit = 50;
58 static int detach_auto = 1;
59 static timestamp_t gc_log_expire_time;
60 static const char *gc_log_expire = "1.day.ago";
61 static const char *prune_expire = "2.weeks.ago";
62 static const char *prune_worktrees_expire = "3.months.ago";
63 static unsigned long big_pack_threshold;
64 static unsigned long max_delta_cache_size = DEFAULT_DELTA_CACHE_SIZE;
66 static struct strvec reflog = STRVEC_INIT;
67 static struct strvec repack = STRVEC_INIT;
68 static struct strvec prune = STRVEC_INIT;
69 static struct strvec prune_worktrees = STRVEC_INIT;
70 static struct strvec rerere = STRVEC_INIT;
72 static struct tempfile *pidfile;
73 static struct lock_file log_lock;
75 static struct string_list pack_garbage = STRING_LIST_INIT_DUP;
77 static void clean_pack_garbage(void)
79 int i;
80 for (i = 0; i < pack_garbage.nr; i++)
81 unlink_or_warn(pack_garbage.items[i].string);
82 string_list_clear(&pack_garbage, 0);
85 static void report_pack_garbage(unsigned seen_bits, const char *path)
87 if (seen_bits == PACKDIR_FILE_IDX)
88 string_list_append(&pack_garbage, path);
91 static void process_log_file(void)
93 struct stat st;
94 if (fstat(get_lock_file_fd(&log_lock), &st)) {
96 * Perhaps there was an i/o error or another
97 * unlikely situation. Try to make a note of
98 * this in gc.log along with any existing
99 * messages.
101 int saved_errno = errno;
102 fprintf(stderr, _("Failed to fstat %s: %s"),
103 get_lock_file_path(&log_lock),
104 strerror(saved_errno));
105 fflush(stderr);
106 commit_lock_file(&log_lock);
107 errno = saved_errno;
108 } else if (st.st_size) {
109 /* There was some error recorded in the lock file */
110 commit_lock_file(&log_lock);
111 } else {
112 /* No error, clean up any old gc.log */
113 unlink(git_path("gc.log"));
114 rollback_lock_file(&log_lock);
118 static void process_log_file_at_exit(void)
120 fflush(stderr);
121 process_log_file();
124 static void process_log_file_on_signal(int signo)
126 process_log_file();
127 sigchain_pop(signo);
128 raise(signo);
131 static int gc_config_is_timestamp_never(const char *var)
133 const char *value;
134 timestamp_t expire;
136 if (!git_config_get_value(var, &value) && value) {
137 if (parse_expiry_date(value, &expire))
138 die(_("failed to parse '%s' value '%s'"), var, value);
139 return expire == 0;
141 return 0;
144 static void gc_config(void)
146 const char *value;
148 if (!git_config_get_value("gc.packrefs", &value)) {
149 if (value && !strcmp(value, "notbare"))
150 pack_refs = -1;
151 else
152 pack_refs = git_config_bool("gc.packrefs", value);
155 if (gc_config_is_timestamp_never("gc.reflogexpire") &&
156 gc_config_is_timestamp_never("gc.reflogexpireunreachable"))
157 prune_reflogs = 0;
159 git_config_get_int("gc.aggressivewindow", &aggressive_window);
160 git_config_get_int("gc.aggressivedepth", &aggressive_depth);
161 git_config_get_int("gc.auto", &gc_auto_threshold);
162 git_config_get_int("gc.autopacklimit", &gc_auto_pack_limit);
163 git_config_get_bool("gc.autodetach", &detach_auto);
164 git_config_get_bool("gc.cruftpacks", &cruft_packs);
165 git_config_get_expiry("gc.pruneexpire", &prune_expire);
166 git_config_get_expiry("gc.worktreepruneexpire", &prune_worktrees_expire);
167 git_config_get_expiry("gc.logexpiry", &gc_log_expire);
169 git_config_get_ulong("gc.bigpackthreshold", &big_pack_threshold);
170 git_config_get_ulong("pack.deltacachesize", &max_delta_cache_size);
172 git_config(git_default_config, NULL);
175 struct maintenance_run_opts;
176 static int maintenance_task_pack_refs(MAYBE_UNUSED struct maintenance_run_opts *opts)
178 struct child_process cmd = CHILD_PROCESS_INIT;
180 cmd.git_cmd = 1;
181 strvec_pushl(&cmd.args, "pack-refs", "--all", "--prune", NULL);
182 return run_command(&cmd);
185 static int too_many_loose_objects(void)
188 * Quickly check if a "gc" is needed, by estimating how
189 * many loose objects there are. Because SHA-1 is evenly
190 * distributed, we can check only one and get a reasonable
191 * estimate.
193 DIR *dir;
194 struct dirent *ent;
195 int auto_threshold;
196 int num_loose = 0;
197 int needed = 0;
198 const unsigned hexsz_loose = the_hash_algo->hexsz - 2;
200 dir = opendir(git_path("objects/17"));
201 if (!dir)
202 return 0;
204 auto_threshold = DIV_ROUND_UP(gc_auto_threshold, 256);
205 while ((ent = readdir(dir)) != NULL) {
206 if (strspn(ent->d_name, "0123456789abcdef") != hexsz_loose ||
207 ent->d_name[hexsz_loose] != '\0')
208 continue;
209 if (++num_loose > auto_threshold) {
210 needed = 1;
211 break;
214 closedir(dir);
215 return needed;
218 static struct packed_git *find_base_packs(struct string_list *packs,
219 unsigned long limit)
221 struct packed_git *p, *base = NULL;
223 for (p = get_all_packs(the_repository); p; p = p->next) {
224 if (!p->pack_local || p->is_cruft)
225 continue;
226 if (limit) {
227 if (p->pack_size >= limit)
228 string_list_append(packs, p->pack_name);
229 } else if (!base || base->pack_size < p->pack_size) {
230 base = p;
234 if (base)
235 string_list_append(packs, base->pack_name);
237 return base;
240 static int too_many_packs(void)
242 struct packed_git *p;
243 int cnt;
245 if (gc_auto_pack_limit <= 0)
246 return 0;
248 for (cnt = 0, p = get_all_packs(the_repository); p; p = p->next) {
249 if (!p->pack_local)
250 continue;
251 if (p->pack_keep)
252 continue;
254 * Perhaps check the size of the pack and count only
255 * very small ones here?
257 cnt++;
259 return gc_auto_pack_limit < cnt;
262 static uint64_t total_ram(void)
264 #if defined(HAVE_SYSINFO)
265 struct sysinfo si;
267 if (!sysinfo(&si))
268 return si.totalram;
269 #elif defined(HAVE_BSD_SYSCTL) && (defined(HW_MEMSIZE) || defined(HW_PHYSMEM))
270 int64_t physical_memory;
271 int mib[2];
272 size_t length;
274 mib[0] = CTL_HW;
275 # if defined(HW_MEMSIZE)
276 mib[1] = HW_MEMSIZE;
277 # else
278 mib[1] = HW_PHYSMEM;
279 # endif
280 length = sizeof(int64_t);
281 if (!sysctl(mib, 2, &physical_memory, &length, NULL, 0))
282 return physical_memory;
283 #elif defined(GIT_WINDOWS_NATIVE)
284 MEMORYSTATUSEX memInfo;
286 memInfo.dwLength = sizeof(MEMORYSTATUSEX);
287 if (GlobalMemoryStatusEx(&memInfo))
288 return memInfo.ullTotalPhys;
289 #endif
290 return 0;
293 static uint64_t estimate_repack_memory(struct packed_git *pack)
295 unsigned long nr_objects = repo_approximate_object_count(the_repository);
296 size_t os_cache, heap;
298 if (!pack || !nr_objects)
299 return 0;
302 * First we have to scan through at least one pack.
303 * Assume enough room in OS file cache to keep the entire pack
304 * or we may accidentally evict data of other processes from
305 * the cache.
307 os_cache = pack->pack_size + pack->index_size;
308 /* then pack-objects needs lots more for book keeping */
309 heap = sizeof(struct object_entry) * nr_objects;
311 * internal rev-list --all --objects takes up some memory too,
312 * let's say half of it is for blobs
314 heap += sizeof(struct blob) * nr_objects / 2;
316 * and the other half is for trees (commits and tags are
317 * usually insignificant)
319 heap += sizeof(struct tree) * nr_objects / 2;
320 /* and then obj_hash[], underestimated in fact */
321 heap += sizeof(struct object *) * nr_objects;
322 /* revindex is used also */
323 heap += (sizeof(off_t) + sizeof(uint32_t)) * nr_objects;
325 * read_sha1_file() (either at delta calculation phase, or
326 * writing phase) also fills up the delta base cache
328 heap += delta_base_cache_limit;
329 /* and of course pack-objects has its own delta cache */
330 heap += max_delta_cache_size;
332 return os_cache + heap;
335 static int keep_one_pack(struct string_list_item *item, void *data UNUSED)
337 strvec_pushf(&repack, "--keep-pack=%s", basename(item->string));
338 return 0;
341 static void add_repack_all_option(struct string_list *keep_pack)
343 if (prune_expire && !strcmp(prune_expire, "now"))
344 strvec_push(&repack, "-a");
345 else if (cruft_packs) {
346 strvec_push(&repack, "--cruft");
347 if (prune_expire)
348 strvec_pushf(&repack, "--cruft-expiration=%s", prune_expire);
349 } else {
350 strvec_push(&repack, "-A");
351 if (prune_expire)
352 strvec_pushf(&repack, "--unpack-unreachable=%s", prune_expire);
355 if (keep_pack)
356 for_each_string_list(keep_pack, keep_one_pack, NULL);
359 static void add_repack_incremental_option(void)
361 strvec_push(&repack, "--no-write-bitmap-index");
364 static int need_to_gc(void)
367 * Setting gc.auto to 0 or negative can disable the
368 * automatic gc.
370 if (gc_auto_threshold <= 0)
371 return 0;
374 * If there are too many loose objects, but not too many
375 * packs, we run "repack -d -l". If there are too many packs,
376 * we run "repack -A -d -l". Otherwise we tell the caller
377 * there is no need.
379 if (too_many_packs()) {
380 struct string_list keep_pack = STRING_LIST_INIT_NODUP;
382 if (big_pack_threshold) {
383 find_base_packs(&keep_pack, big_pack_threshold);
384 if (keep_pack.nr >= gc_auto_pack_limit) {
385 big_pack_threshold = 0;
386 string_list_clear(&keep_pack, 0);
387 find_base_packs(&keep_pack, 0);
389 } else {
390 struct packed_git *p = find_base_packs(&keep_pack, 0);
391 uint64_t mem_have, mem_want;
393 mem_have = total_ram();
394 mem_want = estimate_repack_memory(p);
397 * Only allow 1/2 of memory for pack-objects, leave
398 * the rest for the OS and other processes in the
399 * system.
401 if (!mem_have || mem_want < mem_have / 2)
402 string_list_clear(&keep_pack, 0);
405 add_repack_all_option(&keep_pack);
406 string_list_clear(&keep_pack, 0);
407 } else if (too_many_loose_objects())
408 add_repack_incremental_option();
409 else
410 return 0;
412 if (run_hooks("pre-auto-gc"))
413 return 0;
414 return 1;
417 /* return NULL on success, else hostname running the gc */
418 static const char *lock_repo_for_gc(int force, pid_t* ret_pid)
420 struct lock_file lock = LOCK_INIT;
421 char my_host[HOST_NAME_MAX + 1];
422 struct strbuf sb = STRBUF_INIT;
423 struct stat st;
424 uintmax_t pid;
425 FILE *fp;
426 int fd;
427 char *pidfile_path;
429 if (is_tempfile_active(pidfile))
430 /* already locked */
431 return NULL;
433 if (xgethostname(my_host, sizeof(my_host)))
434 xsnprintf(my_host, sizeof(my_host), "unknown");
436 pidfile_path = git_pathdup("gc.pid");
437 fd = hold_lock_file_for_update(&lock, pidfile_path,
438 LOCK_DIE_ON_ERROR);
439 if (!force) {
440 static char locking_host[HOST_NAME_MAX + 1];
441 static char *scan_fmt;
442 int should_exit;
444 if (!scan_fmt)
445 scan_fmt = xstrfmt("%s %%%ds", "%"SCNuMAX, HOST_NAME_MAX);
446 fp = fopen(pidfile_path, "r");
447 memset(locking_host, 0, sizeof(locking_host));
448 should_exit =
449 fp != NULL &&
450 !fstat(fileno(fp), &st) &&
452 * 12 hour limit is very generous as gc should
453 * never take that long. On the other hand we
454 * don't really need a strict limit here,
455 * running gc --auto one day late is not a big
456 * problem. --force can be used in manual gc
457 * after the user verifies that no gc is
458 * running.
460 time(NULL) - st.st_mtime <= 12 * 3600 &&
461 fscanf(fp, scan_fmt, &pid, locking_host) == 2 &&
462 /* be gentle to concurrent "gc" on remote hosts */
463 (strcmp(locking_host, my_host) || !kill(pid, 0) || errno == EPERM);
464 if (fp)
465 fclose(fp);
466 if (should_exit) {
467 if (fd >= 0)
468 rollback_lock_file(&lock);
469 *ret_pid = pid;
470 free(pidfile_path);
471 return locking_host;
475 strbuf_addf(&sb, "%"PRIuMAX" %s",
476 (uintmax_t) getpid(), my_host);
477 write_in_full(fd, sb.buf, sb.len);
478 strbuf_release(&sb);
479 commit_lock_file(&lock);
480 pidfile = register_tempfile(pidfile_path);
481 free(pidfile_path);
482 return NULL;
486 * Returns 0 if there was no previous error and gc can proceed, 1 if
487 * gc should not proceed due to an error in the last run. Prints a
488 * message and returns with a non-[01] status code if an error occurred
489 * while reading gc.log
491 static int report_last_gc_error(void)
493 struct strbuf sb = STRBUF_INIT;
494 int ret = 0;
495 ssize_t len;
496 struct stat st;
497 char *gc_log_path = git_pathdup("gc.log");
499 if (stat(gc_log_path, &st)) {
500 if (errno == ENOENT)
501 goto done;
503 ret = die_message_errno(_("cannot stat '%s'"), gc_log_path);
504 goto done;
507 if (st.st_mtime < gc_log_expire_time)
508 goto done;
510 len = strbuf_read_file(&sb, gc_log_path, 0);
511 if (len < 0)
512 ret = die_message_errno(_("cannot read '%s'"), gc_log_path);
513 else if (len > 0) {
515 * A previous gc failed. Report the error, and don't
516 * bother with an automatic gc run since it is likely
517 * to fail in the same way.
519 warning(_("The last gc run reported the following. "
520 "Please correct the root cause\n"
521 "and remove %s\n"
522 "Automatic cleanup will not be performed "
523 "until the file is removed.\n\n"
524 "%s"),
525 gc_log_path, sb.buf);
526 ret = 1;
528 strbuf_release(&sb);
529 done:
530 free(gc_log_path);
531 return ret;
534 static void gc_before_repack(void)
537 * We may be called twice, as both the pre- and
538 * post-daemonized phases will call us, but running these
539 * commands more than once is pointless and wasteful.
541 static int done = 0;
542 if (done++)
543 return;
545 if (pack_refs && maintenance_task_pack_refs(NULL))
546 die(FAILED_RUN, "pack-refs");
548 if (prune_reflogs) {
549 struct child_process cmd = CHILD_PROCESS_INIT;
551 cmd.git_cmd = 1;
552 strvec_pushv(&cmd.args, reflog.v);
553 if (run_command(&cmd))
554 die(FAILED_RUN, reflog.v[0]);
558 int cmd_gc(int argc, const char **argv, const char *prefix)
560 int aggressive = 0;
561 int auto_gc = 0;
562 int quiet = 0;
563 int force = 0;
564 const char *name;
565 pid_t pid;
566 int daemonized = 0;
567 int keep_largest_pack = -1;
568 timestamp_t dummy;
569 struct child_process rerere_cmd = CHILD_PROCESS_INIT;
571 struct option builtin_gc_options[] = {
572 OPT__QUIET(&quiet, N_("suppress progress reporting")),
573 { OPTION_STRING, 0, "prune", &prune_expire, N_("date"),
574 N_("prune unreferenced objects"),
575 PARSE_OPT_OPTARG, NULL, (intptr_t)prune_expire },
576 OPT_BOOL(0, "cruft", &cruft_packs, N_("pack unreferenced objects separately")),
577 OPT_BOOL(0, "aggressive", &aggressive, N_("be more thorough (increased runtime)")),
578 OPT_BOOL_F(0, "auto", &auto_gc, N_("enable auto-gc mode"),
579 PARSE_OPT_NOCOMPLETE),
580 OPT_BOOL_F(0, "force", &force,
581 N_("force running gc even if there may be another gc running"),
582 PARSE_OPT_NOCOMPLETE),
583 OPT_BOOL(0, "keep-largest-pack", &keep_largest_pack,
584 N_("repack all other packs except the largest pack")),
585 OPT_END()
588 if (argc == 2 && !strcmp(argv[1], "-h"))
589 usage_with_options(builtin_gc_usage, builtin_gc_options);
591 strvec_pushl(&reflog, "reflog", "expire", "--all", NULL);
592 strvec_pushl(&repack, "repack", "-d", "-l", NULL);
593 strvec_pushl(&prune, "prune", "--expire", NULL);
594 strvec_pushl(&prune_worktrees, "worktree", "prune", "--expire", NULL);
595 strvec_pushl(&rerere, "rerere", "gc", NULL);
597 /* default expiry time, overwritten in gc_config */
598 gc_config();
599 if (parse_expiry_date(gc_log_expire, &gc_log_expire_time))
600 die(_("failed to parse gc.logExpiry value %s"), gc_log_expire);
602 if (pack_refs < 0)
603 pack_refs = !is_bare_repository();
605 argc = parse_options(argc, argv, prefix, builtin_gc_options,
606 builtin_gc_usage, 0);
607 if (argc > 0)
608 usage_with_options(builtin_gc_usage, builtin_gc_options);
610 if (prune_expire && parse_expiry_date(prune_expire, &dummy))
611 die(_("failed to parse prune expiry value %s"), prune_expire);
613 if (aggressive) {
614 strvec_push(&repack, "-f");
615 if (aggressive_depth > 0)
616 strvec_pushf(&repack, "--depth=%d", aggressive_depth);
617 if (aggressive_window > 0)
618 strvec_pushf(&repack, "--window=%d", aggressive_window);
620 if (quiet)
621 strvec_push(&repack, "-q");
623 if (auto_gc) {
625 * Auto-gc should be least intrusive as possible.
627 if (!need_to_gc())
628 return 0;
629 if (!quiet) {
630 if (detach_auto)
631 fprintf(stderr, _("Auto packing the repository in background for optimum performance.\n"));
632 else
633 fprintf(stderr, _("Auto packing the repository for optimum performance.\n"));
634 fprintf(stderr, _("See \"git help gc\" for manual housekeeping.\n"));
636 if (detach_auto) {
637 int ret = report_last_gc_error();
639 if (ret == 1)
640 /* Last gc --auto failed. Skip this one. */
641 return 0;
642 else if (ret)
643 /* an I/O error occurred, already reported */
644 return ret;
646 if (lock_repo_for_gc(force, &pid))
647 return 0;
648 gc_before_repack(); /* dies on failure */
649 delete_tempfile(&pidfile);
652 * failure to daemonize is ok, we'll continue
653 * in foreground
655 daemonized = !daemonize();
657 } else {
658 struct string_list keep_pack = STRING_LIST_INIT_NODUP;
660 if (keep_largest_pack != -1) {
661 if (keep_largest_pack)
662 find_base_packs(&keep_pack, 0);
663 } else if (big_pack_threshold) {
664 find_base_packs(&keep_pack, big_pack_threshold);
667 add_repack_all_option(&keep_pack);
668 string_list_clear(&keep_pack, 0);
671 name = lock_repo_for_gc(force, &pid);
672 if (name) {
673 if (auto_gc)
674 return 0; /* be quiet on --auto */
675 die(_("gc is already running on machine '%s' pid %"PRIuMAX" (use --force if not)"),
676 name, (uintmax_t)pid);
679 if (daemonized) {
680 hold_lock_file_for_update(&log_lock,
681 git_path("gc.log"),
682 LOCK_DIE_ON_ERROR);
683 dup2(get_lock_file_fd(&log_lock), 2);
684 sigchain_push_common(process_log_file_on_signal);
685 atexit(process_log_file_at_exit);
688 gc_before_repack();
690 if (!repository_format_precious_objects) {
691 struct child_process repack_cmd = CHILD_PROCESS_INIT;
693 repack_cmd.git_cmd = 1;
694 repack_cmd.close_object_store = 1;
695 strvec_pushv(&repack_cmd.args, repack.v);
696 if (run_command(&repack_cmd))
697 die(FAILED_RUN, repack.v[0]);
699 if (prune_expire) {
700 struct child_process prune_cmd = CHILD_PROCESS_INIT;
702 /* run `git prune` even if using cruft packs */
703 strvec_push(&prune, prune_expire);
704 if (quiet)
705 strvec_push(&prune, "--no-progress");
706 if (repo_has_promisor_remote(the_repository))
707 strvec_push(&prune,
708 "--exclude-promisor-objects");
709 prune_cmd.git_cmd = 1;
710 strvec_pushv(&prune_cmd.args, prune.v);
711 if (run_command(&prune_cmd))
712 die(FAILED_RUN, prune.v[0]);
716 if (prune_worktrees_expire) {
717 struct child_process prune_worktrees_cmd = CHILD_PROCESS_INIT;
719 strvec_push(&prune_worktrees, prune_worktrees_expire);
720 prune_worktrees_cmd.git_cmd = 1;
721 strvec_pushv(&prune_worktrees_cmd.args, prune_worktrees.v);
722 if (run_command(&prune_worktrees_cmd))
723 die(FAILED_RUN, prune_worktrees.v[0]);
726 rerere_cmd.git_cmd = 1;
727 strvec_pushv(&rerere_cmd.args, rerere.v);
728 if (run_command(&rerere_cmd))
729 die(FAILED_RUN, rerere.v[0]);
731 report_garbage = report_pack_garbage;
732 reprepare_packed_git(the_repository);
733 if (pack_garbage.nr > 0) {
734 close_object_store(the_repository->objects);
735 clean_pack_garbage();
738 if (the_repository->settings.gc_write_commit_graph == 1)
739 write_commit_graph_reachable(the_repository->objects->odb,
740 !quiet && !daemonized ? COMMIT_GRAPH_WRITE_PROGRESS : 0,
741 NULL);
743 if (auto_gc && too_many_loose_objects())
744 warning(_("There are too many unreachable loose objects; "
745 "run 'git prune' to remove them."));
747 if (!daemonized)
748 unlink(git_path("gc.log"));
750 return 0;
753 static const char *const builtin_maintenance_run_usage[] = {
754 N_("git maintenance run [--auto] [--[no-]quiet] [--task=<task>] [--schedule]"),
755 NULL
758 enum schedule_priority {
759 SCHEDULE_NONE = 0,
760 SCHEDULE_WEEKLY = 1,
761 SCHEDULE_DAILY = 2,
762 SCHEDULE_HOURLY = 3,
765 static enum schedule_priority parse_schedule(const char *value)
767 if (!value)
768 return SCHEDULE_NONE;
769 if (!strcasecmp(value, "hourly"))
770 return SCHEDULE_HOURLY;
771 if (!strcasecmp(value, "daily"))
772 return SCHEDULE_DAILY;
773 if (!strcasecmp(value, "weekly"))
774 return SCHEDULE_WEEKLY;
775 return SCHEDULE_NONE;
778 static int maintenance_opt_schedule(const struct option *opt, const char *arg,
779 int unset)
781 enum schedule_priority *priority = opt->value;
783 if (unset)
784 die(_("--no-schedule is not allowed"));
786 *priority = parse_schedule(arg);
788 if (!*priority)
789 die(_("unrecognized --schedule argument '%s'"), arg);
791 return 0;
794 struct maintenance_run_opts {
795 int auto_flag;
796 int quiet;
797 enum schedule_priority schedule;
800 /* Remember to update object flag allocation in object.h */
801 #define SEEN (1u<<0)
803 struct cg_auto_data {
804 int num_not_in_graph;
805 int limit;
808 static int dfs_on_ref(const char *refname UNUSED,
809 const struct object_id *oid,
810 int flags UNUSED,
811 void *cb_data)
813 struct cg_auto_data *data = (struct cg_auto_data *)cb_data;
814 int result = 0;
815 struct object_id peeled;
816 struct commit_list *stack = NULL;
817 struct commit *commit;
819 if (!peel_iterated_oid(oid, &peeled))
820 oid = &peeled;
821 if (oid_object_info(the_repository, oid, NULL) != OBJ_COMMIT)
822 return 0;
824 commit = lookup_commit(the_repository, oid);
825 if (!commit)
826 return 0;
827 if (repo_parse_commit(the_repository, commit) ||
828 commit_graph_position(commit) != COMMIT_NOT_FROM_GRAPH)
829 return 0;
831 data->num_not_in_graph++;
833 if (data->num_not_in_graph >= data->limit)
834 return 1;
836 commit_list_append(commit, &stack);
838 while (!result && stack) {
839 struct commit_list *parent;
841 commit = pop_commit(&stack);
843 for (parent = commit->parents; parent; parent = parent->next) {
844 if (repo_parse_commit(the_repository, parent->item) ||
845 commit_graph_position(parent->item) != COMMIT_NOT_FROM_GRAPH ||
846 parent->item->object.flags & SEEN)
847 continue;
849 parent->item->object.flags |= SEEN;
850 data->num_not_in_graph++;
852 if (data->num_not_in_graph >= data->limit) {
853 result = 1;
854 break;
857 commit_list_append(parent->item, &stack);
861 free_commit_list(stack);
862 return result;
865 static int should_write_commit_graph(void)
867 int result;
868 struct cg_auto_data data;
870 data.num_not_in_graph = 0;
871 data.limit = 100;
872 git_config_get_int("maintenance.commit-graph.auto",
873 &data.limit);
875 if (!data.limit)
876 return 0;
877 if (data.limit < 0)
878 return 1;
880 result = for_each_ref(dfs_on_ref, &data);
882 repo_clear_commit_marks(the_repository, SEEN);
884 return result;
887 static int run_write_commit_graph(struct maintenance_run_opts *opts)
889 struct child_process child = CHILD_PROCESS_INIT;
891 child.git_cmd = child.close_object_store = 1;
892 strvec_pushl(&child.args, "commit-graph", "write",
893 "--split", "--reachable", NULL);
895 if (opts->quiet)
896 strvec_push(&child.args, "--no-progress");
898 return !!run_command(&child);
901 static int maintenance_task_commit_graph(struct maintenance_run_opts *opts)
903 prepare_repo_settings(the_repository);
904 if (!the_repository->settings.core_commit_graph)
905 return 0;
907 if (run_write_commit_graph(opts)) {
908 error(_("failed to write commit-graph"));
909 return 1;
912 return 0;
915 static int fetch_remote(struct remote *remote, void *cbdata)
917 struct maintenance_run_opts *opts = cbdata;
918 struct child_process child = CHILD_PROCESS_INIT;
920 if (remote->skip_default_update)
921 return 0;
923 child.git_cmd = 1;
924 strvec_pushl(&child.args, "fetch", remote->name,
925 "--prefetch", "--prune", "--no-tags",
926 "--no-write-fetch-head", "--recurse-submodules=no",
927 NULL);
929 if (opts->quiet)
930 strvec_push(&child.args, "--quiet");
932 return !!run_command(&child);
935 static int maintenance_task_prefetch(struct maintenance_run_opts *opts)
937 if (for_each_remote(fetch_remote, opts)) {
938 error(_("failed to prefetch remotes"));
939 return 1;
942 return 0;
945 static int maintenance_task_gc(struct maintenance_run_opts *opts)
947 struct child_process child = CHILD_PROCESS_INIT;
949 child.git_cmd = child.close_object_store = 1;
950 strvec_push(&child.args, "gc");
952 if (opts->auto_flag)
953 strvec_push(&child.args, "--auto");
954 if (opts->quiet)
955 strvec_push(&child.args, "--quiet");
956 else
957 strvec_push(&child.args, "--no-quiet");
959 return run_command(&child);
962 static int prune_packed(struct maintenance_run_opts *opts)
964 struct child_process child = CHILD_PROCESS_INIT;
966 child.git_cmd = 1;
967 strvec_push(&child.args, "prune-packed");
969 if (opts->quiet)
970 strvec_push(&child.args, "--quiet");
972 return !!run_command(&child);
975 struct write_loose_object_data {
976 FILE *in;
977 int count;
978 int batch_size;
981 static int loose_object_auto_limit = 100;
983 static int loose_object_count(const struct object_id *oid UNUSED,
984 const char *path UNUSED,
985 void *data)
987 int *count = (int*)data;
988 if (++(*count) >= loose_object_auto_limit)
989 return 1;
990 return 0;
993 static int loose_object_auto_condition(void)
995 int count = 0;
997 git_config_get_int("maintenance.loose-objects.auto",
998 &loose_object_auto_limit);
1000 if (!loose_object_auto_limit)
1001 return 0;
1002 if (loose_object_auto_limit < 0)
1003 return 1;
1005 return for_each_loose_file_in_objdir(the_repository->objects->odb->path,
1006 loose_object_count,
1007 NULL, NULL, &count);
1010 static int bail_on_loose(const struct object_id *oid UNUSED,
1011 const char *path UNUSED,
1012 void *data UNUSED)
1014 return 1;
1017 static int write_loose_object_to_stdin(const struct object_id *oid,
1018 const char *path UNUSED,
1019 void *data)
1021 struct write_loose_object_data *d = (struct write_loose_object_data *)data;
1023 fprintf(d->in, "%s\n", oid_to_hex(oid));
1025 return ++(d->count) > d->batch_size;
1028 static int pack_loose(struct maintenance_run_opts *opts)
1030 struct repository *r = the_repository;
1031 int result = 0;
1032 struct write_loose_object_data data;
1033 struct child_process pack_proc = CHILD_PROCESS_INIT;
1036 * Do not start pack-objects process
1037 * if there are no loose objects.
1039 if (!for_each_loose_file_in_objdir(r->objects->odb->path,
1040 bail_on_loose,
1041 NULL, NULL, NULL))
1042 return 0;
1044 pack_proc.git_cmd = 1;
1046 strvec_push(&pack_proc.args, "pack-objects");
1047 if (opts->quiet)
1048 strvec_push(&pack_proc.args, "--quiet");
1049 strvec_pushf(&pack_proc.args, "%s/pack/loose", r->objects->odb->path);
1051 pack_proc.in = -1;
1053 if (start_command(&pack_proc)) {
1054 error(_("failed to start 'git pack-objects' process"));
1055 return 1;
1058 data.in = xfdopen(pack_proc.in, "w");
1059 data.count = 0;
1060 data.batch_size = 50000;
1062 for_each_loose_file_in_objdir(r->objects->odb->path,
1063 write_loose_object_to_stdin,
1064 NULL,
1065 NULL,
1066 &data);
1068 fclose(data.in);
1070 if (finish_command(&pack_proc)) {
1071 error(_("failed to finish 'git pack-objects' process"));
1072 result = 1;
1075 return result;
1078 static int maintenance_task_loose_objects(struct maintenance_run_opts *opts)
1080 return prune_packed(opts) || pack_loose(opts);
1083 static int incremental_repack_auto_condition(void)
1085 struct packed_git *p;
1086 int incremental_repack_auto_limit = 10;
1087 int count = 0;
1089 prepare_repo_settings(the_repository);
1090 if (!the_repository->settings.core_multi_pack_index)
1091 return 0;
1093 git_config_get_int("maintenance.incremental-repack.auto",
1094 &incremental_repack_auto_limit);
1096 if (!incremental_repack_auto_limit)
1097 return 0;
1098 if (incremental_repack_auto_limit < 0)
1099 return 1;
1101 for (p = get_packed_git(the_repository);
1102 count < incremental_repack_auto_limit && p;
1103 p = p->next) {
1104 if (!p->multi_pack_index)
1105 count++;
1108 return count >= incremental_repack_auto_limit;
1111 static int multi_pack_index_write(struct maintenance_run_opts *opts)
1113 struct child_process child = CHILD_PROCESS_INIT;
1115 child.git_cmd = 1;
1116 strvec_pushl(&child.args, "multi-pack-index", "write", NULL);
1118 if (opts->quiet)
1119 strvec_push(&child.args, "--no-progress");
1121 if (run_command(&child))
1122 return error(_("failed to write multi-pack-index"));
1124 return 0;
1127 static int multi_pack_index_expire(struct maintenance_run_opts *opts)
1129 struct child_process child = CHILD_PROCESS_INIT;
1131 child.git_cmd = child.close_object_store = 1;
1132 strvec_pushl(&child.args, "multi-pack-index", "expire", NULL);
1134 if (opts->quiet)
1135 strvec_push(&child.args, "--no-progress");
1137 if (run_command(&child))
1138 return error(_("'git multi-pack-index expire' failed"));
1140 return 0;
1143 #define TWO_GIGABYTES (INT32_MAX)
1145 static off_t get_auto_pack_size(void)
1148 * The "auto" value is special: we optimize for
1149 * one large pack-file (i.e. from a clone) and
1150 * expect the rest to be small and they can be
1151 * repacked quickly.
1153 * The strategy we select here is to select a
1154 * size that is one more than the second largest
1155 * pack-file. This ensures that we will repack
1156 * at least two packs if there are three or more
1157 * packs.
1159 off_t max_size = 0;
1160 off_t second_largest_size = 0;
1161 off_t result_size;
1162 struct packed_git *p;
1163 struct repository *r = the_repository;
1165 reprepare_packed_git(r);
1166 for (p = get_all_packs(r); p; p = p->next) {
1167 if (p->pack_size > max_size) {
1168 second_largest_size = max_size;
1169 max_size = p->pack_size;
1170 } else if (p->pack_size > second_largest_size)
1171 second_largest_size = p->pack_size;
1174 result_size = second_largest_size + 1;
1176 /* But limit ourselves to a batch size of 2g */
1177 if (result_size > TWO_GIGABYTES)
1178 result_size = TWO_GIGABYTES;
1180 return result_size;
1183 static int multi_pack_index_repack(struct maintenance_run_opts *opts)
1185 struct child_process child = CHILD_PROCESS_INIT;
1187 child.git_cmd = child.close_object_store = 1;
1188 strvec_pushl(&child.args, "multi-pack-index", "repack", NULL);
1190 if (opts->quiet)
1191 strvec_push(&child.args, "--no-progress");
1193 strvec_pushf(&child.args, "--batch-size=%"PRIuMAX,
1194 (uintmax_t)get_auto_pack_size());
1196 if (run_command(&child))
1197 return error(_("'git multi-pack-index repack' failed"));
1199 return 0;
1202 static int maintenance_task_incremental_repack(struct maintenance_run_opts *opts)
1204 prepare_repo_settings(the_repository);
1205 if (!the_repository->settings.core_multi_pack_index) {
1206 warning(_("skipping incremental-repack task because core.multiPackIndex is disabled"));
1207 return 0;
1210 if (multi_pack_index_write(opts))
1211 return 1;
1212 if (multi_pack_index_expire(opts))
1213 return 1;
1214 if (multi_pack_index_repack(opts))
1215 return 1;
1216 return 0;
1219 typedef int maintenance_task_fn(struct maintenance_run_opts *opts);
1222 * An auto condition function returns 1 if the task should run
1223 * and 0 if the task should NOT run. See needs_to_gc() for an
1224 * example.
1226 typedef int maintenance_auto_fn(void);
1228 struct maintenance_task {
1229 const char *name;
1230 maintenance_task_fn *fn;
1231 maintenance_auto_fn *auto_condition;
1232 unsigned enabled:1;
1234 enum schedule_priority schedule;
1236 /* -1 if not selected. */
1237 int selected_order;
1240 enum maintenance_task_label {
1241 TASK_PREFETCH,
1242 TASK_LOOSE_OBJECTS,
1243 TASK_INCREMENTAL_REPACK,
1244 TASK_GC,
1245 TASK_COMMIT_GRAPH,
1246 TASK_PACK_REFS,
1248 /* Leave as final value */
1249 TASK__COUNT
1252 static struct maintenance_task tasks[] = {
1253 [TASK_PREFETCH] = {
1254 "prefetch",
1255 maintenance_task_prefetch,
1257 [TASK_LOOSE_OBJECTS] = {
1258 "loose-objects",
1259 maintenance_task_loose_objects,
1260 loose_object_auto_condition,
1262 [TASK_INCREMENTAL_REPACK] = {
1263 "incremental-repack",
1264 maintenance_task_incremental_repack,
1265 incremental_repack_auto_condition,
1267 [TASK_GC] = {
1268 "gc",
1269 maintenance_task_gc,
1270 need_to_gc,
1273 [TASK_COMMIT_GRAPH] = {
1274 "commit-graph",
1275 maintenance_task_commit_graph,
1276 should_write_commit_graph,
1278 [TASK_PACK_REFS] = {
1279 "pack-refs",
1280 maintenance_task_pack_refs,
1281 NULL,
1285 static int compare_tasks_by_selection(const void *a_, const void *b_)
1287 const struct maintenance_task *a = a_;
1288 const struct maintenance_task *b = b_;
1290 return b->selected_order - a->selected_order;
1293 static int maintenance_run_tasks(struct maintenance_run_opts *opts)
1295 int i, found_selected = 0;
1296 int result = 0;
1297 struct lock_file lk;
1298 struct repository *r = the_repository;
1299 char *lock_path = xstrfmt("%s/maintenance", r->objects->odb->path);
1301 if (hold_lock_file_for_update(&lk, lock_path, LOCK_NO_DEREF) < 0) {
1303 * Another maintenance command is running.
1305 * If --auto was provided, then it is likely due to a
1306 * recursive process stack. Do not report an error in
1307 * that case.
1309 if (!opts->auto_flag && !opts->quiet)
1310 warning(_("lock file '%s' exists, skipping maintenance"),
1311 lock_path);
1312 free(lock_path);
1313 return 0;
1315 free(lock_path);
1317 for (i = 0; !found_selected && i < TASK__COUNT; i++)
1318 found_selected = tasks[i].selected_order >= 0;
1320 if (found_selected)
1321 QSORT(tasks, TASK__COUNT, compare_tasks_by_selection);
1323 for (i = 0; i < TASK__COUNT; i++) {
1324 if (found_selected && tasks[i].selected_order < 0)
1325 continue;
1327 if (!found_selected && !tasks[i].enabled)
1328 continue;
1330 if (opts->auto_flag &&
1331 (!tasks[i].auto_condition ||
1332 !tasks[i].auto_condition()))
1333 continue;
1335 if (opts->schedule && tasks[i].schedule < opts->schedule)
1336 continue;
1338 trace2_region_enter("maintenance", tasks[i].name, r);
1339 if (tasks[i].fn(opts)) {
1340 error(_("task '%s' failed"), tasks[i].name);
1341 result = 1;
1343 trace2_region_leave("maintenance", tasks[i].name, r);
1346 rollback_lock_file(&lk);
1347 return result;
1350 static void initialize_maintenance_strategy(void)
1352 char *config_str;
1354 if (git_config_get_string("maintenance.strategy", &config_str))
1355 return;
1357 if (!strcasecmp(config_str, "incremental")) {
1358 tasks[TASK_GC].schedule = SCHEDULE_NONE;
1359 tasks[TASK_COMMIT_GRAPH].enabled = 1;
1360 tasks[TASK_COMMIT_GRAPH].schedule = SCHEDULE_HOURLY;
1361 tasks[TASK_PREFETCH].enabled = 1;
1362 tasks[TASK_PREFETCH].schedule = SCHEDULE_HOURLY;
1363 tasks[TASK_INCREMENTAL_REPACK].enabled = 1;
1364 tasks[TASK_INCREMENTAL_REPACK].schedule = SCHEDULE_DAILY;
1365 tasks[TASK_LOOSE_OBJECTS].enabled = 1;
1366 tasks[TASK_LOOSE_OBJECTS].schedule = SCHEDULE_DAILY;
1367 tasks[TASK_PACK_REFS].enabled = 1;
1368 tasks[TASK_PACK_REFS].schedule = SCHEDULE_WEEKLY;
1372 static void initialize_task_config(int schedule)
1374 int i;
1375 struct strbuf config_name = STRBUF_INIT;
1376 gc_config();
1378 if (schedule)
1379 initialize_maintenance_strategy();
1381 for (i = 0; i < TASK__COUNT; i++) {
1382 int config_value;
1383 char *config_str;
1385 strbuf_reset(&config_name);
1386 strbuf_addf(&config_name, "maintenance.%s.enabled",
1387 tasks[i].name);
1389 if (!git_config_get_bool(config_name.buf, &config_value))
1390 tasks[i].enabled = config_value;
1392 strbuf_reset(&config_name);
1393 strbuf_addf(&config_name, "maintenance.%s.schedule",
1394 tasks[i].name);
1396 if (!git_config_get_string(config_name.buf, &config_str)) {
1397 tasks[i].schedule = parse_schedule(config_str);
1398 free(config_str);
1402 strbuf_release(&config_name);
1405 static int task_option_parse(const struct option *opt,
1406 const char *arg, int unset)
1408 int i, num_selected = 0;
1409 struct maintenance_task *task = NULL;
1411 BUG_ON_OPT_NEG(unset);
1413 for (i = 0; i < TASK__COUNT; i++) {
1414 if (tasks[i].selected_order >= 0)
1415 num_selected++;
1416 if (!strcasecmp(tasks[i].name, arg)) {
1417 task = &tasks[i];
1421 if (!task) {
1422 error(_("'%s' is not a valid task"), arg);
1423 return 1;
1426 if (task->selected_order >= 0) {
1427 error(_("task '%s' cannot be selected multiple times"), arg);
1428 return 1;
1431 task->selected_order = num_selected + 1;
1433 return 0;
1436 static int maintenance_run(int argc, const char **argv, const char *prefix)
1438 int i;
1439 struct maintenance_run_opts opts;
1440 struct option builtin_maintenance_run_options[] = {
1441 OPT_BOOL(0, "auto", &opts.auto_flag,
1442 N_("run tasks based on the state of the repository")),
1443 OPT_CALLBACK(0, "schedule", &opts.schedule, N_("frequency"),
1444 N_("run tasks based on frequency"),
1445 maintenance_opt_schedule),
1446 OPT_BOOL(0, "quiet", &opts.quiet,
1447 N_("do not report progress or other information over stderr")),
1448 OPT_CALLBACK_F(0, "task", NULL, N_("task"),
1449 N_("run a specific task"),
1450 PARSE_OPT_NONEG, task_option_parse),
1451 OPT_END()
1453 memset(&opts, 0, sizeof(opts));
1455 opts.quiet = !isatty(2);
1457 for (i = 0; i < TASK__COUNT; i++)
1458 tasks[i].selected_order = -1;
1460 argc = parse_options(argc, argv, prefix,
1461 builtin_maintenance_run_options,
1462 builtin_maintenance_run_usage,
1463 PARSE_OPT_STOP_AT_NON_OPTION);
1465 if (opts.auto_flag && opts.schedule)
1466 die(_("use at most one of --auto and --schedule=<frequency>"));
1468 initialize_task_config(opts.schedule);
1470 if (argc != 0)
1471 usage_with_options(builtin_maintenance_run_usage,
1472 builtin_maintenance_run_options);
1473 return maintenance_run_tasks(&opts);
1476 static char *get_maintpath(void)
1478 struct strbuf sb = STRBUF_INIT;
1479 const char *p = the_repository->worktree ?
1480 the_repository->worktree : the_repository->gitdir;
1482 strbuf_realpath(&sb, p, 1);
1483 return strbuf_detach(&sb, NULL);
1486 static char const * const builtin_maintenance_register_usage[] = {
1487 "git maintenance register [--config-file <path>]",
1488 NULL
1491 static int maintenance_register(int argc, const char **argv, const char *prefix)
1493 char *config_file = NULL;
1494 struct option options[] = {
1495 OPT_STRING(0, "config-file", &config_file, N_("file"), N_("use given config file")),
1496 OPT_END(),
1498 int found = 0;
1499 const char *key = "maintenance.repo";
1500 char *maintpath = get_maintpath();
1501 struct string_list_item *item;
1502 const struct string_list *list;
1504 argc = parse_options(argc, argv, prefix, options,
1505 builtin_maintenance_register_usage, 0);
1506 if (argc)
1507 usage_with_options(builtin_maintenance_register_usage,
1508 options);
1510 /* Disable foreground maintenance */
1511 git_config_set("maintenance.auto", "false");
1513 /* Set maintenance strategy, if unset */
1514 if (git_config_get("maintenance.strategy"))
1515 git_config_set("maintenance.strategy", "incremental");
1517 if (!git_config_get_string_multi(key, &list)) {
1518 for_each_string_list_item(item, list) {
1519 if (!strcmp(maintpath, item->string)) {
1520 found = 1;
1521 break;
1526 if (!found) {
1527 int rc;
1528 char *user_config = NULL, *xdg_config = NULL;
1530 if (!config_file) {
1531 git_global_config(&user_config, &xdg_config);
1532 config_file = user_config;
1533 if (!user_config)
1534 die(_("$HOME not set"));
1536 rc = git_config_set_multivar_in_file_gently(
1537 config_file, "maintenance.repo", maintpath,
1538 CONFIG_REGEX_NONE, 0);
1539 free(user_config);
1540 free(xdg_config);
1542 if (rc)
1543 die(_("unable to add '%s' value of '%s'"),
1544 key, maintpath);
1547 free(maintpath);
1548 return 0;
1551 static char const * const builtin_maintenance_unregister_usage[] = {
1552 "git maintenance unregister [--config-file <path>] [--force]",
1553 NULL
1556 static int maintenance_unregister(int argc, const char **argv, const char *prefix)
1558 int force = 0;
1559 char *config_file = NULL;
1560 struct option options[] = {
1561 OPT_STRING(0, "config-file", &config_file, N_("file"), N_("use given config file")),
1562 OPT__FORCE(&force,
1563 N_("return success even if repository was not registered"),
1564 PARSE_OPT_NOCOMPLETE),
1565 OPT_END(),
1567 const char *key = "maintenance.repo";
1568 char *maintpath = get_maintpath();
1569 int found = 0;
1570 struct string_list_item *item;
1571 const struct string_list *list;
1572 struct config_set cs = { { 0 } };
1574 argc = parse_options(argc, argv, prefix, options,
1575 builtin_maintenance_unregister_usage, 0);
1576 if (argc)
1577 usage_with_options(builtin_maintenance_unregister_usage,
1578 options);
1580 if (config_file) {
1581 git_configset_init(&cs);
1582 git_configset_add_file(&cs, config_file);
1584 if (!(config_file
1585 ? git_configset_get_string_multi(&cs, key, &list)
1586 : git_config_get_string_multi(key, &list))) {
1587 for_each_string_list_item(item, list) {
1588 if (!strcmp(maintpath, item->string)) {
1589 found = 1;
1590 break;
1595 if (found) {
1596 int rc;
1597 char *user_config = NULL, *xdg_config = NULL;
1598 if (!config_file) {
1599 git_global_config(&user_config, &xdg_config);
1600 config_file = user_config;
1601 if (!user_config)
1602 die(_("$HOME not set"));
1604 rc = git_config_set_multivar_in_file_gently(
1605 config_file, key, NULL, maintpath,
1606 CONFIG_FLAGS_MULTI_REPLACE | CONFIG_FLAGS_FIXED_VALUE);
1607 free(user_config);
1608 free(xdg_config);
1610 if (rc &&
1611 (!force || rc == CONFIG_NOTHING_SET))
1612 die(_("unable to unset '%s' value of '%s'"),
1613 key, maintpath);
1614 } else if (!force) {
1615 die(_("repository '%s' is not registered"), maintpath);
1618 git_configset_clear(&cs);
1619 free(maintpath);
1620 return 0;
1623 static const char *get_frequency(enum schedule_priority schedule)
1625 switch (schedule) {
1626 case SCHEDULE_HOURLY:
1627 return "hourly";
1628 case SCHEDULE_DAILY:
1629 return "daily";
1630 case SCHEDULE_WEEKLY:
1631 return "weekly";
1632 default:
1633 BUG("invalid schedule %d", schedule);
1638 * get_schedule_cmd` reads the GIT_TEST_MAINT_SCHEDULER environment variable
1639 * to mock the schedulers that `git maintenance start` rely on.
1641 * For test purpose, GIT_TEST_MAINT_SCHEDULER can be set to a comma-separated
1642 * list of colon-separated key/value pairs where each pair contains a scheduler
1643 * and its corresponding mock.
1645 * * If $GIT_TEST_MAINT_SCHEDULER is not set, return false and leave the
1646 * arguments unmodified.
1648 * * If $GIT_TEST_MAINT_SCHEDULER is set, return true.
1649 * In this case, the *cmd value is read as input.
1651 * * if the input value *cmd is the key of one of the comma-separated list
1652 * item, then *is_available is set to true and *cmd is modified and becomes
1653 * the mock command.
1655 * * if the input value *cmd isn’t the key of any of the comma-separated list
1656 * item, then *is_available is set to false.
1658 * Ex.:
1659 * GIT_TEST_MAINT_SCHEDULER not set
1660 * +-------+-------------------------------------------------+
1661 * | Input | Output |
1662 * | *cmd | return code | *cmd | *is_available |
1663 * +-------+-------------+-------------------+---------------+
1664 * | "foo" | false | "foo" (unchanged) | (unchanged) |
1665 * +-------+-------------+-------------------+---------------+
1667 * GIT_TEST_MAINT_SCHEDULER set to “foo:./mock_foo.sh,bar:./mock_bar.sh”
1668 * +-------+-------------------------------------------------+
1669 * | Input | Output |
1670 * | *cmd | return code | *cmd | *is_available |
1671 * +-------+-------------+-------------------+---------------+
1672 * | "foo" | true | "./mock.foo.sh" | true |
1673 * | "qux" | true | "qux" (unchanged) | false |
1674 * +-------+-------------+-------------------+---------------+
1676 static int get_schedule_cmd(const char **cmd, int *is_available)
1678 char *testing = xstrdup_or_null(getenv("GIT_TEST_MAINT_SCHEDULER"));
1679 struct string_list_item *item;
1680 struct string_list list = STRING_LIST_INIT_NODUP;
1682 if (!testing)
1683 return 0;
1685 if (is_available)
1686 *is_available = 0;
1688 string_list_split_in_place(&list, testing, ',', -1);
1689 for_each_string_list_item(item, &list) {
1690 struct string_list pair = STRING_LIST_INIT_NODUP;
1692 if (string_list_split_in_place(&pair, item->string, ':', 2) != 2)
1693 continue;
1695 if (!strcmp(*cmd, pair.items[0].string)) {
1696 *cmd = pair.items[1].string;
1697 if (is_available)
1698 *is_available = 1;
1699 string_list_clear(&list, 0);
1700 UNLEAK(testing);
1701 return 1;
1705 string_list_clear(&list, 0);
1706 free(testing);
1707 return 1;
1710 static int is_launchctl_available(void)
1712 const char *cmd = "launchctl";
1713 int is_available;
1714 if (get_schedule_cmd(&cmd, &is_available))
1715 return is_available;
1717 #ifdef __APPLE__
1718 return 1;
1719 #else
1720 return 0;
1721 #endif
1724 static char *launchctl_service_name(const char *frequency)
1726 struct strbuf label = STRBUF_INIT;
1727 strbuf_addf(&label, "org.git-scm.git.%s", frequency);
1728 return strbuf_detach(&label, NULL);
1731 static char *launchctl_service_filename(const char *name)
1733 char *expanded;
1734 struct strbuf filename = STRBUF_INIT;
1735 strbuf_addf(&filename, "~/Library/LaunchAgents/%s.plist", name);
1737 expanded = interpolate_path(filename.buf, 1);
1738 if (!expanded)
1739 die(_("failed to expand path '%s'"), filename.buf);
1741 strbuf_release(&filename);
1742 return expanded;
1745 static char *launchctl_get_uid(void)
1747 return xstrfmt("gui/%d", getuid());
1750 static int launchctl_boot_plist(int enable, const char *filename)
1752 const char *cmd = "launchctl";
1753 int result;
1754 struct child_process child = CHILD_PROCESS_INIT;
1755 char *uid = launchctl_get_uid();
1757 get_schedule_cmd(&cmd, NULL);
1758 strvec_split(&child.args, cmd);
1759 strvec_pushl(&child.args, enable ? "bootstrap" : "bootout", uid,
1760 filename, NULL);
1762 child.no_stderr = 1;
1763 child.no_stdout = 1;
1765 if (start_command(&child))
1766 die(_("failed to start launchctl"));
1768 result = finish_command(&child);
1770 free(uid);
1771 return result;
1774 static int launchctl_remove_plist(enum schedule_priority schedule)
1776 const char *frequency = get_frequency(schedule);
1777 char *name = launchctl_service_name(frequency);
1778 char *filename = launchctl_service_filename(name);
1779 int result = launchctl_boot_plist(0, filename);
1780 unlink(filename);
1781 free(filename);
1782 free(name);
1783 return result;
1786 static int launchctl_remove_plists(void)
1788 return launchctl_remove_plist(SCHEDULE_HOURLY) ||
1789 launchctl_remove_plist(SCHEDULE_DAILY) ||
1790 launchctl_remove_plist(SCHEDULE_WEEKLY);
1793 static int launchctl_list_contains_plist(const char *name, const char *cmd)
1795 struct child_process child = CHILD_PROCESS_INIT;
1797 strvec_split(&child.args, cmd);
1798 strvec_pushl(&child.args, "list", name, NULL);
1800 child.no_stderr = 1;
1801 child.no_stdout = 1;
1803 if (start_command(&child))
1804 die(_("failed to start launchctl"));
1806 /* Returns failure if 'name' doesn't exist. */
1807 return !finish_command(&child);
1810 static int launchctl_schedule_plist(const char *exec_path, enum schedule_priority schedule)
1812 int i, fd;
1813 const char *preamble, *repeat;
1814 const char *frequency = get_frequency(schedule);
1815 char *name = launchctl_service_name(frequency);
1816 char *filename = launchctl_service_filename(name);
1817 struct lock_file lk = LOCK_INIT;
1818 static unsigned long lock_file_timeout_ms = ULONG_MAX;
1819 struct strbuf plist = STRBUF_INIT, plist2 = STRBUF_INIT;
1820 struct stat st;
1821 const char *cmd = "launchctl";
1823 get_schedule_cmd(&cmd, NULL);
1824 preamble = "<?xml version=\"1.0\"?>\n"
1825 "<!DOCTYPE plist PUBLIC \"-//Apple//DTD PLIST 1.0//EN\" \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">\n"
1826 "<plist version=\"1.0\">"
1827 "<dict>\n"
1828 "<key>Label</key><string>%s</string>\n"
1829 "<key>ProgramArguments</key>\n"
1830 "<array>\n"
1831 "<string>%s/git</string>\n"
1832 "<string>--exec-path=%s</string>\n"
1833 "<string>for-each-repo</string>\n"
1834 "<string>--config=maintenance.repo</string>\n"
1835 "<string>maintenance</string>\n"
1836 "<string>run</string>\n"
1837 "<string>--schedule=%s</string>\n"
1838 "</array>\n"
1839 "<key>StartCalendarInterval</key>\n"
1840 "<array>\n";
1841 strbuf_addf(&plist, preamble, name, exec_path, exec_path, frequency);
1843 switch (schedule) {
1844 case SCHEDULE_HOURLY:
1845 repeat = "<dict>\n"
1846 "<key>Hour</key><integer>%d</integer>\n"
1847 "<key>Minute</key><integer>0</integer>\n"
1848 "</dict>\n";
1849 for (i = 1; i <= 23; i++)
1850 strbuf_addf(&plist, repeat, i);
1851 break;
1853 case SCHEDULE_DAILY:
1854 repeat = "<dict>\n"
1855 "<key>Day</key><integer>%d</integer>\n"
1856 "<key>Hour</key><integer>0</integer>\n"
1857 "<key>Minute</key><integer>0</integer>\n"
1858 "</dict>\n";
1859 for (i = 1; i <= 6; i++)
1860 strbuf_addf(&plist, repeat, i);
1861 break;
1863 case SCHEDULE_WEEKLY:
1864 strbuf_addstr(&plist,
1865 "<dict>\n"
1866 "<key>Day</key><integer>0</integer>\n"
1867 "<key>Hour</key><integer>0</integer>\n"
1868 "<key>Minute</key><integer>0</integer>\n"
1869 "</dict>\n");
1870 break;
1872 default:
1873 /* unreachable */
1874 break;
1876 strbuf_addstr(&plist, "</array>\n</dict>\n</plist>\n");
1878 if (safe_create_leading_directories(filename))
1879 die(_("failed to create directories for '%s'"), filename);
1881 if ((long)lock_file_timeout_ms < 0 &&
1882 git_config_get_ulong("gc.launchctlplistlocktimeoutms",
1883 &lock_file_timeout_ms))
1884 lock_file_timeout_ms = 150;
1886 fd = hold_lock_file_for_update_timeout(&lk, filename, LOCK_DIE_ON_ERROR,
1887 lock_file_timeout_ms);
1890 * Does this file already exist? With the intended contents? Is it
1891 * registered already? Then it does not need to be re-registered.
1893 if (!stat(filename, &st) && st.st_size == plist.len &&
1894 strbuf_read_file(&plist2, filename, plist.len) == plist.len &&
1895 !strbuf_cmp(&plist, &plist2) &&
1896 launchctl_list_contains_plist(name, cmd))
1897 rollback_lock_file(&lk);
1898 else {
1899 if (write_in_full(fd, plist.buf, plist.len) < 0 ||
1900 commit_lock_file(&lk))
1901 die_errno(_("could not write '%s'"), filename);
1903 /* bootout might fail if not already running, so ignore */
1904 launchctl_boot_plist(0, filename);
1905 if (launchctl_boot_plist(1, filename))
1906 die(_("failed to bootstrap service %s"), filename);
1909 free(filename);
1910 free(name);
1911 strbuf_release(&plist);
1912 strbuf_release(&plist2);
1913 return 0;
1916 static int launchctl_add_plists(void)
1918 const char *exec_path = git_exec_path();
1920 return launchctl_schedule_plist(exec_path, SCHEDULE_HOURLY) ||
1921 launchctl_schedule_plist(exec_path, SCHEDULE_DAILY) ||
1922 launchctl_schedule_plist(exec_path, SCHEDULE_WEEKLY);
1925 static int launchctl_update_schedule(int run_maintenance, int fd)
1927 if (run_maintenance)
1928 return launchctl_add_plists();
1929 else
1930 return launchctl_remove_plists();
1933 static int is_schtasks_available(void)
1935 const char *cmd = "schtasks";
1936 int is_available;
1937 if (get_schedule_cmd(&cmd, &is_available))
1938 return is_available;
1940 #ifdef GIT_WINDOWS_NATIVE
1941 return 1;
1942 #else
1943 return 0;
1944 #endif
1947 static char *schtasks_task_name(const char *frequency)
1949 struct strbuf label = STRBUF_INIT;
1950 strbuf_addf(&label, "Git Maintenance (%s)", frequency);
1951 return strbuf_detach(&label, NULL);
1954 static int schtasks_remove_task(enum schedule_priority schedule)
1956 const char *cmd = "schtasks";
1957 struct child_process child = CHILD_PROCESS_INIT;
1958 const char *frequency = get_frequency(schedule);
1959 char *name = schtasks_task_name(frequency);
1961 get_schedule_cmd(&cmd, NULL);
1962 strvec_split(&child.args, cmd);
1963 strvec_pushl(&child.args, "/delete", "/tn", name, "/f", NULL);
1964 free(name);
1966 return run_command(&child);
1969 static int schtasks_remove_tasks(void)
1971 return schtasks_remove_task(SCHEDULE_HOURLY) ||
1972 schtasks_remove_task(SCHEDULE_DAILY) ||
1973 schtasks_remove_task(SCHEDULE_WEEKLY);
1976 static int schtasks_schedule_task(const char *exec_path, enum schedule_priority schedule)
1978 const char *cmd = "schtasks";
1979 int result;
1980 struct child_process child = CHILD_PROCESS_INIT;
1981 const char *xml;
1982 struct tempfile *tfile;
1983 const char *frequency = get_frequency(schedule);
1984 char *name = schtasks_task_name(frequency);
1985 struct strbuf tfilename = STRBUF_INIT;
1987 get_schedule_cmd(&cmd, NULL);
1989 strbuf_addf(&tfilename, "%s/schedule_%s_XXXXXX",
1990 get_git_common_dir(), frequency);
1991 tfile = xmks_tempfile(tfilename.buf);
1992 strbuf_release(&tfilename);
1994 if (!fdopen_tempfile(tfile, "w"))
1995 die(_("failed to create temp xml file"));
1997 xml = "<?xml version=\"1.0\" ?>\n"
1998 "<Task version=\"1.4\" xmlns=\"http://schemas.microsoft.com/windows/2004/02/mit/task\">\n"
1999 "<Triggers>\n"
2000 "<CalendarTrigger>\n";
2001 fputs(xml, tfile->fp);
2003 switch (schedule) {
2004 case SCHEDULE_HOURLY:
2005 fprintf(tfile->fp,
2006 "<StartBoundary>2020-01-01T01:00:00</StartBoundary>\n"
2007 "<Enabled>true</Enabled>\n"
2008 "<ScheduleByDay>\n"
2009 "<DaysInterval>1</DaysInterval>\n"
2010 "</ScheduleByDay>\n"
2011 "<Repetition>\n"
2012 "<Interval>PT1H</Interval>\n"
2013 "<Duration>PT23H</Duration>\n"
2014 "<StopAtDurationEnd>false</StopAtDurationEnd>\n"
2015 "</Repetition>\n");
2016 break;
2018 case SCHEDULE_DAILY:
2019 fprintf(tfile->fp,
2020 "<StartBoundary>2020-01-01T00:00:00</StartBoundary>\n"
2021 "<Enabled>true</Enabled>\n"
2022 "<ScheduleByWeek>\n"
2023 "<DaysOfWeek>\n"
2024 "<Monday />\n"
2025 "<Tuesday />\n"
2026 "<Wednesday />\n"
2027 "<Thursday />\n"
2028 "<Friday />\n"
2029 "<Saturday />\n"
2030 "</DaysOfWeek>\n"
2031 "<WeeksInterval>1</WeeksInterval>\n"
2032 "</ScheduleByWeek>\n");
2033 break;
2035 case SCHEDULE_WEEKLY:
2036 fprintf(tfile->fp,
2037 "<StartBoundary>2020-01-01T00:00:00</StartBoundary>\n"
2038 "<Enabled>true</Enabled>\n"
2039 "<ScheduleByWeek>\n"
2040 "<DaysOfWeek>\n"
2041 "<Sunday />\n"
2042 "</DaysOfWeek>\n"
2043 "<WeeksInterval>1</WeeksInterval>\n"
2044 "</ScheduleByWeek>\n");
2045 break;
2047 default:
2048 break;
2051 xml = "</CalendarTrigger>\n"
2052 "</Triggers>\n"
2053 "<Principals>\n"
2054 "<Principal id=\"Author\">\n"
2055 "<LogonType>InteractiveToken</LogonType>\n"
2056 "<RunLevel>LeastPrivilege</RunLevel>\n"
2057 "</Principal>\n"
2058 "</Principals>\n"
2059 "<Settings>\n"
2060 "<MultipleInstancesPolicy>IgnoreNew</MultipleInstancesPolicy>\n"
2061 "<Enabled>true</Enabled>\n"
2062 "<Hidden>true</Hidden>\n"
2063 "<UseUnifiedSchedulingEngine>true</UseUnifiedSchedulingEngine>\n"
2064 "<WakeToRun>false</WakeToRun>\n"
2065 "<ExecutionTimeLimit>PT72H</ExecutionTimeLimit>\n"
2066 "<Priority>7</Priority>\n"
2067 "</Settings>\n"
2068 "<Actions Context=\"Author\">\n"
2069 "<Exec>\n"
2070 "<Command>\"%s\\git.exe\"</Command>\n"
2071 "<Arguments>--exec-path=\"%s\" for-each-repo --config=maintenance.repo maintenance run --schedule=%s</Arguments>\n"
2072 "</Exec>\n"
2073 "</Actions>\n"
2074 "</Task>\n";
2075 fprintf(tfile->fp, xml, exec_path, exec_path, frequency);
2076 strvec_split(&child.args, cmd);
2077 strvec_pushl(&child.args, "/create", "/tn", name, "/f", "/xml",
2078 get_tempfile_path(tfile), NULL);
2079 close_tempfile_gently(tfile);
2081 child.no_stdout = 1;
2082 child.no_stderr = 1;
2084 if (start_command(&child))
2085 die(_("failed to start schtasks"));
2086 result = finish_command(&child);
2088 delete_tempfile(&tfile);
2089 free(name);
2090 return result;
2093 static int schtasks_schedule_tasks(void)
2095 const char *exec_path = git_exec_path();
2097 return schtasks_schedule_task(exec_path, SCHEDULE_HOURLY) ||
2098 schtasks_schedule_task(exec_path, SCHEDULE_DAILY) ||
2099 schtasks_schedule_task(exec_path, SCHEDULE_WEEKLY);
2102 static int schtasks_update_schedule(int run_maintenance, int fd)
2104 if (run_maintenance)
2105 return schtasks_schedule_tasks();
2106 else
2107 return schtasks_remove_tasks();
2110 MAYBE_UNUSED
2111 static int check_crontab_process(const char *cmd)
2113 struct child_process child = CHILD_PROCESS_INIT;
2115 strvec_split(&child.args, cmd);
2116 strvec_push(&child.args, "-l");
2117 child.no_stdin = 1;
2118 child.no_stdout = 1;
2119 child.no_stderr = 1;
2120 child.silent_exec_failure = 1;
2122 if (start_command(&child))
2123 return 0;
2124 /* Ignore exit code, as an empty crontab will return error. */
2125 finish_command(&child);
2126 return 1;
2129 static int is_crontab_available(void)
2131 const char *cmd = "crontab";
2132 int is_available;
2134 if (get_schedule_cmd(&cmd, &is_available))
2135 return is_available;
2137 #ifdef __APPLE__
2139 * macOS has cron, but it requires special permissions and will
2140 * create a UI alert when attempting to run this command.
2142 return 0;
2143 #else
2144 return check_crontab_process(cmd);
2145 #endif
2148 #define BEGIN_LINE "# BEGIN GIT MAINTENANCE SCHEDULE"
2149 #define END_LINE "# END GIT MAINTENANCE SCHEDULE"
2151 static int crontab_update_schedule(int run_maintenance, int fd)
2153 const char *cmd = "crontab";
2154 int result = 0;
2155 int in_old_region = 0;
2156 struct child_process crontab_list = CHILD_PROCESS_INIT;
2157 struct child_process crontab_edit = CHILD_PROCESS_INIT;
2158 FILE *cron_list, *cron_in;
2159 struct strbuf line = STRBUF_INIT;
2160 struct tempfile *tmpedit = NULL;
2162 get_schedule_cmd(&cmd, NULL);
2163 strvec_split(&crontab_list.args, cmd);
2164 strvec_push(&crontab_list.args, "-l");
2165 crontab_list.in = -1;
2166 crontab_list.out = dup(fd);
2167 crontab_list.git_cmd = 0;
2169 if (start_command(&crontab_list))
2170 return error(_("failed to run 'crontab -l'; your system might not support 'cron'"));
2172 /* Ignore exit code, as an empty crontab will return error. */
2173 finish_command(&crontab_list);
2175 tmpedit = mks_tempfile_t(".git_cron_edit_tmpXXXXXX");
2176 if (!tmpedit) {
2177 result = error(_("failed to create crontab temporary file"));
2178 goto out;
2180 cron_in = fdopen_tempfile(tmpedit, "w");
2181 if (!cron_in) {
2182 result = error(_("failed to open temporary file"));
2183 goto out;
2187 * Read from the .lock file, filtering out the old
2188 * schedule while appending the new schedule.
2190 cron_list = fdopen(fd, "r");
2191 rewind(cron_list);
2193 while (!strbuf_getline_lf(&line, cron_list)) {
2194 if (!in_old_region && !strcmp(line.buf, BEGIN_LINE))
2195 in_old_region = 1;
2196 else if (in_old_region && !strcmp(line.buf, END_LINE))
2197 in_old_region = 0;
2198 else if (!in_old_region)
2199 fprintf(cron_in, "%s\n", line.buf);
2201 strbuf_release(&line);
2203 if (run_maintenance) {
2204 struct strbuf line_format = STRBUF_INIT;
2205 const char *exec_path = git_exec_path();
2207 fprintf(cron_in, "%s\n", BEGIN_LINE);
2208 fprintf(cron_in,
2209 "# The following schedule was created by Git\n");
2210 fprintf(cron_in, "# Any edits made in this region might be\n");
2211 fprintf(cron_in,
2212 "# replaced in the future by a Git command.\n\n");
2214 strbuf_addf(&line_format,
2215 "%%s %%s * * %%s \"%s/git\" --exec-path=\"%s\" for-each-repo --config=maintenance.repo maintenance run --schedule=%%s\n",
2216 exec_path, exec_path);
2217 fprintf(cron_in, line_format.buf, "0", "1-23", "*", "hourly");
2218 fprintf(cron_in, line_format.buf, "0", "0", "1-6", "daily");
2219 fprintf(cron_in, line_format.buf, "0", "0", "0", "weekly");
2220 strbuf_release(&line_format);
2222 fprintf(cron_in, "\n%s\n", END_LINE);
2225 fflush(cron_in);
2227 strvec_split(&crontab_edit.args, cmd);
2228 strvec_push(&crontab_edit.args, get_tempfile_path(tmpedit));
2229 crontab_edit.git_cmd = 0;
2231 if (start_command(&crontab_edit)) {
2232 result = error(_("failed to run 'crontab'; your system might not support 'cron'"));
2233 goto out;
2236 if (finish_command(&crontab_edit))
2237 result = error(_("'crontab' died"));
2238 else
2239 fclose(cron_list);
2240 out:
2241 delete_tempfile(&tmpedit);
2242 return result;
2245 static int real_is_systemd_timer_available(void)
2247 struct child_process child = CHILD_PROCESS_INIT;
2249 strvec_pushl(&child.args, "systemctl", "--user", "list-timers", NULL);
2250 child.no_stdin = 1;
2251 child.no_stdout = 1;
2252 child.no_stderr = 1;
2253 child.silent_exec_failure = 1;
2255 if (start_command(&child))
2256 return 0;
2257 if (finish_command(&child))
2258 return 0;
2259 return 1;
2262 static int is_systemd_timer_available(void)
2264 const char *cmd = "systemctl";
2265 int is_available;
2267 if (get_schedule_cmd(&cmd, &is_available))
2268 return is_available;
2270 return real_is_systemd_timer_available();
2273 static char *xdg_config_home_systemd(const char *filename)
2275 return xdg_config_home_for("systemd/user", filename);
2278 static int systemd_timer_enable_unit(int enable,
2279 enum schedule_priority schedule)
2281 const char *cmd = "systemctl";
2282 struct child_process child = CHILD_PROCESS_INIT;
2283 const char *frequency = get_frequency(schedule);
2286 * Disabling the systemd unit while it is already disabled makes
2287 * systemctl print an error.
2288 * Let's ignore it since it means we already are in the expected state:
2289 * the unit is disabled.
2291 * On the other hand, enabling a systemd unit which is already enabled
2292 * produces no error.
2294 if (!enable)
2295 child.no_stderr = 1;
2297 get_schedule_cmd(&cmd, NULL);
2298 strvec_split(&child.args, cmd);
2299 strvec_pushl(&child.args, "--user", enable ? "enable" : "disable",
2300 "--now", NULL);
2301 strvec_pushf(&child.args, "git-maintenance@%s.timer", frequency);
2303 if (start_command(&child))
2304 return error(_("failed to start systemctl"));
2305 if (finish_command(&child))
2307 * Disabling an already disabled systemd unit makes
2308 * systemctl fail.
2309 * Let's ignore this failure.
2311 * Enabling an enabled systemd unit doesn't fail.
2313 if (enable)
2314 return error(_("failed to run systemctl"));
2315 return 0;
2318 static int systemd_timer_delete_unit_templates(void)
2320 int ret = 0;
2321 char *filename = xdg_config_home_systemd("git-maintenance@.timer");
2322 if (unlink(filename) && !is_missing_file_error(errno))
2323 ret = error_errno(_("failed to delete '%s'"), filename);
2324 FREE_AND_NULL(filename);
2326 filename = xdg_config_home_systemd("git-maintenance@.service");
2327 if (unlink(filename) && !is_missing_file_error(errno))
2328 ret = error_errno(_("failed to delete '%s'"), filename);
2330 free(filename);
2331 return ret;
2334 static int systemd_timer_delete_units(void)
2336 return systemd_timer_enable_unit(0, SCHEDULE_HOURLY) ||
2337 systemd_timer_enable_unit(0, SCHEDULE_DAILY) ||
2338 systemd_timer_enable_unit(0, SCHEDULE_WEEKLY) ||
2339 systemd_timer_delete_unit_templates();
2342 static int systemd_timer_write_unit_templates(const char *exec_path)
2344 char *filename;
2345 FILE *file;
2346 const char *unit;
2348 filename = xdg_config_home_systemd("git-maintenance@.timer");
2349 if (safe_create_leading_directories(filename)) {
2350 error(_("failed to create directories for '%s'"), filename);
2351 goto error;
2353 file = fopen_or_warn(filename, "w");
2354 if (!file)
2355 goto error;
2357 unit = "# This file was created and is maintained by Git.\n"
2358 "# Any edits made in this file might be replaced in the future\n"
2359 "# by a Git command.\n"
2360 "\n"
2361 "[Unit]\n"
2362 "Description=Optimize Git repositories data\n"
2363 "\n"
2364 "[Timer]\n"
2365 "OnCalendar=%i\n"
2366 "Persistent=true\n"
2367 "\n"
2368 "[Install]\n"
2369 "WantedBy=timers.target\n";
2370 if (fputs(unit, file) == EOF) {
2371 error(_("failed to write to '%s'"), filename);
2372 fclose(file);
2373 goto error;
2375 if (fclose(file) == EOF) {
2376 error_errno(_("failed to flush '%s'"), filename);
2377 goto error;
2379 free(filename);
2381 filename = xdg_config_home_systemd("git-maintenance@.service");
2382 file = fopen_or_warn(filename, "w");
2383 if (!file)
2384 goto error;
2386 unit = "# This file was created and is maintained by Git.\n"
2387 "# Any edits made in this file might be replaced in the future\n"
2388 "# by a Git command.\n"
2389 "\n"
2390 "[Unit]\n"
2391 "Description=Optimize Git repositories data\n"
2392 "\n"
2393 "[Service]\n"
2394 "Type=oneshot\n"
2395 "ExecStart=\"%s/git\" --exec-path=\"%s\" for-each-repo --config=maintenance.repo maintenance run --schedule=%%i\n"
2396 "LockPersonality=yes\n"
2397 "MemoryDenyWriteExecute=yes\n"
2398 "NoNewPrivileges=yes\n"
2399 "RestrictAddressFamilies=AF_UNIX AF_INET AF_INET6\n"
2400 "RestrictNamespaces=yes\n"
2401 "RestrictRealtime=yes\n"
2402 "RestrictSUIDSGID=yes\n"
2403 "SystemCallArchitectures=native\n"
2404 "SystemCallFilter=@system-service\n";
2405 if (fprintf(file, unit, exec_path, exec_path) < 0) {
2406 error(_("failed to write to '%s'"), filename);
2407 fclose(file);
2408 goto error;
2410 if (fclose(file) == EOF) {
2411 error_errno(_("failed to flush '%s'"), filename);
2412 goto error;
2414 free(filename);
2415 return 0;
2417 error:
2418 free(filename);
2419 systemd_timer_delete_unit_templates();
2420 return -1;
2423 static int systemd_timer_setup_units(void)
2425 const char *exec_path = git_exec_path();
2427 int ret = systemd_timer_write_unit_templates(exec_path) ||
2428 systemd_timer_enable_unit(1, SCHEDULE_HOURLY) ||
2429 systemd_timer_enable_unit(1, SCHEDULE_DAILY) ||
2430 systemd_timer_enable_unit(1, SCHEDULE_WEEKLY);
2431 if (ret)
2432 systemd_timer_delete_units();
2433 return ret;
2436 static int systemd_timer_update_schedule(int run_maintenance, int fd)
2438 if (run_maintenance)
2439 return systemd_timer_setup_units();
2440 else
2441 return systemd_timer_delete_units();
2444 enum scheduler {
2445 SCHEDULER_INVALID = -1,
2446 SCHEDULER_AUTO,
2447 SCHEDULER_CRON,
2448 SCHEDULER_SYSTEMD,
2449 SCHEDULER_LAUNCHCTL,
2450 SCHEDULER_SCHTASKS,
2453 static const struct {
2454 const char *name;
2455 int (*is_available)(void);
2456 int (*update_schedule)(int run_maintenance, int fd);
2457 } scheduler_fn[] = {
2458 [SCHEDULER_CRON] = {
2459 .name = "crontab",
2460 .is_available = is_crontab_available,
2461 .update_schedule = crontab_update_schedule,
2463 [SCHEDULER_SYSTEMD] = {
2464 .name = "systemctl",
2465 .is_available = is_systemd_timer_available,
2466 .update_schedule = systemd_timer_update_schedule,
2468 [SCHEDULER_LAUNCHCTL] = {
2469 .name = "launchctl",
2470 .is_available = is_launchctl_available,
2471 .update_schedule = launchctl_update_schedule,
2473 [SCHEDULER_SCHTASKS] = {
2474 .name = "schtasks",
2475 .is_available = is_schtasks_available,
2476 .update_schedule = schtasks_update_schedule,
2480 static enum scheduler parse_scheduler(const char *value)
2482 if (!value)
2483 return SCHEDULER_INVALID;
2484 else if (!strcasecmp(value, "auto"))
2485 return SCHEDULER_AUTO;
2486 else if (!strcasecmp(value, "cron") || !strcasecmp(value, "crontab"))
2487 return SCHEDULER_CRON;
2488 else if (!strcasecmp(value, "systemd") ||
2489 !strcasecmp(value, "systemd-timer"))
2490 return SCHEDULER_SYSTEMD;
2491 else if (!strcasecmp(value, "launchctl"))
2492 return SCHEDULER_LAUNCHCTL;
2493 else if (!strcasecmp(value, "schtasks"))
2494 return SCHEDULER_SCHTASKS;
2495 else
2496 return SCHEDULER_INVALID;
2499 static int maintenance_opt_scheduler(const struct option *opt, const char *arg,
2500 int unset)
2502 enum scheduler *scheduler = opt->value;
2504 BUG_ON_OPT_NEG(unset);
2506 *scheduler = parse_scheduler(arg);
2507 if (*scheduler == SCHEDULER_INVALID)
2508 return error(_("unrecognized --scheduler argument '%s'"), arg);
2509 return 0;
2512 struct maintenance_start_opts {
2513 enum scheduler scheduler;
2516 static enum scheduler resolve_scheduler(enum scheduler scheduler)
2518 if (scheduler != SCHEDULER_AUTO)
2519 return scheduler;
2521 #if defined(__APPLE__)
2522 return SCHEDULER_LAUNCHCTL;
2524 #elif defined(GIT_WINDOWS_NATIVE)
2525 return SCHEDULER_SCHTASKS;
2527 #elif defined(__linux__)
2528 if (is_systemd_timer_available())
2529 return SCHEDULER_SYSTEMD;
2530 else if (is_crontab_available())
2531 return SCHEDULER_CRON;
2532 else
2533 die(_("neither systemd timers nor crontab are available"));
2535 #else
2536 return SCHEDULER_CRON;
2537 #endif
2540 static void validate_scheduler(enum scheduler scheduler)
2542 if (scheduler == SCHEDULER_INVALID)
2543 BUG("invalid scheduler");
2544 if (scheduler == SCHEDULER_AUTO)
2545 BUG("resolve_scheduler should have been called before");
2547 if (!scheduler_fn[scheduler].is_available())
2548 die(_("%s scheduler is not available"),
2549 scheduler_fn[scheduler].name);
2552 static int update_background_schedule(const struct maintenance_start_opts *opts,
2553 int enable)
2555 unsigned int i;
2556 int result = 0;
2557 struct lock_file lk;
2558 char *lock_path = xstrfmt("%s/schedule", the_repository->objects->odb->path);
2560 if (hold_lock_file_for_update(&lk, lock_path, LOCK_NO_DEREF) < 0) {
2561 free(lock_path);
2562 return error(_("another process is scheduling background maintenance"));
2565 for (i = 1; i < ARRAY_SIZE(scheduler_fn); i++) {
2566 if (enable && opts->scheduler == i)
2567 continue;
2568 if (!scheduler_fn[i].is_available())
2569 continue;
2570 scheduler_fn[i].update_schedule(0, get_lock_file_fd(&lk));
2573 if (enable)
2574 result = scheduler_fn[opts->scheduler].update_schedule(
2575 1, get_lock_file_fd(&lk));
2577 rollback_lock_file(&lk);
2579 free(lock_path);
2580 return result;
2583 static const char *const builtin_maintenance_start_usage[] = {
2584 N_("git maintenance start [--scheduler=<scheduler>]"),
2585 NULL
2588 static int maintenance_start(int argc, const char **argv, const char *prefix)
2590 struct maintenance_start_opts opts = { 0 };
2591 struct option options[] = {
2592 OPT_CALLBACK_F(
2593 0, "scheduler", &opts.scheduler, N_("scheduler"),
2594 N_("scheduler to trigger git maintenance run"),
2595 PARSE_OPT_NONEG, maintenance_opt_scheduler),
2596 OPT_END()
2598 const char *register_args[] = { "register", NULL };
2600 argc = parse_options(argc, argv, prefix, options,
2601 builtin_maintenance_start_usage, 0);
2602 if (argc)
2603 usage_with_options(builtin_maintenance_start_usage, options);
2605 opts.scheduler = resolve_scheduler(opts.scheduler);
2606 validate_scheduler(opts.scheduler);
2608 if (maintenance_register(ARRAY_SIZE(register_args)-1, register_args, NULL))
2609 warning(_("failed to add repo to global config"));
2610 return update_background_schedule(&opts, 1);
2613 static const char *const builtin_maintenance_stop_usage[] = {
2614 "git maintenance stop",
2615 NULL
2618 static int maintenance_stop(int argc, const char **argv, const char *prefix)
2620 struct option options[] = {
2621 OPT_END()
2623 argc = parse_options(argc, argv, prefix, options,
2624 builtin_maintenance_stop_usage, 0);
2625 if (argc)
2626 usage_with_options(builtin_maintenance_stop_usage, options);
2627 return update_background_schedule(NULL, 0);
2630 static const char * const builtin_maintenance_usage[] = {
2631 N_("git maintenance <subcommand> [<options>]"),
2632 NULL,
2635 int cmd_maintenance(int argc, const char **argv, const char *prefix)
2637 parse_opt_subcommand_fn *fn = NULL;
2638 struct option builtin_maintenance_options[] = {
2639 OPT_SUBCOMMAND("run", &fn, maintenance_run),
2640 OPT_SUBCOMMAND("start", &fn, maintenance_start),
2641 OPT_SUBCOMMAND("stop", &fn, maintenance_stop),
2642 OPT_SUBCOMMAND("register", &fn, maintenance_register),
2643 OPT_SUBCOMMAND("unregister", &fn, maintenance_unregister),
2644 OPT_END(),
2647 argc = parse_options(argc, argv, prefix, builtin_maintenance_options,
2648 builtin_maintenance_usage, 0);
2649 return fn(argc, argv, prefix);