blob: 89344454c181fc5fb6448213bad28954b6691204 [file] [log] [blame]
"""Handles the Stacked GIT configuration files
__copyright__ = """
Copyright (C) 2005, Catalin Marinas <>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License version 2 as
published by the Free Software Foundation.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
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, re
from stgit import basedir
from stgit.exception import *
from import *
class GitConfigException(StgException):
class GitConfig:
'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'
def get(self, name):
if self.__cache.has_key(name):
return self.__cache[name]
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)
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()
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()
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:
return result
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