Skip to content

Commit ce7f30d

Browse files
derrickstoleegitster
authored andcommitted
maintenance: use default schedule if not configured
The 'git maintenance (register|start)' subcommands add the current repository to the global Git config so maintenance will operate on that repository. It does not specify what maintenance should occur or how often. If a user sets any 'maintenance.<task>.schedule' config value, then they have chosen a specific schedule for themselves and Git should respect that when running 'git maintenance run --schedule=<frequency>'. To make this process extremely simple for users, assume a default schedule when no 'maintenance.<task>.schedule' or '...enabled' config settings are concretely set. This is only an in-process assumption, so future versions of Git could adjust this expected schedule. Helped-by: Martin Ågren <[email protected]> Signed-off-by: Derrick Stolee <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 2fec604 commit ce7f30d

File tree

3 files changed

+80
-4
lines changed

3 files changed

+80
-4
lines changed

Documentation/git-maintenance.txt

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,21 @@ register::
3737
`maintenance.<task>.schedule`. The tasks that are enabled are safe
3838
for running in the background without disrupting foreground
3939
processes.
40+
+
41+
If your repository has no `maintenance.<task>.schedule` configuration
42+
values set, then Git will use a recommended default schedule that performs
43+
background maintenance that will not interrupt foreground commands. The
44+
default schedule is as follows:
45+
+
46+
* `gc`: disabled.
47+
* `commit-graph`: hourly.
48+
* `prefetch`: hourly.
49+
* `loose-objects`: daily.
50+
* `incremental-repack`: daily.
51+
+
52+
`git maintenance register` will also disable foreground maintenance by
53+
setting `maintenance.auto = false` in the current repository. This config
54+
setting will remain after a `git maintenance unregister` command.
4055

4156
run::
4257
Run one or more maintenance tasks. If one or more `--task` options

builtin/gc.c

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1251,6 +1251,59 @@ static int compare_tasks_by_selection(const void *a_, const void *b_)
12511251
return b->selected_order - a->selected_order;
12521252
}
12531253

1254+
static int has_schedule_config(void)
1255+
{
1256+
int i, found = 0;
1257+
struct strbuf config_name = STRBUF_INIT;
1258+
size_t prefix;
1259+
1260+
strbuf_addstr(&config_name, "maintenance.");
1261+
prefix = config_name.len;
1262+
1263+
for (i = 0; !found && i < TASK__COUNT; i++) {
1264+
char *value;
1265+
1266+
strbuf_setlen(&config_name, prefix);
1267+
strbuf_addf(&config_name, "%s.schedule", tasks[i].name);
1268+
1269+
if (!git_config_get_string(config_name.buf, &value)) {
1270+
found = 1;
1271+
FREE_AND_NULL(value);
1272+
}
1273+
1274+
strbuf_setlen(&config_name, prefix);
1275+
strbuf_addf(&config_name, "%s.enabled", tasks[i].name);
1276+
1277+
if (!git_config_get_string(config_name.buf, &value)) {
1278+
found = 1;
1279+
FREE_AND_NULL(value);
1280+
}
1281+
}
1282+
1283+
strbuf_release(&config_name);
1284+
return found;
1285+
}
1286+
1287+
static void set_recommended_schedule(void)
1288+
{
1289+
if (has_schedule_config())
1290+
return;
1291+
1292+
tasks[TASK_GC].enabled = 0;
1293+
1294+
tasks[TASK_PREFETCH].enabled = 1;
1295+
tasks[TASK_PREFETCH].schedule = SCHEDULE_HOURLY;
1296+
1297+
tasks[TASK_COMMIT_GRAPH].enabled = 1;
1298+
tasks[TASK_COMMIT_GRAPH].schedule = SCHEDULE_HOURLY;
1299+
1300+
tasks[TASK_LOOSE_OBJECTS].enabled = 1;
1301+
tasks[TASK_LOOSE_OBJECTS].schedule = SCHEDULE_DAILY;
1302+
1303+
tasks[TASK_INCREMENTAL_REPACK].enabled = 1;
1304+
tasks[TASK_INCREMENTAL_REPACK].schedule = SCHEDULE_DAILY;
1305+
}
1306+
12541307
static int maintenance_run_tasks(struct maintenance_run_opts *opts)
12551308
{
12561309
int i, found_selected = 0;
@@ -1280,6 +1333,8 @@ static int maintenance_run_tasks(struct maintenance_run_opts *opts)
12801333

12811334
if (found_selected)
12821335
QSORT(tasks, TASK__COUNT, compare_tasks_by_selection);
1336+
else if (opts->schedule != SCHEDULE_NONE)
1337+
set_recommended_schedule();
12831338

12841339
for (i = 0; i < TASK__COUNT; i++) {
12851340
if (found_selected && tasks[i].selected_order < 0)
@@ -1417,6 +1472,9 @@ static int maintenance_register(void)
14171472
if (!the_repository || !the_repository->gitdir)
14181473
return 0;
14191474

1475+
/* Disable foreground maintenance */
1476+
git_config_set("maintenance.auto", "false");
1477+
14201478
config_get.git_cmd = 1;
14211479
strvec_pushl(&config_get.args, "config", "--global", "--get", "maintenance.repo",
14221480
the_repository->worktree ? the_repository->worktree

t/t7900-maintenance.sh

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -305,11 +305,14 @@ test_expect_success 'register and unregister' '
305305
git config --global --add maintenance.repo /existing1 &&
306306
git config --global --add maintenance.repo /existing2 &&
307307
git config --global --get-all maintenance.repo >before &&
308+
308309
git maintenance register &&
309-
git config --global --get-all maintenance.repo >actual &&
310-
cp before after &&
311-
pwd >>after &&
312-
test_cmp after actual &&
310+
test_cmp_config false maintenance.auto &&
311+
git config --global --get-all maintenance.repo >between &&
312+
cp before expect &&
313+
pwd >>expect &&
314+
test_cmp expect between &&
315+
313316
git maintenance unregister &&
314317
git config --global --get-all maintenance.repo >actual &&
315318
test_cmp before actual

0 commit comments

Comments
 (0)