documentation: fix apostrophe usage
[git.git] / builtin / gc.c
blob00192ae5d32162229dee52cde1a77a6846c75443
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 "date.h"
16 #include "environment.h"
17 #include "hex.h"
18 #include "repository.h"
19 #include "config.h"
20 #include "tempfile.h"
21 #include "lockfile.h"
22 #include "parse-options.h"
23 #include "run-command.h"
24 #include "sigchain.h"
25 #include "strvec.h"
26 #include "commit.h"
27 #include "commit-graph.h"
28 #include "packfile.h"
29 #include "object-file.h"
30 #include "object-store-ll.h"
31 #include "pack.h"
32 #include "pack-objects.h"
33 #include "path.h"
34 #include "blob.h"
35 #include "tree.h"
36 #include "promisor-remote.h"
37 #include "refs.h"
38 #include "remote.h"
39 #include "exec-cmd.h"
40 #include "gettext.h"
41 #include "hook.h"
42 #include "setup.h"
43 #include "trace2.h"
45 #define FAILED_RUN "failed to run %s"
47 static const char * const builtin_gc_usage[] = {
48 N_("git gc [<options>]"),
49 NULL
52 static int pack_refs = 1;
53 static int prune_reflogs = 1;
54 static int cruft_packs = 1;
55 static int aggressive_depth = 50;
56 static int aggressive_window = 250;
57 static int gc_auto_threshold = 6700;
58 static int gc_auto_pack_limit = 50;
59 static int detach_auto = 1;
60 static timestamp_t gc_log_expire_time;
61 static const char *gc_log_expire = "1.day.ago";
62 static const char *prune_expire = "2.weeks.ago";
63 static const char *prune_worktrees_expire = "3.months.ago";
64 static unsigned long big_pack_threshold;
65 static unsigned long max_delta_cache_size = DEFAULT_DELTA_CACHE_SIZE;
67 static struct strvec reflog = STRVEC_INIT;
68 static struct strvec repack = STRVEC_INIT;
69 static struct strvec prune = STRVEC_INIT;
70 static struct strvec prune_worktrees = STRVEC_INIT;
71 static struct strvec rerere = STRVEC_INIT;
73 static struct tempfile *pidfile;
74 static struct lock_file log_lock;
76 static struct string_list pack_garbage = STRING_LIST_INIT_DUP;
78 static void clean_pack_garbage(void)
80 int i;
81 for (i = 0; i < pack_garbage.nr; i++)
82 unlink_or_warn(pack_garbage.items[i].string);
83 string_list_clear(&pack_garbage, 0);
86 static void report_pack_garbage(unsigned seen_bits, const char *path)
88 if (seen_bits == PACKDIR_FILE_IDX)
89 string_list_append(&pack_garbage, path);
92 static void process_log_file(void)
94 struct stat st;
95 if (fstat(get_lock_file_fd(&log_lock), &st)) {
97 * Perhaps there was an i/o error or another
98 * unlikely situation. Try to make a note of
99 * this in gc.log along with any existing
100 * messages.
102 int saved_errno = errno;
103 fprintf(stderr, _("Failed to fstat %s: %s"),
104 get_lock_file_path(&log_lock),
105 strerror(saved_errno));
106 fflush(stderr);
107 commit_lock_file(&log_lock);
108 errno = saved_errno;
109 } else if (st.st_size) {
110 /* There was some error recorded in the lock file */
111 commit_lock_file(&log_lock);
112 } else {
113 /* No error, clean up any old gc.log */
114 unlink(git_path("gc.log"));
115 rollback_lock_file(&log_lock);
119 static void process_log_file_at_exit(void)
121 fflush(stderr);
122 process_log_file();
125 static void process_log_file_on_signal(int signo)
127 process_log_file();
128 sigchain_pop(signo);
129 raise(signo);
132 static int gc_config_is_timestamp_never(const char *var)
134 const char *value;
135 timestamp_t expire;
137 if (!git_config_get_value(var, &value) && value) {
138 if (parse_expiry_date(value, &expire))
139 die(_("failed to parse '%s' value '%s'"), var, value);
140 return expire == 0;
142 return 0;
145 static void gc_config(void)
147 const char *value;
149 if (!git_config_get_value("gc.packrefs", &value)) {
150 if (value && !strcmp(value, "notbare"))
151 pack_refs = -1;
152 else
153 pack_refs = git_config_bool("gc.packrefs", value);
156 if (gc_config_is_timestamp_never("gc.reflogexpire") &&
157 gc_config_is_timestamp_never("gc.reflogexpireunreachable"))
158 prune_reflogs = 0;
160 git_config_get_int("gc.aggressivewindow", &aggressive_window);
161 git_config_get_int("gc.aggressivedepth", &aggressive_depth);
162 git_config_get_int("gc.auto", &gc_auto_threshold);
163 git_config_get_int("gc.autopacklimit", &gc_auto_pack_limit);
164 git_config_get_bool("gc.autodetach", &detach_auto);
165 git_config_get_bool("gc.cruftpacks", &cruft_packs);
166 git_config_get_expiry("gc.pruneexpire", &prune_expire);
167 git_config_get_expiry("gc.worktreepruneexpire", &prune_worktrees_expire);
168 git_config_get_expiry("gc.logexpiry", &gc_log_expire);
170 git_config_get_ulong("gc.bigpackthreshold", &big_pack_threshold);
171 git_config_get_ulong("pack.deltacachesize", &max_delta_cache_size);
173 git_config(git_default_config, NULL);
176 struct maintenance_run_opts;
177 static int maintenance_task_pack_refs(MAYBE_UNUSED struct maintenance_run_opts *opts)
179 struct child_process cmd = CHILD_PROCESS_INIT;
181 cmd.git_cmd = 1;
182 strvec_pushl(&cmd.args, "pack-refs", "--all", "--prune", NULL);
183 return run_command(&cmd);
186 static int too_many_loose_objects(void)
189 * Quickly check if a "gc" is needed, by estimating how
190 * many loose objects there are. Because SHA-1 is evenly
191 * distributed, we can check only one and get a reasonable
192 * estimate.
194 DIR *dir;
195 struct dirent *ent;
196 int auto_threshold;
197 int num_loose = 0;
198 int needed = 0;
199 const unsigned hexsz_loose = the_hash_algo->hexsz - 2;
201 dir = opendir(git_path("objects/17"));
202 if (!dir)
203 return 0;
205 auto_threshold = DIV_ROUND_UP(gc_auto_threshold, 256);
206 while ((ent = readdir(dir)) != NULL) {
207 if (strspn(ent->d_name, "0123456789abcdef") != hexsz_loose ||
208 ent->d_name[hexsz_loose] != '\0')
209 continue;
210 if (++num_loose > auto_threshold) {
211 needed = 1;
212 break;
215 closedir(dir);
216 return needed;
219 static struct packed_git *find_base_packs(struct string_list *packs,
220 unsigned long limit)
222 struct packed_git *p, *base = NULL;
224 for (p = get_all_packs(the_repository); p; p = p->next) {
225 if (!p->pack_local || p->is_cruft)
226 continue;
227 if (limit) {
228 if (p->pack_size >= limit)
229 string_list_append(packs, p->pack_name);
230 } else if (!base || base->pack_size < p->pack_size) {
231 base = p;
235 if (base)
236 string_list_append(packs, base->pack_name);
238 return base;
241 static int too_many_packs(void)
243 struct packed_git *p;
244 int cnt;
246 if (gc_auto_pack_limit <= 0)
247 return 0;
249 for (cnt = 0, p = get_all_packs(the_repository); p; p = p->next) {
250 if (!p->pack_local)
251 continue;
252 if (p->pack_keep)
253 continue;
255 * Perhaps check the size of the pack and count only
256 * very small ones here?
258 cnt++;
260 return gc_auto_pack_limit < cnt;
263 static uint64_t total_ram(void)
265 #if defined(HAVE_SYSINFO)
266 struct sysinfo si;
268 if (!sysinfo(&si))
269 return si.totalram;
270 #elif defined(HAVE_BSD_SYSCTL) && (defined(HW_MEMSIZE) || defined(HW_PHYSMEM))
271 int64_t physical_memory;
272 int mib[2];
273 size_t length;
275 mib[0] = CTL_HW;
276 # if defined(HW_MEMSIZE)
277 mib[1] = HW_MEMSIZE;
278 # else
279 mib[1] = HW_PHYSMEM;
280 # endif
281 length = sizeof(int64_t);
282 if (!sysctl(mib, 2, &physical_memory, &length, NULL, 0))
283 return physical_memory;
284 #elif defined(GIT_WINDOWS_NATIVE)
285 MEMORYSTATUSEX memInfo;
287 memInfo.dwLength = sizeof(MEMORYSTATUSEX);
288 if (GlobalMemoryStatusEx(&memInfo))
289 return memInfo.ullTotalPhys;
290 #endif
291 return 0;
294 static uint64_t estimate_repack_memory(struct packed_git *pack)
296 unsigned long nr_objects = repo_approximate_object_count(the_repository);
297 size_t os_cache, heap;
299 if (!pack || !nr_objects)
300 return 0;
303 * First we have to scan through at least one pack.
304 * Assume enough room in OS file cache to keep the entire pack
305 * or we may accidentally evict data of other processes from
306 * the cache.
308 os_cache = pack->pack_size + pack->index_size;
309 /* then pack-objects needs lots more for book keeping */
310 heap = sizeof(struct object_entry) * nr_objects;
312 * internal rev-list --all --objects takes up some memory too,
313 * let's say half of it is for blobs
315 heap += sizeof(struct blob) * nr_objects / 2;
317 * and the other half is for trees (commits and tags are
318 * usually insignificant)
320 heap += sizeof(struct tree) * nr_objects / 2;
321 /* and then obj_hash[], underestimated in fact */
322 heap += sizeof(struct object *) * nr_objects;
323 /* revindex is used also */
324 heap += (sizeof(off_t) + sizeof(uint32_t)) * nr_objects;
326 * read_sha1_file() (either at delta calculation phase, or
327 * writing phase) also fills up the delta base cache
329 heap += delta_base_cache_limit;
330 /* and of course pack-objects has its own delta cache */
331 heap += max_delta_cache_size;
333 return os_cache + heap;
336 static int keep_one_pack(struct string_list_item *item, void *data UNUSED)
338 strvec_pushf(&repack, "--keep-pack=%s", basename(item->string));
339 return 0;
342 static void add_repack_all_option(struct string_list *keep_pack)
344 if (prune_expire && !strcmp(prune_expire, "now"))
345 strvec_push(&repack, "-a");
346 else if (cruft_packs) {
347 strvec_push(&repack, "--cruft");
348 if (prune_expire)
349 strvec_pushf(&repack, "--cruft-expiration=%s", prune_expire);
350 } else {
351 strvec_push(&repack, "-A");
352 if (prune_expire)
353 strvec_pushf(&repack, "--unpack-unreachable=%s", prune_expire);
356 if (keep_pack)
357 for_each_string_list(keep_pack, keep_one_pack, NULL);
360 static void add_repack_incremental_option(void)
362 strvec_push(&repack, "--no-write-bitmap-index");
365 static int need_to_gc(void)
368 * Setting gc.auto to 0 or negative can disable the
369 * automatic gc.
371 if (gc_auto_threshold <= 0)
372 return 0;
375 * If there are too many loose objects, but not too many
376 * packs, we run "repack -d -l". If there are too many packs,
377 * we run "repack -A -d -l". Otherwise we tell the caller
378 * there is no need.
380 if (too_many_packs()) {
381 struct string_list keep_pack = STRING_LIST_INIT_NODUP;
383 if (big_pack_threshold) {
384 find_base_packs(&keep_pack, big_pack_threshold);
385 if (keep_pack.nr >= gc_auto_pack_limit) {
386 big_pack_threshold = 0;
387 string_list_clear(&keep_pack, 0);
388 find_base_packs(&keep_pack, 0);
390 } else {
391 struct packed_git *p = find_base_packs(&keep_pack, 0);
392 uint64_t mem_have, mem_want;
394 mem_have = total_ram();
395 mem_want = estimate_repack_memory(p);
398 * Only allow 1/2 of memory for pack-objects, leave
399 * the rest for the OS and other processes in the
400 * system.
402 if (!mem_have || mem_want < mem_have / 2)
403 string_list_clear(&keep_pack, 0);
406 add_repack_all_option(&keep_pack);
407 string_list_clear(&keep_pack, 0);
408 } else if (too_many_loose_objects())
409 add_repack_incremental_option();
410 else
411 return 0;
413 if (run_hooks("pre-auto-gc"))
414 return 0;
415 return 1;
418 /* return NULL on success, else hostname running the gc */
419 static const char *lock_repo_for_gc(int force, pid_t* ret_pid)
421 struct lock_file lock = LOCK_INIT;
422 char my_host[HOST_NAME_MAX + 1];
423 struct strbuf sb = STRBUF_INIT;
424 struct stat st;
425 uintmax_t pid;
426 FILE *fp;
427 int fd;
428 char *pidfile_path;
430 if (is_tempfile_active(pidfile))
431 /* already locked */
432 return NULL;
434 if (xgethostname(my_host, sizeof(my_host)))
435 xsnprintf(my_host, sizeof(my_host), "unknown");
437 pidfile_path = git_pathdup("gc.pid");
438 fd = hold_lock_file_for_update(&lock, pidfile_path,
439 LOCK_DIE_ON_ERROR);
440 if (!force) {
441 static char locking_host[HOST_NAME_MAX + 1];
442 static char *scan_fmt;
443 int should_exit;
445 if (!scan_fmt)
446 scan_fmt = xstrfmt("%s %%%ds", "%"SCNuMAX, HOST_NAME_MAX);
447 fp = fopen(pidfile_path, "r");
448 memset(locking_host, 0, sizeof(locking_host));
449 should_exit =
450 fp != NULL &&
451 !fstat(fileno(fp), &st) &&
453 * 12 hour limit is very generous as gc should
454 * never take that long. On the other hand we
455 * don't really need a strict limit here,
456 * running gc --auto one day late is not a big
457 * problem. --force can be used in manual gc
458 * after the user verifies that no gc is
459 * running.
461 time(NULL) - st.st_mtime <= 12 * 3600 &&
462 fscanf(fp, scan_fmt, &pid, locking_host) == 2 &&
463 /* be gentle to concurrent "gc" on remote hosts */
464 (strcmp(locking_host, my_host) || !kill(pid, 0) || errno == EPERM);
465 if (fp)
466 fclose(fp);
467 if (should_exit) {
468 if (fd >= 0)
469 rollback_lock_file(&lock);
470 *ret_pid = pid;
471 free(pidfile_path);
472 return locking_host;
476 strbuf_addf(&sb, "%"PRIuMAX" %s",
477 (uintmax_t) getpid(), my_host);
478 write_in_full(fd, sb.buf, sb.len);
479 strbuf_release(&sb);
480 commit_lock_file(&lock);
481 pidfile = register_tempfile(pidfile_path);
482 free(pidfile_path);
483 return NULL;
487 * Returns 0 if there was no previous error and gc can proceed, 1 if
488 * gc should not proceed due to an error in the last run. Prints a
489 * message and returns with a non-[01] status code if an error occurred
490 * while reading gc.log
492 static int report_last_gc_error(void)
494 struct strbuf sb = STRBUF_INIT;
495 int ret = 0;
496 ssize_t len;
497 struct stat st;
498 char *gc_log_path = git_pathdup("gc.log");
500 if (stat(gc_log_path, &st)) {
501 if (errno == ENOENT)
502 goto done;
504 ret = die_message_errno(_("cannot stat '%s'"), gc_log_path);
505 goto done;
508 if (st.st_mtime < gc_log_expire_time)
509 goto done;
511 len = strbuf_read_file(&sb, gc_log_path, 0);
512 if (len < 0)
513 ret = die_message_errno(_("cannot read '%s'"), gc_log_path);
514 else if (len > 0) {
516 * A previous gc failed. Report the error, and don't
517 * bother with an automatic gc run since it is likely
518 * to fail in the same way.
520 warning(_("The last gc run reported the following. "
521 "Please correct the root cause\n"
522 "and remove %s\n"
523 "Automatic cleanup will not be performed "
524 "until the file is removed.\n\n"
525 "%s"),
526 gc_log_path, sb.buf);
527 ret = 1;
529 strbuf_release(&sb);
530 done:
531 free(gc_log_path);
532 return ret;
535 static void gc_before_repack(void)
538 * We may be called twice, as both the pre- and
539 * post-daemonized phases will call us, but running these
540 * commands more than once is pointless and wasteful.
542 static int done = 0;
543 if (done++)
544 return;
546 if (pack_refs && maintenance_task_pack_refs(NULL))
547 die(FAILED_RUN, "pack-refs");
549 if (prune_reflogs) {
550 struct child_process cmd = CHILD_PROCESS_INIT;
552 cmd.git_cmd = 1;
553 strvec_pushv(&cmd.args, reflog.v);
554 if (run_command(&cmd))
555 die(FAILED_RUN, reflog.v[0]);
559 int cmd_gc(int argc, const char **argv, const char *prefix)
561 int aggressive = 0;
562 int auto_gc = 0;
563 int quiet = 0;
564 int force = 0;
565 const char *name;
566 pid_t pid;
567 int daemonized = 0;
568 int keep_largest_pack = -1;
569 timestamp_t dummy;
570 struct child_process rerere_cmd = CHILD_PROCESS_INIT;
572 struct option builtin_gc_options[] = {
573 OPT__QUIET(&quiet, N_("suppress progress reporting")),
574 { OPTION_STRING, 0, "prune", &prune_expire, N_("date"),
575 N_("prune unreferenced objects"),
576 PARSE_OPT_OPTARG, NULL, (intptr_t)prune_expire },
577 OPT_BOOL(0, "cruft", &cruft_packs, N_("pack unreferenced objects separately")),
578 OPT_BOOL(0, "aggressive", &aggressive, N_("be more thorough (increased runtime)")),
579 OPT_BOOL_F(0, "auto", &auto_gc, N_("enable auto-gc mode"),
580 PARSE_OPT_NOCOMPLETE),
581 OPT_BOOL_F(0, "force", &force,
582 N_("force running gc even if there may be another gc running"),
583 PARSE_OPT_NOCOMPLETE),
584 OPT_BOOL(0, "keep-largest-pack", &keep_largest_pack,
585 N_("repack all other packs except the largest pack")),
586 OPT_END()
589 if (argc == 2 && !strcmp(argv[1], "-h"))
590 usage_with_options(builtin_gc_usage, builtin_gc_options);
592 strvec_pushl(&reflog, "reflog", "expire", "--all", NULL);
593 strvec_pushl(&repack, "repack", "-d", "-l", NULL);
594 strvec_pushl(&prune, "prune", "--expire", NULL);
595 strvec_pushl(&prune_worktrees, "worktree", "prune", "--expire", NULL);
596 strvec_pushl(&rerere, "rerere", "gc", NULL);
598 /* default expiry time, overwritten in gc_config */
599 gc_config();
600 if (parse_expiry_date(gc_log_expire, &gc_log_expire_time))
601 die(_("failed to parse gc.logExpiry value %s"), gc_log_expire);
603 if (pack_refs < 0)
604 pack_refs = !is_bare_repository();
606 argc = parse_options(argc, argv, prefix, builtin_gc_options,
607 builtin_gc_usage, 0);
608 if (argc > 0)
609 usage_with_options(builtin_gc_usage, builtin_gc_options);
611 if (prune_expire && parse_expiry_date(prune_expire, &dummy))
612 die(_("failed to parse prune expiry value %s"), prune_expire);
614 if (aggressive) {
615 strvec_push(&repack, "-f");
616 if (aggressive_depth > 0)
617 strvec_pushf(&repack, "--depth=%d", aggressive_depth);
618 if (aggressive_window > 0)
619 strvec_pushf(&repack, "--window=%d", aggressive_window);
621 if (quiet)
622 strvec_push(&repack, "-q");
624 if (auto_gc) {
626 * Auto-gc should be least intrusive as possible.
628 if (!need_to_gc())
629 return 0;
630 if (!quiet) {
631 if (detach_auto)
632 fprintf(stderr, _("Auto packing the repository in background for optimum performance.\n"));
633 else
634 fprintf(stderr, _("Auto packing the repository for optimum performance.\n"));
635 fprintf(stderr, _("See \"git help gc\" for manual housekeeping.\n"));
637 if (detach_auto) {
638 int ret = report_last_gc_error();
640 if (ret == 1)
641 /* Last gc --auto failed. Skip this one. */
642 return 0;
643 else if (ret)
644 /* an I/O error occurred, already reported */
645 return ret;
647 if (lock_repo_for_gc(force, &pid))
648 return 0;
649 gc_before_repack(); /* dies on failure */
650 delete_tempfile(&pidfile);
653 * failure to daemonize is ok, we'll continue
654 * in foreground
656 daemonized = !daemonize();
658 } else {
659 struct string_list keep_pack = STRING_LIST_INIT_NODUP;
661 if (keep_largest_pack != -1) {
662 if (keep_largest_pack)
663 find_base_packs(&keep_pack, 0);
664 } else if (big_pack_threshold) {
665 find_base_packs(&keep_pack, big_pack_threshold);
668 add_repack_all_option(&keep_pack);
669 string_list_clear(&keep_pack, 0);
672 name = lock_repo_for_gc(force, &pid);
673 if (name) {
674 if (auto_gc)
675 return 0; /* be quiet on --auto */
676 die(_("gc is already running on machine '%s' pid %"PRIuMAX" (use --force if not)"),
677 name, (uintmax_t)pid);
680 if (daemonized) {
681 hold_lock_file_for_update(&log_lock,
682 git_path("gc.log"),
683 LOCK_DIE_ON_ERROR);
684 dup2(get_lock_file_fd(&log_lock), 2);
685 sigchain_push_common(process_log_file_on_signal);
686 atexit(process_log_file_at_exit);
689 gc_before_repack();
691 if (!repository_format_precious_objects) {
692 struct child_process repack_cmd = CHILD_PROCESS_INIT;
694 repack_cmd.git_cmd = 1;
695 repack_cmd.close_object_store = 1;
696 strvec_pushv(&repack_cmd.args, repack.v);
697 if (run_command(&repack_cmd))
698 die(FAILED_RUN, repack.v[0]);
700 if (prune_expire) {
701 struct child_process prune_cmd = CHILD_PROCESS_INIT;
703 /* run `git prune` even if using cruft packs */
704 strvec_push(&prune, prune_expire);
705 if (quiet)
706 strvec_push(&prune, "--no-progress");
707 if (repo_has_promisor_remote(the_repository))
708 strvec_push(&prune,
709 "--exclude-promisor-objects");
710 prune_cmd.git_cmd = 1;
711 strvec_pushv(&prune_cmd.args, prune.v);
712 if (run_command(&prune_cmd))
713 die(FAILED_RUN, prune.v[0]);
717 if (prune_worktrees_expire) {
718 struct child_process prune_worktrees_cmd = CHILD_PROCESS_INIT;
720 strvec_push(&prune_worktrees, prune_worktrees_expire);
721 prune_worktrees_cmd.git_cmd = 1;
722 strvec_pushv(&prune_worktrees_cmd.args, prune_worktrees.v);
723 if (run_command(&prune_worktrees_cmd))
724 die(FAILED_RUN, prune_worktrees.v[0]);
727 rerere_cmd.git_cmd = 1;
728 strvec_pushv(&rerere_cmd.args, rerere.v);
729 if (run_command(&rerere_cmd))
730 die(FAILED_RUN, rerere.v[0]);
732 report_garbage = report_pack_garbage;
733 reprepare_packed_git(the_repository);
734 if (pack_garbage.nr > 0) {
735 close_object_store(the_repository->objects);
736 clean_pack_garbage();
739 if (the_repository->settings.gc_write_commit_graph == 1)
740 write_commit_graph_reachable(the_repository->objects->odb,
741 !quiet && !daemonized ? COMMIT_GRAPH_WRITE_PROGRESS : 0,
742 NULL);
744 if (auto_gc && too_many_loose_objects())
745 warning(_("There are too many unreachable loose objects; "
746 "run 'git prune' to remove them."));
748 if (!daemonized)
749 unlink(git_path("gc.log"));
751 return 0;
754 static const char *const builtin_maintenance_run_usage[] = {
755 N_("git maintenance run [--auto] [--[no-]quiet] [--task=<task>] [--schedule]"),
756 NULL
759 enum schedule_priority {
760 SCHEDULE_NONE = 0,
761 SCHEDULE_WEEKLY = 1,
762 SCHEDULE_DAILY = 2,
763 SCHEDULE_HOURLY = 3,
766 static enum schedule_priority parse_schedule(const char *value)
768 if (!value)
769 return SCHEDULE_NONE;
770 if (!strcasecmp(value, "hourly"))
771 return SCHEDULE_HOURLY;
772 if (!strcasecmp(value, "daily"))
773 return SCHEDULE_DAILY;
774 if (!strcasecmp(value, "weekly"))
775 return SCHEDULE_WEEKLY;
776 return SCHEDULE_NONE;
779 static int maintenance_opt_schedule(const struct option *opt, const char *arg,
780 int unset)
782 enum schedule_priority *priority = opt->value;
784 if (unset)
785 die(_("--no-schedule is not allowed"));
787 *priority = parse_schedule(arg);
789 if (!*priority)
790 die(_("unrecognized --schedule argument '%s'"), arg);
792 return 0;
795 struct maintenance_run_opts {
796 int auto_flag;
797 int quiet;
798 enum schedule_priority schedule;
801 /* Remember to update object flag allocation in object.h */
802 #define SEEN (1u<<0)
804 struct cg_auto_data {
805 int num_not_in_graph;
806 int limit;
809 static int dfs_on_ref(const char *refname UNUSED,
810 const struct object_id *oid,
811 int flags UNUSED,
812 void *cb_data)
814 struct cg_auto_data *data = (struct cg_auto_data *)cb_data;
815 int result = 0;
816 struct object_id peeled;
817 struct commit_list *stack = NULL;
818 struct commit *commit;
820 if (!peel_iterated_oid(oid, &peeled))
821 oid = &peeled;
822 if (oid_object_info(the_repository, oid, NULL) != OBJ_COMMIT)
823 return 0;
825 commit = lookup_commit(the_repository, oid);
826 if (!commit)
827 return 0;
828 if (repo_parse_commit(the_repository, commit) ||
829 commit_graph_position(commit) != COMMIT_NOT_FROM_GRAPH)
830 return 0;
832 data->num_not_in_graph++;
834 if (data->num_not_in_graph >= data->limit)
835 return 1;
837 commit_list_append(commit, &stack);
839 while (!result && stack) {
840 struct commit_list *parent;
842 commit = pop_commit(&stack);
844 for (parent = commit->parents; parent; parent = parent->next) {
845 if (repo_parse_commit(the_repository, parent->item) ||
846 commit_graph_position(parent->item) != COMMIT_NOT_FROM_GRAPH ||
847 parent->item->object.flags & SEEN)
848 continue;
850 parent->item->object.flags |= SEEN;
851 data->num_not_in_graph++;
853 if (data->num_not_in_graph >= data->limit) {
854 result = 1;
855 break;
858 commit_list_append(parent->item, &stack);
862 free_commit_list(stack);
863 return result;
866 static int should_write_commit_graph(void)
868 int result;
869 struct cg_auto_data data;
871 data.num_not_in_graph = 0;
872 data.limit = 100;
873 git_config_get_int("maintenance.commit-graph.auto",
874 &data.limit);
876 if (!data.limit)
877 return 0;
878 if (data.limit < 0)
879 return 1;
881 result = for_each_ref(dfs_on_ref, &data);
883 repo_clear_commit_marks(the_repository, SEEN);
885 return result;
888 static int run_write_commit_graph(struct maintenance_run_opts *opts)
890 struct child_process child = CHILD_PROCESS_INIT;
892 child.git_cmd = child.close_object_store = 1;
893 strvec_pushl(&child.args, "commit-graph", "write",
894 "--split", "--reachable", NULL);
896 if (opts->quiet)
897 strvec_push(&child.args, "--no-progress");
899 return !!run_command(&child);
902 static int maintenance_task_commit_graph(struct maintenance_run_opts *opts)
904 prepare_repo_settings(the_repository);
905 if (!the_repository->settings.core_commit_graph)
906 return 0;
908 if (run_write_commit_graph(opts)) {
909 error(_("failed to write commit-graph"));
910 return 1;
913 return 0;
916 static int fetch_remote(struct remote *remote, void *cbdata)
918 struct maintenance_run_opts *opts = cbdata;
919 struct child_process child = CHILD_PROCESS_INIT;
921 if (remote->skip_default_update)
922 return 0;
924 child.git_cmd = 1;
925 strvec_pushl(&child.args, "fetch", remote->name,
926 "--prefetch", "--prune", "--no-tags",
927 "--no-write-fetch-head", "--recurse-submodules=no",
928 NULL);
930 if (opts->quiet)
931 strvec_push(&child.args, "--quiet");
933 return !!run_command(&child);
936 static int maintenance_task_prefetch(struct maintenance_run_opts *opts)
938 if (for_each_remote(fetch_remote, opts)) {
939 error(_("failed to prefetch remotes"));
940 return 1;
943 return 0;
946 static int maintenance_task_gc(struct maintenance_run_opts *opts)
948 struct child_process child = CHILD_PROCESS_INIT;
950 child.git_cmd = child.close_object_store = 1;
951 strvec_push(&child.args, "gc");
953 if (opts->auto_flag)
954 strvec_push(&child.args, "--auto");
955 if (opts->quiet)
956 strvec_push(&child.args, "--quiet");
957 else
958 strvec_push(&child.args, "--no-quiet");
960 return run_command(&child);
963 static int prune_packed(struct maintenance_run_opts *opts)
965 struct child_process child = CHILD_PROCESS_INIT;
967 child.git_cmd = 1;
968 strvec_push(&child.args, "prune-packed");
970 if (opts->quiet)
971 strvec_push(&child.args, "--quiet");
973 return !!run_command(&child);
976 struct write_loose_object_data {
977 FILE *in;
978 int count;
979 int batch_size;
982 static int loose_object_auto_limit = 100;
984 static int loose_object_count(const struct object_id *oid UNUSED,
985 const char *path UNUSED,
986 void *data)
988 int *count = (int*)data;
989 if (++(*count) >= loose_object_auto_limit)
990 return 1;
991 return 0;
994 static int loose_object_auto_condition(void)
996 int count = 0;
998 git_config_get_int("maintenance.loose-objects.auto",
999 &loose_object_auto_limit);
1001 if (!loose_object_auto_limit)
1002 return 0;
1003 if (loose_object_auto_limit < 0)
1004 return 1;
1006 return for_each_loose_file_in_objdir(the_repository->objects->odb->path,
1007 loose_object_count,
1008 NULL, NULL, &count);
1011 static int bail_on_loose(const struct object_id *oid UNUSED,
1012 const char *path UNUSED,
1013 void *data UNUSED)
1015 return 1;
1018 static int write_loose_object_to_stdin(const struct object_id *oid,
1019 const char *path UNUSED,
1020 void *data)
1022 struct write_loose_object_data *d = (struct write_loose_object_data *)data;
1024 fprintf(d->in, "%s\n", oid_to_hex(oid));
1026 return ++(d->count) > d->batch_size;
1029 static int pack_loose(struct maintenance_run_opts *opts)
1031 struct repository *r = the_repository;
1032 int result = 0;
1033 struct write_loose_object_data data;
1034 struct child_process pack_proc = CHILD_PROCESS_INIT;
1037 * Do not start pack-objects process
1038 * if there are no loose objects.
1040 if (!for_each_loose_file_in_objdir(r->objects->odb->path,
1041 bail_on_loose,
1042 NULL, NULL, NULL))
1043 return 0;
1045 pack_proc.git_cmd = 1;
1047 strvec_push(&pack_proc.args, "pack-objects");
1048 if (opts->quiet)
1049 strvec_push(&pack_proc.args, "--quiet");
1050 strvec_pushf(&pack_proc.args, "%s/pack/loose", r->objects->odb->path);
1052 pack_proc.in = -1;
1054 if (start_command(&pack_proc)) {
1055 error(_("failed to start 'git pack-objects' process"));
1056 return 1;
1059 data.in = xfdopen(pack_proc.in, "w");
1060 data.count = 0;
1061 data.batch_size = 50000;
1063 for_each_loose_file_in_objdir(r->objects->odb->path,
1064 write_loose_object_to_stdin,
1065 NULL,
1066 NULL,
1067 &data);
1069 fclose(data.in);
1071 if (finish_command(&pack_proc)) {
1072 error(_("failed to finish 'git pack-objects' process"));
1073 result = 1;
1076 return result;
1079 static int maintenance_task_loose_objects(struct maintenance_run_opts *opts)
1081 return prune_packed(opts) || pack_loose(opts);
1084 static int incremental_repack_auto_condition(void)
1086 struct packed_git *p;
1087 int incremental_repack_auto_limit = 10;
1088 int count = 0;
1090 prepare_repo_settings(the_repository);
1091 if (!the_repository->settings.core_multi_pack_index)
1092 return 0;
1094 git_config_get_int("maintenance.incremental-repack.auto",
1095 &incremental_repack_auto_limit);
1097 if (!incremental_repack_auto_limit)
1098 return 0;
1099 if (incremental_repack_auto_limit < 0)
1100 return 1;
1102 for (p = get_packed_git(the_repository);
1103 count < incremental_repack_auto_limit && p;
1104 p = p->next) {
1105 if (!p->multi_pack_index)
1106 count++;
1109 return count >= incremental_repack_auto_limit;
1112 static int multi_pack_index_write(struct maintenance_run_opts *opts)
1114 struct child_process child = CHILD_PROCESS_INIT;
1116 child.git_cmd = 1;
1117 strvec_pushl(&child.args, "multi-pack-index", "write", NULL);
1119 if (opts->quiet)
1120 strvec_push(&child.args, "--no-progress");
1122 if (run_command(&child))
1123 return error(_("failed to write multi-pack-index"));
1125 return 0;
1128 static int multi_pack_index_expire(struct maintenance_run_opts *opts)
1130 struct child_process child = CHILD_PROCESS_INIT;
1132 child.git_cmd = child.close_object_store = 1;
1133 strvec_pushl(&child.args, "multi-pack-index", "expire", NULL);
1135 if (opts->quiet)
1136 strvec_push(&child.args, "--no-progress");
1138 if (run_command(&child))
1139 return error(_("'git multi-pack-index expire' failed"));
1141 return 0;
1144 #define TWO_GIGABYTES (INT32_MAX)
1146 static off_t get_auto_pack_size(void)
1149 * The "auto" value is special: we optimize for
1150 * one large pack-file (i.e. from a clone) and
1151 * expect the rest to be small and they can be
1152 * repacked quickly.
1154 * The strategy we select here is to select a
1155 * size that is one more than the second largest
1156 * pack-file. This ensures that we will repack
1157 * at least two packs if there are three or more
1158 * packs.
1160 off_t max_size = 0;
1161 off_t second_largest_size = 0;
1162 off_t result_size;
1163 struct packed_git *p;
1164 struct repository *r = the_repository;
1166 reprepare_packed_git(r);
1167 for (p = get_all_packs(r); p; p = p->next) {
1168 if (p->pack_size > max_size) {
1169 second_largest_size = max_size;
1170 max_size = p->pack_size;
1171 } else if (p->pack_size > second_largest_size)
1172 second_largest_size = p->pack_size;
1175 result_size = second_largest_size + 1;
1177 /* But limit ourselves to a batch size of 2g */
1178 if (result_size > TWO_GIGABYTES)
1179 result_size = TWO_GIGABYTES;
1181 return result_size;
1184 static int multi_pack_index_repack(struct maintenance_run_opts *opts)
1186 struct child_process child = CHILD_PROCESS_INIT;
1188 child.git_cmd = child.close_object_store = 1;
1189 strvec_pushl(&child.args, "multi-pack-index", "repack", NULL);
1191 if (opts->quiet)
1192 strvec_push(&child.args, "--no-progress");
1194 strvec_pushf(&child.args, "--batch-size=%"PRIuMAX,
1195 (uintmax_t)get_auto_pack_size());
1197 if (run_command(&child))
1198 return error(_("'git multi-pack-index repack' failed"));
1200 return 0;
1203 static int maintenance_task_incremental_repack(struct maintenance_run_opts *opts)
1205 prepare_repo_settings(the_repository);
1206 if (!the_repository->settings.core_multi_pack_index) {
1207 warning(_("skipping incremental-repack task because core.multiPackIndex is disabled"));
1208 return 0;
1211 if (multi_pack_index_write(opts))
1212 return 1;
1213 if (multi_pack_index_expire(opts))
1214 return 1;
1215 if (multi_pack_index_repack(opts))
1216 return 1;
1217 return 0;
1220 typedef int maintenance_task_fn(struct maintenance_run_opts *opts);
1223 * An auto condition function returns 1 if the task should run
1224 * and 0 if the task should NOT run. See needs_to_gc() for an
1225 * example.
1227 typedef int maintenance_auto_fn(void);
1229 struct maintenance_task {
1230 const char *name;
1231 maintenance_task_fn *fn;
1232 maintenance_auto_fn *auto_condition;
1233 unsigned enabled:1;
1235 enum schedule_priority schedule;
1237 /* -1 if not selected. */
1238 int selected_order;
1241 enum maintenance_task_label {
1242 TASK_PREFETCH,
1243 TASK_LOOSE_OBJECTS,
1244 TASK_INCREMENTAL_REPACK,
1245 TASK_GC,
1246 TASK_COMMIT_GRAPH,
1247 TASK_PACK_REFS,
1249 /* Leave as final value */
1250 TASK__COUNT
1253 static struct maintenance_task tasks[] = {
1254 [TASK_PREFETCH] = {
1255 "prefetch",
1256 maintenance_task_prefetch,
1258 [TASK_LOOSE_OBJECTS] = {
1259 "loose-objects",
1260 maintenance_task_loose_objects,
1261 loose_object_auto_condition,
1263 [TASK_INCREMENTAL_REPACK] = {
1264 "incremental-repack",
1265 maintenance_task_incremental_repack,
1266 incremental_repack_auto_condition,
1268 [TASK_GC] = {
1269 "gc",
1270 maintenance_task_gc,
1271 need_to_gc,
1274 [TASK_COMMIT_GRAPH] = {
1275 "commit-graph",
1276 maintenance_task_commit_graph,
1277 should_write_commit_graph,
1279 [TASK_PACK_REFS] = {
1280 "pack-refs",
1281 maintenance_task_pack_refs,
1282 NULL,
1286 static int compare_tasks_by_selection(const void *a_, const void *b_)
1288 const struct maintenance_task *a = a_;
1289 const struct maintenance_task *b = b_;
1291 return b->selected_order - a->selected_order;
1294 static int maintenance_run_tasks(struct maintenance_run_opts *opts)
1296 int i, found_selected = 0;
1297 int result = 0;
1298 struct lock_file lk;
1299 struct repository *r = the_repository;
1300 char *lock_path = xstrfmt("%s/maintenance", r->objects->odb->path);
1302 if (hold_lock_file_for_update(&lk, lock_path, LOCK_NO_DEREF) < 0) {
1304 * Another maintenance command is running.
1306 * If --auto was provided, then it is likely due to a
1307 * recursive process stack. Do not report an error in
1308 * that case.
1310 if (!opts->auto_flag && !opts->quiet)
1311 warning(_("lock file '%s' exists, skipping maintenance"),
1312 lock_path);
1313 free(lock_path);
1314 return 0;
1316 free(lock_path);
1318 for (i = 0; !found_selected && i < TASK__COUNT; i++)
1319 found_selected = tasks[i].selected_order >= 0;
1321 if (found_selected)
1322 QSORT(tasks, TASK__COUNT, compare_tasks_by_selection);
1324 for (i = 0; i < TASK__COUNT; i++) {
1325 if (found_selected && tasks[i].selected_order < 0)
1326 continue;
1328 if (!found_selected && !tasks[i].enabled)
1329 continue;
1331 if (opts->auto_flag &&
1332 (!tasks[i].auto_condition ||
1333 !tasks[i].auto_condition()))
1334 continue;
1336 if (opts->schedule && tasks[i].schedule < opts->schedule)
1337 continue;
1339 trace2_region_enter("maintenance", tasks[i].name, r);
1340 if (tasks[i].fn(opts)) {
1341 error(_("task '%s' failed"), tasks[i].name);
1342 result = 1;
1344 trace2_region_leave("maintenance", tasks[i].name, r);
1347 rollback_lock_file(&lk);
1348 return result;
1351 static void initialize_maintenance_strategy(void)
1353 char *config_str;
1355 if (git_config_get_string("maintenance.strategy", &config_str))
1356 return;
1358 if (!strcasecmp(config_str, "incremental")) {
1359 tasks[TASK_GC].schedule = SCHEDULE_NONE;
1360 tasks[TASK_COMMIT_GRAPH].enabled = 1;
1361 tasks[TASK_COMMIT_GRAPH].schedule = SCHEDULE_HOURLY;
1362 tasks[TASK_PREFETCH].enabled = 1;
1363 tasks[TASK_PREFETCH].schedule = SCHEDULE_HOURLY;
1364 tasks[TASK_INCREMENTAL_REPACK].enabled = 1;
1365 tasks[TASK_INCREMENTAL_REPACK].schedule = SCHEDULE_DAILY;
1366 tasks[TASK_LOOSE_OBJECTS].enabled = 1;
1367 tasks[TASK_LOOSE_OBJECTS].schedule = SCHEDULE_DAILY;
1368 tasks[TASK_PACK_REFS].enabled = 1;
1369 tasks[TASK_PACK_REFS].schedule = SCHEDULE_WEEKLY;
1373 static void initialize_task_config(int schedule)
1375 int i;
1376 struct strbuf config_name = STRBUF_INIT;
1377 gc_config();
1379 if (schedule)
1380 initialize_maintenance_strategy();
1382 for (i = 0; i < TASK__COUNT; i++) {
1383 int config_value;
1384 char *config_str;
1386 strbuf_reset(&config_name);
1387 strbuf_addf(&config_name, "maintenance.%s.enabled",
1388 tasks[i].name);
1390 if (!git_config_get_bool(config_name.buf, &config_value))
1391 tasks[i].enabled = config_value;
1393 strbuf_reset(&config_name);
1394 strbuf_addf(&config_name, "maintenance.%s.schedule",
1395 tasks[i].name);
1397 if (!git_config_get_string(config_name.buf, &config_str)) {
1398 tasks[i].schedule = parse_schedule(config_str);
1399 free(config_str);
1403 strbuf_release(&config_name);
1406 static int task_option_parse(const struct option *opt UNUSED,
1407 const char *arg, int unset)
1409 int i, num_selected = 0;
1410 struct maintenance_task *task = NULL;
1412 BUG_ON_OPT_NEG(unset);
1414 for (i = 0; i < TASK__COUNT; i++) {
1415 if (tasks[i].selected_order >= 0)
1416 num_selected++;
1417 if (!strcasecmp(tasks[i].name, arg)) {
1418 task = &tasks[i];
1422 if (!task) {
1423 error(_("'%s' is not a valid task"), arg);
1424 return 1;
1427 if (task->selected_order >= 0) {
1428 error(_("task '%s' cannot be selected multiple times"), arg);
1429 return 1;
1432 task->selected_order = num_selected + 1;
1434 return 0;
1437 static int maintenance_run(int argc, const char **argv, const char *prefix)
1439 int i;
1440 struct maintenance_run_opts opts;
1441 struct option builtin_maintenance_run_options[] = {
1442 OPT_BOOL(0, "auto", &opts.auto_flag,
1443 N_("run tasks based on the state of the repository")),
1444 OPT_CALLBACK(0, "schedule", &opts.schedule, N_("frequency"),
1445 N_("run tasks based on frequency"),
1446 maintenance_opt_schedule),
1447 OPT_BOOL(0, "quiet", &opts.quiet,
1448 N_("do not report progress or other information over stderr")),
1449 OPT_CALLBACK_F(0, "task", NULL, N_("task"),
1450 N_("run a specific task"),
1451 PARSE_OPT_NONEG, task_option_parse),
1452 OPT_END()
1454 memset(&opts, 0, sizeof(opts));
1456 opts.quiet = !isatty(2);
1458 for (i = 0; i < TASK__COUNT; i++)
1459 tasks[i].selected_order = -1;
1461 argc = parse_options(argc, argv, prefix,
1462 builtin_maintenance_run_options,
1463 builtin_maintenance_run_usage,
1464 PARSE_OPT_STOP_AT_NON_OPTION);
1466 if (opts.auto_flag && opts.schedule)
1467 die(_("use at most one of --auto and --schedule=<frequency>"));
1469 initialize_task_config(opts.schedule);
1471 if (argc != 0)
1472 usage_with_options(builtin_maintenance_run_usage,
1473 builtin_maintenance_run_options);
1474 return maintenance_run_tasks(&opts);
1477 static char *get_maintpath(void)
1479 struct strbuf sb = STRBUF_INIT;
1480 const char *p = the_repository->worktree ?
1481 the_repository->worktree : the_repository->gitdir;
1483 strbuf_realpath(&sb, p, 1);
1484 return strbuf_detach(&sb, NULL);
1487 static char const * const builtin_maintenance_register_usage[] = {
1488 "git maintenance register [--config-file <path>]",
1489 NULL
1492 static int maintenance_register(int argc, const char **argv, const char *prefix)
1494 char *config_file = NULL;
1495 struct option options[] = {
1496 OPT_STRING(0, "config-file", &config_file, N_("file"), N_("use given config file")),
1497 OPT_END(),
1499 int found = 0;
1500 const char *key = "maintenance.repo";
1501 char *maintpath = get_maintpath();
1502 struct string_list_item *item;
1503 const struct string_list *list;
1505 argc = parse_options(argc, argv, prefix, options,
1506 builtin_maintenance_register_usage, 0);
1507 if (argc)
1508 usage_with_options(builtin_maintenance_register_usage,
1509 options);
1511 /* Disable foreground maintenance */
1512 git_config_set("maintenance.auto", "false");
1514 /* Set maintenance strategy, if unset */
1515 if (git_config_get("maintenance.strategy"))
1516 git_config_set("maintenance.strategy", "incremental");
1518 if (!git_config_get_string_multi(key, &list)) {
1519 for_each_string_list_item(item, list) {
1520 if (!strcmp(maintpath, item->string)) {
1521 found = 1;
1522 break;
1527 if (!found) {
1528 int rc;
1529 char *user_config = NULL, *xdg_config = NULL;
1531 if (!config_file) {
1532 git_global_config(&user_config, &xdg_config);
1533 config_file = user_config;
1534 if (!user_config)
1535 die(_("$HOME not set"));
1537 rc = git_config_set_multivar_in_file_gently(
1538 config_file, "maintenance.repo", maintpath,
1539 CONFIG_REGEX_NONE, 0);
1540 free(user_config);
1541 free(xdg_config);
1543 if (rc)
1544 die(_("unable to add '%s' value of '%s'"),
1545 key, maintpath);
1548 free(maintpath);
1549 return 0;
1552 static char const * const builtin_maintenance_unregister_usage[] = {
1553 "git maintenance unregister [--config-file <path>] [--force]",
1554 NULL
1557 static int maintenance_unregister(int argc, const char **argv, const char *prefix)
1559 int force = 0;
1560 char *config_file = NULL;
1561 struct option options[] = {
1562 OPT_STRING(0, "config-file", &config_file, N_("file"), N_("use given config file")),
1563 OPT__FORCE(&force,
1564 N_("return success even if repository was not registered"),
1565 PARSE_OPT_NOCOMPLETE),
1566 OPT_END(),
1568 const char *key = "maintenance.repo";
1569 char *maintpath = get_maintpath();
1570 int found = 0;
1571 struct string_list_item *item;
1572 const struct string_list *list;
1573 struct config_set cs = { { 0 } };
1575 argc = parse_options(argc, argv, prefix, options,
1576 builtin_maintenance_unregister_usage, 0);
1577 if (argc)
1578 usage_with_options(builtin_maintenance_unregister_usage,
1579 options);
1581 if (config_file) {
1582 git_configset_init(&cs);
1583 git_configset_add_file(&cs, config_file);
1585 if (!(config_file
1586 ? git_configset_get_string_multi(&cs, key, &list)
1587 : git_config_get_string_multi(key, &list))) {
1588 for_each_string_list_item(item, list) {
1589 if (!strcmp(maintpath, item->string)) {
1590 found = 1;
1591 break;
1596 if (found) {
1597 int rc;
1598 char *user_config = NULL, *xdg_config = NULL;
1599 if (!config_file) {
1600 git_global_config(&user_config, &xdg_config);
1601 config_file = user_config;
1602 if (!user_config)
1603 die(_("$HOME not set"));
1605 rc = git_config_set_multivar_in_file_gently(
1606 config_file, key, NULL, maintpath,
1607 CONFIG_FLAGS_MULTI_REPLACE | CONFIG_FLAGS_FIXED_VALUE);
1608 free(user_config);
1609 free(xdg_config);
1611 if (rc &&
1612 (!force || rc == CONFIG_NOTHING_SET))
1613 die(_("unable to unset '%s' value of '%s'"),
1614 key, maintpath);
1615 } else if (!force) {
1616 die(_("repository '%s' is not registered"), maintpath);
1619 git_configset_clear(&cs);
1620 free(maintpath);
1621 return 0;
1624 static const char *get_frequency(enum schedule_priority schedule)
1626 switch (schedule) {
1627 case SCHEDULE_HOURLY:
1628 return "hourly";
1629 case SCHEDULE_DAILY:
1630 return "daily";
1631 case SCHEDULE_WEEKLY:
1632 return "weekly";
1633 default:
1634 BUG("invalid schedule %d", schedule);
1639 * get_schedule_cmd` reads the GIT_TEST_MAINT_SCHEDULER environment variable
1640 * to mock the schedulers that `git maintenance start` rely on.
1642 * For test purpose, GIT_TEST_MAINT_SCHEDULER can be set to a comma-separated
1643 * list of colon-separated key/value pairs where each pair contains a scheduler
1644 * and its corresponding mock.
1646 * * If $GIT_TEST_MAINT_SCHEDULER is not set, return false and leave the
1647 * arguments unmodified.
1649 * * If $GIT_TEST_MAINT_SCHEDULER is set, return true.
1650 * In this case, the *cmd value is read as input.
1652 * * if the input value *cmd is the key of one of the comma-separated list
1653 * item, then *is_available is set to true and *cmd is modified and becomes
1654 * the mock command.
1656 * * if the input value *cmd isn’t the key of any of the comma-separated list
1657 * item, then *is_available is set to false.
1659 * Ex.:
1660 * GIT_TEST_MAINT_SCHEDULER not set
1661 * +-------+-------------------------------------------------+
1662 * | Input | Output |
1663 * | *cmd | return code | *cmd | *is_available |
1664 * +-------+-------------+-------------------+---------------+
1665 * | "foo" | false | "foo" (unchanged) | (unchanged) |
1666 * +-------+-------------+-------------------+---------------+
1668 * GIT_TEST_MAINT_SCHEDULER set to “foo:./mock_foo.sh,bar:./mock_bar.sh”
1669 * +-------+-------------------------------------------------+
1670 * | Input | Output |
1671 * | *cmd | return code | *cmd | *is_available |
1672 * +-------+-------------+-------------------+---------------+
1673 * | "foo" | true | "./mock.foo.sh" | true |
1674 * | "qux" | true | "qux" (unchanged) | false |
1675 * +-------+-------------+-------------------+---------------+
1677 static int get_schedule_cmd(const char **cmd, int *is_available)
1679 char *testing = xstrdup_or_null(getenv("GIT_TEST_MAINT_SCHEDULER"));
1680 struct string_list_item *item;
1681 struct string_list list = STRING_LIST_INIT_NODUP;
1683 if (!testing)
1684 return 0;
1686 if (is_available)
1687 *is_available = 0;
1689 string_list_split_in_place(&list, testing, ",", -1);
1690 for_each_string_list_item(item, &list) {
1691 struct string_list pair = STRING_LIST_INIT_NODUP;
1693 if (string_list_split_in_place(&pair, item->string, ":", 2) != 2)
1694 continue;
1696 if (!strcmp(*cmd, pair.items[0].string)) {
1697 *cmd = pair.items[1].string;
1698 if (is_available)
1699 *is_available = 1;
1700 string_list_clear(&list, 0);
1701 UNLEAK(testing);
1702 return 1;
1706 string_list_clear(&list, 0);
1707 free(testing);
1708 return 1;
1711 static int get_random_minute(void)
1713 /* Use a static value when under tests. */
1714 if (getenv("GIT_TEST_MAINT_SCHEDULER"))
1715 return 13;
1717 return git_rand() % 60;
1720 static int is_launchctl_available(void)
1722 const char *cmd = "launchctl";
1723 int is_available;
1724 if (get_schedule_cmd(&cmd, &is_available))
1725 return is_available;
1727 #ifdef __APPLE__
1728 return 1;
1729 #else
1730 return 0;
1731 #endif
1734 static char *launchctl_service_name(const char *frequency)
1736 struct strbuf label = STRBUF_INIT;
1737 strbuf_addf(&label, "org.git-scm.git.%s", frequency);
1738 return strbuf_detach(&label, NULL);
1741 static char *launchctl_service_filename(const char *name)
1743 char *expanded;
1744 struct strbuf filename = STRBUF_INIT;
1745 strbuf_addf(&filename, "~/Library/LaunchAgents/%s.plist", name);
1747 expanded = interpolate_path(filename.buf, 1);
1748 if (!expanded)
1749 die(_("failed to expand path '%s'"), filename.buf);
1751 strbuf_release(&filename);
1752 return expanded;
1755 static char *launchctl_get_uid(void)
1757 return xstrfmt("gui/%d", getuid());
1760 static int launchctl_boot_plist(int enable, const char *filename)
1762 const char *cmd = "launchctl";
1763 int result;
1764 struct child_process child = CHILD_PROCESS_INIT;
1765 char *uid = launchctl_get_uid();
1767 get_schedule_cmd(&cmd, NULL);
1768 strvec_split(&child.args, cmd);
1769 strvec_pushl(&child.args, enable ? "bootstrap" : "bootout", uid,
1770 filename, NULL);
1772 child.no_stderr = 1;
1773 child.no_stdout = 1;
1775 if (start_command(&child))
1776 die(_("failed to start launchctl"));
1778 result = finish_command(&child);
1780 free(uid);
1781 return result;
1784 static int launchctl_remove_plist(enum schedule_priority schedule)
1786 const char *frequency = get_frequency(schedule);
1787 char *name = launchctl_service_name(frequency);
1788 char *filename = launchctl_service_filename(name);
1789 int result = launchctl_boot_plist(0, filename);
1790 unlink(filename);
1791 free(filename);
1792 free(name);
1793 return result;
1796 static int launchctl_remove_plists(void)
1798 return launchctl_remove_plist(SCHEDULE_HOURLY) ||
1799 launchctl_remove_plist(SCHEDULE_DAILY) ||
1800 launchctl_remove_plist(SCHEDULE_WEEKLY);
1803 static int launchctl_list_contains_plist(const char *name, const char *cmd)
1805 struct child_process child = CHILD_PROCESS_INIT;
1807 strvec_split(&child.args, cmd);
1808 strvec_pushl(&child.args, "list", name, NULL);
1810 child.no_stderr = 1;
1811 child.no_stdout = 1;
1813 if (start_command(&child))
1814 die(_("failed to start launchctl"));
1816 /* Returns failure if 'name' doesn't exist. */
1817 return !finish_command(&child);
1820 static int launchctl_schedule_plist(const char *exec_path, enum schedule_priority schedule)
1822 int i, fd;
1823 const char *preamble, *repeat;
1824 const char *frequency = get_frequency(schedule);
1825 char *name = launchctl_service_name(frequency);
1826 char *filename = launchctl_service_filename(name);
1827 struct lock_file lk = LOCK_INIT;
1828 static unsigned long lock_file_timeout_ms = ULONG_MAX;
1829 struct strbuf plist = STRBUF_INIT, plist2 = STRBUF_INIT;
1830 struct stat st;
1831 const char *cmd = "launchctl";
1832 int minute = get_random_minute();
1834 get_schedule_cmd(&cmd, NULL);
1835 preamble = "<?xml version=\"1.0\"?>\n"
1836 "<!DOCTYPE plist PUBLIC \"-//Apple//DTD PLIST 1.0//EN\" \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">\n"
1837 "<plist version=\"1.0\">"
1838 "<dict>\n"
1839 "<key>Label</key><string>%s</string>\n"
1840 "<key>ProgramArguments</key>\n"
1841 "<array>\n"
1842 "<string>%s/git</string>\n"
1843 "<string>--exec-path=%s</string>\n"
1844 "<string>for-each-repo</string>\n"
1845 "<string>--config=maintenance.repo</string>\n"
1846 "<string>maintenance</string>\n"
1847 "<string>run</string>\n"
1848 "<string>--schedule=%s</string>\n"
1849 "</array>\n"
1850 "<key>StartCalendarInterval</key>\n"
1851 "<array>\n";
1852 strbuf_addf(&plist, preamble, name, exec_path, exec_path, frequency);
1854 switch (schedule) {
1855 case SCHEDULE_HOURLY:
1856 repeat = "<dict>\n"
1857 "<key>Hour</key><integer>%d</integer>\n"
1858 "<key>Minute</key><integer>%d</integer>\n"
1859 "</dict>\n";
1860 for (i = 1; i <= 23; i++)
1861 strbuf_addf(&plist, repeat, i, minute);
1862 break;
1864 case SCHEDULE_DAILY:
1865 repeat = "<dict>\n"
1866 "<key>Day</key><integer>%d</integer>\n"
1867 "<key>Hour</key><integer>0</integer>\n"
1868 "<key>Minute</key><integer>%d</integer>\n"
1869 "</dict>\n";
1870 for (i = 1; i <= 6; i++)
1871 strbuf_addf(&plist, repeat, i, minute);
1872 break;
1874 case SCHEDULE_WEEKLY:
1875 strbuf_addf(&plist,
1876 "<dict>\n"
1877 "<key>Day</key><integer>0</integer>\n"
1878 "<key>Hour</key><integer>0</integer>\n"
1879 "<key>Minute</key><integer>%d</integer>\n"
1880 "</dict>\n",
1881 minute);
1882 break;
1884 default:
1885 /* unreachable */
1886 break;
1888 strbuf_addstr(&plist, "</array>\n</dict>\n</plist>\n");
1890 if (safe_create_leading_directories(filename))
1891 die(_("failed to create directories for '%s'"), filename);
1893 if ((long)lock_file_timeout_ms < 0 &&
1894 git_config_get_ulong("gc.launchctlplistlocktimeoutms",
1895 &lock_file_timeout_ms))
1896 lock_file_timeout_ms = 150;
1898 fd = hold_lock_file_for_update_timeout(&lk, filename, LOCK_DIE_ON_ERROR,
1899 lock_file_timeout_ms);
1902 * Does this file already exist? With the intended contents? Is it
1903 * registered already? Then it does not need to be re-registered.
1905 if (!stat(filename, &st) && st.st_size == plist.len &&
1906 strbuf_read_file(&plist2, filename, plist.len) == plist.len &&
1907 !strbuf_cmp(&plist, &plist2) &&
1908 launchctl_list_contains_plist(name, cmd))
1909 rollback_lock_file(&lk);
1910 else {
1911 if (write_in_full(fd, plist.buf, plist.len) < 0 ||
1912 commit_lock_file(&lk))
1913 die_errno(_("could not write '%s'"), filename);
1915 /* bootout might fail if not already running, so ignore */
1916 launchctl_boot_plist(0, filename);
1917 if (launchctl_boot_plist(1, filename))
1918 die(_("failed to bootstrap service %s"), filename);
1921 free(filename);
1922 free(name);
1923 strbuf_release(&plist);
1924 strbuf_release(&plist2);
1925 return 0;
1928 static int launchctl_add_plists(void)
1930 const char *exec_path = git_exec_path();
1932 return launchctl_schedule_plist(exec_path, SCHEDULE_HOURLY) ||
1933 launchctl_schedule_plist(exec_path, SCHEDULE_DAILY) ||
1934 launchctl_schedule_plist(exec_path, SCHEDULE_WEEKLY);
1937 static int launchctl_update_schedule(int run_maintenance, int fd UNUSED)
1939 if (run_maintenance)
1940 return launchctl_add_plists();
1941 else
1942 return launchctl_remove_plists();
1945 static int is_schtasks_available(void)
1947 const char *cmd = "schtasks";
1948 int is_available;
1949 if (get_schedule_cmd(&cmd, &is_available))
1950 return is_available;
1952 #ifdef GIT_WINDOWS_NATIVE
1953 return 1;
1954 #else
1955 return 0;
1956 #endif
1959 static char *schtasks_task_name(const char *frequency)
1961 struct strbuf label = STRBUF_INIT;
1962 strbuf_addf(&label, "Git Maintenance (%s)", frequency);
1963 return strbuf_detach(&label, NULL);
1966 static int schtasks_remove_task(enum schedule_priority schedule)
1968 const char *cmd = "schtasks";
1969 struct child_process child = CHILD_PROCESS_INIT;
1970 const char *frequency = get_frequency(schedule);
1971 char *name = schtasks_task_name(frequency);
1973 get_schedule_cmd(&cmd, NULL);
1974 strvec_split(&child.args, cmd);
1975 strvec_pushl(&child.args, "/delete", "/tn", name, "/f", NULL);
1976 free(name);
1978 return run_command(&child);
1981 static int schtasks_remove_tasks(void)
1983 return schtasks_remove_task(SCHEDULE_HOURLY) ||
1984 schtasks_remove_task(SCHEDULE_DAILY) ||
1985 schtasks_remove_task(SCHEDULE_WEEKLY);
1988 static int schtasks_schedule_task(const char *exec_path, enum schedule_priority schedule)
1990 const char *cmd = "schtasks";
1991 int result;
1992 struct child_process child = CHILD_PROCESS_INIT;
1993 const char *xml;
1994 struct tempfile *tfile;
1995 const char *frequency = get_frequency(schedule);
1996 char *name = schtasks_task_name(frequency);
1997 struct strbuf tfilename = STRBUF_INIT;
1998 int minute = get_random_minute();
2000 get_schedule_cmd(&cmd, NULL);
2002 strbuf_addf(&tfilename, "%s/schedule_%s_XXXXXX",
2003 get_git_common_dir(), frequency);
2004 tfile = xmks_tempfile(tfilename.buf);
2005 strbuf_release(&tfilename);
2007 if (!fdopen_tempfile(tfile, "w"))
2008 die(_("failed to create temp xml file"));
2010 xml = "<?xml version=\"1.0\" ?>\n"
2011 "<Task version=\"1.4\" xmlns=\"http://schemas.microsoft.com/windows/2004/02/mit/task\">\n"
2012 "<Triggers>\n"
2013 "<CalendarTrigger>\n";
2014 fputs(xml, tfile->fp);
2016 switch (schedule) {
2017 case SCHEDULE_HOURLY:
2018 fprintf(tfile->fp,
2019 "<StartBoundary>2020-01-01T01:%02d:00</StartBoundary>\n"
2020 "<Enabled>true</Enabled>\n"
2021 "<ScheduleByDay>\n"
2022 "<DaysInterval>1</DaysInterval>\n"
2023 "</ScheduleByDay>\n"
2024 "<Repetition>\n"
2025 "<Interval>PT1H</Interval>\n"
2026 "<Duration>PT23H</Duration>\n"
2027 "<StopAtDurationEnd>false</StopAtDurationEnd>\n"
2028 "</Repetition>\n",
2029 minute);
2030 break;
2032 case SCHEDULE_DAILY:
2033 fprintf(tfile->fp,
2034 "<StartBoundary>2020-01-01T00:%02d:00</StartBoundary>\n"
2035 "<Enabled>true</Enabled>\n"
2036 "<ScheduleByWeek>\n"
2037 "<DaysOfWeek>\n"
2038 "<Monday />\n"
2039 "<Tuesday />\n"
2040 "<Wednesday />\n"
2041 "<Thursday />\n"
2042 "<Friday />\n"
2043 "<Saturday />\n"
2044 "</DaysOfWeek>\n"
2045 "<WeeksInterval>1</WeeksInterval>\n"
2046 "</ScheduleByWeek>\n",
2047 minute);
2048 break;
2050 case SCHEDULE_WEEKLY:
2051 fprintf(tfile->fp,
2052 "<StartBoundary>2020-01-01T00:%02d:00</StartBoundary>\n"
2053 "<Enabled>true</Enabled>\n"
2054 "<ScheduleByWeek>\n"
2055 "<DaysOfWeek>\n"
2056 "<Sunday />\n"
2057 "</DaysOfWeek>\n"
2058 "<WeeksInterval>1</WeeksInterval>\n"
2059 "</ScheduleByWeek>\n",
2060 minute);
2061 break;
2063 default:
2064 break;
2067 xml = "</CalendarTrigger>\n"
2068 "</Triggers>\n"
2069 "<Principals>\n"
2070 "<Principal id=\"Author\">\n"
2071 "<LogonType>InteractiveToken</LogonType>\n"
2072 "<RunLevel>LeastPrivilege</RunLevel>\n"
2073 "</Principal>\n"
2074 "</Principals>\n"
2075 "<Settings>\n"
2076 "<MultipleInstancesPolicy>IgnoreNew</MultipleInstancesPolicy>\n"
2077 "<Enabled>true</Enabled>\n"
2078 "<Hidden>true</Hidden>\n"
2079 "<UseUnifiedSchedulingEngine>true</UseUnifiedSchedulingEngine>\n"
2080 "<WakeToRun>false</WakeToRun>\n"
2081 "<ExecutionTimeLimit>PT72H</ExecutionTimeLimit>\n"
2082 "<Priority>7</Priority>\n"
2083 "</Settings>\n"
2084 "<Actions Context=\"Author\">\n"
2085 "<Exec>\n"
2086 "<Command>\"%s\\headless-git.exe\"</Command>\n"
2087 "<Arguments>--exec-path=\"%s\" for-each-repo --config=maintenance.repo maintenance run --schedule=%s</Arguments>\n"
2088 "</Exec>\n"
2089 "</Actions>\n"
2090 "</Task>\n";
2091 fprintf(tfile->fp, xml, exec_path, exec_path, frequency);
2092 strvec_split(&child.args, cmd);
2093 strvec_pushl(&child.args, "/create", "/tn", name, "/f", "/xml",
2094 get_tempfile_path(tfile), NULL);
2095 close_tempfile_gently(tfile);
2097 child.no_stdout = 1;
2098 child.no_stderr = 1;
2100 if (start_command(&child))
2101 die(_("failed to start schtasks"));
2102 result = finish_command(&child);
2104 delete_tempfile(&tfile);
2105 free(name);
2106 return result;
2109 static int schtasks_schedule_tasks(void)
2111 const char *exec_path = git_exec_path();
2113 return schtasks_schedule_task(exec_path, SCHEDULE_HOURLY) ||
2114 schtasks_schedule_task(exec_path, SCHEDULE_DAILY) ||
2115 schtasks_schedule_task(exec_path, SCHEDULE_WEEKLY);
2118 static int schtasks_update_schedule(int run_maintenance, int fd UNUSED)
2120 if (run_maintenance)
2121 return schtasks_schedule_tasks();
2122 else
2123 return schtasks_remove_tasks();
2126 MAYBE_UNUSED
2127 static int check_crontab_process(const char *cmd)
2129 struct child_process child = CHILD_PROCESS_INIT;
2131 strvec_split(&child.args, cmd);
2132 strvec_push(&child.args, "-l");
2133 child.no_stdin = 1;
2134 child.no_stdout = 1;
2135 child.no_stderr = 1;
2136 child.silent_exec_failure = 1;
2138 if (start_command(&child))
2139 return 0;
2140 /* Ignore exit code, as an empty crontab will return error. */
2141 finish_command(&child);
2142 return 1;
2145 static int is_crontab_available(void)
2147 const char *cmd = "crontab";
2148 int is_available;
2150 if (get_schedule_cmd(&cmd, &is_available))
2151 return is_available;
2153 #ifdef __APPLE__
2155 * macOS has cron, but it requires special permissions and will
2156 * create a UI alert when attempting to run this command.
2158 return 0;
2159 #else
2160 return check_crontab_process(cmd);
2161 #endif
2164 #define BEGIN_LINE "# BEGIN GIT MAINTENANCE SCHEDULE"
2165 #define END_LINE "# END GIT MAINTENANCE SCHEDULE"
2167 static int crontab_update_schedule(int run_maintenance, int fd)
2169 const char *cmd = "crontab";
2170 int result = 0;
2171 int in_old_region = 0;
2172 struct child_process crontab_list = CHILD_PROCESS_INIT;
2173 struct child_process crontab_edit = CHILD_PROCESS_INIT;
2174 FILE *cron_list, *cron_in;
2175 struct strbuf line = STRBUF_INIT;
2176 struct tempfile *tmpedit = NULL;
2177 int minute = get_random_minute();
2179 get_schedule_cmd(&cmd, NULL);
2180 strvec_split(&crontab_list.args, cmd);
2181 strvec_push(&crontab_list.args, "-l");
2182 crontab_list.in = -1;
2183 crontab_list.out = dup(fd);
2184 crontab_list.git_cmd = 0;
2186 if (start_command(&crontab_list))
2187 return error(_("failed to run 'crontab -l'; your system might not support 'cron'"));
2189 /* Ignore exit code, as an empty crontab will return error. */
2190 finish_command(&crontab_list);
2192 tmpedit = mks_tempfile_t(".git_cron_edit_tmpXXXXXX");
2193 if (!tmpedit) {
2194 result = error(_("failed to create crontab temporary file"));
2195 goto out;
2197 cron_in = fdopen_tempfile(tmpedit, "w");
2198 if (!cron_in) {
2199 result = error(_("failed to open temporary file"));
2200 goto out;
2204 * Read from the .lock file, filtering out the old
2205 * schedule while appending the new schedule.
2207 cron_list = fdopen(fd, "r");
2208 rewind(cron_list);
2210 while (!strbuf_getline_lf(&line, cron_list)) {
2211 if (!in_old_region && !strcmp(line.buf, BEGIN_LINE))
2212 in_old_region = 1;
2213 else if (in_old_region && !strcmp(line.buf, END_LINE))
2214 in_old_region = 0;
2215 else if (!in_old_region)
2216 fprintf(cron_in, "%s\n", line.buf);
2218 strbuf_release(&line);
2220 if (run_maintenance) {
2221 struct strbuf line_format = STRBUF_INIT;
2222 const char *exec_path = git_exec_path();
2224 fprintf(cron_in, "%s\n", BEGIN_LINE);
2225 fprintf(cron_in,
2226 "# The following schedule was created by Git\n");
2227 fprintf(cron_in, "# Any edits made in this region might be\n");
2228 fprintf(cron_in,
2229 "# replaced in the future by a Git command.\n\n");
2231 strbuf_addf(&line_format,
2232 "%%d %%s * * %%s \"%s/git\" --exec-path=\"%s\" for-each-repo --config=maintenance.repo maintenance run --schedule=%%s\n",
2233 exec_path, exec_path);
2234 fprintf(cron_in, line_format.buf, minute, "1-23", "*", "hourly");
2235 fprintf(cron_in, line_format.buf, minute, "0", "1-6", "daily");
2236 fprintf(cron_in, line_format.buf, minute, "0", "0", "weekly");
2237 strbuf_release(&line_format);
2239 fprintf(cron_in, "\n%s\n", END_LINE);
2242 fflush(cron_in);
2244 strvec_split(&crontab_edit.args, cmd);
2245 strvec_push(&crontab_edit.args, get_tempfile_path(tmpedit));
2246 crontab_edit.git_cmd = 0;
2248 if (start_command(&crontab_edit)) {
2249 result = error(_("failed to run 'crontab'; your system might not support 'cron'"));
2250 goto out;
2253 if (finish_command(&crontab_edit))
2254 result = error(_("'crontab' died"));
2255 else
2256 fclose(cron_list);
2257 out:
2258 delete_tempfile(&tmpedit);
2259 return result;
2262 static int real_is_systemd_timer_available(void)
2264 struct child_process child = CHILD_PROCESS_INIT;
2266 strvec_pushl(&child.args, "systemctl", "--user", "list-timers", NULL);
2267 child.no_stdin = 1;
2268 child.no_stdout = 1;
2269 child.no_stderr = 1;
2270 child.silent_exec_failure = 1;
2272 if (start_command(&child))
2273 return 0;
2274 if (finish_command(&child))
2275 return 0;
2276 return 1;
2279 static int is_systemd_timer_available(void)
2281 const char *cmd = "systemctl";
2282 int is_available;
2284 if (get_schedule_cmd(&cmd, &is_available))
2285 return is_available;
2287 return real_is_systemd_timer_available();
2290 static char *xdg_config_home_systemd(const char *filename)
2292 return xdg_config_home_for("systemd/user", filename);
2295 #define SYSTEMD_UNIT_FORMAT "git-maintenance@%s.%s"
2297 static int systemd_timer_delete_timer_file(enum schedule_priority priority)
2299 int ret = 0;
2300 const char *frequency = get_frequency(priority);
2301 char *local_timer_name = xstrfmt(SYSTEMD_UNIT_FORMAT, frequency, "timer");
2302 char *filename = xdg_config_home_systemd(local_timer_name);
2304 if (unlink(filename) && !is_missing_file_error(errno))
2305 ret = error_errno(_("failed to delete '%s'"), filename);
2307 free(filename);
2308 free(local_timer_name);
2309 return ret;
2312 static int systemd_timer_delete_service_template(void)
2314 int ret = 0;
2315 char *local_service_name = xstrfmt(SYSTEMD_UNIT_FORMAT, "", "service");
2316 char *filename = xdg_config_home_systemd(local_service_name);
2317 if (unlink(filename) && !is_missing_file_error(errno))
2318 ret = error_errno(_("failed to delete '%s'"), filename);
2320 free(filename);
2321 free(local_service_name);
2322 return ret;
2326 * Write the schedule information into a git-maintenance@<schedule>.timer
2327 * file using a custom minute. This timer file cannot use the templating
2328 * system, so we generate a specific file for each.
2330 static int systemd_timer_write_timer_file(enum schedule_priority schedule,
2331 int minute)
2333 int res = -1;
2334 char *filename;
2335 FILE *file;
2336 const char *unit;
2337 char *schedule_pattern = NULL;
2338 const char *frequency = get_frequency(schedule);
2339 char *local_timer_name = xstrfmt(SYSTEMD_UNIT_FORMAT, frequency, "timer");
2341 filename = xdg_config_home_systemd(local_timer_name);
2343 if (safe_create_leading_directories(filename)) {
2344 error(_("failed to create directories for '%s'"), filename);
2345 goto error;
2347 file = fopen_or_warn(filename, "w");
2348 if (!file)
2349 goto error;
2351 switch (schedule) {
2352 case SCHEDULE_HOURLY:
2353 schedule_pattern = xstrfmt("*-*-* 1..23:%02d:00", minute);
2354 break;
2356 case SCHEDULE_DAILY:
2357 schedule_pattern = xstrfmt("Tue..Sun *-*-* 0:%02d:00", minute);
2358 break;
2360 case SCHEDULE_WEEKLY:
2361 schedule_pattern = xstrfmt("Mon 0:%02d:00", minute);
2362 break;
2364 default:
2365 BUG("Unhandled schedule_priority");
2368 unit = "# This file was created and is maintained by Git.\n"
2369 "# Any edits made in this file might be replaced in the future\n"
2370 "# by a Git command.\n"
2371 "\n"
2372 "[Unit]\n"
2373 "Description=Optimize Git repositories data\n"
2374 "\n"
2375 "[Timer]\n"
2376 "OnCalendar=%s\n"
2377 "Persistent=true\n"
2378 "\n"
2379 "[Install]\n"
2380 "WantedBy=timers.target\n";
2381 if (fprintf(file, unit, schedule_pattern) < 0) {
2382 error(_("failed to write to '%s'"), filename);
2383 fclose(file);
2384 goto error;
2386 if (fclose(file) == EOF) {
2387 error_errno(_("failed to flush '%s'"), filename);
2388 goto error;
2391 res = 0;
2393 error:
2394 free(schedule_pattern);
2395 free(local_timer_name);
2396 free(filename);
2397 return res;
2401 * No matter the schedule, we use the same service and can make use of the
2402 * templating system. When installing git-maintenance@<schedule>.timer,
2403 * systemd will notice that git-maintenance@.service exists as a template
2404 * and will use this file and insert the <schedule> into the template at
2405 * the position of "%i".
2407 static int systemd_timer_write_service_template(const char *exec_path)
2409 int res = -1;
2410 char *filename;
2411 FILE *file;
2412 const char *unit;
2413 char *local_service_name = xstrfmt(SYSTEMD_UNIT_FORMAT, "", "service");
2415 filename = xdg_config_home_systemd(local_service_name);
2416 if (safe_create_leading_directories(filename)) {
2417 error(_("failed to create directories for '%s'"), filename);
2418 goto error;
2420 file = fopen_or_warn(filename, "w");
2421 if (!file)
2422 goto error;
2424 unit = "# This file was created and is maintained by Git.\n"
2425 "# Any edits made in this file might be replaced in the future\n"
2426 "# by a Git command.\n"
2427 "\n"
2428 "[Unit]\n"
2429 "Description=Optimize Git repositories data\n"
2430 "\n"
2431 "[Service]\n"
2432 "Type=oneshot\n"
2433 "ExecStart=\"%s/git\" --exec-path=\"%s\" for-each-repo --config=maintenance.repo maintenance run --schedule=%%i\n"
2434 "LockPersonality=yes\n"
2435 "MemoryDenyWriteExecute=yes\n"
2436 "NoNewPrivileges=yes\n"
2437 "RestrictAddressFamilies=AF_UNIX AF_INET AF_INET6 AF_VSOCK\n"
2438 "RestrictNamespaces=yes\n"
2439 "RestrictRealtime=yes\n"
2440 "RestrictSUIDSGID=yes\n"
2441 "SystemCallArchitectures=native\n"
2442 "SystemCallFilter=@system-service\n";
2443 if (fprintf(file, unit, exec_path, exec_path) < 0) {
2444 error(_("failed to write to '%s'"), filename);
2445 fclose(file);
2446 goto error;
2448 if (fclose(file) == EOF) {
2449 error_errno(_("failed to flush '%s'"), filename);
2450 goto error;
2453 res = 0;
2455 error:
2456 free(local_service_name);
2457 free(filename);
2458 return res;
2461 static int systemd_timer_enable_unit(int enable,
2462 enum schedule_priority schedule,
2463 int minute)
2465 const char *cmd = "systemctl";
2466 struct child_process child = CHILD_PROCESS_INIT;
2467 const char *frequency = get_frequency(schedule);
2470 * Disabling the systemd unit while it is already disabled makes
2471 * systemctl print an error.
2472 * Let's ignore it since it means we already are in the expected state:
2473 * the unit is disabled.
2475 * On the other hand, enabling a systemd unit which is already enabled
2476 * produces no error.
2478 if (!enable)
2479 child.no_stderr = 1;
2480 else if (systemd_timer_write_timer_file(schedule, minute))
2481 return -1;
2483 get_schedule_cmd(&cmd, NULL);
2484 strvec_split(&child.args, cmd);
2485 strvec_pushl(&child.args, "--user", enable ? "enable" : "disable",
2486 "--now", NULL);
2487 strvec_pushf(&child.args, SYSTEMD_UNIT_FORMAT, frequency, "timer");
2489 if (start_command(&child))
2490 return error(_("failed to start systemctl"));
2491 if (finish_command(&child))
2493 * Disabling an already disabled systemd unit makes
2494 * systemctl fail.
2495 * Let's ignore this failure.
2497 * Enabling an enabled systemd unit doesn't fail.
2499 if (enable)
2500 return error(_("failed to run systemctl"));
2501 return 0;
2505 * A previous version of Git wrote the timer units as template files.
2506 * Clean these up, if they exist.
2508 static void systemd_timer_delete_stale_timer_templates(void)
2510 char *timer_template_name = xstrfmt(SYSTEMD_UNIT_FORMAT, "", "timer");
2511 char *filename = xdg_config_home_systemd(timer_template_name);
2513 if (unlink(filename) && !is_missing_file_error(errno))
2514 warning(_("failed to delete '%s'"), filename);
2516 free(filename);
2517 free(timer_template_name);
2520 static int systemd_timer_delete_unit_files(void)
2522 systemd_timer_delete_stale_timer_templates();
2524 /* Purposefully not short-circuited to make sure all are called. */
2525 return systemd_timer_delete_timer_file(SCHEDULE_HOURLY) |
2526 systemd_timer_delete_timer_file(SCHEDULE_DAILY) |
2527 systemd_timer_delete_timer_file(SCHEDULE_WEEKLY) |
2528 systemd_timer_delete_service_template();
2531 static int systemd_timer_delete_units(void)
2533 int minute = get_random_minute();
2534 /* Purposefully not short-circuited to make sure all are called. */
2535 return systemd_timer_enable_unit(0, SCHEDULE_HOURLY, minute) |
2536 systemd_timer_enable_unit(0, SCHEDULE_DAILY, minute) |
2537 systemd_timer_enable_unit(0, SCHEDULE_WEEKLY, minute) |
2538 systemd_timer_delete_unit_files();
2541 static int systemd_timer_setup_units(void)
2543 int minute = get_random_minute();
2544 const char *exec_path = git_exec_path();
2546 int ret = systemd_timer_write_service_template(exec_path) ||
2547 systemd_timer_enable_unit(1, SCHEDULE_HOURLY, minute) ||
2548 systemd_timer_enable_unit(1, SCHEDULE_DAILY, minute) ||
2549 systemd_timer_enable_unit(1, SCHEDULE_WEEKLY, minute);
2551 if (ret)
2552 systemd_timer_delete_units();
2553 else
2554 systemd_timer_delete_stale_timer_templates();
2556 return ret;
2559 static int systemd_timer_update_schedule(int run_maintenance, int fd UNUSED)
2561 if (run_maintenance)
2562 return systemd_timer_setup_units();
2563 else
2564 return systemd_timer_delete_units();
2567 enum scheduler {
2568 SCHEDULER_INVALID = -1,
2569 SCHEDULER_AUTO,
2570 SCHEDULER_CRON,
2571 SCHEDULER_SYSTEMD,
2572 SCHEDULER_LAUNCHCTL,
2573 SCHEDULER_SCHTASKS,
2576 static const struct {
2577 const char *name;
2578 int (*is_available)(void);
2579 int (*update_schedule)(int run_maintenance, int fd);
2580 } scheduler_fn[] = {
2581 [SCHEDULER_CRON] = {
2582 .name = "crontab",
2583 .is_available = is_crontab_available,
2584 .update_schedule = crontab_update_schedule,
2586 [SCHEDULER_SYSTEMD] = {
2587 .name = "systemctl",
2588 .is_available = is_systemd_timer_available,
2589 .update_schedule = systemd_timer_update_schedule,
2591 [SCHEDULER_LAUNCHCTL] = {
2592 .name = "launchctl",
2593 .is_available = is_launchctl_available,
2594 .update_schedule = launchctl_update_schedule,
2596 [SCHEDULER_SCHTASKS] = {
2597 .name = "schtasks",
2598 .is_available = is_schtasks_available,
2599 .update_schedule = schtasks_update_schedule,
2603 static enum scheduler parse_scheduler(const char *value)
2605 if (!value)
2606 return SCHEDULER_INVALID;
2607 else if (!strcasecmp(value, "auto"))
2608 return SCHEDULER_AUTO;
2609 else if (!strcasecmp(value, "cron") || !strcasecmp(value, "crontab"))
2610 return SCHEDULER_CRON;
2611 else if (!strcasecmp(value, "systemd") ||
2612 !strcasecmp(value, "systemd-timer"))
2613 return SCHEDULER_SYSTEMD;
2614 else if (!strcasecmp(value, "launchctl"))
2615 return SCHEDULER_LAUNCHCTL;
2616 else if (!strcasecmp(value, "schtasks"))
2617 return SCHEDULER_SCHTASKS;
2618 else
2619 return SCHEDULER_INVALID;
2622 static int maintenance_opt_scheduler(const struct option *opt, const char *arg,
2623 int unset)
2625 enum scheduler *scheduler = opt->value;
2627 BUG_ON_OPT_NEG(unset);
2629 *scheduler = parse_scheduler(arg);
2630 if (*scheduler == SCHEDULER_INVALID)
2631 return error(_("unrecognized --scheduler argument '%s'"), arg);
2632 return 0;
2635 struct maintenance_start_opts {
2636 enum scheduler scheduler;
2639 static enum scheduler resolve_scheduler(enum scheduler scheduler)
2641 if (scheduler != SCHEDULER_AUTO)
2642 return scheduler;
2644 #if defined(__APPLE__)
2645 return SCHEDULER_LAUNCHCTL;
2647 #elif defined(GIT_WINDOWS_NATIVE)
2648 return SCHEDULER_SCHTASKS;
2650 #elif defined(__linux__)
2651 if (is_systemd_timer_available())
2652 return SCHEDULER_SYSTEMD;
2653 else if (is_crontab_available())
2654 return SCHEDULER_CRON;
2655 else
2656 die(_("neither systemd timers nor crontab are available"));
2658 #else
2659 return SCHEDULER_CRON;
2660 #endif
2663 static void validate_scheduler(enum scheduler scheduler)
2665 if (scheduler == SCHEDULER_INVALID)
2666 BUG("invalid scheduler");
2667 if (scheduler == SCHEDULER_AUTO)
2668 BUG("resolve_scheduler should have been called before");
2670 if (!scheduler_fn[scheduler].is_available())
2671 die(_("%s scheduler is not available"),
2672 scheduler_fn[scheduler].name);
2675 static int update_background_schedule(const struct maintenance_start_opts *opts,
2676 int enable)
2678 unsigned int i;
2679 int result = 0;
2680 struct lock_file lk;
2681 char *lock_path = xstrfmt("%s/schedule", the_repository->objects->odb->path);
2683 if (hold_lock_file_for_update(&lk, lock_path, LOCK_NO_DEREF) < 0) {
2684 free(lock_path);
2685 return error(_("another process is scheduling background maintenance"));
2688 for (i = 1; i < ARRAY_SIZE(scheduler_fn); i++) {
2689 if (enable && opts->scheduler == i)
2690 continue;
2691 if (!scheduler_fn[i].is_available())
2692 continue;
2693 scheduler_fn[i].update_schedule(0, get_lock_file_fd(&lk));
2696 if (enable)
2697 result = scheduler_fn[opts->scheduler].update_schedule(
2698 1, get_lock_file_fd(&lk));
2700 rollback_lock_file(&lk);
2702 free(lock_path);
2703 return result;
2706 static const char *const builtin_maintenance_start_usage[] = {
2707 N_("git maintenance start [--scheduler=<scheduler>]"),
2708 NULL
2711 static int maintenance_start(int argc, const char **argv, const char *prefix)
2713 struct maintenance_start_opts opts = { 0 };
2714 struct option options[] = {
2715 OPT_CALLBACK_F(
2716 0, "scheduler", &opts.scheduler, N_("scheduler"),
2717 N_("scheduler to trigger git maintenance run"),
2718 PARSE_OPT_NONEG, maintenance_opt_scheduler),
2719 OPT_END()
2721 const char *register_args[] = { "register", NULL };
2723 argc = parse_options(argc, argv, prefix, options,
2724 builtin_maintenance_start_usage, 0);
2725 if (argc)
2726 usage_with_options(builtin_maintenance_start_usage, options);
2728 opts.scheduler = resolve_scheduler(opts.scheduler);
2729 validate_scheduler(opts.scheduler);
2731 if (update_background_schedule(&opts, 1))
2732 die(_("failed to set up maintenance schedule"));
2734 if (maintenance_register(ARRAY_SIZE(register_args)-1, register_args, NULL))
2735 warning(_("failed to add repo to global config"));
2736 return 0;
2739 static const char *const builtin_maintenance_stop_usage[] = {
2740 "git maintenance stop",
2741 NULL
2744 static int maintenance_stop(int argc, const char **argv, const char *prefix)
2746 struct option options[] = {
2747 OPT_END()
2749 argc = parse_options(argc, argv, prefix, options,
2750 builtin_maintenance_stop_usage, 0);
2751 if (argc)
2752 usage_with_options(builtin_maintenance_stop_usage, options);
2753 return update_background_schedule(NULL, 0);
2756 static const char * const builtin_maintenance_usage[] = {
2757 N_("git maintenance <subcommand> [<options>]"),
2758 NULL,
2761 int cmd_maintenance(int argc, const char **argv, const char *prefix)
2763 parse_opt_subcommand_fn *fn = NULL;
2764 struct option builtin_maintenance_options[] = {
2765 OPT_SUBCOMMAND("run", &fn, maintenance_run),
2766 OPT_SUBCOMMAND("start", &fn, maintenance_start),
2767 OPT_SUBCOMMAND("stop", &fn, maintenance_stop),
2768 OPT_SUBCOMMAND("register", &fn, maintenance_register),
2769 OPT_SUBCOMMAND("unregister", &fn, maintenance_unregister),
2770 OPT_END(),
2773 argc = parse_options(argc, argv, prefix, builtin_maintenance_options,
2774 builtin_maintenance_usage, 0);
2775 return fn(argc, argv, prefix);