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