at commit a0189536

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 "hex.h"
8 #include "lockfile.h"
9 #include "object-file.h"
10 #include "odb.h"
11 #include "odb/source.h"
12 #include "odb/source-files.h"
13 #include "odb/source-loose.h"
14 #include "pack-objects.h"
15 #include "packfile.h"
16 #include "path.h"
17 #include "promisor-remote.h"
18 #include "repack.h"
19 #include "run-command.h"
20 #include "strbuf.h"
21 #include "string-list.h"
22 #include "strvec.h"
23 #include "tree.h"
24 #include "write-or-die.h"
25
26 static void odb_source_files_reparent(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(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 enum odb_read_status 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 struct strbuf *errmsg)
84 {
85 struct odb_source_files *files = odb_source_files_downcast(source);
86 enum odb_read_status ret_packed, ret_loose;
87
88 ret_packed = odb_source_read_object_info(&files->packed->base, oid, oi,
89 flags, errmsg);
90 if (!ret_packed)
91 return 0;
92
93 ret_loose = odb_source_read_object_info(&files->loose->base, oid, oi, flags,
94 ret_packed == ODB_READ_NOT_FOUND ? errmsg : NULL);
95 if (!ret_loose)
96 return 0;
97
98 /*
99 * Reading the packed object may have failed even though the object
100 * exists, for example because it is corrupt. Report this failure to
101 * the caller in case neither of the sources was able to read the
102 * object, and prefer the error of the packed source in case both
103 * reads have failed.
104 */
105 if (ret_packed != ODB_READ_NOT_FOUND)
106 return ret_packed;
107 return ret_loose;
108 }
109
110 static int odb_source_files_read_object_stream(struct odb_stream **out,
111 struct odb_source *source,
112 const struct object_id *oid)
113 {
114 struct odb_source_files *files = odb_source_files_downcast(source);
115 if (!odb_source_read_object_stream(out, &files->packed->base, oid) ||
116 !odb_source_read_object_stream(out, &files->loose->base, oid))
117 return 0;
118 return -1;
119 }
120
121 static int odb_source_files_for_each_object(struct odb_source *source,
122 const struct object_info *request,
123 odb_for_each_object_cb cb,
124 void *cb_data,
125 const struct odb_for_each_object_options *opts)
126 {
127 struct odb_source_files *files = odb_source_files_downcast(source);
128 int ret;
129
130 if (!(opts->flags & ODB_FOR_EACH_OBJECT_PROMISOR_ONLY)) {
131 ret = odb_source_for_each_object(&files->loose->base, request, cb, cb_data, opts);
132 if (ret)
133 return ret;
134 }
135
136 ret = odb_source_for_each_object(&files->packed->base, request, cb, cb_data, opts);
137 if (ret)
138 return ret;
139
140 return 0;
141 }
142
143 static int odb_source_files_count_objects(struct odb_source *source,
144 enum odb_count_objects_flags flags,
145 unsigned long *out)
146 {
147 struct odb_source_files *files = odb_source_files_downcast(source);
148 unsigned long count;
149 int ret;
150
151 ret = odb_source_count_objects(&files->packed->base, flags, &count);
152 if (ret < 0)
153 goto out;
154
155 if (!(flags & ODB_COUNT_OBJECTS_APPROXIMATE)) {
156 unsigned long loose_count;
157
158 ret = odb_source_count_objects(&files->loose->base, flags, &loose_count);
159 if (ret < 0)
160 goto out;
161
162 count += loose_count;
163 }
164
165 *out = count;
166 ret = 0;
167
168 out:
169 return ret;
170 }
171
172 static int odb_source_files_find_abbrev_len(struct odb_source *source,
173 const struct object_id *oid,
174 unsigned min_len,
175 unsigned *out)
176 {
177 struct odb_source_files *files = odb_source_files_downcast(source);
178 unsigned len = min_len;
179 int ret;
180
181 ret = odb_source_find_abbrev_len(&files->packed->base, oid, len, &len);
182 if (ret < 0)
183 goto out;
184
185 ret = odb_source_find_abbrev_len(&files->loose->base, oid, len, &len);
186 if (ret < 0)
187 goto out;
188
189 *out = len;
190 ret = 0;
191
192 out:
193 return ret;
194 }
195
196 static int odb_source_files_freshen_object(struct odb_source *source,
197 const struct object_id *oid,
198 const time_t *mtime)
199 {
200 struct odb_source_files *files = odb_source_files_downcast(source);
201 if (odb_source_freshen_object(&files->packed->base, oid, mtime) ||
202 odb_source_freshen_object(&files->loose->base, oid, mtime))
203 return 1;
204 return 0;
205 }
206
207 static int odb_source_files_write_object(struct odb_source *source,
208 const void *buf, size_t len,
209 enum object_type type,
210 const struct object_id *oid,
211 const struct object_id *compat_oid,
212 const time_t *mtime,
213 enum odb_write_object_flags flags)
214 {
215 struct odb_source_files *files = odb_source_files_downcast(source);
216 return odb_source_write_object(&files->loose->base, buf, len, type,
217 oid, compat_oid, mtime, flags);
218 }
219
220 static int odb_source_files_write_object_stream(struct odb_source *source,
221 struct odb_stream *stream,
222 struct object_id *oid)
223 {
224 struct odb_source_files *files = odb_source_files_downcast(source);
225 return odb_source_write_object_stream(&files->loose->base, stream, oid);
226 }
227
228 static int odb_source_files_begin_transaction(struct odb_source *source,
229 struct odb_transaction **out,
230 enum odb_transaction_flags flags)
231 {
232 return odb_transaction_files_begin(source, out, flags);
233 }
234
235 static int odb_source_files_read_alternates(struct odb_source *source,
236 struct strvec *out)
237 {
238 struct strbuf buf = STRBUF_INIT;
239 char *path;
240
241 path = xstrfmt("%s/info/alternates", source->path);
242 if (strbuf_read_file(&buf, path, 1024) < 0) {
243 warn_on_fopen_errors(path);
244 free(path);
245 return 0;
246 }
247 parse_alternates(buf.buf, '\n', source->path, out);
248
249 strbuf_release(&buf);
250 free(path);
251 return 0;
252 }
253
254 static int odb_source_files_write_alternate(struct odb_source *source,
255 const char *alternate)
256 {
257 struct lock_file lock = LOCK_INIT;
258 char *path = xstrfmt("%s/%s", source->path, "info/alternates");
259 FILE *in, *out;
260 int found = 0;
261 int ret;
262
263 repo_hold_lock_file_for_update(source->odb->repo, &lock, path,
264 LOCK_DIE_ON_ERROR);
265 out = fdopen_lock_file(&lock, "w");
266 if (!out) {
267 ret = error_errno(_("unable to fdopen alternates lockfile"));
268 goto out;
269 }
270
271 in = fopen(path, "r");
272 if (in) {
273 struct strbuf line = STRBUF_INIT;
274
275 while (strbuf_getline(&line, in) != EOF) {
276 if (!strcmp(alternate, line.buf)) {
277 found = 1;
278 break;
279 }
280 fprintf_or_die(out, "%s\n", line.buf);
281 }
282
283 strbuf_release(&line);
284 fclose(in);
285 } else if (errno != ENOENT) {
286 ret = error_errno(_("unable to read alternates file"));
287 goto out;
288 }
289
290 if (found) {
291 rollback_lock_file(&lock);
292 } else {
293 fprintf_or_die(out, "%s\n", alternate);
294 if (commit_lock_file(&lock)) {
295 ret = error_errno(_("unable to move new alternates file into place"));
296 goto out;
297 }
298 }
299
300 ret = 0;
301
302 out:
303 free(path);
304 return ret;
305 }
306
307 static int too_many_loose_objects(struct odb_source_files *files, int limit)
308 {
309 unsigned long loose_count;
310
311 if (limit <= 0)
312 return 0;
313
314 if (odb_source_count_objects(&files->loose->base, ODB_COUNT_OBJECTS_APPROXIMATE,
315 &loose_count) < 0)
316 return 0;
317
318 /*
319 * This is weird, but stems from legacy behaviour: the GC auto
320 * threshold was always essentially interpreted as if it was rounded up
321 * to the next multiple 256 of, so we retain this behaviour for now.
322 */
323 return loose_count > (DIV_ROUND_UP(((unsigned long) limit), 256) * 256);
324 }
325
326 static struct packed_git *find_base_packs(struct odb_source_files *files,
327 struct string_list *packs,
328 unsigned long limit)
329 {
330 struct packfile_list_entry *e;
331 struct packed_git *base = NULL;
332
333 for (e = packfile_store_get_packs(files->packed); e; e = e->next) {
334 if (e->pack->is_cruft)
335 continue;
336 if (limit) {
337 if ((uintmax_t) e->pack->pack_size >= limit)
338 string_list_append(packs, e->pack->pack_name);
339 } else if (!base || base->pack_size < e->pack->pack_size) {
340 base = e->pack;
341 }
342 }
343
344 if (base)
345 string_list_append(packs, base->pack_name);
346
347 return base;
348 }
349
350 static int too_many_packs(struct odb_source_files *files, int gc_auto_pack_limit)
351 {
352 struct packfile_list_entry *e;
353 int cnt = 0;
354
355 if (gc_auto_pack_limit <= 0)
356 return 0;
357
358 for (e = packfile_store_get_packs(files->packed); e; e = e->next) {
359 if (e->pack->pack_keep)
360 continue;
361 /*
362 * Perhaps check the size of the pack and count only
363 * very small ones here?
364 */
365 cnt++;
366 }
367 return gc_auto_pack_limit < cnt;
368 }
369
370 static uint64_t total_ram(void)
371 {
372 #if defined(HAVE_SYSINFO)
373 struct sysinfo si;
374
375 if (!sysinfo(&si)) {
376 uint64_t total = si.totalram;
377
378 if (si.mem_unit > 1)
379 total *= (uint64_t)si.mem_unit;
380 return total;
381 }
382 #elif defined(HAVE_BSD_SYSCTL) && (defined(HW_MEMSIZE) || defined(HW_PHYSMEM) || defined(HW_PHYSMEM64))
383 uint64_t physical_memory;
384 int mib[2];
385 size_t length;
386
387 mib[0] = CTL_HW;
388 # if defined(HW_MEMSIZE)
389 mib[1] = HW_MEMSIZE;
390 # elif defined(HW_PHYSMEM64)
391 mib[1] = HW_PHYSMEM64;
392 # else
393 mib[1] = HW_PHYSMEM;
394 # endif
395 length = sizeof(physical_memory);
396 if (!sysctl(mib, 2, &physical_memory, &length, NULL, 0)) {
397 if (length == 4) {
398 uint32_t mem;
399
400 if (!sysctl(mib, 2, &mem, &length, NULL, 0))
401 physical_memory = mem;
402 }
403 return physical_memory;
404 }
405 #elif defined(GIT_WINDOWS_NATIVE)
406 MEMORYSTATUSEX memInfo;
407
408 memInfo.dwLength = sizeof(MEMORYSTATUSEX);
409 if (GlobalMemoryStatusEx(&memInfo))
410 return memInfo.ullTotalPhys;
411 #endif
412 return 0;
413 }
414
415 static uint64_t estimate_repack_memory(struct odb_source_files *files,
416 struct packed_git *pack)
417 {
418 unsigned long max_delta_cache_size = DEFAULT_DELTA_CACHE_SIZE;
419 unsigned long delta_base_cache_limit = DEFAULT_DELTA_BASE_CACHE_LIMIT;
420 unsigned long nr_objects;
421 size_t os_cache, heap;
422
423 if (odb_source_count_objects(&files->base, ODB_COUNT_OBJECTS_APPROXIMATE,
424 &nr_objects) < 0)
425 return 0;
426
427 if (!pack || !nr_objects)
428 return 0;
429
430 repo_config_get_ulong(files->base.odb->repo, "pack.deltacachesize",
431 &max_delta_cache_size);
432 repo_config_get_ulong(files->base.odb->repo, "core.deltabasecachelimit",
433 &delta_base_cache_limit);
434
435 /*
436 * First we have to scan through at least one pack.
437 * Assume enough room in OS file cache to keep the entire pack
438 * or we may accidentally evict data of other processes from
439 * the cache.
440 */
441 os_cache = pack->pack_size + pack->index_size;
442 /* then pack-objects needs lots more for book keeping */
443 heap = sizeof(struct object_entry) * nr_objects;
444 /*
445 * internal rev-list --all --objects takes up some memory too,
446 * let's say half of it is for blobs
447 */
448 heap += sizeof(struct blob) * nr_objects / 2;
449 /*
450 * and the other half is for trees (commits and tags are
451 * usually insignificant)
452 */
453 heap += sizeof(struct tree) * nr_objects / 2;
454 /* and then obj_hash[], underestimated in fact */
455 heap += sizeof(struct object *) * nr_objects;
456 /* revindex is used also */
457 heap += (sizeof(off_t) + sizeof(uint32_t)) * nr_objects;
458 /*
459 * read_sha1_file() (either at delta calculation phase, or
460 * writing phase) also fills up the delta base cache
461 */
462 heap += delta_base_cache_limit;
463 /* and of course pack-objects has its own delta cache */
464 heap += max_delta_cache_size;
465
466 return os_cache + heap;
467 }
468
469 static int keep_one_pack(struct string_list_item *item, void *data)
470 {
471 struct strvec *args = data;
472 strvec_pushf(args, "--keep-pack=%s", basename(item->string));
473 return 0;
474 }
475
476 static void add_repack_all_option(struct repository *repo,
477 const struct odb_optimize_options *opts,
478 struct string_list *keep_pack,
479 struct strvec *args)
480 {
481 char *repack_filter = NULL;
482 char *repack_filter_to = NULL;
483
484 repo_config_get_string(repo, "gc.repackfilter", &repack_filter);
485 repo_config_get_string(repo, "gc.repackfilterto", &repack_filter_to);
486
487 if (opts->prune_expire && !strcmp(opts->prune_expire, "now") &&
488 !(opts->cruft_packs && opts->expire_to))
489 strvec_push(args, "-a");
490 else if (opts->cruft_packs) {
491 strvec_push(args, "--cruft");
492 if (opts->prune_expire)
493 strvec_pushf(args, "--cruft-expiration=%s", opts->prune_expire);
494 if (opts->max_cruft_size)
495 strvec_pushf(args, "--max-cruft-size=%lu",
496 opts->max_cruft_size);
497 if (opts->expire_to)
498 strvec_pushf(args, "--expire-to=%s", opts->expire_to);
499 } else {
500 strvec_push(args, "-A");
501 if (opts->prune_expire)
502 strvec_pushf(args, "--unpack-unreachable=%s", opts->prune_expire);
503 }
504
505 if (keep_pack)
506 for_each_string_list(keep_pack, keep_one_pack, args);
507
508 if (repack_filter && *repack_filter)
509 strvec_pushf(args, "--filter=%s", repack_filter);
510 if (repack_filter_to && *repack_filter_to)
511 strvec_pushf(args, "--filter-to=%s", repack_filter_to);
512
513 free(repack_filter);
514 free(repack_filter_to);
515 }
516
517 static void add_repack_incremental_option(struct strvec *args)
518 {
519 strvec_push(args, "--no-write-bitmap-index");
520 }
521
522 bool odb_source_files_optimize_required(struct odb_source *source,
523 const struct odb_optimize_options *opts)
524 {
525 struct odb_source_files *files = odb_source_files_downcast(source);
526 struct repository *repo = source->odb->repo;
527
528 switch (opts->strategy) {
529 case ODB_OPTIMIZE_INCREMENTAL: {
530 int gc_auto_threshold = 6700;
531 int gc_auto_pack_limit = 50;
532
533 repo_config_get_int(repo, "gc.auto", &gc_auto_threshold);
534 repo_config_get_int(repo, "gc.autopacklimit", &gc_auto_pack_limit);
535
536 /*
537 * Setting gc.auto to 0 or negative can disable the
538 * automatic gc.
539 */
540 if (gc_auto_threshold <= 0)
541 return false;
542 if (!too_many_packs(files, gc_auto_pack_limit) &&
543 !too_many_loose_objects(files, gc_auto_threshold))
544 return false;
545
546 return true;
547 }
548 case ODB_OPTIMIZE_GEOMETRIC: {
549 struct pack_geometry geometry = {
550 .split_factor = 2,
551 };
552 struct pack_objects_args po_args = {
553 .local = 1,
554 };
555 struct existing_packs existing_packs = EXISTING_PACKS_INIT;
556 struct string_list kept_packs = STRING_LIST_INIT_DUP;
557 int auto_value = 6700;
558 bool ret;
559
560 repo_config_get_int(repo, "maintenance.geometric-repack.auto",
561 &auto_value);
562 if (!auto_value)
563 return false;
564 if (auto_value < 0)
565 return true;
566
567 repo_config_get_int(repo, "maintenance.geometric-repack.splitFactor",
568 &geometry.split_factor);
569
570 existing_packs.repo = repo;
571 existing_packs_collect(&existing_packs, &kept_packs);
572 pack_geometry_init(&geometry, &existing_packs, &po_args);
573 pack_geometry_split(&geometry);
574
575 /*
576 * When we'd merge at least two packs with one another we always
577 * perform the repack.
578 */
579 if (geometry.split) {
580 ret = true;
581 goto out;
582 }
583
584 /*
585 * Otherwise, we estimate the number of loose objects to determine
586 * whether we want to create a new packfile or not.
587 */
588 if (too_many_loose_objects(files, auto_value)) {
589 ret = true;
590 goto out;
591 }
592
593 ret = false;
594
595 out:
596 existing_packs_release(&existing_packs);
597 pack_geometry_release(&geometry);
598 return ret;
599 }
600 default:
601 BUG("unknown maintenance strategy '%d'", opts->strategy);
602 }
603 }
604
605 int odb_source_files_optimize(struct odb_source *source,
606 const struct odb_optimize_options *opts)
607 {
608 struct odb_source_files *files = odb_source_files_downcast(source);
609 struct repository *repo = source->odb->repo;
610 struct child_process repack_cmd = CHILD_PROCESS_INIT;
611 unsigned long big_pack_threshold = 0;
612 int gc_auto_threshold = 6700;
613 int gc_auto_pack_limit = 50;
614 int ret;
615
616 repo_config_get_int(repo, "gc.auto", &gc_auto_threshold);
617 repo_config_get_int(repo, "gc.autopacklimit", &gc_auto_pack_limit);
618 repo_config_get_ulong(repo, "gc.bigpackthreshold", &big_pack_threshold);
619
620 if (repo->repository_format_precious_objects)
621 return 0;
622
623 repack_cmd.git_cmd = 1;
624 repack_cmd.odb_to_close = repo->objects;
625
626 strvec_pushl(&repack_cmd.args, "repack", "-d", "-l", NULL);
627 if (opts->flags & ODB_OPTIMIZE_NO_REUSE_DELTAS)
628 strvec_push(&repack_cmd.args, "-f");
629 if (opts->depth > 0)
630 strvec_pushf(&repack_cmd.args, "--depth=%d", opts->depth);
631 if (opts->window > 0)
632 strvec_pushf(&repack_cmd.args, "--window=%d", opts->window);
633 if (!(opts->flags & ODB_OPTIMIZE_VERBOSE))
634 strvec_push(&repack_cmd.args, "-q");
635
636 /*
637 * There's three cases we need to consider:
638 *
639 * - If we're invoked without `--auto` we'll need to perform a full
640 * repack.
641 *
642 * - If we're invoked with `--auto` and there's too many packs, then
643 * we perform a full repack, as well.
644 *
645 * - Otherwise we perform an incremental repack.
646 */
647 switch (opts->strategy) {
648 case ODB_OPTIMIZE_INCREMENTAL:
649 if (!(opts->flags & ODB_OPTIMIZE_AUTO)) {
650 struct string_list keep_pack = STRING_LIST_INIT_NODUP;
651
652 if (opts->keep_largest_pack != -1) {
653 if (opts->keep_largest_pack)
654 find_base_packs(files, &keep_pack, 0);
655 } else if (big_pack_threshold) {
656 find_base_packs(files, &keep_pack, big_pack_threshold);
657 }
658
659 add_repack_all_option(repo, opts, &keep_pack, &repack_cmd.args);
660 string_list_clear(&keep_pack, 0);
661 } else {
662 if (too_many_packs(files, gc_auto_pack_limit)) {
663 struct string_list keep_pack = STRING_LIST_INIT_NODUP;
664
665 if (big_pack_threshold) {
666 find_base_packs(files, &keep_pack, big_pack_threshold);
667 if (keep_pack.nr >= (unsigned long) gc_auto_pack_limit) {
668 string_list_clear(&keep_pack, 0);
669 find_base_packs(files, &keep_pack, 0);
670 }
671 } else {
672 struct packed_git *p = find_base_packs(files, &keep_pack, 0);
673 uint64_t mem_have, mem_want;
674
675 mem_have = total_ram();
676 mem_want = estimate_repack_memory(files, p);
677
678 /*
679 * Only allow 1/2 of memory for pack-objects, leave
680 * the rest for the OS and other processes in the
681 * system.
682 */
683 if (!mem_have || mem_want < mem_have / 2)
684 string_list_clear(&keep_pack, 0);
685 }
686
687 add_repack_all_option(repo, opts, &keep_pack, &repack_cmd.args);
688 string_list_clear(&keep_pack, 0);
689 } else {
690 add_repack_incremental_option(&repack_cmd.args);
691 }
692 }
693
694 break;
695 case ODB_OPTIMIZE_GEOMETRIC: {
696 struct pack_geometry geometry = {
697 .split_factor = 2,
698 };
699 struct pack_objects_args po_args = {
700 .local = 1,
701 };
702 struct existing_packs existing_packs = EXISTING_PACKS_INIT;
703 struct string_list kept_packs = STRING_LIST_INIT_DUP;
704
705 repo_config_get_int(repo, "maintenance.geometric-repack.splitFactor",
706 &geometry.split_factor);
707
708 existing_packs.repo = repo;
709 existing_packs_collect(&existing_packs, &kept_packs);
710 pack_geometry_init(&geometry, &existing_packs, &po_args);
711 pack_geometry_split(&geometry);
712
713 if (geometry.split < geometry.pack_nr) {
714 strvec_pushf(&repack_cmd.args, "--geometric=%d",
715 geometry.split_factor);
716 } else {
717 add_repack_all_option(repo, opts, NULL, &repack_cmd.args);
718 }
719 if (repo->settings.core_multi_pack_index)
720 strvec_push(&repack_cmd.args, "--write-midx");
721
722 existing_packs_release(&existing_packs);
723 pack_geometry_release(&geometry);
724 break;
725 }
726 default:
727 die("unknown maintenance strategy '%d'", opts->strategy);
728 }
729
730 if (run_command(&repack_cmd)) {
731 ret = error("failed to run %s", repack_cmd.args.v[0]);
732 goto out;
733 }
734
735 /* Geometric repacking uses cruft packs, so we don't have to prune separately. */
736 if (opts->strategy != ODB_OPTIMIZE_GEOMETRIC && opts->prune_expire) {
737 struct child_process prune_cmd = CHILD_PROCESS_INIT;
738
739 strvec_pushl(&prune_cmd.args, "prune", "--expire", NULL);
740 /* run `git prune` even if using cruft packs */
741 strvec_push(&prune_cmd.args, opts->prune_expire);
742 if (!(opts->flags & ODB_OPTIMIZE_VERBOSE))
743 strvec_push(&prune_cmd.args, "--no-progress");
744 if (repo_has_promisor_remote(repo))
745 strvec_push(&prune_cmd.args,
746 "--exclude-promisor-objects");
747 prune_cmd.git_cmd = 1;
748
749 if (run_command(&prune_cmd)) {
750 ret = error("failed to run %s", prune_cmd.args.v[0]);
751 goto out;
752 }
753 }
754
755 if (opts->flags & ODB_OPTIMIZE_AUTO && too_many_loose_objects(files, gc_auto_threshold))
756 warning(_("There are too many unreachable loose objects; "
757 "run 'git prune' to remove them."));
758
759 ret = 0;
760
761 out:
762 return ret;
763 }
764
765 struct odb_pack_generator_files {
766 struct odb_pack_generator base;
767 struct child_process cp;
768 };
769
770 static int odb_pack_generator_files_finish(struct odb_pack_generator *_generator)
771 {
772 struct odb_pack_generator_files *generator =
773 (struct odb_pack_generator_files *)_generator;
774 int ret;
775
776 ret = finish_command(&generator->cp);
777 free(generator);
778
779 if (ret) {
780 /*
781 * On failure, pack-objects is expected to have written a
782 * useful error message to its standard error stream already.
783 * Death by signal is worth mentioning, though, with the
784 * exception of SIGPIPE: that is a normal occurrence when the
785 * consumer of the pack hangs up.
786 */
787 if (ret > 128 && ret - 128 == SIGPIPE)
788 return -1;
789 if (ret > 128)
790 error(_("pack-objects died of signal %d"), ret - 128);
791 return -1;
792 }
793
794 return 0;
795 }
796
797 static int odb_source_files_generate_pack(struct odb_source *source UNUSED,
798 struct odb_pack_generator **out,
799 const struct odb_generate_pack_options *opts)
800 {
801 struct odb_pack_generator_files *generator;
802 struct child_process *cp;
803 FILE *in;
804
805 CALLOC_ARRAY(generator, 1);
806 child_process_init(&generator->cp);
807 cp = &generator->cp;
808
809 /*
810 * The hook is expected to spawn "$hook git pack-objects <args...>"
811 * and to behave like git-pack-objects(1) would have. This can for
812 * example be used to serve precomputed packfiles.
813 */
814 if (opts->pack_objects_hook) {
815 strvec_push(&cp->args, opts->pack_objects_hook);
816 strvec_push(&cp->args, "git");
817 cp->use_shell = 1;
818 } else {
819 cp->git_cmd = 1;
820 }
821
822 /*
823 * The caller-provided shallow boundary overrides any shallow state
824 * that the repository itself may have, so the shallow file needs to
825 * be neutralized.
826 */
827 if (opts->shallows.nr) {
828 strvec_push(&cp->args, "--shallow-file");
829 strvec_push(&cp->args, "");
830 }
831 strvec_push(&cp->args, "pack-objects");
832 strvec_push(&cp->args, "--revs");
833 strvec_push(&cp->args, "--stdout");
834 if (opts->thin)
835 strvec_push(&cp->args, "--thin");
836 if (opts->shallow)
837 strvec_push(&cp->args, "--shallow");
838 if (opts->ofs_delta)
839 strvec_push(&cp->args, "--delta-base-offset");
840 if (opts->include_tag)
841 strvec_push(&cp->args, "--include-tag");
842 if (opts->missing_allow_promisor)
843 strvec_push(&cp->args, "--missing=allow-promisor");
844 if (opts->disable_bitmaps)
845 strvec_push(&cp->args, "--no-use-bitmap-index");
846 switch (opts->progress) {
847 case ODB_GENERATE_PACK_PROGRESS_NONE:
848 strvec_push(&cp->args, "--quiet");
849 break;
850 case ODB_GENERATE_PACK_PROGRESS_STANDARD:
851 strvec_push(&cp->args, "--progress");
852 break;
853 case ODB_GENERATE_PACK_PROGRESS_VERBOSE:
854 strvec_push(&cp->args, "--all-progress");
855 break;
856 default:
857 BUG("unknown progress option %d", opts->progress);
858 }
859 if (opts->filter_spec)
860 strvec_pushf(&cp->args, "--filter=%s", opts->filter_spec);
861 if (opts->uri_protocols)
862 for (size_t i = 0; i < opts->uri_protocols->nr; i++)
863 strvec_pushf(&cp->args, "--uri-protocol=%s",
864 opts->uri_protocols->items[i].string);
865
866 cp->in = -1;
867 cp->out = opts->pack_fd;
868 cp->err = opts->progress_fd;
869 cp->clean_on_exit = 1;
870
871 if (start_command(cp)) {
872 free(generator);
873 return error(_("could not spawn pack-objects"));
874 }
875
876 /*
877 * Feed the objects to pack-objects. This is safe to do synchronously
878 * because pack-objects consumes all of its standard input before it
879 * starts to generate the pack.
880 */
881 in = xfdopen(cp->in, "w");
882 for (size_t i = 0; i < opts->shallows.nr; i++)
883 fprintf(in, "--shallow %s\n", oid_to_hex(&opts->shallows.oid[i]));
884 for (size_t i = 0; i < opts->wants.nr; i++)
885 fprintf(in, "%s\n", oid_to_hex(&opts->wants.oid[i]));
886 fprintf(in, "--not\n");
887 for (size_t i = 0; i < opts->haves.nr; i++)
888 fprintf(in, "%s\n", oid_to_hex(&opts->haves.oid[i]));
889 fprintf(in, "\n");
890 fflush(in);
891 if (ferror(in)) {
892 error(_("error writing to pack-objects"));
893 fclose(in);
894 if (opts->pack_fd < 0)
895 close(cp->out);
896 if (opts->progress_fd < 0)
897 close(cp->err);
898 finish_command(cp);
899 free(generator);
900 return -1;
901 }
902 fclose(in);
903
904 generator->base.out = opts->pack_fd < 0 ? cp->out : -1;
905 generator->base.err = opts->progress_fd < 0 ? cp->err : -1;
906 generator->base.finish = odb_pack_generator_files_finish;
907
908 *out = &generator->base;
909 return 0;
910 }
911
912 static int odb_source_files_fsck(struct odb_source *source,
913 struct odb_fsck_options *opts)
914 {
915 struct odb_source_files *files = odb_source_files_downcast(source);
916 int ret = 0;
917
918 if (!(opts->flags & ODB_FSCK_FULL) && !source->local)
919 return 0;
920
921 ret |= odb_source_fsck(&files->loose->base, opts);
922 ret |= odb_source_fsck(&files->packed->base, opts);
923
924 return ret;
925 }
926
927 struct odb_source_files *odb_source_files_new(struct object_database *odb,
928 const char *path,
929 bool local)
930 {
931 struct odb_source_files *files;
932
933 CALLOC_ARRAY(files, 1);
934 odb_source_init(&files->base, odb, ODB_SOURCE_FILES, path, local);
935 files->loose = odb_source_loose_new(odb, path, local);
936 files->packed = odb_source_packed_new(odb, path, local);
937
938 files->base.free = odb_source_files_free;
939 files->base.close = odb_source_files_close;
940 files->base.create_on_disk = odb_source_files_create_on_disk;
941 files->base.prepare = odb_source_files_prepare;
942 files->base.fsck = odb_source_files_fsck;
943 files->base.read_object_info = odb_source_files_read_object_info;
944 files->base.read_object_stream = odb_source_files_read_object_stream;
945 files->base.for_each_object = odb_source_files_for_each_object;
946 files->base.count_objects = odb_source_files_count_objects;
947 files->base.find_abbrev_len = odb_source_files_find_abbrev_len;
948 files->base.freshen_object = odb_source_files_freshen_object;
949 files->base.write_object = odb_source_files_write_object;
950 files->base.write_object_stream = odb_source_files_write_object_stream;
951 files->base.begin_transaction = odb_source_files_begin_transaction;
952 files->base.read_alternates = odb_source_files_read_alternates;
953 files->base.write_alternate = odb_source_files_write_alternate;
954 files->base.optimize = odb_source_files_optimize;
955 files->base.optimize_required = odb_source_files_optimize_required;
956 files->base.generate_pack = odb_source_files_generate_pack;
957
958 /*
959 * Ideally, we would only ever store absolute paths in the source. This
960 * is not (yet) possible though because we access and assume relative
961 * paths in the primary ODB source in some user-facing functionality.
962 */
963 if (!is_absolute_path(path))
964 chdir_notify_register(odb_source_files_reparent, files);
965
966 return files;
967 }