diff mbox series

[v2,04/11] maintenance: initialize task array

Message ID 1db3b96280d9bf7908b7dc9fa13b80c445164a99.1597760589.git.gitgitgadget@gmail.com (mailing list archive)
State Superseded
Headers show
Series Maintenance I: Command, gc and commit-graph tasks | expand

Commit Message

Jean-Noël Avila via GitGitGadget Aug. 18, 2020, 2:23 p.m. UTC
From: Derrick Stolee <dstolee@microsoft.com>

In anticipation of implementing multiple maintenance tasks inside the
'maintenance' builtin, use a list of structs to describe the work to be
done.

The struct maintenance_task stores the name of the task (as given by a
future command-line argument) along with a function pointer to its
implementation and a boolean for whether the step is enabled.

A list these structs are initialized with the full list of implemented
tasks along with a default order. For now, this list only contains the
"gc" task. This task is also the only task enabled by default.

The run subcommand will return a nonzero exit code if any task fails.
However, it will attempt all tasks in its loop before returning with the
failure. Also each failed task will send an error message.

Helped-by: Taylor Blau <me@ttaylorr.com>
Helped-by: Junio C Hamano <gitster@pobox.com>
Signed-off-by: Derrick Stolee <dstolee@microsoft.com>
---
 builtin/gc.c | 38 +++++++++++++++++++++++++++++++++++++-
 1 file changed, 37 insertions(+), 1 deletion(-)

Comments

Jonathan Tan Aug. 18, 2020, 11:46 p.m. UTC | #1
> From: Derrick Stolee <dstolee@microsoft.com>
> 
> In anticipation of implementing multiple maintenance tasks inside the
> 'maintenance' builtin, use a list of structs to describe the work to be
> done.
> 
> The struct maintenance_task stores the name of the task (as given by a
> future command-line argument) along with a function pointer to its
> implementation and a boolean for whether the step is enabled.
> 
> A list these structs are initialized with the full list of implemented
> tasks along with a default order. For now, this list only contains the
> "gc" task. This task is also the only task enabled by default.
> 
> The run subcommand will return a nonzero exit code if any task fails.
> However, it will attempt all tasks in its loop before returning with the
> failure. Also each failed task will send an error message.

Better to say "will print an error message", I think.

Other than that, patches 1-4 look good to me.
diff mbox series

Patch

diff --git a/builtin/gc.c b/builtin/gc.c
index b75ddb780b..946d871d54 100644
--- a/builtin/gc.c
+++ b/builtin/gc.c
@@ -728,9 +728,45 @@  static int maintenance_task_gc(struct maintenance_opts *opts)
 	return run_command(&child);
 }
 
+typedef int maintenance_task_fn(struct maintenance_opts *opts);
+
+struct maintenance_task {
+	const char *name;
+	maintenance_task_fn *fn;
+	unsigned enabled:1;
+};
+
+enum maintenance_task_label {
+	TASK_GC,
+
+	/* Leave as final value */
+	TASK__COUNT
+};
+
+static struct maintenance_task tasks[] = {
+	[TASK_GC] = {
+		"gc",
+		maintenance_task_gc,
+		1,
+	},
+};
+
 static int maintenance_run(struct maintenance_opts *opts)
 {
-	return maintenance_task_gc(opts);
+	int i;
+	int result = 0;
+
+	for (i = 0; i < TASK__COUNT; i++) {
+		if (!tasks[i].enabled)
+			continue;
+
+		if (tasks[i].fn(opts)) {
+			error(_("task '%s' failed"), tasks[i].name);
+			result = 1;
+		}
+	}
+
+	return result;
 }
 
 int cmd_maintenance(int argc, const char **argv, const char *prefix)