{"thread":{"id":"46338","subject":"\"Branch exists\" error while trying to rename a non-existing branch to an existing one","startedAt":"2017-07-09T18:27:38Z","lastAt":"2017-07-10T14:14:28Z","messageCount":3,"participants":["Kaartic Sivaraam","Junio C Hamano"],"isPatch":false,"patchVersion":null,"patchTotal":null},"messages":[{"id":"324038","messageId":"1499624859.8552.9.camel@gmail.com","threadId":"46338","inReplyTo":null,"subject":"\"Branch exists\" error while trying to rename a non-existing branch to an existing one","fromName":"Kaartic Sivaraam","fromEmail":"kaarticsivaraam91196@gmail.com","sentAt":"2017-07-09T18:27:39Z","receivedAt":"2017-07-09T18:27:38Z","isPatch":false,"sender":{"key":"kaartic.sivaraam@gmail.com","avatar":"https://avatars.githubusercontent.com/u/12448084?v=4"},"body":"Hello all,\n\nI recently got the following error message by change as a result of the\ncommand,\n\n    $ git branch -m no-branch master\n    fatal: A branch named 'master' already exists.\n\nNote: no-branch is an hypothetical branch that doesn't exist.\n\nShouldn't I get a 'no-branch' doesn't exist before that? Wouldn't this\nbehaviour make the users search for the non-existing 'no-branch' in\ntheir repo?\n\nI tried digging the implementation a little and what I could interpret\nfrom it is,\n\n    * only the validity of new branch name (master, in the above case)\n    is checked\n    * checking for existence of the branch being renamed(no-branch) is\n    not done at all. It seems to be left to the lower level commands to\n    identify.\n\nI'm puzzled by seeing this. Why isn't there any check for the existence\nof the branch being renamed and warning the user about that first?\n\n-- \nKaartic\n"},{"id":"324041","messageId":"xmqqd1998mks.fsf@gitster.mtv.corp.google.com","threadId":"46338","inReplyTo":"1499624859.8552.9.camel@gmail.com","subject":"Re: \"Branch exists\" error while trying to rename a non-existing branch to an existing one","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2017-07-09T18:57:55Z","receivedAt":"2017-07-09T18:58:01Z","isPatch":false,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Kaartic Sivaraam <kaarticsivaraam91196@gmail.com> writes:\n\n> I recently got the following error message by change as a result of the\n> command,\n>\n>     $ git branch -m no-branch master\n>     fatal: A branch named 'master' already exists.\n>\n> Note: no-branch is an hypothetical branch that doesn't exist.\n>\n> Shouldn't I get a 'no-branch' doesn't exist before that?\n\nThis is borderline \"meh\" at least to me.  An argument against a\nhypothetical version of Git that \"fixes\" your issue would be that no\nmatter what the source of renaming is, as long as 'master' exists,\n\"branch -m\" shouldn't overwrite it, and it is a good thing to remind\nthe user that 'master' exists and the user meant to rename it to\nsomething else.\n\nOf course you can make the error message three-way, but I do not\nthink it is worth it.\n"},{"id":"324065","messageId":"1499696071.1647.2.camel@gmail.com","threadId":"46338","inReplyTo":"xmqqd1998mks.fsf@gitster.mtv.corp.google.com","subject":"Re: \"Branch exists\" error while trying to rename a non-existing branch to an existing one","fromName":"Kaartic Sivaraam","fromEmail":"kaarticsivaraam91196@gmail.com","sentAt":"2017-07-10T14:14:31Z","receivedAt":"2017-07-10T14:14:28Z","isPatch":false,"sender":{"key":"kaartic.sivaraam@gmail.com","avatar":"https://avatars.githubusercontent.com/u/12448084?v=4"},"body":"On Sun, 2017-07-09 at 11:57 -0700, Junio C Hamano wrote:\n> This is borderline \"meh\" at least to me.  An argument against a\n> hypothetical version of Git that \"fixes\" your issue would be that no\n> matter what the source of renaming is, as long as 'master' exists,\n> \"branch -m\" shouldn't overwrite it, and it is a good thing to remind\n> the user that 'master' exists and the user meant to rename it to\n> something else.\n> \nI'm not against the fact of reminding the user about an existing\nbranch. I'm with the fact that, warn him when he really has to care\nabout a branch being overwritten i.e., when he tries to rename an\n\"existing\" branch to one that refers to another existing branch.\n\nI found this behaviour odd as I try to relate it with the 'mv' command.\nIt's behaviour is as follows,\n\n    $ ls\n    file  some_file\n    $ mv nothing file\n    mv: cannot stat 'nothing': No such file or directory\n\nIf I haven't missed anything the following patch seems to fix the\nproblem,\n\ndiff --git a/builtin/branch.c b/builtin/branch.c\nindex 48a513a84..2869aaca8 100644\n--- a/builtin/branch.c\n+++ b/builtin/branch.c\n@@ -476,7 +476,10 @@ static void rename_branch(const char *oldname, const char *newname, int force)\n         */\n        clobber_head_ok = !strcmp(oldname, newname);\n \n-       validate_new_branchname(newname, &newref, force, clobber_head_ok);\n+       if(ref_exists(oldref.buf))\n+               validate_new_branchname(newname, &newref, force, clobber_head_ok);\n+       else\n+               die(_(\"Branch '%s' does not exist.\"), oldname);\n \n        reject_rebase_or_bisect_branch(oldref.buf);\n\n\n-- \nKaartic\n"}]}