chiark / gitweb /
0fc54b8296154c4757508c68b75109ba42aa6b3d
[stgit] / stgit / commands / pick.py
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 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
25
26 help = 'Import a patch from a different branch or a commit object'
27 kind = 'patch'
28 usage = ['[options] ([<patch1>] [<patch2>] [<patch3>..<patch4>])|<commit>']
29 description = """
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 '--reverse'
34 option. The log and author information are those of the commit
35 object."""
36
37 args = [argparse.patch_range(argparse.applied_patches,
38                              argparse.unapplied_patches,
39                              argparse.hidden_patches)]
40 options = [
41     opt('-n', '--name',
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', '--reverse', action = 'store_true',
46         short = 'Reverse the commit object before importing'),
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('--unapplied', action = 'store_true',
56         short = 'Keep the patch unapplied')]
57
58 directory = DirectoryGotoToplevel(log = True)
59
60 def __pick_commit(commit_id, patchname, options):
61     """Pick a commit id.
62     """
63     commit = git.Commit(commit_id)
64
65     if options.name:
66         patchname = options.name
67
68     if options.parent:
69         parent = git_id(crt_series, options.parent)
70     else:
71         parent = commit.get_parent()
72
73     if not options.reverse:
74         bottom = parent
75         top = commit_id
76     else:
77         bottom = commit_id
78         top = parent
79
80     if options.fold:
81         out.start('Folding commit %s' % commit_id)
82
83         # try a direct git apply first
84         if not git.apply_diff(bottom, top):
85             git.merge_recursive(bottom, git.get_head(), top)
86
87         out.done()
88     elif options.update:
89         rev1 = git_id(crt_series, 'HEAD^')
90         rev2 = git_id(crt_series, 'HEAD')
91         files = git.barefiles(rev1, rev2).split('\n')
92
93         out.start('Updating with commit %s' % commit_id)
94
95         if not git.apply_diff(bottom, top, files = files):
96             raise CmdException, 'Patch updating failed'
97
98         out.done()
99     else:
100         message = commit.get_log()
101         if options.expose:
102             message += '(imported from commit %s)\n' % commit.get_id_hash()
103         author_name, author_email, author_date = \
104                      name_email_date(commit.get_author())
105
106         out.start('Importing commit %s' % commit_id)
107
108         newpatch = crt_series.new_patch(patchname, message = message, can_edit = False,
109                                         unapplied = True, bottom = bottom, top = top,
110                                         author_name = author_name,
111                                         author_email = author_email,
112                                         author_date = author_date)
113         # in case the patch name was automatically generated
114         patchname = newpatch.get_name()
115
116         # find a patchlog to fork from
117         refbranchname, refpatchname = parse_rev(patchname)
118         if refpatchname:
119             if refbranchname:
120                 # assume the refseries is OK, since we already resolved
121                 # commit_str to a git_id
122                 refseries = Series(refbranchname)
123             else:
124                 refseries = crt_series
125             patch = refseries.get_patch(refpatchname)
126             if patch.get_log():
127                 out.info("Log was %s" % newpatch.get_log())
128                 out.info("Setting log to %s\n" %  patch.get_log())
129                 newpatch.set_log(patch.get_log())
130                 out.info("Log is now %s" % newpatch.get_log())
131             else:
132                 out.info("No log for %s\n" % patchname)
133
134         if not options.unapplied:
135             modified = crt_series.push_patch(patchname)
136         else:
137             modified = False
138
139         if crt_series.empty_patch(patchname):
140             out.done('empty patch')
141         elif modified:
142             out.done('modified')
143         else:
144             out.done()
145
146
147 def func(parser, options, args):
148     """Import a commit object as a new patch
149     """
150     if not args:
151         parser.error('incorrect number of arguments')
152
153     if not options.unapplied:
154         check_local_changes()
155         check_conflicts()
156         check_head_top_equal(crt_series)
157
158     if options.ref_branch:
159         remote_series = Series(options.ref_branch)
160     else:
161         remote_series = crt_series
162
163     applied = remote_series.get_applied()
164     unapplied = remote_series.get_unapplied()
165     try:
166         patches = parse_patches(args, applied + unapplied, len(applied))
167         commit_id = None
168     except CmdException:
169         if len(args) > 1:
170             raise
171         # no patches found, try a commit id
172         commit_id = git_id(remote_series, args[0])
173
174     if not commit_id and len(patches) > 1:
175         if options.name:
176             raise CmdException, '--name can only be specified with one patch'
177         if options.parent:
178             raise CmdException, '--parent can only be specified with one patch'
179
180     if options.update and not crt_series.get_current():
181         raise CmdException, 'No patches applied'
182
183     if commit_id:
184         __pick_commit(commit_id, None, options)
185     else:
186         if options.unapplied:
187             patches.reverse()
188         for patch in patches:
189             __pick_commit(git_id(remote_series, patch), patch, options)
190
191     print_crt_patch(crt_series)