mercurial/patch.py
changeset 6280 9db24a36d182
parent 6275 fda369b5779c
child 6295 bace1990ab12
equal deleted inserted replaced
6279:c50ac875ffcb 6280:9db24a36d182
   503         self.ui.warn(_("Hunk #%d FAILED at %d\n") % (h.number, orig_start))
   503         self.ui.warn(_("Hunk #%d FAILED at %d\n") % (h.number, orig_start))
   504         self.rej.append(h)
   504         self.rej.append(h)
   505         return -1
   505         return -1
   506 
   506 
   507 class hunk:
   507 class hunk:
   508     def __init__(self, desc, num, lr, context, gitpatch=None):
   508     def __init__(self, desc, num, lr, context, create=False, remove=False):
   509         self.number = num
   509         self.number = num
   510         self.desc = desc
   510         self.desc = desc
   511         self.hunk = [ desc ]
   511         self.hunk = [ desc ]
   512         self.a = []
   512         self.a = []
   513         self.b = []
   513         self.b = []
   514         if context:
   514         if context:
   515             self.read_context_hunk(lr)
   515             self.read_context_hunk(lr)
   516         else:
   516         else:
   517             self.read_unified_hunk(lr)
   517             self.read_unified_hunk(lr)
   518         self.gitpatch = gitpatch
   518         self.create = create
       
   519         self.remove = remove and not create
   519 
   520 
   520     def read_unified_hunk(self, lr):
   521     def read_unified_hunk(self, lr):
   521         m = unidesc.match(self.desc)
   522         m = unidesc.match(self.desc)
   522         if not m:
   523         if not m:
   523             raise PatchError(_("bad hunk #%d") % self.number)
   524             raise PatchError(_("bad hunk #%d") % self.number)
   638         self.desc = "@@ -%d,%d +%d,%d @@\n" % (self.starta, self.lena,
   639         self.desc = "@@ -%d,%d +%d,%d @@\n" % (self.starta, self.lena,
   639                                              self.startb, self.lenb)
   640                                              self.startb, self.lenb)
   640         self.hunk[0] = self.desc
   641         self.hunk[0] = self.desc
   641 
   642 
   642     def reverse(self):
   643     def reverse(self):
       
   644         self.create, self.remove = self.remove, self.create
   643         origlena = self.lena
   645         origlena = self.lena
   644         origstarta = self.starta
   646         origstarta = self.starta
   645         self.lena = self.lenb
   647         self.lena = self.lenb
   646         self.starta = self.startb
   648         self.starta = self.startb
   647         self.lenb = origlena
   649         self.lenb = origlena
   668 
   670 
   669     def complete(self):
   671     def complete(self):
   670         return len(self.a) == self.lena and len(self.b) == self.lenb
   672         return len(self.a) == self.lena and len(self.b) == self.lenb
   671 
   673 
   672     def createfile(self):
   674     def createfile(self):
   673         create = self.gitpatch is None or self.gitpatch.op == 'ADD'
   675         return self.starta == 0 and self.lena == 0 and self.create
   674         return self.starta == 0 and self.lena == 0 and create
       
   675 
   676 
   676     def rmfile(self):
   677     def rmfile(self):
   677         remove = self.gitpatch is None or self.gitpatch.op == 'DELETE'
   678         return self.startb == 0 and self.lenb == 0 and self.remove
   678         return self.startb == 0 and self.lenb == 0 and remove
       
   679 
   679 
   680     def fuzzit(self, l, fuzz, toponly):
   680     def fuzzit(self, l, fuzz, toponly):
   681         # this removes context lines from the top and bottom of list 'l'.  It
   681         # this removes context lines from the top and bottom of list 'l'.  It
   682         # checks the hunk to make sure only context lines are removed, and then
   682         # checks the hunk to make sure only context lines are removed, and then
   683         # returns a new shortened list of lines.
   683         # returns a new shortened list of lines.
   910             ((context or context == None) and x.startswith('***************')))):
   910             ((context or context == None) and x.startswith('***************')))):
   911             try:
   911             try:
   912                 if context == None and x.startswith('***************'):
   912                 if context == None and x.startswith('***************'):
   913                     context = True
   913                     context = True
   914                 gpatch = changed.get(bfile[2:], (None, None))[1]
   914                 gpatch = changed.get(bfile[2:], (None, None))[1]
   915                 current_hunk = hunk(x, hunknum + 1, lr, context, gpatch)
   915                 create = afile == '/dev/null' or gpatch and gpatch.op == 'ADD'
       
   916                 remove = bfile == '/dev/null' or gpatch and gpatch.op == 'DELETE'
       
   917                 current_hunk = hunk(x, hunknum + 1, lr, context, create, remove)
   916             except PatchError, err:
   918             except PatchError, err:
   917                 ui.debug(err)
   919                 ui.debug(err)
   918                 current_hunk = None
   920                 current_hunk = None
   919                 continue
   921                 continue
   920             hunknum += 1
   922             hunknum += 1