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