2 Copyright (C) 2005, Catalin Marinas <catalin.marinas@gmail.com>
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License version 2 as
6 published by the Free Software Foundation.
8 This program is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 GNU General Public License for more details.
13 You should have received a copy of the GNU General Public License
14 along with this program; if not, write to the Free Software
15 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 from optparse import OptionParser, make_option
21 from stgit.commands.common import *
22 from stgit.utils import *
23 from stgit.out import *
24 from stgit import stack, git
25 from stgit.stack import Series
28 help = 'import a patch from a different branch or a commit object'
29 usage = """%prog [options] ([<patch1>] [<patch2>] [<patch3>..<patch4>])|<commit>
31 Import one or more patches from a different branch or a commit object
32 into the current series. By default, the name of the imported patch is
33 used as the name of the current patch. It can be overridden with the
34 '--name' option. A commit object can be reverted with the '--reverse'
35 option. The log and author information are those of the commit
38 directory = DirectoryGotoToplevel()
39 options = [make_option('-n', '--name',
40 help = 'use NAME as the patch name'),
41 make_option('-B', '--ref-branch',
42 help = 'pick patches from BRANCH'),
43 make_option('-r', '--reverse',
44 help = 'reverse the commit object before importing',
45 action = 'store_true'),
46 make_option('-p', '--parent', metavar = 'COMMITID',
47 help = 'use COMMITID as parent'),
48 make_option('-x', '--expose',
49 help = 'append the imported commit id to the patch log',
50 action = 'store_true'),
52 help = 'fold the commit object into the current patch',
53 action = 'store_true'),
54 make_option('--update',
55 help = 'like fold but only update the current patch files',
56 action = 'store_true'),
57 make_option('--unapplied',
58 help = 'keep the patch unapplied',
59 action = 'store_true')]
61 def __pick_commit(commit_id, patchname, options):
64 commit = git.Commit(commit_id)
67 patchname = options.name
70 parent = git_id(crt_series, options.parent)
72 parent = commit.get_parent()
74 if not options.reverse:
82 out.start('Folding commit %s' % commit_id)
84 # try a direct git apply first
85 if not git.apply_diff(bottom, top):
86 git.merge(bottom, git.get_head(), top, recursive = True)
90 rev1 = git_id(crt_series, '//bottom')
91 rev2 = git_id(crt_series, '//top')
92 files = git.barefiles(rev1, rev2).split('\n')
94 out.start('Updating with commit %s' % commit_id)
96 if not git.apply_diff(bottom, top, files = files):
97 raise CmdException, 'Patch updating failed'
101 message = commit.get_log()
103 message += '(imported from commit %s)\n' % commit.get_id_hash()
104 author_name, author_email, author_date = \
105 name_email_date(commit.get_author())
107 out.start('Importing commit %s' % commit_id)
109 newpatch = crt_series.new_patch(patchname, message = message, can_edit = False,
110 unapplied = True, bottom = bottom, top = top,
111 author_name = author_name,
112 author_email = author_email,
113 author_date = author_date)
114 # in case the patch name was automatically generated
115 patchname = newpatch.get_name()
117 # find a patchlog to fork from
118 (refpatchname, refbranchname, refpatchid) = parse_rev(patchname)
119 if refpatchname and not refpatchid and \
120 (not refpatchid or refpatchid == 'top'):
121 # FIXME: should also support picking //top.old
123 # assume the refseries is OK, since we already resolved
124 # commit_str to a git_id
125 refseries = Series(refbranchname)
127 refseries = crt_series
128 patch = refseries.get_patch(refpatchname)
130 out.info("Log was %s" % newpatch.get_log())
131 out.info("Setting log to %s\n" % patch.get_log())
132 newpatch.set_log(patch.get_log())
133 out.info("Log is now %s" % newpatch.get_log())
135 out.info("No log for %s\n" % patchname)
137 if not options.unapplied:
138 modified = crt_series.push_patch(patchname)
142 if crt_series.empty_patch(patchname):
143 out.done('empty patch')
150 def func(parser, options, args):
151 """Import a commit object as a new patch
154 parser.error('incorrect number of arguments')
156 if not options.unapplied:
157 check_local_changes()
159 check_head_top_equal(crt_series)
161 if options.ref_branch:
162 remote_series = Series(options.ref_branch)
164 remote_series = crt_series
166 applied = remote_series.get_applied()
167 unapplied = remote_series.get_unapplied()
169 patches = parse_patches(args, applied + unapplied, len(applied))
174 # no patches found, try a commit id
175 commit_id = git_id(remote_series, args[0])
177 if not commit_id and len(patches) > 1:
179 raise CmdException, '--name can only be specified with one patch'
181 raise CmdException, '--parent can only be specified with one patch'
183 if (options.fold or options.update) and not crt_series.get_current():
184 raise CmdException, 'No patches applied'
187 __pick_commit(commit_id, None, options)
189 if options.unapplied:
191 for patch in patches:
192 __pick_commit(git_id(remote_series, patch), patch, options)
194 print_crt_patch(crt_series)