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