From d19f02083604b3f999f9ccb486e8e6c9bd7de4d8 Mon Sep 17 00:00:00 2001 From: Han-Wen Nienhuys Date: Tue, 13 Feb 2007 18:01:35 +0100 Subject: [PATCH] cache inventory, saving on lookup time. --- darcs2git.py | 55 +++++++++++++++++++++++++++++++++++-------------------- 1 file changed, 35 insertions(+), 20 deletions(-) diff --git a/darcs2git.py b/darcs2git.py index 72533c9..4285aa1 100644 --- a/darcs2git.py +++ b/darcs2git.py @@ -10,7 +10,9 @@ # - time zones # - file modes # - use binary search to find from-patch ica. conflict. -# +# - use checkpointing to optimize speed? +# - use get --partial ? + import glob import os @@ -105,17 +107,17 @@ test end result.""") if not args: p.print_help () sys.exit (2) - + + options.basename = os.path.basename (args[0]).replace ('.darcs', '') if not options.target_git_repo: - p = args[0] - p = os.path.abspath (p) - options.target_git_repo = os.path.basename (p).replace ('.darcs', '') - options.target_git_repo += '.git' - + options.target_git_repo = options.basename + '.git' + if options.debug: global log_file name = options.target_git_repo.replace ('.git', '.log') - + if name == options.target_git_repo: + name += '.log' + progress ("Shell log to %s" % name) log_file = open (name, 'w') @@ -163,15 +165,16 @@ class DarcsConversionRepo: self._current_number = -1 self._is_valid = -1 - - def is_valid (self): + self._inventory_dict = None + + def is_contiguous (self): if not self._is_valid: return False darcs_dir = self.dir + '/_darcs' inv = self.inventory () for p in self.patches[:self._current_number + 1]: - if p.short_id () not in inv: + if not self.has_patch (p): return False if self._current_number + 1 != len (inv.split ('\n[')): @@ -180,13 +183,9 @@ class DarcsConversionRepo: return True def has_patch (self, p): - id = p.attributes['hash'] - darcs_dir = self.dir + '/_darcs' - f = darcs_dir + '/patches/' + id - if not os.path.exists (f): - return False + assert self._is_valid - return p.short_id () in self.inventory () + return self.inventory_dict ().has_key (p.short_id ()) def pristine_tree (self): return self.dir + '/_darcs/pristine' @@ -198,7 +197,10 @@ class DarcsConversionRepo: dir = self.dir progress ('Rewinding %d patches' % count) system ('cd %(dir)s && echo ay|darcs obliterate --ignore-times --last %(count)d' % locals ()) - + d = self.inventory_dict () + for p in self.patches[self._current_number - count:self._current_number+1]: + del d[p.short_id ()] + def clean (self): system ('rm -rf %s' % self.dir) @@ -212,15 +214,17 @@ class DarcsConversionRepo: dir = self.dir progress ('Pull patch %d' % patch.number) - self._current_number = patch.number system ('cd %(dir)s && darcs pull --ignore-times --quiet --all --match "hash %(id)s" %(source_repo)s ' % locals ()) + self._current_number = patch.number + def create_fresh (self): dir = self.dir system ('rm -rf %(dir)s && mkdir %(dir)s && darcs init --repo %(dir)s' % locals ()) self._is_valid = True self._current_number = -1 + self._inventory_dict = None def inventory (self): darcs_dir = self.dir + '/_darcs' @@ -229,6 +233,17 @@ class DarcsConversionRepo: i += open (f).read () return i + def inventory_dict (self): + if type (self._inventory_dict) == type ({}): + return self._inventory_dict + + self._inventory_dict = {} + + def note_patch (m): + self._inventory_dict[m.group (1)] = 1 + + re.sub (r'\n([^*\n])+\*\*([0-9]+)', note_patch, self.inventory ()) + def start_at (self, num): progress ('Go to patch %d' % num) @@ -260,7 +275,7 @@ class DarcsConversionRepo: It might be quicker and/or more correct to wind/rewind the repo with pull and unpull.""" - valid = self.is_valid () + valid = self.is_contiguous () where = '' if valid: where = 'at %d' % self._current_number -- 2.11.4.GIT