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

[PATCH 03/11] builtin/maintenance: centralize configuration of explicit tasks

From
Patrick Steinhardt <ps@pks.im>
Date
May 27, 2025, 14:04 UTC
Message-ID
<20250527-b4-pks-maintenance-ref-lock-race-v1-3-e1ceb2dea66e@pks.im>
In-Reply-To
<20250527-b4-pks-maintenance-ref-lock-race-v1-0-e1ceb2dea66e@pks.im>

Users of git-maintenance(1) can explicitly ask it to run specific tasks by passing the `--task=` command line option. This option can be passed multiple times, which causes us to execute tasks in the same order as the tasks have been provided by the user.

The order in which tasks are run is computed in `task_option_parse()`: every time we parse such a command line argument, we modify the global array of tasks by seting the selected index for that specific task. This has two downsides:

  - We modify global state, which makes it hard to follow the logic.
  - The configuration of tasks is split across multiple different
    functions, so it is not easy to figure out the different factors
    that play a role in selecting tasks.

Refactor the logic so that `task_option_parse()` does not modify global state anymore. Instead, this function now only collects the list of configured tasks. The logic to configure ordering of the respective tasks is then deferred to `initialize_task_config()`.

This refactoring solves the second problem, that the configuration of tasks is spread across multiple different locations. The first problem, that we modify global state, will be fixed in a subsequent commit.

Signed-off-by: Patrick Steinhardt <ps@pks.im>
---
 builtin/gc.c | 47 ++++++++++++++++++++++++-----------------------
 1 file changed, 24 insertions(+), 23 deletions(-)
diff --git a/builtin/gc.c b/builtin/gc.c
index 7adda8d2d0d..c4af9b11287 100644
--- a/builtin/gc.c
+++ b/builtin/gc.c
@@ -1690,15 +1690,22 @@ static void initialize_maintenance_strategy(void)
 	}
 }
 
