Convert "push" to the lib infrastructure
[stgit] / stgit / commands / push.py
CommitLineData
fcee87cf
CM
1
2__copyright__ = """
3Copyright (C) 2005, Catalin Marinas <catalin.marinas@gmail.com>
4
5This program is free software; you can redistribute it and/or modify
6it under the terms of the GNU General Public License version 2 as
7published by the Free Software Foundation.
8
9This program is distributed in the hope that it will be useful,
10but WITHOUT ANY WARRANTY; without even the implied warranty of
11MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12GNU General Public License for more details.
13
14You should have received a copy of the GNU General Public License
15along with this program; if not, write to the Free Software
16Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17"""
18
f979802d
CM
19from stgit.commands import common
20from stgit.lib import transaction
21from stgit import argparse
575bbdae 22from stgit.argparse import opt
fcee87cf 23
575bbdae 24help = 'Push one or more patches onto the stack'
33ff9cdd 25kind = 'stack'
575bbdae
KH
26usage = ['[options] [<patch1>] [<patch2>] [<patch3>..<patch4>]']
27description = """
6b1e0111
CM
28Push one or more patches (defaulting to the first unapplied one) onto
29the stack. The 'push' operation allows patch reordering by commuting
625e8de7
KH
30them with the three-way merge algorithm. If there are conflicts while
31pushing a patch, those conflicts are written to the work tree, and the
32command halts. Conflicts raised during the push operation have to be
33fixed and the 'resolved' command run (alternatively, you may undo the
34conflicting push with 'stg undo').
26aab5b0 35
718a5e51
CM
36The command also notifies when the patch becomes empty (fully merged
37upstream) or is modified (three-way merged) by the 'push' operation."""
fcee87cf 38
6c8a90e1 39args = [argparse.patch_range(argparse.unapplied_patches)]
575bbdae
KH
40options = [
41 opt('-a', '--all', action = 'store_true',
42 short = 'Push all the unapplied patches'),
43 opt('-n', '--number', type = 'int',
44 short = 'Push the specified number of patches'),
45 opt('--reverse', action = 'store_true',
f979802d
CM
46 short = 'Push the patches in reverse order')
47 ] + argparse.keep_option() + argparse.merged_option()
fcee87cf 48
f979802d 49directory = common.DirectoryHasRepositoryLib()
fcee87cf
CM
50
51def func(parser, options, args):
f979802d
CM
52 """Pushes the given patches or the first unapplied onto the stack."""
53 stack = directory.repository.current_stack
54 iw = stack.repository.default_iw
55 clean_iw = (not options.keep and iw) or None
56 trans = transaction.StackTransaction(stack, 'pop',
57 check_clean_iw = clean_iw)
58
59 if not trans.unapplied:
60 raise common.CmdException('No patches to push')
61
62 if options.all:
63 patches = list(trans.unapplied)
64 elif options.number:
65 patches = trans.unapplied[:options.number]
66 elif not args:
67 patches = [trans.unapplied[0]]
fcee87cf 68 else:
f979802d 69 patches = common.parse_patches(args, trans.unapplied)
fcee87cf 70
f979802d
CM
71 if not patches:
72 raise common.CmdException('No patches to push')
fcee87cf
CM
73
74 if options.reverse:
75 patches.reverse()
76
f979802d
CM
77 try:
78 if options.merged:
79 merged = set(trans.check_merged(patches))
80 else:
81 merged = set()
82 for pn in patches:
83 trans.push_patch(pn, iw, allow_interactive = True,
84 already_merged = pn in merged)
85 except transaction.TransactionHalted:
86 pass
87 return trans.run(iw)