Make stgit.config use git-repo-config.
[stgit] / stgit / config.py
index e6c9538..d42754b 100644 (file)
@@ -18,33 +18,127 @@ along with this program; if not, write to the Free Software
 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 """
 
-import os, ConfigParser
-
-
-if 'GIT_DIR' in os.environ:
-    __git_dir = os.environ['GIT_DIR']
-else:
-    __git_dir = '.git'
-
-config = ConfigParser.RawConfigParser()
-
-# Set the defaults
-config.add_section('stgit')
-config.set('stgit', 'autoresolved', 'no')
-config.set('stgit', 'smtpserver', 'localhost:25')
-config.set('stgit', 'smtpdelay', '2')
-config.set('stgit', 'merger',
-           'diff3 -L local -L older -L remote -m -E ' \
-           '"%(branch1)s" "%(ancestor)s" "%(branch2)s" > "%(output)s"')
-config.set('stgit', 'keeporig', 'yes')
-
-# Read the configuration files (if any) and override the default settings
-config.read('/etc/stgitrc')
-config.read(os.path.expanduser('~/.stgitrc'))
-config.read(os.path.join(__git_dir, 'stgitrc'))
-
-# [gitmergeonefile] section is deprecated. In case it exists copy the
-# options/values to the [stgit] one
-if config.has_section('gitmergeonefile'):
-    for option, value in config.items('gitmergeonefile'):
-        config.set('stgit', option, value)
+import os, re
+from stgit import basedir
+
+class GitConfigException(Exception):
+    pass
+
+class GitConfig:
+    __defaults={
+        'stgit.autoresolved':  'no',
+        'stgit.smtpserver':    'localhost:25',
+        'stgit.smtpdelay':     '5',
+        'stgit.pullcmd':       'git-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'
+        }
+
+    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):
+        stream = os.popen('git repo-config --get %s' % name, 'r')
+        value = stream.readline().strip()
+        stream.close()
+        if len(value) > 0:
+            return value
+        elif (self.__defaults.has_key(name)):
+            return self.__defaults[name]
+        else:
+            return None
+
+    def getall(self, name):
+        stream = os.popen('git repo-config --get-all %s' % name, 'r')
+        values = [line.strip() for line in stream]
+        stream.close()
+        return values
+
+    def getint(self, name):
+        value = self.get(name)
+        if value.isdigit():
+            return int(value)
+        else:
+            raise GitConfigException, 'Value for "%s" is not an integer: "%s"' % (name, value)
+
+    def set(self, name, value):
+        self.__run('git-repo-config', [name, value])
+
+    def sections_matching(self, regexp):
+        """Takes a regexp with a single group, matches it against all
+        config variables, and returns a list whose members are the
+        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:
+            m = re.match('^%s ' % regexp, line)
+            if m:
+                result.append(m.group(1))
+        stream.close()
+        return result
+        
+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
+    # FIXME: handle EDITOR the same way ?
+
+class ConfigOption:
+    """Delayed cached reading of a configuration option.
+    """
+    def __init__(self, section, option):
+        self.__section = section
+        self.__option = option
+        self.__value = None
+
+    def __str__(self):
+        if not self.__value:
+            self.__value = config.get(self.__section + '.' + self.__option)
+        return self.__value
+
+
+# cached extensions
+__extensions = None
+
+def file_extensions():
+    """Returns a dictionary with the conflict file extensions
+    """
+    global __extensions
+
+    if not __extensions:
+        cfg_ext = config.get('stgit.extensions').split()
+        if len(cfg_ext) != 3:
+            raise CmdException, '"extensions" configuration error'
+
+        __extensions = { 'ancestor': cfg_ext[0],
+                         'current':  cfg_ext[1],
+                         'patched':  cfg_ext[2] }
+
+    return __extensions