Skip to content

repack: write filtered-out objects as loose objects - #2314

Open
achakilam-nv wants to merge 2 commits into
gitgitgadget:masterfrom
achakilam-nv:filter-to-loose
Open

achakilam-nv wants to merge 2 commits into
gitgitgadget:masterfrom
achakilam-nv:filter-to-loose

Conversation

@achakilam-nv

Copy link
Copy Markdown

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.

cc: Martin Fick mfick@nvidia.com
cc: Christian Couder christian.couder@gmail.com

@achakilam-nv

Copy link
Copy Markdown
Author

/submit

@gitgitgadget

gitgitgadget Bot commented Nov 13, 2026

Copy link
Copy Markdown

Submitted as pull.2314.git.1794607911.gitgitgadget@gmail.com

To fetch this version into FETCH_HEAD:

git fetch https://github.com/gitgitgadget/git/ pr-2314/achakilam-nv/filter-to-loose-v1

To fetch this version to local tag pr-2314/achakilam-nv/filter-to-loose-v1:

git fetch --no-tags https://github.com/gitgitgadget/git/ tag pr-2314/achakilam-nv/filter-to-loose-v1

@@ -12,7 +12,7 @@ SYNOPSIS
'git repack' [-a] [-A] [-d] [-f] [-F] [-l] [-n] [-q] [-b] [-m]

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Junio C Hamano wrote on the Git mailing list (how to reply to this email):

"Adithya Chakilam via GitGitGadget" <gitgitgadget@gmail.com> writes:

> From: Adithya Chakilam <achakilam@nvidia.com>
>
> 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.

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).

> +--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.

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.

> diff --git a/builtin/repack.c b/builtin/repack.c
> index 21835453c6..140a992f1b 100644
> --- a/builtin/repack.c
> +++ b/builtin/repack.c
> @@ -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).

> +		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.

> diff --git a/repack-filtered.c b/repack-filtered.c
> index 869b9fc6e3..51c9dfeb13 100644
> --- a/repack-filtered.c
> +++ b/repack-filtered.c
> @@ -29,10 +50,18 @@ int write_filtered_pack(const struct write_pack_opts *opts,
>  		strvec_pushf(&cmd.args, "--keep-pack=%s", item->string);
>  
>  	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.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Adithya Chakilam wrote on the Git mailing list (how to reply to this email):

> > From: Adithya Chakilam <achakilam@nvidia.com>
> >
> > 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.
> 
> 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).

Sure. To add more context, this allows large objects to be offloaded
to other repositories (possibly accessed via alternates) and not packed
to avoid the delta searching overhead incurred when packing objects.
It also makes it easier to upload individual objects to a large-object
promisor that does not implement the Git protocol, supporting
the offloading workflow described in section II.4 of the
large-object-promisors document.

> > +--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.
> 
> 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.

Added a check and a test.

> > diff --git a/builtin/repack.c b/builtin/repack.c
> > index 21835453c6..140a992f1b 100644
> > --- a/builtin/repack.c
> > +++ b/builtin/repack.c
> > @@ -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).
> 
> > +             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.

Changed it to consistently use directory.


> 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.

Modified to use OPT_FILENAME and added a test as said.

