blob: db76af4f31cefa966540565ad1a91dcfba71802a [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 Stoleee841a792020-09-25 12:33:38 +000033#include "object-store.h"
Derrick Stolee2fec6042020-09-11 17:49:18 +000034#include "exec-cmd.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;
Jeff King07e7dbf2016-08-11 12:13:09 -040045static int aggressive_depth = 50;
Johannes Schindelin1c192f32007-12-06 12:03:38 +000046static int aggressive_window = 250;
Junio C Hamano2c3c4392007-09-05 13:01:37 -070047static int gc_auto_threshold = 6700;
Junio C Hamano97063972008-03-23 00:04:48 -070048static int gc_auto_pack_limit = 50;
Nguyễn Thái Ngọc Duy9f673f92014-02-08 14:08:52 +070049static int detach_auto = 1;
Johannes Schindelindddbad72017-04-26 21:29:31 +020050static timestamp_t gc_log_expire_time;
David Turnera831c062017-02-10 16:28:22 -050051static const char *gc_log_expire = "1.day.ago";
David Brysond3154b42008-09-30 13:28:58 -070052static const char *prune_expire = "2.weeks.ago";
Nguyễn Thái Ngọc Duye3df33b2014-11-30 15:24:53 +070053static const char *prune_worktrees_expire = "3.months.ago";
Nguyễn Thái Ngọc Duy55dfe132018-04-15 17:36:15 +020054static unsigned long big_pack_threshold;
Nguyễn Thái Ngọc Duy9806f5a2018-04-15 17:36:17 +020055static unsigned long max_delta_cache_size = DEFAULT_DELTA_CACHE_SIZE;
James Bowes6757ada2007-03-13 21:58:22 -040056
Jeff King22f9b7f2020-07-28 16:24:27 -040057static struct strvec reflog = STRVEC_INIT;
58static struct strvec repack = STRVEC_INIT;
59static struct strvec prune = STRVEC_INIT;
60static struct strvec prune_worktrees = STRVEC_INIT;
61static struct strvec rerere = STRVEC_INIT;
James Bowes6757ada2007-03-13 21:58:22 -040062
Jeff King076aa2c2017-09-05 08:15:08 -040063static struct tempfile *pidfile;
Nguyễn Thái Ngọc Duy329e6e82015-09-19 12:13:23 +070064static struct lock_file log_lock;
Jonathan Nieder4c5baf02013-10-16 16:11:46 -070065
Doug Kelly478f34d2015-11-03 21:05:08 -060066static struct string_list pack_garbage = STRING_LIST_INIT_DUP;
67
68static void clean_pack_garbage(void)
69{
70 int i;
71 for (i = 0; i < pack_garbage.nr; i++)
72 unlink_or_warn(pack_garbage.items[i].string);
73 string_list_clear(&pack_garbage, 0);
74}
75
76static void report_pack_garbage(unsigned seen_bits, const char *path)
77{
78 if (seen_bits == PACKDIR_FILE_IDX)
79 string_list_append(&pack_garbage, path);
80}
81
Nguyễn Thái Ngọc Duy329e6e82015-09-19 12:13:23 +070082static void process_log_file(void)
83{
84 struct stat st;
David Turnera831c062017-02-10 16:28:22 -050085 if (fstat(get_lock_file_fd(&log_lock), &st)) {
86 /*
87 * Perhaps there was an i/o error or another
88 * unlikely situation. Try to make a note of
89 * this in gc.log along with any existing
90 * messages.
91 */
92 int saved_errno = errno;
93 fprintf(stderr, _("Failed to fstat %s: %s"),
Martin Ågrend4a49762021-01-05 20:23:46 +010094 get_lock_file_path(&log_lock),
David Turnera831c062017-02-10 16:28:22 -050095 strerror(saved_errno));
96 fflush(stderr);
Nguyễn Thái Ngọc Duy329e6e82015-09-19 12:13:23 +070097 commit_lock_file(&log_lock);
David Turnera831c062017-02-10 16:28:22 -050098 errno = saved_errno;
99 } else if (st.st_size) {
100 /* There was some error recorded in the lock file */
101 commit_lock_file(&log_lock);
102 } else {
103 /* No error, clean up any old gc.log */
104 unlink(git_path("gc.log"));
Nguyễn Thái Ngọc Duy329e6e82015-09-19 12:13:23 +0700105 rollback_lock_file(&log_lock);
David Turnera831c062017-02-10 16:28:22 -0500106 }
Nguyễn Thái Ngọc Duy329e6e82015-09-19 12:13:23 +0700107}
108
109static void process_log_file_at_exit(void)
110{
111 fflush(stderr);
112 process_log_file();
113}
114
115static void process_log_file_on_signal(int signo)
116{
117 process_log_file();
118 sigchain_pop(signo);
119 raise(signo);
120}
121
Ævar Arnfjörð Bjarmasonbf3d70f2019-03-28 17:14:34 +0100122static int gc_config_is_timestamp_never(const char *var)
123{
124 const char *value;
125 timestamp_t expire;
126
127 if (!git_config_get_value(var, &value) && value) {
128 if (parse_expiry_date(value, &expire))
129 die(_("failed to parse '%s' value '%s'"), var, value);
130 return expire == 0;
131 }
132 return 0;
133}
134
Tanay Abhra5801d3b2014-08-07 09:21:22 -0700135static void gc_config(void)
James Bowes6757ada2007-03-13 21:58:22 -0400136{
Tanay Abhra5801d3b2014-08-07 09:21:22 -0700137 const char *value;
138
139 if (!git_config_get_value("gc.packrefs", &value)) {
Miklos Vajnac5e5a2c2008-02-08 15:26:18 +0100140 if (value && !strcmp(value, "notbare"))
James Bowes6757ada2007-03-13 21:58:22 -0400141 pack_refs = -1;
142 else
Tanay Abhra5801d3b2014-08-07 09:21:22 -0700143 pack_refs = git_config_bool("gc.packrefs", value);
James Bowes6757ada2007-03-13 21:58:22 -0400144 }
Tanay Abhra5801d3b2014-08-07 09:21:22 -0700145
Ævar Arnfjörð Bjarmasonbf3d70f2019-03-28 17:14:34 +0100146 if (gc_config_is_timestamp_never("gc.reflogexpire") &&
147 gc_config_is_timestamp_never("gc.reflogexpireunreachable"))
148 prune_reflogs = 0;
149
Tanay Abhra5801d3b2014-08-07 09:21:22 -0700150 git_config_get_int("gc.aggressivewindow", &aggressive_window);
151 git_config_get_int("gc.aggressivedepth", &aggressive_depth);
152 git_config_get_int("gc.auto", &gc_auto_threshold);
153 git_config_get_int("gc.autopacklimit", &gc_auto_pack_limit);
154 git_config_get_bool("gc.autodetach", &detach_auto);
Christian Couder77d67972017-02-27 19:00:13 +0100155 git_config_get_expiry("gc.pruneexpire", &prune_expire);
156 git_config_get_expiry("gc.worktreepruneexpire", &prune_worktrees_expire);
Junio C Hamano94c9b5a2017-03-17 13:50:23 -0700157 git_config_get_expiry("gc.logexpiry", &gc_log_expire);
David Turnera831c062017-02-10 16:28:22 -0500158
Nguyễn Thái Ngọc Duy55dfe132018-04-15 17:36:15 +0200159 git_config_get_ulong("gc.bigpackthreshold", &big_pack_threshold);
Nguyễn Thái Ngọc Duy9806f5a2018-04-15 17:36:17 +0200160 git_config_get_ulong("pack.deltacachesize", &max_delta_cache_size);
Nguyễn Thái Ngọc Duy55dfe132018-04-15 17:36:15 +0200161
Tanay Abhra5801d3b2014-08-07 09:21:22 -0700162 git_config(git_default_config, NULL);
James Bowes6757ada2007-03-13 21:58:22 -0400163}
164
Derrick Stolee41abfe12021-02-09 13:42:28 +0000165struct maintenance_run_opts;
166static int maintenance_task_pack_refs(MAYBE_UNUSED struct maintenance_run_opts *opts)
167{
168 struct strvec pack_refs_cmd = STRVEC_INIT;
169 strvec_pushl(&pack_refs_cmd, "pack-refs", "--all", "--prune", NULL);
170
171 return run_command_v_opt(pack_refs_cmd.v, RUN_GIT_CMD);
172}
173
Junio C Hamanoa087cc92007-09-17 00:44:17 -0700174static int too_many_loose_objects(void)
Junio C Hamano2c3c4392007-09-05 13:01:37 -0700175{
176 /*
177 * Quickly check if a "gc" is needed, by estimating how
178 * many loose objects there are. Because SHA-1 is evenly
179 * distributed, we can check only one and get a reasonable
180 * estimate.
181 */
Junio C Hamano2c3c4392007-09-05 13:01:37 -0700182 DIR *dir;
183 struct dirent *ent;
184 int auto_threshold;
185 int num_loose = 0;
186 int needed = 0;
Ævar Arnfjörð Bjarmasone5cdbd52019-03-15 16:59:53 +0100187 const unsigned hexsz_loose = the_hash_algo->hexsz - 2;
Junio C Hamano2c3c4392007-09-05 13:01:37 -0700188
Jeff King07af8892017-03-28 15:47:03 -0400189 dir = opendir(git_path("objects/17"));
Junio C Hamano2c3c4392007-09-05 13:01:37 -0700190 if (!dir)
191 return 0;
192
René Scharfe42c78a22017-07-08 12:35:35 +0200193 auto_threshold = DIV_ROUND_UP(gc_auto_threshold, 256);
Junio C Hamano2c3c4392007-09-05 13:01:37 -0700194 while ((ent = readdir(dir)) != NULL) {
Ævar Arnfjörð Bjarmasone5cdbd52019-03-15 16:59:53 +0100195 if (strspn(ent->d_name, "0123456789abcdef") != hexsz_loose ||
196 ent->d_name[hexsz_loose] != '\0')
Junio C Hamano2c3c4392007-09-05 13:01:37 -0700197 continue;
198 if (++num_loose > auto_threshold) {
199 needed = 1;
200 break;
201 }
202 }
203 closedir(dir);
204 return needed;
205}
206
Nguyễn Thái Ngọc Duy9806f5a2018-04-15 17:36:17 +0200207static struct packed_git *find_base_packs(struct string_list *packs,
208 unsigned long limit)
Nguyễn Thái Ngọc Duyae4e89e2018-04-15 17:36:14 +0200209{
210 struct packed_git *p, *base = NULL;
211
Derrick Stolee454ea2e2018-08-20 16:52:04 +0000212 for (p = get_all_packs(the_repository); p; p = p->next) {
Nguyễn Thái Ngọc Duyae4e89e2018-04-15 17:36:14 +0200213 if (!p->pack_local)
214 continue;
Nguyễn Thái Ngọc Duy55dfe132018-04-15 17:36:15 +0200215 if (limit) {
216 if (p->pack_size >= limit)
217 string_list_append(packs, p->pack_name);
218 } else if (!base || base->pack_size < p->pack_size) {
Nguyễn Thái Ngọc Duyae4e89e2018-04-15 17:36:14 +0200219 base = p;
220 }
221 }
222
223 if (base)
224 string_list_append(packs, base->pack_name);
Nguyễn Thái Ngọc Duy9806f5a2018-04-15 17:36:17 +0200225
226 return base;
Nguyễn Thái Ngọc Duyae4e89e2018-04-15 17:36:14 +0200227}
228
Junio C Hamano17815502007-09-17 00:55:13 -0700229static int too_many_packs(void)
230{
231 struct packed_git *p;
232 int cnt;
233
234 if (gc_auto_pack_limit <= 0)
235 return 0;
236
Derrick Stolee454ea2e2018-08-20 16:52:04 +0000237 for (cnt = 0, p = get_all_packs(the_repository); p; p = p->next) {
Junio C Hamano17815502007-09-17 00:55:13 -0700238 if (!p->pack_local)
239 continue;
Brandon Casey01af2492008-11-12 11:59:07 -0600240 if (p->pack_keep)
Junio C Hamano17815502007-09-17 00:55:13 -0700241 continue;
242 /*
243 * Perhaps check the size of the pack and count only
244 * very small ones here?
245 */
246 cnt++;
247 }
Eric Wong5f4e3bf2016-06-25 06:46:47 +0000248 return gc_auto_pack_limit < cnt;
Junio C Hamano17815502007-09-17 00:55:13 -0700249}
250
Nguyễn Thái Ngọc Duy9806f5a2018-04-15 17:36:17 +0200251static uint64_t total_ram(void)
252{
253#if defined(HAVE_SYSINFO)
254 struct sysinfo si;
255
256 if (!sysinfo(&si))
257 return si.totalram;
258#elif defined(HAVE_BSD_SYSCTL) && (defined(HW_MEMSIZE) || defined(HW_PHYSMEM))
259 int64_t physical_memory;
260 int mib[2];
261 size_t length;
262
263 mib[0] = CTL_HW;
264# if defined(HW_MEMSIZE)
265 mib[1] = HW_MEMSIZE;
266# else
267 mib[1] = HW_PHYSMEM;
268# endif
269 length = sizeof(int64_t);
270 if (!sysctl(mib, 2, &physical_memory, &length, NULL, 0))
271 return physical_memory;
272#elif defined(GIT_WINDOWS_NATIVE)
273 MEMORYSTATUSEX memInfo;
274
275 memInfo.dwLength = sizeof(MEMORYSTATUSEX);
276 if (GlobalMemoryStatusEx(&memInfo))
277 return memInfo.ullTotalPhys;
278#endif
279 return 0;
280}
281
282static uint64_t estimate_repack_memory(struct packed_git *pack)
283{
284 unsigned long nr_objects = approximate_object_count();
285 size_t os_cache, heap;
286
287 if (!pack || !nr_objects)
288 return 0;
289
290 /*
291 * First we have to scan through at least one pack.
292 * Assume enough room in OS file cache to keep the entire pack
293 * or we may accidentally evict data of other processes from
294 * the cache.
295 */
296 os_cache = pack->pack_size + pack->index_size;
297 /* then pack-objects needs lots more for book keeping */
298 heap = sizeof(struct object_entry) * nr_objects;
299 /*
300 * internal rev-list --all --objects takes up some memory too,
301 * let's say half of it is for blobs
302 */
303 heap += sizeof(struct blob) * nr_objects / 2;
304 /*
305 * and the other half is for trees (commits and tags are
306 * usually insignificant)
307 */
308 heap += sizeof(struct tree) * nr_objects / 2;
309 /* and then obj_hash[], underestimated in fact */
310 heap += sizeof(struct object *) * nr_objects;
311 /* revindex is used also */
Taylor Blau2891b432021-01-13 17:24:54 -0500312 heap += (sizeof(off_t) + sizeof(uint32_t)) * nr_objects;
Nguyễn Thái Ngọc Duy9806f5a2018-04-15 17:36:17 +0200313 /*
314 * read_sha1_file() (either at delta calculation phase, or
315 * writing phase) also fills up the delta base cache
316 */
317 heap += delta_base_cache_limit;
318 /* and of course pack-objects has its own delta cache */
319 heap += max_delta_cache_size;
320
321 return os_cache + heap;
322}
323
Nguyễn Thái Ngọc Duyae4e89e2018-04-15 17:36:14 +0200324static int keep_one_pack(struct string_list_item *item, void *data)
325{
Jeff King22f9b7f2020-07-28 16:24:27 -0400326 strvec_pushf(&repack, "--keep-pack=%s", basename(item->string));
Nguyễn Thái Ngọc Duyae4e89e2018-04-15 17:36:14 +0200327 return 0;
328}
329
330static void add_repack_all_option(struct string_list *keep_pack)
Jeff King7e52f562012-04-07 06:30:09 -0400331{
332 if (prune_expire && !strcmp(prune_expire, "now"))
Jeff King22f9b7f2020-07-28 16:24:27 -0400333 strvec_push(&repack, "-a");
Jeff King7e52f562012-04-07 06:30:09 -0400334 else {
Jeff King22f9b7f2020-07-28 16:24:27 -0400335 strvec_push(&repack, "-A");
Jeff King234587f2012-04-18 14:10:19 -0700336 if (prune_expire)
Jeff King22f9b7f2020-07-28 16:24:27 -0400337 strvec_pushf(&repack, "--unpack-unreachable=%s", prune_expire);
Jeff King7e52f562012-04-07 06:30:09 -0400338 }
Nguyễn Thái Ngọc Duyae4e89e2018-04-15 17:36:14 +0200339
340 if (keep_pack)
341 for_each_string_list(keep_pack, keep_one_pack, NULL);
Jeff King7e52f562012-04-07 06:30:09 -0400342}
343
David Turnerbdf56de2016-12-28 17:45:41 -0500344static void add_repack_incremental_option(void)
345{
Jeff King22f9b7f2020-07-28 16:24:27 -0400346 strvec_push(&repack, "--no-write-bitmap-index");
David Turnerbdf56de2016-12-28 17:45:41 -0500347}
348
Junio C Hamanoa087cc92007-09-17 00:44:17 -0700349static int need_to_gc(void)
350{
351 /*
Brandon Caseyb14d2552008-03-19 16:53:20 -0500352 * Setting gc.auto to 0 or negative can disable the
353 * automatic gc.
Junio C Hamanoa087cc92007-09-17 00:44:17 -0700354 */
Brandon Caseyb14d2552008-03-19 16:53:20 -0500355 if (gc_auto_threshold <= 0)
Junio C Hamanoa087cc92007-09-17 00:44:17 -0700356 return 0;
357
Junio C Hamano17815502007-09-17 00:55:13 -0700358 /*
359 * If there are too many loose objects, but not too many
360 * packs, we run "repack -d -l". If there are too many packs,
361 * we run "repack -A -d -l". Otherwise we tell the caller
362 * there is no need.
363 */
Nguyễn Thái Ngọc Duy55dfe132018-04-15 17:36:15 +0200364 if (too_many_packs()) {
365 struct string_list keep_pack = STRING_LIST_INIT_NODUP;
366
Nguyễn Thái Ngọc Duy8fc67762018-04-15 17:36:16 +0200367 if (big_pack_threshold) {
Nguyễn Thái Ngọc Duy55dfe132018-04-15 17:36:15 +0200368 find_base_packs(&keep_pack, big_pack_threshold);
Nguyễn Thái Ngọc Duy8fc67762018-04-15 17:36:16 +0200369 if (keep_pack.nr >= gc_auto_pack_limit) {
370 big_pack_threshold = 0;
371 string_list_clear(&keep_pack, 0);
372 find_base_packs(&keep_pack, 0);
373 }
Nguyễn Thái Ngọc Duy9806f5a2018-04-15 17:36:17 +0200374 } else {
375 struct packed_git *p = find_base_packs(&keep_pack, 0);
376 uint64_t mem_have, mem_want;
377
378 mem_have = total_ram();
379 mem_want = estimate_repack_memory(p);
380
381 /*
382 * Only allow 1/2 of memory for pack-objects, leave
383 * the rest for the OS and other processes in the
384 * system.
385 */
386 if (!mem_have || mem_want < mem_have / 2)
387 string_list_clear(&keep_pack, 0);
Nguyễn Thái Ngọc Duy8fc67762018-04-15 17:36:16 +0200388 }
Nguyễn Thái Ngọc Duy55dfe132018-04-15 17:36:15 +0200389
390 add_repack_all_option(&keep_pack);
391 string_list_clear(&keep_pack, 0);
392 } else if (too_many_loose_objects())
David Turnerbdf56de2016-12-28 17:45:41 -0500393 add_repack_incremental_option();
394 else
Junio C Hamano17815502007-09-17 00:55:13 -0700395 return 0;
Miklos Vajnabde30542008-04-02 21:34:38 +0200396
Benoit Pierre15048f82014-03-18 11:00:53 +0100397 if (run_hook_le(NULL, "pre-auto-gc", NULL))
Miklos Vajnabde30542008-04-02 21:34:38 +0200398 return 0;
Junio C Hamano95143f92007-09-17 00:48:39 -0700399 return 1;
Junio C Hamanoa087cc92007-09-17 00:44:17 -0700400}
401
Nguyễn Thái Ngọc Duy64a99eb2013-08-08 18:05:38 +0700402/* return NULL on success, else hostname running the gc */
403static const char *lock_repo_for_gc(int force, pid_t* ret_pid)
404{
Martin Ågrenb2275862018-05-09 22:55:38 +0200405 struct lock_file lock = LOCK_INIT;
René Scharfeda25bdb2017-04-18 17:57:42 -0400406 char my_host[HOST_NAME_MAX + 1];
Nguyễn Thái Ngọc Duy64a99eb2013-08-08 18:05:38 +0700407 struct strbuf sb = STRBUF_INIT;
408 struct stat st;
409 uintmax_t pid;
410 FILE *fp;
Elia Pinto4f1c0b22014-01-29 08:59:37 -0800411 int fd;
Michael Haggerty00539ce2015-08-10 11:47:48 +0200412 char *pidfile_path;
Nguyễn Thái Ngọc Duy64a99eb2013-08-08 18:05:38 +0700413
Jeff King076aa2c2017-09-05 08:15:08 -0400414 if (is_tempfile_active(pidfile))
Jonathan Nieder4c5baf02013-10-16 16:11:46 -0700415 /* already locked */
416 return NULL;
417
David Turner5781a9a2017-04-18 17:57:43 -0400418 if (xgethostname(my_host, sizeof(my_host)))
Jeff King5096d492015-09-24 17:06:08 -0400419 xsnprintf(my_host, sizeof(my_host), "unknown");
Nguyễn Thái Ngọc Duy64a99eb2013-08-08 18:05:38 +0700420
Michael Haggerty00539ce2015-08-10 11:47:48 +0200421 pidfile_path = git_pathdup("gc.pid");
422 fd = hold_lock_file_for_update(&lock, pidfile_path,
Nguyễn Thái Ngọc Duy64a99eb2013-08-08 18:05:38 +0700423 LOCK_DIE_ON_ERROR);
424 if (!force) {
René Scharfeda25bdb2017-04-18 17:57:42 -0400425 static char locking_host[HOST_NAME_MAX + 1];
426 static char *scan_fmt;
Elia Pinto4f1c0b22014-01-29 08:59:37 -0800427 int should_exit;
René Scharfeda25bdb2017-04-18 17:57:42 -0400428
429 if (!scan_fmt)
Junio C Hamanoafe2fab2017-09-17 12:16:55 +0900430 scan_fmt = xstrfmt("%s %%%ds", "%"SCNuMAX, HOST_NAME_MAX);
Michael Haggerty00539ce2015-08-10 11:47:48 +0200431 fp = fopen(pidfile_path, "r");
Nguyễn Thái Ngọc Duy64a99eb2013-08-08 18:05:38 +0700432 memset(locking_host, 0, sizeof(locking_host));
433 should_exit =
434 fp != NULL &&
435 !fstat(fileno(fp), &st) &&
436 /*
437 * 12 hour limit is very generous as gc should
438 * never take that long. On the other hand we
439 * don't really need a strict limit here,
440 * running gc --auto one day late is not a big
441 * problem. --force can be used in manual gc
442 * after the user verifies that no gc is
443 * running.
444 */
445 time(NULL) - st.st_mtime <= 12 * 3600 &&
René Scharfeda25bdb2017-04-18 17:57:42 -0400446 fscanf(fp, scan_fmt, &pid, locking_host) == 2 &&
Nguyễn Thái Ngọc Duy64a99eb2013-08-08 18:05:38 +0700447 /* be gentle to concurrent "gc" on remote hosts */
Kyle J. McKayed7eda82013-12-31 04:07:39 -0800448 (strcmp(locking_host, my_host) || !kill(pid, 0) || errno == EPERM);
Nguyễn Thái Ngọc Duy64a99eb2013-08-08 18:05:38 +0700449 if (fp != NULL)
450 fclose(fp);
451 if (should_exit) {
452 if (fd >= 0)
453 rollback_lock_file(&lock);
454 *ret_pid = pid;
Michael Haggerty00539ce2015-08-10 11:47:48 +0200455 free(pidfile_path);
Nguyễn Thái Ngọc Duy64a99eb2013-08-08 18:05:38 +0700456 return locking_host;
457 }
458 }
459
460 strbuf_addf(&sb, "%"PRIuMAX" %s",
461 (uintmax_t) getpid(), my_host);
462 write_in_full(fd, sb.buf, sb.len);
463 strbuf_release(&sb);
464 commit_lock_file(&lock);
Jeff King076aa2c2017-09-05 08:15:08 -0400465 pidfile = register_tempfile(pidfile_path);
Michael Haggertyebebeae2015-08-10 11:47:49 +0200466 free(pidfile_path);
Nguyễn Thái Ngọc Duy64a99eb2013-08-08 18:05:38 +0700467 return NULL;
468}
469
Jonathan Nieder30299702018-07-16 23:57:40 -0700470/*
471 * Returns 0 if there was no previous error and gc can proceed, 1 if
472 * gc should not proceed due to an error in the last run. Prints a
Elijah Newren15beaaa2019-11-05 17:07:23 +0000473 * message and returns -1 if an error occurred while reading gc.log
Jonathan Nieder30299702018-07-16 23:57:40 -0700474 */
475static int report_last_gc_error(void)
Nguyễn Thái Ngọc Duy329e6e82015-09-19 12:13:23 +0700476{
477 struct strbuf sb = STRBUF_INIT;
Jonathan Nieder30299702018-07-16 23:57:40 -0700478 int ret = 0;
Jonathan Nieder3c426ec2018-07-16 23:53:21 -0700479 ssize_t len;
David Turnera831c062017-02-10 16:28:22 -0500480 struct stat st;
481 char *gc_log_path = git_pathdup("gc.log");
Nguyễn Thái Ngọc Duy329e6e82015-09-19 12:13:23 +0700482
David Turnera831c062017-02-10 16:28:22 -0500483 if (stat(gc_log_path, &st)) {
484 if (errno == ENOENT)
485 goto done;
486
Jonathan Nieder30299702018-07-16 23:57:40 -0700487 ret = error_errno(_("cannot stat '%s'"), gc_log_path);
488 goto done;
David Turnera831c062017-02-10 16:28:22 -0500489 }
490
491 if (st.st_mtime < gc_log_expire_time)
492 goto done;
493
Jonathan Nieder3c426ec2018-07-16 23:53:21 -0700494 len = strbuf_read_file(&sb, gc_log_path, 0);
495 if (len < 0)
Jonathan Nieder30299702018-07-16 23:57:40 -0700496 ret = error_errno(_("cannot read '%s'"), gc_log_path);
497 else if (len > 0) {
498 /*
499 * A previous gc failed. Report the error, and don't
500 * bother with an automatic gc run since it is likely
501 * to fail in the same way.
502 */
503 warning(_("The last gc run reported the following. "
Nguyễn Thái Ngọc Duy329e6e82015-09-19 12:13:23 +0700504 "Please correct the root cause\n"
Ævar Arnfjörð Bjarmasonb45c1722021-08-31 16:37:29 +0200505 "and remove %s\n"
Nguyễn Thái Ngọc Duy329e6e82015-09-19 12:13:23 +0700506 "Automatic cleanup will not be performed "
507 "until the file is removed.\n\n"
508 "%s"),
David Turnera831c062017-02-10 16:28:22 -0500509 gc_log_path, sb.buf);
Jonathan Nieder30299702018-07-16 23:57:40 -0700510 ret = 1;
511 }
Nguyễn Thái Ngọc Duy329e6e82015-09-19 12:13:23 +0700512 strbuf_release(&sb);
David Turnera831c062017-02-10 16:28:22 -0500513done:
514 free(gc_log_path);
Jonathan Nieder30299702018-07-16 23:57:40 -0700515 return ret;
Nguyễn Thái Ngọc Duy329e6e82015-09-19 12:13:23 +0700516}
517
Jonathan Niederfec2ed22018-07-16 23:54:16 -0700518static void gc_before_repack(void)
Nguyễn Thái Ngọc Duy62aad182014-05-25 07:38:29 +0700519{
Ævar Arnfjörð Bjarmasoncd8eb3a2019-03-15 16:59:54 +0100520 /*
521 * We may be called twice, as both the pre- and
522 * post-daemonized phases will call us, but running these
523 * commands more than once is pointless and wasteful.
524 */
525 static int done = 0;
526 if (done++)
527 return;
528
Derrick Stolee41abfe12021-02-09 13:42:28 +0000529 if (pack_refs && maintenance_task_pack_refs(NULL))
530 die(FAILED_RUN, "pack-refs");
Nguyễn Thái Ngọc Duy62aad182014-05-25 07:38:29 +0700531
Jeff Kingd70a9eb2020-07-28 20:37:20 -0400532 if (prune_reflogs && run_command_v_opt(reflog.v, RUN_GIT_CMD))
533 die(FAILED_RUN, reflog.v[0]);
Nguyễn Thái Ngọc Duy62aad182014-05-25 07:38:29 +0700534}
535
James Bowes6757ada2007-03-13 21:58:22 -0400536int cmd_gc(int argc, const char **argv, const char *prefix)
537{
James Bowes44c637c2007-11-01 21:02:27 -0400538 int aggressive = 0;
Junio C Hamano2c3c4392007-09-05 13:01:37 -0700539 int auto_gc = 0;
Frank Lichtenhelda0c14cb2008-02-29 22:53:39 +0100540 int quiet = 0;
Nguyễn Thái Ngọc Duy64a99eb2013-08-08 18:05:38 +0700541 int force = 0;
542 const char *name;
543 pid_t pid;
Nguyễn Thái Ngọc Duy329e6e82015-09-19 12:13:23 +0700544 int daemonized = 0;
Ævar Arnfjörð Bjarmason793c1462020-11-20 12:55:22 +0100545 int keep_largest_pack = -1;
Junio C Hamano8ab5aa42018-04-21 12:13:13 +0900546 timestamp_t dummy;
James Bowes6757ada2007-03-13 21:58:22 -0400547
James Bowes44c637c2007-11-01 21:02:27 -0400548 struct option builtin_gc_options[] = {
Nguyễn Thái Ngọc Duy6705c162012-08-20 19:32:14 +0700549 OPT__QUIET(&quiet, N_("suppress progress reporting")),
550 { OPTION_STRING, 0, "prune", &prune_expire, N_("date"),
551 N_("prune unreferenced objects"),
Johannes Schindelin58e9d9d2009-02-14 23:10:10 +0100552 PARSE_OPT_OPTARG, NULL, (intptr_t)prune_expire },
Stefan Bellerd5d09d42013-08-03 13:51:19 +0200553 OPT_BOOL(0, "aggressive", &aggressive, N_("be more thorough (increased runtime)")),
Nguyễn Thái Ngọc Duy7e1eeaa2018-02-09 18:01:58 +0700554 OPT_BOOL_F(0, "auto", &auto_gc, N_("enable auto-gc mode"),
555 PARSE_OPT_NOCOMPLETE),
556 OPT_BOOL_F(0, "force", &force,
557 N_("force running gc even if there may be another gc running"),
558 PARSE_OPT_NOCOMPLETE),
Ævar Arnfjörð Bjarmason793c1462020-11-20 12:55:22 +0100559 OPT_BOOL(0, "keep-largest-pack", &keep_largest_pack,
Nguyễn Thái Ngọc Duyae4e89e2018-04-15 17:36:14 +0200560 N_("repack all other packs except the largest pack")),
James Bowes44c637c2007-11-01 21:02:27 -0400561 OPT_END()
562 };
563
Nguyễn Thái Ngọc Duy0c8151b2010-10-22 01:47:19 -0500564 if (argc == 2 && !strcmp(argv[1], "-h"))
565 usage_with_options(builtin_gc_usage, builtin_gc_options);
566
Jeff King22f9b7f2020-07-28 16:24:27 -0400567 strvec_pushl(&reflog, "reflog", "expire", "--all", NULL);
568 strvec_pushl(&repack, "repack", "-d", "-l", NULL);
569 strvec_pushl(&prune, "prune", "--expire", NULL);
570 strvec_pushl(&prune_worktrees, "worktree", "prune", "--expire", NULL);
571 strvec_pushl(&rerere, "rerere", "gc", NULL);
Jeff King234587f2012-04-18 14:10:19 -0700572
David Turnera831c062017-02-10 16:28:22 -0500573 /* default expiry time, overwritten in gc_config */
Tanay Abhra5801d3b2014-08-07 09:21:22 -0700574 gc_config();
David Turnera831c062017-02-10 16:28:22 -0500575 if (parse_expiry_date(gc_log_expire, &gc_log_expire_time))
Junio C Hamano96913c92018-04-23 22:36:14 +0900576 die(_("failed to parse gc.logexpiry value %s"), gc_log_expire);
James Bowes6757ada2007-03-13 21:58:22 -0400577
578 if (pack_refs < 0)
579 pack_refs = !is_bare_repository();
580
Stephen Boyd37782922009-05-23 11:53:12 -0700581 argc = parse_options(argc, argv, prefix, builtin_gc_options,
582 builtin_gc_usage, 0);
James Bowes44c637c2007-11-01 21:02:27 -0400583 if (argc > 0)
584 usage_with_options(builtin_gc_usage, builtin_gc_options);
585
Junio C Hamano8ab5aa42018-04-21 12:13:13 +0900586 if (prune_expire && parse_expiry_date(prune_expire, &dummy))
587 die(_("failed to parse prune expiry value %s"), prune_expire);
588
James Bowes44c637c2007-11-01 21:02:27 -0400589 if (aggressive) {
Jeff King22f9b7f2020-07-28 16:24:27 -0400590 strvec_push(&repack, "-f");
Nguyễn Thái Ngọc Duy125f8142014-03-16 20:35:03 +0700591 if (aggressive_depth > 0)
Jeff King22f9b7f2020-07-28 16:24:27 -0400592 strvec_pushf(&repack, "--depth=%d", aggressive_depth);
Jeff King234587f2012-04-18 14:10:19 -0700593 if (aggressive_window > 0)
Jeff King22f9b7f2020-07-28 16:24:27 -0400594 strvec_pushf(&repack, "--window=%d", aggressive_window);
James Bowes6757ada2007-03-13 21:58:22 -0400595 }
Frank Lichtenhelda0c14cb2008-02-29 22:53:39 +0100596 if (quiet)
Jeff King22f9b7f2020-07-28 16:24:27 -0400597 strvec_push(&repack, "-q");
James Bowes6757ada2007-03-13 21:58:22 -0400598
Junio C Hamano2c3c4392007-09-05 13:01:37 -0700599 if (auto_gc) {
600 /*
601 * Auto-gc should be least intrusive as possible.
602 */
Junio C Hamano2c3c4392007-09-05 13:01:37 -0700603 if (!need_to_gc())
604 return 0;
Nguyễn Thái Ngọc Duy9f673f92014-02-08 14:08:52 +0700605 if (!quiet) {
606 if (detach_auto)
607 fprintf(stderr, _("Auto packing the repository in background for optimum performance.\n"));
608 else
609 fprintf(stderr, _("Auto packing the repository for optimum performance.\n"));
610 fprintf(stderr, _("See \"git help gc\" for manual housekeeping.\n"));
611 }
Nguyễn Thái Ngọc Duy62aad182014-05-25 07:38:29 +0700612 if (detach_auto) {
Jonathan Nieder30299702018-07-16 23:57:40 -0700613 int ret = report_last_gc_error();
614 if (ret < 0)
Elijah Newren15beaaa2019-11-05 17:07:23 +0000615 /* an I/O error occurred, already reported */
Jonathan Nieder30299702018-07-16 23:57:40 -0700616 exit(128);
617 if (ret == 1)
618 /* Last gc --auto failed. Skip this one. */
619 return 0;
Nguyễn Thái Ngọc Duy329e6e82015-09-19 12:13:23 +0700620
Jeff Kingc45af942017-07-11 05:06:35 -0400621 if (lock_repo_for_gc(force, &pid))
622 return 0;
Jonathan Niederfec2ed22018-07-16 23:54:16 -0700623 gc_before_repack(); /* dies on failure */
Jeff Kingc45af942017-07-11 05:06:35 -0400624 delete_tempfile(&pidfile);
625
Nguyễn Thái Ngọc Duy9f673f92014-02-08 14:08:52 +0700626 /*
627 * failure to daemonize is ok, we'll continue
628 * in foreground
629 */
Nguyễn Thái Ngọc Duy329e6e82015-09-19 12:13:23 +0700630 daemonized = !daemonize();
Nguyễn Thái Ngọc Duy62aad182014-05-25 07:38:29 +0700631 }
Nguyễn Thái Ngọc Duyae4e89e2018-04-15 17:36:14 +0200632 } else {
633 struct string_list keep_pack = STRING_LIST_INIT_NODUP;
634
Ævar Arnfjörð Bjarmason793c1462020-11-20 12:55:22 +0100635 if (keep_largest_pack != -1) {
636 if (keep_largest_pack)
Nguyễn Thái Ngọc Duy55dfe132018-04-15 17:36:15 +0200637 find_base_packs(&keep_pack, 0);
638 } else if (big_pack_threshold) {
639 find_base_packs(&keep_pack, big_pack_threshold);
Nguyễn Thái Ngọc Duyae4e89e2018-04-15 17:36:14 +0200640 }
641
642 add_repack_all_option(&keep_pack);
643 string_list_clear(&keep_pack, 0);
644 }
Junio C Hamano2c3c4392007-09-05 13:01:37 -0700645
Nguyễn Thái Ngọc Duy64a99eb2013-08-08 18:05:38 +0700646 name = lock_repo_for_gc(force, &pid);
647 if (name) {
648 if (auto_gc)
649 return 0; /* be quiet on --auto */
650 die(_("gc is already running on machine '%s' pid %"PRIuMAX" (use --force if not)"),
651 name, (uintmax_t)pid);
652 }
653
Nguyễn Thái Ngọc Duy329e6e82015-09-19 12:13:23 +0700654 if (daemonized) {
655 hold_lock_file_for_update(&log_lock,
656 git_path("gc.log"),
657 LOCK_DIE_ON_ERROR);
Junio C Hamano076c8272015-10-15 15:43:32 -0700658 dup2(get_lock_file_fd(&log_lock), 2);
Nguyễn Thái Ngọc Duy329e6e82015-09-19 12:13:23 +0700659 sigchain_push_common(process_log_file_on_signal);
660 atexit(process_log_file_at_exit);
661 }
662
Jonathan Niederfec2ed22018-07-16 23:54:16 -0700663 gc_before_repack();
James Bowes6757ada2007-03-13 21:58:22 -0400664
Jeff King067fbd42015-06-23 06:54:11 -0400665 if (!repository_format_precious_objects) {
Derrick Stolee2d511cf2019-05-17 11:41:49 -0700666 close_object_store(the_repository->objects);
Jeff Kingd70a9eb2020-07-28 20:37:20 -0400667 if (run_command_v_opt(repack.v, RUN_GIT_CMD))
668 die(FAILED_RUN, repack.v[0]);
James Bowes6757ada2007-03-13 21:58:22 -0400669
Jeff King067fbd42015-06-23 06:54:11 -0400670 if (prune_expire) {
Jeff King22f9b7f2020-07-28 16:24:27 -0400671 strvec_push(&prune, prune_expire);
Jeff King067fbd42015-06-23 06:54:11 -0400672 if (quiet)
Jeff King22f9b7f2020-07-28 16:24:27 -0400673 strvec_push(&prune, "--no-progress");
Christian Couderb14ed5a2019-06-25 15:40:31 +0200674 if (has_promisor_remote())
Jeff King22f9b7f2020-07-28 16:24:27 -0400675 strvec_push(&prune,
Jeff Kingf6d89422020-07-28 16:26:31 -0400676 "--exclude-promisor-objects");
Jeff Kingd70a9eb2020-07-28 20:37:20 -0400677 if (run_command_v_opt(prune.v, RUN_GIT_CMD))
678 die(FAILED_RUN, prune.v[0]);
Jeff King067fbd42015-06-23 06:54:11 -0400679 }
Johannes Schindelin58e9d9d2009-02-14 23:10:10 +0100680 }
James Bowes6757ada2007-03-13 21:58:22 -0400681
Nguyễn Thái Ngọc Duye3df33b2014-11-30 15:24:53 +0700682 if (prune_worktrees_expire) {
Jeff King22f9b7f2020-07-28 16:24:27 -0400683 strvec_push(&prune_worktrees, prune_worktrees_expire);
Jeff Kingd70a9eb2020-07-28 20:37:20 -0400684 if (run_command_v_opt(prune_worktrees.v, RUN_GIT_CMD))
685 die(FAILED_RUN, prune_worktrees.v[0]);
Nguyễn Thái Ngọc Duye3df33b2014-11-30 15:24:53 +0700686 }
687
Jeff Kingd70a9eb2020-07-28 20:37:20 -0400688 if (run_command_v_opt(rerere.v, RUN_GIT_CMD))
689 die(FAILED_RUN, rerere.v[0]);
James Bowes6757ada2007-03-13 21:58:22 -0400690
Doug Kelly478f34d2015-11-03 21:05:08 -0600691 report_garbage = report_pack_garbage;
Stefan Bellera49d2832018-03-23 18:45:21 +0100692 reprepare_packed_git(the_repository);
Johannes Schindelin5bdece02018-12-15 14:04:01 -0800693 if (pack_garbage.nr > 0) {
Derrick Stolee2d511cf2019-05-17 11:41:49 -0700694 close_object_store(the_repository->objects);
Doug Kelly478f34d2015-11-03 21:05:08 -0600695 clean_pack_garbage();
Johannes Schindelin5bdece02018-12-15 14:04:01 -0800696 }
Doug Kelly478f34d2015-11-03 21:05:08 -0600697
Derrick Stolee7211b9e2019-08-13 11:37:43 -0700698 prepare_repo_settings(the_repository);
699 if (the_repository->settings.gc_write_commit_graph == 1)
Taylor Blau0bd52e22020-02-03 21:51:50 -0800700 write_commit_graph_reachable(the_repository->objects->odb,
Junio C Hamanof4f8dfe2019-09-09 12:26:36 -0700701 !quiet && !daemonized ? COMMIT_GRAPH_WRITE_PROGRESS : 0,
Derrick Stolee7211b9e2019-08-13 11:37:43 -0700702 NULL);
Derrick Stoleed5d5d7b2018-06-27 09:24:46 -0400703
Junio C Hamanoa087cc92007-09-17 00:44:17 -0700704 if (auto_gc && too_many_loose_objects())
Ævar Arnfjörð Bjarmasonfea61282011-02-22 23:42:24 +0000705 warning(_("There are too many unreachable loose objects; "
706 "run 'git prune' to remove them."));
Junio C Hamanoa087cc92007-09-17 00:44:17 -0700707
David Turnera831c062017-02-10 16:28:22 -0500708 if (!daemonized)
709 unlink(git_path("gc.log"));
710
James Bowes6757ada2007-03-13 21:58:22 -0400711 return 0;
712}
Derrick Stolee2057d752020-09-17 18:11:42 +0000713
Derrick Stoleeb08ff1f2020-09-11 17:49:15 +0000714static const char *const builtin_maintenance_run_usage[] = {
715 N_("git maintenance run [--auto] [--[no-]quiet] [--task=<task>] [--schedule]"),
Derrick Stolee2057d752020-09-17 18:11:42 +0000716 NULL
717};
718
Derrick Stoleeb08ff1f2020-09-11 17:49:15 +0000719enum schedule_priority {
720 SCHEDULE_NONE = 0,
721 SCHEDULE_WEEKLY = 1,
722 SCHEDULE_DAILY = 2,
723 SCHEDULE_HOURLY = 3,
724};
725
726static enum schedule_priority parse_schedule(const char *value)
727{
728 if (!value)
729 return SCHEDULE_NONE;
730 if (!strcasecmp(value, "hourly"))
731 return SCHEDULE_HOURLY;
732 if (!strcasecmp(value, "daily"))
733 return SCHEDULE_DAILY;
734 if (!strcasecmp(value, "weekly"))
735 return SCHEDULE_WEEKLY;
736 return SCHEDULE_NONE;
737}
738
739static int maintenance_opt_schedule(const struct option *opt, const char *arg,
740 int unset)
741{
742 enum schedule_priority *priority = opt->value;
743
744 if (unset)
745 die(_("--no-schedule is not allowed"));
746
747 *priority = parse_schedule(arg);
748
749 if (!*priority)
750 die(_("unrecognized --schedule argument '%s'"), arg);
751
752 return 0;
753}
754
Derrick Stolee2057d752020-09-17 18:11:42 +0000755struct maintenance_run_opts {
756 int auto_flag;
Derrick Stolee3ddaad02020-09-17 18:11:43 +0000757 int quiet;
Derrick Stoleeb08ff1f2020-09-11 17:49:15 +0000758 enum schedule_priority schedule;
Derrick Stolee2057d752020-09-17 18:11:42 +0000759};
760
Derrick Stolee4ddc79b2020-09-17 18:11:51 +0000761/* Remember to update object flag allocation in object.h */
762#define SEEN (1u<<0)
763
764struct cg_auto_data {
765 int num_not_in_graph;
766 int limit;
767};
768
769static int dfs_on_ref(const char *refname,
770 const struct object_id *oid, int flags,
771 void *cb_data)
772{
773 struct cg_auto_data *data = (struct cg_auto_data *)cb_data;
774 int result = 0;
775 struct object_id peeled;
776 struct commit_list *stack = NULL;
777 struct commit *commit;
778
Jeff King36a31792021-01-20 14:44:43 -0500779 if (!peel_iterated_oid(oid, &peeled))
Derrick Stolee4ddc79b2020-09-17 18:11:51 +0000780 oid = &peeled;
781 if (oid_object_info(the_repository, oid, NULL) != OBJ_COMMIT)
782 return 0;
783
784 commit = lookup_commit(the_repository, oid);
785 if (!commit)
786 return 0;
Derrick Stolee8f801802020-10-08 00:50:39 +0000787 if (parse_commit(commit) ||
788 commit_graph_position(commit) != COMMIT_NOT_FROM_GRAPH)
Derrick Stolee4ddc79b2020-09-17 18:11:51 +0000789 return 0;
790
Derrick Stolee8f801802020-10-08 00:50:39 +0000791 data->num_not_in_graph++;
792
793 if (data->num_not_in_graph >= data->limit)
794 return 1;
795
Derrick Stolee4ddc79b2020-09-17 18:11:51 +0000796 commit_list_append(commit, &stack);
797
798 while (!result && stack) {
799 struct commit_list *parent;
800
801 commit = pop_commit(&stack);
802
803 for (parent = commit->parents; parent; parent = parent->next) {
804 if (parse_commit(parent->item) ||
805 commit_graph_position(parent->item) != COMMIT_NOT_FROM_GRAPH ||
806 parent->item->object.flags & SEEN)
807 continue;
808
809 parent->item->object.flags |= SEEN;
810 data->num_not_in_graph++;
811
812 if (data->num_not_in_graph >= data->limit) {
813 result = 1;
814 break;
815 }
816
817 commit_list_append(parent->item, &stack);
818 }
819 }
820
821 free_commit_list(stack);
822 return result;
823}
824
825static int should_write_commit_graph(void)
826{
827 int result;
828 struct cg_auto_data data;
829
830 data.num_not_in_graph = 0;
831 data.limit = 100;
832 git_config_get_int("maintenance.commit-graph.auto",
833 &data.limit);
834
835 if (!data.limit)
836 return 0;
837 if (data.limit < 0)
838 return 1;
839
840 result = for_each_ref(dfs_on_ref, &data);
841
René Scharfecd888842020-10-31 13:46:08 +0100842 repo_clear_commit_marks(the_repository, SEEN);
Derrick Stolee4ddc79b2020-09-17 18:11:51 +0000843
844 return result;
845}
846
Derrick Stolee663b2b12020-09-17 18:11:46 +0000847static int run_write_commit_graph(struct maintenance_run_opts *opts)
848{
849 struct child_process child = CHILD_PROCESS_INIT;
850
851 child.git_cmd = 1;
852 strvec_pushl(&child.args, "commit-graph", "write",
853 "--split", "--reachable", NULL);
854
855 if (opts->quiet)
856 strvec_push(&child.args, "--no-progress");
857
858 return !!run_command(&child);
859}
860
861static int maintenance_task_commit_graph(struct maintenance_run_opts *opts)
862{
Derrick Stoleed3341072020-10-12 13:28:34 +0000863 prepare_repo_settings(the_repository);
864 if (!the_repository->settings.core_commit_graph)
865 return 0;
866
Derrick Stolee663b2b12020-09-17 18:11:46 +0000867 close_object_store(the_repository->objects);
868 if (run_write_commit_graph(opts)) {
869 error(_("failed to write commit-graph"));
870 return 1;
871 }
872
873 return 0;
874}
875
Derrick Stoleea039a1f2021-04-06 18:47:46 +0000876static int fetch_remote(struct remote *remote, void *cbdata)
Derrick Stolee28cb5e62020-09-25 12:33:31 +0000877{
Derrick Stoleea039a1f2021-04-06 18:47:46 +0000878 struct maintenance_run_opts *opts = cbdata;
Derrick Stolee28cb5e62020-09-25 12:33:31 +0000879 struct child_process child = CHILD_PROCESS_INIT;
880
Derrick Stolee32f67882021-04-16 12:49:59 +0000881 if (remote->skip_default_update)
882 return 0;
883
Derrick Stolee28cb5e62020-09-25 12:33:31 +0000884 child.git_cmd = 1;
Derrick Stoleecfd781e2021-04-16 12:49:58 +0000885 strvec_pushl(&child.args, "fetch", remote->name,
886 "--prefetch", "--prune", "--no-tags",
Derrick Stolee28cb5e62020-09-25 12:33:31 +0000887 "--no-write-fetch-head", "--recurse-submodules=no",
Derrick Stoleecfd781e2021-04-16 12:49:58 +0000888 NULL);
Derrick Stolee28cb5e62020-09-25 12:33:31 +0000889
890 if (opts->quiet)
891 strvec_push(&child.args, "--quiet");
892
Derrick Stolee28cb5e62020-09-25 12:33:31 +0000893 return !!run_command(&child);
894}
895
Derrick Stolee28cb5e62020-09-25 12:33:31 +0000896static int maintenance_task_prefetch(struct maintenance_run_opts *opts)
897{
Derrick Stolee96eaffe2021-01-19 12:52:03 +0000898 git_config_set_multivar_gently("log.excludedecoration",
899 "refs/prefetch/",
900 "refs/prefetch/",
901 CONFIG_FLAGS_FIXED_VALUE |
902 CONFIG_FLAGS_MULTI_REPLACE);
903
Derrick Stoleea039a1f2021-04-06 18:47:46 +0000904 if (for_each_remote(fetch_remote, opts)) {
905 error(_("failed to prefetch remotes"));
906 return 1;
Derrick Stolee28cb5e62020-09-25 12:33:31 +0000907 }
908
Derrick Stoleea039a1f2021-04-06 18:47:46 +0000909 return 0;
Derrick Stolee28cb5e62020-09-25 12:33:31 +0000910}
911
Derrick Stolee2057d752020-09-17 18:11:42 +0000912static int maintenance_task_gc(struct maintenance_run_opts *opts)
913{
914 struct child_process child = CHILD_PROCESS_INIT;
915
916 child.git_cmd = 1;
917 strvec_push(&child.args, "gc");
918
919 if (opts->auto_flag)
920 strvec_push(&child.args, "--auto");
Derrick Stolee3ddaad02020-09-17 18:11:43 +0000921 if (opts->quiet)
922 strvec_push(&child.args, "--quiet");
923 else
924 strvec_push(&child.args, "--no-quiet");
Derrick Stolee2057d752020-09-17 18:11:42 +0000925
926 close_object_store(the_repository->objects);
927 return run_command(&child);
928}
929
Derrick Stolee252cfb72020-09-25 12:33:32 +0000930static int prune_packed(struct maintenance_run_opts *opts)
931{
932 struct child_process child = CHILD_PROCESS_INIT;
933
934 child.git_cmd = 1;
935 strvec_push(&child.args, "prune-packed");
936
937 if (opts->quiet)
938 strvec_push(&child.args, "--quiet");
939
940 return !!run_command(&child);
941}
942
943struct write_loose_object_data {
944 FILE *in;
945 int count;
946 int batch_size;
947};
948
Derrick Stolee3e220e62020-09-25 12:33:33 +0000949static int loose_object_auto_limit = 100;
950
951static int loose_object_count(const struct object_id *oid,
952 const char *path,
953 void *data)
954{
955 int *count = (int*)data;
956 if (++(*count) >= loose_object_auto_limit)
957 return 1;
958 return 0;
959}
960
961static int loose_object_auto_condition(void)
962{
963 int count = 0;
964
965 git_config_get_int("maintenance.loose-objects.auto",
966 &loose_object_auto_limit);
967
968 if (!loose_object_auto_limit)
969 return 0;
970 if (loose_object_auto_limit < 0)
971 return 1;
972
973 return for_each_loose_file_in_objdir(the_repository->objects->odb->path,
974 loose_object_count,
975 NULL, NULL, &count);
976}
977
Derrick Stolee252cfb72020-09-25 12:33:32 +0000978static int bail_on_loose(const struct object_id *oid,
979 const char *path,
980 void *data)
981{
982 return 1;
983}
984
985static int write_loose_object_to_stdin(const struct object_id *oid,
986 const char *path,
987 void *data)
988{
989 struct write_loose_object_data *d = (struct write_loose_object_data *)data;
990
991 fprintf(d->in, "%s\n", oid_to_hex(oid));
992
993 return ++(d->count) > d->batch_size;
994}
995
996static int pack_loose(struct maintenance_run_opts *opts)
997{
998 struct repository *r = the_repository;
999 int result = 0;
1000 struct write_loose_object_data data;
1001 struct child_process pack_proc = CHILD_PROCESS_INIT;
1002
1003 /*
1004 * Do not start pack-objects process
1005 * if there are no loose objects.
1006 */
1007 if (!for_each_loose_file_in_objdir(r->objects->odb->path,
1008 bail_on_loose,
1009 NULL, NULL, NULL))
1010 return 0;
1011
1012 pack_proc.git_cmd = 1;
1013
1014 strvec_push(&pack_proc.args, "pack-objects");
1015 if (opts->quiet)
1016 strvec_push(&pack_proc.args, "--quiet");
1017 strvec_pushf(&pack_proc.args, "%s/pack/loose", r->objects->odb->path);
1018
1019 pack_proc.in = -1;
1020
1021 if (start_command(&pack_proc)) {
1022 error(_("failed to start 'git pack-objects' process"));
1023 return 1;
1024 }
1025
1026 data.in = xfdopen(pack_proc.in, "w");
1027 data.count = 0;
1028 data.batch_size = 50000;
1029
1030 for_each_loose_file_in_objdir(r->objects->odb->path,
1031 write_loose_object_to_stdin,
1032 NULL,
1033 NULL,
1034 &data);
1035
1036 fclose(data.in);
1037
1038 if (finish_command(&pack_proc)) {
1039 error(_("failed to finish 'git pack-objects' process"));
1040 result = 1;
1041 }
1042
1043 return result;
1044}
1045
1046static int maintenance_task_loose_objects(struct maintenance_run_opts *opts)
1047{
1048 return prune_packed(opts) || pack_loose(opts);
1049}
1050
Derrick Stoleee841a792020-09-25 12:33:38 +00001051static int incremental_repack_auto_condition(void)
1052{
1053 struct packed_git *p;
1054 int enabled;
1055 int incremental_repack_auto_limit = 10;
1056 int count = 0;
1057
1058 if (git_config_get_bool("core.multiPackIndex", &enabled) ||
1059 !enabled)
1060 return 0;
1061
1062 git_config_get_int("maintenance.incremental-repack.auto",
1063 &incremental_repack_auto_limit);
1064
1065 if (!incremental_repack_auto_limit)
1066 return 0;
1067 if (incremental_repack_auto_limit < 0)
1068 return 1;
1069
1070 for (p = get_packed_git(the_repository);
1071 count < incremental_repack_auto_limit && p;
1072 p = p->next) {
1073 if (!p->multi_pack_index)
1074 count++;
1075 }
1076
1077 return count >= incremental_repack_auto_limit;
1078}
1079
Derrick Stolee52fe41f2020-09-25 12:33:36 +00001080static int multi_pack_index_write(struct maintenance_run_opts *opts)
1081{
1082 struct child_process child = CHILD_PROCESS_INIT;
1083
1084 child.git_cmd = 1;
1085 strvec_pushl(&child.args, "multi-pack-index", "write", NULL);
1086
1087 if (opts->quiet)
1088 strvec_push(&child.args, "--no-progress");
1089
1090 if (run_command(&child))
1091 return error(_("failed to write multi-pack-index"));
1092
1093 return 0;
1094}
1095
1096static int multi_pack_index_expire(struct maintenance_run_opts *opts)
1097{
1098 struct child_process child = CHILD_PROCESS_INIT;
1099
1100 child.git_cmd = 1;
1101 strvec_pushl(&child.args, "multi-pack-index", "expire", NULL);
1102
1103 if (opts->quiet)
1104 strvec_push(&child.args, "--no-progress");
1105
1106 close_object_store(the_repository->objects);
1107
1108 if (run_command(&child))
1109 return error(_("'git multi-pack-index expire' failed"));
1110
1111 return 0;
1112}
1113
Derrick Stoleea13e3d02020-09-25 12:33:37 +00001114#define TWO_GIGABYTES (INT32_MAX)
1115
1116static off_t get_auto_pack_size(void)
1117{
1118 /*
1119 * The "auto" value is special: we optimize for
1120 * one large pack-file (i.e. from a clone) and
1121 * expect the rest to be small and they can be
1122 * repacked quickly.
1123 *
1124 * The strategy we select here is to select a
1125 * size that is one more than the second largest
1126 * pack-file. This ensures that we will repack
1127 * at least two packs if there are three or more
1128 * packs.
1129 */
1130 off_t max_size = 0;
1131 off_t second_largest_size = 0;
1132 off_t result_size;
1133 struct packed_git *p;
1134 struct repository *r = the_repository;
1135
1136 reprepare_packed_git(r);
1137 for (p = get_all_packs(r); p; p = p->next) {
1138 if (p->pack_size > max_size) {
1139 second_largest_size = max_size;
1140 max_size = p->pack_size;
1141 } else if (p->pack_size > second_largest_size)
1142 second_largest_size = p->pack_size;
1143 }
1144
1145 result_size = second_largest_size + 1;
1146
1147 /* But limit ourselves to a batch size of 2g */
1148 if (result_size > TWO_GIGABYTES)
1149 result_size = TWO_GIGABYTES;
1150
1151 return result_size;
1152}
1153
Derrick Stolee52fe41f2020-09-25 12:33:36 +00001154static int multi_pack_index_repack(struct maintenance_run_opts *opts)
1155{
1156 struct child_process child = CHILD_PROCESS_INIT;
1157
1158 child.git_cmd = 1;
1159 strvec_pushl(&child.args, "multi-pack-index", "repack", NULL);
1160
1161 if (opts->quiet)
1162 strvec_push(&child.args, "--no-progress");
1163
Derrick Stoleea13e3d02020-09-25 12:33:37 +00001164 strvec_pushf(&child.args, "--batch-size=%"PRIuMAX,
1165 (uintmax_t)get_auto_pack_size());
Derrick Stolee52fe41f2020-09-25 12:33:36 +00001166
1167 close_object_store(the_repository->objects);
1168
1169 if (run_command(&child))
1170 return error(_("'git multi-pack-index repack' failed"));
1171
1172 return 0;
1173}
1174
1175static int maintenance_task_incremental_repack(struct maintenance_run_opts *opts)
1176{
1177 prepare_repo_settings(the_repository);
1178 if (!the_repository->settings.core_multi_pack_index) {
1179 warning(_("skipping incremental-repack task because core.multiPackIndex is disabled"));
1180 return 0;
1181 }
1182
1183 if (multi_pack_index_write(opts))
1184 return 1;
1185 if (multi_pack_index_expire(opts))
1186 return 1;
1187 if (multi_pack_index_repack(opts))
1188 return 1;
1189 return 0;
1190}
1191
Derrick Stolee3103e982020-09-17 18:11:45 +00001192typedef int maintenance_task_fn(struct maintenance_run_opts *opts);
1193
Derrick Stolee916d0622020-09-17 18:11:50 +00001194/*
1195 * An auto condition function returns 1 if the task should run
1196 * and 0 if the task should NOT run. See needs_to_gc() for an
1197 * example.
1198 */
1199typedef int maintenance_auto_fn(void);
1200
Derrick Stolee3103e982020-09-17 18:11:45 +00001201struct maintenance_task {
1202 const char *name;
1203 maintenance_task_fn *fn;
Derrick Stolee916d0622020-09-17 18:11:50 +00001204 maintenance_auto_fn *auto_condition;
Derrick Stolee3103e982020-09-17 18:11:45 +00001205 unsigned enabled:1;
Derrick Stolee090511b2020-09-17 18:11:47 +00001206
Derrick Stoleeb08ff1f2020-09-11 17:49:15 +00001207 enum schedule_priority schedule;
1208
Derrick Stolee090511b2020-09-17 18:11:47 +00001209 /* -1 if not selected. */
1210 int selected_order;
Derrick Stolee3103e982020-09-17 18:11:45 +00001211};
1212
1213enum maintenance_task_label {
Derrick Stolee28cb5e62020-09-25 12:33:31 +00001214 TASK_PREFETCH,
Derrick Stolee252cfb72020-09-25 12:33:32 +00001215 TASK_LOOSE_OBJECTS,
Derrick Stolee52fe41f2020-09-25 12:33:36 +00001216 TASK_INCREMENTAL_REPACK,
Derrick Stolee3103e982020-09-17 18:11:45 +00001217 TASK_GC,
Derrick Stolee663b2b12020-09-17 18:11:46 +00001218 TASK_COMMIT_GRAPH,
Derrick Stolee41abfe12021-02-09 13:42:28 +00001219 TASK_PACK_REFS,
Derrick Stolee3103e982020-09-17 18:11:45 +00001220
1221 /* Leave as final value */
1222 TASK__COUNT
1223};
1224
1225static struct maintenance_task tasks[] = {
Derrick Stolee28cb5e62020-09-25 12:33:31 +00001226 [TASK_PREFETCH] = {
1227 "prefetch",
1228 maintenance_task_prefetch,
1229 },
Derrick Stolee252cfb72020-09-25 12:33:32 +00001230 [TASK_LOOSE_OBJECTS] = {
1231 "loose-objects",
1232 maintenance_task_loose_objects,
Derrick Stolee3e220e62020-09-25 12:33:33 +00001233 loose_object_auto_condition,
Derrick Stolee252cfb72020-09-25 12:33:32 +00001234 },
Derrick Stolee52fe41f2020-09-25 12:33:36 +00001235 [TASK_INCREMENTAL_REPACK] = {
1236 "incremental-repack",
1237 maintenance_task_incremental_repack,
Derrick Stoleee841a792020-09-25 12:33:38 +00001238 incremental_repack_auto_condition,
Derrick Stolee52fe41f2020-09-25 12:33:36 +00001239 },
Derrick Stolee3103e982020-09-17 18:11:45 +00001240 [TASK_GC] = {
1241 "gc",
1242 maintenance_task_gc,
Derrick Stolee916d0622020-09-17 18:11:50 +00001243 need_to_gc,
Derrick Stolee3103e982020-09-17 18:11:45 +00001244 1,
1245 },
Derrick Stolee663b2b12020-09-17 18:11:46 +00001246 [TASK_COMMIT_GRAPH] = {
1247 "commit-graph",
1248 maintenance_task_commit_graph,
Derrick Stolee4ddc79b2020-09-17 18:11:51 +00001249 should_write_commit_graph,
Derrick Stolee663b2b12020-09-17 18:11:46 +00001250 },
Derrick Stolee41abfe12021-02-09 13:42:28 +00001251 [TASK_PACK_REFS] = {
1252 "pack-refs",
1253 maintenance_task_pack_refs,
1254 NULL,
1255 },
Derrick Stolee3103e982020-09-17 18:11:45 +00001256};
1257
Derrick Stolee090511b2020-09-17 18:11:47 +00001258static int compare_tasks_by_selection(const void *a_, const void *b_)
1259{
René Scharfea1c74792020-11-17 22:59:49 +01001260 const struct maintenance_task *a = a_;
1261 const struct maintenance_task *b = b_;
Derrick Stolee090511b2020-09-17 18:11:47 +00001262
1263 return b->selected_order - a->selected_order;
1264}
1265
Derrick Stolee3103e982020-09-17 18:11:45 +00001266static int maintenance_run_tasks(struct maintenance_run_opts *opts)
1267{
Derrick Stolee090511b2020-09-17 18:11:47 +00001268 int i, found_selected = 0;
Derrick Stolee3103e982020-09-17 18:11:45 +00001269 int result = 0;
Derrick Stoleed7514f62020-09-17 18:11:48 +00001270 struct lock_file lk;
1271 struct repository *r = the_repository;
1272 char *lock_path = xstrfmt("%s/maintenance", r->objects->odb->path);
1273
1274 if (hold_lock_file_for_update(&lk, lock_path, LOCK_NO_DEREF) < 0) {
1275 /*
1276 * Another maintenance command is running.
1277 *
1278 * If --auto was provided, then it is likely due to a
1279 * recursive process stack. Do not report an error in
1280 * that case.
1281 */
1282 if (!opts->auto_flag && !opts->quiet)
1283 warning(_("lock file '%s' exists, skipping maintenance"),
1284 lock_path);
1285 free(lock_path);
1286 return 0;
1287 }
1288 free(lock_path);
Derrick Stolee3103e982020-09-17 18:11:45 +00001289
Derrick Stolee090511b2020-09-17 18:11:47 +00001290 for (i = 0; !found_selected && i < TASK__COUNT; i++)
1291 found_selected = tasks[i].selected_order >= 0;
1292
1293 if (found_selected)
1294 QSORT(tasks, TASK__COUNT, compare_tasks_by_selection);
1295
Derrick Stolee3103e982020-09-17 18:11:45 +00001296 for (i = 0; i < TASK__COUNT; i++) {
Derrick Stolee090511b2020-09-17 18:11:47 +00001297 if (found_selected && tasks[i].selected_order < 0)
1298 continue;
1299
1300 if (!found_selected && !tasks[i].enabled)
Derrick Stolee3103e982020-09-17 18:11:45 +00001301 continue;
1302
Derrick Stolee916d0622020-09-17 18:11:50 +00001303 if (opts->auto_flag &&
1304 (!tasks[i].auto_condition ||
1305 !tasks[i].auto_condition()))
1306 continue;
1307
Derrick Stoleeb08ff1f2020-09-11 17:49:15 +00001308 if (opts->schedule && tasks[i].schedule < opts->schedule)
1309 continue;
1310
Derrick Stolee25914c42020-09-17 18:11:52 +00001311 trace2_region_enter("maintenance", tasks[i].name, r);
Derrick Stolee3103e982020-09-17 18:11:45 +00001312 if (tasks[i].fn(opts)) {
1313 error(_("task '%s' failed"), tasks[i].name);
1314 result = 1;
1315 }
Derrick Stolee25914c42020-09-17 18:11:52 +00001316 trace2_region_leave("maintenance", tasks[i].name, r);
Derrick Stolee3103e982020-09-17 18:11:45 +00001317 }
1318
Derrick Stoleed7514f62020-09-17 18:11:48 +00001319 rollback_lock_file(&lk);
Derrick Stolee3103e982020-09-17 18:11:45 +00001320 return result;
1321}
1322
Derrick Stoleea4cb1a22020-10-15 17:22:02 +00001323static void initialize_maintenance_strategy(void)
1324{
1325 char *config_str;
1326
1327 if (git_config_get_string("maintenance.strategy", &config_str))
1328 return;
1329
1330 if (!strcasecmp(config_str, "incremental")) {
1331 tasks[TASK_GC].schedule = SCHEDULE_NONE;
1332 tasks[TASK_COMMIT_GRAPH].enabled = 1;
1333 tasks[TASK_COMMIT_GRAPH].schedule = SCHEDULE_HOURLY;
1334 tasks[TASK_PREFETCH].enabled = 1;
1335 tasks[TASK_PREFETCH].schedule = SCHEDULE_HOURLY;
1336 tasks[TASK_INCREMENTAL_REPACK].enabled = 1;
1337 tasks[TASK_INCREMENTAL_REPACK].schedule = SCHEDULE_DAILY;
1338 tasks[TASK_LOOSE_OBJECTS].enabled = 1;
1339 tasks[TASK_LOOSE_OBJECTS].schedule = SCHEDULE_DAILY;
Derrick Stoleeacc1c4d2021-02-09 13:42:29 +00001340 tasks[TASK_PACK_REFS].enabled = 1;
1341 tasks[TASK_PACK_REFS].schedule = SCHEDULE_WEEKLY;
Derrick Stoleea4cb1a22020-10-15 17:22:02 +00001342 }
1343}
1344
1345static void initialize_task_config(int schedule)
Derrick Stolee65d655b2020-09-17 18:11:49 +00001346{
1347 int i;
1348 struct strbuf config_name = STRBUF_INIT;
Derrick Stolee916d0622020-09-17 18:11:50 +00001349 gc_config();
1350
Derrick Stoleea4cb1a22020-10-15 17:22:02 +00001351 if (schedule)
1352 initialize_maintenance_strategy();
1353
Derrick Stolee65d655b2020-09-17 18:11:49 +00001354 for (i = 0; i < TASK__COUNT; i++) {
1355 int config_value;
Derrick Stoleeb08ff1f2020-09-11 17:49:15 +00001356 char *config_str;
Derrick Stolee65d655b2020-09-17 18:11:49 +00001357
Derrick Stoleeb08ff1f2020-09-11 17:49:15 +00001358 strbuf_reset(&config_name);
Derrick Stolee65d655b2020-09-17 18:11:49 +00001359 strbuf_addf(&config_name, "maintenance.%s.enabled",
1360 tasks[i].name);
1361
1362 if (!git_config_get_bool(config_name.buf, &config_value))
1363 tasks[i].enabled = config_value;
Derrick Stoleeb08ff1f2020-09-11 17:49:15 +00001364
1365 strbuf_reset(&config_name);
1366 strbuf_addf(&config_name, "maintenance.%s.schedule",
1367 tasks[i].name);
1368
1369 if (!git_config_get_string(config_name.buf, &config_str)) {
1370 tasks[i].schedule = parse_schedule(config_str);
1371 free(config_str);
1372 }
Derrick Stolee65d655b2020-09-17 18:11:49 +00001373 }
1374
1375 strbuf_release(&config_name);
1376}
1377
Derrick Stolee090511b2020-09-17 18:11:47 +00001378static int task_option_parse(const struct option *opt,
1379 const char *arg, int unset)
1380{
1381 int i, num_selected = 0;
1382 struct maintenance_task *task = NULL;
1383
1384 BUG_ON_OPT_NEG(unset);
1385
1386 for (i = 0; i < TASK__COUNT; i++) {
1387 if (tasks[i].selected_order >= 0)
1388 num_selected++;
1389 if (!strcasecmp(tasks[i].name, arg)) {
1390 task = &tasks[i];
1391 }
1392 }
1393
1394 if (!task) {
1395 error(_("'%s' is not a valid task"), arg);
1396 return 1;
1397 }
1398
1399 if (task->selected_order >= 0) {
1400 error(_("task '%s' cannot be selected multiple times"), arg);
1401 return 1;
1402 }
1403
1404 task->selected_order = num_selected + 1;
1405
1406 return 0;
1407}
1408
Derrick Stolee2057d752020-09-17 18:11:42 +00001409static int maintenance_run(int argc, const char **argv, const char *prefix)
1410{
Derrick Stolee090511b2020-09-17 18:11:47 +00001411 int i;
Derrick Stolee2057d752020-09-17 18:11:42 +00001412 struct maintenance_run_opts opts;
1413 struct option builtin_maintenance_run_options[] = {
1414 OPT_BOOL(0, "auto", &opts.auto_flag,
1415 N_("run tasks based on the state of the repository")),
Derrick Stoleeb08ff1f2020-09-11 17:49:15 +00001416 OPT_CALLBACK(0, "schedule", &opts.schedule, N_("frequency"),
1417 N_("run tasks based on frequency"),
1418 maintenance_opt_schedule),
Derrick Stolee3ddaad02020-09-17 18:11:43 +00001419 OPT_BOOL(0, "quiet", &opts.quiet,
1420 N_("do not report progress or other information over stderr")),
Derrick Stolee090511b2020-09-17 18:11:47 +00001421 OPT_CALLBACK_F(0, "task", NULL, N_("task"),
1422 N_("run a specific task"),
1423 PARSE_OPT_NONEG, task_option_parse),
Derrick Stolee2057d752020-09-17 18:11:42 +00001424 OPT_END()
1425 };
1426 memset(&opts, 0, sizeof(opts));
1427
Derrick Stolee3ddaad02020-09-17 18:11:43 +00001428 opts.quiet = !isatty(2);
1429
Derrick Stolee090511b2020-09-17 18:11:47 +00001430 for (i = 0; i < TASK__COUNT; i++)
1431 tasks[i].selected_order = -1;
1432
Derrick Stolee2057d752020-09-17 18:11:42 +00001433 argc = parse_options(argc, argv, prefix,
1434 builtin_maintenance_run_options,
1435 builtin_maintenance_run_usage,
1436 PARSE_OPT_STOP_AT_NON_OPTION);
1437
Derrick Stoleeb08ff1f2020-09-11 17:49:15 +00001438 if (opts.auto_flag && opts.schedule)
1439 die(_("use at most one of --auto and --schedule=<frequency>"));
1440
Derrick Stoleea4cb1a22020-10-15 17:22:02 +00001441 initialize_task_config(opts.schedule);
1442
Derrick Stolee2057d752020-09-17 18:11:42 +00001443 if (argc != 0)
1444 usage_with_options(builtin_maintenance_run_usage,
1445 builtin_maintenance_run_options);
Derrick Stolee3103e982020-09-17 18:11:45 +00001446 return maintenance_run_tasks(&opts);
Derrick Stolee2057d752020-09-17 18:11:42 +00001447}
1448
Eric Sunshine26c79742021-02-23 02:31:07 -05001449static char *get_maintpath(void)
1450{
1451 struct strbuf sb = STRBUF_INIT;
1452 const char *p = the_repository->worktree ?
1453 the_repository->worktree : the_repository->gitdir;
1454
1455 strbuf_realpath(&sb, p, 1);
1456 return strbuf_detach(&sb, NULL);
1457}
1458
Derrick Stolee0c18b702020-09-11 17:49:17 +00001459static int maintenance_register(void)
1460{
Eric Sunshine26c79742021-02-23 02:31:07 -05001461 int rc;
Derrick Stolee61f7a382020-10-15 17:22:03 +00001462 char *config_value;
Derrick Stolee0c18b702020-09-11 17:49:17 +00001463 struct child_process config_set = CHILD_PROCESS_INIT;
1464 struct child_process config_get = CHILD_PROCESS_INIT;
Eric Sunshine26c79742021-02-23 02:31:07 -05001465 char *maintpath = get_maintpath();
Derrick Stolee0c18b702020-09-11 17:49:17 +00001466
Derrick Stolee61f7a382020-10-15 17:22:03 +00001467 /* Disable foreground maintenance */
1468 git_config_set("maintenance.auto", "false");
1469
1470 /* Set maintenance strategy, if unset */
1471 if (!git_config_get_string("maintenance.strategy", &config_value))
1472 free(config_value);
1473 else
1474 git_config_set("maintenance.strategy", "incremental");
1475
Derrick Stolee0c18b702020-09-11 17:49:17 +00001476 config_get.git_cmd = 1;
Derrick Stolee483a6d92020-11-25 22:12:56 +00001477 strvec_pushl(&config_get.args, "config", "--global", "--get",
Eric Sunshine26c79742021-02-23 02:31:07 -05001478 "--fixed-value", "maintenance.repo", maintpath, NULL);
Derrick Stolee0c18b702020-09-11 17:49:17 +00001479 config_get.out = -1;
1480
Eric Sunshine26c79742021-02-23 02:31:07 -05001481 if (start_command(&config_get)) {
1482 rc = error(_("failed to run 'git config'"));
1483 goto done;
1484 }
Derrick Stolee0c18b702020-09-11 17:49:17 +00001485
1486 /* We already have this value in our config! */
Eric Sunshine26c79742021-02-23 02:31:07 -05001487 if (!finish_command(&config_get)) {
1488 rc = 0;
1489 goto done;
1490 }
Derrick Stolee0c18b702020-09-11 17:49:17 +00001491
1492 config_set.git_cmd = 1;
1493 strvec_pushl(&config_set.args, "config", "--add", "--global", "maintenance.repo",
Eric Sunshine26c79742021-02-23 02:31:07 -05001494 maintpath, NULL);
Derrick Stolee0c18b702020-09-11 17:49:17 +00001495
Eric Sunshine26c79742021-02-23 02:31:07 -05001496 rc = run_command(&config_set);
1497
1498done:
1499 free(maintpath);
1500 return rc;
Derrick Stolee0c18b702020-09-11 17:49:17 +00001501}
1502
1503static int maintenance_unregister(void)
1504{
Eric Sunshine26c79742021-02-23 02:31:07 -05001505 int rc;
Derrick Stolee0c18b702020-09-11 17:49:17 +00001506 struct child_process config_unset = CHILD_PROCESS_INIT;
Eric Sunshine26c79742021-02-23 02:31:07 -05001507 char *maintpath = get_maintpath();
Derrick Stolee0c18b702020-09-11 17:49:17 +00001508
Derrick Stolee0c18b702020-09-11 17:49:17 +00001509 config_unset.git_cmd = 1;
1510 strvec_pushl(&config_unset.args, "config", "--global", "--unset",
Eric Sunshine26c79742021-02-23 02:31:07 -05001511 "--fixed-value", "maintenance.repo", maintpath, NULL);
Derrick Stolee0c18b702020-09-11 17:49:17 +00001512
Eric Sunshine26c79742021-02-23 02:31:07 -05001513 rc = run_command(&config_unset);
1514 free(maintpath);
1515 return rc;
Derrick Stolee0c18b702020-09-11 17:49:17 +00001516}
1517
Derrick Stolee2afe7e32021-01-05 13:08:27 +00001518static const char *get_frequency(enum schedule_priority schedule)
1519{
1520 switch (schedule) {
1521 case SCHEDULE_HOURLY:
1522 return "hourly";
1523 case SCHEDULE_DAILY:
1524 return "daily";
1525 case SCHEDULE_WEEKLY:
1526 return "weekly";
1527 default:
1528 BUG("invalid schedule %d", schedule);
1529 }
1530}
1531
1532static char *launchctl_service_name(const char *frequency)
1533{
1534 struct strbuf label = STRBUF_INIT;
1535 strbuf_addf(&label, "org.git-scm.git.%s", frequency);
1536 return strbuf_detach(&label, NULL);
1537}
1538
1539static char *launchctl_service_filename(const char *name)
1540{
1541 char *expanded;
1542 struct strbuf filename = STRBUF_INIT;
1543 strbuf_addf(&filename, "~/Library/LaunchAgents/%s.plist", name);
1544
Johannes Schindelina03b0972021-07-24 22:06:52 +00001545 expanded = interpolate_path(filename.buf, 1);
Derrick Stolee2afe7e32021-01-05 13:08:27 +00001546 if (!expanded)
1547 die(_("failed to expand path '%s'"), filename.buf);
1548
1549 strbuf_release(&filename);
1550 return expanded;
1551}
1552
1553static char *launchctl_get_uid(void)
1554{
1555 return xstrfmt("gui/%d", getuid());
1556}
1557
1558static int launchctl_boot_plist(int enable, const char *filename, const char *cmd)
1559{
1560 int result;
1561 struct child_process child = CHILD_PROCESS_INIT;
1562 char *uid = launchctl_get_uid();
1563
1564 strvec_split(&child.args, cmd);
1565 if (enable)
1566 strvec_push(&child.args, "bootstrap");
1567 else
1568 strvec_push(&child.args, "bootout");
1569 strvec_push(&child.args, uid);
1570 strvec_push(&child.args, filename);
1571
1572 child.no_stderr = 1;
1573 child.no_stdout = 1;
1574
1575 if (start_command(&child))
1576 die(_("failed to start launchctl"));
1577
1578 result = finish_command(&child);
1579
1580 free(uid);
1581 return result;
1582}
1583
1584static int launchctl_remove_plist(enum schedule_priority schedule, const char *cmd)
1585{
1586 const char *frequency = get_frequency(schedule);
1587 char *name = launchctl_service_name(frequency);
1588 char *filename = launchctl_service_filename(name);
1589 int result = launchctl_boot_plist(0, filename, cmd);
1590 unlink(filename);
1591 free(filename);
1592 free(name);
1593 return result;
1594}
1595
1596static int launchctl_remove_plists(const char *cmd)
1597{
1598 return launchctl_remove_plist(SCHEDULE_HOURLY, cmd) ||
1599 launchctl_remove_plist(SCHEDULE_DAILY, cmd) ||
1600 launchctl_remove_plist(SCHEDULE_WEEKLY, cmd);
1601}
1602
Derrick Stoleea16eb6b2021-08-24 15:44:00 +00001603static int launchctl_list_contains_plist(const char *name, const char *cmd)
1604{
Derrick Stoleea16eb6b2021-08-24 15:44:00 +00001605 struct child_process child = CHILD_PROCESS_INIT;
Derrick Stoleea16eb6b2021-08-24 15:44:00 +00001606
1607 strvec_split(&child.args, cmd);
1608 strvec_pushl(&child.args, "list", name, NULL);
1609
1610 child.no_stderr = 1;
1611 child.no_stdout = 1;
1612
1613 if (start_command(&child))
1614 die(_("failed to start launchctl"));
1615
Derrick Stoleea16eb6b2021-08-24 15:44:00 +00001616 /* Returns failure if 'name' doesn't exist. */
Ævar Arnfjörð Bjarmason3218cb72021-09-12 02:24:40 +02001617 return !finish_command(&child);
Derrick Stoleea16eb6b2021-08-24 15:44:00 +00001618}
1619
Derrick Stolee2afe7e32021-01-05 13:08:27 +00001620static int launchctl_schedule_plist(const char *exec_path, enum schedule_priority schedule, const char *cmd)
1621{
Johannes Schindelinbb011222021-08-24 15:43:59 +00001622 int i, fd;
Derrick Stolee2afe7e32021-01-05 13:08:27 +00001623 const char *preamble, *repeat;
1624 const char *frequency = get_frequency(schedule);
1625 char *name = launchctl_service_name(frequency);
1626 char *filename = launchctl_service_filename(name);
Johannes Schindelinbb011222021-08-24 15:43:59 +00001627 struct lock_file lk = LOCK_INIT;
1628 static unsigned long lock_file_timeout_ms = ULONG_MAX;
Derrick Stoleea16eb6b2021-08-24 15:44:00 +00001629 struct strbuf plist = STRBUF_INIT, plist2 = STRBUF_INIT;
1630 struct stat st;
Derrick Stolee2afe7e32021-01-05 13:08:27 +00001631
Derrick Stolee3797a0a2021-01-05 13:08:28 +00001632 preamble = "<?xml version=\"1.0\"?>\n"
Derrick Stolee2afe7e32021-01-05 13:08:27 +00001633 "<!DOCTYPE plist PUBLIC \"-//Apple//DTD PLIST 1.0//EN\" \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">\n"
1634 "<plist version=\"1.0\">"
1635 "<dict>\n"
1636 "<key>Label</key><string>%s</string>\n"
1637 "<key>ProgramArguments</key>\n"
1638 "<array>\n"
1639 "<string>%s/git</string>\n"
1640 "<string>--exec-path=%s</string>\n"
1641 "<string>for-each-repo</string>\n"
1642 "<string>--config=maintenance.repo</string>\n"
1643 "<string>maintenance</string>\n"
1644 "<string>run</string>\n"
1645 "<string>--schedule=%s</string>\n"
1646 "</array>\n"
1647 "<key>StartCalendarInterval</key>\n"
1648 "<array>\n";
Johannes Schindelinbb011222021-08-24 15:43:59 +00001649 strbuf_addf(&plist, preamble, name, exec_path, exec_path, frequency);
Derrick Stolee2afe7e32021-01-05 13:08:27 +00001650
1651 switch (schedule) {
1652 case SCHEDULE_HOURLY:
1653 repeat = "<dict>\n"
1654 "<key>Hour</key><integer>%d</integer>\n"
1655 "<key>Minute</key><integer>0</integer>\n"
1656 "</dict>\n";
1657 for (i = 1; i <= 23; i++)
Johannes Schindelinbb011222021-08-24 15:43:59 +00001658 strbuf_addf(&plist, repeat, i);
Derrick Stolee2afe7e32021-01-05 13:08:27 +00001659 break;
1660
1661 case SCHEDULE_DAILY:
1662 repeat = "<dict>\n"
1663 "<key>Day</key><integer>%d</integer>\n"
1664 "<key>Hour</key><integer>0</integer>\n"
1665 "<key>Minute</key><integer>0</integer>\n"
1666 "</dict>\n";
1667 for (i = 1; i <= 6; i++)
Johannes Schindelinbb011222021-08-24 15:43:59 +00001668 strbuf_addf(&plist, repeat, i);
Derrick Stolee2afe7e32021-01-05 13:08:27 +00001669 break;
1670
1671 case SCHEDULE_WEEKLY:
Johannes Schindelinbb011222021-08-24 15:43:59 +00001672 strbuf_addstr(&plist,
1673 "<dict>\n"
1674 "<key>Day</key><integer>0</integer>\n"
1675 "<key>Hour</key><integer>0</integer>\n"
1676 "<key>Minute</key><integer>0</integer>\n"
1677 "</dict>\n");
Derrick Stolee2afe7e32021-01-05 13:08:27 +00001678 break;
1679
1680 default:
1681 /* unreachable */
1682 break;
1683 }
Johannes Schindelinbb011222021-08-24 15:43:59 +00001684 strbuf_addstr(&plist, "</array>\n</dict>\n</plist>\n");
Derrick Stolee2afe7e32021-01-05 13:08:27 +00001685
Johannes Schindelinbb011222021-08-24 15:43:59 +00001686 if (safe_create_leading_directories(filename))
1687 die(_("failed to create directories for '%s'"), filename);
1688
1689 if ((long)lock_file_timeout_ms < 0 &&
1690 git_config_get_ulong("gc.launchctlplistlocktimeoutms",
1691 &lock_file_timeout_ms))
1692 lock_file_timeout_ms = 150;
1693
1694 fd = hold_lock_file_for_update_timeout(&lk, filename, LOCK_DIE_ON_ERROR,
1695 lock_file_timeout_ms);
1696
Derrick Stoleea16eb6b2021-08-24 15:44:00 +00001697 /*
1698 * Does this file already exist? With the intended contents? Is it
1699 * registered already? Then it does not need to be re-registered.
1700 */
1701 if (!stat(filename, &st) && st.st_size == plist.len &&
1702 strbuf_read_file(&plist2, filename, plist.len) == plist.len &&
1703 !strbuf_cmp(&plist, &plist2) &&
1704 launchctl_list_contains_plist(name, cmd))
1705 rollback_lock_file(&lk);
1706 else {
1707 if (write_in_full(fd, plist.buf, plist.len) < 0 ||
1708 commit_lock_file(&lk))
1709 die_errno(_("could not write '%s'"), filename);
Derrick Stolee2afe7e32021-01-05 13:08:27 +00001710
Derrick Stoleea16eb6b2021-08-24 15:44:00 +00001711 /* bootout might fail if not already running, so ignore */
1712 launchctl_boot_plist(0, filename, cmd);
1713 if (launchctl_boot_plist(1, filename, cmd))
1714 die(_("failed to bootstrap service %s"), filename);
1715 }
Derrick Stolee2afe7e32021-01-05 13:08:27 +00001716
1717 free(filename);
1718 free(name);
Johannes Schindelinbb011222021-08-24 15:43:59 +00001719 strbuf_release(&plist);
Derrick Stoleea16eb6b2021-08-24 15:44:00 +00001720 strbuf_release(&plist2);
Derrick Stolee2afe7e32021-01-05 13:08:27 +00001721 return 0;
1722}
1723
1724static int launchctl_add_plists(const char *cmd)
1725{
1726 const char *exec_path = git_exec_path();
1727
1728 return launchctl_schedule_plist(exec_path, SCHEDULE_HOURLY, cmd) ||
1729 launchctl_schedule_plist(exec_path, SCHEDULE_DAILY, cmd) ||
1730 launchctl_schedule_plist(exec_path, SCHEDULE_WEEKLY, cmd);
1731}
1732
1733static int launchctl_update_schedule(int run_maintenance, int fd, const char *cmd)
1734{
1735 if (run_maintenance)
1736 return launchctl_add_plists(cmd);
1737 else
1738 return launchctl_remove_plists(cmd);
1739}
1740
Derrick Stolee3797a0a2021-01-05 13:08:28 +00001741static char *schtasks_task_name(const char *frequency)
1742{
1743 struct strbuf label = STRBUF_INIT;
1744 strbuf_addf(&label, "Git Maintenance (%s)", frequency);
1745 return strbuf_detach(&label, NULL);
1746}
1747
1748static int schtasks_remove_task(enum schedule_priority schedule, const char *cmd)
1749{
1750 int result;
1751 struct strvec args = STRVEC_INIT;
1752 const char *frequency = get_frequency(schedule);
1753 char *name = schtasks_task_name(frequency);
1754
1755 strvec_split(&args, cmd);
1756 strvec_pushl(&args, "/delete", "/tn", name, "/f", NULL);
1757
1758 result = run_command_v_opt(args.v, 0);
1759
1760 strvec_clear(&args);
1761 free(name);
1762 return result;
1763}
1764
1765static int schtasks_remove_tasks(const char *cmd)
1766{
1767 return schtasks_remove_task(SCHEDULE_HOURLY, cmd) ||
1768 schtasks_remove_task(SCHEDULE_DAILY, cmd) ||
1769 schtasks_remove_task(SCHEDULE_WEEKLY, cmd);
1770}
1771
1772static int schtasks_schedule_task(const char *exec_path, enum schedule_priority schedule, const char *cmd)
1773{
1774 int result;
1775 struct child_process child = CHILD_PROCESS_INIT;
1776 const char *xml;
1777 struct tempfile *tfile;
1778 const char *frequency = get_frequency(schedule);
1779 char *name = schtasks_task_name(frequency);
1780 struct strbuf tfilename = STRBUF_INIT;
1781
1782 strbuf_addf(&tfilename, "%s/schedule_%s_XXXXXX",
1783 get_git_common_dir(), frequency);
1784 tfile = xmks_tempfile(tfilename.buf);
1785 strbuf_release(&tfilename);
1786
1787 if (!fdopen_tempfile(tfile, "w"))
1788 die(_("failed to create temp xml file"));
1789
1790 xml = "<?xml version=\"1.0\" ?>\n"
1791 "<Task version=\"1.4\" xmlns=\"http://schemas.microsoft.com/windows/2004/02/mit/task\">\n"
1792 "<Triggers>\n"
1793 "<CalendarTrigger>\n";
1794 fputs(xml, tfile->fp);
1795
1796 switch (schedule) {
1797 case SCHEDULE_HOURLY:
1798 fprintf(tfile->fp,
1799 "<StartBoundary>2020-01-01T01:00:00</StartBoundary>\n"
1800 "<Enabled>true</Enabled>\n"
1801 "<ScheduleByDay>\n"
1802 "<DaysInterval>1</DaysInterval>\n"
1803 "</ScheduleByDay>\n"
1804 "<Repetition>\n"
1805 "<Interval>PT1H</Interval>\n"
1806 "<Duration>PT23H</Duration>\n"
1807 "<StopAtDurationEnd>false</StopAtDurationEnd>\n"
1808 "</Repetition>\n");
1809 break;
1810
1811 case SCHEDULE_DAILY:
1812 fprintf(tfile->fp,
1813 "<StartBoundary>2020-01-01T00:00:00</StartBoundary>\n"
1814 "<Enabled>true</Enabled>\n"
1815 "<ScheduleByWeek>\n"
1816 "<DaysOfWeek>\n"
1817 "<Monday />\n"
1818 "<Tuesday />\n"
1819 "<Wednesday />\n"
1820 "<Thursday />\n"
1821 "<Friday />\n"
1822 "<Saturday />\n"
1823 "</DaysOfWeek>\n"
1824 "<WeeksInterval>1</WeeksInterval>\n"
1825 "</ScheduleByWeek>\n");
1826 break;
1827
1828 case SCHEDULE_WEEKLY:
1829 fprintf(tfile->fp,
1830 "<StartBoundary>2020-01-01T00:00:00</StartBoundary>\n"
1831 "<Enabled>true</Enabled>\n"
1832 "<ScheduleByWeek>\n"
1833 "<DaysOfWeek>\n"
1834 "<Sunday />\n"
1835 "</DaysOfWeek>\n"
1836 "<WeeksInterval>1</WeeksInterval>\n"
1837 "</ScheduleByWeek>\n");
1838 break;
1839
1840 default:
1841 break;
1842 }
1843
1844 xml = "</CalendarTrigger>\n"
1845 "</Triggers>\n"
1846 "<Principals>\n"
1847 "<Principal id=\"Author\">\n"
1848 "<LogonType>InteractiveToken</LogonType>\n"
1849 "<RunLevel>LeastPrivilege</RunLevel>\n"
1850 "</Principal>\n"
1851 "</Principals>\n"
1852 "<Settings>\n"
1853 "<MultipleInstancesPolicy>IgnoreNew</MultipleInstancesPolicy>\n"
1854 "<Enabled>true</Enabled>\n"
1855 "<Hidden>true</Hidden>\n"
1856 "<UseUnifiedSchedulingEngine>true</UseUnifiedSchedulingEngine>\n"
1857 "<WakeToRun>false</WakeToRun>\n"
1858 "<ExecutionTimeLimit>PT72H</ExecutionTimeLimit>\n"
1859 "<Priority>7</Priority>\n"
1860 "</Settings>\n"
1861 "<Actions Context=\"Author\">\n"
1862 "<Exec>\n"
1863 "<Command>\"%s\\git.exe\"</Command>\n"
1864 "<Arguments>--exec-path=\"%s\" for-each-repo --config=maintenance.repo maintenance run --schedule=%s</Arguments>\n"
1865 "</Exec>\n"
1866 "</Actions>\n"
1867 "</Task>\n";
1868 fprintf(tfile->fp, xml, exec_path, exec_path, frequency);
1869 strvec_split(&child.args, cmd);
1870 strvec_pushl(&child.args, "/create", "/tn", name, "/f", "/xml",
1871 get_tempfile_path(tfile), NULL);
1872 close_tempfile_gently(tfile);
1873
1874 child.no_stdout = 1;
1875 child.no_stderr = 1;
1876
1877 if (start_command(&child))
1878 die(_("failed to start schtasks"));
1879 result = finish_command(&child);
1880
1881 delete_tempfile(&tfile);
1882 free(name);
1883 return result;
1884}
1885
1886static int schtasks_schedule_tasks(const char *cmd)
1887{
1888 const char *exec_path = git_exec_path();
1889
1890 return schtasks_schedule_task(exec_path, SCHEDULE_HOURLY, cmd) ||
1891 schtasks_schedule_task(exec_path, SCHEDULE_DAILY, cmd) ||
1892 schtasks_schedule_task(exec_path, SCHEDULE_WEEKLY, cmd);
1893}
1894
1895static int schtasks_update_schedule(int run_maintenance, int fd, const char *cmd)
1896{
1897 if (run_maintenance)
1898 return schtasks_schedule_tasks(cmd);
1899 else
1900 return schtasks_remove_tasks(cmd);
1901}
1902
Derrick Stolee2fec6042020-09-11 17:49:18 +00001903#define BEGIN_LINE "# BEGIN GIT MAINTENANCE SCHEDULE"
1904#define END_LINE "# END GIT MAINTENANCE SCHEDULE"
1905
Derrick Stolee31345d52020-11-24 04:16:42 +00001906static int crontab_update_schedule(int run_maintenance, int fd, const char *cmd)
Derrick Stolee2fec6042020-09-11 17:49:18 +00001907{
1908 int result = 0;
1909 int in_old_region = 0;
1910 struct child_process crontab_list = CHILD_PROCESS_INIT;
1911 struct child_process crontab_edit = CHILD_PROCESS_INIT;
1912 FILE *cron_list, *cron_in;
Derrick Stolee2fec6042020-09-11 17:49:18 +00001913 struct strbuf line = STRBUF_INIT;
Derrick Stolee2fec6042020-09-11 17:49:18 +00001914
Derrick Stolee31345d52020-11-24 04:16:42 +00001915 strvec_split(&crontab_list.args, cmd);
Derrick Stolee2fec6042020-09-11 17:49:18 +00001916 strvec_push(&crontab_list.args, "-l");
1917 crontab_list.in = -1;
Derrick Stolee31345d52020-11-24 04:16:42 +00001918 crontab_list.out = dup(fd);
Derrick Stolee2fec6042020-09-11 17:49:18 +00001919 crontab_list.git_cmd = 0;
1920
Derrick Stolee31345d52020-11-24 04:16:42 +00001921 if (start_command(&crontab_list))
1922 return error(_("failed to run 'crontab -l'; your system might not support 'cron'"));
Derrick Stolee2fec6042020-09-11 17:49:18 +00001923
1924 /* Ignore exit code, as an empty crontab will return error. */
1925 finish_command(&crontab_list);
1926
1927 /*
1928 * Read from the .lock file, filtering out the old
1929 * schedule while appending the new schedule.
1930 */
Derrick Stolee31345d52020-11-24 04:16:42 +00001931 cron_list = fdopen(fd, "r");
Derrick Stolee2fec6042020-09-11 17:49:18 +00001932 rewind(cron_list);
1933
Derrick Stolee31345d52020-11-24 04:16:42 +00001934 strvec_split(&crontab_edit.args, cmd);
Derrick Stolee2fec6042020-09-11 17:49:18 +00001935 crontab_edit.in = -1;
1936 crontab_edit.git_cmd = 0;
1937
Derrick Stolee31345d52020-11-24 04:16:42 +00001938 if (start_command(&crontab_edit))
1939 return error(_("failed to run 'crontab'; your system might not support 'cron'"));
Derrick Stolee2fec6042020-09-11 17:49:18 +00001940
1941 cron_in = fdopen(crontab_edit.in, "w");
1942 if (!cron_in) {
1943 result = error(_("failed to open stdin of 'crontab'"));
1944 goto done_editing;
1945 }
1946
1947 while (!strbuf_getline_lf(&line, cron_list)) {
1948 if (!in_old_region && !strcmp(line.buf, BEGIN_LINE))
1949 in_old_region = 1;
Martin Ågren66dc0a32020-12-21 22:26:32 +01001950 else if (in_old_region && !strcmp(line.buf, END_LINE))
Derrick Stolee2fec6042020-09-11 17:49:18 +00001951 in_old_region = 0;
Martin Ågren66dc0a32020-12-21 22:26:32 +01001952 else if (!in_old_region)
1953 fprintf(cron_in, "%s\n", line.buf);
Derrick Stolee2fec6042020-09-11 17:49:18 +00001954 }
Lénaïc Huardc5d0b122021-05-10 21:59:09 +02001955 strbuf_release(&line);
Derrick Stolee2fec6042020-09-11 17:49:18 +00001956
1957 if (run_maintenance) {
1958 struct strbuf line_format = STRBUF_INIT;
1959 const char *exec_path = git_exec_path();
1960
1961 fprintf(cron_in, "%s\n", BEGIN_LINE);
1962 fprintf(cron_in,
1963 "# The following schedule was created by Git\n");
1964 fprintf(cron_in, "# Any edits made in this region might be\n");
1965 fprintf(cron_in,
1966 "# replaced in the future by a Git command.\n\n");
1967
1968 strbuf_addf(&line_format,
1969 "%%s %%s * * %%s \"%s/git\" --exec-path=\"%s\" for-each-repo --config=maintenance.repo maintenance run --schedule=%%s\n",
1970 exec_path, exec_path);
1971 fprintf(cron_in, line_format.buf, "0", "1-23", "*", "hourly");
1972 fprintf(cron_in, line_format.buf, "0", "0", "1-6", "daily");
1973 fprintf(cron_in, line_format.buf, "0", "0", "0", "weekly");
1974 strbuf_release(&line_format);
1975
1976 fprintf(cron_in, "\n%s\n", END_LINE);
1977 }
1978
1979 fflush(cron_in);
1980 fclose(cron_in);
1981 close(crontab_edit.in);
1982
1983done_editing:
Derrick Stolee31345d52020-11-24 04:16:42 +00001984 if (finish_command(&crontab_edit))
Derrick Stolee2fec6042020-09-11 17:49:18 +00001985 result = error(_("'crontab' died"));
Derrick Stolee31345d52020-11-24 04:16:42 +00001986 else
1987 fclose(cron_list);
1988 return result;
1989}
Derrick Stolee2fec6042020-09-11 17:49:18 +00001990
Derrick Stolee2afe7e32021-01-05 13:08:27 +00001991#if defined(__APPLE__)
1992static const char platform_scheduler[] = "launchctl";
Derrick Stolee3797a0a2021-01-05 13:08:28 +00001993#elif defined(GIT_WINDOWS_NATIVE)
1994static const char platform_scheduler[] = "schtasks";
Derrick Stolee2afe7e32021-01-05 13:08:27 +00001995#else
Derrick Stolee31345d52020-11-24 04:16:42 +00001996static const char platform_scheduler[] = "crontab";
Derrick Stolee2afe7e32021-01-05 13:08:27 +00001997#endif
Derrick Stolee31345d52020-11-24 04:16:42 +00001998
1999static int update_background_schedule(int enable)
2000{
2001 int result;
2002 const char *scheduler = platform_scheduler;
2003 const char *cmd = scheduler;
2004 char *testing;
2005 struct lock_file lk;
2006 char *lock_path = xstrfmt("%s/schedule", the_repository->objects->odb->path);
2007
2008 testing = xstrdup_or_null(getenv("GIT_TEST_MAINT_SCHEDULER"));
2009 if (testing) {
2010 char *sep = strchr(testing, ':');
2011 if (!sep)
2012 die("GIT_TEST_MAINT_SCHEDULER unparseable: %s", testing);
2013 *sep = '\0';
2014 scheduler = testing;
2015 cmd = sep + 1;
2016 }
2017
Lénaïc Huardc5d0b122021-05-10 21:59:09 +02002018 if (hold_lock_file_for_update(&lk, lock_path, LOCK_NO_DEREF) < 0) {
2019 result = error(_("another process is scheduling background maintenance"));
2020 goto cleanup;
2021 }
Derrick Stolee31345d52020-11-24 04:16:42 +00002022
Derrick Stolee2afe7e32021-01-05 13:08:27 +00002023 if (!strcmp(scheduler, "launchctl"))
Junio C Hamano58e2ce92021-01-25 14:19:17 -08002024 result = launchctl_update_schedule(enable, get_lock_file_fd(&lk), cmd);
Derrick Stolee3797a0a2021-01-05 13:08:28 +00002025 else if (!strcmp(scheduler, "schtasks"))
Junio C Hamano58e2ce92021-01-25 14:19:17 -08002026 result = schtasks_update_schedule(enable, get_lock_file_fd(&lk), cmd);
Derrick Stolee2afe7e32021-01-05 13:08:27 +00002027 else if (!strcmp(scheduler, "crontab"))
Junio C Hamano58e2ce92021-01-25 14:19:17 -08002028 result = crontab_update_schedule(enable, get_lock_file_fd(&lk), cmd);
Derrick Stolee31345d52020-11-24 04:16:42 +00002029 else
2030 die("unknown background scheduler: %s", scheduler);
2031
Derrick Stolee2fec6042020-09-11 17:49:18 +00002032 rollback_lock_file(&lk);
Lénaïc Huardc5d0b122021-05-10 21:59:09 +02002033
2034cleanup:
2035 free(lock_path);
Derrick Stolee31345d52020-11-24 04:16:42 +00002036 free(testing);
Derrick Stolee2fec6042020-09-11 17:49:18 +00002037 return result;
2038}
2039
2040static int maintenance_start(void)
2041{
2042 if (maintenance_register())
2043 warning(_("failed to add repo to global config"));
2044
2045 return update_background_schedule(1);
2046}
2047
2048static int maintenance_stop(void)
2049{
2050 return update_background_schedule(0);
2051}
2052
Derrick Stolee0c18b702020-09-11 17:49:17 +00002053static const char builtin_maintenance_usage[] = N_("git maintenance <subcommand> [<options>]");
Derrick Stolee2057d752020-09-17 18:11:42 +00002054
2055int cmd_maintenance(int argc, const char **argv, const char *prefix)
2056{
2057 if (argc < 2 ||
2058 (argc == 2 && !strcmp(argv[1], "-h")))
2059 usage(builtin_maintenance_usage);
2060
2061 if (!strcmp(argv[1], "run"))
2062 return maintenance_run(argc - 1, argv + 1, prefix);
Derrick Stolee2fec6042020-09-11 17:49:18 +00002063 if (!strcmp(argv[1], "start"))
2064 return maintenance_start();
2065 if (!strcmp(argv[1], "stop"))
2066 return maintenance_stop();
Derrick Stolee0c18b702020-09-11 17:49:17 +00002067 if (!strcmp(argv[1], "register"))
2068 return maintenance_register();
2069 if (!strcmp(argv[1], "unregister"))
2070 return maintenance_unregister();
Derrick Stolee2057d752020-09-17 18:11:42 +00002071
2072 die(_("invalid subcommand: %s"), argv[1]);
2073}