[PATCH 2 of 2] record: allow splitting of hunks by manually editing patches
A. S. Budden
abudden at gmail.com
Mon Feb 20 13:30:35 UTC 2012
# HG changeset patch
# User A. S. Budden <abudden at gmail.com>
# Date 1329683565 0
# Node ID 58f3e35efe716309e64bf498f44ffb0117554aad
# Parent ea6086ad210db327591965ad58f9e656313ce2fe
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;
this patch is intended to overcome that limitation.
In order to take control over which lines in the hunk are applied, an editor is
opened with a single-hunk patch. Instructions on how to edit the patch are
included with the patch (this follows Git's method of doing things). Although
patch editing sounds complicated, in practice, editing is actually very simple
as all the user needs to do is either replace the '-' at the start of line with
a space or delete lines starting with a '+' (this is explained in the
instructions). Given how rarely I'd expect this to be used in general, I felt
that this was an acceptable level of complexity.
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. A partial commit 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.
In my opinion, this is a valuable addition to Mercurial: Git can do this and I
often find myself using Git on some projects for this feature alone. However,
I dislike the way that the partial commit is essentially the default way of
committing in Git and I feel that it should be available for exceptional
circumstances but not the default behaviour; this is one of the reasons I'd
rather use Mercurial.
diff --git a/hgext/record.py b/hgext/record.py
--- a/hgext/record.py
+++ b/hgext/record.py
@@ -261,7 +261,7 @@
def filterpatch(ui, headers):
"""Interactively filter patch chunks into applied-only chunks"""
- def prompt(skipfile, skipall, query):
+ def prompt(skipfile, skipall, query, chunk):
"""prompt query, and process base inputs
- y/n for the rest of file
@@ -271,20 +271,25 @@
Return True/False and possibly updated skipfile and skipall.
"""
+ newpatches = None
if skipall is not None:
- return skipall, skipfile, skipall
+ return skipall, skipfile, skipall, newpatches
if skipfile is not None:
- return skipfile, skipfile, skipall
+ return skipfile, skipfile, skipall, newpatches
while True:
choices = [
('yes', _('&Yes, record this change')),
('no', _('&No, skip this change')),
+ ('edit', _('&Edit the change manually')),
('skip', _('&Skip remaining changes to this file')),
('file', _('Record remaining changes to this &file')),
('done', _('&Done, skip remaining changes and files')),
('all', _('Record &all changes to all remaining files')),
('quit', _('&Quit, recording no changes')),
('?', _('&?, display help'))]
+ # If there's no chunk, we can't edit a patch
+ if chunk is None:
+ del choices[map(lambda x: x[0], choices).index('edit')]
# Extract the parts of 'choices' as separate lists
promptchoices = map(lambda x: x[1], choices)
@@ -310,6 +315,58 @@
ret = True
elif r == results.index('no'): # no
ret = False
+ elif 'edit' in results and r == results.index('edit'): # Edit Patch
+ if chunk is None:
+ ui.write(_('cannot edit patch for whole file'))
+ ui.write("\n")
+ continue
+ if chunk.header.binary():
+ ui.write(_('cannot edit patch for binary file'))
+ ui.write("\n")
+ continue
+ # Patch comment based on the Git one (based on comment at end of
+ # http://mercurial.selenic.com/wiki/RecordExtension)
+ phelp = '---' + _("""
+To remove '-' lines, make them ' ' lines (context).
+To remove '+' lines, delete them.
+Lines starting with # will be removed from the patch.
+
+If the patch applies cleanly, the edited hunk will immediately be
+added to the record list. If it does not apply cleanly, a rejects
+file will be generated: you can use that when you try again. If
+all lines of the hunk are removed, then the edit is aborted and
+the hunk is left unchanged.
+""")
+ (patchfd, patchfn) = tempfile.mkstemp(prefix="hg-editor-",
+ suffix=".diff", text=True)
+ try:
+ # Write the initial patch
+ f = os.fdopen(patchfd, "w")
+ chunk.header.write(f)
+ chunk.write(f)
+ f.write('\n'.join(['# ' + i for i in phelp.splitlines()]))
+ f.close()
+ # Start the editor and wait for it to complete
+ editor = ui.geteditor()
+ util.system("%s \"%s\"" % (editor, patchfn),
+ environ={'HGUSER': ui.username()},
+ onerr=util.Abort, errprefix=_("edit failed"),
+ out=ui.fout)
+ # Remove comment lines
+ patchfp = open(patchfn)
+ ncpatchfp = cStringIO.StringIO()
+ for line in patchfp:
+ if not line.startswith('#'):
+ ncpatchfp.write(line)
+ patchfp.close()
+ ncpatchfp.seek(0)
+ newpatches = parsepatch(ncpatchfp)
+ finally:
+ os.unlink(patchfn)
+ del ncpatchfp
+ # Signal that the chunk shouldn't be applied as-is, but
+ # provide the new patch to be used instead.
+ ret = False
elif r == results.index('skip'): # Skip
ret = skipfile = False
elif r == results.index('file'): # file (Record remaining)
@@ -320,7 +377,7 @@
ret = skipall = True
elif r == results.index('quit'): # quit
raise util.Abort(_('user quit'))
- return ret, skipfile, skipall
+ return ret, skipfile, skipall, newpatches
seen = set()
applied = {} # 'filename' -> [] of chunks
@@ -338,7 +395,7 @@
h.pretty(ui)
msg = (_('examine changes to %s?') %
_(' and ').join(map(repr, h.files())))
- r, skipfile, skipall = prompt(skipfile, skipall, msg)
+ r, skipfile, skipall, np = prompt(skipfile, skipall, msg, None)
if not r:
continue
applied[h.filename()] = [h]
@@ -354,12 +411,19 @@
idx = pos - len(h.hunks) + i
msg = _('record change %d/%d to %r?') % (idx, total,
chunk.filename())
- r, skipfile, skipall = prompt(skipfile, skipall, msg)
+ r, skipfile, skipall, newpatches = prompt(skipfile,
+ skipall, msg, chunk)
if r:
if fixoffset:
chunk = copy.copy(chunk)
chunk.toline += fixoffset
applied[chunk.filename()].append(chunk)
+ elif newpatches is not None:
+ for newpatch in newpatches:
+ for newhunk in newpatch.hunks:
+ if fixoffset:
+ newhunk.toline += fixoffset
+ applied[newhunk.filename()].append(newhunk)
else:
fixoffset += chunk.removed - chunk.added
return sum([h for h in applied.itervalues()
@@ -384,6 +448,7 @@
y - record this change
n - skip this change
+ e - edit this change manually
s - skip remaining changes to this file
f - record remaining changes to this file
diff --git a/tests/test-diff-color.t b/tests/test-diff-color.t
--- a/tests/test-diff-color.t
+++ b/tests/test-diff-color.t
@@ -95,7 +95,7 @@
a
a
c
- \x1b[0;33mrecord this change to 'a'? [Ynsfdaq?]\x1b[0m (esc)
+ \x1b[0;33mrecord this change to 'a'? [Ynesfdaq?]\x1b[0m (esc)
$ echo "[extensions]" >> $HGRCPATH
$ echo "mq=" >> $HGRCPATH
@@ -123,4 +123,4 @@
a
a
c
- \x1b[0;33mrecord this change to 'a'? [Ynsfdaq?]\x1b[0m (esc)
+ \x1b[0;33mrecord this change to 'a'? [Ynesfdaq?]\x1b[0m (esc)
diff --git a/tests/test-keyword.t b/tests/test-keyword.t
--- a/tests/test-keyword.t
+++ b/tests/test-keyword.t
@@ -344,12 +344,12 @@
+foo
do not process $Id:
xxx $
- record change 1/2 to 'a'? [Ynsfdaq?]
+ record change 1/2 to 'a'? [Ynesfdaq?]
@@ -2,2 +3,3 @@
do not process $Id:
xxx $
+bar
- record change 2/2 to 'a'? [Ynsfdaq?]
+ record change 2/2 to 'a'? [Ynesfdaq?]
$ hg identify
d17e03c92c97+ tip
@@ -401,12 +401,12 @@
+foo
do not process $Id:
xxx $
- record change 1/2 to 'a'? [Ynsfdaq?]
+ record change 1/2 to 'a'? [Ynesfdaq?]
@@ -2,2 +3,3 @@
do not process $Id:
xxx $
+bar
- record change 2/2 to 'a'? [Ynsfdaq?]
+ record change 2/2 to 'a'? [Ynesfdaq?]
File a should be clean
diff --git a/tests/test-mq-qrefresh-interactive.t b/tests/test-mq-qrefresh-interactive.t
--- a/tests/test-mq-qrefresh-interactive.t
+++ b/tests/test-mq-qrefresh-interactive.t
@@ -191,13 +191,13 @@
-2
+2 2
3
- record change 1/4 to '1.txt'? [Ynsfdaq?]
+ record change 1/4 to '1.txt'? [Ynesfdaq?]
@@ -3,3 +3,3 @@
3
-4
+4 4
5
- record change 2/4 to '1.txt'? [Ynsfdaq?]
+ record change 2/4 to '1.txt'? [Ynesfdaq?]
diff --git a/2.txt b/2.txt
1 hunks, 1 lines changed
examine changes to '2.txt'? [Ynsfdaq?]
@@ -208,7 +208,7 @@
c
d
e
- record change 3/4 to '2.txt'? [Ynsfdaq?]
+ record change 3/4 to '2.txt'? [Ynesfdaq?]
diff --git a/dir/a.txt b/dir/a.txt
1 hunks, 1 lines changed
examine changes to 'dir/a.txt'? [Ynsfdaq?]
@@ -287,7 +287,7 @@
-4
+4 4
5
- record change 1/2 to '1.txt'? [Ynsfdaq?]
+ record change 1/2 to '1.txt'? [Ynesfdaq?]
diff --git a/dir/a.txt b/dir/a.txt
1 hunks, 1 lines changed
examine changes to 'dir/a.txt'? [Ynsfdaq?]
@@ -297,7 +297,7 @@
someone
up
- record change 2/2 to 'dir/a.txt'? [Ynsfdaq?]
+ record change 2/2 to 'dir/a.txt'? [Ynesfdaq?]
After final qrefresh 'tip'
diff --git a/tests/test-mq-subrepo.t b/tests/test-mq-subrepo.t
--- a/tests/test-mq-subrepo.t
+++ b/tests/test-mq-subrepo.t
@@ -291,7 +291,7 @@
@@ -1,1 +1,2 @@
sub = sub
+sub2 = sub2
- record this change to '.hgsub'? [Ynsfdaq?]
+ record this change to '.hgsub'? [Ynesfdaq?]
abort: uncommitted changes in subrepository sub2
[255]
% update substate when modifying .hgsub w/clean updated subrepo
@@ -303,7 +303,7 @@
@@ -1,1 +1,2 @@
sub = sub
+sub2 = sub2
- record this change to '.hgsub'? [Ynsfdaq?]
+ record this change to '.hgsub'? [Ynesfdaq?]
path sub
source sub
revision b2fdb12cd82b021c3b7053d67802e77b6eeaee31
diff --git a/tests/test-qrecord.t b/tests/test-qrecord.t
--- a/tests/test-qrecord.t
+++ b/tests/test-qrecord.t
@@ -40,6 +40,7 @@
y - record this change
n - skip this change
+ e - edit this change manually
s - skip remaining changes to this file
f - record remaining changes to this file
@@ -251,13 +252,13 @@
-2
+2 2
3
- record change 1/4 to '1.txt'? [Ynsfdaq?]
+ record change 1/4 to '1.txt'? [Ynesfdaq?]
@@ -3,3 +3,3 @@
3
-4
+4 4
5
- record change 2/4 to '1.txt'? [Ynsfdaq?]
+ record change 2/4 to '1.txt'? [Ynesfdaq?]
diff --git a/2.txt b/2.txt
1 hunks, 1 lines changed
examine changes to '2.txt'? [Ynsfdaq?]
@@ -268,7 +269,7 @@
c
d
e
- record change 3/4 to '2.txt'? [Ynsfdaq?]
+ record change 3/4 to '2.txt'? [Ynesfdaq?]
diff --git a/dir/a.txt b/dir/a.txt
1 hunks, 1 lines changed
examine changes to 'dir/a.txt'? [Ynsfdaq?]
@@ -348,7 +349,7 @@
-4
+4 4
5
- record change 1/2 to '1.txt'? [Ynsfdaq?]
+ record change 1/2 to '1.txt'? [Ynesfdaq?]
diff --git a/dir/a.txt b/dir/a.txt
1 hunks, 1 lines changed
examine changes to 'dir/a.txt'? [Ynsfdaq?]
@@ -358,7 +359,7 @@
someone
up
- record change 2/2 to 'dir/a.txt'? [Ynsfdaq?]
+ record change 2/2 to 'dir/a.txt'? [Ynesfdaq?]
After qrecord b.patch 'tip'
diff --git a/tests/test-record.t b/tests/test-record.t
--- a/tests/test-record.t
+++ b/tests/test-record.t
@@ -264,7 +264,7 @@
9
10
+11
- record this change to 'plain'? [Ynsfdaq?]
+ record this change to 'plain'? [Ynesfdaq?]
Modify end of plain file, no EOL
@@ -282,7 +282,7 @@
11
+7264f99c5f5ff3261504828afa4fb4d406c3af54
\ No newline at end of file
- record this change to 'plain'? [Ynsfdaq?]
+ record this change to 'plain'? [Ynesfdaq?]
Modify end of plain file, add EOL
@@ -304,7 +304,7 @@
-7264f99c5f5ff3261504828afa4fb4d406c3af54
\ No newline at end of file
+7264f99c5f5ff3261504828afa4fb4d406c3af54
- record change 1/2 to 'plain'? [Ynsfdaq?]
+ record change 1/2 to 'plain'? [Ynesfdaq?]
diff --git a/plain2 b/plain2
new file mode 100644
examine changes to 'plain2'? [Ynsfdaq?]
@@ -334,21 +334,21 @@
2
3
4
- record change 1/3 to 'plain'? [Ynsfdaq?]
+ record change 1/3 to 'plain'? [Ynesfdaq?]
@@ -8,5 +8,3 @@
8
9
10
-11
-7264f99c5f5ff3261504828afa4fb4d406c3af54
- record change 2/3 to 'plain'? [Ynsfdaq?]
+ record change 2/3 to 'plain'? [Ynesfdaq?]
diff --git a/plain2 b/plain2
1 hunks, 1 lines changed
examine changes to 'plain2'? [Ynsfdaq?]
@@ -1,1 +1,2 @@
1
+2
- record change 3/3 to 'plain2'? [Ynsfdaq?]
+ record change 3/3 to 'plain2'? [Ynesfdaq?]
$ hg tip -p
changeset: 11:21df83db12b8
@@ -407,7 +407,7 @@
7
8
9
- record change 1/2 to 'plain'? [Ynsfdaq?]
+ record change 1/2 to 'plain'? [Ynesfdaq?]
@@ -4,7 +1,7 @@
4
5
@@ -417,7 +417,7 @@
9
-10
+10.new
- record change 2/2 to 'plain'? [Ynsfdaq?]
+ record change 2/2 to 'plain'? [Ynesfdaq?]
$ hg tip -p
changeset: 12:99337501826f
@@ -453,7 +453,7 @@
4
5
6
- record this change to 'plain'? [Ynsfdaq?]
+ record this change to 'plain'? [Ynesfdaq?]
$ hg tip -p
changeset: 13:bbd45465d540
@@ -501,7 +501,7 @@
7
8
9
- record change 1/2 to 'plain'? [Ynsfdaq?]
+ record change 1/2 to 'plain'? [Ynesfdaq?]
@@ -1,7 +4,6 @@
4
5
@@ -510,7 +510,7 @@
8
9
-10.new
- record change 2/2 to 'plain'? [Ynsfdaq?]
+ record change 2/2 to 'plain'? [Ynesfdaq?]
Add to beginning, middle, end
@@ -536,7 +536,7 @@
+3
4
5
- record change 1/3 to 'plain'? [Ynsfdaq?]
+ record change 1/3 to 'plain'? [Ynesfdaq?]
@@ -1,6 +4,8 @@
4
5
@@ -546,7 +546,7 @@
7
8
9
- record change 2/3 to 'plain'? [Ynsfdaq?]
+ record change 2/3 to 'plain'? [Ynesfdaq?]
@@ -3,4 +8,6 @@
6
7
@@ -554,7 +554,7 @@
9
+10
+11
- record change 3/3 to 'plain'? [Ynsfdaq?]
+ record change 3/3 to 'plain'? [Ynesfdaq?]
$ hg tip -p
changeset: 15:f34a7937ec33
@@ -594,7 +594,7 @@
9
+10
+11
- record this change to 'plain'? [Ynsfdaq?]
+ record this change to 'plain'? [Ynesfdaq?]
$ hg tip -p
changeset: 16:f9900b71a04c
@@ -631,7 +631,7 @@
@@ -1,1 +1,2 @@
a
+a
- record this change to 'subdir/a'? [Ynsfdaq?]
+ record this change to 'subdir/a'? [Ynesfdaq?]
$ hg tip -p
changeset: 18:61be427a9deb
@@ -661,6 +661,8 @@
$ hg record <<EOF
> ?
+ > y
+ > ?
> q
> EOF
diff --git a/subdir/f1 b/subdir/f1
@@ -675,6 +677,20 @@
q - Quit, recording no changes
? - ?, display help
examine changes to 'subdir/f1'? [Ynsfdaq?]
+ @@ -1,1 +1,2 @@
+ a
+ +a
+ record change 1/2 to 'subdir/f1'? [Ynesfdaq?]
+ y - Yes, record this change
+ n - No, skip this change
+ e - Edit the change manually
+ s - Skip remaining changes to this file
+ f - Record remaining changes to this file
+ d - Done, skip remaining changes and files
+ a - Record all changes to all remaining files
+ q - Quit, recording no changes
+ ? - ?, display help
+ record change 1/2 to 'subdir/f1'? [Ynesfdaq?]
abort: user quit
[255]
@@ -789,7 +805,7 @@
a
a
+a
- record this change to 'subdir/f1'? [Ynsfdaq?]
+ record this change to 'subdir/f1'? [Ynesfdaq?]
$ hg tip --config diff.git=True -p
changeset: 22:3261adceb075
@@ -825,7 +841,7 @@
a
a
+b
- record this change to 'subdir/f1'? [Ynsfdaq?]
+ record this change to 'subdir/f1'? [Ynesfdaq?]
$ hg tip --config diff.git=True -p
changeset: 23:b429867550db
@@ -863,7 +879,7 @@
a
b
+c
- record this change to 'subdir/f1'? [Ynsfdaq?]
+ record this change to 'subdir/f1'? [Ynesfdaq?]
$ hg tip --config diff.git=True -p
changeset: 24:0b082130c20a
@@ -914,6 +930,138 @@
$ hg up -C
0 files updated, 0 files merged, 1 files removed, 0 files unresolved
+Editing patch
+
+ $ hg branch patchediting
+ marked working directory as branch patchediting
+ (branches are permanent and global, did you want a bookmark?)
+
+ $ cat > editor << '__EOF__'
+ > #!/bin/sh
+ > sed -i -e 7d -e '5s/^-/ /' "$1"
+ > __EOF__
+ $ chmod +x editor
+ $ cat > editedfile << '__EOF__'
+ > This is the first line
+ > This is the second line
+ > This is the third line
+ > __EOF__
+ $ hg add editedfile
+ $ hg commit -mx
+ $ cat > editedfile << '__EOF__'
+ > This line has changed
+ > This change will be committed
+ > This is the third line
+ > __EOF__
+ $ HGEDITOR="'`pwd`'"/editor hg record -my <<EOF
+ > y
+ > e
+ > EOF
+ diff --git a/editedfile b/editedfile
+ 1 hunks, 2 lines changed
+ examine changes to 'editedfile'? [Ynsfdaq?]
+ @@ -1,3 +1,3 @@
+ -This is the first line
+ -This is the second line
+ +This line has changed
+ +This change will be committed
+ This is the third line
+ record this change to 'editedfile'? [Ynesfdaq?]
+ $ cat editedfile
+ This line has changed
+ This change will be committed
+ This is the third line
+ $ hg cat -r tip editedfile
+ This is the first line
+ This change will be committed
+ This is the third line
+ $ hg revert editedfile
+
+Removing changes from patch
+
+ $ sed -i -e '3s/third/second/' -e '2s/will/will not/' -e 1d editedfile
+ $ echo "This line has been added" >> editedfile
+ $ cat > editor << '__EOF__'
+ > #!/bin/sh
+ > sed -i -e 's/^[-+]/ /' "$1"
+ > __EOF__
+ $ chmod +x editor
+ $ HGEDITOR="'`pwd`'"/editor hg record <<EOF
+ > y
+ > e
+ > EOF
+ diff --git a/editedfile b/editedfile
+ 1 hunks, 3 lines changed
+ examine changes to 'editedfile'? [Ynsfdaq?]
+ @@ -1,3 +1,3 @@
+ -This is the first line
+ -This change will be committed
+ -This is the third line
+ +This change will not be committed
+ +This is the second line
+ +This line has been added
+ record this change to 'editedfile'? [Ynesfdaq?]
+ no changes to record
+ $ cat editedfile
+ This change will not be committed
+ This is the second line
+ This line has been added
+ $ hg cat -r tip editedfile
+ This is the first line
+ This change will be committed
+ This is the third line
+ $ hg revert editedfile
+
+Invalid patch
+
+ $ sed -i -e '3s/third/second/' -e '2s/will/will not/' -e 1d editedfile
+ $ echo "This line has been added" >> editedfile
+ $ cat > editor << '__EOF__'
+ > #!/bin/sh
+ > sed -i s/This/That/ "$1"
+ > __EOF__
+ $ chmod +x editor
+ $ HGEDITOR="'`pwd`'"/editor hg record <<EOF
+ > y
+ > e
+ > EOF
+ diff --git a/editedfile b/editedfile
+ 1 hunks, 3 lines changed
+ examine changes to 'editedfile'? [Ynsfdaq?]
+ @@ -1,3 +1,3 @@
+ -This is the first line
+ -This change will be committed
+ -This is the third line
+ +This change will not be committed
+ +This is the second line
+ +This line has been added
+ record this change to 'editedfile'? [Ynesfdaq?]
+ patching file editedfile
+ Hunk #1 FAILED at 0
+ 1 out of 1 hunks FAILED -- saving rejects to file editedfile.rej
+ abort: patch failed to apply
+ [255]
+ $ cat editedfile
+ This change will not be committed
+ This is the second line
+ This line has been added
+ $ hg cat -r tip editedfile
+ This is the first line
+ This change will be committed
+ This is the third line
+ $ cat editedfile.rej
+ --- editedfile
+ +++ editedfile
+ @@ -1,3 +1,3 @@
+ -That is the first line
+ -That change will be committed
+ -That is the third line
+ +That change will not be committed
+ +That is the second line
+ +That line has been added
+ $ hg up -C default
+ 0 files updated, 0 files merged, 1 files removed, 0 files unresolved
+
With win32text
$ echo '[extensions]' >> .hg/hgrc
@@ -943,10 +1091,10 @@
b
c
+d
- record this change to 'subdir/f1'? [Ynsfdaq?]
+ record this change to 'subdir/f1'? [Ynesfdaq?]
$ hg tip -p
- changeset: 26:b8306e70edc4
+ changeset: 28:b8306e70edc4
tag: tip
parent: 24:0b082130c20a
user: test
More information about the Mercurial-devel
mailing list