Refactor message printing
[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 [<patchnames>] | -n NUM [<prefix>]] | -t <committish>
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 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 -n/--number option specifies the number of patches to uncommit. In
39 this case, at most one patch name may be specified. It is used as
40 prefix to which the patch number is appended. If no patch names are
41 provided on the command line, StGIT automatically generates them based
42 on the first line of the patch description.
43
44 The -t/--to option specifies that all commits up to and including the
45 given commit should be uncommitted.
46
47 Only commits with exactly one parent can be uncommitted; in other
48 words, you can't uncommit a merge."""
49
50 options = [make_option('-n', '--number', type = 'int',
51 help = 'uncommit the specified number of commits'),
52 make_option('-t', '--to',
53 help = 'uncommit to the specified commit')]
54
55 def func(parser, options, args):
56 """Uncommit a number of patches.
57 """
58 if options.to:
59 if options.number:
60 parser.error('cannot give both --to and --number')
61 if len(args) != 0:
62 parser.error('cannot specify patch name with --to')
63 patch_nr = patchnames = None
64 to_commit = git.rev_parse(options.to)
65 elif options.number:
66 if options.number <= 0:
67 parser.error('invalid value passed to --number')
68
69 patch_nr = options.number
70
71 if len(args) == 0:
72 patchnames = None
73 elif len(args) == 1:
74 # prefix specified
75 patchnames = ['%s%d' % (args[0], i)
76 for i in xrange(patch_nr, 0, -1)]
77 else:
78 parser.error('when using --number, specify at most one patch name')
79 elif len(args) == 0:
80 patchnames = None
81 patch_nr = 1
82 else:
83 patchnames = args
84 patch_nr = len(patchnames)
85
86 if crt_series.get_protected():
87 raise CmdException, \
88 'This branch is protected. Uncommit is not permitted'
89
90 def get_commit(commit_id):
91 commit = git.Commit(commit_id)
92 try:
93 parent, = commit.get_parents()
94 except ValueError:
95 raise CmdException('Commit %s does not have exactly one parent'
96 % commit_id)
97 return (commit, commit_id, parent)
98
99 commits = []
100 next_commit = crt_series.get_base()
101 if patch_nr:
102 out.start('Uncommitting %d patches' % patch_nr)
103 for i in xrange(patch_nr):
104 commit, commit_id, parent = get_commit(next_commit)
105 commits.append((commit, commit_id, parent))
106 next_commit = parent
107 else:
108 out.start('Uncommitting to %s' % to_commit)
109 while True:
110 commit, commit_id, parent = get_commit(next_commit)
111 commits.append((commit, commit_id, parent))
112 if commit_id == to_commit:
113 break
114 next_commit = parent
115 patch_nr = len(commits)
116
117 for (commit, commit_id, parent), patchname in \
118 zip(commits, patchnames or [None for i in xrange(len(commits))]):
119 author_name, author_email, author_date = \
120 name_email_date(commit.get_author())
121 crt_series.new_patch(patchname,
122 can_edit = False, before_existing = True,
123 top = commit_id, bottom = parent,
124 message = commit.get_log(),
125 author_name = author_name,
126 author_email = author_email,
127 author_date = author_date)
128
129 out.done()