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
14 #include "repository.h"
18 #include "parse-options.h"
19 #include "run-command.h"
23 #include "commit-graph.h"
25 #include "object-store.h"
27 #include "pack-objects.h"
30 #include "promisor-remote.h"
36 #define FAILED_RUN "failed to run %s"
38 static const char * const builtin_gc_usage
[] = {
39 N_("git gc [<options>]"),
43 static int pack_refs
= 1;
44 static int prune_reflogs
= 1;
45 static int cruft_packs
= 0;
46 static int aggressive_depth
= 50;
47 static int aggressive_window
= 250;
48 static int gc_auto_threshold
= 6700;
49 static int gc_auto_pack_limit
= 50;
50 static int detach_auto
= 1;
51 static timestamp_t gc_log_expire_time
;
52 static const char *gc_log_expire
= "1.day.ago";
53 static const char *prune_expire
= "2.weeks.ago";
54 static const char *prune_worktrees_expire
= "3.months.ago";
55 static unsigned long big_pack_threshold
;
56 static unsigned long max_delta_cache_size
= DEFAULT_DELTA_CACHE_SIZE
;
58 static struct strvec reflog
= STRVEC_INIT
;
59 static struct strvec repack
= STRVEC_INIT
;
60 static struct strvec prune
= STRVEC_INIT
;
61 static struct strvec prune_worktrees
= STRVEC_INIT
;
62 static struct strvec rerere
= STRVEC_INIT
;
64 static struct tempfile
*pidfile
;
65 static struct lock_file log_lock
;
67 static struct string_list pack_garbage
= STRING_LIST_INIT_DUP
;
69 static void clean_pack_garbage(void)
72 for (i
= 0; i
< pack_garbage
.nr
; i
++)
73 unlink_or_warn(pack_garbage
.items
[i
].string
);
74 string_list_clear(&pack_garbage
, 0);
77 static void report_pack_garbage(unsigned seen_bits
, const char *path
)
79 if (seen_bits
== PACKDIR_FILE_IDX
)
80 string_list_append(&pack_garbage
, path
);
83 static void process_log_file(void)
86 if (fstat(get_lock_file_fd(&log_lock
), &st
)) {
88 * Perhaps there was an i/o error or another
89 * unlikely situation. Try to make a note of
90 * this in gc.log along with any existing
93 int saved_errno
= errno
;
94 fprintf(stderr
, _("Failed to fstat %s: %s"),
95 get_lock_file_path(&log_lock
),
96 strerror(saved_errno
));
98 commit_lock_file(&log_lock
);
100 } else if (st
.st_size
) {
101 /* There was some error recorded in the lock file */
102 commit_lock_file(&log_lock
);
104 /* No error, clean up any old gc.log */
105 unlink(git_path("gc.log"));
106 rollback_lock_file(&log_lock
);
110 static void process_log_file_at_exit(void)
116 static void process_log_file_on_signal(int signo
)
123 static int gc_config_is_timestamp_never(const char *var
)
128 if (!git_config_get_value(var
, &value
) && value
) {
129 if (parse_expiry_date(value
, &expire
))
130 die(_("failed to parse '%s' value '%s'"), var
, value
);
136 static void gc_config(void)
140 if (!git_config_get_value("gc.packrefs", &value
)) {
141 if (value
&& !strcmp(value
, "notbare"))
144 pack_refs
= git_config_bool("gc.packrefs", value
);
147 if (gc_config_is_timestamp_never("gc.reflogexpire") &&
148 gc_config_is_timestamp_never("gc.reflogexpireunreachable"))
151 git_config_get_int("gc.aggressivewindow", &aggressive_window
);
152 git_config_get_int("gc.aggressivedepth", &aggressive_depth
);
153 git_config_get_int("gc.auto", &gc_auto_threshold
);
154 git_config_get_int("gc.autopacklimit", &gc_auto_pack_limit
);
155 git_config_get_bool("gc.autodetach", &detach_auto
);
156 git_config_get_bool("gc.cruftpacks", &cruft_packs
);
157 git_config_get_expiry("gc.pruneexpire", &prune_expire
);
158 git_config_get_expiry("gc.worktreepruneexpire", &prune_worktrees_expire
);
159 git_config_get_expiry("gc.logexpiry", &gc_log_expire
);
161 git_config_get_ulong("gc.bigpackthreshold", &big_pack_threshold
);
162 git_config_get_ulong("pack.deltacachesize", &max_delta_cache_size
);
164 git_config(git_default_config
, NULL
);
167 struct maintenance_run_opts
;
168 static int maintenance_task_pack_refs(MAYBE_UNUSED
struct maintenance_run_opts
*opts
)
170 const char *argv
[] = { "pack-refs", "--all", "--prune", NULL
};
172 return run_command_v_opt(argv
, RUN_GIT_CMD
);
175 static int too_many_loose_objects(void)
178 * Quickly check if a "gc" is needed, by estimating how
179 * many loose objects there are. Because SHA-1 is evenly
180 * distributed, we can check only one and get a reasonable
188 const unsigned hexsz_loose
= the_hash_algo
->hexsz
- 2;
190 dir
= opendir(git_path("objects/17"));
194 auto_threshold
= DIV_ROUND_UP(gc_auto_threshold
, 256);
195 while ((ent
= readdir(dir
)) != NULL
) {
196 if (strspn(ent
->d_name
, "0123456789abcdef") != hexsz_loose
||
197 ent
->d_name
[hexsz_loose
] != '\0')
199 if (++num_loose
> auto_threshold
) {
208 static struct packed_git
*find_base_packs(struct string_list
*packs
,
211 struct packed_git
*p
, *base
= NULL
;
213 for (p
= get_all_packs(the_repository
); p
; p
= p
->next
) {
217 if (p
->pack_size
>= limit
)
218 string_list_append(packs
, p
->pack_name
);
219 } else if (!base
|| base
->pack_size
< p
->pack_size
) {
225 string_list_append(packs
, base
->pack_name
);
230 static int too_many_packs(void)
232 struct packed_git
*p
;
235 if (gc_auto_pack_limit
<= 0)
238 for (cnt
= 0, p
= get_all_packs(the_repository
); p
; p
= p
->next
) {
244 * Perhaps check the size of the pack and count only
245 * very small ones here?
249 return gc_auto_pack_limit
< cnt
;
252 static uint64_t total_ram(void)
254 #if defined(HAVE_SYSINFO)
259 #elif defined(HAVE_BSD_SYSCTL) && (defined(HW_MEMSIZE) || defined(HW_PHYSMEM))
260 int64_t physical_memory
;
265 # if defined(HW_MEMSIZE)
270 length
= sizeof(int64_t);
271 if (!sysctl(mib
, 2, &physical_memory
, &length
, NULL
, 0))
272 return physical_memory
;
273 #elif defined(GIT_WINDOWS_NATIVE)
274 MEMORYSTATUSEX memInfo
;
276 memInfo
.dwLength
= sizeof(MEMORYSTATUSEX
);
277 if (GlobalMemoryStatusEx(&memInfo
))
278 return memInfo
.ullTotalPhys
;
283 static uint64_t estimate_repack_memory(struct packed_git
*pack
)
285 unsigned long nr_objects
= approximate_object_count();
286 size_t os_cache
, heap
;
288 if (!pack
|| !nr_objects
)
292 * First we have to scan through at least one pack.
293 * Assume enough room in OS file cache to keep the entire pack
294 * or we may accidentally evict data of other processes from
297 os_cache
= pack
->pack_size
+ pack
->index_size
;
298 /* then pack-objects needs lots more for book keeping */
299 heap
= sizeof(struct object_entry
) * nr_objects
;
301 * internal rev-list --all --objects takes up some memory too,
302 * let's say half of it is for blobs
304 heap
+= sizeof(struct blob
) * nr_objects
/ 2;
306 * and the other half is for trees (commits and tags are
307 * usually insignificant)
309 heap
+= sizeof(struct tree
) * nr_objects
/ 2;
310 /* and then obj_hash[], underestimated in fact */
311 heap
+= sizeof(struct object
*) * nr_objects
;
312 /* revindex is used also */
313 heap
+= (sizeof(off_t
) + sizeof(uint32_t)) * nr_objects
;
315 * read_sha1_file() (either at delta calculation phase, or
316 * writing phase) also fills up the delta base cache
318 heap
+= delta_base_cache_limit
;
319 /* and of course pack-objects has its own delta cache */
320 heap
+= max_delta_cache_size
;
322 return os_cache
+ heap
;
325 static int keep_one_pack(struct string_list_item
*item
, void *data
)
327 strvec_pushf(&repack
, "--keep-pack=%s", basename(item
->string
));
331 static void add_repack_all_option(struct string_list
*keep_pack
)
333 if (prune_expire
&& !strcmp(prune_expire
, "now"))
334 strvec_push(&repack
, "-a");
335 else if (cruft_packs
) {
336 strvec_push(&repack
, "--cruft");
338 strvec_pushf(&repack
, "--cruft-expiration=%s", prune_expire
);
340 strvec_push(&repack
, "-A");
342 strvec_pushf(&repack
, "--unpack-unreachable=%s", prune_expire
);
346 for_each_string_list(keep_pack
, keep_one_pack
, NULL
);
349 static void add_repack_incremental_option(void)
351 strvec_push(&repack
, "--no-write-bitmap-index");
354 static int need_to_gc(void)
357 * Setting gc.auto to 0 or negative can disable the
360 if (gc_auto_threshold
<= 0)
364 * If there are too many loose objects, but not too many
365 * packs, we run "repack -d -l". If there are too many packs,
366 * we run "repack -A -d -l". Otherwise we tell the caller
369 if (too_many_packs()) {
370 struct string_list keep_pack
= STRING_LIST_INIT_NODUP
;
372 if (big_pack_threshold
) {
373 find_base_packs(&keep_pack
, big_pack_threshold
);
374 if (keep_pack
.nr
>= gc_auto_pack_limit
) {
375 big_pack_threshold
= 0;
376 string_list_clear(&keep_pack
, 0);
377 find_base_packs(&keep_pack
, 0);
380 struct packed_git
*p
= find_base_packs(&keep_pack
, 0);
381 uint64_t mem_have
, mem_want
;
383 mem_have
= total_ram();
384 mem_want
= estimate_repack_memory(p
);
387 * Only allow 1/2 of memory for pack-objects, leave
388 * the rest for the OS and other processes in the
391 if (!mem_have
|| mem_want
< mem_have
/ 2)
392 string_list_clear(&keep_pack
, 0);
395 add_repack_all_option(&keep_pack
);
396 string_list_clear(&keep_pack
, 0);
397 } else if (too_many_loose_objects())
398 add_repack_incremental_option();
402 if (run_hooks("pre-auto-gc"))
407 /* return NULL on success, else hostname running the gc */
408 static const char *lock_repo_for_gc(int force
, pid_t
* ret_pid
)
410 struct lock_file lock
= LOCK_INIT
;
411 char my_host
[HOST_NAME_MAX
+ 1];
412 struct strbuf sb
= STRBUF_INIT
;
419 if (is_tempfile_active(pidfile
))
423 if (xgethostname(my_host
, sizeof(my_host
)))
424 xsnprintf(my_host
, sizeof(my_host
), "unknown");
426 pidfile_path
= git_pathdup("gc.pid");
427 fd
= hold_lock_file_for_update(&lock
, pidfile_path
,
430 static char locking_host
[HOST_NAME_MAX
+ 1];
431 static char *scan_fmt
;
435 scan_fmt
= xstrfmt("%s %%%ds", "%"SCNuMAX
, HOST_NAME_MAX
);
436 fp
= fopen(pidfile_path
, "r");
437 memset(locking_host
, 0, sizeof(locking_host
));
440 !fstat(fileno(fp
), &st
) &&
442 * 12 hour limit is very generous as gc should
443 * never take that long. On the other hand we
444 * don't really need a strict limit here,
445 * running gc --auto one day late is not a big
446 * problem. --force can be used in manual gc
447 * after the user verifies that no gc is
450 time(NULL
) - st
.st_mtime
<= 12 * 3600 &&
451 fscanf(fp
, scan_fmt
, &pid
, locking_host
) == 2 &&
452 /* be gentle to concurrent "gc" on remote hosts */
453 (strcmp(locking_host
, my_host
) || !kill(pid
, 0) || errno
== EPERM
);
458 rollback_lock_file(&lock
);
465 strbuf_addf(&sb
, "%"PRIuMAX
" %s",
466 (uintmax_t) getpid(), my_host
);
467 write_in_full(fd
, sb
.buf
, sb
.len
);
469 commit_lock_file(&lock
);
470 pidfile
= register_tempfile(pidfile_path
);
476 * Returns 0 if there was no previous error and gc can proceed, 1 if
477 * gc should not proceed due to an error in the last run. Prints a
478 * message and returns with a non-[01] status code if an error occurred
479 * while reading gc.log
481 static int report_last_gc_error(void)
483 struct strbuf sb
= STRBUF_INIT
;
487 char *gc_log_path
= git_pathdup("gc.log");
489 if (stat(gc_log_path
, &st
)) {
493 ret
= die_message_errno(_("cannot stat '%s'"), gc_log_path
);
497 if (st
.st_mtime
< gc_log_expire_time
)
500 len
= strbuf_read_file(&sb
, gc_log_path
, 0);
502 ret
= die_message_errno(_("cannot read '%s'"), gc_log_path
);
505 * A previous gc failed. Report the error, and don't
506 * bother with an automatic gc run since it is likely
507 * to fail in the same way.
509 warning(_("The last gc run reported the following. "
510 "Please correct the root cause\n"
512 "Automatic cleanup will not be performed "
513 "until the file is removed.\n\n"
515 gc_log_path
, sb
.buf
);
524 static void gc_before_repack(void)
527 * We may be called twice, as both the pre- and
528 * post-daemonized phases will call us, but running these
529 * commands more than once is pointless and wasteful.
535 if (pack_refs
&& maintenance_task_pack_refs(NULL
))
536 die(FAILED_RUN
, "pack-refs");
538 if (prune_reflogs
&& run_command_v_opt(reflog
.v
, RUN_GIT_CMD
))
539 die(FAILED_RUN
, reflog
.v
[0]);
542 int cmd_gc(int argc
, const char **argv
, const char *prefix
)
551 int keep_largest_pack
= -1;
554 struct option builtin_gc_options
[] = {
555 OPT__QUIET(&quiet
, N_("suppress progress reporting")),
556 { OPTION_STRING
, 0, "prune", &prune_expire
, N_("date"),
557 N_("prune unreferenced objects"),
558 PARSE_OPT_OPTARG
, NULL
, (intptr_t)prune_expire
},
559 OPT_BOOL(0, "cruft", &cruft_packs
, N_("pack unreferenced objects separately")),
560 OPT_BOOL(0, "aggressive", &aggressive
, N_("be more thorough (increased runtime)")),
561 OPT_BOOL_F(0, "auto", &auto_gc
, N_("enable auto-gc mode"),
562 PARSE_OPT_NOCOMPLETE
),
563 OPT_BOOL_F(0, "force", &force
,
564 N_("force running gc even if there may be another gc running"),
565 PARSE_OPT_NOCOMPLETE
),
566 OPT_BOOL(0, "keep-largest-pack", &keep_largest_pack
,
567 N_("repack all other packs except the largest pack")),
571 if (argc
== 2 && !strcmp(argv
[1], "-h"))
572 usage_with_options(builtin_gc_usage
, builtin_gc_options
);
574 strvec_pushl(&reflog
, "reflog", "expire", "--all", NULL
);
575 strvec_pushl(&repack
, "repack", "-d", "-l", NULL
);
576 strvec_pushl(&prune
, "prune", "--expire", NULL
);
577 strvec_pushl(&prune_worktrees
, "worktree", "prune", "--expire", NULL
);
578 strvec_pushl(&rerere
, "rerere", "gc", NULL
);
580 /* default expiry time, overwritten in gc_config */
582 if (parse_expiry_date(gc_log_expire
, &gc_log_expire_time
))
583 die(_("failed to parse gc.logExpiry value %s"), gc_log_expire
);
586 pack_refs
= !is_bare_repository();
588 argc
= parse_options(argc
, argv
, prefix
, builtin_gc_options
,
589 builtin_gc_usage
, 0);
591 usage_with_options(builtin_gc_usage
, builtin_gc_options
);
593 if (prune_expire
&& parse_expiry_date(prune_expire
, &dummy
))
594 die(_("failed to parse prune expiry value %s"), prune_expire
);
597 strvec_push(&repack
, "-f");
598 if (aggressive_depth
> 0)
599 strvec_pushf(&repack
, "--depth=%d", aggressive_depth
);
600 if (aggressive_window
> 0)
601 strvec_pushf(&repack
, "--window=%d", aggressive_window
);
604 strvec_push(&repack
, "-q");
608 * Auto-gc should be least intrusive as possible.
614 fprintf(stderr
, _("Auto packing the repository in background for optimum performance.\n"));
616 fprintf(stderr
, _("Auto packing the repository for optimum performance.\n"));
617 fprintf(stderr
, _("See \"git help gc\" for manual housekeeping.\n"));
620 int ret
= report_last_gc_error();
623 /* Last gc --auto failed. Skip this one. */
626 /* an I/O error occurred, already reported */
629 if (lock_repo_for_gc(force
, &pid
))
631 gc_before_repack(); /* dies on failure */
632 delete_tempfile(&pidfile
);
635 * failure to daemonize is ok, we'll continue
638 daemonized
= !daemonize();
641 struct string_list keep_pack
= STRING_LIST_INIT_NODUP
;
643 if (keep_largest_pack
!= -1) {
644 if (keep_largest_pack
)
645 find_base_packs(&keep_pack
, 0);
646 } else if (big_pack_threshold
) {
647 find_base_packs(&keep_pack
, big_pack_threshold
);
650 add_repack_all_option(&keep_pack
);
651 string_list_clear(&keep_pack
, 0);
654 name
= lock_repo_for_gc(force
, &pid
);
657 return 0; /* be quiet on --auto */
658 die(_("gc is already running on machine '%s' pid %"PRIuMAX
" (use --force if not)"),
659 name
, (uintmax_t)pid
);
663 hold_lock_file_for_update(&log_lock
,
666 dup2(get_lock_file_fd(&log_lock
), 2);
667 sigchain_push_common(process_log_file_on_signal
);
668 atexit(process_log_file_at_exit
);
673 if (!repository_format_precious_objects
) {
674 if (run_command_v_opt(repack
.v
,
675 RUN_GIT_CMD
| RUN_CLOSE_OBJECT_STORE
))
676 die(FAILED_RUN
, repack
.v
[0]);
679 /* run `git prune` even if using cruft packs */
680 strvec_push(&prune
, prune_expire
);
682 strvec_push(&prune
, "--no-progress");
683 if (has_promisor_remote())
685 "--exclude-promisor-objects");
686 if (run_command_v_opt(prune
.v
, RUN_GIT_CMD
))
687 die(FAILED_RUN
, prune
.v
[0]);
691 if (prune_worktrees_expire
) {
692 strvec_push(&prune_worktrees
, prune_worktrees_expire
);
693 if (run_command_v_opt(prune_worktrees
.v
, RUN_GIT_CMD
))
694 die(FAILED_RUN
, prune_worktrees
.v
[0]);
697 if (run_command_v_opt(rerere
.v
, RUN_GIT_CMD
))
698 die(FAILED_RUN
, rerere
.v
[0]);
700 report_garbage
= report_pack_garbage
;
701 reprepare_packed_git(the_repository
);
702 if (pack_garbage
.nr
> 0) {
703 close_object_store(the_repository
->objects
);
704 clean_pack_garbage();
707 prepare_repo_settings(the_repository
);
708 if (the_repository
->settings
.gc_write_commit_graph
== 1)
709 write_commit_graph_reachable(the_repository
->objects
->odb
,
710 !quiet
&& !daemonized
? COMMIT_GRAPH_WRITE_PROGRESS
: 0,
713 if (auto_gc
&& too_many_loose_objects())
714 warning(_("There are too many unreachable loose objects; "
715 "run 'git prune' to remove them."));
718 unlink(git_path("gc.log"));
723 static const char *const builtin_maintenance_run_usage
[] = {
724 N_("git maintenance run [--auto] [--[no-]quiet] [--task=<task>] [--schedule]"),
728 enum schedule_priority
{
735 static enum schedule_priority
parse_schedule(const char *value
)
738 return SCHEDULE_NONE
;
739 if (!strcasecmp(value
, "hourly"))
740 return SCHEDULE_HOURLY
;
741 if (!strcasecmp(value
, "daily"))
742 return SCHEDULE_DAILY
;
743 if (!strcasecmp(value
, "weekly"))
744 return SCHEDULE_WEEKLY
;
745 return SCHEDULE_NONE
;
748 static int maintenance_opt_schedule(const struct option
*opt
, const char *arg
,
751 enum schedule_priority
*priority
= opt
->value
;
754 die(_("--no-schedule is not allowed"));
756 *priority
= parse_schedule(arg
);
759 die(_("unrecognized --schedule argument '%s'"), arg
);
764 struct maintenance_run_opts
{
767 enum schedule_priority schedule
;
770 /* Remember to update object flag allocation in object.h */
773 struct cg_auto_data
{
774 int num_not_in_graph
;
778 static int dfs_on_ref(const char *refname UNUSED
,
779 const struct object_id
*oid
,
783 struct cg_auto_data
*data
= (struct cg_auto_data
*)cb_data
;
785 struct object_id peeled
;
786 struct commit_list
*stack
= NULL
;
787 struct commit
*commit
;
789 if (!peel_iterated_oid(oid
, &peeled
))
791 if (oid_object_info(the_repository
, oid
, NULL
) != OBJ_COMMIT
)
794 commit
= lookup_commit(the_repository
, oid
);
797 if (parse_commit(commit
) ||
798 commit_graph_position(commit
) != COMMIT_NOT_FROM_GRAPH
)
801 data
->num_not_in_graph
++;
803 if (data
->num_not_in_graph
>= data
->limit
)
806 commit_list_append(commit
, &stack
);
808 while (!result
&& stack
) {
809 struct commit_list
*parent
;
811 commit
= pop_commit(&stack
);
813 for (parent
= commit
->parents
; parent
; parent
= parent
->next
) {
814 if (parse_commit(parent
->item
) ||
815 commit_graph_position(parent
->item
) != COMMIT_NOT_FROM_GRAPH
||
816 parent
->item
->object
.flags
& SEEN
)
819 parent
->item
->object
.flags
|= SEEN
;
820 data
->num_not_in_graph
++;
822 if (data
->num_not_in_graph
>= data
->limit
) {
827 commit_list_append(parent
->item
, &stack
);
831 free_commit_list(stack
);
835 static int should_write_commit_graph(void)
838 struct cg_auto_data data
;
840 data
.num_not_in_graph
= 0;
842 git_config_get_int("maintenance.commit-graph.auto",
850 result
= for_each_ref(dfs_on_ref
, &data
);
852 repo_clear_commit_marks(the_repository
, SEEN
);
857 static int run_write_commit_graph(struct maintenance_run_opts
*opts
)
859 struct child_process child
= CHILD_PROCESS_INIT
;
861 child
.git_cmd
= child
.close_object_store
= 1;
862 strvec_pushl(&child
.args
, "commit-graph", "write",
863 "--split", "--reachable", NULL
);
866 strvec_push(&child
.args
, "--no-progress");
868 return !!run_command(&child
);
871 static int maintenance_task_commit_graph(struct maintenance_run_opts
*opts
)
873 prepare_repo_settings(the_repository
);
874 if (!the_repository
->settings
.core_commit_graph
)
877 if (run_write_commit_graph(opts
)) {
878 error(_("failed to write commit-graph"));
885 static int fetch_remote(struct remote
*remote
, void *cbdata
)
887 struct maintenance_run_opts
*opts
= cbdata
;
888 struct child_process child
= CHILD_PROCESS_INIT
;
890 if (remote
->skip_default_update
)
894 strvec_pushl(&child
.args
, "fetch", remote
->name
,
895 "--prefetch", "--prune", "--no-tags",
896 "--no-write-fetch-head", "--recurse-submodules=no",
900 strvec_push(&child
.args
, "--quiet");
902 return !!run_command(&child
);
905 static int maintenance_task_prefetch(struct maintenance_run_opts
*opts
)
907 if (for_each_remote(fetch_remote
, opts
)) {
908 error(_("failed to prefetch remotes"));
915 static int maintenance_task_gc(struct maintenance_run_opts
*opts
)
917 struct child_process child
= CHILD_PROCESS_INIT
;
919 child
.git_cmd
= child
.close_object_store
= 1;
920 strvec_push(&child
.args
, "gc");
923 strvec_push(&child
.args
, "--auto");
925 strvec_push(&child
.args
, "--quiet");
927 strvec_push(&child
.args
, "--no-quiet");
929 return run_command(&child
);
932 static int prune_packed(struct maintenance_run_opts
*opts
)
934 struct child_process child
= CHILD_PROCESS_INIT
;
937 strvec_push(&child
.args
, "prune-packed");
940 strvec_push(&child
.args
, "--quiet");
942 return !!run_command(&child
);
945 struct write_loose_object_data
{
951 static int loose_object_auto_limit
= 100;
953 static int loose_object_count(const struct object_id
*oid
,
957 int *count
= (int*)data
;
958 if (++(*count
) >= loose_object_auto_limit
)
963 static int loose_object_auto_condition(void)
967 git_config_get_int("maintenance.loose-objects.auto",
968 &loose_object_auto_limit
);
970 if (!loose_object_auto_limit
)
972 if (loose_object_auto_limit
< 0)
975 return for_each_loose_file_in_objdir(the_repository
->objects
->odb
->path
,
980 static int bail_on_loose(const struct object_id
*oid
,
987 static int write_loose_object_to_stdin(const struct object_id
*oid
,
991 struct write_loose_object_data
*d
= (struct write_loose_object_data
*)data
;
993 fprintf(d
->in
, "%s\n", oid_to_hex(oid
));
995 return ++(d
->count
) > d
->batch_size
;
998 static int pack_loose(struct maintenance_run_opts
*opts
)
1000 struct repository
*r
= the_repository
;
1002 struct write_loose_object_data data
;
1003 struct child_process pack_proc
= CHILD_PROCESS_INIT
;
1006 * Do not start pack-objects process
1007 * if there are no loose objects.
1009 if (!for_each_loose_file_in_objdir(r
->objects
->odb
->path
,
1014 pack_proc
.git_cmd
= 1;
1016 strvec_push(&pack_proc
.args
, "pack-objects");
1018 strvec_push(&pack_proc
.args
, "--quiet");
1019 strvec_pushf(&pack_proc
.args
, "%s/pack/loose", r
->objects
->odb
->path
);
1023 if (start_command(&pack_proc
)) {
1024 error(_("failed to start 'git pack-objects' process"));
1028 data
.in
= xfdopen(pack_proc
.in
, "w");
1030 data
.batch_size
= 50000;
1032 for_each_loose_file_in_objdir(r
->objects
->odb
->path
,
1033 write_loose_object_to_stdin
,
1040 if (finish_command(&pack_proc
)) {
1041 error(_("failed to finish 'git pack-objects' process"));
1048 static int maintenance_task_loose_objects(struct maintenance_run_opts
*opts
)
1050 return prune_packed(opts
) || pack_loose(opts
);
1053 static int incremental_repack_auto_condition(void)
1055 struct packed_git
*p
;
1056 int incremental_repack_auto_limit
= 10;
1059 prepare_repo_settings(the_repository
);
1060 if (!the_repository
->settings
.core_multi_pack_index
)
1063 git_config_get_int("maintenance.incremental-repack.auto",
1064 &incremental_repack_auto_limit
);
1066 if (!incremental_repack_auto_limit
)
1068 if (incremental_repack_auto_limit
< 0)
1071 for (p
= get_packed_git(the_repository
);
1072 count
< incremental_repack_auto_limit
&& p
;
1074 if (!p
->multi_pack_index
)
1078 return count
>= incremental_repack_auto_limit
;
1081 static int multi_pack_index_write(struct maintenance_run_opts
*opts
)
1083 struct child_process child
= CHILD_PROCESS_INIT
;
1086 strvec_pushl(&child
.args
, "multi-pack-index", "write", NULL
);
1089 strvec_push(&child
.args
, "--no-progress");
1091 if (run_command(&child
))
1092 return error(_("failed to write multi-pack-index"));
1097 static int multi_pack_index_expire(struct maintenance_run_opts
*opts
)
1099 struct child_process child
= CHILD_PROCESS_INIT
;
1101 child
.git_cmd
= child
.close_object_store
= 1;
1102 strvec_pushl(&child
.args
, "multi-pack-index", "expire", NULL
);
1105 strvec_push(&child
.args
, "--no-progress");
1107 if (run_command(&child
))
1108 return error(_("'git multi-pack-index expire' failed"));
1113 #define TWO_GIGABYTES (INT32_MAX)
1115 static off_t
get_auto_pack_size(void)
1118 * The "auto" value is special: we optimize for
1119 * one large pack-file (i.e. from a clone) and
1120 * expect the rest to be small and they can be
1123 * The strategy we select here is to select a
1124 * size that is one more than the second largest
1125 * pack-file. This ensures that we will repack
1126 * at least two packs if there are three or more
1130 off_t second_largest_size
= 0;
1132 struct packed_git
*p
;
1133 struct repository
*r
= the_repository
;
1135 reprepare_packed_git(r
);
1136 for (p
= get_all_packs(r
); p
; p
= p
->next
) {
1137 if (p
->pack_size
> max_size
) {
1138 second_largest_size
= max_size
;
1139 max_size
= p
->pack_size
;
1140 } else if (p
->pack_size
> second_largest_size
)
1141 second_largest_size
= p
->pack_size
;
1144 result_size
= second_largest_size
+ 1;
1146 /* But limit ourselves to a batch size of 2g */
1147 if (result_size
> TWO_GIGABYTES
)
1148 result_size
= TWO_GIGABYTES
;
1153 static int multi_pack_index_repack(struct maintenance_run_opts
*opts
)
1155 struct child_process child
= CHILD_PROCESS_INIT
;
1157 child
.git_cmd
= child
.close_object_store
= 1;
1158 strvec_pushl(&child
.args
, "multi-pack-index", "repack", NULL
);
1161 strvec_push(&child
.args
, "--no-progress");
1163 strvec_pushf(&child
.args
, "--batch-size=%"PRIuMAX
,
1164 (uintmax_t)get_auto_pack_size());
1166 if (run_command(&child
))
1167 return error(_("'git multi-pack-index repack' failed"));
1172 static int maintenance_task_incremental_repack(struct maintenance_run_opts
*opts
)
1174 prepare_repo_settings(the_repository
);
1175 if (!the_repository
->settings
.core_multi_pack_index
) {
1176 warning(_("skipping incremental-repack task because core.multiPackIndex is disabled"));
1180 if (multi_pack_index_write(opts
))
1182 if (multi_pack_index_expire(opts
))
1184 if (multi_pack_index_repack(opts
))
1189 typedef int maintenance_task_fn(struct maintenance_run_opts
*opts
);
1192 * An auto condition function returns 1 if the task should run
1193 * and 0 if the task should NOT run. See needs_to_gc() for an
1196 typedef int maintenance_auto_fn(void);
1198 struct maintenance_task
{
1200 maintenance_task_fn
*fn
;
1201 maintenance_auto_fn
*auto_condition
;
1204 enum schedule_priority schedule
;
1206 /* -1 if not selected. */
1210 enum maintenance_task_label
{
1213 TASK_INCREMENTAL_REPACK
,
1218 /* Leave as final value */
1222 static struct maintenance_task tasks
[] = {
1225 maintenance_task_prefetch
,
1227 [TASK_LOOSE_OBJECTS
] = {
1229 maintenance_task_loose_objects
,
1230 loose_object_auto_condition
,
1232 [TASK_INCREMENTAL_REPACK
] = {
1233 "incremental-repack",
1234 maintenance_task_incremental_repack
,
1235 incremental_repack_auto_condition
,
1239 maintenance_task_gc
,
1243 [TASK_COMMIT_GRAPH
] = {
1245 maintenance_task_commit_graph
,
1246 should_write_commit_graph
,
1248 [TASK_PACK_REFS
] = {
1250 maintenance_task_pack_refs
,
1255 static int compare_tasks_by_selection(const void *a_
, const void *b_
)
1257 const struct maintenance_task
*a
= a_
;
1258 const struct maintenance_task
*b
= b_
;
1260 return b
->selected_order
- a
->selected_order
;
1263 static int maintenance_run_tasks(struct maintenance_run_opts
*opts
)
1265 int i
, found_selected
= 0;
1267 struct lock_file lk
;
1268 struct repository
*r
= the_repository
;
1269 char *lock_path
= xstrfmt("%s/maintenance", r
->objects
->odb
->path
);
1271 if (hold_lock_file_for_update(&lk
, lock_path
, LOCK_NO_DEREF
) < 0) {
1273 * Another maintenance command is running.
1275 * If --auto was provided, then it is likely due to a
1276 * recursive process stack. Do not report an error in
1279 if (!opts
->auto_flag
&& !opts
->quiet
)
1280 warning(_("lock file '%s' exists, skipping maintenance"),
1287 for (i
= 0; !found_selected
&& i
< TASK__COUNT
; i
++)
1288 found_selected
= tasks
[i
].selected_order
>= 0;
1291 QSORT(tasks
, TASK__COUNT
, compare_tasks_by_selection
);
1293 for (i
= 0; i
< TASK__COUNT
; i
++) {
1294 if (found_selected
&& tasks
[i
].selected_order
< 0)
1297 if (!found_selected
&& !tasks
[i
].enabled
)
1300 if (opts
->auto_flag
&&
1301 (!tasks
[i
].auto_condition
||
1302 !tasks
[i
].auto_condition()))
1305 if (opts
->schedule
&& tasks
[i
].schedule
< opts
->schedule
)
1308 trace2_region_enter("maintenance", tasks
[i
].name
, r
);
1309 if (tasks
[i
].fn(opts
)) {
1310 error(_("task '%s' failed"), tasks
[i
].name
);
1313 trace2_region_leave("maintenance", tasks
[i
].name
, r
);
1316 rollback_lock_file(&lk
);
1320 static void initialize_maintenance_strategy(void)
1324 if (git_config_get_string("maintenance.strategy", &config_str
))
1327 if (!strcasecmp(config_str
, "incremental")) {
1328 tasks
[TASK_GC
].schedule
= SCHEDULE_NONE
;
1329 tasks
[TASK_COMMIT_GRAPH
].enabled
= 1;
1330 tasks
[TASK_COMMIT_GRAPH
].schedule
= SCHEDULE_HOURLY
;
1331 tasks
[TASK_PREFETCH
].enabled
= 1;
1332 tasks
[TASK_PREFETCH
].schedule
= SCHEDULE_HOURLY
;
1333 tasks
[TASK_INCREMENTAL_REPACK
].enabled
= 1;
1334 tasks
[TASK_INCREMENTAL_REPACK
].schedule
= SCHEDULE_DAILY
;
1335 tasks
[TASK_LOOSE_OBJECTS
].enabled
= 1;
1336 tasks
[TASK_LOOSE_OBJECTS
].schedule
= SCHEDULE_DAILY
;
1337 tasks
[TASK_PACK_REFS
].enabled
= 1;
1338 tasks
[TASK_PACK_REFS
].schedule
= SCHEDULE_WEEKLY
;
1342 static void initialize_task_config(int schedule
)
1345 struct strbuf config_name
= STRBUF_INIT
;
1349 initialize_maintenance_strategy();
1351 for (i
= 0; i
< TASK__COUNT
; i
++) {
1355 strbuf_reset(&config_name
);
1356 strbuf_addf(&config_name
, "maintenance.%s.enabled",
1359 if (!git_config_get_bool(config_name
.buf
, &config_value
))
1360 tasks
[i
].enabled
= config_value
;
1362 strbuf_reset(&config_name
);
1363 strbuf_addf(&config_name
, "maintenance.%s.schedule",
1366 if (!git_config_get_string(config_name
.buf
, &config_str
)) {
1367 tasks
[i
].schedule
= parse_schedule(config_str
);
1372 strbuf_release(&config_name
);
1375 static int task_option_parse(const struct option
*opt
,
1376 const char *arg
, int unset
)
1378 int i
, num_selected
= 0;
1379 struct maintenance_task
*task
= NULL
;
1381 BUG_ON_OPT_NEG(unset
);
1383 for (i
= 0; i
< TASK__COUNT
; i
++) {
1384 if (tasks
[i
].selected_order
>= 0)
1386 if (!strcasecmp(tasks
[i
].name
, arg
)) {
1392 error(_("'%s' is not a valid task"), arg
);
1396 if (task
->selected_order
>= 0) {
1397 error(_("task '%s' cannot be selected multiple times"), arg
);
1401 task
->selected_order
= num_selected
+ 1;
1406 static int maintenance_run(int argc
, const char **argv
, const char *prefix
)
1409 struct maintenance_run_opts opts
;
1410 struct option builtin_maintenance_run_options
[] = {
1411 OPT_BOOL(0, "auto", &opts
.auto_flag
,
1412 N_("run tasks based on the state of the repository")),
1413 OPT_CALLBACK(0, "schedule", &opts
.schedule
, N_("frequency"),
1414 N_("run tasks based on frequency"),
1415 maintenance_opt_schedule
),
1416 OPT_BOOL(0, "quiet", &opts
.quiet
,
1417 N_("do not report progress or other information over stderr")),
1418 OPT_CALLBACK_F(0, "task", NULL
, N_("task"),
1419 N_("run a specific task"),
1420 PARSE_OPT_NONEG
, task_option_parse
),
1423 memset(&opts
, 0, sizeof(opts
));
1425 opts
.quiet
= !isatty(2);
1427 for (i
= 0; i
< TASK__COUNT
; i
++)
1428 tasks
[i
].selected_order
= -1;
1430 argc
= parse_options(argc
, argv
, prefix
,
1431 builtin_maintenance_run_options
,
1432 builtin_maintenance_run_usage
,
1433 PARSE_OPT_STOP_AT_NON_OPTION
);
1435 if (opts
.auto_flag
&& opts
.schedule
)
1436 die(_("use at most one of --auto and --schedule=<frequency>"));
1438 initialize_task_config(opts
.schedule
);
1441 usage_with_options(builtin_maintenance_run_usage
,
1442 builtin_maintenance_run_options
);
1443 return maintenance_run_tasks(&opts
);
1446 static char *get_maintpath(void)
1448 struct strbuf sb
= STRBUF_INIT
;
1449 const char *p
= the_repository
->worktree
?
1450 the_repository
->worktree
: the_repository
->gitdir
;
1452 strbuf_realpath(&sb
, p
, 1);
1453 return strbuf_detach(&sb
, NULL
);
1456 static char const * const builtin_maintenance_register_usage
[] = {
1457 "git maintenance register",
1461 static int maintenance_register(int argc
, const char **argv
, const char *prefix
)
1463 struct option options
[] = {
1468 struct child_process config_set
= CHILD_PROCESS_INIT
;
1469 struct child_process config_get
= CHILD_PROCESS_INIT
;
1470 char *maintpath
= get_maintpath();
1472 argc
= parse_options(argc
, argv
, prefix
, options
,
1473 builtin_maintenance_register_usage
, 0);
1475 usage_with_options(builtin_maintenance_register_usage
,
1478 /* Disable foreground maintenance */
1479 git_config_set("maintenance.auto", "false");
1481 /* Set maintenance strategy, if unset */
1482 if (!git_config_get_string("maintenance.strategy", &config_value
))
1485 git_config_set("maintenance.strategy", "incremental");
1487 config_get
.git_cmd
= 1;
1488 strvec_pushl(&config_get
.args
, "config", "--global", "--get",
1489 "--fixed-value", "maintenance.repo", maintpath
, NULL
);
1490 config_get
.out
= -1;
1492 if (start_command(&config_get
)) {
1493 rc
= error(_("failed to run 'git config'"));
1497 /* We already have this value in our config! */
1498 if (!finish_command(&config_get
)) {
1503 config_set
.git_cmd
= 1;
1504 strvec_pushl(&config_set
.args
, "config", "--add", "--global", "maintenance.repo",
1507 rc
= run_command(&config_set
);
1514 static char const * const builtin_maintenance_unregister_usage
[] = {
1515 "git maintenance unregister",
1519 static int maintenance_unregister(int argc
, const char **argv
, const char *prefix
)
1521 struct option options
[] = {
1525 struct child_process config_unset
= CHILD_PROCESS_INIT
;
1526 char *maintpath
= get_maintpath();
1528 argc
= parse_options(argc
, argv
, prefix
, options
,
1529 builtin_maintenance_unregister_usage
, 0);
1531 usage_with_options(builtin_maintenance_unregister_usage
,
1534 config_unset
.git_cmd
= 1;
1535 strvec_pushl(&config_unset
.args
, "config", "--global", "--unset",
1536 "--fixed-value", "maintenance.repo", maintpath
, NULL
);
1538 rc
= run_command(&config_unset
);
1543 static const char *get_frequency(enum schedule_priority schedule
)
1546 case SCHEDULE_HOURLY
:
1548 case SCHEDULE_DAILY
:
1550 case SCHEDULE_WEEKLY
:
1553 BUG("invalid schedule %d", schedule
);
1558 * get_schedule_cmd` reads the GIT_TEST_MAINT_SCHEDULER environment variable
1559 * to mock the schedulers that `git maintenance start` rely on.
1561 * For test purpose, GIT_TEST_MAINT_SCHEDULER can be set to a comma-separated
1562 * list of colon-separated key/value pairs where each pair contains a scheduler
1563 * and its corresponding mock.
1565 * * If $GIT_TEST_MAINT_SCHEDULER is not set, return false and leave the
1566 * arguments unmodified.
1568 * * If $GIT_TEST_MAINT_SCHEDULER is set, return true.
1569 * In this case, the *cmd value is read as input.
1571 * * if the input value *cmd is the key of one of the comma-separated list
1572 * item, then *is_available is set to true and *cmd is modified and becomes
1575 * * if the input value *cmd isn’t the key of any of the comma-separated list
1576 * item, then *is_available is set to false.
1579 * GIT_TEST_MAINT_SCHEDULER not set
1580 * +-------+-------------------------------------------------+
1581 * | Input | Output |
1582 * | *cmd | return code | *cmd | *is_available |
1583 * +-------+-------------+-------------------+---------------+
1584 * | "foo" | false | "foo" (unchanged) | (unchanged) |
1585 * +-------+-------------+-------------------+---------------+
1587 * GIT_TEST_MAINT_SCHEDULER set to “foo:./mock_foo.sh,bar:./mock_bar.sh”
1588 * +-------+-------------------------------------------------+
1589 * | Input | Output |
1590 * | *cmd | return code | *cmd | *is_available |
1591 * +-------+-------------+-------------------+---------------+
1592 * | "foo" | true | "./mock.foo.sh" | true |
1593 * | "qux" | true | "qux" (unchanged) | false |
1594 * +-------+-------------+-------------------+---------------+
1596 static int get_schedule_cmd(const char **cmd
, int *is_available
)
1598 char *testing
= xstrdup_or_null(getenv("GIT_TEST_MAINT_SCHEDULER"));
1599 struct string_list_item
*item
;
1600 struct string_list list
= STRING_LIST_INIT_NODUP
;
1608 string_list_split_in_place(&list
, testing
, ',', -1);
1609 for_each_string_list_item(item
, &list
) {
1610 struct string_list pair
= STRING_LIST_INIT_NODUP
;
1612 if (string_list_split_in_place(&pair
, item
->string
, ':', 2) != 2)
1615 if (!strcmp(*cmd
, pair
.items
[0].string
)) {
1616 *cmd
= pair
.items
[1].string
;
1619 string_list_clear(&list
, 0);
1625 string_list_clear(&list
, 0);
1630 static int is_launchctl_available(void)
1632 const char *cmd
= "launchctl";
1634 if (get_schedule_cmd(&cmd
, &is_available
))
1635 return is_available
;
1644 static char *launchctl_service_name(const char *frequency
)
1646 struct strbuf label
= STRBUF_INIT
;
1647 strbuf_addf(&label
, "org.git-scm.git.%s", frequency
);
1648 return strbuf_detach(&label
, NULL
);
1651 static char *launchctl_service_filename(const char *name
)
1654 struct strbuf filename
= STRBUF_INIT
;
1655 strbuf_addf(&filename
, "~/Library/LaunchAgents/%s.plist", name
);
1657 expanded
= interpolate_path(filename
.buf
, 1);
1659 die(_("failed to expand path '%s'"), filename
.buf
);
1661 strbuf_release(&filename
);
1665 static char *launchctl_get_uid(void)
1667 return xstrfmt("gui/%d", getuid());
1670 static int launchctl_boot_plist(int enable
, const char *filename
)
1672 const char *cmd
= "launchctl";
1674 struct child_process child
= CHILD_PROCESS_INIT
;
1675 char *uid
= launchctl_get_uid();
1677 get_schedule_cmd(&cmd
, NULL
);
1678 strvec_split(&child
.args
, cmd
);
1679 strvec_pushl(&child
.args
, enable
? "bootstrap" : "bootout", uid
,
1682 child
.no_stderr
= 1;
1683 child
.no_stdout
= 1;
1685 if (start_command(&child
))
1686 die(_("failed to start launchctl"));
1688 result
= finish_command(&child
);
1694 static int launchctl_remove_plist(enum schedule_priority schedule
)
1696 const char *frequency
= get_frequency(schedule
);
1697 char *name
= launchctl_service_name(frequency
);
1698 char *filename
= launchctl_service_filename(name
);
1699 int result
= launchctl_boot_plist(0, filename
);
1706 static int launchctl_remove_plists(void)
1708 return launchctl_remove_plist(SCHEDULE_HOURLY
) ||
1709 launchctl_remove_plist(SCHEDULE_DAILY
) ||
1710 launchctl_remove_plist(SCHEDULE_WEEKLY
);
1713 static int launchctl_list_contains_plist(const char *name
, const char *cmd
)
1715 struct child_process child
= CHILD_PROCESS_INIT
;
1717 strvec_split(&child
.args
, cmd
);
1718 strvec_pushl(&child
.args
, "list", name
, NULL
);
1720 child
.no_stderr
= 1;
1721 child
.no_stdout
= 1;
1723 if (start_command(&child
))
1724 die(_("failed to start launchctl"));
1726 /* Returns failure if 'name' doesn't exist. */
1727 return !finish_command(&child
);
1730 static int launchctl_schedule_plist(const char *exec_path
, enum schedule_priority schedule
)
1733 const char *preamble
, *repeat
;
1734 const char *frequency
= get_frequency(schedule
);
1735 char *name
= launchctl_service_name(frequency
);
1736 char *filename
= launchctl_service_filename(name
);
1737 struct lock_file lk
= LOCK_INIT
;
1738 static unsigned long lock_file_timeout_ms
= ULONG_MAX
;
1739 struct strbuf plist
= STRBUF_INIT
, plist2
= STRBUF_INIT
;
1741 const char *cmd
= "launchctl";
1743 get_schedule_cmd(&cmd
, NULL
);
1744 preamble
= "<?xml version=\"1.0\"?>\n"
1745 "<!DOCTYPE plist PUBLIC \"-//Apple//DTD PLIST 1.0//EN\" \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">\n"
1746 "<plist version=\"1.0\">"
1748 "<key>Label</key><string>%s</string>\n"
1749 "<key>ProgramArguments</key>\n"
1751 "<string>%s/git</string>\n"
1752 "<string>--exec-path=%s</string>\n"
1753 "<string>for-each-repo</string>\n"
1754 "<string>--config=maintenance.repo</string>\n"
1755 "<string>maintenance</string>\n"
1756 "<string>run</string>\n"
1757 "<string>--schedule=%s</string>\n"
1759 "<key>StartCalendarInterval</key>\n"
1761 strbuf_addf(&plist
, preamble
, name
, exec_path
, exec_path
, frequency
);
1764 case SCHEDULE_HOURLY
:
1766 "<key>Hour</key><integer>%d</integer>\n"
1767 "<key>Minute</key><integer>0</integer>\n"
1769 for (i
= 1; i
<= 23; i
++)
1770 strbuf_addf(&plist
, repeat
, i
);
1773 case SCHEDULE_DAILY
:
1775 "<key>Day</key><integer>%d</integer>\n"
1776 "<key>Hour</key><integer>0</integer>\n"
1777 "<key>Minute</key><integer>0</integer>\n"
1779 for (i
= 1; i
<= 6; i
++)
1780 strbuf_addf(&plist
, repeat
, i
);
1783 case SCHEDULE_WEEKLY
:
1784 strbuf_addstr(&plist
,
1786 "<key>Day</key><integer>0</integer>\n"
1787 "<key>Hour</key><integer>0</integer>\n"
1788 "<key>Minute</key><integer>0</integer>\n"
1796 strbuf_addstr(&plist
, "</array>\n</dict>\n</plist>\n");
1798 if (safe_create_leading_directories(filename
))
1799 die(_("failed to create directories for '%s'"), filename
);
1801 if ((long)lock_file_timeout_ms
< 0 &&
1802 git_config_get_ulong("gc.launchctlplistlocktimeoutms",
1803 &lock_file_timeout_ms
))
1804 lock_file_timeout_ms
= 150;
1806 fd
= hold_lock_file_for_update_timeout(&lk
, filename
, LOCK_DIE_ON_ERROR
,
1807 lock_file_timeout_ms
);
1810 * Does this file already exist? With the intended contents? Is it
1811 * registered already? Then it does not need to be re-registered.
1813 if (!stat(filename
, &st
) && st
.st_size
== plist
.len
&&
1814 strbuf_read_file(&plist2
, filename
, plist
.len
) == plist
.len
&&
1815 !strbuf_cmp(&plist
, &plist2
) &&
1816 launchctl_list_contains_plist(name
, cmd
))
1817 rollback_lock_file(&lk
);
1819 if (write_in_full(fd
, plist
.buf
, plist
.len
) < 0 ||
1820 commit_lock_file(&lk
))
1821 die_errno(_("could not write '%s'"), filename
);
1823 /* bootout might fail if not already running, so ignore */
1824 launchctl_boot_plist(0, filename
);
1825 if (launchctl_boot_plist(1, filename
))
1826 die(_("failed to bootstrap service %s"), filename
);
1831 strbuf_release(&plist
);
1832 strbuf_release(&plist2
);
1836 static int launchctl_add_plists(void)
1838 const char *exec_path
= git_exec_path();
1840 return launchctl_schedule_plist(exec_path
, SCHEDULE_HOURLY
) ||
1841 launchctl_schedule_plist(exec_path
, SCHEDULE_DAILY
) ||
1842 launchctl_schedule_plist(exec_path
, SCHEDULE_WEEKLY
);
1845 static int launchctl_update_schedule(int run_maintenance
, int fd
)
1847 if (run_maintenance
)
1848 return launchctl_add_plists();
1850 return launchctl_remove_plists();
1853 static int is_schtasks_available(void)
1855 const char *cmd
= "schtasks";
1857 if (get_schedule_cmd(&cmd
, &is_available
))
1858 return is_available
;
1860 #ifdef GIT_WINDOWS_NATIVE
1867 static char *schtasks_task_name(const char *frequency
)
1869 struct strbuf label
= STRBUF_INIT
;
1870 strbuf_addf(&label
, "Git Maintenance (%s)", frequency
);
1871 return strbuf_detach(&label
, NULL
);
1874 static int schtasks_remove_task(enum schedule_priority schedule
)
1876 const char *cmd
= "schtasks";
1878 struct strvec args
= STRVEC_INIT
;
1879 const char *frequency
= get_frequency(schedule
);
1880 char *name
= schtasks_task_name(frequency
);
1882 get_schedule_cmd(&cmd
, NULL
);
1883 strvec_split(&args
, cmd
);
1884 strvec_pushl(&args
, "/delete", "/tn", name
, "/f", NULL
);
1886 result
= run_command_v_opt(args
.v
, 0);
1888 strvec_clear(&args
);
1893 static int schtasks_remove_tasks(void)
1895 return schtasks_remove_task(SCHEDULE_HOURLY
) ||
1896 schtasks_remove_task(SCHEDULE_DAILY
) ||
1897 schtasks_remove_task(SCHEDULE_WEEKLY
);
1900 static int schtasks_schedule_task(const char *exec_path
, enum schedule_priority schedule
)
1902 const char *cmd
= "schtasks";
1904 struct child_process child
= CHILD_PROCESS_INIT
;
1906 struct tempfile
*tfile
;
1907 const char *frequency
= get_frequency(schedule
);
1908 char *name
= schtasks_task_name(frequency
);
1909 struct strbuf tfilename
= STRBUF_INIT
;
1911 get_schedule_cmd(&cmd
, NULL
);
1913 strbuf_addf(&tfilename
, "%s/schedule_%s_XXXXXX",
1914 get_git_common_dir(), frequency
);
1915 tfile
= xmks_tempfile(tfilename
.buf
);
1916 strbuf_release(&tfilename
);
1918 if (!fdopen_tempfile(tfile
, "w"))
1919 die(_("failed to create temp xml file"));
1921 xml
= "<?xml version=\"1.0\" ?>\n"
1922 "<Task version=\"1.4\" xmlns=\"http://schemas.microsoft.com/windows/2004/02/mit/task\">\n"
1924 "<CalendarTrigger>\n";
1925 fputs(xml
, tfile
->fp
);
1928 case SCHEDULE_HOURLY
:
1930 "<StartBoundary>2020-01-01T01:00:00</StartBoundary>\n"
1931 "<Enabled>true</Enabled>\n"
1933 "<DaysInterval>1</DaysInterval>\n"
1934 "</ScheduleByDay>\n"
1936 "<Interval>PT1H</Interval>\n"
1937 "<Duration>PT23H</Duration>\n"
1938 "<StopAtDurationEnd>false</StopAtDurationEnd>\n"
1942 case SCHEDULE_DAILY
:
1944 "<StartBoundary>2020-01-01T00:00:00</StartBoundary>\n"
1945 "<Enabled>true</Enabled>\n"
1946 "<ScheduleByWeek>\n"
1955 "<WeeksInterval>1</WeeksInterval>\n"
1956 "</ScheduleByWeek>\n");
1959 case SCHEDULE_WEEKLY
:
1961 "<StartBoundary>2020-01-01T00:00:00</StartBoundary>\n"
1962 "<Enabled>true</Enabled>\n"
1963 "<ScheduleByWeek>\n"
1967 "<WeeksInterval>1</WeeksInterval>\n"
1968 "</ScheduleByWeek>\n");
1975 xml
= "</CalendarTrigger>\n"
1978 "<Principal id=\"Author\">\n"
1979 "<LogonType>InteractiveToken</LogonType>\n"
1980 "<RunLevel>LeastPrivilege</RunLevel>\n"
1984 "<MultipleInstancesPolicy>IgnoreNew</MultipleInstancesPolicy>\n"
1985 "<Enabled>true</Enabled>\n"
1986 "<Hidden>true</Hidden>\n"
1987 "<UseUnifiedSchedulingEngine>true</UseUnifiedSchedulingEngine>\n"
1988 "<WakeToRun>false</WakeToRun>\n"
1989 "<ExecutionTimeLimit>PT72H</ExecutionTimeLimit>\n"
1990 "<Priority>7</Priority>\n"
1992 "<Actions Context=\"Author\">\n"
1994 "<Command>\"%s\\git.exe\"</Command>\n"
1995 "<Arguments>--exec-path=\"%s\" for-each-repo --config=maintenance.repo maintenance run --schedule=%s</Arguments>\n"
1999 fprintf(tfile
->fp
, xml
, exec_path
, exec_path
, frequency
);
2000 strvec_split(&child
.args
, cmd
);
2001 strvec_pushl(&child
.args
, "/create", "/tn", name
, "/f", "/xml",
2002 get_tempfile_path(tfile
), NULL
);
2003 close_tempfile_gently(tfile
);
2005 child
.no_stdout
= 1;
2006 child
.no_stderr
= 1;
2008 if (start_command(&child
))
2009 die(_("failed to start schtasks"));
2010 result
= finish_command(&child
);
2012 delete_tempfile(&tfile
);
2017 static int schtasks_schedule_tasks(void)
2019 const char *exec_path
= git_exec_path();
2021 return schtasks_schedule_task(exec_path
, SCHEDULE_HOURLY
) ||
2022 schtasks_schedule_task(exec_path
, SCHEDULE_DAILY
) ||
2023 schtasks_schedule_task(exec_path
, SCHEDULE_WEEKLY
);
2026 static int schtasks_update_schedule(int run_maintenance
, int fd
)
2028 if (run_maintenance
)
2029 return schtasks_schedule_tasks();
2031 return schtasks_remove_tasks();
2035 static int check_crontab_process(const char *cmd
)
2037 struct child_process child
= CHILD_PROCESS_INIT
;
2039 strvec_split(&child
.args
, cmd
);
2040 strvec_push(&child
.args
, "-l");
2042 child
.no_stdout
= 1;
2043 child
.no_stderr
= 1;
2044 child
.silent_exec_failure
= 1;
2046 if (start_command(&child
))
2048 /* Ignore exit code, as an empty crontab will return error. */
2049 finish_command(&child
);
2053 static int is_crontab_available(void)
2055 const char *cmd
= "crontab";
2058 if (get_schedule_cmd(&cmd
, &is_available
))
2059 return is_available
;
2063 * macOS has cron, but it requires special permissions and will
2064 * create a UI alert when attempting to run this command.
2068 return check_crontab_process(cmd
);
2072 #define BEGIN_LINE "# BEGIN GIT MAINTENANCE SCHEDULE"
2073 #define END_LINE "# END GIT MAINTENANCE SCHEDULE"
2075 static int crontab_update_schedule(int run_maintenance
, int fd
)
2077 const char *cmd
= "crontab";
2079 int in_old_region
= 0;
2080 struct child_process crontab_list
= CHILD_PROCESS_INIT
;
2081 struct child_process crontab_edit
= CHILD_PROCESS_INIT
;
2082 FILE *cron_list
, *cron_in
;
2083 struct strbuf line
= STRBUF_INIT
;
2084 struct tempfile
*tmpedit
= NULL
;
2086 get_schedule_cmd(&cmd
, NULL
);
2087 strvec_split(&crontab_list
.args
, cmd
);
2088 strvec_push(&crontab_list
.args
, "-l");
2089 crontab_list
.in
= -1;
2090 crontab_list
.out
= dup(fd
);
2091 crontab_list
.git_cmd
= 0;
2093 if (start_command(&crontab_list
))
2094 return error(_("failed to run 'crontab -l'; your system might not support 'cron'"));
2096 /* Ignore exit code, as an empty crontab will return error. */
2097 finish_command(&crontab_list
);
2099 tmpedit
= mks_tempfile_t(".git_cron_edit_tmpXXXXXX");
2101 result
= error(_("failed to create crontab temporary file"));
2104 cron_in
= fdopen_tempfile(tmpedit
, "w");
2106 result
= error(_("failed to open temporary file"));
2111 * Read from the .lock file, filtering out the old
2112 * schedule while appending the new schedule.
2114 cron_list
= fdopen(fd
, "r");
2117 while (!strbuf_getline_lf(&line
, cron_list
)) {
2118 if (!in_old_region
&& !strcmp(line
.buf
, BEGIN_LINE
))
2120 else if (in_old_region
&& !strcmp(line
.buf
, END_LINE
))
2122 else if (!in_old_region
)
2123 fprintf(cron_in
, "%s\n", line
.buf
);
2125 strbuf_release(&line
);
2127 if (run_maintenance
) {
2128 struct strbuf line_format
= STRBUF_INIT
;
2129 const char *exec_path
= git_exec_path();
2131 fprintf(cron_in
, "%s\n", BEGIN_LINE
);
2133 "# The following schedule was created by Git\n");
2134 fprintf(cron_in
, "# Any edits made in this region might be\n");
2136 "# replaced in the future by a Git command.\n\n");
2138 strbuf_addf(&line_format
,
2139 "%%s %%s * * %%s \"%s/git\" --exec-path=\"%s\" for-each-repo --config=maintenance.repo maintenance run --schedule=%%s\n",
2140 exec_path
, exec_path
);
2141 fprintf(cron_in
, line_format
.buf
, "0", "1-23", "*", "hourly");
2142 fprintf(cron_in
, line_format
.buf
, "0", "0", "1-6", "daily");
2143 fprintf(cron_in
, line_format
.buf
, "0", "0", "0", "weekly");
2144 strbuf_release(&line_format
);
2146 fprintf(cron_in
, "\n%s\n", END_LINE
);
2151 strvec_split(&crontab_edit
.args
, cmd
);
2152 strvec_push(&crontab_edit
.args
, get_tempfile_path(tmpedit
));
2153 crontab_edit
.git_cmd
= 0;
2155 if (start_command(&crontab_edit
)) {
2156 result
= error(_("failed to run 'crontab'; your system might not support 'cron'"));
2160 if (finish_command(&crontab_edit
))
2161 result
= error(_("'crontab' died"));
2165 delete_tempfile(&tmpedit
);
2169 static int real_is_systemd_timer_available(void)
2171 struct child_process child
= CHILD_PROCESS_INIT
;
2173 strvec_pushl(&child
.args
, "systemctl", "--user", "list-timers", NULL
);
2175 child
.no_stdout
= 1;
2176 child
.no_stderr
= 1;
2177 child
.silent_exec_failure
= 1;
2179 if (start_command(&child
))
2181 if (finish_command(&child
))
2186 static int is_systemd_timer_available(void)
2188 const char *cmd
= "systemctl";
2191 if (get_schedule_cmd(&cmd
, &is_available
))
2192 return is_available
;
2194 return real_is_systemd_timer_available();
2197 static char *xdg_config_home_systemd(const char *filename
)
2199 return xdg_config_home_for("systemd/user", filename
);
2202 static int systemd_timer_enable_unit(int enable
,
2203 enum schedule_priority schedule
)
2205 const char *cmd
= "systemctl";
2206 struct child_process child
= CHILD_PROCESS_INIT
;
2207 const char *frequency
= get_frequency(schedule
);
2210 * Disabling the systemd unit while it is already disabled makes
2211 * systemctl print an error.
2212 * Let's ignore it since it means we already are in the expected state:
2213 * the unit is disabled.
2215 * On the other hand, enabling a systemd unit which is already enabled
2216 * produces no error.
2219 child
.no_stderr
= 1;
2221 get_schedule_cmd(&cmd
, NULL
);
2222 strvec_split(&child
.args
, cmd
);
2223 strvec_pushl(&child
.args
, "--user", enable
? "enable" : "disable",
2225 strvec_pushf(&child
.args
, "git-maintenance@%s.timer", frequency
);
2227 if (start_command(&child
))
2228 return error(_("failed to start systemctl"));
2229 if (finish_command(&child
))
2231 * Disabling an already disabled systemd unit makes
2233 * Let's ignore this failure.
2235 * Enabling an enabled systemd unit doesn't fail.
2238 return error(_("failed to run systemctl"));
2242 static int systemd_timer_delete_unit_templates(void)
2245 char *filename
= xdg_config_home_systemd("git-maintenance@.timer");
2246 if (unlink(filename
) && !is_missing_file_error(errno
))
2247 ret
= error_errno(_("failed to delete '%s'"), filename
);
2248 FREE_AND_NULL(filename
);
2250 filename
= xdg_config_home_systemd("git-maintenance@.service");
2251 if (unlink(filename
) && !is_missing_file_error(errno
))
2252 ret
= error_errno(_("failed to delete '%s'"), filename
);
2258 static int systemd_timer_delete_units(void)
2260 return systemd_timer_enable_unit(0, SCHEDULE_HOURLY
) ||
2261 systemd_timer_enable_unit(0, SCHEDULE_DAILY
) ||
2262 systemd_timer_enable_unit(0, SCHEDULE_WEEKLY
) ||
2263 systemd_timer_delete_unit_templates();
2266 static int systemd_timer_write_unit_templates(const char *exec_path
)
2272 filename
= xdg_config_home_systemd("git-maintenance@.timer");
2273 if (safe_create_leading_directories(filename
)) {
2274 error(_("failed to create directories for '%s'"), filename
);
2277 file
= fopen_or_warn(filename
, "w");
2281 unit
= "# This file was created and is maintained by Git.\n"
2282 "# Any edits made in this file might be replaced in the future\n"
2283 "# by a Git command.\n"
2286 "Description=Optimize Git repositories data\n"
2293 "WantedBy=timers.target\n";
2294 if (fputs(unit
, file
) == EOF
) {
2295 error(_("failed to write to '%s'"), filename
);
2299 if (fclose(file
) == EOF
) {
2300 error_errno(_("failed to flush '%s'"), filename
);
2305 filename
= xdg_config_home_systemd("git-maintenance@.service");
2306 file
= fopen_or_warn(filename
, "w");
2310 unit
= "# This file was created and is maintained by Git.\n"
2311 "# Any edits made in this file might be replaced in the future\n"
2312 "# by a Git command.\n"
2315 "Description=Optimize Git repositories data\n"
2319 "ExecStart=\"%s/git\" --exec-path=\"%s\" for-each-repo --config=maintenance.repo maintenance run --schedule=%%i\n"
2320 "LockPersonality=yes\n"
2321 "MemoryDenyWriteExecute=yes\n"
2322 "NoNewPrivileges=yes\n"
2323 "RestrictAddressFamilies=AF_UNIX AF_INET AF_INET6\n"
2324 "RestrictNamespaces=yes\n"
2325 "RestrictRealtime=yes\n"
2326 "RestrictSUIDSGID=yes\n"
2327 "SystemCallArchitectures=native\n"
2328 "SystemCallFilter=@system-service\n";
2329 if (fprintf(file
, unit
, exec_path
, exec_path
) < 0) {
2330 error(_("failed to write to '%s'"), filename
);
2334 if (fclose(file
) == EOF
) {
2335 error_errno(_("failed to flush '%s'"), filename
);
2343 systemd_timer_delete_unit_templates();
2347 static int systemd_timer_setup_units(void)
2349 const char *exec_path
= git_exec_path();
2351 int ret
= systemd_timer_write_unit_templates(exec_path
) ||
2352 systemd_timer_enable_unit(1, SCHEDULE_HOURLY
) ||
2353 systemd_timer_enable_unit(1, SCHEDULE_DAILY
) ||
2354 systemd_timer_enable_unit(1, SCHEDULE_WEEKLY
);
2356 systemd_timer_delete_units();
2360 static int systemd_timer_update_schedule(int run_maintenance
, int fd
)
2362 if (run_maintenance
)
2363 return systemd_timer_setup_units();
2365 return systemd_timer_delete_units();
2369 SCHEDULER_INVALID
= -1,
2373 SCHEDULER_LAUNCHCTL
,
2377 static const struct {
2379 int (*is_available
)(void);
2380 int (*update_schedule
)(int run_maintenance
, int fd
);
2381 } scheduler_fn
[] = {
2382 [SCHEDULER_CRON
] = {
2384 .is_available
= is_crontab_available
,
2385 .update_schedule
= crontab_update_schedule
,
2387 [SCHEDULER_SYSTEMD
] = {
2388 .name
= "systemctl",
2389 .is_available
= is_systemd_timer_available
,
2390 .update_schedule
= systemd_timer_update_schedule
,
2392 [SCHEDULER_LAUNCHCTL
] = {
2393 .name
= "launchctl",
2394 .is_available
= is_launchctl_available
,
2395 .update_schedule
= launchctl_update_schedule
,
2397 [SCHEDULER_SCHTASKS
] = {
2399 .is_available
= is_schtasks_available
,
2400 .update_schedule
= schtasks_update_schedule
,
2404 static enum scheduler
parse_scheduler(const char *value
)
2407 return SCHEDULER_INVALID
;
2408 else if (!strcasecmp(value
, "auto"))
2409 return SCHEDULER_AUTO
;
2410 else if (!strcasecmp(value
, "cron") || !strcasecmp(value
, "crontab"))
2411 return SCHEDULER_CRON
;
2412 else if (!strcasecmp(value
, "systemd") ||
2413 !strcasecmp(value
, "systemd-timer"))
2414 return SCHEDULER_SYSTEMD
;
2415 else if (!strcasecmp(value
, "launchctl"))
2416 return SCHEDULER_LAUNCHCTL
;
2417 else if (!strcasecmp(value
, "schtasks"))
2418 return SCHEDULER_SCHTASKS
;
2420 return SCHEDULER_INVALID
;
2423 static int maintenance_opt_scheduler(const struct option
*opt
, const char *arg
,
2426 enum scheduler
*scheduler
= opt
->value
;
2428 BUG_ON_OPT_NEG(unset
);
2430 *scheduler
= parse_scheduler(arg
);
2431 if (*scheduler
== SCHEDULER_INVALID
)
2432 return error(_("unrecognized --scheduler argument '%s'"), arg
);
2436 struct maintenance_start_opts
{
2437 enum scheduler scheduler
;
2440 static enum scheduler
resolve_scheduler(enum scheduler scheduler
)
2442 if (scheduler
!= SCHEDULER_AUTO
)
2445 #if defined(__APPLE__)
2446 return SCHEDULER_LAUNCHCTL
;
2448 #elif defined(GIT_WINDOWS_NATIVE)
2449 return SCHEDULER_SCHTASKS
;
2451 #elif defined(__linux__)
2452 if (is_systemd_timer_available())
2453 return SCHEDULER_SYSTEMD
;
2454 else if (is_crontab_available())
2455 return SCHEDULER_CRON
;
2457 die(_("neither systemd timers nor crontab are available"));
2460 return SCHEDULER_CRON
;
2464 static void validate_scheduler(enum scheduler scheduler
)
2466 if (scheduler
== SCHEDULER_INVALID
)
2467 BUG("invalid scheduler");
2468 if (scheduler
== SCHEDULER_AUTO
)
2469 BUG("resolve_scheduler should have been called before");
2471 if (!scheduler_fn
[scheduler
].is_available())
2472 die(_("%s scheduler is not available"),
2473 scheduler_fn
[scheduler
].name
);
2476 static int update_background_schedule(const struct maintenance_start_opts
*opts
,
2481 struct lock_file lk
;
2482 char *lock_path
= xstrfmt("%s/schedule", the_repository
->objects
->odb
->path
);
2484 if (hold_lock_file_for_update(&lk
, lock_path
, LOCK_NO_DEREF
) < 0) {
2486 return error(_("another process is scheduling background maintenance"));
2489 for (i
= 1; i
< ARRAY_SIZE(scheduler_fn
); i
++) {
2490 if (enable
&& opts
->scheduler
== i
)
2492 if (!scheduler_fn
[i
].is_available())
2494 scheduler_fn
[i
].update_schedule(0, get_lock_file_fd(&lk
));
2498 result
= scheduler_fn
[opts
->scheduler
].update_schedule(
2499 1, get_lock_file_fd(&lk
));
2501 rollback_lock_file(&lk
);
2507 static const char *const builtin_maintenance_start_usage
[] = {
2508 N_("git maintenance start [--scheduler=<scheduler>]"),
2512 static int maintenance_start(int argc
, const char **argv
, const char *prefix
)
2514 struct maintenance_start_opts opts
= { 0 };
2515 struct option options
[] = {
2517 0, "scheduler", &opts
.scheduler
, N_("scheduler"),
2518 N_("scheduler to trigger git maintenance run"),
2519 PARSE_OPT_NONEG
, maintenance_opt_scheduler
),
2522 const char *register_args
[] = { "register", NULL
};
2524 argc
= parse_options(argc
, argv
, prefix
, options
,
2525 builtin_maintenance_start_usage
, 0);
2527 usage_with_options(builtin_maintenance_start_usage
, options
);
2529 opts
.scheduler
= resolve_scheduler(opts
.scheduler
);
2530 validate_scheduler(opts
.scheduler
);
2532 if (maintenance_register(ARRAY_SIZE(register_args
)-1, register_args
, NULL
))
2533 warning(_("failed to add repo to global config"));
2534 return update_background_schedule(&opts
, 1);
2537 static const char *const builtin_maintenance_stop_usage
[] = {
2538 "git maintenance stop",
2542 static int maintenance_stop(int argc
, const char **argv
, const char *prefix
)
2544 struct option options
[] = {
2547 argc
= parse_options(argc
, argv
, prefix
, options
,
2548 builtin_maintenance_stop_usage
, 0);
2550 usage_with_options(builtin_maintenance_stop_usage
, options
);
2551 return update_background_schedule(NULL
, 0);
2554 static const char * const builtin_maintenance_usage
[] = {
2555 N_("git maintenance <subcommand> [<options>]"),
2559 int cmd_maintenance(int argc
, const char **argv
, const char *prefix
)
2561 parse_opt_subcommand_fn
*fn
= NULL
;
2562 struct option builtin_maintenance_options
[] = {
2563 OPT_SUBCOMMAND("run", &fn
, maintenance_run
),
2564 OPT_SUBCOMMAND("start", &fn
, maintenance_start
),
2565 OPT_SUBCOMMAND("stop", &fn
, maintenance_stop
),
2566 OPT_SUBCOMMAND("register", &fn
, maintenance_register
),
2567 OPT_SUBCOMMAND("unregister", &fn
, maintenance_unregister
),
2571 argc
= parse_options(argc
, argv
, prefix
, builtin_maintenance_options
,
2572 builtin_maintenance_usage
, 0);
2573 return fn(argc
, argv
, prefix
);