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