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