> > diff --git a/repack-filtered.c b/repack-filtered.c
> > index 869b9fc6e3..51c9dfeb13 100644
> > --- a/repack-filtered.c
> > +++ b/repack-filtered.c
> > @@ -29,10 +50,18 @@ int write_filtered_pack(const struct write_pack_opts *opts,
> >               strvec_pushf(&cmd.args, "--keep-pack=%s", item->string);
> >
> >       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.

start_command did close. Thanks!

…objects

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.

Signed-off-by: Adithya Chakilam <achakilam@nvidia.com>
--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.

Signed-off-by: Adithya Chakilam <achakilam@nvidia.com>
@achakilam-nv

Copy link
Copy Markdown
Author

/submit

@gitgitgadget

gitgitgadget Bot commented Nov 25, 2026

Copy link
Copy Markdown

Submitted as pull.2314.v2.git.1795571091.gitgitgadget@gmail.com

To fetch this version into FETCH_HEAD:

git fetch https://github.com/gitgitgadget/git/ pr-2314/achakilam-nv/filter-to-loose-v2

To fetch this version to local tag pr-2314/achakilam-nv/filter-to-loose-v2:

git fetch --no-tags https://github.com/gitgitgadget/git/ tag pr-2314/achakilam-nv/filter-to-loose-v2

@@ -12,7 +12,7 @@ SYNOPSIS
'git repack' [-a] [-A] [-d] [-f] [-F] [-l] [-n] [-q] [-b] [-m]

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Junio C Hamano wrote on the Git mailing list (how to reply to this email):

"Adithya Chakilam via GitGitGadget" <gitgitgadget@gmail.com> writes:

> 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.

Micronit: the patch to the documentation says <directory>, not <dir>.

> 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.
>
> Signed-off-by: Adithya Chakilam <achakilam@nvidia.com>
> ---
>  Documentation/git-repack.adoc |  11 ++-
>  builtin/repack.c              |  25 ++++-
>  repack-filtered.c             |  44 ++++++++-
>  repack.h                      |   1 +
>  t/t7700-repack.sh             | 172 ++++++++++++++++++++++++++++++++++
>  5 files changed, 246 insertions(+), 7 deletions(-)
>
> diff --git a/Documentation/git-repack.adoc b/Documentation/git-repack.adoc
> index a1f9e64f66..bcd7c1fe54 100644
> --- a/Documentation/git-repack.adoc
> +++ b/Documentation/git-repack.adoc
> @@ -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=<directory>]] [--drop-filtered [--dry-run]]

This is getting overly long.  Time to line-wrap?

> @@ -183,6 +183,15 @@ depth is 4095.
>  	`objects` and `objects/info/alternates` sections of
>  	linkgit:gitrepository-layout[5].
>  
> +--filter-to-loose=<directory>::
> +	Like `--filter-to`, but treat `<directory>` as an object
> +	directory and write the filtered out objects there as loose
> +	objects instead of as a pack. The directory must already
> +	exist. Cannot be combined with `--filter-to` or
> +	`--max-pack-size`. Objects already present in `<directory>` are
> +	not rewritten. All objects are written before any existing
> +	pack is removed.

I wonder if the readers find this description sufficient to decide
between the use of this option and writing the evicted objects into a
pack.  What is your motivation behind the addition of this option?  What
makes it more useful to evict them as individual loose object files, as
opposed to a single 'junk' pack?  Is there some lesson or story you can
offer to help the readers decide if this option matches their needs?


> diff --git a/builtin/repack.c b/builtin/repack.c
> index 21835453c6..dc2b4a3ce2 100644
> --- a/builtin/repack.c
> +++ b/builtin/repack.c
> @@ -1,6 +1,7 @@
>  #define DISABLE_SIGN_COMPARE_WARNINGS
>  
>  #include "builtin.h"
> +#include "abspath.h"
>  #include "config.h"
>  #include "environment.h"
>  #include "parse-options.h"
> @@ -41,7 +42,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=<directory>]] [--drop-filtered [--dry-run]]"),
>  	NULL
>  };
>  
> @@ -182,6 +183,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 +259,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_FILENAME(0, "filter-to-loose", &filter_to_loose,
> +			   N_("object directory to write filtered out objects into as loose objects")),

As OPT_FILENAME() always gives you a newly allocated string, you would
need to free(filter_to_loose) below the 'cleanup' label, where you
release resources held by other variables like 'po_args', 'existing',
etc.

It may make sense to drop 'const' from the type, as it seems to be our
convention to make things we pass to free() non-const.

> @@ -282,8 +286,19 @@ 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) {
> +		if (!is_directory(filter_to_loose))
> +			die(_("'%s' is not a directory"), filter_to_loose);
> +		filter_to = filter_to_loose;
> +	}

What happens when you use --filter-to-loose and specify your own object
directory, e.g., --filter-to-loose=objects in a bare repository, or
--filter-to-loose=.git/objects?

unpack-objects runs while the source packs are still on disk, sees via
odb_freshen_object() that every filtered-out object already exists in a
pack, and skips writing loose copies.  With -d, cmd_repack() then
removes the old packs, losing those objects, I suspect.

Perhaps you want to prevent such permanent data loss by comparing
realpath(filter_to_loose) and repo_get_object_directory(repo) or
something?  Perhaps add a test to make sure this does not happen?

>  	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");

OK.

