2015-12-04 17:02:56 +00:00
|
|
|
{- git-annex command
|
|
|
|
-
|
2021-01-25 21:34:58 +00:00
|
|
|
- Copyright 2015-2021 Joey Hess <id@joeyh.name>
|
2015-12-04 17:02:56 +00:00
|
|
|
-
|
2019-03-13 19:48:14 +00:00
|
|
|
- Licensed under the GNU AGPL version 3 or higher.
|
2015-12-04 17:02:56 +00:00
|
|
|
-}
|
|
|
|
|
|
|
|
module Command.Smudge where
|
|
|
|
|
|
|
|
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.FileMatcher
|
2015-12-24 17:15:26 +00:00
|
|
|
import Annex.Ingest
|
2016-06-09 19:17:08 +00:00
|
|
|
import Annex.CatFile
|
2018-10-25 18:43:13 +00:00
|
|
|
import Logs.Smudge
|
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
|
2019-12-27 18:58:10 +00:00
|
|
|
import Git.Types
|
|
|
|
import Git.HashObject
|
2018-11-15 17:04:40 +00:00
|
|
|
import qualified Git
|
2019-12-27 18:58:10 +00:00
|
|
|
import qualified Git.Ref
|
2019-10-23 19:20:00 +00:00
|
|
|
import qualified Annex
|
2016-06-09 19:17:08 +00:00
|
|
|
import Backend
|
2019-06-25 15:37:52 +00:00
|
|
|
import Utility.Metered
|
2019-10-23 18:37:51 +00:00
|
|
|
import Annex.InodeSentinal
|
|
|
|
import Utility.InodeCache
|
2019-12-26 20:24:40 +00:00
|
|
|
import Config.GitConfig
|
2021-01-25 21:34:58 +00:00
|
|
|
import qualified Types.Backend
|
2015-12-04 18:03:10 +00:00
|
|
|
|
2019-01-14 19:19:20 +00:00
|
|
|
import qualified Data.ByteString as S
|
|
|
|
import qualified Data.ByteString.Lazy as L
|
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
|
|
|
|
2018-10-25 18:43:13 +00:00
|
|
|
data SmudgeOptions = UpdateOption | SmudgeOptions
|
2015-12-04 19:30:06 +00:00
|
|
|
{ smudgeFile :: FilePath
|
|
|
|
, cleanOption :: Bool
|
|
|
|
}
|
2015-12-04 17:02:56 +00:00
|
|
|
|
2015-12-04 19:30:06 +00:00
|
|
|
optParser :: CmdParamsDesc -> Parser SmudgeOptions
|
2018-10-25 18:43:13 +00:00
|
|
|
optParser desc = smudgeoptions <|> updateoption
|
|
|
|
where
|
|
|
|
smudgeoptions = SmudgeOptions
|
|
|
|
<$> argument str ( metavar desc )
|
|
|
|
<*> switch ( long "clean" <> help "clean filter" )
|
|
|
|
updateoption = flag' UpdateOption
|
|
|
|
( long "update" <> help "populate annexed worktree files" )
|
2015-12-04 19:30:06 +00:00
|
|
|
|
|
|
|
seek :: SmudgeOptions -> CommandSeek
|
2018-10-25 18:43:13 +00:00
|
|
|
seek (SmudgeOptions f False) = commandAction (smudge f)
|
2020-11-02 20:31:28 +00:00
|
|
|
seek (SmudgeOptions f True) = commandAction (clean (toRawFilePath f))
|
2018-10-25 18:43:13 +00:00
|
|
|
seek UpdateOption = commandAction update
|
2015-12-04 19:30:06 +00:00
|
|
|
|
2015-12-04 21:18:26 +00:00
|
|
|
-- Smudge filter is fed git file content, and if it's a pointer to an
|
2018-10-25 18:43:13 +00:00
|
|
|
-- available annex object, git expects it to output its content.
|
|
|
|
--
|
|
|
|
-- However, this does not do that. It outputs the pointer, and records
|
|
|
|
-- the filename in the smudge log. Git hooks run after commands like checkout
|
|
|
|
-- then run git annex smudge --update which populates the work tree files
|
|
|
|
-- with annex content. This is done for several reasons:
|
|
|
|
--
|
|
|
|
-- * To support annex.thin
|
|
|
|
-- * Because git currently buffers the whole object received from the
|
|
|
|
-- smudge filter in memory, which is a problem with large files.
|
2015-12-04 19:30:06 +00:00
|
|
|
smudge :: FilePath -> CommandStart
|
2015-12-07 18:35:46 +00:00
|
|
|
smudge file = do
|
2019-01-14 19:19:20 +00:00
|
|
|
b <- liftIO $ L.hGetContents stdin
|
|
|
|
case parseLinkTargetOrPointerLazy b of
|
2018-10-25 18:43:13 +00:00
|
|
|
Nothing -> noop
|
2015-12-04 18:03:10 +00:00
|
|
|
Just k -> do
|
2019-12-09 17:49:05 +00:00
|
|
|
topfile <- inRepo (toTopFilePath (toRawFilePath file))
|
2018-10-25 18:43:13 +00:00
|
|
|
Database.Keys.addAssociatedFile k topfile
|
2018-10-25 19:40:12 +00:00
|
|
|
void $ smudgeLog k topfile
|
2019-01-14 19:19:20 +00:00
|
|
|
liftIO $ L.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.
|
2020-11-02 20:31:28 +00:00
|
|
|
clean :: RawFilePath -> CommandStart
|
2015-12-04 19:30:06 +00:00
|
|
|
clean file = do
|
2019-01-14 19:19:20 +00:00
|
|
|
b <- liftIO $ L.hGetContents stdin
|
2018-11-15 17:04:40 +00:00
|
|
|
ifM fileoutsiderepo
|
2019-01-14 19:19:20 +00:00
|
|
|
( liftIO $ L.hPut stdout b
|
2020-06-18 16:56:29 +00:00
|
|
|
, do
|
|
|
|
-- Avoid a potential deadlock.
|
|
|
|
Annex.changeState $ \s -> s
|
|
|
|
{ Annex.insmudgecleanfilter = True }
|
|
|
|
go b
|
2018-11-15 17:04:40 +00:00
|
|
|
)
|
2015-12-04 19:30:06 +00:00
|
|
|
stop
|
2016-01-01 18:16:40 +00:00
|
|
|
where
|
2020-06-18 16:56:29 +00:00
|
|
|
go b = case parseLinkTargetOrPointerLazy b of
|
|
|
|
Just k -> do
|
2020-11-10 16:10:51 +00:00
|
|
|
addingExistingLink file k $ do
|
|
|
|
getMoveRaceRecovery k file
|
|
|
|
liftIO $ L.hPut stdout b
|
2020-06-18 16:56:29 +00:00
|
|
|
Nothing -> do
|
2020-11-02 20:31:28 +00:00
|
|
|
let fileref = Git.Ref.fileRef file
|
2020-06-18 16:56:29 +00:00
|
|
|
indexmeta <- catObjectMetaData fileref
|
|
|
|
go' b indexmeta =<< catKey' fileref indexmeta
|
|
|
|
go' b indexmeta oldkey = ifM (shouldAnnex file indexmeta oldkey)
|
2018-08-27 18:47:17 +00:00
|
|
|
( do
|
|
|
|
-- 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"
|
2019-01-14 19:19:20 +00:00
|
|
|
then L.length b `seq` return ()
|
2018-08-27 18:47:17 +00:00
|
|
|
else liftIO $ hClose stdin
|
2018-10-25 20:38:04 +00:00
|
|
|
|
2018-10-30 04:40:17 +00:00
|
|
|
-- Optimization for the case when the file is already
|
|
|
|
-- annexed and is unmodified.
|
2018-10-25 20:38:04 +00:00
|
|
|
case oldkey of
|
2021-01-25 21:34:58 +00:00
|
|
|
Nothing -> doingest Nothing
|
2020-11-02 20:31:28 +00:00
|
|
|
Just ko -> ifM (isUnmodifiedCheap ko file)
|
2018-10-25 20:38:04 +00:00
|
|
|
( liftIO $ emitPointer ko
|
2021-01-25 21:34:58 +00:00
|
|
|
, updateingest ko
|
2018-10-25 20:38:04 +00:00
|
|
|
)
|
2019-01-14 19:19:20 +00:00
|
|
|
, liftIO $ L.hPut stdout b
|
2018-08-27 18:47:17 +00:00
|
|
|
)
|
2018-10-25 20:38:04 +00:00
|
|
|
|
2021-01-25 21:34:58 +00:00
|
|
|
-- Use the same backend that was used before, when possible.
|
|
|
|
-- If the old key's backend does not support generating keys,
|
|
|
|
-- use the default backend.
|
|
|
|
updateingest oldkey =
|
|
|
|
maybeLookupBackendVariety (fromKey keyVariety oldkey) >>= \case
|
|
|
|
Nothing -> doingest Nothing
|
|
|
|
Just oldbackend -> case Types.Backend.genKey oldbackend of
|
|
|
|
Just _ -> doingest (Just oldbackend)
|
|
|
|
Nothing -> doingest Nothing
|
|
|
|
|
|
|
|
doingest preferredbackend = do
|
2018-10-25 20:38:04 +00:00
|
|
|
-- Can't restage associated files because git add
|
|
|
|
-- runs this and has the index locked.
|
|
|
|
let norestage = Restage False
|
|
|
|
liftIO . emitPointer
|
|
|
|
=<< postingest
|
2021-01-25 21:34:58 +00:00
|
|
|
=<< (\ld -> ingest' preferredbackend nullMeterUpdate ld Nothing norestage)
|
2020-11-02 20:31:28 +00:00
|
|
|
=<< lockDown cfg (fromRawFilePath file)
|
2018-08-27 18:47:17 +00:00
|
|
|
|
|
|
|
postingest (Just k, _) = do
|
2016-01-01 18:16:40 +00:00
|
|
|
logStatus k InfoPresent
|
|
|
|
return k
|
2018-08-27 18:47:17 +00:00
|
|
|
postingest _ = error "could not add file to the annex"
|
|
|
|
|
2016-01-07 21:39:59 +00:00
|
|
|
cfg = LockDownConfig
|
|
|
|
{ lockingFile = False
|
2019-05-07 17:04:39 +00:00
|
|
|
, hardlinkFileTmpDir = Nothing
|
2016-01-07 21:39:59 +00:00
|
|
|
}
|
2015-12-04 19:30:06 +00:00
|
|
|
|
2018-11-15 17:04:40 +00:00
|
|
|
-- git diff can run the clean filter on files outside the
|
|
|
|
-- repository; can't annex those
|
|
|
|
fileoutsiderepo = do
|
2020-11-02 20:31:28 +00:00
|
|
|
repopath <- liftIO . absPath =<< fromRepo Git.repoPath
|
2018-11-15 17:04:40 +00:00
|
|
|
filepath <- liftIO $ absPath file
|
|
|
|
return $ not $ dirContains repopath filepath
|
|
|
|
|
2019-10-24 18:10:48 +00:00
|
|
|
-- If annex.largefiles is configured, matching files are added to the
|
|
|
|
-- annex. But annex.gitaddtoannex can be set to false to disable that.
|
2019-10-23 18:37:51 +00:00
|
|
|
--
|
2019-10-24 18:10:48 +00:00
|
|
|
-- When annex.largefiles is not configured, files are normally not
|
|
|
|
-- added to the annex, so will be added to git. But some heuristics
|
|
|
|
-- are used to avoid bad behavior:
|
2019-10-23 18:37:51 +00:00
|
|
|
--
|
2019-12-27 18:58:10 +00:00
|
|
|
-- If the file is annexed in the index, keep it annexed.
|
|
|
|
-- This prevents accidental conversions.
|
2019-10-24 18:10:48 +00:00
|
|
|
--
|
|
|
|
-- Otherwise, when the file's inode is the same as one that was used for
|
|
|
|
-- annexed content before, annex it. This handles cases such as renaming an
|
2019-10-23 18:37:51 +00:00
|
|
|
-- unlocked annexed file followed by git add, which the user naturally
|
|
|
|
-- expects to behave the same as git mv.
|
2020-11-02 20:31:28 +00:00
|
|
|
shouldAnnex :: RawFilePath -> Maybe (Sha, FileSize, ObjectType) -> Maybe Key -> Annex Bool
|
2019-12-27 18:58:10 +00:00
|
|
|
shouldAnnex file indexmeta moldkey = ifM (annexGitAddToAnnex <$> Annex.getGitConfig)
|
|
|
|
( checkunchangedgitfile $ checkmatcher checkheuristics
|
|
|
|
, checkunchangedgitfile checkheuristics
|
2019-10-23 19:20:00 +00:00
|
|
|
)
|
2018-08-27 18:47:17 +00:00
|
|
|
where
|
2019-12-26 20:24:40 +00:00
|
|
|
checkmatcher d
|
|
|
|
| dotfile file = ifM (getGitConfigVal annexDotFiles)
|
|
|
|
( go
|
2020-03-09 18:20:02 +00:00
|
|
|
, d
|
2019-12-26 20:24:40 +00:00
|
|
|
)
|
|
|
|
| otherwise = go
|
|
|
|
where
|
|
|
|
go = do
|
|
|
|
matcher <- largeFilesMatcher
|
|
|
|
checkFileMatcher' matcher file d
|
2019-10-23 19:20:00 +00:00
|
|
|
|
2019-10-24 18:10:48 +00:00
|
|
|
checkheuristics = case moldkey of
|
2018-08-27 18:47:17 +00:00
|
|
|
Just _ -> return True
|
2019-10-24 18:10:48 +00:00
|
|
|
Nothing -> checkknowninode
|
|
|
|
|
2020-11-02 20:31:28 +00:00
|
|
|
checkknowninode = withTSDelta (liftIO . genInodeCache file) >>= \case
|
2019-10-24 18:10:48 +00:00
|
|
|
Nothing -> pure False
|
|
|
|
Just ic -> Database.Keys.isInodeKnown ic =<< sentinalStatus
|
2015-12-04 19:30:06 +00:00
|
|
|
|
2019-12-27 18:58:10 +00:00
|
|
|
-- This checks for a case where the file had been added to git
|
|
|
|
-- previously, not to the annex before, and its content is not
|
|
|
|
-- changed, but git is running the clean filter again on it
|
|
|
|
-- (eg because its mtime or inode changed, or just because git feels
|
|
|
|
-- like it). Such a file should not be added to the annex, even if
|
|
|
|
-- annex.largefiles now matches it, because the content is not
|
|
|
|
-- changed.
|
|
|
|
checkunchangedgitfile cont = case (moldkey, indexmeta) of
|
2020-11-05 15:26:34 +00:00
|
|
|
(Nothing, Just (sha, sz, _)) -> liftIO (catchMaybeIO (getFileSize file)) >>= \case
|
2019-12-27 18:58:10 +00:00
|
|
|
Just sz' | sz' == sz -> do
|
|
|
|
-- The size is the same, so the file
|
|
|
|
-- is not much larger than what was stored
|
|
|
|
-- in git before, so it won't be out of
|
|
|
|
-- line to hash it. However, the content
|
|
|
|
-- is prevented from being stored in git
|
|
|
|
-- when hashing.
|
|
|
|
h <- inRepo $ hashObjectStart False
|
|
|
|
sha' <- liftIO $ hashFile h file
|
|
|
|
liftIO $ hashObjectStop h
|
|
|
|
if sha' == sha
|
|
|
|
then return False
|
|
|
|
else cont
|
|
|
|
_ -> cont
|
|
|
|
_ -> cont
|
|
|
|
|
2015-12-04 19:30:06 +00:00
|
|
|
emitPointer :: Key -> IO ()
|
2019-01-14 19:19:20 +00:00
|
|
|
emitPointer = S.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.
|
2019-12-05 15:40:10 +00:00
|
|
|
getMoveRaceRecovery :: Key -> RawFilePath -> Annex ()
|
2018-08-22 20:01:50 +00:00
|
|
|
getMoveRaceRecovery k file = void $ tryNonAsync $
|
2018-10-25 18:31:45 +00:00
|
|
|
whenM (inAnnex k) $ do
|
2019-12-11 18:12:22 +00:00
|
|
|
obj <- calcRepo (gitAnnexLocation k)
|
2018-10-25 18:31:45 +00:00
|
|
|
-- 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]
|
2018-10-25 18:43:13 +00:00
|
|
|
|
|
|
|
update :: CommandStart
|
|
|
|
update = do
|
|
|
|
updateSmudged (Restage True)
|
|
|
|
stop
|
|
|
|
|
|
|
|
updateSmudged :: Restage -> Annex ()
|
|
|
|
updateSmudged restage = streamSmudged $ \k topf -> do
|
2019-12-09 17:49:05 +00:00
|
|
|
f <- fromRepo (fromTopFilePath topf)
|
2018-10-25 18:43:13 +00:00
|
|
|
whenM (inAnnex k) $ do
|
2019-12-11 18:12:22 +00:00
|
|
|
obj <- calcRepo (gitAnnexLocation k)
|
2021-01-26 01:19:07 +00:00
|
|
|
objic <- withTSDelta (liftIO . genInodeCache obj)
|
2021-01-25 21:25:42 +00:00
|
|
|
populatePointerFile restage k obj f >>= \case
|
2021-01-26 01:19:07 +00:00
|
|
|
Just ic -> do
|
|
|
|
cs <- Database.Keys.getInodeCaches k
|
|
|
|
if null cs
|
|
|
|
then Database.Keys.addInodeCaches k (catMaybes [Just ic, objic])
|
|
|
|
else Database.Keys.addInodeCaches k [ic]
|
2021-01-25 21:25:42 +00:00
|
|
|
Nothing -> liftIO (isPointerFile f) >>= \case
|
2018-10-25 18:43:13 +00:00
|
|
|
Just k' | k' == k -> toplevelWarning False $
|
2019-12-05 15:40:10 +00:00
|
|
|
"unable to populate worktree file " ++ fromRawFilePath f
|
2018-10-25 18:43:13 +00:00
|
|
|
_ -> noop
|