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 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
|
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
|
|
|
|
2015-07-08 16:33:27 +00:00
|
|
|
cmd :: Command
|
2018-02-19 18:28:17 +00:00
|
|
|
cmd = withGlobalOptions [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
|
2020-07-13 21:04:02 +00:00
|
|
|
seek ps = withFilesInGitAnnex ww seeker =<< workTreeItems ww ps
|
2020-05-28 19:55:17 +00:00
|
|
|
where
|
|
|
|
ww = WarnUnmatchLsFiles
|
2010-11-11 22:54:52 +00:00
|
|
|
|
2020-07-13 21:04:02 +00:00
|
|
|
seeker :: AnnexedFileSeeker
|
|
|
|
seeker = AnnexedFileSeeker
|
2020-07-22 18:23:28 +00:00
|
|
|
{ startAction = start
|
2020-07-15 15:15:28 +00:00
|
|
|
, checkContentPresent = Just True
|
2020-07-13 21:04:02 +00:00
|
|
|
, usesLocationLog = False
|
|
|
|
}
|
|
|
|
|
2020-09-14 20:49:33 +00:00
|
|
|
start :: SeekInput -> RawFilePath -> Key -> CommandStart
|
|
|
|
start si file key =
|
|
|
|
starting "unannex" (mkActionItem (key, file)) si $
|
2019-12-09 17:49:05 +00:00
|
|
|
perform file key
|
2010-11-02 23:04:24 +00:00
|
|
|
|
2019-12-09 17:49:05 +00:00
|
|
|
perform :: RawFilePath -> Key -> CommandPerform
|
2019-08-26 19:52:19 +00:00
|
|
|
perform file key = do
|
2019-12-09 17:49:05 +00:00
|
|
|
liftIO $ removeFile (fromRawFilePath file)
|
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]
|
2019-08-26 19:52:19 +00:00
|
|
|
next $ cleanup file key
|
2013-07-22 21:06:00 +00:00
|
|
|
|
2019-12-09 17:49:05 +00:00
|
|
|
cleanup :: RawFilePath -> Key -> CommandCleanup
|
2019-08-26 19:52:19 +00:00
|
|
|
cleanup file key = do
|
2016-01-05 21:22:19 +00:00
|
|
|
Database.Keys.removeAssociatedFile key =<< inRepo (toTopFilePath file)
|
2020-11-06 18:10:58 +00:00
|
|
|
src <- calcRepo (gitAnnexLocation key)
|
2012-03-14 21:43:34 +00:00
|
|
|
ifM (Annex.getState Annex.fast)
|
2014-04-15 18:23:08 +00:00
|
|
|
( do
|
|
|
|
-- Only make a hard link if the annexed file does not
|
|
|
|
-- 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
|
|
|
)
|