@matheusbsbs / matheus-bagliano / commits / 3103e9848f
Fork 0

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 <me@ttaylorr.com> Helped-by: Junio C Hamano <gitster@pobox.com> Signed-off-by: Derrick Stolee <dstolee@microsoft.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Derrick Stolee committed Sep 17, 2020 at 18:11 UTC 3103e9848f7b7534eac14c0521d7b49b32466b70
1 file changed +42 -1
builtin/gc.c
+42 -1
@@ -728,6 +728,47 @@ static int maintenance_task_gc(struct maintenance_run_opts *opts)
728 return run_command(&child);
729 }
730
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 +
772 static int maintenance_run(int argc, const char **argv, const char *prefix)
773 {
774 struct maintenance_run_opts opts;
@@ -750,7 +791,7 @@ static int maintenance_run(int argc, const char **argv, const char *prefix)
791 if (argc != 0)
792 usage_with_options(builtin_maintenance_run_usage,
793 builtin_maintenance_run_options);
753 - return maintenance_task_gc(&opts);
794 + return maintenance_run_tasks(&opts);
795 }
796
797 static const char builtin_maintenance_usage[] = N_("git maintenance run [<options>]");