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

[GSoC][PATCH 1/5] builtin/pack-refs: factor out core logic into a helper

From
Meet Soni <meetsoni3017@gmail.com>
Date
Aug 26, 2025, 07:36 UTC
Message-ID
<20250826073645.1074397-2-meetsoni3017@gmail.com>
In-Reply-To
<20250826073645.1074397-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.

Introduce a new `pack-refs.h` header to define the public interface for this shared logic. It contains the declaration for a new helper function, `pack_refs_core()`, and a macro for the common usage options.

Move the option parsing and packing logic from `cmd_pack_refs()` into a new helper function named `pack_refs_core()`. This helper is made generic by accepting the command's usage string as a parameter.

The original `cmd_pack_refs()` is simplified to a thin wrapper that is only responsible for defining its specific usage array and calling the shared helper.

Mentored-by: Patrick Steinhardt <ps@pks.im>
Mentored-by: shejialuo <shejialuo@gmail.com>
Signed-off-by: Meet Soni <meetsoni3017@gmail.com>
---
 builtin/pack-refs.c | 31 ++++++++++++++++++++-----------
 pack-refs.h         | 22 ++++++++++++++++++++++
 2 files changed, 42 insertions(+), 11 deletions(-)
 create mode 100644 pack-refs.h
diff --git a/builtin/pack-refs.c b/builtin/pack-refs.c
index 5e28d0f9e8..7c4d7854c8 100644
--- a/builtin/pack-refs.c
+++ b/builtin/pack-refs.c
@@ -2,19 +2,16 @@
 #include "config.h"
 #include "environment.h"
 #include "gettext.h"
+#include "pack-refs.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
-};
-
-int cmd_pack_refs(int argc,
-		  const char **argv,
-		  const char *prefix,
-		  struct repository *repo)
+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;
@@ -39,8 +36,8 @@ int cmd_pack_refs(int argc,
 		OPT_END(),
 	};
 	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);
+	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);
@@ -58,3 +55,15 @@ int cmd_pack_refs(int argc,
 	string_list_clear(&option_excluded_refs, 0);
 	return ret;
 }
+
+int cmd_pack_refs(int argc,
+		  const char **argv,
+		  const char *prefix,
+		  struct repository *repo)
+{
+	static char const * const pack_refs_usage[] = {
+		N_("git pack-refs " PACK_REFS_OPTS),
+		NULL
+	};
+	return pack_refs_core(argc, argv, prefix, repo, pack_refs_usage);
+}
diff --git a/pack-refs.h b/pack-refs.h
new file mode 100644
index 0000000000..ba51d154e3
--- /dev/null
+++ b/pack-refs.h
@@ -0,0 +1,22 @@
+#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 2 of 17 in “Add refs optimize subcommand”
  1. Meet SoniAug 26, 2025
  2. [GSoC][PATCH 1/5] builtin/pack-refs: factor out core logic into a helperMeet Soni, Aug 26, 2025
  3. Patrick SteinhardtSep 2, 2025
  4. Meet SoniSep 3, 2025
  5. Patrick SteinhardtSep 3, 2025
  6. Junio C HamanoSep 3, 2025
  7. Patrick SteinhardtSep 3, 2025
  8. Junio C HamanoSep 3, 2025
  9. [GSoC][PATCH 2/5] doc: factor out common optionMeet Soni, Aug 26, 2025
  10. [GSoC][PATCH 3/5] builtin/refs: add optimize subcommandMeet Soni, Aug 26, 2025
  11. Patrick SteinhardtSep 2, 2025
  12. [GSoC][PATCH 4/5] t0601: refactor tests to be shareableMeet Soni, Aug 26, 2025
  13. [GSoC][PATCH 5/5] t: add test for git refs optimize subcommandMeet Soni, Aug 26, 2025
  14. shejialuoAug 26, 2025
  15. Meet SoniAug 31, 2025
  16. Patrick SteinhardtSep 2, 2025
  17. Meet SoniSep 3, 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.