Merge branch 'darcs' into master
[git-darcs-import.git] / src / Darcs / Commands / ShowContents.lhs
blob4d572e5d8f8524fb04e19c8efe1e96653742c01e
1 % Copyright (C) 2007 Eric Kow
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.
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.
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.
18 \subsubsection{darcs show contents}
19 \begin{code}
20 module Darcs.Commands.ShowContents ( show_contents ) where
22 import Control.Monad ( filterM )
23 import System.IO ( stdout )
24 import System.FilePath ( takeFileName )
26 import qualified Data.ByteString as B
27 import Workaround ( getCurrentDirectory )
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 \end{code}
40 \options{show contents}
41 \begin{code}
42 show_contents_description :: String
43 show_contents_description = "Outputs a specific version of a file."
44 \end{code}
45 \haskell{show_contents_help}
46 \begin{code}
47 show_contents_help :: String
48 show_contents_help =
49 "Show contents can be used to display an earlier version of some file(s).\n"++
50 "If you give show contents no version arguments, it displays the recorded\n"++
51 "version of the file(s).\n"
53 show_contents :: DarcsCommand
54 show_contents = DarcsCommand {command_name = "contents",
55 command_help = show_contents_help,
56 command_description = show_contents_description,
57 command_extra_args = -1,
58 command_extra_arg_help
59 = ["[FILE]..."],
60 command_command = show_contents_cmd,
61 command_prereq = findRepository,
62 command_get_arg_possibilities = return [],
63 command_argdefaults = nodefaults,
64 command_advanced_options = [],
65 command_basic_options = [match_one, working_repo_dir]}
66 \end{code}
68 \begin{code}
69 show_contents_cmd :: [DarcsFlag] -> [String] -> IO ()
70 show_contents_cmd opts args = withRepository opts $- \repository -> do
71 formerdir <- getCurrentDirectory
72 path_list <- map sp2fn `fmap` fixSubPaths opts args
73 thename <- return $ takeFileName formerdir
74 withTempDir thename $ \dir -> do
75 if have_nonrange_match opts
76 then get_partial_nonrange_match repository opts path_list
77 else createPartialsPristineDirectoryTree repository path_list (toFilePath dir)
78 filterM mDoesFileExist path_list >>= mapM_ (\f -> mReadFilePS f >>= B.hPut stdout)
79 \end{code}