[PATCH] builtin/worktree.c: add option for setting worktree name

Subsystems: documentation, the rest

STALE3729d

5 messages, 2 authors, 2016-06-25 · open the first message on its own page

[PATCH] builtin/worktree.c: add option for setting worktree name

From: Barret Rennie <hidden>
Date: 2016-06-25 05:23:31

Add the --name parameter to git worktree add that allows the user to set
the name of the created worktree directory. A worktree must not already
exist with the current name or creation will fail.

Signed-off-by: Barret Rennie <redacted>
---
 Documentation/git-worktree.txt |  6 +++++-
 builtin/worktree.c             | 24 ++++++++++++++++++------
 t/t2025-worktree-add.sh        | 16 ++++++++++++++++
 3 files changed, 39 insertions(+), 7 deletions(-)
diff --git a/Documentation/git-worktree.txt b/Documentation/git-worktree.txt
index 23d8d2a..2af0ee4 100644
--- a/Documentation/git-worktree.txt
+++ b/Documentation/git-worktree.txt
@@ -9,7 +9,7 @@ git-worktree - Manage multiple working trees
 SYNOPSIS
 --------
 [verse]
-'git worktree add' [-f] [--detach] [--checkout] [-b <new-branch>] <path> [<branch>]
+'git worktree add' [-f] [--detach] [--checkout] [-b <new-branch>] [--name <name>] <path> [<branch>]
 'git worktree prune' [-n] [-v] [--expire <expire>]
 'git worktree list' [--porcelain]
 
@@ -88,6 +88,10 @@ OPTIONS
 	With `add`, detach HEAD in the new working tree. See "DETACHED HEAD"
 	in linkgit:git-checkout[1].
 
+--name::
+	Set the name for the worktree. If there is already a worktree with this
+	name, the command will fail.
+
 --[no-]checkout::
 	By default, `add` checks out `<branch>`, however, `--no-checkout` can
 	be used to suppress checkout in order to make customizations,
diff --git a/builtin/worktree.c b/builtin/worktree.c
index e3199a2..ed071b2 100644
--- a/builtin/worktree.c
+++ b/builtin/worktree.c
@@ -24,6 +24,7 @@ struct add_opts {
 	int checkout;
 	const char *new_branch;
 	int force_new_branch;
+	const char *name;
 };
 
 static int show_only;
@@ -212,19 +213,29 @@ static int add_worktree(const char *path, const char *refname,
 			die(_("invalid reference: %s"), refname);
 	}
 
-	name = worktree_basename(path, &len);
+	if (opts->name) {
+		name = opts->name;
+		len = strlen(name);
+	} else {
+		name = worktree_basename(path, &len);
+	}
+
 	strbuf_addstr(&sb_repo,
 		      git_path("worktrees/%.*s", (int)(path + len - name), name));
+
 	len = sb_repo.len;
 	if (safe_create_leading_directories_const(sb_repo.buf))
 		die_errno(_("could not create leading directories of '%s'"),
 			  sb_repo.buf);
-	while (!stat(sb_repo.buf, &st)) {
-		counter++;
-		strbuf_setlen(&sb_repo, len);
-		strbuf_addf(&sb_repo, "%d", counter);
+
+	if (!opts->name) {
+		while (!stat(sb_repo.buf, &st)) {
+			counter++;
+			strbuf_setlen(&sb_repo, len);
+			strbuf_addf(&sb_repo, "%d", counter);
+		}
+		name = strrchr(sb_repo.buf, '/') + 1;
 	}
-	name = strrchr(sb_repo.buf, '/') + 1;
 
 	junk_pid = getpid();
 	atexit(remove_junk);
@@ -326,6 +337,7 @@ static int add(int ac, const char **av, const char *prefix)
 			   N_("create or reset a branch")),
 		OPT_BOOL(0, "detach", &opts.detach, N_("detach HEAD at named commit")),
 		OPT_BOOL(0, "checkout", &opts.checkout, N_("populate the new working tree")),
+		OPT_STRING(0, "name", &opts.name, N_("name"), N_("set name for working tree")),
 		OPT_END()
 	};
 
