diff mercurial/commands.py @ 35745:3bd8ab4c80a5

branch: add a --rev flag to change branch name of given revisions This patch adds a new --rev flag to hg branch which can be used to change branch of revisions. This is motivated from topic extension where you can change topic on revisions but this one has few restrictions which are: 1) You cannot change branch name in between the stack 2) You cannot change branch name and set it to an existing name 3) You cannot change branch of non-linear set of commits 4) You cannot change branch of merge commits. Tests are added for the same. .. feature:: An experimental flag `--rev` to `hg branch` which can be used to change branch of changesets. Differential Revision: https://phab.mercurial-scm.org/D1074
author Pulkit Goyal <7895pulkit@gmail.com>
date Sun, 15 Oct 2017 23:08:45 +0530
parents 35a0f6f31eef
children e5b6ba786d83
line wrap: on
line diff
--- a/mercurial/commands.py	Tue Jan 16 23:50:01 2018 +0900
+++ b/mercurial/commands.py	Sun Oct 15 23:08:45 2017 +0530
@@ -1002,7 +1002,9 @@
 @command('branch',
     [('f', 'force', None,
      _('set branch name even if it shadows an existing branch')),
-    ('C', 'clean', None, _('reset branch name to parent branch name'))],
+     ('C', 'clean', None, _('reset branch name to parent branch name')),
+     ('r', 'rev', [], _('change branches of the given revs (EXPERIMENTAL)')),
+    ],
     _('[-fC] [NAME]'))
 def branch(ui, repo, label=None, **opts):
     """set or show the current branch name
@@ -1034,10 +1036,13 @@
     Returns 0 on success.
     """
     opts = pycompat.byteskwargs(opts)
+    revs = opts.get('rev')
     if label:
         label = label.strip()
 
     if not opts.get('clean') and not label:
+        if revs:
+            raise error.Abort(_("no branch name specified for the revisions"))
         ui.write("%s\n" % repo.dirstate.branch())
         return
 
@@ -1047,13 +1052,23 @@
             repo.dirstate.setbranch(label)
             ui.status(_('reset working directory to branch %s\n') % label)
         elif label:
+
+            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():
                 if label not in [p.branch() for p in repo[None].parents()]:
                     raise error.Abort(_('a branch of the same name already'
                                        ' exists'),
                                      # i18n: "it" refers to an existing branch
                                      hint=_("use 'hg update' to switch to it"))
-            scmutil.checknewlabel(repo, label, 'branch')
+
             repo.dirstate.setbranch(label)
             ui.status(_('marked working directory as branch %s\n') % label)