diff mbox series

[v3,09/10] repack: honor `-l` when calculating pack geometry

Message ID 285695deafa5a4a49f774dc484782dd8e4fd4997.1681384405.git.ps@pks.im (mailing list archive)
State New, archived
Headers show
Series repack: fix geometric repacking with gitalternates | expand

Commit Message

Patrick Steinhardt April 13, 2023, 11:16 a.m. UTC
When the user passes `-l` to git-repack(1), then they essentially ask us
to only repack objects part of the local object database while ignoring
any packfiles part of an alternate object database. And we in fact honor
this bit when doing a geometric repack as the resulting packfile will
only ever contain local objects.

What we're missing though is that we don't take locality of packfiles
into account when computing whether the geometric sequence is intact or
not. So even though we would only ever roll up local packfiles anyway,
we could end up trying to repack because of non-local packfiles. This
does not make much sense, and in the worst case it can cause us to try
and do the geometric repack over and over again because we're never able
to restore the geometric sequence.

Fix this bug by honoring whether the user has passed `-l`. If so, we
skip adding any non-local packfiles to the pack geometry.

Signed-off-by: Patrick Steinhardt <ps@pks.im>
---
 builtin/repack.c            | 13 +++++++--
 t/t7703-repack-geometric.sh | 58 +++++++++++++++++++++++++++++++++++++
 2 files changed, 69 insertions(+), 2 deletions(-)

Comments

Derrick Stolee April 13, 2023, 1:59 p.m. UTC | #1
On 4/13/2023 7:16 AM, Patrick Steinhardt wrote:
> When the user passes `-l` to git-repack(1), then they essentially ask us
> to only repack objects part of the local object database while ignoring
> any packfiles part of an alternate object database. And we in fact honor
> this bit when doing a geometric repack as the resulting packfile will
> only ever contain local objects.

> +	# Verify that our assumptions actually hold: both generated packfiles
> +	# should have three objects and should be non-equal.
> +	packed_objects shared/.git/objects/pack/pack-*.idx >packed-objects &&
> +	test_line_count = 3 packed-objects &&
> +	packed_objects member/.git/objects/pack/pack-*.idx >packed-objetcs &&

Typo: s/packed-objetcs/packed-objects/

> +	test_line_count = 3 packed-objects &&
> +	test "$(basename member/.git/objects/pack/pack-*.pack)" != "$(basename shared/.git/objects/pack/pack-*.pack)" &&

nit: could we do this where we store the output of the previous two
commands into different files and then use "! test_cmp"?

	packed_objects shared/.git/objects/pack/pack-*.idx >shared-objects &&
	packed_objects member/.git/objects/pack/pack-*.idx >member-objects &&
	test_line_count = 3 shared-objects &&
	test_line_count = 3 member-objects &&
	! test_cmp shared-objects member-objects &&

Thanks,
-Stolee
Patrick Steinhardt April 13, 2023, 2:13 p.m. UTC | #2
On Thu, Apr 13, 2023 at 09:59:02AM -0400, Derrick Stolee wrote:
> On 4/13/2023 7:16 AM, Patrick Steinhardt wrote:
> > When the user passes `-l` to git-repack(1), then they essentially ask us
> > to only repack objects part of the local object database while ignoring
> > any packfiles part of an alternate object database. And we in fact honor
> > this bit when doing a geometric repack as the resulting packfile will
> > only ever contain local objects.
> 
> > +	# Verify that our assumptions actually hold: both generated packfiles
> > +	# should have three objects and should be non-equal.
> > +	packed_objects shared/.git/objects/pack/pack-*.idx >packed-objects &&
> > +	test_line_count = 3 packed-objects &&
> > +	packed_objects member/.git/objects/pack/pack-*.idx >packed-objetcs &&
> 
> Typo: s/packed-objetcs/packed-objects/
> 
> > +	test_line_count = 3 packed-objects &&
> > +	test "$(basename member/.git/objects/pack/pack-*.pack)" != "$(basename shared/.git/objects/pack/pack-*.pack)" &&
> 
> nit: could we do this where we store the output of the previous two
> commands into different files and then use "! test_cmp"?
> 
> 	packed_objects shared/.git/objects/pack/pack-*.idx >shared-objects &&
> 	packed_objects member/.git/objects/pack/pack-*.idx >member-objects &&
> 	test_line_count = 3 shared-objects &&
> 	test_line_count = 3 member-objects &&
> 	! test_cmp shared-objects member-objects &&

