builtin/maintenance: introduce "worktree-prune" task
While git-gc(1) knows to prune stale worktrees, git-maintenance(1) does not yet have a task for this cleanup. Introduce a new "worktree-prune" task to plug this gap. Signed-off-by: Patrick Steinhardt <ps@pks.im> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Patrick Steinhardt committed
May 7, 2025 at 09:21 UTC
ec31474656de3849fb9ed31f238fabdb6a59f1b1
4 files changed
+128
Documentation/config/maintenance.adoc
+8
@@ -83,3 +83,11 @@ maintenance.reflog-expire.auto::
83
positive value implies the command should run when the number of
84
expired reflog entries in the "HEAD" reflog is at least the value of
85
`maintenance.loose-objects.auto`. The default value is 100.
86
+
87
+maintenance.worktree-prune.auto::
88
+ This integer config option controls how often the `worktree-prune` task
89
+ should be run as part of `git maintenance run --auto`. If zero, then
90
+ the `worktree-prune` task will not run with the `--auto` option. A
91
+ negative value will force the task to run every time. Otherwise, a
92
+ positive value implies the command should run when the number of
93
+ prunable worktrees exceeds the value. The default value is 1.
Documentation/git-maintenance.adoc
+4
@@ -166,6 +166,10 @@ reflog-expire::
166
The `reflog-expire` task deletes any entries in the reflog older than the
167
expiry threshold. See linkgit:git-reflog[1] for more information.
168
169
+worktree-prune::
170
+ The `worktree-prune` task deletes stale or broken worktrees. See
171
+ linkit:git-worktree[1] for more information.
172
+
173
OPTIONS
174
-------
175
--auto::
builtin/gc.c
+45
@@ -43,6 +43,7 @@
43
#include "hook.h"
44
#include "setup.h"
45
#include "trace2.h"
46
+#include "worktree.h"
47
48
#define FAILED_RUN "failed to run %s"
49
@@ -345,6 +346,44 @@ static int maintenance_task_worktree_prune(struct maintenance_run_opts *opts UNU
346
return run_command(&prune_worktrees_cmd);
347
}
348
349
+static int worktree_prune_condition(struct gc_config *cfg)
350
+{
351
+ struct strbuf buf = STRBUF_INIT;
352
+ int should_prune = 0, limit = 1;
353
+ timestamp_t expiry_date;
354
+ struct dirent *d;
355
+ DIR *dir = NULL;
356
+
357
+ git_config_get_int("maintenance.worktree-prune.auto", &limit);
358
+ if (limit <= 0) {
359
+ should_prune = limit < 0;
360
+ goto out;
361
+ }
362
+
363
+ if (parse_expiry_date(cfg->prune_worktrees_expire, &expiry_date))
364
+ goto out;
365
+
366
+ dir = opendir(repo_git_path_replace(the_repository, &buf, "worktrees"));
367
+ if (!dir)
368
+ goto out;
369
+
370
+ while (limit && (d = readdir_skip_dot_and_dotdot(dir))) {
371
+ char *wtpath;
372
+ strbuf_reset(&buf);
373
+ if (should_prune_worktree(d->d_name, &buf, &wtpath, expiry_date))
374
+ limit--;
375
+ free(wtpath);
376
+ }
377
+
378
+ should_prune = !limit;
379
+
380
+out:
381
+ if (dir)
382
+ closedir(dir);
383
+ strbuf_release(&buf);
384
+ return should_prune;
385
+}
386
+
387
static int too_many_loose_objects(struct gc_config *cfg)
388
{
389
/*
@@ -1465,6 +1504,7 @@ enum maintenance_task_label {
1504
TASK_COMMIT_GRAPH,
1505
TASK_PACK_REFS,
1506
TASK_REFLOG_EXPIRE,
1507
+ TASK_WORKTREE_PRUNE,
1508
1509
/* Leave as final value */
1510
TASK__COUNT
@@ -1506,6 +1546,11 @@ static struct maintenance_task tasks[] = {
1546
maintenance_task_reflog_expire,
1547
reflog_expire_condition,
1548
},
1549
+ [TASK_WORKTREE_PRUNE] = {
1550
+ "worktree-prune",
1551
+ maintenance_task_worktree_prune,
1552
+ worktree_prune_condition,
1553
+ },
1554
};
1555
1556
static int compare_tasks_by_selection(const void *a_, const void *b_)
t/t7900-maintenance.sh
+71
@@ -493,6 +493,77 @@ test_expect_success 'reflog-expire task --auto only packs when exceeding limits'
493
test_subcommand git reflog expire --all <reflog-expire-auto.txt
494
'
495
496
+test_expect_worktree_prune () {
497
+ negate=
498
+ if test "$1" = "!"
499
+ then
500
+ negate="!"
501
+ shift
502
+ fi
503
+
504
+ rm -f "worktree-prune.txt" &&
505
+ GIT_TRACE2_EVENT="$(pwd)/worktree-prune.txt" "$@" &&
506
+ test_subcommand $negate git worktree prune --expire 3.months.ago <worktree-prune.txt
507
+}
508
+
509
+test_expect_success 'worktree-prune task without --auto always prunes' '
510
+ test_expect_worktree_prune git maintenance run --task=worktree-prune
511
+'
512
+
513
+test_expect_success 'worktree-prune task --auto only prunes with prunable worktree' '
514
+ test_expect_worktree_prune ! git maintenance run --auto --task=worktree-prune &&
515
+ mkdir .git/worktrees &&
516
+ : >.git/worktrees/abc &&
517
+ test_expect_worktree_prune git maintenance run --auto --task=worktree-prune
518
+'
519
+
520
+test_expect_success 'worktree-prune task with --auto honors maintenance.worktree-prune.auto' '
521
+ # A negative value should always prune.
522
+ test_expect_worktree_prune git -c maintenance.worktree-prune.auto=-1 maintenance run --auto --task=worktree-prune &&
523
+
524
+ mkdir .git/worktrees &&
525
+ : >.git/worktrees/first &&
526
+ : >.git/worktrees/second &&
527
+ : >.git/worktrees/third &&
528
+
529
+ # Zero should never prune.
530
+ test_expect_worktree_prune ! git -c maintenance.worktree-prune.auto=0 maintenance run --auto --task=worktree-prune &&
531
+ # A positive value should require at least this many prunable worktrees.
532
+ test_expect_worktree_prune ! git -c maintenance.worktree-prune.auto=4 maintenance run --auto --task=worktree-prune &&
533
+ test_expect_worktree_prune git -c maintenance.worktree-prune.auto=3 maintenance run --auto --task=worktree-prune
534
+'
535
+
536
+test_expect_success 'worktree-prune task with --auto honors maintenance.worktree-prune.auto' '
537
+ # A negative value should always prune.
538
+ test_expect_worktree_prune git -c maintenance.worktree-prune.auto=-1 maintenance run --auto --task=worktree-prune &&
539
+
540
+ mkdir .git/worktrees &&
541
+ : >.git/worktrees/first &&
542
+ : >.git/worktrees/second &&
543
+ : >.git/worktrees/third &&
544
+
545
+ # Zero should never prune.
546
+ test_expect_worktree_prune ! git -c maintenance.worktree-prune.auto=0 maintenance run --auto --task=worktree-prune &&
547
+ # A positive value should require at least this many prunable worktrees.
548
+ test_expect_worktree_prune ! git -c maintenance.worktree-prune.auto=4 maintenance run --auto --task=worktree-prune &&
549
+ test_expect_worktree_prune git -c maintenance.worktree-prune.auto=3 maintenance run --auto --task=worktree-prune
550
+'
551
+
552
+test_expect_success 'worktree-prune task honors gc.worktreePruneExpire' '
553
+ git worktree add worktree &&
554
+ rm -rf worktree &&
555
+
556
+ rm -f worktree-prune.txt &&
557
+ GIT_TRACE2_EVENT="$(pwd)/worktree-prune.txt" git -c gc.worktreePruneExpire=1.week.ago maintenance run --auto --task=worktree-prune &&
558
+ test_subcommand ! git worktree prune --expire 1.week.ago <worktree-prune.txt &&
559
+ test_path_is_dir .git/worktrees/worktree &&
560
+
561
+ rm -f worktree-prune.txt &&
562
+ GIT_TRACE2_EVENT="$(pwd)/worktree-prune.txt" git -c gc.worktreePruneExpire=now maintenance run --auto --task=worktree-prune &&
563
+ test_subcommand git worktree prune --expire now <worktree-prune.txt &&
564
+ test_path_is_missing .git/worktrees/worktree
565
+'
566
+
567
test_expect_success '--auto and --schedule incompatible' '
568
test_must_fail git maintenance run --auto --schedule=daily 2>err &&
569
test_grep "at most one" err