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 struct strvec pack_refs_cmd
= STRVEC_INIT
;
173 strvec_pushl(&pack_refs_cmd
, "pack-refs", "--all", "--prune", NULL
);
175 ret
= run_command_v_opt(pack_refs_cmd
.v
, RUN_GIT_CMD
);
177 strvec_clear(&pack_refs_cmd
);
182 static int too_many_loose_objects(void)
185 * Quickly check if a "gc" is needed, by estimating how
186 * many loose objects there are. Because SHA-1 is evenly
187 * distributed, we can check only one and get a reasonable
195 const unsigned hexsz_loose
= the_hash_algo
->hexsz
- 2;
197 dir
= opendir(git_path("objects/17"));
201 auto_threshold
= DIV_ROUND_UP(gc_auto_threshold
, 256);
202 while ((ent
= readdir(dir
)) != NULL
) {
203 if (strspn(ent
->d_name
, "0123456789abcdef") != hexsz_loose
||
204 ent
->d_name
[hexsz_loose
] != '\0')
206 if (++num_loose
> auto_threshold
) {
215 static struct packed_git
*find_base_packs(struct string_list
*packs
,
218 struct packed_git
*p
, *base
= NULL
;
220 for (p
= get_all_packs(the_repository
); p
; p
= p
->next
) {
224 if (p
->pack_size
>= limit
)
225 string_list_append(packs
, p
->pack_name
);
226 } else if (!base
|| base
->pack_size
< p
->pack_size
) {
232 string_list_append(packs
, base
->pack_name
);
237 static int too_many_packs(void)
239 struct packed_git
*p
;
242 if (gc_auto_pack_limit
<= 0)
245 for (cnt
= 0, p
= get_all_packs(the_repository
); p
; p
= p
->next
) {
251 * Perhaps check the size of the pack and count only
252 * very small ones here?
256 return gc_auto_pack_limit
< cnt
;
259 static uint64_t total_ram(void)
261 #if defined(HAVE_SYSINFO)
266 #elif defined(HAVE_BSD_SYSCTL) && (defined(HW_MEMSIZE) || defined(HW_PHYSMEM))
267 int64_t physical_memory
;
272 # if defined(HW_MEMSIZE)
277 length
= sizeof(int64_t);
278 if (!sysctl(mib
, 2, &physical_memory
, &length
, NULL
, 0))
279 return physical_memory
;
280 #elif defined(GIT_WINDOWS_NATIVE)
281 MEMORYSTATUSEX memInfo
;
283 memInfo
.dwLength
= sizeof(MEMORYSTATUSEX
);
284 if (GlobalMemoryStatusEx(&memInfo
))
285 return memInfo
.ullTotalPhys
;
290 static uint64_t estimate_repack_memory(struct packed_git
*pack
)
292 unsigned long nr_objects
= approximate_object_count();
293 size_t os_cache
, heap
;
295 if (!pack
|| !nr_objects
)
299 * First we have to scan through at least one pack.
300 * Assume enough room in OS file cache to keep the entire pack
301 * or we may accidentally evict data of other processes from
304 os_cache
= pack
->pack_size
+ pack
->index_size
;
305 /* then pack-objects needs lots more for book keeping */
306 heap
= sizeof(struct object_entry
) * nr_objects
;
308 * internal rev-list --all --objects takes up some memory too,
309 * let's say half of it is for blobs
311 heap
+= sizeof(struct blob
) * nr_objects
/ 2;
313 * and the other half is for trees (commits and tags are
314 * usually insignificant)
316 heap
+= sizeof(struct tree
) * nr_objects
/ 2;
317 /* and then obj_hash[], underestimated in fact */
318 heap
+= sizeof(struct object
*) * nr_objects
;
319 /* revindex is used also */
320 heap
+= (sizeof(off_t
) + sizeof(uint32_t)) * nr_objects
;
322 * read_sha1_file() (either at delta calculation phase, or
323 * writing phase) also fills up the delta base cache
325 heap
+= delta_base_cache_limit
;
326 /* and of course pack-objects has its own delta cache */
327 heap
+= max_delta_cache_size
;
329 return os_cache
+ heap
;
332 static int keep_one_pack(struct string_list_item
*item
, void *data
)
334 strvec_pushf(&repack
, "--keep-pack=%s", basename(item
->string
));
338 static void add_repack_all_option(struct string_list
*keep_pack
)
340 if (prune_expire
&& !strcmp(prune_expire
, "now"))
341 strvec_push(&repack
, "-a");
342 else if (cruft_packs
) {
343 strvec_push(&repack
, "--cruft");
345 strvec_pushf(&repack
, "--cruft-expiration=%s", prune_expire
);
347 strvec_push(&repack
, "-A");
349 strvec_pushf(&repack
, "--unpack-unreachable=%s", prune_expire
);
353 for_each_string_list(keep_pack
, keep_one_pack
, NULL
);
356 static void add_repack_incremental_option(void)
358 strvec_push(&repack
, "--no-write-bitmap-index");
361 static int need_to_gc(void)
364 * Setting gc.auto to 0 or negative can disable the
367 if (gc_auto_threshold
<= 0)
371 * If there are too many loose objects, but not too many
372 * packs, we run "repack -d -l". If there are too many packs,
373 * we run "repack -A -d -l". Otherwise we tell the caller
376 if (too_many_packs()) {
377 struct string_list keep_pack
= STRING_LIST_INIT_NODUP
;
379 if (big_pack_threshold
) {
380 find_base_packs(&keep_pack
, big_pack_threshold
);
381 if (keep_pack
.nr
>= gc_auto_pack_limit
) {
382 big_pack_threshold
= 0;
383 string_list_clear(&keep_pack
, 0);
384 find_base_packs(&keep_pack
, 0);
387 struct packed_git
*p
= find_base_packs(&keep_pack
, 0);
388 uint64_t mem_have
, mem_want
;
390 mem_have
= total_ram();
391 mem_want
= estimate_repack_memory(p
);
394 * Only allow 1/2 of memory for pack-objects, leave
395 * the rest for the OS and other processes in the
398 if (!mem_have
|| mem_want
< mem_have
/ 2)
399 string_list_clear(&keep_pack
, 0);
402 add_repack_all_option(&keep_pack
);
403 string_list_clear(&keep_pack
, 0);
404 } else if (too_many_loose_objects())
405 add_repack_incremental_option();
409 if (run_hooks("pre-auto-gc"))
414 /* return NULL on success, else hostname running the gc */
415 static const char *lock_repo_for_gc(int force
, pid_t
* ret_pid
)
417 struct lock_file lock
= LOCK_INIT
;
418 char my_host
[HOST_NAME_MAX
+ 1];
419 struct strbuf sb
= STRBUF_INIT
;
426 if (is_tempfile_active(pidfile
))
430 if (xgethostname(my_host
, sizeof(my_host
)))
431 xsnprintf(my_host
, sizeof(my_host
), "unknown");
433 pidfile_path
= git_pathdup("gc.pid");
434 fd
= hold_lock_file_for_update(&lock
, pidfile_path
,
437 static char locking_host
[HOST_NAME_MAX
+ 1];
438 static char *scan_fmt
;
442 scan_fmt
= xstrfmt("%s %%%ds", "%"SCNuMAX
, HOST_NAME_MAX
);
443 fp
= fopen(pidfile_path
, "r");
444 memset(locking_host
, 0, sizeof(locking_host
));
447 !fstat(fileno(fp
), &st
) &&
449 * 12 hour limit is very generous as gc should
450 * never take that long. On the other hand we
451 * don't really need a strict limit here,
452 * running gc --auto one day late is not a big
453 * problem. --force can be used in manual gc
454 * after the user verifies that no gc is
457 time(NULL
) - st
.st_mtime
<= 12 * 3600 &&
458 fscanf(fp
, scan_fmt
, &pid
, locking_host
) == 2 &&
459 /* be gentle to concurrent "gc" on remote hosts */
460 (strcmp(locking_host
, my_host
) || !kill(pid
, 0) || errno
== EPERM
);
465 rollback_lock_file(&lock
);
472 strbuf_addf(&sb
, "%"PRIuMAX
" %s",
473 (uintmax_t) getpid(), my_host
);
474 write_in_full(fd
, sb
.buf
, sb
.len
);
476 commit_lock_file(&lock
);
477 pidfile
= register_tempfile(pidfile_path
);
483 * Returns 0 if there was no previous error and gc can proceed, 1 if
484 * gc should not proceed due to an error in the last run. Prints a
485 * message and returns with a non-[01] status code if an error occurred
486 * while reading gc.log
488 static int report_last_gc_error(void)
490 struct strbuf sb
= STRBUF_INIT
;
494 char *gc_log_path
= git_pathdup("gc.log");
496 if (stat(gc_log_path
, &st
)) {
500 ret
= die_message_errno(_("cannot stat '%s'"), gc_log_path
);
504 if (st
.st_mtime
< gc_log_expire_time
)
507 len
= strbuf_read_file(&sb
, gc_log_path
, 0);
509 ret
= die_message_errno(_("cannot read '%s'"), gc_log_path
);
512 * A previous gc failed. Report the error, and don't
513 * bother with an automatic gc run since it is likely
514 * to fail in the same way.
516 warning(_("The last gc run reported the following. "
517 "Please correct the root cause\n"
519 "Automatic cleanup will not be performed "
520 "until the file is removed.\n\n"
522 gc_log_path
, sb
.buf
);
531 static void gc_before_repack(void)
534 * We may be called twice, as both the pre- and
535 * post-daemonized phases will call us, but running these
536 * commands more than once is pointless and wasteful.
542 if (pack_refs
&& maintenance_task_pack_refs(NULL
))
543 die(FAILED_RUN
, "pack-refs");
545 if (prune_reflogs
&& run_command_v_opt(reflog
.v
, RUN_GIT_CMD
))
546 die(FAILED_RUN
, reflog
.v
[0]);
549 int cmd_gc(int argc
, const char **argv
, const char *prefix
)
558 int keep_largest_pack
= -1;
561 struct option builtin_gc_options
[] = {
562 OPT__QUIET(&quiet
, N_("suppress progress reporting")),
563 { OPTION_STRING
, 0, "prune", &prune_expire
, N_("date"),
564 N_("prune unreferenced objects"),
565 PARSE_OPT_OPTARG
, NULL
, (intptr_t)prune_expire
},
566 OPT_BOOL(0, "cruft", &cruft_packs
, N_("pack unreferenced objects separately")),
567 OPT_BOOL(0, "aggressive", &aggressive
, N_("be more thorough (increased runtime)")),
568 OPT_BOOL_F(0, "auto", &auto_gc
, N_("enable auto-gc mode"),
569 PARSE_OPT_NOCOMPLETE
),
570 OPT_BOOL_F(0, "force", &force
,
571 N_("force running gc even if there may be another gc running"),
572 PARSE_OPT_NOCOMPLETE
),
573 OPT_BOOL(0, "keep-largest-pack", &keep_largest_pack
,
574 N_("repack all other packs except the largest pack")),
578 if (argc
== 2 && !strcmp(argv
[1], "-h"))
579 usage_with_options(builtin_gc_usage
, builtin_gc_options
);
581 strvec_pushl(&reflog
, "reflog", "expire", "--all", NULL
);
582 strvec_pushl(&repack
, "repack", "-d", "-l", NULL
);
583 strvec_pushl(&prune
, "prune", "--expire", NULL
);
584 strvec_pushl(&prune_worktrees
, "worktree", "prune", "--expire", NULL
);
585 strvec_pushl(&rerere
, "rerere", "gc", NULL
);
587 /* default expiry time, overwritten in gc_config */
589 if (parse_expiry_date(gc_log_expire
, &gc_log_expire_time
))
590 die(_("failed to parse gc.logExpiry value %s"), gc_log_expire
);
593 pack_refs
= !is_bare_repository();
595 argc
= parse_options(argc
, argv
, prefix
, builtin_gc_options
,
596 builtin_gc_usage
, 0);
598 usage_with_options(builtin_gc_usage
, builtin_gc_options
);
600 if (prune_expire
&& parse_expiry_date(prune_expire
, &dummy
))
601 die(_("failed to parse prune expiry value %s"), prune_expire
);
604 strvec_push(&repack
, "-f");
605 if (aggressive_depth
> 0)
606 strvec_pushf(&repack
, "--depth=%d", aggressive_depth
);
607 if (aggressive_window
> 0)
608 strvec_pushf(&repack
, "--window=%d", aggressive_window
);
611 strvec_push(&repack
, "-q");
615 * Auto-gc should be least intrusive as possible.
621 fprintf(stderr
, _("Auto packing the repository in background for optimum performance.\n"));
623 fprintf(stderr
, _("Auto packing the repository for optimum performance.\n"));
624 fprintf(stderr
, _("See \"git help gc\" for manual housekeeping.\n"));
627 int ret
= report_last_gc_error();
630 /* Last gc --auto failed. Skip this one. */
633 /* an I/O error occurred, already reported */
636 if (lock_repo_for_gc(force
, &pid
))
638 gc_before_repack(); /* dies on failure */
639 delete_tempfile(&pidfile
);
642 * failure to daemonize is ok, we'll continue
645 daemonized
= !daemonize();
648 struct string_list keep_pack
= STRING_LIST_INIT_NODUP
;
650 if (keep_largest_pack
!= -1) {
651 if (keep_largest_pack
)
652 find_base_packs(&keep_pack
, 0);
653 } else if (big_pack_threshold
) {
654 find_base_packs(&keep_pack
, big_pack_threshold
);
657 add_repack_all_option(&keep_pack
);
658 string_list_clear(&keep_pack
, 0);
661 name
= lock_repo_for_gc(force
, &pid
);
664 return 0; /* be quiet on --auto */
665 die(_("gc is already running on machine '%s' pid %"PRIuMAX
" (use --force if not)"),
666 name
, (uintmax_t)pid
);
670 hold_lock_file_for_update(&log_lock
,
673 dup2(get_lock_file_fd(&log_lock
), 2);
674 sigchain_push_common(process_log_file_on_signal
);
675 atexit(process_log_file_at_exit
);
680 if (!repository_format_precious_objects
) {
681 if (run_command_v_opt(repack
.v
,
682 RUN_GIT_CMD
| RUN_CLOSE_OBJECT_STORE
))
683 die(FAILED_RUN
, repack
.v
[0]);
686 /* run `git prune` even if using cruft packs */
687 strvec_push(&prune
, prune_expire
);
689 strvec_push(&prune
, "--no-progress");
690 if (has_promisor_remote())
692 "--exclude-promisor-objects");
693 if (run_command_v_opt(prune
.v
, RUN_GIT_CMD
))
694 die(FAILED_RUN
, prune
.v
[0]);
698 if (prune_worktrees_expire
) {
699 strvec_push(&prune_worktrees
, prune_worktrees_expire
);
700 if (run_command_v_opt(prune_worktrees
.v
, RUN_GIT_CMD
))
701 die(FAILED_RUN
, prune_worktrees
.v
[0]);
704 if (run_command_v_opt(rerere
.v
, RUN_GIT_CMD
))
705 die(FAILED_RUN
, rerere
.v
[0]);
707 report_garbage
= report_pack_garbage
;
708 reprepare_packed_git(the_repository
);
709 if (pack_garbage
.nr
> 0) {
710 close_object_store(the_repository
->objects
);
711 clean_pack_garbage();
714 prepare_repo_settings(the_repository
);
715 if (the_repository
->settings
.gc_write_commit_graph
== 1)
716 write_commit_graph_reachable(the_repository
->objects
->odb
,
717 !quiet
&& !daemonized
? COMMIT_GRAPH_WRITE_PROGRESS
: 0,
720 if (auto_gc
&& too_many_loose_objects())
721 warning(_("There are too many unreachable loose objects; "
722 "run 'git prune' to remove them."));
725 unlink(git_path("gc.log"));
730 static const char *const builtin_maintenance_run_usage
[] = {
731 N_("git maintenance run [--auto] [--[no-]quiet] [--task=<task>] [--schedule]"),
735 enum schedule_priority
{
742 static enum schedule_priority
parse_schedule(const char *value
)
745 return SCHEDULE_NONE
;
746 if (!strcasecmp(value
, "hourly"))
747 return SCHEDULE_HOURLY
;
748 if (!strcasecmp(value
, "daily"))
749 return SCHEDULE_DAILY
;
750 if (!strcasecmp(value
, "weekly"))
751 return SCHEDULE_WEEKLY
;
752 return SCHEDULE_NONE
;
755 static int maintenance_opt_schedule(const struct option
*opt
, const char *arg
,
758 enum schedule_priority
*priority
= opt
->value
;
761 die(_("--no-schedule is not allowed"));
763 *priority
= parse_schedule(arg
);
766 die(_("unrecognized --schedule argument '%s'"), arg
);
771 struct maintenance_run_opts
{
774 enum schedule_priority schedule
;
777 /* Remember to update object flag allocation in object.h */
780 struct cg_auto_data
{
781 int num_not_in_graph
;
785 static int dfs_on_ref(const char *refname UNUSED
,
786 const struct object_id
*oid
,
790 struct cg_auto_data
*data
= (struct cg_auto_data
*)cb_data
;
792 struct object_id peeled
;
793 struct commit_list
*stack
= NULL
;
794 struct commit
*commit
;
796 if (!peel_iterated_oid(oid
, &peeled
))
798 if (oid_object_info(the_repository
, oid
, NULL
) != OBJ_COMMIT
)
801 commit
= lookup_commit(the_repository
, oid
);
804 if (parse_commit(commit
) ||
805 commit_graph_position(commit
) != COMMIT_NOT_FROM_GRAPH
)
808 data
->num_not_in_graph
++;
810 if (data
->num_not_in_graph
>= data
->limit
)
813 commit_list_append(commit
, &stack
);
815 while (!result
&& stack
) {
816 struct commit_list
*parent
;
818 commit
= pop_commit(&stack
);
820 for (parent
= commit
->parents
; parent
; parent
= parent
->next
) {
821 if (parse_commit(parent
->item
) ||
822 commit_graph_position(parent
->item
) != COMMIT_NOT_FROM_GRAPH
||
823 parent
->item
->object
.flags
& SEEN
)
826 parent
->item
->object
.flags
|= SEEN
;
827 data
->num_not_in_graph
++;
829 if (data
->num_not_in_graph
>= data
->limit
) {
834 commit_list_append(parent
->item
, &stack
);
838 free_commit_list(stack
);
842 static int should_write_commit_graph(void)
845 struct cg_auto_data data
;
847 data
.num_not_in_graph
= 0;
849 git_config_get_int("maintenance.commit-graph.auto",
857 result
= for_each_ref(dfs_on_ref
, &data
);
859 repo_clear_commit_marks(the_repository
, SEEN
);
864 static int run_write_commit_graph(struct maintenance_run_opts
*opts
)
866 struct child_process child
= CHILD_PROCESS_INIT
;
868 child
.git_cmd
= child
.close_object_store
= 1;
869 strvec_pushl(&child
.args
, "commit-graph", "write",
870 "--split", "--reachable", NULL
);
873 strvec_push(&child
.args
, "--no-progress");
875 return !!run_command(&child
);
878 static int maintenance_task_commit_graph(struct maintenance_run_opts
*opts
)
880 prepare_repo_settings(the_repository
);
881 if (!the_repository
->settings
.core_commit_graph
)
884 if (run_write_commit_graph(opts
)) {
885 error(_("failed to write commit-graph"));
892 static int fetch_remote(struct remote
*remote
, void *cbdata
)
894 struct maintenance_run_opts
*opts
= cbdata
;
895 struct child_process child
= CHILD_PROCESS_INIT
;
897 if (remote
->skip_default_update
)
901 strvec_pushl(&child
.args
, "fetch", remote
->name
,
902 "--prefetch", "--prune", "--no-tags",
903 "--no-write-fetch-head", "--recurse-submodules=no",
907 strvec_push(&child
.args
, "--quiet");
909 return !!run_command(&child
);
912 static int maintenance_task_prefetch(struct maintenance_run_opts
*opts
)
914 if (for_each_remote(fetch_remote
, opts
)) {
915 error(_("failed to prefetch remotes"));
922 static int maintenance_task_gc(struct maintenance_run_opts
*opts
)
924 struct child_process child
= CHILD_PROCESS_INIT
;
926 child
.git_cmd
= child
.close_object_store
= 1;
927 strvec_push(&child
.args
, "gc");
930 strvec_push(&child
.args
, "--auto");
932 strvec_push(&child
.args
, "--quiet");
934 strvec_push(&child
.args
, "--no-quiet");
936 return run_command(&child
);
939 static int prune_packed(struct maintenance_run_opts
*opts
)
941 struct child_process child
= CHILD_PROCESS_INIT
;
944 strvec_push(&child
.args
, "prune-packed");
947 strvec_push(&child
.args
, "--quiet");
949 return !!run_command(&child
);
952 struct write_loose_object_data
{
958 static int loose_object_auto_limit
= 100;
960 static int loose_object_count(const struct object_id
*oid
,
964 int *count
= (int*)data
;
965 if (++(*count
) >= loose_object_auto_limit
)
970 static int loose_object_auto_condition(void)
974 git_config_get_int("maintenance.loose-objects.auto",
975 &loose_object_auto_limit
);
977 if (!loose_object_auto_limit
)
979 if (loose_object_auto_limit
< 0)
982 return for_each_loose_file_in_objdir(the_repository
->objects
->odb
->path
,
987 static int bail_on_loose(const struct object_id
*oid
,
994 static int write_loose_object_to_stdin(const struct object_id
*oid
,
998 struct write_loose_object_data
*d
= (struct write_loose_object_data
*)data
;
1000 fprintf(d
->in
, "%s\n", oid_to_hex(oid
));
1002 return ++(d
->count
) > d
->batch_size
;
1005 static int pack_loose(struct maintenance_run_opts
*opts
)
1007 struct repository
*r
= the_repository
;
1009 struct write_loose_object_data data
;
1010 struct child_process pack_proc
= CHILD_PROCESS_INIT
;
1013 * Do not start pack-objects process
1014 * if there are no loose objects.
1016 if (!for_each_loose_file_in_objdir(r
->objects
->odb
->path
,
1021 pack_proc
.git_cmd
= 1;
1023 strvec_push(&pack_proc
.args
, "pack-objects");
1025 strvec_push(&pack_proc
.args
, "--quiet");
1026 strvec_pushf(&pack_proc
.args
, "%s/pack/loose", r
->objects
->odb
->path
);
1030 if (start_command(&pack_proc
)) {
1031 error(_("failed to start 'git pack-objects' process"));
1035 data
.in
= xfdopen(pack_proc
.in
, "w");
1037 data
.batch_size
= 50000;
1039 for_each_loose_file_in_objdir(r
->objects
->odb
->path
,
1040 write_loose_object_to_stdin
,
1047 if (finish_command(&pack_proc
)) {
1048 error(_("failed to finish 'git pack-objects' process"));
1055 static int maintenance_task_loose_objects(struct maintenance_run_opts
*opts
)
1057 return prune_packed(opts
) || pack_loose(opts
);
1060 static int incremental_repack_auto_condition(void)
1062 struct packed_git
*p
;
1063 int incremental_repack_auto_limit
= 10;
1066 prepare_repo_settings(the_repository
);
1067 if (!the_repository
->settings
.core_multi_pack_index
)
1070 git_config_get_int("maintenance.incremental-repack.auto",
1071 &incremental_repack_auto_limit
);
1073 if (!incremental_repack_auto_limit
)
1075 if (incremental_repack_auto_limit
< 0)
1078 for (p
= get_packed_git(the_repository
);
1079 count
< incremental_repack_auto_limit
&& p
;
1081 if (!p
->multi_pack_index
)
1085 return count
>= incremental_repack_auto_limit
;
1088 static int multi_pack_index_write(struct maintenance_run_opts
*opts
)
1090 struct child_process child
= CHILD_PROCESS_INIT
;
1093 strvec_pushl(&child
.args
, "multi-pack-index", "write", NULL
);
1096 strvec_push(&child
.args
, "--no-progress");
1098 if (run_command(&child
))
1099 return error(_("failed to write multi-pack-index"));
1104 static int multi_pack_index_expire(struct maintenance_run_opts
*opts
)
1106 struct child_process child
= CHILD_PROCESS_INIT
;
1108 child
.git_cmd
= child
.close_object_store
= 1;
1109 strvec_pushl(&child
.args
, "multi-pack-index", "expire", NULL
);
1112 strvec_push(&child
.args
, "--no-progress");
1114 if (run_command(&child
))
1115 return error(_("'git multi-pack-index expire' failed"));
1120 #define TWO_GIGABYTES (INT32_MAX)
1122 static off_t
get_auto_pack_size(void)
1125 * The "auto" value is special: we optimize for
1126 * one large pack-file (i.e. from a clone) and
1127 * expect the rest to be small and they can be
1130 * The strategy we select here is to select a
1131 * size that is one more than the second largest
1132 * pack-file. This ensures that we will repack
1133 * at least two packs if there are three or more
1137 off_t second_largest_size
= 0;
1139 struct packed_git
*p
;
1140 struct repository
*r
= the_repository
;
1142 reprepare_packed_git(r
);
1143 for (p
= get_all_packs(r
); p
; p
= p
->next
) {
1144 if (p
->pack_size
> max_size
) {
1145 second_largest_size
= max_size
;
1146 max_size
= p
->pack_size
;
1147 } else if (p
->pack_size
> second_largest_size
)
1148 second_largest_size
= p
->pack_size
;
1151 result_size
= second_largest_size
+ 1;
1153 /* But limit ourselves to a batch size of 2g */
1154 if (result_size
> TWO_GIGABYTES
)
1155 result_size
= TWO_GIGABYTES
;
1160 static int multi_pack_index_repack(struct maintenance_run_opts
*opts
)
1162 struct child_process child
= CHILD_PROCESS_INIT
;
1164 child
.git_cmd
= child
.close_object_store
= 1;
1165 strvec_pushl(&child
.args
, "multi-pack-index", "repack", NULL
);
1168 strvec_push(&child
.args
, "--no-progress");
1170 strvec_pushf(&child
.args
, "--batch-size=%"PRIuMAX
,
1171 (uintmax_t)get_auto_pack_size());
1173 if (run_command(&child
))
1174 return error(_("'git multi-pack-index repack' failed"));
1179 static int maintenance_task_incremental_repack(struct maintenance_run_opts
*opts
)
1181 prepare_repo_settings(the_repository
);
1182 if (!the_repository
->settings
.core_multi_pack_index
) {
1183 warning(_("skipping incremental-repack task because core.multiPackIndex is disabled"));
1187 if (multi_pack_index_write(opts
))
1189 if (multi_pack_index_expire(opts
))
1191 if (multi_pack_index_repack(opts
))
1196 typedef int maintenance_task_fn(struct maintenance_run_opts
*opts
);
1199 * An auto condition function returns 1 if the task should run
1200 * and 0 if the task should NOT run. See needs_to_gc() for an
1203 typedef int maintenance_auto_fn(void);
1205 struct maintenance_task
{
1207 maintenance_task_fn
*fn
;
1208 maintenance_auto_fn
*auto_condition
;
1211 enum schedule_priority schedule
;
1213 /* -1 if not selected. */
1217 enum maintenance_task_label
{
1220 TASK_INCREMENTAL_REPACK
,
1225 /* Leave as final value */
1229 static struct maintenance_task tasks
[] = {
1232 maintenance_task_prefetch
,
1234 [TASK_LOOSE_OBJECTS
] = {
1236 maintenance_task_loose_objects
,
1237 loose_object_auto_condition
,
1239 [TASK_INCREMENTAL_REPACK
] = {
1240 "incremental-repack",
1241 maintenance_task_incremental_repack
,
1242 incremental_repack_auto_condition
,
1246 maintenance_task_gc
,
1250 [TASK_COMMIT_GRAPH
] = {
1252 maintenance_task_commit_graph
,
1253 should_write_commit_graph
,
1255 [TASK_PACK_REFS
] = {
1257 maintenance_task_pack_refs
,
1262 static int compare_tasks_by_selection(const void *a_
, const void *b_
)
1264 const struct maintenance_task
*a
= a_
;
1265 const struct maintenance_task
*b
= b_
;
1267 return b
->selected_order
- a
->selected_order
;
1270 static int maintenance_run_tasks(struct maintenance_run_opts
*opts
)
1272 int i
, found_selected
= 0;
1274 struct lock_file lk
;
1275 struct repository
*r
= the_repository
;
1276 char *lock_path
= xstrfmt("%s/maintenance", r
->objects
->odb
->path
);
1278 if (hold_lock_file_for_update(&lk
, lock_path
, LOCK_NO_DEREF
) < 0) {
1280 * Another maintenance command is running.
1282 * If --auto was provided, then it is likely due to a
1283 * recursive process stack. Do not report an error in
1286 if (!opts
->auto_flag
&& !opts
->quiet
)
1287 warning(_("lock file '%s' exists, skipping maintenance"),
1294 for (i
= 0; !found_selected
&& i
< TASK__COUNT
; i
++)
1295 found_selected
= tasks
[i
].selected_order
>= 0;
1298 QSORT(tasks
, TASK__COUNT
, compare_tasks_by_selection
);
1300 for (i
= 0; i
< TASK__COUNT
; i
++) {
1301 if (found_selected
&& tasks
[i
].selected_order
< 0)
1304 if (!found_selected
&& !tasks
[i
].enabled
)
1307 if (opts
->auto_flag
&&
1308 (!tasks
[i
].auto_condition
||
1309 !tasks
[i
].auto_condition()))
1312 if (opts
->schedule
&& tasks
[i
].schedule
< opts
->schedule
)
1315 trace2_region_enter("maintenance", tasks
[i
].name
, r
);
1316 if (tasks
[i
].fn(opts
)) {
1317 error(_("task '%s' failed"), tasks
[i
].name
);
1320 trace2_region_leave("maintenance", tasks
[i
].name
, r
);
1323 rollback_lock_file(&lk
);
1327 static void initialize_maintenance_strategy(void)
1331 if (git_config_get_string("maintenance.strategy", &config_str
))
1334 if (!strcasecmp(config_str
, "incremental")) {
1335 tasks
[TASK_GC
].schedule
= SCHEDULE_NONE
;
1336 tasks
[TASK_COMMIT_GRAPH
].enabled
= 1;
1337 tasks
[TASK_COMMIT_GRAPH
].schedule
= SCHEDULE_HOURLY
;
1338 tasks
[TASK_PREFETCH
].enabled
= 1;
1339 tasks
[TASK_PREFETCH
].schedule
= SCHEDULE_HOURLY
;
1340 tasks
[TASK_INCREMENTAL_REPACK
].enabled
= 1;
1341 tasks
[TASK_INCREMENTAL_REPACK
].schedule
= SCHEDULE_DAILY
;
1342 tasks
[TASK_LOOSE_OBJECTS
].enabled
= 1;
1343 tasks
[TASK_LOOSE_OBJECTS
].schedule
= SCHEDULE_DAILY
;
1344 tasks
[TASK_PACK_REFS
].enabled
= 1;
1345 tasks
[TASK_PACK_REFS
].schedule
= SCHEDULE_WEEKLY
;
1349 static void initialize_task_config(int schedule
)
1352 struct strbuf config_name
= STRBUF_INIT
;
1356 initialize_maintenance_strategy();
1358 for (i
= 0; i
< TASK__COUNT
; i
++) {
1362 strbuf_reset(&config_name
);
1363 strbuf_addf(&config_name
, "maintenance.%s.enabled",
1366 if (!git_config_get_bool(config_name
.buf
, &config_value
))
1367 tasks
[i
].enabled
= config_value
;
1369 strbuf_reset(&config_name
);
1370 strbuf_addf(&config_name
, "maintenance.%s.schedule",
1373 if (!git_config_get_string(config_name
.buf
, &config_str
)) {
1374 tasks
[i
].schedule
= parse_schedule(config_str
);
1379 strbuf_release(&config_name
);
1382 static int task_option_parse(const struct option
*opt
,
1383 const char *arg
, int unset
)
1385 int i
, num_selected
= 0;
1386 struct maintenance_task
*task
= NULL
;
1388 BUG_ON_OPT_NEG(unset
);
1390 for (i
= 0; i
< TASK__COUNT
; i
++) {
1391 if (tasks
[i
].selected_order
>= 0)
1393 if (!strcasecmp(tasks
[i
].name
, arg
)) {
1399 error(_("'%s' is not a valid task"), arg
);
1403 if (task
->selected_order
>= 0) {
1404 error(_("task '%s' cannot be selected multiple times"), arg
);
1408 task
->selected_order
= num_selected
+ 1;
1413 static int maintenance_run(int argc
, const char **argv
, const char *prefix
)
1416 struct maintenance_run_opts opts
;
1417 struct option builtin_maintenance_run_options
[] = {
1418 OPT_BOOL(0, "auto", &opts
.auto_flag
,
1419 N_("run tasks based on the state of the repository")),
1420 OPT_CALLBACK(0, "schedule", &opts
.schedule
, N_("frequency"),
1421 N_("run tasks based on frequency"),
1422 maintenance_opt_schedule
),
1423 OPT_BOOL(0, "quiet", &opts
.quiet
,
1424 N_("do not report progress or other information over stderr")),
1425 OPT_CALLBACK_F(0, "task", NULL
, N_("task"),
1426 N_("run a specific task"),
1427 PARSE_OPT_NONEG
, task_option_parse
),
1430 memset(&opts
, 0, sizeof(opts
));
1432 opts
.quiet
= !isatty(2);
1434 for (i
= 0; i
< TASK__COUNT
; i
++)
1435 tasks
[i
].selected_order
= -1;
1437 argc
= parse_options(argc
, argv
, prefix
,
1438 builtin_maintenance_run_options
,
1439 builtin_maintenance_run_usage
,
1440 PARSE_OPT_STOP_AT_NON_OPTION
);
1442 if (opts
.auto_flag
&& opts
.schedule
)
1443 die(_("use at most one of --auto and --schedule=<frequency>"));
1445 initialize_task_config(opts
.schedule
);
1448 usage_with_options(builtin_maintenance_run_usage
,
1449 builtin_maintenance_run_options
);
1450 return maintenance_run_tasks(&opts
);
1453 static char *get_maintpath(void)
1455 struct strbuf sb
= STRBUF_INIT
;
1456 const char *p
= the_repository
->worktree
?
1457 the_repository
->worktree
: the_repository
->gitdir
;
1459 strbuf_realpath(&sb
, p
, 1);
1460 return strbuf_detach(&sb
, NULL
);
1463 static char const * const builtin_maintenance_register_usage
[] = {
1464 "git maintenance register",
1468 static int maintenance_register(int argc
, const char **argv
, const char *prefix
)
1470 struct option options
[] = {
1475 struct child_process config_set
= CHILD_PROCESS_INIT
;
1476 struct child_process config_get
= CHILD_PROCESS_INIT
;
1477 char *maintpath
= get_maintpath();
1479 argc
= parse_options(argc
, argv
, prefix
, options
,
1480 builtin_maintenance_register_usage
, 0);
1482 usage_with_options(builtin_maintenance_register_usage
,
1485 /* Disable foreground maintenance */
1486 git_config_set("maintenance.auto", "false");
1488 /* Set maintenance strategy, if unset */
1489 if (!git_config_get_string("maintenance.strategy", &config_value
))
1492 git_config_set("maintenance.strategy", "incremental");
1494 config_get
.git_cmd
= 1;
1495 strvec_pushl(&config_get
.args
, "config", "--global", "--get",
1496 "--fixed-value", "maintenance.repo", maintpath
, NULL
);
1497 config_get
.out
= -1;
1499 if (start_command(&config_get
)) {
1500 rc
= error(_("failed to run 'git config'"));
1504 /* We already have this value in our config! */
1505 if (!finish_command(&config_get
)) {
1510 config_set
.git_cmd
= 1;
1511 strvec_pushl(&config_set
.args
, "config", "--add", "--global", "maintenance.repo",
1514 rc
= run_command(&config_set
);
1521 static char const * const builtin_maintenance_unregister_usage
[] = {
1522 "git maintenance unregister [--force]",
1526 static int maintenance_unregister(int argc
, const char **argv
, const char *prefix
)
1529 struct option options
[] = {
1531 N_("return success even if repository was not registered"),
1532 PARSE_OPT_NOCOMPLETE
),
1535 const char *key
= "maintenance.repo";
1537 struct child_process config_unset
= CHILD_PROCESS_INIT
;
1538 char *maintpath
= get_maintpath();
1540 struct string_list_item
*item
;
1541 const struct string_list
*list
;
1543 argc
= parse_options(argc
, argv
, prefix
, options
,
1544 builtin_maintenance_unregister_usage
, 0);
1546 usage_with_options(builtin_maintenance_unregister_usage
,
1549 list
= git_config_get_value_multi(key
);
1551 for_each_string_list_item(item
, list
) {
1552 if (!strcmp(maintpath
, item
->string
)) {
1560 config_unset
.git_cmd
= 1;
1561 strvec_pushl(&config_unset
.args
, "config", "--global", "--unset",
1562 "--fixed-value", key
, maintpath
, NULL
);
1564 rc
= run_command(&config_unset
);
1565 } else if (!force
) {
1566 die(_("repository '%s' is not registered"), maintpath
);
1573 static const char *get_frequency(enum schedule_priority schedule
)
1576 case SCHEDULE_HOURLY
:
1578 case SCHEDULE_DAILY
:
1580 case SCHEDULE_WEEKLY
:
1583 BUG("invalid schedule %d", schedule
);
1588 * get_schedule_cmd` reads the GIT_TEST_MAINT_SCHEDULER environment variable
1589 * to mock the schedulers that `git maintenance start` rely on.
1591 * For test purpose, GIT_TEST_MAINT_SCHEDULER can be set to a comma-separated
1592 * list of colon-separated key/value pairs where each pair contains a scheduler
1593 * and its corresponding mock.
1595 * * If $GIT_TEST_MAINT_SCHEDULER is not set, return false and leave the
1596 * arguments unmodified.
1598 * * If $GIT_TEST_MAINT_SCHEDULER is set, return true.
1599 * In this case, the *cmd value is read as input.
1601 * * if the input value *cmd is the key of one of the comma-separated list
1602 * item, then *is_available is set to true and *cmd is modified and becomes
1605 * * if the input value *cmd isn’t the key of any of the comma-separated list
1606 * item, then *is_available is set to false.
1609 * GIT_TEST_MAINT_SCHEDULER not set
1610 * +-------+-------------------------------------------------+
1611 * | Input | Output |
1612 * | *cmd | return code | *cmd | *is_available |
1613 * +-------+-------------+-------------------+---------------+
1614 * | "foo" | false | "foo" (unchanged) | (unchanged) |
1615 * +-------+-------------+-------------------+---------------+
1617 * GIT_TEST_MAINT_SCHEDULER set to “foo:./mock_foo.sh,bar:./mock_bar.sh”
1618 * +-------+-------------------------------------------------+
1619 * | Input | Output |
1620 * | *cmd | return code | *cmd | *is_available |
1621 * +-------+-------------+-------------------+---------------+
1622 * | "foo" | true | "./mock.foo.sh" | true |
1623 * | "qux" | true | "qux" (unchanged) | false |
1624 * +-------+-------------+-------------------+---------------+
1626 static int get_schedule_cmd(const char **cmd
, int *is_available
)
1628 char *testing
= xstrdup_or_null(getenv("GIT_TEST_MAINT_SCHEDULER"));
1629 struct string_list_item
*item
;
1630 struct string_list list
= STRING_LIST_INIT_NODUP
;
1638 string_list_split_in_place(&list
, testing
, ',', -1);
1639 for_each_string_list_item(item
, &list
) {
1640 struct string_list pair
= STRING_LIST_INIT_NODUP
;
1642 if (string_list_split_in_place(&pair
, item
->string
, ':', 2) != 2)
1645 if (!strcmp(*cmd
, pair
.items
[0].string
)) {
1646 *cmd
= pair
.items
[1].string
;
1649 string_list_clear(&list
, 0);
1655 string_list_clear(&list
, 0);
1660 static int is_launchctl_available(void)
1662 const char *cmd
= "launchctl";
1664 if (get_schedule_cmd(&cmd
, &is_available
))
1665 return is_available
;
1674 static char *launchctl_service_name(const char *frequency
)
1676 struct strbuf label
= STRBUF_INIT
;
1677 strbuf_addf(&label
, "org.git-scm.git.%s", frequency
);
1678 return strbuf_detach(&label
, NULL
);
1681 static char *launchctl_service_filename(const char *name
)
1684 struct strbuf filename
= STRBUF_INIT
;
1685 strbuf_addf(&filename
, "~/Library/LaunchAgents/%s.plist", name
);
1687 expanded
= interpolate_path(filename
.buf
, 1);
1689 die(_("failed to expand path '%s'"), filename
.buf
);
1691 strbuf_release(&filename
);
1695 static char *launchctl_get_uid(void)
1697 return xstrfmt("gui/%d", getuid());
1700 static int launchctl_boot_plist(int enable
, const char *filename
)
1702 const char *cmd
= "launchctl";
1704 struct child_process child
= CHILD_PROCESS_INIT
;
1705 char *uid
= launchctl_get_uid();
1707 get_schedule_cmd(&cmd
, NULL
);
1708 strvec_split(&child
.args
, cmd
);
1709 strvec_pushl(&child
.args
, enable
? "bootstrap" : "bootout", uid
,
1712 child
.no_stderr
= 1;
1713 child
.no_stdout
= 1;
1715 if (start_command(&child
))
1716 die(_("failed to start launchctl"));
1718 result
= finish_command(&child
);
1724 static int launchctl_remove_plist(enum schedule_priority schedule
)
1726 const char *frequency
= get_frequency(schedule
);
1727 char *name
= launchctl_service_name(frequency
);
1728 char *filename
= launchctl_service_filename(name
);
1729 int result
= launchctl_boot_plist(0, filename
);
1736 static int launchctl_remove_plists(void)
1738 return launchctl_remove_plist(SCHEDULE_HOURLY
) ||
1739 launchctl_remove_plist(SCHEDULE_DAILY
) ||
1740 launchctl_remove_plist(SCHEDULE_WEEKLY
);
1743 static int launchctl_list_contains_plist(const char *name
, const char *cmd
)
1745 struct child_process child
= CHILD_PROCESS_INIT
;
1747 strvec_split(&child
.args
, cmd
);
1748 strvec_pushl(&child
.args
, "list", name
, NULL
);
1750 child
.no_stderr
= 1;
1751 child
.no_stdout
= 1;
1753 if (start_command(&child
))
1754 die(_("failed to start launchctl"));
1756 /* Returns failure if 'name' doesn't exist. */
1757 return !finish_command(&child
);
1760 static int launchctl_schedule_plist(const char *exec_path
, enum schedule_priority schedule
)
1763 const char *preamble
, *repeat
;
1764 const char *frequency
= get_frequency(schedule
);
1765 char *name
= launchctl_service_name(frequency
);
1766 char *filename
= launchctl_service_filename(name
);
1767 struct lock_file lk
= LOCK_INIT
;
1768 static unsigned long lock_file_timeout_ms
= ULONG_MAX
;
1769 struct strbuf plist
= STRBUF_INIT
, plist2
= STRBUF_INIT
;
1771 const char *cmd
= "launchctl";
1773 get_schedule_cmd(&cmd
, NULL
);
1774 preamble
= "<?xml version=\"1.0\"?>\n"
1775 "<!DOCTYPE plist PUBLIC \"-//Apple//DTD PLIST 1.0//EN\" \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">\n"
1776 "<plist version=\"1.0\">"
1778 "<key>Label</key><string>%s</string>\n"
1779 "<key>ProgramArguments</key>\n"
1781 "<string>%s/git</string>\n"
1782 "<string>--exec-path=%s</string>\n"
1783 "<string>for-each-repo</string>\n"
1784 "<string>--config=maintenance.repo</string>\n"
1785 "<string>maintenance</string>\n"
1786 "<string>run</string>\n"
1787 "<string>--schedule=%s</string>\n"
1789 "<key>StartCalendarInterval</key>\n"
1791 strbuf_addf(&plist
, preamble
, name
, exec_path
, exec_path
, frequency
);
1794 case SCHEDULE_HOURLY
:
1796 "<key>Hour</key><integer>%d</integer>\n"
1797 "<key>Minute</key><integer>0</integer>\n"
1799 for (i
= 1; i
<= 23; i
++)
1800 strbuf_addf(&plist
, repeat
, i
);
1803 case SCHEDULE_DAILY
:
1805 "<key>Day</key><integer>%d</integer>\n"
1806 "<key>Hour</key><integer>0</integer>\n"
1807 "<key>Minute</key><integer>0</integer>\n"
1809 for (i
= 1; i
<= 6; i
++)
1810 strbuf_addf(&plist
, repeat
, i
);
1813 case SCHEDULE_WEEKLY
:
1814 strbuf_addstr(&plist
,
1816 "<key>Day</key><integer>0</integer>\n"
1817 "<key>Hour</key><integer>0</integer>\n"
1818 "<key>Minute</key><integer>0</integer>\n"
1826 strbuf_addstr(&plist
, "</array>\n</dict>\n</plist>\n");
1828 if (safe_create_leading_directories(filename
))
1829 die(_("failed to create directories for '%s'"), filename
);
1831 if ((long)lock_file_timeout_ms
< 0 &&
1832 git_config_get_ulong("gc.launchctlplistlocktimeoutms",
1833 &lock_file_timeout_ms
))
1834 lock_file_timeout_ms
= 150;
1836 fd
= hold_lock_file_for_update_timeout(&lk
, filename
, LOCK_DIE_ON_ERROR
,
1837 lock_file_timeout_ms
);
1840 * Does this file already exist? With the intended contents? Is it
1841 * registered already? Then it does not need to be re-registered.
1843 if (!stat(filename
, &st
) && st
.st_size
== plist
.len
&&
1844 strbuf_read_file(&plist2
, filename
, plist
.len
) == plist
.len
&&
1845 !strbuf_cmp(&plist
, &plist2
) &&
1846 launchctl_list_contains_plist(name
, cmd
))
1847 rollback_lock_file(&lk
);
1849 if (write_in_full(fd
, plist
.buf
, plist
.len
) < 0 ||
1850 commit_lock_file(&lk
))
1851 die_errno(_("could not write '%s'"), filename
);
1853 /* bootout might fail if not already running, so ignore */
1854 launchctl_boot_plist(0, filename
);
1855 if (launchctl_boot_plist(1, filename
))
1856 die(_("failed to bootstrap service %s"), filename
);
1861 strbuf_release(&plist
);
1862 strbuf_release(&plist2
);
1866 static int launchctl_add_plists(void)
1868 const char *exec_path
= git_exec_path();
1870 return launchctl_schedule_plist(exec_path
, SCHEDULE_HOURLY
) ||
1871 launchctl_schedule_plist(exec_path
, SCHEDULE_DAILY
) ||
1872 launchctl_schedule_plist(exec_path
, SCHEDULE_WEEKLY
);
1875 static int launchctl_update_schedule(int run_maintenance
, int fd
)
1877 if (run_maintenance
)
1878 return launchctl_add_plists();
1880 return launchctl_remove_plists();
1883 static int is_schtasks_available(void)
1885 const char *cmd
= "schtasks";
1887 if (get_schedule_cmd(&cmd
, &is_available
))
1888 return is_available
;
1890 #ifdef GIT_WINDOWS_NATIVE
1897 static char *schtasks_task_name(const char *frequency
)
1899 struct strbuf label
= STRBUF_INIT
;
1900 strbuf_addf(&label
, "Git Maintenance (%s)", frequency
);
1901 return strbuf_detach(&label
, NULL
);
1904 static int schtasks_remove_task(enum schedule_priority schedule
)
1906 const char *cmd
= "schtasks";
1908 struct strvec args
= STRVEC_INIT
;
1909 const char *frequency
= get_frequency(schedule
);
1910 char *name
= schtasks_task_name(frequency
);
1912 get_schedule_cmd(&cmd
, NULL
);
1913 strvec_split(&args
, cmd
);
1914 strvec_pushl(&args
, "/delete", "/tn", name
, "/f", NULL
);
1916 result
= run_command_v_opt(args
.v
, 0);
1918 strvec_clear(&args
);
1923 static int schtasks_remove_tasks(void)
1925 return schtasks_remove_task(SCHEDULE_HOURLY
) ||
1926 schtasks_remove_task(SCHEDULE_DAILY
) ||
1927 schtasks_remove_task(SCHEDULE_WEEKLY
);
1930 static int schtasks_schedule_task(const char *exec_path
, enum schedule_priority schedule
)
1932 const char *cmd
= "schtasks";
1934 struct child_process child
= CHILD_PROCESS_INIT
;
1936 struct tempfile
*tfile
;
1937 const char *frequency
= get_frequency(schedule
);
1938 char *name
= schtasks_task_name(frequency
);
1939 struct strbuf tfilename
= STRBUF_INIT
;
1941 get_schedule_cmd(&cmd
, NULL
);
1943 strbuf_addf(&tfilename
, "%s/schedule_%s_XXXXXX",
1944 get_git_common_dir(), frequency
);
1945 tfile
= xmks_tempfile(tfilename
.buf
);
1946 strbuf_release(&tfilename
);
1948 if (!fdopen_tempfile(tfile
, "w"))
1949 die(_("failed to create temp xml file"));
1951 xml
= "<?xml version=\"1.0\" ?>\n"
1952 "<Task version=\"1.4\" xmlns=\"http://schemas.microsoft.com/windows/2004/02/mit/task\">\n"
1954 "<CalendarTrigger>\n";
1955 fputs(xml
, tfile
->fp
);
1958 case SCHEDULE_HOURLY
:
1960 "<StartBoundary>2020-01-01T01:00:00</StartBoundary>\n"
1961 "<Enabled>true</Enabled>\n"
1963 "<DaysInterval>1</DaysInterval>\n"
1964 "</ScheduleByDay>\n"
1966 "<Interval>PT1H</Interval>\n"
1967 "<Duration>PT23H</Duration>\n"
1968 "<StopAtDurationEnd>false</StopAtDurationEnd>\n"
1972 case SCHEDULE_DAILY
:
1974 "<StartBoundary>2020-01-01T00:00:00</StartBoundary>\n"
1975 "<Enabled>true</Enabled>\n"
1976 "<ScheduleByWeek>\n"
1985 "<WeeksInterval>1</WeeksInterval>\n"
1986 "</ScheduleByWeek>\n");
1989 case SCHEDULE_WEEKLY
:
1991 "<StartBoundary>2020-01-01T00:00:00</StartBoundary>\n"
1992 "<Enabled>true</Enabled>\n"
1993 "<ScheduleByWeek>\n"
1997 "<WeeksInterval>1</WeeksInterval>\n"
1998 "</ScheduleByWeek>\n");
2005 xml
= "</CalendarTrigger>\n"
2008 "<Principal id=\"Author\">\n"
2009 "<LogonType>InteractiveToken</LogonType>\n"
2010 "<RunLevel>LeastPrivilege</RunLevel>\n"
2014 "<MultipleInstancesPolicy>IgnoreNew</MultipleInstancesPolicy>\n"
2015 "<Enabled>true</Enabled>\n"
2016 "<Hidden>true</Hidden>\n"
2017 "<UseUnifiedSchedulingEngine>true</UseUnifiedSchedulingEngine>\n"
2018 "<WakeToRun>false</WakeToRun>\n"
2019 "<ExecutionTimeLimit>PT72H</ExecutionTimeLimit>\n"
2020 "<Priority>7</Priority>\n"
2022 "<Actions Context=\"Author\">\n"
2024 "<Command>\"%s\\git.exe\"</Command>\n"
2025 "<Arguments>--exec-path=\"%s\" for-each-repo --config=maintenance.repo maintenance run --schedule=%s</Arguments>\n"
2029 fprintf(tfile
->fp
, xml
, exec_path
, exec_path
, frequency
);
2030 strvec_split(&child
.args
, cmd
);
2031 strvec_pushl(&child
.args
, "/create", "/tn", name
, "/f", "/xml",
2032 get_tempfile_path(tfile
), NULL
);
2033 close_tempfile_gently(tfile
);
2035 child
.no_stdout
= 1;
2036 child
.no_stderr
= 1;
2038 if (start_command(&child
))
2039 die(_("failed to start schtasks"));
2040 result
= finish_command(&child
);
2042 delete_tempfile(&tfile
);
2047 static int schtasks_schedule_tasks(void)
2049 const char *exec_path
= git_exec_path();
2051 return schtasks_schedule_task(exec_path
, SCHEDULE_HOURLY
) ||
2052 schtasks_schedule_task(exec_path
, SCHEDULE_DAILY
) ||
2053 schtasks_schedule_task(exec_path
, SCHEDULE_WEEKLY
);
2056 static int schtasks_update_schedule(int run_maintenance
, int fd
)
2058 if (run_maintenance
)
2059 return schtasks_schedule_tasks();
2061 return schtasks_remove_tasks();
2065 static int check_crontab_process(const char *cmd
)
2067 struct child_process child
= CHILD_PROCESS_INIT
;
2069 strvec_split(&child
.args
, cmd
);
2070 strvec_push(&child
.args
, "-l");
2072 child
.no_stdout
= 1;
2073 child
.no_stderr
= 1;
2074 child
.silent_exec_failure
= 1;
2076 if (start_command(&child
))
2078 /* Ignore exit code, as an empty crontab will return error. */
2079 finish_command(&child
);
2083 static int is_crontab_available(void)
2085 const char *cmd
= "crontab";
2088 if (get_schedule_cmd(&cmd
, &is_available
))
2089 return is_available
;
2093 * macOS has cron, but it requires special permissions and will
2094 * create a UI alert when attempting to run this command.
2098 return check_crontab_process(cmd
);
2102 #define BEGIN_LINE "# BEGIN GIT MAINTENANCE SCHEDULE"
2103 #define END_LINE "# END GIT MAINTENANCE SCHEDULE"
2105 static int crontab_update_schedule(int run_maintenance
, int fd
)
2107 const char *cmd
= "crontab";
2109 int in_old_region
= 0;
2110 struct child_process crontab_list
= CHILD_PROCESS_INIT
;
2111 struct child_process crontab_edit
= CHILD_PROCESS_INIT
;
2112 FILE *cron_list
, *cron_in
;
2113 struct strbuf line
= STRBUF_INIT
;
2114 struct tempfile
*tmpedit
= NULL
;
2116 get_schedule_cmd(&cmd
, NULL
);
2117 strvec_split(&crontab_list
.args
, cmd
);
2118 strvec_push(&crontab_list
.args
, "-l");
2119 crontab_list
.in
= -1;
2120 crontab_list
.out
= dup(fd
);
2121 crontab_list
.git_cmd
= 0;
2123 if (start_command(&crontab_list
))
2124 return error(_("failed to run 'crontab -l'; your system might not support 'cron'"));
2126 /* Ignore exit code, as an empty crontab will return error. */
2127 finish_command(&crontab_list
);
2129 tmpedit
= mks_tempfile_t(".git_cron_edit_tmpXXXXXX");
2131 result
= error(_("failed to create crontab temporary file"));
2134 cron_in
= fdopen_tempfile(tmpedit
, "w");
2136 result
= error(_("failed to open temporary file"));
2141 * Read from the .lock file, filtering out the old
2142 * schedule while appending the new schedule.
2144 cron_list
= fdopen(fd
, "r");
2147 while (!strbuf_getline_lf(&line
, cron_list
)) {
2148 if (!in_old_region
&& !strcmp(line
.buf
, BEGIN_LINE
))
2150 else if (in_old_region
&& !strcmp(line
.buf
, END_LINE
))
2152 else if (!in_old_region
)
2153 fprintf(cron_in
, "%s\n", line
.buf
);
2155 strbuf_release(&line
);
2157 if (run_maintenance
) {
2158 struct strbuf line_format
= STRBUF_INIT
;
2159 const char *exec_path
= git_exec_path();
2161 fprintf(cron_in
, "%s\n", BEGIN_LINE
);
2163 "# The following schedule was created by Git\n");
2164 fprintf(cron_in
, "# Any edits made in this region might be\n");
2166 "# replaced in the future by a Git command.\n\n");
2168 strbuf_addf(&line_format
,
2169 "%%s %%s * * %%s \"%s/git\" --exec-path=\"%s\" for-each-repo --config=maintenance.repo maintenance run --schedule=%%s\n",
2170 exec_path
, exec_path
);
2171 fprintf(cron_in
, line_format
.buf
, "0", "1-23", "*", "hourly");
2172 fprintf(cron_in
, line_format
.buf
, "0", "0", "1-6", "daily");
2173 fprintf(cron_in
, line_format
.buf
, "0", "0", "0", "weekly");
2174 strbuf_release(&line_format
);
2176 fprintf(cron_in
, "\n%s\n", END_LINE
);
2181 strvec_split(&crontab_edit
.args
, cmd
);
2182 strvec_push(&crontab_edit
.args
, get_tempfile_path(tmpedit
));
2183 crontab_edit
.git_cmd
= 0;
2185 if (start_command(&crontab_edit
)) {
2186 result
= error(_("failed to run 'crontab'; your system might not support 'cron'"));
2190 if (finish_command(&crontab_edit
))
2191 result
= error(_("'crontab' died"));
2195 delete_tempfile(&tmpedit
);
2199 static int real_is_systemd_timer_available(void)
2201 struct child_process child
= CHILD_PROCESS_INIT
;
2203 strvec_pushl(&child
.args
, "systemctl", "--user", "list-timers", NULL
);
2205 child
.no_stdout
= 1;
2206 child
.no_stderr
= 1;
2207 child
.silent_exec_failure
= 1;
2209 if (start_command(&child
))
2211 if (finish_command(&child
))
2216 static int is_systemd_timer_available(void)
2218 const char *cmd
= "systemctl";
2221 if (get_schedule_cmd(&cmd
, &is_available
))
2222 return is_available
;
2224 return real_is_systemd_timer_available();
2227 static char *xdg_config_home_systemd(const char *filename
)
2229 return xdg_config_home_for("systemd/user", filename
);
2232 static int systemd_timer_enable_unit(int enable
,
2233 enum schedule_priority schedule
)
2235 const char *cmd
= "systemctl";
2236 struct child_process child
= CHILD_PROCESS_INIT
;
2237 const char *frequency
= get_frequency(schedule
);
2240 * Disabling the systemd unit while it is already disabled makes
2241 * systemctl print an error.
2242 * Let's ignore it since it means we already are in the expected state:
2243 * the unit is disabled.
2245 * On the other hand, enabling a systemd unit which is already enabled
2246 * produces no error.
2249 child
.no_stderr
= 1;
2251 get_schedule_cmd(&cmd
, NULL
);
2252 strvec_split(&child
.args
, cmd
);
2253 strvec_pushl(&child
.args
, "--user", enable
? "enable" : "disable",
2255 strvec_pushf(&child
.args
, "git-maintenance@%s.timer", frequency
);
2257 if (start_command(&child
))
2258 return error(_("failed to start systemctl"));
2259 if (finish_command(&child
))
2261 * Disabling an already disabled systemd unit makes
2263 * Let's ignore this failure.
2265 * Enabling an enabled systemd unit doesn't fail.
2268 return error(_("failed to run systemctl"));
2272 static int systemd_timer_delete_unit_templates(void)
2275 char *filename
= xdg_config_home_systemd("git-maintenance@.timer");
2276 if (unlink(filename
) && !is_missing_file_error(errno
))
2277 ret
= error_errno(_("failed to delete '%s'"), filename
);
2278 FREE_AND_NULL(filename
);
2280 filename
= xdg_config_home_systemd("git-maintenance@.service");
2281 if (unlink(filename
) && !is_missing_file_error(errno
))
2282 ret
= error_errno(_("failed to delete '%s'"), filename
);
2288 static int systemd_timer_delete_units(void)
2290 return systemd_timer_enable_unit(0, SCHEDULE_HOURLY
) ||
2291 systemd_timer_enable_unit(0, SCHEDULE_DAILY
) ||
2292 systemd_timer_enable_unit(0, SCHEDULE_WEEKLY
) ||
2293 systemd_timer_delete_unit_templates();
2296 static int systemd_timer_write_unit_templates(const char *exec_path
)
2302 filename
= xdg_config_home_systemd("git-maintenance@.timer");
2303 if (safe_create_leading_directories(filename
)) {
2304 error(_("failed to create directories for '%s'"), filename
);
2307 file
= fopen_or_warn(filename
, "w");
2311 unit
= "# This file was created and is maintained by Git.\n"
2312 "# Any edits made in this file might be replaced in the future\n"
2313 "# by a Git command.\n"
2316 "Description=Optimize Git repositories data\n"
2323 "WantedBy=timers.target\n";
2324 if (fputs(unit
, file
) == EOF
) {
2325 error(_("failed to write to '%s'"), filename
);
2329 if (fclose(file
) == EOF
) {
2330 error_errno(_("failed to flush '%s'"), filename
);
2335 filename
= xdg_config_home_systemd("git-maintenance@.service");
2336 file
= fopen_or_warn(filename
, "w");
2340 unit
= "# This file was created and is maintained by Git.\n"
2341 "# Any edits made in this file might be replaced in the future\n"
2342 "# by a Git command.\n"
2345 "Description=Optimize Git repositories data\n"
2349 "ExecStart=\"%s/git\" --exec-path=\"%s\" for-each-repo --config=maintenance.repo maintenance run --schedule=%%i\n"
2350 "LockPersonality=yes\n"
2351 "MemoryDenyWriteExecute=yes\n"
2352 "NoNewPrivileges=yes\n"
2353 "RestrictAddressFamilies=AF_UNIX AF_INET AF_INET6\n"
2354 "RestrictNamespaces=yes\n"
2355 "RestrictRealtime=yes\n"
2356 "RestrictSUIDSGID=yes\n"
2357 "SystemCallArchitectures=native\n"
2358 "SystemCallFilter=@system-service\n";
2359 if (fprintf(file
, unit
, exec_path
, exec_path
) < 0) {
2360 error(_("failed to write to '%s'"), filename
);
2364 if (fclose(file
) == EOF
) {
2365 error_errno(_("failed to flush '%s'"), filename
);
2373 systemd_timer_delete_unit_templates();
2377 static int systemd_timer_setup_units(void)
2379 const char *exec_path
= git_exec_path();
2381 int ret
= systemd_timer_write_unit_templates(exec_path
) ||
2382 systemd_timer_enable_unit(1, SCHEDULE_HOURLY
) ||
2383 systemd_timer_enable_unit(1, SCHEDULE_DAILY
) ||
2384 systemd_timer_enable_unit(1, SCHEDULE_WEEKLY
);
2386 systemd_timer_delete_units();
2390 static int systemd_timer_update_schedule(int run_maintenance
, int fd
)
2392 if (run_maintenance
)
2393 return systemd_timer_setup_units();
2395 return systemd_timer_delete_units();
2399 SCHEDULER_INVALID
= -1,
2403 SCHEDULER_LAUNCHCTL
,
2407 static const struct {
2409 int (*is_available
)(void);
2410 int (*update_schedule
)(int run_maintenance
, int fd
);
2411 } scheduler_fn
[] = {
2412 [SCHEDULER_CRON
] = {
2414 .is_available
= is_crontab_available
,
2415 .update_schedule
= crontab_update_schedule
,
2417 [SCHEDULER_SYSTEMD
] = {
2418 .name
= "systemctl",
2419 .is_available
= is_systemd_timer_available
,
2420 .update_schedule
= systemd_timer_update_schedule
,
2422 [SCHEDULER_LAUNCHCTL
] = {
2423 .name
= "launchctl",
2424 .is_available
= is_launchctl_available
,
2425 .update_schedule
= launchctl_update_schedule
,
2427 [SCHEDULER_SCHTASKS
] = {
2429 .is_available
= is_schtasks_available
,
2430 .update_schedule
= schtasks_update_schedule
,
2434 static enum scheduler
parse_scheduler(const char *value
)
2437 return SCHEDULER_INVALID
;
2438 else if (!strcasecmp(value
, "auto"))
2439 return SCHEDULER_AUTO
;
2440 else if (!strcasecmp(value
, "cron") || !strcasecmp(value
, "crontab"))
2441 return SCHEDULER_CRON
;
2442 else if (!strcasecmp(value
, "systemd") ||
2443 !strcasecmp(value
, "systemd-timer"))
2444 return SCHEDULER_SYSTEMD
;
2445 else if (!strcasecmp(value
, "launchctl"))
2446 return SCHEDULER_LAUNCHCTL
;
2447 else if (!strcasecmp(value
, "schtasks"))
2448 return SCHEDULER_SCHTASKS
;
2450 return SCHEDULER_INVALID
;
2453 static int maintenance_opt_scheduler(const struct option
*opt
, const char *arg
,
2456 enum scheduler
*scheduler
= opt
->value
;
2458 BUG_ON_OPT_NEG(unset
);
2460 *scheduler
= parse_scheduler(arg
);
2461 if (*scheduler
== SCHEDULER_INVALID
)
2462 return error(_("unrecognized --scheduler argument '%s'"), arg
);
2466 struct maintenance_start_opts
{
2467 enum scheduler scheduler
;
2470 static enum scheduler
resolve_scheduler(enum scheduler scheduler
)
2472 if (scheduler
!= SCHEDULER_AUTO
)
2475 #if defined(__APPLE__)
2476 return SCHEDULER_LAUNCHCTL
;
2478 #elif defined(GIT_WINDOWS_NATIVE)
2479 return SCHEDULER_SCHTASKS
;
2481 #elif defined(__linux__)
2482 if (is_systemd_timer_available())
2483 return SCHEDULER_SYSTEMD
;
2484 else if (is_crontab_available())
2485 return SCHEDULER_CRON
;
2487 die(_("neither systemd timers nor crontab are available"));
2490 return SCHEDULER_CRON
;
2494 static void validate_scheduler(enum scheduler scheduler
)
2496 if (scheduler
== SCHEDULER_INVALID
)
2497 BUG("invalid scheduler");
2498 if (scheduler
== SCHEDULER_AUTO
)
2499 BUG("resolve_scheduler should have been called before");
2501 if (!scheduler_fn
[scheduler
].is_available())
2502 die(_("%s scheduler is not available"),
2503 scheduler_fn
[scheduler
].name
);
2506 static int update_background_schedule(const struct maintenance_start_opts
*opts
,
2511 struct lock_file lk
;
2512 char *lock_path
= xstrfmt("%s/schedule", the_repository
->objects
->odb
->path
);
2514 if (hold_lock_file_for_update(&lk
, lock_path
, LOCK_NO_DEREF
) < 0) {
2516 return error(_("another process is scheduling background maintenance"));
2519 for (i
= 1; i
< ARRAY_SIZE(scheduler_fn
); i
++) {
2520 if (enable
&& opts
->scheduler
== i
)
2522 if (!scheduler_fn
[i
].is_available())
2524 scheduler_fn
[i
].update_schedule(0, get_lock_file_fd(&lk
));
2528 result
= scheduler_fn
[opts
->scheduler
].update_schedule(
2529 1, get_lock_file_fd(&lk
));
2531 rollback_lock_file(&lk
);
2537 static const char *const builtin_maintenance_start_usage
[] = {
2538 N_("git maintenance start [--scheduler=<scheduler>]"),
2542 static int maintenance_start(int argc
, const char **argv
, const char *prefix
)
2544 struct maintenance_start_opts opts
= { 0 };
2545 struct option options
[] = {
2547 0, "scheduler", &opts
.scheduler
, N_("scheduler"),
2548 N_("scheduler to trigger git maintenance run"),
2549 PARSE_OPT_NONEG
, maintenance_opt_scheduler
),
2552 const char *register_args
[] = { "register", NULL
};
2554 argc
= parse_options(argc
, argv
, prefix
, options
,
2555 builtin_maintenance_start_usage
, 0);
2557 usage_with_options(builtin_maintenance_start_usage
, options
);
2559 opts
.scheduler
= resolve_scheduler(opts
.scheduler
);
2560 validate_scheduler(opts
.scheduler
);
2562 if (maintenance_register(ARRAY_SIZE(register_args
)-1, register_args
, NULL
))
2563 warning(_("failed to add repo to global config"));
2564 return update_background_schedule(&opts
, 1);
2567 static const char *const builtin_maintenance_stop_usage
[] = {
2568 "git maintenance stop",
2572 static int maintenance_stop(int argc
, const char **argv
, const char *prefix
)
2574 struct option options
[] = {
2577 argc
= parse_options(argc
, argv
, prefix
, options
,
2578 builtin_maintenance_stop_usage
, 0);
2580 usage_with_options(builtin_maintenance_stop_usage
, options
);
2581 return update_background_schedule(NULL
, 0);
2584 static const char * const builtin_maintenance_usage
[] = {
2585 N_("git maintenance <subcommand> [<options>]"),
2589 int cmd_maintenance(int argc
, const char **argv
, const char *prefix
)
2591 parse_opt_subcommand_fn
*fn
= NULL
;
2592 struct option builtin_maintenance_options
[] = {
2593 OPT_SUBCOMMAND("run", &fn
, maintenance_run
),
2594 OPT_SUBCOMMAND("start", &fn
, maintenance_start
),
2595 OPT_SUBCOMMAND("stop", &fn
, maintenance_stop
),
2596 OPT_SUBCOMMAND("register", &fn
, maintenance_register
),
2597 OPT_SUBCOMMAND("unregister", &fn
, maintenance_unregister
),
2601 argc
= parse_options(argc
, argv
, prefix
, builtin_maintenance_options
,
2602 builtin_maintenance_usage
, 0);
2603 return fn(argc
, argv
, prefix
);