maintenance: add 'unregister --force'
[git/debian.git] / builtin / gc.c
blobdc0ba9e3648bae0e840d3ea4aef37fa0fc663768
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 "repository.h"
15 #include "config.h"
16 #include "tempfile.h"
17 #include "lockfile.h"
18 #include "parse-options.h"
19 #include "run-command.h"
20 #include "sigchain.h"
21 #include "strvec.h"
22 #include "commit.h"
23 #include "commit-graph.h"
24 #include "packfile.h"
25 #include "object-store.h"
26 #include "pack.h"
27 #include "pack-objects.h"
28 #include "blob.h"
29 #include "tree.h"
30 #include "promisor-remote.h"
31 #include "refs.h"
32 #include "remote.h"
33 #include "exec-cmd.h"
34 #include "hook.h"
36 #define FAILED_RUN "failed to run %s"
38 static const char * const builtin_gc_usage[] = {
39 N_("git gc [<options>]"),
40 NULL
43 static int pack_refs = 1;
44 static int prune_reflogs = 1;
45 static int cruft_packs = 0;
46 static int aggressive_depth = 50;
47 static int aggressive_window = 250;
48 static int gc_auto_threshold = 6700;
49 static int gc_auto_pack_limit = 50;
50 static int detach_auto = 1;
51 static timestamp_t gc_log_expire_time;
52 static const char *gc_log_expire = "1.day.ago";
53 static const char *prune_expire = "2.weeks.ago";
54 static const char *prune_worktrees_expire = "3.months.ago";
55 static unsigned long big_pack_threshold;
56 static unsigned long max_delta_cache_size = DEFAULT_DELTA_CACHE_SIZE;
58 static struct strvec reflog = STRVEC_INIT;
59 static struct strvec repack = STRVEC_INIT;
60 static struct strvec prune = STRVEC_INIT;
61 static struct strvec prune_worktrees = STRVEC_INIT;
62 static struct strvec rerere = STRVEC_INIT;
64 static struct tempfile *pidfile;
65 static struct lock_file log_lock;
67 static struct string_list pack_garbage = STRING_LIST_INIT_DUP;
69 static void clean_pack_garbage(void)
71 int i;
72 for (i = 0; i < pack_garbage.nr; i++)
73 unlink_or_warn(pack_garbage.items[i].string);
74 string_list_clear(&pack_garbage, 0);
77 static void report_pack_garbage(unsigned seen_bits, const char *path)
79 if (seen_bits == PACKDIR_FILE_IDX)
80 string_list_append(&pack_garbage, path);
83 static void process_log_file(void)
85 struct stat st;
86 if (fstat(get_lock_file_fd(&log_lock), &st)) {
88 * Perhaps there was an i/o error or another
89 * unlikely situation. Try to make a note of
90 * this in gc.log along with any existing
91 * messages.
93 int saved_errno = errno;
94 fprintf(stderr, _("Failed to fstat %s: %s"),
95 get_lock_file_path(&log_lock),
96 strerror(saved_errno));
97 fflush(stderr);
98 commit_lock_file(&log_lock);
99 errno = saved_errno;
100 } else if (st.st_size) {
101 /* There was some error recorded in the lock file */
102 commit_lock_file(&log_lock);
103 } else {
104 /* No error, clean up any old gc.log */
105 unlink(git_path("gc.log"));
106 rollback_lock_file(&log_lock);
110 static void process_log_file_at_exit(void)
112 fflush(stderr);
113 process_log_file();
116 static void process_log_file_on_signal(int signo)
118 process_log_file();
119 sigchain_pop(signo);
120 raise(signo);
123 static int gc_config_is_timestamp_never(const char *var)
125 const char *value;
126 timestamp_t expire;
128 if (!git_config_get_value(var, &value) && value) {
129 if (parse_expiry_date(value, &expire))
130 die(_("failed to parse '%s' value '%s'"), var, value);
131 return expire == 0;
133 return 0;
136 static void gc_config(void)
138 const char *value;
140 if (!git_config_get_value("gc.packrefs", &value)) {
141 if (value && !strcmp(value, "notbare"))
142 pack_refs = -1;
143 else
144 pack_refs = git_config_bool("gc.packrefs", value);
147 if (gc_config_is_timestamp_never("gc.reflogexpire") &&
148 gc_config_is_timestamp_never("gc.reflogexpireunreachable"))
149 prune_reflogs = 0;
151 git_config_get_int("gc.aggressivewindow", &aggressive_window);
152 git_config_get_int("gc.aggressivedepth", &aggressive_depth);
153 git_config_get_int("gc.auto", &gc_auto_threshold);
154 git_config_get_int("gc.autopacklimit", &gc_auto_pack_limit);
155 git_config_get_bool("gc.autodetach", &detach_auto);
156 git_config_get_bool("gc.cruftpacks", &cruft_packs);
157 git_config_get_expiry("gc.pruneexpire", &prune_expire);
158 git_config_get_expiry("gc.worktreepruneexpire", &prune_worktrees_expire);
159 git_config_get_expiry("gc.logexpiry", &gc_log_expire);
161 git_config_get_ulong("gc.bigpackthreshold", &big_pack_threshold);
162 git_config_get_ulong("pack.deltacachesize", &max_delta_cache_size);
164 git_config(git_default_config, NULL);
167 struct maintenance_run_opts;
168 static int maintenance_task_pack_refs(MAYBE_UNUSED struct maintenance_run_opts *opts)
170 struct strvec pack_refs_cmd = STRVEC_INIT;
171 int ret;
173 strvec_pushl(&pack_refs_cmd, "pack-refs", "--all", "--prune", NULL);
175 ret = run_command_v_opt(pack_refs_cmd.v, RUN_GIT_CMD);
177 strvec_clear(&pack_refs_cmd);
179 return ret;
182 static int too_many_loose_objects(void)
185 * Quickly check if a "gc" is needed, by estimating how
186 * many loose objects there are. Because SHA-1 is evenly
187 * distributed, we can check only one and get a reasonable
188 * estimate.
190 DIR *dir;
191 struct dirent *ent;
192 int auto_threshold;
193 int num_loose = 0;
194 int needed = 0;
195 const unsigned hexsz_loose = the_hash_algo->hexsz - 2;
197 dir = opendir(git_path("objects/17"));
198 if (!dir)
199 return 0;
201 auto_threshold = DIV_ROUND_UP(gc_auto_threshold, 256);
202 while ((ent = readdir(dir)) != NULL) {
203 if (strspn(ent->d_name, "0123456789abcdef") != hexsz_loose ||
204 ent->d_name[hexsz_loose] != '\0')
205 continue;
206 if (++num_loose > auto_threshold) {
207 needed = 1;
208 break;
211 closedir(dir);
212 return needed;
215 static struct packed_git *find_base_packs(struct string_list *packs,
216 unsigned long limit)
218 struct packed_git *p, *base = NULL;
220 for (p = get_all_packs(the_repository); p; p = p->next) {
221 if (!p->pack_local)
222 continue;
223 if (limit) {
224 if (p->pack_size >= limit)
225 string_list_append(packs, p->pack_name);
226 } else if (!base || base->pack_size < p->pack_size) {
227 base = p;
231 if (base)
232 string_list_append(packs, base->pack_name);
234 return base;
237 static int too_many_packs(void)
239 struct packed_git *p;
240 int cnt;
242 if (gc_auto_pack_limit <= 0)
243 return 0;
245 for (cnt = 0, p = get_all_packs(the_repository); p; p = p->next) {
246 if (!p->pack_local)
247 continue;
248 if (p->pack_keep)
249 continue;
251 * Perhaps check the size of the pack and count only
252 * very small ones here?
254 cnt++;
256 return gc_auto_pack_limit < cnt;
259 static uint64_t total_ram(void)
261 #if defined(HAVE_SYSINFO)
262 struct sysinfo si;
264 if (!sysinfo(&si))
265 return si.totalram;
266 #elif defined(HAVE_BSD_SYSCTL) && (defined(HW_MEMSIZE) || defined(HW_PHYSMEM))
267 int64_t physical_memory;
268 int mib[2];
269 size_t length;
271 mib[0] = CTL_HW;
272 # if defined(HW_MEMSIZE)
273 mib[1] = HW_MEMSIZE;
274 # else
275 mib[1] = HW_PHYSMEM;
276 # endif
277 length = sizeof(int64_t);
278 if (!sysctl(mib, 2, &physical_memory, &length, NULL, 0))
279 return physical_memory;
280 #elif defined(GIT_WINDOWS_NATIVE)
281 MEMORYSTATUSEX memInfo;
283 memInfo.dwLength = sizeof(MEMORYSTATUSEX);
284 if (GlobalMemoryStatusEx(&memInfo))
285 return memInfo.ullTotalPhys;
286 #endif
287 return 0;
290 static uint64_t estimate_repack_memory(struct packed_git *pack)
292 unsigned long nr_objects = approximate_object_count();
293 size_t os_cache, heap;
295 if (!pack || !nr_objects)
296 return 0;
299 * First we have to scan through at least one pack.
300 * Assume enough room in OS file cache to keep the entire pack
301 * or we may accidentally evict data of other processes from
302 * the cache.
304 os_cache = pack->pack_size + pack->index_size;
305 /* then pack-objects needs lots more for book keeping */
306 heap = sizeof(struct object_entry) * nr_objects;
308 * internal rev-list --all --objects takes up some memory too,
309 * let's say half of it is for blobs
311 heap += sizeof(struct blob) * nr_objects / 2;
313 * and the other half is for trees (commits and tags are
314 * usually insignificant)
316 heap += sizeof(struct tree) * nr_objects / 2;
317 /* and then obj_hash[], underestimated in fact */
318 heap += sizeof(struct object *) * nr_objects;
319 /* revindex is used also */
320 heap += (sizeof(off_t) + sizeof(uint32_t)) * nr_objects;
322 * read_sha1_file() (either at delta calculation phase, or
323 * writing phase) also fills up the delta base cache
325 heap += delta_base_cache_limit;
326 /* and of course pack-objects has its own delta cache */
327 heap += max_delta_cache_size;
329 return os_cache + heap;
332 static int keep_one_pack(struct string_list_item *item, void *data)
334 strvec_pushf(&repack, "--keep-pack=%s", basename(item->string));
335 return 0;
338 static void add_repack_all_option(struct string_list *keep_pack)
340 if (prune_expire && !strcmp(prune_expire, "now"))
341 strvec_push(&repack, "-a");
342 else if (cruft_packs) {
343 strvec_push(&repack, "--cruft");
344 if (prune_expire)
345 strvec_pushf(&repack, "--cruft-expiration=%s", prune_expire);
346 } else {
347 strvec_push(&repack, "-A");
348 if (prune_expire)
349 strvec_pushf(&repack, "--unpack-unreachable=%s", prune_expire);
352 if (keep_pack)
353 for_each_string_list(keep_pack, keep_one_pack, NULL);
356 static void add_repack_incremental_option(void)
358 strvec_push(&repack, "--no-write-bitmap-index");
361 static int need_to_gc(void)
364 * Setting gc.auto to 0 or negative can disable the
365 * automatic gc.
367 if (gc_auto_threshold <= 0)
368 return 0;
371 * If there are too many loose objects, but not too many
372 * packs, we run "repack -d -l". If there are too many packs,
373 * we run "repack -A -d -l". Otherwise we tell the caller
374 * there is no need.
376 if (too_many_packs()) {
377 struct string_list keep_pack = STRING_LIST_INIT_NODUP;
379 if (big_pack_threshold) {
380 find_base_packs(&keep_pack, big_pack_threshold);
381 if (keep_pack.nr >= gc_auto_pack_limit) {
382 big_pack_threshold = 0;
383 string_list_clear(&keep_pack, 0);
384 find_base_packs(&keep_pack, 0);
386 } else {
387 struct packed_git *p = find_base_packs(&keep_pack, 0);
388 uint64_t mem_have, mem_want;
390 mem_have = total_ram();
391 mem_want = estimate_repack_memory(p);
394 * Only allow 1/2 of memory for pack-objects, leave
395 * the rest for the OS and other processes in the
396 * system.
398 if (!mem_have || mem_want < mem_have / 2)
399 string_list_clear(&keep_pack, 0);
402 add_repack_all_option(&keep_pack);
403 string_list_clear(&keep_pack, 0);
404 } else if (too_many_loose_objects())
405 add_repack_incremental_option();
406 else
407 return 0;
409 if (run_hooks("pre-auto-gc"))
410 return 0;
411 return 1;
414 /* return NULL on success, else hostname running the gc */
415 static const char *lock_repo_for_gc(int force, pid_t* ret_pid)
417 struct lock_file lock = LOCK_INIT;
418 char my_host[HOST_NAME_MAX + 1];
419 struct strbuf sb = STRBUF_INIT;
420 struct stat st;
421 uintmax_t pid;
422 FILE *fp;
423 int fd;
424 char *pidfile_path;
426 if (is_tempfile_active(pidfile))
427 /* already locked */
428 return NULL;
430 if (xgethostname(my_host, sizeof(my_host)))
431 xsnprintf(my_host, sizeof(my_host), "unknown");
433 pidfile_path = git_pathdup("gc.pid");
434 fd = hold_lock_file_for_update(&lock, pidfile_path,
435 LOCK_DIE_ON_ERROR);
436 if (!force) {
437 static char locking_host[HOST_NAME_MAX + 1];
438 static char *scan_fmt;
439 int should_exit;
441 if (!scan_fmt)
442 scan_fmt = xstrfmt("%s %%%ds", "%"SCNuMAX, HOST_NAME_MAX);
443 fp = fopen(pidfile_path, "r");
444 memset(locking_host, 0, sizeof(locking_host));
445 should_exit =
446 fp != NULL &&
447 !fstat(fileno(fp), &st) &&
449 * 12 hour limit is very generous as gc should
450 * never take that long. On the other hand we
451 * don't really need a strict limit here,
452 * running gc --auto one day late is not a big
453 * problem. --force can be used in manual gc
454 * after the user verifies that no gc is
455 * running.
457 time(NULL) - st.st_mtime <= 12 * 3600 &&
458 fscanf(fp, scan_fmt, &pid, locking_host) == 2 &&
459 /* be gentle to concurrent "gc" on remote hosts */
460 (strcmp(locking_host, my_host) || !kill(pid, 0) || errno == EPERM);
461 if (fp)
462 fclose(fp);
463 if (should_exit) {
464 if (fd >= 0)
465 rollback_lock_file(&lock);
466 *ret_pid = pid;
467 free(pidfile_path);
468 return locking_host;
472 strbuf_addf(&sb, "%"PRIuMAX" %s",
473 (uintmax_t) getpid(), my_host);
474 write_in_full(fd, sb.buf, sb.len);
475 strbuf_release(&sb);
476 commit_lock_file(&lock);
477 pidfile = register_tempfile(pidfile_path);
478 free(pidfile_path);
479 return NULL;
483 * Returns 0 if there was no previous error and gc can proceed, 1 if
484 * gc should not proceed due to an error in the last run. Prints a
485 * message and returns with a non-[01] status code if an error occurred
486 * while reading gc.log
488 static int report_last_gc_error(void)
490 struct strbuf sb = STRBUF_INIT;
491 int ret = 0;
492 ssize_t len;
493 struct stat st;
494 char *gc_log_path = git_pathdup("gc.log");
496 if (stat(gc_log_path, &st)) {
497 if (errno == ENOENT)
498 goto done;
500 ret = die_message_errno(_("cannot stat '%s'"), gc_log_path);
501 goto done;
504 if (st.st_mtime < gc_log_expire_time)
505 goto done;
507 len = strbuf_read_file(&sb, gc_log_path, 0);
508 if (len < 0)
509 ret = die_message_errno(_("cannot read '%s'"), gc_log_path);
510 else if (len > 0) {
512 * A previous gc failed. Report the error, and don't
513 * bother with an automatic gc run since it is likely
514 * to fail in the same way.
516 warning(_("The last gc run reported the following. "
517 "Please correct the root cause\n"
518 "and remove %s\n"
519 "Automatic cleanup will not be performed "
520 "until the file is removed.\n\n"
521 "%s"),
522 gc_log_path, sb.buf);
523 ret = 1;
525 strbuf_release(&sb);
526 done:
527 free(gc_log_path);
528 return ret;
531 static void gc_before_repack(void)
534 * We may be called twice, as both the pre- and
535 * post-daemonized phases will call us, but running these
536 * commands more than once is pointless and wasteful.
538 static int done = 0;
539 if (done++)
540 return;
542 if (pack_refs && maintenance_task_pack_refs(NULL))
543 die(FAILED_RUN, "pack-refs");
545 if (prune_reflogs && run_command_v_opt(reflog.v, RUN_GIT_CMD))
546 die(FAILED_RUN, reflog.v[0]);
549 int cmd_gc(int argc, const char **argv, const char *prefix)
551 int aggressive = 0;
552 int auto_gc = 0;
553 int quiet = 0;
554 int force = 0;
555 const char *name;
556 pid_t pid;
557 int daemonized = 0;
558 int keep_largest_pack = -1;
559 timestamp_t dummy;
561 struct option builtin_gc_options[] = {
562 OPT__QUIET(&quiet, N_("suppress progress reporting")),
563 { OPTION_STRING, 0, "prune", &prune_expire, N_("date"),
564 N_("prune unreferenced objects"),
565 PARSE_OPT_OPTARG, NULL, (intptr_t)prune_expire },
566 OPT_BOOL(0, "cruft", &cruft_packs, N_("pack unreferenced objects separately")),
567 OPT_BOOL(0, "aggressive", &aggressive, N_("be more thorough (increased runtime)")),
568 OPT_BOOL_F(0, "auto", &auto_gc, N_("enable auto-gc mode"),
569 PARSE_OPT_NOCOMPLETE),
570 OPT_BOOL_F(0, "force", &force,
571 N_("force running gc even if there may be another gc running"),
572 PARSE_OPT_NOCOMPLETE),
573 OPT_BOOL(0, "keep-largest-pack", &keep_largest_pack,
574 N_("repack all other packs except the largest pack")),
575 OPT_END()
578 if (argc == 2 && !strcmp(argv[1], "-h"))
579 usage_with_options(builtin_gc_usage, builtin_gc_options);
581 strvec_pushl(&reflog, "reflog", "expire", "--all", NULL);
582 strvec_pushl(&repack, "repack", "-d", "-l", NULL);
583 strvec_pushl(&prune, "prune", "--expire", NULL);
584 strvec_pushl(&prune_worktrees, "worktree", "prune", "--expire", NULL);
585 strvec_pushl(&rerere, "rerere", "gc", NULL);
587 /* default expiry time, overwritten in gc_config */
588 gc_config();
589 if (parse_expiry_date(gc_log_expire, &gc_log_expire_time))
590 die(_("failed to parse gc.logExpiry value %s"), gc_log_expire);
592 if (pack_refs < 0)
593 pack_refs = !is_bare_repository();
595 argc = parse_options(argc, argv, prefix, builtin_gc_options,
596 builtin_gc_usage, 0);
597 if (argc > 0)
598 usage_with_options(builtin_gc_usage, builtin_gc_options);
600 if (prune_expire && parse_expiry_date(prune_expire, &dummy))
601 die(_("failed to parse prune expiry value %s"), prune_expire);
603 if (aggressive) {
604 strvec_push(&repack, "-f");
605 if (aggressive_depth > 0)
606 strvec_pushf(&repack, "--depth=%d", aggressive_depth);
607 if (aggressive_window > 0)
608 strvec_pushf(&repack, "--window=%d", aggressive_window);
610 if (quiet)
611 strvec_push(&repack, "-q");
613 if (auto_gc) {
615 * Auto-gc should be least intrusive as possible.
617 if (!need_to_gc())
618 return 0;
619 if (!quiet) {
620 if (detach_auto)
621 fprintf(stderr, _("Auto packing the repository in background for optimum performance.\n"));
622 else
623 fprintf(stderr, _("Auto packing the repository for optimum performance.\n"));
624 fprintf(stderr, _("See \"git help gc\" for manual housekeeping.\n"));
626 if (detach_auto) {
627 int ret = report_last_gc_error();
629 if (ret == 1)
630 /* Last gc --auto failed. Skip this one. */
631 return 0;
632 else if (ret)
633 /* an I/O error occurred, already reported */
634 return ret;
636 if (lock_repo_for_gc(force, &pid))
637 return 0;
638 gc_before_repack(); /* dies on failure */
639 delete_tempfile(&pidfile);
642 * failure to daemonize is ok, we'll continue
643 * in foreground
645 daemonized = !daemonize();
647 } else {
648 struct string_list keep_pack = STRING_LIST_INIT_NODUP;
650 if (keep_largest_pack != -1) {
651 if (keep_largest_pack)
652 find_base_packs(&keep_pack, 0);
653 } else if (big_pack_threshold) {
654 find_base_packs(&keep_pack, big_pack_threshold);
657 add_repack_all_option(&keep_pack);
658 string_list_clear(&keep_pack, 0);
661 name = lock_repo_for_gc(force, &pid);
662 if (name) {
663 if (auto_gc)
664 return 0; /* be quiet on --auto */
665 die(_("gc is already running on machine '%s' pid %"PRIuMAX" (use --force if not)"),
666 name, (uintmax_t)pid);
669 if (daemonized) {
670 hold_lock_file_for_update(&log_lock,
671 git_path("gc.log"),
672 LOCK_DIE_ON_ERROR);
673 dup2(get_lock_file_fd(&log_lock), 2);
674 sigchain_push_common(process_log_file_on_signal);
675 atexit(process_log_file_at_exit);
678 gc_before_repack();
680 if (!repository_format_precious_objects) {
681 if (run_command_v_opt(repack.v,
682 RUN_GIT_CMD | RUN_CLOSE_OBJECT_STORE))
683 die(FAILED_RUN, repack.v[0]);
685 if (prune_expire) {
686 /* run `git prune` even if using cruft packs */
687 strvec_push(&prune, prune_expire);
688 if (quiet)
689 strvec_push(&prune, "--no-progress");
690 if (has_promisor_remote())
691 strvec_push(&prune,
692 "--exclude-promisor-objects");
693 if (run_command_v_opt(prune.v, RUN_GIT_CMD))
694 die(FAILED_RUN, prune.v[0]);
698 if (prune_worktrees_expire) {
699 strvec_push(&prune_worktrees, prune_worktrees_expire);
700 if (run_command_v_opt(prune_worktrees.v, RUN_GIT_CMD))
701 die(FAILED_RUN, prune_worktrees.v[0]);
704 if (run_command_v_opt(rerere.v, RUN_GIT_CMD))
705 die(FAILED_RUN, rerere.v[0]);
707 report_garbage = report_pack_garbage;
708 reprepare_packed_git(the_repository);
709 if (pack_garbage.nr > 0) {
710 close_object_store(the_repository->objects);
711 clean_pack_garbage();
714 prepare_repo_settings(the_repository);
715 if (the_repository->settings.gc_write_commit_graph == 1)
716 write_commit_graph_reachable(the_repository->objects->odb,
717 !quiet && !daemonized ? COMMIT_GRAPH_WRITE_PROGRESS : 0,
718 NULL);
720 if (auto_gc && too_many_loose_objects())
721 warning(_("There are too many unreachable loose objects; "
722 "run 'git prune' to remove them."));
724 if (!daemonized)
725 unlink(git_path("gc.log"));
727 return 0;
730 static const char *const builtin_maintenance_run_usage[] = {
731 N_("git maintenance run [--auto] [--[no-]quiet] [--task=<task>] [--schedule]"),
732 NULL
735 enum schedule_priority {
736 SCHEDULE_NONE = 0,
737 SCHEDULE_WEEKLY = 1,
738 SCHEDULE_DAILY = 2,
739 SCHEDULE_HOURLY = 3,
742 static enum schedule_priority parse_schedule(const char *value)
744 if (!value)
745 return SCHEDULE_NONE;
746 if (!strcasecmp(value, "hourly"))
747 return SCHEDULE_HOURLY;
748 if (!strcasecmp(value, "daily"))
749 return SCHEDULE_DAILY;
750 if (!strcasecmp(value, "weekly"))
751 return SCHEDULE_WEEKLY;
752 return SCHEDULE_NONE;
755 static int maintenance_opt_schedule(const struct option *opt, const char *arg,
756 int unset)
758 enum schedule_priority *priority = opt->value;
760 if (unset)
761 die(_("--no-schedule is not allowed"));
763 *priority = parse_schedule(arg);
765 if (!*priority)
766 die(_("unrecognized --schedule argument '%s'"), arg);
768 return 0;
771 struct maintenance_run_opts {
772 int auto_flag;
773 int quiet;
774 enum schedule_priority schedule;
777 /* Remember to update object flag allocation in object.h */
778 #define SEEN (1u<<0)
780 struct cg_auto_data {
781 int num_not_in_graph;
782 int limit;
785 static int dfs_on_ref(const char *refname UNUSED,
786 const struct object_id *oid,
787 int flags UNUSED,
788 void *cb_data)
790 struct cg_auto_data *data = (struct cg_auto_data *)cb_data;
791 int result = 0;
792 struct object_id peeled;
793 struct commit_list *stack = NULL;
794 struct commit *commit;
796 if (!peel_iterated_oid(oid, &peeled))
797 oid = &peeled;
798 if (oid_object_info(the_repository, oid, NULL) != OBJ_COMMIT)
799 return 0;
801 commit = lookup_commit(the_repository, oid);
802 if (!commit)
803 return 0;
804 if (parse_commit(commit) ||
805 commit_graph_position(commit) != COMMIT_NOT_FROM_GRAPH)
806 return 0;
808 data->num_not_in_graph++;
810 if (data->num_not_in_graph >= data->limit)
811 return 1;
813 commit_list_append(commit, &stack);
815 while (!result && stack) {
816 struct commit_list *parent;
818 commit = pop_commit(&stack);
820 for (parent = commit->parents; parent; parent = parent->next) {
821 if (parse_commit(parent->item) ||
822 commit_graph_position(parent->item) != COMMIT_NOT_FROM_GRAPH ||
823 parent->item->object.flags & SEEN)
824 continue;
826 parent->item->object.flags |= SEEN;
827 data->num_not_in_graph++;
829 if (data->num_not_in_graph >= data->limit) {
830 result = 1;
831 break;
834 commit_list_append(parent->item, &stack);
838 free_commit_list(stack);
839 return result;
842 static int should_write_commit_graph(void)
844 int result;
845 struct cg_auto_data data;
847 data.num_not_in_graph = 0;
848 data.limit = 100;
849 git_config_get_int("maintenance.commit-graph.auto",
850 &data.limit);
852 if (!data.limit)
853 return 0;
854 if (data.limit < 0)
855 return 1;
857 result = for_each_ref(dfs_on_ref, &data);
859 repo_clear_commit_marks(the_repository, SEEN);
861 return result;
864 static int run_write_commit_graph(struct maintenance_run_opts *opts)
866 struct child_process child = CHILD_PROCESS_INIT;
868 child.git_cmd = child.close_object_store = 1;
869 strvec_pushl(&child.args, "commit-graph", "write",
870 "--split", "--reachable", NULL);
872 if (opts->quiet)
873 strvec_push(&child.args, "--no-progress");
875 return !!run_command(&child);
878 static int maintenance_task_commit_graph(struct maintenance_run_opts *opts)
880 prepare_repo_settings(the_repository);
881 if (!the_repository->settings.core_commit_graph)
882 return 0;
884 if (run_write_commit_graph(opts)) {
885 error(_("failed to write commit-graph"));
886 return 1;
889 return 0;
892 static int fetch_remote(struct remote *remote, void *cbdata)
894 struct maintenance_run_opts *opts = cbdata;
895 struct child_process child = CHILD_PROCESS_INIT;
897 if (remote->skip_default_update)
898 return 0;
900 child.git_cmd = 1;
901 strvec_pushl(&child.args, "fetch", remote->name,
902 "--prefetch", "--prune", "--no-tags",
903 "--no-write-fetch-head", "--recurse-submodules=no",
904 NULL);
906 if (opts->quiet)
907 strvec_push(&child.args, "--quiet");
909 return !!run_command(&child);
912 static int maintenance_task_prefetch(struct maintenance_run_opts *opts)
914 if (for_each_remote(fetch_remote, opts)) {
915 error(_("failed to prefetch remotes"));
916 return 1;
919 return 0;
922 static int maintenance_task_gc(struct maintenance_run_opts *opts)
924 struct child_process child = CHILD_PROCESS_INIT;
926 child.git_cmd = child.close_object_store = 1;
927 strvec_push(&child.args, "gc");
929 if (opts->auto_flag)
930 strvec_push(&child.args, "--auto");
931 if (opts->quiet)
932 strvec_push(&child.args, "--quiet");
933 else
934 strvec_push(&child.args, "--no-quiet");
936 return run_command(&child);
939 static int prune_packed(struct maintenance_run_opts *opts)
941 struct child_process child = CHILD_PROCESS_INIT;
943 child.git_cmd = 1;
944 strvec_push(&child.args, "prune-packed");
946 if (opts->quiet)
947 strvec_push(&child.args, "--quiet");
949 return !!run_command(&child);
952 struct write_loose_object_data {
953 FILE *in;
954 int count;
955 int batch_size;
958 static int loose_object_auto_limit = 100;
960 static int loose_object_count(const struct object_id *oid,
961 const char *path,
962 void *data)
964 int *count = (int*)data;
965 if (++(*count) >= loose_object_auto_limit)
966 return 1;
967 return 0;
970 static int loose_object_auto_condition(void)
972 int count = 0;
974 git_config_get_int("maintenance.loose-objects.auto",
975 &loose_object_auto_limit);
977 if (!loose_object_auto_limit)
978 return 0;
979 if (loose_object_auto_limit < 0)
980 return 1;
982 return for_each_loose_file_in_objdir(the_repository->objects->odb->path,
983 loose_object_count,
984 NULL, NULL, &count);
987 static int bail_on_loose(const struct object_id *oid,
988 const char *path,
989 void *data)
991 return 1;
994 static int write_loose_object_to_stdin(const struct object_id *oid,
995 const char *path,
996 void *data)
998 struct write_loose_object_data *d = (struct write_loose_object_data *)data;
1000 fprintf(d->in, "%s\n", oid_to_hex(oid));
1002 return ++(d->count) > d->batch_size;
1005 static int pack_loose(struct maintenance_run_opts *opts)
1007 struct repository *r = the_repository;
1008 int result = 0;
1009 struct write_loose_object_data data;
1010 struct child_process pack_proc = CHILD_PROCESS_INIT;
1013 * Do not start pack-objects process
1014 * if there are no loose objects.
1016 if (!for_each_loose_file_in_objdir(r->objects->odb->path,
1017 bail_on_loose,
1018 NULL, NULL, NULL))
1019 return 0;
1021 pack_proc.git_cmd = 1;
1023 strvec_push(&pack_proc.args, "pack-objects");
1024 if (opts->quiet)
1025 strvec_push(&pack_proc.args, "--quiet");
1026 strvec_pushf(&pack_proc.args, "%s/pack/loose", r->objects->odb->path);
1028 pack_proc.in = -1;
1030 if (start_command(&pack_proc)) {
1031 error(_("failed to start 'git pack-objects' process"));
1032 return 1;
1035 data.in = xfdopen(pack_proc.in, "w");
1036 data.count = 0;
1037 data.batch_size = 50000;
1039 for_each_loose_file_in_objdir(r->objects->odb->path,
1040 write_loose_object_to_stdin,
1041 NULL,
1042 NULL,
1043 &data);
1045 fclose(data.in);
1047 if (finish_command(&pack_proc)) {
1048 error(_("failed to finish 'git pack-objects' process"));
1049 result = 1;
1052 return result;
1055 static int maintenance_task_loose_objects(struct maintenance_run_opts *opts)
1057 return prune_packed(opts) || pack_loose(opts);
1060 static int incremental_repack_auto_condition(void)
1062 struct packed_git *p;
1063 int incremental_repack_auto_limit = 10;
1064 int count = 0;
1066 prepare_repo_settings(the_repository);
1067 if (!the_repository->settings.core_multi_pack_index)
1068 return 0;
1070 git_config_get_int("maintenance.incremental-repack.auto",
1071 &incremental_repack_auto_limit);
1073 if (!incremental_repack_auto_limit)
1074 return 0;
1075 if (incremental_repack_auto_limit < 0)
1076 return 1;
1078 for (p = get_packed_git(the_repository);
1079 count < incremental_repack_auto_limit && p;
1080 p = p->next) {
1081 if (!p->multi_pack_index)
1082 count++;
1085 return count >= incremental_repack_auto_limit;
1088 static int multi_pack_index_write(struct maintenance_run_opts *opts)
1090 struct child_process child = CHILD_PROCESS_INIT;
1092 child.git_cmd = 1;
1093 strvec_pushl(&child.args, "multi-pack-index", "write", NULL);
1095 if (opts->quiet)
1096 strvec_push(&child.args, "--no-progress");
1098 if (run_command(&child))
1099 return error(_("failed to write multi-pack-index"));
1101 return 0;
1104 static int multi_pack_index_expire(struct maintenance_run_opts *opts)
1106 struct child_process child = CHILD_PROCESS_INIT;
1108 child.git_cmd = child.close_object_store = 1;
1109 strvec_pushl(&child.args, "multi-pack-index", "expire", NULL);
1111 if (opts->quiet)
1112 strvec_push(&child.args, "--no-progress");
1114 if (run_command(&child))
1115 return error(_("'git multi-pack-index expire' failed"));
1117 return 0;
1120 #define TWO_GIGABYTES (INT32_MAX)
1122 static off_t get_auto_pack_size(void)
1125 * The "auto" value is special: we optimize for
1126 * one large pack-file (i.e. from a clone) and
1127 * expect the rest to be small and they can be
1128 * repacked quickly.
1130 * The strategy we select here is to select a
1131 * size that is one more than the second largest
1132 * pack-file. This ensures that we will repack
1133 * at least two packs if there are three or more
1134 * packs.
1136 off_t max_size = 0;
1137 off_t second_largest_size = 0;
1138 off_t result_size;
1139 struct packed_git *p;
1140 struct repository *r = the_repository;
1142 reprepare_packed_git(r);
1143 for (p = get_all_packs(r); p; p = p->next) {
1144 if (p->pack_size > max_size) {
1145 second_largest_size = max_size;
1146 max_size = p->pack_size;
1147 } else if (p->pack_size > second_largest_size)
1148 second_largest_size = p->pack_size;
1151 result_size = second_largest_size + 1;
1153 /* But limit ourselves to a batch size of 2g */
1154 if (result_size > TWO_GIGABYTES)
1155 result_size = TWO_GIGABYTES;
1157 return result_size;
1160 static int multi_pack_index_repack(struct maintenance_run_opts *opts)
1162 struct child_process child = CHILD_PROCESS_INIT;
1164 child.git_cmd = child.close_object_store = 1;
1165 strvec_pushl(&child.args, "multi-pack-index", "repack", NULL);
1167 if (opts->quiet)
1168 strvec_push(&child.args, "--no-progress");
1170 strvec_pushf(&child.args, "--batch-size=%"PRIuMAX,
1171 (uintmax_t)get_auto_pack_size());
1173 if (run_command(&child))
1174 return error(_("'git multi-pack-index repack' failed"));
1176 return 0;
1179 static int maintenance_task_incremental_repack(struct maintenance_run_opts *opts)
1181 prepare_repo_settings(the_repository);
1182 if (!the_repository->settings.core_multi_pack_index) {
1183 warning(_("skipping incremental-repack task because core.multiPackIndex is disabled"));
1184 return 0;
1187 if (multi_pack_index_write(opts))
1188 return 1;
1189 if (multi_pack_index_expire(opts))
1190 return 1;
1191 if (multi_pack_index_repack(opts))
1192 return 1;
1193 return 0;
1196 typedef int maintenance_task_fn(struct maintenance_run_opts *opts);
1199 * An auto condition function returns 1 if the task should run
1200 * and 0 if the task should NOT run. See needs_to_gc() for an
1201 * example.
1203 typedef int maintenance_auto_fn(void);
1205 struct maintenance_task {
1206 const char *name;
1207 maintenance_task_fn *fn;
1208 maintenance_auto_fn *auto_condition;
1209 unsigned enabled:1;
1211 enum schedule_priority schedule;
1213 /* -1 if not selected. */
1214 int selected_order;
1217 enum maintenance_task_label {
1218 TASK_PREFETCH,
1219 TASK_LOOSE_OBJECTS,
1220 TASK_INCREMENTAL_REPACK,
1221 TASK_GC,
1222 TASK_COMMIT_GRAPH,
1223 TASK_PACK_REFS,
1225 /* Leave as final value */
1226 TASK__COUNT
1229 static struct maintenance_task tasks[] = {
1230 [TASK_PREFETCH] = {
1231 "prefetch",
1232 maintenance_task_prefetch,
1234 [TASK_LOOSE_OBJECTS] = {
1235 "loose-objects",
1236 maintenance_task_loose_objects,
1237 loose_object_auto_condition,
1239 [TASK_INCREMENTAL_REPACK] = {
1240 "incremental-repack",
1241 maintenance_task_incremental_repack,
1242 incremental_repack_auto_condition,
1244 [TASK_GC] = {
1245 "gc",
1246 maintenance_task_gc,
1247 need_to_gc,
1250 [TASK_COMMIT_GRAPH] = {
1251 "commit-graph",
1252 maintenance_task_commit_graph,
1253 should_write_commit_graph,
1255 [TASK_PACK_REFS] = {
1256 "pack-refs",
1257 maintenance_task_pack_refs,
1258 NULL,
1262 static int compare_tasks_by_selection(const void *a_, const void *b_)
1264 const struct maintenance_task *a = a_;
1265 const struct maintenance_task *b = b_;
1267 return b->selected_order - a->selected_order;
1270 static int maintenance_run_tasks(struct maintenance_run_opts *opts)
1272 int i, found_selected = 0;
1273 int result = 0;
1274 struct lock_file lk;
1275 struct repository *r = the_repository;
1276 char *lock_path = xstrfmt("%s/maintenance", r->objects->odb->path);
1278 if (hold_lock_file_for_update(&lk, lock_path, LOCK_NO_DEREF) < 0) {
1280 * Another maintenance command is running.
1282 * If --auto was provided, then it is likely due to a
1283 * recursive process stack. Do not report an error in
1284 * that case.
1286 if (!opts->auto_flag && !opts->quiet)
1287 warning(_("lock file '%s' exists, skipping maintenance"),
1288 lock_path);
1289 free(lock_path);
1290 return 0;
1292 free(lock_path);
1294 for (i = 0; !found_selected && i < TASK__COUNT; i++)
1295 found_selected = tasks[i].selected_order >= 0;
1297 if (found_selected)
1298 QSORT(tasks, TASK__COUNT, compare_tasks_by_selection);
1300 for (i = 0; i < TASK__COUNT; i++) {
1301 if (found_selected && tasks[i].selected_order < 0)
1302 continue;
1304 if (!found_selected && !tasks[i].enabled)
1305 continue;
1307 if (opts->auto_flag &&
1308 (!tasks[i].auto_condition ||
1309 !tasks[i].auto_condition()))
1310 continue;
1312 if (opts->schedule && tasks[i].schedule < opts->schedule)
1313 continue;
1315 trace2_region_enter("maintenance", tasks[i].name, r);
1316 if (tasks[i].fn(opts)) {
1317 error(_("task '%s' failed"), tasks[i].name);
1318 result = 1;
1320 trace2_region_leave("maintenance", tasks[i].name, r);
1323 rollback_lock_file(&lk);
1324 return result;
1327 static void initialize_maintenance_strategy(void)
1329 char *config_str;
1331 if (git_config_get_string("maintenance.strategy", &config_str))
1332 return;
1334 if (!strcasecmp(config_str, "incremental")) {
1335 tasks[TASK_GC].schedule = SCHEDULE_NONE;
1336 tasks[TASK_COMMIT_GRAPH].enabled = 1;
1337 tasks[TASK_COMMIT_GRAPH].schedule = SCHEDULE_HOURLY;
1338 tasks[TASK_PREFETCH].enabled = 1;
1339 tasks[TASK_PREFETCH].schedule = SCHEDULE_HOURLY;
1340 tasks[TASK_INCREMENTAL_REPACK].enabled = 1;
1341 tasks[TASK_INCREMENTAL_REPACK].schedule = SCHEDULE_DAILY;
1342 tasks[TASK_LOOSE_OBJECTS].enabled = 1;
1343 tasks[TASK_LOOSE_OBJECTS].schedule = SCHEDULE_DAILY;
1344 tasks[TASK_PACK_REFS].enabled = 1;
1345 tasks[TASK_PACK_REFS].schedule = SCHEDULE_WEEKLY;
1349 static void initialize_task_config(int schedule)
1351 int i;
1352 struct strbuf config_name = STRBUF_INIT;
1353 gc_config();
1355 if (schedule)
1356 initialize_maintenance_strategy();
1358 for (i = 0; i < TASK__COUNT; i++) {
1359 int config_value;
1360 char *config_str;
1362 strbuf_reset(&config_name);
1363 strbuf_addf(&config_name, "maintenance.%s.enabled",
1364 tasks[i].name);
1366 if (!git_config_get_bool(config_name.buf, &config_value))
1367 tasks[i].enabled = config_value;
1369 strbuf_reset(&config_name);
1370 strbuf_addf(&config_name, "maintenance.%s.schedule",
1371 tasks[i].name);
1373 if (!git_config_get_string(config_name.buf, &config_str)) {
1374 tasks[i].schedule = parse_schedule(config_str);
1375 free(config_str);
1379 strbuf_release(&config_name);
1382 static int task_option_parse(const struct option *opt,
1383 const char *arg, int unset)
1385 int i, num_selected = 0;
1386 struct maintenance_task *task = NULL;
1388 BUG_ON_OPT_NEG(unset);
1390 for (i = 0; i < TASK__COUNT; i++) {
1391 if (tasks[i].selected_order >= 0)
1392 num_selected++;
1393 if (!strcasecmp(tasks[i].name, arg)) {
1394 task = &tasks[i];
1398 if (!task) {
1399 error(_("'%s' is not a valid task"), arg);
1400 return 1;
1403 if (task->selected_order >= 0) {
1404 error(_("task '%s' cannot be selected multiple times"), arg);
1405 return 1;
1408 task->selected_order = num_selected + 1;
1410 return 0;
1413 static int maintenance_run(int argc, const char **argv, const char *prefix)
1415 int i;
1416 struct maintenance_run_opts opts;
1417 struct option builtin_maintenance_run_options[] = {
1418 OPT_BOOL(0, "auto", &opts.auto_flag,
1419 N_("run tasks based on the state of the repository")),
1420 OPT_CALLBACK(0, "schedule", &opts.schedule, N_("frequency"),
1421 N_("run tasks based on frequency"),
1422 maintenance_opt_schedule),
1423 OPT_BOOL(0, "quiet", &opts.quiet,
1424 N_("do not report progress or other information over stderr")),
1425 OPT_CALLBACK_F(0, "task", NULL, N_("task"),
1426 N_("run a specific task"),
1427 PARSE_OPT_NONEG, task_option_parse),
1428 OPT_END()
1430 memset(&opts, 0, sizeof(opts));
1432 opts.quiet = !isatty(2);
1434 for (i = 0; i < TASK__COUNT; i++)
1435 tasks[i].selected_order = -1;
1437 argc = parse_options(argc, argv, prefix,
1438 builtin_maintenance_run_options,
1439 builtin_maintenance_run_usage,
1440 PARSE_OPT_STOP_AT_NON_OPTION);
1442 if (opts.auto_flag && opts.schedule)
1443 die(_("use at most one of --auto and --schedule=<frequency>"));
1445 initialize_task_config(opts.schedule);
1447 if (argc != 0)
1448 usage_with_options(builtin_maintenance_run_usage,
1449 builtin_maintenance_run_options);
1450 return maintenance_run_tasks(&opts);
1453 static char *get_maintpath(void)
1455 struct strbuf sb = STRBUF_INIT;
1456 const char *p = the_repository->worktree ?
1457 the_repository->worktree : the_repository->gitdir;
1459 strbuf_realpath(&sb, p, 1);
1460 return strbuf_detach(&sb, NULL);
1463 static char const * const builtin_maintenance_register_usage[] = {
1464 "git maintenance register",
1465 NULL
1468 static int maintenance_register(int argc, const char **argv, const char *prefix)
1470 struct option options[] = {
1471 OPT_END(),
1473 int rc;
1474 char *config_value;
1475 struct child_process config_set = CHILD_PROCESS_INIT;
1476 struct child_process config_get = CHILD_PROCESS_INIT;
1477 char *maintpath = get_maintpath();
1479 argc = parse_options(argc, argv, prefix, options,
1480 builtin_maintenance_register_usage, 0);
1481 if (argc)
1482 usage_with_options(builtin_maintenance_register_usage,
1483 options);
1485 /* Disable foreground maintenance */
1486 git_config_set("maintenance.auto", "false");
1488 /* Set maintenance strategy, if unset */
1489 if (!git_config_get_string("maintenance.strategy", &config_value))
1490 free(config_value);
1491 else
1492 git_config_set("maintenance.strategy", "incremental");
1494 config_get.git_cmd = 1;
1495 strvec_pushl(&config_get.args, "config", "--global", "--get",
1496 "--fixed-value", "maintenance.repo", maintpath, NULL);
1497 config_get.out = -1;
1499 if (start_command(&config_get)) {
1500 rc = error(_("failed to run 'git config'"));
1501 goto done;
1504 /* We already have this value in our config! */
1505 if (!finish_command(&config_get)) {
1506 rc = 0;
1507 goto done;
1510 config_set.git_cmd = 1;
1511 strvec_pushl(&config_set.args, "config", "--add", "--global", "maintenance.repo",
1512 maintpath, NULL);
1514 rc = run_command(&config_set);
1516 done:
1517 free(maintpath);
1518 return rc;
1521 static char const * const builtin_maintenance_unregister_usage[] = {
1522 "git maintenance unregister [--force]",
1523 NULL
1526 static int maintenance_unregister(int argc, const char **argv, const char *prefix)
1528 int force = 0;
1529 struct option options[] = {
1530 OPT__FORCE(&force,
1531 N_("return success even if repository was not registered"),
1532 PARSE_OPT_NOCOMPLETE),
1533 OPT_END(),
1535 const char *key = "maintenance.repo";
1536 int rc = 0;
1537 struct child_process config_unset = CHILD_PROCESS_INIT;
1538 char *maintpath = get_maintpath();
1539 int found = 0;
1540 struct string_list_item *item;
1541 const struct string_list *list;
1543 argc = parse_options(argc, argv, prefix, options,
1544 builtin_maintenance_unregister_usage, 0);
1545 if (argc)
1546 usage_with_options(builtin_maintenance_unregister_usage,
1547 options);
1549 list = git_config_get_value_multi(key);
1550 if (list) {
1551 for_each_string_list_item(item, list) {
1552 if (!strcmp(maintpath, item->string)) {
1553 found = 1;
1554 break;
1559 if (found) {
1560 config_unset.git_cmd = 1;
1561 strvec_pushl(&config_unset.args, "config", "--global", "--unset",
1562 "--fixed-value", key, maintpath, NULL);
1564 rc = run_command(&config_unset);
1565 } else if (!force) {
1566 die(_("repository '%s' is not registered"), maintpath);
1569 free(maintpath);
1570 return rc;
1573 static const char *get_frequency(enum schedule_priority schedule)
1575 switch (schedule) {
1576 case SCHEDULE_HOURLY:
1577 return "hourly";
1578 case SCHEDULE_DAILY:
1579 return "daily";
1580 case SCHEDULE_WEEKLY:
1581 return "weekly";
1582 default:
1583 BUG("invalid schedule %d", schedule);
1588 * get_schedule_cmd` reads the GIT_TEST_MAINT_SCHEDULER environment variable
1589 * to mock the schedulers that `git maintenance start` rely on.
1591 * For test purpose, GIT_TEST_MAINT_SCHEDULER can be set to a comma-separated
1592 * list of colon-separated key/value pairs where each pair contains a scheduler
1593 * and its corresponding mock.
1595 * * If $GIT_TEST_MAINT_SCHEDULER is not set, return false and leave the
1596 * arguments unmodified.
1598 * * If $GIT_TEST_MAINT_SCHEDULER is set, return true.
1599 * In this case, the *cmd value is read as input.
1601 * * if the input value *cmd is the key of one of the comma-separated list
1602 * item, then *is_available is set to true and *cmd is modified and becomes
1603 * the mock command.
1605 * * if the input value *cmd isn’t the key of any of the comma-separated list
1606 * item, then *is_available is set to false.
1608 * Ex.:
1609 * GIT_TEST_MAINT_SCHEDULER not set
1610 * +-------+-------------------------------------------------+
1611 * | Input | Output |
1612 * | *cmd | return code | *cmd | *is_available |
1613 * +-------+-------------+-------------------+---------------+
1614 * | "foo" | false | "foo" (unchanged) | (unchanged) |
1615 * +-------+-------------+-------------------+---------------+
1617 * GIT_TEST_MAINT_SCHEDULER set to “foo:./mock_foo.sh,bar:./mock_bar.sh”
1618 * +-------+-------------------------------------------------+
1619 * | Input | Output |
1620 * | *cmd | return code | *cmd | *is_available |
1621 * +-------+-------------+-------------------+---------------+
1622 * | "foo" | true | "./mock.foo.sh" | true |
1623 * | "qux" | true | "qux" (unchanged) | false |
1624 * +-------+-------------+-------------------+---------------+
1626 static int get_schedule_cmd(const char **cmd, int *is_available)
1628 char *testing = xstrdup_or_null(getenv("GIT_TEST_MAINT_SCHEDULER"));
1629 struct string_list_item *item;
1630 struct string_list list = STRING_LIST_INIT_NODUP;
1632 if (!testing)
1633 return 0;
1635 if (is_available)
1636 *is_available = 0;
1638 string_list_split_in_place(&list, testing, ',', -1);
1639 for_each_string_list_item(item, &list) {
1640 struct string_list pair = STRING_LIST_INIT_NODUP;
1642 if (string_list_split_in_place(&pair, item->string, ':', 2) != 2)
1643 continue;
1645 if (!strcmp(*cmd, pair.items[0].string)) {
1646 *cmd = pair.items[1].string;
1647 if (is_available)
1648 *is_available = 1;
1649 string_list_clear(&list, 0);
1650 UNLEAK(testing);
1651 return 1;
1655 string_list_clear(&list, 0);
1656 free(testing);
1657 return 1;
1660 static int is_launchctl_available(void)
1662 const char *cmd = "launchctl";
1663 int is_available;
1664 if (get_schedule_cmd(&cmd, &is_available))
1665 return is_available;
1667 #ifdef __APPLE__
1668 return 1;
1669 #else
1670 return 0;
1671 #endif
1674 static char *launchctl_service_name(const char *frequency)
1676 struct strbuf label = STRBUF_INIT;
1677 strbuf_addf(&label, "org.git-scm.git.%s", frequency);
1678 return strbuf_detach(&label, NULL);
1681 static char *launchctl_service_filename(const char *name)
1683 char *expanded;
1684 struct strbuf filename = STRBUF_INIT;
1685 strbuf_addf(&filename, "~/Library/LaunchAgents/%s.plist", name);
1687 expanded = interpolate_path(filename.buf, 1);
1688 if (!expanded)
1689 die(_("failed to expand path '%s'"), filename.buf);
1691 strbuf_release(&filename);
1692 return expanded;
1695 static char *launchctl_get_uid(void)
1697 return xstrfmt("gui/%d", getuid());
1700 static int launchctl_boot_plist(int enable, const char *filename)
1702 const char *cmd = "launchctl";
1703 int result;
1704 struct child_process child = CHILD_PROCESS_INIT;
1705 char *uid = launchctl_get_uid();
1707 get_schedule_cmd(&cmd, NULL);
1708 strvec_split(&child.args, cmd);
1709 strvec_pushl(&child.args, enable ? "bootstrap" : "bootout", uid,
1710 filename, NULL);
1712 child.no_stderr = 1;
1713 child.no_stdout = 1;
1715 if (start_command(&child))
1716 die(_("failed to start launchctl"));
1718 result = finish_command(&child);
1720 free(uid);
1721 return result;
1724 static int launchctl_remove_plist(enum schedule_priority schedule)
1726 const char *frequency = get_frequency(schedule);
1727 char *name = launchctl_service_name(frequency);
1728 char *filename = launchctl_service_filename(name);
1729 int result = launchctl_boot_plist(0, filename);
1730 unlink(filename);
1731 free(filename);
1732 free(name);
1733 return result;
1736 static int launchctl_remove_plists(void)
1738 return launchctl_remove_plist(SCHEDULE_HOURLY) ||
1739 launchctl_remove_plist(SCHEDULE_DAILY) ||
1740 launchctl_remove_plist(SCHEDULE_WEEKLY);
1743 static int launchctl_list_contains_plist(const char *name, const char *cmd)
1745 struct child_process child = CHILD_PROCESS_INIT;
1747 strvec_split(&child.args, cmd);
1748 strvec_pushl(&child.args, "list", name, NULL);
1750 child.no_stderr = 1;
1751 child.no_stdout = 1;
1753 if (start_command(&child))
1754 die(_("failed to start launchctl"));
1756 /* Returns failure if 'name' doesn't exist. */
1757 return !finish_command(&child);
1760 static int launchctl_schedule_plist(const char *exec_path, enum schedule_priority schedule)
1762 int i, fd;
1763 const char *preamble, *repeat;
1764 const char *frequency = get_frequency(schedule);
1765 char *name = launchctl_service_name(frequency);
1766 char *filename = launchctl_service_filename(name);
1767 struct lock_file lk = LOCK_INIT;
1768 static unsigned long lock_file_timeout_ms = ULONG_MAX;
1769 struct strbuf plist = STRBUF_INIT, plist2 = STRBUF_INIT;
1770 struct stat st;
1771 const char *cmd = "launchctl";
1773 get_schedule_cmd(&cmd, NULL);
1774 preamble = "<?xml version=\"1.0\"?>\n"
1775 "<!DOCTYPE plist PUBLIC \"-//Apple//DTD PLIST 1.0//EN\" \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">\n"
1776 "<plist version=\"1.0\">"
1777 "<dict>\n"
1778 "<key>Label</key><string>%s</string>\n"
1779 "<key>ProgramArguments</key>\n"
1780 "<array>\n"
1781 "<string>%s/git</string>\n"
1782 "<string>--exec-path=%s</string>\n"
1783 "<string>for-each-repo</string>\n"
1784 "<string>--config=maintenance.repo</string>\n"
1785 "<string>maintenance</string>\n"
1786 "<string>run</string>\n"
1787 "<string>--schedule=%s</string>\n"
1788 "</array>\n"
1789 "<key>StartCalendarInterval</key>\n"
1790 "<array>\n";
1791 strbuf_addf(&plist, preamble, name, exec_path, exec_path, frequency);
1793 switch (schedule) {
1794 case SCHEDULE_HOURLY:
1795 repeat = "<dict>\n"
1796 "<key>Hour</key><integer>%d</integer>\n"
1797 "<key>Minute</key><integer>0</integer>\n"
1798 "</dict>\n";
1799 for (i = 1; i <= 23; i++)
1800 strbuf_addf(&plist, repeat, i);
1801 break;
1803 case SCHEDULE_DAILY:
1804 repeat = "<dict>\n"
1805 "<key>Day</key><integer>%d</integer>\n"
1806 "<key>Hour</key><integer>0</integer>\n"
1807 "<key>Minute</key><integer>0</integer>\n"
1808 "</dict>\n";
1809 for (i = 1; i <= 6; i++)
1810 strbuf_addf(&plist, repeat, i);
1811 break;
1813 case SCHEDULE_WEEKLY:
1814 strbuf_addstr(&plist,
1815 "<dict>\n"
1816 "<key>Day</key><integer>0</integer>\n"
1817 "<key>Hour</key><integer>0</integer>\n"
1818 "<key>Minute</key><integer>0</integer>\n"
1819 "</dict>\n");
1820 break;
1822 default:
1823 /* unreachable */
1824 break;
1826 strbuf_addstr(&plist, "</array>\n</dict>\n</plist>\n");
1828 if (safe_create_leading_directories(filename))
1829 die(_("failed to create directories for '%s'"), filename);
1831 if ((long)lock_file_timeout_ms < 0 &&
1832 git_config_get_ulong("gc.launchctlplistlocktimeoutms",
1833 &lock_file_timeout_ms))
1834 lock_file_timeout_ms = 150;
1836 fd = hold_lock_file_for_update_timeout(&lk, filename, LOCK_DIE_ON_ERROR,
1837 lock_file_timeout_ms);
1840 * Does this file already exist? With the intended contents? Is it
1841 * registered already? Then it does not need to be re-registered.
1843 if (!stat(filename, &st) && st.st_size == plist.len &&
1844 strbuf_read_file(&plist2, filename, plist.len) == plist.len &&
1845 !strbuf_cmp(&plist, &plist2) &&
1846 launchctl_list_contains_plist(name, cmd))
1847 rollback_lock_file(&lk);
1848 else {
1849 if (write_in_full(fd, plist.buf, plist.len) < 0 ||
1850 commit_lock_file(&lk))
1851 die_errno(_("could not write '%s'"), filename);
1853 /* bootout might fail if not already running, so ignore */
1854 launchctl_boot_plist(0, filename);
1855 if (launchctl_boot_plist(1, filename))
1856 die(_("failed to bootstrap service %s"), filename);
1859 free(filename);
1860 free(name);
1861 strbuf_release(&plist);
1862 strbuf_release(&plist2);
1863 return 0;
1866 static int launchctl_add_plists(void)
1868 const char *exec_path = git_exec_path();
1870 return launchctl_schedule_plist(exec_path, SCHEDULE_HOURLY) ||
1871 launchctl_schedule_plist(exec_path, SCHEDULE_DAILY) ||
1872 launchctl_schedule_plist(exec_path, SCHEDULE_WEEKLY);
1875 static int launchctl_update_schedule(int run_maintenance, int fd)
1877 if (run_maintenance)
1878 return launchctl_add_plists();
1879 else
1880 return launchctl_remove_plists();
1883 static int is_schtasks_available(void)
1885 const char *cmd = "schtasks";
1886 int is_available;
1887 if (get_schedule_cmd(&cmd, &is_available))
1888 return is_available;
1890 #ifdef GIT_WINDOWS_NATIVE
1891 return 1;
1892 #else
1893 return 0;
1894 #endif
1897 static char *schtasks_task_name(const char *frequency)
1899 struct strbuf label = STRBUF_INIT;
1900 strbuf_addf(&label, "Git Maintenance (%s)", frequency);
1901 return strbuf_detach(&label, NULL);
1904 static int schtasks_remove_task(enum schedule_priority schedule)
1906 const char *cmd = "schtasks";
1907 int result;
1908 struct strvec args = STRVEC_INIT;
1909 const char *frequency = get_frequency(schedule);
1910 char *name = schtasks_task_name(frequency);
1912 get_schedule_cmd(&cmd, NULL);
1913 strvec_split(&args, cmd);
1914 strvec_pushl(&args, "/delete", "/tn", name, "/f", NULL);
1916 result = run_command_v_opt(args.v, 0);
1918 strvec_clear(&args);
1919 free(name);
1920 return result;
1923 static int schtasks_remove_tasks(void)
1925 return schtasks_remove_task(SCHEDULE_HOURLY) ||
1926 schtasks_remove_task(SCHEDULE_DAILY) ||
1927 schtasks_remove_task(SCHEDULE_WEEKLY);
1930 static int schtasks_schedule_task(const char *exec_path, enum schedule_priority schedule)
1932 const char *cmd = "schtasks";
1933 int result;
1934 struct child_process child = CHILD_PROCESS_INIT;
1935 const char *xml;
1936 struct tempfile *tfile;
1937 const char *frequency = get_frequency(schedule);
1938 char *name = schtasks_task_name(frequency);
1939 struct strbuf tfilename = STRBUF_INIT;
1941 get_schedule_cmd(&cmd, NULL);
1943 strbuf_addf(&tfilename, "%s/schedule_%s_XXXXXX",
1944 get_git_common_dir(), frequency);
1945 tfile = xmks_tempfile(tfilename.buf);
1946 strbuf_release(&tfilename);
1948 if (!fdopen_tempfile(tfile, "w"))
1949 die(_("failed to create temp xml file"));
1951 xml = "<?xml version=\"1.0\" ?>\n"
1952 "<Task version=\"1.4\" xmlns=\"http://schemas.microsoft.com/windows/2004/02/mit/task\">\n"
1953 "<Triggers>\n"
1954 "<CalendarTrigger>\n";
1955 fputs(xml, tfile->fp);
1957 switch (schedule) {
1958 case SCHEDULE_HOURLY:
1959 fprintf(tfile->fp,
1960 "<StartBoundary>2020-01-01T01:00:00</StartBoundary>\n"
1961 "<Enabled>true</Enabled>\n"
1962 "<ScheduleByDay>\n"
1963 "<DaysInterval>1</DaysInterval>\n"
1964 "</ScheduleByDay>\n"
1965 "<Repetition>\n"
1966 "<Interval>PT1H</Interval>\n"
1967 "<Duration>PT23H</Duration>\n"
1968 "<StopAtDurationEnd>false</StopAtDurationEnd>\n"
1969 "</Repetition>\n");
1970 break;
1972 case SCHEDULE_DAILY:
1973 fprintf(tfile->fp,
1974 "<StartBoundary>2020-01-01T00:00:00</StartBoundary>\n"
1975 "<Enabled>true</Enabled>\n"
1976 "<ScheduleByWeek>\n"
1977 "<DaysOfWeek>\n"
1978 "<Monday />\n"
1979 "<Tuesday />\n"
1980 "<Wednesday />\n"
1981 "<Thursday />\n"
1982 "<Friday />\n"
1983 "<Saturday />\n"
1984 "</DaysOfWeek>\n"
1985 "<WeeksInterval>1</WeeksInterval>\n"
1986 "</ScheduleByWeek>\n");
1987 break;
1989 case SCHEDULE_WEEKLY:
1990 fprintf(tfile->fp,
1991 "<StartBoundary>2020-01-01T00:00:00</StartBoundary>\n"
1992 "<Enabled>true</Enabled>\n"
1993 "<ScheduleByWeek>\n"
1994 "<DaysOfWeek>\n"
1995 "<Sunday />\n"
1996 "</DaysOfWeek>\n"
1997 "<WeeksInterval>1</WeeksInterval>\n"
1998 "</ScheduleByWeek>\n");
1999 break;
2001 default:
2002 break;
2005 xml = "</CalendarTrigger>\n"
2006 "</Triggers>\n"
2007 "<Principals>\n"
2008 "<Principal id=\"Author\">\n"
2009 "<LogonType>InteractiveToken</LogonType>\n"
2010 "<RunLevel>LeastPrivilege</RunLevel>\n"
2011 "</Principal>\n"
2012 "</Principals>\n"
2013 "<Settings>\n"
2014 "<MultipleInstancesPolicy>IgnoreNew</MultipleInstancesPolicy>\n"
2015 "<Enabled>true</Enabled>\n"
2016 "<Hidden>true</Hidden>\n"
2017 "<UseUnifiedSchedulingEngine>true</UseUnifiedSchedulingEngine>\n"
2018 "<WakeToRun>false</WakeToRun>\n"
2019 "<ExecutionTimeLimit>PT72H</ExecutionTimeLimit>\n"
2020 "<Priority>7</Priority>\n"
2021 "</Settings>\n"
2022 "<Actions Context=\"Author\">\n"
2023 "<Exec>\n"
2024 "<Command>\"%s\\git.exe\"</Command>\n"
2025 "<Arguments>--exec-path=\"%s\" for-each-repo --config=maintenance.repo maintenance run --schedule=%s</Arguments>\n"
2026 "</Exec>\n"
2027 "</Actions>\n"
2028 "</Task>\n";
2029 fprintf(tfile->fp, xml, exec_path, exec_path, frequency);
2030 strvec_split(&child.args, cmd);
2031 strvec_pushl(&child.args, "/create", "/tn", name, "/f", "/xml",
2032 get_tempfile_path(tfile), NULL);
2033 close_tempfile_gently(tfile);
2035 child.no_stdout = 1;
2036 child.no_stderr = 1;
2038 if (start_command(&child))
2039 die(_("failed to start schtasks"));
2040 result = finish_command(&child);
2042 delete_tempfile(&tfile);
2043 free(name);
2044 return result;
2047 static int schtasks_schedule_tasks(void)
2049 const char *exec_path = git_exec_path();
2051 return schtasks_schedule_task(exec_path, SCHEDULE_HOURLY) ||
2052 schtasks_schedule_task(exec_path, SCHEDULE_DAILY) ||
2053 schtasks_schedule_task(exec_path, SCHEDULE_WEEKLY);
2056 static int schtasks_update_schedule(int run_maintenance, int fd)
2058 if (run_maintenance)
2059 return schtasks_schedule_tasks();
2060 else
2061 return schtasks_remove_tasks();
2064 MAYBE_UNUSED
2065 static int check_crontab_process(const char *cmd)
2067 struct child_process child = CHILD_PROCESS_INIT;
2069 strvec_split(&child.args, cmd);
2070 strvec_push(&child.args, "-l");
2071 child.no_stdin = 1;
2072 child.no_stdout = 1;
2073 child.no_stderr = 1;
2074 child.silent_exec_failure = 1;
2076 if (start_command(&child))
2077 return 0;
2078 /* Ignore exit code, as an empty crontab will return error. */
2079 finish_command(&child);
2080 return 1;
2083 static int is_crontab_available(void)
2085 const char *cmd = "crontab";
2086 int is_available;
2088 if (get_schedule_cmd(&cmd, &is_available))
2089 return is_available;
2091 #ifdef __APPLE__
2093 * macOS has cron, but it requires special permissions and will
2094 * create a UI alert when attempting to run this command.
2096 return 0;
2097 #else
2098 return check_crontab_process(cmd);
2099 #endif
2102 #define BEGIN_LINE "# BEGIN GIT MAINTENANCE SCHEDULE"
2103 #define END_LINE "# END GIT MAINTENANCE SCHEDULE"
2105 static int crontab_update_schedule(int run_maintenance, int fd)
2107 const char *cmd = "crontab";
2108 int result = 0;
2109 int in_old_region = 0;
2110 struct child_process crontab_list = CHILD_PROCESS_INIT;
2111 struct child_process crontab_edit = CHILD_PROCESS_INIT;
2112 FILE *cron_list, *cron_in;
2113 struct strbuf line = STRBUF_INIT;
2114 struct tempfile *tmpedit = NULL;
2116 get_schedule_cmd(&cmd, NULL);
2117 strvec_split(&crontab_list.args, cmd);
2118 strvec_push(&crontab_list.args, "-l");
2119 crontab_list.in = -1;
2120 crontab_list.out = dup(fd);
2121 crontab_list.git_cmd = 0;
2123 if (start_command(&crontab_list))
2124 return error(_("failed to run 'crontab -l'; your system might not support 'cron'"));
2126 /* Ignore exit code, as an empty crontab will return error. */
2127 finish_command(&crontab_list);
2129 tmpedit = mks_tempfile_t(".git_cron_edit_tmpXXXXXX");
2130 if (!tmpedit) {
2131 result = error(_("failed to create crontab temporary file"));
2132 goto out;
2134 cron_in = fdopen_tempfile(tmpedit, "w");
2135 if (!cron_in) {
2136 result = error(_("failed to open temporary file"));
2137 goto out;
2141 * Read from the .lock file, filtering out the old
2142 * schedule while appending the new schedule.
2144 cron_list = fdopen(fd, "r");
2145 rewind(cron_list);
2147 while (!strbuf_getline_lf(&line, cron_list)) {
2148 if (!in_old_region && !strcmp(line.buf, BEGIN_LINE))
2149 in_old_region = 1;
2150 else if (in_old_region && !strcmp(line.buf, END_LINE))
2151 in_old_region = 0;
2152 else if (!in_old_region)
2153 fprintf(cron_in, "%s\n", line.buf);
2155 strbuf_release(&line);
2157 if (run_maintenance) {
2158 struct strbuf line_format = STRBUF_INIT;
2159 const char *exec_path = git_exec_path();
2161 fprintf(cron_in, "%s\n", BEGIN_LINE);
2162 fprintf(cron_in,
2163 "# The following schedule was created by Git\n");
2164 fprintf(cron_in, "# Any edits made in this region might be\n");
2165 fprintf(cron_in,
2166 "# replaced in the future by a Git command.\n\n");
2168 strbuf_addf(&line_format,
2169 "%%s %%s * * %%s \"%s/git\" --exec-path=\"%s\" for-each-repo --config=maintenance.repo maintenance run --schedule=%%s\n",
2170 exec_path, exec_path);
2171 fprintf(cron_in, line_format.buf, "0", "1-23", "*", "hourly");
2172 fprintf(cron_in, line_format.buf, "0", "0", "1-6", "daily");
2173 fprintf(cron_in, line_format.buf, "0", "0", "0", "weekly");
2174 strbuf_release(&line_format);
2176 fprintf(cron_in, "\n%s\n", END_LINE);
2179 fflush(cron_in);
2181 strvec_split(&crontab_edit.args, cmd);
2182 strvec_push(&crontab_edit.args, get_tempfile_path(tmpedit));
2183 crontab_edit.git_cmd = 0;
2185 if (start_command(&crontab_edit)) {
2186 result = error(_("failed to run 'crontab'; your system might not support 'cron'"));
2187 goto out;
2190 if (finish_command(&crontab_edit))
2191 result = error(_("'crontab' died"));
2192 else
2193 fclose(cron_list);
2194 out:
2195 delete_tempfile(&tmpedit);
2196 return result;
2199 static int real_is_systemd_timer_available(void)
2201 struct child_process child = CHILD_PROCESS_INIT;
2203 strvec_pushl(&child.args, "systemctl", "--user", "list-timers", NULL);
2204 child.no_stdin = 1;
2205 child.no_stdout = 1;
2206 child.no_stderr = 1;
2207 child.silent_exec_failure = 1;
2209 if (start_command(&child))
2210 return 0;
2211 if (finish_command(&child))
2212 return 0;
2213 return 1;
2216 static int is_systemd_timer_available(void)
2218 const char *cmd = "systemctl";
2219 int is_available;
2221 if (get_schedule_cmd(&cmd, &is_available))
2222 return is_available;
2224 return real_is_systemd_timer_available();
2227 static char *xdg_config_home_systemd(const char *filename)
2229 return xdg_config_home_for("systemd/user", filename);
2232 static int systemd_timer_enable_unit(int enable,
2233 enum schedule_priority schedule)
2235 const char *cmd = "systemctl";
2236 struct child_process child = CHILD_PROCESS_INIT;
2237 const char *frequency = get_frequency(schedule);
2240 * Disabling the systemd unit while it is already disabled makes
2241 * systemctl print an error.
2242 * Let's ignore it since it means we already are in the expected state:
2243 * the unit is disabled.
2245 * On the other hand, enabling a systemd unit which is already enabled
2246 * produces no error.
2248 if (!enable)
2249 child.no_stderr = 1;
2251 get_schedule_cmd(&cmd, NULL);
2252 strvec_split(&child.args, cmd);
2253 strvec_pushl(&child.args, "--user", enable ? "enable" : "disable",
2254 "--now", NULL);
2255 strvec_pushf(&child.args, "git-maintenance@%s.timer", frequency);
2257 if (start_command(&child))
2258 return error(_("failed to start systemctl"));
2259 if (finish_command(&child))
2261 * Disabling an already disabled systemd unit makes
2262 * systemctl fail.
2263 * Let's ignore this failure.
2265 * Enabling an enabled systemd unit doesn't fail.
2267 if (enable)
2268 return error(_("failed to run systemctl"));
2269 return 0;
2272 static int systemd_timer_delete_unit_templates(void)
2274 int ret = 0;
2275 char *filename = xdg_config_home_systemd("git-maintenance@.timer");
2276 if (unlink(filename) && !is_missing_file_error(errno))
2277 ret = error_errno(_("failed to delete '%s'"), filename);
2278 FREE_AND_NULL(filename);
2280 filename = xdg_config_home_systemd("git-maintenance@.service");
2281 if (unlink(filename) && !is_missing_file_error(errno))
2282 ret = error_errno(_("failed to delete '%s'"), filename);
2284 free(filename);
2285 return ret;
2288 static int systemd_timer_delete_units(void)
2290 return systemd_timer_enable_unit(0, SCHEDULE_HOURLY) ||
2291 systemd_timer_enable_unit(0, SCHEDULE_DAILY) ||
2292 systemd_timer_enable_unit(0, SCHEDULE_WEEKLY) ||
2293 systemd_timer_delete_unit_templates();
2296 static int systemd_timer_write_unit_templates(const char *exec_path)
2298 char *filename;
2299 FILE *file;
2300 const char *unit;
2302 filename = xdg_config_home_systemd("git-maintenance@.timer");
2303 if (safe_create_leading_directories(filename)) {
2304 error(_("failed to create directories for '%s'"), filename);
2305 goto error;
2307 file = fopen_or_warn(filename, "w");
2308 if (!file)
2309 goto error;
2311 unit = "# This file was created and is maintained by Git.\n"
2312 "# Any edits made in this file might be replaced in the future\n"
2313 "# by a Git command.\n"
2314 "\n"
2315 "[Unit]\n"
2316 "Description=Optimize Git repositories data\n"
2317 "\n"
2318 "[Timer]\n"
2319 "OnCalendar=%i\n"
2320 "Persistent=true\n"
2321 "\n"
2322 "[Install]\n"
2323 "WantedBy=timers.target\n";
2324 if (fputs(unit, file) == EOF) {
2325 error(_("failed to write to '%s'"), filename);
2326 fclose(file);
2327 goto error;
2329 if (fclose(file) == EOF) {
2330 error_errno(_("failed to flush '%s'"), filename);
2331 goto error;
2333 free(filename);
2335 filename = xdg_config_home_systemd("git-maintenance@.service");
2336 file = fopen_or_warn(filename, "w");
2337 if (!file)
2338 goto error;
2340 unit = "# This file was created and is maintained by Git.\n"
2341 "# Any edits made in this file might be replaced in the future\n"
2342 "# by a Git command.\n"
2343 "\n"
2344 "[Unit]\n"
2345 "Description=Optimize Git repositories data\n"
2346 "\n"
2347 "[Service]\n"
2348 "Type=oneshot\n"
2349 "ExecStart=\"%s/git\" --exec-path=\"%s\" for-each-repo --config=maintenance.repo maintenance run --schedule=%%i\n"
2350 "LockPersonality=yes\n"
2351 "MemoryDenyWriteExecute=yes\n"
2352 "NoNewPrivileges=yes\n"
2353 "RestrictAddressFamilies=AF_UNIX AF_INET AF_INET6\n"
2354 "RestrictNamespaces=yes\n"
2355 "RestrictRealtime=yes\n"
2356 "RestrictSUIDSGID=yes\n"
2357 "SystemCallArchitectures=native\n"
2358 "SystemCallFilter=@system-service\n";
2359 if (fprintf(file, unit, exec_path, exec_path) < 0) {
2360 error(_("failed to write to '%s'"), filename);
2361 fclose(file);
2362 goto error;
2364 if (fclose(file) == EOF) {
2365 error_errno(_("failed to flush '%s'"), filename);
2366 goto error;
2368 free(filename);
2369 return 0;
2371 error:
2372 free(filename);
2373 systemd_timer_delete_unit_templates();
2374 return -1;
2377 static int systemd_timer_setup_units(void)
2379 const char *exec_path = git_exec_path();
2381 int ret = systemd_timer_write_unit_templates(exec_path) ||
2382 systemd_timer_enable_unit(1, SCHEDULE_HOURLY) ||
2383 systemd_timer_enable_unit(1, SCHEDULE_DAILY) ||
2384 systemd_timer_enable_unit(1, SCHEDULE_WEEKLY);
2385 if (ret)
2386 systemd_timer_delete_units();
2387 return ret;
2390 static int systemd_timer_update_schedule(int run_maintenance, int fd)
2392 if (run_maintenance)
2393 return systemd_timer_setup_units();
2394 else
2395 return systemd_timer_delete_units();
2398 enum scheduler {
2399 SCHEDULER_INVALID = -1,
2400 SCHEDULER_AUTO,
2401 SCHEDULER_CRON,
2402 SCHEDULER_SYSTEMD,
2403 SCHEDULER_LAUNCHCTL,
2404 SCHEDULER_SCHTASKS,
2407 static const struct {
2408 const char *name;
2409 int (*is_available)(void);
2410 int (*update_schedule)(int run_maintenance, int fd);
2411 } scheduler_fn[] = {
2412 [SCHEDULER_CRON] = {
2413 .name = "crontab",
2414 .is_available = is_crontab_available,
2415 .update_schedule = crontab_update_schedule,
2417 [SCHEDULER_SYSTEMD] = {
2418 .name = "systemctl",
2419 .is_available = is_systemd_timer_available,
2420 .update_schedule = systemd_timer_update_schedule,
2422 [SCHEDULER_LAUNCHCTL] = {
2423 .name = "launchctl",
2424 .is_available = is_launchctl_available,
2425 .update_schedule = launchctl_update_schedule,
2427 [SCHEDULER_SCHTASKS] = {
2428 .name = "schtasks",
2429 .is_available = is_schtasks_available,
2430 .update_schedule = schtasks_update_schedule,
2434 static enum scheduler parse_scheduler(const char *value)
2436 if (!value)
2437 return SCHEDULER_INVALID;
2438 else if (!strcasecmp(value, "auto"))
2439 return SCHEDULER_AUTO;
2440 else if (!strcasecmp(value, "cron") || !strcasecmp(value, "crontab"))
2441 return SCHEDULER_CRON;
2442 else if (!strcasecmp(value, "systemd") ||
2443 !strcasecmp(value, "systemd-timer"))
2444 return SCHEDULER_SYSTEMD;
2445 else if (!strcasecmp(value, "launchctl"))
2446 return SCHEDULER_LAUNCHCTL;
2447 else if (!strcasecmp(value, "schtasks"))
2448 return SCHEDULER_SCHTASKS;
2449 else
2450 return SCHEDULER_INVALID;
2453 static int maintenance_opt_scheduler(const struct option *opt, const char *arg,
2454 int unset)
2456 enum scheduler *scheduler = opt->value;
2458 BUG_ON_OPT_NEG(unset);
2460 *scheduler = parse_scheduler(arg);
2461 if (*scheduler == SCHEDULER_INVALID)
2462 return error(_("unrecognized --scheduler argument '%s'"), arg);
2463 return 0;
2466 struct maintenance_start_opts {
2467 enum scheduler scheduler;
2470 static enum scheduler resolve_scheduler(enum scheduler scheduler)
2472 if (scheduler != SCHEDULER_AUTO)
2473 return scheduler;
2475 #if defined(__APPLE__)
2476 return SCHEDULER_LAUNCHCTL;
2478 #elif defined(GIT_WINDOWS_NATIVE)
2479 return SCHEDULER_SCHTASKS;
2481 #elif defined(__linux__)
2482 if (is_systemd_timer_available())
2483 return SCHEDULER_SYSTEMD;
2484 else if (is_crontab_available())
2485 return SCHEDULER_CRON;
2486 else
2487 die(_("neither systemd timers nor crontab are available"));
2489 #else
2490 return SCHEDULER_CRON;
2491 #endif
2494 static void validate_scheduler(enum scheduler scheduler)
2496 if (scheduler == SCHEDULER_INVALID)
2497 BUG("invalid scheduler");
2498 if (scheduler == SCHEDULER_AUTO)
2499 BUG("resolve_scheduler should have been called before");
2501 if (!scheduler_fn[scheduler].is_available())
2502 die(_("%s scheduler is not available"),
2503 scheduler_fn[scheduler].name);
2506 static int update_background_schedule(const struct maintenance_start_opts *opts,
2507 int enable)
2509 unsigned int i;
2510 int result = 0;
2511 struct lock_file lk;
2512 char *lock_path = xstrfmt("%s/schedule", the_repository->objects->odb->path);
2514 if (hold_lock_file_for_update(&lk, lock_path, LOCK_NO_DEREF) < 0) {
2515 free(lock_path);
2516 return error(_("another process is scheduling background maintenance"));
2519 for (i = 1; i < ARRAY_SIZE(scheduler_fn); i++) {
2520 if (enable && opts->scheduler == i)
2521 continue;
2522 if (!scheduler_fn[i].is_available())
2523 continue;
2524 scheduler_fn[i].update_schedule(0, get_lock_file_fd(&lk));
2527 if (enable)
2528 result = scheduler_fn[opts->scheduler].update_schedule(
2529 1, get_lock_file_fd(&lk));
2531 rollback_lock_file(&lk);
2533 free(lock_path);
2534 return result;
2537 static const char *const builtin_maintenance_start_usage[] = {
2538 N_("git maintenance start [--scheduler=<scheduler>]"),
2539 NULL
2542 static int maintenance_start(int argc, const char **argv, const char *prefix)
2544 struct maintenance_start_opts opts = { 0 };
2545 struct option options[] = {
2546 OPT_CALLBACK_F(
2547 0, "scheduler", &opts.scheduler, N_("scheduler"),
2548 N_("scheduler to trigger git maintenance run"),
2549 PARSE_OPT_NONEG, maintenance_opt_scheduler),
2550 OPT_END()
2552 const char *register_args[] = { "register", NULL };
2554 argc = parse_options(argc, argv, prefix, options,
2555 builtin_maintenance_start_usage, 0);
2556 if (argc)
2557 usage_with_options(builtin_maintenance_start_usage, options);
2559 opts.scheduler = resolve_scheduler(opts.scheduler);
2560 validate_scheduler(opts.scheduler);
2562 if (maintenance_register(ARRAY_SIZE(register_args)-1, register_args, NULL))
2563 warning(_("failed to add repo to global config"));
2564 return update_background_schedule(&opts, 1);
2567 static const char *const builtin_maintenance_stop_usage[] = {
2568 "git maintenance stop",
2569 NULL
2572 static int maintenance_stop(int argc, const char **argv, const char *prefix)
2574 struct option options[] = {
2575 OPT_END()
2577 argc = parse_options(argc, argv, prefix, options,
2578 builtin_maintenance_stop_usage, 0);
2579 if (argc)
2580 usage_with_options(builtin_maintenance_stop_usage, options);
2581 return update_background_schedule(NULL, 0);
2584 static const char * const builtin_maintenance_usage[] = {
2585 N_("git maintenance <subcommand> [<options>]"),
2586 NULL,
2589 int cmd_maintenance(int argc, const char **argv, const char *prefix)
2591 parse_opt_subcommand_fn *fn = NULL;
2592 struct option builtin_maintenance_options[] = {
2593 OPT_SUBCOMMAND("run", &fn, maintenance_run),
2594 OPT_SUBCOMMAND("start", &fn, maintenance_start),
2595 OPT_SUBCOMMAND("stop", &fn, maintenance_stop),
2596 OPT_SUBCOMMAND("register", &fn, maintenance_register),
2597 OPT_SUBCOMMAND("unregister", &fn, maintenance_unregister),
2598 OPT_END(),
2601 argc = parse_options(argc, argv, prefix, builtin_maintenance_options,
2602 builtin_maintenance_usage, 0);
2603 return fn(argc, argv, prefix);