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
12 #define USE_THE_REPOSITORY_VARIABLE
16 #include "environment.h"
21 #include "parse-options.h"
22 #include "run-command.h"
26 #include "commit-graph.h"
28 #include "object-file.h"
29 #include "object-store-ll.h"
31 #include "pack-objects.h"
35 #include "promisor-remote.h"
44 #define FAILED_RUN "failed to run %s"
46 static const char * const builtin_gc_usage
[] = {
47 N_("git gc [<options>]"),
51 static timestamp_t gc_log_expire_time
;
53 static struct strvec reflog
= STRVEC_INIT
;
54 static struct strvec repack
= STRVEC_INIT
;
55 static struct strvec prune
= STRVEC_INIT
;
56 static struct strvec prune_worktrees
= STRVEC_INIT
;
57 static struct strvec rerere
= STRVEC_INIT
;
59 static struct tempfile
*pidfile
;
60 static struct lock_file log_lock
;
62 static struct string_list pack_garbage
= STRING_LIST_INIT_DUP
;
64 static void clean_pack_garbage(void)
67 for (i
= 0; i
< pack_garbage
.nr
; i
++)
68 unlink_or_warn(pack_garbage
.items
[i
].string
);
69 string_list_clear(&pack_garbage
, 0);
72 static void report_pack_garbage(unsigned seen_bits
, const char *path
)
74 if (seen_bits
== PACKDIR_FILE_IDX
)
75 string_list_append(&pack_garbage
, path
);
78 static void process_log_file(void)
81 if (fstat(get_lock_file_fd(&log_lock
), &st
)) {
83 * Perhaps there was an i/o error or another
84 * unlikely situation. Try to make a note of
85 * this in gc.log along with any existing
88 int saved_errno
= errno
;
89 fprintf(stderr
, _("Failed to fstat %s: %s"),
90 get_lock_file_path(&log_lock
),
91 strerror(saved_errno
));
93 commit_lock_file(&log_lock
);
95 } else if (st
.st_size
) {
96 /* There was some error recorded in the lock file */
97 commit_lock_file(&log_lock
);
99 /* No error, clean up any old gc.log */
100 unlink(git_path("gc.log"));
101 rollback_lock_file(&log_lock
);
105 static void process_log_file_at_exit(void)
111 static int gc_config_is_timestamp_never(const char *var
)
116 if (!git_config_get_value(var
, &value
) && value
) {
117 if (parse_expiry_date(value
, &expire
))
118 die(_("failed to parse '%s' value '%s'"), var
, value
);
128 unsigned long max_cruft_size
;
129 int aggressive_depth
;
130 int aggressive_window
;
131 int gc_auto_threshold
;
132 int gc_auto_pack_limit
;
136 char *prune_worktrees_expire
;
138 char *repack_filter_to
;
139 unsigned long big_pack_threshold
;
140 unsigned long max_delta_cache_size
;
143 #define GC_CONFIG_INIT { \
145 .prune_reflogs = 1, \
147 .aggressive_depth = 50, \
148 .aggressive_window = 250, \
149 .gc_auto_threshold = 6700, \
150 .gc_auto_pack_limit = 50, \
152 .gc_log_expire = xstrdup("1.day.ago"), \
153 .prune_expire = xstrdup("2.weeks.ago"), \
154 .prune_worktrees_expire = xstrdup("3.months.ago"), \
155 .max_delta_cache_size = DEFAULT_DELTA_CACHE_SIZE, \
158 static void gc_config_release(struct gc_config
*cfg
)
160 free(cfg
->gc_log_expire
);
161 free(cfg
->prune_expire
);
162 free(cfg
->prune_worktrees_expire
);
163 free(cfg
->repack_filter
);
164 free(cfg
->repack_filter_to
);
167 static void gc_config(struct gc_config
*cfg
)
172 if (!git_config_get_value("gc.packrefs", &value
)) {
173 if (value
&& !strcmp(value
, "notbare"))
176 cfg
->pack_refs
= git_config_bool("gc.packrefs", value
);
179 if (gc_config_is_timestamp_never("gc.reflogexpire") &&
180 gc_config_is_timestamp_never("gc.reflogexpireunreachable"))
181 cfg
->prune_reflogs
= 0;
183 git_config_get_int("gc.aggressivewindow", &cfg
->aggressive_window
);
184 git_config_get_int("gc.aggressivedepth", &cfg
->aggressive_depth
);
185 git_config_get_int("gc.auto", &cfg
->gc_auto_threshold
);
186 git_config_get_int("gc.autopacklimit", &cfg
->gc_auto_pack_limit
);
187 git_config_get_bool("gc.autodetach", &cfg
->detach_auto
);
188 git_config_get_bool("gc.cruftpacks", &cfg
->cruft_packs
);
189 git_config_get_ulong("gc.maxcruftsize", &cfg
->max_cruft_size
);
191 if (!repo_config_get_expiry(the_repository
, "gc.pruneexpire", &owned
)) {
192 free(cfg
->prune_expire
);
193 cfg
->prune_expire
= owned
;
196 if (!repo_config_get_expiry(the_repository
, "gc.worktreepruneexpire", &owned
)) {
197 free(cfg
->prune_worktrees_expire
);
198 cfg
->prune_worktrees_expire
= owned
;
201 if (!repo_config_get_expiry(the_repository
, "gc.logexpiry", &owned
)) {
202 free(cfg
->gc_log_expire
);
203 cfg
->gc_log_expire
= owned
;
206 git_config_get_ulong("gc.bigpackthreshold", &cfg
->big_pack_threshold
);
207 git_config_get_ulong("pack.deltacachesize", &cfg
->max_delta_cache_size
);
209 if (!git_config_get_string("gc.repackfilter", &owned
)) {
210 free(cfg
->repack_filter
);
211 cfg
->repack_filter
= owned
;
214 if (!git_config_get_string("gc.repackfilterto", &owned
)) {
215 free(cfg
->repack_filter_to
);
216 cfg
->repack_filter_to
= owned
;
219 git_config(git_default_config
, NULL
);
222 enum schedule_priority
{
229 static enum schedule_priority
parse_schedule(const char *value
)
232 return SCHEDULE_NONE
;
233 if (!strcasecmp(value
, "hourly"))
234 return SCHEDULE_HOURLY
;
235 if (!strcasecmp(value
, "daily"))
236 return SCHEDULE_DAILY
;
237 if (!strcasecmp(value
, "weekly"))
238 return SCHEDULE_WEEKLY
;
239 return SCHEDULE_NONE
;
242 struct maintenance_run_opts
{
246 enum schedule_priority schedule
;
248 #define MAINTENANCE_RUN_OPTS_INIT { \
252 static int pack_refs_condition(UNUSED
struct gc_config
*cfg
)
255 * The auto-repacking logic for refs is handled by the ref backends and
256 * exposed via `git pack-refs --auto`. We thus always return truish
257 * here and let the backend decide for us.
262 static int maintenance_task_pack_refs(struct maintenance_run_opts
*opts
,
263 UNUSED
struct gc_config
*cfg
)
265 struct child_process cmd
= CHILD_PROCESS_INIT
;
268 strvec_pushl(&cmd
.args
, "pack-refs", "--all", "--prune", NULL
);
270 strvec_push(&cmd
.args
, "--auto");
272 return run_command(&cmd
);
275 static int too_many_loose_objects(struct gc_config
*cfg
)
278 * Quickly check if a "gc" is needed, by estimating how
279 * many loose objects there are. Because SHA-1 is evenly
280 * distributed, we can check only one and get a reasonable
288 const unsigned hexsz_loose
= the_hash_algo
->hexsz
- 2;
290 dir
= opendir(git_path("objects/17"));
294 auto_threshold
= DIV_ROUND_UP(cfg
->gc_auto_threshold
, 256);
295 while ((ent
= readdir(dir
)) != NULL
) {
296 if (strspn(ent
->d_name
, "0123456789abcdef") != hexsz_loose
||
297 ent
->d_name
[hexsz_loose
] != '\0')
299 if (++num_loose
> auto_threshold
) {
308 static struct packed_git
*find_base_packs(struct string_list
*packs
,
311 struct packed_git
*p
, *base
= NULL
;
313 for (p
= get_all_packs(the_repository
); p
; p
= p
->next
) {
314 if (!p
->pack_local
|| p
->is_cruft
)
317 if (p
->pack_size
>= limit
)
318 string_list_append(packs
, p
->pack_name
);
319 } else if (!base
|| base
->pack_size
< p
->pack_size
) {
325 string_list_append(packs
, base
->pack_name
);
330 static int too_many_packs(struct gc_config
*cfg
)
332 struct packed_git
*p
;
335 if (cfg
->gc_auto_pack_limit
<= 0)
338 for (cnt
= 0, p
= get_all_packs(the_repository
); p
; p
= p
->next
) {
344 * Perhaps check the size of the pack and count only
345 * very small ones here?
349 return cfg
->gc_auto_pack_limit
< cnt
;
352 static uint64_t total_ram(void)
354 #if defined(HAVE_SYSINFO)
359 #elif defined(HAVE_BSD_SYSCTL) && (defined(HW_MEMSIZE) || defined(HW_PHYSMEM))
360 int64_t physical_memory
;
365 # if defined(HW_MEMSIZE)
370 length
= sizeof(int64_t);
371 if (!sysctl(mib
, 2, &physical_memory
, &length
, NULL
, 0))
372 return physical_memory
;
373 #elif defined(GIT_WINDOWS_NATIVE)
374 MEMORYSTATUSEX memInfo
;
376 memInfo
.dwLength
= sizeof(MEMORYSTATUSEX
);
377 if (GlobalMemoryStatusEx(&memInfo
))
378 return memInfo
.ullTotalPhys
;
383 static uint64_t estimate_repack_memory(struct gc_config
*cfg
,
384 struct packed_git
*pack
)
386 unsigned long nr_objects
= repo_approximate_object_count(the_repository
);
387 size_t os_cache
, heap
;
389 if (!pack
|| !nr_objects
)
393 * First we have to scan through at least one pack.
394 * Assume enough room in OS file cache to keep the entire pack
395 * or we may accidentally evict data of other processes from
398 os_cache
= pack
->pack_size
+ pack
->index_size
;
399 /* then pack-objects needs lots more for book keeping */
400 heap
= sizeof(struct object_entry
) * nr_objects
;
402 * internal rev-list --all --objects takes up some memory too,
403 * let's say half of it is for blobs
405 heap
+= sizeof(struct blob
) * nr_objects
/ 2;
407 * and the other half is for trees (commits and tags are
408 * usually insignificant)
410 heap
+= sizeof(struct tree
) * nr_objects
/ 2;
411 /* and then obj_hash[], underestimated in fact */
412 heap
+= sizeof(struct object
*) * nr_objects
;
413 /* revindex is used also */
414 heap
+= (sizeof(off_t
) + sizeof(uint32_t)) * nr_objects
;
416 * read_sha1_file() (either at delta calculation phase, or
417 * writing phase) also fills up the delta base cache
419 heap
+= delta_base_cache_limit
;
420 /* and of course pack-objects has its own delta cache */
421 heap
+= cfg
->max_delta_cache_size
;
423 return os_cache
+ heap
;
426 static int keep_one_pack(struct string_list_item
*item
, void *data UNUSED
)
428 strvec_pushf(&repack
, "--keep-pack=%s", basename(item
->string
));
432 static void add_repack_all_option(struct gc_config
*cfg
,
433 struct string_list
*keep_pack
)
435 if (cfg
->prune_expire
&& !strcmp(cfg
->prune_expire
, "now"))
436 strvec_push(&repack
, "-a");
437 else if (cfg
->cruft_packs
) {
438 strvec_push(&repack
, "--cruft");
439 if (cfg
->prune_expire
)
440 strvec_pushf(&repack
, "--cruft-expiration=%s", cfg
->prune_expire
);
441 if (cfg
->max_cruft_size
)
442 strvec_pushf(&repack
, "--max-cruft-size=%lu",
443 cfg
->max_cruft_size
);
445 strvec_push(&repack
, "-A");
446 if (cfg
->prune_expire
)
447 strvec_pushf(&repack
, "--unpack-unreachable=%s", cfg
->prune_expire
);
451 for_each_string_list(keep_pack
, keep_one_pack
, NULL
);
453 if (cfg
->repack_filter
&& *cfg
->repack_filter
)
454 strvec_pushf(&repack
, "--filter=%s", cfg
->repack_filter
);
455 if (cfg
->repack_filter_to
&& *cfg
->repack_filter_to
)
456 strvec_pushf(&repack
, "--filter-to=%s", cfg
->repack_filter_to
);
459 static void add_repack_incremental_option(void)
461 strvec_push(&repack
, "--no-write-bitmap-index");
464 static int need_to_gc(struct gc_config
*cfg
)
467 * Setting gc.auto to 0 or negative can disable the
470 if (cfg
->gc_auto_threshold
<= 0)
474 * If there are too many loose objects, but not too many
475 * packs, we run "repack -d -l". If there are too many packs,
476 * we run "repack -A -d -l". Otherwise we tell the caller
479 if (too_many_packs(cfg
)) {
480 struct string_list keep_pack
= STRING_LIST_INIT_NODUP
;
482 if (cfg
->big_pack_threshold
) {
483 find_base_packs(&keep_pack
, cfg
->big_pack_threshold
);
484 if (keep_pack
.nr
>= cfg
->gc_auto_pack_limit
) {
485 cfg
->big_pack_threshold
= 0;
486 string_list_clear(&keep_pack
, 0);
487 find_base_packs(&keep_pack
, 0);
490 struct packed_git
*p
= find_base_packs(&keep_pack
, 0);
491 uint64_t mem_have
, mem_want
;
493 mem_have
= total_ram();
494 mem_want
= estimate_repack_memory(cfg
, p
);
497 * Only allow 1/2 of memory for pack-objects, leave
498 * the rest for the OS and other processes in the
501 if (!mem_have
|| mem_want
< mem_have
/ 2)
502 string_list_clear(&keep_pack
, 0);
505 add_repack_all_option(cfg
, &keep_pack
);
506 string_list_clear(&keep_pack
, 0);
507 } else if (too_many_loose_objects(cfg
))
508 add_repack_incremental_option();
512 if (run_hooks(the_repository
, "pre-auto-gc"))
517 /* return NULL on success, else hostname running the gc */
518 static const char *lock_repo_for_gc(int force
, pid_t
* ret_pid
)
520 struct lock_file lock
= LOCK_INIT
;
521 char my_host
[HOST_NAME_MAX
+ 1];
522 struct strbuf sb
= STRBUF_INIT
;
529 if (is_tempfile_active(pidfile
))
533 if (xgethostname(my_host
, sizeof(my_host
)))
534 xsnprintf(my_host
, sizeof(my_host
), "unknown");
536 pidfile_path
= git_pathdup("gc.pid");
537 fd
= hold_lock_file_for_update(&lock
, pidfile_path
,
540 static char locking_host
[HOST_NAME_MAX
+ 1];
541 static char *scan_fmt
;
545 scan_fmt
= xstrfmt("%s %%%ds", "%"SCNuMAX
, HOST_NAME_MAX
);
546 fp
= fopen(pidfile_path
, "r");
547 memset(locking_host
, 0, sizeof(locking_host
));
550 !fstat(fileno(fp
), &st
) &&
552 * 12 hour limit is very generous as gc should
553 * never take that long. On the other hand we
554 * don't really need a strict limit here,
555 * running gc --auto one day late is not a big
556 * problem. --force can be used in manual gc
557 * after the user verifies that no gc is
560 time(NULL
) - st
.st_mtime
<= 12 * 3600 &&
561 fscanf(fp
, scan_fmt
, &pid
, locking_host
) == 2 &&
562 /* be gentle to concurrent "gc" on remote hosts */
563 (strcmp(locking_host
, my_host
) || !kill(pid
, 0) || errno
== EPERM
);
568 rollback_lock_file(&lock
);
575 strbuf_addf(&sb
, "%"PRIuMAX
" %s",
576 (uintmax_t) getpid(), my_host
);
577 write_in_full(fd
, sb
.buf
, sb
.len
);
579 commit_lock_file(&lock
);
580 pidfile
= register_tempfile(pidfile_path
);
586 * Returns 0 if there was no previous error and gc can proceed, 1 if
587 * gc should not proceed due to an error in the last run. Prints a
588 * message and returns with a non-[01] status code if an error occurred
589 * while reading gc.log
591 static int report_last_gc_error(void)
593 struct strbuf sb
= STRBUF_INIT
;
597 char *gc_log_path
= git_pathdup("gc.log");
599 if (stat(gc_log_path
, &st
)) {
603 ret
= die_message_errno(_("cannot stat '%s'"), gc_log_path
);
607 if (st
.st_mtime
< gc_log_expire_time
)
610 len
= strbuf_read_file(&sb
, gc_log_path
, 0);
612 ret
= die_message_errno(_("cannot read '%s'"), gc_log_path
);
615 * A previous gc failed. Report the error, and don't
616 * bother with an automatic gc run since it is likely
617 * to fail in the same way.
619 warning(_("The last gc run reported the following. "
620 "Please correct the root cause\n"
622 "Automatic cleanup will not be performed "
623 "until the file is removed.\n\n"
625 gc_log_path
, sb
.buf
);
634 static void gc_before_repack(struct maintenance_run_opts
*opts
,
635 struct gc_config
*cfg
)
638 * We may be called twice, as both the pre- and
639 * post-daemonized phases will call us, but running these
640 * commands more than once is pointless and wasteful.
646 if (cfg
->pack_refs
&& maintenance_task_pack_refs(opts
, cfg
))
647 die(FAILED_RUN
, "pack-refs");
649 if (cfg
->prune_reflogs
) {
650 struct child_process cmd
= CHILD_PROCESS_INIT
;
653 strvec_pushv(&cmd
.args
, reflog
.v
);
654 if (run_command(&cmd
))
655 die(FAILED_RUN
, reflog
.v
[0]);
662 struct repository
*repo UNUSED
)
670 int keep_largest_pack
= -1;
672 struct child_process rerere_cmd
= CHILD_PROCESS_INIT
;
673 struct maintenance_run_opts opts
= MAINTENANCE_RUN_OPTS_INIT
;
674 struct gc_config cfg
= GC_CONFIG_INIT
;
675 const char *prune_expire_sentinel
= "sentinel";
676 const char *prune_expire_arg
= prune_expire_sentinel
;
679 struct option builtin_gc_options
[] = {
680 OPT__QUIET(&quiet
, N_("suppress progress reporting")),
681 { OPTION_STRING
, 0, "prune", &prune_expire_arg
, N_("date"),
682 N_("prune unreferenced objects"),
683 PARSE_OPT_OPTARG
, NULL
, (intptr_t)prune_expire_arg
},
684 OPT_BOOL(0, "cruft", &cfg
.cruft_packs
, N_("pack unreferenced objects separately")),
685 OPT_MAGNITUDE(0, "max-cruft-size", &cfg
.max_cruft_size
,
686 N_("with --cruft, limit the size of new cruft packs")),
687 OPT_BOOL(0, "aggressive", &aggressive
, N_("be more thorough (increased runtime)")),
688 OPT_BOOL_F(0, "auto", &opts
.auto_flag
, N_("enable auto-gc mode"),
689 PARSE_OPT_NOCOMPLETE
),
690 OPT_BOOL(0, "detach", &opts
.detach
,
691 N_("perform garbage collection in the background")),
692 OPT_BOOL_F(0, "force", &force
,
693 N_("force running gc even if there may be another gc running"),
694 PARSE_OPT_NOCOMPLETE
),
695 OPT_BOOL(0, "keep-largest-pack", &keep_largest_pack
,
696 N_("repack all other packs except the largest pack")),
700 if (argc
== 2 && !strcmp(argv
[1], "-h"))
701 usage_with_options(builtin_gc_usage
, builtin_gc_options
);
703 strvec_pushl(&reflog
, "reflog", "expire", "--all", NULL
);
704 strvec_pushl(&repack
, "repack", "-d", "-l", NULL
);
705 strvec_pushl(&prune
, "prune", "--expire", NULL
);
706 strvec_pushl(&prune_worktrees
, "worktree", "prune", "--expire", NULL
);
707 strvec_pushl(&rerere
, "rerere", "gc", NULL
);
711 if (parse_expiry_date(cfg
.gc_log_expire
, &gc_log_expire_time
))
712 die(_("failed to parse gc.logExpiry value %s"), cfg
.gc_log_expire
);
714 if (cfg
.pack_refs
< 0)
715 cfg
.pack_refs
= !is_bare_repository();
717 argc
= parse_options(argc
, argv
, prefix
, builtin_gc_options
,
718 builtin_gc_usage
, 0);
720 usage_with_options(builtin_gc_usage
, builtin_gc_options
);
722 if (prune_expire_arg
!= prune_expire_sentinel
) {
723 free(cfg
.prune_expire
);
724 cfg
.prune_expire
= xstrdup_or_null(prune_expire_arg
);
726 if (cfg
.prune_expire
&& parse_expiry_date(cfg
.prune_expire
, &dummy
))
727 die(_("failed to parse prune expiry value %s"), cfg
.prune_expire
);
730 strvec_push(&repack
, "-f");
731 if (cfg
.aggressive_depth
> 0)
732 strvec_pushf(&repack
, "--depth=%d", cfg
.aggressive_depth
);
733 if (cfg
.aggressive_window
> 0)
734 strvec_pushf(&repack
, "--window=%d", cfg
.aggressive_window
);
737 strvec_push(&repack
, "-q");
739 if (opts
.auto_flag
) {
740 if (cfg
.detach_auto
&& opts
.detach
< 0)
744 * Auto-gc should be least intrusive as possible.
746 if (!need_to_gc(&cfg
)) {
753 fprintf(stderr
, _("Auto packing the repository in background for optimum performance.\n"));
755 fprintf(stderr
, _("Auto packing the repository for optimum performance.\n"));
756 fprintf(stderr
, _("See \"git help gc\" for manual housekeeping.\n"));
759 struct string_list keep_pack
= STRING_LIST_INIT_NODUP
;
761 if (keep_largest_pack
!= -1) {
762 if (keep_largest_pack
)
763 find_base_packs(&keep_pack
, 0);
764 } else if (cfg
.big_pack_threshold
) {
765 find_base_packs(&keep_pack
, cfg
.big_pack_threshold
);
768 add_repack_all_option(&cfg
, &keep_pack
);
769 string_list_clear(&keep_pack
, 0);
772 if (opts
.detach
> 0) {
773 ret
= report_last_gc_error();
775 /* Last gc --auto failed. Skip this one. */
780 /* an I/O error occurred, already reported */
784 if (lock_repo_for_gc(force
, &pid
)) {
789 gc_before_repack(&opts
, &cfg
); /* dies on failure */
790 delete_tempfile(&pidfile
);
793 * failure to daemonize is ok, we'll continue
796 daemonized
= !daemonize();
799 name
= lock_repo_for_gc(force
, &pid
);
801 if (opts
.auto_flag
) {
803 goto out
; /* be quiet on --auto */
806 die(_("gc is already running on machine '%s' pid %"PRIuMAX
" (use --force if not)"),
807 name
, (uintmax_t)pid
);
811 hold_lock_file_for_update(&log_lock
,
814 dup2(get_lock_file_fd(&log_lock
), 2);
815 atexit(process_log_file_at_exit
);
818 gc_before_repack(&opts
, &cfg
);
820 if (!repository_format_precious_objects
) {
821 struct child_process repack_cmd
= CHILD_PROCESS_INIT
;
823 repack_cmd
.git_cmd
= 1;
824 repack_cmd
.close_object_store
= 1;
825 strvec_pushv(&repack_cmd
.args
, repack
.v
);
826 if (run_command(&repack_cmd
))
827 die(FAILED_RUN
, repack
.v
[0]);
829 if (cfg
.prune_expire
) {
830 struct child_process prune_cmd
= CHILD_PROCESS_INIT
;
832 /* run `git prune` even if using cruft packs */
833 strvec_push(&prune
, cfg
.prune_expire
);
835 strvec_push(&prune
, "--no-progress");
836 if (repo_has_promisor_remote(the_repository
))
838 "--exclude-promisor-objects");
839 prune_cmd
.git_cmd
= 1;
840 strvec_pushv(&prune_cmd
.args
, prune
.v
);
841 if (run_command(&prune_cmd
))
842 die(FAILED_RUN
, prune
.v
[0]);
846 if (cfg
.prune_worktrees_expire
) {
847 struct child_process prune_worktrees_cmd
= CHILD_PROCESS_INIT
;
849 strvec_push(&prune_worktrees
, cfg
.prune_worktrees_expire
);
850 prune_worktrees_cmd
.git_cmd
= 1;
851 strvec_pushv(&prune_worktrees_cmd
.args
, prune_worktrees
.v
);
852 if (run_command(&prune_worktrees_cmd
))
853 die(FAILED_RUN
, prune_worktrees
.v
[0]);
856 rerere_cmd
.git_cmd
= 1;
857 strvec_pushv(&rerere_cmd
.args
, rerere
.v
);
858 if (run_command(&rerere_cmd
))
859 die(FAILED_RUN
, rerere
.v
[0]);
861 report_garbage
= report_pack_garbage
;
862 reprepare_packed_git(the_repository
);
863 if (pack_garbage
.nr
> 0) {
864 close_object_store(the_repository
->objects
);
865 clean_pack_garbage();
868 if (the_repository
->settings
.gc_write_commit_graph
== 1)
869 write_commit_graph_reachable(the_repository
->objects
->odb
,
870 !quiet
&& !daemonized
? COMMIT_GRAPH_WRITE_PROGRESS
: 0,
873 if (opts
.auto_flag
&& too_many_loose_objects(&cfg
))
874 warning(_("There are too many unreachable loose objects; "
875 "run 'git prune' to remove them."));
878 unlink(git_path("gc.log"));
881 gc_config_release(&cfg
);
885 static const char *const builtin_maintenance_run_usage
[] = {
886 N_("git maintenance run [--auto] [--[no-]quiet] [--task=<task>] [--schedule]"),
890 static int maintenance_opt_schedule(const struct option
*opt
, const char *arg
,
893 enum schedule_priority
*priority
= opt
->value
;
896 die(_("--no-schedule is not allowed"));
898 *priority
= parse_schedule(arg
);
901 die(_("unrecognized --schedule argument '%s'"), arg
);
906 /* Remember to update object flag allocation in object.h */
909 struct cg_auto_data
{
910 int num_not_in_graph
;
914 static int dfs_on_ref(const char *refname UNUSED
,
915 const char *referent UNUSED
,
916 const struct object_id
*oid
,
920 struct cg_auto_data
*data
= (struct cg_auto_data
*)cb_data
;
922 struct object_id peeled
;
923 struct commit_list
*stack
= NULL
;
924 struct commit
*commit
;
926 if (!peel_iterated_oid(the_repository
, oid
, &peeled
))
928 if (oid_object_info(the_repository
, oid
, NULL
) != OBJ_COMMIT
)
931 commit
= lookup_commit(the_repository
, oid
);
934 if (repo_parse_commit(the_repository
, commit
) ||
935 commit_graph_position(commit
) != COMMIT_NOT_FROM_GRAPH
)
938 data
->num_not_in_graph
++;
940 if (data
->num_not_in_graph
>= data
->limit
)
943 commit_list_append(commit
, &stack
);
945 while (!result
&& stack
) {
946 struct commit_list
*parent
;
948 commit
= pop_commit(&stack
);
950 for (parent
= commit
->parents
; parent
; parent
= parent
->next
) {
951 if (repo_parse_commit(the_repository
, parent
->item
) ||
952 commit_graph_position(parent
->item
) != COMMIT_NOT_FROM_GRAPH
||
953 parent
->item
->object
.flags
& SEEN
)
956 parent
->item
->object
.flags
|= SEEN
;
957 data
->num_not_in_graph
++;
959 if (data
->num_not_in_graph
>= data
->limit
) {
964 commit_list_append(parent
->item
, &stack
);
968 free_commit_list(stack
);
972 static int should_write_commit_graph(struct gc_config
*cfg UNUSED
)
975 struct cg_auto_data data
;
977 data
.num_not_in_graph
= 0;
979 git_config_get_int("maintenance.commit-graph.auto",
987 result
= refs_for_each_ref(get_main_ref_store(the_repository
),
990 repo_clear_commit_marks(the_repository
, SEEN
);
995 static int run_write_commit_graph(struct maintenance_run_opts
*opts
)
997 struct child_process child
= CHILD_PROCESS_INIT
;
999 child
.git_cmd
= child
.close_object_store
= 1;
1000 strvec_pushl(&child
.args
, "commit-graph", "write",
1001 "--split", "--reachable", NULL
);
1004 strvec_push(&child
.args
, "--no-progress");
1006 return !!run_command(&child
);
1009 static int maintenance_task_commit_graph(struct maintenance_run_opts
*opts
,
1010 struct gc_config
*cfg UNUSED
)
1012 prepare_repo_settings(the_repository
);
1013 if (!the_repository
->settings
.core_commit_graph
)
1016 if (run_write_commit_graph(opts
)) {
1017 error(_("failed to write commit-graph"));
1024 static int fetch_remote(struct remote
*remote
, void *cbdata
)
1026 struct maintenance_run_opts
*opts
= cbdata
;
1027 struct child_process child
= CHILD_PROCESS_INIT
;
1029 if (remote
->skip_default_update
)
1033 strvec_pushl(&child
.args
, "fetch", remote
->name
,
1034 "--prefetch", "--prune", "--no-tags",
1035 "--no-write-fetch-head", "--recurse-submodules=no",
1039 strvec_push(&child
.args
, "--quiet");
1041 return !!run_command(&child
);
1044 static int maintenance_task_prefetch(struct maintenance_run_opts
*opts
,
1045 struct gc_config
*cfg UNUSED
)
1047 if (for_each_remote(fetch_remote
, opts
)) {
1048 error(_("failed to prefetch remotes"));
1055 static int maintenance_task_gc(struct maintenance_run_opts
*opts
,
1056 struct gc_config
*cfg UNUSED
)
1058 struct child_process child
= CHILD_PROCESS_INIT
;
1060 child
.git_cmd
= child
.close_object_store
= 1;
1061 strvec_push(&child
.args
, "gc");
1063 if (opts
->auto_flag
)
1064 strvec_push(&child
.args
, "--auto");
1066 strvec_push(&child
.args
, "--quiet");
1068 strvec_push(&child
.args
, "--no-quiet");
1069 strvec_push(&child
.args
, "--no-detach");
1071 return run_command(&child
);
1074 static int prune_packed(struct maintenance_run_opts
*opts
)
1076 struct child_process child
= CHILD_PROCESS_INIT
;
1079 strvec_push(&child
.args
, "prune-packed");
1082 strvec_push(&child
.args
, "--quiet");
1084 return !!run_command(&child
);
1087 struct write_loose_object_data
{
1093 static int loose_object_auto_limit
= 100;
1095 static int loose_object_count(const struct object_id
*oid UNUSED
,
1096 const char *path UNUSED
,
1099 int *count
= (int*)data
;
1100 if (++(*count
) >= loose_object_auto_limit
)
1105 static int loose_object_auto_condition(struct gc_config
*cfg UNUSED
)
1109 git_config_get_int("maintenance.loose-objects.auto",
1110 &loose_object_auto_limit
);
1112 if (!loose_object_auto_limit
)
1114 if (loose_object_auto_limit
< 0)
1117 return for_each_loose_file_in_objdir(the_repository
->objects
->odb
->path
,
1119 NULL
, NULL
, &count
);
1122 static int bail_on_loose(const struct object_id
*oid UNUSED
,
1123 const char *path UNUSED
,
1129 static int write_loose_object_to_stdin(const struct object_id
*oid
,
1130 const char *path UNUSED
,
1133 struct write_loose_object_data
*d
= (struct write_loose_object_data
*)data
;
1135 fprintf(d
->in
, "%s\n", oid_to_hex(oid
));
1137 return ++(d
->count
) > d
->batch_size
;
1140 static int pack_loose(struct maintenance_run_opts
*opts
)
1142 struct repository
*r
= the_repository
;
1144 struct write_loose_object_data data
;
1145 struct child_process pack_proc
= CHILD_PROCESS_INIT
;
1148 * Do not start pack-objects process
1149 * if there are no loose objects.
1151 if (!for_each_loose_file_in_objdir(r
->objects
->odb
->path
,
1156 pack_proc
.git_cmd
= 1;
1158 strvec_push(&pack_proc
.args
, "pack-objects");
1160 strvec_push(&pack_proc
.args
, "--quiet");
1161 strvec_pushf(&pack_proc
.args
, "%s/pack/loose", r
->objects
->odb
->path
);
1166 * git-pack-objects(1) ends up writing the pack hash to stdout, which
1167 * we do not care for.
1171 if (start_command(&pack_proc
)) {
1172 error(_("failed to start 'git pack-objects' process"));
1176 data
.in
= xfdopen(pack_proc
.in
, "w");
1178 data
.batch_size
= 50000;
1180 for_each_loose_file_in_objdir(r
->objects
->odb
->path
,
1181 write_loose_object_to_stdin
,
1188 if (finish_command(&pack_proc
)) {
1189 error(_("failed to finish 'git pack-objects' process"));
1196 static int maintenance_task_loose_objects(struct maintenance_run_opts
*opts
,
1197 struct gc_config
*cfg UNUSED
)
1199 return prune_packed(opts
) || pack_loose(opts
);
1202 static int incremental_repack_auto_condition(struct gc_config
*cfg UNUSED
)
1204 struct packed_git
*p
;
1205 int incremental_repack_auto_limit
= 10;
1208 prepare_repo_settings(the_repository
);
1209 if (!the_repository
->settings
.core_multi_pack_index
)
1212 git_config_get_int("maintenance.incremental-repack.auto",
1213 &incremental_repack_auto_limit
);
1215 if (!incremental_repack_auto_limit
)
1217 if (incremental_repack_auto_limit
< 0)
1220 for (p
= get_packed_git(the_repository
);
1221 count
< incremental_repack_auto_limit
&& p
;
1223 if (!p
->multi_pack_index
)
1227 return count
>= incremental_repack_auto_limit
;
1230 static int multi_pack_index_write(struct maintenance_run_opts
*opts
)
1232 struct child_process child
= CHILD_PROCESS_INIT
;
1235 strvec_pushl(&child
.args
, "multi-pack-index", "write", NULL
);
1238 strvec_push(&child
.args
, "--no-progress");
1240 if (run_command(&child
))
1241 return error(_("failed to write multi-pack-index"));
1246 static int multi_pack_index_expire(struct maintenance_run_opts
*opts
)
1248 struct child_process child
= CHILD_PROCESS_INIT
;
1250 child
.git_cmd
= child
.close_object_store
= 1;
1251 strvec_pushl(&child
.args
, "multi-pack-index", "expire", NULL
);
1254 strvec_push(&child
.args
, "--no-progress");
1256 if (run_command(&child
))
1257 return error(_("'git multi-pack-index expire' failed"));
1262 #define TWO_GIGABYTES (INT32_MAX)
1264 static off_t
get_auto_pack_size(void)
1267 * The "auto" value is special: we optimize for
1268 * one large pack-file (i.e. from a clone) and
1269 * expect the rest to be small and they can be
1272 * The strategy we select here is to select a
1273 * size that is one more than the second largest
1274 * pack-file. This ensures that we will repack
1275 * at least two packs if there are three or more
1279 off_t second_largest_size
= 0;
1281 struct packed_git
*p
;
1282 struct repository
*r
= the_repository
;
1284 reprepare_packed_git(r
);
1285 for (p
= get_all_packs(r
); p
; p
= p
->next
) {
1286 if (p
->pack_size
> max_size
) {
1287 second_largest_size
= max_size
;
1288 max_size
= p
->pack_size
;
1289 } else if (p
->pack_size
> second_largest_size
)
1290 second_largest_size
= p
->pack_size
;
1293 result_size
= second_largest_size
+ 1;
1295 /* But limit ourselves to a batch size of 2g */
1296 if (result_size
> TWO_GIGABYTES
)
1297 result_size
= TWO_GIGABYTES
;
1302 static int multi_pack_index_repack(struct maintenance_run_opts
*opts
)
1304 struct child_process child
= CHILD_PROCESS_INIT
;
1306 child
.git_cmd
= child
.close_object_store
= 1;
1307 strvec_pushl(&child
.args
, "multi-pack-index", "repack", NULL
);
1310 strvec_push(&child
.args
, "--no-progress");
1312 strvec_pushf(&child
.args
, "--batch-size=%"PRIuMAX
,
1313 (uintmax_t)get_auto_pack_size());
1315 if (run_command(&child
))
1316 return error(_("'git multi-pack-index repack' failed"));
1321 static int maintenance_task_incremental_repack(struct maintenance_run_opts
*opts
,
1322 struct gc_config
*cfg UNUSED
)
1324 prepare_repo_settings(the_repository
);
1325 if (!the_repository
->settings
.core_multi_pack_index
) {
1326 warning(_("skipping incremental-repack task because core.multiPackIndex is disabled"));
1330 if (multi_pack_index_write(opts
))
1332 if (multi_pack_index_expire(opts
))
1334 if (multi_pack_index_repack(opts
))
1339 typedef int maintenance_task_fn(struct maintenance_run_opts
*opts
,
1340 struct gc_config
*cfg
);
1343 * An auto condition function returns 1 if the task should run
1344 * and 0 if the task should NOT run. See needs_to_gc() for an
1347 typedef int maintenance_auto_fn(struct gc_config
*cfg
);
1349 struct maintenance_task
{
1351 maintenance_task_fn
*fn
;
1352 maintenance_auto_fn
*auto_condition
;
1355 enum schedule_priority schedule
;
1357 /* -1 if not selected. */
1361 enum maintenance_task_label
{
1364 TASK_INCREMENTAL_REPACK
,
1369 /* Leave as final value */
1373 static struct maintenance_task tasks
[] = {
1376 maintenance_task_prefetch
,
1378 [TASK_LOOSE_OBJECTS
] = {
1380 maintenance_task_loose_objects
,
1381 loose_object_auto_condition
,
1383 [TASK_INCREMENTAL_REPACK
] = {
1384 "incremental-repack",
1385 maintenance_task_incremental_repack
,
1386 incremental_repack_auto_condition
,
1390 maintenance_task_gc
,
1394 [TASK_COMMIT_GRAPH
] = {
1396 maintenance_task_commit_graph
,
1397 should_write_commit_graph
,
1399 [TASK_PACK_REFS
] = {
1401 maintenance_task_pack_refs
,
1402 pack_refs_condition
,
1406 static int compare_tasks_by_selection(const void *a_
, const void *b_
)
1408 const struct maintenance_task
*a
= a_
;
1409 const struct maintenance_task
*b
= b_
;
1411 return b
->selected_order
- a
->selected_order
;
1414 static int maintenance_run_tasks(struct maintenance_run_opts
*opts
,
1415 struct gc_config
*cfg
)
1417 int i
, found_selected
= 0;
1419 struct lock_file lk
;
1420 struct repository
*r
= the_repository
;
1421 char *lock_path
= xstrfmt("%s/maintenance", r
->objects
->odb
->path
);
1423 if (hold_lock_file_for_update(&lk
, lock_path
, LOCK_NO_DEREF
) < 0) {
1425 * Another maintenance command is running.
1427 * If --auto was provided, then it is likely due to a
1428 * recursive process stack. Do not report an error in
1431 if (!opts
->auto_flag
&& !opts
->quiet
)
1432 warning(_("lock file '%s' exists, skipping maintenance"),
1439 /* Failure to daemonize is ok, we'll continue in foreground. */
1440 if (opts
->detach
> 0) {
1441 trace2_region_enter("maintenance", "detach", the_repository
);
1443 trace2_region_leave("maintenance", "detach", the_repository
);
1446 for (i
= 0; !found_selected
&& i
< TASK__COUNT
; i
++)
1447 found_selected
= tasks
[i
].selected_order
>= 0;
1450 QSORT(tasks
, TASK__COUNT
, compare_tasks_by_selection
);
1452 for (i
= 0; i
< TASK__COUNT
; i
++) {
1453 if (found_selected
&& tasks
[i
].selected_order
< 0)
1456 if (!found_selected
&& !tasks
[i
].enabled
)
1459 if (opts
->auto_flag
&&
1460 (!tasks
[i
].auto_condition
||
1461 !tasks
[i
].auto_condition(cfg
)))
1464 if (opts
->schedule
&& tasks
[i
].schedule
< opts
->schedule
)
1467 trace2_region_enter("maintenance", tasks
[i
].name
, r
);
1468 if (tasks
[i
].fn(opts
, cfg
)) {
1469 error(_("task '%s' failed"), tasks
[i
].name
);
1472 trace2_region_leave("maintenance", tasks
[i
].name
, r
);
1475 rollback_lock_file(&lk
);
1479 static void initialize_maintenance_strategy(void)
1481 const char *config_str
;
1483 if (git_config_get_string_tmp("maintenance.strategy", &config_str
))
1486 if (!strcasecmp(config_str
, "incremental")) {
1487 tasks
[TASK_GC
].schedule
= SCHEDULE_NONE
;
1488 tasks
[TASK_COMMIT_GRAPH
].enabled
= 1;
1489 tasks
[TASK_COMMIT_GRAPH
].schedule
= SCHEDULE_HOURLY
;
1490 tasks
[TASK_PREFETCH
].enabled
= 1;
1491 tasks
[TASK_PREFETCH
].schedule
= SCHEDULE_HOURLY
;
1492 tasks
[TASK_INCREMENTAL_REPACK
].enabled
= 1;
1493 tasks
[TASK_INCREMENTAL_REPACK
].schedule
= SCHEDULE_DAILY
;
1494 tasks
[TASK_LOOSE_OBJECTS
].enabled
= 1;
1495 tasks
[TASK_LOOSE_OBJECTS
].schedule
= SCHEDULE_DAILY
;
1496 tasks
[TASK_PACK_REFS
].enabled
= 1;
1497 tasks
[TASK_PACK_REFS
].schedule
= SCHEDULE_WEEKLY
;
1501 static void initialize_task_config(int schedule
)
1504 struct strbuf config_name
= STRBUF_INIT
;
1507 initialize_maintenance_strategy();
1509 for (i
= 0; i
< TASK__COUNT
; i
++) {
1513 strbuf_reset(&config_name
);
1514 strbuf_addf(&config_name
, "maintenance.%s.enabled",
1517 if (!git_config_get_bool(config_name
.buf
, &config_value
))
1518 tasks
[i
].enabled
= config_value
;
1520 strbuf_reset(&config_name
);
1521 strbuf_addf(&config_name
, "maintenance.%s.schedule",
1524 if (!git_config_get_string(config_name
.buf
, &config_str
)) {
1525 tasks
[i
].schedule
= parse_schedule(config_str
);
1530 strbuf_release(&config_name
);
1533 static int task_option_parse(const struct option
*opt UNUSED
,
1534 const char *arg
, int unset
)
1536 int i
, num_selected
= 0;
1537 struct maintenance_task
*task
= NULL
;
1539 BUG_ON_OPT_NEG(unset
);
1541 for (i
= 0; i
< TASK__COUNT
; i
++) {
1542 if (tasks
[i
].selected_order
>= 0)
1544 if (!strcasecmp(tasks
[i
].name
, arg
)) {
1550 error(_("'%s' is not a valid task"), arg
);
1554 if (task
->selected_order
>= 0) {
1555 error(_("task '%s' cannot be selected multiple times"), arg
);
1559 task
->selected_order
= num_selected
+ 1;
1564 static int maintenance_run(int argc
, const char **argv
, const char *prefix
)
1567 struct maintenance_run_opts opts
= MAINTENANCE_RUN_OPTS_INIT
;
1568 struct gc_config cfg
= GC_CONFIG_INIT
;
1569 struct option builtin_maintenance_run_options
[] = {
1570 OPT_BOOL(0, "auto", &opts
.auto_flag
,
1571 N_("run tasks based on the state of the repository")),
1572 OPT_BOOL(0, "detach", &opts
.detach
,
1573 N_("perform maintenance in the background")),
1574 OPT_CALLBACK(0, "schedule", &opts
.schedule
, N_("frequency"),
1575 N_("run tasks based on frequency"),
1576 maintenance_opt_schedule
),
1577 OPT_BOOL(0, "quiet", &opts
.quiet
,
1578 N_("do not report progress or other information over stderr")),
1579 OPT_CALLBACK_F(0, "task", NULL
, N_("task"),
1580 N_("run a specific task"),
1581 PARSE_OPT_NONEG
, task_option_parse
),
1586 opts
.quiet
= !isatty(2);
1588 for (i
= 0; i
< TASK__COUNT
; i
++)
1589 tasks
[i
].selected_order
= -1;
1591 argc
= parse_options(argc
, argv
, prefix
,
1592 builtin_maintenance_run_options
,
1593 builtin_maintenance_run_usage
,
1594 PARSE_OPT_STOP_AT_NON_OPTION
);
1596 if (opts
.auto_flag
&& opts
.schedule
)
1597 die(_("use at most one of --auto and --schedule=<frequency>"));
1600 initialize_task_config(opts
.schedule
);
1603 usage_with_options(builtin_maintenance_run_usage
,
1604 builtin_maintenance_run_options
);
1606 ret
= maintenance_run_tasks(&opts
, &cfg
);
1607 gc_config_release(&cfg
);
1611 static char *get_maintpath(void)
1613 struct strbuf sb
= STRBUF_INIT
;
1614 const char *p
= the_repository
->worktree
?
1615 the_repository
->worktree
: the_repository
->gitdir
;
1617 strbuf_realpath(&sb
, p
, 1);
1618 return strbuf_detach(&sb
, NULL
);
1621 static char const * const builtin_maintenance_register_usage
[] = {
1622 "git maintenance register [--config-file <path>]",
1626 static int maintenance_register(int argc
, const char **argv
, const char *prefix
)
1628 char *config_file
= NULL
;
1629 struct option options
[] = {
1630 OPT_STRING(0, "config-file", &config_file
, N_("file"), N_("use given config file")),
1634 const char *key
= "maintenance.repo";
1635 char *maintpath
= get_maintpath();
1636 struct string_list_item
*item
;
1637 const struct string_list
*list
;
1639 argc
= parse_options(argc
, argv
, prefix
, options
,
1640 builtin_maintenance_register_usage
, 0);
1642 usage_with_options(builtin_maintenance_register_usage
,
1645 /* Disable foreground maintenance */
1646 git_config_set("maintenance.auto", "false");
1648 /* Set maintenance strategy, if unset */
1649 if (git_config_get("maintenance.strategy"))
1650 git_config_set("maintenance.strategy", "incremental");
1652 if (!git_config_get_string_multi(key
, &list
)) {
1653 for_each_string_list_item(item
, list
) {
1654 if (!strcmp(maintpath
, item
->string
)) {
1663 char *global_config_file
= NULL
;
1666 global_config_file
= git_global_config();
1667 config_file
= global_config_file
;
1670 die(_("$HOME not set"));
1671 rc
= git_config_set_multivar_in_file_gently(
1672 config_file
, "maintenance.repo", maintpath
,
1673 CONFIG_REGEX_NONE
, NULL
, 0);
1674 free(global_config_file
);
1677 die(_("unable to add '%s' value of '%s'"),
1685 static char const * const builtin_maintenance_unregister_usage
[] = {
1686 "git maintenance unregister [--config-file <path>] [--force]",
1690 static int maintenance_unregister(int argc
, const char **argv
, const char *prefix
)
1693 char *config_file
= NULL
;
1694 struct option options
[] = {
1695 OPT_STRING(0, "config-file", &config_file
, N_("file"), N_("use given config file")),
1697 N_("return success even if repository was not registered"),
1698 PARSE_OPT_NOCOMPLETE
),
1701 const char *key
= "maintenance.repo";
1702 char *maintpath
= get_maintpath();
1704 struct string_list_item
*item
;
1705 const struct string_list
*list
;
1706 struct config_set cs
= { { 0 } };
1708 argc
= parse_options(argc
, argv
, prefix
, options
,
1709 builtin_maintenance_unregister_usage
, 0);
1711 usage_with_options(builtin_maintenance_unregister_usage
,
1715 git_configset_init(&cs
);
1716 git_configset_add_file(&cs
, config_file
);
1719 ? git_configset_get_string_multi(&cs
, key
, &list
)
1720 : git_config_get_string_multi(key
, &list
))) {
1721 for_each_string_list_item(item
, list
) {
1722 if (!strcmp(maintpath
, item
->string
)) {
1731 char *global_config_file
= NULL
;
1734 global_config_file
= git_global_config();
1735 config_file
= global_config_file
;
1738 die(_("$HOME not set"));
1739 rc
= git_config_set_multivar_in_file_gently(
1740 config_file
, key
, NULL
, maintpath
, NULL
,
1741 CONFIG_FLAGS_MULTI_REPLACE
| CONFIG_FLAGS_FIXED_VALUE
);
1742 free(global_config_file
);
1745 (!force
|| rc
== CONFIG_NOTHING_SET
))
1746 die(_("unable to unset '%s' value of '%s'"),
1748 } else if (!force
) {
1749 die(_("repository '%s' is not registered"), maintpath
);
1752 git_configset_clear(&cs
);
1757 static const char *get_frequency(enum schedule_priority schedule
)
1760 case SCHEDULE_HOURLY
:
1762 case SCHEDULE_DAILY
:
1764 case SCHEDULE_WEEKLY
:
1767 BUG("invalid schedule %d", schedule
);
1771 static const char *extraconfig
[] = {
1772 "credential.interactive=false",
1773 "core.askPass=true", /* 'true' returns success, but no output. */
1777 static const char *get_extra_config_parameters(void) {
1778 static const char *result
= NULL
;
1779 struct strbuf builder
= STRBUF_INIT
;
1784 for (const char **s
= extraconfig
; s
&& *s
; s
++)
1785 strbuf_addf(&builder
, "-c %s ", *s
);
1787 result
= strbuf_detach(&builder
, NULL
);
1791 static const char *get_extra_launchctl_strings(void) {
1792 static const char *result
= NULL
;
1793 struct strbuf builder
= STRBUF_INIT
;
1798 for (const char **s
= extraconfig
; s
&& *s
; s
++) {
1799 strbuf_addstr(&builder
, "<string>-c</string>\n");
1800 strbuf_addf(&builder
, "<string>%s</string>\n", *s
);
1803 result
= strbuf_detach(&builder
, NULL
);
1808 * get_schedule_cmd` reads the GIT_TEST_MAINT_SCHEDULER environment variable
1809 * to mock the schedulers that `git maintenance start` rely on.
1811 * For test purpose, GIT_TEST_MAINT_SCHEDULER can be set to a comma-separated
1812 * list of colon-separated key/value pairs where each pair contains a scheduler
1813 * and its corresponding mock.
1815 * * If $GIT_TEST_MAINT_SCHEDULER is not set, return false and leave the
1816 * arguments unmodified.
1818 * * If $GIT_TEST_MAINT_SCHEDULER is set, return true.
1819 * In this case, the *cmd value is read as input.
1821 * * if the input value cmd is the key of one of the comma-separated list
1822 * item, then *is_available is set to true and *out is set to
1825 * * if the input value *cmd isn’t the key of any of the comma-separated list
1826 * item, then *is_available is set to false and *out is set to the original
1830 * GIT_TEST_MAINT_SCHEDULER not set
1831 * +-------+-------------------------------------------------+
1832 * | Input | Output |
1833 * | *cmd | return code | *out | *is_available |
1834 * +-------+-------------+-------------------+---------------+
1835 * | "foo" | false | NULL | (unchanged) |
1836 * +-------+-------------+-------------------+---------------+
1838 * GIT_TEST_MAINT_SCHEDULER set to “foo:./mock_foo.sh,bar:./mock_bar.sh”
1839 * +-------+-------------------------------------------------+
1840 * | Input | Output |
1841 * | *cmd | return code | *out | *is_available |
1842 * +-------+-------------+-------------------+---------------+
1843 * | "foo" | true | "./mock.foo.sh" | true |
1844 * | "qux" | true | "qux" (allocated) | false |
1845 * +-------+-------------+-------------------+---------------+
1847 static int get_schedule_cmd(const char *cmd
, int *is_available
, char **out
)
1849 char *testing
= xstrdup_or_null(getenv("GIT_TEST_MAINT_SCHEDULER"));
1850 struct string_list_item
*item
;
1851 struct string_list list
= STRING_LIST_INIT_NODUP
;
1859 string_list_split_in_place(&list
, testing
, ",", -1);
1860 for_each_string_list_item(item
, &list
) {
1861 struct string_list pair
= STRING_LIST_INIT_NODUP
;
1863 if (string_list_split_in_place(&pair
, item
->string
, ":", 2) != 2)
1866 if (!strcmp(cmd
, pair
.items
[0].string
)) {
1868 *out
= xstrdup(pair
.items
[1].string
);
1871 string_list_clear(&pair
, 0);
1875 string_list_clear(&pair
, 0);
1879 *out
= xstrdup(cmd
);
1882 string_list_clear(&list
, 0);
1887 static int get_random_minute(void)
1889 /* Use a static value when under tests. */
1890 if (getenv("GIT_TEST_MAINT_SCHEDULER"))
1893 return git_rand() % 60;
1896 static int is_launchctl_available(void)
1899 if (get_schedule_cmd("launchctl", &is_available
, NULL
))
1900 return is_available
;
1909 static char *launchctl_service_name(const char *frequency
)
1911 struct strbuf label
= STRBUF_INIT
;
1912 strbuf_addf(&label
, "org.git-scm.git.%s", frequency
);
1913 return strbuf_detach(&label
, NULL
);
1916 static char *launchctl_service_filename(const char *name
)
1919 struct strbuf filename
= STRBUF_INIT
;
1920 strbuf_addf(&filename
, "~/Library/LaunchAgents/%s.plist", name
);
1922 expanded
= interpolate_path(filename
.buf
, 1);
1924 die(_("failed to expand path '%s'"), filename
.buf
);
1926 strbuf_release(&filename
);
1930 static char *launchctl_get_uid(void)
1932 return xstrfmt("gui/%d", getuid());
1935 static int launchctl_boot_plist(int enable
, const char *filename
)
1939 struct child_process child
= CHILD_PROCESS_INIT
;
1940 char *uid
= launchctl_get_uid();
1942 get_schedule_cmd("launchctl", NULL
, &cmd
);
1943 strvec_split(&child
.args
, cmd
);
1944 strvec_pushl(&child
.args
, enable
? "bootstrap" : "bootout", uid
,
1947 child
.no_stderr
= 1;
1948 child
.no_stdout
= 1;
1950 if (start_command(&child
))
1951 die(_("failed to start launchctl"));
1953 result
= finish_command(&child
);
1960 static int launchctl_remove_plist(enum schedule_priority schedule
)
1962 const char *frequency
= get_frequency(schedule
);
1963 char *name
= launchctl_service_name(frequency
);
1964 char *filename
= launchctl_service_filename(name
);
1965 int result
= launchctl_boot_plist(0, filename
);
1972 static int launchctl_remove_plists(void)
1974 return launchctl_remove_plist(SCHEDULE_HOURLY
) ||
1975 launchctl_remove_plist(SCHEDULE_DAILY
) ||
1976 launchctl_remove_plist(SCHEDULE_WEEKLY
);
1979 static int launchctl_list_contains_plist(const char *name
, const char *cmd
)
1981 struct child_process child
= CHILD_PROCESS_INIT
;
1983 strvec_split(&child
.args
, cmd
);
1984 strvec_pushl(&child
.args
, "list", name
, NULL
);
1986 child
.no_stderr
= 1;
1987 child
.no_stdout
= 1;
1989 if (start_command(&child
))
1990 die(_("failed to start launchctl"));
1992 /* Returns failure if 'name' doesn't exist. */
1993 return !finish_command(&child
);
1996 static int launchctl_schedule_plist(const char *exec_path
, enum schedule_priority schedule
)
1999 const char *preamble
, *repeat
;
2000 const char *frequency
= get_frequency(schedule
);
2001 char *name
= launchctl_service_name(frequency
);
2002 char *filename
= launchctl_service_filename(name
);
2003 struct lock_file lk
= LOCK_INIT
;
2004 static unsigned long lock_file_timeout_ms
= ULONG_MAX
;
2005 struct strbuf plist
= STRBUF_INIT
, plist2
= STRBUF_INIT
;
2008 int minute
= get_random_minute();
2010 get_schedule_cmd("launchctl", NULL
, &cmd
);
2011 preamble
= "<?xml version=\"1.0\"?>\n"
2012 "<!DOCTYPE plist PUBLIC \"-//Apple//DTD PLIST 1.0//EN\" \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">\n"
2013 "<plist version=\"1.0\">"
2015 "<key>Label</key><string>%s</string>\n"
2016 "<key>ProgramArguments</key>\n"
2018 "<string>%s/git</string>\n"
2019 "<string>--exec-path=%s</string>\n"
2020 "%s" /* For extra config parameters. */
2021 "<string>for-each-repo</string>\n"
2022 "<string>--keep-going</string>\n"
2023 "<string>--config=maintenance.repo</string>\n"
2024 "<string>maintenance</string>\n"
2025 "<string>run</string>\n"
2026 "<string>--schedule=%s</string>\n"
2028 "<key>StartCalendarInterval</key>\n"
2030 strbuf_addf(&plist
, preamble
, name
, exec_path
, exec_path
,
2031 get_extra_launchctl_strings(), frequency
);
2034 case SCHEDULE_HOURLY
:
2036 "<key>Hour</key><integer>%d</integer>\n"
2037 "<key>Minute</key><integer>%d</integer>\n"
2039 for (i
= 1; i
<= 23; i
++)
2040 strbuf_addf(&plist
, repeat
, i
, minute
);
2043 case SCHEDULE_DAILY
:
2045 "<key>Day</key><integer>%d</integer>\n"
2046 "<key>Hour</key><integer>0</integer>\n"
2047 "<key>Minute</key><integer>%d</integer>\n"
2049 for (i
= 1; i
<= 6; i
++)
2050 strbuf_addf(&plist
, repeat
, i
, minute
);
2053 case SCHEDULE_WEEKLY
:
2056 "<key>Day</key><integer>0</integer>\n"
2057 "<key>Hour</key><integer>0</integer>\n"
2058 "<key>Minute</key><integer>%d</integer>\n"
2067 strbuf_addstr(&plist
, "</array>\n</dict>\n</plist>\n");
2069 if (safe_create_leading_directories(filename
))
2070 die(_("failed to create directories for '%s'"), filename
);
2072 if ((long)lock_file_timeout_ms
< 0 &&
2073 git_config_get_ulong("gc.launchctlplistlocktimeoutms",
2074 &lock_file_timeout_ms
))
2075 lock_file_timeout_ms
= 150;
2077 fd
= hold_lock_file_for_update_timeout(&lk
, filename
, LOCK_DIE_ON_ERROR
,
2078 lock_file_timeout_ms
);
2081 * Does this file already exist? With the intended contents? Is it
2082 * registered already? Then it does not need to be re-registered.
2084 if (!stat(filename
, &st
) && st
.st_size
== plist
.len
&&
2085 strbuf_read_file(&plist2
, filename
, plist
.len
) == plist
.len
&&
2086 !strbuf_cmp(&plist
, &plist2
) &&
2087 launchctl_list_contains_plist(name
, cmd
))
2088 rollback_lock_file(&lk
);
2090 if (write_in_full(fd
, plist
.buf
, plist
.len
) < 0 ||
2091 commit_lock_file(&lk
))
2092 die_errno(_("could not write '%s'"), filename
);
2094 /* bootout might fail if not already running, so ignore */
2095 launchctl_boot_plist(0, filename
);
2096 if (launchctl_boot_plist(1, filename
))
2097 die(_("failed to bootstrap service %s"), filename
);
2103 strbuf_release(&plist
);
2104 strbuf_release(&plist2
);
2108 static int launchctl_add_plists(void)
2110 const char *exec_path
= git_exec_path();
2112 return launchctl_schedule_plist(exec_path
, SCHEDULE_HOURLY
) ||
2113 launchctl_schedule_plist(exec_path
, SCHEDULE_DAILY
) ||
2114 launchctl_schedule_plist(exec_path
, SCHEDULE_WEEKLY
);
2117 static int launchctl_update_schedule(int run_maintenance
, int fd UNUSED
)
2119 if (run_maintenance
)
2120 return launchctl_add_plists();
2122 return launchctl_remove_plists();
2125 static int is_schtasks_available(void)
2128 if (get_schedule_cmd("schtasks", &is_available
, NULL
))
2129 return is_available
;
2131 #ifdef GIT_WINDOWS_NATIVE
2138 static char *schtasks_task_name(const char *frequency
)
2140 struct strbuf label
= STRBUF_INIT
;
2141 strbuf_addf(&label
, "Git Maintenance (%s)", frequency
);
2142 return strbuf_detach(&label
, NULL
);
2145 static int schtasks_remove_task(enum schedule_priority schedule
)
2148 struct child_process child
= CHILD_PROCESS_INIT
;
2149 const char *frequency
= get_frequency(schedule
);
2150 char *name
= schtasks_task_name(frequency
);
2152 get_schedule_cmd("schtasks", NULL
, &cmd
);
2153 strvec_split(&child
.args
, cmd
);
2154 strvec_pushl(&child
.args
, "/delete", "/tn", name
, "/f", NULL
);
2158 return run_command(&child
);
2161 static int schtasks_remove_tasks(void)
2163 return schtasks_remove_task(SCHEDULE_HOURLY
) ||
2164 schtasks_remove_task(SCHEDULE_DAILY
) ||
2165 schtasks_remove_task(SCHEDULE_WEEKLY
);
2168 static int schtasks_schedule_task(const char *exec_path
, enum schedule_priority schedule
)
2172 struct child_process child
= CHILD_PROCESS_INIT
;
2174 struct tempfile
*tfile
;
2175 const char *frequency
= get_frequency(schedule
);
2176 char *name
= schtasks_task_name(frequency
);
2177 struct strbuf tfilename
= STRBUF_INIT
;
2178 int minute
= get_random_minute();
2180 get_schedule_cmd("schtasks", NULL
, &cmd
);
2182 strbuf_addf(&tfilename
, "%s/schedule_%s_XXXXXX",
2183 repo_get_common_dir(the_repository
), frequency
);
2184 tfile
= xmks_tempfile(tfilename
.buf
);
2185 strbuf_release(&tfilename
);
2187 if (!fdopen_tempfile(tfile
, "w"))
2188 die(_("failed to create temp xml file"));
2190 xml
= "<?xml version=\"1.0\" ?>\n"
2191 "<Task version=\"1.4\" xmlns=\"http://schemas.microsoft.com/windows/2004/02/mit/task\">\n"
2193 "<CalendarTrigger>\n";
2194 fputs(xml
, tfile
->fp
);
2197 case SCHEDULE_HOURLY
:
2199 "<StartBoundary>2020-01-01T01:%02d:00</StartBoundary>\n"
2200 "<Enabled>true</Enabled>\n"
2202 "<DaysInterval>1</DaysInterval>\n"
2203 "</ScheduleByDay>\n"
2205 "<Interval>PT1H</Interval>\n"
2206 "<Duration>PT23H</Duration>\n"
2207 "<StopAtDurationEnd>false</StopAtDurationEnd>\n"
2212 case SCHEDULE_DAILY
:
2214 "<StartBoundary>2020-01-01T00:%02d:00</StartBoundary>\n"
2215 "<Enabled>true</Enabled>\n"
2216 "<ScheduleByWeek>\n"
2225 "<WeeksInterval>1</WeeksInterval>\n"
2226 "</ScheduleByWeek>\n",
2230 case SCHEDULE_WEEKLY
:
2232 "<StartBoundary>2020-01-01T00:%02d:00</StartBoundary>\n"
2233 "<Enabled>true</Enabled>\n"
2234 "<ScheduleByWeek>\n"
2238 "<WeeksInterval>1</WeeksInterval>\n"
2239 "</ScheduleByWeek>\n",
2247 xml
= "</CalendarTrigger>\n"
2250 "<Principal id=\"Author\">\n"
2251 "<LogonType>InteractiveToken</LogonType>\n"
2252 "<RunLevel>LeastPrivilege</RunLevel>\n"
2256 "<MultipleInstancesPolicy>IgnoreNew</MultipleInstancesPolicy>\n"
2257 "<Enabled>true</Enabled>\n"
2258 "<Hidden>true</Hidden>\n"
2259 "<UseUnifiedSchedulingEngine>true</UseUnifiedSchedulingEngine>\n"
2260 "<WakeToRun>false</WakeToRun>\n"
2261 "<ExecutionTimeLimit>PT72H</ExecutionTimeLimit>\n"
2262 "<Priority>7</Priority>\n"
2264 "<Actions Context=\"Author\">\n"
2266 "<Command>\"%s\\headless-git.exe\"</Command>\n"
2267 "<Arguments>--exec-path=\"%s\" %s for-each-repo --keep-going --config=maintenance.repo maintenance run --schedule=%s</Arguments>\n"
2271 fprintf(tfile
->fp
, xml
, exec_path
, exec_path
,
2272 get_extra_config_parameters(), frequency
);
2273 strvec_split(&child
.args
, cmd
);
2274 strvec_pushl(&child
.args
, "/create", "/tn", name
, "/f", "/xml",
2275 get_tempfile_path(tfile
), NULL
);
2276 close_tempfile_gently(tfile
);
2278 child
.no_stdout
= 1;
2279 child
.no_stderr
= 1;
2281 if (start_command(&child
))
2282 die(_("failed to start schtasks"));
2283 result
= finish_command(&child
);
2285 delete_tempfile(&tfile
);
2291 static int schtasks_schedule_tasks(void)
2293 const char *exec_path
= git_exec_path();
2295 return schtasks_schedule_task(exec_path
, SCHEDULE_HOURLY
) ||
2296 schtasks_schedule_task(exec_path
, SCHEDULE_DAILY
) ||
2297 schtasks_schedule_task(exec_path
, SCHEDULE_WEEKLY
);
2300 static int schtasks_update_schedule(int run_maintenance
, int fd UNUSED
)
2302 if (run_maintenance
)
2303 return schtasks_schedule_tasks();
2305 return schtasks_remove_tasks();
2309 static int check_crontab_process(const char *cmd
)
2311 struct child_process child
= CHILD_PROCESS_INIT
;
2313 strvec_split(&child
.args
, cmd
);
2314 strvec_push(&child
.args
, "-l");
2316 child
.no_stdout
= 1;
2317 child
.no_stderr
= 1;
2318 child
.silent_exec_failure
= 1;
2320 if (start_command(&child
))
2322 /* Ignore exit code, as an empty crontab will return error. */
2323 finish_command(&child
);
2327 static int is_crontab_available(void)
2333 if (get_schedule_cmd("crontab", &is_available
, &cmd
)) {
2340 * macOS has cron, but it requires special permissions and will
2341 * create a UI alert when attempting to run this command.
2345 ret
= check_crontab_process(cmd
);
2353 #define BEGIN_LINE "# BEGIN GIT MAINTENANCE SCHEDULE"
2354 #define END_LINE "# END GIT MAINTENANCE SCHEDULE"
2356 static int crontab_update_schedule(int run_maintenance
, int fd
)
2360 int in_old_region
= 0;
2361 struct child_process crontab_list
= CHILD_PROCESS_INIT
;
2362 struct child_process crontab_edit
= CHILD_PROCESS_INIT
;
2363 FILE *cron_list
, *cron_in
;
2364 struct strbuf line
= STRBUF_INIT
;
2365 struct tempfile
*tmpedit
= NULL
;
2366 int minute
= get_random_minute();
2368 get_schedule_cmd("crontab", NULL
, &cmd
);
2369 strvec_split(&crontab_list
.args
, cmd
);
2370 strvec_push(&crontab_list
.args
, "-l");
2371 crontab_list
.in
= -1;
2372 crontab_list
.out
= dup(fd
);
2373 crontab_list
.git_cmd
= 0;
2375 if (start_command(&crontab_list
)) {
2376 result
= error(_("failed to run 'crontab -l'; your system might not support 'cron'"));
2380 /* Ignore exit code, as an empty crontab will return error. */
2381 finish_command(&crontab_list
);
2383 tmpedit
= mks_tempfile_t(".git_cron_edit_tmpXXXXXX");
2385 result
= error(_("failed to create crontab temporary file"));
2388 cron_in
= fdopen_tempfile(tmpedit
, "w");
2390 result
= error(_("failed to open temporary file"));
2395 * Read from the .lock file, filtering out the old
2396 * schedule while appending the new schedule.
2398 cron_list
= fdopen(fd
, "r");
2401 while (!strbuf_getline_lf(&line
, cron_list
)) {
2402 if (!in_old_region
&& !strcmp(line
.buf
, BEGIN_LINE
))
2404 else if (in_old_region
&& !strcmp(line
.buf
, END_LINE
))
2406 else if (!in_old_region
)
2407 fprintf(cron_in
, "%s\n", line
.buf
);
2409 strbuf_release(&line
);
2411 if (run_maintenance
) {
2412 struct strbuf line_format
= STRBUF_INIT
;
2413 const char *exec_path
= git_exec_path();
2415 fprintf(cron_in
, "%s\n", BEGIN_LINE
);
2417 "# The following schedule was created by Git\n");
2418 fprintf(cron_in
, "# Any edits made in this region might be\n");
2420 "# replaced in the future by a Git command.\n\n");
2422 strbuf_addf(&line_format
,
2423 "%%d %%s * * %%s \"%s/git\" --exec-path=\"%s\" %s for-each-repo --keep-going --config=maintenance.repo maintenance run --schedule=%%s\n",
2424 exec_path
, exec_path
, get_extra_config_parameters());
2425 fprintf(cron_in
, line_format
.buf
, minute
, "1-23", "*", "hourly");
2426 fprintf(cron_in
, line_format
.buf
, minute
, "0", "1-6", "daily");
2427 fprintf(cron_in
, line_format
.buf
, minute
, "0", "0", "weekly");
2428 strbuf_release(&line_format
);
2430 fprintf(cron_in
, "\n%s\n", END_LINE
);
2435 strvec_split(&crontab_edit
.args
, cmd
);
2436 strvec_push(&crontab_edit
.args
, get_tempfile_path(tmpedit
));
2437 crontab_edit
.git_cmd
= 0;
2439 if (start_command(&crontab_edit
)) {
2440 result
= error(_("failed to run 'crontab'; your system might not support 'cron'"));
2444 if (finish_command(&crontab_edit
))
2445 result
= error(_("'crontab' died"));
2450 delete_tempfile(&tmpedit
);
2455 static int real_is_systemd_timer_available(void)
2457 struct child_process child
= CHILD_PROCESS_INIT
;
2459 strvec_pushl(&child
.args
, "systemctl", "--user", "list-timers", NULL
);
2461 child
.no_stdout
= 1;
2462 child
.no_stderr
= 1;
2463 child
.silent_exec_failure
= 1;
2465 if (start_command(&child
))
2467 if (finish_command(&child
))
2472 static int is_systemd_timer_available(void)
2476 if (get_schedule_cmd("systemctl", &is_available
, NULL
))
2477 return is_available
;
2479 return real_is_systemd_timer_available();
2482 static char *xdg_config_home_systemd(const char *filename
)
2484 return xdg_config_home_for("systemd/user", filename
);
2487 #define SYSTEMD_UNIT_FORMAT "git-maintenance@%s.%s"
2489 static int systemd_timer_delete_timer_file(enum schedule_priority priority
)
2492 const char *frequency
= get_frequency(priority
);
2493 char *local_timer_name
= xstrfmt(SYSTEMD_UNIT_FORMAT
, frequency
, "timer");
2494 char *filename
= xdg_config_home_systemd(local_timer_name
);
2496 if (unlink(filename
) && !is_missing_file_error(errno
))
2497 ret
= error_errno(_("failed to delete '%s'"), filename
);
2500 free(local_timer_name
);
2504 static int systemd_timer_delete_service_template(void)
2507 char *local_service_name
= xstrfmt(SYSTEMD_UNIT_FORMAT
, "", "service");
2508 char *filename
= xdg_config_home_systemd(local_service_name
);
2509 if (unlink(filename
) && !is_missing_file_error(errno
))
2510 ret
= error_errno(_("failed to delete '%s'"), filename
);
2513 free(local_service_name
);
2518 * Write the schedule information into a git-maintenance@<schedule>.timer
2519 * file using a custom minute. This timer file cannot use the templating
2520 * system, so we generate a specific file for each.
2522 static int systemd_timer_write_timer_file(enum schedule_priority schedule
,
2529 char *schedule_pattern
= NULL
;
2530 const char *frequency
= get_frequency(schedule
);
2531 char *local_timer_name
= xstrfmt(SYSTEMD_UNIT_FORMAT
, frequency
, "timer");
2533 filename
= xdg_config_home_systemd(local_timer_name
);
2535 if (safe_create_leading_directories(filename
)) {
2536 error(_("failed to create directories for '%s'"), filename
);
2539 file
= fopen_or_warn(filename
, "w");
2544 case SCHEDULE_HOURLY
:
2545 schedule_pattern
= xstrfmt("*-*-* 1..23:%02d:00", minute
);
2548 case SCHEDULE_DAILY
:
2549 schedule_pattern
= xstrfmt("Tue..Sun *-*-* 0:%02d:00", minute
);
2552 case SCHEDULE_WEEKLY
:
2553 schedule_pattern
= xstrfmt("Mon 0:%02d:00", minute
);
2557 BUG("Unhandled schedule_priority");
2560 unit
= "# This file was created and is maintained by Git.\n"
2561 "# Any edits made in this file might be replaced in the future\n"
2562 "# by a Git command.\n"
2565 "Description=Optimize Git repositories data\n"
2572 "WantedBy=timers.target\n";
2573 if (fprintf(file
, unit
, schedule_pattern
) < 0) {
2574 error(_("failed to write to '%s'"), filename
);
2578 if (fclose(file
) == EOF
) {
2579 error_errno(_("failed to flush '%s'"), filename
);
2586 free(schedule_pattern
);
2587 free(local_timer_name
);
2593 * No matter the schedule, we use the same service and can make use of the
2594 * templating system. When installing git-maintenance@<schedule>.timer,
2595 * systemd will notice that git-maintenance@.service exists as a template
2596 * and will use this file and insert the <schedule> into the template at
2597 * the position of "%i".
2599 static int systemd_timer_write_service_template(const char *exec_path
)
2605 char *local_service_name
= xstrfmt(SYSTEMD_UNIT_FORMAT
, "", "service");
2607 filename
= xdg_config_home_systemd(local_service_name
);
2608 if (safe_create_leading_directories(filename
)) {
2609 error(_("failed to create directories for '%s'"), filename
);
2612 file
= fopen_or_warn(filename
, "w");
2616 unit
= "# This file was created and is maintained by Git.\n"
2617 "# Any edits made in this file might be replaced in the future\n"
2618 "# by a Git command.\n"
2621 "Description=Optimize Git repositories data\n"
2625 "ExecStart=\"%s/git\" --exec-path=\"%s\" %s for-each-repo --keep-going --config=maintenance.repo maintenance run --schedule=%%i\n"
2626 "LockPersonality=yes\n"
2627 "MemoryDenyWriteExecute=yes\n"
2628 "NoNewPrivileges=yes\n"
2629 "RestrictAddressFamilies=AF_UNIX AF_INET AF_INET6 AF_VSOCK\n"
2630 "RestrictNamespaces=yes\n"
2631 "RestrictRealtime=yes\n"
2632 "RestrictSUIDSGID=yes\n"
2633 "SystemCallArchitectures=native\n"
2634 "SystemCallFilter=@system-service\n";
2635 if (fprintf(file
, unit
, exec_path
, exec_path
, get_extra_config_parameters()) < 0) {
2636 error(_("failed to write to '%s'"), filename
);
2640 if (fclose(file
) == EOF
) {
2641 error_errno(_("failed to flush '%s'"), filename
);
2648 free(local_service_name
);
2653 static int systemd_timer_enable_unit(int enable
,
2654 enum schedule_priority schedule
,
2658 struct child_process child
= CHILD_PROCESS_INIT
;
2659 const char *frequency
= get_frequency(schedule
);
2663 * Disabling the systemd unit while it is already disabled makes
2664 * systemctl print an error.
2665 * Let's ignore it since it means we already are in the expected state:
2666 * the unit is disabled.
2668 * On the other hand, enabling a systemd unit which is already enabled
2669 * produces no error.
2672 child
.no_stderr
= 1;
2673 } else if (systemd_timer_write_timer_file(schedule
, minute
)) {
2678 get_schedule_cmd("systemctl", NULL
, &cmd
);
2679 strvec_split(&child
.args
, cmd
);
2680 strvec_pushl(&child
.args
, "--user", enable
? "enable" : "disable",
2682 strvec_pushf(&child
.args
, SYSTEMD_UNIT_FORMAT
, frequency
, "timer");
2684 if (start_command(&child
)) {
2685 ret
= error(_("failed to start systemctl"));
2689 if (finish_command(&child
)) {
2691 * Disabling an already disabled systemd unit makes
2693 * Let's ignore this failure.
2695 * Enabling an enabled systemd unit doesn't fail.
2698 ret
= error(_("failed to run systemctl"));
2711 * A previous version of Git wrote the timer units as template files.
2712 * Clean these up, if they exist.
2714 static void systemd_timer_delete_stale_timer_templates(void)
2716 char *timer_template_name
= xstrfmt(SYSTEMD_UNIT_FORMAT
, "", "timer");
2717 char *filename
= xdg_config_home_systemd(timer_template_name
);
2719 if (unlink(filename
) && !is_missing_file_error(errno
))
2720 warning(_("failed to delete '%s'"), filename
);
2723 free(timer_template_name
);
2726 static int systemd_timer_delete_unit_files(void)
2728 systemd_timer_delete_stale_timer_templates();
2730 /* Purposefully not short-circuited to make sure all are called. */
2731 return systemd_timer_delete_timer_file(SCHEDULE_HOURLY
) |
2732 systemd_timer_delete_timer_file(SCHEDULE_DAILY
) |
2733 systemd_timer_delete_timer_file(SCHEDULE_WEEKLY
) |
2734 systemd_timer_delete_service_template();
2737 static int systemd_timer_delete_units(void)
2739 int minute
= get_random_minute();
2740 /* Purposefully not short-circuited to make sure all are called. */
2741 return systemd_timer_enable_unit(0, SCHEDULE_HOURLY
, minute
) |
2742 systemd_timer_enable_unit(0, SCHEDULE_DAILY
, minute
) |
2743 systemd_timer_enable_unit(0, SCHEDULE_WEEKLY
, minute
) |
2744 systemd_timer_delete_unit_files();
2747 static int systemd_timer_setup_units(void)
2749 int minute
= get_random_minute();
2750 const char *exec_path
= git_exec_path();
2752 int ret
= systemd_timer_write_service_template(exec_path
) ||
2753 systemd_timer_enable_unit(1, SCHEDULE_HOURLY
, minute
) ||
2754 systemd_timer_enable_unit(1, SCHEDULE_DAILY
, minute
) ||
2755 systemd_timer_enable_unit(1, SCHEDULE_WEEKLY
, minute
);
2758 systemd_timer_delete_units();
2760 systemd_timer_delete_stale_timer_templates();
2765 static int systemd_timer_update_schedule(int run_maintenance
, int fd UNUSED
)
2767 if (run_maintenance
)
2768 return systemd_timer_setup_units();
2770 return systemd_timer_delete_units();
2774 SCHEDULER_INVALID
= -1,
2778 SCHEDULER_LAUNCHCTL
,
2782 static const struct {
2784 int (*is_available
)(void);
2785 int (*update_schedule
)(int run_maintenance
, int fd
);
2786 } scheduler_fn
[] = {
2787 [SCHEDULER_CRON
] = {
2789 .is_available
= is_crontab_available
,
2790 .update_schedule
= crontab_update_schedule
,
2792 [SCHEDULER_SYSTEMD
] = {
2793 .name
= "systemctl",
2794 .is_available
= is_systemd_timer_available
,
2795 .update_schedule
= systemd_timer_update_schedule
,
2797 [SCHEDULER_LAUNCHCTL
] = {
2798 .name
= "launchctl",
2799 .is_available
= is_launchctl_available
,
2800 .update_schedule
= launchctl_update_schedule
,
2802 [SCHEDULER_SCHTASKS
] = {
2804 .is_available
= is_schtasks_available
,
2805 .update_schedule
= schtasks_update_schedule
,
2809 static enum scheduler
parse_scheduler(const char *value
)
2812 return SCHEDULER_INVALID
;
2813 else if (!strcasecmp(value
, "auto"))
2814 return SCHEDULER_AUTO
;
2815 else if (!strcasecmp(value
, "cron") || !strcasecmp(value
, "crontab"))
2816 return SCHEDULER_CRON
;
2817 else if (!strcasecmp(value
, "systemd") ||
2818 !strcasecmp(value
, "systemd-timer"))
2819 return SCHEDULER_SYSTEMD
;
2820 else if (!strcasecmp(value
, "launchctl"))
2821 return SCHEDULER_LAUNCHCTL
;
2822 else if (!strcasecmp(value
, "schtasks"))
2823 return SCHEDULER_SCHTASKS
;
2825 return SCHEDULER_INVALID
;
2828 static int maintenance_opt_scheduler(const struct option
*opt
, const char *arg
,
2831 enum scheduler
*scheduler
= opt
->value
;
2833 BUG_ON_OPT_NEG(unset
);
2835 *scheduler
= parse_scheduler(arg
);
2836 if (*scheduler
== SCHEDULER_INVALID
)
2837 return error(_("unrecognized --scheduler argument '%s'"), arg
);
2841 struct maintenance_start_opts
{
2842 enum scheduler scheduler
;
2845 static enum scheduler
resolve_scheduler(enum scheduler scheduler
)
2847 if (scheduler
!= SCHEDULER_AUTO
)
2850 #if defined(__APPLE__)
2851 return SCHEDULER_LAUNCHCTL
;
2853 #elif defined(GIT_WINDOWS_NATIVE)
2854 return SCHEDULER_SCHTASKS
;
2856 #elif defined(__linux__)
2857 if (is_systemd_timer_available())
2858 return SCHEDULER_SYSTEMD
;
2859 else if (is_crontab_available())
2860 return SCHEDULER_CRON
;
2862 die(_("neither systemd timers nor crontab are available"));
2865 return SCHEDULER_CRON
;
2869 static void validate_scheduler(enum scheduler scheduler
)
2871 if (scheduler
== SCHEDULER_INVALID
)
2872 BUG("invalid scheduler");
2873 if (scheduler
== SCHEDULER_AUTO
)
2874 BUG("resolve_scheduler should have been called before");
2876 if (!scheduler_fn
[scheduler
].is_available())
2877 die(_("%s scheduler is not available"),
2878 scheduler_fn
[scheduler
].name
);
2881 static int update_background_schedule(const struct maintenance_start_opts
*opts
,
2886 struct lock_file lk
;
2887 char *lock_path
= xstrfmt("%s/schedule", the_repository
->objects
->odb
->path
);
2889 if (hold_lock_file_for_update(&lk
, lock_path
, LOCK_NO_DEREF
) < 0) {
2891 return error(_("another process is scheduling background maintenance"));
2894 for (i
= 1; i
< ARRAY_SIZE(scheduler_fn
); i
++) {
2895 if (enable
&& opts
->scheduler
== i
)
2897 if (!scheduler_fn
[i
].is_available())
2899 scheduler_fn
[i
].update_schedule(0, get_lock_file_fd(&lk
));
2903 result
= scheduler_fn
[opts
->scheduler
].update_schedule(
2904 1, get_lock_file_fd(&lk
));
2906 rollback_lock_file(&lk
);
2912 static const char *const builtin_maintenance_start_usage
[] = {
2913 N_("git maintenance start [--scheduler=<scheduler>]"),
2917 static int maintenance_start(int argc
, const char **argv
, const char *prefix
)
2919 struct maintenance_start_opts opts
= { 0 };
2920 struct option options
[] = {
2922 0, "scheduler", &opts
.scheduler
, N_("scheduler"),
2923 N_("scheduler to trigger git maintenance run"),
2924 PARSE_OPT_NONEG
, maintenance_opt_scheduler
),
2927 const char *register_args
[] = { "register", NULL
};
2929 argc
= parse_options(argc
, argv
, prefix
, options
,
2930 builtin_maintenance_start_usage
, 0);
2932 usage_with_options(builtin_maintenance_start_usage
, options
);
2934 opts
.scheduler
= resolve_scheduler(opts
.scheduler
);
2935 validate_scheduler(opts
.scheduler
);
2937 if (update_background_schedule(&opts
, 1))
2938 die(_("failed to set up maintenance schedule"));
2940 if (maintenance_register(ARRAY_SIZE(register_args
)-1, register_args
, NULL
))
2941 warning(_("failed to add repo to global config"));
2945 static const char *const builtin_maintenance_stop_usage
[] = {
2946 "git maintenance stop",
2950 static int maintenance_stop(int argc
, const char **argv
, const char *prefix
)
2952 struct option options
[] = {
2955 argc
= parse_options(argc
, argv
, prefix
, options
,
2956 builtin_maintenance_stop_usage
, 0);
2958 usage_with_options(builtin_maintenance_stop_usage
, options
);
2959 return update_background_schedule(NULL
, 0);
2962 static const char * const builtin_maintenance_usage
[] = {
2963 N_("git maintenance <subcommand> [<options>]"),
2967 int cmd_maintenance(int argc
,
2970 struct repository
*repo UNUSED
)
2972 parse_opt_subcommand_fn
*fn
= NULL
;
2973 struct option builtin_maintenance_options
[] = {
2974 OPT_SUBCOMMAND("run", &fn
, maintenance_run
),
2975 OPT_SUBCOMMAND("start", &fn
, maintenance_start
),
2976 OPT_SUBCOMMAND("stop", &fn
, maintenance_stop
),
2977 OPT_SUBCOMMAND("register", &fn
, maintenance_register
),
2978 OPT_SUBCOMMAND("unregister", &fn
, maintenance_unregister
),
2982 argc
= parse_options(argc
, argv
, prefix
, builtin_maintenance_options
,
2983 builtin_maintenance_usage
, 0);
2984 return fn(argc
, argv
, prefix
);