Skip to contentSkip to conversation

repack: write filtered-out objects as loose objects

PATCH In review Adithya Chakilam via GitGitGadget 2 participants 4 messages Nov 13 – Nov 14 Documentation

lore
Share & tools
lore
#0 lore raw

cover repack: write filtered-out objects as loose objects

Some key-value object stores manage and retrieve Git objects individually. The existing --filter-to option writes filtered-out objects into packs, so using it with such stores requires unpacking the resulting packs before importing the objects.

This series adds --filter-to-loose=dir to git repack, allowing filtered-out objects to be written directly as loose objects into another object directory.

With --filter-to-loose, repack streams the filtered pack into unpack-objects, avoiding that extra step and the intermediate pack file. For example:

git repack -a --filter=blob=1m --filter-to-loose=/path/to/objects

The destination is treated as a Git object directory.

The first patch adds the option for objects already stored in packs. The second extends it to include loose objects excluded by the filter, so the destination also receives those objects. Objects already present in the destination are not rewritten. With -d, the source copies of those loose objects are removed after all objects have been written successfully.

Adithya Chakilam (2):
repack: add --filter-to-loose to write filtered-out objects as loose
objects
repack: also copy loose objects excluded by --filter-to-loose

 Documentation/git-repack.adoc |  14 ++-
 builtin/repack.c              |  32 +++++-
 repack-filtered.c             | 152 ++++++++++++++++++++++++++-
 repack.h                      |  14 +++
 t/t7700-repack.sh             | 189 ++++++++++++++++++++++++++++++++++
 5 files changed, 394 insertions(+), 7 deletions(-)

base-commit: 1891de46f79dc444a47db1c87bd0c8c76b5463f2
Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-2314%2Fachakilam-nv%2Ffilter-to-loose-v1
Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-2314/achakilam-nv/filter-to-loose-v1
Pull-Request: https://github.com/gitgitgadget/git/pull/2314

Signature · 1 line
gitgitgadget
#1 lore raw

PATCH 1/2 repack: add --filter-to-loose to write filtered-out objects as loose objects

From: Adithya Chakilam <[email protected]>

Some external object stores (like key-value) manage and retrieve git objects individually. Although repack's --filter-to option can separate objects from the main object store, it writes them into packs. Using those objects individually then requires an additional unpacking step.

Add --filter-to-loose=<dir> to write filtered-out objects directly as loose objects into <dir>, treating it as an object directory. Stream the filtered pack into unpack-objects instead of writing an intermediate pack file to disk.

The filtered pack is built with --stdin-packs, so this initially handles only objects already stored in packs. Loose objects in the source repository are left in place.

---
 Documentation/git-repack.adoc |  10 ++-
 builtin/repack.c              |  21 ++++--
 repack-filtered.c             |  44 +++++++++++-
 repack.h                      |   1 +
 t/t7700-repack.sh             | 130 ++++++++++++++++++++++++++++++++++
 5 files changed, 199 insertions(+), 7 deletions(-)
Documentation/git-repack.adoc +9 -1
index a1f9e64f66..df1390154f 100644
@@ -12,7 +12,7 @@ SYNOPSIS
 'git repack' [-a] [-A] [-d] [-f] [-F] [-l] [-n] [-q] [-b] [-m]
 	[--window=<n>] [--depth=<n>] [--threads=<n>] [--keep-pack=<pack-name>]
 	[--write-midx[=<mode>]] [--name-hash-version=<n>] [--path-walk]
-	[--filter=<filter-spec>] [--drop-filtered [--dry-run]]
+	[--filter=<filter-spec> [--filter-to-loose=<dir>]] [--drop-filtered [--dry-run]]
 
 DESCRIPTION
 -----------
@@ -183,6 +183,14 @@ depth is 4095.
 	`objects` and `objects/info/alternates` sections of
 	linkgit:gitrepository-layout[5].
 
+--filter-to-loose=<dir>::
+	Like `--filter-to`, but treat `<dir>` as an object directory
+	and write the filtered out objects there as loose objects
+	instead of as a pack. Cannot be combined with `--filter-to`
+	or `--max-pack-size`. Objects already present in `<dir>` are
+	not rewritten. All objects are written before any existing
+	pack is removed.
+
 --drop-filtered::
 	Delete the local objects that match the `--filter` specification
 	instead of keeping them in a separate packfile, reclaiming the
