1 %  Copyright (C) 2003,2005 David Roundy
    2 %
    3 %  This program is free software; you can redistribute it and/or modify
    4 %  it under the terms of the GNU General Public License as published by
    5 %  the Free Software Foundation; either version 2, or (at your option)
    6 %  any later version.
    7 %
    8 %  This program is distributed in the hope that it will be useful,
    9 %  but WITHOUT ANY WARRANTY; without even the implied warranty of
   10 %  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   11 %  GNU General Public License for more details.
   12 %
   13 %  You should have received a copy of the GNU General Public License
   14 %  along with this program; see the file COPYING.  If not, write to
   15 %  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
   16 %  Boston, MA 02110-1301, USA.
   17 
   18 \darcsCommand{repair}
   19 \begin{code}
   20 module Darcs.Commands.Repair ( repair ) where
   21 import System.IO
   22 
   23 import Darcs.Commands
   24 import Darcs.Arguments ( DarcsFlag(),
   25                         working_repo_dir, umask_option,
   26                       )
   27 import Darcs.Repository ( withRepoLock, ($-), amInRepository,
   28                           replacePristineFromSlurpy, writePatchSet )
   29 import Darcs.Repository.Repair( replayRepository,
   30                                 RepositoryConsistency(..) )
   31 
   32 repair_description :: String
   33 repair_description = "Repair a corrupted repository."
   34 
   35 repair_help :: String
   36 repair_help =
   37  "The `darcs repair' command attempts to fix corruption in the current\n" ++
   38  "repository.  Currently it can only repair damage to the pristine tree,\n" ++
   39  "which is where most corruption occurs.\n"
   40 
   41 repair :: DarcsCommand
   42 repair = DarcsCommand {command_name = "repair",
   43                        command_help = repair_help,
   44                        command_description = repair_description,
   45                        command_extra_args = 0,
   46                        command_extra_arg_help = [],
   47                        command_command = repair_cmd,
   48                        command_prereq = amInRepository,
   49                        command_get_arg_possibilities = return [],
   50                        command_argdefaults = nodefaults,
   51                        command_advanced_options = [umask_option],
   52                        command_basic_options = [working_repo_dir]}
   53 
   54 repair_cmd :: [DarcsFlag] -> [String] -> IO ()
   55 repair_cmd opts _ = withRepoLock opts $- \repository -> do
   56   replayRepository repository opts $ \state ->
   57     case state of
   58       RepositoryConsistent ->
   59           putStrLn "The repository is already consistent, no changes made."
   60       BrokenPristine s -> do
   61                putStrLn "Fixing pristine tree..."
   62                replacePristineFromSlurpy repository s
   63       BrokenPatches s newps  -> do
   64                putStrLn "Writing out repaired patches..."
   65                writePatchSet newps opts
   66                putStrLn "Fixing pristine tree..."
   67                replacePristineFromSlurpy repository s
   68                return ()
   69 
   70 \end{code}