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
16 #include "environment.h"
18 #include "repository.h"
22 #include "parse-options.h"
23 #include "run-command.h"
27 #include "commit-graph.h"
29 #include "object-file.h"
30 #include "object-store-ll.h"
32 #include "pack-objects.h"
36 #include "promisor-remote.h"
45 #define FAILED_RUN "failed to run %s"
47 static const char * const builtin_gc_usage
[] = {
48 N_("git gc [<options>]"),
52 static int pack_refs
= 1;
53 static int prune_reflogs
= 1;
54 static int cruft_packs
= 1;
55 static unsigned long max_cruft_size
;
56 static int aggressive_depth
= 50;
57 static int aggressive_window
= 250;
58 static int gc_auto_threshold
= 6700;
59 static int gc_auto_pack_limit
= 50;
60 static int detach_auto
= 1;
61 static timestamp_t gc_log_expire_time
;
62 static const char *gc_log_expire
= "1.day.ago";
63 static const char *prune_expire
= "2.weeks.ago";
64 static const char *prune_worktrees_expire
= "3.months.ago";
65 static char *repack_filter
;
66 static char *repack_filter_to
;
67 static unsigned long big_pack_threshold
;
68 static unsigned long max_delta_cache_size
= DEFAULT_DELTA_CACHE_SIZE
;
70 static struct strvec reflog
= STRVEC_INIT
;
71 static struct strvec repack
= STRVEC_INIT
;
72 static struct strvec prune
= STRVEC_INIT
;
73 static struct strvec prune_worktrees
= STRVEC_INIT
;
74 static struct strvec rerere
= STRVEC_INIT
;
76 static struct tempfile
*pidfile
;
77 static struct lock_file log_lock
;
79 static struct string_list pack_garbage
= STRING_LIST_INIT_DUP
;
81 static void clean_pack_garbage(void)
84 for (i
= 0; i
< pack_garbage
.nr
; i
++)
85 unlink_or_warn(pack_garbage
.items
[i
].string
);
86 string_list_clear(&pack_garbage
, 0);
89 static void report_pack_garbage(unsigned seen_bits
, const char *path
)
91 if (seen_bits
== PACKDIR_FILE_IDX
)
92 string_list_append(&pack_garbage
, path
);
95 static void process_log_file(void)
98 if (fstat(get_lock_file_fd(&log_lock
), &st
)) {
100 * Perhaps there was an i/o error or another
101 * unlikely situation. Try to make a note of
102 * this in gc.log along with any existing
105 int saved_errno
= errno
;
106 fprintf(stderr
, _("Failed to fstat %s: %s"),
107 get_lock_file_path(&log_lock
),
108 strerror(saved_errno
));
110 commit_lock_file(&log_lock
);
112 } else if (st
.st_size
) {
113 /* There was some error recorded in the lock file */
114 commit_lock_file(&log_lock
);
116 /* No error, clean up any old gc.log */
117 unlink(git_path("gc.log"));
118 rollback_lock_file(&log_lock
);
122 static void process_log_file_at_exit(void)
128 static void process_log_file_on_signal(int signo
)
135 static int gc_config_is_timestamp_never(const char *var
)
140 if (!git_config_get_value(var
, &value
) && value
) {
141 if (parse_expiry_date(value
, &expire
))
142 die(_("failed to parse '%s' value '%s'"), var
, value
);
148 static void gc_config(void)
152 if (!git_config_get_value("gc.packrefs", &value
)) {
153 if (value
&& !strcmp(value
, "notbare"))
156 pack_refs
= git_config_bool("gc.packrefs", value
);
159 if (gc_config_is_timestamp_never("gc.reflogexpire") &&
160 gc_config_is_timestamp_never("gc.reflogexpireunreachable"))
163 git_config_get_int("gc.aggressivewindow", &aggressive_window
);
164 git_config_get_int("gc.aggressivedepth", &aggressive_depth
);
165 git_config_get_int("gc.auto", &gc_auto_threshold
);
166 git_config_get_int("gc.autopacklimit", &gc_auto_pack_limit
);
167 git_config_get_bool("gc.autodetach", &detach_auto
);
168 git_config_get_bool("gc.cruftpacks", &cruft_packs
);
169 git_config_get_ulong("gc.maxcruftsize", &max_cruft_size
);
170 git_config_get_expiry("gc.pruneexpire", &prune_expire
);
171 git_config_get_expiry("gc.worktreepruneexpire", &prune_worktrees_expire
);
172 git_config_get_expiry("gc.logexpiry", &gc_log_expire
);
174 git_config_get_ulong("gc.bigpackthreshold", &big_pack_threshold
);
175 git_config_get_ulong("pack.deltacachesize", &max_delta_cache_size
);
177 git_config_get_string("gc.repackfilter", &repack_filter
);
178 git_config_get_string("gc.repackfilterto", &repack_filter_to
);
180 git_config(git_default_config
, NULL
);
183 enum schedule_priority
{
190 static enum schedule_priority
parse_schedule(const char *value
)
193 return SCHEDULE_NONE
;
194 if (!strcasecmp(value
, "hourly"))
195 return SCHEDULE_HOURLY
;
196 if (!strcasecmp(value
, "daily"))
197 return SCHEDULE_DAILY
;
198 if (!strcasecmp(value
, "weekly"))
199 return SCHEDULE_WEEKLY
;
200 return SCHEDULE_NONE
;
203 struct maintenance_run_opts
{
206 enum schedule_priority schedule
;
209 static int pack_refs_condition(void)
212 * The auto-repacking logic for refs is handled by the ref backends and
213 * exposed via `git pack-refs --auto`. We thus always return truish
214 * here and let the backend decide for us.
219 static int maintenance_task_pack_refs(MAYBE_UNUSED
struct maintenance_run_opts
*opts
)
221 struct child_process cmd
= CHILD_PROCESS_INIT
;
224 strvec_pushl(&cmd
.args
, "pack-refs", "--all", "--prune", NULL
);
226 strvec_push(&cmd
.args
, "--auto");
228 return run_command(&cmd
);
231 static int too_many_loose_objects(void)
234 * Quickly check if a "gc" is needed, by estimating how
235 * many loose objects there are. Because SHA-1 is evenly
236 * distributed, we can check only one and get a reasonable
244 const unsigned hexsz_loose
= the_hash_algo
->hexsz
- 2;
246 dir
= opendir(git_path("objects/17"));
250 auto_threshold
= DIV_ROUND_UP(gc_auto_threshold
, 256);
251 while ((ent
= readdir(dir
)) != NULL
) {
252 if (strspn(ent
->d_name
, "0123456789abcdef") != hexsz_loose
||
253 ent
->d_name
[hexsz_loose
] != '\0')
255 if (++num_loose
> auto_threshold
) {
264 static struct packed_git
*find_base_packs(struct string_list
*packs
,
267 struct packed_git
*p
, *base
= NULL
;
269 for (p
= get_all_packs(the_repository
); p
; p
= p
->next
) {
270 if (!p
->pack_local
|| p
->is_cruft
)
273 if (p
->pack_size
>= limit
)
274 string_list_append(packs
, p
->pack_name
);
275 } else if (!base
|| base
->pack_size
< p
->pack_size
) {
281 string_list_append(packs
, base
->pack_name
);
286 static int too_many_packs(void)
288 struct packed_git
*p
;
291 if (gc_auto_pack_limit
<= 0)
294 for (cnt
= 0, p
= get_all_packs(the_repository
); p
; p
= p
->next
) {
300 * Perhaps check the size of the pack and count only
301 * very small ones here?
305 return gc_auto_pack_limit
< cnt
;
308 static uint64_t total_ram(void)
310 #if defined(HAVE_SYSINFO)
315 #elif defined(HAVE_BSD_SYSCTL) && (defined(HW_MEMSIZE) || defined(HW_PHYSMEM))
316 int64_t physical_memory
;
321 # if defined(HW_MEMSIZE)
326 length
= sizeof(int64_t);
327 if (!sysctl(mib
, 2, &physical_memory
, &length
, NULL
, 0))
328 return physical_memory
;
329 #elif defined(GIT_WINDOWS_NATIVE)
330 MEMORYSTATUSEX memInfo
;
332 memInfo
.dwLength
= sizeof(MEMORYSTATUSEX
);
333 if (GlobalMemoryStatusEx(&memInfo
))
334 return memInfo
.ullTotalPhys
;
339 static uint64_t estimate_repack_memory(struct packed_git
*pack
)
341 unsigned long nr_objects
= repo_approximate_object_count(the_repository
);
342 size_t os_cache
, heap
;
344 if (!pack
|| !nr_objects
)
348 * First we have to scan through at least one pack.
349 * Assume enough room in OS file cache to keep the entire pack
350 * or we may accidentally evict data of other processes from
353 os_cache
= pack
->pack_size
+ pack
->index_size
;
354 /* then pack-objects needs lots more for book keeping */
355 heap
= sizeof(struct object_entry
) * nr_objects
;
357 * internal rev-list --all --objects takes up some memory too,
358 * let's say half of it is for blobs
360 heap
+= sizeof(struct blob
) * nr_objects
/ 2;
362 * and the other half is for trees (commits and tags are
363 * usually insignificant)
365 heap
+= sizeof(struct tree
) * nr_objects
/ 2;
366 /* and then obj_hash[], underestimated in fact */
367 heap
+= sizeof(struct object
*) * nr_objects
;
368 /* revindex is used also */
369 heap
+= (sizeof(off_t
) + sizeof(uint32_t)) * nr_objects
;
371 * read_sha1_file() (either at delta calculation phase, or
372 * writing phase) also fills up the delta base cache
374 heap
+= delta_base_cache_limit
;
375 /* and of course pack-objects has its own delta cache */
376 heap
+= max_delta_cache_size
;
378 return os_cache
+ heap
;
381 static int keep_one_pack(struct string_list_item
*item
, void *data UNUSED
)
383 strvec_pushf(&repack
, "--keep-pack=%s", basename(item
->string
));
387 static void add_repack_all_option(struct string_list
*keep_pack
)
389 if (prune_expire
&& !strcmp(prune_expire
, "now"))
390 strvec_push(&repack
, "-a");
391 else if (cruft_packs
) {
392 strvec_push(&repack
, "--cruft");
394 strvec_pushf(&repack
, "--cruft-expiration=%s", prune_expire
);
396 strvec_pushf(&repack
, "--max-cruft-size=%lu",
399 strvec_push(&repack
, "-A");
401 strvec_pushf(&repack
, "--unpack-unreachable=%s", prune_expire
);
405 for_each_string_list(keep_pack
, keep_one_pack
, NULL
);
407 if (repack_filter
&& *repack_filter
)
408 strvec_pushf(&repack
, "--filter=%s", repack_filter
);
409 if (repack_filter_to
&& *repack_filter_to
)
410 strvec_pushf(&repack
, "--filter-to=%s", repack_filter_to
);
413 static void add_repack_incremental_option(void)
415 strvec_push(&repack
, "--no-write-bitmap-index");
418 static int need_to_gc(void)
421 * Setting gc.auto to 0 or negative can disable the
424 if (gc_auto_threshold
<= 0)
428 * If there are too many loose objects, but not too many
429 * packs, we run "repack -d -l". If there are too many packs,
430 * we run "repack -A -d -l". Otherwise we tell the caller
433 if (too_many_packs()) {
434 struct string_list keep_pack
= STRING_LIST_INIT_NODUP
;
436 if (big_pack_threshold
) {
437 find_base_packs(&keep_pack
, big_pack_threshold
);
438 if (keep_pack
.nr
>= gc_auto_pack_limit
) {
439 big_pack_threshold
= 0;
440 string_list_clear(&keep_pack
, 0);
441 find_base_packs(&keep_pack
, 0);
444 struct packed_git
*p
= find_base_packs(&keep_pack
, 0);
445 uint64_t mem_have
, mem_want
;
447 mem_have
= total_ram();
448 mem_want
= estimate_repack_memory(p
);
451 * Only allow 1/2 of memory for pack-objects, leave
452 * the rest for the OS and other processes in the
455 if (!mem_have
|| mem_want
< mem_have
/ 2)
456 string_list_clear(&keep_pack
, 0);
459 add_repack_all_option(&keep_pack
);
460 string_list_clear(&keep_pack
, 0);
461 } else if (too_many_loose_objects())
462 add_repack_incremental_option();
466 if (run_hooks("pre-auto-gc"))
471 /* return NULL on success, else hostname running the gc */
472 static const char *lock_repo_for_gc(int force
, pid_t
* ret_pid
)
474 struct lock_file lock
= LOCK_INIT
;
475 char my_host
[HOST_NAME_MAX
+ 1];
476 struct strbuf sb
= STRBUF_INIT
;
483 if (is_tempfile_active(pidfile
))
487 if (xgethostname(my_host
, sizeof(my_host
)))
488 xsnprintf(my_host
, sizeof(my_host
), "unknown");
490 pidfile_path
= git_pathdup("gc.pid");
491 fd
= hold_lock_file_for_update(&lock
, pidfile_path
,
494 static char locking_host
[HOST_NAME_MAX
+ 1];
495 static char *scan_fmt
;
499 scan_fmt
= xstrfmt("%s %%%ds", "%"SCNuMAX
, HOST_NAME_MAX
);
500 fp
= fopen(pidfile_path
, "r");
501 memset(locking_host
, 0, sizeof(locking_host
));
504 !fstat(fileno(fp
), &st
) &&
506 * 12 hour limit is very generous as gc should
507 * never take that long. On the other hand we
508 * don't really need a strict limit here,
509 * running gc --auto one day late is not a big
510 * problem. --force can be used in manual gc
511 * after the user verifies that no gc is
514 time(NULL
) - st
.st_mtime
<= 12 * 3600 &&
515 fscanf(fp
, scan_fmt
, &pid
, locking_host
) == 2 &&
516 /* be gentle to concurrent "gc" on remote hosts */
517 (strcmp(locking_host
, my_host
) || !kill(pid
, 0) || errno
== EPERM
);
522 rollback_lock_file(&lock
);
529 strbuf_addf(&sb
, "%"PRIuMAX
" %s",
530 (uintmax_t) getpid(), my_host
);
531 write_in_full(fd
, sb
.buf
, sb
.len
);
533 commit_lock_file(&lock
);
534 pidfile
= register_tempfile(pidfile_path
);
540 * Returns 0 if there was no previous error and gc can proceed, 1 if
541 * gc should not proceed due to an error in the last run. Prints a
542 * message and returns with a non-[01] status code if an error occurred
543 * while reading gc.log
545 static int report_last_gc_error(void)
547 struct strbuf sb
= STRBUF_INIT
;
551 char *gc_log_path
= git_pathdup("gc.log");
553 if (stat(gc_log_path
, &st
)) {
557 ret
= die_message_errno(_("cannot stat '%s'"), gc_log_path
);
561 if (st
.st_mtime
< gc_log_expire_time
)
564 len
= strbuf_read_file(&sb
, gc_log_path
, 0);
566 ret
= die_message_errno(_("cannot read '%s'"), gc_log_path
);
569 * A previous gc failed. Report the error, and don't
570 * bother with an automatic gc run since it is likely
571 * to fail in the same way.
573 warning(_("The last gc run reported the following. "
574 "Please correct the root cause\n"
576 "Automatic cleanup will not be performed "
577 "until the file is removed.\n\n"
579 gc_log_path
, sb
.buf
);
588 static void gc_before_repack(struct maintenance_run_opts
*opts
)
591 * We may be called twice, as both the pre- and
592 * post-daemonized phases will call us, but running these
593 * commands more than once is pointless and wasteful.
599 if (pack_refs
&& maintenance_task_pack_refs(opts
))
600 die(FAILED_RUN
, "pack-refs");
603 struct child_process cmd
= CHILD_PROCESS_INIT
;
606 strvec_pushv(&cmd
.args
, reflog
.v
);
607 if (run_command(&cmd
))
608 die(FAILED_RUN
, reflog
.v
[0]);
612 int cmd_gc(int argc
, const char **argv
, const char *prefix
)
620 int keep_largest_pack
= -1;
622 struct child_process rerere_cmd
= CHILD_PROCESS_INIT
;
623 struct maintenance_run_opts opts
= {0};
625 struct option builtin_gc_options
[] = {
626 OPT__QUIET(&quiet
, N_("suppress progress reporting")),
627 { OPTION_STRING
, 0, "prune", &prune_expire
, N_("date"),
628 N_("prune unreferenced objects"),
629 PARSE_OPT_OPTARG
, NULL
, (intptr_t)prune_expire
},
630 OPT_BOOL(0, "cruft", &cruft_packs
, N_("pack unreferenced objects separately")),
631 OPT_MAGNITUDE(0, "max-cruft-size", &max_cruft_size
,
632 N_("with --cruft, limit the size of new cruft packs")),
633 OPT_BOOL(0, "aggressive", &aggressive
, N_("be more thorough (increased runtime)")),
634 OPT_BOOL_F(0, "auto", &opts
.auto_flag
, N_("enable auto-gc mode"),
635 PARSE_OPT_NOCOMPLETE
),
636 OPT_BOOL_F(0, "force", &force
,
637 N_("force running gc even if there may be another gc running"),
638 PARSE_OPT_NOCOMPLETE
),
639 OPT_BOOL(0, "keep-largest-pack", &keep_largest_pack
,
640 N_("repack all other packs except the largest pack")),
644 if (argc
== 2 && !strcmp(argv
[1], "-h"))
645 usage_with_options(builtin_gc_usage
, builtin_gc_options
);
647 strvec_pushl(&reflog
, "reflog", "expire", "--all", NULL
);
648 strvec_pushl(&repack
, "repack", "-d", "-l", NULL
);
649 strvec_pushl(&prune
, "prune", "--expire", NULL
);
650 strvec_pushl(&prune_worktrees
, "worktree", "prune", "--expire", NULL
);
651 strvec_pushl(&rerere
, "rerere", "gc", NULL
);
653 /* default expiry time, overwritten in gc_config */
655 if (parse_expiry_date(gc_log_expire
, &gc_log_expire_time
))
656 die(_("failed to parse gc.logExpiry value %s"), gc_log_expire
);
659 pack_refs
= !is_bare_repository();
661 argc
= parse_options(argc
, argv
, prefix
, builtin_gc_options
,
662 builtin_gc_usage
, 0);
664 usage_with_options(builtin_gc_usage
, builtin_gc_options
);
666 if (prune_expire
&& parse_expiry_date(prune_expire
, &dummy
))
667 die(_("failed to parse prune expiry value %s"), prune_expire
);
670 strvec_push(&repack
, "-f");
671 if (aggressive_depth
> 0)
672 strvec_pushf(&repack
, "--depth=%d", aggressive_depth
);
673 if (aggressive_window
> 0)
674 strvec_pushf(&repack
, "--window=%d", aggressive_window
);
677 strvec_push(&repack
, "-q");
679 if (opts
.auto_flag
) {
681 * Auto-gc should be least intrusive as possible.
687 fprintf(stderr
, _("Auto packing the repository in background for optimum performance.\n"));
689 fprintf(stderr
, _("Auto packing the repository for optimum performance.\n"));
690 fprintf(stderr
, _("See \"git help gc\" for manual housekeeping.\n"));
693 int ret
= report_last_gc_error();
696 /* Last gc --auto failed. Skip this one. */
699 /* an I/O error occurred, already reported */
702 if (lock_repo_for_gc(force
, &pid
))
704 gc_before_repack(&opts
); /* dies on failure */
705 delete_tempfile(&pidfile
);
708 * failure to daemonize is ok, we'll continue
711 daemonized
= !daemonize();
714 struct string_list keep_pack
= STRING_LIST_INIT_NODUP
;
716 if (keep_largest_pack
!= -1) {
717 if (keep_largest_pack
)
718 find_base_packs(&keep_pack
, 0);
719 } else if (big_pack_threshold
) {
720 find_base_packs(&keep_pack
, big_pack_threshold
);
723 add_repack_all_option(&keep_pack
);
724 string_list_clear(&keep_pack
, 0);
727 name
= lock_repo_for_gc(force
, &pid
);
730 return 0; /* be quiet on --auto */
731 die(_("gc is already running on machine '%s' pid %"PRIuMAX
" (use --force if not)"),
732 name
, (uintmax_t)pid
);
736 hold_lock_file_for_update(&log_lock
,
739 dup2(get_lock_file_fd(&log_lock
), 2);
740 sigchain_push_common(process_log_file_on_signal
);
741 atexit(process_log_file_at_exit
);
744 gc_before_repack(&opts
);
746 if (!repository_format_precious_objects
) {
747 struct child_process repack_cmd
= CHILD_PROCESS_INIT
;
749 repack_cmd
.git_cmd
= 1;
750 repack_cmd
.close_object_store
= 1;
751 strvec_pushv(&repack_cmd
.args
, repack
.v
);
752 if (run_command(&repack_cmd
))
753 die(FAILED_RUN
, repack
.v
[0]);
756 struct child_process prune_cmd
= CHILD_PROCESS_INIT
;
758 /* run `git prune` even if using cruft packs */
759 strvec_push(&prune
, prune_expire
);
761 strvec_push(&prune
, "--no-progress");
762 if (repo_has_promisor_remote(the_repository
))
764 "--exclude-promisor-objects");
765 prune_cmd
.git_cmd
= 1;
766 strvec_pushv(&prune_cmd
.args
, prune
.v
);
767 if (run_command(&prune_cmd
))
768 die(FAILED_RUN
, prune
.v
[0]);
772 if (prune_worktrees_expire
) {
773 struct child_process prune_worktrees_cmd
= CHILD_PROCESS_INIT
;
775 strvec_push(&prune_worktrees
, prune_worktrees_expire
);
776 prune_worktrees_cmd
.git_cmd
= 1;
777 strvec_pushv(&prune_worktrees_cmd
.args
, prune_worktrees
.v
);
778 if (run_command(&prune_worktrees_cmd
))
779 die(FAILED_RUN
, prune_worktrees
.v
[0]);
782 rerere_cmd
.git_cmd
= 1;
783 strvec_pushv(&rerere_cmd
.args
, rerere
.v
);
784 if (run_command(&rerere_cmd
))
785 die(FAILED_RUN
, rerere
.v
[0]);
787 report_garbage
= report_pack_garbage
;
788 reprepare_packed_git(the_repository
);
789 if (pack_garbage
.nr
> 0) {
790 close_object_store(the_repository
->objects
);
791 clean_pack_garbage();
794 if (the_repository
->settings
.gc_write_commit_graph
== 1)
795 write_commit_graph_reachable(the_repository
->objects
->odb
,
796 !quiet
&& !daemonized
? COMMIT_GRAPH_WRITE_PROGRESS
: 0,
799 if (opts
.auto_flag
&& too_many_loose_objects())
800 warning(_("There are too many unreachable loose objects; "
801 "run 'git prune' to remove them."));
804 unlink(git_path("gc.log"));
809 static const char *const builtin_maintenance_run_usage
[] = {
810 N_("git maintenance run [--auto] [--[no-]quiet] [--task=<task>] [--schedule]"),
814 static int maintenance_opt_schedule(const struct option
*opt
, const char *arg
,
817 enum schedule_priority
*priority
= opt
->value
;
820 die(_("--no-schedule is not allowed"));
822 *priority
= parse_schedule(arg
);
825 die(_("unrecognized --schedule argument '%s'"), arg
);
830 /* Remember to update object flag allocation in object.h */
833 struct cg_auto_data
{
834 int num_not_in_graph
;
838 static int dfs_on_ref(const char *refname UNUSED
,
839 const struct object_id
*oid
,
843 struct cg_auto_data
*data
= (struct cg_auto_data
*)cb_data
;
845 struct object_id peeled
;
846 struct commit_list
*stack
= NULL
;
847 struct commit
*commit
;
849 if (!peel_iterated_oid(oid
, &peeled
))
851 if (oid_object_info(the_repository
, oid
, NULL
) != OBJ_COMMIT
)
854 commit
= lookup_commit(the_repository
, oid
);
857 if (repo_parse_commit(the_repository
, commit
) ||
858 commit_graph_position(commit
) != COMMIT_NOT_FROM_GRAPH
)
861 data
->num_not_in_graph
++;
863 if (data
->num_not_in_graph
>= data
->limit
)
866 commit_list_append(commit
, &stack
);
868 while (!result
&& stack
) {
869 struct commit_list
*parent
;
871 commit
= pop_commit(&stack
);
873 for (parent
= commit
->parents
; parent
; parent
= parent
->next
) {
874 if (repo_parse_commit(the_repository
, parent
->item
) ||
875 commit_graph_position(parent
->item
) != COMMIT_NOT_FROM_GRAPH
||
876 parent
->item
->object
.flags
& SEEN
)
879 parent
->item
->object
.flags
|= SEEN
;
880 data
->num_not_in_graph
++;
882 if (data
->num_not_in_graph
>= data
->limit
) {
887 commit_list_append(parent
->item
, &stack
);
891 free_commit_list(stack
);
895 static int should_write_commit_graph(void)
898 struct cg_auto_data data
;
900 data
.num_not_in_graph
= 0;
902 git_config_get_int("maintenance.commit-graph.auto",
910 result
= refs_for_each_ref(get_main_ref_store(the_repository
),
913 repo_clear_commit_marks(the_repository
, SEEN
);
918 static int run_write_commit_graph(struct maintenance_run_opts
*opts
)
920 struct child_process child
= CHILD_PROCESS_INIT
;
922 child
.git_cmd
= child
.close_object_store
= 1;
923 strvec_pushl(&child
.args
, "commit-graph", "write",
924 "--split", "--reachable", NULL
);
927 strvec_push(&child
.args
, "--no-progress");
929 return !!run_command(&child
);
932 static int maintenance_task_commit_graph(struct maintenance_run_opts
*opts
)
934 prepare_repo_settings(the_repository
);
935 if (!the_repository
->settings
.core_commit_graph
)
938 if (run_write_commit_graph(opts
)) {
939 error(_("failed to write commit-graph"));
946 static int fetch_remote(struct remote
*remote
, void *cbdata
)
948 struct maintenance_run_opts
*opts
= cbdata
;
949 struct child_process child
= CHILD_PROCESS_INIT
;
951 if (remote
->skip_default_update
)
955 strvec_pushl(&child
.args
, "fetch", remote
->name
,
956 "--prefetch", "--prune", "--no-tags",
957 "--no-write-fetch-head", "--recurse-submodules=no",
961 strvec_push(&child
.args
, "--quiet");
963 return !!run_command(&child
);
966 static int maintenance_task_prefetch(struct maintenance_run_opts
*opts
)
968 if (for_each_remote(fetch_remote
, opts
)) {
969 error(_("failed to prefetch remotes"));
976 static int maintenance_task_gc(struct maintenance_run_opts
*opts
)
978 struct child_process child
= CHILD_PROCESS_INIT
;
980 child
.git_cmd
= child
.close_object_store
= 1;
981 strvec_push(&child
.args
, "gc");
984 strvec_push(&child
.args
, "--auto");
986 strvec_push(&child
.args
, "--quiet");
988 strvec_push(&child
.args
, "--no-quiet");
990 return run_command(&child
);
993 static int prune_packed(struct maintenance_run_opts
*opts
)
995 struct child_process child
= CHILD_PROCESS_INIT
;
998 strvec_push(&child
.args
, "prune-packed");
1001 strvec_push(&child
.args
, "--quiet");
1003 return !!run_command(&child
);
1006 struct write_loose_object_data
{
1012 static int loose_object_auto_limit
= 100;
1014 static int loose_object_count(const struct object_id
*oid UNUSED
,
1015 const char *path UNUSED
,
1018 int *count
= (int*)data
;
1019 if (++(*count
) >= loose_object_auto_limit
)
1024 static int loose_object_auto_condition(void)
1028 git_config_get_int("maintenance.loose-objects.auto",
1029 &loose_object_auto_limit
);
1031 if (!loose_object_auto_limit
)
1033 if (loose_object_auto_limit
< 0)
1036 return for_each_loose_file_in_objdir(the_repository
->objects
->odb
->path
,
1038 NULL
, NULL
, &count
);
1041 static int bail_on_loose(const struct object_id
*oid UNUSED
,
1042 const char *path UNUSED
,
1048 static int write_loose_object_to_stdin(const struct object_id
*oid
,
1049 const char *path UNUSED
,
1052 struct write_loose_object_data
*d
= (struct write_loose_object_data
*)data
;
1054 fprintf(d
->in
, "%s\n", oid_to_hex(oid
));
1056 return ++(d
->count
) > d
->batch_size
;
1059 static int pack_loose(struct maintenance_run_opts
*opts
)
1061 struct repository
*r
= the_repository
;
1063 struct write_loose_object_data data
;
1064 struct child_process pack_proc
= CHILD_PROCESS_INIT
;
1067 * Do not start pack-objects process
1068 * if there are no loose objects.
1070 if (!for_each_loose_file_in_objdir(r
->objects
->odb
->path
,
1075 pack_proc
.git_cmd
= 1;
1077 strvec_push(&pack_proc
.args
, "pack-objects");
1079 strvec_push(&pack_proc
.args
, "--quiet");
1080 strvec_pushf(&pack_proc
.args
, "%s/pack/loose", r
->objects
->odb
->path
);
1084 if (start_command(&pack_proc
)) {
1085 error(_("failed to start 'git pack-objects' process"));
1089 data
.in
= xfdopen(pack_proc
.in
, "w");
1091 data
.batch_size
= 50000;
1093 for_each_loose_file_in_objdir(r
->objects
->odb
->path
,
1094 write_loose_object_to_stdin
,
1101 if (finish_command(&pack_proc
)) {
1102 error(_("failed to finish 'git pack-objects' process"));
1109 static int maintenance_task_loose_objects(struct maintenance_run_opts
*opts
)
1111 return prune_packed(opts
) || pack_loose(opts
);
1114 static int incremental_repack_auto_condition(void)
1116 struct packed_git
*p
;
1117 int incremental_repack_auto_limit
= 10;
1120 prepare_repo_settings(the_repository
);
1121 if (!the_repository
->settings
.core_multi_pack_index
)
1124 git_config_get_int("maintenance.incremental-repack.auto",
1125 &incremental_repack_auto_limit
);
1127 if (!incremental_repack_auto_limit
)
1129 if (incremental_repack_auto_limit
< 0)
1132 for (p
= get_packed_git(the_repository
);
1133 count
< incremental_repack_auto_limit
&& p
;
1135 if (!p
->multi_pack_index
)
1139 return count
>= incremental_repack_auto_limit
;
1142 static int multi_pack_index_write(struct maintenance_run_opts
*opts
)
1144 struct child_process child
= CHILD_PROCESS_INIT
;
1147 strvec_pushl(&child
.args
, "multi-pack-index", "write", NULL
);
1150 strvec_push(&child
.args
, "--no-progress");
1152 if (run_command(&child
))
1153 return error(_("failed to write multi-pack-index"));
1158 static int multi_pack_index_expire(struct maintenance_run_opts
*opts
)
1160 struct child_process child
= CHILD_PROCESS_INIT
;
1162 child
.git_cmd
= child
.close_object_store
= 1;
1163 strvec_pushl(&child
.args
, "multi-pack-index", "expire", NULL
);
1166 strvec_push(&child
.args
, "--no-progress");
1168 if (run_command(&child
))
1169 return error(_("'git multi-pack-index expire' failed"));
1174 #define TWO_GIGABYTES (INT32_MAX)
1176 static off_t
get_auto_pack_size(void)
1179 * The "auto" value is special: we optimize for
1180 * one large pack-file (i.e. from a clone) and
1181 * expect the rest to be small and they can be
1184 * The strategy we select here is to select a
1185 * size that is one more than the second largest
1186 * pack-file. This ensures that we will repack
1187 * at least two packs if there are three or more
1191 off_t second_largest_size
= 0;
1193 struct packed_git
*p
;
1194 struct repository
*r
= the_repository
;
1196 reprepare_packed_git(r
);
1197 for (p
= get_all_packs(r
); p
; p
= p
->next
) {
1198 if (p
->pack_size
> max_size
) {
1199 second_largest_size
= max_size
;
1200 max_size
= p
->pack_size
;
1201 } else if (p
->pack_size
> second_largest_size
)
1202 second_largest_size
= p
->pack_size
;
1205 result_size
= second_largest_size
+ 1;
1207 /* But limit ourselves to a batch size of 2g */
1208 if (result_size
> TWO_GIGABYTES
)
1209 result_size
= TWO_GIGABYTES
;
1214 static int multi_pack_index_repack(struct maintenance_run_opts
*opts
)
1216 struct child_process child
= CHILD_PROCESS_INIT
;
1218 child
.git_cmd
= child
.close_object_store
= 1;
1219 strvec_pushl(&child
.args
, "multi-pack-index", "repack", NULL
);
1222 strvec_push(&child
.args
, "--no-progress");
1224 strvec_pushf(&child
.args
, "--batch-size=%"PRIuMAX
,
1225 (uintmax_t)get_auto_pack_size());
1227 if (run_command(&child
))
1228 return error(_("'git multi-pack-index repack' failed"));
1233 static int maintenance_task_incremental_repack(struct maintenance_run_opts
*opts
)
1235 prepare_repo_settings(the_repository
);
1236 if (!the_repository
->settings
.core_multi_pack_index
) {
1237 warning(_("skipping incremental-repack task because core.multiPackIndex is disabled"));
1241 if (multi_pack_index_write(opts
))
1243 if (multi_pack_index_expire(opts
))
1245 if (multi_pack_index_repack(opts
))
1250 typedef int maintenance_task_fn(struct maintenance_run_opts
*opts
);
1253 * An auto condition function returns 1 if the task should run
1254 * and 0 if the task should NOT run. See needs_to_gc() for an
1257 typedef int maintenance_auto_fn(void);
1259 struct maintenance_task
{
1261 maintenance_task_fn
*fn
;
1262 maintenance_auto_fn
*auto_condition
;
1265 enum schedule_priority schedule
;
1267 /* -1 if not selected. */
1271 enum maintenance_task_label
{
1274 TASK_INCREMENTAL_REPACK
,
1279 /* Leave as final value */
1283 static struct maintenance_task tasks
[] = {
1286 maintenance_task_prefetch
,
1288 [TASK_LOOSE_OBJECTS
] = {
1290 maintenance_task_loose_objects
,
1291 loose_object_auto_condition
,
1293 [TASK_INCREMENTAL_REPACK
] = {
1294 "incremental-repack",
1295 maintenance_task_incremental_repack
,
1296 incremental_repack_auto_condition
,
1300 maintenance_task_gc
,
1304 [TASK_COMMIT_GRAPH
] = {
1306 maintenance_task_commit_graph
,
1307 should_write_commit_graph
,
1309 [TASK_PACK_REFS
] = {
1311 maintenance_task_pack_refs
,
1312 pack_refs_condition
,
1316 static int compare_tasks_by_selection(const void *a_
, const void *b_
)
1318 const struct maintenance_task
*a
= a_
;
1319 const struct maintenance_task
*b
= b_
;
1321 return b
->selected_order
- a
->selected_order
;
1324 static int maintenance_run_tasks(struct maintenance_run_opts
*opts
)
1326 int i
, found_selected
= 0;
1328 struct lock_file lk
;
1329 struct repository
*r
= the_repository
;
1330 char *lock_path
= xstrfmt("%s/maintenance", r
->objects
->odb
->path
);
1332 if (hold_lock_file_for_update(&lk
, lock_path
, LOCK_NO_DEREF
) < 0) {
1334 * Another maintenance command is running.
1336 * If --auto was provided, then it is likely due to a
1337 * recursive process stack. Do not report an error in
1340 if (!opts
->auto_flag
&& !opts
->quiet
)
1341 warning(_("lock file '%s' exists, skipping maintenance"),
1348 for (i
= 0; !found_selected
&& i
< TASK__COUNT
; i
++)
1349 found_selected
= tasks
[i
].selected_order
>= 0;
1352 QSORT(tasks
, TASK__COUNT
, compare_tasks_by_selection
);
1354 for (i
= 0; i
< TASK__COUNT
; i
++) {
1355 if (found_selected
&& tasks
[i
].selected_order
< 0)
1358 if (!found_selected
&& !tasks
[i
].enabled
)
1361 if (opts
->auto_flag
&&
1362 (!tasks
[i
].auto_condition
||
1363 !tasks
[i
].auto_condition()))
1366 if (opts
->schedule
&& tasks
[i
].schedule
< opts
->schedule
)
1369 trace2_region_enter("maintenance", tasks
[i
].name
, r
);
1370 if (tasks
[i
].fn(opts
)) {
1371 error(_("task '%s' failed"), tasks
[i
].name
);
1374 trace2_region_leave("maintenance", tasks
[i
].name
, r
);
1377 rollback_lock_file(&lk
);
1381 static void initialize_maintenance_strategy(void)
1385 if (git_config_get_string("maintenance.strategy", &config_str
))
1388 if (!strcasecmp(config_str
, "incremental")) {
1389 tasks
[TASK_GC
].schedule
= SCHEDULE_NONE
;
1390 tasks
[TASK_COMMIT_GRAPH
].enabled
= 1;
1391 tasks
[TASK_COMMIT_GRAPH
].schedule
= SCHEDULE_HOURLY
;
1392 tasks
[TASK_PREFETCH
].enabled
= 1;
1393 tasks
[TASK_PREFETCH
].schedule
= SCHEDULE_HOURLY
;
1394 tasks
[TASK_INCREMENTAL_REPACK
].enabled
= 1;
1395 tasks
[TASK_INCREMENTAL_REPACK
].schedule
= SCHEDULE_DAILY
;
1396 tasks
[TASK_LOOSE_OBJECTS
].enabled
= 1;
1397 tasks
[TASK_LOOSE_OBJECTS
].schedule
= SCHEDULE_DAILY
;
1398 tasks
[TASK_PACK_REFS
].enabled
= 1;
1399 tasks
[TASK_PACK_REFS
].schedule
= SCHEDULE_WEEKLY
;
1403 static void initialize_task_config(int schedule
)
1406 struct strbuf config_name
= STRBUF_INIT
;
1410 initialize_maintenance_strategy();
1412 for (i
= 0; i
< TASK__COUNT
; i
++) {
1416 strbuf_reset(&config_name
);
1417 strbuf_addf(&config_name
, "maintenance.%s.enabled",
1420 if (!git_config_get_bool(config_name
.buf
, &config_value
))
1421 tasks
[i
].enabled
= config_value
;
1423 strbuf_reset(&config_name
);
1424 strbuf_addf(&config_name
, "maintenance.%s.schedule",
1427 if (!git_config_get_string(config_name
.buf
, &config_str
)) {
1428 tasks
[i
].schedule
= parse_schedule(config_str
);
1433 strbuf_release(&config_name
);
1436 static int task_option_parse(const struct option
*opt UNUSED
,
1437 const char *arg
, int unset
)
1439 int i
, num_selected
= 0;
1440 struct maintenance_task
*task
= NULL
;
1442 BUG_ON_OPT_NEG(unset
);
1444 for (i
= 0; i
< TASK__COUNT
; i
++) {
1445 if (tasks
[i
].selected_order
>= 0)
1447 if (!strcasecmp(tasks
[i
].name
, arg
)) {
1453 error(_("'%s' is not a valid task"), arg
);
1457 if (task
->selected_order
>= 0) {
1458 error(_("task '%s' cannot be selected multiple times"), arg
);
1462 task
->selected_order
= num_selected
+ 1;
1467 static int maintenance_run(int argc
, const char **argv
, const char *prefix
)
1470 struct maintenance_run_opts opts
;
1471 struct option builtin_maintenance_run_options
[] = {
1472 OPT_BOOL(0, "auto", &opts
.auto_flag
,
1473 N_("run tasks based on the state of the repository")),
1474 OPT_CALLBACK(0, "schedule", &opts
.schedule
, N_("frequency"),
1475 N_("run tasks based on frequency"),
1476 maintenance_opt_schedule
),
1477 OPT_BOOL(0, "quiet", &opts
.quiet
,
1478 N_("do not report progress or other information over stderr")),
1479 OPT_CALLBACK_F(0, "task", NULL
, N_("task"),
1480 N_("run a specific task"),
1481 PARSE_OPT_NONEG
, task_option_parse
),
1484 memset(&opts
, 0, sizeof(opts
));
1486 opts
.quiet
= !isatty(2);
1488 for (i
= 0; i
< TASK__COUNT
; i
++)
1489 tasks
[i
].selected_order
= -1;
1491 argc
= parse_options(argc
, argv
, prefix
,
1492 builtin_maintenance_run_options
,
1493 builtin_maintenance_run_usage
,
1494 PARSE_OPT_STOP_AT_NON_OPTION
);
1496 if (opts
.auto_flag
&& opts
.schedule
)
1497 die(_("use at most one of --auto and --schedule=<frequency>"));
1499 initialize_task_config(opts
.schedule
);
1502 usage_with_options(builtin_maintenance_run_usage
,
1503 builtin_maintenance_run_options
);
1504 return maintenance_run_tasks(&opts
);
1507 static char *get_maintpath(void)
1509 struct strbuf sb
= STRBUF_INIT
;
1510 const char *p
= the_repository
->worktree
?
1511 the_repository
->worktree
: the_repository
->gitdir
;
1513 strbuf_realpath(&sb
, p
, 1);
1514 return strbuf_detach(&sb
, NULL
);
1517 static char const * const builtin_maintenance_register_usage
[] = {
1518 "git maintenance register [--config-file <path>]",
1522 static int maintenance_register(int argc
, const char **argv
, const char *prefix
)
1524 char *config_file
= NULL
;
1525 struct option options
[] = {
1526 OPT_STRING(0, "config-file", &config_file
, N_("file"), N_("use given config file")),
1530 const char *key
= "maintenance.repo";
1531 char *maintpath
= get_maintpath();
1532 struct string_list_item
*item
;
1533 const struct string_list
*list
;
1535 argc
= parse_options(argc
, argv
, prefix
, options
,
1536 builtin_maintenance_register_usage
, 0);
1538 usage_with_options(builtin_maintenance_register_usage
,
1541 /* Disable foreground maintenance */
1542 git_config_set("maintenance.auto", "false");
1544 /* Set maintenance strategy, if unset */
1545 if (git_config_get("maintenance.strategy"))
1546 git_config_set("maintenance.strategy", "incremental");
1548 if (!git_config_get_string_multi(key
, &list
)) {
1549 for_each_string_list_item(item
, list
) {
1550 if (!strcmp(maintpath
, item
->string
)) {
1559 char *global_config_file
= NULL
;
1562 global_config_file
= git_global_config();
1563 config_file
= global_config_file
;
1566 die(_("$HOME not set"));
1567 rc
= git_config_set_multivar_in_file_gently(
1568 config_file
, "maintenance.repo", maintpath
,
1569 CONFIG_REGEX_NONE
, NULL
, 0);
1570 free(global_config_file
);
1573 die(_("unable to add '%s' value of '%s'"),
1581 static char const * const builtin_maintenance_unregister_usage
[] = {
1582 "git maintenance unregister [--config-file <path>] [--force]",
1586 static int maintenance_unregister(int argc
, const char **argv
, const char *prefix
)
1589 char *config_file
= NULL
;
1590 struct option options
[] = {
1591 OPT_STRING(0, "config-file", &config_file
, N_("file"), N_("use given config file")),
1593 N_("return success even if repository was not registered"),
1594 PARSE_OPT_NOCOMPLETE
),
1597 const char *key
= "maintenance.repo";
1598 char *maintpath
= get_maintpath();
1600 struct string_list_item
*item
;
1601 const struct string_list
*list
;
1602 struct config_set cs
= { { 0 } };
1604 argc
= parse_options(argc
, argv
, prefix
, options
,
1605 builtin_maintenance_unregister_usage
, 0);
1607 usage_with_options(builtin_maintenance_unregister_usage
,
1611 git_configset_init(&cs
);
1612 git_configset_add_file(&cs
, config_file
);
1615 ? git_configset_get_string_multi(&cs
, key
, &list
)
1616 : git_config_get_string_multi(key
, &list
))) {
1617 for_each_string_list_item(item
, list
) {
1618 if (!strcmp(maintpath
, item
->string
)) {
1627 char *global_config_file
= NULL
;
1630 global_config_file
= git_global_config();
1631 config_file
= global_config_file
;
1634 die(_("$HOME not set"));
1635 rc
= git_config_set_multivar_in_file_gently(
1636 config_file
, key
, NULL
, maintpath
, NULL
,
1637 CONFIG_FLAGS_MULTI_REPLACE
| CONFIG_FLAGS_FIXED_VALUE
);
1638 free(global_config_file
);
1641 (!force
|| rc
== CONFIG_NOTHING_SET
))
1642 die(_("unable to unset '%s' value of '%s'"),
1644 } else if (!force
) {
1645 die(_("repository '%s' is not registered"), maintpath
);
1648 git_configset_clear(&cs
);
1653 static const char *get_frequency(enum schedule_priority schedule
)
1656 case SCHEDULE_HOURLY
:
1658 case SCHEDULE_DAILY
:
1660 case SCHEDULE_WEEKLY
:
1663 BUG("invalid schedule %d", schedule
);
1668 * get_schedule_cmd` reads the GIT_TEST_MAINT_SCHEDULER environment variable
1669 * to mock the schedulers that `git maintenance start` rely on.
1671 * For test purpose, GIT_TEST_MAINT_SCHEDULER can be set to a comma-separated
1672 * list of colon-separated key/value pairs where each pair contains a scheduler
1673 * and its corresponding mock.
1675 * * If $GIT_TEST_MAINT_SCHEDULER is not set, return false and leave the
1676 * arguments unmodified.
1678 * * If $GIT_TEST_MAINT_SCHEDULER is set, return true.
1679 * In this case, the *cmd value is read as input.
1681 * * if the input value *cmd is the key of one of the comma-separated list
1682 * item, then *is_available is set to true and *cmd is modified and becomes
1685 * * if the input value *cmd isn’t the key of any of the comma-separated list
1686 * item, then *is_available is set to false.
1689 * GIT_TEST_MAINT_SCHEDULER not set
1690 * +-------+-------------------------------------------------+
1691 * | Input | Output |
1692 * | *cmd | return code | *cmd | *is_available |
1693 * +-------+-------------+-------------------+---------------+
1694 * | "foo" | false | "foo" (unchanged) | (unchanged) |
1695 * +-------+-------------+-------------------+---------------+
1697 * GIT_TEST_MAINT_SCHEDULER set to “foo:./mock_foo.sh,bar:./mock_bar.sh”
1698 * +-------+-------------------------------------------------+
1699 * | Input | Output |
1700 * | *cmd | return code | *cmd | *is_available |
1701 * +-------+-------------+-------------------+---------------+
1702 * | "foo" | true | "./mock.foo.sh" | true |
1703 * | "qux" | true | "qux" (unchanged) | false |
1704 * +-------+-------------+-------------------+---------------+
1706 static int get_schedule_cmd(const char **cmd
, int *is_available
)
1708 char *testing
= xstrdup_or_null(getenv("GIT_TEST_MAINT_SCHEDULER"));
1709 struct string_list_item
*item
;
1710 struct string_list list
= STRING_LIST_INIT_NODUP
;
1718 string_list_split_in_place(&list
, testing
, ",", -1);
1719 for_each_string_list_item(item
, &list
) {
1720 struct string_list pair
= STRING_LIST_INIT_NODUP
;
1722 if (string_list_split_in_place(&pair
, item
->string
, ":", 2) != 2)
1725 if (!strcmp(*cmd
, pair
.items
[0].string
)) {
1726 *cmd
= pair
.items
[1].string
;
1729 string_list_clear(&list
, 0);
1735 string_list_clear(&list
, 0);
1740 static int get_random_minute(void)
1742 /* Use a static value when under tests. */
1743 if (getenv("GIT_TEST_MAINT_SCHEDULER"))
1746 return git_rand() % 60;
1749 static int is_launchctl_available(void)
1751 const char *cmd
= "launchctl";
1753 if (get_schedule_cmd(&cmd
, &is_available
))
1754 return is_available
;
1763 static char *launchctl_service_name(const char *frequency
)
1765 struct strbuf label
= STRBUF_INIT
;
1766 strbuf_addf(&label
, "org.git-scm.git.%s", frequency
);
1767 return strbuf_detach(&label
, NULL
);
1770 static char *launchctl_service_filename(const char *name
)
1773 struct strbuf filename
= STRBUF_INIT
;
1774 strbuf_addf(&filename
, "~/Library/LaunchAgents/%s.plist", name
);
1776 expanded
= interpolate_path(filename
.buf
, 1);
1778 die(_("failed to expand path '%s'"), filename
.buf
);
1780 strbuf_release(&filename
);
1784 static char *launchctl_get_uid(void)
1786 return xstrfmt("gui/%d", getuid());
1789 static int launchctl_boot_plist(int enable
, const char *filename
)
1791 const char *cmd
= "launchctl";
1793 struct child_process child
= CHILD_PROCESS_INIT
;
1794 char *uid
= launchctl_get_uid();
1796 get_schedule_cmd(&cmd
, NULL
);
1797 strvec_split(&child
.args
, cmd
);
1798 strvec_pushl(&child
.args
, enable
? "bootstrap" : "bootout", uid
,
1801 child
.no_stderr
= 1;
1802 child
.no_stdout
= 1;
1804 if (start_command(&child
))
1805 die(_("failed to start launchctl"));
1807 result
= finish_command(&child
);
1813 static int launchctl_remove_plist(enum schedule_priority schedule
)
1815 const char *frequency
= get_frequency(schedule
);
1816 char *name
= launchctl_service_name(frequency
);
1817 char *filename
= launchctl_service_filename(name
);
1818 int result
= launchctl_boot_plist(0, filename
);
1825 static int launchctl_remove_plists(void)
1827 return launchctl_remove_plist(SCHEDULE_HOURLY
) ||
1828 launchctl_remove_plist(SCHEDULE_DAILY
) ||
1829 launchctl_remove_plist(SCHEDULE_WEEKLY
);
1832 static int launchctl_list_contains_plist(const char *name
, const char *cmd
)
1834 struct child_process child
= CHILD_PROCESS_INIT
;
1836 strvec_split(&child
.args
, cmd
);
1837 strvec_pushl(&child
.args
, "list", name
, NULL
);
1839 child
.no_stderr
= 1;
1840 child
.no_stdout
= 1;
1842 if (start_command(&child
))
1843 die(_("failed to start launchctl"));
1845 /* Returns failure if 'name' doesn't exist. */
1846 return !finish_command(&child
);
1849 static int launchctl_schedule_plist(const char *exec_path
, enum schedule_priority schedule
)
1852 const char *preamble
, *repeat
;
1853 const char *frequency
= get_frequency(schedule
);
1854 char *name
= launchctl_service_name(frequency
);
1855 char *filename
= launchctl_service_filename(name
);
1856 struct lock_file lk
= LOCK_INIT
;
1857 static unsigned long lock_file_timeout_ms
= ULONG_MAX
;
1858 struct strbuf plist
= STRBUF_INIT
, plist2
= STRBUF_INIT
;
1860 const char *cmd
= "launchctl";
1861 int minute
= get_random_minute();
1863 get_schedule_cmd(&cmd
, NULL
);
1864 preamble
= "<?xml version=\"1.0\"?>\n"
1865 "<!DOCTYPE plist PUBLIC \"-//Apple//DTD PLIST 1.0//EN\" \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">\n"
1866 "<plist version=\"1.0\">"
1868 "<key>Label</key><string>%s</string>\n"
1869 "<key>ProgramArguments</key>\n"
1871 "<string>%s/git</string>\n"
1872 "<string>--exec-path=%s</string>\n"
1873 "<string>for-each-repo</string>\n"
1874 "<string>--keep-going</string>\n"
1875 "<string>--config=maintenance.repo</string>\n"
1876 "<string>maintenance</string>\n"
1877 "<string>run</string>\n"
1878 "<string>--schedule=%s</string>\n"
1880 "<key>StartCalendarInterval</key>\n"
1882 strbuf_addf(&plist
, preamble
, name
, exec_path
, exec_path
, frequency
);
1885 case SCHEDULE_HOURLY
:
1887 "<key>Hour</key><integer>%d</integer>\n"
1888 "<key>Minute</key><integer>%d</integer>\n"
1890 for (i
= 1; i
<= 23; i
++)
1891 strbuf_addf(&plist
, repeat
, i
, minute
);
1894 case SCHEDULE_DAILY
:
1896 "<key>Day</key><integer>%d</integer>\n"
1897 "<key>Hour</key><integer>0</integer>\n"
1898 "<key>Minute</key><integer>%d</integer>\n"
1900 for (i
= 1; i
<= 6; i
++)
1901 strbuf_addf(&plist
, repeat
, i
, minute
);
1904 case SCHEDULE_WEEKLY
:
1907 "<key>Day</key><integer>0</integer>\n"
1908 "<key>Hour</key><integer>0</integer>\n"
1909 "<key>Minute</key><integer>%d</integer>\n"
1918 strbuf_addstr(&plist
, "</array>\n</dict>\n</plist>\n");
1920 if (safe_create_leading_directories(filename
))
1921 die(_("failed to create directories for '%s'"), filename
);
1923 if ((long)lock_file_timeout_ms
< 0 &&
1924 git_config_get_ulong("gc.launchctlplistlocktimeoutms",
1925 &lock_file_timeout_ms
))
1926 lock_file_timeout_ms
= 150;
1928 fd
= hold_lock_file_for_update_timeout(&lk
, filename
, LOCK_DIE_ON_ERROR
,
1929 lock_file_timeout_ms
);
1932 * Does this file already exist? With the intended contents? Is it
1933 * registered already? Then it does not need to be re-registered.
1935 if (!stat(filename
, &st
) && st
.st_size
== plist
.len
&&
1936 strbuf_read_file(&plist2
, filename
, plist
.len
) == plist
.len
&&
1937 !strbuf_cmp(&plist
, &plist2
) &&
1938 launchctl_list_contains_plist(name
, cmd
))
1939 rollback_lock_file(&lk
);
1941 if (write_in_full(fd
, plist
.buf
, plist
.len
) < 0 ||
1942 commit_lock_file(&lk
))
1943 die_errno(_("could not write '%s'"), filename
);
1945 /* bootout might fail if not already running, so ignore */
1946 launchctl_boot_plist(0, filename
);
1947 if (launchctl_boot_plist(1, filename
))
1948 die(_("failed to bootstrap service %s"), filename
);
1953 strbuf_release(&plist
);
1954 strbuf_release(&plist2
);
1958 static int launchctl_add_plists(void)
1960 const char *exec_path
= git_exec_path();
1962 return launchctl_schedule_plist(exec_path
, SCHEDULE_HOURLY
) ||
1963 launchctl_schedule_plist(exec_path
, SCHEDULE_DAILY
) ||
1964 launchctl_schedule_plist(exec_path
, SCHEDULE_WEEKLY
);
1967 static int launchctl_update_schedule(int run_maintenance
, int fd UNUSED
)
1969 if (run_maintenance
)
1970 return launchctl_add_plists();
1972 return launchctl_remove_plists();
1975 static int is_schtasks_available(void)
1977 const char *cmd
= "schtasks";
1979 if (get_schedule_cmd(&cmd
, &is_available
))
1980 return is_available
;
1982 #ifdef GIT_WINDOWS_NATIVE
1989 static char *schtasks_task_name(const char *frequency
)
1991 struct strbuf label
= STRBUF_INIT
;
1992 strbuf_addf(&label
, "Git Maintenance (%s)", frequency
);
1993 return strbuf_detach(&label
, NULL
);
1996 static int schtasks_remove_task(enum schedule_priority schedule
)
1998 const char *cmd
= "schtasks";
1999 struct child_process child
= CHILD_PROCESS_INIT
;
2000 const char *frequency
= get_frequency(schedule
);
2001 char *name
= schtasks_task_name(frequency
);
2003 get_schedule_cmd(&cmd
, NULL
);
2004 strvec_split(&child
.args
, cmd
);
2005 strvec_pushl(&child
.args
, "/delete", "/tn", name
, "/f", NULL
);
2008 return run_command(&child
);
2011 static int schtasks_remove_tasks(void)
2013 return schtasks_remove_task(SCHEDULE_HOURLY
) ||
2014 schtasks_remove_task(SCHEDULE_DAILY
) ||
2015 schtasks_remove_task(SCHEDULE_WEEKLY
);
2018 static int schtasks_schedule_task(const char *exec_path
, enum schedule_priority schedule
)
2020 const char *cmd
= "schtasks";
2022 struct child_process child
= CHILD_PROCESS_INIT
;
2024 struct tempfile
*tfile
;
2025 const char *frequency
= get_frequency(schedule
);
2026 char *name
= schtasks_task_name(frequency
);
2027 struct strbuf tfilename
= STRBUF_INIT
;
2028 int minute
= get_random_minute();
2030 get_schedule_cmd(&cmd
, NULL
);
2032 strbuf_addf(&tfilename
, "%s/schedule_%s_XXXXXX",
2033 get_git_common_dir(), frequency
);
2034 tfile
= xmks_tempfile(tfilename
.buf
);
2035 strbuf_release(&tfilename
);
2037 if (!fdopen_tempfile(tfile
, "w"))
2038 die(_("failed to create temp xml file"));
2040 xml
= "<?xml version=\"1.0\" ?>\n"
2041 "<Task version=\"1.4\" xmlns=\"http://schemas.microsoft.com/windows/2004/02/mit/task\">\n"
2043 "<CalendarTrigger>\n";
2044 fputs(xml
, tfile
->fp
);
2047 case SCHEDULE_HOURLY
:
2049 "<StartBoundary>2020-01-01T01:%02d:00</StartBoundary>\n"
2050 "<Enabled>true</Enabled>\n"
2052 "<DaysInterval>1</DaysInterval>\n"
2053 "</ScheduleByDay>\n"
2055 "<Interval>PT1H</Interval>\n"
2056 "<Duration>PT23H</Duration>\n"
2057 "<StopAtDurationEnd>false</StopAtDurationEnd>\n"
2062 case SCHEDULE_DAILY
:
2064 "<StartBoundary>2020-01-01T00:%02d:00</StartBoundary>\n"
2065 "<Enabled>true</Enabled>\n"
2066 "<ScheduleByWeek>\n"
2075 "<WeeksInterval>1</WeeksInterval>\n"
2076 "</ScheduleByWeek>\n",
2080 case SCHEDULE_WEEKLY
:
2082 "<StartBoundary>2020-01-01T00:%02d:00</StartBoundary>\n"
2083 "<Enabled>true</Enabled>\n"
2084 "<ScheduleByWeek>\n"
2088 "<WeeksInterval>1</WeeksInterval>\n"
2089 "</ScheduleByWeek>\n",
2097 xml
= "</CalendarTrigger>\n"
2100 "<Principal id=\"Author\">\n"
2101 "<LogonType>InteractiveToken</LogonType>\n"
2102 "<RunLevel>LeastPrivilege</RunLevel>\n"
2106 "<MultipleInstancesPolicy>IgnoreNew</MultipleInstancesPolicy>\n"
2107 "<Enabled>true</Enabled>\n"
2108 "<Hidden>true</Hidden>\n"
2109 "<UseUnifiedSchedulingEngine>true</UseUnifiedSchedulingEngine>\n"
2110 "<WakeToRun>false</WakeToRun>\n"
2111 "<ExecutionTimeLimit>PT72H</ExecutionTimeLimit>\n"
2112 "<Priority>7</Priority>\n"
2114 "<Actions Context=\"Author\">\n"
2116 "<Command>\"%s\\headless-git.exe\"</Command>\n"
2117 "<Arguments>--exec-path=\"%s\" for-each-repo --keep-going --config=maintenance.repo maintenance run --schedule=%s</Arguments>\n"
2121 fprintf(tfile
->fp
, xml
, exec_path
, exec_path
, frequency
);
2122 strvec_split(&child
.args
, cmd
);
2123 strvec_pushl(&child
.args
, "/create", "/tn", name
, "/f", "/xml",
2124 get_tempfile_path(tfile
), NULL
);
2125 close_tempfile_gently(tfile
);
2127 child
.no_stdout
= 1;
2128 child
.no_stderr
= 1;
2130 if (start_command(&child
))
2131 die(_("failed to start schtasks"));
2132 result
= finish_command(&child
);
2134 delete_tempfile(&tfile
);
2139 static int schtasks_schedule_tasks(void)
2141 const char *exec_path
= git_exec_path();
2143 return schtasks_schedule_task(exec_path
, SCHEDULE_HOURLY
) ||
2144 schtasks_schedule_task(exec_path
, SCHEDULE_DAILY
) ||
2145 schtasks_schedule_task(exec_path
, SCHEDULE_WEEKLY
);
2148 static int schtasks_update_schedule(int run_maintenance
, int fd UNUSED
)
2150 if (run_maintenance
)
2151 return schtasks_schedule_tasks();
2153 return schtasks_remove_tasks();
2157 static int check_crontab_process(const char *cmd
)
2159 struct child_process child
= CHILD_PROCESS_INIT
;
2161 strvec_split(&child
.args
, cmd
);
2162 strvec_push(&child
.args
, "-l");
2164 child
.no_stdout
= 1;
2165 child
.no_stderr
= 1;
2166 child
.silent_exec_failure
= 1;
2168 if (start_command(&child
))
2170 /* Ignore exit code, as an empty crontab will return error. */
2171 finish_command(&child
);
2175 static int is_crontab_available(void)
2177 const char *cmd
= "crontab";
2180 if (get_schedule_cmd(&cmd
, &is_available
))
2181 return is_available
;
2185 * macOS has cron, but it requires special permissions and will
2186 * create a UI alert when attempting to run this command.
2190 return check_crontab_process(cmd
);
2194 #define BEGIN_LINE "# BEGIN GIT MAINTENANCE SCHEDULE"
2195 #define END_LINE "# END GIT MAINTENANCE SCHEDULE"
2197 static int crontab_update_schedule(int run_maintenance
, int fd
)
2199 const char *cmd
= "crontab";
2201 int in_old_region
= 0;
2202 struct child_process crontab_list
= CHILD_PROCESS_INIT
;
2203 struct child_process crontab_edit
= CHILD_PROCESS_INIT
;
2204 FILE *cron_list
, *cron_in
;
2205 struct strbuf line
= STRBUF_INIT
;
2206 struct tempfile
*tmpedit
= NULL
;
2207 int minute
= get_random_minute();
2209 get_schedule_cmd(&cmd
, NULL
);
2210 strvec_split(&crontab_list
.args
, cmd
);
2211 strvec_push(&crontab_list
.args
, "-l");
2212 crontab_list
.in
= -1;
2213 crontab_list
.out
= dup(fd
);
2214 crontab_list
.git_cmd
= 0;
2216 if (start_command(&crontab_list
))
2217 return error(_("failed to run 'crontab -l'; your system might not support 'cron'"));
2219 /* Ignore exit code, as an empty crontab will return error. */
2220 finish_command(&crontab_list
);
2222 tmpedit
= mks_tempfile_t(".git_cron_edit_tmpXXXXXX");
2224 result
= error(_("failed to create crontab temporary file"));
2227 cron_in
= fdopen_tempfile(tmpedit
, "w");
2229 result
= error(_("failed to open temporary file"));
2234 * Read from the .lock file, filtering out the old
2235 * schedule while appending the new schedule.
2237 cron_list
= fdopen(fd
, "r");
2240 while (!strbuf_getline_lf(&line
, cron_list
)) {
2241 if (!in_old_region
&& !strcmp(line
.buf
, BEGIN_LINE
))
2243 else if (in_old_region
&& !strcmp(line
.buf
, END_LINE
))
2245 else if (!in_old_region
)
2246 fprintf(cron_in
, "%s\n", line
.buf
);
2248 strbuf_release(&line
);
2250 if (run_maintenance
) {
2251 struct strbuf line_format
= STRBUF_INIT
;
2252 const char *exec_path
= git_exec_path();
2254 fprintf(cron_in
, "%s\n", BEGIN_LINE
);
2256 "# The following schedule was created by Git\n");
2257 fprintf(cron_in
, "# Any edits made in this region might be\n");
2259 "# replaced in the future by a Git command.\n\n");
2261 strbuf_addf(&line_format
,
2262 "%%d %%s * * %%s \"%s/git\" --exec-path=\"%s\" for-each-repo --keep-going --config=maintenance.repo maintenance run --schedule=%%s\n",
2263 exec_path
, exec_path
);
2264 fprintf(cron_in
, line_format
.buf
, minute
, "1-23", "*", "hourly");
2265 fprintf(cron_in
, line_format
.buf
, minute
, "0", "1-6", "daily");
2266 fprintf(cron_in
, line_format
.buf
, minute
, "0", "0", "weekly");
2267 strbuf_release(&line_format
);
2269 fprintf(cron_in
, "\n%s\n", END_LINE
);
2274 strvec_split(&crontab_edit
.args
, cmd
);
2275 strvec_push(&crontab_edit
.args
, get_tempfile_path(tmpedit
));
2276 crontab_edit
.git_cmd
= 0;
2278 if (start_command(&crontab_edit
)) {
2279 result
= error(_("failed to run 'crontab'; your system might not support 'cron'"));
2283 if (finish_command(&crontab_edit
))
2284 result
= error(_("'crontab' died"));
2288 delete_tempfile(&tmpedit
);
2292 static int real_is_systemd_timer_available(void)
2294 struct child_process child
= CHILD_PROCESS_INIT
;
2296 strvec_pushl(&child
.args
, "systemctl", "--user", "list-timers", NULL
);
2298 child
.no_stdout
= 1;
2299 child
.no_stderr
= 1;
2300 child
.silent_exec_failure
= 1;
2302 if (start_command(&child
))
2304 if (finish_command(&child
))
2309 static int is_systemd_timer_available(void)
2311 const char *cmd
= "systemctl";
2314 if (get_schedule_cmd(&cmd
, &is_available
))
2315 return is_available
;
2317 return real_is_systemd_timer_available();
2320 static char *xdg_config_home_systemd(const char *filename
)
2322 return xdg_config_home_for("systemd/user", filename
);
2325 #define SYSTEMD_UNIT_FORMAT "git-maintenance@%s.%s"
2327 static int systemd_timer_delete_timer_file(enum schedule_priority priority
)
2330 const char *frequency
= get_frequency(priority
);
2331 char *local_timer_name
= xstrfmt(SYSTEMD_UNIT_FORMAT
, frequency
, "timer");
2332 char *filename
= xdg_config_home_systemd(local_timer_name
);
2334 if (unlink(filename
) && !is_missing_file_error(errno
))
2335 ret
= error_errno(_("failed to delete '%s'"), filename
);
2338 free(local_timer_name
);
2342 static int systemd_timer_delete_service_template(void)
2345 char *local_service_name
= xstrfmt(SYSTEMD_UNIT_FORMAT
, "", "service");
2346 char *filename
= xdg_config_home_systemd(local_service_name
);
2347 if (unlink(filename
) && !is_missing_file_error(errno
))
2348 ret
= error_errno(_("failed to delete '%s'"), filename
);
2351 free(local_service_name
);
2356 * Write the schedule information into a git-maintenance@<schedule>.timer
2357 * file using a custom minute. This timer file cannot use the templating
2358 * system, so we generate a specific file for each.
2360 static int systemd_timer_write_timer_file(enum schedule_priority schedule
,
2367 char *schedule_pattern
= NULL
;
2368 const char *frequency
= get_frequency(schedule
);
2369 char *local_timer_name
= xstrfmt(SYSTEMD_UNIT_FORMAT
, frequency
, "timer");
2371 filename
= xdg_config_home_systemd(local_timer_name
);
2373 if (safe_create_leading_directories(filename
)) {
2374 error(_("failed to create directories for '%s'"), filename
);
2377 file
= fopen_or_warn(filename
, "w");
2382 case SCHEDULE_HOURLY
:
2383 schedule_pattern
= xstrfmt("*-*-* 1..23:%02d:00", minute
);
2386 case SCHEDULE_DAILY
:
2387 schedule_pattern
= xstrfmt("Tue..Sun *-*-* 0:%02d:00", minute
);
2390 case SCHEDULE_WEEKLY
:
2391 schedule_pattern
= xstrfmt("Mon 0:%02d:00", minute
);
2395 BUG("Unhandled schedule_priority");
2398 unit
= "# This file was created and is maintained by Git.\n"
2399 "# Any edits made in this file might be replaced in the future\n"
2400 "# by a Git command.\n"
2403 "Description=Optimize Git repositories data\n"
2410 "WantedBy=timers.target\n";
2411 if (fprintf(file
, unit
, schedule_pattern
) < 0) {
2412 error(_("failed to write to '%s'"), filename
);
2416 if (fclose(file
) == EOF
) {
2417 error_errno(_("failed to flush '%s'"), filename
);
2424 free(schedule_pattern
);
2425 free(local_timer_name
);
2431 * No matter the schedule, we use the same service and can make use of the
2432 * templating system. When installing git-maintenance@<schedule>.timer,
2433 * systemd will notice that git-maintenance@.service exists as a template
2434 * and will use this file and insert the <schedule> into the template at
2435 * the position of "%i".
2437 static int systemd_timer_write_service_template(const char *exec_path
)
2443 char *local_service_name
= xstrfmt(SYSTEMD_UNIT_FORMAT
, "", "service");
2445 filename
= xdg_config_home_systemd(local_service_name
);
2446 if (safe_create_leading_directories(filename
)) {
2447 error(_("failed to create directories for '%s'"), filename
);
2450 file
= fopen_or_warn(filename
, "w");
2454 unit
= "# This file was created and is maintained by Git.\n"
2455 "# Any edits made in this file might be replaced in the future\n"
2456 "# by a Git command.\n"
2459 "Description=Optimize Git repositories data\n"
2463 "ExecStart=\"%s/git\" --exec-path=\"%s\" for-each-repo --keep-going --config=maintenance.repo maintenance run --schedule=%%i\n"
2464 "LockPersonality=yes\n"
2465 "MemoryDenyWriteExecute=yes\n"
2466 "NoNewPrivileges=yes\n"
2467 "RestrictAddressFamilies=AF_UNIX AF_INET AF_INET6 AF_VSOCK\n"
2468 "RestrictNamespaces=yes\n"
2469 "RestrictRealtime=yes\n"
2470 "RestrictSUIDSGID=yes\n"
2471 "SystemCallArchitectures=native\n"
2472 "SystemCallFilter=@system-service\n";
2473 if (fprintf(file
, unit
, exec_path
, exec_path
) < 0) {
2474 error(_("failed to write to '%s'"), filename
);
2478 if (fclose(file
) == EOF
) {
2479 error_errno(_("failed to flush '%s'"), filename
);
2486 free(local_service_name
);
2491 static int systemd_timer_enable_unit(int enable
,
2492 enum schedule_priority schedule
,
2495 const char *cmd
= "systemctl";
2496 struct child_process child
= CHILD_PROCESS_INIT
;
2497 const char *frequency
= get_frequency(schedule
);
2500 * Disabling the systemd unit while it is already disabled makes
2501 * systemctl print an error.
2502 * Let's ignore it since it means we already are in the expected state:
2503 * the unit is disabled.
2505 * On the other hand, enabling a systemd unit which is already enabled
2506 * produces no error.
2509 child
.no_stderr
= 1;
2510 else if (systemd_timer_write_timer_file(schedule
, minute
))
2513 get_schedule_cmd(&cmd
, NULL
);
2514 strvec_split(&child
.args
, cmd
);
2515 strvec_pushl(&child
.args
, "--user", enable
? "enable" : "disable",
2517 strvec_pushf(&child
.args
, SYSTEMD_UNIT_FORMAT
, frequency
, "timer");
2519 if (start_command(&child
))
2520 return error(_("failed to start systemctl"));
2521 if (finish_command(&child
))
2523 * Disabling an already disabled systemd unit makes
2525 * Let's ignore this failure.
2527 * Enabling an enabled systemd unit doesn't fail.
2530 return error(_("failed to run systemctl"));
2535 * A previous version of Git wrote the timer units as template files.
2536 * Clean these up, if they exist.
2538 static void systemd_timer_delete_stale_timer_templates(void)
2540 char *timer_template_name
= xstrfmt(SYSTEMD_UNIT_FORMAT
, "", "timer");
2541 char *filename
= xdg_config_home_systemd(timer_template_name
);
2543 if (unlink(filename
) && !is_missing_file_error(errno
))
2544 warning(_("failed to delete '%s'"), filename
);
2547 free(timer_template_name
);
2550 static int systemd_timer_delete_unit_files(void)
2552 systemd_timer_delete_stale_timer_templates();
2554 /* Purposefully not short-circuited to make sure all are called. */
2555 return systemd_timer_delete_timer_file(SCHEDULE_HOURLY
) |
2556 systemd_timer_delete_timer_file(SCHEDULE_DAILY
) |
2557 systemd_timer_delete_timer_file(SCHEDULE_WEEKLY
) |
2558 systemd_timer_delete_service_template();
2561 static int systemd_timer_delete_units(void)
2563 int minute
= get_random_minute();
2564 /* Purposefully not short-circuited to make sure all are called. */
2565 return systemd_timer_enable_unit(0, SCHEDULE_HOURLY
, minute
) |
2566 systemd_timer_enable_unit(0, SCHEDULE_DAILY
, minute
) |
2567 systemd_timer_enable_unit(0, SCHEDULE_WEEKLY
, minute
) |
2568 systemd_timer_delete_unit_files();
2571 static int systemd_timer_setup_units(void)
2573 int minute
= get_random_minute();
2574 const char *exec_path
= git_exec_path();
2576 int ret
= systemd_timer_write_service_template(exec_path
) ||
2577 systemd_timer_enable_unit(1, SCHEDULE_HOURLY
, minute
) ||
2578 systemd_timer_enable_unit(1, SCHEDULE_DAILY
, minute
) ||
2579 systemd_timer_enable_unit(1, SCHEDULE_WEEKLY
, minute
);
2582 systemd_timer_delete_units();
2584 systemd_timer_delete_stale_timer_templates();
2589 static int systemd_timer_update_schedule(int run_maintenance
, int fd UNUSED
)
2591 if (run_maintenance
)
2592 return systemd_timer_setup_units();
2594 return systemd_timer_delete_units();
2598 SCHEDULER_INVALID
= -1,
2602 SCHEDULER_LAUNCHCTL
,
2606 static const struct {
2608 int (*is_available
)(void);
2609 int (*update_schedule
)(int run_maintenance
, int fd
);
2610 } scheduler_fn
[] = {
2611 [SCHEDULER_CRON
] = {
2613 .is_available
= is_crontab_available
,
2614 .update_schedule
= crontab_update_schedule
,
2616 [SCHEDULER_SYSTEMD
] = {
2617 .name
= "systemctl",
2618 .is_available
= is_systemd_timer_available
,
2619 .update_schedule
= systemd_timer_update_schedule
,
2621 [SCHEDULER_LAUNCHCTL
] = {
2622 .name
= "launchctl",
2623 .is_available
= is_launchctl_available
,
2624 .update_schedule
= launchctl_update_schedule
,
2626 [SCHEDULER_SCHTASKS
] = {
2628 .is_available
= is_schtasks_available
,
2629 .update_schedule
= schtasks_update_schedule
,
2633 static enum scheduler
parse_scheduler(const char *value
)
2636 return SCHEDULER_INVALID
;
2637 else if (!strcasecmp(value
, "auto"))
2638 return SCHEDULER_AUTO
;
2639 else if (!strcasecmp(value
, "cron") || !strcasecmp(value
, "crontab"))
2640 return SCHEDULER_CRON
;
2641 else if (!strcasecmp(value
, "systemd") ||
2642 !strcasecmp(value
, "systemd-timer"))
2643 return SCHEDULER_SYSTEMD
;
2644 else if (!strcasecmp(value
, "launchctl"))
2645 return SCHEDULER_LAUNCHCTL
;
2646 else if (!strcasecmp(value
, "schtasks"))
2647 return SCHEDULER_SCHTASKS
;
2649 return SCHEDULER_INVALID
;
2652 static int maintenance_opt_scheduler(const struct option
*opt
, const char *arg
,
2655 enum scheduler
*scheduler
= opt
->value
;
2657 BUG_ON_OPT_NEG(unset
);
2659 *scheduler
= parse_scheduler(arg
);
2660 if (*scheduler
== SCHEDULER_INVALID
)
2661 return error(_("unrecognized --scheduler argument '%s'"), arg
);
2665 struct maintenance_start_opts
{
2666 enum scheduler scheduler
;
2669 static enum scheduler
resolve_scheduler(enum scheduler scheduler
)
2671 if (scheduler
!= SCHEDULER_AUTO
)
2674 #if defined(__APPLE__)
2675 return SCHEDULER_LAUNCHCTL
;
2677 #elif defined(GIT_WINDOWS_NATIVE)
2678 return SCHEDULER_SCHTASKS
;
2680 #elif defined(__linux__)
2681 if (is_systemd_timer_available())
2682 return SCHEDULER_SYSTEMD
;
2683 else if (is_crontab_available())
2684 return SCHEDULER_CRON
;
2686 die(_("neither systemd timers nor crontab are available"));
2689 return SCHEDULER_CRON
;
2693 static void validate_scheduler(enum scheduler scheduler
)
2695 if (scheduler
== SCHEDULER_INVALID
)
2696 BUG("invalid scheduler");
2697 if (scheduler
== SCHEDULER_AUTO
)
2698 BUG("resolve_scheduler should have been called before");
2700 if (!scheduler_fn
[scheduler
].is_available())
2701 die(_("%s scheduler is not available"),
2702 scheduler_fn
[scheduler
].name
);
2705 static int update_background_schedule(const struct maintenance_start_opts
*opts
,
2710 struct lock_file lk
;
2711 char *lock_path
= xstrfmt("%s/schedule", the_repository
->objects
->odb
->path
);
2713 if (hold_lock_file_for_update(&lk
, lock_path
, LOCK_NO_DEREF
) < 0) {
2715 return error(_("another process is scheduling background maintenance"));
2718 for (i
= 1; i
< ARRAY_SIZE(scheduler_fn
); i
++) {
2719 if (enable
&& opts
->scheduler
== i
)
2721 if (!scheduler_fn
[i
].is_available())
2723 scheduler_fn
[i
].update_schedule(0, get_lock_file_fd(&lk
));
2727 result
= scheduler_fn
[opts
->scheduler
].update_schedule(
2728 1, get_lock_file_fd(&lk
));
2730 rollback_lock_file(&lk
);
2736 static const char *const builtin_maintenance_start_usage
[] = {
2737 N_("git maintenance start [--scheduler=<scheduler>]"),
2741 static int maintenance_start(int argc
, const char **argv
, const char *prefix
)
2743 struct maintenance_start_opts opts
= { 0 };
2744 struct option options
[] = {
2746 0, "scheduler", &opts
.scheduler
, N_("scheduler"),
2747 N_("scheduler to trigger git maintenance run"),
2748 PARSE_OPT_NONEG
, maintenance_opt_scheduler
),
2751 const char *register_args
[] = { "register", NULL
};
2753 argc
= parse_options(argc
, argv
, prefix
, options
,
2754 builtin_maintenance_start_usage
, 0);
2756 usage_with_options(builtin_maintenance_start_usage
, options
);
2758 opts
.scheduler
= resolve_scheduler(opts
.scheduler
);
2759 validate_scheduler(opts
.scheduler
);
2761 if (update_background_schedule(&opts
, 1))
2762 die(_("failed to set up maintenance schedule"));
2764 if (maintenance_register(ARRAY_SIZE(register_args
)-1, register_args
, NULL
))
2765 warning(_("failed to add repo to global config"));
2769 static const char *const builtin_maintenance_stop_usage
[] = {
2770 "git maintenance stop",
2774 static int maintenance_stop(int argc
, const char **argv
, const char *prefix
)
2776 struct option options
[] = {
2779 argc
= parse_options(argc
, argv
, prefix
, options
,
2780 builtin_maintenance_stop_usage
, 0);
2782 usage_with_options(builtin_maintenance_stop_usage
, options
);
2783 return update_background_schedule(NULL
, 0);
2786 static const char * const builtin_maintenance_usage
[] = {
2787 N_("git maintenance <subcommand> [<options>]"),
2791 int cmd_maintenance(int argc
, const char **argv
, const char *prefix
)
2793 parse_opt_subcommand_fn
*fn
= NULL
;
2794 struct option builtin_maintenance_options
[] = {
2795 OPT_SUBCOMMAND("run", &fn
, maintenance_run
),
2796 OPT_SUBCOMMAND("start", &fn
, maintenance_start
),
2797 OPT_SUBCOMMAND("stop", &fn
, maintenance_stop
),
2798 OPT_SUBCOMMAND("register", &fn
, maintenance_register
),
2799 OPT_SUBCOMMAND("unregister", &fn
, maintenance_unregister
),
2803 argc
= parse_options(argc
, argv
, prefix
, builtin_maintenance_options
,
2804 builtin_maintenance_usage
, 0);
2805 return fn(argc
, argv
, prefix
);