D1913: branch: allow changing branch name to existing name if possible

pulkit (Pulkit Goyal) phabricator at mercurial-scm.org
Fri Jan 19 14:40:16 UTC 2018


pulkit created this revision.
Herald added a subscriber: mercurial-devel.
Herald added a reviewer: hg-reviewers.

REVISION SUMMARY
  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.

REPOSITORY
  rHG Mercurial

REVISION DETAIL
  https://phab.mercurial-scm.org/D1913

AFFECTED FILES
  mercurial/cmdutil.py
  mercurial/commands.py
  tests/test-branch-change.t

CHANGE DETAILS

diff --git a/tests/test-branch-change.t b/tests/test-branch-change.t
--- a/tests/test-branch-change.t
+++ b/tests/test-branch-change.t
@@ -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]
diff --git a/mercurial/commands.py b/mercurial/commands.py
--- a/mercurial/commands.py
+++ b/mercurial/commands.py
@@ -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():
diff --git a/mercurial/cmdutil.py b/mercurial/cmdutil.py
--- a/mercurial/cmdutil.py
+++ b/mercurial/cmdutil.py
@@ -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):



To: pulkit, #hg-reviewers
Cc: mercurial-devel


More information about the Mercurial-devel mailing list