2021-05-14 14:59:48 +00:00
|
|
|
{- git-annex command
|
|
|
|
-
|
|
|
|
- Copyright 2021 Joey Hess <id@joeyh.name>
|
|
|
|
-
|
|
|
|
- Licensed under the GNU AGPL version 3 or higher.
|
|
|
|
-}
|
|
|
|
|
2021-05-17 15:06:47 +00:00
|
|
|
{-# LANGUAGE OverloadedStrings #-}
|
|
|
|
|
2021-05-14 14:59:48 +00:00
|
|
|
module Command.FilterBranch where
|
|
|
|
|
|
|
|
import Command
|
2021-05-17 15:06:47 +00:00
|
|
|
import qualified Annex
|
|
|
|
import qualified Annex.Branch
|
|
|
|
import Annex.Branch.Transitions (filterBranch, FileTransition(..))
|
|
|
|
import Annex.HashObject
|
|
|
|
import Annex.Tmp
|
2021-05-17 18:04:14 +00:00
|
|
|
import Annex.SpecialRemote.Config
|
|
|
|
import Types.ProposedAccepted
|
2021-05-17 15:06:47 +00:00
|
|
|
import Logs
|
2021-05-17 18:04:14 +00:00
|
|
|
import Logs.Remote
|
2021-05-17 15:06:47 +00:00
|
|
|
import Git.Types
|
|
|
|
import Git.FilePath
|
|
|
|
import Git.Index
|
|
|
|
import Git.Env
|
|
|
|
import Git.UpdateIndex
|
|
|
|
import qualified Git.LsTree as LsTree
|
2021-05-17 17:24:58 +00:00
|
|
|
import qualified Git.Branch as Git
|
2021-05-17 15:06:47 +00:00
|
|
|
import Utility.RawFilePath
|
2021-05-14 14:59:48 +00:00
|
|
|
|
2021-05-17 18:04:14 +00:00
|
|
|
import qualified Data.Map as M
|
2021-05-14 18:14:46 +00:00
|
|
|
import qualified Data.Set as S
|
2021-05-17 15:06:47 +00:00
|
|
|
import qualified Data.ByteString.Lazy as L
|
|
|
|
import Data.ByteString.Builder
|
|
|
|
import qualified System.FilePath.ByteString as P
|
2021-05-14 18:14:46 +00:00
|
|
|
|
2021-05-14 14:59:48 +00:00
|
|
|
cmd :: Command
|
2021-05-17 15:06:47 +00:00
|
|
|
cmd = noMessages $ withGlobalOptions [annexedMatchingOptions] $
|
2021-05-14 14:59:48 +00:00
|
|
|
command "filter-branch" SectionMaintenance
|
|
|
|
"filter information from the git-annex branch"
|
|
|
|
paramPaths (seek <$$> optParser)
|
|
|
|
|
|
|
|
data FilterBranchOptions = FilterBranchOptions
|
|
|
|
{ includeFiles :: CmdParams
|
|
|
|
, keyOptions :: Maybe KeyOptions
|
2021-05-14 18:14:46 +00:00
|
|
|
, keyInformation :: [IncludeExclude (DeferredParse UUID)]
|
|
|
|
, repoConfig :: [IncludeExclude (DeferredParse UUID)]
|
2021-05-14 14:59:48 +00:00
|
|
|
, includeGlobalConfig :: Bool
|
|
|
|
}
|
|
|
|
|
|
|
|
optParser :: CmdParamsDesc -> Parser FilterBranchOptions
|
|
|
|
optParser desc = FilterBranchOptions
|
|
|
|
<$> cmdParams desc
|
|
|
|
<*> optional parseKeyOptions
|
2021-05-14 18:14:46 +00:00
|
|
|
<*> many (parseIncludeExclude "key-information")
|
|
|
|
<*> many (parseIncludeExclude "repo-config")
|
2021-05-14 14:59:48 +00:00
|
|
|
<*> switch
|
|
|
|
( long "include-global-config"
|
|
|
|
<> help "include global configuration"
|
|
|
|
)
|
2021-05-14 18:14:46 +00:00
|
|
|
|
|
|
|
data IncludeExclude t
|
|
|
|
= Include t
|
|
|
|
| Exclude t
|
|
|
|
| IncludeAll
|
|
|
|
deriving (Show, Eq, Ord)
|
|
|
|
|
|
|
|
isInclude :: IncludeExclude t -> Bool
|
|
|
|
isInclude (Include _) = True
|
|
|
|
isInclude IncludeAll = True
|
|
|
|
isInclude (Exclude _) = False
|
|
|
|
|
|
|
|
parseIncludeExclude :: String -> Parser (IncludeExclude (DeferredParse UUID))
|
|
|
|
parseIncludeExclude s =
|
|
|
|
( Include <$> parseRepositoryOption
|
|
|
|
("include-" ++ s ++ "-for")
|
|
|
|
"include information about a repository"
|
|
|
|
) <|>
|
|
|
|
( Exclude <$> parseRepositoryOption
|
|
|
|
("exclude-" ++ s ++ "-for")
|
|
|
|
"exclude information about a repository"
|
|
|
|
) <|>
|
|
|
|
( flag' IncludeAll
|
|
|
|
( long ("include-all-" ++ s)
|
|
|
|
<> help "include information about all non-excluded repositories"
|
2021-05-14 14:59:48 +00:00
|
|
|
)
|
2021-05-14 18:14:46 +00:00
|
|
|
)
|
2021-05-14 14:59:48 +00:00
|
|
|
|
|
|
|
parseRepositoryOption :: String -> String -> Parser (DeferredParse UUID)
|
|
|
|
parseRepositoryOption s h = parseUUIDOption <$> strOption
|
|
|
|
( long s
|
|
|
|
<> help h
|
2021-05-14 18:14:46 +00:00
|
|
|
<> metavar (paramRemote `paramOr` paramDesc `paramOr` paramUUID)
|
2021-05-14 14:59:48 +00:00
|
|
|
<> completeRemotes
|
|
|
|
)
|
|
|
|
|
2021-05-14 18:14:46 +00:00
|
|
|
mkUUIDMatcher :: [IncludeExclude (DeferredParse UUID)] -> Annex (UUID -> Bool)
|
2021-05-17 18:04:14 +00:00
|
|
|
mkUUIDMatcher l = do
|
|
|
|
sameasmap <- M.mapMaybe
|
|
|
|
(toUUID . fromProposedAccepted <$$> M.lookup sameasUUIDField)
|
|
|
|
<$> remoteConfigMap
|
|
|
|
mkUUIDMatcher' sameasmap <$> mapM get l
|
2021-05-14 18:14:46 +00:00
|
|
|
where
|
|
|
|
get (Include v) = Include <$> getParsed v
|
|
|
|
get (Exclude v) = Exclude <$> getParsed v
|
|
|
|
get IncludeAll = pure IncludeAll
|
|
|
|
|
2021-05-17 18:04:14 +00:00
|
|
|
mkUUIDMatcher' :: M.Map UUID UUID -> [IncludeExclude UUID] -> (UUID -> Bool)
|
|
|
|
mkUUIDMatcher' sameasmap l = \u ->
|
|
|
|
let sameas = M.lookup u sameasmap
|
|
|
|
in ( S.member (Include u) includes
|
|
|
|
|| S.member IncludeAll includes
|
|
|
|
|| maybe False (\u' -> S.member (Include u') includes) sameas
|
|
|
|
)
|
2021-05-14 18:14:46 +00:00
|
|
|
&& S.notMember (Exclude u) excludes
|
2021-05-17 18:04:14 +00:00
|
|
|
&& maybe True (\u' -> S.notMember (Exclude u') excludes) sameas
|
2021-05-14 18:14:46 +00:00
|
|
|
where
|
2021-05-17 18:04:14 +00:00
|
|
|
(includes, excludes) = (S.partition isInclude (S.fromList l))
|
2021-05-14 18:14:46 +00:00
|
|
|
|
2021-05-14 14:59:48 +00:00
|
|
|
seek :: FilterBranchOptions -> CommandSeek
|
2021-05-17 15:06:47 +00:00
|
|
|
seek o = withOtherTmp $ \tmpdir -> do
|
|
|
|
let tmpindex = tmpdir P.</> "index"
|
|
|
|
gc <- Annex.getGitConfig
|
2021-05-17 17:24:58 +00:00
|
|
|
tmpindexrepo <- Annex.inRepo $ \r ->
|
2021-05-17 15:06:47 +00:00
|
|
|
addGitEnv r indexEnv (fromRawFilePath tmpindex)
|
2021-05-17 17:24:58 +00:00
|
|
|
withUpdateIndex tmpindexrepo $ \h -> do
|
2021-05-17 15:06:47 +00:00
|
|
|
keyinfomatcher <- mkUUIDMatcher (keyInformation o)
|
2021-05-17 17:24:58 +00:00
|
|
|
repoconfigmatcher <- mkUUIDMatcher (repoConfig o)
|
|
|
|
|
2021-05-17 15:06:47 +00:00
|
|
|
let addtoindex f sha = liftIO $ streamUpdateIndex' h $
|
|
|
|
pureStreamer $ L.fromStrict $ LsTree.formatLsTree $ LsTree.TreeItem
|
|
|
|
{ LsTree.mode = fromTreeItemType TreeFile
|
|
|
|
, LsTree.typeobj = fmtObjectType BlobObject
|
|
|
|
, LsTree.sha = sha
|
|
|
|
, LsTree.size = Nothing
|
|
|
|
, LsTree.file = asTopFilePath f
|
|
|
|
}
|
|
|
|
|
|
|
|
let filterbanch matcher f c
|
|
|
|
| L.null c = noop
|
|
|
|
| otherwise = case filterBranch matcher gc f c of
|
|
|
|
ChangeFile builder -> do
|
|
|
|
let c' = toLazyByteString builder
|
|
|
|
unless (L.null c') $
|
|
|
|
addtoindex f =<< hashBlob c'
|
|
|
|
-- This could perhaps be optimised by looking
|
|
|
|
-- up the sha of the file in the branch.
|
|
|
|
PreserveFile -> addtoindex f =<< hashBlob c
|
|
|
|
|
2021-05-17 17:24:58 +00:00
|
|
|
-- Add information for all keys that are being included,
|
|
|
|
-- filtering out information for repositories that are not
|
|
|
|
-- being included.
|
2021-05-17 15:06:47 +00:00
|
|
|
let addkeyinfo k = startingCustomOutput k $ do
|
|
|
|
forM_ (keyLogFiles gc k) $ \f ->
|
|
|
|
filterbanch keyinfomatcher f
|
|
|
|
=<< Annex.Branch.get f
|
|
|
|
next (return True)
|
|
|
|
let seeker = AnnexedFileSeeker
|
|
|
|
{ startAction = \_ _ k -> addkeyinfo k
|
|
|
|
, checkContentPresent = Nothing
|
|
|
|
, usesLocationLog = True
|
|
|
|
}
|
|
|
|
-- Avoid the usual default of all files in the current
|
|
|
|
-- directory and below, because this command is documented
|
|
|
|
-- as only including the information it has explicitly been
|
|
|
|
-- told to include.
|
|
|
|
when (not (null (includeFiles o)) || isJust (keyOptions o)) $
|
|
|
|
withKeyOptions (keyOptions o) False seeker
|
|
|
|
(commandAction . \(_, k, _) -> addkeyinfo k)
|
|
|
|
(withFilesInGitAnnex ww seeker)
|
|
|
|
=<< workTreeItems ww (includeFiles o)
|
|
|
|
|
2021-05-17 17:24:58 +00:00
|
|
|
-- Add repository configs for all repositories that are
|
|
|
|
-- being included.
|
|
|
|
forM_ topLevelUUIDBasedLogs $ \f ->
|
|
|
|
filterbanch repoconfigmatcher f
|
|
|
|
=<< Annex.Branch.get f
|
|
|
|
|
|
|
|
-- Add global configs when included.
|
|
|
|
when (includeGlobalConfig o) $
|
|
|
|
forM_ otherTopLevelLogs $ \f -> do
|
|
|
|
c <- Annex.Branch.get f
|
|
|
|
unless (L.null c) $
|
|
|
|
addtoindex f =<< hashBlob c
|
|
|
|
|
|
|
|
-- Commit the temporary index, and output the result.
|
|
|
|
t <- liftIO $ Git.writeTree tmpindexrepo
|
2021-05-17 15:06:47 +00:00
|
|
|
liftIO $ removeWhenExistsWith removeLink tmpindex
|
2021-05-17 17:24:58 +00:00
|
|
|
cmode <- annexCommitMode <$> Annex.getGitConfig
|
|
|
|
cmessage <- Annex.Branch.commitMessage
|
|
|
|
c <- inRepo $ Git.commitTree cmode cmessage [] t
|
|
|
|
liftIO $ putStrLn (fromRef c)
|
2021-05-17 15:06:47 +00:00
|
|
|
where
|
|
|
|
ww = WarnUnmatchLsFiles
|