@@ -73,6 +73,10 @@ OPTIONS
--quiet::
Do not report progress or other information over `stderr`.
+--task=<task>::
+ If this option is specified one or more times, then only run the
+ specified tasks in the specified order.
+
GIT
---
Part of the linkgit:git[1] suite
@@ -708,6 +708,7 @@ static const char * const builtin_maintenance_usage[] = {
static struct maintenance_opts {
int auto_flag;
int quiet;
+ int tasks_selected;
} opts;
static int run_write_commit_graph(void)
@@ -789,7 +790,9 @@ typedef int maintenance_task_fn(void);
struct maintenance_task {
const char *name;
maintenance_task_fn *fn;
- unsigned enabled:1;
+ unsigned enabled:1,
+ selected:1;
+ int selected_order;
};
enum maintenance_task_label {
@@ -812,13 +815,29 @@ static struct maintenance_task tasks[] = {
},
};
+static int compare_tasks_by_selection(const void *a_, const void *b_)
+{
+ const struct maintenance_task *a, *b;
+
+ a = (const struct maintenance_task *)&a_;
+ b = (const struct maintenance_task *)&b_;
+
+ return b->selected_order - a->selected_order;
+}
+
static int maintenance_run(void)
{
int i;
int result = 0;
+ if (opts.tasks_selected)
+ QSORT(tasks, TASK__COUNT, compare_tasks_by_selection);
+
for (i = 0; i < TASK__COUNT; i++) {
- if (!tasks[i].enabled)
+ if (opts.tasks_selected && !tasks[i].selected)
+ continue;
+
+ if (!opts.tasks_selected && !tasks[i].enabled)
continue;
if (tasks[i].fn()) {
@@ -830,6 +849,39 @@ static int maintenance_run(void)
return result;
}
+static int task_option_parse(const struct option *opt,
+ const char *arg, int unset)
+{
+ int i;
+ struct maintenance_task *task = NULL;
+
+ BUG_ON_OPT_NEG(unset);
+
+ opts.tasks_selected++;
+
+ for (i = 0; i < TASK__COUNT; i++) {
+ if (!strcasecmp(tasks[i].name, arg)) {
+ task = &tasks[i];
+ break;
+ }
+ }
+
+ if (!task) {
+ error(_("'%s' is not a valid task"), arg);
+ return 1;
+ }
+
+ if (task->selected) {
+ error(_("task '%s' cannot be selected multiple times"), arg);
+ return 1;
+ }
+
+ task->selected = 1;
+ task->selected_order = opts.tasks_selected;
+
+ return 0;
+}
+
int cmd_maintenance(int argc, const char **argv, const char *prefix)
{
static struct option builtin_maintenance_options[] = {
@@ -837,6 +889,9 @@ int cmd_maintenance(int argc, const char **argv, const char *prefix)
N_("run tasks based on the state of the repository")),
OPT_BOOL(0, "quiet", &opts.quiet,
N_("do not report progress or other information over stderr")),
+ OPT_CALLBACK_F(0, "task", NULL, N_("task"),
+ N_("run a specific task"),
+ PARSE_OPT_NONEG, task_option_parse),
OPT_END()
};
@@ -20,4 +20,27 @@ test_expect_success 'run [--auto|--quiet]' '
grep ",\"gc\",\"--quiet\"" run-quiet.txt
'
+test_expect_success 'run --task=<task>' '
+ GIT_TRACE2_EVENT="$(pwd)/run-commit-graph.txt" git maintenance run --task=commit-graph &&
+ GIT_TRACE2_EVENT="$(pwd)/run-gc.txt" git maintenance run --task=gc &&
+ GIT_TRACE2_EVENT="$(pwd)/run-commit-graph.txt" git maintenance run --task=commit-graph &&
+ GIT_TRACE2_EVENT="$(pwd)/run-both.txt" git maintenance run --task=commit-graph --task=gc &&
+ ! grep ",\"gc\"" run-commit-graph.txt &&
+ grep ",\"gc\"" run-gc.txt &&
+ grep ",\"gc\"" run-both.txt &&
+ grep ",\"commit-graph\",\"write\"" run-commit-graph.txt &&
+ ! grep ",\"commit-graph\",\"write\"" run-gc.txt &&
+ grep ",\"commit-graph\",\"write\"" run-both.txt
+'
+
+test_expect_success 'run --task=bogus' '
+ test_must_fail git maintenance run --task=bogus 2>err &&
+ test_i18ngrep "is not a valid task" err
+'
+
+test_expect_success 'run --task duplicate' '
+ test_must_fail git maintenance run --task=gc --task=gc 2>err &&
+ test_i18ngrep "cannot be selected multiple times" err
+'
+
test_done