[PATCH 1 of 2 v2] branch: add --new option for explicitly creating new branches

Mads Kiilerich mads at kiilerich.com
Thu Apr 16 20:13:47 UTC 2015


# HG changeset patch
# User Mads Kiilerich <madski at unity3d.com>
# Date 1429215134 14400
#      Thu Apr 16 16:12:14 2015 -0400
# Node ID d8e2697b4ca385bfcf944b897dad28fb57e3bcd6
# Parent  c560d8c687916cb70a6d54c2c9ddcb5c9e457be2
branch: add --new option for explicitly creating new branches

Users could perhaps think that 'hg branch foo' would update to the head of foo.
It is more clear that 'hg branch --new foo' is creating a new branch, not
updating to an existing one.

diff --git a/mercurial/commands.py b/mercurial/commands.py
--- a/mercurial/commands.py
+++ b/mercurial/commands.py
@@ -1058,7 +1058,8 @@ def bookmark(ui, repo, *names, **opts):
 @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')),
+    ('n', 'new', '', _('mark working directory as creating new branch'))],
     _('[-fC] [NAME]'))
 def branch(ui, repo, label=None, **opts):
     """set or show the current branch name
@@ -1069,11 +1070,12 @@ def branch(ui, repo, label=None, **opts)
        light-weight bookmark instead. See :hg:`help glossary` for more
        information about named branches and bookmarks.
 
-    With no argument, show the current branch name. With one argument,
-    set the working directory branch name (the branch will not exist
-    in the repository until the next commit). Standard practice
-    recommends that primary development take place on the 'default'
-    branch.
+    With no argument, show the current branch name.
+
+    With no argument or --new, set the working directory branch name to create
+    a new branch (the branch will not exist in the repository until the next
+    commit). Standard practice recommends that primary development take place
+    on the 'default' branch.
 
     Unless -f/--force is specified, branch will not let you set a
     branch name that already exists.
@@ -1087,6 +1089,12 @@ def branch(ui, repo, label=None, **opts)
 
     Returns 0 on success.
     """
+    new = opts.get('new')
+    if new:
+        if label:
+            raise util.Abort(_("--new only takes one branch name"))
+        label = new
+
     if label:
         label = label.strip()
 
diff --git a/tests/test-branches.t b/tests/test-branches.t
--- a/tests/test-branches.t
+++ b/tests/test-branches.t
@@ -11,7 +11,11 @@
   (branches are permanent and global, did you want a bookmark?)
   $ hg commit -d '1 0' -m "Adding a branch"
 
-  $ hg branch q
+  $ hg branch --new q q
+  abort: --new only takes one branch name
+  [255]
+
+  $ hg branch --new q
   marked working directory as branch q
   (branches are permanent and global, did you want a bookmark?)
   $ echo 'aa' >a
diff --git a/tests/test-completion.t b/tests/test-completion.t
--- a/tests/test-completion.t
+++ b/tests/test-completion.t
@@ -221,7 +221,7 @@ Show all commands + options
   backout: merge, commit, parent, rev, edit, tool, include, exclude, message, logfile, date, user
   bisect: reset, good, bad, skip, extend, command, noupdate
   bookmarks: force, rev, delete, rename, inactive, template
-  branch: force, clean
+  branch: force, clean, new
   branches: active, closed, template
   bundle: force, rev, branch, base, all, type, ssh, remotecmd, insecure
   cat: output, rev, decode, include, exclude


More information about the Mercurial-devel mailing list