Commits

David Douard  committed 91ec993

[hooks] add a repository creation hook (closes #2716057)

  • Participants
  • Parent commits e82a53b

Comments (0)

Files changed (2)

         hgadmin_path = edit.get('hgadmin_path')
         if not base_url or not hgadmin_path:
             return # standard validation will catch this, let's bail out
+        url = '/'.join((base_url, hgadmin_path))
         repo = self._cw.create_entity('Repository',
                                       title=u'%s repository for %s' % (hgadmin_path,
                                                                        self.entity.eid),
                                       type=u'mercurial',
-                                      source_url=u'/'.join((base_url, hgadmin_path)),
+                                      source_url=unicode(url),
                                       has_anonymous_access=False)
         edit['hgadmin_repository'] = repo.eid
 
 
+class MercurialServerRepositoryCreation(Hook):
+    __regid__ = 'mercurialserver.initrepo'
+    __select__ = Hook.__select__ & match_rtype('hosted_by')
+    events = ('after_add_relation', )
+
+    def __call__(self):
+        if self.rtype == 'hosted_by':
+            repoeid = self.eidfrom
+        else:
+            repoeid = self.eidto
+        repo = self._cw.entity_from_eid(repoeid)
+        try:
+            commands.init(ui.ui(),  repo.source_url.encode('ascii'))
+        except UnicodeEncodeError:
+            msg = self._cw._('source_url must be ASCII encodable')
+            raise ValidationError(self.eidfrom, {'source_url': msg})
+
 class MercurialServerAccessKeyUpdateHook(Hook):
     """Schedule an update operation for the access key"""
 

File testutils.py

 
 from mercurial import commands, ui, hg
 
+from cubes.vcsfile import bridge
+
 class MercurialServerTCMixin(object):
     debug = False
 
-    def create_local_mercurial_repo(self, basepath, base_name, repo_name, title):
+    def _prepare_repo(self, basepath, basename, reponame):
+        repopath = osp.join(basepath, basename, reponame)
+        if osp.isdir(repopath):
+            shutil.rmtree(repopath)
+        os.makedirs(repopath)
+        return repopath
+
+    def create_local_mercurial_repo(self, basepath, basename, reponame, title):
         """ Create a local mercurial repository """
-        repo_path = osp.join(basepath, base_name, repo_name)
-        if osp.isdir(repo_path):
-            shutil.rmtree(repo_path)
-        os.makedirs(repo_path)
-        commands.init(ui.ui(), repo_path)
+        repopath = self._prepare_repo(basepath, basename, reponame)
         repo = self.session.create_entity('Repository',
                                           title=title,
                                           type=u'mercurial',
-                                          source_url=u"file://" + repo_path)
+                                          source_url=u'file://' + repopath)
         return repo
 
     def create_local_mercurial_server(self, basepath, name):
         # mercurial server creates repo directory recursively but this is not
         # the case in this fake env: we need to create receiving directory first
-        hgadmin_repo = self.create_local_mercurial_repo(basepath, name, 'hgadmin',
-                                                        title=u"Administrative repo for '%s' repo" % name)
+        self._prepare_repo(basepath, name, 'hgadmin')
         server_config = self.session.create_entity('MercurialServerConfig',
-                                                   base_url=hgadmin_repo.source_url[:-len('/hgadmin')],
-                                                   hgadmin_repository=hgadmin_repo)
+                                                   base_url=u'file://' + osp.join(basepath, name),
+                                                   hgadmin_path=u'hgadmin')
+        source_url = 'file://%s/hgadmin' % osp.join(basepath, name)
+        commands.init(ui.ui(),  source_url.encode('ascii'))
         return server_config
 
+    def refresh(self):
+        with self.session.repo.internal_session() as session:
+            bridge.import_content(session, raise_on_error=True)
+            session.commit()
+
     def setup_database(self):
         '''initialize a fake hgadmin repository'''
         super(MercurialServerTCMixin, self).setup_database()
         self.admin_user_repo = self.create_local_mercurial_repo(self.basepath, 'default', 'admin_repo',
                                                                 u'Repo for user admin')
         self.admin_user_repo.set_relations(hosted_by=self.server_config)
+        self.commit()
+        self.refresh()
 
     def tearDown(self):
         '''removes the fake hgadmin repository'''
         super(MercurialServerTCMixin, self).tearDown()
         if self.basepath and osp.isdir(self.basepath):
             if not self.debug:
-                shutil.rmtree(self.basepath)
+                shutil.rmtree(self.basepath, ignore_errors=True)
             else:
                 print "[DEBUG] repositories can be found in %s" % self.basepath