Generate command lists automatically
[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
575bbdae 20from stgit.argparse import opt
fcee87cf
CM
21from stgit.commands.common import *
22from stgit.utils import *
5e888f30 23from stgit.out import *
fcee87cf
CM
24from stgit import stack, git
25from stgit.config import config
26
575bbdae 27help = 'Generate a new commit for the current patch'
33ff9cdd 28kind = 'patch'
575bbdae
KH
29usage = ['[options] [<files or dirs>]']
30description = """
26aab5b0
CM
31Include the latest tree changes in the current patch. This command
32generates a new GIT commit object with the patch details, the previous
b8038cef
PO
33one no longer being visible. The '--force' option is useful
34when a commit object was created with a different tool
35but the changes need to be included in the current patch."""
fcee87cf 36
575bbdae
KH
37options = [
38 opt('-f', '--force', action = 'store_true',
39 short = 'Force the refresh even if HEAD and top differ'),
40 opt('--update', action = 'store_true',
41 short = 'Only update the current patch files'),
42 opt('--index', action = 'store_true',
43 short = 'Refresh from index instead of worktree', long = """
44 Instead of setting the patch top to the current contents of
45 the worktree, set it to the current contents of the index."""),
46 opt('--undo', action = 'store_true',
47 short = 'Revert the commit generated by the last refresh'),
48 opt('-a', '--annotate', metavar = 'NOTE',
49 short = 'Annotate the patch log entry'),
50 opt('-p', '--patch',
51 short = 'Refresh (applied) PATCH instead of the top patch')]
52
d4356ac6 53directory = DirectoryHasRepository()
fcee87cf
CM
54
55def func(parser, options, args):
d4356ac6
CM
56 """Generate a new commit for the current or given patch.
57 """
58 args = git.ls_files(args)
59 directory.cd_to_topdir()
fcee87cf 60
d4356ac6 61 autoresolved = config.get('stgit.autoresolved')
fcee87cf
CM
62 if autoresolved != 'yes':
63 check_conflicts()
64
42fc7623 65 if options.patch:
6ee70d6b 66 if args or options.update:
42fc7623
YD
67 raise CmdException, \
68 'Only full refresh is available with the --patch option'
69 patch = options.patch
70 if not crt_series.patch_applied(patch):
71 raise CmdException, 'Patches "%s" not applied' % patch
72 else:
73 patch = crt_series.get_current()
74 if not patch:
75 raise CmdException, 'No patches applied'
fcee87cf 76
c3a72ae1
PO
77 if options.index:
78 if args or options.update:
79 raise CmdException, \
80 'Only full refresh is available with the --index option'
81 if options.patch:
82 raise CmdException, \
83 '--patch is not compatible with the --index option'
84
fcee87cf 85 if not options.force:
6972fd6b 86 check_head_top_equal(crt_series)
fcee87cf 87
f80bef49 88 if options.undo:
27ac2b7e 89 out.start('Undoing the refresh of "%s"' % patch)
f80bef49 90 crt_series.undo_refresh()
27ac2b7e 91 out.done()
f80bef49
CM
92 return
93
c3a72ae1
PO
94 if not options.index:
95 files = [path for (stat, path) in git.tree_status(files = args, verbose = True)]
6ee70d6b 96
c3a72ae1 97 if options.index or files or not crt_series.head_top_equal():
42fc7623
YD
98 if options.patch:
99 applied = crt_series.get_applied()
100 between = applied[:applied.index(patch):-1]
6972fd6b 101 pop_patches(crt_series, between, keep = True)
6ee70d6b 102 elif options.update:
e4560d7e
CM
103 rev1 = git_id(crt_series, 'HEAD^')
104 rev2 = git_id(crt_series, 'HEAD')
6ee70d6b
CM
105 patch_files = git.barefiles(rev1, rev2).split('\n')
106 files = [f for f in files if f in patch_files]
107 if not files:
108 out.info('No modified files for updating patch "%s"' % patch)
109 return
42fc7623 110
27ac2b7e 111 out.start('Refreshing patch "%s"' % patch)
fcee87cf
CM
112
113 if autoresolved == 'yes':
114 resolved_all()
c3a72ae1
PO
115
116 if options.index:
117 crt_series.refresh_patch(cache_update = False,
118 backup = True, notes = options.annotate)
119 else:
120 crt_series.refresh_patch(files = files,
121 backup = True, notes = options.annotate)
fcee87cf 122
dc4e5946 123 if crt_series.empty_patch(patch):
27ac2b7e 124 out.done('empty patch')
dc4e5946 125 else:
27ac2b7e 126 out.done()
7c53fcf5 127
42fc7623
YD
128 if options.patch:
129 between.reverse()
6972fd6b 130 push_patches(crt_series, between)
ea7634bb
CM
131 elif options.annotate:
132 # only annotate the top log entry as there is no need to
133 # refresh the patch and generate a full commit
134 crt_series.log_patch(crt_series.get_patch(patch), None,
135 notes = options.annotate)
fcee87cf 136 else:
27ac2b7e 137 out.info('Patch "%s" is already up to date' % patch)