2015-12-04 17:02:56 +00:00
|
|
|
{- git-annex command
|
|
|
|
-
|
|
|
|
- Copyright 2015 Joey Hess <id@joeyh.name>
|
|
|
|
-
|
|
|
|
- Licensed under the GNU GPL version 3 or higher.
|
|
|
|
-}
|
|
|
|
|
|
|
|
module Command.Smudge where
|
|
|
|
|
|
|
|
import Common.Annex
|
|
|
|
import Command
|
2015-12-04 19:30:06 +00:00
|
|
|
import Annex.Content
|
2015-12-09 18:25:33 +00:00
|
|
|
import Annex.Link
|
2015-12-04 19:30:06 +00:00
|
|
|
import Annex.MetaData
|
|
|
|
import Annex.FileMatcher
|
2015-12-15 17:06:52 +00:00
|
|
|
import Annex.InodeSentinal
|
2015-12-04 19:30:06 +00:00
|
|
|
import Types.KeySource
|
|
|
|
import Backend
|
|
|
|
import Logs.Location
|
2015-12-09 21:00:37 +00:00
|
|
|
import qualified Database.Keys
|
2015-12-04 18:03:10 +00:00
|
|
|
|
|
|
|
import qualified Data.ByteString.Lazy as B
|
2015-12-04 17:02:56 +00:00
|
|
|
|
|
|
|
cmd :: Command
|
2015-12-04 19:30:06 +00:00
|
|
|
cmd = noCommit $ noMessages $
|
2015-12-04 17:02:56 +00:00
|
|
|
command "smudge" SectionPlumbing
|
|
|
|
"git smudge filter"
|
2015-12-04 19:30:06 +00:00
|
|
|
paramFile (seek <$$> optParser)
|
2015-12-04 17:02:56 +00:00
|
|
|
|
2015-12-04 19:30:06 +00:00
|
|
|
data SmudgeOptions = SmudgeOptions
|
|
|
|
{ smudgeFile :: FilePath
|
|
|
|
, cleanOption :: Bool
|
|
|
|
}
|
2015-12-04 17:02:56 +00:00
|
|
|
|
2015-12-04 19:30:06 +00:00
|
|
|
optParser :: CmdParamsDesc -> Parser SmudgeOptions
|
|
|
|
optParser desc = SmudgeOptions
|
|
|
|
<$> argument str ( metavar desc )
|
|
|
|
<*> switch ( long "clean" <> help "clean filter" )
|
|
|
|
|
|
|
|
seek :: SmudgeOptions -> CommandSeek
|
|
|
|
seek o = commandAction $
|
|
|
|
(if cleanOption o then clean else smudge) (smudgeFile o)
|
|
|
|
|
2015-12-04 21:18:26 +00:00
|
|
|
-- Smudge filter is fed git file content, and if it's a pointer to an
|
|
|
|
-- available annex object, should output its content.
|
2015-12-04 19:30:06 +00:00
|
|
|
smudge :: FilePath -> CommandStart
|
2015-12-07 18:35:46 +00:00
|
|
|
smudge file = do
|
2015-12-09 18:25:33 +00:00
|
|
|
b <- liftIO $ B.hGetContents stdin
|
|
|
|
case parseLinkOrPointer b of
|
|
|
|
Nothing -> liftIO $ B.putStr b
|
2015-12-04 18:03:10 +00:00
|
|
|
Just k -> do
|
2015-12-11 21:53:37 +00:00
|
|
|
-- A previous unlocked checkout of the file may have
|
|
|
|
-- led to the annex object getting modified;
|
|
|
|
-- don't provide such modified content as it
|
2015-12-15 17:06:52 +00:00
|
|
|
-- will be confusing. inAnnex will detect such
|
2015-12-11 21:53:37 +00:00
|
|
|
-- modifications.
|
|
|
|
ifM (inAnnex k)
|
|
|
|
( do
|
|
|
|
content <- calcRepo (gitAnnexLocation k)
|
|
|
|
liftIO $ B.putStr . fromMaybe b
|
|
|
|
=<< catchMaybeIO (B.readFile content)
|
|
|
|
, liftIO $ B.putStr b
|
|
|
|
)
|
2015-12-09 21:47:05 +00:00
|
|
|
Database.Keys.addAssociatedFile k file
|
2015-12-04 18:03:10 +00:00
|
|
|
stop
|
2015-12-04 19:30:06 +00:00
|
|
|
|
2015-12-09 19:24:32 +00:00
|
|
|
-- Clean filter is fed file content on stdin, decides if a file
|
|
|
|
-- should be stored in the annex, and outputs a pointer to its
|
|
|
|
-- injested content.
|
2015-12-04 19:30:06 +00:00
|
|
|
clean :: FilePath -> CommandStart
|
|
|
|
clean file = do
|
2015-12-09 19:24:32 +00:00
|
|
|
b <- liftIO $ B.hGetContents stdin
|
|
|
|
if isJust (parseLinkOrPointer b)
|
|
|
|
then liftIO $ B.hPut stdout b
|
|
|
|
else ifM (shouldAnnex file)
|
|
|
|
( do
|
|
|
|
k <- ingest file
|
2015-12-15 17:06:52 +00:00
|
|
|
oldkeys <- filter (/= k)
|
|
|
|
<$> Database.Keys.getAssociatedKey file
|
|
|
|
mapM_ (cleanOldKey file) oldkeys
|
2015-12-09 21:47:05 +00:00
|
|
|
Database.Keys.addAssociatedFile k file
|
2015-12-09 19:24:32 +00:00
|
|
|
liftIO $ emitPointer k
|
|
|
|
, liftIO $ B.hPut stdout b
|
|
|
|
)
|
2015-12-04 19:30:06 +00:00
|
|
|
stop
|
|
|
|
|
2015-12-15 17:06:52 +00:00
|
|
|
-- If the file being cleaned was hard linked to the old key's annex object,
|
|
|
|
-- modifying the file will have caused the object to have the wrong content.
|
|
|
|
-- Clean up from that, making the
|
|
|
|
cleanOldKey :: FilePath -> Key -> Annex ()
|
|
|
|
cleanOldKey modifiedfile key = do
|
|
|
|
obj <- calcRepo (gitAnnexLocation key)
|
|
|
|
caches <- Database.Keys.getInodeCaches key
|
|
|
|
unlessM (sameInodeCache obj caches) $ do
|
|
|
|
unlinkAnnex key
|
|
|
|
fs <- filter (/= modifiedfile)
|
|
|
|
<$> Database.Keys.getAssociatedFiles key
|
|
|
|
fs' <- filterM (`sameInodeCache` caches) fs
|
|
|
|
case fs' of
|
|
|
|
-- If linkAnnex fails, the file with the content
|
|
|
|
-- is still present, so no need for any recovery.
|
|
|
|
(f:_) -> void $ linkAnnex key f
|
|
|
|
_ -> lostcontent
|
|
|
|
where
|
|
|
|
lostcontent = logStatus key InfoMissing
|
|
|
|
|
2015-12-04 19:30:06 +00:00
|
|
|
shouldAnnex :: FilePath -> Annex Bool
|
|
|
|
shouldAnnex file = do
|
|
|
|
matcher <- largeFilesMatcher
|
|
|
|
checkFileMatcher matcher file
|
|
|
|
|
|
|
|
ingest :: FilePath -> Annex Key
|
|
|
|
ingest file = do
|
|
|
|
backend <- chooseBackend file
|
|
|
|
let source = KeySource
|
|
|
|
{ keyFilename = file
|
|
|
|
, contentLocation = file
|
|
|
|
, inodeCache = Nothing
|
|
|
|
}
|
|
|
|
k <- fst . fromMaybe (error "failed to generate a key")
|
|
|
|
<$> genKey source backend
|
2015-12-11 17:56:12 +00:00
|
|
|
-- Hard link (or copy) file content to annex object
|
2015-12-04 19:30:06 +00:00
|
|
|
-- to prevent it from being lost when git checks out
|
|
|
|
-- a branch not containing this file.
|
|
|
|
r <- linkAnnex k file
|
|
|
|
case r of
|
|
|
|
LinkAnnexFailed -> error "Problem adding file to the annex"
|
|
|
|
LinkAnnexOk -> logStatus k InfoPresent
|
|
|
|
LinkAnnexNoop -> noop
|
|
|
|
genMetaData k file
|
|
|
|
=<< liftIO (getFileStatus file)
|
|
|
|
return k
|
|
|
|
|
|
|
|
emitPointer :: Key -> IO ()
|
2015-12-10 20:06:58 +00:00
|
|
|
emitPointer = putStr . formatPointer
|