Bug 449381 - support "tip" and "default" in pushlog fromchange and tochange
authorDirkjan Ochtman <dirkjan@ochtman.nl>
Mon, 01 Sep 2008 11:03:36 +0200
changeset 44 1f5164ca184a
parent 43 c57e026c9e22
child 45 58dba74cfa4b
push id18
push usertmielczarek@mozilla.com
push dateThu, 04 Sep 2008 10:14:36 +0000
bugs449381
Bug 449381 - support "tip" and "default" in pushlog fromchange and tochange This makes it easier to request from last-known to current-tip.
pushlog-feed.py
--- a/pushlog-feed.py
+++ b/pushlog-feed.py
@@ -64,39 +64,33 @@ def getpushlogentriesbydate(conn, startd
         entries.append((id,user,date,node))
         lastid = id
     return entries
 
 def getpushlogentriesbychangeset(conn, fromchange, tochange, tipsonly):
     """Get entries in the push log between two changesets. Return changesets
     pushed after |fromchange|, up to and including |tochange|.
     If |tipsonly| is True, return only the tip changeset from each push."""
-    entries = []
-    # find the changeset before the first changeset
-    fromchange += "%"
-    e = conn.execute("SELECT pushid FROM changesets WHERE node LIKE ?", (fromchange,)).fetchone()
-    if e is None:
-        return []
-    fromid = e[0]
-    # find the last changeset
-    tochange += "%"
-    e = conn.execute("SELECT pushid FROM changesets WHERE node LIKE ?", (tochange,)).fetchone()
-    if e is None:
-        return []
-    toid = e[0]
-    if fromid >= toid:
+    csets = []
+    for cset in (fromchange, tochange):
+        e = conn.execute("SELECT pushid FROM changesets WHERE node = ?", (cset,)).fetchone()
+        if e is None:
+            return []
+        csets.append(e[0])
+    if csets[0] >= csets[1]:
         return []
     # now get all the changesets from right after fromchange, up to and
     # including tochange
-    res = conn.execute("SELECT id, user, date, node FROM pushlog LEFT JOIN changesets ON id = pushid WHERE id > ? AND id <= ? ORDER BY date DESC, rev DESC", (fromid, toid))
+    res = conn.execute("SELECT id, user, date, node FROM pushlog LEFT JOIN changesets ON id = pushid WHERE id > ? AND id <= ? ORDER BY date DESC, rev DESC", (csets[0], csets[1]))
     lastid = None
+    entries = []
     for (id, user, date, node) in res:
         if tipsonly and id == lastid:
             continue
-        entries.append((id,user,date,node))
+        entries.append((id, user, date, node))
         lastid = id
     return entries
 
 def gettotalpushlogentries(conn):
     """Return the total number of pushes logged in the pushlog."""
     return conn.execute("SELECT COUNT(*) FROM pushlog").fetchone()[0]
 
 def localdate(ts):
@@ -109,21 +103,22 @@ def localdate(ts):
 def doParseDate(datestring):
     try:
         date = time.strptime(datestring, "%Y-%m-%d %H:%M:%S")
     except ValueError:
         date, x = cal.parse(datestring)
     return time.mktime(date)
 
 def pushlogSetup(web, req):
-    repopath = os.path.dirname(web.repo.path)
+    repo = web.repo
+    repopath = os.path.dirname(repo.path)
     reponame = os.path.basename(repopath)
     if reponame == '.hg':
         reponame = os.path.basename(os.path.dirname(repopath))
-    pushdb = os.path.join(web.repo.path, "pushlog2.db")
+    pushdb = os.path.join(repo.path, "pushlog2.db")
     conn = sqlite.connect(pushdb)
 
     if 'node' in req.form:
         page = int(req.form['node'][0])
     else:
         page = 1
 
     tipsonly = False
@@ -132,51 +127,58 @@ def pushlogSetup(web, req):
     dates = []
     if 'startdate' in req.form and 'enddate' in req.form:
         startdate = doParseDate(req.form['startdate'][0])
         enddate = doParseDate(req.form['enddate'][0])
         dates = [{'startdate':localdate(startdate), 'enddate':localdate(enddate)}]
         page = 1
         total = 1
         e = getpushlogentriesbydate(conn, startdate, enddate, tipsonly)
-    elif 'fromchange' in req.form and 'tochange' in req.form:
-        fromchange = req.form['fromchange'][0]
-        tochange = req.form['tochange'][0]
+    elif 'fromchange' in req.form:
+        fromchange = hex(repo.lookup(req.form.get('fromchange', ['null'])[0]))
+        tochange = hex(repo.lookup(req.form.get('tochange', ['default'])[0]))
         page = 1
         total = 1
         e = getpushlogentriesbychangeset(conn, fromchange, tochange, tipsonly)
     else:
         e = getpushlogentries(conn, (page - 1) * PUSHES_PER_PAGE, 10, tipsonly)
         total = gettotalpushlogentries(conn)
+
+    # figure out the urlbase
     proto = req.env.get('wsgi.url_scheme')
     if proto == 'https':
         proto = 'https'
         default_port = "443"
     else:
         proto = 'http'
         default_port = "80"
     port = req.env["SERVER_PORT"]
     port = port != default_port and (":" + port) or ""
 
     urlbase = '%s://%s%s' % (proto, req.env['SERVER_NAME'], port)
     return (e, urlbase, reponame, total, page, dates)
     
 def pushlogFeed(web, req):
     (e, urlbase, reponame, total, page, dates) = pushlogSetup(web, req)
     isotime = lambda x: datetime.utcfromtimestamp(x).isoformat() + 'Z'
+    
+    if e:
+        dt = isotime(e[0][2])
+    else:
+        dt = datetime.utcnow().isoformat().split('.', 1)[0] + 'Z'
 
     resp = ["""<?xml version="1.0" encoding="UTF-8"?>
 <feed xmlns="http://www.w3.org/2005/Atom">
  <id>%(urlbase)s%(url)spushlog</id>
  <link rel="self" href="%(urlbase)s%(url)spushlog" />
  <updated>%(date)s</updated>
  <title>%(reponame)s Pushlog</title>""" % {'urlbase': urlbase,
                               'url': req.url,
                               'reponame': reponame,
-                              'date': isotime(e[0][2])}];
+                              'date': dt}];
 
     for id, user, date, node in e:
         ctx = web.repo.changectx(node)
         resp.append("""
  <entry>
   <title>Changeset %(node)s</title>
   <id>http://www.selenic.com/mercurial/#changeset-%(node)s</id>
   <link href="%(urlbase)s%(url)srev/%(node)s" />