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

login
register
mail settings
Submitter phabricator
Date Jan. 19, 2018, 8:06 p.m.
Message ID <265da8e7c6b7709754a6bd7b48ad12eb@localhost.localdomain>
Download mbox | patch
Permalink /patch/26953/
State Not Applicable
Headers show

Comments

phabricator - Jan. 19, 2018, 8:06 p.m.
This revision was automatically updated to reflect the committed changes.
Closed by commit rHGe5b6ba786d83: branch: allow changing branch name to existing name if possible (authored by pulkit, committed by ).

REPOSITORY
  rHG Mercurial

CHANGES SINCE LAST UPDATE
  https://phab.mercurial-scm.org/D1913?vs=4936&id=4945

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

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

CHANGE DETAILS




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

Patch

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):