builtin/repack.c: implement support for `--max-cruft-size`
[git/gitster.git] / builtin / gc.c
blobc97c9fb04644456866d43c147e7e3e735f1be097
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 unsigned long big_pack_threshold;
66 static unsigned long max_delta_cache_size = DEFAULT_DELTA_CACHE_SIZE;
68 static struct strvec reflog = STRVEC_INIT;
69 static struct strvec repack = STRVEC_INIT;
70 static struct strvec prune = STRVEC_INIT;
71 static struct strvec prune_worktrees = STRVEC_INIT;
72 static struct strvec rerere = STRVEC_INIT;
74 static struct tempfile *pidfile;
75 static struct lock_file log_lock;
77 static struct string_list pack_garbage = STRING_LIST_INIT_DUP;
79 static void clean_pack_garbage(void)
81 int i;
82 for (i = 0; i < pack_garbage.nr; i++)
83 unlink_or_warn(pack_garbage.items[i].string);
84 string_list_clear(&pack_garbage, 0);
87 static void report_pack_garbage(unsigned seen_bits, const char *path)
89 if (seen_bits == PACKDIR_FILE_IDX)
90 string_list_append(&pack_garbage, path);
93 static void process_log_file(void)
95 struct stat st;
96 if (fstat(get_lock_file_fd(&log_lock), &st)) {
98 * Perhaps there was an i/o error or another
99 * unlikely situation. Try to make a note of
100 * this in gc.log along with any existing
101 * messages.
103 int saved_errno = errno;
104 fprintf(stderr, _("Failed to fstat %s: %s"),
105 get_lock_file_path(&log_lock),
106 strerror(saved_errno));
107 fflush(stderr);
108 commit_lock_file(&log_lock);
109 errno = saved_errno;
110 } else if (st.st_size) {
111 /* There was some error recorded in the lock file */
112 commit_lock_file(&log_lock);
113 } else {
114 /* No error, clean up any old gc.log */
115 unlink(git_path("gc.log"));
116 rollback_lock_file(&log_lock);
120 static void process_log_file_at_exit(void)
122 fflush(stderr);
123 process_log_file();
126 static void process_log_file_on_signal(int signo)
128 process_log_file();
129 sigchain_pop(signo);
130 raise(signo);
133 static int gc_config_is_timestamp_never(const char *var)
135 const char *value;
136 timestamp_t expire;
138 if (!git_config_get_value(var, &value) && value) {
139 if (parse_expiry_date(value, &expire))
140 die(_("failed to parse '%s' value '%s'"), var, value);
141 return expire == 0;
143 return 0;
146 static void gc_config(void)
148 const char *value;
150 if (!git_config_get_value("gc.packrefs", &value)) {
151 if (value && !strcmp(value, "notbare"))
152 pack_refs = -1;
153 else
154 pack_refs = git_config_bool("gc.packrefs", value);
157 if (gc_config_is_timestamp_never("gc.reflogexpire") &&
158 gc_config_is_timestamp_never("gc.reflogexpireunreachable"))
159 prune_reflogs = 0;
161 git_config_get_int("gc.aggressivewindow", &aggressive_window);
162 git_config_get_int("gc.aggressivedepth", &aggressive_depth);
163 git_config_get_int("gc.auto", &gc_auto_threshold);
164 git_config_get_int("gc.autopacklimit", &gc_auto_pack_limit);
165 git_config_get_bool("gc.autodetach", &detach_auto);
166 git_config_get_bool("gc.cruftpacks", &cruft_packs);
167 git_config_get_ulong("gc.maxcruftsize", &max_cruft_size);
168 git_config_get_expiry("gc.pruneexpire", &prune_expire);
169 git_config_get_expiry("gc.worktreepruneexpire", &prune_worktrees_expire);
170 git_config_get_expiry("gc.logexpiry", &gc_log_expire);
172 git_config_get_ulong("gc.bigpackthreshold", &big_pack_threshold);
173 git_config_get_ulong("pack.deltacachesize", &max_delta_cache_size);
175 git_config(git_default_config, NULL);
178 struct maintenance_run_opts;
179 static int maintenance_task_pack_refs(MAYBE_UNUSED struct maintenance_run_opts *opts)
181 struct child_process cmd = CHILD_PROCESS_INIT;
183 cmd.git_cmd = 1;
184 strvec_pushl(&cmd.args, "pack-refs", "--all", "--prune", NULL);
185 return run_command(&cmd);
188 static int too_many_loose_objects(void)
191 * Quickly check if a "gc" is needed, by estimating how
192 * many loose objects there are. Because SHA-1 is evenly
193 * distributed, we can check only one and get a reasonable
194 * estimate.
196 DIR *dir;
197 struct dirent *ent;
198 int auto_threshold;
199 int num_loose = 0;
200 int needed = 0;
201 const unsigned hexsz_loose = the_hash_algo->hexsz - 2;
203 dir = opendir(git_path("objects/17"));
204 if (!dir)
205 return 0;
207 auto_threshold = DIV_ROUND_UP(gc_auto_threshold, 256);
208 while ((ent = readdir(dir)) != NULL) {
209 if (strspn(ent->d_name, "0123456789abcdef") != hexsz_loose ||
210 ent->d_name[hexsz_loose] != '\0')
211 continue;
212 if (++num_loose > auto_threshold) {
213 needed = 1;
214 break;
217 closedir(dir);
218 return needed;
221 static struct packed_git *find_base_packs(struct string_list *packs,
222 unsigned long limit)
224 struct packed_git *p, *base = NULL;
226 for (p = get_all_packs(the_repository); p; p = p->next) {
227 if (!p->pack_local || p->is_cruft)
228 continue;
229 if (limit) {
230 if (p->pack_size >= limit)
231 string_list_append(packs, p->pack_name);
232 } else if (!base || base->pack_size < p->pack_size) {
233 base = p;
237 if (base)
238 string_list_append(packs, base->pack_name);
240 return base;
243 static int too_many_packs(void)
245 struct packed_git *p;
246 int cnt;
248 if (gc_auto_pack_limit <= 0)
249 return 0;
251 for (cnt = 0, p = get_all_packs(the_repository); p; p = p->next) {
252 if (!p->pack_local)
253 continue;
254 if (p->pack_keep)
255 continue;
257 * Perhaps check the size of the pack and count only
258 * very small ones here?
260 cnt++;
262 return gc_auto_pack_limit < cnt;
265 static uint64_t total_ram(void)
267 #if defined(HAVE_SYSINFO)
268 struct sysinfo si;
270 if (!sysinfo(&si))
271 return si.totalram;
272 #elif defined(HAVE_BSD_SYSCTL) && (defined(HW_MEMSIZE) || defined(HW_PHYSMEM))
273 int64_t physical_memory;
274 int mib[2];
275 size_t length;
277 mib[0] = CTL_HW;
278 # if defined(HW_MEMSIZE)
279 mib[1] = HW_MEMSIZE;
280 # else
281 mib[1] = HW_PHYSMEM;
282 # endif
283 length = sizeof(int64_t);
284 if (!sysctl(mib, 2, &physical_memory, &length, NULL, 0))
285 return physical_memory;
286 #elif defined(GIT_WINDOWS_NATIVE)
287 MEMORYSTATUSEX memInfo;
289 memInfo.dwLength = sizeof(MEMORYSTATUSEX);
290 if (GlobalMemoryStatusEx(&memInfo))
291 return memInfo.ullTotalPhys;
292 #endif
293 return 0;
296 static uint64_t estimate_repack_memory(struct packed_git *pack)
298 unsigned long nr_objects = repo_approximate_object_count(the_repository);
299 size_t os_cache, heap;
301 if (!pack || !nr_objects)
302 return 0;
305 * First we have to scan through at least one pack.
306 * Assume enough room in OS file cache to keep the entire pack
307 * or we may accidentally evict data of other processes from
308 * the cache.
310 os_cache = pack->pack_size + pack->index_size;
311 /* then pack-objects needs lots more for book keeping */
312 heap = sizeof(struct object_entry) * nr_objects;
314 * internal rev-list --all --objects takes up some memory too,
315 * let's say half of it is for blobs
317 heap += sizeof(struct blob) * nr_objects / 2;
319 * and the other half is for trees (commits and tags are
320 * usually insignificant)
322 heap += sizeof(struct tree) * nr_objects / 2;
323 /* and then obj_hash[], underestimated in fact */
324 heap += sizeof(struct object *) * nr_objects;
325 /* revindex is used also */
326 heap += (sizeof(off_t) + sizeof(uint32_t)) * nr_objects;
328 * read_sha1_file() (either at delta calculation phase, or
329 * writing phase) also fills up the delta base cache
331 heap += delta_base_cache_limit;
332 /* and of course pack-objects has its own delta cache */
333 heap += max_delta_cache_size;
335 return os_cache + heap;
338 static int keep_one_pack(struct string_list_item *item, void *data UNUSED)
340 strvec_pushf(&repack, "--keep-pack=%s", basename(item->string));
341 return 0;
344 static void add_repack_all_option(struct string_list *keep_pack)
346 if (prune_expire && !strcmp(prune_expire, "now"))
347 strvec_push(&repack, "-a");
348 else if (cruft_packs) {
349 strvec_push(&repack, "--cruft");
350 if (prune_expire)
351 strvec_pushf(&repack, "--cruft-expiration=%s", prune_expire);
352 if (max_cruft_size)
353 strvec_pushf(&repack, "--max-cruft-size=%lu",
354 max_cruft_size);
355 } else {
356 strvec_push(&repack, "-A");
357 if (prune_expire)
358 strvec_pushf(&repack, "--unpack-unreachable=%s", prune_expire);
361 if (keep_pack)
362 for_each_string_list(keep_pack, keep_one_pack, NULL);
365 static void add_repack_incremental_option(void)
367 strvec_push(&repack, "--no-write-bitmap-index");
370 static int need_to_gc(void)
373 * Setting gc.auto to 0 or negative can disable the
374 * automatic gc.
376 if (gc_auto_threshold <= 0)
377 return 0;
380 * If there are too many loose objects, but not too many
381 * packs, we run "repack -d -l". If there are too many packs,
382 * we run "repack -A -d -l". Otherwise we tell the caller
383 * there is no need.
385 if (too_many_packs()) {
386 struct string_list keep_pack = STRING_LIST_INIT_NODUP;
388 if (big_pack_threshold) {
389 find_base_packs(&keep_pack, big_pack_threshold);
390 if (keep_pack.nr >= gc_auto_pack_limit) {
391 big_pack_threshold = 0;
392 string_list_clear(&keep_pack, 0);
393 find_base_packs(&keep_pack, 0);
395 } else {
396 struct packed_git *p = find_base_packs(&keep_pack, 0);
397 uint64_t mem_have, mem_want;
399 mem_have = total_ram();
400 mem_want = estimate_repack_memory(p);
403 * Only allow 1/2 of memory for pack-objects, leave
404 * the rest for the OS and other processes in the
405 * system.
407 if (!mem_have || mem_want < mem_have / 2)
408 string_list_clear(&keep_pack, 0);
411 add_repack_all_option(&keep_pack);
412 string_list_clear(&keep_pack, 0);
413 } else if (too_many_loose_objects())
414 add_repack_incremental_option();
415 else
416 return 0;
418 if (run_hooks("pre-auto-gc"))
419 return 0;
420 return 1;
423 /* return NULL on success, else hostname running the gc */
424 static const char *lock_repo_for_gc(int force, pid_t* ret_pid)
426 struct lock_file lock = LOCK_INIT;
427 char my_host[HOST_NAME_MAX + 1];
428 struct strbuf sb = STRBUF_INIT;
429 struct stat st;
430 uintmax_t pid;
431 FILE *fp;
432 int fd;
433 char *pidfile_path;
435 if (is_tempfile_active(pidfile))
436 /* already locked */
437 return NULL;
439 if (xgethostname(my_host, sizeof(my_host)))
440 xsnprintf(my_host, sizeof(my_host), "unknown");
442 pidfile_path = git_pathdup("gc.pid");
443 fd = hold_lock_file_for_update(&lock, pidfile_path,
444 LOCK_DIE_ON_ERROR);
445 if (!force) {
446 static char locking_host[HOST_NAME_MAX + 1];
447 static char *scan_fmt;
448 int should_exit;
450 if (!scan_fmt)
451 scan_fmt = xstrfmt("%s %%%ds", "%"SCNuMAX, HOST_NAME_MAX);
452 fp = fopen(pidfile_path, "r");
453 memset(locking_host, 0, sizeof(locking_host));
454 should_exit =
455 fp != NULL &&
456 !fstat(fileno(fp), &st) &&
458 * 12 hour limit is very generous as gc should
459 * never take that long. On the other hand we
460 * don't really need a strict limit here,
461 * running gc --auto one day late is not a big
462 * problem. --force can be used in manual gc
463 * after the user verifies that no gc is
464 * running.
466 time(NULL) - st.st_mtime <= 12 * 3600 &&
467 fscanf(fp, scan_fmt, &pid, locking_host) == 2 &&
468 /* be gentle to concurrent "gc" on remote hosts */
469 (strcmp(locking_host, my_host) || !kill(pid, 0) || errno == EPERM);
470 if (fp)
471 fclose(fp);
472 if (should_exit) {
473 if (fd >= 0)
474 rollback_lock_file(&lock);
475 *ret_pid = pid;
476 free(pidfile_path);
477 return locking_host;
481 strbuf_addf(&sb, "%"PRIuMAX" %s",
482 (uintmax_t) getpid(), my_host);
483 write_in_full(fd, sb.buf, sb.len);
484 strbuf_release(&sb);
485 commit_lock_file(&lock);
486 pidfile = register_tempfile(pidfile_path);
487 free(pidfile_path);
488 return NULL;
492 * Returns 0 if there was no previous error and gc can proceed, 1 if
493 * gc should not proceed due to an error in the last run. Prints a
494 * message and returns with a non-[01] status code if an error occurred
495 * while reading gc.log
497 static int report_last_gc_error(void)
499 struct strbuf sb = STRBUF_INIT;
500 int ret = 0;
501 ssize_t len;
502 struct stat st;
503 char *gc_log_path = git_pathdup("gc.log");
505 if (stat(gc_log_path, &st)) {
506 if (errno == ENOENT)
507 goto done;
509 ret = die_message_errno(_("cannot stat '%s'"), gc_log_path);
510 goto done;
513 if (st.st_mtime < gc_log_expire_time)
514 goto done;
516 len = strbuf_read_file(&sb, gc_log_path, 0);
517 if (len < 0)
518 ret = die_message_errno(_("cannot read '%s'"), gc_log_path);
519 else if (len > 0) {
521 * A previous gc failed. Report the error, and don't
522 * bother with an automatic gc run since it is likely
523 * to fail in the same way.
525 warning(_("The last gc run reported the following. "
526 "Please correct the root cause\n"
527 "and remove %s\n"
528 "Automatic cleanup will not be performed "
529 "until the file is removed.\n\n"
530 "%s"),
531 gc_log_path, sb.buf);
532 ret = 1;
534 strbuf_release(&sb);
535 done:
536 free(gc_log_path);
537 return ret;
540 static void gc_before_repack(void)
543 * We may be called twice, as both the pre- and
544 * post-daemonized phases will call us, but running these
545 * commands more than once is pointless and wasteful.
547 static int done = 0;
548 if (done++)
549 return;
551 if (pack_refs && maintenance_task_pack_refs(NULL))
552 die(FAILED_RUN, "pack-refs");
554 if (prune_reflogs) {
555 struct child_process cmd = CHILD_PROCESS_INIT;
557 cmd.git_cmd = 1;
558 strvec_pushv(&cmd.args, reflog.v);
559 if (run_command(&cmd))
560 die(FAILED_RUN, reflog.v[0]);
564 int cmd_gc(int argc, const char **argv, const char *prefix)
566 int aggressive = 0;
567 int auto_gc = 0;
568 int quiet = 0;
569 int force = 0;
570 const char *name;
571 pid_t pid;
572 int daemonized = 0;
573 int keep_largest_pack = -1;
574 timestamp_t dummy;
575 struct child_process rerere_cmd = CHILD_PROCESS_INIT;
577 struct option builtin_gc_options[] = {
578 OPT__QUIET(&quiet, N_("suppress progress reporting")),
579 { OPTION_STRING, 0, "prune", &prune_expire, N_("date"),
580 N_("prune unreferenced objects"),
581 PARSE_OPT_OPTARG, NULL, (intptr_t)prune_expire },
582 OPT_BOOL(0, "cruft", &cruft_packs, N_("pack unreferenced objects separately")),
583 OPT_MAGNITUDE(0, "max-cruft-size", &max_cruft_size,
584 N_("with --cruft, limit the size of new cruft packs")),
585 OPT_BOOL(0, "aggressive", &aggressive, N_("be more thorough (increased runtime)")),
586 OPT_BOOL_F(0, "auto", &auto_gc, N_("enable auto-gc mode"),
587 PARSE_OPT_NOCOMPLETE),
588 OPT_BOOL_F(0, "force", &force,
589 N_("force running gc even if there may be another gc running"),
590 PARSE_OPT_NOCOMPLETE),
591 OPT_BOOL(0, "keep-largest-pack", &keep_largest_pack,
592 N_("repack all other packs except the largest pack")),
593 OPT_END()
596 if (argc == 2 && !strcmp(argv[1], "-h"))
597 usage_with_options(builtin_gc_usage, builtin_gc_options);
599 strvec_pushl(&reflog, "reflog", "expire", "--all", NULL);
600 strvec_pushl(&repack, "repack", "-d", "-l", NULL);
601 strvec_pushl(&prune, "prune", "--expire", NULL);
602 strvec_pushl(&prune_worktrees, "worktree", "prune", "--expire", NULL);
603 strvec_pushl(&rerere, "rerere", "gc", NULL);
605 /* default expiry time, overwritten in gc_config */
606 gc_config();
607 if (parse_expiry_date(gc_log_expire, &gc_log_expire_time))
608 die(_("failed to parse gc.logExpiry value %s"), gc_log_expire);
610 if (pack_refs < 0)
611 pack_refs = !is_bare_repository();
613 argc = parse_options(argc, argv, prefix, builtin_gc_options,
614 builtin_gc_usage, 0);
615 if (argc > 0)
616 usage_with_options(builtin_gc_usage, builtin_gc_options);
618 if (prune_expire && parse_expiry_date(prune_expire, &dummy))
619 die(_("failed to parse prune expiry value %s"), prune_expire);
621 if (aggressive) {
622 strvec_push(&repack, "-f");
623 if (aggressive_depth > 0)
624 strvec_pushf(&repack, "--depth=%d", aggressive_depth);
625 if (aggressive_window > 0)
626 strvec_pushf(&repack, "--window=%d", aggressive_window);
628 if (quiet)
629 strvec_push(&repack, "-q");
631 if (auto_gc) {
633 * Auto-gc should be least intrusive as possible.
635 if (!need_to_gc())
636 return 0;
637 if (!quiet) {
638 if (detach_auto)
639 fprintf(stderr, _("Auto packing the repository in background for optimum performance.\n"));
640 else
641 fprintf(stderr, _("Auto packing the repository for optimum performance.\n"));
642 fprintf(stderr, _("See \"git help gc\" for manual housekeeping.\n"));
644 if (detach_auto) {
645 int ret = report_last_gc_error();
647 if (ret == 1)
648 /* Last gc --auto failed. Skip this one. */
649 return 0;
650 else if (ret)
651 /* an I/O error occurred, already reported */
652 return ret;
654 if (lock_repo_for_gc(force, &pid))
655 return 0;
656 gc_before_repack(); /* dies on failure */
657 delete_tempfile(&pidfile);
660 * failure to daemonize is ok, we'll continue
661 * in foreground
663 daemonized = !daemonize();
665 } else {
666 struct string_list keep_pack = STRING_LIST_INIT_NODUP;
668 if (keep_largest_pack != -1) {
669 if (keep_largest_pack)
670 find_base_packs(&keep_pack, 0);
671 } else if (big_pack_threshold) {
672 find_base_packs(&keep_pack, big_pack_threshold);
675 add_repack_all_option(&keep_pack);
676 string_list_clear(&keep_pack, 0);
679 name = lock_repo_for_gc(force, &pid);
680 if (name) {
681 if (auto_gc)
682 return 0; /* be quiet on --auto */
683 die(_("gc is already running on machine '%s' pid %"PRIuMAX" (use --force if not)"),
684 name, (uintmax_t)pid);
687 if (daemonized) {
688 hold_lock_file_for_update(&log_lock,
689 git_path("gc.log"),
690 LOCK_DIE_ON_ERROR);
691 dup2(get_lock_file_fd(&log_lock), 2);
692 sigchain_push_common(process_log_file_on_signal);
693 atexit(process_log_file_at_exit);
696 gc_before_repack();
698 if (!repository_format_precious_objects) {
699 struct child_process repack_cmd = CHILD_PROCESS_INIT;
701 repack_cmd.git_cmd = 1;
702 repack_cmd.close_object_store = 1;
703 strvec_pushv(&repack_cmd.args, repack.v);
704 if (run_command(&repack_cmd))
705 die(FAILED_RUN, repack.v[0]);
707 if (prune_expire) {
708 struct child_process prune_cmd = CHILD_PROCESS_INIT;
710 /* run `git prune` even if using cruft packs */
711 strvec_push(&prune, prune_expire);
712 if (quiet)
713 strvec_push(&prune, "--no-progress");
714 if (repo_has_promisor_remote(the_repository))
715 strvec_push(&prune,
716 "--exclude-promisor-objects");
717 prune_cmd.git_cmd = 1;
718 strvec_pushv(&prune_cmd.args, prune.v);
719 if (run_command(&prune_cmd))
720 die(FAILED_RUN, prune.v[0]);
724 if (prune_worktrees_expire) {
725 struct child_process prune_worktrees_cmd = CHILD_PROCESS_INIT;
727 strvec_push(&prune_worktrees, prune_worktrees_expire);
728 prune_worktrees_cmd.git_cmd = 1;
729 strvec_pushv(&prune_worktrees_cmd.args, prune_worktrees.v);
730 if (run_command(&prune_worktrees_cmd))
731 die(FAILED_RUN, prune_worktrees.v[0]);
734 rerere_cmd.git_cmd = 1;
735 strvec_pushv(&rerere_cmd.args, rerere.v);
736 if (run_command(&rerere_cmd))
737 die(FAILED_RUN, rerere.v[0]);
739 report_garbage = report_pack_garbage;
740 reprepare_packed_git(the_repository);
741 if (pack_garbage.nr > 0) {
742 close_object_store(the_repository->objects);
743 clean_pack_garbage();
746 if (the_repository->settings.gc_write_commit_graph == 1)
747 write_commit_graph_reachable(the_repository->objects->odb,
748 !quiet && !daemonized ? COMMIT_GRAPH_WRITE_PROGRESS : 0,
749 NULL);
751 if (auto_gc && too_many_loose_objects())
752 warning(_("There are too many unreachable loose objects; "
753 "run 'git prune' to remove them."));
755 if (!daemonized)
756 unlink(git_path("gc.log"));
758 return 0;
761 static const char *const builtin_maintenance_run_usage[] = {
762 N_("git maintenance run [--auto] [--[no-]quiet] [--task=<task>] [--schedule]"),
763 NULL
766 enum schedule_priority {
767 SCHEDULE_NONE = 0,
768 SCHEDULE_WEEKLY = 1,
769 SCHEDULE_DAILY = 2,
770 SCHEDULE_HOURLY = 3,
773 static enum schedule_priority parse_schedule(const char *value)
775 if (!value)
776 return SCHEDULE_NONE;
777 if (!strcasecmp(value, "hourly"))
778 return SCHEDULE_HOURLY;
779 if (!strcasecmp(value, "daily"))
780 return SCHEDULE_DAILY;
781 if (!strcasecmp(value, "weekly"))
782 return SCHEDULE_WEEKLY;
783 return SCHEDULE_NONE;
786 static int maintenance_opt_schedule(const struct option *opt, const char *arg,
787 int unset)
789 enum schedule_priority *priority = opt->value;
791 if (unset)
792 die(_("--no-schedule is not allowed"));
794 *priority = parse_schedule(arg);
796 if (!*priority)
797 die(_("unrecognized --schedule argument '%s'"), arg);
799 return 0;
802 struct maintenance_run_opts {
803 int auto_flag;
804 int quiet;
805 enum schedule_priority schedule;
808 /* Remember to update object flag allocation in object.h */
809 #define SEEN (1u<<0)
811 struct cg_auto_data {
812 int num_not_in_graph;
813 int limit;
816 static int dfs_on_ref(const char *refname UNUSED,
817 const struct object_id *oid,
818 int flags UNUSED,
819 void *cb_data)
821 struct cg_auto_data *data = (struct cg_auto_data *)cb_data;
822 int result = 0;
823 struct object_id peeled;
824 struct commit_list *stack = NULL;
825 struct commit *commit;
827 if (!peel_iterated_oid(oid, &peeled))
828 oid = &peeled;
829 if (oid_object_info(the_repository, oid, NULL) != OBJ_COMMIT)
830 return 0;
832 commit = lookup_commit(the_repository, oid);
833 if (!commit)
834 return 0;
835 if (repo_parse_commit(the_repository, commit) ||
836 commit_graph_position(commit) != COMMIT_NOT_FROM_GRAPH)
837 return 0;
839 data->num_not_in_graph++;
841 if (data->num_not_in_graph >= data->limit)
842 return 1;
844 commit_list_append(commit, &stack);
846 while (!result && stack) {
847 struct commit_list *parent;
849 commit = pop_commit(&stack);
851 for (parent = commit->parents; parent; parent = parent->next) {
852 if (repo_parse_commit(the_repository, parent->item) ||
853 commit_graph_position(parent->item) != COMMIT_NOT_FROM_GRAPH ||
854 parent->item->object.flags & SEEN)
855 continue;
857 parent->item->object.flags |= SEEN;
858 data->num_not_in_graph++;
860 if (data->num_not_in_graph >= data->limit) {
861 result = 1;
862 break;
865 commit_list_append(parent->item, &stack);
869 free_commit_list(stack);
870 return result;
873 static int should_write_commit_graph(void)
875 int result;
876 struct cg_auto_data data;
878 data.num_not_in_graph = 0;
879 data.limit = 100;
880 git_config_get_int("maintenance.commit-graph.auto",
881 &data.limit);
883 if (!data.limit)
884 return 0;
885 if (data.limit < 0)
886 return 1;
888 result = for_each_ref(dfs_on_ref, &data);
890 repo_clear_commit_marks(the_repository, SEEN);
892 return result;
895 static int run_write_commit_graph(struct maintenance_run_opts *opts)
897 struct child_process child = CHILD_PROCESS_INIT;
899 child.git_cmd = child.close_object_store = 1;
900 strvec_pushl(&child.args, "commit-graph", "write",
901 "--split", "--reachable", NULL);
903 if (opts->quiet)
904 strvec_push(&child.args, "--no-progress");
906 return !!run_command(&child);
909 static int maintenance_task_commit_graph(struct maintenance_run_opts *opts)
911 prepare_repo_settings(the_repository);
912 if (!the_repository->settings.core_commit_graph)
913 return 0;
915 if (run_write_commit_graph(opts)) {
916 error(_("failed to write commit-graph"));
917 return 1;
920 return 0;
923 static int fetch_remote(struct remote *remote, void *cbdata)
925 struct maintenance_run_opts *opts = cbdata;
926 struct child_process child = CHILD_PROCESS_INIT;
928 if (remote->skip_default_update)
929 return 0;
931 child.git_cmd = 1;
932 strvec_pushl(&child.args, "fetch", remote->name,
933 "--prefetch", "--prune", "--no-tags",
934 "--no-write-fetch-head", "--recurse-submodules=no",
935 NULL);
937 if (opts->quiet)
938 strvec_push(&child.args, "--quiet");
940 return !!run_command(&child);
943 static int maintenance_task_prefetch(struct maintenance_run_opts *opts)
945 if (for_each_remote(fetch_remote, opts)) {
946 error(_("failed to prefetch remotes"));
947 return 1;
950 return 0;
953 static int maintenance_task_gc(struct maintenance_run_opts *opts)
955 struct child_process child = CHILD_PROCESS_INIT;
957 child.git_cmd = child.close_object_store = 1;
958 strvec_push(&child.args, "gc");
960 if (opts->auto_flag)
961 strvec_push(&child.args, "--auto");
962 if (opts->quiet)
963 strvec_push(&child.args, "--quiet");
964 else
965 strvec_push(&child.args, "--no-quiet");
967 return run_command(&child);
970 static int prune_packed(struct maintenance_run_opts *opts)
972 struct child_process child = CHILD_PROCESS_INIT;
974 child.git_cmd = 1;
975 strvec_push(&child.args, "prune-packed");
977 if (opts->quiet)
978 strvec_push(&child.args, "--quiet");
980 return !!run_command(&child);
983 struct write_loose_object_data {
984 FILE *in;
985 int count;
986 int batch_size;
989 static int loose_object_auto_limit = 100;
991 static int loose_object_count(const struct object_id *oid UNUSED,
992 const char *path UNUSED,
993 void *data)
995 int *count = (int*)data;
996 if (++(*count) >= loose_object_auto_limit)
997 return 1;
998 return 0;
1001 static int loose_object_auto_condition(void)
1003 int count = 0;
1005 git_config_get_int("maintenance.loose-objects.auto",
1006 &loose_object_auto_limit);
1008 if (!loose_object_auto_limit)
1009 return 0;
1010 if (loose_object_auto_limit < 0)
1011 return 1;
1013 return for_each_loose_file_in_objdir(the_repository->objects->odb->path,
1014 loose_object_count,
1015 NULL, NULL, &count);
1018 static int bail_on_loose(const struct object_id *oid UNUSED,
1019 const char *path UNUSED,
1020 void *data UNUSED)
1022 return 1;
1025 static int write_loose_object_to_stdin(const struct object_id *oid,
1026 const char *path UNUSED,
1027 void *data)
1029 struct write_loose_object_data *d = (struct write_loose_object_data *)data;
1031 fprintf(d->in, "%s\n", oid_to_hex(oid));
1033 return ++(d->count) > d->batch_size;
1036 static int pack_loose(struct maintenance_run_opts *opts)
1038 struct repository *r = the_repository;
1039 int result = 0;
1040 struct write_loose_object_data data;
1041 struct child_process pack_proc = CHILD_PROCESS_INIT;
1044 * Do not start pack-objects process
1045 * if there are no loose objects.
1047 if (!for_each_loose_file_in_objdir(r->objects->odb->path,
1048 bail_on_loose,
1049 NULL, NULL, NULL))
1050 return 0;
1052 pack_proc.git_cmd = 1;
1054 strvec_push(&pack_proc.args, "pack-objects");
1055 if (opts->quiet)
1056 strvec_push(&pack_proc.args, "--quiet");
1057 strvec_pushf(&pack_proc.args, "%s/pack/loose", r->objects->odb->path);
1059 pack_proc.in = -1;
1061 if (start_command(&pack_proc)) {
1062 error(_("failed to start 'git pack-objects' process"));
1063 return 1;
1066 data.in = xfdopen(pack_proc.in, "w");
1067 data.count = 0;
1068 data.batch_size = 50000;
1070 for_each_loose_file_in_objdir(r->objects->odb->path,
1071 write_loose_object_to_stdin,
1072 NULL,
1073 NULL,
1074 &data);
1076 fclose(data.in);
1078 if (finish_command(&pack_proc)) {
1079 error(_("failed to finish 'git pack-objects' process"));
1080 result = 1;
1083 return result;
1086 static int maintenance_task_loose_objects(struct maintenance_run_opts *opts)
1088 return prune_packed(opts) || pack_loose(opts);
1091 static int incremental_repack_auto_condition(void)
1093 struct packed_git *p;
1094 int incremental_repack_auto_limit = 10;
1095 int count = 0;
1097 prepare_repo_settings(the_repository);
1098 if (!the_repository->settings.core_multi_pack_index)
1099 return 0;
1101 git_config_get_int("maintenance.incremental-repack.auto",
1102 &incremental_repack_auto_limit);
1104 if (!incremental_repack_auto_limit)
1105 return 0;
1106 if (incremental_repack_auto_limit < 0)
1107 return 1;
1109 for (p = get_packed_git(the_repository);
1110 count < incremental_repack_auto_limit && p;
1111 p = p->next) {
1112 if (!p->multi_pack_index)
1113 count++;
1116 return count >= incremental_repack_auto_limit;
1119 static int multi_pack_index_write(struct maintenance_run_opts *opts)
1121 struct child_process child = CHILD_PROCESS_INIT;
1123 child.git_cmd = 1;
1124 strvec_pushl(&child.args, "multi-pack-index", "write", NULL);
1126 if (opts->quiet)
1127 strvec_push(&child.args, "--no-progress");
1129 if (run_command(&child))
1130 return error(_("failed to write multi-pack-index"));
1132 return 0;
1135 static int multi_pack_index_expire(struct maintenance_run_opts *opts)
1137 struct child_process child = CHILD_PROCESS_INIT;
1139 child.git_cmd = child.close_object_store = 1;
1140 strvec_pushl(&child.args, "multi-pack-index", "expire", NULL);
1142 if (opts->quiet)
1143 strvec_push(&child.args, "--no-progress");
1145 if (run_command(&child))
1146 return error(_("'git multi-pack-index expire' failed"));
1148 return 0;
1151 #define TWO_GIGABYTES (INT32_MAX)
1153 static off_t get_auto_pack_size(void)
1156 * The "auto" value is special: we optimize for
1157 * one large pack-file (i.e. from a clone) and
1158 * expect the rest to be small and they can be
1159 * repacked quickly.
1161 * The strategy we select here is to select a
1162 * size that is one more than the second largest
1163 * pack-file. This ensures that we will repack
1164 * at least two packs if there are three or more
1165 * packs.
1167 off_t max_size = 0;
1168 off_t second_largest_size = 0;
1169 off_t result_size;
1170 struct packed_git *p;
1171 struct repository *r = the_repository;
1173 reprepare_packed_git(r);
1174 for (p = get_all_packs(r); p; p = p->next) {
1175 if (p->pack_size > max_size) {
1176 second_largest_size = max_size;
1177 max_size = p->pack_size;
1178 } else if (p->pack_size > second_largest_size)
1179 second_largest_size = p->pack_size;
1182 result_size = second_largest_size + 1;
1184 /* But limit ourselves to a batch size of 2g */
1185 if (result_size > TWO_GIGABYTES)
1186 result_size = TWO_GIGABYTES;
1188 return result_size;
1191 static int multi_pack_index_repack(struct maintenance_run_opts *opts)
1193 struct child_process child = CHILD_PROCESS_INIT;
1195 child.git_cmd = child.close_object_store = 1;
1196 strvec_pushl(&child.args, "multi-pack-index", "repack", NULL);
1198 if (opts->quiet)
1199 strvec_push(&child.args, "--no-progress");
1201 strvec_pushf(&child.args, "--batch-size=%"PRIuMAX,
1202 (uintmax_t)get_auto_pack_size());
1204 if (run_command(&child))
1205 return error(_("'git multi-pack-index repack' failed"));
1207 return 0;
1210 static int maintenance_task_incremental_repack(struct maintenance_run_opts *opts)
1212 prepare_repo_settings(the_repository);
1213 if (!the_repository->settings.core_multi_pack_index) {
1214 warning(_("skipping incremental-repack task because core.multiPackIndex is disabled"));
1215 return 0;
1218 if (multi_pack_index_write(opts))
1219 return 1;
1220 if (multi_pack_index_expire(opts))
1221 return 1;
1222 if (multi_pack_index_repack(opts))
1223 return 1;
1224 return 0;
1227 typedef int maintenance_task_fn(struct maintenance_run_opts *opts);
1230 * An auto condition function returns 1 if the task should run
1231 * and 0 if the task should NOT run. See needs_to_gc() for an
1232 * example.
1234 typedef int maintenance_auto_fn(void);
1236 struct maintenance_task {
1237 const char *name;
1238 maintenance_task_fn *fn;
1239 maintenance_auto_fn *auto_condition;
1240 unsigned enabled:1;
1242 enum schedule_priority schedule;
1244 /* -1 if not selected. */
1245 int selected_order;
1248 enum maintenance_task_label {
1249 TASK_PREFETCH,
1250 TASK_LOOSE_OBJECTS,
1251 TASK_INCREMENTAL_REPACK,
1252 TASK_GC,
1253 TASK_COMMIT_GRAPH,
1254 TASK_PACK_REFS,
1256 /* Leave as final value */
1257 TASK__COUNT
1260 static struct maintenance_task tasks[] = {
1261 [TASK_PREFETCH] = {
1262 "prefetch",
1263 maintenance_task_prefetch,
1265 [TASK_LOOSE_OBJECTS] = {
1266 "loose-objects",
1267 maintenance_task_loose_objects,
1268 loose_object_auto_condition,
1270 [TASK_INCREMENTAL_REPACK] = {
1271 "incremental-repack",
1272 maintenance_task_incremental_repack,
1273 incremental_repack_auto_condition,
1275 [TASK_GC] = {
1276 "gc",
1277 maintenance_task_gc,
1278 need_to_gc,
1281 [TASK_COMMIT_GRAPH] = {
1282 "commit-graph",
1283 maintenance_task_commit_graph,
1284 should_write_commit_graph,
1286 [TASK_PACK_REFS] = {
1287 "pack-refs",
1288 maintenance_task_pack_refs,
1289 NULL,
1293 static int compare_tasks_by_selection(const void *a_, const void *b_)
1295 const struct maintenance_task *a = a_;
1296 const struct maintenance_task *b = b_;
1298 return b->selected_order - a->selected_order;
1301 static int maintenance_run_tasks(struct maintenance_run_opts *opts)
1303 int i, found_selected = 0;
1304 int result = 0;
1305 struct lock_file lk;
1306 struct repository *r = the_repository;
1307 char *lock_path = xstrfmt("%s/maintenance", r->objects->odb->path);
1309 if (hold_lock_file_for_update(&lk, lock_path, LOCK_NO_DEREF) < 0) {
1311 * Another maintenance command is running.
1313 * If --auto was provided, then it is likely due to a
1314 * recursive process stack. Do not report an error in
1315 * that case.
1317 if (!opts->auto_flag && !opts->quiet)
1318 warning(_("lock file '%s' exists, skipping maintenance"),
1319 lock_path);
1320 free(lock_path);
1321 return 0;
1323 free(lock_path);
1325 for (i = 0; !found_selected && i < TASK__COUNT; i++)
1326 found_selected = tasks[i].selected_order >= 0;
1328 if (found_selected)
1329 QSORT(tasks, TASK__COUNT, compare_tasks_by_selection);
1331 for (i = 0; i < TASK__COUNT; i++) {
1332 if (found_selected && tasks[i].selected_order < 0)
1333 continue;
1335 if (!found_selected && !tasks[i].enabled)
1336 continue;
1338 if (opts->auto_flag &&
1339 (!tasks[i].auto_condition ||
1340 !tasks[i].auto_condition()))
1341 continue;
1343 if (opts->schedule && tasks[i].schedule < opts->schedule)
1344 continue;
1346 trace2_region_enter("maintenance", tasks[i].name, r);
1347 if (tasks[i].fn(opts)) {
1348 error(_("task '%s' failed"), tasks[i].name);
1349 result = 1;
1351 trace2_region_leave("maintenance", tasks[i].name, r);
1354 rollback_lock_file(&lk);
1355 return result;
1358 static void initialize_maintenance_strategy(void)
1360 char *config_str;
1362 if (git_config_get_string("maintenance.strategy", &config_str))
1363 return;
1365 if (!strcasecmp(config_str, "incremental")) {
1366 tasks[TASK_GC].schedule = SCHEDULE_NONE;
1367 tasks[TASK_COMMIT_GRAPH].enabled = 1;
1368 tasks[TASK_COMMIT_GRAPH].schedule = SCHEDULE_HOURLY;
1369 tasks[TASK_PREFETCH].enabled = 1;
1370 tasks[TASK_PREFETCH].schedule = SCHEDULE_HOURLY;
1371 tasks[TASK_INCREMENTAL_REPACK].enabled = 1;
1372 tasks[TASK_INCREMENTAL_REPACK].schedule = SCHEDULE_DAILY;
1373 tasks[TASK_LOOSE_OBJECTS].enabled = 1;
1374 tasks[TASK_LOOSE_OBJECTS].schedule = SCHEDULE_DAILY;
1375 tasks[TASK_PACK_REFS].enabled = 1;
1376 tasks[TASK_PACK_REFS].schedule = SCHEDULE_WEEKLY;
1380 static void initialize_task_config(int schedule)
1382 int i;
1383 struct strbuf config_name = STRBUF_INIT;
1384 gc_config();
1386 if (schedule)
1387 initialize_maintenance_strategy();
1389 for (i = 0; i < TASK__COUNT; i++) {
1390 int config_value;
1391 char *config_str;
1393 strbuf_reset(&config_name);
1394 strbuf_addf(&config_name, "maintenance.%s.enabled",
1395 tasks[i].name);
1397 if (!git_config_get_bool(config_name.buf, &config_value))
1398 tasks[i].enabled = config_value;
1400 strbuf_reset(&config_name);
1401 strbuf_addf(&config_name, "maintenance.%s.schedule",
1402 tasks[i].name);
1404 if (!git_config_get_string(config_name.buf, &config_str)) {
1405 tasks[i].schedule = parse_schedule(config_str);
1406 free(config_str);
1410 strbuf_release(&config_name);
1413 static int task_option_parse(const struct option *opt UNUSED,
1414 const char *arg, int unset)
1416 int i, num_selected = 0;
1417 struct maintenance_task *task = NULL;
1419 BUG_ON_OPT_NEG(unset);
1421 for (i = 0; i < TASK__COUNT; i++) {
1422 if (tasks[i].selected_order >= 0)
1423 num_selected++;
1424 if (!strcasecmp(tasks[i].name, arg)) {
1425 task = &tasks[i];
1429 if (!task) {
1430 error(_("'%s' is not a valid task"), arg);
1431 return 1;
1434 if (task->selected_order >= 0) {
1435 error(_("task '%s' cannot be selected multiple times"), arg);
1436 return 1;
1439 task->selected_order = num_selected + 1;
1441 return 0;
1444 static int maintenance_run(int argc, const char **argv, const char *prefix)
1446 int i;
1447 struct maintenance_run_opts opts;
1448 struct option builtin_maintenance_run_options[] = {
1449 OPT_BOOL(0, "auto", &opts.auto_flag,
1450 N_("run tasks based on the state of the repository")),
1451 OPT_CALLBACK(0, "schedule", &opts.schedule, N_("frequency"),
1452 N_("run tasks based on frequency"),
1453 maintenance_opt_schedule),
1454 OPT_BOOL(0, "quiet", &opts.quiet,
1455 N_("do not report progress or other information over stderr")),
1456 OPT_CALLBACK_F(0, "task", NULL, N_("task"),
1457 N_("run a specific task"),
1458 PARSE_OPT_NONEG, task_option_parse),
1459 OPT_END()
1461 memset(&opts, 0, sizeof(opts));
1463 opts.quiet = !isatty(2);
1465 for (i = 0; i < TASK__COUNT; i++)
1466 tasks[i].selected_order = -1;
1468 argc = parse_options(argc, argv, prefix,
1469 builtin_maintenance_run_options,
1470 builtin_maintenance_run_usage,
1471 PARSE_OPT_STOP_AT_NON_OPTION);
1473 if (opts.auto_flag && opts.schedule)
1474 die(_("use at most one of --auto and --schedule=<frequency>"));
1476 initialize_task_config(opts.schedule);
1478 if (argc != 0)
1479 usage_with_options(builtin_maintenance_run_usage,
1480 builtin_maintenance_run_options);
1481 return maintenance_run_tasks(&opts);
1484 static char *get_maintpath(void)
1486 struct strbuf sb = STRBUF_INIT;
1487 const char *p = the_repository->worktree ?
1488 the_repository->worktree : the_repository->gitdir;
1490 strbuf_realpath(&sb, p, 1);
1491 return strbuf_detach(&sb, NULL);
1494 static char const * const builtin_maintenance_register_usage[] = {
1495 "git maintenance register [--config-file <path>]",
1496 NULL
1499 static int maintenance_register(int argc, const char **argv, const char *prefix)
1501 char *config_file = NULL;
1502 struct option options[] = {
1503 OPT_STRING(0, "config-file", &config_file, N_("file"), N_("use given config file")),
1504 OPT_END(),
1506 int found = 0;
1507 const char *key = "maintenance.repo";
1508 char *maintpath = get_maintpath();
1509 struct string_list_item *item;
1510 const struct string_list *list;
1512 argc = parse_options(argc, argv, prefix, options,
1513 builtin_maintenance_register_usage, 0);
1514 if (argc)
1515 usage_with_options(builtin_maintenance_register_usage,
1516 options);
1518 /* Disable foreground maintenance */
1519 git_config_set("maintenance.auto", "false");
1521 /* Set maintenance strategy, if unset */
1522 if (git_config_get("maintenance.strategy"))
1523 git_config_set("maintenance.strategy", "incremental");
1525 if (!git_config_get_string_multi(key, &list)) {
1526 for_each_string_list_item(item, list) {
1527 if (!strcmp(maintpath, item->string)) {
1528 found = 1;
1529 break;
1534 if (!found) {
1535 int rc;
1536 char *user_config = NULL, *xdg_config = NULL;
1538 if (!config_file) {
1539 git_global_config(&user_config, &xdg_config);
1540 config_file = user_config;
1541 if (!user_config)
1542 die(_("$HOME not set"));
1544 rc = git_config_set_multivar_in_file_gently(
1545 config_file, "maintenance.repo", maintpath,
1546 CONFIG_REGEX_NONE, 0);
1547 free(user_config);
1548 free(xdg_config);
1550 if (rc)
1551 die(_("unable to add '%s' value of '%s'"),
1552 key, maintpath);
1555 free(maintpath);
1556 return 0;
1559 static char const * const builtin_maintenance_unregister_usage[] = {
1560 "git maintenance unregister [--config-file <path>] [--force]",
1561 NULL
1564 static int maintenance_unregister(int argc, const char **argv, const char *prefix)
1566 int force = 0;
1567 char *config_file = NULL;
1568 struct option options[] = {
1569 OPT_STRING(0, "config-file", &config_file, N_("file"), N_("use given config file")),
1570 OPT__FORCE(&force,
1571 N_("return success even if repository was not registered"),
1572 PARSE_OPT_NOCOMPLETE),
1573 OPT_END(),
1575 const char *key = "maintenance.repo";
1576 char *maintpath = get_maintpath();
1577 int found = 0;
1578 struct string_list_item *item;
1579 const struct string_list *list;
1580 struct config_set cs = { { 0 } };
1582 argc = parse_options(argc, argv, prefix, options,
1583 builtin_maintenance_unregister_usage, 0);
1584 if (argc)
1585 usage_with_options(builtin_maintenance_unregister_usage,
1586 options);
1588 if (config_file) {
1589 git_configset_init(&cs);
1590 git_configset_add_file(&cs, config_file);
1592 if (!(config_file
1593 ? git_configset_get_string_multi(&cs, key, &list)
1594 : git_config_get_string_multi(key, &list))) {
1595 for_each_string_list_item(item, list) {
1596 if (!strcmp(maintpath, item->string)) {
1597 found = 1;
1598 break;
1603 if (found) {
1604 int rc;
1605 char *user_config = NULL, *xdg_config = NULL;
1606 if (!config_file) {
1607 git_global_config(&user_config, &xdg_config);
1608 config_file = user_config;
1609 if (!user_config)
1610 die(_("$HOME not set"));
1612 rc = git_config_set_multivar_in_file_gently(
1613 config_file, key, NULL, maintpath,
1614 CONFIG_FLAGS_MULTI_REPLACE | CONFIG_FLAGS_FIXED_VALUE);
1615 free(user_config);
1616 free(xdg_config);
1618 if (rc &&
1619 (!force || rc == CONFIG_NOTHING_SET))
1620 die(_("unable to unset '%s' value of '%s'"),
1621 key, maintpath);
1622 } else if (!force) {
1623 die(_("repository '%s' is not registered"), maintpath);
1626 git_configset_clear(&cs);
1627 free(maintpath);
1628 return 0;
1631 static const char *get_frequency(enum schedule_priority schedule)
1633 switch (schedule) {
1634 case SCHEDULE_HOURLY:
1635 return "hourly";
1636 case SCHEDULE_DAILY:
1637 return "daily";
1638 case SCHEDULE_WEEKLY:
1639 return "weekly";
1640 default:
1641 BUG("invalid schedule %d", schedule);
1646 * get_schedule_cmd` reads the GIT_TEST_MAINT_SCHEDULER environment variable
1647 * to mock the schedulers that `git maintenance start` rely on.
1649 * For test purpose, GIT_TEST_MAINT_SCHEDULER can be set to a comma-separated
1650 * list of colon-separated key/value pairs where each pair contains a scheduler
1651 * and its corresponding mock.
1653 * * If $GIT_TEST_MAINT_SCHEDULER is not set, return false and leave the
1654 * arguments unmodified.
1656 * * If $GIT_TEST_MAINT_SCHEDULER is set, return true.
1657 * In this case, the *cmd value is read as input.
1659 * * if the input value *cmd is the key of one of the comma-separated list
1660 * item, then *is_available is set to true and *cmd is modified and becomes
1661 * the mock command.
1663 * * if the input value *cmd isn’t the key of any of the comma-separated list
1664 * item, then *is_available is set to false.
1666 * Ex.:
1667 * GIT_TEST_MAINT_SCHEDULER not set
1668 * +-------+-------------------------------------------------+
1669 * | Input | Output |
1670 * | *cmd | return code | *cmd | *is_available |
1671 * +-------+-------------+-------------------+---------------+
1672 * | "foo" | false | "foo" (unchanged) | (unchanged) |
1673 * +-------+-------------+-------------------+---------------+
1675 * GIT_TEST_MAINT_SCHEDULER set to “foo:./mock_foo.sh,bar:./mock_bar.sh”
1676 * +-------+-------------------------------------------------+
1677 * | Input | Output |
1678 * | *cmd | return code | *cmd | *is_available |
1679 * +-------+-------------+-------------------+---------------+
1680 * | "foo" | true | "./mock.foo.sh" | true |
1681 * | "qux" | true | "qux" (unchanged) | false |
1682 * +-------+-------------+-------------------+---------------+
1684 static int get_schedule_cmd(const char **cmd, int *is_available)
1686 char *testing = xstrdup_or_null(getenv("GIT_TEST_MAINT_SCHEDULER"));
1687 struct string_list_item *item;
1688 struct string_list list = STRING_LIST_INIT_NODUP;
1690 if (!testing)
1691 return 0;
1693 if (is_available)
1694 *is_available = 0;
1696 string_list_split_in_place(&list, testing, ",", -1);
1697 for_each_string_list_item(item, &list) {
1698 struct string_list pair = STRING_LIST_INIT_NODUP;
1700 if (string_list_split_in_place(&pair, item->string, ":", 2) != 2)
1701 continue;
1703 if (!strcmp(*cmd, pair.items[0].string)) {
1704 *cmd = pair.items[1].string;
1705 if (is_available)
1706 *is_available = 1;
1707 string_list_clear(&list, 0);
1708 UNLEAK(testing);
1709 return 1;
1713 string_list_clear(&list, 0);
1714 free(testing);
1715 return 1;
1718 static int get_random_minute(void)
1720 /* Use a static value when under tests. */
1721 if (getenv("GIT_TEST_MAINT_SCHEDULER"))
1722 return 13;
1724 return git_rand() % 60;
1727 static int is_launchctl_available(void)
1729 const char *cmd = "launchctl";
1730 int is_available;
1731 if (get_schedule_cmd(&cmd, &is_available))
1732 return is_available;
1734 #ifdef __APPLE__
1735 return 1;
1736 #else
1737 return 0;
1738 #endif
1741 static char *launchctl_service_name(const char *frequency)
1743 struct strbuf label = STRBUF_INIT;
1744 strbuf_addf(&label, "org.git-scm.git.%s", frequency);
1745 return strbuf_detach(&label, NULL);
1748 static char *launchctl_service_filename(const char *name)
1750 char *expanded;
1751 struct strbuf filename = STRBUF_INIT;
1752 strbuf_addf(&filename, "~/Library/LaunchAgents/%s.plist", name);
1754 expanded = interpolate_path(filename.buf, 1);
1755 if (!expanded)
1756 die(_("failed to expand path '%s'"), filename.buf);
1758 strbuf_release(&filename);
1759 return expanded;
1762 static char *launchctl_get_uid(void)
1764 return xstrfmt("gui/%d", getuid());
1767 static int launchctl_boot_plist(int enable, const char *filename)
1769 const char *cmd = "launchctl";
1770 int result;
1771 struct child_process child = CHILD_PROCESS_INIT;
1772 char *uid = launchctl_get_uid();
1774 get_schedule_cmd(&cmd, NULL);
1775 strvec_split(&child.args, cmd);
1776 strvec_pushl(&child.args, enable ? "bootstrap" : "bootout", uid,
1777 filename, NULL);
1779 child.no_stderr = 1;
1780 child.no_stdout = 1;
1782 if (start_command(&child))
1783 die(_("failed to start launchctl"));
1785 result = finish_command(&child);
1787 free(uid);
1788 return result;
1791 static int launchctl_remove_plist(enum schedule_priority schedule)
1793 const char *frequency = get_frequency(schedule);
1794 char *name = launchctl_service_name(frequency);
1795 char *filename = launchctl_service_filename(name);
1796 int result = launchctl_boot_plist(0, filename);
1797 unlink(filename);
1798 free(filename);
1799 free(name);
1800 return result;
1803 static int launchctl_remove_plists(void)
1805 return launchctl_remove_plist(SCHEDULE_HOURLY) ||
1806 launchctl_remove_plist(SCHEDULE_DAILY) ||
1807 launchctl_remove_plist(SCHEDULE_WEEKLY);
1810 static int launchctl_list_contains_plist(const char *name, const char *cmd)
1812 struct child_process child = CHILD_PROCESS_INIT;
1814 strvec_split(&child.args, cmd);
1815 strvec_pushl(&child.args, "list", name, NULL);
1817 child.no_stderr = 1;
1818 child.no_stdout = 1;
1820 if (start_command(&child))
1821 die(_("failed to start launchctl"));
1823 /* Returns failure if 'name' doesn't exist. */
1824 return !finish_command(&child);
1827 static int launchctl_schedule_plist(const char *exec_path, enum schedule_priority schedule)
1829 int i, fd;
1830 const char *preamble, *repeat;
1831 const char *frequency = get_frequency(schedule);
1832 char *name = launchctl_service_name(frequency);
1833 char *filename = launchctl_service_filename(name);
1834 struct lock_file lk = LOCK_INIT;
1835 static unsigned long lock_file_timeout_ms = ULONG_MAX;
1836 struct strbuf plist = STRBUF_INIT, plist2 = STRBUF_INIT;
1837 struct stat st;
1838 const char *cmd = "launchctl";
1839 int minute = get_random_minute();
1841 get_schedule_cmd(&cmd, NULL);
1842 preamble = "<?xml version=\"1.0\"?>\n"
1843 "<!DOCTYPE plist PUBLIC \"-//Apple//DTD PLIST 1.0//EN\" \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">\n"
1844 "<plist version=\"1.0\">"
1845 "<dict>\n"
1846 "<key>Label</key><string>%s</string>\n"
1847 "<key>ProgramArguments</key>\n"
1848 "<array>\n"
1849 "<string>%s/git</string>\n"
1850 "<string>--exec-path=%s</string>\n"
1851 "<string>for-each-repo</string>\n"
1852 "<string>--config=maintenance.repo</string>\n"
1853 "<string>maintenance</string>\n"
1854 "<string>run</string>\n"
1855 "<string>--schedule=%s</string>\n"
1856 "</array>\n"
1857 "<key>StartCalendarInterval</key>\n"
1858 "<array>\n";
1859 strbuf_addf(&plist, preamble, name, exec_path, exec_path, frequency);
1861 switch (schedule) {
1862 case SCHEDULE_HOURLY:
1863 repeat = "<dict>\n"
1864 "<key>Hour</key><integer>%d</integer>\n"
1865 "<key>Minute</key><integer>%d</integer>\n"
1866 "</dict>\n";
1867 for (i = 1; i <= 23; i++)
1868 strbuf_addf(&plist, repeat, i, minute);
1869 break;
1871 case SCHEDULE_DAILY:
1872 repeat = "<dict>\n"
1873 "<key>Day</key><integer>%d</integer>\n"
1874 "<key>Hour</key><integer>0</integer>\n"
1875 "<key>Minute</key><integer>%d</integer>\n"
1876 "</dict>\n";
1877 for (i = 1; i <= 6; i++)
1878 strbuf_addf(&plist, repeat, i, minute);
1879 break;
1881 case SCHEDULE_WEEKLY:
1882 strbuf_addf(&plist,
1883 "<dict>\n"
1884 "<key>Day</key><integer>0</integer>\n"
1885 "<key>Hour</key><integer>0</integer>\n"
1886 "<key>Minute</key><integer>%d</integer>\n"
1887 "</dict>\n",
1888 minute);
1889 break;
1891 default:
1892 /* unreachable */
1893 break;
1895 strbuf_addstr(&plist, "</array>\n</dict>\n</plist>\n");
1897 if (safe_create_leading_directories(filename))
1898 die(_("failed to create directories for '%s'"), filename);
1900 if ((long)lock_file_timeout_ms < 0 &&
1901 git_config_get_ulong("gc.launchctlplistlocktimeoutms",
1902 &lock_file_timeout_ms))
1903 lock_file_timeout_ms = 150;
1905 fd = hold_lock_file_for_update_timeout(&lk, filename, LOCK_DIE_ON_ERROR,
1906 lock_file_timeout_ms);
1909 * Does this file already exist? With the intended contents? Is it
1910 * registered already? Then it does not need to be re-registered.
1912 if (!stat(filename, &st) && st.st_size == plist.len &&
1913 strbuf_read_file(&plist2, filename, plist.len) == plist.len &&
1914 !strbuf_cmp(&plist, &plist2) &&
1915 launchctl_list_contains_plist(name, cmd))
1916 rollback_lock_file(&lk);
1917 else {
1918 if (write_in_full(fd, plist.buf, plist.len) < 0 ||
1919 commit_lock_file(&lk))
1920 die_errno(_("could not write '%s'"), filename);
1922 /* bootout might fail if not already running, so ignore */
1923 launchctl_boot_plist(0, filename);
1924 if (launchctl_boot_plist(1, filename))
1925 die(_("failed to bootstrap service %s"), filename);
1928 free(filename);
1929 free(name);
1930 strbuf_release(&plist);
1931 strbuf_release(&plist2);
1932 return 0;
1935 static int launchctl_add_plists(void)
1937 const char *exec_path = git_exec_path();
1939 return launchctl_schedule_plist(exec_path, SCHEDULE_HOURLY) ||
1940 launchctl_schedule_plist(exec_path, SCHEDULE_DAILY) ||
1941 launchctl_schedule_plist(exec_path, SCHEDULE_WEEKLY);
1944 static int launchctl_update_schedule(int run_maintenance, int fd UNUSED)
1946 if (run_maintenance)
1947 return launchctl_add_plists();
1948 else
1949 return launchctl_remove_plists();
1952 static int is_schtasks_available(void)
1954 const char *cmd = "schtasks";
1955 int is_available;
1956 if (get_schedule_cmd(&cmd, &is_available))
1957 return is_available;
1959 #ifdef GIT_WINDOWS_NATIVE
1960 return 1;
1961 #else
1962 return 0;
1963 #endif
1966 static char *schtasks_task_name(const char *frequency)
1968 struct strbuf label = STRBUF_INIT;
1969 strbuf_addf(&label, "Git Maintenance (%s)", frequency);
1970 return strbuf_detach(&label, NULL);
1973 static int schtasks_remove_task(enum schedule_priority schedule)
1975 const char *cmd = "schtasks";
1976 struct child_process child = CHILD_PROCESS_INIT;
1977 const char *frequency = get_frequency(schedule);
1978 char *name = schtasks_task_name(frequency);
1980 get_schedule_cmd(&cmd, NULL);
1981 strvec_split(&child.args, cmd);
1982 strvec_pushl(&child.args, "/delete", "/tn", name, "/f", NULL);
1983 free(name);
1985 return run_command(&child);
1988 static int schtasks_remove_tasks(void)
1990 return schtasks_remove_task(SCHEDULE_HOURLY) ||
1991 schtasks_remove_task(SCHEDULE_DAILY) ||
1992 schtasks_remove_task(SCHEDULE_WEEKLY);
1995 static int schtasks_schedule_task(const char *exec_path, enum schedule_priority schedule)
1997 const char *cmd = "schtasks";
1998 int result;
1999 struct child_process child = CHILD_PROCESS_INIT;
2000 const char *xml;
2001 struct tempfile *tfile;
2002 const char *frequency = get_frequency(schedule);
2003 char *name = schtasks_task_name(frequency);
2004 struct strbuf tfilename = STRBUF_INIT;
2005 int minute = get_random_minute();
2007 get_schedule_cmd(&cmd, NULL);
2009 strbuf_addf(&tfilename, "%s/schedule_%s_XXXXXX",
2010 get_git_common_dir(), frequency);
2011 tfile = xmks_tempfile(tfilename.buf);
2012 strbuf_release(&tfilename);
2014 if (!fdopen_tempfile(tfile, "w"))
2015 die(_("failed to create temp xml file"));
2017 xml = "<?xml version=\"1.0\" ?>\n"
2018 "<Task version=\"1.4\" xmlns=\"http://schemas.microsoft.com/windows/2004/02/mit/task\">\n"
2019 "<Triggers>\n"
2020 "<CalendarTrigger>\n";
2021 fputs(xml, tfile->fp);
2023 switch (schedule) {
2024 case SCHEDULE_HOURLY:
2025 fprintf(tfile->fp,
2026 "<StartBoundary>2020-01-01T01:%02d:00</StartBoundary>\n"
2027 "<Enabled>true</Enabled>\n"
2028 "<ScheduleByDay>\n"
2029 "<DaysInterval>1</DaysInterval>\n"
2030 "</ScheduleByDay>\n"
2031 "<Repetition>\n"
2032 "<Interval>PT1H</Interval>\n"
2033 "<Duration>PT23H</Duration>\n"
2034 "<StopAtDurationEnd>false</StopAtDurationEnd>\n"
2035 "</Repetition>\n",
2036 minute);
2037 break;
2039 case SCHEDULE_DAILY:
2040 fprintf(tfile->fp,
2041 "<StartBoundary>2020-01-01T00:%02d:00</StartBoundary>\n"
2042 "<Enabled>true</Enabled>\n"
2043 "<ScheduleByWeek>\n"
2044 "<DaysOfWeek>\n"
2045 "<Monday />\n"
2046 "<Tuesday />\n"
2047 "<Wednesday />\n"
2048 "<Thursday />\n"
2049 "<Friday />\n"
2050 "<Saturday />\n"
2051 "</DaysOfWeek>\n"
2052 "<WeeksInterval>1</WeeksInterval>\n"
2053 "</ScheduleByWeek>\n",
2054 minute);
2055 break;
2057 case SCHEDULE_WEEKLY:
2058 fprintf(tfile->fp,
2059 "<StartBoundary>2020-01-01T00:%02d:00</StartBoundary>\n"
2060 "<Enabled>true</Enabled>\n"
2061 "<ScheduleByWeek>\n"
2062 "<DaysOfWeek>\n"
2063 "<Sunday />\n"
2064 "</DaysOfWeek>\n"
2065 "<WeeksInterval>1</WeeksInterval>\n"
2066 "</ScheduleByWeek>\n",
2067 minute);
2068 break;
2070 default:
2071 break;
2074 xml = "</CalendarTrigger>\n"
2075 "</Triggers>\n"
2076 "<Principals>\n"
2077 "<Principal id=\"Author\">\n"
2078 "<LogonType>InteractiveToken</LogonType>\n"
2079 "<RunLevel>LeastPrivilege</RunLevel>\n"
2080 "</Principal>\n"
2081 "</Principals>\n"
2082 "<Settings>\n"
2083 "<MultipleInstancesPolicy>IgnoreNew</MultipleInstancesPolicy>\n"
2084 "<Enabled>true</Enabled>\n"
2085 "<Hidden>true</Hidden>\n"
2086 "<UseUnifiedSchedulingEngine>true</UseUnifiedSchedulingEngine>\n"
2087 "<WakeToRun>false</WakeToRun>\n"
2088 "<ExecutionTimeLimit>PT72H</ExecutionTimeLimit>\n"
2089 "<Priority>7</Priority>\n"
2090 "</Settings>\n"
2091 "<Actions Context=\"Author\">\n"
2092 "<Exec>\n"
2093 "<Command>\"%s\\headless-git.exe\"</Command>\n"
2094 "<Arguments>--exec-path=\"%s\" for-each-repo --config=maintenance.repo maintenance run --schedule=%s</Arguments>\n"
2095 "</Exec>\n"
2096 "</Actions>\n"
2097 "</Task>\n";
2098 fprintf(tfile->fp, xml, exec_path, exec_path, frequency);
2099 strvec_split(&child.args, cmd);
2100 strvec_pushl(&child.args, "/create", "/tn", name, "/f", "/xml",
2101 get_tempfile_path(tfile), NULL);
2102 close_tempfile_gently(tfile);
2104 child.no_stdout = 1;
2105 child.no_stderr = 1;
2107 if (start_command(&child))
2108 die(_("failed to start schtasks"));
2109 result = finish_command(&child);
2111 delete_tempfile(&tfile);
2112 free(name);
2113 return result;
2116 static int schtasks_schedule_tasks(void)
2118 const char *exec_path = git_exec_path();
2120 return schtasks_schedule_task(exec_path, SCHEDULE_HOURLY) ||
2121 schtasks_schedule_task(exec_path, SCHEDULE_DAILY) ||
2122 schtasks_schedule_task(exec_path, SCHEDULE_WEEKLY);
2125 static int schtasks_update_schedule(int run_maintenance, int fd UNUSED)
2127 if (run_maintenance)
2128 return schtasks_schedule_tasks();
2129 else
2130 return schtasks_remove_tasks();
2133 MAYBE_UNUSED
2134 static int check_crontab_process(const char *cmd)
2136 struct child_process child = CHILD_PROCESS_INIT;
2138 strvec_split(&child.args, cmd);
2139 strvec_push(&child.args, "-l");
2140 child.no_stdin = 1;
2141 child.no_stdout = 1;
2142 child.no_stderr = 1;
2143 child.silent_exec_failure = 1;
2145 if (start_command(&child))
2146 return 0;
2147 /* Ignore exit code, as an empty crontab will return error. */
2148 finish_command(&child);
2149 return 1;
2152 static int is_crontab_available(void)
2154 const char *cmd = "crontab";
2155 int is_available;
2157 if (get_schedule_cmd(&cmd, &is_available))
2158 return is_available;
2160 #ifdef __APPLE__
2162 * macOS has cron, but it requires special permissions and will
2163 * create a UI alert when attempting to run this command.
2165 return 0;
2166 #else
2167 return check_crontab_process(cmd);
2168 #endif
2171 #define BEGIN_LINE "# BEGIN GIT MAINTENANCE SCHEDULE"
2172 #define END_LINE "# END GIT MAINTENANCE SCHEDULE"
2174 static int crontab_update_schedule(int run_maintenance, int fd)
2176 const char *cmd = "crontab";
2177 int result = 0;
2178 int in_old_region = 0;
2179 struct child_process crontab_list = CHILD_PROCESS_INIT;
2180 struct child_process crontab_edit = CHILD_PROCESS_INIT;
2181 FILE *cron_list, *cron_in;
2182 struct strbuf line = STRBUF_INIT;
2183 struct tempfile *tmpedit = NULL;
2184 int minute = get_random_minute();
2186 get_schedule_cmd(&cmd, NULL);
2187 strvec_split(&crontab_list.args, cmd);
2188 strvec_push(&crontab_list.args, "-l");
2189 crontab_list.in = -1;
2190 crontab_list.out = dup(fd);
2191 crontab_list.git_cmd = 0;
2193 if (start_command(&crontab_list))
2194 return error(_("failed to run 'crontab -l'; your system might not support 'cron'"));
2196 /* Ignore exit code, as an empty crontab will return error. */
2197 finish_command(&crontab_list);
2199 tmpedit = mks_tempfile_t(".git_cron_edit_tmpXXXXXX");
2200 if (!tmpedit) {
2201 result = error(_("failed to create crontab temporary file"));
2202 goto out;
2204 cron_in = fdopen_tempfile(tmpedit, "w");
2205 if (!cron_in) {
2206 result = error(_("failed to open temporary file"));
2207 goto out;
2211 * Read from the .lock file, filtering out the old
2212 * schedule while appending the new schedule.
2214 cron_list = fdopen(fd, "r");
2215 rewind(cron_list);
2217 while (!strbuf_getline_lf(&line, cron_list)) {
2218 if (!in_old_region && !strcmp(line.buf, BEGIN_LINE))
2219 in_old_region = 1;
2220 else if (in_old_region && !strcmp(line.buf, END_LINE))
2221 in_old_region = 0;
2222 else if (!in_old_region)
2223 fprintf(cron_in, "%s\n", line.buf);
2225 strbuf_release(&line);
2227 if (run_maintenance) {
2228 struct strbuf line_format = STRBUF_INIT;
2229 const char *exec_path = git_exec_path();
2231 fprintf(cron_in, "%s\n", BEGIN_LINE);
2232 fprintf(cron_in,
2233 "# The following schedule was created by Git\n");
2234 fprintf(cron_in, "# Any edits made in this region might be\n");
2235 fprintf(cron_in,
2236 "# replaced in the future by a Git command.\n\n");
2238 strbuf_addf(&line_format,
2239 "%%d %%s * * %%s \"%s/git\" --exec-path=\"%s\" for-each-repo --config=maintenance.repo maintenance run --schedule=%%s\n",
2240 exec_path, exec_path);
2241 fprintf(cron_in, line_format.buf, minute, "1-23", "*", "hourly");
2242 fprintf(cron_in, line_format.buf, minute, "0", "1-6", "daily");
2243 fprintf(cron_in, line_format.buf, minute, "0", "0", "weekly");
2244 strbuf_release(&line_format);
2246 fprintf(cron_in, "\n%s\n", END_LINE);
2249 fflush(cron_in);
2251 strvec_split(&crontab_edit.args, cmd);
2252 strvec_push(&crontab_edit.args, get_tempfile_path(tmpedit));
2253 crontab_edit.git_cmd = 0;
2255 if (start_command(&crontab_edit)) {
2256 result = error(_("failed to run 'crontab'; your system might not support 'cron'"));
2257 goto out;
2260 if (finish_command(&crontab_edit))
2261 result = error(_("'crontab' died"));
2262 else
2263 fclose(cron_list);
2264 out:
2265 delete_tempfile(&tmpedit);
2266 return result;
2269 static int real_is_systemd_timer_available(void)
2271 struct child_process child = CHILD_PROCESS_INIT;
2273 strvec_pushl(&child.args, "systemctl", "--user", "list-timers", NULL);
2274 child.no_stdin = 1;
2275 child.no_stdout = 1;
2276 child.no_stderr = 1;
2277 child.silent_exec_failure = 1;
2279 if (start_command(&child))
2280 return 0;
2281 if (finish_command(&child))
2282 return 0;
2283 return 1;
2286 static int is_systemd_timer_available(void)
2288 const char *cmd = "systemctl";
2289 int is_available;
2291 if (get_schedule_cmd(&cmd, &is_available))
2292 return is_available;
2294 return real_is_systemd_timer_available();
2297 static char *xdg_config_home_systemd(const char *filename)
2299 return xdg_config_home_for("systemd/user", filename);
2302 #define SYSTEMD_UNIT_FORMAT "git-maintenance@%s.%s"
2304 static int systemd_timer_delete_timer_file(enum schedule_priority priority)
2306 int ret = 0;
2307 const char *frequency = get_frequency(priority);
2308 char *local_timer_name = xstrfmt(SYSTEMD_UNIT_FORMAT, frequency, "timer");
2309 char *filename = xdg_config_home_systemd(local_timer_name);
2311 if (unlink(filename) && !is_missing_file_error(errno))
2312 ret = error_errno(_("failed to delete '%s'"), filename);
2314 free(filename);
2315 free(local_timer_name);
2316 return ret;
2319 static int systemd_timer_delete_service_template(void)
2321 int ret = 0;
2322 char *local_service_name = xstrfmt(SYSTEMD_UNIT_FORMAT, "", "service");
2323 char *filename = xdg_config_home_systemd(local_service_name);
2324 if (unlink(filename) && !is_missing_file_error(errno))
2325 ret = error_errno(_("failed to delete '%s'"), filename);
2327 free(filename);
2328 free(local_service_name);
2329 return ret;
2333 * Write the schedule information into a git-maintenance@<schedule>.timer
2334 * file using a custom minute. This timer file cannot use the templating
2335 * system, so we generate a specific file for each.
2337 static int systemd_timer_write_timer_file(enum schedule_priority schedule,
2338 int minute)
2340 int res = -1;
2341 char *filename;
2342 FILE *file;
2343 const char *unit;
2344 char *schedule_pattern = NULL;
2345 const char *frequency = get_frequency(schedule);
2346 char *local_timer_name = xstrfmt(SYSTEMD_UNIT_FORMAT, frequency, "timer");
2348 filename = xdg_config_home_systemd(local_timer_name);
2350 if (safe_create_leading_directories(filename)) {
2351 error(_("failed to create directories for '%s'"), filename);
2352 goto error;
2354 file = fopen_or_warn(filename, "w");
2355 if (!file)
2356 goto error;
2358 switch (schedule) {
2359 case SCHEDULE_HOURLY:
2360 schedule_pattern = xstrfmt("*-*-* 1..23:%02d:00", minute);
2361 break;
2363 case SCHEDULE_DAILY:
2364 schedule_pattern = xstrfmt("Tue..Sun *-*-* 0:%02d:00", minute);
2365 break;
2367 case SCHEDULE_WEEKLY:
2368 schedule_pattern = xstrfmt("Mon 0:%02d:00", minute);
2369 break;
2371 default:
2372 BUG("Unhandled schedule_priority");
2375 unit = "# This file was created and is maintained by Git.\n"
2376 "# Any edits made in this file might be replaced in the future\n"
2377 "# by a Git command.\n"
2378 "\n"
2379 "[Unit]\n"
2380 "Description=Optimize Git repositories data\n"
2381 "\n"
2382 "[Timer]\n"
2383 "OnCalendar=%s\n"
2384 "Persistent=true\n"
2385 "\n"
2386 "[Install]\n"
2387 "WantedBy=timers.target\n";
2388 if (fprintf(file, unit, schedule_pattern) < 0) {
2389 error(_("failed to write to '%s'"), filename);
2390 fclose(file);
2391 goto error;
2393 if (fclose(file) == EOF) {
2394 error_errno(_("failed to flush '%s'"), filename);
2395 goto error;
2398 res = 0;
2400 error:
2401 free(schedule_pattern);
2402 free(local_timer_name);
2403 free(filename);
2404 return res;
2408 * No matter the schedule, we use the same service and can make use of the
2409 * templating system. When installing git-maintenance@<schedule>.timer,
2410 * systemd will notice that git-maintenance@.service exists as a template
2411 * and will use this file and insert the <schedule> into the template at
2412 * the position of "%i".
2414 static int systemd_timer_write_service_template(const char *exec_path)
2416 int res = -1;
2417 char *filename;
2418 FILE *file;
2419 const char *unit;
2420 char *local_service_name = xstrfmt(SYSTEMD_UNIT_FORMAT, "", "service");
2422 filename = xdg_config_home_systemd(local_service_name);
2423 if (safe_create_leading_directories(filename)) {
2424 error(_("failed to create directories for '%s'"), filename);
2425 goto error;
2427 file = fopen_or_warn(filename, "w");
2428 if (!file)
2429 goto error;
2431 unit = "# This file was created and is maintained by Git.\n"
2432 "# Any edits made in this file might be replaced in the future\n"
2433 "# by a Git command.\n"
2434 "\n"
2435 "[Unit]\n"
2436 "Description=Optimize Git repositories data\n"
2437 "\n"
2438 "[Service]\n"
2439 "Type=oneshot\n"
2440 "ExecStart=\"%s/git\" --exec-path=\"%s\" for-each-repo --config=maintenance.repo maintenance run --schedule=%%i\n"
2441 "LockPersonality=yes\n"
2442 "MemoryDenyWriteExecute=yes\n"
2443 "NoNewPrivileges=yes\n"
2444 "RestrictAddressFamilies=AF_UNIX AF_INET AF_INET6 AF_VSOCK\n"
2445 "RestrictNamespaces=yes\n"
2446 "RestrictRealtime=yes\n"
2447 "RestrictSUIDSGID=yes\n"
2448 "SystemCallArchitectures=native\n"
2449 "SystemCallFilter=@system-service\n";
2450 if (fprintf(file, unit, exec_path, exec_path) < 0) {
2451 error(_("failed to write to '%s'"), filename);
2452 fclose(file);
2453 goto error;
2455 if (fclose(file) == EOF) {
2456 error_errno(_("failed to flush '%s'"), filename);
2457 goto error;
2460 res = 0;
2462 error:
2463 free(local_service_name);
2464 free(filename);
2465 return res;
2468 static int systemd_timer_enable_unit(int enable,
2469 enum schedule_priority schedule,
2470 int minute)
2472 const char *cmd = "systemctl";
2473 struct child_process child = CHILD_PROCESS_INIT;
2474 const char *frequency = get_frequency(schedule);
2477 * Disabling the systemd unit while it is already disabled makes
2478 * systemctl print an error.
2479 * Let's ignore it since it means we already are in the expected state:
2480 * the unit is disabled.
2482 * On the other hand, enabling a systemd unit which is already enabled
2483 * produces no error.
2485 if (!enable)
2486 child.no_stderr = 1;
2487 else if (systemd_timer_write_timer_file(schedule, minute))
2488 return -1;
2490 get_schedule_cmd(&cmd, NULL);
2491 strvec_split(&child.args, cmd);
2492 strvec_pushl(&child.args, "--user", enable ? "enable" : "disable",
2493 "--now", NULL);
2494 strvec_pushf(&child.args, SYSTEMD_UNIT_FORMAT, frequency, "timer");
2496 if (start_command(&child))
2497 return error(_("failed to start systemctl"));
2498 if (finish_command(&child))
2500 * Disabling an already disabled systemd unit makes
2501 * systemctl fail.
2502 * Let's ignore this failure.
2504 * Enabling an enabled systemd unit doesn't fail.
2506 if (enable)
2507 return error(_("failed to run systemctl"));
2508 return 0;
2512 * A previous version of Git wrote the timer units as template files.
2513 * Clean these up, if they exist.
2515 static void systemd_timer_delete_stale_timer_templates(void)
2517 char *timer_template_name = xstrfmt(SYSTEMD_UNIT_FORMAT, "", "timer");
2518 char *filename = xdg_config_home_systemd(timer_template_name);
2520 if (unlink(filename) && !is_missing_file_error(errno))
2521 warning(_("failed to delete '%s'"), filename);
2523 free(filename);
2524 free(timer_template_name);
2527 static int systemd_timer_delete_unit_files(void)
2529 systemd_timer_delete_stale_timer_templates();
2531 /* Purposefully not short-circuited to make sure all are called. */
2532 return systemd_timer_delete_timer_file(SCHEDULE_HOURLY) |
2533 systemd_timer_delete_timer_file(SCHEDULE_DAILY) |
2534 systemd_timer_delete_timer_file(SCHEDULE_WEEKLY) |
2535 systemd_timer_delete_service_template();
2538 static int systemd_timer_delete_units(void)
2540 int minute = get_random_minute();
2541 /* Purposefully not short-circuited to make sure all are called. */
2542 return systemd_timer_enable_unit(0, SCHEDULE_HOURLY, minute) |
2543 systemd_timer_enable_unit(0, SCHEDULE_DAILY, minute) |
2544 systemd_timer_enable_unit(0, SCHEDULE_WEEKLY, minute) |
2545 systemd_timer_delete_unit_files();
2548 static int systemd_timer_setup_units(void)
2550 int minute = get_random_minute();
2551 const char *exec_path = git_exec_path();
2553 int ret = systemd_timer_write_service_template(exec_path) ||
2554 systemd_timer_enable_unit(1, SCHEDULE_HOURLY, minute) ||
2555 systemd_timer_enable_unit(1, SCHEDULE_DAILY, minute) ||
2556 systemd_timer_enable_unit(1, SCHEDULE_WEEKLY, minute);
2558 if (ret)
2559 systemd_timer_delete_units();
2560 else
2561 systemd_timer_delete_stale_timer_templates();
2563 return ret;
2566 static int systemd_timer_update_schedule(int run_maintenance, int fd UNUSED)
2568 if (run_maintenance)
2569 return systemd_timer_setup_units();
2570 else
2571 return systemd_timer_delete_units();
2574 enum scheduler {
2575 SCHEDULER_INVALID = -1,
2576 SCHEDULER_AUTO,
2577 SCHEDULER_CRON,
2578 SCHEDULER_SYSTEMD,
2579 SCHEDULER_LAUNCHCTL,
2580 SCHEDULER_SCHTASKS,
2583 static const struct {
2584 const char *name;
2585 int (*is_available)(void);
2586 int (*update_schedule)(int run_maintenance, int fd);
2587 } scheduler_fn[] = {
2588 [SCHEDULER_CRON] = {
2589 .name = "crontab",
2590 .is_available = is_crontab_available,
2591 .update_schedule = crontab_update_schedule,
2593 [SCHEDULER_SYSTEMD] = {
2594 .name = "systemctl",
2595 .is_available = is_systemd_timer_available,
2596 .update_schedule = systemd_timer_update_schedule,
2598 [SCHEDULER_LAUNCHCTL] = {
2599 .name = "launchctl",
2600 .is_available = is_launchctl_available,
2601 .update_schedule = launchctl_update_schedule,
2603 [SCHEDULER_SCHTASKS] = {
2604 .name = "schtasks",
2605 .is_available = is_schtasks_available,
2606 .update_schedule = schtasks_update_schedule,
2610 static enum scheduler parse_scheduler(const char *value)
2612 if (!value)
2613 return SCHEDULER_INVALID;
2614 else if (!strcasecmp(value, "auto"))
2615 return SCHEDULER_AUTO;
2616 else if (!strcasecmp(value, "cron") || !strcasecmp(value, "crontab"))
2617 return SCHEDULER_CRON;
2618 else if (!strcasecmp(value, "systemd") ||
2619 !strcasecmp(value, "systemd-timer"))
2620 return SCHEDULER_SYSTEMD;
2621 else if (!strcasecmp(value, "launchctl"))
2622 return SCHEDULER_LAUNCHCTL;
2623 else if (!strcasecmp(value, "schtasks"))
2624 return SCHEDULER_SCHTASKS;
2625 else
2626 return SCHEDULER_INVALID;
2629 static int maintenance_opt_scheduler(const struct option *opt, const char *arg,
2630 int unset)
2632 enum scheduler *scheduler = opt->value;
2634 BUG_ON_OPT_NEG(unset);
2636 *scheduler = parse_scheduler(arg);
2637 if (*scheduler == SCHEDULER_INVALID)
2638 return error(_("unrecognized --scheduler argument '%s'"), arg);
2639 return 0;
2642 struct maintenance_start_opts {
2643 enum scheduler scheduler;
2646 static enum scheduler resolve_scheduler(enum scheduler scheduler)
2648 if (scheduler != SCHEDULER_AUTO)
2649 return scheduler;
2651 #if defined(__APPLE__)
2652 return SCHEDULER_LAUNCHCTL;
2654 #elif defined(GIT_WINDOWS_NATIVE)
2655 return SCHEDULER_SCHTASKS;
2657 #elif defined(__linux__)
2658 if (is_systemd_timer_available())
2659 return SCHEDULER_SYSTEMD;
2660 else if (is_crontab_available())
2661 return SCHEDULER_CRON;
2662 else
2663 die(_("neither systemd timers nor crontab are available"));
2665 #else
2666 return SCHEDULER_CRON;
2667 #endif
2670 static void validate_scheduler(enum scheduler scheduler)
2672 if (scheduler == SCHEDULER_INVALID)
2673 BUG("invalid scheduler");
2674 if (scheduler == SCHEDULER_AUTO)
2675 BUG("resolve_scheduler should have been called before");
2677 if (!scheduler_fn[scheduler].is_available())
2678 die(_("%s scheduler is not available"),
2679 scheduler_fn[scheduler].name);
2682 static int update_background_schedule(const struct maintenance_start_opts *opts,
2683 int enable)
2685 unsigned int i;
2686 int result = 0;
2687 struct lock_file lk;
2688 char *lock_path = xstrfmt("%s/schedule", the_repository->objects->odb->path);
2690 if (hold_lock_file_for_update(&lk, lock_path, LOCK_NO_DEREF) < 0) {
2691 free(lock_path);
2692 return error(_("another process is scheduling background maintenance"));
2695 for (i = 1; i < ARRAY_SIZE(scheduler_fn); i++) {
2696 if (enable && opts->scheduler == i)
2697 continue;
2698 if (!scheduler_fn[i].is_available())
2699 continue;
2700 scheduler_fn[i].update_schedule(0, get_lock_file_fd(&lk));
2703 if (enable)
2704 result = scheduler_fn[opts->scheduler].update_schedule(
2705 1, get_lock_file_fd(&lk));
2707 rollback_lock_file(&lk);
2709 free(lock_path);
2710 return result;
2713 static const char *const builtin_maintenance_start_usage[] = {
2714 N_("git maintenance start [--scheduler=<scheduler>]"),
2715 NULL
2718 static int maintenance_start(int argc, const char **argv, const char *prefix)
2720 struct maintenance_start_opts opts = { 0 };
2721 struct option options[] = {
2722 OPT_CALLBACK_F(
2723 0, "scheduler", &opts.scheduler, N_("scheduler"),
2724 N_("scheduler to trigger git maintenance run"),
2725 PARSE_OPT_NONEG, maintenance_opt_scheduler),
2726 OPT_END()
2728 const char *register_args[] = { "register", NULL };
2730 argc = parse_options(argc, argv, prefix, options,
2731 builtin_maintenance_start_usage, 0);
2732 if (argc)
2733 usage_with_options(builtin_maintenance_start_usage, options);
2735 opts.scheduler = resolve_scheduler(opts.scheduler);
2736 validate_scheduler(opts.scheduler);
2738 if (update_background_schedule(&opts, 1))
2739 die(_("failed to set up maintenance schedule"));
2741 if (maintenance_register(ARRAY_SIZE(register_args)-1, register_args, NULL))
2742 warning(_("failed to add repo to global config"));
2743 return 0;
2746 static const char *const builtin_maintenance_stop_usage[] = {
2747 "git maintenance stop",
2748 NULL
2751 static int maintenance_stop(int argc, const char **argv, const char *prefix)
2753 struct option options[] = {
2754 OPT_END()
2756 argc = parse_options(argc, argv, prefix, options,
2757 builtin_maintenance_stop_usage, 0);
2758 if (argc)
2759 usage_with_options(builtin_maintenance_stop_usage, options);
2760 return update_background_schedule(NULL, 0);
2763 static const char * const builtin_maintenance_usage[] = {
2764 N_("git maintenance <subcommand> [<options>]"),
2765 NULL,
2768 int cmd_maintenance(int argc, const char **argv, const char *prefix)
2770 parse_opt_subcommand_fn *fn = NULL;
2771 struct option builtin_maintenance_options[] = {
2772 OPT_SUBCOMMAND("run", &fn, maintenance_run),
2773 OPT_SUBCOMMAND("start", &fn, maintenance_start),
2774 OPT_SUBCOMMAND("stop", &fn, maintenance_stop),
2775 OPT_SUBCOMMAND("register", &fn, maintenance_register),
2776 OPT_SUBCOMMAND("unregister", &fn, maintenance_unregister),
2777 OPT_END(),
2780 argc = parse_options(argc, argv, prefix, builtin_maintenance_options,
2781 builtin_maintenance_usage, 0);
2782 return fn(argc, argv, prefix);