sapling/hgext/record.py

121 lines
3.8 KiB
Python
Raw Normal View History

# record.py
#
# Copyright 2007 Bryan O'Sullivan <bos@serpentine.com>
#
# This software may be used and distributed according to the terms of the
2010-01-20 07:20:08 +03:00
# GNU General Public License version 2 or any later version.
'''commands to interactively select changes for commit/qrefresh'''
from mercurial.i18n import _
from mercurial import cmdutil, commands, extensions
from mercurial import util
2011-05-22 17:10:03 +04:00
cmdtable = {}
command = cmdutil.command(cmdtable)
testedwith = 'internal'
2011-05-22 17:10:03 +04:00
2011-05-22 17:10:03 +04:00
@command("record",
2011-06-10 12:58:10 +04:00
# same options as commit + white space diff options
[c for c in commands.table['^commit|ci'][1][:]
if c[1] != "interactive"] + commands.diffwsopts,
2011-05-22 17:10:03 +04:00
_('hg record [OPTION]... [FILE]...'))
def record(ui, repo, *pats, **opts):
2007-08-10 04:29:16 +04:00
'''interactively select changes to commit
2010-04-22 12:24:49 +04:00
If a list of files is omitted, all changes reported by :hg:`status`
will be candidates for recording.
2007-08-10 04:29:16 +04:00
2010-04-22 12:24:49 +04:00
See :hg:`help dates` for a list of formats valid for -d/--date.
You will be prompted for whether to record changes to each
modified file, and for files with multiple changes, for each
change to use. For each query, the following responses are
possible::
2007-08-10 04:29:16 +04:00
y - record this change
n - skip this change
record: allow splitting of hunks by manually editing patches It is possible that unrelated changes in a file are on sequential lines. The current record extension does not allow these to be committed independently. An example use case for this is in software development for deeply embedded real-time systems. In these environments, it is not always possible to use a debugger (due to time-constraints) and hence inline UART-based printing is often used. When fixing a bug in a module, it is often convenient to add a large number of 'printf's (linked to the UART via a custom fputc) to the module in order to work out what is going wrong. printf is a very slow function (and also variadic so somewhat frowned upon by the MISRA standard) and hence it is highly undesirable to commit these lines to the repository. If only a partial fix is implemented, however, it is desirable to commit the fix without deleting all of the printf lines. This is also simplifies removal of the printf lines as once the final fix is committed, 'hg revert' does the rest. It is likely that the printf lines will be very near the actual fix, so being able to split the hunk is very useful in this case. There were two alternatives I considered for the user interface. One was to manually edit the patch, the other to allow a hunk to be split into individual lines for consideration. The latter option would require a significant refactor of the record module and is less flexible. While the former is potentially more complicated to use, this is a feature that is likely to only be used in certain exceptional cases (such as the use case proposed above) and hence I felt that the complexity would not be a considerable issue. I've also written a follow-up patch that refactors the 'prompt' code to base everything on the choices variable. This tidies up and clarifies the code a bit (removes constructs like 'if ret == 7' and removes the 'e' option from the file scope options as it's not relevant there. It's not really a necessity, so I've excluded it from this submission for now, but I can send it separately if there's a desire and it's on bitbucket (see below) in the meantime. Possible future improvements include: * Tidying up the 'prompt' code to base everything on the choices variable. This would allow entries to be removed from the prompt as currently 'e' is offered even for entire file patches, which is currently unsupported. * Allowing the entire file (or even multi-file) patch to be edited manually: this would require quite a large refactor without much benefit, so I decided to exclude it from the initial submission. * Allow the option to retry if a patch fails to apply (this is what Git does). This would require quite a bit of refactoring given the current 'hg record' implementation, so it's debatable whether it's worth it. Output is similar to existing record user interface except that an additional option ('e') exists to allow manual editing of the patch. This opens the user's configured editor with the patch. A comment is added to the bottom of the patch explaining what to do (based on Git's one). A large proportion of the changeset is test-case changes to update the options reported by record (Ynesfdaq? instead of Ynsfdaq?). Functional changes are in record.py and there are some new test cases in test-record.t.
2012-03-31 01:08:46 +04:00
e - edit this change manually
2007-08-10 04:29:16 +04:00
s - skip remaining changes to this file
f - record remaining changes to this file
2007-08-10 04:29:16 +04:00
d - done, skip remaining changes and files
a - record all changes to all remaining files
q - quit, recording no changes
2007-08-10 04:29:16 +04:00
? - display help
This command is not available when committing a merge.'''
cmdutil.dorecord(ui, repo, commands.commit, 'commit', False, *pats, **opts)
def qrefresh(origfn, ui, repo, *pats, **opts):
if not opts['interactive']:
return origfn(ui, repo, *pats, **opts)
mq = extensions.find('mq')
def committomq(ui, repo, *pats, **opts):
# At this point the working copy contains only changes that
# were accepted. All other changes were reverted.
# We can't pass *pats here since qrefresh will undo all other
# changed files in the patch that aren't in pats.
mq.refresh(ui, repo, **opts)
# backup all changed files
cmdutil.dorecord(ui, repo, committomq, 'qrefresh', True, *pats, **opts)
# This command registration is replaced during uisetup().
@command('qrecord',
[],
_('hg qrecord [OPTION]... PATCH [FILE]...'),
inferrepo=True)
def qrecord(ui, repo, patch, *pats, **opts):
'''interactively record a new patch
2010-04-22 12:24:49 +04:00
See :hg:`help qnew` & :hg:`help record` for more information and
usage.
'''
try:
mq = extensions.find('mq')
except KeyError:
raise util.Abort(_("'mq' extension not loaded"))
repo.mq.checkpatchname(patch)
def committomq(ui, repo, *pats, **opts):
opts['checkname'] = False
mq.new(ui, repo, patch, *pats, **opts)
cmdutil.dorecord(ui, repo, committomq, 'qnew', False, *pats, **opts)
def qnew(origfn, ui, repo, patch, *args, **opts):
if opts['interactive']:
return qrecord(ui, repo, patch, *args, **opts)
return origfn(ui, repo, patch, *args, **opts)
def uisetup(ui):
try:
mq = extensions.find('mq')
except KeyError:
return
2011-05-22 17:10:03 +04:00
cmdtable["qrecord"] = \
(qrecord,
# same options as qnew, but copy them so we don't get
2011-06-10 12:58:10 +04:00
# -i/--interactive for qrecord and add white space diff options
mq.cmdtable['^qnew'][1][:] + commands.diffwsopts,
2011-05-22 17:10:03 +04:00
_('hg qrecord [OPTION]... PATCH [FILE]...'))
_wrapcmd('qnew', mq.cmdtable, qnew, _("interactively record a new patch"))
_wrapcmd('qrefresh', mq.cmdtable, qrefresh,
_("interactively select changes to refresh"))
def _wrapcmd(cmd, table, wrapfn, msg):
entry = extensions.wrapcommand(table, cmd, wrapfn)
entry[1].append(('i', 'interactive', None, msg))