]> git.ipfire.org Git - thirdparty/git.git/blob - builtin/gc.c
Merge branch 'en/fetch-negotiation-default-fix'
[thirdparty/git.git] / builtin / gc.c
1 /*
2 * git gc builtin command
3 *
4 * Cleanup unreachable files and optimize the repository.
5 *
6 * Copyright (c) 2007 James Bowes
7 *
8 * Based on git-gc.sh, which is
9 *
10 * Copyright (c) 2006 Shawn O. Pearce
11 */
12
13 #include "builtin.h"
14 #include "repository.h"
15 #include "config.h"
16 #include "tempfile.h"
17 #include "lockfile.h"
18 #include "parse-options.h"
19 #include "run-command.h"
20 #include "sigchain.h"
21 #include "strvec.h"
22 #include "commit.h"
23 #include "commit-graph.h"
24 #include "packfile.h"
25 #include "object-store.h"
26 #include "pack.h"
27 #include "pack-objects.h"
28 #include "blob.h"
29 #include "tree.h"
30 #include "promisor-remote.h"
31 #include "refs.h"
32 #include "remote.h"
33 #include "object-store.h"
34 #include "exec-cmd.h"
35 #include "hook.h"
36
37 #define FAILED_RUN "failed to run %s"
38
39 static const char * const builtin_gc_usage[] = {
40 N_("git gc [<options>]"),
41 NULL
42 };
43
44 static int pack_refs = 1;
45 static int prune_reflogs = 1;
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;
57
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;
63
64 static struct tempfile *pidfile;
65 static struct lock_file log_lock;
66
67 static struct string_list pack_garbage = STRING_LIST_INIT_DUP;
68
69 static void clean_pack_garbage(void)
70 {
71 int i;
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);
75 }
76
77 static void report_pack_garbage(unsigned seen_bits, const char *path)
78 {
79 if (seen_bits == PACKDIR_FILE_IDX)
80 string_list_append(&pack_garbage, path);
81 }
82
83 static void process_log_file(void)
84 {
85 struct stat st;
86 if (fstat(get_lock_file_fd(&log_lock), &st)) {
87 /*
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
91 * messages.
92 */
93 int saved_errno = errno;
94 fprintf(stderr, _("Failed to fstat %s: %s"),
95 get_lock_file_path(&log_lock),
96 strerror(saved_errno));
97 fflush(stderr);
98 commit_lock_file(&log_lock);
99 errno = saved_errno;
100 } else if (st.st_size) {
101 /* There was some error recorded in the lock file */
102 commit_lock_file(&log_lock);
103 } else {
104 /* No error, clean up any old gc.log */
105 unlink(git_path("gc.log"));
106 rollback_lock_file(&log_lock);
107 }
108 }
109
110 static void process_log_file_at_exit(void)
111 {
112 fflush(stderr);
113 process_log_file();
114 }
115
116 static void process_log_file_on_signal(int signo)
117 {
118 process_log_file();
119 sigchain_pop(signo);
120 raise(signo);
121 }
122
123 static int gc_config_is_timestamp_never(const char *var)
124 {
125 const char *value;
126 timestamp_t expire;
127
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);
131 return expire == 0;
132 }
133 return 0;
134 }
135
136 static void gc_config(void)
137 {
138 const char *value;
139
140 if (!git_config_get_value("gc.packrefs", &value)) {
141 if (value && !strcmp(value, "notbare"))
142 pack_refs = -1;
143 else
144 pack_refs = git_config_bool("gc.packrefs", value);
145 }
146
147 if (gc_config_is_timestamp_never("gc.reflogexpire") &&
148 gc_config_is_timestamp_never("gc.reflogexpireunreachable"))
149 prune_reflogs = 0;
150
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_expiry("gc.pruneexpire", &prune_expire);
157 git_config_get_expiry("gc.worktreepruneexpire", &prune_worktrees_expire);
158 git_config_get_expiry("gc.logexpiry", &gc_log_expire);
159
160 git_config_get_ulong("gc.bigpackthreshold", &big_pack_threshold);
161 git_config_get_ulong("pack.deltacachesize", &max_delta_cache_size);
162
163 git_config(git_default_config, NULL);
164 }
165
166 struct maintenance_run_opts;
167 static int maintenance_task_pack_refs(MAYBE_UNUSED struct maintenance_run_opts *opts)
168 {
169 struct strvec pack_refs_cmd = STRVEC_INIT;
170 strvec_pushl(&pack_refs_cmd, "pack-refs", "--all", "--prune", NULL);
171
172 return run_command_v_opt(pack_refs_cmd.v, RUN_GIT_CMD);
173 }
174
175 static int too_many_loose_objects(void)
176 {
177 /*
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
181 * estimate.
182 */
183 DIR *dir;
184 struct dirent *ent;
185 int auto_threshold;
186 int num_loose = 0;
187 int needed = 0;
188 const unsigned hexsz_loose = the_hash_algo->hexsz - 2;
189
190 dir = opendir(git_path("objects/17"));
191 if (!dir)
192 return 0;
193
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')
198 continue;
199 if (++num_loose > auto_threshold) {
200 needed = 1;
201 break;
202 }
203 }
204 closedir(dir);
205 return needed;
206 }
207
208 static struct packed_git *find_base_packs(struct string_list *packs,
209 unsigned long limit)
210 {
211 struct packed_git *p, *base = NULL;
212
213 for (p = get_all_packs(the_repository); p; p = p->next) {
214 if (!p->pack_local)
215 continue;
216 if (limit) {
217 if (p->pack_size >= limit)
218 string_list_append(packs, p->pack_name);
219 } else if (!base || base->pack_size < p->pack_size) {
220 base = p;
221 }
222 }
223
224 if (base)
225 string_list_append(packs, base->pack_name);
226
227 return base;
228 }
229
230 static int too_many_packs(void)
231 {
232 struct packed_git *p;
233 int cnt;
234
235 if (gc_auto_pack_limit <= 0)
236 return 0;
237
238 for (cnt = 0, p = get_all_packs(the_repository); p; p = p->next) {
239 if (!p->pack_local)
240 continue;
241 if (p->pack_keep)
242 continue;
243 /*
244 * Perhaps check the size of the pack and count only
245 * very small ones here?
246 */
247 cnt++;
248 }
249 return gc_auto_pack_limit < cnt;
250 }
251
252 static uint64_t total_ram(void)
253 {
254 #if defined(HAVE_SYSINFO)
255 struct sysinfo si;
256
257 if (!sysinfo(&si))
258 return si.totalram;
259 #elif defined(HAVE_BSD_SYSCTL) && (defined(HW_MEMSIZE) || defined(HW_PHYSMEM))
260 int64_t physical_memory;
261 int mib[2];
262 size_t length;
263
264 mib[0] = CTL_HW;
265 # if defined(HW_MEMSIZE)
266 mib[1] = HW_MEMSIZE;
267 # else
268 mib[1] = HW_PHYSMEM;
269 # endif
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;
275
276 memInfo.dwLength = sizeof(MEMORYSTATUSEX);
277 if (GlobalMemoryStatusEx(&memInfo))
278 return memInfo.ullTotalPhys;
279 #endif
280 return 0;
281 }
282
283 static uint64_t estimate_repack_memory(struct packed_git *pack)
284 {
285 unsigned long nr_objects = approximate_object_count();
286 size_t os_cache, heap;
287
288 if (!pack || !nr_objects)
289 return 0;
290
291 /*
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
295 * the cache.
296 */
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;
300 /*
301 * internal rev-list --all --objects takes up some memory too,
302 * let's say half of it is for blobs
303 */
304 heap += sizeof(struct blob) * nr_objects / 2;
305 /*
306 * and the other half is for trees (commits and tags are
307 * usually insignificant)
308 */
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;
314 /*
315 * read_sha1_file() (either at delta calculation phase, or
316 * writing phase) also fills up the delta base cache
317 */
318 heap += delta_base_cache_limit;
319 /* and of course pack-objects has its own delta cache */
320 heap += max_delta_cache_size;
321
322 return os_cache + heap;
323 }
324
325 static int keep_one_pack(struct string_list_item *item, void *data)
326 {
327 strvec_pushf(&repack, "--keep-pack=%s", basename(item->string));
328 return 0;
329 }
330
331 static void add_repack_all_option(struct string_list *keep_pack)
332 {
333 if (prune_expire && !strcmp(prune_expire, "now"))
334 strvec_push(&repack, "-a");
335 else {
336 strvec_push(&repack, "-A");
337 if (prune_expire)
338 strvec_pushf(&repack, "--unpack-unreachable=%s", prune_expire);
339 }
340
341 if (keep_pack)
342 for_each_string_list(keep_pack, keep_one_pack, NULL);
343 }
344
345 static void add_repack_incremental_option(void)
346 {
347 strvec_push(&repack, "--no-write-bitmap-index");
348 }
349
350 static int need_to_gc(void)
351 {
352 /*
353 * Setting gc.auto to 0 or negative can disable the
354 * automatic gc.
355 */
356 if (gc_auto_threshold <= 0)
357 return 0;
358
359 /*
360 * If there are too many loose objects, but not too many
361 * packs, we run "repack -d -l". If there are too many packs,
362 * we run "repack -A -d -l". Otherwise we tell the caller
363 * there is no need.
364 */
365 if (too_many_packs()) {
366 struct string_list keep_pack = STRING_LIST_INIT_NODUP;
367
368 if (big_pack_threshold) {
369 find_base_packs(&keep_pack, big_pack_threshold);
370 if (keep_pack.nr >= gc_auto_pack_limit) {
371 big_pack_threshold = 0;
372 string_list_clear(&keep_pack, 0);
373 find_base_packs(&keep_pack, 0);
374 }
375 } else {
376 struct packed_git *p = find_base_packs(&keep_pack, 0);
377 uint64_t mem_have, mem_want;
378
379 mem_have = total_ram();
380 mem_want = estimate_repack_memory(p);
381
382 /*
383 * Only allow 1/2 of memory for pack-objects, leave
384 * the rest for the OS and other processes in the
385 * system.
386 */
387 if (!mem_have || mem_want < mem_have / 2)
388 string_list_clear(&keep_pack, 0);
389 }
390
391 add_repack_all_option(&keep_pack);
392 string_list_clear(&keep_pack, 0);
393 } else if (too_many_loose_objects())
394 add_repack_incremental_option();
395 else
396 return 0;
397
398 if (run_hooks("pre-auto-gc"))
399 return 0;
400 return 1;
401 }
402
403 /* return NULL on success, else hostname running the gc */
404 static const char *lock_repo_for_gc(int force, pid_t* ret_pid)
405 {
406 struct lock_file lock = LOCK_INIT;
407 char my_host[HOST_NAME_MAX + 1];
408 struct strbuf sb = STRBUF_INIT;
409 struct stat st;
410 uintmax_t pid;
411 FILE *fp;
412 int fd;
413 char *pidfile_path;
414
415 if (is_tempfile_active(pidfile))
416 /* already locked */
417 return NULL;
418
419 if (xgethostname(my_host, sizeof(my_host)))
420 xsnprintf(my_host, sizeof(my_host), "unknown");
421
422 pidfile_path = git_pathdup("gc.pid");
423 fd = hold_lock_file_for_update(&lock, pidfile_path,
424 LOCK_DIE_ON_ERROR);
425 if (!force) {
426 static char locking_host[HOST_NAME_MAX + 1];
427 static char *scan_fmt;
428 int should_exit;
429
430 if (!scan_fmt)
431 scan_fmt = xstrfmt("%s %%%ds", "%"SCNuMAX, HOST_NAME_MAX);
432 fp = fopen(pidfile_path, "r");
433 memset(locking_host, 0, sizeof(locking_host));
434 should_exit =
435 fp != NULL &&
436 !fstat(fileno(fp), &st) &&
437 /*
438 * 12 hour limit is very generous as gc should
439 * never take that long. On the other hand we
440 * don't really need a strict limit here,
441 * running gc --auto one day late is not a big
442 * problem. --force can be used in manual gc
443 * after the user verifies that no gc is
444 * running.
445 */
446 time(NULL) - st.st_mtime <= 12 * 3600 &&
447 fscanf(fp, scan_fmt, &pid, locking_host) == 2 &&
448 /* be gentle to concurrent "gc" on remote hosts */
449 (strcmp(locking_host, my_host) || !kill(pid, 0) || errno == EPERM);
450 if (fp != NULL)
451 fclose(fp);
452 if (should_exit) {
453 if (fd >= 0)
454 rollback_lock_file(&lock);
455 *ret_pid = pid;
456 free(pidfile_path);
457 return locking_host;
458 }
459 }
460
461 strbuf_addf(&sb, "%"PRIuMAX" %s",
462 (uintmax_t) getpid(), my_host);
463 write_in_full(fd, sb.buf, sb.len);
464 strbuf_release(&sb);
465 commit_lock_file(&lock);
466 pidfile = register_tempfile(pidfile_path);
467 free(pidfile_path);
468 return NULL;
469 }
470
471 /*
472 * Returns 0 if there was no previous error and gc can proceed, 1 if
473 * gc should not proceed due to an error in the last run. Prints a
474 * message and returns with a non-[01] status code if an error occurred
475 * while reading gc.log
476 */
477 static int report_last_gc_error(void)
478 {
479 struct strbuf sb = STRBUF_INIT;
480 int ret = 0;
481 ssize_t len;
482 struct stat st;
483 char *gc_log_path = git_pathdup("gc.log");
484
485 if (stat(gc_log_path, &st)) {
486 if (errno == ENOENT)
487 goto done;
488
489 ret = die_message_errno(_("cannot stat '%s'"), gc_log_path);
490 goto done;
491 }
492
493 if (st.st_mtime < gc_log_expire_time)
494 goto done;
495
496 len = strbuf_read_file(&sb, gc_log_path, 0);
497 if (len < 0)
498 ret = die_message_errno(_("cannot read '%s'"), gc_log_path);
499 else if (len > 0) {
500 /*
501 * A previous gc failed. Report the error, and don't
502 * bother with an automatic gc run since it is likely
503 * to fail in the same way.
504 */
505 warning(_("The last gc run reported the following. "
506 "Please correct the root cause\n"
507 "and remove %s\n"
508 "Automatic cleanup will not be performed "
509 "until the file is removed.\n\n"
510 "%s"),
511 gc_log_path, sb.buf);
512 ret = 1;
513 }
514 strbuf_release(&sb);
515 done:
516 free(gc_log_path);
517 return ret;
518 }
519
520 static void gc_before_repack(void)
521 {
522 /*
523 * We may be called twice, as both the pre- and
524 * post-daemonized phases will call us, but running these
525 * commands more than once is pointless and wasteful.
526 */
527 static int done = 0;
528 if (done++)
529 return;
530
531 if (pack_refs && maintenance_task_pack_refs(NULL))
532 die(FAILED_RUN, "pack-refs");
533
534 if (prune_reflogs && run_command_v_opt(reflog.v, RUN_GIT_CMD))
535 die(FAILED_RUN, reflog.v[0]);
536 }
537
538 int cmd_gc(int argc, const char **argv, const char *prefix)
539 {
540 int aggressive = 0;
541 int auto_gc = 0;
542 int quiet = 0;
543 int force = 0;
544 const char *name;
545 pid_t pid;
546 int daemonized = 0;
547 int keep_largest_pack = -1;
548 timestamp_t dummy;
549
550 struct option builtin_gc_options[] = {
551 OPT__QUIET(&quiet, N_("suppress progress reporting")),
552 { OPTION_STRING, 0, "prune", &prune_expire, N_("date"),
553 N_("prune unreferenced objects"),
554 PARSE_OPT_OPTARG, NULL, (intptr_t)prune_expire },
555 OPT_BOOL(0, "aggressive", &aggressive, N_("be more thorough (increased runtime)")),
556 OPT_BOOL_F(0, "auto", &auto_gc, N_("enable auto-gc mode"),
557 PARSE_OPT_NOCOMPLETE),
558 OPT_BOOL_F(0, "force", &force,
559 N_("force running gc even if there may be another gc running"),
560 PARSE_OPT_NOCOMPLETE),
561 OPT_BOOL(0, "keep-largest-pack", &keep_largest_pack,
562 N_("repack all other packs except the largest pack")),
563 OPT_END()
564 };
565
566 if (argc == 2 && !strcmp(argv[1], "-h"))
567 usage_with_options(builtin_gc_usage, builtin_gc_options);
568
569 strvec_pushl(&reflog, "reflog", "expire", "--all", NULL);
570 strvec_pushl(&repack, "repack", "-d", "-l", NULL);
571 strvec_pushl(&prune, "prune", "--expire", NULL);
572 strvec_pushl(&prune_worktrees, "worktree", "prune", "--expire", NULL);
573 strvec_pushl(&rerere, "rerere", "gc", NULL);
574
575 /* default expiry time, overwritten in gc_config */
576 gc_config();
577 if (parse_expiry_date(gc_log_expire, &gc_log_expire_time))
578 die(_("failed to parse gc.logexpiry value %s"), gc_log_expire);
579
580 if (pack_refs < 0)
581 pack_refs = !is_bare_repository();
582
583 argc = parse_options(argc, argv, prefix, builtin_gc_options,
584 builtin_gc_usage, 0);
585 if (argc > 0)
586 usage_with_options(builtin_gc_usage, builtin_gc_options);
587
588 if (prune_expire && parse_expiry_date(prune_expire, &dummy))
589 die(_("failed to parse prune expiry value %s"), prune_expire);
590
591 if (aggressive) {
592 strvec_push(&repack, "-f");
593 if (aggressive_depth > 0)
594 strvec_pushf(&repack, "--depth=%d", aggressive_depth);
595 if (aggressive_window > 0)
596 strvec_pushf(&repack, "--window=%d", aggressive_window);
597 }
598 if (quiet)
599 strvec_push(&repack, "-q");
600
601 if (auto_gc) {
602 /*
603 * Auto-gc should be least intrusive as possible.
604 */
605 if (!need_to_gc())
606 return 0;
607 if (!quiet) {
608 if (detach_auto)
609 fprintf(stderr, _("Auto packing the repository in background for optimum performance.\n"));
610 else
611 fprintf(stderr, _("Auto packing the repository for optimum performance.\n"));
612 fprintf(stderr, _("See \"git help gc\" for manual housekeeping.\n"));
613 }
614 if (detach_auto) {
615 int ret = report_last_gc_error();
616
617 if (ret == 1)
618 /* Last gc --auto failed. Skip this one. */
619 return 0;
620 else if (ret)
621 /* an I/O error occurred, already reported */
622 return ret;
623
624 if (lock_repo_for_gc(force, &pid))
625 return 0;
626 gc_before_repack(); /* dies on failure */
627 delete_tempfile(&pidfile);
628
629 /*
630 * failure to daemonize is ok, we'll continue
631 * in foreground
632 */
633 daemonized = !daemonize();
634 }
635 } else {
636 struct string_list keep_pack = STRING_LIST_INIT_NODUP;
637
638 if (keep_largest_pack != -1) {
639 if (keep_largest_pack)
640 find_base_packs(&keep_pack, 0);
641 } else if (big_pack_threshold) {
642 find_base_packs(&keep_pack, big_pack_threshold);
643 }
644
645 add_repack_all_option(&keep_pack);
646 string_list_clear(&keep_pack, 0);
647 }
648
649 name = lock_repo_for_gc(force, &pid);
650 if (name) {
651 if (auto_gc)
652 return 0; /* be quiet on --auto */
653 die(_("gc is already running on machine '%s' pid %"PRIuMAX" (use --force if not)"),
654 name, (uintmax_t)pid);
655 }
656
657 if (daemonized) {
658 hold_lock_file_for_update(&log_lock,
659 git_path("gc.log"),
660 LOCK_DIE_ON_ERROR);
661 dup2(get_lock_file_fd(&log_lock), 2);
662 sigchain_push_common(process_log_file_on_signal);
663 atexit(process_log_file_at_exit);
664 }
665
666 gc_before_repack();
667
668 if (!repository_format_precious_objects) {
669 if (run_command_v_opt(repack.v,
670 RUN_GIT_CMD | RUN_CLOSE_OBJECT_STORE))
671 die(FAILED_RUN, repack.v[0]);
672
673 if (prune_expire) {
674 strvec_push(&prune, prune_expire);
675 if (quiet)
676 strvec_push(&prune, "--no-progress");
677 if (has_promisor_remote())
678 strvec_push(&prune,
679 "--exclude-promisor-objects");
680 if (run_command_v_opt(prune.v, RUN_GIT_CMD))
681 die(FAILED_RUN, prune.v[0]);
682 }
683 }
684
685 if (prune_worktrees_expire) {
686 strvec_push(&prune_worktrees, prune_worktrees_expire);
687 if (run_command_v_opt(prune_worktrees.v, RUN_GIT_CMD))
688 die(FAILED_RUN, prune_worktrees.v[0]);
689 }
690
691 if (run_command_v_opt(rerere.v, RUN_GIT_CMD))
692 die(FAILED_RUN, rerere.v[0]);
693
694 report_garbage = report_pack_garbage;
695 reprepare_packed_git(the_repository);
696 if (pack_garbage.nr > 0) {
697 close_object_store(the_repository->objects);
698 clean_pack_garbage();
699 }
700
701 prepare_repo_settings(the_repository);
702 if (the_repository->settings.gc_write_commit_graph == 1)
703 write_commit_graph_reachable(the_repository->objects->odb,
704 !quiet && !daemonized ? COMMIT_GRAPH_WRITE_PROGRESS : 0,
705 NULL);
706
707 if (auto_gc && too_many_loose_objects())
708 warning(_("There are too many unreachable loose objects; "
709 "run 'git prune' to remove them."));
710
711 if (!daemonized)
712 unlink(git_path("gc.log"));
713
714 return 0;
715 }
716
717 static const char *const builtin_maintenance_run_usage[] = {
718 N_("git maintenance run [--auto] [--[no-]quiet] [--task=<task>] [--schedule]"),
719 NULL
720 };
721
722 enum schedule_priority {
723 SCHEDULE_NONE = 0,
724 SCHEDULE_WEEKLY = 1,
725 SCHEDULE_DAILY = 2,
726 SCHEDULE_HOURLY = 3,
727 };
728
729 static enum schedule_priority parse_schedule(const char *value)
730 {
731 if (!value)
732 return SCHEDULE_NONE;
733 if (!strcasecmp(value, "hourly"))
734 return SCHEDULE_HOURLY;
735 if (!strcasecmp(value, "daily"))
736 return SCHEDULE_DAILY;
737 if (!strcasecmp(value, "weekly"))
738 return SCHEDULE_WEEKLY;
739 return SCHEDULE_NONE;
740 }
741
742 static int maintenance_opt_schedule(const struct option *opt, const char *arg,
743 int unset)
744 {
745 enum schedule_priority *priority = opt->value;
746
747 if (unset)
748 die(_("--no-schedule is not allowed"));
749
750 *priority = parse_schedule(arg);
751
752 if (!*priority)
753 die(_("unrecognized --schedule argument '%s'"), arg);
754
755 return 0;
756 }
757
758 struct maintenance_run_opts {
759 int auto_flag;
760 int quiet;
761 enum schedule_priority schedule;
762 };
763
764 /* Remember to update object flag allocation in object.h */
765 #define SEEN (1u<<0)
766
767 struct cg_auto_data {
768 int num_not_in_graph;
769 int limit;
770 };
771
772 static int dfs_on_ref(const char *refname,
773 const struct object_id *oid, int flags,
774 void *cb_data)
775 {
776 struct cg_auto_data *data = (struct cg_auto_data *)cb_data;
777 int result = 0;
778 struct object_id peeled;
779 struct commit_list *stack = NULL;
780 struct commit *commit;
781
782 if (!peel_iterated_oid(oid, &peeled))
783 oid = &peeled;
784 if (oid_object_info(the_repository, oid, NULL) != OBJ_COMMIT)
785 return 0;
786
787 commit = lookup_commit(the_repository, oid);
788 if (!commit)
789 return 0;
790 if (parse_commit(commit) ||
791 commit_graph_position(commit) != COMMIT_NOT_FROM_GRAPH)
792 return 0;
793
794 data->num_not_in_graph++;
795
796 if (data->num_not_in_graph >= data->limit)
797 return 1;
798
799 commit_list_append(commit, &stack);
800
801 while (!result && stack) {
802 struct commit_list *parent;
803
804 commit = pop_commit(&stack);
805
806 for (parent = commit->parents; parent; parent = parent->next) {
807 if (parse_commit(parent->item) ||
808 commit_graph_position(parent->item) != COMMIT_NOT_FROM_GRAPH ||
809 parent->item->object.flags & SEEN)
810 continue;
811
812 parent->item->object.flags |= SEEN;
813 data->num_not_in_graph++;
814
815 if (data->num_not_in_graph >= data->limit) {
816 result = 1;
817 break;
818 }
819
820 commit_list_append(parent->item, &stack);
821 }
822 }
823
824 free_commit_list(stack);
825 return result;
826 }
827
828 static int should_write_commit_graph(void)
829 {
830 int result;
831 struct cg_auto_data data;
832
833 data.num_not_in_graph = 0;
834 data.limit = 100;
835 git_config_get_int("maintenance.commit-graph.auto",
836 &data.limit);
837
838 if (!data.limit)
839 return 0;
840 if (data.limit < 0)
841 return 1;
842
843 result = for_each_ref(dfs_on_ref, &data);
844
845 repo_clear_commit_marks(the_repository, SEEN);
846
847 return result;
848 }
849
850 static int run_write_commit_graph(struct maintenance_run_opts *opts)
851 {
852 struct child_process child = CHILD_PROCESS_INIT;
853
854 child.git_cmd = child.close_object_store = 1;
855 strvec_pushl(&child.args, "commit-graph", "write",
856 "--split", "--reachable", NULL);
857
858 if (opts->quiet)
859 strvec_push(&child.args, "--no-progress");
860
861 return !!run_command(&child);
862 }
863
864 static int maintenance_task_commit_graph(struct maintenance_run_opts *opts)
865 {
866 prepare_repo_settings(the_repository);
867 if (!the_repository->settings.core_commit_graph)
868 return 0;
869
870 if (run_write_commit_graph(opts)) {
871 error(_("failed to write commit-graph"));
872 return 1;
873 }
874
875 return 0;
876 }
877
878 static int fetch_remote(struct remote *remote, void *cbdata)
879 {
880 struct maintenance_run_opts *opts = cbdata;
881 struct child_process child = CHILD_PROCESS_INIT;
882
883 if (remote->skip_default_update)
884 return 0;
885
886 child.git_cmd = 1;
887 strvec_pushl(&child.args, "fetch", remote->name,
888 "--prefetch", "--prune", "--no-tags",
889 "--no-write-fetch-head", "--recurse-submodules=no",
890 NULL);
891
892 if (opts->quiet)
893 strvec_push(&child.args, "--quiet");
894
895 return !!run_command(&child);
896 }
897
898 static int maintenance_task_prefetch(struct maintenance_run_opts *opts)
899 {
900 git_config_set_multivar_gently("log.excludedecoration",
901 "refs/prefetch/",
902 "refs/prefetch/",
903 CONFIG_FLAGS_FIXED_VALUE |
904 CONFIG_FLAGS_MULTI_REPLACE);
905
906 if (for_each_remote(fetch_remote, opts)) {
907 error(_("failed to prefetch remotes"));
908 return 1;
909 }
910
911 return 0;
912 }
913
914 static int maintenance_task_gc(struct maintenance_run_opts *opts)
915 {
916 struct child_process child = CHILD_PROCESS_INIT;
917
918 child.git_cmd = child.close_object_store = 1;
919 strvec_push(&child.args, "gc");
920
921 if (opts->auto_flag)
922 strvec_push(&child.args, "--auto");
923 if (opts->quiet)
924 strvec_push(&child.args, "--quiet");
925 else
926 strvec_push(&child.args, "--no-quiet");
927
928 return run_command(&child);
929 }
930
931 static int prune_packed(struct maintenance_run_opts *opts)
932 {
933 struct child_process child = CHILD_PROCESS_INIT;
934
935 child.git_cmd = 1;
936 strvec_push(&child.args, "prune-packed");
937
938 if (opts->quiet)
939 strvec_push(&child.args, "--quiet");
940
941 return !!run_command(&child);
942 }
943
944 struct write_loose_object_data {
945 FILE *in;
946 int count;
947 int batch_size;
948 };
949
950 static int loose_object_auto_limit = 100;
951
952 static int loose_object_count(const struct object_id *oid,
953 const char *path,
954 void *data)
955 {
956 int *count = (int*)data;
957 if (++(*count) >= loose_object_auto_limit)
958 return 1;
959 return 0;
960 }
961
962 static int loose_object_auto_condition(void)
963 {
964 int count = 0;
965
966 git_config_get_int("maintenance.loose-objects.auto",
967 &loose_object_auto_limit);
968
969 if (!loose_object_auto_limit)
970 return 0;
971 if (loose_object_auto_limit < 0)
972 return 1;
973
974 return for_each_loose_file_in_objdir(the_repository->objects->odb->path,
975 loose_object_count,
976 NULL, NULL, &count);
977 }
978
979 static int bail_on_loose(const struct object_id *oid,
980 const char *path,
981 void *data)
982 {
983 return 1;
984 }
985
986 static int write_loose_object_to_stdin(const struct object_id *oid,
987 const char *path,
988 void *data)
989 {
990 struct write_loose_object_data *d = (struct write_loose_object_data *)data;
991
992 fprintf(d->in, "%s\n", oid_to_hex(oid));
993
994 return ++(d->count) > d->batch_size;
995 }
996
997 static int pack_loose(struct maintenance_run_opts *opts)
998 {
999 struct repository *r = the_repository;
1000 int result = 0;
1001 struct write_loose_object_data data;
1002 struct child_process pack_proc = CHILD_PROCESS_INIT;
1003
1004 /*
1005 * Do not start pack-objects process
1006 * if there are no loose objects.
1007 */
1008 if (!for_each_loose_file_in_objdir(r->objects->odb->path,
1009 bail_on_loose,
1010 NULL, NULL, NULL))
1011 return 0;
1012
1013 pack_proc.git_cmd = 1;
1014
1015 strvec_push(&pack_proc.args, "pack-objects");
1016 if (opts->quiet)
1017 strvec_push(&pack_proc.args, "--quiet");
1018 strvec_pushf(&pack_proc.args, "%s/pack/loose", r->objects->odb->path);
1019
1020 pack_proc.in = -1;
1021
1022 if (start_command(&pack_proc)) {
1023 error(_("failed to start 'git pack-objects' process"));
1024 return 1;
1025 }
1026
1027 data.in = xfdopen(pack_proc.in, "w");
1028 data.count = 0;
1029 data.batch_size = 50000;
1030
1031 for_each_loose_file_in_objdir(r->objects->odb->path,
1032 write_loose_object_to_stdin,
1033 NULL,
1034 NULL,
1035 &data);
1036
1037 fclose(data.in);
1038
1039 if (finish_command(&pack_proc)) {
1040 error(_("failed to finish 'git pack-objects' process"));
1041 result = 1;
1042 }
1043
1044 return result;
1045 }
1046
1047 static int maintenance_task_loose_objects(struct maintenance_run_opts *opts)
1048 {
1049 return prune_packed(opts) || pack_loose(opts);
1050 }
1051
1052 static int incremental_repack_auto_condition(void)
1053 {
1054 struct packed_git *p;
1055 int incremental_repack_auto_limit = 10;
1056 int count = 0;
1057
1058 prepare_repo_settings(the_repository);
1059 if (!the_repository->settings.core_multi_pack_index)
1060 return 0;
1061
1062 git_config_get_int("maintenance.incremental-repack.auto",
1063 &incremental_repack_auto_limit);
1064
1065 if (!incremental_repack_auto_limit)
1066 return 0;
1067 if (incremental_repack_auto_limit < 0)
1068 return 1;
1069
1070 for (p = get_packed_git(the_repository);
1071 count < incremental_repack_auto_limit && p;
1072 p = p->next) {
1073 if (!p->multi_pack_index)
1074 count++;
1075 }
1076
1077 return count >= incremental_repack_auto_limit;
1078 }
1079
1080 static int multi_pack_index_write(struct maintenance_run_opts *opts)
1081 {
1082 struct child_process child = CHILD_PROCESS_INIT;
1083
1084 child.git_cmd = 1;
1085 strvec_pushl(&child.args, "multi-pack-index", "write", NULL);
1086
1087 if (opts->quiet)
1088 strvec_push(&child.args, "--no-progress");
1089
1090 if (run_command(&child))
1091 return error(_("failed to write multi-pack-index"));
1092
1093 return 0;
1094 }
1095
1096 static int multi_pack_index_expire(struct maintenance_run_opts *opts)
1097 {
1098 struct child_process child = CHILD_PROCESS_INIT;
1099
1100 child.git_cmd = child.close_object_store = 1;
1101 strvec_pushl(&child.args, "multi-pack-index", "expire", NULL);
1102
1103 if (opts->quiet)
1104 strvec_push(&child.args, "--no-progress");
1105
1106 if (run_command(&child))
1107 return error(_("'git multi-pack-index expire' failed"));
1108
1109 return 0;
1110 }
1111
1112 #define TWO_GIGABYTES (INT32_MAX)
1113
1114 static off_t get_auto_pack_size(void)
1115 {
1116 /*
1117 * The "auto" value is special: we optimize for
1118 * one large pack-file (i.e. from a clone) and
1119 * expect the rest to be small and they can be
1120 * repacked quickly.
1121 *
1122 * The strategy we select here is to select a
1123 * size that is one more than the second largest
1124 * pack-file. This ensures that we will repack
1125 * at least two packs if there are three or more
1126 * packs.
1127 */
1128 off_t max_size = 0;
1129 off_t second_largest_size = 0;
1130 off_t result_size;
1131 struct packed_git *p;
1132 struct repository *r = the_repository;
1133
1134 reprepare_packed_git(r);
1135 for (p = get_all_packs(r); p; p = p->next) {
1136 if (p->pack_size > max_size) {
1137 second_largest_size = max_size;
1138 max_size = p->pack_size;
1139 } else if (p->pack_size > second_largest_size)
1140 second_largest_size = p->pack_size;
1141 }
1142
1143 result_size = second_largest_size + 1;
1144
1145 /* But limit ourselves to a batch size of 2g */
1146 if (result_size > TWO_GIGABYTES)
1147 result_size = TWO_GIGABYTES;
1148
1149 return result_size;
1150 }
1151
1152 static int multi_pack_index_repack(struct maintenance_run_opts *opts)
1153 {
1154 struct child_process child = CHILD_PROCESS_INIT;
1155
1156 child.git_cmd = child.close_object_store = 1;
1157 strvec_pushl(&child.args, "multi-pack-index", "repack", NULL);
1158
1159 if (opts->quiet)
1160 strvec_push(&child.args, "--no-progress");
1161
1162 strvec_pushf(&child.args, "--batch-size=%"PRIuMAX,
1163 (uintmax_t)get_auto_pack_size());
1164
1165 if (run_command(&child))
1166 return error(_("'git multi-pack-index repack' failed"));
1167
1168 return 0;
1169 }
1170
1171 static int maintenance_task_incremental_repack(struct maintenance_run_opts *opts)
1172 {
1173 prepare_repo_settings(the_repository);
1174 if (!the_repository->settings.core_multi_pack_index) {
1175 warning(_("skipping incremental-repack task because core.multiPackIndex is disabled"));
1176 return 0;
1177 }
1178
1179 if (multi_pack_index_write(opts))
1180 return 1;
1181 if (multi_pack_index_expire(opts))
1182 return 1;
1183 if (multi_pack_index_repack(opts))
1184 return 1;
1185 return 0;
1186 }
1187
1188 typedef int maintenance_task_fn(struct maintenance_run_opts *opts);
1189
1190 /*
1191 * An auto condition function returns 1 if the task should run
1192 * and 0 if the task should NOT run. See needs_to_gc() for an
1193 * example.
1194 */
1195 typedef int maintenance_auto_fn(void);
1196
1197 struct maintenance_task {
1198 const char *name;
1199 maintenance_task_fn *fn;
1200 maintenance_auto_fn *auto_condition;
1201 unsigned enabled:1;
1202
1203 enum schedule_priority schedule;
1204
1205 /* -1 if not selected. */
1206 int selected_order;
1207 };
1208
1209 enum maintenance_task_label {
1210 TASK_PREFETCH,
1211 TASK_LOOSE_OBJECTS,
1212 TASK_INCREMENTAL_REPACK,
1213 TASK_GC,
1214 TASK_COMMIT_GRAPH,
1215 TASK_PACK_REFS,
1216
1217 /* Leave as final value */
1218 TASK__COUNT
1219 };
1220
1221 static struct maintenance_task tasks[] = {
1222 [TASK_PREFETCH] = {
1223 "prefetch",
1224 maintenance_task_prefetch,
1225 },
1226 [TASK_LOOSE_OBJECTS] = {
1227 "loose-objects",
1228 maintenance_task_loose_objects,
1229 loose_object_auto_condition,
1230 },
1231 [TASK_INCREMENTAL_REPACK] = {
1232 "incremental-repack",
1233 maintenance_task_incremental_repack,
1234 incremental_repack_auto_condition,
1235 },
1236 [TASK_GC] = {
1237 "gc",
1238 maintenance_task_gc,
1239 need_to_gc,
1240 1,
1241 },
1242 [TASK_COMMIT_GRAPH] = {
1243 "commit-graph",
1244 maintenance_task_commit_graph,
1245 should_write_commit_graph,
1246 },
1247 [TASK_PACK_REFS] = {
1248 "pack-refs",
1249 maintenance_task_pack_refs,
1250 NULL,
1251 },
1252 };
1253
1254 static int compare_tasks_by_selection(const void *a_, const void *b_)
1255 {
1256 const struct maintenance_task *a = a_;
1257 const struct maintenance_task *b = b_;
1258
1259 return b->selected_order - a->selected_order;
1260 }
1261
1262 static int maintenance_run_tasks(struct maintenance_run_opts *opts)
1263 {
1264 int i, found_selected = 0;
1265 int result = 0;
1266 struct lock_file lk;
1267 struct repository *r = the_repository;
1268 char *lock_path = xstrfmt("%s/maintenance", r->objects->odb->path);
1269
1270 if (hold_lock_file_for_update(&lk, lock_path, LOCK_NO_DEREF) < 0) {
1271 /*
1272 * Another maintenance command is running.
1273 *
1274 * If --auto was provided, then it is likely due to a
1275 * recursive process stack. Do not report an error in
1276 * that case.
1277 */
1278 if (!opts->auto_flag && !opts->quiet)
1279 warning(_("lock file '%s' exists, skipping maintenance"),
1280 lock_path);
1281 free(lock_path);
1282 return 0;
1283 }
1284 free(lock_path);
1285
1286 for (i = 0; !found_selected && i < TASK__COUNT; i++)
1287 found_selected = tasks[i].selected_order >= 0;
1288
1289 if (found_selected)
1290 QSORT(tasks, TASK__COUNT, compare_tasks_by_selection);
1291
1292 for (i = 0; i < TASK__COUNT; i++) {
1293 if (found_selected && tasks[i].selected_order < 0)
1294 continue;
1295
1296 if (!found_selected && !tasks[i].enabled)
1297 continue;
1298
1299 if (opts->auto_flag &&
1300 (!tasks[i].auto_condition ||
1301 !tasks[i].auto_condition()))
1302 continue;
1303
1304 if (opts->schedule && tasks[i].schedule < opts->schedule)
1305 continue;
1306
1307 trace2_region_enter("maintenance", tasks[i].name, r);
1308 if (tasks[i].fn(opts)) {
1309 error(_("task '%s' failed"), tasks[i].name);
1310 result = 1;
1311 }
1312 trace2_region_leave("maintenance", tasks[i].name, r);
1313 }
1314
1315 rollback_lock_file(&lk);
1316 return result;
1317 }
1318
1319 static void initialize_maintenance_strategy(void)
1320 {
1321 char *config_str;
1322
1323 if (git_config_get_string("maintenance.strategy", &config_str))
1324 return;
1325
1326 if (!strcasecmp(config_str, "incremental")) {
1327 tasks[TASK_GC].schedule = SCHEDULE_NONE;
1328 tasks[TASK_COMMIT_GRAPH].enabled = 1;
1329 tasks[TASK_COMMIT_GRAPH].schedule = SCHEDULE_HOURLY;
1330 tasks[TASK_PREFETCH].enabled = 1;
1331 tasks[TASK_PREFETCH].schedule = SCHEDULE_HOURLY;
1332 tasks[TASK_INCREMENTAL_REPACK].enabled = 1;
1333 tasks[TASK_INCREMENTAL_REPACK].schedule = SCHEDULE_DAILY;
1334 tasks[TASK_LOOSE_OBJECTS].enabled = 1;
1335 tasks[TASK_LOOSE_OBJECTS].schedule = SCHEDULE_DAILY;
1336 tasks[TASK_PACK_REFS].enabled = 1;
1337 tasks[TASK_PACK_REFS].schedule = SCHEDULE_WEEKLY;
1338 }
1339 }
1340
1341 static void initialize_task_config(int schedule)
1342 {
1343 int i;
1344 struct strbuf config_name = STRBUF_INIT;
1345 gc_config();
1346
1347 if (schedule)
1348 initialize_maintenance_strategy();
1349
1350 for (i = 0; i < TASK__COUNT; i++) {
1351 int config_value;
1352 char *config_str;
1353
1354 strbuf_reset(&config_name);
1355 strbuf_addf(&config_name, "maintenance.%s.enabled",
1356 tasks[i].name);
1357
1358 if (!git_config_get_bool(config_name.buf, &config_value))
1359 tasks[i].enabled = config_value;
1360
1361 strbuf_reset(&config_name);
1362 strbuf_addf(&config_name, "maintenance.%s.schedule",
1363 tasks[i].name);
1364
1365 if (!git_config_get_string(config_name.buf, &config_str)) {
1366 tasks[i].schedule = parse_schedule(config_str);
1367 free(config_str);
1368 }
1369 }
1370
1371 strbuf_release(&config_name);
1372 }
1373
1374 static int task_option_parse(const struct option *opt,
1375 const char *arg, int unset)
1376 {
1377 int i, num_selected = 0;
1378 struct maintenance_task *task = NULL;
1379
1380 BUG_ON_OPT_NEG(unset);
1381
1382 for (i = 0; i < TASK__COUNT; i++) {
1383 if (tasks[i].selected_order >= 0)
1384 num_selected++;
1385 if (!strcasecmp(tasks[i].name, arg)) {
1386 task = &tasks[i];
1387 }
1388 }
1389
1390 if (!task) {
1391 error(_("'%s' is not a valid task"), arg);
1392 return 1;
1393 }
1394
1395 if (task->selected_order >= 0) {
1396 error(_("task '%s' cannot be selected multiple times"), arg);
1397 return 1;
1398 }
1399
1400 task->selected_order = num_selected + 1;
1401
1402 return 0;
1403 }
1404
1405 static int maintenance_run(int argc, const char **argv, const char *prefix)
1406 {
1407 int i;
1408 struct maintenance_run_opts opts;
1409 struct option builtin_maintenance_run_options[] = {
1410 OPT_BOOL(0, "auto", &opts.auto_flag,
1411 N_("run tasks based on the state of the repository")),
1412 OPT_CALLBACK(0, "schedule", &opts.schedule, N_("frequency"),
1413 N_("run tasks based on frequency"),
1414 maintenance_opt_schedule),
1415 OPT_BOOL(0, "quiet", &opts.quiet,
1416 N_("do not report progress or other information over stderr")),
1417 OPT_CALLBACK_F(0, "task", NULL, N_("task"),
1418 N_("run a specific task"),
1419 PARSE_OPT_NONEG, task_option_parse),
1420 OPT_END()
1421 };
1422 memset(&opts, 0, sizeof(opts));
1423
1424 opts.quiet = !isatty(2);
1425
1426 for (i = 0; i < TASK__COUNT; i++)
1427 tasks[i].selected_order = -1;
1428
1429 argc = parse_options(argc, argv, prefix,
1430 builtin_maintenance_run_options,
1431 builtin_maintenance_run_usage,
1432 PARSE_OPT_STOP_AT_NON_OPTION);
1433
1434 if (opts.auto_flag && opts.schedule)
1435 die(_("use at most one of --auto and --schedule=<frequency>"));
1436
1437 initialize_task_config(opts.schedule);
1438
1439 if (argc != 0)
1440 usage_with_options(builtin_maintenance_run_usage,
1441 builtin_maintenance_run_options);
1442 return maintenance_run_tasks(&opts);
1443 }
1444
1445 static char *get_maintpath(void)
1446 {
1447 struct strbuf sb = STRBUF_INIT;
1448 const char *p = the_repository->worktree ?
1449 the_repository->worktree : the_repository->gitdir;
1450
1451 strbuf_realpath(&sb, p, 1);
1452 return strbuf_detach(&sb, NULL);
1453 }
1454
1455 static int maintenance_register(void)
1456 {
1457 int rc;
1458 char *config_value;
1459 struct child_process config_set = CHILD_PROCESS_INIT;
1460 struct child_process config_get = CHILD_PROCESS_INIT;
1461 char *maintpath = get_maintpath();
1462
1463 /* Disable foreground maintenance */
1464 git_config_set("maintenance.auto", "false");
1465
1466 /* Set maintenance strategy, if unset */
1467 if (!git_config_get_string("maintenance.strategy", &config_value))
1468 free(config_value);
1469 else
1470 git_config_set("maintenance.strategy", "incremental");
1471
1472 config_get.git_cmd = 1;
1473 strvec_pushl(&config_get.args, "config", "--global", "--get",
1474 "--fixed-value", "maintenance.repo", maintpath, NULL);
1475 config_get.out = -1;
1476
1477 if (start_command(&config_get)) {
1478 rc = error(_("failed to run 'git config'"));
1479 goto done;
1480 }
1481
1482 /* We already have this value in our config! */
1483 if (!finish_command(&config_get)) {
1484 rc = 0;
1485 goto done;
1486 }
1487
1488 config_set.git_cmd = 1;
1489 strvec_pushl(&config_set.args, "config", "--add", "--global", "maintenance.repo",
1490 maintpath, NULL);
1491
1492 rc = run_command(&config_set);
1493
1494 done:
1495 free(maintpath);
1496 return rc;
1497 }
1498
1499 static int maintenance_unregister(void)
1500 {
1501 int rc;
1502 struct child_process config_unset = CHILD_PROCESS_INIT;
1503 char *maintpath = get_maintpath();
1504
1505 config_unset.git_cmd = 1;
1506 strvec_pushl(&config_unset.args, "config", "--global", "--unset",
1507 "--fixed-value", "maintenance.repo", maintpath, NULL);
1508
1509 rc = run_command(&config_unset);
1510 free(maintpath);
1511 return rc;
1512 }
1513
1514 static const char *get_frequency(enum schedule_priority schedule)
1515 {
1516 switch (schedule) {
1517 case SCHEDULE_HOURLY:
1518 return "hourly";
1519 case SCHEDULE_DAILY:
1520 return "daily";
1521 case SCHEDULE_WEEKLY:
1522 return "weekly";
1523 default:
1524 BUG("invalid schedule %d", schedule);
1525 }
1526 }
1527
1528 /*
1529 * get_schedule_cmd` reads the GIT_TEST_MAINT_SCHEDULER environment variable
1530 * to mock the schedulers that `git maintenance start` rely on.
1531 *
1532 * For test purpose, GIT_TEST_MAINT_SCHEDULER can be set to a comma-separated
1533 * list of colon-separated key/value pairs where each pair contains a scheduler
1534 * and its corresponding mock.
1535 *
1536 * * If $GIT_TEST_MAINT_SCHEDULER is not set, return false and leave the
1537 * arguments unmodified.
1538 *
1539 * * If $GIT_TEST_MAINT_SCHEDULER is set, return true.
1540 * In this case, the *cmd value is read as input.
1541 *
1542 * * if the input value *cmd is the key of one of the comma-separated list
1543 * item, then *is_available is set to true and *cmd is modified and becomes
1544 * the mock command.
1545 *
1546 * * if the input value *cmd isn’t the key of any of the comma-separated list
1547 * item, then *is_available is set to false.
1548 *
1549 * Ex.:
1550 * GIT_TEST_MAINT_SCHEDULER not set
1551 * +-------+-------------------------------------------------+
1552 * | Input | Output |
1553 * | *cmd | return code | *cmd | *is_available |
1554 * +-------+-------------+-------------------+---------------+
1555 * | "foo" | false | "foo" (unchanged) | (unchanged) |
1556 * +-------+-------------+-------------------+---------------+
1557 *
1558 * GIT_TEST_MAINT_SCHEDULER set to “foo:./mock_foo.sh,bar:./mock_bar.sh”
1559 * +-------+-------------------------------------------------+
1560 * | Input | Output |
1561 * | *cmd | return code | *cmd | *is_available |
1562 * +-------+-------------+-------------------+---------------+
1563 * | "foo" | true | "./mock.foo.sh" | true |
1564 * | "qux" | true | "qux" (unchanged) | false |
1565 * +-------+-------------+-------------------+---------------+
1566 */
1567 static int get_schedule_cmd(const char **cmd, int *is_available)
1568 {
1569 char *testing = xstrdup_or_null(getenv("GIT_TEST_MAINT_SCHEDULER"));
1570 struct string_list_item *item;
1571 struct string_list list = STRING_LIST_INIT_NODUP;
1572
1573 if (!testing)
1574 return 0;
1575
1576 if (is_available)
1577 *is_available = 0;
1578
1579 string_list_split_in_place(&list, testing, ',', -1);
1580 for_each_string_list_item(item, &list) {
1581 struct string_list pair = STRING_LIST_INIT_NODUP;
1582
1583 if (string_list_split_in_place(&pair, item->string, ':', 2) != 2)
1584 continue;
1585
1586 if (!strcmp(*cmd, pair.items[0].string)) {
1587 *cmd = pair.items[1].string;
1588 if (is_available)
1589 *is_available = 1;
1590 string_list_clear(&list, 0);
1591 UNLEAK(testing);
1592 return 1;
1593 }
1594 }
1595
1596 string_list_clear(&list, 0);
1597 free(testing);
1598 return 1;
1599 }
1600
1601 static int is_launchctl_available(void)
1602 {
1603 const char *cmd = "launchctl";
1604 int is_available;
1605 if (get_schedule_cmd(&cmd, &is_available))
1606 return is_available;
1607
1608 #ifdef __APPLE__
1609 return 1;
1610 #else
1611 return 0;
1612 #endif
1613 }
1614
1615 static char *launchctl_service_name(const char *frequency)
1616 {
1617 struct strbuf label = STRBUF_INIT;
1618 strbuf_addf(&label, "org.git-scm.git.%s", frequency);
1619 return strbuf_detach(&label, NULL);
1620 }
1621
1622 static char *launchctl_service_filename(const char *name)
1623 {
1624 char *expanded;
1625 struct strbuf filename = STRBUF_INIT;
1626 strbuf_addf(&filename, "~/Library/LaunchAgents/%s.plist", name);
1627
1628 expanded = interpolate_path(filename.buf, 1);
1629 if (!expanded)
1630 die(_("failed to expand path '%s'"), filename.buf);
1631
1632 strbuf_release(&filename);
1633 return expanded;
1634 }
1635
1636 static char *launchctl_get_uid(void)
1637 {
1638 return xstrfmt("gui/%d", getuid());
1639 }
1640
1641 static int launchctl_boot_plist(int enable, const char *filename)
1642 {
1643 const char *cmd = "launchctl";
1644 int result;
1645 struct child_process child = CHILD_PROCESS_INIT;
1646 char *uid = launchctl_get_uid();
1647
1648 get_schedule_cmd(&cmd, NULL);
1649 strvec_split(&child.args, cmd);
1650 strvec_pushl(&child.args, enable ? "bootstrap" : "bootout", uid,
1651 filename, NULL);
1652
1653 child.no_stderr = 1;
1654 child.no_stdout = 1;
1655
1656 if (start_command(&child))
1657 die(_("failed to start launchctl"));
1658
1659 result = finish_command(&child);
1660
1661 free(uid);
1662 return result;
1663 }
1664
1665 static int launchctl_remove_plist(enum schedule_priority schedule)
1666 {
1667 const char *frequency = get_frequency(schedule);
1668 char *name = launchctl_service_name(frequency);
1669 char *filename = launchctl_service_filename(name);
1670 int result = launchctl_boot_plist(0, filename);
1671 unlink(filename);
1672 free(filename);
1673 free(name);
1674 return result;
1675 }
1676
1677 static int launchctl_remove_plists(void)
1678 {
1679 return launchctl_remove_plist(SCHEDULE_HOURLY) ||
1680 launchctl_remove_plist(SCHEDULE_DAILY) ||
1681 launchctl_remove_plist(SCHEDULE_WEEKLY);
1682 }
1683
1684 static int launchctl_list_contains_plist(const char *name, const char *cmd)
1685 {
1686 struct child_process child = CHILD_PROCESS_INIT;
1687
1688 strvec_split(&child.args, cmd);
1689 strvec_pushl(&child.args, "list", name, NULL);
1690
1691 child.no_stderr = 1;
1692 child.no_stdout = 1;
1693
1694 if (start_command(&child))
1695 die(_("failed to start launchctl"));
1696
1697 /* Returns failure if 'name' doesn't exist. */
1698 return !finish_command(&child);
1699 }
1700
1701 static int launchctl_schedule_plist(const char *exec_path, enum schedule_priority schedule)
1702 {
1703 int i, fd;
1704 const char *preamble, *repeat;
1705 const char *frequency = get_frequency(schedule);
1706 char *name = launchctl_service_name(frequency);
1707 char *filename = launchctl_service_filename(name);
1708 struct lock_file lk = LOCK_INIT;
1709 static unsigned long lock_file_timeout_ms = ULONG_MAX;
1710 struct strbuf plist = STRBUF_INIT, plist2 = STRBUF_INIT;
1711 struct stat st;
1712 const char *cmd = "launchctl";
1713
1714 get_schedule_cmd(&cmd, NULL);
1715 preamble = "<?xml version=\"1.0\"?>\n"
1716 "<!DOCTYPE plist PUBLIC \"-//Apple//DTD PLIST 1.0//EN\" \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">\n"
1717 "<plist version=\"1.0\">"
1718 "<dict>\n"
1719 "<key>Label</key><string>%s</string>\n"
1720 "<key>ProgramArguments</key>\n"
1721 "<array>\n"
1722 "<string>%s/git</string>\n"
1723 "<string>--exec-path=%s</string>\n"
1724 "<string>for-each-repo</string>\n"
1725 "<string>--config=maintenance.repo</string>\n"
1726 "<string>maintenance</string>\n"
1727 "<string>run</string>\n"
1728 "<string>--schedule=%s</string>\n"
1729 "</array>\n"
1730 "<key>StartCalendarInterval</key>\n"
1731 "<array>\n";
1732 strbuf_addf(&plist, preamble, name, exec_path, exec_path, frequency);
1733
1734 switch (schedule) {
1735 case SCHEDULE_HOURLY:
1736 repeat = "<dict>\n"
1737 "<key>Hour</key><integer>%d</integer>\n"
1738 "<key>Minute</key><integer>0</integer>\n"
1739 "</dict>\n";
1740 for (i = 1; i <= 23; i++)
1741 strbuf_addf(&plist, repeat, i);
1742 break;
1743
1744 case SCHEDULE_DAILY:
1745 repeat = "<dict>\n"
1746 "<key>Day</key><integer>%d</integer>\n"
1747 "<key>Hour</key><integer>0</integer>\n"
1748 "<key>Minute</key><integer>0</integer>\n"
1749 "</dict>\n";
1750 for (i = 1; i <= 6; i++)
1751 strbuf_addf(&plist, repeat, i);
1752 break;
1753
1754 case SCHEDULE_WEEKLY:
1755 strbuf_addstr(&plist,
1756 "<dict>\n"
1757 "<key>Day</key><integer>0</integer>\n"
1758 "<key>Hour</key><integer>0</integer>\n"
1759 "<key>Minute</key><integer>0</integer>\n"
1760 "</dict>\n");
1761 break;
1762
1763 default:
1764 /* unreachable */
1765 break;
1766 }
1767 strbuf_addstr(&plist, "</array>\n</dict>\n</plist>\n");
1768
1769 if (safe_create_leading_directories(filename))
1770 die(_("failed to create directories for '%s'"), filename);
1771
1772 if ((long)lock_file_timeout_ms < 0 &&
1773 git_config_get_ulong("gc.launchctlplistlocktimeoutms",
1774 &lock_file_timeout_ms))
1775 lock_file_timeout_ms = 150;
1776
1777 fd = hold_lock_file_for_update_timeout(&lk, filename, LOCK_DIE_ON_ERROR,
1778 lock_file_timeout_ms);
1779
1780 /*
1781 * Does this file already exist? With the intended contents? Is it
1782 * registered already? Then it does not need to be re-registered.
1783 */
1784 if (!stat(filename, &st) && st.st_size == plist.len &&
1785 strbuf_read_file(&plist2, filename, plist.len) == plist.len &&
1786 !strbuf_cmp(&plist, &plist2) &&
1787 launchctl_list_contains_plist(name, cmd))
1788 rollback_lock_file(&lk);
1789 else {
1790 if (write_in_full(fd, plist.buf, plist.len) < 0 ||
1791 commit_lock_file(&lk))
1792 die_errno(_("could not write '%s'"), filename);
1793
1794 /* bootout might fail if not already running, so ignore */
1795 launchctl_boot_plist(0, filename);
1796 if (launchctl_boot_plist(1, filename))
1797 die(_("failed to bootstrap service %s"), filename);
1798 }
1799
1800 free(filename);
1801 free(name);
1802 strbuf_release(&plist);
1803 strbuf_release(&plist2);
1804 return 0;
1805 }
1806
1807 static int launchctl_add_plists(void)
1808 {
1809 const char *exec_path = git_exec_path();
1810
1811 return launchctl_schedule_plist(exec_path, SCHEDULE_HOURLY) ||
1812 launchctl_schedule_plist(exec_path, SCHEDULE_DAILY) ||
1813 launchctl_schedule_plist(exec_path, SCHEDULE_WEEKLY);
1814 }
1815
1816 static int launchctl_update_schedule(int run_maintenance, int fd)
1817 {
1818 if (run_maintenance)
1819 return launchctl_add_plists();
1820 else
1821 return launchctl_remove_plists();
1822 }
1823
1824 static int is_schtasks_available(void)
1825 {
1826 const char *cmd = "schtasks";
1827 int is_available;
1828 if (get_schedule_cmd(&cmd, &is_available))
1829 return is_available;
1830
1831 #ifdef GIT_WINDOWS_NATIVE
1832 return 1;
1833 #else
1834 return 0;
1835 #endif
1836 }
1837
1838 static char *schtasks_task_name(const char *frequency)
1839 {
1840 struct strbuf label = STRBUF_INIT;
1841 strbuf_addf(&label, "Git Maintenance (%s)", frequency);
1842 return strbuf_detach(&label, NULL);
1843 }
1844
1845 static int schtasks_remove_task(enum schedule_priority schedule)
1846 {
1847 const char *cmd = "schtasks";
1848 int result;
1849 struct strvec args = STRVEC_INIT;
1850 const char *frequency = get_frequency(schedule);
1851 char *name = schtasks_task_name(frequency);
1852
1853 get_schedule_cmd(&cmd, NULL);
1854 strvec_split(&args, cmd);
1855 strvec_pushl(&args, "/delete", "/tn", name, "/f", NULL);
1856
1857 result = run_command_v_opt(args.v, 0);
1858
1859 strvec_clear(&args);
1860 free(name);
1861 return result;
1862 }
1863
1864 static int schtasks_remove_tasks(void)
1865 {
1866 return schtasks_remove_task(SCHEDULE_HOURLY) ||
1867 schtasks_remove_task(SCHEDULE_DAILY) ||
1868 schtasks_remove_task(SCHEDULE_WEEKLY);
1869 }
1870
1871 static int schtasks_schedule_task(const char *exec_path, enum schedule_priority schedule)
1872 {
1873 const char *cmd = "schtasks";
1874 int result;
1875 struct child_process child = CHILD_PROCESS_INIT;
1876 const char *xml;
1877 struct tempfile *tfile;
1878 const char *frequency = get_frequency(schedule);
1879 char *name = schtasks_task_name(frequency);
1880 struct strbuf tfilename = STRBUF_INIT;
1881
1882 get_schedule_cmd(&cmd, NULL);
1883
1884 strbuf_addf(&tfilename, "%s/schedule_%s_XXXXXX",
1885 get_git_common_dir(), frequency);
1886 tfile = xmks_tempfile(tfilename.buf);
1887 strbuf_release(&tfilename);
1888
1889 if (!fdopen_tempfile(tfile, "w"))
1890 die(_("failed to create temp xml file"));
1891
1892 xml = "<?xml version=\"1.0\" ?>\n"
1893 "<Task version=\"1.4\" xmlns=\"http://schemas.microsoft.com/windows/2004/02/mit/task\">\n"
1894 "<Triggers>\n"
1895 "<CalendarTrigger>\n";
1896 fputs(xml, tfile->fp);
1897
1898 switch (schedule) {
1899 case SCHEDULE_HOURLY:
1900 fprintf(tfile->fp,
1901 "<StartBoundary>2020-01-01T01:00:00</StartBoundary>\n"
1902 "<Enabled>true</Enabled>\n"
1903 "<ScheduleByDay>\n"
1904 "<DaysInterval>1</DaysInterval>\n"
1905 "</ScheduleByDay>\n"
1906 "<Repetition>\n"
1907 "<Interval>PT1H</Interval>\n"
1908 "<Duration>PT23H</Duration>\n"
1909 "<StopAtDurationEnd>false</StopAtDurationEnd>\n"
1910 "</Repetition>\n");
1911 break;
1912
1913 case SCHEDULE_DAILY:
1914 fprintf(tfile->fp,
1915 "<StartBoundary>2020-01-01T00:00:00</StartBoundary>\n"
1916 "<Enabled>true</Enabled>\n"
1917 "<ScheduleByWeek>\n"
1918 "<DaysOfWeek>\n"
1919 "<Monday />\n"
1920 "<Tuesday />\n"
1921 "<Wednesday />\n"
1922 "<Thursday />\n"
1923 "<Friday />\n"
1924 "<Saturday />\n"
1925 "</DaysOfWeek>\n"
1926 "<WeeksInterval>1</WeeksInterval>\n"
1927 "</ScheduleByWeek>\n");
1928 break;
1929
1930 case SCHEDULE_WEEKLY:
1931 fprintf(tfile->fp,
1932 "<StartBoundary>2020-01-01T00:00:00</StartBoundary>\n"
1933 "<Enabled>true</Enabled>\n"
1934 "<ScheduleByWeek>\n"
1935 "<DaysOfWeek>\n"
1936 "<Sunday />\n"
1937 "</DaysOfWeek>\n"
1938 "<WeeksInterval>1</WeeksInterval>\n"
1939 "</ScheduleByWeek>\n");
1940 break;
1941
1942 default:
1943 break;
1944 }
1945
1946 xml = "</CalendarTrigger>\n"
1947 "</Triggers>\n"
1948 "<Principals>\n"
1949 "<Principal id=\"Author\">\n"
1950 "<LogonType>InteractiveToken</LogonType>\n"
1951 "<RunLevel>LeastPrivilege</RunLevel>\n"
1952 "</Principal>\n"
1953 "</Principals>\n"
1954 "<Settings>\n"
1955 "<MultipleInstancesPolicy>IgnoreNew</MultipleInstancesPolicy>\n"
1956 "<Enabled>true</Enabled>\n"
1957 "<Hidden>true</Hidden>\n"
1958 "<UseUnifiedSchedulingEngine>true</UseUnifiedSchedulingEngine>\n"
1959 "<WakeToRun>false</WakeToRun>\n"
1960 "<ExecutionTimeLimit>PT72H</ExecutionTimeLimit>\n"
1961 "<Priority>7</Priority>\n"
1962 "</Settings>\n"
1963 "<Actions Context=\"Author\">\n"
1964 "<Exec>\n"
1965 "<Command>\"%s\\git.exe\"</Command>\n"
1966 "<Arguments>--exec-path=\"%s\" for-each-repo --config=maintenance.repo maintenance run --schedule=%s</Arguments>\n"
1967 "</Exec>\n"
1968 "</Actions>\n"
1969 "</Task>\n";
1970 fprintf(tfile->fp, xml, exec_path, exec_path, frequency);
1971 strvec_split(&child.args, cmd);
1972 strvec_pushl(&child.args, "/create", "/tn", name, "/f", "/xml",
1973 get_tempfile_path(tfile), NULL);
1974 close_tempfile_gently(tfile);
1975
1976 child.no_stdout = 1;
1977 child.no_stderr = 1;
1978
1979 if (start_command(&child))
1980 die(_("failed to start schtasks"));
1981 result = finish_command(&child);
1982
1983 delete_tempfile(&tfile);
1984 free(name);
1985 return result;
1986 }
1987
1988 static int schtasks_schedule_tasks(void)
1989 {
1990 const char *exec_path = git_exec_path();
1991
1992 return schtasks_schedule_task(exec_path, SCHEDULE_HOURLY) ||
1993 schtasks_schedule_task(exec_path, SCHEDULE_DAILY) ||
1994 schtasks_schedule_task(exec_path, SCHEDULE_WEEKLY);
1995 }
1996
1997 static int schtasks_update_schedule(int run_maintenance, int fd)
1998 {
1999 if (run_maintenance)
2000 return schtasks_schedule_tasks();
2001 else
2002 return schtasks_remove_tasks();
2003 }
2004
2005 MAYBE_UNUSED
2006 static int check_crontab_process(const char *cmd)
2007 {
2008 struct child_process child = CHILD_PROCESS_INIT;
2009
2010 strvec_split(&child.args, cmd);
2011 strvec_push(&child.args, "-l");
2012 child.no_stdin = 1;
2013 child.no_stdout = 1;
2014 child.no_stderr = 1;
2015 child.silent_exec_failure = 1;
2016
2017 if (start_command(&child))
2018 return 0;
2019 /* Ignore exit code, as an empty crontab will return error. */
2020 finish_command(&child);
2021 return 1;
2022 }
2023
2024 static int is_crontab_available(void)
2025 {
2026 const char *cmd = "crontab";
2027 int is_available;
2028
2029 if (get_schedule_cmd(&cmd, &is_available))
2030 return is_available;
2031
2032 #ifdef __APPLE__
2033 /*
2034 * macOS has cron, but it requires special permissions and will
2035 * create a UI alert when attempting to run this command.
2036 */
2037 return 0;
2038 #else
2039 return check_crontab_process(cmd);
2040 #endif
2041 }
2042
2043 #define BEGIN_LINE "# BEGIN GIT MAINTENANCE SCHEDULE"
2044 #define END_LINE "# END GIT MAINTENANCE SCHEDULE"
2045
2046 static int crontab_update_schedule(int run_maintenance, int fd)
2047 {
2048 const char *cmd = "crontab";
2049 int result = 0;
2050 int in_old_region = 0;
2051 struct child_process crontab_list = CHILD_PROCESS_INIT;
2052 struct child_process crontab_edit = CHILD_PROCESS_INIT;
2053 FILE *cron_list, *cron_in;
2054 struct strbuf line = STRBUF_INIT;
2055
2056 get_schedule_cmd(&cmd, NULL);
2057 strvec_split(&crontab_list.args, cmd);
2058 strvec_push(&crontab_list.args, "-l");
2059 crontab_list.in = -1;
2060 crontab_list.out = dup(fd);
2061 crontab_list.git_cmd = 0;
2062
2063 if (start_command(&crontab_list))
2064 return error(_("failed to run 'crontab -l'; your system might not support 'cron'"));
2065
2066 /* Ignore exit code, as an empty crontab will return error. */
2067 finish_command(&crontab_list);
2068
2069 /*
2070 * Read from the .lock file, filtering out the old
2071 * schedule while appending the new schedule.
2072 */
2073 cron_list = fdopen(fd, "r");
2074 rewind(cron_list);
2075
2076 strvec_split(&crontab_edit.args, cmd);
2077 crontab_edit.in = -1;
2078 crontab_edit.git_cmd = 0;
2079
2080 if (start_command(&crontab_edit))
2081 return error(_("failed to run 'crontab'; your system might not support 'cron'"));
2082
2083 cron_in = fdopen(crontab_edit.in, "w");
2084 if (!cron_in) {
2085 result = error(_("failed to open stdin of 'crontab'"));
2086 goto done_editing;
2087 }
2088
2089 while (!strbuf_getline_lf(&line, cron_list)) {
2090 if (!in_old_region && !strcmp(line.buf, BEGIN_LINE))
2091 in_old_region = 1;
2092 else if (in_old_region && !strcmp(line.buf, END_LINE))
2093 in_old_region = 0;
2094 else if (!in_old_region)
2095 fprintf(cron_in, "%s\n", line.buf);
2096 }
2097 strbuf_release(&line);
2098
2099 if (run_maintenance) {
2100 struct strbuf line_format = STRBUF_INIT;
2101 const char *exec_path = git_exec_path();
2102
2103 fprintf(cron_in, "%s\n", BEGIN_LINE);
2104 fprintf(cron_in,
2105 "# The following schedule was created by Git\n");
2106 fprintf(cron_in, "# Any edits made in this region might be\n");
2107 fprintf(cron_in,
2108 "# replaced in the future by a Git command.\n\n");
2109
2110 strbuf_addf(&line_format,
2111 "%%s %%s * * %%s \"%s/git\" --exec-path=\"%s\" for-each-repo --config=maintenance.repo maintenance run --schedule=%%s\n",
2112 exec_path, exec_path);
2113 fprintf(cron_in, line_format.buf, "0", "1-23", "*", "hourly");
2114 fprintf(cron_in, line_format.buf, "0", "0", "1-6", "daily");
2115 fprintf(cron_in, line_format.buf, "0", "0", "0", "weekly");
2116 strbuf_release(&line_format);
2117
2118 fprintf(cron_in, "\n%s\n", END_LINE);
2119 }
2120
2121 fflush(cron_in);
2122 fclose(cron_in);
2123 close(crontab_edit.in);
2124
2125 done_editing:
2126 if (finish_command(&crontab_edit))
2127 result = error(_("'crontab' died"));
2128 else
2129 fclose(cron_list);
2130 return result;
2131 }
2132
2133 static int real_is_systemd_timer_available(void)
2134 {
2135 struct child_process child = CHILD_PROCESS_INIT;
2136
2137 strvec_pushl(&child.args, "systemctl", "--user", "list-timers", NULL);
2138 child.no_stdin = 1;
2139 child.no_stdout = 1;
2140 child.no_stderr = 1;
2141 child.silent_exec_failure = 1;
2142
2143 if (start_command(&child))
2144 return 0;
2145 if (finish_command(&child))
2146 return 0;
2147 return 1;
2148 }
2149
2150 static int is_systemd_timer_available(void)
2151 {
2152 const char *cmd = "systemctl";
2153 int is_available;
2154
2155 if (get_schedule_cmd(&cmd, &is_available))
2156 return is_available;
2157
2158 return real_is_systemd_timer_available();
2159 }
2160
2161 static char *xdg_config_home_systemd(const char *filename)
2162 {
2163 return xdg_config_home_for("systemd/user", filename);
2164 }
2165
2166 static int systemd_timer_enable_unit(int enable,
2167 enum schedule_priority schedule)
2168 {
2169 const char *cmd = "systemctl";
2170 struct child_process child = CHILD_PROCESS_INIT;
2171 const char *frequency = get_frequency(schedule);
2172
2173 /*
2174 * Disabling the systemd unit while it is already disabled makes
2175 * systemctl print an error.
2176 * Let's ignore it since it means we already are in the expected state:
2177 * the unit is disabled.
2178 *
2179 * On the other hand, enabling a systemd unit which is already enabled
2180 * produces no error.
2181 */
2182 if (!enable)
2183 child.no_stderr = 1;
2184
2185 get_schedule_cmd(&cmd, NULL);
2186 strvec_split(&child.args, cmd);
2187 strvec_pushl(&child.args, "--user", enable ? "enable" : "disable",
2188 "--now", NULL);
2189 strvec_pushf(&child.args, "git-maintenance@%s.timer", frequency);
2190
2191 if (start_command(&child))
2192 return error(_("failed to start systemctl"));
2193 if (finish_command(&child))
2194 /*
2195 * Disabling an already disabled systemd unit makes
2196 * systemctl fail.
2197 * Let's ignore this failure.
2198 *
2199 * Enabling an enabled systemd unit doesn't fail.
2200 */
2201 if (enable)
2202 return error(_("failed to run systemctl"));
2203 return 0;
2204 }
2205
2206 static int systemd_timer_delete_unit_templates(void)
2207 {
2208 int ret = 0;
2209 char *filename = xdg_config_home_systemd("git-maintenance@.timer");
2210 if (unlink(filename) && !is_missing_file_error(errno))
2211 ret = error_errno(_("failed to delete '%s'"), filename);
2212 FREE_AND_NULL(filename);
2213
2214 filename = xdg_config_home_systemd("git-maintenance@.service");
2215 if (unlink(filename) && !is_missing_file_error(errno))
2216 ret = error_errno(_("failed to delete '%s'"), filename);
2217
2218 free(filename);
2219 return ret;
2220 }
2221
2222 static int systemd_timer_delete_units(void)
2223 {
2224 return systemd_timer_enable_unit(0, SCHEDULE_HOURLY) ||
2225 systemd_timer_enable_unit(0, SCHEDULE_DAILY) ||
2226 systemd_timer_enable_unit(0, SCHEDULE_WEEKLY) ||
2227 systemd_timer_delete_unit_templates();
2228 }
2229
2230 static int systemd_timer_write_unit_templates(const char *exec_path)
2231 {
2232 char *filename;
2233 FILE *file;
2234 const char *unit;
2235
2236 filename = xdg_config_home_systemd("git-maintenance@.timer");
2237 if (safe_create_leading_directories(filename)) {
2238 error(_("failed to create directories for '%s'"), filename);
2239 goto error;
2240 }
2241 file = fopen_or_warn(filename, "w");
2242 if (file == NULL)
2243 goto error;
2244
2245 unit = "# This file was created and is maintained by Git.\n"
2246 "# Any edits made in this file might be replaced in the future\n"
2247 "# by a Git command.\n"
2248 "\n"
2249 "[Unit]\n"
2250 "Description=Optimize Git repositories data\n"
2251 "\n"
2252 "[Timer]\n"
2253 "OnCalendar=%i\n"
2254 "Persistent=true\n"
2255 "\n"
2256 "[Install]\n"
2257 "WantedBy=timers.target\n";
2258 if (fputs(unit, file) == EOF) {
2259 error(_("failed to write to '%s'"), filename);
2260 fclose(file);
2261 goto error;
2262 }
2263 if (fclose(file) == EOF) {
2264 error_errno(_("failed to flush '%s'"), filename);
2265 goto error;
2266 }
2267 free(filename);
2268
2269 filename = xdg_config_home_systemd("git-maintenance@.service");
2270 file = fopen_or_warn(filename, "w");
2271 if (file == NULL)
2272 goto error;
2273
2274 unit = "# This file was created and is maintained by Git.\n"
2275 "# Any edits made in this file might be replaced in the future\n"
2276 "# by a Git command.\n"
2277 "\n"
2278 "[Unit]\n"
2279 "Description=Optimize Git repositories data\n"
2280 "\n"
2281 "[Service]\n"
2282 "Type=oneshot\n"
2283 "ExecStart=\"%s/git\" --exec-path=\"%s\" for-each-repo --config=maintenance.repo maintenance run --schedule=%%i\n"
2284 "LockPersonality=yes\n"
2285 "MemoryDenyWriteExecute=yes\n"
2286 "NoNewPrivileges=yes\n"
2287 "RestrictAddressFamilies=AF_UNIX AF_INET AF_INET6\n"
2288 "RestrictNamespaces=yes\n"
2289 "RestrictRealtime=yes\n"
2290 "RestrictSUIDSGID=yes\n"
2291 "SystemCallArchitectures=native\n"
2292 "SystemCallFilter=@system-service\n";
2293 if (fprintf(file, unit, exec_path, exec_path) < 0) {
2294 error(_("failed to write to '%s'"), filename);
2295 fclose(file);
2296 goto error;
2297 }
2298 if (fclose(file) == EOF) {
2299 error_errno(_("failed to flush '%s'"), filename);
2300 goto error;
2301 }
2302 free(filename);
2303 return 0;
2304
2305 error:
2306 free(filename);
2307 systemd_timer_delete_unit_templates();
2308 return -1;
2309 }
2310
2311 static int systemd_timer_setup_units(void)
2312 {
2313 const char *exec_path = git_exec_path();
2314
2315 int ret = systemd_timer_write_unit_templates(exec_path) ||
2316 systemd_timer_enable_unit(1, SCHEDULE_HOURLY) ||
2317 systemd_timer_enable_unit(1, SCHEDULE_DAILY) ||
2318 systemd_timer_enable_unit(1, SCHEDULE_WEEKLY);
2319 if (ret)
2320 systemd_timer_delete_units();
2321 return ret;
2322 }
2323
2324 static int systemd_timer_update_schedule(int run_maintenance, int fd)
2325 {
2326 if (run_maintenance)
2327 return systemd_timer_setup_units();
2328 else
2329 return systemd_timer_delete_units();
2330 }
2331
2332 enum scheduler {
2333 SCHEDULER_INVALID = -1,
2334 SCHEDULER_AUTO,
2335 SCHEDULER_CRON,
2336 SCHEDULER_SYSTEMD,
2337 SCHEDULER_LAUNCHCTL,
2338 SCHEDULER_SCHTASKS,
2339 };
2340
2341 static const struct {
2342 const char *name;
2343 int (*is_available)(void);
2344 int (*update_schedule)(int run_maintenance, int fd);
2345 } scheduler_fn[] = {
2346 [SCHEDULER_CRON] = {
2347 .name = "crontab",
2348 .is_available = is_crontab_available,
2349 .update_schedule = crontab_update_schedule,
2350 },
2351 [SCHEDULER_SYSTEMD] = {
2352 .name = "systemctl",
2353 .is_available = is_systemd_timer_available,
2354 .update_schedule = systemd_timer_update_schedule,
2355 },
2356 [SCHEDULER_LAUNCHCTL] = {
2357 .name = "launchctl",
2358 .is_available = is_launchctl_available,
2359 .update_schedule = launchctl_update_schedule,
2360 },
2361 [SCHEDULER_SCHTASKS] = {
2362 .name = "schtasks",
2363 .is_available = is_schtasks_available,
2364 .update_schedule = schtasks_update_schedule,
2365 },
2366 };
2367
2368 static enum scheduler parse_scheduler(const char *value)
2369 {
2370 if (!value)
2371 return SCHEDULER_INVALID;
2372 else if (!strcasecmp(value, "auto"))
2373 return SCHEDULER_AUTO;
2374 else if (!strcasecmp(value, "cron") || !strcasecmp(value, "crontab"))
2375 return SCHEDULER_CRON;
2376 else if (!strcasecmp(value, "systemd") ||
2377 !strcasecmp(value, "systemd-timer"))
2378 return SCHEDULER_SYSTEMD;
2379 else if (!strcasecmp(value, "launchctl"))
2380 return SCHEDULER_LAUNCHCTL;
2381 else if (!strcasecmp(value, "schtasks"))
2382 return SCHEDULER_SCHTASKS;
2383 else
2384 return SCHEDULER_INVALID;
2385 }
2386
2387 static int maintenance_opt_scheduler(const struct option *opt, const char *arg,
2388 int unset)
2389 {
2390 enum scheduler *scheduler = opt->value;
2391
2392 BUG_ON_OPT_NEG(unset);
2393
2394 *scheduler = parse_scheduler(arg);
2395 if (*scheduler == SCHEDULER_INVALID)
2396 return error(_("unrecognized --scheduler argument '%s'"), arg);
2397 return 0;
2398 }
2399
2400 struct maintenance_start_opts {
2401 enum scheduler scheduler;
2402 };
2403
2404 static enum scheduler resolve_scheduler(enum scheduler scheduler)
2405 {
2406 if (scheduler != SCHEDULER_AUTO)
2407 return scheduler;
2408
2409 #if defined(__APPLE__)
2410 return SCHEDULER_LAUNCHCTL;
2411
2412 #elif defined(GIT_WINDOWS_NATIVE)
2413 return SCHEDULER_SCHTASKS;
2414
2415 #elif defined(__linux__)
2416 if (is_systemd_timer_available())
2417 return SCHEDULER_SYSTEMD;
2418 else if (is_crontab_available())
2419 return SCHEDULER_CRON;
2420 else
2421 die(_("neither systemd timers nor crontab are available"));
2422
2423 #else
2424 return SCHEDULER_CRON;
2425 #endif
2426 }
2427
2428 static void validate_scheduler(enum scheduler scheduler)
2429 {
2430 if (scheduler == SCHEDULER_INVALID)
2431 BUG("invalid scheduler");
2432 if (scheduler == SCHEDULER_AUTO)
2433 BUG("resolve_scheduler should have been called before");
2434
2435 if (!scheduler_fn[scheduler].is_available())
2436 die(_("%s scheduler is not available"),
2437 scheduler_fn[scheduler].name);
2438 }
2439
2440 static int update_background_schedule(const struct maintenance_start_opts *opts,
2441 int enable)
2442 {
2443 unsigned int i;
2444 int result = 0;
2445 struct lock_file lk;
2446 char *lock_path = xstrfmt("%s/schedule", the_repository->objects->odb->path);
2447
2448 if (hold_lock_file_for_update(&lk, lock_path, LOCK_NO_DEREF) < 0) {
2449 free(lock_path);
2450 return error(_("another process is scheduling background maintenance"));
2451 }
2452
2453 for (i = 1; i < ARRAY_SIZE(scheduler_fn); i++) {
2454 if (enable && opts->scheduler == i)
2455 continue;
2456 if (!scheduler_fn[i].is_available())
2457 continue;
2458 scheduler_fn[i].update_schedule(0, get_lock_file_fd(&lk));
2459 }
2460
2461 if (enable)
2462 result = scheduler_fn[opts->scheduler].update_schedule(
2463 1, get_lock_file_fd(&lk));
2464
2465 rollback_lock_file(&lk);
2466
2467 free(lock_path);
2468 return result;
2469 }
2470
2471 static const char *const builtin_maintenance_start_usage[] = {
2472 N_("git maintenance start [--scheduler=<scheduler>]"),
2473 NULL
2474 };
2475
2476 static int maintenance_start(int argc, const char **argv, const char *prefix)
2477 {
2478 struct maintenance_start_opts opts = { 0 };
2479 struct option options[] = {
2480 OPT_CALLBACK_F(
2481 0, "scheduler", &opts.scheduler, N_("scheduler"),
2482 N_("scheduler to trigger git maintenance run"),
2483 PARSE_OPT_NONEG, maintenance_opt_scheduler),
2484 OPT_END()
2485 };
2486
2487 argc = parse_options(argc, argv, prefix, options,
2488 builtin_maintenance_start_usage, 0);
2489 if (argc)
2490 usage_with_options(builtin_maintenance_start_usage, options);
2491
2492 opts.scheduler = resolve_scheduler(opts.scheduler);
2493 validate_scheduler(opts.scheduler);
2494
2495 if (maintenance_register())
2496 warning(_("failed to add repo to global config"));
2497 return update_background_schedule(&opts, 1);
2498 }
2499
2500 static int maintenance_stop(void)
2501 {
2502 return update_background_schedule(NULL, 0);
2503 }
2504
2505 static const char builtin_maintenance_usage[] = N_("git maintenance <subcommand> [<options>]");
2506
2507 int cmd_maintenance(int argc, const char **argv, const char *prefix)
2508 {
2509 if (argc < 2 ||
2510 (argc == 2 && !strcmp(argv[1], "-h")))
2511 usage(builtin_maintenance_usage);
2512
2513 if (!strcmp(argv[1], "run"))
2514 return maintenance_run(argc - 1, argv + 1, prefix);
2515 if (!strcmp(argv[1], "start"))
2516 return maintenance_start(argc - 1, argv + 1, prefix);
2517 if (!strcmp(argv[1], "stop"))
2518 return maintenance_stop();
2519 if (!strcmp(argv[1], "register"))
2520 return maintenance_register();
2521 if (!strcmp(argv[1], "unregister"))
2522 return maintenance_unregister();
2523
2524 die(_("invalid subcommand: %s"), argv[1]);
2525 }