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
24 from stgit.stack import Series
27 help = 'import a patch from a different branch or a commit object'
28 usage = """%prog [options] [<patch@branch>|<commit>]
30 Import a patch from a different branch or a commit object into the
31 current series. By default, the name of the imported patch is used as
32 the name of the current patch. It can be overridden with the '--name'
33 option. A commit object can be reverted with the '--reverse'
34 option. The log and author information are those of the commit object."""
36 options = [make_option('-n', '--name',
37 help = 'use NAME as the patch name'),
38 make_option('-r', '--reverse',
39 help = 'reverse the commit object before importing',
40 action = 'store_true'),
41 make_option('-p', '--parent', metavar = 'COMMITID',
42 help = 'use COMMITID as parent'),
44 help = 'fold the commit object into the current patch',
45 action = 'store_true'),
46 make_option('--update',
47 help = 'like fold but only update the current patch files',
48 action = 'store_true'),
49 make_option('--unapplied',
50 help = 'keep the patch unapplied',
51 action = 'store_true')]
54 def func(parser, options, args):
55 """Import a commit object as a new patch
58 parser.error('incorrect number of arguments')
60 if not options.unapplied:
63 check_head_top_equal()
66 commit_id = git_id(commit_str)
67 commit = git.Commit(commit_id)
69 if options.fold or options.update:
70 if not crt_series.get_current():
71 raise CmdException, 'No patches applied'
73 patch_branch = commit_str.split('@')
75 patchname = options.name
76 elif len(patch_branch) == 2:
77 patchname = patch_branch[0]
82 parent = git_id(options.parent)
84 parent = commit.get_parent()
86 if not options.reverse:
94 print 'Folding commit %s...' % commit_id,
97 # try a direct git-apply first
98 if not git.apply_diff(bottom, top):
99 git.merge(bottom, git.get_head(), top, recursive = True)
103 rev1 = git_id('//bottom')
104 rev2 = git_id('//top')
105 files = git.barefiles(rev1, rev2).split('\n')
107 print 'Updating with commit %s...' % commit_id,
110 if not git.apply_diff(bottom, top, files = files):
111 raise CmdException, 'Patch updating failed'
115 message = commit.get_log()
116 author_name, author_email, author_date = \
117 name_email_date(commit.get_author())
119 print 'Importing commit %s...' % commit_id,
122 newpatch = crt_series.new_patch(patchname, message = message, can_edit = False,
123 unapplied = True, bottom = bottom, top = top,
124 author_name = author_name,
125 author_email = author_email,
126 author_date = author_date)
127 # find a patchlog to fork from
128 (refpatchname, refbranchname, refpatchid) = parse_rev(commit_str)
129 if refpatchname and not refpatchid and \
130 (not refpatchid or refpatchid == 'top'):
131 # FIXME: should also support picking //top.old
133 # assume the refseries is OK, since we already resolved
134 # commit_str to a git_id
135 refseries = Series(refbranchname)
137 refseries = crt_series
138 patch = refseries.get_patch(refpatchname)
140 print"log was %s" % newpatch.get_log()
141 print "setting log to %s\n" % patch.get_log()
142 newpatch.set_log(patch.get_log())
143 print"log is now %s" % newpatch.get_log()
145 print "no log for %s\n" % patchname
147 if not options.unapplied:
148 modified = crt_series.push_patch(patchname)
152 if crt_series.empty_patch(patchname):
153 print 'done (empty patch)'
155 print 'done (modified)'