diff mbox series

[1/2] t7700: add tests for `--keep-unreachable`

Message ID 20250203-b4-pks-repack-unreachable-objects-wo-packfiles-v1-1-7c4d69c5072c@pks.im (mailing list archive)
State Superseded
Headers show
Series builtin/repack: fix `--keep-unreachable` when there are no packs | expand

Commit Message

Patrick Steinhardt Feb. 3, 2025, 1:06 p.m. UTC
We don't have any tests for `git repack --keep-unreachable`. Add three
tests that exercise its behaviour with different packed states for the
unreachable object.

Note that the last test is failing. This will be fixed in the next
commit.

Signed-off-by: Patrick Steinhardt <ps@pks.im>
---
 t/t7700-repack.sh | 73 +++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 73 insertions(+)

Comments

Junio C Hamano Feb. 3, 2025, 5:13 p.m. UTC | #1
Patrick Steinhardt <ps@pks.im> writes:

> +expect_object_count () {
> +	find .git/objects \( -type d \( -name pack -o -name info \) -prune \) -o -type f -print >objects &&
> +	test_line_count = "$1" objects
> +}

So this is counting "loose" objects.  Do we want to have "loose"
somewhere in its name?

> +expect_object_in_idx () {
> +	git verify-pack -v "$1" >objects &&
> +	test_grep "^$2" objects
> +}

And this one checks if an object exists in a given pack in somewhat
an expensive way.  It can take either .idx or .pack but the name
makes the caller assume it should be called with .idx, which is OK.

Would it achieve the same effect and be faster to do something like

	git show-index <"$1" >objects &&
	test_grep "^[0-9]* $2 (" objects

instead?

Thanks.
Jeff King Feb. 3, 2025, 6:32 p.m. UTC | #2
On Mon, Feb 03, 2025 at 02:06:54PM +0100, Patrick Steinhardt wrote:

> We don't have any tests for `git repack --keep-unreachable`. Add three
> tests that exercise its behaviour with different packed states for the
> unreachable object.

There are a few in t7701. It's spelled "-k" there, so a grep for
"--keep-unreachable" would not find them.

> +test_expect_success '--keep-unreachable appends unreachable packed objects to new pack' '
> [...]
> +test_expect_success '--keep-unreachable packs unreachable loose object with existing packs' '

I think these match the two that are in t7701.

-Peff
Junio C Hamano Feb. 3, 2025, 11:53 p.m. UTC | #3
Jeff King <peff@peff.net> writes:

> On Mon, Feb 03, 2025 at 02:06:54PM +0100, Patrick Steinhardt wrote:
>
>> We don't have any tests for `git repack --keep-unreachable`. Add three
>> tests that exercise its behaviour with different packed states for the
>> unreachable object.
>
> There are a few in t7701. It's spelled "-k" there, so a grep for
> "--keep-unreachable" would not find them.

Ahh, good eyes.  Thanks.

>
>> +test_expect_success '--keep-unreachable appends unreachable packed objects to new pack' '
>> [...]
>> +test_expect_success '--keep-unreachable packs unreachable loose object with existing packs' '
>
> I think these match the two that are in t7701.

Doubly thanks.
Jeff King Feb. 4, 2025, 2:35 a.m. UTC | #4
On Mon, Feb 03, 2025 at 03:53:46PM -0800, Junio C Hamano wrote:

> Jeff King <peff@peff.net> writes:
> 
> > On Mon, Feb 03, 2025 at 02:06:54PM +0100, Patrick Steinhardt wrote:
> >
> >> We don't have any tests for `git repack --keep-unreachable`. Add three
> >> tests that exercise its behaviour with different packed states for the
> >> unreachable object.
> >
> > There are a few in t7701. It's spelled "-k" there, so a grep for
> > "--keep-unreachable" would not find them.
> 
> Ahh, good eyes.  Thanks.

I got to cheat a little as the original author of the flag. ;)

