Implement a 'pick' command for cherry-picking
[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
31the name of the current patch. It can be overriden with the '--name'
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',
39 action = 'store_true')]
40
41
42def func(parser, options, args):
43 """Import a commit object as a new patch
44 """
45 if len(args) != 1:
46 parser.error('incorrect number of arguments')
47
48 check_local_changes()
49 check_conflicts()
50 check_head_top_equal()
51
52 commit_str = args[0]
53 patch_branch = commit_str.split('@')
54
55 if len(patch_branch) == 2:
56 patch = patch_branch[0]
57 elif options.name:
58 patch = options.name
59 else:
60 raise CmdException, 'Unkown patch name'
61
62 commit_id = git_id(commit_str)
63 commit = git.Commit(commit_id)
64
65 if not options.reverse:
66 bottom = commit.get_parent()
67 top = commit_id
68 else:
69 bottom = commit_id
70 top = commit.get_parent()
71
72 message = commit.get_log()
73 author_name, author_email, author_date = \
74 name_email_date(commit.get_author())
75
76 print 'Importing commit %s...' % commit_id,
77 sys.stdout.flush()
78
79 crt_series.new_patch(patch, message = message, can_edit = False,
80 unapplied = True, bottom = bottom, top = top,
81 author_name = author_name,
82 author_email = author_email,
83 author_date = author_date)
84 crt_series.push_patch(patch)
85
86 print 'done'
87 print_crt_patch()