##// END OF EJS Templates
mq: replace `add` call with newer API...
marmoute -
r48556:7a06e04c default
parent child Browse files
Show More
@@ -1,4311 +1,4315
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 behavior can be configured with::
31 files creations or deletions. This behavior 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 It may be desirable for mq changesets to be kept in the secret phase (see
41 It may be desirable for mq changesets to be kept in the secret phase (see
42 :hg:`help phases`), which can be enabled with the following setting::
42 :hg:`help phases`), which can be enabled with the following setting::
43
43
44 [mq]
44 [mq]
45 secret = True
45 secret = True
46
46
47 You will by default be managing a patch queue named "patches". You can
47 You will by default be managing a patch queue named "patches". You can
48 create other, independent patch queues with the :hg:`qqueue` command.
48 create other, independent patch queues with the :hg:`qqueue` command.
49
49
50 If the working directory contains uncommitted files, qpush, qpop and
50 If the working directory contains uncommitted files, qpush, qpop and
51 qgoto abort immediately. If -f/--force is used, the changes are
51 qgoto abort immediately. If -f/--force is used, the changes are
52 discarded. Setting::
52 discarded. Setting::
53
53
54 [mq]
54 [mq]
55 keepchanges = True
55 keepchanges = True
56
56
57 make them behave as if --keep-changes were passed, and non-conflicting
57 make them behave as if --keep-changes were passed, and non-conflicting
58 local changes will be tolerated and preserved. If incompatible options
58 local changes will be tolerated and preserved. If incompatible options
59 such as -f/--force or --exact are passed, this setting is ignored.
59 such as -f/--force or --exact are passed, this setting is ignored.
60
60
61 This extension used to provide a strip command. This command now lives
61 This extension used to provide a strip command. This command now lives
62 in the strip extension.
62 in the strip extension.
63 '''
63 '''
64
64
65 from __future__ import absolute_import, print_function
65 from __future__ import absolute_import, print_function
66
66
67 import errno
67 import errno
68 import os
68 import os
69 import re
69 import re
70 import shutil
70 import shutil
71 import sys
71 import sys
72 from mercurial.i18n import _
72 from mercurial.i18n import _
73 from mercurial.node import (
73 from mercurial.node import (
74 bin,
74 bin,
75 hex,
75 hex,
76 nullrev,
76 nullrev,
77 short,
77 short,
78 )
78 )
79 from mercurial.pycompat import (
79 from mercurial.pycompat import (
80 delattr,
80 delattr,
81 getattr,
81 getattr,
82 open,
82 open,
83 )
83 )
84 from mercurial import (
84 from mercurial import (
85 cmdutil,
85 cmdutil,
86 commands,
86 commands,
87 dirstateguard,
87 dirstateguard,
88 encoding,
88 encoding,
89 error,
89 error,
90 extensions,
90 extensions,
91 hg,
91 hg,
92 localrepo,
92 localrepo,
93 lock as lockmod,
93 lock as lockmod,
94 logcmdutil,
94 logcmdutil,
95 patch as patchmod,
95 patch as patchmod,
96 phases,
96 phases,
97 pycompat,
97 pycompat,
98 registrar,
98 registrar,
99 revsetlang,
99 revsetlang,
100 scmutil,
100 scmutil,
101 smartset,
101 smartset,
102 strip,
102 strip,
103 subrepoutil,
103 subrepoutil,
104 util,
104 util,
105 vfs as vfsmod,
105 vfs as vfsmod,
106 )
106 )
107 from mercurial.utils import (
107 from mercurial.utils import (
108 dateutil,
108 dateutil,
109 stringutil,
109 stringutil,
110 urlutil,
110 urlutil,
111 )
111 )
112
112
113 release = lockmod.release
113 release = lockmod.release
114 seriesopts = [(b's', b'summary', None, _(b'print first line of patch header'))]
114 seriesopts = [(b's', b'summary', None, _(b'print first line of patch header'))]
115
115
116 cmdtable = {}
116 cmdtable = {}
117 command = registrar.command(cmdtable)
117 command = registrar.command(cmdtable)
118 # Note for extension authors: ONLY specify testedwith = 'ships-with-hg-core' for
118 # Note for extension authors: ONLY specify testedwith = 'ships-with-hg-core' for
119 # extensions which SHIP WITH MERCURIAL. Non-mainline extensions should
119 # extensions which SHIP WITH MERCURIAL. Non-mainline extensions should
120 # be specifying the version(s) of Mercurial they are tested with, or
120 # be specifying the version(s) of Mercurial they are tested with, or
121 # leave the attribute unspecified.
121 # leave the attribute unspecified.
122 testedwith = b'ships-with-hg-core'
122 testedwith = b'ships-with-hg-core'
123
123
124 configtable = {}
124 configtable = {}
125 configitem = registrar.configitem(configtable)
125 configitem = registrar.configitem(configtable)
126
126
127 configitem(
127 configitem(
128 b'mq',
128 b'mq',
129 b'git',
129 b'git',
130 default=b'auto',
130 default=b'auto',
131 )
131 )
132 configitem(
132 configitem(
133 b'mq',
133 b'mq',
134 b'keepchanges',
134 b'keepchanges',
135 default=False,
135 default=False,
136 )
136 )
137 configitem(
137 configitem(
138 b'mq',
138 b'mq',
139 b'plain',
139 b'plain',
140 default=False,
140 default=False,
141 )
141 )
142 configitem(
142 configitem(
143 b'mq',
143 b'mq',
144 b'secret',
144 b'secret',
145 default=False,
145 default=False,
146 )
146 )
147
147
148 # force load strip extension formerly included in mq and import some utility
148 # force load strip extension formerly included in mq and import some utility
149 try:
149 try:
150 extensions.find(b'strip')
150 extensions.find(b'strip')
151 except KeyError:
151 except KeyError:
152 # note: load is lazy so we could avoid the try-except,
152 # note: load is lazy so we could avoid the try-except,
153 # but I (marmoute) prefer this explicit code.
153 # but I (marmoute) prefer this explicit code.
154 class dummyui(object):
154 class dummyui(object):
155 def debug(self, msg):
155 def debug(self, msg):
156 pass
156 pass
157
157
158 def log(self, event, msgfmt, *msgargs, **opts):
158 def log(self, event, msgfmt, *msgargs, **opts):
159 pass
159 pass
160
160
161 extensions.load(dummyui(), b'strip', b'')
161 extensions.load(dummyui(), b'strip', b'')
162
162
163 strip = strip.strip
163 strip = strip.strip
164
164
165
165
166 def checksubstate(repo, baserev=None):
166 def checksubstate(repo, baserev=None):
167 """return list of subrepos at a different revision than substate.
167 """return list of subrepos at a different revision than substate.
168 Abort if any subrepos have uncommitted changes."""
168 Abort if any subrepos have uncommitted changes."""
169 inclsubs = []
169 inclsubs = []
170 wctx = repo[None]
170 wctx = repo[None]
171 if baserev:
171 if baserev:
172 bctx = repo[baserev]
172 bctx = repo[baserev]
173 else:
173 else:
174 bctx = wctx.p1()
174 bctx = wctx.p1()
175 for s in sorted(wctx.substate):
175 for s in sorted(wctx.substate):
176 wctx.sub(s).bailifchanged(True)
176 wctx.sub(s).bailifchanged(True)
177 if s not in bctx.substate or bctx.sub(s).dirty():
177 if s not in bctx.substate or bctx.sub(s).dirty():
178 inclsubs.append(s)
178 inclsubs.append(s)
179 return inclsubs
179 return inclsubs
180
180
181
181
182 # Patch names looks like unix-file names.
182 # Patch names looks like unix-file names.
183 # They must be joinable with queue directory and result in the patch path.
183 # They must be joinable with queue directory and result in the patch path.
184 normname = util.normpath
184 normname = util.normpath
185
185
186
186
187 class statusentry(object):
187 class statusentry(object):
188 def __init__(self, node, name):
188 def __init__(self, node, name):
189 self.node, self.name = node, name
189 self.node, self.name = node, name
190
190
191 def __bytes__(self):
191 def __bytes__(self):
192 return hex(self.node) + b':' + self.name
192 return hex(self.node) + b':' + self.name
193
193
194 __str__ = encoding.strmethod(__bytes__)
194 __str__ = encoding.strmethod(__bytes__)
195 __repr__ = encoding.strmethod(__bytes__)
195 __repr__ = encoding.strmethod(__bytes__)
196
196
197
197
198 # The order of the headers in 'hg export' HG patches:
198 # The order of the headers in 'hg export' HG patches:
199 HGHEADERS = [
199 HGHEADERS = [
200 # '# HG changeset patch',
200 # '# HG changeset patch',
201 b'# User ',
201 b'# User ',
202 b'# Date ',
202 b'# Date ',
203 b'# ',
203 b'# ',
204 b'# Branch ',
204 b'# Branch ',
205 b'# Node ID ',
205 b'# Node ID ',
206 b'# Parent ', # can occur twice for merges - but that is not relevant for mq
206 b'# Parent ', # can occur twice for merges - but that is not relevant for mq
207 ]
207 ]
208 # The order of headers in plain 'mail style' patches:
208 # The order of headers in plain 'mail style' patches:
209 PLAINHEADERS = {
209 PLAINHEADERS = {
210 b'from': 0,
210 b'from': 0,
211 b'date': 1,
211 b'date': 1,
212 b'subject': 2,
212 b'subject': 2,
213 }
213 }
214
214
215
215
216 def inserthgheader(lines, header, value):
216 def inserthgheader(lines, header, value):
217 """Assuming lines contains a HG patch header, add a header line with value.
217 """Assuming lines contains a HG patch header, add a header line with value.
218 >>> try: inserthgheader([], b'# Date ', b'z')
218 >>> try: inserthgheader([], b'# Date ', b'z')
219 ... except ValueError as inst: print("oops")
219 ... except ValueError as inst: print("oops")
220 oops
220 oops
221 >>> inserthgheader([b'# HG changeset patch'], b'# Date ', b'z')
221 >>> inserthgheader([b'# HG changeset patch'], b'# Date ', b'z')
222 ['# HG changeset patch', '# Date z']
222 ['# HG changeset patch', '# Date z']
223 >>> inserthgheader([b'# HG changeset patch', b''], b'# Date ', b'z')
223 >>> inserthgheader([b'# HG changeset patch', b''], b'# Date ', b'z')
224 ['# HG changeset patch', '# Date z', '']
224 ['# HG changeset patch', '# Date z', '']
225 >>> inserthgheader([b'# HG changeset patch', b'# User y'], b'# Date ', b'z')
225 >>> inserthgheader([b'# HG changeset patch', b'# User y'], b'# Date ', b'z')
226 ['# HG changeset patch', '# User y', '# Date z']
226 ['# HG changeset patch', '# User y', '# Date z']
227 >>> inserthgheader([b'# HG changeset patch', b'# Date x', b'# User y'],
227 >>> inserthgheader([b'# HG changeset patch', b'# Date x', b'# User y'],
228 ... b'# User ', b'z')
228 ... b'# User ', b'z')
229 ['# HG changeset patch', '# Date x', '# User z']
229 ['# HG changeset patch', '# Date x', '# User z']
230 >>> inserthgheader([b'# HG changeset patch', b'# Date y'], b'# Date ', b'z')
230 >>> inserthgheader([b'# HG changeset patch', b'# Date y'], b'# Date ', b'z')
231 ['# HG changeset patch', '# Date z']
231 ['# HG changeset patch', '# Date z']
232 >>> inserthgheader([b'# HG changeset patch', b'', b'# Date y'],
232 >>> inserthgheader([b'# HG changeset patch', b'', b'# Date y'],
233 ... b'# Date ', b'z')
233 ... b'# Date ', b'z')
234 ['# HG changeset patch', '# Date z', '', '# Date y']
234 ['# HG changeset patch', '# Date z', '', '# Date y']
235 >>> inserthgheader([b'# HG changeset patch', b'# Parent y'],
235 >>> inserthgheader([b'# HG changeset patch', b'# Parent y'],
236 ... b'# Date ', b'z')
236 ... b'# Date ', b'z')
237 ['# HG changeset patch', '# Date z', '# Parent y']
237 ['# HG changeset patch', '# Date z', '# Parent y']
238 """
238 """
239 start = lines.index(b'# HG changeset patch') + 1
239 start = lines.index(b'# HG changeset patch') + 1
240 newindex = HGHEADERS.index(header)
240 newindex = HGHEADERS.index(header)
241 bestpos = len(lines)
241 bestpos = len(lines)
242 for i in range(start, len(lines)):
242 for i in range(start, len(lines)):
243 line = lines[i]
243 line = lines[i]
244 if not line.startswith(b'# '):
244 if not line.startswith(b'# '):
245 bestpos = min(bestpos, i)
245 bestpos = min(bestpos, i)
246 break
246 break
247 for lineindex, h in enumerate(HGHEADERS):
247 for lineindex, h in enumerate(HGHEADERS):
248 if line.startswith(h):
248 if line.startswith(h):
249 if lineindex == newindex:
249 if lineindex == newindex:
250 lines[i] = header + value
250 lines[i] = header + value
251 return lines
251 return lines
252 if lineindex > newindex:
252 if lineindex > newindex:
253 bestpos = min(bestpos, i)
253 bestpos = min(bestpos, i)
254 break # next line
254 break # next line
255 lines.insert(bestpos, header + value)
255 lines.insert(bestpos, header + value)
256 return lines
256 return lines
257
257
258
258
259 def insertplainheader(lines, header, value):
259 def insertplainheader(lines, header, value):
260 """For lines containing a plain patch header, add a header line with value.
260 """For lines containing a plain patch header, add a header line with value.
261 >>> insertplainheader([], b'Date', b'z')
261 >>> insertplainheader([], b'Date', b'z')
262 ['Date: z']
262 ['Date: z']
263 >>> insertplainheader([b''], b'Date', b'z')
263 >>> insertplainheader([b''], b'Date', b'z')
264 ['Date: z', '']
264 ['Date: z', '']
265 >>> insertplainheader([b'x'], b'Date', b'z')
265 >>> insertplainheader([b'x'], b'Date', b'z')
266 ['Date: z', '', 'x']
266 ['Date: z', '', 'x']
267 >>> insertplainheader([b'From: y', b'x'], b'Date', b'z')
267 >>> insertplainheader([b'From: y', b'x'], b'Date', b'z')
268 ['From: y', 'Date: z', '', 'x']
268 ['From: y', 'Date: z', '', 'x']
269 >>> insertplainheader([b' date : x', b' from : y', b''], b'From', b'z')
269 >>> insertplainheader([b' date : x', b' from : y', b''], b'From', b'z')
270 [' date : x', 'From: z', '']
270 [' date : x', 'From: z', '']
271 >>> insertplainheader([b'', b'Date: y'], b'Date', b'z')
271 >>> insertplainheader([b'', b'Date: y'], b'Date', b'z')
272 ['Date: z', '', 'Date: y']
272 ['Date: z', '', 'Date: y']
273 >>> insertplainheader([b'foo: bar', b'DATE: z', b'x'], b'From', b'y')
273 >>> insertplainheader([b'foo: bar', b'DATE: z', b'x'], b'From', b'y')
274 ['From: y', 'foo: bar', 'DATE: z', '', 'x']
274 ['From: y', 'foo: bar', 'DATE: z', '', 'x']
275 """
275 """
276 newprio = PLAINHEADERS[header.lower()]
276 newprio = PLAINHEADERS[header.lower()]
277 bestpos = len(lines)
277 bestpos = len(lines)
278 for i, line in enumerate(lines):
278 for i, line in enumerate(lines):
279 if b':' in line:
279 if b':' in line:
280 lheader = line.split(b':', 1)[0].strip().lower()
280 lheader = line.split(b':', 1)[0].strip().lower()
281 lprio = PLAINHEADERS.get(lheader, newprio + 1)
281 lprio = PLAINHEADERS.get(lheader, newprio + 1)
282 if lprio == newprio:
282 if lprio == newprio:
283 lines[i] = b'%s: %s' % (header, value)
283 lines[i] = b'%s: %s' % (header, value)
284 return lines
284 return lines
285 if lprio > newprio and i < bestpos:
285 if lprio > newprio and i < bestpos:
286 bestpos = i
286 bestpos = i
287 else:
287 else:
288 if line:
288 if line:
289 lines.insert(i, b'')
289 lines.insert(i, b'')
290 if i < bestpos:
290 if i < bestpos:
291 bestpos = i
291 bestpos = i
292 break
292 break
293 lines.insert(bestpos, b'%s: %s' % (header, value))
293 lines.insert(bestpos, b'%s: %s' % (header, value))
294 return lines
294 return lines
295
295
296
296
297 class patchheader(object):
297 class patchheader(object):
298 def __init__(self, pf, plainmode=False):
298 def __init__(self, pf, plainmode=False):
299 def eatdiff(lines):
299 def eatdiff(lines):
300 while lines:
300 while lines:
301 l = lines[-1]
301 l = lines[-1]
302 if (
302 if (
303 l.startswith(b"diff -")
303 l.startswith(b"diff -")
304 or l.startswith(b"Index:")
304 or l.startswith(b"Index:")
305 or l.startswith(b"===========")
305 or l.startswith(b"===========")
306 ):
306 ):
307 del lines[-1]
307 del lines[-1]
308 else:
308 else:
309 break
309 break
310
310
311 def eatempty(lines):
311 def eatempty(lines):
312 while lines:
312 while lines:
313 if not lines[-1].strip():
313 if not lines[-1].strip():
314 del lines[-1]
314 del lines[-1]
315 else:
315 else:
316 break
316 break
317
317
318 message = []
318 message = []
319 comments = []
319 comments = []
320 user = None
320 user = None
321 date = None
321 date = None
322 parent = None
322 parent = None
323 format = None
323 format = None
324 subject = None
324 subject = None
325 branch = None
325 branch = None
326 nodeid = None
326 nodeid = None
327 diffstart = 0
327 diffstart = 0
328
328
329 for line in open(pf, b'rb'):
329 for line in open(pf, b'rb'):
330 line = line.rstrip()
330 line = line.rstrip()
331 if line.startswith(b'diff --git') or (
331 if line.startswith(b'diff --git') or (
332 diffstart and line.startswith(b'+++ ')
332 diffstart and line.startswith(b'+++ ')
333 ):
333 ):
334 diffstart = 2
334 diffstart = 2
335 break
335 break
336 diffstart = 0 # reset
336 diffstart = 0 # reset
337 if line.startswith(b"--- "):
337 if line.startswith(b"--- "):
338 diffstart = 1
338 diffstart = 1
339 continue
339 continue
340 elif format == b"hgpatch":
340 elif format == b"hgpatch":
341 # parse values when importing the result of an hg export
341 # parse values when importing the result of an hg export
342 if line.startswith(b"# User "):
342 if line.startswith(b"# User "):
343 user = line[7:]
343 user = line[7:]
344 elif line.startswith(b"# Date "):
344 elif line.startswith(b"# Date "):
345 date = line[7:]
345 date = line[7:]
346 elif line.startswith(b"# Parent "):
346 elif line.startswith(b"# Parent "):
347 parent = line[9:].lstrip() # handle double trailing space
347 parent = line[9:].lstrip() # handle double trailing space
348 elif line.startswith(b"# Branch "):
348 elif line.startswith(b"# Branch "):
349 branch = line[9:]
349 branch = line[9:]
350 elif line.startswith(b"# Node ID "):
350 elif line.startswith(b"# Node ID "):
351 nodeid = line[10:]
351 nodeid = line[10:]
352 elif not line.startswith(b"# ") and line:
352 elif not line.startswith(b"# ") and line:
353 message.append(line)
353 message.append(line)
354 format = None
354 format = None
355 elif line == b'# HG changeset patch':
355 elif line == b'# HG changeset patch':
356 message = []
356 message = []
357 format = b"hgpatch"
357 format = b"hgpatch"
358 elif format != b"tagdone" and (
358 elif format != b"tagdone" and (
359 line.startswith(b"Subject: ") or line.startswith(b"subject: ")
359 line.startswith(b"Subject: ") or line.startswith(b"subject: ")
360 ):
360 ):
361 subject = line[9:]
361 subject = line[9:]
362 format = b"tag"
362 format = b"tag"
363 elif format != b"tagdone" and (
363 elif format != b"tagdone" and (
364 line.startswith(b"From: ") or line.startswith(b"from: ")
364 line.startswith(b"From: ") or line.startswith(b"from: ")
365 ):
365 ):
366 user = line[6:]
366 user = line[6:]
367 format = b"tag"
367 format = b"tag"
368 elif format != b"tagdone" and (
368 elif format != b"tagdone" and (
369 line.startswith(b"Date: ") or line.startswith(b"date: ")
369 line.startswith(b"Date: ") or line.startswith(b"date: ")
370 ):
370 ):
371 date = line[6:]
371 date = line[6:]
372 format = b"tag"
372 format = b"tag"
373 elif format == b"tag" and line == b"":
373 elif format == b"tag" and line == b"":
374 # when looking for tags (subject: from: etc) they
374 # when looking for tags (subject: from: etc) they
375 # end once you find a blank line in the source
375 # end once you find a blank line in the source
376 format = b"tagdone"
376 format = b"tagdone"
377 elif message or line:
377 elif message or line:
378 message.append(line)
378 message.append(line)
379 comments.append(line)
379 comments.append(line)
380
380
381 eatdiff(message)
381 eatdiff(message)
382 eatdiff(comments)
382 eatdiff(comments)
383 # Remember the exact starting line of the patch diffs before consuming
383 # Remember the exact starting line of the patch diffs before consuming
384 # empty lines, for external use by TortoiseHg and others
384 # empty lines, for external use by TortoiseHg and others
385 self.diffstartline = len(comments)
385 self.diffstartline = len(comments)
386 eatempty(message)
386 eatempty(message)
387 eatempty(comments)
387 eatempty(comments)
388
388
389 # make sure message isn't empty
389 # make sure message isn't empty
390 if format and format.startswith(b"tag") and subject:
390 if format and format.startswith(b"tag") and subject:
391 message.insert(0, subject)
391 message.insert(0, subject)
392
392
393 self.message = message
393 self.message = message
394 self.comments = comments
394 self.comments = comments
395 self.user = user
395 self.user = user
396 self.date = date
396 self.date = date
397 self.parent = parent
397 self.parent = parent
398 # nodeid and branch are for external use by TortoiseHg and others
398 # nodeid and branch are for external use by TortoiseHg and others
399 self.nodeid = nodeid
399 self.nodeid = nodeid
400 self.branch = branch
400 self.branch = branch
401 self.haspatch = diffstart > 1
401 self.haspatch = diffstart > 1
402 self.plainmode = (
402 self.plainmode = (
403 plainmode
403 plainmode
404 or b'# HG changeset patch' not in self.comments
404 or b'# HG changeset patch' not in self.comments
405 and any(
405 and any(
406 c.startswith(b'Date: ') or c.startswith(b'From: ')
406 c.startswith(b'Date: ') or c.startswith(b'From: ')
407 for c in self.comments
407 for c in self.comments
408 )
408 )
409 )
409 )
410
410
411 def setuser(self, user):
411 def setuser(self, user):
412 try:
412 try:
413 inserthgheader(self.comments, b'# User ', user)
413 inserthgheader(self.comments, b'# User ', user)
414 except ValueError:
414 except ValueError:
415 if self.plainmode:
415 if self.plainmode:
416 insertplainheader(self.comments, b'From', user)
416 insertplainheader(self.comments, b'From', user)
417 else:
417 else:
418 tmp = [b'# HG changeset patch', b'# User ' + user]
418 tmp = [b'# HG changeset patch', b'# User ' + user]
419 self.comments = tmp + self.comments
419 self.comments = tmp + self.comments
420 self.user = user
420 self.user = user
421
421
422 def setdate(self, date):
422 def setdate(self, date):
423 try:
423 try:
424 inserthgheader(self.comments, b'# Date ', date)
424 inserthgheader(self.comments, b'# Date ', date)
425 except ValueError:
425 except ValueError:
426 if self.plainmode:
426 if self.plainmode:
427 insertplainheader(self.comments, b'Date', date)
427 insertplainheader(self.comments, b'Date', date)
428 else:
428 else:
429 tmp = [b'# HG changeset patch', b'# Date ' + date]
429 tmp = [b'# HG changeset patch', b'# Date ' + date]
430 self.comments = tmp + self.comments
430 self.comments = tmp + self.comments
431 self.date = date
431 self.date = date
432
432
433 def setparent(self, parent):
433 def setparent(self, parent):
434 try:
434 try:
435 inserthgheader(self.comments, b'# Parent ', parent)
435 inserthgheader(self.comments, b'# Parent ', parent)
436 except ValueError:
436 except ValueError:
437 if not self.plainmode:
437 if not self.plainmode:
438 tmp = [b'# HG changeset patch', b'# Parent ' + parent]
438 tmp = [b'# HG changeset patch', b'# Parent ' + parent]
439 self.comments = tmp + self.comments
439 self.comments = tmp + self.comments
440 self.parent = parent
440 self.parent = parent
441
441
442 def setmessage(self, message):
442 def setmessage(self, message):
443 if self.comments:
443 if self.comments:
444 self._delmsg()
444 self._delmsg()
445 self.message = [message]
445 self.message = [message]
446 if message:
446 if message:
447 if self.plainmode and self.comments and self.comments[-1]:
447 if self.plainmode and self.comments and self.comments[-1]:
448 self.comments.append(b'')
448 self.comments.append(b'')
449 self.comments.append(message)
449 self.comments.append(message)
450
450
451 def __bytes__(self):
451 def __bytes__(self):
452 s = b'\n'.join(self.comments).rstrip()
452 s = b'\n'.join(self.comments).rstrip()
453 if not s:
453 if not s:
454 return b''
454 return b''
455 return s + b'\n\n'
455 return s + b'\n\n'
456
456
457 __str__ = encoding.strmethod(__bytes__)
457 __str__ = encoding.strmethod(__bytes__)
458
458
459 def _delmsg(self):
459 def _delmsg(self):
460 """Remove existing message, keeping the rest of the comments fields.
460 """Remove existing message, keeping the rest of the comments fields.
461 If comments contains 'subject: ', message will prepend
461 If comments contains 'subject: ', message will prepend
462 the field and a blank line."""
462 the field and a blank line."""
463 if self.message:
463 if self.message:
464 subj = b'subject: ' + self.message[0].lower()
464 subj = b'subject: ' + self.message[0].lower()
465 for i in pycompat.xrange(len(self.comments)):
465 for i in pycompat.xrange(len(self.comments)):
466 if subj == self.comments[i].lower():
466 if subj == self.comments[i].lower():
467 del self.comments[i]
467 del self.comments[i]
468 self.message = self.message[2:]
468 self.message = self.message[2:]
469 break
469 break
470 ci = 0
470 ci = 0
471 for mi in self.message:
471 for mi in self.message:
472 while mi != self.comments[ci]:
472 while mi != self.comments[ci]:
473 ci += 1
473 ci += 1
474 del self.comments[ci]
474 del self.comments[ci]
475
475
476
476
477 def newcommit(repo, phase, *args, **kwargs):
477 def newcommit(repo, phase, *args, **kwargs):
478 """helper dedicated to ensure a commit respect mq.secret setting
478 """helper dedicated to ensure a commit respect mq.secret setting
479
479
480 It should be used instead of repo.commit inside the mq source for operation
480 It should be used instead of repo.commit inside the mq source for operation
481 creating new changeset.
481 creating new changeset.
482 """
482 """
483 repo = repo.unfiltered()
483 repo = repo.unfiltered()
484 if phase is None:
484 if phase is None:
485 if repo.ui.configbool(b'mq', b'secret'):
485 if repo.ui.configbool(b'mq', b'secret'):
486 phase = phases.secret
486 phase = phases.secret
487 overrides = {(b'ui', b'allowemptycommit'): True}
487 overrides = {(b'ui', b'allowemptycommit'): True}
488 if phase is not None:
488 if phase is not None:
489 overrides[(b'phases', b'new-commit')] = phase
489 overrides[(b'phases', b'new-commit')] = phase
490 with repo.ui.configoverride(overrides, b'mq'):
490 with repo.ui.configoverride(overrides, b'mq'):
491 repo.ui.setconfig(b'ui', b'allowemptycommit', True)
491 repo.ui.setconfig(b'ui', b'allowemptycommit', True)
492 return repo.commit(*args, **kwargs)
492 return repo.commit(*args, **kwargs)
493
493
494
494
495 class AbortNoCleanup(error.Abort):
495 class AbortNoCleanup(error.Abort):
496 pass
496 pass
497
497
498
498
499 class queue(object):
499 class queue(object):
500 def __init__(self, ui, baseui, path, patchdir=None):
500 def __init__(self, ui, baseui, path, patchdir=None):
501 self.basepath = path
501 self.basepath = path
502 try:
502 try:
503 with open(os.path.join(path, b'patches.queue'), 'rb') as fh:
503 with open(os.path.join(path, b'patches.queue'), 'rb') as fh:
504 cur = fh.read().rstrip()
504 cur = fh.read().rstrip()
505
505
506 if not cur:
506 if not cur:
507 curpath = os.path.join(path, b'patches')
507 curpath = os.path.join(path, b'patches')
508 else:
508 else:
509 curpath = os.path.join(path, b'patches-' + cur)
509 curpath = os.path.join(path, b'patches-' + cur)
510 except IOError:
510 except IOError:
511 curpath = os.path.join(path, b'patches')
511 curpath = os.path.join(path, b'patches')
512 self.path = patchdir or curpath
512 self.path = patchdir or curpath
513 self.opener = vfsmod.vfs(self.path)
513 self.opener = vfsmod.vfs(self.path)
514 self.ui = ui
514 self.ui = ui
515 self.baseui = baseui
515 self.baseui = baseui
516 self.applieddirty = False
516 self.applieddirty = False
517 self.seriesdirty = False
517 self.seriesdirty = False
518 self.added = []
518 self.added = []
519 self.seriespath = b"series"
519 self.seriespath = b"series"
520 self.statuspath = b"status"
520 self.statuspath = b"status"
521 self.guardspath = b"guards"
521 self.guardspath = b"guards"
522 self.activeguards = None
522 self.activeguards = None
523 self.guardsdirty = False
523 self.guardsdirty = False
524 # Handle mq.git as a bool with extended values
524 # Handle mq.git as a bool with extended values
525 gitmode = ui.config(b'mq', b'git').lower()
525 gitmode = ui.config(b'mq', b'git').lower()
526 boolmode = stringutil.parsebool(gitmode)
526 boolmode = stringutil.parsebool(gitmode)
527 if boolmode is not None:
527 if boolmode is not None:
528 if boolmode:
528 if boolmode:
529 gitmode = b'yes'
529 gitmode = b'yes'
530 else:
530 else:
531 gitmode = b'no'
531 gitmode = b'no'
532 self.gitmode = gitmode
532 self.gitmode = gitmode
533 # deprecated config: mq.plain
533 # deprecated config: mq.plain
534 self.plainmode = ui.configbool(b'mq', b'plain')
534 self.plainmode = ui.configbool(b'mq', b'plain')
535 self.checkapplied = True
535 self.checkapplied = True
536
536
537 @util.propertycache
537 @util.propertycache
538 def applied(self):
538 def applied(self):
539 def parselines(lines):
539 def parselines(lines):
540 for l in lines:
540 for l in lines:
541 entry = l.split(b':', 1)
541 entry = l.split(b':', 1)
542 if len(entry) > 1:
542 if len(entry) > 1:
543 n, name = entry
543 n, name = entry
544 yield statusentry(bin(n), name)
544 yield statusentry(bin(n), name)
545 elif l.strip():
545 elif l.strip():
546 self.ui.warn(
546 self.ui.warn(
547 _(b'malformated mq status line: %s\n')
547 _(b'malformated mq status line: %s\n')
548 % stringutil.pprint(entry)
548 % stringutil.pprint(entry)
549 )
549 )
550 # else we ignore empty lines
550 # else we ignore empty lines
551
551
552 try:
552 try:
553 lines = self.opener.read(self.statuspath).splitlines()
553 lines = self.opener.read(self.statuspath).splitlines()
554 return list(parselines(lines))
554 return list(parselines(lines))
555 except IOError as e:
555 except IOError as e:
556 if e.errno == errno.ENOENT:
556 if e.errno == errno.ENOENT:
557 return []
557 return []
558 raise
558 raise
559
559
560 @util.propertycache
560 @util.propertycache
561 def fullseries(self):
561 def fullseries(self):
562 try:
562 try:
563 return self.opener.read(self.seriespath).splitlines()
563 return self.opener.read(self.seriespath).splitlines()
564 except IOError as e:
564 except IOError as e:
565 if e.errno == errno.ENOENT:
565 if e.errno == errno.ENOENT:
566 return []
566 return []
567 raise
567 raise
568
568
569 @util.propertycache
569 @util.propertycache
570 def series(self):
570 def series(self):
571 self.parseseries()
571 self.parseseries()
572 return self.series
572 return self.series
573
573
574 @util.propertycache
574 @util.propertycache
575 def seriesguards(self):
575 def seriesguards(self):
576 self.parseseries()
576 self.parseseries()
577 return self.seriesguards
577 return self.seriesguards
578
578
579 def invalidate(self):
579 def invalidate(self):
580 for a in 'applied fullseries series seriesguards'.split():
580 for a in 'applied fullseries series seriesguards'.split():
581 if a in self.__dict__:
581 if a in self.__dict__:
582 delattr(self, a)
582 delattr(self, a)
583 self.applieddirty = False
583 self.applieddirty = False
584 self.seriesdirty = False
584 self.seriesdirty = False
585 self.guardsdirty = False
585 self.guardsdirty = False
586 self.activeguards = None
586 self.activeguards = None
587
587
588 def diffopts(self, opts=None, patchfn=None, plain=False):
588 def diffopts(self, opts=None, patchfn=None, plain=False):
589 """Return diff options tweaked for this mq use, possibly upgrading to
589 """Return diff options tweaked for this mq use, possibly upgrading to
590 git format, and possibly plain and without lossy options."""
590 git format, and possibly plain and without lossy options."""
591 diffopts = patchmod.difffeatureopts(
591 diffopts = patchmod.difffeatureopts(
592 self.ui,
592 self.ui,
593 opts,
593 opts,
594 git=True,
594 git=True,
595 whitespace=not plain,
595 whitespace=not plain,
596 formatchanging=not plain,
596 formatchanging=not plain,
597 )
597 )
598 if self.gitmode == b'auto':
598 if self.gitmode == b'auto':
599 diffopts.upgrade = True
599 diffopts.upgrade = True
600 elif self.gitmode == b'keep':
600 elif self.gitmode == b'keep':
601 pass
601 pass
602 elif self.gitmode in (b'yes', b'no'):
602 elif self.gitmode in (b'yes', b'no'):
603 diffopts.git = self.gitmode == b'yes'
603 diffopts.git = self.gitmode == b'yes'
604 else:
604 else:
605 raise error.Abort(
605 raise error.Abort(
606 _(b'mq.git option can be auto/keep/yes/no got %s')
606 _(b'mq.git option can be auto/keep/yes/no got %s')
607 % self.gitmode
607 % self.gitmode
608 )
608 )
609 if patchfn:
609 if patchfn:
610 diffopts = self.patchopts(diffopts, patchfn)
610 diffopts = self.patchopts(diffopts, patchfn)
611 return diffopts
611 return diffopts
612
612
613 def patchopts(self, diffopts, *patches):
613 def patchopts(self, diffopts, *patches):
614 """Return a copy of input diff options with git set to true if
614 """Return a copy of input diff options with git set to true if
615 referenced patch is a git patch and should be preserved as such.
615 referenced patch is a git patch and should be preserved as such.
616 """
616 """
617 diffopts = diffopts.copy()
617 diffopts = diffopts.copy()
618 if not diffopts.git and self.gitmode == b'keep':
618 if not diffopts.git and self.gitmode == b'keep':
619 for patchfn in patches:
619 for patchfn in patches:
620 patchf = self.opener(patchfn, b'r')
620 patchf = self.opener(patchfn, b'r')
621 # if the patch was a git patch, refresh it as a git patch
621 # if the patch was a git patch, refresh it as a git patch
622 diffopts.git = any(
622 diffopts.git = any(
623 line.startswith(b'diff --git') for line in patchf
623 line.startswith(b'diff --git') for line in patchf
624 )
624 )
625 patchf.close()
625 patchf.close()
626 return diffopts
626 return diffopts
627
627
628 def join(self, *p):
628 def join(self, *p):
629 return os.path.join(self.path, *p)
629 return os.path.join(self.path, *p)
630
630
631 def findseries(self, patch):
631 def findseries(self, patch):
632 def matchpatch(l):
632 def matchpatch(l):
633 l = l.split(b'#', 1)[0]
633 l = l.split(b'#', 1)[0]
634 return l.strip() == patch
634 return l.strip() == patch
635
635
636 for index, l in enumerate(self.fullseries):
636 for index, l in enumerate(self.fullseries):
637 if matchpatch(l):
637 if matchpatch(l):
638 return index
638 return index
639 return None
639 return None
640
640
641 guard_re = re.compile(br'\s?#([-+][^-+# \t\r\n\f][^# \t\r\n\f]*)')
641 guard_re = re.compile(br'\s?#([-+][^-+# \t\r\n\f][^# \t\r\n\f]*)')
642
642
643 def parseseries(self):
643 def parseseries(self):
644 self.series = []
644 self.series = []
645 self.seriesguards = []
645 self.seriesguards = []
646 for l in self.fullseries:
646 for l in self.fullseries:
647 h = l.find(b'#')
647 h = l.find(b'#')
648 if h == -1:
648 if h == -1:
649 patch = l
649 patch = l
650 comment = b''
650 comment = b''
651 elif h == 0:
651 elif h == 0:
652 continue
652 continue
653 else:
653 else:
654 patch = l[:h]
654 patch = l[:h]
655 comment = l[h:]
655 comment = l[h:]
656 patch = patch.strip()
656 patch = patch.strip()
657 if patch:
657 if patch:
658 if patch in self.series:
658 if patch in self.series:
659 raise error.Abort(
659 raise error.Abort(
660 _(b'%s appears more than once in %s')
660 _(b'%s appears more than once in %s')
661 % (patch, self.join(self.seriespath))
661 % (patch, self.join(self.seriespath))
662 )
662 )
663 self.series.append(patch)
663 self.series.append(patch)
664 self.seriesguards.append(self.guard_re.findall(comment))
664 self.seriesguards.append(self.guard_re.findall(comment))
665
665
666 def checkguard(self, guard):
666 def checkguard(self, guard):
667 if not guard:
667 if not guard:
668 return _(b'guard cannot be an empty string')
668 return _(b'guard cannot be an empty string')
669 bad_chars = b'# \t\r\n\f'
669 bad_chars = b'# \t\r\n\f'
670 first = guard[0]
670 first = guard[0]
671 if first in b'-+':
671 if first in b'-+':
672 return _(b'guard %r starts with invalid character: %r') % (
672 return _(b'guard %r starts with invalid character: %r') % (
673 guard,
673 guard,
674 first,
674 first,
675 )
675 )
676 for c in bad_chars:
676 for c in bad_chars:
677 if c in guard:
677 if c in guard:
678 return _(b'invalid character in guard %r: %r') % (guard, c)
678 return _(b'invalid character in guard %r: %r') % (guard, c)
679
679
680 def setactive(self, guards):
680 def setactive(self, guards):
681 for guard in guards:
681 for guard in guards:
682 bad = self.checkguard(guard)
682 bad = self.checkguard(guard)
683 if bad:
683 if bad:
684 raise error.Abort(bad)
684 raise error.Abort(bad)
685 guards = sorted(set(guards))
685 guards = sorted(set(guards))
686 self.ui.debug(b'active guards: %s\n' % b' '.join(guards))
686 self.ui.debug(b'active guards: %s\n' % b' '.join(guards))
687 self.activeguards = guards
687 self.activeguards = guards
688 self.guardsdirty = True
688 self.guardsdirty = True
689
689
690 def active(self):
690 def active(self):
691 if self.activeguards is None:
691 if self.activeguards is None:
692 self.activeguards = []
692 self.activeguards = []
693 try:
693 try:
694 guards = self.opener.read(self.guardspath).split()
694 guards = self.opener.read(self.guardspath).split()
695 except IOError as err:
695 except IOError as err:
696 if err.errno != errno.ENOENT:
696 if err.errno != errno.ENOENT:
697 raise
697 raise
698 guards = []
698 guards = []
699 for i, guard in enumerate(guards):
699 for i, guard in enumerate(guards):
700 bad = self.checkguard(guard)
700 bad = self.checkguard(guard)
701 if bad:
701 if bad:
702 self.ui.warn(
702 self.ui.warn(
703 b'%s:%d: %s\n'
703 b'%s:%d: %s\n'
704 % (self.join(self.guardspath), i + 1, bad)
704 % (self.join(self.guardspath), i + 1, bad)
705 )
705 )
706 else:
706 else:
707 self.activeguards.append(guard)
707 self.activeguards.append(guard)
708 return self.activeguards
708 return self.activeguards
709
709
710 def setguards(self, idx, guards):
710 def setguards(self, idx, guards):
711 for g in guards:
711 for g in guards:
712 if len(g) < 2:
712 if len(g) < 2:
713 raise error.Abort(_(b'guard %r too short') % g)
713 raise error.Abort(_(b'guard %r too short') % g)
714 if g[0] not in b'-+':
714 if g[0] not in b'-+':
715 raise error.Abort(_(b'guard %r starts with invalid char') % g)
715 raise error.Abort(_(b'guard %r starts with invalid char') % g)
716 bad = self.checkguard(g[1:])
716 bad = self.checkguard(g[1:])
717 if bad:
717 if bad:
718 raise error.Abort(bad)
718 raise error.Abort(bad)
719 drop = self.guard_re.sub(b'', self.fullseries[idx])
719 drop = self.guard_re.sub(b'', self.fullseries[idx])
720 self.fullseries[idx] = drop + b''.join([b' #' + g for g in guards])
720 self.fullseries[idx] = drop + b''.join([b' #' + g for g in guards])
721 self.parseseries()
721 self.parseseries()
722 self.seriesdirty = True
722 self.seriesdirty = True
723
723
724 def pushable(self, idx):
724 def pushable(self, idx):
725 if isinstance(idx, bytes):
725 if isinstance(idx, bytes):
726 idx = self.series.index(idx)
726 idx = self.series.index(idx)
727 patchguards = self.seriesguards[idx]
727 patchguards = self.seriesguards[idx]
728 if not patchguards:
728 if not patchguards:
729 return True, None
729 return True, None
730 guards = self.active()
730 guards = self.active()
731 exactneg = [
731 exactneg = [
732 g for g in patchguards if g.startswith(b'-') and g[1:] in guards
732 g for g in patchguards if g.startswith(b'-') and g[1:] in guards
733 ]
733 ]
734 if exactneg:
734 if exactneg:
735 return False, stringutil.pprint(exactneg[0])
735 return False, stringutil.pprint(exactneg[0])
736 pos = [g for g in patchguards if g.startswith(b'+')]
736 pos = [g for g in patchguards if g.startswith(b'+')]
737 exactpos = [g for g in pos if g[1:] in guards]
737 exactpos = [g for g in pos if g[1:] in guards]
738 if pos:
738 if pos:
739 if exactpos:
739 if exactpos:
740 return True, stringutil.pprint(exactpos[0])
740 return True, stringutil.pprint(exactpos[0])
741 return False, b' '.join([stringutil.pprint(p) for p in pos])
741 return False, b' '.join([stringutil.pprint(p) for p in pos])
742 return True, b''
742 return True, b''
743
743
744 def explainpushable(self, idx, all_patches=False):
744 def explainpushable(self, idx, all_patches=False):
745 if all_patches:
745 if all_patches:
746 write = self.ui.write
746 write = self.ui.write
747 else:
747 else:
748 write = self.ui.warn
748 write = self.ui.warn
749
749
750 if all_patches or self.ui.verbose:
750 if all_patches or self.ui.verbose:
751 if isinstance(idx, bytes):
751 if isinstance(idx, bytes):
752 idx = self.series.index(idx)
752 idx = self.series.index(idx)
753 pushable, why = self.pushable(idx)
753 pushable, why = self.pushable(idx)
754 if all_patches and pushable:
754 if all_patches and pushable:
755 if why is None:
755 if why is None:
756 write(
756 write(
757 _(b'allowing %s - no guards in effect\n')
757 _(b'allowing %s - no guards in effect\n')
758 % self.series[idx]
758 % self.series[idx]
759 )
759 )
760 else:
760 else:
761 if not why:
761 if not why:
762 write(
762 write(
763 _(b'allowing %s - no matching negative guards\n')
763 _(b'allowing %s - no matching negative guards\n')
764 % self.series[idx]
764 % self.series[idx]
765 )
765 )
766 else:
766 else:
767 write(
767 write(
768 _(b'allowing %s - guarded by %s\n')
768 _(b'allowing %s - guarded by %s\n')
769 % (self.series[idx], why)
769 % (self.series[idx], why)
770 )
770 )
771 if not pushable:
771 if not pushable:
772 if why:
772 if why:
773 write(
773 write(
774 _(b'skipping %s - guarded by %s\n')
774 _(b'skipping %s - guarded by %s\n')
775 % (self.series[idx], why)
775 % (self.series[idx], why)
776 )
776 )
777 else:
777 else:
778 write(
778 write(
779 _(b'skipping %s - no matching guards\n')
779 _(b'skipping %s - no matching guards\n')
780 % self.series[idx]
780 % self.series[idx]
781 )
781 )
782
782
783 def savedirty(self):
783 def savedirty(self):
784 def writelist(items, path):
784 def writelist(items, path):
785 fp = self.opener(path, b'wb')
785 fp = self.opener(path, b'wb')
786 for i in items:
786 for i in items:
787 fp.write(b"%s\n" % i)
787 fp.write(b"%s\n" % i)
788 fp.close()
788 fp.close()
789
789
790 if self.applieddirty:
790 if self.applieddirty:
791 writelist(map(bytes, self.applied), self.statuspath)
791 writelist(map(bytes, self.applied), self.statuspath)
792 self.applieddirty = False
792 self.applieddirty = False
793 if self.seriesdirty:
793 if self.seriesdirty:
794 writelist(self.fullseries, self.seriespath)
794 writelist(self.fullseries, self.seriespath)
795 self.seriesdirty = False
795 self.seriesdirty = False
796 if self.guardsdirty:
796 if self.guardsdirty:
797 writelist(self.activeguards, self.guardspath)
797 writelist(self.activeguards, self.guardspath)
798 self.guardsdirty = False
798 self.guardsdirty = False
799 if self.added:
799 if self.added:
800 qrepo = self.qrepo()
800 qrepo = self.qrepo()
801 if qrepo:
801 if qrepo:
802 qrepo[None].add(f for f in self.added if f not in qrepo[None])
802 qrepo[None].add(f for f in self.added if f not in qrepo[None])
803 self.added = []
803 self.added = []
804
804
805 def removeundo(self, repo):
805 def removeundo(self, repo):
806 undo = repo.sjoin(b'undo')
806 undo = repo.sjoin(b'undo')
807 if not os.path.exists(undo):
807 if not os.path.exists(undo):
808 return
808 return
809 try:
809 try:
810 os.unlink(undo)
810 os.unlink(undo)
811 except OSError as inst:
811 except OSError as inst:
812 self.ui.warn(
812 self.ui.warn(
813 _(b'error removing undo: %s\n') % stringutil.forcebytestr(inst)
813 _(b'error removing undo: %s\n') % stringutil.forcebytestr(inst)
814 )
814 )
815
815
816 def backup(self, repo, files, copy=False):
816 def backup(self, repo, files, copy=False):
817 # backup local changes in --force case
817 # backup local changes in --force case
818 for f in sorted(files):
818 for f in sorted(files):
819 absf = repo.wjoin(f)
819 absf = repo.wjoin(f)
820 if os.path.lexists(absf):
820 if os.path.lexists(absf):
821 absorig = scmutil.backuppath(self.ui, repo, f)
821 absorig = scmutil.backuppath(self.ui, repo, f)
822 self.ui.note(
822 self.ui.note(
823 _(b'saving current version of %s as %s\n')
823 _(b'saving current version of %s as %s\n')
824 % (f, os.path.relpath(absorig))
824 % (f, os.path.relpath(absorig))
825 )
825 )
826
826
827 if copy:
827 if copy:
828 util.copyfile(absf, absorig)
828 util.copyfile(absf, absorig)
829 else:
829 else:
830 util.rename(absf, absorig)
830 util.rename(absf, absorig)
831
831
832 def printdiff(
832 def printdiff(
833 self,
833 self,
834 repo,
834 repo,
835 diffopts,
835 diffopts,
836 node1,
836 node1,
837 node2=None,
837 node2=None,
838 files=None,
838 files=None,
839 fp=None,
839 fp=None,
840 changes=None,
840 changes=None,
841 opts=None,
841 opts=None,
842 ):
842 ):
843 if opts is None:
843 if opts is None:
844 opts = {}
844 opts = {}
845 stat = opts.get(b'stat')
845 stat = opts.get(b'stat')
846 m = scmutil.match(repo[node1], files, opts)
846 m = scmutil.match(repo[node1], files, opts)
847 logcmdutil.diffordiffstat(
847 logcmdutil.diffordiffstat(
848 self.ui,
848 self.ui,
849 repo,
849 repo,
850 diffopts,
850 diffopts,
851 repo[node1],
851 repo[node1],
852 repo[node2],
852 repo[node2],
853 m,
853 m,
854 changes,
854 changes,
855 stat,
855 stat,
856 fp,
856 fp,
857 )
857 )
858
858
859 def mergeone(self, repo, mergeq, head, patch, rev, diffopts):
859 def mergeone(self, repo, mergeq, head, patch, rev, diffopts):
860 # first try just applying the patch
860 # first try just applying the patch
861 (err, n) = self.apply(
861 (err, n) = self.apply(
862 repo, [patch], update_status=False, strict=True, merge=rev
862 repo, [patch], update_status=False, strict=True, merge=rev
863 )
863 )
864
864
865 if err == 0:
865 if err == 0:
866 return (err, n)
866 return (err, n)
867
867
868 if n is None:
868 if n is None:
869 raise error.Abort(_(b"apply failed for patch %s") % patch)
869 raise error.Abort(_(b"apply failed for patch %s") % patch)
870
870
871 self.ui.warn(_(b"patch didn't work out, merging %s\n") % patch)
871 self.ui.warn(_(b"patch didn't work out, merging %s\n") % patch)
872
872
873 # apply failed, strip away that rev and merge.
873 # apply failed, strip away that rev and merge.
874 hg.clean(repo, head)
874 hg.clean(repo, head)
875 strip(self.ui, repo, [n], update=False, backup=False)
875 strip(self.ui, repo, [n], update=False, backup=False)
876
876
877 ctx = repo[rev]
877 ctx = repo[rev]
878 ret = hg.merge(ctx, remind=False)
878 ret = hg.merge(ctx, remind=False)
879 if ret:
879 if ret:
880 raise error.Abort(_(b"update returned %d") % ret)
880 raise error.Abort(_(b"update returned %d") % ret)
881 n = newcommit(repo, None, ctx.description(), ctx.user(), force=True)
881 n = newcommit(repo, None, ctx.description(), ctx.user(), force=True)
882 if n is None:
882 if n is None:
883 raise error.Abort(_(b"repo commit failed"))
883 raise error.Abort(_(b"repo commit failed"))
884 try:
884 try:
885 ph = patchheader(mergeq.join(patch), self.plainmode)
885 ph = patchheader(mergeq.join(patch), self.plainmode)
886 except Exception:
886 except Exception:
887 raise error.Abort(_(b"unable to read %s") % patch)
887 raise error.Abort(_(b"unable to read %s") % patch)
888
888
889 diffopts = self.patchopts(diffopts, patch)
889 diffopts = self.patchopts(diffopts, patch)
890 patchf = self.opener(patch, b"w")
890 patchf = self.opener(patch, b"w")
891 comments = bytes(ph)
891 comments = bytes(ph)
892 if comments:
892 if comments:
893 patchf.write(comments)
893 patchf.write(comments)
894 self.printdiff(repo, diffopts, head, n, fp=patchf)
894 self.printdiff(repo, diffopts, head, n, fp=patchf)
895 patchf.close()
895 patchf.close()
896 self.removeundo(repo)
896 self.removeundo(repo)
897 return (0, n)
897 return (0, n)
898
898
899 def qparents(self, repo, rev=None):
899 def qparents(self, repo, rev=None):
900 """return the mq handled parent or p1
900 """return the mq handled parent or p1
901
901
902 In some case where mq get himself in being the parent of a merge the
902 In some case where mq get himself in being the parent of a merge the
903 appropriate parent may be p2.
903 appropriate parent may be p2.
904 (eg: an in progress merge started with mq disabled)
904 (eg: an in progress merge started with mq disabled)
905
905
906 If no parent are managed by mq, p1 is returned.
906 If no parent are managed by mq, p1 is returned.
907 """
907 """
908 if rev is None:
908 if rev is None:
909 (p1, p2) = repo.dirstate.parents()
909 (p1, p2) = repo.dirstate.parents()
910 if p2 == repo.nullid:
910 if p2 == repo.nullid:
911 return p1
911 return p1
912 if not self.applied:
912 if not self.applied:
913 return None
913 return None
914 return self.applied[-1].node
914 return self.applied[-1].node
915 p1, p2 = repo.changelog.parents(rev)
915 p1, p2 = repo.changelog.parents(rev)
916 if p2 != repo.nullid and p2 in [x.node for x in self.applied]:
916 if p2 != repo.nullid and p2 in [x.node for x in self.applied]:
917 return p2
917 return p2
918 return p1
918 return p1
919
919
920 def mergepatch(self, repo, mergeq, series, diffopts):
920 def mergepatch(self, repo, mergeq, series, diffopts):
921 if not self.applied:
921 if not self.applied:
922 # each of the patches merged in will have two parents. This
922 # each of the patches merged in will have two parents. This
923 # can confuse the qrefresh, qdiff, and strip code because it
923 # can confuse the qrefresh, qdiff, and strip code because it
924 # needs to know which parent is actually in the patch queue.
924 # needs to know which parent is actually in the patch queue.
925 # so, we insert a merge marker with only one parent. This way
925 # so, we insert a merge marker with only one parent. This way
926 # the first patch in the queue is never a merge patch
926 # the first patch in the queue is never a merge patch
927 #
927 #
928 pname = b".hg.patches.merge.marker"
928 pname = b".hg.patches.merge.marker"
929 n = newcommit(repo, None, b'[mq]: merge marker', force=True)
929 n = newcommit(repo, None, b'[mq]: merge marker', force=True)
930 self.removeundo(repo)
930 self.removeundo(repo)
931 self.applied.append(statusentry(n, pname))
931 self.applied.append(statusentry(n, pname))
932 self.applieddirty = True
932 self.applieddirty = True
933
933
934 head = self.qparents(repo)
934 head = self.qparents(repo)
935
935
936 for patch in series:
936 for patch in series:
937 patch = mergeq.lookup(patch, strict=True)
937 patch = mergeq.lookup(patch, strict=True)
938 if not patch:
938 if not patch:
939 self.ui.warn(_(b"patch %s does not exist\n") % patch)
939 self.ui.warn(_(b"patch %s does not exist\n") % patch)
940 return (1, None)
940 return (1, None)
941 pushable, reason = self.pushable(patch)
941 pushable, reason = self.pushable(patch)
942 if not pushable:
942 if not pushable:
943 self.explainpushable(patch, all_patches=True)
943 self.explainpushable(patch, all_patches=True)
944 continue
944 continue
945 info = mergeq.isapplied(patch)
945 info = mergeq.isapplied(patch)
946 if not info:
946 if not info:
947 self.ui.warn(_(b"patch %s is not applied\n") % patch)
947 self.ui.warn(_(b"patch %s is not applied\n") % patch)
948 return (1, None)
948 return (1, None)
949 rev = info[1]
949 rev = info[1]
950 err, head = self.mergeone(repo, mergeq, head, patch, rev, diffopts)
950 err, head = self.mergeone(repo, mergeq, head, patch, rev, diffopts)
951 if head:
951 if head:
952 self.applied.append(statusentry(head, patch))
952 self.applied.append(statusentry(head, patch))
953 self.applieddirty = True
953 self.applieddirty = True
954 if err:
954 if err:
955 return (err, head)
955 return (err, head)
956 self.savedirty()
956 self.savedirty()
957 return (0, head)
957 return (0, head)
958
958
959 def patch(self, repo, patchfile):
959 def patch(self, repo, patchfile):
960 """Apply patchfile to the working directory.
960 """Apply patchfile to the working directory.
961 patchfile: name of patch file"""
961 patchfile: name of patch file"""
962 files = set()
962 files = set()
963 try:
963 try:
964 fuzz = patchmod.patch(
964 fuzz = patchmod.patch(
965 self.ui, repo, patchfile, strip=1, files=files, eolmode=None
965 self.ui, repo, patchfile, strip=1, files=files, eolmode=None
966 )
966 )
967 return (True, list(files), fuzz)
967 return (True, list(files), fuzz)
968 except Exception as inst:
968 except Exception as inst:
969 self.ui.note(stringutil.forcebytestr(inst) + b'\n')
969 self.ui.note(stringutil.forcebytestr(inst) + b'\n')
970 if not self.ui.verbose:
970 if not self.ui.verbose:
971 self.ui.warn(_(b"patch failed, unable to continue (try -v)\n"))
971 self.ui.warn(_(b"patch failed, unable to continue (try -v)\n"))
972 self.ui.traceback()
972 self.ui.traceback()
973 return (False, list(files), False)
973 return (False, list(files), False)
974
974
975 def apply(
975 def apply(
976 self,
976 self,
977 repo,
977 repo,
978 series,
978 series,
979 list=False,
979 list=False,
980 update_status=True,
980 update_status=True,
981 strict=False,
981 strict=False,
982 patchdir=None,
982 patchdir=None,
983 merge=None,
983 merge=None,
984 all_files=None,
984 all_files=None,
985 tobackup=None,
985 tobackup=None,
986 keepchanges=False,
986 keepchanges=False,
987 ):
987 ):
988 wlock = lock = tr = None
988 wlock = lock = tr = None
989 try:
989 try:
990 wlock = repo.wlock()
990 wlock = repo.wlock()
991 lock = repo.lock()
991 lock = repo.lock()
992 tr = repo.transaction(b"qpush")
992 tr = repo.transaction(b"qpush")
993 try:
993 try:
994 ret = self._apply(
994 ret = self._apply(
995 repo,
995 repo,
996 series,
996 series,
997 list,
997 list,
998 update_status,
998 update_status,
999 strict,
999 strict,
1000 patchdir,
1000 patchdir,
1001 merge,
1001 merge,
1002 all_files=all_files,
1002 all_files=all_files,
1003 tobackup=tobackup,
1003 tobackup=tobackup,
1004 keepchanges=keepchanges,
1004 keepchanges=keepchanges,
1005 )
1005 )
1006 tr.close()
1006 tr.close()
1007 self.savedirty()
1007 self.savedirty()
1008 return ret
1008 return ret
1009 except AbortNoCleanup:
1009 except AbortNoCleanup:
1010 tr.close()
1010 tr.close()
1011 self.savedirty()
1011 self.savedirty()
1012 raise
1012 raise
1013 except: # re-raises
1013 except: # re-raises
1014 try:
1014 try:
1015 tr.abort()
1015 tr.abort()
1016 finally:
1016 finally:
1017 self.invalidate()
1017 self.invalidate()
1018 raise
1018 raise
1019 finally:
1019 finally:
1020 release(tr, lock, wlock)
1020 release(tr, lock, wlock)
1021 self.removeundo(repo)
1021 self.removeundo(repo)
1022
1022
1023 def _apply(
1023 def _apply(
1024 self,
1024 self,
1025 repo,
1025 repo,
1026 series,
1026 series,
1027 list=False,
1027 list=False,
1028 update_status=True,
1028 update_status=True,
1029 strict=False,
1029 strict=False,
1030 patchdir=None,
1030 patchdir=None,
1031 merge=None,
1031 merge=None,
1032 all_files=None,
1032 all_files=None,
1033 tobackup=None,
1033 tobackup=None,
1034 keepchanges=False,
1034 keepchanges=False,
1035 ):
1035 ):
1036 """returns (error, hash)
1036 """returns (error, hash)
1037
1037
1038 error = 1 for unable to read, 2 for patch failed, 3 for patch
1038 error = 1 for unable to read, 2 for patch failed, 3 for patch
1039 fuzz. tobackup is None or a set of files to backup before they
1039 fuzz. tobackup is None or a set of files to backup before they
1040 are modified by a patch.
1040 are modified by a patch.
1041 """
1041 """
1042 # TODO unify with commands.py
1042 # TODO unify with commands.py
1043 if not patchdir:
1043 if not patchdir:
1044 patchdir = self.path
1044 patchdir = self.path
1045 err = 0
1045 err = 0
1046 n = None
1046 n = None
1047 for patchname in series:
1047 for patchname in series:
1048 pushable, reason = self.pushable(patchname)
1048 pushable, reason = self.pushable(patchname)
1049 if not pushable:
1049 if not pushable:
1050 self.explainpushable(patchname, all_patches=True)
1050 self.explainpushable(patchname, all_patches=True)
1051 continue
1051 continue
1052 self.ui.status(_(b"applying %s\n") % patchname)
1052 self.ui.status(_(b"applying %s\n") % patchname)
1053 pf = os.path.join(patchdir, patchname)
1053 pf = os.path.join(patchdir, patchname)
1054
1054
1055 try:
1055 try:
1056 ph = patchheader(self.join(patchname), self.plainmode)
1056 ph = patchheader(self.join(patchname), self.plainmode)
1057 except IOError:
1057 except IOError:
1058 self.ui.warn(_(b"unable to read %s\n") % patchname)
1058 self.ui.warn(_(b"unable to read %s\n") % patchname)
1059 err = 1
1059 err = 1
1060 break
1060 break
1061
1061
1062 message = ph.message
1062 message = ph.message
1063 if not message:
1063 if not message:
1064 # The commit message should not be translated
1064 # The commit message should not be translated
1065 message = b"imported patch %s\n" % patchname
1065 message = b"imported patch %s\n" % patchname
1066 else:
1066 else:
1067 if list:
1067 if list:
1068 # The commit message should not be translated
1068 # The commit message should not be translated
1069 message.append(b"\nimported patch %s" % patchname)
1069 message.append(b"\nimported patch %s" % patchname)
1070 message = b'\n'.join(message)
1070 message = b'\n'.join(message)
1071
1071
1072 if ph.haspatch:
1072 if ph.haspatch:
1073 if tobackup:
1073 if tobackup:
1074 touched = patchmod.changedfiles(self.ui, repo, pf)
1074 touched = patchmod.changedfiles(self.ui, repo, pf)
1075 touched = set(touched) & tobackup
1075 touched = set(touched) & tobackup
1076 if touched and keepchanges:
1076 if touched and keepchanges:
1077 raise AbortNoCleanup(
1077 raise AbortNoCleanup(
1078 _(b"conflicting local changes found"),
1078 _(b"conflicting local changes found"),
1079 hint=_(b"did you forget to qrefresh?"),
1079 hint=_(b"did you forget to qrefresh?"),
1080 )
1080 )
1081 self.backup(repo, touched, copy=True)
1081 self.backup(repo, touched, copy=True)
1082 tobackup = tobackup - touched
1082 tobackup = tobackup - touched
1083 (patcherr, files, fuzz) = self.patch(repo, pf)
1083 (patcherr, files, fuzz) = self.patch(repo, pf)
1084 if all_files is not None:
1084 if all_files is not None:
1085 all_files.update(files)
1085 all_files.update(files)
1086 patcherr = not patcherr
1086 patcherr = not patcherr
1087 else:
1087 else:
1088 self.ui.warn(_(b"patch %s is empty\n") % patchname)
1088 self.ui.warn(_(b"patch %s is empty\n") % patchname)
1089 patcherr, files, fuzz = 0, [], 0
1089 patcherr, files, fuzz = 0, [], 0
1090
1090
1091 if merge and files:
1091 if merge and files:
1092 # Mark as removed/merged and update dirstate parent info
1092 # Mark as removed/merged and update dirstate parent info
1093 with repo.dirstate.parentchange():
1093 with repo.dirstate.parentchange():
1094 for f in files:
1094 for f in files:
1095 repo.dirstate.update_file_p1(f, p1_tracked=True)
1095 repo.dirstate.update_file_p1(f, p1_tracked=True)
1096 p1 = repo.dirstate.p1()
1096 p1 = repo.dirstate.p1()
1097 repo.setparents(p1, merge)
1097 repo.setparents(p1, merge)
1098
1098
1099 if all_files and b'.hgsubstate' in all_files:
1099 if all_files and b'.hgsubstate' in all_files:
1100 wctx = repo[None]
1100 wctx = repo[None]
1101 pctx = repo[b'.']
1101 pctx = repo[b'.']
1102 overwrite = False
1102 overwrite = False
1103 mergedsubstate = subrepoutil.submerge(
1103 mergedsubstate = subrepoutil.submerge(
1104 repo, pctx, wctx, wctx, overwrite
1104 repo, pctx, wctx, wctx, overwrite
1105 )
1105 )
1106 files += mergedsubstate.keys()
1106 files += mergedsubstate.keys()
1107
1107
1108 match = scmutil.matchfiles(repo, files or [])
1108 match = scmutil.matchfiles(repo, files or [])
1109 oldtip = repo.changelog.tip()
1109 oldtip = repo.changelog.tip()
1110 n = newcommit(
1110 n = newcommit(
1111 repo, None, message, ph.user, ph.date, match=match, force=True
1111 repo, None, message, ph.user, ph.date, match=match, force=True
1112 )
1112 )
1113 if repo.changelog.tip() == oldtip:
1113 if repo.changelog.tip() == oldtip:
1114 raise error.Abort(
1114 raise error.Abort(
1115 _(b"qpush exactly duplicates child changeset")
1115 _(b"qpush exactly duplicates child changeset")
1116 )
1116 )
1117 if n is None:
1117 if n is None:
1118 raise error.Abort(_(b"repository commit failed"))
1118 raise error.Abort(_(b"repository commit failed"))
1119
1119
1120 if update_status:
1120 if update_status:
1121 self.applied.append(statusentry(n, patchname))
1121 self.applied.append(statusentry(n, patchname))
1122
1122
1123 if patcherr:
1123 if patcherr:
1124 self.ui.warn(
1124 self.ui.warn(
1125 _(b"patch failed, rejects left in working directory\n")
1125 _(b"patch failed, rejects left in working directory\n")
1126 )
1126 )
1127 err = 2
1127 err = 2
1128 break
1128 break
1129
1129
1130 if fuzz and strict:
1130 if fuzz and strict:
1131 self.ui.warn(_(b"fuzz found when applying patch, stopping\n"))
1131 self.ui.warn(_(b"fuzz found when applying patch, stopping\n"))
1132 err = 3
1132 err = 3
1133 break
1133 break
1134 return (err, n)
1134 return (err, n)
1135
1135
1136 def _cleanup(self, patches, numrevs, keep=False):
1136 def _cleanup(self, patches, numrevs, keep=False):
1137 if not keep:
1137 if not keep:
1138 r = self.qrepo()
1138 r = self.qrepo()
1139 if r:
1139 if r:
1140 r[None].forget(patches)
1140 r[None].forget(patches)
1141 for p in patches:
1141 for p in patches:
1142 try:
1142 try:
1143 os.unlink(self.join(p))
1143 os.unlink(self.join(p))
1144 except OSError as inst:
1144 except OSError as inst:
1145 if inst.errno != errno.ENOENT:
1145 if inst.errno != errno.ENOENT:
1146 raise
1146 raise
1147
1147
1148 qfinished = []
1148 qfinished = []
1149 if numrevs:
1149 if numrevs:
1150 qfinished = self.applied[:numrevs]
1150 qfinished = self.applied[:numrevs]
1151 del self.applied[:numrevs]
1151 del self.applied[:numrevs]
1152 self.applieddirty = True
1152 self.applieddirty = True
1153
1153
1154 unknown = []
1154 unknown = []
1155
1155
1156 sortedseries = []
1156 sortedseries = []
1157 for p in patches:
1157 for p in patches:
1158 idx = self.findseries(p)
1158 idx = self.findseries(p)
1159 if idx is None:
1159 if idx is None:
1160 sortedseries.append((-1, p))
1160 sortedseries.append((-1, p))
1161 else:
1161 else:
1162 sortedseries.append((idx, p))
1162 sortedseries.append((idx, p))
1163
1163
1164 sortedseries.sort(reverse=True)
1164 sortedseries.sort(reverse=True)
1165 for (i, p) in sortedseries:
1165 for (i, p) in sortedseries:
1166 if i != -1:
1166 if i != -1:
1167 del self.fullseries[i]
1167 del self.fullseries[i]
1168 else:
1168 else:
1169 unknown.append(p)
1169 unknown.append(p)
1170
1170
1171 if unknown:
1171 if unknown:
1172 if numrevs:
1172 if numrevs:
1173 rev = {entry.name: entry.node for entry in qfinished}
1173 rev = {entry.name: entry.node for entry in qfinished}
1174 for p in unknown:
1174 for p in unknown:
1175 msg = _(b'revision %s refers to unknown patches: %s\n')
1175 msg = _(b'revision %s refers to unknown patches: %s\n')
1176 self.ui.warn(msg % (short(rev[p]), p))
1176 self.ui.warn(msg % (short(rev[p]), p))
1177 else:
1177 else:
1178 msg = _(b'unknown patches: %s\n')
1178 msg = _(b'unknown patches: %s\n')
1179 raise error.Abort(b''.join(msg % p for p in unknown))
1179 raise error.Abort(b''.join(msg % p for p in unknown))
1180
1180
1181 self.parseseries()
1181 self.parseseries()
1182 self.seriesdirty = True
1182 self.seriesdirty = True
1183 return [entry.node for entry in qfinished]
1183 return [entry.node for entry in qfinished]
1184
1184
1185 def _revpatches(self, repo, revs):
1185 def _revpatches(self, repo, revs):
1186 firstrev = repo[self.applied[0].node].rev()
1186 firstrev = repo[self.applied[0].node].rev()
1187 patches = []
1187 patches = []
1188 for i, rev in enumerate(revs):
1188 for i, rev in enumerate(revs):
1189
1189
1190 if rev < firstrev:
1190 if rev < firstrev:
1191 raise error.Abort(_(b'revision %d is not managed') % rev)
1191 raise error.Abort(_(b'revision %d is not managed') % rev)
1192
1192
1193 ctx = repo[rev]
1193 ctx = repo[rev]
1194 base = self.applied[i].node
1194 base = self.applied[i].node
1195 if ctx.node() != base:
1195 if ctx.node() != base:
1196 msg = _(b'cannot delete revision %d above applied patches')
1196 msg = _(b'cannot delete revision %d above applied patches')
1197 raise error.Abort(msg % rev)
1197 raise error.Abort(msg % rev)
1198
1198
1199 patch = self.applied[i].name
1199 patch = self.applied[i].name
1200 for fmt in (b'[mq]: %s', b'imported patch %s'):
1200 for fmt in (b'[mq]: %s', b'imported patch %s'):
1201 if ctx.description() == fmt % patch:
1201 if ctx.description() == fmt % patch:
1202 msg = _(b'patch %s finalized without changeset message\n')
1202 msg = _(b'patch %s finalized without changeset message\n')
1203 repo.ui.status(msg % patch)
1203 repo.ui.status(msg % patch)
1204 break
1204 break
1205
1205
1206 patches.append(patch)
1206 patches.append(patch)
1207 return patches
1207 return patches
1208
1208
1209 def finish(self, repo, revs):
1209 def finish(self, repo, revs):
1210 # Manually trigger phase computation to ensure phasedefaults is
1210 # Manually trigger phase computation to ensure phasedefaults is
1211 # executed before we remove the patches.
1211 # executed before we remove the patches.
1212 repo._phasecache
1212 repo._phasecache
1213 patches = self._revpatches(repo, sorted(revs))
1213 patches = self._revpatches(repo, sorted(revs))
1214 qfinished = self._cleanup(patches, len(patches))
1214 qfinished = self._cleanup(patches, len(patches))
1215 if qfinished and repo.ui.configbool(b'mq', b'secret'):
1215 if qfinished and repo.ui.configbool(b'mq', b'secret'):
1216 # only use this logic when the secret option is added
1216 # only use this logic when the secret option is added
1217 oldqbase = repo[qfinished[0]]
1217 oldqbase = repo[qfinished[0]]
1218 tphase = phases.newcommitphase(repo.ui)
1218 tphase = phases.newcommitphase(repo.ui)
1219 if oldqbase.phase() > tphase and oldqbase.p1().phase() <= tphase:
1219 if oldqbase.phase() > tphase and oldqbase.p1().phase() <= tphase:
1220 with repo.transaction(b'qfinish') as tr:
1220 with repo.transaction(b'qfinish') as tr:
1221 phases.advanceboundary(repo, tr, tphase, qfinished)
1221 phases.advanceboundary(repo, tr, tphase, qfinished)
1222
1222
1223 def delete(self, repo, patches, opts):
1223 def delete(self, repo, patches, opts):
1224 if not patches and not opts.get(b'rev'):
1224 if not patches and not opts.get(b'rev'):
1225 raise error.Abort(
1225 raise error.Abort(
1226 _(b'qdelete requires at least one revision or patch name')
1226 _(b'qdelete requires at least one revision or patch name')
1227 )
1227 )
1228
1228
1229 realpatches = []
1229 realpatches = []
1230 for patch in patches:
1230 for patch in patches:
1231 patch = self.lookup(patch, strict=True)
1231 patch = self.lookup(patch, strict=True)
1232 info = self.isapplied(patch)
1232 info = self.isapplied(patch)
1233 if info:
1233 if info:
1234 raise error.Abort(_(b"cannot delete applied patch %s") % patch)
1234 raise error.Abort(_(b"cannot delete applied patch %s") % patch)
1235 if patch not in self.series:
1235 if patch not in self.series:
1236 raise error.Abort(_(b"patch %s not in series file") % patch)
1236 raise error.Abort(_(b"patch %s not in series file") % patch)
1237 if patch not in realpatches:
1237 if patch not in realpatches:
1238 realpatches.append(patch)
1238 realpatches.append(patch)
1239
1239
1240 numrevs = 0
1240 numrevs = 0
1241 if opts.get(b'rev'):
1241 if opts.get(b'rev'):
1242 if not self.applied:
1242 if not self.applied:
1243 raise error.Abort(_(b'no patches applied'))
1243 raise error.Abort(_(b'no patches applied'))
1244 revs = scmutil.revrange(repo, opts.get(b'rev'))
1244 revs = scmutil.revrange(repo, opts.get(b'rev'))
1245 revs.sort()
1245 revs.sort()
1246 revpatches = self._revpatches(repo, revs)
1246 revpatches = self._revpatches(repo, revs)
1247 realpatches += revpatches
1247 realpatches += revpatches
1248 numrevs = len(revpatches)
1248 numrevs = len(revpatches)
1249
1249
1250 self._cleanup(realpatches, numrevs, opts.get(b'keep'))
1250 self._cleanup(realpatches, numrevs, opts.get(b'keep'))
1251
1251
1252 def checktoppatch(self, repo):
1252 def checktoppatch(self, repo):
1253 '''check that working directory is at qtip'''
1253 '''check that working directory is at qtip'''
1254 if self.applied:
1254 if self.applied:
1255 top = self.applied[-1].node
1255 top = self.applied[-1].node
1256 patch = self.applied[-1].name
1256 patch = self.applied[-1].name
1257 if repo.dirstate.p1() != top:
1257 if repo.dirstate.p1() != top:
1258 raise error.Abort(_(b"working directory revision is not qtip"))
1258 raise error.Abort(_(b"working directory revision is not qtip"))
1259 return top, patch
1259 return top, patch
1260 return None, None
1260 return None, None
1261
1261
1262 def putsubstate2changes(self, substatestate, changes):
1262 def putsubstate2changes(self, substatestate, changes):
1263 if isinstance(changes, list):
1263 if isinstance(changes, list):
1264 mar = changes[:3]
1264 mar = changes[:3]
1265 else:
1265 else:
1266 mar = (changes.modified, changes.added, changes.removed)
1266 mar = (changes.modified, changes.added, changes.removed)
1267 if any((b'.hgsubstate' in files for files in mar)):
1267 if any((b'.hgsubstate' in files for files in mar)):
1268 return # already listed up
1268 return # already listed up
1269 # not yet listed up
1269 # not yet listed up
1270 if substatestate in b'a?':
1270 if substatestate in b'a?':
1271 mar[1].append(b'.hgsubstate')
1271 mar[1].append(b'.hgsubstate')
1272 elif substatestate in b'r':
1272 elif substatestate in b'r':
1273 mar[2].append(b'.hgsubstate')
1273 mar[2].append(b'.hgsubstate')
1274 else: # modified
1274 else: # modified
1275 mar[0].append(b'.hgsubstate')
1275 mar[0].append(b'.hgsubstate')
1276
1276
1277 def checklocalchanges(self, repo, force=False, refresh=True):
1277 def checklocalchanges(self, repo, force=False, refresh=True):
1278 excsuffix = b''
1278 excsuffix = b''
1279 if refresh:
1279 if refresh:
1280 excsuffix = b', qrefresh first'
1280 excsuffix = b', qrefresh first'
1281 # plain versions for i18n tool to detect them
1281 # plain versions for i18n tool to detect them
1282 _(b"local changes found, qrefresh first")
1282 _(b"local changes found, qrefresh first")
1283 _(b"local changed subrepos found, qrefresh first")
1283 _(b"local changed subrepos found, qrefresh first")
1284
1284
1285 s = repo.status()
1285 s = repo.status()
1286 if not force:
1286 if not force:
1287 cmdutil.checkunfinished(repo)
1287 cmdutil.checkunfinished(repo)
1288 if s.modified or s.added or s.removed or s.deleted:
1288 if s.modified or s.added or s.removed or s.deleted:
1289 _(b"local changes found") # i18n tool detection
1289 _(b"local changes found") # i18n tool detection
1290 raise error.Abort(_(b"local changes found" + excsuffix))
1290 raise error.Abort(_(b"local changes found" + excsuffix))
1291 if checksubstate(repo):
1291 if checksubstate(repo):
1292 _(b"local changed subrepos found") # i18n tool detection
1292 _(b"local changed subrepos found") # i18n tool detection
1293 raise error.Abort(
1293 raise error.Abort(
1294 _(b"local changed subrepos found" + excsuffix)
1294 _(b"local changed subrepos found" + excsuffix)
1295 )
1295 )
1296 else:
1296 else:
1297 cmdutil.checkunfinished(repo, skipmerge=True)
1297 cmdutil.checkunfinished(repo, skipmerge=True)
1298 return s
1298 return s
1299
1299
1300 _reserved = (b'series', b'status', b'guards', b'.', b'..')
1300 _reserved = (b'series', b'status', b'guards', b'.', b'..')
1301
1301
1302 def checkreservedname(self, name):
1302 def checkreservedname(self, name):
1303 if name in self._reserved:
1303 if name in self._reserved:
1304 raise error.Abort(
1304 raise error.Abort(
1305 _(b'"%s" cannot be used as the name of a patch') % name
1305 _(b'"%s" cannot be used as the name of a patch') % name
1306 )
1306 )
1307 if name != name.strip():
1307 if name != name.strip():
1308 # whitespace is stripped by parseseries()
1308 # whitespace is stripped by parseseries()
1309 raise error.Abort(
1309 raise error.Abort(
1310 _(b'patch name cannot begin or end with whitespace')
1310 _(b'patch name cannot begin or end with whitespace')
1311 )
1311 )
1312 for prefix in (b'.hg', b'.mq'):
1312 for prefix in (b'.hg', b'.mq'):
1313 if name.startswith(prefix):
1313 if name.startswith(prefix):
1314 raise error.Abort(
1314 raise error.Abort(
1315 _(b'patch name cannot begin with "%s"') % prefix
1315 _(b'patch name cannot begin with "%s"') % prefix
1316 )
1316 )
1317 for c in (b'#', b':', b'\r', b'\n'):
1317 for c in (b'#', b':', b'\r', b'\n'):
1318 if c in name:
1318 if c in name:
1319 raise error.Abort(
1319 raise error.Abort(
1320 _(b'%r cannot be used in the name of a patch')
1320 _(b'%r cannot be used in the name of a patch')
1321 % pycompat.bytestr(c)
1321 % pycompat.bytestr(c)
1322 )
1322 )
1323
1323
1324 def checkpatchname(self, name, force=False):
1324 def checkpatchname(self, name, force=False):
1325 self.checkreservedname(name)
1325 self.checkreservedname(name)
1326 if not force and os.path.exists(self.join(name)):
1326 if not force and os.path.exists(self.join(name)):
1327 if os.path.isdir(self.join(name)):
1327 if os.path.isdir(self.join(name)):
1328 raise error.Abort(
1328 raise error.Abort(
1329 _(b'"%s" already exists as a directory') % name
1329 _(b'"%s" already exists as a directory') % name
1330 )
1330 )
1331 else:
1331 else:
1332 raise error.Abort(_(b'patch "%s" already exists') % name)
1332 raise error.Abort(_(b'patch "%s" already exists') % name)
1333
1333
1334 def makepatchname(self, title, fallbackname):
1334 def makepatchname(self, title, fallbackname):
1335 """Return a suitable filename for title, adding a suffix to make
1335 """Return a suitable filename for title, adding a suffix to make
1336 it unique in the existing list"""
1336 it unique in the existing list"""
1337 namebase = re.sub(br'[\s\W_]+', b'_', title.lower()).strip(b'_')
1337 namebase = re.sub(br'[\s\W_]+', b'_', title.lower()).strip(b'_')
1338 namebase = namebase[:75] # avoid too long name (issue5117)
1338 namebase = namebase[:75] # avoid too long name (issue5117)
1339 if namebase:
1339 if namebase:
1340 try:
1340 try:
1341 self.checkreservedname(namebase)
1341 self.checkreservedname(namebase)
1342 except error.Abort:
1342 except error.Abort:
1343 namebase = fallbackname
1343 namebase = fallbackname
1344 else:
1344 else:
1345 namebase = fallbackname
1345 namebase = fallbackname
1346 name = namebase
1346 name = namebase
1347 i = 0
1347 i = 0
1348 while True:
1348 while True:
1349 if name not in self.fullseries:
1349 if name not in self.fullseries:
1350 try:
1350 try:
1351 self.checkpatchname(name)
1351 self.checkpatchname(name)
1352 break
1352 break
1353 except error.Abort:
1353 except error.Abort:
1354 pass
1354 pass
1355 i += 1
1355 i += 1
1356 name = b'%s__%d' % (namebase, i)
1356 name = b'%s__%d' % (namebase, i)
1357 return name
1357 return name
1358
1358
1359 def checkkeepchanges(self, keepchanges, force):
1359 def checkkeepchanges(self, keepchanges, force):
1360 if force and keepchanges:
1360 if force and keepchanges:
1361 raise error.Abort(_(b'cannot use both --force and --keep-changes'))
1361 raise error.Abort(_(b'cannot use both --force and --keep-changes'))
1362
1362
1363 def new(self, repo, patchfn, *pats, **opts):
1363 def new(self, repo, patchfn, *pats, **opts):
1364 """options:
1364 """options:
1365 msg: a string or a no-argument function returning a string
1365 msg: a string or a no-argument function returning a string
1366 """
1366 """
1367 opts = pycompat.byteskwargs(opts)
1367 opts = pycompat.byteskwargs(opts)
1368 msg = opts.get(b'msg')
1368 msg = opts.get(b'msg')
1369 edit = opts.get(b'edit')
1369 edit = opts.get(b'edit')
1370 editform = opts.get(b'editform', b'mq.qnew')
1370 editform = opts.get(b'editform', b'mq.qnew')
1371 user = opts.get(b'user')
1371 user = opts.get(b'user')
1372 date = opts.get(b'date')
1372 date = opts.get(b'date')
1373 if date:
1373 if date:
1374 date = dateutil.parsedate(date)
1374 date = dateutil.parsedate(date)
1375 diffopts = self.diffopts({b'git': opts.get(b'git')}, plain=True)
1375 diffopts = self.diffopts({b'git': opts.get(b'git')}, plain=True)
1376 if opts.get(b'checkname', True):
1376 if opts.get(b'checkname', True):
1377 self.checkpatchname(patchfn)
1377 self.checkpatchname(patchfn)
1378 inclsubs = checksubstate(repo)
1378 inclsubs = checksubstate(repo)
1379 if inclsubs:
1379 if inclsubs:
1380 substatestate = repo.dirstate[b'.hgsubstate']
1380 substatestate = repo.dirstate[b'.hgsubstate']
1381 if opts.get(b'include') or opts.get(b'exclude') or pats:
1381 if opts.get(b'include') or opts.get(b'exclude') or pats:
1382 # detect missing files in pats
1382 # detect missing files in pats
1383 def badfn(f, msg):
1383 def badfn(f, msg):
1384 if f != b'.hgsubstate': # .hgsubstate is auto-created
1384 if f != b'.hgsubstate': # .hgsubstate is auto-created
1385 raise error.Abort(b'%s: %s' % (f, msg))
1385 raise error.Abort(b'%s: %s' % (f, msg))
1386
1386
1387 match = scmutil.match(repo[None], pats, opts, badfn=badfn)
1387 match = scmutil.match(repo[None], pats, opts, badfn=badfn)
1388 changes = repo.status(match=match)
1388 changes = repo.status(match=match)
1389 else:
1389 else:
1390 changes = self.checklocalchanges(repo, force=True)
1390 changes = self.checklocalchanges(repo, force=True)
1391 commitfiles = list(inclsubs)
1391 commitfiles = list(inclsubs)
1392 commitfiles.extend(changes.modified)
1392 commitfiles.extend(changes.modified)
1393 commitfiles.extend(changes.added)
1393 commitfiles.extend(changes.added)
1394 commitfiles.extend(changes.removed)
1394 commitfiles.extend(changes.removed)
1395 match = scmutil.matchfiles(repo, commitfiles)
1395 match = scmutil.matchfiles(repo, commitfiles)
1396 if len(repo[None].parents()) > 1:
1396 if len(repo[None].parents()) > 1:
1397 raise error.Abort(_(b'cannot manage merge changesets'))
1397 raise error.Abort(_(b'cannot manage merge changesets'))
1398 self.checktoppatch(repo)
1398 self.checktoppatch(repo)
1399 insert = self.fullseriesend()
1399 insert = self.fullseriesend()
1400 with repo.wlock():
1400 with repo.wlock():
1401 try:
1401 try:
1402 # if patch file write fails, abort early
1402 # if patch file write fails, abort early
1403 p = self.opener(patchfn, b"w")
1403 p = self.opener(patchfn, b"w")
1404 except IOError as e:
1404 except IOError as e:
1405 raise error.Abort(
1405 raise error.Abort(
1406 _(b'cannot write patch "%s": %s')
1406 _(b'cannot write patch "%s": %s')
1407 % (patchfn, encoding.strtolocal(e.strerror))
1407 % (patchfn, encoding.strtolocal(e.strerror))
1408 )
1408 )
1409 try:
1409 try:
1410 defaultmsg = b"[mq]: %s" % patchfn
1410 defaultmsg = b"[mq]: %s" % patchfn
1411 editor = cmdutil.getcommiteditor(editform=editform)
1411 editor = cmdutil.getcommiteditor(editform=editform)
1412 if edit:
1412 if edit:
1413
1413
1414 def finishdesc(desc):
1414 def finishdesc(desc):
1415 if desc.rstrip():
1415 if desc.rstrip():
1416 return desc
1416 return desc
1417 else:
1417 else:
1418 return defaultmsg
1418 return defaultmsg
1419
1419
1420 # i18n: this message is shown in editor with "HG: " prefix
1420 # i18n: this message is shown in editor with "HG: " prefix
1421 extramsg = _(b'Leave message empty to use default message.')
1421 extramsg = _(b'Leave message empty to use default message.')
1422 editor = cmdutil.getcommiteditor(
1422 editor = cmdutil.getcommiteditor(
1423 finishdesc=finishdesc,
1423 finishdesc=finishdesc,
1424 extramsg=extramsg,
1424 extramsg=extramsg,
1425 editform=editform,
1425 editform=editform,
1426 )
1426 )
1427 commitmsg = msg
1427 commitmsg = msg
1428 else:
1428 else:
1429 commitmsg = msg or defaultmsg
1429 commitmsg = msg or defaultmsg
1430
1430
1431 n = newcommit(
1431 n = newcommit(
1432 repo,
1432 repo,
1433 None,
1433 None,
1434 commitmsg,
1434 commitmsg,
1435 user,
1435 user,
1436 date,
1436 date,
1437 match=match,
1437 match=match,
1438 force=True,
1438 force=True,
1439 editor=editor,
1439 editor=editor,
1440 )
1440 )
1441 if n is None:
1441 if n is None:
1442 raise error.Abort(_(b"repo commit failed"))
1442 raise error.Abort(_(b"repo commit failed"))
1443 try:
1443 try:
1444 self.fullseries[insert:insert] = [patchfn]
1444 self.fullseries[insert:insert] = [patchfn]
1445 self.applied.append(statusentry(n, patchfn))
1445 self.applied.append(statusentry(n, patchfn))
1446 self.parseseries()
1446 self.parseseries()
1447 self.seriesdirty = True
1447 self.seriesdirty = True
1448 self.applieddirty = True
1448 self.applieddirty = True
1449 nctx = repo[n]
1449 nctx = repo[n]
1450 ph = patchheader(self.join(patchfn), self.plainmode)
1450 ph = patchheader(self.join(patchfn), self.plainmode)
1451 if user:
1451 if user:
1452 ph.setuser(user)
1452 ph.setuser(user)
1453 if date:
1453 if date:
1454 ph.setdate(b'%d %d' % date)
1454 ph.setdate(b'%d %d' % date)
1455 ph.setparent(hex(nctx.p1().node()))
1455 ph.setparent(hex(nctx.p1().node()))
1456 msg = nctx.description().strip()
1456 msg = nctx.description().strip()
1457 if msg == defaultmsg.strip():
1457 if msg == defaultmsg.strip():
1458 msg = b''
1458 msg = b''
1459 ph.setmessage(msg)
1459 ph.setmessage(msg)
1460 p.write(bytes(ph))
1460 p.write(bytes(ph))
1461 if commitfiles:
1461 if commitfiles:
1462 parent = self.qparents(repo, n)
1462 parent = self.qparents(repo, n)
1463 if inclsubs:
1463 if inclsubs:
1464 self.putsubstate2changes(substatestate, changes)
1464 self.putsubstate2changes(substatestate, changes)
1465 chunks = patchmod.diff(
1465 chunks = patchmod.diff(
1466 repo,
1466 repo,
1467 node1=parent,
1467 node1=parent,
1468 node2=n,
1468 node2=n,
1469 changes=changes,
1469 changes=changes,
1470 opts=diffopts,
1470 opts=diffopts,
1471 )
1471 )
1472 for chunk in chunks:
1472 for chunk in chunks:
1473 p.write(chunk)
1473 p.write(chunk)
1474 p.close()
1474 p.close()
1475 r = self.qrepo()
1475 r = self.qrepo()
1476 if r:
1476 if r:
1477 r[None].add([patchfn])
1477 r[None].add([patchfn])
1478 except: # re-raises
1478 except: # re-raises
1479 repo.rollback()
1479 repo.rollback()
1480 raise
1480 raise
1481 except Exception:
1481 except Exception:
1482 patchpath = self.join(patchfn)
1482 patchpath = self.join(patchfn)
1483 try:
1483 try:
1484 os.unlink(patchpath)
1484 os.unlink(patchpath)
1485 except OSError:
1485 except OSError:
1486 self.ui.warn(_(b'error unlinking %s\n') % patchpath)
1486 self.ui.warn(_(b'error unlinking %s\n') % patchpath)
1487 raise
1487 raise
1488 self.removeundo(repo)
1488 self.removeundo(repo)
1489
1489
1490 def isapplied(self, patch):
1490 def isapplied(self, patch):
1491 """returns (index, rev, patch)"""
1491 """returns (index, rev, patch)"""
1492 for i, a in enumerate(self.applied):
1492 for i, a in enumerate(self.applied):
1493 if a.name == patch:
1493 if a.name == patch:
1494 return (i, a.node, a.name)
1494 return (i, a.node, a.name)
1495 return None
1495 return None
1496
1496
1497 # if the exact patch name does not exist, we try a few
1497 # if the exact patch name does not exist, we try a few
1498 # variations. If strict is passed, we try only #1
1498 # variations. If strict is passed, we try only #1
1499 #
1499 #
1500 # 1) a number (as string) to indicate an offset in the series file
1500 # 1) a number (as string) to indicate an offset in the series file
1501 # 2) a unique substring of the patch name was given
1501 # 2) a unique substring of the patch name was given
1502 # 3) patchname[-+]num to indicate an offset in the series file
1502 # 3) patchname[-+]num to indicate an offset in the series file
1503 def lookup(self, patch, strict=False):
1503 def lookup(self, patch, strict=False):
1504 def partialname(s):
1504 def partialname(s):
1505 if s in self.series:
1505 if s in self.series:
1506 return s
1506 return s
1507 matches = [x for x in self.series if s in x]
1507 matches = [x for x in self.series if s in x]
1508 if len(matches) > 1:
1508 if len(matches) > 1:
1509 self.ui.warn(_(b'patch name "%s" is ambiguous:\n') % s)
1509 self.ui.warn(_(b'patch name "%s" is ambiguous:\n') % s)
1510 for m in matches:
1510 for m in matches:
1511 self.ui.warn(b' %s\n' % m)
1511 self.ui.warn(b' %s\n' % m)
1512 return None
1512 return None
1513 if matches:
1513 if matches:
1514 return matches[0]
1514 return matches[0]
1515 if self.series and self.applied:
1515 if self.series and self.applied:
1516 if s == b'qtip':
1516 if s == b'qtip':
1517 return self.series[self.seriesend(True) - 1]
1517 return self.series[self.seriesend(True) - 1]
1518 if s == b'qbase':
1518 if s == b'qbase':
1519 return self.series[0]
1519 return self.series[0]
1520 return None
1520 return None
1521
1521
1522 if patch in self.series:
1522 if patch in self.series:
1523 return patch
1523 return patch
1524
1524
1525 if not os.path.isfile(self.join(patch)):
1525 if not os.path.isfile(self.join(patch)):
1526 try:
1526 try:
1527 sno = int(patch)
1527 sno = int(patch)
1528 except (ValueError, OverflowError):
1528 except (ValueError, OverflowError):
1529 pass
1529 pass
1530 else:
1530 else:
1531 if -len(self.series) <= sno < len(self.series):
1531 if -len(self.series) <= sno < len(self.series):
1532 return self.series[sno]
1532 return self.series[sno]
1533
1533
1534 if not strict:
1534 if not strict:
1535 res = partialname(patch)
1535 res = partialname(patch)
1536 if res:
1536 if res:
1537 return res
1537 return res
1538 minus = patch.rfind(b'-')
1538 minus = patch.rfind(b'-')
1539 if minus >= 0:
1539 if minus >= 0:
1540 res = partialname(patch[:minus])
1540 res = partialname(patch[:minus])
1541 if res:
1541 if res:
1542 i = self.series.index(res)
1542 i = self.series.index(res)
1543 try:
1543 try:
1544 off = int(patch[minus + 1 :] or 1)
1544 off = int(patch[minus + 1 :] or 1)
1545 except (ValueError, OverflowError):
1545 except (ValueError, OverflowError):
1546 pass
1546 pass
1547 else:
1547 else:
1548 if i - off >= 0:
1548 if i - off >= 0:
1549 return self.series[i - off]
1549 return self.series[i - off]
1550 plus = patch.rfind(b'+')
1550 plus = patch.rfind(b'+')
1551 if plus >= 0:
1551 if plus >= 0:
1552 res = partialname(patch[:plus])
1552 res = partialname(patch[:plus])
1553 if res:
1553 if res:
1554 i = self.series.index(res)
1554 i = self.series.index(res)
1555 try:
1555 try:
1556 off = int(patch[plus + 1 :] or 1)
1556 off = int(patch[plus + 1 :] or 1)
1557 except (ValueError, OverflowError):
1557 except (ValueError, OverflowError):
1558 pass
1558 pass
1559 else:
1559 else:
1560 if i + off < len(self.series):
1560 if i + off < len(self.series):
1561 return self.series[i + off]
1561 return self.series[i + off]
1562 raise error.Abort(_(b"patch %s not in series") % patch)
1562 raise error.Abort(_(b"patch %s not in series") % patch)
1563
1563
1564 def push(
1564 def push(
1565 self,
1565 self,
1566 repo,
1566 repo,
1567 patch=None,
1567 patch=None,
1568 force=False,
1568 force=False,
1569 list=False,
1569 list=False,
1570 mergeq=None,
1570 mergeq=None,
1571 all=False,
1571 all=False,
1572 move=False,
1572 move=False,
1573 exact=False,
1573 exact=False,
1574 nobackup=False,
1574 nobackup=False,
1575 keepchanges=False,
1575 keepchanges=False,
1576 ):
1576 ):
1577 self.checkkeepchanges(keepchanges, force)
1577 self.checkkeepchanges(keepchanges, force)
1578 diffopts = self.diffopts()
1578 diffopts = self.diffopts()
1579 with repo.wlock():
1579 with repo.wlock():
1580 heads = []
1580 heads = []
1581 for hs in repo.branchmap().iterheads():
1581 for hs in repo.branchmap().iterheads():
1582 heads.extend(hs)
1582 heads.extend(hs)
1583 if not heads:
1583 if not heads:
1584 heads = [repo.nullid]
1584 heads = [repo.nullid]
1585 if repo.dirstate.p1() not in heads and not exact:
1585 if repo.dirstate.p1() not in heads and not exact:
1586 self.ui.status(_(b"(working directory not at a head)\n"))
1586 self.ui.status(_(b"(working directory not at a head)\n"))
1587
1587
1588 if not self.series:
1588 if not self.series:
1589 self.ui.warn(_(b'no patches in series\n'))
1589 self.ui.warn(_(b'no patches in series\n'))
1590 return 0
1590 return 0
1591
1591
1592 # Suppose our series file is: A B C and the current 'top'
1592 # Suppose our series file is: A B C and the current 'top'
1593 # patch is B. qpush C should be performed (moving forward)
1593 # patch is B. qpush C should be performed (moving forward)
1594 # qpush B is a NOP (no change) qpush A is an error (can't
1594 # qpush B is a NOP (no change) qpush A is an error (can't
1595 # go backwards with qpush)
1595 # go backwards with qpush)
1596 if patch:
1596 if patch:
1597 patch = self.lookup(patch)
1597 patch = self.lookup(patch)
1598 info = self.isapplied(patch)
1598 info = self.isapplied(patch)
1599 if info and info[0] >= len(self.applied) - 1:
1599 if info and info[0] >= len(self.applied) - 1:
1600 self.ui.warn(
1600 self.ui.warn(
1601 _(b'qpush: %s is already at the top\n') % patch
1601 _(b'qpush: %s is already at the top\n') % patch
1602 )
1602 )
1603 return 0
1603 return 0
1604
1604
1605 pushable, reason = self.pushable(patch)
1605 pushable, reason = self.pushable(patch)
1606 if pushable:
1606 if pushable:
1607 if self.series.index(patch) < self.seriesend():
1607 if self.series.index(patch) < self.seriesend():
1608 raise error.Abort(
1608 raise error.Abort(
1609 _(b"cannot push to a previous patch: %s") % patch
1609 _(b"cannot push to a previous patch: %s") % patch
1610 )
1610 )
1611 else:
1611 else:
1612 if reason:
1612 if reason:
1613 reason = _(b'guarded by %s') % reason
1613 reason = _(b'guarded by %s') % reason
1614 else:
1614 else:
1615 reason = _(b'no matching guards')
1615 reason = _(b'no matching guards')
1616 self.ui.warn(
1616 self.ui.warn(
1617 _(b"cannot push '%s' - %s\n") % (patch, reason)
1617 _(b"cannot push '%s' - %s\n") % (patch, reason)
1618 )
1618 )
1619 return 1
1619 return 1
1620 elif all:
1620 elif all:
1621 patch = self.series[-1]
1621 patch = self.series[-1]
1622 if self.isapplied(patch):
1622 if self.isapplied(patch):
1623 self.ui.warn(_(b'all patches are currently applied\n'))
1623 self.ui.warn(_(b'all patches are currently applied\n'))
1624 return 0
1624 return 0
1625
1625
1626 # Following the above example, starting at 'top' of B:
1626 # Following the above example, starting at 'top' of B:
1627 # qpush should be performed (pushes C), but a subsequent
1627 # qpush should be performed (pushes C), but a subsequent
1628 # qpush without an argument is an error (nothing to
1628 # qpush without an argument is an error (nothing to
1629 # apply). This allows a loop of "...while hg qpush..." to
1629 # apply). This allows a loop of "...while hg qpush..." to
1630 # work as it detects an error when done
1630 # work as it detects an error when done
1631 start = self.seriesend()
1631 start = self.seriesend()
1632 if start == len(self.series):
1632 if start == len(self.series):
1633 self.ui.warn(_(b'patch series already fully applied\n'))
1633 self.ui.warn(_(b'patch series already fully applied\n'))
1634 return 1
1634 return 1
1635 if not force and not keepchanges:
1635 if not force and not keepchanges:
1636 self.checklocalchanges(repo, refresh=self.applied)
1636 self.checklocalchanges(repo, refresh=self.applied)
1637
1637
1638 if exact:
1638 if exact:
1639 if keepchanges:
1639 if keepchanges:
1640 raise error.Abort(
1640 raise error.Abort(
1641 _(b"cannot use --exact and --keep-changes together")
1641 _(b"cannot use --exact and --keep-changes together")
1642 )
1642 )
1643 if move:
1643 if move:
1644 raise error.Abort(
1644 raise error.Abort(
1645 _(b'cannot use --exact and --move together')
1645 _(b'cannot use --exact and --move together')
1646 )
1646 )
1647 if self.applied:
1647 if self.applied:
1648 raise error.Abort(
1648 raise error.Abort(
1649 _(b'cannot push --exact with applied patches')
1649 _(b'cannot push --exact with applied patches')
1650 )
1650 )
1651 root = self.series[start]
1651 root = self.series[start]
1652 target = patchheader(self.join(root), self.plainmode).parent
1652 target = patchheader(self.join(root), self.plainmode).parent
1653 if not target:
1653 if not target:
1654 raise error.Abort(
1654 raise error.Abort(
1655 _(b"%s does not have a parent recorded") % root
1655 _(b"%s does not have a parent recorded") % root
1656 )
1656 )
1657 if not repo[target] == repo[b'.']:
1657 if not repo[target] == repo[b'.']:
1658 hg.update(repo, target)
1658 hg.update(repo, target)
1659
1659
1660 if move:
1660 if move:
1661 if not patch:
1661 if not patch:
1662 raise error.Abort(_(b"please specify the patch to move"))
1662 raise error.Abort(_(b"please specify the patch to move"))
1663 for fullstart, rpn in enumerate(self.fullseries):
1663 for fullstart, rpn in enumerate(self.fullseries):
1664 # strip markers for patch guards
1664 # strip markers for patch guards
1665 if self.guard_re.split(rpn, 1)[0] == self.series[start]:
1665 if self.guard_re.split(rpn, 1)[0] == self.series[start]:
1666 break
1666 break
1667 for i, rpn in enumerate(self.fullseries[fullstart:]):
1667 for i, rpn in enumerate(self.fullseries[fullstart:]):
1668 # strip markers for patch guards
1668 # strip markers for patch guards
1669 if self.guard_re.split(rpn, 1)[0] == patch:
1669 if self.guard_re.split(rpn, 1)[0] == patch:
1670 break
1670 break
1671 index = fullstart + i
1671 index = fullstart + i
1672 assert index < len(self.fullseries)
1672 assert index < len(self.fullseries)
1673 fullpatch = self.fullseries[index]
1673 fullpatch = self.fullseries[index]
1674 del self.fullseries[index]
1674 del self.fullseries[index]
1675 self.fullseries.insert(fullstart, fullpatch)
1675 self.fullseries.insert(fullstart, fullpatch)
1676 self.parseseries()
1676 self.parseseries()
1677 self.seriesdirty = True
1677 self.seriesdirty = True
1678
1678
1679 self.applieddirty = True
1679 self.applieddirty = True
1680 if start > 0:
1680 if start > 0:
1681 self.checktoppatch(repo)
1681 self.checktoppatch(repo)
1682 if not patch:
1682 if not patch:
1683 patch = self.series[start]
1683 patch = self.series[start]
1684 end = start + 1
1684 end = start + 1
1685 else:
1685 else:
1686 end = self.series.index(patch, start) + 1
1686 end = self.series.index(patch, start) + 1
1687
1687
1688 tobackup = set()
1688 tobackup = set()
1689 if (not nobackup and force) or keepchanges:
1689 if (not nobackup and force) or keepchanges:
1690 status = self.checklocalchanges(repo, force=True)
1690 status = self.checklocalchanges(repo, force=True)
1691 if keepchanges:
1691 if keepchanges:
1692 tobackup.update(
1692 tobackup.update(
1693 status.modified
1693 status.modified
1694 + status.added
1694 + status.added
1695 + status.removed
1695 + status.removed
1696 + status.deleted
1696 + status.deleted
1697 )
1697 )
1698 else:
1698 else:
1699 tobackup.update(status.modified + status.added)
1699 tobackup.update(status.modified + status.added)
1700
1700
1701 s = self.series[start:end]
1701 s = self.series[start:end]
1702 all_files = set()
1702 all_files = set()
1703 try:
1703 try:
1704 if mergeq:
1704 if mergeq:
1705 ret = self.mergepatch(repo, mergeq, s, diffopts)
1705 ret = self.mergepatch(repo, mergeq, s, diffopts)
1706 else:
1706 else:
1707 ret = self.apply(
1707 ret = self.apply(
1708 repo,
1708 repo,
1709 s,
1709 s,
1710 list,
1710 list,
1711 all_files=all_files,
1711 all_files=all_files,
1712 tobackup=tobackup,
1712 tobackup=tobackup,
1713 keepchanges=keepchanges,
1713 keepchanges=keepchanges,
1714 )
1714 )
1715 except AbortNoCleanup:
1715 except AbortNoCleanup:
1716 raise
1716 raise
1717 except: # re-raises
1717 except: # re-raises
1718 self.ui.warn(_(b'cleaning up working directory...\n'))
1718 self.ui.warn(_(b'cleaning up working directory...\n'))
1719 cmdutil.revert(
1719 cmdutil.revert(
1720 self.ui,
1720 self.ui,
1721 repo,
1721 repo,
1722 repo[b'.'],
1722 repo[b'.'],
1723 no_backup=True,
1723 no_backup=True,
1724 )
1724 )
1725 # only remove unknown files that we know we touched or
1725 # only remove unknown files that we know we touched or
1726 # created while patching
1726 # created while patching
1727 for f in all_files:
1727 for f in all_files:
1728 if f not in repo.dirstate:
1728 if f not in repo.dirstate:
1729 repo.wvfs.unlinkpath(f, ignoremissing=True)
1729 repo.wvfs.unlinkpath(f, ignoremissing=True)
1730 self.ui.warn(_(b'done\n'))
1730 self.ui.warn(_(b'done\n'))
1731 raise
1731 raise
1732
1732
1733 if not self.applied:
1733 if not self.applied:
1734 return ret[0]
1734 return ret[0]
1735 top = self.applied[-1].name
1735 top = self.applied[-1].name
1736 if ret[0] and ret[0] > 1:
1736 if ret[0] and ret[0] > 1:
1737 msg = _(b"errors during apply, please fix and qrefresh %s\n")
1737 msg = _(b"errors during apply, please fix and qrefresh %s\n")
1738 self.ui.write(msg % top)
1738 self.ui.write(msg % top)
1739 else:
1739 else:
1740 self.ui.write(_(b"now at: %s\n") % top)
1740 self.ui.write(_(b"now at: %s\n") % top)
1741 return ret[0]
1741 return ret[0]
1742
1742
1743 def pop(
1743 def pop(
1744 self,
1744 self,
1745 repo,
1745 repo,
1746 patch=None,
1746 patch=None,
1747 force=False,
1747 force=False,
1748 update=True,
1748 update=True,
1749 all=False,
1749 all=False,
1750 nobackup=False,
1750 nobackup=False,
1751 keepchanges=False,
1751 keepchanges=False,
1752 ):
1752 ):
1753 self.checkkeepchanges(keepchanges, force)
1753 self.checkkeepchanges(keepchanges, force)
1754 with repo.wlock():
1754 with repo.wlock():
1755 if patch:
1755 if patch:
1756 # index, rev, patch
1756 # index, rev, patch
1757 info = self.isapplied(patch)
1757 info = self.isapplied(patch)
1758 if not info:
1758 if not info:
1759 patch = self.lookup(patch)
1759 patch = self.lookup(patch)
1760 info = self.isapplied(patch)
1760 info = self.isapplied(patch)
1761 if not info:
1761 if not info:
1762 raise error.Abort(_(b"patch %s is not applied") % patch)
1762 raise error.Abort(_(b"patch %s is not applied") % patch)
1763
1763
1764 if not self.applied:
1764 if not self.applied:
1765 # Allow qpop -a to work repeatedly,
1765 # Allow qpop -a to work repeatedly,
1766 # but not qpop without an argument
1766 # but not qpop without an argument
1767 self.ui.warn(_(b"no patches applied\n"))
1767 self.ui.warn(_(b"no patches applied\n"))
1768 return not all
1768 return not all
1769
1769
1770 if all:
1770 if all:
1771 start = 0
1771 start = 0
1772 elif patch:
1772 elif patch:
1773 start = info[0] + 1
1773 start = info[0] + 1
1774 else:
1774 else:
1775 start = len(self.applied) - 1
1775 start = len(self.applied) - 1
1776
1776
1777 if start >= len(self.applied):
1777 if start >= len(self.applied):
1778 self.ui.warn(_(b"qpop: %s is already at the top\n") % patch)
1778 self.ui.warn(_(b"qpop: %s is already at the top\n") % patch)
1779 return
1779 return
1780
1780
1781 if not update:
1781 if not update:
1782 parents = repo.dirstate.parents()
1782 parents = repo.dirstate.parents()
1783 rr = [x.node for x in self.applied]
1783 rr = [x.node for x in self.applied]
1784 for p in parents:
1784 for p in parents:
1785 if p in rr:
1785 if p in rr:
1786 self.ui.warn(_(b"qpop: forcing dirstate update\n"))
1786 self.ui.warn(_(b"qpop: forcing dirstate update\n"))
1787 update = True
1787 update = True
1788 else:
1788 else:
1789 parents = [p.node() for p in repo[None].parents()]
1789 parents = [p.node() for p in repo[None].parents()]
1790 update = any(
1790 update = any(
1791 entry.node in parents for entry in self.applied[start:]
1791 entry.node in parents for entry in self.applied[start:]
1792 )
1792 )
1793
1793
1794 tobackup = set()
1794 tobackup = set()
1795 if update:
1795 if update:
1796 s = self.checklocalchanges(repo, force=force or keepchanges)
1796 s = self.checklocalchanges(repo, force=force or keepchanges)
1797 if force:
1797 if force:
1798 if not nobackup:
1798 if not nobackup:
1799 tobackup.update(s.modified + s.added)
1799 tobackup.update(s.modified + s.added)
1800 elif keepchanges:
1800 elif keepchanges:
1801 tobackup.update(
1801 tobackup.update(
1802 s.modified + s.added + s.removed + s.deleted
1802 s.modified + s.added + s.removed + s.deleted
1803 )
1803 )
1804
1804
1805 self.applieddirty = True
1805 self.applieddirty = True
1806 end = len(self.applied)
1806 end = len(self.applied)
1807 rev = self.applied[start].node
1807 rev = self.applied[start].node
1808
1808
1809 try:
1809 try:
1810 heads = repo.changelog.heads(rev)
1810 heads = repo.changelog.heads(rev)
1811 except error.LookupError:
1811 except error.LookupError:
1812 node = short(rev)
1812 node = short(rev)
1813 raise error.Abort(_(b'trying to pop unknown node %s') % node)
1813 raise error.Abort(_(b'trying to pop unknown node %s') % node)
1814
1814
1815 if heads != [self.applied[-1].node]:
1815 if heads != [self.applied[-1].node]:
1816 raise error.Abort(
1816 raise error.Abort(
1817 _(
1817 _(
1818 b"popping would remove a revision not "
1818 b"popping would remove a revision not "
1819 b"managed by this patch queue"
1819 b"managed by this patch queue"
1820 )
1820 )
1821 )
1821 )
1822 if not repo[self.applied[-1].node].mutable():
1822 if not repo[self.applied[-1].node].mutable():
1823 raise error.Abort(
1823 raise error.Abort(
1824 _(b"popping would remove a public revision"),
1824 _(b"popping would remove a public revision"),
1825 hint=_(b"see 'hg help phases' for details"),
1825 hint=_(b"see 'hg help phases' for details"),
1826 )
1826 )
1827
1827
1828 # we know there are no local changes, so we can make a simplified
1828 # we know there are no local changes, so we can make a simplified
1829 # form of hg.update.
1829 # form of hg.update.
1830 if update:
1830 if update:
1831 qp = self.qparents(repo, rev)
1831 qp = self.qparents(repo, rev)
1832 ctx = repo[qp]
1832 ctx = repo[qp]
1833 st = repo.status(qp, b'.')
1833 st = repo.status(qp, b'.')
1834 m, a, r, d = st.modified, st.added, st.removed, st.deleted
1834 m, a, r, d = st.modified, st.added, st.removed, st.deleted
1835 if d:
1835 if d:
1836 raise error.Abort(_(b"deletions found between repo revs"))
1836 raise error.Abort(_(b"deletions found between repo revs"))
1837
1837
1838 tobackup = set(a + m + r) & tobackup
1838 tobackup = set(a + m + r) & tobackup
1839 if keepchanges and tobackup:
1839 if keepchanges and tobackup:
1840 raise error.Abort(_(b"local changes found, qrefresh first"))
1840 raise error.Abort(_(b"local changes found, qrefresh first"))
1841 self.backup(repo, tobackup)
1841 self.backup(repo, tobackup)
1842 with repo.dirstate.parentchange():
1842 with repo.dirstate.parentchange():
1843 for f in a:
1843 for f in a:
1844 repo.wvfs.unlinkpath(f, ignoremissing=True)
1844 repo.wvfs.unlinkpath(f, ignoremissing=True)
1845 repo.dirstate.update_file(
1845 repo.dirstate.update_file(
1846 f, p1_tracked=False, wc_tracked=False
1846 f, p1_tracked=False, wc_tracked=False
1847 )
1847 )
1848 for f in m + r:
1848 for f in m + r:
1849 fctx = ctx[f]
1849 fctx = ctx[f]
1850 repo.wwrite(f, fctx.data(), fctx.flags())
1850 repo.wwrite(f, fctx.data(), fctx.flags())
1851 repo.dirstate.update_file(
1851 repo.dirstate.update_file(
1852 f, p1_tracked=True, wc_tracked=True
1852 f, p1_tracked=True, wc_tracked=True
1853 )
1853 )
1854 repo.setparents(qp, repo.nullid)
1854 repo.setparents(qp, repo.nullid)
1855 for patch in reversed(self.applied[start:end]):
1855 for patch in reversed(self.applied[start:end]):
1856 self.ui.status(_(b"popping %s\n") % patch.name)
1856 self.ui.status(_(b"popping %s\n") % patch.name)
1857 del self.applied[start:end]
1857 del self.applied[start:end]
1858 strip(self.ui, repo, [rev], update=False, backup=False)
1858 strip(self.ui, repo, [rev], update=False, backup=False)
1859 for s, state in repo[b'.'].substate.items():
1859 for s, state in repo[b'.'].substate.items():
1860 repo[b'.'].sub(s).get(state)
1860 repo[b'.'].sub(s).get(state)
1861 if self.applied:
1861 if self.applied:
1862 self.ui.write(_(b"now at: %s\n") % self.applied[-1].name)
1862 self.ui.write(_(b"now at: %s\n") % self.applied[-1].name)
1863 else:
1863 else:
1864 self.ui.write(_(b"patch queue now empty\n"))
1864 self.ui.write(_(b"patch queue now empty\n"))
1865
1865
1866 def diff(self, repo, pats, opts):
1866 def diff(self, repo, pats, opts):
1867 top, patch = self.checktoppatch(repo)
1867 top, patch = self.checktoppatch(repo)
1868 if not top:
1868 if not top:
1869 self.ui.write(_(b"no patches applied\n"))
1869 self.ui.write(_(b"no patches applied\n"))
1870 return
1870 return
1871 qp = self.qparents(repo, top)
1871 qp = self.qparents(repo, top)
1872 if opts.get(b'reverse'):
1872 if opts.get(b'reverse'):
1873 node1, node2 = None, qp
1873 node1, node2 = None, qp
1874 else:
1874 else:
1875 node1, node2 = qp, None
1875 node1, node2 = qp, None
1876 diffopts = self.diffopts(opts, patch)
1876 diffopts = self.diffopts(opts, patch)
1877 self.printdiff(repo, diffopts, node1, node2, files=pats, opts=opts)
1877 self.printdiff(repo, diffopts, node1, node2, files=pats, opts=opts)
1878
1878
1879 def refresh(self, repo, pats=None, **opts):
1879 def refresh(self, repo, pats=None, **opts):
1880 opts = pycompat.byteskwargs(opts)
1880 opts = pycompat.byteskwargs(opts)
1881 if not self.applied:
1881 if not self.applied:
1882 self.ui.write(_(b"no patches applied\n"))
1882 self.ui.write(_(b"no patches applied\n"))
1883 return 1
1883 return 1
1884 msg = opts.get(b'msg', b'').rstrip()
1884 msg = opts.get(b'msg', b'').rstrip()
1885 edit = opts.get(b'edit')
1885 edit = opts.get(b'edit')
1886 editform = opts.get(b'editform', b'mq.qrefresh')
1886 editform = opts.get(b'editform', b'mq.qrefresh')
1887 newuser = opts.get(b'user')
1887 newuser = opts.get(b'user')
1888 newdate = opts.get(b'date')
1888 newdate = opts.get(b'date')
1889 if newdate:
1889 if newdate:
1890 newdate = b'%d %d' % dateutil.parsedate(newdate)
1890 newdate = b'%d %d' % dateutil.parsedate(newdate)
1891 wlock = repo.wlock()
1891 wlock = repo.wlock()
1892
1892
1893 try:
1893 try:
1894 self.checktoppatch(repo)
1894 self.checktoppatch(repo)
1895 (top, patchfn) = (self.applied[-1].node, self.applied[-1].name)
1895 (top, patchfn) = (self.applied[-1].node, self.applied[-1].name)
1896 if repo.changelog.heads(top) != [top]:
1896 if repo.changelog.heads(top) != [top]:
1897 raise error.Abort(
1897 raise error.Abort(
1898 _(b"cannot qrefresh a revision with children")
1898 _(b"cannot qrefresh a revision with children")
1899 )
1899 )
1900 if not repo[top].mutable():
1900 if not repo[top].mutable():
1901 raise error.Abort(
1901 raise error.Abort(
1902 _(b"cannot qrefresh public revision"),
1902 _(b"cannot qrefresh public revision"),
1903 hint=_(b"see 'hg help phases' for details"),
1903 hint=_(b"see 'hg help phases' for details"),
1904 )
1904 )
1905
1905
1906 cparents = repo.changelog.parents(top)
1906 cparents = repo.changelog.parents(top)
1907 patchparent = self.qparents(repo, top)
1907 patchparent = self.qparents(repo, top)
1908
1908
1909 inclsubs = checksubstate(repo, patchparent)
1909 inclsubs = checksubstate(repo, patchparent)
1910 if inclsubs:
1910 if inclsubs:
1911 substatestate = repo.dirstate[b'.hgsubstate']
1911 substatestate = repo.dirstate[b'.hgsubstate']
1912
1912
1913 ph = patchheader(self.join(patchfn), self.plainmode)
1913 ph = patchheader(self.join(patchfn), self.plainmode)
1914 diffopts = self.diffopts(
1914 diffopts = self.diffopts(
1915 {b'git': opts.get(b'git')}, patchfn, plain=True
1915 {b'git': opts.get(b'git')}, patchfn, plain=True
1916 )
1916 )
1917 if newuser:
1917 if newuser:
1918 ph.setuser(newuser)
1918 ph.setuser(newuser)
1919 if newdate:
1919 if newdate:
1920 ph.setdate(newdate)
1920 ph.setdate(newdate)
1921 ph.setparent(hex(patchparent))
1921 ph.setparent(hex(patchparent))
1922
1922
1923 # only commit new patch when write is complete
1923 # only commit new patch when write is complete
1924 patchf = self.opener(patchfn, b'w', atomictemp=True)
1924 patchf = self.opener(patchfn, b'w', atomictemp=True)
1925
1925
1926 # update the dirstate in place, strip off the qtip commit
1926 # update the dirstate in place, strip off the qtip commit
1927 # and then commit.
1927 # and then commit.
1928 #
1928 #
1929 # this should really read:
1929 # this should really read:
1930 # st = repo.status(top, patchparent)
1930 # st = repo.status(top, patchparent)
1931 # but we do it backwards to take advantage of manifest/changelog
1931 # but we do it backwards to take advantage of manifest/changelog
1932 # caching against the next repo.status call
1932 # caching against the next repo.status call
1933 st = repo.status(patchparent, top)
1933 st = repo.status(patchparent, top)
1934 mm, aa, dd = st.modified, st.added, st.removed
1934 mm, aa, dd = st.modified, st.added, st.removed
1935 ctx = repo[top]
1935 ctx = repo[top]
1936 aaa = aa[:]
1936 aaa = aa[:]
1937 match1 = scmutil.match(repo[None], pats, opts)
1937 match1 = scmutil.match(repo[None], pats, opts)
1938 # in short mode, we only diff the files included in the
1938 # in short mode, we only diff the files included in the
1939 # patch already plus specified files
1939 # patch already plus specified files
1940 if opts.get(b'short'):
1940 if opts.get(b'short'):
1941 # if amending a patch, we start with existing
1941 # if amending a patch, we start with existing
1942 # files plus specified files - unfiltered
1942 # files plus specified files - unfiltered
1943 match = scmutil.matchfiles(repo, mm + aa + dd + match1.files())
1943 match = scmutil.matchfiles(repo, mm + aa + dd + match1.files())
1944 # filter with include/exclude options
1944 # filter with include/exclude options
1945 match1 = scmutil.match(repo[None], opts=opts)
1945 match1 = scmutil.match(repo[None], opts=opts)
1946 else:
1946 else:
1947 match = scmutil.matchall(repo)
1947 match = scmutil.matchall(repo)
1948 stb = repo.status(match=match)
1948 stb = repo.status(match=match)
1949 m, a, r, d = stb.modified, stb.added, stb.removed, stb.deleted
1949 m, a, r, d = stb.modified, stb.added, stb.removed, stb.deleted
1950 mm = set(mm)
1950 mm = set(mm)
1951 aa = set(aa)
1951 aa = set(aa)
1952 dd = set(dd)
1952 dd = set(dd)
1953
1953
1954 # we might end up with files that were added between
1954 # we might end up with files that were added between
1955 # qtip and the dirstate parent, but then changed in the
1955 # qtip and the dirstate parent, but then changed in the
1956 # local dirstate. in this case, we want them to only
1956 # local dirstate. in this case, we want them to only
1957 # show up in the added section
1957 # show up in the added section
1958 for x in m:
1958 for x in m:
1959 if x not in aa:
1959 if x not in aa:
1960 mm.add(x)
1960 mm.add(x)
1961 # we might end up with files added by the local dirstate that
1961 # we might end up with files added by the local dirstate that
1962 # were deleted by the patch. In this case, they should only
1962 # were deleted by the patch. In this case, they should only
1963 # show up in the changed section.
1963 # show up in the changed section.
1964 for x in a:
1964 for x in a:
1965 if x in dd:
1965 if x in dd:
1966 dd.remove(x)
1966 dd.remove(x)
1967 mm.add(x)
1967 mm.add(x)
1968 else:
1968 else:
1969 aa.add(x)
1969 aa.add(x)
1970 # make sure any files deleted in the local dirstate
1970 # make sure any files deleted in the local dirstate
1971 # are not in the add or change column of the patch
1971 # are not in the add or change column of the patch
1972 forget = []
1972 forget = []
1973 for x in d + r:
1973 for x in d + r:
1974 if x in aa:
1974 if x in aa:
1975 aa.remove(x)
1975 aa.remove(x)
1976 forget.append(x)
1976 forget.append(x)
1977 continue
1977 continue
1978 else:
1978 else:
1979 mm.discard(x)
1979 mm.discard(x)
1980 dd.add(x)
1980 dd.add(x)
1981
1981
1982 m = list(mm)
1982 m = list(mm)
1983 r = list(dd)
1983 r = list(dd)
1984 a = list(aa)
1984 a = list(aa)
1985
1985
1986 # create 'match' that includes the files to be recommitted.
1986 # create 'match' that includes the files to be recommitted.
1987 # apply match1 via repo.status to ensure correct case handling.
1987 # apply match1 via repo.status to ensure correct case handling.
1988 st = repo.status(patchparent, match=match1)
1988 st = repo.status(patchparent, match=match1)
1989 cm, ca, cr, cd = st.modified, st.added, st.removed, st.deleted
1989 cm, ca, cr, cd = st.modified, st.added, st.removed, st.deleted
1990 allmatches = set(cm + ca + cr + cd)
1990 allmatches = set(cm + ca + cr + cd)
1991 refreshchanges = [x.intersection(allmatches) for x in (mm, aa, dd)]
1991 refreshchanges = [x.intersection(allmatches) for x in (mm, aa, dd)]
1992
1992
1993 files = set(inclsubs)
1993 files = set(inclsubs)
1994 for x in refreshchanges:
1994 for x in refreshchanges:
1995 files.update(x)
1995 files.update(x)
1996 match = scmutil.matchfiles(repo, files)
1996 match = scmutil.matchfiles(repo, files)
1997
1997
1998 bmlist = repo[top].bookmarks()
1998 bmlist = repo[top].bookmarks()
1999
1999
2000 with repo.dirstate.parentchange():
2000 with repo.dirstate.parentchange():
2001 # XXX do we actually need the dirstateguard
2001 # XXX do we actually need the dirstateguard
2002 dsguard = None
2002 dsguard = None
2003 try:
2003 try:
2004 dsguard = dirstateguard.dirstateguard(repo, b'mq.refresh')
2004 dsguard = dirstateguard.dirstateguard(repo, b'mq.refresh')
2005 if diffopts.git or diffopts.upgrade:
2005 if diffopts.git or diffopts.upgrade:
2006 copies = {}
2006 copies = {}
2007 for dst in a:
2007 for dst in a:
2008 src = repo.dirstate.copied(dst)
2008 src = repo.dirstate.copied(dst)
2009 # during qfold, the source file for copies may
2009 # during qfold, the source file for copies may
2010 # be removed. Treat this as a simple add.
2010 # be removed. Treat this as a simple add.
2011 if src is not None and src in repo.dirstate:
2011 if src is not None and src in repo.dirstate:
2012 copies.setdefault(src, []).append(dst)
2012 copies.setdefault(src, []).append(dst)
2013 repo.dirstate.add(dst)
2013 repo.dirstate.update_file(
2014 dst, p1_tracked=False, wc_tracked=True
2015 )
2014 # remember the copies between patchparent and qtip
2016 # remember the copies between patchparent and qtip
2015 for dst in aaa:
2017 for dst in aaa:
2016 src = ctx[dst].copysource()
2018 src = ctx[dst].copysource()
2017 if src:
2019 if src:
2018 copies.setdefault(src, []).extend(
2020 copies.setdefault(src, []).extend(
2019 copies.get(dst, [])
2021 copies.get(dst, [])
2020 )
2022 )
2021 if dst in a:
2023 if dst in a:
2022 copies[src].append(dst)
2024 copies[src].append(dst)
2023 # we can't copy a file created by the patch itself
2025 # we can't copy a file created by the patch itself
2024 if dst in copies:
2026 if dst in copies:
2025 del copies[dst]
2027 del copies[dst]
2026 for src, dsts in pycompat.iteritems(copies):
2028 for src, dsts in pycompat.iteritems(copies):
2027 for dst in dsts:
2029 for dst in dsts:
2028 repo.dirstate.copy(src, dst)
2030 repo.dirstate.copy(src, dst)
2029 else:
2031 else:
2030 for dst in a:
2032 for dst in a:
2031 repo.dirstate.add(dst)
2033 repo.dirstate.update_file(
2034 dst, p1_tracked=False, wc_tracked=True
2035 )
2032 # Drop useless copy information
2036 # Drop useless copy information
2033 for f in list(repo.dirstate.copies()):
2037 for f in list(repo.dirstate.copies()):
2034 repo.dirstate.copy(None, f)
2038 repo.dirstate.copy(None, f)
2035 for f in r:
2039 for f in r:
2036 repo.dirstate.update_file_p1(f, p1_tracked=True)
2040 repo.dirstate.update_file_p1(f, p1_tracked=True)
2037 # if the patch excludes a modified file, mark that
2041 # if the patch excludes a modified file, mark that
2038 # file with mtime=0 so status can see it.
2042 # file with mtime=0 so status can see it.
2039 mm = []
2043 mm = []
2040 for i in pycompat.xrange(len(m) - 1, -1, -1):
2044 for i in pycompat.xrange(len(m) - 1, -1, -1):
2041 if not match1(m[i]):
2045 if not match1(m[i]):
2042 mm.append(m[i])
2046 mm.append(m[i])
2043 del m[i]
2047 del m[i]
2044 for f in m:
2048 for f in m:
2045 repo.dirstate.update_file_p1(f, p1_tracked=True)
2049 repo.dirstate.update_file_p1(f, p1_tracked=True)
2046 for f in mm:
2050 for f in mm:
2047 repo.dirstate.update_file_p1(f, p1_tracked=True)
2051 repo.dirstate.update_file_p1(f, p1_tracked=True)
2048 for f in forget:
2052 for f in forget:
2049 repo.dirstate.update_file_p1(f, p1_tracked=False)
2053 repo.dirstate.update_file_p1(f, p1_tracked=False)
2050
2054
2051 user = ph.user or ctx.user()
2055 user = ph.user or ctx.user()
2052
2056
2053 oldphase = repo[top].phase()
2057 oldphase = repo[top].phase()
2054
2058
2055 # assumes strip can roll itself back if interrupted
2059 # assumes strip can roll itself back if interrupted
2056 repo.setparents(*cparents)
2060 repo.setparents(*cparents)
2057 self.applied.pop()
2061 self.applied.pop()
2058 self.applieddirty = True
2062 self.applieddirty = True
2059 strip(self.ui, repo, [top], update=False, backup=False)
2063 strip(self.ui, repo, [top], update=False, backup=False)
2060 dsguard.close()
2064 dsguard.close()
2061 finally:
2065 finally:
2062 release(dsguard)
2066 release(dsguard)
2063
2067
2064 try:
2068 try:
2065 # might be nice to attempt to roll back strip after this
2069 # might be nice to attempt to roll back strip after this
2066
2070
2067 defaultmsg = b"[mq]: %s" % patchfn
2071 defaultmsg = b"[mq]: %s" % patchfn
2068 editor = cmdutil.getcommiteditor(editform=editform)
2072 editor = cmdutil.getcommiteditor(editform=editform)
2069 if edit:
2073 if edit:
2070
2074
2071 def finishdesc(desc):
2075 def finishdesc(desc):
2072 if desc.rstrip():
2076 if desc.rstrip():
2073 ph.setmessage(desc)
2077 ph.setmessage(desc)
2074 return desc
2078 return desc
2075 return defaultmsg
2079 return defaultmsg
2076
2080
2077 # i18n: this message is shown in editor with "HG: " prefix
2081 # i18n: this message is shown in editor with "HG: " prefix
2078 extramsg = _(b'Leave message empty to use default message.')
2082 extramsg = _(b'Leave message empty to use default message.')
2079 editor = cmdutil.getcommiteditor(
2083 editor = cmdutil.getcommiteditor(
2080 finishdesc=finishdesc,
2084 finishdesc=finishdesc,
2081 extramsg=extramsg,
2085 extramsg=extramsg,
2082 editform=editform,
2086 editform=editform,
2083 )
2087 )
2084 message = msg or b"\n".join(ph.message)
2088 message = msg or b"\n".join(ph.message)
2085 elif not msg:
2089 elif not msg:
2086 if not ph.message:
2090 if not ph.message:
2087 message = defaultmsg
2091 message = defaultmsg
2088 else:
2092 else:
2089 message = b"\n".join(ph.message)
2093 message = b"\n".join(ph.message)
2090 else:
2094 else:
2091 message = msg
2095 message = msg
2092 ph.setmessage(msg)
2096 ph.setmessage(msg)
2093
2097
2094 # Ensure we create a new changeset in the same phase than
2098 # Ensure we create a new changeset in the same phase than
2095 # the old one.
2099 # the old one.
2096 lock = tr = None
2100 lock = tr = None
2097 try:
2101 try:
2098 lock = repo.lock()
2102 lock = repo.lock()
2099 tr = repo.transaction(b'mq')
2103 tr = repo.transaction(b'mq')
2100 n = newcommit(
2104 n = newcommit(
2101 repo,
2105 repo,
2102 oldphase,
2106 oldphase,
2103 message,
2107 message,
2104 user,
2108 user,
2105 ph.date,
2109 ph.date,
2106 match=match,
2110 match=match,
2107 force=True,
2111 force=True,
2108 editor=editor,
2112 editor=editor,
2109 )
2113 )
2110 # only write patch after a successful commit
2114 # only write patch after a successful commit
2111 c = [list(x) for x in refreshchanges]
2115 c = [list(x) for x in refreshchanges]
2112 if inclsubs:
2116 if inclsubs:
2113 self.putsubstate2changes(substatestate, c)
2117 self.putsubstate2changes(substatestate, c)
2114 chunks = patchmod.diff(
2118 chunks = patchmod.diff(
2115 repo, patchparent, changes=c, opts=diffopts
2119 repo, patchparent, changes=c, opts=diffopts
2116 )
2120 )
2117 comments = bytes(ph)
2121 comments = bytes(ph)
2118 if comments:
2122 if comments:
2119 patchf.write(comments)
2123 patchf.write(comments)
2120 for chunk in chunks:
2124 for chunk in chunks:
2121 patchf.write(chunk)
2125 patchf.write(chunk)
2122 patchf.close()
2126 patchf.close()
2123
2127
2124 marks = repo._bookmarks
2128 marks = repo._bookmarks
2125 marks.applychanges(repo, tr, [(bm, n) for bm in bmlist])
2129 marks.applychanges(repo, tr, [(bm, n) for bm in bmlist])
2126 tr.close()
2130 tr.close()
2127
2131
2128 self.applied.append(statusentry(n, patchfn))
2132 self.applied.append(statusentry(n, patchfn))
2129 finally:
2133 finally:
2130 lockmod.release(tr, lock)
2134 lockmod.release(tr, lock)
2131 except: # re-raises
2135 except: # re-raises
2132 ctx = repo[cparents[0]]
2136 ctx = repo[cparents[0]]
2133 repo.dirstate.rebuild(ctx.node(), ctx.manifest())
2137 repo.dirstate.rebuild(ctx.node(), ctx.manifest())
2134 self.savedirty()
2138 self.savedirty()
2135 self.ui.warn(
2139 self.ui.warn(
2136 _(
2140 _(
2137 b'qrefresh interrupted while patch was popped! '
2141 b'qrefresh interrupted while patch was popped! '
2138 b'(revert --all, qpush to recover)\n'
2142 b'(revert --all, qpush to recover)\n'
2139 )
2143 )
2140 )
2144 )
2141 raise
2145 raise
2142 finally:
2146 finally:
2143 wlock.release()
2147 wlock.release()
2144 self.removeundo(repo)
2148 self.removeundo(repo)
2145
2149
2146 def init(self, repo, create=False):
2150 def init(self, repo, create=False):
2147 if not create and os.path.isdir(self.path):
2151 if not create and os.path.isdir(self.path):
2148 raise error.Abort(_(b"patch queue directory already exists"))
2152 raise error.Abort(_(b"patch queue directory already exists"))
2149 try:
2153 try:
2150 os.mkdir(self.path)
2154 os.mkdir(self.path)
2151 except OSError as inst:
2155 except OSError as inst:
2152 if inst.errno != errno.EEXIST or not create:
2156 if inst.errno != errno.EEXIST or not create:
2153 raise
2157 raise
2154 if create:
2158 if create:
2155 return self.qrepo(create=True)
2159 return self.qrepo(create=True)
2156
2160
2157 def unapplied(self, repo, patch=None):
2161 def unapplied(self, repo, patch=None):
2158 if patch and patch not in self.series:
2162 if patch and patch not in self.series:
2159 raise error.Abort(_(b"patch %s is not in series file") % patch)
2163 raise error.Abort(_(b"patch %s is not in series file") % patch)
2160 if not patch:
2164 if not patch:
2161 start = self.seriesend()
2165 start = self.seriesend()
2162 else:
2166 else:
2163 start = self.series.index(patch) + 1
2167 start = self.series.index(patch) + 1
2164 unapplied = []
2168 unapplied = []
2165 for i in pycompat.xrange(start, len(self.series)):
2169 for i in pycompat.xrange(start, len(self.series)):
2166 pushable, reason = self.pushable(i)
2170 pushable, reason = self.pushable(i)
2167 if pushable:
2171 if pushable:
2168 unapplied.append((i, self.series[i]))
2172 unapplied.append((i, self.series[i]))
2169 self.explainpushable(i)
2173 self.explainpushable(i)
2170 return unapplied
2174 return unapplied
2171
2175
2172 def qseries(
2176 def qseries(
2173 self,
2177 self,
2174 repo,
2178 repo,
2175 missing=None,
2179 missing=None,
2176 start=0,
2180 start=0,
2177 length=None,
2181 length=None,
2178 status=None,
2182 status=None,
2179 summary=False,
2183 summary=False,
2180 ):
2184 ):
2181 def displayname(pfx, patchname, state):
2185 def displayname(pfx, patchname, state):
2182 if pfx:
2186 if pfx:
2183 self.ui.write(pfx)
2187 self.ui.write(pfx)
2184 if summary:
2188 if summary:
2185 ph = patchheader(self.join(patchname), self.plainmode)
2189 ph = patchheader(self.join(patchname), self.plainmode)
2186 if ph.message:
2190 if ph.message:
2187 msg = ph.message[0]
2191 msg = ph.message[0]
2188 else:
2192 else:
2189 msg = b''
2193 msg = b''
2190
2194
2191 if self.ui.formatted():
2195 if self.ui.formatted():
2192 width = self.ui.termwidth() - len(pfx) - len(patchname) - 2
2196 width = self.ui.termwidth() - len(pfx) - len(patchname) - 2
2193 if width > 0:
2197 if width > 0:
2194 msg = stringutil.ellipsis(msg, width)
2198 msg = stringutil.ellipsis(msg, width)
2195 else:
2199 else:
2196 msg = b''
2200 msg = b''
2197 self.ui.write(patchname, label=b'qseries.' + state)
2201 self.ui.write(patchname, label=b'qseries.' + state)
2198 self.ui.write(b': ')
2202 self.ui.write(b': ')
2199 self.ui.write(msg, label=b'qseries.message.' + state)
2203 self.ui.write(msg, label=b'qseries.message.' + state)
2200 else:
2204 else:
2201 self.ui.write(patchname, label=b'qseries.' + state)
2205 self.ui.write(patchname, label=b'qseries.' + state)
2202 self.ui.write(b'\n')
2206 self.ui.write(b'\n')
2203
2207
2204 applied = {p.name for p in self.applied}
2208 applied = {p.name for p in self.applied}
2205 if length is None:
2209 if length is None:
2206 length = len(self.series) - start
2210 length = len(self.series) - start
2207 if not missing:
2211 if not missing:
2208 if self.ui.verbose:
2212 if self.ui.verbose:
2209 idxwidth = len(b"%d" % (start + length - 1))
2213 idxwidth = len(b"%d" % (start + length - 1))
2210 for i in pycompat.xrange(start, start + length):
2214 for i in pycompat.xrange(start, start + length):
2211 patch = self.series[i]
2215 patch = self.series[i]
2212 if patch in applied:
2216 if patch in applied:
2213 char, state = b'A', b'applied'
2217 char, state = b'A', b'applied'
2214 elif self.pushable(i)[0]:
2218 elif self.pushable(i)[0]:
2215 char, state = b'U', b'unapplied'
2219 char, state = b'U', b'unapplied'
2216 else:
2220 else:
2217 char, state = b'G', b'guarded'
2221 char, state = b'G', b'guarded'
2218 pfx = b''
2222 pfx = b''
2219 if self.ui.verbose:
2223 if self.ui.verbose:
2220 pfx = b'%*d %s ' % (idxwidth, i, char)
2224 pfx = b'%*d %s ' % (idxwidth, i, char)
2221 elif status and status != char:
2225 elif status and status != char:
2222 continue
2226 continue
2223 displayname(pfx, patch, state)
2227 displayname(pfx, patch, state)
2224 else:
2228 else:
2225 msng_list = []
2229 msng_list = []
2226 for root, dirs, files in os.walk(self.path):
2230 for root, dirs, files in os.walk(self.path):
2227 d = root[len(self.path) + 1 :]
2231 d = root[len(self.path) + 1 :]
2228 for f in files:
2232 for f in files:
2229 fl = os.path.join(d, f)
2233 fl = os.path.join(d, f)
2230 if (
2234 if (
2231 fl not in self.series
2235 fl not in self.series
2232 and fl
2236 and fl
2233 not in (
2237 not in (
2234 self.statuspath,
2238 self.statuspath,
2235 self.seriespath,
2239 self.seriespath,
2236 self.guardspath,
2240 self.guardspath,
2237 )
2241 )
2238 and not fl.startswith(b'.')
2242 and not fl.startswith(b'.')
2239 ):
2243 ):
2240 msng_list.append(fl)
2244 msng_list.append(fl)
2241 for x in sorted(msng_list):
2245 for x in sorted(msng_list):
2242 pfx = self.ui.verbose and b'D ' or b''
2246 pfx = self.ui.verbose and b'D ' or b''
2243 displayname(pfx, x, b'missing')
2247 displayname(pfx, x, b'missing')
2244
2248
2245 def issaveline(self, l):
2249 def issaveline(self, l):
2246 if l.name == b'.hg.patches.save.line':
2250 if l.name == b'.hg.patches.save.line':
2247 return True
2251 return True
2248
2252
2249 def qrepo(self, create=False):
2253 def qrepo(self, create=False):
2250 ui = self.baseui.copy()
2254 ui = self.baseui.copy()
2251 # copy back attributes set by ui.pager()
2255 # copy back attributes set by ui.pager()
2252 if self.ui.pageractive and not ui.pageractive:
2256 if self.ui.pageractive and not ui.pageractive:
2253 ui.pageractive = self.ui.pageractive
2257 ui.pageractive = self.ui.pageractive
2254 # internal config: ui.formatted
2258 # internal config: ui.formatted
2255 ui.setconfig(
2259 ui.setconfig(
2256 b'ui',
2260 b'ui',
2257 b'formatted',
2261 b'formatted',
2258 self.ui.config(b'ui', b'formatted'),
2262 self.ui.config(b'ui', b'formatted'),
2259 b'mqpager',
2263 b'mqpager',
2260 )
2264 )
2261 ui.setconfig(
2265 ui.setconfig(
2262 b'ui',
2266 b'ui',
2263 b'interactive',
2267 b'interactive',
2264 self.ui.config(b'ui', b'interactive'),
2268 self.ui.config(b'ui', b'interactive'),
2265 b'mqpager',
2269 b'mqpager',
2266 )
2270 )
2267 if create or os.path.isdir(self.join(b".hg")):
2271 if create or os.path.isdir(self.join(b".hg")):
2268 return hg.repository(ui, path=self.path, create=create)
2272 return hg.repository(ui, path=self.path, create=create)
2269
2273
2270 def restore(self, repo, rev, delete=None, qupdate=None):
2274 def restore(self, repo, rev, delete=None, qupdate=None):
2271 desc = repo[rev].description().strip()
2275 desc = repo[rev].description().strip()
2272 lines = desc.splitlines()
2276 lines = desc.splitlines()
2273 datastart = None
2277 datastart = None
2274 series = []
2278 series = []
2275 applied = []
2279 applied = []
2276 qpp = None
2280 qpp = None
2277 for i, line in enumerate(lines):
2281 for i, line in enumerate(lines):
2278 if line == b'Patch Data:':
2282 if line == b'Patch Data:':
2279 datastart = i + 1
2283 datastart = i + 1
2280 elif line.startswith(b'Dirstate:'):
2284 elif line.startswith(b'Dirstate:'):
2281 l = line.rstrip()
2285 l = line.rstrip()
2282 l = l[10:].split(b' ')
2286 l = l[10:].split(b' ')
2283 qpp = [bin(x) for x in l]
2287 qpp = [bin(x) for x in l]
2284 elif datastart is not None:
2288 elif datastart is not None:
2285 l = line.rstrip()
2289 l = line.rstrip()
2286 n, name = l.split(b':', 1)
2290 n, name = l.split(b':', 1)
2287 if n:
2291 if n:
2288 applied.append(statusentry(bin(n), name))
2292 applied.append(statusentry(bin(n), name))
2289 else:
2293 else:
2290 series.append(l)
2294 series.append(l)
2291 if datastart is None:
2295 if datastart is None:
2292 self.ui.warn(_(b"no saved patch data found\n"))
2296 self.ui.warn(_(b"no saved patch data found\n"))
2293 return 1
2297 return 1
2294 self.ui.warn(_(b"restoring status: %s\n") % lines[0])
2298 self.ui.warn(_(b"restoring status: %s\n") % lines[0])
2295 self.fullseries = series
2299 self.fullseries = series
2296 self.applied = applied
2300 self.applied = applied
2297 self.parseseries()
2301 self.parseseries()
2298 self.seriesdirty = True
2302 self.seriesdirty = True
2299 self.applieddirty = True
2303 self.applieddirty = True
2300 heads = repo.changelog.heads()
2304 heads = repo.changelog.heads()
2301 if delete:
2305 if delete:
2302 if rev not in heads:
2306 if rev not in heads:
2303 self.ui.warn(_(b"save entry has children, leaving it alone\n"))
2307 self.ui.warn(_(b"save entry has children, leaving it alone\n"))
2304 else:
2308 else:
2305 self.ui.warn(_(b"removing save entry %s\n") % short(rev))
2309 self.ui.warn(_(b"removing save entry %s\n") % short(rev))
2306 pp = repo.dirstate.parents()
2310 pp = repo.dirstate.parents()
2307 if rev in pp:
2311 if rev in pp:
2308 update = True
2312 update = True
2309 else:
2313 else:
2310 update = False
2314 update = False
2311 strip(self.ui, repo, [rev], update=update, backup=False)
2315 strip(self.ui, repo, [rev], update=update, backup=False)
2312 if qpp:
2316 if qpp:
2313 self.ui.warn(
2317 self.ui.warn(
2314 _(b"saved queue repository parents: %s %s\n")
2318 _(b"saved queue repository parents: %s %s\n")
2315 % (short(qpp[0]), short(qpp[1]))
2319 % (short(qpp[0]), short(qpp[1]))
2316 )
2320 )
2317 if qupdate:
2321 if qupdate:
2318 self.ui.status(_(b"updating queue directory\n"))
2322 self.ui.status(_(b"updating queue directory\n"))
2319 r = self.qrepo()
2323 r = self.qrepo()
2320 if not r:
2324 if not r:
2321 self.ui.warn(_(b"unable to load queue repository\n"))
2325 self.ui.warn(_(b"unable to load queue repository\n"))
2322 return 1
2326 return 1
2323 hg.clean(r, qpp[0])
2327 hg.clean(r, qpp[0])
2324
2328
2325 def save(self, repo, msg=None):
2329 def save(self, repo, msg=None):
2326 if not self.applied:
2330 if not self.applied:
2327 self.ui.warn(_(b"save: no patches applied, exiting\n"))
2331 self.ui.warn(_(b"save: no patches applied, exiting\n"))
2328 return 1
2332 return 1
2329 if self.issaveline(self.applied[-1]):
2333 if self.issaveline(self.applied[-1]):
2330 self.ui.warn(_(b"status is already saved\n"))
2334 self.ui.warn(_(b"status is already saved\n"))
2331 return 1
2335 return 1
2332
2336
2333 if not msg:
2337 if not msg:
2334 msg = _(b"hg patches saved state")
2338 msg = _(b"hg patches saved state")
2335 else:
2339 else:
2336 msg = b"hg patches: " + msg.rstrip(b'\r\n')
2340 msg = b"hg patches: " + msg.rstrip(b'\r\n')
2337 r = self.qrepo()
2341 r = self.qrepo()
2338 if r:
2342 if r:
2339 pp = r.dirstate.parents()
2343 pp = r.dirstate.parents()
2340 msg += b"\nDirstate: %s %s" % (hex(pp[0]), hex(pp[1]))
2344 msg += b"\nDirstate: %s %s" % (hex(pp[0]), hex(pp[1]))
2341 msg += b"\n\nPatch Data:\n"
2345 msg += b"\n\nPatch Data:\n"
2342 msg += b''.join(b'%s\n' % x for x in self.applied)
2346 msg += b''.join(b'%s\n' % x for x in self.applied)
2343 msg += b''.join(b':%s\n' % x for x in self.fullseries)
2347 msg += b''.join(b':%s\n' % x for x in self.fullseries)
2344 n = repo.commit(msg, force=True)
2348 n = repo.commit(msg, force=True)
2345 if not n:
2349 if not n:
2346 self.ui.warn(_(b"repo commit failed\n"))
2350 self.ui.warn(_(b"repo commit failed\n"))
2347 return 1
2351 return 1
2348 self.applied.append(statusentry(n, b'.hg.patches.save.line'))
2352 self.applied.append(statusentry(n, b'.hg.patches.save.line'))
2349 self.applieddirty = True
2353 self.applieddirty = True
2350 self.removeundo(repo)
2354 self.removeundo(repo)
2351
2355
2352 def fullseriesend(self):
2356 def fullseriesend(self):
2353 if self.applied:
2357 if self.applied:
2354 p = self.applied[-1].name
2358 p = self.applied[-1].name
2355 end = self.findseries(p)
2359 end = self.findseries(p)
2356 if end is None:
2360 if end is None:
2357 return len(self.fullseries)
2361 return len(self.fullseries)
2358 return end + 1
2362 return end + 1
2359 return 0
2363 return 0
2360
2364
2361 def seriesend(self, all_patches=False):
2365 def seriesend(self, all_patches=False):
2362 """If all_patches is False, return the index of the next pushable patch
2366 """If all_patches is False, return the index of the next pushable patch
2363 in the series, or the series length. If all_patches is True, return the
2367 in the series, or the series length. If all_patches is True, return the
2364 index of the first patch past the last applied one.
2368 index of the first patch past the last applied one.
2365 """
2369 """
2366 end = 0
2370 end = 0
2367
2371
2368 def nextpatch(start):
2372 def nextpatch(start):
2369 if all_patches or start >= len(self.series):
2373 if all_patches or start >= len(self.series):
2370 return start
2374 return start
2371 for i in pycompat.xrange(start, len(self.series)):
2375 for i in pycompat.xrange(start, len(self.series)):
2372 p, reason = self.pushable(i)
2376 p, reason = self.pushable(i)
2373 if p:
2377 if p:
2374 return i
2378 return i
2375 self.explainpushable(i)
2379 self.explainpushable(i)
2376 return len(self.series)
2380 return len(self.series)
2377
2381
2378 if self.applied:
2382 if self.applied:
2379 p = self.applied[-1].name
2383 p = self.applied[-1].name
2380 try:
2384 try:
2381 end = self.series.index(p)
2385 end = self.series.index(p)
2382 except ValueError:
2386 except ValueError:
2383 return 0
2387 return 0
2384 return nextpatch(end + 1)
2388 return nextpatch(end + 1)
2385 return nextpatch(end)
2389 return nextpatch(end)
2386
2390
2387 def appliedname(self, index):
2391 def appliedname(self, index):
2388 pname = self.applied[index].name
2392 pname = self.applied[index].name
2389 if not self.ui.verbose:
2393 if not self.ui.verbose:
2390 p = pname
2394 p = pname
2391 else:
2395 else:
2392 p = (b"%d" % self.series.index(pname)) + b" " + pname
2396 p = (b"%d" % self.series.index(pname)) + b" " + pname
2393 return p
2397 return p
2394
2398
2395 def qimport(
2399 def qimport(
2396 self,
2400 self,
2397 repo,
2401 repo,
2398 files,
2402 files,
2399 patchname=None,
2403 patchname=None,
2400 rev=None,
2404 rev=None,
2401 existing=None,
2405 existing=None,
2402 force=None,
2406 force=None,
2403 git=False,
2407 git=False,
2404 ):
2408 ):
2405 def checkseries(patchname):
2409 def checkseries(patchname):
2406 if patchname in self.series:
2410 if patchname in self.series:
2407 raise error.Abort(
2411 raise error.Abort(
2408 _(b'patch %s is already in the series file') % patchname
2412 _(b'patch %s is already in the series file') % patchname
2409 )
2413 )
2410
2414
2411 if rev:
2415 if rev:
2412 if files:
2416 if files:
2413 raise error.Abort(
2417 raise error.Abort(
2414 _(b'option "-r" not valid when importing files')
2418 _(b'option "-r" not valid when importing files')
2415 )
2419 )
2416 rev = scmutil.revrange(repo, rev)
2420 rev = scmutil.revrange(repo, rev)
2417 rev.sort(reverse=True)
2421 rev.sort(reverse=True)
2418 elif not files:
2422 elif not files:
2419 raise error.Abort(_(b'no files or revisions specified'))
2423 raise error.Abort(_(b'no files or revisions specified'))
2420 if (len(files) > 1 or len(rev) > 1) and patchname:
2424 if (len(files) > 1 or len(rev) > 1) and patchname:
2421 raise error.Abort(
2425 raise error.Abort(
2422 _(b'option "-n" not valid when importing multiple patches')
2426 _(b'option "-n" not valid when importing multiple patches')
2423 )
2427 )
2424 imported = []
2428 imported = []
2425 if rev:
2429 if rev:
2426 # If mq patches are applied, we can only import revisions
2430 # If mq patches are applied, we can only import revisions
2427 # that form a linear path to qbase.
2431 # that form a linear path to qbase.
2428 # Otherwise, they should form a linear path to a head.
2432 # Otherwise, they should form a linear path to a head.
2429 heads = repo.changelog.heads(repo.changelog.node(rev.first()))
2433 heads = repo.changelog.heads(repo.changelog.node(rev.first()))
2430 if len(heads) > 1:
2434 if len(heads) > 1:
2431 raise error.Abort(
2435 raise error.Abort(
2432 _(b'revision %d is the root of more than one branch')
2436 _(b'revision %d is the root of more than one branch')
2433 % rev.last()
2437 % rev.last()
2434 )
2438 )
2435 if self.applied:
2439 if self.applied:
2436 base = repo.changelog.node(rev.first())
2440 base = repo.changelog.node(rev.first())
2437 if base in [n.node for n in self.applied]:
2441 if base in [n.node for n in self.applied]:
2438 raise error.Abort(
2442 raise error.Abort(
2439 _(b'revision %d is already managed') % rev.first()
2443 _(b'revision %d is already managed') % rev.first()
2440 )
2444 )
2441 if heads != [self.applied[-1].node]:
2445 if heads != [self.applied[-1].node]:
2442 raise error.Abort(
2446 raise error.Abort(
2443 _(b'revision %d is not the parent of the queue')
2447 _(b'revision %d is not the parent of the queue')
2444 % rev.first()
2448 % rev.first()
2445 )
2449 )
2446 base = repo.changelog.rev(self.applied[0].node)
2450 base = repo.changelog.rev(self.applied[0].node)
2447 lastparent = repo.changelog.parentrevs(base)[0]
2451 lastparent = repo.changelog.parentrevs(base)[0]
2448 else:
2452 else:
2449 if heads != [repo.changelog.node(rev.first())]:
2453 if heads != [repo.changelog.node(rev.first())]:
2450 raise error.Abort(
2454 raise error.Abort(
2451 _(b'revision %d has unmanaged children') % rev.first()
2455 _(b'revision %d has unmanaged children') % rev.first()
2452 )
2456 )
2453 lastparent = None
2457 lastparent = None
2454
2458
2455 diffopts = self.diffopts({b'git': git})
2459 diffopts = self.diffopts({b'git': git})
2456 with repo.transaction(b'qimport') as tr:
2460 with repo.transaction(b'qimport') as tr:
2457 for r in rev:
2461 for r in rev:
2458 if not repo[r].mutable():
2462 if not repo[r].mutable():
2459 raise error.Abort(
2463 raise error.Abort(
2460 _(b'revision %d is not mutable') % r,
2464 _(b'revision %d is not mutable') % r,
2461 hint=_(b"see 'hg help phases' " b'for details'),
2465 hint=_(b"see 'hg help phases' " b'for details'),
2462 )
2466 )
2463 p1, p2 = repo.changelog.parentrevs(r)
2467 p1, p2 = repo.changelog.parentrevs(r)
2464 n = repo.changelog.node(r)
2468 n = repo.changelog.node(r)
2465 if p2 != nullrev:
2469 if p2 != nullrev:
2466 raise error.Abort(
2470 raise error.Abort(
2467 _(b'cannot import merge revision %d') % r
2471 _(b'cannot import merge revision %d') % r
2468 )
2472 )
2469 if lastparent and lastparent != r:
2473 if lastparent and lastparent != r:
2470 raise error.Abort(
2474 raise error.Abort(
2471 _(b'revision %d is not the parent of %d')
2475 _(b'revision %d is not the parent of %d')
2472 % (r, lastparent)
2476 % (r, lastparent)
2473 )
2477 )
2474 lastparent = p1
2478 lastparent = p1
2475
2479
2476 if not patchname:
2480 if not patchname:
2477 patchname = self.makepatchname(
2481 patchname = self.makepatchname(
2478 repo[r].description().split(b'\n', 1)[0],
2482 repo[r].description().split(b'\n', 1)[0],
2479 b'%d.diff' % r,
2483 b'%d.diff' % r,
2480 )
2484 )
2481 checkseries(patchname)
2485 checkseries(patchname)
2482 self.checkpatchname(patchname, force)
2486 self.checkpatchname(patchname, force)
2483 self.fullseries.insert(0, patchname)
2487 self.fullseries.insert(0, patchname)
2484
2488
2485 with self.opener(patchname, b"w") as fp:
2489 with self.opener(patchname, b"w") as fp:
2486 cmdutil.exportfile(repo, [n], fp, opts=diffopts)
2490 cmdutil.exportfile(repo, [n], fp, opts=diffopts)
2487
2491
2488 se = statusentry(n, patchname)
2492 se = statusentry(n, patchname)
2489 self.applied.insert(0, se)
2493 self.applied.insert(0, se)
2490
2494
2491 self.added.append(patchname)
2495 self.added.append(patchname)
2492 imported.append(patchname)
2496 imported.append(patchname)
2493 patchname = None
2497 patchname = None
2494 if rev and repo.ui.configbool(b'mq', b'secret'):
2498 if rev and repo.ui.configbool(b'mq', b'secret'):
2495 # if we added anything with --rev, move the secret root
2499 # if we added anything with --rev, move the secret root
2496 phases.retractboundary(repo, tr, phases.secret, [n])
2500 phases.retractboundary(repo, tr, phases.secret, [n])
2497 self.parseseries()
2501 self.parseseries()
2498 self.applieddirty = True
2502 self.applieddirty = True
2499 self.seriesdirty = True
2503 self.seriesdirty = True
2500
2504
2501 for i, filename in enumerate(files):
2505 for i, filename in enumerate(files):
2502 if existing:
2506 if existing:
2503 if filename == b'-':
2507 if filename == b'-':
2504 raise error.Abort(
2508 raise error.Abort(
2505 _(b'-e is incompatible with import from -')
2509 _(b'-e is incompatible with import from -')
2506 )
2510 )
2507 filename = normname(filename)
2511 filename = normname(filename)
2508 self.checkreservedname(filename)
2512 self.checkreservedname(filename)
2509 if urlutil.url(filename).islocal():
2513 if urlutil.url(filename).islocal():
2510 originpath = self.join(filename)
2514 originpath = self.join(filename)
2511 if not os.path.isfile(originpath):
2515 if not os.path.isfile(originpath):
2512 raise error.Abort(
2516 raise error.Abort(
2513 _(b"patch %s does not exist") % filename
2517 _(b"patch %s does not exist") % filename
2514 )
2518 )
2515
2519
2516 if patchname:
2520 if patchname:
2517 self.checkpatchname(patchname, force)
2521 self.checkpatchname(patchname, force)
2518
2522
2519 self.ui.write(
2523 self.ui.write(
2520 _(b'renaming %s to %s\n') % (filename, patchname)
2524 _(b'renaming %s to %s\n') % (filename, patchname)
2521 )
2525 )
2522 util.rename(originpath, self.join(patchname))
2526 util.rename(originpath, self.join(patchname))
2523 else:
2527 else:
2524 patchname = filename
2528 patchname = filename
2525
2529
2526 else:
2530 else:
2527 if filename == b'-' and not patchname:
2531 if filename == b'-' and not patchname:
2528 raise error.Abort(
2532 raise error.Abort(
2529 _(b'need --name to import a patch from -')
2533 _(b'need --name to import a patch from -')
2530 )
2534 )
2531 elif not patchname:
2535 elif not patchname:
2532 patchname = normname(
2536 patchname = normname(
2533 os.path.basename(filename.rstrip(b'/'))
2537 os.path.basename(filename.rstrip(b'/'))
2534 )
2538 )
2535 self.checkpatchname(patchname, force)
2539 self.checkpatchname(patchname, force)
2536 try:
2540 try:
2537 if filename == b'-':
2541 if filename == b'-':
2538 text = self.ui.fin.read()
2542 text = self.ui.fin.read()
2539 else:
2543 else:
2540 fp = hg.openpath(self.ui, filename)
2544 fp = hg.openpath(self.ui, filename)
2541 text = fp.read()
2545 text = fp.read()
2542 fp.close()
2546 fp.close()
2543 except (OSError, IOError):
2547 except (OSError, IOError):
2544 raise error.Abort(_(b"unable to read file %s") % filename)
2548 raise error.Abort(_(b"unable to read file %s") % filename)
2545 patchf = self.opener(patchname, b"w")
2549 patchf = self.opener(patchname, b"w")
2546 patchf.write(text)
2550 patchf.write(text)
2547 patchf.close()
2551 patchf.close()
2548 if not force:
2552 if not force:
2549 checkseries(patchname)
2553 checkseries(patchname)
2550 if patchname not in self.series:
2554 if patchname not in self.series:
2551 index = self.fullseriesend() + i
2555 index = self.fullseriesend() + i
2552 self.fullseries[index:index] = [patchname]
2556 self.fullseries[index:index] = [patchname]
2553 self.parseseries()
2557 self.parseseries()
2554 self.seriesdirty = True
2558 self.seriesdirty = True
2555 self.ui.warn(_(b"adding %s to series file\n") % patchname)
2559 self.ui.warn(_(b"adding %s to series file\n") % patchname)
2556 self.added.append(patchname)
2560 self.added.append(patchname)
2557 imported.append(patchname)
2561 imported.append(patchname)
2558 patchname = None
2562 patchname = None
2559
2563
2560 self.removeundo(repo)
2564 self.removeundo(repo)
2561 return imported
2565 return imported
2562
2566
2563
2567
2564 def fixkeepchangesopts(ui, opts):
2568 def fixkeepchangesopts(ui, opts):
2565 if (
2569 if (
2566 not ui.configbool(b'mq', b'keepchanges')
2570 not ui.configbool(b'mq', b'keepchanges')
2567 or opts.get(b'force')
2571 or opts.get(b'force')
2568 or opts.get(b'exact')
2572 or opts.get(b'exact')
2569 ):
2573 ):
2570 return opts
2574 return opts
2571 opts = dict(opts)
2575 opts = dict(opts)
2572 opts[b'keep_changes'] = True
2576 opts[b'keep_changes'] = True
2573 return opts
2577 return opts
2574
2578
2575
2579
2576 @command(
2580 @command(
2577 b"qdelete|qremove|qrm",
2581 b"qdelete|qremove|qrm",
2578 [
2582 [
2579 (b'k', b'keep', None, _(b'keep patch file')),
2583 (b'k', b'keep', None, _(b'keep patch file')),
2580 (
2584 (
2581 b'r',
2585 b'r',
2582 b'rev',
2586 b'rev',
2583 [],
2587 [],
2584 _(b'stop managing a revision (DEPRECATED)'),
2588 _(b'stop managing a revision (DEPRECATED)'),
2585 _(b'REV'),
2589 _(b'REV'),
2586 ),
2590 ),
2587 ],
2591 ],
2588 _(b'hg qdelete [-k] [PATCH]...'),
2592 _(b'hg qdelete [-k] [PATCH]...'),
2589 helpcategory=command.CATEGORY_CHANGE_ORGANIZATION,
2593 helpcategory=command.CATEGORY_CHANGE_ORGANIZATION,
2590 )
2594 )
2591 def delete(ui, repo, *patches, **opts):
2595 def delete(ui, repo, *patches, **opts):
2592 """remove patches from queue
2596 """remove patches from queue
2593
2597
2594 The patches must not be applied, and at least one patch is required. Exact
2598 The patches must not be applied, and at least one patch is required. Exact
2595 patch identifiers must be given. With -k/--keep, the patch files are
2599 patch identifiers must be given. With -k/--keep, the patch files are
2596 preserved in the patch directory.
2600 preserved in the patch directory.
2597
2601
2598 To stop managing a patch and move it into permanent history,
2602 To stop managing a patch and move it into permanent history,
2599 use the :hg:`qfinish` command."""
2603 use the :hg:`qfinish` command."""
2600 q = repo.mq
2604 q = repo.mq
2601 q.delete(repo, patches, pycompat.byteskwargs(opts))
2605 q.delete(repo, patches, pycompat.byteskwargs(opts))
2602 q.savedirty()
2606 q.savedirty()
2603 return 0
2607 return 0
2604
2608
2605
2609
2606 @command(
2610 @command(
2607 b"qapplied",
2611 b"qapplied",
2608 [(b'1', b'last', None, _(b'show only the preceding applied patch'))]
2612 [(b'1', b'last', None, _(b'show only the preceding applied patch'))]
2609 + seriesopts,
2613 + seriesopts,
2610 _(b'hg qapplied [-1] [-s] [PATCH]'),
2614 _(b'hg qapplied [-1] [-s] [PATCH]'),
2611 helpcategory=command.CATEGORY_CHANGE_ORGANIZATION,
2615 helpcategory=command.CATEGORY_CHANGE_ORGANIZATION,
2612 )
2616 )
2613 def applied(ui, repo, patch=None, **opts):
2617 def applied(ui, repo, patch=None, **opts):
2614 """print the patches already applied
2618 """print the patches already applied
2615
2619
2616 Returns 0 on success."""
2620 Returns 0 on success."""
2617
2621
2618 q = repo.mq
2622 q = repo.mq
2619 opts = pycompat.byteskwargs(opts)
2623 opts = pycompat.byteskwargs(opts)
2620
2624
2621 if patch:
2625 if patch:
2622 if patch not in q.series:
2626 if patch not in q.series:
2623 raise error.Abort(_(b"patch %s is not in series file") % patch)
2627 raise error.Abort(_(b"patch %s is not in series file") % patch)
2624 end = q.series.index(patch) + 1
2628 end = q.series.index(patch) + 1
2625 else:
2629 else:
2626 end = q.seriesend(True)
2630 end = q.seriesend(True)
2627
2631
2628 if opts.get(b'last') and not end:
2632 if opts.get(b'last') and not end:
2629 ui.write(_(b"no patches applied\n"))
2633 ui.write(_(b"no patches applied\n"))
2630 return 1
2634 return 1
2631 elif opts.get(b'last') and end == 1:
2635 elif opts.get(b'last') and end == 1:
2632 ui.write(_(b"only one patch applied\n"))
2636 ui.write(_(b"only one patch applied\n"))
2633 return 1
2637 return 1
2634 elif opts.get(b'last'):
2638 elif opts.get(b'last'):
2635 start = end - 2
2639 start = end - 2
2636 end = 1
2640 end = 1
2637 else:
2641 else:
2638 start = 0
2642 start = 0
2639
2643
2640 q.qseries(
2644 q.qseries(
2641 repo, length=end, start=start, status=b'A', summary=opts.get(b'summary')
2645 repo, length=end, start=start, status=b'A', summary=opts.get(b'summary')
2642 )
2646 )
2643
2647
2644
2648
2645 @command(
2649 @command(
2646 b"qunapplied",
2650 b"qunapplied",
2647 [(b'1', b'first', None, _(b'show only the first patch'))] + seriesopts,
2651 [(b'1', b'first', None, _(b'show only the first patch'))] + seriesopts,
2648 _(b'hg qunapplied [-1] [-s] [PATCH]'),
2652 _(b'hg qunapplied [-1] [-s] [PATCH]'),
2649 helpcategory=command.CATEGORY_CHANGE_ORGANIZATION,
2653 helpcategory=command.CATEGORY_CHANGE_ORGANIZATION,
2650 )
2654 )
2651 def unapplied(ui, repo, patch=None, **opts):
2655 def unapplied(ui, repo, patch=None, **opts):
2652 """print the patches not yet applied
2656 """print the patches not yet applied
2653
2657
2654 Returns 0 on success."""
2658 Returns 0 on success."""
2655
2659
2656 q = repo.mq
2660 q = repo.mq
2657 opts = pycompat.byteskwargs(opts)
2661 opts = pycompat.byteskwargs(opts)
2658 if patch:
2662 if patch:
2659 if patch not in q.series:
2663 if patch not in q.series:
2660 raise error.Abort(_(b"patch %s is not in series file") % patch)
2664 raise error.Abort(_(b"patch %s is not in series file") % patch)
2661 start = q.series.index(patch) + 1
2665 start = q.series.index(patch) + 1
2662 else:
2666 else:
2663 start = q.seriesend(True)
2667 start = q.seriesend(True)
2664
2668
2665 if start == len(q.series) and opts.get(b'first'):
2669 if start == len(q.series) and opts.get(b'first'):
2666 ui.write(_(b"all patches applied\n"))
2670 ui.write(_(b"all patches applied\n"))
2667 return 1
2671 return 1
2668
2672
2669 if opts.get(b'first'):
2673 if opts.get(b'first'):
2670 length = 1
2674 length = 1
2671 else:
2675 else:
2672 length = None
2676 length = None
2673 q.qseries(
2677 q.qseries(
2674 repo,
2678 repo,
2675 start=start,
2679 start=start,
2676 length=length,
2680 length=length,
2677 status=b'U',
2681 status=b'U',
2678 summary=opts.get(b'summary'),
2682 summary=opts.get(b'summary'),
2679 )
2683 )
2680
2684
2681
2685
2682 @command(
2686 @command(
2683 b"qimport",
2687 b"qimport",
2684 [
2688 [
2685 (b'e', b'existing', None, _(b'import file in patch directory')),
2689 (b'e', b'existing', None, _(b'import file in patch directory')),
2686 (b'n', b'name', b'', _(b'name of patch file'), _(b'NAME')),
2690 (b'n', b'name', b'', _(b'name of patch file'), _(b'NAME')),
2687 (b'f', b'force', None, _(b'overwrite existing files')),
2691 (b'f', b'force', None, _(b'overwrite existing files')),
2688 (
2692 (
2689 b'r',
2693 b'r',
2690 b'rev',
2694 b'rev',
2691 [],
2695 [],
2692 _(b'place existing revisions under mq control'),
2696 _(b'place existing revisions under mq control'),
2693 _(b'REV'),
2697 _(b'REV'),
2694 ),
2698 ),
2695 (b'g', b'git', None, _(b'use git extended diff format')),
2699 (b'g', b'git', None, _(b'use git extended diff format')),
2696 (b'P', b'push', None, _(b'qpush after importing')),
2700 (b'P', b'push', None, _(b'qpush after importing')),
2697 ],
2701 ],
2698 _(b'hg qimport [-e] [-n NAME] [-f] [-g] [-P] [-r REV]... [FILE]...'),
2702 _(b'hg qimport [-e] [-n NAME] [-f] [-g] [-P] [-r REV]... [FILE]...'),
2699 helpcategory=command.CATEGORY_IMPORT_EXPORT,
2703 helpcategory=command.CATEGORY_IMPORT_EXPORT,
2700 )
2704 )
2701 def qimport(ui, repo, *filename, **opts):
2705 def qimport(ui, repo, *filename, **opts):
2702 """import a patch or existing changeset
2706 """import a patch or existing changeset
2703
2707
2704 The patch is inserted into the series after the last applied
2708 The patch is inserted into the series after the last applied
2705 patch. If no patches have been applied, qimport prepends the patch
2709 patch. If no patches have been applied, qimport prepends the patch
2706 to the series.
2710 to the series.
2707
2711
2708 The patch will have the same name as its source file unless you
2712 The patch will have the same name as its source file unless you
2709 give it a new one with -n/--name.
2713 give it a new one with -n/--name.
2710
2714
2711 You can register an existing patch inside the patch directory with
2715 You can register an existing patch inside the patch directory with
2712 the -e/--existing flag.
2716 the -e/--existing flag.
2713
2717
2714 With -f/--force, an existing patch of the same name will be
2718 With -f/--force, an existing patch of the same name will be
2715 overwritten.
2719 overwritten.
2716
2720
2717 An existing changeset may be placed under mq control with -r/--rev
2721 An existing changeset may be placed under mq control with -r/--rev
2718 (e.g. qimport --rev . -n patch will place the current revision
2722 (e.g. qimport --rev . -n patch will place the current revision
2719 under mq control). With -g/--git, patches imported with --rev will
2723 under mq control). With -g/--git, patches imported with --rev will
2720 use the git diff format. See the diffs help topic for information
2724 use the git diff format. See the diffs help topic for information
2721 on why this is important for preserving rename/copy information
2725 on why this is important for preserving rename/copy information
2722 and permission changes. Use :hg:`qfinish` to remove changesets
2726 and permission changes. Use :hg:`qfinish` to remove changesets
2723 from mq control.
2727 from mq control.
2724
2728
2725 To import a patch from standard input, pass - as the patch file.
2729 To import a patch from standard input, pass - as the patch file.
2726 When importing from standard input, a patch name must be specified
2730 When importing from standard input, a patch name must be specified
2727 using the --name flag.
2731 using the --name flag.
2728
2732
2729 To import an existing patch while renaming it::
2733 To import an existing patch while renaming it::
2730
2734
2731 hg qimport -e existing-patch -n new-name
2735 hg qimport -e existing-patch -n new-name
2732
2736
2733 Returns 0 if import succeeded.
2737 Returns 0 if import succeeded.
2734 """
2738 """
2735 opts = pycompat.byteskwargs(opts)
2739 opts = pycompat.byteskwargs(opts)
2736 with repo.lock(): # cause this may move phase
2740 with repo.lock(): # cause this may move phase
2737 q = repo.mq
2741 q = repo.mq
2738 try:
2742 try:
2739 imported = q.qimport(
2743 imported = q.qimport(
2740 repo,
2744 repo,
2741 filename,
2745 filename,
2742 patchname=opts.get(b'name'),
2746 patchname=opts.get(b'name'),
2743 existing=opts.get(b'existing'),
2747 existing=opts.get(b'existing'),
2744 force=opts.get(b'force'),
2748 force=opts.get(b'force'),
2745 rev=opts.get(b'rev'),
2749 rev=opts.get(b'rev'),
2746 git=opts.get(b'git'),
2750 git=opts.get(b'git'),
2747 )
2751 )
2748 finally:
2752 finally:
2749 q.savedirty()
2753 q.savedirty()
2750
2754
2751 if imported and opts.get(b'push') and not opts.get(b'rev'):
2755 if imported and opts.get(b'push') and not opts.get(b'rev'):
2752 return q.push(repo, imported[-1])
2756 return q.push(repo, imported[-1])
2753 return 0
2757 return 0
2754
2758
2755
2759
2756 def qinit(ui, repo, create):
2760 def qinit(ui, repo, create):
2757 """initialize a new queue repository
2761 """initialize a new queue repository
2758
2762
2759 This command also creates a series file for ordering patches, and
2763 This command also creates a series file for ordering patches, and
2760 an mq-specific .hgignore file in the queue repository, to exclude
2764 an mq-specific .hgignore file in the queue repository, to exclude
2761 the status and guards files (these contain mostly transient state).
2765 the status and guards files (these contain mostly transient state).
2762
2766
2763 Returns 0 if initialization succeeded."""
2767 Returns 0 if initialization succeeded."""
2764 q = repo.mq
2768 q = repo.mq
2765 r = q.init(repo, create)
2769 r = q.init(repo, create)
2766 q.savedirty()
2770 q.savedirty()
2767 if r:
2771 if r:
2768 if not os.path.exists(r.wjoin(b'.hgignore')):
2772 if not os.path.exists(r.wjoin(b'.hgignore')):
2769 fp = r.wvfs(b'.hgignore', b'w')
2773 fp = r.wvfs(b'.hgignore', b'w')
2770 fp.write(b'^\\.hg\n')
2774 fp.write(b'^\\.hg\n')
2771 fp.write(b'^\\.mq\n')
2775 fp.write(b'^\\.mq\n')
2772 fp.write(b'syntax: glob\n')
2776 fp.write(b'syntax: glob\n')
2773 fp.write(b'status\n')
2777 fp.write(b'status\n')
2774 fp.write(b'guards\n')
2778 fp.write(b'guards\n')
2775 fp.close()
2779 fp.close()
2776 if not os.path.exists(r.wjoin(b'series')):
2780 if not os.path.exists(r.wjoin(b'series')):
2777 r.wvfs(b'series', b'w').close()
2781 r.wvfs(b'series', b'w').close()
2778 r[None].add([b'.hgignore', b'series'])
2782 r[None].add([b'.hgignore', b'series'])
2779 commands.add(ui, r)
2783 commands.add(ui, r)
2780 return 0
2784 return 0
2781
2785
2782
2786
2783 @command(
2787 @command(
2784 b"qinit",
2788 b"qinit",
2785 [(b'c', b'create-repo', None, _(b'create queue repository'))],
2789 [(b'c', b'create-repo', None, _(b'create queue repository'))],
2786 _(b'hg qinit [-c]'),
2790 _(b'hg qinit [-c]'),
2787 helpcategory=command.CATEGORY_REPO_CREATION,
2791 helpcategory=command.CATEGORY_REPO_CREATION,
2788 helpbasic=True,
2792 helpbasic=True,
2789 )
2793 )
2790 def init(ui, repo, **opts):
2794 def init(ui, repo, **opts):
2791 """init a new queue repository (DEPRECATED)
2795 """init a new queue repository (DEPRECATED)
2792
2796
2793 The queue repository is unversioned by default. If
2797 The queue repository is unversioned by default. If
2794 -c/--create-repo is specified, qinit will create a separate nested
2798 -c/--create-repo is specified, qinit will create a separate nested
2795 repository for patches (qinit -c may also be run later to convert
2799 repository for patches (qinit -c may also be run later to convert
2796 an unversioned patch repository into a versioned one). You can use
2800 an unversioned patch repository into a versioned one). You can use
2797 qcommit to commit changes to this queue repository.
2801 qcommit to commit changes to this queue repository.
2798
2802
2799 This command is deprecated. Without -c, it's implied by other relevant
2803 This command is deprecated. Without -c, it's implied by other relevant
2800 commands. With -c, use :hg:`init --mq` instead."""
2804 commands. With -c, use :hg:`init --mq` instead."""
2801 return qinit(ui, repo, create=opts.get('create_repo'))
2805 return qinit(ui, repo, create=opts.get('create_repo'))
2802
2806
2803
2807
2804 @command(
2808 @command(
2805 b"qclone",
2809 b"qclone",
2806 [
2810 [
2807 (b'', b'pull', None, _(b'use pull protocol to copy metadata')),
2811 (b'', b'pull', None, _(b'use pull protocol to copy metadata')),
2808 (
2812 (
2809 b'U',
2813 b'U',
2810 b'noupdate',
2814 b'noupdate',
2811 None,
2815 None,
2812 _(b'do not update the new working directories'),
2816 _(b'do not update the new working directories'),
2813 ),
2817 ),
2814 (
2818 (
2815 b'',
2819 b'',
2816 b'uncompressed',
2820 b'uncompressed',
2817 None,
2821 None,
2818 _(b'use uncompressed transfer (fast over LAN)'),
2822 _(b'use uncompressed transfer (fast over LAN)'),
2819 ),
2823 ),
2820 (
2824 (
2821 b'p',
2825 b'p',
2822 b'patches',
2826 b'patches',
2823 b'',
2827 b'',
2824 _(b'location of source patch repository'),
2828 _(b'location of source patch repository'),
2825 _(b'REPO'),
2829 _(b'REPO'),
2826 ),
2830 ),
2827 ]
2831 ]
2828 + cmdutil.remoteopts,
2832 + cmdutil.remoteopts,
2829 _(b'hg qclone [OPTION]... SOURCE [DEST]'),
2833 _(b'hg qclone [OPTION]... SOURCE [DEST]'),
2830 helpcategory=command.CATEGORY_REPO_CREATION,
2834 helpcategory=command.CATEGORY_REPO_CREATION,
2831 norepo=True,
2835 norepo=True,
2832 )
2836 )
2833 def clone(ui, source, dest=None, **opts):
2837 def clone(ui, source, dest=None, **opts):
2834 """clone main and patch repository at same time
2838 """clone main and patch repository at same time
2835
2839
2836 If source is local, destination will have no patches applied. If
2840 If source is local, destination will have no patches applied. If
2837 source is remote, this command can not check if patches are
2841 source is remote, this command can not check if patches are
2838 applied in source, so cannot guarantee that patches are not
2842 applied in source, so cannot guarantee that patches are not
2839 applied in destination. If you clone remote repository, be sure
2843 applied in destination. If you clone remote repository, be sure
2840 before that it has no patches applied.
2844 before that it has no patches applied.
2841
2845
2842 Source patch repository is looked for in <src>/.hg/patches by
2846 Source patch repository is looked for in <src>/.hg/patches by
2843 default. Use -p <url> to change.
2847 default. Use -p <url> to change.
2844
2848
2845 The patch directory must be a nested Mercurial repository, as
2849 The patch directory must be a nested Mercurial repository, as
2846 would be created by :hg:`init --mq`.
2850 would be created by :hg:`init --mq`.
2847
2851
2848 Return 0 on success.
2852 Return 0 on success.
2849 """
2853 """
2850 opts = pycompat.byteskwargs(opts)
2854 opts = pycompat.byteskwargs(opts)
2851
2855
2852 def patchdir(repo):
2856 def patchdir(repo):
2853 """compute a patch repo url from a repo object"""
2857 """compute a patch repo url from a repo object"""
2854 url = repo.url()
2858 url = repo.url()
2855 if url.endswith(b'/'):
2859 if url.endswith(b'/'):
2856 url = url[:-1]
2860 url = url[:-1]
2857 return url + b'/.hg/patches'
2861 return url + b'/.hg/patches'
2858
2862
2859 # main repo (destination and sources)
2863 # main repo (destination and sources)
2860 if dest is None:
2864 if dest is None:
2861 dest = hg.defaultdest(source)
2865 dest = hg.defaultdest(source)
2862 __, source_path, __ = urlutil.get_clone_path(ui, source)
2866 __, source_path, __ = urlutil.get_clone_path(ui, source)
2863 sr = hg.peer(ui, opts, source_path)
2867 sr = hg.peer(ui, opts, source_path)
2864
2868
2865 # patches repo (source only)
2869 # patches repo (source only)
2866 if opts.get(b'patches'):
2870 if opts.get(b'patches'):
2867 __, patchespath, __ = urlutil.get_clone_path(ui, opts.get(b'patches'))
2871 __, patchespath, __ = urlutil.get_clone_path(ui, opts.get(b'patches'))
2868 else:
2872 else:
2869 patchespath = patchdir(sr)
2873 patchespath = patchdir(sr)
2870 try:
2874 try:
2871 hg.peer(ui, opts, patchespath)
2875 hg.peer(ui, opts, patchespath)
2872 except error.RepoError:
2876 except error.RepoError:
2873 raise error.Abort(
2877 raise error.Abort(
2874 _(b'versioned patch repository not found (see init --mq)')
2878 _(b'versioned patch repository not found (see init --mq)')
2875 )
2879 )
2876 qbase, destrev = None, None
2880 qbase, destrev = None, None
2877 if sr.local():
2881 if sr.local():
2878 repo = sr.local()
2882 repo = sr.local()
2879 if repo.mq.applied and repo[qbase].phase() != phases.secret:
2883 if repo.mq.applied and repo[qbase].phase() != phases.secret:
2880 qbase = repo.mq.applied[0].node
2884 qbase = repo.mq.applied[0].node
2881 if not hg.islocal(dest):
2885 if not hg.islocal(dest):
2882 heads = set(repo.heads())
2886 heads = set(repo.heads())
2883 destrev = list(heads.difference(repo.heads(qbase)))
2887 destrev = list(heads.difference(repo.heads(qbase)))
2884 destrev.append(repo.changelog.parents(qbase)[0])
2888 destrev.append(repo.changelog.parents(qbase)[0])
2885 elif sr.capable(b'lookup'):
2889 elif sr.capable(b'lookup'):
2886 try:
2890 try:
2887 qbase = sr.lookup(b'qbase')
2891 qbase = sr.lookup(b'qbase')
2888 except error.RepoError:
2892 except error.RepoError:
2889 pass
2893 pass
2890
2894
2891 ui.note(_(b'cloning main repository\n'))
2895 ui.note(_(b'cloning main repository\n'))
2892 sr, dr = hg.clone(
2896 sr, dr = hg.clone(
2893 ui,
2897 ui,
2894 opts,
2898 opts,
2895 sr.url(),
2899 sr.url(),
2896 dest,
2900 dest,
2897 pull=opts.get(b'pull'),
2901 pull=opts.get(b'pull'),
2898 revs=destrev,
2902 revs=destrev,
2899 update=False,
2903 update=False,
2900 stream=opts.get(b'uncompressed'),
2904 stream=opts.get(b'uncompressed'),
2901 )
2905 )
2902
2906
2903 ui.note(_(b'cloning patch repository\n'))
2907 ui.note(_(b'cloning patch repository\n'))
2904 hg.clone(
2908 hg.clone(
2905 ui,
2909 ui,
2906 opts,
2910 opts,
2907 opts.get(b'patches') or patchdir(sr),
2911 opts.get(b'patches') or patchdir(sr),
2908 patchdir(dr),
2912 patchdir(dr),
2909 pull=opts.get(b'pull'),
2913 pull=opts.get(b'pull'),
2910 update=not opts.get(b'noupdate'),
2914 update=not opts.get(b'noupdate'),
2911 stream=opts.get(b'uncompressed'),
2915 stream=opts.get(b'uncompressed'),
2912 )
2916 )
2913
2917
2914 if dr.local():
2918 if dr.local():
2915 repo = dr.local()
2919 repo = dr.local()
2916 if qbase:
2920 if qbase:
2917 ui.note(
2921 ui.note(
2918 _(
2922 _(
2919 b'stripping applied patches from destination '
2923 b'stripping applied patches from destination '
2920 b'repository\n'
2924 b'repository\n'
2921 )
2925 )
2922 )
2926 )
2923 strip(ui, repo, [qbase], update=False, backup=None)
2927 strip(ui, repo, [qbase], update=False, backup=None)
2924 if not opts.get(b'noupdate'):
2928 if not opts.get(b'noupdate'):
2925 ui.note(_(b'updating destination repository\n'))
2929 ui.note(_(b'updating destination repository\n'))
2926 hg.update(repo, repo.changelog.tip())
2930 hg.update(repo, repo.changelog.tip())
2927
2931
2928
2932
2929 @command(
2933 @command(
2930 b"qcommit|qci",
2934 b"qcommit|qci",
2931 commands.table[b"commit|ci"][1],
2935 commands.table[b"commit|ci"][1],
2932 _(b'hg qcommit [OPTION]... [FILE]...'),
2936 _(b'hg qcommit [OPTION]... [FILE]...'),
2933 helpcategory=command.CATEGORY_COMMITTING,
2937 helpcategory=command.CATEGORY_COMMITTING,
2934 inferrepo=True,
2938 inferrepo=True,
2935 )
2939 )
2936 def commit(ui, repo, *pats, **opts):
2940 def commit(ui, repo, *pats, **opts):
2937 """commit changes in the queue repository (DEPRECATED)
2941 """commit changes in the queue repository (DEPRECATED)
2938
2942
2939 This command is deprecated; use :hg:`commit --mq` instead."""
2943 This command is deprecated; use :hg:`commit --mq` instead."""
2940 q = repo.mq
2944 q = repo.mq
2941 r = q.qrepo()
2945 r = q.qrepo()
2942 if not r:
2946 if not r:
2943 raise error.Abort(b'no queue repository')
2947 raise error.Abort(b'no queue repository')
2944 commands.commit(r.ui, r, *pats, **opts)
2948 commands.commit(r.ui, r, *pats, **opts)
2945
2949
2946
2950
2947 @command(
2951 @command(
2948 b"qseries",
2952 b"qseries",
2949 [
2953 [
2950 (b'm', b'missing', None, _(b'print patches not in series')),
2954 (b'm', b'missing', None, _(b'print patches not in series')),
2951 ]
2955 ]
2952 + seriesopts,
2956 + seriesopts,
2953 _(b'hg qseries [-ms]'),
2957 _(b'hg qseries [-ms]'),
2954 helpcategory=command.CATEGORY_CHANGE_ORGANIZATION,
2958 helpcategory=command.CATEGORY_CHANGE_ORGANIZATION,
2955 )
2959 )
2956 def series(ui, repo, **opts):
2960 def series(ui, repo, **opts):
2957 """print the entire series file
2961 """print the entire series file
2958
2962
2959 Returns 0 on success."""
2963 Returns 0 on success."""
2960 repo.mq.qseries(
2964 repo.mq.qseries(
2961 repo, missing=opts.get('missing'), summary=opts.get('summary')
2965 repo, missing=opts.get('missing'), summary=opts.get('summary')
2962 )
2966 )
2963 return 0
2967 return 0
2964
2968
2965
2969
2966 @command(
2970 @command(
2967 b"qtop",
2971 b"qtop",
2968 seriesopts,
2972 seriesopts,
2969 _(b'hg qtop [-s]'),
2973 _(b'hg qtop [-s]'),
2970 helpcategory=command.CATEGORY_CHANGE_ORGANIZATION,
2974 helpcategory=command.CATEGORY_CHANGE_ORGANIZATION,
2971 )
2975 )
2972 def top(ui, repo, **opts):
2976 def top(ui, repo, **opts):
2973 """print the name of the current patch
2977 """print the name of the current patch
2974
2978
2975 Returns 0 on success."""
2979 Returns 0 on success."""
2976 q = repo.mq
2980 q = repo.mq
2977 if q.applied:
2981 if q.applied:
2978 t = q.seriesend(True)
2982 t = q.seriesend(True)
2979 else:
2983 else:
2980 t = 0
2984 t = 0
2981
2985
2982 if t:
2986 if t:
2983 q.qseries(
2987 q.qseries(
2984 repo,
2988 repo,
2985 start=t - 1,
2989 start=t - 1,
2986 length=1,
2990 length=1,
2987 status=b'A',
2991 status=b'A',
2988 summary=opts.get('summary'),
2992 summary=opts.get('summary'),
2989 )
2993 )
2990 else:
2994 else:
2991 ui.write(_(b"no patches applied\n"))
2995 ui.write(_(b"no patches applied\n"))
2992 return 1
2996 return 1
2993
2997
2994
2998
2995 @command(
2999 @command(
2996 b"qnext",
3000 b"qnext",
2997 seriesopts,
3001 seriesopts,
2998 _(b'hg qnext [-s]'),
3002 _(b'hg qnext [-s]'),
2999 helpcategory=command.CATEGORY_CHANGE_ORGANIZATION,
3003 helpcategory=command.CATEGORY_CHANGE_ORGANIZATION,
3000 )
3004 )
3001 def next(ui, repo, **opts):
3005 def next(ui, repo, **opts):
3002 """print the name of the next pushable patch
3006 """print the name of the next pushable patch
3003
3007
3004 Returns 0 on success."""
3008 Returns 0 on success."""
3005 q = repo.mq
3009 q = repo.mq
3006 end = q.seriesend()
3010 end = q.seriesend()
3007 if end == len(q.series):
3011 if end == len(q.series):
3008 ui.write(_(b"all patches applied\n"))
3012 ui.write(_(b"all patches applied\n"))
3009 return 1
3013 return 1
3010 q.qseries(repo, start=end, length=1, summary=opts.get('summary'))
3014 q.qseries(repo, start=end, length=1, summary=opts.get('summary'))
3011
3015
3012
3016
3013 @command(
3017 @command(
3014 b"qprev",
3018 b"qprev",
3015 seriesopts,
3019 seriesopts,
3016 _(b'hg qprev [-s]'),
3020 _(b'hg qprev [-s]'),
3017 helpcategory=command.CATEGORY_CHANGE_ORGANIZATION,
3021 helpcategory=command.CATEGORY_CHANGE_ORGANIZATION,
3018 )
3022 )
3019 def prev(ui, repo, **opts):
3023 def prev(ui, repo, **opts):
3020 """print the name of the preceding applied patch
3024 """print the name of the preceding applied patch
3021
3025
3022 Returns 0 on success."""
3026 Returns 0 on success."""
3023 q = repo.mq
3027 q = repo.mq
3024 l = len(q.applied)
3028 l = len(q.applied)
3025 if l == 1:
3029 if l == 1:
3026 ui.write(_(b"only one patch applied\n"))
3030 ui.write(_(b"only one patch applied\n"))
3027 return 1
3031 return 1
3028 if not l:
3032 if not l:
3029 ui.write(_(b"no patches applied\n"))
3033 ui.write(_(b"no patches applied\n"))
3030 return 1
3034 return 1
3031 idx = q.series.index(q.applied[-2].name)
3035 idx = q.series.index(q.applied[-2].name)
3032 q.qseries(
3036 q.qseries(
3033 repo, start=idx, length=1, status=b'A', summary=opts.get('summary')
3037 repo, start=idx, length=1, status=b'A', summary=opts.get('summary')
3034 )
3038 )
3035
3039
3036
3040
3037 def setupheaderopts(ui, opts):
3041 def setupheaderopts(ui, opts):
3038 if not opts.get(b'user') and opts.get(b'currentuser'):
3042 if not opts.get(b'user') and opts.get(b'currentuser'):
3039 opts[b'user'] = ui.username()
3043 opts[b'user'] = ui.username()
3040 if not opts.get(b'date') and opts.get(b'currentdate'):
3044 if not opts.get(b'date') and opts.get(b'currentdate'):
3041 opts[b'date'] = b"%d %d" % dateutil.makedate()
3045 opts[b'date'] = b"%d %d" % dateutil.makedate()
3042
3046
3043
3047
3044 @command(
3048 @command(
3045 b"qnew",
3049 b"qnew",
3046 [
3050 [
3047 (b'e', b'edit', None, _(b'invoke editor on commit messages')),
3051 (b'e', b'edit', None, _(b'invoke editor on commit messages')),
3048 (b'f', b'force', None, _(b'import uncommitted changes (DEPRECATED)')),
3052 (b'f', b'force', None, _(b'import uncommitted changes (DEPRECATED)')),
3049 (b'g', b'git', None, _(b'use git extended diff format')),
3053 (b'g', b'git', None, _(b'use git extended diff format')),
3050 (b'U', b'currentuser', None, _(b'add "From: <current user>" to patch')),
3054 (b'U', b'currentuser', None, _(b'add "From: <current user>" to patch')),
3051 (b'u', b'user', b'', _(b'add "From: <USER>" to patch'), _(b'USER')),
3055 (b'u', b'user', b'', _(b'add "From: <USER>" to patch'), _(b'USER')),
3052 (b'D', b'currentdate', None, _(b'add "Date: <current date>" to patch')),
3056 (b'D', b'currentdate', None, _(b'add "Date: <current date>" to patch')),
3053 (b'd', b'date', b'', _(b'add "Date: <DATE>" to patch'), _(b'DATE')),
3057 (b'd', b'date', b'', _(b'add "Date: <DATE>" to patch'), _(b'DATE')),
3054 ]
3058 ]
3055 + cmdutil.walkopts
3059 + cmdutil.walkopts
3056 + cmdutil.commitopts,
3060 + cmdutil.commitopts,
3057 _(b'hg qnew [-e] [-m TEXT] [-l FILE] PATCH [FILE]...'),
3061 _(b'hg qnew [-e] [-m TEXT] [-l FILE] PATCH [FILE]...'),
3058 helpcategory=command.CATEGORY_COMMITTING,
3062 helpcategory=command.CATEGORY_COMMITTING,
3059 helpbasic=True,
3063 helpbasic=True,
3060 inferrepo=True,
3064 inferrepo=True,
3061 )
3065 )
3062 def new(ui, repo, patch, *args, **opts):
3066 def new(ui, repo, patch, *args, **opts):
3063 """create a new patch
3067 """create a new patch
3064
3068
3065 qnew creates a new patch on top of the currently-applied patch (if
3069 qnew creates a new patch on top of the currently-applied patch (if
3066 any). The patch will be initialized with any outstanding changes
3070 any). The patch will be initialized with any outstanding changes
3067 in the working directory. You may also use -I/--include,
3071 in the working directory. You may also use -I/--include,
3068 -X/--exclude, and/or a list of files after the patch name to add
3072 -X/--exclude, and/or a list of files after the patch name to add
3069 only changes to matching files to the new patch, leaving the rest
3073 only changes to matching files to the new patch, leaving the rest
3070 as uncommitted modifications.
3074 as uncommitted modifications.
3071
3075
3072 -u/--user and -d/--date can be used to set the (given) user and
3076 -u/--user and -d/--date can be used to set the (given) user and
3073 date, respectively. -U/--currentuser and -D/--currentdate set user
3077 date, respectively. -U/--currentuser and -D/--currentdate set user
3074 to current user and date to current date.
3078 to current user and date to current date.
3075
3079
3076 -e/--edit, -m/--message or -l/--logfile set the patch header as
3080 -e/--edit, -m/--message or -l/--logfile set the patch header as
3077 well as the commit message. If none is specified, the header is
3081 well as the commit message. If none is specified, the header is
3078 empty and the commit message is '[mq]: PATCH'.
3082 empty and the commit message is '[mq]: PATCH'.
3079
3083
3080 Use the -g/--git option to keep the patch in the git extended diff
3084 Use the -g/--git option to keep the patch in the git extended diff
3081 format. Read the diffs help topic for more information on why this
3085 format. Read the diffs help topic for more information on why this
3082 is important for preserving permission changes and copy/rename
3086 is important for preserving permission changes and copy/rename
3083 information.
3087 information.
3084
3088
3085 Returns 0 on successful creation of a new patch.
3089 Returns 0 on successful creation of a new patch.
3086 """
3090 """
3087 opts = pycompat.byteskwargs(opts)
3091 opts = pycompat.byteskwargs(opts)
3088 msg = cmdutil.logmessage(ui, opts)
3092 msg = cmdutil.logmessage(ui, opts)
3089 q = repo.mq
3093 q = repo.mq
3090 opts[b'msg'] = msg
3094 opts[b'msg'] = msg
3091 setupheaderopts(ui, opts)
3095 setupheaderopts(ui, opts)
3092 q.new(repo, patch, *args, **pycompat.strkwargs(opts))
3096 q.new(repo, patch, *args, **pycompat.strkwargs(opts))
3093 q.savedirty()
3097 q.savedirty()
3094 return 0
3098 return 0
3095
3099
3096
3100
3097 @command(
3101 @command(
3098 b"qrefresh",
3102 b"qrefresh",
3099 [
3103 [
3100 (b'e', b'edit', None, _(b'invoke editor on commit messages')),
3104 (b'e', b'edit', None, _(b'invoke editor on commit messages')),
3101 (b'g', b'git', None, _(b'use git extended diff format')),
3105 (b'g', b'git', None, _(b'use git extended diff format')),
3102 (
3106 (
3103 b's',
3107 b's',
3104 b'short',
3108 b'short',
3105 None,
3109 None,
3106 _(b'refresh only files already in the patch and specified files'),
3110 _(b'refresh only files already in the patch and specified files'),
3107 ),
3111 ),
3108 (
3112 (
3109 b'U',
3113 b'U',
3110 b'currentuser',
3114 b'currentuser',
3111 None,
3115 None,
3112 _(b'add/update author field in patch with current user'),
3116 _(b'add/update author field in patch with current user'),
3113 ),
3117 ),
3114 (
3118 (
3115 b'u',
3119 b'u',
3116 b'user',
3120 b'user',
3117 b'',
3121 b'',
3118 _(b'add/update author field in patch with given user'),
3122 _(b'add/update author field in patch with given user'),
3119 _(b'USER'),
3123 _(b'USER'),
3120 ),
3124 ),
3121 (
3125 (
3122 b'D',
3126 b'D',
3123 b'currentdate',
3127 b'currentdate',
3124 None,
3128 None,
3125 _(b'add/update date field in patch with current date'),
3129 _(b'add/update date field in patch with current date'),
3126 ),
3130 ),
3127 (
3131 (
3128 b'd',
3132 b'd',
3129 b'date',
3133 b'date',
3130 b'',
3134 b'',
3131 _(b'add/update date field in patch with given date'),
3135 _(b'add/update date field in patch with given date'),
3132 _(b'DATE'),
3136 _(b'DATE'),
3133 ),
3137 ),
3134 ]
3138 ]
3135 + cmdutil.walkopts
3139 + cmdutil.walkopts
3136 + cmdutil.commitopts,
3140 + cmdutil.commitopts,
3137 _(b'hg qrefresh [-I] [-X] [-e] [-m TEXT] [-l FILE] [-s] [FILE]...'),
3141 _(b'hg qrefresh [-I] [-X] [-e] [-m TEXT] [-l FILE] [-s] [FILE]...'),
3138 helpcategory=command.CATEGORY_COMMITTING,
3142 helpcategory=command.CATEGORY_COMMITTING,
3139 helpbasic=True,
3143 helpbasic=True,
3140 inferrepo=True,
3144 inferrepo=True,
3141 )
3145 )
3142 def refresh(ui, repo, *pats, **opts):
3146 def refresh(ui, repo, *pats, **opts):
3143 """update the current patch
3147 """update the current patch
3144
3148
3145 If any file patterns are provided, the refreshed patch will
3149 If any file patterns are provided, the refreshed patch will
3146 contain only the modifications that match those patterns; the
3150 contain only the modifications that match those patterns; the
3147 remaining modifications will remain in the working directory.
3151 remaining modifications will remain in the working directory.
3148
3152
3149 If -s/--short is specified, files currently included in the patch
3153 If -s/--short is specified, files currently included in the patch
3150 will be refreshed just like matched files and remain in the patch.
3154 will be refreshed just like matched files and remain in the patch.
3151
3155
3152 If -e/--edit is specified, Mercurial will start your configured editor for
3156 If -e/--edit is specified, Mercurial will start your configured editor for
3153 you to enter a message. In case qrefresh fails, you will find a backup of
3157 you to enter a message. In case qrefresh fails, you will find a backup of
3154 your message in ``.hg/last-message.txt``.
3158 your message in ``.hg/last-message.txt``.
3155
3159
3156 hg add/remove/copy/rename work as usual, though you might want to
3160 hg add/remove/copy/rename work as usual, though you might want to
3157 use git-style patches (-g/--git or [diff] git=1) to track copies
3161 use git-style patches (-g/--git or [diff] git=1) to track copies
3158 and renames. See the diffs help topic for more information on the
3162 and renames. See the diffs help topic for more information on the
3159 git diff format.
3163 git diff format.
3160
3164
3161 Returns 0 on success.
3165 Returns 0 on success.
3162 """
3166 """
3163 opts = pycompat.byteskwargs(opts)
3167 opts = pycompat.byteskwargs(opts)
3164 q = repo.mq
3168 q = repo.mq
3165 message = cmdutil.logmessage(ui, opts)
3169 message = cmdutil.logmessage(ui, opts)
3166 setupheaderopts(ui, opts)
3170 setupheaderopts(ui, opts)
3167 with repo.wlock():
3171 with repo.wlock():
3168 ret = q.refresh(repo, pats, msg=message, **pycompat.strkwargs(opts))
3172 ret = q.refresh(repo, pats, msg=message, **pycompat.strkwargs(opts))
3169 q.savedirty()
3173 q.savedirty()
3170 return ret
3174 return ret
3171
3175
3172
3176
3173 @command(
3177 @command(
3174 b"qdiff",
3178 b"qdiff",
3175 cmdutil.diffopts + cmdutil.diffopts2 + cmdutil.walkopts,
3179 cmdutil.diffopts + cmdutil.diffopts2 + cmdutil.walkopts,
3176 _(b'hg qdiff [OPTION]... [FILE]...'),
3180 _(b'hg qdiff [OPTION]... [FILE]...'),
3177 helpcategory=command.CATEGORY_FILE_CONTENTS,
3181 helpcategory=command.CATEGORY_FILE_CONTENTS,
3178 helpbasic=True,
3182 helpbasic=True,
3179 inferrepo=True,
3183 inferrepo=True,
3180 )
3184 )
3181 def diff(ui, repo, *pats, **opts):
3185 def diff(ui, repo, *pats, **opts):
3182 """diff of the current patch and subsequent modifications
3186 """diff of the current patch and subsequent modifications
3183
3187
3184 Shows a diff which includes the current patch as well as any
3188 Shows a diff which includes the current patch as well as any
3185 changes which have been made in the working directory since the
3189 changes which have been made in the working directory since the
3186 last refresh (thus showing what the current patch would become
3190 last refresh (thus showing what the current patch would become
3187 after a qrefresh).
3191 after a qrefresh).
3188
3192
3189 Use :hg:`diff` if you only want to see the changes made since the
3193 Use :hg:`diff` if you only want to see the changes made since the
3190 last qrefresh, or :hg:`export qtip` if you want to see changes
3194 last qrefresh, or :hg:`export qtip` if you want to see changes
3191 made by the current patch without including changes made since the
3195 made by the current patch without including changes made since the
3192 qrefresh.
3196 qrefresh.
3193
3197
3194 Returns 0 on success.
3198 Returns 0 on success.
3195 """
3199 """
3196 ui.pager(b'qdiff')
3200 ui.pager(b'qdiff')
3197 repo.mq.diff(repo, pats, pycompat.byteskwargs(opts))
3201 repo.mq.diff(repo, pats, pycompat.byteskwargs(opts))
3198 return 0
3202 return 0
3199
3203
3200
3204
3201 @command(
3205 @command(
3202 b'qfold',
3206 b'qfold',
3203 [
3207 [
3204 (b'e', b'edit', None, _(b'invoke editor on commit messages')),
3208 (b'e', b'edit', None, _(b'invoke editor on commit messages')),
3205 (b'k', b'keep', None, _(b'keep folded patch files')),
3209 (b'k', b'keep', None, _(b'keep folded patch files')),
3206 ]
3210 ]
3207 + cmdutil.commitopts,
3211 + cmdutil.commitopts,
3208 _(b'hg qfold [-e] [-k] [-m TEXT] [-l FILE] PATCH...'),
3212 _(b'hg qfold [-e] [-k] [-m TEXT] [-l FILE] PATCH...'),
3209 helpcategory=command.CATEGORY_CHANGE_MANAGEMENT,
3213 helpcategory=command.CATEGORY_CHANGE_MANAGEMENT,
3210 )
3214 )
3211 def fold(ui, repo, *files, **opts):
3215 def fold(ui, repo, *files, **opts):
3212 """fold the named patches into the current patch
3216 """fold the named patches into the current patch
3213
3217
3214 Patches must not yet be applied. Each patch will be successively
3218 Patches must not yet be applied. Each patch will be successively
3215 applied to the current patch in the order given. If all the
3219 applied to the current patch in the order given. If all the
3216 patches apply successfully, the current patch will be refreshed
3220 patches apply successfully, the current patch will be refreshed
3217 with the new cumulative patch, and the folded patches will be
3221 with the new cumulative patch, and the folded patches will be
3218 deleted. With -k/--keep, the folded patch files will not be
3222 deleted. With -k/--keep, the folded patch files will not be
3219 removed afterwards.
3223 removed afterwards.
3220
3224
3221 The header for each folded patch will be concatenated with the
3225 The header for each folded patch will be concatenated with the
3222 current patch header, separated by a line of ``* * *``.
3226 current patch header, separated by a line of ``* * *``.
3223
3227
3224 Returns 0 on success."""
3228 Returns 0 on success."""
3225 opts = pycompat.byteskwargs(opts)
3229 opts = pycompat.byteskwargs(opts)
3226 q = repo.mq
3230 q = repo.mq
3227 if not files:
3231 if not files:
3228 raise error.Abort(_(b'qfold requires at least one patch name'))
3232 raise error.Abort(_(b'qfold requires at least one patch name'))
3229 if not q.checktoppatch(repo)[0]:
3233 if not q.checktoppatch(repo)[0]:
3230 raise error.Abort(_(b'no patches applied'))
3234 raise error.Abort(_(b'no patches applied'))
3231 q.checklocalchanges(repo)
3235 q.checklocalchanges(repo)
3232
3236
3233 message = cmdutil.logmessage(ui, opts)
3237 message = cmdutil.logmessage(ui, opts)
3234
3238
3235 parent = q.lookup(b'qtip')
3239 parent = q.lookup(b'qtip')
3236 patches = []
3240 patches = []
3237 messages = []
3241 messages = []
3238 for f in files:
3242 for f in files:
3239 p = q.lookup(f)
3243 p = q.lookup(f)
3240 if p in patches or p == parent:
3244 if p in patches or p == parent:
3241 ui.warn(_(b'skipping already folded patch %s\n') % p)
3245 ui.warn(_(b'skipping already folded patch %s\n') % p)
3242 if q.isapplied(p):
3246 if q.isapplied(p):
3243 raise error.Abort(
3247 raise error.Abort(
3244 _(b'qfold cannot fold already applied patch %s') % p
3248 _(b'qfold cannot fold already applied patch %s') % p
3245 )
3249 )
3246 patches.append(p)
3250 patches.append(p)
3247
3251
3248 for p in patches:
3252 for p in patches:
3249 if not message:
3253 if not message:
3250 ph = patchheader(q.join(p), q.plainmode)
3254 ph = patchheader(q.join(p), q.plainmode)
3251 if ph.message:
3255 if ph.message:
3252 messages.append(ph.message)
3256 messages.append(ph.message)
3253 pf = q.join(p)
3257 pf = q.join(p)
3254 (patchsuccess, files, fuzz) = q.patch(repo, pf)
3258 (patchsuccess, files, fuzz) = q.patch(repo, pf)
3255 if not patchsuccess:
3259 if not patchsuccess:
3256 raise error.Abort(_(b'error folding patch %s') % p)
3260 raise error.Abort(_(b'error folding patch %s') % p)
3257
3261
3258 if not message:
3262 if not message:
3259 ph = patchheader(q.join(parent), q.plainmode)
3263 ph = patchheader(q.join(parent), q.plainmode)
3260 message = ph.message
3264 message = ph.message
3261 for msg in messages:
3265 for msg in messages:
3262 if msg:
3266 if msg:
3263 if message:
3267 if message:
3264 message.append(b'* * *')
3268 message.append(b'* * *')
3265 message.extend(msg)
3269 message.extend(msg)
3266 message = b'\n'.join(message)
3270 message = b'\n'.join(message)
3267
3271
3268 diffopts = q.patchopts(q.diffopts(), *patches)
3272 diffopts = q.patchopts(q.diffopts(), *patches)
3269 with repo.wlock():
3273 with repo.wlock():
3270 q.refresh(
3274 q.refresh(
3271 repo,
3275 repo,
3272 msg=message,
3276 msg=message,
3273 git=diffopts.git,
3277 git=diffopts.git,
3274 edit=opts.get(b'edit'),
3278 edit=opts.get(b'edit'),
3275 editform=b'mq.qfold',
3279 editform=b'mq.qfold',
3276 )
3280 )
3277 q.delete(repo, patches, opts)
3281 q.delete(repo, patches, opts)
3278 q.savedirty()
3282 q.savedirty()
3279
3283
3280
3284
3281 @command(
3285 @command(
3282 b"qgoto",
3286 b"qgoto",
3283 [
3287 [
3284 (
3288 (
3285 b'',
3289 b'',
3286 b'keep-changes',
3290 b'keep-changes',
3287 None,
3291 None,
3288 _(b'tolerate non-conflicting local changes'),
3292 _(b'tolerate non-conflicting local changes'),
3289 ),
3293 ),
3290 (b'f', b'force', None, _(b'overwrite any local changes')),
3294 (b'f', b'force', None, _(b'overwrite any local changes')),
3291 (b'', b'no-backup', None, _(b'do not save backup copies of files')),
3295 (b'', b'no-backup', None, _(b'do not save backup copies of files')),
3292 ],
3296 ],
3293 _(b'hg qgoto [OPTION]... PATCH'),
3297 _(b'hg qgoto [OPTION]... PATCH'),
3294 helpcategory=command.CATEGORY_CHANGE_ORGANIZATION,
3298 helpcategory=command.CATEGORY_CHANGE_ORGANIZATION,
3295 )
3299 )
3296 def goto(ui, repo, patch, **opts):
3300 def goto(ui, repo, patch, **opts):
3297 """push or pop patches until named patch is at top of stack
3301 """push or pop patches until named patch is at top of stack
3298
3302
3299 Returns 0 on success."""
3303 Returns 0 on success."""
3300 opts = pycompat.byteskwargs(opts)
3304 opts = pycompat.byteskwargs(opts)
3301 opts = fixkeepchangesopts(ui, opts)
3305 opts = fixkeepchangesopts(ui, opts)
3302 q = repo.mq
3306 q = repo.mq
3303 patch = q.lookup(patch)
3307 patch = q.lookup(patch)
3304 nobackup = opts.get(b'no_backup')
3308 nobackup = opts.get(b'no_backup')
3305 keepchanges = opts.get(b'keep_changes')
3309 keepchanges = opts.get(b'keep_changes')
3306 if q.isapplied(patch):
3310 if q.isapplied(patch):
3307 ret = q.pop(
3311 ret = q.pop(
3308 repo,
3312 repo,
3309 patch,
3313 patch,
3310 force=opts.get(b'force'),
3314 force=opts.get(b'force'),
3311 nobackup=nobackup,
3315 nobackup=nobackup,
3312 keepchanges=keepchanges,
3316 keepchanges=keepchanges,
3313 )
3317 )
3314 else:
3318 else:
3315 ret = q.push(
3319 ret = q.push(
3316 repo,
3320 repo,
3317 patch,
3321 patch,
3318 force=opts.get(b'force'),
3322 force=opts.get(b'force'),
3319 nobackup=nobackup,
3323 nobackup=nobackup,
3320 keepchanges=keepchanges,
3324 keepchanges=keepchanges,
3321 )
3325 )
3322 q.savedirty()
3326 q.savedirty()
3323 return ret
3327 return ret
3324
3328
3325
3329
3326 @command(
3330 @command(
3327 b"qguard",
3331 b"qguard",
3328 [
3332 [
3329 (b'l', b'list', None, _(b'list all patches and guards')),
3333 (b'l', b'list', None, _(b'list all patches and guards')),
3330 (b'n', b'none', None, _(b'drop all guards')),
3334 (b'n', b'none', None, _(b'drop all guards')),
3331 ],
3335 ],
3332 _(b'hg qguard [-l] [-n] [PATCH] [-- [+GUARD]... [-GUARD]...]'),
3336 _(b'hg qguard [-l] [-n] [PATCH] [-- [+GUARD]... [-GUARD]...]'),
3333 helpcategory=command.CATEGORY_CHANGE_ORGANIZATION,
3337 helpcategory=command.CATEGORY_CHANGE_ORGANIZATION,
3334 )
3338 )
3335 def guard(ui, repo, *args, **opts):
3339 def guard(ui, repo, *args, **opts):
3336 """set or print guards for a patch
3340 """set or print guards for a patch
3337
3341
3338 Guards control whether a patch can be pushed. A patch with no
3342 Guards control whether a patch can be pushed. A patch with no
3339 guards is always pushed. A patch with a positive guard ("+foo") is
3343 guards is always pushed. A patch with a positive guard ("+foo") is
3340 pushed only if the :hg:`qselect` command has activated it. A patch with
3344 pushed only if the :hg:`qselect` command has activated it. A patch with
3341 a negative guard ("-foo") is never pushed if the :hg:`qselect` command
3345 a negative guard ("-foo") is never pushed if the :hg:`qselect` command
3342 has activated it.
3346 has activated it.
3343
3347
3344 With no arguments, print the currently active guards.
3348 With no arguments, print the currently active guards.
3345 With arguments, set guards for the named patch.
3349 With arguments, set guards for the named patch.
3346
3350
3347 .. note::
3351 .. note::
3348
3352
3349 Specifying negative guards now requires '--'.
3353 Specifying negative guards now requires '--'.
3350
3354
3351 To set guards on another patch::
3355 To set guards on another patch::
3352
3356
3353 hg qguard other.patch -- +2.6.17 -stable
3357 hg qguard other.patch -- +2.6.17 -stable
3354
3358
3355 Returns 0 on success.
3359 Returns 0 on success.
3356 """
3360 """
3357
3361
3358 def status(idx):
3362 def status(idx):
3359 guards = q.seriesguards[idx] or [b'unguarded']
3363 guards = q.seriesguards[idx] or [b'unguarded']
3360 if q.series[idx] in applied:
3364 if q.series[idx] in applied:
3361 state = b'applied'
3365 state = b'applied'
3362 elif q.pushable(idx)[0]:
3366 elif q.pushable(idx)[0]:
3363 state = b'unapplied'
3367 state = b'unapplied'
3364 else:
3368 else:
3365 state = b'guarded'
3369 state = b'guarded'
3366 label = b'qguard.patch qguard.%s qseries.%s' % (state, state)
3370 label = b'qguard.patch qguard.%s qseries.%s' % (state, state)
3367 ui.write(b'%s: ' % ui.label(q.series[idx], label))
3371 ui.write(b'%s: ' % ui.label(q.series[idx], label))
3368
3372
3369 for i, guard in enumerate(guards):
3373 for i, guard in enumerate(guards):
3370 if guard.startswith(b'+'):
3374 if guard.startswith(b'+'):
3371 ui.write(guard, label=b'qguard.positive')
3375 ui.write(guard, label=b'qguard.positive')
3372 elif guard.startswith(b'-'):
3376 elif guard.startswith(b'-'):
3373 ui.write(guard, label=b'qguard.negative')
3377 ui.write(guard, label=b'qguard.negative')
3374 else:
3378 else:
3375 ui.write(guard, label=b'qguard.unguarded')
3379 ui.write(guard, label=b'qguard.unguarded')
3376 if i != len(guards) - 1:
3380 if i != len(guards) - 1:
3377 ui.write(b' ')
3381 ui.write(b' ')
3378 ui.write(b'\n')
3382 ui.write(b'\n')
3379
3383
3380 q = repo.mq
3384 q = repo.mq
3381 applied = {p.name for p in q.applied}
3385 applied = {p.name for p in q.applied}
3382 patch = None
3386 patch = None
3383 args = list(args)
3387 args = list(args)
3384 if opts.get('list'):
3388 if opts.get('list'):
3385 if args or opts.get('none'):
3389 if args or opts.get('none'):
3386 raise error.Abort(
3390 raise error.Abort(
3387 _(b'cannot mix -l/--list with options or arguments')
3391 _(b'cannot mix -l/--list with options or arguments')
3388 )
3392 )
3389 for i in pycompat.xrange(len(q.series)):
3393 for i in pycompat.xrange(len(q.series)):
3390 status(i)
3394 status(i)
3391 return
3395 return
3392 if not args or args[0][0:1] in b'-+':
3396 if not args or args[0][0:1] in b'-+':
3393 if not q.applied:
3397 if not q.applied:
3394 raise error.Abort(_(b'no patches applied'))
3398 raise error.Abort(_(b'no patches applied'))
3395 patch = q.applied[-1].name
3399 patch = q.applied[-1].name
3396 if patch is None and args[0][0:1] not in b'-+':
3400 if patch is None and args[0][0:1] not in b'-+':
3397 patch = args.pop(0)
3401 patch = args.pop(0)
3398 if patch is None:
3402 if patch is None:
3399 raise error.Abort(_(b'no patch to work with'))
3403 raise error.Abort(_(b'no patch to work with'))
3400 if args or opts.get('none'):
3404 if args or opts.get('none'):
3401 idx = q.findseries(patch)
3405 idx = q.findseries(patch)
3402 if idx is None:
3406 if idx is None:
3403 raise error.Abort(_(b'no patch named %s') % patch)
3407 raise error.Abort(_(b'no patch named %s') % patch)
3404 q.setguards(idx, args)
3408 q.setguards(idx, args)
3405 q.savedirty()
3409 q.savedirty()
3406 else:
3410 else:
3407 status(q.series.index(q.lookup(patch)))
3411 status(q.series.index(q.lookup(patch)))
3408
3412
3409
3413
3410 @command(
3414 @command(
3411 b"qheader",
3415 b"qheader",
3412 [],
3416 [],
3413 _(b'hg qheader [PATCH]'),
3417 _(b'hg qheader [PATCH]'),
3414 helpcategory=command.CATEGORY_CHANGE_ORGANIZATION,
3418 helpcategory=command.CATEGORY_CHANGE_ORGANIZATION,
3415 )
3419 )
3416 def header(ui, repo, patch=None):
3420 def header(ui, repo, patch=None):
3417 """print the header of the topmost or specified patch
3421 """print the header of the topmost or specified patch
3418
3422
3419 Returns 0 on success."""
3423 Returns 0 on success."""
3420 q = repo.mq
3424 q = repo.mq
3421
3425
3422 if patch:
3426 if patch:
3423 patch = q.lookup(patch)
3427 patch = q.lookup(patch)
3424 else:
3428 else:
3425 if not q.applied:
3429 if not q.applied:
3426 ui.write(_(b'no patches applied\n'))
3430 ui.write(_(b'no patches applied\n'))
3427 return 1
3431 return 1
3428 patch = q.lookup(b'qtip')
3432 patch = q.lookup(b'qtip')
3429 ph = patchheader(q.join(patch), q.plainmode)
3433 ph = patchheader(q.join(patch), q.plainmode)
3430
3434
3431 ui.write(b'\n'.join(ph.message) + b'\n')
3435 ui.write(b'\n'.join(ph.message) + b'\n')
3432
3436
3433
3437
3434 def lastsavename(path):
3438 def lastsavename(path):
3435 (directory, base) = os.path.split(path)
3439 (directory, base) = os.path.split(path)
3436 names = os.listdir(directory)
3440 names = os.listdir(directory)
3437 namere = re.compile(b"%s.([0-9]+)" % base)
3441 namere = re.compile(b"%s.([0-9]+)" % base)
3438 maxindex = None
3442 maxindex = None
3439 maxname = None
3443 maxname = None
3440 for f in names:
3444 for f in names:
3441 m = namere.match(f)
3445 m = namere.match(f)
3442 if m:
3446 if m:
3443 index = int(m.group(1))
3447 index = int(m.group(1))
3444 if maxindex is None or index > maxindex:
3448 if maxindex is None or index > maxindex:
3445 maxindex = index
3449 maxindex = index
3446 maxname = f
3450 maxname = f
3447 if maxname:
3451 if maxname:
3448 return (os.path.join(directory, maxname), maxindex)
3452 return (os.path.join(directory, maxname), maxindex)
3449 return (None, None)
3453 return (None, None)
3450
3454
3451
3455
3452 def savename(path):
3456 def savename(path):
3453 (last, index) = lastsavename(path)
3457 (last, index) = lastsavename(path)
3454 if last is None:
3458 if last is None:
3455 index = 0
3459 index = 0
3456 newpath = path + b".%d" % (index + 1)
3460 newpath = path + b".%d" % (index + 1)
3457 return newpath
3461 return newpath
3458
3462
3459
3463
3460 @command(
3464 @command(
3461 b"qpush",
3465 b"qpush",
3462 [
3466 [
3463 (
3467 (
3464 b'',
3468 b'',
3465 b'keep-changes',
3469 b'keep-changes',
3466 None,
3470 None,
3467 _(b'tolerate non-conflicting local changes'),
3471 _(b'tolerate non-conflicting local changes'),
3468 ),
3472 ),
3469 (b'f', b'force', None, _(b'apply on top of local changes')),
3473 (b'f', b'force', None, _(b'apply on top of local changes')),
3470 (
3474 (
3471 b'e',
3475 b'e',
3472 b'exact',
3476 b'exact',
3473 None,
3477 None,
3474 _(b'apply the target patch to its recorded parent'),
3478 _(b'apply the target patch to its recorded parent'),
3475 ),
3479 ),
3476 (b'l', b'list', None, _(b'list patch name in commit text')),
3480 (b'l', b'list', None, _(b'list patch name in commit text')),
3477 (b'a', b'all', None, _(b'apply all patches')),
3481 (b'a', b'all', None, _(b'apply all patches')),
3478 (b'm', b'merge', None, _(b'merge from another queue (DEPRECATED)')),
3482 (b'm', b'merge', None, _(b'merge from another queue (DEPRECATED)')),
3479 (b'n', b'name', b'', _(b'merge queue name (DEPRECATED)'), _(b'NAME')),
3483 (b'n', b'name', b'', _(b'merge queue name (DEPRECATED)'), _(b'NAME')),
3480 (
3484 (
3481 b'',
3485 b'',
3482 b'move',
3486 b'move',
3483 None,
3487 None,
3484 _(b'reorder patch series and apply only the patch'),
3488 _(b'reorder patch series and apply only the patch'),
3485 ),
3489 ),
3486 (b'', b'no-backup', None, _(b'do not save backup copies of files')),
3490 (b'', b'no-backup', None, _(b'do not save backup copies of files')),
3487 ],
3491 ],
3488 _(b'hg qpush [-f] [-l] [-a] [--move] [PATCH | INDEX]'),
3492 _(b'hg qpush [-f] [-l] [-a] [--move] [PATCH | INDEX]'),
3489 helpcategory=command.CATEGORY_CHANGE_ORGANIZATION,
3493 helpcategory=command.CATEGORY_CHANGE_ORGANIZATION,
3490 helpbasic=True,
3494 helpbasic=True,
3491 )
3495 )
3492 def push(ui, repo, patch=None, **opts):
3496 def push(ui, repo, patch=None, **opts):
3493 """push the next patch onto the stack
3497 """push the next patch onto the stack
3494
3498
3495 By default, abort if the working directory contains uncommitted
3499 By default, abort if the working directory contains uncommitted
3496 changes. With --keep-changes, abort only if the uncommitted files
3500 changes. With --keep-changes, abort only if the uncommitted files
3497 overlap with patched files. With -f/--force, backup and patch over
3501 overlap with patched files. With -f/--force, backup and patch over
3498 uncommitted changes.
3502 uncommitted changes.
3499
3503
3500 Return 0 on success.
3504 Return 0 on success.
3501 """
3505 """
3502 q = repo.mq
3506 q = repo.mq
3503 mergeq = None
3507 mergeq = None
3504
3508
3505 opts = pycompat.byteskwargs(opts)
3509 opts = pycompat.byteskwargs(opts)
3506 opts = fixkeepchangesopts(ui, opts)
3510 opts = fixkeepchangesopts(ui, opts)
3507 if opts.get(b'merge'):
3511 if opts.get(b'merge'):
3508 if opts.get(b'name'):
3512 if opts.get(b'name'):
3509 newpath = repo.vfs.join(opts.get(b'name'))
3513 newpath = repo.vfs.join(opts.get(b'name'))
3510 else:
3514 else:
3511 newpath, i = lastsavename(q.path)
3515 newpath, i = lastsavename(q.path)
3512 if not newpath:
3516 if not newpath:
3513 ui.warn(_(b"no saved queues found, please use -n\n"))
3517 ui.warn(_(b"no saved queues found, please use -n\n"))
3514 return 1
3518 return 1
3515 mergeq = queue(ui, repo.baseui, repo.path, newpath)
3519 mergeq = queue(ui, repo.baseui, repo.path, newpath)
3516 ui.warn(_(b"merging with queue at: %s\n") % mergeq.path)
3520 ui.warn(_(b"merging with queue at: %s\n") % mergeq.path)
3517 ret = q.push(
3521 ret = q.push(
3518 repo,
3522 repo,
3519 patch,
3523 patch,
3520 force=opts.get(b'force'),
3524 force=opts.get(b'force'),
3521 list=opts.get(b'list'),
3525 list=opts.get(b'list'),
3522 mergeq=mergeq,
3526 mergeq=mergeq,
3523 all=opts.get(b'all'),
3527 all=opts.get(b'all'),
3524 move=opts.get(b'move'),
3528 move=opts.get(b'move'),
3525 exact=opts.get(b'exact'),
3529 exact=opts.get(b'exact'),
3526 nobackup=opts.get(b'no_backup'),
3530 nobackup=opts.get(b'no_backup'),
3527 keepchanges=opts.get(b'keep_changes'),
3531 keepchanges=opts.get(b'keep_changes'),
3528 )
3532 )
3529 return ret
3533 return ret
3530
3534
3531
3535
3532 @command(
3536 @command(
3533 b"qpop",
3537 b"qpop",
3534 [
3538 [
3535 (b'a', b'all', None, _(b'pop all patches')),
3539 (b'a', b'all', None, _(b'pop all patches')),
3536 (b'n', b'name', b'', _(b'queue name to pop (DEPRECATED)'), _(b'NAME')),
3540 (b'n', b'name', b'', _(b'queue name to pop (DEPRECATED)'), _(b'NAME')),
3537 (
3541 (
3538 b'',
3542 b'',
3539 b'keep-changes',
3543 b'keep-changes',
3540 None,
3544 None,
3541 _(b'tolerate non-conflicting local changes'),
3545 _(b'tolerate non-conflicting local changes'),
3542 ),
3546 ),
3543 (b'f', b'force', None, _(b'forget any local changes to patched files')),
3547 (b'f', b'force', None, _(b'forget any local changes to patched files')),
3544 (b'', b'no-backup', None, _(b'do not save backup copies of files')),
3548 (b'', b'no-backup', None, _(b'do not save backup copies of files')),
3545 ],
3549 ],
3546 _(b'hg qpop [-a] [-f] [PATCH | INDEX]'),
3550 _(b'hg qpop [-a] [-f] [PATCH | INDEX]'),
3547 helpcategory=command.CATEGORY_CHANGE_ORGANIZATION,
3551 helpcategory=command.CATEGORY_CHANGE_ORGANIZATION,
3548 helpbasic=True,
3552 helpbasic=True,
3549 )
3553 )
3550 def pop(ui, repo, patch=None, **opts):
3554 def pop(ui, repo, patch=None, **opts):
3551 """pop the current patch off the stack
3555 """pop the current patch off the stack
3552
3556
3553 Without argument, pops off the top of the patch stack. If given a
3557 Without argument, pops off the top of the patch stack. If given a
3554 patch name, keeps popping off patches until the named patch is at
3558 patch name, keeps popping off patches until the named patch is at
3555 the top of the stack.
3559 the top of the stack.
3556
3560
3557 By default, abort if the working directory contains uncommitted
3561 By default, abort if the working directory contains uncommitted
3558 changes. With --keep-changes, abort only if the uncommitted files
3562 changes. With --keep-changes, abort only if the uncommitted files
3559 overlap with patched files. With -f/--force, backup and discard
3563 overlap with patched files. With -f/--force, backup and discard
3560 changes made to such files.
3564 changes made to such files.
3561
3565
3562 Return 0 on success.
3566 Return 0 on success.
3563 """
3567 """
3564 opts = pycompat.byteskwargs(opts)
3568 opts = pycompat.byteskwargs(opts)
3565 opts = fixkeepchangesopts(ui, opts)
3569 opts = fixkeepchangesopts(ui, opts)
3566 localupdate = True
3570 localupdate = True
3567 if opts.get(b'name'):
3571 if opts.get(b'name'):
3568 q = queue(ui, repo.baseui, repo.path, repo.vfs.join(opts.get(b'name')))
3572 q = queue(ui, repo.baseui, repo.path, repo.vfs.join(opts.get(b'name')))
3569 ui.warn(_(b'using patch queue: %s\n') % q.path)
3573 ui.warn(_(b'using patch queue: %s\n') % q.path)
3570 localupdate = False
3574 localupdate = False
3571 else:
3575 else:
3572 q = repo.mq
3576 q = repo.mq
3573 ret = q.pop(
3577 ret = q.pop(
3574 repo,
3578 repo,
3575 patch,
3579 patch,
3576 force=opts.get(b'force'),
3580 force=opts.get(b'force'),
3577 update=localupdate,
3581 update=localupdate,
3578 all=opts.get(b'all'),
3582 all=opts.get(b'all'),
3579 nobackup=opts.get(b'no_backup'),
3583 nobackup=opts.get(b'no_backup'),
3580 keepchanges=opts.get(b'keep_changes'),
3584 keepchanges=opts.get(b'keep_changes'),
3581 )
3585 )
3582 q.savedirty()
3586 q.savedirty()
3583 return ret
3587 return ret
3584
3588
3585
3589
3586 @command(
3590 @command(
3587 b"qrename|qmv",
3591 b"qrename|qmv",
3588 [],
3592 [],
3589 _(b'hg qrename PATCH1 [PATCH2]'),
3593 _(b'hg qrename PATCH1 [PATCH2]'),
3590 helpcategory=command.CATEGORY_CHANGE_ORGANIZATION,
3594 helpcategory=command.CATEGORY_CHANGE_ORGANIZATION,
3591 )
3595 )
3592 def rename(ui, repo, patch, name=None, **opts):
3596 def rename(ui, repo, patch, name=None, **opts):
3593 """rename a patch
3597 """rename a patch
3594
3598
3595 With one argument, renames the current patch to PATCH1.
3599 With one argument, renames the current patch to PATCH1.
3596 With two arguments, renames PATCH1 to PATCH2.
3600 With two arguments, renames PATCH1 to PATCH2.
3597
3601
3598 Returns 0 on success."""
3602 Returns 0 on success."""
3599 q = repo.mq
3603 q = repo.mq
3600 if not name:
3604 if not name:
3601 name = patch
3605 name = patch
3602 patch = None
3606 patch = None
3603
3607
3604 if patch:
3608 if patch:
3605 patch = q.lookup(patch)
3609 patch = q.lookup(patch)
3606 else:
3610 else:
3607 if not q.applied:
3611 if not q.applied:
3608 ui.write(_(b'no patches applied\n'))
3612 ui.write(_(b'no patches applied\n'))
3609 return
3613 return
3610 patch = q.lookup(b'qtip')
3614 patch = q.lookup(b'qtip')
3611 absdest = q.join(name)
3615 absdest = q.join(name)
3612 if os.path.isdir(absdest):
3616 if os.path.isdir(absdest):
3613 name = normname(os.path.join(name, os.path.basename(patch)))
3617 name = normname(os.path.join(name, os.path.basename(patch)))
3614 absdest = q.join(name)
3618 absdest = q.join(name)
3615 q.checkpatchname(name)
3619 q.checkpatchname(name)
3616
3620
3617 ui.note(_(b'renaming %s to %s\n') % (patch, name))
3621 ui.note(_(b'renaming %s to %s\n') % (patch, name))
3618 i = q.findseries(patch)
3622 i = q.findseries(patch)
3619 guards = q.guard_re.findall(q.fullseries[i])
3623 guards = q.guard_re.findall(q.fullseries[i])
3620 q.fullseries[i] = name + b''.join([b' #' + g for g in guards])
3624 q.fullseries[i] = name + b''.join([b' #' + g for g in guards])
3621 q.parseseries()
3625 q.parseseries()
3622 q.seriesdirty = True
3626 q.seriesdirty = True
3623
3627
3624 info = q.isapplied(patch)
3628 info = q.isapplied(patch)
3625 if info:
3629 if info:
3626 q.applied[info[0]] = statusentry(info[1], name)
3630 q.applied[info[0]] = statusentry(info[1], name)
3627 q.applieddirty = True
3631 q.applieddirty = True
3628
3632
3629 destdir = os.path.dirname(absdest)
3633 destdir = os.path.dirname(absdest)
3630 if not os.path.isdir(destdir):
3634 if not os.path.isdir(destdir):
3631 os.makedirs(destdir)
3635 os.makedirs(destdir)
3632 util.rename(q.join(patch), absdest)
3636 util.rename(q.join(patch), absdest)
3633 r = q.qrepo()
3637 r = q.qrepo()
3634 if r and patch in r.dirstate:
3638 if r and patch in r.dirstate:
3635 wctx = r[None]
3639 wctx = r[None]
3636 with r.wlock():
3640 with r.wlock():
3637 if r.dirstate[patch] == b'a':
3641 if r.dirstate[patch] == b'a':
3638 r.dirstate.set_untracked(patch)
3642 r.dirstate.set_untracked(patch)
3639 r.dirstate.set_tracked(name)
3643 r.dirstate.set_tracked(name)
3640 else:
3644 else:
3641 wctx.copy(patch, name)
3645 wctx.copy(patch, name)
3642 wctx.forget([patch])
3646 wctx.forget([patch])
3643
3647
3644 q.savedirty()
3648 q.savedirty()
3645
3649
3646
3650
3647 @command(
3651 @command(
3648 b"qrestore",
3652 b"qrestore",
3649 [
3653 [
3650 (b'd', b'delete', None, _(b'delete save entry')),
3654 (b'd', b'delete', None, _(b'delete save entry')),
3651 (b'u', b'update', None, _(b'update queue working directory')),
3655 (b'u', b'update', None, _(b'update queue working directory')),
3652 ],
3656 ],
3653 _(b'hg qrestore [-d] [-u] REV'),
3657 _(b'hg qrestore [-d] [-u] REV'),
3654 helpcategory=command.CATEGORY_CHANGE_ORGANIZATION,
3658 helpcategory=command.CATEGORY_CHANGE_ORGANIZATION,
3655 )
3659 )
3656 def restore(ui, repo, rev, **opts):
3660 def restore(ui, repo, rev, **opts):
3657 """restore the queue state saved by a revision (DEPRECATED)
3661 """restore the queue state saved by a revision (DEPRECATED)
3658
3662
3659 This command is deprecated, use :hg:`rebase` instead."""
3663 This command is deprecated, use :hg:`rebase` instead."""
3660 rev = repo.lookup(rev)
3664 rev = repo.lookup(rev)
3661 q = repo.mq
3665 q = repo.mq
3662 q.restore(repo, rev, delete=opts.get('delete'), qupdate=opts.get('update'))
3666 q.restore(repo, rev, delete=opts.get('delete'), qupdate=opts.get('update'))
3663 q.savedirty()
3667 q.savedirty()
3664 return 0
3668 return 0
3665
3669
3666
3670
3667 @command(
3671 @command(
3668 b"qsave",
3672 b"qsave",
3669 [
3673 [
3670 (b'c', b'copy', None, _(b'copy patch directory')),
3674 (b'c', b'copy', None, _(b'copy patch directory')),
3671 (b'n', b'name', b'', _(b'copy directory name'), _(b'NAME')),
3675 (b'n', b'name', b'', _(b'copy directory name'), _(b'NAME')),
3672 (b'e', b'empty', None, _(b'clear queue status file')),
3676 (b'e', b'empty', None, _(b'clear queue status file')),
3673 (b'f', b'force', None, _(b'force copy')),
3677 (b'f', b'force', None, _(b'force copy')),
3674 ]
3678 ]
3675 + cmdutil.commitopts,
3679 + cmdutil.commitopts,
3676 _(b'hg qsave [-m TEXT] [-l FILE] [-c] [-n NAME] [-e] [-f]'),
3680 _(b'hg qsave [-m TEXT] [-l FILE] [-c] [-n NAME] [-e] [-f]'),
3677 helpcategory=command.CATEGORY_CHANGE_ORGANIZATION,
3681 helpcategory=command.CATEGORY_CHANGE_ORGANIZATION,
3678 )
3682 )
3679 def save(ui, repo, **opts):
3683 def save(ui, repo, **opts):
3680 """save current queue state (DEPRECATED)
3684 """save current queue state (DEPRECATED)
3681
3685
3682 This command is deprecated, use :hg:`rebase` instead."""
3686 This command is deprecated, use :hg:`rebase` instead."""
3683 q = repo.mq
3687 q = repo.mq
3684 opts = pycompat.byteskwargs(opts)
3688 opts = pycompat.byteskwargs(opts)
3685 message = cmdutil.logmessage(ui, opts)
3689 message = cmdutil.logmessage(ui, opts)
3686 ret = q.save(repo, msg=message)
3690 ret = q.save(repo, msg=message)
3687 if ret:
3691 if ret:
3688 return ret
3692 return ret
3689 q.savedirty() # save to .hg/patches before copying
3693 q.savedirty() # save to .hg/patches before copying
3690 if opts.get(b'copy'):
3694 if opts.get(b'copy'):
3691 path = q.path
3695 path = q.path
3692 if opts.get(b'name'):
3696 if opts.get(b'name'):
3693 newpath = os.path.join(q.basepath, opts.get(b'name'))
3697 newpath = os.path.join(q.basepath, opts.get(b'name'))
3694 if os.path.exists(newpath):
3698 if os.path.exists(newpath):
3695 if not os.path.isdir(newpath):
3699 if not os.path.isdir(newpath):
3696 raise error.Abort(
3700 raise error.Abort(
3697 _(b'destination %s exists and is not a directory')
3701 _(b'destination %s exists and is not a directory')
3698 % newpath
3702 % newpath
3699 )
3703 )
3700 if not opts.get(b'force'):
3704 if not opts.get(b'force'):
3701 raise error.Abort(
3705 raise error.Abort(
3702 _(b'destination %s exists, use -f to force') % newpath
3706 _(b'destination %s exists, use -f to force') % newpath
3703 )
3707 )
3704 else:
3708 else:
3705 newpath = savename(path)
3709 newpath = savename(path)
3706 ui.warn(_(b"copy %s to %s\n") % (path, newpath))
3710 ui.warn(_(b"copy %s to %s\n") % (path, newpath))
3707 util.copyfiles(path, newpath)
3711 util.copyfiles(path, newpath)
3708 if opts.get(b'empty'):
3712 if opts.get(b'empty'):
3709 del q.applied[:]
3713 del q.applied[:]
3710 q.applieddirty = True
3714 q.applieddirty = True
3711 q.savedirty()
3715 q.savedirty()
3712 return 0
3716 return 0
3713
3717
3714
3718
3715 @command(
3719 @command(
3716 b"qselect",
3720 b"qselect",
3717 [
3721 [
3718 (b'n', b'none', None, _(b'disable all guards')),
3722 (b'n', b'none', None, _(b'disable all guards')),
3719 (b's', b'series', None, _(b'list all guards in series file')),
3723 (b's', b'series', None, _(b'list all guards in series file')),
3720 (b'', b'pop', None, _(b'pop to before first guarded applied patch')),
3724 (b'', b'pop', None, _(b'pop to before first guarded applied patch')),
3721 (b'', b'reapply', None, _(b'pop, then reapply patches')),
3725 (b'', b'reapply', None, _(b'pop, then reapply patches')),
3722 ],
3726 ],
3723 _(b'hg qselect [OPTION]... [GUARD]...'),
3727 _(b'hg qselect [OPTION]... [GUARD]...'),
3724 helpcategory=command.CATEGORY_CHANGE_ORGANIZATION,
3728 helpcategory=command.CATEGORY_CHANGE_ORGANIZATION,
3725 )
3729 )
3726 def select(ui, repo, *args, **opts):
3730 def select(ui, repo, *args, **opts):
3727 """set or print guarded patches to push
3731 """set or print guarded patches to push
3728
3732
3729 Use the :hg:`qguard` command to set or print guards on patch, then use
3733 Use the :hg:`qguard` command to set or print guards on patch, then use
3730 qselect to tell mq which guards to use. A patch will be pushed if
3734 qselect to tell mq which guards to use. A patch will be pushed if
3731 it has no guards or any positive guards match the currently
3735 it has no guards or any positive guards match the currently
3732 selected guard, but will not be pushed if any negative guards
3736 selected guard, but will not be pushed if any negative guards
3733 match the current guard. For example::
3737 match the current guard. For example::
3734
3738
3735 qguard foo.patch -- -stable (negative guard)
3739 qguard foo.patch -- -stable (negative guard)
3736 qguard bar.patch +stable (positive guard)
3740 qguard bar.patch +stable (positive guard)
3737 qselect stable
3741 qselect stable
3738
3742
3739 This activates the "stable" guard. mq will skip foo.patch (because
3743 This activates the "stable" guard. mq will skip foo.patch (because
3740 it has a negative match) but push bar.patch (because it has a
3744 it has a negative match) but push bar.patch (because it has a
3741 positive match).
3745 positive match).
3742
3746
3743 With no arguments, prints the currently active guards.
3747 With no arguments, prints the currently active guards.
3744 With one argument, sets the active guard.
3748 With one argument, sets the active guard.
3745
3749
3746 Use -n/--none to deactivate guards (no other arguments needed).
3750 Use -n/--none to deactivate guards (no other arguments needed).
3747 When no guards are active, patches with positive guards are
3751 When no guards are active, patches with positive guards are
3748 skipped and patches with negative guards are pushed.
3752 skipped and patches with negative guards are pushed.
3749
3753
3750 qselect can change the guards on applied patches. It does not pop
3754 qselect can change the guards on applied patches. It does not pop
3751 guarded patches by default. Use --pop to pop back to the last
3755 guarded patches by default. Use --pop to pop back to the last
3752 applied patch that is not guarded. Use --reapply (which implies
3756 applied patch that is not guarded. Use --reapply (which implies
3753 --pop) to push back to the current patch afterwards, but skip
3757 --pop) to push back to the current patch afterwards, but skip
3754 guarded patches.
3758 guarded patches.
3755
3759
3756 Use -s/--series to print a list of all guards in the series file
3760 Use -s/--series to print a list of all guards in the series file
3757 (no other arguments needed). Use -v for more information.
3761 (no other arguments needed). Use -v for more information.
3758
3762
3759 Returns 0 on success."""
3763 Returns 0 on success."""
3760
3764
3761 q = repo.mq
3765 q = repo.mq
3762 opts = pycompat.byteskwargs(opts)
3766 opts = pycompat.byteskwargs(opts)
3763 guards = q.active()
3767 guards = q.active()
3764 pushable = lambda i: q.pushable(q.applied[i].name)[0]
3768 pushable = lambda i: q.pushable(q.applied[i].name)[0]
3765 if args or opts.get(b'none'):
3769 if args or opts.get(b'none'):
3766 old_unapplied = q.unapplied(repo)
3770 old_unapplied = q.unapplied(repo)
3767 old_guarded = [
3771 old_guarded = [
3768 i for i in pycompat.xrange(len(q.applied)) if not pushable(i)
3772 i for i in pycompat.xrange(len(q.applied)) if not pushable(i)
3769 ]
3773 ]
3770 q.setactive(args)
3774 q.setactive(args)
3771 q.savedirty()
3775 q.savedirty()
3772 if not args:
3776 if not args:
3773 ui.status(_(b'guards deactivated\n'))
3777 ui.status(_(b'guards deactivated\n'))
3774 if not opts.get(b'pop') and not opts.get(b'reapply'):
3778 if not opts.get(b'pop') and not opts.get(b'reapply'):
3775 unapplied = q.unapplied(repo)
3779 unapplied = q.unapplied(repo)
3776 guarded = [
3780 guarded = [
3777 i for i in pycompat.xrange(len(q.applied)) if not pushable(i)
3781 i for i in pycompat.xrange(len(q.applied)) if not pushable(i)
3778 ]
3782 ]
3779 if len(unapplied) != len(old_unapplied):
3783 if len(unapplied) != len(old_unapplied):
3780 ui.status(
3784 ui.status(
3781 _(
3785 _(
3782 b'number of unguarded, unapplied patches has '
3786 b'number of unguarded, unapplied patches has '
3783 b'changed from %d to %d\n'
3787 b'changed from %d to %d\n'
3784 )
3788 )
3785 % (len(old_unapplied), len(unapplied))
3789 % (len(old_unapplied), len(unapplied))
3786 )
3790 )
3787 if len(guarded) != len(old_guarded):
3791 if len(guarded) != len(old_guarded):
3788 ui.status(
3792 ui.status(
3789 _(
3793 _(
3790 b'number of guarded, applied patches has changed '
3794 b'number of guarded, applied patches has changed '
3791 b'from %d to %d\n'
3795 b'from %d to %d\n'
3792 )
3796 )
3793 % (len(old_guarded), len(guarded))
3797 % (len(old_guarded), len(guarded))
3794 )
3798 )
3795 elif opts.get(b'series'):
3799 elif opts.get(b'series'):
3796 guards = {}
3800 guards = {}
3797 noguards = 0
3801 noguards = 0
3798 for gs in q.seriesguards:
3802 for gs in q.seriesguards:
3799 if not gs:
3803 if not gs:
3800 noguards += 1
3804 noguards += 1
3801 for g in gs:
3805 for g in gs:
3802 guards.setdefault(g, 0)
3806 guards.setdefault(g, 0)
3803 guards[g] += 1
3807 guards[g] += 1
3804 if ui.verbose:
3808 if ui.verbose:
3805 guards[b'NONE'] = noguards
3809 guards[b'NONE'] = noguards
3806 guards = list(guards.items())
3810 guards = list(guards.items())
3807 guards.sort(key=lambda x: x[0][1:])
3811 guards.sort(key=lambda x: x[0][1:])
3808 if guards:
3812 if guards:
3809 ui.note(_(b'guards in series file:\n'))
3813 ui.note(_(b'guards in series file:\n'))
3810 for guard, count in guards:
3814 for guard, count in guards:
3811 ui.note(b'%2d ' % count)
3815 ui.note(b'%2d ' % count)
3812 ui.write(guard, b'\n')
3816 ui.write(guard, b'\n')
3813 else:
3817 else:
3814 ui.note(_(b'no guards in series file\n'))
3818 ui.note(_(b'no guards in series file\n'))
3815 else:
3819 else:
3816 if guards:
3820 if guards:
3817 ui.note(_(b'active guards:\n'))
3821 ui.note(_(b'active guards:\n'))
3818 for g in guards:
3822 for g in guards:
3819 ui.write(g, b'\n')
3823 ui.write(g, b'\n')
3820 else:
3824 else:
3821 ui.write(_(b'no active guards\n'))
3825 ui.write(_(b'no active guards\n'))
3822 reapply = opts.get(b'reapply') and q.applied and q.applied[-1].name
3826 reapply = opts.get(b'reapply') and q.applied and q.applied[-1].name
3823 popped = False
3827 popped = False
3824 if opts.get(b'pop') or opts.get(b'reapply'):
3828 if opts.get(b'pop') or opts.get(b'reapply'):
3825 for i in pycompat.xrange(len(q.applied)):
3829 for i in pycompat.xrange(len(q.applied)):
3826 if not pushable(i):
3830 if not pushable(i):
3827 ui.status(_(b'popping guarded patches\n'))
3831 ui.status(_(b'popping guarded patches\n'))
3828 popped = True
3832 popped = True
3829 if i == 0:
3833 if i == 0:
3830 q.pop(repo, all=True)
3834 q.pop(repo, all=True)
3831 else:
3835 else:
3832 q.pop(repo, q.applied[i - 1].name)
3836 q.pop(repo, q.applied[i - 1].name)
3833 break
3837 break
3834 if popped:
3838 if popped:
3835 try:
3839 try:
3836 if reapply:
3840 if reapply:
3837 ui.status(_(b'reapplying unguarded patches\n'))
3841 ui.status(_(b'reapplying unguarded patches\n'))
3838 q.push(repo, reapply)
3842 q.push(repo, reapply)
3839 finally:
3843 finally:
3840 q.savedirty()
3844 q.savedirty()
3841
3845
3842
3846
3843 @command(
3847 @command(
3844 b"qfinish",
3848 b"qfinish",
3845 [(b'a', b'applied', None, _(b'finish all applied changesets'))],
3849 [(b'a', b'applied', None, _(b'finish all applied changesets'))],
3846 _(b'hg qfinish [-a] [REV]...'),
3850 _(b'hg qfinish [-a] [REV]...'),
3847 helpcategory=command.CATEGORY_CHANGE_ORGANIZATION,
3851 helpcategory=command.CATEGORY_CHANGE_ORGANIZATION,
3848 )
3852 )
3849 def finish(ui, repo, *revrange, **opts):
3853 def finish(ui, repo, *revrange, **opts):
3850 """move applied patches into repository history
3854 """move applied patches into repository history
3851
3855
3852 Finishes the specified revisions (corresponding to applied
3856 Finishes the specified revisions (corresponding to applied
3853 patches) by moving them out of mq control into regular repository
3857 patches) by moving them out of mq control into regular repository
3854 history.
3858 history.
3855
3859
3856 Accepts a revision range or the -a/--applied option. If --applied
3860 Accepts a revision range or the -a/--applied option. If --applied
3857 is specified, all applied mq revisions are removed from mq
3861 is specified, all applied mq revisions are removed from mq
3858 control. Otherwise, the given revisions must be at the base of the
3862 control. Otherwise, the given revisions must be at the base of the
3859 stack of applied patches.
3863 stack of applied patches.
3860
3864
3861 This can be especially useful if your changes have been applied to
3865 This can be especially useful if your changes have been applied to
3862 an upstream repository, or if you are about to push your changes
3866 an upstream repository, or if you are about to push your changes
3863 to upstream.
3867 to upstream.
3864
3868
3865 Returns 0 on success.
3869 Returns 0 on success.
3866 """
3870 """
3867 if not opts.get('applied') and not revrange:
3871 if not opts.get('applied') and not revrange:
3868 raise error.Abort(_(b'no revisions specified'))
3872 raise error.Abort(_(b'no revisions specified'))
3869 elif opts.get('applied'):
3873 elif opts.get('applied'):
3870 revrange = (b'qbase::qtip',) + revrange
3874 revrange = (b'qbase::qtip',) + revrange
3871
3875
3872 q = repo.mq
3876 q = repo.mq
3873 if not q.applied:
3877 if not q.applied:
3874 ui.status(_(b'no patches applied\n'))
3878 ui.status(_(b'no patches applied\n'))
3875 return 0
3879 return 0
3876
3880
3877 revs = scmutil.revrange(repo, revrange)
3881 revs = scmutil.revrange(repo, revrange)
3878 if repo[b'.'].rev() in revs and repo[None].files():
3882 if repo[b'.'].rev() in revs and repo[None].files():
3879 ui.warn(_(b'warning: uncommitted changes in the working directory\n'))
3883 ui.warn(_(b'warning: uncommitted changes in the working directory\n'))
3880 # queue.finish may changes phases but leave the responsibility to lock the
3884 # queue.finish may changes phases but leave the responsibility to lock the
3881 # repo to the caller to avoid deadlock with wlock. This command code is
3885 # repo to the caller to avoid deadlock with wlock. This command code is
3882 # responsibility for this locking.
3886 # responsibility for this locking.
3883 with repo.lock():
3887 with repo.lock():
3884 q.finish(repo, revs)
3888 q.finish(repo, revs)
3885 q.savedirty()
3889 q.savedirty()
3886 return 0
3890 return 0
3887
3891
3888
3892
3889 @command(
3893 @command(
3890 b"qqueue",
3894 b"qqueue",
3891 [
3895 [
3892 (b'l', b'list', False, _(b'list all available queues')),
3896 (b'l', b'list', False, _(b'list all available queues')),
3893 (b'', b'active', False, _(b'print name of active queue')),
3897 (b'', b'active', False, _(b'print name of active queue')),
3894 (b'c', b'create', False, _(b'create new queue')),
3898 (b'c', b'create', False, _(b'create new queue')),
3895 (b'', b'rename', False, _(b'rename active queue')),
3899 (b'', b'rename', False, _(b'rename active queue')),
3896 (b'', b'delete', False, _(b'delete reference to queue')),
3900 (b'', b'delete', False, _(b'delete reference to queue')),
3897 (b'', b'purge', False, _(b'delete queue, and remove patch dir')),
3901 (b'', b'purge', False, _(b'delete queue, and remove patch dir')),
3898 ],
3902 ],
3899 _(b'[OPTION] [QUEUE]'),
3903 _(b'[OPTION] [QUEUE]'),
3900 helpcategory=command.CATEGORY_CHANGE_ORGANIZATION,
3904 helpcategory=command.CATEGORY_CHANGE_ORGANIZATION,
3901 )
3905 )
3902 def qqueue(ui, repo, name=None, **opts):
3906 def qqueue(ui, repo, name=None, **opts):
3903 """manage multiple patch queues
3907 """manage multiple patch queues
3904
3908
3905 Supports switching between different patch queues, as well as creating
3909 Supports switching between different patch queues, as well as creating
3906 new patch queues and deleting existing ones.
3910 new patch queues and deleting existing ones.
3907
3911
3908 Omitting a queue name or specifying -l/--list will show you the registered
3912 Omitting a queue name or specifying -l/--list will show you the registered
3909 queues - by default the "normal" patches queue is registered. The currently
3913 queues - by default the "normal" patches queue is registered. The currently
3910 active queue will be marked with "(active)". Specifying --active will print
3914 active queue will be marked with "(active)". Specifying --active will print
3911 only the name of the active queue.
3915 only the name of the active queue.
3912
3916
3913 To create a new queue, use -c/--create. The queue is automatically made
3917 To create a new queue, use -c/--create. The queue is automatically made
3914 active, except in the case where there are applied patches from the
3918 active, except in the case where there are applied patches from the
3915 currently active queue in the repository. Then the queue will only be
3919 currently active queue in the repository. Then the queue will only be
3916 created and switching will fail.
3920 created and switching will fail.
3917
3921
3918 To delete an existing queue, use --delete. You cannot delete the currently
3922 To delete an existing queue, use --delete. You cannot delete the currently
3919 active queue.
3923 active queue.
3920
3924
3921 Returns 0 on success.
3925 Returns 0 on success.
3922 """
3926 """
3923 q = repo.mq
3927 q = repo.mq
3924 _defaultqueue = b'patches'
3928 _defaultqueue = b'patches'
3925 _allqueues = b'patches.queues'
3929 _allqueues = b'patches.queues'
3926 _activequeue = b'patches.queue'
3930 _activequeue = b'patches.queue'
3927
3931
3928 def _getcurrent():
3932 def _getcurrent():
3929 cur = os.path.basename(q.path)
3933 cur = os.path.basename(q.path)
3930 if cur.startswith(b'patches-'):
3934 if cur.startswith(b'patches-'):
3931 cur = cur[8:]
3935 cur = cur[8:]
3932 return cur
3936 return cur
3933
3937
3934 def _noqueues():
3938 def _noqueues():
3935 try:
3939 try:
3936 fh = repo.vfs(_allqueues, b'r')
3940 fh = repo.vfs(_allqueues, b'r')
3937 fh.close()
3941 fh.close()
3938 except IOError:
3942 except IOError:
3939 return True
3943 return True
3940
3944
3941 return False
3945 return False
3942
3946
3943 def _getqueues():
3947 def _getqueues():
3944 current = _getcurrent()
3948 current = _getcurrent()
3945
3949
3946 try:
3950 try:
3947 fh = repo.vfs(_allqueues, b'r')
3951 fh = repo.vfs(_allqueues, b'r')
3948 queues = [queue.strip() for queue in fh if queue.strip()]
3952 queues = [queue.strip() for queue in fh if queue.strip()]
3949 fh.close()
3953 fh.close()
3950 if current not in queues:
3954 if current not in queues:
3951 queues.append(current)
3955 queues.append(current)
3952 except IOError:
3956 except IOError:
3953 queues = [_defaultqueue]
3957 queues = [_defaultqueue]
3954
3958
3955 return sorted(queues)
3959 return sorted(queues)
3956
3960
3957 def _setactive(name):
3961 def _setactive(name):
3958 if q.applied:
3962 if q.applied:
3959 raise error.Abort(
3963 raise error.Abort(
3960 _(
3964 _(
3961 b'new queue created, but cannot make active '
3965 b'new queue created, but cannot make active '
3962 b'as patches are applied'
3966 b'as patches are applied'
3963 )
3967 )
3964 )
3968 )
3965 _setactivenocheck(name)
3969 _setactivenocheck(name)
3966
3970
3967 def _setactivenocheck(name):
3971 def _setactivenocheck(name):
3968 fh = repo.vfs(_activequeue, b'w')
3972 fh = repo.vfs(_activequeue, b'w')
3969 if name != b'patches':
3973 if name != b'patches':
3970 fh.write(name)
3974 fh.write(name)
3971 fh.close()
3975 fh.close()
3972
3976
3973 def _addqueue(name):
3977 def _addqueue(name):
3974 fh = repo.vfs(_allqueues, b'a')
3978 fh = repo.vfs(_allqueues, b'a')
3975 fh.write(b'%s\n' % (name,))
3979 fh.write(b'%s\n' % (name,))
3976 fh.close()
3980 fh.close()
3977
3981
3978 def _queuedir(name):
3982 def _queuedir(name):
3979 if name == b'patches':
3983 if name == b'patches':
3980 return repo.vfs.join(b'patches')
3984 return repo.vfs.join(b'patches')
3981 else:
3985 else:
3982 return repo.vfs.join(b'patches-' + name)
3986 return repo.vfs.join(b'patches-' + name)
3983
3987
3984 def _validname(name):
3988 def _validname(name):
3985 for n in name:
3989 for n in name:
3986 if n in b':\\/.':
3990 if n in b':\\/.':
3987 return False
3991 return False
3988 return True
3992 return True
3989
3993
3990 def _delete(name):
3994 def _delete(name):
3991 if name not in existing:
3995 if name not in existing:
3992 raise error.Abort(_(b'cannot delete queue that does not exist'))
3996 raise error.Abort(_(b'cannot delete queue that does not exist'))
3993
3997
3994 current = _getcurrent()
3998 current = _getcurrent()
3995
3999
3996 if name == current:
4000 if name == current:
3997 raise error.Abort(_(b'cannot delete currently active queue'))
4001 raise error.Abort(_(b'cannot delete currently active queue'))
3998
4002
3999 fh = repo.vfs(b'patches.queues.new', b'w')
4003 fh = repo.vfs(b'patches.queues.new', b'w')
4000 for queue in existing:
4004 for queue in existing:
4001 if queue == name:
4005 if queue == name:
4002 continue
4006 continue
4003 fh.write(b'%s\n' % (queue,))
4007 fh.write(b'%s\n' % (queue,))
4004 fh.close()
4008 fh.close()
4005 repo.vfs.rename(b'patches.queues.new', _allqueues)
4009 repo.vfs.rename(b'patches.queues.new', _allqueues)
4006
4010
4007 opts = pycompat.byteskwargs(opts)
4011 opts = pycompat.byteskwargs(opts)
4008 if not name or opts.get(b'list') or opts.get(b'active'):
4012 if not name or opts.get(b'list') or opts.get(b'active'):
4009 current = _getcurrent()
4013 current = _getcurrent()
4010 if opts.get(b'active'):
4014 if opts.get(b'active'):
4011 ui.write(b'%s\n' % (current,))
4015 ui.write(b'%s\n' % (current,))
4012 return
4016 return
4013 for queue in _getqueues():
4017 for queue in _getqueues():
4014 ui.write(b'%s' % (queue,))
4018 ui.write(b'%s' % (queue,))
4015 if queue == current and not ui.quiet:
4019 if queue == current and not ui.quiet:
4016 ui.write(_(b' (active)\n'))
4020 ui.write(_(b' (active)\n'))
4017 else:
4021 else:
4018 ui.write(b'\n')
4022 ui.write(b'\n')
4019 return
4023 return
4020
4024
4021 if not _validname(name):
4025 if not _validname(name):
4022 raise error.Abort(
4026 raise error.Abort(
4023 _(b'invalid queue name, may not contain the characters ":\\/."')
4027 _(b'invalid queue name, may not contain the characters ":\\/."')
4024 )
4028 )
4025
4029
4026 with repo.wlock():
4030 with repo.wlock():
4027 existing = _getqueues()
4031 existing = _getqueues()
4028
4032
4029 if opts.get(b'create'):
4033 if opts.get(b'create'):
4030 if name in existing:
4034 if name in existing:
4031 raise error.Abort(_(b'queue "%s" already exists') % name)
4035 raise error.Abort(_(b'queue "%s" already exists') % name)
4032 if _noqueues():
4036 if _noqueues():
4033 _addqueue(_defaultqueue)
4037 _addqueue(_defaultqueue)
4034 _addqueue(name)
4038 _addqueue(name)
4035 _setactive(name)
4039 _setactive(name)
4036 elif opts.get(b'rename'):
4040 elif opts.get(b'rename'):
4037 current = _getcurrent()
4041 current = _getcurrent()
4038 if name == current:
4042 if name == current:
4039 raise error.Abort(
4043 raise error.Abort(
4040 _(b'can\'t rename "%s" to its current name') % name
4044 _(b'can\'t rename "%s" to its current name') % name
4041 )
4045 )
4042 if name in existing:
4046 if name in existing:
4043 raise error.Abort(_(b'queue "%s" already exists') % name)
4047 raise error.Abort(_(b'queue "%s" already exists') % name)
4044
4048
4045 olddir = _queuedir(current)
4049 olddir = _queuedir(current)
4046 newdir = _queuedir(name)
4050 newdir = _queuedir(name)
4047
4051
4048 if os.path.exists(newdir):
4052 if os.path.exists(newdir):
4049 raise error.Abort(
4053 raise error.Abort(
4050 _(b'non-queue directory "%s" already exists') % newdir
4054 _(b'non-queue directory "%s" already exists') % newdir
4051 )
4055 )
4052
4056
4053 fh = repo.vfs(b'patches.queues.new', b'w')
4057 fh = repo.vfs(b'patches.queues.new', b'w')
4054 for queue in existing:
4058 for queue in existing:
4055 if queue == current:
4059 if queue == current:
4056 fh.write(b'%s\n' % (name,))
4060 fh.write(b'%s\n' % (name,))
4057 if os.path.exists(olddir):
4061 if os.path.exists(olddir):
4058 util.rename(olddir, newdir)
4062 util.rename(olddir, newdir)
4059 else:
4063 else:
4060 fh.write(b'%s\n' % (queue,))
4064 fh.write(b'%s\n' % (queue,))
4061 fh.close()
4065 fh.close()
4062 repo.vfs.rename(b'patches.queues.new', _allqueues)
4066 repo.vfs.rename(b'patches.queues.new', _allqueues)
4063 _setactivenocheck(name)
4067 _setactivenocheck(name)
4064 elif opts.get(b'delete'):
4068 elif opts.get(b'delete'):
4065 _delete(name)
4069 _delete(name)
4066 elif opts.get(b'purge'):
4070 elif opts.get(b'purge'):
4067 if name in existing:
4071 if name in existing:
4068 _delete(name)
4072 _delete(name)
4069 qdir = _queuedir(name)
4073 qdir = _queuedir(name)
4070 if os.path.exists(qdir):
4074 if os.path.exists(qdir):
4071 shutil.rmtree(qdir)
4075 shutil.rmtree(qdir)
4072 else:
4076 else:
4073 if name not in existing:
4077 if name not in existing:
4074 raise error.Abort(_(b'use --create to create a new queue'))
4078 raise error.Abort(_(b'use --create to create a new queue'))
4075 _setactive(name)
4079 _setactive(name)
4076
4080
4077
4081
4078 def mqphasedefaults(repo, roots):
4082 def mqphasedefaults(repo, roots):
4079 """callback used to set mq changeset as secret when no phase data exists"""
4083 """callback used to set mq changeset as secret when no phase data exists"""
4080 if repo.mq.applied:
4084 if repo.mq.applied:
4081 if repo.ui.configbool(b'mq', b'secret'):
4085 if repo.ui.configbool(b'mq', b'secret'):
4082 mqphase = phases.secret
4086 mqphase = phases.secret
4083 else:
4087 else:
4084 mqphase = phases.draft
4088 mqphase = phases.draft
4085 qbase = repo[repo.mq.applied[0].node]
4089 qbase = repo[repo.mq.applied[0].node]
4086 roots[mqphase].add(qbase.node())
4090 roots[mqphase].add(qbase.node())
4087 return roots
4091 return roots
4088
4092
4089
4093
4090 def reposetup(ui, repo):
4094 def reposetup(ui, repo):
4091 class mqrepo(repo.__class__):
4095 class mqrepo(repo.__class__):
4092 @localrepo.unfilteredpropertycache
4096 @localrepo.unfilteredpropertycache
4093 def mq(self):
4097 def mq(self):
4094 return queue(self.ui, self.baseui, self.path)
4098 return queue(self.ui, self.baseui, self.path)
4095
4099
4096 def invalidateall(self):
4100 def invalidateall(self):
4097 super(mqrepo, self).invalidateall()
4101 super(mqrepo, self).invalidateall()
4098 if localrepo.hasunfilteredcache(self, 'mq'):
4102 if localrepo.hasunfilteredcache(self, 'mq'):
4099 # recreate mq in case queue path was changed
4103 # recreate mq in case queue path was changed
4100 delattr(self.unfiltered(), 'mq')
4104 delattr(self.unfiltered(), 'mq')
4101
4105
4102 def abortifwdirpatched(self, errmsg, force=False):
4106 def abortifwdirpatched(self, errmsg, force=False):
4103 if self.mq.applied and self.mq.checkapplied and not force:
4107 if self.mq.applied and self.mq.checkapplied and not force:
4104 parents = self.dirstate.parents()
4108 parents = self.dirstate.parents()
4105 patches = [s.node for s in self.mq.applied]
4109 patches = [s.node for s in self.mq.applied]
4106 if any(p in patches for p in parents):
4110 if any(p in patches for p in parents):
4107 raise error.Abort(errmsg)
4111 raise error.Abort(errmsg)
4108
4112
4109 def commit(
4113 def commit(
4110 self,
4114 self,
4111 text=b"",
4115 text=b"",
4112 user=None,
4116 user=None,
4113 date=None,
4117 date=None,
4114 match=None,
4118 match=None,
4115 force=False,
4119 force=False,
4116 editor=False,
4120 editor=False,
4117 extra=None,
4121 extra=None,
4118 ):
4122 ):
4119 if extra is None:
4123 if extra is None:
4120 extra = {}
4124 extra = {}
4121 self.abortifwdirpatched(
4125 self.abortifwdirpatched(
4122 _(b'cannot commit over an applied mq patch'), force
4126 _(b'cannot commit over an applied mq patch'), force
4123 )
4127 )
4124
4128
4125 return super(mqrepo, self).commit(
4129 return super(mqrepo, self).commit(
4126 text, user, date, match, force, editor, extra
4130 text, user, date, match, force, editor, extra
4127 )
4131 )
4128
4132
4129 def checkpush(self, pushop):
4133 def checkpush(self, pushop):
4130 if self.mq.applied and self.mq.checkapplied and not pushop.force:
4134 if self.mq.applied and self.mq.checkapplied and not pushop.force:
4131 outapplied = [e.node for e in self.mq.applied]
4135 outapplied = [e.node for e in self.mq.applied]
4132 if pushop.revs:
4136 if pushop.revs:
4133 # Assume applied patches have no non-patch descendants and
4137 # Assume applied patches have no non-patch descendants and
4134 # are not on remote already. Filtering any changeset not
4138 # are not on remote already. Filtering any changeset not
4135 # pushed.
4139 # pushed.
4136 heads = set(pushop.revs)
4140 heads = set(pushop.revs)
4137 for node in reversed(outapplied):
4141 for node in reversed(outapplied):
4138 if node in heads:
4142 if node in heads:
4139 break
4143 break
4140 else:
4144 else:
4141 outapplied.pop()
4145 outapplied.pop()
4142 # looking for pushed and shared changeset
4146 # looking for pushed and shared changeset
4143 for node in outapplied:
4147 for node in outapplied:
4144 if self[node].phase() < phases.secret:
4148 if self[node].phase() < phases.secret:
4145 raise error.Abort(_(b'source has mq patches applied'))
4149 raise error.Abort(_(b'source has mq patches applied'))
4146 # no non-secret patches pushed
4150 # no non-secret patches pushed
4147 super(mqrepo, self).checkpush(pushop)
4151 super(mqrepo, self).checkpush(pushop)
4148
4152
4149 def _findtags(self):
4153 def _findtags(self):
4150 '''augment tags from base class with patch tags'''
4154 '''augment tags from base class with patch tags'''
4151 result = super(mqrepo, self)._findtags()
4155 result = super(mqrepo, self)._findtags()
4152
4156
4153 q = self.mq
4157 q = self.mq
4154 if not q.applied:
4158 if not q.applied:
4155 return result
4159 return result
4156
4160
4157 mqtags = [(patch.node, patch.name) for patch in q.applied]
4161 mqtags = [(patch.node, patch.name) for patch in q.applied]
4158
4162
4159 try:
4163 try:
4160 # for now ignore filtering business
4164 # for now ignore filtering business
4161 self.unfiltered().changelog.rev(mqtags[-1][0])
4165 self.unfiltered().changelog.rev(mqtags[-1][0])
4162 except error.LookupError:
4166 except error.LookupError:
4163 self.ui.warn(
4167 self.ui.warn(
4164 _(b'mq status file refers to unknown node %s\n')
4168 _(b'mq status file refers to unknown node %s\n')
4165 % short(mqtags[-1][0])
4169 % short(mqtags[-1][0])
4166 )
4170 )
4167 return result
4171 return result
4168
4172
4169 # do not add fake tags for filtered revisions
4173 # do not add fake tags for filtered revisions
4170 included = self.changelog.hasnode
4174 included = self.changelog.hasnode
4171 mqtags = [mqt for mqt in mqtags if included(mqt[0])]
4175 mqtags = [mqt for mqt in mqtags if included(mqt[0])]
4172 if not mqtags:
4176 if not mqtags:
4173 return result
4177 return result
4174
4178
4175 mqtags.append((mqtags[-1][0], b'qtip'))
4179 mqtags.append((mqtags[-1][0], b'qtip'))
4176 mqtags.append((mqtags[0][0], b'qbase'))
4180 mqtags.append((mqtags[0][0], b'qbase'))
4177 mqtags.append((self.changelog.parents(mqtags[0][0])[0], b'qparent'))
4181 mqtags.append((self.changelog.parents(mqtags[0][0])[0], b'qparent'))
4178 tags = result[0]
4182 tags = result[0]
4179 for patch in mqtags:
4183 for patch in mqtags:
4180 if patch[1] in tags:
4184 if patch[1] in tags:
4181 self.ui.warn(
4185 self.ui.warn(
4182 _(b'tag %s overrides mq patch of the same name\n')
4186 _(b'tag %s overrides mq patch of the same name\n')
4183 % patch[1]
4187 % patch[1]
4184 )
4188 )
4185 else:
4189 else:
4186 tags[patch[1]] = patch[0]
4190 tags[patch[1]] = patch[0]
4187
4191
4188 return result
4192 return result
4189
4193
4190 if repo.local():
4194 if repo.local():
4191 repo.__class__ = mqrepo
4195 repo.__class__ = mqrepo
4192
4196
4193 repo._phasedefaults.append(mqphasedefaults)
4197 repo._phasedefaults.append(mqphasedefaults)
4194
4198
4195
4199
4196 def mqimport(orig, ui, repo, *args, **kwargs):
4200 def mqimport(orig, ui, repo, *args, **kwargs):
4197 if util.safehasattr(repo, b'abortifwdirpatched') and not kwargs.get(
4201 if util.safehasattr(repo, b'abortifwdirpatched') and not kwargs.get(
4198 'no_commit', False
4202 'no_commit', False
4199 ):
4203 ):
4200 repo.abortifwdirpatched(
4204 repo.abortifwdirpatched(
4201 _(b'cannot import over an applied patch'), kwargs.get('force')
4205 _(b'cannot import over an applied patch'), kwargs.get('force')
4202 )
4206 )
4203 return orig(ui, repo, *args, **kwargs)
4207 return orig(ui, repo, *args, **kwargs)
4204
4208
4205
4209
4206 def mqinit(orig, ui, *args, **kwargs):
4210 def mqinit(orig, ui, *args, **kwargs):
4207 mq = kwargs.pop('mq', None)
4211 mq = kwargs.pop('mq', None)
4208
4212
4209 if not mq:
4213 if not mq:
4210 return orig(ui, *args, **kwargs)
4214 return orig(ui, *args, **kwargs)
4211
4215
4212 if args:
4216 if args:
4213 repopath = args[0]
4217 repopath = args[0]
4214 if not hg.islocal(repopath):
4218 if not hg.islocal(repopath):
4215 raise error.Abort(
4219 raise error.Abort(
4216 _(b'only a local queue repository may be initialized')
4220 _(b'only a local queue repository may be initialized')
4217 )
4221 )
4218 else:
4222 else:
4219 repopath = cmdutil.findrepo(encoding.getcwd())
4223 repopath = cmdutil.findrepo(encoding.getcwd())
4220 if not repopath:
4224 if not repopath:
4221 raise error.Abort(
4225 raise error.Abort(
4222 _(b'there is no Mercurial repository here (.hg not found)')
4226 _(b'there is no Mercurial repository here (.hg not found)')
4223 )
4227 )
4224 repo = hg.repository(ui, repopath)
4228 repo = hg.repository(ui, repopath)
4225 return qinit(ui, repo, True)
4229 return qinit(ui, repo, True)
4226
4230
4227
4231
4228 def mqcommand(orig, ui, repo, *args, **kwargs):
4232 def mqcommand(orig, ui, repo, *args, **kwargs):
4229 """Add --mq option to operate on patch repository instead of main"""
4233 """Add --mq option to operate on patch repository instead of main"""
4230
4234
4231 # some commands do not like getting unknown options
4235 # some commands do not like getting unknown options
4232 mq = kwargs.pop('mq', None)
4236 mq = kwargs.pop('mq', None)
4233
4237
4234 if not mq:
4238 if not mq:
4235 return orig(ui, repo, *args, **kwargs)
4239 return orig(ui, repo, *args, **kwargs)
4236
4240
4237 q = repo.mq
4241 q = repo.mq
4238 r = q.qrepo()
4242 r = q.qrepo()
4239 if not r:
4243 if not r:
4240 raise error.Abort(_(b'no queue repository'))
4244 raise error.Abort(_(b'no queue repository'))
4241 return orig(r.ui, r, *args, **kwargs)
4245 return orig(r.ui, r, *args, **kwargs)
4242
4246
4243
4247
4244 def summaryhook(ui, repo):
4248 def summaryhook(ui, repo):
4245 q = repo.mq
4249 q = repo.mq
4246 m = []
4250 m = []
4247 a, u = len(q.applied), len(q.unapplied(repo))
4251 a, u = len(q.applied), len(q.unapplied(repo))
4248 if a:
4252 if a:
4249 m.append(ui.label(_(b"%d applied"), b'qseries.applied') % a)
4253 m.append(ui.label(_(b"%d applied"), b'qseries.applied') % a)
4250 if u:
4254 if u:
4251 m.append(ui.label(_(b"%d unapplied"), b'qseries.unapplied') % u)
4255 m.append(ui.label(_(b"%d unapplied"), b'qseries.unapplied') % u)
4252 if m:
4256 if m:
4253 # i18n: column positioning for "hg summary"
4257 # i18n: column positioning for "hg summary"
4254 ui.write(_(b"mq: %s\n") % b', '.join(m))
4258 ui.write(_(b"mq: %s\n") % b', '.join(m))
4255 else:
4259 else:
4256 # i18n: column positioning for "hg summary"
4260 # i18n: column positioning for "hg summary"
4257 ui.note(_(b"mq: (empty queue)\n"))
4261 ui.note(_(b"mq: (empty queue)\n"))
4258
4262
4259
4263
4260 revsetpredicate = registrar.revsetpredicate()
4264 revsetpredicate = registrar.revsetpredicate()
4261
4265
4262
4266
4263 @revsetpredicate(b'mq()')
4267 @revsetpredicate(b'mq()')
4264 def revsetmq(repo, subset, x):
4268 def revsetmq(repo, subset, x):
4265 """Changesets managed by MQ."""
4269 """Changesets managed by MQ."""
4266 revsetlang.getargs(x, 0, 0, _(b"mq takes no arguments"))
4270 revsetlang.getargs(x, 0, 0, _(b"mq takes no arguments"))
4267 applied = {repo[r.node].rev() for r in repo.mq.applied}
4271 applied = {repo[r.node].rev() for r in repo.mq.applied}
4268 return smartset.baseset([r for r in subset if r in applied])
4272 return smartset.baseset([r for r in subset if r in applied])
4269
4273
4270
4274
4271 # tell hggettext to extract docstrings from these functions:
4275 # tell hggettext to extract docstrings from these functions:
4272 i18nfunctions = [revsetmq]
4276 i18nfunctions = [revsetmq]
4273
4277
4274
4278
4275 def extsetup(ui):
4279 def extsetup(ui):
4276 # Ensure mq wrappers are called first, regardless of extension load order by
4280 # Ensure mq wrappers are called first, regardless of extension load order by
4277 # NOT wrapping in uisetup() and instead deferring to init stage two here.
4281 # NOT wrapping in uisetup() and instead deferring to init stage two here.
4278 mqopt = [(b'', b'mq', None, _(b"operate on patch repository"))]
4282 mqopt = [(b'', b'mq', None, _(b"operate on patch repository"))]
4279
4283
4280 extensions.wrapcommand(commands.table, b'import', mqimport)
4284 extensions.wrapcommand(commands.table, b'import', mqimport)
4281 cmdutil.summaryhooks.add(b'mq', summaryhook)
4285 cmdutil.summaryhooks.add(b'mq', summaryhook)
4282
4286
4283 entry = extensions.wrapcommand(commands.table, b'init', mqinit)
4287 entry = extensions.wrapcommand(commands.table, b'init', mqinit)
4284 entry[1].extend(mqopt)
4288 entry[1].extend(mqopt)
4285
4289
4286 def dotable(cmdtable):
4290 def dotable(cmdtable):
4287 for cmd, entry in pycompat.iteritems(cmdtable):
4291 for cmd, entry in pycompat.iteritems(cmdtable):
4288 cmd = cmdutil.parsealiases(cmd)[0]
4292 cmd = cmdutil.parsealiases(cmd)[0]
4289 func = entry[0]
4293 func = entry[0]
4290 if func.norepo:
4294 if func.norepo:
4291 continue
4295 continue
4292 entry = extensions.wrapcommand(cmdtable, cmd, mqcommand)
4296 entry = extensions.wrapcommand(cmdtable, cmd, mqcommand)
4293 entry[1].extend(mqopt)
4297 entry[1].extend(mqopt)
4294
4298
4295 dotable(commands.table)
4299 dotable(commands.table)
4296
4300
4297 thismodule = sys.modules["hgext.mq"]
4301 thismodule = sys.modules["hgext.mq"]
4298 for extname, extmodule in extensions.extensions():
4302 for extname, extmodule in extensions.extensions():
4299 if extmodule != thismodule:
4303 if extmodule != thismodule:
4300 dotable(getattr(extmodule, 'cmdtable', {}))
4304 dotable(getattr(extmodule, 'cmdtable', {}))
4301
4305
4302
4306
4303 colortable = {
4307 colortable = {
4304 b'qguard.negative': b'red',
4308 b'qguard.negative': b'red',
4305 b'qguard.positive': b'yellow',
4309 b'qguard.positive': b'yellow',
4306 b'qguard.unguarded': b'green',
4310 b'qguard.unguarded': b'green',
4307 b'qseries.applied': b'blue bold underline',
4311 b'qseries.applied': b'blue bold underline',
4308 b'qseries.guarded': b'black bold',
4312 b'qseries.guarded': b'black bold',
4309 b'qseries.missing': b'red bold',
4313 b'qseries.missing': b'red bold',
4310 b'qseries.unapplied': b'black bold',
4314 b'qseries.unapplied': b'black bold',
4311 }
4315 }
General Comments 0
You need to be logged in to leave comments. Login now