1 # -*- coding: utf-8 -*-
4 Copyright (C) 2006, Karl Hasselström <kha@treskal.com>
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.
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.
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
21 from optparse
import OptionParser
, make_option
23 from stgit
.commands
.common
import *
24 from stgit
.utils
import *
25 from stgit
.out
import *
26 from stgit
import stack
, git
28 help = 'turn regular GIT commits into StGIT patches'
29 usage
= """%prog [<patchnames>] | -n NUM [<prefix>]] | -t <committish> [-x]
31 Take one or more git commits at the base of the current stack and turn
32 them into StGIT patches. The new patches are created as applied patches
33 at the bottom of the stack. This is the exact opposite of 'stg commit'.
35 By default, the number of patches to uncommit is determined by the
36 number of patch names provided on the command line. First name is used
37 for the first patch to uncommit, i.e. for the newest patch.
39 The -n/--number option specifies the number of patches to uncommit. In
40 this case, at most one patch name may be specified. It is used as
41 prefix to which the patch number is appended. If no patch names are
42 provided on the command line, StGIT automatically generates them based
43 on the first line of the patch description.
45 The -t/--to option specifies that all commits up to and including the
46 given commit should be uncommitted.
48 Only commits with exactly one parent can be uncommitted; in other
49 words, you can't uncommit a merge."""
51 directory
= DirectoryGotoToplevel()
52 options
= [make_option('-n', '--number', type = 'int',
53 help = 'uncommit the specified number of commits'),
54 make_option('-t', '--to',
55 help = 'uncommit to the specified commit'),
56 make_option('-x', '--exclusive',
57 help = 'exclude the commit specified by the --to option',
58 action
= 'store_true')]
60 def func(parser
, options
, args
):
61 """Uncommit a number of patches.
65 parser
.error('cannot give both --to and --number')
67 parser
.error('cannot specify patch name with --to')
68 patch_nr
= patchnames
= None
69 to_commit
= git_id(crt_series
, options
.to
)
71 if options
.number
<= 0:
72 parser
.error('invalid value passed to --number')
74 patch_nr
= options
.number
80 patchnames
= ['%s%d' %
(args
[0], i
)
81 for i
in xrange(patch_nr
, 0, -1)]
83 parser
.error('when using --number, specify at most one patch name')
89 patch_nr
= len(patchnames
)
91 if crt_series
.get_protected():
93 'This branch is protected. Uncommit is not permitted'
95 def get_commit(commit_id
):
96 commit
= git
.Commit(commit_id
)
98 parent
, = commit
.get_parents()
100 raise CmdException('Commit %s does not have exactly one parent'
102 return (commit
, commit_id
, parent
)
105 next_commit
= crt_series
.get_base()
107 out
.start('Uncommitting %d patches' % patch_nr
)
108 for i
in xrange(patch_nr
):
109 commit
, commit_id
, parent
= get_commit(next_commit
)
110 commits
.append((commit
, commit_id
, parent
))
113 if options
.exclusive
:
114 out
.start('Uncommitting to %s (exclusive)' % to_commit
)
116 out
.start('Uncommitting to %s' % to_commit
)
118 commit
, commit_id
, parent
= get_commit(next_commit
)
119 if commit_id
== to_commit
:
120 if not options
.exclusive
:
121 commits
.append((commit
, commit_id
, parent
))
123 commits
.append((commit
, commit_id
, parent
))
125 patch_nr
= len(commits
)
127 for (commit
, commit_id
, parent
), patchname
in \
128 zip(commits
, patchnames
or [None for i
in xrange(len(commits
))]):
129 author_name
, author_email
, author_date
= \
130 name_email_date(commit
.get_author())
131 crt_series
.new_patch(patchname
,
132 can_edit
= False, before_existing
= True,
134 top
= commit_id
, bottom
= parent
,
135 message
= commit
.get_log(),
136 author_name
= author_name
,
137 author_email
= author_email
,
138 author_date
= author_date
)