+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'
+ }
+
+ __cache={}
+
+ 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):
+ if self.__cache.has_key(name):
+ return self.__cache[name]
+
+ stream = os.popen('git repo-config --get %s' % name, 'r')
+ value = stream.readline().strip()
+ stream.close()
+ if len(value) > 0:
+ pass
+ elif (self.__defaults.has_key(name)):
+ value = self.__defaults[name]
+ else:
+ value = None
+
+ self.__cache[name] = value
+ return value
+
+ def getall(self, name):
+ if self.__cache.has_key(name):
+ return self.__cache[name]
+
+ stream = os.popen('git repo-config --get-all %s' % name, 'r')
+ values = [line.strip() for line in stream]
+ stream.close()
+
+ 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 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