-Peff
Patrick Steinhardt Feb. 4, 2025, 7 a.m. UTC | #5
On Mon, Feb 03, 2025 at 09:35:38PM -0500, Jeff King wrote:
> On Mon, Feb 03, 2025 at 03:53:46PM -0800, Junio C Hamano wrote:
> 
> > Jeff King <peff@peff.net> writes:
> > 
> > > On Mon, Feb 03, 2025 at 02:06:54PM +0100, Patrick Steinhardt wrote:
> > >
> > >> We don't have any tests for `git repack --keep-unreachable`. Add three
> > >> tests that exercise its behaviour with different packed states for the
> > >> unreachable object.
> > >
> > > There are a few in t7701. It's spelled "-k" there, so a grep for
> > > "--keep-unreachable" would not find them.
> > 
> > Ahh, good eyes.  Thanks.
> 
> I got to cheat a little as the original author of the flag. ;)

The file is even *named* "unpack-unreachable". I didn't figure to grep
for "-k" though. Thanks for the pointer!

Patrick
diff mbox series

Patch

diff --git a/t/t7700-repack.sh b/t/t7700-repack.sh
index be1188e736..b26566473f 100755
--- a/t/t7700-repack.sh
+++ b/t/t7700-repack.sh
@@ -826,4 +826,77 @@  test_expect_success '-n overrides repack.updateServerInfo=true' '
 	test_server_info_missing
 '
 
+expect_object_count () {
+	find .git/objects \( -type d \( -name pack -o -name info \) -prune \) -o -type f -print >objects &&
+	test_line_count = "$1" objects
+}
+
+expect_object_in_idx () {
+	git verify-pack -v "$1" >objects &&
+	test_grep "^$2" objects
+}
+
+test_expect_success '--keep-unreachable appends unreachable packed objects to new pack' '
+	test_when_finished "rm -rf repo" &&
+	git init repo &&
+	(
+		cd repo &&
+		git config set core.logAllRefUpdates false &&
+
+		# Set up the repo so that all objects, including the
+		# unreachable one, are packed.
+		test_commit --no-tag unreachable &&
+		git repack -ad &&
+		expect_object_count 0 &&
+		unreachable_oid=$(git rev-parse --verify HEAD) &&
+		git commit --amend --message rewritten &&
+
+		git repack -ad --keep-unreachable &&
+		expect_object_count 0 &&
+		expect_object_in_idx .git/objects/pack/*.idx "$unreachable_oid"
+	)
+'
+
+test_expect_success '--keep-unreachable packs unreachable loose object with existing packs' '
+	test_when_finished "rm -rf repo" &&
+	git init repo &&
+	(
+		cd repo &&
+		git config set core.logAllRefUpdates false &&
+
+		# Set up the repo so that we have an existing packfile with
+		# reachable objects, only. The unreachable object as well as
+		# the rewritten commit are both loose.
+		test_commit --no-tag initial &&
+		git repack -ad &&
+		git commit --amend --message unreachable &&
+		unreachable_oid=$(git rev-parse --verify HEAD) &&
+		git commit --amend --message rewritten &&
+		expect_object_count 2 &&
+
+		git repack -ad --keep-unreachable &&
+		expect_object_count 0 &&
+		expect_object_in_idx .git/objects/pack/*.idx "$unreachable_oid"
+	)
+'
+
+test_expect_failure '--keep-unreachable packs unreachable loose object without existing packs' '
+	test_when_finished "rm -rf repo" &&
+	git init repo &&
+	(
+		cd repo &&
+		git config set core.logAllRefUpdates false &&
+
+		# Set up the repo so that all objects are unpacked.
+		test_commit --no-tag unreachable &&
+		unreachable_oid=$(git rev-parse --verify HEAD) &&
+		git commit --amend --message rewritten &&
+		expect_object_count 4 &&
+
+		git repack -ad --keep-unreachable &&
+		expect_object_count 0 &&
+		expect_object_in_idx .git/objects/pack/*.idx "$unreachable_oid"
+	)
+'
+
 test_done