Do not use the pager when the output is empty
[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
CM
39 action = 'store_true'),
40 make_option('--fold',
41 help = 'fold the commit object into the current patch',
d0bfda1a
CM
42 action = 'store_true'),
43 make_option('--update',
44 help = 'like fold but only update the current patch files',
0618ea9c
CM
45 action = 'store_true')]
46
47
48def func(parser, options, args):
49 """Import a commit object as a new patch
50 """
51 if len(args) != 1:
52 parser.error('incorrect number of arguments')
53
54 check_local_changes()
55 check_conflicts()
56 check_head_top_equal()
57
58 commit_str = args[0]
0618ea9c 59
d0bfda1a 60 if options.fold or options.update:
ba524d4c
CM
61 if not crt_series.get_current():
62 raise CmdException, 'No patches applied'
0618ea9c 63 else:
ba524d4c 64 patch_branch = commit_str.split('@')
9a6e2cc3 65 if options.name:
ba524d4c 66 patch = options.name
9a6e2cc3
CM
67 elif len(patch_branch) == 2:
68 patch = patch_branch[0]
ba524d4c 69 else:
69517f72 70 raise CmdException, 'Unknown patch name'
0618ea9c
CM
71
72 commit_id = git_id(commit_str)
73 commit = git.Commit(commit_id)
74
75 if not options.reverse:
76 bottom = commit.get_parent()
77 top = commit_id
78 else:
79 bottom = commit_id
80 top = commit.get_parent()
81
ba524d4c
CM
82 if options.fold:
83 print 'Folding commit %s...' % commit_id,
84 sys.stdout.flush()
0618ea9c 85
d218d2f8
CM
86 # try a direct git-apply first
87 if not git.apply_diff(bottom, top):
88 git.merge(bottom, git.get_head(), top)
5f9ce587
CM
89
90 print 'done'
d0bfda1a
CM
91 elif options.update:
92 rev1 = git_id('//bottom')
93 rev2 = git_id('//top')
94 files = git.barefiles(rev1, rev2).split('\n')
95
96 print 'Updating with commit %s...' % commit_id,
97 sys.stdout.flush()
98
99 if not git.apply_diff(bottom, top, files = files):
100 raise CmdException, 'Patch updating failed'
101
102 print 'done'
ba524d4c
CM
103 else:
104 message = commit.get_log()
105 author_name, author_email, author_date = \
106 name_email_date(commit.get_author())
107
108 print 'Importing commit %s...' % commit_id,
109 sys.stdout.flush()
0618ea9c 110
ba524d4c
CM
111 crt_series.new_patch(patch, message = message, can_edit = False,
112 unapplied = True, bottom = bottom, top = top,
113 author_name = author_name,
114 author_email = author_email,
115 author_date = author_date)
5f9ce587 116 modified = crt_series.push_patch(patch)
0618ea9c 117
5f9ce587
CM
118 if crt_series.empty_patch(patch):
119 print 'done (empty patch)'
120 elif modified:
121 print 'done (modified)'
122 else:
123 print 'done'
ba524d4c 124
0618ea9c 125 print_crt_patch()