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