diff --git a/t/t2025-worktree-add.sh b/t/t2025-worktree-add.sh
index 4bcc335..9abcf8e 100755
--- a/t/t2025-worktree-add.sh
+++ b/t/t2025-worktree-add.sh
@@ -63,6 +63,22 @@ test_expect_success '"add" worktree' '
 	)
 '
 
+test_expect_success '"add" worktree with name' '
+	git worktree add --detach --name custom-name another-worktree master &&
+	(
+		cd here &&
+		test_cmp ../init.t init.t
+	) &&
+	(
+		cd .git/worktrees &&
+		test -d custom-name
+	)
+'
+
+test_expect_success '"add" worktree with name that already exists' '
+	test_must_fail git worktree add --name custom-name --detach yet-another-worktree master
+'
+
 test_expect_success '"add" worktree from a subdir' '
 	(
 		mkdir sub &&
-- 
2.9.0

Re: [PATCH] builtin/worktree.c: add option for setting worktree name

From: Johannes Sixt <hidden>
Date: 2016-06-25 07:16:02

Am 25.06.2016 um 07:15 schrieb Barret Rennie:
+--name::
+	Set the name for the worktree. If there is already a worktree with this
What is "the name for the worktree"? Is it the directory where it lives 
in? Is it how it is listed with 'git worktree list'?

How is --name different from the <path> argument?

-- Hannes

Re: [PATCH] builtin/worktree.c: add option for setting worktree name

From: Barret Rennie <hidden>
Date: 2016-06-25 07:29:56

What is "the name for the worktree"? Is it the directory where it lives in?
Is it how it is listed with 'git worktree list'?
The name of the worktree is the name of the created directory in
`.git/worktrees`.
How is --name different from the <path> argument?
Currently, if you run:
	
	git worktree add /my/worktree/checkout <branch>

you get a worktree "named" checkout, i.e., `.git/worktrees/checkout`. The
idea with this patch is to allow you use a more specific name when you would
otherwise have mulitiple worktrees of the form `checkout`, `checkout1`, etc.

That is, you could do

	git worktree add --name branch1 /worktrees/branch1/src branch1
	git worktree add --name branch2 /worktrees/branch2/src branch2
	git worktree add --name branch3 /worktrees/branch3/src branch3

and have `.git/worktrees/branch1`, `.git/worktrees/branch2` and
`.git/worktrees/branch3` instead of `.git/worktrees/src`,
`.git/worktrees/src1`, `.git/worktrees/src2`. That way, it becomes more clear
when poking inside `.git/worktrees` which directory points to which checkout.

Perhaps "worktree name" isn't the most clear nomenclature for this feature.
Would "worktree directory name" be better?

Re: [PATCH] builtin/worktree.c: add option for setting worktree name

From: Barret Rennie <hidden>
Date: 2016-06-25 07:30:00

What is "the name for the worktree"? Is it the directory where it lives in?
Is it how it is listed with 'git worktree list'?
The name of the worktree is the name of the created directory in
`.git/worktrees`.
How is --name different from the <path> argument?
Currently, if you run:
	
	git worktree add /my/worktree/checkout <branch>

you get a worktree "named" checkout, i.e., `.git/worktrees/checkout`. The
idea with this patch is to allow you use a more specific name when you would
otherwise have mulitiple worktrees of the form `checkout`, `checkout1`, etc.

That is, you could do

	git worktree add --name branch1 /worktrees/branch1/src branch1
	git worktree add --name branch2 /worktrees/branch2/src branch2
	git worktree add --name branch3 /worktrees/branch3/src branch3

and have `.git/worktrees/branch1`, `.git/worktrees/branch2` and
`.git/worktrees/branch3` instead of `.git/worktrees/src`,
`.git/worktrees/src1`, `.git/worktrees/src2`. That way, it becomes more clear
when poking inside `.git/worktrees` which directory points to which checkout.

Perhaps "worktree name" isn't the most clear nomenclature for this feature.
Would "worktree directory name" be better?

Re: [PATCH] builtin/worktree.c: add option for setting worktree name

From: Barret Rennie <hidden>
Date: 2016-06-25 07:33:06

Sorry for replying to that message twice. I hit a bug in Apple Mail.

--Barret
Keyboard shortcuts
hback out one level
jnext message in thread
kprevious message in thread
ldrill in
Escclose help / fold thread tree
?toggle this help