Skip to contentSkip to conversation

worktree: new place for "git prune --worktrees"

PATCH In review Nguyễn Thái Ngọc Duy 2 participants 3 messages Jun 15, 2016

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 worktree: new place for "git prune --worktrees"

Commit 23af91d (prune: strategies for linked checkouts - 2014-11-30) adds "--worktrees" to "git prune" without realizing that "git prune" is for object database only. This patch moves the same functionality to a new command "git worktree".

---
In future I probably move the big block of text in git-checkout.txt to git-worktree.txt and add "git worktree list". But let's start with something small and simple before "git prune --worktrees" is shipped out.

 .gitignore                           |   1 +
 Documentation/git-prune.txt          |   3 -
 Documentation/git-worktree.txt (new) |  48 +++++++++++++
 Makefile                             |   1 +
 builtin.h                            |   1 +
 builtin/gc.c                         |   2 +-
 builtin/prune.c                      |  99 --------------------------
 builtin/worktree.c (new)             | 133 +++++++++++++++++++++++++++++++++++
 command-list.txt                     |   1 +
 git.c                                |   1 +
 t/t2026-prune-linked-checkouts.sh    |  22 +++---
 11 files changed, 198 insertions(+), 114 deletions(-)
 create mode 100644 Documentation/git-worktree.txt
 create mode 100644 builtin/worktree.c
.gitignore +1 -0
index 422c538..a685ec1 100644
@@ -171,6 +171,7 @@
 /git-verify-tag
 /git-web--browse
 /git-whatchanged
+/git-worktree
 /git-write-tree
 /git-core-*/?*
 /gitweb/GITWEB-BUILD-OPTIONS
Documentation/git-prune.txt +0 -3
index 1cf3bed..7a493c8 100644
@@ -48,9 +48,6 @@ OPTIONS
 --expire <time>::
 	Only expire loose objects older than <time>.
 
---worktrees::
-	Prune dead working tree information in $GIT_DIR/worktrees.
-
 <head>...::
 	In addition to objects
 	reachable from any of our references, keep objects
Documentation/git-worktree.txt new file +48 -0

48 lines not on this page · raw message

Makefile +1 -0
index 149f1c7..89785f6 100644
@@ -909,6 +909,7 @@ BUILTIN_OBJS += builtin/var.o
 BUILTIN_OBJS += builtin/verify-commit.o
 BUILTIN_OBJS += builtin/verify-pack.o
 BUILTIN_OBJS += builtin/verify-tag.o
+BUILTIN_OBJS += builtin/worktree.o
 BUILTIN_OBJS += builtin/write-tree.o
 
 GITLIBS = $(LIB_FILE) $(XDIFF_LIB)
builtin.h +1 -0
index b87df70..9e04f97 100644
@@ -133,6 +133,7 @@ extern int cmd_verify_commit(int argc, const char **argv, const char *prefix);
 extern int cmd_verify_tag(int argc, const char **argv, const char *prefix);
 extern int cmd_version(int argc, const char **argv, const char *prefix);
 extern int cmd_whatchanged(int argc, const char **argv, const char *prefix);
+extern int cmd_worktree(int argc, const char **argv, const char *prefix);
 extern int cmd_write_tree(int argc, const char **argv, const char *prefix);
 extern int cmd_verify_pack(int argc, const char **argv, const char *prefix);
 extern int cmd_show_ref(int argc, const char **argv, const char *prefix);
builtin/gc.c +1 -1
index 36fe333..4957c39 100644
@@ -293,7 +293,7 @@ int cmd_gc(int argc, const char **argv, const char *prefix)
 	argv_array_pushl(&reflog, "reflog", "expire", "--all", NULL);
 	argv_array_pushl(&repack, "repack", "-d", "-l", NULL);
 	argv_array_pushl(&prune, "prune", "--expire", NULL);
-	argv_array_pushl(&prune_worktrees, "prune", "--worktrees", "--expire", NULL);
+	argv_array_pushl(&prune_worktrees, "worktree", "prune", "--expire", NULL);
 	argv_array_pushl(&rerere, "rerere", "gc", NULL);
 
 	gc_config();
builtin/prune.c +0 -99

127 lines not on this page · raw message

builtin/worktree.c new file +133 -0

133 lines not on this page · raw message

command-list.txt +1 -0
index b17c011..2a94137 100644
@@ -148,4 +148,5 @@ git-verify-pack                         plumbinginterrogators
 git-verify-tag                          ancillaryinterrogators
 gitweb                                  ancillaryinterrogators
 git-whatchanged                         ancillaryinterrogators
+git-worktree                            mainporcelain
 git-write-tree                          plumbingmanipulators
git.c +1 -0
index 44374b1..fa77bc9 100644
@@ -483,6 +483,7 @@ static struct cmd_struct commands[] = {
 	{ "verify-tag", cmd_verify_tag, RUN_SETUP },
 	{ "version", cmd_version },
 	{ "whatchanged", cmd_whatchanged, RUN_SETUP },
+	{ "worktree", cmd_worktree, RUN_SETUP },
 	{ "write-tree", cmd_write_tree, RUN_SETUP },
 };
 
