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