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

[PATCH 6/8] fetch: move option related variables into main function

From
Patrick Steinhardt <ps@pks.im>
Date
Apr 19, 2023, 12:31 UTC
Message-ID
<640a8840e1b87ec8ea985dd2299e1bab584d516d.1681906949.git.ps@pks.im>
In-Reply-To
<cover.1681906948.git.ps@pks.im>

The options of git-fetch(1) which we pass to `parse_options()` are declared globally in `builtin/fetch.c`. This means we're forced to use global variables for all the options, which is more likely to cause confusion than explicitly passing state around.

Refactor the code to move the options into `cmd_fetch()`. Move variables that were previously forced to be declared globally and which are only used by `cmd_fetch()` into function-local scope.

Signed-off-by: Patrick Steinhardt <ps@pks.im>
---
 builtin/fetch.c | 194 ++++++++++++++++++++++++------------------------
 1 file changed, 98 insertions(+), 96 deletions(-)
diff --git a/builtin/fetch.c b/builtin/fetch.c
index bcc156a9ce..81581b0033 100644
--- a/builtin/fetch.c
+++ b/builtin/fetch.c
@@ -79,9 +79,8 @@ static int all, append, dry_run, force, keep, multiple, update_head_ok;
 static int write_fetch_head = 1;
 static int verbosity, deepen_relative, set_upstream, refetch;
 static int progress = -1;
-static int enable_auto_gc = 1;
-static int tags = TAGS_DEFAULT, unshallow, update_shallow, deepen;
-static int max_jobs = -1, submodule_fetch_jobs_config = -1;
+static int tags = TAGS_DEFAULT, update_shallow, deepen;
+static int submodule_fetch_jobs_config = -1;
 static int fetch_parallel_config = 1;
 static int atomic_fetch;
 static enum transport_family family;
@@ -92,17 +91,11 @@ static struct string_list deepen_not = STRING_LIST_INIT_NODUP;
 static struct strbuf default_rla = STRBUF_INIT;
 static struct transport *gtransport;
 static struct transport *gsecondary;
-static const char *submodule_prefix = "";
 static int recurse_submodules = RECURSE_SUBMODULES_DEFAULT;
-static int recurse_submodules_cli = RECURSE_SUBMODULES_DEFAULT;
-static int recurse_submodules_default = RECURSE_SUBMODULES_ON_DEMAND;
 static struct refspec refmap = REFSPEC_INIT_FETCH;
 static struct list_objects_filter_options filter_options = LIST_OBJECTS_FILTER_INIT;
 static struct string_list server_options = STRING_LIST_INIT_DUP;
 static struct string_list negotiation_tip = STRING_LIST_INIT_NODUP;
