From a264e49b1c1c62950f442df07863d3cf92db8684 Mon Sep 17 00:00:00 2001 From: Samuel Tardieu Date: Fri, 22 May 2009 22:38:11 +0200 Subject: [PATCH] Load the whole config at once and cache it for future use MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Instead of loading the config values one at at time when we need then, read and parse the configuration files all at once using git config --list --null and cache the result. This should be a performance win. Signed-off-by: Samuel Tardieu Signed-off-by: Karl Hasselström --- stgit/config.py | 34 ++++++++++++++++++++-------------- 1 file changed, 20 insertions(+), 14 deletions(-) diff --git a/stgit/config.py b/stgit/config.py index dbca5fb..c40756c 100644 --- a/stgit/config.py +++ b/stgit/config.py @@ -40,25 +40,31 @@ class GitConfig: 'stgit.shortnr': '5' } - __cache={} + __cache = None + + def load(self): + """Load the whole configuration in __cache unless it has been + done already.""" + if self.__cache is not None: + return + self.__cache = {} + lines = Run('git', 'config', '--list', '--null').raw_output() + for line in filter(None, lines.split('\0')): + key, value = line.split('\n', 1) + self.__cache.setdefault(key, []).append(value) def get(self, name): - if self.__cache.has_key(name): - return self.__cache[name] - try: - value = Run('git', 'config', '--get', name).output_one_line() - except RunException: - value = self.__defaults.get(name, None) - self.__cache[name] = value - return value + self.load() + if name not in self.__cache: + self.__cache[name] = [self.__defaults.get(name, None)] + return self.__cache[name][0] def getall(self, name): - if self.__cache.has_key(name): + self.load() + try: return self.__cache[name] - values = Run('git', 'config', '--get-all', name - ).returns([0, 1]).output_lines() - self.__cache[name] = values - return values + except KeyError: + return [] def getint(self, name): value = self.get(name) -- 2.11.0