t/t2026-prune-linked-checkouts.sh +11 -11
index 1821a48..e872f02 100755
@@ -8,15 +8,15 @@ test_expect_success initialize '
 	git commit --allow-empty -m init
 '
 
-test_expect_success 'prune --worktrees on normal repo' '
-	git prune --worktrees &&
-	test_must_fail git prune --worktrees abc
+test_expect_success 'worktree prune on normal repo' '
+	git worktree prune &&
+	test_must_fail git worktree prune abc
 '
 
 test_expect_success 'prune files inside $GIT_DIR/worktrees' '
 	mkdir .git/worktrees &&
 	: >.git/worktrees/abc &&
-	git prune --worktrees --verbose >actual &&
+	git worktree prune --verbose >actual &&
 	cat >expect <<EOF &&
 Removing worktrees/abc: not a valid directory
 EOF
@@ -31,7 +31,7 @@ test_expect_success 'prune directories without gitdir' '
 	cat >expect <<EOF &&
 Removing worktrees/def: gitdir file does not exist
 EOF
-	git prune --worktrees --verbose >actual &&
+	git worktree prune --verbose >actual &&
 	test_i18ncmp expect actual &&
 	! test -d .git/worktrees/def &&
 	! test -d .git/worktrees
@@ -42,7 +42,7 @@ test_expect_success SANITY 'prune directories with unreadable gitdir' '
 	: >.git/worktrees/def/def &&
 	: >.git/worktrees/def/gitdir &&
 	chmod u-r .git/worktrees/def/gitdir &&
-	git prune --worktrees --verbose >actual &&
+	git worktree prune --verbose >actual &&
 	test_i18ngrep "Removing worktrees/def: unable to read gitdir file" actual &&
 	! test -d .git/worktrees/def &&
 	! test -d .git/worktrees
@@ -52,7 +52,7 @@ test_expect_success 'prune directories with invalid gitdir' '
 	mkdir -p .git/worktrees/def/abc &&
 	: >.git/worktrees/def/def &&
 	: >.git/worktrees/def/gitdir &&
-	git prune --worktrees --verbose >actual &&
+	git worktree prune --verbose >actual &&
 	test_i18ngrep "Removing worktrees/def: invalid gitdir file" actual &&
 	! test -d .git/worktrees/def &&
 	! test -d .git/worktrees
@@ -62,7 +62,7 @@ test_expect_success 'prune directories with gitdir pointing to nowhere' '
 	mkdir -p .git/worktrees/def/abc &&
 	: >.git/worktrees/def/def &&
 	echo "$(pwd)"/nowhere >.git/worktrees/def/gitdir &&
-	git prune --worktrees --verbose >actual &&
+	git worktree prune --verbose >actual &&
 	test_i18ngrep "Removing worktrees/def: gitdir file points to non-existent location" actual &&
 	! test -d .git/worktrees/def &&
 	! test -d .git/worktrees
@@ -72,7 +72,7 @@ test_expect_success 'not prune locked checkout' '
 	test_when_finished rm -r .git/worktrees &&
 	mkdir -p .git/worktrees/ghi &&
 	: >.git/worktrees/ghi/locked &&
-	git prune --worktrees &&
+	git worktree prune &&
 	test -d .git/worktrees/ghi
 '
 
@@ -82,14 +82,14 @@ test_expect_success 'not prune recent checkouts' '
 	mkdir -p .git/worktrees/jlm &&
 	echo "$(pwd)"/zz >.git/worktrees/jlm/gitdir &&
 	rmdir zz &&
-	git prune --worktrees --verbose --expire=2.days.ago &&
+	git worktree prune --verbose --expire=2.days.ago &&
 	test -d .git/worktrees/jlm
 '
 
 test_expect_success 'not prune proper checkouts' '
 	test_when_finished rm -r .git/worktrees &&
 	git checkout "--to=$PWD/nop" --detach master &&
-	git prune --worktrees &&
+	git worktree prune &&
 	test -d .git/worktrees/nop
 '
 
Signature · 1 line
2.3.0.rc1.137.g477eb31
#1 lore raw
Quoted 11 lines · Nguyễn Thái Ngọc Duy

Commit 23af91d (prune: strategies for linked checkouts - 2014-11-30) adds "--worktrees" to "git prune" without realizing that "git prune" is for object database only. This patch moves the same functionality to a new command "git worktree".

---
In future I probably move the big block of text in git-checkout.txt to git-worktree.txt and add "git worktree list". But let's start with something small and simple before "git prune --worktrees" is shipped out.

