summaryrefslogtreecommitdiff
path: root/git-recover-repository.hs
diff options
context:
space:
mode:
authorGravatar Joey Hess <joey@kitenet.net>2013-10-20 17:50:51 -0400
committerGravatar Joey Hess <joey@kitenet.net>2013-10-20 17:50:51 -0400
commit66ccc37a99994f1206d308c0c0003ac2605d3c42 (patch)
tree17fd97ca91a7c963cf80ac1274c94ac841e8572c /git-recover-repository.hs
parent66e2c6e3a16480e25f82ef447cfebac16997aac5 (diff)
git-recover-repository 1/2 done
Diffstat (limited to 'git-recover-repository.hs')
-rw-r--r--git-recover-repository.hs73
1 files changed, 73 insertions, 0 deletions
diff --git a/git-recover-repository.hs b/git-recover-repository.hs
new file mode 100644
index 000000000..6654057fa
--- /dev/null
+++ b/git-recover-repository.hs
@@ -0,0 +1,73 @@
+{- git-recover-repository program
+ -
+ - Copyright 2012 Joey Hess <joey@kitenet.net>
+ -
+ - Licensed under the GNU GPL version 3 or higher.
+ -}
+
+import System.Environment
+import System.Log.Logger
+import System.Log.Formatter
+import System.Log.Handler (setFormatter)
+import System.Log.Handler.Simple
+import qualified Data.Set as S
+
+import Common
+import qualified Git.CurrentRepo
+import qualified Git.RecoverRepository
+import qualified Git.Config
+
+header :: String
+header = "Usage: git-recover-repository"
+
+usage :: a
+usage = error $ "bad parameters\n\n" ++ header
+
+parseArgs :: IO Bool
+parseArgs = do
+ args <- getArgs
+ return $ or $ map parse args
+ where
+ parse "--force" = True
+ parse _ = usage
+
+enableDebugOutput :: IO ()
+enableDebugOutput = do
+ s <- setFormatter
+ <$> streamHandler stderr NOTICE
+ <*> pure (simpleLogFormatter "$msg")
+ updateGlobalLogger rootLoggerName (setLevel DEBUG . setHandlers [s])
+
+main :: IO ()
+main = do
+ enableDebugOutput
+ forced <- parseArgs
+
+ g <- Git.Config.read =<< Git.CurrentRepo.get
+ missing <- Git.RecoverRepository.cleanCorruptObjects g
+ stillmissing <- Git.RecoverRepository.retrieveMissingObjects missing g
+ if S.null stillmissing
+ then putStr $ unlines
+ [ "Successfully recovered repository!"
+ , "You should run \"git fsck\" to make sure, but it looks like"
+ , "everything was recovered ok."
+ ]
+ else do
+ putStrLn $ unwords
+ [ show (S.size stillmissing)
+ , "missing objects could not be recovered!"
+ ]
+ if forced
+ then do
+ remotebranches <- Git.RecoverRepository.removeTrackingBranches stillmissing g
+ unless (null remotebranches) $
+ putStrLn $ unwords
+ [ "removed"
+ , show (length remotebranches)
+ , "remote tracking branches that referred to missing objects"
+ ]
+ localbranches <- Git.RecoverRepository.resetLocalBranches stillmissing g
+ unless (null localbranches) $ do
+ putStrLn "Reset these local branches to old versions before the missing objects were committed:"
+ putStr $ unlines $ map show localbranches
+ else putStrLn "To force a recovery to a usable state, run this command again with the --force parameter."