comparison mercurial/patch.py @ 25424:69609f43c752

revert: add an experimental config to use inverted selection We had a discussion on the list about the interactive ui for revert. This patch adds a flag to allow people to test the second alternative (referred to as proposition 2 on the mailing list). It effectively inverts the signs in the
author Laurent Charignon <lcharignon@fb.com>
date Fri, 29 May 2015 13:11:52 -0700
parents 724421cb4745
children fb04372d7b38
comparison
equal deleted inserted replaced
25423:525fbf24b51b 25424:69609f43c752
1382 if i < 0: 1382 if i < 0:
1383 i = s.find(' ') 1383 i = s.find(' ')
1384 if i < 0: 1384 if i < 0:
1385 return s 1385 return s
1386 return s[:i] 1386 return s[:i]
1387
1388 def reversehunks(hunks):
1389 '''reverse the signs in the hunks given as argument
1390
1391 This function operates on hunks coming out of patch.filterpatch, that is
1392 a list of the form: [header1, hunk1, hunk2, header2...]. Example usage:
1393
1394 >>> rawpatch = """diff --git a/folder1/g b/folder1/g
1395 ... --- a/folder1/g
1396 ... +++ b/folder1/g
1397 ... @@ -1,7 +1,7 @@
1398 ... +firstline
1399 ... c
1400 ... 1
1401 ... 2
1402 ... + 3
1403 ... -4
1404 ... 5
1405 ... d
1406 ... +lastline"""
1407 >>> hunks = parsepatch(rawpatch)
1408 >>> hunkscomingfromfilterpatch = []
1409 >>> for h in hunks:
1410 ... hunkscomingfromfilterpatch.append(h)
1411 ... hunkscomingfromfilterpatch.extend(h.hunks)
1412
1413 >>> reversedhunks = reversehunks(hunkscomingfromfilterpatch)
1414 >>> fp = cStringIO.StringIO()
1415 >>> for c in reversedhunks:
1416 ... c.write(fp)
1417 >>> fp.seek(0)
1418 >>> reversedpatch = fp.read()
1419 >>> print reversedpatch
1420 diff --git a/folder1/g b/folder1/g
1421 --- a/folder1/g
1422 +++ b/folder1/g
1423 @@ -1,4 +1,3 @@
1424 -firstline
1425 c
1426 1
1427 2
1428 @@ -1,6 +2,6 @@
1429 c
1430 1
1431 2
1432 - 3
1433 +4
1434 5
1435 d
1436 @@ -5,3 +6,2 @@
1437 5
1438 d
1439 -lastline
1440
1441 '''
1442
1443 import crecord as crecordmod
1444 newhunks = []
1445 for c in hunks:
1446 if isinstance(c, crecordmod.uihunk):
1447 # curses hunks encapsulate the record hunk in _hunk
1448 c = c._hunk
1449 if isinstance(c, recordhunk):
1450 for j, line in enumerate(c.hunk):
1451 if line.startswith("-"):
1452 c.hunk[j] = "+" + c.hunk[j][1:]
1453 elif line.startswith("+"):
1454 c.hunk[j] = "-" + c.hunk[j][1:]
1455 c.added, c.removed = c.removed, c.added
1456 newhunks.append(c)
1457 return newhunks
1387 1458
1388 def parsepatch(originalchunks): 1459 def parsepatch(originalchunks):
1389 """patch -> [] of headers -> [] of hunks """ 1460 """patch -> [] of headers -> [] of hunks """
1390 class parser(object): 1461 class parser(object):
1391 """patch parsing state machine""" 1462 """patch parsing state machine"""