2010-11-02 19:04:24 -04:00
|
|
|
{- git-annex command
|
|
|
|
-
|
2015-01-21 12:50:09 -04:00
|
|
|
- Copyright 2010-2013 Joey Hess <id@joeyh.name>
|
2010-11-02 19:04:24 -04:00
|
|
|
-
|
2019-03-13 15:48:14 -04:00
|
|
|
- Licensed under the GNU AGPL version 3 or higher.
|
2010-11-02 19:04:24 -04:00
|
|
|
-}
|
|
|
|
|
|
|
|
module Command.Unannex where
|
|
|
|
|
|
|
|
import Command
|
|
|
|
import qualified Annex
|
2016-03-09 13:43:22 -04:00
|
|
|
import Annex.Perms
|
2011-12-14 15:56:11 -04:00
|
|
|
import qualified Git.Command
|
2013-10-28 16:56:01 -04:00
|
|
|
import Utility.CopyFile
|
2015-12-21 18:00:13 -04:00
|
|
|
import qualified Database.Keys
|
2016-01-05 17:22:19 -04:00
|
|
|
import Git.FilePath
|
2020-11-06 14:10:58 -04:00
|
|
|
import qualified Utility.RawFilePath as R
|
2010-11-02 19:04:24 -04:00
|
|
|
|
2015-07-08 12:33:27 -04:00
|
|
|
cmd :: Command
|
2018-02-19 14:28:17 -04:00
|
|
|
cmd = withGlobalOptions [annexedMatchingOptions] $
|
2015-07-08 15:08:02 -04:00
|
|
|
command "unannex" SectionUtility
|
2017-02-11 09:38:49 +00:00
|
|
|
"undo accidental add command"
|
2015-07-08 15:08:02 -04:00
|
|
|
paramPaths (withParams seek)
|
2010-12-30 15:06:26 -04:00
|
|
|
|
2015-07-08 12:33:27 -04:00
|
|
|
seek :: CmdParams -> CommandSeek
|
2020-07-13 17:04:02 -04:00
|
|
|
seek ps = withFilesInGitAnnex ww seeker =<< workTreeItems ww ps
|
2020-05-28 15:55:17 -04:00
|
|
|
where
|
|
|
|
ww = WarnUnmatchLsFiles
|
2010-11-11 18:54:52 -04:00
|
|
|
|
2020-07-13 17:04:02 -04:00
|
|
|
seeker :: AnnexedFileSeeker
|
|
|
|
seeker = AnnexedFileSeeker
|
2020-07-22 14:23:28 -04:00
|
|
|
{ startAction = start
|
2020-07-15 11:15:28 -04:00
|
|
|
, checkContentPresent = Just True
|
2020-07-13 17:04:02 -04:00
|
|
|
, usesLocationLog = False
|
|
|
|
}
|
|
|
|
|
2020-09-14 16:49:33 -04:00
|
|
|
start :: SeekInput -> RawFilePath -> Key -> CommandStart
|
|
|
|
start si file key =
|
|
|
|
starting "unannex" (mkActionItem (key, file)) si $
|
2019-12-09 13:49:05 -04:00
|
|
|
perform file key
|
2010-11-02 19:04:24 -04:00
|
|
|
|
2019-12-09 13:49:05 -04:00
|
|
|
perform :: RawFilePath -> Key -> CommandPerform
|
2019-08-26 15:52:19 -04:00
|
|
|
perform file key = do
|
2019-12-09 13:49:05 -04:00
|
|
|
liftIO $ removeFile (fromRawFilePath file)
|
2015-06-01 13:52:23 -04:00
|
|
|
inRepo $ Git.Command.run
|
|
|
|
[ Param "rm"
|
|
|
|
, Param "--cached"
|
|
|
|
, Param "--force"
|
|
|
|
, Param "--quiet"
|
|
|
|
, Param "--"
|
2019-12-09 13:49:05 -04:00
|
|
|
, File (fromRawFilePath file)
|
2015-06-01 13:52:23 -04:00
|
|
|
]
|
2019-08-26 15:52:19 -04:00
|
|
|
next $ cleanup file key
|
2013-07-22 17:06:00 -04:00
|
|
|
|
2019-12-09 13:49:05 -04:00
|
|
|
cleanup :: RawFilePath -> Key -> CommandCleanup
|
2019-08-26 15:52:19 -04:00
|
|
|
cleanup file key = do
|
2016-01-05 17:22:19 -04:00
|
|
|
Database.Keys.removeAssociatedFile key =<< inRepo (toTopFilePath file)
|
2020-11-06 14:10:58 -04:00
|
|
|
src <- calcRepo (gitAnnexLocation key)
|
2012-03-14 17:43:34 -04:00
|
|
|
ifM (Annex.getState Annex.fast)
|
2014-04-15 14:23:08 -04: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 14:10:58 -04:00
|
|
|
s <- liftIO $ R.getFileStatus src
|
2014-04-15 14:23:08 -04:00
|
|
|
if linkCount s > 1
|
|
|
|
then copyfrom src
|
|
|
|
else hardlinkfrom src
|
2013-10-28 16:56:01 -04:00
|
|
|
, copyfrom src
|
2012-03-14 17:43:34 -04:00
|
|
|
)
|
2013-02-06 14:02:18 -04:00
|
|
|
where
|
2013-10-28 16:56:01 -04:00
|
|
|
copyfrom src =
|
2020-11-06 14:10:58 -04:00
|
|
|
thawContent file `after` liftIO
|
|
|
|
(copyFileExternal CopyAllMetaData
|
|
|
|
(fromRawFilePath src)
|
|
|
|
(fromRawFilePath file))
|
2013-10-28 16:56:01 -04:00
|
|
|
hardlinkfrom src =
|
|
|
|
-- creating a hard link could fall; fall back to copying
|
2020-11-06 14:10:58 -04:00
|
|
|
ifM (liftIO $ catchBoolIO $ R.createLink src file >> return True)
|
2013-10-28 16:56:01 -04:00
|
|
|
( return True
|
|
|
|
, copyfrom src
|
2013-02-06 14:02:18 -04:00
|
|
|
)
|