X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~mdw/git/stgit/blobdiff_plain/f7ed76a9b6158888e8efe2faef9c095802fd93fe..dbb0cf2c1d46199f8a4cc5e1b52b9a24494f794e:/stgit/config.py diff --git a/stgit/config.py b/stgit/config.py index f5fbdab..8934445 100644 --- a/stgit/config.py +++ b/stgit/config.py @@ -18,90 +18,103 @@ 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 -from StringIO import StringIO +import os, re from stgit import basedir - -config = ConfigParser.RawConfigParser() - -def git_config(filename): - """Open a git config file and convert it to be understood by - Python.""" - try: - f = file(filename) - cont = False - lines = [] - for line in f: - line = line.strip() - - if cont: - # continued line, add a space at the beginning - line = ' ' + line - - if line and line[-1] == '\\': - line = line[:-1].rstrip() - cont = True - else: - line = line + '\n' - cont = False - - lines.append(line) - - f.close() - cfg_str = ''.join(lines) - except IOError: - cfg_str = '' - - strio = StringIO(cfg_str) - strio.name = filename - - return strio +from stgit.exception import * +from stgit.run import * + +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.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={} + + 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 + + def getall(self, name): + if self.__cache.has_key(name): + return self.__cache[name] + values = Run('git', 'config', '--get-all', name + ).returns([0, 1]).output_lines() + self.__cache[name] = values + 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 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', 'config', '--rename-section', from_name, to_name + ).returns([0, 1]).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]).discard_stderr().discard_output() + self.__cache.clear() + + def set(self, 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 + config variables, and returns a list whose members are the + group contents, for all variable names matching the regexp. + """ + result = [] + 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)) + return result + +config=GitConfig() def config_setup(): global config - # Set the defaults - config.add_section('stgit') - config.set('stgit', 'autoresolved', 'no') - config.set('stgit', 'smtpserver', 'localhost:25') - config.set('stgit', 'smtpdelay', '5') - config.set('stgit', 'pullcmd', 'git-pull') - config.set('stgit', 'merger', - 'diff3 -L current -L ancestor -L patched -m -E ' \ - '"%(branch1)s" "%(ancestor)s" "%(branch2)s" > "%(output)s"') - config.set('stgit', 'autoimerge', 'no') - config.set('stgit', 'keeporig', 'yes') - config.set('stgit', 'keepoptimized', 'no') - config.set('stgit', 'extensions', '.ancestor .current .patched') - - # Read the configuration files (if any) and override the default settings - # stgitrc are read for backward compatibility - config.read('/etc/stgitrc') - config.read(os.path.expanduser('~/.stgitrc')) - config.read(os.path.join(basedir.get(), 'stgitrc')) - - # GIT configuration files can have a [stgit] section - try: - global_config = os.environ['GIT_CONFIG'] - except KeyError: - global_config = os.path.expanduser('~/.gitconfig') - try: - local_config = os.environ['GIT_CONFIG_LOCAL'] - except KeyError: - local_config = os.path.join(basedir.get(), 'config') - config.readfp(git_config(global_config)) - config.readfp(git_config(local_config)) - # Set the PAGER environment to the config value (if any) - if config.has_option('stgit', 'pager'): - os.environ['PAGER'] = config.get('stgit', 'pager') - - # [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) - + 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. @@ -113,7 +126,7 @@ class ConfigOption: def __str__(self): if not self.__value: - self.__value = config.get(self.__section, self.__option) + self.__value = config.get(self.__section + '.' + self.__option) return self.__value @@ -126,7 +139,7 @@ def file_extensions(): global __extensions if not __extensions: - cfg_ext = config.get('stgit', 'extensions').split() + cfg_ext = config.get('stgit.extensions').split() if len(cfg_ext) != 3: raise CmdException, '"extensions" configuration error'