]>
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" |
234587fc | 21 | #include "argv-array.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" | |
6757ada4 JB |
30 | |
31 | #define FAILED_RUN "failed to run %s" | |
32 | ||
44c637c8 | 33 | static const char * const builtin_gc_usage[] = { |
9c9b4f2f | 34 | N_("git gc [<options>]"), |
44c637c8 JB |
35 | NULL |
36 | }; | |
6757ada4 | 37 | |
56752391 | 38 | static int pack_refs = 1; |
62aad184 | 39 | static int prune_reflogs = 1; |
07e7dbf0 | 40 | static int aggressive_depth = 50; |
1c192f34 | 41 | static int aggressive_window = 250; |
2c3c4399 | 42 | static int gc_auto_threshold = 6700; |
97063974 | 43 | static int gc_auto_pack_limit = 50; |
d5d5d7b6 | 44 | static int gc_write_commit_graph; |
9f673f94 | 45 | static int detach_auto = 1; |
dddbad72 | 46 | static timestamp_t gc_log_expire_time; |
a831c06a | 47 | static const char *gc_log_expire = "1.day.ago"; |
d3154b44 | 48 | static const char *prune_expire = "2.weeks.ago"; |
e3df33bb | 49 | static const char *prune_worktrees_expire = "3.months.ago"; |
55dfe13d | 50 | static unsigned long big_pack_threshold; |
9806f5a7 | 51 | static unsigned long max_delta_cache_size = DEFAULT_DELTA_CACHE_SIZE; |
6757ada4 | 52 | |
234587fc JK |
53 | static struct argv_array pack_refs_cmd = ARGV_ARRAY_INIT; |
54 | static struct argv_array reflog = ARGV_ARRAY_INIT; | |
55 | static struct argv_array repack = ARGV_ARRAY_INIT; | |
56 | static struct argv_array prune = ARGV_ARRAY_INIT; | |
e3df33bb | 57 | static struct argv_array prune_worktrees = ARGV_ARRAY_INIT; |
234587fc | 58 | static struct argv_array rerere = ARGV_ARRAY_INIT; |
6757ada4 | 59 | |
076aa2cb | 60 | static struct tempfile *pidfile; |
329e6e87 | 61 | static struct lock_file log_lock; |
4c5baf02 | 62 | |
478f34d2 DK |
63 | static struct string_list pack_garbage = STRING_LIST_INIT_DUP; |
64 | ||
65 | static void clean_pack_garbage(void) | |
66 | { | |
67 | int i; | |
68 | for (i = 0; i < pack_garbage.nr; i++) | |
69 | unlink_or_warn(pack_garbage.items[i].string); | |
70 | string_list_clear(&pack_garbage, 0); | |
71 | } | |
72 | ||
73 | static void report_pack_garbage(unsigned seen_bits, const char *path) | |
74 | { | |
75 | if (seen_bits == PACKDIR_FILE_IDX) | |
76 | string_list_append(&pack_garbage, path); | |
77 | } | |
78 | ||
329e6e87 NTND |
79 | static void process_log_file(void) |
80 | { | |
81 | struct stat st; | |
a831c06a DT |
82 | if (fstat(get_lock_file_fd(&log_lock), &st)) { |
83 | /* | |
84 | * Perhaps there was an i/o error or another | |
85 | * unlikely situation. Try to make a note of | |
86 | * this in gc.log along with any existing | |
87 | * messages. | |
88 | */ | |
89 | int saved_errno = errno; | |
90 | fprintf(stderr, _("Failed to fstat %s: %s"), | |
076aa2cb | 91 | get_tempfile_path(log_lock.tempfile), |
a831c06a DT |
92 | strerror(saved_errno)); |
93 | fflush(stderr); | |
329e6e87 | 94 | commit_lock_file(&log_lock); |
a831c06a DT |
95 | errno = saved_errno; |
96 | } else if (st.st_size) { | |
97 | /* There was some error recorded in the lock file */ | |
98 | commit_lock_file(&log_lock); | |
99 | } else { | |
100 | /* No error, clean up any old gc.log */ | |
101 | unlink(git_path("gc.log")); | |
329e6e87 | 102 | rollback_lock_file(&log_lock); |
a831c06a | 103 | } |
329e6e87 NTND |
104 | } |
105 | ||
106 | static void process_log_file_at_exit(void) | |
107 | { | |
108 | fflush(stderr); | |
109 | process_log_file(); | |
110 | } | |
111 | ||
112 | static void process_log_file_on_signal(int signo) | |
113 | { | |
114 | process_log_file(); | |
115 | sigchain_pop(signo); | |
116 | raise(signo); | |
117 | } | |
118 | ||
5801d3b4 | 119 | static void gc_config(void) |
6757ada4 | 120 | { |
5801d3b4 TA |
121 | const char *value; |
122 | ||
123 | if (!git_config_get_value("gc.packrefs", &value)) { | |
c5e5a2c0 | 124 | if (value && !strcmp(value, "notbare")) |
6757ada4 JB |
125 | pack_refs = -1; |
126 | else | |
5801d3b4 | 127 | pack_refs = git_config_bool("gc.packrefs", value); |
17815501 | 128 | } |
5801d3b4 TA |
129 | |
130 | git_config_get_int("gc.aggressivewindow", &aggressive_window); | |
131 | git_config_get_int("gc.aggressivedepth", &aggressive_depth); | |
132 | git_config_get_int("gc.auto", &gc_auto_threshold); | |
133 | git_config_get_int("gc.autopacklimit", &gc_auto_pack_limit); | |
d5d5d7b6 | 134 | git_config_get_bool("gc.writecommitgraph", &gc_write_commit_graph); |
5801d3b4 | 135 | git_config_get_bool("gc.autodetach", &detach_auto); |
77d67977 CC |
136 | git_config_get_expiry("gc.pruneexpire", &prune_expire); |
137 | git_config_get_expiry("gc.worktreepruneexpire", &prune_worktrees_expire); | |
94c9b5af | 138 | git_config_get_expiry("gc.logexpiry", &gc_log_expire); |
a831c06a | 139 | |
55dfe13d | 140 | git_config_get_ulong("gc.bigpackthreshold", &big_pack_threshold); |
9806f5a7 | 141 | git_config_get_ulong("pack.deltacachesize", &max_delta_cache_size); |
55dfe13d | 142 | |
5801d3b4 | 143 | git_config(git_default_config, NULL); |
6757ada4 JB |
144 | } |
145 | ||
a087cc98 | 146 | static int too_many_loose_objects(void) |
2c3c4399 JH |
147 | { |
148 | /* | |
149 | * Quickly check if a "gc" is needed, by estimating how | |
150 | * many loose objects there are. Because SHA-1 is evenly | |
151 | * distributed, we can check only one and get a reasonable | |
152 | * estimate. | |
153 | */ | |
2c3c4399 JH |
154 | DIR *dir; |
155 | struct dirent *ent; | |
156 | int auto_threshold; | |
157 | int num_loose = 0; | |
158 | int needed = 0; | |
159 | ||
17815501 JH |
160 | if (gc_auto_threshold <= 0) |
161 | return 0; | |
162 | ||
07af8891 | 163 | dir = opendir(git_path("objects/17")); |
2c3c4399 JH |
164 | if (!dir) |
165 | return 0; | |
166 | ||
42c78a21 | 167 | auto_threshold = DIV_ROUND_UP(gc_auto_threshold, 256); |
2c3c4399 JH |
168 | while ((ent = readdir(dir)) != NULL) { |
169 | if (strspn(ent->d_name, "0123456789abcdef") != 38 || | |
170 | ent->d_name[38] != '\0') | |
171 | continue; | |
172 | if (++num_loose > auto_threshold) { | |
173 | needed = 1; | |
174 | break; | |
175 | } | |
176 | } | |
177 | closedir(dir); | |
178 | return needed; | |
179 | } | |
180 | ||
9806f5a7 NTND |
181 | static struct packed_git *find_base_packs(struct string_list *packs, |
182 | unsigned long limit) | |
ae4e89e5 NTND |
183 | { |
184 | struct packed_git *p, *base = NULL; | |
185 | ||
454ea2e4 | 186 | for (p = get_all_packs(the_repository); p; p = p->next) { |
ae4e89e5 NTND |
187 | if (!p->pack_local) |
188 | continue; | |
55dfe13d NTND |
189 | if (limit) { |
190 | if (p->pack_size >= limit) | |
191 | string_list_append(packs, p->pack_name); | |
192 | } else if (!base || base->pack_size < p->pack_size) { | |
ae4e89e5 NTND |
193 | base = p; |
194 | } | |
195 | } | |
196 | ||
197 | if (base) | |
198 | string_list_append(packs, base->pack_name); | |
9806f5a7 NTND |
199 | |
200 | return base; | |
ae4e89e5 NTND |
201 | } |
202 | ||
17815501 JH |
203 | static int too_many_packs(void) |
204 | { | |
205 | struct packed_git *p; | |
206 | int cnt; | |
207 | ||
208 | if (gc_auto_pack_limit <= 0) | |
209 | return 0; | |
210 | ||
454ea2e4 | 211 | for (cnt = 0, p = get_all_packs(the_repository); p; p = p->next) { |
17815501 JH |
212 | if (!p->pack_local) |
213 | continue; | |
01af249f | 214 | if (p->pack_keep) |
17815501 JH |
215 | continue; |
216 | /* | |
217 | * Perhaps check the size of the pack and count only | |
218 | * very small ones here? | |
219 | */ | |
220 | cnt++; | |
221 | } | |
5f4e3bf5 | 222 | return gc_auto_pack_limit < cnt; |
17815501 JH |
223 | } |
224 | ||
9806f5a7 NTND |
225 | static uint64_t total_ram(void) |
226 | { | |
227 | #if defined(HAVE_SYSINFO) | |
228 | struct sysinfo si; | |
229 | ||
230 | if (!sysinfo(&si)) | |
231 | return si.totalram; | |
232 | #elif defined(HAVE_BSD_SYSCTL) && (defined(HW_MEMSIZE) || defined(HW_PHYSMEM)) | |
233 | int64_t physical_memory; | |
234 | int mib[2]; | |
235 | size_t length; | |
236 | ||
237 | mib[0] = CTL_HW; | |
238 | # if defined(HW_MEMSIZE) | |
239 | mib[1] = HW_MEMSIZE; | |
240 | # else | |
241 | mib[1] = HW_PHYSMEM; | |
242 | # endif | |
243 | length = sizeof(int64_t); | |
244 | if (!sysctl(mib, 2, &physical_memory, &length, NULL, 0)) | |
245 | return physical_memory; | |
246 | #elif defined(GIT_WINDOWS_NATIVE) | |
247 | MEMORYSTATUSEX memInfo; | |
248 | ||
249 | memInfo.dwLength = sizeof(MEMORYSTATUSEX); | |
250 | if (GlobalMemoryStatusEx(&memInfo)) | |
251 | return memInfo.ullTotalPhys; | |
252 | #endif | |
253 | return 0; | |
254 | } | |
255 | ||
256 | static uint64_t estimate_repack_memory(struct packed_git *pack) | |
257 | { | |
258 | unsigned long nr_objects = approximate_object_count(); | |
259 | size_t os_cache, heap; | |
260 | ||
261 | if (!pack || !nr_objects) | |
262 | return 0; | |
263 | ||
264 | /* | |
265 | * First we have to scan through at least one pack. | |
266 | * Assume enough room in OS file cache to keep the entire pack | |
267 | * or we may accidentally evict data of other processes from | |
268 | * the cache. | |
269 | */ | |
270 | os_cache = pack->pack_size + pack->index_size; | |
271 | /* then pack-objects needs lots more for book keeping */ | |
272 | heap = sizeof(struct object_entry) * nr_objects; | |
273 | /* | |
274 | * internal rev-list --all --objects takes up some memory too, | |
275 | * let's say half of it is for blobs | |
276 | */ | |
277 | heap += sizeof(struct blob) * nr_objects / 2; | |
278 | /* | |
279 | * and the other half is for trees (commits and tags are | |
280 | * usually insignificant) | |
281 | */ | |
282 | heap += sizeof(struct tree) * nr_objects / 2; | |
283 | /* and then obj_hash[], underestimated in fact */ | |
284 | heap += sizeof(struct object *) * nr_objects; | |
285 | /* revindex is used also */ | |
286 | heap += sizeof(struct revindex_entry) * nr_objects; | |
287 | /* | |
288 | * read_sha1_file() (either at delta calculation phase, or | |
289 | * writing phase) also fills up the delta base cache | |
290 | */ | |
291 | heap += delta_base_cache_limit; | |
292 | /* and of course pack-objects has its own delta cache */ | |
293 | heap += max_delta_cache_size; | |
294 | ||
295 | return os_cache + heap; | |
296 | } | |
297 | ||
ae4e89e5 NTND |
298 | static int keep_one_pack(struct string_list_item *item, void *data) |
299 | { | |
300 | argv_array_pushf(&repack, "--keep-pack=%s", basename(item->string)); | |
301 | return 0; | |
302 | } | |
303 | ||
304 | static void add_repack_all_option(struct string_list *keep_pack) | |
7e52f566 JK |
305 | { |
306 | if (prune_expire && !strcmp(prune_expire, "now")) | |
234587fc | 307 | argv_array_push(&repack, "-a"); |
7e52f566 | 308 | else { |
234587fc JK |
309 | argv_array_push(&repack, "-A"); |
310 | if (prune_expire) | |
311 | argv_array_pushf(&repack, "--unpack-unreachable=%s", prune_expire); | |
7e52f566 | 312 | } |
ae4e89e5 NTND |
313 | |
314 | if (keep_pack) | |
315 | for_each_string_list(keep_pack, keep_one_pack, NULL); | |
7e52f566 JK |
316 | } |
317 | ||
bdf56de8 DT |
318 | static void add_repack_incremental_option(void) |
319 | { | |
320 | argv_array_push(&repack, "--no-write-bitmap-index"); | |
321 | } | |
322 | ||
a087cc98 JH |
323 | static int need_to_gc(void) |
324 | { | |
325 | /* | |
b14d255b BC |
326 | * Setting gc.auto to 0 or negative can disable the |
327 | * automatic gc. | |
a087cc98 | 328 | */ |
b14d255b | 329 | if (gc_auto_threshold <= 0) |
95143f9e JH |
330 | return 0; |
331 | ||
17815501 JH |
332 | /* |
333 | * If there are too many loose objects, but not too many | |
334 | * packs, we run "repack -d -l". If there are too many packs, | |
335 | * we run "repack -A -d -l". Otherwise we tell the caller | |
336 | * there is no need. | |
337 | */ | |
55dfe13d NTND |
338 | if (too_many_packs()) { |
339 | struct string_list keep_pack = STRING_LIST_INIT_NODUP; | |
340 | ||
8fc67762 | 341 | if (big_pack_threshold) { |
55dfe13d | 342 | find_base_packs(&keep_pack, big_pack_threshold); |
8fc67762 NTND |
343 | if (keep_pack.nr >= gc_auto_pack_limit) { |
344 | big_pack_threshold = 0; | |
345 | string_list_clear(&keep_pack, 0); | |
346 | find_base_packs(&keep_pack, 0); | |
347 | } | |
9806f5a7 NTND |
348 | } else { |
349 | struct packed_git *p = find_base_packs(&keep_pack, 0); | |
350 | uint64_t mem_have, mem_want; | |
351 | ||
352 | mem_have = total_ram(); | |
353 | mem_want = estimate_repack_memory(p); | |
354 | ||
355 | /* | |
356 | * Only allow 1/2 of memory for pack-objects, leave | |
357 | * the rest for the OS and other processes in the | |
358 | * system. | |
359 | */ | |
360 | if (!mem_have || mem_want < mem_have / 2) | |
361 | string_list_clear(&keep_pack, 0); | |
8fc67762 | 362 | } |
55dfe13d NTND |
363 | |
364 | add_repack_all_option(&keep_pack); | |
365 | string_list_clear(&keep_pack, 0); | |
366 | } else if (too_many_loose_objects()) | |
bdf56de8 DT |
367 | add_repack_incremental_option(); |
368 | else | |
17815501 | 369 | return 0; |
bde30540 | 370 | |
15048f8a | 371 | if (run_hook_le(NULL, "pre-auto-gc", NULL)) |
bde30540 | 372 | return 0; |
95143f9e | 373 | return 1; |
a087cc98 JH |
374 | } |
375 | ||
64a99eb4 NTND |
376 | /* return NULL on success, else hostname running the gc */ |
377 | static const char *lock_repo_for_gc(int force, pid_t* ret_pid) | |
378 | { | |
b2275868 | 379 | struct lock_file lock = LOCK_INIT; |
da25bdb7 | 380 | char my_host[HOST_NAME_MAX + 1]; |
64a99eb4 NTND |
381 | struct strbuf sb = STRBUF_INIT; |
382 | struct stat st; | |
383 | uintmax_t pid; | |
384 | FILE *fp; | |
4f1c0b21 | 385 | int fd; |
00539cef | 386 | char *pidfile_path; |
64a99eb4 | 387 | |
076aa2cb | 388 | if (is_tempfile_active(pidfile)) |
4c5baf02 JN |
389 | /* already locked */ |
390 | return NULL; | |
391 | ||
5781a9a2 | 392 | if (xgethostname(my_host, sizeof(my_host))) |
5096d490 | 393 | xsnprintf(my_host, sizeof(my_host), "unknown"); |
64a99eb4 | 394 | |
00539cef MH |
395 | pidfile_path = git_pathdup("gc.pid"); |
396 | fd = hold_lock_file_for_update(&lock, pidfile_path, | |
64a99eb4 NTND |
397 | LOCK_DIE_ON_ERROR); |
398 | if (!force) { | |
da25bdb7 RS |
399 | static char locking_host[HOST_NAME_MAX + 1]; |
400 | static char *scan_fmt; | |
4f1c0b21 | 401 | int should_exit; |
da25bdb7 RS |
402 | |
403 | if (!scan_fmt) | |
afe2fab7 | 404 | scan_fmt = xstrfmt("%s %%%ds", "%"SCNuMAX, HOST_NAME_MAX); |
00539cef | 405 | fp = fopen(pidfile_path, "r"); |
64a99eb4 NTND |
406 | memset(locking_host, 0, sizeof(locking_host)); |
407 | should_exit = | |
408 | fp != NULL && | |
409 | !fstat(fileno(fp), &st) && | |
410 | /* | |
411 | * 12 hour limit is very generous as gc should | |
412 | * never take that long. On the other hand we | |
413 | * don't really need a strict limit here, | |
414 | * running gc --auto one day late is not a big | |
415 | * problem. --force can be used in manual gc | |
416 | * after the user verifies that no gc is | |
417 | * running. | |
418 | */ | |
419 | time(NULL) - st.st_mtime <= 12 * 3600 && | |
da25bdb7 | 420 | fscanf(fp, scan_fmt, &pid, locking_host) == 2 && |
64a99eb4 | 421 | /* be gentle to concurrent "gc" on remote hosts */ |
ed7eda8b | 422 | (strcmp(locking_host, my_host) || !kill(pid, 0) || errno == EPERM); |
64a99eb4 NTND |
423 | if (fp != NULL) |
424 | fclose(fp); | |
425 | if (should_exit) { | |
426 | if (fd >= 0) | |
427 | rollback_lock_file(&lock); | |
428 | *ret_pid = pid; | |
00539cef | 429 | free(pidfile_path); |
64a99eb4 NTND |
430 | return locking_host; |
431 | } | |
432 | } | |
433 | ||
434 | strbuf_addf(&sb, "%"PRIuMAX" %s", | |
435 | (uintmax_t) getpid(), my_host); | |
436 | write_in_full(fd, sb.buf, sb.len); | |
437 | strbuf_release(&sb); | |
438 | commit_lock_file(&lock); | |
076aa2cb | 439 | pidfile = register_tempfile(pidfile_path); |
ebebeaea | 440 | free(pidfile_path); |
64a99eb4 NTND |
441 | return NULL; |
442 | } | |
443 | ||
30299702 JN |
444 | /* |
445 | * Returns 0 if there was no previous error and gc can proceed, 1 if | |
446 | * gc should not proceed due to an error in the last run. Prints a | |
447 | * message and returns -1 if an error occured while reading gc.log | |
448 | */ | |
449 | static int report_last_gc_error(void) | |
329e6e87 NTND |
450 | { |
451 | struct strbuf sb = STRBUF_INIT; | |
30299702 | 452 | int ret = 0; |
3c426ecc | 453 | ssize_t len; |
a831c06a DT |
454 | struct stat st; |
455 | char *gc_log_path = git_pathdup("gc.log"); | |
329e6e87 | 456 | |
a831c06a DT |
457 | if (stat(gc_log_path, &st)) { |
458 | if (errno == ENOENT) | |
459 | goto done; | |
460 | ||
30299702 JN |
461 | ret = error_errno(_("cannot stat '%s'"), gc_log_path); |
462 | goto done; | |
a831c06a DT |
463 | } |
464 | ||
465 | if (st.st_mtime < gc_log_expire_time) | |
466 | goto done; | |
467 | ||
3c426ecc JN |
468 | len = strbuf_read_file(&sb, gc_log_path, 0); |
469 | if (len < 0) | |
30299702 JN |
470 | ret = error_errno(_("cannot read '%s'"), gc_log_path); |
471 | else if (len > 0) { | |
472 | /* | |
473 | * A previous gc failed. Report the error, and don't | |
474 | * bother with an automatic gc run since it is likely | |
475 | * to fail in the same way. | |
476 | */ | |
477 | warning(_("The last gc run reported the following. " | |
329e6e87 NTND |
478 | "Please correct the root cause\n" |
479 | "and remove %s.\n" | |
480 | "Automatic cleanup will not be performed " | |
481 | "until the file is removed.\n\n" | |
482 | "%s"), | |
a831c06a | 483 | gc_log_path, sb.buf); |
30299702 JN |
484 | ret = 1; |
485 | } | |
329e6e87 | 486 | strbuf_release(&sb); |
a831c06a DT |
487 | done: |
488 | free(gc_log_path); | |
30299702 | 489 | return ret; |
329e6e87 NTND |
490 | } |
491 | ||
fec2ed21 | 492 | static void gc_before_repack(void) |
62aad184 NTND |
493 | { |
494 | if (pack_refs && run_command_v_opt(pack_refs_cmd.argv, RUN_GIT_CMD)) | |
fec2ed21 | 495 | die(FAILED_RUN, pack_refs_cmd.argv[0]); |
62aad184 NTND |
496 | |
497 | if (prune_reflogs && run_command_v_opt(reflog.argv, RUN_GIT_CMD)) | |
fec2ed21 | 498 | die(FAILED_RUN, reflog.argv[0]); |
62aad184 NTND |
499 | |
500 | pack_refs = 0; | |
501 | prune_reflogs = 0; | |
62aad184 NTND |
502 | } |
503 | ||
6757ada4 JB |
504 | int cmd_gc(int argc, const char **argv, const char *prefix) |
505 | { | |
44c637c8 | 506 | int aggressive = 0; |
2c3c4399 | 507 | int auto_gc = 0; |
a0c14cbb | 508 | int quiet = 0; |
64a99eb4 NTND |
509 | int force = 0; |
510 | const char *name; | |
511 | pid_t pid; | |
329e6e87 | 512 | int daemonized = 0; |
ae4e89e5 | 513 | int keep_base_pack = -1; |
8ab5aa4b | 514 | timestamp_t dummy; |
6757ada4 | 515 | |
44c637c8 | 516 | struct option builtin_gc_options[] = { |
6705c162 NTND |
517 | OPT__QUIET(&quiet, N_("suppress progress reporting")), |
518 | { OPTION_STRING, 0, "prune", &prune_expire, N_("date"), | |
519 | N_("prune unreferenced objects"), | |
58e9d9d4 | 520 | PARSE_OPT_OPTARG, NULL, (intptr_t)prune_expire }, |
d5d09d47 | 521 | OPT_BOOL(0, "aggressive", &aggressive, N_("be more thorough (increased runtime)")), |
7e1eeaa4 NTND |
522 | OPT_BOOL_F(0, "auto", &auto_gc, N_("enable auto-gc mode"), |
523 | PARSE_OPT_NOCOMPLETE), | |
524 | OPT_BOOL_F(0, "force", &force, | |
525 | N_("force running gc even if there may be another gc running"), | |
526 | PARSE_OPT_NOCOMPLETE), | |
ae4e89e5 NTND |
527 | OPT_BOOL(0, "keep-largest-pack", &keep_base_pack, |
528 | N_("repack all other packs except the largest pack")), | |
44c637c8 JB |
529 | OPT_END() |
530 | }; | |
531 | ||
0c8151b6 NTND |
532 | if (argc == 2 && !strcmp(argv[1], "-h")) |
533 | usage_with_options(builtin_gc_usage, builtin_gc_options); | |
534 | ||
234587fc JK |
535 | argv_array_pushl(&pack_refs_cmd, "pack-refs", "--all", "--prune", NULL); |
536 | argv_array_pushl(&reflog, "reflog", "expire", "--all", NULL); | |
537 | argv_array_pushl(&repack, "repack", "-d", "-l", NULL); | |
2cfe2a78 | 538 | argv_array_pushl(&prune, "prune", "--expire", NULL); |
df0b6cfb | 539 | argv_array_pushl(&prune_worktrees, "worktree", "prune", "--expire", NULL); |
234587fc JK |
540 | argv_array_pushl(&rerere, "rerere", "gc", NULL); |
541 | ||
a831c06a | 542 | /* default expiry time, overwritten in gc_config */ |
5801d3b4 | 543 | gc_config(); |
a831c06a | 544 | if (parse_expiry_date(gc_log_expire, &gc_log_expire_time)) |
96913c9d | 545 | die(_("failed to parse gc.logexpiry value %s"), gc_log_expire); |
6757ada4 JB |
546 | |
547 | if (pack_refs < 0) | |
548 | pack_refs = !is_bare_repository(); | |
549 | ||
37782920 SB |
550 | argc = parse_options(argc, argv, prefix, builtin_gc_options, |
551 | builtin_gc_usage, 0); | |
44c637c8 JB |
552 | if (argc > 0) |
553 | usage_with_options(builtin_gc_usage, builtin_gc_options); | |
554 | ||
8ab5aa4b JH |
555 | if (prune_expire && parse_expiry_date(prune_expire, &dummy)) |
556 | die(_("failed to parse prune expiry value %s"), prune_expire); | |
557 | ||
44c637c8 | 558 | if (aggressive) { |
234587fc | 559 | argv_array_push(&repack, "-f"); |
125f8146 NTND |
560 | if (aggressive_depth > 0) |
561 | argv_array_pushf(&repack, "--depth=%d", aggressive_depth); | |
234587fc JK |
562 | if (aggressive_window > 0) |
563 | argv_array_pushf(&repack, "--window=%d", aggressive_window); | |
6757ada4 | 564 | } |
a0c14cbb | 565 | if (quiet) |
234587fc | 566 | argv_array_push(&repack, "-q"); |
6757ada4 | 567 | |
2c3c4399 JH |
568 | if (auto_gc) { |
569 | /* | |
570 | * Auto-gc should be least intrusive as possible. | |
571 | */ | |
2c3c4399 JH |
572 | if (!need_to_gc()) |
573 | return 0; | |
9f673f94 NTND |
574 | if (!quiet) { |
575 | if (detach_auto) | |
576 | fprintf(stderr, _("Auto packing the repository in background for optimum performance.\n")); | |
577 | else | |
578 | fprintf(stderr, _("Auto packing the repository for optimum performance.\n")); | |
579 | fprintf(stderr, _("See \"git help gc\" for manual housekeeping.\n")); | |
580 | } | |
62aad184 | 581 | if (detach_auto) { |
30299702 JN |
582 | int ret = report_last_gc_error(); |
583 | if (ret < 0) | |
584 | /* an I/O error occured, already reported */ | |
585 | exit(128); | |
586 | if (ret == 1) | |
587 | /* Last gc --auto failed. Skip this one. */ | |
588 | return 0; | |
329e6e87 | 589 | |
c45af94d JK |
590 | if (lock_repo_for_gc(force, &pid)) |
591 | return 0; | |
fec2ed21 | 592 | gc_before_repack(); /* dies on failure */ |
c45af94d JK |
593 | delete_tempfile(&pidfile); |
594 | ||
9f673f94 NTND |
595 | /* |
596 | * failure to daemonize is ok, we'll continue | |
597 | * in foreground | |
598 | */ | |
329e6e87 | 599 | daemonized = !daemonize(); |
62aad184 | 600 | } |
ae4e89e5 NTND |
601 | } else { |
602 | struct string_list keep_pack = STRING_LIST_INIT_NODUP; | |
603 | ||
604 | if (keep_base_pack != -1) { | |
605 | if (keep_base_pack) | |
55dfe13d NTND |
606 | find_base_packs(&keep_pack, 0); |
607 | } else if (big_pack_threshold) { | |
608 | find_base_packs(&keep_pack, big_pack_threshold); | |
ae4e89e5 NTND |
609 | } |
610 | ||
611 | add_repack_all_option(&keep_pack); | |
612 | string_list_clear(&keep_pack, 0); | |
613 | } | |
2c3c4399 | 614 | |
64a99eb4 NTND |
615 | name = lock_repo_for_gc(force, &pid); |
616 | if (name) { | |
617 | if (auto_gc) | |
618 | return 0; /* be quiet on --auto */ | |
619 | die(_("gc is already running on machine '%s' pid %"PRIuMAX" (use --force if not)"), | |
620 | name, (uintmax_t)pid); | |
621 | } | |
622 | ||
329e6e87 NTND |
623 | if (daemonized) { |
624 | hold_lock_file_for_update(&log_lock, | |
625 | git_path("gc.log"), | |
626 | LOCK_DIE_ON_ERROR); | |
076c8278 | 627 | dup2(get_lock_file_fd(&log_lock), 2); |
329e6e87 NTND |
628 | sigchain_push_common(process_log_file_on_signal); |
629 | atexit(process_log_file_at_exit); | |
630 | } | |
631 | ||
fec2ed21 | 632 | gc_before_repack(); |
6757ada4 | 633 | |
067fbd41 | 634 | if (!repository_format_precious_objects) { |
12e73a3c | 635 | close_all_packs(the_repository->objects); |
067fbd41 | 636 | if (run_command_v_opt(repack.argv, RUN_GIT_CMD)) |
fec2ed21 | 637 | die(FAILED_RUN, repack.argv[0]); |
067fbd41 JK |
638 | |
639 | if (prune_expire) { | |
640 | argv_array_push(&prune, prune_expire); | |
641 | if (quiet) | |
642 | argv_array_push(&prune, "--no-progress"); | |
0c16cd49 JT |
643 | if (repository_format_partial_clone) |
644 | argv_array_push(&prune, | |
645 | "--exclude-promisor-objects"); | |
067fbd41 | 646 | if (run_command_v_opt(prune.argv, RUN_GIT_CMD)) |
fec2ed21 | 647 | die(FAILED_RUN, prune.argv[0]); |
067fbd41 | 648 | } |
58e9d9d4 | 649 | } |
6757ada4 | 650 | |
e3df33bb NTND |
651 | if (prune_worktrees_expire) { |
652 | argv_array_push(&prune_worktrees, prune_worktrees_expire); | |
653 | if (run_command_v_opt(prune_worktrees.argv, RUN_GIT_CMD)) | |
fec2ed21 | 654 | die(FAILED_RUN, prune_worktrees.argv[0]); |
e3df33bb NTND |
655 | } |
656 | ||
234587fc | 657 | if (run_command_v_opt(rerere.argv, RUN_GIT_CMD)) |
fec2ed21 | 658 | die(FAILED_RUN, rerere.argv[0]); |
6757ada4 | 659 | |
478f34d2 | 660 | report_garbage = report_pack_garbage; |
a49d2834 | 661 | reprepare_packed_git(the_repository); |
478f34d2 DK |
662 | if (pack_garbage.nr > 0) |
663 | clean_pack_garbage(); | |
664 | ||
d5d5d7b6 | 665 | if (gc_write_commit_graph) |
7b0f2292 | 666 | write_commit_graph_reachable(get_object_directory(), 0, |
6b89a34c | 667 | !quiet && !daemonized); |
d5d5d7b6 | 668 | |
a087cc98 | 669 | if (auto_gc && too_many_loose_objects()) |
fea6128b ÆAB |
670 | warning(_("There are too many unreachable loose objects; " |
671 | "run 'git prune' to remove them.")); | |
a087cc98 | 672 | |
a831c06a DT |
673 | if (!daemonized) |
674 | unlink(git_path("gc.log")); | |
675 | ||
6757ada4 JB |
676 | return 0; |
677 | } |