2010-12-03 04:33:41 +00:00
|
|
|
{- git-annex command
|
|
|
|
-
|
2021-11-08 20:47:00 +00:00
|
|
|
- Copyright 2010-2021 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
|
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)
|
|
|
|
|
2015-07-08 16:33:27 +00:00
|
|
|
cmd :: Command
|
2015-07-08 19:08:02 +00:00
|
|
|
cmd = addCheck check $
|
|
|
|
command "uninit" SectionUtility
|
|
|
|
"de-initialize git-annex and clean out repository"
|
|
|
|
paramPaths (withParams seek)
|
2010-12-30 19:06:26 +00:00
|
|
|
|
2011-10-27 20:31:35 +00:00
|
|
|
check :: Annex ()
|
|
|
|
check = do
|
2012-06-11 14:33:58 +00:00
|
|
|
b <- current_branch
|
2021-11-08 20:47:00 +00:00
|
|
|
when (b == Just Annex.Branch.name) $ giveup $
|
|
|
|
"cannot uninit when the " ++ Git.fromRef Annex.Branch.name ++ " branch is checked out"
|
2020-10-30 19:55:59 +00:00
|
|
|
top <- fromRepo Git.repoPath
|
|
|
|
currdir <- liftIO R.getCurrentDirectory
|
2014-06-10 23:20:14 +00:00
|
|
|
whenM ((/=) <$> liftIO (absPath top) <*> liftIO (absPath currdir)) $
|
2016-11-16 01:29:54 +00:00
|
|
|
giveup "can only run uninit from the top of the git repository"
|
2012-11-12 05:05:04 +00:00
|
|
|
where
|
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
|
|
|
|
2015-07-08 16:33:27 +00:00
|
|
|
seek :: CmdParams -> CommandSeek
|
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
|
|
|
seek ps = do
|
2020-05-28 19:55:17 +00:00
|
|
|
l <- workTreeItems ww ps
|
2020-09-18 17:33:35 +00:00
|
|
|
withFilesNotInGit
|
|
|
|
(CheckGitIgnore False)
|
2023-04-25 23:26:20 +00:00
|
|
|
(WarnUnmatchWorkTreeItems "uninit")
|
2022-10-26 17:58:20 +00:00
|
|
|
checksymlinks
|
2020-09-18 17:33:35 +00:00
|
|
|
l
|
2022-06-28 19:28:14 +00:00
|
|
|
withFilesInGitAnnex ww (Command.Unannex.seeker True) l
|
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
|
|
|
finish
|
2020-05-28 19:55:17 +00:00
|
|
|
where
|
2023-04-25 23:26:20 +00:00
|
|
|
ww = WarnUnmatchLsFiles "uninit"
|
2022-10-26 17:58:20 +00:00
|
|
|
checksymlinks (_, f) =
|
|
|
|
commandAction $ lookupKey f >>= \case
|
|
|
|
Nothing -> stop
|
|
|
|
Just k -> startCheckIncomplete (fromRawFilePath f) k
|
2012-10-22 15:54:50 +00:00
|
|
|
|
|
|
|
{- git annex symlinks that are not checked into git could be left by an
|
|
|
|
- interrupted add. -}
|
2014-04-17 22:03:39 +00:00
|
|
|
startCheckIncomplete :: FilePath -> Key -> CommandStart
|
2016-11-16 01:29:54 +00:00
|
|
|
startCheckIncomplete file _ = giveup $ unlines
|
2012-10-22 15:54:50 +00:00
|
|
|
[ 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
|
|
|
|
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
|
|
|
finish :: Annex ()
|
|
|
|
finish = 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
|
2019-08-27 16:59:57 +00:00
|
|
|
leftovers <- removeUnannexed =<< listKeys InAnnex
|
2016-07-12 18:18:35 +00:00
|
|
|
prepareRemoveAnnexDir annexdir
|
2013-07-18 17:30:12 +00:00
|
|
|
if null leftovers
|
2013-07-16 19:00:25 +00:00
|
|
|
then liftIO $ removeDirectoryRecursive annexdir
|
2016-11-16 01:29:54 +00:00
|
|
|
else giveup $ unlines
|
2013-07-16 19:00:25 +00:00
|
|
|
[ "Not fully uninitialized"
|
2020-11-06 18:10:58 +00:00
|
|
|
, "Some annexed data is still left in " ++ fromRawFilePath annexobjectdir
|
2013-07-16 19:00:25 +00:00
|
|
|
, "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."
|
|
|
|
]
|
2011-08-17 18:14:43 +00:00
|
|
|
uninitialize
|
2011-07-04 20:19:04 +00:00
|
|
|
-- avoid normal shutdown
|
2012-01-28 19:41:52 +00:00
|
|
|
saveState False
|
2021-11-08 20:47:00 +00:00
|
|
|
whenM (inRepo $ Git.Ref.exists Annex.Branch.fullname) $
|
|
|
|
inRepo $ Git.Command.run
|
|
|
|
[Param "branch", Param "-D", Param $ Git.fromRef Annex.Branch.name]
|
2011-11-11 05:52:58 +00:00
|
|
|
liftIO exitSuccess
|
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
|