> +test_expect_success '--filter-to-loose writes filtered out objects loose' '
> +	rm -rf loose-work loose-src.git loose-dst.git &&
> +	git init --bare loose-dst.git &&
> +	git init loose-work &&
> +	test_commit -C loose-work base file1 content1 &&
> +	git clone --bare loose-work loose-src.git &&
> +	git -C loose-src.git repack -a -d &&
> +
> +	blob=$(git -C loose-src.git rev-parse HEAD:file1) &&
> +	git -C loose-src.git -c repack.writebitmaps=false repack -a -d \
> +		--filter=blob:none --filter-to-loose=../loose-dst.git/objects &&
> +
> +	test_path_is_file loose-dst.git/objects/$(test_oid_to_path $blob) &&
> +	find loose-dst.git/objects/pack -name "*.pack" >actual-packs &&
> +	test_must_be_empty actual-packs &&
> +	test_must_fail git -C loose-src.git cat-file -e $blob &&
> +
> +	echo $(pwd)/loose-dst.git/objects >loose-src.git/objects/info/alternates &&
> +	echo content1 >expect &&
> +	git -C loose-src.git cat-file blob $blob >actual &&
> +	test_cmp expect actual
> +'
> +
> +test_expect_success '--filter-to-loose writes objects that also exist in the source' '
> +	rm -rf loose-work loose-src.git loose-dst.git &&
> +	git init --bare loose-dst.git &&
> +	git init loose-work &&
> +	test_commit -C loose-work base file1 content1 &&
> +	git clone --bare loose-work loose-src.git &&
> +	git -C loose-src.git repack -a -d &&
> +
> +	blob=$(git -C loose-src.git rev-parse HEAD:file1) &&
> +	git -C loose-src.git -c repack.writebitmaps=false repack -a -d \
> +		--filter=blob:none --filter-to-loose=../loose-dst.git/objects &&
> +
> +	test_path_is_file loose-dst.git/objects/$(test_oid_to_path $blob)
> +'

Curious.  This is identical to the early half of the previous test.  If
the previous one passes, this one certainly would, as there is nothing
done differently.

objects instead of as a pack. The directory must already
exist. Cannot be combined with `--filter-to` or
`--max-pack-size`. Objects already present in `<directory>` are
not rewritten. All objects are written before any existing

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Junio C Hamano wrote on the Git mailing list (how to reply to this email):

"Adithya Chakilam via GitGitGadget" <gitgitgadget@gmail.com> writes:

> diff --git a/Documentation/git-repack.adoc b/Documentation/git-repack.adoc
> index bcd7c1fe54..1cba380177 100644
> --- a/Documentation/git-repack.adoc
> +++ b/Documentation/git-repack.adoc
> @@ -192,6 +192,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 `<directory>` as well, and with `-d` the source copies are
> +	removed once everything has been written.
> +

Does this format correctly?  Have you tried "make doc" and checked the
rendered results?

Find and imitate how the next entry `--drop-filtered::` adds second and
subsequent paragraphs by prefixing them with a line with a lone '+' on
it, and not indenting them.

>  --drop-filtered::
>  	Delete the local objects that match the `--filter` specification
>  	instead of keeping them in a separate packfile, reclaiming the

> diff --git a/repack-filtered.c b/repack-filtered.c
> index b33274b5b8..180c28a758 100644
> --- a/repack-filtered.c
> +++ b/repack-filtered.c
> + ...
> +int move_loose_filtered_objects(struct repository *repo,
> +				const struct list_objects_filter_options *filter,
> +				const char *dest_objdir,
> +				struct oidset *moved)
> +{
> +	struct odb_source_files *files =
> +		odb_source_files_downcast(repo->objects->source);
> +	struct oidset loose = OIDSET_INIT;
> +	struct oidset omitted = OIDSET_INIT;
> +	struct oidset_iter iter;
> +	const struct object_id *oid;
> +	struct strbuf src = STRBUF_INIT;
> +	struct strbuf dst = STRBUF_INIT;
> +	struct strbuf tmp = STRBUF_INIT;
> +	int ret = 0;
> +
> +	if (filter->choice != LOFC_BLOB_LIMIT)
> +		return 0;

This silently turns move_loose_filtered_objects() into a no-op for every
filter other than 'blob:limit', doesn't it?  That contradicts both the
proposed commit message and the documentation.

In particular, suppose a repository has a loose blob and the user runs

    git repack -a -d --filter=blob:none --filter-to-loose=../dst/objects

The main pack-objects invocation excludes all blobs,
write_filtered_pack() passes --stdin-packs without --unpacked so it only
sees existing packs, and move_loose_filtered_objects() returns 0
immediately.  The loose blob is neither copied to the destination nor
removed from the source.  The code should support LOFC_BLOB_NONE at
least and document and reject unsupported filters, no?

> +	ret = for_each_loose_file_in_source(&files->dirs->loose->base,
> +					    collect_loose_oid, NULL, NULL,
> +					    &loose);
> +	if (ret)
> +		goto out;
> +	ret = list_objects_filter__filter_oidset(repo, filter, &loose,
> +						 &omitted);
> +	if (ret)
> +		goto out;

Enumerating every loose object on disk and filtering purely by size
ignores reachability, pack membership, and the index.  Note that in
cmd_repack(), move_loose_filtered_objects() runs after the main pack and
any cruft pack have been written, yet before generated_pack_install()
and prune_packed_objects() have removed loose copies of newly packed
objects.

Consider three cases where a loose blob larger than the blob:limit
threshold exists in the source repository.

 * with --cruft -a -d, write_cruft_pack() packs an unreachable loose
   blob into the new cruft pack, leaving its loose file on disk until
   prune_packed_objects() runs later.  Because
   move_loose_filtered_objects() runs beforehand and checks every loose
   file, it copies that unreachable blob to the destination, breaking
   the invariant tested in [1/2] that unreachable objects in cruft packs
   are left alone.  Am I mistaken?

 * with -a -d (without --cruft), an unreachable loose blob is never
   packed, yet move_loose_filtered_objects() copies it into the
   destination and remove_moved_loose_objects() unlinks the source copy
   immediately, bypassing normal prune expiration and polluting the
   destination with unreachable objects.

 * in a non-bare repository where a large loose blob is staged in the
   index, pack-objects --indexed-objects keeps that blob in the main
   pack rather than filtering it out, yet move_loose_filtered_objects()
   still copies it to the destination.

In order to move only truly filtered-out reachable objects, a walk from
the refs over reachable objects, or filtering after packs are installed,
excluding any object that ended up in the kept/new/cruft packs or index,
is needed, instead of walking all loose files indiscriminately, no?

> +	oidset_iter_init(&omitted, &iter);
> +	while ((oid = oidset_iter_next(&iter))) {
> +		odb_loose_path(files->dirs->loose, &src, oid);
> +		dest_loose_path(&dst, dest_objdir, oid);
> +
> +		/*
> +		 * Only the destination decides whether the copy is needed;
> +		 * the object being present in the source says nothing.
> +		 */
> +		if (!access(dst.buf, F_OK)) {
> +			oidset_insert(moved, oid);
> +			continue;
> +		}

As noted on [1/2], if dest_objdir is the source repository's own object
directory, dst and src refer to the same file, so access() succeeds and
remove_moved_loose_objects() unlinks the only copy of the loose object.

> +		if (safe_create_leading_directories(repo, dst.buf) < 0) {
> +			ret = error_errno(_("unable to create leading directories of '%s'"),
> +					  dst.buf);
> +			goto out;
> +		}
> +		strbuf_reset(&tmp);
> +		strbuf_addf(&tmp, "%s.tmp-%"PRIuMAX, dst.buf,
> +			    (uintmax_t)getpid());
> +		if (copy_file(repo, tmp.buf, src.buf, 0444) ||
> +		    finalize_object_file(repo, tmp.buf, dst.buf)) {

Two issues:

 * copy_file() maps mode 0444 to 0666 (masked by umask), so the written
   loose object is writable instead of read-only 0444 as created by
   normal loose-object writes.  As we do not specify what the
   destination directory really is (other than "it looks like an object
   directory with a bunch of loose object files in it"), it might be OK
   that such details are not identical to real object files, but I
   suspect that it is not your intention to offer a poor imitation of
   it.

 * Naming the temporary file <oid>.tmp-<pid> uses O_EXCL against a fixed
   PID name and does not match the tmp_obj_ prefix that fsck ignores, so
   a crashed run leaves a file that triggers "bad sha1 file" warnings in
   'git fsck' and can block a later run with the same PID.

Using a tmp_obj_XXXXXX template with git_mkstemp_mode(..., 0444) and
copy_fd() would avoid both of these problems.

> +void remove_moved_loose_objects(struct repository *repo,
> +				const struct oidset *moved)
> +{
> +	struct odb_source_files *files =
> +		odb_source_files_downcast(repo->objects->source);
> +	struct strbuf path = STRBUF_INIT;
> +	struct oidset_iter iter;
> +	const struct object_id *oid;
> +
> +	oidset_iter_init(moved, &iter);
> +	while ((oid = oidset_iter_next(&iter))) {
> +		odb_loose_path(files->dirs->loose, &path, oid);
> +		unlink(path.buf);
> +	}
> +	strbuf_release(&path);
> +}

Unlike prune_packed_objects() that use unlink_or_warn(), this silently
ignores unlink() errors.  It also leaves empty 2-hex 'fan-out'
subdirectories behind, which may or may not matter.

> diff --git a/t/t7700-repack.sh b/t/t7700-repack.sh
> index 1b74d40551..d48ab73293 100755
> --- a/t/t7700-repack.sh
> +++ b/t/t7700-repack.sh
> @@ -547,6 +547,44 @@ setup_loose_src () {
>  	fi
>  }
>  ...
> +test_expect_success '--filter-to-loose leaves an identical existing destination object alone' '
> +	setup_loose_src &&
> +
> +	test-tool genrandom big 2k |
> +	GIT_OBJECT_DIRECTORY=loose-dst.git/objects git hash-object -w --stdin &&
> +
> +	path=loose-dst.git/objects/$(test_oid_to_path $big) &&
> +	test_path_is_file $path &&
> +
> +	ls -i $path >before &&
> +	git -C loose-src.git -c repack.writebitmaps=false repack -a -d \
> +		--filter=blob:limit=1k --filter-to-loose=../loose-dst.git/objects &&
> +
> +	ls -i $path >after &&
> +	test_cmp before after &&
> +	git -C loose-dst.git fsck
> +'

I am not sure what this wants to test.  Does this test [1/2]'s
unpack-objects code path, rather than the access() + copy_file() in
move_loose_filtered_objects() that is new code in this patch?

Also 'ls -i' as a measure of testing file identity is not portable (some
platforms lack the notion of i-num) and nowhere else in the t/ directory
do we use it.  Perhaps 'test-tool chmtime' or something would serve as a
substitute?

@gitgitgadget

gitgitgadget Bot commented Nov 25, 2026

Copy link
Copy Markdown

Junio C Hamano wrote on the Git mailing list (how to reply to this email):

"Adithya Chakilam via GitGitGadget" <gitgitgadget@gmail.com> writes:

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

Shouldn't this be "--filter=blob:limit=1m"?

> 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.

Note that as implemented, the second patch only handles blob:limit
filters and silently skips loose objects under other filters such as
blob:none, I think.

@gitgitgadget

gitgitgadget Bot commented Nov 26, 2026

Copy link
Copy Markdown

Christian Couder wrote on the Git mailing list (how to reply to this email):

On Wed, Nov 25, 2026 at 2:44 AM Adithya Chakilam via GitGitGadget
<gitgitgadget@gmail.com> wrote:
>
> 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.

One could say that it's the existing `--filter=<filter-spec>` option
that writes filtered-out objects into a separate pack, as it does so
even if `--filter-to=<dir>` isn't set. This means that
`--filter-to=<dir>` merely decides where the separate pack will be
written. When it is not set, `--filter=<filter-spec>` writes the
separate pack to the usual "<gitdir>/objects/pack/" directory.

> 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.

What if someone wants to write the filtered-out objects as loose
objects into the repository's own "<gitdir>/objects/" directory? (Also
it looks like in patch 2, passing the repository's own
"<gitdir>/objects/" directory to `--filter-to-loose` might currently
cause data loss when combined with `-d`.)

It might be more coherent with how `--filter=<filter-spec>` and
`--drop-filtered` work to separate *how* filtered-out objects are
handled from *where* they are written. For example, with an option
like `--unpack-filtered` (or `--loosen-filtered`) alongside
`--filter=<filter-spec>` defaulting to "<gitdir>/objects/".

One issue with reusing `--filter-to` for the external destination is
that, despite the documentation calling it `--filter-to=<dir>`, it
currently takes a pack path prefix (e.g. ".../objects/pack/pack")
rather than an object directory. So its meaning might differ between
pack mode and loose mode. Still this seems worth thinking through
before going for `--filter-to-loose=<dir>`.

(Note that when I implemented `--filter-to=<dir>`, `--expire-to=<dir>`
was already passing a pack path prefix, so I thought it might be
actually OK or could be clarified later, though I failed to mention
it. Now I plan to send a patch to fix the docs for both
`--filter-to=<dir>` and `--expire-to=<dir>`, or at least to discuss
this separately.)

Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant