Explicitly specify utf-8 coding in file
[stgit] / stgit / commands / uncommit.py
1 # -*- coding: utf-8 -*-
2
3 __copyright__ = """
4 Copyright (C) 2006, Karl Hasselström <kha@treskal.com>
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License version 2 as
8 published by the Free Software Foundation.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 """
19
20 import sys, os
21 from optparse import OptionParser, make_option
22
23 from stgit.commands.common import *
24 from stgit.utils import *
25 from stgit import stack, git
26
27 help = 'turn regular git commits into StGIT patches'
28 usage = """%prog [options] <patchname1> [<patchname2> ... ]
29
30 Take one or more git commits at the base of the current stack and turn
31 them into StGIT patches. The new patches are created as applied patches
32 at the bottom of the stack. This is the exact opposite of 'stg commit'.
33
34 By default, the number of the patches to uncommit is determined by the
35 number of patch names provided on the command line. First name is used
36 for the first patch to uncommit, i.e. for the newest patch.
37
38 The --number option specifies the number of patches to uncommit. In
39 this case, only one patch name may be specified. It is used as prefix to
40 which the patch number is appended.
41
42 Only commits with exactly one parent can be uncommitted; in other
43 words, you can't uncommit a merge."""
44
45 options = [make_option('-n', '--number', type = 'int',
46 help = 'uncommit the specified number of commits')]
47
48 def func(parser, options, args):
49 if len(args) == 0:
50 parser.error('you must specify at least one patch name')
51 if options.number:
52 if len(args) != 1:
53 parser.error('when using --number, specify exactly one patch name')
54 patchnames = ['%s%d' % (args[0], i)
55 for i in xrange(options.number - 1, -1, -1)]
56 else:
57 patchnames = args
58
59 if crt_series.get_protected():
60 raise CmdException, 'This branch is protected. Uncommit is not permitted'
61
62 print 'Uncommitting %d patches...' % len(patchnames),
63 sys.stdout.flush()
64
65 for patchname in patchnames:
66 base_file = crt_series.get_base_file()
67 commit_id = read_string(base_file)
68 commit = git.Commit(commit_id)
69 try:
70 parent, = commit.get_parents()
71 except ValueError:
72 raise CmdException, ('Commit %s does not have exactly one parent'
73 % commit_id)
74 author_name, author_email, author_date = name_email_date(
75 commit.get_author())
76 crt_series.new_patch(patchname,
77 can_edit = False, before_existing = True,
78 top = commit_id, bottom = parent,
79 message = commit.get_log(),
80 author_name = author_name,
81 author_email = author_email,
82 author_date = author_date)
83 write_string(base_file, parent)
84
85 print 'done'