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