2010-11-02 23:04:24 +00:00
|
|
|
{- git-annex command
|
|
|
|
-
|
2017-04-07 19:55:34 +00:00
|
|
|
- Copyright 2010-2017 Joey Hess <id@joeyh.name>
|
2010-11-02 23:04:24 +00:00
|
|
|
-
|
|
|
|
- Licensed under the GNU GPL version 3 or higher.
|
|
|
|
-}
|
|
|
|
|
|
|
|
module Command.Add where
|
|
|
|
|
|
|
|
import Command
|
2015-12-22 17:23:33 +00:00
|
|
|
import Annex.Ingest
|
2011-10-15 20:21:08 +00:00
|
|
|
import Logs.Location
|
2011-10-04 04:40:47 +00:00
|
|
|
import Annex.Content
|
2012-12-24 17:37:29 +00:00
|
|
|
import Annex.Content.Direct
|
fully support core.symlinks=false in all relevant symlink handling code
Refactored annex link code into nice clean new library.
Audited and dealt with calls to createSymbolicLink.
Remaining calls are all safe, because:
Annex/Link.hs: ( liftIO $ createSymbolicLink linktarget file
only when core.symlinks=true
Assistant/WebApp/Configurators/Local.hs: createSymbolicLink link link
test if symlinks can be made
Command/Fix.hs: liftIO $ createSymbolicLink link file
command only works in indirect mode
Command/FromKey.hs: liftIO $ createSymbolicLink link file
command only works in indirect mode
Command/Indirect.hs: liftIO $ createSymbolicLink l f
refuses to run if core.symlinks=false
Init.hs: createSymbolicLink f f2
test if symlinks can be made
Remote/Directory.hs: go [file] = catchBoolIO $ createSymbolicLink file f >> return True
fast key linking; catches failure to make symlink and falls back to copy
Remote/Git.hs: liftIO $ catchBoolIO $ createSymbolicLink loc file >> return True
ditto
Upgrade/V1.hs: liftIO $ createSymbolicLink link f
v1 repos could not be on a filesystem w/o symlinks
Audited and dealt with calls to readSymbolicLink.
Remaining calls are all safe, because:
Annex/Link.hs: ( liftIO $ catchMaybeIO $ readSymbolicLink file
only when core.symlinks=true
Assistant/Threads/Watcher.hs: ifM ((==) (Just link) <$> liftIO (catchMaybeIO $ readSymbolicLink file))
code that fixes real symlinks when inotify sees them
It's ok to not fix psdueo-symlinks.
Assistant/Threads/Watcher.hs: mlink <- liftIO (catchMaybeIO $ readSymbolicLink file)
ditto
Command/Fix.hs: stopUnless ((/=) (Just link) <$> liftIO (catchMaybeIO $ readSymbolicLink file)) $ do
command only works in indirect mode
Upgrade/V1.hs: getsymlink = takeFileName <$> readSymbolicLink file
v1 repos could not be on a filesystem w/o symlinks
Audited and dealt with calls to isSymbolicLink.
(Typically used with getSymbolicLinkStatus, but that is just used because
getFileStatus is not as robust; it also works on pseudolinks.)
Remaining calls are all safe, because:
Assistant/Threads/SanityChecker.hs: | isSymbolicLink s -> addsymlink file ms
only handles staging of symlinks that were somehow not staged
(might need to be updated to support pseudolinks, but this is
only a belt-and-suspenders check anyway, and I've never seen the code run)
Command/Add.hs: if isSymbolicLink s || not (isRegularFile s)
avoids adding symlinks to the annex, so not relevant
Command/Indirect.hs: | isSymbolicLink s -> void $ flip whenAnnexed f $
only allowed on systems that support symlinks
Command/Indirect.hs: whenM (liftIO $ not . isSymbolicLink <$> getSymbolicLinkStatus f) $ do
ditto
Seek.hs:notSymlink f = liftIO $ not . isSymbolicLink <$> getSymbolicLinkStatus f
used to find unlocked files, only relevant in indirect mode
Utility/FSEvents.hs: | Files.isSymbolicLink s = runhook addSymlinkHook $ Just s
Utility/FSEvents.hs: | Files.isSymbolicLink s ->
Utility/INotify.hs: | Files.isSymbolicLink s ->
Utility/INotify.hs: checkfiletype Files.isSymbolicLink addSymlinkHook f
Utility/Kqueue.hs: | Files.isSymbolicLink s = callhook addSymlinkHook (Just s) change
all above are lower-level, not relevant
Audited and dealt with calls to isSymLink.
Remaining calls are all safe, because:
Annex/Direct.hs: | isSymLink (getmode item) =
This is looking at git diff-tree objects, not files on disk
Command/Unused.hs: | isSymLink (LsTree.mode l) = do
This is looking at git ls-tree, not file on disk
Utility/FileMode.hs:isSymLink :: FileMode -> Bool
Utility/FileMode.hs:isSymLink = checkMode symbolicLinkMode
low-level
Done!!
2013-02-17 19:05:55 +00:00
|
|
|
import qualified Annex
|
|
|
|
import qualified Annex.Queue
|
2016-01-20 20:36:33 +00:00
|
|
|
import qualified Database.Keys
|
2012-12-24 17:37:29 +00:00
|
|
|
import Config
|
2013-03-29 20:17:13 +00:00
|
|
|
import Annex.FileMatcher
|
2016-05-16 19:30:40 +00:00
|
|
|
import Annex.Link
|
2015-12-15 18:02:53 +00:00
|
|
|
import Annex.Version
|
2016-05-16 19:30:40 +00:00
|
|
|
import Git.FilePath
|
2010-11-02 23:04:24 +00:00
|
|
|
|
2015-07-08 16:33:27 +00:00
|
|
|
cmd :: Command
|
2016-01-19 15:54:13 +00:00
|
|
|
cmd = notBareRepo $ withGlobalOptions (jobsOption : jsonOption : fileMatchingOptions) $
|
2015-07-08 19:08:02 +00:00
|
|
|
command "add" SectionCommon "add files to annex"
|
2015-07-10 17:18:46 +00:00
|
|
|
paramPaths (seek <$$> optParser)
|
2015-02-06 21:08:14 +00:00
|
|
|
|
2015-07-10 17:18:46 +00:00
|
|
|
data AddOptions = AddOptions
|
|
|
|
{ addThese :: CmdParams
|
|
|
|
, includeDotFiles :: Bool
|
2016-01-19 21:46:46 +00:00
|
|
|
, batchOption :: BatchMode
|
2017-04-07 19:55:34 +00:00
|
|
|
, updateOnly :: Bool
|
2015-07-10 17:18:46 +00:00
|
|
|
}
|
2014-03-26 18:52:07 +00:00
|
|
|
|
2015-07-10 17:18:46 +00:00
|
|
|
optParser :: CmdParamsDesc -> Parser AddOptions
|
|
|
|
optParser desc = AddOptions
|
|
|
|
<$> cmdParams desc
|
|
|
|
<*> switch
|
|
|
|
( long "include-dotfiles"
|
|
|
|
<> help "don't skip dotfiles"
|
|
|
|
)
|
2016-01-19 21:46:46 +00:00
|
|
|
<*> parseBatchOption
|
2017-04-07 19:55:34 +00:00
|
|
|
<*> switch
|
|
|
|
( long "update"
|
|
|
|
<> short 'u'
|
|
|
|
<> help "only update tracked files"
|
|
|
|
)
|
2010-12-30 18:19:16 +00:00
|
|
|
|
2015-07-10 17:18:46 +00:00
|
|
|
seek :: AddOptions -> CommandSeek
|
2015-11-05 22:24:15 +00:00
|
|
|
seek o = allowConcurrentOutput $ do
|
fix inversion of control in CommandSeek (no behavior changes)
I've been disliking how the command seek actions were written for some
time, with their inversion of control and ugly workarounds.
The last straw to fix it was sync --content, which didn't fit the
Annex [CommandStart] interface well at all. I have not yet made it take
advantage of the changed interface though.
The crucial change, and probably why I didn't do it this way from the
beginning, is to make each CommandStart action be run with exceptions
caught, and if it fails, increment a failure counter in annex state.
So I finally remove the very first code I wrote for git-annex, which
was before I had exception handling in the Annex monad, and so ran outside
that monad, passing state explicitly as it ran each CommandStart action.
This was a real slog from 1 to 5 am.
Test suite passes.
Memory usage is lower than before, sometimes by a couple of megabytes, and
remains constant, even when running in a large repo, and even when
repeatedly failing and incrementing the error counter. So no accidental
laziness space leaks.
Wall clock speed is identical, even in large repos.
This commit was sponsored by an anonymous bitcoiner.
2014-01-20 08:11:42 +00:00
|
|
|
matcher <- largeFilesMatcher
|
2016-01-19 21:46:46 +00:00
|
|
|
let gofile file = ifM (checkFileMatcher matcher file <||> Annex.getState Annex.force)
|
fix inversion of control in CommandSeek (no behavior changes)
I've been disliking how the command seek actions were written for some
time, with their inversion of control and ugly workarounds.
The last straw to fix it was sync --content, which didn't fit the
Annex [CommandStart] interface well at all. I have not yet made it take
advantage of the changed interface though.
The crucial change, and probably why I didn't do it this way from the
beginning, is to make each CommandStart action be run with exceptions
caught, and if it fails, increment a failure counter in annex state.
So I finally remove the very first code I wrote for git-annex, which
was before I had exception handling in the Annex monad, and so ran outside
that monad, passing state explicitly as it ran each CommandStart action.
This was a real slog from 1 to 5 am.
Test suite passes.
Memory usage is lower than before, sometimes by a couple of megabytes, and
remains constant, even when running in a large repo, and even when
repeatedly failing and incrementing the error counter. So no accidental
laziness space leaks.
Wall clock speed is identical, even in large repos.
This commit was sponsored by an anonymous bitcoiner.
2014-01-20 08:11:42 +00:00
|
|
|
( start file
|
2016-01-28 18:04:32 +00:00
|
|
|
, ifM (annexAddSmallFiles <$> Annex.getGitConfig)
|
|
|
|
( startSmall file
|
|
|
|
, stop
|
|
|
|
)
|
fix inversion of control in CommandSeek (no behavior changes)
I've been disliking how the command seek actions were written for some
time, with their inversion of control and ugly workarounds.
The last straw to fix it was sync --content, which didn't fit the
Annex [CommandStart] interface well at all. I have not yet made it take
advantage of the changed interface though.
The crucial change, and probably why I didn't do it this way from the
beginning, is to make each CommandStart action be run with exceptions
caught, and if it fails, increment a failure counter in annex state.
So I finally remove the very first code I wrote for git-annex, which
was before I had exception handling in the Annex monad, and so ran outside
that monad, passing state explicitly as it ran each CommandStart action.
This was a real slog from 1 to 5 am.
Test suite passes.
Memory usage is lower than before, sometimes by a couple of megabytes, and
remains constant, even when running in a large repo, and even when
repeatedly failing and incrementing the error counter. So no accidental
laziness space leaks.
Wall clock speed is identical, even in large repos.
This commit was sponsored by an anonymous bitcoiner.
2014-01-20 08:11:42 +00:00
|
|
|
)
|
2016-01-19 21:46:46 +00:00
|
|
|
case batchOption o of
|
2017-04-07 19:55:34 +00:00
|
|
|
Batch
|
|
|
|
| updateOnly o ->
|
|
|
|
giveup "--update --batch is not supported"
|
|
|
|
| otherwise -> batchFiles gofile
|
2016-01-19 21:46:46 +00:00
|
|
|
NoBatch -> do
|
|
|
|
let go a = a gofile (addThese o)
|
2017-04-07 19:55:34 +00:00
|
|
|
unless (updateOnly o) $
|
|
|
|
go (withFilesNotInGit (not $ includeDotFiles o))
|
2016-12-05 18:02:11 +00:00
|
|
|
go withFilesMaybeModified
|
|
|
|
unlessM (versionSupportsUnlockedPointers <||> isDirect) $
|
|
|
|
go withFilesOldUnlocked
|
2010-11-11 22:54:52 +00:00
|
|
|
|
2015-04-08 20:14:23 +00:00
|
|
|
{- Pass file off to git-add. -}
|
|
|
|
startSmall :: FilePath -> CommandStart
|
|
|
|
startSmall file = do
|
|
|
|
showStart "add" file
|
2015-12-02 19:12:33 +00:00
|
|
|
next $ next $ addSmall file
|
2015-12-02 18:48:42 +00:00
|
|
|
|
2015-12-02 19:12:33 +00:00
|
|
|
addSmall :: FilePath -> Annex Bool
|
|
|
|
addSmall file = do
|
2015-04-08 20:16:42 +00:00
|
|
|
showNote "non-large file; adding content to git repository"
|
2015-12-02 19:12:33 +00:00
|
|
|
addFile file
|
2015-07-07 20:15:30 +00:00
|
|
|
|
2015-12-02 19:12:33 +00:00
|
|
|
addFile :: FilePath -> Annex Bool
|
|
|
|
addFile file = do
|
2015-07-08 19:08:02 +00:00
|
|
|
ps <- forceParams
|
|
|
|
Annex.Queue.addCommand "add" (ps++[Param "--"]) [file]
|
2015-12-02 19:12:33 +00:00
|
|
|
return True
|
2015-04-08 20:14:23 +00:00
|
|
|
|
2012-02-14 03:42:44 +00:00
|
|
|
start :: FilePath -> CommandStart
|
2016-05-16 19:30:40 +00:00
|
|
|
start file = do
|
|
|
|
ifM versionSupportsUnlockedPointers
|
|
|
|
( do
|
|
|
|
mk <- liftIO $ isPointerFile file
|
|
|
|
maybe go fixuppointer mk
|
|
|
|
, go
|
|
|
|
)
|
2012-11-12 05:05:04 +00:00
|
|
|
where
|
2016-05-16 19:30:40 +00:00
|
|
|
go = ifAnnexed file addpresent add
|
2012-11-12 05:05:04 +00:00
|
|
|
add = do
|
2013-04-23 21:22:56 +00:00
|
|
|
ms <- liftIO $ catchMaybeIO $ getSymbolicLinkStatus file
|
|
|
|
case ms of
|
|
|
|
Nothing -> stop
|
2015-07-07 20:15:30 +00:00
|
|
|
Just s
|
|
|
|
| not (isRegularFile s) && not (isSymbolicLink s) -> stop
|
2013-04-23 21:22:56 +00:00
|
|
|
| otherwise -> do
|
|
|
|
showStart "add" file
|
2015-07-07 20:15:30 +00:00
|
|
|
next $ if isSymbolicLink s
|
2015-12-02 19:12:33 +00:00
|
|
|
then next $ addFile file
|
2015-07-07 20:15:30 +00:00
|
|
|
else perform file
|
2015-12-16 18:53:41 +00:00
|
|
|
addpresent key = ifM versionSupportsUnlockedPointers
|
2014-09-18 18:24:38 +00:00
|
|
|
( do
|
|
|
|
ms <- liftIO $ catchMaybeIO $ getSymbolicLinkStatus file
|
|
|
|
case ms of
|
2016-02-16 18:43:43 +00:00
|
|
|
Just s | isSymbolicLink s -> fixuplink key
|
2015-12-16 18:53:41 +00:00
|
|
|
_ -> ifM (sameInodeCache file =<< Database.Keys.getInodeCaches key)
|
|
|
|
( stop, add )
|
|
|
|
, ifM isDirect
|
|
|
|
( do
|
|
|
|
ms <- liftIO $ catchMaybeIO $ getSymbolicLinkStatus file
|
|
|
|
case ms of
|
2016-02-16 18:43:43 +00:00
|
|
|
Just s | isSymbolicLink s -> fixuplink key
|
2015-12-16 18:53:41 +00:00
|
|
|
_ -> ifM (goodContent key file)
|
|
|
|
( stop , add )
|
2016-02-16 18:43:43 +00:00
|
|
|
, fixuplink key
|
2015-12-16 18:53:41 +00:00
|
|
|
)
|
2013-02-20 17:37:46 +00:00
|
|
|
)
|
2016-02-16 18:43:43 +00:00
|
|
|
fixuplink key = do
|
2014-09-18 18:24:38 +00:00
|
|
|
-- the annexed symlink is present but not yet added to git
|
2012-11-12 05:05:04 +00:00
|
|
|
showStart "add" file
|
|
|
|
liftIO $ removeFile file
|
2016-05-16 19:30:40 +00:00
|
|
|
addLink file key Nothing
|
|
|
|
next $ next $
|
2016-02-16 18:43:43 +00:00
|
|
|
cleanup key =<< inAnnex key
|
2016-05-16 19:30:40 +00:00
|
|
|
fixuppointer key = do
|
|
|
|
-- the pointer file is present, but not yet added to git
|
|
|
|
showStart "add" file
|
|
|
|
Database.Keys.addAssociatedFile key =<< inRepo (toTopFilePath file)
|
|
|
|
next $ next $ addFile file
|
2010-11-02 23:04:24 +00:00
|
|
|
|
2012-06-06 17:07:30 +00:00
|
|
|
perform :: FilePath -> CommandPerform
|
2015-12-22 19:23:27 +00:00
|
|
|
perform file = do
|
2016-02-16 18:43:43 +00:00
|
|
|
lockingfile <- not <$> addUnlocked
|
2016-01-07 21:39:59 +00:00
|
|
|
let cfg = LockDownConfig
|
|
|
|
{ lockingFile = lockingfile
|
|
|
|
, hardlinkFileTmp = True
|
|
|
|
}
|
2016-02-16 18:43:43 +00:00
|
|
|
lockDown cfg file >>= ingestAdd >>= finish
|
2013-09-25 20:07:11 +00:00
|
|
|
where
|
2016-02-16 18:43:43 +00:00
|
|
|
finish (Just key) = next $ cleanup key True
|
|
|
|
finish Nothing = stop
|
2012-06-06 00:28:34 +00:00
|
|
|
|
2016-02-16 18:43:43 +00:00
|
|
|
cleanup :: Key -> Bool -> CommandCleanup
|
|
|
|
cleanup key hascontent = do
|
2016-07-26 23:15:34 +00:00
|
|
|
maybeShowJSON $ JSONChunk [("key", key2file key)]
|
2014-01-05 18:09:57 +00:00
|
|
|
when hascontent $
|
|
|
|
logStatus key InfoPresent
|
2013-02-05 17:41:48 +00:00
|
|
|
return True
|