Skip to content

Commit ec31474

Browse files
pks-tgitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent ae76c1c commit ec31474

File tree

4 files changed

+128
-0
lines changed

4 files changed

+128
-0
lines changed

Documentation/config/maintenance.adoc

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,3 +83,11 @@ maintenance.reflog-expire.auto::
8383
positive value implies the command should run when the number of
8484
expired reflog entries in the "HEAD" reflog is at least the value of
8585
`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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,10 @@ reflog-expire::
166166
The `reflog-expire` task deletes any entries in the reflog older than the
167167
expiry threshold. See linkgit:git-reflog[1] for more information.
168168

169+
worktree-prune::
170+
The `worktree-prune` task deletes stale or broken worktrees. See
171+
linkit:git-worktree[1] for more information.
172+
169173
OPTIONS
170174
-------
171175
--auto::

builtin/gc.c

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
#include "hook.h"
4444
#include "setup.h"
4545
#include "trace2.h"
46+
#include "worktree.h"
4647

4748
#define FAILED_RUN "failed to run %s"
4849

@@ -345,6 +346,44 @@ static int maintenance_task_worktree_prune(struct maintenance_run_opts *opts UNU
345346
return run_command(&prune_worktrees_cmd);
346347
}
347348

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+
348387
static int too_many_loose_objects(struct gc_config *cfg)
349388
{
350389
/*
@@ -1465,6 +1504,7 @@ enum maintenance_task_label {
14651504
TASK_COMMIT_GRAPH,
14661505
TASK_PACK_REFS,
14671506
TASK_REFLOG_EXPIRE,
1507+
TASK_WORKTREE_PRUNE,
14681508

14691509
/* Leave as final value */
14701510
TASK__COUNT
@@ -1506,6 +1546,11 @@ static struct maintenance_task tasks[] = {
15061546
maintenance_task_reflog_expire,
15071547
reflog_expire_condition,
15081548
},
1549+
[TASK_WORKTREE_PRUNE] = {
1550+
"worktree-prune",
1551+
maintenance_task_worktree_prune,
1552+
worktree_prune_condition,
1553+
},
15091554
};
15101555

15111556
static int compare_tasks_by_selection(const void *a_, const void *b_)

t/t7900-maintenance.sh

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -493,6 +493,77 @@ test_expect_success 'reflog-expire task --auto only packs when exceeding limits'
493493
test_subcommand git reflog expire --all <reflog-expire-auto.txt
494494
'
495495

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+
496567
test_expect_success '--auto and --schedule incompatible' '
497568
test_must_fail git maintenance run --auto --schedule=daily 2>err &&
498569
test_grep "at most one" err

0 commit comments

Comments
 (0)