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 struct maintenance_run_opts
;
184 static int maintenance_task_pack_refs(MAYBE_UNUSED
struct maintenance_run_opts
*opts
)
186 struct child_process cmd
= CHILD_PROCESS_INIT
;
189 strvec_pushl(&cmd
.args
, "pack-refs", "--all", "--prune", NULL
);
190 return run_command(&cmd
);
193 static int too_many_loose_objects(void)
196 * Quickly check if a "gc" is needed, by estimating how
197 * many loose objects there are. Because SHA-1 is evenly
198 * distributed, we can check only one and get a reasonable
206 const unsigned hexsz_loose
= the_hash_algo
->hexsz
- 2;
208 dir
= opendir(git_path("objects/17"));
212 auto_threshold
= DIV_ROUND_UP(gc_auto_threshold
, 256);
213 while ((ent
= readdir(dir
)) != NULL
) {
214 if (strspn(ent
->d_name
, "0123456789abcdef") != hexsz_loose
||
215 ent
->d_name
[hexsz_loose
] != '\0')
217 if (++num_loose
> auto_threshold
) {
226 static struct packed_git
*find_base_packs(struct string_list
*packs
,
229 struct packed_git
*p
, *base
= NULL
;
231 for (p
= get_all_packs(the_repository
); p
; p
= p
->next
) {
232 if (!p
->pack_local
|| p
->is_cruft
)
235 if (p
->pack_size
>= limit
)
236 string_list_append(packs
, p
->pack_name
);
237 } else if (!base
|| base
->pack_size
< p
->pack_size
) {
243 string_list_append(packs
, base
->pack_name
);
248 static int too_many_packs(void)
250 struct packed_git
*p
;
253 if (gc_auto_pack_limit
<= 0)
256 for (cnt
= 0, p
= get_all_packs(the_repository
); p
; p
= p
->next
) {
262 * Perhaps check the size of the pack and count only
263 * very small ones here?
267 return gc_auto_pack_limit
< cnt
;
270 static uint64_t total_ram(void)
272 #if defined(HAVE_SYSINFO)
277 #elif defined(HAVE_BSD_SYSCTL) && (defined(HW_MEMSIZE) || defined(HW_PHYSMEM))
278 int64_t physical_memory
;
283 # if defined(HW_MEMSIZE)
288 length
= sizeof(int64_t);
289 if (!sysctl(mib
, 2, &physical_memory
, &length
, NULL
, 0))
290 return physical_memory
;
291 #elif defined(GIT_WINDOWS_NATIVE)
292 MEMORYSTATUSEX memInfo
;
294 memInfo
.dwLength
= sizeof(MEMORYSTATUSEX
);
295 if (GlobalMemoryStatusEx(&memInfo
))
296 return memInfo
.ullTotalPhys
;
301 static uint64_t estimate_repack_memory(struct packed_git
*pack
)
303 unsigned long nr_objects
= repo_approximate_object_count(the_repository
);
304 size_t os_cache
, heap
;
306 if (!pack
|| !nr_objects
)
310 * First we have to scan through at least one pack.
311 * Assume enough room in OS file cache to keep the entire pack
312 * or we may accidentally evict data of other processes from
315 os_cache
= pack
->pack_size
+ pack
->index_size
;
316 /* then pack-objects needs lots more for book keeping */
317 heap
= sizeof(struct object_entry
) * nr_objects
;
319 * internal rev-list --all --objects takes up some memory too,
320 * let's say half of it is for blobs
322 heap
+= sizeof(struct blob
) * nr_objects
/ 2;
324 * and the other half is for trees (commits and tags are
325 * usually insignificant)
327 heap
+= sizeof(struct tree
) * nr_objects
/ 2;
328 /* and then obj_hash[], underestimated in fact */
329 heap
+= sizeof(struct object
*) * nr_objects
;
330 /* revindex is used also */
331 heap
+= (sizeof(off_t
) + sizeof(uint32_t)) * nr_objects
;
333 * read_sha1_file() (either at delta calculation phase, or
334 * writing phase) also fills up the delta base cache
336 heap
+= delta_base_cache_limit
;
337 /* and of course pack-objects has its own delta cache */
338 heap
+= max_delta_cache_size
;
340 return os_cache
+ heap
;
343 static int keep_one_pack(struct string_list_item
*item
, void *data UNUSED
)
345 strvec_pushf(&repack
, "--keep-pack=%s", basename(item
->string
));
349 static void add_repack_all_option(struct string_list
*keep_pack
)
351 if (prune_expire
&& !strcmp(prune_expire
, "now"))
352 strvec_push(&repack
, "-a");
353 else if (cruft_packs
) {
354 strvec_push(&repack
, "--cruft");
356 strvec_pushf(&repack
, "--cruft-expiration=%s", prune_expire
);
358 strvec_pushf(&repack
, "--max-cruft-size=%lu",
361 strvec_push(&repack
, "-A");
363 strvec_pushf(&repack
, "--unpack-unreachable=%s", prune_expire
);
367 for_each_string_list(keep_pack
, keep_one_pack
, NULL
);
369 if (repack_filter
&& *repack_filter
)
370 strvec_pushf(&repack
, "--filter=%s", repack_filter
);
371 if (repack_filter_to
&& *repack_filter_to
)
372 strvec_pushf(&repack
, "--filter-to=%s", repack_filter_to
);
375 static void add_repack_incremental_option(void)
377 strvec_push(&repack
, "--no-write-bitmap-index");
380 static int need_to_gc(void)
383 * Setting gc.auto to 0 or negative can disable the
386 if (gc_auto_threshold
<= 0)
390 * If there are too many loose objects, but not too many
391 * packs, we run "repack -d -l". If there are too many packs,
392 * we run "repack -A -d -l". Otherwise we tell the caller
395 if (too_many_packs()) {
396 struct string_list keep_pack
= STRING_LIST_INIT_NODUP
;
398 if (big_pack_threshold
) {
399 find_base_packs(&keep_pack
, big_pack_threshold
);
400 if (keep_pack
.nr
>= gc_auto_pack_limit
) {
401 big_pack_threshold
= 0;
402 string_list_clear(&keep_pack
, 0);
403 find_base_packs(&keep_pack
, 0);
406 struct packed_git
*p
= find_base_packs(&keep_pack
, 0);
407 uint64_t mem_have
, mem_want
;
409 mem_have
= total_ram();
410 mem_want
= estimate_repack_memory(p
);
413 * Only allow 1/2 of memory for pack-objects, leave
414 * the rest for the OS and other processes in the
417 if (!mem_have
|| mem_want
< mem_have
/ 2)
418 string_list_clear(&keep_pack
, 0);
421 add_repack_all_option(&keep_pack
);
422 string_list_clear(&keep_pack
, 0);
423 } else if (too_many_loose_objects())
424 add_repack_incremental_option();
428 if (run_hooks("pre-auto-gc"))
433 /* return NULL on success, else hostname running the gc */
434 static const char *lock_repo_for_gc(int force
, pid_t
* ret_pid
)
436 struct lock_file lock
= LOCK_INIT
;
437 char my_host
[HOST_NAME_MAX
+ 1];
438 struct strbuf sb
= STRBUF_INIT
;
445 if (is_tempfile_active(pidfile
))
449 if (xgethostname(my_host
, sizeof(my_host
)))
450 xsnprintf(my_host
, sizeof(my_host
), "unknown");
452 pidfile_path
= git_pathdup("gc.pid");
453 fd
= hold_lock_file_for_update(&lock
, pidfile_path
,
456 static char locking_host
[HOST_NAME_MAX
+ 1];
457 static char *scan_fmt
;
461 scan_fmt
= xstrfmt("%s %%%ds", "%"SCNuMAX
, HOST_NAME_MAX
);
462 fp
= fopen(pidfile_path
, "r");
463 memset(locking_host
, 0, sizeof(locking_host
));
466 !fstat(fileno(fp
), &st
) &&
468 * 12 hour limit is very generous as gc should
469 * never take that long. On the other hand we
470 * don't really need a strict limit here,
471 * running gc --auto one day late is not a big
472 * problem. --force can be used in manual gc
473 * after the user verifies that no gc is
476 time(NULL
) - st
.st_mtime
<= 12 * 3600 &&
477 fscanf(fp
, scan_fmt
, &pid
, locking_host
) == 2 &&
478 /* be gentle to concurrent "gc" on remote hosts */
479 (strcmp(locking_host
, my_host
) || !kill(pid
, 0) || errno
== EPERM
);
484 rollback_lock_file(&lock
);
491 strbuf_addf(&sb
, "%"PRIuMAX
" %s",
492 (uintmax_t) getpid(), my_host
);
493 write_in_full(fd
, sb
.buf
, sb
.len
);
495 commit_lock_file(&lock
);
496 pidfile
= register_tempfile(pidfile_path
);
502 * Returns 0 if there was no previous error and gc can proceed, 1 if
503 * gc should not proceed due to an error in the last run. Prints a
504 * message and returns with a non-[01] status code if an error occurred
505 * while reading gc.log
507 static int report_last_gc_error(void)
509 struct strbuf sb
= STRBUF_INIT
;
513 char *gc_log_path
= git_pathdup("gc.log");
515 if (stat(gc_log_path
, &st
)) {
519 ret
= die_message_errno(_("cannot stat '%s'"), gc_log_path
);
523 if (st
.st_mtime
< gc_log_expire_time
)
526 len
= strbuf_read_file(&sb
, gc_log_path
, 0);
528 ret
= die_message_errno(_("cannot read '%s'"), gc_log_path
);
531 * A previous gc failed. Report the error, and don't
532 * bother with an automatic gc run since it is likely
533 * to fail in the same way.
535 warning(_("The last gc run reported the following. "
536 "Please correct the root cause\n"
538 "Automatic cleanup will not be performed "
539 "until the file is removed.\n\n"
541 gc_log_path
, sb
.buf
);
550 static void gc_before_repack(void)
553 * We may be called twice, as both the pre- and
554 * post-daemonized phases will call us, but running these
555 * commands more than once is pointless and wasteful.
561 if (pack_refs
&& maintenance_task_pack_refs(NULL
))
562 die(FAILED_RUN
, "pack-refs");
565 struct child_process cmd
= CHILD_PROCESS_INIT
;
568 strvec_pushv(&cmd
.args
, reflog
.v
);
569 if (run_command(&cmd
))
570 die(FAILED_RUN
, reflog
.v
[0]);
574 int cmd_gc(int argc
, const char **argv
, const char *prefix
)
583 int keep_largest_pack
= -1;
585 struct child_process rerere_cmd
= CHILD_PROCESS_INIT
;
587 struct option builtin_gc_options
[] = {
588 OPT__QUIET(&quiet
, N_("suppress progress reporting")),
589 { OPTION_STRING
, 0, "prune", &prune_expire
, N_("date"),
590 N_("prune unreferenced objects"),
591 PARSE_OPT_OPTARG
, NULL
, (intptr_t)prune_expire
},
592 OPT_BOOL(0, "cruft", &cruft_packs
, N_("pack unreferenced objects separately")),
593 OPT_MAGNITUDE(0, "max-cruft-size", &max_cruft_size
,
594 N_("with --cruft, limit the size of new cruft packs")),
595 OPT_BOOL(0, "aggressive", &aggressive
, N_("be more thorough (increased runtime)")),
596 OPT_BOOL_F(0, "auto", &auto_gc
, N_("enable auto-gc mode"),
597 PARSE_OPT_NOCOMPLETE
),
598 OPT_BOOL_F(0, "force", &force
,
599 N_("force running gc even if there may be another gc running"),
600 PARSE_OPT_NOCOMPLETE
),
601 OPT_BOOL(0, "keep-largest-pack", &keep_largest_pack
,
602 N_("repack all other packs except the largest pack")),
606 if (argc
== 2 && !strcmp(argv
[1], "-h"))
607 usage_with_options(builtin_gc_usage
, builtin_gc_options
);
609 strvec_pushl(&reflog
, "reflog", "expire", "--all", NULL
);
610 strvec_pushl(&repack
, "repack", "-d", "-l", NULL
);
611 strvec_pushl(&prune
, "prune", "--expire", NULL
);
612 strvec_pushl(&prune_worktrees
, "worktree", "prune", "--expire", NULL
);
613 strvec_pushl(&rerere
, "rerere", "gc", NULL
);
615 /* default expiry time, overwritten in gc_config */
617 if (parse_expiry_date(gc_log_expire
, &gc_log_expire_time
))
618 die(_("failed to parse gc.logExpiry value %s"), gc_log_expire
);
621 pack_refs
= !is_bare_repository();
623 argc
= parse_options(argc
, argv
, prefix
, builtin_gc_options
,
624 builtin_gc_usage
, 0);
626 usage_with_options(builtin_gc_usage
, builtin_gc_options
);
628 if (prune_expire
&& parse_expiry_date(prune_expire
, &dummy
))
629 die(_("failed to parse prune expiry value %s"), prune_expire
);
632 strvec_push(&repack
, "-f");
633 if (aggressive_depth
> 0)
634 strvec_pushf(&repack
, "--depth=%d", aggressive_depth
);
635 if (aggressive_window
> 0)
636 strvec_pushf(&repack
, "--window=%d", aggressive_window
);
639 strvec_push(&repack
, "-q");
643 * Auto-gc should be least intrusive as possible.
649 fprintf(stderr
, _("Auto packing the repository in background for optimum performance.\n"));
651 fprintf(stderr
, _("Auto packing the repository for optimum performance.\n"));
652 fprintf(stderr
, _("See \"git help gc\" for manual housekeeping.\n"));
655 int ret
= report_last_gc_error();
658 /* Last gc --auto failed. Skip this one. */
661 /* an I/O error occurred, already reported */
664 if (lock_repo_for_gc(force
, &pid
))
666 gc_before_repack(); /* dies on failure */
667 delete_tempfile(&pidfile
);
670 * failure to daemonize is ok, we'll continue
673 daemonized
= !daemonize();
676 struct string_list keep_pack
= STRING_LIST_INIT_NODUP
;
678 if (keep_largest_pack
!= -1) {
679 if (keep_largest_pack
)
680 find_base_packs(&keep_pack
, 0);
681 } else if (big_pack_threshold
) {
682 find_base_packs(&keep_pack
, big_pack_threshold
);
685 add_repack_all_option(&keep_pack
);
686 string_list_clear(&keep_pack
, 0);
689 name
= lock_repo_for_gc(force
, &pid
);
692 return 0; /* be quiet on --auto */
693 die(_("gc is already running on machine '%s' pid %"PRIuMAX
" (use --force if not)"),
694 name
, (uintmax_t)pid
);
698 hold_lock_file_for_update(&log_lock
,
701 dup2(get_lock_file_fd(&log_lock
), 2);
702 sigchain_push_common(process_log_file_on_signal
);
703 atexit(process_log_file_at_exit
);
708 if (!repository_format_precious_objects
) {
709 struct child_process repack_cmd
= CHILD_PROCESS_INIT
;
711 repack_cmd
.git_cmd
= 1;
712 repack_cmd
.close_object_store
= 1;
713 strvec_pushv(&repack_cmd
.args
, repack
.v
);
714 if (run_command(&repack_cmd
))
715 die(FAILED_RUN
, repack
.v
[0]);
718 struct child_process prune_cmd
= CHILD_PROCESS_INIT
;
720 /* run `git prune` even if using cruft packs */
721 strvec_push(&prune
, prune_expire
);
723 strvec_push(&prune
, "--no-progress");
724 if (repo_has_promisor_remote(the_repository
))
726 "--exclude-promisor-objects");
727 prune_cmd
.git_cmd
= 1;
728 strvec_pushv(&prune_cmd
.args
, prune
.v
);
729 if (run_command(&prune_cmd
))
730 die(FAILED_RUN
, prune
.v
[0]);
734 if (prune_worktrees_expire
) {
735 struct child_process prune_worktrees_cmd
= CHILD_PROCESS_INIT
;
737 strvec_push(&prune_worktrees
, prune_worktrees_expire
);
738 prune_worktrees_cmd
.git_cmd
= 1;
739 strvec_pushv(&prune_worktrees_cmd
.args
, prune_worktrees
.v
);
740 if (run_command(&prune_worktrees_cmd
))
741 die(FAILED_RUN
, prune_worktrees
.v
[0]);
744 rerere_cmd
.git_cmd
= 1;
745 strvec_pushv(&rerere_cmd
.args
, rerere
.v
);
746 if (run_command(&rerere_cmd
))
747 die(FAILED_RUN
, rerere
.v
[0]);
749 report_garbage
= report_pack_garbage
;
750 reprepare_packed_git(the_repository
);
751 if (pack_garbage
.nr
> 0) {
752 close_object_store(the_repository
->objects
);
753 clean_pack_garbage();
756 if (the_repository
->settings
.gc_write_commit_graph
== 1)
757 write_commit_graph_reachable(the_repository
->objects
->odb
,
758 !quiet
&& !daemonized
? COMMIT_GRAPH_WRITE_PROGRESS
: 0,
761 if (auto_gc
&& too_many_loose_objects())
762 warning(_("There are too many unreachable loose objects; "
763 "run 'git prune' to remove them."));
766 unlink(git_path("gc.log"));
771 static const char *const builtin_maintenance_run_usage
[] = {
772 N_("git maintenance run [--auto] [--[no-]quiet] [--task=<task>] [--schedule]"),
776 enum schedule_priority
{
783 static enum schedule_priority
parse_schedule(const char *value
)
786 return SCHEDULE_NONE
;
787 if (!strcasecmp(value
, "hourly"))
788 return SCHEDULE_HOURLY
;
789 if (!strcasecmp(value
, "daily"))
790 return SCHEDULE_DAILY
;
791 if (!strcasecmp(value
, "weekly"))
792 return SCHEDULE_WEEKLY
;
793 return SCHEDULE_NONE
;
796 static int maintenance_opt_schedule(const struct option
*opt
, const char *arg
,
799 enum schedule_priority
*priority
= opt
->value
;
802 die(_("--no-schedule is not allowed"));
804 *priority
= parse_schedule(arg
);
807 die(_("unrecognized --schedule argument '%s'"), arg
);
812 struct maintenance_run_opts
{
815 enum schedule_priority schedule
;
818 /* Remember to update object flag allocation in object.h */
821 struct cg_auto_data
{
822 int num_not_in_graph
;
826 static int dfs_on_ref(const char *refname UNUSED
,
827 const struct object_id
*oid
,
831 struct cg_auto_data
*data
= (struct cg_auto_data
*)cb_data
;
833 struct object_id peeled
;
834 struct commit_list
*stack
= NULL
;
835 struct commit
*commit
;
837 if (!peel_iterated_oid(oid
, &peeled
))
839 if (oid_object_info(the_repository
, oid
, NULL
) != OBJ_COMMIT
)
842 commit
= lookup_commit(the_repository
, oid
);
845 if (repo_parse_commit(the_repository
, commit
) ||
846 commit_graph_position(commit
) != COMMIT_NOT_FROM_GRAPH
)
849 data
->num_not_in_graph
++;
851 if (data
->num_not_in_graph
>= data
->limit
)
854 commit_list_append(commit
, &stack
);
856 while (!result
&& stack
) {
857 struct commit_list
*parent
;
859 commit
= pop_commit(&stack
);
861 for (parent
= commit
->parents
; parent
; parent
= parent
->next
) {
862 if (repo_parse_commit(the_repository
, parent
->item
) ||
863 commit_graph_position(parent
->item
) != COMMIT_NOT_FROM_GRAPH
||
864 parent
->item
->object
.flags
& SEEN
)
867 parent
->item
->object
.flags
|= SEEN
;
868 data
->num_not_in_graph
++;
870 if (data
->num_not_in_graph
>= data
->limit
) {
875 commit_list_append(parent
->item
, &stack
);
879 free_commit_list(stack
);
883 static int should_write_commit_graph(void)
886 struct cg_auto_data data
;
888 data
.num_not_in_graph
= 0;
890 git_config_get_int("maintenance.commit-graph.auto",
898 result
= for_each_ref(dfs_on_ref
, &data
);
900 repo_clear_commit_marks(the_repository
, SEEN
);
905 static int run_write_commit_graph(struct maintenance_run_opts
*opts
)
907 struct child_process child
= CHILD_PROCESS_INIT
;
909 child
.git_cmd
= child
.close_object_store
= 1;
910 strvec_pushl(&child
.args
, "commit-graph", "write",
911 "--split", "--reachable", NULL
);
914 strvec_push(&child
.args
, "--no-progress");
916 return !!run_command(&child
);
919 static int maintenance_task_commit_graph(struct maintenance_run_opts
*opts
)
921 prepare_repo_settings(the_repository
);
922 if (!the_repository
->settings
.core_commit_graph
)
925 if (run_write_commit_graph(opts
)) {
926 error(_("failed to write commit-graph"));
933 static int fetch_remote(struct remote
*remote
, void *cbdata
)
935 struct maintenance_run_opts
*opts
= cbdata
;
936 struct child_process child
= CHILD_PROCESS_INIT
;
938 if (remote
->skip_default_update
)
942 strvec_pushl(&child
.args
, "fetch", remote
->name
,
943 "--prefetch", "--prune", "--no-tags",
944 "--no-write-fetch-head", "--recurse-submodules=no",
948 strvec_push(&child
.args
, "--quiet");
950 return !!run_command(&child
);
953 static int maintenance_task_prefetch(struct maintenance_run_opts
*opts
)
955 if (for_each_remote(fetch_remote
, opts
)) {
956 error(_("failed to prefetch remotes"));
963 static int maintenance_task_gc(struct maintenance_run_opts
*opts
)
965 struct child_process child
= CHILD_PROCESS_INIT
;
967 child
.git_cmd
= child
.close_object_store
= 1;
968 strvec_push(&child
.args
, "gc");
971 strvec_push(&child
.args
, "--auto");
973 strvec_push(&child
.args
, "--quiet");
975 strvec_push(&child
.args
, "--no-quiet");
977 return run_command(&child
);
980 static int prune_packed(struct maintenance_run_opts
*opts
)
982 struct child_process child
= CHILD_PROCESS_INIT
;
985 strvec_push(&child
.args
, "prune-packed");
988 strvec_push(&child
.args
, "--quiet");
990 return !!run_command(&child
);
993 struct write_loose_object_data
{
999 static int loose_object_auto_limit
= 100;
1001 static int loose_object_count(const struct object_id
*oid UNUSED
,
1002 const char *path UNUSED
,
1005 int *count
= (int*)data
;
1006 if (++(*count
) >= loose_object_auto_limit
)
1011 static int loose_object_auto_condition(void)
1015 git_config_get_int("maintenance.loose-objects.auto",
1016 &loose_object_auto_limit
);
1018 if (!loose_object_auto_limit
)
1020 if (loose_object_auto_limit
< 0)
1023 return for_each_loose_file_in_objdir(the_repository
->objects
->odb
->path
,
1025 NULL
, NULL
, &count
);
1028 static int bail_on_loose(const struct object_id
*oid UNUSED
,
1029 const char *path UNUSED
,
1035 static int write_loose_object_to_stdin(const struct object_id
*oid
,
1036 const char *path UNUSED
,
1039 struct write_loose_object_data
*d
= (struct write_loose_object_data
*)data
;
1041 fprintf(d
->in
, "%s\n", oid_to_hex(oid
));
1043 return ++(d
->count
) > d
->batch_size
;
1046 static int pack_loose(struct maintenance_run_opts
*opts
)
1048 struct repository
*r
= the_repository
;
1050 struct write_loose_object_data data
;
1051 struct child_process pack_proc
= CHILD_PROCESS_INIT
;
1054 * Do not start pack-objects process
1055 * if there are no loose objects.
1057 if (!for_each_loose_file_in_objdir(r
->objects
->odb
->path
,
1062 pack_proc
.git_cmd
= 1;
1064 strvec_push(&pack_proc
.args
, "pack-objects");
1066 strvec_push(&pack_proc
.args
, "--quiet");
1067 strvec_pushf(&pack_proc
.args
, "%s/pack/loose", r
->objects
->odb
->path
);
1071 if (start_command(&pack_proc
)) {
1072 error(_("failed to start 'git pack-objects' process"));
1076 data
.in
= xfdopen(pack_proc
.in
, "w");
1078 data
.batch_size
= 50000;
1080 for_each_loose_file_in_objdir(r
->objects
->odb
->path
,
1081 write_loose_object_to_stdin
,
1088 if (finish_command(&pack_proc
)) {
1089 error(_("failed to finish 'git pack-objects' process"));
1096 static int maintenance_task_loose_objects(struct maintenance_run_opts
*opts
)
1098 return prune_packed(opts
) || pack_loose(opts
);
1101 static int incremental_repack_auto_condition(void)
1103 struct packed_git
*p
;
1104 int incremental_repack_auto_limit
= 10;
1107 prepare_repo_settings(the_repository
);
1108 if (!the_repository
->settings
.core_multi_pack_index
)
1111 git_config_get_int("maintenance.incremental-repack.auto",
1112 &incremental_repack_auto_limit
);
1114 if (!incremental_repack_auto_limit
)
1116 if (incremental_repack_auto_limit
< 0)
1119 for (p
= get_packed_git(the_repository
);
1120 count
< incremental_repack_auto_limit
&& p
;
1122 if (!p
->multi_pack_index
)
1126 return count
>= incremental_repack_auto_limit
;
1129 static int multi_pack_index_write(struct maintenance_run_opts
*opts
)
1131 struct child_process child
= CHILD_PROCESS_INIT
;
1134 strvec_pushl(&child
.args
, "multi-pack-index", "write", NULL
);
1137 strvec_push(&child
.args
, "--no-progress");
1139 if (run_command(&child
))
1140 return error(_("failed to write multi-pack-index"));
1145 static int multi_pack_index_expire(struct maintenance_run_opts
*opts
)
1147 struct child_process child
= CHILD_PROCESS_INIT
;
1149 child
.git_cmd
= child
.close_object_store
= 1;
1150 strvec_pushl(&child
.args
, "multi-pack-index", "expire", NULL
);
1153 strvec_push(&child
.args
, "--no-progress");
1155 if (run_command(&child
))
1156 return error(_("'git multi-pack-index expire' failed"));
1161 #define TWO_GIGABYTES (INT32_MAX)
1163 static off_t
get_auto_pack_size(void)
1166 * The "auto" value is special: we optimize for
1167 * one large pack-file (i.e. from a clone) and
1168 * expect the rest to be small and they can be
1171 * The strategy we select here is to select a
1172 * size that is one more than the second largest
1173 * pack-file. This ensures that we will repack
1174 * at least two packs if there are three or more
1178 off_t second_largest_size
= 0;
1180 struct packed_git
*p
;
1181 struct repository
*r
= the_repository
;
1183 reprepare_packed_git(r
);
1184 for (p
= get_all_packs(r
); p
; p
= p
->next
) {
1185 if (p
->pack_size
> max_size
) {
1186 second_largest_size
= max_size
;
1187 max_size
= p
->pack_size
;
1188 } else if (p
->pack_size
> second_largest_size
)
1189 second_largest_size
= p
->pack_size
;
1192 result_size
= second_largest_size
+ 1;
1194 /* But limit ourselves to a batch size of 2g */
1195 if (result_size
> TWO_GIGABYTES
)
1196 result_size
= TWO_GIGABYTES
;
1201 static int multi_pack_index_repack(struct maintenance_run_opts
*opts
)
1203 struct child_process child
= CHILD_PROCESS_INIT
;
1205 child
.git_cmd
= child
.close_object_store
= 1;
1206 strvec_pushl(&child
.args
, "multi-pack-index", "repack", NULL
);
1209 strvec_push(&child
.args
, "--no-progress");
1211 strvec_pushf(&child
.args
, "--batch-size=%"PRIuMAX
,
1212 (uintmax_t)get_auto_pack_size());
1214 if (run_command(&child
))
1215 return error(_("'git multi-pack-index repack' failed"));
1220 static int maintenance_task_incremental_repack(struct maintenance_run_opts
*opts
)
1222 prepare_repo_settings(the_repository
);
1223 if (!the_repository
->settings
.core_multi_pack_index
) {
1224 warning(_("skipping incremental-repack task because core.multiPackIndex is disabled"));
1228 if (multi_pack_index_write(opts
))
1230 if (multi_pack_index_expire(opts
))
1232 if (multi_pack_index_repack(opts
))
1237 typedef int maintenance_task_fn(struct maintenance_run_opts
*opts
);
1240 * An auto condition function returns 1 if the task should run
1241 * and 0 if the task should NOT run. See needs_to_gc() for an
1244 typedef int maintenance_auto_fn(void);
1246 struct maintenance_task
{
1248 maintenance_task_fn
*fn
;
1249 maintenance_auto_fn
*auto_condition
;
1252 enum schedule_priority schedule
;
1254 /* -1 if not selected. */
1258 enum maintenance_task_label
{
1261 TASK_INCREMENTAL_REPACK
,
1266 /* Leave as final value */
1270 static struct maintenance_task tasks
[] = {
1273 maintenance_task_prefetch
,
1275 [TASK_LOOSE_OBJECTS
] = {
1277 maintenance_task_loose_objects
,
1278 loose_object_auto_condition
,
1280 [TASK_INCREMENTAL_REPACK
] = {
1281 "incremental-repack",
1282 maintenance_task_incremental_repack
,
1283 incremental_repack_auto_condition
,
1287 maintenance_task_gc
,
1291 [TASK_COMMIT_GRAPH
] = {
1293 maintenance_task_commit_graph
,
1294 should_write_commit_graph
,
1296 [TASK_PACK_REFS
] = {
1298 maintenance_task_pack_refs
,
1303 static int compare_tasks_by_selection(const void *a_
, const void *b_
)
1305 const struct maintenance_task
*a
= a_
;
1306 const struct maintenance_task
*b
= b_
;
1308 return b
->selected_order
- a
->selected_order
;
1311 static int maintenance_run_tasks(struct maintenance_run_opts
*opts
)
1313 int i
, found_selected
= 0;
1315 struct lock_file lk
;
1316 struct repository
*r
= the_repository
;
1317 char *lock_path
= xstrfmt("%s/maintenance", r
->objects
->odb
->path
);
1319 if (hold_lock_file_for_update(&lk
, lock_path
, LOCK_NO_DEREF
) < 0) {
1321 * Another maintenance command is running.
1323 * If --auto was provided, then it is likely due to a
1324 * recursive process stack. Do not report an error in
1327 if (!opts
->auto_flag
&& !opts
->quiet
)
1328 warning(_("lock file '%s' exists, skipping maintenance"),
1335 for (i
= 0; !found_selected
&& i
< TASK__COUNT
; i
++)
1336 found_selected
= tasks
[i
].selected_order
>= 0;
1339 QSORT(tasks
, TASK__COUNT
, compare_tasks_by_selection
);
1341 for (i
= 0; i
< TASK__COUNT
; i
++) {
1342 if (found_selected
&& tasks
[i
].selected_order
< 0)
1345 if (!found_selected
&& !tasks
[i
].enabled
)
1348 if (opts
->auto_flag
&&
1349 (!tasks
[i
].auto_condition
||
1350 !tasks
[i
].auto_condition()))
1353 if (opts
->schedule
&& tasks
[i
].schedule
< opts
->schedule
)
1356 trace2_region_enter("maintenance", tasks
[i
].name
, r
);
1357 if (tasks
[i
].fn(opts
)) {
1358 error(_("task '%s' failed"), tasks
[i
].name
);
1361 trace2_region_leave("maintenance", tasks
[i
].name
, r
);
1364 rollback_lock_file(&lk
);
1368 static void initialize_maintenance_strategy(void)
1372 if (git_config_get_string("maintenance.strategy", &config_str
))
1375 if (!strcasecmp(config_str
, "incremental")) {
1376 tasks
[TASK_GC
].schedule
= SCHEDULE_NONE
;
1377 tasks
[TASK_COMMIT_GRAPH
].enabled
= 1;
1378 tasks
[TASK_COMMIT_GRAPH
].schedule
= SCHEDULE_HOURLY
;
1379 tasks
[TASK_PREFETCH
].enabled
= 1;
1380 tasks
[TASK_PREFETCH
].schedule
= SCHEDULE_HOURLY
;
1381 tasks
[TASK_INCREMENTAL_REPACK
].enabled
= 1;
1382 tasks
[TASK_INCREMENTAL_REPACK
].schedule
= SCHEDULE_DAILY
;
1383 tasks
[TASK_LOOSE_OBJECTS
].enabled
= 1;
1384 tasks
[TASK_LOOSE_OBJECTS
].schedule
= SCHEDULE_DAILY
;
1385 tasks
[TASK_PACK_REFS
].enabled
= 1;
1386 tasks
[TASK_PACK_REFS
].schedule
= SCHEDULE_WEEKLY
;
1390 static void initialize_task_config(int schedule
)
1393 struct strbuf config_name
= STRBUF_INIT
;
1397 initialize_maintenance_strategy();
1399 for (i
= 0; i
< TASK__COUNT
; i
++) {
1403 strbuf_reset(&config_name
);
1404 strbuf_addf(&config_name
, "maintenance.%s.enabled",
1407 if (!git_config_get_bool(config_name
.buf
, &config_value
))
1408 tasks
[i
].enabled
= config_value
;
1410 strbuf_reset(&config_name
);
1411 strbuf_addf(&config_name
, "maintenance.%s.schedule",
1414 if (!git_config_get_string(config_name
.buf
, &config_str
)) {
1415 tasks
[i
].schedule
= parse_schedule(config_str
);
1420 strbuf_release(&config_name
);
1423 static int task_option_parse(const struct option
*opt UNUSED
,
1424 const char *arg
, int unset
)
1426 int i
, num_selected
= 0;
1427 struct maintenance_task
*task
= NULL
;
1429 BUG_ON_OPT_NEG(unset
);
1431 for (i
= 0; i
< TASK__COUNT
; i
++) {
1432 if (tasks
[i
].selected_order
>= 0)
1434 if (!strcasecmp(tasks
[i
].name
, arg
)) {
1440 error(_("'%s' is not a valid task"), arg
);
1444 if (task
->selected_order
>= 0) {
1445 error(_("task '%s' cannot be selected multiple times"), arg
);
1449 task
->selected_order
= num_selected
+ 1;
1454 static int maintenance_run(int argc
, const char **argv
, const char *prefix
)
1457 struct maintenance_run_opts opts
;
1458 struct option builtin_maintenance_run_options
[] = {
1459 OPT_BOOL(0, "auto", &opts
.auto_flag
,
1460 N_("run tasks based on the state of the repository")),
1461 OPT_CALLBACK(0, "schedule", &opts
.schedule
, N_("frequency"),
1462 N_("run tasks based on frequency"),
1463 maintenance_opt_schedule
),
1464 OPT_BOOL(0, "quiet", &opts
.quiet
,
1465 N_("do not report progress or other information over stderr")),
1466 OPT_CALLBACK_F(0, "task", NULL
, N_("task"),
1467 N_("run a specific task"),
1468 PARSE_OPT_NONEG
, task_option_parse
),
1471 memset(&opts
, 0, sizeof(opts
));
1473 opts
.quiet
= !isatty(2);
1475 for (i
= 0; i
< TASK__COUNT
; i
++)
1476 tasks
[i
].selected_order
= -1;
1478 argc
= parse_options(argc
, argv
, prefix
,
1479 builtin_maintenance_run_options
,
1480 builtin_maintenance_run_usage
,
1481 PARSE_OPT_STOP_AT_NON_OPTION
);
1483 if (opts
.auto_flag
&& opts
.schedule
)
1484 die(_("use at most one of --auto and --schedule=<frequency>"));
1486 initialize_task_config(opts
.schedule
);
1489 usage_with_options(builtin_maintenance_run_usage
,
1490 builtin_maintenance_run_options
);
1491 return maintenance_run_tasks(&opts
);
1494 static char *get_maintpath(void)
1496 struct strbuf sb
= STRBUF_INIT
;
1497 const char *p
= the_repository
->worktree
?
1498 the_repository
->worktree
: the_repository
->gitdir
;
1500 strbuf_realpath(&sb
, p
, 1);
1501 return strbuf_detach(&sb
, NULL
);
1504 static char const * const builtin_maintenance_register_usage
[] = {
1505 "git maintenance register [--config-file <path>]",
1509 static int maintenance_register(int argc
, const char **argv
, const char *prefix
)
1511 char *config_file
= NULL
;
1512 struct option options
[] = {
1513 OPT_STRING(0, "config-file", &config_file
, N_("file"), N_("use given config file")),
1517 const char *key
= "maintenance.repo";
1518 char *maintpath
= get_maintpath();
1519 struct string_list_item
*item
;
1520 const struct string_list
*list
;
1522 argc
= parse_options(argc
, argv
, prefix
, options
,
1523 builtin_maintenance_register_usage
, 0);
1525 usage_with_options(builtin_maintenance_register_usage
,
1528 /* Disable foreground maintenance */
1529 git_config_set("maintenance.auto", "false");
1531 /* Set maintenance strategy, if unset */
1532 if (git_config_get("maintenance.strategy"))
1533 git_config_set("maintenance.strategy", "incremental");
1535 if (!git_config_get_string_multi(key
, &list
)) {
1536 for_each_string_list_item(item
, list
) {
1537 if (!strcmp(maintpath
, item
->string
)) {
1546 char *user_config
= NULL
, *xdg_config
= NULL
;
1549 git_global_config(&user_config
, &xdg_config
);
1550 config_file
= user_config
;
1552 die(_("$HOME not set"));
1554 rc
= git_config_set_multivar_in_file_gently(
1555 config_file
, "maintenance.repo", maintpath
,
1556 CONFIG_REGEX_NONE
, 0);
1561 die(_("unable to add '%s' value of '%s'"),
1569 static char const * const builtin_maintenance_unregister_usage
[] = {
1570 "git maintenance unregister [--config-file <path>] [--force]",
1574 static int maintenance_unregister(int argc
, const char **argv
, const char *prefix
)
1577 char *config_file
= NULL
;
1578 struct option options
[] = {
1579 OPT_STRING(0, "config-file", &config_file
, N_("file"), N_("use given config file")),
1581 N_("return success even if repository was not registered"),
1582 PARSE_OPT_NOCOMPLETE
),
1585 const char *key
= "maintenance.repo";
1586 char *maintpath
= get_maintpath();
1588 struct string_list_item
*item
;
1589 const struct string_list
*list
;
1590 struct config_set cs
= { { 0 } };
1592 argc
= parse_options(argc
, argv
, prefix
, options
,
1593 builtin_maintenance_unregister_usage
, 0);
1595 usage_with_options(builtin_maintenance_unregister_usage
,
1599 git_configset_init(&cs
);
1600 git_configset_add_file(&cs
, config_file
);
1603 ? git_configset_get_string_multi(&cs
, key
, &list
)
1604 : git_config_get_string_multi(key
, &list
))) {
1605 for_each_string_list_item(item
, list
) {
1606 if (!strcmp(maintpath
, item
->string
)) {
1615 char *user_config
= NULL
, *xdg_config
= NULL
;
1617 git_global_config(&user_config
, &xdg_config
);
1618 config_file
= user_config
;
1620 die(_("$HOME not set"));
1622 rc
= git_config_set_multivar_in_file_gently(
1623 config_file
, key
, NULL
, maintpath
,
1624 CONFIG_FLAGS_MULTI_REPLACE
| CONFIG_FLAGS_FIXED_VALUE
);
1629 (!force
|| rc
== CONFIG_NOTHING_SET
))
1630 die(_("unable to unset '%s' value of '%s'"),
1632 } else if (!force
) {
1633 die(_("repository '%s' is not registered"), maintpath
);
1636 git_configset_clear(&cs
);
1641 static const char *get_frequency(enum schedule_priority schedule
)
1644 case SCHEDULE_HOURLY
:
1646 case SCHEDULE_DAILY
:
1648 case SCHEDULE_WEEKLY
:
1651 BUG("invalid schedule %d", schedule
);
1656 * get_schedule_cmd` reads the GIT_TEST_MAINT_SCHEDULER environment variable
1657 * to mock the schedulers that `git maintenance start` rely on.
1659 * For test purpose, GIT_TEST_MAINT_SCHEDULER can be set to a comma-separated
1660 * list of colon-separated key/value pairs where each pair contains a scheduler
1661 * and its corresponding mock.
1663 * * If $GIT_TEST_MAINT_SCHEDULER is not set, return false and leave the
1664 * arguments unmodified.
1666 * * If $GIT_TEST_MAINT_SCHEDULER is set, return true.
1667 * In this case, the *cmd value is read as input.
1669 * * if the input value *cmd is the key of one of the comma-separated list
1670 * item, then *is_available is set to true and *cmd is modified and becomes
1673 * * if the input value *cmd isn’t the key of any of the comma-separated list
1674 * item, then *is_available is set to false.
1677 * GIT_TEST_MAINT_SCHEDULER not set
1678 * +-------+-------------------------------------------------+
1679 * | Input | Output |
1680 * | *cmd | return code | *cmd | *is_available |
1681 * +-------+-------------+-------------------+---------------+
1682 * | "foo" | false | "foo" (unchanged) | (unchanged) |
1683 * +-------+-------------+-------------------+---------------+
1685 * GIT_TEST_MAINT_SCHEDULER set to “foo:./mock_foo.sh,bar:./mock_bar.sh”
1686 * +-------+-------------------------------------------------+
1687 * | Input | Output |
1688 * | *cmd | return code | *cmd | *is_available |
1689 * +-------+-------------+-------------------+---------------+
1690 * | "foo" | true | "./mock.foo.sh" | true |
1691 * | "qux" | true | "qux" (unchanged) | false |
1692 * +-------+-------------+-------------------+---------------+
1694 static int get_schedule_cmd(const char **cmd
, int *is_available
)
1696 char *testing
= xstrdup_or_null(getenv("GIT_TEST_MAINT_SCHEDULER"));
1697 struct string_list_item
*item
;
1698 struct string_list list
= STRING_LIST_INIT_NODUP
;
1706 string_list_split_in_place(&list
, testing
, ",", -1);
1707 for_each_string_list_item(item
, &list
) {
1708 struct string_list pair
= STRING_LIST_INIT_NODUP
;
1710 if (string_list_split_in_place(&pair
, item
->string
, ":", 2) != 2)
1713 if (!strcmp(*cmd
, pair
.items
[0].string
)) {
1714 *cmd
= pair
.items
[1].string
;
1717 string_list_clear(&list
, 0);
1723 string_list_clear(&list
, 0);
1728 static int get_random_minute(void)
1730 /* Use a static value when under tests. */
1731 if (getenv("GIT_TEST_MAINT_SCHEDULER"))
1734 return git_rand() % 60;
1737 static int is_launchctl_available(void)
1739 const char *cmd
= "launchctl";
1741 if (get_schedule_cmd(&cmd
, &is_available
))
1742 return is_available
;
1751 static char *launchctl_service_name(const char *frequency
)
1753 struct strbuf label
= STRBUF_INIT
;
1754 strbuf_addf(&label
, "org.git-scm.git.%s", frequency
);
1755 return strbuf_detach(&label
, NULL
);
1758 static char *launchctl_service_filename(const char *name
)
1761 struct strbuf filename
= STRBUF_INIT
;
1762 strbuf_addf(&filename
, "~/Library/LaunchAgents/%s.plist", name
);
1764 expanded
= interpolate_path(filename
.buf
, 1);
1766 die(_("failed to expand path '%s'"), filename
.buf
);
1768 strbuf_release(&filename
);
1772 static char *launchctl_get_uid(void)
1774 return xstrfmt("gui/%d", getuid());
1777 static int launchctl_boot_plist(int enable
, const char *filename
)
1779 const char *cmd
= "launchctl";
1781 struct child_process child
= CHILD_PROCESS_INIT
;
1782 char *uid
= launchctl_get_uid();
1784 get_schedule_cmd(&cmd
, NULL
);
1785 strvec_split(&child
.args
, cmd
);
1786 strvec_pushl(&child
.args
, enable
? "bootstrap" : "bootout", uid
,
1789 child
.no_stderr
= 1;
1790 child
.no_stdout
= 1;
1792 if (start_command(&child
))
1793 die(_("failed to start launchctl"));
1795 result
= finish_command(&child
);
1801 static int launchctl_remove_plist(enum schedule_priority schedule
)
1803 const char *frequency
= get_frequency(schedule
);
1804 char *name
= launchctl_service_name(frequency
);
1805 char *filename
= launchctl_service_filename(name
);
1806 int result
= launchctl_boot_plist(0, filename
);
1813 static int launchctl_remove_plists(void)
1815 return launchctl_remove_plist(SCHEDULE_HOURLY
) ||
1816 launchctl_remove_plist(SCHEDULE_DAILY
) ||
1817 launchctl_remove_plist(SCHEDULE_WEEKLY
);
1820 static int launchctl_list_contains_plist(const char *name
, const char *cmd
)
1822 struct child_process child
= CHILD_PROCESS_INIT
;
1824 strvec_split(&child
.args
, cmd
);
1825 strvec_pushl(&child
.args
, "list", name
, NULL
);
1827 child
.no_stderr
= 1;
1828 child
.no_stdout
= 1;
1830 if (start_command(&child
))
1831 die(_("failed to start launchctl"));
1833 /* Returns failure if 'name' doesn't exist. */
1834 return !finish_command(&child
);
1837 static int launchctl_schedule_plist(const char *exec_path
, enum schedule_priority schedule
)
1840 const char *preamble
, *repeat
;
1841 const char *frequency
= get_frequency(schedule
);
1842 char *name
= launchctl_service_name(frequency
);
1843 char *filename
= launchctl_service_filename(name
);
1844 struct lock_file lk
= LOCK_INIT
;
1845 static unsigned long lock_file_timeout_ms
= ULONG_MAX
;
1846 struct strbuf plist
= STRBUF_INIT
, plist2
= STRBUF_INIT
;
1848 const char *cmd
= "launchctl";
1849 int minute
= get_random_minute();
1851 get_schedule_cmd(&cmd
, NULL
);
1852 preamble
= "<?xml version=\"1.0\"?>\n"
1853 "<!DOCTYPE plist PUBLIC \"-//Apple//DTD PLIST 1.0//EN\" \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">\n"
1854 "<plist version=\"1.0\">"
1856 "<key>Label</key><string>%s</string>\n"
1857 "<key>ProgramArguments</key>\n"
1859 "<string>%s/git</string>\n"
1860 "<string>--exec-path=%s</string>\n"
1861 "<string>for-each-repo</string>\n"
1862 "<string>--config=maintenance.repo</string>\n"
1863 "<string>maintenance</string>\n"
1864 "<string>run</string>\n"
1865 "<string>--schedule=%s</string>\n"
1867 "<key>StartCalendarInterval</key>\n"
1869 strbuf_addf(&plist
, preamble
, name
, exec_path
, exec_path
, frequency
);
1872 case SCHEDULE_HOURLY
:
1874 "<key>Hour</key><integer>%d</integer>\n"
1875 "<key>Minute</key><integer>%d</integer>\n"
1877 for (i
= 1; i
<= 23; i
++)
1878 strbuf_addf(&plist
, repeat
, i
, minute
);
1881 case SCHEDULE_DAILY
:
1883 "<key>Day</key><integer>%d</integer>\n"
1884 "<key>Hour</key><integer>0</integer>\n"
1885 "<key>Minute</key><integer>%d</integer>\n"
1887 for (i
= 1; i
<= 6; i
++)
1888 strbuf_addf(&plist
, repeat
, i
, minute
);
1891 case SCHEDULE_WEEKLY
:
1894 "<key>Day</key><integer>0</integer>\n"
1895 "<key>Hour</key><integer>0</integer>\n"
1896 "<key>Minute</key><integer>%d</integer>\n"
1905 strbuf_addstr(&plist
, "</array>\n</dict>\n</plist>\n");
1907 if (safe_create_leading_directories(filename
))
1908 die(_("failed to create directories for '%s'"), filename
);
1910 if ((long)lock_file_timeout_ms
< 0 &&
1911 git_config_get_ulong("gc.launchctlplistlocktimeoutms",
1912 &lock_file_timeout_ms
))
1913 lock_file_timeout_ms
= 150;
1915 fd
= hold_lock_file_for_update_timeout(&lk
, filename
, LOCK_DIE_ON_ERROR
,
1916 lock_file_timeout_ms
);
1919 * Does this file already exist? With the intended contents? Is it
1920 * registered already? Then it does not need to be re-registered.
1922 if (!stat(filename
, &st
) && st
.st_size
== plist
.len
&&
1923 strbuf_read_file(&plist2
, filename
, plist
.len
) == plist
.len
&&
1924 !strbuf_cmp(&plist
, &plist2
) &&
1925 launchctl_list_contains_plist(name
, cmd
))
1926 rollback_lock_file(&lk
);
1928 if (write_in_full(fd
, plist
.buf
, plist
.len
) < 0 ||
1929 commit_lock_file(&lk
))
1930 die_errno(_("could not write '%s'"), filename
);
1932 /* bootout might fail if not already running, so ignore */
1933 launchctl_boot_plist(0, filename
);
1934 if (launchctl_boot_plist(1, filename
))
1935 die(_("failed to bootstrap service %s"), filename
);
1940 strbuf_release(&plist
);
1941 strbuf_release(&plist2
);
1945 static int launchctl_add_plists(void)
1947 const char *exec_path
= git_exec_path();
1949 return launchctl_schedule_plist(exec_path
, SCHEDULE_HOURLY
) ||
1950 launchctl_schedule_plist(exec_path
, SCHEDULE_DAILY
) ||
1951 launchctl_schedule_plist(exec_path
, SCHEDULE_WEEKLY
);
1954 static int launchctl_update_schedule(int run_maintenance
, int fd UNUSED
)
1956 if (run_maintenance
)
1957 return launchctl_add_plists();
1959 return launchctl_remove_plists();
1962 static int is_schtasks_available(void)
1964 const char *cmd
= "schtasks";
1966 if (get_schedule_cmd(&cmd
, &is_available
))
1967 return is_available
;
1969 #ifdef GIT_WINDOWS_NATIVE
1976 static char *schtasks_task_name(const char *frequency
)
1978 struct strbuf label
= STRBUF_INIT
;
1979 strbuf_addf(&label
, "Git Maintenance (%s)", frequency
);
1980 return strbuf_detach(&label
, NULL
);
1983 static int schtasks_remove_task(enum schedule_priority schedule
)
1985 const char *cmd
= "schtasks";
1986 struct child_process child
= CHILD_PROCESS_INIT
;
1987 const char *frequency
= get_frequency(schedule
);
1988 char *name
= schtasks_task_name(frequency
);
1990 get_schedule_cmd(&cmd
, NULL
);
1991 strvec_split(&child
.args
, cmd
);
1992 strvec_pushl(&child
.args
, "/delete", "/tn", name
, "/f", NULL
);
1995 return run_command(&child
);
1998 static int schtasks_remove_tasks(void)
2000 return schtasks_remove_task(SCHEDULE_HOURLY
) ||
2001 schtasks_remove_task(SCHEDULE_DAILY
) ||
2002 schtasks_remove_task(SCHEDULE_WEEKLY
);
2005 static int schtasks_schedule_task(const char *exec_path
, enum schedule_priority schedule
)
2007 const char *cmd
= "schtasks";
2009 struct child_process child
= CHILD_PROCESS_INIT
;
2011 struct tempfile
*tfile
;
2012 const char *frequency
= get_frequency(schedule
);
2013 char *name
= schtasks_task_name(frequency
);
2014 struct strbuf tfilename
= STRBUF_INIT
;
2015 int minute
= get_random_minute();
2017 get_schedule_cmd(&cmd
, NULL
);
2019 strbuf_addf(&tfilename
, "%s/schedule_%s_XXXXXX",
2020 get_git_common_dir(), frequency
);
2021 tfile
= xmks_tempfile(tfilename
.buf
);
2022 strbuf_release(&tfilename
);
2024 if (!fdopen_tempfile(tfile
, "w"))
2025 die(_("failed to create temp xml file"));
2027 xml
= "<?xml version=\"1.0\" ?>\n"
2028 "<Task version=\"1.4\" xmlns=\"http://schemas.microsoft.com/windows/2004/02/mit/task\">\n"
2030 "<CalendarTrigger>\n";
2031 fputs(xml
, tfile
->fp
);
2034 case SCHEDULE_HOURLY
:
2036 "<StartBoundary>2020-01-01T01:%02d:00</StartBoundary>\n"
2037 "<Enabled>true</Enabled>\n"
2039 "<DaysInterval>1</DaysInterval>\n"
2040 "</ScheduleByDay>\n"
2042 "<Interval>PT1H</Interval>\n"
2043 "<Duration>PT23H</Duration>\n"
2044 "<StopAtDurationEnd>false</StopAtDurationEnd>\n"
2049 case SCHEDULE_DAILY
:
2051 "<StartBoundary>2020-01-01T00:%02d:00</StartBoundary>\n"
2052 "<Enabled>true</Enabled>\n"
2053 "<ScheduleByWeek>\n"
2062 "<WeeksInterval>1</WeeksInterval>\n"
2063 "</ScheduleByWeek>\n",
2067 case SCHEDULE_WEEKLY
:
2069 "<StartBoundary>2020-01-01T00:%02d:00</StartBoundary>\n"
2070 "<Enabled>true</Enabled>\n"
2071 "<ScheduleByWeek>\n"
2075 "<WeeksInterval>1</WeeksInterval>\n"
2076 "</ScheduleByWeek>\n",
2084 xml
= "</CalendarTrigger>\n"
2087 "<Principal id=\"Author\">\n"
2088 "<LogonType>InteractiveToken</LogonType>\n"
2089 "<RunLevel>LeastPrivilege</RunLevel>\n"
2093 "<MultipleInstancesPolicy>IgnoreNew</MultipleInstancesPolicy>\n"
2094 "<Enabled>true</Enabled>\n"
2095 "<Hidden>true</Hidden>\n"
2096 "<UseUnifiedSchedulingEngine>true</UseUnifiedSchedulingEngine>\n"
2097 "<WakeToRun>false</WakeToRun>\n"
2098 "<ExecutionTimeLimit>PT72H</ExecutionTimeLimit>\n"
2099 "<Priority>7</Priority>\n"
2101 "<Actions Context=\"Author\">\n"
2103 "<Command>\"%s\\headless-git.exe\"</Command>\n"
2104 "<Arguments>--exec-path=\"%s\" for-each-repo --config=maintenance.repo maintenance run --schedule=%s</Arguments>\n"
2108 fprintf(tfile
->fp
, xml
, exec_path
, exec_path
, frequency
);
2109 strvec_split(&child
.args
, cmd
);
2110 strvec_pushl(&child
.args
, "/create", "/tn", name
, "/f", "/xml",
2111 get_tempfile_path(tfile
), NULL
);
2112 close_tempfile_gently(tfile
);
2114 child
.no_stdout
= 1;
2115 child
.no_stderr
= 1;
2117 if (start_command(&child
))
2118 die(_("failed to start schtasks"));
2119 result
= finish_command(&child
);
2121 delete_tempfile(&tfile
);
2126 static int schtasks_schedule_tasks(void)
2128 const char *exec_path
= git_exec_path();
2130 return schtasks_schedule_task(exec_path
, SCHEDULE_HOURLY
) ||
2131 schtasks_schedule_task(exec_path
, SCHEDULE_DAILY
) ||
2132 schtasks_schedule_task(exec_path
, SCHEDULE_WEEKLY
);
2135 static int schtasks_update_schedule(int run_maintenance
, int fd UNUSED
)
2137 if (run_maintenance
)
2138 return schtasks_schedule_tasks();
2140 return schtasks_remove_tasks();
2144 static int check_crontab_process(const char *cmd
)
2146 struct child_process child
= CHILD_PROCESS_INIT
;
2148 strvec_split(&child
.args
, cmd
);
2149 strvec_push(&child
.args
, "-l");
2151 child
.no_stdout
= 1;
2152 child
.no_stderr
= 1;
2153 child
.silent_exec_failure
= 1;
2155 if (start_command(&child
))
2157 /* Ignore exit code, as an empty crontab will return error. */
2158 finish_command(&child
);
2162 static int is_crontab_available(void)
2164 const char *cmd
= "crontab";
2167 if (get_schedule_cmd(&cmd
, &is_available
))
2168 return is_available
;
2172 * macOS has cron, but it requires special permissions and will
2173 * create a UI alert when attempting to run this command.
2177 return check_crontab_process(cmd
);
2181 #define BEGIN_LINE "# BEGIN GIT MAINTENANCE SCHEDULE"
2182 #define END_LINE "# END GIT MAINTENANCE SCHEDULE"
2184 static int crontab_update_schedule(int run_maintenance
, int fd
)
2186 const char *cmd
= "crontab";
2188 int in_old_region
= 0;
2189 struct child_process crontab_list
= CHILD_PROCESS_INIT
;
2190 struct child_process crontab_edit
= CHILD_PROCESS_INIT
;
2191 FILE *cron_list
, *cron_in
;
2192 struct strbuf line
= STRBUF_INIT
;
2193 struct tempfile
*tmpedit
= NULL
;
2194 int minute
= get_random_minute();
2196 get_schedule_cmd(&cmd
, NULL
);
2197 strvec_split(&crontab_list
.args
, cmd
);
2198 strvec_push(&crontab_list
.args
, "-l");
2199 crontab_list
.in
= -1;
2200 crontab_list
.out
= dup(fd
);
2201 crontab_list
.git_cmd
= 0;
2203 if (start_command(&crontab_list
))
2204 return error(_("failed to run 'crontab -l'; your system might not support 'cron'"));
2206 /* Ignore exit code, as an empty crontab will return error. */
2207 finish_command(&crontab_list
);
2209 tmpedit
= mks_tempfile_t(".git_cron_edit_tmpXXXXXX");
2211 result
= error(_("failed to create crontab temporary file"));
2214 cron_in
= fdopen_tempfile(tmpedit
, "w");
2216 result
= error(_("failed to open temporary file"));
2221 * Read from the .lock file, filtering out the old
2222 * schedule while appending the new schedule.
2224 cron_list
= fdopen(fd
, "r");
2227 while (!strbuf_getline_lf(&line
, cron_list
)) {
2228 if (!in_old_region
&& !strcmp(line
.buf
, BEGIN_LINE
))
2230 else if (in_old_region
&& !strcmp(line
.buf
, END_LINE
))
2232 else if (!in_old_region
)
2233 fprintf(cron_in
, "%s\n", line
.buf
);
2235 strbuf_release(&line
);
2237 if (run_maintenance
) {
2238 struct strbuf line_format
= STRBUF_INIT
;
2239 const char *exec_path
= git_exec_path();
2241 fprintf(cron_in
, "%s\n", BEGIN_LINE
);
2243 "# The following schedule was created by Git\n");
2244 fprintf(cron_in
, "# Any edits made in this region might be\n");
2246 "# replaced in the future by a Git command.\n\n");
2248 strbuf_addf(&line_format
,
2249 "%%d %%s * * %%s \"%s/git\" --exec-path=\"%s\" for-each-repo --config=maintenance.repo maintenance run --schedule=%%s\n",
2250 exec_path
, exec_path
);
2251 fprintf(cron_in
, line_format
.buf
, minute
, "1-23", "*", "hourly");
2252 fprintf(cron_in
, line_format
.buf
, minute
, "0", "1-6", "daily");
2253 fprintf(cron_in
, line_format
.buf
, minute
, "0", "0", "weekly");
2254 strbuf_release(&line_format
);
2256 fprintf(cron_in
, "\n%s\n", END_LINE
);
2261 strvec_split(&crontab_edit
.args
, cmd
);
2262 strvec_push(&crontab_edit
.args
, get_tempfile_path(tmpedit
));
2263 crontab_edit
.git_cmd
= 0;
2265 if (start_command(&crontab_edit
)) {
2266 result
= error(_("failed to run 'crontab'; your system might not support 'cron'"));
2270 if (finish_command(&crontab_edit
))
2271 result
= error(_("'crontab' died"));
2275 delete_tempfile(&tmpedit
);
2279 static int real_is_systemd_timer_available(void)
2281 struct child_process child
= CHILD_PROCESS_INIT
;
2283 strvec_pushl(&child
.args
, "systemctl", "--user", "list-timers", NULL
);
2285 child
.no_stdout
= 1;
2286 child
.no_stderr
= 1;
2287 child
.silent_exec_failure
= 1;
2289 if (start_command(&child
))
2291 if (finish_command(&child
))
2296 static int is_systemd_timer_available(void)
2298 const char *cmd
= "systemctl";
2301 if (get_schedule_cmd(&cmd
, &is_available
))
2302 return is_available
;
2304 return real_is_systemd_timer_available();
2307 static char *xdg_config_home_systemd(const char *filename
)
2309 return xdg_config_home_for("systemd/user", filename
);
2312 #define SYSTEMD_UNIT_FORMAT "git-maintenance@%s.%s"
2314 static int systemd_timer_delete_timer_file(enum schedule_priority priority
)
2317 const char *frequency
= get_frequency(priority
);
2318 char *local_timer_name
= xstrfmt(SYSTEMD_UNIT_FORMAT
, frequency
, "timer");
2319 char *filename
= xdg_config_home_systemd(local_timer_name
);
2321 if (unlink(filename
) && !is_missing_file_error(errno
))
2322 ret
= error_errno(_("failed to delete '%s'"), filename
);
2325 free(local_timer_name
);
2329 static int systemd_timer_delete_service_template(void)
2332 char *local_service_name
= xstrfmt(SYSTEMD_UNIT_FORMAT
, "", "service");
2333 char *filename
= xdg_config_home_systemd(local_service_name
);
2334 if (unlink(filename
) && !is_missing_file_error(errno
))
2335 ret
= error_errno(_("failed to delete '%s'"), filename
);
2338 free(local_service_name
);
2343 * Write the schedule information into a git-maintenance@<schedule>.timer
2344 * file using a custom minute. This timer file cannot use the templating
2345 * system, so we generate a specific file for each.
2347 static int systemd_timer_write_timer_file(enum schedule_priority schedule
,
2354 char *schedule_pattern
= NULL
;
2355 const char *frequency
= get_frequency(schedule
);
2356 char *local_timer_name
= xstrfmt(SYSTEMD_UNIT_FORMAT
, frequency
, "timer");
2358 filename
= xdg_config_home_systemd(local_timer_name
);
2360 if (safe_create_leading_directories(filename
)) {
2361 error(_("failed to create directories for '%s'"), filename
);
2364 file
= fopen_or_warn(filename
, "w");
2369 case SCHEDULE_HOURLY
:
2370 schedule_pattern
= xstrfmt("*-*-* 1..23:%02d:00", minute
);
2373 case SCHEDULE_DAILY
:
2374 schedule_pattern
= xstrfmt("Tue..Sun *-*-* 0:%02d:00", minute
);
2377 case SCHEDULE_WEEKLY
:
2378 schedule_pattern
= xstrfmt("Mon 0:%02d:00", minute
);
2382 BUG("Unhandled schedule_priority");
2385 unit
= "# This file was created and is maintained by Git.\n"
2386 "# Any edits made in this file might be replaced in the future\n"
2387 "# by a Git command.\n"
2390 "Description=Optimize Git repositories data\n"
2397 "WantedBy=timers.target\n";
2398 if (fprintf(file
, unit
, schedule_pattern
) < 0) {
2399 error(_("failed to write to '%s'"), filename
);
2403 if (fclose(file
) == EOF
) {
2404 error_errno(_("failed to flush '%s'"), filename
);
2411 free(schedule_pattern
);
2412 free(local_timer_name
);
2418 * No matter the schedule, we use the same service and can make use of the
2419 * templating system. When installing git-maintenance@<schedule>.timer,
2420 * systemd will notice that git-maintenance@.service exists as a template
2421 * and will use this file and insert the <schedule> into the template at
2422 * the position of "%i".
2424 static int systemd_timer_write_service_template(const char *exec_path
)
2430 char *local_service_name
= xstrfmt(SYSTEMD_UNIT_FORMAT
, "", "service");
2432 filename
= xdg_config_home_systemd(local_service_name
);
2433 if (safe_create_leading_directories(filename
)) {
2434 error(_("failed to create directories for '%s'"), filename
);
2437 file
= fopen_or_warn(filename
, "w");
2441 unit
= "# This file was created and is maintained by Git.\n"
2442 "# Any edits made in this file might be replaced in the future\n"
2443 "# by a Git command.\n"
2446 "Description=Optimize Git repositories data\n"
2450 "ExecStart=\"%s/git\" --exec-path=\"%s\" for-each-repo --config=maintenance.repo maintenance run --schedule=%%i\n"
2451 "LockPersonality=yes\n"
2452 "MemoryDenyWriteExecute=yes\n"
2453 "NoNewPrivileges=yes\n"
2454 "RestrictAddressFamilies=AF_UNIX AF_INET AF_INET6 AF_VSOCK\n"
2455 "RestrictNamespaces=yes\n"
2456 "RestrictRealtime=yes\n"
2457 "RestrictSUIDSGID=yes\n"
2458 "SystemCallArchitectures=native\n"
2459 "SystemCallFilter=@system-service\n";
2460 if (fprintf(file
, unit
, exec_path
, exec_path
) < 0) {
2461 error(_("failed to write to '%s'"), filename
);
2465 if (fclose(file
) == EOF
) {
2466 error_errno(_("failed to flush '%s'"), filename
);
2473 free(local_service_name
);
2478 static int systemd_timer_enable_unit(int enable
,
2479 enum schedule_priority schedule
,
2482 const char *cmd
= "systemctl";
2483 struct child_process child
= CHILD_PROCESS_INIT
;
2484 const char *frequency
= get_frequency(schedule
);
2487 * Disabling the systemd unit while it is already disabled makes
2488 * systemctl print an error.
2489 * Let's ignore it since it means we already are in the expected state:
2490 * the unit is disabled.
2492 * On the other hand, enabling a systemd unit which is already enabled
2493 * produces no error.
2496 child
.no_stderr
= 1;
2497 else if (systemd_timer_write_timer_file(schedule
, minute
))
2500 get_schedule_cmd(&cmd
, NULL
);
2501 strvec_split(&child
.args
, cmd
);
2502 strvec_pushl(&child
.args
, "--user", enable
? "enable" : "disable",
2504 strvec_pushf(&child
.args
, SYSTEMD_UNIT_FORMAT
, frequency
, "timer");
2506 if (start_command(&child
))
2507 return error(_("failed to start systemctl"));
2508 if (finish_command(&child
))
2510 * Disabling an already disabled systemd unit makes
2512 * Let's ignore this failure.
2514 * Enabling an enabled systemd unit doesn't fail.
2517 return error(_("failed to run systemctl"));
2522 * A previous version of Git wrote the timer units as template files.
2523 * Clean these up, if they exist.
2525 static void systemd_timer_delete_stale_timer_templates(void)
2527 char *timer_template_name
= xstrfmt(SYSTEMD_UNIT_FORMAT
, "", "timer");
2528 char *filename
= xdg_config_home_systemd(timer_template_name
);
2530 if (unlink(filename
) && !is_missing_file_error(errno
))
2531 warning(_("failed to delete '%s'"), filename
);
2534 free(timer_template_name
);
2537 static int systemd_timer_delete_unit_files(void)
2539 systemd_timer_delete_stale_timer_templates();
2541 /* Purposefully not short-circuited to make sure all are called. */
2542 return systemd_timer_delete_timer_file(SCHEDULE_HOURLY
) |
2543 systemd_timer_delete_timer_file(SCHEDULE_DAILY
) |
2544 systemd_timer_delete_timer_file(SCHEDULE_WEEKLY
) |
2545 systemd_timer_delete_service_template();
2548 static int systemd_timer_delete_units(void)
2550 int minute
= get_random_minute();
2551 /* Purposefully not short-circuited to make sure all are called. */
2552 return systemd_timer_enable_unit(0, SCHEDULE_HOURLY
, minute
) |
2553 systemd_timer_enable_unit(0, SCHEDULE_DAILY
, minute
) |
2554 systemd_timer_enable_unit(0, SCHEDULE_WEEKLY
, minute
) |
2555 systemd_timer_delete_unit_files();
2558 static int systemd_timer_setup_units(void)
2560 int minute
= get_random_minute();
2561 const char *exec_path
= git_exec_path();
2563 int ret
= systemd_timer_write_service_template(exec_path
) ||
2564 systemd_timer_enable_unit(1, SCHEDULE_HOURLY
, minute
) ||
2565 systemd_timer_enable_unit(1, SCHEDULE_DAILY
, minute
) ||
2566 systemd_timer_enable_unit(1, SCHEDULE_WEEKLY
, minute
);
2569 systemd_timer_delete_units();
2571 systemd_timer_delete_stale_timer_templates();
2576 static int systemd_timer_update_schedule(int run_maintenance
, int fd UNUSED
)
2578 if (run_maintenance
)
2579 return systemd_timer_setup_units();
2581 return systemd_timer_delete_units();
2585 SCHEDULER_INVALID
= -1,
2589 SCHEDULER_LAUNCHCTL
,
2593 static const struct {
2595 int (*is_available
)(void);
2596 int (*update_schedule
)(int run_maintenance
, int fd
);
2597 } scheduler_fn
[] = {
2598 [SCHEDULER_CRON
] = {
2600 .is_available
= is_crontab_available
,
2601 .update_schedule
= crontab_update_schedule
,
2603 [SCHEDULER_SYSTEMD
] = {
2604 .name
= "systemctl",
2605 .is_available
= is_systemd_timer_available
,
2606 .update_schedule
= systemd_timer_update_schedule
,
2608 [SCHEDULER_LAUNCHCTL
] = {
2609 .name
= "launchctl",
2610 .is_available
= is_launchctl_available
,
2611 .update_schedule
= launchctl_update_schedule
,
2613 [SCHEDULER_SCHTASKS
] = {
2615 .is_available
= is_schtasks_available
,
2616 .update_schedule
= schtasks_update_schedule
,
2620 static enum scheduler
parse_scheduler(const char *value
)
2623 return SCHEDULER_INVALID
;
2624 else if (!strcasecmp(value
, "auto"))
2625 return SCHEDULER_AUTO
;
2626 else if (!strcasecmp(value
, "cron") || !strcasecmp(value
, "crontab"))
2627 return SCHEDULER_CRON
;
2628 else if (!strcasecmp(value
, "systemd") ||
2629 !strcasecmp(value
, "systemd-timer"))
2630 return SCHEDULER_SYSTEMD
;
2631 else if (!strcasecmp(value
, "launchctl"))
2632 return SCHEDULER_LAUNCHCTL
;
2633 else if (!strcasecmp(value
, "schtasks"))
2634 return SCHEDULER_SCHTASKS
;
2636 return SCHEDULER_INVALID
;
2639 static int maintenance_opt_scheduler(const struct option
*opt
, const char *arg
,
2642 enum scheduler
*scheduler
= opt
->value
;
2644 BUG_ON_OPT_NEG(unset
);
2646 *scheduler
= parse_scheduler(arg
);
2647 if (*scheduler
== SCHEDULER_INVALID
)
2648 return error(_("unrecognized --scheduler argument '%s'"), arg
);
2652 struct maintenance_start_opts
{
2653 enum scheduler scheduler
;
2656 static enum scheduler
resolve_scheduler(enum scheduler scheduler
)
2658 if (scheduler
!= SCHEDULER_AUTO
)
2661 #if defined(__APPLE__)
2662 return SCHEDULER_LAUNCHCTL
;
2664 #elif defined(GIT_WINDOWS_NATIVE)
2665 return SCHEDULER_SCHTASKS
;
2667 #elif defined(__linux__)
2668 if (is_systemd_timer_available())
2669 return SCHEDULER_SYSTEMD
;
2670 else if (is_crontab_available())
2671 return SCHEDULER_CRON
;
2673 die(_("neither systemd timers nor crontab are available"));
2676 return SCHEDULER_CRON
;
2680 static void validate_scheduler(enum scheduler scheduler
)
2682 if (scheduler
== SCHEDULER_INVALID
)
2683 BUG("invalid scheduler");
2684 if (scheduler
== SCHEDULER_AUTO
)
2685 BUG("resolve_scheduler should have been called before");
2687 if (!scheduler_fn
[scheduler
].is_available())
2688 die(_("%s scheduler is not available"),
2689 scheduler_fn
[scheduler
].name
);
2692 static int update_background_schedule(const struct maintenance_start_opts
*opts
,
2697 struct lock_file lk
;
2698 char *lock_path
= xstrfmt("%s/schedule", the_repository
->objects
->odb
->path
);
2700 if (hold_lock_file_for_update(&lk
, lock_path
, LOCK_NO_DEREF
) < 0) {
2702 return error(_("another process is scheduling background maintenance"));
2705 for (i
= 1; i
< ARRAY_SIZE(scheduler_fn
); i
++) {
2706 if (enable
&& opts
->scheduler
== i
)
2708 if (!scheduler_fn
[i
].is_available())
2710 scheduler_fn
[i
].update_schedule(0, get_lock_file_fd(&lk
));
2714 result
= scheduler_fn
[opts
->scheduler
].update_schedule(
2715 1, get_lock_file_fd(&lk
));
2717 rollback_lock_file(&lk
);
2723 static const char *const builtin_maintenance_start_usage
[] = {
2724 N_("git maintenance start [--scheduler=<scheduler>]"),
2728 static int maintenance_start(int argc
, const char **argv
, const char *prefix
)
2730 struct maintenance_start_opts opts
= { 0 };
2731 struct option options
[] = {
2733 0, "scheduler", &opts
.scheduler
, N_("scheduler"),
2734 N_("scheduler to trigger git maintenance run"),
2735 PARSE_OPT_NONEG
, maintenance_opt_scheduler
),
2738 const char *register_args
[] = { "register", NULL
};
2740 argc
= parse_options(argc
, argv
, prefix
, options
,
2741 builtin_maintenance_start_usage
, 0);
2743 usage_with_options(builtin_maintenance_start_usage
, options
);
2745 opts
.scheduler
= resolve_scheduler(opts
.scheduler
);
2746 validate_scheduler(opts
.scheduler
);
2748 if (update_background_schedule(&opts
, 1))
2749 die(_("failed to set up maintenance schedule"));
2751 if (maintenance_register(ARRAY_SIZE(register_args
)-1, register_args
, NULL
))
2752 warning(_("failed to add repo to global config"));
2756 static const char *const builtin_maintenance_stop_usage
[] = {
2757 "git maintenance stop",
2761 static int maintenance_stop(int argc
, const char **argv
, const char *prefix
)
2763 struct option options
[] = {
2766 argc
= parse_options(argc
, argv
, prefix
, options
,
2767 builtin_maintenance_stop_usage
, 0);
2769 usage_with_options(builtin_maintenance_stop_usage
, options
);
2770 return update_background_schedule(NULL
, 0);
2773 static const char * const builtin_maintenance_usage
[] = {
2774 N_("git maintenance <subcommand> [<options>]"),
2778 int cmd_maintenance(int argc
, const char **argv
, const char *prefix
)
2780 parse_opt_subcommand_fn
*fn
= NULL
;
2781 struct option builtin_maintenance_options
[] = {
2782 OPT_SUBCOMMAND("run", &fn
, maintenance_run
),
2783 OPT_SUBCOMMAND("start", &fn
, maintenance_start
),
2784 OPT_SUBCOMMAND("stop", &fn
, maintenance_stop
),
2785 OPT_SUBCOMMAND("register", &fn
, maintenance_register
),
2786 OPT_SUBCOMMAND("unregister", &fn
, maintenance_unregister
),
2790 argc
= parse_options(argc
, argv
, prefix
, builtin_maintenance_options
,
2791 builtin_maintenance_usage
, 0);
2792 return fn(argc
, argv
, prefix
);