Load the whole config at once and cache it for future use
[stgit] / stgit / config.py
index 3eabc8c..c40756c 100644 (file)
@@ -31,41 +31,46 @@ class GitConfig:
         'stgit.autoresolved':  'no',
         'stgit.smtpserver':    'localhost:25',
         'stgit.smtpdelay':     '5',
-        'stgit.pullcmd':       'git-pull',
-        'stgit.fetchcmd':      'git-fetch',
+        'stgit.pullcmd':       'git pull',
+        'stgit.fetchcmd':      'git fetch',
         'stgit.pull-policy':   'pull',
-        'stgit.merger':                'diff3 -L current -L ancestor -L patched -m -E ' \
-                               '"%(branch1)s" "%(ancestor)s" "%(branch2)s" > "%(output)s"',
         'stgit.autoimerge':    'no',
-        'stgit.keeporig':      'yes',
         'stgit.keepoptimized': 'no',
         'stgit.extensions':    '.ancestor .current .patched',
         '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-repo-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-repo-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)
-        if value.isdigit():
+        if value == None:
+            return None
+        elif value.isdigit():
             return int(value)
         else:
             raise GitConfigException, 'Value for "%s" is not an integer: "%s"' % (name, value)
@@ -73,23 +78,23 @@ class GitConfig:
     def rename_section(self, from_name, to_name):
         """Rename a section in the config file. Silently do nothing if
         the section doesn't exist."""
-        Run('git-repo-config', '--rename-section', from_name, to_name
-            ).returns([0, 1]).run()
+        Run('git', 'config', '--rename-section', from_name, to_name
+            ).returns([0, 1, 128]).run()
         self.__cache.clear()
 
     def remove_section(self, name):
         """Remove a section in the config file. Silently do nothing if
         the section doesn't exist."""
-        Run('git-repo-config', '--remove-section', name
-            ).returns([0, 1]).discard_stderr().discard_output()
+        Run('git', 'config', '--remove-section', name
+            ).returns([0, 1, 128]).discard_stderr().discard_output()
         self.__cache.clear()
 
     def set(self, name, value):
-        Run('git-repo-config', name, value).run()
+        Run('git', 'config', name, value).run()
         self.__cache[name] = value
 
     def unset(self, name):
-        Run('git-repo-config', '--unset', name)
+        Run('git', 'config', '--unset', name)
         self.__cache[name] = None
 
     def sections_matching(self, regexp):
@@ -98,7 +103,7 @@ class GitConfig:
         group contents, for all variable names matching the regexp.
         """
         result = []
-        for line in Run('git-repo-config', '--get-regexp', '"^%s$"' % regexp
+        for line in Run('git', 'config', '--get-regexp', '"^%s$"' % regexp
                         ).returns([0, 1]).output_lines():
             m = re.match('^%s ' % regexp, line)
             if m: