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