Infrastructure for current directory handling
[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.out import *
24 from stgit import stack, git
25 from stgit.stack import Series
26
27
28 help = 'import a patch from a different branch or a commit object'
29 usage = """%prog [options] [<patch@branch>|<commit>]
30
31 Import a patch from a different branch or a commit object into the
32 current series. By default, the name of the imported patch is used as
33 the name of the current patch. It can be overridden with the '--name'
34 option. A commit object can be reverted with the '--reverse'
35 option. The log and author information are those of the commit object."""
36
37 directory = DirectoryHasRepository()
38 options = [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',
42 action = 'store_true'),
43 make_option('-p', '--parent', metavar = 'COMMITID',
44 help = 'use COMMITID as parent'),
45 make_option('-x', '--expose',
46 help = 'append the imported commit id to the patch log',
47 action = 'store_true'),
48 make_option('--fold',
49 help = 'fold the commit object into the current patch',
50 action = 'store_true'),
51 make_option('--update',
52 help = 'like fold but only update the current patch files',
53 action = 'store_true'),
54 make_option('--unapplied',
55 help = 'keep the patch unapplied',
56 action = 'store_true')]
57
58
59 def 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
65 if not options.unapplied:
66 check_local_changes()
67 check_conflicts()
68 check_head_top_equal()
69
70 commit_str = args[0]
71 commit_id = git_id(commit_str)
72 commit = git.Commit(commit_id)
73
74 if options.fold or options.update:
75 if not crt_series.get_current():
76 raise CmdException, 'No patches applied'
77 else:
78 patch_branch = commit_str.split('@')
79 if options.name:
80 patchname = options.name
81 elif len(patch_branch) == 2:
82 patchname = patch_branch[0]
83 else:
84 patchname = None
85
86 if options.parent:
87 parent = git_id(options.parent)
88 else:
89 parent = commit.get_parent()
90
91 if not options.reverse:
92 bottom = parent
93 top = commit_id
94 else:
95 bottom = commit_id
96 top = parent
97
98 if options.fold:
99 out.start('Folding commit %s' % commit_id)
100
101 # try a direct git-apply first
102 if not git.apply_diff(bottom, top):
103 git.merge(bottom, git.get_head(), top, recursive = True)
104
105 out.done()
106 elif options.update:
107 rev1 = git_id('//bottom')
108 rev2 = git_id('//top')
109 files = git.barefiles(rev1, rev2).split('\n')
110
111 out.start('Updating with commit %s' % commit_id)
112
113 if not git.apply_diff(bottom, top, files = files):
114 raise CmdException, 'Patch updating failed'
115
116 out.done()
117 else:
118 message = commit.get_log()
119 if options.expose:
120 message += '(imported from commit %s)\n' % commit.get_id_hash()
121 author_name, author_email, author_date = \
122 name_email_date(commit.get_author())
123
124 out.start('Importing commit %s' % commit_id)
125
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)
131 # in case the patch name was automatically generated
132 patchname = newpatch.get_name()
133
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():
147 out.info("Log was %s" % newpatch.get_log())
148 out.info("Setting log to %s\n" % patch.get_log())
149 newpatch.set_log(patch.get_log())
150 out.info("Log is now %s" % newpatch.get_log())
151 else:
152 out.info("No log for %s\n" % patchname)
153
154 if not options.unapplied:
155 modified = crt_series.push_patch(patchname)
156 else:
157 modified = False
158
159 if crt_series.empty_patch(patchname):
160 out.done('empty patch')
161 elif modified:
162 out.done('modified')
163 else:
164 out.done()
165
166 print_crt_patch()