Skip to content

Commit e3df33b

Browse files
pcloudsgitster
authored andcommitted
gc: support prune --worktrees
Helped-by: Marc Branchaud <[email protected]> Signed-off-by: Marc Branchaud <[email protected]> Signed-off-by: Nguyễn Thái Ngọc Duy <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 09dbb90 commit e3df33b

File tree

3 files changed

+24
-4
lines changed

3 files changed

+24
-4
lines changed

Documentation/config.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1229,6 +1229,13 @@ gc.pruneexpire::
12291229
"now" may be used to disable this grace period and always prune
12301230
unreachable objects immediately.
12311231

1232+
gc.pruneworktreesexpire::
1233+
When 'git gc' is run, it will call
1234+
'prune --worktrees --expire 3.months.ago'.
1235+
Override the grace period with this config variable. The value
1236+
"now" may be used to disable the grace period and prune
1237+
$GIT_DIR/worktrees immediately.
1238+
12321239
gc.reflogexpire::
12331240
gc.<pattern>.reflogexpire::
12341241
'git reflog expire' removes reflog entries older than

Documentation/git-checkout.txt

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -435,17 +435,20 @@ $GIT_DIR or $GIT_COMMON_DIR when you need to directly access something
435435
inside $GIT_DIR. Use `git rev-parse --git-path` to get the final path.
436436

437437
When you are done with a linked working tree you can simply delete it.
438-
You can clean up any stale $GIT_DIR/worktrees entries via `git prune
439-
--worktrees` in the main or any linked working tree.
438+
The working tree's entry in the repository's $GIT_DIR/worktrees
439+
directory will eventually be removed automatically (see
440+
`gc.pruneworktreesexpire` in linkgit::git-config[1]), or you can run
441+
`git prune --worktrees` in the main or any linked working tree to
442+
clean up any stale entries in $GIT_DIR/worktrees.
440443

441444
If you move a linked working directory to another file system, or
442445
within a file system that does not support hard links, you need to run
443446
at least one git command inside the linked working directory
444447
(e.g. `git status`) in order to update its entry in $GIT_DIR/worktrees
445448
so that it does not get automatically removed.
446449

447-
To prevent `git prune --worktrees` from deleting a $GIT_DIR/worktrees
448-
entry (which can be useful in some situations, such as when the
450+
To prevent a $GIT_DIR/worktrees entry from from being pruned (which
451+
can be useful in some situations, such as when the
449452
entry's working tree is stored on a portable device), add a file named
450453
'locked' to the entry's directory. The file contains the reason in
451454
plain text. For example, if a linked working tree's `.git` file points

builtin/gc.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,13 @@ static int gc_auto_threshold = 6700;
3333
static int gc_auto_pack_limit = 50;
3434
static int detach_auto = 1;
3535
static const char *prune_expire = "2.weeks.ago";
36+
static const char *prune_worktrees_expire = "3.months.ago";
3637

3738
static struct argv_array pack_refs_cmd = ARGV_ARRAY_INIT;
3839
static struct argv_array reflog = ARGV_ARRAY_INIT;
3940
static struct argv_array repack = ARGV_ARRAY_INIT;
4041
static struct argv_array prune = ARGV_ARRAY_INIT;
42+
static struct argv_array prune_worktrees = ARGV_ARRAY_INIT;
4143
static struct argv_array rerere = ARGV_ARRAY_INIT;
4244

4345
static char *pidfile;
@@ -83,6 +85,7 @@ static void gc_config(void)
8385
git_config_get_int("gc.autopacklimit", &gc_auto_pack_limit);
8486
git_config_get_bool("gc.autodetach", &detach_auto);
8587
git_config_date_string("gc.pruneexpire", &prune_expire);
88+
git_config_date_string("gc.pruneworktreesexpire", &prune_worktrees_expire);
8689
git_config(git_default_config, NULL);
8790
}
8891

@@ -290,6 +293,7 @@ int cmd_gc(int argc, const char **argv, const char *prefix)
290293
argv_array_pushl(&reflog, "reflog", "expire", "--all", NULL);
291294
argv_array_pushl(&repack, "repack", "-d", "-l", NULL);
292295
argv_array_pushl(&prune, "prune", "--expire", NULL);
296+
argv_array_pushl(&prune_worktrees, "prune", "--worktrees", "--expire", NULL);
293297
argv_array_pushl(&rerere, "rerere", "gc", NULL);
294298

295299
gc_config();
@@ -359,6 +363,12 @@ int cmd_gc(int argc, const char **argv, const char *prefix)
359363
return error(FAILED_RUN, prune.argv[0]);
360364
}
361365

366+
if (prune_worktrees_expire) {
367+
argv_array_push(&prune_worktrees, prune_worktrees_expire);
368+
if (run_command_v_opt(prune_worktrees.argv, RUN_GIT_CMD))
369+
return error(FAILED_RUN, prune_worktrees.argv[0]);
370+
}
371+
362372
if (run_command_v_opt(rerere.argv, RUN_GIT_CMD))
363373
return error(FAILED_RUN, rerere.argv[0]);
364374

0 commit comments

Comments
 (0)