Patchwork [stable] branch: make --force work even when specifying revs

login
register
mail settings
Submitter Manuel Jacob
Date March 1, 2020, 6:41 p.m.
Message ID <1744b28471b78764a1a7.1583088078@tmp>
Download mbox | patch
Permalink /patch/45414/
State Accepted
Headers show

Comments

Manuel Jacob - March 1, 2020, 6:41 p.m.
# HG changeset patch
# User Manuel Jacob <me@manueljacob.de>
# Date 1583087963 -3600
#      Sun Mar 01 19:39:23 2020 +0100
# Branch stable
# Node ID 1744b28471b78764a1a75490c24471140681e680
# Parent  bb58931d0c4f8e3b28ad6764fb8d6b66757f9577
branch: make --force work even when specifying revs

The `hg branch` command accepts a `--force` parameter that allows to
"set branch name even if it shadows an existing branch". However, before this
patch, that didn’t work when specifying revs with `-r`.
Yuya Nishihara - March 2, 2020, 2:24 p.m.
On Sun, 01 Mar 2020 19:41:18 +0100, Manuel Jacob wrote:
> # HG changeset patch
> # User Manuel Jacob <me@manueljacob.de>
> # Date 1583087963 -3600
> #      Sun Mar 01 19:39:23 2020 +0100
> # Branch stable
> # Node ID 1744b28471b78764a1a75490c24471140681e680
> # Parent  bb58931d0c4f8e3b28ad6764fb8d6b66757f9577
> branch: make --force work even when specifying revs

Queued for stable, thanks.

Patch

diff --git a/mercurial/cmdutil.py b/mercurial/cmdutil.py
--- a/mercurial/cmdutil.py
+++ b/mercurial/cmdutil.py
@@ -961,7 +961,7 @@ 
     raise error.UnknownCommand(cmd, allcmds)
 
 
-def changebranch(ui, repo, revs, label):
+def changebranch(ui, repo, revs, label, opts):
     """ Change the branch name of given revs to label """
 
     with repo.wlock(), repo.lock(), repo.transaction(b'branches'):
@@ -979,7 +979,11 @@ 
 
         root = repo[roots.first()]
         rpb = {parent.branch() for parent in root.parents()}
-        if label not in rpb and label in repo.branchmap():
+        if (
+            not opts.get(b'force')
+            and label not in rpb
+            and label in repo.branchmap()
+        ):
             raise error.Abort(_(b"a branch of the same name already exists"))
 
         if repo.revs(b'obsolete() and %ld', revs):
diff --git a/mercurial/commands.py b/mercurial/commands.py
--- a/mercurial/commands.py
+++ b/mercurial/commands.py
@@ -1338,7 +1338,7 @@ 
 
             scmutil.checknewlabel(repo, label, b'branch')
             if revs:
-                return cmdutil.changebranch(ui, repo, revs, label)
+                return cmdutil.changebranch(ui, repo, revs, label, opts)
 
             if not opts.get(b'force') and label in repo.branchmap():
                 if label not in [p.branch() for p in repo[None].parents()]:
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
@@ -355,6 +355,13 @@ 
   abort: a branch of the same name already exists
   [255]
 
+  $ hg branch -r . stable --force
+  changed branch on 1 changesets
+  $ hg branches
+  stable                        34:d1c2addda4a2
+  jkl                           29:6bc1c6c2c9da (inactive)
+  ghi                           28:2f1019bd29d2 (inactive)
+
 Changing branch on public changeset
 
   $ hg phase -r . -p