{-# OPTIONS_GHC -fno-warn-orphans #-}moduleDarcs.Patch.Prim.V1.Apply()whereimportDarcs.Patch.Apply(Apply(..))importDarcs.Patch.Repair(RepairToFL(..))importDarcs.Patch.Prim.Class(PrimApply(..))importDarcs.Patch.Prim.V1.Core(Prim(..),DirPatchType(..),FilePatchType(..))importDarcs.Patch.Prim.V1.Show(showHunk)importDarcs.Patch.FileName(fn2fp)importDarcs.Patch.Format(FileNameFormat(..))importDarcs.Patch.TokenReplace(tryTokInternal)importDarcs.Patch.ApplyMonad(ApplyMonad(..))importStorage.Hashed.Tree(Tree)importDarcs.Witnesses.Ordered(FL(..),mapFL_FL,spanFL,(:>)(..))importDarcs.Witnesses.Unsafe(unsafeCoercePStart)importByteStringUtils(unlinesPS,breakAfterNthNewline,breakBeforeNthNewline,)importPrinter(renderString)importqualifiedData.ByteStringasB(ByteString,empty,null,concat)importqualifiedData.ByteString.Char8asBC(pack,singleton,unpack)importData.List(intersperse)#include "gadts.h"#include "impossible.h"typeFileContents=B.ByteStringinstanceApplyPrimwheretypeApplyStatePrim=Treeapply(FPfRmFile)=mRemoveFilefapply(FPfAddFile)=mCreateFilefapplyp@(FP_(Hunk___))=applyPrimFL(p:>:NilFL)apply(FPf(TokReplaceton))=mModifyFilePSsfdoreplacewheredoreplacels=casemapM(tryTokInternalt(BC.packo)(BC.packn))lsofNothing->fail$"replace patch to "++fn2fpf++" couldn't apply."Justls'->return$mapB.concatls'apply(FPf(Binaryon))=mModifyFilePSfdoapplywheredoapplyoldf=ifo==oldfthenreturnnelsefail$"binary patch to "++fn2fpf++" couldn't apply."apply(DPdAddDir)=mCreateDirectorydapply(DPdRmDir)=mRemoveDirectorydapply(Moveff')=mRenameff'apply(ChangePrefpft)=mChangePrefpftinstanceRepairToFLPrimwhereapplyAndTryToFixFL(FPfRmFile)=dox<-mReadFilePSfmRemoveFilefreturn$caseB.nullxofTrue->NothingFalse->Just("WARNING: Fixing removal of non-empty file "++fn2fpf,-- No need to coerce because the content-- removal patch has freely decided contextsFPf(BinaryxB.empty):>:FPfRmFile:>:NilFL)applyAndTryToFixFL(FPfAddFile)=doexists<-mDoesFileExistfifexiststhenreturn$Just("WARNING: Dropping add of existing file "++fn2fpf,-- the old context was wrong, so we have to coerceunsafeCoercePStartNilFL)elsedomCreateFilefreturnNothingapplyAndTryToFixFL(DPfAddDir)=doexists<-mDoesDirectoryExistfifexiststhenreturn$Just("WARNING: Dropping add of existing directory "++fn2fpf,-- the old context was wrong, so we have to coerceunsafeCoercePStartNilFL)elsedomCreateDirectoryfreturnNothingapplyAndTryToFixFLp=doapplyp;returnNothinginstancePrimApplyPrimwhereapplyPrimFLNilFL=return()applyPrimFL((FPfh@(Hunk___)):>:the_ps)=casespanFLf_hunkthe_psof(xs:>ps')->doletfoo=h:>:mapFL_FL(\(FP_h')->h')xsmModifyFilePSf$hunkmodfooapplyPrimFLps'wheref_hunk(FPf'(Hunk___))|f==f'=Truef_hunk_=Falsehunkmod::ApplyMonadmTree=>FLFilePatchTypeC(xy)->B.ByteString->mB.ByteStringhunkmodNilFLps=returnpshunkmod(Hunklineoldnew:>:hs)ps=caseapplyHunkLines[(line,old,new)]psofJustps'->hunkmodhsps'Nothing->fail$"### Error applying:\n"++(renderString$showHunkNewFormatflineoldnew)++"\n### to file "++fn2fpf++":\n"++BC.unpackpshunkmod__=impossibleapplyPrimFL(p:>:ps)=doapplypapplyPrimFLpsapplyHunks::[(Int,[B.ByteString],[B.ByteString])]->B.ByteString->Maybe[B.ByteString]applyHunks[]ps=Just[ps]applyHunks((l,[],n):hs)ps=casebreakBeforeNthNewline(l-2)psof(prfix,after_prefix)->dorest<-applyHunkshsafter_prefixreturn$interspersenl(prfix:n)++restwherenl=BC.singleton'\n'applyHunks((l,o,n):hs)ps=casebreakBeforeNthNewline(l-2)psof(prfix,after_prefix)->casebreakBeforeNthNewline(lengtho)after_prefixof(oo,_)|oo/=unlinesPS(B.empty:o)->fail"applyHunks error"(_,suffix)->dorest<-applyHunkshssuffixreturn$interspersenl(prfix:n)++restwherenl=BC.singleton'\n'applyHunkLines::[(Int,[B.ByteString],[B.ByteString])]->FileContents->MaybeFileContentsapplyHunkLines[]c=JustcapplyHunkLines[(1,[],n)]ps|B.nullps=Just$unlinesPS(n++[B.empty])applyHunkLineshs@((l,o,n):hs')ps=dopss<-caselof1->casebreakAfterNthNewline(lengtho)psofNothing->ifps==unlinesPSothenreturn$interspersenlnelsefail"applyHunkLines: Unexpected hunks"Just(shouldbeo,suffix)|shouldbeo/=unlinesPS(o++[B.empty])->fail$"applyHunkLines: Bad patch!"|nulln->dox<-applyHunkLineshs'suffixreturn[x]|otherwise->dorest<-applyHunkshs'suffixreturn$interspersenln++nl:rest_|l<0->bug"Prim.applyHunkLines: After -ve lines?"|otherwise->applyHunkshspsletresult=B.concatpssreturnresultwherenl=BC.singleton'\n'