[PATCH v2 0/2] repack: write filtered-out objects as loose objects
- From
- Adithya Chakilam via GitGitGadget <gitgitgadget@gmail.com>
- Date
- Nov 25, 2026, 01:44 UTC
- Message-ID
- <pull.2314.v2.git.1795571091.gitgitgadget@gmail.com>
- In-Reply-To
- <pull.2314.git.1794607911.gitgitgadget@gmail.com>
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-looseDocumentation/git-repack.adoc | 15 ++- builtin/repack.c | 36 +++++- repack-filtered.c | 152 +++++++++++++++++++++- repack.h | 14 +++ t/t7700-repack.sh | 231 ++++++++++++++++++++++++++++++++++ 5 files changed, 441 insertions(+), 7 deletions(-)
base-commit: 1891de46f79dc444a47db1c87bd0c8c76b5463f2 Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-2314%2Fachakilam-nv%2Ffilter-to-loose-v2 Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-2314/achakilam-nv/filter-to-loose-v2 Pull-Request: https://github.com/gitgitgadget/git/pull/2314
Range-diff vs v1:
1: 4488a1534e ! 1: 4157ce33a1 repack: add --filter-to-loose to write filtered-out objects as loose objects
@@ Documentation/git-repack.adoc: SYNOPSIS
[--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]]
++ [--filter=<filter-spec> [--filter-to-loose=<directory>]] [--drop-filtered [--dry-run]]
DESCRIPTION
-----------
@@ Documentation/git-repack.adoc: 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
++--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.
+
@@ Documentation/git-repack.adoc: depth is 4095.
instead of keeping them in a separate packfile, reclaiming the
## builtin/repack.c ##
+@@
+ #define DISABLE_SIGN_COMPARE_WARNINGS
+
+ #include "builtin.h"
++#include "abspath.h"
+ #include "config.h"
+ #include "environment.h"
+ #include "parse-options.h"
@@ builtin/repack.c: 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]]"),
++ "[--filter=<filter-spec> [--filter-to-loose=<directory>]] [--drop-filtered [--dry-run]]"),
NULL
};
@@ builtin/repack.c: 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"),
++ OPT_FILENAME(0, "filter-to-loose", &filter_to_loose,
+ 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)")),
@@ builtin/repack.c: int cmd_repack(int argc,
+ die_for_incompatible_opt2(!!filter_to_loose, "--filter-to-loose",
+ !!po_args.max_pack_size, "--max-pack-size");
+
-+ if (filter_to_loose)
++ if (filter_to_loose) {
++ if (!is_directory(filter_to_loose))
++ die(_("'%s' is not a directory"), filter_to_loose);
+ filter_to = filter_to_loose;
++ }
if (dry_run && !drop_filtered)
die(_("--dry-run only takes effect with --drop-filtered"));
@@ repack-filtered.c: int write_filtered_pack(const struct write_pack_opts *opts,
- if (ret)
+ if (ret) {
+ if (opts->loose) {
-+ close(unpack.in);
++ /* start_command() already closed cmd.out, i.e. unpack.in */
+ finish_command(&unpack);
+ sigchain_pop(SIGPIPE);
+ }
@@ t/t7700-repack.sh: test_expect_success '--filter-to stores filtered out objects'
+ fi
+}
+
-+test_expect_success '--filter-to-loose failure to write leaves the source intact' '
++test_expect_success '--filter-to-loose requires an existing directory' '
++ setup_loose_src &&
++
++ ls loose-src.git/objects/pack/*.pack >packs-before &&
++ test_must_fail git -C loose-src.git -c repack.writebitmaps=false \
++ repack -a -d --filter=blob:limit=1k \
++ --filter-to-loose=../no-such-dir 2>err &&
++ test_grep "no-such-dir.*is not a directory" err &&
++ test_path_is_missing no-such-dir &&
++
++ ls loose-src.git/objects/pack/*.pack >packs-after &&
++ test_cmp packs-before packs-after &&
++ git -C loose-src.git cat-file -e $big
++'
++
++test_expect_success POSIXPERM,SANITY '--filter-to-loose failure to write leaves the source intact' '
+ setup_loose_src &&
-+ >not-a-dir &&
++ mkdir read-only &&
++ chmod a-w read-only &&
++ test_when_finished "chmod u+w read-only" &&
+
+ ls loose-src.git/objects/pack/*.pack >packs-before &&
+ test_must_fail git -C loose-src.git -c repack.writebitmaps=false \
+ repack -a -d --filter=blob:limit=1k \
-+ --filter-to-loose=../not-a-dir &&
++ --filter-to-loose=../read-only &&
+
+ ls loose-src.git/objects/pack/*.pack >packs-after &&
+ test_cmp packs-before packs-after &&
@@ t/t7700-repack.sh: test_expect_success '--filter-to stores filtered out objects'
+ git -C loose-src.git cat-file -e $big
+'
+
++test_expect_success '--filter-to-loose resolves a relative path from a subdirectory' '
++ rm -rf sub-work extra-loose-objects &&
++ git init sub-work &&
++ test-tool genrandom big 2k >sub-work/big &&
++ git -C sub-work add big &&
++ test_tick &&
++ git -C sub-work commit -m big &&
++ big=$(git -C sub-work rev-parse HEAD:big) &&
++ # objects in the index are never filtered out
++ git -C sub-work update-index --force-remove big &&
++ git -C sub-work repack -a -d &&
++
++ mkdir extra-loose-objects &&
++ mkdir -p sub-work/sub/dir &&
++ (
++ cd sub-work/sub/dir &&
++ git -c repack.writebitmaps=false repack -a -d \
++ --filter=blob:limit=1k \
++ --filter-to-loose=../../../extra-loose-objects
++ ) &&
++
++ test_path_is_file extra-loose-objects/$(test_oid_to_path $big)
++'
++
+test_expect_success '--filter-to-loose does not move objects from kept packs' '
+ setup_loose_src &&
+
@@ t/t7700-repack.sh: test_expect_success '--filter-to stores filtered out objects'
+'
+
+test_expect_success '--filter-to-loose requires --filter, rejects --filter-to and --max-pack-size' '
++ mkdir -p x y &&
+ test_must_fail git -C bare.git repack -a --filter-to-loose=../x 2>err &&
+ test_grep "can only be used along with" err &&
+
2: ba2534e3bc ! 2: 0f1f0356b4 repack: also copy loose objects excluded by --filter-to-loose
@@ Documentation/git-repack.adoc: depth is 4095.
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
++ copied to `<directory>` as well, and with `-d` the source copies are
+ removed once everything has been written.
+
--drop-filtered::
@@ t/t7700-repack.sh: setup_loose_src () {
+ git -C loose-dst.git fsck
+'
+
- test_expect_success '--filter-to-loose failure to write leaves the source intact' '
+ test_expect_success '--filter-to-loose requires an existing directory' '
setup_loose_src &&
- >not-a-dir &&
-@@ t/t7700-repack.sh: test_expect_success '--filter-to-loose failure to write leaves the source intact
- git -C loose-src.git cat-file -e $big
+
+@@ t/t7700-repack.sh: test_expect_success '--filter-to-loose resolves a relative path from a subdirect
+ test_path_is_file extra-loose-objects/$(test_oid_to_path $big)
'
+test_expect_success '--filter-to-loose failure to copy a loose object leaves the source intact' '-- gitgitgadget