Make stgit.config use git-repo-config.
[stgit] / stgit / config.py
CommitLineData
41a6d859
CM
1"""Handles the Stacked GIT configuration files
2"""
3
4__copyright__ = """
5Copyright (C) 2005, Catalin Marinas <catalin.marinas@gmail.com>
6
7This program is free software; you can redistribute it and/or modify
8it under the terms of the GNU General Public License version 2 as
9published by the Free Software Foundation.
10
11This program is distributed in the hope that it will be useful,
12but WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14GNU General Public License for more details.
15
16You should have received a copy of the GNU General Public License
17along with this program; if not, write to the Free Software
18Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19"""
20
c73e63b7 21import os, re
170f576b 22from stgit import basedir
41a6d859 23
c73e63b7
YD
24class GitConfigException(Exception):
25 pass
26
27class GitConfig:
28 __defaults={
29 'stgit.autoresolved': 'no',
30 'stgit.smtpserver': 'localhost:25',
31 'stgit.smtpdelay': '5',
32 'stgit.pullcmd': 'git-pull',
33 'stgit.merger': 'diff3 -L current -L ancestor -L patched -m -E ' \
34 '"%(branch1)s" "%(ancestor)s" "%(branch2)s" > "%(output)s"',
35 'stgit.autoimerge': 'no',
36 'stgit.keeporig': 'yes',
37 'stgit.keepoptimized': 'no',
38 'stgit.extensions': '.ancestor .current .patched',
39 'stgit.shortnr': '5'
40 }
41
42 def __run(self, cmd, args=None):
43 """__run: runs cmd using spawnvp.
44
45 Runs cmd using spawnvp. The shell is avoided so it won't mess up
46 our arguments. If args is very large, the command is run multiple
47 times; args is split xargs style: cmd is passed on each
48 invocation. Unlike xargs, returns immediately if any non-zero
49 return code is received.
50 """
51
52 args_l=cmd.split()
53 if args is None:
54 args = []
55 for i in range(0, len(args)+1, 100):
56 r=os.spawnvp(os.P_WAIT, args_l[0], args_l + args[i:min(i+100, len(args))])
57 if r:
58 return r
59 return 0
60
61 def get(self, name):
62 stream = os.popen('git repo-config --get %s' % name, 'r')
63 value = stream.readline().strip()
64 stream.close()
65 if len(value) > 0:
66 return value
67 elif (self.__defaults.has_key(name)):
68 return self.__defaults[name]
69 else:
70 return None
71
72 def getall(self, name):
73 stream = os.popen('git repo-config --get-all %s' % name, 'r')
74 values = [line.strip() for line in stream]
75 stream.close()
76 return values
77
78 def getint(self, name):
79 value = self.get(name)
80 if value.isdigit():
81 return int(value)
82 else:
83 raise GitConfigException, 'Value for "%s" is not an integer: "%s"' % (name, value)
84
85 def set(self, name, value):
86 self.__run('git-repo-config', [name, value])
87
88 def sections_matching(self, regexp):
89 """Takes a regexp with a single group, matches it against all
90 config variables, and returns a list whose members are the
91 group contents, for all variable names matching the regexp.
92 """
93 result = []
94 stream = os.popen('git repo-config --get-regexp "^%s$"' % regexp, 'r')
95 for line in stream:
96 m = re.match('^%s ' % regexp, line)
97 if m:
98 result.append(m.group(1))
99 stream.close()
100 return result
101
102config=GitConfig()
abcc2620 103
eee7283e
CM
104def config_setup():
105 global config
106
eee7283e 107 # Set the PAGER environment to the config value (if any)
c73e63b7
YD
108 pager = config.get('stgit.pager')
109 if pager:
110 os.environ['PAGER'] = pager
111 # FIXME: handle EDITOR the same way ?
eee7283e
CM
112
113class ConfigOption:
114 """Delayed cached reading of a configuration option.
115 """
116 def __init__(self, section, option):
117 self.__section = section
118 self.__option = option
119 self.__value = None
120
121 def __str__(self):
122 if not self.__value:
c73e63b7 123 self.__value = config.get(self.__section + '.' + self.__option)
eee7283e 124 return self.__value
d7fade4b
CM
125
126
127# cached extensions
128__extensions = None
129
130def file_extensions():
131 """Returns a dictionary with the conflict file extensions
132 """
133 global __extensions
134
135 if not __extensions:
c73e63b7 136 cfg_ext = config.get('stgit.extensions').split()
d7fade4b
CM
137 if len(cfg_ext) != 3:
138 raise CmdException, '"extensions" configuration error'
139
140 __extensions = { 'ancestor': cfg_ext[0],
141 'current': cfg_ext[1],
142 'patched': cfg_ext[2] }
143
144 return __extensions