diff --git a/mercurial/commands.py b/mercurial/commands.py
index bcdfb6078b9f5bd34982ef8e2287c3837c033d92_bWVyY3VyaWFsL2NvbW1hbmRzLnB5..12dbdd348bb0977366200bf96cb6d2afa85faf13_bWVyY3VyaWFsL2NvbW1hbmRzLnB5 100644
--- a/mercurial/commands.py
+++ b/mercurial/commands.py
@@ -943,6 +943,9 @@
 
     Returns 0 on success.
     """
+    if label:
+        label = label.strip()
+
     if not opts.get('clean') and not label:
         ui.write("%s\n" % repo.dirstate.branch())
         return
diff --git a/tests/test-branches.t b/tests/test-branches.t
index bcdfb6078b9f5bd34982ef8e2287c3837c033d92_dGVzdHMvdGVzdC1icmFuY2hlcy50..12dbdd348bb0977366200bf96cb6d2afa85faf13_dGVzdHMvdGVzdC1icmFuY2hlcy50 100644
--- a/tests/test-branches.t
+++ b/tests/test-branches.t
@@ -68,6 +68,18 @@
   abort: '\n' cannot be used in a name
   [255]
 
+trailing or leading spaces should be stripped before testing duplicates
+
+  $ hg branch 'b '
+  abort: a branch of the same name already exists
+  (use 'hg update' to switch to it)
+  [255]
+
+  $ hg branch ' b'
+  abort: a branch of the same name already exists
+  (use 'hg update' to switch to it)
+  [255]
+
 verify update will accept invalid legacy branch names
 
   $ hg init test-invalid-branch-name