Properly remove all config for a deleted branch
[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.run import *
24
25 class GitConfigException(Exception):
26 pass
27
28 class GitConfig:
29 __defaults={
30 'stgit.autoresolved': 'no',
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.merger': 'diff3 -L current -L ancestor -L patched -m -E ' \
37 '"%(branch1)s" "%(ancestor)s" "%(branch2)s" > "%(output)s"',
38 'stgit.autoimerge': 'no',
39 'stgit.keeporig': 'yes',
40 'stgit.keepoptimized': 'no',
41 'stgit.extensions': '.ancestor .current .patched',
42 'stgit.shortnr': '5'
43 }
44
45 __cache={}
46
47 def get(self, name):
48 if self.__cache.has_key(name):
49 return self.__cache[name]
50 try:
51 value = Run('git-repo-config', '--get', name).output_one_line()
52 except RunException:
53 value = self.__defaults.get(name, None)
54 self.__cache[name] = value
55 return value
56
57 def getall(self, name):
58 if self.__cache.has_key(name):
59 return self.__cache[name]
60 values = Run('git-repo-config', '--get-all', name
61 ).returns([0, 1]).output_lines()
62 self.__cache[name] = values
63 return values
64
65 def getint(self, name):
66 value = self.get(name)
67 if value.isdigit():
68 return int(value)
69 else:
70 raise GitConfigException, 'Value for "%s" is not an integer: "%s"' % (name, value)
71
72 def rename_section(self, from_name, to_name):
73 """Rename a section in the config file. Silently do nothing if
74 the section doesn't exist."""
75 Run('git-repo-config', '--rename-section', from_name, to_name
76 ).returns([0, 1]).run()
77 self.__cache.clear()
78
79 def remove_section(self, name):
80 """Remove a section in the config file. Silently do nothing if
81 the section doesn't exist."""
82 Run('git-repo-config', '--remove-section', name
83 ).returns([0, 1]).discard_stderr().discard_output()
84 self.__cache.clear()
85
86 def set(self, name, value):
87 Run('git-repo-config', name, value).run()
88 self.__cache[name] = value
89
90 def unset(self, name):
91 Run('git-repo-config', '--unset', name)
92 self.__cache[name] = None
93
94 def sections_matching(self, regexp):
95 """Takes a regexp with a single group, matches it against all
96 config variables, and returns a list whose members are the
97 group contents, for all variable names matching the regexp.
98 """
99 result = []
100 for line in Run('git-repo-config', '--get-regexp', '"^%s$"' % regexp
101 ).returns([0, 1]).output_lines():
102 m = re.match('^%s ' % regexp, line)
103 if m:
104 result.append(m.group(1))
105 return result
106
107 config=GitConfig()
108
109 def config_setup():
110 global config
111
112 # Set the PAGER environment to the config value (if any)
113 pager = config.get('stgit.pager')
114 if pager:
115 os.environ['PAGER'] = pager
116 # FIXME: handle EDITOR the same way ?
117
118 class ConfigOption:
119 """Delayed cached reading of a configuration option.
120 """
121 def __init__(self, section, option):
122 self.__section = section
123 self.__option = option
124 self.__value = None
125
126 def __str__(self):
127 if not self.__value:
128 self.__value = config.get(self.__section + '.' + self.__option)
129 return self.__value
130
131
132 # cached extensions
133 __extensions = None
134
135 def file_extensions():
136 """Returns a dictionary with the conflict file extensions
137 """
138 global __extensions
139
140 if not __extensions:
141 cfg_ext = config.get('stgit.extensions').split()
142 if len(cfg_ext) != 3:
143 raise CmdException, '"extensions" configuration error'
144
145 __extensions = { 'ancestor': cfg_ext[0],
146 'current': cfg_ext[1],
147 'patched': cfg_ext[2] }
148
149 return __extensions