我想编写一个Haskell程序来查询关于darcs存储库的信息。与其调用darcs可执行文件并解析结果,我更愿意直接使用darcs库。它是据说是“非常正在进行的工作”和“缺乏一个稳定的API",但似乎是有用的。
我想我可以通过学习darcsden源代码来回答我的问题,例如,从这个模块开始,但是我认为这不仅对我有帮助,如果一个知识渊博的人提供一个评论介绍来补充这样的研究。
下面是一个具体的例子。
如何计算给定文件的最新修补程序以及修补程序的日期、作者和名称?如果您解释解决方案中使用的关键库函数,这将是非常有用的。
编辑:
以下是一些对不熟悉darcs源代码的人来说可能并不明显的评论。我从贾森·达吉特的硕士论文那里学到了他们,希望他们能帮助我理解Ganesh给出的答案。
在Darcs中,补丁在应用补丁之前和之后都有一个预上下文和后上下文,表示存储库的状态。在源代码中,这些上下文是在补丁类型上使用幻影类型建模的。这些幻影类型被称为见证者,seal2被用来消除它们。
在补丁列表中,只有第一个预上下文和最后一个后上下文在类型中被表示。所有其他上下文都使用存在类型隐藏。Darcs定义前向列表(称为FL)和反向列表(称为RL)。反向列出存储修补程序(按时间顺序)(由darcs完成的模块化修补程序重新排序)。反向列表可用于访问头部位置的最新修补程序。所有以RL命名的函数都创建或操作这样的反向列表。
发布于 2012-10-18 12:09:22
-- This works with darcs 2.9.5 (a tag in the development repo
-- at http://darcs.net/screened).
--
-- It should work with darcs 2.8.2 with the following changes:
-- - some minor namespace changes
-- - change withRepositoryDirectory to pass [] instead of YesUseCache
-- - comment out the line below that uses the "patch index"
import Control.Applicative ( (<$>) )
import Darcs.Patch.Info ( PatchInfo )
import Darcs.Patch.Inspect ( listTouchedFiles )
import Darcs.Patch.PatchInfoAnd ( info )
import Darcs.Patch.Set ( newset2RL )
import Darcs.Patch.Witnesses.Ordered ( mapRL )
import Darcs.Patch.Witnesses.Sealed ( seal2, unseal2 )
import Darcs.Repository
( withRepositoryDirectory, RepoJob(..), readRepo )
import Darcs.Repository.FileMod ( filterPatches )
import Darcs.Repository.Flags ( UseCache(..) )
import Data.Maybe ( listToMaybe )
getChange
:: FilePath -- ^repository directory
-> FilePath -- ^file path
-> IO (Maybe PatchInfo) -- ^patch metadata
getChange repoDir fileName =
-- Select the repository from repositoryDirectory.
--
-- The function parameter to 'RepoJob' needs to be polymorphic
-- in the underlying patch type (darcs-1 or darcs-2).
withRepositoryDirectory YesUseCache repoDir $ RepoJob $ \repo -> do
-- 'readRepo' gives us a PatchSet, a lazy witnessed list of all
-- the patches structured by "clean tags".
--
-- We use 'newset2RL' to get rid of the tag structure as we don't
-- need it, and 'mapRL seal2' to get rid of the witnesses which we
-- also don't need. The result is of type '[Sealed2 p]', where 'p'
-- is the underlying patch type of the repository we are reading
-- (either darcs-1 or darcs-2)
patches <- mapRL seal2 . newset2RL <$> readRepo repo
-- Use the recently introduced "patch index" to filter the list of
-- patches from the repo down to ones that just touch 'fileName'.
--
-- This step is optional: we can remove it and the result will be
-- the same, but substantially slower on large repositories where
-- the patch we want is far back in the repo.
patches <- filterPatches repo [fileName] patches
-- Use 'filter' and 'listToMaybe' to get the first patch that touches
-- 'fileName'.
--
-- The filter is superfluous in this simple case if the patch
-- index was used, but doesn't cost much if so.
--
-- Note that this doesn't track renames, so isn't suitable for
-- finding anything but the last patch that touched 'fileName'.
--
-- 'unseal2' is used to lift a function that works on witnessed
-- patches to one that works on "sealed" patches.
let wanted = unseal2 (\patch -> fileName `elem` listTouchedFiles patch)
let thepatch = listToMaybe . filter wanted $ patches
-- Finally, return the metadata of the patch.
--
-- Things get a little bit more complex if we want to deal
-- with the contents of the patch, because the specific
-- patch type isn't known statically - it might be
-- darcs-1 or darcs-2.
--
-- The best approach is to write a polymorphic function that
-- can accept any instance of 'RepoPatch'.
return (fmap (unseal2 info) thepatch)https://stackoverflow.com/questions/12941793
复制相似问题