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