Thanks. I notice that after applying this, builtin/prune.c does not revert to the original before 23af91d. It stops including "dir.h" and it adds an extra blank line. We've been including a header that is not necessary even in v2.4.0, it seems. We used to walk $GIT_DIR/objects ourselves to find loose object files and "dir.h" was needed for is_dot_or_dotdot(); for_each_loose_file_in_objdir() is what we use these days to hide the implementation details these days; 27e1e22 forgot to remove the inclusion when it did this.

The C code part is mostly just \C-x \C-v and I found nothing questionable.

command-list.txt git-verify-pack plumbinginterrogators · line 151

Quoted 9 lines
command-list.txt +1 -0
index b17c011..2a94137 100644
@@ -148,4 +148,5 @@ git-verify-pack                         plumbinginterrogators
 git-verify-tag                          ancillaryinterrogators
 gitweb                                  ancillaryinterrogators
 git-whatchanged                         ancillaryinterrogators
+git-worktree                            mainporcelain

I doubt that a helper that is primarily spawned from "gc" as its implementation detail is more mainporcelain than "git config" is:

git-config ancillarymanipulators

I also wonder if "git worktree" command should have a mode that works in a way similar to how new-workdir (in contrib/workdir) does, instead of an option "checkout --to" that looks just out of place as "worktree prune" was out of place in "prune". The feature is doing a lot more than what "checkout" normally does (somewhere in between "checkout" and "clone", I would say), and it may be cleaner to use an independent command "git worktree" to manage a separate worktree.

And when that happens, the command should definitely be classified as a mainporcelain.

git.c static struct cmd_struct commands[] = { · line 486

Quoted 9 lines
git.c +1 -0
index 44374b1..fa77bc9 100644
@@ -483,6 +483,7 @@ static struct cmd_struct commands[] = {
 	{ "verify-tag", cmd_verify_tag, RUN_SETUP },
 	{ "version", cmd_version },
 	{ "whatchanged", cmd_whatchanged, RUN_SETUP },
+	{ "worktree", cmd_worktree, RUN_SETUP },

We do not NEED_WORK_TREE because we can create a new worktree off of a bare repository?

author
#2 lore raw
Quoted 23 lines · Junio C Hamano
Quoted 11 lines · Nguyễn Thái Ngọc Duy

Commit 23af91d (prune: strategies for linked checkouts - 2014-11-30) adds "--worktrees" to "git prune" without realizing that "git prune" is for object database only. This patch moves the same functionality to a new command "git worktree".

---
In future I probably move the big block of text in git-checkout.txt to git-worktree.txt and add "git worktree list". But let's start with something small and simple before "git prune --worktrees" is shipped out.

Thanks. I notice that after applying this, builtin/prune.c does not revert to the original before 23af91d. It stops including "dir.h" and it adds an extra blank line. We've been including a header that is not necessary even in v2.4.0, it seems. We used to walk $GIT_DIR/objects ourselves to find loose object files and "dir.h" was needed for is_dot_or_dotdot(); for_each_loose_file_in_objdir() is what we use these days to hide the implementation details these days; 27e1e22 forgot to remove the inclusion when it did this.

Quoted 2 lines

The C code part is mostly just \C-x \C-v and I found nothing questionable.

I was about to prepare v2, but the only change in the end was removing this blank line, not worth another round.

Quoted 23 lines

command-list.txt git-verify-pack plumbinginterrogators · line 151

Quoted 9 lines
command-list.txt +1 -0
index b17c011..2a94137 100644
@@ -148,4 +148,5 @@ git-verify-pack                         plumbinginterrogators
 git-verify-tag                          ancillaryinterrogators
 gitweb                                  ancillaryinterrogators
 git-whatchanged                         ancillaryinterrogators
+git-worktree                            mainporcelain

I doubt that a helper that is primarily spawned from "gc" as its implementation detail is more mainporcelain than "git config" is:

git-config ancillarymanipulators

I also wonder if "git worktree" command should have a mode that works in a way similar to how new-workdir (in contrib/workdir) does, instead of an option "checkout --to" that looks just out of place as "worktree prune" was out of place in "prune". The feature is doing a lot more than what "checkout" normally does (somewhere in between "checkout" and "clone", I would say), and it may be cleaner to use an independent command "git worktree" to manage a separate worktree.

Quoted 2 lines

And when that happens, the command should definitely be classified as a mainporcelain.

And because "git worktree add" is coming, I guess it's ok to stick to mainporcelain here..

Quoted 4 lines
Quoted 4 lines
git.c +0 -0
index 44374b1..fa77bc9 100644
Quoted 8 lines

git.c static struct cmd_struct commands[] = { · line 486

Quoted 5 lines
git.c +1 -0
@@ -483,6 +483,7 @@ static struct cmd_struct commands[] = {
      { "verify-tag", cmd_verify_tag, RUN_SETUP },
      { "version", cmd_version },
      { "whatchanged", cmd_whatchanged, RUN_SETUP },
+     { "worktree", cmd_worktree, RUN_SETUP },

We do not NEED_WORK_TREE because we can create a new worktree off of a bare repository?

Yes.

Signature · 1 line
Duy