git@vger.kernel.org mailing list mirror (one of many)
 help / color / mirror / code / Atom feed
* "Branch exists" error while trying to rename a non-existing branch to an existing one
@ 2017-07-09 18:27 Kaartic Sivaraam
  2017-07-09 18:57 ` Junio C Hamano
  0 siblings, 1 reply; 3+ messages in thread
From: Kaartic Sivaraam @ 2017-07-09 18:27 UTC (permalink / raw)
  To: git

Hello all,

I recently got the following error message by change as a result of the
command,

    $ git branch -m no-branch master
    fatal: A branch named 'master' already exists.

Note: no-branch is an hypothetical branch that doesn't exist.

Shouldn't I get a 'no-branch' doesn't exist before that? Wouldn't this
behaviour make the users search for the non-existing 'no-branch' in
their repo?

I tried digging the implementation a little and what I could interpret
from it is,

    * only the validity of new branch name (master, in the above case)
    is checked
    * checking for existence of the branch being renamed(no-branch) is
    not done at all. It seems to be left to the lower level commands to
    identify.

I'm puzzled by seeing this. Why isn't there any check for the existence
of the branch being renamed and warning the user about that first?

-- 
Kaartic

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: "Branch exists" error while trying to rename a non-existing branch to an existing one
  2017-07-09 18:27 "Branch exists" error while trying to rename a non-existing branch to an existing one Kaartic Sivaraam
@ 2017-07-09 18:57 ` Junio C Hamano
  2017-07-10 14:14   ` Kaartic Sivaraam
  0 siblings, 1 reply; 3+ messages in thread
From: Junio C Hamano @ 2017-07-09 18:57 UTC (permalink / raw)
  To: Kaartic Sivaraam; +Cc: git

Kaartic Sivaraam <kaarticsivaraam91196@gmail.com> writes:

> I recently got the following error message by change as a result of the
> command,
>
>     $ git branch -m no-branch master
>     fatal: A branch named 'master' already exists.
>
> Note: no-branch is an hypothetical branch that doesn't exist.
>
> Shouldn't I get a 'no-branch' doesn't exist before that?

This is borderline "meh" at least to me.  An argument against a
hypothetical version of Git that "fixes" your issue would be that no
matter what the source of renaming is, as long as 'master' exists,
"branch -m" shouldn't overwrite it, and it is a good thing to remind
the user that 'master' exists and the user meant to rename it to
something else.

Of course you can make the error message three-way, but I do not
think it is worth it.

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: "Branch exists" error while trying to rename a non-existing branch to an existing one
  2017-07-09 18:57 ` Junio C Hamano
@ 2017-07-10 14:14   ` Kaartic Sivaraam
  0 siblings, 0 replies; 3+ messages in thread
From: Kaartic Sivaraam @ 2017-07-10 14:14 UTC (permalink / raw)
  To: Junio C Hamano; +Cc: git

On Sun, 2017-07-09 at 11:57 -0700, Junio C Hamano wrote:
> This is borderline "meh" at least to me.  An argument against a
> hypothetical version of Git that "fixes" your issue would be that no
> matter what the source of renaming is, as long as 'master' exists,
> "branch -m" shouldn't overwrite it, and it is a good thing to remind
> the user that 'master' exists and the user meant to rename it to
> something else.
> 
I'm not against the fact of reminding the user about an existing
branch. I'm with the fact that, warn him when he really has to care
about a branch being overwritten i.e., when he tries to rename an
"existing" branch to one that refers to another existing branch.

I found this behaviour odd as I try to relate it with the 'mv' command.
It's behaviour is as follows,

    $ ls
    file  some_file
    $ mv nothing file
    mv: cannot stat 'nothing': No such file or directory

If I haven't missed anything the following patch seems to fix the
problem,

diff --git a/builtin/branch.c b/builtin/branch.c
index 48a513a84..2869aaca8 100644
--- a/builtin/branch.c
+++ b/builtin/branch.c
@@ -476,7 +476,10 @@ static void rename_branch(const char *oldname, const char *newname, int force)
         */
        clobber_head_ok = !strcmp(oldname, newname);
 
-       validate_new_branchname(newname, &newref, force, clobber_head_ok);
+       if(ref_exists(oldref.buf))
+               validate_new_branchname(newname, &newref, force, clobber_head_ok);
+       else
+               die(_("Branch '%s' does not exist."), oldname);
 
        reject_rebase_or_bisect_branch(oldref.buf);


-- 
Kaartic

^ permalink raw reply related	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2017-07-10 14:14 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2017-07-09 18:27 "Branch exists" error while trying to rename a non-existing branch to an existing one Kaartic Sivaraam
2017-07-09 18:57 ` Junio C Hamano
2017-07-10 14:14   ` Kaartic Sivaraam

Code repositories for project(s) associated with this public inbox

	https://80x24.org/mirrors/git.git

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 read-only IMAP folder(s) and NNTP newsgroup(s).