Compare

aae63be1d8391401e0ad1663ed159bcd03326b0a..426d291b6095a51ee249b70a088f99cd67d4bb27

builtin/fsck: move reverse index verification into the packed source

The checks for reverse indexes live in `check_pack_rev_indexes()`, which
is hosted in "builtin/fsck.c". These checks are obviously specific to
the "packed" backend.

Move the logic into `odb_source_packed_fsck()`. As in the preceding
commit, drop the dedicated `ERROR_PACK_REV_INDEX` bit and instead use
the generic `ERROR_OBJECT` bit.

Note that this changes behaviour in two ways:

  - The checks are now skipped when "--connectivity-only" was passed.
    This is because we don't even run `odb_fsck()` at all when that
    flag has been passed by the user, and not verifying data structures
    of the object database matches the documented intent of that flag,
    which is to only check the connectivity of reachable objects.

  - The checks are now skipped for non-local sources when "--no-full"
    was passed. This is, again, in line with the documented intent of
    that flag.

Add a test to cast these semantics into stone.

Signed-off-by: Patrick Steinhardt <ps@pks.im>
Signed-off-by: Junio C Hamano <gitster@pobox.com>

Patrick Steinhardt <ps@pks.im> · 2026-09-11 13:27 UTC · committed by Junio C Hamano

