]>
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" |
e841a79a | 33 | #include "object-store.h" |
6757ada4 JB |
34 | |
35 | #define FAILED_RUN "failed to run %s" | |
36 | ||
44c637c8 | 37 | static const char * const builtin_gc_usage[] = { |
9c9b4f2f | 38 | N_("git gc [<options>]"), |
44c637c8 JB |
39 | NULL |
40 | }; | |
6757ada4 | 41 | |
56752391 | 42 | static int pack_refs = 1; |
62aad184 | 43 | static int prune_reflogs = 1; |
07e7dbf0 | 44 | static int aggressive_depth = 50; |
1c192f34 | 45 | static int aggressive_window = 250; |
2c3c4399 | 46 | static int gc_auto_threshold = 6700; |
97063974 | 47 | static int gc_auto_pack_limit = 50; |
9f673f94 | 48 | static int detach_auto = 1; |
dddbad72 | 49 | static timestamp_t gc_log_expire_time; |
a831c06a | 50 | static const char *gc_log_expire = "1.day.ago"; |
d3154b44 | 51 | static const char *prune_expire = "2.weeks.ago"; |
e3df33bb | 52 | static const char *prune_worktrees_expire = "3.months.ago"; |
55dfe13d | 53 | static unsigned long big_pack_threshold; |
9806f5a7 | 54 | static unsigned long max_delta_cache_size = DEFAULT_DELTA_CACHE_SIZE; |
6757ada4 | 55 | |
22f9b7f3 JK |
56 | static struct strvec pack_refs_cmd = STRVEC_INIT; |
57 | static struct strvec reflog = STRVEC_INIT; | |
58 | static struct strvec repack = STRVEC_INIT; | |
59 | static struct strvec prune = STRVEC_INIT; | |
60 | static struct strvec prune_worktrees = STRVEC_INIT; | |
61 | static struct strvec rerere = STRVEC_INIT; | |
6757ada4 | 62 | |
076aa2cb | 63 | static struct tempfile *pidfile; |
329e6e87 | 64 | static struct lock_file log_lock; |
4c5baf02 | 65 | |
478f34d2 DK |
66 | static struct string_list pack_garbage = STRING_LIST_INIT_DUP; |
67 | ||
68 | static void clean_pack_garbage(void) | |
69 | { | |
70 | int i; | |
71 | for (i = 0; i < pack_garbage.nr; i++) | |
72 | unlink_or_warn(pack_garbage.items[i].string); | |
73 | string_list_clear(&pack_garbage, 0); | |
74 | } | |
75 | ||
76 | static void report_pack_garbage(unsigned seen_bits, const char *path) | |
77 | { | |
78 | if (seen_bits == PACKDIR_FILE_IDX) | |
79 | string_list_append(&pack_garbage, path); | |
80 | } | |
81 | ||
329e6e87 NTND |
82 | static void process_log_file(void) |
83 | { | |
84 | struct stat st; | |
a831c06a DT |
85 | if (fstat(get_lock_file_fd(&log_lock), &st)) { |
86 | /* | |
87 | * Perhaps there was an i/o error or another | |
88 | * unlikely situation. Try to make a note of | |
89 | * this in gc.log along with any existing | |
90 | * messages. | |
91 | */ | |
92 | int saved_errno = errno; | |
93 | fprintf(stderr, _("Failed to fstat %s: %s"), | |
076aa2cb | 94 | get_tempfile_path(log_lock.tempfile), |
a831c06a DT |
95 | strerror(saved_errno)); |
96 | fflush(stderr); | |
329e6e87 | 97 | commit_lock_file(&log_lock); |
a831c06a DT |
98 | errno = saved_errno; |
99 | } else if (st.st_size) { | |
100 | /* There was some error recorded in the lock file */ | |
101 | commit_lock_file(&log_lock); | |
102 | } else { | |
103 | /* No error, clean up any old gc.log */ | |
104 | unlink(git_path("gc.log")); | |
329e6e87 | 105 | rollback_lock_file(&log_lock); |
a831c06a | 106 | } |
329e6e87 NTND |
107 | } |
108 | ||
109 | static void process_log_file_at_exit(void) | |
110 | { | |
111 | fflush(stderr); | |
112 | process_log_file(); | |
113 | } | |
114 | ||
115 | static void process_log_file_on_signal(int signo) | |
116 | { | |
117 | process_log_file(); | |
118 | sigchain_pop(signo); | |
119 | raise(signo); | |
120 | } | |
121 | ||
bf3d70fe ÆAB |
122 | static int gc_config_is_timestamp_never(const char *var) |
123 | { | |
124 | const char *value; | |
125 | timestamp_t expire; | |
126 | ||
127 | if (!git_config_get_value(var, &value) && value) { | |
128 | if (parse_expiry_date(value, &expire)) | |
129 | die(_("failed to parse '%s' value '%s'"), var, value); | |
130 | return expire == 0; | |
131 | } | |
132 | return 0; | |
133 | } | |
134 | ||
5801d3b4 | 135 | static void gc_config(void) |
6757ada4 | 136 | { |
5801d3b4 TA |
137 | const char *value; |
138 | ||
139 | if (!git_config_get_value("gc.packrefs", &value)) { | |
c5e5a2c0 | 140 | if (value && !strcmp(value, "notbare")) |
6757ada4 JB |
141 | pack_refs = -1; |
142 | else | |
5801d3b4 | 143 | pack_refs = git_config_bool("gc.packrefs", value); |
17815501 | 144 | } |
5801d3b4 | 145 | |
bf3d70fe ÆAB |
146 | if (gc_config_is_timestamp_never("gc.reflogexpire") && |
147 | gc_config_is_timestamp_never("gc.reflogexpireunreachable")) | |
148 | prune_reflogs = 0; | |
149 | ||
5801d3b4 TA |
150 | git_config_get_int("gc.aggressivewindow", &aggressive_window); |
151 | git_config_get_int("gc.aggressivedepth", &aggressive_depth); | |
152 | git_config_get_int("gc.auto", &gc_auto_threshold); | |
153 | git_config_get_int("gc.autopacklimit", &gc_auto_pack_limit); | |
154 | git_config_get_bool("gc.autodetach", &detach_auto); | |
77d67977 CC |
155 | git_config_get_expiry("gc.pruneexpire", &prune_expire); |
156 | git_config_get_expiry("gc.worktreepruneexpire", &prune_worktrees_expire); | |
94c9b5af | 157 | git_config_get_expiry("gc.logexpiry", &gc_log_expire); |
a831c06a | 158 | |
55dfe13d | 159 | git_config_get_ulong("gc.bigpackthreshold", &big_pack_threshold); |
9806f5a7 | 160 | git_config_get_ulong("pack.deltacachesize", &max_delta_cache_size); |
55dfe13d | 161 | |
5801d3b4 | 162 | git_config(git_default_config, NULL); |
6757ada4 JB |
163 | } |
164 | ||
a087cc98 | 165 | static int too_many_loose_objects(void) |
2c3c4399 JH |
166 | { |
167 | /* | |
168 | * Quickly check if a "gc" is needed, by estimating how | |
169 | * many loose objects there are. Because SHA-1 is evenly | |
170 | * distributed, we can check only one and get a reasonable | |
171 | * estimate. | |
172 | */ | |
2c3c4399 JH |
173 | DIR *dir; |
174 | struct dirent *ent; | |
175 | int auto_threshold; | |
176 | int num_loose = 0; | |
177 | int needed = 0; | |
e5cdbd5f | 178 | const unsigned hexsz_loose = the_hash_algo->hexsz - 2; |
2c3c4399 | 179 | |
07af8891 | 180 | dir = opendir(git_path("objects/17")); |
2c3c4399 JH |
181 | if (!dir) |
182 | return 0; | |
183 | ||
42c78a21 | 184 | auto_threshold = DIV_ROUND_UP(gc_auto_threshold, 256); |
2c3c4399 | 185 | while ((ent = readdir(dir)) != NULL) { |
e5cdbd5f ÆAB |
186 | if (strspn(ent->d_name, "0123456789abcdef") != hexsz_loose || |
187 | ent->d_name[hexsz_loose] != '\0') | |
2c3c4399 JH |
188 | continue; |
189 | if (++num_loose > auto_threshold) { | |
190 | needed = 1; | |
191 | break; | |
192 | } | |
193 | } | |
194 | closedir(dir); | |
195 | return needed; | |
196 | } | |
197 | ||
9806f5a7 NTND |
198 | static struct packed_git *find_base_packs(struct string_list *packs, |
199 | unsigned long limit) | |
ae4e89e5 NTND |
200 | { |
201 | struct packed_git *p, *base = NULL; | |
202 | ||
454ea2e4 | 203 | for (p = get_all_packs(the_repository); p; p = p->next) { |
ae4e89e5 NTND |
204 | if (!p->pack_local) |
205 | continue; | |
55dfe13d NTND |
206 | if (limit) { |
207 | if (p->pack_size >= limit) | |
208 | string_list_append(packs, p->pack_name); | |
209 | } else if (!base || base->pack_size < p->pack_size) { | |
ae4e89e5 NTND |
210 | base = p; |
211 | } | |
212 | } | |
213 | ||
214 | if (base) | |
215 | string_list_append(packs, base->pack_name); | |
9806f5a7 NTND |
216 | |
217 | return base; | |
ae4e89e5 NTND |
218 | } |
219 | ||
17815501 JH |
220 | static int too_many_packs(void) |
221 | { | |
222 | struct packed_git *p; | |
223 | int cnt; | |
224 | ||
225 | if (gc_auto_pack_limit <= 0) | |
226 | return 0; | |
227 | ||
454ea2e4 | 228 | for (cnt = 0, p = get_all_packs(the_repository); p; p = p->next) { |
17815501 JH |
229 | if (!p->pack_local) |
230 | continue; | |
01af249f | 231 | if (p->pack_keep) |
17815501 JH |
232 | continue; |
233 | /* | |
234 | * Perhaps check the size of the pack and count only | |
235 | * very small ones here? | |
236 | */ | |
237 | cnt++; | |
238 | } | |
5f4e3bf5 | 239 | return gc_auto_pack_limit < cnt; |
17815501 JH |
240 | } |
241 | ||
9806f5a7 NTND |
242 | static uint64_t total_ram(void) |
243 | { | |
244 | #if defined(HAVE_SYSINFO) | |
245 | struct sysinfo si; | |
246 | ||
247 | if (!sysinfo(&si)) | |
248 | return si.totalram; | |
249 | #elif defined(HAVE_BSD_SYSCTL) && (defined(HW_MEMSIZE) || defined(HW_PHYSMEM)) | |
250 | int64_t physical_memory; | |
251 | int mib[2]; | |
252 | size_t length; | |
253 | ||
254 | mib[0] = CTL_HW; | |
255 | # if defined(HW_MEMSIZE) | |
256 | mib[1] = HW_MEMSIZE; | |
257 | # else | |
258 | mib[1] = HW_PHYSMEM; | |
259 | # endif | |
260 | length = sizeof(int64_t); | |
261 | if (!sysctl(mib, 2, &physical_memory, &length, NULL, 0)) | |
262 | return physical_memory; | |
263 | #elif defined(GIT_WINDOWS_NATIVE) | |
264 | MEMORYSTATUSEX memInfo; | |
265 | ||
266 | memInfo.dwLength = sizeof(MEMORYSTATUSEX); | |
267 | if (GlobalMemoryStatusEx(&memInfo)) | |
268 | return memInfo.ullTotalPhys; | |
269 | #endif | |
270 | return 0; | |
271 | } | |
272 | ||
273 | static uint64_t estimate_repack_memory(struct packed_git *pack) | |
274 | { | |
275 | unsigned long nr_objects = approximate_object_count(); | |
276 | size_t os_cache, heap; | |
277 | ||
278 | if (!pack || !nr_objects) | |
279 | return 0; | |
280 | ||
281 | /* | |
282 | * First we have to scan through at least one pack. | |
283 | * Assume enough room in OS file cache to keep the entire pack | |
284 | * or we may accidentally evict data of other processes from | |
285 | * the cache. | |
286 | */ | |
287 | os_cache = pack->pack_size + pack->index_size; | |
288 | /* then pack-objects needs lots more for book keeping */ | |
289 | heap = sizeof(struct object_entry) * nr_objects; | |
290 | /* | |
291 | * internal rev-list --all --objects takes up some memory too, | |
292 | * let's say half of it is for blobs | |
293 | */ | |
294 | heap += sizeof(struct blob) * nr_objects / 2; | |
295 | /* | |
296 | * and the other half is for trees (commits and tags are | |
297 | * usually insignificant) | |
298 | */ | |
299 | heap += sizeof(struct tree) * nr_objects / 2; | |
300 | /* and then obj_hash[], underestimated in fact */ | |
301 | heap += sizeof(struct object *) * nr_objects; | |
302 | /* revindex is used also */ | |
303 | heap += sizeof(struct revindex_entry) * nr_objects; | |
304 | /* | |
305 | * read_sha1_file() (either at delta calculation phase, or | |
306 | * writing phase) also fills up the delta base cache | |
307 | */ | |
308 | heap += delta_base_cache_limit; | |
309 | /* and of course pack-objects has its own delta cache */ | |
310 | heap += max_delta_cache_size; | |
311 | ||
312 | return os_cache + heap; | |
313 | } | |
314 | ||
ae4e89e5 NTND |
315 | static int keep_one_pack(struct string_list_item *item, void *data) |
316 | { | |
22f9b7f3 | 317 | strvec_pushf(&repack, "--keep-pack=%s", basename(item->string)); |
ae4e89e5 NTND |
318 | return 0; |
319 | } | |
320 | ||
321 | static void add_repack_all_option(struct string_list *keep_pack) | |
7e52f566 JK |
322 | { |
323 | if (prune_expire && !strcmp(prune_expire, "now")) | |
22f9b7f3 | 324 | strvec_push(&repack, "-a"); |
7e52f566 | 325 | else { |
22f9b7f3 | 326 | strvec_push(&repack, "-A"); |
234587fc | 327 | if (prune_expire) |
22f9b7f3 | 328 | strvec_pushf(&repack, "--unpack-unreachable=%s", prune_expire); |
7e52f566 | 329 | } |
ae4e89e5 NTND |
330 | |
331 | if (keep_pack) | |
332 | for_each_string_list(keep_pack, keep_one_pack, NULL); | |
7e52f566 JK |
333 | } |
334 | ||
bdf56de8 DT |
335 | static void add_repack_incremental_option(void) |
336 | { | |
22f9b7f3 | 337 | strvec_push(&repack, "--no-write-bitmap-index"); |
bdf56de8 DT |
338 | } |
339 | ||
a087cc98 JH |
340 | static int need_to_gc(void) |
341 | { | |
342 | /* | |
b14d255b BC |
343 | * Setting gc.auto to 0 or negative can disable the |
344 | * automatic gc. | |
a087cc98 | 345 | */ |
b14d255b | 346 | if (gc_auto_threshold <= 0) |
95143f9e JH |
347 | return 0; |
348 | ||
17815501 JH |
349 | /* |
350 | * If there are too many loose objects, but not too many | |
351 | * packs, we run "repack -d -l". If there are too many packs, | |
352 | * we run "repack -A -d -l". Otherwise we tell the caller | |
353 | * there is no need. | |
354 | */ | |
55dfe13d NTND |
355 | if (too_many_packs()) { |
356 | struct string_list keep_pack = STRING_LIST_INIT_NODUP; | |
357 | ||
8fc67762 | 358 | if (big_pack_threshold) { |
55dfe13d | 359 | find_base_packs(&keep_pack, big_pack_threshold); |
8fc67762 NTND |
360 | if (keep_pack.nr >= gc_auto_pack_limit) { |
361 | big_pack_threshold = 0; | |
362 | string_list_clear(&keep_pack, 0); | |
363 | find_base_packs(&keep_pack, 0); | |
364 | } | |
9806f5a7 NTND |
365 | } else { |
366 | struct packed_git *p = find_base_packs(&keep_pack, 0); | |
367 | uint64_t mem_have, mem_want; | |
368 | ||
369 | mem_have = total_ram(); | |
370 | mem_want = estimate_repack_memory(p); | |
371 | ||
372 | /* | |
373 | * Only allow 1/2 of memory for pack-objects, leave | |
374 | * the rest for the OS and other processes in the | |
375 | * system. | |
376 | */ | |
377 | if (!mem_have || mem_want < mem_have / 2) | |
378 | string_list_clear(&keep_pack, 0); | |
8fc67762 | 379 | } |
55dfe13d NTND |
380 | |
381 | add_repack_all_option(&keep_pack); | |
382 | string_list_clear(&keep_pack, 0); | |
383 | } else if (too_many_loose_objects()) | |
bdf56de8 DT |
384 | add_repack_incremental_option(); |
385 | else | |
17815501 | 386 | return 0; |
bde30540 | 387 | |
15048f8a | 388 | if (run_hook_le(NULL, "pre-auto-gc", NULL)) |
bde30540 | 389 | return 0; |
95143f9e | 390 | return 1; |
a087cc98 JH |
391 | } |
392 | ||
64a99eb4 NTND |
393 | /* return NULL on success, else hostname running the gc */ |
394 | static const char *lock_repo_for_gc(int force, pid_t* ret_pid) | |
395 | { | |
b2275868 | 396 | struct lock_file lock = LOCK_INIT; |
da25bdb7 | 397 | char my_host[HOST_NAME_MAX + 1]; |
64a99eb4 NTND |
398 | struct strbuf sb = STRBUF_INIT; |
399 | struct stat st; | |
400 | uintmax_t pid; | |
401 | FILE *fp; | |
4f1c0b21 | 402 | int fd; |
00539cef | 403 | char *pidfile_path; |
64a99eb4 | 404 | |
076aa2cb | 405 | if (is_tempfile_active(pidfile)) |
4c5baf02 JN |
406 | /* already locked */ |
407 | return NULL; | |
408 | ||
5781a9a2 | 409 | if (xgethostname(my_host, sizeof(my_host))) |
5096d490 | 410 | xsnprintf(my_host, sizeof(my_host), "unknown"); |
64a99eb4 | 411 | |
00539cef MH |
412 | pidfile_path = git_pathdup("gc.pid"); |
413 | fd = hold_lock_file_for_update(&lock, pidfile_path, | |
64a99eb4 NTND |
414 | LOCK_DIE_ON_ERROR); |
415 | if (!force) { | |
da25bdb7 RS |
416 | static char locking_host[HOST_NAME_MAX + 1]; |
417 | static char *scan_fmt; | |
4f1c0b21 | 418 | int should_exit; |
da25bdb7 RS |
419 | |
420 | if (!scan_fmt) | |
afe2fab7 | 421 | scan_fmt = xstrfmt("%s %%%ds", "%"SCNuMAX, HOST_NAME_MAX); |
00539cef | 422 | fp = fopen(pidfile_path, "r"); |
64a99eb4 NTND |
423 | memset(locking_host, 0, sizeof(locking_host)); |
424 | should_exit = | |
425 | fp != NULL && | |
426 | !fstat(fileno(fp), &st) && | |
427 | /* | |
428 | * 12 hour limit is very generous as gc should | |
429 | * never take that long. On the other hand we | |
430 | * don't really need a strict limit here, | |
431 | * running gc --auto one day late is not a big | |
432 | * problem. --force can be used in manual gc | |
433 | * after the user verifies that no gc is | |
434 | * running. | |
435 | */ | |
436 | time(NULL) - st.st_mtime <= 12 * 3600 && | |
da25bdb7 | 437 | fscanf(fp, scan_fmt, &pid, locking_host) == 2 && |
64a99eb4 | 438 | /* be gentle to concurrent "gc" on remote hosts */ |
ed7eda8b | 439 | (strcmp(locking_host, my_host) || !kill(pid, 0) || errno == EPERM); |
64a99eb4 NTND |
440 | if (fp != NULL) |
441 | fclose(fp); | |
442 | if (should_exit) { | |
443 | if (fd >= 0) | |
444 | rollback_lock_file(&lock); | |
445 | *ret_pid = pid; | |
00539cef | 446 | free(pidfile_path); |
64a99eb4 NTND |
447 | return locking_host; |
448 | } | |
449 | } | |
450 | ||
451 | strbuf_addf(&sb, "%"PRIuMAX" %s", | |
452 | (uintmax_t) getpid(), my_host); | |
453 | write_in_full(fd, sb.buf, sb.len); | |
454 | strbuf_release(&sb); | |
455 | commit_lock_file(&lock); | |
076aa2cb | 456 | pidfile = register_tempfile(pidfile_path); |
ebebeaea | 457 | free(pidfile_path); |
64a99eb4 NTND |
458 | return NULL; |
459 | } | |
460 | ||
30299702 JN |
461 | /* |
462 | * Returns 0 if there was no previous error and gc can proceed, 1 if | |
463 | * gc should not proceed due to an error in the last run. Prints a | |
15beaaa3 | 464 | * message and returns -1 if an error occurred while reading gc.log |
30299702 JN |
465 | */ |
466 | static int report_last_gc_error(void) | |
329e6e87 NTND |
467 | { |
468 | struct strbuf sb = STRBUF_INIT; | |
30299702 | 469 | int ret = 0; |
3c426ecc | 470 | ssize_t len; |
a831c06a DT |
471 | struct stat st; |
472 | char *gc_log_path = git_pathdup("gc.log"); | |
329e6e87 | 473 | |
a831c06a DT |
474 | if (stat(gc_log_path, &st)) { |
475 | if (errno == ENOENT) | |
476 | goto done; | |
477 | ||
30299702 JN |
478 | ret = error_errno(_("cannot stat '%s'"), gc_log_path); |
479 | goto done; | |
a831c06a DT |
480 | } |
481 | ||
482 | if (st.st_mtime < gc_log_expire_time) | |
483 | goto done; | |
484 | ||
3c426ecc JN |
485 | len = strbuf_read_file(&sb, gc_log_path, 0); |
486 | if (len < 0) | |
30299702 JN |
487 | ret = error_errno(_("cannot read '%s'"), gc_log_path); |
488 | else if (len > 0) { | |
489 | /* | |
490 | * A previous gc failed. Report the error, and don't | |
491 | * bother with an automatic gc run since it is likely | |
492 | * to fail in the same way. | |
493 | */ | |
494 | warning(_("The last gc run reported the following. " | |
329e6e87 NTND |
495 | "Please correct the root cause\n" |
496 | "and remove %s.\n" | |
497 | "Automatic cleanup will not be performed " | |
498 | "until the file is removed.\n\n" | |
499 | "%s"), | |
a831c06a | 500 | gc_log_path, sb.buf); |
30299702 JN |
501 | ret = 1; |
502 | } | |
329e6e87 | 503 | strbuf_release(&sb); |
a831c06a DT |
504 | done: |
505 | free(gc_log_path); | |
30299702 | 506 | return ret; |
329e6e87 NTND |
507 | } |
508 | ||
fec2ed21 | 509 | static void gc_before_repack(void) |
62aad184 | 510 | { |
cd8eb3a0 ÆAB |
511 | /* |
512 | * We may be called twice, as both the pre- and | |
513 | * post-daemonized phases will call us, but running these | |
514 | * commands more than once is pointless and wasteful. | |
515 | */ | |
516 | static int done = 0; | |
517 | if (done++) | |
518 | return; | |
519 | ||
d70a9eb6 JK |
520 | if (pack_refs && run_command_v_opt(pack_refs_cmd.v, RUN_GIT_CMD)) |
521 | die(FAILED_RUN, pack_refs_cmd.v[0]); | |
62aad184 | 522 | |
d70a9eb6 JK |
523 | if (prune_reflogs && run_command_v_opt(reflog.v, RUN_GIT_CMD)) |
524 | die(FAILED_RUN, reflog.v[0]); | |
62aad184 NTND |
525 | } |
526 | ||
6757ada4 JB |
527 | int cmd_gc(int argc, const char **argv, const char *prefix) |
528 | { | |
44c637c8 | 529 | int aggressive = 0; |
2c3c4399 | 530 | int auto_gc = 0; |
a0c14cbb | 531 | int quiet = 0; |
64a99eb4 NTND |
532 | int force = 0; |
533 | const char *name; | |
534 | pid_t pid; | |
329e6e87 | 535 | int daemonized = 0; |
ae4e89e5 | 536 | int keep_base_pack = -1; |
8ab5aa4b | 537 | timestamp_t dummy; |
6757ada4 | 538 | |
44c637c8 | 539 | struct option builtin_gc_options[] = { |
6705c162 NTND |
540 | OPT__QUIET(&quiet, N_("suppress progress reporting")), |
541 | { OPTION_STRING, 0, "prune", &prune_expire, N_("date"), | |
542 | N_("prune unreferenced objects"), | |
58e9d9d4 | 543 | PARSE_OPT_OPTARG, NULL, (intptr_t)prune_expire }, |
d5d09d47 | 544 | OPT_BOOL(0, "aggressive", &aggressive, N_("be more thorough (increased runtime)")), |
7e1eeaa4 NTND |
545 | OPT_BOOL_F(0, "auto", &auto_gc, N_("enable auto-gc mode"), |
546 | PARSE_OPT_NOCOMPLETE), | |
547 | OPT_BOOL_F(0, "force", &force, | |
548 | N_("force running gc even if there may be another gc running"), | |
549 | PARSE_OPT_NOCOMPLETE), | |
ae4e89e5 NTND |
550 | OPT_BOOL(0, "keep-largest-pack", &keep_base_pack, |
551 | N_("repack all other packs except the largest pack")), | |
44c637c8 JB |
552 | OPT_END() |
553 | }; | |
554 | ||
0c8151b6 NTND |
555 | if (argc == 2 && !strcmp(argv[1], "-h")) |
556 | usage_with_options(builtin_gc_usage, builtin_gc_options); | |
557 | ||
22f9b7f3 JK |
558 | strvec_pushl(&pack_refs_cmd, "pack-refs", "--all", "--prune", NULL); |
559 | strvec_pushl(&reflog, "reflog", "expire", "--all", NULL); | |
560 | strvec_pushl(&repack, "repack", "-d", "-l", NULL); | |
561 | strvec_pushl(&prune, "prune", "--expire", NULL); | |
562 | strvec_pushl(&prune_worktrees, "worktree", "prune", "--expire", NULL); | |
563 | strvec_pushl(&rerere, "rerere", "gc", NULL); | |
234587fc | 564 | |
a831c06a | 565 | /* default expiry time, overwritten in gc_config */ |
5801d3b4 | 566 | gc_config(); |
a831c06a | 567 | if (parse_expiry_date(gc_log_expire, &gc_log_expire_time)) |
96913c9d | 568 | die(_("failed to parse gc.logexpiry value %s"), gc_log_expire); |
6757ada4 JB |
569 | |
570 | if (pack_refs < 0) | |
571 | pack_refs = !is_bare_repository(); | |
572 | ||
37782920 SB |
573 | argc = parse_options(argc, argv, prefix, builtin_gc_options, |
574 | builtin_gc_usage, 0); | |
44c637c8 JB |
575 | if (argc > 0) |
576 | usage_with_options(builtin_gc_usage, builtin_gc_options); | |
577 | ||
8ab5aa4b JH |
578 | if (prune_expire && parse_expiry_date(prune_expire, &dummy)) |
579 | die(_("failed to parse prune expiry value %s"), prune_expire); | |
580 | ||
44c637c8 | 581 | if (aggressive) { |
22f9b7f3 | 582 | strvec_push(&repack, "-f"); |
125f8146 | 583 | if (aggressive_depth > 0) |
22f9b7f3 | 584 | strvec_pushf(&repack, "--depth=%d", aggressive_depth); |
234587fc | 585 | if (aggressive_window > 0) |
22f9b7f3 | 586 | strvec_pushf(&repack, "--window=%d", aggressive_window); |
6757ada4 | 587 | } |
a0c14cbb | 588 | if (quiet) |
22f9b7f3 | 589 | strvec_push(&repack, "-q"); |
6757ada4 | 590 | |
2c3c4399 JH |
591 | if (auto_gc) { |
592 | /* | |
593 | * Auto-gc should be least intrusive as possible. | |
594 | */ | |
2c3c4399 JH |
595 | if (!need_to_gc()) |
596 | return 0; | |
9f673f94 NTND |
597 | if (!quiet) { |
598 | if (detach_auto) | |
599 | fprintf(stderr, _("Auto packing the repository in background for optimum performance.\n")); | |
600 | else | |
601 | fprintf(stderr, _("Auto packing the repository for optimum performance.\n")); | |
602 | fprintf(stderr, _("See \"git help gc\" for manual housekeeping.\n")); | |
603 | } | |
62aad184 | 604 | if (detach_auto) { |
30299702 JN |
605 | int ret = report_last_gc_error(); |
606 | if (ret < 0) | |
15beaaa3 | 607 | /* an I/O error occurred, already reported */ |
30299702 JN |
608 | exit(128); |
609 | if (ret == 1) | |
610 | /* Last gc --auto failed. Skip this one. */ | |
611 | return 0; | |
329e6e87 | 612 | |
c45af94d JK |
613 | if (lock_repo_for_gc(force, &pid)) |
614 | return 0; | |
fec2ed21 | 615 | gc_before_repack(); /* dies on failure */ |
c45af94d JK |
616 | delete_tempfile(&pidfile); |
617 | ||
9f673f94 NTND |
618 | /* |
619 | * failure to daemonize is ok, we'll continue | |
620 | * in foreground | |
621 | */ | |
329e6e87 | 622 | daemonized = !daemonize(); |
62aad184 | 623 | } |
ae4e89e5 NTND |
624 | } else { |
625 | struct string_list keep_pack = STRING_LIST_INIT_NODUP; | |
626 | ||
627 | if (keep_base_pack != -1) { | |
628 | if (keep_base_pack) | |
55dfe13d NTND |
629 | find_base_packs(&keep_pack, 0); |
630 | } else if (big_pack_threshold) { | |
631 | find_base_packs(&keep_pack, big_pack_threshold); | |
ae4e89e5 NTND |
632 | } |
633 | ||
634 | add_repack_all_option(&keep_pack); | |
635 | string_list_clear(&keep_pack, 0); | |
636 | } | |
2c3c4399 | 637 | |
64a99eb4 NTND |
638 | name = lock_repo_for_gc(force, &pid); |
639 | if (name) { | |
640 | if (auto_gc) | |
641 | return 0; /* be quiet on --auto */ | |
642 | die(_("gc is already running on machine '%s' pid %"PRIuMAX" (use --force if not)"), | |
643 | name, (uintmax_t)pid); | |
644 | } | |
645 | ||
329e6e87 NTND |
646 | if (daemonized) { |
647 | hold_lock_file_for_update(&log_lock, | |
648 | git_path("gc.log"), | |
649 | LOCK_DIE_ON_ERROR); | |
076c8278 | 650 | dup2(get_lock_file_fd(&log_lock), 2); |
329e6e87 NTND |
651 | sigchain_push_common(process_log_file_on_signal); |
652 | atexit(process_log_file_at_exit); | |
653 | } | |
654 | ||
fec2ed21 | 655 | gc_before_repack(); |
6757ada4 | 656 | |
067fbd41 | 657 | if (!repository_format_precious_objects) { |
2d511cfc | 658 | close_object_store(the_repository->objects); |
d70a9eb6 JK |
659 | if (run_command_v_opt(repack.v, RUN_GIT_CMD)) |
660 | die(FAILED_RUN, repack.v[0]); | |
067fbd41 JK |
661 | |
662 | if (prune_expire) { | |
22f9b7f3 | 663 | strvec_push(&prune, prune_expire); |
067fbd41 | 664 | if (quiet) |
22f9b7f3 | 665 | strvec_push(&prune, "--no-progress"); |
b14ed5ad | 666 | if (has_promisor_remote()) |
22f9b7f3 | 667 | strvec_push(&prune, |
f6d8942b | 668 | "--exclude-promisor-objects"); |
d70a9eb6 JK |
669 | if (run_command_v_opt(prune.v, RUN_GIT_CMD)) |
670 | die(FAILED_RUN, prune.v[0]); | |
067fbd41 | 671 | } |
58e9d9d4 | 672 | } |
6757ada4 | 673 | |
e3df33bb | 674 | if (prune_worktrees_expire) { |
22f9b7f3 | 675 | strvec_push(&prune_worktrees, prune_worktrees_expire); |
d70a9eb6 JK |
676 | if (run_command_v_opt(prune_worktrees.v, RUN_GIT_CMD)) |
677 | die(FAILED_RUN, prune_worktrees.v[0]); | |
e3df33bb NTND |
678 | } |
679 | ||
d70a9eb6 JK |
680 | if (run_command_v_opt(rerere.v, RUN_GIT_CMD)) |
681 | die(FAILED_RUN, rerere.v[0]); | |
6757ada4 | 682 | |
478f34d2 | 683 | report_garbage = report_pack_garbage; |
a49d2834 | 684 | reprepare_packed_git(the_repository); |
5bdece0d | 685 | if (pack_garbage.nr > 0) { |
2d511cfc | 686 | close_object_store(the_repository->objects); |
478f34d2 | 687 | clean_pack_garbage(); |
5bdece0d | 688 | } |
478f34d2 | 689 | |
7211b9e7 DS |
690 | prepare_repo_settings(the_repository); |
691 | if (the_repository->settings.gc_write_commit_graph == 1) | |
0bd52e27 | 692 | write_commit_graph_reachable(the_repository->objects->odb, |
f4f8dfe1 | 693 | !quiet && !daemonized ? COMMIT_GRAPH_WRITE_PROGRESS : 0, |
7211b9e7 | 694 | NULL); |
d5d5d7b6 | 695 | |
a087cc98 | 696 | if (auto_gc && too_many_loose_objects()) |
fea6128b ÆAB |
697 | warning(_("There are too many unreachable loose objects; " |
698 | "run 'git prune' to remove them.")); | |
a087cc98 | 699 | |
a831c06a DT |
700 | if (!daemonized) |
701 | unlink(git_path("gc.log")); | |
702 | ||
6757ada4 JB |
703 | return 0; |
704 | } | |
2057d750 | 705 | |
b08ff1fe DS |
706 | static const char *const builtin_maintenance_run_usage[] = { |
707 | N_("git maintenance run [--auto] [--[no-]quiet] [--task=<task>] [--schedule]"), | |
2057d750 DS |
708 | NULL |
709 | }; | |
710 | ||
b08ff1fe DS |
711 | enum schedule_priority { |
712 | SCHEDULE_NONE = 0, | |
713 | SCHEDULE_WEEKLY = 1, | |
714 | SCHEDULE_DAILY = 2, | |
715 | SCHEDULE_HOURLY = 3, | |
716 | }; | |
717 | ||
718 | static enum schedule_priority parse_schedule(const char *value) | |
719 | { | |
720 | if (!value) | |
721 | return SCHEDULE_NONE; | |
722 | if (!strcasecmp(value, "hourly")) | |
723 | return SCHEDULE_HOURLY; | |
724 | if (!strcasecmp(value, "daily")) | |
725 | return SCHEDULE_DAILY; | |
726 | if (!strcasecmp(value, "weekly")) | |
727 | return SCHEDULE_WEEKLY; | |
728 | return SCHEDULE_NONE; | |
729 | } | |
730 | ||
731 | static int maintenance_opt_schedule(const struct option *opt, const char *arg, | |
732 | int unset) | |
733 | { | |
734 | enum schedule_priority *priority = opt->value; | |
735 | ||
736 | if (unset) | |
737 | die(_("--no-schedule is not allowed")); | |
738 | ||
739 | *priority = parse_schedule(arg); | |
740 | ||
741 | if (!*priority) | |
742 | die(_("unrecognized --schedule argument '%s'"), arg); | |
743 | ||
744 | return 0; | |
745 | } | |
746 | ||
2057d750 DS |
747 | struct maintenance_run_opts { |
748 | int auto_flag; | |
3ddaad0e | 749 | int quiet; |
b08ff1fe | 750 | enum schedule_priority schedule; |
2057d750 DS |
751 | }; |
752 | ||
4ddc79b2 DS |
753 | /* Remember to update object flag allocation in object.h */ |
754 | #define SEEN (1u<<0) | |
755 | ||
756 | struct cg_auto_data { | |
757 | int num_not_in_graph; | |
758 | int limit; | |
759 | }; | |
760 | ||
761 | static int dfs_on_ref(const char *refname, | |
762 | const struct object_id *oid, int flags, | |
763 | void *cb_data) | |
764 | { | |
765 | struct cg_auto_data *data = (struct cg_auto_data *)cb_data; | |
766 | int result = 0; | |
767 | struct object_id peeled; | |
768 | struct commit_list *stack = NULL; | |
769 | struct commit *commit; | |
770 | ||
771 | if (!peel_ref(refname, &peeled)) | |
772 | oid = &peeled; | |
773 | if (oid_object_info(the_repository, oid, NULL) != OBJ_COMMIT) | |
774 | return 0; | |
775 | ||
776 | commit = lookup_commit(the_repository, oid); | |
777 | if (!commit) | |
778 | return 0; | |
779 | if (parse_commit(commit)) | |
780 | return 0; | |
781 | ||
782 | commit_list_append(commit, &stack); | |
783 | ||
784 | while (!result && stack) { | |
785 | struct commit_list *parent; | |
786 | ||
787 | commit = pop_commit(&stack); | |
788 | ||
789 | for (parent = commit->parents; parent; parent = parent->next) { | |
790 | if (parse_commit(parent->item) || | |
791 | commit_graph_position(parent->item) != COMMIT_NOT_FROM_GRAPH || | |
792 | parent->item->object.flags & SEEN) | |
793 | continue; | |
794 | ||
795 | parent->item->object.flags |= SEEN; | |
796 | data->num_not_in_graph++; | |
797 | ||
798 | if (data->num_not_in_graph >= data->limit) { | |
799 | result = 1; | |
800 | break; | |
801 | } | |
802 | ||
803 | commit_list_append(parent->item, &stack); | |
804 | } | |
805 | } | |
806 | ||
807 | free_commit_list(stack); | |
808 | return result; | |
809 | } | |
810 | ||
811 | static int should_write_commit_graph(void) | |
812 | { | |
813 | int result; | |
814 | struct cg_auto_data data; | |
815 | ||
816 | data.num_not_in_graph = 0; | |
817 | data.limit = 100; | |
818 | git_config_get_int("maintenance.commit-graph.auto", | |
819 | &data.limit); | |
820 | ||
821 | if (!data.limit) | |
822 | return 0; | |
823 | if (data.limit < 0) | |
824 | return 1; | |
825 | ||
826 | result = for_each_ref(dfs_on_ref, &data); | |
827 | ||
828 | clear_commit_marks_all(SEEN); | |
829 | ||
830 | return result; | |
831 | } | |
832 | ||
663b2b1b DS |
833 | static int run_write_commit_graph(struct maintenance_run_opts *opts) |
834 | { | |
835 | struct child_process child = CHILD_PROCESS_INIT; | |
836 | ||
837 | child.git_cmd = 1; | |
838 | strvec_pushl(&child.args, "commit-graph", "write", | |
839 | "--split", "--reachable", NULL); | |
840 | ||
841 | if (opts->quiet) | |
842 | strvec_push(&child.args, "--no-progress"); | |
843 | ||
844 | return !!run_command(&child); | |
845 | } | |
846 | ||
847 | static int maintenance_task_commit_graph(struct maintenance_run_opts *opts) | |
848 | { | |
849 | close_object_store(the_repository->objects); | |
850 | if (run_write_commit_graph(opts)) { | |
851 | error(_("failed to write commit-graph")); | |
852 | return 1; | |
853 | } | |
854 | ||
855 | return 0; | |
856 | } | |
857 | ||
28cb5e66 DS |
858 | static int fetch_remote(const char *remote, struct maintenance_run_opts *opts) |
859 | { | |
860 | struct child_process child = CHILD_PROCESS_INIT; | |
861 | ||
862 | child.git_cmd = 1; | |
863 | strvec_pushl(&child.args, "fetch", remote, "--prune", "--no-tags", | |
864 | "--no-write-fetch-head", "--recurse-submodules=no", | |
865 | "--refmap=", NULL); | |
866 | ||
867 | if (opts->quiet) | |
868 | strvec_push(&child.args, "--quiet"); | |
869 | ||
870 | strvec_pushf(&child.args, "+refs/heads/*:refs/prefetch/%s/*", remote); | |
871 | ||
872 | return !!run_command(&child); | |
873 | } | |
874 | ||
875 | static int append_remote(struct remote *remote, void *cbdata) | |
876 | { | |
877 | struct string_list *remotes = (struct string_list *)cbdata; | |
878 | ||
879 | string_list_append(remotes, remote->name); | |
880 | return 0; | |
881 | } | |
882 | ||
883 | static int maintenance_task_prefetch(struct maintenance_run_opts *opts) | |
884 | { | |
885 | int result = 0; | |
886 | struct string_list_item *item; | |
887 | struct string_list remotes = STRING_LIST_INIT_DUP; | |
888 | ||
889 | if (for_each_remote(append_remote, &remotes)) { | |
890 | error(_("failed to fill remotes")); | |
891 | result = 1; | |
892 | goto cleanup; | |
893 | } | |
894 | ||
895 | for_each_string_list_item(item, &remotes) | |
896 | result |= fetch_remote(item->string, opts); | |
897 | ||
898 | cleanup: | |
899 | string_list_clear(&remotes, 0); | |
900 | return result; | |
901 | } | |
902 | ||
2057d750 DS |
903 | static int maintenance_task_gc(struct maintenance_run_opts *opts) |
904 | { | |
905 | struct child_process child = CHILD_PROCESS_INIT; | |
906 | ||
907 | child.git_cmd = 1; | |
908 | strvec_push(&child.args, "gc"); | |
909 | ||
910 | if (opts->auto_flag) | |
911 | strvec_push(&child.args, "--auto"); | |
3ddaad0e DS |
912 | if (opts->quiet) |
913 | strvec_push(&child.args, "--quiet"); | |
914 | else | |
915 | strvec_push(&child.args, "--no-quiet"); | |
2057d750 DS |
916 | |
917 | close_object_store(the_repository->objects); | |
918 | return run_command(&child); | |
919 | } | |
920 | ||
252cfb7c DS |
921 | static int prune_packed(struct maintenance_run_opts *opts) |
922 | { | |
923 | struct child_process child = CHILD_PROCESS_INIT; | |
924 | ||
925 | child.git_cmd = 1; | |
926 | strvec_push(&child.args, "prune-packed"); | |
927 | ||
928 | if (opts->quiet) | |
929 | strvec_push(&child.args, "--quiet"); | |
930 | ||
931 | return !!run_command(&child); | |
932 | } | |
933 | ||
934 | struct write_loose_object_data { | |
935 | FILE *in; | |
936 | int count; | |
937 | int batch_size; | |
938 | }; | |
939 | ||
3e220e60 DS |
940 | static int loose_object_auto_limit = 100; |
941 | ||
942 | static int loose_object_count(const struct object_id *oid, | |
943 | const char *path, | |
944 | void *data) | |
945 | { | |
946 | int *count = (int*)data; | |
947 | if (++(*count) >= loose_object_auto_limit) | |
948 | return 1; | |
949 | return 0; | |
950 | } | |
951 | ||
952 | static int loose_object_auto_condition(void) | |
953 | { | |
954 | int count = 0; | |
955 | ||
956 | git_config_get_int("maintenance.loose-objects.auto", | |
957 | &loose_object_auto_limit); | |
958 | ||
959 | if (!loose_object_auto_limit) | |
960 | return 0; | |
961 | if (loose_object_auto_limit < 0) | |
962 | return 1; | |
963 | ||
964 | return for_each_loose_file_in_objdir(the_repository->objects->odb->path, | |
965 | loose_object_count, | |
966 | NULL, NULL, &count); | |
967 | } | |
968 | ||
252cfb7c DS |
969 | static int bail_on_loose(const struct object_id *oid, |
970 | const char *path, | |
971 | void *data) | |
972 | { | |
973 | return 1; | |
974 | } | |
975 | ||
976 | static int write_loose_object_to_stdin(const struct object_id *oid, | |
977 | const char *path, | |
978 | void *data) | |
979 | { | |
980 | struct write_loose_object_data *d = (struct write_loose_object_data *)data; | |
981 | ||
982 | fprintf(d->in, "%s\n", oid_to_hex(oid)); | |
983 | ||
984 | return ++(d->count) > d->batch_size; | |
985 | } | |
986 | ||
987 | static int pack_loose(struct maintenance_run_opts *opts) | |
988 | { | |
989 | struct repository *r = the_repository; | |
990 | int result = 0; | |
991 | struct write_loose_object_data data; | |
992 | struct child_process pack_proc = CHILD_PROCESS_INIT; | |
993 | ||
994 | /* | |
995 | * Do not start pack-objects process | |
996 | * if there are no loose objects. | |
997 | */ | |
998 | if (!for_each_loose_file_in_objdir(r->objects->odb->path, | |
999 | bail_on_loose, | |
1000 | NULL, NULL, NULL)) | |
1001 | return 0; | |
1002 | ||
1003 | pack_proc.git_cmd = 1; | |
1004 | ||
1005 | strvec_push(&pack_proc.args, "pack-objects"); | |
1006 | if (opts->quiet) | |
1007 | strvec_push(&pack_proc.args, "--quiet"); | |
1008 | strvec_pushf(&pack_proc.args, "%s/pack/loose", r->objects->odb->path); | |
1009 | ||
1010 | pack_proc.in = -1; | |
1011 | ||
1012 | if (start_command(&pack_proc)) { | |
1013 | error(_("failed to start 'git pack-objects' process")); | |
1014 | return 1; | |
1015 | } | |
1016 | ||
1017 | data.in = xfdopen(pack_proc.in, "w"); | |
1018 | data.count = 0; | |
1019 | data.batch_size = 50000; | |
1020 | ||
1021 | for_each_loose_file_in_objdir(r->objects->odb->path, | |
1022 | write_loose_object_to_stdin, | |
1023 | NULL, | |
1024 | NULL, | |
1025 | &data); | |
1026 | ||
1027 | fclose(data.in); | |
1028 | ||
1029 | if (finish_command(&pack_proc)) { | |
1030 | error(_("failed to finish 'git pack-objects' process")); | |
1031 | result = 1; | |
1032 | } | |
1033 | ||
1034 | return result; | |
1035 | } | |
1036 | ||
1037 | static int maintenance_task_loose_objects(struct maintenance_run_opts *opts) | |
1038 | { | |
1039 | return prune_packed(opts) || pack_loose(opts); | |
1040 | } | |
1041 | ||
e841a79a DS |
1042 | static int incremental_repack_auto_condition(void) |
1043 | { | |
1044 | struct packed_git *p; | |
1045 | int enabled; | |
1046 | int incremental_repack_auto_limit = 10; | |
1047 | int count = 0; | |
1048 | ||
1049 | if (git_config_get_bool("core.multiPackIndex", &enabled) || | |
1050 | !enabled) | |
1051 | return 0; | |
1052 | ||
1053 | git_config_get_int("maintenance.incremental-repack.auto", | |
1054 | &incremental_repack_auto_limit); | |
1055 | ||
1056 | if (!incremental_repack_auto_limit) | |
1057 | return 0; | |
1058 | if (incremental_repack_auto_limit < 0) | |
1059 | return 1; | |
1060 | ||
1061 | for (p = get_packed_git(the_repository); | |
1062 | count < incremental_repack_auto_limit && p; | |
1063 | p = p->next) { | |
1064 | if (!p->multi_pack_index) | |
1065 | count++; | |
1066 | } | |
1067 | ||
1068 | return count >= incremental_repack_auto_limit; | |
1069 | } | |
1070 | ||
52fe41ff DS |
1071 | static int multi_pack_index_write(struct maintenance_run_opts *opts) |
1072 | { | |
1073 | struct child_process child = CHILD_PROCESS_INIT; | |
1074 | ||
1075 | child.git_cmd = 1; | |
1076 | strvec_pushl(&child.args, "multi-pack-index", "write", NULL); | |
1077 | ||
1078 | if (opts->quiet) | |
1079 | strvec_push(&child.args, "--no-progress"); | |
1080 | ||
1081 | if (run_command(&child)) | |
1082 | return error(_("failed to write multi-pack-index")); | |
1083 | ||
1084 | return 0; | |
1085 | } | |
1086 | ||
1087 | static int multi_pack_index_expire(struct maintenance_run_opts *opts) | |
1088 | { | |
1089 | struct child_process child = CHILD_PROCESS_INIT; | |
1090 | ||
1091 | child.git_cmd = 1; | |
1092 | strvec_pushl(&child.args, "multi-pack-index", "expire", NULL); | |
1093 | ||
1094 | if (opts->quiet) | |
1095 | strvec_push(&child.args, "--no-progress"); | |
1096 | ||
1097 | close_object_store(the_repository->objects); | |
1098 | ||
1099 | if (run_command(&child)) | |
1100 | return error(_("'git multi-pack-index expire' failed")); | |
1101 | ||
1102 | return 0; | |
1103 | } | |
1104 | ||
a13e3d0e DS |
1105 | #define TWO_GIGABYTES (INT32_MAX) |
1106 | ||
1107 | static off_t get_auto_pack_size(void) | |
1108 | { | |
1109 | /* | |
1110 | * The "auto" value is special: we optimize for | |
1111 | * one large pack-file (i.e. from a clone) and | |
1112 | * expect the rest to be small and they can be | |
1113 | * repacked quickly. | |
1114 | * | |
1115 | * The strategy we select here is to select a | |
1116 | * size that is one more than the second largest | |
1117 | * pack-file. This ensures that we will repack | |
1118 | * at least two packs if there are three or more | |
1119 | * packs. | |
1120 | */ | |
1121 | off_t max_size = 0; | |
1122 | off_t second_largest_size = 0; | |
1123 | off_t result_size; | |
1124 | struct packed_git *p; | |
1125 | struct repository *r = the_repository; | |
1126 | ||
1127 | reprepare_packed_git(r); | |
1128 | for (p = get_all_packs(r); p; p = p->next) { | |
1129 | if (p->pack_size > max_size) { | |
1130 | second_largest_size = max_size; | |
1131 | max_size = p->pack_size; | |
1132 | } else if (p->pack_size > second_largest_size) | |
1133 | second_largest_size = p->pack_size; | |
1134 | } | |
1135 | ||
1136 | result_size = second_largest_size + 1; | |
1137 | ||
1138 | /* But limit ourselves to a batch size of 2g */ | |
1139 | if (result_size > TWO_GIGABYTES) | |
1140 | result_size = TWO_GIGABYTES; | |
1141 | ||
1142 | return result_size; | |
1143 | } | |
1144 | ||
52fe41ff DS |
1145 | static int multi_pack_index_repack(struct maintenance_run_opts *opts) |
1146 | { | |
1147 | struct child_process child = CHILD_PROCESS_INIT; | |
1148 | ||
1149 | child.git_cmd = 1; | |
1150 | strvec_pushl(&child.args, "multi-pack-index", "repack", NULL); | |
1151 | ||
1152 | if (opts->quiet) | |
1153 | strvec_push(&child.args, "--no-progress"); | |
1154 | ||
a13e3d0e DS |
1155 | strvec_pushf(&child.args, "--batch-size=%"PRIuMAX, |
1156 | (uintmax_t)get_auto_pack_size()); | |
52fe41ff DS |
1157 | |
1158 | close_object_store(the_repository->objects); | |
1159 | ||
1160 | if (run_command(&child)) | |
1161 | return error(_("'git multi-pack-index repack' failed")); | |
1162 | ||
1163 | return 0; | |
1164 | } | |
1165 | ||
1166 | static int maintenance_task_incremental_repack(struct maintenance_run_opts *opts) | |
1167 | { | |
1168 | prepare_repo_settings(the_repository); | |
1169 | if (!the_repository->settings.core_multi_pack_index) { | |
1170 | warning(_("skipping incremental-repack task because core.multiPackIndex is disabled")); | |
1171 | return 0; | |
1172 | } | |
1173 | ||
1174 | if (multi_pack_index_write(opts)) | |
1175 | return 1; | |
1176 | if (multi_pack_index_expire(opts)) | |
1177 | return 1; | |
1178 | if (multi_pack_index_repack(opts)) | |
1179 | return 1; | |
1180 | return 0; | |
1181 | } | |
1182 | ||
3103e984 DS |
1183 | typedef int maintenance_task_fn(struct maintenance_run_opts *opts); |
1184 | ||
916d0626 DS |
1185 | /* |
1186 | * An auto condition function returns 1 if the task should run | |
1187 | * and 0 if the task should NOT run. See needs_to_gc() for an | |
1188 | * example. | |
1189 | */ | |
1190 | typedef int maintenance_auto_fn(void); | |
1191 | ||
3103e984 DS |
1192 | struct maintenance_task { |
1193 | const char *name; | |
1194 | maintenance_task_fn *fn; | |
916d0626 | 1195 | maintenance_auto_fn *auto_condition; |
3103e984 | 1196 | unsigned enabled:1; |
090511bc | 1197 | |
b08ff1fe DS |
1198 | enum schedule_priority schedule; |
1199 | ||
090511bc DS |
1200 | /* -1 if not selected. */ |
1201 | int selected_order; | |
3103e984 DS |
1202 | }; |
1203 | ||
1204 | enum maintenance_task_label { | |
28cb5e66 | 1205 | TASK_PREFETCH, |
252cfb7c | 1206 | TASK_LOOSE_OBJECTS, |
52fe41ff | 1207 | TASK_INCREMENTAL_REPACK, |
3103e984 | 1208 | TASK_GC, |
663b2b1b | 1209 | TASK_COMMIT_GRAPH, |
3103e984 DS |
1210 | |
1211 | /* Leave as final value */ | |
1212 | TASK__COUNT | |
1213 | }; | |
1214 | ||
1215 | static struct maintenance_task tasks[] = { | |
28cb5e66 DS |
1216 | [TASK_PREFETCH] = { |
1217 | "prefetch", | |
1218 | maintenance_task_prefetch, | |
1219 | }, | |
252cfb7c DS |
1220 | [TASK_LOOSE_OBJECTS] = { |
1221 | "loose-objects", | |
1222 | maintenance_task_loose_objects, | |
3e220e60 | 1223 | loose_object_auto_condition, |
252cfb7c | 1224 | }, |
52fe41ff DS |
1225 | [TASK_INCREMENTAL_REPACK] = { |
1226 | "incremental-repack", | |
1227 | maintenance_task_incremental_repack, | |
e841a79a | 1228 | incremental_repack_auto_condition, |
52fe41ff | 1229 | }, |
3103e984 DS |
1230 | [TASK_GC] = { |
1231 | "gc", | |
1232 | maintenance_task_gc, | |
916d0626 | 1233 | need_to_gc, |
3103e984 DS |
1234 | 1, |
1235 | }, | |
663b2b1b DS |
1236 | [TASK_COMMIT_GRAPH] = { |
1237 | "commit-graph", | |
1238 | maintenance_task_commit_graph, | |
4ddc79b2 | 1239 | should_write_commit_graph, |
663b2b1b | 1240 | }, |
3103e984 DS |
1241 | }; |
1242 | ||
090511bc DS |
1243 | static int compare_tasks_by_selection(const void *a_, const void *b_) |
1244 | { | |
1245 | const struct maintenance_task *a, *b; | |
1246 | ||
1247 | a = (const struct maintenance_task *)&a_; | |
1248 | b = (const struct maintenance_task *)&b_; | |
1249 | ||
1250 | return b->selected_order - a->selected_order; | |
1251 | } | |
1252 | ||
3103e984 DS |
1253 | static int maintenance_run_tasks(struct maintenance_run_opts *opts) |
1254 | { | |
090511bc | 1255 | int i, found_selected = 0; |
3103e984 | 1256 | int result = 0; |
d7514f6e DS |
1257 | struct lock_file lk; |
1258 | struct repository *r = the_repository; | |
1259 | char *lock_path = xstrfmt("%s/maintenance", r->objects->odb->path); | |
1260 | ||
1261 | if (hold_lock_file_for_update(&lk, lock_path, LOCK_NO_DEREF) < 0) { | |
1262 | /* | |
1263 | * Another maintenance command is running. | |
1264 | * | |
1265 | * If --auto was provided, then it is likely due to a | |
1266 | * recursive process stack. Do not report an error in | |
1267 | * that case. | |
1268 | */ | |
1269 | if (!opts->auto_flag && !opts->quiet) | |
1270 | warning(_("lock file '%s' exists, skipping maintenance"), | |
1271 | lock_path); | |
1272 | free(lock_path); | |
1273 | return 0; | |
1274 | } | |
1275 | free(lock_path); | |
3103e984 | 1276 | |
090511bc DS |
1277 | for (i = 0; !found_selected && i < TASK__COUNT; i++) |
1278 | found_selected = tasks[i].selected_order >= 0; | |
1279 | ||
1280 | if (found_selected) | |
1281 | QSORT(tasks, TASK__COUNT, compare_tasks_by_selection); | |
1282 | ||
3103e984 | 1283 | for (i = 0; i < TASK__COUNT; i++) { |
090511bc DS |
1284 | if (found_selected && tasks[i].selected_order < 0) |
1285 | continue; | |
1286 | ||
1287 | if (!found_selected && !tasks[i].enabled) | |
3103e984 DS |
1288 | continue; |
1289 | ||
916d0626 DS |
1290 | if (opts->auto_flag && |
1291 | (!tasks[i].auto_condition || | |
1292 | !tasks[i].auto_condition())) | |
1293 | continue; | |
1294 | ||
b08ff1fe DS |
1295 | if (opts->schedule && tasks[i].schedule < opts->schedule) |
1296 | continue; | |
1297 | ||
25914c4f | 1298 | trace2_region_enter("maintenance", tasks[i].name, r); |
3103e984 DS |
1299 | if (tasks[i].fn(opts)) { |
1300 | error(_("task '%s' failed"), tasks[i].name); | |
1301 | result = 1; | |
1302 | } | |
25914c4f | 1303 | trace2_region_leave("maintenance", tasks[i].name, r); |
3103e984 DS |
1304 | } |
1305 | ||
d7514f6e | 1306 | rollback_lock_file(&lk); |
3103e984 DS |
1307 | return result; |
1308 | } | |
1309 | ||
65d655b5 DS |
1310 | static void initialize_task_config(void) |
1311 | { | |
1312 | int i; | |
1313 | struct strbuf config_name = STRBUF_INIT; | |
916d0626 DS |
1314 | gc_config(); |
1315 | ||
65d655b5 DS |
1316 | for (i = 0; i < TASK__COUNT; i++) { |
1317 | int config_value; | |
b08ff1fe | 1318 | char *config_str; |
65d655b5 | 1319 | |
b08ff1fe | 1320 | strbuf_reset(&config_name); |
65d655b5 DS |
1321 | strbuf_addf(&config_name, "maintenance.%s.enabled", |
1322 | tasks[i].name); | |
1323 | ||
1324 | if (!git_config_get_bool(config_name.buf, &config_value)) | |
1325 | tasks[i].enabled = config_value; | |
b08ff1fe DS |
1326 | |
1327 | strbuf_reset(&config_name); | |
1328 | strbuf_addf(&config_name, "maintenance.%s.schedule", | |
1329 | tasks[i].name); | |
1330 | ||
1331 | if (!git_config_get_string(config_name.buf, &config_str)) { | |
1332 | tasks[i].schedule = parse_schedule(config_str); | |
1333 | free(config_str); | |
1334 | } | |
65d655b5 DS |
1335 | } |
1336 | ||
1337 | strbuf_release(&config_name); | |
1338 | } | |
1339 | ||
090511bc DS |
1340 | static int task_option_parse(const struct option *opt, |
1341 | const char *arg, int unset) | |
1342 | { | |
1343 | int i, num_selected = 0; | |
1344 | struct maintenance_task *task = NULL; | |
1345 | ||
1346 | BUG_ON_OPT_NEG(unset); | |
1347 | ||
1348 | for (i = 0; i < TASK__COUNT; i++) { | |
1349 | if (tasks[i].selected_order >= 0) | |
1350 | num_selected++; | |
1351 | if (!strcasecmp(tasks[i].name, arg)) { | |
1352 | task = &tasks[i]; | |
1353 | } | |
1354 | } | |
1355 | ||
1356 | if (!task) { | |
1357 | error(_("'%s' is not a valid task"), arg); | |
1358 | return 1; | |
1359 | } | |
1360 | ||
1361 | if (task->selected_order >= 0) { | |
1362 | error(_("task '%s' cannot be selected multiple times"), arg); | |
1363 | return 1; | |
1364 | } | |
1365 | ||
1366 | task->selected_order = num_selected + 1; | |
1367 | ||
1368 | return 0; | |
1369 | } | |
1370 | ||
2057d750 DS |
1371 | static int maintenance_run(int argc, const char **argv, const char *prefix) |
1372 | { | |
090511bc | 1373 | int i; |
2057d750 DS |
1374 | struct maintenance_run_opts opts; |
1375 | struct option builtin_maintenance_run_options[] = { | |
1376 | OPT_BOOL(0, "auto", &opts.auto_flag, | |
1377 | N_("run tasks based on the state of the repository")), | |
b08ff1fe DS |
1378 | OPT_CALLBACK(0, "schedule", &opts.schedule, N_("frequency"), |
1379 | N_("run tasks based on frequency"), | |
1380 | maintenance_opt_schedule), | |
3ddaad0e DS |
1381 | OPT_BOOL(0, "quiet", &opts.quiet, |
1382 | N_("do not report progress or other information over stderr")), | |
090511bc DS |
1383 | OPT_CALLBACK_F(0, "task", NULL, N_("task"), |
1384 | N_("run a specific task"), | |
1385 | PARSE_OPT_NONEG, task_option_parse), | |
2057d750 DS |
1386 | OPT_END() |
1387 | }; | |
1388 | memset(&opts, 0, sizeof(opts)); | |
1389 | ||
3ddaad0e | 1390 | opts.quiet = !isatty(2); |
65d655b5 | 1391 | initialize_task_config(); |
3ddaad0e | 1392 | |
090511bc DS |
1393 | for (i = 0; i < TASK__COUNT; i++) |
1394 | tasks[i].selected_order = -1; | |
1395 | ||
2057d750 DS |
1396 | argc = parse_options(argc, argv, prefix, |
1397 | builtin_maintenance_run_options, | |
1398 | builtin_maintenance_run_usage, | |
1399 | PARSE_OPT_STOP_AT_NON_OPTION); | |
1400 | ||
b08ff1fe DS |
1401 | if (opts.auto_flag && opts.schedule) |
1402 | die(_("use at most one of --auto and --schedule=<frequency>")); | |
1403 | ||
2057d750 DS |
1404 | if (argc != 0) |
1405 | usage_with_options(builtin_maintenance_run_usage, | |
1406 | builtin_maintenance_run_options); | |
3103e984 | 1407 | return maintenance_run_tasks(&opts); |
2057d750 DS |
1408 | } |
1409 | ||
1410 | static const char builtin_maintenance_usage[] = N_("git maintenance run [<options>]"); | |
1411 | ||
1412 | int cmd_maintenance(int argc, const char **argv, const char *prefix) | |
1413 | { | |
1414 | if (argc < 2 || | |
1415 | (argc == 2 && !strcmp(argv[1], "-h"))) | |
1416 | usage(builtin_maintenance_usage); | |
1417 | ||
1418 | if (!strcmp(argv[1], "run")) | |
1419 | return maintenance_run(argc - 1, argv + 1, prefix); | |
1420 | ||
1421 | die(_("invalid subcommand: %s"), argv[1]); | |
1422 | } |