1 %  Copyright (C) 2007 Eric Kow
    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{show contents}
   19 \begin{code}
   20 module Darcs.Commands.ShowContents ( show_contents ) where
   21 
   22 import Control.Monad ( filterM )
   23 import System.IO ( stdout )
   24 import System.FilePath.Posix ( takeFileName )
   25 
   26 import qualified Data.ByteString as B
   27 import Workaround ( getCurrentDirectory )
   28 
   29 import Darcs.Commands ( DarcsCommand(..), nodefaults )
   30 import Darcs.Arguments ( DarcsFlag, match_one,
   31                          working_repo_dir, fixSubPaths )
   32 import Darcs.RepoPath ( toFilePath, sp2fn )
   33 import Darcs.IO ( mReadFilePS, mDoesFileExist )
   34 import Darcs.Match ( get_partial_nonrange_match, have_nonrange_match )
   35 import Darcs.Repository ( withRepository, ($-), findRepository,
   36                           createPartialsPristineDirectoryTree )
   37 import Darcs.Lock ( withTempDir )
   38 
   39 show_contents_description :: String
   40 show_contents_description = "Outputs a specific version of a file."
   41 
   42 show_contents_help :: String
   43 show_contents_help =
   44   "Show contents can be used to display an earlier version of some file(s).\n"++
   45   "If you give show contents no version arguments, it displays the recorded\n"++
   46   "version of the file(s).\n"
   47 
   48 show_contents :: DarcsCommand
   49 show_contents = DarcsCommand {command_name = "contents",
   50                               command_help = show_contents_help,
   51                               command_description = show_contents_description,
   52                               command_extra_args = -1,
   53                               command_extra_arg_help
   54                                     = ["[FILE]..."],
   55                               command_command = show_contents_cmd,
   56                               command_prereq = findRepository,
   57                               command_get_arg_possibilities = return [],
   58                               command_argdefaults = nodefaults,
   59                               command_advanced_options = [],
   60                               command_basic_options = [match_one, working_repo_dir]}
   61 
   62 show_contents_cmd :: [DarcsFlag] -> [String] -> IO ()
   63 show_contents_cmd opts args = withRepository opts $- \repository -> do
   64   formerdir <- getCurrentDirectory
   65   path_list <- map sp2fn `fmap` fixSubPaths opts args
   66   thename <- return $ takeFileName formerdir
   67   withTempDir thename $ \dir -> do
   68      if have_nonrange_match opts
   69         then get_partial_nonrange_match repository opts path_list
   70         else createPartialsPristineDirectoryTree repository path_list (toFilePath dir)
   71      filterM mDoesFileExist path_list >>= mapM_ (\f -> mReadFilePS f >>= B.hPut stdout)
   72 \end{code}