builtin/refs: add optimize subcommand

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> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Meet Soni committed Sep 19, 2025 at 13:56 UTC ecc70a48a5ea5e568b1cbdd111f7ddba62dbe4d6
2 files changed +27
Documentation/git-refs.adoc
+10
@@ -18,6 +18,7 @@ git refs list [--count=<count>] [--shell|--perl|--python|--tcl]
18 [--contains[=<object>]] [--no-contains[=<object>]]
19 [(--exclude=<pattern>)...] [--start-after=<marker>]
20 [ --stdin | (<pattern>...)]
21 +git refs optimize [--all] [--no-prune] [--auto] [--include <pattern>] [--exclude <pattern>]
22
23 DESCRIPTION
24 -----------
@@ -38,6 +39,11 @@ list::
39 formatting, and sorting. This subcommand is an alias for
40 linkgit:git-for-each-ref[1] and offers identical functionality.
41
42 +optimize::
43 + Optimizes references to improve repository performance and reduce disk
44 + usage. This subcommand is an alias for linkgit:git-pack-refs[1] and
45 + offers identical functionality.
46 +
47 OPTIONS
48 -------
49
@@ -73,6 +79,10 @@ The following options are specific to 'git refs list':
79
80 include::for-each-ref-options.adoc[]
81
82 +The following options are specific to 'git refs optimize':
83 +
84 +include::pack-refs-options.adoc[]
85 +
86 KNOWN LIMITATIONS
87 -----------------
88
builtin/refs.c
+17
@@ -2,6 +2,7 @@
2 #include "builtin.h"
3 #include "config.h"
4 #include "fsck.h"
5 +#include "pack-refs.h"
6 #include "parse-options.h"
7 #include "refs.h"
8 #include "strbuf.h"
@@ -14,6 +15,9 @@
15 #define REFS_VERIFY_USAGE \
16 N_("git refs verify [--strict] [--verbose]")
17
18 +#define REFS_OPTIMIZE_USAGE \
19 + N_("git refs optimize " PACK_REFS_OPTS)
20 +
21 static int cmd_refs_migrate(int argc, const char **argv, const char *prefix,
22 struct repository *repo UNUSED)
23 {
@@ -113,6 +117,17 @@ static int cmd_refs_list(int argc, const char **argv, const char *prefix,
117 return for_each_ref_core(argc, argv, prefix, repo, refs_list_usage);
118 }
119
120 +static int cmd_refs_optimize(int argc, const char **argv, const char *prefix,
121 + struct repository *repo)
122 +{
123 + static char const * const refs_optimize_usage[] = {
124 + REFS_OPTIMIZE_USAGE,
125 + NULL
126 + };
127 +
128 + return pack_refs_core(argc, argv, prefix, repo, refs_optimize_usage);
129 +}
130 +
131 int cmd_refs(int argc,
132 const char **argv,
133 const char *prefix,
@@ -122,6 +137,7 @@ int cmd_refs(int argc,
137 REFS_MIGRATE_USAGE,
138 REFS_VERIFY_USAGE,
139 "git refs list " COMMON_USAGE_FOR_EACH_REF,
140 + REFS_OPTIMIZE_USAGE,
141 NULL,
142 };
143 parse_opt_subcommand_fn *fn = NULL;
@@ -129,6 +145,7 @@ int cmd_refs(int argc,
145 OPT_SUBCOMMAND("migrate", &fn, cmd_refs_migrate),
146 OPT_SUBCOMMAND("verify", &fn, cmd_refs_verify),
147 OPT_SUBCOMMAND("list", &fn, cmd_refs_list),
148 + OPT_SUBCOMMAND("optimize", &fn, cmd_refs_optimize),
149 OPT_END(),
150 };
151