branch: allow changing branch name to existing name if possible
authorPulkit Goyal <7895pulkit@gmail.com>
Fri, 19 Jan 2018 18:45:20 +0530
changeset 35746 e5b6ba786d83
parent 35745 3bd8ab4c80a5
child 35747 de32acb24949
branch: allow changing branch name to existing name if possible With the functionality added in previous patch we can change branches of a revision but not everytime even if it's possible to do so. For example cosider the following case: o 3 added a (foo) o 2 added b (foo) o 1 added c (bar) o 0 added d (bar) Here if I want to change the branch of rev 2,3 to bar, it was not possible and it will say, "a branch with same name exists". This patch allows us to change branch of 2,3 to bar. The underlying logic for changing branch finds the changesets from the revs passed which have no parents in revs. We only support revsets which have only one such root, so to support this we check whether the parent of the root has the same name as that of the new name and if so, we can use the new name to change branches. Differential Revision: https://phab.mercurial-scm.org/D1913
mercurial/cmdutil.py
mercurial/commands.py
tests/test-branch-change.t
--- a/mercurial/cmdutil.py	Sun Oct 15 23:08:45 2017 +0530
+++ b/mercurial/cmdutil.py	Fri Jan 19 18:45:20 2018 +0530
@@ -727,6 +727,11 @@
         if len(roots) > 1:
             raise error.Abort(_("cannot change branch of non-linear revisions"))
         rewriteutil.precheck(repo, revs, 'change branch of')
+
+        root = repo[roots.first()]
+        if not root.p1().branch() == label and label in repo.branchmap():
+            raise error.Abort(_("a branch of the same name already exists"))
+
         if repo.revs('merge() and %ld', revs):
             raise error.Abort(_("cannot change branch of a merge commit"))
         if repo.revs('obsolete() and %ld', revs):
--- a/mercurial/commands.py	Sun Oct 15 23:08:45 2017 +0530
+++ b/mercurial/commands.py	Fri Jan 19 18:45:20 2018 +0530
@@ -1055,11 +1055,6 @@
 
             scmutil.checknewlabel(repo, label, 'branch')
             if revs:
-                # XXX: we should allow setting name to existing branch if the
-                # branch of root of the revs is same as the new branch name
-                if label in repo.branchmap():
-                    raise error.Abort(_('a branch of the same'
-                                        ' name already exists'))
                 return cmdutil.changebranch(ui, repo, revs, label)
 
             if not opts.get('force') and label in repo.branchmap():
--- a/tests/test-branch-change.t	Sun Oct 15 23:08:45 2017 +0530
+++ b/tests/test-branch-change.t	Fri Jan 19 18:45:20 2018 +0530
@@ -267,15 +267,49 @@
   $ hg branch
   stable
 
-Changing to same branch name does not work
+Changing to same branch is no-op
 
   $ hg branch -r 19::21 stable
-  abort: a branch of the same name already exists
-  [255]
+  changed branch on 0 changesets
+
+Changing branch name to existing branch name if the branch of parent of root of
+revs is same as the new branch name
+
+  $ hg branch -r 20::21 bugfix
+  changed branch on 2 changesets
+  $ hg glog
+  o  25:714defe1cf34 Added d
+  |  bugfix ()
+  o  24:98394def28fc Added c
+  |  bugfix ()
+  | @  23:6a5ddbcfb870 added bar
+  | |  stable (b1)
+  | o  22:baedc6e98a67 Added e
+  |/   stable ()
+  o  19:fd45b986b109 Added b
+  |  stable ()
+  o  18:204d2769eca2 Added a
+     stable ()
+
+  $ hg branch -r 24:25 stable
+  changed branch on 2 changesets
+  $ hg glog
+  o  27:4ec342341562 Added d
+  |  stable ()
+  o  26:83f48859c2de Added c
+  |  stable ()
+  | @  23:6a5ddbcfb870 added bar
+  | |  stable (b1)
+  | o  22:baedc6e98a67 Added e
+  |/   stable ()
+  o  19:fd45b986b109 Added b
+  |  stable ()
+  o  18:204d2769eca2 Added a
+     stable ()
 
 Testing on merge
 
-  $ hg merge -r 20
+  $ hg merge -r 26
   1 files updated, 0 files merged, 0 files removed, 0 files unresolved
   (branch merge, don't forget to commit)
 
@@ -289,8 +323,8 @@
 
 Changing branch on public changeset
 
-  $ hg phase -r 21 -p
-  $ hg branch -r 21 def
+  $ hg phase -r 27 -p
+  $ hg branch -r 27 def
   abort: cannot change branch of public changesets
   (see 'hg help phases' for details)
   [255]