3 """Migrate a post-receive-email configuration to be usable with git_multimail.py.
5 See README.migrate-from-post-receive-email for more information.
12 from git_multimail import CommandError
13 from git_multimail import Config
14 from git_multimail import read_output
50 Your post-receive-email configuration has been converted to
51 git-multimail format. Please see README and
52 README.migrate-from-post-receive-email to learn about other
53 git-multimail configuration possibilities.
55 For example, git-multimail has the following new options with no
56 equivalent in post-receive-email. You might want to read about them
57 to see if they would be useful in your situation:
62 def _check_old_config_exists(old):
63 """Check that at least one old configuration value is set."""
65 for name in OLD_NAMES:
72 def _check_new_config_clear(new):
73 """Check that none of the new configuration names are set."""
76 for name in NEW_NAMES:
79 sys.stderr.write('INFO: The following configuration values already exist:\n\n')
80 sys.stderr.write(' "%s.%s"\n' % (new.section, name))
86 def erase_values(config, names):
90 sys.stderr.write('...unsetting "%s.%s"\n' % (config.section, name))
91 config.unset_all(name)
94 '\nWARNING: could not unset "%s.%s". '
95 'Perhaps it is not set at the --local level?\n\n'
96 % (config.section, name)
100 def is_section_empty(section, local):
101 """Return True iff the specified configuration section is empty.
103 Iff local is True, use the --local option when invoking 'git
107 local_option = ['--local']
115 ['--get-regexp', '^%s\.' % (section,)]
118 t, e, traceback = sys.exc_info()
120 # This means that no settings were found.
128 def remove_section_if_empty(section):
129 """If the specified configuration section is empty, delete it."""
132 empty = is_section_empty(section, local=True)
134 # Older versions of git do not support the --local option, so
135 # if the first attempt fails, try without --local.
137 empty = is_section_empty(section, local=False)
140 '\nINFO: If configuration section "%s.*" is empty, you might want '
147 sys.stderr.write('...removing section "%s.*"\n' % (section,))
148 read_output(['git', 'config', '--remove-section', section])
151 '\nINFO: Configuration section "%s.*" still has contents. '
152 'It will not be deleted.\n\n'
157 def migrate_config(strict=False, retain=False, overwrite=False):
158 old = Config('hooks')
159 new = Config('multimailhook')
160 if not _check_old_config_exists(old):
162 'Your repository has no post-receive-email configuration. '
165 if not _check_new_config_clear(new):
167 sys.stderr.write('\nWARNING: Erasing the above values...\n\n')
168 erase_values(new, NEW_NAMES)
171 '\nERROR: Refusing to overwrite existing values. Use the --overwrite\n'
172 'option to continue anyway.'
177 msg = 'git-multimail does not support "%s.%s"' % (old.section, name,)
181 'Please unset that value then try again, or run without --strict.'
185 sys.stderr.write('\nWARNING: %s (ignoring).\n\n' % (msg,))
187 for name in ['mailinglist', 'announcelist']:
190 '...copying "%s.%s" to "%s.%s"\n' % (old.section, name, new.section, name)
192 old_recipients = old.get_all(name, default=None)
193 old_recipients = ', '.join(o.strip() for o in old_recipients)
194 new.set_recipients(name, old_recipients)
198 '...setting "%s.commitlist" to the empty string\n' % (new.section,)
200 new.set_recipients('commitlist', '')
202 '...setting "%s.announceshortlog" to "true"\n' % (new.section,)
204 new.set('announceshortlog', 'true')
206 for name in ['envelopesender', 'emailmaxlines', 'diffopts', 'scancommitforcc']:
209 '...copying "%s.%s" to "%s.%s"\n' % (old.section, name, new.section, name)
211 new.set(name, old.get(name))
216 '...copying "%s.%s" to "%s.%s"\n' % (old.section, name, new.section, name)
218 new.set(name, old.get(name))
221 '...setting "%s.%s" to "[SCM]" to preserve old subject lines\n'
222 % (new.section, name)
224 new.set(name, '[SCM]')
227 erase_values(old, OLD_NAMES)
228 remove_section_if_empty(old.section)
230 sys.stderr.write(INFO)
231 for name in NEW_NAMES:
232 if name not in OLD_NAMES:
233 sys.stderr.write(' "%s.%s"\n' % (new.section, name,))
234 sys.stderr.write('\n')
238 parser = optparse.OptionParser(
240 usage='%prog [OPTIONS]',
244 '--strict', action='store_true', default=False,
246 'Slavishly configure git-multimail as closely as possible to '
247 'the post-receive-email configuration. Default is to turn '
248 'on some new features that have no equivalent in post-receive-email.'
252 '--retain', action='store_true', default=False,
254 'Retain the post-receive-email configuration values. '
255 'Default is to delete them after the new values are set.'
259 '--overwrite', action='store_true', default=False,
261 'Overwrite any existing git-multimail configuration settings. '
262 'Default is to abort if such settings already exist.'
266 (options, args) = parser.parse_args(args)
269 parser.error('Unexpected arguments: %s' % (' '.join(args),))
271 migrate_config(strict=options.strict, retain=options.retain, overwrite=options.overwrite)