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 * | |
23 | from stgit import stack, git | |
24 | ||
25 | ||
26 | help = 'import a patch from a different branch or a commit object' | |
27 | usage = """%prog [options] [<patch@branch>|<commit>] | |
28 | ||
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 | |
388f63b6 | 31 | the name of the current patch. It can be overridden with the '--name' |
0618ea9c CM |
32 | option. A commit object can be reverted with the '--reverse' |
33 | option. The log and author information are those of the commit object.""" | |
34 | ||
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', | |
ba524d4c | 39 | action = 'store_true'), |
89e3bc51 YD |
40 | make_option('-p', '--parent', metavar = 'COMMITID', |
41 | help = 'use COMMITID as parent'), | |
ba524d4c CM |
42 | make_option('--fold', |
43 | help = 'fold the commit object into the current patch', | |
d0bfda1a CM |
44 | action = 'store_true'), |
45 | make_option('--update', | |
46 | help = 'like fold but only update the current patch files', | |
0618ea9c CM |
47 | action = 'store_true')] |
48 | ||
49 | ||
50 | def func(parser, options, args): | |
51 | """Import a commit object as a new patch | |
52 | """ | |
53 | if len(args) != 1: | |
54 | parser.error('incorrect number of arguments') | |
55 | ||
56 | check_local_changes() | |
57 | check_conflicts() | |
58 | check_head_top_equal() | |
59 | ||
60 | commit_str = args[0] | |
fff9bce5 CM |
61 | commit_id = git_id(commit_str) |
62 | commit = git.Commit(commit_id) | |
0618ea9c | 63 | |
d0bfda1a | 64 | if options.fold or options.update: |
ba524d4c CM |
65 | if not crt_series.get_current(): |
66 | raise CmdException, 'No patches applied' | |
0618ea9c | 67 | else: |
ba524d4c | 68 | patch_branch = commit_str.split('@') |
9a6e2cc3 | 69 | if options.name: |
ba524d4c | 70 | patch = options.name |
9a6e2cc3 CM |
71 | elif len(patch_branch) == 2: |
72 | patch = patch_branch[0] | |
ba524d4c | 73 | else: |
fff9bce5 CM |
74 | patch = make_patch_name(commit.get_log()) |
75 | if not patch: | |
76 | raise CmdException, 'Unknown patch name' | |
0618ea9c | 77 | |
89e3bc51 YD |
78 | if options.parent: |
79 | parent = git_id(options.parent) | |
80 | else: | |
81 | parent = commit.get_parent() | |
82 | ||
0618ea9c | 83 | if not options.reverse: |
89e3bc51 | 84 | bottom = parent |
0618ea9c CM |
85 | top = commit_id |
86 | else: | |
87 | bottom = commit_id | |
89e3bc51 | 88 | top = parent |
0618ea9c | 89 | |
ba524d4c CM |
90 | if options.fold: |
91 | print 'Folding commit %s...' % commit_id, | |
92 | sys.stdout.flush() | |
0618ea9c | 93 | |
d218d2f8 CM |
94 | # try a direct git-apply first |
95 | if not git.apply_diff(bottom, top): | |
96 | git.merge(bottom, git.get_head(), top) | |
5f9ce587 CM |
97 | |
98 | print 'done' | |
d0bfda1a CM |
99 | elif options.update: |
100 | rev1 = git_id('//bottom') | |
101 | rev2 = git_id('//top') | |
102 | files = git.barefiles(rev1, rev2).split('\n') | |
103 | ||
104 | print 'Updating with commit %s...' % commit_id, | |
105 | sys.stdout.flush() | |
106 | ||
107 | if not git.apply_diff(bottom, top, files = files): | |
108 | raise CmdException, 'Patch updating failed' | |
109 | ||
110 | print 'done' | |
ba524d4c CM |
111 | else: |
112 | message = commit.get_log() | |
113 | author_name, author_email, author_date = \ | |
114 | name_email_date(commit.get_author()) | |
115 | ||
116 | print 'Importing commit %s...' % commit_id, | |
117 | sys.stdout.flush() | |
0618ea9c | 118 | |
ba524d4c CM |
119 | crt_series.new_patch(patch, message = message, can_edit = False, |
120 | unapplied = True, bottom = bottom, top = top, | |
121 | author_name = author_name, | |
122 | author_email = author_email, | |
123 | author_date = author_date) | |
5f9ce587 | 124 | modified = crt_series.push_patch(patch) |
0618ea9c | 125 | |
5f9ce587 CM |
126 | if crt_series.empty_patch(patch): |
127 | print 'done (empty patch)' | |
128 | elif modified: | |
129 | print 'done (modified)' | |
130 | else: | |
131 | print 'done' | |
ba524d4c | 132 | |
0618ea9c | 133 | print_crt_patch() |