1 """Handles the Stacked GIT configuration files
5 Copyright (C) 2005, Catalin Marinas <catalin.marinas@gmail.com>
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.
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.
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
22 from stgit
import basedir
23 from stgit
.exception
import *
24 from stgit
.run
import *
26 class GitConfigException(StgException
):
31 'stgit.autoresolved': 'no',
32 'stgit.smtpserver': 'localhost:25',
33 'stgit.smtpdelay': '5',
34 'stgit.pullcmd': 'git pull',
35 'stgit.fetchcmd': 'git fetch',
36 'stgit.pull-policy': 'pull',
37 'stgit.merger': 'diff3 -L current -L ancestor -L patched -m -E ' \
38 '"%(branch1)s" "%(ancestor)s" "%(branch2)s" > "%(output)s"',
39 'stgit.autoimerge': 'no',
40 'stgit.keeporig': 'yes',
41 'stgit.keepoptimized': 'no',
42 'stgit.extensions': '.ancestor .current .patched',
49 if self
.__cache
.has_key(name
):
50 return self
.__cache
[name
]
52 value
= Run('git', 'config', '--get', name
).output_one_line()
54 value
= self
.__defaults
.get(name
, None)
55 self
.__cache
[name
] = value
58 def getall(self
, name
):
59 if self
.__cache
.has_key(name
):
60 return self
.__cache
[name
]
61 values
= Run('git', 'config', '--get-all', name
62 ).returns([0, 1]).output_lines()
63 self
.__cache
[name
] = values
66 def getint(self
, name
):
67 value
= self
.get(name
)
73 raise GitConfigException
, 'Value for "%s" is not an integer: "%s"' %
(name
, value
)
75 def rename_section(self
, from_name
, to_name
):
76 """Rename a section in the config file. Silently do nothing if
77 the section doesn't exist."""
78 Run('git', 'config', '--rename-section', from_name
, to_name
79 ).returns([0, 1]).run()
82 def remove_section(self
, name
):
83 """Remove a section in the config file. Silently do nothing if
84 the section doesn't exist."""
85 Run('git', 'config', '--remove-section', name
86 ).returns([0, 1]).discard_stderr().discard_output()
89 def set(self
, name
, value
):
90 Run('git', 'config', name
, value
).run()
91 self
.__cache
[name
] = value
93 def unset(self
, name
):
94 Run('git', 'config', '--unset', name
)
95 self
.__cache
[name
] = None
97 def sections_matching(self
, regexp
):
98 """Takes a regexp with a single group, matches it against all
99 config variables, and returns a list whose members are the
100 group contents, for all variable names matching the regexp.
103 for line
in Run('git', 'config', '--get-regexp', '"^%s$"' % regexp
104 ).returns([0, 1]).output_lines():
105 m
= re
.match('^%s ' % regexp
, line
)
107 result
.append(m
.group(1))
115 # Set the PAGER environment to the config value (if any)
116 pager
= config
.get('stgit.pager')
118 os
.environ
['PAGER'] = pager
119 # FIXME: handle EDITOR the same way ?
122 """Delayed cached reading of a configuration option.
124 def __init__(self
, section
, option
):
125 self
.__section
= section
126 self
.__option
= option
131 self
.__value
= config
.get(self
.__section
+ '.' + self
.__option
)
138 def file_extensions():
139 """Returns a dictionary with the conflict file extensions
144 cfg_ext
= config
.get('stgit.extensions').split()
145 if len(cfg_ext
) != 3:
146 raise CmdException
, '"extensions" configuration error'
148 __extensions
= { 'ancestor': cfg_ext
[0],
149 'current': cfg_ext
[1],
150 'patched': cfg_ext
[2] }