* [funny] "git checkout -t origin/xyzzy" seems to misbehave
@ 2008-09-21 8:23 Junio C Hamano
2008-09-21 17:44 ` Alex Riesen
2008-09-21 18:36 ` Daniel Barkalow
0 siblings, 2 replies; 3+ messages in thread
From: Junio C Hamano @ 2008-09-21 8:23 UTC (permalink / raw)
To: git; +Cc: Johannes Schindelin, Daniel Barkalow
When you
* are on a branch "foo" that is not "bar",
* have "origin/bar",
* and already have a local branch "bar",
"git checkout -t origin/bar" seems to misbehave.
$ git clone -s git.junio victim-002
$ cd victim-002
$ git branch
* master
$ git checkout -t origin/next
Branch next set up to track remote branch refs/remotes/origin/next.
Switched to a new branch "next"
$ git checkout -t origin/master
fatal: A branch named 'master' already exists.
$ git branch
master
* next
$ git diff --cached --shortstat
60 files changed, 2378 insertions(+), 3412 deletions(-)
$ git diff --cached master
$ exit
The first "checkout -t" is fine. The failed one seems to have already
updated the index and the work tree when it notices that it cannot create
a new branch.
I suspect "-t" does not have to be in effect to trigger this; in other
words, "git checkout -b master origin/master" would have the same issue.
I'm reporting this before digging it further myself, because I may not be
able to diagnose this before I leave for a vacation.
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [funny] "git checkout -t origin/xyzzy" seems to misbehave
2008-09-21 8:23 [funny] "git checkout -t origin/xyzzy" seems to misbehave Junio C Hamano
@ 2008-09-21 17:44 ` Alex Riesen
2008-09-21 18:36 ` Daniel Barkalow
1 sibling, 0 replies; 3+ messages in thread
From: Alex Riesen @ 2008-09-21 17:44 UTC (permalink / raw)
To: Junio C Hamano; +Cc: git, Johannes Schindelin, Daniel Barkalow
Junio C Hamano, Sun, Sep 21, 2008 10:23:00 +0200:
> When you
>
> * are on a branch "foo" that is not "bar",
> * have "origin/bar",
> * and already have a local branch "bar",
>
> "git checkout -t origin/bar" seems to misbehave.
>
> $ git clone -s git.junio victim-002
> $ cd victim-002
> $ git branch
> * master
> $ git checkout -t origin/next
> Branch next set up to track remote branch refs/remotes/origin/next.
> Switched to a new branch "next"
> $ git checkout -t origin/master
> fatal: A branch named 'master' already exists.
> $ git branch
> master
> * next
> $ git diff --cached --shortstat
> 60 files changed, 2378 insertions(+), 3412 deletions(-)
> $ git diff --cached master
> $ exit
>
> The first "checkout -t" is fine. The failed one seems to have already
> updated the index and the work tree when it notices that it cannot create
> a new branch.
Precisely this (branch already exists) case is easy to handle with a
resolve_ref in builtin-checkout.c:switch_branches.
The other errors will still leave index and working tree in this
state: branch.c:create_branch does not cleanup in case of errors,
it just dies.
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [funny] "git checkout -t origin/xyzzy" seems to misbehave
2008-09-21 8:23 [funny] "git checkout -t origin/xyzzy" seems to misbehave Junio C Hamano
2008-09-21 17:44 ` Alex Riesen
@ 2008-09-21 18:36 ` Daniel Barkalow
1 sibling, 0 replies; 3+ messages in thread
From: Daniel Barkalow @ 2008-09-21 18:36 UTC (permalink / raw)
To: Junio C Hamano; +Cc: git, Johannes Schindelin
On Sun, 21 Sep 2008, Junio C Hamano wrote:
> When you
>
> * are on a branch "foo" that is not "bar",
> * have "origin/bar",
> * and already have a local branch "bar",
>
> "git checkout -t origin/bar" seems to misbehave.
It's always been the case if the thing that fails is changing the ref for
HEAD, you're left with the index and working tree changed but the ref
unchanged. OTOH, the C conversion lost the early checks that a new branch
name is plausible in advance of trying anything.
commit c36a025b20ac752f8960bc36dcbab98ca1824657
Author: Daniel Barkalow <barkalow@iabervon.org>
Date: Sun Sep 21 14:25:31 2008 -0400
Check early that a new branch is new and valid
If you fail to update refs to change branches in checkout, your index
and working tree are left already updated. We don't have an easy way
to undo this, but at least we can check things that would make the
creation of a new branch fail. These checks were in the shell version,
and were lost in the C conversion.
The messages are from the shell version, and should probably be made nicer.
Signed-off-by: Daniel Barkalow <barkalow@iabervon.org>
diff --git a/builtin-checkout.c b/builtin-checkout.c
index 9377a1c..4497b70 100644
--- a/builtin-checkout.c
+++ b/builtin-checkout.c
@@ -580,6 +580,18 @@ no_reference:
return checkout_paths(source_tree, pathspec);
}
+ if (opts.new_branch) {
+ struct strbuf buf;
+ strbuf_init(&buf, 0);
+ strbuf_addstr(&buf, "refs/heads/");
+ strbuf_addstr(&buf, opts.new_branch);
+ if (!get_sha1(buf.buf, rev))
+ die("git checkout: branch %s already exists", opts.new_branch);
+ if (check_ref_format(buf.buf))
+ die("git checkout: we do not like '%s' as a branch name.", opts.new_branch);
+ strbuf_release(&buf);
+ }
+
if (new.name && !new.commit) {
die("Cannot switch branch to a non-commit.");
}
^ permalink raw reply related [flat|nested] 3+ messages in thread
end of thread, other threads:[~2008-09-22 17:56 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2008-09-21 8:23 [funny] "git checkout -t origin/xyzzy" seems to misbehave Junio C Hamano
2008-09-21 17:44 ` Alex Riesen
2008-09-21 18:36 ` Daniel Barkalow
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).