mq.py
4318 lines
| 143.4 KiB
| text/x-python
|
PythonLexer
/ hgext / mq.py
|
r6187 | # mq.py - patch queues for mercurial | ||
|
r1808 | # | ||
|
r2859 | # Copyright 2005, 2006 Chris Mason <mason@suse.com> | ||
|
r1808 | # | ||
|
r8225 | # This software may be used and distributed according to the terms of the | ||
|
r10263 | # GNU General Public License version 2 or any later version. | ||
|
r1808 | |||
|
r8932 | '''manage a stack of patches | ||
|
r2554 | |||
This extension lets you work with a stack of patches in a Mercurial | ||||
|
r7983 | repository. It manages two stacks of patches - all known patches, and | ||
|
r2554 | applied patches (subset of known patches). | ||
Known patches are represented as patch files in the .hg/patches | ||||
|
r7983 | directory. Applied patches are both patch files and changesets. | ||
|
r2554 | |||
|
r30879 | Common tasks (use :hg:`help COMMAND` for more details):: | ||
|
r2554 | |||
|
r9157 | create new patch qnew | ||
import existing patch qimport | ||||
|
r2554 | |||
|
r9157 | print patch series qseries | ||
print applied patches qapplied | ||||
|
r2554 | |||
|
r9157 | add known patch to applied stack qpush | ||
remove patch from applied stack qpop | ||||
refresh contents of top applied patch qrefresh | ||||
|
r10190 | |||
By default, mq will automatically use git patches when required to | ||||
avoid losing file mode changes, copy records, binary files or empty | ||||
|
r26098 | files creations or deletions. This behavior can be configured with:: | ||
|
r10190 | |||
[mq] | ||||
git = auto/keep/yes/no | ||||
If set to 'keep', mq will obey the [diff] section configuration while | ||||
preserving existing git patches upon qrefresh. If set to 'yes' or | ||||
'no', mq will override the [diff] section and always generate git or | ||||
regular patches, possibly losing data in the second case. | ||||
|
r11234 | |||
|
r16040 | It may be desirable for mq changesets to be kept in the secret phase (see | ||
|
r16017 | :hg:`help phases`), which can be enabled with the following setting:: | ||
[mq] | ||||
secret = True | ||||
|
r11234 | You will by default be managing a patch queue named "patches". You can | ||
create other, independent patch queues with the :hg:`qqueue` command. | ||||
|
r16656 | |||
If the working directory contains uncommitted files, qpush, qpop and | ||||
qgoto abort immediately. If -f/--force is used, the changes are | ||||
|
r16733 | discarded. Setting:: | ||
|
r16656 | |||
[mq] | ||||
|
r16733 | keepchanges = True | ||
make them behave as if --keep-changes were passed, and non-conflicting | ||||
|
r16656 | local changes will be tolerated and preserved. If incompatible options | ||
such as -f/--force or --exact are passed, this setting is ignored. | ||||
|
r19826 | |||
This extension used to provide a strip command. This command now lives | ||||
in the strip extension. | ||||
|
r2554 | ''' | ||
|
r34139 | from __future__ import absolute_import, print_function | ||
|
r29127 | |||
import errno | ||||
import os | ||||
import re | ||||
import shutil | ||||
|
r44471 | import sys | ||
|
r3891 | from mercurial.i18n import _ | ||
|
r29127 | from mercurial.node import ( | ||
bin, | ||||
hex, | ||||
nullrev, | ||||
short, | ||||
) | ||||
|
r43359 | from mercurial.pycompat import ( | ||
|
r43360 | delattr, | ||
|
r43359 | getattr, | ||
open, | ||||
) | ||||
|
r29127 | from mercurial import ( | ||
cmdutil, | ||||
commands, | ||||
|
r30489 | dirstateguard, | ||
|
r34024 | encoding, | ||
|
r29127 | error, | ||
extensions, | ||||
hg, | ||||
localrepo, | ||||
lock as lockmod, | ||||
|
r35906 | logcmdutil, | ||
|
r29127 | patch as patchmod, | ||
phases, | ||||
|
r30519 | pycompat, | ||
|
r29127 | registrar, | ||
|
r31024 | revsetlang, | ||
|
r29127 | scmutil, | ||
|
r31023 | smartset, | ||
|
r46477 | strip, | ||
|
r36026 | subrepoutil, | ||
|
r29127 | util, | ||
|
r31243 | vfs as vfsmod, | ||
|
r29127 | ) | ||
|
r37102 | from mercurial.utils import ( | ||
dateutil, | ||||
stringutil, | ||||
r47669 | urlutil, | |||
|
r37102 | ) | ||
|
r29127 | |||
release = lockmod.release | ||||
|
r43347 | seriesopts = [(b's', b'summary', None, _(b'print first line of patch header'))] | ||
|
r14298 | |||
cmdtable = {} | ||||
|
r32337 | command = registrar.command(cmdtable) | ||
|
r29841 | # Note for extension authors: ONLY specify testedwith = 'ships-with-hg-core' for | ||
|
r25186 | # extensions which SHIP WITH MERCURIAL. Non-mainline extensions should | ||
# be specifying the version(s) of Mercurial they are tested with, or | ||||
# leave the attribute unspecified. | ||||
|
r43347 | testedwith = b'ships-with-hg-core' | ||
|
r14298 | |||
|
r34182 | configtable = {} | ||
configitem = registrar.configitem(configtable) | ||||
|
r43346 | configitem( | ||
|
r46554 | b'mq', | ||
b'git', | ||||
default=b'auto', | ||||
|
r34182 | ) | ||
|
r43346 | configitem( | ||
|
r46554 | b'mq', | ||
b'keepchanges', | ||||
default=False, | ||||
|
r34183 | ) | ||
|
r43346 | configitem( | ||
|
r46554 | b'mq', | ||
b'plain', | ||||
default=False, | ||||
|
r34184 | ) | ||
|
r43346 | configitem( | ||
|
r46554 | b'mq', | ||
b'secret', | ||||
default=False, | ||||
|
r34185 | ) | ||
|
r34182 | |||
|
r19951 | # force load strip extension formerly included in mq and import some utility | ||
|
r19822 | try: | ||
|
r46477 | extensions.find(b'strip') | ||
|
r19822 | except KeyError: | ||
# note: load is lazy so we could avoid the try-except, | ||||
|
r19951 | # but I (marmoute) prefer this explicit code. | ||
|
r19822 | class dummyui(object): | ||
def debug(self, msg): | ||||
pass | ||||
|
r43346 | |||
|
r41031 | def log(self, event, msgfmt, *msgargs, **opts): | ||
pass | ||||
|
r43346 | |||
|
r46477 | extensions.load(dummyui(), b'strip', b'') | ||
strip = strip.strip | ||||
|
r42692 | |||
|
r43346 | |||
|
r42692 | def checksubstate(repo, baserev=None): | ||
|
r46554 | """return list of subrepos at a different revision than substate. | ||
Abort if any subrepos have uncommitted changes.""" | ||||
|
r42692 | inclsubs = [] | ||
wctx = repo[None] | ||||
if baserev: | ||||
bctx = repo[baserev] | ||||
else: | ||||
bctx = wctx.p1() | ||||
for s in sorted(wctx.substate): | ||||
wctx.sub(s).bailifchanged(True) | ||||
if s not in bctx.substate or bctx.sub(s).dirty(): | ||||
inclsubs.append(s) | ||||
return inclsubs | ||||
|
r19823 | |||
|
r43346 | |||
|
r4037 | # Patch names looks like unix-file names. | ||
# They must be joinable with queue directory and result in the patch path. | ||||
normname = util.normpath | ||||
|
r43346 | |||
|
r8778 | class statusentry(object): | ||
|
r10682 | def __init__(self, node, name): | ||
self.node, self.name = node, name | ||||
|
r35860 | |||
def __bytes__(self): | ||||
|
r43347 | return hex(self.node) + b':' + self.name | ||
|
r2780 | |||
|
r35860 | __str__ = encoding.strmethod(__bytes__) | ||
__repr__ = encoding.strmethod(__bytes__) | ||||
|
r43346 | |||
|
r22546 | # The order of the headers in 'hg export' HG patches: | ||
HGHEADERS = [ | ||||
|
r43346 | # '# HG changeset patch', | ||
|
r43347 | b'# User ', | ||
b'# Date ', | ||||
b'# ', | ||||
b'# Branch ', | ||||
b'# Node ID ', | ||||
b'# Parent ', # can occur twice for merges - but that is not relevant for mq | ||||
|
r43346 | ] | ||
|
r23442 | # The order of headers in plain 'mail style' patches: | ||
PLAINHEADERS = { | ||||
|
r43347 | b'from': 0, | ||
b'date': 1, | ||||
b'subject': 2, | ||||
|
r43346 | } | ||
|
r22546 | |||
def inserthgheader(lines, header, value): | ||||
"""Assuming lines contains a HG patch header, add a header line with value. | ||||
|
r34133 | >>> try: inserthgheader([], b'# Date ', b'z') | ||
|
r34140 | ... except ValueError as inst: print("oops") | ||
|
r22546 | oops | ||
|
r34133 | >>> inserthgheader([b'# HG changeset patch'], b'# Date ', b'z') | ||
|
r22546 | ['# HG changeset patch', '# Date z'] | ||
|
r34133 | >>> inserthgheader([b'# HG changeset patch', b''], b'# Date ', b'z') | ||
|
r22546 | ['# HG changeset patch', '# Date z', ''] | ||
|
r34133 | >>> inserthgheader([b'# HG changeset patch', b'# User y'], b'# Date ', b'z') | ||
|
r22546 | ['# HG changeset patch', '# User y', '# Date z'] | ||
|
r34133 | >>> inserthgheader([b'# HG changeset patch', b'# Date x', b'# User y'], | ||
... b'# User ', b'z') | ||||
|
r23412 | ['# HG changeset patch', '# Date x', '# User z'] | ||
|
r34133 | >>> inserthgheader([b'# HG changeset patch', b'# Date y'], b'# Date ', b'z') | ||
|
r22546 | ['# HG changeset patch', '# Date z'] | ||
|
r34133 | >>> inserthgheader([b'# HG changeset patch', b'', b'# Date y'], | ||
... b'# Date ', b'z') | ||||
|
r22546 | ['# HG changeset patch', '# Date z', '', '# Date y'] | ||
|
r34133 | >>> inserthgheader([b'# HG changeset patch', b'# Parent y'], | ||
... b'# Date ', b'z') | ||||
|
r22546 | ['# HG changeset patch', '# Date z', '# Parent y'] | ||
""" | ||||
|
r43347 | start = lines.index(b'# HG changeset patch') + 1 | ||
|
r22546 | newindex = HGHEADERS.index(header) | ||
|
r23412 | bestpos = len(lines) | ||
|
r22546 | for i in range(start, len(lines)): | ||
line = lines[i] | ||||
|
r43347 | if not line.startswith(b'# '): | ||
|
r23412 | bestpos = min(bestpos, i) | ||
break | ||||
|
r22546 | for lineindex, h in enumerate(HGHEADERS): | ||
if line.startswith(h): | ||||
if lineindex == newindex: | ||||
lines[i] = header + value | ||||
|
r23412 | return lines | ||
if lineindex > newindex: | ||||
bestpos = min(bestpos, i) | ||||
|
r43346 | break # next line | ||
|
r23412 | lines.insert(bestpos, header + value) | ||
|
r22546 | return lines | ||
|
r43346 | |||
|
r23345 | def insertplainheader(lines, header, value): | ||
|
r23442 | """For lines containing a plain patch header, add a header line with value. | ||
|
r34133 | >>> insertplainheader([], b'Date', b'z') | ||
|
r23442 | ['Date: z'] | ||
|
r34133 | >>> insertplainheader([b''], b'Date', b'z') | ||
|
r23442 | ['Date: z', ''] | ||
|
r34133 | >>> insertplainheader([b'x'], b'Date', b'z') | ||
|
r23442 | ['Date: z', '', 'x'] | ||
|
r34133 | >>> insertplainheader([b'From: y', b'x'], b'Date', b'z') | ||
|
r23442 | ['From: y', 'Date: z', '', 'x'] | ||
|
r34133 | >>> insertplainheader([b' date : x', b' from : y', b''], b'From', b'z') | ||
|
r23442 | [' date : x', 'From: z', ''] | ||
|
r34133 | >>> insertplainheader([b'', b'Date: y'], b'Date', b'z') | ||
|
r23442 | ['Date: z', '', 'Date: y'] | ||
|
r34133 | >>> insertplainheader([b'foo: bar', b'DATE: z', b'x'], b'From', b'y') | ||
|
r23442 | ['From: y', 'foo: bar', 'DATE: z', '', 'x'] | ||
""" | ||||
newprio = PLAINHEADERS[header.lower()] | ||||
bestpos = len(lines) | ||||
for i, line in enumerate(lines): | ||||
|
r43347 | if b':' in line: | ||
lheader = line.split(b':', 1)[0].strip().lower() | ||||
|
r23442 | lprio = PLAINHEADERS.get(lheader, newprio + 1) | ||
if lprio == newprio: | ||||
|
r43347 | lines[i] = b'%s: %s' % (header, value) | ||
|
r23442 | return lines | ||
if lprio > newprio and i < bestpos: | ||||
bestpos = i | ||||
else: | ||||
if line: | ||||
|
r43347 | lines.insert(i, b'') | ||
|
r23442 | if i < bestpos: | ||
bestpos = i | ||||
break | ||||
|
r43347 | lines.insert(bestpos, b'%s: %s' % (header, value)) | ||
|
r23345 | return lines | ||
|
r43346 | |||
|
r7399 | class patchheader(object): | ||
|
r10397 | def __init__(self, pf, plainmode=False): | ||
|
r8653 | def eatdiff(lines): | ||
while lines: | ||||
l = lines[-1] | ||||
|
r43346 | if ( | ||
|
r43347 | l.startswith(b"diff -") | ||
or l.startswith(b"Index:") | ||||
or l.startswith(b"===========") | ||||
|
r43346 | ): | ||
|
r8653 | del lines[-1] | ||
else: | ||||
break | ||||
|
r43346 | |||
|
r8653 | def eatempty(lines): | ||
while lines: | ||||
|
r10688 | if not lines[-1].strip(): | ||
|
r8653 | del lines[-1] | ||
else: | ||||
break | ||||
message = [] | ||||
comments = [] | ||||
user = None | ||||
date = None | ||||
|
r10397 | parent = None | ||
|
r8653 | format = None | ||
subject = None | ||||
|
r13229 | branch = None | ||
nodeid = None | ||||
|
r8653 | diffstart = 0 | ||
|
r43347 | for line in open(pf, b'rb'): | ||
|
r8653 | line = line.rstrip() | ||
|
r43347 | if line.startswith(b'diff --git') or ( | ||
diffstart and line.startswith(b'+++ ') | ||||
|
r43346 | ): | ||
|
r8653 | diffstart = 2 | ||
break | ||||
|
r43346 | diffstart = 0 # reset | ||
|
r43347 | if line.startswith(b"--- "): | ||
|
r8653 | diffstart = 1 | ||
continue | ||||
|
r43347 | elif format == b"hgpatch": | ||
|
r8653 | # parse values when importing the result of an hg export | ||
|
r43347 | if line.startswith(b"# User "): | ||
|
r8653 | user = line[7:] | ||
|
r43347 | elif line.startswith(b"# Date "): | ||
|
r8653 | date = line[7:] | ||
|
r43347 | elif line.startswith(b"# Parent "): | ||
|
r43346 | parent = line[9:].lstrip() # handle double trailing space | ||
|
r43347 | elif line.startswith(b"# Branch "): | ||
|
r13229 | branch = line[9:] | ||
|
r43347 | elif line.startswith(b"# Node ID "): | ||
|
r13229 | nodeid = line[10:] | ||
|
r43347 | elif not line.startswith(b"# ") and line: | ||
|
r8653 | message.append(line) | ||
format = None | ||||
|
r43347 | elif line == b'# HG changeset patch': | ||
|
r9287 | message = [] | ||
|
r43347 | format = b"hgpatch" | ||
elif format != b"tagdone" and ( | ||||
line.startswith(b"Subject: ") or line.startswith(b"subject: ") | ||||
|
r43346 | ): | ||
|
r8653 | subject = line[9:] | ||
|
r43347 | format = b"tag" | ||
elif format != b"tagdone" and ( | ||||
line.startswith(b"From: ") or line.startswith(b"from: ") | ||||
|
r43346 | ): | ||
|
r8653 | user = line[6:] | ||
|
r43347 | format = b"tag" | ||
elif format != b"tagdone" and ( | ||||
line.startswith(b"Date: ") or line.startswith(b"date: ") | ||||
|
r43346 | ): | ||
|
r10397 | date = line[6:] | ||
|
r43347 | format = b"tag" | ||
elif format == b"tag" and line == b"": | ||||
|
r8653 | # when looking for tags (subject: from: etc) they | ||
# end once you find a blank line in the source | ||||
|
r43347 | format = b"tagdone" | ||
|
r8653 | elif message or line: | ||
message.append(line) | ||||
comments.append(line) | ||||
eatdiff(message) | ||||
eatdiff(comments) | ||||
|
r13229 | # Remember the exact starting line of the patch diffs before consuming | ||
# empty lines, for external use by TortoiseHg and others | ||||
self.diffstartline = len(comments) | ||||
|
r8653 | eatempty(message) | ||
eatempty(comments) | ||||
# make sure message isn't empty | ||||
|
r43347 | if format and format.startswith(b"tag") and subject: | ||
|
r8653 | message.insert(0, subject) | ||
|
r7399 | self.message = message | ||
self.comments = comments | ||||
self.user = user | ||||
self.date = date | ||||
|
r10397 | self.parent = parent | ||
|
r13229 | # nodeid and branch are for external use by TortoiseHg and others | ||
self.nodeid = nodeid | ||||
self.branch = branch | ||||
|
r8653 | self.haspatch = diffstart > 1 | ||
|
r43346 | self.plainmode = ( | ||
plainmode | ||||
|
r43347 | or b'# HG changeset patch' not in self.comments | ||
|
r43346 | and any( | ||
|
r43347 | c.startswith(b'Date: ') or c.startswith(b'From: ') | ||
|
r43346 | for c in self.comments | ||
) | ||||
) | ||||
|
r7399 | |||
def setuser(self, user): | ||||
|
r23443 | try: | ||
|
r43347 | inserthgheader(self.comments, b'# User ', user) | ||
|
r23443 | except ValueError: | ||
if self.plainmode: | ||||
|
r43347 | insertplainheader(self.comments, b'From', user) | ||
|
r23443 | else: | ||
|
r43347 | tmp = [b'# HG changeset patch', b'# User ' + user] | ||
|
r23443 | self.comments = tmp + self.comments | ||
|
r7399 | self.user = user | ||
def setdate(self, date): | ||||
|
r23443 | try: | ||
|
r43347 | inserthgheader(self.comments, b'# Date ', date) | ||
|
r23443 | except ValueError: | ||
if self.plainmode: | ||||
|
r43347 | insertplainheader(self.comments, b'Date', date) | ||
|
r23443 | else: | ||
|
r43347 | tmp = [b'# HG changeset patch', b'# Date ' + date] | ||
|
r23443 | self.comments = tmp + self.comments | ||
|
r9337 | self.date = date | ||
|
r7399 | |||
|
r10397 | def setparent(self, parent): | ||
|
r23443 | try: | ||
|
r43347 | inserthgheader(self.comments, b'# Parent ', parent) | ||
|
r23443 | except ValueError: | ||
if not self.plainmode: | ||||
|
r43347 | tmp = [b'# HG changeset patch', b'# Parent ' + parent] | ||
|
r23443 | self.comments = tmp + self.comments | ||
|
r10397 | self.parent = parent | ||
|
r7399 | def setmessage(self, message): | ||
if self.comments: | ||||
self._delmsg() | ||||
self.message = [message] | ||||
|
r23344 | if message: | ||
if self.plainmode and self.comments and self.comments[-1]: | ||||
|
r43347 | self.comments.append(b'') | ||
|
r23344 | self.comments.append(message) | ||
|
r7399 | |||
|
r35934 | def __bytes__(self): | ||
|
r43347 | s = b'\n'.join(self.comments).rstrip() | ||
|
r22522 | if not s: | ||
|
r43347 | return b'' | ||
return s + b'\n\n' | ||||
|
r7399 | |||
|
r35934 | __str__ = encoding.strmethod(__bytes__) | ||
|
r7399 | def _delmsg(self): | ||
|
r46554 | """Remove existing message, keeping the rest of the comments fields. | ||
|
r7399 | If comments contains 'subject: ', message will prepend | ||
|
r46554 | the field and a blank line.""" | ||
|
r7399 | if self.message: | ||
|
r43347 | subj = b'subject: ' + self.message[0].lower() | ||
|
r38806 | for i in pycompat.xrange(len(self.comments)): | ||
|
r7399 | if subj == self.comments[i].lower(): | ||
del self.comments[i] | ||||
self.message = self.message[2:] | ||||
break | ||||
ci = 0 | ||||
|
r8632 | for mi in self.message: | ||
while mi != self.comments[ci]: | ||||
|
r7399 | ci += 1 | ||
del self.comments[ci] | ||||
|
r43346 | |||
|
r16102 | def newcommit(repo, phase, *args, **kwargs): | ||
|
r16057 | """helper dedicated to ensure a commit respect mq.secret setting | ||
It should be used instead of repo.commit inside the mq source for operation | ||||
creating new changeset. | ||||
|
r15926 | """ | ||
|
r18010 | repo = repo.unfiltered() | ||
|
r16100 | if phase is None: | ||
|
r43347 | if repo.ui.configbool(b'mq', b'secret'): | ||
|
r16100 | phase = phases.secret | ||
|
r43347 | overrides = {(b'ui', b'allowemptycommit'): True} | ||
|
r16100 | if phase is not None: | ||
|
r43347 | overrides[(b'phases', b'new-commit')] = phase | ||
with repo.ui.configoverride(overrides, b'mq'): | ||||
repo.ui.setconfig(b'ui', b'allowemptycommit', True) | ||||
|
r15926 | return repo.commit(*args, **kwargs) | ||
|
r43346 | |||
|
r16654 | class AbortNoCleanup(error.Abort): | ||
pass | ||||
|
r43346 | |||
|
r8778 | class queue(object): | ||
|
r19064 | def __init__(self, ui, baseui, path, patchdir=None): | ||
|
r1808 | self.basepath = path | ||
|
r11229 | try: | ||
|
r43906 | with open(os.path.join(path, b'patches.queue'), 'rb') as fh: | ||
|
r36124 | cur = fh.read().rstrip() | ||
|
r11270 | if not cur: | ||
|
r43347 | curpath = os.path.join(path, b'patches') | ||
|
r11270 | else: | ||
|
r43347 | curpath = os.path.join(path, b'patches-' + cur) | ||
|
r11229 | except IOError: | ||
|
r43347 | curpath = os.path.join(path, b'patches') | ||
|
r11229 | self.path = patchdir or curpath | ||
|
r31243 | self.opener = vfsmod.vfs(self.path) | ||
|
r1808 | self.ui = ui | ||
|
r19064 | self.baseui = baseui | ||
|
r15879 | self.applieddirty = False | ||
self.seriesdirty = False | ||||
|
r11462 | self.added = [] | ||
|
r43347 | self.seriespath = b"series" | ||
self.statuspath = b"status" | ||||
self.guardspath = b"guards" | ||||
|
r14590 | self.activeguards = None | ||
|
r14591 | self.guardsdirty = False | ||
|
r10190 | # Handle mq.git as a bool with extended values | ||
|
r43347 | gitmode = ui.config(b'mq', b'git').lower() | ||
|
r37102 | boolmode = stringutil.parsebool(gitmode) | ||
|
r34182 | if boolmode is not None: | ||
if boolmode: | ||||
|
r43347 | gitmode = b'yes' | ||
|
r24306 | else: | ||
|
r43347 | gitmode = b'no' | ||
|
r34182 | self.gitmode = gitmode | ||
|
r25827 | # deprecated config: mq.plain | ||
|
r43347 | self.plainmode = ui.configbool(b'mq', b'plain') | ||
|
r19856 | self.checkapplied = True | ||
|
r1810 | |||
|
r8524 | @util.propertycache | ||
def applied(self): | ||||
|
r15258 | def parselines(lines): | ||
for l in lines: | ||||
|
r43347 | entry = l.split(b':', 1) | ||
|
r15258 | if len(entry) > 1: | ||
n, name = entry | ||||
yield statusentry(bin(n), name) | ||||
elif l.strip(): | ||||
|
r43346 | self.ui.warn( | ||
|
r43347 | _(b'malformated mq status line: %s\n') | ||
|
r43346 | % stringutil.pprint(entry) | ||
) | ||||
|
r15258 | # else we ignore empty lines | ||
|
r43346 | |||
|
r15258 | try: | ||
|
r14588 | lines = self.opener.read(self.statuspath).splitlines() | ||
|
r13507 | return list(parselines(lines)) | ||
|
r25660 | except IOError as e: | ||
|
r15258 | if e.errno == errno.ENOENT: | ||
return [] | ||||
raise | ||||
|
r8524 | |||
@util.propertycache | ||||
|
r14572 | def fullseries(self): | ||
|
r15258 | try: | ||
|
r15878 | return self.opener.read(self.seriespath).splitlines() | ||
|
r25660 | except IOError as e: | ||
|
r15258 | if e.errno == errno.ENOENT: | ||
return [] | ||||
raise | ||||
|
r8524 | |||
@util.propertycache | ||||
def series(self): | ||||
|
r14575 | self.parseseries() | ||
|
r8524 | return self.series | ||
@util.propertycache | ||||
|
r14573 | def seriesguards(self): | ||
|
r14575 | self.parseseries() | ||
|
r14573 | return self.seriesguards | ||
|
r1808 | |||
|
r8525 | def invalidate(self): | ||
|
r43744 | for a in 'applied fullseries series seriesguards'.split(): | ||
|
r8525 | if a in self.__dict__: | ||
delattr(self, a) | ||||
|
r15879 | self.applieddirty = False | ||
self.seriesdirty = False | ||||
|
r14591 | self.guardsdirty = False | ||
|
r14590 | self.activeguards = None | ||
|
r8525 | |||
|
r34092 | def diffopts(self, opts=None, patchfn=None, plain=False): | ||
"""Return diff options tweaked for this mq use, possibly upgrading to | ||||
git format, and possibly plain and without lossy options.""" | ||||
|
r43346 | diffopts = patchmod.difffeatureopts( | ||
self.ui, | ||||
opts, | ||||
git=True, | ||||
whitespace=not plain, | ||||
formatchanging=not plain, | ||||
) | ||||
|
r43347 | if self.gitmode == b'auto': | ||
|
r10190 | diffopts.upgrade = True | ||
|
r43347 | elif self.gitmode == b'keep': | ||
|
r10190 | pass | ||
|
r43347 | elif self.gitmode in (b'yes', b'no'): | ||
diffopts.git = self.gitmode == b'yes' | ||||
|
r10190 | else: | ||
|
r43346 | raise error.Abort( | ||
|
r43387 | _(b'mq.git option can be auto/keep/yes/no got %s') | ||
|
r43346 | % self.gitmode | ||
) | ||||
|
r10184 | if patchfn: | ||
|
r10185 | diffopts = self.patchopts(diffopts, patchfn) | ||
return diffopts | ||||
|
r10186 | def patchopts(self, diffopts, *patches): | ||
|
r10185 | """Return a copy of input diff options with git set to true if | ||
|
r10190 | referenced patch is a git patch and should be preserved as such. | ||
|
r10185 | """ | ||
diffopts = diffopts.copy() | ||||
|
r43347 | if not diffopts.git and self.gitmode == b'keep': | ||
|
r10190 | for patchfn in patches: | ||
|
r43347 | patchf = self.opener(patchfn, b'r') | ||
|
r10190 | # if the patch was a git patch, refresh it as a git patch | ||
|
r43346 | diffopts.git = any( | ||
|
r43347 | line.startswith(b'diff --git') for line in patchf | ||
|
r43346 | ) | ||
|
r10190 | patchf.close() | ||
|
r10184 | return diffopts | ||
|
r2874 | |||
|
r2819 | def join(self, *p): | ||
return os.path.join(self.path, *p) | ||||
|
r14574 | def findseries(self, patch): | ||
|
r10685 | def matchpatch(l): | ||
|
r43347 | l = l.split(b'#', 1)[0] | ||
|
r10685 | return l.strip() == patch | ||
|
r43346 | |||
|
r14572 | for index, l in enumerate(self.fullseries): | ||
|
r10685 | if matchpatch(l): | ||
return index | ||||
|
r1808 | return None | ||
|
r35145 | guard_re = re.compile(br'\s?#([-+][^-+# \t\r\n\f][^# \t\r\n\f]*)') | ||
|
r2821 | |||
|
r14575 | def parseseries(self): | ||
|
r1808 | self.series = [] | ||
|
r14573 | self.seriesguards = [] | ||
|
r14572 | for l in self.fullseries: | ||
|
r43347 | h = l.find(b'#') | ||
|
r2821 | if h == -1: | ||
patch = l | ||||
|
r43347 | comment = b'' | ||
|
r2821 | elif h == 0: | ||
continue | ||||
else: | ||||
patch = l[:h] | ||||
comment = l[h:] | ||||
patch = patch.strip() | ||||
if patch: | ||||
|
r3184 | if patch in self.series: | ||
|
r43346 | raise error.Abort( | ||
|
r43347 | _(b'%s appears more than once in %s') | ||
|
r43346 | % (patch, self.join(self.seriespath)) | ||
) | ||||
|
r2821 | self.series.append(patch) | ||
|
r14573 | self.seriesguards.append(self.guard_re.findall(comment)) | ||
|
r2821 | |||
|
r14576 | def checkguard(self, guard): | ||
|
r6607 | if not guard: | ||
|
r43347 | return _(b'guard cannot be an empty string') | ||
bad_chars = b'# \t\r\n\f' | ||||
|
r2821 | first = guard[0] | ||
|
r43347 | if first in b'-+': | ||
return _(b'guard %r starts with invalid character: %r') % ( | ||||
|
r43346 | guard, | ||
first, | ||||
) | ||||
|
r2821 | for c in bad_chars: | ||
if c in guard: | ||||
|
r43347 | return _(b'invalid character in guard %r: %r') % (guard, c) | ||
|
r3223 | |||
|
r14578 | def setactive(self, guards): | ||
|
r2821 | for guard in guards: | ||
|
r14576 | bad = self.checkguard(guard) | ||
|
r2821 | if bad: | ||
|
r26587 | raise error.Abort(bad) | ||
|
r8209 | guards = sorted(set(guards)) | ||
|
r43347 | self.ui.debug(b'active guards: %s\n' % b' '.join(guards)) | ||
|
r14590 | self.activeguards = guards | ||
|
r14591 | self.guardsdirty = True | ||
|
r2821 | |||
def active(self): | ||||
|
r14590 | if self.activeguards is None: | ||
self.activeguards = [] | ||||
|
r2821 | try: | ||
|
r14589 | guards = self.opener.read(self.guardspath).split() | ||
|
r25660 | except IOError as err: | ||
|
r10282 | if err.errno != errno.ENOENT: | ||
raise | ||||
|
r2821 | guards = [] | ||
for i, guard in enumerate(guards): | ||||
|
r14576 | bad = self.checkguard(guard) | ||
|
r2821 | if bad: | ||
|
r43346 | self.ui.warn( | ||
|
r43347 | b'%s:%d: %s\n' | ||
% (self.join(self.guardspath), i + 1, bad) | ||||
|
r43346 | ) | ||
|
r2821 | else: | ||
|
r14590 | self.activeguards.append(guard) | ||
return self.activeguards | ||||
|
r2821 | |||
|
r14577 | def setguards(self, idx, guards): | ||
|
r2821 | for g in guards: | ||
if len(g) < 2: | ||||
|
r43347 | raise error.Abort(_(b'guard %r too short') % g) | ||
if g[0] not in b'-+': | ||||
raise error.Abort(_(b'guard %r starts with invalid char') % g) | ||||
|
r14576 | bad = self.checkguard(g[1:]) | ||
|
r2821 | if bad: | ||
|
r26587 | raise error.Abort(bad) | ||
|
r43347 | drop = self.guard_re.sub(b'', self.fullseries[idx]) | ||
self.fullseries[idx] = drop + b''.join([b' #' + g for g in guards]) | ||||
|
r14575 | self.parseseries() | ||
|
r14593 | self.seriesdirty = True | ||
|
r3223 | |||
|
r2821 | def pushable(self, idx): | ||
|
r36123 | if isinstance(idx, bytes): | ||
|
r2821 | idx = self.series.index(idx) | ||
|
r14573 | patchguards = self.seriesguards[idx] | ||
|
r2821 | if not patchguards: | ||
return True, None | ||||
guards = self.active() | ||||
|
r43346 | exactneg = [ | ||
|
r43347 | g for g in patchguards if g.startswith(b'-') and g[1:] in guards | ||
|
r43346 | ] | ||
|
r2821 | if exactneg: | ||
|
r39084 | return False, stringutil.pprint(exactneg[0]) | ||
|
r43347 | pos = [g for g in patchguards if g.startswith(b'+')] | ||
|
r2850 | exactpos = [g for g in pos if g[1:] in guards] | ||
|
r2821 | if pos: | ||
|
r2850 | if exactpos: | ||
|
r39084 | return True, stringutil.pprint(exactpos[0]) | ||
|
r43347 | return False, b' '.join([stringutil.pprint(p) for p in pos]) | ||
return True, b'' | ||||
|
r2821 | |||
|
r14579 | def explainpushable(self, idx, all_patches=False): | ||
|
r24306 | if all_patches: | ||
write = self.ui.write | ||||
else: | ||||
write = self.ui.warn | ||||
|
r2821 | if all_patches or self.ui.verbose: | ||
|
r37539 | if isinstance(idx, bytes): | ||
|
r2821 | idx = self.series.index(idx) | ||
pushable, why = self.pushable(idx) | ||||
if all_patches and pushable: | ||||
if why is None: | ||||
|
r43346 | write( | ||
|
r43347 | _(b'allowing %s - no guards in effect\n') | ||
|
r43346 | % self.series[idx] | ||
) | ||||
|
r2821 | else: | ||
if not why: | ||||
|
r43346 | write( | ||
|
r43347 | _(b'allowing %s - no matching negative guards\n') | ||
|
r43346 | % self.series[idx] | ||
) | ||||
|
r2821 | else: | ||
|
r43346 | write( | ||
|
r43347 | _(b'allowing %s - guarded by %s\n') | ||
|
r43346 | % (self.series[idx], why) | ||
) | ||||
|
r2821 | if not pushable: | ||
|
r2829 | if why: | ||
|
r43346 | write( | ||
|
r43347 | _(b'skipping %s - guarded by %s\n') | ||
|
r43346 | % (self.series[idx], why) | ||
) | ||||
|
r2821 | else: | ||
|
r43346 | write( | ||
|
r43347 | _(b'skipping %s - no matching guards\n') | ||
|
r43346 | % self.series[idx] | ||
) | ||||
|
r1810 | |||
|
r14580 | def savedirty(self): | ||
|
r14594 | def writelist(items, path): | ||
|
r43347 | fp = self.opener(path, b'wb') | ||
|
r2772 | for i in items: | ||
|
r43347 | fp.write(b"%s\n" % i) | ||
|
r2772 | fp.close() | ||
|
r43346 | |||
|
r14592 | if self.applieddirty: | ||
|
r35862 | writelist(map(bytes, self.applied), self.statuspath) | ||
|
r15883 | self.applieddirty = False | ||
|
r14593 | if self.seriesdirty: | ||
|
r14594 | writelist(self.fullseries, self.seriespath) | ||
|
r15883 | self.seriesdirty = False | ||
|
r14591 | if self.guardsdirty: | ||
|
r14594 | writelist(self.activeguards, self.guardspath) | ||
|
r15883 | self.guardsdirty = False | ||
|
r11546 | if self.added: | ||
qrepo = self.qrepo() | ||||
if qrepo: | ||||
|
r12658 | qrepo[None].add(f for f in self.added if f not in qrepo[None]) | ||
|
r11546 | self.added = [] | ||
|
r1808 | |||
|
r4207 | def removeundo(self, repo): | ||
|
r43347 | undo = repo.sjoin(b'undo') | ||
|
r4207 | if not os.path.exists(undo): | ||
return | ||||
try: | ||||
os.unlink(undo) | ||||
|
r25660 | except OSError as inst: | ||
|
r43346 | self.ui.warn( | ||
|
r43347 | _(b'error removing undo: %s\n') % stringutil.forcebytestr(inst) | ||
|
r43346 | ) | ||
|
r4207 | |||
|
r16634 | def backup(self, repo, files, copy=False): | ||
|
r16633 | # backup local changes in --force case | ||
for f in sorted(files): | ||||
absf = repo.wjoin(f) | ||||
if os.path.lexists(absf): | ||||
|
r41748 | absorig = scmutil.backuppath(self.ui, repo, f) | ||
|
r43346 | self.ui.note( | ||
|
r43347 | _(b'saving current version of %s as %s\n') | ||
|
r43346 | % (f, os.path.relpath(absorig)) | ||
) | ||||
|
r41732 | |||
|
r16634 | if copy: | ||
|
r26943 | util.copyfile(absf, absorig) | ||
|
r16634 | else: | ||
|
r26943 | util.rename(absf, absorig) | ||
|
r16633 | |||
|
r43346 | def printdiff( | ||
self, | ||||
repo, | ||||
diffopts, | ||||
node1, | ||||
node2=None, | ||||
files=None, | ||||
fp=None, | ||||
changes=None, | ||||
opts=None, | ||||
): | ||||
|
r31432 | if opts is None: | ||
opts = {} | ||||
|
r43347 | stat = opts.get(b'stat') | ||
|
r14671 | m = scmutil.match(repo[node1], files, opts) | ||
|
r43346 | logcmdutil.diffordiffstat( | ||
|
r45340 | self.ui, | ||
repo, | ||||
diffopts, | ||||
repo[node1], | ||||
repo[node2], | ||||
m, | ||||
changes, | ||||
stat, | ||||
fp, | ||||
|
r43346 | ) | ||
|
r2874 | |||
|
r10184 | def mergeone(self, repo, mergeq, head, patch, rev, diffopts): | ||
|
r1808 | # first try just applying the patch | ||
|
r43346 | (err, n) = self.apply( | ||
repo, [patch], update_status=False, strict=True, merge=rev | ||||
) | ||||
|
r1808 | |||
if err == 0: | ||||
return (err, n) | ||||
if n is None: | ||||
|
r43347 | raise error.Abort(_(b"apply failed for patch %s") % patch) | ||
self.ui.warn(_(b"patch didn't work out, merging %s\n") % patch) | ||||
|
r1808 | |||
# apply failed, strip away that rev and merge. | ||||
|
r4917 | hg.clean(repo, head) | ||
|
r22057 | strip(self.ui, repo, [n], update=False, backup=False) | ||
|
r1808 | |||
|
r6747 | ctx = repo[rev] | ||
|
r44917 | ret = hg.merge(ctx, remind=False) | ||
|
r1808 | if ret: | ||
|
r43347 | raise error.Abort(_(b"update returned %d") % ret) | ||
|
r16102 | n = newcommit(repo, None, ctx.description(), ctx.user(), force=True) | ||
|
r8527 | if n is None: | ||
|
r43347 | raise error.Abort(_(b"repo commit failed")) | ||
|
r1808 | try: | ||
|
r10397 | ph = patchheader(mergeq.join(patch), self.plainmode) | ||
|
r16689 | except Exception: | ||
|
r43347 | raise error.Abort(_(b"unable to read %s") % patch) | ||
|
r1808 | |||
|
r10185 | diffopts = self.patchopts(diffopts, patch) | ||
|
r43347 | patchf = self.opener(patch, b"w") | ||
|
r36685 | comments = bytes(ph) | ||
|
r1808 | if comments: | ||
patchf.write(comments) | ||||
|
r10184 | self.printdiff(repo, diffopts, head, n, fp=patchf) | ||
|
r1808 | patchf.close() | ||
|
r4207 | self.removeundo(repo) | ||
|
r1808 | return (0, n) | ||
|
r1810 | |||
|
r1808 | def qparents(self, repo, rev=None): | ||
|
r19816 | """return the mq handled parent or p1 | ||
In some case where mq get himself in being the parent of a merge the | ||||
|
r19951 | appropriate parent may be p2. | ||
|
r19816 | (eg: an in progress merge started with mq disabled) | ||
If no parent are managed by mq, p1 is returned. | ||||
""" | ||||
|
r1808 | if rev is None: | ||
(p1, p2) = repo.dirstate.parents() | ||||
|
r47771 | if p2 == repo.nullid: | ||
|
r1808 | return p1 | ||
|
r10686 | if not self.applied: | ||
|
r1808 | return None | ||
|
r10678 | return self.applied[-1].node | ||
p1, p2 = repo.changelog.parents(rev) | ||||
|
r47771 | if p2 != repo.nullid and p2 in [x.node for x in self.applied]: | ||
|
r10680 | return p2 | ||
|
r10678 | return p1 | ||
|
r1808 | |||
|
r10184 | def mergepatch(self, repo, mergeq, series, diffopts): | ||
|
r10686 | if not self.applied: | ||
|
r1808 | # each of the patches merged in will have two parents. This | ||
# can confuse the qrefresh, qdiff, and strip code because it | ||||
# needs to know which parent is actually in the patch queue. | ||||
# so, we insert a merge marker with only one parent. This way | ||||
# the first patch in the queue is never a merge patch | ||||
# | ||||
|
r43347 | pname = b".hg.patches.merge.marker" | ||
n = newcommit(repo, None, b'[mq]: merge marker', force=True) | ||||
|
r4207 | self.removeundo(repo) | ||
|
r10678 | self.applied.append(statusentry(n, pname)) | ||
|
r15879 | self.applieddirty = True | ||
|
r1808 | |||
head = self.qparents(repo) | ||||
for patch in series: | ||||
|
r2696 | patch = mergeq.lookup(patch, strict=True) | ||
|
r1808 | if not patch: | ||
|
r43347 | self.ui.warn(_(b"patch %s does not exist\n") % patch) | ||
|
r1808 | return (1, None) | ||
|
r2821 | pushable, reason = self.pushable(patch) | ||
if not pushable: | ||||
|
r14579 | self.explainpushable(patch, all_patches=True) | ||
|
r2821 | continue | ||
|
r1808 | info = mergeq.isapplied(patch) | ||
if not info: | ||||
|
r43347 | self.ui.warn(_(b"patch %s is not applied\n") % patch) | ||
|
r1808 | return (1, None) | ||
|
r10678 | rev = info[1] | ||
|
r10184 | err, head = self.mergeone(repo, mergeq, head, patch, rev, diffopts) | ||
|
r1808 | if head: | ||
|
r10678 | self.applied.append(statusentry(head, patch)) | ||
|
r15879 | self.applieddirty = True | ||
|
r1808 | if err: | ||
return (err, head) | ||||
|
r14580 | self.savedirty() | ||
|
r1808 | return (0, head) | ||
|
r2748 | def patch(self, repo, patchfile): | ||
|
r46554 | """Apply patchfile to the working directory. | ||
patchfile: name of patch file""" | ||||
|
r14564 | files = set() | ||
|
r2748 | try: | ||
|
r43346 | fuzz = patchmod.patch( | ||
self.ui, repo, patchfile, strip=1, files=files, eolmode=None | ||||
) | ||||
|
r14260 | return (True, list(files), fuzz) | ||
|
r25660 | except Exception as inst: | ||
|
r43347 | self.ui.note(stringutil.forcebytestr(inst) + b'\n') | ||
|
r2919 | if not self.ui.verbose: | ||
|
r43347 | self.ui.warn(_(b"patch failed, unable to continue (try -v)\n")) | ||
|
r15085 | self.ui.traceback() | ||
|
r14260 | return (False, list(files), False) | ||
|
r2796 | |||
|
r43346 | def apply( | ||
self, | ||||
repo, | ||||
series, | ||||
list=False, | ||||
update_status=True, | ||||
strict=False, | ||||
patchdir=None, | ||||
merge=None, | ||||
all_files=None, | ||||
tobackup=None, | ||||
keepchanges=False, | ||||
): | ||||
|
r26578 | wlock = lock = tr = None | ||
|
r4418 | try: | ||
|
r4917 | wlock = repo.wlock() | ||
|
r4915 | lock = repo.lock() | ||
|
r43347 | tr = repo.transaction(b"qpush") | ||
|
r4418 | try: | ||
|
r43346 | ret = self._apply( | ||
repo, | ||||
series, | ||||
list, | ||||
update_status, | ||||
strict, | ||||
patchdir, | ||||
merge, | ||||
all_files=all_files, | ||||
tobackup=tobackup, | ||||
keepchanges=keepchanges, | ||||
) | ||||
|
r4915 | tr.close() | ||
|
r14580 | self.savedirty() | ||
|
r4915 | return ret | ||
|
r16654 | except AbortNoCleanup: | ||
tr.close() | ||||
self.savedirty() | ||||
|
r24826 | raise | ||
|
r43346 | except: # re-raises | ||
|
r4915 | try: | ||
tr.abort() | ||||
finally: | ||||
|
r15881 | self.invalidate() | ||
|
r4915 | raise | ||
finally: | ||||
|
r26578 | release(tr, lock, wlock) | ||
|
r5527 | self.removeundo(repo) | ||
|
r4418 | |||
|
r43346 | def _apply( | ||
self, | ||||
repo, | ||||
series, | ||||
list=False, | ||||
update_status=True, | ||||
strict=False, | ||||
patchdir=None, | ||||
merge=None, | ||||
all_files=None, | ||||
tobackup=None, | ||||
keepchanges=False, | ||||
): | ||||
|
r16634 | """returns (error, hash) | ||
error = 1 for unable to read, 2 for patch failed, 3 for patch | ||||
fuzz. tobackup is None or a set of files to backup before they | ||||
are modified by a patch. | ||||
""" | ||||
|
r1808 | # TODO unify with commands.py | ||
if not patchdir: | ||||
patchdir = self.path | ||||
err = 0 | ||||
n = None | ||||
|
r2934 | for patchname in series: | ||
pushable, reason = self.pushable(patchname) | ||||
|
r2821 | if not pushable: | ||
|
r14579 | self.explainpushable(patchname, all_patches=True) | ||
|
r2821 | continue | ||
|
r43347 | self.ui.status(_(b"applying %s\n") % patchname) | ||
|
r2934 | pf = os.path.join(patchdir, patchname) | ||
|
r1808 | |||
try: | ||||
|
r10397 | ph = patchheader(self.join(patchname), self.plainmode) | ||
|
r14239 | except IOError: | ||
|
r43347 | self.ui.warn(_(b"unable to read %s\n") % patchname) | ||
|
r1808 | err = 1 | ||
break | ||||
|
r7399 | message = ph.message | ||
|
r1808 | if not message: | ||
|
r12849 | # The commit message should not be translated | ||
|
r43347 | message = b"imported patch %s\n" % patchname | ||
|
r1808 | else: | ||
if list: | ||||
|
r12849 | # The commit message should not be translated | ||
|
r43347 | message.append(b"\nimported patch %s" % patchname) | ||
message = b'\n'.join(message) | ||||
|
r1808 | |||
|
r7782 | if ph.haspatch: | ||
|
r16634 | if tobackup: | ||
touched = patchmod.changedfiles(self.ui, repo, pf) | ||||
touched = set(touched) & tobackup | ||||
|
r16733 | if touched and keepchanges: | ||
|
r16654 | raise AbortNoCleanup( | ||
|
r43347 | _(b"conflicting local changes found"), | ||
hint=_(b"did you forget to qrefresh?"), | ||||
|
r43346 | ) | ||
|
r16634 | self.backup(repo, touched, copy=True) | ||
tobackup = tobackup - touched | ||||
|
r7782 | (patcherr, files, fuzz) = self.patch(repo, pf) | ||
|
r10661 | if all_files is not None: | ||
all_files.update(files) | ||||
|
r7782 | patcherr = not patcherr | ||
else: | ||||
|
r43347 | self.ui.warn(_(b"patch %s is empty\n") % patchname) | ||
|
r7782 | patcherr, files, fuzz = 0, [], 0 | ||
|
r1808 | |||
|
r2934 | if merge and files: | ||
|
r4332 | # Mark as removed/merged and update dirstate parent info | ||
removed = [] | ||||
merged = [] | ||||
for f in files: | ||||
|
r12344 | if os.path.lexists(repo.wjoin(f)): | ||
|
r4332 | merged.append(f) | ||
else: | ||||
removed.append(f) | ||||
|
r32347 | with repo.dirstate.parentchange(): | ||
for f in removed: | ||||
r48497 | repo.dirstate.update_file_p1(f, p1_tracked=True) | |||
|
r32347 | for f in merged: | ||
repo.dirstate.merge(f) | ||||
|
r41444 | p1 = repo.dirstate.p1() | ||
|
r32347 | repo.setparents(p1, merge) | ||
|
r6603 | |||
|
r43347 | if all_files and b'.hgsubstate' in all_files: | ||
|
r20959 | wctx = repo[None] | ||
|
r43347 | pctx = repo[b'.'] | ||
|
r19638 | overwrite = False | ||
|
r43346 | mergedsubstate = subrepoutil.submerge( | ||
repo, pctx, wctx, wctx, overwrite | ||||
) | ||||
|
r19638 | files += mergedsubstate.keys() | ||
|
r14322 | match = scmutil.matchfiles(repo, files or []) | ||
|
r39931 | oldtip = repo.changelog.tip() | ||
|
r43346 | n = newcommit( | ||
repo, None, message, ph.user, ph.date, match=match, force=True | ||||
) | ||||
|
r39931 | if repo.changelog.tip() == oldtip: | ||
|
r43347 | raise error.Abort( | ||
_(b"qpush exactly duplicates child changeset") | ||||
) | ||||
|
r8527 | if n is None: | ||
|
r43347 | raise error.Abort(_(b"repository commit failed")) | ||
|
r1808 | |||
if update_status: | ||||
|
r10678 | self.applied.append(statusentry(n, patchname)) | ||
|
r1808 | |||
if patcherr: | ||||
|
r43346 | self.ui.warn( | ||
|
r43387 | _(b"patch failed, rejects left in working directory\n") | ||
|
r43346 | ) | ||
|
r8875 | err = 2 | ||
|
r1808 | break | ||
if fuzz and strict: | ||||
|
r43347 | self.ui.warn(_(b"fuzz found when applying patch, stopping\n")) | ||
|
r8875 | err = 3 | ||
|
r1808 | break | ||
return (err, n) | ||||
|
r8833 | def _cleanup(self, patches, numrevs, keep=False): | ||
if not keep: | ||||
r = self.qrepo() | ||||
if r: | ||||
|
r14435 | r[None].forget(patches) | ||
for p in patches: | ||||
|
r18067 | try: | ||
os.unlink(self.join(p)) | ||||
|
r25660 | except OSError as inst: | ||
|
r18067 | if inst.errno != errno.ENOENT: | ||
raise | ||||
|
r8833 | |||
|
r15920 | qfinished = [] | ||
|
r8833 | if numrevs: | ||
|
r14010 | qfinished = self.applied[:numrevs] | ||
|
r8833 | del self.applied[:numrevs] | ||
|
r15879 | self.applieddirty = True | ||
|
r8833 | |||
|
r14010 | unknown = [] | ||
|
r37543 | sortedseries = [] | ||
for p in patches: | ||||
idx = self.findseries(p) | ||||
if idx is None: | ||||
sortedseries.append((-1, p)) | ||||
else: | ||||
sortedseries.append((idx, p)) | ||||
sortedseries.sort(reverse=True) | ||||
for (i, p) in sortedseries: | ||||
if i != -1: | ||||
|
r14572 | del self.fullseries[i] | ||
|
r14010 | else: | ||
unknown.append(p) | ||||
if unknown: | ||||
if numrevs: | ||||
|
r44937 | rev = {entry.name: entry.node for entry in qfinished} | ||
|
r14010 | for p in unknown: | ||
|
r43347 | msg = _(b'revision %s refers to unknown patches: %s\n') | ||
|
r14010 | self.ui.warn(msg % (short(rev[p]), p)) | ||
else: | ||||
|
r43347 | msg = _(b'unknown patches: %s\n') | ||
raise error.Abort(b''.join(msg % p for p in unknown)) | ||||
|
r14010 | |||
|
r14575 | self.parseseries() | ||
|
r15879 | self.seriesdirty = True | ||
|
r15920 | return [entry.node for entry in qfinished] | ||
|
r6645 | |||
|
r8833 | def _revpatches(self, repo, revs): | ||
|
r10678 | firstrev = repo[self.applied[0].node].rev() | ||
|
r6645 | patches = [] | ||
|
r8833 | for i, rev in enumerate(revs): | ||
|
r8832 | |||
|
r6645 | if rev < firstrev: | ||
|
r43347 | raise error.Abort(_(b'revision %d is not managed') % rev) | ||
|
r8832 | |||
ctx = repo[rev] | ||||
|
r10678 | base = self.applied[i].node | ||
|
r8832 | if ctx.node() != base: | ||
|
r43347 | msg = _(b'cannot delete revision %d above applied patches') | ||
|
r26587 | raise error.Abort(msg % rev) | ||
|
r8832 | |||
|
r8833 | patch = self.applied[i].name | ||
|
r43347 | for fmt in (b'[mq]: %s', b'imported patch %s'): | ||
|
r8832 | if ctx.description() == fmt % patch: | ||
|
r43347 | msg = _(b'patch %s finalized without changeset message\n') | ||
|
r8832 | repo.ui.status(msg % patch) | ||
break | ||||
|
r8833 | patches.append(patch) | ||
return patches | ||||
|
r6645 | |||
|
r8833 | def finish(self, repo, revs): | ||
|
r16029 | # Manually trigger phase computation to ensure phasedefaults is | ||
# executed before we remove the patches. | ||||
|
r16657 | repo._phasecache | ||
|
r8833 | patches = self._revpatches(repo, sorted(revs)) | ||
|
r15920 | qfinished = self._cleanup(patches, len(patches)) | ||
|
r43347 | if qfinished and repo.ui.configbool(b'mq', b'secret'): | ||
|
r16029 | # only use this logic when the secret option is added | ||
|
r15920 | oldqbase = repo[qfinished[0]] | ||
|
r34563 | tphase = phases.newcommitphase(repo.ui) | ||
|
r16290 | if oldqbase.phase() > tphase and oldqbase.p1().phase() <= tphase: | ||
|
r43347 | with repo.transaction(b'qfinish') as tr: | ||
|
r22069 | phases.advanceboundary(repo, tr, tphase, qfinished) | ||
|
r6645 | |||
|
r3088 | def delete(self, repo, patches, opts): | ||
|
r43347 | if not patches and not opts.get(b'rev'): | ||
|
r43346 | raise error.Abort( | ||
|
r43387 | _(b'qdelete requires at least one revision or patch name') | ||
|
r43346 | ) | ||
|
r4736 | |||
|
r11365 | realpatches = [] | ||
|
r2905 | for patch in patches: | ||
patch = self.lookup(patch, strict=True) | ||||
info = self.isapplied(patch) | ||||
|
r3373 | if info: | ||
|
r43347 | raise error.Abort(_(b"cannot delete applied patch %s") % patch) | ||
|
r2905 | if patch not in self.series: | ||
|
r43347 | raise error.Abort(_(b"patch %s not in series file") % patch) | ||
|
r12655 | if patch not in realpatches: | ||
realpatches.append(patch) | ||||
|
r3373 | |||
|
r8833 | numrevs = 0 | ||
|
r43347 | if opts.get(b'rev'): | ||
|
r3373 | if not self.applied: | ||
|
r43347 | raise error.Abort(_(b'no patches applied')) | ||
revs = scmutil.revrange(repo, opts.get(b'rev')) | ||||
|
r22803 | revs.sort() | ||
|
r8833 | revpatches = self._revpatches(repo, revs) | ||
|
r11365 | realpatches += revpatches | ||
|
r8833 | numrevs = len(revpatches) | ||
|
r2905 | |||
|
r43347 | self._cleanup(realpatches, numrevs, opts.get(b'keep')) | ||
|
r1810 | |||
|
r14581 | def checktoppatch(self, repo): | ||
|
r18343 | '''check that working directory is at qtip''' | ||
|
r10686 | if self.applied: | ||
|
r10678 | top = self.applied[-1].node | ||
|
r10191 | patch = self.applied[-1].name | ||
|
r18343 | if repo.dirstate.p1() != top: | ||
|
r43347 | raise error.Abort(_(b"working directory revision is not qtip")) | ||
|
r10191 | return top, patch | ||
return None, None | ||||
|
r17152 | def putsubstate2changes(self, substatestate, changes): | ||
|
r44039 | if isinstance(changes, list): | ||
mar = changes[:3] | ||||
else: | ||||
mar = (changes.modified, changes.added, changes.removed) | ||||
if any((b'.hgsubstate' in files for files in mar)): | ||||
return # already listed up | ||||
|
r17152 | # not yet listed up | ||
|
r43347 | if substatestate in b'a?': | ||
|
r44039 | mar[1].append(b'.hgsubstate') | ||
|
r43347 | elif substatestate in b'r': | ||
|
r44039 | mar[2].append(b'.hgsubstate') | ||
|
r43346 | else: # modified | ||
|
r44039 | mar[0].append(b'.hgsubstate') | ||
|
r17152 | |||
|
r19812 | def checklocalchanges(self, repo, force=False, refresh=True): | ||
|
r43347 | excsuffix = b'' | ||
|
r14256 | if refresh: | ||
|
r43347 | excsuffix = b', qrefresh first' | ||
|
r19812 | # plain versions for i18n tool to detect them | ||
|
r43347 | _(b"local changes found, qrefresh first") | ||
_(b"local changed subrepos found, qrefresh first") | ||||
|
r42689 | |||
s = repo.status() | ||||
if not force: | ||||
|
r42800 | cmdutil.checkunfinished(repo) | ||
|
r42689 | if s.modified or s.added or s.removed or s.deleted: | ||
|
r43347 | _(b"local changes found") # i18n tool detection | ||
raise error.Abort(_(b"local changes found" + excsuffix)) | ||||
|
r42689 | if checksubstate(repo): | ||
|
r43347 | _(b"local changed subrepos found") # i18n tool detection | ||
raise error.Abort( | ||||
_(b"local changed subrepos found" + excsuffix) | ||||
) | ||||
|
r42800 | else: | ||
cmdutil.checkunfinished(repo, skipmerge=True) | ||||
|
r42689 | return s | ||
|
r4713 | |||
|
r43347 | _reserved = (b'series', b'status', b'guards', b'.', b'..') | ||
|
r43346 | |||
|
r14584 | def checkreservedname(self, name): | ||
|
r14054 | if name in self._reserved: | ||
|
r43346 | raise error.Abort( | ||
|
r43347 | _(b'"%s" cannot be used as the name of a patch') % name | ||
|
r43346 | ) | ||
|
r31556 | if name != name.strip(): | ||
# whitespace is stripped by parseseries() | ||||
|
r43346 | raise error.Abort( | ||
|
r43387 | _(b'patch name cannot begin or end with whitespace') | ||
|
r43346 | ) | ||
|
r43347 | for prefix in (b'.hg', b'.mq'): | ||
|
r14054 | if name.startswith(prefix): | ||
|
r43346 | raise error.Abort( | ||
|
r43347 | _(b'patch name cannot begin with "%s"') % prefix | ||
|
r43346 | ) | ||
|
r43347 | for c in (b'#', b':', b'\r', b'\n'): | ||
|
r14054 | if c in name: | ||
|
r43346 | raise error.Abort( | ||
|
r43347 | _(b'%r cannot be used in the name of a patch') | ||
|
r43346 | % pycompat.bytestr(c) | ||
) | ||||
|
r14054 | |||
|
r14422 | def checkpatchname(self, name, force=False): | ||
|
r14584 | self.checkreservedname(name) | ||
|
r14422 | if not force and os.path.exists(self.join(name)): | ||
if os.path.isdir(self.join(name)): | ||||
|
r43346 | raise error.Abort( | ||
|
r43347 | _(b'"%s" already exists as a directory') % name | ||
|
r43346 | ) | ||
|
r14422 | else: | ||
|
r43347 | raise error.Abort(_(b'patch "%s" already exists') % name) | ||
|
r5981 | |||
|
r27918 | def makepatchname(self, title, fallbackname): | ||
"""Return a suitable filename for title, adding a suffix to make | ||||
it unique in the existing list""" | ||||
|
r41673 | namebase = re.sub(br'[\s\W_]+', b'_', title.lower()).strip(b'_') | ||
|
r43346 | namebase = namebase[:75] # avoid too long name (issue5117) | ||
|
r27919 | if namebase: | ||
try: | ||||
self.checkreservedname(namebase) | ||||
except error.Abort: | ||||
namebase = fallbackname | ||||
else: | ||||
|
r27918 | namebase = fallbackname | ||
name = namebase | ||||
i = 0 | ||||
|
r27919 | while True: | ||
if name not in self.fullseries: | ||||
try: | ||||
self.checkpatchname(name) | ||||
break | ||||
except error.Abort: | ||||
pass | ||||
|
r27918 | i += 1 | ||
|
r43347 | name = b'%s__%d' % (namebase, i) | ||
|
r27918 | return name | ||
|
r16733 | def checkkeepchanges(self, keepchanges, force): | ||
if force and keepchanges: | ||||
|
r43347 | raise error.Abort(_(b'cannot use both --force and --keep-changes')) | ||
|
r16654 | |||
|
r7162 | def new(self, repo, patchfn, *pats, **opts): | ||
|
r7157 | """options: | ||
|
r46554 | msg: a string or a no-argument function returning a string | ||
|
r7157 | """ | ||
|
r36405 | opts = pycompat.byteskwargs(opts) | ||
|
r43347 | msg = opts.get(b'msg') | ||
edit = opts.get(b'edit') | ||||
editform = opts.get(b'editform', b'mq.qnew') | ||||
user = opts.get(b'user') | ||||
date = opts.get(b'date') | ||||
|
r6139 | if date: | ||
|
r36625 | date = dateutil.parsedate(date) | ||
|
r43347 | diffopts = self.diffopts({b'git': opts.get(b'git')}, plain=True) | ||
if opts.get(b'checkname', True): | ||||
|
r14424 | self.checkpatchname(patchfn) | ||
|
r19813 | inclsubs = checksubstate(repo) | ||
|
r13174 | if inclsubs: | ||
|
r43347 | substatestate = repo.dirstate[b'.hgsubstate'] | ||
if opts.get(b'include') or opts.get(b'exclude') or pats: | ||||
|
r7161 | # detect missing files in pats | ||
def badfn(f, msg): | ||||
|
r43347 | if f != b'.hgsubstate': # .hgsubstate is auto-created | ||
raise error.Abort(b'%s: %s' % (f, msg)) | ||||
|
r43346 | |||
|
r25469 | match = scmutil.match(repo[None], pats, opts, badfn=badfn) | ||
|
r16366 | changes = repo.status(match=match) | ||
|
r4713 | else: | ||
|
r16366 | changes = self.checklocalchanges(repo, force=True) | ||
|
r20786 | commitfiles = list(inclsubs) | ||
|
r44039 | commitfiles.extend(changes.modified) | ||
commitfiles.extend(changes.added) | ||||
commitfiles.extend(changes.removed) | ||||
|
r20786 | match = scmutil.matchfiles(repo, commitfiles) | ||
|
r10372 | if len(repo[None].parents()) > 1: | ||
|
r43347 | raise error.Abort(_(b'cannot manage merge changesets')) | ||
|
r14581 | self.checktoppatch(repo) | ||
|
r14585 | insert = self.fullseriesend() | ||
|
r27827 | with repo.wlock(): | ||
|
r12878 | try: | ||
# if patch file write fails, abort early | ||||
|
r43347 | p = self.opener(patchfn, b"w") | ||
|
r25660 | except IOError as e: | ||
|
r43346 | raise error.Abort( | ||
|
r43347 | _(b'cannot write patch "%s": %s') | ||
|
r43346 | % (patchfn, encoding.strtolocal(e.strerror)) | ||
) | ||||
|
r7162 | try: | ||
|
r43347 | defaultmsg = b"[mq]: %s" % patchfn | ||
|
r22003 | editor = cmdutil.getcommiteditor(editform=editform) | ||
|
r21420 | if edit: | ||
|
r43346 | |||
|
r21421 | def finishdesc(desc): | ||
|
r21234 | if desc.rstrip(): | ||
return desc | ||||
else: | ||||
return defaultmsg | ||||
|
r43346 | |||
|
r21421 | # i18n: this message is shown in editor with "HG: " prefix | ||
|
r43347 | extramsg = _(b'Leave message empty to use default message.') | ||
|
r43346 | editor = cmdutil.getcommiteditor( | ||
finishdesc=finishdesc, | ||||
extramsg=extramsg, | ||||
editform=editform, | ||||
) | ||||
|
r21234 | commitmsg = msg | ||
else: | ||||
commitmsg = msg or defaultmsg | ||||
|
r43346 | n = newcommit( | ||
repo, | ||||
None, | ||||
commitmsg, | ||||
user, | ||||
date, | ||||
match=match, | ||||
force=True, | ||||
editor=editor, | ||||
) | ||||
|
r8527 | if n is None: | ||
|
r43347 | raise error.Abort(_(b"repo commit failed")) | ||
|
r7162 | try: | ||
|
r14572 | self.fullseries[insert:insert] = [patchfn] | ||
|
r10678 | self.applied.append(statusentry(n, patchfn)) | ||
|
r14575 | self.parseseries() | ||
|
r15879 | self.seriesdirty = True | ||
self.applieddirty = True | ||||
|
r21234 | nctx = repo[n] | ||
|
r22547 | ph = patchheader(self.join(patchfn), self.plainmode) | ||
if user: | ||||
ph.setuser(user) | ||||
if date: | ||||
|
r43347 | ph.setdate(b'%d %d' % date) | ||
|
r22547 | ph.setparent(hex(nctx.p1().node())) | ||
msg = nctx.description().strip() | ||||
if msg == defaultmsg.strip(): | ||||
|
r43347 | msg = b'' | ||
|
r22547 | ph.setmessage(msg) | ||
|
r35966 | p.write(bytes(ph)) | ||
|
r7162 | if commitfiles: | ||
parent = self.qparents(repo, n) | ||||
|
r16366 | if inclsubs: | ||
|
r17152 | self.putsubstate2changes(substatestate, changes) | ||
|
r43346 | chunks = patchmod.diff( | ||
repo, | ||||
node1=parent, | ||||
node2=n, | ||||
changes=changes, | ||||
opts=diffopts, | ||||
) | ||||
|
r7308 | for chunk in chunks: | ||
p.write(chunk) | ||||
|
r7162 | p.close() | ||
r = self.qrepo() | ||||
|
r10282 | if r: | ||
|
r11303 | r[None].add([patchfn]) | ||
|
r43346 | except: # re-raises | ||
|
r7162 | repo.rollback() | ||
raise | ||||
|
r7280 | except Exception: | ||
|
r7162 | patchpath = self.join(patchfn) | ||
try: | ||||
os.unlink(patchpath) | ||||
|
r16688 | except OSError: | ||
|
r43347 | self.ui.warn(_(b'error unlinking %s\n') % patchpath) | ||
|
r7162 | raise | ||
|
r4915 | self.removeundo(repo) | ||
|
r1808 | |||
def isapplied(self, patch): | ||||
"""returns (index, rev, patch)""" | ||||
|
r8632 | for i, a in enumerate(self.applied): | ||
|
r2780 | if a.name == patch: | ||
|
r10678 | return (i, a.node, a.name) | ||
|
r1808 | return None | ||
|
r3223 | # if the exact patch name does not exist, we try a few | ||
|
r2696 | # variations. If strict is passed, we try only #1 | ||
# | ||||
|
r15256 | # 1) a number (as string) to indicate an offset in the series file | ||
|
r2696 | # 2) a unique substring of the patch name was given | ||
# 3) patchname[-+]num to indicate an offset in the series file | ||||
def lookup(self, patch, strict=False): | ||||
|
r14595 | def partialname(s): | ||
|
r2696 | if s in self.series: | ||
return s | ||||
|
r2765 | matches = [x for x in self.series if s in x] | ||
if len(matches) > 1: | ||||
|
r43347 | self.ui.warn(_(b'patch name "%s" is ambiguous:\n') % s) | ||
|
r2765 | for m in matches: | ||
|
r43347 | self.ui.warn(b' %s\n' % m) | ||
|
r2765 | return None | ||
if matches: | ||||
return matches[0] | ||||
|
r10686 | if self.series and self.applied: | ||
|
r43347 | if s == b'qtip': | ||
|
r18054 | return self.series[self.seriesend(True) - 1] | ||
|
r43347 | if s == b'qbase': | ||
|
r2696 | return self.series[0] | ||
return None | ||||
|
r7568 | |||
if patch in self.series: | ||||
return patch | ||||
|
r2696 | |||
|
r2819 | if not os.path.isfile(self.join(patch)): | ||
|
r1808 | try: | ||
sno = int(patch) | ||||
|
r10282 | except (ValueError, OverflowError): | ||
|
r2696 | pass | ||
else: | ||||
|
r7568 | if -len(self.series) <= sno < len(self.series): | ||
|
r2821 | return self.series[sno] | ||
|
r7568 | |||
|
r2696 | if not strict: | ||
|
r14595 | res = partialname(patch) | ||
|
r2696 | if res: | ||
return res | ||||
|
r43347 | minus = patch.rfind(b'-') | ||
|
r3082 | if minus >= 0: | ||
|
r14595 | res = partialname(patch[:minus]) | ||
|
r2696 | if res: | ||
i = self.series.index(res) | ||||
try: | ||||
|
r43346 | off = int(patch[minus + 1 :] or 1) | ||
|
r10282 | except (ValueError, OverflowError): | ||
|
r2696 | pass | ||
else: | ||||
if i - off >= 0: | ||||
return self.series[i - off] | ||||
|
r43347 | plus = patch.rfind(b'+') | ||
|
r3082 | if plus >= 0: | ||
|
r14595 | res = partialname(patch[:plus]) | ||
|
r2696 | if res: | ||
i = self.series.index(res) | ||||
try: | ||||
|
r43346 | off = int(patch[plus + 1 :] or 1) | ||
|
r10282 | except (ValueError, OverflowError): | ||
|
r2696 | pass | ||
else: | ||||
if i + off < len(self.series): | ||||
return self.series[i + off] | ||||
|
r43347 | raise error.Abort(_(b"patch %s not in series") % patch) | ||
|
r1808 | |||
|
r43346 | def push( | ||
self, | ||||
repo, | ||||
patch=None, | ||||
force=False, | ||||
list=False, | ||||
mergeq=None, | ||||
all=False, | ||||
move=False, | ||||
exact=False, | ||||
nobackup=False, | ||||
keepchanges=False, | ||||
): | ||||
|
r16733 | self.checkkeepchanges(keepchanges, force) | ||
|
r10184 | diffopts = self.diffopts() | ||
|
r27828 | with repo.wlock(): | ||
|
r20119 | heads = [] | ||
|
r42169 | for hs in repo.branchmap().iterheads(): | ||
|
r20119 | heads.extend(hs) | ||
|
r10362 | if not heads: | ||
|
r47771 | heads = [repo.nullid] | ||
|
r13878 | if repo.dirstate.p1() not in heads and not exact: | ||
|
r43347 | self.ui.status(_(b"(working directory not at a head)\n")) | ||
|
r6340 | |||
|
r8795 | if not self.series: | ||
|
r43347 | self.ui.warn(_(b'no patches in series\n')) | ||
|
r8795 | return 0 | ||
|
r7398 | |||
|
r4915 | # Suppose our series file is: A B C and the current 'top' | ||
# patch is B. qpush C should be performed (moving forward) | ||||
# qpush B is a NOP (no change) qpush A is an error (can't | ||||
# go backwards with qpush) | ||||
if patch: | ||||
|
r15257 | patch = self.lookup(patch) | ||
|
r4915 | info = self.isapplied(patch) | ||
|
r13369 | if info and info[0] >= len(self.applied) - 1: | ||
|
r43347 | self.ui.warn( | ||
_(b'qpush: %s is already at the top\n') % patch | ||||
) | ||||
|
r11439 | return 0 | ||
|
r13369 | |||
|
r7398 | pushable, reason = self.pushable(patch) | ||
|
r13369 | if pushable: | ||
|
r14586 | if self.series.index(patch) < self.seriesend(): | ||
|
r26587 | raise error.Abort( | ||
|
r43347 | _(b"cannot push to a previous patch: %s") % patch | ||
|
r43346 | ) | ||
|
r13369 | else: | ||
|
r7398 | if reason: | ||
|
r43347 | reason = _(b'guarded by %s') % reason | ||
|
r4915 | else: | ||
|
r43347 | reason = _(b'no matching guards') | ||
self.ui.warn( | ||||
_(b"cannot push '%s' - %s\n") % (patch, reason) | ||||
) | ||||
|
r7398 | return 1 | ||
elif all: | ||||
patch = self.series[-1] | ||||
if self.isapplied(patch): | ||||
|
r43347 | self.ui.warn(_(b'all patches are currently applied\n')) | ||
|
r7398 | return 0 | ||
|
r4100 | |||
|
r4915 | # Following the above example, starting at 'top' of B: | ||
# qpush should be performed (pushes C), but a subsequent | ||||
# qpush without an argument is an error (nothing to | ||||
# apply). This allows a loop of "...while hg qpush..." to | ||||
# work as it detects an error when done | ||||
|
r14586 | start = self.seriesend() | ||
|
r7398 | if start == len(self.series): | ||
|
r43347 | self.ui.warn(_(b'patch series already fully applied\n')) | ||
|
r4915 | return 1 | ||
|
r16733 | if not force and not keepchanges: | ||
|
r14732 | self.checklocalchanges(repo, refresh=self.applied) | ||
|
r1810 | |||
|
r13033 | if exact: | ||
|
r16733 | if keepchanges: | ||
|
r26587 | raise error.Abort( | ||
|
r43347 | _(b"cannot use --exact and --keep-changes together") | ||
|
r43346 | ) | ||
|
r13033 | if move: | ||
|
r43346 | raise error.Abort( | ||
|
r43387 | _(b'cannot use --exact and --move together') | ||
|
r43346 | ) | ||
|
r13033 | if self.applied: | ||
|
r43346 | raise error.Abort( | ||
|
r43387 | _(b'cannot push --exact with applied patches') | ||
|
r43346 | ) | ||
|
r13033 | root = self.series[start] | ||
target = patchheader(self.join(root), self.plainmode).parent | ||||
if not target: | ||||
|
r26587 | raise error.Abort( | ||
|
r43347 | _(b"%s does not have a parent recorded") % root | ||
|
r43346 | ) | ||
|
r43347 | if not repo[target] == repo[b'.']: | ||
|
r13033 | hg.update(repo, target) | ||
|
r11064 | if move: | ||
|
r11715 | if not patch: | ||
|
r43347 | raise error.Abort(_(b"please specify the patch to move")) | ||
|
r16303 | for fullstart, rpn in enumerate(self.fullseries): | ||
# strip markers for patch guards | ||||
if self.guard_re.split(rpn, 1)[0] == self.series[start]: | ||||
break | ||||
for i, rpn in enumerate(self.fullseries[fullstart:]): | ||||
|
r11715 | # strip markers for patch guards | ||
if self.guard_re.split(rpn, 1)[0] == patch: | ||||
break | ||||
|
r16303 | index = fullstart + i | ||
|
r14572 | assert index < len(self.fullseries) | ||
fullpatch = self.fullseries[index] | ||||
del self.fullseries[index] | ||||
|
r16303 | self.fullseries.insert(fullstart, fullpatch) | ||
|
r14575 | self.parseseries() | ||
|
r15879 | self.seriesdirty = True | ||
self.applieddirty = True | ||||
|
r4915 | if start > 0: | ||
|
r14581 | self.checktoppatch(repo) | ||
|
r4915 | if not patch: | ||
patch = self.series[start] | ||||
end = start + 1 | ||||
|
r4418 | else: | ||
|
r4915 | end = self.series.index(patch, start) + 1 | ||
|
r8875 | |||
|
r16634 | tobackup = set() | ||
|
r16733 | if (not nobackup and force) or keepchanges: | ||
|
r22925 | status = self.checklocalchanges(repo, force=True) | ||
|
r16733 | if keepchanges: | ||
|
r43346 | tobackup.update( | ||
status.modified | ||||
+ status.added | ||||
+ status.removed | ||||
+ status.deleted | ||||
) | ||||
|
r16654 | else: | ||
|
r22925 | tobackup.update(status.modified + status.added) | ||
|
r16634 | |||
|
r4915 | s = self.series[start:end] | ||
|
r10661 | all_files = set() | ||
|
r4915 | try: | ||
if mergeq: | ||||
|
r10184 | ret = self.mergepatch(repo, mergeq, s, diffopts) | ||
|
r4915 | else: | ||
|
r43346 | ret = self.apply( | ||
repo, | ||||
s, | ||||
list, | ||||
all_files=all_files, | ||||
tobackup=tobackup, | ||||
keepchanges=keepchanges, | ||||
) | ||||
|
r24826 | except AbortNoCleanup: | ||
raise | ||||
|
r43346 | except: # re-raises | ||
|
r43347 | self.ui.warn(_(b'cleaning up working directory...\n')) | ||
|
r43346 | cmdutil.revert( | ||
|
r46554 | self.ui, | ||
repo, | ||||
repo[b'.'], | ||||
no_backup=True, | ||||
|
r43346 | ) | ||
|
r4915 | # only remove unknown files that we know we touched or | ||
# created while patching | ||||
|
r10662 | for f in all_files: | ||
if f not in repo.dirstate: | ||||
|
r31309 | repo.wvfs.unlinkpath(f, ignoremissing=True) | ||
|
r43347 | self.ui.warn(_(b'done\n')) | ||
|
r4915 | raise | ||
|
r8875 | |||
|
r9590 | if not self.applied: | ||
return ret[0] | ||||
|
r4915 | top = self.applied[-1].name | ||
|
r8875 | if ret[0] and ret[0] > 1: | ||
|
r43347 | msg = _(b"errors during apply, please fix and qrefresh %s\n") | ||
|
r8875 | self.ui.write(msg % top) | ||
|
r4915 | else: | ||
|
r43347 | self.ui.write(_(b"now at: %s\n") % top) | ||
|
r4915 | return ret[0] | ||
|
r8875 | |||
|
r43346 | def pop( | ||
self, | ||||
repo, | ||||
patch=None, | ||||
force=False, | ||||
update=True, | ||||
all=False, | ||||
nobackup=False, | ||||
keepchanges=False, | ||||
): | ||||
|
r16733 | self.checkkeepchanges(keepchanges, force) | ||
|
r27829 | with repo.wlock(): | ||
|
r4915 | if patch: | ||
# index, rev, patch | ||||
info = self.isapplied(patch) | ||||
if not info: | ||||
patch = self.lookup(patch) | ||||
info = self.isapplied(patch) | ||||
if not info: | ||||
|
r43347 | raise error.Abort(_(b"patch %s is not applied") % patch) | ||
|
r4100 | |||
|
r10686 | if not self.applied: | ||
|
r4915 | # Allow qpop -a to work repeatedly, | ||
# but not qpop without an argument | ||||
|
r43347 | self.ui.warn(_(b"no patches applied\n")) | ||
|
r4915 | return not all | ||
|
r1808 | |||
|
r7620 | if all: | ||
start = 0 | ||||
elif patch: | ||||
start = info[0] + 1 | ||||
else: | ||||
start = len(self.applied) - 1 | ||||
if start >= len(self.applied): | ||||
|
r43347 | self.ui.warn(_(b"qpop: %s is already at the top\n") % patch) | ||
|
r7620 | return | ||
|
r4915 | if not update: | ||
parents = repo.dirstate.parents() | ||||
|
r10678 | rr = [x.node for x in self.applied] | ||
|
r4915 | for p in parents: | ||
if p in rr: | ||||
|
r43347 | self.ui.warn(_(b"qpop: forcing dirstate update\n")) | ||
|
r4915 | update = True | ||
|
r7621 | else: | ||
|
r10678 | parents = [p.node() for p in repo[None].parents()] | ||
|
r43346 | update = any( | ||
entry.node in parents for entry in self.applied[start:] | ||||
) | ||||
|
r1808 | |||
|
r16633 | tobackup = set() | ||
if update: | ||||
|
r22925 | s = self.checklocalchanges(repo, force=force or keepchanges) | ||
|
r16653 | if force: | ||
if not nobackup: | ||||
|
r22925 | tobackup.update(s.modified + s.added) | ||
|
r16733 | elif keepchanges: | ||
|
r43346 | tobackup.update( | ||
s.modified + s.added + s.removed + s.deleted | ||||
) | ||||
|
r14732 | |||
|
r15879 | self.applieddirty = True | ||
|
r4915 | end = len(self.applied) | ||
|
r10678 | rev = self.applied[start].node | ||
|
r5980 | |||
|
r7621 | try: | ||
heads = repo.changelog.heads(rev) | ||||
|
r7639 | except error.LookupError: | ||
|
r7621 | node = short(rev) | ||
|
r43347 | raise error.Abort(_(b'trying to pop unknown node %s') % node) | ||
|
r7621 | |||
|
r10678 | if heads != [self.applied[-1].node]: | ||
|
r43346 | raise error.Abort( | ||
_( | ||||
|
r43347 | b"popping would remove a revision not " | ||
b"managed by this patch queue" | ||||
|
r43346 | ) | ||
) | ||||
|
r16048 | if not repo[self.applied[-1].node].mutable(): | ||
|
r26587 | raise error.Abort( | ||
|
r43347 | _(b"popping would remove a public revision"), | ||
hint=_(b"see 'hg help phases' for details"), | ||||
|
r43346 | ) | ||
|
r5980 | |||
|
r4915 | # we know there are no local changes, so we can make a simplified | ||
# form of hg.update. | ||||
if update: | ||||
qp = self.qparents(repo, rev) | ||||
|
r10663 | ctx = repo[qp] | ||
|
r44039 | st = repo.status(qp, b'.') | ||
m, a, r, d = st.modified, st.added, st.removed, st.deleted | ||||
|
r4915 | if d: | ||
|
r43347 | raise error.Abort(_(b"deletions found between repo revs")) | ||
|
r16633 | |||
|
r16653 | tobackup = set(a + m + r) & tobackup | ||
|
r16733 | if keepchanges and tobackup: | ||
|
r43347 | raise error.Abort(_(b"local changes found, qrefresh first")) | ||
|
r16653 | self.backup(repo, tobackup) | ||
|
r32347 | with repo.dirstate.parentchange(): | ||
for f in a: | ||||
repo.wvfs.unlinkpath(f, ignoremissing=True) | ||||
repo.dirstate.drop(f) | ||||
for f in m + r: | ||||
fctx = ctx[f] | ||||
repo.wwrite(f, fctx.data(), fctx.flags()) | ||||
r48517 | repo.dirstate.update_file( | |||
f, p1_tracked=True, wc_tracked=True | ||||
) | ||||
|
r47771 | repo.setparents(qp, repo.nullid) | ||
|
r9110 | for patch in reversed(self.applied[start:end]): | ||
|
r43347 | self.ui.status(_(b"popping %s\n") % patch.name) | ||
|
r5987 | del self.applied[start:end] | ||
|
r22057 | strip(self.ui, repo, [rev], update=False, backup=False) | ||
|
r43347 | for s, state in repo[b'.'].substate.items(): | ||
repo[b'.'].sub(s).get(state) | ||||
|
r10686 | if self.applied: | ||
|
r43347 | self.ui.write(_(b"now at: %s\n") % self.applied[-1].name) | ||
|
r4915 | else: | ||
|
r43347 | self.ui.write(_(b"patch queue now empty\n")) | ||
|
r1808 | |||
|
r2937 | def diff(self, repo, pats, opts): | ||
|
r14581 | top, patch = self.checktoppatch(repo) | ||
|
r1808 | if not top: | ||
|
r43347 | self.ui.write(_(b"no patches applied\n")) | ||
|
r1808 | return | ||
qp = self.qparents(repo, top) | ||||
|
r43347 | if opts.get(b'reverse'): | ||
|
r9725 | node1, node2 = None, qp | ||
else: | ||||
node1, node2 = qp, None | ||||
|
r10191 | diffopts = self.diffopts(opts, patch) | ||
|
r10184 | self.printdiff(repo, diffopts, node1, node2, files=pats, opts=opts) | ||
|
r1808 | |||
|
r2938 | def refresh(self, repo, pats=None, **opts): | ||
|
r36405 | opts = pycompat.byteskwargs(opts) | ||
|
r10686 | if not self.applied: | ||
|
r43347 | self.ui.write(_(b"no patches applied\n")) | ||
|
r3004 | return 1 | ||
|
r43347 | msg = opts.get(b'msg', b'').rstrip() | ||
edit = opts.get(b'edit') | ||||
editform = opts.get(b'editform', b'mq.qrefresh') | ||||
newuser = opts.get(b'user') | ||||
newdate = opts.get(b'date') | ||||
|
r6139 | if newdate: | ||
|
r43347 | newdate = b'%d %d' % dateutil.parsedate(newdate) | ||
|
r1808 | wlock = repo.wlock() | ||
|
r10366 | |||
|
r4915 | try: | ||
|
r14581 | self.checktoppatch(repo) | ||
|
r10678 | (top, patchfn) = (self.applied[-1].node, self.applied[-1].name) | ||
|
r5980 | if repo.changelog.heads(top) != [top]: | ||
|
r43347 | raise error.Abort( | ||
_(b"cannot qrefresh a revision with children") | ||||
) | ||||
|
r16048 | if not repo[top].mutable(): | ||
|
r43346 | raise error.Abort( | ||
|
r43347 | _(b"cannot qrefresh public revision"), | ||
hint=_(b"see 'hg help phases' for details"), | ||||
|
r43346 | ) | ||
|
r10366 | |||
|
r17153 | cparents = repo.changelog.parents(top) | ||
patchparent = self.qparents(repo, top) | ||||
|
r37399 | inclsubs = checksubstate(repo, patchparent) | ||
|
r17152 | if inclsubs: | ||
|
r43347 | substatestate = repo.dirstate[b'.hgsubstate'] | ||
|
r13174 | |||
|
r10397 | ph = patchheader(self.join(patchfn), self.plainmode) | ||
|
r43346 | diffopts = self.diffopts( | ||
|
r43347 | {b'git': opts.get(b'git')}, patchfn, plain=True | ||
|
r43346 | ) | ||
|
r5673 | if newuser: | ||
|
r7399 | ph.setuser(newuser) | ||
|
r5788 | if newdate: | ||
|
r7399 | ph.setdate(newdate) | ||
|
r10397 | ph.setparent(hex(patchparent)) | ||
|
r5180 | |||
|
r7400 | # only commit new patch when write is complete | ||
|
r43347 | patchf = self.opener(patchfn, b'w', atomictemp=True) | ||
|
r7400 | |||
|
r10366 | # update the dirstate in place, strip off the qtip commit | ||
# and then commit. | ||||
# | ||||
# this should really read: | ||||
|
r44039 | # st = repo.status(top, patchparent) | ||
|
r17425 | # but we do it backwards to take advantage of manifest/changelog | ||
|
r10366 | # caching against the next repo.status call | ||
|
r44039 | st = repo.status(patchparent, top) | ||
mm, aa, dd = st.modified, st.added, st.removed | ||||
|
r41945 | ctx = repo[top] | ||
|
r10366 | aaa = aa[:] | ||
|
r34085 | match1 = scmutil.match(repo[None], pats, opts) | ||
|
r10366 | # in short mode, we only diff the files included in the | ||
# patch already plus specified files | ||||
|
r43347 | if opts.get(b'short'): | ||
|
r10366 | # if amending a patch, we start with existing | ||
# files plus specified files - unfiltered | ||||
|
r34085 | match = scmutil.matchfiles(repo, mm + aa + dd + match1.files()) | ||
|
r17424 | # filter with include/exclude options | ||
|
r34085 | match1 = scmutil.match(repo[None], opts=opts) | ||
|
r10366 | else: | ||
|
r14322 | match = scmutil.matchall(repo) | ||
|
r44039 | stb = repo.status(match=match) | ||
m, a, r, d = stb.modified, stb.added, stb.removed, stb.deleted | ||||
|
r12948 | mm = set(mm) | ||
aa = set(aa) | ||||
dd = set(dd) | ||||
|
r1808 | |||
|
r10366 | # we might end up with files that were added between | ||
# qtip and the dirstate parent, but then changed in the | ||||
# local dirstate. in this case, we want them to only | ||||
# show up in the added section | ||||
for x in m: | ||||
if x not in aa: | ||||
|
r12948 | mm.add(x) | ||
|
r10366 | # we might end up with files added by the local dirstate that | ||
# were deleted by the patch. In this case, they should only | ||||
# show up in the changed section. | ||||
for x in a: | ||||
if x in dd: | ||||
|
r12948 | dd.remove(x) | ||
mm.add(x) | ||||
|
r10366 | else: | ||
|
r12948 | aa.add(x) | ||
|
r10366 | # make sure any files deleted in the local dirstate | ||
# are not in the add or change column of the patch | ||||
forget = [] | ||||
for x in d + r: | ||||
if x in aa: | ||||
|
r12948 | aa.remove(x) | ||
|
r10366 | forget.append(x) | ||
continue | ||||
|
r12948 | else: | ||
mm.discard(x) | ||||
dd.add(x) | ||||
m = list(mm) | ||||
r = list(dd) | ||||
a = list(aa) | ||||
|
r17888 | |||
|
r18644 | # create 'match' that includes the files to be recommitted. | ||
|
r34085 | # apply match1 via repo.status to ensure correct case handling. | ||
|
r44039 | st = repo.status(patchparent, match=match1) | ||
cm, ca, cr, cd = st.modified, st.added, st.removed, st.deleted | ||||
|
r17888 | allmatches = set(cm + ca + cr + cd) | ||
refreshchanges = [x.intersection(allmatches) for x in (mm, aa, dd)] | ||||
files = set(inclsubs) | ||||
for x in refreshchanges: | ||||
|
r20827 | files.update(x) | ||
|
r17888 | match = scmutil.matchfiles(repo, files) | ||
|
r17730 | bmlist = repo[top].bookmarks() | ||
|
r1808 | |||
r48397 | with repo.dirstate.parentchange(): | |||
# XXX do we actually need the dirstateguard | ||||
dsguard = None | ||||
try: | ||||
dsguard = dirstateguard.dirstateguard(repo, b'mq.refresh') | ||||
if diffopts.git or diffopts.upgrade: | ||||
copies = {} | ||||
for dst in a: | ||||
src = repo.dirstate.copied(dst) | ||||
# during qfold, the source file for copies may | ||||
# be removed. Treat this as a simple add. | ||||
if src is not None and src in repo.dirstate: | ||||
copies.setdefault(src, []).append(dst) | ||||
repo.dirstate.add(dst) | ||||
# remember the copies between patchparent and qtip | ||||
for dst in aaa: | ||||
src = ctx[dst].copysource() | ||||
if src: | ||||
copies.setdefault(src, []).extend( | ||||
copies.get(dst, []) | ||||
) | ||||
if dst in a: | ||||
copies[src].append(dst) | ||||
# we can't copy a file created by the patch itself | ||||
if dst in copies: | ||||
del copies[dst] | ||||
for src, dsts in pycompat.iteritems(copies): | ||||
for dst in dsts: | ||||
repo.dirstate.copy(src, dst) | ||||
else: | ||||
for dst in a: | ||||
repo.dirstate.add(dst) | ||||
# Drop useless copy information | ||||
for f in list(repo.dirstate.copies()): | ||||
repo.dirstate.copy(None, f) | ||||
for f in r: | ||||
r48497 | repo.dirstate.update_file_p1(f, p1_tracked=True) | |||
r48397 | # if the patch excludes a modified file, mark that | |||
# file with mtime=0 so status can see it. | ||||
mm = [] | ||||
for i in pycompat.xrange(len(m) - 1, -1, -1): | ||||
if not match1(m[i]): | ||||
mm.append(m[i]) | ||||
del m[i] | ||||
for f in m: | ||||
r48517 | repo.dirstate.update_file_p1(f, p1_tracked=True) | |||
r48397 | for f in mm: | |||
repo.dirstate.normallookup(f) | ||||
for f in forget: | ||||
repo.dirstate.drop(f) | ||||
user = ph.user or ctx.user() | ||||
oldphase = repo[top].phase() | ||||
# assumes strip can roll itself back if interrupted | ||||
repo.setparents(*cparents) | ||||
self.applied.pop() | ||||
self.applieddirty = True | ||||
strip(self.ui, repo, [top], update=False, backup=False) | ||||
dsguard.close() | ||||
finally: | ||||
release(dsguard) | ||||
|
r10366 | |||
try: | ||||
# might be nice to attempt to roll back strip after this | ||||
|
r16100 | |||
|
r43347 | defaultmsg = b"[mq]: %s" % patchfn | ||
|
r22003 | editor = cmdutil.getcommiteditor(editform=editform) | ||
|
r21422 | if edit: | ||
|
r43346 | |||
|
r21423 | def finishdesc(desc): | ||
|
r21236 | if desc.rstrip(): | ||
ph.setmessage(desc) | ||||
return desc | ||||
return defaultmsg | ||||
|
r43346 | |||
|
r21423 | # i18n: this message is shown in editor with "HG: " prefix | ||
|
r43347 | extramsg = _(b'Leave message empty to use default message.') | ||
|
r43346 | editor = cmdutil.getcommiteditor( | ||
finishdesc=finishdesc, | ||||
extramsg=extramsg, | ||||
editform=editform, | ||||
) | ||||
|
r43347 | message = msg or b"\n".join(ph.message) | ||
|
r21236 | elif not msg: | ||
|
r21235 | if not ph.message: | ||
|
r21236 | message = defaultmsg | ||
|
r21235 | else: | ||
|
r43347 | message = b"\n".join(ph.message) | ||
|
r21235 | else: | ||
message = msg | ||||
ph.setmessage(msg) | ||||
|
r16100 | # Ensure we create a new changeset in the same phase than | ||
# the old one. | ||||
|
r27001 | lock = tr = None | ||
try: | ||||
lock = repo.lock() | ||||
|
r43347 | tr = repo.transaction(b'mq') | ||
|
r43346 | n = newcommit( | ||
repo, | ||||
oldphase, | ||||
message, | ||||
user, | ||||
ph.date, | ||||
match=match, | ||||
force=True, | ||||
editor=editor, | ||||
) | ||||
|
r27000 | # only write patch after a successful commit | ||
c = [list(x) for x in refreshchanges] | ||||
if inclsubs: | ||||
self.putsubstate2changes(substatestate, c) | ||||
|
r43346 | chunks = patchmod.diff( | ||
repo, patchparent, changes=c, opts=diffopts | ||||
) | ||||
|
r35966 | comments = bytes(ph) | ||
|
r27000 | if comments: | ||
patchf.write(comments) | ||||
for chunk in chunks: | ||||
patchf.write(chunk) | ||||
patchf.close() | ||||
marks = repo._bookmarks | ||||
|
r33489 | marks.applychanges(repo, tr, [(bm, n) for bm in bmlist]) | ||
|
r27001 | tr.close() | ||
|
r27000 | |||
self.applied.append(statusentry(n, patchfn)) | ||||
|
r27001 | finally: | ||
|
r30070 | lockmod.release(tr, lock) | ||
|
r43346 | except: # re-raises | ||
|
r10366 | ctx = repo[cparents[0]] | ||
repo.dirstate.rebuild(ctx.node(), ctx.manifest()) | ||||
|
r14580 | self.savedirty() | ||
|
r43346 | self.ui.warn( | ||
_( | ||||
|
r43347 | b'qrefresh interrupted while patch was popped! ' | ||
b'(revert --all, qpush to recover)\n' | ||||
|
r43346 | ) | ||
) | ||||
|
r10366 | raise | ||
|
r4915 | finally: | ||
|
r8112 | wlock.release() | ||
|
r7401 | self.removeundo(repo) | ||
|
r1808 | |||
def init(self, repo, create=False): | ||||
|
r4071 | if not create and os.path.isdir(self.path): | ||
|
r43347 | raise error.Abort(_(b"patch queue directory already exists")) | ||
|
r4071 | try: | ||
os.mkdir(self.path) | ||||
|
r25660 | except OSError as inst: | ||
|
r4071 | if inst.errno != errno.EEXIST or not create: | ||
raise | ||||
|
r1808 | if create: | ||
return self.qrepo(create=True) | ||||
def unapplied(self, repo, patch=None): | ||||
if patch and patch not in self.series: | ||||
|
r43347 | raise error.Abort(_(b"patch %s is not in series file") % patch) | ||
|
r1808 | if not patch: | ||
|
r14586 | start = self.seriesend() | ||
|
r1808 | else: | ||
start = self.series.index(patch) + 1 | ||||
|
r2821 | unapplied = [] | ||
|
r38806 | for i in pycompat.xrange(start, len(self.series)): | ||
|
r2821 | pushable, reason = self.pushable(i) | ||
if pushable: | ||||
unapplied.append((i, self.series[i])) | ||||
|
r14579 | self.explainpushable(i) | ||
|
r2821 | return unapplied | ||
|
r1810 | |||
|
r43346 | def qseries( | ||
self, | ||||
repo, | ||||
missing=None, | ||||
start=0, | ||||
length=None, | ||||
status=None, | ||||
summary=False, | ||||
): | ||||
|
r10824 | def displayname(pfx, patchname, state): | ||
|
r10932 | if pfx: | ||
self.ui.write(pfx) | ||||
|
r3183 | if summary: | ||
|
r10397 | ph = patchheader(self.join(patchname), self.plainmode) | ||
|
r24306 | if ph.message: | ||
msg = ph.message[0] | ||||
else: | ||||
|
r43347 | msg = b'' | ||
|
r24306 | |||
|
r11327 | if self.ui.formatted(): | ||
|
r12689 | width = self.ui.termwidth() - len(pfx) - len(patchname) - 2 | ||
|
r9874 | if width > 0: | ||
|
r37102 | msg = stringutil.ellipsis(msg, width) | ||
|
r9874 | else: | ||
|
r43347 | msg = b'' | ||
self.ui.write(patchname, label=b'qseries.' + state) | ||||
self.ui.write(b': ') | ||||
self.ui.write(msg, label=b'qseries.message.' + state) | ||||
|
r3183 | else: | ||
|
r43347 | self.ui.write(patchname, label=b'qseries.' + state) | ||
self.ui.write(b'\n') | ||||
|
r3183 | |||
|
r42224 | applied = {p.name for p in self.applied} | ||
|
r4239 | if length is None: | ||
|
r3183 | length = len(self.series) - start | ||
|
r1808 | if not missing: | ||
|
r9016 | if self.ui.verbose: | ||
|
r43347 | idxwidth = len(b"%d" % (start + length - 1)) | ||
|
r38806 | for i in pycompat.xrange(start, start + length): | ||
|
r4239 | patch = self.series[i] | ||
if patch in applied: | ||||
|
r43347 | char, state = b'A', b'applied' | ||
|
r4239 | elif self.pushable(i)[0]: | ||
|
r43347 | char, state = b'U', b'unapplied' | ||
|
r4239 | else: | ||
|
r43347 | char, state = b'G', b'guarded' | ||
pfx = b'' | ||||
|
r1808 | if self.ui.verbose: | ||
|
r43347 | pfx = b'%*d %s ' % (idxwidth, i, char) | ||
|
r10824 | elif status and status != char: | ||
|
r4238 | continue | ||
|
r10824 | displayname(pfx, patch, state) | ||
|
r1808 | else: | ||
|
r2794 | msng_list = [] | ||
|
r1808 | for root, dirs, files in os.walk(self.path): | ||
|
r43346 | d = root[len(self.path) + 1 :] | ||
|
r1808 | for f in files: | ||
fl = os.path.join(d, f) | ||||
|
r43346 | if ( | ||
fl not in self.series | ||||
and fl | ||||
not in ( | ||||
self.statuspath, | ||||
self.seriespath, | ||||
self.guardspath, | ||||
) | ||||
|
r43347 | and not fl.startswith(b'.') | ||
|
r43346 | ): | ||
|
r2794 | msng_list.append(fl) | ||
|
r8209 | for x in sorted(msng_list): | ||
|
r43347 | pfx = self.ui.verbose and b'D ' or b'' | ||
displayname(pfx, x, b'missing') | ||||
|
r1808 | |||
def issaveline(self, l): | ||||
|
r43347 | if l.name == b'.hg.patches.save.line': | ||
|
r1808 | return True | ||
def qrepo(self, create=False): | ||||
|
r19064 | ui = self.baseui.copy() | ||
|
r34919 | # copy back attributes set by ui.pager() | ||
if self.ui.pageractive and not ui.pageractive: | ||||
ui.pageractive = self.ui.pageractive | ||||
# internal config: ui.formatted | ||||
|
r43346 | ui.setconfig( | ||
|
r43347 | b'ui', | ||
b'formatted', | ||||
self.ui.config(b'ui', b'formatted'), | ||||
b'mqpager', | ||||
|
r43346 | ) | ||
ui.setconfig( | ||||
|
r43347 | b'ui', | ||
b'interactive', | ||||
self.ui.config(b'ui', b'interactive'), | ||||
b'mqpager', | ||||
|
r43346 | ) | ||
|
r43347 | if create or os.path.isdir(self.join(b".hg")): | ||
|
r11965 | return hg.repository(ui, path=self.path, create=create) | ||
|
r1808 | |||
def restore(self, repo, rev, delete=None, qupdate=None): | ||||
|
r10681 | desc = repo[rev].description().strip() | ||
|
r1808 | lines = desc.splitlines() | ||
datastart = None | ||||
series = [] | ||||
applied = [] | ||||
qpp = None | ||||
|
r8632 | for i, line in enumerate(lines): | ||
|
r43347 | if line == b'Patch Data:': | ||
|
r1808 | datastart = i + 1 | ||
|
r43347 | elif line.startswith(b'Dirstate:'): | ||
|
r8632 | l = line.rstrip() | ||
|
r43347 | l = l[10:].split(b' ') | ||
|
r10282 | qpp = [bin(x) for x in l] | ||
|
r13031 | elif datastart is not None: | ||
|
r8632 | l = line.rstrip() | ||
|
r43347 | n, name = l.split(b':', 1) | ||
|
r10683 | if n: | ||
applied.append(statusentry(bin(n), name)) | ||||
|
r3185 | else: | ||
|
r10682 | series.append(l) | ||
|
r8527 | if datastart is None: | ||
|
r43347 | self.ui.warn(_(b"no saved patch data found\n")) | ||
|
r1808 | return 1 | ||
|
r43347 | self.ui.warn(_(b"restoring status: %s\n") % lines[0]) | ||
|
r14572 | self.fullseries = series | ||
|
r1808 | self.applied = applied | ||
|
r14575 | self.parseseries() | ||
|
r15879 | self.seriesdirty = True | ||
self.applieddirty = True | ||||
|
r1808 | heads = repo.changelog.heads() | ||
if delete: | ||||
if rev not in heads: | ||||
|
r43347 | self.ui.warn(_(b"save entry has children, leaving it alone\n")) | ||
|
r1808 | else: | ||
|
r43347 | self.ui.warn(_(b"removing save entry %s\n") % short(rev)) | ||
|
r1808 | pp = repo.dirstate.parents() | ||
if rev in pp: | ||||
update = True | ||||
else: | ||||
update = False | ||||
|
r22057 | strip(self.ui, repo, [rev], update=update, backup=False) | ||
|
r1808 | if qpp: | ||
|
r43346 | self.ui.warn( | ||
|
r43347 | _(b"saved queue repository parents: %s %s\n") | ||
|
r43346 | % (short(qpp[0]), short(qpp[1])) | ||
) | ||||
|
r1808 | if qupdate: | ||
|
r43347 | self.ui.status(_(b"updating queue directory\n")) | ||
|
r1808 | r = self.qrepo() | ||
if not r: | ||||
|
r43347 | self.ui.warn(_(b"unable to load queue repository\n")) | ||
|
r1808 | return 1 | ||
|
r2808 | hg.clean(r, qpp[0]) | ||
|
r1808 | |||
def save(self, repo, msg=None): | ||||
|
r10686 | if not self.applied: | ||
|
r43347 | self.ui.warn(_(b"save: no patches applied, exiting\n")) | ||
|
r1808 | return 1 | ||
if self.issaveline(self.applied[-1]): | ||||
|
r43347 | self.ui.warn(_(b"status is already saved\n")) | ||
|
r1808 | return 1 | ||
|
r1810 | |||
|
r1808 | if not msg: | ||
|
r43347 | msg = _(b"hg patches saved state") | ||
|
r1808 | else: | ||
|
r43347 | msg = b"hg patches: " + msg.rstrip(b'\r\n') | ||
|
r1808 | r = self.qrepo() | ||
if r: | ||||
pp = r.dirstate.parents() | ||||
|
r43347 | msg += b"\nDirstate: %s %s" % (hex(pp[0]), hex(pp[1])) | ||
msg += b"\n\nPatch Data:\n" | ||||
msg += b''.join(b'%s\n' % x for x in self.applied) | ||||
msg += b''.join(b':%s\n' % x for x in self.fullseries) | ||||
|
r10679 | n = repo.commit(msg, force=True) | ||
|
r1808 | if not n: | ||
|
r43347 | self.ui.warn(_(b"repo commit failed\n")) | ||
|
r1808 | return 1 | ||
|
r43347 | self.applied.append(statusentry(n, b'.hg.patches.save.line')) | ||
|
r15879 | self.applieddirty = True | ||
|
r4209 | self.removeundo(repo) | ||
|
r1808 | |||
|
r14585 | def fullseriesend(self): | ||
|
r10686 | if self.applied: | ||
|
r2780 | p = self.applied[-1].name | ||
|
r14574 | end = self.findseries(p) | ||
|
r8527 | if end is None: | ||
|
r14572 | return len(self.fullseries) | ||
|
r2698 | return end + 1 | ||
return 0 | ||||
|
r14586 | def seriesend(self, all_patches=False): | ||
|
r4406 | """If all_patches is False, return the index of the next pushable patch | ||
in the series, or the series length. If all_patches is True, return the | ||||
index of the first patch past the last applied one. | ||||
""" | ||||
|
r1808 | end = 0 | ||
|
r43346 | |||
|
r19500 | def nextpatch(start): | ||
|
r10687 | if all_patches or start >= len(self.series): | ||
|
r2821 | return start | ||
|
r38806 | for i in pycompat.xrange(start, len(self.series)): | ||
|
r2821 | p, reason = self.pushable(i) | ||
if p: | ||||
|
r16063 | return i | ||
|
r14579 | self.explainpushable(i) | ||
|
r16063 | return len(self.series) | ||
|
r43346 | |||
|
r10686 | if self.applied: | ||
|
r2780 | p = self.applied[-1].name | ||
|
r1808 | try: | ||
end = self.series.index(p) | ||||
except ValueError: | ||||
return 0 | ||||
|
r19500 | return nextpatch(end + 1) | ||
return nextpatch(end) | ||||
|
r1808 | |||
def appliedname(self, index): | ||||
|
r2780 | pname = self.applied[index].name | ||
|
r1808 | if not self.ui.verbose: | ||
|
r2677 | p = pname | ||
else: | ||||
|
r43347 | p = (b"%d" % self.series.index(pname)) + b" " + pname | ||
|
r1808 | return p | ||
|
r1810 | |||
|
r43346 | def qimport( | ||
self, | ||||
repo, | ||||
files, | ||||
patchname=None, | ||||
rev=None, | ||||
existing=None, | ||||
force=None, | ||||
git=False, | ||||
): | ||||
|
r3141 | def checkseries(patchname): | ||
if patchname in self.series: | ||||
|
r43346 | raise error.Abort( | ||
|
r43347 | _(b'patch %s is already in the series file') % patchname | ||
|
r43346 | ) | ||
|
r3141 | |||
if rev: | ||||
if files: | ||||
|
r43346 | raise error.Abort( | ||
|
r43387 | _(b'option "-r" not valid when importing files') | ||
|
r43346 | ) | ||
|
r14319 | rev = scmutil.revrange(repo, rev) | ||
|
r9032 | rev.sort(reverse=True) | ||
|
r16987 | elif not files: | ||
|
r43347 | raise error.Abort(_(b'no files or revisions specified')) | ||
|
r3141 | if (len(files) > 1 or len(rev) > 1) and patchname: | ||
|
r43346 | raise error.Abort( | ||
|
r43387 | _(b'option "-n" not valid when importing multiple patches') | ||
|
r43346 | ) | ||
|
r16119 | imported = [] | ||
|
r3141 | if rev: | ||
# If mq patches are applied, we can only import revisions | ||||
# that form a linear path to qbase. | ||||
# Otherwise, they should form a linear path to a head. | ||||
|
r22821 | heads = repo.changelog.heads(repo.changelog.node(rev.first())) | ||
|
r3141 | if len(heads) > 1: | ||
|
r43346 | raise error.Abort( | ||
|
r43387 | _(b'revision %d is the root of more than one branch') | ||
|
r43346 | % rev.last() | ||
) | ||||
|
r3141 | if self.applied: | ||
|
r22821 | base = repo.changelog.node(rev.first()) | ||
|
r10678 | if base in [n.node for n in self.applied]: | ||
|
r43346 | raise error.Abort( | ||
|
r43347 | _(b'revision %d is already managed') % rev.first() | ||
|
r43346 | ) | ||
|
r10678 | if heads != [self.applied[-1].node]: | ||
|
r43346 | raise error.Abort( | ||
|
r43387 | _(b'revision %d is not the parent of the queue') | ||
|
r43346 | % rev.first() | ||
) | ||||
|
r10678 | base = repo.changelog.rev(self.applied[0].node) | ||
|
r3141 | lastparent = repo.changelog.parentrevs(base)[0] | ||
else: | ||||
|
r22821 | if heads != [repo.changelog.node(rev.first())]: | ||
|
r43346 | raise error.Abort( | ||
|
r43347 | _(b'revision %d has unmanaged children') % rev.first() | ||
|
r43346 | ) | ||
|
r3141 | lastparent = None | ||
|
r43347 | diffopts = self.diffopts({b'git': git}) | ||
with repo.transaction(b'qimport') as tr: | ||||
|
r22049 | for r in rev: | ||
if not repo[r].mutable(): | ||||
|
r43346 | raise error.Abort( | ||
|
r43347 | _(b'revision %d is not mutable') % r, | ||
hint=_(b"see 'hg help phases' " b'for details'), | ||||
|
r43346 | ) | ||
|
r22049 | p1, p2 = repo.changelog.parentrevs(r) | ||
n = repo.changelog.node(r) | ||||
if p2 != nullrev: | ||||
|
r43346 | raise error.Abort( | ||
|
r43347 | _(b'cannot import merge revision %d') % r | ||
|
r43346 | ) | ||
|
r22049 | if lastparent and lastparent != r: | ||
|
r43346 | raise error.Abort( | ||
|
r43387 | _(b'revision %d is not the parent of %d') | ||
|
r43346 | % (r, lastparent) | ||
) | ||||
|
r22049 | lastparent = p1 | ||
if not patchname: | ||||
|
r27918 | patchname = self.makepatchname( | ||
|
r43347 | repo[r].description().split(b'\n', 1)[0], | ||
b'%d.diff' % r, | ||||
|
r43346 | ) | ||
|
r22049 | checkseries(patchname) | ||
self.checkpatchname(patchname, force) | ||||
self.fullseries.insert(0, patchname) | ||||
|
r43347 | with self.opener(patchname, b"w") as fp: | ||
|
r37621 | cmdutil.exportfile(repo, [n], fp, opts=diffopts) | ||
|
r22049 | |||
se = statusentry(n, patchname) | ||||
self.applied.insert(0, se) | ||||
self.added.append(patchname) | ||||
imported.append(patchname) | ||||
patchname = None | ||||
|
r43347 | if rev and repo.ui.configbool(b'mq', b'secret'): | ||
|
r22049 | # if we added anything with --rev, move the secret root | ||
|
r22070 | phases.retractboundary(repo, tr, phases.secret, [n]) | ||
|
r22049 | self.parseseries() | ||
self.applieddirty = True | ||||
self.seriesdirty = True | ||||
|
r3141 | |||
|
r10687 | for i, filename in enumerate(files): | ||
|
r1808 | if existing: | ||
|
r43347 | if filename == b'-': | ||
|
r43346 | raise error.Abort( | ||
|
r43347 | _(b'-e is incompatible with import from -') | ||
|
r43346 | ) | ||
|
r11699 | filename = normname(filename) | ||
|
r14584 | self.checkreservedname(filename) | ||
r47669 | if urlutil.url(filename).islocal(): | |||
|
r20394 | originpath = self.join(filename) | ||
if not os.path.isfile(originpath): | ||||
|
r26587 | raise error.Abort( | ||
|
r43347 | _(b"patch %s does not exist") % filename | ||
|
r43346 | ) | ||
|
r11699 | |||
if patchname: | ||||
|
r14423 | self.checkpatchname(patchname, force) | ||
|
r11699 | |||
|
r43346 | self.ui.write( | ||
|
r43347 | _(b'renaming %s to %s\n') % (filename, patchname) | ||
|
r43346 | ) | ||
|
r11701 | util.rename(originpath, self.join(patchname)) | ||
|
r11699 | else: | ||
patchname = filename | ||||
|
r1808 | else: | ||
|
r43347 | if filename == b'-' and not patchname: | ||
raise error.Abort( | ||||
_(b'need --name to import a patch from -') | ||||
) | ||||
|
r14395 | elif not patchname: | ||
|
r43347 | patchname = normname( | ||
os.path.basename(filename.rstrip(b'/')) | ||||
) | ||||
|
r14423 | self.checkpatchname(patchname, force) | ||
|
r1808 | try: | ||
|
r43347 | if filename == b'-': | ||
|
r14636 | text = self.ui.fin.read() | ||
|
r3547 | else: | ||
|
r17887 | fp = hg.openpath(self.ui, filename) | ||
|
r13400 | text = fp.read() | ||
fp.close() | ||||
|
r7421 | except (OSError, IOError): | ||
|
r43347 | raise error.Abort(_(b"unable to read file %s") % filename) | ||
patchf = self.opener(patchname, b"w") | ||||
|
r1808 | patchf.write(text) | ||
|
r13400 | patchf.close() | ||
|
r7160 | if not force: | ||
checkseries(patchname) | ||||
if patchname not in self.series: | ||||
|
r14585 | index = self.fullseriesend() + i | ||
|
r14572 | self.fullseries[index:index] = [patchname] | ||
|
r14575 | self.parseseries() | ||
|
r14593 | self.seriesdirty = True | ||
|
r43347 | self.ui.warn(_(b"adding %s to series file\n") % patchname) | ||
|
r11462 | self.added.append(patchname) | ||
|
r16119 | imported.append(patchname) | ||
|
r3133 | patchname = None | ||
|
r1808 | |||
|
r13409 | self.removeundo(repo) | ||
|
r16119 | return imported | ||
|
r13409 | |||
|
r43346 | |||
|
r16733 | def fixkeepchangesopts(ui, opts): | ||
|
r43346 | if ( | ||
|
r43347 | not ui.configbool(b'mq', b'keepchanges') | ||
or opts.get(b'force') | ||||
or opts.get(b'exact') | ||||
|
r43346 | ): | ||
|
r16656 | return opts | ||
opts = dict(opts) | ||||
|
r43347 | opts[b'keep_changes'] = True | ||
|
r16656 | return opts | ||
|
r43346 | |||
@command( | ||||
|
r43347 | b"qdelete|qremove|qrm", | ||
|
r43346 | [ | ||
|
r43347 | (b'k', b'keep', None, _(b'keep patch file')), | ||
( | ||||
b'r', | ||||
b'rev', | ||||
[], | ||||
_(b'stop managing a revision (DEPRECATED)'), | ||||
_(b'REV'), | ||||
), | ||||
|
r43346 | ], | ||
|
r43347 | _(b'hg qdelete [-k] [PATCH]...'), | ||
|
r43346 | helpcategory=command.CATEGORY_CHANGE_ORGANIZATION, | ||
) | ||||
|
r3373 | def delete(ui, repo, *patches, **opts): | ||
|
r2905 | """remove patches from queue | ||
|
r2752 | |||
|
r15798 | The patches must not be applied, and at least one patch is required. Exact | ||
patch identifiers must be given. With -k/--keep, the patch files are | ||||
preserved in the patch directory. | ||||
|
r8904 | |||
To stop managing a patch and move it into permanent history, | ||||
|
r11307 | use the :hg:`qfinish` command.""" | ||
|
r2724 | q = repo.mq | ||
|
r36295 | q.delete(repo, patches, pycompat.byteskwargs(opts)) | ||
|
r14580 | q.savedirty() | ||
|
r1808 | return 0 | ||
|
r43346 | |||
@command( | ||||
|
r43347 | b"qapplied", | ||
[(b'1', b'last', None, _(b'show only the preceding applied patch'))] | ||||
|
r43346 | + seriesopts, | ||
|
r43347 | _(b'hg qapplied [-1] [-s] [PATCH]'), | ||
|
r43346 | helpcategory=command.CATEGORY_CHANGE_ORGANIZATION, | ||
) | ||||
|
r1808 | def applied(ui, repo, patch=None, **opts): | ||
|
r12538 | """print the patches already applied | ||
Returns 0 on success.""" | ||||
|
r9364 | |||
|
r3183 | q = repo.mq | ||
|
r34506 | opts = pycompat.byteskwargs(opts) | ||
|
r9364 | |||
|
r3183 | if patch: | ||
if patch not in q.series: | ||||
|
r43347 | raise error.Abort(_(b"patch %s is not in series file") % patch) | ||
|
r3183 | end = q.series.index(patch) + 1 | ||
else: | ||||
|
r14586 | end = q.seriesend(True) | ||
|
r9364 | |||
|
r43347 | if opts.get(b'last') and not end: | ||
ui.write(_(b"no patches applied\n")) | ||||
|
r9364 | return 1 | ||
|
r43347 | elif opts.get(b'last') and end == 1: | ||
ui.write(_(b"only one patch applied\n")) | ||||
|
r9364 | return 1 | ||
|
r43347 | elif opts.get(b'last'): | ||
|
r9364 | start = end - 2 | ||
end = 1 | ||||
else: | ||||
start = 0 | ||||
|
r43346 | q.qseries( | ||
|
r43347 | repo, length=end, start=start, status=b'A', summary=opts.get(b'summary') | ||
|
r43346 | ) | ||
@command( | ||||
|
r43347 | b"qunapplied", | ||
[(b'1', b'first', None, _(b'show only the first patch'))] + seriesopts, | ||||
_(b'hg qunapplied [-1] [-s] [PATCH]'), | ||||
|
r43346 | helpcategory=command.CATEGORY_CHANGE_ORGANIZATION, | ||
) | ||||
|
r1808 | def unapplied(ui, repo, patch=None, **opts): | ||
|
r12538 | """print the patches not yet applied | ||
Returns 0 on success.""" | ||||
|
r9364 | |||
|
r3183 | q = repo.mq | ||
|
r34506 | opts = pycompat.byteskwargs(opts) | ||
|
r3183 | if patch: | ||
if patch not in q.series: | ||||
|
r43347 | raise error.Abort(_(b"patch %s is not in series file") % patch) | ||
|
r3183 | start = q.series.index(patch) + 1 | ||
else: | ||||
|
r14586 | start = q.seriesend(True) | ||
|
r9364 | |||
|
r43347 | if start == len(q.series) and opts.get(b'first'): | ||
ui.write(_(b"all patches applied\n")) | ||||
|
r9364 | return 1 | ||
|
r43347 | if opts.get(b'first'): | ||
|
r24306 | length = 1 | ||
else: | ||||
length = None | ||||
|
r43346 | q.qseries( | ||
repo, | ||||
start=start, | ||||
length=length, | ||||
|
r43347 | status=b'U', | ||
summary=opts.get(b'summary'), | ||||
|
r43346 | ) | ||
@command( | ||||
|
r43347 | b"qimport", | ||
|
r43346 | [ | ||
|
r43347 | (b'e', b'existing', None, _(b'import file in patch directory')), | ||
(b'n', b'name', b'', _(b'name of patch file'), _(b'NAME')), | ||||
(b'f', b'force', None, _(b'overwrite existing files')), | ||||
|
r43346 | ( | ||
|
r43347 | b'r', | ||
b'rev', | ||||
|
r43346 | [], | ||
|
r43347 | _(b'place existing revisions under mq control'), | ||
_(b'REV'), | ||||
|
r43346 | ), | ||
|
r43347 | (b'g', b'git', None, _(b'use git extended diff format')), | ||
(b'P', b'push', None, _(b'qpush after importing')), | ||||
|
r43346 | ], | ||
|
r43347 | _(b'hg qimport [-e] [-n NAME] [-f] [-g] [-P] [-r REV]... [FILE]...'), | ||
|
r43346 | helpcategory=command.CATEGORY_IMPORT_EXPORT, | ||
) | ||||
|
r1808 | def qimport(ui, repo, *filename, **opts): | ||
|
r16152 | """import a patch or existing changeset | ||
|
r3141 | |||
|
r7994 | The patch is inserted into the series after the last applied | ||
patch. If no patches have been applied, qimport prepends the patch | ||||
|
r6634 | to the series. | ||
|
r3141 | The patch will have the same name as its source file unless you | ||
|
r8076 | give it a new one with -n/--name. | ||
|
r3141 | |||
|
r7994 | You can register an existing patch inside the patch directory with | ||
|
r8076 | the -e/--existing flag. | ||
|
r3141 | |||
|
r8076 | With -f/--force, an existing patch of the same name will be | ||
|
r7994 | overwritten. | ||
|
r3141 | |||
|
r8076 | An existing changeset may be placed under mq control with -r/--rev | ||
|
r19397 | (e.g. qimport --rev . -n patch will place the current revision | ||
under mq control). With -g/--git, patches imported with --rev will | ||||
use the git diff format. See the diffs help topic for information | ||||
on why this is important for preserving rename/copy information | ||||
and permission changes. Use :hg:`qfinish` to remove changesets | ||||
from mq control. | ||||
|
r8075 | |||
To import a patch from standard input, pass - as the patch file. | ||||
When importing from standard input, a patch name must be specified | ||||
using the --name flag. | ||||
|
r11700 | |||
|
r11706 | To import an existing patch while renaming it:: | ||
|
r11700 | |||
hg qimport -e existing-patch -n new-name | ||||
|
r12538 | |||
Returns 0 if import succeeded. | ||||
|
r3141 | """ | ||
|
r34506 | opts = pycompat.byteskwargs(opts) | ||
|
r43346 | with repo.lock(): # cause this may move phase | ||
|
r16027 | q = repo.mq | ||
try: | ||||
|
r16119 | imported = q.qimport( | ||
|
r43346 | repo, | ||
filename, | ||||
|
r43347 | patchname=opts.get(b'name'), | ||
existing=opts.get(b'existing'), | ||||
force=opts.get(b'force'), | ||||
rev=opts.get(b'rev'), | ||||
git=opts.get(b'git'), | ||||
|
r43346 | ) | ||
|
r16027 | finally: | ||
q.savedirty() | ||||
|
r16681 | |||
|
r43347 | if imported and opts.get(b'push') and not opts.get(b'rev'): | ||
|
r16681 | return q.push(repo, imported[-1]) | ||
|
r1808 | return 0 | ||
|
r43346 | |||
|
r10480 | def qinit(ui, repo, create): | ||
"""initialize a new queue repository | ||||
|
r2754 | |||
|
r10480 | This command also creates a series file for ordering patches, and | ||
an mq-specific .hgignore file in the queue repository, to exclude | ||||
|
r12538 | the status and guards files (these contain mostly transient state). | ||
Returns 0 if initialization succeeded.""" | ||||
|
r2724 | q = repo.mq | ||
|
r10480 | r = q.init(repo, create) | ||
|
r14580 | q.savedirty() | ||
|
r1808 | if r: | ||
|
r43347 | if not os.path.exists(r.wjoin(b'.hgignore')): | ||
fp = r.wvfs(b'.hgignore', b'w') | ||||
fp.write(b'^\\.hg\n') | ||||
fp.write(b'^\\.mq\n') | ||||
fp.write(b'syntax: glob\n') | ||||
fp.write(b'status\n') | ||||
fp.write(b'guards\n') | ||||
|
r4071 | fp.close() | ||
|
r43347 | if not os.path.exists(r.wjoin(b'series')): | ||
r.wvfs(b'series', b'w').close() | ||||
r[None].add([b'.hgignore', b'series']) | ||||
|
r4071 | commands.add(ui, r) | ||
|
r1808 | return 0 | ||
|
r43346 | |||
@command( | ||||
|
r43347 | b"qinit", | ||
[(b'c', b'create-repo', None, _(b'create queue repository'))], | ||||
_(b'hg qinit [-c]'), | ||||
|
r43346 | helpcategory=command.CATEGORY_REPO_CREATION, | ||
helpbasic=True, | ||||
) | ||||
|
r10480 | def init(ui, repo, **opts): | ||
"""init a new queue repository (DEPRECATED) | ||||
The queue repository is unversioned by default. If | ||||
-c/--create-repo is specified, qinit will create a separate nested | ||||
repository for patches (qinit -c may also be run later to convert | ||||
an unversioned patch repository into a versioned one). You can use | ||||
qcommit to commit changes to this queue repository. | ||||
This command is deprecated. Without -c, it's implied by other relevant | ||||
|
r11193 | commands. With -c, use :hg:`init --mq` instead.""" | ||
|
r43906 | return qinit(ui, repo, create=opts.get('create_repo')) | ||
|
r10480 | |||
|
r43346 | |||
@command( | ||||
|
r43347 | b"qclone", | ||
|
r43346 | [ | ||
|
r43347 | (b'', b'pull', None, _(b'use pull protocol to copy metadata')), | ||
|
r43346 | ( | ||
|
r43347 | b'U', | ||
b'noupdate', | ||||
|
r43346 | None, | ||
|
r43347 | _(b'do not update the new working directories'), | ||
|
r43346 | ), | ||
( | ||||
|
r43347 | b'', | ||
b'uncompressed', | ||||
None, | ||||
_(b'use uncompressed transfer (fast over LAN)'), | ||||
), | ||||
( | ||||
b'p', | ||||
b'patches', | ||||
b'', | ||||
_(b'location of source patch repository'), | ||||
_(b'REPO'), | ||||
|
r43346 | ), | ||
] | ||||
+ cmdutil.remoteopts, | ||||
|
r43347 | _(b'hg qclone [OPTION]... SOURCE [DEST]'), | ||
|
r43346 | helpcategory=command.CATEGORY_REPO_CREATION, | ||
norepo=True, | ||||
) | ||||
|
r2720 | def clone(ui, source, dest=None, **opts): | ||
|
r46554 | """clone main and patch repository at same time | ||
|
r2720 | |||
|
r7983 | If source is local, destination will have no patches applied. If | ||
|
r2720 | source is remote, this command can not check if patches are | ||
applied in source, so cannot guarantee that patches are not | ||||
|
r7983 | applied in destination. If you clone remote repository, be sure | ||
|
r2720 | before that it has no patches applied. | ||
Source patch repository is looked for in <src>/.hg/patches by | ||||
|
r7983 | default. Use -p <url> to change. | ||
|
r4862 | |||
|
r8760 | The patch directory must be a nested Mercurial repository, as | ||
|
r11193 | would be created by :hg:`init --mq`. | ||
|
r12538 | |||
Return 0 on success. | ||||
|
r46554 | """ | ||
|
r34506 | opts = pycompat.byteskwargs(opts) | ||
|
r43346 | |||
|
r5226 | def patchdir(repo): | ||
|
r15921 | """compute a patch repo url from a repo object""" | ||
|
r5226 | url = repo.url() | ||
|
r43347 | if url.endswith(b'/'): | ||
|
r5226 | url = url[:-1] | ||
|
r43347 | return url + b'/.hg/patches' | ||
|
r15921 | |||
# main repo (destination and sources) | ||||
|
r2720 | if dest is None: | ||
dest = hg.defaultdest(source) | ||||
r47697 | __, source_path, __ = urlutil.get_clone_path(ui, source) | |||
sr = hg.peer(ui, opts, source_path) | ||||
|
r15921 | |||
# patches repo (source only) | ||||
|
r43347 | if opts.get(b'patches'): | ||
r47697 | __, patchespath, __ = urlutil.get_clone_path(ui, opts.get(b'patches')) | |||
|
r7729 | else: | ||
patchespath = patchdir(sr) | ||||
|
r4862 | try: | ||
|
r17191 | hg.peer(ui, opts, patchespath) | ||
|
r7637 | except error.RepoError: | ||
|
r43346 | raise error.Abort( | ||
|
r43387 | _(b'versioned patch repository not found (see init --mq)') | ||
|
r43346 | ) | ||
|
r2720 | qbase, destrev = None, None | ||
if sr.local(): | ||||
|
r17191 | repo = sr.local() | ||
if repo.mq.applied and repo[qbase].phase() != phases.secret: | ||||
qbase = repo.mq.applied[0].node | ||||
|
r2720 | if not hg.islocal(dest): | ||
|
r17191 | heads = set(repo.heads()) | ||
destrev = list(heads.difference(repo.heads(qbase))) | ||||
destrev.append(repo.changelog.parents(qbase)[0]) | ||||
|
r43347 | elif sr.capable(b'lookup'): | ||
|
r6380 | try: | ||
|
r43347 | qbase = sr.lookup(b'qbase') | ||
|
r7637 | except error.RepoError: | ||
|
r6380 | pass | ||
|
r15921 | |||
|
r43347 | ui.note(_(b'cloning main repository\n')) | ||
|
r43346 | sr, dr = hg.clone( | ||
ui, | ||||
opts, | ||||
sr.url(), | ||||
dest, | ||||
|
r43347 | pull=opts.get(b'pull'), | ||
|
r43346 | revs=destrev, | ||
update=False, | ||||
|
r43347 | stream=opts.get(b'uncompressed'), | ||
|
r43346 | ) | ||
|
r15921 | |||
|
r43347 | ui.note(_(b'cloning patch repository\n')) | ||
|
r43346 | hg.clone( | ||
ui, | ||||
opts, | ||||
|
r43347 | opts.get(b'patches') or patchdir(sr), | ||
|
r43346 | patchdir(dr), | ||
|
r43347 | pull=opts.get(b'pull'), | ||
update=not opts.get(b'noupdate'), | ||||
stream=opts.get(b'uncompressed'), | ||||
|
r43346 | ) | ||
|
r15921 | |||
|
r2720 | if dr.local(): | ||
|
r17191 | repo = dr.local() | ||
|
r2720 | if qbase: | ||
|
r43346 | ui.note( | ||
|
r43347 | _( | ||
b'stripping applied patches from destination ' | ||||
b'repository\n' | ||||
) | ||||
|
r43346 | ) | ||
|
r19819 | strip(ui, repo, [qbase], update=False, backup=None) | ||
|
r43347 | if not opts.get(b'noupdate'): | ||
ui.note(_(b'updating destination repository\n')) | ||||
|
r17191 | hg.update(repo, repo.changelog.tip()) | ||
|
r2720 | |||
|
r43346 | |||
@command( | ||||
|
r43347 | b"qcommit|qci", | ||
commands.table[b"commit|ci"][1], | ||||
_(b'hg qcommit [OPTION]... [FILE]...'), | ||||
|
r43346 | helpcategory=command.CATEGORY_COMMITTING, | ||
inferrepo=True, | ||||
) | ||||
|
r1808 | def commit(ui, repo, *pats, **opts): | ||
|
r10361 | """commit changes in the queue repository (DEPRECATED) | ||
|
r11193 | This command is deprecated; use :hg:`commit --mq` instead.""" | ||
|
r2724 | q = repo.mq | ||
|
r1808 | r = q.qrepo() | ||
|
r10282 | if not r: | ||
|
r43347 | raise error.Abort(b'no queue repository') | ||
|
r1808 | commands.commit(r.ui, r, *pats, **opts) | ||
|
r43346 | |||
@command( | ||||
|
r43347 | b"qseries", | ||
|
r46554 | [ | ||
(b'm', b'missing', None, _(b'print patches not in series')), | ||||
] | ||||
+ seriesopts, | ||||
|
r43347 | _(b'hg qseries [-ms]'), | ||
|
r43346 | helpcategory=command.CATEGORY_CHANGE_ORGANIZATION, | ||
) | ||||
|
r1808 | def series(ui, repo, **opts): | ||
|
r12538 | """print the entire series file | ||
Returns 0 on success.""" | ||||
|
r43346 | repo.mq.qseries( | ||
|
r43906 | repo, missing=opts.get('missing'), summary=opts.get('summary') | ||
|
r43346 | ) | ||
|
r1808 | return 0 | ||
|
r43346 | |||
@command( | ||||
|
r43347 | b"qtop", | ||
|
r43346 | seriesopts, | ||
|
r43347 | _(b'hg qtop [-s]'), | ||
|
r43346 | helpcategory=command.CATEGORY_CHANGE_ORGANIZATION, | ||
) | ||||
|
r1808 | def top(ui, repo, **opts): | ||
|
r12538 | """print the name of the current patch | ||
Returns 0 on success.""" | ||||
|
r3183 | q = repo.mq | ||
|
r24306 | if q.applied: | ||
t = q.seriesend(True) | ||||
else: | ||||
t = 0 | ||||
|
r3183 | if t: | ||
|
r43346 | q.qseries( | ||
repo, | ||||
start=t - 1, | ||||
length=1, | ||||
|
r43347 | status=b'A', | ||
|
r43906 | summary=opts.get('summary'), | ||
|
r43346 | ) | ||
|
r3183 | else: | ||
|
r43347 | ui.write(_(b"no patches applied\n")) | ||
|
r3183 | return 1 | ||
|
r1808 | |||
|
r43346 | |||
@command( | ||||
|
r43347 | b"qnext", | ||
|
r43346 | seriesopts, | ||
|
r43347 | _(b'hg qnext [-s]'), | ||
|
r43346 | helpcategory=command.CATEGORY_CHANGE_ORGANIZATION, | ||
) | ||||
|
r1808 | def next(ui, repo, **opts): | ||
|
r16063 | """print the name of the next pushable patch | ||
|
r12538 | |||
Returns 0 on success.""" | ||||
|
r3183 | q = repo.mq | ||
|
r14586 | end = q.seriesend() | ||
|
r3183 | if end == len(q.series): | ||
|
r43347 | ui.write(_(b"all patches applied\n")) | ||
|
r3183 | return 1 | ||
|
r43906 | q.qseries(repo, start=end, length=1, summary=opts.get('summary')) | ||
|
r1808 | |||
|
r43346 | |||
@command( | ||||
|
r43347 | b"qprev", | ||
|
r43346 | seriesopts, | ||
|
r43347 | _(b'hg qprev [-s]'), | ||
|
r43346 | helpcategory=command.CATEGORY_CHANGE_ORGANIZATION, | ||
) | ||||
|
r1808 | def prev(ui, repo, **opts): | ||
|
r16188 | """print the name of the preceding applied patch | ||
|
r12538 | |||
Returns 0 on success.""" | ||||
|
r3183 | q = repo.mq | ||
l = len(q.applied) | ||||
if l == 1: | ||||
|
r43347 | ui.write(_(b"only one patch applied\n")) | ||
|
r3183 | return 1 | ||
if not l: | ||||
|
r43347 | ui.write(_(b"no patches applied\n")) | ||
|
r3183 | return 1 | ||
|
r16064 | idx = q.series.index(q.applied[-2].name) | ||
|
r43346 | q.qseries( | ||
|
r43906 | repo, start=idx, length=1, status=b'A', summary=opts.get('summary') | ||
|
r43346 | ) | ||
|
r1808 | |||
|
r5673 | def setupheaderopts(ui, opts): | ||
|
r43347 | if not opts.get(b'user') and opts.get(b'currentuser'): | ||
opts[b'user'] = ui.username() | ||||
if not opts.get(b'date') and opts.get(b'currentdate'): | ||||
opts[b'date'] = b"%d %d" % dateutil.makedate() | ||||
|
r5673 | |||
|
r43346 | |||
@command( | ||||
|
r43347 | b"qnew", | ||
|
r43346 | [ | ||
|
r43347 | (b'e', b'edit', None, _(b'invoke editor on commit messages')), | ||
(b'f', b'force', None, _(b'import uncommitted changes (DEPRECATED)')), | ||||
(b'g', b'git', None, _(b'use git extended diff format')), | ||||
(b'U', b'currentuser', None, _(b'add "From: <current user>" to patch')), | ||||
(b'u', b'user', b'', _(b'add "From: <USER>" to patch'), _(b'USER')), | ||||
(b'D', b'currentdate', None, _(b'add "Date: <current date>" to patch')), | ||||
(b'd', b'date', b'', _(b'add "Date: <DATE>" to patch'), _(b'DATE')), | ||||
|
r43346 | ] | ||
+ cmdutil.walkopts | ||||
+ cmdutil.commitopts, | ||||
|
r43347 | _(b'hg qnew [-e] [-m TEXT] [-l FILE] PATCH [FILE]...'), | ||
|
r43346 | helpcategory=command.CATEGORY_COMMITTING, | ||
helpbasic=True, | ||||
inferrepo=True, | ||||
) | ||||
|
r4713 | def new(ui, repo, patch, *args, **opts): | ||
|
r2754 | """create a new patch | ||
|
r7994 | qnew creates a new patch on top of the currently-applied patch (if | ||
|
r10808 | any). The patch will be initialized with any outstanding changes | ||
in the working directory. You may also use -I/--include, | ||||
|
r8076 | -X/--exclude, and/or a list of files after the patch name to add | ||
only changes to matching files to the new patch, leaving the rest | ||||
as uncommitted modifications. | ||||
|
r2754 | |||
|
r8076 | -u/--user and -d/--date can be used to set the (given) user and | ||
date, respectively. -U/--currentuser and -D/--currentdate set user | ||||
to current user and date to current date. | ||||
|
r7306 | |||
|
r8076 | -e/--edit, -m/--message or -l/--logfile set the patch header as | ||
well as the commit message. If none is specified, the header is | ||||
empty and the commit message is '[mq]: PATCH'. | ||||
|
r7307 | |||
|
r8076 | Use the -g/--git option to keep the patch in the git extended diff | ||
|
r7387 | format. Read the diffs help topic for more information on why this | ||
is important for preserving permission changes and copy/rename | ||||
information. | ||||
|
r12538 | |||
Returns 0 on successful creation of a new patch. | ||||
|
r7306 | """ | ||
|
r34506 | opts = pycompat.byteskwargs(opts) | ||
|
r14635 | msg = cmdutil.logmessage(ui, opts) | ||
|
r2724 | q = repo.mq | ||
|
r43347 | opts[b'msg'] = msg | ||
|
r5673 | setupheaderopts(ui, opts) | ||
|
r34506 | q.new(repo, patch, *args, **pycompat.strkwargs(opts)) | ||
|
r14580 | q.savedirty() | ||
|
r1808 | return 0 | ||
|
r43346 | |||
@command( | ||||
|
r43347 | b"qrefresh", | ||
|
r43346 | [ | ||
|
r43347 | (b'e', b'edit', None, _(b'invoke editor on commit messages')), | ||
(b'g', b'git', None, _(b'use git extended diff format')), | ||||
|
r43346 | ( | ||
|
r43347 | b's', | ||
b'short', | ||||
|
r43346 | None, | ||
|
r43347 | _(b'refresh only files already in the patch and specified files'), | ||
|
r43346 | ), | ||
( | ||||
|
r43347 | b'U', | ||
b'currentuser', | ||||
|
r43346 | None, | ||
|
r43347 | _(b'add/update author field in patch with current user'), | ||
|
r43346 | ), | ||
( | ||||
|
r43347 | b'u', | ||
b'user', | ||||
b'', | ||||
_(b'add/update author field in patch with given user'), | ||||
_(b'USER'), | ||||
|
r43346 | ), | ||
( | ||||
|
r43347 | b'D', | ||
b'currentdate', | ||||
|
r43346 | None, | ||
|
r43347 | _(b'add/update date field in patch with current date'), | ||
|
r43346 | ), | ||
( | ||||
|
r43347 | b'd', | ||
b'date', | ||||
b'', | ||||
_(b'add/update date field in patch with given date'), | ||||
_(b'DATE'), | ||||
|
r43346 | ), | ||
] | ||||
+ cmdutil.walkopts | ||||
+ cmdutil.commitopts, | ||||
|
r43347 | _(b'hg qrefresh [-I] [-X] [-e] [-m TEXT] [-l FILE] [-s] [FILE]...'), | ||
|
r43346 | helpcategory=command.CATEGORY_COMMITTING, | ||
helpbasic=True, | ||||
inferrepo=True, | ||||
) | ||||
|
r2938 | def refresh(ui, repo, *pats, **opts): | ||
|
r2940 | """update the current patch | ||
|
r7994 | If any file patterns are provided, the refreshed patch will | ||
contain only the modifications that match those patterns; the | ||||
remaining modifications will remain in the working directory. | ||||
|
r4048 | |||
|
r8076 | If -s/--short is specified, files currently included in the patch | ||
|
r7994 | will be refreshed just like matched files and remain in the patch. | ||
|
r7113 | |||
|
r11947 | If -e/--edit is specified, Mercurial will start your configured editor for | ||
you to enter a message. In case qrefresh fails, you will find a backup of | ||||
your message in ``.hg/last-message.txt``. | ||||
|
r7994 | hg add/remove/copy/rename work as usual, though you might want to | ||
|
r8076 | use git-style patches (-g/--git or [diff] git=1) to track copies | ||
and renames. See the diffs help topic for more information on the | ||||
git diff format. | ||||
|
r12538 | |||
Returns 0 on success. | ||||
|
r2940 | """ | ||
|
r34506 | opts = pycompat.byteskwargs(opts) | ||
|
r2724 | q = repo.mq | ||
|
r14635 | message = cmdutil.logmessage(ui, opts) | ||
|
r5673 | setupheaderopts(ui, opts) | ||
|
r27830 | with repo.wlock(): | ||
|
r34506 | ret = q.refresh(repo, pats, msg=message, **pycompat.strkwargs(opts)) | ||
|
r14620 | q.savedirty() | ||
return ret | ||||
|
r1808 | |||
|
r43346 | |||
@command( | ||||
|
r43347 | b"qdiff", | ||
|
r43346 | cmdutil.diffopts + cmdutil.diffopts2 + cmdutil.walkopts, | ||
|
r43347 | _(b'hg qdiff [OPTION]... [FILE]...'), | ||
|
r43346 | helpcategory=command.CATEGORY_FILE_CONTENTS, | ||
helpbasic=True, | ||||
inferrepo=True, | ||||
) | ||||
|
r2937 | def diff(ui, repo, *pats, **opts): | ||
|
r6606 | """diff of the current patch and subsequent modifications | ||
|
r6621 | |||
|
r7994 | Shows a diff which includes the current patch as well as any | ||
changes which have been made in the working directory since the | ||||
last refresh (thus showing what the current patch would become | ||||
after a qrefresh). | ||||
|
r6621 | |||
|
r10973 | Use :hg:`diff` if you only want to see the changes made since the | ||
last qrefresh, or :hg:`export qtip` if you want to see changes | ||||
made by the current patch without including changes made since the | ||||
|
r7994 | qrefresh. | ||
|
r12538 | |||
Returns 0 on success. | ||||
|
r6606 | """ | ||
|
r43347 | ui.pager(b'qdiff') | ||
|
r34506 | repo.mq.diff(repo, pats, pycompat.byteskwargs(opts)) | ||
|
r1808 | return 0 | ||
|
r43346 | |||
@command( | ||||
|
r43347 | b'qfold', | ||
|
r43346 | [ | ||
|
r43347 | (b'e', b'edit', None, _(b'invoke editor on commit messages')), | ||
(b'k', b'keep', None, _(b'keep folded patch files')), | ||||
|
r43346 | ] | ||
+ cmdutil.commitopts, | ||||
|
r43347 | _(b'hg qfold [-e] [-k] [-m TEXT] [-l FILE] PATCH...'), | ||
|
r43346 | helpcategory=command.CATEGORY_CHANGE_MANAGEMENT, | ||
) | ||||
|
r2753 | def fold(ui, repo, *files, **opts): | ||
|
r2748 | """fold the named patches into the current patch | ||
|
r2753 | |||
|
r2771 | Patches must not yet be applied. Each patch will be successively | ||
applied to the current patch in the order given. If all the | ||||
patches apply successfully, the current patch will be refreshed | ||||
|
r7994 | with the new cumulative patch, and the folded patches will be | ||
deleted. With -k/--keep, the folded patch files will not be | ||||
removed afterwards. | ||||
|
r2771 | |||
|
r7994 | The header for each folded patch will be concatenated with the | ||
|
r12755 | current patch header, separated by a line of ``* * *``. | ||
|
r12538 | |||
Returns 0 on success.""" | ||||
|
r34506 | opts = pycompat.byteskwargs(opts) | ||
|
r2748 | q = repo.mq | ||
if not files: | ||||
|
r43347 | raise error.Abort(_(b'qfold requires at least one patch name')) | ||
|
r14581 | if not q.checktoppatch(repo)[0]: | ||
|
r43347 | raise error.Abort(_(b'no patches applied')) | ||
|
r14583 | q.checklocalchanges(repo) | ||
|
r2748 | |||
|
r14635 | message = cmdutil.logmessage(ui, opts) | ||
|
r2753 | |||
|
r43347 | parent = q.lookup(b'qtip') | ||
|
r2748 | patches = [] | ||
messages = [] | ||||
for f in files: | ||||
|
r2936 | p = q.lookup(f) | ||
if p in patches or p == parent: | ||||
|
r43347 | ui.warn(_(b'skipping already folded patch %s\n') % p) | ||
|
r2936 | if q.isapplied(p): | ||
|
r43346 | raise error.Abort( | ||
|
r43347 | _(b'qfold cannot fold already applied patch %s') % p | ||
|
r43346 | ) | ||
|
r2936 | patches.append(p) | ||
|
r2748 | |||
|
r2936 | for p in patches: | ||
|
r2753 | if not message: | ||
|
r10397 | ph = patchheader(q.join(p), q.plainmode) | ||
|
r7454 | if ph.message: | ||
messages.append(ph.message) | ||||
|
r2936 | pf = q.join(p) | ||
|
r2748 | (patchsuccess, files, fuzz) = q.patch(repo, pf) | ||
if not patchsuccess: | ||||
|
r43347 | raise error.Abort(_(b'error folding patch %s') % p) | ||
|
r2748 | |||
|
r2753 | if not message: | ||
|
r10397 | ph = patchheader(q.join(parent), q.plainmode) | ||
|
r21270 | message = ph.message | ||
|
r2753 | for msg in messages: | ||
|
r20053 | if msg: | ||
if message: | ||||
|
r43347 | message.append(b'* * *') | ||
|
r20053 | message.extend(msg) | ||
|
r43347 | message = b'\n'.join(message) | ||
|
r2753 | |||
|
r10186 | diffopts = q.patchopts(q.diffopts(), *patches) | ||
|
r27831 | with repo.wlock(): | ||
|
r43346 | q.refresh( | ||
repo, | ||||
msg=message, | ||||
git=diffopts.git, | ||||
|
r43347 | edit=opts.get(b'edit'), | ||
editform=b'mq.qfold', | ||||
|
r43346 | ) | ||
|
r14620 | q.delete(repo, patches, opts) | ||
q.savedirty() | ||||
|
r2748 | |||
|
r43346 | |||
@command( | ||||
|
r43347 | b"qgoto", | ||
|
r43346 | [ | ||
|
r43347 | ( | ||
b'', | ||||
b'keep-changes', | ||||
None, | ||||
_(b'tolerate non-conflicting local changes'), | ||||
), | ||||
(b'f', b'force', None, _(b'overwrite any local changes')), | ||||
(b'', b'no-backup', None, _(b'do not save backup copies of files')), | ||||
|
r43346 | ], | ||
|
r43347 | _(b'hg qgoto [OPTION]... PATCH'), | ||
|
r43346 | helpcategory=command.CATEGORY_CHANGE_ORGANIZATION, | ||
) | ||||
|
r4432 | def goto(ui, repo, patch, **opts): | ||
|
r46554 | """push or pop patches until named patch is at top of stack | ||
Returns 0 on success.""" | ||||
|
r34506 | opts = pycompat.byteskwargs(opts) | ||
|
r16733 | opts = fixkeepchangesopts(ui, opts) | ||
|
r4432 | q = repo.mq | ||
patch = q.lookup(patch) | ||||
|
r43347 | nobackup = opts.get(b'no_backup') | ||
keepchanges = opts.get(b'keep_changes') | ||||
|
r4432 | if q.isapplied(patch): | ||
|
r43346 | ret = q.pop( | ||
repo, | ||||
patch, | ||||
|
r43347 | force=opts.get(b'force'), | ||
|
r43346 | nobackup=nobackup, | ||
keepchanges=keepchanges, | ||||
) | ||||
|
r4432 | else: | ||
|
r43346 | ret = q.push( | ||
repo, | ||||
patch, | ||||
|
r43347 | force=opts.get(b'force'), | ||
|
r43346 | nobackup=nobackup, | ||
keepchanges=keepchanges, | ||||
) | ||||
|
r14580 | q.savedirty() | ||
|
r4432 | return ret | ||
|
r43346 | |||
@command( | ||||
|
r43347 | b"qguard", | ||
|
r43346 | [ | ||
|
r43347 | (b'l', b'list', None, _(b'list all patches and guards')), | ||
(b'n', b'none', None, _(b'drop all guards')), | ||||
|
r43346 | ], | ||
|
r43347 | _(b'hg qguard [-l] [-n] [PATCH] [-- [+GUARD]... [-GUARD]...]'), | ||
|
r43346 | helpcategory=command.CATEGORY_CHANGE_ORGANIZATION, | ||
) | ||||
|
r2821 | def guard(ui, repo, *args, **opts): | ||
|
r46554 | """set or print guards for a patch | ||
|
r2821 | |||
|
r2940 | Guards control whether a patch can be pushed. A patch with no | ||
guards is always pushed. A patch with a positive guard ("+foo") is | ||||
|
r11307 | pushed only if the :hg:`qselect` command has activated it. A patch with | ||
a negative guard ("-foo") is never pushed if the :hg:`qselect` command | ||||
|
r2940 | has activated it. | ||
|
r2821 | |||
|
r2940 | With no arguments, print the currently active guards. | ||
With arguments, set guards for the named patch. | ||||
|
r12389 | |||
.. note:: | ||||
|
r19997 | |||
|
r12389 | Specifying negative guards now requires '--'. | ||
|
r2821 | |||
|
r9824 | To set guards on another patch:: | ||
|
r10476 | hg qguard other.patch -- +2.6.17 -stable | ||
|
r12538 | |||
Returns 0 on success. | ||||
|
r46554 | """ | ||
|
r43346 | |||
|
r2821 | def status(idx): | ||
|
r43347 | guards = q.seriesguards[idx] or [b'unguarded'] | ||
|
r11819 | if q.series[idx] in applied: | ||
|
r43347 | state = b'applied' | ||
|
r11819 | elif q.pushable(idx)[0]: | ||
|
r43347 | state = b'unapplied' | ||
|
r11819 | else: | ||
|
r43347 | state = b'guarded' | ||
label = b'qguard.patch qguard.%s qseries.%s' % (state, state) | ||||
ui.write(b'%s: ' % ui.label(q.series[idx], label)) | ||||
|
r11819 | |||
|
r10822 | for i, guard in enumerate(guards): | ||
|
r43347 | if guard.startswith(b'+'): | ||
ui.write(guard, label=b'qguard.positive') | ||||
elif guard.startswith(b'-'): | ||||
ui.write(guard, label=b'qguard.negative') | ||||
|
r10822 | else: | ||
|
r43347 | ui.write(guard, label=b'qguard.unguarded') | ||
|
r10822 | if i != len(guards) - 1: | ||
|
r43347 | ui.write(b' ') | ||
ui.write(b'\n') | ||||
|
r43346 | |||
|
r2821 | q = repo.mq | ||
|
r44937 | applied = {p.name for p in q.applied} | ||
|
r2821 | patch = None | ||
args = list(args) | ||||
|
r43906 | if opts.get('list'): | ||
if args or opts.get('none'): | ||||
|
r43346 | raise error.Abort( | ||
|
r43387 | _(b'cannot mix -l/--list with options or arguments') | ||
|
r43346 | ) | ||
|
r38806 | for i in pycompat.xrange(len(q.series)): | ||
|
r2821 | status(i) | ||
return | ||||
|
r43347 | if not args or args[0][0:1] in b'-+': | ||
|
r2821 | if not q.applied: | ||
|
r43347 | raise error.Abort(_(b'no patches applied')) | ||
|
r2821 | patch = q.applied[-1].name | ||
|
r43347 | if patch is None and args[0][0:1] not in b'-+': | ||
|
r2821 | patch = args.pop(0) | ||
if patch is None: | ||||
|
r43347 | raise error.Abort(_(b'no patch to work with')) | ||
|
r43906 | if args or opts.get('none'): | ||
|
r14574 | idx = q.findseries(patch) | ||
|
r4133 | if idx is None: | ||
|
r43347 | raise error.Abort(_(b'no patch named %s') % patch) | ||
|
r14577 | q.setguards(idx, args) | ||
|
r14580 | q.savedirty() | ||
|
r2821 | else: | ||
status(q.series.index(q.lookup(patch))) | ||||
|
r43346 | |||
@command( | ||||
|
r43347 | b"qheader", | ||
|
r43346 | [], | ||
|
r43347 | _(b'hg qheader [PATCH]'), | ||
|
r43346 | helpcategory=command.CATEGORY_CHANGE_ORGANIZATION, | ||
) | ||||
|
r2747 | def header(ui, repo, patch=None): | ||
|
r12538 | """print the header of the topmost or specified patch | ||
Returns 0 on success.""" | ||||
|
r2747 | q = repo.mq | ||
if patch: | ||||
patch = q.lookup(patch) | ||||
else: | ||||
if not q.applied: | ||||
|
r43347 | ui.write(_(b'no patches applied\n')) | ||
|
r3008 | return 1 | ||
|
r43347 | patch = q.lookup(b'qtip') | ||
|
r10397 | ph = patchheader(q.join(patch), q.plainmode) | ||
|
r2747 | |||
|
r43347 | ui.write(b'\n'.join(ph.message) + b'\n') | ||
|
r2747 | |||
|
r43346 | |||
|
r1808 | def lastsavename(path): | ||
|
r2794 | (directory, base) = os.path.split(path) | ||
names = os.listdir(directory) | ||||
|
r43347 | namere = re.compile(b"%s.([0-9]+)" % base) | ||
|
r2794 | maxindex = None | ||
|
r1808 | maxname = None | ||
for f in names: | ||||
m = namere.match(f) | ||||
if m: | ||||
index = int(m.group(1)) | ||||
|
r8527 | if maxindex is None or index > maxindex: | ||
|
r2794 | maxindex = index | ||
|
r1808 | maxname = f | ||
if maxname: | ||||
|
r2794 | return (os.path.join(directory, maxname), maxindex) | ||
|
r1808 | return (None, None) | ||
|
r1810 | |||
|
r43346 | |||
|
r1808 | def savename(path): | ||
(last, index) = lastsavename(path) | ||||
if last is None: | ||||
index = 0 | ||||
|
r43347 | newpath = path + b".%d" % (index + 1) | ||
|
r1808 | return newpath | ||
|
r43346 | |||
@command( | ||||
|
r43347 | b"qpush", | ||
|
r43346 | [ | ||
( | ||||
|
r43347 | b'', | ||
b'keep-changes', | ||||
None, | ||||
_(b'tolerate non-conflicting local changes'), | ||||
), | ||||
(b'f', b'force', None, _(b'apply on top of local changes')), | ||||
( | ||||
b'e', | ||||
b'exact', | ||||
|
r43346 | None, | ||
|
r43347 | _(b'apply the target patch to its recorded parent'), | ||
|
r43346 | ), | ||
|
r43347 | (b'l', b'list', None, _(b'list patch name in commit text')), | ||
(b'a', b'all', None, _(b'apply all patches')), | ||||
(b'm', b'merge', None, _(b'merge from another queue (DEPRECATED)')), | ||||
(b'n', b'name', b'', _(b'merge queue name (DEPRECATED)'), _(b'NAME')), | ||||
( | ||||
b'', | ||||
b'move', | ||||
None, | ||||
_(b'reorder patch series and apply only the patch'), | ||||
), | ||||
(b'', b'no-backup', None, _(b'do not save backup copies of files')), | ||||
|
r43346 | ], | ||
|
r43347 | _(b'hg qpush [-f] [-l] [-a] [--move] [PATCH | INDEX]'), | ||
|
r43346 | helpcategory=command.CATEGORY_CHANGE_ORGANIZATION, | ||
helpbasic=True, | ||||
) | ||||
|
r1808 | def push(ui, repo, patch=None, **opts): | ||
|
r6552 | """push the next patch onto the stack | ||
|
r6553 | |||
|
r16654 | By default, abort if the working directory contains uncommitted | ||
|
r16733 | changes. With --keep-changes, abort only if the uncommitted files | ||
|
r16654 | overlap with patched files. With -f/--force, backup and patch over | ||
uncommitted changes. | ||||
|
r12538 | |||
|
r13725 | Return 0 on success. | ||
|
r6552 | """ | ||
|
r2724 | q = repo.mq | ||
|
r1808 | mergeq = None | ||
|
r1810 | |||
|
r34506 | opts = pycompat.byteskwargs(opts) | ||
|
r16733 | opts = fixkeepchangesopts(ui, opts) | ||
|
r43347 | if opts.get(b'merge'): | ||
if opts.get(b'name'): | ||||
newpath = repo.vfs.join(opts.get(b'name')) | ||||
|
r1808 | else: | ||
|
r1810 | newpath, i = lastsavename(q.path) | ||
|
r1808 | if not newpath: | ||
|
r43347 | ui.warn(_(b"no saved queues found, please use -n\n")) | ||
|
r1808 | return 1 | ||
|
r19064 | mergeq = queue(ui, repo.baseui, repo.path, newpath) | ||
|
r43347 | ui.warn(_(b"merging with queue at: %s\n") % mergeq.path) | ||
|
r43346 | ret = q.push( | ||
repo, | ||||
patch, | ||||
|
r43347 | force=opts.get(b'force'), | ||
list=opts.get(b'list'), | ||||
|
r43346 | mergeq=mergeq, | ||
|
r43347 | all=opts.get(b'all'), | ||
move=opts.get(b'move'), | ||||
exact=opts.get(b'exact'), | ||||
nobackup=opts.get(b'no_backup'), | ||||
keepchanges=opts.get(b'keep_changes'), | ||||
|
r43346 | ) | ||
|
r1808 | return ret | ||
|
r43346 | |||
@command( | ||||
|
r43347 | b"qpop", | ||
|
r43346 | [ | ||
|
r43347 | (b'a', b'all', None, _(b'pop all patches')), | ||
(b'n', b'name', b'', _(b'queue name to pop (DEPRECATED)'), _(b'NAME')), | ||||
( | ||||
b'', | ||||
b'keep-changes', | ||||
None, | ||||
_(b'tolerate non-conflicting local changes'), | ||||
), | ||||
(b'f', b'force', None, _(b'forget any local changes to patched files')), | ||||
(b'', b'no-backup', None, _(b'do not save backup copies of files')), | ||||
|
r43346 | ], | ||
|
r43347 | _(b'hg qpop [-a] [-f] [PATCH | INDEX]'), | ||
|
r43346 | helpcategory=command.CATEGORY_CHANGE_ORGANIZATION, | ||
helpbasic=True, | ||||
) | ||||
|
r1808 | def pop(ui, repo, patch=None, **opts): | ||
|
r6611 | """pop the current patch off the stack | ||
|
r6621 | |||
|
r16653 | Without argument, pops off the top of the patch stack. If given a | ||
patch name, keeps popping off patches until the named patch is at | ||||
the top of the stack. | ||||
By default, abort if the working directory contains uncommitted | ||||
|
r16733 | changes. With --keep-changes, abort only if the uncommitted files | ||
|
r16653 | overlap with patched files. With -f/--force, backup and discard | ||
changes made to such files. | ||||
|
r12538 | |||
Return 0 on success. | ||||
|
r6611 | """ | ||
|
r34506 | opts = pycompat.byteskwargs(opts) | ||
|
r16733 | opts = fixkeepchangesopts(ui, opts) | ||
|
r1808 | localupdate = True | ||
|
r43347 | if opts.get(b'name'): | ||
q = queue(ui, repo.baseui, repo.path, repo.vfs.join(opts.get(b'name'))) | ||||
ui.warn(_(b'using patch queue: %s\n') % q.path) | ||||
|
r1808 | localupdate = False | ||
else: | ||||
|
r2724 | q = repo.mq | ||
|
r43346 | ret = q.pop( | ||
repo, | ||||
patch, | ||||
|
r43347 | force=opts.get(b'force'), | ||
|
r43346 | update=localupdate, | ||
|
r43347 | all=opts.get(b'all'), | ||
nobackup=opts.get(b'no_backup'), | ||||
keepchanges=opts.get(b'keep_changes'), | ||||
|
r43346 | ) | ||
|
r14580 | q.savedirty() | ||
|
r4099 | return ret | ||
|
r1808 | |||
|
r43346 | |||
@command( | ||||
|
r43347 | b"qrename|qmv", | ||
|
r43346 | [], | ||
|
r43347 | _(b'hg qrename PATCH1 [PATCH2]'), | ||
|
r43346 | helpcategory=command.CATEGORY_CHANGE_ORGANIZATION, | ||
) | ||||
|
r2750 | def rename(ui, repo, patch, name=None, **opts): | ||
"""rename a patch | ||||
With one argument, renames the current patch to PATCH1. | ||||
|
r12538 | With two arguments, renames PATCH1 to PATCH2. | ||
Returns 0 on success.""" | ||||
|
r2750 | q = repo.mq | ||
if not name: | ||||
name = patch | ||||
patch = None | ||||
if patch: | ||||
patch = q.lookup(patch) | ||||
else: | ||||
if not q.applied: | ||||
|
r43347 | ui.write(_(b'no patches applied\n')) | ||
|
r2750 | return | ||
|
r43347 | patch = q.lookup(b'qtip') | ||
|
r3083 | absdest = q.join(name) | ||
if os.path.isdir(absdest): | ||||
|
r4037 | name = normname(os.path.join(name, os.path.basename(patch))) | ||
|
r3083 | absdest = q.join(name) | ||
|
r14423 | q.checkpatchname(name) | ||
|
r2750 | |||
|
r43347 | ui.note(_(b'renaming %s to %s\n') % (patch, name)) | ||
|
r14574 | i = q.findseries(patch) | ||
|
r14572 | guards = q.guard_re.findall(q.fullseries[i]) | ||
|
r43347 | q.fullseries[i] = name + b''.join([b' #' + g for g in guards]) | ||
|
r14575 | q.parseseries() | ||
|
r15879 | q.seriesdirty = True | ||
|
r2750 | |||
info = q.isapplied(patch) | ||||
if info: | ||||
|
r2818 | q.applied[info[0]] = statusentry(info[1], name) | ||
|
r15879 | q.applieddirty = True | ||
|
r2750 | |||
|
r11513 | destdir = os.path.dirname(absdest) | ||
if not os.path.isdir(destdir): | ||||
os.makedirs(destdir) | ||||
|
r2819 | util.rename(q.join(patch), absdest) | ||
|
r2750 | r = q.qrepo() | ||
|
r12875 | if r and patch in r.dirstate: | ||
|
r11303 | wctx = r[None] | ||
|
r27848 | with r.wlock(): | ||
|
r43347 | if r.dirstate[patch] == b'a': | ||
r48401 | r.dirstate.set_untracked(patch) | |||
r48398 | r.dirstate.set_tracked(name) | |||
|
r6648 | else: | ||
|
r11303 | wctx.copy(patch, name) | ||
|
r14435 | wctx.forget([patch]) | ||
|
r2750 | |||
|
r14580 | q.savedirty() | ||
|
r2750 | |||
|
r43346 | |||
@command( | ||||
|
r43347 | b"qrestore", | ||
|
r43346 | [ | ||
|
r43347 | (b'd', b'delete', None, _(b'delete save entry')), | ||
(b'u', b'update', None, _(b'update queue working directory')), | ||||
|
r43346 | ], | ||
|
r43347 | _(b'hg qrestore [-d] [-u] REV'), | ||
|
r43346 | helpcategory=command.CATEGORY_CHANGE_ORGANIZATION, | ||
) | ||||
|
r1808 | def restore(ui, repo, rev, **opts): | ||
|
r10360 | """restore the queue state saved by a revision (DEPRECATED) | ||
|
r12352 | This command is deprecated, use :hg:`rebase` instead.""" | ||
|
r1808 | rev = repo.lookup(rev) | ||
|
r2724 | q = repo.mq | ||
|
r43906 | q.restore(repo, rev, delete=opts.get('delete'), qupdate=opts.get('update')) | ||
|
r14580 | q.savedirty() | ||
|
r1808 | return 0 | ||
|
r43346 | |||
@command( | ||||
|
r43347 | b"qsave", | ||
|
r43346 | [ | ||
|
r43347 | (b'c', b'copy', None, _(b'copy patch directory')), | ||
(b'n', b'name', b'', _(b'copy directory name'), _(b'NAME')), | ||||
(b'e', b'empty', None, _(b'clear queue status file')), | ||||
(b'f', b'force', None, _(b'force copy')), | ||||
|
r43346 | ] | ||
+ cmdutil.commitopts, | ||||
|
r43347 | _(b'hg qsave [-m TEXT] [-l FILE] [-c] [-n NAME] [-e] [-f]'), | ||
|
r43346 | helpcategory=command.CATEGORY_CHANGE_ORGANIZATION, | ||
) | ||||
|
r1808 | def save(ui, repo, **opts): | ||
|
r10360 | """save current queue state (DEPRECATED) | ||
|
r12352 | This command is deprecated, use :hg:`rebase` instead.""" | ||
|
r2724 | q = repo.mq | ||
|
r34506 | opts = pycompat.byteskwargs(opts) | ||
|
r14635 | message = cmdutil.logmessage(ui, opts) | ||
|
r2694 | ret = q.save(repo, msg=message) | ||
|
r1808 | if ret: | ||
return ret | ||||
|
r43346 | q.savedirty() # save to .hg/patches before copying | ||
|
r43347 | if opts.get(b'copy'): | ||
|
r1808 | path = q.path | ||
|
r43347 | if opts.get(b'name'): | ||
newpath = os.path.join(q.basepath, opts.get(b'name')) | ||||
|
r1808 | if os.path.exists(newpath): | ||
if not os.path.isdir(newpath): | ||||
|
r43346 | raise error.Abort( | ||
|
r43387 | _(b'destination %s exists and is not a directory') | ||
|
r43346 | % newpath | ||
) | ||||
|
r43347 | if not opts.get(b'force'): | ||
|
r43346 | raise error.Abort( | ||
|
r43387 | _(b'destination %s exists, use -f to force') % newpath | ||
|
r43346 | ) | ||
|
r1808 | else: | ||
newpath = savename(path) | ||||
|
r43347 | ui.warn(_(b"copy %s to %s\n") % (path, newpath)) | ||
|
r1808 | util.copyfiles(path, newpath) | ||
|
r43347 | if opts.get(b'empty'): | ||
|
r15880 | del q.applied[:] | ||
q.applieddirty = True | ||||
q.savedirty() | ||||
|
r1808 | return 0 | ||
|
r1810 | |||
|
r1808 | |||
|
r43346 | @command( | ||
|
r43347 | b"qselect", | ||
|
r43346 | [ | ||
|
r43347 | (b'n', b'none', None, _(b'disable all guards')), | ||
(b's', b'series', None, _(b'list all guards in series file')), | ||||
(b'', b'pop', None, _(b'pop to before first guarded applied patch')), | ||||
(b'', b'reapply', None, _(b'pop, then reapply patches')), | ||||
|
r43346 | ], | ||
|
r43347 | _(b'hg qselect [OPTION]... [GUARD]...'), | ||
|
r43346 | helpcategory=command.CATEGORY_CHANGE_ORGANIZATION, | ||
) | ||||
|
r2821 | def select(ui, repo, *args, **opts): | ||
|
r46554 | """set or print guarded patches to push | ||
|
r2821 | |||
|
r11307 | Use the :hg:`qguard` command to set or print guards on patch, then use | ||
|
r7994 | qselect to tell mq which guards to use. A patch will be pushed if | ||
it has no guards or any positive guards match the currently | ||||
selected guard, but will not be pushed if any negative guards | ||||
|
r9824 | match the current guard. For example:: | ||
|
r2821 | |||
|
r13791 | qguard foo.patch -- -stable (negative guard) | ||
qguard bar.patch +stable (positive guard) | ||||
|
r2821 | qselect stable | ||
|
r2940 | This activates the "stable" guard. mq will skip foo.patch (because | ||
|
r7994 | it has a negative match) but push bar.patch (because it has a | ||
positive match). | ||||
|
r2821 | |||
|
r2940 | With no arguments, prints the currently active guards. | ||
With one argument, sets the active guard. | ||||
|
r3223 | |||
|
r2940 | Use -n/--none to deactivate guards (no other arguments needed). | ||
|
r7994 | When no guards are active, patches with positive guards are | ||
skipped and patches with negative guards are pushed. | ||||
|
r2821 | |||
|
r2940 | qselect can change the guards on applied patches. It does not pop | ||
|
r7994 | guarded patches by default. Use --pop to pop back to the last | ||
applied patch that is not guarded. Use --reapply (which implies | ||||
--pop) to push back to the current patch afterwards, but skip | ||||
guarded patches. | ||||
|
r2844 | |||
|
r7994 | Use -s/--series to print a list of all guards in the series file | ||
|
r12538 | (no other arguments needed). Use -v for more information. | ||
|
r46554 | Returns 0 on success.""" | ||
|
r2821 | |||
q = repo.mq | ||||
|
r34506 | opts = pycompat.byteskwargs(opts) | ||
|
r2821 | guards = q.active() | ||
|
r22453 | pushable = lambda i: q.pushable(q.applied[i].name)[0] | ||
|
r43347 | if args or opts.get(b'none'): | ||
|
r2844 | old_unapplied = q.unapplied(repo) | ||
|
r43346 | old_guarded = [ | ||
i for i in pycompat.xrange(len(q.applied)) if not pushable(i) | ||||
] | ||||
|
r14578 | q.setactive(args) | ||
|
r14580 | q.savedirty() | ||
|
r2821 | if not args: | ||
|
r43347 | ui.status(_(b'guards deactivated\n')) | ||
if not opts.get(b'pop') and not opts.get(b'reapply'): | ||||
|
r2844 | unapplied = q.unapplied(repo) | ||
|
r43346 | guarded = [ | ||
i for i in pycompat.xrange(len(q.applied)) if not pushable(i) | ||||
] | ||||
|
r2844 | if len(unapplied) != len(old_unapplied): | ||
|
r43346 | ui.status( | ||
_( | ||||
|
r43347 | b'number of unguarded, unapplied patches has ' | ||
b'changed from %d to %d\n' | ||||
|
r43346 | ) | ||
% (len(old_unapplied), len(unapplied)) | ||||
) | ||||
|
r2844 | if len(guarded) != len(old_guarded): | ||
|
r43346 | ui.status( | ||
_( | ||||
|
r43347 | b'number of guarded, applied patches has changed ' | ||
b'from %d to %d\n' | ||||
|
r43346 | ) | ||
% (len(old_guarded), len(guarded)) | ||||
) | ||||
|
r43347 | elif opts.get(b'series'): | ||
|
r2821 | guards = {} | ||
noguards = 0 | ||||
|
r14573 | for gs in q.seriesguards: | ||
|
r2821 | if not gs: | ||
noguards += 1 | ||||
for g in gs: | ||||
guards.setdefault(g, 0) | ||||
guards[g] += 1 | ||||
if ui.verbose: | ||||
|
r43347 | guards[b'NONE'] = noguards | ||
|
r36296 | guards = list(guards.items()) | ||
|
r9032 | guards.sort(key=lambda x: x[0][1:]) | ||
|
r2821 | if guards: | ||
|
r43347 | ui.note(_(b'guards in series file:\n')) | ||
|
r2821 | for guard, count in guards: | ||
|
r43347 | ui.note(b'%2d ' % count) | ||
ui.write(guard, b'\n') | ||||
|
r2821 | else: | ||
|
r43347 | ui.note(_(b'no guards in series file\n')) | ||
|
r2821 | else: | ||
if guards: | ||||
|
r43347 | ui.note(_(b'active guards:\n')) | ||
|
r2821 | for g in guards: | ||
|
r43347 | ui.write(g, b'\n') | ||
|
r2821 | else: | ||
|
r43347 | ui.write(_(b'no active guards\n')) | ||
reapply = opts.get(b'reapply') and q.applied and q.applied[-1].name | ||||
|
r2844 | popped = False | ||
|
r43347 | if opts.get(b'pop') or opts.get(b'reapply'): | ||
|
r38806 | for i in pycompat.xrange(len(q.applied)): | ||
|
r22456 | if not pushable(i): | ||
|
r43347 | ui.status(_(b'popping guarded patches\n')) | ||
|
r2844 | popped = True | ||
if i == 0: | ||||
q.pop(repo, all=True) | ||||
else: | ||||
|
r22455 | q.pop(repo, q.applied[i - 1].name) | ||
|
r2844 | break | ||
if popped: | ||||
try: | ||||
if reapply: | ||||
|
r43347 | ui.status(_(b'reapplying unguarded patches\n')) | ||
|
r2844 | q.push(repo, reapply) | ||
finally: | ||||
|
r14580 | q.savedirty() | ||
|
r2821 | |||
|
r43346 | |||
@command( | ||||
|
r43347 | b"qfinish", | ||
[(b'a', b'applied', None, _(b'finish all applied changesets'))], | ||||
_(b'hg qfinish [-a] [REV]...'), | ||||
|
r43346 | helpcategory=command.CATEGORY_CHANGE_ORGANIZATION, | ||
) | ||||
|
r6645 | def finish(ui, repo, *revrange, **opts): | ||
"""move applied patches into repository history | ||||
|
r7994 | Finishes the specified revisions (corresponding to applied | ||
patches) by moving them out of mq control into regular repository | ||||
history. | ||||
|
r6645 | |||
|
r8076 | Accepts a revision range or the -a/--applied option. If --applied | ||
is specified, all applied mq revisions are removed from mq | ||||
control. Otherwise, the given revisions must be at the base of the | ||||
stack of applied patches. | ||||
|
r6645 | |||
|
r7994 | This can be especially useful if your changes have been applied to | ||
an upstream repository, or if you are about to push your changes | ||||
to upstream. | ||||
|
r12538 | |||
Returns 0 on success. | ||||
|
r6645 | """ | ||
|
r43906 | if not opts.get('applied') and not revrange: | ||
|
r43347 | raise error.Abort(_(b'no revisions specified')) | ||
|
r43906 | elif opts.get('applied'): | ||
|
r43347 | revrange = (b'qbase::qtip',) + revrange | ||
|
r6645 | |||
q = repo.mq | ||||
if not q.applied: | ||||
|
r43347 | ui.status(_(b'no patches applied\n')) | ||
|
r6645 | return 0 | ||
|
r14319 | revs = scmutil.revrange(repo, revrange) | ||
|
r43347 | if repo[b'.'].rev() in revs and repo[None].files(): | ||
ui.warn(_(b'warning: uncommitted changes in the working directory\n')) | ||||
|
r17512 | # queue.finish may changes phases but leave the responsibility to lock the | ||
|
r15920 | # repo to the caller to avoid deadlock with wlock. This command code is | ||
|
r17424 | # responsibility for this locking. | ||
|
r27847 | with repo.lock(): | ||
|
r15920 | q.finish(repo, revs) | ||
q.savedirty() | ||||
|
r6645 | return 0 | ||
|
r43346 | |||
@command( | ||||
|
r43347 | b"qqueue", | ||
|
r43346 | [ | ||
|
r43347 | (b'l', b'list', False, _(b'list all available queues')), | ||
(b'', b'active', False, _(b'print name of active queue')), | ||||
(b'c', b'create', False, _(b'create new queue')), | ||||
(b'', b'rename', False, _(b'rename active queue')), | ||||
(b'', b'delete', False, _(b'delete reference to queue')), | ||||
(b'', b'purge', False, _(b'delete queue, and remove patch dir')), | ||||
|
r43346 | ], | ||
|
r43347 | _(b'[OPTION] [QUEUE]'), | ||
|
r43346 | helpcategory=command.CATEGORY_CHANGE_ORGANIZATION, | ||
) | ||||
|
r11229 | def qqueue(ui, repo, name=None, **opts): | ||
|
r46554 | """manage multiple patch queues | ||
|
r11229 | |||
Supports switching between different patch queues, as well as creating | ||||
new patch queues and deleting existing ones. | ||||
Omitting a queue name or specifying -l/--list will show you the registered | ||||
queues - by default the "normal" patches queue is registered. The currently | ||||
|
r14987 | active queue will be marked with "(active)". Specifying --active will print | ||
only the name of the active queue. | ||||
|
r11229 | |||
To create a new queue, use -c/--create. The queue is automatically made | ||||
active, except in the case where there are applied patches from the | ||||
currently active queue in the repository. Then the queue will only be | ||||
created and switching will fail. | ||||
To delete an existing queue, use --delete. You cannot delete the currently | ||||
active queue. | ||||
|
r12538 | |||
Returns 0 on success. | ||||
|
r46554 | """ | ||
|
r11229 | q = repo.mq | ||
|
r43347 | _defaultqueue = b'patches' | ||
_allqueues = b'patches.queues' | ||||
_activequeue = b'patches.queue' | ||||
|
r11229 | |||
def _getcurrent(): | ||||
|
r11270 | cur = os.path.basename(q.path) | ||
|
r43347 | if cur.startswith(b'patches-'): | ||
|
r11270 | cur = cur[8:] | ||
return cur | ||||
|
r11229 | |||
def _noqueues(): | ||||
try: | ||||
|
r43347 | fh = repo.vfs(_allqueues, b'r') | ||
|
r11229 | fh.close() | ||
except IOError: | ||||
return True | ||||
return False | ||||
def _getqueues(): | ||||
current = _getcurrent() | ||||
try: | ||||
|
r43347 | fh = repo.vfs(_allqueues, b'r') | ||
|
r11229 | queues = [queue.strip() for queue in fh if queue.strip()] | ||
|
r13400 | fh.close() | ||
|
r11229 | if current not in queues: | ||
queues.append(current) | ||||
except IOError: | ||||
queues = [_defaultqueue] | ||||
return sorted(queues) | ||||
def _setactive(name): | ||||
if q.applied: | ||||
|
r43346 | raise error.Abort( | ||
_( | ||||
|
r43347 | b'new queue created, but cannot make active ' | ||
b'as patches are applied' | ||||
|
r43346 | ) | ||
) | ||||
|
r11938 | _setactivenocheck(name) | ||
def _setactivenocheck(name): | ||||
|
r43347 | fh = repo.vfs(_activequeue, b'w') | ||
if name != b'patches': | ||||
|
r11270 | fh.write(name) | ||
|
r11229 | fh.close() | ||
def _addqueue(name): | ||||
|
r43347 | fh = repo.vfs(_allqueues, b'a') | ||
fh.write(b'%s\n' % (name,)) | ||||
|
r11229 | fh.close() | ||
|
r11939 | def _queuedir(name): | ||
|
r43347 | if name == b'patches': | ||
return repo.vfs.join(b'patches') | ||||
|
r11939 | else: | ||
|
r43347 | return repo.vfs.join(b'patches-' + name) | ||
|
r11939 | |||
|
r11270 | def _validname(name): | ||
for n in name: | ||||
|
r43347 | if n in b':\\/.': | ||
|
r11270 | return False | ||
return True | ||||
|
r11966 | def _delete(name): | ||
if name not in existing: | ||||
|
r43347 | raise error.Abort(_(b'cannot delete queue that does not exist')) | ||
|
r11966 | |||
current = _getcurrent() | ||||
if name == current: | ||||
|
r43347 | raise error.Abort(_(b'cannot delete currently active queue')) | ||
fh = repo.vfs(b'patches.queues.new', b'w') | ||||
|
r11966 | for queue in existing: | ||
if queue == name: | ||||
continue | ||||
|
r43347 | fh.write(b'%s\n' % (queue,)) | ||
|
r11966 | fh.close() | ||
|
r43347 | repo.vfs.rename(b'patches.queues.new', _allqueues) | ||
|
r11966 | |||
|
r34506 | opts = pycompat.byteskwargs(opts) | ||
|
r43347 | if not name or opts.get(b'list') or opts.get(b'active'): | ||
|
r11229 | current = _getcurrent() | ||
|
r43347 | if opts.get(b'active'): | ||
ui.write(b'%s\n' % (current,)) | ||||
|
r14987 | return | ||
|
r11229 | for queue in _getqueues(): | ||
|
r43347 | ui.write(b'%s' % (queue,)) | ||
|
r11767 | if queue == current and not ui.quiet: | ||
|
r43347 | ui.write(_(b' (active)\n')) | ||
|
r11229 | else: | ||
|
r43347 | ui.write(b'\n') | ||
|
r11229 | return | ||
|
r11270 | if not _validname(name): | ||
|
r26587 | raise error.Abort( | ||
|
r43347 | _(b'invalid queue name, may not contain the characters ":\\/."') | ||
|
r43346 | ) | ||
|
r11270 | |||
|
r29752 | with repo.wlock(): | ||
existing = _getqueues() | ||||
|
r43347 | if opts.get(b'create'): | ||
|
r29752 | if name in existing: | ||
|
r43347 | raise error.Abort(_(b'queue "%s" already exists') % name) | ||
|
r29752 | if _noqueues(): | ||
_addqueue(_defaultqueue) | ||||
_addqueue(name) | ||||
_setactive(name) | ||||
|
r43347 | elif opts.get(b'rename'): | ||
|
r29752 | current = _getcurrent() | ||
if name == current: | ||||
|
r43346 | raise error.Abort( | ||
|
r43347 | _(b'can\'t rename "%s" to its current name') % name | ||
|
r43346 | ) | ||
|
r29752 | if name in existing: | ||
|
r43347 | raise error.Abort(_(b'queue "%s" already exists') % name) | ||
|
r29752 | |||
olddir = _queuedir(current) | ||||
newdir = _queuedir(name) | ||||
if os.path.exists(newdir): | ||||
|
r43346 | raise error.Abort( | ||
|
r43347 | _(b'non-queue directory "%s" already exists') % newdir | ||
|
r43346 | ) | ||
|
r29752 | |||
|
r43347 | fh = repo.vfs(b'patches.queues.new', b'w') | ||
|
r29752 | for queue in existing: | ||
if queue == current: | ||||
|
r43347 | fh.write(b'%s\n' % (name,)) | ||
|
r29752 | if os.path.exists(olddir): | ||
util.rename(olddir, newdir) | ||||
else: | ||||
|
r43347 | fh.write(b'%s\n' % (queue,)) | ||
|
r29752 | fh.close() | ||
|
r43347 | repo.vfs.rename(b'patches.queues.new', _allqueues) | ||
|
r29752 | _setactivenocheck(name) | ||
|
r43347 | elif opts.get(b'delete'): | ||
|
r11967 | _delete(name) | ||
|
r43347 | elif opts.get(b'purge'): | ||
|
r29752 | if name in existing: | ||
_delete(name) | ||||
qdir = _queuedir(name) | ||||
if os.path.exists(qdir): | ||||
shutil.rmtree(qdir) | ||||
else: | ||||
if name not in existing: | ||||
|
r43347 | raise error.Abort(_(b'use --create to create a new queue')) | ||
|
r29752 | _setactive(name) | ||
|
r11229 | |||
|
r43346 | |||
|
r15928 | def mqphasedefaults(repo, roots): | ||
"""callback used to set mq changeset as secret when no phase data exists""" | ||||
if repo.mq.applied: | ||||
|
r43347 | if repo.ui.configbool(b'mq', b'secret'): | ||
|
r16028 | mqphase = phases.secret | ||
else: | ||||
mqphase = phases.draft | ||||
|
r15972 | qbase = repo[repo.mq.applied[0].node] | ||
|
r16028 | roots[mqphase].add(qbase.node()) | ||
|
r15928 | return roots | ||
|
r43346 | |||
|
r1808 | def reposetup(ui, repo): | ||
|
r2818 | class mqrepo(repo.__class__): | ||
|
r19395 | @localrepo.unfilteredpropertycache | ||
|
r8524 | def mq(self): | ||
|
r19064 | return queue(self.ui, self.baseui, self.path) | ||
|
r8524 | |||
|
r20628 | def invalidateall(self): | ||
super(mqrepo, self).invalidateall() | ||||
|
r43906 | if localrepo.hasunfilteredcache(self, 'mq'): | ||
|
r20629 | # recreate mq in case queue path was changed | ||
|
r43906 | delattr(self.unfiltered(), 'mq') | ||
|
r20628 | |||
|
r14596 | def abortifwdirpatched(self, errmsg, force=False): | ||
|
r19856 | if self.mq.applied and self.mq.checkapplied and not force: | ||
|
r13520 | parents = self.dirstate.parents() | ||
patches = [s.node for s in self.mq.applied] | ||||
|
r41418 | if any(p in patches for p in parents): | ||
|
r26587 | raise error.Abort(errmsg) | ||
|
r3223 | |||
|
r43346 | def commit( | ||
self, | ||||
|
r43347 | text=b"", | ||
|
r43346 | user=None, | ||
date=None, | ||||
match=None, | ||||
force=False, | ||||
editor=False, | ||||
extra=None, | ||||
): | ||||
|
r31407 | if extra is None: | ||
extra = {} | ||||
|
r14596 | self.abortifwdirpatched( | ||
|
r43347 | _(b'cannot commit over an applied mq patch'), force | ||
|
r43346 | ) | ||
return super(mqrepo, self).commit( | ||||
text, user, date, match, force, editor, extra | ||||
) | ||||
|
r2845 | |||
|
r20924 | def checkpush(self, pushop): | ||
if self.mq.applied and self.mq.checkapplied and not pushop.force: | ||||
|
r15952 | outapplied = [e.node for e in self.mq.applied] | ||
|
r20924 | if pushop.revs: | ||
|
r15952 | # Assume applied patches have no non-patch descendants and | ||
# are not on remote already. Filtering any changeset not | ||||
# pushed. | ||||
|
r20924 | heads = set(pushop.revs) | ||
|
r15952 | for node in reversed(outapplied): | ||
if node in heads: | ||||
break | ||||
else: | ||||
outapplied.pop() | ||||
# looking for pushed and shared changeset | ||||
for node in outapplied: | ||||
|
r17954 | if self[node].phase() < phases.secret: | ||
|
r43347 | raise error.Abort(_(b'source has mq patches applied')) | ||
|
r15952 | # no non-secret patches pushed | ||
|
r20924 | super(mqrepo, self).checkpush(pushop) | ||
|
r3223 | |||
|
r9145 | def _findtags(self): | ||
'''augment tags from base class with patch tags''' | ||||
result = super(mqrepo, self)._findtags() | ||||
|
r2682 | |||
|
r2724 | q = self.mq | ||
|
r2723 | if not q.applied: | ||
|
r9145 | return result | ||
|
r2663 | |||
|
r10678 | mqtags = [(patch.node, patch.name) for patch in q.applied] | ||
|
r5979 | |||
|
r13508 | try: | ||
|
r18011 | # for now ignore filtering business | ||
self.unfiltered().changelog.rev(mqtags[-1][0]) | ||||
|
r14600 | except error.LookupError: | ||
|
r43346 | self.ui.warn( | ||
|
r43347 | _(b'mq status file refers to unknown node %s\n') | ||
|
r43346 | % short(mqtags[-1][0]) | ||
) | ||||
|
r9145 | return result | ||
|
r5979 | |||
|
r18662 | # do not add fake tags for filtered revisions | ||
included = self.changelog.hasnode | ||||
mqtags = [mqt for mqt in mqtags if included(mqt[0])] | ||||
if not mqtags: | ||||
return result | ||||
|
r43347 | mqtags.append((mqtags[-1][0], b'qtip')) | ||
mqtags.append((mqtags[0][0], b'qbase')) | ||||
mqtags.append((self.changelog.parents(mqtags[0][0])[0], b'qparent')) | ||||
|
r9145 | tags = result[0] | ||
|
r2723 | for patch in mqtags: | ||
|
r9145 | if patch[1] in tags: | ||
|
r43346 | self.ui.warn( | ||
|
r43387 | _(b'tag %s overrides mq patch of the same name\n') | ||
|
r43346 | % patch[1] | ||
) | ||||
|
r2723 | else: | ||
|
r9145 | tags[patch[1]] = patch[0] | ||
|
r2682 | |||
|
r9145 | return result | ||
|
r2664 | |||
|
r2851 | if repo.local(): | ||
repo.__class__ = mqrepo | ||||
|
r1808 | |||
|
r15928 | repo._phasedefaults.append(mqphasedefaults) | ||
|
r43346 | |||
|
r7216 | def mqimport(orig, ui, repo, *args, **kwargs): | ||
|
r43347 | if util.safehasattr(repo, b'abortifwdirpatched') and not kwargs.get( | ||
|
r43906 | 'no_commit', False | ||
|
r43346 | ): | ||
repo.abortifwdirpatched( | ||||
|
r43906 | _(b'cannot import over an applied patch'), kwargs.get('force') | ||
|
r43346 | ) | ||
|
r7216 | return orig(ui, repo, *args, **kwargs) | ||
|
r43346 | |||
|
r10402 | def mqinit(orig, ui, *args, **kwargs): | ||
|
r43906 | mq = kwargs.pop('mq', None) | ||
|
r10402 | |||
if not mq: | ||||
return orig(ui, *args, **kwargs) | ||||
|
r10691 | if args: | ||
repopath = args[0] | ||||
if not hg.islocal(repopath): | ||||
|
r43346 | raise error.Abort( | ||
|
r43387 | _(b'only a local queue repository may be initialized') | ||
|
r43346 | ) | ||
|
r10691 | else: | ||
|
r39843 | repopath = cmdutil.findrepo(encoding.getcwd()) | ||
|
r10691 | if not repopath: | ||
|
r43346 | raise error.Abort( | ||
|
r43387 | _(b'there is no Mercurial repository here (.hg not found)') | ||
|
r43346 | ) | ||
|
r10402 | repo = hg.repository(ui, repopath) | ||
|
r10480 | return qinit(ui, repo, True) | ||
|
r10402 | |||
|
r43346 | |||
|
r10359 | def mqcommand(orig, ui, repo, *args, **kwargs): | ||
"""Add --mq option to operate on patch repository instead of main""" | ||||
# some commands do not like getting unknown options | ||||
|
r43906 | mq = kwargs.pop('mq', None) | ||
|
r10359 | |||
if not mq: | ||||
return orig(ui, repo, *args, **kwargs) | ||||
q = repo.mq | ||||
r = q.qrepo() | ||||
if not r: | ||||
|
r43347 | raise error.Abort(_(b'no queue repository')) | ||
|
r10407 | return orig(r.ui, r, *args, **kwargs) | ||
|
r10359 | |||
|
r43346 | |||
|
r19212 | def summaryhook(ui, repo): | ||
|
r11107 | q = repo.mq | ||
m = [] | ||||
a, u = len(q.applied), len(q.unapplied(repo)) | ||||
if a: | ||||
|
r43347 | m.append(ui.label(_(b"%d applied"), b'qseries.applied') % a) | ||
|
r11107 | if u: | ||
|
r43347 | m.append(ui.label(_(b"%d unapplied"), b'qseries.unapplied') % u) | ||
|
r11107 | if m: | ||
|
r17893 | # i18n: column positioning for "hg summary" | ||
|
r43347 | ui.write(_(b"mq: %s\n") % b', '.join(m)) | ||
|
r11107 | else: | ||
|
r17892 | # i18n: column positioning for "hg summary" | ||
|
r43347 | ui.note(_(b"mq: (empty queue)\n")) | ||
|
r11107 | |||
|
r43346 | |||
|
r28394 | revsetpredicate = registrar.revsetpredicate() | ||
|
r27586 | |||
|
r43346 | |||
|
r43347 | @revsetpredicate(b'mq()') | ||
|
r14210 | def revsetmq(repo, subset, x): | ||
|
r46554 | """Changesets managed by MQ.""" | ||
|
r43347 | revsetlang.getargs(x, 0, 0, _(b"mq takes no arguments")) | ||
|
r42224 | applied = {repo[r.node].rev() for r in repo.mq.applied} | ||
|
r31023 | return smartset.baseset([r for r in subset if r in applied]) | ||
|
r14210 | |||
|
r43346 | |||
|
r14210 | # tell hggettext to extract docstrings from these functions: | ||
i18nfunctions = [revsetmq] | ||||
|
r43346 | |||
|
r17101 | def extsetup(ui): | ||
# Ensure mq wrappers are called first, regardless of extension load order by | ||||
# NOT wrapping in uisetup() and instead deferring to init stage two here. | ||||
|
r43347 | mqopt = [(b'', b'mq', None, _(b"operate on patch repository"))] | ||
extensions.wrapcommand(commands.table, b'import', mqimport) | ||||
cmdutil.summaryhooks.add(b'mq', summaryhook) | ||||
entry = extensions.wrapcommand(commands.table, b'init', mqinit) | ||||
|
r10402 | entry[1].extend(mqopt) | ||
|
r12036 | def dotable(cmdtable): | ||
|
r43375 | for cmd, entry in pycompat.iteritems(cmdtable): | ||
|
r12036 | cmd = cmdutil.parsealiases(cmd)[0] | ||
|
r28313 | func = entry[0] | ||
|
r30485 | if func.norepo: | ||
|
r12036 | continue | ||
entry = extensions.wrapcommand(cmdtable, cmd, mqcommand) | ||||
entry[1].extend(mqopt) | ||||
dotable(commands.table) | ||||
|
r44471 | thismodule = sys.modules["hgext.mq"] | ||
|
r12036 | for extname, extmodule in extensions.extensions(): | ||
|
r44471 | if extmodule != thismodule: | ||
|
r12036 | dotable(getattr(extmodule, 'cmdtable', {})) | ||
|
r7142 | |||
|
r43346 | |||
colortable = { | ||||
|
r43347 | b'qguard.negative': b'red', | ||
b'qguard.positive': b'yellow', | ||||
b'qguard.unguarded': b'green', | ||||
b'qseries.applied': b'blue bold underline', | ||||
b'qseries.guarded': b'black bold', | ||||
b'qseries.missing': b'red bold', | ||||
b'qseries.unapplied': b'black bold', | ||||
|
r43346 | } | ||