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 import stack, git
26 help = 'import a patch from a different branch or a commit object'
27 usage = """%prog [options] [<patch@branch>|<commit>]
29 Import a patch from a different branch or a commit object into the
30 current series. By default, the name of the imported patch is used as
31 the name of the current patch. It can be overridden with the '--name'
32 option. A commit object can be reverted with the '--reverse'
33 option. The log and author information are those of the commit object."""
35 options = [make_option('-n', '--name',
36 help = 'use NAME as the patch name'),
37 make_option('-r', '--reverse',
38 help = 'reverse the commit object before importing',
39 action = 'store_true'),
40 make_option('-p', '--parent', metavar = 'COMMITID',
41 help = 'use COMMITID as parent'),
43 help = 'fold the commit object into the current patch',
44 action = 'store_true'),
45 make_option('--update',
46 help = 'like fold but only update the current patch files',
47 action = 'store_true')]
50 def func(parser, options, args):
51 """Import a commit object as a new patch
54 parser.error('incorrect number of arguments')
58 check_head_top_equal()
61 commit_id = git_id(commit_str)
62 commit = git.Commit(commit_id)
64 if options.fold or options.update:
65 if not crt_series.get_current():
66 raise CmdException, 'No patches applied'
68 patch_branch = commit_str.split('@')
71 elif len(patch_branch) == 2:
72 patch = patch_branch[0]
74 patch = make_patch_name(commit.get_log(), crt_series.patch_exists)
77 parent = git_id(options.parent)
79 parent = commit.get_parent()
81 if not options.reverse:
89 print 'Folding commit %s...' % commit_id,
92 # try a direct git-apply first
93 if not git.apply_diff(bottom, top):
94 git.merge(bottom, git.get_head(), top, recursive = True)
98 rev1 = git_id('//bottom')
99 rev2 = git_id('//top')
100 files = git.barefiles(rev1, rev2).split('\n')
102 print 'Updating with commit %s...' % commit_id,
105 if not git.apply_diff(bottom, top, files = files):
106 raise CmdException, 'Patch updating failed'
110 message = commit.get_log()
111 author_name, author_email, author_date = \
112 name_email_date(commit.get_author())
114 print 'Importing commit %s...' % commit_id,
117 crt_series.new_patch(patch, message = message, can_edit = False,
118 unapplied = True, bottom = bottom, top = top,
119 author_name = author_name,
120 author_email = author_email,
121 author_date = author_date)
122 modified = crt_series.push_patch(patch)
124 if crt_series.empty_patch(patch):
125 print 'done (empty patch)'
127 print 'done (modified)'