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