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