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