changeset 124:291925677a9f

tag_repo: remove gentags command, extend repo.tags(), HgChangeEditor now takes either repo or repo_path
author Luke Opperman <luke@loppear.com>
date Thu, 04 Dec 2008 13:10:40 -0600
parents 58de7aea8a77
children c35f59aa200e
files __init__.py hg_delta_editor.py svncommand.py tag_repo.py tests/test_tags.py util.py
diffstat 6 files changed, 60 insertions(+), 28 deletions(-) [+]
line wrap: on
line diff
--- a/__init__.py
+++ b/__init__.py
@@ -5,6 +5,14 @@ from mercurial import hg
 
 import svncommand
 import fetch_command
+import tag_repo
+import util
+
+def reposetup(ui, repo):
+    if not util.is_svn_repo(repo):
+        return
+
+    repo.__class__ = tag_repo.generate_repo_class(ui, repo)
 
 
 def svn(ui, repo, subcommand, *args, **opts):
--- a/hg_delta_editor.py
+++ b/hg_delta_editor.py
@@ -58,7 +58,8 @@ class HgChangeReceiver(delta.Editor):
         f.close()
         self.revmap[revnum, branch] = node_hash
 
-    def __init__(self, path, ui_=None, subdir='', author_host='',
+    def __init__(self, path=None, repo=None, ui_=None,
+                 subdir='', author_host='',
                  tag_locations=['tags']):
         """path is the path to the target hg repo.
 
@@ -68,8 +69,15 @@ class HgChangeReceiver(delta.Editor):
         if not ui_:
             ui_ = ui.ui()
         self.ui = ui_
-        self.path = path
-        self.__setup_repo(path)
+        if repo:
+            self.repo = repo
+            self.path = os.path.normpath(os.path.join(self.repo.path, '..'))
+        elif path:
+            self.path = path
+            self.__setup_repo(path)
+        else:
+            raise TypeError("Expected either path or repo argument")
+
         self.subdir = subdir
         if self.subdir and self.subdir[0] == '/':
             self.subdir = self.subdir[1:]
--- a/svncommand.py
+++ b/svncommand.py
@@ -1,5 +1,4 @@
 import os
-import pickle
 import stat
 import sys
 import traceback
@@ -9,7 +8,6 @@ from mercurial import node
 from mercurial import util as merc_util
 
 import svnwrap
-import hg_delta_editor
 import util
 from util import register_subcommand, svn_subcommands, generate_help
 # dirty trick to force demandimport to run my decorator anyway.
@@ -78,19 +76,6 @@ def help_command(ui, args=None, **opts):
         return
     ui.status(generate_help())
 
-
-@register_subcommand('gentags')
-def generate_hg_tags(ui, hg_repo_path, **opts):
-    """Save tags to .hg/localtags
-    """
-    hg_editor = hg_delta_editor.HgChangeReceiver(hg_repo_path, ui_=ui)
-    f = open(hg_editor.tag_info_file)
-    tag_info = pickle.load(f)
-    f = open(os.path.join(hg_repo_path, '.hg', 'localtags'), 'w')
-    for tag, source in tag_info.iteritems():
-        source_ha = hg_editor.get_parent_revision(source[1]+1, source[0])
-        f.write('%s tag/%s\n' % (node.hex(source_ha), tag))
-
 @register_subcommand('up')
 def update(ui, args, repo, clean=False, **opts):
     """Update to a specified Subversion revision number.
new file mode 100644
--- /dev/null
+++ b/tag_repo.py
@@ -0,0 +1,25 @@
+import pickle
+
+from mercurial import node
+
+import hg_delta_editor
+
+
+def tags_from_tag_info(repo):
+    hg_editor = hg_delta_editor.HgChangeReceiver(repo=repo)
+    for tag, source in hg_editor.tags.iteritems():
+        source_ha = hg_editor.get_parent_revision(source[1]+1, source[0])
+        yield 'tag/%s'%tag, node.hex(source_ha)
+
+
+def generate_repo_class(ui, repo):
+
+    class svntagrepo(repo.__class__):
+        def tags(self):
+            tags = dict((k, node.bin(v))
+                        for k,v in tags_from_tag_info(self))
+            hg_tags = super(svntagrepo, self).tags()
+            tags.update(hg_tags)
+            return tags
+
+    return svntagrepo
--- a/tests/test_tags.py
+++ b/tests/test_tags.py
@@ -7,12 +7,19 @@ from mercurial import ui
 import test_util
 
 import svncommand
+import tag_repo
 
 class TestTags(test_util.TestBase):
     def _load_fixture_and_fetch(self, fixture_name, stupid=False):
         return test_util.load_fixture_and_fetch(fixture_name, self.repo_path,
                                                 self.wc_path, stupid=stupid)
 
+    def getrepo(self):
+        ui_ = ui.ui()
+        repo = hg.repository(ui_, self.wc_path)
+        repo.__class__ = tag_repo.generate_repo_class(ui_, repo)
+        return repo
+
     def _test_tag_revision_info(self, repo):
         self.assertEqual(node.hex(repo[0].node()),
                          'a47d0ce778660a91c31bf2c21c448e9ee296ac90')
@@ -24,8 +31,7 @@ class TestTags(test_util.TestBase):
         repo = self._load_fixture_and_fetch('basic_tag_tests.svndump',
                                             stupid=stupid)
         self._test_tag_revision_info(repo)
-        svncommand.generate_hg_tags(ui.ui(), self.wc_path)
-        repo = hg.repository(ui.ui(), self.wc_path)
+        repo = self.getrepo()
         self.assertEqual(repo['tip'].node(), repo['tag/tag_r3'].node())
         self.assertEqual(repo['tip'].node(), repo['tag/copied_tag'].node())
 
@@ -36,8 +42,7 @@ class TestTags(test_util.TestBase):
         repo = self._load_fixture_and_fetch('remove_tag_test.svndump',
                                             stupid=stupid)
         self._test_tag_revision_info(repo)
-        svncommand.generate_hg_tags(ui.ui(), self.wc_path)
-        repo = hg.repository(ui.ui(), self.wc_path)
+        repo = self.getrepo()
         self.assertEqual(repo['tip'].node(), repo['tag/tag_r3'].node())
         self.assert_('tag/copied_tag' not in repo.tags())
 
@@ -48,8 +53,7 @@ class TestTags(test_util.TestBase):
         repo = self._load_fixture_and_fetch('rename_tag_test.svndump',
                                             stupid=stupid)
         self._test_tag_revision_info(repo)
-        svncommand.generate_hg_tags(ui.ui(), self.wc_path)
-        repo = hg.repository(ui.ui(), self.wc_path)
+        repo = self.getrepo()
         self.assertEqual(repo['tip'].node(), repo['tag/tag_r3'].node())
         self.assertEqual(repo['tip'].node(), repo['tag/other_tag_r3'].node())
         self.assert_('tag/copied_tag' not in repo.tags())
@@ -60,8 +64,7 @@ class TestTags(test_util.TestBase):
     def test_branch_from_tag(self, stupid=False):
         repo = self._load_fixture_and_fetch('branch_from_tag.svndump',
                                             stupid=stupid)
-        svncommand.generate_hg_tags(ui.ui(), self.wc_path)
-        repo = hg.repository(ui.ui(), self.wc_path)
+        repo = self.getrepo()
         self.assertEqual(repo['tip'].node(), repo['branch_from_tag'].node())
         self.assertEqual(repo[1].node(), repo['tag/tag_r3'].node())
         self.assertEqual(repo['branch_from_tag'].parents()[0].node(),
@@ -73,8 +76,7 @@ class TestTags(test_util.TestBase):
     def test_tag_by_renaming_branch(self, stupid=False):
         repo = self._load_fixture_and_fetch('tag_by_rename_branch.svndump',
                                             stupid=stupid)
-        svncommand.generate_hg_tags(ui.ui(), self.wc_path)
-        repo = hg.repository(ui.ui(), self.wc_path)
+        repo = self.getrepo()
         self.assertEqual(node.hex(repo['tip'].node()),
                          '1b941f92acc343939274bd8bbf25984fa9706bb9')
         self.assertEqual(node.hex(repo['tag/dummy'].node()),
--- a/util.py
+++ b/util.py
@@ -94,3 +94,7 @@ def outgoing_revisions(ui, repo, hg_edit
         working_rev = working_rev[0]
     if working_rev.node() != node.nullid:
         return outgoing_rev_hashes
+
+
+def is_svn_repo(repo):
+    return os.path.exists(os.path.join(repo.path, 'svn'))