chiark / gitweb /
Make the 'series --short' length configurable
[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
21import os, ConfigParser
abcc2620 22from StringIO import StringIO
170f576b 23from stgit import basedir
41a6d859 24
41a6d859
CM
25config = ConfigParser.RawConfigParser()
26
abcc2620
CM
27def git_config(filename):
28 """Open a git config file and convert it to be understood by
29 Python."""
30 try:
31 f = file(filename)
32 cont = False
33 lines = []
34 for line in f:
35 line = line.strip()
36
37 if cont:
38 # continued line, add a space at the beginning
39 line = ' ' + line
40
41 if line and line[-1] == '\\':
42 line = line[:-1].rstrip()
43 cont = True
44 else:
45 line = line + '\n'
46 cont = False
47
48 lines.append(line)
49
50 f.close()
51 cfg_str = ''.join(lines)
52 except IOError:
53 cfg_str = ''
54
55 strio = StringIO(cfg_str)
56 strio.name = filename
57
58 return strio
59
eee7283e
CM
60def config_setup():
61 global config
62
63 # Set the defaults
64 config.add_section('stgit')
65 config.set('stgit', 'autoresolved', 'no')
66 config.set('stgit', 'smtpserver', 'localhost:25')
67 config.set('stgit', 'smtpdelay', '5')
68 config.set('stgit', 'pullcmd', 'git-pull')
69 config.set('stgit', 'merger',
70 'diff3 -L current -L ancestor -L patched -m -E ' \
71 '"%(branch1)s" "%(ancestor)s" "%(branch2)s" > "%(output)s"')
f7ed76a9 72 config.set('stgit', 'autoimerge', 'no')
eee7283e
CM
73 config.set('stgit', 'keeporig', 'yes')
74 config.set('stgit', 'keepoptimized', 'no')
75 config.set('stgit', 'extensions', '.ancestor .current .patched')
c6f366f6 76 config.set('stgit', 'shortnr', '5')
eee7283e
CM
77
78 # Read the configuration files (if any) and override the default settings
abcc2620 79 # stgitrc are read for backward compatibility
eee7283e
CM
80 config.read('/etc/stgitrc')
81 config.read(os.path.expanduser('~/.stgitrc'))
82 config.read(os.path.join(basedir.get(), 'stgitrc'))
83
abcc2620 84 # GIT configuration files can have a [stgit] section
9e3f506f
KH
85 try:
86 global_config = os.environ['GIT_CONFIG']
87 except KeyError:
88 global_config = os.path.expanduser('~/.gitconfig')
89 try:
90 local_config = os.environ['GIT_CONFIG_LOCAL']
91 except KeyError:
92 local_config = os.path.join(basedir.get(), 'config')
93 config.readfp(git_config(global_config))
94 config.readfp(git_config(local_config))
abcc2620 95
eee7283e
CM
96 # Set the PAGER environment to the config value (if any)
97 if config.has_option('stgit', 'pager'):
98 os.environ['PAGER'] = config.get('stgit', 'pager')
99
100 # [gitmergeonefile] section is deprecated. In case it exists copy the
101 # options/values to the [stgit] one
102 if config.has_section('gitmergeonefile'):
103 for option, value in config.items('gitmergeonefile'):
104 config.set('stgit', option, value)
105
106
107class ConfigOption:
108 """Delayed cached reading of a configuration option.
109 """
110 def __init__(self, section, option):
111 self.__section = section
112 self.__option = option
113 self.__value = None
114
115 def __str__(self):
116 if not self.__value:
117 self.__value = config.get(self.__section, self.__option)
118 return self.__value
d7fade4b
CM
119
120
121# cached extensions
122__extensions = None
123
124def file_extensions():
125 """Returns a dictionary with the conflict file extensions
126 """
127 global __extensions
128
129 if not __extensions:
130 cfg_ext = config.get('stgit', 'extensions').split()
131 if len(cfg_ext) != 3:
132 raise CmdException, '"extensions" configuration error'
133
134 __extensions = { 'ancestor': cfg_ext[0],
135 'current': cfg_ext[1],
136 'patched': cfg_ext[2] }
137
138 return __extensions