123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166(**************************************************************************)(* *)(* Copyright 2012-2019 OCamlPro *)(* Copyright 2012 INRIA *)(* *)(* All rights reserved. This file is distributed under the terms of the *)(* GNU Lesser General Public License version 2.1, with the special *)(* exception on linking described in the file LICENSE. *)(* *)(**************************************************************************)openOpamFilename.OpopenOpamProcess.Job.OpmoduleVCS=structletname=`hgletmark_prefix="opam-mark"letexistsrepo_root=OpamFilename.exists_dir(repo_root/".hg")lethgrepo_root=letdir=OpamFilename.Dir.to_stringrepo_rootinfun?verbose?env?stdoutargs->OpamSystem.make_command~dir?verbose?env?stdout"hg"argsletinitrepo_root_repo_url=OpamFilename.mkdirrepo_root;hgrepo_root["init"]@@>funr->OpamSystem.raise_on_process_errorr;Done()letmark_from_urlurl=matchurl.OpamUrl.hashwith|None->mark_prefix|Somefragment->mark_prefix^"-"^fragmentletfetch?cache_dir:_?subpath:_repo_rootrepo_url=letsrc=OpamUrl.base_urlrepo_urlinletrev=OpamStd.Option.default"default"repo_url.OpamUrl.hashinletmark=mark_from_urlrepo_urlinhgrepo_root["pull";"--rev";rev;src]@@>funr->OpamSystem.raise_on_process_errorr;hgrepo_root["bookmark";"--force";"--rev";rev;mark]@@>funr->OpamSystem.raise_on_process_errorr;Done()letrevisionrepo_root=hgrepo_root["identify";"--id"]@@>funr->OpamSystem.raise_on_process_errorr;matchr.OpamProcess.r_stdoutwith|[]->DoneNone|full::_->Done(Somefull)letcleanrepo_root=hgrepo_root["revert";"--all";"--no-backup"]@@>funr->OpamSystem.raise_on_process_errorr;Done()letreset_treerepo_rootrepo_url=letmark=mark_from_urlrepo_urlinhgrepo_root["update";"--clean";"--rev";mark]@@>funr->OpamSystem.raise_on_process_errorr;Done()letpatch_applied=reset_treeletdiffrepo_rootrepo_url=letpatch_file=OpamSystem.temp_file~auto_clean:false"hg-diff"inletfinalise()=OpamSystem.remove_filepatch_fileinOpamProcess.Job.catch(fune->finalise();raisee)@@fun()->letmark=mark_from_urlrepo_urlinhgrepo_root~stdout:patch_file["diff";"--text";"--subrepos";"--reverse";"--rev";mark]@@>funr->ifOpamProcess.is_failurerthen(finalise();OpamSystem.internal_error"Hg error: '%s' not found."mark)elseifOpamSystem.file_is_emptypatch_filethen(finalise();DoneNone)elseDone(Some(OpamFilename.of_stringpatch_file))letis_up_to_daterepo_rootrepo_url=letmark=mark_from_urlrepo_urlinhgrepo_root["status";"--subrepos";"--rev";mark]@@>funr->OpamSystem.raise_on_process_errorr;Done(r.OpamProcess.r_stdout=[])letversioned_filesrepo_root=hgrepo_root["locate"]@@>funr->OpamSystem.raise_on_process_errorr;Doner.OpamProcess.r_stdoutletvc_dirrepo_root=OpamFilename.Op.(repo_root/".hg")letcurrent_branchrepo_root=hgrepo_root["identify";"--bookmarks"]@@>funr->OpamSystem.raise_on_process_errorr;matchr.OpamProcess.r_stdoutwith|[]->DoneNone|marks::_->letmarks=OpamStd.String.splitmarks' 'inletmarks=List.filter(OpamStd.String.starts_with~prefix:mark_prefix)marksinmatchmarkswith|mark::_->Done(Somemark)|[]->hgrepo_root["identify";"--branch"]@@>funr->OpamSystem.raise_on_process_errorr;matchr.OpamProcess.r_stdoutwith|branch::_whenbranch<>"default"->Done(Somebranch)|_->DoneNoneletis_dirty?subpath:_repo_root=hgrepo_root["status";"--subrepos"]@@>funr->OpamSystem.raise_on_process_errorr;Done(r.OpamProcess.r_stdout=[])letmodified_filesrepo_root=hgrepo_root["status";"--subrepos"]@@>funr->OpamSystem.raise_on_process_errorr;letfiles=OpamStd.List.filter_map(funline->matchOpamStd.String.splitline' 'with|("A"|"M")::file::[]->Somefile|_->None)r.OpamProcess.r_stdoutinDonefilesletget_remote_url?hashrepo_root=hgrepo_root["paths";"default"]@@>function|{OpamProcess.r_code=0;_}asr->(matchr.r_stdoutwith|[url]->(leturl=OpamUrl.parse~backend:`hgurlinifOpamUrl.local_dirurl<>NonethenDoneNoneelseletcheck_remotehash=hgrepo_root["id";"-r";hash;"default"]@@>funr->ifOpamProcess.is_successrthenDone(Some{urlwithhash=Somehash})(* default branch of hg is default *)elseDone(Some{urlwithhash=None})inmatchhashwith|None->(hgrepo_root["branch"]@@>function|{OpamProcess.r_code=0;OpamProcess.r_stdout=[hash];_}->check_remotehash|{OpamProcess.r_code=0;_}|{OpamProcess.r_code=1;_}->Done(Someurl)|r->OpamSystem.process_errorr)|Somehash->check_remotehash)|_->DoneNone)|{OpamProcess.r_code=1;_}->DoneNone|r->OpamSystem.process_errorrendmoduleB=OpamVCS.Make(VCS)