Remove the resolved command
[stgit] / stgit / config.py
1 """Handles the Stacked GIT configuration files
2 """
3
4 __copyright__ = """
5 Copyright (C) 2005, Catalin Marinas <catalin.marinas@gmail.com>
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License version 2 as
9 published by the Free Software Foundation.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 """
20
21 import os, re
22 from stgit import basedir
23 from stgit.exception import *
24 from stgit.run import *
25
26 class GitConfigException(StgException):
27 pass
28
29 class GitConfig:
30 __defaults={
31 'stgit.smtpserver': 'localhost:25',
32 'stgit.smtpdelay': '5',
33 'stgit.pullcmd': 'git pull',
34 'stgit.fetchcmd': 'git fetch',
35 'stgit.pull-policy': 'pull',
36 'stgit.autoimerge': 'no',
37 'stgit.keepoptimized': 'no',
38 'stgit.extensions': '.ancestor .current .patched',
39 'stgit.shortnr': '5',
40 'stgit.pager': 'less -FRSX'
41 }
42
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 = {}
51 lines = Run('git', 'config', '--null', '--list').raw_output()
52 for line in filter(None, lines.split('\0')):
53 key, value = line.split('\n', 1)
54 self.__cache.setdefault(key, []).append(value)
55
56 def get(self, name):
57 self.load()
58 if name not in self.__cache:
59 self.__cache[name] = [self.__defaults.get(name, None)]
60 return self.__cache[name][0]
61
62 def getall(self, name):
63 self.load()
64 try:
65 return self.__cache[name]
66 except KeyError:
67 return []
68
69 def getint(self, name):
70 value = self.get(name)
71 if value == None:
72 return None
73 elif value.isdigit():
74 return int(value)
75 else:
76 raise GitConfigException, 'Value for "%s" is not an integer: "%s"' % (name, value)
77
78 def rename_section(self, from_name, to_name):
79 """Rename a section in the config file. Silently do nothing if
80 the section doesn't exist."""
81 Run('git', 'config', '--rename-section', from_name, to_name
82 ).returns([0, 1, 128]).run()
83 self.__cache.clear()
84
85 def remove_section(self, name):
86 """Remove a section in the config file. Silently do nothing if
87 the section doesn't exist."""
88 Run('git', 'config', '--remove-section', name
89 ).returns([0, 1, 128]).discard_stderr().discard_output()
90 self.__cache.clear()
91
92 def set(self, name, value):
93 Run('git', 'config', name, value).run()
94 self.__cache[name] = value
95
96 def unset(self, name):
97 Run('git', 'config', '--unset', name)
98 self.__cache[name] = None
99
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 = []
106 for line in Run('git', 'config', '--get-regexp', '"^%s$"' % regexp
107 ).returns([0, 1]).output_lines():
108 m = re.match('^%s ' % regexp, line)
109 if m:
110 result.append(m.group(1))
111 return result
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()
117
118 config=GitConfig()
119
120 def config_setup():
121 global config
122
123 os.environ.setdefault('PAGER', config.get('stgit.pager'))
124 # FIXME: handle EDITOR the same way ?
125
126 class ConfigOption:
127 """Delayed cached reading of a configuration option.
128 """
129 def __init__(self, section, option):
130 self.__section = section
131 self.__option = option
132 self.__value = None
133
134 def __str__(self):
135 if not self.__value:
136 self.__value = config.get(self.__section + '.' + self.__option)
137 return self.__value
138
139
140 # cached extensions
141 __extensions = None
142
143 def file_extensions():
144 """Returns a dictionary with the conflict file extensions
145 """
146 global __extensions
147
148 if not __extensions:
149 cfg_ext = config.get('stgit.extensions').split()
150 if len(cfg_ext) != 3:
151 raise CmdException, '"extensions" configuration error'
152
153 __extensions = { 'ancestor': cfg_ext[0],
154 'current': cfg_ext[1],
155 'patched': cfg_ext[2] }
156
157 return __extensions