chiark / gitweb /
65462b3d48ff7095d822d9ca565dde8b1aed9184
[stgit] / stgit / commands / push.py
1
2 __copyright__ = """
3 Copyright (C) 2005, Catalin Marinas <catalin.marinas@gmail.com>
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License version 2 as
7 published by the Free Software Foundation.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 """
18
19 import sys, os
20 from optparse import OptionParser, make_option
21
22 from stgit.commands.common import *
23 from stgit.utils import *
24 from stgit import stack, git
25
26
27 help = 'push a patch on top of the series'
28 usage = '%prog [options] [<name>]'
29
30 options = [make_option('-a', '--all',
31                        help = 'push all the unapplied patches',
32                        action = 'store_true'),
33            make_option('-n', '--number', type = 'int',
34                        help = 'push the specified number of patches'),
35            make_option('-t', '--to', metavar = 'PATCH1[:PATCH2]',
36                        help = 'push all patches to PATCH1 or between '
37                        'PATCH1 and PATCH2'),
38            make_option('--reverse',
39                        help = 'push the patches in reverse order',
40                        action = 'store_true'),
41            make_option('--undo',
42                        help = 'undo the last push operation',
43                        action = 'store_true')]
44
45
46 def func(parser, options, args):
47     """Pushes the given patch or all onto the series
48     """
49     # If --undo is passed, do the work and exit
50     if options.undo:
51         patch = crt_series.get_current()
52         if not patch:
53             raise CmdException, 'No patch to undo'
54
55         print 'Undoing the "%s" push...' % patch,
56         sys.stdout.flush()
57         resolved_all()
58         crt_series.undo_push()
59         print 'done'
60         print_crt_patch()
61
62         return
63
64     check_local_changes()
65     check_conflicts()
66     check_head_top_equal()
67
68     unapplied = crt_series.get_unapplied()
69     if not unapplied:
70         raise CmdException, 'No more patches to push'
71
72     if options.to:
73         boundaries = options.to.split(':')
74         if len(boundaries) == 1:
75             if boundaries[0] not in unapplied:
76                 raise CmdException, 'Patch "%s" not unapplied' % boundaries[0]
77             patches = unapplied[:unapplied.index(boundaries[0])+1]
78         elif len(boundaries) == 2:
79             if boundaries[0] not in unapplied:
80                 raise CmdException, 'Patch "%s" not unapplied' % boundaries[0]
81             if boundaries[1] not in unapplied:
82                 raise CmdException, 'Patch "%s" not unapplied' % boundaries[1]
83             lb = unapplied.index(boundaries[0])
84             hb = unapplied.index(boundaries[1])
85             if lb > hb:
86                 raise CmdException, 'Patch "%s" after "%s"' \
87                       % (boundaries[0], boundaries[1])
88             patches = unapplied[lb:hb+1]
89         else:
90             raise CmdException, 'incorrect parameters to "--to"'
91     elif options.number:
92         patches = unapplied[:options.number]
93     elif options.all:
94         patches = unapplied
95     elif len(args) == 0:
96         patches = [unapplied[0]]
97     elif len(args) == 1:
98         patches = [args[0]]
99     else:
100         parser.error('incorrect number of arguments')
101
102     if patches == []:
103         raise CmdException, 'No patches to push'
104
105     if options.reverse:
106         patches.reverse()
107
108     for p in patches:
109         print 'Pushing patch "%s"...' % p,
110         sys.stdout.flush()
111
112         crt_series.push_patch(p)
113
114         if crt_series.empty_patch(p):
115             print 'done (empty patch)'
116         else:
117             print 'done'
118     print_crt_patch()