Re: git worktree fails to recreate existing branch even with -B

2016-02-15 Thread Duy Nguyen
On Tue, Feb 09, 2016 at 05:54:01PM +0300, Kirill Likhodedov wrote:
> Git doesn’t allow me to execute 
> git worktree add -B   
> where  already exists in the repository.
> 
> The command prints the following:
> Preparing  (identifier )
> fatal: Refusing to point HEAD outside of refs/
> 
> I’m trying to create a worktree on an existing branch ,
> which should point to . This obviously should fail with
> “-b”, but I use “-B” and expect it to be reset to  as
> mentioned in the docs:
> 
> By default, -b refuses to create a new branch if it already exists.  
> -B overrides this safeguard, resetting  to .
> 
> Do I miss something or there is a bug?

According to the man page, this looks like a bug.

> Steps to reproduce:
> 
> git init wt
> cd wt
> echo 'asd' > a.txt ; git add a.txt ; git commit -m initial
> git branch br1
> git worktree add -B br1 ~/temp/br1 master
> 
> Error message:
> Preparing /Users/loki/temp/br1 (identifier br1)
> fatal: Refusing to point HEAD outside of refs/

GIT_TRACE=2 gives me

trace: built-in: git 'symbolic-ref' 'HEAD' ''
fatal: Refusing to point HEAD outside of refs/

So we pass wrong argument to symbolic-ref. The '' should be
'refs/heads/br1'. This patch seems to fix it.

-- 8< --
diff --git a/builtin/worktree.c b/builtin/worktree.c
index 475b958..d5b319f 100644
--- a/builtin/worktree.c
+++ b/builtin/worktree.c
@@ -202,7 +202,7 @@ static int add_worktree(const char *path, const char 
*refname,
 
/* is 'refname' a branch or commit? */
if (opts->force_new_branch) /* definitely a branch */
-   ;
+   strbuf_addf(, "refs/heads/%s", refname);
else if (!opts->detach && !strbuf_check_branch_ref(, refname) &&
 ref_exists(symref.buf)) { /* it's a branch */
if (!opts->force)
-- 8< --
--
Duy
--
To unsubscribe from this list: send the line "unsubscribe git" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html


git worktree fails to recreate existing branch even with -B

2016-02-09 Thread Kirill Likhodedov
Git doesn’t allow me to execute 
git worktree add -B   
where  already exists in the repository.

The command prints the following:
Preparing  (identifier )
fatal: Refusing to point HEAD outside of refs/

I’m trying to create a worktree on an existing branch , which should 
point to . This obviously should fail with “-b”, but I use “-B” 
and expect it to be reset to  as mentioned in the docs:

By default, -b refuses to create a new branch if it already exists.  
-B overrides this safeguard, resetting  to .

Do I miss something or there is a bug?

Steps to reproduce:

git init wt
cd wt
echo 'asd' > a.txt ; git add a.txt ; git commit -m initial
git branch br1
git worktree add -B br1 ~/temp/br1 master

Error message:
Preparing /Users/loki/temp/br1 (identifier br1)
fatal: Refusing to point HEAD outside of refs/

Thanks a lot!
-- Kirill--
To unsubscribe from this list: send the line "unsubscribe git" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html