Subject: [PATCH v2 0/3] git worktree prune improvements

From: Nguyễn Thái Ngọc Duy <[email-masked]>
Date: Thu, 15 Mar 2018 17:44:09 +0100

v2 fixes comments from Eric and rebases on 'master' since 'worktree
move' has been merged. Commit messages are updated to reflect this.

There's one thing I didn't do is moving the new paragraph up [1]. I
still feel this it's not right to put it there since it starts with
'gc --auto' introduction, then goes to loose objects and packs.
Inserting a note between the loose objects paragraph and the packs one
does not make much sense.

Interdiff

-- 8< --
diff --git a/Documentation/git-gc.txt b/Documentation/git-gc.txt
index 862c931104..3126e0dd00 100644
--- a/Documentation/git-gc.txt
+++ b/Documentation/git-gc.txt
@@ -17,7 +17,7 @@ Runs a number of housekeeping tasks within the current repository,
 such as compressing file revisions (to reduce disk space and increase
 performance), removing unreachable objects which may have been
 created from prior invocations of 'git add', packing refs, pruning
-reflog, rerere or stale working trees.
+reflog, rerere metadata or stale working trees.
 
 Users are encouraged to run this task on a regular basis within
 each repository to maintain good disk space utilization and good
@@ -46,14 +46,14 @@ OPTIONS
 	With this option, 'git gc' checks whether any housekeeping is
 	required; if not, it exits without performing any work.
 	Some git commands run `git gc --auto` after performing
-	operations that could create many loose objects.
+	operations that could create many loose objects. Housekeeping
+	is required if there are too many loose objects or too many
+	packs in the repository.
 +
-Housekeeping is required if there are too many loose objects or
-too many packs in the repository. If the number of loose objects
-exceeds the value of the `gc.auto` configuration variable, then
-all loose objects are combined into a single pack using
-`git repack -d -l`.  Setting the value of `gc.auto` to 0
-disables automatic packing of loose objects.
+If the number of loose objects exceeds the value of the `gc.auto`
+configuration variable, then all loose objects are combined into a
+single pack using `git repack -d -l`.  Setting the value of `gc.auto`
+to 0 disables automatic packing of loose objects.
 +
 If the number of packs exceeds the value of `gc.autoPackLimit`,
 then existing packs (except those marked with a `.keep` file)
@@ -61,9 +61,10 @@ are consolidated into a single pack by using the `-A` option of
 'git repack'. Setting `gc.autoPackLimit` to 0 disables
 automatic consolidation of packs.
 +
-If `git gc --auto` goes ahead because of either too loose objects or
-packs, all other housekeeping tasks (e.g. rerere, working trees,
-reflog...) will also be be performed.
+If houskeeping is required due to many loose objects or packs, all
+other housekeeping tasks (e.g. rerere, working trees, reflog...) will
+be performed as well.
+
 
 --prune=<date>::
 	Prune loose objects older than date (default is 2 weeks ago,
@@ -138,9 +139,10 @@ The optional configuration variable `gc.pruneExpire` controls how old
 the unreferenced loose objects have to be before they are pruned.  The
 default is "2 weeks ago".
 
-The optional gc.worktreePruneExpire controls how old a stale working
-tree before `git worktree prune` deletes it. The default is "3 months
-ago".
+Optional configuration variable `gc.worktreePruneExpire` controls how
+old a stale working tree should be before `git worktree prune` deletes
+it. Default is "3 months ago".
+
 
 Notes
 -----
diff --git a/builtin/worktree.c b/builtin/worktree.c
index 4d4404e97f..b1e8f0534c 100644
--- a/builtin/worktree.c
+++ b/builtin/worktree.c
@@ -100,10 +100,8 @@ static int prune_worktree(const char *id, struct strbuf *reason)
 	path[len] = '\0';
 	if (!file_exists(path)) {
 		free(path);
-		if (st.st_mtime <= expire) {
-			if (!stat(git_path("worktrees/%s/index", id), &st) &&
-			    st.st_mtime > expire)
-				return 0;
+		if (stat(git_path("worktrees/%s/index", id), &st) ||
+		    st.st_mtime <= expire) {
 			strbuf_addf(reason, _("Removing worktrees/%s: gitdir file points to non-existent location"), id);
 			return 1;
 		} else {
diff --git a/t/t2026-worktree-prune.sh b/t/t2026-worktree-prune.sh
index a0f1e3bb80..b7d6d5d45a 100755
--- a/t/t2026-worktree-prune.sh
+++ b/t/t2026-worktree-prune.sh
@@ -78,10 +78,9 @@ test_expect_success 'not prune locked checkout' '
 
 test_expect_success 'not prune recent checkouts' '
 	test_when_finished rm -r .git/worktrees &&
-	mkdir zz &&
-	mkdir -p .git/worktrees/jlm &&
-	echo "$(pwd)"/zz >.git/worktrees/jlm/gitdir &&
-	rmdir zz &&
+	git worktree add jlm HEAD &&
+	test -d .git/worktrees/jlm &&
+	rm -rf jlm &&
 	git worktree prune --verbose --expire=2.days.ago &&
 	test -d .git/worktrees/jlm
 '
-- 8< --

[1] https://public-inbox.org/git/[email-masked]/

Nguyễn Thái Ngọc Duy (3):
  gc.txt: more details about what gc does
  worktree: delete dead code
  worktree prune: improve prune logic when worktree is moved

 Documentation/git-gc.txt               | 28 +++++++++++++++++---------
 Documentation/gitrepository-layout.txt |  5 -----
 builtin/worktree.c                     | 11 ++--------
 t/t2026-worktree-prune.sh              |  7 +++----
 4 files changed, 24 insertions(+), 27 deletions(-)

-- 
2.16.2.903.gd04caf5039