Add refs optimize subcommand
Subsystems (3)
v2 cover Add refs optimize subcommand
This series introduces git refs optimize as a modern replacement for git pack-refs, continuing the effort to consolidate commands under the git refs namespace.
changes in v2:
- Updated documentation.
- moved pack_refs_core() from builtin/pack-refs.c to pack-refs.c.
Meet Soni (5):
builtin/pack-refs: factor out core logic into a shared library
doc: factor out common option
builtin/refs: add optimize subcommand
t0601: refactor tests to be shareable
t: add test for git refs optimize subcommand
Documentation/git-pack-refs.adoc | 54 +--- Documentation/git-refs.adoc | 10 + Documentation/pack-refs-options.adoc | 52 ++++ Makefile | 1 + builtin/pack-refs.c | 55 +--- builtin/refs.c | 16 + meson.build | 1 + pack-refs.c | 56 ++++ pack-refs.h | 23 ++ t/meson.build | 3 +- t/pack-refs-tests.sh | 431 +++++++++++++++++++++++++++ t/t0601-reffiles-pack-refs.sh | 430 +------------------------- t/t1463-refs-optimize.sh | 17 ++ 13 files changed, 616 insertions(+), 533 deletions(-) create mode 100644 Documentation/pack-refs-options.adoc create mode 100644 pack-refs.c create mode 100644 pack-refs.h create mode 100644 t/pack-refs-tests.sh create mode 100755 t/t1463-refs-optimize.sh
Range-diff against v1:
1: 0268933403 < -: ---------- builtin/pack-refs: factor out core logic into a helper -: ---------- > 1: 67a9187b59 builtin/pack-refs: factor out core logic into a shared library 2: ec14212c7b = 2: 422eaa0c8b doc: factor out common option 3: d3a4d7cb67 ! 3: 067a2baa97 builtin/refs: add optimize subcommand
@@ Documentation/git-refs.adoc: list::
linkgit:git-for-each-ref[1] and offers identical functionality.
+optimize::
-+ Pack references into a single file to improve repository performance
-+ and reduce storage usage. This subcommand is an alias for
-+ linkgit:git-pack-refs[1] and offers identical functionality.
++ Optimizes references to improve repository performance and reduce disk
++ usage. This subcommand is an alias for linkgit:git-pack-refs[1] and
++ offers identical functionality.
+
OPTIONS
-------
4: 16fa3f5445 = 4: 640ebd9a4f t0601: refactor tests to be shareable 5: 22620613d9 = 5: 37ca586bb9 t: add test for git refs optimize subcommand
base-commit: 1fa68948c3d76328236cac73d2adf33c905bd8e3
Signature · 1 line
2.34.1
PATCH v2 1/5 builtin/pack-refs: factor out core logic into a shared library
The implementation of git pack-refs is monolithic within cmd_pack_refs(), making it impossible to share its logic with other commands. To enable code reuse for the upcoming git refs optimize subcommand, refactor the core logic into a shared helper function.
Split the original builtin/pack-refs.c file into two parts:
- A new shared library file, pack-refs.c, which contains the core option parsing and packing logic in a new pack_refs_core() helper function.
- The original builtin/pack-refs.c, which is now a thin wrapper responsible only for defining the git pack-refs command and calling the shared helper.
A new pack-refs.h header is also introduced to define the public interface for this shared logic.
--- Makefile | 1 + builtin/pack-refs.c | 55 ++++---------------------------------------- meson.build | 1 + pack-refs.c | 56 +++++++++++++++++++++++++++++++++++++++++++++ pack-refs.h | 23 +++++++++++++++++++ 5 files changed, 86 insertions(+), 50 deletions(-) create mode 100644 pack-refs.c create mode 100644 pack-refs.h
Makefile +1 -0
@@ -1094,6 +1094,7 @@ LIB_OBJS += pack-bitmap.o LIB_OBJS += pack-check.o LIB_OBJS += pack-mtimes.o LIB_OBJS += pack-objects.o +LIB_OBJS += pack-refs.o LIB_OBJS += pack-revindex.o LIB_OBJS += pack-write.o LIB_OBJS += packfile.o
builtin/pack-refs.c +5 -50
65 lines not on this page · raw message
meson.build +1 -0
@@ -407,6 +407,7 @@ libgit_sources = [ 'pack-check.c', 'pack-mtimes.c', 'pack-objects.c', + 'pack-refs.c', 'pack-revindex.c', 'pack-write.c', 'packfile.c',
pack-refs.c new file +56 -0
56 lines not on this page · raw message
pack-refs.h new file +23 -0
@@ -0,0 +1,23 @@ +#ifndef PACK_REFS_H +#define PACK_REFS_H + +struct repository ; + +/* +* Shared usage string for options common to git-pack-refs(1) +* and git-refs-optimize(1). The command-specific part (e.g., "git refs optimize ") +* must be prepended by the caller. +*/ +#define PACK_REFS_OPTS \ + "[--all] [--no-prune] [--auto] [--include <pattern>] [--exclude <pattern>]" + +/* +* The core logic for pack-refs and its clones. +*/ +int pack_refs_core (int argc, +const char **argv, +const char *prefix, +struct repository *repo, +const char *const *usage_opts); + +#endif /* PACK_REFS_H */
Signature · 1 line
2.34.1
PATCH v2 2/5 doc: factor out common option
In reply to Meet SoniIn preparation for adding documentation for git refs optimize, factor out the common options from the git-pack-refs man page into a shareable file pack-refs-options.adoc and update git-pack-refs.adoc to use an include:: macro.
This change is a pure refactoring and results in no change to the final rendered documentation for pack-refs.
--- Documentation/git-pack-refs.adoc | 54 +--------------------------- Documentation/pack-refs-options.adoc | 52 +++++++++++++++++++++++++++ 2 files changed, 53 insertions(+), 53 deletions(-) create mode 100644 Documentation/pack-refs-options.adoc
Documentation/git-pack-refs.adoc +1 -53
@@ -45,59 +45,7 @@ unpacked. OPTIONS ------- ---all:: - -The command by default packs all tags and refs that are already -packed, and leaves other refs -alone. This is because branches are expected to be actively -developed and packing their tips does not help performance. -This option causes all refs to be packed as well, with the exception -of hidden refs, broken refs, and symbolic refs. Useful for a repository -with many branches of historical interests. - ---no-prune:: - -The command usually removes loose refs under `$GIT_DIR/refs` -hierarchy after packing them. This option tells it not to. - ---auto:: - -Pack refs as needed depending on the current state of the ref database. The -behavior depends on the ref format used by the repository and may change in the -future. -+ - - "files": Loose references are packed into the `packed-refs` file - based on the ratio of loose references to the size of the - `packed-refs` file. The bigger the `packed-refs` file, the more loose - references need to exist before we repack. -+ - - "reftable": Tables are compacted such that they form a geometric - sequence. For two tables N and N+1, where N+1 is newer, this - maintains the property that N is at least twice as big as N+1. Only - tables that violate this property are compacted. - ---include <pattern>:: - -Pack refs based on a `glob(7)` pattern. Repetitions of this option -accumulate inclusion patterns. If a ref is both included in `--include` and -`--exclude`, `--exclude` takes precedence. Using `--include` will preclude all -tags from being included by default. Symbolic refs and broken refs will never -be packed. When used with `--all`, it will be a noop. Use `--no-include` to clear -and reset the list of patterns. - ---exclude <pattern>:: - -Do not pack refs matching the given `glob(7)` pattern. Repetitions of this option -accumulate exclusion patterns. Use `--no-exclude` to clear and reset the list of -patterns. If a ref is already packed, including it with `--exclude` will not -unpack it. -+ -When used with `--all`, pack only loose refs which do not match any of -the provided `--exclude` patterns. -+ -When used with `--include`, refs provided to `--include`, minus refs that are -provided to `--exclude` will be packed. - +include::pack-refs-options.adoc[] BUGS ----
Documentation/pack-refs-options.adoc new file +52 -0
52 lines not on this page · raw message
Signature · 1 line
2.34.1
Re: [GSoC][PATCH v2 2/5] doc: factor out common option
Quoted 1 line · Meet Soni
Subject: Re: [GSoC][PATCH v2 2/5] doc: factor out common option
Be careful to make sure that your title is sufficiently clear. I do not think the above is sufficiently clear to let readers tell what doc was updated to factor out options that are common across which subsystem.
PATCH v2 3/5 builtin/refs: add optimize subcommand
In reply to Meet SoniAs part of the ongoing effort to consolidate reference handling, introduce a new optimize subcommand. This command provides the same functionality and exit-code behavior as git pack-refs, serving as its modern replacement.
Implement cmd_refs_optimize by having it call the pack_refs_core() helper function. This helper was factored out of the original cmd_pack_refs in a preceding commit, allowing both commands to share the same core logic as independent peers.
Add documentation for the new command. The man page leverages the shared options file, created in a previous commit, by using the AsciiDoc include:: macro to ensure consistency with git-pack-refs(1).
--- Documentation/git-refs.adoc | 10 ++++++++++ builtin/refs.c | 16 ++++++++++++++++ 2 files changed, 26 insertions(+)
Documentation/git-refs.adoc +10 -0
@@ -18,6 +18,7 @@ git refs list [--count=<count>] [--shell|--perl|--python|--tcl] [--contains[=<object>]] [--no-contains[=<object>]] [(--exclude=<pattern>)...] [--start-after=<marker>] [ --stdin | <pattern>... ] +git refs optimize [--all] [--no-prune] [--auto] [--include <pattern>] [--exclude <pattern>] DESCRIPTION -----------
@@ -38,6 +39,11 @@ list:: formatting, and sorting. This subcommand is an alias for linkgit:git-for-each-ref[1] and offers identical functionality. +optimize:: + Optimizes references to improve repository performance and reduce disk + usage. This subcommand is an alias for linkgit:git-pack-refs[1] and + offers identical functionality. + OPTIONS -------
@@ -73,6 +79,10 @@ The following options are specific to 'git refs list': include::for-each-ref-options.adoc[] +The following options are specific to 'git refs optimize': + +include::pack-refs-options.adoc[] + KNOWN LIMITATIONS -----------------
builtin/refs.c +16 -0
@@ -2,6 +2,7 @@#include "builtin.h" #include "config.h" #include "fsck.h" +#include "pack-refs.h" #include "parse-options.h" #include "refs.h" #include "strbuf.h"
@@ -14,6 +15,9 @@#define REFS_VERIFY_USAGE \ N_("git refs verify [--strict] [--verbose]") +#define REFS_OPTIMIZE_USAGE \ + N_("git refs optimize " PACK_REFS_OPTS) +static int cmd_refs_migrate (int argc,const char **argv,const char *prefix, struct repository *repo UNUSED) {
@@ -113,6 +117,16 @@ static int cmd_refs_list(int argc, const char **argv, const char *prefix,return for_each_ref_core (argc, argv, prefix, repo, refs_list_usage); } +static int cmd_refs_optimize (int argc,const char **argv,const char *prefix, +struct repository *repo) +{ +static char const *const refs_optimize_usage[] = { +REFS_OPTIMIZE_USAGE , +NULL + }; +return pack_refs_core (argc, argv, prefix, repo, refs_optimize_usage); +} + int cmd_refs(int argc, const char **argv, const char *prefix,
@@ -122,6 +136,7 @@ int cmd_refs(int argc,REFS_MIGRATE_USAGE ,REFS_VERIFY_USAGE ,"git refs list " COMMON_USAGE_FOR_EACH_REF , +REFS_OPTIMIZE_USAGE ,NULL , }; parse_opt_subcommand_fn *fn =NULL ;
@@ -129,6 +144,7 @@ int cmd_refs(int argc,OPT_SUBCOMMAND ("migrate" , &fn, cmd_refs_migrate),OPT_SUBCOMMAND ("verify" , &fn, cmd_refs_verify),OPT_SUBCOMMAND ("list" , &fn, cmd_refs_list), +OPT_SUBCOMMAND ("optimize" , &fn, cmd_refs_optimize), OPT_END(), };
Signature · 1 line
2.34.1
PATCH v2 4/5 t0601: refactor tests to be shareable
In reply to Meet SoniIn preparation for adding tests for the new git refs optimize command, refactor the existing t0601 test suite to make its logic shareable.
Move the core test logic from t0601-reffiles-pack-refs.sh into a new pack-refs-tests.sh file. Inside this new script, replace hardcoded calls to "pack-refs" with the $pack_refs variable.
The original t0601-reffiles-pack-refs.sh script now becomes a simple "driver". It is responsible for setting the default value of the variable and then sourcing the test library.
This new structure follows the established pattern used for sharing tests between git-for-each-ref and git-refs list and prepares the test suite for the refs optimize tests to be added in a subsequent commit.
--- t/pack-refs-tests.sh | 431 ++++++++++++++++++++++++++++++++++ t/t0601-reffiles-pack-refs.sh | 430 +-------------------------------- 2 files changed, 432 insertions(+), 429 deletions(-) create mode 100644 t/pack-refs-tests.sh
t/pack-refs-tests.sh new file +431 -0
@@ -0,0 +1,431 @@ +pack_refs=${pack_refs:-pack-refs} + +test_expect_success 'enable reflogs' ' + git config core.logallrefupdates true +' + +test_expect_success 'prepare a trivial repository' ' + echo Hello > A && + git update-index --add A && + git commit -m "Initial commit." && + HEAD=$(git rev-parse --verify HEAD) +' + +test_expect_success '${pack_refs} --prune --all' ' + test_path_is_missing .git/packed-refs && + git ${pack_refs} --no-prune --all && + test_path_is_file .git/packed-refs && + N=$(find .git/refs -type f | wc -l) && + test "$N" != 0 && + + git ${pack_refs} --prune --all && + test_path_is_file .git/packed-refs && + N=$(find .git/refs -type f) && + test -z "$N" +' + +SHA1= + +test_expect_success 'see if git show-ref works as expected' ' + git branch a && + SHA1=$(cat .git/refs/heads/a) && + echo "$SHA1 refs/heads/a" >expect && + git show-ref a >result && + test_cmp expect result +' + +test_expect_success 'see if a branch still exists when packed' ' + git branch b && + git ${pack_refs} --all && + rm -f .git/refs/heads/b && + echo "$SHA1 refs/heads/b" >expect && + git show-ref b >result && + test_cmp expect result +' + +test_expect_success 'git branch c/d should barf if branch c exists' ' + git branch c && + git ${pack_refs} --all && + rm -f .git/refs/heads/c && + test_must_fail git branch c/d +' + +test_expect_success 'see if a branch still exists after git ${pack_refs} --prune' ' + git branch e && + git ${pack_refs} --all --prune && + echo "$SHA1 refs/heads/e" >expect && + git show-ref e >result && + test_cmp expect result +' + +test_expect_success 'see if git ${pack_refs} --prune remove ref files' ' + git branch f && + git ${pack_refs} --all --prune && + ! test -f .git/refs/heads/f +' + +test_expect_success 'see if git ${pack_refs} --prune removes empty dirs' ' + git branch r/s/t && + git ${pack_refs} --all --prune && + ! test -e .git/refs/heads/r +' + +test_expect_success 'git branch g should work when git branch g/h has been deleted' ' + git branch g/h && + git ${pack_refs} --all --prune && + git branch -d g/h && + git branch g && + git ${pack_refs} --all && + git branch -d g +' + +test_expect_success 'git branch i/j/k should barf if branch i exists' ' + git branch i && + git ${pack_refs} --all --prune && + test_must_fail git branch i/j/k +' + +test_expect_success 'test git branch k after branch k/l/m and k/lm have been deleted' ' + git branch k/l && + git branch k/lm && + git branch -d k/l && + git branch k/l/m && + git branch -d k/l/m && + git branch -d k/lm && + git branch k +' + +test_expect_success 'test git branch n after some branch deletion and pruning' ' + git branch n/o && + git branch n/op && + git branch -d n/o && + git branch n/o/p && + git branch -d n/op && + git ${pack_refs} --all --prune && + git branch -d n/o/p && + git branch n +' + +test_expect_success 'test excluded refs are not packed' ' + git branch dont_pack1 && + git branch dont_pack2 && + git branch pack_this && + git ${pack_refs} --all --exclude "refs/heads/dont_pack*" && + test -f .git/refs/heads/dont_pack1 && + test -f .git/refs/heads/dont_pack2 && + ! test -f .git/refs/heads/pack_this' + +test_expect_success 'test --no-exclude refs clears excluded refs' ' + git branch dont_pack3 && + git branch dont_pack4 && + git ${pack_refs} --all --exclude "refs/heads/dont_pack*" --no-exclude && + ! test -f .git/refs/heads/dont_pack3 && + ! test -f .git/refs/heads/dont_pack4' + +test_expect_success 'test only included refs are packed' ' + git branch pack_this1 && + git branch pack_this2 && + git tag dont_pack5 && + git ${pack_refs} --include "refs/heads/pack_this*" && + test -f .git/refs/tags/dont_pack5 && + ! test -f .git/refs/heads/pack_this1 && + ! test -f .git/refs/heads/pack_this2' + +test_expect_success 'test --no-include refs clears included refs' ' + git branch pack1 && + git branch pack2 && + git ${pack_refs} --include "refs/heads/pack*" --no-include && + test -f .git/refs/heads/pack1 && + test -f .git/refs/heads/pack2' + +test_expect_success 'test --exclude takes precedence over --include' ' + git branch dont_pack5 && + git ${pack_refs} --include "refs/heads/pack*" --exclude "refs/heads/pack*" && + test -f .git/refs/heads/dont_pack5' + +test_expect_success 'see if up-to-date packed refs are preserved' ' + git branch q && + git ${pack_refs} --all --prune && + git update-ref refs/heads/q refs/heads/q && + ! test -f .git/refs/heads/q +' + +test_expect_success 'pack, prune and repack' ' + git tag foo && + git ${pack_refs} --all --prune && + git show-ref >all-of-them && + git ${pack_refs} && + git show-ref >again && + test_cmp all-of-them again +' + +test_expect_success 'explicit ${pack_refs} with dangling packed reference' ' + git commit --allow-empty -m "soon to be garbage-collected" && + git ${pack_refs} --all && + git reset --hard HEAD^ && + git reflog expire --expire=all --all && + git prune --expire=all && + git ${pack_refs} --all 2>result && + test_must_be_empty result +' + +test_expect_success 'delete ref with dangling packed version' ' + git checkout -b lamb && + git commit --allow-empty -m "future garbage" && + git ${pack_refs} --all && + git reset --hard HEAD^ && + git checkout main && + git reflog expire --expire=all --all && + git prune --expire=all && + git branch -d lamb 2>result && + test_must_be_empty result +' + +test_expect_success 'delete ref while another dangling packed ref' ' + git branch lamb && + git commit --allow-empty -m "future garbage" && + git ${pack_refs} --all && + git reset --hard HEAD^ && + git reflog expire --expire=all --all && + git prune --expire=all && + git branch -d lamb 2>result && + test_must_be_empty result +' + +test_expect_success 'pack ref directly below refs/' ' + git update-ref refs/top HEAD && + git ${pack_refs} --all --prune && + grep refs/top .git/packed-refs && + test_path_is_missing .git/refs/top +' + +test_expect_success 'do not pack ref in refs/bisect' ' + git update-ref refs/bisect/local HEAD && + git ${pack_refs} --all --prune && + ! grep refs/bisect/local .git/packed-refs >/dev/null && + test_path_is_file .git/refs/bisect/local +' + +test_expect_success 'disable reflogs' ' + git config core.logallrefupdates false && + rm -rf .git/logs +' + +test_expect_success 'create packed foo/bar/baz branch' ' + git branch foo/bar/baz && + git ${pack_refs} --all --prune && + test_path_is_missing .git/refs/heads/foo/bar/baz && + test_must_fail git reflog exists refs/heads/foo/bar/baz +' + +test_expect_success 'notice d/f conflict with existing directory' ' + test_must_fail git branch foo && + test_must_fail git branch foo/bar +' + +test_expect_success 'existing directory reports concrete ref' ' + test_must_fail git branch foo 2>stderr && + test_grep refs/heads/foo/bar/baz stderr +' + +test_expect_success 'notice d/f conflict with existing ref' ' + test_must_fail git branch foo/bar/baz/extra && + test_must_fail git branch foo/bar/baz/lots/of/extra/components +' + +test_expect_success 'reject packed-refs with unterminated line' ' + cp .git/packed-refs .git/packed-refs.bak && + test_when_finished "mv .git/packed-refs.bak .git/packed-refs" && + printf "%s" "$HEAD refs/zzzzz" >>.git/packed-refs && + echo "fatal: unterminated line in .git/packed-refs: $HEAD refs/zzzzz" >expected_err && + test_must_fail git for-each-ref >out 2>err && + test_cmp expected_err err +' + +test_expect_success 'reject packed-refs containing junk' ' + cp .git/packed-refs .git/packed-refs.bak && + test_when_finished "mv .git/packed-refs.bak .git/packed-refs" && + printf "%s\n" "bogus content" >>.git/packed-refs && + echo "fatal: unexpected line in .git/packed-refs: bogus content" >expected_err && + test_must_fail git for-each-ref >out 2>err && + test_cmp expected_err err +' + +test_expect_success 'reject packed-refs with a short SHA-1' ' + cp .git/packed-refs .git/packed-refs.bak && + test_when_finished "mv .git/packed-refs.bak .git/packed-refs" && + printf "%.7s %s\n" $HEAD refs/zzzzz >>.git/packed-refs && + printf "fatal: unexpected line in .git/packed-refs: %.7s %s\n" $HEAD refs/zzzzz >expected_err && + test_must_fail git for-each-ref >out 2>err && + test_cmp expected_err err +' + +test_expect_success 'timeout if packed-refs.lock exists' ' + LOCK=.git/packed-refs.lock && + >"$LOCK" && + test_when_finished "rm -f $LOCK" && + test_must_fail git ${pack_refs} --all --prune +' + +test_expect_success 'retry acquiring packed-refs.lock' ' + LOCK=.git/packed-refs.lock && + >"$LOCK" && + test_when_finished "wait && rm -f $LOCK" && + { + ( sleep 1 && rm -f $LOCK ) & + } && + git -c core.packedrefstimeout=3000 ${pack_refs} --all --prune +' + +test_expect_success SYMLINKS 'pack symlinked packed-refs' ' + # First make sure that symlinking works when reading: + git update-ref refs/heads/lossy refs/heads/main && + git for-each-ref >all-refs-before && + mv .git/packed-refs .git/my-deviant-packed-refs && + ln -s my-deviant-packed-refs .git/packed-refs && + git for-each-ref >all-refs-linked && + test_cmp all-refs-before all-refs-linked && + git ${pack_refs} --all --prune && + git for-each-ref >all-refs-packed && + test_cmp all-refs-before all-refs-packed && + test -h .git/packed-refs && + test "$(test_readlink .git/packed-refs)" = "my-deviant-packed-refs" +' + +# The 'packed-refs' file is stored directly in .git/. This means it is global +# to the repository, and can only contain refs that are shared across all +# worktrees. +test_expect_success 'refs/worktree must not be packed' ' + test_commit initial && + test_commit wt1 && + test_commit wt2 && + git worktree add wt1 wt1 && + git worktree add wt2 wt2 && + git checkout initial && + git update-ref refs/worktree/foo HEAD && + git -C wt1 update-ref refs/worktree/foo HEAD && + git -C wt2 update-ref refs/worktree/foo HEAD && + git ${pack_refs} --all && + test_path_is_missing .git/refs/tags/wt1 && + test_path_is_file .git/refs/worktree/foo && + test_path_is_file .git/worktrees/wt1/refs/worktree/foo && + test_path_is_file .git/worktrees/wt2/refs/worktree/foo +' + +# we do not want to count on running ${pack_refs} to +# actually pack it, as it is perfectly reasonable to +# skip processing a broken ref +test_expect_success 'create packed-refs file with broken ref' ' + test_tick && git commit --allow-empty -m one && + recoverable=$(git rev-parse HEAD) && + test_tick && git commit --allow-empty -m two && + missing=$(git rev-parse HEAD) && + rm -f .git/refs/heads/main && + cat >.git/packed-refs <<-EOF && + $missing refs/heads/main + $recoverable refs/heads/other + EOF + echo $missing >expect && + git rev-parse refs/heads/main >actual && + test_cmp expect actual +' + +test_expect_success '${pack_refs} does not silently delete broken packed ref' ' + git ${pack_refs} --all --prune && + git rev-parse refs/heads/main >actual && + test_cmp expect actual +' + +test_expect_success '${pack_refs} does not drop broken refs during deletion' ' + git update-ref -d refs/heads/other && + git rev-parse refs/heads/main >actual && + test_cmp expect actual +' + +for command in "git ${pack_refs} --all --auto" "git maintenance run --task=${pack_refs} --auto" +do + test_expect_success "$command does not repack below 16 refs without packed-refs" ' + test_when_finished "rm -rf repo" && + git init repo && + ( + cd repo && + git config set maintenance.auto false && + git commit --allow-empty --message "initial" && + + # Create 14 additional references, which brings us to + # 15 together with the default branch. + printf "create refs/heads/loose-%d HEAD\n" $(test_seq 14) >stdin && + git update-ref --stdin <stdin && + test_path_is_missing .git/packed-refs && + git ${pack_refs} --auto --all && + test_path_is_missing .git/packed-refs && + + # Create the 16th reference, which should cause us to repack. + git update-ref refs/heads/loose-15 HEAD && + git ${pack_refs} --auto --all && + test_path_is_file .git/packed-refs + ) + ' + + test_expect_success "$command does not repack below 16 refs with small packed-refs" ' + test_when_finished "rm -rf repo" && + git init repo && + ( + cd repo && + git config set maintenance.auto false && + git commit --allow-empty --message "initial" && + + git ${pack_refs} --all && + test_line_count = 2 .git/packed-refs && + + # Create 15 loose references. + printf "create refs/heads/loose-%d HEAD\n" $(test_seq 15) >stdin && + git update-ref --stdin <stdin && + git ${pack_refs} --auto --all && + test_line_count = 2 .git/packed-refs && + + # Create the 16th loose reference, which should cause us to repack. + git update-ref refs/heads/loose-17 HEAD && + git ${pack_refs} --auto --all && + test_line_count = 18 .git/packed-refs + ) + ' + + test_expect_success "$command scales with size of packed-refs" ' + test_when_finished "rm -rf repo" && + git init repo && + ( + cd repo && + git config set maintenance.auto false && + git commit --allow-empty --message "initial" && + + # Create 99 packed refs. This should cause the heuristic + # to require more than the minimum amount of loose refs. + test_seq 99 | + while read i + do + printf "create refs/heads/packed-%d HEAD\n" $i || return 1 + done >stdin && + git update-ref --stdin <stdin && + git ${pack_refs} --all && + test_line_count = 101 .git/packed-refs && + + # Create 24 loose refs, which should not yet cause us to repack. + printf "create refs/heads/loose-%d HEAD\n" $(test_seq 24) >stdin && + git update-ref --stdin <stdin && + git ${pack_refs} --auto --all && + test_line_count = 101 .git/packed-refs && + + # Create another handful of refs to cross the border. + # Note that we explicitly do not check for strict + # boundaries here, as this also depends on the size of + # the object hash. + printf "create refs/heads/addn-%d HEAD\n" $(test_seq 10) >stdin && + git update-ref --stdin <stdin && + git ${pack_refs} --auto --all && + test_line_count = 135 .git/packed-refs + ) + ' +done + +test_done
t/t0601-reffiles-pack-refs.sh +1 -429
433 lines not on this page · raw message
Signature · 1 line
2.34.1
PATCH v2 5/5 t: add test for git refs optimize subcommand
In reply to Meet SoniAdd a test script, t/t1463-refs-optimize.sh, for the new git refs optimize command.
This script acts as a simple driver, leveraging the shared test library created in the preceding commit. It works by overriding the $pack_refs variable to "refs optimize" and then sourcing the shared library (t/pack-refs-tests.sh).
This approach ensures that git refs optimize is tested against the entire comprehensive test suite of git pack-refs, verifying that it acts as a compatible drop-in replacement.
--- t/meson.build | 3 ++- t/t1463-refs-optimize.sh | 17 +++++++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) create mode 100755 t/t1463-refs-optimize.sh
t/meson.build +2 -1
@@ -210,6 +210,7 @@ integration_tests = [ 't1451-fsck-buffer.sh', 't1460-refs-migrate.sh', 't1461-refs-list.sh', + 't1463-refs-optimize.sh', 't1500-rev-parse.sh', 't1501-work-tree.sh', 't1502-rev-parse-parseopt.sh',
@@ -1216,4 +1217,4 @@ if perl.found() and time.found() timeout: 0, ) endforeach -endif +endif
t/t1463-refs-optimize.sh new file +17 -0
@@ -0,0 +1,17 @@ +#!/bin/sh + +test_description='git refs optimize should not change the branch semantic + +This test runs git refs optimize and git show-ref and checks that the branch +semantic is still the same. +' + +GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main +export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME +GIT_TEST_DEFAULT_REF_FORMAT=files +export GIT_TEST_DEFAULT_REF_FORMAT + +. ./test-lib.sh + +pack_refs='refs optimize' +. "$TEST_DIRECTORY"/pack-refs-tests.sh
Signature · 1 line
2.34.1
Quoted 3 lines · Meet Soni
This series introduces
git refs optimizeas a modern replacement forgit pack-refs, continuing the effort to consolidate commands under thegit refsnamespace.
Sorry, but I do not quite see the point of this change. Is it your goal to eventually remove "git pack-refs"?
I would very much understand if this were:
"git pack-refs" is a command that is very specific to the files backend to optimize the way refs are stored in that backend. It does not do anything to other backends.
Introduce "git refs optimize" as the end-user facing front-end so that later different backends, including reftable backend, can define their own way to optimize the way refs are stored in them. As the first step, switch on what backend is in use in the repository, and invoke "git pack-refs" if the repository uses the files backend.
And when framed this way, I am not sure it is a good direction forward to have pack-refs.[ch] top-level files. I have to wonder if the approach should be more along this line?
- Define the "optimize" action in the refs API. What it really means to "optimize" may differ from backend to backend. There may be refs_optimize(struct ref_store *refs) API entry point.
- Add the new action to the vtable for refs backends. There may be no action defined for reftable backend for now, or you may find there already are reftable specific optimizations you want to trigger from there.
- Figure out how this interacts with existing refs_pack_refs(); most likely it as the backend specific option, should go away, and its implementation would move to the "optimize" action driven from the vtable for files backend.
Once it is done, you do not necessarily need "git refs optimize", but the "git pack-refs" could be the front-end to trigger the more generic "optimize" action. In other words, in a repository whose refs are stored in reftable, "git pack-refs" would cease to be a no-op but can perform optimizations suitable in that repository.
That way, users do not need to learn a new command, which may be also an advantage over what is being proposed here.
Thanks.
Quoted 18 lines · Junio C Hamano
And when framed this way, I am not sure it is a good direction forward to have pack-refs.[ch] top-level files. I have to wonder if the approach should be more along this line?
- Define the "optimize" action in the refs API. What it really means to "optimize" may differ from backend to backend. There may be
refs_optimize(struct ref_store *refs)API entry point.- Add the new action to the vtable for refs backends. There may be no action defined for reftable backend for now, or you may find there already are reftable specific optimizations you want to trigger from there.
- Figure out how this interacts with existing
refs_pack_refs(); most likely it as the backend specific option, should go away, and its implementation would move to the "optimize" action driven from the vtable for files backend.
Quoted 8 lines
Once it is done, you do not necessarily need "git refs optimize", but the "git pack-refs" could be the front-end to trigger the more generic "optimize" action. In other words, in a repository whose refs are stored in reftable, "git pack-refs" would cease to be a no-op but can perform optimizations suitable in that repository.
That way, users do not need to learn a new command, which may be also an advantage over what is being proposed here.
I do not have strong opinions between keeping "git pack-refs" and adding "git refs optimize"; in other words, the UI exposed to end-user is secondary, so if people prefer the latter, that is fine by me.
I do care about how the code is structured, though.
Thanks.
v3 cover Add refs optimize subcommand
In reply to Meet SoniHi everyone,
This series introduces git refs optimize as a modern replacement for git pack-refs, continuing the effort to consolidate commands under the git refs namespace.
Changes in v3:
- Reworked the entire series from scratch to implement the architectural feedback provided by Junio in the v2 review.
- Instead of a simple UI alias, this version introduces a generic, backend-agnostic refs_optimize() API.
- Added implementations for both the 'files' backend (packing) and the 'reftable' backend (compaction).
Meet Soni (9):
refs: add a generic 'optimize' API
files-backend: implement 'optimize' action
reftable-backend: implement 'optimize' action builtin/pack-refs: convert to use the generic refs_optimize() API builtin/pack-refs: factor out core logic into a shared library
doc: pack-refs: factor out common options builtin/refs: add optimize subcommand
t0601: refactor tests to be shareable
t: add test for git refs optimize subcommand
Documentation/git-pack-refs.adoc | 53 +--- Documentation/git-refs.adoc | 10 + Documentation/pack-refs-options.adoc | 52 ++++ Makefile | 1 + builtin/pack-refs.c | 54 +--- builtin/refs.c | 17 ++ meson.build | 1 + pack-refs.c | 56 ++++ pack-refs.h | 23 ++ refs.c | 7 + refs.h | 6 + refs/files-backend.c | 10 + refs/refs-internal.h | 3 + refs/reftable-backend.c | 7 + t/meson.build | 3 +- t/pack-refs-tests.sh | 431 +++++++++++++++++++++++++++ t/t0601-reffiles-pack-refs.sh | 430 +------------------------- t/t1463-refs-optimize.sh | 17 ++ 18 files changed, 650 insertions(+), 531 deletions(-) create mode 100644 Documentation/pack-refs-options.adoc create mode 100644 pack-refs.c create mode 100644 pack-refs.h create mode 100644 t/pack-refs-tests.sh create mode 100755 t/t1463-refs-optimize.sh
Range-diff against v2:
-: ---------- > 1: a837ae6f5d refs: add a generic 'optimize' API
-: ---------- > 2: e0613b14b6 files-backend: implement 'optimize' action
-: ---------- > 3: ae8d2d29d9 reftable-backend: implement 'optimize' action
-: ---------- > 4: a8cba8a355 builtin/pack-refs: convert to use the generic refs_optimize() API
1: 67a9187b59 ! 5: ec1085ccd8 builtin/pack-refs: factor out core logic into a shared library
@@ builtin/pack-refs.c
- if (!pack_refs_opts.includes->nr)
- string_list_append(pack_refs_opts.includes, "refs/tags/*");
-
-- ret = refs_pack_refs(get_main_ref_store(repo), &pack_refs_opts);
--
+- ret = refs_optimize(get_main_ref_store(repo), &pack_refs_opts);
+
- clear_ref_exclusions(&excludes);
- string_list_clear(&included_refs, 0);
- string_list_clear(&option_excluded_refs, 0);
@@ pack-refs.c (new)
+ if (!pack_refs_opts.includes->nr)
+ string_list_append(pack_refs_opts.includes, "refs/tags/*");
+
-+ ret = refs_pack_refs(get_main_ref_store(repo), &pack_refs_opts);
++ ret = refs_optimize(get_main_ref_store(repo), &pack_refs_opts);
+
+ clear_ref_exclusions(&excludes);
+ string_list_clear(&included_refs, 0); 2: 422eaa0c8b ! 6: e1758816bf doc: factor out common option
@@ Metadata
Author: Meet Soni <[email protected]>
## Commit message ##
- doc: factor out common option
+ doc: pack-refs: factor out common options
In preparation for adding documentation for git refs optimize, factor out the common options from the git-pack-refs man page into a shareable file pack-refs-options.adoc and update git-pack-refs.adoc to use an include:: macro.
- This change is a pure refactoring and results in no change to the
- final rendered documentation for pack-refs.
+ This change is a pure refactoring and results in no change to the final
+ rendered documentation for pack-refs.
@@ Documentation/git-pack-refs.adoc: unpacked.
-+
-When used with --include, refs provided to --include, minus refs that are -provided to --exclude will be packed.
--
+include::pack-refs-options.adoc[]
+
BUGS
- ----
## Documentation/pack-refs-options.adoc (new) ##
@@
3: 067a2baa97 ! 7: e3a908fe72 builtin/refs: add optimize subcommand
@@ Commit message
As part of the ongoing effort to consolidate reference handling, introduce a new optimize subcommand. This command provides the same
- functionality and exit-code behavior as git pack-refs, serving
- as its modern replacement.
+ functionality and exit-code behavior as git pack-refs, serving as its
+ modern replacement.
Implement cmd_refs_optimize by having it call the pack_refs_core() helper function. This helper was factored out of the original
- cmd_pack_refs in a preceding commit, allowing both commands to
- share the same core logic as independent peers.
+ cmd_pack_refs in a preceding commit, allowing both commands to share
+ the same core logic as independent peers.
Add documentation for the new command. The man page leverages the shared options file, created in a previous commit, by using the AsciiDoc
@@ Documentation/git-refs.adoc
@@ Documentation/git-refs.adoc: git refs list [--count=<count>] [--shell|--perl|--python|--tcl]
[--contains[=<object>]] [--no-contains[=<object>]] [(--exclude=<pattern>)...] [--start-after=<marker>]
- [ --stdin | <pattern>... ]
+ [ --stdin | (<pattern>...)]
+git refs optimize [--all] [--no-prune] [--auto] [--include <pattern>] [--exclude <pattern>]
DESCRIPTION
@@ builtin/refs.c: static int cmd_refs_list(int argc, const char **argv, const char
+ REFS_OPTIMIZE_USAGE,
+ NULL
+ };
++
+ return pack_refs_core(argc, argv, prefix, repo, refs_optimize_usage);
+}
+
4: 640ebd9a4f ! 8: 4f63632ac2 t0601: refactor tests to be shareable
@@ Commit message
variable and then sourcing the test library.
This new structure follows the established pattern used for sharing
- tests between git-for-each-ref and git-refs list and prepares the test suite
- for the refs optimize tests to be added in a subsequent commit.
+ tests between git-for-each-ref and git-refs list and prepares the
+ test suite for the refs optimize tests to be added in a subsequent
+ commit.
5: 37ca586bb9 = 9: 39eed2831a t: add test for git refs optimize subcommand
base-commit: f814da676ae46aac5be0a98b99373a76dee6cedb
Signature · 1 line
2.34.1
PATCH v3 1/9 refs: add a generic 'optimize' API
Add a new generic refs_optimize() API function that dispatches to a backend-specific implementation via a new 'optimize' vtable method.
This lays the architectural groundwork for different reference backends (like 'files' and 'reftable') to provide their own storage optimization logic, which will be called from a single, generic entry point.
--- refs.c | 7 +++++++ refs.h | 6 ++++++ refs/refs-internal.h | 3 +++ 3 files changed, 16 insertions(+)
refs.c +7 -0
@@ -2282,6 +2282,13 @@ int refs_pack_refs(struct ref_store *refs, struct pack_refs_opts *opts)return refs->be->pack_refs (refs, opts); } +int refs_optimize (struct ref_store *refs,struct pack_refs_opts *opts) +{ +if (!refs->be->optimize) +return 0 ; +return refs->be->optimize (refs, opts); +} +int peel_iterated_oid (struct repository *r,const struct object_id *base,struct object_id *peeled) { if (current_ref_iter &&
refs.h +6 -0
@@ -480,6 +480,12 @@ struct pack_refs_opts {*/ int refs_pack_refs (struct ref_store *refs,struct pack_refs_opts *opts); +/* +* Optimize the ref store. The exact behavior is up to the backend. +* For the files backend, this is equivalent to packing refs. +*/ +int refs_optimize (struct ref_store *refs,struct pack_refs_opts *opts); +/* * Setup reflog before using. Fill in err and return -1 on failure. */
refs/refs-internal.h +3 -0
@@ -447,6 +447,8 @@ typedef int ref_transaction_commit_fn(struct ref_store *refs,typedef int pack_refs_fn (struct ref_store *ref_store,struct pack_refs_opts *opts); +typedef int optimize_fn (struct ref_store *ref_store, +struct pack_refs_opts *opts);typedef int rename_ref_fn (struct ref_store *ref_store,const char *oldref,const char *newref,const char *logmsg);
@@ -572,6 +574,7 @@ struct ref_storage_be {ref_transaction_abort_fn *transaction_abort;pack_refs_fn *pack_refs; +optimize_fn *optimize;rename_ref_fn *rename_ref;copy_ref_fn *copy_ref;
Signature · 1 line
2.34.1
Re: [GSoC][PATCH v3 1/9] refs: add a generic 'optimize' API
Quoted 3 lines · Meet Soni
Add a new generic
refs_optimize()API function that dispatches to a backend-specific implementation via a new 'optimize' vtable method.
Should "API" be enough instead of using "API function"? However, I think we need to give the motivation.
Quoted 18 lines
This lays the architectural groundwork for different reference backends (like 'files' and 'reftable') to provide their own storage optimization logic, which will be called from a single, generic entry point.
Mentored-by Patrick Steinhardt <[email protected]>Mentored-by shejialuo <[email protected]>Signed-off-by Meet Soni <[email protected]>--- refs.c | 7 +++++++ refs.h | 6 ++++++ refs/refs-internal.h | 3 +++ 3 files changed, 16 insertions(+)refs.c +0
-0
refs.c int refs_pack_refs(struct ref_store *refs, struct pack_refs_opts *opts) · line 2288
Quoted 7 lines
refs.c +4
-0@@ -2282,6 +2282,13 @@ int refs_pack_refs(struct ref_store *refs, struct pack_refs_opts *opts)return refs->be->pack_refs (refs, opts); } +int refs_optimize (struct ref_store *refs,struct pack_refs_opts *opts) +{ +if (!refs->be->optimize) +return 0 ;
I don't think we need to check refs->be->optimize. Even though for some backends, we won't do any optimization, we should register this callback instead of assigning NULL.
refs.c int refs_pack_refs(struct ref_store *refs, struct pack_refs_opts *opts) · line 2291
Quoted 3 lines
refs.c +3
-0+return refs->be->optimize (refs, opts); +} +
Thanks,
Jialuo
Re: [GSoC][PATCH v3 1/9] refs: add a generic 'optimize' API
Quoted 8 lines · shejialuo
Quoted 8 lines
refs.c +0
-0@@ -2282,6 +2282,13 @@ int refs_pack_refs(struct ref_store *refs, struct pack_refs_opts *opts)return refs->be->pack_refs (refs, opts); }
Quoted 8 lines
refs.c int refs_pack_refs(struct ref_store *refs, struct pack_refs_opts *opts) · line 2288
Quoted 4 lines
refs.c +4
-0+int refs_optimize (struct ref_store *refs,struct pack_refs_opts *opts) +{ +if (!refs->be->optimize) +return 0 ;I don't think we need to check
refs->be->optimize. Even though for some backends, we won't do any optimization, we should register this callback instead of assigningNULL.
Yeah, all the existing functions at the refs.c level simply assumes that refs->be->method always exists, and it would be sensible to be coherent with them.
Thanks.
PATCH v3 2/9 files-backend: implement 'optimize' action
In reply to Meet SoniWith the generic refs_optimize() API now in place, provide the first implementation for the 'files' reference backend. This makes the new API functional for existing repositories and serves as the foundation for migrating user-facing commands to the new architecture.
The implementation simply calls the existing files_pack_refs() function, as 'packing' is the method used to optimize the files-based reference store.
Wire up the new files_optimize() function to the optimize slot in the files backend's virtual table.
--- refs/files-backend.c | 10 ++++++++++ 1 file changed, 10 insertions(+)
refs/files-backend.c +10 -0
@@ -1473,6 +1473,15 @@ static int files_pack_refs(struct ref_store *ref_store,return 0 ; } +static int files_optimize (struct ref_store *ref_store,struct pack_refs_opts *opts) +{ +/* +* For the "files" backend, "optimizing" is the same as "packing". +* So, we just call the existing worker function for packing. +*/ +return files_pack_refs (ref_store, opts); +} +/* * People using contrib's git-new-workdir have .git/logs/refs -> * /some/other/path/.git/logs/refs, and that may live on another device.
@@ -3909,6 +3918,7 @@ struct ref_storage_be refs_be_files = { .transaction_abort = files_transaction_abort, .pack_refs = files_pack_refs, + .optimize = files_optimize, .rename_ref = files_rename_ref, .copy_ref = files_copy_ref,
Signature · 1 line
2.34.1
PATCH v3 3/9 reftable-backend: implement 'optimize' action
In reply to Meet SoniTo make the new generic optimize API fully functional, provide an implementation for the 'reftable' reference backend.
For the reftable backend, the 'optimize' action is to compact its tables. The existing reftable_be_pack_refs() function already provides this logic, so the new reftable_be_optimize() function simply calls it.
Wire up the new function to the optimize slot in the reftable backend's virtual table.
--- refs/reftable-backend.c | 7 +++++++ 1 file changed, 7 insertions(+)
refs/reftable-backend.c +7 -0
@@ -1721,6 +1721,12 @@ static int reftable_be_pack_refs(struct ref_store *ref_store,return ret; } +static int reftable_be_optimize (struct ref_store *ref_store, +struct pack_refs_opts *opts) +{ +return reftable_be_pack_refs (ref_store, opts); +} +struct write_create_symref_arg {struct reftable_ref_store *refs;struct reftable_stack *stack;
@@ -2702,6 +2708,7 @@ struct ref_storage_be refs_be_reftable = { .transaction_abort = reftable_be_transaction_abort, .pack_refs = reftable_be_pack_refs, + .optimize = reftable_be_optimize, .rename_ref = reftable_be_rename_ref, .copy_ref = reftable_be_copy_ref,
Signature · 1 line
2.34.1
PATCH v3 4/9 builtin/pack-refs: convert to use the generic refs_optimize() API
In reply to Meet SoniThe git pack-refs command is tied to the 'files' reference backend. In a repository that uses a different backend (like 'reftable'), the command is a no-op.
To make git pack-refs a truly generic frontend for reference optimization, refactor it to use the new generic refs_optimize() API. This will allow the command to automatically work with any backend that implements the optimize action in the future.
The command continues to handle parsing its own command-line options, but now calls the generic API to perform the action instead of a backend-specific function.
--- builtin/pack-refs.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
builtin/pack-refs.c +1 -1
@@ -51,7 +51,7 @@ int cmd_pack_refs(int argc,if (!pack_refs_opts.includes->nr)string_list_append (pack_refs_opts.includes,"refs/tags/*" ); - ret =refs_pack_refs (get_main_ref_store (repo), &pack_refs_opts); + ret =refs_optimize (get_main_ref_store (repo), &pack_refs_opts);clear_ref_exclusions (&excludes);string_list_clear (&included_refs,0 );
Signature · 1 line
2.34.1
Re: [GSoC][PATCH v3 4/9] builtin/pack-refs: convert to use the generic refs_optimize() API
Quoted 4 lines · Meet Soni
The
git pack-refscommand is tied to the 'files' reference backend. In a repository that uses a different backend (like 'reftable'), the command is a no-op.
I think git pack-refs would work for reftable backend. It would eventually call reftable_be_pack_refs. And from my intuitive understanding, it would compact the reftable to reduce the size of the table.
Quoted 5 lines
To make
git pack-refsa truly generic frontend for reference optimization, refactor it to use the new genericrefs_optimize()API. This will allow the command to automatically work with any backend that implements theoptimizeaction in the future.
If my above understanding is correct, "git pack-refs" has already worked with all the backends. But for reftable backend, the semantic of "pack-refs" is not clear, optimize is a much better semantic word.
Thanks,
Jialuo
PATCH v3 5/9 builtin/pack-refs: factor out core logic into a shared library
In reply to Meet SoniThe implementation of git pack-refs is monolithic within cmd_pack_refs(), making it impossible to share its logic with other commands. To enable code reuse for the upcoming git refs optimize subcommand, refactor the core logic into a shared helper function.
Split the original builtin/pack-refs.c file into two parts:
- A new shared library file, pack-refs.c, which contains the core option parsing and packing logic in a new pack_refs_core() helper function.
- The original builtin/pack-refs.c, which is now a thin wrapper responsible only for defining the git pack-refs command and calling the shared helper.
A new pack-refs.h header is also introduced to define the public interface for this shared logic.
--- Makefile | 1 + builtin/pack-refs.c | 54 ++++--------------------------------------- meson.build | 1 + pack-refs.c | 56 +++++++++++++++++++++++++++++++++++++++++++++ pack-refs.h | 23 +++++++++++++++++++ 5 files changed, 86 insertions(+), 49 deletions(-) create mode 100644 pack-refs.c create mode 100644 pack-refs.h
Makefile +1 -0
@@ -1094,6 +1094,7 @@ LIB_OBJS += pack-bitmap.o LIB_OBJS += pack-check.o LIB_OBJS += pack-mtimes.o LIB_OBJS += pack-objects.o +LIB_OBJS += pack-refs.o LIB_OBJS += pack-revindex.o LIB_OBJS += pack-write.o LIB_OBJS += packfile.o
builtin/pack-refs.c +5 -49
65 lines not on this page · raw message
meson.build +1 -0
@@ -407,6 +407,7 @@ libgit_sources = [ 'pack-check.c', 'pack-mtimes.c', 'pack-objects.c', + 'pack-refs.c', 'pack-revindex.c', 'pack-write.c', 'packfile.c',
pack-refs.c new file +56 -0
56 lines not on this page · raw message
pack-refs.h new file +23 -0
@@ -0,0 +1,23 @@ +#ifndef PACK_REFS_H +#define PACK_REFS_H + +struct repository ; + +/* +* Shared usage string for options common to git-pack-refs(1) +* and git-refs-optimize(1). The command-specific part (e.g., "git refs optimize ") +* must be prepended by the caller. +*/ +#define PACK_REFS_OPTS \ + "[--all] [--no-prune] [--auto] [--include <pattern>] [--exclude <pattern>]" + +/* +* The core logic for pack-refs and its clones. +*/ +int pack_refs_core (int argc, +const char **argv, +const char *prefix, +struct repository *repo, +const char *const *usage_opts); + +#endif /* PACK_REFS_H */
Signature · 1 line
2.34.1
PATCH v3 6/9 doc: pack-refs: factor out common options
In reply to Meet SoniIn preparation for adding documentation for git refs optimize, factor out the common options from the git-pack-refs man page into a shareable file pack-refs-options.adoc and update git-pack-refs.adoc to use an include:: macro.
This change is a pure refactoring and results in no change to the final rendered documentation for pack-refs.
--- Documentation/git-pack-refs.adoc | 53 +--------------------------- Documentation/pack-refs-options.adoc | 52 +++++++++++++++++++++++++++ 2 files changed, 53 insertions(+), 52 deletions(-) create mode 100644 Documentation/pack-refs-options.adoc
Documentation/git-pack-refs.adoc +1 -52
@@ -45,58 +45,7 @@ unpacked. OPTIONS ------- ---all:: - -The command by default packs all tags and refs that are already -packed, and leaves other refs -alone. This is because branches are expected to be actively -developed and packing their tips does not help performance. -This option causes all refs to be packed as well, with the exception -of hidden refs, broken refs, and symbolic refs. Useful for a repository -with many branches of historical interests. - ---no-prune:: - -The command usually removes loose refs under `$GIT_DIR/refs` -hierarchy after packing them. This option tells it not to. - ---auto:: - -Pack refs as needed depending on the current state of the ref database. The -behavior depends on the ref format used by the repository and may change in the -future. -+ - - "files": Loose references are packed into the `packed-refs` file - based on the ratio of loose references to the size of the - `packed-refs` file. The bigger the `packed-refs` file, the more loose - references need to exist before we repack. -+ - - "reftable": Tables are compacted such that they form a geometric - sequence. For two tables N and N+1, where N+1 is newer, this - maintains the property that N is at least twice as big as N+1. Only - tables that violate this property are compacted. - ---include <pattern>:: - -Pack refs based on a `glob(7)` pattern. Repetitions of this option -accumulate inclusion patterns. If a ref is both included in `--include` and -`--exclude`, `--exclude` takes precedence. Using `--include` will preclude all -tags from being included by default. Symbolic refs and broken refs will never -be packed. When used with `--all`, it will be a noop. Use `--no-include` to clear -and reset the list of patterns. - ---exclude <pattern>:: - -Do not pack refs matching the given `glob(7)` pattern. Repetitions of this option -accumulate exclusion patterns. Use `--no-exclude` to clear and reset the list of -patterns. If a ref is already packed, including it with `--exclude` will not -unpack it. -+ -When used with `--all`, pack only loose refs which do not match any of -the provided `--exclude` patterns. -+ -When used with `--include`, refs provided to `--include`, minus refs that are -provided to `--exclude` will be packed. +include::pack-refs-options.adoc[] BUGS
Documentation/pack-refs-options.adoc new file +52 -0
52 lines not on this page · raw message
Signature · 1 line
2.34.1
PATCH v3 7/9 builtin/refs: add optimize subcommand
In reply to Meet SoniAs part of the ongoing effort to consolidate reference handling, introduce a new optimize subcommand. This command provides the same functionality and exit-code behavior as git pack-refs, serving as its modern replacement.
Implement cmd_refs_optimize by having it call the pack_refs_core() helper function. This helper was factored out of the original cmd_pack_refs in a preceding commit, allowing both commands to share the same core logic as independent peers.
Add documentation for the new command. The man page leverages the shared options file, created in a previous commit, by using the AsciiDoc include:: macro to ensure consistency with git-pack-refs(1).
--- Documentation/git-refs.adoc | 10 ++++++++++ builtin/refs.c | 17 +++++++++++++++++ 2 files changed, 27 insertions(+)
Documentation/git-refs.adoc +10 -0
@@ -18,6 +18,7 @@ git refs list [--count=<count>] [--shell|--perl|--python|--tcl] [--contains[=<object>]] [--no-contains[=<object>]] [(--exclude=<pattern>)...] [--start-after=<marker>] [ --stdin | (<pattern>...)] +git refs optimize [--all] [--no-prune] [--auto] [--include <pattern>] [--exclude <pattern>] DESCRIPTION -----------
@@ -38,6 +39,11 @@ list:: formatting, and sorting. This subcommand is an alias for linkgit:git-for-each-ref[1] and offers identical functionality. +optimize:: + Optimizes references to improve repository performance and reduce disk + usage. This subcommand is an alias for linkgit:git-pack-refs[1] and + offers identical functionality. + OPTIONS -------
@@ -73,6 +79,10 @@ The following options are specific to 'git refs list': include::for-each-ref-options.adoc[] +The following options are specific to 'git refs optimize': + +include::pack-refs-options.adoc[] + KNOWN LIMITATIONS -----------------
builtin/refs.c +17 -0
@@ -2,6 +2,7 @@#include "builtin.h" #include "config.h" #include "fsck.h" +#include "pack-refs.h" #include "parse-options.h" #include "refs.h" #include "strbuf.h"
@@ -14,6 +15,9 @@#define REFS_VERIFY_USAGE \ N_("git refs verify [--strict] [--verbose]") +#define REFS_OPTIMIZE_USAGE \ + N_("git refs optimize " PACK_REFS_OPTS) +static int cmd_refs_migrate (int argc,const char **argv,const char *prefix, struct repository *repo UNUSED) {
@@ -113,6 +117,17 @@ static int cmd_refs_list(int argc, const char **argv, const char *prefix,return for_each_ref_core (argc, argv, prefix, repo, refs_list_usage); } +static int cmd_refs_optimize (int argc,const char **argv,const char *prefix, +struct repository *repo) +{ +static char const *const refs_optimize_usage[] = { +REFS_OPTIMIZE_USAGE , +NULL + }; + +return pack_refs_core (argc, argv, prefix, repo, refs_optimize_usage); +} + int cmd_refs(int argc, const char **argv, const char *prefix,
@@ -122,6 +137,7 @@ int cmd_refs(int argc,REFS_MIGRATE_USAGE ,REFS_VERIFY_USAGE ,"git refs list " COMMON_USAGE_FOR_EACH_REF , +REFS_OPTIMIZE_USAGE ,NULL , }; parse_opt_subcommand_fn *fn =NULL ;
@@ -129,6 +145,7 @@ int cmd_refs(int argc,OPT_SUBCOMMAND ("migrate" , &fn, cmd_refs_migrate),OPT_SUBCOMMAND ("verify" , &fn, cmd_refs_verify),OPT_SUBCOMMAND ("list" , &fn, cmd_refs_list), +OPT_SUBCOMMAND ("optimize" , &fn, cmd_refs_optimize), OPT_END(), };
Signature · 1 line
2.34.1
Re: [GSoC][PATCH v3 7/9] builtin/refs: add optimize subcommand
Quoted 12 lines · Meet Soni
builtin/refs.c +3
-0+#define REFS_OPTIMIZE_USAGE \ + N_("git refs optimize " PACK_REFS_OPTS) +static int cmd_refs_migrate (int argc,const char **argv,const char *prefix, struct repository *repo UNUSED) {
builtin/refs.c static int cmd_refs_list(int argc, const char **argv, const char *prefix, · line 121
Quoted 5 lines
builtin/refs.c +2
-0@@ -113,6 +117,17 @@ static int cmd_refs_list(int argc, const char **argv, const char *prefix,return for_each_ref_core (argc, argv, prefix, repo, refs_list_usage); } +static int cmd_refs_optimize (int argc,const char **argv,const char *prefix, +struct repository *repo)
This line does not align; will locally fix (no need to resend only to fix this).
Quoted 13 lines
builtin/refs.c +9
-0+{ +static char const *const refs_optimize_usage[] = { +REFS_OPTIMIZE_USAGE , +NULL + }; + +return pack_refs_core (argc, argv, prefix, repo, refs_optimize_usage); +} + int cmd_refs(int argc, const char **argv, const char *prefix,
builtin/refs.c int cmd_refs(int argc, · line 142
Quoted 6 lines
builtin/refs.c +1
-0@@ -122,6 +137,7 @@ int cmd_refs(int argc,REFS_MIGRATE_USAGE ,REFS_VERIFY_USAGE ,"git refs list " COMMON_USAGE_FOR_EACH_REF , +REFS_OPTIMIZE_USAGE , NULL, };
We may want to clean this up outside this topic, perhaps a clean-up patch after all the dust settles. Lack of REFS_LIST_USAGE stands out like a sore thumb.
PATCH v3 8/9 t0601: refactor tests to be shareable
In reply to Meet SoniIn preparation for adding tests for the new git refs optimize command, refactor the existing t0601 test suite to make its logic shareable.
Move the core test logic from t0601-reffiles-pack-refs.sh into a new pack-refs-tests.sh file. Inside this new script, replace hardcoded calls to "pack-refs" with the $pack_refs variable.
The original t0601-reffiles-pack-refs.sh script now becomes a simple "driver". It is responsible for setting the default value of the variable and then sourcing the test library.
This new structure follows the established pattern used for sharing tests between git-for-each-ref and git-refs list and prepares the test suite for the refs optimize tests to be added in a subsequent commit.
--- t/pack-refs-tests.sh | 431 ++++++++++++++++++++++++++++++++++ t/t0601-reffiles-pack-refs.sh | 430 +-------------------------------- 2 files changed, 432 insertions(+), 429 deletions(-) create mode 100644 t/pack-refs-tests.sh
t/pack-refs-tests.sh new file +431 -0
@@ -0,0 +1,431 @@ +pack_refs=${pack_refs:-pack-refs} + +test_expect_success 'enable reflogs' ' + git config core.logallrefupdates true +' + +test_expect_success 'prepare a trivial repository' ' + echo Hello > A && + git update-index --add A && + git commit -m "Initial commit." && + HEAD=$(git rev-parse --verify HEAD) +' + +test_expect_success '${pack_refs} --prune --all' ' + test_path_is_missing .git/packed-refs && + git ${pack_refs} --no-prune --all && + test_path_is_file .git/packed-refs && + N=$(find .git/refs -type f | wc -l) && + test "$N" != 0 && + + git ${pack_refs} --prune --all && + test_path_is_file .git/packed-refs && + N=$(find .git/refs -type f) && + test -z "$N" +' + +SHA1= + +test_expect_success 'see if git show-ref works as expected' ' + git branch a && + SHA1=$(cat .git/refs/heads/a) && + echo "$SHA1 refs/heads/a" >expect && + git show-ref a >result && + test_cmp expect result +' + +test_expect_success 'see if a branch still exists when packed' ' + git branch b && + git ${pack_refs} --all && + rm -f .git/refs/heads/b && + echo "$SHA1 refs/heads/b" >expect && + git show-ref b >result && + test_cmp expect result +' + +test_expect_success 'git branch c/d should barf if branch c exists' ' + git branch c && + git ${pack_refs} --all && + rm -f .git/refs/heads/c && + test_must_fail git branch c/d +' + +test_expect_success 'see if a branch still exists after git ${pack_refs} --prune' ' + git branch e && + git ${pack_refs} --all --prune && + echo "$SHA1 refs/heads/e" >expect && + git show-ref e >result && + test_cmp expect result +' + +test_expect_success 'see if git ${pack_refs} --prune remove ref files' ' + git branch f && + git ${pack_refs} --all --prune && + ! test -f .git/refs/heads/f +' + +test_expect_success 'see if git ${pack_refs} --prune removes empty dirs' ' + git branch r/s/t && + git ${pack_refs} --all --prune && + ! test -e .git/refs/heads/r +' + +test_expect_success 'git branch g should work when git branch g/h has been deleted' ' + git branch g/h && + git ${pack_refs} --all --prune && + git branch -d g/h && + git branch g && + git ${pack_refs} --all && + git branch -d g +' + +test_expect_success 'git branch i/j/k should barf if branch i exists' ' + git branch i && + git ${pack_refs} --all --prune && + test_must_fail git branch i/j/k +' + +test_expect_success 'test git branch k after branch k/l/m and k/lm have been deleted' ' + git branch k/l && + git branch k/lm && + git branch -d k/l && + git branch k/l/m && + git branch -d k/l/m && + git branch -d k/lm && + git branch k +' + +test_expect_success 'test git branch n after some branch deletion and pruning' ' + git branch n/o && + git branch n/op && + git branch -d n/o && + git branch n/o/p && + git branch -d n/op && + git ${pack_refs} --all --prune && + git branch -d n/o/p && + git branch n +' + +test_expect_success 'test excluded refs are not packed' ' + git branch dont_pack1 && + git branch dont_pack2 && + git branch pack_this && + git ${pack_refs} --all --exclude "refs/heads/dont_pack*" && + test -f .git/refs/heads/dont_pack1 && + test -f .git/refs/heads/dont_pack2 && + ! test -f .git/refs/heads/pack_this' + +test_expect_success 'test --no-exclude refs clears excluded refs' ' + git branch dont_pack3 && + git branch dont_pack4 && + git ${pack_refs} --all --exclude "refs/heads/dont_pack*" --no-exclude && + ! test -f .git/refs/heads/dont_pack3 && + ! test -f .git/refs/heads/dont_pack4' + +test_expect_success 'test only included refs are packed' ' + git branch pack_this1 && + git branch pack_this2 && + git tag dont_pack5 && + git ${pack_refs} --include "refs/heads/pack_this*" && + test -f .git/refs/tags/dont_pack5 && + ! test -f .git/refs/heads/pack_this1 && + ! test -f .git/refs/heads/pack_this2' + +test_expect_success 'test --no-include refs clears included refs' ' + git branch pack1 && + git branch pack2 && + git ${pack_refs} --include "refs/heads/pack*" --no-include && + test -f .git/refs/heads/pack1 && + test -f .git/refs/heads/pack2' + +test_expect_success 'test --exclude takes precedence over --include' ' + git branch dont_pack5 && + git ${pack_refs} --include "refs/heads/pack*" --exclude "refs/heads/pack*" && + test -f .git/refs/heads/dont_pack5' + +test_expect_success 'see if up-to-date packed refs are preserved' ' + git branch q && + git ${pack_refs} --all --prune && + git update-ref refs/heads/q refs/heads/q && + ! test -f .git/refs/heads/q +' + +test_expect_success 'pack, prune and repack' ' + git tag foo && + git ${pack_refs} --all --prune && + git show-ref >all-of-them && + git ${pack_refs} && + git show-ref >again && + test_cmp all-of-them again +' + +test_expect_success 'explicit ${pack_refs} with dangling packed reference' ' + git commit --allow-empty -m "soon to be garbage-collected" && + git ${pack_refs} --all && + git reset --hard HEAD^ && + git reflog expire --expire=all --all && + git prune --expire=all && + git ${pack_refs} --all 2>result && + test_must_be_empty result +' + +test_expect_success 'delete ref with dangling packed version' ' + git checkout -b lamb && + git commit --allow-empty -m "future garbage" && + git ${pack_refs} --all && + git reset --hard HEAD^ && + git checkout main && + git reflog expire --expire=all --all && + git prune --expire=all && + git branch -d lamb 2>result && + test_must_be_empty result +' + +test_expect_success 'delete ref while another dangling packed ref' ' + git branch lamb && + git commit --allow-empty -m "future garbage" && + git ${pack_refs} --all && + git reset --hard HEAD^ && + git reflog expire --expire=all --all && + git prune --expire=all && + git branch -d lamb 2>result && + test_must_be_empty result +' + +test_expect_success 'pack ref directly below refs/' ' + git update-ref refs/top HEAD && + git ${pack_refs} --all --prune && + grep refs/top .git/packed-refs && + test_path_is_missing .git/refs/top +' + +test_expect_success 'do not pack ref in refs/bisect' ' + git update-ref refs/bisect/local HEAD && + git ${pack_refs} --all --prune && + ! grep refs/bisect/local .git/packed-refs >/dev/null && + test_path_is_file .git/refs/bisect/local +' + +test_expect_success 'disable reflogs' ' + git config core.logallrefupdates false && + rm -rf .git/logs +' + +test_expect_success 'create packed foo/bar/baz branch' ' + git branch foo/bar/baz && + git ${pack_refs} --all --prune && + test_path_is_missing .git/refs/heads/foo/bar/baz && + test_must_fail git reflog exists refs/heads/foo/bar/baz +' + +test_expect_success 'notice d/f conflict with existing directory' ' + test_must_fail git branch foo && + test_must_fail git branch foo/bar +' + +test_expect_success 'existing directory reports concrete ref' ' + test_must_fail git branch foo 2>stderr && + test_grep refs/heads/foo/bar/baz stderr +' + +test_expect_success 'notice d/f conflict with existing ref' ' + test_must_fail git branch foo/bar/baz/extra && + test_must_fail git branch foo/bar/baz/lots/of/extra/components +' + +test_expect_success 'reject packed-refs with unterminated line' ' + cp .git/packed-refs .git/packed-refs.bak && + test_when_finished "mv .git/packed-refs.bak .git/packed-refs" && + printf "%s" "$HEAD refs/zzzzz" >>.git/packed-refs && + echo "fatal: unterminated line in .git/packed-refs: $HEAD refs/zzzzz" >expected_err && + test_must_fail git for-each-ref >out 2>err && + test_cmp expected_err err +' + +test_expect_success 'reject packed-refs containing junk' ' + cp .git/packed-refs .git/packed-refs.bak && + test_when_finished "mv .git/packed-refs.bak .git/packed-refs" && + printf "%s\n" "bogus content" >>.git/packed-refs && + echo "fatal: unexpected line in .git/packed-refs: bogus content" >expected_err && + test_must_fail git for-each-ref >out 2>err && + test_cmp expected_err err +' + +test_expect_success 'reject packed-refs with a short SHA-1' ' + cp .git/packed-refs .git/packed-refs.bak && + test_when_finished "mv .git/packed-refs.bak .git/packed-refs" && + printf "%.7s %s\n" $HEAD refs/zzzzz >>.git/packed-refs && + printf "fatal: unexpected line in .git/packed-refs: %.7s %s\n" $HEAD refs/zzzzz >expected_err && + test_must_fail git for-each-ref >out 2>err && + test_cmp expected_err err +' + +test_expect_success 'timeout if packed-refs.lock exists' ' + LOCK=.git/packed-refs.lock && + >"$LOCK" && + test_when_finished "rm -f $LOCK" && + test_must_fail git ${pack_refs} --all --prune +' + +test_expect_success 'retry acquiring packed-refs.lock' ' + LOCK=.git/packed-refs.lock && + >"$LOCK" && + test_when_finished "wait && rm -f $LOCK" && + { + ( sleep 1 && rm -f $LOCK ) & + } && + git -c core.packedrefstimeout=3000 ${pack_refs} --all --prune +' + +test_expect_success SYMLINKS 'pack symlinked packed-refs' ' + # First make sure that symlinking works when reading: + git update-ref refs/heads/lossy refs/heads/main && + git for-each-ref >all-refs-before && + mv .git/packed-refs .git/my-deviant-packed-refs && + ln -s my-deviant-packed-refs .git/packed-refs && + git for-each-ref >all-refs-linked && + test_cmp all-refs-before all-refs-linked && + git ${pack_refs} --all --prune && + git for-each-ref >all-refs-packed && + test_cmp all-refs-before all-refs-packed && + test -h .git/packed-refs && + test "$(test_readlink .git/packed-refs)" = "my-deviant-packed-refs" +' + +# The 'packed-refs' file is stored directly in .git/. This means it is global +# to the repository, and can only contain refs that are shared across all +# worktrees. +test_expect_success 'refs/worktree must not be packed' ' + test_commit initial && + test_commit wt1 && + test_commit wt2 && + git worktree add wt1 wt1 && + git worktree add wt2 wt2 && + git checkout initial && + git update-ref refs/worktree/foo HEAD && + git -C wt1 update-ref refs/worktree/foo HEAD && + git -C wt2 update-ref refs/worktree/foo HEAD && + git ${pack_refs} --all && + test_path_is_missing .git/refs/tags/wt1 && + test_path_is_file .git/refs/worktree/foo && + test_path_is_file .git/worktrees/wt1/refs/worktree/foo && + test_path_is_file .git/worktrees/wt2/refs/worktree/foo +' + +# we do not want to count on running ${pack_refs} to +# actually pack it, as it is perfectly reasonable to +# skip processing a broken ref +test_expect_success 'create packed-refs file with broken ref' ' + test_tick && git commit --allow-empty -m one && + recoverable=$(git rev-parse HEAD) && + test_tick && git commit --allow-empty -m two && + missing=$(git rev-parse HEAD) && + rm -f .git/refs/heads/main && + cat >.git/packed-refs <<-EOF && + $missing refs/heads/main + $recoverable refs/heads/other + EOF + echo $missing >expect && + git rev-parse refs/heads/main >actual && + test_cmp expect actual +' + +test_expect_success '${pack_refs} does not silently delete broken packed ref' ' + git ${pack_refs} --all --prune && + git rev-parse refs/heads/main >actual && + test_cmp expect actual +' + +test_expect_success '${pack_refs} does not drop broken refs during deletion' ' + git update-ref -d refs/heads/other && + git rev-parse refs/heads/main >actual && + test_cmp expect actual +' + +for command in "git ${pack_refs} --all --auto" "git maintenance run --task=${pack_refs} --auto" +do + test_expect_success "$command does not repack below 16 refs without packed-refs" ' + test_when_finished "rm -rf repo" && + git init repo && + ( + cd repo && + git config set maintenance.auto false && + git commit --allow-empty --message "initial" && + + # Create 14 additional references, which brings us to + # 15 together with the default branch. + printf "create refs/heads/loose-%d HEAD\n" $(test_seq 14) >stdin && + git update-ref --stdin <stdin && + test_path_is_missing .git/packed-refs && + git ${pack_refs} --auto --all && + test_path_is_missing .git/packed-refs && + + # Create the 16th reference, which should cause us to repack. + git update-ref refs/heads/loose-15 HEAD && + git ${pack_refs} --auto --all && + test_path_is_file .git/packed-refs + ) + ' + + test_expect_success "$command does not repack below 16 refs with small packed-refs" ' + test_when_finished "rm -rf repo" && + git init repo && + ( + cd repo && + git config set maintenance.auto false && + git commit --allow-empty --message "initial" && + + git ${pack_refs} --all && + test_line_count = 2 .git/packed-refs && + + # Create 15 loose references. + printf "create refs/heads/loose-%d HEAD\n" $(test_seq 15) >stdin && + git update-ref --stdin <stdin && + git ${pack_refs} --auto --all && + test_line_count = 2 .git/packed-refs && + + # Create the 16th loose reference, which should cause us to repack. + git update-ref refs/heads/loose-17 HEAD && + git ${pack_refs} --auto --all && + test_line_count = 18 .git/packed-refs + ) + ' + + test_expect_success "$command scales with size of packed-refs" ' + test_when_finished "rm -rf repo" && + git init repo && + ( + cd repo && + git config set maintenance.auto false && + git commit --allow-empty --message "initial" && + + # Create 99 packed refs. This should cause the heuristic + # to require more than the minimum amount of loose refs. + test_seq 99 | + while read i + do + printf "create refs/heads/packed-%d HEAD\n" $i || return 1 + done >stdin && + git update-ref --stdin <stdin && + git ${pack_refs} --all && + test_line_count = 101 .git/packed-refs && + + # Create 24 loose refs, which should not yet cause us to repack. + printf "create refs/heads/loose-%d HEAD\n" $(test_seq 24) >stdin && + git update-ref --stdin <stdin && + git ${pack_refs} --auto --all && + test_line_count = 101 .git/packed-refs && + + # Create another handful of refs to cross the border. + # Note that we explicitly do not check for strict + # boundaries here, as this also depends on the size of + # the object hash. + printf "create refs/heads/addn-%d HEAD\n" $(test_seq 10) >stdin && + git update-ref --stdin <stdin && + git ${pack_refs} --auto --all && + test_line_count = 135 .git/packed-refs + ) + ' +done + +test_done
t/t0601-reffiles-pack-refs.sh +1 -429
433 lines not on this page · raw message
Signature · 1 line
2.34.1
PATCH v3 9/9 t: add test for git refs optimize subcommand
In reply to Meet SoniAdd a test script, t/t1463-refs-optimize.sh, for the new git refs optimize command.
This script acts as a simple driver, leveraging the shared test library created in the preceding commit. It works by overriding the $pack_refs variable to "refs optimize" and then sourcing the shared library (t/pack-refs-tests.sh).
This approach ensures that git refs optimize is tested against the entire comprehensive test suite of git pack-refs, verifying that it acts as a compatible drop-in replacement.
--- t/meson.build | 3 ++- t/t1463-refs-optimize.sh | 17 +++++++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) create mode 100755 t/t1463-refs-optimize.sh
t/meson.build +2 -1
@@ -211,6 +211,7 @@ integration_tests = [ 't1451-fsck-buffer.sh', 't1460-refs-migrate.sh', 't1461-refs-list.sh', + 't1463-refs-optimize.sh', 't1500-rev-parse.sh', 't1501-work-tree.sh', 't1502-rev-parse-parseopt.sh',
@@ -1219,4 +1220,4 @@ if perl.found() and time.found() timeout: 0, ) endforeach -endif +endif
t/t1463-refs-optimize.sh new file +17 -0
@@ -0,0 +1,17 @@ +#!/bin/sh + +test_description='git refs optimize should not change the branch semantic + +This test runs git refs optimize and git show-ref and checks that the branch +semantic is still the same. +' + +GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main +export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME +GIT_TEST_DEFAULT_REF_FORMAT=files +export GIT_TEST_DEFAULT_REF_FORMAT + +. ./test-lib.sh + +pack_refs='refs optimize' +. "$TEST_DIRECTORY"/pack-refs-tests.sh
Signature · 1 line
2.34.1
v4 cover Add refs optimize subcommand
In reply to Meet SoniHi everyone,
This series introduces git refs optimize as a modern replacement for git pack-refs, continuing the effort to consolidate commands under the git refs namespace.
Changes in v4:
- Improved commit messages and formatting.
- Removed the NULL check from the refs_optimize() dispatcher in refs.c to align with the project's conventions for API functions
Meet Soni (9):
refs: add a generic 'optimize' API
files-backend: implement 'optimize' action
reftable-backend: implement 'optimize' action builtin/pack-refs: convert to use the generic refs_optimize() API builtin/pack-refs: factor out core logic into a shared library
doc: pack-refs: factor out common options builtin/refs: add optimize subcommand
t0601: refactor tests to be shareable
t: add test for git refs optimize subcommand
Documentation/git-pack-refs.adoc | 53 +--- Documentation/git-refs.adoc | 10 + Documentation/pack-refs-options.adoc | 52 ++++ Makefile | 1 + builtin/pack-refs.c | 54 +--- builtin/refs.c | 17 ++ meson.build | 1 + pack-refs.c | 56 ++++ pack-refs.h | 23 ++ refs.c | 5 + refs.h | 6 + refs/files-backend.c | 10 + refs/refs-internal.h | 3 + refs/reftable-backend.c | 7 + t/meson.build | 3 +- t/pack-refs-tests.sh | 431 +++++++++++++++++++++++++++ t/t0601-reffiles-pack-refs.sh | 430 +------------------------- t/t1463-refs-optimize.sh | 17 ++ 18 files changed, 648 insertions(+), 531 deletions(-) create mode 100644 Documentation/pack-refs-options.adoc create mode 100644 pack-refs.c create mode 100644 pack-refs.h create mode 100644 t/pack-refs-tests.sh create mode 100755 t/t1463-refs-optimize.sh
Range-diff against v3:
1: a837ae6f5d ! 1: 8d8aa56fe9 refs: add a generic 'optimize' API
@@ Metadata
## Commit message ##
refs: add a generic 'optimize' API
- Add a new generic refs_optimize() API function that dispatches to a
+ The existing pack-refs API is conceptually tied to the 'files'
+ backend, but its behavior is generic (e.g., it triggers compaction for
+ reftable). This naming is confusing.
+
+ Introduce a new generic refs_optimize() API that dispatches to a
backend-specific implementation via a new 'optimize' vtable method.
This lays the architectural groundwork for different reference backends
@@ refs.c: int refs_pack_refs(struct ref_store *refs, struct pack_refs_opts *opts)
+int refs_optimize(struct ref_store *refs, struct pack_refs_opts *opts) +{
-+ if (!refs->be->optimize)
-+ return 0;
+ return refs->be->optimize(refs, opts);
+}
+
2: e0613b14b6 = 2: aebdf90fef files-backend: implement 'optimize' action 3: ae8d2d29d9 = 3: 0b41b52e36 reftable-backend: implement 'optimize' action 4: a8cba8a355 ! 4: bcb76f2460 builtin/pack-refs: convert to use the generic refs_optimize() API
@@ Metadata
## Commit message ##
builtin/pack-refs: convert to use the generic refs_optimize() API
- The git pack-refs command is tied to the 'files' reference backend. In
- a repository that uses a different backend (like 'reftable'), the
- command is a no-op.
+ The git pack-refs command behaves generically, triggering a pack for
+ the 'files' backend and a compaction for the 'reftable' backend.
+ However, the name of the command and its corresponding API is
+ conceptually tied to the 'files' backend implementation.
- To make git pack-refs a truly generic frontend for reference
- optimization, refactor it to use the new generic refs_optimize() API.
- This will allow the command to automatically work with any backend
- that implements the optimize action in the future.
+ To create a cleaner, more generic interface, refactor git pack-refs to
+ use the new refs_optimize() API. "Optimize" is a better semantic term
+ for this generic action.
- The command continues to handle parsing its own command-line options,
- but now calls the generic API to perform the action instead of a
- backend-specific function.
+ This change allows git pack-refs to act as a backend-agnostic frontend
+ for reference optimization, and paves the way for the new `git refs
+ optimize` command to do the same.
5: ec1085ccd8 = 5: 6c6d63edff builtin/pack-refs: factor out core logic into a shared library 6: e1758816bf = 6: 5c5d3e2699 doc: pack-refs: factor out common options 7: e3a908fe72 ! 7: d2bff276b8 builtin/refs: add optimize subcommand
@@ builtin/refs.c: static int cmd_refs_list(int argc, const char **argv, const char
}
+static int cmd_refs_optimize(int argc, const char **argv, const char *prefix,
-+ struct repository *repo)
++ struct repository *repo)
+{
+ static char const * const refs_optimize_usage[] = {
+ REFS_OPTIMIZE_USAGE,
8: 4f63632ac2 = 8: 5a865d2828 t0601: refactor tests to be shareable 9: 39eed2831a = 9: 3d7d40b510 t: add test for git refs optimize subcommand
base-commit: f814da676ae46aac5be0a98b99373a76dee6cedb
Signature · 1 line
2.34.1
PATCH v4 1/9 refs: add a generic 'optimize' API
The existing pack-refs API is conceptually tied to the 'files' backend, but its behavior is generic (e.g., it triggers compaction for reftable). This naming is confusing.
Introduce a new generic refs_optimize() API that dispatches to a backend-specific implementation via a new 'optimize' vtable method.
This lays the architectural groundwork for different reference backends (like 'files' and 'reftable') to provide their own storage optimization logic, which will be called from a single, generic entry point.
--- refs.c | 5 +++++ refs.h | 6 ++++++ refs/refs-internal.h | 3 +++ 3 files changed, 14 insertions(+)
refs.c +5 -0
@@ -2282,6 +2282,11 @@ int refs_pack_refs(struct ref_store *refs, struct pack_refs_opts *opts)return refs->be->pack_refs (refs, opts); } +int refs_optimize (struct ref_store *refs,struct pack_refs_opts *opts) +{ +return refs->be->optimize (refs, opts); +} +int peel_iterated_oid (struct repository *r,const struct object_id *base,struct object_id *peeled) { if (current_ref_iter &&
refs.h +6 -0
@@ -480,6 +480,12 @@ struct pack_refs_opts {*/ int refs_pack_refs (struct ref_store *refs,struct pack_refs_opts *opts); +/* +* Optimize the ref store. The exact behavior is up to the backend. +* For the files backend, this is equivalent to packing refs. +*/ +int refs_optimize (struct ref_store *refs,struct pack_refs_opts *opts); +/* * Setup reflog before using. Fill in err and return -1 on failure. */
refs/refs-internal.h +3 -0
@@ -447,6 +447,8 @@ typedef int ref_transaction_commit_fn(struct ref_store *refs,typedef int pack_refs_fn (struct ref_store *ref_store,struct pack_refs_opts *opts); +typedef int optimize_fn (struct ref_store *ref_store, +struct pack_refs_opts *opts);typedef int rename_ref_fn (struct ref_store *ref_store,const char *oldref,const char *newref,const char *logmsg);
@@ -572,6 +574,7 @@ struct ref_storage_be {ref_transaction_abort_fn *transaction_abort;pack_refs_fn *pack_refs; +optimize_fn *optimize;rename_ref_fn *rename_ref;copy_ref_fn *copy_ref;
Signature · 1 line
2.34.1
Re: [GSoC][PATCH v4 1/9] refs: add a generic 'optimize' API
Quoted 10 lines · Meet Soni
The existing
pack-refsAPI is conceptually tied to the 'files' backend, but its behavior is generic (e.g., it triggers compaction for reftable). This naming is confusing.Introduce a new generic
refs_optimize()API that dispatches to a backend-specific implementation via a new 'optimize' vtable method.This lays the architectural groundwork for different reference backends (like 'files' and 'reftable') to provide their own storage optimization logic, which will be called from a single, generic entry point.
I agree with this change in the architecture in general -- "packing refs" is certainly a term that is specific to the "files" backend. So renaming that infrastructure to instead say "optimizing refs" feels like a sensible step as it adjusts naming to reality.
But what I don't quite get is why we end up with both a pack_refs_fn and an optimize_fn after this series, where the latter is always calling the former. Wouldn't it be more sensible step to make this a couple of simple renames? E.g.:
- pack_refs_fn -> optimize_refs_fn
- refs_pack_refs() -> refs_optimize()
- struct pack_refs_opts -> struct refs_optimize_opts
It would probably be a bit of the bigger patch to do all these renames at once. But there aren't _that_ many users of this infra, and I'd quite welcome those changes.
Patrick
PATCH v4 2/9 files-backend: implement 'optimize' action
In reply to Meet SoniWith the generic refs_optimize() API now in place, provide the first implementation for the 'files' reference backend. This makes the new API functional for existing repositories and serves as the foundation for migrating user-facing commands to the new architecture.
The implementation simply calls the existing files_pack_refs() function, as 'packing' is the method used to optimize the files-based reference store.
Wire up the new files_optimize() function to the optimize slot in the files backend's virtual table.
--- refs/files-backend.c | 10 ++++++++++ 1 file changed, 10 insertions(+)
refs/files-backend.c +10 -0
@@ -1473,6 +1473,15 @@ static int files_pack_refs(struct ref_store *ref_store,return 0 ; } +static int files_optimize (struct ref_store *ref_store,struct pack_refs_opts *opts) +{ +/* +* For the "files" backend, "optimizing" is the same as "packing". +* So, we just call the existing worker function for packing. +*/ +return files_pack_refs (ref_store, opts); +} +/* * People using contrib's git-new-workdir have .git/logs/refs -> * /some/other/path/.git/logs/refs, and that may live on another device.
@@ -3909,6 +3918,7 @@ struct ref_storage_be refs_be_files = { .transaction_abort = files_transaction_abort, .pack_refs = files_pack_refs, + .optimize = files_optimize, .rename_ref = files_rename_ref, .copy_ref = files_copy_ref,
Signature · 1 line
2.34.1
PATCH v4 3/9 reftable-backend: implement 'optimize' action
In reply to Meet SoniTo make the new generic optimize API fully functional, provide an implementation for the 'reftable' reference backend.
For the reftable backend, the 'optimize' action is to compact its tables. The existing reftable_be_pack_refs() function already provides this logic, so the new reftable_be_optimize() function simply calls it.
Wire up the new function to the optimize slot in the reftable backend's virtual table.
--- refs/reftable-backend.c | 7 +++++++ 1 file changed, 7 insertions(+)
refs/reftable-backend.c +7 -0
@@ -1721,6 +1721,12 @@ static int reftable_be_pack_refs(struct ref_store *ref_store,return ret; } +static int reftable_be_optimize (struct ref_store *ref_store, +struct pack_refs_opts *opts) +{ +return reftable_be_pack_refs (ref_store, opts); +} +struct write_create_symref_arg {struct reftable_ref_store *refs;struct reftable_stack *stack;
@@ -2702,6 +2708,7 @@ struct ref_storage_be refs_be_reftable = { .transaction_abort = reftable_be_transaction_abort, .pack_refs = reftable_be_pack_refs, + .optimize = reftable_be_optimize, .rename_ref = reftable_be_rename_ref, .copy_ref = reftable_be_copy_ref,
Signature · 1 line
2.34.1
PATCH v4 4/9 builtin/pack-refs: convert to use the generic refs_optimize() API
In reply to Meet SoniThe git pack-refs command behaves generically, triggering a pack for the 'files' backend and a compaction for the 'reftable' backend. However, the name of the command and its corresponding API is conceptually tied to the 'files' backend implementation.
To create a cleaner, more generic interface, refactor git pack-refs to use the new refs_optimize() API. "Optimize" is a better semantic term for this generic action.
This change allows git pack-refs to act as a backend-agnostic frontend for reference optimization, and paves the way for the new git refs optimize command to do the same.
--- builtin/pack-refs.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
builtin/pack-refs.c +1 -1
@@ -51,7 +51,7 @@ int cmd_pack_refs(int argc,if (!pack_refs_opts.includes->nr)string_list_append (pack_refs_opts.includes,"refs/tags/*" ); - ret =refs_pack_refs (get_main_ref_store (repo), &pack_refs_opts); + ret =refs_optimize (get_main_ref_store (repo), &pack_refs_opts);clear_ref_exclusions (&excludes);string_list_clear (&included_refs,0 );
Signature · 1 line
2.34.1
PATCH v4 5/9 builtin/pack-refs: factor out core logic into a shared library
In reply to Meet SoniThe implementation of git pack-refs is monolithic within cmd_pack_refs(), making it impossible to share its logic with other commands. To enable code reuse for the upcoming git refs optimize subcommand, refactor the core logic into a shared helper function.
Split the original builtin/pack-refs.c file into two parts:
- A new shared library file, pack-refs.c, which contains the core option parsing and packing logic in a new pack_refs_core() helper function.
- The original builtin/pack-refs.c, which is now a thin wrapper responsible only for defining the git pack-refs command and calling the shared helper.
A new pack-refs.h header is also introduced to define the public interface for this shared logic.
--- Makefile | 1 + builtin/pack-refs.c | 54 ++++--------------------------------------- meson.build | 1 + pack-refs.c | 56 +++++++++++++++++++++++++++++++++++++++++++++ pack-refs.h | 23 +++++++++++++++++++ 5 files changed, 86 insertions(+), 49 deletions(-) create mode 100644 pack-refs.c create mode 100644 pack-refs.h
Makefile +1 -0
@@ -1094,6 +1094,7 @@ LIB_OBJS += pack-bitmap.o LIB_OBJS += pack-check.o LIB_OBJS += pack-mtimes.o LIB_OBJS += pack-objects.o +LIB_OBJS += pack-refs.o LIB_OBJS += pack-revindex.o LIB_OBJS += pack-write.o LIB_OBJS += packfile.o
builtin/pack-refs.c +5 -49
65 lines not on this page · raw message
meson.build +1 -0
@@ -407,6 +407,7 @@ libgit_sources = [ 'pack-check.c', 'pack-mtimes.c', 'pack-objects.c', + 'pack-refs.c', 'pack-revindex.c', 'pack-write.c', 'packfile.c',
pack-refs.c new file +56 -0
56 lines not on this page · raw message
pack-refs.h new file +23 -0
@@ -0,0 +1,23 @@ +#ifndef PACK_REFS_H +#define PACK_REFS_H + +struct repository ; + +/* +* Shared usage string for options common to git-pack-refs(1) +* and git-refs-optimize(1). The command-specific part (e.g., "git refs optimize ") +* must be prepended by the caller. +*/ +#define PACK_REFS_OPTS \ + "[--all] [--no-prune] [--auto] [--include <pattern>] [--exclude <pattern>]" + +/* +* The core logic for pack-refs and its clones. +*/ +int pack_refs_core (int argc, +const char **argv, +const char *prefix, +struct repository *repo, +const char *const *usage_opts); + +#endif /* PACK_REFS_H */
Signature · 1 line
2.34.1
Re: [GSoC][PATCH v4 5/9] builtin/pack-refs: factor out core logic into a shared library
Quoted 10 lines · Meet Soni
The implementation of
git pack-refsis monolithic withincmd_pack_refs(), making it impossible to share its logic with other commands. To enable code reuse for the upcominggit refs optimizesubcommand, refactor the core logic into a shared helper function.Split the original
builtin/pack-refs.cfile into two parts:- A new shared library file,
pack-refs.c, which contains the core option parsing and packing logic in a newpack_refs_core()helper function.
Should we maybe host that file in "refs/pack.c"? This ensures that all ref-related infra continues to sit in one place. We could also build on your previous steps and call it "refs/optimize.c" right away.
Quoted 6 lines
pack-refs.c new file +0
-0
pack-refs.c line 9
Quoted 9 lines
pack-refs.c new file +9
-0@@ -0,0 +1,56 @@ +#include "builtin.h" +#include "config.h" +#include "environment.h" +#include "pack-refs.h" +#include "parse-options.h" +#include "refs.h" +#include "revision.h" + +int pack_refs_core(int argc,
If we want to go with "refs/optimize.c" I'd call this refs_optimize_core().
Quoted 7 lines
pack-refs.h new file +1
-0@@ -0,0 +1,23 @@ +#ifndef PACK_REFS_H
pack-refs.h line 11
Quoted 10 lines
pack-refs.h new file +10
-0+#define PACK_REFS_H + +struct repository ; + +/* +* Shared usage string for options common to git-pack-refs(1) +* and git-refs-optimize(1). The command-specific part (e.g., "git refs optimize ") +* must be prepended by the caller. +*/ +#define PACK_REFS_OPTS \
This would become REFS_OPTIMIZE_OPTS.
pack-refs.h line 23
Quoted 12 lines
pack-refs.h new file +12
-0+"[--all] [--no-prune] [--auto] [--include <pattern>] [--exclude <pattern>]" + +/* +* The core logic for pack-refs and its clones. +*/ +int pack_refs_core(int argc, + const char **argv, + const char *prefix, + struct repository *repo, + const char * const *usage_opts); + +#endif /* PACK_REFS_H */
Patrick
PATCH v4 6/9 doc: pack-refs: factor out common options
In reply to Meet SoniIn preparation for adding documentation for git refs optimize, factor out the common options from the git-pack-refs man page into a shareable file pack-refs-options.adoc and update git-pack-refs.adoc to use an include:: macro.
This change is a pure refactoring and results in no change to the final rendered documentation for pack-refs.
--- Documentation/git-pack-refs.adoc | 53 +--------------------------- Documentation/pack-refs-options.adoc | 52 +++++++++++++++++++++++++++ 2 files changed, 53 insertions(+), 52 deletions(-) create mode 100644 Documentation/pack-refs-options.adoc
Documentation/git-pack-refs.adoc +1 -52
@@ -45,58 +45,7 @@ unpacked. OPTIONS ------- ---all:: - -The command by default packs all tags and refs that are already -packed, and leaves other refs -alone. This is because branches are expected to be actively -developed and packing their tips does not help performance. -This option causes all refs to be packed as well, with the exception -of hidden refs, broken refs, and symbolic refs. Useful for a repository -with many branches of historical interests. - ---no-prune:: - -The command usually removes loose refs under `$GIT_DIR/refs` -hierarchy after packing them. This option tells it not to. - ---auto:: - -Pack refs as needed depending on the current state of the ref database. The -behavior depends on the ref format used by the repository and may change in the -future. -+ - - "files": Loose references are packed into the `packed-refs` file - based on the ratio of loose references to the size of the - `packed-refs` file. The bigger the `packed-refs` file, the more loose - references need to exist before we repack. -+ - - "reftable": Tables are compacted such that they form a geometric - sequence. For two tables N and N+1, where N+1 is newer, this - maintains the property that N is at least twice as big as N+1. Only - tables that violate this property are compacted. - ---include <pattern>:: - -Pack refs based on a `glob(7)` pattern. Repetitions of this option -accumulate inclusion patterns. If a ref is both included in `--include` and -`--exclude`, `--exclude` takes precedence. Using `--include` will preclude all -tags from being included by default. Symbolic refs and broken refs will never -be packed. When used with `--all`, it will be a noop. Use `--no-include` to clear -and reset the list of patterns. - ---exclude <pattern>:: - -Do not pack refs matching the given `glob(7)` pattern. Repetitions of this option -accumulate exclusion patterns. Use `--no-exclude` to clear and reset the list of -patterns. If a ref is already packed, including it with `--exclude` will not -unpack it. -+ -When used with `--all`, pack only loose refs which do not match any of -the provided `--exclude` patterns. -+ -When used with `--include`, refs provided to `--include`, minus refs that are -provided to `--exclude` will be packed. +include::pack-refs-options.adoc[] BUGS
Documentation/pack-refs-options.adoc new file +52 -0
52 lines not on this page · raw message
Signature · 1 line
2.34.1
PATCH v4 7/9 builtin/refs: add optimize subcommand
In reply to Meet SoniAs part of the ongoing effort to consolidate reference handling, introduce a new optimize subcommand. This command provides the same functionality and exit-code behavior as git pack-refs, serving as its modern replacement.
Implement cmd_refs_optimize by having it call the pack_refs_core() helper function. This helper was factored out of the original cmd_pack_refs in a preceding commit, allowing both commands to share the same core logic as independent peers.
Add documentation for the new command. The man page leverages the shared options file, created in a previous commit, by using the AsciiDoc include:: macro to ensure consistency with git-pack-refs(1).
--- Documentation/git-refs.adoc | 10 ++++++++++ builtin/refs.c | 17 +++++++++++++++++ 2 files changed, 27 insertions(+)
Documentation/git-refs.adoc +10 -0
@@ -18,6 +18,7 @@ git refs list [--count=<count>] [--shell|--perl|--python|--tcl] [--contains[=<object>]] [--no-contains[=<object>]] [(--exclude=<pattern>)...] [--start-after=<marker>] [ --stdin | (<pattern>...)] +git refs optimize [--all] [--no-prune] [--auto] [--include <pattern>] [--exclude <pattern>] DESCRIPTION -----------
@@ -38,6 +39,11 @@ list:: formatting, and sorting. This subcommand is an alias for linkgit:git-for-each-ref[1] and offers identical functionality. +optimize:: + Optimizes references to improve repository performance and reduce disk + usage. This subcommand is an alias for linkgit:git-pack-refs[1] and + offers identical functionality. + OPTIONS -------
@@ -73,6 +79,10 @@ The following options are specific to 'git refs list': include::for-each-ref-options.adoc[] +The following options are specific to 'git refs optimize': + +include::pack-refs-options.adoc[] + KNOWN LIMITATIONS -----------------
builtin/refs.c +17 -0
@@ -2,6 +2,7 @@#include "builtin.h" #include "config.h" #include "fsck.h" +#include "pack-refs.h" #include "parse-options.h" #include "refs.h" #include "strbuf.h"
@@ -14,6 +15,9 @@#define REFS_VERIFY_USAGE \ N_("git refs verify [--strict] [--verbose]") +#define REFS_OPTIMIZE_USAGE \ + N_("git refs optimize " PACK_REFS_OPTS) +static int cmd_refs_migrate (int argc,const char **argv,const char *prefix, struct repository *repo UNUSED) {
@@ -113,6 +117,17 @@ static int cmd_refs_list(int argc, const char **argv, const char *prefix,return for_each_ref_core (argc, argv, prefix, repo, refs_list_usage); } +static int cmd_refs_optimize (int argc,const char **argv,const char *prefix, +struct repository *repo) +{ +static char const *const refs_optimize_usage[] = { +REFS_OPTIMIZE_USAGE , +NULL + }; + +return pack_refs_core (argc, argv, prefix, repo, refs_optimize_usage); +} + int cmd_refs(int argc, const char **argv, const char *prefix,
@@ -122,6 +137,7 @@ int cmd_refs(int argc,REFS_MIGRATE_USAGE ,REFS_VERIFY_USAGE ,"git refs list " COMMON_USAGE_FOR_EACH_REF , +REFS_OPTIMIZE_USAGE ,NULL , }; parse_opt_subcommand_fn *fn =NULL ;
@@ -129,6 +145,7 @@ int cmd_refs(int argc,OPT_SUBCOMMAND ("migrate" , &fn, cmd_refs_migrate),OPT_SUBCOMMAND ("verify" , &fn, cmd_refs_verify),OPT_SUBCOMMAND ("list" , &fn, cmd_refs_list), +OPT_SUBCOMMAND ("optimize" , &fn, cmd_refs_optimize), OPT_END(), };
Signature · 1 line
2.34.1
PATCH v4 8/9 t0601: refactor tests to be shareable
In reply to Meet SoniIn preparation for adding tests for the new git refs optimize command, refactor the existing t0601 test suite to make its logic shareable.
Move the core test logic from t0601-reffiles-pack-refs.sh into a new pack-refs-tests.sh file. Inside this new script, replace hardcoded calls to "pack-refs" with the $pack_refs variable.
The original t0601-reffiles-pack-refs.sh script now becomes a simple "driver". It is responsible for setting the default value of the variable and then sourcing the test library.
This new structure follows the established pattern used for sharing tests between git-for-each-ref and git-refs list and prepares the test suite for the refs optimize tests to be added in a subsequent commit.
--- t/pack-refs-tests.sh | 431 ++++++++++++++++++++++++++++++++++ t/t0601-reffiles-pack-refs.sh | 430 +-------------------------------- 2 files changed, 432 insertions(+), 429 deletions(-) create mode 100644 t/pack-refs-tests.sh
t/pack-refs-tests.sh new file +431 -0
@@ -0,0 +1,431 @@ +pack_refs=${pack_refs:-pack-refs} + +test_expect_success 'enable reflogs' ' + git config core.logallrefupdates true +' + +test_expect_success 'prepare a trivial repository' ' + echo Hello > A && + git update-index --add A && + git commit -m "Initial commit." && + HEAD=$(git rev-parse --verify HEAD) +' + +test_expect_success '${pack_refs} --prune --all' ' + test_path_is_missing .git/packed-refs && + git ${pack_refs} --no-prune --all && + test_path_is_file .git/packed-refs && + N=$(find .git/refs -type f | wc -l) && + test "$N" != 0 && + + git ${pack_refs} --prune --all && + test_path_is_file .git/packed-refs && + N=$(find .git/refs -type f) && + test -z "$N" +' + +SHA1= + +test_expect_success 'see if git show-ref works as expected' ' + git branch a && + SHA1=$(cat .git/refs/heads/a) && + echo "$SHA1 refs/heads/a" >expect && + git show-ref a >result && + test_cmp expect result +' + +test_expect_success 'see if a branch still exists when packed' ' + git branch b && + git ${pack_refs} --all && + rm -f .git/refs/heads/b && + echo "$SHA1 refs/heads/b" >expect && + git show-ref b >result && + test_cmp expect result +' + +test_expect_success 'git branch c/d should barf if branch c exists' ' + git branch c && + git ${pack_refs} --all && + rm -f .git/refs/heads/c && + test_must_fail git branch c/d +' + +test_expect_success 'see if a branch still exists after git ${pack_refs} --prune' ' + git branch e && + git ${pack_refs} --all --prune && + echo "$SHA1 refs/heads/e" >expect && + git show-ref e >result && + test_cmp expect result +' + +test_expect_success 'see if git ${pack_refs} --prune remove ref files' ' + git branch f && + git ${pack_refs} --all --prune && + ! test -f .git/refs/heads/f +' + +test_expect_success 'see if git ${pack_refs} --prune removes empty dirs' ' + git branch r/s/t && + git ${pack_refs} --all --prune && + ! test -e .git/refs/heads/r +' + +test_expect_success 'git branch g should work when git branch g/h has been deleted' ' + git branch g/h && + git ${pack_refs} --all --prune && + git branch -d g/h && + git branch g && + git ${pack_refs} --all && + git branch -d g +' + +test_expect_success 'git branch i/j/k should barf if branch i exists' ' + git branch i && + git ${pack_refs} --all --prune && + test_must_fail git branch i/j/k +' + +test_expect_success 'test git branch k after branch k/l/m and k/lm have been deleted' ' + git branch k/l && + git branch k/lm && + git branch -d k/l && + git branch k/l/m && + git branch -d k/l/m && + git branch -d k/lm && + git branch k +' + +test_expect_success 'test git branch n after some branch deletion and pruning' ' + git branch n/o && + git branch n/op && + git branch -d n/o && + git branch n/o/p && + git branch -d n/op && + git ${pack_refs} --all --prune && + git branch -d n/o/p && + git branch n +' + +test_expect_success 'test excluded refs are not packed' ' + git branch dont_pack1 && + git branch dont_pack2 && + git branch pack_this && + git ${pack_refs} --all --exclude "refs/heads/dont_pack*" && + test -f .git/refs/heads/dont_pack1 && + test -f .git/refs/heads/dont_pack2 && + ! test -f .git/refs/heads/pack_this' + +test_expect_success 'test --no-exclude refs clears excluded refs' ' + git branch dont_pack3 && + git branch dont_pack4 && + git ${pack_refs} --all --exclude "refs/heads/dont_pack*" --no-exclude && + ! test -f .git/refs/heads/dont_pack3 && + ! test -f .git/refs/heads/dont_pack4' + +test_expect_success 'test only included refs are packed' ' + git branch pack_this1 && + git branch pack_this2 && + git tag dont_pack5 && + git ${pack_refs} --include "refs/heads/pack_this*" && + test -f .git/refs/tags/dont_pack5 && + ! test -f .git/refs/heads/pack_this1 && + ! test -f .git/refs/heads/pack_this2' + +test_expect_success 'test --no-include refs clears included refs' ' + git branch pack1 && + git branch pack2 && + git ${pack_refs} --include "refs/heads/pack*" --no-include && + test -f .git/refs/heads/pack1 && + test -f .git/refs/heads/pack2' + +test_expect_success 'test --exclude takes precedence over --include' ' + git branch dont_pack5 && + git ${pack_refs} --include "refs/heads/pack*" --exclude "refs/heads/pack*" && + test -f .git/refs/heads/dont_pack5' + +test_expect_success 'see if up-to-date packed refs are preserved' ' + git branch q && + git ${pack_refs} --all --prune && + git update-ref refs/heads/q refs/heads/q && + ! test -f .git/refs/heads/q +' + +test_expect_success 'pack, prune and repack' ' + git tag foo && + git ${pack_refs} --all --prune && + git show-ref >all-of-them && + git ${pack_refs} && + git show-ref >again && + test_cmp all-of-them again +' + +test_expect_success 'explicit ${pack_refs} with dangling packed reference' ' + git commit --allow-empty -m "soon to be garbage-collected" && + git ${pack_refs} --all && + git reset --hard HEAD^ && + git reflog expire --expire=all --all && + git prune --expire=all && + git ${pack_refs} --all 2>result && + test_must_be_empty result +' + +test_expect_success 'delete ref with dangling packed version' ' + git checkout -b lamb && + git commit --allow-empty -m "future garbage" && + git ${pack_refs} --all && + git reset --hard HEAD^ && + git checkout main && + git reflog expire --expire=all --all && + git prune --expire=all && + git branch -d lamb 2>result && + test_must_be_empty result +' + +test_expect_success 'delete ref while another dangling packed ref' ' + git branch lamb && + git commit --allow-empty -m "future garbage" && + git ${pack_refs} --all && + git reset --hard HEAD^ && + git reflog expire --expire=all --all && + git prune --expire=all && + git branch -d lamb 2>result && + test_must_be_empty result +' + +test_expect_success 'pack ref directly below refs/' ' + git update-ref refs/top HEAD && + git ${pack_refs} --all --prune && + grep refs/top .git/packed-refs && + test_path_is_missing .git/refs/top +' + +test_expect_success 'do not pack ref in refs/bisect' ' + git update-ref refs/bisect/local HEAD && + git ${pack_refs} --all --prune && + ! grep refs/bisect/local .git/packed-refs >/dev/null && + test_path_is_file .git/refs/bisect/local +' + +test_expect_success 'disable reflogs' ' + git config core.logallrefupdates false && + rm -rf .git/logs +' + +test_expect_success 'create packed foo/bar/baz branch' ' + git branch foo/bar/baz && + git ${pack_refs} --all --prune && + test_path_is_missing .git/refs/heads/foo/bar/baz && + test_must_fail git reflog exists refs/heads/foo/bar/baz +' + +test_expect_success 'notice d/f conflict with existing directory' ' + test_must_fail git branch foo && + test_must_fail git branch foo/bar +' + +test_expect_success 'existing directory reports concrete ref' ' + test_must_fail git branch foo 2>stderr && + test_grep refs/heads/foo/bar/baz stderr +' + +test_expect_success 'notice d/f conflict with existing ref' ' + test_must_fail git branch foo/bar/baz/extra && + test_must_fail git branch foo/bar/baz/lots/of/extra/components +' + +test_expect_success 'reject packed-refs with unterminated line' ' + cp .git/packed-refs .git/packed-refs.bak && + test_when_finished "mv .git/packed-refs.bak .git/packed-refs" && + printf "%s" "$HEAD refs/zzzzz" >>.git/packed-refs && + echo "fatal: unterminated line in .git/packed-refs: $HEAD refs/zzzzz" >expected_err && + test_must_fail git for-each-ref >out 2>err && + test_cmp expected_err err +' + +test_expect_success 'reject packed-refs containing junk' ' + cp .git/packed-refs .git/packed-refs.bak && + test_when_finished "mv .git/packed-refs.bak .git/packed-refs" && + printf "%s\n" "bogus content" >>.git/packed-refs && + echo "fatal: unexpected line in .git/packed-refs: bogus content" >expected_err && + test_must_fail git for-each-ref >out 2>err && + test_cmp expected_err err +' + +test_expect_success 'reject packed-refs with a short SHA-1' ' + cp .git/packed-refs .git/packed-refs.bak && + test_when_finished "mv .git/packed-refs.bak .git/packed-refs" && + printf "%.7s %s\n" $HEAD refs/zzzzz >>.git/packed-refs && + printf "fatal: unexpected line in .git/packed-refs: %.7s %s\n" $HEAD refs/zzzzz >expected_err && + test_must_fail git for-each-ref >out 2>err && + test_cmp expected_err err +' + +test_expect_success 'timeout if packed-refs.lock exists' ' + LOCK=.git/packed-refs.lock && + >"$LOCK" && + test_when_finished "rm -f $LOCK" && + test_must_fail git ${pack_refs} --all --prune +' + +test_expect_success 'retry acquiring packed-refs.lock' ' + LOCK=.git/packed-refs.lock && + >"$LOCK" && + test_when_finished "wait && rm -f $LOCK" && + { + ( sleep 1 && rm -f $LOCK ) & + } && + git -c core.packedrefstimeout=3000 ${pack_refs} --all --prune +' + +test_expect_success SYMLINKS 'pack symlinked packed-refs' ' + # First make sure that symlinking works when reading: + git update-ref refs/heads/lossy refs/heads/main && + git for-each-ref >all-refs-before && + mv .git/packed-refs .git/my-deviant-packed-refs && + ln -s my-deviant-packed-refs .git/packed-refs && + git for-each-ref >all-refs-linked && + test_cmp all-refs-before all-refs-linked && + git ${pack_refs} --all --prune && + git for-each-ref >all-refs-packed && + test_cmp all-refs-before all-refs-packed && + test -h .git/packed-refs && + test "$(test_readlink .git/packed-refs)" = "my-deviant-packed-refs" +' + +# The 'packed-refs' file is stored directly in .git/. This means it is global +# to the repository, and can only contain refs that are shared across all +# worktrees. +test_expect_success 'refs/worktree must not be packed' ' + test_commit initial && + test_commit wt1 && + test_commit wt2 && + git worktree add wt1 wt1 && + git worktree add wt2 wt2 && + git checkout initial && + git update-ref refs/worktree/foo HEAD && + git -C wt1 update-ref refs/worktree/foo HEAD && + git -C wt2 update-ref refs/worktree/foo HEAD && + git ${pack_refs} --all && + test_path_is_missing .git/refs/tags/wt1 && + test_path_is_file .git/refs/worktree/foo && + test_path_is_file .git/worktrees/wt1/refs/worktree/foo && + test_path_is_file .git/worktrees/wt2/refs/worktree/foo +' + +# we do not want to count on running ${pack_refs} to +# actually pack it, as it is perfectly reasonable to +# skip processing a broken ref +test_expect_success 'create packed-refs file with broken ref' ' + test_tick && git commit --allow-empty -m one && + recoverable=$(git rev-parse HEAD) && + test_tick && git commit --allow-empty -m two && + missing=$(git rev-parse HEAD) && + rm -f .git/refs/heads/main && + cat >.git/packed-refs <<-EOF && + $missing refs/heads/main + $recoverable refs/heads/other + EOF + echo $missing >expect && + git rev-parse refs/heads/main >actual && + test_cmp expect actual +' + +test_expect_success '${pack_refs} does not silently delete broken packed ref' ' + git ${pack_refs} --all --prune && + git rev-parse refs/heads/main >actual && + test_cmp expect actual +' + +test_expect_success '${pack_refs} does not drop broken refs during deletion' ' + git update-ref -d refs/heads/other && + git rev-parse refs/heads/main >actual && + test_cmp expect actual +' + +for command in "git ${pack_refs} --all --auto" "git maintenance run --task=${pack_refs} --auto" +do + test_expect_success "$command does not repack below 16 refs without packed-refs" ' + test_when_finished "rm -rf repo" && + git init repo && + ( + cd repo && + git config set maintenance.auto false && + git commit --allow-empty --message "initial" && + + # Create 14 additional references, which brings us to + # 15 together with the default branch. + printf "create refs/heads/loose-%d HEAD\n" $(test_seq 14) >stdin && + git update-ref --stdin <stdin && + test_path_is_missing .git/packed-refs && + git ${pack_refs} --auto --all && + test_path_is_missing .git/packed-refs && + + # Create the 16th reference, which should cause us to repack. + git update-ref refs/heads/loose-15 HEAD && + git ${pack_refs} --auto --all && + test_path_is_file .git/packed-refs + ) + ' + + test_expect_success "$command does not repack below 16 refs with small packed-refs" ' + test_when_finished "rm -rf repo" && + git init repo && + ( + cd repo && + git config set maintenance.auto false && + git commit --allow-empty --message "initial" && + + git ${pack_refs} --all && + test_line_count = 2 .git/packed-refs && + + # Create 15 loose references. + printf "create refs/heads/loose-%d HEAD\n" $(test_seq 15) >stdin && + git update-ref --stdin <stdin && + git ${pack_refs} --auto --all && + test_line_count = 2 .git/packed-refs && + + # Create the 16th loose reference, which should cause us to repack. + git update-ref refs/heads/loose-17 HEAD && + git ${pack_refs} --auto --all && + test_line_count = 18 .git/packed-refs + ) + ' + + test_expect_success "$command scales with size of packed-refs" ' + test_when_finished "rm -rf repo" && + git init repo && + ( + cd repo && + git config set maintenance.auto false && + git commit --allow-empty --message "initial" && + + # Create 99 packed refs. This should cause the heuristic + # to require more than the minimum amount of loose refs. + test_seq 99 | + while read i + do + printf "create refs/heads/packed-%d HEAD\n" $i || return 1 + done >stdin && + git update-ref --stdin <stdin && + git ${pack_refs} --all && + test_line_count = 101 .git/packed-refs && + + # Create 24 loose refs, which should not yet cause us to repack. + printf "create refs/heads/loose-%d HEAD\n" $(test_seq 24) >stdin && + git update-ref --stdin <stdin && + git ${pack_refs} --auto --all && + test_line_count = 101 .git/packed-refs && + + # Create another handful of refs to cross the border. + # Note that we explicitly do not check for strict + # boundaries here, as this also depends on the size of + # the object hash. + printf "create refs/heads/addn-%d HEAD\n" $(test_seq 10) >stdin && + git update-ref --stdin <stdin && + git ${pack_refs} --auto --all && + test_line_count = 135 .git/packed-refs + ) + ' +done + +test_done
t/t0601-reffiles-pack-refs.sh +1 -429
433 lines not on this page · raw message
Signature · 1 line
2.34.1
PATCH v4 9/9 t: add test for git refs optimize subcommand
In reply to Meet SoniAdd a test script, t/t1463-refs-optimize.sh, for the new git refs optimize command.
This script acts as a simple driver, leveraging the shared test library created in the preceding commit. It works by overriding the $pack_refs variable to "refs optimize" and then sourcing the shared library (t/pack-refs-tests.sh).
This approach ensures that git refs optimize is tested against the entire comprehensive test suite of git pack-refs, verifying that it acts as a compatible drop-in replacement.
--- t/meson.build | 3 ++- t/t1463-refs-optimize.sh | 17 +++++++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) create mode 100755 t/t1463-refs-optimize.sh
t/meson.build +2 -1
@@ -211,6 +211,7 @@ integration_tests = [ 't1451-fsck-buffer.sh', 't1460-refs-migrate.sh', 't1461-refs-list.sh', + 't1463-refs-optimize.sh', 't1500-rev-parse.sh', 't1501-work-tree.sh', 't1502-rev-parse-parseopt.sh',
@@ -1219,4 +1220,4 @@ if perl.found() and time.found() timeout: 0, ) endforeach -endif +endif
t/t1463-refs-optimize.sh new file +17 -0
@@ -0,0 +1,17 @@ +#!/bin/sh + +test_description='git refs optimize should not change the branch semantic + +This test runs git refs optimize and git show-ref and checks that the branch +semantic is still the same. +' + +GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main +export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME +GIT_TEST_DEFAULT_REF_FORMAT=files +export GIT_TEST_DEFAULT_REF_FORMAT + +. ./test-lib.sh + +pack_refs='refs optimize' +. "$TEST_DIRECTORY"/pack-refs-tests.sh
Signature · 1 line
2.34.1
Quoted 10 lines · Meet Soni
Hi everyone,
This series introduces
git refs optimizeas a modern replacement forgit pack-refs, continuing the effort to consolidate commands under thegit refsnamespace.Changes in v4:
- Improved commit messages and formatting.
- Removed the NULL check from therefs_optimize()dispatcher inrefs.cto align with the project's conventions for API functions
I haven't read these patches carefully myself, but if you need further updates to this topic, you may want to rebase on a slightly newer commit that already has "git refs exists". Either 07f29476de (i.e. the merge of that topic to 'master'), or 92c87bdc40 (the batch that contains that merge) would work fine.
I can keep resolving the conflicts between this and the other topic just fine, but I'd rather see you tested and proofread these patches yourself in the context of the codebase that these changes will eventually be used with.
Thanks.