Yeah, this is definitely easier to read.

Patrick
Junio C Hamano April 13, 2023, 3:40 p.m. UTC | #3
Derrick Stolee <derrickstolee@github.com> writes:

>> +	test_line_count = 3 packed-objects &&
>> +	packed_objects member/.git/objects/pack/pack-*.idx >packed-objetcs &&
>
> Typo: s/packed-objetcs/packed-objects/
>
>> +	test_line_count = 3 packed-objects &&

That's a good one.  Because the result file created by the previous
step happens to also have three lines, the typo does not cause the
test to fail, but it is not testing what it designed to test.

Here is "git diff @{1}" after my local "rebase -i" ("diff" of the
result is easier to read than "range-diff" in this case, as I did
not touch any log message).

Thanks for a very good set of eyes.

diff --git c/t/t5331-pack-objects-stdin.sh w/t/t5331-pack-objects-stdin.sh
index 45e24fa94a..acab31667a 100755
--- c/t/t5331-pack-objects-stdin.sh
+++ w/t/t5331-pack-objects-stdin.sh
@@ -7,7 +7,7 @@ export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
 TEST_PASSES_SANITIZE_LEAK=true
 . ./test-lib.sh
 
-packed_objects() {
+packed_objects () {
 	git show-index <"$1" >tmp-object-list &&
 	cut -d' ' -f2 tmp-object-list | sort &&
 	rm tmp-object-list
diff --git c/t/t7703-repack-geometric.sh w/t/t7703-repack-geometric.sh
index 33d7977fca..57796f3437 100755
--- c/t/t7703-repack-geometric.sh
+++ w/t/t7703-repack-geometric.sh
@@ -10,7 +10,7 @@ objdir=.git/objects
 packdir=$objdir/pack
 midx=$objdir/pack/multi-pack-index
 
-packed_objects() {
+packed_objects () {
 	git show-index <"$1" >tmp-object-list &&
 	cut -d' ' -f2 tmp-object-list &&
 	rm tmp-object-list
@@ -380,7 +380,7 @@ test_expect_success '--geometric -l with non-intact geometric sequence across OD
 	# should have three objects and should be non-equal.
 	packed_objects shared/.git/objects/pack/pack-*.idx >packed-objects &&
 	test_line_count = 3 packed-objects &&
-	packed_objects member/.git/objects/pack/pack-*.idx >packed-objetcs &&
+	packed_objects member/.git/objects/pack/pack-*.idx >packed-objects &&
 	test_line_count = 3 packed-objects &&
 	test "$(basename member/.git/objects/pack/pack-*.pack)" != "$(basename shared/.git/objects/pack/pack-*.pack)" &&
diff mbox series

Patch

diff --git a/builtin/repack.c b/builtin/repack.c
index 80fc860613..5768aeb1f3 100644
--- a/builtin/repack.c
+++ b/builtin/repack.c
@@ -323,7 +323,8 @@  static int geometry_cmp(const void *va, const void *vb)
 }
 
 static void init_pack_geometry(struct pack_geometry **geometry_p,
-			       struct string_list *existing_kept_packs)
+			       struct string_list *existing_kept_packs,
+			       const struct pack_objects_args *args)
 {
 	struct packed_git *p;
 	struct pack_geometry *geometry;
@@ -333,6 +334,14 @@  static void init_pack_geometry(struct pack_geometry **geometry_p,
 	geometry = *geometry_p;
 
 	for (p = get_all_packs(the_repository); p; p = p->next) {
+		if (args->local && !p->pack_local)
+			/*
+			 * When asked to only repack local packfiles we skip
+			 * over any packfiles that are borrowed from alternate
+			 * object directories.
+			 */
+			continue;
+
 		if (!pack_kept_objects) {
 			/*
 			 * Any pack that has its pack_keep bit set will appear
@@ -900,7 +909,7 @@  int cmd_repack(int argc, const char **argv, const char *prefix)
 	if (geometric_factor) {
 		if (pack_everything)
 			die(_("options '%s' and '%s' cannot be used together"), "--geometric", "-A/-a");
-		init_pack_geometry(&geometry, &existing_kept_packs);
+		init_pack_geometry(&geometry, &existing_kept_packs, &po_args);
 		split_pack_geometry(geometry, geometric_factor);
 	}
 
diff --git a/t/t7703-repack-geometric.sh b/t/t7703-repack-geometric.sh
index d0823f2eb2..c440956ad5 100755
--- a/t/t7703-repack-geometric.sh
+++ b/t/t7703-repack-geometric.sh
@@ -10,6 +10,12 @@  objdir=.git/objects
 packdir=$objdir/pack
 midx=$objdir/pack/multi-pack-index
 
+packed_objects() {
+	git show-index <"$1" >tmp-object-list &&
+	cut -d' ' -f2 tmp-object-list &&
+	rm tmp-object-list
+ }
+
 test_expect_success '--geometric with no packs' '
 	git init geometric &&
 	test_when_finished "rm -fr geometric" &&
@@ -361,4 +367,56 @@  test_expect_success '--geometric with same pack in main and alternate ODB' '
 	test_cmp expected-files actual-files
 '
 
+test_expect_success '--geometric -l with non-intact geometric sequence across ODBs' '
+	test_when_finished "rm -fr shared member" &&
+
+	git init shared &&
+	test_commit_bulk -C shared --start=1 1 &&
+
+	git clone --shared shared member &&
+	test_commit_bulk -C member --start=2 1 &&
+
+	# Verify that our assumptions actually hold: both generated packfiles
+	# should have three objects and should be non-equal.
+	packed_objects shared/.git/objects/pack/pack-*.idx >packed-objects &&
+	test_line_count = 3 packed-objects &&
+	packed_objects member/.git/objects/pack/pack-*.idx >packed-objetcs &&
+	test_line_count = 3 packed-objects &&
+	test "$(basename member/.git/objects/pack/pack-*.pack)" != "$(basename shared/.git/objects/pack/pack-*.pack)" &&
+
+	# Perform the geometric repack. With `-l`, we should only see the local
+	# packfile and thus arrive at the conclusion that the geometric
+	# sequence is intact. We thus expect no changes.
+	#
+	# Note that we are tweaking mtimes of the packfiles so that we can
+	# verify they did not change. This is done in order to detect the case
+	# where we do repack objects, but the resulting packfile is the same.
+	test-tool chmtime --verbose =0 member/.git/objects/pack/* >expected-member-packs &&
+	git -C member repack --geometric=2 -l -d &&
+	test-tool chmtime --verbose member/.git/objects/pack/* >actual-member-packs &&
+	test_cmp expected-member-packs actual-member-packs &&
+
+	{
+		packed_objects shared/.git/objects/pack/pack-*.idx &&
+		packed_objects member/.git/objects/pack/pack-*.idx
+	} | sort >expected-objects &&
+
+	# On the other hand, when doing a non-local geometric repack we should
+	# see both packfiles and thus repack them. We expect that the shared
+	# object database was not changed.
+	test-tool chmtime --verbose =0 shared/.git/objects/pack/* >expected-shared-packs &&
+	git -C member repack --geometric=2 -d &&
+	test-tool chmtime --verbose shared/.git/objects/pack/* >actual-shared-packs &&
+	test_cmp expected-shared-packs actual-shared-packs &&
+
+	# Furthermore, we expect that the member repository now has a single
+	# packfile that contains the combined shared and non-shared objects.
+	ls member/.git/objects/pack/pack-*.idx >actual &&
+	test_line_count = 1 actual &&
+	packed_objects member/.git/objects/pack/pack-*.idx >actual-objects &&
+	test_line_count = 6 actual-objects &&
+	sort <actual-objects >actual-objects.sorted &&
+	test_cmp expected-objects actual-objects.sorted
+'
+
 test_done