gc: support prune --worktrees

Helped-by: Marc Branchaud <marcnarc@xiplink.com> Signed-off-by: Marc Branchaud <marcnarc@xiplink.com> Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Nguyễn Thái Ngọc Duy committed Nov 30, 2014 at 15:24 UTC e3df33bb1b14207848ff339ca27644a01ef17c26
3 files changed +24 -4
Documentation/config.txt
+7
@@ -1229,6 +1229,13 @@ gc.pruneexpire::
1229 "now" may be used to disable this grace period and always prune
1230 unreachable objects immediately.
1231
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 +
1239 gc.reflogexpire::
1240 gc.<pattern>.reflogexpire::
1241 'git reflog expire' removes reflog entries older than
Documentation/git-checkout.txt
+7 -4
@@ -435,8 +435,11 @@ $GIT_DIR or $GIT_COMMON_DIR when you need to directly access something
435 inside $GIT_DIR. Use `git rev-parse --git-path` to get the final path.
436
437 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.
443
444 If you move a linked working directory to another file system, or
445 within a file system that does not support hard links, you need to run
@@ -444,8 +447,8 @@ at least one git command inside the linked working directory
447 (e.g. `git status`) in order to update its entry in $GIT_DIR/worktrees
448 so that it does not get automatically removed.
449
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
452 entry's working tree is stored on a portable device), add a file named
453 'locked' to the entry's directory. The file contains the reason in
454 plain text. For example, if a linked working tree's `.git` file points
builtin/gc.c
+10
@@ -33,11 +33,13 @@ static int gc_auto_threshold = 6700;
33 static int gc_auto_pack_limit = 50;
34 static int detach_auto = 1;
35 static const char *prune_expire = "2.weeks.ago";
36 +static const char *prune_worktrees_expire = "3.months.ago";
37
38 static struct argv_array pack_refs_cmd = ARGV_ARRAY_INIT;
39 static struct argv_array reflog = ARGV_ARRAY_INIT;
40 static struct argv_array repack = ARGV_ARRAY_INIT;
41 static struct argv_array prune = ARGV_ARRAY_INIT;
42 +static struct argv_array prune_worktrees = ARGV_ARRAY_INIT;
43 static struct argv_array rerere = ARGV_ARRAY_INIT;
44
45 static char *pidfile;
@@ -83,6 +85,7 @@ static void gc_config(void)
85 git_config_get_int("gc.autopacklimit", &gc_auto_pack_limit);
86 git_config_get_bool("gc.autodetach", &detach_auto);
87 git_config_date_string("gc.pruneexpire", &prune_expire);
88 + git_config_date_string("gc.pruneworktreesexpire", &prune_worktrees_expire);
89 git_config(git_default_config, NULL);
90 }
91
@@ -290,6 +293,7 @@ int cmd_gc(int argc, const char **argv, const char *prefix)
293 argv_array_pushl(&reflog, "reflog", "expire", "--all", NULL);
294 argv_array_pushl(&repack, "repack", "-d", "-l", NULL);
295 argv_array_pushl(&prune, "prune", "--expire", NULL);
296 + argv_array_pushl(&prune_worktrees, "prune", "--worktrees", "--expire", NULL);
297 argv_array_pushl(&rerere, "rerere", "gc", NULL);
298
299 gc_config();
@@ -359,6 +363,12 @@ int cmd_gc(int argc, const char **argv, const char *prefix)
363 return error(FAILED_RUN, prune.argv[0]);
364 }
365
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 +
372 if (run_command_v_opt(rerere.argv, RUN_GIT_CMD))
373 return error(FAILED_RUN, rerere.argv[0]);
374