##// END OF EJS Templates
patch: rewrite reversehunks (issue5337)...
Jun Wu -
r32979:66117dae default
parent child Browse files
Show More
@@ -427,6 +427,54 b' class uihunk(patchnode):'
427 self.pretty(x)
427 self.pretty(x)
428 return x.getvalue()
428 return x.getvalue()
429
429
430 def reversehunk(self):
431 """return a recordhunk which is the reverse of the hunk
432
433 Assuming the displayed patch is diff(A, B) result. The returned hunk is
434 intended to be applied to B, instead of A.
435
436 For example, when A is "0\n1\n2\n6\n" and B is "0\n3\n4\n5\n6\n", and
437 the user made the following selection:
438
439 0
440 [x] -1 [x]: selected
441 [ ] -2 [ ]: not selected
442 [x] +3
443 [ ] +4
444 [x] +5
445 6
446
447 This function returns a hunk like:
448
449 0
450 -3
451 -4
452 -5
453 +1
454 +4
455 6
456
457 Note "4" was first deleted then added. That's because "4" exists in B
458 side and "-4" must exist between "-3" and "-5" to make the patch
459 applicable to B.
460 """
461 dels = []
462 adds = []
463 for line in self.changedlines:
464 text = line.linetext
465 if line.applied:
466 if text[0] == '+':
467 dels.append(text[1:])
468 elif text[0] == '-':
469 adds.append(text[1:])
470 elif text[0] == '+':
471 dels.append(text[1:])
472 adds.append(text[1:])
473 hunk = ['-%s' % l for l in dels] + ['+%s' % l for l in adds]
474 h = self._hunk
475 return patchmod.recordhunk(h.header, h.toline, h.fromline, h.proc,
476 h.before, hunk, h.after)
477
430 def __getattr__(self, name):
478 def __getattr__(self, name):
431 return getattr(self._hunk, name)
479 return getattr(self._hunk, name)
432
480
@@ -959,6 +959,18 b' class recordhunk(object):'
959 rem = len([h for h in hunk if h[0] == '-'])
959 rem = len([h for h in hunk if h[0] == '-'])
960 return add, rem
960 return add, rem
961
961
962 def reversehunk(self):
963 """return another recordhunk which is the reverse of the hunk
964
965 If this hunk is diff(A, B), the returned hunk is diff(B, A). To do
966 that, swap fromline/toline and +/- signs while keep other things
967 unchanged.
968 """
969 m = {'+': '-', '-': '+'}
970 hunk = ['%s%s' % (m[l[0]], l[1:]) for l in self.hunk]
971 return recordhunk(self.header, self.toline, self.fromline, self.proc,
972 self.before, hunk, self.after)
973
962 def write(self, fp):
974 def write(self, fp):
963 delta = len(self.before) + len(self.after)
975 delta = len(self.before) + len(self.after)
964 if self.after and self.after[-1] == '\\ No newline at end of file\n':
976 if self.after and self.after[-1] == '\\ No newline at end of file\n':
@@ -1493,7 +1505,7 b' def reversehunks(hunks):'
1493 c
1505 c
1494 1
1506 1
1495 2
1507 2
1496 @@ -1,6 +2,6 @@
1508 @@ -2,6 +1,6 @@
1497 c
1509 c
1498 1
1510 1
1499 2
1511 2
@@ -1501,26 +1513,17 b' def reversehunks(hunks):'
1501 +4
1513 +4
1502 5
1514 5
1503 d
1515 d
1504 @@ -5,3 +6,2 @@
1516 @@ -6,3 +5,2 @@
1505 5
1517 5
1506 d
1518 d
1507 -lastline
1519 -lastline
1508
1520
1509 '''
1521 '''
1510
1522
1511 from . import crecord as crecordmod
1512 newhunks = []
1523 newhunks = []
1513 for c in hunks:
1524 for c in hunks:
1514 if isinstance(c, crecordmod.uihunk):
1525 if util.safehasattr(c, 'reversehunk'):
1515 # curses hunks encapsulate the record hunk in _hunk
1526 c = c.reversehunk()
1516 c = c._hunk
1517 if isinstance(c, recordhunk):
1518 for j, line in enumerate(c.hunk):
1519 if line.startswith("-"):
1520 c.hunk[j] = "+" + c.hunk[j][1:]
1521 elif line.startswith("+"):
1522 c.hunk[j] = "-" + c.hunk[j][1:]
1523 c.added, c.removed = c.removed, c.added
1524 newhunks.append(c)
1527 newhunks.append(c)
1525 return newhunks
1528 return newhunks
1526
1529
General Comments 0
You need to be logged in to leave comments. Login now