# HG changeset patch # User Dirkjan Ochtman # Date 1244739150 -7200 # Node ID d4615986e1db89ea31639e49e15b4cbbb2a9f89c # Parent f137231f9d300b786032e51a31bbb11399d78d92 extract the filemap support into a separate class diff --git a/hgsubversion/hg_delta_editor.py b/hgsubversion/hg_delta_editor.py --- a/hgsubversion/hg_delta_editor.py +++ b/hgsubversion/hg_delta_editor.py @@ -97,7 +97,6 @@ class HgChangeReceiver(delta.Editor): author_host = self.ui.config('hgsubversion', 'defaulthost', uuid) authors = self.ui.config('hgsubversion', 'authormap') - filemap = self.ui.config('hgsubversion', 'filemap') tag_locations = self.ui.configlist('hgsubversion', 'tagpaths', ['tags']) self.usebranchnames = self.ui.configbool('hgsubversion', 'usebranchnames', True) @@ -130,10 +129,7 @@ class HgChangeReceiver(delta.Editor): if authors: self.authors.load(authors) self.lastdate = '1970-01-01 00:00:00 -0000' - self.includepaths = {} - self.excludepaths = {} - if filemap and os.path.exists(filemap): - self.readfilemap(filemap) + self.filemap = maps.FileMap(repo) def hashes(self): return dict((v, k) for (k, v) in self.revmap.iteritems()) @@ -270,41 +266,13 @@ class HgChangeReceiver(delta.Editor): path = path[1:] return path - def _is_file_included(self, subpath): - def checkpathinmap(path, mapping): - def rpairs(name): - yield '.', name - e = len(name) - while e != -1: - yield name[:e], name[e+1:] - e = name.rfind('/', 0, e) - - for pre, suf in rpairs(path): - try: - return mapping[pre] - except KeyError, err: - pass - return None - - if len(self.includepaths) and len(subpath): - inc = checkpathinmap(subpath, self.includepaths) - else: - inc = subpath - if len(self.excludepaths) and len(subpath): - exc = checkpathinmap(subpath, self.excludepaths) - else: - exc = None - if inc is None or exc is not None: - return False - return True - def _is_path_valid(self, path): if path is None: return False subpath = self._split_branch_path(path)[0] if subpath is None: return False - return self._is_file_included(subpath) + return subpath in self.filemap def _is_path_tag(self, path): """If path could represent the path to a tag, returns the potential tag @@ -718,41 +686,6 @@ class HgChangeReceiver(delta.Editor): new = self.repo.commitctx(ctx) self.ui.status('Marked branch %s as closed.\n' % (branch or 'default')) - def readfilemap(self, filemapfile): - self.ui.note( - ('Reading file map from %s\n') - % filemapfile) - def addpathtomap(path, mapping, mapname): - if path in mapping: - self.ui.warn(('Duplicate %s entry in %s: "%d"\n') % - (mapname, filemapfile, path)) - else: - self.ui.debug(('%sing %s\n') % - (mapname.capitalize().strip('e'), path)) - mapping[path] = path - - f = open(filemapfile, 'r') - for line in f: - if line.strip() == '' or line.strip()[0] == '#': - continue - try: - cmd, path = line.split(' ', 1) - cmd = cmd.strip() - path = path.strip() - if cmd == 'include': - addpathtomap(path, self.includepaths, 'include') - elif cmd == 'exclude': - addpathtomap(path, self.excludepaths, 'exclude') - else: - self.ui.warn( - ('Unknown filemap command %s\n') - % cmd) - except IndexError: - self.ui.warn( - ('Ignoring bad line in filemap %s: %s\n') - % (filemapfile, line.rstrip())) - f.close() - def _get_uuid(self): return open(os.path.join(self.meta_data_dir, 'uuid')).read() diff --git a/hgsubversion/maps.py b/hgsubversion/maps.py --- a/hgsubversion/maps.py +++ b/hgsubversion/maps.py @@ -135,3 +135,71 @@ class RevMap(dict): f.flush() f.close() dict.__setitem__(self, (revnum, branch), hash) + + +class FileMap(object): + + def __init__(self, repo): + self.ui = repo.ui + self.include = {} + self.exclude = {} + filemap = repo.ui.config('hgsubversion', 'filemap') + if filemap and os.path.exists(filemap): + self.load(filemap) + + def _rpairs(self, name): + yield '.', name + e = len(name) + while e != -1: + yield name[:e], name[e+1:] + e = name.rfind('/', 0, e) + + def check(self, map, path): + map = getattr(self, map) + for pre, suf in self._rpairs(path): + if pre not in map: + continue + return map[pre] + return None + + def __contains__(self, path): + if len(self.include) and len(path): + inc = self.check('include', path) + else: + inc = path + if len(self.exclude) and len(path): + exc = self.check('exclude', path) + else: + exc = None + if inc is None or exc is not None: + return False + return True + + def add(self, fn, map, path): + mapping = getattr(self, map) + if path in mapping: + msg = 'duplicate %s entry in %s: "%d"\n' + self.ui.warn(msg % (map, fn, path)) + return + bits = map.strip('e'), path + self.ui.debug('%sing %s\n' % bits) + mapping[path] = path + + def load(self, fn): + self.ui.note('reading file map from %s\n' % fn) + f = open(fn, 'r') + for line in f: + if line.strip() == '' or line.strip()[0] == '#': + continue + try: + cmd, path = line.split(' ', 1) + cmd = cmd.strip() + path = path.strip() + if cmd in ('include', 'exclude'): + self.add(fn, cmd, path) + continue + self.ui.warn('unknown filemap command %s\n' % cmd) + except IndexError: + msg = 'ignoring bad line in filemap %s: %s\n' + self.ui.warn(msg % (fn, line.rstrip())) + f.close() diff --git a/hgsubversion/stupid.py b/hgsubversion/stupid.py --- a/hgsubversion/stupid.py +++ b/hgsubversion/stupid.py @@ -82,7 +82,7 @@ def filteriterhunks(hg_editor): applycurrent = False for data in iterhunks(ui, fp, sourcefile): if data[0] == 'file': - if hg_editor._is_file_included(data[1][1]): + if data[1][1] in hg_editor.filemap: applycurrent = True else: applycurrent = False @@ -585,8 +585,7 @@ def convert_rev(ui, hg_editor, svn, r, t if '' in files_touched: files_touched.remove('') - excluded = [f for f in files_touched - if not hg_editor._is_file_included(f)] + excluded = [f for f in files_touched if f not in hg_editor.filemap] for f in excluded: files_touched.remove(f)