mercurial/patch.py
changeset 14565 3cacc232f27f
parent 14564 65f4512e40e4
child 14566 d0c2cc11e611
equal deleted inserted replaced
14564:65f4512e40e4 14565:3cacc232f27f
  1191 
  1191 
  1192     while gitpatches:
  1192     while gitpatches:
  1193         gp = gitpatches.pop()[2]
  1193         gp = gitpatches.pop()[2]
  1194         yield 'file', ('a/' + gp.path, 'b/' + gp.path, None, gp)
  1194         yield 'file', ('a/' + gp.path, 'b/' + gp.path, None, gp)
  1195 
  1195 
  1196 def applydiff(ui, fp, changed, backend, store, strip=1, eolmode='strict'):
  1196 def applydiff(ui, fp, backend, store, strip=1, eolmode='strict'):
  1197     """Reads a patch from fp and tries to apply it.
  1197     """Reads a patch from fp and tries to apply it.
  1198 
  1198 
  1199     The dict 'changed' is filled in with all of the filenames changed
  1199     Returns 0 for a clean patch, -1 if any rejects were found and 1 if
  1200     by the patch. Returns 0 for a clean patch, -1 if any rejects were
  1200     there was any fuzz.
  1201     found and 1 if there was any fuzz.
       
  1202 
  1201 
  1203     If 'eolmode' is 'strict', the patch content and patched file are
  1202     If 'eolmode' is 'strict', the patch content and patched file are
  1204     read in binary mode. Otherwise, line endings are ignored when
  1203     read in binary mode. Otherwise, line endings are ignored when
  1205     patching then normalized according to 'eolmode'.
  1204     patching then normalized according to 'eolmode'.
  1206     """
  1205     """
  1207     return _applydiff(ui, fp, patchfile, backend, store, changed, strip=strip,
  1206     return _applydiff(ui, fp, patchfile, backend, store, strip=strip,
  1208                       eolmode=eolmode)
  1207                       eolmode=eolmode)
  1209 
  1208 
  1210 def _applydiff(ui, fp, patcher, backend, store, changed, strip=1,
  1209 def _applydiff(ui, fp, patcher, backend, store, strip=1,
  1211                eolmode='strict'):
  1210                eolmode='strict'):
  1212 
  1211 
  1213     def pstrip(p):
  1212     def pstrip(p):
  1214         return pathstrip(p, strip - 1)[1]
  1213         return pathstrip(p, strip - 1)[1]
  1215 
  1214 
  1220     for state, values in iterhunks(fp):
  1219     for state, values in iterhunks(fp):
  1221         if state == 'hunk':
  1220         if state == 'hunk':
  1222             if not current_file:
  1221             if not current_file:
  1223                 continue
  1222                 continue
  1224             ret = current_file.apply(values)
  1223             ret = current_file.apply(values)
  1225             if ret >= 0:
  1224             if ret > 0:
  1226                 changed.add(current_file.fname)
  1225                 err = 1
  1227                 if ret > 0:
       
  1228                     err = 1
       
  1229         elif state == 'file':
  1226         elif state == 'file':
  1230             if current_file:
  1227             if current_file:
  1231                 rejects += current_file.close()
  1228                 rejects += current_file.close()
  1232                 current_file = None
  1229                 current_file = None
  1233             afile, bfile, first_hunk, gp = values
  1230             afile, bfile, first_hunk, gp = values
  1234             copysource = None
  1231             copysource = None
  1235             if gp:
  1232             if gp:
  1236                 path = pstrip(gp.path)
  1233                 path = pstrip(gp.path)
  1237                 if gp.oldpath:
  1234                 if gp.oldpath:
  1238                     copysource = pstrip(gp.oldpath)
  1235                     copysource = pstrip(gp.oldpath)
  1239                 changed.add(path)
       
  1240                 if gp.op == 'RENAME':
  1236                 if gp.op == 'RENAME':
  1241                     backend.unlink(copysource)
  1237                     backend.unlink(copysource)
  1242                 if not first_hunk:
  1238                 if not first_hunk:
  1243                     if gp.op == 'DELETE':
  1239                     if gp.op == 'DELETE':
  1244                         backend.unlink(path)
  1240                         backend.unlink(path)
  1352     try:
  1348     try:
  1353         fp = open(patchobj, 'rb')
  1349         fp = open(patchobj, 'rb')
  1354     except TypeError:
  1350     except TypeError:
  1355         fp = patchobj
  1351         fp = patchobj
  1356     try:
  1352     try:
  1357         ret = applydiff(ui, fp, files, backend, store, strip=strip,
  1353         ret = applydiff(ui, fp, backend, store, strip=strip,
  1358                         eolmode=eolmode)
  1354                         eolmode=eolmode)
  1359     finally:
  1355     finally:
  1360         if fp != patchobj:
  1356         if fp != patchobj:
  1361             fp.close()
  1357             fp.close()
  1362         files.update(backend.close())
  1358         files.update(backend.close())