git-annex/Command/Uninit.hs

66 lines
1.8 KiB
Haskell
Raw Normal View History

2010-12-03 04:33:41 +00:00
{- git-annex command
-
- Copyright 2010 Joey Hess <joey@kitenet.net>
-
- Licensed under the GNU GPL version 3 or higher.
-}
module Command.Uninit where
2011-10-05 20:02:51 +00:00
import Common.Annex
2010-12-03 04:33:41 +00:00
import Command
import qualified Git
2011-12-14 19:56:11 +00:00
import qualified Git.Command
2010-12-03 04:33:41 +00:00
import qualified Annex
import qualified Command.Unannex
import Init
2011-10-04 04:40:47 +00:00
import qualified Annex.Branch
import Annex.Content
2010-12-03 04:33:41 +00:00
def :: [Command]
def = [addCheck check $ command "uninit" paramPaths seek
"de-initialize git-annex and clean out repository"]
check :: Annex ()
check = do
b <- current_branch
when (b == Annex.Branch.name) $ error $
"cannot uninit when the " ++ show b ++ " branch is checked out"
top <- fromRepo Git.repoPath
cwd <- liftIO getCurrentDirectory
2012-06-12 15:32:06 +00:00
whenM ((/=) <$> liftIO (absPath top) <*> liftIO (absPath cwd)) $
error "can only run uninit from the top of the git repository"
where
support all filename encodings with ghc 7.4 Under ghc 7.4, this seems to be able to handle all filename encodings again. Including filename encodings that do not match the LANG setting. I think this will not work with earlier versions of ghc, it uses some ghc internals. Turns out that ghc 7.4 has a special filesystem encoding that it uses when reading/writing filenames (as FilePaths). This encoding is documented to allow "arbitrary undecodable bytes to be round-tripped through it". So, to get FilePaths from eg, git ls-files, set the Handle that is reading from git to use this encoding. Then things basically just work. However, I have not found a way to make Text read using this encoding. Text really does assume unicode. So I had to switch back to using String when reading/writing data to git. Which is a pity, because it's some percent slower, but at least it works. Note that stdout and stderr also have to be set to this encoding, or printing out filenames that contain undecodable bytes causes a crash. IMHO this is a misfeature in ghc, that the user can pass you a filename, which you can readFile, etc, but that default, putStr of filename may cause a crash! Git.CheckAttr gave me special trouble, because the filenames I got back from git, after feeding them in, had further encoding breakage. Rather than try to deal with that, I just zip up the input filenames with the attributes. Which must be returned in the same order queried for this to work. Also of note is an apparent GHC bug I worked around in Git.CheckAttr. It used to forkProcess and feed git from the child process. Unfortunatly, after this forkProcess, accessing the `files` variable from the parent returns []. Not the value that was passed into the function. This screams of a bad bug, that's clobbering a variable, but for now I just avoid forkProcess there to work around it. That forkProcess was itself only added because of a ghc bug, #624389. I've confirmed that the test case for that bug doesn't reproduce it with ghc 7.4. So that's ok, except for the new ghc bug I have not isolated and reported. Why does this simple bit of code magnet the ghc bugs? :) Also, the symlink touching code is currently broken, when used on utf-8 filenames in a non-utf-8 locale, or probably on any filename containing undecodable bytes, and I temporarily commented it out.
2012-02-03 19:12:41 +00:00
current_branch = Git.Ref . Prelude.head . lines <$> revhead
revhead = inRepo $ Git.Command.pipeReadStrict
[Params "rev-parse --abbrev-ref HEAD"]
seek :: [CommandSeek]
seek = [withFilesInGit $ whenAnnexed startUnannex, withNothing start]
2011-12-31 08:11:39 +00:00
startUnannex :: FilePath -> (Key, Backend) -> CommandStart
startUnannex file info = do
-- Force fast mode before running unannex. This way, if multiple
-- files link to a key, it will be left in the annex and hardlinked
-- to by each.
Annex.changeState $ \s -> s { Annex.fast = True }
Command.Unannex.start file info
2010-12-03 04:33:41 +00:00
start :: CommandStart
2011-07-04 19:50:30 +00:00
start = next perform
2010-12-03 04:33:41 +00:00
perform :: CommandPerform
2011-07-04 19:50:30 +00:00
perform = next cleanup
cleanup :: CommandCleanup
cleanup = do
2011-11-11 05:52:58 +00:00
annexdir <- fromRepo gitAnnexDir
uninitialize
mapM_ removeAnnex =<< getKeysPresent
liftIO $ removeDirectoryRecursive annexdir
-- avoid normal shutdown
saveState False
2011-12-14 19:56:11 +00:00
inRepo $ Git.Command.run "branch"
[Param "-D", Param $ show Annex.Branch.name]
2011-11-11 05:52:58 +00:00
liftIO exitSuccess