如何使用darcs库查询补丁信息?

时间:2012-10-17 19:21:43

标签: haskell darcs

我想编写一个Haskell程序来查询有关darcs存储库的信息。我宁愿直接使用darcs库,而不是调用darcs可执行文件并解析结果。它是said to be“正在进行的工作”和“缺乏稳定的API”,但似乎可以使用。

我想我可以通过研究darcsden源代码来回答我的问题,例如,从this module开始,但我认为,如果知识渊博的人提供补充此类补充的评论性介绍,它不仅对我有用。一项研究。

所以,这是一个具体的例子。

如何为给定文件计算影响它的最新补丁以及补丁的日期,作者和名称?如果您解释解决方案中使用的密钥库函数,那将非常有用。


编辑:

对于不熟悉darc源代码的人来说,这些评论可能并不明显。我从Jason Dagit's master thesis学到了它们,并希望它们有助于理解Ganesh给出的答案。

在Darcs中,补丁具有前置和后置上下文,表示应用补丁之前和之后的存储库状态。在源代码中,这些上下文使用关于补丁类型的幻像类型进行建模。这些幻像类型称为见证人,seal2用于摆脱它们。

在补丁列表中,只有第一个前置上下文和最后一个后置上下文在类型中表示。使用存在类型隐藏所有其他上下文。 Darcs定义了前向列表(称为FL)和反向列表(称为RL)。反向列表以反向(按时间顺序)顺序存储补丁(由darcs完成的模补丁重新排序)。反向列表可用于访问头部位置的最新补丁。名称中包含RL的所有函数都在此类反向列表上创建或运行。

1 个答案:

答案 0 :(得分:4)

-- 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)