Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sync repo darcs #7137

Merged
merged 6 commits into from
Aug 26, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 48 additions & 3 deletions cabal-install/src/Distribution/Client/VCS.hs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE NamedFieldPuns, RecordWildCards, RankNTypes #-}
module Distribution.Client.VCS (
-- * VCS driver type
Expand Down Expand Up @@ -45,20 +47,29 @@ import Distribution.Simple.Program
( Program(programFindVersion)
, ConfiguredProgram(programVersion)
, simpleProgram, findProgramVersion
, ProgramInvocation(..), programInvocation, runProgramInvocation
, ProgramInvocation(..), programInvocation, runProgramInvocation, getProgramInvocationOutput
, emptyProgramDb, requireProgram )
import Distribution.Version
( mkVersion )
import qualified Distribution.PackageDescription as PD

import Control.Applicative
( liftA2 )
import Control.Exception
( throw, try )
import Control.Monad.Trans
( liftIO )
import qualified Data.Char as Char
import qualified Data.List as List
import qualified Data.Map as Map
import System.FilePath
( takeDirectory )
import System.Directory
( doesDirectoryExist )
( doesDirectoryExist
, removeDirectoryRecursive
)
import System.IO.Error
( isDoesNotExistError )


-- | A driver for a version control system, e.g. git, darcs etc.
Expand Down Expand Up @@ -305,7 +316,41 @@ vcsDarcs =

vcsSyncRepos :: Verbosity -> ConfiguredProgram
-> [(SourceRepositoryPackage f, FilePath)] -> IO [MonitorFilePath]
vcsSyncRepos _v _p _rs = fail "sync repo not yet supported for darcs"
vcsSyncRepos _ _ [] = return []
vcsSyncRepos verbosity prog ((primaryRepo, primaryLocalDir) : secondaryRepos) =
monitors <$ do
vcsSyncRepo verbosity prog primaryRepo primaryLocalDir Nothing
for_ secondaryRepos $ \ (repo, localDir) ->
vcsSyncRepo verbosity prog repo localDir $ Just primaryLocalDir
where
dirs = primaryLocalDir : (snd <$> secondaryRepos)
monitors = monitorDirectoryExistence <$> dirs

vcsSyncRepo verbosity prog SourceRepositoryPackage{..} localDir _peer =
try (lines <$> darcsWithOutput localDir ["log", "--last", "1"]) >>= \ case
Right (_:_:_:x:_)
| Just tag <- (List.stripPrefix "tagged " . List.dropWhile Char.isSpace) x
, Just tag' <- srpTag
, tag == tag' -> pure ()
Left e | not (isDoesNotExistError e) -> throw e
_ -> do
removeDirectoryRecursive localDir `catch` liftA2 unless isDoesNotExistError throw
darcs (takeDirectory localDir) cloneArgs
where
darcs :: FilePath -> [String] -> IO ()
darcs = darcs' runProgramInvocation

darcsWithOutput :: FilePath -> [String] -> IO String
darcsWithOutput = darcs' getProgramInvocationOutput

darcs' f cwd args = f verbosity (programInvocation prog args)
{ progInvokeCwd = Just cwd }

cloneArgs = ["clone"] ++ tagArgs ++ [srpLocation, localDir] ++ verboseArg
tagArgs = case srpTag of
Nothing -> []
Just tag -> ["-t" ++ tag]
verboseArg = [ "--quiet" | verbosity < Verbosity.normal ]

darcsProgram :: Program
darcsProgram = (simpleProgram "darcs") {
Expand Down
2 changes: 2 additions & 0 deletions changelog.d/sync-repo-darcs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
synopsis: Sync repo darcs
prs: #7137