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