git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[GSoC][PATCH v4 7/9] builtin/refs: add optimize subcommand

From
Meet Soni <meetsoni3017@gmail.com>
Date
Sep 19, 2025, 08:26 UTC
Message-ID
<20250919082647.535213-8-meetsoni3017@gmail.com>
In-Reply-To
<20250919082647.535213-1-meetsoni3017@gmail.com>

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.

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

Mentored-by: Patrick Steinhardt <ps@pks.im>
Mentored-by: shejialuo <shejialuo@gmail.com>
Signed-off-by: Meet Soni <meetsoni3017@gmail.com>
---
 Documentation/git-refs.adoc | 10 ++++++++++
 builtin/refs.c              | 17 +++++++++++++++++
 2 files changed, 27 insertions(+)
diff --git a/Documentation/git-refs.adoc b/Documentation/git-refs.adoc
index d462953fb5..e233f21eeb 100644
--- a/Documentation/git-refs.adoc
+++ b/Documentation/git-refs.adoc
@@ -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
 -----------------
 
diff --git a/builtin/refs.c b/builtin/refs.c
index 76224feba4..785f476e4b 100644
--- a/builtin/refs.c
+++ b/builtin/refs.c
@@ -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(),
 	};
 
-- 
2.34.1
Previous: Meet SoniNext: Meet Soni
Message 33 of 36 in “Add refs optimize subcommand”
  1. Meet SoniSep 6, 2025
  2. [GSoC][PATCH v2 1/5] builtin/pack-refs: factor out core logic into a shared libraryMeet Soni, Sep 6, 2025
  3. [GSoC][PATCH v2 2/5] doc: factor out common optionMeet Soni, Sep 6, 2025
  4. Junio C HamanoSep 8, 2025
  5. [GSoC][PATCH v2 3/5] builtin/refs: add optimize subcommandMeet Soni, Sep 6, 2025
  6. [GSoC][PATCH v2 4/5] t0601: refactor tests to be shareableMeet Soni, Sep 6, 2025
  7. [GSoC][PATCH v2 5/5] t: add test for git refs optimize subcommandMeet Soni, Sep 6, 2025
  8. Junio C HamanoSep 8, 2025
  9. Junio C HamanoSep 8, 2025
  10. [GSoC][PATCH v3 0/9] Add refs optimize subcommandMeet Soni, Sep 18, 2025
  11. [GSoC][PATCH v3 1/9] refs: add a generic 'optimize' APIMeet Soni, Sep 18, 2025
  12. shejialuoSep 18, 2025
  13. Junio C HamanoSep 18, 2025
  14. [GSoC][PATCH v3 2/9] files-backend: implement 'optimize' actionMeet Soni, Sep 18, 2025
  15. [GSoC][PATCH v3 3/9] reftable-backend: implement 'optimize' actionMeet Soni, Sep 18, 2025
  16. [GSoC][PATCH v3 4/9] builtin/pack-refs: convert to use the generic refs_optimize() APIMeet Soni, Sep 18, 2025
  17. shejialuoSep 18, 2025
  18. [GSoC][PATCH v3 5/9] builtin/pack-refs: factor out core logic into a shared libraryMeet Soni, Sep 18, 2025
  19. [GSoC][GSoC][PATCH v3 6/9] doc: pack-refs: factor out common optionsMeet Soni, Sep 18, 2025
  20. [GSoC][PATCH v3 7/9] builtin/refs: add optimize subcommandMeet Soni, Sep 18, 2025
  21. Junio C HamanoSep 18, 2025
  22. [GSoC][PATCH v3 8/9] t0601: refactor tests to be shareableMeet Soni, Sep 18, 2025
  23. [GSoC][PATCH v3 9/9] t: add test for git refs optimize subcommandMeet Soni, Sep 18, 2025
  24. [GSoC][PATCH v4 0/9] Add refs optimize subcommandMeet Soni, Sep 19, 2025
  25. [GSoC][PATCH v4 1/9] refs: add a generic 'optimize' APIMeet Soni, Sep 19, 2025
  26. Patrick SteinhardtSep 24, 2025
  27. [GSoC][PATCH v4 2/9] files-backend: implement 'optimize' actionMeet Soni, Sep 19, 2025
  28. [GSoC][PATCH v4 3/9] reftable-backend: implement 'optimize' actionMeet Soni, Sep 19, 2025
  29. [GSoC][PATCH v4 4/9] builtin/pack-refs: convert to use the generic refs_optimize() APIMeet Soni, Sep 19, 2025
  30. [GSoC][PATCH v4 5/9] builtin/pack-refs: factor out core logic into a shared libraryMeet Soni, Sep 19, 2025
  31. Patrick SteinhardtSep 24, 2025
  32. [GSoC][PATCH v4 6/9] doc: pack-refs: factor out common optionsMeet Soni, Sep 19, 2025
  33. [GSoC][PATCH v4 7/9] builtin/refs: add optimize subcommandMeet Soni, Sep 19, 2025
  34. [GSoC][PATCH v4 8/9] t0601: refactor tests to be shareableMeet Soni, Sep 19, 2025
  35. [GSoC][PATCH v4 9/9] t: add test for git refs optimize subcommandMeet Soni, Sep 19, 2025
  36. Junio C HamanoSep 19, 2025

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.