builtin/repack.c +17 -4
index 21835453c6..140a992f1b 100644
@@ -41,7 +41,7 @@ static const char *const git_repack_usage[] = {
 	N_("git repack [-a] [-A] [-d] [-f] [-F] [-l] [-n] [-q] [-b] [-m]\n"
 	   "[--window=<n>] [--depth=<n>] [--threads=<n>] [--keep-pack=<pack-name>]\n"
 	   "[--write-midx[=<mode>]] [--name-hash-version=<n>] [--path-walk]\n"
-	   "[--filter=<filter-spec>] [--drop-filtered [--dry-run]]"),
+	   "[--filter=<filter-spec> [--filter-to-loose=<dir>]] [--drop-filtered [--dry-run]]"),
 	NULL
 };
 
@@ -182,6 +182,7 @@ int cmd_repack(int argc,
 	const char *cruft_expiration = NULL;
 	const char *expire_to = NULL;
 	const char *filter_to = NULL;
+	const char *filter_to_loose = NULL;
 	const char *opt_window = NULL;
 	const char *opt_window_memory = NULL;
 	const char *opt_depth = NULL;
@@ -257,6 +258,8 @@ int cmd_repack(int argc,
 			   N_("pack prefix to store a pack containing pruned objects")),
 		OPT_STRING(0, "filter-to", &filter_to, N_("dir"),
 			   N_("pack prefix to store a pack containing filtered out objects")),
+		OPT_STRING(0, "filter-to-loose", &filter_to_loose, N_("dir"),
+			   N_("object directory to write filtered out objects into as loose objects")),
 		OPT_BOOL(0, "drop-filtered", &drop_filtered,
 				N_("delete filtered out objects (requires --filter)")),
 		OPT_BOOL(0, "dry-run", &dry_run,
@@ -282,8 +285,16 @@ int cmd_repack(int argc,
 	po_args.depth = xstrdup_or_null(opt_depth);
 	po_args.threads = xstrdup_or_null(opt_threads);
 
-	die_for_incompatible_opt2(drop_filtered, "--drop-filtered",
-		!!filter_to, "--filter-to");
+	die_for_incompatible_opt3(drop_filtered, "--drop-filtered",
+			!!filter_to, "--filter-to",
+			!!filter_to_loose, "--filter-to-loose");
+	die_for_incompatible_opt2(!!filter_to, "--filter-to",
+			!!filter_to_loose, "--filter-to-loose");
+	die_for_incompatible_opt2(!!filter_to_loose, "--filter-to-loose",
+			!!po_args.max_pack_size, "--max-pack-size");
+
+	if (filter_to_loose)
+		filter_to = filter_to_loose;
 
 	if (dry_run && !drop_filtered)
 		die(_("--dry-run only takes effect with --drop-filtered"));
@@ -553,7 +564,8 @@ int cmd_repack(int argc,
 		strvec_pushf(&cmd.args, "--filter=%s",
 			     expand_list_objects_filter_spec(&po_args.filter_options));
 	else if (filter_to)
-		die(_("option '%s' can only be used along with '%s'"), "--filter-to", "--filter");
+		die(_("option '%s' can only be used along with '%s'"),
+		    filter_to_loose ? "--filter-to-loose" : "--filter-to", "--filter");
 
 	if (geometry.split_factor)
 		cmd.in = -1;
@@ -698,6 +710,7 @@ int cmd_repack(int argc,
 		struct write_pack_opts opts = {
 			.po_args = &po_args,
 			.destination = filter_to,
+			.loose = !!filter_to_loose,
 			.packdir = packdir,
 			.packtmp = packtmp,
 		};
repack-filtered.c +42 -2

77 lines not on this page · raw message

repack.h +1 -0
index 61e554e4ed..d5528745ec 100644
@@ -41,6 +41,7 @@ void repack_remove_redundant_pack(struct repository *repo, const char *dir_name,
 struct write_pack_opts {
 	struct pack_objects_args *po_args;
 	const char *destination;
+	bool loose;
 	const char *packdir;
 	const char *packtmp;
 };
t/t7700-repack.sh +130 -0

136 lines not on this page · raw message

Signature · 1 line
gitgitgadget
#3 lore raw

PATCH 2/2 repack: also copy loose objects excluded by --filter-to-loose

In reply to Adithya Chakilam via GitGitGadget

From: Adithya Chakilam <[email protected]>

--filter-to-loose only writes objects that were in an existing pack, because the filtered pack is built with --stdin-packs. Loose objects that the filter excludes stay behind in the source repository, so the destination does not get the complete set of filtered-out objects.

Copy such loose objects to the destination as well. An object already present in the destination is not rewritten. With -d, the source copies are removed once everything is written.

---
 Documentation/git-repack.adoc |   4 ++
 builtin/repack.c              |  11 ++++
 repack-filtered.c             | 108 ++++++++++++++++++++++++++++++++++
 repack.h                      |  13 ++++
 t/t7700-repack.sh             |  59 +++++++++++++++++++
 5 files changed, 195 insertions(+)
Documentation/git-repack.adoc +4 -0
index df1390154f..e2025c29ee 100644
@@ -191,6 +191,10 @@ depth is 4095.
 	not rewritten. All objects are written before any existing
 	pack is removed.
 
+	Loose objects in this repository that the filter excludes are
+	copied to `<dir>` as well, and with `-d` the source copies are
+	removed once everything has been written.
+
 --drop-filtered::
 	Delete the local objects that match the `--filter` specification
 	instead of keeping them in a separate packfile, reclaiming the
builtin/repack.c +11 -0
index 140a992f1b..a946ad28a3 100644
@@ -183,6 +183,7 @@ int cmd_repack(int argc,
 	const char *expire_to = NULL;
 	const char *filter_to = NULL;
 	const char *filter_to_loose = NULL;
+	struct oidset moved_loose = OIDSET_INIT;
 	const char *opt_window = NULL;
 	const char *opt_window_memory = NULL;
 	const char *opt_depth = NULL;
@@ -721,6 +722,14 @@ int cmd_repack(int argc,
 		ret = write_filtered_pack(&opts, &existing, &names);
 		if (ret)
 			goto cleanup;
+
+		if (filter_to_loose) {
+			ret = move_loose_filtered_objects(repo,
+					&po_args.filter_options, filter_to,
+					&moved_loose);
+			if (ret)
+				goto cleanup;
+		}
 	}
 
 	string_list_sort(&names);
@@ -777,6 +786,7 @@ int cmd_repack(int argc,
 		if (show_progress)
 			opts |= PRUNE_PACKED_VERBOSE;
 		prune_packed_objects(repo, opts);
+		remove_moved_loose_objects(repo, &moved_loose);
 
 		if (!keep_unreachable &&
 		    (!(pack_everything & LOOSEN_UNREACHABLE) ||
@@ -801,6 +811,7 @@ cleanup:
 	string_list_clear(&keep_pack_list, 0);
 	string_list_clear(&names, 1);
 	oidset_clear(&drop_oids);
+	oidset_clear(&moved_loose);
 	existing_packs_release(&existing);
 	pack_geometry_release(&geometry);
 	pack_objects_args_release(&po_args);
repack-filtered.c +108 -0

117 lines not on this page · raw message

repack.h +13 -0
index d5528745ec..18a0dc6314 100644
@@ -168,6 +168,19 @@ int write_filtered_pack(const struct write_pack_opts *opts,
 			struct existing_packs *existing,
 			struct string_list *names);
 
+/*
+ * Copy loose objects that the filter excludes into the loose-object store
+ * at `dest_objdir`, recording them in `moved`.
+ */
+int move_loose_filtered_objects(struct repository *repo,
+				const struct list_objects_filter_options *filter,
+				const char *dest_objdir,
+				struct oidset *moved);
+
+/* Delete the source copies of objects copied by move_loose_filtered_objects. */
+void remove_moved_loose_objects(struct repository *repo,
+				const struct oidset *moved);
+
 int enumerate_promisor_blobs(struct repository *repo,
 			     const struct list_objects_filter_options *filter,
 			     struct oidset *to_drop);
t/t7700-repack.sh +59 -0

71 lines not on this page · raw message

Signature · 1 line
gitgitgadget
#2 lore raw

Re: [PATCH 1/2] repack: add --filter-to-loose to write filtered-out objects as loose objects

In reply to Adithya Chakilam via GitGitGadget
Quoted 7 lines · Adithya Chakilam via GitGitGadget

From: Adithya Chakilam <[email protected]>

Some external object stores (like key-value) manage and retrieve git objects individually. Although repack's --filter-to option can separate objects from the main object store, it writes them into packs. Using those objects individually then requires an additional unpacking step.

Quoted 8 lines

Add --filter-to-loose=<dir> to write filtered-out objects directly as loose objects into <dir>, treating it as an object directory. Stream the filtered pack into unpack-objects instead of writing an intermediate pack file to disk.

The filtered pack is built with --stdin-packs, so this initially handles only objects already stored in packs. Loose objects in the source repository are left in place.

I haven't decided whether this new feature makes sense or not, so I won't comment on that aspect. Take the following review comments with that in mind (i.e., pretend as if I said "if we assume doing this makes sense, then..." before every sentence).

Documentation/git-repack.adoc depth is 4095. · line 192

Quoted 7 lines

+--filter-to-loose=<dir>::

Documentation/git-repack.adoc +6 -0
+	Like `--filter-to`, but treat `<dir>` as an object directory
+	and write the filtered out objects there as loose objects
+	instead of as a pack. Cannot be combined with `--filter-to`
+	or `--max-pack-size`. Objects already present in `<dir>` are
+	not rewritten. All objects are written before any existing
+	pack is removed.

We talk about "already present in ...", so presumably we require the directory to exist before we run. Have a test that ensures that passing a non-existing directory will make the command error out before doing any real work.

builtin/repack.c static const char *const git_repack_usage[] = { · line 44

Quoted 10 lines
builtin/repack.c +1 -1
index 21835453c6..140a992f1b 100644
@@ -41,7 +41,7 @@ static const char *const git_repack_usage[] = {
 	N_("git repack [-a] [-A] [-d] [-f] [-F] [-l] [-n] [-q] [-b] [-m]\n"
 	   "[--window=<n>] [--depth=<n>] [--threads=<n>] [--keep-pack=<pack-name>]\n"
 	   "[--write-midx[=<mode>]] [--name-hash-version=<n>] [--path-walk]\n"
-	   "[--filter=<filter-spec>] [--drop-filtered [--dry-run]]"),
+	   "[--filter=<filter-spec> [--filter-to-loose=<dir>]] [--drop-filtered [--dry-run]]"),

The above documentation and help text consistently tell us that the value passed is a directory (do we use the abbreviated <dir> so commonly? If not, it would make sense to spell it out, i.e., <directory>, for clarity).

builtin/repack.c int cmd_repack(int argc, · line 262

Quoted 2 lines
builtin/repack.c +2 -0
+		OPT_STRING(0, "filter-to-loose", &filter_to_loose, N_("dir"),
+			   N_("object directory to write filtered out objects into as loose objects")),

Here, too.

This should use OPT_FILENAME so that a "git repack" started from a subdirectory with a relative path to the external loose object store will find the directory at the correct place, I think. Have a test to validate the combination to see that such a repack that was given a relative path to a directory find ...

	mkdir extra-loose-objects &&
	mkdir sub/dir &&
	(
		cd sub/dir &&
		git repack --filter-to-loose=../../extra-loose-objects ...
	)

the directory correct at the right place.

Quoted 6 lines
repack-filtered.c +0 -0
index 869b9fc6e3..51c9dfeb13 100644
@@ -29,10 +50,18 @@ int write_filtered_pack(const struct write_pack_opts *opts,
 		strvec_pushf(&cmd.args, "--keep-pack=%s", item->string);

repack-filtered.c int write_filtered_pack(const struct write_pack_opts *opts, · line 59

Quoted 10 lines
repack-filtered.c +5 -1
 
 	cmd.in = -1;
+	if (opts->loose)
+		cmd.out = unpack.in;
 
 	ret = start_command(&cmd);
-	if (ret)
+	if (ret) {
+		if (opts->loose) {
+			close(unpack.in);

Please double check that this is not closing the same thing twice. IIRC, a failing start_command() would close all file descriptors associated with cmd so you may have already closed unpack.in when start_command() gave us a failure, which presumably closed cmd.out.