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
15 #include "environment.h"
17 #include "repository.h"
21 #include "parse-options.h"
22 #include "run-command.h"
26 #include "commit-graph.h"
28 #include "object-store.h"
30 #include "pack-objects.h"
33 #include "promisor-remote.h"
42 #define FAILED_RUN "failed to run %s"
44 static const char * const builtin_gc_usage
[] = {
45 N_("git gc [<options>]"),
49 static int pack_refs
= 1;
50 static int prune_reflogs
= 1;
51 static int cruft_packs
= -1;
52 static int aggressive_depth
= 50;
53 static int aggressive_window
= 250;
54 static int gc_auto_threshold
= 6700;
55 static int gc_auto_pack_limit
= 50;
56 static int detach_auto
= 1;
57 static timestamp_t gc_log_expire_time
;
58 static const char *gc_log_expire
= "1.day.ago";
59 static const char *prune_expire
= "2.weeks.ago";
60 static const char *prune_worktrees_expire
= "3.months.ago";
61 static unsigned long big_pack_threshold
;
62 static unsigned long max_delta_cache_size
= DEFAULT_DELTA_CACHE_SIZE
;
64 static struct strvec reflog
= STRVEC_INIT
;
65 static struct strvec repack
= STRVEC_INIT
;
66 static struct strvec prune
= STRVEC_INIT
;
67 static struct strvec prune_worktrees
= STRVEC_INIT
;
68 static struct strvec rerere
= STRVEC_INIT
;
70 static struct tempfile
*pidfile
;
71 static struct lock_file log_lock
;
73 static struct string_list pack_garbage
= STRING_LIST_INIT_DUP
;
75 static void clean_pack_garbage(void)
78 for (i
= 0; i
< pack_garbage
.nr
; i
++)
79 unlink_or_warn(pack_garbage
.items
[i
].string
);
80 string_list_clear(&pack_garbage
, 0);
83 static void report_pack_garbage(unsigned seen_bits
, const char *path
)
85 if (seen_bits
== PACKDIR_FILE_IDX
)
86 string_list_append(&pack_garbage
, path
);
89 static void process_log_file(void)
92 if (fstat(get_lock_file_fd(&log_lock
), &st
)) {
94 * Perhaps there was an i/o error or another
95 * unlikely situation. Try to make a note of
96 * this in gc.log along with any existing
99 int saved_errno
= errno
;
100 fprintf(stderr
, _("Failed to fstat %s: %s"),
101 get_lock_file_path(&log_lock
),
102 strerror(saved_errno
));
104 commit_lock_file(&log_lock
);
106 } else if (st
.st_size
) {
107 /* There was some error recorded in the lock file */
108 commit_lock_file(&log_lock
);
110 /* No error, clean up any old gc.log */
111 unlink(git_path("gc.log"));
112 rollback_lock_file(&log_lock
);
116 static void process_log_file_at_exit(void)
122 static void process_log_file_on_signal(int signo
)
129 static int gc_config_is_timestamp_never(const char *var
)
134 if (!git_config_get_value(var
, &value
) && value
) {
135 if (parse_expiry_date(value
, &expire
))
136 die(_("failed to parse '%s' value '%s'"), var
, value
);
142 static void gc_config(void)
146 if (!git_config_get_value("gc.packrefs", &value
)) {
147 if (value
&& !strcmp(value
, "notbare"))
150 pack_refs
= git_config_bool("gc.packrefs", value
);
153 if (gc_config_is_timestamp_never("gc.reflogexpire") &&
154 gc_config_is_timestamp_never("gc.reflogexpireunreachable"))
157 git_config_get_int("gc.aggressivewindow", &aggressive_window
);
158 git_config_get_int("gc.aggressivedepth", &aggressive_depth
);
159 git_config_get_int("gc.auto", &gc_auto_threshold
);
160 git_config_get_int("gc.autopacklimit", &gc_auto_pack_limit
);
161 git_config_get_bool("gc.autodetach", &detach_auto
);
162 git_config_get_bool("gc.cruftpacks", &cruft_packs
);
163 git_config_get_expiry("gc.pruneexpire", &prune_expire
);
164 git_config_get_expiry("gc.worktreepruneexpire", &prune_worktrees_expire
);
165 git_config_get_expiry("gc.logexpiry", &gc_log_expire
);
167 git_config_get_ulong("gc.bigpackthreshold", &big_pack_threshold
);
168 git_config_get_ulong("pack.deltacachesize", &max_delta_cache_size
);
170 git_config(git_default_config
, NULL
);
173 struct maintenance_run_opts
;
174 static int maintenance_task_pack_refs(MAYBE_UNUSED
struct maintenance_run_opts
*opts
)
176 struct child_process cmd
= CHILD_PROCESS_INIT
;
179 strvec_pushl(&cmd
.args
, "pack-refs", "--all", "--prune", NULL
);
180 return run_command(&cmd
);
183 static int too_many_loose_objects(void)
186 * Quickly check if a "gc" is needed, by estimating how
187 * many loose objects there are. Because SHA-1 is evenly
188 * distributed, we can check only one and get a reasonable
196 const unsigned hexsz_loose
= the_hash_algo
->hexsz
- 2;
198 dir
= opendir(git_path("objects/17"));
202 auto_threshold
= DIV_ROUND_UP(gc_auto_threshold
, 256);
203 while ((ent
= readdir(dir
)) != NULL
) {
204 if (strspn(ent
->d_name
, "0123456789abcdef") != hexsz_loose
||
205 ent
->d_name
[hexsz_loose
] != '\0')
207 if (++num_loose
> auto_threshold
) {
216 static struct packed_git
*find_base_packs(struct string_list
*packs
,
219 struct packed_git
*p
, *base
= NULL
;
221 for (p
= get_all_packs(the_repository
); p
; p
= p
->next
) {
225 if (p
->pack_size
>= limit
)
226 string_list_append(packs
, p
->pack_name
);
227 } else if (!base
|| base
->pack_size
< p
->pack_size
) {
233 string_list_append(packs
, base
->pack_name
);
238 static int too_many_packs(void)
240 struct packed_git
*p
;
243 if (gc_auto_pack_limit
<= 0)
246 for (cnt
= 0, p
= get_all_packs(the_repository
); p
; p
= p
->next
) {
252 * Perhaps check the size of the pack and count only
253 * very small ones here?
257 return gc_auto_pack_limit
< cnt
;
260 static uint64_t total_ram(void)
262 #if defined(HAVE_SYSINFO)
267 #elif defined(HAVE_BSD_SYSCTL) && (defined(HW_MEMSIZE) || defined(HW_PHYSMEM))
268 int64_t physical_memory
;
273 # if defined(HW_MEMSIZE)
278 length
= sizeof(int64_t);
279 if (!sysctl(mib
, 2, &physical_memory
, &length
, NULL
, 0))
280 return physical_memory
;
281 #elif defined(GIT_WINDOWS_NATIVE)
282 MEMORYSTATUSEX memInfo
;
284 memInfo
.dwLength
= sizeof(MEMORYSTATUSEX
);
285 if (GlobalMemoryStatusEx(&memInfo
))
286 return memInfo
.ullTotalPhys
;
291 static uint64_t estimate_repack_memory(struct packed_git
*pack
)
293 unsigned long nr_objects
= repo_approximate_object_count(the_repository
);
294 size_t os_cache
, heap
;
296 if (!pack
|| !nr_objects
)
300 * First we have to scan through at least one pack.
301 * Assume enough room in OS file cache to keep the entire pack
302 * or we may accidentally evict data of other processes from
305 os_cache
= pack
->pack_size
+ pack
->index_size
;
306 /* then pack-objects needs lots more for book keeping */
307 heap
= sizeof(struct object_entry
) * nr_objects
;
309 * internal rev-list --all --objects takes up some memory too,
310 * let's say half of it is for blobs
312 heap
+= sizeof(struct blob
) * nr_objects
/ 2;
314 * and the other half is for trees (commits and tags are
315 * usually insignificant)
317 heap
+= sizeof(struct tree
) * nr_objects
/ 2;
318 /* and then obj_hash[], underestimated in fact */
319 heap
+= sizeof(struct object
*) * nr_objects
;
320 /* revindex is used also */
321 heap
+= (sizeof(off_t
) + sizeof(uint32_t)) * nr_objects
;
323 * read_sha1_file() (either at delta calculation phase, or
324 * writing phase) also fills up the delta base cache
326 heap
+= delta_base_cache_limit
;
327 /* and of course pack-objects has its own delta cache */
328 heap
+= max_delta_cache_size
;
330 return os_cache
+ heap
;
333 static int keep_one_pack(struct string_list_item
*item
, void *data UNUSED
)
335 strvec_pushf(&repack
, "--keep-pack=%s", basename(item
->string
));
339 static void add_repack_all_option(struct string_list
*keep_pack
)
341 if (prune_expire
&& !strcmp(prune_expire
, "now"))
342 strvec_push(&repack
, "-a");
343 else if (cruft_packs
) {
344 strvec_push(&repack
, "--cruft");
346 strvec_pushf(&repack
, "--cruft-expiration=%s", prune_expire
);
348 strvec_push(&repack
, "-A");
350 strvec_pushf(&repack
, "--unpack-unreachable=%s", prune_expire
);
354 for_each_string_list(keep_pack
, keep_one_pack
, NULL
);
357 static void add_repack_incremental_option(void)
359 strvec_push(&repack
, "--no-write-bitmap-index");
362 static int need_to_gc(void)
365 * Setting gc.auto to 0 or negative can disable the
368 if (gc_auto_threshold
<= 0)
372 * If there are too many loose objects, but not too many
373 * packs, we run "repack -d -l". If there are too many packs,
374 * we run "repack -A -d -l". Otherwise we tell the caller
377 if (too_many_packs()) {
378 struct string_list keep_pack
= STRING_LIST_INIT_NODUP
;
380 if (big_pack_threshold
) {
381 find_base_packs(&keep_pack
, big_pack_threshold
);
382 if (keep_pack
.nr
>= gc_auto_pack_limit
) {
383 big_pack_threshold
= 0;
384 string_list_clear(&keep_pack
, 0);
385 find_base_packs(&keep_pack
, 0);
388 struct packed_git
*p
= find_base_packs(&keep_pack
, 0);
389 uint64_t mem_have
, mem_want
;
391 mem_have
= total_ram();
392 mem_want
= estimate_repack_memory(p
);
395 * Only allow 1/2 of memory for pack-objects, leave
396 * the rest for the OS and other processes in the
399 if (!mem_have
|| mem_want
< mem_have
/ 2)
400 string_list_clear(&keep_pack
, 0);
403 add_repack_all_option(&keep_pack
);
404 string_list_clear(&keep_pack
, 0);
405 } else if (too_many_loose_objects())
406 add_repack_incremental_option();
410 if (run_hooks("pre-auto-gc"))
415 /* return NULL on success, else hostname running the gc */
416 static const char *lock_repo_for_gc(int force
, pid_t
* ret_pid
)
418 struct lock_file lock
= LOCK_INIT
;
419 char my_host
[HOST_NAME_MAX
+ 1];
420 struct strbuf sb
= STRBUF_INIT
;
427 if (is_tempfile_active(pidfile
))
431 if (xgethostname(my_host
, sizeof(my_host
)))
432 xsnprintf(my_host
, sizeof(my_host
), "unknown");
434 pidfile_path
= git_pathdup("gc.pid");
435 fd
= hold_lock_file_for_update(&lock
, pidfile_path
,
438 static char locking_host
[HOST_NAME_MAX
+ 1];
439 static char *scan_fmt
;
443 scan_fmt
= xstrfmt("%s %%%ds", "%"SCNuMAX
, HOST_NAME_MAX
);
444 fp
= fopen(pidfile_path
, "r");
445 memset(locking_host
, 0, sizeof(locking_host
));
448 !fstat(fileno(fp
), &st
) &&
450 * 12 hour limit is very generous as gc should
451 * never take that long. On the other hand we
452 * don't really need a strict limit here,
453 * running gc --auto one day late is not a big
454 * problem. --force can be used in manual gc
455 * after the user verifies that no gc is
458 time(NULL
) - st
.st_mtime
<= 12 * 3600 &&
459 fscanf(fp
, scan_fmt
, &pid
, locking_host
) == 2 &&
460 /* be gentle to concurrent "gc" on remote hosts */
461 (strcmp(locking_host
, my_host
) || !kill(pid
, 0) || errno
== EPERM
);
466 rollback_lock_file(&lock
);
473 strbuf_addf(&sb
, "%"PRIuMAX
" %s",
474 (uintmax_t) getpid(), my_host
);
475 write_in_full(fd
, sb
.buf
, sb
.len
);
477 commit_lock_file(&lock
);
478 pidfile
= register_tempfile(pidfile_path
);
484 * Returns 0 if there was no previous error and gc can proceed, 1 if
485 * gc should not proceed due to an error in the last run. Prints a
486 * message and returns with a non-[01] status code if an error occurred
487 * while reading gc.log
489 static int report_last_gc_error(void)
491 struct strbuf sb
= STRBUF_INIT
;
495 char *gc_log_path
= git_pathdup("gc.log");
497 if (stat(gc_log_path
, &st
)) {
501 ret
= die_message_errno(_("cannot stat '%s'"), gc_log_path
);
505 if (st
.st_mtime
< gc_log_expire_time
)
508 len
= strbuf_read_file(&sb
, gc_log_path
, 0);
510 ret
= die_message_errno(_("cannot read '%s'"), gc_log_path
);
513 * A previous gc failed. Report the error, and don't
514 * bother with an automatic gc run since it is likely
515 * to fail in the same way.
517 warning(_("The last gc run reported the following. "
518 "Please correct the root cause\n"
520 "Automatic cleanup will not be performed "
521 "until the file is removed.\n\n"
523 gc_log_path
, sb
.buf
);
532 static void gc_before_repack(void)
535 * We may be called twice, as both the pre- and
536 * post-daemonized phases will call us, but running these
537 * commands more than once is pointless and wasteful.
543 if (pack_refs
&& maintenance_task_pack_refs(NULL
))
544 die(FAILED_RUN
, "pack-refs");
547 struct child_process cmd
= CHILD_PROCESS_INIT
;
550 strvec_pushv(&cmd
.args
, reflog
.v
);
551 if (run_command(&cmd
))
552 die(FAILED_RUN
, reflog
.v
[0]);
556 int cmd_gc(int argc
, const char **argv
, const char *prefix
)
565 int keep_largest_pack
= -1;
567 struct child_process rerere_cmd
= CHILD_PROCESS_INIT
;
569 struct option builtin_gc_options
[] = {
570 OPT__QUIET(&quiet
, N_("suppress progress reporting")),
571 { OPTION_STRING
, 0, "prune", &prune_expire
, N_("date"),
572 N_("prune unreferenced objects"),
573 PARSE_OPT_OPTARG
, NULL
, (intptr_t)prune_expire
},
574 OPT_BOOL(0, "cruft", &cruft_packs
, N_("pack unreferenced objects separately")),
575 OPT_BOOL(0, "aggressive", &aggressive
, N_("be more thorough (increased runtime)")),
576 OPT_BOOL_F(0, "auto", &auto_gc
, N_("enable auto-gc mode"),
577 PARSE_OPT_NOCOMPLETE
),
578 OPT_BOOL_F(0, "force", &force
,
579 N_("force running gc even if there may be another gc running"),
580 PARSE_OPT_NOCOMPLETE
),
581 OPT_BOOL(0, "keep-largest-pack", &keep_largest_pack
,
582 N_("repack all other packs except the largest pack")),
586 if (argc
== 2 && !strcmp(argv
[1], "-h"))
587 usage_with_options(builtin_gc_usage
, builtin_gc_options
);
589 strvec_pushl(&reflog
, "reflog", "expire", "--all", NULL
);
590 strvec_pushl(&repack
, "repack", "-d", "-l", NULL
);
591 strvec_pushl(&prune
, "prune", "--expire", NULL
);
592 strvec_pushl(&prune_worktrees
, "worktree", "prune", "--expire", NULL
);
593 strvec_pushl(&rerere
, "rerere", "gc", NULL
);
595 /* default expiry time, overwritten in gc_config */
597 if (parse_expiry_date(gc_log_expire
, &gc_log_expire_time
))
598 die(_("failed to parse gc.logExpiry value %s"), gc_log_expire
);
601 pack_refs
= !is_bare_repository();
603 argc
= parse_options(argc
, argv
, prefix
, builtin_gc_options
,
604 builtin_gc_usage
, 0);
606 usage_with_options(builtin_gc_usage
, builtin_gc_options
);
608 if (prune_expire
&& parse_expiry_date(prune_expire
, &dummy
))
609 die(_("failed to parse prune expiry value %s"), prune_expire
);
611 prepare_repo_settings(the_repository
);
613 cruft_packs
= the_repository
->settings
.gc_cruft_packs
;
616 strvec_push(&repack
, "-f");
617 if (aggressive_depth
> 0)
618 strvec_pushf(&repack
, "--depth=%d", aggressive_depth
);
619 if (aggressive_window
> 0)
620 strvec_pushf(&repack
, "--window=%d", aggressive_window
);
623 strvec_push(&repack
, "-q");
627 * Auto-gc should be least intrusive as possible.
633 fprintf(stderr
, _("Auto packing the repository in background for optimum performance.\n"));
635 fprintf(stderr
, _("Auto packing the repository for optimum performance.\n"));
636 fprintf(stderr
, _("See \"git help gc\" for manual housekeeping.\n"));
639 int ret
= report_last_gc_error();
642 /* Last gc --auto failed. Skip this one. */
645 /* an I/O error occurred, already reported */
648 if (lock_repo_for_gc(force
, &pid
))
650 gc_before_repack(); /* dies on failure */
651 delete_tempfile(&pidfile
);
654 * failure to daemonize is ok, we'll continue
657 daemonized
= !daemonize();
660 struct string_list keep_pack
= STRING_LIST_INIT_NODUP
;
662 if (keep_largest_pack
!= -1) {
663 if (keep_largest_pack
)
664 find_base_packs(&keep_pack
, 0);
665 } else if (big_pack_threshold
) {
666 find_base_packs(&keep_pack
, big_pack_threshold
);
669 add_repack_all_option(&keep_pack
);
670 string_list_clear(&keep_pack
, 0);
673 name
= lock_repo_for_gc(force
, &pid
);
676 return 0; /* be quiet on --auto */
677 die(_("gc is already running on machine '%s' pid %"PRIuMAX
" (use --force if not)"),
678 name
, (uintmax_t)pid
);
682 hold_lock_file_for_update(&log_lock
,
685 dup2(get_lock_file_fd(&log_lock
), 2);
686 sigchain_push_common(process_log_file_on_signal
);
687 atexit(process_log_file_at_exit
);
692 if (!repository_format_precious_objects
) {
693 struct child_process repack_cmd
= CHILD_PROCESS_INIT
;
695 repack_cmd
.git_cmd
= 1;
696 repack_cmd
.close_object_store
= 1;
697 strvec_pushv(&repack_cmd
.args
, repack
.v
);
698 if (run_command(&repack_cmd
))
699 die(FAILED_RUN
, repack
.v
[0]);
702 struct child_process prune_cmd
= CHILD_PROCESS_INIT
;
704 /* run `git prune` even if using cruft packs */
705 strvec_push(&prune
, prune_expire
);
707 strvec_push(&prune
, "--no-progress");
708 if (repo_has_promisor_remote(the_repository
))
710 "--exclude-promisor-objects");
711 prune_cmd
.git_cmd
= 1;
712 strvec_pushv(&prune_cmd
.args
, prune
.v
);
713 if (run_command(&prune_cmd
))
714 die(FAILED_RUN
, prune
.v
[0]);
718 if (prune_worktrees_expire
) {
719 struct child_process prune_worktrees_cmd
= CHILD_PROCESS_INIT
;
721 strvec_push(&prune_worktrees
, prune_worktrees_expire
);
722 prune_worktrees_cmd
.git_cmd
= 1;
723 strvec_pushv(&prune_worktrees_cmd
.args
, prune_worktrees
.v
);
724 if (run_command(&prune_worktrees_cmd
))
725 die(FAILED_RUN
, prune_worktrees
.v
[0]);
728 rerere_cmd
.git_cmd
= 1;
729 strvec_pushv(&rerere_cmd
.args
, rerere
.v
);
730 if (run_command(&rerere_cmd
))
731 die(FAILED_RUN
, rerere
.v
[0]);
733 report_garbage
= report_pack_garbage
;
734 reprepare_packed_git(the_repository
);
735 if (pack_garbage
.nr
> 0) {
736 close_object_store(the_repository
->objects
);
737 clean_pack_garbage();
740 if (the_repository
->settings
.gc_write_commit_graph
== 1)
741 write_commit_graph_reachable(the_repository
->objects
->odb
,
742 !quiet
&& !daemonized
? COMMIT_GRAPH_WRITE_PROGRESS
: 0,
745 if (auto_gc
&& too_many_loose_objects())
746 warning(_("There are too many unreachable loose objects; "
747 "run 'git prune' to remove them."));
750 unlink(git_path("gc.log"));
755 static const char *const builtin_maintenance_run_usage
[] = {
756 N_("git maintenance run [--auto] [--[no-]quiet] [--task=<task>] [--schedule]"),
760 enum schedule_priority
{
767 static enum schedule_priority
parse_schedule(const char *value
)
770 return SCHEDULE_NONE
;
771 if (!strcasecmp(value
, "hourly"))
772 return SCHEDULE_HOURLY
;
773 if (!strcasecmp(value
, "daily"))
774 return SCHEDULE_DAILY
;
775 if (!strcasecmp(value
, "weekly"))
776 return SCHEDULE_WEEKLY
;
777 return SCHEDULE_NONE
;
780 static int maintenance_opt_schedule(const struct option
*opt
, const char *arg
,
783 enum schedule_priority
*priority
= opt
->value
;
786 die(_("--no-schedule is not allowed"));
788 *priority
= parse_schedule(arg
);
791 die(_("unrecognized --schedule argument '%s'"), arg
);
796 struct maintenance_run_opts
{
799 enum schedule_priority schedule
;
802 /* Remember to update object flag allocation in object.h */
805 struct cg_auto_data
{
806 int num_not_in_graph
;
810 static int dfs_on_ref(const char *refname UNUSED
,
811 const struct object_id
*oid
,
815 struct cg_auto_data
*data
= (struct cg_auto_data
*)cb_data
;
817 struct object_id peeled
;
818 struct commit_list
*stack
= NULL
;
819 struct commit
*commit
;
821 if (!peel_iterated_oid(oid
, &peeled
))
823 if (oid_object_info(the_repository
, oid
, NULL
) != OBJ_COMMIT
)
826 commit
= lookup_commit(the_repository
, oid
);
829 if (repo_parse_commit(the_repository
, commit
) ||
830 commit_graph_position(commit
) != COMMIT_NOT_FROM_GRAPH
)
833 data
->num_not_in_graph
++;
835 if (data
->num_not_in_graph
>= data
->limit
)
838 commit_list_append(commit
, &stack
);
840 while (!result
&& stack
) {
841 struct commit_list
*parent
;
843 commit
= pop_commit(&stack
);
845 for (parent
= commit
->parents
; parent
; parent
= parent
->next
) {
846 if (repo_parse_commit(the_repository
, parent
->item
) ||
847 commit_graph_position(parent
->item
) != COMMIT_NOT_FROM_GRAPH
||
848 parent
->item
->object
.flags
& SEEN
)
851 parent
->item
->object
.flags
|= SEEN
;
852 data
->num_not_in_graph
++;
854 if (data
->num_not_in_graph
>= data
->limit
) {
859 commit_list_append(parent
->item
, &stack
);
863 free_commit_list(stack
);
867 static int should_write_commit_graph(void)
870 struct cg_auto_data data
;
872 data
.num_not_in_graph
= 0;
874 git_config_get_int("maintenance.commit-graph.auto",
882 result
= for_each_ref(dfs_on_ref
, &data
);
884 repo_clear_commit_marks(the_repository
, SEEN
);
889 static int run_write_commit_graph(struct maintenance_run_opts
*opts
)
891 struct child_process child
= CHILD_PROCESS_INIT
;
893 child
.git_cmd
= child
.close_object_store
= 1;
894 strvec_pushl(&child
.args
, "commit-graph", "write",
895 "--split", "--reachable", NULL
);
898 strvec_push(&child
.args
, "--no-progress");
900 return !!run_command(&child
);
903 static int maintenance_task_commit_graph(struct maintenance_run_opts
*opts
)
905 prepare_repo_settings(the_repository
);
906 if (!the_repository
->settings
.core_commit_graph
)
909 if (run_write_commit_graph(opts
)) {
910 error(_("failed to write commit-graph"));
917 static int fetch_remote(struct remote
*remote
, void *cbdata
)
919 struct maintenance_run_opts
*opts
= cbdata
;
920 struct child_process child
= CHILD_PROCESS_INIT
;
922 if (remote
->skip_default_update
)
926 strvec_pushl(&child
.args
, "fetch", remote
->name
,
927 "--prefetch", "--prune", "--no-tags",
928 "--no-write-fetch-head", "--recurse-submodules=no",
932 strvec_push(&child
.args
, "--quiet");
934 return !!run_command(&child
);
937 static int maintenance_task_prefetch(struct maintenance_run_opts
*opts
)
939 if (for_each_remote(fetch_remote
, opts
)) {
940 error(_("failed to prefetch remotes"));
947 static int maintenance_task_gc(struct maintenance_run_opts
*opts
)
949 struct child_process child
= CHILD_PROCESS_INIT
;
951 child
.git_cmd
= child
.close_object_store
= 1;
952 strvec_push(&child
.args
, "gc");
955 strvec_push(&child
.args
, "--auto");
957 strvec_push(&child
.args
, "--quiet");
959 strvec_push(&child
.args
, "--no-quiet");
961 return run_command(&child
);
964 static int prune_packed(struct maintenance_run_opts
*opts
)
966 struct child_process child
= CHILD_PROCESS_INIT
;
969 strvec_push(&child
.args
, "prune-packed");
972 strvec_push(&child
.args
, "--quiet");
974 return !!run_command(&child
);
977 struct write_loose_object_data
{
983 static int loose_object_auto_limit
= 100;
985 static int loose_object_count(const struct object_id
*oid UNUSED
,
986 const char *path UNUSED
,
989 int *count
= (int*)data
;
990 if (++(*count
) >= loose_object_auto_limit
)
995 static int loose_object_auto_condition(void)
999 git_config_get_int("maintenance.loose-objects.auto",
1000 &loose_object_auto_limit
);
1002 if (!loose_object_auto_limit
)
1004 if (loose_object_auto_limit
< 0)
1007 return for_each_loose_file_in_objdir(the_repository
->objects
->odb
->path
,
1009 NULL
, NULL
, &count
);
1012 static int bail_on_loose(const struct object_id
*oid UNUSED
,
1013 const char *path UNUSED
,
1019 static int write_loose_object_to_stdin(const struct object_id
*oid
,
1020 const char *path UNUSED
,
1023 struct write_loose_object_data
*d
= (struct write_loose_object_data
*)data
;
1025 fprintf(d
->in
, "%s\n", oid_to_hex(oid
));
1027 return ++(d
->count
) > d
->batch_size
;
1030 static int pack_loose(struct maintenance_run_opts
*opts
)
1032 struct repository
*r
= the_repository
;
1034 struct write_loose_object_data data
;
1035 struct child_process pack_proc
= CHILD_PROCESS_INIT
;
1038 * Do not start pack-objects process
1039 * if there are no loose objects.
1041 if (!for_each_loose_file_in_objdir(r
->objects
->odb
->path
,
1046 pack_proc
.git_cmd
= 1;
1048 strvec_push(&pack_proc
.args
, "pack-objects");
1050 strvec_push(&pack_proc
.args
, "--quiet");
1051 strvec_pushf(&pack_proc
.args
, "%s/pack/loose", r
->objects
->odb
->path
);
1055 if (start_command(&pack_proc
)) {
1056 error(_("failed to start 'git pack-objects' process"));
1060 data
.in
= xfdopen(pack_proc
.in
, "w");
1062 data
.batch_size
= 50000;
1064 for_each_loose_file_in_objdir(r
->objects
->odb
->path
,
1065 write_loose_object_to_stdin
,
1072 if (finish_command(&pack_proc
)) {
1073 error(_("failed to finish 'git pack-objects' process"));
1080 static int maintenance_task_loose_objects(struct maintenance_run_opts
*opts
)
1082 return prune_packed(opts
) || pack_loose(opts
);
1085 static int incremental_repack_auto_condition(void)
1087 struct packed_git
*p
;
1088 int incremental_repack_auto_limit
= 10;
1091 prepare_repo_settings(the_repository
);
1092 if (!the_repository
->settings
.core_multi_pack_index
)
1095 git_config_get_int("maintenance.incremental-repack.auto",
1096 &incremental_repack_auto_limit
);
1098 if (!incremental_repack_auto_limit
)
1100 if (incremental_repack_auto_limit
< 0)
1103 for (p
= get_packed_git(the_repository
);
1104 count
< incremental_repack_auto_limit
&& p
;
1106 if (!p
->multi_pack_index
)
1110 return count
>= incremental_repack_auto_limit
;
1113 static int multi_pack_index_write(struct maintenance_run_opts
*opts
)
1115 struct child_process child
= CHILD_PROCESS_INIT
;
1118 strvec_pushl(&child
.args
, "multi-pack-index", "write", NULL
);
1121 strvec_push(&child
.args
, "--no-progress");
1123 if (run_command(&child
))
1124 return error(_("failed to write multi-pack-index"));
1129 static int multi_pack_index_expire(struct maintenance_run_opts
*opts
)
1131 struct child_process child
= CHILD_PROCESS_INIT
;
1133 child
.git_cmd
= child
.close_object_store
= 1;
1134 strvec_pushl(&child
.args
, "multi-pack-index", "expire", NULL
);
1137 strvec_push(&child
.args
, "--no-progress");
1139 if (run_command(&child
))
1140 return error(_("'git multi-pack-index expire' failed"));
1145 #define TWO_GIGABYTES (INT32_MAX)
1147 static off_t
get_auto_pack_size(void)
1150 * The "auto" value is special: we optimize for
1151 * one large pack-file (i.e. from a clone) and
1152 * expect the rest to be small and they can be
1155 * The strategy we select here is to select a
1156 * size that is one more than the second largest
1157 * pack-file. This ensures that we will repack
1158 * at least two packs if there are three or more
1162 off_t second_largest_size
= 0;
1164 struct packed_git
*p
;
1165 struct repository
*r
= the_repository
;
1167 reprepare_packed_git(r
);
1168 for (p
= get_all_packs(r
); p
; p
= p
->next
) {
1169 if (p
->pack_size
> max_size
) {
1170 second_largest_size
= max_size
;
1171 max_size
= p
->pack_size
;
1172 } else if (p
->pack_size
> second_largest_size
)
1173 second_largest_size
= p
->pack_size
;
1176 result_size
= second_largest_size
+ 1;
1178 /* But limit ourselves to a batch size of 2g */
1179 if (result_size
> TWO_GIGABYTES
)
1180 result_size
= TWO_GIGABYTES
;
1185 static int multi_pack_index_repack(struct maintenance_run_opts
*opts
)
1187 struct child_process child
= CHILD_PROCESS_INIT
;
1189 child
.git_cmd
= child
.close_object_store
= 1;
1190 strvec_pushl(&child
.args
, "multi-pack-index", "repack", NULL
);
1193 strvec_push(&child
.args
, "--no-progress");
1195 strvec_pushf(&child
.args
, "--batch-size=%"PRIuMAX
,
1196 (uintmax_t)get_auto_pack_size());
1198 if (run_command(&child
))
1199 return error(_("'git multi-pack-index repack' failed"));
1204 static int maintenance_task_incremental_repack(struct maintenance_run_opts
*opts
)
1206 prepare_repo_settings(the_repository
);
1207 if (!the_repository
->settings
.core_multi_pack_index
) {
1208 warning(_("skipping incremental-repack task because core.multiPackIndex is disabled"));
1212 if (multi_pack_index_write(opts
))
1214 if (multi_pack_index_expire(opts
))
1216 if (multi_pack_index_repack(opts
))
1221 typedef int maintenance_task_fn(struct maintenance_run_opts
*opts
);
1224 * An auto condition function returns 1 if the task should run
1225 * and 0 if the task should NOT run. See needs_to_gc() for an
1228 typedef int maintenance_auto_fn(void);
1230 struct maintenance_task
{
1232 maintenance_task_fn
*fn
;
1233 maintenance_auto_fn
*auto_condition
;
1236 enum schedule_priority schedule
;
1238 /* -1 if not selected. */
1242 enum maintenance_task_label
{
1245 TASK_INCREMENTAL_REPACK
,
1250 /* Leave as final value */
1254 static struct maintenance_task tasks
[] = {
1257 maintenance_task_prefetch
,
1259 [TASK_LOOSE_OBJECTS
] = {
1261 maintenance_task_loose_objects
,
1262 loose_object_auto_condition
,
1264 [TASK_INCREMENTAL_REPACK
] = {
1265 "incremental-repack",
1266 maintenance_task_incremental_repack
,
1267 incremental_repack_auto_condition
,
1271 maintenance_task_gc
,
1275 [TASK_COMMIT_GRAPH
] = {
1277 maintenance_task_commit_graph
,
1278 should_write_commit_graph
,
1280 [TASK_PACK_REFS
] = {
1282 maintenance_task_pack_refs
,
1287 static int compare_tasks_by_selection(const void *a_
, const void *b_
)
1289 const struct maintenance_task
*a
= a_
;
1290 const struct maintenance_task
*b
= b_
;
1292 return b
->selected_order
- a
->selected_order
;
1295 static int maintenance_run_tasks(struct maintenance_run_opts
*opts
)
1297 int i
, found_selected
= 0;
1299 struct lock_file lk
;
1300 struct repository
*r
= the_repository
;
1301 char *lock_path
= xstrfmt("%s/maintenance", r
->objects
->odb
->path
);
1303 if (hold_lock_file_for_update(&lk
, lock_path
, LOCK_NO_DEREF
) < 0) {
1305 * Another maintenance command is running.
1307 * If --auto was provided, then it is likely due to a
1308 * recursive process stack. Do not report an error in
1311 if (!opts
->auto_flag
&& !opts
->quiet
)
1312 warning(_("lock file '%s' exists, skipping maintenance"),
1319 for (i
= 0; !found_selected
&& i
< TASK__COUNT
; i
++)
1320 found_selected
= tasks
[i
].selected_order
>= 0;
1323 QSORT(tasks
, TASK__COUNT
, compare_tasks_by_selection
);
1325 for (i
= 0; i
< TASK__COUNT
; i
++) {
1326 if (found_selected
&& tasks
[i
].selected_order
< 0)
1329 if (!found_selected
&& !tasks
[i
].enabled
)
1332 if (opts
->auto_flag
&&
1333 (!tasks
[i
].auto_condition
||
1334 !tasks
[i
].auto_condition()))
1337 if (opts
->schedule
&& tasks
[i
].schedule
< opts
->schedule
)
1340 trace2_region_enter("maintenance", tasks
[i
].name
, r
);
1341 if (tasks
[i
].fn(opts
)) {
1342 error(_("task '%s' failed"), tasks
[i
].name
);
1345 trace2_region_leave("maintenance", tasks
[i
].name
, r
);
1348 rollback_lock_file(&lk
);
1352 static void initialize_maintenance_strategy(void)
1356 if (git_config_get_string("maintenance.strategy", &config_str
))
1359 if (!strcasecmp(config_str
, "incremental")) {
1360 tasks
[TASK_GC
].schedule
= SCHEDULE_NONE
;
1361 tasks
[TASK_COMMIT_GRAPH
].enabled
= 1;
1362 tasks
[TASK_COMMIT_GRAPH
].schedule
= SCHEDULE_HOURLY
;
1363 tasks
[TASK_PREFETCH
].enabled
= 1;
1364 tasks
[TASK_PREFETCH
].schedule
= SCHEDULE_HOURLY
;
1365 tasks
[TASK_INCREMENTAL_REPACK
].enabled
= 1;
1366 tasks
[TASK_INCREMENTAL_REPACK
].schedule
= SCHEDULE_DAILY
;
1367 tasks
[TASK_LOOSE_OBJECTS
].enabled
= 1;
1368 tasks
[TASK_LOOSE_OBJECTS
].schedule
= SCHEDULE_DAILY
;
1369 tasks
[TASK_PACK_REFS
].enabled
= 1;
1370 tasks
[TASK_PACK_REFS
].schedule
= SCHEDULE_WEEKLY
;
1374 static void initialize_task_config(int schedule
)
1377 struct strbuf config_name
= STRBUF_INIT
;
1381 initialize_maintenance_strategy();
1383 for (i
= 0; i
< TASK__COUNT
; i
++) {
1387 strbuf_reset(&config_name
);
1388 strbuf_addf(&config_name
, "maintenance.%s.enabled",
1391 if (!git_config_get_bool(config_name
.buf
, &config_value
))
1392 tasks
[i
].enabled
= config_value
;
1394 strbuf_reset(&config_name
);
1395 strbuf_addf(&config_name
, "maintenance.%s.schedule",
1398 if (!git_config_get_string(config_name
.buf
, &config_str
)) {
1399 tasks
[i
].schedule
= parse_schedule(config_str
);
1404 strbuf_release(&config_name
);
1407 static int task_option_parse(const struct option
*opt
,
1408 const char *arg
, int unset
)
1410 int i
, num_selected
= 0;
1411 struct maintenance_task
*task
= NULL
;
1413 BUG_ON_OPT_NEG(unset
);
1415 for (i
= 0; i
< TASK__COUNT
; i
++) {
1416 if (tasks
[i
].selected_order
>= 0)
1418 if (!strcasecmp(tasks
[i
].name
, arg
)) {
1424 error(_("'%s' is not a valid task"), arg
);
1428 if (task
->selected_order
>= 0) {
1429 error(_("task '%s' cannot be selected multiple times"), arg
);
1433 task
->selected_order
= num_selected
+ 1;
1438 static int maintenance_run(int argc
, const char **argv
, const char *prefix
)
1441 struct maintenance_run_opts opts
;
1442 struct option builtin_maintenance_run_options
[] = {
1443 OPT_BOOL(0, "auto", &opts
.auto_flag
,
1444 N_("run tasks based on the state of the repository")),
1445 OPT_CALLBACK(0, "schedule", &opts
.schedule
, N_("frequency"),
1446 N_("run tasks based on frequency"),
1447 maintenance_opt_schedule
),
1448 OPT_BOOL(0, "quiet", &opts
.quiet
,
1449 N_("do not report progress or other information over stderr")),
1450 OPT_CALLBACK_F(0, "task", NULL
, N_("task"),
1451 N_("run a specific task"),
1452 PARSE_OPT_NONEG
, task_option_parse
),
1455 memset(&opts
, 0, sizeof(opts
));
1457 opts
.quiet
= !isatty(2);
1459 for (i
= 0; i
< TASK__COUNT
; i
++)
1460 tasks
[i
].selected_order
= -1;
1462 argc
= parse_options(argc
, argv
, prefix
,
1463 builtin_maintenance_run_options
,
1464 builtin_maintenance_run_usage
,
1465 PARSE_OPT_STOP_AT_NON_OPTION
);
1467 if (opts
.auto_flag
&& opts
.schedule
)
1468 die(_("use at most one of --auto and --schedule=<frequency>"));
1470 initialize_task_config(opts
.schedule
);
1473 usage_with_options(builtin_maintenance_run_usage
,
1474 builtin_maintenance_run_options
);
1475 return maintenance_run_tasks(&opts
);
1478 static char *get_maintpath(void)
1480 struct strbuf sb
= STRBUF_INIT
;
1481 const char *p
= the_repository
->worktree
?
1482 the_repository
->worktree
: the_repository
->gitdir
;
1484 strbuf_realpath(&sb
, p
, 1);
1485 return strbuf_detach(&sb
, NULL
);
1488 static char const * const builtin_maintenance_register_usage
[] = {
1489 "git maintenance register [--config-file <path>]",
1493 static int maintenance_register(int argc
, const char **argv
, const char *prefix
)
1495 char *config_file
= NULL
;
1496 struct option options
[] = {
1497 OPT_STRING(0, "config-file", &config_file
, N_("file"), N_("use given config file")),
1501 const char *key
= "maintenance.repo";
1503 char *maintpath
= get_maintpath();
1504 struct string_list_item
*item
;
1505 const struct string_list
*list
;
1507 argc
= parse_options(argc
, argv
, prefix
, options
,
1508 builtin_maintenance_register_usage
, 0);
1510 usage_with_options(builtin_maintenance_register_usage
,
1513 /* Disable foreground maintenance */
1514 git_config_set("maintenance.auto", "false");
1516 /* Set maintenance strategy, if unset */
1517 if (!git_config_get_string("maintenance.strategy", &config_value
))
1520 git_config_set("maintenance.strategy", "incremental");
1522 list
= git_config_get_value_multi(key
);
1524 for_each_string_list_item(item
, list
) {
1525 if (!strcmp(maintpath
, item
->string
)) {
1534 char *user_config
= NULL
, *xdg_config
= NULL
;
1537 git_global_config(&user_config
, &xdg_config
);
1538 config_file
= user_config
;
1540 die(_("$HOME not set"));
1542 rc
= git_config_set_multivar_in_file_gently(
1543 config_file
, "maintenance.repo", maintpath
,
1544 CONFIG_REGEX_NONE
, 0);
1549 die(_("unable to add '%s' value of '%s'"),
1557 static char const * const builtin_maintenance_unregister_usage
[] = {
1558 "git maintenance unregister [--config-file <path>] [--force]",
1562 static int maintenance_unregister(int argc
, const char **argv
, const char *prefix
)
1565 char *config_file
= NULL
;
1566 struct option options
[] = {
1567 OPT_STRING(0, "config-file", &config_file
, N_("file"), N_("use given config file")),
1569 N_("return success even if repository was not registered"),
1570 PARSE_OPT_NOCOMPLETE
),
1573 const char *key
= "maintenance.repo";
1574 char *maintpath
= get_maintpath();
1576 struct string_list_item
*item
;
1577 const struct string_list
*list
;
1578 struct config_set cs
= { { 0 } };
1580 argc
= parse_options(argc
, argv
, prefix
, options
,
1581 builtin_maintenance_unregister_usage
, 0);
1583 usage_with_options(builtin_maintenance_unregister_usage
,
1587 git_configset_init(&cs
);
1588 git_configset_add_file(&cs
, config_file
);
1589 list
= git_configset_get_value_multi(&cs
, key
);
1591 list
= git_config_get_value_multi(key
);
1594 for_each_string_list_item(item
, list
) {
1595 if (!strcmp(maintpath
, item
->string
)) {
1604 char *user_config
= NULL
, *xdg_config
= NULL
;
1606 git_global_config(&user_config
, &xdg_config
);
1607 config_file
= user_config
;
1609 die(_("$HOME not set"));
1611 rc
= git_config_set_multivar_in_file_gently(
1612 config_file
, key
, NULL
, maintpath
,
1613 CONFIG_FLAGS_MULTI_REPLACE
| CONFIG_FLAGS_FIXED_VALUE
);
1618 (!force
|| rc
== CONFIG_NOTHING_SET
))
1619 die(_("unable to unset '%s' value of '%s'"),
1621 } else if (!force
) {
1622 die(_("repository '%s' is not registered"), maintpath
);
1625 git_configset_clear(&cs
);
1630 static const char *get_frequency(enum schedule_priority schedule
)
1633 case SCHEDULE_HOURLY
:
1635 case SCHEDULE_DAILY
:
1637 case SCHEDULE_WEEKLY
:
1640 BUG("invalid schedule %d", schedule
);
1645 * get_schedule_cmd` reads the GIT_TEST_MAINT_SCHEDULER environment variable
1646 * to mock the schedulers that `git maintenance start` rely on.
1648 * For test purpose, GIT_TEST_MAINT_SCHEDULER can be set to a comma-separated
1649 * list of colon-separated key/value pairs where each pair contains a scheduler
1650 * and its corresponding mock.
1652 * * If $GIT_TEST_MAINT_SCHEDULER is not set, return false and leave the
1653 * arguments unmodified.
1655 * * If $GIT_TEST_MAINT_SCHEDULER is set, return true.
1656 * In this case, the *cmd value is read as input.
1658 * * if the input value *cmd is the key of one of the comma-separated list
1659 * item, then *is_available is set to true and *cmd is modified and becomes
1662 * * if the input value *cmd isn’t the key of any of the comma-separated list
1663 * item, then *is_available is set to false.
1666 * GIT_TEST_MAINT_SCHEDULER not set
1667 * +-------+-------------------------------------------------+
1668 * | Input | Output |
1669 * | *cmd | return code | *cmd | *is_available |
1670 * +-------+-------------+-------------------+---------------+
1671 * | "foo" | false | "foo" (unchanged) | (unchanged) |
1672 * +-------+-------------+-------------------+---------------+
1674 * GIT_TEST_MAINT_SCHEDULER set to “foo:./mock_foo.sh,bar:./mock_bar.sh”
1675 * +-------+-------------------------------------------------+
1676 * | Input | Output |
1677 * | *cmd | return code | *cmd | *is_available |
1678 * +-------+-------------+-------------------+---------------+
1679 * | "foo" | true | "./mock.foo.sh" | true |
1680 * | "qux" | true | "qux" (unchanged) | false |
1681 * +-------+-------------+-------------------+---------------+
1683 static int get_schedule_cmd(const char **cmd
, int *is_available
)
1685 char *testing
= xstrdup_or_null(getenv("GIT_TEST_MAINT_SCHEDULER"));
1686 struct string_list_item
*item
;
1687 struct string_list list
= STRING_LIST_INIT_NODUP
;
1695 string_list_split_in_place(&list
, testing
, ',', -1);
1696 for_each_string_list_item(item
, &list
) {
1697 struct string_list pair
= STRING_LIST_INIT_NODUP
;
1699 if (string_list_split_in_place(&pair
, item
->string
, ':', 2) != 2)
1702 if (!strcmp(*cmd
, pair
.items
[0].string
)) {
1703 *cmd
= pair
.items
[1].string
;
1706 string_list_clear(&list
, 0);
1712 string_list_clear(&list
, 0);
1717 static int is_launchctl_available(void)
1719 const char *cmd
= "launchctl";
1721 if (get_schedule_cmd(&cmd
, &is_available
))
1722 return is_available
;
1731 static char *launchctl_service_name(const char *frequency
)
1733 struct strbuf label
= STRBUF_INIT
;
1734 strbuf_addf(&label
, "org.git-scm.git.%s", frequency
);
1735 return strbuf_detach(&label
, NULL
);
1738 static char *launchctl_service_filename(const char *name
)
1741 struct strbuf filename
= STRBUF_INIT
;
1742 strbuf_addf(&filename
, "~/Library/LaunchAgents/%s.plist", name
);
1744 expanded
= interpolate_path(filename
.buf
, 1);
1746 die(_("failed to expand path '%s'"), filename
.buf
);
1748 strbuf_release(&filename
);
1752 static char *launchctl_get_uid(void)
1754 return xstrfmt("gui/%d", getuid());
1757 static int launchctl_boot_plist(int enable
, const char *filename
)
1759 const char *cmd
= "launchctl";
1761 struct child_process child
= CHILD_PROCESS_INIT
;
1762 char *uid
= launchctl_get_uid();
1764 get_schedule_cmd(&cmd
, NULL
);
1765 strvec_split(&child
.args
, cmd
);
1766 strvec_pushl(&child
.args
, enable
? "bootstrap" : "bootout", uid
,
1769 child
.no_stderr
= 1;
1770 child
.no_stdout
= 1;
1772 if (start_command(&child
))
1773 die(_("failed to start launchctl"));
1775 result
= finish_command(&child
);
1781 static int launchctl_remove_plist(enum schedule_priority schedule
)
1783 const char *frequency
= get_frequency(schedule
);
1784 char *name
= launchctl_service_name(frequency
);
1785 char *filename
= launchctl_service_filename(name
);
1786 int result
= launchctl_boot_plist(0, filename
);
1793 static int launchctl_remove_plists(void)
1795 return launchctl_remove_plist(SCHEDULE_HOURLY
) ||
1796 launchctl_remove_plist(SCHEDULE_DAILY
) ||
1797 launchctl_remove_plist(SCHEDULE_WEEKLY
);
1800 static int launchctl_list_contains_plist(const char *name
, const char *cmd
)
1802 struct child_process child
= CHILD_PROCESS_INIT
;
1804 strvec_split(&child
.args
, cmd
);
1805 strvec_pushl(&child
.args
, "list", name
, NULL
);
1807 child
.no_stderr
= 1;
1808 child
.no_stdout
= 1;
1810 if (start_command(&child
))
1811 die(_("failed to start launchctl"));
1813 /* Returns failure if 'name' doesn't exist. */
1814 return !finish_command(&child
);
1817 static int launchctl_schedule_plist(const char *exec_path
, enum schedule_priority schedule
)
1820 const char *preamble
, *repeat
;
1821 const char *frequency
= get_frequency(schedule
);
1822 char *name
= launchctl_service_name(frequency
);
1823 char *filename
= launchctl_service_filename(name
);
1824 struct lock_file lk
= LOCK_INIT
;
1825 static unsigned long lock_file_timeout_ms
= ULONG_MAX
;
1826 struct strbuf plist
= STRBUF_INIT
, plist2
= STRBUF_INIT
;
1828 const char *cmd
= "launchctl";
1830 get_schedule_cmd(&cmd
, NULL
);
1831 preamble
= "<?xml version=\"1.0\"?>\n"
1832 "<!DOCTYPE plist PUBLIC \"-//Apple//DTD PLIST 1.0//EN\" \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">\n"
1833 "<plist version=\"1.0\">"
1835 "<key>Label</key><string>%s</string>\n"
1836 "<key>ProgramArguments</key>\n"
1838 "<string>%s/git</string>\n"
1839 "<string>--exec-path=%s</string>\n"
1840 "<string>for-each-repo</string>\n"
1841 "<string>--config=maintenance.repo</string>\n"
1842 "<string>maintenance</string>\n"
1843 "<string>run</string>\n"
1844 "<string>--schedule=%s</string>\n"
1846 "<key>StartCalendarInterval</key>\n"
1848 strbuf_addf(&plist
, preamble
, name
, exec_path
, exec_path
, frequency
);
1851 case SCHEDULE_HOURLY
:
1853 "<key>Hour</key><integer>%d</integer>\n"
1854 "<key>Minute</key><integer>0</integer>\n"
1856 for (i
= 1; i
<= 23; i
++)
1857 strbuf_addf(&plist
, repeat
, i
);
1860 case SCHEDULE_DAILY
:
1862 "<key>Day</key><integer>%d</integer>\n"
1863 "<key>Hour</key><integer>0</integer>\n"
1864 "<key>Minute</key><integer>0</integer>\n"
1866 for (i
= 1; i
<= 6; i
++)
1867 strbuf_addf(&plist
, repeat
, i
);
1870 case SCHEDULE_WEEKLY
:
1871 strbuf_addstr(&plist
,
1873 "<key>Day</key><integer>0</integer>\n"
1874 "<key>Hour</key><integer>0</integer>\n"
1875 "<key>Minute</key><integer>0</integer>\n"
1883 strbuf_addstr(&plist
, "</array>\n</dict>\n</plist>\n");
1885 if (safe_create_leading_directories(filename
))
1886 die(_("failed to create directories for '%s'"), filename
);
1888 if ((long)lock_file_timeout_ms
< 0 &&
1889 git_config_get_ulong("gc.launchctlplistlocktimeoutms",
1890 &lock_file_timeout_ms
))
1891 lock_file_timeout_ms
= 150;
1893 fd
= hold_lock_file_for_update_timeout(&lk
, filename
, LOCK_DIE_ON_ERROR
,
1894 lock_file_timeout_ms
);
1897 * Does this file already exist? With the intended contents? Is it
1898 * registered already? Then it does not need to be re-registered.
1900 if (!stat(filename
, &st
) && st
.st_size
== plist
.len
&&
1901 strbuf_read_file(&plist2
, filename
, plist
.len
) == plist
.len
&&
1902 !strbuf_cmp(&plist
, &plist2
) &&
1903 launchctl_list_contains_plist(name
, cmd
))
1904 rollback_lock_file(&lk
);
1906 if (write_in_full(fd
, plist
.buf
, plist
.len
) < 0 ||
1907 commit_lock_file(&lk
))
1908 die_errno(_("could not write '%s'"), filename
);
1910 /* bootout might fail if not already running, so ignore */
1911 launchctl_boot_plist(0, filename
);
1912 if (launchctl_boot_plist(1, filename
))
1913 die(_("failed to bootstrap service %s"), filename
);
1918 strbuf_release(&plist
);
1919 strbuf_release(&plist2
);
1923 static int launchctl_add_plists(void)
1925 const char *exec_path
= git_exec_path();
1927 return launchctl_schedule_plist(exec_path
, SCHEDULE_HOURLY
) ||
1928 launchctl_schedule_plist(exec_path
, SCHEDULE_DAILY
) ||
1929 launchctl_schedule_plist(exec_path
, SCHEDULE_WEEKLY
);
1932 static int launchctl_update_schedule(int run_maintenance
, int fd
)
1934 if (run_maintenance
)
1935 return launchctl_add_plists();
1937 return launchctl_remove_plists();
1940 static int is_schtasks_available(void)
1942 const char *cmd
= "schtasks";
1944 if (get_schedule_cmd(&cmd
, &is_available
))
1945 return is_available
;
1947 #ifdef GIT_WINDOWS_NATIVE
1954 static char *schtasks_task_name(const char *frequency
)
1956 struct strbuf label
= STRBUF_INIT
;
1957 strbuf_addf(&label
, "Git Maintenance (%s)", frequency
);
1958 return strbuf_detach(&label
, NULL
);
1961 static int schtasks_remove_task(enum schedule_priority schedule
)
1963 const char *cmd
= "schtasks";
1964 struct child_process child
= CHILD_PROCESS_INIT
;
1965 const char *frequency
= get_frequency(schedule
);
1966 char *name
= schtasks_task_name(frequency
);
1968 get_schedule_cmd(&cmd
, NULL
);
1969 strvec_split(&child
.args
, cmd
);
1970 strvec_pushl(&child
.args
, "/delete", "/tn", name
, "/f", NULL
);
1973 return run_command(&child
);
1976 static int schtasks_remove_tasks(void)
1978 return schtasks_remove_task(SCHEDULE_HOURLY
) ||
1979 schtasks_remove_task(SCHEDULE_DAILY
) ||
1980 schtasks_remove_task(SCHEDULE_WEEKLY
);
1983 static int schtasks_schedule_task(const char *exec_path
, enum schedule_priority schedule
)
1985 const char *cmd
= "schtasks";
1987 struct child_process child
= CHILD_PROCESS_INIT
;
1989 struct tempfile
*tfile
;
1990 const char *frequency
= get_frequency(schedule
);
1991 char *name
= schtasks_task_name(frequency
);
1992 struct strbuf tfilename
= STRBUF_INIT
;
1994 get_schedule_cmd(&cmd
, NULL
);
1996 strbuf_addf(&tfilename
, "%s/schedule_%s_XXXXXX",
1997 get_git_common_dir(), frequency
);
1998 tfile
= xmks_tempfile(tfilename
.buf
);
1999 strbuf_release(&tfilename
);
2001 if (!fdopen_tempfile(tfile
, "w"))
2002 die(_("failed to create temp xml file"));
2004 xml
= "<?xml version=\"1.0\" ?>\n"
2005 "<Task version=\"1.4\" xmlns=\"http://schemas.microsoft.com/windows/2004/02/mit/task\">\n"
2007 "<CalendarTrigger>\n";
2008 fputs(xml
, tfile
->fp
);
2011 case SCHEDULE_HOURLY
:
2013 "<StartBoundary>2020-01-01T01:00:00</StartBoundary>\n"
2014 "<Enabled>true</Enabled>\n"
2016 "<DaysInterval>1</DaysInterval>\n"
2017 "</ScheduleByDay>\n"
2019 "<Interval>PT1H</Interval>\n"
2020 "<Duration>PT23H</Duration>\n"
2021 "<StopAtDurationEnd>false</StopAtDurationEnd>\n"
2025 case SCHEDULE_DAILY
:
2027 "<StartBoundary>2020-01-01T00:00:00</StartBoundary>\n"
2028 "<Enabled>true</Enabled>\n"
2029 "<ScheduleByWeek>\n"
2038 "<WeeksInterval>1</WeeksInterval>\n"
2039 "</ScheduleByWeek>\n");
2042 case SCHEDULE_WEEKLY
:
2044 "<StartBoundary>2020-01-01T00:00:00</StartBoundary>\n"
2045 "<Enabled>true</Enabled>\n"
2046 "<ScheduleByWeek>\n"
2050 "<WeeksInterval>1</WeeksInterval>\n"
2051 "</ScheduleByWeek>\n");
2058 xml
= "</CalendarTrigger>\n"
2061 "<Principal id=\"Author\">\n"
2062 "<LogonType>InteractiveToken</LogonType>\n"
2063 "<RunLevel>LeastPrivilege</RunLevel>\n"
2067 "<MultipleInstancesPolicy>IgnoreNew</MultipleInstancesPolicy>\n"
2068 "<Enabled>true</Enabled>\n"
2069 "<Hidden>true</Hidden>\n"
2070 "<UseUnifiedSchedulingEngine>true</UseUnifiedSchedulingEngine>\n"
2071 "<WakeToRun>false</WakeToRun>\n"
2072 "<ExecutionTimeLimit>PT72H</ExecutionTimeLimit>\n"
2073 "<Priority>7</Priority>\n"
2075 "<Actions Context=\"Author\">\n"
2077 "<Command>\"%s\\git.exe\"</Command>\n"
2078 "<Arguments>--exec-path=\"%s\" for-each-repo --config=maintenance.repo maintenance run --schedule=%s</Arguments>\n"
2082 fprintf(tfile
->fp
, xml
, exec_path
, exec_path
, frequency
);
2083 strvec_split(&child
.args
, cmd
);
2084 strvec_pushl(&child
.args
, "/create", "/tn", name
, "/f", "/xml",
2085 get_tempfile_path(tfile
), NULL
);
2086 close_tempfile_gently(tfile
);
2088 child
.no_stdout
= 1;
2089 child
.no_stderr
= 1;
2091 if (start_command(&child
))
2092 die(_("failed to start schtasks"));
2093 result
= finish_command(&child
);
2095 delete_tempfile(&tfile
);
2100 static int schtasks_schedule_tasks(void)
2102 const char *exec_path
= git_exec_path();
2104 return schtasks_schedule_task(exec_path
, SCHEDULE_HOURLY
) ||
2105 schtasks_schedule_task(exec_path
, SCHEDULE_DAILY
) ||
2106 schtasks_schedule_task(exec_path
, SCHEDULE_WEEKLY
);
2109 static int schtasks_update_schedule(int run_maintenance
, int fd
)
2111 if (run_maintenance
)
2112 return schtasks_schedule_tasks();
2114 return schtasks_remove_tasks();
2118 static int check_crontab_process(const char *cmd
)
2120 struct child_process child
= CHILD_PROCESS_INIT
;
2122 strvec_split(&child
.args
, cmd
);
2123 strvec_push(&child
.args
, "-l");
2125 child
.no_stdout
= 1;
2126 child
.no_stderr
= 1;
2127 child
.silent_exec_failure
= 1;
2129 if (start_command(&child
))
2131 /* Ignore exit code, as an empty crontab will return error. */
2132 finish_command(&child
);
2136 static int is_crontab_available(void)
2138 const char *cmd
= "crontab";
2141 if (get_schedule_cmd(&cmd
, &is_available
))
2142 return is_available
;
2146 * macOS has cron, but it requires special permissions and will
2147 * create a UI alert when attempting to run this command.
2151 return check_crontab_process(cmd
);
2155 #define BEGIN_LINE "# BEGIN GIT MAINTENANCE SCHEDULE"
2156 #define END_LINE "# END GIT MAINTENANCE SCHEDULE"
2158 static int crontab_update_schedule(int run_maintenance
, int fd
)
2160 const char *cmd
= "crontab";
2162 int in_old_region
= 0;
2163 struct child_process crontab_list
= CHILD_PROCESS_INIT
;
2164 struct child_process crontab_edit
= CHILD_PROCESS_INIT
;
2165 FILE *cron_list
, *cron_in
;
2166 struct strbuf line
= STRBUF_INIT
;
2167 struct tempfile
*tmpedit
= NULL
;
2169 get_schedule_cmd(&cmd
, NULL
);
2170 strvec_split(&crontab_list
.args
, cmd
);
2171 strvec_push(&crontab_list
.args
, "-l");
2172 crontab_list
.in
= -1;
2173 crontab_list
.out
= dup(fd
);
2174 crontab_list
.git_cmd
= 0;
2176 if (start_command(&crontab_list
))
2177 return error(_("failed to run 'crontab -l'; your system might not support 'cron'"));
2179 /* Ignore exit code, as an empty crontab will return error. */
2180 finish_command(&crontab_list
);
2182 tmpedit
= mks_tempfile_t(".git_cron_edit_tmpXXXXXX");
2184 result
= error(_("failed to create crontab temporary file"));
2187 cron_in
= fdopen_tempfile(tmpedit
, "w");
2189 result
= error(_("failed to open temporary file"));
2194 * Read from the .lock file, filtering out the old
2195 * schedule while appending the new schedule.
2197 cron_list
= fdopen(fd
, "r");
2200 while (!strbuf_getline_lf(&line
, cron_list
)) {
2201 if (!in_old_region
&& !strcmp(line
.buf
, BEGIN_LINE
))
2203 else if (in_old_region
&& !strcmp(line
.buf
, END_LINE
))
2205 else if (!in_old_region
)
2206 fprintf(cron_in
, "%s\n", line
.buf
);
2208 strbuf_release(&line
);
2210 if (run_maintenance
) {
2211 struct strbuf line_format
= STRBUF_INIT
;
2212 const char *exec_path
= git_exec_path();
2214 fprintf(cron_in
, "%s\n", BEGIN_LINE
);
2216 "# The following schedule was created by Git\n");
2217 fprintf(cron_in
, "# Any edits made in this region might be\n");
2219 "# replaced in the future by a Git command.\n\n");
2221 strbuf_addf(&line_format
,
2222 "%%s %%s * * %%s \"%s/git\" --exec-path=\"%s\" for-each-repo --config=maintenance.repo maintenance run --schedule=%%s\n",
2223 exec_path
, exec_path
);
2224 fprintf(cron_in
, line_format
.buf
, "0", "1-23", "*", "hourly");
2225 fprintf(cron_in
, line_format
.buf
, "0", "0", "1-6", "daily");
2226 fprintf(cron_in
, line_format
.buf
, "0", "0", "0", "weekly");
2227 strbuf_release(&line_format
);
2229 fprintf(cron_in
, "\n%s\n", END_LINE
);
2234 strvec_split(&crontab_edit
.args
, cmd
);
2235 strvec_push(&crontab_edit
.args
, get_tempfile_path(tmpedit
));
2236 crontab_edit
.git_cmd
= 0;
2238 if (start_command(&crontab_edit
)) {
2239 result
= error(_("failed to run 'crontab'; your system might not support 'cron'"));
2243 if (finish_command(&crontab_edit
))
2244 result
= error(_("'crontab' died"));
2248 delete_tempfile(&tmpedit
);
2252 static int real_is_systemd_timer_available(void)
2254 struct child_process child
= CHILD_PROCESS_INIT
;
2256 strvec_pushl(&child
.args
, "systemctl", "--user", "list-timers", NULL
);
2258 child
.no_stdout
= 1;
2259 child
.no_stderr
= 1;
2260 child
.silent_exec_failure
= 1;
2262 if (start_command(&child
))
2264 if (finish_command(&child
))
2269 static int is_systemd_timer_available(void)
2271 const char *cmd
= "systemctl";
2274 if (get_schedule_cmd(&cmd
, &is_available
))
2275 return is_available
;
2277 return real_is_systemd_timer_available();
2280 static char *xdg_config_home_systemd(const char *filename
)
2282 return xdg_config_home_for("systemd/user", filename
);
2285 static int systemd_timer_enable_unit(int enable
,
2286 enum schedule_priority schedule
)
2288 const char *cmd
= "systemctl";
2289 struct child_process child
= CHILD_PROCESS_INIT
;
2290 const char *frequency
= get_frequency(schedule
);
2293 * Disabling the systemd unit while it is already disabled makes
2294 * systemctl print an error.
2295 * Let's ignore it since it means we already are in the expected state:
2296 * the unit is disabled.
2298 * On the other hand, enabling a systemd unit which is already enabled
2299 * produces no error.
2302 child
.no_stderr
= 1;
2304 get_schedule_cmd(&cmd
, NULL
);
2305 strvec_split(&child
.args
, cmd
);
2306 strvec_pushl(&child
.args
, "--user", enable
? "enable" : "disable",
2308 strvec_pushf(&child
.args
, "git-maintenance@%s.timer", frequency
);
2310 if (start_command(&child
))
2311 return error(_("failed to start systemctl"));
2312 if (finish_command(&child
))
2314 * Disabling an already disabled systemd unit makes
2316 * Let's ignore this failure.
2318 * Enabling an enabled systemd unit doesn't fail.
2321 return error(_("failed to run systemctl"));
2325 static int systemd_timer_delete_unit_templates(void)
2328 char *filename
= xdg_config_home_systemd("git-maintenance@.timer");
2329 if (unlink(filename
) && !is_missing_file_error(errno
))
2330 ret
= error_errno(_("failed to delete '%s'"), filename
);
2331 FREE_AND_NULL(filename
);
2333 filename
= xdg_config_home_systemd("git-maintenance@.service");
2334 if (unlink(filename
) && !is_missing_file_error(errno
))
2335 ret
= error_errno(_("failed to delete '%s'"), filename
);
2341 static int systemd_timer_delete_units(void)
2343 return systemd_timer_enable_unit(0, SCHEDULE_HOURLY
) ||
2344 systemd_timer_enable_unit(0, SCHEDULE_DAILY
) ||
2345 systemd_timer_enable_unit(0, SCHEDULE_WEEKLY
) ||
2346 systemd_timer_delete_unit_templates();
2349 static int systemd_timer_write_unit_templates(const char *exec_path
)
2355 filename
= xdg_config_home_systemd("git-maintenance@.timer");
2356 if (safe_create_leading_directories(filename
)) {
2357 error(_("failed to create directories for '%s'"), filename
);
2360 file
= fopen_or_warn(filename
, "w");
2364 unit
= "# This file was created and is maintained by Git.\n"
2365 "# Any edits made in this file might be replaced in the future\n"
2366 "# by a Git command.\n"
2369 "Description=Optimize Git repositories data\n"
2376 "WantedBy=timers.target\n";
2377 if (fputs(unit
, file
) == EOF
) {
2378 error(_("failed to write to '%s'"), filename
);
2382 if (fclose(file
) == EOF
) {
2383 error_errno(_("failed to flush '%s'"), filename
);
2388 filename
= xdg_config_home_systemd("git-maintenance@.service");
2389 file
= fopen_or_warn(filename
, "w");
2393 unit
= "# This file was created and is maintained by Git.\n"
2394 "# Any edits made in this file might be replaced in the future\n"
2395 "# by a Git command.\n"
2398 "Description=Optimize Git repositories data\n"
2402 "ExecStart=\"%s/git\" --exec-path=\"%s\" for-each-repo --config=maintenance.repo maintenance run --schedule=%%i\n"
2403 "LockPersonality=yes\n"
2404 "MemoryDenyWriteExecute=yes\n"
2405 "NoNewPrivileges=yes\n"
2406 "RestrictAddressFamilies=AF_UNIX AF_INET AF_INET6\n"
2407 "RestrictNamespaces=yes\n"
2408 "RestrictRealtime=yes\n"
2409 "RestrictSUIDSGID=yes\n"
2410 "SystemCallArchitectures=native\n"
2411 "SystemCallFilter=@system-service\n";
2412 if (fprintf(file
, unit
, exec_path
, exec_path
) < 0) {
2413 error(_("failed to write to '%s'"), filename
);
2417 if (fclose(file
) == EOF
) {
2418 error_errno(_("failed to flush '%s'"), filename
);
2426 systemd_timer_delete_unit_templates();
2430 static int systemd_timer_setup_units(void)
2432 const char *exec_path
= git_exec_path();
2434 int ret
= systemd_timer_write_unit_templates(exec_path
) ||
2435 systemd_timer_enable_unit(1, SCHEDULE_HOURLY
) ||
2436 systemd_timer_enable_unit(1, SCHEDULE_DAILY
) ||
2437 systemd_timer_enable_unit(1, SCHEDULE_WEEKLY
);
2439 systemd_timer_delete_units();
2443 static int systemd_timer_update_schedule(int run_maintenance
, int fd
)
2445 if (run_maintenance
)
2446 return systemd_timer_setup_units();
2448 return systemd_timer_delete_units();
2452 SCHEDULER_INVALID
= -1,
2456 SCHEDULER_LAUNCHCTL
,
2460 static const struct {
2462 int (*is_available
)(void);
2463 int (*update_schedule
)(int run_maintenance
, int fd
);
2464 } scheduler_fn
[] = {
2465 [SCHEDULER_CRON
] = {
2467 .is_available
= is_crontab_available
,
2468 .update_schedule
= crontab_update_schedule
,
2470 [SCHEDULER_SYSTEMD
] = {
2471 .name
= "systemctl",
2472 .is_available
= is_systemd_timer_available
,
2473 .update_schedule
= systemd_timer_update_schedule
,
2475 [SCHEDULER_LAUNCHCTL
] = {
2476 .name
= "launchctl",
2477 .is_available
= is_launchctl_available
,
2478 .update_schedule
= launchctl_update_schedule
,
2480 [SCHEDULER_SCHTASKS
] = {
2482 .is_available
= is_schtasks_available
,
2483 .update_schedule
= schtasks_update_schedule
,
2487 static enum scheduler
parse_scheduler(const char *value
)
2490 return SCHEDULER_INVALID
;
2491 else if (!strcasecmp(value
, "auto"))
2492 return SCHEDULER_AUTO
;
2493 else if (!strcasecmp(value
, "cron") || !strcasecmp(value
, "crontab"))
2494 return SCHEDULER_CRON
;
2495 else if (!strcasecmp(value
, "systemd") ||
2496 !strcasecmp(value
, "systemd-timer"))
2497 return SCHEDULER_SYSTEMD
;
2498 else if (!strcasecmp(value
, "launchctl"))
2499 return SCHEDULER_LAUNCHCTL
;
2500 else if (!strcasecmp(value
, "schtasks"))
2501 return SCHEDULER_SCHTASKS
;
2503 return SCHEDULER_INVALID
;
2506 static int maintenance_opt_scheduler(const struct option
*opt
, const char *arg
,
2509 enum scheduler
*scheduler
= opt
->value
;
2511 BUG_ON_OPT_NEG(unset
);
2513 *scheduler
= parse_scheduler(arg
);
2514 if (*scheduler
== SCHEDULER_INVALID
)
2515 return error(_("unrecognized --scheduler argument '%s'"), arg
);
2519 struct maintenance_start_opts
{
2520 enum scheduler scheduler
;
2523 static enum scheduler
resolve_scheduler(enum scheduler scheduler
)
2525 if (scheduler
!= SCHEDULER_AUTO
)
2528 #if defined(__APPLE__)
2529 return SCHEDULER_LAUNCHCTL
;
2531 #elif defined(GIT_WINDOWS_NATIVE)
2532 return SCHEDULER_SCHTASKS
;
2534 #elif defined(__linux__)
2535 if (is_systemd_timer_available())
2536 return SCHEDULER_SYSTEMD
;
2537 else if (is_crontab_available())
2538 return SCHEDULER_CRON
;
2540 die(_("neither systemd timers nor crontab are available"));
2543 return SCHEDULER_CRON
;
2547 static void validate_scheduler(enum scheduler scheduler
)
2549 if (scheduler
== SCHEDULER_INVALID
)
2550 BUG("invalid scheduler");
2551 if (scheduler
== SCHEDULER_AUTO
)
2552 BUG("resolve_scheduler should have been called before");
2554 if (!scheduler_fn
[scheduler
].is_available())
2555 die(_("%s scheduler is not available"),
2556 scheduler_fn
[scheduler
].name
);
2559 static int update_background_schedule(const struct maintenance_start_opts
*opts
,
2564 struct lock_file lk
;
2565 char *lock_path
= xstrfmt("%s/schedule", the_repository
->objects
->odb
->path
);
2567 if (hold_lock_file_for_update(&lk
, lock_path
, LOCK_NO_DEREF
) < 0) {
2569 return error(_("another process is scheduling background maintenance"));
2572 for (i
= 1; i
< ARRAY_SIZE(scheduler_fn
); i
++) {
2573 if (enable
&& opts
->scheduler
== i
)
2575 if (!scheduler_fn
[i
].is_available())
2577 scheduler_fn
[i
].update_schedule(0, get_lock_file_fd(&lk
));
2581 result
= scheduler_fn
[opts
->scheduler
].update_schedule(
2582 1, get_lock_file_fd(&lk
));
2584 rollback_lock_file(&lk
);
2590 static const char *const builtin_maintenance_start_usage
[] = {
2591 N_("git maintenance start [--scheduler=<scheduler>]"),
2595 static int maintenance_start(int argc
, const char **argv
, const char *prefix
)
2597 struct maintenance_start_opts opts
= { 0 };
2598 struct option options
[] = {
2600 0, "scheduler", &opts
.scheduler
, N_("scheduler"),
2601 N_("scheduler to trigger git maintenance run"),
2602 PARSE_OPT_NONEG
, maintenance_opt_scheduler
),
2605 const char *register_args
[] = { "register", NULL
};
2607 argc
= parse_options(argc
, argv
, prefix
, options
,
2608 builtin_maintenance_start_usage
, 0);
2610 usage_with_options(builtin_maintenance_start_usage
, options
);
2612 opts
.scheduler
= resolve_scheduler(opts
.scheduler
);
2613 validate_scheduler(opts
.scheduler
);
2615 if (maintenance_register(ARRAY_SIZE(register_args
)-1, register_args
, NULL
))
2616 warning(_("failed to add repo to global config"));
2617 return update_background_schedule(&opts
, 1);
2620 static const char *const builtin_maintenance_stop_usage
[] = {
2621 "git maintenance stop",
2625 static int maintenance_stop(int argc
, const char **argv
, const char *prefix
)
2627 struct option options
[] = {
2630 argc
= parse_options(argc
, argv
, prefix
, options
,
2631 builtin_maintenance_stop_usage
, 0);
2633 usage_with_options(builtin_maintenance_stop_usage
, options
);
2634 return update_background_schedule(NULL
, 0);
2637 static const char * const builtin_maintenance_usage
[] = {
2638 N_("git maintenance <subcommand> [<options>]"),
2642 int cmd_maintenance(int argc
, const char **argv
, const char *prefix
)
2644 parse_opt_subcommand_fn
*fn
= NULL
;
2645 struct option builtin_maintenance_options
[] = {
2646 OPT_SUBCOMMAND("run", &fn
, maintenance_run
),
2647 OPT_SUBCOMMAND("start", &fn
, maintenance_start
),
2648 OPT_SUBCOMMAND("stop", &fn
, maintenance_stop
),
2649 OPT_SUBCOMMAND("register", &fn
, maintenance_register
),
2650 OPT_SUBCOMMAND("unregister", &fn
, maintenance_unregister
),
2654 argc
= parse_options(argc
, argv
, prefix
, builtin_maintenance_options
,
2655 builtin_maintenance_usage
, 0);
2656 return fn(argc
, argv
, prefix
);