-static int fetch_write_commit_graph = -1;
-static int stdin_refspecs = 0;
-static int negotiate_only;
 
 static int git_fetch_config(const char *k, const char *v, void *cb)
 {
@@ -160,92 +153,6 @@ static int parse_refmap_arg(const struct option *opt, const char *arg, int unset
 	return 0;
 }
 
-static struct option builtin_fetch_options[] = {
-	OPT__VERBOSITY(&verbosity),
-	OPT_BOOL(0, "all", &all,
-		 N_("fetch from all remotes")),
-	OPT_BOOL(0, "set-upstream", &set_upstream,
-		 N_("set upstream for git pull/fetch")),
-	OPT_BOOL('a', "append", &append,
-		 N_("append to .git/FETCH_HEAD instead of overwriting")),
-	OPT_BOOL(0, "atomic", &atomic_fetch,
-		 N_("use atomic transaction to update references")),
-	OPT_STRING(0, "upload-pack", &upload_pack, N_("path"),
-		   N_("path to upload pack on remote end")),
-	OPT__FORCE(&force, N_("force overwrite of local reference"), 0),
-	OPT_BOOL('m', "multiple", &multiple,
-		 N_("fetch from multiple remotes")),
-	OPT_SET_INT('t', "tags", &tags,
-		    N_("fetch all tags and associated objects"), TAGS_SET),
-	OPT_SET_INT('n', NULL, &tags,
-		    N_("do not fetch all tags (--no-tags)"), TAGS_UNSET),
-	OPT_INTEGER('j', "jobs", &max_jobs,
-		    N_("number of submodules fetched in parallel")),
-	OPT_BOOL(0, "prefetch", &prefetch,
-		 N_("modify the refspec to place all refs within refs/prefetch/")),
-	OPT_BOOL('p', "prune", &prune,
-		 N_("prune remote-tracking branches no longer on remote")),
-	OPT_BOOL('P', "prune-tags", &prune_tags,
-		 N_("prune local tags no longer on remote and clobber changed tags")),
-	OPT_CALLBACK_F(0, "recurse-submodules", &recurse_submodules_cli, N_("on-demand"),
-		    N_("control recursive fetching of submodules"),
-		    PARSE_OPT_OPTARG, option_fetch_parse_recurse_submodules),
-	OPT_BOOL(0, "dry-run", &dry_run,
-		 N_("dry run")),
-	OPT_BOOL(0, "write-fetch-head", &write_fetch_head,
-		 N_("write fetched references to the FETCH_HEAD file")),
-	OPT_BOOL('k', "keep", &keep, N_("keep downloaded pack")),
-	OPT_BOOL('u', "update-head-ok", &update_head_ok,
-		    N_("allow updating of HEAD ref")),
-	OPT_BOOL(0, "progress", &progress, N_("force progress reporting")),
-	OPT_STRING(0, "depth", &depth, N_("depth"),
-		   N_("deepen history of shallow clone")),
-	OPT_STRING(0, "shallow-since", &deepen_since, N_("time"),
-		   N_("deepen history of shallow repository based on time")),
-	OPT_STRING_LIST(0, "shallow-exclude", &deepen_not, N_("revision"),
-			N_("deepen history of shallow clone, excluding rev")),
-	OPT_INTEGER(0, "deepen", &deepen_relative,
-		    N_("deepen history of shallow clone")),
-	OPT_SET_INT_F(0, "unshallow", &unshallow,
-		      N_("convert to a complete repository"),
-		      1, PARSE_OPT_NONEG),
-	OPT_SET_INT_F(0, "refetch", &refetch,
-		      N_("re-fetch without negotiating common commits"),
-		      1, PARSE_OPT_NONEG),
-	{ OPTION_STRING, 0, "submodule-prefix", &submodule_prefix, N_("dir"),
-		   N_("prepend this to submodule path output"), PARSE_OPT_HIDDEN },
-	OPT_CALLBACK_F(0, "recurse-submodules-default",
-		   &recurse_submodules_default, N_("on-demand"),
-		   N_("default for recursive fetching of submodules "
-		      "(lower priority than config files)"),
-		   PARSE_OPT_HIDDEN, option_fetch_parse_recurse_submodules),
-	OPT_BOOL(0, "update-shallow", &update_shallow,
-		 N_("accept refs that update .git/shallow")),
-	OPT_CALLBACK_F(0, "refmap", NULL, N_("refmap"),
-		       N_("specify fetch refmap"), PARSE_OPT_NONEG, parse_refmap_arg),
-	OPT_STRING_LIST('o', "server-option", &server_options, N_("server-specific"), N_("option to transmit")),
-	OPT_SET_INT('4', "ipv4", &family, N_("use IPv4 addresses only"),
-			TRANSPORT_FAMILY_IPV4),
-	OPT_SET_INT('6', "ipv6", &family, N_("use IPv6 addresses only"),
-			TRANSPORT_FAMILY_IPV6),
-	OPT_STRING_LIST(0, "negotiation-tip", &negotiation_tip, N_("revision"),
-			N_("report that we have only objects reachable from this object")),
-	OPT_BOOL(0, "negotiate-only", &negotiate_only,
-		 N_("do not fetch a packfile; instead, print ancestors of negotiation tips")),
-	OPT_PARSE_LIST_OBJECTS_FILTER(&filter_options),
-	OPT_BOOL(0, "auto-maintenance", &enable_auto_gc,
-		 N_("run 'maintenance --auto' after fetching")),
-	OPT_BOOL(0, "auto-gc", &enable_auto_gc,
-		 N_("run 'maintenance --auto' after fetching")),
-	OPT_BOOL(0, "show-forced-updates", &fetch_show_forced_updates,
-		 N_("check for forced-updates on all updated branches")),
-	OPT_BOOL(0, "write-commit-graph", &fetch_write_commit_graph,
-		 N_("write the commit-graph after fetching")),
-	OPT_BOOL(0, "stdin", &stdin_refspecs,
-		 N_("accept refspecs from stdin")),
-	OPT_END()
-};
-
 static void unlock_pack(unsigned int flags)
 {
 	if (gtransport)
@@ -2145,13 +2052,108 @@ static int fetch_one(struct remote *remote, int argc, const char **argv,
 
 int cmd_fetch(int argc, const char **argv, const char *prefix)
 {
-	int i;
 	const char *bundle_uri;
+	const char *submodule_prefix = "";
 	enum display_format display_format = DISPLAY_FORMAT_UNKNOWN;
 	struct string_list list = STRING_LIST_INIT_DUP;
 	struct remote *remote = NULL;
 	int result = 0;
 	int prune_tags_ok = 1;
+	int enable_auto_gc = 1;
+	int unshallow = 0;
+	int max_jobs = -1;
+	int recurse_submodules_cli = RECURSE_SUBMODULES_DEFAULT;
+	int recurse_submodules_default = RECURSE_SUBMODULES_ON_DEMAND;
+	int fetch_write_commit_graph = -1;
+	int stdin_refspecs = 0;
+	int negotiate_only = 0;
+	int i;
+
+	struct option builtin_fetch_options[] = {
+		OPT__VERBOSITY(&verbosity),
+		OPT_BOOL(0, "all", &all,
+			 N_("fetch from all remotes")),
+		OPT_BOOL(0, "set-upstream", &set_upstream,
+			 N_("set upstream for git pull/fetch")),
+		OPT_BOOL('a', "append", &append,
+			 N_("append to .git/FETCH_HEAD instead of overwriting")),
+		OPT_BOOL(0, "atomic", &atomic_fetch,
+			 N_("use atomic transaction to update references")),
+		OPT_STRING(0, "upload-pack", &upload_pack, N_("path"),
+			   N_("path to upload pack on remote end")),
+		OPT__FORCE(&force, N_("force overwrite of local reference"), 0),
+		OPT_BOOL('m', "multiple", &multiple,
+			 N_("fetch from multiple remotes")),
+		OPT_SET_INT('t', "tags", &tags,
+			    N_("fetch all tags and associated objects"), TAGS_SET),
+		OPT_SET_INT('n', NULL, &tags,
+			    N_("do not fetch all tags (--no-tags)"), TAGS_UNSET),
+		OPT_INTEGER('j', "jobs", &max_jobs,
+			    N_("number of submodules fetched in parallel")),
+		OPT_BOOL(0, "prefetch", &prefetch,
+			 N_("modify the refspec to place all refs within refs/prefetch/")),
+		OPT_BOOL('p', "prune", &prune,
+			 N_("prune remote-tracking branches no longer on remote")),
+		OPT_BOOL('P', "prune-tags", &prune_tags,
+			 N_("prune local tags no longer on remote and clobber changed tags")),
+		OPT_CALLBACK_F(0, "recurse-submodules", &recurse_submodules_cli, N_("on-demand"),
+			    N_("control recursive fetching of submodules"),
+			    PARSE_OPT_OPTARG, option_fetch_parse_recurse_submodules),
+		OPT_BOOL(0, "dry-run", &dry_run,
+			 N_("dry run")),
+		OPT_BOOL(0, "write-fetch-head", &write_fetch_head,
+			 N_("write fetched references to the FETCH_HEAD file")),
+		OPT_BOOL('k', "keep", &keep, N_("keep downloaded pack")),
+		OPT_BOOL('u', "update-head-ok", &update_head_ok,
+			    N_("allow updating of HEAD ref")),
+		OPT_BOOL(0, "progress", &progress, N_("force progress reporting")),
+		OPT_STRING(0, "depth", &depth, N_("depth"),
+			   N_("deepen history of shallow clone")),
+		OPT_STRING(0, "shallow-since", &deepen_since, N_("time"),
+			   N_("deepen history of shallow repository based on time")),
+		OPT_STRING_LIST(0, "shallow-exclude", &deepen_not, N_("revision"),
+				N_("deepen history of shallow clone, excluding rev")),
+		OPT_INTEGER(0, "deepen", &deepen_relative,
+			    N_("deepen history of shallow clone")),
+		OPT_SET_INT_F(0, "unshallow", &unshallow,
+			      N_("convert to a complete repository"),
+			      1, PARSE_OPT_NONEG),
+		OPT_SET_INT_F(0, "refetch", &refetch,
+			      N_("re-fetch without negotiating common commits"),
+			      1, PARSE_OPT_NONEG),
+		{ OPTION_STRING, 0, "submodule-prefix", &submodule_prefix, N_("dir"),
+			   N_("prepend this to submodule path output"), PARSE_OPT_HIDDEN },
+		OPT_CALLBACK_F(0, "recurse-submodules-default",
+			   &recurse_submodules_default, N_("on-demand"),
+			   N_("default for recursive fetching of submodules "
+			      "(lower priority than config files)"),
+			   PARSE_OPT_HIDDEN, option_fetch_parse_recurse_submodules),
+		OPT_BOOL(0, "update-shallow", &update_shallow,
+			 N_("accept refs that update .git/shallow")),
+		OPT_CALLBACK_F(0, "refmap", NULL, N_("refmap"),
+			       N_("specify fetch refmap"), PARSE_OPT_NONEG, parse_refmap_arg),
+		OPT_STRING_LIST('o', "server-option", &server_options, N_("server-specific"), N_("option to transmit")),
+		OPT_SET_INT('4', "ipv4", &family, N_("use IPv4 addresses only"),
+				TRANSPORT_FAMILY_IPV4),
+		OPT_SET_INT('6', "ipv6", &family, N_("use IPv6 addresses only"),
+				TRANSPORT_FAMILY_IPV6),
+		OPT_STRING_LIST(0, "negotiation-tip", &negotiation_tip, N_("revision"),
+				N_("report that we have only objects reachable from this object")),
+		OPT_BOOL(0, "negotiate-only", &negotiate_only,
+			 N_("do not fetch a packfile; instead, print ancestors of negotiation tips")),
+		OPT_PARSE_LIST_OBJECTS_FILTER(&filter_options),
+		OPT_BOOL(0, "auto-maintenance", &enable_auto_gc,
+			 N_("run 'maintenance --auto' after fetching")),
+		OPT_BOOL(0, "auto-gc", &enable_auto_gc,
+			 N_("run 'maintenance --auto' after fetching")),
+		OPT_BOOL(0, "show-forced-updates", &fetch_show_forced_updates,
+			 N_("check for forced-updates on all updated branches")),
+		OPT_BOOL(0, "write-commit-graph", &fetch_write_commit_graph,
+			 N_("write the commit-graph after fetching")),
+		OPT_BOOL(0, "stdin", &stdin_refspecs,
+			 N_("accept refspecs from stdin")),
+		OPT_END()
+	};
 
 	packet_trace_identity("fetch");
 
-- 
2.40.0
Previous: Patrick SteinhardtNext: Patrick Steinhardt
Message 13 of 120 in “fetch: introduce machine-parseable output”
  1. 0/8 fetch: introduce machine-parseable outputPatrick Steinhardt, Apr 19, 2023
  2. 1/8 fetch: split out tests for output formatPatrick Steinhardt, Apr 19, 2023
  3. 2/8 fetch: add a test to exercise invalid output formatsPatrick Steinhardt, Apr 19, 2023
  4. 3/8 fetch: fix missing from-reference when fetching HEAD:fooPatrick Steinhardt, Apr 19, 2023
  5. Jacob KellerApr 26, 2023
  6. Patrick SteinhardtApr 27, 2023
  7. Jacob KellerApr 26, 2023
  8. Patrick SteinhardtApr 27, 2023
  9. Glen ChooApr 26, 2023
  10. Patrick SteinhardtApr 27, 2023
  11. 4/8 fetch: introduce `display_format` enumPatrick Steinhardt, Apr 19, 2023
  12. 5/8 fetch: move display format parsing into main functionPatrick Steinhardt, Apr 19, 2023
  13. 6/8 fetch: move option related variables into main functionPatrick Steinhardt, Apr 19, 2023
  14. 7/8 fetch: introduce new `--output-format` optionPatrick Steinhardt, Apr 19, 2023
  15. Glen ChooApr 26, 2023
  16. Patrick SteinhardtApr 27, 2023
  17. 8/8 fetch: introduce machine-parseable "porcelain" output formatPatrick Steinhardt, Apr 19, 2023
  18. Glen ChooApr 26, 2023
  19. Patrick SteinhardtApr 27, 2023
  20. Glen ChooApr 27, 2023
  21. Patrick SteinhardtApr 28, 2023
  22. Glen ChooApr 28, 2023
  23. Felipe ContrerasMay 2, 2023
  24. Felipe ContrerasApr 24, 2023
  25. Patrick SteinhardtApr 25, 2023
  26. Jacob KellerApr 26, 2023
  27. Junio C HamanoApr 26, 2023
  28. Jacob KellerApr 26, 2023
  29. Patrick SteinhardtApr 27, 2023
  30. Jacob KellerApr 27, 2023
  31. Glen ChooApr 27, 2023
  32. Junio C HamanoApr 26, 2023
  33. Glen ChooApr 26, 2023
  34. Glen ChooApr 26, 2023
  35. Jacob KellerApr 26, 2023
  36. 0/8 fetch: introduce machine-parseable outputPatrick Steinhardt, Apr 27, 2023
  37. 1/8 fetch: split out tests for output formatPatrick Steinhardt, Apr 27, 2023
  38. SZEDER GáborApr 29, 2023
  39. Patrick SteinhardtMay 3, 2023
  40. 2/8 fetch: add a test to exercise invalid output formatsPatrick Steinhardt, Apr 27, 2023
  41. 3/8 fetch: fix missing from-reference when fetching HEAD:fooPatrick Steinhardt, Apr 27, 2023
  42. Glen ChooApr 27, 2023
  43. Jacob KellerApr 27, 2023
  44. 4/8 fetch: introduce `display_format` enumPatrick Steinhardt, Apr 27, 2023
  45. 5/8 fetch: move display format parsing into main functionPatrick Steinhardt, Apr 27, 2023
  46. 6/8 fetch: move option related variables into main functionPatrick Steinhardt, Apr 27, 2023
  47. Junio C HamanoApr 27, 2023
  48. 7/8 fetch: introduce new `--output-format` optionPatrick Steinhardt, Apr 27, 2023
  49. Junio C HamanoApr 27, 2023
  50. Glen ChooApr 28, 2023
  51. Patrick SteinhardtMay 3, 2023
  52. Glen ChooApr 28, 2023
  53. Patrick SteinhardtMay 3, 2023
  54. Patrick SteinhardtMay 3, 2023
  55. 8/8 fetch: introduce machine-parseable "porcelain" output formatPatrick Steinhardt, Apr 27, 2023
  56. Jacob KellerApr 27, 2023
  57. Glen ChooApr 28, 2023
  58. 0/8 fetch: introduce machine-parseable outputPatrick Steinhardt, May 3, 2023
  59. 1/8 fetch: fix `--no-recurse-submodules` with multi-remote fetchesPatrick Steinhardt, May 3, 2023
  60. Glen ChooMay 8, 2023
  61. Patrick SteinhardtMay 9, 2023
  62. 2/8 fetch: split out tests for output formatPatrick Steinhardt, May 3, 2023
  63. 3/8 fetch: add a test to exercise invalid output formatsPatrick Steinhardt, May 3, 2023
  64. 4/8 fetch: fix missing from-reference when fetching HEAD:fooPatrick Steinhardt, May 3, 2023
  65. 5/8 fetch: introduce `display_format` enumPatrick Steinhardt, May 3, 2023
  66. 6/8 fetch: move display format parsing into main functionPatrick Steinhardt, May 3, 2023
  67. 7/8 fetch: move option related variables into main functionPatrick Steinhardt, May 3, 2023
  68. 8/8 fetch: introduce machine-parseable "porcelain" output formatPatrick Steinhardt, May 3, 2023
  69. Glen ChooMay 8, 2023
  70. Patrick SteinhardtMay 9, 2023
  71. Glen ChooMay 9, 2023
  72. Junio C HamanoMay 3, 2023
  73. Junio C HamanoMay 3, 2023
  74. Patrick SteinhardtMay 4, 2023
  75. Glen ChooMay 9, 2023
  76. Patrick SteinhardtMay 9, 2023
  77. 0/8 fetch: introduce machine-parseable outputPatrick Steinhardt, May 9, 2023
  78. 1/8 fetch: fix `--no-recurse-submodules` with multi-remote fetchesPatrick Steinhardt, May 9, 2023
  79. Junio C HamanoMay 9, 2023
  80. Glen ChooMay 9, 2023
  81. Patrick SteinhardtMay 10, 2023
  82. 2/8 fetch: split out tests for output formatPatrick Steinhardt, May 9, 2023
  83. 3/8 fetch: add a test to exercise invalid output formatsPatrick Steinhardt, May 9, 2023
  84. Junio C HamanoMay 9, 2023
  85. Patrick SteinhardtMay 10, 2023
  86. 4/8 fetch: fix missing from-reference when fetching HEAD:fooPatrick Steinhardt, May 9, 2023
  87. Junio C HamanoMay 9, 2023
  88. Patrick SteinhardtMay 10, 2023
  89. 5/8 fetch: introduce `display_format` enumPatrick Steinhardt, May 9, 2023
  90. Junio C HamanoMay 9, 2023
  91. Patrick SteinhardtMay 10, 2023
  92. 6/8 fetch: move display format parsing into main functionPatrick Steinhardt, May 9, 2023
  93. Junio C HamanoMay 9, 2023
  94. Glen ChooMay 9, 2023
  95. Patrick SteinhardtMay 10, 2023
  96. 7/8 fetch: move option related variables into main functionPatrick Steinhardt, May 9, 2023
  97. 8/8 fetch: introduce machine-parseable "porcelain" output formatPatrick Steinhardt, May 9, 2023
  98. Junio C HamanoMay 9, 2023
  99. Patrick SteinhardtMay 10, 2023
  100. 0/9 fetch: introduce machine-parseable outputPatrick Steinhardt, May 10, 2023
  101. 1/9 fetch: fix `--no-recurse-submodules` with multi-remote fetchesPatrick Steinhardt, May 10, 2023
  102. 2/9 fetch: split out tests for output formatPatrick Steinhardt, May 10, 2023
  103. 3/9 fetch: add a test to exercise invalid output formatsPatrick Steinhardt, May 10, 2023
  104. 4/9 fetch: print left-hand side when fetching HEAD:fooPatrick Steinhardt, May 10, 2023
  105. Glen ChooMay 12, 2023
  106. Jeff KingMay 13, 2023
  107. Patrick SteinhardtMay 15, 2023
  108. 5/9 fetch: refactor calculation of the display table widthPatrick Steinhardt, May 10, 2023
  109. Glen ChooMay 12, 2023
  110. 6/9 fetch: introduce `display_format` enumPatrick Steinhardt, May 10, 2023
  111. 7/9 fetch: lift up parsing of "fetch.output" config variablePatrick Steinhardt, May 10, 2023
  112. 8/9 fetch: move option related variables into main functionPatrick Steinhardt, May 10, 2023
  113. 9/9 fetch: introduce machine-parseable "porcelain" output formatPatrick Steinhardt, May 10, 2023
  114. Glen ChooMay 12, 2023
  115. Junio C HamanoMay 10, 2023
  116. Patrick SteinhardtMay 11, 2023
  117. Junio C HamanoMay 11, 2023
  118. Felipe ContrerasMay 11, 2023
  119. Glen ChooMay 12, 2023
  120. Patrick SteinhardtMay 12, 2023

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.