Skip to content

Commit 3103e98

Browse files
derrickstoleegitster
authored andcommitted
maintenance: initialize task array
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 print an error message. Helped-by: Taylor Blau <[email protected]> Helped-by: Junio C Hamano <[email protected]> Signed-off-by: Derrick Stolee <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent a95ce12 commit 3103e98

File tree

1 file changed

+42
-1
lines changed

1 file changed

+42
-1
lines changed

builtin/gc.c

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -728,6 +728,47 @@ static int maintenance_task_gc(struct maintenance_run_opts *opts)
728728
return run_command(&child);
729729
}
730730

731+
typedef int maintenance_task_fn(struct maintenance_run_opts *opts);
732+
733+
struct maintenance_task {
734+
const char *name;
735+
maintenance_task_fn *fn;
736+
unsigned enabled:1;
737+
};
738+
739+
enum maintenance_task_label {
740+
TASK_GC,
741+
742+
/* Leave as final value */
743+
TASK__COUNT
744+
};
745+
746+
static struct maintenance_task tasks[] = {
747+
[TASK_GC] = {
748+
"gc",
749+
maintenance_task_gc,
750+
1,
751+
},
752+
};
753+
754+
static int maintenance_run_tasks(struct maintenance_run_opts *opts)
755+
{
756+
int i;
757+
int result = 0;
758+
759+
for (i = 0; i < TASK__COUNT; i++) {
760+
if (!tasks[i].enabled)
761+
continue;
762+
763+
if (tasks[i].fn(opts)) {
764+
error(_("task '%s' failed"), tasks[i].name);
765+
result = 1;
766+
}
767+
}
768+
769+
return result;
770+
}
771+
731772
static int maintenance_run(int argc, const char **argv, const char *prefix)
732773
{
733774
struct maintenance_run_opts opts;
@@ -750,7 +791,7 @@ static int maintenance_run(int argc, const char **argv, const char *prefix)
750791
if (argc != 0)
751792
usage_with_options(builtin_maintenance_run_usage,
752793
builtin_maintenance_run_options);
753-
return maintenance_task_gc(&opts);
794+
return maintenance_run_tasks(&opts);
754795
}
755796

756797
static const char builtin_maintenance_usage[] = N_("git maintenance run [<options>]");

0 commit comments

Comments
 (0)