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