Move bugsync command into buginfo and provide --sync and --reset modes
authorGregory Szorc <gps@mozilla.com>
Fri, 23 Aug 2013 22:56:22 -0700
changeset 42 526604a2c687273c320f1f3f76994bcebf435ddf
parent 41 523a35fff8e31c793e93907451a1a546228c972c
child 43 6d1832c87bc27b0bf5094ab81088aa58654082cc
push id25
push usergszorc@mozilla.com
push dateTue, 27 Aug 2013 01:48:27 +0000
Move bugsync command into buginfo and provide --sync and --reset modes
__init__.py
mozautomation/changetracker.py
--- a/__init__.py
+++ b/__init__.py
@@ -97,19 +97,19 @@ the local machine. This facilitates rapi
 running `hg pushlogsync`.
 
 Once pushlog data is synced, you can use `hg changesetpushes` to look up push
 information for a specific changeset.
 
 Bug Info
 ========
 
-Information about bugs is extracted from commit messages via the `hg bugsync`
-command. Once information about bugs is extracted, you can look up information
-about specific bugs via `hg buginfo`.
+Information about bugs is extracted from commit messages as changesets are
+introduced into the repository. You can look up information about specific
+bugs via `hg buginfo`.
 """
 
 import datetime
 import errno
 import os
 import re
 import shutil
 import sys
@@ -455,39 +455,30 @@ def changesetpushes(ui, repo, rev, all=F
     """Display pushlog information for a changeset.
 
     This command prints pushlog entries for a given changeset. It is used to
     answer the question: how did a changeset propagate to all the trees.
     """
     print_changeset_pushes(ui, repo, rev, all=all)
 
 
-@command('bugsync', [], 'hg bugsync')
-def syncbuginfo(ui, repo, **opts):
-    """Synchronize bug info with the local database.
-
-    This command must be performed before `hg buginfo` to ensure the data is up
-    to date.
-    """
-    for rev in repo:
-        ui.progress('changeset', rev, total=len(repo))
-        ctx = repo[rev]
+@command('buginfo', [
+    ('a', 'all', False, _('Show all trees, not just release trees.'), ''),
+    ('', 'reset', False, _('Wipe and repopulate the bug database.'), ''),
+    ('', 'sync', False, _('Synchronize the bug database.'), ''),
+    ], _('hg buginfo [BUG] ...'))
+def buginfo(ui, repo, *bugs, **opts):
+    if opts['sync']:
+        repo.sync_bug_database()
+        return
 
-        bugs = repo._bugs_in_description(ctx.description())
-        if not bugs:
-            continue
-        repo.changetracker.associate_bugs_with_changeset(bugs, ctx.node())
+    if opts['reset']:
+        repo.reset_bug_database()
+        return
 
-    ui.progress('changeset', None)
-
-
-@command('buginfo',
-    [('a', 'all', False, _('Show all trees, not just release trees.'), '')],
-    _('hg buginfo [BUG] ...'))
-def buginfo(ui, repo, *bugs, **opts):
     tracker = ChangeTracker(repo.join('changetracker.db'))
 
     nodes = set()
     for bug in bugs:
         nodes |= set(tracker.changesets_with_bug(bug))
 
     # Sorting by topological order would probably be preferred. This is quick
     # and easy.
@@ -703,16 +694,31 @@ def reposetup(ui, repo):
                 version = '%s.%s' % (major, minor)
                 if marker:
                     version += '%s%s' % (marker, after)
 
                 d[version] = (key, node, major, minor, marker or None, after or None)
 
             return d
 
+        def reset_bug_database(self):
+            self.changetracker.wipe_bugs()
+            self.sync_bug_database()
+
+        def sync_bug_database(self):
+            for rev in self:
+                ui.progress('changeset', rev, total=len(self))
+                ctx = self[rev]
+                bugs = self._bugs_in_description(ctx.description())
+                if bugs:
+                    self.changetracker.associate_bugs_with_changeset(bugs,
+                        ctx.node())
+
+            ui.progress('changeset', None)
+
         BUG_RE = re.compile(r'''# bug followed by any sequence of numbers, or
                                 # a standalone sequence of numbers
                              (
                                (?:
                                  bug |
                                  b= |
                                  # a sequence of 5+ numbers preceded by whitespace
                                  (?=\b\#?\d{5,}) |
--- a/mozautomation/changetracker.py
+++ b/mozautomation/changetracker.py
@@ -135,8 +135,12 @@ class ChangeTracker(object):
         with self._db:
             self._db.executemany('INSERT OR REPLACE INTO bug_changesets '
                 'VALUES (?, ?)', [(bug, buffer(changeset)) for bug in bugs])
 
     def changesets_with_bug(self, bug):
         for row in self._db.execute('SELECT changeset FROM bug_changesets WHERE '
             'bug = ?', [bug]):
             yield str(row[0])
+
+    def wipe_bugs(self):
+        with self._db:
+            self._db.execute('DELETE FROM bug_changesets')