Add '--update' option to 'refresh'
[stgit] / stgit / commands / refresh.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
19import sys, os
20from optparse import OptionParser, make_option
21
22from stgit.commands.common import *
23from stgit.utils import *
24from stgit import stack, git
25from stgit.config import config
26
27
28help = 'generate a new commit for the current patch'
026c0689 29usage = """%prog [options] [<files...>]
26aab5b0
CM
30
31Include the latest tree changes in the current patch. This command
32generates a new GIT commit object with the patch details, the previous
33one no longer being visible. The patch attributes like author,
34committer and description can be changed with the command line
35options. The '--force' option is useful when a commit object was
36created with a different tool but the changes need to be included in
37the current patch."""
fcee87cf
CM
38
39options = [make_option('-f', '--force',
40 help = 'force the refresh even if HEAD and '\
41 'top differ',
42 action = 'store_true'),
43 make_option('-e', '--edit',
44 help = 'invoke an editor for the patch '\
45 'description',
46 action = 'store_true'),
6ad48e48
PBG
47 make_option('-s', '--showpatch',
48 help = 'show the patch content in the editor buffer',
49 action = 'store_true'),
6ee70d6b
CM
50 make_option('--update',
51 help = 'only update the current patch files',
52 action = 'store_true'),
f80bef49
CM
53 make_option('--undo',
54 help = 'revert the commit generated by the last refresh',
55 action = 'store_true'),
fcee87cf
CM
56 make_option('-m', '--message',
57 help = 'use MESSAGE as the patch ' \
58 'description'),
19cd0a8f
CM
59 make_option('-a', '--author', metavar = '"NAME <EMAIL>"',
60 help = 'use "NAME <EMAIL>" as the author details'),
fcee87cf
CM
61 make_option('--authname',
62 help = 'use AUTHNAME as the author name'),
63 make_option('--authemail',
64 help = 'use AUTHEMAIL as the author e-mail'),
65 make_option('--authdate',
66 help = 'use AUTHDATE as the author date'),
67 make_option('--commname',
68 help = 'use COMMNAME as the committer name'),
69 make_option('--commemail',
70 help = 'use COMMEMAIL as the committer ' \
c40c3500 71 'e-mail'),
42fc7623
YD
72 make_option('-p', '--patch',
73 help = 'refresh (applied) PATCH instead of the top one'),
c40c3500
CM
74 make_option('--sign',
75 help = 'add Signed-off-by line',
76 action = 'store_true'),
77 make_option('--ack',
78 help = 'add Acked-by line',
79 action = 'store_true')]
fcee87cf
CM
80
81
82def func(parser, options, args):
c73e63b7 83 autoresolved = config.get('stgit.autoresolved')
fcee87cf
CM
84
85 if autoresolved != 'yes':
86 check_conflicts()
87
42fc7623 88 if options.patch:
6ee70d6b 89 if args or options.update:
42fc7623
YD
90 raise CmdException, \
91 'Only full refresh is available with the --patch option'
92 patch = options.patch
93 if not crt_series.patch_applied(patch):
94 raise CmdException, 'Patches "%s" not applied' % patch
95 else:
96 patch = crt_series.get_current()
97 if not patch:
98 raise CmdException, 'No patches applied'
fcee87cf
CM
99
100 if not options.force:
101 check_head_top_equal()
102
f80bef49 103 if options.undo:
27ac2b7e 104 out.start('Undoing the refresh of "%s"' % patch)
f80bef49 105 crt_series.undo_refresh()
27ac2b7e 106 out.done()
f80bef49
CM
107 return
108
19cd0a8f
CM
109 if options.author:
110 options.authname, options.authemail = name_email(options.author)
111
c40c3500
CM
112 if options.sign:
113 sign_str = 'Signed-off-by'
bc7ba6e9
YD
114 if options.ack:
115 raise CmdException, '--ack and --sign were both specified'
c40c3500
CM
116 elif options.ack:
117 sign_str = 'Acked-by'
118 else:
119 sign_str = None
120
6ee70d6b
CM
121 files = [x[1] for x in git.tree_status(verbose = True)]
122 if args:
123 files = [f for f in files if f in args]
124
125 if files or not crt_series.head_top_equal() \
fcee87cf
CM
126 or options.edit or options.message \
127 or options.authname or options.authemail or options.authdate \
c40c3500
CM
128 or options.commname or options.commemail \
129 or options.sign or options.ack:
42fc7623
YD
130
131 if options.patch:
132 applied = crt_series.get_applied()
133 between = applied[:applied.index(patch):-1]
134 pop_patches(between, keep = True)
6ee70d6b
CM
135 elif options.update:
136 rev1 = git_id('//bottom')
137 rev2 = git_id('//top')
138 patch_files = git.barefiles(rev1, rev2).split('\n')
139 files = [f for f in files if f in patch_files]
140 if not files:
141 out.info('No modified files for updating patch "%s"' % patch)
142 return
42fc7623 143
27ac2b7e 144 out.start('Refreshing patch "%s"' % patch)
fcee87cf
CM
145
146 if autoresolved == 'yes':
147 resolved_all()
6ee70d6b 148 crt_series.refresh_patch(files = files,
026c0689 149 message = options.message,
fcee87cf 150 edit = options.edit,
6ad48e48 151 show_patch = options.showpatch,
fcee87cf
CM
152 author_name = options.authname,
153 author_email = options.authemail,
154 author_date = options.authdate,
155 committer_name = options.commname,
f80bef49 156 committer_email = options.commemail,
c40c3500 157 backup = True, sign_str = sign_str)
fcee87cf 158
dc4e5946 159 if crt_series.empty_patch(patch):
27ac2b7e 160 out.done('empty patch')
dc4e5946 161 else:
27ac2b7e 162 out.done()
7c53fcf5 163
42fc7623
YD
164 if options.patch:
165 between.reverse()
166 push_patches(between)
fcee87cf 167 else:
27ac2b7e 168 out.info('Patch "%s" is already up to date' % patch)