Commit | Line | Data |
---|---|---|
0618ea9c CM |
1 | __copyright__ = """ |
2 | Copyright (C) 2005, Catalin Marinas <catalin.marinas@gmail.com> | |
3 | ||
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. | |
7 | ||
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. | |
12 | ||
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 | |
16 | """ | |
17 | ||
18 | import sys, os | |
19 | from optparse import OptionParser, make_option | |
20 | ||
21 | from stgit.commands.common import * | |
22 | from stgit.utils import * | |
5e888f30 | 23 | from stgit.out import * |
0618ea9c | 24 | from stgit import stack, git |
8fce9909 | 25 | from stgit.stack import Series |
0618ea9c CM |
26 | |
27 | ||
28 | help = 'import a patch from a different branch or a commit object' | |
29 | usage = """%prog [options] [<patch@branch>|<commit>] | |
30 | ||
31 | Import a patch from a different branch or a commit object into the | |
32 | current series. By default, the name of the imported patch is used as | |
388f63b6 | 33 | the name of the current patch. It can be overridden with the '--name' |
0618ea9c CM |
34 | option. A commit object can be reverted with the '--reverse' |
35 | option. The log and author information are those of the commit object.""" | |
36 | ||
37 | options = [make_option('-n', '--name', | |
38 | help = 'use NAME as the patch name'), | |
39 | make_option('-r', '--reverse', | |
40 | help = 'reverse the commit object before importing', | |
ba524d4c | 41 | action = 'store_true'), |
89e3bc51 YD |
42 | make_option('-p', '--parent', metavar = 'COMMITID', |
43 | help = 'use COMMITID as parent'), | |
2ff25eb8 CM |
44 | make_option('-x', '--expose', |
45 | help = 'append the imported commit id to the patch log', | |
46 | action = 'store_true'), | |
ba524d4c CM |
47 | make_option('--fold', |
48 | help = 'fold the commit object into the current patch', | |
d0bfda1a CM |
49 | action = 'store_true'), |
50 | make_option('--update', | |
51 | help = 'like fold but only update the current patch files', | |
7829d19d CM |
52 | action = 'store_true'), |
53 | make_option('--unapplied', | |
54 | help = 'keep the patch unapplied', | |
0618ea9c CM |
55 | action = 'store_true')] |
56 | ||
57 | ||
58 | def func(parser, options, args): | |
59 | """Import a commit object as a new patch | |
60 | """ | |
61 | if len(args) != 1: | |
62 | parser.error('incorrect number of arguments') | |
63 | ||
7829d19d CM |
64 | if not options.unapplied: |
65 | check_local_changes() | |
66 | check_conflicts() | |
67 | check_head_top_equal() | |
0618ea9c CM |
68 | |
69 | commit_str = args[0] | |
fff9bce5 CM |
70 | commit_id = git_id(commit_str) |
71 | commit = git.Commit(commit_id) | |
0618ea9c | 72 | |
d0bfda1a | 73 | if options.fold or options.update: |
ba524d4c CM |
74 | if not crt_series.get_current(): |
75 | raise CmdException, 'No patches applied' | |
0618ea9c | 76 | else: |
ba524d4c | 77 | patch_branch = commit_str.split('@') |
9a6e2cc3 | 78 | if options.name: |
2e88afce | 79 | patchname = options.name |
9a6e2cc3 | 80 | elif len(patch_branch) == 2: |
2e88afce | 81 | patchname = patch_branch[0] |
ba524d4c | 82 | else: |
9060d420 | 83 | patchname = None |
0618ea9c | 84 | |
89e3bc51 YD |
85 | if options.parent: |
86 | parent = git_id(options.parent) | |
87 | else: | |
88 | parent = commit.get_parent() | |
89 | ||
0618ea9c | 90 | if not options.reverse: |
89e3bc51 | 91 | bottom = parent |
0618ea9c CM |
92 | top = commit_id |
93 | else: | |
94 | bottom = commit_id | |
89e3bc51 | 95 | top = parent |
0618ea9c | 96 | |
ba524d4c | 97 | if options.fold: |
27ac2b7e | 98 | out.start('Folding commit %s' % commit_id) |
0618ea9c | 99 | |
d218d2f8 CM |
100 | # try a direct git-apply first |
101 | if not git.apply_diff(bottom, top): | |
f3b4fbff | 102 | git.merge(bottom, git.get_head(), top, recursive = True) |
5f9ce587 | 103 | |
27ac2b7e | 104 | out.done() |
d0bfda1a CM |
105 | elif options.update: |
106 | rev1 = git_id('//bottom') | |
107 | rev2 = git_id('//top') | |
108 | files = git.barefiles(rev1, rev2).split('\n') | |
109 | ||
27ac2b7e | 110 | out.start('Updating with commit %s' % commit_id) |
d0bfda1a CM |
111 | |
112 | if not git.apply_diff(bottom, top, files = files): | |
113 | raise CmdException, 'Patch updating failed' | |
114 | ||
27ac2b7e | 115 | out.done() |
ba524d4c CM |
116 | else: |
117 | message = commit.get_log() | |
2ff25eb8 CM |
118 | if options.expose: |
119 | message += '(imported from commit %s)\n' % commit.get_id_hash() | |
ba524d4c CM |
120 | author_name, author_email, author_date = \ |
121 | name_email_date(commit.get_author()) | |
122 | ||
27ac2b7e | 123 | out.start('Importing commit %s' % commit_id) |
0618ea9c | 124 | |
8fce9909 YD |
125 | newpatch = crt_series.new_patch(patchname, message = message, can_edit = False, |
126 | unapplied = True, bottom = bottom, top = top, | |
127 | author_name = author_name, | |
128 | author_email = author_email, | |
129 | author_date = author_date) | |
e2b1102c CM |
130 | # in case the patch name was automatically generated |
131 | patchname = newpatch.get_name() | |
132 | ||
8fce9909 YD |
133 | # find a patchlog to fork from |
134 | (refpatchname, refbranchname, refpatchid) = parse_rev(commit_str) | |
135 | if refpatchname and not refpatchid and \ | |
136 | (not refpatchid or refpatchid == 'top'): | |
137 | # FIXME: should also support picking //top.old | |
138 | if refbranchname: | |
139 | # assume the refseries is OK, since we already resolved | |
140 | # commit_str to a git_id | |
141 | refseries = Series(refbranchname) | |
142 | else: | |
143 | refseries = crt_series | |
144 | patch = refseries.get_patch(refpatchname) | |
145 | if patch.get_log(): | |
27ac2b7e KH |
146 | out.info("Log was %s" % newpatch.get_log()) |
147 | out.info("Setting log to %s\n" % patch.get_log()) | |
8fce9909 | 148 | newpatch.set_log(patch.get_log()) |
27ac2b7e | 149 | out.info("Log is now %s" % newpatch.get_log()) |
8fce9909 | 150 | else: |
27ac2b7e KH |
151 | out.info("No log for %s\n" % patchname) |
152 | ||
7829d19d | 153 | if not options.unapplied: |
2e88afce | 154 | modified = crt_series.push_patch(patchname) |
7829d19d CM |
155 | else: |
156 | modified = False | |
0618ea9c | 157 | |
2e88afce | 158 | if crt_series.empty_patch(patchname): |
27ac2b7e | 159 | out.done('empty patch') |
5f9ce587 | 160 | elif modified: |
27ac2b7e | 161 | out.done('modified') |
5f9ce587 | 162 | else: |
27ac2b7e KH |
163 | out.done() |
164 | ||
0618ea9c | 165 | print_crt_patch() |