hg site extension
 
(Arne Babenhauserheide)
2012-10-30: added the number of bugs to the overview page. No details yet.

added the number of bugs to the overview page. No details yet.

diff --git a/staticsite.py b/staticsite.py
--- a/staticsite.py
+++ b/staticsite.py
@@ -65,7 +65,14 @@ templates = {
 <h1>{forkname} <small>(fork of <a href="../../">{reponame}</a>, found at {forkuri})</small></h1>
 """,
     "foot": "</body></html>\n",
-    "screenstyle": """ """,
+    "screenstyle": """ 
+openbugnumber {
+    color: #f00;
+}
+resolvedbugnumber {
+    color: #00f;
+}
+""",
     "printstyle": """ """,
     "manifesthead": """<h2>""" + _("Commit (click to see the diff)")+""": <a href='../../commit/{hex}.html'>{hex}</a></h2>
 <p>{desc}</p><p>{user}</p>
@@ -95,6 +102,76 @@ def contentequals(filepath, content):
     except IOError: return False # file does not exist. Empty != not existing.
     # TODO: check: return True if content is None?
 
+def bisenabled():
+    """Check if the b extension is enabled to decide if we want to add
+    a bug listing."""
+    enabled = extensions.enabled()
+    if "b" in enabled:
+        return True
+
+def splitbugline(line):
+    """Split a b extension bug line into the ID and the description."""
+    try:
+        bugid = line.split("-")[0].strip()
+    except IndexError:
+        return "", line
+    description = "".join(line.split("-")[1:]).lstrip()
+    return bugid, description
+
+def getbugdetails(ui, bugid):
+    """Get the details for a bug."""
+    # first get the details
+    ui.pushbuffer()
+    req = dispatch.request(["b", "details", bugid], ui=ui)
+    dispatch.dispatch(req)
+    return ui.popbuffer()
+
+def getbugfullid(details, bugid):
+    """Get the real ID of a bug from its detailed info. If it’s not available, just give the short bugid"""
+    try:
+        idline = [i for i in details.splitlines() if i.startswith("ID: ")][0]
+    except IndexError: # no id line
+        return bugid
+    realid = idline[4:].strip()
+    return realid
+
+class BBug(object):
+    """A b-extension bug."""
+    def __init__(self, bugid, description, state, details=""):
+        self.bugid, self.description, self.state, self.details = bugid, description, state, details
+
+def getbuginfo(ui, bugline):
+    """Get information about a bug from its bugline."""
+    bugid, description = splitbugline(bugline)
+    details = getbugdetails(ui, bugid)
+    bugid = getbugfullid(details, bugid)
+    return bugid, description, details
+
+def getbugs(ui, repo):
+    """Get all bugs."""
+    if not bisenabled():
+        return [], []
+    # run the b command to get all open bugs
+    ui.pushbuffer()
+    req = dispatch.request(["b"], ui=ui, repo=repo)
+    dispatch.dispatch(req)
+    openbuglines = [line for line in ui.popbuffer().splitlines() if "-" in line]
+    # similarly get all resolved bugs
+    ui.pushbuffer()
+    req = dispatch.request(["b", "list", "-r"], ui=ui, repo=repo)
+    dispatch.dispatch(req)
+    resolvedbuglines = [line for line in ui.popbuffer().splitlines() if "-" in line]
+    # now turn them into a list of bugs
+    openbugs = []
+    for bugline in openbuglines:
+        bugid, description, details = getbuginfo(ui, bugline)
+        openbugs.append(BBug(bugid, description, "open", details))
+    resolvedbugs = []
+    for bugline in resolvedbuglines:
+        bugid, description, details = getbuginfo(ui, bugline)
+        resolvedbugs.append(BBug(bugid, description, "resolved", details))
+    return openbugs, resolvedbugs
+
 def parsereadme(filepath, truncated=False):
     """Parse the readme file"""
     with open(filepath) as r:
@@ -131,7 +208,17 @@ def writeoverview(ui, repo, target, name
             overview += "</div>"
             break
     # now the links to the log and the files.
-    overview += "\n<p id='nav'><a href='commits'>changelog</a> | <a href='src/" + repo["tip"].hex() + "/'>files</a>"
+    overview += "\n<p id='nav'><a href='commits'>" + _("changelog") + "</a> | <a href='src/" + repo["tip"].hex() + "/'>" + _("files") + "</a>"
+    # and the bugs
+    openbugs, resolvedbugs = getbugs(ui, repo)
+    if openbugs or resolvedbugs:
+        overview += " | " + _("bugs: ")
+        if openbugs:
+            overview += "<small><span class=\"openbugnumber\">" + str(len(openbugs)) + "!</span>"
+        else:
+            overview += "<span class=\"openbugnumberzero\">0</span>"
+        overview += "<span class=\"resolvedbugnumber\">" + str(len(resolvedbugs)) + "√</span></small>"
+        
     # and the forks
     forks = getforkinfo(ui, target)
     if forks:
@@ -480,7 +567,10 @@ def rawpath(target, ctx, filename):
 
 def ctxdiffstat(ui, repo, ctx):
     """Get the diffstat of a change context."""
-    command = "log -r " + ctx.hex() + " --stat --color=never"
+    if "color" in extensions.enabled():
+        command = "log -r " + ctx.hex() + " --stat --color=never"
+    else:
+        command = "log -r " + ctx.hex() + " --stat"
     req = dispatch.request(command.split(), ui=ui, repo=repo)
     ui.pushbuffer()
     dispatch.dispatch(req)