Browse files
62 files changed, +1209 −609
modified Documentation/RelNotes/2.56.0.adoc
@@ -117,6 +117,10 @@ UI, Workflows & Features
117 117 is now terminated with a newline so that Perl avoids appending its
118 118 internal source location data.
119 119
120 + * The '--shallow-file' option of 'git' command requires a value, but the
121 + code did not check the presence of a value and instead segfaulted
122 + without one, which has been corrected.
123 +
120 124
121 125 Performance, Internal Implementation, Development Support etc.
122 126 --------------------------------------------------------------
@@ -407,6 +411,14 @@ Performance, Internal Implementation, Development Support etc.
407 411 early when one side's exclusive commits in the queue are exhausted,
408 412 yielding significant speedups for queries with one-sided histories.
409 413
414 + * A handful of code paths have been corrected to check return values
415 + from functions like curl_easy_duphandle(), deflateInit(), lseek(),
416 + dup(), and strbuf_getline_lf(), resolving several Coverity warnings
417 + about unchecked returns.
418 +
419 + * The setting of a now-unused member '.pretty_given' in the sequencer
420 + machinery has been removed.
421 +
410 422
411 423 Fixes since v2.55
412 424 -----------------
@@ -638,3 +650,22 @@ Fixes since v2.55
638 650 replace outdated terminology, define key terms upfront, and document
639 651 how comment lines in the input are treated.
640 652 (merge 4515c86fd9 kh/doc-trailers later to maint).
653 +
654 + * The 'pack-objects' and delta-encoding code paths have been updated to
655 + use 'size_t' instead of 'unsigned long' for object sizes and offset
656 + limits, avoiding potential truncation issues on 64-bit Windows.
657 + (merge d50ac11724 js/pack-objects-delta-size-t later to maint).
658 +
659 + * A client requesting the promisor-remote capability without a value
660 + caused a null pointer dereference, which has been corrected by
661 + rejecting a request without an argument.
662 + (merge dd6b35ff71 en/serve-promisor-remote-fix later to maint).
663 +
664 + * Various tests in 't7900-maintenance.sh' have been updated to use a
665 + throwaway repository, and auto-detaching of maintenance tasks is now
666 + disabled for these tests to fix flaky races with concurrent background
667 + maintenance jobs.
668 + (merge 2775d8bcd1 ps/t7900-deflake-maintenance later to maint).
669 +
670 + * The help text for the '-l' option of 'git diff' has been updated.
671 + (merge 764243bdf4 en/diff-l-opt-help later to maint).
modified archive-zip.c
@@ -206,7 +206,7 @@ static void *zlib_deflate_raw(void *data, unsigned long size,
206 206 unsigned long *compressed_size)
207 207 {
208 208 git_zstream stream;
209 - unsigned long maxsize;
209 + size_t maxsize;
210 210 void *buffer;
211 211 int result;
212 212
modified bisect.c
@@ -1020,10 +1020,12 @@ void read_bisect_terms(char **read_bad, char **read_good)
1020 1020 die_errno(_("could not read file '%s'"), filename);
1021 1021 }
1022 1022 } else {
1023 - strbuf_getline_lf(&str, fp);
1023 + if (strbuf_getline_lf(&str, fp) == EOF)
1024 + die(_("could not read bad term from file '%s'"), filename);
1024 1025 free(*read_bad);
1025 1026 *read_bad = strbuf_detach(&str, NULL);
1026 - strbuf_getline_lf(&str, fp);
1027 + if (strbuf_getline_lf(&str, fp) == EOF)
1028 + die(_("could not read good term from file '%s'"), filename);
1027 1029 free(*read_good);
1028 1030 *read_good = strbuf_detach(&str, NULL);
1029 1031 }
modified builtin/bisect.c
@@ -566,7 +566,7 @@ static int bisect_next_check(const struct bisect_terms *terms,
566 566 return decide_next(terms, current_term, !state.nr_good, !state.nr_bad);
567 567 }
568 568
569 - static int get_terms(struct bisect_terms *terms)
569 + static int get_terms(struct bisect_terms *terms, int file_missing_is_ok)
570 570 {
571 571 struct strbuf str = STRBUF_INIT;
572 572 FILE *fp = NULL;
@@ -574,14 +574,21 @@ static int get_terms(struct bisect_terms *terms)
574 574
575 575 fp = fopen(git_path_bisect_terms(), "r");
576 576 if (!fp) {
577 - res = -1;
577 + res = file_missing_is_ok ? 0 : -1;
578 578 goto finish;
579 579 }
580 580
581 581 free_terms(terms);
582 - strbuf_getline_lf(&str, fp);
582 + if (strbuf_getline_lf(&str, fp) == EOF) {
583 + res = -1;
584 + goto finish;
585 + }
583 586 terms->term_bad = strbuf_detach(&str, NULL);
584 - strbuf_getline_lf(&str, fp);
587 + if (strbuf_getline_lf(&str, fp) == EOF) {
588 + res = -1;
589 + FREE_AND_NULL(terms->term_bad);
590 + goto finish;
591 + }
585 592 terms->term_good = strbuf_detach(&str, NULL);
586 593
587 594 finish:
@@ -593,7 +600,7 @@ finish:
593 600
594 601 static int bisect_terms(struct bisect_terms *terms, const char *option)
595 602 {
596 - if (get_terms(terms))
603 + if (get_terms(terms, 0))
597 604 return error(_("no terms defined"));
598 605
599 606 if (!option) {
@@ -1157,7 +1164,8 @@ static int process_replay_line(struct bisect_terms *terms, struct strbuf *line)
1157 1164 rev = word_end + strspn(word_end, " \t");
1158 1165 *word_end = '\0'; /* NUL-terminate the word */
1159 1166
1160 - get_terms(terms);
1167 + if (get_terms(terms, 1))
1168 + return error(_("no terms defined"));
1161 1169 if (check_and_set_terms(terms, p))
1162 1170 return -1;
1163 1171
@@ -1430,7 +1438,14 @@ static int bisect_run(struct bisect_terms *terms, int argc, const char **argv)
1430 1438
1431 1439 fflush(stdout);
1432 1440 saved_stdout = dup(1);
1433 - dup2(temporary_stdout_fd, 1);
1441 + if (saved_stdout < 0 ||
1442 + dup2(temporary_stdout_fd, 1) < 0) {
1443 + res = error_errno(_("could not duplicate stdout"));
1444 + if (saved_stdout >= 0)
1445 + close(saved_stdout);
1446 + close(temporary_stdout_fd);
1447 + break;
1448 + }
1434 1449
1435 1450 res = bisect_state(terms, 1, &new_state);
1436 1451
@@ -1510,7 +1525,8 @@ static int cmd_bisect__next(int argc, const char **argv UNUSED, const char *pref
1510 1525 if (argc)
1511 1526 return error(_("'%s' requires 0 arguments"),
1512 1527 "git bisect next");
1513 - get_terms(&terms);
1528 + if (get_terms(&terms, 1))
1529 + return error(_("no terms defined"));
1514 1530 res = bisect_next(&terms, prefix);
1515 1531 free_terms(&terms);
1516 1532 return res;
@@ -1544,7 +1560,8 @@ static int cmd_bisect__skip(int argc, const char **argv, const char *prefix UNUS
1544 1560 struct bisect_terms terms = { 0 };
1545 1561
1546 1562 set_terms(&terms, "bad", "good");
1547 - get_terms(&terms);
1563 + if (get_terms(&terms, 1))
1564 + return error(_("no terms defined"));
1548 1565 res = bisect_skip(&terms, argc, argv);
1549 1566 free_terms(&terms);
1550 1567 return res;
@@ -1556,7 +1573,8 @@ static int cmd_bisect__visualize(int argc, const char **argv, const char *prefix
1556 1573 int res;
1557 1574 struct bisect_terms terms = { 0 };
1558 1575
1559 - get_terms(&terms);
1576 + if (get_terms(&terms, 1))
1577 + return error(_("no terms defined"));
1560 1578 res = bisect_visualize(&terms, argc, argv);
1561 1579 free_terms(&terms);
1562 1580 return res;
@@ -1570,7 +1588,8 @@ static int cmd_bisect__run(int argc, const char **argv, const char *prefix UNUSE
1570 1588
1571 1589 if (!argc)
1572 1590 return error(_("'%s' failed: no command provided."), "git bisect run");
1573 - get_terms(&terms);
1591 + if (get_terms(&terms, 1))
1592 + return error(_("no terms defined"));
1574 1593 res = bisect_run(&terms, argc, argv);
1575 1594 free_terms(&terms);
1576 1595 return res;
@@ -1609,7 +1628,8 @@ int cmd_bisect(int argc,
1609 1628 usage_with_options(git_bisect_usage, options);
1610 1629
1611 1630 set_terms(&terms, "bad", "good");
1612 - get_terms(&terms);
1631 + if (get_terms(&terms, 1))
1632 + return error(_("no terms defined"));
1613 1633 if (check_and_set_terms(&terms, argv[0]) ||
1614 1634 !one_of(argv[0], terms.term_good, terms.term_bad, NULL))
1615 1635 usage_msg_optf(_("unknown command: '%s'"), git_bisect_usage,
modified builtin/bundle.c
@@ -68,32 +68,25 @@ static int parse_options_cmd_bundle(int argc,
68 68 }
69 69
70 70 static int cmd_bundle_create(int argc, const char **argv, const char *prefix,
71 - struct repository *repo UNUSED) {
72 - struct strvec pack_opts = STRVEC_INIT;
71 + struct repository *repo UNUSED)
72 + {
73 + int progress = isatty(STDERR_FILENO);
73 74 int version = -1;
74 - int ret;
75 75 struct option options[] = {
76 - OPT_PASSTHRU_ARGV('q', "quiet", &pack_opts, NULL,
77 - N_("do not show progress meter"),
78 - PARSE_OPT_NOARG),
79 - OPT_PASSTHRU_ARGV(0, "progress", &pack_opts, NULL,
80 - N_("show progress meter"),
81 - PARSE_OPT_NOARG),
82 - OPT_PASSTHRU_ARGV(0, "all-progress", &pack_opts, NULL,
83 - N_("historical; same as --progress"),
84 - PARSE_OPT_NOARG | PARSE_OPT_HIDDEN),
85 - OPT_PASSTHRU_ARGV(0, "all-progress-implied", &pack_opts, NULL,
86 - N_("historical; does nothing"),
87 - PARSE_OPT_NOARG | PARSE_OPT_HIDDEN),
76 + OPT_NEGBIT('q', "quiet", &progress,
77 + N_("do not show progress meter"), 1),
78 + OPT_BIT(0, "progress", &progress,
79 + N_("show progress meter"), 1),
80 + OPT_BIT_F(0, "all-progress", &progress,
81 + N_("historical; same as --progress"), 1,
82 + PARSE_OPT_HIDDEN),
83 + OPT_NOOP_NOARG(0, "all-progress-implied"),
88 84 OPT_INTEGER(0, "version", &version,
89 85 N_("specify bundle format version")),
90 86 OPT_END()
91 87 };
92 88 char *bundle_file;
93 -
94 - if (isatty(STDERR_FILENO))
95 - strvec_push(&pack_opts, "--progress");
96 - strvec_push(&pack_opts, "--all-progress-implied");
89 + int ret;
97 90
98 91 argc = parse_options_cmd_bundle(argc, argv, prefix,
99 92 builtin_bundle_create_usage, options, &bundle_file);
@@ -101,8 +94,7 @@ static int cmd_bundle_create(int argc, const char **argv, const char *prefix,
101 94
102 95 if (!startup_info->have_repository)
103 96 die(_("Need a repository to create a bundle."));
104 - ret = !!create_bundle(the_repository, bundle_file, argc, argv, &pack_opts, version);
105 - strvec_clear(&pack_opts);
97 + ret = !!create_bundle(the_repository, bundle_file, argc, argv, version, progress);
106 98 free(bundle_file);
107 99 return ret;
108 100 }
modified builtin/config.c
@@ -1313,7 +1313,10 @@ static int show_editor(struct config_location_options *opts)
1313 1313 else if (errno != EEXIST)
1314 1314 die_errno(_("cannot create configuration file %s"), config_file);
1315 1315 }
1316 - launch_editor(config_file, NULL, NULL);
1316 + if (launch_editor(config_file, NULL, NULL)) {
1317 + free(config_file);
1318 + return -1;
1319 + }
1317 1320 free(config_file);
1318 1321
1319 1322 return 0;
modified builtin/fast-import.c
@@ -980,7 +980,7 @@ static int store_object(
980 980 struct object_entry *e;
981 981 unsigned char hdr[96];
982 982 struct object_id oid;
983 - unsigned long hdrlen, deltalen;
983 + unsigned long hdrlen, deltalen = 0;
984 984 struct git_hash_ctx c;
985 985 git_zstream s;
986 986 struct repo_config_values *cfg = repo_config_values(the_repository);
@@ -1016,11 +1016,13 @@ static int store_object(
1016 1016
1017 1017 if (last && last->data.len && last->data.buf && last->depth < max_depth
1018 1018 && dat->len > the_hash_algo->rawsz) {
1019 + size_t deltalen_st;
1019 1020
1020 1021 delta_count_attempts_by_type[type]++;
1021 1022 delta = diff_delta(last->data.buf, last->data.len,
1022 1023 dat->buf, dat->len,
1023 - &deltalen, dat->len - the_hash_algo->rawsz);
1024 + &deltalen_st, dat->len - the_hash_algo->rawsz);
1025 + deltalen = cast_size_t_to_ulong(deltalen_st);
1024 1026 } else
1025 1027 delta = NULL;
1026 1028
modified builtin/fsck.c
@@ -7,7 +7,6 @@
7 7 #include "blob.h"
8 8 #include "tag.h"
9 9 #include "refs.h"
10 - #include "pack.h"
11 10 #include "cache-tree.h"
12 11 #include "fsck.h"
13 12 #include "parse-options.h"
@@ -24,7 +23,6 @@
24 23 #include "run-command.h"
25 24 #include "sparse-index.h"
26 25 #include "worktree.h"
27 - #include "pack-revindex.h"
28 26 #include "pack-bitmap.h"
29 27
30 28 #define REACHABLE 0x0001
@@ -37,10 +35,8 @@ static int show_root;
37 35 static int show_tags;
38 36 static int show_unreachable;
39 37 static int include_reflogs = 1;
40 - static int check_full = 1;
41 38 static int connectivity_only;
42 39 static int check_strict;
43 - static int keep_cache_objects;
44 40 static struct fsck_options fsck_walk_options;
45 41 static struct fsck_options fsck_obj_options;
46 42 static int errors_found;
@@ -48,16 +44,12 @@ static int write_lost_and_found;
48 44 static int verbose;
49 45 static int show_progress = -1;
50 46 static int show_dangling = 1;
51 - static int name_objects;
52 - static int check_references = 1;
53 47 static timestamp_t now;
54 48 #define ERROR_OBJECT 01
55 49 #define ERROR_REACHABLE 02
56 - #define ERROR_PACK 04
57 50 #define ERROR_REFS 010
58 51 #define ERROR_COMMIT_GRAPH 020
59 52 #define ERROR_MULTI_PACK_INDEX 040
60 - #define ERROR_PACK_REV_INDEX 0100
61 53 #define ERROR_BITMAP 0200
62 54
63 55 static const char *describe_object(const struct object_id *oid)
@@ -401,14 +393,27 @@ static void check_connectivity(struct repository *repo)
401 393 }
402 394 }
403 395
404 - static int fsck_obj(struct repository *repo,
405 - struct object *obj, void *buffer, unsigned long size)
396 + static int fsck_obj_buffer(const struct object_id *oid, enum object_type type,
397 + unsigned long size, void *buffer, int *eaten, void *cb_data)
406 398 {
399 + struct repository *repo = cb_data;
400 + struct object *obj;
407 401 int err;
408 402
409 - if (obj->flags & SEEN)
410 - return 0;
411 - obj->flags |= SEEN;
403 + /*
404 + * Note, buffer may be NULL if type is OBJ_BLOB. See
405 + * verify_packfile(), data_valid variable for details.
406 + */
407 + obj = parse_object_buffer(repo, oid, type, size, buffer, eaten);
408 + if (!obj) {
409 + errors_found |= ERROR_OBJECT;
410 + err = error(_("%s: object corrupt or missing"),
411 + oid_to_hex(oid));
412 + goto out;
413 + }
414 +
415 + obj->flags &= ~REACHABLE;
416 + obj->flags |= HAS_OBJ | SEEN;
412 417
413 418 if (verbose)
414 419 fprintf_ln(stderr, _("Checking %s %s"),
@@ -417,6 +422,7 @@ static int fsck_obj(struct repository *repo,
417 422
418 423 if (fsck_walk(obj, NULL, &fsck_obj_options))
419 424 objerror(repo, obj, _("broken links"));
425 +
420 426 err = fsck_object(obj, buffer, size, &fsck_obj_options);
421 427 if (err)
422 428 goto out;
@@ -442,32 +448,11 @@ static int fsck_obj(struct repository *repo,
442 448 }
443 449
444 450 out:
445 - if (obj->type == OBJ_TREE)
451 + if (obj && obj->type == OBJ_TREE)
446 452 free_tree_buffer((struct tree *)obj);
447 453 return err;
448 454 }
449 455
450 - static int fsck_obj_buffer(const struct object_id *oid, enum object_type type,
451 - unsigned long size, void *buffer, int *eaten, void *cb_data)
452 - {
453 - struct repository *repo = cb_data;
454 - struct object *obj;
455 -
456 - /*
457 - * Note, buffer may be NULL if type is OBJ_BLOB. See
458 - * verify_packfile(), data_valid variable for details.
459 - */
460 - obj = parse_object_buffer(repo, oid, type, size, buffer, eaten);
461 - if (!obj) {
462 - errors_found |= ERROR_OBJECT;
463 - return error(_("%s: object corrupt or missing"),
464 - oid_to_hex(oid));
465 - }
466 - obj->flags &= ~(REACHABLE | SEEN);
467 - obj->flags |= HAS_OBJ;
468 - return fsck_obj(repo, obj, buffer, size);
469 - }
470 -
471 456 static int default_refs;
472 457
473 458 static void fsck_handle_reflog_oid(struct repository *repo,
@@ -722,7 +707,6 @@ static int fsck_loose(const struct object_id *oid, const char *path,
722 707 void *cb_data)
723 708 {
724 709 struct for_each_loose_cb *data = cb_data;
725 - struct object *obj;
726 710 enum object_type type = OBJ_NONE;
727 711 size_t size;
728 712 void *contents = NULL;
@@ -751,21 +735,7 @@ static int fsck_loose(const struct object_id *oid, const char *path,
751 735 if (!contents && type != OBJ_BLOB)
752 736 BUG("read_loose_object streamed a non-blob");
753 737
754 - obj = parse_object_buffer(data->repo, oid, type, size,
755 - contents, &eaten);
756 -
757 - if (!obj) {
758 - errors_found |= ERROR_OBJECT;
759 - error(_("%s: object could not be parsed: %s"),
760 - oid_to_hex(oid), path);
761 - if (!eaten)
762 - free(contents);
763 - return 0; /* keep checking other objects */
764 - }
765 -
766 - obj->flags &= ~(REACHABLE | SEEN);
767 - obj->flags |= HAS_OBJ;
768 - if (fsck_obj(data->repo, obj, contents, size))
738 + if (fsck_obj_buffer(oid, type, size, contents, &eaten, data->repo))
769 739 errors_found |= ERROR_OBJECT;
770 740
771 741 if (!eaten)
@@ -918,40 +888,6 @@ static int mark_object_for_connectivity(const struct object_id *oid,
918 888 return 0;
919 889 }
920 890
921 - static int check_pack_rev_indexes(struct repository *r, int show_progress)
922 - {
923 - struct progress *progress = NULL;
924 - struct packed_git *p;
925 - uint32_t pack_count = 0;
926 - int res = 0;
927 -
928 - if (show_progress) {
929 - repo_for_each_pack(r, p)
930 - pack_count++;
931 - progress = start_delayed_progress(r,
932 - "Verifying reverse pack-indexes", pack_count);
933 - pack_count = 0;
934 - }
935 -
936 - repo_for_each_pack(r, p) {
937 - int load_error = load_pack_revindex_from_disk(p);
938 -
939 - if (load_error < 0) {
940 - error(_("unable to load rev-index for pack '%s'"), p->pack_name);
941 - res = ERROR_PACK_REV_INDEX;
942 - } else if (!load_error &&
943 - !load_pack_revindex(r, p) &&
944 - verify_pack_revindex(p)) {
945 - error(_("invalid rev-index for pack '%s'"), p->pack_name);
946 - res = ERROR_PACK_REV_INDEX;
947 - }
948 - display_progress(progress, ++pack_count);
949 - }
950 - stop_progress(&progress);
951 -
952 - return res;
953 - }
954 -
955 891 static void fsck_refs(struct repository *r)
956 892 {
957 893 struct child_process refs_verify = CHILD_PROCESS_INIT;
@@ -986,30 +922,38 @@ static char const * const fsck_usage[] = {
986 922 NULL
987 923 };
988 924
989 - static struct option fsck_opts[] = {
990 - OPT__VERBOSE(&verbose, N_("be verbose")),
991 - OPT_BOOL(0, "unreachable", &show_unreachable, N_("show unreachable objects")),
992 - OPT_BOOL(0, "dangling", &show_dangling, N_("show dangling objects")),
993 - OPT_BOOL(0, "tags", &show_tags, N_("report tags")),
994 - OPT_BOOL(0, "root", &show_root, N_("report root nodes")),
995 - OPT_BOOL(0, "cache", &keep_cache_objects, N_("make index objects head nodes")),
996 - OPT_BOOL(0, "reflogs", &include_reflogs, N_("make reflogs head nodes (default)")),
997 - OPT_BOOL(0, "full", &check_full, N_("also consider packs and alternate objects")),
998 - OPT_BOOL(0, "connectivity-only", &connectivity_only, N_("check only connectivity")),
999 - OPT_BOOL(0, "strict", &check_strict, N_("enable more strict checking")),
1000 - OPT_BOOL(0, "lost-found", &write_lost_and_found,
1001 - N_("write dangling objects in .git/lost-found")),
1002 - OPT_BOOL(0, "progress", &show_progress, N_("show progress")),
1003 - OPT_BOOL(0, "name-objects", &name_objects, N_("show verbose names for reachable objects")),
1004 - OPT_BOOL(0, "references", &check_references, N_("check reference database consistency")),
1005 - OPT_END(),
1006 - };
1007 -
1008 925 int cmd_fsck(int argc,
1009 926 const char **argv,
1010 927 const char *prefix,
1011 928 struct repository *repo)
1012 929 {
930 + struct odb_fsck_options odb_fsck_opts = {
931 + .flags = ODB_FSCK_FULL,
932 + .object_cb = fsck_obj_buffer,
933 + .object_payload = repo,
934 + };
935 + int keep_cache_objects = 0;
936 + int name_objects = 0;
937 + int check_references = 1;
938 + struct option fsck_opts[] = {
939 + OPT__VERBOSE(&verbose, N_("be verbose")),
940 + OPT_BOOL(0, "unreachable", &show_unreachable, N_("show unreachable objects")),
941 + OPT_BOOL(0, "dangling", &show_dangling, N_("show dangling objects")),
942 + OPT_BOOL(0, "tags", &show_tags, N_("report tags")),
943 + OPT_BOOL(0, "root", &show_root, N_("report root nodes")),
944 + OPT_BOOL(0, "cache", &keep_cache_objects, N_("make index objects head nodes")),
945 + OPT_BOOL(0, "reflogs", &include_reflogs, N_("make reflogs head nodes (default)")),
946 + OPT_BIT(0, "full", &odb_fsck_opts.flags,
947 + N_("also consider packs and alternate objects"), ODB_FSCK_FULL),
948 + OPT_BOOL(0, "connectivity-only", &connectivity_only, N_("check only connectivity")),
949 + OPT_BOOL(0, "strict", &check_strict, N_("enable more strict checking")),
950 + OPT_BOOL(0, "lost-found", &write_lost_and_found,
951 + N_("write dangling objects in .git/lost-found")),
952 + OPT_BOOL(0, "progress", &show_progress, N_("show progress")),
953 + OPT_BOOL(0, "name-objects", &name_objects, N_("show verbose names for reachable objects")),
954 + OPT_BOOL(0, "references", &check_references, N_("check reference database consistency")),
955 + OPT_END(),
956 + };
1013 957 struct odb_source *source;
1014 958 struct snapshot snap = {
1015 959 .nr = 0,
@@ -1039,9 +983,11 @@ int cmd_fsck(int argc,
1039 983 show_progress = isatty(2);
1040 984 if (verbose)
1041 985 show_progress = 0;
986 + if (show_progress)
987 + odb_fsck_opts.flags |= ODB_FSCK_PROGRESS;
1042 988
1043 989 if (write_lost_and_found) {
1044 - check_full = 1;
990 + odb_fsck_opts.flags |= ODB_FSCK_FULL;
1045 991 include_reflogs = 0;
1046 992 }
1047 993
@@ -1069,36 +1015,12 @@ int cmd_fsck(int argc,
1069 1015 odb_for_each_object(repo->objects, NULL,
1070 1016 mark_object_for_connectivity, repo, 0);
1071 1017 } else {
1072 - odb_prepare_alternates(repo->objects);
1073 1018 for (source = repo->objects->sources; source; source = source->next)
1074 - fsck_source(repo, source);
1075 -
1076 - if (check_full) {
1077 - struct packed_git *p;
1078 - uint32_t total = 0, count = 0;
1079 - struct progress *progress = NULL;
1080 -
1081 - if (show_progress) {
1082 - repo_for_each_pack(repo, p) {
1083 - if (open_pack_index(p))
1084 - continue;
1085 - total += p->num_objects;
1086 - }
1087 -
1088 - progress = start_progress(repo,
1089 - _("Checking objects"), total);
1090 - }
1019 + if ((odb_fsck_opts.flags & ODB_FSCK_FULL) || source->local)
1020 + fsck_source(repo, source);
1091 1021
1092 - repo_for_each_pack(repo, p) {
1093 - /* verify gives error messages itself */
1094 - if (verify_pack(repo,
1095 - p, fsck_obj_buffer, repo,
1096 - progress, count))
1097 - errors_found |= ERROR_PACK;
1098 - count += p->num_objects;
1099 - }
1100 - stop_progress(&progress);
1101 - }
1022 + if (odb_fsck(repo->objects, &odb_fsck_opts) < 0)
1023 + errors_found |= ERROR_OBJECT;
1102 1024
1103 1025 if (fsck_finish(&fsck_obj_options))
1104 1026 errors_found |= ERROR_OBJECT;
@@ -1146,7 +1068,6 @@ int cmd_fsck(int argc,
1146 1068 free_worktrees(worktrees);
1147 1069 }
1148 1070
1149 - errors_found |= check_pack_rev_indexes(repo, show_progress);
1150 1071 if (verify_bitmap_files(repo))
1151 1072 errors_found |= ERROR_BITMAP;
1152 1073
@@ -1155,7 +1076,6 @@ int cmd_fsck(int argc,
1155 1076 if (repo->settings.core_commit_graph) {
1156 1077 struct child_process commit_graph_verify = CHILD_PROCESS_INIT;
1157 1078
1158 - odb_prepare_alternates(repo->objects);
1159 1079 for (source = repo->objects->sources; source; source = source->next) {
1160 1080 child_process_init(&commit_graph_verify);
1161 1081 commit_graph_verify.git_cmd = 1;
@@ -1173,7 +1093,6 @@ int cmd_fsck(int argc,
1173 1093 if (repo->settings.core_multi_pack_index) {
1174 1094 struct child_process midx_verify = CHILD_PROCESS_INIT;
1175 1095
1176 - odb_prepare_alternates(repo->objects);
1177 1096 for (source = repo->objects->sources; source; source = source->next) {
1178 1097 child_process_init(&midx_verify);
1179 1098 midx_verify.git_cmd = 1;
modified builtin/last-modified.c
@@ -290,7 +290,8 @@ static void process_parent(struct last_modified *lm,
290 290 {
291 291 struct bitmap *active_p;
292 292
293 - repo_parse_commit(lm->rev.repo, parent);
293 + if (repo_parse_commit(lm->rev.repo, parent))
294 + return;
294 295 active_p = active_paths_for(lm, parent);
295 296
296 297 /*
@@ -414,12 +415,14 @@ static int last_modified_run(struct last_modified *lm)
414 415 * Otherwise, make sure that 'c' isn't reachable from anything
415 416 * in the '--not' queue.
416 417 */
417 - repo_parse_commit(lm->rev.repo, c);
418 + if (repo_parse_commit(lm->rev.repo, c))
419 + goto cleanup;
418 420
419 421 while ((n = prio_queue_get(&not_queue))) {
420 422 struct commit_list *np;
421 423
422 - repo_parse_commit(lm->rev.repo, n);
424 + if (repo_parse_commit(lm->rev.repo, n))
425 + continue;
423 426
424 427 for (np = n->parents; np; np = np->next) {
425 428 if (!(np->item->object.flags & PARENT2)) {
modified builtin/pack-objects.c
@@ -261,8 +261,8 @@ static int exclude_promisor_objects_best_effort;
261 261
262 262 static int use_delta_islands;
263 263
264 - static unsigned long delta_cache_size = 0;
265 - static unsigned long max_delta_cache_size = DEFAULT_DELTA_CACHE_SIZE;
264 + static size_t delta_cache_size = 0;
265 + static size_t max_delta_cache_size = DEFAULT_DELTA_CACHE_SIZE;
266 266 static unsigned long cache_max_small_delta_size = 1000;
267 267
268 268 static unsigned long window_memory_limit = 0;
@@ -354,7 +354,8 @@ static void index_commit_for_bitmap(struct commit *commit)
354 354
355 355 static void *get_delta(struct object_entry *entry)
356 356 {
357 - unsigned long size, base_size, delta_size;
357 + unsigned long size, base_size;
358 + size_t delta_size;
358 359 void *buf, *base_buf, *delta_buf;
359 360 enum object_type type;
360 361 size_t size_st = 0, base_size_st = 0;
@@ -488,7 +489,7 @@ static void copy_pack_data(struct hashfile *f,
488 489 off_t len)
489 490 {
490 491 unsigned char *in;
491 - unsigned long avail;
492 + size_t avail;
492 493
493 494 while (len) {
494 495 in = use_pack(p, w_curs, offset, &avail);
@@ -1779,8 +1780,6 @@ static int want_object_in_pack_mtime(const struct object_id *oid,
1779 1780 *found_offset = 0;
1780 1781 }
1781 1782
1782 - odb_prepare_alternates(the_repository->objects);
1783 -
1784 1783 for (source = the_repository->objects->sources; source; source = source->next) {
1785 1784 struct odb_source_files *files = odb_source_files_downcast(source);
1786 1785 struct multi_pack_index *m = get_multi_pack_index(files->packed);
@@ -2259,8 +2258,7 @@ static void check_object(struct object_entry *entry, uint32_t object_index)
2259 2258 int have_base = 0;
2260 2259 struct object_id base_ref;
2261 2260 struct object_entry *base_entry;
2262 - unsigned long used, used_0;
2263 - unsigned long avail;
2261 + size_t used, used_0, avail;
2264 2262 off_t ofs;
2265 2263 unsigned char *buf, c;
2266 2264 enum object_type type;
@@ -2688,8 +2686,8 @@ struct unpacked {
2688 2686 unsigned depth;
2689 2687 };
2690 2688
2691 - static int delta_cacheable(unsigned long src_size, unsigned long trg_size,
2692 - unsigned long delta_size)
2689 + static int delta_cacheable(size_t src_size, size_t trg_size,
2690 + size_t delta_size)
2693 2691 {
2694 2692 if (max_delta_cache_size && delta_cache_size + delta_size > max_delta_cache_size)
2695 2693 return 0;
@@ -2772,8 +2770,7 @@ size_t oe_get_size_slow(struct packing_data *pack,
2772 2770 struct pack_window *w_curs;
2773 2771 unsigned char *buf;
2774 2772 enum object_type type;
2775 - unsigned long used, avail;
2776 - size_t size;
2773 + size_t used, avail, size;
2777 2774
2778 2775 if (e->type_ != OBJ_OFS_DELTA && e->type_ != OBJ_REF_DELTA) {
2779 2776 size_t sz;
@@ -2804,11 +2801,12 @@ size_t oe_get_size_slow(struct packing_data *pack,
2804 2801 }
2805 2802
2806 2803 static int try_delta(struct unpacked *trg, struct unpacked *src,
2807 - unsigned max_depth, unsigned long *mem_usage)
2804 + unsigned max_depth, size_t *mem_usage)
2808 2805 {
2809 2806 struct object_entry *trg_entry = trg->entry;
2810 2807 struct object_entry *src_entry = src->entry;
2811 - unsigned long trg_size, src_size, delta_size, sizediff, max_size, sz;
2808 + unsigned long trg_size, src_size, sizediff, max_size, sz;
2809 + size_t delta_size;
2812 2810 unsigned ref_depth;
2813 2811 enum object_type type;
2814 2812 void *delta_buf;
@@ -2972,9 +2970,9 @@ static unsigned int check_delta_limit(struct object_entry *me, unsigned int n)
2972 2970 return m;
2973 2971 }
2974 2972
2975 - static unsigned long free_unpacked(struct unpacked *n)
2973 + static size_t free_unpacked(struct unpacked *n)
2976 2974 {
2977 - unsigned long freed_mem = sizeof_delta_index(n->index);
2975 + size_t freed_mem = sizeof_delta_index(n->index);
2978 2976 free_delta_index(n->index);
2979 2977 n->index = NULL;
2980 2978 if (n->data) {
@@ -2991,7 +2989,7 @@ static void find_deltas(struct object_entry **list, unsigned *list_size,
2991 2989 {
2992 2990 uint32_t i, idx = 0, count = 0;
2993 2991 struct unpacked *array;
2994 - unsigned long mem_usage = 0;
2992 + size_t mem_usage = 0;
2995 2993
2996 2994 CALLOC_ARRAY(array, window);
2997 2995
@@ -3701,7 +3699,7 @@ static int git_pack_config(const char *k, const char *v,
3701 3699 return 0;
3702 3700 }
3703 3701 if (!strcmp(k, "pack.deltacachesize")) {
3704 - max_delta_cache_size = git_config_int(k, v, ctx->kvi);
3702 + max_delta_cache_size = git_config_size_t(k, v, ctx->kvi);
3705 3703 return 0;
3706 3704 }
3707 3705 if (!strcmp(k, "pack.deltacachelimit")) {
@@ -4520,7 +4518,6 @@ static void add_objects_in_unpacked_packs(void)
4520 4518 .source_infop = &source_info,
4521 4519 };
4522 4520
4523 - odb_prepare_alternates(to_pack.repo->objects);
4524 4521 for (source = to_pack.repo->objects->sources; source; source = source->next) {
4525 4522 struct odb_source_files *files = odb_source_files_downcast(source);
4526 4523
modified bundle.c
@@ -1,4 +1,3 @@
1 - #define USE_THE_REPOSITORY_VARIABLE
2 1 #define DISABLE_SIGN_COMPARE_WARNINGS
3 2
4 3 #include "git-compat-util.h"
@@ -21,6 +20,13 @@
21 20 #include "connected.h"
22 21 #include "write-or-die.h"
23 22
23 + /*
24 + * NEEDSWORK: this function implicitly depends on `the_repository` and is not
25 + * available because we dropped USE_THE_REPOSITORY_VARIABLE. We can remove the
26 + * declaration once it's accessible via `repo_config_values`.
27 + */
28 + extern const char *get_log_output_encoding(void);
29 +
24 30 static const char v2_bundle_signature[] = "# v2 git bundle\n";
25 31 static const char v3_bundle_signature[] = "# v3 git bundle\n";
26 32 static struct {
@@ -294,7 +300,8 @@ int list_bundle_refs(struct bundle_header *header, int argc, const char **argv)
294 300 return list_refs(&header->references, argc, argv);
295 301 }
296 302
297 - static int is_tag_in_date_range(struct object *tag, struct rev_info *revs)
303 + static int is_tag_in_date_range(struct repository *repo,
304 + struct object *tag, struct rev_info *revs)
298 305 {
299 306 size_t size;
300 307 enum object_type type;
@@ -305,7 +312,7 @@ static int is_tag_in_date_range(struct object *tag, struct rev_info *revs)
305 312 if (revs->max_age == -1 && revs->min_age == -1)
306 313 goto out;
307 314
308 - buf = odb_read_object(the_repository->objects, &tag->oid, &type, &size);
315 + buf = odb_read_object(repo->objects, &tag->oid, &type, &size);
309 316 if (!buf)
310 317 goto out;
311 318 line = memmem(buf, size, "\ntagger ", 8);
@@ -325,50 +332,52 @@ out:
325 332
326 333
327 334 /* Write the pack data to bundle_fd */
328 - static int write_pack_data(int bundle_fd, struct rev_info *revs, struct strvec *pack_options)
335 + static int write_pack_data(int bundle_fd, struct rev_info *revs, int progress)
329 336 {
330 - struct child_process pack_objects = CHILD_PROCESS_INIT;
337 + struct odb_generate_pack_options opts = ODB_GENERATE_PACK_OPTIONS_INIT;
338 + struct odb_pack_generator *generator;
339 + int ret = 0;
331 340 int i;
332 341
333 - strvec_pushl(&pack_objects.args,
334 - "pack-objects",
335 - "--stdout", "--thin", "--delta-base-offset",
336 - NULL);
337 - strvec_pushv(&pack_objects.args, pack_options->v);
342 + opts.thin = 1;
343 + opts.ofs_delta = 1;
344 + if (progress)
345 + opts.progress = ODB_GENERATE_PACK_PROGRESS_VERBOSE;
338 346 if (revs->filter.choice)
339 - strvec_pushf(&pack_objects.args, "--filter=%s",
340 - list_objects_filter_spec(&revs->filter));
341 - pack_objects.in = -1;
342 - pack_objects.out = bundle_fd;
343 - pack_objects.git_cmd = 1;
347 + opts.filter_spec = list_objects_filter_spec(&revs->filter);
344 348
345 349 /*
346 - * start_command() will close our descriptor if it's >1. Duplicate it
347 - * to avoid surprising the caller.
350 + * The pack generator will consume our descriptor if it's >1.
351 + * Duplicate it to avoid surprising the caller.
348 352 */
349 - if (pack_objects.out > 1) {
350 - pack_objects.out = dup(pack_objects.out);
351 - if (pack_objects.out < 0) {
352 - error_errno(_("unable to dup bundle descriptor"));
353 - child_process_clear(&pack_objects);
354 - return -1;
355 - }
353 + opts.pack_fd = bundle_fd;
354 + if (opts.pack_fd > 1) {
355 + opts.pack_fd = dup(bundle_fd);
356 + if (opts.pack_fd < 0)
357 + return error_errno(_("unable to dup bundle descriptor"));
356 358 }
357 359
358 - if (start_command(&pack_objects))
359 - return error(_("Could not spawn pack-objects"));
360 -
361 360 for (i = 0; i < revs->pending.nr; i++) {
362 361 struct object *object = revs->pending.objects[i].item;
363 362 if (object->flags & UNINTERESTING)
364 - write_or_die(pack_objects.in, "^", 1);
365 - write_or_die(pack_objects.in, oid_to_hex(&object->oid), the_hash_algo->hexsz);
366 - write_or_die(pack_objects.in, "\n", 1);
363 + oid_array_append(&opts.haves, &object->oid);
364 + else
365 + oid_array_append(&opts.wants, &object->oid);
367 366 }
368 - close(pack_objects.in);
369 - if (finish_command(&pack_objects))
370 - return error(_("pack-objects died"));
371 - return 0;
367 +
368 + if (odb_generate_pack(revs->repo->objects, &generator, &opts)) {
369 + ret = error(_("Could not spawn pack-objects"));
370 + goto out;
371 + }
372 +
373 + if (odb_pack_generator_finish(generator)) {
374 + ret = error(_("pack-objects died"));
375 + goto out;
376 + }
377 +
378 + out:
379 + odb_generate_pack_options_release(&opts);
380 + return ret;
372 381 }
373 382
374 383 /*
@@ -395,10 +404,10 @@ static int write_bundle_refs(int bundle_fd, struct rev_info *revs)
395 404
396 405 if (e->item->flags & UNINTERESTING)
397 406 continue;
398 - if (repo_dwim_ref(the_repository, e->name, strlen(e->name),
407 + if (repo_dwim_ref(revs->repo, e->name, strlen(e->name),
399 408 &oid, &ref, 0) != 1)
400 409 goto skip_write_ref;
401 - if (refs_read_ref_full(get_main_ref_store(the_repository), e->name, RESOLVE_REF_READING, &oid, &flag))
410 + if (refs_read_ref_full(get_main_ref_store(revs->repo), e->name, RESOLVE_REF_READING, &oid, &flag))
402 411 flag = 0;
403 412 display_ref = (flag & REF_ISSYMREF) ? e->name : ref;
404 413
@@ -406,7 +415,7 @@ static int write_bundle_refs(int bundle_fd, struct rev_info *revs)
406 415 goto skip_write_ref;
407 416
408 417 if (e->item->type == OBJ_TAG &&
409 - !is_tag_in_date_range(e->item, revs)) {
418 + !is_tag_in_date_range(revs->repo, e->item, revs)) {
410 419 e->item->flags |= UNINTERESTING;
411 420 goto skip_write_ref;
412 421 }
@@ -428,7 +437,8 @@ static int write_bundle_refs(int bundle_fd, struct rev_info *revs)
428 437
429 438 ref_count++;
430 439 strset_add(&objects, display_ref);
431 - write_or_die(bundle_fd, oid_to_hex(&e->item->oid), the_hash_algo->hexsz);
440 + write_or_die(bundle_fd, oid_to_hex(&e->item->oid),
441 + revs->repo->hash_algo->hexsz);
432 442 write_or_die(bundle_fd, " ", 1);
433 443 write_or_die(bundle_fd, display_ref, strlen(display_ref));
434 444 write_or_die(bundle_fd, "\n", 1);
@@ -476,7 +486,7 @@ static void write_bundle_prerequisites(struct commit *commit, void *data)
476 486 }
477 487
478 488 int create_bundle(struct repository *r, const char *path,
479 - int argc, const char **argv, struct strvec *pack_options, int version)
489 + int argc, const char **argv, int version, int progress)
480 490 {
481 491 struct lock_file lock = LOCK_INIT;
482 492 int bundle_fd = -1;
@@ -507,7 +517,7 @@ int create_bundle(struct repository *r, const char *path,
507 517 * SHA1.
508 518 * 2. @filter is required because we parsed an object filter.
509 519 */
510 - if (the_hash_algo != &hash_algos[GIT_HASH_SHA1_LEGACY] || revs.filter.choice)
520 + if (r->hash_algo != &hash_algos[GIT_HASH_SHA1_LEGACY] || revs.filter.choice)
511 521 min_version = 3;
512 522
513 523 if (argc > 1) {
@@ -528,14 +538,15 @@ int create_bundle(struct repository *r, const char *path,
528 538 if (version < 2 || version > 3) {
529 539 die(_("unsupported bundle version %d"), version);
530 540 } else if (version < min_version) {
531 - die(_("cannot write bundle version %d with algorithm %s"), version, the_hash_algo->name);
541 + die(_("cannot write bundle version %d with algorithm %s"), version,
542 + r->hash_algo->name);
532 543 } else if (version == 2) {
533 544 write_or_die(bundle_fd, v2_bundle_signature, strlen(v2_bundle_signature));
534 545 } else {
535 546 const char *capability = "@object-format=";
536 547 write_or_die(bundle_fd, v3_bundle_signature, strlen(v3_bundle_signature));
537 548 write_or_die(bundle_fd, capability, strlen(capability));
538 - write_or_die(bundle_fd, the_hash_algo->name, strlen(the_hash_algo->name));
549 + write_or_die(bundle_fd, r->hash_algo->name, strlen(r->hash_algo->name));
539 550 write_or_die(bundle_fd, "\n", 1);
540 551
541 552 if (revs.filter.choice) {
@@ -584,7 +595,7 @@ int create_bundle(struct repository *r, const char *path,
584 595 }
585 596
586 597 /* write pack */
587 - if (write_pack_data(bundle_fd, &revs_copy, pack_options)) {
598 + if (write_pack_data(bundle_fd, &revs_copy, progress)) {
588 599 ret = -1;
589 600 goto out;
590 601 }
modified bundle.h
@@ -27,8 +27,7 @@ int read_bundle_header(const char *path, struct bundle_header *header);
27 27 int read_bundle_header_fd(int fd, struct bundle_header *header,
28 28 const char *report_path);
29 29 int create_bundle(struct repository *r, const char *path,
30 - int argc, const char **argv, struct strvec *pack_options,
31 - int version);
30 + int argc, const char **argv, int version, int progress);
32 31
33 32 enum verify_bundle_flags {
34 33 VERIFY_BUNDLE_VERBOSE = (1 << 0),
modified commit-graph.c
@@ -651,8 +651,6 @@ struct commit_graph *load_commit_graph_chain_fd_st(struct object_database *odb,
651 651 count = st->st_size / (odb->repo->hash_algo->hexsz + 1);
652 652 CALLOC_ARRAY(oids, count);
653 653
654 - odb_prepare_alternates(odb);
655 -
656 654 for (i = 0; i < count; i++) {
657 655 struct odb_source *source;
658 656
@@ -768,7 +766,6 @@ static struct commit_graph *prepare_commit_graph(struct repository *r)
768 766 if (!commit_graph_compatible(r))
769 767 return NULL;
770 768
771 - odb_prepare_alternates(r->objects);
772 769 for (source = r->objects->sources; source; source = source->next) {
773 770 r->objects->commit_graph = read_commit_graph_one(source);
774 771 if (r->objects->commit_graph)
@@ -2018,7 +2015,6 @@ static void fill_oids_from_all_packs(struct write_commit_graph_context *ctx)
2018 2015 _("Finding commits for commit graph among packed objects"),
2019 2016 ctx->approx_nr_objects);
2020 2017
2021 - odb_prepare_alternates(ctx->r->objects);
2022 2018 for (source = ctx->r->objects->sources; source; source = source->next) {
2023 2019 struct odb_source_files *files = odb_source_files_downcast(source);
2024 2020 odb_source_for_each_object(&files->packed->base, &oi, add_packed_commits_oi,
modified compat/pread.c
@@ -7,6 +7,8 @@ ssize_t git_pread(int fd, void *buf, size_t count, off_t offset)
7 7 ssize_t rc;
8 8
9 9 current_offset = lseek(fd, 0, SEEK_CUR);
10 + if (current_offset < 0)
11 + return -1;
10 12
11 13 if (lseek(fd, offset, SEEK_SET) < 0)
12 14 return -1;
modified config.c
@@ -1271,6 +1271,15 @@ ssize_t git_config_ssize_t(const char *name, const char *value,
1271 1271 return ret;
1272 1272 }
1273 1273
1274 + size_t git_config_size_t(const char *name, const char *value,
1275 + const struct key_value_info *kvi)
1276 + {
1277 + size_t ret;
1278 + if (!git_parse_size_t(value, &ret))
1279 + die_bad_number(name, value, kvi);
1280 + return ret;
1281 + }
1282 +
1274 1283 double git_config_double(const char *name, const char *value,
1275 1284 const struct key_value_info *kvi)
1276 1285 {
modified config.h
@@ -282,6 +282,9 @@ unsigned long git_config_ulong(const char *, const char *,
282 282 ssize_t git_config_ssize_t(const char *, const char *,
283 283 const struct key_value_info *);
284 284
285 + size_t git_config_size_t(const char *, const char *,
286 + const struct key_value_info *);
287 +
285 288 /**
286 289 * Identically to `git_config_double`, but for double-precision floating point
287 290 * values.
modified delta.h
@@ -14,7 +14,7 @@ struct delta_index;
14 14 * using free_delta_index().
15 15 */
16 16 struct delta_index *
17 - create_delta_index(const void *buf, unsigned long bufsize);
17 + create_delta_index(const void *buf, size_t bufsize);
18 18
19 19 /*
20 20 * free_delta_index: free the index created by create_delta_index()
@@ -28,7 +28,7 @@ void free_delta_index(struct delta_index *index);
28 28 *
29 29 * Given pointer must be what create_delta_index() returned, or NULL.
30 30 */
31 - unsigned long sizeof_delta_index(struct delta_index *index);
31 + size_t sizeof_delta_index(struct delta_index *index);
32 32
33 33 /*
34 34 * create_delta: create a delta from given index for the given buffer
@@ -42,8 +42,8 @@ unsigned long sizeof_delta_index(struct delta_index *index);
42 42 */
43 43 void *
44 44 create_delta(const struct delta_index *index,
45 - const void *buf, unsigned long bufsize,
46 - unsigned long *delta_size, unsigned long max_delta_size);
45 + const void *buf, size_t bufsize,
46 + size_t *delta_size, size_t max_delta_size);
47 47
48 48 /*
49 49 * diff_delta: create a delta from source buffer to target buffer
@@ -54,9 +54,9 @@ create_delta(const struct delta_index *index,
54 54 * updated with its size. The returned buffer must be freed by the caller.
55 55 */
56 56 static inline void *
57 - diff_delta(const void *src_buf, unsigned long src_bufsize,
58 - const void *trg_buf, unsigned long trg_bufsize,
59 - unsigned long *delta_size, unsigned long max_delta_size)
57 + diff_delta(const void *src_buf, size_t src_bufsize,
58 + const void *trg_buf, size_t trg_bufsize,
59 + size_t *delta_size, size_t max_delta_size)
60 60 {
61 61 struct delta_index *index = create_delta_index(src_buf, src_bufsize);
62 62 if (index) {
modified diff-delta.c
@@ -125,14 +125,14 @@ struct unpacked_index_entry {
125 125 };
126 126
127 127 struct delta_index {
128 - unsigned long memsize;
128 + size_t memsize;
129 129 const void *src_buf;
130 - unsigned long src_size;
130 + size_t src_size;
131 131 unsigned int hash_mask;
132 132 struct index_entry *hash[FLEX_ARRAY];
133 133 };
134 134
135 - struct delta_index * create_delta_index(const void *buf, unsigned long bufsize)
135 + struct delta_index * create_delta_index(const void *buf, size_t bufsize)
136 136 {
137 137 unsigned int i, hsize, hmask, entries, prev_val, *hash_count;
138 138 const unsigned char *data, *buffer = buf;
@@ -140,7 +140,7 @@ struct delta_index * create_delta_index(const void *buf, unsigned long bufsize)
140 140 struct unpacked_index_entry *entry, **hash;
141 141 struct index_entry *packed_entry, **packed_hash;
142 142 void *mem;
143 - unsigned long memsize;
143 + size_t memsize;
144 144
145 145 if (!buf || !bufsize)
146 146 return NULL;
@@ -302,7 +302,7 @@ void free_delta_index(struct delta_index *index)
302 302 free(index);
303 303 }
304 304
305 - unsigned long sizeof_delta_index(struct delta_index *index)
305 + size_t sizeof_delta_index(struct delta_index *index)
306 306 {
307 307 if (index)
308 308 return index->memsize;
@@ -318,8 +318,8 @@ unsigned long sizeof_delta_index(struct delta_index *index)
318 318
319 319 void *
320 320 create_delta(const struct delta_index *index,
321 - const void *trg_buf, unsigned long trg_size,
322 - unsigned long *delta_size, unsigned long max_size)
321 + const void *trg_buf, size_t trg_size,
322 + size_t *delta_size, size_t max_size)
323 323 {
324 324 unsigned int i, val;
325 325 off_t outpos, moff;
modified diff.c
@@ -3586,7 +3586,7 @@ static unsigned char *deflate_it(char *data,
3586 3586 unsigned long size,
3587 3587 unsigned long *result_size)
3588 3588 {
3589 - int bound;
3589 + size_t bound;
3590 3590 unsigned char *deflated;
3591 3591 git_zstream stream;
3592 3592 struct repo_config_values *cfg = repo_config_values(the_repository);
@@ -3624,9 +3624,11 @@ static void emit_binary_diff_body(struct diff_options *o,
3624 3624 delta = NULL;
3625 3625 deflated = deflate_it(two->ptr, two->size, &deflate_size);
3626 3626 if (one->size && two->size) {
3627 + size_t delta_size_st = 0;
3627 3628 delta = diff_delta(one->ptr, one->size,
3628 3629 two->ptr, two->size,
3629 - &delta_size, deflate_size);
3630 + &delta_size_st, deflate_size);
3631 + delta_size = cast_size_t_to_ulong(delta_size_st);
3630 3632 if (delta) {
3631 3633 void *to_free = delta;
3632 3634 orig_size = delta_size;
@@ -6163,7 +6165,7 @@ struct option *add_diff_options(const struct option *opts,
6163 6165 N_("continue listing the history of a file beyond renames"),
6164 6166 PARSE_OPT_NOARG, diff_opt_follow),
6165 6167 OPT_INTEGER('l', NULL, &options->rename_limit,
6166 - N_("prevent rename/copy detection if the number of rename/copy targets exceeds given limit")),
6168 + N_("limit to cheap rename/copy detection if the number of rename/copy targets exceeds this value")),
6167 6169
6168 6170 OPT_GROUP(N_("Diff algorithm options")),
6169 6171 OPT_CALLBACK_F(0, "minimal", options, NULL,
modified git-zlib.c
@@ -33,7 +33,7 @@ static const char *zerr_to_string(int status)
33 33
34 34 /* uLong is 32-bit on Windows, even on 64-bit systems */
35 35 #define ULONG_MAX_VALUE maximum_unsigned_value_of_type(uLong)
36 - static inline uInt zlib_buf_cap(unsigned long len)
36 + static inline uInt zlib_buf_cap(size_t len)
37 37 {
38 38 return (ZLIB_BUF_MAX < len) ? ZLIB_BUF_MAX : len;
39 39 }
@@ -167,9 +167,21 @@ int git_inflate(git_zstream *strm, int flush)
167 167 return status;
168 168 }
169 169
170 - unsigned long git_deflate_bound(git_zstream *strm, unsigned long size)
170 + size_t git_deflate_bound(git_zstream *strm, size_t size)
171 171 {
172 - return deflateBound(&strm->z, size);
172 + #if SIZE_MAX > ULONG_MAX
173 + if (size > maximum_unsigned_value_of_type(uLong))
174 + /*
175 + * deflateBound() takes uLong, which is 32-bit on
176 + * Windows. For inputs above that range, return zlib's
177 + * stored-block formula (the conservative path it would
178 + * itself use for an unknown stream state) plus the
179 + * worst-case wrapper overhead.
180 + */
181 + return size + (size >> 5) + (size >> 7) + (size >> 11)
182 + + 7 + 18;
183 + #endif
184 + return deflateBound(&strm->z, (uLong)size);
173 185 }
174 186
175 187 void git_deflate_init(git_zstream *strm, int level)
modified git-zlib.h
@@ -5,8 +5,8 @@
5 5
6 6 typedef struct git_zstream {
7 7 struct z_stream_s z;
8 - unsigned long avail_in;
9 - unsigned long avail_out;
8 + size_t avail_in;
9 + size_t avail_out;
10 10 size_t total_in;
11 11 size_t total_out;
12 12 unsigned char *next_in;
@@ -25,6 +25,6 @@ void git_deflate_end(git_zstream *);
25 25 int git_deflate_abort(git_zstream *);
26 26 int git_deflate_end_gently(git_zstream *);
27 27 int git_deflate(git_zstream *, int flush);
28 - unsigned long git_deflate_bound(git_zstream *, unsigned long);
28 + size_t git_deflate_bound(git_zstream *, size_t);
29 29
30 30 #endif /* GIT_ZLIB_H */
modified git.c
@@ -304,11 +304,15 @@ static int handle_options(const char ***argv, int *argc, int *envchanged)
304 304 if (envchanged)
305 305 *envchanged = 1;
306 306 } else if (!strcmp(cmd, "--shallow-file")) {
307 - (*argv)++;
308 - (*argc)--;
309 - setenv(GIT_SHALLOW_FILE_ENVIRONMENT, (*argv)[0], 1);
307 + if (*argc < 2) {
308 + fprintf(stderr, _("no file given for '%s' option\n" ), "--shallow-file");
309 + usage(git_usage_string);
310 + }
311 + setenv(GIT_SHALLOW_FILE_ENVIRONMENT, (*argv)[1], 1);
310 312 if (envchanged)
311 313 *envchanged = 1;
314 + (*argv)++;
315 + (*argc)--;
312 316 } else if (!strcmp(cmd, "-C")) {
313 317 if (*argc < 2) {
314 318 fprintf(stderr, _("no directory given for '%s' option\n" ), "-C");
modified http-push.c
@@ -367,7 +367,7 @@ static void start_put(struct transfer_request *request)
367 367 void *unpacked;
368 368 size_t len;
369 369 int hdrlen;
370 - ssize_t size;
370 + size_t size;
371 371 git_zstream stream;
372 372 struct repo_config_values *cfg = repo_config_values(the_repository);
373 373
modified http.c
@@ -1608,6 +1608,8 @@ struct active_request_slot *get_active_slot(void)
1608 1608
1609 1609 if (!slot->curl) {
1610 1610 slot->curl = curl_easy_duphandle(curl_default);
1611 + if (!slot->curl)
1612 + die("curl_easy_duphandle failed");
1611 1613 curl_session_count++;
1612 1614 }
1613 1615
modified loose.c
@@ -115,7 +115,6 @@ int repo_read_loose_object_map(struct repository *repo)
115 115 {
116 116 struct odb_source *source;
117 117
118 - odb_prepare_alternates(repo->objects);
119 118 for (source = repo->objects->sources; source; source = source->next) {
120 119 struct odb_source_files *files = odb_source_files_downcast(source);
121 120 if (loose_object_map_load(files->loose) < 0)
modified object-name.c
@@ -280,7 +280,6 @@ static int init_object_disambiguation(struct repository *r,
280 280
281 281 ds->len = len;
282 282 ds->repo = r;
283 - odb_prepare_alternates(r->objects);
284 283 return 0;
285 284 }
286 285
modified odb.c
@@ -2,11 +2,10 @@
2 2 #include "abspath.h"
3 3 #include "commit-graph.h"
4 4 #include "config.h"
5 - #include "dir.h"
6 5 #include "environment.h"
7 6 #include "gettext.h"
7 + #include "hashmap.h"
8 8 #include "hex.h"
9 - #include "khash.h"
10 9 #include "lockfile.h"
11 10 #include "loose.h"
12 11 #include "midx.h"
@@ -29,8 +28,47 @@
29 28 #include "trace2.h"
30 29 #include "write-or-die.h"
31 30
32 - KHASH_INIT(odb_path_map, const char * /* key: odb_path */,
33 - struct odb_source *, 1, fspathhash, fspatheq)
31 + /*
32 + * NEEDSWORK: we're using "core.ignoreCase" to deduplicate alternates that
33 + * _may_ be the same. This requires quite a bit of boilerplate for dubious
34 + * benefit:
35 + *
36 + * - Duplicating alternates should really only lead to regressed performance.
37 + *
38 + * - We don't properly resolve symlinks or mointpoints, so we may still end
39 + * up duplicating alternates.
40 + *
41 + * - The value may be lying, in which case we might deduplicate alternates
42 + * that are in fact not mapping to the same directory.
43 + *
44 + * We should investigate whether we can remove this whole mechanism outright.
45 + */
46 + static int odb_source_paths_cmp(struct object_database *o,
47 + const char *a, const char *b)
48 + {
49 + if (o->source_paths_icase < 0) {
50 + int icase = 0;
51 + repo_config_get_bool(o->repo, "core.ignorecase", &icase);
52 + o->source_paths_icase = icase;
53 + }
54 +
55 + return o->source_paths_icase ? strcasecmp(a, b) : strcmp(a, b);
56 + }
57 +
58 + static int odb_source_by_path_cmp(const void *cb_data,
59 + const struct hashmap_entry *entry,
60 + const struct hashmap_entry *entry_or_key,
61 + const void *keydata)
62 + {
63 + struct object_database *o = (struct object_database *)cb_data;
64 + const struct odb_source *source = container_of(entry, const struct odb_source, by_path_entry);
65 + const char *path = keydata;
66 +
67 + if (!path)
68 + path = container_of(entry_or_key, const struct odb_source, by_path_entry)->path;
69 +
70 + return odb_source_paths_cmp(o, source->path, path);
71 + }
34 72
35 73 int odb_mkstemp(struct object_database *odb,
36 74 struct strbuf *temp_filename, const char *pattern)
@@ -58,8 +96,8 @@ int odb_mkstemp(struct object_database *odb,
58 96 */
59 97 static bool odb_is_source_usable(struct object_database *o, const char *path)
60 98 {
61 - int r;
62 99 struct strbuf normalized_objdir = STRBUF_INIT;
100 + struct hashmap_entry key;
63 101 bool usable = false;
64 102
65 103 strbuf_realpath(&normalized_objdir, o->sources->path, 1);
@@ -76,20 +114,18 @@ static bool odb_is_source_usable(struct object_database *o, const char *path)
76 114 * Prevent the common mistake of listing the same
77 115 * thing twice, or object directory itself.
78 116 */
79 - if (!o->source_by_path) {
80 - khiter_t p;
81 -
82 - o->source_by_path = kh_init_odb_path_map();
117 + if (!hashmap_get_size(&o->source_by_path)) {
83 118 assert(!o->sources->next);
84 - p = kh_put_odb_path_map(o->source_by_path, o->sources->path, &r);
85 - assert(r == 1); /* never used */
86 - kh_value(o->source_by_path, p) = o->sources;
119 + hashmap_entry_init(&o->sources->by_path_entry,
120 + strihash(o->sources->path));
121 + hashmap_add(&o->source_by_path, &o->sources->by_path_entry);
87 122 }
88 123
89 - if (fspatheq(path, normalized_objdir.buf))
124 + if (!odb_source_paths_cmp(o, path, normalized_objdir.buf))
90 125 goto out;
91 126
92 - if (kh_get_odb_path_map(o->source_by_path, path) < kh_end(o->source_by_path))
127 + hashmap_entry_init(&key, strihash(path));
128 + if (hashmap_get(&o->source_by_path, &key, path))
93 129 goto out;
94 130
95 131 usable = true;
@@ -172,8 +208,6 @@ static struct odb_source *odb_add_alternate_recursively(struct object_database *
172 208 {
173 209 struct odb_source *alternate = NULL;
174 210 struct strvec sources = STRVEC_INIT;
175 - khiter_t pos;
176 - int ret;
177 211
178 212 if (!odb_is_source_usable(odb, source))
179 213 goto error;
@@ -184,10 +218,11 @@ static struct odb_source *odb_add_alternate_recursively(struct object_database *
184 218 *odb->sources_tail = alternate;
185 219 odb->sources_tail = &(alternate->next);
186 220
187 - pos = kh_put_odb_path_map(odb->source_by_path, alternate->path, &ret);
188 - if (!ret)
221 + hashmap_entry_init(&alternate->by_path_entry, strihash(alternate->path));
222 + if (hashmap_get(&odb->source_by_path, &alternate->by_path_entry,
223 + alternate->path))
189 224 BUG("source must not yet exist");
190 - kh_value(odb->source_by_path, pos) = alternate;
225 + hashmap_add(&odb->source_by_path, &alternate->by_path_entry);
191 226
192 227 /* recursively add alternates */
193 228 odb_source_read_alternates(alternate, &sources);
@@ -210,18 +245,12 @@ void odb_add_to_alternates_file(struct object_database *odb,
210 245 int ret = odb_source_write_alternate(odb->sources, dir);
211 246 if (ret < 0)
212 247 die(NULL);
213 - if (odb->loaded_alternates)
214 - odb_add_alternate_recursively(odb, dir, 0);
248 + odb_add_alternate_recursively(odb, dir, 0);
215 249 }
216 250
217 251 struct odb_source *odb_add_to_alternates_memory(struct object_database *odb,
218 252 const char *dir)
219 253 {
220 - /*
221 - * Make sure alternates are initialized, or else our entry may be
222 - * overwritten when they are.
223 - */
224 - odb_prepare_alternates(odb);
225 254 return odb_add_alternate_recursively(odb, dir, 0);
226 255 }
227 256
@@ -230,12 +259,6 @@ struct odb_source *odb_set_temporary_primary_source(struct object_database *odb,
230 259 {
231 260 struct odb_source *source;
232 261
233 - /*
234 - * Make sure alternates are initialized, or else our entry may be
235 - * overwritten when they are.
236 - */
237 - odb_prepare_alternates(odb);
238 -
239 262 /*
240 263 * Make a new primary odb and link the old primary ODB in as an
241 264 * alternate
@@ -341,7 +364,6 @@ struct odb_source *odb_find_source(struct object_database *odb, const char *obj_
341 364 char *obj_dir_real = real_pathdup(obj_dir, 1);
342 365 struct strbuf odb_path_real = STRBUF_INIT;
343 366
344 - odb_prepare_alternates(odb);
345 367 for (source = odb->sources; source; source = source->next) {
346 368 strbuf_realpath(&odb_path_real, source->path, 1);
347 369 if (!strcmp(obj_dir_real, odb_path_real.buf))
@@ -475,7 +497,6 @@ int odb_for_each_alternate(struct object_database *odb,
475 497 struct odb_source *alternate;
476 498 int r = 0;
477 499
478 - odb_prepare_alternates(odb);
479 500 for (alternate = odb->sources->next; alternate; alternate = alternate->next) {
480 501 r = cb(alternate, payload);
481 502 if (r)
@@ -484,26 +505,22 @@ int odb_for_each_alternate(struct object_database *odb,
484 505 return r;
485 506 }
486 507
487 - void odb_prepare_alternates(struct object_database *odb)
508 + static void odb_prepare_alternates(struct object_database *odb,
509 + const char *alternate_db)
488 510 {
489 511 struct strvec sources = STRVEC_INIT;
490 512
491 - if (odb->loaded_alternates)
492 - return;
493 -
494 - parse_alternates(odb->alternate_db, PATH_SEP, NULL, &sources);
513 + parse_alternates(alternate_db, PATH_SEP, NULL, &sources);
495 514 odb_source_read_alternates(odb->sources, &sources);
515 +
496 516 for (size_t i = 0; i < sources.nr; i++)
497 517 odb_add_alternate_recursively(odb, sources.v[i], 0);
498 518
499 - odb->loaded_alternates = 1;
500 -
501 519 strvec_clear(&sources);
502 520 }
503 521
504 522 int odb_has_alternates(struct object_database *odb)
505 523 {
506 - odb_prepare_alternates(odb);
507 524 return !!odb->sources->next;
508 525 }
509 526
@@ -563,8 +580,6 @@ static int do_oid_object_info_extended(struct object_database *odb,
563 580 if (!odb_source_read_object_info(odb->inmemory_objects, oid, oi, flags))
564 581 return 0;
565 582
566 - odb_prepare_alternates(odb);
567 -
568 583 while (1) {
569 584 struct odb_source *source;
570 585
@@ -827,7 +842,6 @@ int odb_freshen_object(struct object_database *odb,
827 842 const struct object_id *oid)
828 843 {
829 844 struct odb_source *source;
830 - odb_prepare_alternates(odb);
831 845 for (source = odb->sources; source; source = source->next)
832 846 if (odb_source_freshen_object(source, oid, NULL))
833 847 return 1;
@@ -842,7 +856,6 @@ int odb_for_each_object_ext(struct object_database *odb,
842 856 {
843 857 int ret;
844 858
845 - odb_prepare_alternates(odb);
846 859 for (struct odb_source *source = odb->sources; source; source = source->next) {
847 860 if (opts->flags & ODB_FOR_EACH_OBJECT_LOCAL_ONLY && !source->local)
848 861 continue;
@@ -880,7 +893,6 @@ int odb_count_objects(struct object_database *odb,
880 893 return 0;
881 894 }
882 895
883 - odb_prepare_alternates(odb);
884 896 for (source = odb->sources; source; source = source->next) {
885 897 unsigned long c;
886 898
@@ -960,7 +972,6 @@ int odb_find_abbrev_len(struct object_database *odb,
960 972 goto out;
961 973 }
962 974
963 - odb_prepare_alternates(odb);
964 975 for (struct odb_source *source = odb->sources; source; source = source->next) {
965 976 ret = odb_source_find_abbrev_len(source, oid, len, &len);
966 977 if (ret)
@@ -1046,6 +1057,27 @@ bool odb_optimize_required(struct object_database *odb,
1046 1057 return odb_source_optimize_required(odb->sources, opts);
1047 1058 }
1048 1059
1060 + void odb_generate_pack_options_release(struct odb_generate_pack_options *opts)
1061 + {
1062 + oid_array_clear(&opts->wants);
1063 + oid_array_clear(&opts->haves);
1064 + oid_array_clear(&opts->shallows);
1065 + }
1066 +
1067 + int odb_generate_pack(struct object_database *odb,
1068 + struct odb_pack_generator **out,
1069 + const struct odb_generate_pack_options *opts)
1070 + {
1071 + if (!odb->sources->generate_pack)
1072 + return error(_("primary object source does not support generating packfiles"));
1073 + return odb_source_generate_pack(odb->sources, out, opts);
1074 + }
1075 +
1076 + int odb_pack_generator_finish(struct odb_pack_generator *generator)
1077 + {
1078 + return generator->finish(generator);
1079 + }
1080 +
1049 1081 struct object_database *odb_new(struct repository *repo,
1050 1082 enum odb_new_flags flags)
1051 1083 {
@@ -1056,6 +1088,8 @@ struct object_database *odb_new(struct repository *repo,
1056 1088 o->repo = repo;
1057 1089 pthread_mutex_init(&o->replace_mutex, NULL);
1058 1090 string_list_init_dup(&o->submodule_source_paths);
1091 + hashmap_init(&o->source_by_path, odb_source_by_path_cmp, o, 0);
1092 + o->source_paths_icase = -1;
1059 1093
1060 1094 if (flags & ODB_NEW_HONOR_ENV) {
1061 1095 primary_source = xstrdup_or_null(getenv(DB_ENVIRONMENT));
@@ -1066,9 +1100,11 @@ struct object_database *odb_new(struct repository *repo,
1066 1100
1067 1101 o->sources = odb_source_new(o, primary_source, true);
1068 1102 o->sources_tail = &o->sources->next;
1069 - o->alternate_db = secondary_sources;
1070 1103 o->inmemory_objects = &odb_source_inmemory_new(o)->base;
1071 1104
1105 + odb_prepare_alternates(o, secondary_sources);
1106 +
1107 + free(secondary_sources);
1072 1108 free(primary_source);
1073 1109 return o;
1074 1110 }
@@ -1094,8 +1130,7 @@ static void odb_free_sources(struct object_database *o)
1094 1130 odb_source_free(o->inmemory_objects);
1095 1131 o->inmemory_objects = NULL;
1096 1132
1097 - kh_destroy_odb_path_map(o->source_by_path);
1098 - o->source_by_path = NULL;
1133 + hashmap_clear(&o->source_by_path);
1099 1134 }
1100 1135
1101 1136 void odb_free(struct object_database *o)
@@ -1103,8 +1138,6 @@ void odb_free(struct object_database *o)
1103 1138 if (!o)
1104 1139 return;
1105 1140
1106 - free(o->alternate_db);
1107 -
1108 1141 oidmap_clear(&o->replace_map, 1);
1109 1142 pthread_mutex_destroy(&o->replace_mutex);
1110 1143
@@ -1126,14 +1159,14 @@ void odb_prepare(struct object_database *o, enum odb_prepare_flags flags)
1126 1159 * Reprepare alt odbs, in case the alternates file was modified
1127 1160 * during the course of this process. This only _adds_ odbs to
1128 1161 * the linked list, so existing odbs will continue to exist for
1129 - * the lifetime of the process.
1162 + * the lifetime of the process. Consequently, we don't have to
1163 + * reprocess GIT_ALTERNATE_OBJECT_DIRECTORIES here.
1130 1164 */
1131 1165 if (flags & ODB_PREPARE_FLUSH_CACHES) {
1132 - o->loaded_alternates = 0;
1166 + odb_prepare_alternates(o, NULL);
1133 1167 o->object_count_valid = 0;
1134 1168 }
1135 1169
1136 - odb_prepare_alternates(o);
1137 1170 for (source = o->sources; source; source = source->next)
1138 1171 odb_source_prepare(source, flags);
1139 1172
@@ -1144,3 +1177,11 @@ void odb_reprepare(struct object_database *o)
1144 1177 {
1145 1178 odb_prepare(o, ODB_PREPARE_FLUSH_CACHES);
1146 1179 }
1180 +
1181 + int odb_fsck(struct object_database *odb, struct odb_fsck_options *options)
1182 + {
1183 + int ret = 0;
1184 + for (struct odb_source *source = odb->sources; source; source = source->next)
1185 + ret |= odb_source_fsck(source, options);
1186 + return ret;
1187 + }
modified odb.h
@@ -1,7 +1,9 @@
1 1 #ifndef ODB_H
2 2 #define ODB_H
3 3
4 + #include "hashmap.h"
4 5 #include "object.h"
6 + #include "oid-array.h"
5 7 #include "oidset.h"
6 8 #include "oidmap.h"
7 9 #include "string-list.h"
@@ -54,16 +56,19 @@ struct object_database {
54 56 */
55 57 struct odb_source *sources;
56 58 struct odb_source **sources_tail;
57 - struct kh_odb_path_map *source_by_path;
58 59
59 - int loaded_alternates;
60 + /*
61 + * Map of object database sources, keyed by their respective paths.
62 + * This map is used to detect the case where the same source is
63 + * registered multiple times.
64 + */
65 + struct hashmap source_by_path;
60 66
61 67 /*
62 - * A list of alternate object directories loaded from the environment;
63 - * this should not generally need to be accessed directly, but will
64 - * populate the "sources" list when odb_prepare_alternates() is run.
68 + * Whether source paths shall be compared case-insensitively, as
69 + * determined by "core.ignoreCase".
65 70 */
66 - char *alternate_db;
71 + int source_paths_icase;
67 72
68 73 /*
69 74 * Objects that should be substituted by other objects
@@ -201,6 +206,36 @@ void odb_prepare(struct object_database *o, enum odb_prepare_flags flags);
201 206 /* Equivalent to `odb_prepare(o, ODB_PREPARE_FLUSH_CACHES)`. */
202 207 void odb_reprepare(struct object_database *o);
203 208
209 + enum odb_fsck_flags {
210 + /*
211 + * If set, perform a full consistency check for the full object
212 + * database, including all of its sources and the contents of their
213 + * optimized formats. Otherwise, only check the local source, and
214 + * restrict checks of its optimized formats to cheap structural
215 + * verification of their metadata.
216 + */
217 + ODB_FSCK_FULL = (1 << 0),
218 +
219 + /* Display a progress meter, if sensible. */
220 + ODB_FSCK_PROGRESS = (1 << 1),
221 + };
222 +
223 + /* Options that shall be passed to `odb_fsck()`. */
224 + struct odb_fsck_options {
225 + enum odb_fsck_flags flags;
226 +
227 + int (*object_cb)(const struct object_id *oid, enum object_type type,
228 + unsigned long size, void *buffer, int *eaten, void *cb_data);
229 + void *object_payload;
230 + };
231 +
232 + /*
233 + * Run backend-specific integrity checks on all object sources. Each source
234 + * performs the checks appropriate to its type. Returns 0 on success, a
235 + * negative error code otherwise.
236 + */
237 + int odb_fsck(struct object_database *odb, struct odb_fsck_options *opts);
238 +
204 239 /*
205 240 * Find source by its object directory path. Returns a `NULL` pointer in case
206 241 * the source could not be found.
@@ -260,12 +295,6 @@ void odb_for_each_alternate_ref(struct object_database *odb,
260 295 int odb_mkstemp(struct object_database *odb,
261 296 struct strbuf *temp_filename, const char *pattern);
262 297
263 - /*
264 - * Prepare alternate object sources for the given database by reading
265 - * "objects/info/alternates" and opening the respective sources.
266 - */
267 - void odb_prepare_alternates(struct object_database *odb);
268 -
269 298 /*
270 299 * Check whether the object database has any alternates. The primary object
271 300 * source does not count as alternate.
@@ -690,6 +719,157 @@ int odb_write_object_stream(struct object_database *odb,
690 719 struct odb_stream *stream,
691 720 struct object_id *oid);
692 721
722 + /*
723 + * Options for generating a packfile via `odb_generate_pack()`.
724 + */
725 + struct odb_generate_pack_options {
726 + /* Tips of the object graph that shall be packed. */
727 + struct oid_array wants;
728 +
729 + /*
730 + * Boundary of the object graph. Objects reachable from any of these
731 + * tips are expected to already be available to whoever consumes the
732 + * pack and shall thus not be packed.
733 + */
734 + struct oid_array haves;
735 +
736 + /*
737 + * The shallow boundary that shall be used when computing object
738 + * reachability. When set, any shallow information of the repository
739 + * itself shall be ignored in favor of these objects.
740 + */
741 + struct oid_array shallows;
742 +
743 + /*
744 + * Pre-expanded object filter specification that limits the set of
745 + * objects that shall be packed. May be `NULL` in case no filter shall
746 + * be applied.
747 + */
748 + const char *filter_spec;
749 +
750 + /*
751 + * Protocols that may be used to offload objects via packfile URIs.
752 + * May be `NULL` in case packfile URIs shall not be used.
753 + */
754 + const struct string_list *uri_protocols;
755 +
756 + /*
757 + * Hook command that shall be executed instead of the internal
758 + * machinery to generate the pack. It is up to the specific backend
759 + * whether or not this hook is supported. May be `NULL` in case no
760 + * hook shall be executed.
761 + */
762 + const char *pack_objects_hook;
763 +
764 + /*
765 + * File descriptor that the generated pack shall be written to. If set
766 + * to `-1`, a pipe will be created and exposed via the pack generator's
767 + * `out` field. If set to `0`, the pack will be written to the standard
768 + * output stream. Otherwise, the provided descriptor will be written to
769 + * and is consumed by the generator.
770 + */
771 + int pack_fd;
772 +
773 + /*
774 + * File descriptor that progress output shall be written to. The same
775 + * semantics as for `pack_fd` apply, except that `0` will cause the
776 + * generator to write to stderr instead of stdout.
777 + */
778 + int progress_fd;
779 +
780 + /* Whether to print progress or not. */
781 + enum {
782 + /* Don't print progress output. */
783 + ODB_GENERATE_PACK_PROGRESS_NONE,
784 +
785 + /*
786 + * Print progress while computing the packfile, but stop
787 + * printing progress once starting to write it.
788 + */
789 + ODB_GENERATE_PACK_PROGRESS_STANDARD,
790 +
791 + /*
792 + * Similar to STANDARD, but also print progress when writing
793 + * the packfile.
794 + */
795 + ODB_GENERATE_PACK_PROGRESS_VERBOSE,
796 + } progress;
797 +
798 + /* Allow the pack to contain deltas against unpacked objects. */
799 + unsigned thin:1;
800 +
801 + /* Use offset deltas instead of reference deltas. */
802 + unsigned ofs_delta:1;
803 +
804 + /* Include unasked-for annotated tags of packed objects. */
805 + unsigned include_tag:1;
806 +
807 + /* The generated pack is destined for a shallow consumer. */
808 + unsigned shallow:1;
809 +
810 + /* Allow objects that may be missing due to a promisor remote. */
811 + unsigned missing_allow_promisor:1;
812 +
813 + /* Do not use bitmap indices when computing reachability. */
814 + unsigned disable_bitmaps:1;
815 + };
816 +
817 + #define ODB_GENERATE_PACK_OPTIONS_INIT { \
818 + .wants = OID_ARRAY_INIT, \
819 + .haves = OID_ARRAY_INIT, \
820 + .shallows = OID_ARRAY_INIT, \
821 + .pack_fd = -1, \
822 + }
823 +
824 + /* Release resources associated with the options. */
825 + void odb_generate_pack_options_release(struct odb_generate_pack_options *opts);
826 +
827 + /*
828 + * A handle for an ongoing packfile generation as started via
829 + * `odb_generate_pack()`.
830 + */
831 + struct odb_pack_generator {
832 + /*
833 + * File descriptor from which the generated pack can be read. Only set
834 + * when the pack generation was started with `pack_fd == -1`. The
835 + * caller is responsible for closing the descriptor.
836 + */
837 + int out;
838 +
839 + /*
840 + * File descriptor from which progress output can be read. Only set
841 + * when the pack generation was started with `progress_fd == -1`. The
842 + * caller is responsible for closing the descriptor.
843 + */
844 + int err;
845 +
846 + /*
847 + * Callback function to finish this generator. This callback is
848 + * expected to wait for the packfile generation to complete and to then
849 + * free the generator itself.
850 + */
851 + int (*finish)(struct odb_pack_generator *);
852 + };
853 +
854 + /*
855 + * Start generating a packfile from the object database with the given
856 + * options. The pack is generated asynchronously; the caller is expected to
857 + * consume the file descriptors exposed via the pack generator and to then
858 + * wait for completion via `odb_pack_generator_finish()`.
859 + *
860 + * Returns 0 on success and populates the `out` pointer with the pack
861 + * generator. Returns a negative error code otherwise.
862 + */
863 + int odb_generate_pack(struct object_database *odb,
864 + struct odb_pack_generator **out,
865 + const struct odb_generate_pack_options *opts);
866 +
867 + /*
868 + * Wait for the packfile generation to complete and free the pack generator.
869 + * Returns 0 on success, a negative error code otherwise.
870 + */
871 + int odb_pack_generator_finish(struct odb_pack_generator *generator);
872 +
693 873 void parse_alternates(const char *string,
694 874 int sep,
695 875 const char *relative_base,
modified odb/source-files.c
@@ -4,6 +4,7 @@
4 4 #include "chdir-notify.h"
5 5 #include "config.h"
6 6 #include "gettext.h"
7 + #include "hex.h"
7 8 #include "lockfile.h"
8 9 #include "object-file.h"
9 10 #include "odb.h"
@@ -745,6 +746,168 @@ out:
745 746 return ret;
746 747 }
747 748
749 + struct odb_pack_generator_files {
750 + struct odb_pack_generator base;
751 + struct child_process cp;
752 + };
753 +
754 + static int odb_pack_generator_files_finish(struct odb_pack_generator *_generator)
755 + {
756 + struct odb_pack_generator_files *generator =
757 + (struct odb_pack_generator_files *)_generator;
758 + int ret;
759 +
760 + ret = finish_command(&generator->cp);
761 + free(generator);
762 +
763 + if (ret) {
764 + /*
765 + * On failure, pack-objects is expected to have written a
766 + * useful error message to its standard error stream already.
767 + * Death by signal is worth mentioning, though, with the
768 + * exception of SIGPIPE: that is a normal occurrence when the
769 + * consumer of the pack hangs up.
770 + */
771 + if (ret > 128 && ret - 128 == SIGPIPE)
772 + return -1;
773 + if (ret > 128)
774 + error(_("pack-objects died of signal %d"), ret - 128);
775 + return -1;
776 + }
777 +
778 + return 0;
779 + }
780 +
781 + static int odb_source_files_generate_pack(struct odb_source *source UNUSED,
782 + struct odb_pack_generator **out,
783 + const struct odb_generate_pack_options *opts)
784 + {
785 + struct odb_pack_generator_files *generator;
786 + struct child_process *cp;
787 + FILE *in;
788 +
789 + CALLOC_ARRAY(generator, 1);
790 + child_process_init(&generator->cp);
791 + cp = &generator->cp;
792 +
793 + /*
794 + * The hook is expected to spawn "$hook git pack-objects <args...>"
795 + * and to behave like git-pack-objects(1) would have. This can for
796 + * example be used to serve precomputed packfiles.
797 + */
798 + if (opts->pack_objects_hook) {
799 + strvec_push(&cp->args, opts->pack_objects_hook);
800 + strvec_push(&cp->args, "git");
801 + cp->use_shell = 1;
802 + } else {
803 + cp->git_cmd = 1;
804 + }
805 +
806 + /*
807 + * The caller-provided shallow boundary overrides any shallow state
808 + * that the repository itself may have, so the shallow file needs to
809 + * be neutralized.
810 + */
811 + if (opts->shallows.nr) {
812 + strvec_push(&cp->args, "--shallow-file");
813 + strvec_push(&cp->args, "");
814 + }
815 + strvec_push(&cp->args, "pack-objects");
816 + strvec_push(&cp->args, "--revs");
817 + strvec_push(&cp->args, "--stdout");
818 + if (opts->thin)
819 + strvec_push(&cp->args, "--thin");
820 + if (opts->shallow)
821 + strvec_push(&cp->args, "--shallow");
822 + if (opts->ofs_delta)
823 + strvec_push(&cp->args, "--delta-base-offset");
824 + if (opts->include_tag)
825 + strvec_push(&cp->args, "--include-tag");
826 + if (opts->missing_allow_promisor)
827 + strvec_push(&cp->args, "--missing=allow-promisor");
828 + if (opts->disable_bitmaps)
829 + strvec_push(&cp->args, "--no-use-bitmap-index");
830 + switch (opts->progress) {
831 + case ODB_GENERATE_PACK_PROGRESS_NONE:
832 + strvec_push(&cp->args, "--quiet");
833 + break;
834 + case ODB_GENERATE_PACK_PROGRESS_STANDARD:
835 + strvec_push(&cp->args, "--progress");
836 + break;
837 + case ODB_GENERATE_PACK_PROGRESS_VERBOSE:
838 + strvec_push(&cp->args, "--all-progress");
839 + break;
840 + default:
841 + BUG("unknown progress option %d", opts->progress);
842 + }
843 + if (opts->filter_spec)
844 + strvec_pushf(&cp->args, "--filter=%s", opts->filter_spec);
845 + if (opts->uri_protocols)
846 + for (size_t i = 0; i < opts->uri_protocols->nr; i++)
847 + strvec_pushf(&cp->args, "--uri-protocol=%s",
848 + opts->uri_protocols->items[i].string);
849 +
850 + cp->in = -1;
851 + cp->out = opts->pack_fd;
852 + cp->err = opts->progress_fd;
853 + cp->clean_on_exit = 1;
854 +
855 + if (start_command(cp)) {
856 + free(generator);
857 + return error(_("could not spawn pack-objects"));
858 + }
859 +
860 + /*
861 + * Feed the objects to pack-objects. This is safe to do synchronously
862 + * because pack-objects consumes all of its standard input before it
863 + * starts to generate the pack.
864 + */
865 + in = xfdopen(cp->in, "w");
866 + for (size_t i = 0; i < opts->shallows.nr; i++)
867 + fprintf(in, "--shallow %s\n", oid_to_hex(&opts->shallows.oid[i]));
868 + for (size_t i = 0; i < opts->wants.nr; i++)
869 + fprintf(in, "%s\n", oid_to_hex(&opts->wants.oid[i]));
870 + fprintf(in, "--not\n");
871 + for (size_t i = 0; i < opts->haves.nr; i++)
872 + fprintf(in, "%s\n", oid_to_hex(&opts->haves.oid[i]));
873 + fprintf(in, "\n");
874 + fflush(in);
875 + if (ferror(in)) {
876 + error(_("error writing to pack-objects"));
877 + fclose(in);
878 + if (opts->pack_fd < 0)
879 + close(cp->out);
880 + if (opts->progress_fd < 0)
881 + close(cp->err);
882 + finish_command(cp);
883 + free(generator);
884 + return -1;
885 + }
886 + fclose(in);
887 +
888 + generator->base.out = opts->pack_fd < 0 ? cp->out : -1;
889 + generator->base.err = opts->progress_fd < 0 ? cp->err : -1;
890 + generator->base.finish = odb_pack_generator_files_finish;
891 +
892 + *out = &generator->base;
893 + return 0;
894 + }
895 +
896 + static int odb_source_files_fsck(struct odb_source *source,
897 + struct odb_fsck_options *opts)
898 + {
899 + struct odb_source_files *files = odb_source_files_downcast(source);
900 + int ret = 0;
901 +
902 + if (!(opts->flags & ODB_FSCK_FULL) && !source->local)
903 + return 0;
904 +
905 + ret |= odb_source_fsck(&files->loose->base, opts);
906 + ret |= odb_source_fsck(&files->packed->base, opts);
907 +
908 + return ret;
909 + }
910 +
748 911 struct odb_source_files *odb_source_files_new(struct object_database *odb,
749 912 const char *path,
750 913 bool local)
@@ -760,6 +923,7 @@ struct odb_source_files *odb_source_files_new(struct object_database *odb,
760 923 files->base.close = odb_source_files_close;
761 924 files->base.create_on_disk = odb_source_files_create_on_disk;
762 925 files->base.prepare = odb_source_files_prepare;
926 + files->base.fsck = odb_source_files_fsck;
763 927 files->base.read_object_info = odb_source_files_read_object_info;
764 928 files->base.read_object_stream = odb_source_files_read_object_stream;
765 929 files->base.for_each_object = odb_source_files_for_each_object;
@@ -773,6 +937,7 @@ struct odb_source_files *odb_source_files_new(struct object_database *odb,
773 937 files->base.write_alternate = odb_source_files_write_alternate;
774 938 files->base.optimize = odb_source_files_optimize;
775 939 files->base.optimize_required = odb_source_files_optimize_required;
940 + files->base.generate_pack = odb_source_files_generate_pack;
776 941
777 942 /*
778 943 * Ideally, we would only ever store absolute paths in the source. This
modified odb/source-inmemory.c
@@ -1,6 +1,7 @@
1 1 #include "git-compat-util.h"
2 2 #include "object-file.h"
3 3 #include "odb.h"
4 + #include "fsck.h"
4 5 #include "odb/source-inmemory.h"
5 6 #include "odb/streaming.h"
6 7 #include "oidtree.h"
@@ -368,6 +369,12 @@ static void odb_source_inmemory_free(struct odb_source *source)
368 369 free(inmemory);
369 370 }
370 371
372 + static int odb_source_inmemory_fsck(struct odb_source *source UNUSED,
373 + struct odb_fsck_options *opts UNUSED)
374 + {
375 + return 0;
376 + }
377 +
371 378 struct odb_source_inmemory *odb_source_inmemory_new(struct object_database *odb)
372 379 {
373 380 struct odb_source_inmemory *source;
@@ -378,6 +385,7 @@ struct odb_source_inmemory *odb_source_inmemory_new(struct object_database *odb)
378 385 source->base.free = odb_source_inmemory_free;
379 386 source->base.close = odb_source_inmemory_close;
380 387 source->base.prepare = odb_source_inmemory_prepare;
388 + source->base.fsck = odb_source_inmemory_fsck;
381 389 source->base.read_object_info = odb_source_inmemory_read_object_info;
382 390 source->base.read_object_stream = odb_source_inmemory_read_object_stream;
383 391 source->base.for_each_object = odb_source_inmemory_for_each_object;
modified odb/source-loose.c
@@ -1031,6 +1031,12 @@ static void odb_source_loose_free(struct odb_source *source)
1031 1031 free(loose);
1032 1032 }
1033 1033
1034 + static int odb_source_loose_fsck(struct odb_source *source UNUSED,
1035 + struct odb_fsck_options *opts UNUSED)
1036 + {
1037 + return 0;
1038 + }
1039 +
1034 1040 struct odb_source_loose *odb_source_loose_new(struct object_database *odb,
1035 1041 const char *path,
1036 1042 bool local)
@@ -1043,6 +1049,7 @@ struct odb_source_loose *odb_source_loose_new(struct object_database *odb,
1043 1049 loose->base.free = odb_source_loose_free;
1044 1050 loose->base.close = odb_source_loose_close;
1045 1051 loose->base.prepare = odb_source_loose_prepare;
1052 + loose->base.fsck = odb_source_loose_fsck;
1046 1053 loose->base.read_object_info = odb_source_loose_read_object_info;
1047 1054 loose->base.read_object_stream = odb_source_loose_read_object_stream;
1048 1055 loose->base.for_each_object = odb_source_loose_for_each_object;
modified odb/source-packed.c
@@ -2,14 +2,18 @@
2 2 #include "abspath.h"
3 3 #include "chdir-notify.h"
4 4 #include "dir.h"
5 + #include "fsck.h"
5 6 #include "git-zlib.h"
6 7 #include "list-objects-filter-options.h"
7 8 #include "mergesort.h"
8 9 #include "midx.h"
9 10 #include "odb/source-packed.h"
10 11 #include "odb/streaming.h"
12 + #include "pack.h"
13 + #include "pack-revindex.h"
11 14 #include "packfile.h"
12 15 #include "pack-bitmap.h"
16 + #include "progress.h"
13 17
14 18 static int find_pack_entry(struct odb_source_packed *store,
15 19 const struct object_id *oid,
@@ -826,6 +830,88 @@ static void odb_source_packed_free(struct odb_source *source)
826 830 free(packed);
827 831 }
828 832
833 + static int verify_packs(struct odb_source_packed *source,
834 + struct odb_fsck_options *opts)
835 + {
836 + struct progress *progress = NULL;
837 + struct packfile_list_entry *e;
838 + uint32_t total = 0, count = 0;
839 + int ret = 0;
840 +
841 + if (opts->flags & ODB_FSCK_PROGRESS) {
842 + for (e = packfile_store_get_packs(source); e; e = e->next) {
843 + if (open_pack_index(e->pack))
844 + continue;
845 + total += e->pack->num_objects;
846 + }
847 +
848 + progress = start_progress(source->base.odb->repo,
849 + _("Checking objects"), total);
850 + }
851 +
852 + for (e = packfile_store_get_packs(source); e; e = e->next) {
853 + /* verify gives error messages itself */
854 + if (verify_pack(source->base.odb->repo, e->pack,
855 + opts->object_cb, opts->object_payload,
856 + progress, count))
857 + ret = -1;
858 + count += e->pack->num_objects;
859 + }
860 + stop_progress(&progress);
861 +
862 + return ret;
863 + }
864 +
865 + static int verify_reverse_indices(struct odb_source_packed *source,
866 + struct odb_fsck_options *opts)
867 + {
868 + struct progress *progress = NULL;
869 + struct packfile_list_entry *e;
870 + uint32_t pack_count = 0;
871 + int res = 0;
872 +
873 + if (opts->flags & ODB_FSCK_PROGRESS) {
874 + for (e = packfile_store_get_packs(source); e; e = e->next)
875 + pack_count++;
876 + progress = start_delayed_progress(source->base.odb->repo,
877 + "Verifying reverse pack-indexes", pack_count);
878 + pack_count = 0;
879 + }
880 +
881 + for (e = packfile_store_get_packs(source); e; e = e->next) {
882 + int load_error = load_pack_revindex_from_disk(e->pack);
883 +
884 + if (load_error < 0) {
885 + error(_("unable to load rev-index for pack '%s'"), e->pack->pack_name);
886 + res = -1;
887 + } else if (!load_error &&
888 + !load_pack_revindex(source->base.odb->repo, e->pack) &&
889 + verify_pack_revindex(e->pack)) {
890 + error(_("invalid rev-index for pack '%s'"), e->pack->pack_name);
891 + res = -1;
892 + }
893 + display_progress(progress, ++pack_count);
894 + }
895 + stop_progress(&progress);
896 +
897 + return res;
898 + }
899 +
900 + static int odb_source_packed_fsck(struct odb_source *source,
901 + struct odb_fsck_options *opts)
902 + {
903 + struct odb_source_packed *packed = odb_source_packed_downcast(source);
904 + int ret = 0;
905 +
906 + if ((opts->flags & ODB_FSCK_FULL) && verify_packs(packed, opts) < 0)
907 + ret = -1;
908 +
909 + if (verify_reverse_indices(packed, opts) < 0)
910 + ret = -1;
911 +
912 + return ret;
913 + }
914 +
829 915 struct odb_source_packed *odb_source_packed_new(struct object_database *odb,
830 916 const char *path,
831 917 bool local)
@@ -839,6 +925,7 @@ struct odb_source_packed *odb_source_packed_new(struct object_database *odb,
839 925 packed->base.free = odb_source_packed_free;
840 926 packed->base.close = odb_source_packed_close;
841 927 packed->base.prepare = odb_source_packed_prepare;
928 + packed->base.fsck = odb_source_packed_fsck;
842 929 packed->base.read_object_info = odb_source_packed_read_object_info;
843 930 packed->base.read_object_stream = odb_source_packed_read_object_stream;
844 931 packed->base.for_each_object = odb_source_packed_for_each_object;
modified odb/source.h
@@ -1,6 +1,7 @@
1 1 #ifndef ODB_SOURCE_H
2 2 #define ODB_SOURCE_H
3 3
4 + #include "hashmap.h"
4 5 #include "object.h"
5 6 #include "odb.h"
6 7 #include "odb/transaction.h"
@@ -50,6 +51,12 @@ struct strvec;
50 51 struct odb_source {
51 52 struct odb_source *next;
52 53
54 + /*
55 + * Entry in the object database's map of sources, keyed by this
56 + * source's path.
57 + */
58 + struct hashmap_entry by_path_entry;
59 +
53 60 /* Object database that owns this object source. */
54 61 struct object_database *odb;
55 62
@@ -296,6 +303,34 @@ struct odb_source {
296 303 */
297 304 bool (*optimize_required)(struct odb_source *source,
298 305 const struct odb_optimize_options *opts);
306 +
307 + /*
308 + * This callback is expected to start generating a packfile with the
309 + * given options. The pack shall be generated asynchronously so that
310 + * the caller can consume the pack data and progress output while the
311 + * pack is being generated.
312 + *
313 + * This callback is optional. Sources that cannot generate packfiles
314 + * shall leave it unset.
315 + *
316 + * The callback is expected to return 0 on success and populate the
317 + * `out` pointer with the pack generator, a negative error code
318 + * otherwise.
319 + */
320 + int (*generate_pack)(struct odb_source *source,
321 + struct odb_pack_generator **out,
322 + const struct odb_generate_pack_options *opts);
323 +
324 + /*
325 + * This callback is expected to check the integrity of the object source
326 + * and report any errors found via the fsck options. The checks performed
327 + * are backend-specific.
328 + *
329 + * The callback is expected to return 0 on success, a negative error
330 + * code otherwise.
331 + */
332 + int (*fsck)(struct odb_source *source,
333 + struct odb_fsck_options *options);
299 334 };
300 335
301 336 /*
@@ -548,4 +583,30 @@ static inline bool odb_source_optimize_required(struct odb_source *source,
548 583 return source->optimize_required(source, opts);
549 584 }
550 585
586 + /*
587 + * Start generating a packfile from the given source with the given options.
588 + * The pack is generated asynchronously; the caller is expected to consume the
589 + * file descriptors exposed via the pack generator and to then wait for
590 + * completion via `odb_pack_generator_finish()`.
591 + *
592 + * Returns 0 on success and populates the `out` pointer with the pack
593 + * generator, a negative error code otherwise.
594 + */
595 + static inline int odb_source_generate_pack(struct odb_source *source,
596 + struct odb_pack_generator **out,
597 + const struct odb_generate_pack_options *opts)
598 + {
599 + return source->generate_pack(source, out, opts);
600 + }
601 +
602 + /*
603 + * Check the integrity of the object database source. The checks performed
604 + * are backend-specific. Returns 0 on success, a negative error code otherwise.
605 + */
606 + static inline int odb_source_fsck(struct odb_source *source,
607 + struct odb_fsck_options *opts)
608 + {
609 + return source->fsck(source, opts);
610 + }
611 +
551 612 #endif
modified odb/streaming.c
@@ -184,7 +184,6 @@ static int istream_source(struct odb_stream **out,
184 184 {
185 185 struct odb_source *source;
186 186
187 - odb_prepare_alternates(odb);
188 187 for (source = odb->sources; source; source = source->next)
189 188 if (!odb_source_read_object_stream(out, source, oid))
190 189 return 0;
modified oss-fuzz/fuzz-pack-headers.c
@@ -9,7 +9,7 @@ int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
9 9 size_t len;
10 10
11 11 unpack_object_header_buffer((const unsigned char *)data,
12 - (unsigned long)size, &type, &len);
12 + size, &type, &len);
13 13
14 14 return 0;
15 15 }
modified pack-bitmap.c
@@ -717,7 +717,6 @@ static int open_bitmap(struct repository *r,
717 717
718 718 assert(!bitmap_git->map);
719 719
720 - odb_prepare_alternates(r->objects);
721 720 for (source = r->objects->sources; source; source = source->next) {
722 721 struct odb_source_files *files = odb_source_files_downcast(source);
723 722
@@ -3417,7 +3416,6 @@ int verify_bitmap_files(struct repository *r)
3417 3416 struct packed_git *p;
3418 3417 int res = 0;
3419 3418
3420 - odb_prepare_alternates(r->objects);
3421 3419 for (source = r->objects->sources; source; source = source->next) {
3422 3420 struct odb_source_files *files = odb_source_files_downcast(source);
3423 3421 struct multi_pack_index *m = get_multi_pack_index(files->packed);
modified pack-check.c
@@ -34,7 +34,7 @@ int check_pack_crc(struct packed_git *p, struct pack_window **w_curs,
34 34 uint32_t data_crc = crc32(0, NULL, 0);
35 35
36 36 do {
37 - unsigned long avail;
37 + size_t avail;
38 38 void *data = use_pack(p, w_curs, offset, &avail);
39 39 if (avail > len)
40 40 avail = len;
@@ -71,7 +71,7 @@ static int verify_packfile(struct repository *r,
71 71
72 72 git_hash_init(&ctx, r->hash_algo);
73 73 do {
74 - unsigned long remaining;
74 + size_t remaining;
75 75 unsigned char *in = use_pack(p, w_curs, offset, &remaining);
76 76 offset += remaining;
77 77 if (!pack_sig_ofs)
modified packfile.c
@@ -620,7 +620,7 @@ static int in_window(struct repository *r, struct pack_window *win,
620 620 unsigned char *use_pack(struct packed_git *p,
621 621 struct pack_window **w_cursor,
622 622 off_t offset,
623 - unsigned long *left)
623 + size_t *left)
624 624 {
625 625 struct pack_window *win = *w_cursor;
626 626
@@ -866,12 +866,11 @@ struct packfile_list_entry *packfile_store_get_packs(struct odb_source_packed *s
866 866 return store->packs.head;
867 867 }
868 868
869 - unsigned long unpack_object_header_buffer(const unsigned char *buf,
870 - unsigned long len, enum object_type *type, size_t *sizep)
869 + size_t unpack_object_header_buffer(const unsigned char *buf, size_t len,
870 + enum object_type *type, size_t *sizep)
871 871 {
872 872 unsigned shift;
873 - size_t size, c;
874 - unsigned long used = 0;
873 + size_t size, c, used = 0;
875 874
876 875 c = buf[used++];
877 876 *type = (c >> 4) & 7;
@@ -960,8 +959,7 @@ int unpack_object_header(struct packed_git *p,
960 959 size_t *sizep)
961 960 {
962 961 unsigned char *base;
963 - unsigned long left;
964 - unsigned long used;
962 + size_t left, used;
965 963 enum object_type type;
966 964
967 965 /* use_pack() assures us we have [base, base + 20) available
@@ -1938,7 +1936,6 @@ int has_object_pack(struct repository *r, const struct object_id *oid)
1938 1936 {
1939 1937 struct odb_source *source;
1940 1938
1941 - odb_prepare_alternates(r->objects);
1942 1939 for (source = r->objects->sources; source; source = source->next) {
1943 1940 struct odb_source_files *files = odb_source_files_downcast(source);
1944 1941 if (!odb_source_read_object_info(&files->packed->base, oid, NULL, 0))
modified packfile.h
@@ -77,8 +77,6 @@ static inline struct repo_for_each_pack_data repo_for_eack_pack_data_init(struct
77 77 {
78 78 struct repo_for_each_pack_data data = { 0 };
79 79
80 - odb_prepare_alternates(repo->objects);
81 -
82 80 for (struct odb_source *source = repo->objects->sources; source; source = source->next) {
83 81 struct odb_source_files *files = odb_source_files_downcast(source);
84 82 struct packfile_list_entry *entry = packfile_store_get_packs(files->packed);
@@ -240,7 +238,8 @@ uint32_t get_pack_fanout(struct packed_git *p, uint32_t value);
240 238
241 239 struct object_database;
242 240
243 - unsigned char *use_pack(struct packed_git *, struct pack_window **, off_t, unsigned long *);
241 + unsigned char *use_pack(struct packed_git *, struct pack_window **, off_t,
242 + size_t *);
244 243 void close_pack_windows(struct packed_git *);
245 244 void close_pack(struct packed_git *);
246 245 void unuse_pack(struct pack_window **);
@@ -299,7 +298,8 @@ int packfile_fill_entry(struct packed_git *p,
299 298 int is_pack_valid(struct packed_git *);
300 299 void *unpack_entry(struct repository *r, struct packed_git *, off_t,
301 300 enum object_type *, size_t *);
302 - unsigned long unpack_object_header_buffer(const unsigned char *buf, unsigned long len, enum object_type *type, size_t *sizep);
301 + size_t unpack_object_header_buffer(const unsigned char *buf, size_t len,
302 + enum object_type *type, size_t *sizep);
303 303 size_t get_size_from_delta(struct packed_git *, struct pack_window **, off_t);
304 304 int unpack_object_header(struct packed_git *, struct pack_window **, off_t *, size_t *);
305 305 off_t get_delta_base(struct packed_git *p, struct pack_window **w_curs,
modified parse.c
@@ -134,6 +134,15 @@ int git_parse_ssize_t(const char *value, ssize_t *ret)
134 134 return 1;
135 135 }
136 136
137 + int git_parse_size_t(const char *value, size_t *ret)
138 + {
139 + uintmax_t tmp;
140 + if (!git_parse_unsigned(value, &tmp, maximum_signed_value_of_type(size_t)))
141 + return 0;
142 + *ret = tmp;
143 + return 1;
144 + }
145 +
137 146 int git_parse_double(const char *value, double *ret)
138 147 {
139 148 char *end;
modified parse.h
@@ -4,6 +4,7 @@
4 4 int git_parse_signed(const char *value, intmax_t *ret, intmax_t max);
5 5 int git_parse_unsigned(const char *value, uintmax_t *ret, uintmax_t max);
6 6 int git_parse_ssize_t(const char *, ssize_t *);
7 + int git_parse_size_t(const char *, size_t *);
7 8 int git_parse_ulong(const char *, unsigned long *);
8 9 int git_parse_uint(const char *value, unsigned int *ret);
9 10 int git_parse_int(const char *value, int *ret);
modified refs/reftable-backend.c
@@ -1003,7 +1003,8 @@ static int prepare_transaction_update(struct write_transaction_table_arg **out,
1003 1003 struct reftable_addition *addition;
1004 1004
1005 1005 ret = reftable_stack_new_addition(&addition, be->stack,
1006 - &reftable_be_write_options(refs)->opts);
1006 + &reftable_be_write_options(refs)->opts,
1007 + REFTABLE_STACK_NEW_ADDITION_RELOAD);
1007 1008 if (ret) {
1008 1009 if (ret == REFTABLE_LOCK_ERROR)
1009 1010 strbuf_addstr(err, "cannot lock references");
@@ -2009,7 +2010,8 @@ static int reftable_be_rename_ref(struct ref_store *ref_store,
2009 2010 if (ret)
2010 2011 goto done;
2011 2012 ret = reftable_stack_add(arg.be->stack, &write_copy_table, &arg,
2012 - &reftable_be_write_options(refs)->opts);
2013 + &reftable_be_write_options(refs)->opts,
2014 + REFTABLE_STACK_NEW_ADDITION_RELOAD);
2013 2015
2014 2016 done:
2015 2017 assert(ret != REFTABLE_API_ERROR);
@@ -2039,7 +2041,8 @@ static int reftable_be_copy_ref(struct ref_store *ref_store,
2039 2041 if (ret)
2040 2042 goto done;
2041 2043 ret = reftable_stack_add(arg.be->stack, &write_copy_table, &arg,
2042 - &reftable_be_write_options(refs)->opts);
2044 + &reftable_be_write_options(refs)->opts,
2045 + REFTABLE_STACK_NEW_ADDITION_RELOAD);
2043 2046
2044 2047 done:
2045 2048 assert(ret != REFTABLE_API_ERROR);
@@ -2421,7 +2424,8 @@ static int reftable_be_create_reflog(struct ref_store *ref_store,
2421 2424 arg.stack = be->stack;
2422 2425
2423 2426 ret = reftable_stack_add(be->stack, &write_reflog_existence_table, &arg,
2424 - &reftable_be_write_options(refs)->opts);
2427 + &reftable_be_write_options(refs)->opts,
2428 + REFTABLE_STACK_NEW_ADDITION_RELOAD);
2425 2429
2426 2430 done:
2427 2431 return ret;
@@ -2495,7 +2499,8 @@ static int reftable_be_delete_reflog(struct ref_store *ref_store,
2495 2499 arg.stack = be->stack;
2496 2500
2497 2501 ret = reftable_stack_add(be->stack, &write_reflog_delete_table, &arg,
2498 - &reftable_be_write_options(refs)->opts);
2502 + &reftable_be_write_options(refs)->opts,
2503 + REFTABLE_STACK_NEW_ADDITION_RELOAD);
2499 2504
2500 2505 assert(ret != REFTABLE_API_ERROR);
2501 2506 return ret;
@@ -2617,7 +2622,8 @@ static int reftable_be_reflog_expire(struct ref_store *ref_store,
2617 2622 goto done;
2618 2623
2619 2624 ret = reftable_stack_new_addition(&add, be->stack,
2620 - &reftable_be_write_options(refs)->opts);
2625 + &reftable_be_write_options(refs)->opts,
2626 + REFTABLE_STACK_NEW_ADDITION_RELOAD);
2621 2627 if (ret < 0)
2622 2628 goto done;
2623 2629
modified reftable/block.c
@@ -87,7 +87,10 @@ int block_writer_init(struct block_writer *bw, uint8_t typ, uint8_t *block,
87 87 REFTABLE_CALLOC_ARRAY(bw->zstream, 1);
88 88 if (!bw->zstream)
89 89 return REFTABLE_OUT_OF_MEMORY_ERROR;
90 - deflateInit(bw->zstream, 9);
90 + if (deflateInit(bw->zstream, 9) != Z_OK) {
91 + REFTABLE_FREE_AND_NULL(bw->zstream);
92 + return REFTABLE_ZLIB_ERROR;
93 + }
91 94 }
92 95
93 96 return 0;
modified reftable/reftable-stack.h
@@ -58,13 +58,22 @@ uint64_t reftable_stack_next_update_index(struct reftable_stack *st);
58 58 /* holds a transaction to add tables at the top of a stack. */
59 59 struct reftable_addition;
60 60
61 + enum {
62 + /*
63 + * Reload the stack when the stack is out-of-date after locking it.
64 + */
65 + REFTABLE_STACK_NEW_ADDITION_RELOAD = (1 << 0),
66 + };
67 +
61 68 /*
62 69 * returns a new transaction to add reftables to the given stack. As a side
63 - * effect, the ref database is locked.
70 + * effect, the ref database is locked. Accepts REFTABLE_STACK_NEW_ADDITION_*
71 + * flags.
64 72 */
65 73 int reftable_stack_new_addition(struct reftable_addition **dest,
66 74 struct reftable_stack *st,
67 - const struct reftable_write_options *opts);
75 + const struct reftable_write_options *opts,
76 + unsigned int flags);
68 77
69 78 /* Adds a reftable to transaction. */
70 79 int reftable_addition_add(struct reftable_addition *add,
@@ -84,12 +93,14 @@ void reftable_addition_destroy(struct reftable_addition *add);
84 93 /*
85 94 * Add a new table to the stack. The write_table function must call
86 95 * reftable_writer_set_limits, add refs and return an error value.
96 + * The flags are passed through to `reftable_stack_new_addition()`.
87 97 */
88 98 int reftable_stack_add(struct reftable_stack *st,
89 99 int (*write_table)(struct reftable_writer *wr,
90 100 void *write_arg),
91 101 void *write_arg,
92 - const struct reftable_write_options *opts);
102 + const struct reftable_write_options *opts,
103 + unsigned flags);
93 104
94 105 struct reftable_iterator;
95 106
modified reftable/stack.c
@@ -659,7 +659,8 @@ static void reftable_addition_close(struct reftable_addition *add)
659 659
660 660 static int reftable_stack_init_addition(struct reftable_addition *add,
661 661 struct reftable_stack *st,
662 - const struct reftable_write_options *opts)
662 + const struct reftable_write_options *opts,
663 + unsigned int flags)
663 664 {
664 665 struct reftable_buf lock_file_name = REFTABLE_BUF_INIT;
665 666 int err;
@@ -685,11 +686,15 @@ static int reftable_stack_init_addition(struct reftable_addition *add,
685 686 err = stack_uptodate(st);
686 687 if (err < 0)
687 688 goto done;
688 - if (err > 0) {
689 + if (err > 0 && flags & REFTABLE_STACK_NEW_ADDITION_RELOAD) {
689 690 err = reftable_stack_reload_maybe_reuse(add->stack, 1);
690 691 if (err)
691 692 goto done;
692 693 }
694 + if (err > 0) {
695 + err = REFTABLE_OUTDATED_ERROR;
696 + goto done;
697 + }
693 698
694 699 add->next_update_index = reftable_stack_next_update_index(st);
695 700 done:
@@ -703,12 +708,13 @@ static int stack_try_add(struct reftable_stack *st,
703 708 int (*write_table)(struct reftable_writer *wr,
704 709 void *arg),
705 710 void *arg,
706 - const struct reftable_write_options *opts)
711 + const struct reftable_write_options *opts,
712 + unsigned flags)
707 713 {
708 714 struct reftable_addition add;
709 715 int err;
710 716
711 - err = reftable_stack_init_addition(&add, st, opts);
717 + err = reftable_stack_init_addition(&add, st, opts, flags);
712 718 if (err < 0)
713 719 goto done;
714 720
@@ -725,10 +731,17 @@ done:
725 731 int reftable_stack_add(struct reftable_stack *st,
726 732 int (*write)(struct reftable_writer *wr, void *arg),
727 733 void *arg,
728 - const struct reftable_write_options *opts)
734 + const struct reftable_write_options *opts,
735 + unsigned flags)
729 736 {
730 - int err = stack_try_add(st, write, arg, opts);
737 + int err = stack_try_add(st, write, arg, opts, flags);
731 738 if (err < 0) {
739 + if (err == REFTABLE_OUTDATED_ERROR) {
740 + /* Ignore error return, we want to propagate
741 + REFTABLE_OUTDATED_ERROR.
742 + */
743 + reftable_stack_reload(st);
744 + }
732 745 return err;
733 746 }
734 747
@@ -830,7 +843,8 @@ done:
830 843
831 844 int reftable_stack_new_addition(struct reftable_addition **dest,
832 845 struct reftable_stack *st,
833 - const struct reftable_write_options *opts)
846 + const struct reftable_write_options *opts,
847 + unsigned int flags)
834 848 {
835 849 int err;
836 850
@@ -838,7 +852,7 @@ int reftable_stack_new_addition(struct reftable_addition **dest,
838 852 if (!*dest)
839 853 return REFTABLE_OUT_OF_MEMORY_ERROR;
840 854
841 - err = reftable_stack_init_addition(*dest, st, opts);
855 + err = reftable_stack_init_addition(*dest, st, opts, flags);
842 856 if (err) {
843 857 reftable_free(*dest);
844 858 *dest = NULL;
@@ -1826,7 +1840,12 @@ static int reftable_stack_clean_locked(struct reftable_stack *st)
1826 1840 int reftable_stack_clean(struct reftable_stack *st)
1827 1841 {
1828 1842 struct reftable_addition *add = NULL;
1829 - int err = reftable_stack_new_addition(&add, st, NULL);
1843 + int err = reftable_stack_new_addition(&add, st, NULL, 0);
1844 + if (err < 0) {
1845 + goto done;
1846 + }
1847 +
1848 + err = reftable_stack_reload(st);
1830 1849 if (err < 0) {
1831 1850 goto done;
1832 1851 }
modified reftable/writer.c
@@ -150,6 +150,7 @@ int reftable_writer_new(struct reftable_writer **out,
150 150 {
151 151 struct reftable_write_options opts = {0};
152 152 struct reftable_writer *wp;
153 + int err;
153 154
154 155 if (_opts)
155 156 opts = *_opts;
@@ -177,7 +178,12 @@ int reftable_writer_new(struct reftable_writer **out,
177 178 wp->opts = opts;
178 179 wp->hash_id = hash_id;
179 180 wp->flush = flush_func;
180 - writer_reinit_block_writer(wp, REFTABLE_BLOCK_TYPE_REF);
181 + err = writer_reinit_block_writer(wp, REFTABLE_BLOCK_TYPE_REF);
182 + if (err < 0) {
183 + reftable_free(wp->block);
184 + reftable_free(wp);
185 + return err;
186 + }
181 187
182 188 *out = wp;
183 189
modified send-pack.c
@@ -42,16 +42,17 @@ int option_parse_push_signed(const struct option *opt,
42 42 die("bad %s argument: %s", opt->long_name, arg);
43 43 }
44 44
45 - static void feed_object(struct repository *r,
46 - const struct object_id *oid, FILE *fh, int negative)
45 + static void append_negative_object(struct repository *r,
46 + struct oid_array *haves,
47 + const struct object_id *oid)
47 48 {
48 - if (negative && !odb_has_object(r->objects, oid, 0))
49 + /*
50 + * The remote end may have advertised objects that we do not have in
51 + * our object database. Skip those, as we cannot use them as boundary.
52 + */
53 + if (!odb_has_object(r->objects, oid, 0))
49 54 return;
50 -
51 - if (negative)
52 - putc('^', fh);
53 - fputs(oid_to_hex(oid), fh);
54 - putc('\n', fh);
55 + oid_array_append(haves, oid);
55 56 }
56 57
57 58 /*
@@ -62,92 +63,58 @@ static int pack_objects(struct repository *r,
62 63 struct oid_array *negotiated,
63 64 struct send_pack_args *args)
64 65 {
65 - /*
66 - * The child becomes pack-objects --revs; we feed
67 - * the revision parameters to it via its stdin and
68 - * let its stdout go back to the other end.
69 - */
70 - struct child_process po = CHILD_PROCESS_INIT;
71 - FILE *po_in;
66 + struct odb_generate_pack_options opts = ODB_GENERATE_PACK_OPTIONS_INIT;
67 + struct odb_pack_generator *generator;
72 68 int rc;
73 69
74 70 trace2_region_enter("send_pack", "pack_objects", r);
75 - strvec_push(&po.args, "pack-objects");
76 - strvec_push(&po.args, "--all-progress-implied");
77 - strvec_push(&po.args, "--revs");
78 - strvec_push(&po.args, "--stdout");
79 - if (args->use_thin_pack)
80 - strvec_push(&po.args, "--thin");
81 - if (args->use_ofs_delta)
82 - strvec_push(&po.args, "--delta-base-offset");
83 - if (args->quiet || !args->progress)
84 - strvec_push(&po.args, "-q");
71 +
72 + opts.thin = args->use_thin_pack;
73 + opts.ofs_delta = args->use_ofs_delta;
85 74 if (args->progress)
86 - strvec_push(&po.args, "--progress");
87 - if (is_repository_shallow(r))
88 - strvec_push(&po.args, "--shallow");
89 - if (args->disable_bitmaps)
90 - strvec_push(&po.args, "--no-use-bitmap-index");
91 - po.in = -1;
92 - po.out = args->stateless_rpc ? -1 : fd;
93 - po.git_cmd = 1;
94 - po.clean_on_exit = 1;
95 - if (start_command(&po))
96 - die_errno("git pack-objects failed");
75 + opts.progress = ODB_GENERATE_PACK_PROGRESS_VERBOSE;
76 + opts.shallow = is_repository_shallow(r);
77 + opts.disable_bitmaps = args->disable_bitmaps;
97 78
98 79 /*
99 - * We feed the pack-objects we just spawned with revision
100 - * parameters by writing to the pipe.
80 + * The pack is either written directly to the remote's descriptor, or,
81 + * in the case of a stateless RPC, read back from a pipe so that we
82 + * can wrap the pack data into pkt-lines.
101 83 */
102 - po_in = xfdopen(po.in, "w");
84 + opts.pack_fd = args->stateless_rpc ? -1 : fd;
85 +
103 86 for (size_t i = 0; i < advertised->nr; i++)
104 - feed_object(r, &advertised->oid[i], po_in, 1);
87 + append_negative_object(r, &opts.haves, &advertised->oid[i]);
105 88 for (size_t i = 0; i < negotiated->nr; i++)
106 - feed_object(r, &negotiated->oid[i], po_in, 1);
89 + append_negative_object(r, &opts.haves, &negotiated->oid[i]);
107 90
108 91 while (refs) {
109 92 if (!is_null_oid(&refs->old_oid))
110 - feed_object(r, &refs->old_oid, po_in, 1);
93 + append_negative_object(r, &opts.haves, &refs->old_oid);
111 94 if (!is_null_oid(&refs->new_oid))
112 - feed_object(r, &refs->new_oid, po_in, 0);
95 + oid_array_append(&opts.wants, &refs->new_oid);
113 96 refs = refs->next;
114 97 }
115 98
116 - fflush(po_in);
117 - if (ferror(po_in))
118 - die_errno("error writing to pack-objects");
119 - fclose(po_in);
99 + if (odb_generate_pack(r->objects, &generator, &opts))
100 + die("git pack-objects failed");
101 + odb_generate_pack_options_release(&opts);
120 102
121 103 if (args->stateless_rpc) {
122 104 char *buf = xmalloc(LARGE_PACKET_MAX);
123 105 while (1) {
124 - ssize_t n = xread(po.out, buf, LARGE_PACKET_MAX);
106 + ssize_t n = xread(generator->out, buf, LARGE_PACKET_MAX);
125 107 if (n <= 0)
126 108 break;
127 109 send_sideband(fd, -1, buf, n, LARGE_PACKET_MAX);
128 110 }
129 111 free(buf);
130 - close(po.out);
131 - po.out = -1;
112 + close(generator->out);
132 113 }
133 114
134 - rc = finish_command(&po);
135 - if (rc) {
136 - /*
137 - * For a normal non-zero exit, we assume pack-objects wrote
138 - * something useful to stderr. For death by signal, though,
139 - * we should mention it to the user. The exception is SIGPIPE
140 - * (141), because that's a normal occurrence if the remote end
141 - * hangs up (and we'll report that by trying to read the unpack
142 - * status).
143 - */
144 - if (rc > 128 && rc != 141)
145 - error("pack-objects died of signal %d", rc - 128);
146 - trace2_region_leave("send_pack", "pack_objects", r);
147 - return -1;
148 - }
115 + rc = odb_pack_generator_finish(generator);
149 116 trace2_region_leave("send_pack", "pack_objects", r);
150 - return 0;
117 + return rc;
151 118 }
152 119
153 120 static int receive_unpack_status(struct packet_reader *reader)
@@ -768,7 +735,7 @@ int send_pack(struct repository *r,
768 735 goto out;
769 736 }
770 737 if (!args->stateless_rpc)
771 - /* Closed by pack_objects() via start_command() */
738 + /* Consumed by the pack generator in pack_objects() */
772 739 fd[1] = -1;
773 740 }
774 741 if (args->stateless_rpc && cmds_sent)
modified sequencer.c
@@ -6278,7 +6278,6 @@ int sequencer_make_script(struct repository *r, struct strbuf *out,
6278 6278 revs.sort_order = REV_SORT_IN_GRAPH_ORDER;
6279 6279 revs.topo_order = 1;
6280 6280
6281 - revs.pretty_given = 1;
6282 6281 repo_config_get_string(the_repository, "rebase.instructionFormat", &format);
6283 6282 if (!format || !*format) {
6284 6283 free(format);
modified serve.c
@@ -46,6 +46,9 @@ static int promisor_remote_advertise(struct repository *r,
46 46 static void promisor_remote_receive(struct repository *r,
47 47 const char *remotes)
48 48 {
49 + if (!remotes)
50 + die("promisor-remote capability requires an argument");
51 +
49 52 mark_promisor_remotes_as_accepted(r, remotes);
50 53 }
51 54
modified setup.c
@@ -2880,12 +2880,6 @@ int init_db(struct repository *repo,
2880 2880 reinit = create_default_files(repo, template_dir, original_git_dir,
2881 2881 &repo_fmt, init_shared_repository);
2882 2882
2883 - if (!(flags & INIT_DB_SKIP_REFDB))
2884 - create_reference_database(repo, initial_branch, flags & INIT_DB_QUIET);
2885 - create_object_database(repo);
2886 -
2887 - startup_info->have_repository = 1;
2888 -
2889 2883 if (repo_settings_get_shared_repository(repo)) {
2890 2884 char buf[10];
2891 2885 /* We do not spell "group" and such, so that
@@ -2907,6 +2901,12 @@ int init_db(struct repository *repo,
2907 2901 repo_config_set(repo, "receive.denyNonFastforwards", "true");
2908 2902 }
2909 2903
2904 + if (!(flags & INIT_DB_SKIP_REFDB))
2905 + create_reference_database(repo, initial_branch, flags & INIT_DB_QUIET);
2906 + create_object_database(repo);
2907 +
2908 + startup_info->have_repository = 1;
2909 +
2910 2910 if (!(flags & INIT_DB_QUIET)) {
2911 2911 int len = strlen(git_dir);
2912 2912
modified t/helper/test-delta.c
@@ -32,7 +32,7 @@ int cmd__delta(int argc, const char **argv)
32 32 die_errno("unable to read '%s'", argv[3]);
33 33
34 34 if (argv[1][1] == 'd') {
35 - unsigned long delta_size;
35 + size_t delta_size;
36 36 out_buf = diff_delta(from.buf, from.len,
37 37 data.buf, data.len,
38 38 &delta_size, 0);
modified t/helper/test-pack-deltas.c
@@ -22,7 +22,7 @@ static unsigned long do_compress(void **pptr, unsigned long size)
22 22 {
23 23 git_zstream stream;
24 24 void *in, *out;
25 - unsigned long maxsize;
25 + size_t maxsize;
26 26
27 27 git_deflate_init(&stream, 1);
28 28 maxsize = git_deflate_bound(&stream, size);
@@ -49,7 +49,7 @@ static void write_ref_delta(struct hashfile *f,
49 49 {
50 50 unsigned char header[MAX_PACK_OBJECT_HEADER];
51 51 unsigned long delta_size, compressed_size, hdrlen;
52 - size_t size, base_size;
52 + size_t size, base_size, delta_size_st = 0;
53 53 enum object_type type;
54 54 void *base_buf, *delta_buf;
55 55 void *buf = odb_read_object(the_repository->objects,
@@ -65,7 +65,8 @@ static void write_ref_delta(struct hashfile *f,
65 65 die("unable to read %s", oid_to_hex(base));
66 66
67 67 delta_buf = diff_delta(base_buf, base_size,
68 - buf, size, &delta_size, 0);
68 + buf, size, &delta_size_st, 0);
69 + delta_size = cast_size_t_to_ulong(delta_size_st);
69 70
70 71 compressed_size = do_compress(&delta_buf, delta_size);
71 72
modified t/t0041-usage.sh
@@ -107,4 +107,11 @@ test_expect_success 'for-each-ref usage error' '
107 107 test_grep "usage" actual.err
108 108 '
109 109
110 + test_expect_success 'git --shallow-file without a value' '
111 + test_must_fail git --shallow-file >actual 2>actual.err &&
112 + test_line_count = 0 actual &&
113 + test_grep "no file given for " actual.err &&
114 + test_grep "usage" actual.err
115 + '
116 +
110 117 test_done
modified t/t1450-fsck.sh
@@ -844,6 +844,11 @@ test_expect_success 'alternate objects are correctly blamed' '
844 844 echo "../../alt.git/objects" >.git/objects/info/alternates &&
845 845 mkdir alt.git/objects/$(dirname $path) &&
846 846 >alt.git/objects/$(dirname $path)/$(basename $path) &&
847 +
848 + # Without "--full", only the local object source is checked.
849 + git fsck --no-full >out 2>&1 &&
850 + test_must_be_empty out &&
851 +
847 852 test_must_fail git fsck >out 2>&1 &&
848 853 test_grep alt.git out
849 854 '
modified t/t5325-reverse-index.sh
@@ -204,4 +204,12 @@ test_expect_success 'fsck catches invalid header: hash function' '
204 204 "reverse-index file .* has unsupported hash id"
205 205 '
206 206
207 + test_expect_success 'fsck --no-full checks rev-index, --connectivity-only does not' '
208 + test_must_fail git -C corrupt fsck --no-full 2>err &&
209 + test_grep "has unsupported hash id" err &&
210 +
211 + git -C corrupt fsck --connectivity-only 2>err &&
212 + test_grep ! "has unsupported hash id" err
213 + '
214 +
207 215 test_done
modified t/t5516-fetch-push.sh
@@ -1903,20 +1903,20 @@ test_expect_success 'push with config push.useBitmaps' '
1903 1903 test_unconfig push.useBitmaps &&
1904 1904 GIT_TRACE2_EVENT="$PWD/default" \
1905 1905 git push --quiet testrepo main:test &&
1906 - test_subcommand git pack-objects --all-progress-implied --revs --stdout \
1907 - --thin --delta-base-offset -q <default &&
1906 + test_subcommand git pack-objects --revs --stdout --thin \
1907 + --delta-base-offset --quiet <default &&
1908 1908
1909 1909 test_config push.useBitmaps true &&
1910 1910 GIT_TRACE2_EVENT="$PWD/true" \
1911 1911 git push --quiet testrepo main:test2 &&
1912 - test_subcommand git pack-objects --all-progress-implied --revs --stdout \
1913 - --thin --delta-base-offset -q <true &&
1912 + test_subcommand git pack-objects --revs --stdout --thin \
1913 + --delta-base-offset --quiet <true &&
1914 1914
1915 1915 test_config push.useBitmaps false &&
1916 1916 GIT_TRACE2_EVENT="$PWD/false" \
1917 1917 git push --quiet testrepo main:test3 &&
1918 - test_subcommand git pack-objects --all-progress-implied --revs --stdout \
1919 - --thin --delta-base-offset -q --no-use-bitmap-index <false
1918 + test_subcommand git pack-objects --revs --stdout --thin \
1919 + --delta-base-offset --no-use-bitmap-index --quiet <false
1920 1920 '
1921 1921
1922 1922 test_expect_success 'push with config pack.usePathWalk=true' '
modified t/t5701-git-serve.sh
@@ -71,6 +71,17 @@ test_expect_success 'request invalid capability' '
71 71 test_grep "unknown capability" err
72 72 '
73 73
74 + test_expect_success 'promisor-remote capability requires an argument' '
75 + test-tool pkt-line pack >in <<-EOF &&
76 + command=ls-refs
77 + object-format=$(test_oid algo)
78 + promisor-remote
79 + 0000
80 + EOF
81 + test_must_fail test-tool serve-v2 --stateless-rpc 2>err <in &&
82 + test_grep "promisor-remote capability requires an argument" err
83 + '
84 +
74 85 test_expect_success 'request with no command' '
75 86 test-tool pkt-line pack >in <<-EOF &&
76 87 agent=git/test
modified t/t7900-maintenance.sh
@@ -7,9 +7,6 @@ test_description='git maintenance builtin'
7 7 GIT_TEST_COMMIT_GRAPH=0
8 8 GIT_TEST_MULTI_PACK_INDEX=0
9 9
10 - # Ensure that auto-maintenance detaches as usual.
11 - sane_unset GIT_TEST_MAINT_AUTO_DETACH
12 -
13 10 test_lazy_prereq XMLLINT '
14 11 xmllint --version
15 12 '
@@ -67,41 +64,60 @@ test_expect_success 'run [--auto|--quiet] with gc strategy' '
67 64 '
68 65
69 66 test_expect_success 'maintenance.auto config option' '
70 - GIT_TRACE2_EVENT="$(pwd)/default" git commit --quiet --allow-empty -m 1 &&
71 - test_subcommand git maintenance run --auto --quiet --detach <default &&
72 - GIT_TRACE2_EVENT="$(pwd)/true" \
73 - git -c maintenance.auto=true \
74 - commit --quiet --allow-empty -m 2 &&
75 - test_subcommand git maintenance run --auto --quiet --detach <true &&
76 - GIT_TRACE2_EVENT="$(pwd)/false" \
77 - git -c maintenance.auto=false \
78 - commit --quiet --allow-empty -m 3 &&
79 - test_subcommand ! git maintenance run --auto --quiet --detach <false
67 + test_when_finished "rm -rf repo" &&
68 + git init repo &&
69 + (
70 + cd repo &&
71 + sane_unset GIT_TEST_MAINT_AUTO_DETACH &&
72 +
73 + GIT_TRACE2_EVENT="$(pwd)/default" git commit --quiet --allow-empty -m 1 &&
74 + test_subcommand git maintenance run --auto --quiet --detach <default &&
75 + GIT_TRACE2_EVENT="$(pwd)/true" \
76 + git -c maintenance.auto=true \
77 + commit --quiet --allow-empty -m 2 &&
78 + test_subcommand git maintenance run --auto --quiet --detach <true &&
79 + GIT_TRACE2_EVENT="$(pwd)/false" \
80 + git -c maintenance.auto=false \
81 + commit --quiet --allow-empty -m 3 &&
82 + test_subcommand ! git maintenance run --auto --quiet --detach <false
83 + )
80 84 '
81 85
82 86 test_expect_success 'gc.auto config option' '
83 - GIT_TRACE2_EVENT="$(pwd)/default" git commit --quiet --allow-empty -m 1 &&
84 - test_subcommand git maintenance run --auto --quiet --detach <default &&
85 - GIT_TRACE2_EVENT="$(pwd)/true" \
86 - git -c gc.auto=1 commit --quiet --allow-empty -m 2 &&
87 - test_subcommand git maintenance run --auto --quiet --detach <true &&
88 - GIT_TRACE2_EVENT="$(pwd)/false" \
89 - git -c gc.auto=0 commit --quiet --allow-empty -m 3 &&
90 - test_subcommand ! git maintenance run --auto --quiet --detach <false
87 + test_when_finished "rm -rf repo" &&
88 + git init repo &&
89 + (
90 + cd repo &&
91 + sane_unset GIT_TEST_MAINT_AUTO_DETACH &&
92 +
93 + GIT_TRACE2_EVENT="$(pwd)/default" git commit --quiet --allow-empty -m 1 &&
94 + test_subcommand git maintenance run --auto --quiet --detach <default &&
95 + GIT_TRACE2_EVENT="$(pwd)/true" \
96 + git -c gc.auto=1 commit --quiet --allow-empty -m 2 &&
97 + test_subcommand git maintenance run --auto --quiet --detach <true &&
98 + GIT_TRACE2_EVENT="$(pwd)/false" \
99 + git -c gc.auto=0 commit --quiet --allow-empty -m 3 &&
100 + test_subcommand ! git maintenance run --auto --quiet --detach <false
101 + )
91 102 '
92 103
93 104 test_expect_success 'maintenance.auto overrides gc.auto' '
94 - test_when_finished "rm -f trace" &&
105 + test_when_finished "rm -rf repo" &&
106 + git init repo &&
107 + (
108 + cd repo &&
109 + sane_unset GIT_TEST_MAINT_AUTO_DETACH &&
95 110
96 - test_config maintenance.auto false &&
97 - test_config gc.auto 1 &&
98 - GIT_TRACE2_EVENT="$(pwd)/trace" git commit --quiet --allow-empty -m 1 &&
99 - test_subcommand ! git maintenance run --auto --quiet --detach <trace &&
111 + git config set maintenance.auto false &&
112 + git config set gc.auto 1 &&
113 + GIT_TRACE2_EVENT="$(pwd)/trace" git commit --quiet --allow-empty -m 1 &&
114 + test_subcommand ! git maintenance run --auto --quiet --detach <trace &&
100 115
101 - test_config maintenance.auto true &&
102 - test_config gc.auto 0 &&
103 - GIT_TRACE2_EVENT="$(pwd)/trace" git commit --quiet --allow-empty -m 1 &&
104 - test_subcommand git maintenance run --auto --quiet --detach <trace
116 + git config set maintenance.auto true &&
117 + git config set gc.auto 0 &&
118 + GIT_TRACE2_EVENT="$(pwd)/trace" git commit --quiet --allow-empty -m 1 &&
119 + test_subcommand git maintenance run --auto --quiet --detach <trace
120 + )
105 121 '
106 122
107 123 for cfg in maintenance.autoDetach gc.autoDetach
modified t/unit-tests/u-reftable-stack.c
@@ -127,7 +127,7 @@ static void write_n_ref_tables(struct reftable_stack *st,
127 127 cl_reftable_set_hash(ref.value.val1, i, REFTABLE_HASH_SHA1);
128 128
129 129 cl_assert_equal_i(reftable_stack_add(st,
130 - &write_test_ref, &ref, &opts), 0);
130 + &write_test_ref, &ref, &opts, 0), 0);
131 131 }
132 132 }
133 133
@@ -168,7 +168,7 @@ void test_reftable_stack__add_one(void)
168 168 err = reftable_new_stack(&st, dir, NULL);
169 169 cl_assert(!err);
170 170
171 - err = reftable_stack_add(st, write_test_ref, &ref, &opts);
171 + err = reftable_stack_add(st, write_test_ref, &ref, &opts, 0);
172 172 cl_assert(!err);
173 173
174 174 err = reftable_stack_read_ref(st, ref.refname, &dest);
@@ -231,9 +231,12 @@ void test_reftable_stack__uptodate(void)
231 231 cl_assert_equal_i(reftable_new_stack(&st1, dir, NULL), 0);
232 232 cl_assert_equal_i(reftable_new_stack(&st2, dir, NULL), 0);
233 233 cl_assert_equal_i(reftable_stack_add(st1, write_test_ref,
234 - &ref1, NULL), 0);
234 + &ref1, NULL, 0), 0);
235 235 cl_assert_equal_i(reftable_stack_add(st2, write_test_ref,
236 - &ref2, NULL), 0);
236 + &ref2, NULL, 0), REFTABLE_OUTDATED_ERROR);
237 + cl_assert_equal_i(reftable_stack_reload(st2), 0);
238 + cl_assert_equal_i(reftable_stack_add(st2, write_test_ref,
239 + &ref2, NULL, 0), 0);
237 240 reftable_stack_destroy(st1);
238 241 reftable_stack_destroy(st2);
239 242 clear_dir(dir);
@@ -257,7 +260,7 @@ void test_reftable_stack__transaction_api(void)
257 260
258 261 reftable_addition_destroy(add);
259 262
260 - cl_assert_equal_i(reftable_stack_new_addition(&add, st, NULL), 0);
263 + cl_assert_equal_i(reftable_stack_new_addition(&add, st, NULL, 0), 0);
261 264 cl_assert_equal_i(reftable_addition_add(add, write_test_ref,
262 265 &ref), 0);
263 266 cl_assert_equal_i(reftable_addition_commit(add), 0);
@@ -298,17 +301,21 @@ void test_reftable_stack__transaction_with_reload(void)
298 301
299 302 cl_assert_equal_i(reftable_new_stack(&st1, dir, NULL), 0);
300 303 cl_assert_equal_i(reftable_new_stack(&st2, dir, NULL), 0);
301 - cl_assert_equal_i(reftable_stack_new_addition(&add, st1, NULL), 0);
304 + cl_assert_equal_i(reftable_stack_new_addition(&add, st1, NULL, 0), 0);
302 305 cl_assert_equal_i(reftable_addition_add(add, write_test_ref,
303 306 &refs[0]), 0);
304 307 cl_assert_equal_i(reftable_addition_commit(add), 0);
305 308 reftable_addition_destroy(add);
306 309
307 310 /*
308 - * The second stack is now outdated, but it should automatically reload it
309 - * with the newer updates.
311 + * The second stack is now outdated, which we should notice. We do not
312 + * create the addition and lock the stack by default, but allow the
313 + * reload to happen when REFTABLE_STACK_NEW_ADDITION_RELOAD is set.
310 314 */
311 - cl_assert_equal_i(reftable_stack_new_addition(&add, st2, NULL), 0);
315 + cl_assert_equal_i(reftable_stack_new_addition(&add, st2, NULL, 0),
316 + REFTABLE_OUTDATED_ERROR);
317 + cl_assert_equal_i(reftable_stack_new_addition(&add, st2, NULL,
318 + REFTABLE_STACK_NEW_ADDITION_RELOAD), 0);
312 319 cl_assert_equal_i(reftable_addition_add(add, write_test_ref,
313 320 &refs[1]), 0);
314 321 cl_assert_equal_i(reftable_addition_commit(add), 0);
@@ -356,7 +363,7 @@ void test_reftable_stack__transaction_api_performs_auto_compaction(void)
356 363 * better control over when exactly auto compaction runs.
357 364 */
358 365 cl_assert_equal_i(reftable_stack_new_addition(&add,
359 - st, &write_opts), 0);
366 + st, &write_opts, 0), 0);
360 367 cl_assert_equal_i(reftable_addition_add(add,
361 368 write_test_ref, &ref), 0);
362 369 cl_assert_equal_i(reftable_addition_commit(add), 0);
@@ -393,7 +400,7 @@ void test_reftable_stack__auto_compaction_fails_gracefully(void)
393 400
394 401 cl_assert_equal_i(reftable_new_stack(&st, dir, NULL), 0);
395 402 cl_assert_equal_i(reftable_stack_add(st, write_test_ref,
396 - &ref, NULL), 0);
403 + &ref, NULL, 0), 0);
397 404 cl_assert_equal_i(st->merged->tables_len, 1);
398 405 cl_assert_equal_i(st->stats.attempts, 0);
399 406 cl_assert_equal_i(st->stats.failures, 0);
@@ -411,7 +418,7 @@ void test_reftable_stack__auto_compaction_fails_gracefully(void)
411 418 write_file_buf(table_path.buf, "", 0);
412 419
413 420 ref.update_index = 2;
414 - err = reftable_stack_add(st, write_test_ref, &ref, NULL);
421 + err = reftable_stack_add(st, write_test_ref, &ref, NULL, 0);
415 422 cl_assert(!err);
416 423 cl_assert_equal_i(st->merged->tables_len, 2);
417 424 cl_assert_equal_i(st->stats.attempts, 1);
@@ -446,9 +453,9 @@ void test_reftable_stack__update_index_check(void)
446 453
447 454 cl_assert_equal_i(reftable_new_stack(&st, dir, NULL), 0);
448 455 cl_assert_equal_i(reftable_stack_add(st, write_test_ref,
449 - &ref1, NULL), 0);
456 + &ref1, NULL, 0), 0);
450 457 cl_assert_equal_i(reftable_stack_add(st, write_test_ref,
451 - &ref2, NULL), REFTABLE_API_ERROR);
458 + &ref2, NULL, 0), REFTABLE_API_ERROR);
452 459 reftable_stack_destroy(st);
453 460 clear_dir(dir);
454 461 }
@@ -462,7 +469,7 @@ void test_reftable_stack__lock_failure(void)
462 469 cl_assert_equal_i(reftable_new_stack(&st, dir, NULL), 0);
463 470 for (i = -1; i != REFTABLE_EMPTY_TABLE_ERROR; i--)
464 471 cl_assert_equal_i(reftable_stack_add(st, write_error,
465 - &i, NULL), i);
472 + &i, NULL, 0), i);
466 473
467 474 reftable_stack_destroy(st);
468 475 clear_dir(dir);
@@ -506,7 +513,7 @@ void test_reftable_stack__add(void)
506 513
507 514 for (i = 0; i < N; i++)
508 515 cl_assert_equal_i(reftable_stack_add(st, write_test_ref,
509 - &refs[i], &opts), 0);
516 + &refs[i], &opts, 0), 0);
510 517
511 518 for (i = 0; i < N; i++) {
512 519 struct write_log_arg arg = {
@@ -514,7 +521,7 @@ void test_reftable_stack__add(void)
514 521 .update_index = reftable_stack_next_update_index(st),
515 522 };
516 523 cl_assert_equal_i(reftable_stack_add(st, write_test_log,
517 - &arg, &opts), 0);
524 + &arg, &opts, 0), 0);
518 525 }
519 526
520 527 cl_assert_equal_i(reftable_stack_compact_all(st, &opts, NULL), 0);
@@ -597,7 +604,7 @@ void test_reftable_stack__iterator(void)
597 604
598 605 for (i = 0; i < N; i++)
599 606 cl_assert_equal_i(reftable_stack_add(st, write_test_ref,
600 - &refs[i], NULL), 0);
607 + &refs[i], NULL, 0), 0);
601 608
602 609 for (i = 0; i < N; i++) {
603 610 struct write_log_arg arg = {
@@ -606,7 +613,7 @@ void test_reftable_stack__iterator(void)
606 613 };
607 614
608 615 cl_assert_equal_i(reftable_stack_add(st, write_test_log,
609 - &arg, NULL), 0);
616 + &arg, NULL, 0), 0);
610 617 }
611 618
612 619 reftable_stack_init_ref_iterator(st, &it);
@@ -678,11 +685,11 @@ void test_reftable_stack__log_normalize(void)
678 685
679 686 input.value.update.message = (char *) "one\ntwo";
680 687 cl_assert_equal_i(reftable_stack_add(st, write_test_log,
681 - &arg, NULL), REFTABLE_API_ERROR);
688 + &arg, NULL, 0), REFTABLE_API_ERROR);
682 689
683 690 input.value.update.message = (char *) "one";
684 691 cl_assert_equal_i(reftable_stack_add(st, write_test_log,
685 - &arg, NULL), 0);
692 + &arg, NULL, 0), 0);
686 693 cl_assert_equal_i(reftable_stack_read_log(st, input.refname,
687 694 &dest), 0);
688 695 cl_assert_equal_s(dest.value.update.message, "one\n");
@@ -690,7 +697,7 @@ void test_reftable_stack__log_normalize(void)
690 697 input.value.update.message = (char *) "two\n";
691 698 arg.update_index = 2;
692 699 cl_assert_equal_i(reftable_stack_add(st, write_test_log,
693 - &arg, NULL), 0);
700 + &arg, NULL, 0), 0);
694 701 cl_assert_equal_i(reftable_stack_read_log(st, input.refname,
695 702 &dest), 0);
696 703 cl_assert_equal_s(dest.value.update.message, "two\n");
@@ -740,7 +747,7 @@ void test_reftable_stack__tombstone(void)
740 747 }
741 748 for (i = 0; i < N; i++)
742 749 cl_assert_equal_i(reftable_stack_add(st, write_test_ref,
743 - &refs[i], NULL), 0);
750 + &refs[i], NULL, 0), 0);
744 751
745 752 for (i = 0; i < N; i++) {
746 753 struct write_log_arg arg = {
@@ -748,7 +755,7 @@ void test_reftable_stack__tombstone(void)
748 755 .update_index = reftable_stack_next_update_index(st),
749 756 };
750 757 cl_assert_equal_i(reftable_stack_add(st, write_test_log,
751 - &arg, NULL), 0);
758 + &arg, NULL, 0), 0);
752 759 }
753 760
754 761 cl_assert_equal_i(reftable_stack_read_ref(st, "branch",
@@ -794,7 +801,7 @@ void test_reftable_stack__hash_id(void)
794 801
795 802 cl_assert_equal_i(reftable_new_stack(&st, dir, NULL), 0);
796 803 cl_assert_equal_i(reftable_stack_add(st, write_test_ref,
797 - &ref, NULL), 0);
804 + &ref, NULL, 0), 0);
798 805
799 806 /* can't read it with the wrong hash ID. */
800 807 cl_assert_equal_i(reftable_new_stack(&st32, dir,
@@ -862,7 +869,7 @@ void test_reftable_stack__reflog_expire(void)
862 869 .update_index = reftable_stack_next_update_index(st),
863 870 };
864 871 cl_assert_equal_i(reftable_stack_add(st, write_test_log,
865 - &arg, NULL), 0);
872 + &arg, NULL, 0), 0);
866 873 }
867 874
868 875 cl_assert_equal_i(reftable_stack_compact_all(st, NULL, NULL), 0);
@@ -901,7 +908,7 @@ void test_reftable_stack__empty_add(void)
901 908
902 909 cl_assert_equal_i(reftable_new_stack(&st, dir, NULL), 0);
903 910 cl_assert_equal_i(reftable_stack_add(st, write_nothing,
904 - NULL, NULL), 0);
911 + NULL, NULL, 0), 0);
905 912 cl_assert_equal_i(reftable_new_stack(&st2, dir, NULL), 0);
906 913 clear_dir(dir);
907 914 reftable_stack_destroy(st);
@@ -940,7 +947,7 @@ void test_reftable_stack__auto_compaction(void)
940 947 };
941 948 snprintf(name, sizeof(name), "branch%04"PRIuMAX, (uintmax_t)i);
942 949
943 - err = reftable_stack_add(st, write_test_ref, &ref, &opts);
950 + err = reftable_stack_add(st, write_test_ref, &ref, &opts, 0);
944 951 cl_assert(!err);
945 952
946 953 err = reftable_stack_auto_compact(st, &opts);
@@ -976,7 +983,7 @@ void test_reftable_stack__auto_compaction_factor(void)
976 983 };
977 984 xsnprintf(name, sizeof(name), "branch%04"PRIuMAX, (uintmax_t)i);
978 985
979 - err = reftable_stack_add(st, &write_test_ref, &ref, &opts);
986 + err = reftable_stack_add(st, &write_test_ref, &ref, &opts, 0);
980 987 cl_assert(!err);
981 988
982 989 cl_assert(i < 5 || st->merged->tables_len < 5 * fastlogN(i, 5));
@@ -1057,7 +1064,7 @@ void test_reftable_stack__add_performs_auto_compaction(void)
1057 1064 ref.refname = buf;
1058 1065
1059 1066 cl_assert_equal_i(reftable_stack_add(st, write_test_ref,
1060 - &ref, &write_opts), 0);
1067 + &ref, &write_opts, 0), 0);
1061 1068
1062 1069 /*
1063 1070 * The stack length should grow continuously for all runs where
@@ -1296,7 +1303,7 @@ void test_reftable_stack__invalid_limit_updates(void)
1296 1303
1297 1304 reftable_addition_destroy(add);
1298 1305
1299 - cl_assert_equal_i(reftable_stack_new_addition(&add, st, &opts), 0);
1306 + cl_assert_equal_i(reftable_stack_new_addition(&add, st, &opts, 0), 0);
1300 1307
1301 1308 /*
1302 1309 * write_limits_after_ref also updates the update indexes after adding
modified t/unit-tests/u-reftable-table.c
@@ -32,7 +32,8 @@ void test_reftable_table__seek_once(void)
32 32 ret = reftable_table_new(&table, &source, "name");
33 33 cl_assert(!ret);
34 34
35 - reftable_table_init_ref_iterator(table, &it);
35 + ret = reftable_table_init_ref_iterator(table, &it);
36 + cl_assert_equal_i(ret, 0);
36 37 ret = reftable_iterator_seek_ref(&it, "");
37 38 cl_assert(!ret);
38 39 ret = reftable_iterator_next_ref(&it, &ref);
@@ -74,7 +75,8 @@ void test_reftable_table__reseek(void)
74 75 ret = reftable_table_new(&table, &source, "name");
75 76 cl_assert(!ret);
76 77
77 - reftable_table_init_ref_iterator(table, &it);
78 + ret = reftable_table_init_ref_iterator(table, &it);
79 + cl_assert_equal_i(ret, 0);
78 80
79 81 for (size_t i = 0; i < 5; i++) {
80 82 ret = reftable_iterator_seek_ref(&it, "");
modified transport-helper.c
@@ -487,6 +487,8 @@ static int get_exporter(struct transport *transport,
487 487 /* we need to duplicate helper->in because we want to use it after
488 488 * fastexport is done with it. */
489 489 fastexport->out = dup(helper->in);
490 + if (fastexport->out < 0)
491 + return error_errno(_("could not dup helper output fd"));
490 492 strvec_push(&fastexport->args, "fast-export");
491 493 strvec_push(&fastexport->args, "--use-done-feature");
492 494 strvec_push(&fastexport->args, data->signed_tags ?
@@ -1194,7 +1196,9 @@ static int push_refs_with_export(struct transport *transport,
1194 1196
1195 1197 if (data->export_marks) {
1196 1198 strbuf_addf(&buf, "%s.tmp", data->export_marks);
1197 - rename(buf.buf, data->export_marks);
1199 + if (rename(buf.buf, data->export_marks))
1200 + warning_errno(_("could not rename '%s' to '%s'"),
1201 + buf.buf, data->export_marks);
1198 1202 strbuf_release(&buf);
1199 1203 }
1200 1204
modified upload-pack.c
@@ -197,11 +197,11 @@ static void send_client_data(int fd, const char *data, ssize_t sz,
197 197 write_or_die(fd, data, sz);
198 198 }
199 199
200 - static int write_one_shallow(const struct commit_graft *graft, void *cb_data)
200 + static int append_one_shallow(const struct commit_graft *graft, void *cb_data)
201 201 {
202 - FILE *fp = cb_data;
202 + struct oid_array *shallows = cb_data;
203 203 if (graft->nr_parent == -1)
204 - fprintf(fp, "--shallow %s\n", oid_to_hex(&graft->oid));
204 + oid_array_append(shallows, &graft->oid);
205 205 return 0;
206 206 }
207 207
@@ -299,7 +299,8 @@ static int relay_pack_data(int pack_objects_out, struct output_state *os,
299 299 static void create_pack_file(struct upload_pack_data *pack_data,
300 300 const struct string_list *uri_protocols)
301 301 {
302 - struct child_process pack_objects = CHILD_PROCESS_INIT;
302 + struct odb_generate_pack_options opts = ODB_GENERATE_PACK_OPTIONS_INIT;
303 + struct odb_pack_generator *generator;
303 304 struct output_state *output_state = xcalloc(1, sizeof(struct output_state));
304 305 char progress[128];
305 306 char abort_msg[] = "aborting due to possible repository "
@@ -307,78 +308,42 @@ static void create_pack_file(struct upload_pack_data *pack_data,
307 308 uint64_t last_sent_ms = 0;
308 309 ssize_t sz;
309 310 int i;
310 - FILE *pipe_fd;
311 -
312 - if (!pack_data->pack_objects_hook)
313 - pack_objects.git_cmd = 1;
314 - else {
315 - strvec_push(&pack_objects.args, pack_data->pack_objects_hook);
316 - strvec_push(&pack_objects.args, "git");
317 - pack_objects.use_shell = 1;
318 - }
319 311
320 312 if (pack_data->shallow_nr) {
321 - strvec_push(&pack_objects.args, "--shallow-file");
322 - strvec_push(&pack_objects.args, "");
323 - }
324 - strvec_push(&pack_objects.args, "pack-objects");
325 - strvec_push(&pack_objects.args, "--revs");
326 - if (pack_data->use_thin_pack)
327 - strvec_push(&pack_objects.args, "--thin");
328 -
329 - strvec_push(&pack_objects.args, "--stdout");
330 - if (pack_data->shallow_nr)
331 - strvec_push(&pack_objects.args, "--shallow");
332 - if (!pack_data->no_progress)
333 - strvec_push(&pack_objects.args, "--progress");
334 - if (pack_data->use_ofs_delta)
335 - strvec_push(&pack_objects.args, "--delta-base-offset");
336 - if (pack_data->use_include_tag)
337 - strvec_push(&pack_objects.args, "--include-tag");
338 - if (repo_has_accepted_promisor_remote(the_repository))
339 - strvec_push(&pack_objects.args, "--missing=allow-promisor");
340 - if (pack_data->filter_options.choice) {
341 - const char *spec =
342 - expand_list_objects_filter_spec(&pack_data->filter_options);
343 - strvec_pushf(&pack_objects.args, "--filter=%s", spec);
344 - }
345 - if (uri_protocols) {
346 - for (i = 0; i < uri_protocols->nr; i++)
347 - strvec_pushf(&pack_objects.args, "--uri-protocol=%s",
348 - uri_protocols->items[i].string);
313 + for_each_commit_graft(append_one_shallow, &opts.shallows);
314 + opts.shallow = 1;
349 315 }
350 -
351 - pack_objects.in = -1;
352 - pack_objects.out = -1;
353 - pack_objects.err = -1;
354 - pack_objects.clean_on_exit = 1;
355 -
356 - if (start_command(&pack_objects))
357 - die("git upload-pack: unable to fork git-pack-objects");
358 -
359 - pipe_fd = xfdopen(pack_objects.in, "w");
360 -
361 - if (pack_data->shallow_nr)
362 - for_each_commit_graft(write_one_shallow, pipe_fd);
363 -
364 316 for (i = 0; i < pack_data->want_obj.nr; i++)
365 - fprintf(pipe_fd, "%s\n",
366 - oid_to_hex(&pack_data->want_obj.objects[i].item->oid));
367 - fprintf(pipe_fd, "--not\n");
317 + oid_array_append(&opts.wants,
318 + &pack_data->want_obj.objects[i].item->oid);
368 319 for (i = 0; i < pack_data->have_obj.nr; i++)
369 - fprintf(pipe_fd, "%s\n",
370 - oid_to_hex(&pack_data->have_obj.objects[i].item->oid));
320 + oid_array_append(&opts.haves,
321 + &pack_data->have_obj.objects[i].item->oid);
371 322 for (i = 0; i < pack_data->extra_edge_obj.nr; i++)
372 - fprintf(pipe_fd, "%s\n",
373 - oid_to_hex(&pack_data->extra_edge_obj.objects[i].item->oid));
374 - fprintf(pipe_fd, "\n");
375 - fflush(pipe_fd);
376 - fclose(pipe_fd);
377 -
378 - /* We read from pack_objects.err to capture stderr output for
379 - * progress bar, and pack_objects.out to capture the pack data.
380 - */
323 + oid_array_append(&opts.haves,
324 + &pack_data->extra_edge_obj.objects[i].item->oid);
381 325
326 + opts.thin = pack_data->use_thin_pack;
327 + if (!pack_data->no_progress)
328 + opts.progress = ODB_GENERATE_PACK_PROGRESS_STANDARD;
329 + opts.ofs_delta = pack_data->use_ofs_delta;
330 + opts.include_tag = pack_data->use_include_tag;
331 + opts.missing_allow_promisor = repo_has_accepted_promisor_remote(the_repository);
332 + if (pack_data->filter_options.choice)
333 + opts.filter_spec = expand_list_objects_filter_spec(&pack_data->filter_options);
334 + opts.uri_protocols = uri_protocols;
335 + opts.pack_objects_hook = pack_data->pack_objects_hook;
336 + opts.pack_fd = -1;
337 + opts.progress_fd = -1;
338 +
339 + if (odb_generate_pack(the_repository->objects, &generator, &opts))
340 + die("git upload-pack: unable to generate pack");
341 + odb_generate_pack_options_release(&opts);
342 +
343 + /*
344 + * We read from generator->err to capture stderr output for the
345 + * progress bar, and generator->out to capture the pack data.
346 + */
382 347 while (1) {
383 348 uint64_t now_ms = getnanotime() / 1000000;
384 349 struct pollfd pfd[2];
@@ -393,14 +358,14 @@ static void create_pack_file(struct upload_pack_data *pack_data,
393 358 pollsize = 0;
394 359 pe = pu = -1;
395 360
396 - if (0 <= pack_objects.out) {
397 - pfd[pollsize].fd = pack_objects.out;
361 + if (0 <= generator->out) {
362 + pfd[pollsize].fd = generator->out;
398 363 pfd[pollsize].events = POLLIN;
399 364 pu = pollsize;
400 365 pollsize++;
401 366 }
402 - if (0 <= pack_objects.err) {
403 - pfd[pollsize].fd = pack_objects.err;
367 + if (0 <= generator->err) {
368 + pfd[pollsize].fd = generator->err;
404 369 pfd[pollsize].events = POLLIN;
405 370 pe = pollsize;
406 371 pollsize++;
@@ -437,15 +402,15 @@ static void create_pack_file(struct upload_pack_data *pack_data,
437 402 /* Status ready; we ship that in the side-band
438 403 * or dump to the standard error.
439 404 */
440 - sz = xread(pack_objects.err, progress,
405 + sz = xread(generator->err, progress,
441 406 sizeof(progress));
442 407 if (0 < sz) {
443 408 send_client_data(2, progress, sz,
444 409 pack_data->use_sideband);
445 410 last_sent_ms = now_ms;
446 411 } else if (sz == 0) {
447 - close(pack_objects.err);
448 - pack_objects.err = -1;
412 + close(generator->err);
413 + generator->err = -1;
449 414 }
450 415 else
451 416 goto fail;
@@ -455,15 +420,15 @@ static void create_pack_file(struct upload_pack_data *pack_data,
455 420
456 421 if (0 <= pu && (pfd[pu].revents & (POLLIN|POLLHUP))) {
457 422 bool did_send_data;
458 - int result = relay_pack_data(pack_objects.out,
423 + int result = relay_pack_data(generator->out,
459 424 output_state,
460 425 pack_data->use_sideband,
461 426 !!uri_protocols,
462 427 &did_send_data);
463 428
464 429 if (result == 0) {
465 - close(pack_objects.out);
466 - pack_objects.out = -1;
430 + close(generator->out);
431 + generator->out = -1;
467 432 } else if (result < 0) {
468 433 goto fail;
469 434 }
@@ -498,7 +463,7 @@ static void create_pack_file(struct upload_pack_data *pack_data,
498 463 }
499 464 }
500 465
501 - if (finish_command(&pack_objects)) {
466 + if (odb_pack_generator_finish(generator)) {
502 467 error("git upload-pack: git-pack-objects died with error.");
503 468 goto fail;
504 469 }