comparison mercurial/patch.py @ 5852:03ce5a919ae3

patch: handle empty vs no file in git patches (issue906)
author Patrick Mezard <pmezard@gmail.com>
date Sat, 12 Jan 2008 19:35:11 +0100
parents 03f550f9b554
children deb0d3518674
comparison
equal deleted inserted replaced
5851:03f550f9b554 5852:03ce5a919ae3
497 self.ui.warn(_("Hunk #%d FAILED at %d\n") % (h.number, orig_start)) 497 self.ui.warn(_("Hunk #%d FAILED at %d\n") % (h.number, orig_start))
498 self.rej.append(h) 498 self.rej.append(h)
499 return -1 499 return -1
500 500
501 class hunk: 501 class hunk:
502 def __init__(self, desc, num, lr, context): 502 def __init__(self, desc, num, lr, context, gitpatch=None):
503 self.number = num 503 self.number = num
504 self.desc = desc 504 self.desc = desc
505 self.hunk = [ desc ] 505 self.hunk = [ desc ]
506 self.a = [] 506 self.a = []
507 self.b = [] 507 self.b = []
508 if context: 508 if context:
509 self.read_context_hunk(lr) 509 self.read_context_hunk(lr)
510 else: 510 else:
511 self.read_unified_hunk(lr) 511 self.read_unified_hunk(lr)
512 self.gitpatch = gitpatch
512 513
513 def read_unified_hunk(self, lr): 514 def read_unified_hunk(self, lr):
514 m = unidesc.match(self.desc) 515 m = unidesc.match(self.desc)
515 if not m: 516 if not m:
516 raise PatchError(_("bad hunk #%d") % self.number) 517 raise PatchError(_("bad hunk #%d") % self.number)
661 662
662 def complete(self): 663 def complete(self):
663 return len(self.a) == self.lena and len(self.b) == self.lenb 664 return len(self.a) == self.lena and len(self.b) == self.lenb
664 665
665 def createfile(self): 666 def createfile(self):
666 return self.starta == 0 and self.lena == 0 667 create = self.gitpatch is None or self.gitpatch.op == 'ADD'
668 return self.starta == 0 and self.lena == 0 and create
667 669
668 def rmfile(self): 670 def rmfile(self):
669 return self.startb == 0 and self.lenb == 0 671 remove = self.gitpatch is None or self.gitpatch.op == 'DELETE'
672 return self.startb == 0 and self.lenb == 0 and remove
670 673
671 def fuzzit(self, l, fuzz, toponly): 674 def fuzzit(self, l, fuzz, toponly):
672 # this removes context lines from the top and bottom of list 'l'. It 675 # this removes context lines from the top and bottom of list 'l'. It
673 # checks the hunk to make sure only context lines are removed, and then 676 # checks the hunk to make sure only context lines are removed, and then
674 # returns a new shortened list of lines. 677 # returns a new shortened list of lines.
916 if ((sourcefile or state == BFILE) and ((not context and x[0] == '@') or 919 if ((sourcefile or state == BFILE) and ((not context and x[0] == '@') or
917 ((context or context == None) and x.startswith('***************')))): 920 ((context or context == None) and x.startswith('***************')))):
918 try: 921 try:
919 if context == None and x.startswith('***************'): 922 if context == None and x.startswith('***************'):
920 context = True 923 context = True
921 current_hunk = hunk(x, hunknum + 1, lr, context) 924 gpatch = changed.get(bfile[2:], (None, None))[1]
925 current_hunk = hunk(x, hunknum + 1, lr, context, gpatch)
922 except PatchError, err: 926 except PatchError, err:
923 ui.debug(err) 927 ui.debug(err)
924 current_hunk = None 928 current_hunk = None
925 continue 929 continue
926 hunknum += 1 930 hunknum += 1