Skip to content

Commit 1bb6bdb

Browse files
pks-tgitster
authored andcommitted
builtin/maintenance: centralize configuration of explicit tasks
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent bd19b94 commit 1bb6bdb

File tree

1 file changed

+24
-23
lines changed

1 file changed

+24
-23
lines changed

builtin/gc.c

Lines changed: 24 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1690,15 +1690,22 @@ static void initialize_maintenance_strategy(void)
16901690
}
16911691
}
16921692

1693-
static void initialize_task_config(int schedule)
1693+
static void initialize_task_config(const struct string_list *selected_tasks,
1694+
int schedule)
16941695
{
1695-
int i;
16961696
struct strbuf config_name = STRBUF_INIT;
16971697

1698+
for (size_t i = 0; i < TASK__COUNT; i++)
1699+
tasks[i].selected_order = -1;
1700+
for (size_t i = 0; i < selected_tasks->nr; i++) {
1701+
struct maintenance_task *task = selected_tasks->items[i].util;
1702+
task->selected_order = i;
1703+
}
1704+
16981705
if (schedule)
16991706
initialize_maintenance_strategy();
17001707

1701-
for (i = 0; i < TASK__COUNT; i++) {
1708+
for (size_t i = 0; i < TASK__COUNT; i++) {
17021709
int config_value;
17031710
char *config_str;
17041711

@@ -1722,42 +1729,37 @@ static void initialize_task_config(int schedule)
17221729
strbuf_release(&config_name);
17231730
}
17241731

1725-
static int task_option_parse(const struct option *opt UNUSED,
1732+
static int task_option_parse(const struct option *opt,
17261733
const char *arg, int unset)
17271734
{
1728-
int i, num_selected = 0;
1729-
struct maintenance_task *task = NULL;
1735+
struct string_list *selected_tasks = opt->value;
1736+
size_t i;
17301737

17311738
BUG_ON_OPT_NEG(unset);
17321739

1733-
for (i = 0; i < TASK__COUNT; i++) {
1734-
if (tasks[i].selected_order >= 0)
1735-
num_selected++;
1736-
if (!strcasecmp(tasks[i].name, arg)) {
1737-
task = &tasks[i];
1738-
}
1739-
}
1740-
1741-
if (!task) {
1740+
for (i = 0; i < TASK__COUNT; i++)
1741+
if (!strcasecmp(tasks[i].name, arg))
1742+
break;
1743+
if (i >= TASK__COUNT) {
17421744
error(_("'%s' is not a valid task"), arg);
17431745
return 1;
17441746
}
17451747

1746-
if (task->selected_order >= 0) {
1748+
if (unsorted_string_list_has_string(selected_tasks, arg)) {
17471749
error(_("task '%s' cannot be selected multiple times"), arg);
17481750
return 1;
17491751
}
17501752

1751-
task->selected_order = num_selected + 1;
1753+
string_list_append(selected_tasks, arg)->util = &tasks[i];
17521754

17531755
return 0;
17541756
}
17551757

17561758
static int maintenance_run(int argc, const char **argv, const char *prefix,
17571759
struct repository *repo UNUSED)
17581760
{
1759-
int i;
17601761
struct maintenance_run_opts opts = MAINTENANCE_RUN_OPTS_INIT;
1762+
struct string_list selected_tasks = STRING_LIST_INIT_DUP;
17611763
struct gc_config cfg = GC_CONFIG_INIT;
17621764
struct option builtin_maintenance_run_options[] = {
17631765
OPT_BOOL(0, "auto", &opts.auto_flag,
@@ -1769,7 +1771,7 @@ static int maintenance_run(int argc, const char **argv, const char *prefix,
17691771
maintenance_opt_schedule),
17701772
OPT_BOOL(0, "quiet", &opts.quiet,
17711773
N_("do not report progress or other information over stderr")),
1772-
OPT_CALLBACK_F(0, "task", NULL, N_("task"),
1774+
OPT_CALLBACK_F(0, "task", &selected_tasks, N_("task"),
17731775
N_("run a specific task"),
17741776
PARSE_OPT_NONEG, task_option_parse),
17751777
OPT_END()
@@ -1778,9 +1780,6 @@ static int maintenance_run(int argc, const char **argv, const char *prefix,
17781780

17791781
opts.quiet = !isatty(2);
17801782

1781-
for (i = 0; i < TASK__COUNT; i++)
1782-
tasks[i].selected_order = -1;
1783-
17841783
argc = parse_options(argc, argv, prefix,
17851784
builtin_maintenance_run_options,
17861785
builtin_maintenance_run_usage,
@@ -1790,13 +1789,15 @@ static int maintenance_run(int argc, const char **argv, const char *prefix,
17901789
die(_("use at most one of --auto and --schedule=<frequency>"));
17911790

17921791
gc_config(&cfg);
1793-
initialize_task_config(opts.schedule);
1792+
initialize_task_config(&selected_tasks, opts.schedule);
17941793

17951794
if (argc != 0)
17961795
usage_with_options(builtin_maintenance_run_usage,
17971796
builtin_maintenance_run_options);
17981797

17991798
ret = maintenance_run_tasks(&opts, &cfg);
1799+
1800+
string_list_clear(&selected_tasks, 0);
18001801
gc_config_release(&cfg);
18011802
return ret;
18021803
}

0 commit comments

Comments
 (0)