| 1 | # -*- coding: utf-8 -*- |
|---|
| 2 | # |
|---|
| 3 | # Copyright (C) 2005 Edgewall Software |
|---|
| 4 | # Copyright (C) 2005-2009 Lele Gaifax <lele@metapensiero.it> |
|---|
| 5 | # |
|---|
| 6 | # This software is licensed as described in the file COPYING, which |
|---|
| 7 | # you should have received as part of this distribution. The terms |
|---|
| 8 | # are also available at http://trac.edgewall.com/license.html. |
|---|
| 9 | # |
|---|
| 10 | # This software consists of voluntary contributions made by many |
|---|
| 11 | # individuals. For the exact contribution history, see the revision |
|---|
| 12 | # history and logs, available at http://projects.edgewall.com/trac/. |
|---|
| 13 | # |
|---|
| 14 | # Author: Lele Gaifax <lele@metapensiero.it> |
|---|
| 15 | |
|---|
| 16 | from genshi.builder import tag |
|---|
| 17 | |
|---|
| 18 | from trac.config import BoolOption, Option |
|---|
| 19 | from trac.core import Component, implements |
|---|
| 20 | from trac.db import Column, DatabaseManager, Index, Table |
|---|
| 21 | from trac.env import IEnvironmentSetupParticipant |
|---|
| 22 | from trac.util.text import shorten_line |
|---|
| 23 | from trac.versioncontrol import IRepositoryConnector, NoSuchChangeset |
|---|
| 24 | from trac.wiki import IWikiSyntaxProvider |
|---|
| 25 | |
|---|
| 26 | from tracdarcs.repository import DarcsRepository |
|---|
| 27 | |
|---|
| 28 | class DarcsConnector(Component): |
|---|
| 29 | |
|---|
| 30 | implements(IRepositoryConnector, IWikiSyntaxProvider) |
|---|
| 31 | |
|---|
| 32 | dont_escape_8bit = BoolOption('darcs', 'dont_escape_8bit', 'false', |
|---|
| 33 | "Avoid darcs automatic escape of non-7bit chars.") |
|---|
| 34 | |
|---|
| 35 | darcs_command = Option('darcs', 'command', 'darcs', |
|---|
| 36 | "Name of the external darcs binary.") |
|---|
| 37 | |
|---|
| 38 | possible_encodings = Option('darcs', 'possible_encodings', 'utf-8,iso8859-1', |
|---|
| 39 | "Specify possible repository encodings.") |
|---|
| 40 | |
|---|
| 41 | # IRepositoryConnector methods |
|---|
| 42 | |
|---|
| 43 | def get_supported_types(self): |
|---|
| 44 | """Support the `darcs:` scheme""" |
|---|
| 45 | yield ("darcs", 8) |
|---|
| 46 | |
|---|
| 47 | def get_repository(self, type, dir, authname): |
|---|
| 48 | """Return a `DarcsRepository`""" |
|---|
| 49 | db = self.env.get_db_cnx() |
|---|
| 50 | darcs = self.darcs_command |
|---|
| 51 | if self.dont_escape_8bit: |
|---|
| 52 | darcs = "DARCS_DONT_ESCAPE_8BIT=1 " + darcs |
|---|
| 53 | if self.possible_encodings: |
|---|
| 54 | possible_encodings = [e.strip() |
|---|
| 55 | for e in self.possible_encodings.split(',')] |
|---|
| 56 | return DarcsRepository(db, dir, self.env.log, darcs, possible_encodings) |
|---|
| 57 | |
|---|
| 58 | # IWikiSyntaxProvider methods |
|---|
| 59 | |
|---|
| 60 | def get_wiki_syntax(self): |
|---|
| 61 | yield (r'[0-9]{14}-[0-9a-f]{5}-[0-9a-f]{40}(.gz)?', |
|---|
| 62 | lambda formatter, label, match: self._format_link(formatter, 'cset', label, label)) |
|---|
| 63 | |
|---|
| 64 | def get_link_resolvers(self): |
|---|
| 65 | yield ('cset', self._format_link) |
|---|
| 66 | |
|---|
| 67 | def _format_link(self, formatter, ns, rev, label): |
|---|
| 68 | |
|---|
| 69 | repos = self.env.get_repository() |
|---|
| 70 | try: |
|---|
| 71 | chgset = repos.get_changeset(rev) |
|---|
| 72 | return tag.a(chgset.rev, class_="changeset", |
|---|
| 73 | title=shorten_line(chgset.message), |
|---|
| 74 | href=formatter.href.changeset(chgset.rev)) |
|---|
| 75 | except NoSuchChangeset, e: |
|---|
| 76 | return tag.a(label, class_="missing changeset", |
|---|
| 77 | title="No changeset for %s" % rev, rel="nofollow") |
|---|
| 78 | |
|---|
| 79 | class DarcsSetup(Component): |
|---|
| 80 | """Darcs customizer. |
|---|
| 81 | |
|---|
| 82 | The db tables required for the darcs backend are created here. |
|---|
| 83 | """ |
|---|
| 84 | |
|---|
| 85 | implements(IEnvironmentSetupParticipant) |
|---|
| 86 | |
|---|
| 87 | def environment_created(self): |
|---|
| 88 | """After standard environment has been created, add the needed |
|---|
| 89 | tables.""" |
|---|
| 90 | |
|---|
| 91 | db = self.env.get_db_cnx() |
|---|
| 92 | self.upgrade_environment(db) |
|---|
| 93 | db.commit() |
|---|
| 94 | |
|---|
| 95 | def environment_needs_upgrade(self, db): |
|---|
| 96 | """Check to see if the darcs tables are already there.""" |
|---|
| 97 | |
|---|
| 98 | c = db.cursor() |
|---|
| 99 | try: |
|---|
| 100 | c.execute('SELECT repo_id,rev,hash,name ' |
|---|
| 101 | 'FROM darcs_changesets LIMIT 1') |
|---|
| 102 | c.execute('SELECT repo_id,node_id,node_type,add_rev,remove_rev ' |
|---|
| 103 | 'FROM darcs_nodes LIMIT 1') |
|---|
| 104 | c.execute('SELECT repo_id,node_id,rev,path,parent_id,the_change ' |
|---|
| 105 | 'FROM darcs_node_changes LIMIT 1') |
|---|
| 106 | c.execute('SELECT repo_id,node_id,rev,content,size ' |
|---|
| 107 | 'FROM darcs_cache LIMIT 1') |
|---|
| 108 | return False |
|---|
| 109 | except: |
|---|
| 110 | db.rollback() |
|---|
| 111 | return True |
|---|
| 112 | |
|---|
| 113 | def upgrade_environment(self, db): |
|---|
| 114 | """Actually add the new db tables.""" |
|---|
| 115 | |
|---|
| 116 | def drop_table(table_name): |
|---|
| 117 | c = db.cursor() |
|---|
| 118 | try: |
|---|
| 119 | c.execute('drop table %s' % table_name) |
|---|
| 120 | except: |
|---|
| 121 | db.rollback() |
|---|
| 122 | pass |
|---|
| 123 | |
|---|
| 124 | drop_table('darcs_revisions') # until 0.6 |
|---|
| 125 | drop_table('darcs_changesets') |
|---|
| 126 | drop_table('darcs_nodes') |
|---|
| 127 | drop_table('darcs_node_changes') |
|---|
| 128 | drop_table('darcs_cache') |
|---|
| 129 | |
|---|
| 130 | connector = DatabaseManager(self.env)._get_connector()[0] |
|---|
| 131 | if 'postgres' in [supp[0] for supp in connector.get_supported_schemes()]: |
|---|
| 132 | blobtype = 'bytea' |
|---|
| 133 | else: |
|---|
| 134 | blobtype = 'blob' |
|---|
| 135 | rev_table = Table('darcs_changesets', key=('repo_id','rev'))[ |
|---|
| 136 | Column('repo_id'), |
|---|
| 137 | Column('rev',type='int'), |
|---|
| 138 | Column('hash'), |
|---|
| 139 | Column('name'), |
|---|
| 140 | Index(['hash'])] |
|---|
| 141 | node_table = Table('darcs_nodes', key=('repo_id','node_id'))[ |
|---|
| 142 | Column('repo_id'), |
|---|
| 143 | Column('node_id',type='int'), |
|---|
| 144 | Column('node_type',size=1), |
|---|
| 145 | Column('add_rev',type='int'), |
|---|
| 146 | Column('remove_rev',type='int')] |
|---|
| 147 | change_table = Table('darcs_node_changes', key=('repo_id','node_id','rev'))[ |
|---|
| 148 | Column('repo_id'), |
|---|
| 149 | Column('node_id',type='int'), |
|---|
| 150 | Column('rev',type='int'), |
|---|
| 151 | Column('path'), |
|---|
| 152 | Column('parent_id',type='int'), |
|---|
| 153 | Column('the_change')] |
|---|
| 154 | cache_table = Table('darcs_cache', key=('repo_id','node_id','rev'))[ |
|---|
| 155 | Column('repo_id'), |
|---|
| 156 | Column('node_id',type='int'), |
|---|
| 157 | Column('rev',type='int'), |
|---|
| 158 | Column('content',type=blobtype), |
|---|
| 159 | Column('size',type='int')] |
|---|
| 160 | c = db.cursor() |
|---|
| 161 | for t in [rev_table,node_table,change_table,cache_table]: |
|---|
| 162 | for stmt in connector.to_sql(t): |
|---|
| 163 | c.execute(stmt) |
|---|