-static void initialize_task_config(int schedule)
+static void initialize_task_config(const struct string_list *selected_tasks,
+				   int schedule)
 {
-	int i;
 	struct strbuf config_name = STRBUF_INIT;
 
+	for (size_t i = 0; i < TASK__COUNT; i++)
+		tasks[i].selected_order = -1;
+	for (size_t i = 0; i < selected_tasks->nr; i++) {
+		struct maintenance_task *task = selected_tasks->items[i].util;
+		task->selected_order = i;
+	}
+
 	if (schedule)
 		initialize_maintenance_strategy();
 
-	for (i = 0; i < TASK__COUNT; i++) {
+	for (size_t i = 0; i < TASK__COUNT; i++) {
 		int config_value;
 		char *config_str;
 
@@ -1722,33 +1729,28 @@ static void initialize_task_config(int schedule)
 	strbuf_release(&config_name);
 }
 
-static int task_option_parse(const struct option *opt UNUSED,
+static int task_option_parse(const struct option *opt,
 			     const char *arg, int unset)
 {
-	int i, num_selected = 0;
-	struct maintenance_task *task = NULL;
+	struct string_list *selected_tasks = opt->value;
+	size_t i;
 
 	BUG_ON_OPT_NEG(unset);
 
-	for (i = 0; i < TASK__COUNT; i++) {
-		if (tasks[i].selected_order >= 0)
-			num_selected++;
-		if (!strcasecmp(tasks[i].name, arg)) {
-			task = &tasks[i];
-		}
-	}
-
-	if (!task) {
+	for (i = 0; i < TASK__COUNT; i++)
+		if (!strcasecmp(tasks[i].name, arg))
+			break;
+	if (i >= TASK__COUNT) {
 		error(_("'%s' is not a valid task"), arg);
 		return 1;
 	}
 
-	if (task->selected_order >= 0) {
+	if (unsorted_string_list_has_string(selected_tasks, arg)) {
 		error(_("task '%s' cannot be selected multiple times"), arg);
 		return 1;
 	}
 
-	task->selected_order = num_selected + 1;
+	string_list_append(selected_tasks, arg)->util = &tasks[i];
 
 	return 0;
 }
@@ -1756,8 +1758,8 @@ static int task_option_parse(const struct option *opt UNUSED,
 static int maintenance_run(int argc, const char **argv, const char *prefix,
 			   struct repository *repo UNUSED)
 {
-	int i;
 	struct maintenance_run_opts opts = MAINTENANCE_RUN_OPTS_INIT;
+	struct string_list selected_tasks = STRING_LIST_INIT_DUP;
 	struct gc_config cfg = GC_CONFIG_INIT;
 	struct option builtin_maintenance_run_options[] = {
 		OPT_BOOL(0, "auto", &opts.auto_flag,
@@ -1769,7 +1771,7 @@ static int maintenance_run(int argc, const char **argv, const char *prefix,
 			     maintenance_opt_schedule),
 		OPT_BOOL(0, "quiet", &opts.quiet,
 			 N_("do not report progress or other information over stderr")),
-		OPT_CALLBACK_F(0, "task", NULL, N_("task"),
+		OPT_CALLBACK_F(0, "task", &selected_tasks, N_("task"),
 			N_("run a specific task"),
 			PARSE_OPT_NONEG, task_option_parse),
 		OPT_END()
@@ -1778,9 +1780,6 @@ static int maintenance_run(int argc, const char **argv, const char *prefix,
 
 	opts.quiet = !isatty(2);
 
-	for (i = 0; i < TASK__COUNT; i++)
-		tasks[i].selected_order = -1;
-
 	argc = parse_options(argc, argv, prefix,
 			     builtin_maintenance_run_options,
 			     builtin_maintenance_run_usage,
@@ -1790,13 +1789,15 @@ static int maintenance_run(int argc, const char **argv, const char *prefix,
 		die(_("use at most one of --auto and --schedule=<frequency>"));
 
 	gc_config(&cfg);
-	initialize_task_config(opts.schedule);
+	initialize_task_config(&selected_tasks, opts.schedule);
 
 	if (argc != 0)
 		usage_with_options(builtin_maintenance_run_usage,
 				   builtin_maintenance_run_options);
 
 	ret = maintenance_run_tasks(&opts, &cfg);
+
+	string_list_clear(&selected_tasks, 0);
 	gc_config_release(&cfg);
 	return ret;
 }
-- 
2.49.0.1266.g31b7d2e469.dirty
Previous: Patrick SteinhardtNext: Patrick Steinhardt
Message 4 of 71 in “builtin/maintenance: fix ref lock races when detaching”
  1. 00/11 builtin/maintenance: fix ref lock races when detachingPatrick Steinhardt, May 27, 2025
  2. 01/11 builtin/gc: use designated field initializers for maintenance tasksPatrick Steinhardt, May 27, 2025
  3. 02/11 builtin/gc: drop redundant local variablePatrick Steinhardt, May 27, 2025
  4. 03/11 builtin/maintenance: centralize configuration of explicit tasksPatrick Steinhardt, May 27, 2025
  5. 04/11 builtin/maintenance: mark "--task=" and "--schedule=" as incompatiblePatrick Steinhardt, May 27, 2025
  6. Ramsay JonesMay 27, 2025
  7. Patrick SteinhardtMay 28, 2025
  8. 05/11 builtin/maintenance: stop modifying global array of tasksPatrick Steinhardt, May 27, 2025
  9. 06/11 builtin/maintenance: extract function to run tasksPatrick Steinhardt, May 27, 2025
  10. 07/11 builtin/maintenance: fix typedef for function pointersPatrick Steinhardt, May 27, 2025
  11. 08/11 builtin/maintenance: let tasks do maintenance before and after detachPatrick Steinhardt, May 27, 2025
  12. Ramsay JonesMay 27, 2025
  13. Patrick SteinhardtMay 28, 2025
  14. 09/11 builtin/maintenance: fix locking race when packing refs and reflogsPatrick Steinhardt, May 27, 2025
  15. 11/11 builtin/maintenance: fix locking race when handling "gc" taskPatrick Steinhardt, May 27, 2025
  16. 10/11 builtin/gc: avoid global state in `gc_before_repack()`Patrick Steinhardt, May 27, 2025
  17. 00/12 builtin/maintenance: fix ref lock races when detachingPatrick Steinhardt, May 30, 2025
  18. 01/12 builtin/gc: use designated field initializers for maintenance tasksPatrick Steinhardt, May 30, 2025
  19. 02/12 builtin/gc: drop redundant local variablePatrick Steinhardt, May 30, 2025
  20. 04/12 builtin/maintenance: mark "--task=" and "--schedule=" as incompatiblePatrick Steinhardt, May 30, 2025
  21. 03/12 builtin/maintenance: centralize configuration of explicit tasksPatrick Steinhardt, May 30, 2025
  22. 05/12 builtin/maintenance: stop modifying global array of tasksPatrick Steinhardt, May 30, 2025
  23. 07/12 builtin/maintenance: fix typedef for function pointersPatrick Steinhardt, May 30, 2025
  24. 06/12 builtin/maintenance: extract function to run tasksPatrick Steinhardt, May 30, 2025
  25. 08/12 builtin/maintenance: let tasks do maintenance before and after detachPatrick Steinhardt, May 30, 2025
  26. 09/12 builtin/maintenance: fix locking race when packing refs and reflogsPatrick Steinhardt, May 30, 2025
  27. 10/12 usage: allow dying without writing an error messagePatrick Steinhardt, May 30, 2025
  28. 11/12 builtin/gc: avoid global state in `gc_before_repack()`Patrick Steinhardt, May 30, 2025
  29. 12/12 builtin/maintenance: fix locking race when handling "gc" taskPatrick Steinhardt, May 30, 2025
  30. 00/12 builtin/maintenance: fix ref lock races when detachingPatrick Steinhardt, Jun 2, 2025
  31. 01/12 builtin/gc: use designated field initializers for maintenance tasksPatrick Steinhardt, Jun 2, 2025
  32. Karthik NayakJun 2, 2025
  33. 02/12 builtin/gc: drop redundant local variablePatrick Steinhardt, Jun 2, 2025
  34. 03/12 builtin/maintenance: centralize configuration of explicit tasksPatrick Steinhardt, Jun 2, 2025
  35. 04/12 builtin/maintenance: mark "--task=" and "--schedule=" as incompatiblePatrick Steinhardt, Jun 2, 2025
  36. 05/12 builtin/maintenance: stop modifying global array of tasksPatrick Steinhardt, Jun 2, 2025
  37. 06/12 builtin/maintenance: extract function to run tasksPatrick Steinhardt, Jun 2, 2025
  38. 07/12 builtin/maintenance: fix typedef for function pointersPatrick Steinhardt, Jun 2, 2025
  39. 08/12 builtin/maintenance: split into foreground and background tasksPatrick Steinhardt, Jun 2, 2025
  40. 09/12 builtin/maintenance: fix locking race when packing refs and reflogsPatrick Steinhardt, Jun 2, 2025
  41. Kristoffer HaugsbakkJun 2, 2025
  42. Patrick SteinhardtJun 3, 2025
  43. Kristoffer HaugsbakkJun 3, 2025
  44. 10/12 usage: allow dying without writing an error messagePatrick Steinhardt, Jun 2, 2025
  45. Karthik NayakJun 3, 2025
  46. Kristoffer HaugsbakkJun 3, 2025
  47. Karthik NayakJun 3, 2025
  48. Patrick SteinhardtJun 3, 2025
  49. Kristoffer HaugsbakkJun 3, 2025
  50. Junio C HamanoJun 3, 2025
  51. Patrick SteinhardtJun 5, 2025
  52. 11/12 builtin/gc: avoid global state in `gc_before_repack()`Patrick Steinhardt, Jun 2, 2025
  53. 12/12 builtin/maintenance: fix locking race when handling "gc" taskPatrick Steinhardt, Jun 2, 2025
  54. 00/12 builtin/maintenance: fix ref lock races when detachingPatrick Steinhardt, Jun 3, 2025
  55. 01/12 builtin/gc: use designated field initializers for maintenance tasksPatrick Steinhardt, Jun 3, 2025
  56. 02/12 builtin/gc: drop redundant local variablePatrick Steinhardt, Jun 3, 2025
  57. 03/12 builtin/maintenance: centralize configuration of explicit tasksPatrick Steinhardt, Jun 3, 2025
  58. 05/12 builtin/maintenance: stop modifying global array of tasksPatrick Steinhardt, Jun 3, 2025
  59. 04/12 builtin/maintenance: mark "--task=" and "--schedule=" as incompatiblePatrick Steinhardt, Jun 3, 2025
  60. Derrick StoleeJun 5, 2025
  61. 06/12 builtin/maintenance: extract function to run tasksPatrick Steinhardt, Jun 3, 2025
  62. 07/12 builtin/maintenance: fix typedef for function pointersPatrick Steinhardt, Jun 3, 2025
  63. 08/12 builtin/maintenance: split into foreground and background tasksPatrick Steinhardt, Jun 3, 2025
  64. 09/12 builtin/maintenance: fix locking race with refs and reflogs tasksPatrick Steinhardt, Jun 3, 2025
  65. 11/12 builtin/gc: avoid global state in `gc_before_repack()`Patrick Steinhardt, Jun 3, 2025
  66. 10/12 usage: allow dying without writing an error messagePatrick Steinhardt, Jun 3, 2025
  67. 12/12 builtin/maintenance: fix locking race when handling "gc" taskPatrick Steinhardt, Jun 3, 2025
  68. Kristoffer HaugsbakkJun 3, 2025
  69. Ben KnobleJun 4, 2025
  70. Derrick StoleeJun 5, 2025
  71. Patrick SteinhardtJun 6, 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.