improve --branch processing (and differentiate from # syntax)
authorSune Foldager <cryo@cyanite.org>
Thu, 10 Jun 2010 12:46:09 +0200
changeset 11322 3d6915f5a2bb
parent 11321 40c06bbf58be
child 11323 d65b74106113
improve --branch processing (and differentiate from # syntax) Previously #foo and --branch foo were handled identically. The behavior of #foo hasn't changed, but --branch now works like this: 1) If branchmap is not supported on the remote, the operation fails. 2) If branch is '.', substitute with branch of the working dir parent. 3) If branch exists remotely, its heads are expanded. 4) Otherwise, the operation fails. Tests have been added for the new cases.
mercurial/hg.py
tests/test-branch-option
tests/test-branch-option.out
tests/test-hg-parseurl.py.out
--- a/mercurial/hg.py	Sun Jun 06 17:25:00 2010 +0900
+++ b/mercurial/hg.py	Thu Jun 10 12:46:09 2010 +0200
@@ -19,34 +19,45 @@
     return (os.path.isfile(path) and bundlerepo or localrepo)
 
 def addbranchrevs(lrepo, repo, branches, revs):
-    if not branches:
+    hashbranch, branches = branches
+    if not hashbranch and not branches:
         return revs or None, revs and revs[0] or None
     revs = revs and list(revs) or []
     if not repo.capable('branchmap'):
-        revs.extend(branches)
+        if branches:
+            raise util.Abort(_("remote branch lookup not supported"))
+        revs.append(hashbranch)
         return revs, revs[0]
     branchmap = repo.branchmap()
-    for branch in branches:
-        if branch == '.':
+
+    def primary(butf8):
+        if butf8 == '.':
             if not lrepo or not lrepo.local():
                 raise util.Abort(_("dirstate branch not accessible"))
             butf8 = lrepo.dirstate.branch()
-            branch = encoding.tolocal(butf8)
-        else:
-            butf8 = encoding.fromlocal(branch)
         if butf8 in branchmap:
             revs.extend(node.hex(r) for r in reversed(branchmap[butf8]))
+            return True
         else:
-            revs.append(branch)
+            return False
+
+    for branch in branches:
+        butf8 = encoding.fromlocal(branch)
+        if not primary(butf8):
+            raise error.RepoLookupError(_("unknown branch '%s'") % branch)
+    if hashbranch:
+        butf8 = encoding.fromlocal(hashbranch)
+        if not primary(butf8):
+            revs.append(hashbranch)
     return revs, revs[0]
 
 def parseurl(url, branches=None):
-    '''parse url#branch, returning url, branches+[branch]'''
+    '''parse url#branch, returning (url, (branch, branches))'''
 
     if '#' not in url:
-        return url, branches or []
+        return url, (None, branches or [])
     url, branch = url.split('#', 1)
-    return url, (branches or []) + [branch]
+    return url, (branch, branches or [])
 
 schemes = {
     'bundle': bundlerepo,
--- a/tests/test-branch-option	Sun Jun 06 17:25:00 2010 +0900
+++ b/tests/test-branch-option	Thu Jun 10 12:46:09 2010 +0200
@@ -12,6 +12,7 @@
 hg branch c
 echo c > foo
 hg ci -d '0 0' -mc
+hg tag -l z
 cd ..
 hg clone -r 0 branch branch2
 cd branch2
@@ -32,6 +33,10 @@
 echo b2 > foo
 hg ci -d '0 0' -mb2
 
+echo unknown branch and fallback
+hg in -qbz
+hg in -q ../branch#z
+hg out -qbz
 echo in rev c branch a
 hg in -qr c ../branch#a
 hg in -qr c -b a
--- a/tests/test-branch-option.out	Sun Jun 06 17:25:00 2010 +0900
+++ b/tests/test-branch-option.out	Thu Jun 10 12:46:09 2010 +0200
@@ -15,9 +15,14 @@
 marked working directory as branch æ
 1 files updated, 0 files merged, 0 files removed, 0 files unresolved
 marked working directory as branch æ
+created new head
 1 files updated, 0 files merged, 0 files removed, 0 files unresolved
 marked working directory as branch b
 created new head
+unknown branch and fallback
+abort: unknown branch 'z'!
+2:f25d57ab0566
+abort: unknown branch 'z'!
 in rev c branch a
 1:dd6e60a716c6
 2:f25d57ab0566
--- a/tests/test-hg-parseurl.py.out	Sun Jun 06 17:25:00 2010 +0900
+++ b/tests/test-hg-parseurl.py.out	Thu Jun 10 12:46:09 2010 +0200
@@ -1,5 +1,5 @@
-http://example.com/no/anchor, branches: []
-http://example.com/an/anchor, branches: ['foo']
-http://example.com/no/anchor/branches, branches: ['foo']
-http://example.com/an/anchor/branches, branches: ['foo', 'bar']
-http://example.com/an/anchor/branches-None, branches: ['foo']
+http://example.com/no/anchor, branches: (None, [])
+http://example.com/an/anchor, branches: ('foo', [])
+http://example.com/no/anchor/branches, branches: (None, ['foo'])
+http://example.com/an/anchor/branches, branches: ('bar', ['foo'])
+http://example.com/an/anchor/branches-None, branches: ('foo', [])