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