builtin/gc: forward git-gc(1)'s `--auto` flag when packing refs
[alt-git.git] / builtin / gc.c
blobbf1f2a621a71f53f977ccd1c13bf2cab100ed7ba
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 unsigned long max_cruft_size;
56 static int aggressive_depth = 50;
57 static int aggressive_window = 250;
58 static int gc_auto_threshold = 6700;
59 static int gc_auto_pack_limit = 50;
60 static int detach_auto = 1;
61 static timestamp_t gc_log_expire_time;
62 static const char *gc_log_expire = "1.day.ago";
63 static const char *prune_expire = "2.weeks.ago";
64 static const char *prune_worktrees_expire = "3.months.ago";
65 static char *repack_filter;
66 static char *repack_filter_to;
67 static unsigned long big_pack_threshold;
68 static unsigned long max_delta_cache_size = DEFAULT_DELTA_CACHE_SIZE;
70 static struct strvec reflog = STRVEC_INIT;
71 static struct strvec repack = STRVEC_INIT;
72 static struct strvec prune = STRVEC_INIT;
73 static struct strvec prune_worktrees = STRVEC_INIT;
74 static struct strvec rerere = STRVEC_INIT;
76 static struct tempfile *pidfile;
77 static struct lock_file log_lock;
79 static struct string_list pack_garbage = STRING_LIST_INIT_DUP;
81 static void clean_pack_garbage(void)
83 int i;
84 for (i = 0; i < pack_garbage.nr; i++)
85 unlink_or_warn(pack_garbage.items[i].string);
86 string_list_clear(&pack_garbage, 0);
89 static void report_pack_garbage(unsigned seen_bits, const char *path)
91 if (seen_bits == PACKDIR_FILE_IDX)
92 string_list_append(&pack_garbage, path);
95 static void process_log_file(void)
97 struct stat st;
98 if (fstat(get_lock_file_fd(&log_lock), &st)) {
100 * Perhaps there was an i/o error or another
101 * unlikely situation. Try to make a note of
102 * this in gc.log along with any existing
103 * messages.
105 int saved_errno = errno;
106 fprintf(stderr, _("Failed to fstat %s: %s"),
107 get_lock_file_path(&log_lock),
108 strerror(saved_errno));
109 fflush(stderr);
110 commit_lock_file(&log_lock);
111 errno = saved_errno;
112 } else if (st.st_size) {
113 /* There was some error recorded in the lock file */
114 commit_lock_file(&log_lock);
115 } else {
116 /* No error, clean up any old gc.log */
117 unlink(git_path("gc.log"));
118 rollback_lock_file(&log_lock);
122 static void process_log_file_at_exit(void)
124 fflush(stderr);
125 process_log_file();
128 static void process_log_file_on_signal(int signo)
130 process_log_file();
131 sigchain_pop(signo);
132 raise(signo);
135 static int gc_config_is_timestamp_never(const char *var)
137 const char *value;
138 timestamp_t expire;
140 if (!git_config_get_value(var, &value) && value) {
141 if (parse_expiry_date(value, &expire))
142 die(_("failed to parse '%s' value '%s'"), var, value);
143 return expire == 0;
145 return 0;
148 static void gc_config(void)
150 const char *value;
152 if (!git_config_get_value("gc.packrefs", &value)) {
153 if (value && !strcmp(value, "notbare"))
154 pack_refs = -1;
155 else
156 pack_refs = git_config_bool("gc.packrefs", value);
159 if (gc_config_is_timestamp_never("gc.reflogexpire") &&
160 gc_config_is_timestamp_never("gc.reflogexpireunreachable"))
161 prune_reflogs = 0;
163 git_config_get_int("gc.aggressivewindow", &aggressive_window);
164 git_config_get_int("gc.aggressivedepth", &aggressive_depth);
165 git_config_get_int("gc.auto", &gc_auto_threshold);
166 git_config_get_int("gc.autopacklimit", &gc_auto_pack_limit);
167 git_config_get_bool("gc.autodetach", &detach_auto);
168 git_config_get_bool("gc.cruftpacks", &cruft_packs);
169 git_config_get_ulong("gc.maxcruftsize", &max_cruft_size);
170 git_config_get_expiry("gc.pruneexpire", &prune_expire);
171 git_config_get_expiry("gc.worktreepruneexpire", &prune_worktrees_expire);
172 git_config_get_expiry("gc.logexpiry", &gc_log_expire);
174 git_config_get_ulong("gc.bigpackthreshold", &big_pack_threshold);
175 git_config_get_ulong("pack.deltacachesize", &max_delta_cache_size);
177 git_config_get_string("gc.repackfilter", &repack_filter);
178 git_config_get_string("gc.repackfilterto", &repack_filter_to);
180 git_config(git_default_config, NULL);
183 enum schedule_priority {
184 SCHEDULE_NONE = 0,
185 SCHEDULE_WEEKLY = 1,
186 SCHEDULE_DAILY = 2,
187 SCHEDULE_HOURLY = 3,
190 static enum schedule_priority parse_schedule(const char *value)
192 if (!value)
193 return SCHEDULE_NONE;
194 if (!strcasecmp(value, "hourly"))
195 return SCHEDULE_HOURLY;
196 if (!strcasecmp(value, "daily"))
197 return SCHEDULE_DAILY;
198 if (!strcasecmp(value, "weekly"))
199 return SCHEDULE_WEEKLY;
200 return SCHEDULE_NONE;
203 struct maintenance_run_opts {
204 int auto_flag;
205 int quiet;
206 enum schedule_priority schedule;
209 static int maintenance_task_pack_refs(MAYBE_UNUSED struct maintenance_run_opts *opts)
211 struct child_process cmd = CHILD_PROCESS_INIT;
213 cmd.git_cmd = 1;
214 strvec_pushl(&cmd.args, "pack-refs", "--all", "--prune", NULL);
215 if (opts->auto_flag)
216 strvec_push(&cmd.args, "--auto");
218 return run_command(&cmd);
221 static int too_many_loose_objects(void)
224 * Quickly check if a "gc" is needed, by estimating how
225 * many loose objects there are. Because SHA-1 is evenly
226 * distributed, we can check only one and get a reasonable
227 * estimate.
229 DIR *dir;
230 struct dirent *ent;
231 int auto_threshold;
232 int num_loose = 0;
233 int needed = 0;
234 const unsigned hexsz_loose = the_hash_algo->hexsz - 2;
236 dir = opendir(git_path("objects/17"));
237 if (!dir)
238 return 0;
240 auto_threshold = DIV_ROUND_UP(gc_auto_threshold, 256);
241 while ((ent = readdir(dir)) != NULL) {
242 if (strspn(ent->d_name, "0123456789abcdef") != hexsz_loose ||
243 ent->d_name[hexsz_loose] != '\0')
244 continue;
245 if (++num_loose > auto_threshold) {
246 needed = 1;
247 break;
250 closedir(dir);
251 return needed;
254 static struct packed_git *find_base_packs(struct string_list *packs,
255 unsigned long limit)
257 struct packed_git *p, *base = NULL;
259 for (p = get_all_packs(the_repository); p; p = p->next) {
260 if (!p->pack_local || p->is_cruft)
261 continue;
262 if (limit) {
263 if (p->pack_size >= limit)
264 string_list_append(packs, p->pack_name);
265 } else if (!base || base->pack_size < p->pack_size) {
266 base = p;
270 if (base)
271 string_list_append(packs, base->pack_name);
273 return base;
276 static int too_many_packs(void)
278 struct packed_git *p;
279 int cnt;
281 if (gc_auto_pack_limit <= 0)
282 return 0;
284 for (cnt = 0, p = get_all_packs(the_repository); p; p = p->next) {
285 if (!p->pack_local)
286 continue;
287 if (p->pack_keep)
288 continue;
290 * Perhaps check the size of the pack and count only
291 * very small ones here?
293 cnt++;
295 return gc_auto_pack_limit < cnt;
298 static uint64_t total_ram(void)
300 #if defined(HAVE_SYSINFO)
301 struct sysinfo si;
303 if (!sysinfo(&si))
304 return si.totalram;
305 #elif defined(HAVE_BSD_SYSCTL) && (defined(HW_MEMSIZE) || defined(HW_PHYSMEM))
306 int64_t physical_memory;
307 int mib[2];
308 size_t length;
310 mib[0] = CTL_HW;
311 # if defined(HW_MEMSIZE)
312 mib[1] = HW_MEMSIZE;
313 # else
314 mib[1] = HW_PHYSMEM;
315 # endif
316 length = sizeof(int64_t);
317 if (!sysctl(mib, 2, &physical_memory, &length, NULL, 0))
318 return physical_memory;
319 #elif defined(GIT_WINDOWS_NATIVE)
320 MEMORYSTATUSEX memInfo;
322 memInfo.dwLength = sizeof(MEMORYSTATUSEX);
323 if (GlobalMemoryStatusEx(&memInfo))
324 return memInfo.ullTotalPhys;
325 #endif
326 return 0;
329 static uint64_t estimate_repack_memory(struct packed_git *pack)
331 unsigned long nr_objects = repo_approximate_object_count(the_repository);
332 size_t os_cache, heap;
334 if (!pack || !nr_objects)
335 return 0;
338 * First we have to scan through at least one pack.
339 * Assume enough room in OS file cache to keep the entire pack
340 * or we may accidentally evict data of other processes from
341 * the cache.
343 os_cache = pack->pack_size + pack->index_size;
344 /* then pack-objects needs lots more for book keeping */
345 heap = sizeof(struct object_entry) * nr_objects;
347 * internal rev-list --all --objects takes up some memory too,
348 * let's say half of it is for blobs
350 heap += sizeof(struct blob) * nr_objects / 2;
352 * and the other half is for trees (commits and tags are
353 * usually insignificant)
355 heap += sizeof(struct tree) * nr_objects / 2;
356 /* and then obj_hash[], underestimated in fact */
357 heap += sizeof(struct object *) * nr_objects;
358 /* revindex is used also */
359 heap += (sizeof(off_t) + sizeof(uint32_t)) * nr_objects;
361 * read_sha1_file() (either at delta calculation phase, or
362 * writing phase) also fills up the delta base cache
364 heap += delta_base_cache_limit;
365 /* and of course pack-objects has its own delta cache */
366 heap += max_delta_cache_size;
368 return os_cache + heap;
371 static int keep_one_pack(struct string_list_item *item, void *data UNUSED)
373 strvec_pushf(&repack, "--keep-pack=%s", basename(item->string));
374 return 0;
377 static void add_repack_all_option(struct string_list *keep_pack)
379 if (prune_expire && !strcmp(prune_expire, "now"))
380 strvec_push(&repack, "-a");
381 else if (cruft_packs) {
382 strvec_push(&repack, "--cruft");
383 if (prune_expire)
384 strvec_pushf(&repack, "--cruft-expiration=%s", prune_expire);
385 if (max_cruft_size)
386 strvec_pushf(&repack, "--max-cruft-size=%lu",
387 max_cruft_size);
388 } else {
389 strvec_push(&repack, "-A");
390 if (prune_expire)
391 strvec_pushf(&repack, "--unpack-unreachable=%s", prune_expire);
394 if (keep_pack)
395 for_each_string_list(keep_pack, keep_one_pack, NULL);
397 if (repack_filter && *repack_filter)
398 strvec_pushf(&repack, "--filter=%s", repack_filter);
399 if (repack_filter_to && *repack_filter_to)
400 strvec_pushf(&repack, "--filter-to=%s", repack_filter_to);
403 static void add_repack_incremental_option(void)
405 strvec_push(&repack, "--no-write-bitmap-index");
408 static int need_to_gc(void)
411 * Setting gc.auto to 0 or negative can disable the
412 * automatic gc.
414 if (gc_auto_threshold <= 0)
415 return 0;
418 * If there are too many loose objects, but not too many
419 * packs, we run "repack -d -l". If there are too many packs,
420 * we run "repack -A -d -l". Otherwise we tell the caller
421 * there is no need.
423 if (too_many_packs()) {
424 struct string_list keep_pack = STRING_LIST_INIT_NODUP;
426 if (big_pack_threshold) {
427 find_base_packs(&keep_pack, big_pack_threshold);
428 if (keep_pack.nr >= gc_auto_pack_limit) {
429 big_pack_threshold = 0;
430 string_list_clear(&keep_pack, 0);
431 find_base_packs(&keep_pack, 0);
433 } else {
434 struct packed_git *p = find_base_packs(&keep_pack, 0);
435 uint64_t mem_have, mem_want;
437 mem_have = total_ram();
438 mem_want = estimate_repack_memory(p);
441 * Only allow 1/2 of memory for pack-objects, leave
442 * the rest for the OS and other processes in the
443 * system.
445 if (!mem_have || mem_want < mem_have / 2)
446 string_list_clear(&keep_pack, 0);
449 add_repack_all_option(&keep_pack);
450 string_list_clear(&keep_pack, 0);
451 } else if (too_many_loose_objects())
452 add_repack_incremental_option();
453 else
454 return 0;
456 if (run_hooks("pre-auto-gc"))
457 return 0;
458 return 1;
461 /* return NULL on success, else hostname running the gc */
462 static const char *lock_repo_for_gc(int force, pid_t* ret_pid)
464 struct lock_file lock = LOCK_INIT;
465 char my_host[HOST_NAME_MAX + 1];
466 struct strbuf sb = STRBUF_INIT;
467 struct stat st;
468 uintmax_t pid;
469 FILE *fp;
470 int fd;
471 char *pidfile_path;
473 if (is_tempfile_active(pidfile))
474 /* already locked */
475 return NULL;
477 if (xgethostname(my_host, sizeof(my_host)))
478 xsnprintf(my_host, sizeof(my_host), "unknown");
480 pidfile_path = git_pathdup("gc.pid");
481 fd = hold_lock_file_for_update(&lock, pidfile_path,
482 LOCK_DIE_ON_ERROR);
483 if (!force) {
484 static char locking_host[HOST_NAME_MAX + 1];
485 static char *scan_fmt;
486 int should_exit;
488 if (!scan_fmt)
489 scan_fmt = xstrfmt("%s %%%ds", "%"SCNuMAX, HOST_NAME_MAX);
490 fp = fopen(pidfile_path, "r");
491 memset(locking_host, 0, sizeof(locking_host));
492 should_exit =
493 fp != NULL &&
494 !fstat(fileno(fp), &st) &&
496 * 12 hour limit is very generous as gc should
497 * never take that long. On the other hand we
498 * don't really need a strict limit here,
499 * running gc --auto one day late is not a big
500 * problem. --force can be used in manual gc
501 * after the user verifies that no gc is
502 * running.
504 time(NULL) - st.st_mtime <= 12 * 3600 &&
505 fscanf(fp, scan_fmt, &pid, locking_host) == 2 &&
506 /* be gentle to concurrent "gc" on remote hosts */
507 (strcmp(locking_host, my_host) || !kill(pid, 0) || errno == EPERM);
508 if (fp)
509 fclose(fp);
510 if (should_exit) {
511 if (fd >= 0)
512 rollback_lock_file(&lock);
513 *ret_pid = pid;
514 free(pidfile_path);
515 return locking_host;
519 strbuf_addf(&sb, "%"PRIuMAX" %s",
520 (uintmax_t) getpid(), my_host);
521 write_in_full(fd, sb.buf, sb.len);
522 strbuf_release(&sb);
523 commit_lock_file(&lock);
524 pidfile = register_tempfile(pidfile_path);
525 free(pidfile_path);
526 return NULL;
530 * Returns 0 if there was no previous error and gc can proceed, 1 if
531 * gc should not proceed due to an error in the last run. Prints a
532 * message and returns with a non-[01] status code if an error occurred
533 * while reading gc.log
535 static int report_last_gc_error(void)
537 struct strbuf sb = STRBUF_INIT;
538 int ret = 0;
539 ssize_t len;
540 struct stat st;
541 char *gc_log_path = git_pathdup("gc.log");
543 if (stat(gc_log_path, &st)) {
544 if (errno == ENOENT)
545 goto done;
547 ret = die_message_errno(_("cannot stat '%s'"), gc_log_path);
548 goto done;
551 if (st.st_mtime < gc_log_expire_time)
552 goto done;
554 len = strbuf_read_file(&sb, gc_log_path, 0);
555 if (len < 0)
556 ret = die_message_errno(_("cannot read '%s'"), gc_log_path);
557 else if (len > 0) {
559 * A previous gc failed. Report the error, and don't
560 * bother with an automatic gc run since it is likely
561 * to fail in the same way.
563 warning(_("The last gc run reported the following. "
564 "Please correct the root cause\n"
565 "and remove %s\n"
566 "Automatic cleanup will not be performed "
567 "until the file is removed.\n\n"
568 "%s"),
569 gc_log_path, sb.buf);
570 ret = 1;
572 strbuf_release(&sb);
573 done:
574 free(gc_log_path);
575 return ret;
578 static void gc_before_repack(struct maintenance_run_opts *opts)
581 * We may be called twice, as both the pre- and
582 * post-daemonized phases will call us, but running these
583 * commands more than once is pointless and wasteful.
585 static int done = 0;
586 if (done++)
587 return;
589 if (pack_refs && maintenance_task_pack_refs(opts))
590 die(FAILED_RUN, "pack-refs");
592 if (prune_reflogs) {
593 struct child_process cmd = CHILD_PROCESS_INIT;
595 cmd.git_cmd = 1;
596 strvec_pushv(&cmd.args, reflog.v);
597 if (run_command(&cmd))
598 die(FAILED_RUN, reflog.v[0]);
602 int cmd_gc(int argc, const char **argv, const char *prefix)
604 int aggressive = 0;
605 int quiet = 0;
606 int force = 0;
607 const char *name;
608 pid_t pid;
609 int daemonized = 0;
610 int keep_largest_pack = -1;
611 timestamp_t dummy;
612 struct child_process rerere_cmd = CHILD_PROCESS_INIT;
613 struct maintenance_run_opts opts = {0};
615 struct option builtin_gc_options[] = {
616 OPT__QUIET(&quiet, N_("suppress progress reporting")),
617 { OPTION_STRING, 0, "prune", &prune_expire, N_("date"),
618 N_("prune unreferenced objects"),
619 PARSE_OPT_OPTARG, NULL, (intptr_t)prune_expire },
620 OPT_BOOL(0, "cruft", &cruft_packs, N_("pack unreferenced objects separately")),
621 OPT_MAGNITUDE(0, "max-cruft-size", &max_cruft_size,
622 N_("with --cruft, limit the size of new cruft packs")),
623 OPT_BOOL(0, "aggressive", &aggressive, N_("be more thorough (increased runtime)")),
624 OPT_BOOL_F(0, "auto", &opts.auto_flag, N_("enable auto-gc mode"),
625 PARSE_OPT_NOCOMPLETE),
626 OPT_BOOL_F(0, "force", &force,
627 N_("force running gc even if there may be another gc running"),
628 PARSE_OPT_NOCOMPLETE),
629 OPT_BOOL(0, "keep-largest-pack", &keep_largest_pack,
630 N_("repack all other packs except the largest pack")),
631 OPT_END()
634 if (argc == 2 && !strcmp(argv[1], "-h"))
635 usage_with_options(builtin_gc_usage, builtin_gc_options);
637 strvec_pushl(&reflog, "reflog", "expire", "--all", NULL);
638 strvec_pushl(&repack, "repack", "-d", "-l", NULL);
639 strvec_pushl(&prune, "prune", "--expire", NULL);
640 strvec_pushl(&prune_worktrees, "worktree", "prune", "--expire", NULL);
641 strvec_pushl(&rerere, "rerere", "gc", NULL);
643 /* default expiry time, overwritten in gc_config */
644 gc_config();
645 if (parse_expiry_date(gc_log_expire, &gc_log_expire_time))
646 die(_("failed to parse gc.logExpiry value %s"), gc_log_expire);
648 if (pack_refs < 0)
649 pack_refs = !is_bare_repository();
651 argc = parse_options(argc, argv, prefix, builtin_gc_options,
652 builtin_gc_usage, 0);
653 if (argc > 0)
654 usage_with_options(builtin_gc_usage, builtin_gc_options);
656 if (prune_expire && parse_expiry_date(prune_expire, &dummy))
657 die(_("failed to parse prune expiry value %s"), prune_expire);
659 if (aggressive) {
660 strvec_push(&repack, "-f");
661 if (aggressive_depth > 0)
662 strvec_pushf(&repack, "--depth=%d", aggressive_depth);
663 if (aggressive_window > 0)
664 strvec_pushf(&repack, "--window=%d", aggressive_window);
666 if (quiet)
667 strvec_push(&repack, "-q");
669 if (opts.auto_flag) {
671 * Auto-gc should be least intrusive as possible.
673 if (!need_to_gc())
674 return 0;
675 if (!quiet) {
676 if (detach_auto)
677 fprintf(stderr, _("Auto packing the repository in background for optimum performance.\n"));
678 else
679 fprintf(stderr, _("Auto packing the repository for optimum performance.\n"));
680 fprintf(stderr, _("See \"git help gc\" for manual housekeeping.\n"));
682 if (detach_auto) {
683 int ret = report_last_gc_error();
685 if (ret == 1)
686 /* Last gc --auto failed. Skip this one. */
687 return 0;
688 else if (ret)
689 /* an I/O error occurred, already reported */
690 return ret;
692 if (lock_repo_for_gc(force, &pid))
693 return 0;
694 gc_before_repack(&opts); /* dies on failure */
695 delete_tempfile(&pidfile);
698 * failure to daemonize is ok, we'll continue
699 * in foreground
701 daemonized = !daemonize();
703 } else {
704 struct string_list keep_pack = STRING_LIST_INIT_NODUP;
706 if (keep_largest_pack != -1) {
707 if (keep_largest_pack)
708 find_base_packs(&keep_pack, 0);
709 } else if (big_pack_threshold) {
710 find_base_packs(&keep_pack, big_pack_threshold);
713 add_repack_all_option(&keep_pack);
714 string_list_clear(&keep_pack, 0);
717 name = lock_repo_for_gc(force, &pid);
718 if (name) {
719 if (opts.auto_flag)
720 return 0; /* be quiet on --auto */
721 die(_("gc is already running on machine '%s' pid %"PRIuMAX" (use --force if not)"),
722 name, (uintmax_t)pid);
725 if (daemonized) {
726 hold_lock_file_for_update(&log_lock,
727 git_path("gc.log"),
728 LOCK_DIE_ON_ERROR);
729 dup2(get_lock_file_fd(&log_lock), 2);
730 sigchain_push_common(process_log_file_on_signal);
731 atexit(process_log_file_at_exit);
734 gc_before_repack(&opts);
736 if (!repository_format_precious_objects) {
737 struct child_process repack_cmd = CHILD_PROCESS_INIT;
739 repack_cmd.git_cmd = 1;
740 repack_cmd.close_object_store = 1;
741 strvec_pushv(&repack_cmd.args, repack.v);
742 if (run_command(&repack_cmd))
743 die(FAILED_RUN, repack.v[0]);
745 if (prune_expire) {
746 struct child_process prune_cmd = CHILD_PROCESS_INIT;
748 /* run `git prune` even if using cruft packs */
749 strvec_push(&prune, prune_expire);
750 if (quiet)
751 strvec_push(&prune, "--no-progress");
752 if (repo_has_promisor_remote(the_repository))
753 strvec_push(&prune,
754 "--exclude-promisor-objects");
755 prune_cmd.git_cmd = 1;
756 strvec_pushv(&prune_cmd.args, prune.v);
757 if (run_command(&prune_cmd))
758 die(FAILED_RUN, prune.v[0]);
762 if (prune_worktrees_expire) {
763 struct child_process prune_worktrees_cmd = CHILD_PROCESS_INIT;
765 strvec_push(&prune_worktrees, prune_worktrees_expire);
766 prune_worktrees_cmd.git_cmd = 1;
767 strvec_pushv(&prune_worktrees_cmd.args, prune_worktrees.v);
768 if (run_command(&prune_worktrees_cmd))
769 die(FAILED_RUN, prune_worktrees.v[0]);
772 rerere_cmd.git_cmd = 1;
773 strvec_pushv(&rerere_cmd.args, rerere.v);
774 if (run_command(&rerere_cmd))
775 die(FAILED_RUN, rerere.v[0]);
777 report_garbage = report_pack_garbage;
778 reprepare_packed_git(the_repository);
779 if (pack_garbage.nr > 0) {
780 close_object_store(the_repository->objects);
781 clean_pack_garbage();
784 if (the_repository->settings.gc_write_commit_graph == 1)
785 write_commit_graph_reachable(the_repository->objects->odb,
786 !quiet && !daemonized ? COMMIT_GRAPH_WRITE_PROGRESS : 0,
787 NULL);
789 if (opts.auto_flag && too_many_loose_objects())
790 warning(_("There are too many unreachable loose objects; "
791 "run 'git prune' to remove them."));
793 if (!daemonized)
794 unlink(git_path("gc.log"));
796 return 0;
799 static const char *const builtin_maintenance_run_usage[] = {
800 N_("git maintenance run [--auto] [--[no-]quiet] [--task=<task>] [--schedule]"),
801 NULL
804 static int maintenance_opt_schedule(const struct option *opt, const char *arg,
805 int unset)
807 enum schedule_priority *priority = opt->value;
809 if (unset)
810 die(_("--no-schedule is not allowed"));
812 *priority = parse_schedule(arg);
814 if (!*priority)
815 die(_("unrecognized --schedule argument '%s'"), arg);
817 return 0;
820 /* Remember to update object flag allocation in object.h */
821 #define SEEN (1u<<0)
823 struct cg_auto_data {
824 int num_not_in_graph;
825 int limit;
828 static int dfs_on_ref(const char *refname UNUSED,
829 const struct object_id *oid,
830 int flags UNUSED,
831 void *cb_data)
833 struct cg_auto_data *data = (struct cg_auto_data *)cb_data;
834 int result = 0;
835 struct object_id peeled;
836 struct commit_list *stack = NULL;
837 struct commit *commit;
839 if (!peel_iterated_oid(oid, &peeled))
840 oid = &peeled;
841 if (oid_object_info(the_repository, oid, NULL) != OBJ_COMMIT)
842 return 0;
844 commit = lookup_commit(the_repository, oid);
845 if (!commit)
846 return 0;
847 if (repo_parse_commit(the_repository, commit) ||
848 commit_graph_position(commit) != COMMIT_NOT_FROM_GRAPH)
849 return 0;
851 data->num_not_in_graph++;
853 if (data->num_not_in_graph >= data->limit)
854 return 1;
856 commit_list_append(commit, &stack);
858 while (!result && stack) {
859 struct commit_list *parent;
861 commit = pop_commit(&stack);
863 for (parent = commit->parents; parent; parent = parent->next) {
864 if (repo_parse_commit(the_repository, parent->item) ||
865 commit_graph_position(parent->item) != COMMIT_NOT_FROM_GRAPH ||
866 parent->item->object.flags & SEEN)
867 continue;
869 parent->item->object.flags |= SEEN;
870 data->num_not_in_graph++;
872 if (data->num_not_in_graph >= data->limit) {
873 result = 1;
874 break;
877 commit_list_append(parent->item, &stack);
881 free_commit_list(stack);
882 return result;
885 static int should_write_commit_graph(void)
887 int result;
888 struct cg_auto_data data;
890 data.num_not_in_graph = 0;
891 data.limit = 100;
892 git_config_get_int("maintenance.commit-graph.auto",
893 &data.limit);
895 if (!data.limit)
896 return 0;
897 if (data.limit < 0)
898 return 1;
900 result = for_each_ref(dfs_on_ref, &data);
902 repo_clear_commit_marks(the_repository, SEEN);
904 return result;
907 static int run_write_commit_graph(struct maintenance_run_opts *opts)
909 struct child_process child = CHILD_PROCESS_INIT;
911 child.git_cmd = child.close_object_store = 1;
912 strvec_pushl(&child.args, "commit-graph", "write",
913 "--split", "--reachable", NULL);
915 if (opts->quiet)
916 strvec_push(&child.args, "--no-progress");
918 return !!run_command(&child);
921 static int maintenance_task_commit_graph(struct maintenance_run_opts *opts)
923 prepare_repo_settings(the_repository);
924 if (!the_repository->settings.core_commit_graph)
925 return 0;
927 if (run_write_commit_graph(opts)) {
928 error(_("failed to write commit-graph"));
929 return 1;
932 return 0;
935 static int fetch_remote(struct remote *remote, void *cbdata)
937 struct maintenance_run_opts *opts = cbdata;
938 struct child_process child = CHILD_PROCESS_INIT;
940 if (remote->skip_default_update)
941 return 0;
943 child.git_cmd = 1;
944 strvec_pushl(&child.args, "fetch", remote->name,
945 "--prefetch", "--prune", "--no-tags",
946 "--no-write-fetch-head", "--recurse-submodules=no",
947 NULL);
949 if (opts->quiet)
950 strvec_push(&child.args, "--quiet");
952 return !!run_command(&child);
955 static int maintenance_task_prefetch(struct maintenance_run_opts *opts)
957 if (for_each_remote(fetch_remote, opts)) {
958 error(_("failed to prefetch remotes"));
959 return 1;
962 return 0;
965 static int maintenance_task_gc(struct maintenance_run_opts *opts)
967 struct child_process child = CHILD_PROCESS_INIT;
969 child.git_cmd = child.close_object_store = 1;
970 strvec_push(&child.args, "gc");
972 if (opts->auto_flag)
973 strvec_push(&child.args, "--auto");
974 if (opts->quiet)
975 strvec_push(&child.args, "--quiet");
976 else
977 strvec_push(&child.args, "--no-quiet");
979 return run_command(&child);
982 static int prune_packed(struct maintenance_run_opts *opts)
984 struct child_process child = CHILD_PROCESS_INIT;
986 child.git_cmd = 1;
987 strvec_push(&child.args, "prune-packed");
989 if (opts->quiet)
990 strvec_push(&child.args, "--quiet");
992 return !!run_command(&child);
995 struct write_loose_object_data {
996 FILE *in;
997 int count;
998 int batch_size;
1001 static int loose_object_auto_limit = 100;
1003 static int loose_object_count(const struct object_id *oid UNUSED,
1004 const char *path UNUSED,
1005 void *data)
1007 int *count = (int*)data;
1008 if (++(*count) >= loose_object_auto_limit)
1009 return 1;
1010 return 0;
1013 static int loose_object_auto_condition(void)
1015 int count = 0;
1017 git_config_get_int("maintenance.loose-objects.auto",
1018 &loose_object_auto_limit);
1020 if (!loose_object_auto_limit)
1021 return 0;
1022 if (loose_object_auto_limit < 0)
1023 return 1;
1025 return for_each_loose_file_in_objdir(the_repository->objects->odb->path,
1026 loose_object_count,
1027 NULL, NULL, &count);
1030 static int bail_on_loose(const struct object_id *oid UNUSED,
1031 const char *path UNUSED,
1032 void *data UNUSED)
1034 return 1;
1037 static int write_loose_object_to_stdin(const struct object_id *oid,
1038 const char *path UNUSED,
1039 void *data)
1041 struct write_loose_object_data *d = (struct write_loose_object_data *)data;
1043 fprintf(d->in, "%s\n", oid_to_hex(oid));
1045 return ++(d->count) > d->batch_size;
1048 static int pack_loose(struct maintenance_run_opts *opts)
1050 struct repository *r = the_repository;
1051 int result = 0;
1052 struct write_loose_object_data data;
1053 struct child_process pack_proc = CHILD_PROCESS_INIT;
1056 * Do not start pack-objects process
1057 * if there are no loose objects.
1059 if (!for_each_loose_file_in_objdir(r->objects->odb->path,
1060 bail_on_loose,
1061 NULL, NULL, NULL))
1062 return 0;
1064 pack_proc.git_cmd = 1;
1066 strvec_push(&pack_proc.args, "pack-objects");
1067 if (opts->quiet)
1068 strvec_push(&pack_proc.args, "--quiet");
1069 strvec_pushf(&pack_proc.args, "%s/pack/loose", r->objects->odb->path);
1071 pack_proc.in = -1;
1073 if (start_command(&pack_proc)) {
1074 error(_("failed to start 'git pack-objects' process"));
1075 return 1;
1078 data.in = xfdopen(pack_proc.in, "w");
1079 data.count = 0;
1080 data.batch_size = 50000;
1082 for_each_loose_file_in_objdir(r->objects->odb->path,
1083 write_loose_object_to_stdin,
1084 NULL,
1085 NULL,
1086 &data);
1088 fclose(data.in);
1090 if (finish_command(&pack_proc)) {
1091 error(_("failed to finish 'git pack-objects' process"));
1092 result = 1;
1095 return result;
1098 static int maintenance_task_loose_objects(struct maintenance_run_opts *opts)
1100 return prune_packed(opts) || pack_loose(opts);
1103 static int incremental_repack_auto_condition(void)
1105 struct packed_git *p;
1106 int incremental_repack_auto_limit = 10;
1107 int count = 0;
1109 prepare_repo_settings(the_repository);
1110 if (!the_repository->settings.core_multi_pack_index)
1111 return 0;
1113 git_config_get_int("maintenance.incremental-repack.auto",
1114 &incremental_repack_auto_limit);
1116 if (!incremental_repack_auto_limit)
1117 return 0;
1118 if (incremental_repack_auto_limit < 0)
1119 return 1;
1121 for (p = get_packed_git(the_repository);
1122 count < incremental_repack_auto_limit && p;
1123 p = p->next) {
1124 if (!p->multi_pack_index)
1125 count++;
1128 return count >= incremental_repack_auto_limit;
1131 static int multi_pack_index_write(struct maintenance_run_opts *opts)
1133 struct child_process child = CHILD_PROCESS_INIT;
1135 child.git_cmd = 1;
1136 strvec_pushl(&child.args, "multi-pack-index", "write", NULL);
1138 if (opts->quiet)
1139 strvec_push(&child.args, "--no-progress");
1141 if (run_command(&child))
1142 return error(_("failed to write multi-pack-index"));
1144 return 0;
1147 static int multi_pack_index_expire(struct maintenance_run_opts *opts)
1149 struct child_process child = CHILD_PROCESS_INIT;
1151 child.git_cmd = child.close_object_store = 1;
1152 strvec_pushl(&child.args, "multi-pack-index", "expire", NULL);
1154 if (opts->quiet)
1155 strvec_push(&child.args, "--no-progress");
1157 if (run_command(&child))
1158 return error(_("'git multi-pack-index expire' failed"));
1160 return 0;
1163 #define TWO_GIGABYTES (INT32_MAX)
1165 static off_t get_auto_pack_size(void)
1168 * The "auto" value is special: we optimize for
1169 * one large pack-file (i.e. from a clone) and
1170 * expect the rest to be small and they can be
1171 * repacked quickly.
1173 * The strategy we select here is to select a
1174 * size that is one more than the second largest
1175 * pack-file. This ensures that we will repack
1176 * at least two packs if there are three or more
1177 * packs.
1179 off_t max_size = 0;
1180 off_t second_largest_size = 0;
1181 off_t result_size;
1182 struct packed_git *p;
1183 struct repository *r = the_repository;
1185 reprepare_packed_git(r);
1186 for (p = get_all_packs(r); p; p = p->next) {
1187 if (p->pack_size > max_size) {
1188 second_largest_size = max_size;
1189 max_size = p->pack_size;
1190 } else if (p->pack_size > second_largest_size)
1191 second_largest_size = p->pack_size;
1194 result_size = second_largest_size + 1;
1196 /* But limit ourselves to a batch size of 2g */
1197 if (result_size > TWO_GIGABYTES)
1198 result_size = TWO_GIGABYTES;
1200 return result_size;
1203 static int multi_pack_index_repack(struct maintenance_run_opts *opts)
1205 struct child_process child = CHILD_PROCESS_INIT;
1207 child.git_cmd = child.close_object_store = 1;
1208 strvec_pushl(&child.args, "multi-pack-index", "repack", NULL);
1210 if (opts->quiet)
1211 strvec_push(&child.args, "--no-progress");
1213 strvec_pushf(&child.args, "--batch-size=%"PRIuMAX,
1214 (uintmax_t)get_auto_pack_size());
1216 if (run_command(&child))
1217 return error(_("'git multi-pack-index repack' failed"));
1219 return 0;
1222 static int maintenance_task_incremental_repack(struct maintenance_run_opts *opts)
1224 prepare_repo_settings(the_repository);
1225 if (!the_repository->settings.core_multi_pack_index) {
1226 warning(_("skipping incremental-repack task because core.multiPackIndex is disabled"));
1227 return 0;
1230 if (multi_pack_index_write(opts))
1231 return 1;
1232 if (multi_pack_index_expire(opts))
1233 return 1;
1234 if (multi_pack_index_repack(opts))
1235 return 1;
1236 return 0;
1239 typedef int maintenance_task_fn(struct maintenance_run_opts *opts);
1242 * An auto condition function returns 1 if the task should run
1243 * and 0 if the task should NOT run. See needs_to_gc() for an
1244 * example.
1246 typedef int maintenance_auto_fn(void);
1248 struct maintenance_task {
1249 const char *name;
1250 maintenance_task_fn *fn;
1251 maintenance_auto_fn *auto_condition;
1252 unsigned enabled:1;
1254 enum schedule_priority schedule;
1256 /* -1 if not selected. */
1257 int selected_order;
1260 enum maintenance_task_label {
1261 TASK_PREFETCH,
1262 TASK_LOOSE_OBJECTS,
1263 TASK_INCREMENTAL_REPACK,
1264 TASK_GC,
1265 TASK_COMMIT_GRAPH,
1266 TASK_PACK_REFS,
1268 /* Leave as final value */
1269 TASK__COUNT
1272 static struct maintenance_task tasks[] = {
1273 [TASK_PREFETCH] = {
1274 "prefetch",
1275 maintenance_task_prefetch,
1277 [TASK_LOOSE_OBJECTS] = {
1278 "loose-objects",
1279 maintenance_task_loose_objects,
1280 loose_object_auto_condition,
1282 [TASK_INCREMENTAL_REPACK] = {
1283 "incremental-repack",
1284 maintenance_task_incremental_repack,
1285 incremental_repack_auto_condition,
1287 [TASK_GC] = {
1288 "gc",
1289 maintenance_task_gc,
1290 need_to_gc,
1293 [TASK_COMMIT_GRAPH] = {
1294 "commit-graph",
1295 maintenance_task_commit_graph,
1296 should_write_commit_graph,
1298 [TASK_PACK_REFS] = {
1299 "pack-refs",
1300 maintenance_task_pack_refs,
1301 NULL,
1305 static int compare_tasks_by_selection(const void *a_, const void *b_)
1307 const struct maintenance_task *a = a_;
1308 const struct maintenance_task *b = b_;
1310 return b->selected_order - a->selected_order;
1313 static int maintenance_run_tasks(struct maintenance_run_opts *opts)
1315 int i, found_selected = 0;
1316 int result = 0;
1317 struct lock_file lk;
1318 struct repository *r = the_repository;
1319 char *lock_path = xstrfmt("%s/maintenance", r->objects->odb->path);
1321 if (hold_lock_file_for_update(&lk, lock_path, LOCK_NO_DEREF) < 0) {
1323 * Another maintenance command is running.
1325 * If --auto was provided, then it is likely due to a
1326 * recursive process stack. Do not report an error in
1327 * that case.
1329 if (!opts->auto_flag && !opts->quiet)
1330 warning(_("lock file '%s' exists, skipping maintenance"),
1331 lock_path);
1332 free(lock_path);
1333 return 0;
1335 free(lock_path);
1337 for (i = 0; !found_selected && i < TASK__COUNT; i++)
1338 found_selected = tasks[i].selected_order >= 0;
1340 if (found_selected)
1341 QSORT(tasks, TASK__COUNT, compare_tasks_by_selection);
1343 for (i = 0; i < TASK__COUNT; i++) {
1344 if (found_selected && tasks[i].selected_order < 0)
1345 continue;
1347 if (!found_selected && !tasks[i].enabled)
1348 continue;
1350 if (opts->auto_flag &&
1351 (!tasks[i].auto_condition ||
1352 !tasks[i].auto_condition()))
1353 continue;
1355 if (opts->schedule && tasks[i].schedule < opts->schedule)
1356 continue;
1358 trace2_region_enter("maintenance", tasks[i].name, r);
1359 if (tasks[i].fn(opts)) {
1360 error(_("task '%s' failed"), tasks[i].name);
1361 result = 1;
1363 trace2_region_leave("maintenance", tasks[i].name, r);
1366 rollback_lock_file(&lk);
1367 return result;
1370 static void initialize_maintenance_strategy(void)
1372 char *config_str;
1374 if (git_config_get_string("maintenance.strategy", &config_str))
1375 return;
1377 if (!strcasecmp(config_str, "incremental")) {
1378 tasks[TASK_GC].schedule = SCHEDULE_NONE;
1379 tasks[TASK_COMMIT_GRAPH].enabled = 1;
1380 tasks[TASK_COMMIT_GRAPH].schedule = SCHEDULE_HOURLY;
1381 tasks[TASK_PREFETCH].enabled = 1;
1382 tasks[TASK_PREFETCH].schedule = SCHEDULE_HOURLY;
1383 tasks[TASK_INCREMENTAL_REPACK].enabled = 1;
1384 tasks[TASK_INCREMENTAL_REPACK].schedule = SCHEDULE_DAILY;
1385 tasks[TASK_LOOSE_OBJECTS].enabled = 1;
1386 tasks[TASK_LOOSE_OBJECTS].schedule = SCHEDULE_DAILY;
1387 tasks[TASK_PACK_REFS].enabled = 1;
1388 tasks[TASK_PACK_REFS].schedule = SCHEDULE_WEEKLY;
1392 static void initialize_task_config(int schedule)
1394 int i;
1395 struct strbuf config_name = STRBUF_INIT;
1396 gc_config();
1398 if (schedule)
1399 initialize_maintenance_strategy();
1401 for (i = 0; i < TASK__COUNT; i++) {
1402 int config_value;
1403 char *config_str;
1405 strbuf_reset(&config_name);
1406 strbuf_addf(&config_name, "maintenance.%s.enabled",
1407 tasks[i].name);
1409 if (!git_config_get_bool(config_name.buf, &config_value))
1410 tasks[i].enabled = config_value;
1412 strbuf_reset(&config_name);
1413 strbuf_addf(&config_name, "maintenance.%s.schedule",
1414 tasks[i].name);
1416 if (!git_config_get_string(config_name.buf, &config_str)) {
1417 tasks[i].schedule = parse_schedule(config_str);
1418 free(config_str);
1422 strbuf_release(&config_name);
1425 static int task_option_parse(const struct option *opt UNUSED,
1426 const char *arg, int unset)
1428 int i, num_selected = 0;
1429 struct maintenance_task *task = NULL;
1431 BUG_ON_OPT_NEG(unset);
1433 for (i = 0; i < TASK__COUNT; i++) {
1434 if (tasks[i].selected_order >= 0)
1435 num_selected++;
1436 if (!strcasecmp(tasks[i].name, arg)) {
1437 task = &tasks[i];
1441 if (!task) {
1442 error(_("'%s' is not a valid task"), arg);
1443 return 1;
1446 if (task->selected_order >= 0) {
1447 error(_("task '%s' cannot be selected multiple times"), arg);
1448 return 1;
1451 task->selected_order = num_selected + 1;
1453 return 0;
1456 static int maintenance_run(int argc, const char **argv, const char *prefix)
1458 int i;
1459 struct maintenance_run_opts opts;
1460 struct option builtin_maintenance_run_options[] = {
1461 OPT_BOOL(0, "auto", &opts.auto_flag,
1462 N_("run tasks based on the state of the repository")),
1463 OPT_CALLBACK(0, "schedule", &opts.schedule, N_("frequency"),
1464 N_("run tasks based on frequency"),
1465 maintenance_opt_schedule),
1466 OPT_BOOL(0, "quiet", &opts.quiet,
1467 N_("do not report progress or other information over stderr")),
1468 OPT_CALLBACK_F(0, "task", NULL, N_("task"),
1469 N_("run a specific task"),
1470 PARSE_OPT_NONEG, task_option_parse),
1471 OPT_END()
1473 memset(&opts, 0, sizeof(opts));
1475 opts.quiet = !isatty(2);
1477 for (i = 0; i < TASK__COUNT; i++)
1478 tasks[i].selected_order = -1;
1480 argc = parse_options(argc, argv, prefix,
1481 builtin_maintenance_run_options,
1482 builtin_maintenance_run_usage,
1483 PARSE_OPT_STOP_AT_NON_OPTION);
1485 if (opts.auto_flag && opts.schedule)
1486 die(_("use at most one of --auto and --schedule=<frequency>"));
1488 initialize_task_config(opts.schedule);
1490 if (argc != 0)
1491 usage_with_options(builtin_maintenance_run_usage,
1492 builtin_maintenance_run_options);
1493 return maintenance_run_tasks(&opts);
1496 static char *get_maintpath(void)
1498 struct strbuf sb = STRBUF_INIT;
1499 const char *p = the_repository->worktree ?
1500 the_repository->worktree : the_repository->gitdir;
1502 strbuf_realpath(&sb, p, 1);
1503 return strbuf_detach(&sb, NULL);
1506 static char const * const builtin_maintenance_register_usage[] = {
1507 "git maintenance register [--config-file <path>]",
1508 NULL
1511 static int maintenance_register(int argc, const char **argv, const char *prefix)
1513 char *config_file = NULL;
1514 struct option options[] = {
1515 OPT_STRING(0, "config-file", &config_file, N_("file"), N_("use given config file")),
1516 OPT_END(),
1518 int found = 0;
1519 const char *key = "maintenance.repo";
1520 char *maintpath = get_maintpath();
1521 struct string_list_item *item;
1522 const struct string_list *list;
1524 argc = parse_options(argc, argv, prefix, options,
1525 builtin_maintenance_register_usage, 0);
1526 if (argc)
1527 usage_with_options(builtin_maintenance_register_usage,
1528 options);
1530 /* Disable foreground maintenance */
1531 git_config_set("maintenance.auto", "false");
1533 /* Set maintenance strategy, if unset */
1534 if (git_config_get("maintenance.strategy"))
1535 git_config_set("maintenance.strategy", "incremental");
1537 if (!git_config_get_string_multi(key, &list)) {
1538 for_each_string_list_item(item, list) {
1539 if (!strcmp(maintpath, item->string)) {
1540 found = 1;
1541 break;
1546 if (!found) {
1547 int rc;
1548 char *global_config_file = NULL;
1550 if (!config_file) {
1551 global_config_file = git_global_config();
1552 config_file = global_config_file;
1554 if (!config_file)
1555 die(_("$HOME not set"));
1556 rc = git_config_set_multivar_in_file_gently(
1557 config_file, "maintenance.repo", maintpath,
1558 CONFIG_REGEX_NONE, 0);
1559 free(global_config_file);
1561 if (rc)
1562 die(_("unable to add '%s' value of '%s'"),
1563 key, maintpath);
1566 free(maintpath);
1567 return 0;
1570 static char const * const builtin_maintenance_unregister_usage[] = {
1571 "git maintenance unregister [--config-file <path>] [--force]",
1572 NULL
1575 static int maintenance_unregister(int argc, const char **argv, const char *prefix)
1577 int force = 0;
1578 char *config_file = NULL;
1579 struct option options[] = {
1580 OPT_STRING(0, "config-file", &config_file, N_("file"), N_("use given config file")),
1581 OPT__FORCE(&force,
1582 N_("return success even if repository was not registered"),
1583 PARSE_OPT_NOCOMPLETE),
1584 OPT_END(),
1586 const char *key = "maintenance.repo";
1587 char *maintpath = get_maintpath();
1588 int found = 0;
1589 struct string_list_item *item;
1590 const struct string_list *list;
1591 struct config_set cs = { { 0 } };
1593 argc = parse_options(argc, argv, prefix, options,
1594 builtin_maintenance_unregister_usage, 0);
1595 if (argc)
1596 usage_with_options(builtin_maintenance_unregister_usage,
1597 options);
1599 if (config_file) {
1600 git_configset_init(&cs);
1601 git_configset_add_file(&cs, config_file);
1603 if (!(config_file
1604 ? git_configset_get_string_multi(&cs, key, &list)
1605 : git_config_get_string_multi(key, &list))) {
1606 for_each_string_list_item(item, list) {
1607 if (!strcmp(maintpath, item->string)) {
1608 found = 1;
1609 break;
1614 if (found) {
1615 int rc;
1616 char *global_config_file = NULL;
1618 if (!config_file) {
1619 global_config_file = git_global_config();
1620 config_file = global_config_file;
1622 if (!config_file)
1623 die(_("$HOME not set"));
1624 rc = git_config_set_multivar_in_file_gently(
1625 config_file, key, NULL, maintpath,
1626 CONFIG_FLAGS_MULTI_REPLACE | CONFIG_FLAGS_FIXED_VALUE);
1627 free(global_config_file);
1629 if (rc &&
1630 (!force || rc == CONFIG_NOTHING_SET))
1631 die(_("unable to unset '%s' value of '%s'"),
1632 key, maintpath);
1633 } else if (!force) {
1634 die(_("repository '%s' is not registered"), maintpath);
1637 git_configset_clear(&cs);
1638 free(maintpath);
1639 return 0;
1642 static const char *get_frequency(enum schedule_priority schedule)
1644 switch (schedule) {
1645 case SCHEDULE_HOURLY:
1646 return "hourly";
1647 case SCHEDULE_DAILY:
1648 return "daily";
1649 case SCHEDULE_WEEKLY:
1650 return "weekly";
1651 default:
1652 BUG("invalid schedule %d", schedule);
1657 * get_schedule_cmd` reads the GIT_TEST_MAINT_SCHEDULER environment variable
1658 * to mock the schedulers that `git maintenance start` rely on.
1660 * For test purpose, GIT_TEST_MAINT_SCHEDULER can be set to a comma-separated
1661 * list of colon-separated key/value pairs where each pair contains a scheduler
1662 * and its corresponding mock.
1664 * * If $GIT_TEST_MAINT_SCHEDULER is not set, return false and leave the
1665 * arguments unmodified.
1667 * * If $GIT_TEST_MAINT_SCHEDULER is set, return true.
1668 * In this case, the *cmd value is read as input.
1670 * * if the input value *cmd is the key of one of the comma-separated list
1671 * item, then *is_available is set to true and *cmd is modified and becomes
1672 * the mock command.
1674 * * if the input value *cmd isn’t the key of any of the comma-separated list
1675 * item, then *is_available is set to false.
1677 * Ex.:
1678 * GIT_TEST_MAINT_SCHEDULER not set
1679 * +-------+-------------------------------------------------+
1680 * | Input | Output |
1681 * | *cmd | return code | *cmd | *is_available |
1682 * +-------+-------------+-------------------+---------------+
1683 * | "foo" | false | "foo" (unchanged) | (unchanged) |
1684 * +-------+-------------+-------------------+---------------+
1686 * GIT_TEST_MAINT_SCHEDULER set to “foo:./mock_foo.sh,bar:./mock_bar.sh”
1687 * +-------+-------------------------------------------------+
1688 * | Input | Output |
1689 * | *cmd | return code | *cmd | *is_available |
1690 * +-------+-------------+-------------------+---------------+
1691 * | "foo" | true | "./mock.foo.sh" | true |
1692 * | "qux" | true | "qux" (unchanged) | false |
1693 * +-------+-------------+-------------------+---------------+
1695 static int get_schedule_cmd(const char **cmd, int *is_available)
1697 char *testing = xstrdup_or_null(getenv("GIT_TEST_MAINT_SCHEDULER"));
1698 struct string_list_item *item;
1699 struct string_list list = STRING_LIST_INIT_NODUP;
1701 if (!testing)
1702 return 0;
1704 if (is_available)
1705 *is_available = 0;
1707 string_list_split_in_place(&list, testing, ",", -1);
1708 for_each_string_list_item(item, &list) {
1709 struct string_list pair = STRING_LIST_INIT_NODUP;
1711 if (string_list_split_in_place(&pair, item->string, ":", 2) != 2)
1712 continue;
1714 if (!strcmp(*cmd, pair.items[0].string)) {
1715 *cmd = pair.items[1].string;
1716 if (is_available)
1717 *is_available = 1;
1718 string_list_clear(&list, 0);
1719 UNLEAK(testing);
1720 return 1;
1724 string_list_clear(&list, 0);
1725 free(testing);
1726 return 1;
1729 static int get_random_minute(void)
1731 /* Use a static value when under tests. */
1732 if (getenv("GIT_TEST_MAINT_SCHEDULER"))
1733 return 13;
1735 return git_rand() % 60;
1738 static int is_launchctl_available(void)
1740 const char *cmd = "launchctl";
1741 int is_available;
1742 if (get_schedule_cmd(&cmd, &is_available))
1743 return is_available;
1745 #ifdef __APPLE__
1746 return 1;
1747 #else
1748 return 0;
1749 #endif
1752 static char *launchctl_service_name(const char *frequency)
1754 struct strbuf label = STRBUF_INIT;
1755 strbuf_addf(&label, "org.git-scm.git.%s", frequency);
1756 return strbuf_detach(&label, NULL);
1759 static char *launchctl_service_filename(const char *name)
1761 char *expanded;
1762 struct strbuf filename = STRBUF_INIT;
1763 strbuf_addf(&filename, "~/Library/LaunchAgents/%s.plist", name);
1765 expanded = interpolate_path(filename.buf, 1);
1766 if (!expanded)
1767 die(_("failed to expand path '%s'"), filename.buf);
1769 strbuf_release(&filename);
1770 return expanded;
1773 static char *launchctl_get_uid(void)
1775 return xstrfmt("gui/%d", getuid());
1778 static int launchctl_boot_plist(int enable, const char *filename)
1780 const char *cmd = "launchctl";
1781 int result;
1782 struct child_process child = CHILD_PROCESS_INIT;
1783 char *uid = launchctl_get_uid();
1785 get_schedule_cmd(&cmd, NULL);
1786 strvec_split(&child.args, cmd);
1787 strvec_pushl(&child.args, enable ? "bootstrap" : "bootout", uid,
1788 filename, NULL);
1790 child.no_stderr = 1;
1791 child.no_stdout = 1;
1793 if (start_command(&child))
1794 die(_("failed to start launchctl"));
1796 result = finish_command(&child);
1798 free(uid);
1799 return result;
1802 static int launchctl_remove_plist(enum schedule_priority schedule)
1804 const char *frequency = get_frequency(schedule);
1805 char *name = launchctl_service_name(frequency);
1806 char *filename = launchctl_service_filename(name);
1807 int result = launchctl_boot_plist(0, filename);
1808 unlink(filename);
1809 free(filename);
1810 free(name);
1811 return result;
1814 static int launchctl_remove_plists(void)
1816 return launchctl_remove_plist(SCHEDULE_HOURLY) ||
1817 launchctl_remove_plist(SCHEDULE_DAILY) ||
1818 launchctl_remove_plist(SCHEDULE_WEEKLY);
1821 static int launchctl_list_contains_plist(const char *name, const char *cmd)
1823 struct child_process child = CHILD_PROCESS_INIT;
1825 strvec_split(&child.args, cmd);
1826 strvec_pushl(&child.args, "list", name, NULL);
1828 child.no_stderr = 1;
1829 child.no_stdout = 1;
1831 if (start_command(&child))
1832 die(_("failed to start launchctl"));
1834 /* Returns failure if 'name' doesn't exist. */
1835 return !finish_command(&child);
1838 static int launchctl_schedule_plist(const char *exec_path, enum schedule_priority schedule)
1840 int i, fd;
1841 const char *preamble, *repeat;
1842 const char *frequency = get_frequency(schedule);
1843 char *name = launchctl_service_name(frequency);
1844 char *filename = launchctl_service_filename(name);
1845 struct lock_file lk = LOCK_INIT;
1846 static unsigned long lock_file_timeout_ms = ULONG_MAX;
1847 struct strbuf plist = STRBUF_INIT, plist2 = STRBUF_INIT;
1848 struct stat st;
1849 const char *cmd = "launchctl";
1850 int minute = get_random_minute();
1852 get_schedule_cmd(&cmd, NULL);
1853 preamble = "<?xml version=\"1.0\"?>\n"
1854 "<!DOCTYPE plist PUBLIC \"-//Apple//DTD PLIST 1.0//EN\" \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">\n"
1855 "<plist version=\"1.0\">"
1856 "<dict>\n"
1857 "<key>Label</key><string>%s</string>\n"
1858 "<key>ProgramArguments</key>\n"
1859 "<array>\n"
1860 "<string>%s/git</string>\n"
1861 "<string>--exec-path=%s</string>\n"
1862 "<string>for-each-repo</string>\n"
1863 "<string>--config=maintenance.repo</string>\n"
1864 "<string>maintenance</string>\n"
1865 "<string>run</string>\n"
1866 "<string>--schedule=%s</string>\n"
1867 "</array>\n"
1868 "<key>StartCalendarInterval</key>\n"
1869 "<array>\n";
1870 strbuf_addf(&plist, preamble, name, exec_path, exec_path, frequency);
1872 switch (schedule) {
1873 case SCHEDULE_HOURLY:
1874 repeat = "<dict>\n"
1875 "<key>Hour</key><integer>%d</integer>\n"
1876 "<key>Minute</key><integer>%d</integer>\n"
1877 "</dict>\n";
1878 for (i = 1; i <= 23; i++)
1879 strbuf_addf(&plist, repeat, i, minute);
1880 break;
1882 case SCHEDULE_DAILY:
1883 repeat = "<dict>\n"
1884 "<key>Day</key><integer>%d</integer>\n"
1885 "<key>Hour</key><integer>0</integer>\n"
1886 "<key>Minute</key><integer>%d</integer>\n"
1887 "</dict>\n";
1888 for (i = 1; i <= 6; i++)
1889 strbuf_addf(&plist, repeat, i, minute);
1890 break;
1892 case SCHEDULE_WEEKLY:
1893 strbuf_addf(&plist,
1894 "<dict>\n"
1895 "<key>Day</key><integer>0</integer>\n"
1896 "<key>Hour</key><integer>0</integer>\n"
1897 "<key>Minute</key><integer>%d</integer>\n"
1898 "</dict>\n",
1899 minute);
1900 break;
1902 default:
1903 /* unreachable */
1904 break;
1906 strbuf_addstr(&plist, "</array>\n</dict>\n</plist>\n");
1908 if (safe_create_leading_directories(filename))
1909 die(_("failed to create directories for '%s'"), filename);
1911 if ((long)lock_file_timeout_ms < 0 &&
1912 git_config_get_ulong("gc.launchctlplistlocktimeoutms",
1913 &lock_file_timeout_ms))
1914 lock_file_timeout_ms = 150;
1916 fd = hold_lock_file_for_update_timeout(&lk, filename, LOCK_DIE_ON_ERROR,
1917 lock_file_timeout_ms);
1920 * Does this file already exist? With the intended contents? Is it
1921 * registered already? Then it does not need to be re-registered.
1923 if (!stat(filename, &st) && st.st_size == plist.len &&
1924 strbuf_read_file(&plist2, filename, plist.len) == plist.len &&
1925 !strbuf_cmp(&plist, &plist2) &&
1926 launchctl_list_contains_plist(name, cmd))
1927 rollback_lock_file(&lk);
1928 else {
1929 if (write_in_full(fd, plist.buf, plist.len) < 0 ||
1930 commit_lock_file(&lk))
1931 die_errno(_("could not write '%s'"), filename);
1933 /* bootout might fail if not already running, so ignore */
1934 launchctl_boot_plist(0, filename);
1935 if (launchctl_boot_plist(1, filename))
1936 die(_("failed to bootstrap service %s"), filename);
1939 free(filename);
1940 free(name);
1941 strbuf_release(&plist);
1942 strbuf_release(&plist2);
1943 return 0;
1946 static int launchctl_add_plists(void)
1948 const char *exec_path = git_exec_path();
1950 return launchctl_schedule_plist(exec_path, SCHEDULE_HOURLY) ||
1951 launchctl_schedule_plist(exec_path, SCHEDULE_DAILY) ||
1952 launchctl_schedule_plist(exec_path, SCHEDULE_WEEKLY);
1955 static int launchctl_update_schedule(int run_maintenance, int fd UNUSED)
1957 if (run_maintenance)
1958 return launchctl_add_plists();
1959 else
1960 return launchctl_remove_plists();
1963 static int is_schtasks_available(void)
1965 const char *cmd = "schtasks";
1966 int is_available;
1967 if (get_schedule_cmd(&cmd, &is_available))
1968 return is_available;
1970 #ifdef GIT_WINDOWS_NATIVE
1971 return 1;
1972 #else
1973 return 0;
1974 #endif
1977 static char *schtasks_task_name(const char *frequency)
1979 struct strbuf label = STRBUF_INIT;
1980 strbuf_addf(&label, "Git Maintenance (%s)", frequency);
1981 return strbuf_detach(&label, NULL);
1984 static int schtasks_remove_task(enum schedule_priority schedule)
1986 const char *cmd = "schtasks";
1987 struct child_process child = CHILD_PROCESS_INIT;
1988 const char *frequency = get_frequency(schedule);
1989 char *name = schtasks_task_name(frequency);
1991 get_schedule_cmd(&cmd, NULL);
1992 strvec_split(&child.args, cmd);
1993 strvec_pushl(&child.args, "/delete", "/tn", name, "/f", NULL);
1994 free(name);
1996 return run_command(&child);
1999 static int schtasks_remove_tasks(void)
2001 return schtasks_remove_task(SCHEDULE_HOURLY) ||
2002 schtasks_remove_task(SCHEDULE_DAILY) ||
2003 schtasks_remove_task(SCHEDULE_WEEKLY);
2006 static int schtasks_schedule_task(const char *exec_path, enum schedule_priority schedule)
2008 const char *cmd = "schtasks";
2009 int result;
2010 struct child_process child = CHILD_PROCESS_INIT;
2011 const char *xml;
2012 struct tempfile *tfile;
2013 const char *frequency = get_frequency(schedule);
2014 char *name = schtasks_task_name(frequency);
2015 struct strbuf tfilename = STRBUF_INIT;
2016 int minute = get_random_minute();
2018 get_schedule_cmd(&cmd, NULL);
2020 strbuf_addf(&tfilename, "%s/schedule_%s_XXXXXX",
2021 get_git_common_dir(), frequency);
2022 tfile = xmks_tempfile(tfilename.buf);
2023 strbuf_release(&tfilename);
2025 if (!fdopen_tempfile(tfile, "w"))
2026 die(_("failed to create temp xml file"));
2028 xml = "<?xml version=\"1.0\" ?>\n"
2029 "<Task version=\"1.4\" xmlns=\"http://schemas.microsoft.com/windows/2004/02/mit/task\">\n"
2030 "<Triggers>\n"
2031 "<CalendarTrigger>\n";
2032 fputs(xml, tfile->fp);
2034 switch (schedule) {
2035 case SCHEDULE_HOURLY:
2036 fprintf(tfile->fp,
2037 "<StartBoundary>2020-01-01T01:%02d:00</StartBoundary>\n"
2038 "<Enabled>true</Enabled>\n"
2039 "<ScheduleByDay>\n"
2040 "<DaysInterval>1</DaysInterval>\n"
2041 "</ScheduleByDay>\n"
2042 "<Repetition>\n"
2043 "<Interval>PT1H</Interval>\n"
2044 "<Duration>PT23H</Duration>\n"
2045 "<StopAtDurationEnd>false</StopAtDurationEnd>\n"
2046 "</Repetition>\n",
2047 minute);
2048 break;
2050 case SCHEDULE_DAILY:
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 "<Monday />\n"
2057 "<Tuesday />\n"
2058 "<Wednesday />\n"
2059 "<Thursday />\n"
2060 "<Friday />\n"
2061 "<Saturday />\n"
2062 "</DaysOfWeek>\n"
2063 "<WeeksInterval>1</WeeksInterval>\n"
2064 "</ScheduleByWeek>\n",
2065 minute);
2066 break;
2068 case SCHEDULE_WEEKLY:
2069 fprintf(tfile->fp,
2070 "<StartBoundary>2020-01-01T00:%02d:00</StartBoundary>\n"
2071 "<Enabled>true</Enabled>\n"
2072 "<ScheduleByWeek>\n"
2073 "<DaysOfWeek>\n"
2074 "<Sunday />\n"
2075 "</DaysOfWeek>\n"
2076 "<WeeksInterval>1</WeeksInterval>\n"
2077 "</ScheduleByWeek>\n",
2078 minute);
2079 break;
2081 default:
2082 break;
2085 xml = "</CalendarTrigger>\n"
2086 "</Triggers>\n"
2087 "<Principals>\n"
2088 "<Principal id=\"Author\">\n"
2089 "<LogonType>InteractiveToken</LogonType>\n"
2090 "<RunLevel>LeastPrivilege</RunLevel>\n"
2091 "</Principal>\n"
2092 "</Principals>\n"
2093 "<Settings>\n"
2094 "<MultipleInstancesPolicy>IgnoreNew</MultipleInstancesPolicy>\n"
2095 "<Enabled>true</Enabled>\n"
2096 "<Hidden>true</Hidden>\n"
2097 "<UseUnifiedSchedulingEngine>true</UseUnifiedSchedulingEngine>\n"
2098 "<WakeToRun>false</WakeToRun>\n"
2099 "<ExecutionTimeLimit>PT72H</ExecutionTimeLimit>\n"
2100 "<Priority>7</Priority>\n"
2101 "</Settings>\n"
2102 "<Actions Context=\"Author\">\n"
2103 "<Exec>\n"
2104 "<Command>\"%s\\headless-git.exe\"</Command>\n"
2105 "<Arguments>--exec-path=\"%s\" for-each-repo --config=maintenance.repo maintenance run --schedule=%s</Arguments>\n"
2106 "</Exec>\n"
2107 "</Actions>\n"
2108 "</Task>\n";
2109 fprintf(tfile->fp, xml, exec_path, exec_path, frequency);
2110 strvec_split(&child.args, cmd);
2111 strvec_pushl(&child.args, "/create", "/tn", name, "/f", "/xml",
2112 get_tempfile_path(tfile), NULL);
2113 close_tempfile_gently(tfile);
2115 child.no_stdout = 1;
2116 child.no_stderr = 1;
2118 if (start_command(&child))
2119 die(_("failed to start schtasks"));
2120 result = finish_command(&child);
2122 delete_tempfile(&tfile);
2123 free(name);
2124 return result;
2127 static int schtasks_schedule_tasks(void)
2129 const char *exec_path = git_exec_path();
2131 return schtasks_schedule_task(exec_path, SCHEDULE_HOURLY) ||
2132 schtasks_schedule_task(exec_path, SCHEDULE_DAILY) ||
2133 schtasks_schedule_task(exec_path, SCHEDULE_WEEKLY);
2136 static int schtasks_update_schedule(int run_maintenance, int fd UNUSED)
2138 if (run_maintenance)
2139 return schtasks_schedule_tasks();
2140 else
2141 return schtasks_remove_tasks();
2144 MAYBE_UNUSED
2145 static int check_crontab_process(const char *cmd)
2147 struct child_process child = CHILD_PROCESS_INIT;
2149 strvec_split(&child.args, cmd);
2150 strvec_push(&child.args, "-l");
2151 child.no_stdin = 1;
2152 child.no_stdout = 1;
2153 child.no_stderr = 1;
2154 child.silent_exec_failure = 1;
2156 if (start_command(&child))
2157 return 0;
2158 /* Ignore exit code, as an empty crontab will return error. */
2159 finish_command(&child);
2160 return 1;
2163 static int is_crontab_available(void)
2165 const char *cmd = "crontab";
2166 int is_available;
2168 if (get_schedule_cmd(&cmd, &is_available))
2169 return is_available;
2171 #ifdef __APPLE__
2173 * macOS has cron, but it requires special permissions and will
2174 * create a UI alert when attempting to run this command.
2176 return 0;
2177 #else
2178 return check_crontab_process(cmd);
2179 #endif
2182 #define BEGIN_LINE "# BEGIN GIT MAINTENANCE SCHEDULE"
2183 #define END_LINE "# END GIT MAINTENANCE SCHEDULE"
2185 static int crontab_update_schedule(int run_maintenance, int fd)
2187 const char *cmd = "crontab";
2188 int result = 0;
2189 int in_old_region = 0;
2190 struct child_process crontab_list = CHILD_PROCESS_INIT;
2191 struct child_process crontab_edit = CHILD_PROCESS_INIT;
2192 FILE *cron_list, *cron_in;
2193 struct strbuf line = STRBUF_INIT;
2194 struct tempfile *tmpedit = NULL;
2195 int minute = get_random_minute();
2197 get_schedule_cmd(&cmd, NULL);
2198 strvec_split(&crontab_list.args, cmd);
2199 strvec_push(&crontab_list.args, "-l");
2200 crontab_list.in = -1;
2201 crontab_list.out = dup(fd);
2202 crontab_list.git_cmd = 0;
2204 if (start_command(&crontab_list))
2205 return error(_("failed to run 'crontab -l'; your system might not support 'cron'"));
2207 /* Ignore exit code, as an empty crontab will return error. */
2208 finish_command(&crontab_list);
2210 tmpedit = mks_tempfile_t(".git_cron_edit_tmpXXXXXX");
2211 if (!tmpedit) {
2212 result = error(_("failed to create crontab temporary file"));
2213 goto out;
2215 cron_in = fdopen_tempfile(tmpedit, "w");
2216 if (!cron_in) {
2217 result = error(_("failed to open temporary file"));
2218 goto out;
2222 * Read from the .lock file, filtering out the old
2223 * schedule while appending the new schedule.
2225 cron_list = fdopen(fd, "r");
2226 rewind(cron_list);
2228 while (!strbuf_getline_lf(&line, cron_list)) {
2229 if (!in_old_region && !strcmp(line.buf, BEGIN_LINE))
2230 in_old_region = 1;
2231 else if (in_old_region && !strcmp(line.buf, END_LINE))
2232 in_old_region = 0;
2233 else if (!in_old_region)
2234 fprintf(cron_in, "%s\n", line.buf);
2236 strbuf_release(&line);
2238 if (run_maintenance) {
2239 struct strbuf line_format = STRBUF_INIT;
2240 const char *exec_path = git_exec_path();
2242 fprintf(cron_in, "%s\n", BEGIN_LINE);
2243 fprintf(cron_in,
2244 "# The following schedule was created by Git\n");
2245 fprintf(cron_in, "# Any edits made in this region might be\n");
2246 fprintf(cron_in,
2247 "# replaced in the future by a Git command.\n\n");
2249 strbuf_addf(&line_format,
2250 "%%d %%s * * %%s \"%s/git\" --exec-path=\"%s\" for-each-repo --config=maintenance.repo maintenance run --schedule=%%s\n",
2251 exec_path, exec_path);
2252 fprintf(cron_in, line_format.buf, minute, "1-23", "*", "hourly");
2253 fprintf(cron_in, line_format.buf, minute, "0", "1-6", "daily");
2254 fprintf(cron_in, line_format.buf, minute, "0", "0", "weekly");
2255 strbuf_release(&line_format);
2257 fprintf(cron_in, "\n%s\n", END_LINE);
2260 fflush(cron_in);
2262 strvec_split(&crontab_edit.args, cmd);
2263 strvec_push(&crontab_edit.args, get_tempfile_path(tmpedit));
2264 crontab_edit.git_cmd = 0;
2266 if (start_command(&crontab_edit)) {
2267 result = error(_("failed to run 'crontab'; your system might not support 'cron'"));
2268 goto out;
2271 if (finish_command(&crontab_edit))
2272 result = error(_("'crontab' died"));
2273 else
2274 fclose(cron_list);
2275 out:
2276 delete_tempfile(&tmpedit);
2277 return result;
2280 static int real_is_systemd_timer_available(void)
2282 struct child_process child = CHILD_PROCESS_INIT;
2284 strvec_pushl(&child.args, "systemctl", "--user", "list-timers", NULL);
2285 child.no_stdin = 1;
2286 child.no_stdout = 1;
2287 child.no_stderr = 1;
2288 child.silent_exec_failure = 1;
2290 if (start_command(&child))
2291 return 0;
2292 if (finish_command(&child))
2293 return 0;
2294 return 1;
2297 static int is_systemd_timer_available(void)
2299 const char *cmd = "systemctl";
2300 int is_available;
2302 if (get_schedule_cmd(&cmd, &is_available))
2303 return is_available;
2305 return real_is_systemd_timer_available();
2308 static char *xdg_config_home_systemd(const char *filename)
2310 return xdg_config_home_for("systemd/user", filename);
2313 #define SYSTEMD_UNIT_FORMAT "git-maintenance@%s.%s"
2315 static int systemd_timer_delete_timer_file(enum schedule_priority priority)
2317 int ret = 0;
2318 const char *frequency = get_frequency(priority);
2319 char *local_timer_name = xstrfmt(SYSTEMD_UNIT_FORMAT, frequency, "timer");
2320 char *filename = xdg_config_home_systemd(local_timer_name);
2322 if (unlink(filename) && !is_missing_file_error(errno))
2323 ret = error_errno(_("failed to delete '%s'"), filename);
2325 free(filename);
2326 free(local_timer_name);
2327 return ret;
2330 static int systemd_timer_delete_service_template(void)
2332 int ret = 0;
2333 char *local_service_name = xstrfmt(SYSTEMD_UNIT_FORMAT, "", "service");
2334 char *filename = xdg_config_home_systemd(local_service_name);
2335 if (unlink(filename) && !is_missing_file_error(errno))
2336 ret = error_errno(_("failed to delete '%s'"), filename);
2338 free(filename);
2339 free(local_service_name);
2340 return ret;
2344 * Write the schedule information into a git-maintenance@<schedule>.timer
2345 * file using a custom minute. This timer file cannot use the templating
2346 * system, so we generate a specific file for each.
2348 static int systemd_timer_write_timer_file(enum schedule_priority schedule,
2349 int minute)
2351 int res = -1;
2352 char *filename;
2353 FILE *file;
2354 const char *unit;
2355 char *schedule_pattern = NULL;
2356 const char *frequency = get_frequency(schedule);
2357 char *local_timer_name = xstrfmt(SYSTEMD_UNIT_FORMAT, frequency, "timer");
2359 filename = xdg_config_home_systemd(local_timer_name);
2361 if (safe_create_leading_directories(filename)) {
2362 error(_("failed to create directories for '%s'"), filename);
2363 goto error;
2365 file = fopen_or_warn(filename, "w");
2366 if (!file)
2367 goto error;
2369 switch (schedule) {
2370 case SCHEDULE_HOURLY:
2371 schedule_pattern = xstrfmt("*-*-* 1..23:%02d:00", minute);
2372 break;
2374 case SCHEDULE_DAILY:
2375 schedule_pattern = xstrfmt("Tue..Sun *-*-* 0:%02d:00", minute);
2376 break;
2378 case SCHEDULE_WEEKLY:
2379 schedule_pattern = xstrfmt("Mon 0:%02d:00", minute);
2380 break;
2382 default:
2383 BUG("Unhandled schedule_priority");
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 "[Timer]\n"
2394 "OnCalendar=%s\n"
2395 "Persistent=true\n"
2396 "\n"
2397 "[Install]\n"
2398 "WantedBy=timers.target\n";
2399 if (fprintf(file, unit, schedule_pattern) < 0) {
2400 error(_("failed to write to '%s'"), filename);
2401 fclose(file);
2402 goto error;
2404 if (fclose(file) == EOF) {
2405 error_errno(_("failed to flush '%s'"), filename);
2406 goto error;
2409 res = 0;
2411 error:
2412 free(schedule_pattern);
2413 free(local_timer_name);
2414 free(filename);
2415 return res;
2419 * No matter the schedule, we use the same service and can make use of the
2420 * templating system. When installing git-maintenance@<schedule>.timer,
2421 * systemd will notice that git-maintenance@.service exists as a template
2422 * and will use this file and insert the <schedule> into the template at
2423 * the position of "%i".
2425 static int systemd_timer_write_service_template(const char *exec_path)
2427 int res = -1;
2428 char *filename;
2429 FILE *file;
2430 const char *unit;
2431 char *local_service_name = xstrfmt(SYSTEMD_UNIT_FORMAT, "", "service");
2433 filename = xdg_config_home_systemd(local_service_name);
2434 if (safe_create_leading_directories(filename)) {
2435 error(_("failed to create directories for '%s'"), filename);
2436 goto error;
2438 file = fopen_or_warn(filename, "w");
2439 if (!file)
2440 goto error;
2442 unit = "# This file was created and is maintained by Git.\n"
2443 "# Any edits made in this file might be replaced in the future\n"
2444 "# by a Git command.\n"
2445 "\n"
2446 "[Unit]\n"
2447 "Description=Optimize Git repositories data\n"
2448 "\n"
2449 "[Service]\n"
2450 "Type=oneshot\n"
2451 "ExecStart=\"%s/git\" --exec-path=\"%s\" for-each-repo --config=maintenance.repo maintenance run --schedule=%%i\n"
2452 "LockPersonality=yes\n"
2453 "MemoryDenyWriteExecute=yes\n"
2454 "NoNewPrivileges=yes\n"
2455 "RestrictAddressFamilies=AF_UNIX AF_INET AF_INET6 AF_VSOCK\n"
2456 "RestrictNamespaces=yes\n"
2457 "RestrictRealtime=yes\n"
2458 "RestrictSUIDSGID=yes\n"
2459 "SystemCallArchitectures=native\n"
2460 "SystemCallFilter=@system-service\n";
2461 if (fprintf(file, unit, exec_path, exec_path) < 0) {
2462 error(_("failed to write to '%s'"), filename);
2463 fclose(file);
2464 goto error;
2466 if (fclose(file) == EOF) {
2467 error_errno(_("failed to flush '%s'"), filename);
2468 goto error;
2471 res = 0;
2473 error:
2474 free(local_service_name);
2475 free(filename);
2476 return res;
2479 static int systemd_timer_enable_unit(int enable,
2480 enum schedule_priority schedule,
2481 int minute)
2483 const char *cmd = "systemctl";
2484 struct child_process child = CHILD_PROCESS_INIT;
2485 const char *frequency = get_frequency(schedule);
2488 * Disabling the systemd unit while it is already disabled makes
2489 * systemctl print an error.
2490 * Let's ignore it since it means we already are in the expected state:
2491 * the unit is disabled.
2493 * On the other hand, enabling a systemd unit which is already enabled
2494 * produces no error.
2496 if (!enable)
2497 child.no_stderr = 1;
2498 else if (systemd_timer_write_timer_file(schedule, minute))
2499 return -1;
2501 get_schedule_cmd(&cmd, NULL);
2502 strvec_split(&child.args, cmd);
2503 strvec_pushl(&child.args, "--user", enable ? "enable" : "disable",
2504 "--now", NULL);
2505 strvec_pushf(&child.args, SYSTEMD_UNIT_FORMAT, frequency, "timer");
2507 if (start_command(&child))
2508 return error(_("failed to start systemctl"));
2509 if (finish_command(&child))
2511 * Disabling an already disabled systemd unit makes
2512 * systemctl fail.
2513 * Let's ignore this failure.
2515 * Enabling an enabled systemd unit doesn't fail.
2517 if (enable)
2518 return error(_("failed to run systemctl"));
2519 return 0;
2523 * A previous version of Git wrote the timer units as template files.
2524 * Clean these up, if they exist.
2526 static void systemd_timer_delete_stale_timer_templates(void)
2528 char *timer_template_name = xstrfmt(SYSTEMD_UNIT_FORMAT, "", "timer");
2529 char *filename = xdg_config_home_systemd(timer_template_name);
2531 if (unlink(filename) && !is_missing_file_error(errno))
2532 warning(_("failed to delete '%s'"), filename);
2534 free(filename);
2535 free(timer_template_name);
2538 static int systemd_timer_delete_unit_files(void)
2540 systemd_timer_delete_stale_timer_templates();
2542 /* Purposefully not short-circuited to make sure all are called. */
2543 return systemd_timer_delete_timer_file(SCHEDULE_HOURLY) |
2544 systemd_timer_delete_timer_file(SCHEDULE_DAILY) |
2545 systemd_timer_delete_timer_file(SCHEDULE_WEEKLY) |
2546 systemd_timer_delete_service_template();
2549 static int systemd_timer_delete_units(void)
2551 int minute = get_random_minute();
2552 /* Purposefully not short-circuited to make sure all are called. */
2553 return systemd_timer_enable_unit(0, SCHEDULE_HOURLY, minute) |
2554 systemd_timer_enable_unit(0, SCHEDULE_DAILY, minute) |
2555 systemd_timer_enable_unit(0, SCHEDULE_WEEKLY, minute) |
2556 systemd_timer_delete_unit_files();
2559 static int systemd_timer_setup_units(void)
2561 int minute = get_random_minute();
2562 const char *exec_path = git_exec_path();
2564 int ret = systemd_timer_write_service_template(exec_path) ||
2565 systemd_timer_enable_unit(1, SCHEDULE_HOURLY, minute) ||
2566 systemd_timer_enable_unit(1, SCHEDULE_DAILY, minute) ||
2567 systemd_timer_enable_unit(1, SCHEDULE_WEEKLY, minute);
2569 if (ret)
2570 systemd_timer_delete_units();
2571 else
2572 systemd_timer_delete_stale_timer_templates();
2574 return ret;
2577 static int systemd_timer_update_schedule(int run_maintenance, int fd UNUSED)
2579 if (run_maintenance)
2580 return systemd_timer_setup_units();
2581 else
2582 return systemd_timer_delete_units();
2585 enum scheduler {
2586 SCHEDULER_INVALID = -1,
2587 SCHEDULER_AUTO,
2588 SCHEDULER_CRON,
2589 SCHEDULER_SYSTEMD,
2590 SCHEDULER_LAUNCHCTL,
2591 SCHEDULER_SCHTASKS,
2594 static const struct {
2595 const char *name;
2596 int (*is_available)(void);
2597 int (*update_schedule)(int run_maintenance, int fd);
2598 } scheduler_fn[] = {
2599 [SCHEDULER_CRON] = {
2600 .name = "crontab",
2601 .is_available = is_crontab_available,
2602 .update_schedule = crontab_update_schedule,
2604 [SCHEDULER_SYSTEMD] = {
2605 .name = "systemctl",
2606 .is_available = is_systemd_timer_available,
2607 .update_schedule = systemd_timer_update_schedule,
2609 [SCHEDULER_LAUNCHCTL] = {
2610 .name = "launchctl",
2611 .is_available = is_launchctl_available,
2612 .update_schedule = launchctl_update_schedule,
2614 [SCHEDULER_SCHTASKS] = {
2615 .name = "schtasks",
2616 .is_available = is_schtasks_available,
2617 .update_schedule = schtasks_update_schedule,
2621 static enum scheduler parse_scheduler(const char *value)
2623 if (!value)
2624 return SCHEDULER_INVALID;
2625 else if (!strcasecmp(value, "auto"))
2626 return SCHEDULER_AUTO;
2627 else if (!strcasecmp(value, "cron") || !strcasecmp(value, "crontab"))
2628 return SCHEDULER_CRON;
2629 else if (!strcasecmp(value, "systemd") ||
2630 !strcasecmp(value, "systemd-timer"))
2631 return SCHEDULER_SYSTEMD;
2632 else if (!strcasecmp(value, "launchctl"))
2633 return SCHEDULER_LAUNCHCTL;
2634 else if (!strcasecmp(value, "schtasks"))
2635 return SCHEDULER_SCHTASKS;
2636 else
2637 return SCHEDULER_INVALID;
2640 static int maintenance_opt_scheduler(const struct option *opt, const char *arg,
2641 int unset)
2643 enum scheduler *scheduler = opt->value;
2645 BUG_ON_OPT_NEG(unset);
2647 *scheduler = parse_scheduler(arg);
2648 if (*scheduler == SCHEDULER_INVALID)
2649 return error(_("unrecognized --scheduler argument '%s'"), arg);
2650 return 0;
2653 struct maintenance_start_opts {
2654 enum scheduler scheduler;
2657 static enum scheduler resolve_scheduler(enum scheduler scheduler)
2659 if (scheduler != SCHEDULER_AUTO)
2660 return scheduler;
2662 #if defined(__APPLE__)
2663 return SCHEDULER_LAUNCHCTL;
2665 #elif defined(GIT_WINDOWS_NATIVE)
2666 return SCHEDULER_SCHTASKS;
2668 #elif defined(__linux__)
2669 if (is_systemd_timer_available())
2670 return SCHEDULER_SYSTEMD;
2671 else if (is_crontab_available())
2672 return SCHEDULER_CRON;
2673 else
2674 die(_("neither systemd timers nor crontab are available"));
2676 #else
2677 return SCHEDULER_CRON;
2678 #endif
2681 static void validate_scheduler(enum scheduler scheduler)
2683 if (scheduler == SCHEDULER_INVALID)
2684 BUG("invalid scheduler");
2685 if (scheduler == SCHEDULER_AUTO)
2686 BUG("resolve_scheduler should have been called before");
2688 if (!scheduler_fn[scheduler].is_available())
2689 die(_("%s scheduler is not available"),
2690 scheduler_fn[scheduler].name);
2693 static int update_background_schedule(const struct maintenance_start_opts *opts,
2694 int enable)
2696 unsigned int i;
2697 int result = 0;
2698 struct lock_file lk;
2699 char *lock_path = xstrfmt("%s/schedule", the_repository->objects->odb->path);
2701 if (hold_lock_file_for_update(&lk, lock_path, LOCK_NO_DEREF) < 0) {
2702 free(lock_path);
2703 return error(_("another process is scheduling background maintenance"));
2706 for (i = 1; i < ARRAY_SIZE(scheduler_fn); i++) {
2707 if (enable && opts->scheduler == i)
2708 continue;
2709 if (!scheduler_fn[i].is_available())
2710 continue;
2711 scheduler_fn[i].update_schedule(0, get_lock_file_fd(&lk));
2714 if (enable)
2715 result = scheduler_fn[opts->scheduler].update_schedule(
2716 1, get_lock_file_fd(&lk));
2718 rollback_lock_file(&lk);
2720 free(lock_path);
2721 return result;
2724 static const char *const builtin_maintenance_start_usage[] = {
2725 N_("git maintenance start [--scheduler=<scheduler>]"),
2726 NULL
2729 static int maintenance_start(int argc, const char **argv, const char *prefix)
2731 struct maintenance_start_opts opts = { 0 };
2732 struct option options[] = {
2733 OPT_CALLBACK_F(
2734 0, "scheduler", &opts.scheduler, N_("scheduler"),
2735 N_("scheduler to trigger git maintenance run"),
2736 PARSE_OPT_NONEG, maintenance_opt_scheduler),
2737 OPT_END()
2739 const char *register_args[] = { "register", NULL };
2741 argc = parse_options(argc, argv, prefix, options,
2742 builtin_maintenance_start_usage, 0);
2743 if (argc)
2744 usage_with_options(builtin_maintenance_start_usage, options);
2746 opts.scheduler = resolve_scheduler(opts.scheduler);
2747 validate_scheduler(opts.scheduler);
2749 if (update_background_schedule(&opts, 1))
2750 die(_("failed to set up maintenance schedule"));
2752 if (maintenance_register(ARRAY_SIZE(register_args)-1, register_args, NULL))
2753 warning(_("failed to add repo to global config"));
2754 return 0;
2757 static const char *const builtin_maintenance_stop_usage[] = {
2758 "git maintenance stop",
2759 NULL
2762 static int maintenance_stop(int argc, const char **argv, const char *prefix)
2764 struct option options[] = {
2765 OPT_END()
2767 argc = parse_options(argc, argv, prefix, options,
2768 builtin_maintenance_stop_usage, 0);
2769 if (argc)
2770 usage_with_options(builtin_maintenance_stop_usage, options);
2771 return update_background_schedule(NULL, 0);
2774 static const char * const builtin_maintenance_usage[] = {
2775 N_("git maintenance <subcommand> [<options>]"),
2776 NULL,
2779 int cmd_maintenance(int argc, const char **argv, const char *prefix)
2781 parse_opt_subcommand_fn *fn = NULL;
2782 struct option builtin_maintenance_options[] = {
2783 OPT_SUBCOMMAND("run", &fn, maintenance_run),
2784 OPT_SUBCOMMAND("start", &fn, maintenance_start),
2785 OPT_SUBCOMMAND("stop", &fn, maintenance_stop),
2786 OPT_SUBCOMMAND("register", &fn, maintenance_register),
2787 OPT_SUBCOMMAND("unregister", &fn, maintenance_unregister),
2788 OPT_END(),
2791 argc = parse_options(argc, argv, prefix, builtin_maintenance_options,
2792 builtin_maintenance_usage, 0);
2793 return fn(argc, argv, prefix);