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 Command
|
2016-04-13 17:34:24 +00:00
|
|
|
import qualified Annex
|
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.FileMatcher
|
2015-12-24 17:15:26 +00:00
|
|
|
import Annex.Ingest
|
2016-06-09 19:17:08 +00:00
|
|
|
import Annex.CatFile
|
2015-12-04 19:30:06 +00:00
|
|
|
import Logs.Location
|
2015-12-09 21:00:37 +00:00
|
|
|
import qualified Database.Keys
|
2018-08-09 22:17:46 +00:00
|
|
|
import qualified Git.BuildVersion
|
2016-01-05 21:22:19 +00:00
|
|
|
import Git.FilePath
|
2016-06-09 19:17:08 +00:00
|
|
|
import Backend
|
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
|
2016-05-16 18:55:05 +00:00
|
|
|
Database.Keys.addAssociatedFile k =<< inRepo (toTopFilePath file)
|
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.
|
2015-12-29 20:36:21 +00:00
|
|
|
ifM (inAnnex k)
|
2015-12-11 21:53:37 +00:00
|
|
|
( do
|
|
|
|
content <- calcRepo (gitAnnexLocation k)
|
2016-04-13 17:34:24 +00:00
|
|
|
whenM (annexThin <$> Annex.getGitConfig) $
|
|
|
|
warning $ "Not able to honor annex.thin when git is checking out " ++ file ++ " (run git annex fix to re-thin files)"
|
2015-12-11 21:53:37 +00:00
|
|
|
liftIO $ B.putStr . fromMaybe b
|
|
|
|
=<< catchMaybeIO (B.readFile content)
|
|
|
|
, liftIO $ B.putStr b
|
|
|
|
)
|
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
|
2018-08-09 22:17:46 +00:00
|
|
|
-- injested content if so. Otherwise, the original 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
|
2018-08-22 20:01:50 +00:00
|
|
|
case parseLinkOrPointer b of
|
|
|
|
Just k -> do
|
|
|
|
getMoveRaceRecovery k file
|
|
|
|
liftIO $ B.hPut stdout b
|
|
|
|
Nothing -> ifM (shouldAnnex file)
|
2016-05-02 14:53:24 +00:00
|
|
|
( do
|
2018-08-09 22:17:46 +00:00
|
|
|
-- Before git 2.5, failing to consume all
|
|
|
|
-- stdin here would cause a SIGPIPE and
|
|
|
|
-- crash it.
|
|
|
|
-- Newer git catches the signal and
|
|
|
|
-- stops sending, which is much faster.
|
|
|
|
-- (Also, git seems to forget to free memory
|
|
|
|
-- when sending the file, so the less we
|
|
|
|
-- let it send, the less memory it will
|
|
|
|
-- waste.)
|
|
|
|
if Git.BuildVersion.older "2.5"
|
|
|
|
then B.length b `seq` return ()
|
|
|
|
else liftIO $ hClose stdin
|
2016-06-09 19:17:08 +00:00
|
|
|
-- Look up the backend that was used
|
|
|
|
-- for this file before, so that when
|
|
|
|
-- git re-cleans a file its backend does
|
|
|
|
-- not change.
|
2017-02-24 19:16:56 +00:00
|
|
|
currbackend <- maybe Nothing (maybeLookupBackendVariety . keyVariety)
|
2016-06-09 19:17:08 +00:00
|
|
|
<$> catKeyFile file
|
2016-05-02 14:53:24 +00:00
|
|
|
liftIO . emitPointer
|
2016-06-09 19:17:08 +00:00
|
|
|
=<< go
|
2018-08-14 18:22:23 +00:00
|
|
|
=<< (\ld -> ingest' currbackend ld Nothing norestage)
|
2016-06-09 19:17:08 +00:00
|
|
|
=<< lockDown cfg file
|
2015-12-09 19:24:32 +00:00
|
|
|
, liftIO $ B.hPut stdout b
|
|
|
|
)
|
2015-12-04 19:30:06 +00:00
|
|
|
stop
|
2016-01-01 18:16:40 +00:00
|
|
|
where
|
|
|
|
go (Just k, _) = do
|
|
|
|
logStatus k InfoPresent
|
|
|
|
return k
|
|
|
|
go _ = error "could not add file to the annex"
|
2016-01-07 21:39:59 +00:00
|
|
|
cfg = LockDownConfig
|
|
|
|
{ lockingFile = False
|
|
|
|
, hardlinkFileTmp = False
|
|
|
|
}
|
2018-08-14 18:22:23 +00:00
|
|
|
-- Can't restage associated files because git add runs this and has
|
|
|
|
-- the index locked.
|
|
|
|
norestage = Restage False
|
2015-12-04 19:30:06 +00:00
|
|
|
|
|
|
|
shouldAnnex :: FilePath -> Annex Bool
|
|
|
|
shouldAnnex file = do
|
|
|
|
matcher <- largeFilesMatcher
|
|
|
|
checkFileMatcher matcher file
|
|
|
|
|
|
|
|
emitPointer :: Key -> IO ()
|
2015-12-10 20:06:58 +00:00
|
|
|
emitPointer = putStr . formatPointer
|
2018-08-22 20:01:50 +00:00
|
|
|
|
|
|
|
-- Recover from a previous race between eg git mv and git-annex get.
|
|
|
|
-- That could result in the file remaining a pointer file, while
|
|
|
|
-- its content is present in the annex. Populate the pointer file.
|
|
|
|
--
|
|
|
|
-- This also handles the case where a copy of a pointer file is made,
|
|
|
|
-- then git-annex gets the content, and later git add is run on
|
|
|
|
-- the pointer copy. It will then be populated with the content.
|
|
|
|
getMoveRaceRecovery :: Key -> FilePath -> Annex ()
|
|
|
|
getMoveRaceRecovery k file = void $ tryNonAsync $
|
|
|
|
liftIO (isPointerFile file) >>= \k' -> when (Just k == k') $
|
|
|
|
whenM (inAnnex k) $ do
|
|
|
|
obj <- calcRepo (gitAnnexLocation k)
|
|
|
|
-- Cannot restage because git add is running and has
|
|
|
|
-- the index locked.
|
|
|
|
populatePointerFile (Restage False) k obj file >>= \case
|
|
|
|
Nothing -> return ()
|
|
|
|
Just ic -> Database.Keys.addInodeCaches k [ic]
|