X-Git-Url: https://git.distorted.org.uk/~mdw/stgit/blobdiff_plain/170f576bb9eac1dafc139de7b51226d78d31cbbe..a264e49b1c1c62950f442df07863d3cf92db8684:/stgit/config.py diff --git a/stgit/config.py b/stgit/config.py index e28633d..c40756c 100644 --- a/stgit/config.py +++ b/stgit/config.py @@ -18,30 +18,138 @@ 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 - +import os, re from stgit import basedir +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.autoimerge': 'no', + 'stgit.keepoptimized': 'no', + 'stgit.extensions': '.ancestor .current .patched', + 'stgit.shortnr': '5' + } + + __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): + 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): + self.load() + try: + return self.__cache[name] + except KeyError: + return [] + + def getint(self, name): + value = self.get(name) + 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): + """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): + 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 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] } -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(basedir.get(), '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) + return __extensions