All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH 1/2] environment: move grafts_keep_true_parents into repo_config_values
@ 2026-09-02 11:30 Yuvraj Singh Chauhan
  2026-09-02 11:30 ` [PATCH 2/2] pack-objects: add tests for keep-true-parents Yuvraj Singh Chauhan
  0 siblings, 1 reply; 2+ messages in thread
From: Yuvraj Singh Chauhan @ 2026-09-02 11:30 UTC (permalink / raw)
  To: git

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.

Signed-off-by: Yuvraj Singh Chauhan <ysinghcin@gmail.com>
---
 builtin/pack-objects.c | 6 +++---
 commit.c               | 2 +-
 environment.c          | 9 ++++++++-
 environment.h          | 5 +++--
 4 files changed, 15 insertions(+), 7 deletions(-)

diff --git a/builtin/pack-objects.c b/builtin/pack-objects.c
index 65c2ad9a86..0d213dead1 100644
--- a/builtin/pack-objects.c
+++ b/builtin/pack-objects.c
@@ -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")),
diff --git a/commit.c b/commit.c
index ad26f0b40a..5a7ae0696c 100644
--- a/commit.c
+++ b/commit.c
@@ -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)
diff --git a/environment.c b/environment.c
index 76ee65e62b..53e8ab1255 100644
--- a/environment.c
+++ b/environment.c
@@ -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)
diff --git a/environment.h b/environment.h
index e7ec5b0437..b9f31a0aef 100644
--- a/environment.h
+++ b/environment.h
@@ -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);
 
-- 
2.43.0

^ permalink raw reply related	[flat|nested] 2+ messages in thread

* [PATCH 2/2] pack-objects: add tests for keep-true-parents
  2026-09-02 11:30 [PATCH 1/2] environment: move grafts_keep_true_parents into repo_config_values Yuvraj Singh Chauhan
@ 2026-09-02 11:30 ` Yuvraj Singh Chauhan
  0 siblings, 0 replies; 2+ messages in thread
From: Yuvraj Singh Chauhan @ 2026-09-02 11:30 UTC (permalink / raw)
  To: git

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.

Signed-off-by: Yuvraj Singh Chauhan <ysinghcin@gmail.com>
---
 t/t5300-pack-object.sh | 55 ++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 55 insertions(+)

diff --git a/t/t5300-pack-object.sh b/t/t5300-pack-object.sh
index aac139e6a0..62b29c7e05 100755
--- a/t/t5300-pack-object.sh
+++ b/t/t5300-pack-object.sh
@@ -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
-- 
2.43.0

^ permalink raw reply related	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2026-09-02 11:31 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-09-02 11:30 [PATCH 1/2] environment: move grafts_keep_true_parents into repo_config_values Yuvraj Singh Chauhan
2026-09-02 11:30 ` [PATCH 2/2] pack-objects: add tests for keep-true-parents Yuvraj Singh Chauhan

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.