##// END OF EJS Templates
strip: make it clear that --force discards changes (issue310)
Patrick Mezard -
r14026:4f19242d default
parent child Browse files
Show More
@@ -1,3291 +1,3292
1 # mq.py - patch queues for mercurial
1 # mq.py - patch queues for mercurial
2 #
2 #
3 # Copyright 2005, 2006 Chris Mason <mason@suse.com>
3 # Copyright 2005, 2006 Chris Mason <mason@suse.com>
4 #
4 #
5 # This software may be used and distributed according to the terms of the
5 # This software may be used and distributed according to the terms of the
6 # GNU General Public License version 2 or any later version.
6 # GNU General Public License version 2 or any later version.
7
7
8 '''manage a stack of patches
8 '''manage a stack of patches
9
9
10 This extension lets you work with a stack of patches in a Mercurial
10 This extension lets you work with a stack of patches in a Mercurial
11 repository. It manages two stacks of patches - all known patches, and
11 repository. It manages two stacks of patches - all known patches, and
12 applied patches (subset of known patches).
12 applied patches (subset of known patches).
13
13
14 Known patches are represented as patch files in the .hg/patches
14 Known patches are represented as patch files in the .hg/patches
15 directory. Applied patches are both patch files and changesets.
15 directory. Applied patches are both patch files and changesets.
16
16
17 Common tasks (use :hg:`help command` for more details)::
17 Common tasks (use :hg:`help command` for more details)::
18
18
19 create new patch qnew
19 create new patch qnew
20 import existing patch qimport
20 import existing patch qimport
21
21
22 print patch series qseries
22 print patch series qseries
23 print applied patches qapplied
23 print applied patches qapplied
24
24
25 add known patch to applied stack qpush
25 add known patch to applied stack qpush
26 remove patch from applied stack qpop
26 remove patch from applied stack qpop
27 refresh contents of top applied patch qrefresh
27 refresh contents of top applied patch qrefresh
28
28
29 By default, mq will automatically use git patches when required to
29 By default, mq will automatically use git patches when required to
30 avoid losing file mode changes, copy records, binary files or empty
30 avoid losing file mode changes, copy records, binary files or empty
31 files creations or deletions. This behaviour can be configured with::
31 files creations or deletions. This behaviour can be configured with::
32
32
33 [mq]
33 [mq]
34 git = auto/keep/yes/no
34 git = auto/keep/yes/no
35
35
36 If set to 'keep', mq will obey the [diff] section configuration while
36 If set to 'keep', mq will obey the [diff] section configuration while
37 preserving existing git patches upon qrefresh. If set to 'yes' or
37 preserving existing git patches upon qrefresh. If set to 'yes' or
38 'no', mq will override the [diff] section and always generate git or
38 'no', mq will override the [diff] section and always generate git or
39 regular patches, possibly losing data in the second case.
39 regular patches, possibly losing data in the second case.
40
40
41 You will by default be managing a patch queue named "patches". You can
41 You will by default be managing a patch queue named "patches". You can
42 create other, independent patch queues with the :hg:`qqueue` command.
42 create other, independent patch queues with the :hg:`qqueue` command.
43 '''
43 '''
44
44
45 from mercurial.i18n import _
45 from mercurial.i18n import _
46 from mercurial.node import bin, hex, short, nullid, nullrev
46 from mercurial.node import bin, hex, short, nullid, nullrev
47 from mercurial.lock import release
47 from mercurial.lock import release
48 from mercurial import commands, cmdutil, hg, patch, scmutil, util
48 from mercurial import commands, cmdutil, hg, patch, scmutil, util
49 from mercurial import repair, extensions, url, error
49 from mercurial import repair, extensions, url, error
50 import os, sys, re, errno, shutil
50 import os, sys, re, errno, shutil
51
51
52 commands.norepo += " qclone"
52 commands.norepo += " qclone"
53
53
54 # Patch names looks like unix-file names.
54 # Patch names looks like unix-file names.
55 # They must be joinable with queue directory and result in the patch path.
55 # They must be joinable with queue directory and result in the patch path.
56 normname = util.normpath
56 normname = util.normpath
57
57
58 class statusentry(object):
58 class statusentry(object):
59 def __init__(self, node, name):
59 def __init__(self, node, name):
60 self.node, self.name = node, name
60 self.node, self.name = node, name
61 def __repr__(self):
61 def __repr__(self):
62 return hex(self.node) + ':' + self.name
62 return hex(self.node) + ':' + self.name
63
63
64 class patchheader(object):
64 class patchheader(object):
65 def __init__(self, pf, plainmode=False):
65 def __init__(self, pf, plainmode=False):
66 def eatdiff(lines):
66 def eatdiff(lines):
67 while lines:
67 while lines:
68 l = lines[-1]
68 l = lines[-1]
69 if (l.startswith("diff -") or
69 if (l.startswith("diff -") or
70 l.startswith("Index:") or
70 l.startswith("Index:") or
71 l.startswith("===========")):
71 l.startswith("===========")):
72 del lines[-1]
72 del lines[-1]
73 else:
73 else:
74 break
74 break
75 def eatempty(lines):
75 def eatempty(lines):
76 while lines:
76 while lines:
77 if not lines[-1].strip():
77 if not lines[-1].strip():
78 del lines[-1]
78 del lines[-1]
79 else:
79 else:
80 break
80 break
81
81
82 message = []
82 message = []
83 comments = []
83 comments = []
84 user = None
84 user = None
85 date = None
85 date = None
86 parent = None
86 parent = None
87 format = None
87 format = None
88 subject = None
88 subject = None
89 branch = None
89 branch = None
90 nodeid = None
90 nodeid = None
91 diffstart = 0
91 diffstart = 0
92
92
93 for line in file(pf):
93 for line in file(pf):
94 line = line.rstrip()
94 line = line.rstrip()
95 if (line.startswith('diff --git')
95 if (line.startswith('diff --git')
96 or (diffstart and line.startswith('+++ '))):
96 or (diffstart and line.startswith('+++ '))):
97 diffstart = 2
97 diffstart = 2
98 break
98 break
99 diffstart = 0 # reset
99 diffstart = 0 # reset
100 if line.startswith("--- "):
100 if line.startswith("--- "):
101 diffstart = 1
101 diffstart = 1
102 continue
102 continue
103 elif format == "hgpatch":
103 elif format == "hgpatch":
104 # parse values when importing the result of an hg export
104 # parse values when importing the result of an hg export
105 if line.startswith("# User "):
105 if line.startswith("# User "):
106 user = line[7:]
106 user = line[7:]
107 elif line.startswith("# Date "):
107 elif line.startswith("# Date "):
108 date = line[7:]
108 date = line[7:]
109 elif line.startswith("# Parent "):
109 elif line.startswith("# Parent "):
110 parent = line[9:]
110 parent = line[9:]
111 elif line.startswith("# Branch "):
111 elif line.startswith("# Branch "):
112 branch = line[9:]
112 branch = line[9:]
113 elif line.startswith("# Node ID "):
113 elif line.startswith("# Node ID "):
114 nodeid = line[10:]
114 nodeid = line[10:]
115 elif not line.startswith("# ") and line:
115 elif not line.startswith("# ") and line:
116 message.append(line)
116 message.append(line)
117 format = None
117 format = None
118 elif line == '# HG changeset patch':
118 elif line == '# HG changeset patch':
119 message = []
119 message = []
120 format = "hgpatch"
120 format = "hgpatch"
121 elif (format != "tagdone" and (line.startswith("Subject: ") or
121 elif (format != "tagdone" and (line.startswith("Subject: ") or
122 line.startswith("subject: "))):
122 line.startswith("subject: "))):
123 subject = line[9:]
123 subject = line[9:]
124 format = "tag"
124 format = "tag"
125 elif (format != "tagdone" and (line.startswith("From: ") or
125 elif (format != "tagdone" and (line.startswith("From: ") or
126 line.startswith("from: "))):
126 line.startswith("from: "))):
127 user = line[6:]
127 user = line[6:]
128 format = "tag"
128 format = "tag"
129 elif (format != "tagdone" and (line.startswith("Date: ") or
129 elif (format != "tagdone" and (line.startswith("Date: ") or
130 line.startswith("date: "))):
130 line.startswith("date: "))):
131 date = line[6:]
131 date = line[6:]
132 format = "tag"
132 format = "tag"
133 elif format == "tag" and line == "":
133 elif format == "tag" and line == "":
134 # when looking for tags (subject: from: etc) they
134 # when looking for tags (subject: from: etc) they
135 # end once you find a blank line in the source
135 # end once you find a blank line in the source
136 format = "tagdone"
136 format = "tagdone"
137 elif message or line:
137 elif message or line:
138 message.append(line)
138 message.append(line)
139 comments.append(line)
139 comments.append(line)
140
140
141 eatdiff(message)
141 eatdiff(message)
142 eatdiff(comments)
142 eatdiff(comments)
143 # Remember the exact starting line of the patch diffs before consuming
143 # Remember the exact starting line of the patch diffs before consuming
144 # empty lines, for external use by TortoiseHg and others
144 # empty lines, for external use by TortoiseHg and others
145 self.diffstartline = len(comments)
145 self.diffstartline = len(comments)
146 eatempty(message)
146 eatempty(message)
147 eatempty(comments)
147 eatempty(comments)
148
148
149 # make sure message isn't empty
149 # make sure message isn't empty
150 if format and format.startswith("tag") and subject:
150 if format and format.startswith("tag") and subject:
151 message.insert(0, "")
151 message.insert(0, "")
152 message.insert(0, subject)
152 message.insert(0, subject)
153
153
154 self.message = message
154 self.message = message
155 self.comments = comments
155 self.comments = comments
156 self.user = user
156 self.user = user
157 self.date = date
157 self.date = date
158 self.parent = parent
158 self.parent = parent
159 # nodeid and branch are for external use by TortoiseHg and others
159 # nodeid and branch are for external use by TortoiseHg and others
160 self.nodeid = nodeid
160 self.nodeid = nodeid
161 self.branch = branch
161 self.branch = branch
162 self.haspatch = diffstart > 1
162 self.haspatch = diffstart > 1
163 self.plainmode = plainmode
163 self.plainmode = plainmode
164
164
165 def setuser(self, user):
165 def setuser(self, user):
166 if not self.updateheader(['From: ', '# User '], user):
166 if not self.updateheader(['From: ', '# User '], user):
167 try:
167 try:
168 patchheaderat = self.comments.index('# HG changeset patch')
168 patchheaderat = self.comments.index('# HG changeset patch')
169 self.comments.insert(patchheaderat + 1, '# User ' + user)
169 self.comments.insert(patchheaderat + 1, '# User ' + user)
170 except ValueError:
170 except ValueError:
171 if self.plainmode or self._hasheader(['Date: ']):
171 if self.plainmode or self._hasheader(['Date: ']):
172 self.comments = ['From: ' + user] + self.comments
172 self.comments = ['From: ' + user] + self.comments
173 else:
173 else:
174 tmp = ['# HG changeset patch', '# User ' + user, '']
174 tmp = ['# HG changeset patch', '# User ' + user, '']
175 self.comments = tmp + self.comments
175 self.comments = tmp + self.comments
176 self.user = user
176 self.user = user
177
177
178 def setdate(self, date):
178 def setdate(self, date):
179 if not self.updateheader(['Date: ', '# Date '], date):
179 if not self.updateheader(['Date: ', '# Date '], date):
180 try:
180 try:
181 patchheaderat = self.comments.index('# HG changeset patch')
181 patchheaderat = self.comments.index('# HG changeset patch')
182 self.comments.insert(patchheaderat + 1, '# Date ' + date)
182 self.comments.insert(patchheaderat + 1, '# Date ' + date)
183 except ValueError:
183 except ValueError:
184 if self.plainmode or self._hasheader(['From: ']):
184 if self.plainmode or self._hasheader(['From: ']):
185 self.comments = ['Date: ' + date] + self.comments
185 self.comments = ['Date: ' + date] + self.comments
186 else:
186 else:
187 tmp = ['# HG changeset patch', '# Date ' + date, '']
187 tmp = ['# HG changeset patch', '# Date ' + date, '']
188 self.comments = tmp + self.comments
188 self.comments = tmp + self.comments
189 self.date = date
189 self.date = date
190
190
191 def setparent(self, parent):
191 def setparent(self, parent):
192 if not self.updateheader(['# Parent '], parent):
192 if not self.updateheader(['# Parent '], parent):
193 try:
193 try:
194 patchheaderat = self.comments.index('# HG changeset patch')
194 patchheaderat = self.comments.index('# HG changeset patch')
195 self.comments.insert(patchheaderat + 1, '# Parent ' + parent)
195 self.comments.insert(patchheaderat + 1, '# Parent ' + parent)
196 except ValueError:
196 except ValueError:
197 pass
197 pass
198 self.parent = parent
198 self.parent = parent
199
199
200 def setmessage(self, message):
200 def setmessage(self, message):
201 if self.comments:
201 if self.comments:
202 self._delmsg()
202 self._delmsg()
203 self.message = [message]
203 self.message = [message]
204 self.comments += self.message
204 self.comments += self.message
205
205
206 def updateheader(self, prefixes, new):
206 def updateheader(self, prefixes, new):
207 '''Update all references to a field in the patch header.
207 '''Update all references to a field in the patch header.
208 Return whether the field is present.'''
208 Return whether the field is present.'''
209 res = False
209 res = False
210 for prefix in prefixes:
210 for prefix in prefixes:
211 for i in xrange(len(self.comments)):
211 for i in xrange(len(self.comments)):
212 if self.comments[i].startswith(prefix):
212 if self.comments[i].startswith(prefix):
213 self.comments[i] = prefix + new
213 self.comments[i] = prefix + new
214 res = True
214 res = True
215 break
215 break
216 return res
216 return res
217
217
218 def _hasheader(self, prefixes):
218 def _hasheader(self, prefixes):
219 '''Check if a header starts with any of the given prefixes.'''
219 '''Check if a header starts with any of the given prefixes.'''
220 for prefix in prefixes:
220 for prefix in prefixes:
221 for comment in self.comments:
221 for comment in self.comments:
222 if comment.startswith(prefix):
222 if comment.startswith(prefix):
223 return True
223 return True
224 return False
224 return False
225
225
226 def __str__(self):
226 def __str__(self):
227 if not self.comments:
227 if not self.comments:
228 return ''
228 return ''
229 return '\n'.join(self.comments) + '\n\n'
229 return '\n'.join(self.comments) + '\n\n'
230
230
231 def _delmsg(self):
231 def _delmsg(self):
232 '''Remove existing message, keeping the rest of the comments fields.
232 '''Remove existing message, keeping the rest of the comments fields.
233 If comments contains 'subject: ', message will prepend
233 If comments contains 'subject: ', message will prepend
234 the field and a blank line.'''
234 the field and a blank line.'''
235 if self.message:
235 if self.message:
236 subj = 'subject: ' + self.message[0].lower()
236 subj = 'subject: ' + self.message[0].lower()
237 for i in xrange(len(self.comments)):
237 for i in xrange(len(self.comments)):
238 if subj == self.comments[i].lower():
238 if subj == self.comments[i].lower():
239 del self.comments[i]
239 del self.comments[i]
240 self.message = self.message[2:]
240 self.message = self.message[2:]
241 break
241 break
242 ci = 0
242 ci = 0
243 for mi in self.message:
243 for mi in self.message:
244 while mi != self.comments[ci]:
244 while mi != self.comments[ci]:
245 ci += 1
245 ci += 1
246 del self.comments[ci]
246 del self.comments[ci]
247
247
248 class queue(object):
248 class queue(object):
249 def __init__(self, ui, path, patchdir=None):
249 def __init__(self, ui, path, patchdir=None):
250 self.basepath = path
250 self.basepath = path
251 try:
251 try:
252 fh = open(os.path.join(path, 'patches.queue'))
252 fh = open(os.path.join(path, 'patches.queue'))
253 cur = fh.read().rstrip()
253 cur = fh.read().rstrip()
254 fh.close()
254 fh.close()
255 if not cur:
255 if not cur:
256 curpath = os.path.join(path, 'patches')
256 curpath = os.path.join(path, 'patches')
257 else:
257 else:
258 curpath = os.path.join(path, 'patches-' + cur)
258 curpath = os.path.join(path, 'patches-' + cur)
259 except IOError:
259 except IOError:
260 curpath = os.path.join(path, 'patches')
260 curpath = os.path.join(path, 'patches')
261 self.path = patchdir or curpath
261 self.path = patchdir or curpath
262 self.opener = scmutil.opener(self.path)
262 self.opener = scmutil.opener(self.path)
263 self.ui = ui
263 self.ui = ui
264 self.applied_dirty = 0
264 self.applied_dirty = 0
265 self.series_dirty = 0
265 self.series_dirty = 0
266 self.added = []
266 self.added = []
267 self.series_path = "series"
267 self.series_path = "series"
268 self.status_path = "status"
268 self.status_path = "status"
269 self.guards_path = "guards"
269 self.guards_path = "guards"
270 self.active_guards = None
270 self.active_guards = None
271 self.guards_dirty = False
271 self.guards_dirty = False
272 # Handle mq.git as a bool with extended values
272 # Handle mq.git as a bool with extended values
273 try:
273 try:
274 gitmode = ui.configbool('mq', 'git', None)
274 gitmode = ui.configbool('mq', 'git', None)
275 if gitmode is None:
275 if gitmode is None:
276 raise error.ConfigError()
276 raise error.ConfigError()
277 self.gitmode = gitmode and 'yes' or 'no'
277 self.gitmode = gitmode and 'yes' or 'no'
278 except error.ConfigError:
278 except error.ConfigError:
279 self.gitmode = ui.config('mq', 'git', 'auto').lower()
279 self.gitmode = ui.config('mq', 'git', 'auto').lower()
280 self.plainmode = ui.configbool('mq', 'plain', False)
280 self.plainmode = ui.configbool('mq', 'plain', False)
281
281
282 @util.propertycache
282 @util.propertycache
283 def applied(self):
283 def applied(self):
284 if os.path.exists(self.join(self.status_path)):
284 if os.path.exists(self.join(self.status_path)):
285 def parselines(lines):
285 def parselines(lines):
286 for l in lines:
286 for l in lines:
287 entry = l.split(':', 1)
287 entry = l.split(':', 1)
288 if len(entry) > 1:
288 if len(entry) > 1:
289 n, name = entry
289 n, name = entry
290 yield statusentry(bin(n), name)
290 yield statusentry(bin(n), name)
291 elif l.strip():
291 elif l.strip():
292 self.ui.warn(_('malformated mq status line: %s\n') % entry)
292 self.ui.warn(_('malformated mq status line: %s\n') % entry)
293 # else we ignore empty lines
293 # else we ignore empty lines
294 lines = self.opener(self.status_path).read().splitlines()
294 lines = self.opener(self.status_path).read().splitlines()
295 return list(parselines(lines))
295 return list(parselines(lines))
296 return []
296 return []
297
297
298 @util.propertycache
298 @util.propertycache
299 def full_series(self):
299 def full_series(self):
300 if os.path.exists(self.join(self.series_path)):
300 if os.path.exists(self.join(self.series_path)):
301 return self.opener(self.series_path).read().splitlines()
301 return self.opener(self.series_path).read().splitlines()
302 return []
302 return []
303
303
304 @util.propertycache
304 @util.propertycache
305 def series(self):
305 def series(self):
306 self.parse_series()
306 self.parse_series()
307 return self.series
307 return self.series
308
308
309 @util.propertycache
309 @util.propertycache
310 def series_guards(self):
310 def series_guards(self):
311 self.parse_series()
311 self.parse_series()
312 return self.series_guards
312 return self.series_guards
313
313
314 def invalidate(self):
314 def invalidate(self):
315 for a in 'applied full_series series series_guards'.split():
315 for a in 'applied full_series series series_guards'.split():
316 if a in self.__dict__:
316 if a in self.__dict__:
317 delattr(self, a)
317 delattr(self, a)
318 self.applied_dirty = 0
318 self.applied_dirty = 0
319 self.series_dirty = 0
319 self.series_dirty = 0
320 self.guards_dirty = False
320 self.guards_dirty = False
321 self.active_guards = None
321 self.active_guards = None
322
322
323 def diffopts(self, opts={}, patchfn=None):
323 def diffopts(self, opts={}, patchfn=None):
324 diffopts = patch.diffopts(self.ui, opts)
324 diffopts = patch.diffopts(self.ui, opts)
325 if self.gitmode == 'auto':
325 if self.gitmode == 'auto':
326 diffopts.upgrade = True
326 diffopts.upgrade = True
327 elif self.gitmode == 'keep':
327 elif self.gitmode == 'keep':
328 pass
328 pass
329 elif self.gitmode in ('yes', 'no'):
329 elif self.gitmode in ('yes', 'no'):
330 diffopts.git = self.gitmode == 'yes'
330 diffopts.git = self.gitmode == 'yes'
331 else:
331 else:
332 raise util.Abort(_('mq.git option can be auto/keep/yes/no'
332 raise util.Abort(_('mq.git option can be auto/keep/yes/no'
333 ' got %s') % self.gitmode)
333 ' got %s') % self.gitmode)
334 if patchfn:
334 if patchfn:
335 diffopts = self.patchopts(diffopts, patchfn)
335 diffopts = self.patchopts(diffopts, patchfn)
336 return diffopts
336 return diffopts
337
337
338 def patchopts(self, diffopts, *patches):
338 def patchopts(self, diffopts, *patches):
339 """Return a copy of input diff options with git set to true if
339 """Return a copy of input diff options with git set to true if
340 referenced patch is a git patch and should be preserved as such.
340 referenced patch is a git patch and should be preserved as such.
341 """
341 """
342 diffopts = diffopts.copy()
342 diffopts = diffopts.copy()
343 if not diffopts.git and self.gitmode == 'keep':
343 if not diffopts.git and self.gitmode == 'keep':
344 for patchfn in patches:
344 for patchfn in patches:
345 patchf = self.opener(patchfn, 'r')
345 patchf = self.opener(patchfn, 'r')
346 # if the patch was a git patch, refresh it as a git patch
346 # if the patch was a git patch, refresh it as a git patch
347 for line in patchf:
347 for line in patchf:
348 if line.startswith('diff --git'):
348 if line.startswith('diff --git'):
349 diffopts.git = True
349 diffopts.git = True
350 break
350 break
351 patchf.close()
351 patchf.close()
352 return diffopts
352 return diffopts
353
353
354 def join(self, *p):
354 def join(self, *p):
355 return os.path.join(self.path, *p)
355 return os.path.join(self.path, *p)
356
356
357 def find_series(self, patch):
357 def find_series(self, patch):
358 def matchpatch(l):
358 def matchpatch(l):
359 l = l.split('#', 1)[0]
359 l = l.split('#', 1)[0]
360 return l.strip() == patch
360 return l.strip() == patch
361 for index, l in enumerate(self.full_series):
361 for index, l in enumerate(self.full_series):
362 if matchpatch(l):
362 if matchpatch(l):
363 return index
363 return index
364 return None
364 return None
365
365
366 guard_re = re.compile(r'\s?#([-+][^-+# \t\r\n\f][^# \t\r\n\f]*)')
366 guard_re = re.compile(r'\s?#([-+][^-+# \t\r\n\f][^# \t\r\n\f]*)')
367
367
368 def parse_series(self):
368 def parse_series(self):
369 self.series = []
369 self.series = []
370 self.series_guards = []
370 self.series_guards = []
371 for l in self.full_series:
371 for l in self.full_series:
372 h = l.find('#')
372 h = l.find('#')
373 if h == -1:
373 if h == -1:
374 patch = l
374 patch = l
375 comment = ''
375 comment = ''
376 elif h == 0:
376 elif h == 0:
377 continue
377 continue
378 else:
378 else:
379 patch = l[:h]
379 patch = l[:h]
380 comment = l[h:]
380 comment = l[h:]
381 patch = patch.strip()
381 patch = patch.strip()
382 if patch:
382 if patch:
383 if patch in self.series:
383 if patch in self.series:
384 raise util.Abort(_('%s appears more than once in %s') %
384 raise util.Abort(_('%s appears more than once in %s') %
385 (patch, self.join(self.series_path)))
385 (patch, self.join(self.series_path)))
386 self.series.append(patch)
386 self.series.append(patch)
387 self.series_guards.append(self.guard_re.findall(comment))
387 self.series_guards.append(self.guard_re.findall(comment))
388
388
389 def check_guard(self, guard):
389 def check_guard(self, guard):
390 if not guard:
390 if not guard:
391 return _('guard cannot be an empty string')
391 return _('guard cannot be an empty string')
392 bad_chars = '# \t\r\n\f'
392 bad_chars = '# \t\r\n\f'
393 first = guard[0]
393 first = guard[0]
394 if first in '-+':
394 if first in '-+':
395 return (_('guard %r starts with invalid character: %r') %
395 return (_('guard %r starts with invalid character: %r') %
396 (guard, first))
396 (guard, first))
397 for c in bad_chars:
397 for c in bad_chars:
398 if c in guard:
398 if c in guard:
399 return _('invalid character in guard %r: %r') % (guard, c)
399 return _('invalid character in guard %r: %r') % (guard, c)
400
400
401 def set_active(self, guards):
401 def set_active(self, guards):
402 for guard in guards:
402 for guard in guards:
403 bad = self.check_guard(guard)
403 bad = self.check_guard(guard)
404 if bad:
404 if bad:
405 raise util.Abort(bad)
405 raise util.Abort(bad)
406 guards = sorted(set(guards))
406 guards = sorted(set(guards))
407 self.ui.debug('active guards: %s\n' % ' '.join(guards))
407 self.ui.debug('active guards: %s\n' % ' '.join(guards))
408 self.active_guards = guards
408 self.active_guards = guards
409 self.guards_dirty = True
409 self.guards_dirty = True
410
410
411 def active(self):
411 def active(self):
412 if self.active_guards is None:
412 if self.active_guards is None:
413 self.active_guards = []
413 self.active_guards = []
414 try:
414 try:
415 guards = self.opener(self.guards_path).read().split()
415 guards = self.opener(self.guards_path).read().split()
416 except IOError, err:
416 except IOError, err:
417 if err.errno != errno.ENOENT:
417 if err.errno != errno.ENOENT:
418 raise
418 raise
419 guards = []
419 guards = []
420 for i, guard in enumerate(guards):
420 for i, guard in enumerate(guards):
421 bad = self.check_guard(guard)
421 bad = self.check_guard(guard)
422 if bad:
422 if bad:
423 self.ui.warn('%s:%d: %s\n' %
423 self.ui.warn('%s:%d: %s\n' %
424 (self.join(self.guards_path), i + 1, bad))
424 (self.join(self.guards_path), i + 1, bad))
425 else:
425 else:
426 self.active_guards.append(guard)
426 self.active_guards.append(guard)
427 return self.active_guards
427 return self.active_guards
428
428
429 def set_guards(self, idx, guards):
429 def set_guards(self, idx, guards):
430 for g in guards:
430 for g in guards:
431 if len(g) < 2:
431 if len(g) < 2:
432 raise util.Abort(_('guard %r too short') % g)
432 raise util.Abort(_('guard %r too short') % g)
433 if g[0] not in '-+':
433 if g[0] not in '-+':
434 raise util.Abort(_('guard %r starts with invalid char') % g)
434 raise util.Abort(_('guard %r starts with invalid char') % g)
435 bad = self.check_guard(g[1:])
435 bad = self.check_guard(g[1:])
436 if bad:
436 if bad:
437 raise util.Abort(bad)
437 raise util.Abort(bad)
438 drop = self.guard_re.sub('', self.full_series[idx])
438 drop = self.guard_re.sub('', self.full_series[idx])
439 self.full_series[idx] = drop + ''.join([' #' + g for g in guards])
439 self.full_series[idx] = drop + ''.join([' #' + g for g in guards])
440 self.parse_series()
440 self.parse_series()
441 self.series_dirty = True
441 self.series_dirty = True
442
442
443 def pushable(self, idx):
443 def pushable(self, idx):
444 if isinstance(idx, str):
444 if isinstance(idx, str):
445 idx = self.series.index(idx)
445 idx = self.series.index(idx)
446 patchguards = self.series_guards[idx]
446 patchguards = self.series_guards[idx]
447 if not patchguards:
447 if not patchguards:
448 return True, None
448 return True, None
449 guards = self.active()
449 guards = self.active()
450 exactneg = [g for g in patchguards if g[0] == '-' and g[1:] in guards]
450 exactneg = [g for g in patchguards if g[0] == '-' and g[1:] in guards]
451 if exactneg:
451 if exactneg:
452 return False, exactneg[0]
452 return False, exactneg[0]
453 pos = [g for g in patchguards if g[0] == '+']
453 pos = [g for g in patchguards if g[0] == '+']
454 exactpos = [g for g in pos if g[1:] in guards]
454 exactpos = [g for g in pos if g[1:] in guards]
455 if pos:
455 if pos:
456 if exactpos:
456 if exactpos:
457 return True, exactpos[0]
457 return True, exactpos[0]
458 return False, pos
458 return False, pos
459 return True, ''
459 return True, ''
460
460
461 def explain_pushable(self, idx, all_patches=False):
461 def explain_pushable(self, idx, all_patches=False):
462 write = all_patches and self.ui.write or self.ui.warn
462 write = all_patches and self.ui.write or self.ui.warn
463 if all_patches or self.ui.verbose:
463 if all_patches or self.ui.verbose:
464 if isinstance(idx, str):
464 if isinstance(idx, str):
465 idx = self.series.index(idx)
465 idx = self.series.index(idx)
466 pushable, why = self.pushable(idx)
466 pushable, why = self.pushable(idx)
467 if all_patches and pushable:
467 if all_patches and pushable:
468 if why is None:
468 if why is None:
469 write(_('allowing %s - no guards in effect\n') %
469 write(_('allowing %s - no guards in effect\n') %
470 self.series[idx])
470 self.series[idx])
471 else:
471 else:
472 if not why:
472 if not why:
473 write(_('allowing %s - no matching negative guards\n') %
473 write(_('allowing %s - no matching negative guards\n') %
474 self.series[idx])
474 self.series[idx])
475 else:
475 else:
476 write(_('allowing %s - guarded by %r\n') %
476 write(_('allowing %s - guarded by %r\n') %
477 (self.series[idx], why))
477 (self.series[idx], why))
478 if not pushable:
478 if not pushable:
479 if why:
479 if why:
480 write(_('skipping %s - guarded by %r\n') %
480 write(_('skipping %s - guarded by %r\n') %
481 (self.series[idx], why))
481 (self.series[idx], why))
482 else:
482 else:
483 write(_('skipping %s - no matching guards\n') %
483 write(_('skipping %s - no matching guards\n') %
484 self.series[idx])
484 self.series[idx])
485
485
486 def save_dirty(self):
486 def save_dirty(self):
487 def write_list(items, path):
487 def write_list(items, path):
488 fp = self.opener(path, 'w')
488 fp = self.opener(path, 'w')
489 for i in items:
489 for i in items:
490 fp.write("%s\n" % i)
490 fp.write("%s\n" % i)
491 fp.close()
491 fp.close()
492 if self.applied_dirty:
492 if self.applied_dirty:
493 write_list(map(str, self.applied), self.status_path)
493 write_list(map(str, self.applied), self.status_path)
494 if self.series_dirty:
494 if self.series_dirty:
495 write_list(self.full_series, self.series_path)
495 write_list(self.full_series, self.series_path)
496 if self.guards_dirty:
496 if self.guards_dirty:
497 write_list(self.active_guards, self.guards_path)
497 write_list(self.active_guards, self.guards_path)
498 if self.added:
498 if self.added:
499 qrepo = self.qrepo()
499 qrepo = self.qrepo()
500 if qrepo:
500 if qrepo:
501 qrepo[None].add(f for f in self.added if f not in qrepo[None])
501 qrepo[None].add(f for f in self.added if f not in qrepo[None])
502 self.added = []
502 self.added = []
503
503
504 def removeundo(self, repo):
504 def removeundo(self, repo):
505 undo = repo.sjoin('undo')
505 undo = repo.sjoin('undo')
506 if not os.path.exists(undo):
506 if not os.path.exists(undo):
507 return
507 return
508 try:
508 try:
509 os.unlink(undo)
509 os.unlink(undo)
510 except OSError, inst:
510 except OSError, inst:
511 self.ui.warn(_('error removing undo: %s\n') % str(inst))
511 self.ui.warn(_('error removing undo: %s\n') % str(inst))
512
512
513 def printdiff(self, repo, diffopts, node1, node2=None, files=None,
513 def printdiff(self, repo, diffopts, node1, node2=None, files=None,
514 fp=None, changes=None, opts={}):
514 fp=None, changes=None, opts={}):
515 stat = opts.get('stat')
515 stat = opts.get('stat')
516 m = cmdutil.match(repo, files, opts)
516 m = cmdutil.match(repo, files, opts)
517 cmdutil.diffordiffstat(self.ui, repo, diffopts, node1, node2, m,
517 cmdutil.diffordiffstat(self.ui, repo, diffopts, node1, node2, m,
518 changes, stat, fp)
518 changes, stat, fp)
519
519
520 def mergeone(self, repo, mergeq, head, patch, rev, diffopts):
520 def mergeone(self, repo, mergeq, head, patch, rev, diffopts):
521 # first try just applying the patch
521 # first try just applying the patch
522 (err, n) = self.apply(repo, [patch], update_status=False,
522 (err, n) = self.apply(repo, [patch], update_status=False,
523 strict=True, merge=rev)
523 strict=True, merge=rev)
524
524
525 if err == 0:
525 if err == 0:
526 return (err, n)
526 return (err, n)
527
527
528 if n is None:
528 if n is None:
529 raise util.Abort(_("apply failed for patch %s") % patch)
529 raise util.Abort(_("apply failed for patch %s") % patch)
530
530
531 self.ui.warn(_("patch didn't work out, merging %s\n") % patch)
531 self.ui.warn(_("patch didn't work out, merging %s\n") % patch)
532
532
533 # apply failed, strip away that rev and merge.
533 # apply failed, strip away that rev and merge.
534 hg.clean(repo, head)
534 hg.clean(repo, head)
535 self.strip(repo, [n], update=False, backup='strip')
535 self.strip(repo, [n], update=False, backup='strip')
536
536
537 ctx = repo[rev]
537 ctx = repo[rev]
538 ret = hg.merge(repo, rev)
538 ret = hg.merge(repo, rev)
539 if ret:
539 if ret:
540 raise util.Abort(_("update returned %d") % ret)
540 raise util.Abort(_("update returned %d") % ret)
541 n = repo.commit(ctx.description(), ctx.user(), force=True)
541 n = repo.commit(ctx.description(), ctx.user(), force=True)
542 if n is None:
542 if n is None:
543 raise util.Abort(_("repo commit failed"))
543 raise util.Abort(_("repo commit failed"))
544 try:
544 try:
545 ph = patchheader(mergeq.join(patch), self.plainmode)
545 ph = patchheader(mergeq.join(patch), self.plainmode)
546 except:
546 except:
547 raise util.Abort(_("unable to read %s") % patch)
547 raise util.Abort(_("unable to read %s") % patch)
548
548
549 diffopts = self.patchopts(diffopts, patch)
549 diffopts = self.patchopts(diffopts, patch)
550 patchf = self.opener(patch, "w")
550 patchf = self.opener(patch, "w")
551 comments = str(ph)
551 comments = str(ph)
552 if comments:
552 if comments:
553 patchf.write(comments)
553 patchf.write(comments)
554 self.printdiff(repo, diffopts, head, n, fp=patchf)
554 self.printdiff(repo, diffopts, head, n, fp=patchf)
555 patchf.close()
555 patchf.close()
556 self.removeundo(repo)
556 self.removeundo(repo)
557 return (0, n)
557 return (0, n)
558
558
559 def qparents(self, repo, rev=None):
559 def qparents(self, repo, rev=None):
560 if rev is None:
560 if rev is None:
561 (p1, p2) = repo.dirstate.parents()
561 (p1, p2) = repo.dirstate.parents()
562 if p2 == nullid:
562 if p2 == nullid:
563 return p1
563 return p1
564 if not self.applied:
564 if not self.applied:
565 return None
565 return None
566 return self.applied[-1].node
566 return self.applied[-1].node
567 p1, p2 = repo.changelog.parents(rev)
567 p1, p2 = repo.changelog.parents(rev)
568 if p2 != nullid and p2 in [x.node for x in self.applied]:
568 if p2 != nullid and p2 in [x.node for x in self.applied]:
569 return p2
569 return p2
570 return p1
570 return p1
571
571
572 def mergepatch(self, repo, mergeq, series, diffopts):
572 def mergepatch(self, repo, mergeq, series, diffopts):
573 if not self.applied:
573 if not self.applied:
574 # each of the patches merged in will have two parents. This
574 # each of the patches merged in will have two parents. This
575 # can confuse the qrefresh, qdiff, and strip code because it
575 # can confuse the qrefresh, qdiff, and strip code because it
576 # needs to know which parent is actually in the patch queue.
576 # needs to know which parent is actually in the patch queue.
577 # so, we insert a merge marker with only one parent. This way
577 # so, we insert a merge marker with only one parent. This way
578 # the first patch in the queue is never a merge patch
578 # the first patch in the queue is never a merge patch
579 #
579 #
580 pname = ".hg.patches.merge.marker"
580 pname = ".hg.patches.merge.marker"
581 n = repo.commit('[mq]: merge marker', force=True)
581 n = repo.commit('[mq]: merge marker', force=True)
582 self.removeundo(repo)
582 self.removeundo(repo)
583 self.applied.append(statusentry(n, pname))
583 self.applied.append(statusentry(n, pname))
584 self.applied_dirty = 1
584 self.applied_dirty = 1
585
585
586 head = self.qparents(repo)
586 head = self.qparents(repo)
587
587
588 for patch in series:
588 for patch in series:
589 patch = mergeq.lookup(patch, strict=True)
589 patch = mergeq.lookup(patch, strict=True)
590 if not patch:
590 if not patch:
591 self.ui.warn(_("patch %s does not exist\n") % patch)
591 self.ui.warn(_("patch %s does not exist\n") % patch)
592 return (1, None)
592 return (1, None)
593 pushable, reason = self.pushable(patch)
593 pushable, reason = self.pushable(patch)
594 if not pushable:
594 if not pushable:
595 self.explain_pushable(patch, all_patches=True)
595 self.explain_pushable(patch, all_patches=True)
596 continue
596 continue
597 info = mergeq.isapplied(patch)
597 info = mergeq.isapplied(patch)
598 if not info:
598 if not info:
599 self.ui.warn(_("patch %s is not applied\n") % patch)
599 self.ui.warn(_("patch %s is not applied\n") % patch)
600 return (1, None)
600 return (1, None)
601 rev = info[1]
601 rev = info[1]
602 err, head = self.mergeone(repo, mergeq, head, patch, rev, diffopts)
602 err, head = self.mergeone(repo, mergeq, head, patch, rev, diffopts)
603 if head:
603 if head:
604 self.applied.append(statusentry(head, patch))
604 self.applied.append(statusentry(head, patch))
605 self.applied_dirty = 1
605 self.applied_dirty = 1
606 if err:
606 if err:
607 return (err, head)
607 return (err, head)
608 self.save_dirty()
608 self.save_dirty()
609 return (0, head)
609 return (0, head)
610
610
611 def patch(self, repo, patchfile):
611 def patch(self, repo, patchfile):
612 '''Apply patchfile to the working directory.
612 '''Apply patchfile to the working directory.
613 patchfile: name of patch file'''
613 patchfile: name of patch file'''
614 files = {}
614 files = {}
615 try:
615 try:
616 fuzz = patch.patch(patchfile, self.ui, strip=1, cwd=repo.root,
616 fuzz = patch.patch(patchfile, self.ui, strip=1, cwd=repo.root,
617 files=files, eolmode=None)
617 files=files, eolmode=None)
618 except Exception, inst:
618 except Exception, inst:
619 self.ui.note(str(inst) + '\n')
619 self.ui.note(str(inst) + '\n')
620 if not self.ui.verbose:
620 if not self.ui.verbose:
621 self.ui.warn(_("patch failed, unable to continue (try -v)\n"))
621 self.ui.warn(_("patch failed, unable to continue (try -v)\n"))
622 return (False, files, False)
622 return (False, files, False)
623
623
624 return (True, files, fuzz)
624 return (True, files, fuzz)
625
625
626 def apply(self, repo, series, list=False, update_status=True,
626 def apply(self, repo, series, list=False, update_status=True,
627 strict=False, patchdir=None, merge=None, all_files=None):
627 strict=False, patchdir=None, merge=None, all_files=None):
628 wlock = lock = tr = None
628 wlock = lock = tr = None
629 try:
629 try:
630 wlock = repo.wlock()
630 wlock = repo.wlock()
631 lock = repo.lock()
631 lock = repo.lock()
632 tr = repo.transaction("qpush")
632 tr = repo.transaction("qpush")
633 try:
633 try:
634 ret = self._apply(repo, series, list, update_status,
634 ret = self._apply(repo, series, list, update_status,
635 strict, patchdir, merge, all_files=all_files)
635 strict, patchdir, merge, all_files=all_files)
636 tr.close()
636 tr.close()
637 self.save_dirty()
637 self.save_dirty()
638 return ret
638 return ret
639 except:
639 except:
640 try:
640 try:
641 tr.abort()
641 tr.abort()
642 finally:
642 finally:
643 repo.invalidate()
643 repo.invalidate()
644 repo.dirstate.invalidate()
644 repo.dirstate.invalidate()
645 raise
645 raise
646 finally:
646 finally:
647 release(tr, lock, wlock)
647 release(tr, lock, wlock)
648 self.removeundo(repo)
648 self.removeundo(repo)
649
649
650 def _apply(self, repo, series, list=False, update_status=True,
650 def _apply(self, repo, series, list=False, update_status=True,
651 strict=False, patchdir=None, merge=None, all_files=None):
651 strict=False, patchdir=None, merge=None, all_files=None):
652 '''returns (error, hash)
652 '''returns (error, hash)
653 error = 1 for unable to read, 2 for patch failed, 3 for patch fuzz'''
653 error = 1 for unable to read, 2 for patch failed, 3 for patch fuzz'''
654 # TODO unify with commands.py
654 # TODO unify with commands.py
655 if not patchdir:
655 if not patchdir:
656 patchdir = self.path
656 patchdir = self.path
657 err = 0
657 err = 0
658 n = None
658 n = None
659 for patchname in series:
659 for patchname in series:
660 pushable, reason = self.pushable(patchname)
660 pushable, reason = self.pushable(patchname)
661 if not pushable:
661 if not pushable:
662 self.explain_pushable(patchname, all_patches=True)
662 self.explain_pushable(patchname, all_patches=True)
663 continue
663 continue
664 self.ui.status(_("applying %s\n") % patchname)
664 self.ui.status(_("applying %s\n") % patchname)
665 pf = os.path.join(patchdir, patchname)
665 pf = os.path.join(patchdir, patchname)
666
666
667 try:
667 try:
668 ph = patchheader(self.join(patchname), self.plainmode)
668 ph = patchheader(self.join(patchname), self.plainmode)
669 except:
669 except:
670 self.ui.warn(_("unable to read %s\n") % patchname)
670 self.ui.warn(_("unable to read %s\n") % patchname)
671 err = 1
671 err = 1
672 break
672 break
673
673
674 message = ph.message
674 message = ph.message
675 if not message:
675 if not message:
676 # The commit message should not be translated
676 # The commit message should not be translated
677 message = "imported patch %s\n" % patchname
677 message = "imported patch %s\n" % patchname
678 else:
678 else:
679 if list:
679 if list:
680 # The commit message should not be translated
680 # The commit message should not be translated
681 message.append("\nimported patch %s" % patchname)
681 message.append("\nimported patch %s" % patchname)
682 message = '\n'.join(message)
682 message = '\n'.join(message)
683
683
684 if ph.haspatch:
684 if ph.haspatch:
685 (patcherr, files, fuzz) = self.patch(repo, pf)
685 (patcherr, files, fuzz) = self.patch(repo, pf)
686 if all_files is not None:
686 if all_files is not None:
687 all_files.update(files)
687 all_files.update(files)
688 patcherr = not patcherr
688 patcherr = not patcherr
689 else:
689 else:
690 self.ui.warn(_("patch %s is empty\n") % patchname)
690 self.ui.warn(_("patch %s is empty\n") % patchname)
691 patcherr, files, fuzz = 0, [], 0
691 patcherr, files, fuzz = 0, [], 0
692
692
693 if merge and files:
693 if merge and files:
694 # Mark as removed/merged and update dirstate parent info
694 # Mark as removed/merged and update dirstate parent info
695 removed = []
695 removed = []
696 merged = []
696 merged = []
697 for f in files:
697 for f in files:
698 if os.path.lexists(repo.wjoin(f)):
698 if os.path.lexists(repo.wjoin(f)):
699 merged.append(f)
699 merged.append(f)
700 else:
700 else:
701 removed.append(f)
701 removed.append(f)
702 for f in removed:
702 for f in removed:
703 repo.dirstate.remove(f)
703 repo.dirstate.remove(f)
704 for f in merged:
704 for f in merged:
705 repo.dirstate.merge(f)
705 repo.dirstate.merge(f)
706 p1, p2 = repo.dirstate.parents()
706 p1, p2 = repo.dirstate.parents()
707 repo.dirstate.setparents(p1, merge)
707 repo.dirstate.setparents(p1, merge)
708
708
709 files = cmdutil.updatedir(self.ui, repo, files)
709 files = cmdutil.updatedir(self.ui, repo, files)
710 match = cmdutil.matchfiles(repo, files or [])
710 match = cmdutil.matchfiles(repo, files or [])
711 n = repo.commit(message, ph.user, ph.date, match=match, force=True)
711 n = repo.commit(message, ph.user, ph.date, match=match, force=True)
712
712
713 if n is None:
713 if n is None:
714 raise util.Abort(_("repository commit failed"))
714 raise util.Abort(_("repository commit failed"))
715
715
716 if update_status:
716 if update_status:
717 self.applied.append(statusentry(n, patchname))
717 self.applied.append(statusentry(n, patchname))
718
718
719 if patcherr:
719 if patcherr:
720 self.ui.warn(_("patch failed, rejects left in working dir\n"))
720 self.ui.warn(_("patch failed, rejects left in working dir\n"))
721 err = 2
721 err = 2
722 break
722 break
723
723
724 if fuzz and strict:
724 if fuzz and strict:
725 self.ui.warn(_("fuzz found when applying patch, stopping\n"))
725 self.ui.warn(_("fuzz found when applying patch, stopping\n"))
726 err = 3
726 err = 3
727 break
727 break
728 return (err, n)
728 return (err, n)
729
729
730 def _cleanup(self, patches, numrevs, keep=False):
730 def _cleanup(self, patches, numrevs, keep=False):
731 if not keep:
731 if not keep:
732 r = self.qrepo()
732 r = self.qrepo()
733 if r:
733 if r:
734 r[None].remove(patches, True)
734 r[None].remove(patches, True)
735 else:
735 else:
736 for p in patches:
736 for p in patches:
737 os.unlink(self.join(p))
737 os.unlink(self.join(p))
738
738
739 if numrevs:
739 if numrevs:
740 qfinished = self.applied[:numrevs]
740 qfinished = self.applied[:numrevs]
741 del self.applied[:numrevs]
741 del self.applied[:numrevs]
742 self.applied_dirty = 1
742 self.applied_dirty = 1
743
743
744 unknown = []
744 unknown = []
745
745
746 for (i, p) in sorted([(self.find_series(p), p) for p in patches],
746 for (i, p) in sorted([(self.find_series(p), p) for p in patches],
747 reverse=True):
747 reverse=True):
748 if i is not None:
748 if i is not None:
749 del self.full_series[i]
749 del self.full_series[i]
750 else:
750 else:
751 unknown.append(p)
751 unknown.append(p)
752
752
753 if unknown:
753 if unknown:
754 if numrevs:
754 if numrevs:
755 rev = dict((entry.name, entry.node) for entry in qfinished)
755 rev = dict((entry.name, entry.node) for entry in qfinished)
756 for p in unknown:
756 for p in unknown:
757 msg = _('revision %s refers to unknown patches: %s\n')
757 msg = _('revision %s refers to unknown patches: %s\n')
758 self.ui.warn(msg % (short(rev[p]), p))
758 self.ui.warn(msg % (short(rev[p]), p))
759 else:
759 else:
760 msg = _('unknown patches: %s\n')
760 msg = _('unknown patches: %s\n')
761 raise util.Abort(''.join(msg % p for p in unknown))
761 raise util.Abort(''.join(msg % p for p in unknown))
762
762
763 self.parse_series()
763 self.parse_series()
764 self.series_dirty = 1
764 self.series_dirty = 1
765
765
766 def _revpatches(self, repo, revs):
766 def _revpatches(self, repo, revs):
767 firstrev = repo[self.applied[0].node].rev()
767 firstrev = repo[self.applied[0].node].rev()
768 patches = []
768 patches = []
769 for i, rev in enumerate(revs):
769 for i, rev in enumerate(revs):
770
770
771 if rev < firstrev:
771 if rev < firstrev:
772 raise util.Abort(_('revision %d is not managed') % rev)
772 raise util.Abort(_('revision %d is not managed') % rev)
773
773
774 ctx = repo[rev]
774 ctx = repo[rev]
775 base = self.applied[i].node
775 base = self.applied[i].node
776 if ctx.node() != base:
776 if ctx.node() != base:
777 msg = _('cannot delete revision %d above applied patches')
777 msg = _('cannot delete revision %d above applied patches')
778 raise util.Abort(msg % rev)
778 raise util.Abort(msg % rev)
779
779
780 patch = self.applied[i].name
780 patch = self.applied[i].name
781 for fmt in ('[mq]: %s', 'imported patch %s'):
781 for fmt in ('[mq]: %s', 'imported patch %s'):
782 if ctx.description() == fmt % patch:
782 if ctx.description() == fmt % patch:
783 msg = _('patch %s finalized without changeset message\n')
783 msg = _('patch %s finalized without changeset message\n')
784 repo.ui.status(msg % patch)
784 repo.ui.status(msg % patch)
785 break
785 break
786
786
787 patches.append(patch)
787 patches.append(patch)
788 return patches
788 return patches
789
789
790 def finish(self, repo, revs):
790 def finish(self, repo, revs):
791 patches = self._revpatches(repo, sorted(revs))
791 patches = self._revpatches(repo, sorted(revs))
792 self._cleanup(patches, len(patches))
792 self._cleanup(patches, len(patches))
793
793
794 def delete(self, repo, patches, opts):
794 def delete(self, repo, patches, opts):
795 if not patches and not opts.get('rev'):
795 if not patches and not opts.get('rev'):
796 raise util.Abort(_('qdelete requires at least one revision or '
796 raise util.Abort(_('qdelete requires at least one revision or '
797 'patch name'))
797 'patch name'))
798
798
799 realpatches = []
799 realpatches = []
800 for patch in patches:
800 for patch in patches:
801 patch = self.lookup(patch, strict=True)
801 patch = self.lookup(patch, strict=True)
802 info = self.isapplied(patch)
802 info = self.isapplied(patch)
803 if info:
803 if info:
804 raise util.Abort(_("cannot delete applied patch %s") % patch)
804 raise util.Abort(_("cannot delete applied patch %s") % patch)
805 if patch not in self.series:
805 if patch not in self.series:
806 raise util.Abort(_("patch %s not in series file") % patch)
806 raise util.Abort(_("patch %s not in series file") % patch)
807 if patch not in realpatches:
807 if patch not in realpatches:
808 realpatches.append(patch)
808 realpatches.append(patch)
809
809
810 numrevs = 0
810 numrevs = 0
811 if opts.get('rev'):
811 if opts.get('rev'):
812 if not self.applied:
812 if not self.applied:
813 raise util.Abort(_('no patches applied'))
813 raise util.Abort(_('no patches applied'))
814 revs = cmdutil.revrange(repo, opts.get('rev'))
814 revs = cmdutil.revrange(repo, opts.get('rev'))
815 if len(revs) > 1 and revs[0] > revs[1]:
815 if len(revs) > 1 and revs[0] > revs[1]:
816 revs.reverse()
816 revs.reverse()
817 revpatches = self._revpatches(repo, revs)
817 revpatches = self._revpatches(repo, revs)
818 realpatches += revpatches
818 realpatches += revpatches
819 numrevs = len(revpatches)
819 numrevs = len(revpatches)
820
820
821 self._cleanup(realpatches, numrevs, opts.get('keep'))
821 self._cleanup(realpatches, numrevs, opts.get('keep'))
822
822
823 def check_toppatch(self, repo):
823 def check_toppatch(self, repo):
824 if self.applied:
824 if self.applied:
825 top = self.applied[-1].node
825 top = self.applied[-1].node
826 patch = self.applied[-1].name
826 patch = self.applied[-1].name
827 pp = repo.dirstate.parents()
827 pp = repo.dirstate.parents()
828 if top not in pp:
828 if top not in pp:
829 raise util.Abort(_("working directory revision is not qtip"))
829 raise util.Abort(_("working directory revision is not qtip"))
830 return top, patch
830 return top, patch
831 return None, None
831 return None, None
832
832
833 def check_substate(self, repo):
833 def check_substate(self, repo):
834 '''return list of subrepos at a different revision than substate.
834 '''return list of subrepos at a different revision than substate.
835 Abort if any subrepos have uncommitted changes.'''
835 Abort if any subrepos have uncommitted changes.'''
836 inclsubs = []
836 inclsubs = []
837 wctx = repo[None]
837 wctx = repo[None]
838 for s in wctx.substate:
838 for s in wctx.substate:
839 if wctx.sub(s).dirty(True):
839 if wctx.sub(s).dirty(True):
840 raise util.Abort(
840 raise util.Abort(
841 _("uncommitted changes in subrepository %s") % s)
841 _("uncommitted changes in subrepository %s") % s)
842 elif wctx.sub(s).dirty():
842 elif wctx.sub(s).dirty():
843 inclsubs.append(s)
843 inclsubs.append(s)
844 return inclsubs
844 return inclsubs
845
845
846 def check_localchanges(self, repo, force=False, refresh=True):
846 def check_localchanges(self, repo, force=False, refresh=True):
847 m, a, r, d = repo.status()[:4]
847 m, a, r, d = repo.status()[:4]
848 if (m or a or r or d) and not force:
848 if (m or a or r or d) and not force:
849 if refresh:
849 if refresh:
850 raise util.Abort(_("local changes found, refresh first"))
850 raise util.Abort(_("local changes found, refresh first"))
851 else:
851 else:
852 raise util.Abort(_("local changes found"))
852 raise util.Abort(_("local changes found"))
853 return m, a, r, d
853 return m, a, r, d
854
854
855 _reserved = ('series', 'status', 'guards')
855 _reserved = ('series', 'status', 'guards')
856 def check_reserved_name(self, name):
856 def check_reserved_name(self, name):
857 if (name in self._reserved or name.startswith('.hg')
857 if (name in self._reserved or name.startswith('.hg')
858 or name.startswith('.mq') or '#' in name or ':' in name):
858 or name.startswith('.mq') or '#' in name or ':' in name):
859 raise util.Abort(_('"%s" cannot be used as the name of a patch')
859 raise util.Abort(_('"%s" cannot be used as the name of a patch')
860 % name)
860 % name)
861
861
862 def new(self, repo, patchfn, *pats, **opts):
862 def new(self, repo, patchfn, *pats, **opts):
863 """options:
863 """options:
864 msg: a string or a no-argument function returning a string
864 msg: a string or a no-argument function returning a string
865 """
865 """
866 msg = opts.get('msg')
866 msg = opts.get('msg')
867 user = opts.get('user')
867 user = opts.get('user')
868 date = opts.get('date')
868 date = opts.get('date')
869 if date:
869 if date:
870 date = util.parsedate(date)
870 date = util.parsedate(date)
871 diffopts = self.diffopts({'git': opts.get('git')})
871 diffopts = self.diffopts({'git': opts.get('git')})
872 self.check_reserved_name(patchfn)
872 self.check_reserved_name(patchfn)
873 if os.path.exists(self.join(patchfn)):
873 if os.path.exists(self.join(patchfn)):
874 if os.path.isdir(self.join(patchfn)):
874 if os.path.isdir(self.join(patchfn)):
875 raise util.Abort(_('"%s" already exists as a directory')
875 raise util.Abort(_('"%s" already exists as a directory')
876 % patchfn)
876 % patchfn)
877 else:
877 else:
878 raise util.Abort(_('patch "%s" already exists') % patchfn)
878 raise util.Abort(_('patch "%s" already exists') % patchfn)
879
879
880 inclsubs = self.check_substate(repo)
880 inclsubs = self.check_substate(repo)
881 if inclsubs:
881 if inclsubs:
882 inclsubs.append('.hgsubstate')
882 inclsubs.append('.hgsubstate')
883 if opts.get('include') or opts.get('exclude') or pats:
883 if opts.get('include') or opts.get('exclude') or pats:
884 if inclsubs:
884 if inclsubs:
885 pats = list(pats or []) + inclsubs
885 pats = list(pats or []) + inclsubs
886 match = cmdutil.match(repo, pats, opts)
886 match = cmdutil.match(repo, pats, opts)
887 # detect missing files in pats
887 # detect missing files in pats
888 def badfn(f, msg):
888 def badfn(f, msg):
889 if f != '.hgsubstate': # .hgsubstate is auto-created
889 if f != '.hgsubstate': # .hgsubstate is auto-created
890 raise util.Abort('%s: %s' % (f, msg))
890 raise util.Abort('%s: %s' % (f, msg))
891 match.bad = badfn
891 match.bad = badfn
892 m, a, r, d = repo.status(match=match)[:4]
892 m, a, r, d = repo.status(match=match)[:4]
893 else:
893 else:
894 m, a, r, d = self.check_localchanges(repo, force=True)
894 m, a, r, d = self.check_localchanges(repo, force=True)
895 match = cmdutil.matchfiles(repo, m + a + r + inclsubs)
895 match = cmdutil.matchfiles(repo, m + a + r + inclsubs)
896 if len(repo[None].parents()) > 1:
896 if len(repo[None].parents()) > 1:
897 raise util.Abort(_('cannot manage merge changesets'))
897 raise util.Abort(_('cannot manage merge changesets'))
898 commitfiles = m + a + r
898 commitfiles = m + a + r
899 self.check_toppatch(repo)
899 self.check_toppatch(repo)
900 insert = self.full_series_end()
900 insert = self.full_series_end()
901 wlock = repo.wlock()
901 wlock = repo.wlock()
902 try:
902 try:
903 try:
903 try:
904 # if patch file write fails, abort early
904 # if patch file write fails, abort early
905 p = self.opener(patchfn, "w")
905 p = self.opener(patchfn, "w")
906 except IOError, e:
906 except IOError, e:
907 raise util.Abort(_('cannot write patch "%s": %s')
907 raise util.Abort(_('cannot write patch "%s": %s')
908 % (patchfn, e.strerror))
908 % (patchfn, e.strerror))
909 try:
909 try:
910 if self.plainmode:
910 if self.plainmode:
911 if user:
911 if user:
912 p.write("From: " + user + "\n")
912 p.write("From: " + user + "\n")
913 if not date:
913 if not date:
914 p.write("\n")
914 p.write("\n")
915 if date:
915 if date:
916 p.write("Date: %d %d\n\n" % date)
916 p.write("Date: %d %d\n\n" % date)
917 else:
917 else:
918 p.write("# HG changeset patch\n")
918 p.write("# HG changeset patch\n")
919 p.write("# Parent "
919 p.write("# Parent "
920 + hex(repo[None].p1().node()) + "\n")
920 + hex(repo[None].p1().node()) + "\n")
921 if user:
921 if user:
922 p.write("# User " + user + "\n")
922 p.write("# User " + user + "\n")
923 if date:
923 if date:
924 p.write("# Date %s %s\n\n" % date)
924 p.write("# Date %s %s\n\n" % date)
925 if hasattr(msg, '__call__'):
925 if hasattr(msg, '__call__'):
926 msg = msg()
926 msg = msg()
927 commitmsg = msg and msg or ("[mq]: %s" % patchfn)
927 commitmsg = msg and msg or ("[mq]: %s" % patchfn)
928 n = repo.commit(commitmsg, user, date, match=match, force=True)
928 n = repo.commit(commitmsg, user, date, match=match, force=True)
929 if n is None:
929 if n is None:
930 raise util.Abort(_("repo commit failed"))
930 raise util.Abort(_("repo commit failed"))
931 try:
931 try:
932 self.full_series[insert:insert] = [patchfn]
932 self.full_series[insert:insert] = [patchfn]
933 self.applied.append(statusentry(n, patchfn))
933 self.applied.append(statusentry(n, patchfn))
934 self.parse_series()
934 self.parse_series()
935 self.series_dirty = 1
935 self.series_dirty = 1
936 self.applied_dirty = 1
936 self.applied_dirty = 1
937 if msg:
937 if msg:
938 msg = msg + "\n\n"
938 msg = msg + "\n\n"
939 p.write(msg)
939 p.write(msg)
940 if commitfiles:
940 if commitfiles:
941 parent = self.qparents(repo, n)
941 parent = self.qparents(repo, n)
942 chunks = patch.diff(repo, node1=parent, node2=n,
942 chunks = patch.diff(repo, node1=parent, node2=n,
943 match=match, opts=diffopts)
943 match=match, opts=diffopts)
944 for chunk in chunks:
944 for chunk in chunks:
945 p.write(chunk)
945 p.write(chunk)
946 p.close()
946 p.close()
947 wlock.release()
947 wlock.release()
948 wlock = None
948 wlock = None
949 r = self.qrepo()
949 r = self.qrepo()
950 if r:
950 if r:
951 r[None].add([patchfn])
951 r[None].add([patchfn])
952 except:
952 except:
953 repo.rollback()
953 repo.rollback()
954 raise
954 raise
955 except Exception:
955 except Exception:
956 patchpath = self.join(patchfn)
956 patchpath = self.join(patchfn)
957 try:
957 try:
958 os.unlink(patchpath)
958 os.unlink(patchpath)
959 except:
959 except:
960 self.ui.warn(_('error unlinking %s\n') % patchpath)
960 self.ui.warn(_('error unlinking %s\n') % patchpath)
961 raise
961 raise
962 self.removeundo(repo)
962 self.removeundo(repo)
963 finally:
963 finally:
964 release(wlock)
964 release(wlock)
965
965
966 def strip(self, repo, revs, update=True, backup="all", force=None):
966 def strip(self, repo, revs, update=True, backup="all", force=None):
967 wlock = lock = None
967 wlock = lock = None
968 try:
968 try:
969 wlock = repo.wlock()
969 wlock = repo.wlock()
970 lock = repo.lock()
970 lock = repo.lock()
971
971
972 if update:
972 if update:
973 self.check_localchanges(repo, force=force, refresh=False)
973 self.check_localchanges(repo, force=force, refresh=False)
974 urev = self.qparents(repo, revs[0])
974 urev = self.qparents(repo, revs[0])
975 hg.clean(repo, urev)
975 hg.clean(repo, urev)
976 repo.dirstate.write()
976 repo.dirstate.write()
977
977
978 self.removeundo(repo)
978 self.removeundo(repo)
979 for rev in revs:
979 for rev in revs:
980 repair.strip(self.ui, repo, rev, backup)
980 repair.strip(self.ui, repo, rev, backup)
981 # strip may have unbundled a set of backed up revisions after
981 # strip may have unbundled a set of backed up revisions after
982 # the actual strip
982 # the actual strip
983 self.removeundo(repo)
983 self.removeundo(repo)
984 finally:
984 finally:
985 release(lock, wlock)
985 release(lock, wlock)
986
986
987 def isapplied(self, patch):
987 def isapplied(self, patch):
988 """returns (index, rev, patch)"""
988 """returns (index, rev, patch)"""
989 for i, a in enumerate(self.applied):
989 for i, a in enumerate(self.applied):
990 if a.name == patch:
990 if a.name == patch:
991 return (i, a.node, a.name)
991 return (i, a.node, a.name)
992 return None
992 return None
993
993
994 # if the exact patch name does not exist, we try a few
994 # if the exact patch name does not exist, we try a few
995 # variations. If strict is passed, we try only #1
995 # variations. If strict is passed, we try only #1
996 #
996 #
997 # 1) a number to indicate an offset in the series file
997 # 1) a number to indicate an offset in the series file
998 # 2) a unique substring of the patch name was given
998 # 2) a unique substring of the patch name was given
999 # 3) patchname[-+]num to indicate an offset in the series file
999 # 3) patchname[-+]num to indicate an offset in the series file
1000 def lookup(self, patch, strict=False):
1000 def lookup(self, patch, strict=False):
1001 patch = patch and str(patch)
1001 patch = patch and str(patch)
1002
1002
1003 def partial_name(s):
1003 def partial_name(s):
1004 if s in self.series:
1004 if s in self.series:
1005 return s
1005 return s
1006 matches = [x for x in self.series if s in x]
1006 matches = [x for x in self.series if s in x]
1007 if len(matches) > 1:
1007 if len(matches) > 1:
1008 self.ui.warn(_('patch name "%s" is ambiguous:\n') % s)
1008 self.ui.warn(_('patch name "%s" is ambiguous:\n') % s)
1009 for m in matches:
1009 for m in matches:
1010 self.ui.warn(' %s\n' % m)
1010 self.ui.warn(' %s\n' % m)
1011 return None
1011 return None
1012 if matches:
1012 if matches:
1013 return matches[0]
1013 return matches[0]
1014 if self.series and self.applied:
1014 if self.series and self.applied:
1015 if s == 'qtip':
1015 if s == 'qtip':
1016 return self.series[self.series_end(True)-1]
1016 return self.series[self.series_end(True)-1]
1017 if s == 'qbase':
1017 if s == 'qbase':
1018 return self.series[0]
1018 return self.series[0]
1019 return None
1019 return None
1020
1020
1021 if patch is None:
1021 if patch is None:
1022 return None
1022 return None
1023 if patch in self.series:
1023 if patch in self.series:
1024 return patch
1024 return patch
1025
1025
1026 if not os.path.isfile(self.join(patch)):
1026 if not os.path.isfile(self.join(patch)):
1027 try:
1027 try:
1028 sno = int(patch)
1028 sno = int(patch)
1029 except (ValueError, OverflowError):
1029 except (ValueError, OverflowError):
1030 pass
1030 pass
1031 else:
1031 else:
1032 if -len(self.series) <= sno < len(self.series):
1032 if -len(self.series) <= sno < len(self.series):
1033 return self.series[sno]
1033 return self.series[sno]
1034
1034
1035 if not strict:
1035 if not strict:
1036 res = partial_name(patch)
1036 res = partial_name(patch)
1037 if res:
1037 if res:
1038 return res
1038 return res
1039 minus = patch.rfind('-')
1039 minus = patch.rfind('-')
1040 if minus >= 0:
1040 if minus >= 0:
1041 res = partial_name(patch[:minus])
1041 res = partial_name(patch[:minus])
1042 if res:
1042 if res:
1043 i = self.series.index(res)
1043 i = self.series.index(res)
1044 try:
1044 try:
1045 off = int(patch[minus + 1:] or 1)
1045 off = int(patch[minus + 1:] or 1)
1046 except (ValueError, OverflowError):
1046 except (ValueError, OverflowError):
1047 pass
1047 pass
1048 else:
1048 else:
1049 if i - off >= 0:
1049 if i - off >= 0:
1050 return self.series[i - off]
1050 return self.series[i - off]
1051 plus = patch.rfind('+')
1051 plus = patch.rfind('+')
1052 if plus >= 0:
1052 if plus >= 0:
1053 res = partial_name(patch[:plus])
1053 res = partial_name(patch[:plus])
1054 if res:
1054 if res:
1055 i = self.series.index(res)
1055 i = self.series.index(res)
1056 try:
1056 try:
1057 off = int(patch[plus + 1:] or 1)
1057 off = int(patch[plus + 1:] or 1)
1058 except (ValueError, OverflowError):
1058 except (ValueError, OverflowError):
1059 pass
1059 pass
1060 else:
1060 else:
1061 if i + off < len(self.series):
1061 if i + off < len(self.series):
1062 return self.series[i + off]
1062 return self.series[i + off]
1063 raise util.Abort(_("patch %s not in series") % patch)
1063 raise util.Abort(_("patch %s not in series") % patch)
1064
1064
1065 def push(self, repo, patch=None, force=False, list=False,
1065 def push(self, repo, patch=None, force=False, list=False,
1066 mergeq=None, all=False, move=False, exact=False):
1066 mergeq=None, all=False, move=False, exact=False):
1067 diffopts = self.diffopts()
1067 diffopts = self.diffopts()
1068 wlock = repo.wlock()
1068 wlock = repo.wlock()
1069 try:
1069 try:
1070 heads = []
1070 heads = []
1071 for b, ls in repo.branchmap().iteritems():
1071 for b, ls in repo.branchmap().iteritems():
1072 heads += ls
1072 heads += ls
1073 if not heads:
1073 if not heads:
1074 heads = [nullid]
1074 heads = [nullid]
1075 if repo.dirstate.p1() not in heads and not exact:
1075 if repo.dirstate.p1() not in heads and not exact:
1076 self.ui.status(_("(working directory not at a head)\n"))
1076 self.ui.status(_("(working directory not at a head)\n"))
1077
1077
1078 if not self.series:
1078 if not self.series:
1079 self.ui.warn(_('no patches in series\n'))
1079 self.ui.warn(_('no patches in series\n'))
1080 return 0
1080 return 0
1081
1081
1082 patch = self.lookup(patch)
1082 patch = self.lookup(patch)
1083 # Suppose our series file is: A B C and the current 'top'
1083 # Suppose our series file is: A B C and the current 'top'
1084 # patch is B. qpush C should be performed (moving forward)
1084 # patch is B. qpush C should be performed (moving forward)
1085 # qpush B is a NOP (no change) qpush A is an error (can't
1085 # qpush B is a NOP (no change) qpush A is an error (can't
1086 # go backwards with qpush)
1086 # go backwards with qpush)
1087 if patch:
1087 if patch:
1088 info = self.isapplied(patch)
1088 info = self.isapplied(patch)
1089 if info and info[0] >= len(self.applied) - 1:
1089 if info and info[0] >= len(self.applied) - 1:
1090 self.ui.warn(
1090 self.ui.warn(
1091 _('qpush: %s is already at the top\n') % patch)
1091 _('qpush: %s is already at the top\n') % patch)
1092 return 0
1092 return 0
1093
1093
1094 pushable, reason = self.pushable(patch)
1094 pushable, reason = self.pushable(patch)
1095 if pushable:
1095 if pushable:
1096 if self.series.index(patch) < self.series_end():
1096 if self.series.index(patch) < self.series_end():
1097 raise util.Abort(
1097 raise util.Abort(
1098 _("cannot push to a previous patch: %s") % patch)
1098 _("cannot push to a previous patch: %s") % patch)
1099 else:
1099 else:
1100 if reason:
1100 if reason:
1101 reason = _('guarded by %r') % reason
1101 reason = _('guarded by %r') % reason
1102 else:
1102 else:
1103 reason = _('no matching guards')
1103 reason = _('no matching guards')
1104 self.ui.warn(_("cannot push '%s' - %s\n") % (patch, reason))
1104 self.ui.warn(_("cannot push '%s' - %s\n") % (patch, reason))
1105 return 1
1105 return 1
1106 elif all:
1106 elif all:
1107 patch = self.series[-1]
1107 patch = self.series[-1]
1108 if self.isapplied(patch):
1108 if self.isapplied(patch):
1109 self.ui.warn(_('all patches are currently applied\n'))
1109 self.ui.warn(_('all patches are currently applied\n'))
1110 return 0
1110 return 0
1111
1111
1112 # Following the above example, starting at 'top' of B:
1112 # Following the above example, starting at 'top' of B:
1113 # qpush should be performed (pushes C), but a subsequent
1113 # qpush should be performed (pushes C), but a subsequent
1114 # qpush without an argument is an error (nothing to
1114 # qpush without an argument is an error (nothing to
1115 # apply). This allows a loop of "...while hg qpush..." to
1115 # apply). This allows a loop of "...while hg qpush..." to
1116 # work as it detects an error when done
1116 # work as it detects an error when done
1117 start = self.series_end()
1117 start = self.series_end()
1118 if start == len(self.series):
1118 if start == len(self.series):
1119 self.ui.warn(_('patch series already fully applied\n'))
1119 self.ui.warn(_('patch series already fully applied\n'))
1120 return 1
1120 return 1
1121 if not force:
1121 if not force:
1122 self.check_localchanges(repo)
1122 self.check_localchanges(repo)
1123
1123
1124 if exact:
1124 if exact:
1125 if move:
1125 if move:
1126 raise util.Abort(_("cannot use --exact and --move together"))
1126 raise util.Abort(_("cannot use --exact and --move together"))
1127 if self.applied:
1127 if self.applied:
1128 raise util.Abort(_("cannot push --exact with applied patches"))
1128 raise util.Abort(_("cannot push --exact with applied patches"))
1129 root = self.series[start]
1129 root = self.series[start]
1130 target = patchheader(self.join(root), self.plainmode).parent
1130 target = patchheader(self.join(root), self.plainmode).parent
1131 if not target:
1131 if not target:
1132 raise util.Abort(_("%s does not have a parent recorded" % root))
1132 raise util.Abort(_("%s does not have a parent recorded" % root))
1133 if not repo[target] == repo['.']:
1133 if not repo[target] == repo['.']:
1134 hg.update(repo, target)
1134 hg.update(repo, target)
1135
1135
1136 if move:
1136 if move:
1137 if not patch:
1137 if not patch:
1138 raise util.Abort(_("please specify the patch to move"))
1138 raise util.Abort(_("please specify the patch to move"))
1139 for i, rpn in enumerate(self.full_series[start:]):
1139 for i, rpn in enumerate(self.full_series[start:]):
1140 # strip markers for patch guards
1140 # strip markers for patch guards
1141 if self.guard_re.split(rpn, 1)[0] == patch:
1141 if self.guard_re.split(rpn, 1)[0] == patch:
1142 break
1142 break
1143 index = start + i
1143 index = start + i
1144 assert index < len(self.full_series)
1144 assert index < len(self.full_series)
1145 fullpatch = self.full_series[index]
1145 fullpatch = self.full_series[index]
1146 del self.full_series[index]
1146 del self.full_series[index]
1147 self.full_series.insert(start, fullpatch)
1147 self.full_series.insert(start, fullpatch)
1148 self.parse_series()
1148 self.parse_series()
1149 self.series_dirty = 1
1149 self.series_dirty = 1
1150
1150
1151 self.applied_dirty = 1
1151 self.applied_dirty = 1
1152 if start > 0:
1152 if start > 0:
1153 self.check_toppatch(repo)
1153 self.check_toppatch(repo)
1154 if not patch:
1154 if not patch:
1155 patch = self.series[start]
1155 patch = self.series[start]
1156 end = start + 1
1156 end = start + 1
1157 else:
1157 else:
1158 end = self.series.index(patch, start) + 1
1158 end = self.series.index(patch, start) + 1
1159
1159
1160 s = self.series[start:end]
1160 s = self.series[start:end]
1161 all_files = set()
1161 all_files = set()
1162 try:
1162 try:
1163 if mergeq:
1163 if mergeq:
1164 ret = self.mergepatch(repo, mergeq, s, diffopts)
1164 ret = self.mergepatch(repo, mergeq, s, diffopts)
1165 else:
1165 else:
1166 ret = self.apply(repo, s, list, all_files=all_files)
1166 ret = self.apply(repo, s, list, all_files=all_files)
1167 except:
1167 except:
1168 self.ui.warn(_('cleaning up working directory...'))
1168 self.ui.warn(_('cleaning up working directory...'))
1169 node = repo.dirstate.p1()
1169 node = repo.dirstate.p1()
1170 hg.revert(repo, node, None)
1170 hg.revert(repo, node, None)
1171 # only remove unknown files that we know we touched or
1171 # only remove unknown files that we know we touched or
1172 # created while patching
1172 # created while patching
1173 for f in all_files:
1173 for f in all_files:
1174 if f not in repo.dirstate:
1174 if f not in repo.dirstate:
1175 try:
1175 try:
1176 util.unlinkpath(repo.wjoin(f))
1176 util.unlinkpath(repo.wjoin(f))
1177 except OSError, inst:
1177 except OSError, inst:
1178 if inst.errno != errno.ENOENT:
1178 if inst.errno != errno.ENOENT:
1179 raise
1179 raise
1180 self.ui.warn(_('done\n'))
1180 self.ui.warn(_('done\n'))
1181 raise
1181 raise
1182
1182
1183 if not self.applied:
1183 if not self.applied:
1184 return ret[0]
1184 return ret[0]
1185 top = self.applied[-1].name
1185 top = self.applied[-1].name
1186 if ret[0] and ret[0] > 1:
1186 if ret[0] and ret[0] > 1:
1187 msg = _("errors during apply, please fix and refresh %s\n")
1187 msg = _("errors during apply, please fix and refresh %s\n")
1188 self.ui.write(msg % top)
1188 self.ui.write(msg % top)
1189 else:
1189 else:
1190 self.ui.write(_("now at: %s\n") % top)
1190 self.ui.write(_("now at: %s\n") % top)
1191 return ret[0]
1191 return ret[0]
1192
1192
1193 finally:
1193 finally:
1194 wlock.release()
1194 wlock.release()
1195
1195
1196 def pop(self, repo, patch=None, force=False, update=True, all=False):
1196 def pop(self, repo, patch=None, force=False, update=True, all=False):
1197 wlock = repo.wlock()
1197 wlock = repo.wlock()
1198 try:
1198 try:
1199 if patch:
1199 if patch:
1200 # index, rev, patch
1200 # index, rev, patch
1201 info = self.isapplied(patch)
1201 info = self.isapplied(patch)
1202 if not info:
1202 if not info:
1203 patch = self.lookup(patch)
1203 patch = self.lookup(patch)
1204 info = self.isapplied(patch)
1204 info = self.isapplied(patch)
1205 if not info:
1205 if not info:
1206 raise util.Abort(_("patch %s is not applied") % patch)
1206 raise util.Abort(_("patch %s is not applied") % patch)
1207
1207
1208 if not self.applied:
1208 if not self.applied:
1209 # Allow qpop -a to work repeatedly,
1209 # Allow qpop -a to work repeatedly,
1210 # but not qpop without an argument
1210 # but not qpop without an argument
1211 self.ui.warn(_("no patches applied\n"))
1211 self.ui.warn(_("no patches applied\n"))
1212 return not all
1212 return not all
1213
1213
1214 if all:
1214 if all:
1215 start = 0
1215 start = 0
1216 elif patch:
1216 elif patch:
1217 start = info[0] + 1
1217 start = info[0] + 1
1218 else:
1218 else:
1219 start = len(self.applied) - 1
1219 start = len(self.applied) - 1
1220
1220
1221 if start >= len(self.applied):
1221 if start >= len(self.applied):
1222 self.ui.warn(_("qpop: %s is already at the top\n") % patch)
1222 self.ui.warn(_("qpop: %s is already at the top\n") % patch)
1223 return
1223 return
1224
1224
1225 if not update:
1225 if not update:
1226 parents = repo.dirstate.parents()
1226 parents = repo.dirstate.parents()
1227 rr = [x.node for x in self.applied]
1227 rr = [x.node for x in self.applied]
1228 for p in parents:
1228 for p in parents:
1229 if p in rr:
1229 if p in rr:
1230 self.ui.warn(_("qpop: forcing dirstate update\n"))
1230 self.ui.warn(_("qpop: forcing dirstate update\n"))
1231 update = True
1231 update = True
1232 else:
1232 else:
1233 parents = [p.node() for p in repo[None].parents()]
1233 parents = [p.node() for p in repo[None].parents()]
1234 needupdate = False
1234 needupdate = False
1235 for entry in self.applied[start:]:
1235 for entry in self.applied[start:]:
1236 if entry.node in parents:
1236 if entry.node in parents:
1237 needupdate = True
1237 needupdate = True
1238 break
1238 break
1239 update = needupdate
1239 update = needupdate
1240
1240
1241 if not force and update:
1241 if not force and update:
1242 self.check_localchanges(repo)
1242 self.check_localchanges(repo)
1243
1243
1244 self.applied_dirty = 1
1244 self.applied_dirty = 1
1245 end = len(self.applied)
1245 end = len(self.applied)
1246 rev = self.applied[start].node
1246 rev = self.applied[start].node
1247 if update:
1247 if update:
1248 top = self.check_toppatch(repo)[0]
1248 top = self.check_toppatch(repo)[0]
1249
1249
1250 try:
1250 try:
1251 heads = repo.changelog.heads(rev)
1251 heads = repo.changelog.heads(rev)
1252 except error.LookupError:
1252 except error.LookupError:
1253 node = short(rev)
1253 node = short(rev)
1254 raise util.Abort(_('trying to pop unknown node %s') % node)
1254 raise util.Abort(_('trying to pop unknown node %s') % node)
1255
1255
1256 if heads != [self.applied[-1].node]:
1256 if heads != [self.applied[-1].node]:
1257 raise util.Abort(_("popping would remove a revision not "
1257 raise util.Abort(_("popping would remove a revision not "
1258 "managed by this patch queue"))
1258 "managed by this patch queue"))
1259
1259
1260 # we know there are no local changes, so we can make a simplified
1260 # we know there are no local changes, so we can make a simplified
1261 # form of hg.update.
1261 # form of hg.update.
1262 if update:
1262 if update:
1263 qp = self.qparents(repo, rev)
1263 qp = self.qparents(repo, rev)
1264 ctx = repo[qp]
1264 ctx = repo[qp]
1265 m, a, r, d = repo.status(qp, top)[:4]
1265 m, a, r, d = repo.status(qp, top)[:4]
1266 if d:
1266 if d:
1267 raise util.Abort(_("deletions found between repo revs"))
1267 raise util.Abort(_("deletions found between repo revs"))
1268 for f in a:
1268 for f in a:
1269 try:
1269 try:
1270 util.unlinkpath(repo.wjoin(f))
1270 util.unlinkpath(repo.wjoin(f))
1271 except OSError, e:
1271 except OSError, e:
1272 if e.errno != errno.ENOENT:
1272 if e.errno != errno.ENOENT:
1273 raise
1273 raise
1274 repo.dirstate.forget(f)
1274 repo.dirstate.forget(f)
1275 for f in m + r:
1275 for f in m + r:
1276 fctx = ctx[f]
1276 fctx = ctx[f]
1277 repo.wwrite(f, fctx.data(), fctx.flags())
1277 repo.wwrite(f, fctx.data(), fctx.flags())
1278 repo.dirstate.normal(f)
1278 repo.dirstate.normal(f)
1279 repo.dirstate.setparents(qp, nullid)
1279 repo.dirstate.setparents(qp, nullid)
1280 for patch in reversed(self.applied[start:end]):
1280 for patch in reversed(self.applied[start:end]):
1281 self.ui.status(_("popping %s\n") % patch.name)
1281 self.ui.status(_("popping %s\n") % patch.name)
1282 del self.applied[start:end]
1282 del self.applied[start:end]
1283 self.strip(repo, [rev], update=False, backup='strip')
1283 self.strip(repo, [rev], update=False, backup='strip')
1284 if self.applied:
1284 if self.applied:
1285 self.ui.write(_("now at: %s\n") % self.applied[-1].name)
1285 self.ui.write(_("now at: %s\n") % self.applied[-1].name)
1286 else:
1286 else:
1287 self.ui.write(_("patch queue now empty\n"))
1287 self.ui.write(_("patch queue now empty\n"))
1288 finally:
1288 finally:
1289 wlock.release()
1289 wlock.release()
1290
1290
1291 def diff(self, repo, pats, opts):
1291 def diff(self, repo, pats, opts):
1292 top, patch = self.check_toppatch(repo)
1292 top, patch = self.check_toppatch(repo)
1293 if not top:
1293 if not top:
1294 self.ui.write(_("no patches applied\n"))
1294 self.ui.write(_("no patches applied\n"))
1295 return
1295 return
1296 qp = self.qparents(repo, top)
1296 qp = self.qparents(repo, top)
1297 if opts.get('reverse'):
1297 if opts.get('reverse'):
1298 node1, node2 = None, qp
1298 node1, node2 = None, qp
1299 else:
1299 else:
1300 node1, node2 = qp, None
1300 node1, node2 = qp, None
1301 diffopts = self.diffopts(opts, patch)
1301 diffopts = self.diffopts(opts, patch)
1302 self.printdiff(repo, diffopts, node1, node2, files=pats, opts=opts)
1302 self.printdiff(repo, diffopts, node1, node2, files=pats, opts=opts)
1303
1303
1304 def refresh(self, repo, pats=None, **opts):
1304 def refresh(self, repo, pats=None, **opts):
1305 if not self.applied:
1305 if not self.applied:
1306 self.ui.write(_("no patches applied\n"))
1306 self.ui.write(_("no patches applied\n"))
1307 return 1
1307 return 1
1308 msg = opts.get('msg', '').rstrip()
1308 msg = opts.get('msg', '').rstrip()
1309 newuser = opts.get('user')
1309 newuser = opts.get('user')
1310 newdate = opts.get('date')
1310 newdate = opts.get('date')
1311 if newdate:
1311 if newdate:
1312 newdate = '%d %d' % util.parsedate(newdate)
1312 newdate = '%d %d' % util.parsedate(newdate)
1313 wlock = repo.wlock()
1313 wlock = repo.wlock()
1314
1314
1315 try:
1315 try:
1316 self.check_toppatch(repo)
1316 self.check_toppatch(repo)
1317 (top, patchfn) = (self.applied[-1].node, self.applied[-1].name)
1317 (top, patchfn) = (self.applied[-1].node, self.applied[-1].name)
1318 if repo.changelog.heads(top) != [top]:
1318 if repo.changelog.heads(top) != [top]:
1319 raise util.Abort(_("cannot refresh a revision with children"))
1319 raise util.Abort(_("cannot refresh a revision with children"))
1320
1320
1321 inclsubs = self.check_substate(repo)
1321 inclsubs = self.check_substate(repo)
1322
1322
1323 cparents = repo.changelog.parents(top)
1323 cparents = repo.changelog.parents(top)
1324 patchparent = self.qparents(repo, top)
1324 patchparent = self.qparents(repo, top)
1325 ph = patchheader(self.join(patchfn), self.plainmode)
1325 ph = patchheader(self.join(patchfn), self.plainmode)
1326 diffopts = self.diffopts({'git': opts.get('git')}, patchfn)
1326 diffopts = self.diffopts({'git': opts.get('git')}, patchfn)
1327 if msg:
1327 if msg:
1328 ph.setmessage(msg)
1328 ph.setmessage(msg)
1329 if newuser:
1329 if newuser:
1330 ph.setuser(newuser)
1330 ph.setuser(newuser)
1331 if newdate:
1331 if newdate:
1332 ph.setdate(newdate)
1332 ph.setdate(newdate)
1333 ph.setparent(hex(patchparent))
1333 ph.setparent(hex(patchparent))
1334
1334
1335 # only commit new patch when write is complete
1335 # only commit new patch when write is complete
1336 patchf = self.opener(patchfn, 'w', atomictemp=True)
1336 patchf = self.opener(patchfn, 'w', atomictemp=True)
1337
1337
1338 comments = str(ph)
1338 comments = str(ph)
1339 if comments:
1339 if comments:
1340 patchf.write(comments)
1340 patchf.write(comments)
1341
1341
1342 # update the dirstate in place, strip off the qtip commit
1342 # update the dirstate in place, strip off the qtip commit
1343 # and then commit.
1343 # and then commit.
1344 #
1344 #
1345 # this should really read:
1345 # this should really read:
1346 # mm, dd, aa = repo.status(top, patchparent)[:3]
1346 # mm, dd, aa = repo.status(top, patchparent)[:3]
1347 # but we do it backwards to take advantage of manifest/chlog
1347 # but we do it backwards to take advantage of manifest/chlog
1348 # caching against the next repo.status call
1348 # caching against the next repo.status call
1349 mm, aa, dd = repo.status(patchparent, top)[:3]
1349 mm, aa, dd = repo.status(patchparent, top)[:3]
1350 changes = repo.changelog.read(top)
1350 changes = repo.changelog.read(top)
1351 man = repo.manifest.read(changes[0])
1351 man = repo.manifest.read(changes[0])
1352 aaa = aa[:]
1352 aaa = aa[:]
1353 matchfn = cmdutil.match(repo, pats, opts)
1353 matchfn = cmdutil.match(repo, pats, opts)
1354 # in short mode, we only diff the files included in the
1354 # in short mode, we only diff the files included in the
1355 # patch already plus specified files
1355 # patch already plus specified files
1356 if opts.get('short'):
1356 if opts.get('short'):
1357 # if amending a patch, we start with existing
1357 # if amending a patch, we start with existing
1358 # files plus specified files - unfiltered
1358 # files plus specified files - unfiltered
1359 match = cmdutil.matchfiles(repo, mm + aa + dd + matchfn.files())
1359 match = cmdutil.matchfiles(repo, mm + aa + dd + matchfn.files())
1360 # filter with inc/exl options
1360 # filter with inc/exl options
1361 matchfn = cmdutil.match(repo, opts=opts)
1361 matchfn = cmdutil.match(repo, opts=opts)
1362 else:
1362 else:
1363 match = cmdutil.matchall(repo)
1363 match = cmdutil.matchall(repo)
1364 m, a, r, d = repo.status(match=match)[:4]
1364 m, a, r, d = repo.status(match=match)[:4]
1365 mm = set(mm)
1365 mm = set(mm)
1366 aa = set(aa)
1366 aa = set(aa)
1367 dd = set(dd)
1367 dd = set(dd)
1368
1368
1369 # we might end up with files that were added between
1369 # we might end up with files that were added between
1370 # qtip and the dirstate parent, but then changed in the
1370 # qtip and the dirstate parent, but then changed in the
1371 # local dirstate. in this case, we want them to only
1371 # local dirstate. in this case, we want them to only
1372 # show up in the added section
1372 # show up in the added section
1373 for x in m:
1373 for x in m:
1374 if x not in aa:
1374 if x not in aa:
1375 mm.add(x)
1375 mm.add(x)
1376 # we might end up with files added by the local dirstate that
1376 # we might end up with files added by the local dirstate that
1377 # were deleted by the patch. In this case, they should only
1377 # were deleted by the patch. In this case, they should only
1378 # show up in the changed section.
1378 # show up in the changed section.
1379 for x in a:
1379 for x in a:
1380 if x in dd:
1380 if x in dd:
1381 dd.remove(x)
1381 dd.remove(x)
1382 mm.add(x)
1382 mm.add(x)
1383 else:
1383 else:
1384 aa.add(x)
1384 aa.add(x)
1385 # make sure any files deleted in the local dirstate
1385 # make sure any files deleted in the local dirstate
1386 # are not in the add or change column of the patch
1386 # are not in the add or change column of the patch
1387 forget = []
1387 forget = []
1388 for x in d + r:
1388 for x in d + r:
1389 if x in aa:
1389 if x in aa:
1390 aa.remove(x)
1390 aa.remove(x)
1391 forget.append(x)
1391 forget.append(x)
1392 continue
1392 continue
1393 else:
1393 else:
1394 mm.discard(x)
1394 mm.discard(x)
1395 dd.add(x)
1395 dd.add(x)
1396
1396
1397 m = list(mm)
1397 m = list(mm)
1398 r = list(dd)
1398 r = list(dd)
1399 a = list(aa)
1399 a = list(aa)
1400 c = [filter(matchfn, l) for l in (m, a, r)]
1400 c = [filter(matchfn, l) for l in (m, a, r)]
1401 match = cmdutil.matchfiles(repo, set(c[0] + c[1] + c[2] + inclsubs))
1401 match = cmdutil.matchfiles(repo, set(c[0] + c[1] + c[2] + inclsubs))
1402 chunks = patch.diff(repo, patchparent, match=match,
1402 chunks = patch.diff(repo, patchparent, match=match,
1403 changes=c, opts=diffopts)
1403 changes=c, opts=diffopts)
1404 for chunk in chunks:
1404 for chunk in chunks:
1405 patchf.write(chunk)
1405 patchf.write(chunk)
1406
1406
1407 try:
1407 try:
1408 if diffopts.git or diffopts.upgrade:
1408 if diffopts.git or diffopts.upgrade:
1409 copies = {}
1409 copies = {}
1410 for dst in a:
1410 for dst in a:
1411 src = repo.dirstate.copied(dst)
1411 src = repo.dirstate.copied(dst)
1412 # during qfold, the source file for copies may
1412 # during qfold, the source file for copies may
1413 # be removed. Treat this as a simple add.
1413 # be removed. Treat this as a simple add.
1414 if src is not None and src in repo.dirstate:
1414 if src is not None and src in repo.dirstate:
1415 copies.setdefault(src, []).append(dst)
1415 copies.setdefault(src, []).append(dst)
1416 repo.dirstate.add(dst)
1416 repo.dirstate.add(dst)
1417 # remember the copies between patchparent and qtip
1417 # remember the copies between patchparent and qtip
1418 for dst in aaa:
1418 for dst in aaa:
1419 f = repo.file(dst)
1419 f = repo.file(dst)
1420 src = f.renamed(man[dst])
1420 src = f.renamed(man[dst])
1421 if src:
1421 if src:
1422 copies.setdefault(src[0], []).extend(
1422 copies.setdefault(src[0], []).extend(
1423 copies.get(dst, []))
1423 copies.get(dst, []))
1424 if dst in a:
1424 if dst in a:
1425 copies[src[0]].append(dst)
1425 copies[src[0]].append(dst)
1426 # we can't copy a file created by the patch itself
1426 # we can't copy a file created by the patch itself
1427 if dst in copies:
1427 if dst in copies:
1428 del copies[dst]
1428 del copies[dst]
1429 for src, dsts in copies.iteritems():
1429 for src, dsts in copies.iteritems():
1430 for dst in dsts:
1430 for dst in dsts:
1431 repo.dirstate.copy(src, dst)
1431 repo.dirstate.copy(src, dst)
1432 else:
1432 else:
1433 for dst in a:
1433 for dst in a:
1434 repo.dirstate.add(dst)
1434 repo.dirstate.add(dst)
1435 # Drop useless copy information
1435 # Drop useless copy information
1436 for f in list(repo.dirstate.copies()):
1436 for f in list(repo.dirstate.copies()):
1437 repo.dirstate.copy(None, f)
1437 repo.dirstate.copy(None, f)
1438 for f in r:
1438 for f in r:
1439 repo.dirstate.remove(f)
1439 repo.dirstate.remove(f)
1440 # if the patch excludes a modified file, mark that
1440 # if the patch excludes a modified file, mark that
1441 # file with mtime=0 so status can see it.
1441 # file with mtime=0 so status can see it.
1442 mm = []
1442 mm = []
1443 for i in xrange(len(m)-1, -1, -1):
1443 for i in xrange(len(m)-1, -1, -1):
1444 if not matchfn(m[i]):
1444 if not matchfn(m[i]):
1445 mm.append(m[i])
1445 mm.append(m[i])
1446 del m[i]
1446 del m[i]
1447 for f in m:
1447 for f in m:
1448 repo.dirstate.normal(f)
1448 repo.dirstate.normal(f)
1449 for f in mm:
1449 for f in mm:
1450 repo.dirstate.normallookup(f)
1450 repo.dirstate.normallookup(f)
1451 for f in forget:
1451 for f in forget:
1452 repo.dirstate.forget(f)
1452 repo.dirstate.forget(f)
1453
1453
1454 if not msg:
1454 if not msg:
1455 if not ph.message:
1455 if not ph.message:
1456 message = "[mq]: %s\n" % patchfn
1456 message = "[mq]: %s\n" % patchfn
1457 else:
1457 else:
1458 message = "\n".join(ph.message)
1458 message = "\n".join(ph.message)
1459 else:
1459 else:
1460 message = msg
1460 message = msg
1461
1461
1462 user = ph.user or changes[1]
1462 user = ph.user or changes[1]
1463
1463
1464 # assumes strip can roll itself back if interrupted
1464 # assumes strip can roll itself back if interrupted
1465 repo.dirstate.setparents(*cparents)
1465 repo.dirstate.setparents(*cparents)
1466 self.applied.pop()
1466 self.applied.pop()
1467 self.applied_dirty = 1
1467 self.applied_dirty = 1
1468 self.strip(repo, [top], update=False,
1468 self.strip(repo, [top], update=False,
1469 backup='strip')
1469 backup='strip')
1470 except:
1470 except:
1471 repo.dirstate.invalidate()
1471 repo.dirstate.invalidate()
1472 raise
1472 raise
1473
1473
1474 try:
1474 try:
1475 # might be nice to attempt to roll back strip after this
1475 # might be nice to attempt to roll back strip after this
1476 n = repo.commit(message, user, ph.date, match=match,
1476 n = repo.commit(message, user, ph.date, match=match,
1477 force=True)
1477 force=True)
1478 # only write patch after a successful commit
1478 # only write patch after a successful commit
1479 patchf.rename()
1479 patchf.rename()
1480 self.applied.append(statusentry(n, patchfn))
1480 self.applied.append(statusentry(n, patchfn))
1481 except:
1481 except:
1482 ctx = repo[cparents[0]]
1482 ctx = repo[cparents[0]]
1483 repo.dirstate.rebuild(ctx.node(), ctx.manifest())
1483 repo.dirstate.rebuild(ctx.node(), ctx.manifest())
1484 self.save_dirty()
1484 self.save_dirty()
1485 self.ui.warn(_('refresh interrupted while patch was popped! '
1485 self.ui.warn(_('refresh interrupted while patch was popped! '
1486 '(revert --all, qpush to recover)\n'))
1486 '(revert --all, qpush to recover)\n'))
1487 raise
1487 raise
1488 finally:
1488 finally:
1489 wlock.release()
1489 wlock.release()
1490 self.removeundo(repo)
1490 self.removeundo(repo)
1491
1491
1492 def init(self, repo, create=False):
1492 def init(self, repo, create=False):
1493 if not create and os.path.isdir(self.path):
1493 if not create and os.path.isdir(self.path):
1494 raise util.Abort(_("patch queue directory already exists"))
1494 raise util.Abort(_("patch queue directory already exists"))
1495 try:
1495 try:
1496 os.mkdir(self.path)
1496 os.mkdir(self.path)
1497 except OSError, inst:
1497 except OSError, inst:
1498 if inst.errno != errno.EEXIST or not create:
1498 if inst.errno != errno.EEXIST or not create:
1499 raise
1499 raise
1500 if create:
1500 if create:
1501 return self.qrepo(create=True)
1501 return self.qrepo(create=True)
1502
1502
1503 def unapplied(self, repo, patch=None):
1503 def unapplied(self, repo, patch=None):
1504 if patch and patch not in self.series:
1504 if patch and patch not in self.series:
1505 raise util.Abort(_("patch %s is not in series file") % patch)
1505 raise util.Abort(_("patch %s is not in series file") % patch)
1506 if not patch:
1506 if not patch:
1507 start = self.series_end()
1507 start = self.series_end()
1508 else:
1508 else:
1509 start = self.series.index(patch) + 1
1509 start = self.series.index(patch) + 1
1510 unapplied = []
1510 unapplied = []
1511 for i in xrange(start, len(self.series)):
1511 for i in xrange(start, len(self.series)):
1512 pushable, reason = self.pushable(i)
1512 pushable, reason = self.pushable(i)
1513 if pushable:
1513 if pushable:
1514 unapplied.append((i, self.series[i]))
1514 unapplied.append((i, self.series[i]))
1515 self.explain_pushable(i)
1515 self.explain_pushable(i)
1516 return unapplied
1516 return unapplied
1517
1517
1518 def qseries(self, repo, missing=None, start=0, length=None, status=None,
1518 def qseries(self, repo, missing=None, start=0, length=None, status=None,
1519 summary=False):
1519 summary=False):
1520 def displayname(pfx, patchname, state):
1520 def displayname(pfx, patchname, state):
1521 if pfx:
1521 if pfx:
1522 self.ui.write(pfx)
1522 self.ui.write(pfx)
1523 if summary:
1523 if summary:
1524 ph = patchheader(self.join(patchname), self.plainmode)
1524 ph = patchheader(self.join(patchname), self.plainmode)
1525 msg = ph.message and ph.message[0] or ''
1525 msg = ph.message and ph.message[0] or ''
1526 if self.ui.formatted():
1526 if self.ui.formatted():
1527 width = self.ui.termwidth() - len(pfx) - len(patchname) - 2
1527 width = self.ui.termwidth() - len(pfx) - len(patchname) - 2
1528 if width > 0:
1528 if width > 0:
1529 msg = util.ellipsis(msg, width)
1529 msg = util.ellipsis(msg, width)
1530 else:
1530 else:
1531 msg = ''
1531 msg = ''
1532 self.ui.write(patchname, label='qseries.' + state)
1532 self.ui.write(patchname, label='qseries.' + state)
1533 self.ui.write(': ')
1533 self.ui.write(': ')
1534 self.ui.write(msg, label='qseries.message.' + state)
1534 self.ui.write(msg, label='qseries.message.' + state)
1535 else:
1535 else:
1536 self.ui.write(patchname, label='qseries.' + state)
1536 self.ui.write(patchname, label='qseries.' + state)
1537 self.ui.write('\n')
1537 self.ui.write('\n')
1538
1538
1539 applied = set([p.name for p in self.applied])
1539 applied = set([p.name for p in self.applied])
1540 if length is None:
1540 if length is None:
1541 length = len(self.series) - start
1541 length = len(self.series) - start
1542 if not missing:
1542 if not missing:
1543 if self.ui.verbose:
1543 if self.ui.verbose:
1544 idxwidth = len(str(start + length - 1))
1544 idxwidth = len(str(start + length - 1))
1545 for i in xrange(start, start + length):
1545 for i in xrange(start, start + length):
1546 patch = self.series[i]
1546 patch = self.series[i]
1547 if patch in applied:
1547 if patch in applied:
1548 char, state = 'A', 'applied'
1548 char, state = 'A', 'applied'
1549 elif self.pushable(i)[0]:
1549 elif self.pushable(i)[0]:
1550 char, state = 'U', 'unapplied'
1550 char, state = 'U', 'unapplied'
1551 else:
1551 else:
1552 char, state = 'G', 'guarded'
1552 char, state = 'G', 'guarded'
1553 pfx = ''
1553 pfx = ''
1554 if self.ui.verbose:
1554 if self.ui.verbose:
1555 pfx = '%*d %s ' % (idxwidth, i, char)
1555 pfx = '%*d %s ' % (idxwidth, i, char)
1556 elif status and status != char:
1556 elif status and status != char:
1557 continue
1557 continue
1558 displayname(pfx, patch, state)
1558 displayname(pfx, patch, state)
1559 else:
1559 else:
1560 msng_list = []
1560 msng_list = []
1561 for root, dirs, files in os.walk(self.path):
1561 for root, dirs, files in os.walk(self.path):
1562 d = root[len(self.path) + 1:]
1562 d = root[len(self.path) + 1:]
1563 for f in files:
1563 for f in files:
1564 fl = os.path.join(d, f)
1564 fl = os.path.join(d, f)
1565 if (fl not in self.series and
1565 if (fl not in self.series and
1566 fl not in (self.status_path, self.series_path,
1566 fl not in (self.status_path, self.series_path,
1567 self.guards_path)
1567 self.guards_path)
1568 and not fl.startswith('.')):
1568 and not fl.startswith('.')):
1569 msng_list.append(fl)
1569 msng_list.append(fl)
1570 for x in sorted(msng_list):
1570 for x in sorted(msng_list):
1571 pfx = self.ui.verbose and ('D ') or ''
1571 pfx = self.ui.verbose and ('D ') or ''
1572 displayname(pfx, x, 'missing')
1572 displayname(pfx, x, 'missing')
1573
1573
1574 def issaveline(self, l):
1574 def issaveline(self, l):
1575 if l.name == '.hg.patches.save.line':
1575 if l.name == '.hg.patches.save.line':
1576 return True
1576 return True
1577
1577
1578 def qrepo(self, create=False):
1578 def qrepo(self, create=False):
1579 ui = self.ui.copy()
1579 ui = self.ui.copy()
1580 ui.setconfig('paths', 'default', '', overlay=False)
1580 ui.setconfig('paths', 'default', '', overlay=False)
1581 ui.setconfig('paths', 'default-push', '', overlay=False)
1581 ui.setconfig('paths', 'default-push', '', overlay=False)
1582 if create or os.path.isdir(self.join(".hg")):
1582 if create or os.path.isdir(self.join(".hg")):
1583 return hg.repository(ui, path=self.path, create=create)
1583 return hg.repository(ui, path=self.path, create=create)
1584
1584
1585 def restore(self, repo, rev, delete=None, qupdate=None):
1585 def restore(self, repo, rev, delete=None, qupdate=None):
1586 desc = repo[rev].description().strip()
1586 desc = repo[rev].description().strip()
1587 lines = desc.splitlines()
1587 lines = desc.splitlines()
1588 i = 0
1588 i = 0
1589 datastart = None
1589 datastart = None
1590 series = []
1590 series = []
1591 applied = []
1591 applied = []
1592 qpp = None
1592 qpp = None
1593 for i, line in enumerate(lines):
1593 for i, line in enumerate(lines):
1594 if line == 'Patch Data:':
1594 if line == 'Patch Data:':
1595 datastart = i + 1
1595 datastart = i + 1
1596 elif line.startswith('Dirstate:'):
1596 elif line.startswith('Dirstate:'):
1597 l = line.rstrip()
1597 l = line.rstrip()
1598 l = l[10:].split(' ')
1598 l = l[10:].split(' ')
1599 qpp = [bin(x) for x in l]
1599 qpp = [bin(x) for x in l]
1600 elif datastart is not None:
1600 elif datastart is not None:
1601 l = line.rstrip()
1601 l = line.rstrip()
1602 n, name = l.split(':', 1)
1602 n, name = l.split(':', 1)
1603 if n:
1603 if n:
1604 applied.append(statusentry(bin(n), name))
1604 applied.append(statusentry(bin(n), name))
1605 else:
1605 else:
1606 series.append(l)
1606 series.append(l)
1607 if datastart is None:
1607 if datastart is None:
1608 self.ui.warn(_("No saved patch data found\n"))
1608 self.ui.warn(_("No saved patch data found\n"))
1609 return 1
1609 return 1
1610 self.ui.warn(_("restoring status: %s\n") % lines[0])
1610 self.ui.warn(_("restoring status: %s\n") % lines[0])
1611 self.full_series = series
1611 self.full_series = series
1612 self.applied = applied
1612 self.applied = applied
1613 self.parse_series()
1613 self.parse_series()
1614 self.series_dirty = 1
1614 self.series_dirty = 1
1615 self.applied_dirty = 1
1615 self.applied_dirty = 1
1616 heads = repo.changelog.heads()
1616 heads = repo.changelog.heads()
1617 if delete:
1617 if delete:
1618 if rev not in heads:
1618 if rev not in heads:
1619 self.ui.warn(_("save entry has children, leaving it alone\n"))
1619 self.ui.warn(_("save entry has children, leaving it alone\n"))
1620 else:
1620 else:
1621 self.ui.warn(_("removing save entry %s\n") % short(rev))
1621 self.ui.warn(_("removing save entry %s\n") % short(rev))
1622 pp = repo.dirstate.parents()
1622 pp = repo.dirstate.parents()
1623 if rev in pp:
1623 if rev in pp:
1624 update = True
1624 update = True
1625 else:
1625 else:
1626 update = False
1626 update = False
1627 self.strip(repo, [rev], update=update, backup='strip')
1627 self.strip(repo, [rev], update=update, backup='strip')
1628 if qpp:
1628 if qpp:
1629 self.ui.warn(_("saved queue repository parents: %s %s\n") %
1629 self.ui.warn(_("saved queue repository parents: %s %s\n") %
1630 (short(qpp[0]), short(qpp[1])))
1630 (short(qpp[0]), short(qpp[1])))
1631 if qupdate:
1631 if qupdate:
1632 self.ui.status(_("updating queue directory\n"))
1632 self.ui.status(_("updating queue directory\n"))
1633 r = self.qrepo()
1633 r = self.qrepo()
1634 if not r:
1634 if not r:
1635 self.ui.warn(_("Unable to load queue repository\n"))
1635 self.ui.warn(_("Unable to load queue repository\n"))
1636 return 1
1636 return 1
1637 hg.clean(r, qpp[0])
1637 hg.clean(r, qpp[0])
1638
1638
1639 def save(self, repo, msg=None):
1639 def save(self, repo, msg=None):
1640 if not self.applied:
1640 if not self.applied:
1641 self.ui.warn(_("save: no patches applied, exiting\n"))
1641 self.ui.warn(_("save: no patches applied, exiting\n"))
1642 return 1
1642 return 1
1643 if self.issaveline(self.applied[-1]):
1643 if self.issaveline(self.applied[-1]):
1644 self.ui.warn(_("status is already saved\n"))
1644 self.ui.warn(_("status is already saved\n"))
1645 return 1
1645 return 1
1646
1646
1647 if not msg:
1647 if not msg:
1648 msg = _("hg patches saved state")
1648 msg = _("hg patches saved state")
1649 else:
1649 else:
1650 msg = "hg patches: " + msg.rstrip('\r\n')
1650 msg = "hg patches: " + msg.rstrip('\r\n')
1651 r = self.qrepo()
1651 r = self.qrepo()
1652 if r:
1652 if r:
1653 pp = r.dirstate.parents()
1653 pp = r.dirstate.parents()
1654 msg += "\nDirstate: %s %s" % (hex(pp[0]), hex(pp[1]))
1654 msg += "\nDirstate: %s %s" % (hex(pp[0]), hex(pp[1]))
1655 msg += "\n\nPatch Data:\n"
1655 msg += "\n\nPatch Data:\n"
1656 msg += ''.join('%s\n' % x for x in self.applied)
1656 msg += ''.join('%s\n' % x for x in self.applied)
1657 msg += ''.join(':%s\n' % x for x in self.full_series)
1657 msg += ''.join(':%s\n' % x for x in self.full_series)
1658 n = repo.commit(msg, force=True)
1658 n = repo.commit(msg, force=True)
1659 if not n:
1659 if not n:
1660 self.ui.warn(_("repo commit failed\n"))
1660 self.ui.warn(_("repo commit failed\n"))
1661 return 1
1661 return 1
1662 self.applied.append(statusentry(n, '.hg.patches.save.line'))
1662 self.applied.append(statusentry(n, '.hg.patches.save.line'))
1663 self.applied_dirty = 1
1663 self.applied_dirty = 1
1664 self.removeundo(repo)
1664 self.removeundo(repo)
1665
1665
1666 def full_series_end(self):
1666 def full_series_end(self):
1667 if self.applied:
1667 if self.applied:
1668 p = self.applied[-1].name
1668 p = self.applied[-1].name
1669 end = self.find_series(p)
1669 end = self.find_series(p)
1670 if end is None:
1670 if end is None:
1671 return len(self.full_series)
1671 return len(self.full_series)
1672 return end + 1
1672 return end + 1
1673 return 0
1673 return 0
1674
1674
1675 def series_end(self, all_patches=False):
1675 def series_end(self, all_patches=False):
1676 """If all_patches is False, return the index of the next pushable patch
1676 """If all_patches is False, return the index of the next pushable patch
1677 in the series, or the series length. If all_patches is True, return the
1677 in the series, or the series length. If all_patches is True, return the
1678 index of the first patch past the last applied one.
1678 index of the first patch past the last applied one.
1679 """
1679 """
1680 end = 0
1680 end = 0
1681 def next(start):
1681 def next(start):
1682 if all_patches or start >= len(self.series):
1682 if all_patches or start >= len(self.series):
1683 return start
1683 return start
1684 for i in xrange(start, len(self.series)):
1684 for i in xrange(start, len(self.series)):
1685 p, reason = self.pushable(i)
1685 p, reason = self.pushable(i)
1686 if p:
1686 if p:
1687 break
1687 break
1688 self.explain_pushable(i)
1688 self.explain_pushable(i)
1689 return i
1689 return i
1690 if self.applied:
1690 if self.applied:
1691 p = self.applied[-1].name
1691 p = self.applied[-1].name
1692 try:
1692 try:
1693 end = self.series.index(p)
1693 end = self.series.index(p)
1694 except ValueError:
1694 except ValueError:
1695 return 0
1695 return 0
1696 return next(end + 1)
1696 return next(end + 1)
1697 return next(end)
1697 return next(end)
1698
1698
1699 def appliedname(self, index):
1699 def appliedname(self, index):
1700 pname = self.applied[index].name
1700 pname = self.applied[index].name
1701 if not self.ui.verbose:
1701 if not self.ui.verbose:
1702 p = pname
1702 p = pname
1703 else:
1703 else:
1704 p = str(self.series.index(pname)) + " " + pname
1704 p = str(self.series.index(pname)) + " " + pname
1705 return p
1705 return p
1706
1706
1707 def qimport(self, repo, files, patchname=None, rev=None, existing=None,
1707 def qimport(self, repo, files, patchname=None, rev=None, existing=None,
1708 force=None, git=False):
1708 force=None, git=False):
1709 def checkseries(patchname):
1709 def checkseries(patchname):
1710 if patchname in self.series:
1710 if patchname in self.series:
1711 raise util.Abort(_('patch %s is already in the series file')
1711 raise util.Abort(_('patch %s is already in the series file')
1712 % patchname)
1712 % patchname)
1713 def checkfile(patchname):
1713 def checkfile(patchname):
1714 if not force and os.path.exists(self.join(patchname)):
1714 if not force and os.path.exists(self.join(patchname)):
1715 raise util.Abort(_('patch "%s" already exists')
1715 raise util.Abort(_('patch "%s" already exists')
1716 % patchname)
1716 % patchname)
1717
1717
1718 if rev:
1718 if rev:
1719 if files:
1719 if files:
1720 raise util.Abort(_('option "-r" not valid when importing '
1720 raise util.Abort(_('option "-r" not valid when importing '
1721 'files'))
1721 'files'))
1722 rev = cmdutil.revrange(repo, rev)
1722 rev = cmdutil.revrange(repo, rev)
1723 rev.sort(reverse=True)
1723 rev.sort(reverse=True)
1724 if (len(files) > 1 or len(rev) > 1) and patchname:
1724 if (len(files) > 1 or len(rev) > 1) and patchname:
1725 raise util.Abort(_('option "-n" not valid when importing multiple '
1725 raise util.Abort(_('option "-n" not valid when importing multiple '
1726 'patches'))
1726 'patches'))
1727 if rev:
1727 if rev:
1728 # If mq patches are applied, we can only import revisions
1728 # If mq patches are applied, we can only import revisions
1729 # that form a linear path to qbase.
1729 # that form a linear path to qbase.
1730 # Otherwise, they should form a linear path to a head.
1730 # Otherwise, they should form a linear path to a head.
1731 heads = repo.changelog.heads(repo.changelog.node(rev[-1]))
1731 heads = repo.changelog.heads(repo.changelog.node(rev[-1]))
1732 if len(heads) > 1:
1732 if len(heads) > 1:
1733 raise util.Abort(_('revision %d is the root of more than one '
1733 raise util.Abort(_('revision %d is the root of more than one '
1734 'branch') % rev[-1])
1734 'branch') % rev[-1])
1735 if self.applied:
1735 if self.applied:
1736 base = repo.changelog.node(rev[0])
1736 base = repo.changelog.node(rev[0])
1737 if base in [n.node for n in self.applied]:
1737 if base in [n.node for n in self.applied]:
1738 raise util.Abort(_('revision %d is already managed')
1738 raise util.Abort(_('revision %d is already managed')
1739 % rev[0])
1739 % rev[0])
1740 if heads != [self.applied[-1].node]:
1740 if heads != [self.applied[-1].node]:
1741 raise util.Abort(_('revision %d is not the parent of '
1741 raise util.Abort(_('revision %d is not the parent of '
1742 'the queue') % rev[0])
1742 'the queue') % rev[0])
1743 base = repo.changelog.rev(self.applied[0].node)
1743 base = repo.changelog.rev(self.applied[0].node)
1744 lastparent = repo.changelog.parentrevs(base)[0]
1744 lastparent = repo.changelog.parentrevs(base)[0]
1745 else:
1745 else:
1746 if heads != [repo.changelog.node(rev[0])]:
1746 if heads != [repo.changelog.node(rev[0])]:
1747 raise util.Abort(_('revision %d has unmanaged children')
1747 raise util.Abort(_('revision %d has unmanaged children')
1748 % rev[0])
1748 % rev[0])
1749 lastparent = None
1749 lastparent = None
1750
1750
1751 diffopts = self.diffopts({'git': git})
1751 diffopts = self.diffopts({'git': git})
1752 for r in rev:
1752 for r in rev:
1753 p1, p2 = repo.changelog.parentrevs(r)
1753 p1, p2 = repo.changelog.parentrevs(r)
1754 n = repo.changelog.node(r)
1754 n = repo.changelog.node(r)
1755 if p2 != nullrev:
1755 if p2 != nullrev:
1756 raise util.Abort(_('cannot import merge revision %d') % r)
1756 raise util.Abort(_('cannot import merge revision %d') % r)
1757 if lastparent and lastparent != r:
1757 if lastparent and lastparent != r:
1758 raise util.Abort(_('revision %d is not the parent of %d')
1758 raise util.Abort(_('revision %d is not the parent of %d')
1759 % (r, lastparent))
1759 % (r, lastparent))
1760 lastparent = p1
1760 lastparent = p1
1761
1761
1762 if not patchname:
1762 if not patchname:
1763 patchname = normname('%d.diff' % r)
1763 patchname = normname('%d.diff' % r)
1764 self.check_reserved_name(patchname)
1764 self.check_reserved_name(patchname)
1765 checkseries(patchname)
1765 checkseries(patchname)
1766 checkfile(patchname)
1766 checkfile(patchname)
1767 self.full_series.insert(0, patchname)
1767 self.full_series.insert(0, patchname)
1768
1768
1769 patchf = self.opener(patchname, "w")
1769 patchf = self.opener(patchname, "w")
1770 cmdutil.export(repo, [n], fp=patchf, opts=diffopts)
1770 cmdutil.export(repo, [n], fp=patchf, opts=diffopts)
1771 patchf.close()
1771 patchf.close()
1772
1772
1773 se = statusentry(n, patchname)
1773 se = statusentry(n, patchname)
1774 self.applied.insert(0, se)
1774 self.applied.insert(0, se)
1775
1775
1776 self.added.append(patchname)
1776 self.added.append(patchname)
1777 patchname = None
1777 patchname = None
1778 self.parse_series()
1778 self.parse_series()
1779 self.applied_dirty = 1
1779 self.applied_dirty = 1
1780 self.series_dirty = True
1780 self.series_dirty = True
1781
1781
1782 for i, filename in enumerate(files):
1782 for i, filename in enumerate(files):
1783 if existing:
1783 if existing:
1784 if filename == '-':
1784 if filename == '-':
1785 raise util.Abort(_('-e is incompatible with import from -'))
1785 raise util.Abort(_('-e is incompatible with import from -'))
1786 filename = normname(filename)
1786 filename = normname(filename)
1787 self.check_reserved_name(filename)
1787 self.check_reserved_name(filename)
1788 originpath = self.join(filename)
1788 originpath = self.join(filename)
1789 if not os.path.isfile(originpath):
1789 if not os.path.isfile(originpath):
1790 raise util.Abort(_("patch %s does not exist") % filename)
1790 raise util.Abort(_("patch %s does not exist") % filename)
1791
1791
1792 if patchname:
1792 if patchname:
1793 self.check_reserved_name(patchname)
1793 self.check_reserved_name(patchname)
1794 checkfile(patchname)
1794 checkfile(patchname)
1795
1795
1796 self.ui.write(_('renaming %s to %s\n')
1796 self.ui.write(_('renaming %s to %s\n')
1797 % (filename, patchname))
1797 % (filename, patchname))
1798 util.rename(originpath, self.join(patchname))
1798 util.rename(originpath, self.join(patchname))
1799 else:
1799 else:
1800 patchname = filename
1800 patchname = filename
1801
1801
1802 else:
1802 else:
1803 try:
1803 try:
1804 if filename == '-':
1804 if filename == '-':
1805 if not patchname:
1805 if not patchname:
1806 raise util.Abort(
1806 raise util.Abort(
1807 _('need --name to import a patch from -'))
1807 _('need --name to import a patch from -'))
1808 text = sys.stdin.read()
1808 text = sys.stdin.read()
1809 else:
1809 else:
1810 fp = url.open(self.ui, filename)
1810 fp = url.open(self.ui, filename)
1811 text = fp.read()
1811 text = fp.read()
1812 fp.close()
1812 fp.close()
1813 except (OSError, IOError):
1813 except (OSError, IOError):
1814 raise util.Abort(_("unable to read file %s") % filename)
1814 raise util.Abort(_("unable to read file %s") % filename)
1815 if not patchname:
1815 if not patchname:
1816 patchname = normname(os.path.basename(filename))
1816 patchname = normname(os.path.basename(filename))
1817 self.check_reserved_name(patchname)
1817 self.check_reserved_name(patchname)
1818 checkfile(patchname)
1818 checkfile(patchname)
1819 patchf = self.opener(patchname, "w")
1819 patchf = self.opener(patchname, "w")
1820 patchf.write(text)
1820 patchf.write(text)
1821 patchf.close()
1821 patchf.close()
1822 if not force:
1822 if not force:
1823 checkseries(patchname)
1823 checkseries(patchname)
1824 if patchname not in self.series:
1824 if patchname not in self.series:
1825 index = self.full_series_end() + i
1825 index = self.full_series_end() + i
1826 self.full_series[index:index] = [patchname]
1826 self.full_series[index:index] = [patchname]
1827 self.parse_series()
1827 self.parse_series()
1828 self.series_dirty = True
1828 self.series_dirty = True
1829 self.ui.warn(_("adding %s to series file\n") % patchname)
1829 self.ui.warn(_("adding %s to series file\n") % patchname)
1830 self.added.append(patchname)
1830 self.added.append(patchname)
1831 patchname = None
1831 patchname = None
1832
1832
1833 self.removeundo(repo)
1833 self.removeundo(repo)
1834
1834
1835 def delete(ui, repo, *patches, **opts):
1835 def delete(ui, repo, *patches, **opts):
1836 """remove patches from queue
1836 """remove patches from queue
1837
1837
1838 The patches must not be applied, and at least one patch is required. With
1838 The patches must not be applied, and at least one patch is required. With
1839 -k/--keep, the patch files are preserved in the patch directory.
1839 -k/--keep, the patch files are preserved in the patch directory.
1840
1840
1841 To stop managing a patch and move it into permanent history,
1841 To stop managing a patch and move it into permanent history,
1842 use the :hg:`qfinish` command."""
1842 use the :hg:`qfinish` command."""
1843 q = repo.mq
1843 q = repo.mq
1844 q.delete(repo, patches, opts)
1844 q.delete(repo, patches, opts)
1845 q.save_dirty()
1845 q.save_dirty()
1846 return 0
1846 return 0
1847
1847
1848 def applied(ui, repo, patch=None, **opts):
1848 def applied(ui, repo, patch=None, **opts):
1849 """print the patches already applied
1849 """print the patches already applied
1850
1850
1851 Returns 0 on success."""
1851 Returns 0 on success."""
1852
1852
1853 q = repo.mq
1853 q = repo.mq
1854
1854
1855 if patch:
1855 if patch:
1856 if patch not in q.series:
1856 if patch not in q.series:
1857 raise util.Abort(_("patch %s is not in series file") % patch)
1857 raise util.Abort(_("patch %s is not in series file") % patch)
1858 end = q.series.index(patch) + 1
1858 end = q.series.index(patch) + 1
1859 else:
1859 else:
1860 end = q.series_end(True)
1860 end = q.series_end(True)
1861
1861
1862 if opts.get('last') and not end:
1862 if opts.get('last') and not end:
1863 ui.write(_("no patches applied\n"))
1863 ui.write(_("no patches applied\n"))
1864 return 1
1864 return 1
1865 elif opts.get('last') and end == 1:
1865 elif opts.get('last') and end == 1:
1866 ui.write(_("only one patch applied\n"))
1866 ui.write(_("only one patch applied\n"))
1867 return 1
1867 return 1
1868 elif opts.get('last'):
1868 elif opts.get('last'):
1869 start = end - 2
1869 start = end - 2
1870 end = 1
1870 end = 1
1871 else:
1871 else:
1872 start = 0
1872 start = 0
1873
1873
1874 q.qseries(repo, length=end, start=start, status='A',
1874 q.qseries(repo, length=end, start=start, status='A',
1875 summary=opts.get('summary'))
1875 summary=opts.get('summary'))
1876
1876
1877
1877
1878 def unapplied(ui, repo, patch=None, **opts):
1878 def unapplied(ui, repo, patch=None, **opts):
1879 """print the patches not yet applied
1879 """print the patches not yet applied
1880
1880
1881 Returns 0 on success."""
1881 Returns 0 on success."""
1882
1882
1883 q = repo.mq
1883 q = repo.mq
1884 if patch:
1884 if patch:
1885 if patch not in q.series:
1885 if patch not in q.series:
1886 raise util.Abort(_("patch %s is not in series file") % patch)
1886 raise util.Abort(_("patch %s is not in series file") % patch)
1887 start = q.series.index(patch) + 1
1887 start = q.series.index(patch) + 1
1888 else:
1888 else:
1889 start = q.series_end(True)
1889 start = q.series_end(True)
1890
1890
1891 if start == len(q.series) and opts.get('first'):
1891 if start == len(q.series) and opts.get('first'):
1892 ui.write(_("all patches applied\n"))
1892 ui.write(_("all patches applied\n"))
1893 return 1
1893 return 1
1894
1894
1895 length = opts.get('first') and 1 or None
1895 length = opts.get('first') and 1 or None
1896 q.qseries(repo, start=start, length=length, status='U',
1896 q.qseries(repo, start=start, length=length, status='U',
1897 summary=opts.get('summary'))
1897 summary=opts.get('summary'))
1898
1898
1899 def qimport(ui, repo, *filename, **opts):
1899 def qimport(ui, repo, *filename, **opts):
1900 """import a patch
1900 """import a patch
1901
1901
1902 The patch is inserted into the series after the last applied
1902 The patch is inserted into the series after the last applied
1903 patch. If no patches have been applied, qimport prepends the patch
1903 patch. If no patches have been applied, qimport prepends the patch
1904 to the series.
1904 to the series.
1905
1905
1906 The patch will have the same name as its source file unless you
1906 The patch will have the same name as its source file unless you
1907 give it a new one with -n/--name.
1907 give it a new one with -n/--name.
1908
1908
1909 You can register an existing patch inside the patch directory with
1909 You can register an existing patch inside the patch directory with
1910 the -e/--existing flag.
1910 the -e/--existing flag.
1911
1911
1912 With -f/--force, an existing patch of the same name will be
1912 With -f/--force, an existing patch of the same name will be
1913 overwritten.
1913 overwritten.
1914
1914
1915 An existing changeset may be placed under mq control with -r/--rev
1915 An existing changeset may be placed under mq control with -r/--rev
1916 (e.g. qimport --rev tip -n patch will place tip under mq control).
1916 (e.g. qimport --rev tip -n patch will place tip under mq control).
1917 With -g/--git, patches imported with --rev will use the git diff
1917 With -g/--git, patches imported with --rev will use the git diff
1918 format. See the diffs help topic for information on why this is
1918 format. See the diffs help topic for information on why this is
1919 important for preserving rename/copy information and permission
1919 important for preserving rename/copy information and permission
1920 changes. Use :hg:`qfinish` to remove changesets from mq control.
1920 changes. Use :hg:`qfinish` to remove changesets from mq control.
1921
1921
1922 To import a patch from standard input, pass - as the patch file.
1922 To import a patch from standard input, pass - as the patch file.
1923 When importing from standard input, a patch name must be specified
1923 When importing from standard input, a patch name must be specified
1924 using the --name flag.
1924 using the --name flag.
1925
1925
1926 To import an existing patch while renaming it::
1926 To import an existing patch while renaming it::
1927
1927
1928 hg qimport -e existing-patch -n new-name
1928 hg qimport -e existing-patch -n new-name
1929
1929
1930 Returns 0 if import succeeded.
1930 Returns 0 if import succeeded.
1931 """
1931 """
1932 q = repo.mq
1932 q = repo.mq
1933 try:
1933 try:
1934 q.qimport(repo, filename, patchname=opts.get('name'),
1934 q.qimport(repo, filename, patchname=opts.get('name'),
1935 existing=opts.get('existing'), force=opts.get('force'),
1935 existing=opts.get('existing'), force=opts.get('force'),
1936 rev=opts.get('rev'), git=opts.get('git'))
1936 rev=opts.get('rev'), git=opts.get('git'))
1937 finally:
1937 finally:
1938 q.save_dirty()
1938 q.save_dirty()
1939
1939
1940 if opts.get('push') and not opts.get('rev'):
1940 if opts.get('push') and not opts.get('rev'):
1941 return q.push(repo, None)
1941 return q.push(repo, None)
1942 return 0
1942 return 0
1943
1943
1944 def qinit(ui, repo, create):
1944 def qinit(ui, repo, create):
1945 """initialize a new queue repository
1945 """initialize a new queue repository
1946
1946
1947 This command also creates a series file for ordering patches, and
1947 This command also creates a series file for ordering patches, and
1948 an mq-specific .hgignore file in the queue repository, to exclude
1948 an mq-specific .hgignore file in the queue repository, to exclude
1949 the status and guards files (these contain mostly transient state).
1949 the status and guards files (these contain mostly transient state).
1950
1950
1951 Returns 0 if initialization succeeded."""
1951 Returns 0 if initialization succeeded."""
1952 q = repo.mq
1952 q = repo.mq
1953 r = q.init(repo, create)
1953 r = q.init(repo, create)
1954 q.save_dirty()
1954 q.save_dirty()
1955 if r:
1955 if r:
1956 if not os.path.exists(r.wjoin('.hgignore')):
1956 if not os.path.exists(r.wjoin('.hgignore')):
1957 fp = r.wopener('.hgignore', 'w')
1957 fp = r.wopener('.hgignore', 'w')
1958 fp.write('^\\.hg\n')
1958 fp.write('^\\.hg\n')
1959 fp.write('^\\.mq\n')
1959 fp.write('^\\.mq\n')
1960 fp.write('syntax: glob\n')
1960 fp.write('syntax: glob\n')
1961 fp.write('status\n')
1961 fp.write('status\n')
1962 fp.write('guards\n')
1962 fp.write('guards\n')
1963 fp.close()
1963 fp.close()
1964 if not os.path.exists(r.wjoin('series')):
1964 if not os.path.exists(r.wjoin('series')):
1965 r.wopener('series', 'w').close()
1965 r.wopener('series', 'w').close()
1966 r[None].add(['.hgignore', 'series'])
1966 r[None].add(['.hgignore', 'series'])
1967 commands.add(ui, r)
1967 commands.add(ui, r)
1968 return 0
1968 return 0
1969
1969
1970 def init(ui, repo, **opts):
1970 def init(ui, repo, **opts):
1971 """init a new queue repository (DEPRECATED)
1971 """init a new queue repository (DEPRECATED)
1972
1972
1973 The queue repository is unversioned by default. If
1973 The queue repository is unversioned by default. If
1974 -c/--create-repo is specified, qinit will create a separate nested
1974 -c/--create-repo is specified, qinit will create a separate nested
1975 repository for patches (qinit -c may also be run later to convert
1975 repository for patches (qinit -c may also be run later to convert
1976 an unversioned patch repository into a versioned one). You can use
1976 an unversioned patch repository into a versioned one). You can use
1977 qcommit to commit changes to this queue repository.
1977 qcommit to commit changes to this queue repository.
1978
1978
1979 This command is deprecated. Without -c, it's implied by other relevant
1979 This command is deprecated. Without -c, it's implied by other relevant
1980 commands. With -c, use :hg:`init --mq` instead."""
1980 commands. With -c, use :hg:`init --mq` instead."""
1981 return qinit(ui, repo, create=opts.get('create_repo'))
1981 return qinit(ui, repo, create=opts.get('create_repo'))
1982
1982
1983 def clone(ui, source, dest=None, **opts):
1983 def clone(ui, source, dest=None, **opts):
1984 '''clone main and patch repository at same time
1984 '''clone main and patch repository at same time
1985
1985
1986 If source is local, destination will have no patches applied. If
1986 If source is local, destination will have no patches applied. If
1987 source is remote, this command can not check if patches are
1987 source is remote, this command can not check if patches are
1988 applied in source, so cannot guarantee that patches are not
1988 applied in source, so cannot guarantee that patches are not
1989 applied in destination. If you clone remote repository, be sure
1989 applied in destination. If you clone remote repository, be sure
1990 before that it has no patches applied.
1990 before that it has no patches applied.
1991
1991
1992 Source patch repository is looked for in <src>/.hg/patches by
1992 Source patch repository is looked for in <src>/.hg/patches by
1993 default. Use -p <url> to change.
1993 default. Use -p <url> to change.
1994
1994
1995 The patch directory must be a nested Mercurial repository, as
1995 The patch directory must be a nested Mercurial repository, as
1996 would be created by :hg:`init --mq`.
1996 would be created by :hg:`init --mq`.
1997
1997
1998 Return 0 on success.
1998 Return 0 on success.
1999 '''
1999 '''
2000 def patchdir(repo):
2000 def patchdir(repo):
2001 url = repo.url()
2001 url = repo.url()
2002 if url.endswith('/'):
2002 if url.endswith('/'):
2003 url = url[:-1]
2003 url = url[:-1]
2004 return url + '/.hg/patches'
2004 return url + '/.hg/patches'
2005 if dest is None:
2005 if dest is None:
2006 dest = hg.defaultdest(source)
2006 dest = hg.defaultdest(source)
2007 sr = hg.repository(hg.remoteui(ui, opts), ui.expandpath(source))
2007 sr = hg.repository(hg.remoteui(ui, opts), ui.expandpath(source))
2008 if opts.get('patches'):
2008 if opts.get('patches'):
2009 patchespath = ui.expandpath(opts.get('patches'))
2009 patchespath = ui.expandpath(opts.get('patches'))
2010 else:
2010 else:
2011 patchespath = patchdir(sr)
2011 patchespath = patchdir(sr)
2012 try:
2012 try:
2013 hg.repository(ui, patchespath)
2013 hg.repository(ui, patchespath)
2014 except error.RepoError:
2014 except error.RepoError:
2015 raise util.Abort(_('versioned patch repository not found'
2015 raise util.Abort(_('versioned patch repository not found'
2016 ' (see init --mq)'))
2016 ' (see init --mq)'))
2017 qbase, destrev = None, None
2017 qbase, destrev = None, None
2018 if sr.local():
2018 if sr.local():
2019 if sr.mq.applied:
2019 if sr.mq.applied:
2020 qbase = sr.mq.applied[0].node
2020 qbase = sr.mq.applied[0].node
2021 if not hg.islocal(dest):
2021 if not hg.islocal(dest):
2022 heads = set(sr.heads())
2022 heads = set(sr.heads())
2023 destrev = list(heads.difference(sr.heads(qbase)))
2023 destrev = list(heads.difference(sr.heads(qbase)))
2024 destrev.append(sr.changelog.parents(qbase)[0])
2024 destrev.append(sr.changelog.parents(qbase)[0])
2025 elif sr.capable('lookup'):
2025 elif sr.capable('lookup'):
2026 try:
2026 try:
2027 qbase = sr.lookup('qbase')
2027 qbase = sr.lookup('qbase')
2028 except error.RepoError:
2028 except error.RepoError:
2029 pass
2029 pass
2030 ui.note(_('cloning main repository\n'))
2030 ui.note(_('cloning main repository\n'))
2031 sr, dr = hg.clone(ui, sr.url(), dest,
2031 sr, dr = hg.clone(ui, sr.url(), dest,
2032 pull=opts.get('pull'),
2032 pull=opts.get('pull'),
2033 rev=destrev,
2033 rev=destrev,
2034 update=False,
2034 update=False,
2035 stream=opts.get('uncompressed'))
2035 stream=opts.get('uncompressed'))
2036 ui.note(_('cloning patch repository\n'))
2036 ui.note(_('cloning patch repository\n'))
2037 hg.clone(ui, opts.get('patches') or patchdir(sr), patchdir(dr),
2037 hg.clone(ui, opts.get('patches') or patchdir(sr), patchdir(dr),
2038 pull=opts.get('pull'), update=not opts.get('noupdate'),
2038 pull=opts.get('pull'), update=not opts.get('noupdate'),
2039 stream=opts.get('uncompressed'))
2039 stream=opts.get('uncompressed'))
2040 if dr.local():
2040 if dr.local():
2041 if qbase:
2041 if qbase:
2042 ui.note(_('stripping applied patches from destination '
2042 ui.note(_('stripping applied patches from destination '
2043 'repository\n'))
2043 'repository\n'))
2044 dr.mq.strip(dr, [qbase], update=False, backup=None)
2044 dr.mq.strip(dr, [qbase], update=False, backup=None)
2045 if not opts.get('noupdate'):
2045 if not opts.get('noupdate'):
2046 ui.note(_('updating destination repository\n'))
2046 ui.note(_('updating destination repository\n'))
2047 hg.update(dr, dr.changelog.tip())
2047 hg.update(dr, dr.changelog.tip())
2048
2048
2049 def commit(ui, repo, *pats, **opts):
2049 def commit(ui, repo, *pats, **opts):
2050 """commit changes in the queue repository (DEPRECATED)
2050 """commit changes in the queue repository (DEPRECATED)
2051
2051
2052 This command is deprecated; use :hg:`commit --mq` instead."""
2052 This command is deprecated; use :hg:`commit --mq` instead."""
2053 q = repo.mq
2053 q = repo.mq
2054 r = q.qrepo()
2054 r = q.qrepo()
2055 if not r:
2055 if not r:
2056 raise util.Abort('no queue repository')
2056 raise util.Abort('no queue repository')
2057 commands.commit(r.ui, r, *pats, **opts)
2057 commands.commit(r.ui, r, *pats, **opts)
2058
2058
2059 def series(ui, repo, **opts):
2059 def series(ui, repo, **opts):
2060 """print the entire series file
2060 """print the entire series file
2061
2061
2062 Returns 0 on success."""
2062 Returns 0 on success."""
2063 repo.mq.qseries(repo, missing=opts.get('missing'), summary=opts.get('summary'))
2063 repo.mq.qseries(repo, missing=opts.get('missing'), summary=opts.get('summary'))
2064 return 0
2064 return 0
2065
2065
2066 def top(ui, repo, **opts):
2066 def top(ui, repo, **opts):
2067 """print the name of the current patch
2067 """print the name of the current patch
2068
2068
2069 Returns 0 on success."""
2069 Returns 0 on success."""
2070 q = repo.mq
2070 q = repo.mq
2071 t = q.applied and q.series_end(True) or 0
2071 t = q.applied and q.series_end(True) or 0
2072 if t:
2072 if t:
2073 q.qseries(repo, start=t - 1, length=1, status='A',
2073 q.qseries(repo, start=t - 1, length=1, status='A',
2074 summary=opts.get('summary'))
2074 summary=opts.get('summary'))
2075 else:
2075 else:
2076 ui.write(_("no patches applied\n"))
2076 ui.write(_("no patches applied\n"))
2077 return 1
2077 return 1
2078
2078
2079 def next(ui, repo, **opts):
2079 def next(ui, repo, **opts):
2080 """print the name of the next patch
2080 """print the name of the next patch
2081
2081
2082 Returns 0 on success."""
2082 Returns 0 on success."""
2083 q = repo.mq
2083 q = repo.mq
2084 end = q.series_end()
2084 end = q.series_end()
2085 if end == len(q.series):
2085 if end == len(q.series):
2086 ui.write(_("all patches applied\n"))
2086 ui.write(_("all patches applied\n"))
2087 return 1
2087 return 1
2088 q.qseries(repo, start=end, length=1, summary=opts.get('summary'))
2088 q.qseries(repo, start=end, length=1, summary=opts.get('summary'))
2089
2089
2090 def prev(ui, repo, **opts):
2090 def prev(ui, repo, **opts):
2091 """print the name of the previous patch
2091 """print the name of the previous patch
2092
2092
2093 Returns 0 on success."""
2093 Returns 0 on success."""
2094 q = repo.mq
2094 q = repo.mq
2095 l = len(q.applied)
2095 l = len(q.applied)
2096 if l == 1:
2096 if l == 1:
2097 ui.write(_("only one patch applied\n"))
2097 ui.write(_("only one patch applied\n"))
2098 return 1
2098 return 1
2099 if not l:
2099 if not l:
2100 ui.write(_("no patches applied\n"))
2100 ui.write(_("no patches applied\n"))
2101 return 1
2101 return 1
2102 q.qseries(repo, start=l - 2, length=1, status='A',
2102 q.qseries(repo, start=l - 2, length=1, status='A',
2103 summary=opts.get('summary'))
2103 summary=opts.get('summary'))
2104
2104
2105 def setupheaderopts(ui, opts):
2105 def setupheaderopts(ui, opts):
2106 if not opts.get('user') and opts.get('currentuser'):
2106 if not opts.get('user') and opts.get('currentuser'):
2107 opts['user'] = ui.username()
2107 opts['user'] = ui.username()
2108 if not opts.get('date') and opts.get('currentdate'):
2108 if not opts.get('date') and opts.get('currentdate'):
2109 opts['date'] = "%d %d" % util.makedate()
2109 opts['date'] = "%d %d" % util.makedate()
2110
2110
2111 def new(ui, repo, patch, *args, **opts):
2111 def new(ui, repo, patch, *args, **opts):
2112 """create a new patch
2112 """create a new patch
2113
2113
2114 qnew creates a new patch on top of the currently-applied patch (if
2114 qnew creates a new patch on top of the currently-applied patch (if
2115 any). The patch will be initialized with any outstanding changes
2115 any). The patch will be initialized with any outstanding changes
2116 in the working directory. You may also use -I/--include,
2116 in the working directory. You may also use -I/--include,
2117 -X/--exclude, and/or a list of files after the patch name to add
2117 -X/--exclude, and/or a list of files after the patch name to add
2118 only changes to matching files to the new patch, leaving the rest
2118 only changes to matching files to the new patch, leaving the rest
2119 as uncommitted modifications.
2119 as uncommitted modifications.
2120
2120
2121 -u/--user and -d/--date can be used to set the (given) user and
2121 -u/--user and -d/--date can be used to set the (given) user and
2122 date, respectively. -U/--currentuser and -D/--currentdate set user
2122 date, respectively. -U/--currentuser and -D/--currentdate set user
2123 to current user and date to current date.
2123 to current user and date to current date.
2124
2124
2125 -e/--edit, -m/--message or -l/--logfile set the patch header as
2125 -e/--edit, -m/--message or -l/--logfile set the patch header as
2126 well as the commit message. If none is specified, the header is
2126 well as the commit message. If none is specified, the header is
2127 empty and the commit message is '[mq]: PATCH'.
2127 empty and the commit message is '[mq]: PATCH'.
2128
2128
2129 Use the -g/--git option to keep the patch in the git extended diff
2129 Use the -g/--git option to keep the patch in the git extended diff
2130 format. Read the diffs help topic for more information on why this
2130 format. Read the diffs help topic for more information on why this
2131 is important for preserving permission changes and copy/rename
2131 is important for preserving permission changes and copy/rename
2132 information.
2132 information.
2133
2133
2134 Returns 0 on successful creation of a new patch.
2134 Returns 0 on successful creation of a new patch.
2135 """
2135 """
2136 msg = cmdutil.logmessage(opts)
2136 msg = cmdutil.logmessage(opts)
2137 def getmsg():
2137 def getmsg():
2138 return ui.edit(msg, opts.get('user') or ui.username())
2138 return ui.edit(msg, opts.get('user') or ui.username())
2139 q = repo.mq
2139 q = repo.mq
2140 opts['msg'] = msg
2140 opts['msg'] = msg
2141 if opts.get('edit'):
2141 if opts.get('edit'):
2142 opts['msg'] = getmsg
2142 opts['msg'] = getmsg
2143 else:
2143 else:
2144 opts['msg'] = msg
2144 opts['msg'] = msg
2145 setupheaderopts(ui, opts)
2145 setupheaderopts(ui, opts)
2146 q.new(repo, patch, *args, **opts)
2146 q.new(repo, patch, *args, **opts)
2147 q.save_dirty()
2147 q.save_dirty()
2148 return 0
2148 return 0
2149
2149
2150 def refresh(ui, repo, *pats, **opts):
2150 def refresh(ui, repo, *pats, **opts):
2151 """update the current patch
2151 """update the current patch
2152
2152
2153 If any file patterns are provided, the refreshed patch will
2153 If any file patterns are provided, the refreshed patch will
2154 contain only the modifications that match those patterns; the
2154 contain only the modifications that match those patterns; the
2155 remaining modifications will remain in the working directory.
2155 remaining modifications will remain in the working directory.
2156
2156
2157 If -s/--short is specified, files currently included in the patch
2157 If -s/--short is specified, files currently included in the patch
2158 will be refreshed just like matched files and remain in the patch.
2158 will be refreshed just like matched files and remain in the patch.
2159
2159
2160 If -e/--edit is specified, Mercurial will start your configured editor for
2160 If -e/--edit is specified, Mercurial will start your configured editor for
2161 you to enter a message. In case qrefresh fails, you will find a backup of
2161 you to enter a message. In case qrefresh fails, you will find a backup of
2162 your message in ``.hg/last-message.txt``.
2162 your message in ``.hg/last-message.txt``.
2163
2163
2164 hg add/remove/copy/rename work as usual, though you might want to
2164 hg add/remove/copy/rename work as usual, though you might want to
2165 use git-style patches (-g/--git or [diff] git=1) to track copies
2165 use git-style patches (-g/--git or [diff] git=1) to track copies
2166 and renames. See the diffs help topic for more information on the
2166 and renames. See the diffs help topic for more information on the
2167 git diff format.
2167 git diff format.
2168
2168
2169 Returns 0 on success.
2169 Returns 0 on success.
2170 """
2170 """
2171 q = repo.mq
2171 q = repo.mq
2172 message = cmdutil.logmessage(opts)
2172 message = cmdutil.logmessage(opts)
2173 if opts.get('edit'):
2173 if opts.get('edit'):
2174 if not q.applied:
2174 if not q.applied:
2175 ui.write(_("no patches applied\n"))
2175 ui.write(_("no patches applied\n"))
2176 return 1
2176 return 1
2177 if message:
2177 if message:
2178 raise util.Abort(_('option "-e" incompatible with "-m" or "-l"'))
2178 raise util.Abort(_('option "-e" incompatible with "-m" or "-l"'))
2179 patch = q.applied[-1].name
2179 patch = q.applied[-1].name
2180 ph = patchheader(q.join(patch), q.plainmode)
2180 ph = patchheader(q.join(patch), q.plainmode)
2181 message = ui.edit('\n'.join(ph.message), ph.user or ui.username())
2181 message = ui.edit('\n'.join(ph.message), ph.user or ui.username())
2182 # We don't want to lose the patch message if qrefresh fails (issue2062)
2182 # We don't want to lose the patch message if qrefresh fails (issue2062)
2183 msgfile = repo.opener('last-message.txt', 'wb')
2183 msgfile = repo.opener('last-message.txt', 'wb')
2184 msgfile.write(message)
2184 msgfile.write(message)
2185 msgfile.close()
2185 msgfile.close()
2186 setupheaderopts(ui, opts)
2186 setupheaderopts(ui, opts)
2187 ret = q.refresh(repo, pats, msg=message, **opts)
2187 ret = q.refresh(repo, pats, msg=message, **opts)
2188 q.save_dirty()
2188 q.save_dirty()
2189 return ret
2189 return ret
2190
2190
2191 def diff(ui, repo, *pats, **opts):
2191 def diff(ui, repo, *pats, **opts):
2192 """diff of the current patch and subsequent modifications
2192 """diff of the current patch and subsequent modifications
2193
2193
2194 Shows a diff which includes the current patch as well as any
2194 Shows a diff which includes the current patch as well as any
2195 changes which have been made in the working directory since the
2195 changes which have been made in the working directory since the
2196 last refresh (thus showing what the current patch would become
2196 last refresh (thus showing what the current patch would become
2197 after a qrefresh).
2197 after a qrefresh).
2198
2198
2199 Use :hg:`diff` if you only want to see the changes made since the
2199 Use :hg:`diff` if you only want to see the changes made since the
2200 last qrefresh, or :hg:`export qtip` if you want to see changes
2200 last qrefresh, or :hg:`export qtip` if you want to see changes
2201 made by the current patch without including changes made since the
2201 made by the current patch without including changes made since the
2202 qrefresh.
2202 qrefresh.
2203
2203
2204 Returns 0 on success.
2204 Returns 0 on success.
2205 """
2205 """
2206 repo.mq.diff(repo, pats, opts)
2206 repo.mq.diff(repo, pats, opts)
2207 return 0
2207 return 0
2208
2208
2209 def fold(ui, repo, *files, **opts):
2209 def fold(ui, repo, *files, **opts):
2210 """fold the named patches into the current patch
2210 """fold the named patches into the current patch
2211
2211
2212 Patches must not yet be applied. Each patch will be successively
2212 Patches must not yet be applied. Each patch will be successively
2213 applied to the current patch in the order given. If all the
2213 applied to the current patch in the order given. If all the
2214 patches apply successfully, the current patch will be refreshed
2214 patches apply successfully, the current patch will be refreshed
2215 with the new cumulative patch, and the folded patches will be
2215 with the new cumulative patch, and the folded patches will be
2216 deleted. With -k/--keep, the folded patch files will not be
2216 deleted. With -k/--keep, the folded patch files will not be
2217 removed afterwards.
2217 removed afterwards.
2218
2218
2219 The header for each folded patch will be concatenated with the
2219 The header for each folded patch will be concatenated with the
2220 current patch header, separated by a line of ``* * *``.
2220 current patch header, separated by a line of ``* * *``.
2221
2221
2222 Returns 0 on success."""
2222 Returns 0 on success."""
2223
2223
2224 q = repo.mq
2224 q = repo.mq
2225
2225
2226 if not files:
2226 if not files:
2227 raise util.Abort(_('qfold requires at least one patch name'))
2227 raise util.Abort(_('qfold requires at least one patch name'))
2228 if not q.check_toppatch(repo)[0]:
2228 if not q.check_toppatch(repo)[0]:
2229 raise util.Abort(_('no patches applied'))
2229 raise util.Abort(_('no patches applied'))
2230 q.check_localchanges(repo)
2230 q.check_localchanges(repo)
2231
2231
2232 message = cmdutil.logmessage(opts)
2232 message = cmdutil.logmessage(opts)
2233 if opts.get('edit'):
2233 if opts.get('edit'):
2234 if message:
2234 if message:
2235 raise util.Abort(_('option "-e" incompatible with "-m" or "-l"'))
2235 raise util.Abort(_('option "-e" incompatible with "-m" or "-l"'))
2236
2236
2237 parent = q.lookup('qtip')
2237 parent = q.lookup('qtip')
2238 patches = []
2238 patches = []
2239 messages = []
2239 messages = []
2240 for f in files:
2240 for f in files:
2241 p = q.lookup(f)
2241 p = q.lookup(f)
2242 if p in patches or p == parent:
2242 if p in patches or p == parent:
2243 ui.warn(_('Skipping already folded patch %s\n') % p)
2243 ui.warn(_('Skipping already folded patch %s\n') % p)
2244 if q.isapplied(p):
2244 if q.isapplied(p):
2245 raise util.Abort(_('qfold cannot fold already applied patch %s') % p)
2245 raise util.Abort(_('qfold cannot fold already applied patch %s') % p)
2246 patches.append(p)
2246 patches.append(p)
2247
2247
2248 for p in patches:
2248 for p in patches:
2249 if not message:
2249 if not message:
2250 ph = patchheader(q.join(p), q.plainmode)
2250 ph = patchheader(q.join(p), q.plainmode)
2251 if ph.message:
2251 if ph.message:
2252 messages.append(ph.message)
2252 messages.append(ph.message)
2253 pf = q.join(p)
2253 pf = q.join(p)
2254 (patchsuccess, files, fuzz) = q.patch(repo, pf)
2254 (patchsuccess, files, fuzz) = q.patch(repo, pf)
2255 if not patchsuccess:
2255 if not patchsuccess:
2256 raise util.Abort(_('error folding patch %s') % p)
2256 raise util.Abort(_('error folding patch %s') % p)
2257 cmdutil.updatedir(ui, repo, files)
2257 cmdutil.updatedir(ui, repo, files)
2258
2258
2259 if not message:
2259 if not message:
2260 ph = patchheader(q.join(parent), q.plainmode)
2260 ph = patchheader(q.join(parent), q.plainmode)
2261 message, user = ph.message, ph.user
2261 message, user = ph.message, ph.user
2262 for msg in messages:
2262 for msg in messages:
2263 message.append('* * *')
2263 message.append('* * *')
2264 message.extend(msg)
2264 message.extend(msg)
2265 message = '\n'.join(message)
2265 message = '\n'.join(message)
2266
2266
2267 if opts.get('edit'):
2267 if opts.get('edit'):
2268 message = ui.edit(message, user or ui.username())
2268 message = ui.edit(message, user or ui.username())
2269
2269
2270 diffopts = q.patchopts(q.diffopts(), *patches)
2270 diffopts = q.patchopts(q.diffopts(), *patches)
2271 q.refresh(repo, msg=message, git=diffopts.git)
2271 q.refresh(repo, msg=message, git=diffopts.git)
2272 q.delete(repo, patches, opts)
2272 q.delete(repo, patches, opts)
2273 q.save_dirty()
2273 q.save_dirty()
2274
2274
2275 def goto(ui, repo, patch, **opts):
2275 def goto(ui, repo, patch, **opts):
2276 '''push or pop patches until named patch is at top of stack
2276 '''push or pop patches until named patch is at top of stack
2277
2277
2278 Returns 0 on success.'''
2278 Returns 0 on success.'''
2279 q = repo.mq
2279 q = repo.mq
2280 patch = q.lookup(patch)
2280 patch = q.lookup(patch)
2281 if q.isapplied(patch):
2281 if q.isapplied(patch):
2282 ret = q.pop(repo, patch, force=opts.get('force'))
2282 ret = q.pop(repo, patch, force=opts.get('force'))
2283 else:
2283 else:
2284 ret = q.push(repo, patch, force=opts.get('force'))
2284 ret = q.push(repo, patch, force=opts.get('force'))
2285 q.save_dirty()
2285 q.save_dirty()
2286 return ret
2286 return ret
2287
2287
2288 def guard(ui, repo, *args, **opts):
2288 def guard(ui, repo, *args, **opts):
2289 '''set or print guards for a patch
2289 '''set or print guards for a patch
2290
2290
2291 Guards control whether a patch can be pushed. A patch with no
2291 Guards control whether a patch can be pushed. A patch with no
2292 guards is always pushed. A patch with a positive guard ("+foo") is
2292 guards is always pushed. A patch with a positive guard ("+foo") is
2293 pushed only if the :hg:`qselect` command has activated it. A patch with
2293 pushed only if the :hg:`qselect` command has activated it. A patch with
2294 a negative guard ("-foo") is never pushed if the :hg:`qselect` command
2294 a negative guard ("-foo") is never pushed if the :hg:`qselect` command
2295 has activated it.
2295 has activated it.
2296
2296
2297 With no arguments, print the currently active guards.
2297 With no arguments, print the currently active guards.
2298 With arguments, set guards for the named patch.
2298 With arguments, set guards for the named patch.
2299
2299
2300 .. note::
2300 .. note::
2301 Specifying negative guards now requires '--'.
2301 Specifying negative guards now requires '--'.
2302
2302
2303 To set guards on another patch::
2303 To set guards on another patch::
2304
2304
2305 hg qguard other.patch -- +2.6.17 -stable
2305 hg qguard other.patch -- +2.6.17 -stable
2306
2306
2307 Returns 0 on success.
2307 Returns 0 on success.
2308 '''
2308 '''
2309 def status(idx):
2309 def status(idx):
2310 guards = q.series_guards[idx] or ['unguarded']
2310 guards = q.series_guards[idx] or ['unguarded']
2311 if q.series[idx] in applied:
2311 if q.series[idx] in applied:
2312 state = 'applied'
2312 state = 'applied'
2313 elif q.pushable(idx)[0]:
2313 elif q.pushable(idx)[0]:
2314 state = 'unapplied'
2314 state = 'unapplied'
2315 else:
2315 else:
2316 state = 'guarded'
2316 state = 'guarded'
2317 label = 'qguard.patch qguard.%s qseries.%s' % (state, state)
2317 label = 'qguard.patch qguard.%s qseries.%s' % (state, state)
2318 ui.write('%s: ' % ui.label(q.series[idx], label))
2318 ui.write('%s: ' % ui.label(q.series[idx], label))
2319
2319
2320 for i, guard in enumerate(guards):
2320 for i, guard in enumerate(guards):
2321 if guard.startswith('+'):
2321 if guard.startswith('+'):
2322 ui.write(guard, label='qguard.positive')
2322 ui.write(guard, label='qguard.positive')
2323 elif guard.startswith('-'):
2323 elif guard.startswith('-'):
2324 ui.write(guard, label='qguard.negative')
2324 ui.write(guard, label='qguard.negative')
2325 else:
2325 else:
2326 ui.write(guard, label='qguard.unguarded')
2326 ui.write(guard, label='qguard.unguarded')
2327 if i != len(guards) - 1:
2327 if i != len(guards) - 1:
2328 ui.write(' ')
2328 ui.write(' ')
2329 ui.write('\n')
2329 ui.write('\n')
2330 q = repo.mq
2330 q = repo.mq
2331 applied = set(p.name for p in q.applied)
2331 applied = set(p.name for p in q.applied)
2332 patch = None
2332 patch = None
2333 args = list(args)
2333 args = list(args)
2334 if opts.get('list'):
2334 if opts.get('list'):
2335 if args or opts.get('none'):
2335 if args or opts.get('none'):
2336 raise util.Abort(_('cannot mix -l/--list with options or arguments'))
2336 raise util.Abort(_('cannot mix -l/--list with options or arguments'))
2337 for i in xrange(len(q.series)):
2337 for i in xrange(len(q.series)):
2338 status(i)
2338 status(i)
2339 return
2339 return
2340 if not args or args[0][0:1] in '-+':
2340 if not args or args[0][0:1] in '-+':
2341 if not q.applied:
2341 if not q.applied:
2342 raise util.Abort(_('no patches applied'))
2342 raise util.Abort(_('no patches applied'))
2343 patch = q.applied[-1].name
2343 patch = q.applied[-1].name
2344 if patch is None and args[0][0:1] not in '-+':
2344 if patch is None and args[0][0:1] not in '-+':
2345 patch = args.pop(0)
2345 patch = args.pop(0)
2346 if patch is None:
2346 if patch is None:
2347 raise util.Abort(_('no patch to work with'))
2347 raise util.Abort(_('no patch to work with'))
2348 if args or opts.get('none'):
2348 if args or opts.get('none'):
2349 idx = q.find_series(patch)
2349 idx = q.find_series(patch)
2350 if idx is None:
2350 if idx is None:
2351 raise util.Abort(_('no patch named %s') % patch)
2351 raise util.Abort(_('no patch named %s') % patch)
2352 q.set_guards(idx, args)
2352 q.set_guards(idx, args)
2353 q.save_dirty()
2353 q.save_dirty()
2354 else:
2354 else:
2355 status(q.series.index(q.lookup(patch)))
2355 status(q.series.index(q.lookup(patch)))
2356
2356
2357 def header(ui, repo, patch=None):
2357 def header(ui, repo, patch=None):
2358 """print the header of the topmost or specified patch
2358 """print the header of the topmost or specified patch
2359
2359
2360 Returns 0 on success."""
2360 Returns 0 on success."""
2361 q = repo.mq
2361 q = repo.mq
2362
2362
2363 if patch:
2363 if patch:
2364 patch = q.lookup(patch)
2364 patch = q.lookup(patch)
2365 else:
2365 else:
2366 if not q.applied:
2366 if not q.applied:
2367 ui.write(_('no patches applied\n'))
2367 ui.write(_('no patches applied\n'))
2368 return 1
2368 return 1
2369 patch = q.lookup('qtip')
2369 patch = q.lookup('qtip')
2370 ph = patchheader(q.join(patch), q.plainmode)
2370 ph = patchheader(q.join(patch), q.plainmode)
2371
2371
2372 ui.write('\n'.join(ph.message) + '\n')
2372 ui.write('\n'.join(ph.message) + '\n')
2373
2373
2374 def lastsavename(path):
2374 def lastsavename(path):
2375 (directory, base) = os.path.split(path)
2375 (directory, base) = os.path.split(path)
2376 names = os.listdir(directory)
2376 names = os.listdir(directory)
2377 namere = re.compile("%s.([0-9]+)" % base)
2377 namere = re.compile("%s.([0-9]+)" % base)
2378 maxindex = None
2378 maxindex = None
2379 maxname = None
2379 maxname = None
2380 for f in names:
2380 for f in names:
2381 m = namere.match(f)
2381 m = namere.match(f)
2382 if m:
2382 if m:
2383 index = int(m.group(1))
2383 index = int(m.group(1))
2384 if maxindex is None or index > maxindex:
2384 if maxindex is None or index > maxindex:
2385 maxindex = index
2385 maxindex = index
2386 maxname = f
2386 maxname = f
2387 if maxname:
2387 if maxname:
2388 return (os.path.join(directory, maxname), maxindex)
2388 return (os.path.join(directory, maxname), maxindex)
2389 return (None, None)
2389 return (None, None)
2390
2390
2391 def savename(path):
2391 def savename(path):
2392 (last, index) = lastsavename(path)
2392 (last, index) = lastsavename(path)
2393 if last is None:
2393 if last is None:
2394 index = 0
2394 index = 0
2395 newpath = path + ".%d" % (index + 1)
2395 newpath = path + ".%d" % (index + 1)
2396 return newpath
2396 return newpath
2397
2397
2398 def push(ui, repo, patch=None, **opts):
2398 def push(ui, repo, patch=None, **opts):
2399 """push the next patch onto the stack
2399 """push the next patch onto the stack
2400
2400
2401 When -f/--force is applied, all local changes in patched files
2401 When -f/--force is applied, all local changes in patched files
2402 will be lost.
2402 will be lost.
2403
2403
2404 Return 0 on success.
2404 Return 0 on success.
2405 """
2405 """
2406 q = repo.mq
2406 q = repo.mq
2407 mergeq = None
2407 mergeq = None
2408
2408
2409 if opts.get('merge'):
2409 if opts.get('merge'):
2410 if opts.get('name'):
2410 if opts.get('name'):
2411 newpath = repo.join(opts.get('name'))
2411 newpath = repo.join(opts.get('name'))
2412 else:
2412 else:
2413 newpath, i = lastsavename(q.path)
2413 newpath, i = lastsavename(q.path)
2414 if not newpath:
2414 if not newpath:
2415 ui.warn(_("no saved queues found, please use -n\n"))
2415 ui.warn(_("no saved queues found, please use -n\n"))
2416 return 1
2416 return 1
2417 mergeq = queue(ui, repo.join(""), newpath)
2417 mergeq = queue(ui, repo.join(""), newpath)
2418 ui.warn(_("merging with queue at: %s\n") % mergeq.path)
2418 ui.warn(_("merging with queue at: %s\n") % mergeq.path)
2419 ret = q.push(repo, patch, force=opts.get('force'), list=opts.get('list'),
2419 ret = q.push(repo, patch, force=opts.get('force'), list=opts.get('list'),
2420 mergeq=mergeq, all=opts.get('all'), move=opts.get('move'),
2420 mergeq=mergeq, all=opts.get('all'), move=opts.get('move'),
2421 exact=opts.get('exact'))
2421 exact=opts.get('exact'))
2422 return ret
2422 return ret
2423
2423
2424 def pop(ui, repo, patch=None, **opts):
2424 def pop(ui, repo, patch=None, **opts):
2425 """pop the current patch off the stack
2425 """pop the current patch off the stack
2426
2426
2427 By default, pops off the top of the patch stack. If given a patch
2427 By default, pops off the top of the patch stack. If given a patch
2428 name, keeps popping off patches until the named patch is at the
2428 name, keeps popping off patches until the named patch is at the
2429 top of the stack.
2429 top of the stack.
2430
2430
2431 Return 0 on success.
2431 Return 0 on success.
2432 """
2432 """
2433 localupdate = True
2433 localupdate = True
2434 if opts.get('name'):
2434 if opts.get('name'):
2435 q = queue(ui, repo.join(""), repo.join(opts.get('name')))
2435 q = queue(ui, repo.join(""), repo.join(opts.get('name')))
2436 ui.warn(_('using patch queue: %s\n') % q.path)
2436 ui.warn(_('using patch queue: %s\n') % q.path)
2437 localupdate = False
2437 localupdate = False
2438 else:
2438 else:
2439 q = repo.mq
2439 q = repo.mq
2440 ret = q.pop(repo, patch, force=opts.get('force'), update=localupdate,
2440 ret = q.pop(repo, patch, force=opts.get('force'), update=localupdate,
2441 all=opts.get('all'))
2441 all=opts.get('all'))
2442 q.save_dirty()
2442 q.save_dirty()
2443 return ret
2443 return ret
2444
2444
2445 def rename(ui, repo, patch, name=None, **opts):
2445 def rename(ui, repo, patch, name=None, **opts):
2446 """rename a patch
2446 """rename a patch
2447
2447
2448 With one argument, renames the current patch to PATCH1.
2448 With one argument, renames the current patch to PATCH1.
2449 With two arguments, renames PATCH1 to PATCH2.
2449 With two arguments, renames PATCH1 to PATCH2.
2450
2450
2451 Returns 0 on success."""
2451 Returns 0 on success."""
2452
2452
2453 q = repo.mq
2453 q = repo.mq
2454
2454
2455 if not name:
2455 if not name:
2456 name = patch
2456 name = patch
2457 patch = None
2457 patch = None
2458
2458
2459 if patch:
2459 if patch:
2460 patch = q.lookup(patch)
2460 patch = q.lookup(patch)
2461 else:
2461 else:
2462 if not q.applied:
2462 if not q.applied:
2463 ui.write(_('no patches applied\n'))
2463 ui.write(_('no patches applied\n'))
2464 return
2464 return
2465 patch = q.lookup('qtip')
2465 patch = q.lookup('qtip')
2466 absdest = q.join(name)
2466 absdest = q.join(name)
2467 if os.path.isdir(absdest):
2467 if os.path.isdir(absdest):
2468 name = normname(os.path.join(name, os.path.basename(patch)))
2468 name = normname(os.path.join(name, os.path.basename(patch)))
2469 absdest = q.join(name)
2469 absdest = q.join(name)
2470 if os.path.exists(absdest):
2470 if os.path.exists(absdest):
2471 raise util.Abort(_('%s already exists') % absdest)
2471 raise util.Abort(_('%s already exists') % absdest)
2472
2472
2473 if name in q.series:
2473 if name in q.series:
2474 raise util.Abort(
2474 raise util.Abort(
2475 _('A patch named %s already exists in the series file') % name)
2475 _('A patch named %s already exists in the series file') % name)
2476
2476
2477 ui.note(_('renaming %s to %s\n') % (patch, name))
2477 ui.note(_('renaming %s to %s\n') % (patch, name))
2478 i = q.find_series(patch)
2478 i = q.find_series(patch)
2479 guards = q.guard_re.findall(q.full_series[i])
2479 guards = q.guard_re.findall(q.full_series[i])
2480 q.full_series[i] = name + ''.join([' #' + g for g in guards])
2480 q.full_series[i] = name + ''.join([' #' + g for g in guards])
2481 q.parse_series()
2481 q.parse_series()
2482 q.series_dirty = 1
2482 q.series_dirty = 1
2483
2483
2484 info = q.isapplied(patch)
2484 info = q.isapplied(patch)
2485 if info:
2485 if info:
2486 q.applied[info[0]] = statusentry(info[1], name)
2486 q.applied[info[0]] = statusentry(info[1], name)
2487 q.applied_dirty = 1
2487 q.applied_dirty = 1
2488
2488
2489 destdir = os.path.dirname(absdest)
2489 destdir = os.path.dirname(absdest)
2490 if not os.path.isdir(destdir):
2490 if not os.path.isdir(destdir):
2491 os.makedirs(destdir)
2491 os.makedirs(destdir)
2492 util.rename(q.join(patch), absdest)
2492 util.rename(q.join(patch), absdest)
2493 r = q.qrepo()
2493 r = q.qrepo()
2494 if r and patch in r.dirstate:
2494 if r and patch in r.dirstate:
2495 wctx = r[None]
2495 wctx = r[None]
2496 wlock = r.wlock()
2496 wlock = r.wlock()
2497 try:
2497 try:
2498 if r.dirstate[patch] == 'a':
2498 if r.dirstate[patch] == 'a':
2499 r.dirstate.forget(patch)
2499 r.dirstate.forget(patch)
2500 r.dirstate.add(name)
2500 r.dirstate.add(name)
2501 else:
2501 else:
2502 if r.dirstate[name] == 'r':
2502 if r.dirstate[name] == 'r':
2503 wctx.undelete([name])
2503 wctx.undelete([name])
2504 wctx.copy(patch, name)
2504 wctx.copy(patch, name)
2505 wctx.remove([patch], False)
2505 wctx.remove([patch], False)
2506 finally:
2506 finally:
2507 wlock.release()
2507 wlock.release()
2508
2508
2509 q.save_dirty()
2509 q.save_dirty()
2510
2510
2511 def restore(ui, repo, rev, **opts):
2511 def restore(ui, repo, rev, **opts):
2512 """restore the queue state saved by a revision (DEPRECATED)
2512 """restore the queue state saved by a revision (DEPRECATED)
2513
2513
2514 This command is deprecated, use :hg:`rebase` instead."""
2514 This command is deprecated, use :hg:`rebase` instead."""
2515 rev = repo.lookup(rev)
2515 rev = repo.lookup(rev)
2516 q = repo.mq
2516 q = repo.mq
2517 q.restore(repo, rev, delete=opts.get('delete'),
2517 q.restore(repo, rev, delete=opts.get('delete'),
2518 qupdate=opts.get('update'))
2518 qupdate=opts.get('update'))
2519 q.save_dirty()
2519 q.save_dirty()
2520 return 0
2520 return 0
2521
2521
2522 def save(ui, repo, **opts):
2522 def save(ui, repo, **opts):
2523 """save current queue state (DEPRECATED)
2523 """save current queue state (DEPRECATED)
2524
2524
2525 This command is deprecated, use :hg:`rebase` instead."""
2525 This command is deprecated, use :hg:`rebase` instead."""
2526 q = repo.mq
2526 q = repo.mq
2527 message = cmdutil.logmessage(opts)
2527 message = cmdutil.logmessage(opts)
2528 ret = q.save(repo, msg=message)
2528 ret = q.save(repo, msg=message)
2529 if ret:
2529 if ret:
2530 return ret
2530 return ret
2531 q.save_dirty()
2531 q.save_dirty()
2532 if opts.get('copy'):
2532 if opts.get('copy'):
2533 path = q.path
2533 path = q.path
2534 if opts.get('name'):
2534 if opts.get('name'):
2535 newpath = os.path.join(q.basepath, opts.get('name'))
2535 newpath = os.path.join(q.basepath, opts.get('name'))
2536 if os.path.exists(newpath):
2536 if os.path.exists(newpath):
2537 if not os.path.isdir(newpath):
2537 if not os.path.isdir(newpath):
2538 raise util.Abort(_('destination %s exists and is not '
2538 raise util.Abort(_('destination %s exists and is not '
2539 'a directory') % newpath)
2539 'a directory') % newpath)
2540 if not opts.get('force'):
2540 if not opts.get('force'):
2541 raise util.Abort(_('destination %s exists, '
2541 raise util.Abort(_('destination %s exists, '
2542 'use -f to force') % newpath)
2542 'use -f to force') % newpath)
2543 else:
2543 else:
2544 newpath = savename(path)
2544 newpath = savename(path)
2545 ui.warn(_("copy %s to %s\n") % (path, newpath))
2545 ui.warn(_("copy %s to %s\n") % (path, newpath))
2546 util.copyfiles(path, newpath)
2546 util.copyfiles(path, newpath)
2547 if opts.get('empty'):
2547 if opts.get('empty'):
2548 try:
2548 try:
2549 os.unlink(q.join(q.status_path))
2549 os.unlink(q.join(q.status_path))
2550 except:
2550 except:
2551 pass
2551 pass
2552 return 0
2552 return 0
2553
2553
2554 def strip(ui, repo, *revs, **opts):
2554 def strip(ui, repo, *revs, **opts):
2555 """strip changesets and all their descendants from the repository
2555 """strip changesets and all their descendants from the repository
2556
2556
2557 The strip command removes the specified changesets and all their
2557 The strip command removes the specified changesets and all their
2558 descendants. If the working directory has uncommitted changes,
2558 descendants. If the working directory has uncommitted changes, the
2559 the operation is aborted unless the --force flag is supplied.
2559 operation is aborted unless the --force flag is supplied, in which
2560 case changes will be discarded.
2560
2561
2561 If a parent of the working directory is stripped, then the working
2562 If a parent of the working directory is stripped, then the working
2562 directory will automatically be updated to the most recent
2563 directory will automatically be updated to the most recent
2563 available ancestor of the stripped parent after the operation
2564 available ancestor of the stripped parent after the operation
2564 completes.
2565 completes.
2565
2566
2566 Any stripped changesets are stored in ``.hg/strip-backup`` as a
2567 Any stripped changesets are stored in ``.hg/strip-backup`` as a
2567 bundle (see :hg:`help bundle` and :hg:`help unbundle`). They can
2568 bundle (see :hg:`help bundle` and :hg:`help unbundle`). They can
2568 be restored by running :hg:`unbundle .hg/strip-backup/BUNDLE`,
2569 be restored by running :hg:`unbundle .hg/strip-backup/BUNDLE`,
2569 where BUNDLE is the bundle file created by the strip. Note that
2570 where BUNDLE is the bundle file created by the strip. Note that
2570 the local revision numbers will in general be different after the
2571 the local revision numbers will in general be different after the
2571 restore.
2572 restore.
2572
2573
2573 Use the --no-backup option to discard the backup bundle once the
2574 Use the --no-backup option to discard the backup bundle once the
2574 operation completes.
2575 operation completes.
2575
2576
2576 Return 0 on success.
2577 Return 0 on success.
2577 """
2578 """
2578 backup = 'all'
2579 backup = 'all'
2579 if opts.get('backup'):
2580 if opts.get('backup'):
2580 backup = 'strip'
2581 backup = 'strip'
2581 elif opts.get('no_backup') or opts.get('nobackup'):
2582 elif opts.get('no_backup') or opts.get('nobackup'):
2582 backup = 'none'
2583 backup = 'none'
2583
2584
2584 cl = repo.changelog
2585 cl = repo.changelog
2585 revs = set(cmdutil.revrange(repo, revs))
2586 revs = set(cmdutil.revrange(repo, revs))
2586 if not revs:
2587 if not revs:
2587 raise util.Abort(_('empty revision set'))
2588 raise util.Abort(_('empty revision set'))
2588
2589
2589 descendants = set(cl.descendants(*revs))
2590 descendants = set(cl.descendants(*revs))
2590 strippedrevs = revs.union(descendants)
2591 strippedrevs = revs.union(descendants)
2591 roots = revs.difference(descendants)
2592 roots = revs.difference(descendants)
2592
2593
2593 update = False
2594 update = False
2594 # if one of the wdir parent is stripped we'll need
2595 # if one of the wdir parent is stripped we'll need
2595 # to update away to an earlier revision
2596 # to update away to an earlier revision
2596 for p in repo.dirstate.parents():
2597 for p in repo.dirstate.parents():
2597 if p != nullid and cl.rev(p) in strippedrevs:
2598 if p != nullid and cl.rev(p) in strippedrevs:
2598 update = True
2599 update = True
2599 break
2600 break
2600
2601
2601 rootnodes = set(cl.node(r) for r in roots)
2602 rootnodes = set(cl.node(r) for r in roots)
2602
2603
2603 q = repo.mq
2604 q = repo.mq
2604 if q.applied:
2605 if q.applied:
2605 # refresh queue state if we're about to strip
2606 # refresh queue state if we're about to strip
2606 # applied patches
2607 # applied patches
2607 if cl.rev(repo.lookup('qtip')) in strippedrevs:
2608 if cl.rev(repo.lookup('qtip')) in strippedrevs:
2608 q.applied_dirty = True
2609 q.applied_dirty = True
2609 start = 0
2610 start = 0
2610 end = len(q.applied)
2611 end = len(q.applied)
2611 for i, statusentry in enumerate(q.applied):
2612 for i, statusentry in enumerate(q.applied):
2612 if statusentry.node in rootnodes:
2613 if statusentry.node in rootnodes:
2613 # if one of the stripped roots is an applied
2614 # if one of the stripped roots is an applied
2614 # patch, only part of the queue is stripped
2615 # patch, only part of the queue is stripped
2615 start = i
2616 start = i
2616 break
2617 break
2617 del q.applied[start:end]
2618 del q.applied[start:end]
2618 q.save_dirty()
2619 q.save_dirty()
2619
2620
2620 revs = list(rootnodes)
2621 revs = list(rootnodes)
2621 if update and opts.get('keep'):
2622 if update and opts.get('keep'):
2622 wlock = repo.wlock()
2623 wlock = repo.wlock()
2623 try:
2624 try:
2624 urev = repo.mq.qparents(repo, revs[0])
2625 urev = repo.mq.qparents(repo, revs[0])
2625 repo.dirstate.rebuild(urev, repo[urev].manifest())
2626 repo.dirstate.rebuild(urev, repo[urev].manifest())
2626 repo.dirstate.write()
2627 repo.dirstate.write()
2627 update = False
2628 update = False
2628 finally:
2629 finally:
2629 wlock.release()
2630 wlock.release()
2630
2631
2631 repo.mq.strip(repo, revs, backup=backup, update=update,
2632 repo.mq.strip(repo, revs, backup=backup, update=update,
2632 force=opts.get('force'))
2633 force=opts.get('force'))
2633 return 0
2634 return 0
2634
2635
2635 def select(ui, repo, *args, **opts):
2636 def select(ui, repo, *args, **opts):
2636 '''set or print guarded patches to push
2637 '''set or print guarded patches to push
2637
2638
2638 Use the :hg:`qguard` command to set or print guards on patch, then use
2639 Use the :hg:`qguard` command to set or print guards on patch, then use
2639 qselect to tell mq which guards to use. A patch will be pushed if
2640 qselect to tell mq which guards to use. A patch will be pushed if
2640 it has no guards or any positive guards match the currently
2641 it has no guards or any positive guards match the currently
2641 selected guard, but will not be pushed if any negative guards
2642 selected guard, but will not be pushed if any negative guards
2642 match the current guard. For example::
2643 match the current guard. For example::
2643
2644
2644 qguard foo.patch -- -stable (negative guard)
2645 qguard foo.patch -- -stable (negative guard)
2645 qguard bar.patch +stable (positive guard)
2646 qguard bar.patch +stable (positive guard)
2646 qselect stable
2647 qselect stable
2647
2648
2648 This activates the "stable" guard. mq will skip foo.patch (because
2649 This activates the "stable" guard. mq will skip foo.patch (because
2649 it has a negative match) but push bar.patch (because it has a
2650 it has a negative match) but push bar.patch (because it has a
2650 positive match).
2651 positive match).
2651
2652
2652 With no arguments, prints the currently active guards.
2653 With no arguments, prints the currently active guards.
2653 With one argument, sets the active guard.
2654 With one argument, sets the active guard.
2654
2655
2655 Use -n/--none to deactivate guards (no other arguments needed).
2656 Use -n/--none to deactivate guards (no other arguments needed).
2656 When no guards are active, patches with positive guards are
2657 When no guards are active, patches with positive guards are
2657 skipped and patches with negative guards are pushed.
2658 skipped and patches with negative guards are pushed.
2658
2659
2659 qselect can change the guards on applied patches. It does not pop
2660 qselect can change the guards on applied patches. It does not pop
2660 guarded patches by default. Use --pop to pop back to the last
2661 guarded patches by default. Use --pop to pop back to the last
2661 applied patch that is not guarded. Use --reapply (which implies
2662 applied patch that is not guarded. Use --reapply (which implies
2662 --pop) to push back to the current patch afterwards, but skip
2663 --pop) to push back to the current patch afterwards, but skip
2663 guarded patches.
2664 guarded patches.
2664
2665
2665 Use -s/--series to print a list of all guards in the series file
2666 Use -s/--series to print a list of all guards in the series file
2666 (no other arguments needed). Use -v for more information.
2667 (no other arguments needed). Use -v for more information.
2667
2668
2668 Returns 0 on success.'''
2669 Returns 0 on success.'''
2669
2670
2670 q = repo.mq
2671 q = repo.mq
2671 guards = q.active()
2672 guards = q.active()
2672 if args or opts.get('none'):
2673 if args or opts.get('none'):
2673 old_unapplied = q.unapplied(repo)
2674 old_unapplied = q.unapplied(repo)
2674 old_guarded = [i for i in xrange(len(q.applied)) if
2675 old_guarded = [i for i in xrange(len(q.applied)) if
2675 not q.pushable(i)[0]]
2676 not q.pushable(i)[0]]
2676 q.set_active(args)
2677 q.set_active(args)
2677 q.save_dirty()
2678 q.save_dirty()
2678 if not args:
2679 if not args:
2679 ui.status(_('guards deactivated\n'))
2680 ui.status(_('guards deactivated\n'))
2680 if not opts.get('pop') and not opts.get('reapply'):
2681 if not opts.get('pop') and not opts.get('reapply'):
2681 unapplied = q.unapplied(repo)
2682 unapplied = q.unapplied(repo)
2682 guarded = [i for i in xrange(len(q.applied))
2683 guarded = [i for i in xrange(len(q.applied))
2683 if not q.pushable(i)[0]]
2684 if not q.pushable(i)[0]]
2684 if len(unapplied) != len(old_unapplied):
2685 if len(unapplied) != len(old_unapplied):
2685 ui.status(_('number of unguarded, unapplied patches has '
2686 ui.status(_('number of unguarded, unapplied patches has '
2686 'changed from %d to %d\n') %
2687 'changed from %d to %d\n') %
2687 (len(old_unapplied), len(unapplied)))
2688 (len(old_unapplied), len(unapplied)))
2688 if len(guarded) != len(old_guarded):
2689 if len(guarded) != len(old_guarded):
2689 ui.status(_('number of guarded, applied patches has changed '
2690 ui.status(_('number of guarded, applied patches has changed '
2690 'from %d to %d\n') %
2691 'from %d to %d\n') %
2691 (len(old_guarded), len(guarded)))
2692 (len(old_guarded), len(guarded)))
2692 elif opts.get('series'):
2693 elif opts.get('series'):
2693 guards = {}
2694 guards = {}
2694 noguards = 0
2695 noguards = 0
2695 for gs in q.series_guards:
2696 for gs in q.series_guards:
2696 if not gs:
2697 if not gs:
2697 noguards += 1
2698 noguards += 1
2698 for g in gs:
2699 for g in gs:
2699 guards.setdefault(g, 0)
2700 guards.setdefault(g, 0)
2700 guards[g] += 1
2701 guards[g] += 1
2701 if ui.verbose:
2702 if ui.verbose:
2702 guards['NONE'] = noguards
2703 guards['NONE'] = noguards
2703 guards = guards.items()
2704 guards = guards.items()
2704 guards.sort(key=lambda x: x[0][1:])
2705 guards.sort(key=lambda x: x[0][1:])
2705 if guards:
2706 if guards:
2706 ui.note(_('guards in series file:\n'))
2707 ui.note(_('guards in series file:\n'))
2707 for guard, count in guards:
2708 for guard, count in guards:
2708 ui.note('%2d ' % count)
2709 ui.note('%2d ' % count)
2709 ui.write(guard, '\n')
2710 ui.write(guard, '\n')
2710 else:
2711 else:
2711 ui.note(_('no guards in series file\n'))
2712 ui.note(_('no guards in series file\n'))
2712 else:
2713 else:
2713 if guards:
2714 if guards:
2714 ui.note(_('active guards:\n'))
2715 ui.note(_('active guards:\n'))
2715 for g in guards:
2716 for g in guards:
2716 ui.write(g, '\n')
2717 ui.write(g, '\n')
2717 else:
2718 else:
2718 ui.write(_('no active guards\n'))
2719 ui.write(_('no active guards\n'))
2719 reapply = opts.get('reapply') and q.applied and q.appliedname(-1)
2720 reapply = opts.get('reapply') and q.applied and q.appliedname(-1)
2720 popped = False
2721 popped = False
2721 if opts.get('pop') or opts.get('reapply'):
2722 if opts.get('pop') or opts.get('reapply'):
2722 for i in xrange(len(q.applied)):
2723 for i in xrange(len(q.applied)):
2723 pushable, reason = q.pushable(i)
2724 pushable, reason = q.pushable(i)
2724 if not pushable:
2725 if not pushable:
2725 ui.status(_('popping guarded patches\n'))
2726 ui.status(_('popping guarded patches\n'))
2726 popped = True
2727 popped = True
2727 if i == 0:
2728 if i == 0:
2728 q.pop(repo, all=True)
2729 q.pop(repo, all=True)
2729 else:
2730 else:
2730 q.pop(repo, i - 1)
2731 q.pop(repo, i - 1)
2731 break
2732 break
2732 if popped:
2733 if popped:
2733 try:
2734 try:
2734 if reapply:
2735 if reapply:
2735 ui.status(_('reapplying unguarded patches\n'))
2736 ui.status(_('reapplying unguarded patches\n'))
2736 q.push(repo, reapply)
2737 q.push(repo, reapply)
2737 finally:
2738 finally:
2738 q.save_dirty()
2739 q.save_dirty()
2739
2740
2740 def finish(ui, repo, *revrange, **opts):
2741 def finish(ui, repo, *revrange, **opts):
2741 """move applied patches into repository history
2742 """move applied patches into repository history
2742
2743
2743 Finishes the specified revisions (corresponding to applied
2744 Finishes the specified revisions (corresponding to applied
2744 patches) by moving them out of mq control into regular repository
2745 patches) by moving them out of mq control into regular repository
2745 history.
2746 history.
2746
2747
2747 Accepts a revision range or the -a/--applied option. If --applied
2748 Accepts a revision range or the -a/--applied option. If --applied
2748 is specified, all applied mq revisions are removed from mq
2749 is specified, all applied mq revisions are removed from mq
2749 control. Otherwise, the given revisions must be at the base of the
2750 control. Otherwise, the given revisions must be at the base of the
2750 stack of applied patches.
2751 stack of applied patches.
2751
2752
2752 This can be especially useful if your changes have been applied to
2753 This can be especially useful if your changes have been applied to
2753 an upstream repository, or if you are about to push your changes
2754 an upstream repository, or if you are about to push your changes
2754 to upstream.
2755 to upstream.
2755
2756
2756 Returns 0 on success.
2757 Returns 0 on success.
2757 """
2758 """
2758 if not opts.get('applied') and not revrange:
2759 if not opts.get('applied') and not revrange:
2759 raise util.Abort(_('no revisions specified'))
2760 raise util.Abort(_('no revisions specified'))
2760 elif opts.get('applied'):
2761 elif opts.get('applied'):
2761 revrange = ('qbase::qtip',) + revrange
2762 revrange = ('qbase::qtip',) + revrange
2762
2763
2763 q = repo.mq
2764 q = repo.mq
2764 if not q.applied:
2765 if not q.applied:
2765 ui.status(_('no patches applied\n'))
2766 ui.status(_('no patches applied\n'))
2766 return 0
2767 return 0
2767
2768
2768 revs = cmdutil.revrange(repo, revrange)
2769 revs = cmdutil.revrange(repo, revrange)
2769 q.finish(repo, revs)
2770 q.finish(repo, revs)
2770 q.save_dirty()
2771 q.save_dirty()
2771 return 0
2772 return 0
2772
2773
2773 def qqueue(ui, repo, name=None, **opts):
2774 def qqueue(ui, repo, name=None, **opts):
2774 '''manage multiple patch queues
2775 '''manage multiple patch queues
2775
2776
2776 Supports switching between different patch queues, as well as creating
2777 Supports switching between different patch queues, as well as creating
2777 new patch queues and deleting existing ones.
2778 new patch queues and deleting existing ones.
2778
2779
2779 Omitting a queue name or specifying -l/--list will show you the registered
2780 Omitting a queue name or specifying -l/--list will show you the registered
2780 queues - by default the "normal" patches queue is registered. The currently
2781 queues - by default the "normal" patches queue is registered. The currently
2781 active queue will be marked with "(active)".
2782 active queue will be marked with "(active)".
2782
2783
2783 To create a new queue, use -c/--create. The queue is automatically made
2784 To create a new queue, use -c/--create. The queue is automatically made
2784 active, except in the case where there are applied patches from the
2785 active, except in the case where there are applied patches from the
2785 currently active queue in the repository. Then the queue will only be
2786 currently active queue in the repository. Then the queue will only be
2786 created and switching will fail.
2787 created and switching will fail.
2787
2788
2788 To delete an existing queue, use --delete. You cannot delete the currently
2789 To delete an existing queue, use --delete. You cannot delete the currently
2789 active queue.
2790 active queue.
2790
2791
2791 Returns 0 on success.
2792 Returns 0 on success.
2792 '''
2793 '''
2793
2794
2794 q = repo.mq
2795 q = repo.mq
2795
2796
2796 _defaultqueue = 'patches'
2797 _defaultqueue = 'patches'
2797 _allqueues = 'patches.queues'
2798 _allqueues = 'patches.queues'
2798 _activequeue = 'patches.queue'
2799 _activequeue = 'patches.queue'
2799
2800
2800 def _getcurrent():
2801 def _getcurrent():
2801 cur = os.path.basename(q.path)
2802 cur = os.path.basename(q.path)
2802 if cur.startswith('patches-'):
2803 if cur.startswith('patches-'):
2803 cur = cur[8:]
2804 cur = cur[8:]
2804 return cur
2805 return cur
2805
2806
2806 def _noqueues():
2807 def _noqueues():
2807 try:
2808 try:
2808 fh = repo.opener(_allqueues, 'r')
2809 fh = repo.opener(_allqueues, 'r')
2809 fh.close()
2810 fh.close()
2810 except IOError:
2811 except IOError:
2811 return True
2812 return True
2812
2813
2813 return False
2814 return False
2814
2815
2815 def _getqueues():
2816 def _getqueues():
2816 current = _getcurrent()
2817 current = _getcurrent()
2817
2818
2818 try:
2819 try:
2819 fh = repo.opener(_allqueues, 'r')
2820 fh = repo.opener(_allqueues, 'r')
2820 queues = [queue.strip() for queue in fh if queue.strip()]
2821 queues = [queue.strip() for queue in fh if queue.strip()]
2821 fh.close()
2822 fh.close()
2822 if current not in queues:
2823 if current not in queues:
2823 queues.append(current)
2824 queues.append(current)
2824 except IOError:
2825 except IOError:
2825 queues = [_defaultqueue]
2826 queues = [_defaultqueue]
2826
2827
2827 return sorted(queues)
2828 return sorted(queues)
2828
2829
2829 def _setactive(name):
2830 def _setactive(name):
2830 if q.applied:
2831 if q.applied:
2831 raise util.Abort(_('patches applied - cannot set new queue active'))
2832 raise util.Abort(_('patches applied - cannot set new queue active'))
2832 _setactivenocheck(name)
2833 _setactivenocheck(name)
2833
2834
2834 def _setactivenocheck(name):
2835 def _setactivenocheck(name):
2835 fh = repo.opener(_activequeue, 'w')
2836 fh = repo.opener(_activequeue, 'w')
2836 if name != 'patches':
2837 if name != 'patches':
2837 fh.write(name)
2838 fh.write(name)
2838 fh.close()
2839 fh.close()
2839
2840
2840 def _addqueue(name):
2841 def _addqueue(name):
2841 fh = repo.opener(_allqueues, 'a')
2842 fh = repo.opener(_allqueues, 'a')
2842 fh.write('%s\n' % (name,))
2843 fh.write('%s\n' % (name,))
2843 fh.close()
2844 fh.close()
2844
2845
2845 def _queuedir(name):
2846 def _queuedir(name):
2846 if name == 'patches':
2847 if name == 'patches':
2847 return repo.join('patches')
2848 return repo.join('patches')
2848 else:
2849 else:
2849 return repo.join('patches-' + name)
2850 return repo.join('patches-' + name)
2850
2851
2851 def _validname(name):
2852 def _validname(name):
2852 for n in name:
2853 for n in name:
2853 if n in ':\\/.':
2854 if n in ':\\/.':
2854 return False
2855 return False
2855 return True
2856 return True
2856
2857
2857 def _delete(name):
2858 def _delete(name):
2858 if name not in existing:
2859 if name not in existing:
2859 raise util.Abort(_('cannot delete queue that does not exist'))
2860 raise util.Abort(_('cannot delete queue that does not exist'))
2860
2861
2861 current = _getcurrent()
2862 current = _getcurrent()
2862
2863
2863 if name == current:
2864 if name == current:
2864 raise util.Abort(_('cannot delete currently active queue'))
2865 raise util.Abort(_('cannot delete currently active queue'))
2865
2866
2866 fh = repo.opener('patches.queues.new', 'w')
2867 fh = repo.opener('patches.queues.new', 'w')
2867 for queue in existing:
2868 for queue in existing:
2868 if queue == name:
2869 if queue == name:
2869 continue
2870 continue
2870 fh.write('%s\n' % (queue,))
2871 fh.write('%s\n' % (queue,))
2871 fh.close()
2872 fh.close()
2872 util.rename(repo.join('patches.queues.new'), repo.join(_allqueues))
2873 util.rename(repo.join('patches.queues.new'), repo.join(_allqueues))
2873
2874
2874 if not name or opts.get('list'):
2875 if not name or opts.get('list'):
2875 current = _getcurrent()
2876 current = _getcurrent()
2876 for queue in _getqueues():
2877 for queue in _getqueues():
2877 ui.write('%s' % (queue,))
2878 ui.write('%s' % (queue,))
2878 if queue == current and not ui.quiet:
2879 if queue == current and not ui.quiet:
2879 ui.write(_(' (active)\n'))
2880 ui.write(_(' (active)\n'))
2880 else:
2881 else:
2881 ui.write('\n')
2882 ui.write('\n')
2882 return
2883 return
2883
2884
2884 if not _validname(name):
2885 if not _validname(name):
2885 raise util.Abort(
2886 raise util.Abort(
2886 _('invalid queue name, may not contain the characters ":\\/."'))
2887 _('invalid queue name, may not contain the characters ":\\/."'))
2887
2888
2888 existing = _getqueues()
2889 existing = _getqueues()
2889
2890
2890 if opts.get('create'):
2891 if opts.get('create'):
2891 if name in existing:
2892 if name in existing:
2892 raise util.Abort(_('queue "%s" already exists') % name)
2893 raise util.Abort(_('queue "%s" already exists') % name)
2893 if _noqueues():
2894 if _noqueues():
2894 _addqueue(_defaultqueue)
2895 _addqueue(_defaultqueue)
2895 _addqueue(name)
2896 _addqueue(name)
2896 _setactive(name)
2897 _setactive(name)
2897 elif opts.get('rename'):
2898 elif opts.get('rename'):
2898 current = _getcurrent()
2899 current = _getcurrent()
2899 if name == current:
2900 if name == current:
2900 raise util.Abort(_('can\'t rename "%s" to its current name') % name)
2901 raise util.Abort(_('can\'t rename "%s" to its current name') % name)
2901 if name in existing:
2902 if name in existing:
2902 raise util.Abort(_('queue "%s" already exists') % name)
2903 raise util.Abort(_('queue "%s" already exists') % name)
2903
2904
2904 olddir = _queuedir(current)
2905 olddir = _queuedir(current)
2905 newdir = _queuedir(name)
2906 newdir = _queuedir(name)
2906
2907
2907 if os.path.exists(newdir):
2908 if os.path.exists(newdir):
2908 raise util.Abort(_('non-queue directory "%s" already exists') %
2909 raise util.Abort(_('non-queue directory "%s" already exists') %
2909 newdir)
2910 newdir)
2910
2911
2911 fh = repo.opener('patches.queues.new', 'w')
2912 fh = repo.opener('patches.queues.new', 'w')
2912 for queue in existing:
2913 for queue in existing:
2913 if queue == current:
2914 if queue == current:
2914 fh.write('%s\n' % (name,))
2915 fh.write('%s\n' % (name,))
2915 if os.path.exists(olddir):
2916 if os.path.exists(olddir):
2916 util.rename(olddir, newdir)
2917 util.rename(olddir, newdir)
2917 else:
2918 else:
2918 fh.write('%s\n' % (queue,))
2919 fh.write('%s\n' % (queue,))
2919 fh.close()
2920 fh.close()
2920 util.rename(repo.join('patches.queues.new'), repo.join(_allqueues))
2921 util.rename(repo.join('patches.queues.new'), repo.join(_allqueues))
2921 _setactivenocheck(name)
2922 _setactivenocheck(name)
2922 elif opts.get('delete'):
2923 elif opts.get('delete'):
2923 _delete(name)
2924 _delete(name)
2924 elif opts.get('purge'):
2925 elif opts.get('purge'):
2925 if name in existing:
2926 if name in existing:
2926 _delete(name)
2927 _delete(name)
2927 qdir = _queuedir(name)
2928 qdir = _queuedir(name)
2928 if os.path.exists(qdir):
2929 if os.path.exists(qdir):
2929 shutil.rmtree(qdir)
2930 shutil.rmtree(qdir)
2930 else:
2931 else:
2931 if name not in existing:
2932 if name not in existing:
2932 raise util.Abort(_('use --create to create a new queue'))
2933 raise util.Abort(_('use --create to create a new queue'))
2933 _setactive(name)
2934 _setactive(name)
2934
2935
2935 def reposetup(ui, repo):
2936 def reposetup(ui, repo):
2936 class mqrepo(repo.__class__):
2937 class mqrepo(repo.__class__):
2937 @util.propertycache
2938 @util.propertycache
2938 def mq(self):
2939 def mq(self):
2939 return queue(self.ui, self.join(""))
2940 return queue(self.ui, self.join(""))
2940
2941
2941 def abort_if_wdir_patched(self, errmsg, force=False):
2942 def abort_if_wdir_patched(self, errmsg, force=False):
2942 if self.mq.applied and not force:
2943 if self.mq.applied and not force:
2943 parents = self.dirstate.parents()
2944 parents = self.dirstate.parents()
2944 patches = [s.node for s in self.mq.applied]
2945 patches = [s.node for s in self.mq.applied]
2945 if parents[0] in patches or parents[1] in patches:
2946 if parents[0] in patches or parents[1] in patches:
2946 raise util.Abort(errmsg)
2947 raise util.Abort(errmsg)
2947
2948
2948 def commit(self, text="", user=None, date=None, match=None,
2949 def commit(self, text="", user=None, date=None, match=None,
2949 force=False, editor=False, extra={}):
2950 force=False, editor=False, extra={}):
2950 self.abort_if_wdir_patched(
2951 self.abort_if_wdir_patched(
2951 _('cannot commit over an applied mq patch'),
2952 _('cannot commit over an applied mq patch'),
2952 force)
2953 force)
2953
2954
2954 return super(mqrepo, self).commit(text, user, date, match, force,
2955 return super(mqrepo, self).commit(text, user, date, match, force,
2955 editor, extra)
2956 editor, extra)
2956
2957
2957 def checkpush(self, force, revs):
2958 def checkpush(self, force, revs):
2958 if self.mq.applied and not force:
2959 if self.mq.applied and not force:
2959 haspatches = True
2960 haspatches = True
2960 if revs:
2961 if revs:
2961 # Assume applied patches have no non-patch descendants
2962 # Assume applied patches have no non-patch descendants
2962 # and are not on remote already. If they appear in the
2963 # and are not on remote already. If they appear in the
2963 # set of resolved 'revs', bail out.
2964 # set of resolved 'revs', bail out.
2964 applied = set(e.node for e in self.mq.applied)
2965 applied = set(e.node for e in self.mq.applied)
2965 haspatches = bool([n for n in revs if n in applied])
2966 haspatches = bool([n for n in revs if n in applied])
2966 if haspatches:
2967 if haspatches:
2967 raise util.Abort(_('source has mq patches applied'))
2968 raise util.Abort(_('source has mq patches applied'))
2968 super(mqrepo, self).checkpush(force, revs)
2969 super(mqrepo, self).checkpush(force, revs)
2969
2970
2970 def _findtags(self):
2971 def _findtags(self):
2971 '''augment tags from base class with patch tags'''
2972 '''augment tags from base class with patch tags'''
2972 result = super(mqrepo, self)._findtags()
2973 result = super(mqrepo, self)._findtags()
2973
2974
2974 q = self.mq
2975 q = self.mq
2975 if not q.applied:
2976 if not q.applied:
2976 return result
2977 return result
2977
2978
2978 mqtags = [(patch.node, patch.name) for patch in q.applied]
2979 mqtags = [(patch.node, patch.name) for patch in q.applied]
2979
2980
2980 try:
2981 try:
2981 r = self.changelog.rev(mqtags[-1][0])
2982 r = self.changelog.rev(mqtags[-1][0])
2982 except error.RepoLookupError:
2983 except error.RepoLookupError:
2983 self.ui.warn(_('mq status file refers to unknown node %s\n')
2984 self.ui.warn(_('mq status file refers to unknown node %s\n')
2984 % short(mqtags[-1][0]))
2985 % short(mqtags[-1][0]))
2985 return result
2986 return result
2986
2987
2987 mqtags.append((mqtags[-1][0], 'qtip'))
2988 mqtags.append((mqtags[-1][0], 'qtip'))
2988 mqtags.append((mqtags[0][0], 'qbase'))
2989 mqtags.append((mqtags[0][0], 'qbase'))
2989 mqtags.append((self.changelog.parents(mqtags[0][0])[0], 'qparent'))
2990 mqtags.append((self.changelog.parents(mqtags[0][0])[0], 'qparent'))
2990 tags = result[0]
2991 tags = result[0]
2991 for patch in mqtags:
2992 for patch in mqtags:
2992 if patch[1] in tags:
2993 if patch[1] in tags:
2993 self.ui.warn(_('Tag %s overrides mq patch of the same name\n')
2994 self.ui.warn(_('Tag %s overrides mq patch of the same name\n')
2994 % patch[1])
2995 % patch[1])
2995 else:
2996 else:
2996 tags[patch[1]] = patch[0]
2997 tags[patch[1]] = patch[0]
2997
2998
2998 return result
2999 return result
2999
3000
3000 def _branchtags(self, partial, lrev):
3001 def _branchtags(self, partial, lrev):
3001 q = self.mq
3002 q = self.mq
3002 if not q.applied:
3003 if not q.applied:
3003 return super(mqrepo, self)._branchtags(partial, lrev)
3004 return super(mqrepo, self)._branchtags(partial, lrev)
3004
3005
3005 cl = self.changelog
3006 cl = self.changelog
3006 qbasenode = q.applied[0].node
3007 qbasenode = q.applied[0].node
3007 try:
3008 try:
3008 qbase = cl.rev(qbasenode)
3009 qbase = cl.rev(qbasenode)
3009 except error.LookupError:
3010 except error.LookupError:
3010 self.ui.warn(_('mq status file refers to unknown node %s\n')
3011 self.ui.warn(_('mq status file refers to unknown node %s\n')
3011 % short(qbasenode))
3012 % short(qbasenode))
3012 return super(mqrepo, self)._branchtags(partial, lrev)
3013 return super(mqrepo, self)._branchtags(partial, lrev)
3013
3014
3014 start = lrev + 1
3015 start = lrev + 1
3015 if start < qbase:
3016 if start < qbase:
3016 # update the cache (excluding the patches) and save it
3017 # update the cache (excluding the patches) and save it
3017 ctxgen = (self[r] for r in xrange(lrev + 1, qbase))
3018 ctxgen = (self[r] for r in xrange(lrev + 1, qbase))
3018 self._updatebranchcache(partial, ctxgen)
3019 self._updatebranchcache(partial, ctxgen)
3019 self._writebranchcache(partial, cl.node(qbase - 1), qbase - 1)
3020 self._writebranchcache(partial, cl.node(qbase - 1), qbase - 1)
3020 start = qbase
3021 start = qbase
3021 # if start = qbase, the cache is as updated as it should be.
3022 # if start = qbase, the cache is as updated as it should be.
3022 # if start > qbase, the cache includes (part of) the patches.
3023 # if start > qbase, the cache includes (part of) the patches.
3023 # we might as well use it, but we won't save it.
3024 # we might as well use it, but we won't save it.
3024
3025
3025 # update the cache up to the tip
3026 # update the cache up to the tip
3026 ctxgen = (self[r] for r in xrange(start, len(cl)))
3027 ctxgen = (self[r] for r in xrange(start, len(cl)))
3027 self._updatebranchcache(partial, ctxgen)
3028 self._updatebranchcache(partial, ctxgen)
3028
3029
3029 return partial
3030 return partial
3030
3031
3031 if repo.local():
3032 if repo.local():
3032 repo.__class__ = mqrepo
3033 repo.__class__ = mqrepo
3033
3034
3034 def mqimport(orig, ui, repo, *args, **kwargs):
3035 def mqimport(orig, ui, repo, *args, **kwargs):
3035 if (hasattr(repo, 'abort_if_wdir_patched')
3036 if (hasattr(repo, 'abort_if_wdir_patched')
3036 and not kwargs.get('no_commit', False)):
3037 and not kwargs.get('no_commit', False)):
3037 repo.abort_if_wdir_patched(_('cannot import over an applied patch'),
3038 repo.abort_if_wdir_patched(_('cannot import over an applied patch'),
3038 kwargs.get('force'))
3039 kwargs.get('force'))
3039 return orig(ui, repo, *args, **kwargs)
3040 return orig(ui, repo, *args, **kwargs)
3040
3041
3041 def mqinit(orig, ui, *args, **kwargs):
3042 def mqinit(orig, ui, *args, **kwargs):
3042 mq = kwargs.pop('mq', None)
3043 mq = kwargs.pop('mq', None)
3043
3044
3044 if not mq:
3045 if not mq:
3045 return orig(ui, *args, **kwargs)
3046 return orig(ui, *args, **kwargs)
3046
3047
3047 if args:
3048 if args:
3048 repopath = args[0]
3049 repopath = args[0]
3049 if not hg.islocal(repopath):
3050 if not hg.islocal(repopath):
3050 raise util.Abort(_('only a local queue repository '
3051 raise util.Abort(_('only a local queue repository '
3051 'may be initialized'))
3052 'may be initialized'))
3052 else:
3053 else:
3053 repopath = cmdutil.findrepo(os.getcwd())
3054 repopath = cmdutil.findrepo(os.getcwd())
3054 if not repopath:
3055 if not repopath:
3055 raise util.Abort(_('there is no Mercurial repository here '
3056 raise util.Abort(_('there is no Mercurial repository here '
3056 '(.hg not found)'))
3057 '(.hg not found)'))
3057 repo = hg.repository(ui, repopath)
3058 repo = hg.repository(ui, repopath)
3058 return qinit(ui, repo, True)
3059 return qinit(ui, repo, True)
3059
3060
3060 def mqcommand(orig, ui, repo, *args, **kwargs):
3061 def mqcommand(orig, ui, repo, *args, **kwargs):
3061 """Add --mq option to operate on patch repository instead of main"""
3062 """Add --mq option to operate on patch repository instead of main"""
3062
3063
3063 # some commands do not like getting unknown options
3064 # some commands do not like getting unknown options
3064 mq = kwargs.pop('mq', None)
3065 mq = kwargs.pop('mq', None)
3065
3066
3066 if not mq:
3067 if not mq:
3067 return orig(ui, repo, *args, **kwargs)
3068 return orig(ui, repo, *args, **kwargs)
3068
3069
3069 q = repo.mq
3070 q = repo.mq
3070 r = q.qrepo()
3071 r = q.qrepo()
3071 if not r:
3072 if not r:
3072 raise util.Abort(_('no queue repository'))
3073 raise util.Abort(_('no queue repository'))
3073 return orig(r.ui, r, *args, **kwargs)
3074 return orig(r.ui, r, *args, **kwargs)
3074
3075
3075 def summary(orig, ui, repo, *args, **kwargs):
3076 def summary(orig, ui, repo, *args, **kwargs):
3076 r = orig(ui, repo, *args, **kwargs)
3077 r = orig(ui, repo, *args, **kwargs)
3077 q = repo.mq
3078 q = repo.mq
3078 m = []
3079 m = []
3079 a, u = len(q.applied), len(q.unapplied(repo))
3080 a, u = len(q.applied), len(q.unapplied(repo))
3080 if a:
3081 if a:
3081 m.append(ui.label(_("%d applied"), 'qseries.applied') % a)
3082 m.append(ui.label(_("%d applied"), 'qseries.applied') % a)
3082 if u:
3083 if u:
3083 m.append(ui.label(_("%d unapplied"), 'qseries.unapplied') % u)
3084 m.append(ui.label(_("%d unapplied"), 'qseries.unapplied') % u)
3084 if m:
3085 if m:
3085 ui.write("mq: %s\n" % ', '.join(m))
3086 ui.write("mq: %s\n" % ', '.join(m))
3086 else:
3087 else:
3087 ui.note(_("mq: (empty queue)\n"))
3088 ui.note(_("mq: (empty queue)\n"))
3088 return r
3089 return r
3089
3090
3090 def uisetup(ui):
3091 def uisetup(ui):
3091 mqopt = [('', 'mq', None, _("operate on patch repository"))]
3092 mqopt = [('', 'mq', None, _("operate on patch repository"))]
3092
3093
3093 extensions.wrapcommand(commands.table, 'import', mqimport)
3094 extensions.wrapcommand(commands.table, 'import', mqimport)
3094 extensions.wrapcommand(commands.table, 'summary', summary)
3095 extensions.wrapcommand(commands.table, 'summary', summary)
3095
3096
3096 entry = extensions.wrapcommand(commands.table, 'init', mqinit)
3097 entry = extensions.wrapcommand(commands.table, 'init', mqinit)
3097 entry[1].extend(mqopt)
3098 entry[1].extend(mqopt)
3098
3099
3099 nowrap = set(commands.norepo.split(" ") + ['qrecord'])
3100 nowrap = set(commands.norepo.split(" ") + ['qrecord'])
3100
3101
3101 def dotable(cmdtable):
3102 def dotable(cmdtable):
3102 for cmd in cmdtable.keys():
3103 for cmd in cmdtable.keys():
3103 cmd = cmdutil.parsealiases(cmd)[0]
3104 cmd = cmdutil.parsealiases(cmd)[0]
3104 if cmd in nowrap:
3105 if cmd in nowrap:
3105 continue
3106 continue
3106 entry = extensions.wrapcommand(cmdtable, cmd, mqcommand)
3107 entry = extensions.wrapcommand(cmdtable, cmd, mqcommand)
3107 entry[1].extend(mqopt)
3108 entry[1].extend(mqopt)
3108
3109
3109 dotable(commands.table)
3110 dotable(commands.table)
3110
3111
3111 for extname, extmodule in extensions.extensions():
3112 for extname, extmodule in extensions.extensions():
3112 if extmodule.__file__ != __file__:
3113 if extmodule.__file__ != __file__:
3113 dotable(getattr(extmodule, 'cmdtable', {}))
3114 dotable(getattr(extmodule, 'cmdtable', {}))
3114
3115
3115 seriesopts = [('s', 'summary', None, _('print first line of patch header'))]
3116 seriesopts = [('s', 'summary', None, _('print first line of patch header'))]
3116
3117
3117 cmdtable = {
3118 cmdtable = {
3118 "qapplied":
3119 "qapplied":
3119 (applied,
3120 (applied,
3120 [('1', 'last', None, _('show only the last patch'))] + seriesopts,
3121 [('1', 'last', None, _('show only the last patch'))] + seriesopts,
3121 _('hg qapplied [-1] [-s] [PATCH]')),
3122 _('hg qapplied [-1] [-s] [PATCH]')),
3122 "qclone":
3123 "qclone":
3123 (clone,
3124 (clone,
3124 [('', 'pull', None, _('use pull protocol to copy metadata')),
3125 [('', 'pull', None, _('use pull protocol to copy metadata')),
3125 ('U', 'noupdate', None, _('do not update the new working directories')),
3126 ('U', 'noupdate', None, _('do not update the new working directories')),
3126 ('', 'uncompressed', None,
3127 ('', 'uncompressed', None,
3127 _('use uncompressed transfer (fast over LAN)')),
3128 _('use uncompressed transfer (fast over LAN)')),
3128 ('p', 'patches', '',
3129 ('p', 'patches', '',
3129 _('location of source patch repository'), _('REPO')),
3130 _('location of source patch repository'), _('REPO')),
3130 ] + commands.remoteopts,
3131 ] + commands.remoteopts,
3131 _('hg qclone [OPTION]... SOURCE [DEST]')),
3132 _('hg qclone [OPTION]... SOURCE [DEST]')),
3132 "qcommit|qci":
3133 "qcommit|qci":
3133 (commit,
3134 (commit,
3134 commands.table["^commit|ci"][1],
3135 commands.table["^commit|ci"][1],
3135 _('hg qcommit [OPTION]... [FILE]...')),
3136 _('hg qcommit [OPTION]... [FILE]...')),
3136 "^qdiff":
3137 "^qdiff":
3137 (diff,
3138 (diff,
3138 commands.diffopts + commands.diffopts2 + commands.walkopts,
3139 commands.diffopts + commands.diffopts2 + commands.walkopts,
3139 _('hg qdiff [OPTION]... [FILE]...')),
3140 _('hg qdiff [OPTION]... [FILE]...')),
3140 "qdelete|qremove|qrm":
3141 "qdelete|qremove|qrm":
3141 (delete,
3142 (delete,
3142 [('k', 'keep', None, _('keep patch file')),
3143 [('k', 'keep', None, _('keep patch file')),
3143 ('r', 'rev', [],
3144 ('r', 'rev', [],
3144 _('stop managing a revision (DEPRECATED)'), _('REV'))],
3145 _('stop managing a revision (DEPRECATED)'), _('REV'))],
3145 _('hg qdelete [-k] [PATCH]...')),
3146 _('hg qdelete [-k] [PATCH]...')),
3146 'qfold':
3147 'qfold':
3147 (fold,
3148 (fold,
3148 [('e', 'edit', None, _('edit patch header')),
3149 [('e', 'edit', None, _('edit patch header')),
3149 ('k', 'keep', None, _('keep folded patch files')),
3150 ('k', 'keep', None, _('keep folded patch files')),
3150 ] + commands.commitopts,
3151 ] + commands.commitopts,
3151 _('hg qfold [-e] [-k] [-m TEXT] [-l FILE] PATCH...')),
3152 _('hg qfold [-e] [-k] [-m TEXT] [-l FILE] PATCH...')),
3152 'qgoto':
3153 'qgoto':
3153 (goto,
3154 (goto,
3154 [('f', 'force', None, _('overwrite any local changes'))],
3155 [('f', 'force', None, _('overwrite any local changes'))],
3155 _('hg qgoto [OPTION]... PATCH')),
3156 _('hg qgoto [OPTION]... PATCH')),
3156 'qguard':
3157 'qguard':
3157 (guard,
3158 (guard,
3158 [('l', 'list', None, _('list all patches and guards')),
3159 [('l', 'list', None, _('list all patches and guards')),
3159 ('n', 'none', None, _('drop all guards'))],
3160 ('n', 'none', None, _('drop all guards'))],
3160 _('hg qguard [-l] [-n] [PATCH] [-- [+GUARD]... [-GUARD]...]')),
3161 _('hg qguard [-l] [-n] [PATCH] [-- [+GUARD]... [-GUARD]...]')),
3161 'qheader': (header, [], _('hg qheader [PATCH]')),
3162 'qheader': (header, [], _('hg qheader [PATCH]')),
3162 "qimport":
3163 "qimport":
3163 (qimport,
3164 (qimport,
3164 [('e', 'existing', None, _('import file in patch directory')),
3165 [('e', 'existing', None, _('import file in patch directory')),
3165 ('n', 'name', '',
3166 ('n', 'name', '',
3166 _('name of patch file'), _('NAME')),
3167 _('name of patch file'), _('NAME')),
3167 ('f', 'force', None, _('overwrite existing files')),
3168 ('f', 'force', None, _('overwrite existing files')),
3168 ('r', 'rev', [],
3169 ('r', 'rev', [],
3169 _('place existing revisions under mq control'), _('REV')),
3170 _('place existing revisions under mq control'), _('REV')),
3170 ('g', 'git', None, _('use git extended diff format')),
3171 ('g', 'git', None, _('use git extended diff format')),
3171 ('P', 'push', None, _('qpush after importing'))],
3172 ('P', 'push', None, _('qpush after importing'))],
3172 _('hg qimport [-e] [-n NAME] [-f] [-g] [-P] [-r REV]... FILE...')),
3173 _('hg qimport [-e] [-n NAME] [-f] [-g] [-P] [-r REV]... FILE...')),
3173 "^qinit":
3174 "^qinit":
3174 (init,
3175 (init,
3175 [('c', 'create-repo', None, _('create queue repository'))],
3176 [('c', 'create-repo', None, _('create queue repository'))],
3176 _('hg qinit [-c]')),
3177 _('hg qinit [-c]')),
3177 "^qnew":
3178 "^qnew":
3178 (new,
3179 (new,
3179 [('e', 'edit', None, _('edit commit message')),
3180 [('e', 'edit', None, _('edit commit message')),
3180 ('f', 'force', None, _('import uncommitted changes (DEPRECATED)')),
3181 ('f', 'force', None, _('import uncommitted changes (DEPRECATED)')),
3181 ('g', 'git', None, _('use git extended diff format')),
3182 ('g', 'git', None, _('use git extended diff format')),
3182 ('U', 'currentuser', None, _('add "From: <current user>" to patch')),
3183 ('U', 'currentuser', None, _('add "From: <current user>" to patch')),
3183 ('u', 'user', '',
3184 ('u', 'user', '',
3184 _('add "From: <USER>" to patch'), _('USER')),
3185 _('add "From: <USER>" to patch'), _('USER')),
3185 ('D', 'currentdate', None, _('add "Date: <current date>" to patch')),
3186 ('D', 'currentdate', None, _('add "Date: <current date>" to patch')),
3186 ('d', 'date', '',
3187 ('d', 'date', '',
3187 _('add "Date: <DATE>" to patch'), _('DATE'))
3188 _('add "Date: <DATE>" to patch'), _('DATE'))
3188 ] + commands.walkopts + commands.commitopts,
3189 ] + commands.walkopts + commands.commitopts,
3189 _('hg qnew [-e] [-m TEXT] [-l FILE] PATCH [FILE]...')),
3190 _('hg qnew [-e] [-m TEXT] [-l FILE] PATCH [FILE]...')),
3190 "qnext": (next, [] + seriesopts, _('hg qnext [-s]')),
3191 "qnext": (next, [] + seriesopts, _('hg qnext [-s]')),
3191 "qprev": (prev, [] + seriesopts, _('hg qprev [-s]')),
3192 "qprev": (prev, [] + seriesopts, _('hg qprev [-s]')),
3192 "^qpop":
3193 "^qpop":
3193 (pop,
3194 (pop,
3194 [('a', 'all', None, _('pop all patches')),
3195 [('a', 'all', None, _('pop all patches')),
3195 ('n', 'name', '',
3196 ('n', 'name', '',
3196 _('queue name to pop (DEPRECATED)'), _('NAME')),
3197 _('queue name to pop (DEPRECATED)'), _('NAME')),
3197 ('f', 'force', None, _('forget any local changes to patched files'))],
3198 ('f', 'force', None, _('forget any local changes to patched files'))],
3198 _('hg qpop [-a] [-f] [PATCH | INDEX]')),
3199 _('hg qpop [-a] [-f] [PATCH | INDEX]')),
3199 "^qpush":
3200 "^qpush":
3200 (push,
3201 (push,
3201 [('f', 'force', None, _('apply on top of local changes')),
3202 [('f', 'force', None, _('apply on top of local changes')),
3202 ('e', 'exact', None, _('apply the target patch to its recorded parent')),
3203 ('e', 'exact', None, _('apply the target patch to its recorded parent')),
3203 ('l', 'list', None, _('list patch name in commit text')),
3204 ('l', 'list', None, _('list patch name in commit text')),
3204 ('a', 'all', None, _('apply all patches')),
3205 ('a', 'all', None, _('apply all patches')),
3205 ('m', 'merge', None, _('merge from another queue (DEPRECATED)')),
3206 ('m', 'merge', None, _('merge from another queue (DEPRECATED)')),
3206 ('n', 'name', '',
3207 ('n', 'name', '',
3207 _('merge queue name (DEPRECATED)'), _('NAME')),
3208 _('merge queue name (DEPRECATED)'), _('NAME')),
3208 ('', 'move', None, _('reorder patch series and apply only the patch'))],
3209 ('', 'move', None, _('reorder patch series and apply only the patch'))],
3209 _('hg qpush [-f] [-l] [-a] [--move] [PATCH | INDEX]')),
3210 _('hg qpush [-f] [-l] [-a] [--move] [PATCH | INDEX]')),
3210 "^qrefresh":
3211 "^qrefresh":
3211 (refresh,
3212 (refresh,
3212 [('e', 'edit', None, _('edit commit message')),
3213 [('e', 'edit', None, _('edit commit message')),
3213 ('g', 'git', None, _('use git extended diff format')),
3214 ('g', 'git', None, _('use git extended diff format')),
3214 ('s', 'short', None,
3215 ('s', 'short', None,
3215 _('refresh only files already in the patch and specified files')),
3216 _('refresh only files already in the patch and specified files')),
3216 ('U', 'currentuser', None,
3217 ('U', 'currentuser', None,
3217 _('add/update author field in patch with current user')),
3218 _('add/update author field in patch with current user')),
3218 ('u', 'user', '',
3219 ('u', 'user', '',
3219 _('add/update author field in patch with given user'), _('USER')),
3220 _('add/update author field in patch with given user'), _('USER')),
3220 ('D', 'currentdate', None,
3221 ('D', 'currentdate', None,
3221 _('add/update date field in patch with current date')),
3222 _('add/update date field in patch with current date')),
3222 ('d', 'date', '',
3223 ('d', 'date', '',
3223 _('add/update date field in patch with given date'), _('DATE'))
3224 _('add/update date field in patch with given date'), _('DATE'))
3224 ] + commands.walkopts + commands.commitopts,
3225 ] + commands.walkopts + commands.commitopts,
3225 _('hg qrefresh [-I] [-X] [-e] [-m TEXT] [-l FILE] [-s] [FILE]...')),
3226 _('hg qrefresh [-I] [-X] [-e] [-m TEXT] [-l FILE] [-s] [FILE]...')),
3226 'qrename|qmv':
3227 'qrename|qmv':
3227 (rename, [], _('hg qrename PATCH1 [PATCH2]')),
3228 (rename, [], _('hg qrename PATCH1 [PATCH2]')),
3228 "qrestore":
3229 "qrestore":
3229 (restore,
3230 (restore,
3230 [('d', 'delete', None, _('delete save entry')),
3231 [('d', 'delete', None, _('delete save entry')),
3231 ('u', 'update', None, _('update queue working directory'))],
3232 ('u', 'update', None, _('update queue working directory'))],
3232 _('hg qrestore [-d] [-u] REV')),
3233 _('hg qrestore [-d] [-u] REV')),
3233 "qsave":
3234 "qsave":
3234 (save,
3235 (save,
3235 [('c', 'copy', None, _('copy patch directory')),
3236 [('c', 'copy', None, _('copy patch directory')),
3236 ('n', 'name', '',
3237 ('n', 'name', '',
3237 _('copy directory name'), _('NAME')),
3238 _('copy directory name'), _('NAME')),
3238 ('e', 'empty', None, _('clear queue status file')),
3239 ('e', 'empty', None, _('clear queue status file')),
3239 ('f', 'force', None, _('force copy'))] + commands.commitopts,
3240 ('f', 'force', None, _('force copy'))] + commands.commitopts,
3240 _('hg qsave [-m TEXT] [-l FILE] [-c] [-n NAME] [-e] [-f]')),
3241 _('hg qsave [-m TEXT] [-l FILE] [-c] [-n NAME] [-e] [-f]')),
3241 "qselect":
3242 "qselect":
3242 (select,
3243 (select,
3243 [('n', 'none', None, _('disable all guards')),
3244 [('n', 'none', None, _('disable all guards')),
3244 ('s', 'series', None, _('list all guards in series file')),
3245 ('s', 'series', None, _('list all guards in series file')),
3245 ('', 'pop', None, _('pop to before first guarded applied patch')),
3246 ('', 'pop', None, _('pop to before first guarded applied patch')),
3246 ('', 'reapply', None, _('pop, then reapply patches'))],
3247 ('', 'reapply', None, _('pop, then reapply patches'))],
3247 _('hg qselect [OPTION]... [GUARD]...')),
3248 _('hg qselect [OPTION]... [GUARD]...')),
3248 "qseries":
3249 "qseries":
3249 (series,
3250 (series,
3250 [('m', 'missing', None, _('print patches not in series')),
3251 [('m', 'missing', None, _('print patches not in series')),
3251 ] + seriesopts,
3252 ] + seriesopts,
3252 _('hg qseries [-ms]')),
3253 _('hg qseries [-ms]')),
3253 "strip":
3254 "strip":
3254 (strip,
3255 (strip,
3255 [('f', 'force', None, _('force removal of changesets even if the '
3256 [('f', 'force', None, _('force removal of changesets, discard '
3256 'working directory has uncommitted changes')),
3257 'uncommitted changes (no backup)')),
3257 ('b', 'backup', None, _('bundle only changesets with local revision'
3258 ('b', 'backup', None, _('bundle only changesets with local revision'
3258 ' number greater than REV which are not'
3259 ' number greater than REV which are not'
3259 ' descendants of REV (DEPRECATED)')),
3260 ' descendants of REV (DEPRECATED)')),
3260 ('n', 'no-backup', None, _('no backups')),
3261 ('n', 'no-backup', None, _('no backups')),
3261 ('', 'nobackup', None, _('no backups (DEPRECATED)')),
3262 ('', 'nobackup', None, _('no backups (DEPRECATED)')),
3262 ('k', 'keep', None, _("do not modify working copy during strip"))],
3263 ('k', 'keep', None, _("do not modify working copy during strip"))],
3263 _('hg strip [-k] [-f] [-n] REV...')),
3264 _('hg strip [-k] [-f] [-n] REV...')),
3264 "qtop": (top, [] + seriesopts, _('hg qtop [-s]')),
3265 "qtop": (top, [] + seriesopts, _('hg qtop [-s]')),
3265 "qunapplied":
3266 "qunapplied":
3266 (unapplied,
3267 (unapplied,
3267 [('1', 'first', None, _('show only the first patch'))] + seriesopts,
3268 [('1', 'first', None, _('show only the first patch'))] + seriesopts,
3268 _('hg qunapplied [-1] [-s] [PATCH]')),
3269 _('hg qunapplied [-1] [-s] [PATCH]')),
3269 "qfinish":
3270 "qfinish":
3270 (finish,
3271 (finish,
3271 [('a', 'applied', None, _('finish all applied changesets'))],
3272 [('a', 'applied', None, _('finish all applied changesets'))],
3272 _('hg qfinish [-a] [REV]...')),
3273 _('hg qfinish [-a] [REV]...')),
3273 'qqueue':
3274 'qqueue':
3274 (qqueue,
3275 (qqueue,
3275 [
3276 [
3276 ('l', 'list', False, _('list all available queues')),
3277 ('l', 'list', False, _('list all available queues')),
3277 ('c', 'create', False, _('create new queue')),
3278 ('c', 'create', False, _('create new queue')),
3278 ('', 'rename', False, _('rename active queue')),
3279 ('', 'rename', False, _('rename active queue')),
3279 ('', 'delete', False, _('delete reference to queue')),
3280 ('', 'delete', False, _('delete reference to queue')),
3280 ('', 'purge', False, _('delete queue, and remove patch dir')),
3281 ('', 'purge', False, _('delete queue, and remove patch dir')),
3281 ],
3282 ],
3282 _('[OPTION] [QUEUE]')),
3283 _('[OPTION] [QUEUE]')),
3283 }
3284 }
3284
3285
3285 colortable = {'qguard.negative': 'red',
3286 colortable = {'qguard.negative': 'red',
3286 'qguard.positive': 'yellow',
3287 'qguard.positive': 'yellow',
3287 'qguard.unguarded': 'green',
3288 'qguard.unguarded': 'green',
3288 'qseries.applied': 'blue bold underline',
3289 'qseries.applied': 'blue bold underline',
3289 'qseries.guarded': 'black bold',
3290 'qseries.guarded': 'black bold',
3290 'qseries.missing': 'red bold',
3291 'qseries.missing': 'red bold',
3291 'qseries.unapplied': 'black bold'}
3292 'qseries.unapplied': 'black bold'}
General Comments 0
You need to be logged in to leave comments. Login now