From: Derrick Stolee <dstolee@xxxxxxxxxxxxx> Users may want to run certain maintenance tasks only so often. Update the local config with a new 'maintenance.<task>.lastRun' config option that stores the timestamp just before running the maintenance task. I selected the timestamp before the task, as opposed to after the task, for a couple reasons: 1. The time the task takes to execute should not contribute to the interval between running the tasks. If a daily task takes 10 minutes to run, then every day the execution will drift by at least 10 minutes. 2. If the task fails for some unforseen reason, it would be good to indicate that we _attempted_ the task at a certain timestamp. This will avoid spamming a repository that is in a bad state. Signed-off-by: Derrick Stolee <dstolee@xxxxxxxxxxxxx> --- Documentation/config/maintenance.txt | 5 +++++ builtin/gc.c | 16 ++++++++++++++++ t/t7900-maintenance.sh | 10 ++++++++++ 3 files changed, 31 insertions(+) diff --git a/Documentation/config/maintenance.txt b/Documentation/config/maintenance.txt index 06db758172..8dd34169da 100644 --- a/Documentation/config/maintenance.txt +++ b/Documentation/config/maintenance.txt @@ -10,6 +10,11 @@ maintenance.<task>.enabled:: `--task` option exists. By default, only `maintenance.gc.enabled` is true. +maintenance.<task>.lastRun:: + This config value is automatically updated by Git when the task + `<task>` is run. It stores a timestamp representing the most-recent + run of the `<task>`. + maintenance.commit-graph.auto:: This integer config option controls how often the `commit-graph` task should be run as part of `git maintenance run --auto`. If zero, then diff --git a/builtin/gc.c b/builtin/gc.c index f8459df04c..fb6f231a5c 100644 --- a/builtin/gc.c +++ b/builtin/gc.c @@ -1212,6 +1212,20 @@ static int compare_tasks_by_selection(const void *a_, const void *b_) return b->selected_order - a->selected_order; } +static void update_last_run(struct maintenance_task *task) +{ + timestamp_t now = approxidate("now"); + struct strbuf config = STRBUF_INIT; + struct strbuf value = STRBUF_INIT; + strbuf_addf(&config, "maintenance.%s.lastrun", task->name); + strbuf_addf(&value, "%"PRItime"", now); + + git_config_set(config.buf, value.buf); + + strbuf_release(&config); + strbuf_release(&value); +} + static int maintenance_run_tasks(struct maintenance_run_opts *opts) { int i, found_selected = 0; @@ -1254,6 +1268,8 @@ static int maintenance_run_tasks(struct maintenance_run_opts *opts) !tasks[i].auto_condition())) continue; + update_last_run(&tasks[i]); + trace2_region_enter("maintenance", tasks[i].name, r); if (tasks[i].fn(opts)) { error(_("task '%s' failed"), tasks[i].name); diff --git a/t/t7900-maintenance.sh b/t/t7900-maintenance.sh index e0ba19e1ff..a985ce3674 100755 --- a/t/t7900-maintenance.sh +++ b/t/t7900-maintenance.sh @@ -264,4 +264,14 @@ test_expect_success 'maintenance.incremental-repack.auto' ' done ' +test_expect_success 'tasks update maintenance.<task>.lastRun' ' + git config --unset maintenance.commit-graph.lastrun && + GIT_TRACE2_EVENT="$(pwd)/run.txt" \ + GIT_TEST_DATE_NOW=1595000000 \ + git maintenance run --task=commit-graph 2>/dev/null && + test_subcommand git commit-graph write --split --reachable \ + --no-progress <run.txt && + test_cmp_config 1595000000 maintenance.commit-graph.lastrun +' + test_done -- gitgitgadget