[PATCH] identify: restructure code to make it more readable

Idan Kamara idankk86 at gmail.com
Mon Apr 18 17:08:56 CDT 2011


# HG changeset patch
# User Idan Kamara <idankk86 at gmail.com>
# Date 1303164515 -10800
# Node ID 9dded5e47aecfde3336484534a39da2fcde40317
# Parent  87c8ba7850a21ef098f8674e2ea70849da6e1ba3
identify: restructure code to make it more readable

diff -r 87c8ba7850a2 -r 9dded5e47aec mercurial/commands.py
--- a/mercurial/commands.py	Tue Apr 19 00:43:42 2011 +0300
+++ b/mercurial/commands.py	Tue Apr 19 01:08:35 2011 +0300
@@ -2363,78 +2363,86 @@
     hexfunc = ui.debugflag and hex or short
     default = not (num or id or branch or tags or bookmarks)
     output = []
-
     revs = []
-    bms = []
+
     if source:
         source, branches = hg.parseurl(ui.expandpath(source))
         repo = hg.repository(ui, source)
         revs, checkout = hg.addbranchrevs(repo, repo, branches, None)
 
     if not repo.local():
+        if num or branch or tags:
+            raise util.Abort(
+                _("can't query remote revision number, branch, or tags"))
         if not rev and revs:
             rev = revs[0]
         if not rev:
             rev = "tip"
-        if num or branch or tags:
-            raise util.Abort(
-                _("can't query remote revision number, branch, or tags"))
 
         remoterev = repo.lookup(rev)
         if default or id:
             output = [hexfunc(remoterev)]
 
-        if 'bookmarks' in repo.listkeys('namespaces'):
-            hexremoterev = hex(remoterev)
-            bms = [bm for bm, bmrev in repo.listkeys('bookmarks').iteritems()
-                   if bmrev == hexremoterev]
-
-    elif not rev:
-        ctx = repo[None]
-        parents = ctx.parents()
-        changed = False
-        if default or id or num:
-            changed = util.any(repo.status())
-        if default or id:
-            output = ["%s%s" % ('+'.join([hexfunc(p.node()) for p in parents]),
-                                (changed) and "+" or "")]
-        if num:
-            output.append("%s%s" % ('+'.join([str(p.rev()) for p in parents]),
-                                    (changed) and "+" or ""))
+        def getbms():
+            bms = []
+
+            if 'bookmarks' in repo.listkeys('namespaces'):
+                hexremoterev = hex(remoterev)
+                bms = [bm for bm, bmr in repo.listkeys('bookmarks').iteritems()
+                       if bmr == hexremoterev]
+
+            return bms
+
+        if bookmarks:
+            output.extend(getbms())
+        elif default and not ui.quiet:
+            # multiple bookmarks for a single parent separated by '/'
+            bm = '/'.join(getbms())
+            if bm:
+                output.append(bm)
     else:
-        ctx = cmdutil.revsingle(repo, rev)
-        if default or id:
-            output = [hexfunc(ctx.node())]
-        if num:
-            output.append(str(ctx.rev()))
-
-    if repo.local():
-        bms = ctx.bookmarks()
-
-    if repo.local() and default and not ui.quiet:
-        b = ctx.branch()
-        if b != 'default':
-            output.append("(%s)" % b)
-
-        # multiple tags for a single parent separated by '/'
-        t = "/".join(ctx.tags())
-        if t:
-            output.append(t)
-
-    if default and not ui.quiet:
-        # multiple bookmarks for a single parent separated by '/'
-        bm = '/'.join(bms)
-        if bm:
-            output.append(bm)
-
-    if branch:
-        output.append(ctx.branch())
-
-    if tags:
-        output.extend(ctx.tags())
-
-    if bookmarks:
-        output.extend(bms)
+        if not rev:
+            ctx = repo[None]
+            parents = ctx.parents()
+            changed = ""
+            if default or id or num:
+                changed = util.any(repo.status()) and "+" or ""
+            if default or id:
+                output = ["%s%s" %
+                  ('+'.join([hexfunc(p.node()) for p in parents]), changed)]
+            if num:
+                output.append("%s%s" %
+                  ('+'.join([str(p.rev()) for p in parents]), changed))
+        else:
+            ctx = cmdutil.revsingle(repo, rev)
+            if default or id:
+                output = [hexfunc(ctx.node())]
+            if num:
+                output.append(str(ctx.rev()))
+
+        if default and not ui.quiet:
+            b = ctx.branch()
+            if b != 'default':
+                output.append("(%s)" % b)
+
+            # multiple tags for a single parent separated by '/'
+            t = '/'.join(ctx.tags())
+            if t:
+                output.append(t)
+
+            # multiple bookmarks for a single parent separated by '/'
+            bm = '/'.join(ctx.bookmarks())
+            if bm:
+                output.append(bm)
+        else:
+            if branch:
+                output.append(ctx.branch())
+
+            if tags:
+                output.extend(ctx.tags())
+
+            if bookmarks:
+                output.extend(ctx.bookmarks())
 
     if output:
         ui.write("%s\n" % ' '.join(output))


More information about the Mercurial-devel mailing list