Raw
1 #include "git-compat-util.h"
2 #include "abspath.h"
3 #include "blob.h"
4 #include "chdir-notify.h"
5 #include "config.h"
6 #include "gettext.h"
7 #include "lockfile.h"
8 #include "object-file.h"
9 #include "odb.h"
10 #include "odb/source.h"
11 #include "odb/source-files.h"
12 #include "odb/source-loose.h"
13 #include "pack-objects.h"
14 #include "packfile.h"
15 #include "path.h"
16 #include "promisor-remote.h"
17 #include "repack.h"
18 #include "run-command.h"
19 #include "strbuf.h"
20 #include "string-list.h"
21 #include "strvec.h"
22 #include "tree.h"
23 #include "write-or-die.h"
24
25 static void odb_source_files_reparent(const char *name UNUSED,
26 const char *old_cwd,
27 const char *new_cwd,
28 void *cb_data)
29 {
30 struct odb_source_files *files = cb_data;
31 char *path = reparent_relative_path(old_cwd, new_cwd,
32 files->base.path);
33 free(files->base.path);
34 files->base.path = path;
35 }
36
37 static void odb_source_files_free(struct odb_source *source)
38 {
39 struct odb_source_files *files = odb_source_files_downcast(source);
40 chdir_notify_unregister(NULL, odb_source_files_reparent, files);
41 odb_source_free(&files->loose->base);
42 odb_source_free(&files->packed->base);
43 odb_source_release(&files->base);
44 free(files);
45 }
46
47 static void odb_source_files_close(struct odb_source *source)
48 {
49 struct odb_source_files *files = odb_source_files_downcast(source);
50 odb_source_close(&files->loose->base);
51 odb_source_close(&files->packed->base);
52 }
53
54 static int odb_source_files_create_on_disk(struct odb_source *source)
55 {
56 struct strbuf path = STRBUF_INIT;
57
58 safe_create_dir(source->odb->repo, source->path, 1);
59
60 strbuf_addf(&path, "%s/pack", source->path);
61 safe_create_dir(source->odb->repo, path.buf, 1);
62
63 strbuf_reset(&path);
64 strbuf_addf(&path, "%s/info", source->path);
65 safe_create_dir(source->odb->repo, path.buf, 1);
66
67 strbuf_release(&path);
68 return 0;
69 }
70
71 static void odb_source_files_prepare(struct odb_source *source,
72 enum odb_prepare_flags flags)
73 {
74 struct odb_source_files *files = odb_source_files_downcast(source);
75 odb_source_prepare(&files->loose->base, flags);
76 odb_source_prepare(&files->packed->base, flags);
77 }
78
79 static int odb_source_files_read_object_info(struct odb_source *source,
80 const struct object_id *oid,
81 struct object_info *oi,
82 enum object_info_flags flags)
83 {
84 struct odb_source_files *files = odb_source_files_downcast(source);
85
86 if (!odb_source_read_object_info(&files->packed->base, oid, oi, flags) ||
87 !odb_source_read_object_info(&files->loose->base, oid, oi, flags))
88 return 0;
89
90 return -1;
91 }
92
93 static int odb_source_files_read_object_stream(struct odb_read_stream **out,
94 struct odb_source *source,
95 const struct object_id *oid)
96 {
97 struct odb_source_files *files = odb_source_files_downcast(source);
98 if (!odb_source_read_object_stream(out, &files->packed->base, oid) ||
99 !odb_source_read_object_stream(out, &files->loose->base, oid))
100 return 0;
101 return -1;
102 }
103
104 static int odb_source_files_for_each_object(struct odb_source *source,
105 const struct object_info *request,
106 odb_for_each_object_cb cb,
107 void *cb_data,
108 const struct odb_for_each_object_options *opts)
109 {
110 struct odb_source_files *files = odb_source_files_downcast(source);
111 int ret;
112
113 if (!(opts->flags & ODB_FOR_EACH_OBJECT_PROMISOR_ONLY)) {
114 ret = odb_source_for_each_object(&files->loose->base, request, cb, cb_data, opts);
115 if (ret)
116 return ret;
117 }
118
119 ret = odb_source_for_each_object(&files->packed->base, request, cb, cb_data, opts);
120 if (ret)
121 return ret;
122
123 return 0;
124 }
125
126 static int odb_source_files_count_objects(struct odb_source *source,
127 enum odb_count_objects_flags flags,
128 unsigned long *out)
129 {
130 struct odb_source_files *files = odb_source_files_downcast(source);
131 unsigned long count;
132 int ret;
133
134 ret = odb_source_count_objects(&files->packed->base, flags, &count);
135 if (ret < 0)
136 goto out;
137
138 if (!(flags & ODB_COUNT_OBJECTS_APPROXIMATE)) {
139 unsigned long loose_count;
140
141 ret = odb_source_count_objects(&files->loose->base, flags, &loose_count);
142 if (ret < 0)
143 goto out;
144
145 count += loose_count;
146 }
147
148 *out = count;
149 ret = 0;
150
151 out:
152 return ret;
153 }
154
155 static int odb_source_files_find_abbrev_len(struct odb_source *source,
156 const struct object_id *oid,
157 unsigned min_len,
158 unsigned *out)
159 {
160 struct odb_source_files *files = odb_source_files_downcast(source);
161 unsigned len = min_len;
162 int ret;
163
164 ret = odb_source_find_abbrev_len(&files->packed->base, oid, len, &len);
165 if (ret < 0)
166 goto out;
167
168 ret = odb_source_find_abbrev_len(&files->loose->base, oid, len, &len);
169 if (ret < 0)
170 goto out;
171
172 *out = len;
173 ret = 0;
174
175 out:
176 return ret;
177 }
178
179 static int odb_source_files_freshen_object(struct odb_source *source,
180 const struct object_id *oid,
181 const time_t *mtime)
182 {
183 struct odb_source_files *files = odb_source_files_downcast(source);
184 if (odb_source_freshen_object(&files->packed->base, oid, mtime) ||
185 odb_source_freshen_object(&files->loose->base, oid, mtime))
186 return 1;
187 return 0;
188 }
189
190 static int odb_source_files_write_object(struct odb_source *source,
191 const void *buf, size_t len,
192 enum object_type type,
193 const struct object_id *oid,
194 const struct object_id *compat_oid,
195 const time_t *mtime,
196 enum odb_write_object_flags flags)
197 {
198 struct odb_source_files *files = odb_source_files_downcast(source);
199 return odb_source_write_object(&files->loose->base, buf, len, type,
200 oid, compat_oid, mtime, flags);
201 }
202
203 static int odb_source_files_write_object_stream(struct odb_source *source,
204 struct odb_write_stream *stream,
205 size_t len,
206 struct object_id *oid)
207 {
208 struct odb_source_files *files = odb_source_files_downcast(source);
209 return odb_source_write_object_stream(&files->loose->base, stream, len, oid);
210 }
211
212 static int odb_source_files_begin_transaction(struct odb_source *source,
213 struct odb_transaction **out,
214 enum odb_transaction_flags flags)
215 {
216 return odb_transaction_files_begin(source, out, flags);
217 }
218
219 static int odb_source_files_read_alternates(struct odb_source *source,
220 struct strvec *out)
221 {
222 struct strbuf buf = STRBUF_INIT;
223 char *path;
224
225 path = xstrfmt("%s/info/alternates", source->path);
226 if (strbuf_read_file(&buf, path, 1024) < 0) {
227 warn_on_fopen_errors(path);
228 free(path);
229 return 0;
230 }
231 parse_alternates(buf.buf, '\n', source->path, out);
232
233 strbuf_release(&buf);
234 free(path);
235 return 0;
236 }
237
238 static int odb_source_files_write_alternate(struct odb_source *source,
239 const char *alternate)
240 {
241 struct lock_file lock = LOCK_INIT;
242 char *path = xstrfmt("%s/%s", source->path, "info/alternates");
243 FILE *in, *out;
244 int found = 0;
245 int ret;
246
247 repo_hold_lock_file_for_update(source->odb->repo, &lock, path,
248 LOCK_DIE_ON_ERROR);
249 out = fdopen_lock_file(&lock, "w");
250 if (!out) {
251 ret = error_errno(_("unable to fdopen alternates lockfile"));
252 goto out;
253 }
254
255 in = fopen(path, "r");
256 if (in) {
257 struct strbuf line = STRBUF_INIT;
258
259 while (strbuf_getline(&line, in) != EOF) {
260 if (!strcmp(alternate, line.buf)) {
261 found = 1;
262 break;
263 }
264 fprintf_or_die(out, "%s\n", line.buf);
265 }
266
267 strbuf_release(&line);
268 fclose(in);
269 } else if (errno != ENOENT) {
270 ret = error_errno(_("unable to read alternates file"));
271 goto out;
272 }
273
274 if (found) {
275 rollback_lock_file(&lock);
276 } else {
277 fprintf_or_die(out, "%s\n", alternate);
278 if (commit_lock_file(&lock)) {
279 ret = error_errno(_("unable to move new alternates file into place"));
280 goto out;
281 }
282 }
283
284 ret = 0;
285
286 out:
287 free(path);
288 return ret;
289 }
290
291 static int too_many_loose_objects(struct odb_source_files *files, int limit)
292 {
293 unsigned long loose_count;
294
295 if (limit <= 0)
296 return 0;
297
298 if (odb_source_count_objects(&files->loose->base, ODB_COUNT_OBJECTS_APPROXIMATE,
299 &loose_count) < 0)
300 return 0;
301
302 /*
303 * This is weird, but stems from legacy behaviour: the GC auto
304 * threshold was always essentially interpreted as if it was rounded up
305 * to the next multiple 256 of, so we retain this behaviour for now.
306 */
307 return loose_count > (DIV_ROUND_UP(((unsigned long) limit), 256) * 256);
308 }
309
310 static struct packed_git *find_base_packs(struct odb_source_files *files,
311 struct string_list *packs,
312 unsigned long limit)
313 {
314 struct packfile_list_entry *e;
315 struct packed_git *base = NULL;
316
317 for (e = packfile_store_get_packs(files->packed); e; e = e->next) {
318 if (e->pack->is_cruft)
319 continue;
320 if (limit) {
321 if ((uintmax_t) e->pack->pack_size >= limit)
322 string_list_append(packs, e->pack->pack_name);
323 } else if (!base || base->pack_size < e->pack->pack_size) {
324 base = e->pack;
325 }
326 }
327
328 if (base)
329 string_list_append(packs, base->pack_name);
330
331 return base;
332 }
333
334 static int too_many_packs(struct odb_source_files *files, int gc_auto_pack_limit)
335 {
336 struct packfile_list_entry *e;
337 int cnt = 0;
338
339 if (gc_auto_pack_limit <= 0)
340 return 0;
341
342 for (e = packfile_store_get_packs(files->packed); e; e = e->next) {
343 if (e->pack->pack_keep)
344 continue;
345 /*
346 * Perhaps check the size of the pack and count only
347 * very small ones here?
348 */
349 cnt++;
350 }
351 return gc_auto_pack_limit < cnt;
352 }
353
354 static uint64_t total_ram(void)
355 {
356 #if defined(HAVE_SYSINFO)
357 struct sysinfo si;
358
359 if (!sysinfo(&si)) {
360 uint64_t total = si.totalram;
361
362 if (si.mem_unit > 1)
363 total *= (uint64_t)si.mem_unit;
364 return total;
365 }
366 #elif defined(HAVE_BSD_SYSCTL) && (defined(HW_MEMSIZE) || defined(HW_PHYSMEM) || defined(HW_PHYSMEM64))
367 uint64_t physical_memory;
368 int mib[2];
369 size_t length;
370
371 mib[0] = CTL_HW;
372 # if defined(HW_MEMSIZE)
373 mib[1] = HW_MEMSIZE;
374 # elif defined(HW_PHYSMEM64)
375 mib[1] = HW_PHYSMEM64;
376 # else
377 mib[1] = HW_PHYSMEM;
378 # endif
379 length = sizeof(physical_memory);
380 if (!sysctl(mib, 2, &physical_memory, &length, NULL, 0)) {
381 if (length == 4) {
382 uint32_t mem;
383
384 if (!sysctl(mib, 2, &mem, &length, NULL, 0))
385 physical_memory = mem;
386 }
387 return physical_memory;
388 }
389 #elif defined(GIT_WINDOWS_NATIVE)
390 MEMORYSTATUSEX memInfo;
391
392 memInfo.dwLength = sizeof(MEMORYSTATUSEX);
393 if (GlobalMemoryStatusEx(&memInfo))
394 return memInfo.ullTotalPhys;
395 #endif
396 return 0;
397 }
398
399 static uint64_t estimate_repack_memory(struct odb_source_files *files,
400 struct packed_git *pack)
401 {
402 unsigned long max_delta_cache_size = DEFAULT_DELTA_CACHE_SIZE;
403 unsigned long delta_base_cache_limit = DEFAULT_DELTA_BASE_CACHE_LIMIT;
404 unsigned long nr_objects;
405 size_t os_cache, heap;
406
407 if (odb_source_count_objects(&files->base, ODB_COUNT_OBJECTS_APPROXIMATE,
408 &nr_objects) < 0)
409 return 0;
410
411 if (!pack || !nr_objects)
412 return 0;
413
414 repo_config_get_ulong(files->base.odb->repo, "pack.deltacachesize",
415 &max_delta_cache_size);
416 repo_config_get_ulong(files->base.odb->repo, "core.deltabasecachelimit",
417 &delta_base_cache_limit);
418
419 /*
420 * First we have to scan through at least one pack.
421 * Assume enough room in OS file cache to keep the entire pack
422 * or we may accidentally evict data of other processes from
423 * the cache.
424 */
425 os_cache = pack->pack_size + pack->index_size;
426 /* then pack-objects needs lots more for book keeping */
427 heap = sizeof(struct object_entry) * nr_objects;
428 /*
429 * internal rev-list --all --objects takes up some memory too,
430 * let's say half of it is for blobs
431 */
432 heap += sizeof(struct blob) * nr_objects / 2;
433 /*
434 * and the other half is for trees (commits and tags are
435 * usually insignificant)
436 */
437 heap += sizeof(struct tree) * nr_objects / 2;
438 /* and then obj_hash[], underestimated in fact */
439 heap += sizeof(struct object *) * nr_objects;
440 /* revindex is used also */
441 heap += (sizeof(off_t) + sizeof(uint32_t)) * nr_objects;
442 /*
443 * read_sha1_file() (either at delta calculation phase, or
444 * writing phase) also fills up the delta base cache
445 */
446 heap += delta_base_cache_limit;
447 /* and of course pack-objects has its own delta cache */
448 heap += max_delta_cache_size;
449
450 return os_cache + heap;
451 }
452
453 static int keep_one_pack(struct string_list_item *item, void *data)
454 {
455 struct strvec *args = data;
456 strvec_pushf(args, "--keep-pack=%s", basename(item->string));
457 return 0;
458 }
459
460 static void add_repack_all_option(struct repository *repo,
461 const struct odb_optimize_options *opts,
462 struct string_list *keep_pack,
463 struct strvec *args)
464 {
465 char *repack_filter = NULL;
466 char *repack_filter_to = NULL;
467
468 repo_config_get_string(repo, "gc.repackfilter", &repack_filter);
469 repo_config_get_string(repo, "gc.repackfilterto", &repack_filter_to);
470
471 if (opts->prune_expire && !strcmp(opts->prune_expire, "now") &&
472 !(opts->cruft_packs && opts->expire_to))
473 strvec_push(args, "-a");
474 else if (opts->cruft_packs) {
475 strvec_push(args, "--cruft");
476 if (opts->prune_expire)
477 strvec_pushf(args, "--cruft-expiration=%s", opts->prune_expire);
478 if (opts->max_cruft_size)
479 strvec_pushf(args, "--max-cruft-size=%lu",
480 opts->max_cruft_size);
481 if (opts->expire_to)
482 strvec_pushf(args, "--expire-to=%s", opts->expire_to);
483 } else {
484 strvec_push(args, "-A");
485 if (opts->prune_expire)
486 strvec_pushf(args, "--unpack-unreachable=%s", opts->prune_expire);
487 }
488
489 if (keep_pack)
490 for_each_string_list(keep_pack, keep_one_pack, args);
491
492 if (repack_filter && *repack_filter)
493 strvec_pushf(args, "--filter=%s", repack_filter);
494 if (repack_filter_to && *repack_filter_to)
495 strvec_pushf(args, "--filter-to=%s", repack_filter_to);
496
497 free(repack_filter);
498 free(repack_filter_to);
499 }
500
501 static void add_repack_incremental_option(struct strvec *args)
502 {
503 strvec_push(args, "--no-write-bitmap-index");
504 }
505
506 bool odb_source_files_optimize_required(struct odb_source *source,
507 const struct odb_optimize_options *opts)
508 {
509 struct odb_source_files *files = odb_source_files_downcast(source);
510 struct repository *repo = source->odb->repo;
511
512 switch (opts->strategy) {
513 case ODB_OPTIMIZE_INCREMENTAL: {
514 int gc_auto_threshold = 6700;
515 int gc_auto_pack_limit = 50;
516
517 repo_config_get_int(repo, "gc.auto", &gc_auto_threshold);
518 repo_config_get_int(repo, "gc.autopacklimit", &gc_auto_pack_limit);
519
520 /*
521 * Setting gc.auto to 0 or negative can disable the
522 * automatic gc.
523 */
524 if (gc_auto_threshold <= 0)
525 return false;
526 if (!too_many_packs(files, gc_auto_pack_limit) &&
527 !too_many_loose_objects(files, gc_auto_threshold))
528 return false;
529
530 return true;
531 }
532 case ODB_OPTIMIZE_GEOMETRIC: {
533 struct pack_geometry geometry = {
534 .split_factor = 2,
535 };
536 struct pack_objects_args po_args = {
537 .local = 1,
538 };
539 struct existing_packs existing_packs = EXISTING_PACKS_INIT;
540 struct string_list kept_packs = STRING_LIST_INIT_DUP;
541 int auto_value = 100;
542 bool ret;
543
544 repo_config_get_int(repo, "maintenance.geometric-repack.auto",
545 &auto_value);
546 if (!auto_value)
547 return false;
548 if (auto_value < 0)
549 return true;
550
551 repo_config_get_int(repo, "maintenance.geometric-repack.splitFactor",
552 &geometry.split_factor);
553
554 existing_packs.repo = repo;
555 existing_packs_collect(&existing_packs, &kept_packs);
556 pack_geometry_init(&geometry, &existing_packs, &po_args);
557 pack_geometry_split(&geometry);
558
559 /*
560 * When we'd merge at least two packs with one another we always
561 * perform the repack.
562 */
563 if (geometry.split) {
564 ret = true;
565 goto out;
566 }
567
568 /*
569 * Otherwise, we estimate the number of loose objects to determine
570 * whether we want to create a new packfile or not.
571 */
572 if (too_many_loose_objects(files, auto_value)) {
573 ret = true;
574 goto out;
575 }
576
577 ret = false;
578
579 out:
580 existing_packs_release(&existing_packs);
581 pack_geometry_release(&geometry);
582 return ret;
583 }
584 default:
585 BUG("unknown maintenance strategy '%d'", opts->strategy);
586 }
587 }
588
589 int odb_source_files_optimize(struct odb_source *source,
590 const struct odb_optimize_options *opts)
591 {
592 struct odb_source_files *files = odb_source_files_downcast(source);
593 struct repository *repo = source->odb->repo;
594 struct child_process repack_cmd = CHILD_PROCESS_INIT;
595 unsigned long big_pack_threshold = 0;
596 int gc_auto_threshold = 6700;
597 int gc_auto_pack_limit = 50;
598 int ret;
599
600 repo_config_get_int(repo, "gc.auto", &gc_auto_threshold);
601 repo_config_get_int(repo, "gc.autopacklimit", &gc_auto_pack_limit);
602 repo_config_get_ulong(repo, "gc.bigpackthreshold", &big_pack_threshold);
603
604 if (repo->repository_format_precious_objects)
605 return 0;
606
607 repack_cmd.git_cmd = 1;
608 repack_cmd.odb_to_close = repo->objects;
609
610 strvec_pushl(&repack_cmd.args, "repack", "-d", "-l", NULL);
611 if (opts->flags & ODB_OPTIMIZE_NO_REUSE_DELTAS)
612 strvec_push(&repack_cmd.args, "-f");
613 if (opts->depth > 0)
614 strvec_pushf(&repack_cmd.args, "--depth=%d", opts->depth);
615 if (opts->window > 0)
616 strvec_pushf(&repack_cmd.args, "--window=%d", opts->window);
617 if (!(opts->flags & ODB_OPTIMIZE_VERBOSE))
618 strvec_push(&repack_cmd.args, "-q");
619
620 /*
621 * There's three cases we need to consider:
622 *
623 * - If we're invoked without `--auto` we'll need to perform a full
624 * repack.
625 *
626 * - If we're invoked with `--auto` and there's too many packs, then
627 * we perform a full repack, as well.
628 *
629 * - Otherwise we perform an incremental repack.
630 */
631 switch (opts->strategy) {
632 case ODB_OPTIMIZE_INCREMENTAL:
633 if (!(opts->flags & ODB_OPTIMIZE_AUTO)) {
634 struct string_list keep_pack = STRING_LIST_INIT_NODUP;
635
636 if (opts->keep_largest_pack != -1) {
637 if (opts->keep_largest_pack)
638 find_base_packs(files, &keep_pack, 0);
639 } else if (big_pack_threshold) {
640 find_base_packs(files, &keep_pack, big_pack_threshold);
641 }
642
643 add_repack_all_option(repo, opts, &keep_pack, &repack_cmd.args);
644 string_list_clear(&keep_pack, 0);
645 } else {
646 if (too_many_packs(files, gc_auto_pack_limit)) {
647 struct string_list keep_pack = STRING_LIST_INIT_NODUP;
648
649 if (big_pack_threshold) {
650 find_base_packs(files, &keep_pack, big_pack_threshold);
651 if (keep_pack.nr >= (unsigned long) gc_auto_pack_limit) {
652 string_list_clear(&keep_pack, 0);
653 find_base_packs(files, &keep_pack, 0);
654 }
655 } else {
656 struct packed_git *p = find_base_packs(files, &keep_pack, 0);
657 uint64_t mem_have, mem_want;
658
659 mem_have = total_ram();
660 mem_want = estimate_repack_memory(files, p);
661
662 /*
663 * Only allow 1/2 of memory for pack-objects, leave
664 * the rest for the OS and other processes in the
665 * system.
666 */
667 if (!mem_have || mem_want < mem_have / 2)
668 string_list_clear(&keep_pack, 0);
669 }
670
671 add_repack_all_option(repo, opts, &keep_pack, &repack_cmd.args);
672 string_list_clear(&keep_pack, 0);
673 } else {
674 add_repack_incremental_option(&repack_cmd.args);
675 }
676 }
677
678 break;
679 case ODB_OPTIMIZE_GEOMETRIC: {
680 struct pack_geometry geometry = {
681 .split_factor = 2,
682 };
683 struct pack_objects_args po_args = {
684 .local = 1,
685 };
686 struct existing_packs existing_packs = EXISTING_PACKS_INIT;
687 struct string_list kept_packs = STRING_LIST_INIT_DUP;
688
689 repo_config_get_int(repo, "maintenance.geometric-repack.splitFactor",
690 &geometry.split_factor);
691
692 existing_packs.repo = repo;
693 existing_packs_collect(&existing_packs, &kept_packs);
694 pack_geometry_init(&geometry, &existing_packs, &po_args);
695 pack_geometry_split(&geometry);
696
697 if (geometry.split < geometry.pack_nr) {
698 strvec_pushf(&repack_cmd.args, "--geometric=%d",
699 geometry.split_factor);
700 } else {
701 add_repack_all_option(repo, opts, NULL, &repack_cmd.args);
702 }
703 if (repo->settings.core_multi_pack_index)
704 strvec_push(&repack_cmd.args, "--write-midx");
705
706 existing_packs_release(&existing_packs);
707 pack_geometry_release(&geometry);
708 break;
709 }
710 default:
711 die("unknown maintenance strategy '%d'", opts->strategy);
712 }
713
714 if (run_command(&repack_cmd)) {
715 ret = error("failed to run %s", repack_cmd.args.v[0]);
716 goto out;
717 }
718
719 /* Geometric repacking uses cruft packs, so we don't have to prune separately. */
720 if (opts->strategy != ODB_OPTIMIZE_GEOMETRIC && opts->prune_expire) {
721 struct child_process prune_cmd = CHILD_PROCESS_INIT;
722
723 strvec_pushl(&prune_cmd.args, "prune", "--expire", NULL);
724 /* run `git prune` even if using cruft packs */
725 strvec_push(&prune_cmd.args, opts->prune_expire);
726 if (!(opts->flags & ODB_OPTIMIZE_VERBOSE))
727 strvec_push(&prune_cmd.args, "--no-progress");
728 if (repo_has_promisor_remote(repo))
729 strvec_push(&prune_cmd.args,
730 "--exclude-promisor-objects");
731 prune_cmd.git_cmd = 1;
732
733 if (run_command(&prune_cmd)) {
734 ret = error("failed to run %s", prune_cmd.args.v[0]);
735 goto out;
736 }
737 }
738
739 if (opts->flags & ODB_OPTIMIZE_AUTO && too_many_loose_objects(files, gc_auto_threshold))
740 warning(_("There are too many unreachable loose objects; "
741 "run 'git prune' to remove them."));
742
743 ret = 0;
744
745 out:
746 return ret;
747 }
748
749 struct odb_source_files *odb_source_files_new(struct object_database *odb,
750 const char *path,
751 bool local)
752 {
753 struct odb_source_files *files;
754
755 CALLOC_ARRAY(files, 1);
756 odb_source_init(&files->base, odb, ODB_SOURCE_FILES, path, local);
757 files->loose = odb_source_loose_new(odb, path, local);
758 files->packed = odb_source_packed_new(odb, path, local);
759
760 files->base.free = odb_source_files_free;
761 files->base.close = odb_source_files_close;
762 files->base.create_on_disk = odb_source_files_create_on_disk;
763 files->base.prepare = odb_source_files_prepare;
764 files->base.read_object_info = odb_source_files_read_object_info;
765 files->base.read_object_stream = odb_source_files_read_object_stream;
766 files->base.for_each_object = odb_source_files_for_each_object;
767 files->base.count_objects = odb_source_files_count_objects;
768 files->base.find_abbrev_len = odb_source_files_find_abbrev_len;
769 files->base.freshen_object = odb_source_files_freshen_object;
770 files->base.write_object = odb_source_files_write_object;
771 files->base.write_object_stream = odb_source_files_write_object_stream;
772 files->base.begin_transaction = odb_source_files_begin_transaction;
773 files->base.read_alternates = odb_source_files_read_alternates;
774 files->base.write_alternate = odb_source_files_write_alternate;
775 files->base.optimize = odb_source_files_optimize;
776 files->base.optimize_required = odb_source_files_optimize_required;
777
778 /*
779 * Ideally, we would only ever store absolute paths in the source. This
780 * is not (yet) possible though because we access and assume relative
781 * paths in the primary ODB source in some user-facing functionality.
782 */
783 if (!is_absolute_path(path))
784 chdir_notify_register(NULL, odb_source_files_reparent, files);
785
786 return files;
787 }