Remove the resolved command
[stgit] / stgit / config.py
index b016fbd..f205e5b 100644 (file)
@@ -20,88 +20,82 @@ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 
 import os, re
 from stgit import basedir
+from stgit.exception import *
+from stgit.run import *
 
-class GitConfigException(Exception):
+class GitConfigException(StgException):
     pass
 
 class GitConfig:
     __defaults={
-        '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'
+        'stgit.shortnr': '5',
+        'stgit.pager':  'less -FRSX'
         }
 
-    __cache={}
+    __cache = None
 
-    def __run(self, cmd, args=None):
-        """__run: runs cmd using spawnvp.
-    
-        Runs cmd using spawnvp.  The shell is avoided so it won't mess up
-        our arguments.  If args is very large, the command is run multiple
-        times; args is split xargs style: cmd is passed on each
-        invocation.  Unlike xargs, returns immediately if any non-zero
-        return code is received.  
-        """
-        
-        args_l=cmd.split()
-        if args is None:
-            args = []
-        for i in range(0, len(args)+1, 100):
-            r=os.spawnvp(os.P_WAIT, args_l[0], args_l + args[i:min(i+100, len(args))])
-        if r:
-            return r
-        return 0
-    
-    def get(self, name):
-        if self.__cache.has_key(name):
-            return self.__cache[name]
+    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', '--null', '--list').raw_output()
+        for line in filter(None, lines.split('\0')):
+            key, value = line.split('\n', 1)
+            self.__cache.setdefault(key, []).append(value)
 
-        stream = os.popen('git repo-config --get %s' % name, 'r')
-        value = stream.readline().strip()
-        stream.close()
-        if len(value) > 0:
-            pass
-        elif (self.__defaults.has_key(name)):
-            value = self.__defaults[name]
-        else:
-            value = None
-
-        self.__cache[name] = value
-        return value
+    def get(self, name):
+        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]
-
-        stream = os.popen('git repo-config --get-all %s' % name, 'r')
-        values = [line.strip() for line in stream]
-        stream.close()
-
-        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)
 
     def rename_section(self, from_name, to_name):
-        self.__run('git-repo-config --rename-section', [from_name, to_name])
+        """Rename a section in the config file. Silently do nothing if
+        the section doesn't exist."""
+        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', 'config', '--remove-section', name
+            ).returns([0, 1, 128]).discard_stderr().discard_output()
+        self.__cache.clear()
 
     def set(self, name, value):
-        self.__run('git-repo-config', [name, value])
+        Run('git', 'config', name, value).run()
+        self.__cache[name] = value
+
+    def unset(self, name):
+        Run('git', 'config', '--unset', name)
+        self.__cache[name] = None
 
     def sections_matching(self, regexp):
         """Takes a regexp with a single group, matches it against all
@@ -109,23 +103,24 @@ class GitConfig:
         group contents, for all variable names matching the regexp.
         """
         result = []
-        stream = os.popen('git repo-config --get-regexp "^%s$"' % regexp, 'r')
-        for line in stream:
+        for line in Run('git', 'config', '--get-regexp', '"^%s$"' % regexp
+                        ).returns([0, 1]).output_lines():
             m = re.match('^%s ' % regexp, line)
             if m:
                 result.append(m.group(1))
-        stream.close()
         return result
+
+    def get_colorbool(self, name, stdout_is_tty):
+        """Invoke 'git config --get-colorbool' and return the result."""
+        return Run('git', 'config', '--get-colorbool', name,
+                   stdout_is_tty).output_one_line()
         
 config=GitConfig()
 
 def config_setup():
     global config
 
-    # Set the PAGER environment to the config value (if any)
-    pager = config.get('stgit.pager')
-    if pager:
-        os.environ['PAGER'] = pager
+    os.environ.setdefault('PAGER', config.get('stgit.pager'))
     # FIXME: handle EDITOR the same way ?
 
 class ConfigOption: