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 stgit
.argparse
import opt
20 from stgit
.commands
.common
import *
21 from stgit
.utils
import *
22 from stgit
.out
import *
23 from stgit
import argparse
, stack
, git
24 from stgit
.stack
import Series
26 help = 'Import a patch from a different branch or a commit object'
28 usage
= ['[options] ([<patch1>] [<patch2>] [<patch3>..<patch4>])|<commit>']
30 Import one or more patches from a different branch or a commit object
31 into the current series. By default, the name of the imported patch is
32 used as the name of the current patch. It can be overridden with the
33 '--name' option. A commit object can be reverted with the '--revert'
34 option. The log and author information are those of the commit
37 args
= [argparse
.patch_range(argparse
.applied_patches
,
38 argparse
.unapplied_patches
,
39 argparse
.hidden_patches
)]
42 short
= 'Use NAME as the patch name'),
43 opt('-B', '--ref-branch', args
= [argparse
.stg_branches
],
44 short
= 'Pick patches from BRANCH'),
45 opt('-r', '--revert', action
= 'store_true',
46 short
= 'Revert the given commit object'),
47 opt('-p', '--parent', metavar
= 'COMMITID', args
= [argparse
.commit
],
48 short
= 'Use COMMITID as parent'),
49 opt('-x', '--expose', action
= 'store_true',
50 short
= 'Append the imported commit id to the patch log'),
51 opt('--fold', action
= 'store_true',
52 short
= 'Fold the commit object into the current patch'),
53 opt('--update', action
= 'store_true',
54 short
= 'Like fold but only update the current patch files'),
55 opt('-f', '--file', action
= 'append',
56 short
= 'Only fold the given file (can be used multiple times)'),
57 opt('--unapplied', action
= 'store_true',
58 short
= 'Keep the patch unapplied')]
60 directory
= DirectoryGotoToplevel(log
= True)
62 def __pick_commit(commit_id
, patchname
, options
):
65 commit
= git
.Commit(commit_id
)
68 patchname
= options
.name
69 elif patchname
and options
.revert
:
70 patchname
= 'revert-' + patchname
72 patchname
= find_patch_name(patchname
, crt_series
.patch_exists
)
75 parent
= git_id(crt_series
, options
.parent
)
77 parent
= commit
.get_parent()
79 if not options
.revert
:
87 out
.start('Folding commit %s' % commit_id
)
89 # try a direct git apply first
90 if not git
.apply_diff(bottom
, top
, files
= options
.file):
92 raise CmdException('Patch folding failed')
94 git
.merge_recursive(bottom
, git
.get_head(), top
)
98 rev1
= git_id(crt_series
, 'HEAD^')
99 rev2
= git_id(crt_series
, 'HEAD')
100 files
= git
.barefiles(rev1
, rev2
).split('\n')
102 out
.start('Updating with commit %s' % commit_id
)
104 if not git
.apply_diff(bottom
, top
, files
= files
):
105 raise CmdException
, 'Patch updating failed'
109 message
= commit
.get_log()
112 subject
= message
.splitlines()[0]
114 subject
= commit
.get_id_hash()
115 message
= 'Revert "%s"\n\nThis reverts commit %s.\n' \
116 %
(subject
, commit
.get_id_hash())
118 message
+= '(imported from commit %s)\n' % commit
.get_id_hash()
119 author_name
, author_email
, author_date
= \
120 name_email_date(commit
.get_author())
122 out
.start('Importing commit %s' % commit_id
)
124 newpatch
= crt_series
.new_patch(patchname
, message
= message
, can_edit
= False,
125 unapplied
= True, bottom
= bottom
, top
= top
,
126 author_name
= author_name
,
127 author_email
= author_email
,
128 author_date
= author_date
)
129 # in case the patch name was automatically generated
130 patchname
= newpatch
.get_name()
132 # find a patchlog to fork from
133 refbranchname
, refpatchname
= parse_rev(patchname
)
136 # assume the refseries is OK, since we already resolved
137 # commit_str to a git_id
138 refseries
= Series(refbranchname
)
140 refseries
= crt_series
141 patch
= refseries
.get_patch(refpatchname
)
143 out
.info("Log was %s" % newpatch
.get_log())
144 out
.info("Setting log to %s\n" % patch
.get_log())
145 newpatch
.set_log(patch
.get_log())
146 out
.info("Log is now %s" % newpatch
.get_log())
148 out
.info("No log for %s\n" % patchname
)
150 if not options
.unapplied
:
151 modified
= crt_series
.push_patch(patchname
)
155 if crt_series
.empty_patch(patchname
):
156 out
.done('empty patch')
163 def func(parser
, options
, args
):
164 """Import a commit object as a new patch
167 parser
.error('incorrect number of arguments')
169 if options
.file and not options
.fold
:
170 parser
.error('--file can only be specified with --fold')
172 if not options
.unapplied
:
173 check_local_changes()
175 check_head_top_equal(crt_series
)
177 if options
.ref_branch
:
178 remote_series
= Series(options
.ref_branch
)
180 remote_series
= crt_series
182 applied
= remote_series
.get_applied()
183 unapplied
= remote_series
.get_unapplied()
185 patches
= parse_patches(args
, applied
+ unapplied
, len(applied
))
190 # no patches found, try a commit id
191 commit_id
= git_id(remote_series
, args
[0])
193 if not commit_id
and len(patches
) > 1:
195 raise CmdException
, '--name can only be specified with one patch'
197 raise CmdException
, '--parent can only be specified with one patch'
199 if options
.update
and not crt_series
.get_current():
200 raise CmdException
, 'No patches applied'
203 # Try to guess a patch name if the argument was <branch>:<patch>
205 patchname
= args
[0].split(':')[1]
208 __pick_commit(commit_id
, patchname
, options
)
210 if options
.unapplied
:
212 for patch
in patches
:
213 __pick_commit(git_id(remote_series
, patch
), patch
, options
)
215 print_crt_patch(crt_series
)