X-Git-Url: https://git.distorted.org.uk/~mdw/stgit/blobdiff_plain/41a6d8591d5962dbfe8e372fff10c60e06718083..c73e63b7d7733f1308c2c0c504144e93062bb489:/stgit/config.py diff --git a/stgit/config.py b/stgit/config.py index 3bbbd0d..d42754b 100644 --- a/stgit/config.py +++ b/stgit/config.py @@ -18,16 +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 +import os, re +from stgit import basedir +class GitConfigException(Exception): + pass -if 'GIT_DIR' in os.environ: - __git_dir = os.environ['GIT_DIR'] -else: - __git_dir = '.git' +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' + } -config = ConfigParser.RawConfigParser() + 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 -config.readfp(file('/etc/stgitrc')) -config.read(os.path.expanduser('~/.stgitrc')) -config.read(os.path.join(__git_dir, 'stgitrc')) + 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