environment: move grafts_keep_true_parents into repo_config_values
PATCH 1/2 environment: move grafts_keep_true_parents into repo_config_values
Move the global 'grafts_keep_true_parents' configuration variable into the repository-specific 'repo_config_values' struct.
Introduce the getter function 'repo_grafts_keep_true_parents(repo)' which checks whether 'repo->initialized' is set, falling back to 0 when uninitialized.
Update call sites in commit.c to use 'repo_grafts_keep_true_parents(r)' with the existing repository context 'r'. In builtin/pack-objects.c, bind the '--keep-true-parents' option directly to 'cfg->grafts_keep_true_parents'.
When accessing 'repo_config_values' in cmd_pack_objects, use a NULL guard 'repo ? repo : the_repository'. This ensures that invocations without a repository context (e.g. 'git pack-objects -h' outside a repo) do not dereference a NULL pointer.
--- builtin/pack-objects.c | 6 +++--- commit.c | 2 +- environment.c | 9 ++++++++- environment.h | 5 +++-- 4 files changed, 15 insertions(+), 7 deletions(-)
builtin/pack-objects.c +3 -3
@@ -5120,7 +5120,7 @@ static int parse_stdin_packs_mode(const struct option *opt, const char *arg,int cmd_pack_objects (int argc,const char **argv,const char *prefix, - struct repository *repo UNUSED) +struct repository *repo) {int use_internal_rev_list =0 ;int all_progress_implied =0 ;
@@ -5131,7 +5131,7 @@ int cmd_pack_objects(int argc,struct string_list keep_pack_list =STRING_LIST_INIT_NODUP ;struct list_objects_filter_options filter_options =LIST_OBJECTS_FILTER_INIT ; -struct repo_config_values *cfg =repo_config_values (the_repository); +struct repo_config_values *cfg =repo_config_values (repo ? repo : the_repository);struct option pack_objects_options[] = { OPT_CALLBACK_F('q', "quiet", &progress, NULL,
@@ -5215,7 +5215,7 @@ int cmd_pack_objects(int argc, N_("ignore this pack")),OPT_INTEGER (0 ,"compression" , &cfg->pack_compression_level,N_ ("pack compression level" )), -OPT_BOOL (0 ,"keep-true-parents" , &grafts_keep_true_parents, +OPT_BOOL (0 ,"keep-true-parents" , &cfg->grafts_keep_true_parents,N_ ("do not hide commits by grafts" )),OPT_BOOL (0 ,"use-bitmap-index" , &use_bitmap_index, N_("use a bitmap index if available to speed up counting objects")),
commit.c +1 -1
@@ -566,7 +566,7 @@ int parse_commit_buffer(struct repository *r, struct commit *item, const void *b* The clone is shallow if nr_parent < 0, and we must * not traverse its real parents even when we unhide them. */ -if (graft && (graft->nr_parent <0 || !grafts_keep_true_parents)) +if (graft && (graft->nr_parent <0 || !repo_grafts_keep_true_parents (r)))continue ; new_parent =lookup_commit (r, &parent);if (!new_parent)
environment.c +8 -1
@@ -56,7 +56,6 @@ char *check_roundtrip_encoding;#ifndef OBJECT_CREATION_MODE #define OBJECT_CREATION_MODE OBJECT_CREATION_USES_HARDLINKS#endif -int grafts_keep_true_parents;unsigned long pack_size_limit_cfg;#ifndef PROTECT_HFS_DEFAULT
@@ -152,6 +151,13 @@ int repo_has_symlinks(struct repository *repo) : platform_has_symlinks(); } +int repo_grafts_keep_true_parents (struct repository *repo) +{ +return repo->initialized + ?repo_config_values (repo)->grafts_keep_true_parents + :0 ; +} +const char *repo_excludes_file (struct repository *repo) {struct repo_config_values *cfg =repo_config_values (repo);
@@ -770,6 +776,7 @@ void repo_config_values_init(struct repo_config_values *cfg) cfg->core_sparse_checkout_cone =0 ; cfg->sparse_expect_files_outside_of_patterns =0 ; cfg->warn_on_object_refname_ambiguity =1 ; + cfg->grafts_keep_true_parents =0 ; }void repo_config_values_clear (struct repo_config_values *cfg)
environment.h +3 -2
@@ -139,6 +139,7 @@ struct repo_config_values {int ignore_case;int trust_executable_bit;int has_symlinks; +int grafts_keep_true_parents;/* section "sparse" config values */ int sparse_expect_files_outside_of_patterns;
@@ -193,6 +194,8 @@ int repo_trust_executable_bit(struct repository *repo);int repo_has_symlinks (struct repository *repo); +int repo_grafts_keep_true_parents (struct repository *repo); +const char *repo_excludes_file (struct repository *repo);void repo_config_values_init (struct repo_config_values *cfg);
@@ -235,8 +238,6 @@ extern int minimum_abbrev, default_abbrev;extern int assume_unchanged;extern unsigned long pack_size_limit_cfg; -extern int grafts_keep_true_parents; -const char *get_log_output_encoding (void );const char *get_commit_output_encoding (void );
Signature · 1 line
2.43.0
PATCH 2/2 pack-objects: add tests for keep-true-parents
Add tests for '--keep-true-parents' to 't5300-pack-object.sh' to ensure that:
1. Grafts hide parents by default during pack-objects.
2. The flag successfully reveals grafted-over parents.
3. It safely skips shallow commits (where 'nr_parent < 0') without causing failures or undefined behavior.
--- t/t5300-pack-object.sh | 55 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+)
t/t5300-pack-object.sh +55 -0
@@ -766,4 +766,59 @@ test_expect_success '--path-walk thin pack' ' git -C server index-pack --fix-thin --stdin <out.pack ' +test_expect_success 'setup graft and test repos' ' + git init graft-test && + ( + cd graft-test && + git commit --allow-empty -m "root" && + git commit --allow-empty -m "commit_A" && + A=$(git rev-parse HEAD) && + git commit --allow-empty -m "commit_B" && + B=$(git rev-parse HEAD) && + git commit --allow-empty -m "commit_C" && + C=$(git rev-parse HEAD) && + mkdir -p .git/info && + echo "$C $A" >.git/info/grafts && + echo "$C" >../C_sha && + echo "$B" >../B_sha && + echo "$A" >../A_sha + ) +' + +test_expect_success 'pack-objects without --keep-true-parents respects graft (hides real parent)' ' + C=$(cat C_sha) && + B=$(cat B_sha) && + echo "$C" | git -C graft-test pack-objects --revs --stdout >test-no-ktp.pack && + git init unpack-test && + git -C unpack-test index-pack --stdin <test-no-ktp.pack && + git -C unpack-test cat-file -p "$C" >/dev/null && + ! git -C unpack-test cat-file -p "$B" >/dev/null 2>&1 +' + +test_expect_success 'pack-objects --keep-true-parents ignores graft (exposes real parent)' ' + C=$(cat C_sha) && + B=$(cat B_sha) && + echo "$C" | git -C graft-test pack-objects --keep-true-parents --revs --stdout >test-ktp.pack && + git init unpack-test-ktp && + git -C unpack-test-ktp index-pack --stdin <test-ktp.pack && + git -C unpack-test-ktp cat-file -p "$C" >/dev/null && + git -C unpack-test-ktp cat-file -p "$B" >/dev/null +' + +test_expect_success 'pack-objects --keep-true-parents is safe with shallow commits' ' + git init shallow-src && + ( + cd shallow-src && + git commit --allow-empty -m "commit_A" && + git commit --allow-empty -m "commit_B" && + git commit --allow-empty -m "commit_C" + ) && + git clone --no-local --depth=1 shallow-src shallow-clone && + SHALLOW_TIP=$(git -C shallow-clone rev-parse HEAD) && + echo "$SHALLOW_TIP" | git -C shallow-clone pack-objects --keep-true-parents --revs --stdout >shallow-ktp.pack && + git init shallow-unpack && + git -C shallow-unpack index-pack --stdin <shallow-ktp.pack && + git -C shallow-unpack cat-file -p "$SHALLOW_TIP" >/dev/null +' + test_done
Signature · 1 line
2.43.0