blob: eeff2b760e0cc58978618c791a195df0bca6627b [file] [log] [blame]
James Bowes6757ada2007-03-13 21:58:22 -04001/*
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
Peter Hagervallbaffc0e2007-07-15 01:14:45 +020013#include "builtin.h"
Stefan Bellera80d72d2018-03-23 18:20:59 +010014#include "repository.h"
Brandon Williamsb2141fc2017-06-14 11:07:36 -070015#include "config.h"
Michael Haggertyebebeae2015-08-10 11:47:49 +020016#include "tempfile.h"
Michael Haggerty697cc8e2014-10-01 12:28:42 +020017#include "lockfile.h"
James Bowes44c637c2007-11-01 21:02:27 -040018#include "parse-options.h"
James Bowes6757ada2007-03-13 21:58:22 -040019#include "run-command.h"
Jonathan Nieder4c5baf02013-10-16 16:11:46 -070020#include "sigchain.h"
Jeff Kingdbbcd442020-07-28 16:23:39 -040021#include "strvec.h"
Nguyễn Thái Ngọc Duyeab32962013-12-05 20:02:54 +070022#include "commit.h"
Derrick Stoleed5d5d7b2018-06-27 09:24:46 -040023#include "commit-graph.h"
Jonathan Tan0abe14f2017-08-18 15:20:26 -070024#include "packfile.h"
Stefan Bellera80d72d2018-03-23 18:20:59 +010025#include "object-store.h"
Nguyễn Thái Ngọc Duy9806f5a2018-04-15 17:36:17 +020026#include "pack.h"
27#include "pack-objects.h"
28#include "blob.h"
29#include "tree.h"
Christian Couderb14ed5a2019-06-25 15:40:31 +020030#include "promisor-remote.h"
Derrick Stolee4ddc79b2020-09-17 18:11:51 +000031#include "refs.h"
Derrick Stolee28cb5e62020-09-25 12:33:31 +000032#include "remote.h"
Derrick Stolee2fec6042020-09-11 17:49:18 +000033#include "exec-cmd.h"
Emily Shafferbad62a82021-12-22 04:59:29 +010034#include "hook.h"
James Bowes6757ada2007-03-13 21:58:22 -040035
36#define FAILED_RUN "failed to run %s"
37
James Bowes44c637c2007-11-01 21:02:27 -040038static const char * const builtin_gc_usage[] = {
Alex Henrie9c9b4f22015-01-13 00:44:47 -070039 N_("git gc [<options>]"),
James Bowes44c637c2007-11-01 21:02:27 -040040 NULL
41};
James Bowes6757ada2007-03-13 21:58:22 -040042
Linus Torvalds56752392007-05-24 11:41:39 -070043static int pack_refs = 1;
Nguyễn Thái Ngọc Duy62aad182014-05-25 07:38:29 +070044static int prune_reflogs = 1;
Taylor Blau5b924772022-05-20 19:18:14 -040045static int cruft_packs = 0;
Jeff King07e7dbf2016-08-11 12:13:09 -040046static int aggressive_depth = 50;
Johannes Schindelin1c192f32007-12-06 12:03:38 +000047static int aggressive_window = 250;
Junio C Hamano2c3c4392007-09-05 13:01:37 -070048static int gc_auto_threshold = 6700;
Junio C Hamano97063972008-03-23 00:04:48 -070049static int gc_auto_pack_limit = 50;
Nguyễn Thái Ngọc Duy9f673f92014-02-08 14:08:52 +070050static int detach_auto = 1;
Johannes Schindelindddbad72017-04-26 21:29:31 +020051static timestamp_t gc_log_expire_time;
David Turnera831c062017-02-10 16:28:22 -050052static const char *gc_log_expire = "1.day.ago";
David Brysond3154b42008-09-30 13:28:58 -070053static const char *prune_expire = "2.weeks.ago";
Nguyễn Thái Ngọc Duye3df33b2014-11-30 15:24:53 +070054static const char *prune_worktrees_expire = "3.months.ago";
Nguyễn Thái Ngọc Duy55dfe132018-04-15 17:36:15 +020055static unsigned long big_pack_threshold;
Nguyễn Thái Ngọc Duy9806f5a2018-04-15 17:36:17 +020056static unsigned long max_delta_cache_size = DEFAULT_DELTA_CACHE_SIZE;
James Bowes6757ada2007-03-13 21:58:22 -040057
Jeff King22f9b7f2020-07-28 16:24:27 -040058static struct strvec reflog = STRVEC_INIT;
59static struct strvec repack = STRVEC_INIT;
60static struct strvec prune = STRVEC_INIT;
61static struct strvec prune_worktrees = STRVEC_INIT;
62static struct strvec rerere = STRVEC_INIT;
James Bowes6757ada2007-03-13 21:58:22 -040063
Jeff King076aa2c2017-09-05 08:15:08 -040064static struct tempfile *pidfile;
Nguyễn Thái Ngọc Duy329e6e82015-09-19 12:13:23 +070065static struct lock_file log_lock;
Jonathan Nieder4c5baf02013-10-16 16:11:46 -070066
Doug Kelly478f34d2015-11-03 21:05:08 -060067static struct string_list pack_garbage = STRING_LIST_INIT_DUP;
68
69static void clean_pack_garbage(void)
70{
71 int i;
72 for (i = 0; i < pack_garbage.nr; i++)
73 unlink_or_warn(pack_garbage.items[i].string);
74 string_list_clear(&pack_garbage, 0);
75}
76
77static void report_pack_garbage(unsigned seen_bits, const char *path)
78{
79 if (seen_bits == PACKDIR_FILE_IDX)
80 string_list_append(&pack_garbage, path);
81}
82
Nguyễn Thái Ngọc Duy329e6e82015-09-19 12:13:23 +070083static void process_log_file(void)
84{
85 struct stat st;
David Turnera831c062017-02-10 16:28:22 -050086 if (fstat(get_lock_file_fd(&log_lock), &st)) {
87 /*
88 * Perhaps there was an i/o error or another
89 * unlikely situation. Try to make a note of
90 * this in gc.log along with any existing
91 * messages.
92 */
93 int saved_errno = errno;
94 fprintf(stderr, _("Failed to fstat %s: %s"),
Martin Ågrend4a49762021-01-05 20:23:46 +010095 get_lock_file_path(&log_lock),
David Turnera831c062017-02-10 16:28:22 -050096 strerror(saved_errno));
97 fflush(stderr);
Nguyễn Thái Ngọc Duy329e6e82015-09-19 12:13:23 +070098 commit_lock_file(&log_lock);
David Turnera831c062017-02-10 16:28:22 -050099 errno = saved_errno;
100 } else if (st.st_size) {
101 /* There was some error recorded in the lock file */
102 commit_lock_file(&log_lock);
103 } else {
104 /* No error, clean up any old gc.log */
105 unlink(git_path("gc.log"));
Nguyễn Thái Ngọc Duy329e6e82015-09-19 12:13:23 +0700106 rollback_lock_file(&log_lock);
David Turnera831c062017-02-10 16:28:22 -0500107 }
Nguyễn Thái Ngọc Duy329e6e82015-09-19 12:13:23 +0700108}
109
110static void process_log_file_at_exit(void)
111{
112 fflush(stderr);
113 process_log_file();
114}
115
116static void process_log_file_on_signal(int signo)
117{
118 process_log_file();
119 sigchain_pop(signo);
120 raise(signo);
121}
122
Ævar Arnfjörð Bjarmasonbf3d70f2019-03-28 17:14:34 +0100123static int gc_config_is_timestamp_never(const char *var)
124{
125 const char *value;
126 timestamp_t expire;
127
128 if (!git_config_get_value(var, &value) && value) {
129 if (parse_expiry_date(value, &expire))
130 die(_("failed to parse '%s' value '%s'"), var, value);
131 return expire == 0;
132 }
133 return 0;
134}
135
Tanay Abhra5801d3b2014-08-07 09:21:22 -0700136static void gc_config(void)
James Bowes6757ada2007-03-13 21:58:22 -0400137{
Tanay Abhra5801d3b2014-08-07 09:21:22 -0700138 const char *value;
139
140 if (!git_config_get_value("gc.packrefs", &value)) {
Miklos Vajnac5e5a2c2008-02-08 15:26:18 +0100141 if (value && !strcmp(value, "notbare"))
James Bowes6757ada2007-03-13 21:58:22 -0400142 pack_refs = -1;
143 else
Tanay Abhra5801d3b2014-08-07 09:21:22 -0700144 pack_refs = git_config_bool("gc.packrefs", value);
James Bowes6757ada2007-03-13 21:58:22 -0400145 }
Tanay Abhra5801d3b2014-08-07 09:21:22 -0700146
Ævar Arnfjörð Bjarmasonbf3d70f2019-03-28 17:14:34 +0100147 if (gc_config_is_timestamp_never("gc.reflogexpire") &&
148 gc_config_is_timestamp_never("gc.reflogexpireunreachable"))
149 prune_reflogs = 0;
150
Tanay Abhra5801d3b2014-08-07 09:21:22 -0700151 git_config_get_int("gc.aggressivewindow", &aggressive_window);
152 git_config_get_int("gc.aggressivedepth", &aggressive_depth);
153 git_config_get_int("gc.auto", &gc_auto_threshold);
154 git_config_get_int("gc.autopacklimit", &gc_auto_pack_limit);
155 git_config_get_bool("gc.autodetach", &detach_auto);
Taylor Blau5b924772022-05-20 19:18:14 -0400156 git_config_get_bool("gc.cruftpacks", &cruft_packs);
Christian Couder77d67972017-02-27 19:00:13 +0100157 git_config_get_expiry("gc.pruneexpire", &prune_expire);
158 git_config_get_expiry("gc.worktreepruneexpire", &prune_worktrees_expire);
Junio C Hamano94c9b5a2017-03-17 13:50:23 -0700159 git_config_get_expiry("gc.logexpiry", &gc_log_expire);
David Turnera831c062017-02-10 16:28:22 -0500160
Nguyễn Thái Ngọc Duy55dfe132018-04-15 17:36:15 +0200161 git_config_get_ulong("gc.bigpackthreshold", &big_pack_threshold);
Nguyễn Thái Ngọc Duy9806f5a2018-04-15 17:36:17 +0200162 git_config_get_ulong("pack.deltacachesize", &max_delta_cache_size);
Nguyễn Thái Ngọc Duy55dfe132018-04-15 17:36:15 +0200163
Tanay Abhra5801d3b2014-08-07 09:21:22 -0700164 git_config(git_default_config, NULL);
James Bowes6757ada2007-03-13 21:58:22 -0400165}
166
Derrick Stolee41abfe12021-02-09 13:42:28 +0000167struct maintenance_run_opts;
168static int maintenance_task_pack_refs(MAYBE_UNUSED struct maintenance_run_opts *opts)
169{
170 struct strvec pack_refs_cmd = STRVEC_INIT;
Ævar Arnfjörð Bjarmason55916bb2022-07-01 12:42:58 +0200171 int ret;
172
Derrick Stolee41abfe12021-02-09 13:42:28 +0000173 strvec_pushl(&pack_refs_cmd, "pack-refs", "--all", "--prune", NULL);
174
Ævar Arnfjörð Bjarmason55916bb2022-07-01 12:42:58 +0200175 ret = run_command_v_opt(pack_refs_cmd.v, RUN_GIT_CMD);
176
177 strvec_clear(&pack_refs_cmd);
178
179 return ret;
Derrick Stolee41abfe12021-02-09 13:42:28 +0000180}
181
Junio C Hamanoa087cc92007-09-17 00:44:17 -0700182static int too_many_loose_objects(void)
Junio C Hamano2c3c4392007-09-05 13:01:37 -0700183{
184 /*
185 * Quickly check if a "gc" is needed, by estimating how
186 * many loose objects there are. Because SHA-1 is evenly
187 * distributed, we can check only one and get a reasonable
188 * estimate.
189 */
Junio C Hamano2c3c4392007-09-05 13:01:37 -0700190 DIR *dir;
191 struct dirent *ent;
192 int auto_threshold;
193 int num_loose = 0;
194 int needed = 0;
Ævar Arnfjörð Bjarmasone5cdbd52019-03-15 16:59:53 +0100195 const unsigned hexsz_loose = the_hash_algo->hexsz - 2;
Junio C Hamano2c3c4392007-09-05 13:01:37 -0700196
Jeff King07af8892017-03-28 15:47:03 -0400197 dir = opendir(git_path("objects/17"));
Junio C Hamano2c3c4392007-09-05 13:01:37 -0700198 if (!dir)
199 return 0;
200
René Scharfe42c78a22017-07-08 12:35:35 +0200201 auto_threshold = DIV_ROUND_UP(gc_auto_threshold, 256);
Junio C Hamano2c3c4392007-09-05 13:01:37 -0700202 while ((ent = readdir(dir)) != NULL) {
Ævar Arnfjörð Bjarmasone5cdbd52019-03-15 16:59:53 +0100203 if (strspn(ent->d_name, "0123456789abcdef") != hexsz_loose ||
204 ent->d_name[hexsz_loose] != '\0')
Junio C Hamano2c3c4392007-09-05 13:01:37 -0700205 continue;
206 if (++num_loose > auto_threshold) {
207 needed = 1;
208 break;
209 }
210 }
211 closedir(dir);
212 return needed;
213}
214
Nguyễn Thái Ngọc Duy9806f5a2018-04-15 17:36:17 +0200215static struct packed_git *find_base_packs(struct string_list *packs,
216 unsigned long limit)
Nguyễn Thái Ngọc Duyae4e89e2018-04-15 17:36:14 +0200217{
218 struct packed_git *p, *base = NULL;
219
Derrick Stolee454ea2e2018-08-20 16:52:04 +0000220 for (p = get_all_packs(the_repository); p; p = p->next) {
Nguyễn Thái Ngọc Duyae4e89e2018-04-15 17:36:14 +0200221 if (!p->pack_local)
222 continue;
Nguyễn Thái Ngọc Duy55dfe132018-04-15 17:36:15 +0200223 if (limit) {
224 if (p->pack_size >= limit)
225 string_list_append(packs, p->pack_name);
226 } else if (!base || base->pack_size < p->pack_size) {
Nguyễn Thái Ngọc Duyae4e89e2018-04-15 17:36:14 +0200227 base = p;
228 }
229 }
230
231 if (base)
232 string_list_append(packs, base->pack_name);
Nguyễn Thái Ngọc Duy9806f5a2018-04-15 17:36:17 +0200233
234 return base;
Nguyễn Thái Ngọc Duyae4e89e2018-04-15 17:36:14 +0200235}
236
Junio C Hamano17815502007-09-17 00:55:13 -0700237static int too_many_packs(void)
238{
239 struct packed_git *p;
240 int cnt;
241
242 if (gc_auto_pack_limit <= 0)
243 return 0;
244
Derrick Stolee454ea2e2018-08-20 16:52:04 +0000245 for (cnt = 0, p = get_all_packs(the_repository); p; p = p->next) {
Junio C Hamano17815502007-09-17 00:55:13 -0700246 if (!p->pack_local)
247 continue;
Brandon Casey01af2492008-11-12 11:59:07 -0600248 if (p->pack_keep)
Junio C Hamano17815502007-09-17 00:55:13 -0700249 continue;
250 /*
251 * Perhaps check the size of the pack and count only
252 * very small ones here?
253 */
254 cnt++;
255 }
Eric Wong5f4e3bf2016-06-25 06:46:47 +0000256 return gc_auto_pack_limit < cnt;
Junio C Hamano17815502007-09-17 00:55:13 -0700257}
258
Nguyễn Thái Ngọc Duy9806f5a2018-04-15 17:36:17 +0200259static uint64_t total_ram(void)
260{
261#if defined(HAVE_SYSINFO)
262 struct sysinfo si;
263
264 if (!sysinfo(&si))
265 return si.totalram;
266#elif defined(HAVE_BSD_SYSCTL) && (defined(HW_MEMSIZE) || defined(HW_PHYSMEM))
267 int64_t physical_memory;
268 int mib[2];
269 size_t length;
270
271 mib[0] = CTL_HW;
272# if defined(HW_MEMSIZE)
273 mib[1] = HW_MEMSIZE;
274# else
275 mib[1] = HW_PHYSMEM;
276# endif
277 length = sizeof(int64_t);
278 if (!sysctl(mib, 2, &physical_memory, &length, NULL, 0))
279 return physical_memory;
280#elif defined(GIT_WINDOWS_NATIVE)
281 MEMORYSTATUSEX memInfo;
282
283 memInfo.dwLength = sizeof(MEMORYSTATUSEX);
284 if (GlobalMemoryStatusEx(&memInfo))
285 return memInfo.ullTotalPhys;
286#endif
287 return 0;
288}
289
290static uint64_t estimate_repack_memory(struct packed_git *pack)
291{
292 unsigned long nr_objects = approximate_object_count();
293 size_t os_cache, heap;
294
295 if (!pack || !nr_objects)
296 return 0;
297
298 /*
299 * First we have to scan through at least one pack.
300 * Assume enough room in OS file cache to keep the entire pack
301 * or we may accidentally evict data of other processes from
302 * the cache.
303 */
304 os_cache = pack->pack_size + pack->index_size;
305 /* then pack-objects needs lots more for book keeping */
306 heap = sizeof(struct object_entry) * nr_objects;
307 /*
308 * internal rev-list --all --objects takes up some memory too,
309 * let's say half of it is for blobs
310 */
311 heap += sizeof(struct blob) * nr_objects / 2;
312 /*
313 * and the other half is for trees (commits and tags are
314 * usually insignificant)
315 */
316 heap += sizeof(struct tree) * nr_objects / 2;
317 /* and then obj_hash[], underestimated in fact */
318 heap += sizeof(struct object *) * nr_objects;
319 /* revindex is used also */
Taylor Blau2891b432021-01-13 17:24:54 -0500320 heap += (sizeof(off_t) + sizeof(uint32_t)) * nr_objects;
Nguyễn Thái Ngọc Duy9806f5a2018-04-15 17:36:17 +0200321 /*
322 * read_sha1_file() (either at delta calculation phase, or
323 * writing phase) also fills up the delta base cache
324 */
325 heap += delta_base_cache_limit;
326 /* and of course pack-objects has its own delta cache */
327 heap += max_delta_cache_size;
328
329 return os_cache + heap;
330}
331
Nguyễn Thái Ngọc Duyae4e89e2018-04-15 17:36:14 +0200332static int keep_one_pack(struct string_list_item *item, void *data)
333{
Jeff King22f9b7f2020-07-28 16:24:27 -0400334 strvec_pushf(&repack, "--keep-pack=%s", basename(item->string));
Nguyễn Thái Ngọc Duyae4e89e2018-04-15 17:36:14 +0200335 return 0;
336}
337
338static void add_repack_all_option(struct string_list *keep_pack)
Jeff King7e52f562012-04-07 06:30:09 -0400339{
340 if (prune_expire && !strcmp(prune_expire, "now"))
Jeff King22f9b7f2020-07-28 16:24:27 -0400341 strvec_push(&repack, "-a");
Taylor Blau5b924772022-05-20 19:18:14 -0400342 else if (cruft_packs) {
343 strvec_push(&repack, "--cruft");
344 if (prune_expire)
345 strvec_pushf(&repack, "--cruft-expiration=%s", prune_expire);
346 } else {
Jeff King22f9b7f2020-07-28 16:24:27 -0400347 strvec_push(&repack, "-A");
Jeff King234587f2012-04-18 14:10:19 -0700348 if (prune_expire)
Jeff King22f9b7f2020-07-28 16:24:27 -0400349 strvec_pushf(&repack, "--unpack-unreachable=%s", prune_expire);
Jeff King7e52f562012-04-07 06:30:09 -0400350 }
Nguyễn Thái Ngọc Duyae4e89e2018-04-15 17:36:14 +0200351
352 if (keep_pack)
353 for_each_string_list(keep_pack, keep_one_pack, NULL);
Jeff King7e52f562012-04-07 06:30:09 -0400354}
355
David Turnerbdf56de2016-12-28 17:45:41 -0500356static void add_repack_incremental_option(void)
357{
Jeff King22f9b7f2020-07-28 16:24:27 -0400358 strvec_push(&repack, "--no-write-bitmap-index");
David Turnerbdf56de2016-12-28 17:45:41 -0500359}
360
Junio C Hamanoa087cc92007-09-17 00:44:17 -0700361static int need_to_gc(void)
362{
363 /*
Brandon Caseyb14d2552008-03-19 16:53:20 -0500364 * Setting gc.auto to 0 or negative can disable the
365 * automatic gc.
Junio C Hamanoa087cc92007-09-17 00:44:17 -0700366 */
Brandon Caseyb14d2552008-03-19 16:53:20 -0500367 if (gc_auto_threshold <= 0)
Junio C Hamanoa087cc92007-09-17 00:44:17 -0700368 return 0;
369
Junio C Hamano17815502007-09-17 00:55:13 -0700370 /*
371 * If there are too many loose objects, but not too many
372 * packs, we run "repack -d -l". If there are too many packs,
373 * we run "repack -A -d -l". Otherwise we tell the caller
374 * there is no need.
375 */
Nguyễn Thái Ngọc Duy55dfe132018-04-15 17:36:15 +0200376 if (too_many_packs()) {
377 struct string_list keep_pack = STRING_LIST_INIT_NODUP;
378
Nguyễn Thái Ngọc Duy8fc67762018-04-15 17:36:16 +0200379 if (big_pack_threshold) {
Nguyễn Thái Ngọc Duy55dfe132018-04-15 17:36:15 +0200380 find_base_packs(&keep_pack, big_pack_threshold);
Nguyễn Thái Ngọc Duy8fc67762018-04-15 17:36:16 +0200381 if (keep_pack.nr >= gc_auto_pack_limit) {
382 big_pack_threshold = 0;
383 string_list_clear(&keep_pack, 0);
384 find_base_packs(&keep_pack, 0);
385 }
Nguyễn Thái Ngọc Duy9806f5a2018-04-15 17:36:17 +0200386 } else {
387 struct packed_git *p = find_base_packs(&keep_pack, 0);
388 uint64_t mem_have, mem_want;
389
390 mem_have = total_ram();
391 mem_want = estimate_repack_memory(p);
392
393 /*
394 * Only allow 1/2 of memory for pack-objects, leave
395 * the rest for the OS and other processes in the
396 * system.
397 */
398 if (!mem_have || mem_want < mem_have / 2)
399 string_list_clear(&keep_pack, 0);
Nguyễn Thái Ngọc Duy8fc67762018-04-15 17:36:16 +0200400 }
Nguyễn Thái Ngọc Duy55dfe132018-04-15 17:36:15 +0200401
402 add_repack_all_option(&keep_pack);
403 string_list_clear(&keep_pack, 0);
404 } else if (too_many_loose_objects())
David Turnerbdf56de2016-12-28 17:45:41 -0500405 add_repack_incremental_option();
406 else
Junio C Hamano17815502007-09-17 00:55:13 -0700407 return 0;
Miklos Vajnabde30542008-04-02 21:34:38 +0200408
Emily Shafferbad62a82021-12-22 04:59:29 +0100409 if (run_hooks("pre-auto-gc"))
Miklos Vajnabde30542008-04-02 21:34:38 +0200410 return 0;
Junio C Hamano95143f92007-09-17 00:48:39 -0700411 return 1;
Junio C Hamanoa087cc92007-09-17 00:44:17 -0700412}
413
Nguyễn Thái Ngọc Duy64a99eb2013-08-08 18:05:38 +0700414/* return NULL on success, else hostname running the gc */
415static const char *lock_repo_for_gc(int force, pid_t* ret_pid)
416{
Martin Ågrenb2275862018-05-09 22:55:38 +0200417 struct lock_file lock = LOCK_INIT;
René Scharfeda25bdb2017-04-18 17:57:42 -0400418 char my_host[HOST_NAME_MAX + 1];
Nguyễn Thái Ngọc Duy64a99eb2013-08-08 18:05:38 +0700419 struct strbuf sb = STRBUF_INIT;
420 struct stat st;
421 uintmax_t pid;
422 FILE *fp;
Elia Pinto4f1c0b22014-01-29 08:59:37 -0800423 int fd;
Michael Haggerty00539ce2015-08-10 11:47:48 +0200424 char *pidfile_path;
Nguyễn Thái Ngọc Duy64a99eb2013-08-08 18:05:38 +0700425
Jeff King076aa2c2017-09-05 08:15:08 -0400426 if (is_tempfile_active(pidfile))
Jonathan Nieder4c5baf02013-10-16 16:11:46 -0700427 /* already locked */
428 return NULL;
429
David Turner5781a9a2017-04-18 17:57:43 -0400430 if (xgethostname(my_host, sizeof(my_host)))
Jeff King5096d492015-09-24 17:06:08 -0400431 xsnprintf(my_host, sizeof(my_host), "unknown");
Nguyễn Thái Ngọc Duy64a99eb2013-08-08 18:05:38 +0700432
Michael Haggerty00539ce2015-08-10 11:47:48 +0200433 pidfile_path = git_pathdup("gc.pid");
434 fd = hold_lock_file_for_update(&lock, pidfile_path,
Nguyễn Thái Ngọc Duy64a99eb2013-08-08 18:05:38 +0700435 LOCK_DIE_ON_ERROR);
436 if (!force) {
René Scharfeda25bdb2017-04-18 17:57:42 -0400437 static char locking_host[HOST_NAME_MAX + 1];
438 static char *scan_fmt;
Elia Pinto4f1c0b22014-01-29 08:59:37 -0800439 int should_exit;
René Scharfeda25bdb2017-04-18 17:57:42 -0400440
441 if (!scan_fmt)
Junio C Hamanoafe2fab2017-09-17 12:16:55 +0900442 scan_fmt = xstrfmt("%s %%%ds", "%"SCNuMAX, HOST_NAME_MAX);
Michael Haggerty00539ce2015-08-10 11:47:48 +0200443 fp = fopen(pidfile_path, "r");
Nguyễn Thái Ngọc Duy64a99eb2013-08-08 18:05:38 +0700444 memset(locking_host, 0, sizeof(locking_host));
445 should_exit =
446 fp != NULL &&
447 !fstat(fileno(fp), &st) &&
448 /*
449 * 12 hour limit is very generous as gc should
450 * never take that long. On the other hand we
451 * don't really need a strict limit here,
452 * running gc --auto one day late is not a big
453 * problem. --force can be used in manual gc
454 * after the user verifies that no gc is
455 * running.
456 */
457 time(NULL) - st.st_mtime <= 12 * 3600 &&
René Scharfeda25bdb2017-04-18 17:57:42 -0400458 fscanf(fp, scan_fmt, &pid, locking_host) == 2 &&
Nguyễn Thái Ngọc Duy64a99eb2013-08-08 18:05:38 +0700459 /* be gentle to concurrent "gc" on remote hosts */
Kyle J. McKayed7eda82013-12-31 04:07:39 -0800460 (strcmp(locking_host, my_host) || !kill(pid, 0) || errno == EPERM);
Junio C Hamanoafe8a902022-05-02 09:50:37 -0700461 if (fp)
Nguyễn Thái Ngọc Duy64a99eb2013-08-08 18:05:38 +0700462 fclose(fp);
463 if (should_exit) {
464 if (fd >= 0)
465 rollback_lock_file(&lock);
466 *ret_pid = pid;
Michael Haggerty00539ce2015-08-10 11:47:48 +0200467 free(pidfile_path);
Nguyễn Thái Ngọc Duy64a99eb2013-08-08 18:05:38 +0700468 return locking_host;
469 }
470 }
471
472 strbuf_addf(&sb, "%"PRIuMAX" %s",
473 (uintmax_t) getpid(), my_host);
474 write_in_full(fd, sb.buf, sb.len);
475 strbuf_release(&sb);
476 commit_lock_file(&lock);
Jeff King076aa2c2017-09-05 08:15:08 -0400477 pidfile = register_tempfile(pidfile_path);
Michael Haggertyebebeae2015-08-10 11:47:49 +0200478 free(pidfile_path);
Nguyễn Thái Ngọc Duy64a99eb2013-08-08 18:05:38 +0700479 return NULL;
480}
481
Jonathan Nieder30299702018-07-16 23:57:40 -0700482/*
483 * Returns 0 if there was no previous error and gc can proceed, 1 if
484 * gc should not proceed due to an error in the last run. Prints a
Ævar Arnfjörð Bjarmason24f6e6d2021-12-07 19:26:33 +0100485 * message and returns with a non-[01] status code if an error occurred
486 * while reading gc.log
Jonathan Nieder30299702018-07-16 23:57:40 -0700487 */
488static int report_last_gc_error(void)
Nguyễn Thái Ngọc Duy329e6e82015-09-19 12:13:23 +0700489{
490 struct strbuf sb = STRBUF_INIT;
Jonathan Nieder30299702018-07-16 23:57:40 -0700491 int ret = 0;
Jonathan Nieder3c426ec2018-07-16 23:53:21 -0700492 ssize_t len;
David Turnera831c062017-02-10 16:28:22 -0500493 struct stat st;
494 char *gc_log_path = git_pathdup("gc.log");
Nguyễn Thái Ngọc Duy329e6e82015-09-19 12:13:23 +0700495
David Turnera831c062017-02-10 16:28:22 -0500496 if (stat(gc_log_path, &st)) {
497 if (errno == ENOENT)
498 goto done;
499
Ævar Arnfjörð Bjarmason24f6e6d2021-12-07 19:26:33 +0100500 ret = die_message_errno(_("cannot stat '%s'"), gc_log_path);
Jonathan Nieder30299702018-07-16 23:57:40 -0700501 goto done;
David Turnera831c062017-02-10 16:28:22 -0500502 }
503
504 if (st.st_mtime < gc_log_expire_time)
505 goto done;
506
Jonathan Nieder3c426ec2018-07-16 23:53:21 -0700507 len = strbuf_read_file(&sb, gc_log_path, 0);
508 if (len < 0)
Ævar Arnfjörð Bjarmason24f6e6d2021-12-07 19:26:33 +0100509 ret = die_message_errno(_("cannot read '%s'"), gc_log_path);
Jonathan Nieder30299702018-07-16 23:57:40 -0700510 else if (len > 0) {
511 /*
512 * A previous gc failed. Report the error, and don't
513 * bother with an automatic gc run since it is likely
514 * to fail in the same way.
515 */
516 warning(_("The last gc run reported the following. "
Nguyễn Thái Ngọc Duy329e6e82015-09-19 12:13:23 +0700517 "Please correct the root cause\n"
Ævar Arnfjörð Bjarmasonb45c1722021-08-31 16:37:29 +0200518 "and remove %s\n"
Nguyễn Thái Ngọc Duy329e6e82015-09-19 12:13:23 +0700519 "Automatic cleanup will not be performed "
520 "until the file is removed.\n\n"
521 "%s"),
David Turnera831c062017-02-10 16:28:22 -0500522 gc_log_path, sb.buf);
Jonathan Nieder30299702018-07-16 23:57:40 -0700523 ret = 1;
524 }
Nguyễn Thái Ngọc Duy329e6e82015-09-19 12:13:23 +0700525 strbuf_release(&sb);
David Turnera831c062017-02-10 16:28:22 -0500526done:
527 free(gc_log_path);
Jonathan Nieder30299702018-07-16 23:57:40 -0700528 return ret;
Nguyễn Thái Ngọc Duy329e6e82015-09-19 12:13:23 +0700529}
530
Jonathan Niederfec2ed22018-07-16 23:54:16 -0700531static void gc_before_repack(void)
Nguyễn Thái Ngọc Duy62aad182014-05-25 07:38:29 +0700532{
Ævar Arnfjörð Bjarmasoncd8eb3a2019-03-15 16:59:54 +0100533 /*
534 * We may be called twice, as both the pre- and
535 * post-daemonized phases will call us, but running these
536 * commands more than once is pointless and wasteful.
537 */
538 static int done = 0;
539 if (done++)
540 return;
541
Derrick Stolee41abfe12021-02-09 13:42:28 +0000542 if (pack_refs && maintenance_task_pack_refs(NULL))
543 die(FAILED_RUN, "pack-refs");
Nguyễn Thái Ngọc Duy62aad182014-05-25 07:38:29 +0700544
Jeff Kingd70a9eb2020-07-28 20:37:20 -0400545 if (prune_reflogs && run_command_v_opt(reflog.v, RUN_GIT_CMD))
546 die(FAILED_RUN, reflog.v[0]);
Nguyễn Thái Ngọc Duy62aad182014-05-25 07:38:29 +0700547}
548
James Bowes6757ada2007-03-13 21:58:22 -0400549int cmd_gc(int argc, const char **argv, const char *prefix)
550{
James Bowes44c637c2007-11-01 21:02:27 -0400551 int aggressive = 0;
Junio C Hamano2c3c4392007-09-05 13:01:37 -0700552 int auto_gc = 0;
Frank Lichtenhelda0c14cb2008-02-29 22:53:39 +0100553 int quiet = 0;
Nguyễn Thái Ngọc Duy64a99eb2013-08-08 18:05:38 +0700554 int force = 0;
555 const char *name;
556 pid_t pid;
Nguyễn Thái Ngọc Duy329e6e82015-09-19 12:13:23 +0700557 int daemonized = 0;
Ævar Arnfjörð Bjarmason793c1462020-11-20 12:55:22 +0100558 int keep_largest_pack = -1;
Junio C Hamano8ab5aa42018-04-21 12:13:13 +0900559 timestamp_t dummy;
James Bowes6757ada2007-03-13 21:58:22 -0400560
James Bowes44c637c2007-11-01 21:02:27 -0400561 struct option builtin_gc_options[] = {
Nguyễn Thái Ngọc Duy6705c162012-08-20 19:32:14 +0700562 OPT__QUIET(&quiet, N_("suppress progress reporting")),
563 { OPTION_STRING, 0, "prune", &prune_expire, N_("date"),
564 N_("prune unreferenced objects"),
Johannes Schindelin58e9d9d2009-02-14 23:10:10 +0100565 PARSE_OPT_OPTARG, NULL, (intptr_t)prune_expire },
Taylor Blau5b924772022-05-20 19:18:14 -0400566 OPT_BOOL(0, "cruft", &cruft_packs, N_("pack unreferenced objects separately")),
Stefan Bellerd5d09d42013-08-03 13:51:19 +0200567 OPT_BOOL(0, "aggressive", &aggressive, N_("be more thorough (increased runtime)")),
Nguyễn Thái Ngọc Duy7e1eeaa2018-02-09 18:01:58 +0700568 OPT_BOOL_F(0, "auto", &auto_gc, N_("enable auto-gc mode"),
569 PARSE_OPT_NOCOMPLETE),
570 OPT_BOOL_F(0, "force", &force,
571 N_("force running gc even if there may be another gc running"),
572 PARSE_OPT_NOCOMPLETE),
Ævar Arnfjörð Bjarmason793c1462020-11-20 12:55:22 +0100573 OPT_BOOL(0, "keep-largest-pack", &keep_largest_pack,
Nguyễn Thái Ngọc Duyae4e89e2018-04-15 17:36:14 +0200574 N_("repack all other packs except the largest pack")),
James Bowes44c637c2007-11-01 21:02:27 -0400575 OPT_END()
576 };
577
Nguyễn Thái Ngọc Duy0c8151b2010-10-22 01:47:19 -0500578 if (argc == 2 && !strcmp(argv[1], "-h"))
579 usage_with_options(builtin_gc_usage, builtin_gc_options);
580
Jeff King22f9b7f2020-07-28 16:24:27 -0400581 strvec_pushl(&reflog, "reflog", "expire", "--all", NULL);
582 strvec_pushl(&repack, "repack", "-d", "-l", NULL);
583 strvec_pushl(&prune, "prune", "--expire", NULL);
584 strvec_pushl(&prune_worktrees, "worktree", "prune", "--expire", NULL);
585 strvec_pushl(&rerere, "rerere", "gc", NULL);
Jeff King234587f2012-04-18 14:10:19 -0700586
David Turnera831c062017-02-10 16:28:22 -0500587 /* default expiry time, overwritten in gc_config */
Tanay Abhra5801d3b2014-08-07 09:21:22 -0700588 gc_config();
David Turnera831c062017-02-10 16:28:22 -0500589 if (parse_expiry_date(gc_log_expire, &gc_log_expire_time))
Jiang Xinb4eda052022-06-17 18:03:09 +0800590 die(_("failed to parse gc.logExpiry value %s"), gc_log_expire);
James Bowes6757ada2007-03-13 21:58:22 -0400591
592 if (pack_refs < 0)
593 pack_refs = !is_bare_repository();
594
Stephen Boyd37782922009-05-23 11:53:12 -0700595 argc = parse_options(argc, argv, prefix, builtin_gc_options,
596 builtin_gc_usage, 0);
James Bowes44c637c2007-11-01 21:02:27 -0400597 if (argc > 0)
598 usage_with_options(builtin_gc_usage, builtin_gc_options);
599
Junio C Hamano8ab5aa42018-04-21 12:13:13 +0900600 if (prune_expire && parse_expiry_date(prune_expire, &dummy))
601 die(_("failed to parse prune expiry value %s"), prune_expire);
602
James Bowes44c637c2007-11-01 21:02:27 -0400603 if (aggressive) {
Jeff King22f9b7f2020-07-28 16:24:27 -0400604 strvec_push(&repack, "-f");
Nguyễn Thái Ngọc Duy125f8142014-03-16 20:35:03 +0700605 if (aggressive_depth > 0)
Jeff King22f9b7f2020-07-28 16:24:27 -0400606 strvec_pushf(&repack, "--depth=%d", aggressive_depth);
Jeff King234587f2012-04-18 14:10:19 -0700607 if (aggressive_window > 0)
Jeff King22f9b7f2020-07-28 16:24:27 -0400608 strvec_pushf(&repack, "--window=%d", aggressive_window);
James Bowes6757ada2007-03-13 21:58:22 -0400609 }
Frank Lichtenhelda0c14cb2008-02-29 22:53:39 +0100610 if (quiet)
Jeff King22f9b7f2020-07-28 16:24:27 -0400611 strvec_push(&repack, "-q");
James Bowes6757ada2007-03-13 21:58:22 -0400612
Junio C Hamano2c3c4392007-09-05 13:01:37 -0700613 if (auto_gc) {
614 /*
615 * Auto-gc should be least intrusive as possible.
616 */
Junio C Hamano2c3c4392007-09-05 13:01:37 -0700617 if (!need_to_gc())
618 return 0;
Nguyễn Thái Ngọc Duy9f673f92014-02-08 14:08:52 +0700619 if (!quiet) {
620 if (detach_auto)
621 fprintf(stderr, _("Auto packing the repository in background for optimum performance.\n"));
622 else
623 fprintf(stderr, _("Auto packing the repository for optimum performance.\n"));
624 fprintf(stderr, _("See \"git help gc\" for manual housekeeping.\n"));
625 }
Nguyễn Thái Ngọc Duy62aad182014-05-25 07:38:29 +0700626 if (detach_auto) {
Jonathan Nieder30299702018-07-16 23:57:40 -0700627 int ret = report_last_gc_error();
Ævar Arnfjörð Bjarmason0faf84d2021-12-07 19:26:32 +0100628
Jonathan Nieder30299702018-07-16 23:57:40 -0700629 if (ret == 1)
630 /* Last gc --auto failed. Skip this one. */
631 return 0;
Ævar Arnfjörð Bjarmason24f6e6d2021-12-07 19:26:33 +0100632 else if (ret)
633 /* an I/O error occurred, already reported */
634 return ret;
Nguyễn Thái Ngọc Duy329e6e82015-09-19 12:13:23 +0700635
Jeff Kingc45af942017-07-11 05:06:35 -0400636 if (lock_repo_for_gc(force, &pid))
637 return 0;
Jonathan Niederfec2ed22018-07-16 23:54:16 -0700638 gc_before_repack(); /* dies on failure */
Jeff Kingc45af942017-07-11 05:06:35 -0400639 delete_tempfile(&pidfile);
640
Nguyễn Thái Ngọc Duy9f673f92014-02-08 14:08:52 +0700641 /*
642 * failure to daemonize is ok, we'll continue
643 * in foreground
644 */
Nguyễn Thái Ngọc Duy329e6e82015-09-19 12:13:23 +0700645 daemonized = !daemonize();
Nguyễn Thái Ngọc Duy62aad182014-05-25 07:38:29 +0700646 }
Nguyễn Thái Ngọc Duyae4e89e2018-04-15 17:36:14 +0200647 } else {
648 struct string_list keep_pack = STRING_LIST_INIT_NODUP;
649
Ævar Arnfjörð Bjarmason793c1462020-11-20 12:55:22 +0100650 if (keep_largest_pack != -1) {
651 if (keep_largest_pack)
Nguyễn Thái Ngọc Duy55dfe132018-04-15 17:36:15 +0200652 find_base_packs(&keep_pack, 0);
653 } else if (big_pack_threshold) {
654 find_base_packs(&keep_pack, big_pack_threshold);
Nguyễn Thái Ngọc Duyae4e89e2018-04-15 17:36:14 +0200655 }
656
657 add_repack_all_option(&keep_pack);
658 string_list_clear(&keep_pack, 0);
659 }
Junio C Hamano2c3c4392007-09-05 13:01:37 -0700660
Nguyễn Thái Ngọc Duy64a99eb2013-08-08 18:05:38 +0700661 name = lock_repo_for_gc(force, &pid);
662 if (name) {
663 if (auto_gc)
664 return 0; /* be quiet on --auto */
665 die(_("gc is already running on machine '%s' pid %"PRIuMAX" (use --force if not)"),
666 name, (uintmax_t)pid);
667 }
668
Nguyễn Thái Ngọc Duy329e6e82015-09-19 12:13:23 +0700669 if (daemonized) {
670 hold_lock_file_for_update(&log_lock,
671 git_path("gc.log"),
672 LOCK_DIE_ON_ERROR);
Junio C Hamano076c8272015-10-15 15:43:32 -0700673 dup2(get_lock_file_fd(&log_lock), 2);
Nguyễn Thái Ngọc Duy329e6e82015-09-19 12:13:23 +0700674 sigchain_push_common(process_log_file_on_signal);
675 atexit(process_log_file_at_exit);
676 }
677
Jonathan Niederfec2ed22018-07-16 23:54:16 -0700678 gc_before_repack();
James Bowes6757ada2007-03-13 21:58:22 -0400679
Jeff King067fbd42015-06-23 06:54:11 -0400680 if (!repository_format_precious_objects) {
Johannes Schindelinc4dee2c2021-09-09 09:47:08 +0000681 if (run_command_v_opt(repack.v,
682 RUN_GIT_CMD | RUN_CLOSE_OBJECT_STORE))
Jeff Kingd70a9eb2020-07-28 20:37:20 -0400683 die(FAILED_RUN, repack.v[0]);
James Bowes6757ada2007-03-13 21:58:22 -0400684
Jeff King067fbd42015-06-23 06:54:11 -0400685 if (prune_expire) {
Taylor Blau5b924772022-05-20 19:18:14 -0400686 /* run `git prune` even if using cruft packs */
Jeff King22f9b7f2020-07-28 16:24:27 -0400687 strvec_push(&prune, prune_expire);
Jeff King067fbd42015-06-23 06:54:11 -0400688 if (quiet)
Jeff King22f9b7f2020-07-28 16:24:27 -0400689 strvec_push(&prune, "--no-progress");
Christian Couderb14ed5a2019-06-25 15:40:31 +0200690 if (has_promisor_remote())
Jeff King22f9b7f2020-07-28 16:24:27 -0400691 strvec_push(&prune,
Jeff Kingf6d89422020-07-28 16:26:31 -0400692 "--exclude-promisor-objects");
Jeff Kingd70a9eb2020-07-28 20:37:20 -0400693 if (run_command_v_opt(prune.v, RUN_GIT_CMD))
694 die(FAILED_RUN, prune.v[0]);
Jeff King067fbd42015-06-23 06:54:11 -0400695 }
Johannes Schindelin58e9d9d2009-02-14 23:10:10 +0100696 }
James Bowes6757ada2007-03-13 21:58:22 -0400697
Nguyễn Thái Ngọc Duye3df33b2014-11-30 15:24:53 +0700698 if (prune_worktrees_expire) {
Jeff King22f9b7f2020-07-28 16:24:27 -0400699 strvec_push(&prune_worktrees, prune_worktrees_expire);
Jeff Kingd70a9eb2020-07-28 20:37:20 -0400700 if (run_command_v_opt(prune_worktrees.v, RUN_GIT_CMD))
701 die(FAILED_RUN, prune_worktrees.v[0]);
Nguyễn Thái Ngọc Duye3df33b2014-11-30 15:24:53 +0700702 }
703
Jeff Kingd70a9eb2020-07-28 20:37:20 -0400704 if (run_command_v_opt(rerere.v, RUN_GIT_CMD))
705 die(FAILED_RUN, rerere.v[0]);
James Bowes6757ada2007-03-13 21:58:22 -0400706
Doug Kelly478f34d2015-11-03 21:05:08 -0600707 report_garbage = report_pack_garbage;
Stefan Bellera49d2832018-03-23 18:45:21 +0100708 reprepare_packed_git(the_repository);
Johannes Schindelin5bdece02018-12-15 14:04:01 -0800709 if (pack_garbage.nr > 0) {
Derrick Stolee2d511cf2019-05-17 11:41:49 -0700710 close_object_store(the_repository->objects);
Doug Kelly478f34d2015-11-03 21:05:08 -0600711 clean_pack_garbage();
Johannes Schindelin5bdece02018-12-15 14:04:01 -0800712 }
Doug Kelly478f34d2015-11-03 21:05:08 -0600713
Derrick Stolee7211b9e2019-08-13 11:37:43 -0700714 prepare_repo_settings(the_repository);
715 if (the_repository->settings.gc_write_commit_graph == 1)
Taylor Blau0bd52e22020-02-03 21:51:50 -0800716 write_commit_graph_reachable(the_repository->objects->odb,
Junio C Hamanof4f8dfe2019-09-09 12:26:36 -0700717 !quiet && !daemonized ? COMMIT_GRAPH_WRITE_PROGRESS : 0,
Derrick Stolee7211b9e2019-08-13 11:37:43 -0700718 NULL);
Derrick Stoleed5d5d7b2018-06-27 09:24:46 -0400719
Junio C Hamanoa087cc92007-09-17 00:44:17 -0700720 if (auto_gc && too_many_loose_objects())
Ævar Arnfjörð Bjarmasonfea61282011-02-22 23:42:24 +0000721 warning(_("There are too many unreachable loose objects; "
722 "run 'git prune' to remove them."));
Junio C Hamanoa087cc92007-09-17 00:44:17 -0700723
David Turnera831c062017-02-10 16:28:22 -0500724 if (!daemonized)
725 unlink(git_path("gc.log"));
726
James Bowes6757ada2007-03-13 21:58:22 -0400727 return 0;
728}
Derrick Stolee2057d752020-09-17 18:11:42 +0000729
Derrick Stoleeb08ff1f2020-09-11 17:49:15 +0000730static const char *const builtin_maintenance_run_usage[] = {
731 N_("git maintenance run [--auto] [--[no-]quiet] [--task=<task>] [--schedule]"),
Derrick Stolee2057d752020-09-17 18:11:42 +0000732 NULL
733};
734
Derrick Stoleeb08ff1f2020-09-11 17:49:15 +0000735enum schedule_priority {
736 SCHEDULE_NONE = 0,
737 SCHEDULE_WEEKLY = 1,
738 SCHEDULE_DAILY = 2,
739 SCHEDULE_HOURLY = 3,
740};
741
742static enum schedule_priority parse_schedule(const char *value)
743{
744 if (!value)
745 return SCHEDULE_NONE;
746 if (!strcasecmp(value, "hourly"))
747 return SCHEDULE_HOURLY;
748 if (!strcasecmp(value, "daily"))
749 return SCHEDULE_DAILY;
750 if (!strcasecmp(value, "weekly"))
751 return SCHEDULE_WEEKLY;
752 return SCHEDULE_NONE;
753}
754
755static int maintenance_opt_schedule(const struct option *opt, const char *arg,
756 int unset)
757{
758 enum schedule_priority *priority = opt->value;
759
760 if (unset)
761 die(_("--no-schedule is not allowed"));
762
763 *priority = parse_schedule(arg);
764
765 if (!*priority)
766 die(_("unrecognized --schedule argument '%s'"), arg);
767
768 return 0;
769}
770
Derrick Stolee2057d752020-09-17 18:11:42 +0000771struct maintenance_run_opts {
772 int auto_flag;
Derrick Stolee3ddaad02020-09-17 18:11:43 +0000773 int quiet;
Derrick Stoleeb08ff1f2020-09-11 17:49:15 +0000774 enum schedule_priority schedule;
Derrick Stolee2057d752020-09-17 18:11:42 +0000775};
776
Derrick Stolee4ddc79b2020-09-17 18:11:51 +0000777/* Remember to update object flag allocation in object.h */
778#define SEEN (1u<<0)
779
780struct cg_auto_data {
781 int num_not_in_graph;
782 int limit;
783};
784
785static int dfs_on_ref(const char *refname,
786 const struct object_id *oid, int flags,
787 void *cb_data)
788{
789 struct cg_auto_data *data = (struct cg_auto_data *)cb_data;
790 int result = 0;
791 struct object_id peeled;
792 struct commit_list *stack = NULL;
793 struct commit *commit;
794
Jeff King36a31792021-01-20 14:44:43 -0500795 if (!peel_iterated_oid(oid, &peeled))
Derrick Stolee4ddc79b2020-09-17 18:11:51 +0000796 oid = &peeled;
797 if (oid_object_info(the_repository, oid, NULL) != OBJ_COMMIT)
798 return 0;
799
800 commit = lookup_commit(the_repository, oid);
801 if (!commit)
802 return 0;
Derrick Stolee8f801802020-10-08 00:50:39 +0000803 if (parse_commit(commit) ||
804 commit_graph_position(commit) != COMMIT_NOT_FROM_GRAPH)
Derrick Stolee4ddc79b2020-09-17 18:11:51 +0000805 return 0;
806
Derrick Stolee8f801802020-10-08 00:50:39 +0000807 data->num_not_in_graph++;
808
809 if (data->num_not_in_graph >= data->limit)
810 return 1;
811
Derrick Stolee4ddc79b2020-09-17 18:11:51 +0000812 commit_list_append(commit, &stack);
813
814 while (!result && stack) {
815 struct commit_list *parent;
816
817 commit = pop_commit(&stack);
818
819 for (parent = commit->parents; parent; parent = parent->next) {
820 if (parse_commit(parent->item) ||
821 commit_graph_position(parent->item) != COMMIT_NOT_FROM_GRAPH ||
822 parent->item->object.flags & SEEN)
823 continue;
824
825 parent->item->object.flags |= SEEN;
826 data->num_not_in_graph++;
827
828 if (data->num_not_in_graph >= data->limit) {
829 result = 1;
830 break;
831 }
832
833 commit_list_append(parent->item, &stack);
834 }
835 }
836
837 free_commit_list(stack);
838 return result;
839}
840
841static int should_write_commit_graph(void)
842{
843 int result;
844 struct cg_auto_data data;
845
846 data.num_not_in_graph = 0;
847 data.limit = 100;
848 git_config_get_int("maintenance.commit-graph.auto",
849 &data.limit);
850
851 if (!data.limit)
852 return 0;
853 if (data.limit < 0)
854 return 1;
855
856 result = for_each_ref(dfs_on_ref, &data);
857
René Scharfecd888842020-10-31 13:46:08 +0100858 repo_clear_commit_marks(the_repository, SEEN);
Derrick Stolee4ddc79b2020-09-17 18:11:51 +0000859
860 return result;
861}
862
Derrick Stolee663b2b12020-09-17 18:11:46 +0000863static int run_write_commit_graph(struct maintenance_run_opts *opts)
864{
865 struct child_process child = CHILD_PROCESS_INIT;
866
Johannes Schindelinc4dee2c2021-09-09 09:47:08 +0000867 child.git_cmd = child.close_object_store = 1;
Derrick Stolee663b2b12020-09-17 18:11:46 +0000868 strvec_pushl(&child.args, "commit-graph", "write",
869 "--split", "--reachable", NULL);
870
871 if (opts->quiet)
872 strvec_push(&child.args, "--no-progress");
873
874 return !!run_command(&child);
875}
876
877static int maintenance_task_commit_graph(struct maintenance_run_opts *opts)
878{
Derrick Stoleed3341072020-10-12 13:28:34 +0000879 prepare_repo_settings(the_repository);
880 if (!the_repository->settings.core_commit_graph)
881 return 0;
882
Derrick Stolee663b2b12020-09-17 18:11:46 +0000883 if (run_write_commit_graph(opts)) {
884 error(_("failed to write commit-graph"));
885 return 1;
886 }
887
888 return 0;
889}
890
Derrick Stoleea039a1f2021-04-06 18:47:46 +0000891static int fetch_remote(struct remote *remote, void *cbdata)
Derrick Stolee28cb5e62020-09-25 12:33:31 +0000892{
Derrick Stoleea039a1f2021-04-06 18:47:46 +0000893 struct maintenance_run_opts *opts = cbdata;
Derrick Stolee28cb5e62020-09-25 12:33:31 +0000894 struct child_process child = CHILD_PROCESS_INIT;
895
Derrick Stolee32f67882021-04-16 12:49:59 +0000896 if (remote->skip_default_update)
897 return 0;
898
Derrick Stolee28cb5e62020-09-25 12:33:31 +0000899 child.git_cmd = 1;
Derrick Stoleecfd781e2021-04-16 12:49:58 +0000900 strvec_pushl(&child.args, "fetch", remote->name,
901 "--prefetch", "--prune", "--no-tags",
Derrick Stolee28cb5e62020-09-25 12:33:31 +0000902 "--no-write-fetch-head", "--recurse-submodules=no",
Derrick Stoleecfd781e2021-04-16 12:49:58 +0000903 NULL);
Derrick Stolee28cb5e62020-09-25 12:33:31 +0000904
905 if (opts->quiet)
906 strvec_push(&child.args, "--quiet");
907
Derrick Stolee28cb5e62020-09-25 12:33:31 +0000908 return !!run_command(&child);
909}
910
Derrick Stolee28cb5e62020-09-25 12:33:31 +0000911static int maintenance_task_prefetch(struct maintenance_run_opts *opts)
912{
Derrick Stolee96eaffe2021-01-19 12:52:03 +0000913 git_config_set_multivar_gently("log.excludedecoration",
914 "refs/prefetch/",
915 "refs/prefetch/",
916 CONFIG_FLAGS_FIXED_VALUE |
917 CONFIG_FLAGS_MULTI_REPLACE);
918
Derrick Stoleea039a1f2021-04-06 18:47:46 +0000919 if (for_each_remote(fetch_remote, opts)) {
920 error(_("failed to prefetch remotes"));
921 return 1;
Derrick Stolee28cb5e62020-09-25 12:33:31 +0000922 }
923
Derrick Stoleea039a1f2021-04-06 18:47:46 +0000924 return 0;
Derrick Stolee28cb5e62020-09-25 12:33:31 +0000925}
926
Derrick Stolee2057d752020-09-17 18:11:42 +0000927static int maintenance_task_gc(struct maintenance_run_opts *opts)
928{
929 struct child_process child = CHILD_PROCESS_INIT;
930
Johannes Schindelinc4dee2c2021-09-09 09:47:08 +0000931 child.git_cmd = child.close_object_store = 1;
Derrick Stolee2057d752020-09-17 18:11:42 +0000932 strvec_push(&child.args, "gc");
933
934 if (opts->auto_flag)
935 strvec_push(&child.args, "--auto");
Derrick Stolee3ddaad02020-09-17 18:11:43 +0000936 if (opts->quiet)
937 strvec_push(&child.args, "--quiet");
938 else
939 strvec_push(&child.args, "--no-quiet");
Derrick Stolee2057d752020-09-17 18:11:42 +0000940
Derrick Stolee2057d752020-09-17 18:11:42 +0000941 return run_command(&child);
942}
943
Derrick Stolee252cfb72020-09-25 12:33:32 +0000944static int prune_packed(struct maintenance_run_opts *opts)
945{
946 struct child_process child = CHILD_PROCESS_INIT;
947
948 child.git_cmd = 1;
949 strvec_push(&child.args, "prune-packed");
950
951 if (opts->quiet)
952 strvec_push(&child.args, "--quiet");
953
954 return !!run_command(&child);
955}
956
957struct write_loose_object_data {
958 FILE *in;
959 int count;
960 int batch_size;
961};
962
Derrick Stolee3e220e62020-09-25 12:33:33 +0000963static int loose_object_auto_limit = 100;
964
965static int loose_object_count(const struct object_id *oid,
966 const char *path,
967 void *data)
968{
969 int *count = (int*)data;
970 if (++(*count) >= loose_object_auto_limit)
971 return 1;
972 return 0;
973}
974
975static int loose_object_auto_condition(void)
976{
977 int count = 0;
978
979 git_config_get_int("maintenance.loose-objects.auto",
980 &loose_object_auto_limit);
981
982 if (!loose_object_auto_limit)
983 return 0;
984 if (loose_object_auto_limit < 0)
985 return 1;
986
987 return for_each_loose_file_in_objdir(the_repository->objects->odb->path,
988 loose_object_count,
989 NULL, NULL, &count);
990}
991
Derrick Stolee252cfb72020-09-25 12:33:32 +0000992static int bail_on_loose(const struct object_id *oid,
993 const char *path,
994 void *data)
995{
996 return 1;
997}
998
999static int write_loose_object_to_stdin(const struct object_id *oid,
1000 const char *path,
1001 void *data)
1002{
1003 struct write_loose_object_data *d = (struct write_loose_object_data *)data;
1004
1005 fprintf(d->in, "%s\n", oid_to_hex(oid));
1006
1007 return ++(d->count) > d->batch_size;
1008}
1009
1010static int pack_loose(struct maintenance_run_opts *opts)
1011{
1012 struct repository *r = the_repository;
1013 int result = 0;
1014 struct write_loose_object_data data;
1015 struct child_process pack_proc = CHILD_PROCESS_INIT;
1016
1017 /*
1018 * Do not start pack-objects process
1019 * if there are no loose objects.
1020 */
1021 if (!for_each_loose_file_in_objdir(r->objects->odb->path,
1022 bail_on_loose,
1023 NULL, NULL, NULL))
1024 return 0;
1025
1026 pack_proc.git_cmd = 1;
1027
1028 strvec_push(&pack_proc.args, "pack-objects");
1029 if (opts->quiet)
1030 strvec_push(&pack_proc.args, "--quiet");
1031 strvec_pushf(&pack_proc.args, "%s/pack/loose", r->objects->odb->path);
1032
1033 pack_proc.in = -1;
1034
1035 if (start_command(&pack_proc)) {
1036 error(_("failed to start 'git pack-objects' process"));
1037 return 1;
1038 }
1039
1040 data.in = xfdopen(pack_proc.in, "w");
1041 data.count = 0;
1042 data.batch_size = 50000;
1043
1044 for_each_loose_file_in_objdir(r->objects->odb->path,
1045 write_loose_object_to_stdin,
1046 NULL,
1047 NULL,
1048 &data);
1049
1050 fclose(data.in);
1051
1052 if (finish_command(&pack_proc)) {
1053 error(_("failed to finish 'git pack-objects' process"));
1054 result = 1;
1055 }
1056
1057 return result;
1058}
1059
1060static int maintenance_task_loose_objects(struct maintenance_run_opts *opts)
1061{
1062 return prune_packed(opts) || pack_loose(opts);
1063}
1064
Derrick Stoleee841a792020-09-25 12:33:38 +00001065static int incremental_repack_auto_condition(void)
1066{
1067 struct packed_git *p;
Derrick Stoleee841a792020-09-25 12:33:38 +00001068 int incremental_repack_auto_limit = 10;
1069 int count = 0;
1070
Glen Chooa897ab72021-10-15 13:16:31 -07001071 prepare_repo_settings(the_repository);
1072 if (!the_repository->settings.core_multi_pack_index)
Derrick Stoleee841a792020-09-25 12:33:38 +00001073 return 0;
1074
1075 git_config_get_int("maintenance.incremental-repack.auto",
1076 &incremental_repack_auto_limit);
1077
1078 if (!incremental_repack_auto_limit)
1079 return 0;
1080 if (incremental_repack_auto_limit < 0)
1081 return 1;
1082
1083 for (p = get_packed_git(the_repository);
1084 count < incremental_repack_auto_limit && p;
1085 p = p->next) {
1086 if (!p->multi_pack_index)
1087 count++;
1088 }
1089
1090 return count >= incremental_repack_auto_limit;
1091}
1092
Derrick Stolee52fe41f2020-09-25 12:33:36 +00001093static int multi_pack_index_write(struct maintenance_run_opts *opts)
1094{
1095 struct child_process child = CHILD_PROCESS_INIT;
1096
1097 child.git_cmd = 1;
1098 strvec_pushl(&child.args, "multi-pack-index", "write", NULL);
1099
1100 if (opts->quiet)
1101 strvec_push(&child.args, "--no-progress");
1102
1103 if (run_command(&child))
1104 return error(_("failed to write multi-pack-index"));
1105
1106 return 0;
1107}
1108
1109static int multi_pack_index_expire(struct maintenance_run_opts *opts)
1110{
1111 struct child_process child = CHILD_PROCESS_INIT;
1112
Johannes Schindelinc4dee2c2021-09-09 09:47:08 +00001113 child.git_cmd = child.close_object_store = 1;
Derrick Stolee52fe41f2020-09-25 12:33:36 +00001114 strvec_pushl(&child.args, "multi-pack-index", "expire", NULL);
1115
1116 if (opts->quiet)
1117 strvec_push(&child.args, "--no-progress");
1118
Derrick Stolee52fe41f2020-09-25 12:33:36 +00001119 if (run_command(&child))
1120 return error(_("'git multi-pack-index expire' failed"));
1121
1122 return 0;
1123}
1124
Derrick Stoleea13e3d02020-09-25 12:33:37 +00001125#define TWO_GIGABYTES (INT32_MAX)
1126
1127static off_t get_auto_pack_size(void)
1128{
1129 /*
1130 * The "auto" value is special: we optimize for
1131 * one large pack-file (i.e. from a clone) and
1132 * expect the rest to be small and they can be
1133 * repacked quickly.
1134 *
1135 * The strategy we select here is to select a
1136 * size that is one more than the second largest
1137 * pack-file. This ensures that we will repack
1138 * at least two packs if there are three or more
1139 * packs.
1140 */
1141 off_t max_size = 0;
1142 off_t second_largest_size = 0;
1143 off_t result_size;
1144 struct packed_git *p;
1145 struct repository *r = the_repository;
1146
1147 reprepare_packed_git(r);
1148 for (p = get_all_packs(r); p; p = p->next) {
1149 if (p->pack_size > max_size) {
1150 second_largest_size = max_size;
1151 max_size = p->pack_size;
1152 } else if (p->pack_size > second_largest_size)
1153 second_largest_size = p->pack_size;
1154 }
1155
1156 result_size = second_largest_size + 1;
1157
1158 /* But limit ourselves to a batch size of 2g */
1159 if (result_size > TWO_GIGABYTES)
1160 result_size = TWO_GIGABYTES;
1161
1162 return result_size;
1163}
1164
Derrick Stolee52fe41f2020-09-25 12:33:36 +00001165static int multi_pack_index_repack(struct maintenance_run_opts *opts)
1166{
1167 struct child_process child = CHILD_PROCESS_INIT;
1168
Johannes Schindelinc4dee2c2021-09-09 09:47:08 +00001169 child.git_cmd = child.close_object_store = 1;
Derrick Stolee52fe41f2020-09-25 12:33:36 +00001170 strvec_pushl(&child.args, "multi-pack-index", "repack", NULL);
1171
1172 if (opts->quiet)
1173 strvec_push(&child.args, "--no-progress");
1174
Derrick Stoleea13e3d02020-09-25 12:33:37 +00001175 strvec_pushf(&child.args, "--batch-size=%"PRIuMAX,
1176 (uintmax_t)get_auto_pack_size());
Derrick Stolee52fe41f2020-09-25 12:33:36 +00001177
Derrick Stolee52fe41f2020-09-25 12:33:36 +00001178 if (run_command(&child))
1179 return error(_("'git multi-pack-index repack' failed"));
1180
1181 return 0;
1182}
1183
1184static int maintenance_task_incremental_repack(struct maintenance_run_opts *opts)
1185{
1186 prepare_repo_settings(the_repository);
1187 if (!the_repository->settings.core_multi_pack_index) {
1188 warning(_("skipping incremental-repack task because core.multiPackIndex is disabled"));
1189 return 0;
1190 }
1191
1192 if (multi_pack_index_write(opts))
1193 return 1;
1194 if (multi_pack_index_expire(opts))
1195 return 1;
1196 if (multi_pack_index_repack(opts))
1197 return 1;
1198 return 0;
1199}
1200
Derrick Stolee3103e982020-09-17 18:11:45 +00001201typedef int maintenance_task_fn(struct maintenance_run_opts *opts);
1202
Derrick Stolee916d0622020-09-17 18:11:50 +00001203/*
1204 * An auto condition function returns 1 if the task should run
1205 * and 0 if the task should NOT run. See needs_to_gc() for an
1206 * example.
1207 */
1208typedef int maintenance_auto_fn(void);
1209
Derrick Stolee3103e982020-09-17 18:11:45 +00001210struct maintenance_task {
1211 const char *name;
1212 maintenance_task_fn *fn;
Derrick Stolee916d0622020-09-17 18:11:50 +00001213 maintenance_auto_fn *auto_condition;
Derrick Stolee3103e982020-09-17 18:11:45 +00001214 unsigned enabled:1;
Derrick Stolee090511b2020-09-17 18:11:47 +00001215
Derrick Stoleeb08ff1f2020-09-11 17:49:15 +00001216 enum schedule_priority schedule;
1217
Derrick Stolee090511b2020-09-17 18:11:47 +00001218 /* -1 if not selected. */
1219 int selected_order;
Derrick Stolee3103e982020-09-17 18:11:45 +00001220};
1221
1222enum maintenance_task_label {
Derrick Stolee28cb5e62020-09-25 12:33:31 +00001223 TASK_PREFETCH,
Derrick Stolee252cfb72020-09-25 12:33:32 +00001224 TASK_LOOSE_OBJECTS,
Derrick Stolee52fe41f2020-09-25 12:33:36 +00001225 TASK_INCREMENTAL_REPACK,
Derrick Stolee3103e982020-09-17 18:11:45 +00001226 TASK_GC,
Derrick Stolee663b2b12020-09-17 18:11:46 +00001227 TASK_COMMIT_GRAPH,
Derrick Stolee41abfe12021-02-09 13:42:28 +00001228 TASK_PACK_REFS,
Derrick Stolee3103e982020-09-17 18:11:45 +00001229
1230 /* Leave as final value */
1231 TASK__COUNT
1232};
1233
1234static struct maintenance_task tasks[] = {
Derrick Stolee28cb5e62020-09-25 12:33:31 +00001235 [TASK_PREFETCH] = {
1236 "prefetch",
1237 maintenance_task_prefetch,
1238 },
Derrick Stolee252cfb72020-09-25 12:33:32 +00001239 [TASK_LOOSE_OBJECTS] = {
1240 "loose-objects",
1241 maintenance_task_loose_objects,
Derrick Stolee3e220e62020-09-25 12:33:33 +00001242 loose_object_auto_condition,
Derrick Stolee252cfb72020-09-25 12:33:32 +00001243 },
Derrick Stolee52fe41f2020-09-25 12:33:36 +00001244 [TASK_INCREMENTAL_REPACK] = {
1245 "incremental-repack",
1246 maintenance_task_incremental_repack,
Derrick Stoleee841a792020-09-25 12:33:38 +00001247 incremental_repack_auto_condition,
Derrick Stolee52fe41f2020-09-25 12:33:36 +00001248 },
Derrick Stolee3103e982020-09-17 18:11:45 +00001249 [TASK_GC] = {
1250 "gc",
1251 maintenance_task_gc,
Derrick Stolee916d0622020-09-17 18:11:50 +00001252 need_to_gc,
Derrick Stolee3103e982020-09-17 18:11:45 +00001253 1,
1254 },
Derrick Stolee663b2b12020-09-17 18:11:46 +00001255 [TASK_COMMIT_GRAPH] = {
1256 "commit-graph",
1257 maintenance_task_commit_graph,
Derrick Stolee4ddc79b2020-09-17 18:11:51 +00001258 should_write_commit_graph,
Derrick Stolee663b2b12020-09-17 18:11:46 +00001259 },
Derrick Stolee41abfe12021-02-09 13:42:28 +00001260 [TASK_PACK_REFS] = {
1261 "pack-refs",
1262 maintenance_task_pack_refs,
1263 NULL,
1264 },
Derrick Stolee3103e982020-09-17 18:11:45 +00001265};
1266
Derrick Stolee090511b2020-09-17 18:11:47 +00001267static int compare_tasks_by_selection(const void *a_, const void *b_)
1268{
René Scharfea1c74792020-11-17 22:59:49 +01001269 const struct maintenance_task *a = a_;
1270 const struct maintenance_task *b = b_;
Derrick Stolee090511b2020-09-17 18:11:47 +00001271
1272 return b->selected_order - a->selected_order;
1273}
1274
Derrick Stolee3103e982020-09-17 18:11:45 +00001275static int maintenance_run_tasks(struct maintenance_run_opts *opts)
1276{
Derrick Stolee090511b2020-09-17 18:11:47 +00001277 int i, found_selected = 0;
Derrick Stolee3103e982020-09-17 18:11:45 +00001278 int result = 0;
Derrick Stoleed7514f62020-09-17 18:11:48 +00001279 struct lock_file lk;
1280 struct repository *r = the_repository;
1281 char *lock_path = xstrfmt("%s/maintenance", r->objects->odb->path);
1282
1283 if (hold_lock_file_for_update(&lk, lock_path, LOCK_NO_DEREF) < 0) {
1284 /*
1285 * Another maintenance command is running.
1286 *
1287 * If --auto was provided, then it is likely due to a
1288 * recursive process stack. Do not report an error in
1289 * that case.
1290 */
1291 if (!opts->auto_flag && !opts->quiet)
1292 warning(_("lock file '%s' exists, skipping maintenance"),
1293 lock_path);
1294 free(lock_path);
1295 return 0;
1296 }
1297 free(lock_path);
Derrick Stolee3103e982020-09-17 18:11:45 +00001298
Derrick Stolee090511b2020-09-17 18:11:47 +00001299 for (i = 0; !found_selected && i < TASK__COUNT; i++)
1300 found_selected = tasks[i].selected_order >= 0;
1301
1302 if (found_selected)
1303 QSORT(tasks, TASK__COUNT, compare_tasks_by_selection);
1304
Derrick Stolee3103e982020-09-17 18:11:45 +00001305 for (i = 0; i < TASK__COUNT; i++) {
Derrick Stolee090511b2020-09-17 18:11:47 +00001306 if (found_selected && tasks[i].selected_order < 0)
1307 continue;
1308
1309 if (!found_selected && !tasks[i].enabled)
Derrick Stolee3103e982020-09-17 18:11:45 +00001310 continue;
1311
Derrick Stolee916d0622020-09-17 18:11:50 +00001312 if (opts->auto_flag &&
1313 (!tasks[i].auto_condition ||
1314 !tasks[i].auto_condition()))
1315 continue;
1316
Derrick Stoleeb08ff1f2020-09-11 17:49:15 +00001317 if (opts->schedule && tasks[i].schedule < opts->schedule)
1318 continue;
1319
Derrick Stolee25914c42020-09-17 18:11:52 +00001320 trace2_region_enter("maintenance", tasks[i].name, r);
Derrick Stolee3103e982020-09-17 18:11:45 +00001321 if (tasks[i].fn(opts)) {
1322 error(_("task '%s' failed"), tasks[i].name);
1323 result = 1;
1324 }
Derrick Stolee25914c42020-09-17 18:11:52 +00001325 trace2_region_leave("maintenance", tasks[i].name, r);
Derrick Stolee3103e982020-09-17 18:11:45 +00001326 }
1327
Derrick Stoleed7514f62020-09-17 18:11:48 +00001328 rollback_lock_file(&lk);
Derrick Stolee3103e982020-09-17 18:11:45 +00001329 return result;
1330}
1331
Derrick Stoleea4cb1a22020-10-15 17:22:02 +00001332static void initialize_maintenance_strategy(void)
1333{
1334 char *config_str;
1335
1336 if (git_config_get_string("maintenance.strategy", &config_str))
1337 return;
1338
1339 if (!strcasecmp(config_str, "incremental")) {
1340 tasks[TASK_GC].schedule = SCHEDULE_NONE;
1341 tasks[TASK_COMMIT_GRAPH].enabled = 1;
1342 tasks[TASK_COMMIT_GRAPH].schedule = SCHEDULE_HOURLY;
1343 tasks[TASK_PREFETCH].enabled = 1;
1344 tasks[TASK_PREFETCH].schedule = SCHEDULE_HOURLY;
1345 tasks[TASK_INCREMENTAL_REPACK].enabled = 1;
1346 tasks[TASK_INCREMENTAL_REPACK].schedule = SCHEDULE_DAILY;
1347 tasks[TASK_LOOSE_OBJECTS].enabled = 1;
1348 tasks[TASK_LOOSE_OBJECTS].schedule = SCHEDULE_DAILY;
Derrick Stoleeacc1c4d2021-02-09 13:42:29 +00001349 tasks[TASK_PACK_REFS].enabled = 1;
1350 tasks[TASK_PACK_REFS].schedule = SCHEDULE_WEEKLY;
Derrick Stoleea4cb1a22020-10-15 17:22:02 +00001351 }
1352}
1353
1354static void initialize_task_config(int schedule)
Derrick Stolee65d655b2020-09-17 18:11:49 +00001355{
1356 int i;
1357 struct strbuf config_name = STRBUF_INIT;
Derrick Stolee916d0622020-09-17 18:11:50 +00001358 gc_config();
1359
Derrick Stoleea4cb1a22020-10-15 17:22:02 +00001360 if (schedule)
1361 initialize_maintenance_strategy();
1362
Derrick Stolee65d655b2020-09-17 18:11:49 +00001363 for (i = 0; i < TASK__COUNT; i++) {
1364 int config_value;
Derrick Stoleeb08ff1f2020-09-11 17:49:15 +00001365 char *config_str;
Derrick Stolee65d655b2020-09-17 18:11:49 +00001366
Derrick Stoleeb08ff1f2020-09-11 17:49:15 +00001367 strbuf_reset(&config_name);
Derrick Stolee65d655b2020-09-17 18:11:49 +00001368 strbuf_addf(&config_name, "maintenance.%s.enabled",
1369 tasks[i].name);
1370
1371 if (!git_config_get_bool(config_name.buf, &config_value))
1372 tasks[i].enabled = config_value;
Derrick Stoleeb08ff1f2020-09-11 17:49:15 +00001373
1374 strbuf_reset(&config_name);
1375 strbuf_addf(&config_name, "maintenance.%s.schedule",
1376 tasks[i].name);
1377
1378 if (!git_config_get_string(config_name.buf, &config_str)) {
1379 tasks[i].schedule = parse_schedule(config_str);
1380 free(config_str);
1381 }
Derrick Stolee65d655b2020-09-17 18:11:49 +00001382 }
1383
1384 strbuf_release(&config_name);
1385}
1386
Derrick Stolee090511b2020-09-17 18:11:47 +00001387static int task_option_parse(const struct option *opt,
1388 const char *arg, int unset)
1389{
1390 int i, num_selected = 0;
1391 struct maintenance_task *task = NULL;
1392
1393 BUG_ON_OPT_NEG(unset);
1394
1395 for (i = 0; i < TASK__COUNT; i++) {
1396 if (tasks[i].selected_order >= 0)
1397 num_selected++;
1398 if (!strcasecmp(tasks[i].name, arg)) {
1399 task = &tasks[i];
1400 }
1401 }
1402
1403 if (!task) {
1404 error(_("'%s' is not a valid task"), arg);
1405 return 1;
1406 }
1407
1408 if (task->selected_order >= 0) {
1409 error(_("task '%s' cannot be selected multiple times"), arg);
1410 return 1;
1411 }
1412
1413 task->selected_order = num_selected + 1;
1414
1415 return 0;
1416}
1417
Derrick Stolee2057d752020-09-17 18:11:42 +00001418static int maintenance_run(int argc, const char **argv, const char *prefix)
1419{
Derrick Stolee090511b2020-09-17 18:11:47 +00001420 int i;
Derrick Stolee2057d752020-09-17 18:11:42 +00001421 struct maintenance_run_opts opts;
1422 struct option builtin_maintenance_run_options[] = {
1423 OPT_BOOL(0, "auto", &opts.auto_flag,
1424 N_("run tasks based on the state of the repository")),
Derrick Stoleeb08ff1f2020-09-11 17:49:15 +00001425 OPT_CALLBACK(0, "schedule", &opts.schedule, N_("frequency"),
1426 N_("run tasks based on frequency"),
1427 maintenance_opt_schedule),
Derrick Stolee3ddaad02020-09-17 18:11:43 +00001428 OPT_BOOL(0, "quiet", &opts.quiet,
1429 N_("do not report progress or other information over stderr")),
Derrick Stolee090511b2020-09-17 18:11:47 +00001430 OPT_CALLBACK_F(0, "task", NULL, N_("task"),
1431 N_("run a specific task"),
1432 PARSE_OPT_NONEG, task_option_parse),
Derrick Stolee2057d752020-09-17 18:11:42 +00001433 OPT_END()
1434 };
1435 memset(&opts, 0, sizeof(opts));
1436
Derrick Stolee3ddaad02020-09-17 18:11:43 +00001437 opts.quiet = !isatty(2);
1438
Derrick Stolee090511b2020-09-17 18:11:47 +00001439 for (i = 0; i < TASK__COUNT; i++)
1440 tasks[i].selected_order = -1;
1441
Derrick Stolee2057d752020-09-17 18:11:42 +00001442 argc = parse_options(argc, argv, prefix,
1443 builtin_maintenance_run_options,
1444 builtin_maintenance_run_usage,
1445 PARSE_OPT_STOP_AT_NON_OPTION);
1446
Derrick Stoleeb08ff1f2020-09-11 17:49:15 +00001447 if (opts.auto_flag && opts.schedule)
1448 die(_("use at most one of --auto and --schedule=<frequency>"));
1449
Derrick Stoleea4cb1a22020-10-15 17:22:02 +00001450 initialize_task_config(opts.schedule);
1451
Derrick Stolee2057d752020-09-17 18:11:42 +00001452 if (argc != 0)
1453 usage_with_options(builtin_maintenance_run_usage,
1454 builtin_maintenance_run_options);
Derrick Stolee3103e982020-09-17 18:11:45 +00001455 return maintenance_run_tasks(&opts);
Derrick Stolee2057d752020-09-17 18:11:42 +00001456}
1457
Eric Sunshine26c79742021-02-23 02:31:07 -05001458static char *get_maintpath(void)
1459{
1460 struct strbuf sb = STRBUF_INIT;
1461 const char *p = the_repository->worktree ?
1462 the_repository->worktree : the_repository->gitdir;
1463
1464 strbuf_realpath(&sb, p, 1);
1465 return strbuf_detach(&sb, NULL);
1466}
1467
Derrick Stolee0c18b702020-09-11 17:49:17 +00001468static int maintenance_register(void)
1469{
Eric Sunshine26c79742021-02-23 02:31:07 -05001470 int rc;
Derrick Stolee61f7a382020-10-15 17:22:03 +00001471 char *config_value;
Derrick Stolee0c18b702020-09-11 17:49:17 +00001472 struct child_process config_set = CHILD_PROCESS_INIT;
1473 struct child_process config_get = CHILD_PROCESS_INIT;
Eric Sunshine26c79742021-02-23 02:31:07 -05001474 char *maintpath = get_maintpath();
Derrick Stolee0c18b702020-09-11 17:49:17 +00001475
Derrick Stolee61f7a382020-10-15 17:22:03 +00001476 /* Disable foreground maintenance */
1477 git_config_set("maintenance.auto", "false");
1478
1479 /* Set maintenance strategy, if unset */
1480 if (!git_config_get_string("maintenance.strategy", &config_value))
1481 free(config_value);
1482 else
1483 git_config_set("maintenance.strategy", "incremental");
1484
Derrick Stolee0c18b702020-09-11 17:49:17 +00001485 config_get.git_cmd = 1;
Derrick Stolee483a6d92020-11-25 22:12:56 +00001486 strvec_pushl(&config_get.args, "config", "--global", "--get",
Eric Sunshine26c79742021-02-23 02:31:07 -05001487 "--fixed-value", "maintenance.repo", maintpath, NULL);
Derrick Stolee0c18b702020-09-11 17:49:17 +00001488 config_get.out = -1;
1489
Eric Sunshine26c79742021-02-23 02:31:07 -05001490 if (start_command(&config_get)) {
1491 rc = error(_("failed to run 'git config'"));
1492 goto done;
1493 }
Derrick Stolee0c18b702020-09-11 17:49:17 +00001494
1495 /* We already have this value in our config! */
Eric Sunshine26c79742021-02-23 02:31:07 -05001496 if (!finish_command(&config_get)) {
1497 rc = 0;
1498 goto done;
1499 }
Derrick Stolee0c18b702020-09-11 17:49:17 +00001500
1501 config_set.git_cmd = 1;
1502 strvec_pushl(&config_set.args, "config", "--add", "--global", "maintenance.repo",
Eric Sunshine26c79742021-02-23 02:31:07 -05001503 maintpath, NULL);
Derrick Stolee0c18b702020-09-11 17:49:17 +00001504
Eric Sunshine26c79742021-02-23 02:31:07 -05001505 rc = run_command(&config_set);
1506
1507done:
1508 free(maintpath);
1509 return rc;
Derrick Stolee0c18b702020-09-11 17:49:17 +00001510}
1511
1512static int maintenance_unregister(void)
1513{
Eric Sunshine26c79742021-02-23 02:31:07 -05001514 int rc;
Derrick Stolee0c18b702020-09-11 17:49:17 +00001515 struct child_process config_unset = CHILD_PROCESS_INIT;
Eric Sunshine26c79742021-02-23 02:31:07 -05001516 char *maintpath = get_maintpath();
Derrick Stolee0c18b702020-09-11 17:49:17 +00001517
Derrick Stolee0c18b702020-09-11 17:49:17 +00001518 config_unset.git_cmd = 1;
1519 strvec_pushl(&config_unset.args, "config", "--global", "--unset",
Eric Sunshine26c79742021-02-23 02:31:07 -05001520 "--fixed-value", "maintenance.repo", maintpath, NULL);
Derrick Stolee0c18b702020-09-11 17:49:17 +00001521
Eric Sunshine26c79742021-02-23 02:31:07 -05001522 rc = run_command(&config_unset);
1523 free(maintpath);
1524 return rc;
Derrick Stolee0c18b702020-09-11 17:49:17 +00001525}
1526
Derrick Stolee2afe7e32021-01-05 13:08:27 +00001527static const char *get_frequency(enum schedule_priority schedule)
1528{
1529 switch (schedule) {
1530 case SCHEDULE_HOURLY:
1531 return "hourly";
1532 case SCHEDULE_DAILY:
1533 return "daily";
1534 case SCHEDULE_WEEKLY:
1535 return "weekly";
1536 default:
1537 BUG("invalid schedule %d", schedule);
1538 }
1539}
1540
Lénaïc Huardeba1ba92021-09-04 22:54:59 +02001541/*
1542 * get_schedule_cmd` reads the GIT_TEST_MAINT_SCHEDULER environment variable
1543 * to mock the schedulers that `git maintenance start` rely on.
1544 *
1545 * For test purpose, GIT_TEST_MAINT_SCHEDULER can be set to a comma-separated
1546 * list of colon-separated key/value pairs where each pair contains a scheduler
1547 * and its corresponding mock.
1548 *
1549 * * If $GIT_TEST_MAINT_SCHEDULER is not set, return false and leave the
1550 * arguments unmodified.
1551 *
1552 * * If $GIT_TEST_MAINT_SCHEDULER is set, return true.
1553 * In this case, the *cmd value is read as input.
1554 *
1555 * * if the input value *cmd is the key of one of the comma-separated list
1556 * item, then *is_available is set to true and *cmd is modified and becomes
1557 * the mock command.
1558 *
1559 * * if the input value *cmd isn’t the key of any of the comma-separated list
1560 * item, then *is_available is set to false.
1561 *
1562 * Ex.:
1563 * GIT_TEST_MAINT_SCHEDULER not set
1564 * +-------+-------------------------------------------------+
1565 * | Input | Output |
1566 * | *cmd | return code | *cmd | *is_available |
1567 * +-------+-------------+-------------------+---------------+
1568 * | "foo" | false | "foo" (unchanged) | (unchanged) |
1569 * +-------+-------------+-------------------+---------------+
1570 *
1571 * GIT_TEST_MAINT_SCHEDULER set to “foo:./mock_foo.sh,bar:./mock_bar.sh”
1572 * +-------+-------------------------------------------------+
1573 * | Input | Output |
1574 * | *cmd | return code | *cmd | *is_available |
1575 * +-------+-------------+-------------------+---------------+
1576 * | "foo" | true | "./mock.foo.sh" | true |
1577 * | "qux" | true | "qux" (unchanged) | false |
1578 * +-------+-------------+-------------------+---------------+
1579 */
1580static int get_schedule_cmd(const char **cmd, int *is_available)
1581{
1582 char *testing = xstrdup_or_null(getenv("GIT_TEST_MAINT_SCHEDULER"));
1583 struct string_list_item *item;
1584 struct string_list list = STRING_LIST_INIT_NODUP;
1585
1586 if (!testing)
1587 return 0;
1588
1589 if (is_available)
1590 *is_available = 0;
1591
1592 string_list_split_in_place(&list, testing, ',', -1);
1593 for_each_string_list_item(item, &list) {
1594 struct string_list pair = STRING_LIST_INIT_NODUP;
1595
1596 if (string_list_split_in_place(&pair, item->string, ':', 2) != 2)
1597 continue;
1598
1599 if (!strcmp(*cmd, pair.items[0].string)) {
1600 *cmd = pair.items[1].string;
1601 if (is_available)
1602 *is_available = 1;
1603 string_list_clear(&list, 0);
1604 UNLEAK(testing);
1605 return 1;
1606 }
1607 }
1608
1609 string_list_clear(&list, 0);
1610 free(testing);
1611 return 1;
1612}
1613
1614static int is_launchctl_available(void)
1615{
1616 const char *cmd = "launchctl";
1617 int is_available;
1618 if (get_schedule_cmd(&cmd, &is_available))
1619 return is_available;
1620
1621#ifdef __APPLE__
1622 return 1;
1623#else
1624 return 0;
1625#endif
1626}
1627
Derrick Stolee2afe7e32021-01-05 13:08:27 +00001628static char *launchctl_service_name(const char *frequency)
1629{
1630 struct strbuf label = STRBUF_INIT;
1631 strbuf_addf(&label, "org.git-scm.git.%s", frequency);
1632 return strbuf_detach(&label, NULL);
1633}
1634
1635static char *launchctl_service_filename(const char *name)
1636{
1637 char *expanded;
1638 struct strbuf filename = STRBUF_INIT;
1639 strbuf_addf(&filename, "~/Library/LaunchAgents/%s.plist", name);
1640
Johannes Schindelina03b0972021-07-24 22:06:52 +00001641 expanded = interpolate_path(filename.buf, 1);
Derrick Stolee2afe7e32021-01-05 13:08:27 +00001642 if (!expanded)
1643 die(_("failed to expand path '%s'"), filename.buf);
1644
1645 strbuf_release(&filename);
1646 return expanded;
1647}
1648
1649static char *launchctl_get_uid(void)
1650{
1651 return xstrfmt("gui/%d", getuid());
1652}
1653
Lénaïc Huardeba1ba92021-09-04 22:54:59 +02001654static int launchctl_boot_plist(int enable, const char *filename)
Derrick Stolee2afe7e32021-01-05 13:08:27 +00001655{
Lénaïc Huardeba1ba92021-09-04 22:54:59 +02001656 const char *cmd = "launchctl";
Derrick Stolee2afe7e32021-01-05 13:08:27 +00001657 int result;
1658 struct child_process child = CHILD_PROCESS_INIT;
1659 char *uid = launchctl_get_uid();
1660
Lénaïc Huardeba1ba92021-09-04 22:54:59 +02001661 get_schedule_cmd(&cmd, NULL);
Derrick Stolee2afe7e32021-01-05 13:08:27 +00001662 strvec_split(&child.args, cmd);
Lénaïc Huardeba1ba92021-09-04 22:54:59 +02001663 strvec_pushl(&child.args, enable ? "bootstrap" : "bootout", uid,
1664 filename, NULL);
Derrick Stolee2afe7e32021-01-05 13:08:27 +00001665
1666 child.no_stderr = 1;
1667 child.no_stdout = 1;
1668
1669 if (start_command(&child))
1670 die(_("failed to start launchctl"));
1671
1672 result = finish_command(&child);
1673
1674 free(uid);
1675 return result;
1676}
1677
Lénaïc Huardeba1ba92021-09-04 22:54:59 +02001678static int launchctl_remove_plist(enum schedule_priority schedule)
Derrick Stolee2afe7e32021-01-05 13:08:27 +00001679{
1680 const char *frequency = get_frequency(schedule);
1681 char *name = launchctl_service_name(frequency);
1682 char *filename = launchctl_service_filename(name);
Lénaïc Huardeba1ba92021-09-04 22:54:59 +02001683 int result = launchctl_boot_plist(0, filename);
Derrick Stolee2afe7e32021-01-05 13:08:27 +00001684 unlink(filename);
1685 free(filename);
1686 free(name);
1687 return result;
1688}
1689
Lénaïc Huardeba1ba92021-09-04 22:54:59 +02001690static int launchctl_remove_plists(void)
Derrick Stolee2afe7e32021-01-05 13:08:27 +00001691{
Lénaïc Huardeba1ba92021-09-04 22:54:59 +02001692 return launchctl_remove_plist(SCHEDULE_HOURLY) ||
1693 launchctl_remove_plist(SCHEDULE_DAILY) ||
1694 launchctl_remove_plist(SCHEDULE_WEEKLY);
Derrick Stolee2afe7e32021-01-05 13:08:27 +00001695}
1696
Derrick Stoleea16eb6b2021-08-24 15:44:00 +00001697static int launchctl_list_contains_plist(const char *name, const char *cmd)
1698{
Derrick Stoleea16eb6b2021-08-24 15:44:00 +00001699 struct child_process child = CHILD_PROCESS_INIT;
Derrick Stoleea16eb6b2021-08-24 15:44:00 +00001700
1701 strvec_split(&child.args, cmd);
1702 strvec_pushl(&child.args, "list", name, NULL);
1703
1704 child.no_stderr = 1;
1705 child.no_stdout = 1;
1706
1707 if (start_command(&child))
1708 die(_("failed to start launchctl"));
1709
Derrick Stoleea16eb6b2021-08-24 15:44:00 +00001710 /* Returns failure if 'name' doesn't exist. */
Ævar Arnfjörð Bjarmason3218cb72021-09-12 02:24:40 +02001711 return !finish_command(&child);
Derrick Stoleea16eb6b2021-08-24 15:44:00 +00001712}
1713
Lénaïc Huardeba1ba92021-09-04 22:54:59 +02001714static int launchctl_schedule_plist(const char *exec_path, enum schedule_priority schedule)
Derrick Stolee2afe7e32021-01-05 13:08:27 +00001715{
Johannes Schindelinbb011222021-08-24 15:43:59 +00001716 int i, fd;
Derrick Stolee2afe7e32021-01-05 13:08:27 +00001717 const char *preamble, *repeat;
1718 const char *frequency = get_frequency(schedule);
1719 char *name = launchctl_service_name(frequency);
1720 char *filename = launchctl_service_filename(name);
Johannes Schindelinbb011222021-08-24 15:43:59 +00001721 struct lock_file lk = LOCK_INIT;
1722 static unsigned long lock_file_timeout_ms = ULONG_MAX;
Derrick Stoleea16eb6b2021-08-24 15:44:00 +00001723 struct strbuf plist = STRBUF_INIT, plist2 = STRBUF_INIT;
1724 struct stat st;
Junio C Hamanoed8794e2021-09-20 15:20:40 -07001725 const char *cmd = "launchctl";
Derrick Stolee2afe7e32021-01-05 13:08:27 +00001726
Junio C Hamanoed8794e2021-09-20 15:20:40 -07001727 get_schedule_cmd(&cmd, NULL);
Derrick Stolee3797a0a2021-01-05 13:08:28 +00001728 preamble = "<?xml version=\"1.0\"?>\n"
Derrick Stolee2afe7e32021-01-05 13:08:27 +00001729 "<!DOCTYPE plist PUBLIC \"-//Apple//DTD PLIST 1.0//EN\" \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">\n"
1730 "<plist version=\"1.0\">"
1731 "<dict>\n"
1732 "<key>Label</key><string>%s</string>\n"
1733 "<key>ProgramArguments</key>\n"
1734 "<array>\n"
1735 "<string>%s/git</string>\n"
1736 "<string>--exec-path=%s</string>\n"
1737 "<string>for-each-repo</string>\n"
1738 "<string>--config=maintenance.repo</string>\n"
1739 "<string>maintenance</string>\n"
1740 "<string>run</string>\n"
1741 "<string>--schedule=%s</string>\n"
1742 "</array>\n"
1743 "<key>StartCalendarInterval</key>\n"
1744 "<array>\n";
Johannes Schindelinbb011222021-08-24 15:43:59 +00001745 strbuf_addf(&plist, preamble, name, exec_path, exec_path, frequency);
Derrick Stolee2afe7e32021-01-05 13:08:27 +00001746
1747 switch (schedule) {
1748 case SCHEDULE_HOURLY:
1749 repeat = "<dict>\n"
1750 "<key>Hour</key><integer>%d</integer>\n"
1751 "<key>Minute</key><integer>0</integer>\n"
1752 "</dict>\n";
1753 for (i = 1; i <= 23; i++)
Johannes Schindelinbb011222021-08-24 15:43:59 +00001754 strbuf_addf(&plist, repeat, i);
Derrick Stolee2afe7e32021-01-05 13:08:27 +00001755 break;
1756
1757 case SCHEDULE_DAILY:
1758 repeat = "<dict>\n"
1759 "<key>Day</key><integer>%d</integer>\n"
1760 "<key>Hour</key><integer>0</integer>\n"
1761 "<key>Minute</key><integer>0</integer>\n"
1762 "</dict>\n";
1763 for (i = 1; i <= 6; i++)
Johannes Schindelinbb011222021-08-24 15:43:59 +00001764 strbuf_addf(&plist, repeat, i);
Derrick Stolee2afe7e32021-01-05 13:08:27 +00001765 break;
1766
1767 case SCHEDULE_WEEKLY:
Johannes Schindelinbb011222021-08-24 15:43:59 +00001768 strbuf_addstr(&plist,
1769 "<dict>\n"
1770 "<key>Day</key><integer>0</integer>\n"
1771 "<key>Hour</key><integer>0</integer>\n"
1772 "<key>Minute</key><integer>0</integer>\n"
1773 "</dict>\n");
Derrick Stolee2afe7e32021-01-05 13:08:27 +00001774 break;
1775
1776 default:
1777 /* unreachable */
1778 break;
1779 }
Johannes Schindelinbb011222021-08-24 15:43:59 +00001780 strbuf_addstr(&plist, "</array>\n</dict>\n</plist>\n");
Derrick Stolee2afe7e32021-01-05 13:08:27 +00001781
Johannes Schindelinbb011222021-08-24 15:43:59 +00001782 if (safe_create_leading_directories(filename))
1783 die(_("failed to create directories for '%s'"), filename);
1784
1785 if ((long)lock_file_timeout_ms < 0 &&
1786 git_config_get_ulong("gc.launchctlplistlocktimeoutms",
1787 &lock_file_timeout_ms))
1788 lock_file_timeout_ms = 150;
1789
1790 fd = hold_lock_file_for_update_timeout(&lk, filename, LOCK_DIE_ON_ERROR,
1791 lock_file_timeout_ms);
1792
Derrick Stoleea16eb6b2021-08-24 15:44:00 +00001793 /*
1794 * Does this file already exist? With the intended contents? Is it
1795 * registered already? Then it does not need to be re-registered.
1796 */
1797 if (!stat(filename, &st) && st.st_size == plist.len &&
1798 strbuf_read_file(&plist2, filename, plist.len) == plist.len &&
1799 !strbuf_cmp(&plist, &plist2) &&
1800 launchctl_list_contains_plist(name, cmd))
1801 rollback_lock_file(&lk);
1802 else {
1803 if (write_in_full(fd, plist.buf, plist.len) < 0 ||
1804 commit_lock_file(&lk))
1805 die_errno(_("could not write '%s'"), filename);
Derrick Stolee2afe7e32021-01-05 13:08:27 +00001806
Derrick Stoleea16eb6b2021-08-24 15:44:00 +00001807 /* bootout might fail if not already running, so ignore */
Junio C Hamanoed8794e2021-09-20 15:20:40 -07001808 launchctl_boot_plist(0, filename);
1809 if (launchctl_boot_plist(1, filename))
Derrick Stoleea16eb6b2021-08-24 15:44:00 +00001810 die(_("failed to bootstrap service %s"), filename);
1811 }
Derrick Stolee2afe7e32021-01-05 13:08:27 +00001812
1813 free(filename);
1814 free(name);
Johannes Schindelinbb011222021-08-24 15:43:59 +00001815 strbuf_release(&plist);
Derrick Stoleea16eb6b2021-08-24 15:44:00 +00001816 strbuf_release(&plist2);
Derrick Stolee2afe7e32021-01-05 13:08:27 +00001817 return 0;
1818}
1819
Lénaïc Huardeba1ba92021-09-04 22:54:59 +02001820static int launchctl_add_plists(void)
Derrick Stolee2afe7e32021-01-05 13:08:27 +00001821{
1822 const char *exec_path = git_exec_path();
1823
Lénaïc Huardeba1ba92021-09-04 22:54:59 +02001824 return launchctl_schedule_plist(exec_path, SCHEDULE_HOURLY) ||
1825 launchctl_schedule_plist(exec_path, SCHEDULE_DAILY) ||
1826 launchctl_schedule_plist(exec_path, SCHEDULE_WEEKLY);
Derrick Stolee2afe7e32021-01-05 13:08:27 +00001827}
1828
Lénaïc Huardeba1ba92021-09-04 22:54:59 +02001829static int launchctl_update_schedule(int run_maintenance, int fd)
Derrick Stolee2afe7e32021-01-05 13:08:27 +00001830{
1831 if (run_maintenance)
Lénaïc Huardeba1ba92021-09-04 22:54:59 +02001832 return launchctl_add_plists();
Derrick Stolee2afe7e32021-01-05 13:08:27 +00001833 else
Lénaïc Huardeba1ba92021-09-04 22:54:59 +02001834 return launchctl_remove_plists();
1835}
1836
1837static int is_schtasks_available(void)
1838{
1839 const char *cmd = "schtasks";
1840 int is_available;
1841 if (get_schedule_cmd(&cmd, &is_available))
1842 return is_available;
1843
1844#ifdef GIT_WINDOWS_NATIVE
1845 return 1;
1846#else
1847 return 0;
1848#endif
Derrick Stolee2afe7e32021-01-05 13:08:27 +00001849}
1850
Derrick Stolee3797a0a2021-01-05 13:08:28 +00001851static char *schtasks_task_name(const char *frequency)
1852{
1853 struct strbuf label = STRBUF_INIT;
1854 strbuf_addf(&label, "Git Maintenance (%s)", frequency);
1855 return strbuf_detach(&label, NULL);
1856}
1857
Lénaïc Huardeba1ba92021-09-04 22:54:59 +02001858static int schtasks_remove_task(enum schedule_priority schedule)
Derrick Stolee3797a0a2021-01-05 13:08:28 +00001859{
Lénaïc Huardeba1ba92021-09-04 22:54:59 +02001860 const char *cmd = "schtasks";
Derrick Stolee3797a0a2021-01-05 13:08:28 +00001861 int result;
1862 struct strvec args = STRVEC_INIT;
1863 const char *frequency = get_frequency(schedule);
1864 char *name = schtasks_task_name(frequency);
1865
Lénaïc Huardeba1ba92021-09-04 22:54:59 +02001866 get_schedule_cmd(&cmd, NULL);
Derrick Stolee3797a0a2021-01-05 13:08:28 +00001867 strvec_split(&args, cmd);
1868 strvec_pushl(&args, "/delete", "/tn", name, "/f", NULL);
1869
1870 result = run_command_v_opt(args.v, 0);
1871
1872 strvec_clear(&args);
1873 free(name);
1874 return result;
1875}
1876
Lénaïc Huardeba1ba92021-09-04 22:54:59 +02001877static int schtasks_remove_tasks(void)
Derrick Stolee3797a0a2021-01-05 13:08:28 +00001878{
Lénaïc Huardeba1ba92021-09-04 22:54:59 +02001879 return schtasks_remove_task(SCHEDULE_HOURLY) ||
1880 schtasks_remove_task(SCHEDULE_DAILY) ||
1881 schtasks_remove_task(SCHEDULE_WEEKLY);
Derrick Stolee3797a0a2021-01-05 13:08:28 +00001882}
1883
Lénaïc Huardeba1ba92021-09-04 22:54:59 +02001884static int schtasks_schedule_task(const char *exec_path, enum schedule_priority schedule)
Derrick Stolee3797a0a2021-01-05 13:08:28 +00001885{
Lénaïc Huardeba1ba92021-09-04 22:54:59 +02001886 const char *cmd = "schtasks";
Derrick Stolee3797a0a2021-01-05 13:08:28 +00001887 int result;
1888 struct child_process child = CHILD_PROCESS_INIT;
1889 const char *xml;
1890 struct tempfile *tfile;
1891 const char *frequency = get_frequency(schedule);
1892 char *name = schtasks_task_name(frequency);
1893 struct strbuf tfilename = STRBUF_INIT;
1894
Lénaïc Huardeba1ba92021-09-04 22:54:59 +02001895 get_schedule_cmd(&cmd, NULL);
1896
Derrick Stolee3797a0a2021-01-05 13:08:28 +00001897 strbuf_addf(&tfilename, "%s/schedule_%s_XXXXXX",
1898 get_git_common_dir(), frequency);
1899 tfile = xmks_tempfile(tfilename.buf);
1900 strbuf_release(&tfilename);
1901
1902 if (!fdopen_tempfile(tfile, "w"))
1903 die(_("failed to create temp xml file"));
1904
1905 xml = "<?xml version=\"1.0\" ?>\n"
1906 "<Task version=\"1.4\" xmlns=\"http://schemas.microsoft.com/windows/2004/02/mit/task\">\n"
1907 "<Triggers>\n"
1908 "<CalendarTrigger>\n";
1909 fputs(xml, tfile->fp);
1910
1911 switch (schedule) {
1912 case SCHEDULE_HOURLY:
1913 fprintf(tfile->fp,
1914 "<StartBoundary>2020-01-01T01:00:00</StartBoundary>\n"
1915 "<Enabled>true</Enabled>\n"
1916 "<ScheduleByDay>\n"
1917 "<DaysInterval>1</DaysInterval>\n"
1918 "</ScheduleByDay>\n"
1919 "<Repetition>\n"
1920 "<Interval>PT1H</Interval>\n"
1921 "<Duration>PT23H</Duration>\n"
1922 "<StopAtDurationEnd>false</StopAtDurationEnd>\n"
1923 "</Repetition>\n");
1924 break;
1925
1926 case SCHEDULE_DAILY:
1927 fprintf(tfile->fp,
1928 "<StartBoundary>2020-01-01T00:00:00</StartBoundary>\n"
1929 "<Enabled>true</Enabled>\n"
1930 "<ScheduleByWeek>\n"
1931 "<DaysOfWeek>\n"
1932 "<Monday />\n"
1933 "<Tuesday />\n"
1934 "<Wednesday />\n"
1935 "<Thursday />\n"
1936 "<Friday />\n"
1937 "<Saturday />\n"
1938 "</DaysOfWeek>\n"
1939 "<WeeksInterval>1</WeeksInterval>\n"
1940 "</ScheduleByWeek>\n");
1941 break;
1942
1943 case SCHEDULE_WEEKLY:
1944 fprintf(tfile->fp,
1945 "<StartBoundary>2020-01-01T00:00:00</StartBoundary>\n"
1946 "<Enabled>true</Enabled>\n"
1947 "<ScheduleByWeek>\n"
1948 "<DaysOfWeek>\n"
1949 "<Sunday />\n"
1950 "</DaysOfWeek>\n"
1951 "<WeeksInterval>1</WeeksInterval>\n"
1952 "</ScheduleByWeek>\n");
1953 break;
1954
1955 default:
1956 break;
1957 }
1958
1959 xml = "</CalendarTrigger>\n"
1960 "</Triggers>\n"
1961 "<Principals>\n"
1962 "<Principal id=\"Author\">\n"
1963 "<LogonType>InteractiveToken</LogonType>\n"
1964 "<RunLevel>LeastPrivilege</RunLevel>\n"
1965 "</Principal>\n"
1966 "</Principals>\n"
1967 "<Settings>\n"
1968 "<MultipleInstancesPolicy>IgnoreNew</MultipleInstancesPolicy>\n"
1969 "<Enabled>true</Enabled>\n"
1970 "<Hidden>true</Hidden>\n"
1971 "<UseUnifiedSchedulingEngine>true</UseUnifiedSchedulingEngine>\n"
1972 "<WakeToRun>false</WakeToRun>\n"
1973 "<ExecutionTimeLimit>PT72H</ExecutionTimeLimit>\n"
1974 "<Priority>7</Priority>\n"
1975 "</Settings>\n"
1976 "<Actions Context=\"Author\">\n"
1977 "<Exec>\n"
1978 "<Command>\"%s\\git.exe\"</Command>\n"
1979 "<Arguments>--exec-path=\"%s\" for-each-repo --config=maintenance.repo maintenance run --schedule=%s</Arguments>\n"
1980 "</Exec>\n"
1981 "</Actions>\n"
1982 "</Task>\n";
1983 fprintf(tfile->fp, xml, exec_path, exec_path, frequency);
1984 strvec_split(&child.args, cmd);
1985 strvec_pushl(&child.args, "/create", "/tn", name, "/f", "/xml",
1986 get_tempfile_path(tfile), NULL);
1987 close_tempfile_gently(tfile);
1988
1989 child.no_stdout = 1;
1990 child.no_stderr = 1;
1991
1992 if (start_command(&child))
1993 die(_("failed to start schtasks"));
1994 result = finish_command(&child);
1995
1996 delete_tempfile(&tfile);
1997 free(name);
1998 return result;
1999}
2000
Lénaïc Huardeba1ba92021-09-04 22:54:59 +02002001static int schtasks_schedule_tasks(void)
Derrick Stolee3797a0a2021-01-05 13:08:28 +00002002{
2003 const char *exec_path = git_exec_path();
2004
Lénaïc Huardeba1ba92021-09-04 22:54:59 +02002005 return schtasks_schedule_task(exec_path, SCHEDULE_HOURLY) ||
2006 schtasks_schedule_task(exec_path, SCHEDULE_DAILY) ||
2007 schtasks_schedule_task(exec_path, SCHEDULE_WEEKLY);
Derrick Stolee3797a0a2021-01-05 13:08:28 +00002008}
2009
Lénaïc Huardeba1ba92021-09-04 22:54:59 +02002010static int schtasks_update_schedule(int run_maintenance, int fd)
Derrick Stolee3797a0a2021-01-05 13:08:28 +00002011{
2012 if (run_maintenance)
Lénaïc Huardeba1ba92021-09-04 22:54:59 +02002013 return schtasks_schedule_tasks();
Derrick Stolee3797a0a2021-01-05 13:08:28 +00002014 else
Lénaïc Huardeba1ba92021-09-04 22:54:59 +02002015 return schtasks_remove_tasks();
2016}
2017
Derrick Stolee689a2aa2021-11-10 18:35:59 +00002018MAYBE_UNUSED
2019static int check_crontab_process(const char *cmd)
Lénaïc Huardeba1ba92021-09-04 22:54:59 +02002020{
Lénaïc Huardeba1ba92021-09-04 22:54:59 +02002021 struct child_process child = CHILD_PROCESS_INIT;
2022
Lénaïc Huardeba1ba92021-09-04 22:54:59 +02002023 strvec_split(&child.args, cmd);
2024 strvec_push(&child.args, "-l");
2025 child.no_stdin = 1;
2026 child.no_stdout = 1;
2027 child.no_stderr = 1;
2028 child.silent_exec_failure = 1;
2029
2030 if (start_command(&child))
2031 return 0;
2032 /* Ignore exit code, as an empty crontab will return error. */
2033 finish_command(&child);
2034 return 1;
Derrick Stolee3797a0a2021-01-05 13:08:28 +00002035}
2036
Derrick Stolee689a2aa2021-11-10 18:35:59 +00002037static int is_crontab_available(void)
2038{
2039 const char *cmd = "crontab";
2040 int is_available;
2041
2042 if (get_schedule_cmd(&cmd, &is_available))
2043 return is_available;
2044
2045#ifdef __APPLE__
2046 /*
2047 * macOS has cron, but it requires special permissions and will
2048 * create a UI alert when attempting to run this command.
2049 */
2050 return 0;
2051#else
2052 return check_crontab_process(cmd);
2053#endif
2054}
2055
Derrick Stolee2fec6042020-09-11 17:49:18 +00002056#define BEGIN_LINE "# BEGIN GIT MAINTENANCE SCHEDULE"
2057#define END_LINE "# END GIT MAINTENANCE SCHEDULE"
2058
Lénaïc Huardeba1ba92021-09-04 22:54:59 +02002059static int crontab_update_schedule(int run_maintenance, int fd)
Derrick Stolee2fec6042020-09-11 17:49:18 +00002060{
Lénaïc Huardeba1ba92021-09-04 22:54:59 +02002061 const char *cmd = "crontab";
Derrick Stolee2fec6042020-09-11 17:49:18 +00002062 int result = 0;
2063 int in_old_region = 0;
2064 struct child_process crontab_list = CHILD_PROCESS_INIT;
2065 struct child_process crontab_edit = CHILD_PROCESS_INIT;
2066 FILE *cron_list, *cron_in;
Derrick Stolee2fec6042020-09-11 17:49:18 +00002067 struct strbuf line = STRBUF_INIT;
Derrick Stolee2fec6042020-09-11 17:49:18 +00002068
Lénaïc Huardeba1ba92021-09-04 22:54:59 +02002069 get_schedule_cmd(&cmd, NULL);
Derrick Stolee31345d52020-11-24 04:16:42 +00002070 strvec_split(&crontab_list.args, cmd);
Derrick Stolee2fec6042020-09-11 17:49:18 +00002071 strvec_push(&crontab_list.args, "-l");
2072 crontab_list.in = -1;
Derrick Stolee31345d52020-11-24 04:16:42 +00002073 crontab_list.out = dup(fd);
Derrick Stolee2fec6042020-09-11 17:49:18 +00002074 crontab_list.git_cmd = 0;
2075
Derrick Stolee31345d52020-11-24 04:16:42 +00002076 if (start_command(&crontab_list))
2077 return error(_("failed to run 'crontab -l'; your system might not support 'cron'"));
Derrick Stolee2fec6042020-09-11 17:49:18 +00002078
2079 /* Ignore exit code, as an empty crontab will return error. */
2080 finish_command(&crontab_list);
2081
2082 /*
2083 * Read from the .lock file, filtering out the old
2084 * schedule while appending the new schedule.
2085 */
Derrick Stolee31345d52020-11-24 04:16:42 +00002086 cron_list = fdopen(fd, "r");
Derrick Stolee2fec6042020-09-11 17:49:18 +00002087 rewind(cron_list);
2088
Derrick Stolee31345d52020-11-24 04:16:42 +00002089 strvec_split(&crontab_edit.args, cmd);
Derrick Stolee2fec6042020-09-11 17:49:18 +00002090 crontab_edit.in = -1;
2091 crontab_edit.git_cmd = 0;
2092
Derrick Stolee31345d52020-11-24 04:16:42 +00002093 if (start_command(&crontab_edit))
2094 return error(_("failed to run 'crontab'; your system might not support 'cron'"));
Derrick Stolee2fec6042020-09-11 17:49:18 +00002095
2096 cron_in = fdopen(crontab_edit.in, "w");
2097 if (!cron_in) {
2098 result = error(_("failed to open stdin of 'crontab'"));
2099 goto done_editing;
2100 }
2101
2102 while (!strbuf_getline_lf(&line, cron_list)) {
2103 if (!in_old_region && !strcmp(line.buf, BEGIN_LINE))
2104 in_old_region = 1;
Martin Ågren66dc0a32020-12-21 22:26:32 +01002105 else if (in_old_region && !strcmp(line.buf, END_LINE))
Derrick Stolee2fec6042020-09-11 17:49:18 +00002106 in_old_region = 0;
Martin Ågren66dc0a32020-12-21 22:26:32 +01002107 else if (!in_old_region)
2108 fprintf(cron_in, "%s\n", line.buf);
Derrick Stolee2fec6042020-09-11 17:49:18 +00002109 }
Lénaïc Huardc5d0b122021-05-10 21:59:09 +02002110 strbuf_release(&line);
Derrick Stolee2fec6042020-09-11 17:49:18 +00002111
2112 if (run_maintenance) {
2113 struct strbuf line_format = STRBUF_INIT;
2114 const char *exec_path = git_exec_path();
2115
2116 fprintf(cron_in, "%s\n", BEGIN_LINE);
2117 fprintf(cron_in,
2118 "# The following schedule was created by Git\n");
2119 fprintf(cron_in, "# Any edits made in this region might be\n");
2120 fprintf(cron_in,
2121 "# replaced in the future by a Git command.\n\n");
2122
2123 strbuf_addf(&line_format,
2124 "%%s %%s * * %%s \"%s/git\" --exec-path=\"%s\" for-each-repo --config=maintenance.repo maintenance run --schedule=%%s\n",
2125 exec_path, exec_path);
2126 fprintf(cron_in, line_format.buf, "0", "1-23", "*", "hourly");
2127 fprintf(cron_in, line_format.buf, "0", "0", "1-6", "daily");
2128 fprintf(cron_in, line_format.buf, "0", "0", "0", "weekly");
2129 strbuf_release(&line_format);
2130
2131 fprintf(cron_in, "\n%s\n", END_LINE);
2132 }
2133
2134 fflush(cron_in);
2135 fclose(cron_in);
2136 close(crontab_edit.in);
2137
2138done_editing:
Derrick Stolee31345d52020-11-24 04:16:42 +00002139 if (finish_command(&crontab_edit))
Derrick Stolee2fec6042020-09-11 17:49:18 +00002140 result = error(_("'crontab' died"));
Derrick Stolee31345d52020-11-24 04:16:42 +00002141 else
2142 fclose(cron_list);
2143 return result;
2144}
Derrick Stolee2fec6042020-09-11 17:49:18 +00002145
Lénaïc Huardb681b192021-09-04 22:55:00 +02002146static int real_is_systemd_timer_available(void)
Derrick Stolee31345d52020-11-24 04:16:42 +00002147{
Lénaïc Huardb681b192021-09-04 22:55:00 +02002148 struct child_process child = CHILD_PROCESS_INIT;
2149
2150 strvec_pushl(&child.args, "systemctl", "--user", "list-timers", NULL);
2151 child.no_stdin = 1;
2152 child.no_stdout = 1;
2153 child.no_stderr = 1;
2154 child.silent_exec_failure = 1;
2155
2156 if (start_command(&child))
2157 return 0;
2158 if (finish_command(&child))
2159 return 0;
2160 return 1;
2161}
2162
2163static int is_systemd_timer_available(void)
2164{
2165 const char *cmd = "systemctl";
2166 int is_available;
2167
2168 if (get_schedule_cmd(&cmd, &is_available))
2169 return is_available;
2170
2171 return real_is_systemd_timer_available();
2172}
2173
2174static char *xdg_config_home_systemd(const char *filename)
2175{
2176 return xdg_config_home_for("systemd/user", filename);
2177}
2178
2179static int systemd_timer_enable_unit(int enable,
2180 enum schedule_priority schedule)
2181{
2182 const char *cmd = "systemctl";
2183 struct child_process child = CHILD_PROCESS_INIT;
2184 const char *frequency = get_frequency(schedule);
2185
2186 /*
2187 * Disabling the systemd unit while it is already disabled makes
2188 * systemctl print an error.
2189 * Let's ignore it since it means we already are in the expected state:
2190 * the unit is disabled.
2191 *
2192 * On the other hand, enabling a systemd unit which is already enabled
2193 * produces no error.
2194 */
2195 if (!enable)
2196 child.no_stderr = 1;
2197
2198 get_schedule_cmd(&cmd, NULL);
2199 strvec_split(&child.args, cmd);
2200 strvec_pushl(&child.args, "--user", enable ? "enable" : "disable",
2201 "--now", NULL);
2202 strvec_pushf(&child.args, "git-maintenance@%s.timer", frequency);
2203
2204 if (start_command(&child))
2205 return error(_("failed to start systemctl"));
2206 if (finish_command(&child))
2207 /*
2208 * Disabling an already disabled systemd unit makes
2209 * systemctl fail.
2210 * Let's ignore this failure.
2211 *
2212 * Enabling an enabled systemd unit doesn't fail.
2213 */
2214 if (enable)
2215 return error(_("failed to run systemctl"));
2216 return 0;
2217}
2218
2219static int systemd_timer_delete_unit_templates(void)
2220{
2221 int ret = 0;
2222 char *filename = xdg_config_home_systemd("git-maintenance@.timer");
2223 if (unlink(filename) && !is_missing_file_error(errno))
2224 ret = error_errno(_("failed to delete '%s'"), filename);
2225 FREE_AND_NULL(filename);
2226
2227 filename = xdg_config_home_systemd("git-maintenance@.service");
2228 if (unlink(filename) && !is_missing_file_error(errno))
2229 ret = error_errno(_("failed to delete '%s'"), filename);
2230
2231 free(filename);
2232 return ret;
2233}
2234
2235static int systemd_timer_delete_units(void)
2236{
2237 return systemd_timer_enable_unit(0, SCHEDULE_HOURLY) ||
2238 systemd_timer_enable_unit(0, SCHEDULE_DAILY) ||
2239 systemd_timer_enable_unit(0, SCHEDULE_WEEKLY) ||
2240 systemd_timer_delete_unit_templates();
2241}
2242
2243static int systemd_timer_write_unit_templates(const char *exec_path)
2244{
2245 char *filename;
2246 FILE *file;
2247 const char *unit;
2248
2249 filename = xdg_config_home_systemd("git-maintenance@.timer");
2250 if (safe_create_leading_directories(filename)) {
2251 error(_("failed to create directories for '%s'"), filename);
2252 goto error;
2253 }
2254 file = fopen_or_warn(filename, "w");
Junio C Hamanoafe8a902022-05-02 09:50:37 -07002255 if (!file)
Lénaïc Huardb681b192021-09-04 22:55:00 +02002256 goto error;
2257
2258 unit = "# This file was created and is maintained by Git.\n"
2259 "# Any edits made in this file might be replaced in the future\n"
2260 "# by a Git command.\n"
2261 "\n"
2262 "[Unit]\n"
2263 "Description=Optimize Git repositories data\n"
2264 "\n"
2265 "[Timer]\n"
2266 "OnCalendar=%i\n"
2267 "Persistent=true\n"
2268 "\n"
2269 "[Install]\n"
2270 "WantedBy=timers.target\n";
2271 if (fputs(unit, file) == EOF) {
2272 error(_("failed to write to '%s'"), filename);
2273 fclose(file);
2274 goto error;
2275 }
2276 if (fclose(file) == EOF) {
2277 error_errno(_("failed to flush '%s'"), filename);
2278 goto error;
2279 }
2280 free(filename);
2281
2282 filename = xdg_config_home_systemd("git-maintenance@.service");
2283 file = fopen_or_warn(filename, "w");
Junio C Hamanoafe8a902022-05-02 09:50:37 -07002284 if (!file)
Lénaïc Huardb681b192021-09-04 22:55:00 +02002285 goto error;
2286
2287 unit = "# This file was created and is maintained by Git.\n"
2288 "# Any edits made in this file might be replaced in the future\n"
2289 "# by a Git command.\n"
2290 "\n"
2291 "[Unit]\n"
2292 "Description=Optimize Git repositories data\n"
2293 "\n"
2294 "[Service]\n"
2295 "Type=oneshot\n"
2296 "ExecStart=\"%s/git\" --exec-path=\"%s\" for-each-repo --config=maintenance.repo maintenance run --schedule=%%i\n"
2297 "LockPersonality=yes\n"
2298 "MemoryDenyWriteExecute=yes\n"
2299 "NoNewPrivileges=yes\n"
2300 "RestrictAddressFamilies=AF_UNIX AF_INET AF_INET6\n"
2301 "RestrictNamespaces=yes\n"
2302 "RestrictRealtime=yes\n"
2303 "RestrictSUIDSGID=yes\n"
2304 "SystemCallArchitectures=native\n"
2305 "SystemCallFilter=@system-service\n";
2306 if (fprintf(file, unit, exec_path, exec_path) < 0) {
2307 error(_("failed to write to '%s'"), filename);
2308 fclose(file);
2309 goto error;
2310 }
2311 if (fclose(file) == EOF) {
2312 error_errno(_("failed to flush '%s'"), filename);
2313 goto error;
2314 }
2315 free(filename);
2316 return 0;
2317
2318error:
2319 free(filename);
2320 systemd_timer_delete_unit_templates();
2321 return -1;
2322}
2323
2324static int systemd_timer_setup_units(void)
2325{
2326 const char *exec_path = git_exec_path();
2327
2328 int ret = systemd_timer_write_unit_templates(exec_path) ||
2329 systemd_timer_enable_unit(1, SCHEDULE_HOURLY) ||
2330 systemd_timer_enable_unit(1, SCHEDULE_DAILY) ||
2331 systemd_timer_enable_unit(1, SCHEDULE_WEEKLY);
2332 if (ret)
2333 systemd_timer_delete_units();
2334 return ret;
2335}
2336
2337static int systemd_timer_update_schedule(int run_maintenance, int fd)
2338{
2339 if (run_maintenance)
2340 return systemd_timer_setup_units();
2341 else
2342 return systemd_timer_delete_units();
2343}
2344
Lénaïc Huardeba1ba92021-09-04 22:54:59 +02002345enum scheduler {
2346 SCHEDULER_INVALID = -1,
2347 SCHEDULER_AUTO,
2348 SCHEDULER_CRON,
Lénaïc Huardb681b192021-09-04 22:55:00 +02002349 SCHEDULER_SYSTEMD,
Lénaïc Huardeba1ba92021-09-04 22:54:59 +02002350 SCHEDULER_LAUNCHCTL,
2351 SCHEDULER_SCHTASKS,
2352};
Derrick Stolee31345d52020-11-24 04:16:42 +00002353
Lénaïc Huardeba1ba92021-09-04 22:54:59 +02002354static const struct {
2355 const char *name;
2356 int (*is_available)(void);
2357 int (*update_schedule)(int run_maintenance, int fd);
2358} scheduler_fn[] = {
2359 [SCHEDULER_CRON] = {
2360 .name = "crontab",
2361 .is_available = is_crontab_available,
2362 .update_schedule = crontab_update_schedule,
2363 },
Lénaïc Huardb681b192021-09-04 22:55:00 +02002364 [SCHEDULER_SYSTEMD] = {
2365 .name = "systemctl",
2366 .is_available = is_systemd_timer_available,
2367 .update_schedule = systemd_timer_update_schedule,
2368 },
Lénaïc Huardeba1ba92021-09-04 22:54:59 +02002369 [SCHEDULER_LAUNCHCTL] = {
2370 .name = "launchctl",
2371 .is_available = is_launchctl_available,
2372 .update_schedule = launchctl_update_schedule,
2373 },
2374 [SCHEDULER_SCHTASKS] = {
2375 .name = "schtasks",
2376 .is_available = is_schtasks_available,
2377 .update_schedule = schtasks_update_schedule,
2378 },
2379};
2380
2381static enum scheduler parse_scheduler(const char *value)
Derrick Stolee31345d52020-11-24 04:16:42 +00002382{
Lénaïc Huardeba1ba92021-09-04 22:54:59 +02002383 if (!value)
2384 return SCHEDULER_INVALID;
2385 else if (!strcasecmp(value, "auto"))
2386 return SCHEDULER_AUTO;
2387 else if (!strcasecmp(value, "cron") || !strcasecmp(value, "crontab"))
2388 return SCHEDULER_CRON;
Lénaïc Huardb681b192021-09-04 22:55:00 +02002389 else if (!strcasecmp(value, "systemd") ||
2390 !strcasecmp(value, "systemd-timer"))
2391 return SCHEDULER_SYSTEMD;
Lénaïc Huardeba1ba92021-09-04 22:54:59 +02002392 else if (!strcasecmp(value, "launchctl"))
2393 return SCHEDULER_LAUNCHCTL;
2394 else if (!strcasecmp(value, "schtasks"))
2395 return SCHEDULER_SCHTASKS;
2396 else
2397 return SCHEDULER_INVALID;
2398}
2399
2400static int maintenance_opt_scheduler(const struct option *opt, const char *arg,
2401 int unset)
2402{
2403 enum scheduler *scheduler = opt->value;
2404
2405 BUG_ON_OPT_NEG(unset);
2406
2407 *scheduler = parse_scheduler(arg);
2408 if (*scheduler == SCHEDULER_INVALID)
2409 return error(_("unrecognized --scheduler argument '%s'"), arg);
2410 return 0;
2411}
2412
2413struct maintenance_start_opts {
2414 enum scheduler scheduler;
2415};
2416
2417static enum scheduler resolve_scheduler(enum scheduler scheduler)
2418{
2419 if (scheduler != SCHEDULER_AUTO)
2420 return scheduler;
2421
2422#if defined(__APPLE__)
2423 return SCHEDULER_LAUNCHCTL;
2424
2425#elif defined(GIT_WINDOWS_NATIVE)
2426 return SCHEDULER_SCHTASKS;
2427
Lénaïc Huardb681b192021-09-04 22:55:00 +02002428#elif defined(__linux__)
2429 if (is_systemd_timer_available())
2430 return SCHEDULER_SYSTEMD;
2431 else if (is_crontab_available())
2432 return SCHEDULER_CRON;
2433 else
2434 die(_("neither systemd timers nor crontab are available"));
2435
Lénaïc Huardeba1ba92021-09-04 22:54:59 +02002436#else
2437 return SCHEDULER_CRON;
2438#endif
2439}
2440
2441static void validate_scheduler(enum scheduler scheduler)
2442{
2443 if (scheduler == SCHEDULER_INVALID)
2444 BUG("invalid scheduler");
2445 if (scheduler == SCHEDULER_AUTO)
2446 BUG("resolve_scheduler should have been called before");
2447
2448 if (!scheduler_fn[scheduler].is_available())
2449 die(_("%s scheduler is not available"),
2450 scheduler_fn[scheduler].name);
2451}
2452
2453static int update_background_schedule(const struct maintenance_start_opts *opts,
2454 int enable)
2455{
2456 unsigned int i;
2457 int result = 0;
Derrick Stolee31345d52020-11-24 04:16:42 +00002458 struct lock_file lk;
2459 char *lock_path = xstrfmt("%s/schedule", the_repository->objects->odb->path);
2460
Lénaïc Huardc5d0b122021-05-10 21:59:09 +02002461 if (hold_lock_file_for_update(&lk, lock_path, LOCK_NO_DEREF) < 0) {
Lénaïc Huardeba1ba92021-09-04 22:54:59 +02002462 free(lock_path);
2463 return error(_("another process is scheduling background maintenance"));
Lénaïc Huardc5d0b122021-05-10 21:59:09 +02002464 }
Derrick Stolee31345d52020-11-24 04:16:42 +00002465
Lénaïc Huardeba1ba92021-09-04 22:54:59 +02002466 for (i = 1; i < ARRAY_SIZE(scheduler_fn); i++) {
2467 if (enable && opts->scheduler == i)
2468 continue;
2469 if (!scheduler_fn[i].is_available())
2470 continue;
2471 scheduler_fn[i].update_schedule(0, get_lock_file_fd(&lk));
2472 }
2473
2474 if (enable)
2475 result = scheduler_fn[opts->scheduler].update_schedule(
2476 1, get_lock_file_fd(&lk));
Derrick Stolee31345d52020-11-24 04:16:42 +00002477
Derrick Stolee2fec6042020-09-11 17:49:18 +00002478 rollback_lock_file(&lk);
Lénaïc Huardc5d0b122021-05-10 21:59:09 +02002479
Lénaïc Huardc5d0b122021-05-10 21:59:09 +02002480 free(lock_path);
Derrick Stolee2fec6042020-09-11 17:49:18 +00002481 return result;
2482}
2483
Lénaïc Huardeba1ba92021-09-04 22:54:59 +02002484static const char *const builtin_maintenance_start_usage[] = {
2485 N_("git maintenance start [--scheduler=<scheduler>]"),
2486 NULL
2487};
2488
2489static int maintenance_start(int argc, const char **argv, const char *prefix)
Derrick Stolee2fec6042020-09-11 17:49:18 +00002490{
Lénaïc Huardeba1ba92021-09-04 22:54:59 +02002491 struct maintenance_start_opts opts = { 0 };
2492 struct option options[] = {
2493 OPT_CALLBACK_F(
2494 0, "scheduler", &opts.scheduler, N_("scheduler"),
2495 N_("scheduler to trigger git maintenance run"),
2496 PARSE_OPT_NONEG, maintenance_opt_scheduler),
2497 OPT_END()
2498 };
2499
2500 argc = parse_options(argc, argv, prefix, options,
2501 builtin_maintenance_start_usage, 0);
2502 if (argc)
2503 usage_with_options(builtin_maintenance_start_usage, options);
2504
2505 opts.scheduler = resolve_scheduler(opts.scheduler);
2506 validate_scheduler(opts.scheduler);
2507
Derrick Stolee2fec6042020-09-11 17:49:18 +00002508 if (maintenance_register())
2509 warning(_("failed to add repo to global config"));
Lénaïc Huardeba1ba92021-09-04 22:54:59 +02002510 return update_background_schedule(&opts, 1);
Derrick Stolee2fec6042020-09-11 17:49:18 +00002511}
2512
2513static int maintenance_stop(void)
2514{
Lénaïc Huardeba1ba92021-09-04 22:54:59 +02002515 return update_background_schedule(NULL, 0);
Derrick Stolee2fec6042020-09-11 17:49:18 +00002516}
2517
Derrick Stolee0c18b702020-09-11 17:49:17 +00002518static const char builtin_maintenance_usage[] = N_("git maintenance <subcommand> [<options>]");
Derrick Stolee2057d752020-09-17 18:11:42 +00002519
2520int cmd_maintenance(int argc, const char **argv, const char *prefix)
2521{
2522 if (argc < 2 ||
2523 (argc == 2 && !strcmp(argv[1], "-h")))
2524 usage(builtin_maintenance_usage);
2525
2526 if (!strcmp(argv[1], "run"))
2527 return maintenance_run(argc - 1, argv + 1, prefix);
Derrick Stolee2fec6042020-09-11 17:49:18 +00002528 if (!strcmp(argv[1], "start"))
Lénaïc Huardeba1ba92021-09-04 22:54:59 +02002529 return maintenance_start(argc - 1, argv + 1, prefix);
Derrick Stolee2fec6042020-09-11 17:49:18 +00002530 if (!strcmp(argv[1], "stop"))
2531 return maintenance_stop();
Derrick Stolee0c18b702020-09-11 17:49:17 +00002532 if (!strcmp(argv[1], "register"))
2533 return maintenance_register();
2534 if (!strcmp(argv[1], "unregister"))
2535 return maintenance_unregister();
Derrick Stolee2057d752020-09-17 18:11:42 +00002536
2537 die(_("invalid subcommand: %s"), argv[1]);
2538}