2010-11-02 23:04:24 +00:00
|
|
|
{- git-annex command
|
|
|
|
-
|
2021-02-22 16:56:11 +00:00
|
|
|
- Copyright 2010-2021 Joey Hess <id@joeyh.name>
|
2010-11-02 23:04:24 +00:00
|
|
|
-
|
2019-03-13 19:48:14 +00:00
|
|
|
- Licensed under the GNU AGPL version 3 or higher.
|
2010-11-02 23:04:24 +00:00
|
|
|
-}
|
|
|
|
|
|
|
|
module Command.Unannex where
|
|
|
|
|
|
|
|
import Command
|
|
|
|
import qualified Annex
|
2016-03-09 17:43:22 +00:00
|
|
|
import Annex.Perms
|
2021-02-22 17:35:00 +00:00
|
|
|
import Annex.Link
|
2021-02-22 16:56:11 +00:00
|
|
|
import qualified Annex.Queue
|
2013-10-28 20:56:01 +00:00
|
|
|
import Utility.CopyFile
|
2015-12-21 22:00:13 +00:00
|
|
|
import qualified Database.Keys
|
2021-05-12 15:09:38 +00:00
|
|
|
import Utility.InodeCache
|
|
|
|
import Annex.InodeSentinal
|
2016-01-05 21:22:19 +00:00
|
|
|
import Git.FilePath
|
2020-11-06 18:10:58 +00:00
|
|
|
import qualified Utility.RawFilePath as R
|
2010-11-02 23:04:24 +00:00
|
|
|
|
2023-03-01 19:55:58 +00:00
|
|
|
import System.PosixCompat.Files (linkCount)
|
|
|
|
|
2015-07-08 16:33:27 +00:00
|
|
|
cmd :: Command
|
2023-05-03 19:56:20 +00:00
|
|
|
cmd = withAnnexOptions [jsonOptions, annexedMatchingOptions] $
|
2015-07-08 19:08:02 +00:00
|
|
|
command "unannex" SectionUtility
|
2017-02-11 09:38:49 +00:00
|
|
|
"undo accidental add command"
|
2015-07-08 19:08:02 +00:00
|
|
|
paramPaths (withParams seek)
|
2010-12-30 19:06:26 +00:00
|
|
|
|
2015-07-08 16:33:27 +00:00
|
|
|
seek :: CmdParams -> CommandSeek
|
2022-06-28 19:28:14 +00:00
|
|
|
seek ps = withFilesInGitAnnex ww (seeker False) =<< workTreeItems ww ps
|
2020-05-28 19:55:17 +00:00
|
|
|
where
|
2023-04-25 23:26:20 +00:00
|
|
|
ww = WarnUnmatchLsFiles "unannex"
|
2010-11-11 22:54:52 +00:00
|
|
|
|
2022-06-28 19:28:14 +00:00
|
|
|
seeker :: Bool -> AnnexedFileSeeker
|
|
|
|
seeker fast = AnnexedFileSeeker
|
|
|
|
{ startAction = start fast
|
2020-07-15 15:15:28 +00:00
|
|
|
, checkContentPresent = Just True
|
2020-07-13 21:04:02 +00:00
|
|
|
, usesLocationLog = False
|
|
|
|
}
|
|
|
|
|
2022-06-28 19:28:14 +00:00
|
|
|
start :: Bool -> SeekInput -> RawFilePath -> Key -> CommandStart
|
|
|
|
start fast si file key =
|
2020-09-14 20:49:33 +00:00
|
|
|
starting "unannex" (mkActionItem (key, file)) si $
|
2022-06-28 19:28:14 +00:00
|
|
|
perform fast file key
|
2010-11-02 23:04:24 +00:00
|
|
|
|
2022-06-28 19:28:14 +00:00
|
|
|
perform :: Bool -> RawFilePath -> Key -> CommandPerform
|
|
|
|
perform fast file key = do
|
2021-02-22 16:56:11 +00:00
|
|
|
Annex.Queue.addCommand [] "rm"
|
|
|
|
[ Param "--cached"
|
2015-06-01 17:52:23 +00:00
|
|
|
, Param "--force"
|
|
|
|
, Param "--quiet"
|
|
|
|
, Param "--"
|
|
|
|
]
|
2021-02-22 16:56:11 +00:00
|
|
|
[fromRawFilePath file]
|
2021-02-22 17:35:00 +00:00
|
|
|
isAnnexLink file >>= \case
|
|
|
|
-- If the file is locked, it needs to be replaced with
|
|
|
|
-- the content from the annex. Note that it's possible
|
|
|
|
-- for key' (read from the symlink) to differ from key
|
|
|
|
-- (cached in git).
|
|
|
|
Just key' -> do
|
2021-05-12 15:09:38 +00:00
|
|
|
cleanupdb
|
2022-06-28 19:28:14 +00:00
|
|
|
next $ cleanup fast file key'
|
2021-02-22 17:35:00 +00:00
|
|
|
-- If the file is unlocked, it can be unmodified or not and
|
|
|
|
-- does not need to be replaced either way.
|
|
|
|
Nothing -> do
|
2021-05-12 15:09:38 +00:00
|
|
|
cleanupdb
|
2021-02-22 17:35:00 +00:00
|
|
|
next $ return True
|
|
|
|
where
|
2021-05-12 15:09:38 +00:00
|
|
|
cleanupdb = do
|
2021-02-22 17:35:00 +00:00
|
|
|
Database.Keys.removeAssociatedFile key
|
|
|
|
=<< inRepo (toTopFilePath file)
|
2021-05-12 15:09:38 +00:00
|
|
|
maybe noop Database.Keys.removeInodeCache
|
|
|
|
=<< withTSDelta (liftIO . genInodeCache file)
|
2013-07-22 21:06:00 +00:00
|
|
|
|
2022-06-28 19:28:14 +00:00
|
|
|
cleanup :: Bool -> RawFilePath -> Key -> CommandCleanup
|
|
|
|
cleanup fast file key = do
|
2021-02-22 17:35:00 +00:00
|
|
|
liftIO $ removeFile (fromRawFilePath file)
|
2020-11-06 18:10:58 +00:00
|
|
|
src <- calcRepo (gitAnnexLocation key)
|
2022-06-28 19:28:14 +00:00
|
|
|
ifM (pure fast <||> Annex.getRead Annex.fast)
|
2014-04-15 18:23:08 +00:00
|
|
|
( do
|
|
|
|
-- Only make a hard link if the annexed file does not
|
2021-02-22 17:35:00 +00:00
|
|
|
-- already have other hard links pointing at it. This
|
|
|
|
-- avoids unannexing (and uninit) ending up hard
|
|
|
|
-- linking files together, which would be surprising.
|
2020-11-06 18:10:58 +00:00
|
|
|
s <- liftIO $ R.getFileStatus src
|
2014-04-15 18:23:08 +00:00
|
|
|
if linkCount s > 1
|
|
|
|
then copyfrom src
|
|
|
|
else hardlinkfrom src
|
2013-10-28 20:56:01 +00:00
|
|
|
, copyfrom src
|
2012-03-14 21:43:34 +00:00
|
|
|
)
|
2013-02-06 18:02:18 +00:00
|
|
|
where
|
2013-10-28 20:56:01 +00:00
|
|
|
copyfrom src =
|
2020-11-06 18:10:58 +00:00
|
|
|
thawContent file `after` liftIO
|
|
|
|
(copyFileExternal CopyAllMetaData
|
|
|
|
(fromRawFilePath src)
|
|
|
|
(fromRawFilePath file))
|
2013-10-28 20:56:01 +00:00
|
|
|
hardlinkfrom src =
|
|
|
|
-- creating a hard link could fall; fall back to copying
|
2020-11-06 18:10:58 +00:00
|
|
|
ifM (liftIO $ catchBoolIO $ R.createLink src file >> return True)
|
2013-10-28 20:56:01 +00:00
|
|
|
( return True
|
|
|
|
, copyfrom src
|
2013-02-06 18:02:18 +00:00
|
|
|
)
|