| 1 | # -*- coding: iso-8859-1 -*- |
|---|
| 2 | # |
|---|
| 3 | # Copyright (C) 2005 Edgewall Software |
|---|
| 4 | # Copyright (C) 2006 K.S.Sreeram <sreeram@tachyontech.net> |
|---|
| 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: K.S.Sreeram <sreeram@tachyontech.net> |
|---|
| 15 | |
|---|
| 16 | import os, StringIO, mimetypes |
|---|
| 17 | from datetime import tzinfo, timedelta, datetime |
|---|
| 18 | |
|---|
| 19 | from trac.util import TracError |
|---|
| 20 | from trac.versioncontrol import Repository, Node, Changeset, \ |
|---|
| 21 | NoSuchChangeset, NoSuchNode |
|---|
| 22 | |
|---|
| 23 | from command import DarcsCommand |
|---|
| 24 | from updatedb import update_darcsdb |
|---|
| 25 | from dbutil import NODE_FILE_TYPE, NODE_DIR_TYPE |
|---|
| 26 | from dbutil import CHANGE_ADDED, CHANGE_REMOVED, CHANGE_MOVED, \ |
|---|
| 27 | CHANGE_EDITED, CHANGE_MOVED_EDITED |
|---|
| 28 | from dbutil import query_nodes_for_revision |
|---|
| 29 | |
|---|
| 30 | ''' |
|---|
| 31 | This module implements the trac versioncontrol backend API. |
|---|
| 32 | The API consists of 3 classes: DarcsRepository, DarcsNode |
|---|
| 33 | and DarcsChangeset. |
|---|
| 34 | |
|---|
| 35 | Please see the docs in trac.versioncontrol.api for the interface |
|---|
| 36 | which is implemented by this module. |
|---|
| 37 | ''' |
|---|
| 38 | |
|---|
| 39 | ZERO = timedelta(0) |
|---|
| 40 | HOUR = timedelta(hours=1) |
|---|
| 41 | |
|---|
| 42 | class UTC(tzinfo): |
|---|
| 43 | """UTC""" |
|---|
| 44 | |
|---|
| 45 | def utcoffset(self, dt): |
|---|
| 46 | return ZERO |
|---|
| 47 | |
|---|
| 48 | def tzname(self, dt): |
|---|
| 49 | return "UTC" |
|---|
| 50 | |
|---|
| 51 | def dst(self, dt): |
|---|
| 52 | return ZERO |
|---|
| 53 | |
|---|
| 54 | utc = UTC() |
|---|
| 55 | |
|---|
| 56 | # mapping from node types used by the darcs backend and the types |
|---|
| 57 | # used by the trac api |
|---|
| 58 | _node_type_map = { |
|---|
| 59 | NODE_FILE_TYPE : Node.FILE, |
|---|
| 60 | NODE_DIR_TYPE : Node.DIRECTORY |
|---|
| 61 | } |
|---|
| 62 | |
|---|
| 63 | # mapping from change types used by the darcs backend and the types |
|---|
| 64 | # used by the trac api |
|---|
| 65 | _change_map = { |
|---|
| 66 | CHANGE_ADDED : Changeset.ADD, |
|---|
| 67 | CHANGE_REMOVED : Changeset.DELETE, |
|---|
| 68 | CHANGE_MOVED : Changeset.MOVE, |
|---|
| 69 | CHANGE_EDITED : Changeset.EDIT, |
|---|
| 70 | #FIXME: treat moved&edited as just moved? |
|---|
| 71 | CHANGE_MOVED_EDITED : Changeset.MOVE |
|---|
| 72 | } |
|---|
| 73 | |
|---|
| 74 | def to_utc_datetime(dt): |
|---|
| 75 | if isinstance(dt, long): |
|---|
| 76 | try: |
|---|
| 77 | strptime = datetime.strptime |
|---|
| 78 | except AttributeError: |
|---|
| 79 | # Python <2.5 |
|---|
| 80 | import time |
|---|
| 81 | def strptime(s,f): |
|---|
| 82 | tt = time.strptime(s, f) |
|---|
| 83 | return datetime(*tt[0:6]) |
|---|
| 84 | dt = strptime(str(dt), '%Y%m%d%H%M%S') |
|---|
| 85 | |
|---|
| 86 | return dt.replace(tzinfo = utc) |
|---|
| 87 | |
|---|
| 88 | def get_node_type( db, node_id ) : |
|---|
| 89 | c = db.cursor() |
|---|
| 90 | c.execute( 'SELECT node_type FROM darcs_nodes ' + |
|---|
| 91 | 'WHERE node_id = %s', (node_id,) ) |
|---|
| 92 | return c.fetchone()[0] |
|---|
| 93 | |
|---|
| 94 | def get_prev_path_rev( db, node_id, rev ) : |
|---|
| 95 | c = db.cursor() |
|---|
| 96 | c.execute( 'SELECT path,rev FROM darcs_node_changes ' + |
|---|
| 97 | 'WHERE node_id = %s AND rev < %s ' + |
|---|
| 98 | 'ORDER BY rev DESC LIMIT 1', (node_id,rev) ) |
|---|
| 99 | path,rev = c.fetchone() |
|---|
| 100 | return path,rev |
|---|
| 101 | |
|---|
| 102 | class DarcsRepository( Repository ) : |
|---|
| 103 | def __init__( self, db, path, log, config ) : |
|---|
| 104 | Repository.__init__( self, path, None, log ) |
|---|
| 105 | self.db = db |
|---|
| 106 | self.path = path |
|---|
| 107 | self.log = log |
|---|
| 108 | self.config = config |
|---|
| 109 | self.__cmd = DarcsCommand( 'darcs', path, log ) |
|---|
| 110 | # import any new changesets, if any |
|---|
| 111 | update_darcsdb( db, self.__cmd, log ) |
|---|
| 112 | |
|---|
| 113 | def close( self ) : |
|---|
| 114 | pass |
|---|
| 115 | |
|---|
| 116 | def get_changeset( self, rev ) : |
|---|
| 117 | rev = self.normalize_rev( rev ) |
|---|
| 118 | return DarcsChangeset( self.db, rev ) |
|---|
| 119 | |
|---|
| 120 | def get_node( self, path, rev=None ) : |
|---|
| 121 | path = self.normalize_path( path ) |
|---|
| 122 | rev = self.normalize_rev( rev ) |
|---|
| 123 | # compute node_id, node_type and last_rev and then |
|---|
| 124 | # create a DarcsNode object. |
|---|
| 125 | # 'last_rev' is the last revision <= rev where this |
|---|
| 126 | # node was modified. |
|---|
| 127 | if path == '/' : |
|---|
| 128 | node_id = None |
|---|
| 129 | node_type = NODE_DIR_TYPE |
|---|
| 130 | last_rev = rev |
|---|
| 131 | else : |
|---|
| 132 | c = self.db.cursor() |
|---|
| 133 | q = query_nodes_for_revision( rev ) |
|---|
| 134 | q += ' AND dnc.path = %s' |
|---|
| 135 | c.execute( q, (path,) ) |
|---|
| 136 | row = c.fetchone() |
|---|
| 137 | if row is None : |
|---|
| 138 | raise NoSuchNode( path, rev ) |
|---|
| 139 | node_id,last_rev = row[:2] |
|---|
| 140 | node_type = get_node_type( self.db, node_id ) |
|---|
| 141 | return DarcsNode( node_id, node_type, path, last_rev, |
|---|
| 142 | self.db, self.__cmd ) |
|---|
| 143 | |
|---|
| 144 | def get_oldest_rev( self ) : |
|---|
| 145 | if self.get_youngest_rev() is None : |
|---|
| 146 | return None |
|---|
| 147 | return 1 |
|---|
| 148 | |
|---|
| 149 | def get_youngest_rev( self ) : |
|---|
| 150 | c = self.db.cursor() |
|---|
| 151 | c.execute( 'SELECT rev FROM darcs_revisions ' + |
|---|
| 152 | 'ORDER BY rev DESC LIMIT 1' ) |
|---|
| 153 | row = c.fetchone() |
|---|
| 154 | return row and row[0] or None |
|---|
| 155 | |
|---|
| 156 | def previous_rev( self, rev ) : |
|---|
| 157 | rev = self.normalize_rev( rev ) |
|---|
| 158 | if rev > 1 : |
|---|
| 159 | return rev-1 |
|---|
| 160 | return None |
|---|
| 161 | |
|---|
| 162 | def next_rev( self, rev, path='' ) : |
|---|
| 163 | rev = self.normalize_rev( rev ) |
|---|
| 164 | if rev < self.get_youngest_rev() : |
|---|
| 165 | return rev+1 |
|---|
| 166 | return None |
|---|
| 167 | |
|---|
| 168 | def rev_older_than( self, rev1, rev2 ) : |
|---|
| 169 | return self.normalize_rev(rev1) < self.normalize_rev(rev2) |
|---|
| 170 | |
|---|
| 171 | def get_path_history( self, path, rev=None, limit=None ) : |
|---|
| 172 | # FIXME: this is not correct |
|---|
| 173 | return self.get_node( path, rev ).get_history( limit ) |
|---|
| 174 | |
|---|
| 175 | def normalize_path( self, path ) : |
|---|
| 176 | return path and path.strip('/') or '/' |
|---|
| 177 | |
|---|
| 178 | def normalize_rev( self, rev ) : |
|---|
| 179 | youngest = self.get_youngest_rev() |
|---|
| 180 | if rev is None or rev == "" : |
|---|
| 181 | return youngest |
|---|
| 182 | rev = int( rev ) |
|---|
| 183 | if rev > youngest : |
|---|
| 184 | rev = youngest |
|---|
| 185 | return rev |
|---|
| 186 | |
|---|
| 187 | def get_changes( self, old_path, old_rev, new_path, new_rev, |
|---|
| 188 | ignore_ancestry=1 ) : |
|---|
| 189 | old_path = self.normalize_path( old_path ) |
|---|
| 190 | old_rev = self.normalize_rev( old_rev ) |
|---|
| 191 | new_path = self.normalize_path( new_path ) |
|---|
| 192 | new_rev = self.normalize_rev( new_rev ) |
|---|
| 193 | old_node = self.get_node( old_path, old_rev ) |
|---|
| 194 | new_node = self.get_node( new_path, new_rev ) |
|---|
| 195 | |
|---|
| 196 | node_id = old_node._get_node_id() |
|---|
| 197 | if node_id != new_node._get_node_id() : |
|---|
| 198 | raise TracError( 'Node mismatch: base is %s in rev %d ' |
|---|
| 199 | 'and target is %s in rev %d' % (old_path,old_rev, |
|---|
| 200 | new_path,new_rev) ) |
|---|
| 201 | |
|---|
| 202 | if old_node.kind == Node.FILE : |
|---|
| 203 | if old_node.rev != new_node.rev : |
|---|
| 204 | yield (old_node,new_node,Node.FILE,Changeset.EDIT) |
|---|
| 205 | return |
|---|
| 206 | |
|---|
| 207 | c = self.db.cursor() |
|---|
| 208 | c.execute( 'SELECT rev,path FROM darcs_node_changes ' |
|---|
| 209 | 'WHERE node_id = %s AND rev >= %s AND rev <= %s', |
|---|
| 210 | (node_id,old_rev,new_rev) ) |
|---|
| 211 | node_set = dict() |
|---|
| 212 | node_list = [] |
|---|
| 213 | c1 = self.db.cursor() |
|---|
| 214 | for rev,path in c : |
|---|
| 215 | c1.execute( 'SELECT node_id FROM darcs_node_changes ' |
|---|
| 216 | 'WHERE rev = %s AND path LIKE %s', |
|---|
| 217 | (rev,path+'/%') ) |
|---|
| 218 | for nid, in c1 : |
|---|
| 219 | if nid not in node_set : |
|---|
| 220 | node_set[nid] = 1 |
|---|
| 221 | node_list.append( nid ) |
|---|
| 222 | for nid in node_list : |
|---|
| 223 | old_node = new_node = None |
|---|
| 224 | c1.execute( 'SELECT rev,path FROM darcs_node_changes ' |
|---|
| 225 | 'WHERE node_id = %s AND rev < %s ' |
|---|
| 226 | 'ORDER BY rev DESC LIMIT 1', |
|---|
| 227 | (nid,old_rev) ) |
|---|
| 228 | row = c1.fetchone() |
|---|
| 229 | if row is not None : |
|---|
| 230 | rev,path = row |
|---|
| 231 | old_node = self.get_node( path, rev ) |
|---|
| 232 | c1.execute( 'SELECT rev,path,the_change FROM darcs_node_changes ' |
|---|
| 233 | 'WHERE node_id = %s AND rev >= %s AND rev <= %s ' |
|---|
| 234 | 'ORDER BY rev DESC LIMIT 1', |
|---|
| 235 | (nid,old_rev,new_rev) ) |
|---|
| 236 | rev,path,change = c1.fetchone() |
|---|
| 237 | if change != CHANGE_REMOVED : |
|---|
| 238 | new_node = self.get_node( path, rev ) |
|---|
| 239 | assert (old_node is not None) or (new_node is not None) |
|---|
| 240 | kind = old_node and old_node.kind or new_node.kind |
|---|
| 241 | if old_node is None : |
|---|
| 242 | change = Changeset.ADD |
|---|
| 243 | elif new_node is None : |
|---|
| 244 | change = Changeset.DELETE |
|---|
| 245 | elif old_node.path != new_node.path : |
|---|
| 246 | change = Changeset.MOVE |
|---|
| 247 | else : |
|---|
| 248 | change = Changeset.EDIT |
|---|
| 249 | yield (old_node,new_node,kind,change) |
|---|
| 250 | |
|---|
| 251 | def sync( self, rev_callback=None ) : |
|---|
| 252 | # This is called by trac-admin resync |
|---|
| 253 | update_darcsdb(self.db, self.__cmd, self.log, rev_callback=rev_callback) |
|---|
| 254 | |
|---|
| 255 | class DarcsNode( Node ) : |
|---|
| 256 | def __init__( self, node_id, node_type, path, rev, |
|---|
| 257 | db, cmd ) : |
|---|
| 258 | kind = _node_type_map[node_type] |
|---|
| 259 | Node.__init__( self, path, rev, kind ) |
|---|
| 260 | self.__node_id = node_id |
|---|
| 261 | self.__node_type = node_type |
|---|
| 262 | self.__db = db |
|---|
| 263 | self.__cmd = cmd |
|---|
| 264 | self.created_path = path |
|---|
| 265 | self.created_rev = rev |
|---|
| 266 | |
|---|
| 267 | def _get_node_id( self ) : |
|---|
| 268 | return self.__node_id |
|---|
| 269 | |
|---|
| 270 | def get_content( self ) : |
|---|
| 271 | if self.__node_type == NODE_DIR_TYPE : |
|---|
| 272 | return None |
|---|
| 273 | c = self.__db.cursor() |
|---|
| 274 | # check if the file content is there in the cache |
|---|
| 275 | c.execute( 'SELECT content FROM darcs_cache ' |
|---|
| 276 | 'WHERE node_id = %s AND rev = %s', |
|---|
| 277 | (self.__node_id,self.rev) ) |
|---|
| 278 | row = c.fetchone() |
|---|
| 279 | if row is not None : |
|---|
| 280 | # if present just return it |
|---|
| 281 | data = str(buffer(row[0])) |
|---|
| 282 | return StringIO.StringIO( data ) |
|---|
| 283 | # load the file content from the repo |
|---|
| 284 | c.execute( 'SELECT hash FROM darcs_revisions ' + |
|---|
| 285 | 'WHERE rev = %s', (self.rev,) ) |
|---|
| 286 | hash = c.fetchone()[0] |
|---|
| 287 | data = self.__cmd.cat( hash, self.path ) |
|---|
| 288 | |
|---|
| 289 | # save the file content in the cache |
|---|
| 290 | |
|---|
| 291 | # UGLY HACK: the following line is required otherwise |
|---|
| 292 | # db.commit() (pysqlite-2.3.2,winxp) throws |
|---|
| 293 | # OperationalError: 'SQL logic error or missing database' |
|---|
| 294 | #self.__db.cnx.cnx.isolation_level = None |
|---|
| 295 | # END UGLY HACK |
|---|
| 296 | |
|---|
| 297 | c = self.__db.cursor() |
|---|
| 298 | c.execute( 'INSERT INTO darcs_cache(node_id,rev,content) ' |
|---|
| 299 | 'VALUES (%s,%s,%s)', |
|---|
| 300 | (self.__node_id,self.rev,buffer(data)) ) |
|---|
| 301 | self.__db.commit() |
|---|
| 302 | return StringIO.StringIO( data ) |
|---|
| 303 | |
|---|
| 304 | def get_entries( self ) : |
|---|
| 305 | if self.__node_type == NODE_FILE_TYPE : |
|---|
| 306 | return |
|---|
| 307 | q = query_nodes_for_revision( self.rev ) |
|---|
| 308 | if self.__node_id is None : |
|---|
| 309 | q += ' AND dnc.parent_id IS NULL' |
|---|
| 310 | else : |
|---|
| 311 | q += ' AND dnc.parent_id = %d' % self.__node_id |
|---|
| 312 | c = self.__db.cursor() |
|---|
| 313 | c.execute( q ) |
|---|
| 314 | for node_id,rev,path,_ in c : |
|---|
| 315 | node_type = get_node_type( self.__db, node_id ) |
|---|
| 316 | yield DarcsNode( node_id, node_type, path, rev, |
|---|
| 317 | self.__db, self.__cmd ) |
|---|
| 318 | |
|---|
| 319 | def get_history( self, limit=None ) : |
|---|
| 320 | if self.path == '/' : |
|---|
| 321 | for i in range(self.rev,0,-1) : |
|---|
| 322 | yield ( self.path, i, Changeset.EDIT ) |
|---|
| 323 | return |
|---|
| 324 | c = self.__db.cursor() |
|---|
| 325 | q = 'SELECT path,rev,the_change FROM darcs_node_changes ' + \ |
|---|
| 326 | 'WHERE node_id = %s AND rev <= %s ' + \ |
|---|
| 327 | 'ORDER BY rev DESC' |
|---|
| 328 | if limit is not None : |
|---|
| 329 | q += ' LIMIT %d' % limit |
|---|
| 330 | c.execute( q, (self.__node_id,self.rev) ) |
|---|
| 331 | for path,rev,change in c : |
|---|
| 332 | yield ( path, rev, _change_map[change] ) |
|---|
| 333 | |
|---|
| 334 | def get_properties( self ) : |
|---|
| 335 | return {} |
|---|
| 336 | |
|---|
| 337 | def get_content_length( self ) : |
|---|
| 338 | if self.isdir : |
|---|
| 339 | return None |
|---|
| 340 | return len(self.get_content().read()) |
|---|
| 341 | |
|---|
| 342 | def get_content_type( self ) : |
|---|
| 343 | if self.isdir : |
|---|
| 344 | return None |
|---|
| 345 | return mimetypes.guess_type( self.path )[0] |
|---|
| 346 | |
|---|
| 347 | def get_name( self ) : |
|---|
| 348 | return os.path.split( self.path )[1] |
|---|
| 349 | |
|---|
| 350 | def get_last_modified( self ) : |
|---|
| 351 | if self.__node_id is None : |
|---|
| 352 | return 0 |
|---|
| 353 | c = self.__db.cursor() |
|---|
| 354 | c.execute( 'SELECT rev FROM darcs_node_changes ' + |
|---|
| 355 | 'WHERE node_id = %s AND rev = %s', |
|---|
| 356 | (self.__node_id,self.rev) ) |
|---|
| 357 | rev = c.fetchone()[0] |
|---|
| 358 | c.execute( 'SELECT time FROM darcs_revisions ' + |
|---|
| 359 | 'WHERE rev = %s', (rev,) ) |
|---|
| 360 | return to_utc_datetime(c.fetchone()[0]) |
|---|
| 361 | |
|---|
| 362 | class DarcsChangeset( Changeset ) : |
|---|
| 363 | def __init__( self, db, rev ) : |
|---|
| 364 | c = db.cursor() |
|---|
| 365 | c.execute( 'SELECT author,time,name,comment ' + |
|---|
| 366 | 'FROM darcs_revisions WHERE rev = %s', (rev,) ) |
|---|
| 367 | row = c.fetchone() |
|---|
| 368 | if row is None : |
|---|
| 369 | raise NoSuchChangeset( rev ) |
|---|
| 370 | author,date,name,comment = row |
|---|
| 371 | date = to_utc_datetime(date) |
|---|
| 372 | msg = name |
|---|
| 373 | if comment : |
|---|
| 374 | msg += '\n' + comment |
|---|
| 375 | Changeset.__init__( self, rev, msg, author, date ) |
|---|
| 376 | self.__db = db |
|---|
| 377 | |
|---|
| 378 | def get_changes( self ) : |
|---|
| 379 | c = self.__db.cursor() |
|---|
| 380 | c.execute( 'SELECT node_id,path,the_change FROM darcs_node_changes ' + |
|---|
| 381 | 'WHERE rev = %s', (self.rev,) ) |
|---|
| 382 | for node_id,path,change in c : |
|---|
| 383 | node_type = get_node_type( self.__db, node_id ) |
|---|
| 384 | kind = _node_type_map[node_type] |
|---|
| 385 | if change == CHANGE_ADDED : |
|---|
| 386 | prev_path = prev_rev = None |
|---|
| 387 | else : |
|---|
| 388 | prev_path,prev_rev = get_prev_path_rev( self.__db, |
|---|
| 389 | node_id, self.rev ) |
|---|
| 390 | change = _change_map[change] |
|---|
| 391 | yield (path,kind,change,prev_path,prev_rev) |
|---|
| 392 | |
|---|
| 393 | def get_properties( self ) : |
|---|
| 394 | return [] |
|---|