chiark / gitweb /
When reading a config value, pick the last value, not the first
[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
87c93eab 23from stgit.exception import *
f0de3f92 24from stgit.run import *
41a6d859 25
87c93eab 26class GitConfigException(StgException):
c73e63b7
YD
27 pass
28
29class GitConfig:
30 __defaults={
c73e63b7
YD
31 'stgit.smtpserver': 'localhost:25',
32 'stgit.smtpdelay': '5',
1576d681
CM
33 'stgit.pullcmd': 'git pull',
34 'stgit.fetchcmd': 'git fetch',
3b3c26fa 35 'stgit.pull-policy': 'pull',
c73e63b7 36 'stgit.autoimerge': 'no',
c73e63b7
YD
37 'stgit.keepoptimized': 'no',
38 'stgit.extensions': '.ancestor .current .patched',
afddd62b 39 'stgit.shortnr': '5',
e6bca570 40 'stgit.pager': 'less'
c73e63b7
YD
41 }
42
a264e49b
ST
43 __cache = None
44
45 def load(self):
46 """Load the whole configuration in __cache unless it has been
47 done already."""
48 if self.__cache is not None:
49 return
50 self.__cache = {}
ba6208ec 51 lines = Run('git', 'config', '--null', '--list').raw_output()
a264e49b
ST
52 for line in filter(None, lines.split('\0')):
53 key, value = line.split('\n', 1)
54 self.__cache.setdefault(key, []).append(value)
9a4bf454 55
c73e63b7 56 def get(self, name):
a264e49b
ST
57 self.load()
58 if name not in self.__cache:
59 self.__cache[name] = [self.__defaults.get(name, None)]
d34d6e35 60 return self.__cache[name][-1]
c73e63b7
YD
61
62 def getall(self, name):
a264e49b
ST
63 self.load()
64 try:
9a4bf454 65 return self.__cache[name]
a264e49b
ST
66 except KeyError:
67 return []
c73e63b7
YD
68
69 def getint(self, name):
70 value = self.get(name)
e928a3ec
KH
71 if value == None:
72 return None
73 elif value.isdigit():
c73e63b7
YD
74 return int(value)
75 else:
76 raise GitConfigException, 'Value for "%s" is not an integer: "%s"' % (name, value)
77
cb5be4c3 78 def rename_section(self, from_name, to_name):
f0de3f92
KH
79 """Rename a section in the config file. Silently do nothing if
80 the section doesn't exist."""
cd885e08 81 Run('git', 'config', '--rename-section', from_name, to_name
a66b9072 82 ).returns([0, 1, 128]).run()
8591add9 83 self.__cache.clear()
cb5be4c3 84
9a6bcbe2
KH
85 def remove_section(self, name):
86 """Remove a section in the config file. Silently do nothing if
87 the section doesn't exist."""
cd885e08 88 Run('git', 'config', '--remove-section', name
a66b9072 89 ).returns([0, 1, 128]).discard_stderr().discard_output()
9a6bcbe2
KH
90 self.__cache.clear()
91
c73e63b7 92 def set(self, name, value):
cd885e08 93 Run('git', 'config', name, value).run()
8591add9 94 self.__cache[name] = value
c73e63b7 95
0aee23c2 96 def unset(self, name):
641ec552
CP
97 Run('git', 'config', '--unset', name).run()
98 self.__cache[name] = [None]
0aee23c2 99
c73e63b7
YD
100 def sections_matching(self, regexp):
101 """Takes a regexp with a single group, matches it against all
102 config variables, and returns a list whose members are the
103 group contents, for all variable names matching the regexp.
104 """
105 result = []
cd885e08 106 for line in Run('git', 'config', '--get-regexp', '"^%s$"' % regexp
f0de3f92 107 ).returns([0, 1]).output_lines():
c73e63b7
YD
108 m = re.match('^%s ' % regexp, line)
109 if m:
110 result.append(m.group(1))
c73e63b7 111 return result
afddd62b
CM
112
113 def get_colorbool(self, name, stdout_is_tty):
114 """Invoke 'git config --get-colorbool' and return the result."""
115 return Run('git', 'config', '--get-colorbool', name,
116 stdout_is_tty).output_one_line()
c73e63b7
YD
117
118config=GitConfig()
abcc2620 119
eee7283e
CM
120def config_setup():
121 global config
122
afddd62b 123 os.environ.setdefault('PAGER', config.get('stgit.pager'))
e6bca570 124 os.environ.setdefault('LESS', '-FRSX')
c73e63b7 125 # FIXME: handle EDITOR the same way ?
eee7283e
CM
126
127class ConfigOption:
128 """Delayed cached reading of a configuration option.
129 """
130 def __init__(self, section, option):
131 self.__section = section
132 self.__option = option
133 self.__value = None
134
135 def __str__(self):
136 if not self.__value:
c73e63b7 137 self.__value = config.get(self.__section + '.' + self.__option)
eee7283e 138 return self.__value
d7fade4b
CM
139
140
141# cached extensions
142__extensions = None
143
144def file_extensions():
145 """Returns a dictionary with the conflict file extensions
146 """
147 global __extensions
148
149 if not __extensions:
c73e63b7 150 cfg_ext = config.get('stgit.extensions').split()
d7fade4b
CM
151 if len(cfg_ext) != 3:
152 raise CmdException, '"extensions" configuration error'
153
154 __extensions = { 'ancestor': cfg_ext[0],
155 'current': cfg_ext[1],
156 'patched': cfg_ext[2] }
157
158 return __extensions