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