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

[GSoC][PATCH v4 5/9] builtin/pack-refs: factor out core logic into a shared library

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

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.

Mentored-by: Patrick Steinhardt <ps@pks.im>
Mentored-by: shejialuo <shejialuo@gmail.com>
Signed-off-by: Meet Soni <meetsoni3017@gmail.com>
---
 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
diff --git a/Makefile b/Makefile
index 555b7f4dc3..f51297ffc3 100644
--- a/Makefile
+++ b/Makefile
@@ -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
diff --git a/builtin/pack-refs.c b/builtin/pack-refs.c
index dfcf664524..3446b84cda 100644
--- a/builtin/pack-refs.c
+++ b/builtin/pack-refs.c
@@ -1,60 +1,16 @@
 #include "builtin.h"
-#include "config.h"
-#include "environment.h"
 #include "gettext.h"
-#include "parse-options.h"
-#include "refs.h"
-#include "revision.h"
-
-static char const * const pack_refs_usage[] = {
-	N_("git pack-refs [--all] [--no-prune] [--auto] [--include <pattern>] [--exclude <pattern>]"),
-	NULL
-};
+#include "pack-refs.h"
 
 int cmd_pack_refs(int argc,
 		  const char **argv,
 		  const char *prefix,
 		  struct repository *repo)
 {
-	struct ref_exclusions excludes = REF_EXCLUSIONS_INIT;
-	struct string_list included_refs = STRING_LIST_INIT_NODUP;
-	struct pack_refs_opts pack_refs_opts = {
-		.exclusions = &excludes,
-		.includes = &included_refs,
-		.flags = PACK_REFS_PRUNE,
-	};
-	struct string_list option_excluded_refs = STRING_LIST_INIT_NODUP;
-	struct string_list_item *item;
-	int pack_all = 0;
-	int ret;
-
-	struct option opts[] = {
-		OPT_BOOL(0, "all",   &pack_all, N_("pack everything")),
-		OPT_BIT(0, "prune", &pack_refs_opts.flags, N_("prune loose refs (default)"), PACK_REFS_PRUNE),
-		OPT_BIT(0, "auto", &pack_refs_opts.flags, N_("auto-pack refs as needed"), PACK_REFS_AUTO),
-		OPT_STRING_LIST(0, "include", pack_refs_opts.includes, N_("pattern"),
-			N_("references to include")),
-		OPT_STRING_LIST(0, "exclude", &option_excluded_refs, N_("pattern"),
-			N_("references to exclude")),
-		OPT_END(),
+	static char const * const pack_refs_usage[] = {
+		N_("git pack-refs " PACK_REFS_OPTS),
+		NULL
 	};
-	repo_config(repo, git_default_config, NULL);
-	if (parse_options(argc, argv, prefix, opts, pack_refs_usage, 0))
-		usage_with_options(pack_refs_usage, opts);
-
-	for_each_string_list_item(item, &option_excluded_refs)
-		add_ref_exclusion(pack_refs_opts.exclusions, item->string);
-
-	if (pack_all)
-		string_list_append(pack_refs_opts.includes, "*");
-
-	if (!pack_refs_opts.includes->nr)
-		string_list_append(pack_refs_opts.includes, "refs/tags/*");
-
-	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);
-	return ret;
+	return pack_refs_core(argc, argv, prefix, repo, pack_refs_usage);
 }
diff --git a/meson.build b/meson.build
index e8ec0eca16..cedaadad2e 100644
--- a/meson.build
+++ b/meson.build
@@ -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',
diff --git a/pack-refs.c b/pack-refs.c
new file mode 100644
index 0000000000..1a5e07d8b8
--- /dev/null
+++ b/pack-refs.c
@@ -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,
+		   const char **argv,
+		   const char *prefix,
+		   struct repository *repo,
+		   const char * const *usage_opts)
+{
+	struct ref_exclusions excludes = REF_EXCLUSIONS_INIT;
+	struct string_list included_refs = STRING_LIST_INIT_NODUP;
+	struct pack_refs_opts pack_refs_opts = {
+		.exclusions = &excludes,
+		.includes = &included_refs,
+		.flags = PACK_REFS_PRUNE,
+	};
+	struct string_list option_excluded_refs = STRING_LIST_INIT_NODUP;
+	struct string_list_item *item;
+	int pack_all = 0;
+	int ret;
+
+	struct option opts[] = {
+		OPT_BOOL(0, "all",   &pack_all, N_("pack everything")),
+		OPT_BIT(0, "prune", &pack_refs_opts.flags, N_("prune loose refs (default)"), PACK_REFS_PRUNE),
+		OPT_BIT(0, "auto", &pack_refs_opts.flags, N_("auto-pack refs as needed"), PACK_REFS_AUTO),
+		OPT_STRING_LIST(0, "include", pack_refs_opts.includes, N_("pattern"),
+			N_("references to include")),
+		OPT_STRING_LIST(0, "exclude", &option_excluded_refs, N_("pattern"),
+			N_("references to exclude")),
+		OPT_END(),
+	};
+	repo_config(repo, git_default_config, NULL);
+	if (parse_options(argc, argv, prefix, opts, usage_opts, 0))
+		usage_with_options(usage_opts, opts);
+
+	for_each_string_list_item(item, &option_excluded_refs)
+		add_ref_exclusion(pack_refs_opts.exclusions, item->string);
+
+	if (pack_all)
+		string_list_append(pack_refs_opts.includes, "*");
+
+	if (!pack_refs_opts.includes->nr)
+		string_list_append(pack_refs_opts.includes, "refs/tags/*");
+
+	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);
+	return ret;
+}
diff --git a/pack-refs.h b/pack-refs.h
new file mode 100644
index 0000000000..5de27e7da8
--- /dev/null
+++ b/pack-refs.h
@@ -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 */
-- 
2.34.1
Previous: Meet SoniNext: Patrick Steinhardt
Message 30 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.