Add mergetool support to the classic StGit infrastructure
[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={
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 37 'stgit.autoimerge': 'no',
c73e63b7
YD
38 'stgit.keepoptimized': 'no',
39 'stgit.extensions': '.ancestor .current .patched',
40 'stgit.shortnr': '5'
41 }
42
9a4bf454
YD
43 __cache={}
44
c73e63b7 45 def get(self, name):
9a4bf454
YD
46 if self.__cache.has_key(name):
47 return self.__cache[name]
f0de3f92 48 try:
cd885e08 49 value = Run('git', 'config', '--get', name).output_one_line()
f0de3f92
KH
50 except RunException:
51 value = self.__defaults.get(name, None)
9a4bf454
YD
52 self.__cache[name] = value
53 return value
c73e63b7
YD
54
55 def getall(self, name):
9a4bf454
YD
56 if self.__cache.has_key(name):
57 return self.__cache[name]
cd885e08 58 values = Run('git', 'config', '--get-all', name
f0de3f92 59 ).returns([0, 1]).output_lines()
9a4bf454 60 self.__cache[name] = values
c73e63b7
YD
61 return values
62
63 def getint(self, name):
64 value = self.get(name)
e928a3ec
KH
65 if value == None:
66 return None
67 elif value.isdigit():
c73e63b7
YD
68 return int(value)
69 else:
70 raise GitConfigException, 'Value for "%s" is not an integer: "%s"' % (name, value)
71
cb5be4c3 72 def rename_section(self, from_name, to_name):
f0de3f92
KH
73 """Rename a section in the config file. Silently do nothing if
74 the section doesn't exist."""
cd885e08 75 Run('git', 'config', '--rename-section', from_name, to_name
f0de3f92 76 ).returns([0, 1]).run()
8591add9 77 self.__cache.clear()
cb5be4c3 78
9a6bcbe2
KH
79 def remove_section(self, name):
80 """Remove a section in the config file. Silently do nothing if
81 the section doesn't exist."""
cd885e08 82 Run('git', 'config', '--remove-section', name
9a6bcbe2
KH
83 ).returns([0, 1]).discard_stderr().discard_output()
84 self.__cache.clear()
85
c73e63b7 86 def set(self, name, value):
cd885e08 87 Run('git', 'config', name, value).run()
8591add9 88 self.__cache[name] = value
c73e63b7 89
0aee23c2 90 def unset(self, name):
cd885e08 91 Run('git', 'config', '--unset', name)
8591add9 92 self.__cache[name] = None
0aee23c2 93
c73e63b7
YD
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 = []
cd885e08 100 for line in Run('git', 'config', '--get-regexp', '"^%s$"' % regexp
f0de3f92 101 ).returns([0, 1]).output_lines():
c73e63b7
YD
102 m = re.match('^%s ' % regexp, line)
103 if m:
104 result.append(m.group(1))
c73e63b7
YD
105 return result
106
107config=GitConfig()
abcc2620 108
eee7283e
CM
109def config_setup():
110 global config
111
eee7283e 112 # Set the PAGER environment to the config value (if any)
c73e63b7
YD
113 pager = config.get('stgit.pager')
114 if pager:
115 os.environ['PAGER'] = pager
116 # FIXME: handle EDITOR the same way ?
eee7283e
CM
117
118class 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:
c73e63b7 128 self.__value = config.get(self.__section + '.' + self.__option)
eee7283e 129 return self.__value
d7fade4b
CM
130
131
132# cached extensions
133__extensions = None
134
135def file_extensions():
136 """Returns a dictionary with the conflict file extensions
137 """
138 global __extensions
139
140 if not __extensions:
c73e63b7 141 cfg_ext = config.get('stgit.extensions').split()
d7fade4b
CM
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