pretty: add merge and exclude options to %(describe)
[git/debian.git] / builtin / gc.c
blob4c40594d660ebb21c2cf0223b21af88ea81f872c
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 "object-store.h"
34 #include "exec-cmd.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 aggressive_depth = 50;
46 static int aggressive_window = 250;
47 static int gc_auto_threshold = 6700;
48 static int gc_auto_pack_limit = 50;
49 static int detach_auto = 1;
50 static timestamp_t gc_log_expire_time;
51 static const char *gc_log_expire = "1.day.ago";
52 static const char *prune_expire = "2.weeks.ago";
53 static const char *prune_worktrees_expire = "3.months.ago";
54 static unsigned long big_pack_threshold;
55 static unsigned long max_delta_cache_size = DEFAULT_DELTA_CACHE_SIZE;
57 static struct strvec pack_refs_cmd = STRVEC_INIT;
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_expiry("gc.pruneexpire", &prune_expire);
157 git_config_get_expiry("gc.worktreepruneexpire", &prune_worktrees_expire);
158 git_config_get_expiry("gc.logexpiry", &gc_log_expire);
160 git_config_get_ulong("gc.bigpackthreshold", &big_pack_threshold);
161 git_config_get_ulong("pack.deltacachesize", &max_delta_cache_size);
163 git_config(git_default_config, NULL);
166 static int too_many_loose_objects(void)
169 * Quickly check if a "gc" is needed, by estimating how
170 * many loose objects there are. Because SHA-1 is evenly
171 * distributed, we can check only one and get a reasonable
172 * estimate.
174 DIR *dir;
175 struct dirent *ent;
176 int auto_threshold;
177 int num_loose = 0;
178 int needed = 0;
179 const unsigned hexsz_loose = the_hash_algo->hexsz - 2;
181 dir = opendir(git_path("objects/17"));
182 if (!dir)
183 return 0;
185 auto_threshold = DIV_ROUND_UP(gc_auto_threshold, 256);
186 while ((ent = readdir(dir)) != NULL) {
187 if (strspn(ent->d_name, "0123456789abcdef") != hexsz_loose ||
188 ent->d_name[hexsz_loose] != '\0')
189 continue;
190 if (++num_loose > auto_threshold) {
191 needed = 1;
192 break;
195 closedir(dir);
196 return needed;
199 static struct packed_git *find_base_packs(struct string_list *packs,
200 unsigned long limit)
202 struct packed_git *p, *base = NULL;
204 for (p = get_all_packs(the_repository); p; p = p->next) {
205 if (!p->pack_local)
206 continue;
207 if (limit) {
208 if (p->pack_size >= limit)
209 string_list_append(packs, p->pack_name);
210 } else if (!base || base->pack_size < p->pack_size) {
211 base = p;
215 if (base)
216 string_list_append(packs, base->pack_name);
218 return base;
221 static int too_many_packs(void)
223 struct packed_git *p;
224 int cnt;
226 if (gc_auto_pack_limit <= 0)
227 return 0;
229 for (cnt = 0, p = get_all_packs(the_repository); p; p = p->next) {
230 if (!p->pack_local)
231 continue;
232 if (p->pack_keep)
233 continue;
235 * Perhaps check the size of the pack and count only
236 * very small ones here?
238 cnt++;
240 return gc_auto_pack_limit < cnt;
243 static uint64_t total_ram(void)
245 #if defined(HAVE_SYSINFO)
246 struct sysinfo si;
248 if (!sysinfo(&si))
249 return si.totalram;
250 #elif defined(HAVE_BSD_SYSCTL) && (defined(HW_MEMSIZE) || defined(HW_PHYSMEM))
251 int64_t physical_memory;
252 int mib[2];
253 size_t length;
255 mib[0] = CTL_HW;
256 # if defined(HW_MEMSIZE)
257 mib[1] = HW_MEMSIZE;
258 # else
259 mib[1] = HW_PHYSMEM;
260 # endif
261 length = sizeof(int64_t);
262 if (!sysctl(mib, 2, &physical_memory, &length, NULL, 0))
263 return physical_memory;
264 #elif defined(GIT_WINDOWS_NATIVE)
265 MEMORYSTATUSEX memInfo;
267 memInfo.dwLength = sizeof(MEMORYSTATUSEX);
268 if (GlobalMemoryStatusEx(&memInfo))
269 return memInfo.ullTotalPhys;
270 #endif
271 return 0;
274 static uint64_t estimate_repack_memory(struct packed_git *pack)
276 unsigned long nr_objects = approximate_object_count();
277 size_t os_cache, heap;
279 if (!pack || !nr_objects)
280 return 0;
283 * First we have to scan through at least one pack.
284 * Assume enough room in OS file cache to keep the entire pack
285 * or we may accidentally evict data of other processes from
286 * the cache.
288 os_cache = pack->pack_size + pack->index_size;
289 /* then pack-objects needs lots more for book keeping */
290 heap = sizeof(struct object_entry) * nr_objects;
292 * internal rev-list --all --objects takes up some memory too,
293 * let's say half of it is for blobs
295 heap += sizeof(struct blob) * nr_objects / 2;
297 * and the other half is for trees (commits and tags are
298 * usually insignificant)
300 heap += sizeof(struct tree) * nr_objects / 2;
301 /* and then obj_hash[], underestimated in fact */
302 heap += sizeof(struct object *) * nr_objects;
303 /* revindex is used also */
304 heap += (sizeof(off_t) + sizeof(uint32_t)) * nr_objects;
306 * read_sha1_file() (either at delta calculation phase, or
307 * writing phase) also fills up the delta base cache
309 heap += delta_base_cache_limit;
310 /* and of course pack-objects has its own delta cache */
311 heap += max_delta_cache_size;
313 return os_cache + heap;
316 static int keep_one_pack(struct string_list_item *item, void *data)
318 strvec_pushf(&repack, "--keep-pack=%s", basename(item->string));
319 return 0;
322 static void add_repack_all_option(struct string_list *keep_pack)
324 if (prune_expire && !strcmp(prune_expire, "now"))
325 strvec_push(&repack, "-a");
326 else {
327 strvec_push(&repack, "-A");
328 if (prune_expire)
329 strvec_pushf(&repack, "--unpack-unreachable=%s", prune_expire);
332 if (keep_pack)
333 for_each_string_list(keep_pack, keep_one_pack, NULL);
336 static void add_repack_incremental_option(void)
338 strvec_push(&repack, "--no-write-bitmap-index");
341 static int need_to_gc(void)
344 * Setting gc.auto to 0 or negative can disable the
345 * automatic gc.
347 if (gc_auto_threshold <= 0)
348 return 0;
351 * If there are too many loose objects, but not too many
352 * packs, we run "repack -d -l". If there are too many packs,
353 * we run "repack -A -d -l". Otherwise we tell the caller
354 * there is no need.
356 if (too_many_packs()) {
357 struct string_list keep_pack = STRING_LIST_INIT_NODUP;
359 if (big_pack_threshold) {
360 find_base_packs(&keep_pack, big_pack_threshold);
361 if (keep_pack.nr >= gc_auto_pack_limit) {
362 big_pack_threshold = 0;
363 string_list_clear(&keep_pack, 0);
364 find_base_packs(&keep_pack, 0);
366 } else {
367 struct packed_git *p = find_base_packs(&keep_pack, 0);
368 uint64_t mem_have, mem_want;
370 mem_have = total_ram();
371 mem_want = estimate_repack_memory(p);
374 * Only allow 1/2 of memory for pack-objects, leave
375 * the rest for the OS and other processes in the
376 * system.
378 if (!mem_have || mem_want < mem_have / 2)
379 string_list_clear(&keep_pack, 0);
382 add_repack_all_option(&keep_pack);
383 string_list_clear(&keep_pack, 0);
384 } else if (too_many_loose_objects())
385 add_repack_incremental_option();
386 else
387 return 0;
389 if (run_hook_le(NULL, "pre-auto-gc", NULL))
390 return 0;
391 return 1;
394 /* return NULL on success, else hostname running the gc */
395 static const char *lock_repo_for_gc(int force, pid_t* ret_pid)
397 struct lock_file lock = LOCK_INIT;
398 char my_host[HOST_NAME_MAX + 1];
399 struct strbuf sb = STRBUF_INIT;
400 struct stat st;
401 uintmax_t pid;
402 FILE *fp;
403 int fd;
404 char *pidfile_path;
406 if (is_tempfile_active(pidfile))
407 /* already locked */
408 return NULL;
410 if (xgethostname(my_host, sizeof(my_host)))
411 xsnprintf(my_host, sizeof(my_host), "unknown");
413 pidfile_path = git_pathdup("gc.pid");
414 fd = hold_lock_file_for_update(&lock, pidfile_path,
415 LOCK_DIE_ON_ERROR);
416 if (!force) {
417 static char locking_host[HOST_NAME_MAX + 1];
418 static char *scan_fmt;
419 int should_exit;
421 if (!scan_fmt)
422 scan_fmt = xstrfmt("%s %%%ds", "%"SCNuMAX, HOST_NAME_MAX);
423 fp = fopen(pidfile_path, "r");
424 memset(locking_host, 0, sizeof(locking_host));
425 should_exit =
426 fp != NULL &&
427 !fstat(fileno(fp), &st) &&
429 * 12 hour limit is very generous as gc should
430 * never take that long. On the other hand we
431 * don't really need a strict limit here,
432 * running gc --auto one day late is not a big
433 * problem. --force can be used in manual gc
434 * after the user verifies that no gc is
435 * running.
437 time(NULL) - st.st_mtime <= 12 * 3600 &&
438 fscanf(fp, scan_fmt, &pid, locking_host) == 2 &&
439 /* be gentle to concurrent "gc" on remote hosts */
440 (strcmp(locking_host, my_host) || !kill(pid, 0) || errno == EPERM);
441 if (fp != NULL)
442 fclose(fp);
443 if (should_exit) {
444 if (fd >= 0)
445 rollback_lock_file(&lock);
446 *ret_pid = pid;
447 free(pidfile_path);
448 return locking_host;
452 strbuf_addf(&sb, "%"PRIuMAX" %s",
453 (uintmax_t) getpid(), my_host);
454 write_in_full(fd, sb.buf, sb.len);
455 strbuf_release(&sb);
456 commit_lock_file(&lock);
457 pidfile = register_tempfile(pidfile_path);
458 free(pidfile_path);
459 return NULL;
463 * Returns 0 if there was no previous error and gc can proceed, 1 if
464 * gc should not proceed due to an error in the last run. Prints a
465 * message and returns -1 if an error occurred while reading gc.log
467 static int report_last_gc_error(void)
469 struct strbuf sb = STRBUF_INIT;
470 int ret = 0;
471 ssize_t len;
472 struct stat st;
473 char *gc_log_path = git_pathdup("gc.log");
475 if (stat(gc_log_path, &st)) {
476 if (errno == ENOENT)
477 goto done;
479 ret = error_errno(_("cannot stat '%s'"), gc_log_path);
480 goto done;
483 if (st.st_mtime < gc_log_expire_time)
484 goto done;
486 len = strbuf_read_file(&sb, gc_log_path, 0);
487 if (len < 0)
488 ret = error_errno(_("cannot read '%s'"), gc_log_path);
489 else if (len > 0) {
491 * A previous gc failed. Report the error, and don't
492 * bother with an automatic gc run since it is likely
493 * to fail in the same way.
495 warning(_("The last gc run reported the following. "
496 "Please correct the root cause\n"
497 "and remove %s.\n"
498 "Automatic cleanup will not be performed "
499 "until the file is removed.\n\n"
500 "%s"),
501 gc_log_path, sb.buf);
502 ret = 1;
504 strbuf_release(&sb);
505 done:
506 free(gc_log_path);
507 return ret;
510 static void gc_before_repack(void)
513 * We may be called twice, as both the pre- and
514 * post-daemonized phases will call us, but running these
515 * commands more than once is pointless and wasteful.
517 static int done = 0;
518 if (done++)
519 return;
521 if (pack_refs && run_command_v_opt(pack_refs_cmd.v, RUN_GIT_CMD))
522 die(FAILED_RUN, pack_refs_cmd.v[0]);
524 if (prune_reflogs && run_command_v_opt(reflog.v, RUN_GIT_CMD))
525 die(FAILED_RUN, reflog.v[0]);
528 int cmd_gc(int argc, const char **argv, const char *prefix)
530 int aggressive = 0;
531 int auto_gc = 0;
532 int quiet = 0;
533 int force = 0;
534 const char *name;
535 pid_t pid;
536 int daemonized = 0;
537 int keep_largest_pack = -1;
538 timestamp_t dummy;
540 struct option builtin_gc_options[] = {
541 OPT__QUIET(&quiet, N_("suppress progress reporting")),
542 { OPTION_STRING, 0, "prune", &prune_expire, N_("date"),
543 N_("prune unreferenced objects"),
544 PARSE_OPT_OPTARG, NULL, (intptr_t)prune_expire },
545 OPT_BOOL(0, "aggressive", &aggressive, N_("be more thorough (increased runtime)")),
546 OPT_BOOL_F(0, "auto", &auto_gc, N_("enable auto-gc mode"),
547 PARSE_OPT_NOCOMPLETE),
548 OPT_BOOL_F(0, "force", &force,
549 N_("force running gc even if there may be another gc running"),
550 PARSE_OPT_NOCOMPLETE),
551 OPT_BOOL(0, "keep-largest-pack", &keep_largest_pack,
552 N_("repack all other packs except the largest pack")),
553 OPT_END()
556 if (argc == 2 && !strcmp(argv[1], "-h"))
557 usage_with_options(builtin_gc_usage, builtin_gc_options);
559 strvec_pushl(&pack_refs_cmd, "pack-refs", "--all", "--prune", NULL);
560 strvec_pushl(&reflog, "reflog", "expire", "--all", NULL);
561 strvec_pushl(&repack, "repack", "-d", "-l", NULL);
562 strvec_pushl(&prune, "prune", "--expire", NULL);
563 strvec_pushl(&prune_worktrees, "worktree", "prune", "--expire", NULL);
564 strvec_pushl(&rerere, "rerere", "gc", NULL);
566 /* default expiry time, overwritten in gc_config */
567 gc_config();
568 if (parse_expiry_date(gc_log_expire, &gc_log_expire_time))
569 die(_("failed to parse gc.logexpiry value %s"), gc_log_expire);
571 if (pack_refs < 0)
572 pack_refs = !is_bare_repository();
574 argc = parse_options(argc, argv, prefix, builtin_gc_options,
575 builtin_gc_usage, 0);
576 if (argc > 0)
577 usage_with_options(builtin_gc_usage, builtin_gc_options);
579 if (prune_expire && parse_expiry_date(prune_expire, &dummy))
580 die(_("failed to parse prune expiry value %s"), prune_expire);
582 if (aggressive) {
583 strvec_push(&repack, "-f");
584 if (aggressive_depth > 0)
585 strvec_pushf(&repack, "--depth=%d", aggressive_depth);
586 if (aggressive_window > 0)
587 strvec_pushf(&repack, "--window=%d", aggressive_window);
589 if (quiet)
590 strvec_push(&repack, "-q");
592 if (auto_gc) {
594 * Auto-gc should be least intrusive as possible.
596 if (!need_to_gc())
597 return 0;
598 if (!quiet) {
599 if (detach_auto)
600 fprintf(stderr, _("Auto packing the repository in background for optimum performance.\n"));
601 else
602 fprintf(stderr, _("Auto packing the repository for optimum performance.\n"));
603 fprintf(stderr, _("See \"git help gc\" for manual housekeeping.\n"));
605 if (detach_auto) {
606 int ret = report_last_gc_error();
607 if (ret < 0)
608 /* an I/O error occurred, already reported */
609 exit(128);
610 if (ret == 1)
611 /* Last gc --auto failed. Skip this one. */
612 return 0;
614 if (lock_repo_for_gc(force, &pid))
615 return 0;
616 gc_before_repack(); /* dies on failure */
617 delete_tempfile(&pidfile);
620 * failure to daemonize is ok, we'll continue
621 * in foreground
623 daemonized = !daemonize();
625 } else {
626 struct string_list keep_pack = STRING_LIST_INIT_NODUP;
628 if (keep_largest_pack != -1) {
629 if (keep_largest_pack)
630 find_base_packs(&keep_pack, 0);
631 } else if (big_pack_threshold) {
632 find_base_packs(&keep_pack, big_pack_threshold);
635 add_repack_all_option(&keep_pack);
636 string_list_clear(&keep_pack, 0);
639 name = lock_repo_for_gc(force, &pid);
640 if (name) {
641 if (auto_gc)
642 return 0; /* be quiet on --auto */
643 die(_("gc is already running on machine '%s' pid %"PRIuMAX" (use --force if not)"),
644 name, (uintmax_t)pid);
647 if (daemonized) {
648 hold_lock_file_for_update(&log_lock,
649 git_path("gc.log"),
650 LOCK_DIE_ON_ERROR);
651 dup2(get_lock_file_fd(&log_lock), 2);
652 sigchain_push_common(process_log_file_on_signal);
653 atexit(process_log_file_at_exit);
656 gc_before_repack();
658 if (!repository_format_precious_objects) {
659 close_object_store(the_repository->objects);
660 if (run_command_v_opt(repack.v, RUN_GIT_CMD))
661 die(FAILED_RUN, repack.v[0]);
663 if (prune_expire) {
664 strvec_push(&prune, prune_expire);
665 if (quiet)
666 strvec_push(&prune, "--no-progress");
667 if (has_promisor_remote())
668 strvec_push(&prune,
669 "--exclude-promisor-objects");
670 if (run_command_v_opt(prune.v, RUN_GIT_CMD))
671 die(FAILED_RUN, prune.v[0]);
675 if (prune_worktrees_expire) {
676 strvec_push(&prune_worktrees, prune_worktrees_expire);
677 if (run_command_v_opt(prune_worktrees.v, RUN_GIT_CMD))
678 die(FAILED_RUN, prune_worktrees.v[0]);
681 if (run_command_v_opt(rerere.v, RUN_GIT_CMD))
682 die(FAILED_RUN, rerere.v[0]);
684 report_garbage = report_pack_garbage;
685 reprepare_packed_git(the_repository);
686 if (pack_garbage.nr > 0) {
687 close_object_store(the_repository->objects);
688 clean_pack_garbage();
691 prepare_repo_settings(the_repository);
692 if (the_repository->settings.gc_write_commit_graph == 1)
693 write_commit_graph_reachable(the_repository->objects->odb,
694 !quiet && !daemonized ? COMMIT_GRAPH_WRITE_PROGRESS : 0,
695 NULL);
697 if (auto_gc && too_many_loose_objects())
698 warning(_("There are too many unreachable loose objects; "
699 "run 'git prune' to remove them."));
701 if (!daemonized)
702 unlink(git_path("gc.log"));
704 return 0;
707 static const char *const builtin_maintenance_run_usage[] = {
708 N_("git maintenance run [--auto] [--[no-]quiet] [--task=<task>] [--schedule]"),
709 NULL
712 enum schedule_priority {
713 SCHEDULE_NONE = 0,
714 SCHEDULE_WEEKLY = 1,
715 SCHEDULE_DAILY = 2,
716 SCHEDULE_HOURLY = 3,
719 static enum schedule_priority parse_schedule(const char *value)
721 if (!value)
722 return SCHEDULE_NONE;
723 if (!strcasecmp(value, "hourly"))
724 return SCHEDULE_HOURLY;
725 if (!strcasecmp(value, "daily"))
726 return SCHEDULE_DAILY;
727 if (!strcasecmp(value, "weekly"))
728 return SCHEDULE_WEEKLY;
729 return SCHEDULE_NONE;
732 static int maintenance_opt_schedule(const struct option *opt, const char *arg,
733 int unset)
735 enum schedule_priority *priority = opt->value;
737 if (unset)
738 die(_("--no-schedule is not allowed"));
740 *priority = parse_schedule(arg);
742 if (!*priority)
743 die(_("unrecognized --schedule argument '%s'"), arg);
745 return 0;
748 struct maintenance_run_opts {
749 int auto_flag;
750 int quiet;
751 enum schedule_priority schedule;
754 /* Remember to update object flag allocation in object.h */
755 #define SEEN (1u<<0)
757 struct cg_auto_data {
758 int num_not_in_graph;
759 int limit;
762 static int dfs_on_ref(const char *refname,
763 const struct object_id *oid, int flags,
764 void *cb_data)
766 struct cg_auto_data *data = (struct cg_auto_data *)cb_data;
767 int result = 0;
768 struct object_id peeled;
769 struct commit_list *stack = NULL;
770 struct commit *commit;
772 if (!peel_iterated_oid(oid, &peeled))
773 oid = &peeled;
774 if (oid_object_info(the_repository, oid, NULL) != OBJ_COMMIT)
775 return 0;
777 commit = lookup_commit(the_repository, oid);
778 if (!commit)
779 return 0;
780 if (parse_commit(commit) ||
781 commit_graph_position(commit) != COMMIT_NOT_FROM_GRAPH)
782 return 0;
784 data->num_not_in_graph++;
786 if (data->num_not_in_graph >= data->limit)
787 return 1;
789 commit_list_append(commit, &stack);
791 while (!result && stack) {
792 struct commit_list *parent;
794 commit = pop_commit(&stack);
796 for (parent = commit->parents; parent; parent = parent->next) {
797 if (parse_commit(parent->item) ||
798 commit_graph_position(parent->item) != COMMIT_NOT_FROM_GRAPH ||
799 parent->item->object.flags & SEEN)
800 continue;
802 parent->item->object.flags |= SEEN;
803 data->num_not_in_graph++;
805 if (data->num_not_in_graph >= data->limit) {
806 result = 1;
807 break;
810 commit_list_append(parent->item, &stack);
814 free_commit_list(stack);
815 return result;
818 static int should_write_commit_graph(void)
820 int result;
821 struct cg_auto_data data;
823 data.num_not_in_graph = 0;
824 data.limit = 100;
825 git_config_get_int("maintenance.commit-graph.auto",
826 &data.limit);
828 if (!data.limit)
829 return 0;
830 if (data.limit < 0)
831 return 1;
833 result = for_each_ref(dfs_on_ref, &data);
835 repo_clear_commit_marks(the_repository, SEEN);
837 return result;
840 static int run_write_commit_graph(struct maintenance_run_opts *opts)
842 struct child_process child = CHILD_PROCESS_INIT;
844 child.git_cmd = 1;
845 strvec_pushl(&child.args, "commit-graph", "write",
846 "--split", "--reachable", NULL);
848 if (opts->quiet)
849 strvec_push(&child.args, "--no-progress");
851 return !!run_command(&child);
854 static int maintenance_task_commit_graph(struct maintenance_run_opts *opts)
856 prepare_repo_settings(the_repository);
857 if (!the_repository->settings.core_commit_graph)
858 return 0;
860 close_object_store(the_repository->objects);
861 if (run_write_commit_graph(opts)) {
862 error(_("failed to write commit-graph"));
863 return 1;
866 return 0;
869 static int fetch_remote(const char *remote, struct maintenance_run_opts *opts)
871 struct child_process child = CHILD_PROCESS_INIT;
873 child.git_cmd = 1;
874 strvec_pushl(&child.args, "fetch", remote, "--prune", "--no-tags",
875 "--no-write-fetch-head", "--recurse-submodules=no",
876 "--refmap=", NULL);
878 if (opts->quiet)
879 strvec_push(&child.args, "--quiet");
881 strvec_pushf(&child.args, "+refs/heads/*:refs/prefetch/%s/*", remote);
883 return !!run_command(&child);
886 static int append_remote(struct remote *remote, void *cbdata)
888 struct string_list *remotes = (struct string_list *)cbdata;
890 string_list_append(remotes, remote->name);
891 return 0;
894 static int maintenance_task_prefetch(struct maintenance_run_opts *opts)
896 int result = 0;
897 struct string_list_item *item;
898 struct string_list remotes = STRING_LIST_INIT_DUP;
900 git_config_set_multivar_gently("log.excludedecoration",
901 "refs/prefetch/",
902 "refs/prefetch/",
903 CONFIG_FLAGS_FIXED_VALUE |
904 CONFIG_FLAGS_MULTI_REPLACE);
906 if (for_each_remote(append_remote, &remotes)) {
907 error(_("failed to fill remotes"));
908 result = 1;
909 goto cleanup;
912 for_each_string_list_item(item, &remotes)
913 result |= fetch_remote(item->string, opts);
915 cleanup:
916 string_list_clear(&remotes, 0);
917 return result;
920 static int maintenance_task_gc(struct maintenance_run_opts *opts)
922 struct child_process child = CHILD_PROCESS_INIT;
924 child.git_cmd = 1;
925 strvec_push(&child.args, "gc");
927 if (opts->auto_flag)
928 strvec_push(&child.args, "--auto");
929 if (opts->quiet)
930 strvec_push(&child.args, "--quiet");
931 else
932 strvec_push(&child.args, "--no-quiet");
934 close_object_store(the_repository->objects);
935 return run_command(&child);
938 static int prune_packed(struct maintenance_run_opts *opts)
940 struct child_process child = CHILD_PROCESS_INIT;
942 child.git_cmd = 1;
943 strvec_push(&child.args, "prune-packed");
945 if (opts->quiet)
946 strvec_push(&child.args, "--quiet");
948 return !!run_command(&child);
951 struct write_loose_object_data {
952 FILE *in;
953 int count;
954 int batch_size;
957 static int loose_object_auto_limit = 100;
959 static int loose_object_count(const struct object_id *oid,
960 const char *path,
961 void *data)
963 int *count = (int*)data;
964 if (++(*count) >= loose_object_auto_limit)
965 return 1;
966 return 0;
969 static int loose_object_auto_condition(void)
971 int count = 0;
973 git_config_get_int("maintenance.loose-objects.auto",
974 &loose_object_auto_limit);
976 if (!loose_object_auto_limit)
977 return 0;
978 if (loose_object_auto_limit < 0)
979 return 1;
981 return for_each_loose_file_in_objdir(the_repository->objects->odb->path,
982 loose_object_count,
983 NULL, NULL, &count);
986 static int bail_on_loose(const struct object_id *oid,
987 const char *path,
988 void *data)
990 return 1;
993 static int write_loose_object_to_stdin(const struct object_id *oid,
994 const char *path,
995 void *data)
997 struct write_loose_object_data *d = (struct write_loose_object_data *)data;
999 fprintf(d->in, "%s\n", oid_to_hex(oid));
1001 return ++(d->count) > d->batch_size;
1004 static int pack_loose(struct maintenance_run_opts *opts)
1006 struct repository *r = the_repository;
1007 int result = 0;
1008 struct write_loose_object_data data;
1009 struct child_process pack_proc = CHILD_PROCESS_INIT;
1012 * Do not start pack-objects process
1013 * if there are no loose objects.
1015 if (!for_each_loose_file_in_objdir(r->objects->odb->path,
1016 bail_on_loose,
1017 NULL, NULL, NULL))
1018 return 0;
1020 pack_proc.git_cmd = 1;
1022 strvec_push(&pack_proc.args, "pack-objects");
1023 if (opts->quiet)
1024 strvec_push(&pack_proc.args, "--quiet");
1025 strvec_pushf(&pack_proc.args, "%s/pack/loose", r->objects->odb->path);
1027 pack_proc.in = -1;
1029 if (start_command(&pack_proc)) {
1030 error(_("failed to start 'git pack-objects' process"));
1031 return 1;
1034 data.in = xfdopen(pack_proc.in, "w");
1035 data.count = 0;
1036 data.batch_size = 50000;
1038 for_each_loose_file_in_objdir(r->objects->odb->path,
1039 write_loose_object_to_stdin,
1040 NULL,
1041 NULL,
1042 &data);
1044 fclose(data.in);
1046 if (finish_command(&pack_proc)) {
1047 error(_("failed to finish 'git pack-objects' process"));
1048 result = 1;
1051 return result;
1054 static int maintenance_task_loose_objects(struct maintenance_run_opts *opts)
1056 return prune_packed(opts) || pack_loose(opts);
1059 static int incremental_repack_auto_condition(void)
1061 struct packed_git *p;
1062 int enabled;
1063 int incremental_repack_auto_limit = 10;
1064 int count = 0;
1066 if (git_config_get_bool("core.multiPackIndex", &enabled) ||
1067 !enabled)
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 = 1;
1109 strvec_pushl(&child.args, "multi-pack-index", "expire", NULL);
1111 if (opts->quiet)
1112 strvec_push(&child.args, "--no-progress");
1114 close_object_store(the_repository->objects);
1116 if (run_command(&child))
1117 return error(_("'git multi-pack-index expire' failed"));
1119 return 0;
1122 #define TWO_GIGABYTES (INT32_MAX)
1124 static off_t get_auto_pack_size(void)
1127 * The "auto" value is special: we optimize for
1128 * one large pack-file (i.e. from a clone) and
1129 * expect the rest to be small and they can be
1130 * repacked quickly.
1132 * The strategy we select here is to select a
1133 * size that is one more than the second largest
1134 * pack-file. This ensures that we will repack
1135 * at least two packs if there are three or more
1136 * packs.
1138 off_t max_size = 0;
1139 off_t second_largest_size = 0;
1140 off_t result_size;
1141 struct packed_git *p;
1142 struct repository *r = the_repository;
1144 reprepare_packed_git(r);
1145 for (p = get_all_packs(r); p; p = p->next) {
1146 if (p->pack_size > max_size) {
1147 second_largest_size = max_size;
1148 max_size = p->pack_size;
1149 } else if (p->pack_size > second_largest_size)
1150 second_largest_size = p->pack_size;
1153 result_size = second_largest_size + 1;
1155 /* But limit ourselves to a batch size of 2g */
1156 if (result_size > TWO_GIGABYTES)
1157 result_size = TWO_GIGABYTES;
1159 return result_size;
1162 static int multi_pack_index_repack(struct maintenance_run_opts *opts)
1164 struct child_process child = CHILD_PROCESS_INIT;
1166 child.git_cmd = 1;
1167 strvec_pushl(&child.args, "multi-pack-index", "repack", NULL);
1169 if (opts->quiet)
1170 strvec_push(&child.args, "--no-progress");
1172 strvec_pushf(&child.args, "--batch-size=%"PRIuMAX,
1173 (uintmax_t)get_auto_pack_size());
1175 close_object_store(the_repository->objects);
1177 if (run_command(&child))
1178 return error(_("'git multi-pack-index repack' failed"));
1180 return 0;
1183 static int maintenance_task_incremental_repack(struct maintenance_run_opts *opts)
1185 prepare_repo_settings(the_repository);
1186 if (!the_repository->settings.core_multi_pack_index) {
1187 warning(_("skipping incremental-repack task because core.multiPackIndex is disabled"));
1188 return 0;
1191 if (multi_pack_index_write(opts))
1192 return 1;
1193 if (multi_pack_index_expire(opts))
1194 return 1;
1195 if (multi_pack_index_repack(opts))
1196 return 1;
1197 return 0;
1200 typedef int maintenance_task_fn(struct maintenance_run_opts *opts);
1203 * An auto condition function returns 1 if the task should run
1204 * and 0 if the task should NOT run. See needs_to_gc() for an
1205 * example.
1207 typedef int maintenance_auto_fn(void);
1209 struct maintenance_task {
1210 const char *name;
1211 maintenance_task_fn *fn;
1212 maintenance_auto_fn *auto_condition;
1213 unsigned enabled:1;
1215 enum schedule_priority schedule;
1217 /* -1 if not selected. */
1218 int selected_order;
1221 enum maintenance_task_label {
1222 TASK_PREFETCH,
1223 TASK_LOOSE_OBJECTS,
1224 TASK_INCREMENTAL_REPACK,
1225 TASK_GC,
1226 TASK_COMMIT_GRAPH,
1228 /* Leave as final value */
1229 TASK__COUNT
1232 static struct maintenance_task tasks[] = {
1233 [TASK_PREFETCH] = {
1234 "prefetch",
1235 maintenance_task_prefetch,
1237 [TASK_LOOSE_OBJECTS] = {
1238 "loose-objects",
1239 maintenance_task_loose_objects,
1240 loose_object_auto_condition,
1242 [TASK_INCREMENTAL_REPACK] = {
1243 "incremental-repack",
1244 maintenance_task_incremental_repack,
1245 incremental_repack_auto_condition,
1247 [TASK_GC] = {
1248 "gc",
1249 maintenance_task_gc,
1250 need_to_gc,
1253 [TASK_COMMIT_GRAPH] = {
1254 "commit-graph",
1255 maintenance_task_commit_graph,
1256 should_write_commit_graph,
1260 static int compare_tasks_by_selection(const void *a_, const void *b_)
1262 const struct maintenance_task *a = a_;
1263 const struct maintenance_task *b = b_;
1265 return b->selected_order - a->selected_order;
1268 static int maintenance_run_tasks(struct maintenance_run_opts *opts)
1270 int i, found_selected = 0;
1271 int result = 0;
1272 struct lock_file lk;
1273 struct repository *r = the_repository;
1274 char *lock_path = xstrfmt("%s/maintenance", r->objects->odb->path);
1276 if (hold_lock_file_for_update(&lk, lock_path, LOCK_NO_DEREF) < 0) {
1278 * Another maintenance command is running.
1280 * If --auto was provided, then it is likely due to a
1281 * recursive process stack. Do not report an error in
1282 * that case.
1284 if (!opts->auto_flag && !opts->quiet)
1285 warning(_("lock file '%s' exists, skipping maintenance"),
1286 lock_path);
1287 free(lock_path);
1288 return 0;
1290 free(lock_path);
1292 for (i = 0; !found_selected && i < TASK__COUNT; i++)
1293 found_selected = tasks[i].selected_order >= 0;
1295 if (found_selected)
1296 QSORT(tasks, TASK__COUNT, compare_tasks_by_selection);
1298 for (i = 0; i < TASK__COUNT; i++) {
1299 if (found_selected && tasks[i].selected_order < 0)
1300 continue;
1302 if (!found_selected && !tasks[i].enabled)
1303 continue;
1305 if (opts->auto_flag &&
1306 (!tasks[i].auto_condition ||
1307 !tasks[i].auto_condition()))
1308 continue;
1310 if (opts->schedule && tasks[i].schedule < opts->schedule)
1311 continue;
1313 trace2_region_enter("maintenance", tasks[i].name, r);
1314 if (tasks[i].fn(opts)) {
1315 error(_("task '%s' failed"), tasks[i].name);
1316 result = 1;
1318 trace2_region_leave("maintenance", tasks[i].name, r);
1321 rollback_lock_file(&lk);
1322 return result;
1325 static void initialize_maintenance_strategy(void)
1327 char *config_str;
1329 if (git_config_get_string("maintenance.strategy", &config_str))
1330 return;
1332 if (!strcasecmp(config_str, "incremental")) {
1333 tasks[TASK_GC].schedule = SCHEDULE_NONE;
1334 tasks[TASK_COMMIT_GRAPH].enabled = 1;
1335 tasks[TASK_COMMIT_GRAPH].schedule = SCHEDULE_HOURLY;
1336 tasks[TASK_PREFETCH].enabled = 1;
1337 tasks[TASK_PREFETCH].schedule = SCHEDULE_HOURLY;
1338 tasks[TASK_INCREMENTAL_REPACK].enabled = 1;
1339 tasks[TASK_INCREMENTAL_REPACK].schedule = SCHEDULE_DAILY;
1340 tasks[TASK_LOOSE_OBJECTS].enabled = 1;
1341 tasks[TASK_LOOSE_OBJECTS].schedule = SCHEDULE_DAILY;
1345 static void initialize_task_config(int schedule)
1347 int i;
1348 struct strbuf config_name = STRBUF_INIT;
1349 gc_config();
1351 if (schedule)
1352 initialize_maintenance_strategy();
1354 for (i = 0; i < TASK__COUNT; i++) {
1355 int config_value;
1356 char *config_str;
1358 strbuf_reset(&config_name);
1359 strbuf_addf(&config_name, "maintenance.%s.enabled",
1360 tasks[i].name);
1362 if (!git_config_get_bool(config_name.buf, &config_value))
1363 tasks[i].enabled = config_value;
1365 strbuf_reset(&config_name);
1366 strbuf_addf(&config_name, "maintenance.%s.schedule",
1367 tasks[i].name);
1369 if (!git_config_get_string(config_name.buf, &config_str)) {
1370 tasks[i].schedule = parse_schedule(config_str);
1371 free(config_str);
1375 strbuf_release(&config_name);
1378 static int task_option_parse(const struct option *opt,
1379 const char *arg, int unset)
1381 int i, num_selected = 0;
1382 struct maintenance_task *task = NULL;
1384 BUG_ON_OPT_NEG(unset);
1386 for (i = 0; i < TASK__COUNT; i++) {
1387 if (tasks[i].selected_order >= 0)
1388 num_selected++;
1389 if (!strcasecmp(tasks[i].name, arg)) {
1390 task = &tasks[i];
1394 if (!task) {
1395 error(_("'%s' is not a valid task"), arg);
1396 return 1;
1399 if (task->selected_order >= 0) {
1400 error(_("task '%s' cannot be selected multiple times"), arg);
1401 return 1;
1404 task->selected_order = num_selected + 1;
1406 return 0;
1409 static int maintenance_run(int argc, const char **argv, const char *prefix)
1411 int i;
1412 struct maintenance_run_opts opts;
1413 struct option builtin_maintenance_run_options[] = {
1414 OPT_BOOL(0, "auto", &opts.auto_flag,
1415 N_("run tasks based on the state of the repository")),
1416 OPT_CALLBACK(0, "schedule", &opts.schedule, N_("frequency"),
1417 N_("run tasks based on frequency"),
1418 maintenance_opt_schedule),
1419 OPT_BOOL(0, "quiet", &opts.quiet,
1420 N_("do not report progress or other information over stderr")),
1421 OPT_CALLBACK_F(0, "task", NULL, N_("task"),
1422 N_("run a specific task"),
1423 PARSE_OPT_NONEG, task_option_parse),
1424 OPT_END()
1426 memset(&opts, 0, sizeof(opts));
1428 opts.quiet = !isatty(2);
1430 for (i = 0; i < TASK__COUNT; i++)
1431 tasks[i].selected_order = -1;
1433 argc = parse_options(argc, argv, prefix,
1434 builtin_maintenance_run_options,
1435 builtin_maintenance_run_usage,
1436 PARSE_OPT_STOP_AT_NON_OPTION);
1438 if (opts.auto_flag && opts.schedule)
1439 die(_("use at most one of --auto and --schedule=<frequency>"));
1441 initialize_task_config(opts.schedule);
1443 if (argc != 0)
1444 usage_with_options(builtin_maintenance_run_usage,
1445 builtin_maintenance_run_options);
1446 return maintenance_run_tasks(&opts);
1449 static int maintenance_register(void)
1451 char *config_value;
1452 struct child_process config_set = CHILD_PROCESS_INIT;
1453 struct child_process config_get = CHILD_PROCESS_INIT;
1455 /* Disable foreground maintenance */
1456 git_config_set("maintenance.auto", "false");
1458 /* Set maintenance strategy, if unset */
1459 if (!git_config_get_string("maintenance.strategy", &config_value))
1460 free(config_value);
1461 else
1462 git_config_set("maintenance.strategy", "incremental");
1464 config_get.git_cmd = 1;
1465 strvec_pushl(&config_get.args, "config", "--global", "--get",
1466 "--fixed-value", "maintenance.repo",
1467 the_repository->worktree ? the_repository->worktree
1468 : the_repository->gitdir,
1469 NULL);
1470 config_get.out = -1;
1472 if (start_command(&config_get))
1473 return error(_("failed to run 'git config'"));
1475 /* We already have this value in our config! */
1476 if (!finish_command(&config_get))
1477 return 0;
1479 config_set.git_cmd = 1;
1480 strvec_pushl(&config_set.args, "config", "--add", "--global", "maintenance.repo",
1481 the_repository->worktree ? the_repository->worktree
1482 : the_repository->gitdir,
1483 NULL);
1485 return run_command(&config_set);
1488 static int maintenance_unregister(void)
1490 struct child_process config_unset = CHILD_PROCESS_INIT;
1492 config_unset.git_cmd = 1;
1493 strvec_pushl(&config_unset.args, "config", "--global", "--unset",
1494 "--fixed-value", "maintenance.repo",
1495 the_repository->worktree ? the_repository->worktree
1496 : the_repository->gitdir,
1497 NULL);
1499 return run_command(&config_unset);
1502 static const char *get_frequency(enum schedule_priority schedule)
1504 switch (schedule) {
1505 case SCHEDULE_HOURLY:
1506 return "hourly";
1507 case SCHEDULE_DAILY:
1508 return "daily";
1509 case SCHEDULE_WEEKLY:
1510 return "weekly";
1511 default:
1512 BUG("invalid schedule %d", schedule);
1516 static char *launchctl_service_name(const char *frequency)
1518 struct strbuf label = STRBUF_INIT;
1519 strbuf_addf(&label, "org.git-scm.git.%s", frequency);
1520 return strbuf_detach(&label, NULL);
1523 static char *launchctl_service_filename(const char *name)
1525 char *expanded;
1526 struct strbuf filename = STRBUF_INIT;
1527 strbuf_addf(&filename, "~/Library/LaunchAgents/%s.plist", name);
1529 expanded = expand_user_path(filename.buf, 1);
1530 if (!expanded)
1531 die(_("failed to expand path '%s'"), filename.buf);
1533 strbuf_release(&filename);
1534 return expanded;
1537 static char *launchctl_get_uid(void)
1539 return xstrfmt("gui/%d", getuid());
1542 static int launchctl_boot_plist(int enable, const char *filename, const char *cmd)
1544 int result;
1545 struct child_process child = CHILD_PROCESS_INIT;
1546 char *uid = launchctl_get_uid();
1548 strvec_split(&child.args, cmd);
1549 if (enable)
1550 strvec_push(&child.args, "bootstrap");
1551 else
1552 strvec_push(&child.args, "bootout");
1553 strvec_push(&child.args, uid);
1554 strvec_push(&child.args, filename);
1556 child.no_stderr = 1;
1557 child.no_stdout = 1;
1559 if (start_command(&child))
1560 die(_("failed to start launchctl"));
1562 result = finish_command(&child);
1564 free(uid);
1565 return result;
1568 static int launchctl_remove_plist(enum schedule_priority schedule, const char *cmd)
1570 const char *frequency = get_frequency(schedule);
1571 char *name = launchctl_service_name(frequency);
1572 char *filename = launchctl_service_filename(name);
1573 int result = launchctl_boot_plist(0, filename, cmd);
1574 unlink(filename);
1575 free(filename);
1576 free(name);
1577 return result;
1580 static int launchctl_remove_plists(const char *cmd)
1582 return launchctl_remove_plist(SCHEDULE_HOURLY, cmd) ||
1583 launchctl_remove_plist(SCHEDULE_DAILY, cmd) ||
1584 launchctl_remove_plist(SCHEDULE_WEEKLY, cmd);
1587 static int launchctl_schedule_plist(const char *exec_path, enum schedule_priority schedule, const char *cmd)
1589 FILE *plist;
1590 int i;
1591 const char *preamble, *repeat;
1592 const char *frequency = get_frequency(schedule);
1593 char *name = launchctl_service_name(frequency);
1594 char *filename = launchctl_service_filename(name);
1596 if (safe_create_leading_directories(filename))
1597 die(_("failed to create directories for '%s'"), filename);
1598 plist = xfopen(filename, "w");
1600 preamble = "<?xml version=\"1.0\"?>\n"
1601 "<!DOCTYPE plist PUBLIC \"-//Apple//DTD PLIST 1.0//EN\" \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">\n"
1602 "<plist version=\"1.0\">"
1603 "<dict>\n"
1604 "<key>Label</key><string>%s</string>\n"
1605 "<key>ProgramArguments</key>\n"
1606 "<array>\n"
1607 "<string>%s/git</string>\n"
1608 "<string>--exec-path=%s</string>\n"
1609 "<string>for-each-repo</string>\n"
1610 "<string>--config=maintenance.repo</string>\n"
1611 "<string>maintenance</string>\n"
1612 "<string>run</string>\n"
1613 "<string>--schedule=%s</string>\n"
1614 "</array>\n"
1615 "<key>StartCalendarInterval</key>\n"
1616 "<array>\n";
1617 fprintf(plist, preamble, name, exec_path, exec_path, frequency);
1619 switch (schedule) {
1620 case SCHEDULE_HOURLY:
1621 repeat = "<dict>\n"
1622 "<key>Hour</key><integer>%d</integer>\n"
1623 "<key>Minute</key><integer>0</integer>\n"
1624 "</dict>\n";
1625 for (i = 1; i <= 23; i++)
1626 fprintf(plist, repeat, i);
1627 break;
1629 case SCHEDULE_DAILY:
1630 repeat = "<dict>\n"
1631 "<key>Day</key><integer>%d</integer>\n"
1632 "<key>Hour</key><integer>0</integer>\n"
1633 "<key>Minute</key><integer>0</integer>\n"
1634 "</dict>\n";
1635 for (i = 1; i <= 6; i++)
1636 fprintf(plist, repeat, i);
1637 break;
1639 case SCHEDULE_WEEKLY:
1640 fprintf(plist,
1641 "<dict>\n"
1642 "<key>Day</key><integer>0</integer>\n"
1643 "<key>Hour</key><integer>0</integer>\n"
1644 "<key>Minute</key><integer>0</integer>\n"
1645 "</dict>\n");
1646 break;
1648 default:
1649 /* unreachable */
1650 break;
1652 fprintf(plist, "</array>\n</dict>\n</plist>\n");
1653 fclose(plist);
1655 /* bootout might fail if not already running, so ignore */
1656 launchctl_boot_plist(0, filename, cmd);
1657 if (launchctl_boot_plist(1, filename, cmd))
1658 die(_("failed to bootstrap service %s"), filename);
1660 free(filename);
1661 free(name);
1662 return 0;
1665 static int launchctl_add_plists(const char *cmd)
1667 const char *exec_path = git_exec_path();
1669 return launchctl_schedule_plist(exec_path, SCHEDULE_HOURLY, cmd) ||
1670 launchctl_schedule_plist(exec_path, SCHEDULE_DAILY, cmd) ||
1671 launchctl_schedule_plist(exec_path, SCHEDULE_WEEKLY, cmd);
1674 static int launchctl_update_schedule(int run_maintenance, int fd, const char *cmd)
1676 if (run_maintenance)
1677 return launchctl_add_plists(cmd);
1678 else
1679 return launchctl_remove_plists(cmd);
1682 static char *schtasks_task_name(const char *frequency)
1684 struct strbuf label = STRBUF_INIT;
1685 strbuf_addf(&label, "Git Maintenance (%s)", frequency);
1686 return strbuf_detach(&label, NULL);
1689 static int schtasks_remove_task(enum schedule_priority schedule, const char *cmd)
1691 int result;
1692 struct strvec args = STRVEC_INIT;
1693 const char *frequency = get_frequency(schedule);
1694 char *name = schtasks_task_name(frequency);
1696 strvec_split(&args, cmd);
1697 strvec_pushl(&args, "/delete", "/tn", name, "/f", NULL);
1699 result = run_command_v_opt(args.v, 0);
1701 strvec_clear(&args);
1702 free(name);
1703 return result;
1706 static int schtasks_remove_tasks(const char *cmd)
1708 return schtasks_remove_task(SCHEDULE_HOURLY, cmd) ||
1709 schtasks_remove_task(SCHEDULE_DAILY, cmd) ||
1710 schtasks_remove_task(SCHEDULE_WEEKLY, cmd);
1713 static int schtasks_schedule_task(const char *exec_path, enum schedule_priority schedule, const char *cmd)
1715 int result;
1716 struct child_process child = CHILD_PROCESS_INIT;
1717 const char *xml;
1718 struct tempfile *tfile;
1719 const char *frequency = get_frequency(schedule);
1720 char *name = schtasks_task_name(frequency);
1721 struct strbuf tfilename = STRBUF_INIT;
1723 strbuf_addf(&tfilename, "%s/schedule_%s_XXXXXX",
1724 get_git_common_dir(), frequency);
1725 tfile = xmks_tempfile(tfilename.buf);
1726 strbuf_release(&tfilename);
1728 if (!fdopen_tempfile(tfile, "w"))
1729 die(_("failed to create temp xml file"));
1731 xml = "<?xml version=\"1.0\" ?>\n"
1732 "<Task version=\"1.4\" xmlns=\"http://schemas.microsoft.com/windows/2004/02/mit/task\">\n"
1733 "<Triggers>\n"
1734 "<CalendarTrigger>\n";
1735 fputs(xml, tfile->fp);
1737 switch (schedule) {
1738 case SCHEDULE_HOURLY:
1739 fprintf(tfile->fp,
1740 "<StartBoundary>2020-01-01T01:00:00</StartBoundary>\n"
1741 "<Enabled>true</Enabled>\n"
1742 "<ScheduleByDay>\n"
1743 "<DaysInterval>1</DaysInterval>\n"
1744 "</ScheduleByDay>\n"
1745 "<Repetition>\n"
1746 "<Interval>PT1H</Interval>\n"
1747 "<Duration>PT23H</Duration>\n"
1748 "<StopAtDurationEnd>false</StopAtDurationEnd>\n"
1749 "</Repetition>\n");
1750 break;
1752 case SCHEDULE_DAILY:
1753 fprintf(tfile->fp,
1754 "<StartBoundary>2020-01-01T00:00:00</StartBoundary>\n"
1755 "<Enabled>true</Enabled>\n"
1756 "<ScheduleByWeek>\n"
1757 "<DaysOfWeek>\n"
1758 "<Monday />\n"
1759 "<Tuesday />\n"
1760 "<Wednesday />\n"
1761 "<Thursday />\n"
1762 "<Friday />\n"
1763 "<Saturday />\n"
1764 "</DaysOfWeek>\n"
1765 "<WeeksInterval>1</WeeksInterval>\n"
1766 "</ScheduleByWeek>\n");
1767 break;
1769 case SCHEDULE_WEEKLY:
1770 fprintf(tfile->fp,
1771 "<StartBoundary>2020-01-01T00:00:00</StartBoundary>\n"
1772 "<Enabled>true</Enabled>\n"
1773 "<ScheduleByWeek>\n"
1774 "<DaysOfWeek>\n"
1775 "<Sunday />\n"
1776 "</DaysOfWeek>\n"
1777 "<WeeksInterval>1</WeeksInterval>\n"
1778 "</ScheduleByWeek>\n");
1779 break;
1781 default:
1782 break;
1785 xml = "</CalendarTrigger>\n"
1786 "</Triggers>\n"
1787 "<Principals>\n"
1788 "<Principal id=\"Author\">\n"
1789 "<LogonType>InteractiveToken</LogonType>\n"
1790 "<RunLevel>LeastPrivilege</RunLevel>\n"
1791 "</Principal>\n"
1792 "</Principals>\n"
1793 "<Settings>\n"
1794 "<MultipleInstancesPolicy>IgnoreNew</MultipleInstancesPolicy>\n"
1795 "<Enabled>true</Enabled>\n"
1796 "<Hidden>true</Hidden>\n"
1797 "<UseUnifiedSchedulingEngine>true</UseUnifiedSchedulingEngine>\n"
1798 "<WakeToRun>false</WakeToRun>\n"
1799 "<ExecutionTimeLimit>PT72H</ExecutionTimeLimit>\n"
1800 "<Priority>7</Priority>\n"
1801 "</Settings>\n"
1802 "<Actions Context=\"Author\">\n"
1803 "<Exec>\n"
1804 "<Command>\"%s\\git.exe\"</Command>\n"
1805 "<Arguments>--exec-path=\"%s\" for-each-repo --config=maintenance.repo maintenance run --schedule=%s</Arguments>\n"
1806 "</Exec>\n"
1807 "</Actions>\n"
1808 "</Task>\n";
1809 fprintf(tfile->fp, xml, exec_path, exec_path, frequency);
1810 strvec_split(&child.args, cmd);
1811 strvec_pushl(&child.args, "/create", "/tn", name, "/f", "/xml",
1812 get_tempfile_path(tfile), NULL);
1813 close_tempfile_gently(tfile);
1815 child.no_stdout = 1;
1816 child.no_stderr = 1;
1818 if (start_command(&child))
1819 die(_("failed to start schtasks"));
1820 result = finish_command(&child);
1822 delete_tempfile(&tfile);
1823 free(name);
1824 return result;
1827 static int schtasks_schedule_tasks(const char *cmd)
1829 const char *exec_path = git_exec_path();
1831 return schtasks_schedule_task(exec_path, SCHEDULE_HOURLY, cmd) ||
1832 schtasks_schedule_task(exec_path, SCHEDULE_DAILY, cmd) ||
1833 schtasks_schedule_task(exec_path, SCHEDULE_WEEKLY, cmd);
1836 static int schtasks_update_schedule(int run_maintenance, int fd, const char *cmd)
1838 if (run_maintenance)
1839 return schtasks_schedule_tasks(cmd);
1840 else
1841 return schtasks_remove_tasks(cmd);
1844 #define BEGIN_LINE "# BEGIN GIT MAINTENANCE SCHEDULE"
1845 #define END_LINE "# END GIT MAINTENANCE SCHEDULE"
1847 static int crontab_update_schedule(int run_maintenance, int fd, const char *cmd)
1849 int result = 0;
1850 int in_old_region = 0;
1851 struct child_process crontab_list = CHILD_PROCESS_INIT;
1852 struct child_process crontab_edit = CHILD_PROCESS_INIT;
1853 FILE *cron_list, *cron_in;
1854 struct strbuf line = STRBUF_INIT;
1856 strvec_split(&crontab_list.args, cmd);
1857 strvec_push(&crontab_list.args, "-l");
1858 crontab_list.in = -1;
1859 crontab_list.out = dup(fd);
1860 crontab_list.git_cmd = 0;
1862 if (start_command(&crontab_list))
1863 return error(_("failed to run 'crontab -l'; your system might not support 'cron'"));
1865 /* Ignore exit code, as an empty crontab will return error. */
1866 finish_command(&crontab_list);
1869 * Read from the .lock file, filtering out the old
1870 * schedule while appending the new schedule.
1872 cron_list = fdopen(fd, "r");
1873 rewind(cron_list);
1875 strvec_split(&crontab_edit.args, cmd);
1876 crontab_edit.in = -1;
1877 crontab_edit.git_cmd = 0;
1879 if (start_command(&crontab_edit))
1880 return error(_("failed to run 'crontab'; your system might not support 'cron'"));
1882 cron_in = fdopen(crontab_edit.in, "w");
1883 if (!cron_in) {
1884 result = error(_("failed to open stdin of 'crontab'"));
1885 goto done_editing;
1888 while (!strbuf_getline_lf(&line, cron_list)) {
1889 if (!in_old_region && !strcmp(line.buf, BEGIN_LINE))
1890 in_old_region = 1;
1891 else if (in_old_region && !strcmp(line.buf, END_LINE))
1892 in_old_region = 0;
1893 else if (!in_old_region)
1894 fprintf(cron_in, "%s\n", line.buf);
1897 if (run_maintenance) {
1898 struct strbuf line_format = STRBUF_INIT;
1899 const char *exec_path = git_exec_path();
1901 fprintf(cron_in, "%s\n", BEGIN_LINE);
1902 fprintf(cron_in,
1903 "# The following schedule was created by Git\n");
1904 fprintf(cron_in, "# Any edits made in this region might be\n");
1905 fprintf(cron_in,
1906 "# replaced in the future by a Git command.\n\n");
1908 strbuf_addf(&line_format,
1909 "%%s %%s * * %%s \"%s/git\" --exec-path=\"%s\" for-each-repo --config=maintenance.repo maintenance run --schedule=%%s\n",
1910 exec_path, exec_path);
1911 fprintf(cron_in, line_format.buf, "0", "1-23", "*", "hourly");
1912 fprintf(cron_in, line_format.buf, "0", "0", "1-6", "daily");
1913 fprintf(cron_in, line_format.buf, "0", "0", "0", "weekly");
1914 strbuf_release(&line_format);
1916 fprintf(cron_in, "\n%s\n", END_LINE);
1919 fflush(cron_in);
1920 fclose(cron_in);
1921 close(crontab_edit.in);
1923 done_editing:
1924 if (finish_command(&crontab_edit))
1925 result = error(_("'crontab' died"));
1926 else
1927 fclose(cron_list);
1928 return result;
1931 #if defined(__APPLE__)
1932 static const char platform_scheduler[] = "launchctl";
1933 #elif defined(GIT_WINDOWS_NATIVE)
1934 static const char platform_scheduler[] = "schtasks";
1935 #else
1936 static const char platform_scheduler[] = "crontab";
1937 #endif
1939 static int update_background_schedule(int enable)
1941 int result;
1942 const char *scheduler = platform_scheduler;
1943 const char *cmd = scheduler;
1944 char *testing;
1945 struct lock_file lk;
1946 char *lock_path = xstrfmt("%s/schedule", the_repository->objects->odb->path);
1948 testing = xstrdup_or_null(getenv("GIT_TEST_MAINT_SCHEDULER"));
1949 if (testing) {
1950 char *sep = strchr(testing, ':');
1951 if (!sep)
1952 die("GIT_TEST_MAINT_SCHEDULER unparseable: %s", testing);
1953 *sep = '\0';
1954 scheduler = testing;
1955 cmd = sep + 1;
1958 if (hold_lock_file_for_update(&lk, lock_path, LOCK_NO_DEREF) < 0)
1959 return error(_("another process is scheduling background maintenance"));
1961 if (!strcmp(scheduler, "launchctl"))
1962 result = launchctl_update_schedule(enable, get_lock_file_fd(&lk), cmd);
1963 else if (!strcmp(scheduler, "schtasks"))
1964 result = schtasks_update_schedule(enable, get_lock_file_fd(&lk), cmd);
1965 else if (!strcmp(scheduler, "crontab"))
1966 result = crontab_update_schedule(enable, get_lock_file_fd(&lk), cmd);
1967 else
1968 die("unknown background scheduler: %s", scheduler);
1970 rollback_lock_file(&lk);
1971 free(testing);
1972 return result;
1975 static int maintenance_start(void)
1977 if (maintenance_register())
1978 warning(_("failed to add repo to global config"));
1980 return update_background_schedule(1);
1983 static int maintenance_stop(void)
1985 return update_background_schedule(0);
1988 static const char builtin_maintenance_usage[] = N_("git maintenance <subcommand> [<options>]");
1990 int cmd_maintenance(int argc, const char **argv, const char *prefix)
1992 if (argc < 2 ||
1993 (argc == 2 && !strcmp(argv[1], "-h")))
1994 usage(builtin_maintenance_usage);
1996 if (!strcmp(argv[1], "run"))
1997 return maintenance_run(argc - 1, argv + 1, prefix);
1998 if (!strcmp(argv[1], "start"))
1999 return maintenance_start();
2000 if (!strcmp(argv[1], "stop"))
2001 return maintenance_stop();
2002 if (!strcmp(argv[1], "register"))
2003 return maintenance_register();
2004 if (!strcmp(argv[1], "unregister"))
2005 return maintenance_unregister();
2007 die(_("invalid subcommand: %s"), argv[1]);