2010-12-03 04:33:41 +00:00
|
|
|
{- git-annex command
|
|
|
|
-
|
2023-05-11 17:36:59 +00:00
|
|
|
- Copyright 2010-2023 Joey Hess <id@joeyh.name>
|
2010-12-03 04:33:41 +00:00
|
|
|
-
|
2019-03-13 19:48:14 +00:00
|
|
|
- Licensed under the GNU AGPL version 3 or higher.
|
2010-12-03 04:33:41 +00:00
|
|
|
-}
|
|
|
|
|
|
|
|
module Command.Uninit where
|
|
|
|
|
|
|
|
import Command
|
2011-06-30 17:16:57 +00:00
|
|
|
import qualified Git
|
2011-12-14 19:56:11 +00:00
|
|
|
import qualified Git.Command
|
2021-11-08 20:47:00 +00:00
|
|
|
import qualified Git.Ref
|
2023-05-11 17:36:59 +00:00
|
|
|
import qualified Git.Branch
|
2010-12-03 04:33:41 +00:00
|
|
|
import qualified Command.Unannex
|
2011-10-04 04:40:47 +00:00
|
|
|
import qualified Annex.Branch
|
2021-03-01 17:05:47 +00:00
|
|
|
import qualified Annex.Queue
|
2016-07-12 18:18:35 +00:00
|
|
|
import qualified Database.Keys
|
2011-10-04 04:40:47 +00:00
|
|
|
import Annex.Content
|
2014-01-26 20:36:31 +00:00
|
|
|
import Annex.Init
|
2020-09-18 17:33:35 +00:00
|
|
|
import Annex.CheckIgnore
|
2022-10-26 17:58:20 +00:00
|
|
|
import Annex.WorkTree
|
2014-07-11 18:52:53 +00:00
|
|
|
import Utility.FileMode
|
2019-12-11 18:12:22 +00:00
|
|
|
import qualified Utility.RawFilePath as R
|
2014-07-11 18:52:53 +00:00
|
|
|
|
2023-03-01 19:55:58 +00:00
|
|
|
import System.PosixCompat.Files (linkCount)
|
2023-05-11 17:36:59 +00:00
|
|
|
import Control.Concurrent.STM
|
2023-03-01 19:55:58 +00:00
|
|
|
|
2015-07-08 16:33:27 +00:00
|
|
|
cmd :: Command
|
2023-05-11 17:36:59 +00:00
|
|
|
cmd = withAnnexOptions [jsonOptions] $
|
2015-07-08 19:08:02 +00:00
|
|
|
command "uninit" SectionUtility
|
|
|
|
"de-initialize git-annex and clean out repository"
|
2023-05-11 17:20:35 +00:00
|
|
|
paramNothing (withParams seek)
|
2010-12-30 19:06:26 +00:00
|
|
|
|
2023-05-11 17:36:59 +00:00
|
|
|
seek :: CmdParams -> CommandSeek
|
|
|
|
seek = withNothing $ do
|
|
|
|
ok <- liftIO $ newTVarIO False
|
|
|
|
let checkok v a = do
|
|
|
|
liftIO $ atomically $ writeTVar ok v
|
|
|
|
() <- a
|
|
|
|
liftIO $ atomically $ readTVar ok
|
|
|
|
let recordok = do
|
|
|
|
liftIO $ atomically $ writeTVar ok True
|
|
|
|
return True
|
|
|
|
let recordnotok = liftIO $ atomically $ writeTVar ok False
|
|
|
|
|
|
|
|
whenM (checkok False $ commandAction $ checkCanUninit recordok) $ do
|
|
|
|
let symlinksok = checkok True $ withFilesNotInGit
|
|
|
|
(CheckGitIgnore False)
|
|
|
|
(WarnUnmatchWorkTreeItems "uninit")
|
|
|
|
(checksymlinks recordnotok)
|
|
|
|
=<< workTreeItems ww []
|
|
|
|
whenM symlinksok $ do
|
|
|
|
withFilesInGitAnnex ww (Command.Unannex.seeker True)
|
|
|
|
=<< workTreeItems ww []
|
|
|
|
whenM (checkok False $ commandAction $ removeAnnexDir recordok) $
|
2023-05-11 17:50:20 +00:00
|
|
|
commandAction completeUnitialize
|
2012-11-12 05:05:04 +00:00
|
|
|
where
|
2023-05-11 17:36:59 +00:00
|
|
|
ww = WarnUnmatchLsFiles "uninit"
|
|
|
|
checksymlinks recordnotok (_, f) =
|
|
|
|
commandAction $ lookupKey f >>= \case
|
|
|
|
Nothing -> stop
|
|
|
|
Just k -> startCheckIncomplete recordnotok f k
|
|
|
|
|
|
|
|
checkCanUninit :: CommandCleanup -> CommandStart
|
|
|
|
checkCanUninit recordok =
|
|
|
|
starting "uninit check" (ActionItemOther Nothing) (SeekInput []) $ do
|
|
|
|
runchecks
|
|
|
|
next recordok
|
|
|
|
where
|
|
|
|
runchecks = do
|
|
|
|
b <- current_branch
|
|
|
|
when (b == Just Annex.Branch.name) $ giveup $
|
|
|
|
"cannot uninit when the " ++ Git.fromRef Annex.Branch.name ++ " branch is checked out"
|
|
|
|
top <- fromRepo Git.repoPath
|
|
|
|
currdir <- liftIO R.getCurrentDirectory
|
|
|
|
whenM ((/=) <$> liftIO (absPath top) <*> liftIO (absPath currdir)) $
|
|
|
|
giveup "can only run uninit from the top of the git repository"
|
|
|
|
|
2021-11-08 20:47:00 +00:00
|
|
|
current_branch =
|
|
|
|
ifM (inRepo Git.Ref.headExists)
|
|
|
|
( Just . Git.Ref . encodeBS . Prelude.head . lines . decodeBS <$> revhead
|
|
|
|
, return Nothing
|
|
|
|
)
|
2012-11-12 05:05:04 +00:00
|
|
|
revhead = inRepo $ Git.Command.pipeReadStrict
|
2015-06-01 17:52:23 +00:00
|
|
|
[Param "rev-parse", Param "--abbrev-ref", Param "HEAD"]
|
2011-07-04 20:19:04 +00:00
|
|
|
|
2012-10-22 15:54:50 +00:00
|
|
|
{- git annex symlinks that are not checked into git could be left by an
|
|
|
|
- interrupted add. -}
|
2023-05-11 17:36:59 +00:00
|
|
|
startCheckIncomplete :: Annex () -> RawFilePath -> Key -> CommandStart
|
|
|
|
startCheckIncomplete recordnotok file key =
|
|
|
|
starting "uninit check" (mkActionItem (file, key)) (SeekInput []) $ do
|
|
|
|
recordnotok
|
|
|
|
giveup $ unlines err
|
|
|
|
where
|
|
|
|
err =
|
|
|
|
[ fromRawFilePath file ++ " points to annexed content, but is not checked into git."
|
|
|
|
, "Perhaps this was left behind by an interrupted git annex add?"
|
|
|
|
, "Not continuing with uninit; either delete or git annex add the file and retry."
|
|
|
|
]
|
2011-10-27 20:31:35 +00:00
|
|
|
|
2023-05-11 17:36:59 +00:00
|
|
|
removeAnnexDir :: CommandCleanup -> CommandStart
|
|
|
|
removeAnnexDir recordok = do
|
2021-03-01 17:05:47 +00:00
|
|
|
Annex.Queue.flush
|
2019-12-18 20:45:03 +00:00
|
|
|
annexdir <- fromRawFilePath <$> fromRepo gitAnnexDir
|
2013-07-16 19:00:25 +00:00
|
|
|
annexobjectdir <- fromRepo gitAnnexObjectDir
|
2023-05-11 17:36:59 +00:00
|
|
|
starting ("uninit objects") (ActionItemOther (Just (QuotedPath annexobjectdir))) (SeekInput []) $ do
|
|
|
|
leftovers <- removeUnannexed =<< listKeys InAnnex
|
|
|
|
prepareRemoveAnnexDir annexdir
|
|
|
|
if null leftovers
|
|
|
|
then do
|
|
|
|
liftIO $ removeDirectoryRecursive annexdir
|
|
|
|
next recordok
|
|
|
|
else giveup $ unlines
|
|
|
|
[ "Not fully uninitialized"
|
|
|
|
, "Some annexed data is still left in " ++ fromRawFilePath annexobjectdir
|
|
|
|
, "This may include deleted files, or old versions of modified files."
|
|
|
|
, ""
|
|
|
|
, "If you don't care about preserving the data, just delete the"
|
|
|
|
, "directory."
|
|
|
|
, ""
|
|
|
|
, "Or, you can move it to another location, in case it turns out"
|
|
|
|
, "something in there is important."
|
|
|
|
, ""
|
|
|
|
, "Or, you can run `git annex unused` followed by `git annex dropunused`"
|
|
|
|
, "to remove data that is not used by any tag or branch, which might"
|
|
|
|
, "take care of all the data."
|
|
|
|
, ""
|
|
|
|
, "Then run `git annex uninit` again to finish."
|
|
|
|
]
|
2013-07-18 17:30:12 +00:00
|
|
|
|
2014-07-11 18:52:53 +00:00
|
|
|
{- Turn on write bits in all remaining files in the annex directory, in
|
2016-07-12 18:18:35 +00:00
|
|
|
- preparation for removal.
|
|
|
|
-
|
|
|
|
- Also closes sqlite databases that might be in the directory,
|
|
|
|
- to avoid later failure to write any cached changes to them. -}
|
|
|
|
prepareRemoveAnnexDir :: FilePath -> Annex ()
|
|
|
|
prepareRemoveAnnexDir annexdir = do
|
|
|
|
Database.Keys.closeDb
|
|
|
|
liftIO $ prepareRemoveAnnexDir' annexdir
|
|
|
|
|
|
|
|
prepareRemoveAnnexDir' :: FilePath -> IO ()
|
|
|
|
prepareRemoveAnnexDir' annexdir =
|
2017-05-16 03:32:17 +00:00
|
|
|
dirTreeRecursiveSkipping (const False) annexdir
|
2020-11-06 18:10:58 +00:00
|
|
|
>>= mapM_ (void . tryIO . allowWrite . toRawFilePath)
|
2014-07-11 18:52:53 +00:00
|
|
|
|
2013-07-18 17:30:12 +00:00
|
|
|
{- Keys that were moved out of the annex have a hard link still in the
|
|
|
|
- annex, with > 1 link count, and those can be removed.
|
|
|
|
-
|
|
|
|
- Returns keys that cannot be removed. -}
|
|
|
|
removeUnannexed :: [Key] -> Annex [Key]
|
|
|
|
removeUnannexed = go []
|
|
|
|
where
|
2014-10-09 18:53:13 +00:00
|
|
|
go c [] = return c
|
2013-07-18 17:30:12 +00:00
|
|
|
go c (k:ks) = ifM (inAnnexCheck k $ liftIO . enoughlinks)
|
|
|
|
( do
|
2020-07-25 15:54:34 +00:00
|
|
|
lockContentForRemoval k noop removeAnnex
|
2013-07-18 17:30:12 +00:00
|
|
|
go c ks
|
|
|
|
, go (k:c) ks
|
|
|
|
)
|
2013-07-22 21:06:00 +00:00
|
|
|
enoughlinks f = catchBoolIO $ do
|
2019-12-11 18:12:22 +00:00
|
|
|
s <- R.getFileStatus f
|
2013-07-18 17:30:12 +00:00
|
|
|
return $ linkCount s > 1
|
2023-05-11 17:36:59 +00:00
|
|
|
|
2023-05-11 17:50:20 +00:00
|
|
|
completeUnitialize :: CommandStart
|
|
|
|
completeUnitialize =
|
2023-05-11 17:36:59 +00:00
|
|
|
starting ("uninit finish") (ActionItemOther Nothing) (SeekInput []) $ do
|
|
|
|
uninitialize
|
|
|
|
removeAnnexBranch
|
2023-05-11 17:50:20 +00:00
|
|
|
next $ return True
|
2023-05-11 17:36:59 +00:00
|
|
|
|
|
|
|
removeAnnexBranch :: Annex ()
|
|
|
|
removeAnnexBranch = do
|
|
|
|
-- avoid normal shutdown commit to the branch
|
|
|
|
saveState False
|
|
|
|
whenM (inRepo $ Git.Ref.exists Annex.Branch.fullname) $
|
|
|
|
inRepo $ Git.Branch.delete Annex.Branch.name
|