1 %  Copyright (C) 2005 Florian Weimer
    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 files}
   19 \begin{code}
   20 {-# OPTIONS_GHC -cpp #-}
   21 {-# LANGUAGE CPP #-}
   22 #include "gadts.h"
   23 module Darcs.Commands.ShowFiles ( show_files, show_manifest ) where
   24 import Darcs.Arguments ( DarcsFlag(..), working_repo_dir,
   25                         files, directories, pending, nullFlag )
   26 import Darcs.Commands ( DarcsCommand(..), nodefaults, command_alias )
   27 import Darcs.Repository ( Repository, amInRepository, slurp_pending, slurp_recorded,
   28                           withRepository )
   29 import Darcs.Patch ( RepoPatch )
   30 import Darcs.SlurpDirectory ( Slurpy, list_slurpy, list_slurpy_files, list_slurpy_dirs )
   31 
   32 show_files_description :: String
   33 show_files_description = "Show version-controlled files in the working copy."
   34 
   35 show_files_help :: String
   36 show_files_help =
   37  "The `darcs show files' command lists those files and directories in\n" ++
   38  "the working tree that are under version control.  This command is\n" ++
   39  "primarily for scripting purposes; end users will probably want `darcs\n" ++
   40  "whatsnew --summary'.\n" ++
   41  "\n" ++
   42  "A file is `pending' if it has been added but not recorded.  By\n" ++
   43  "default, pending files (and directories) are listed; the --no-pending\n" ++
   44  "option prevents this.\n" ++
   45  "\n" ++
   46  "By default `darcs show files' lists both files and directories, but\n" ++
   47  "the alias `darcs show manifest' only lists files.  The --files,\n" ++
   48  "--directories, --no-files and --no-directories modify this behaviour.\n" ++
   49  "\n" ++
   50  "By default entries are one-per-line (i.e. newline separated).  This\n" ++
   51  "can cause problems if the files themselves contain newlines or other\n" ++
   52  "control characters.  To get aroudn this, the --null option uses the\n" ++
   53  "null character instead.  The script interpreting output from this\n" ++
   54  "command needs to understand this idiom; `xargs -0' is such a command.\n" ++
   55  "\n" ++
   56  "For example, to list version-controlled files by size:\n" ++
   57  "\n" ++
   58  "    darcs show files -0 | xargs -0 ls -ldS\n"
   59 
   60 show_files :: DarcsCommand
   61 show_files = DarcsCommand {
   62   command_name = "files",
   63   command_help = show_files_help,
   64   command_description = show_files_description,
   65   command_extra_args = 0,
   66   command_extra_arg_help = [],
   67   command_command = manifest_cmd to_list_files,
   68   command_prereq = amInRepository,
   69   command_get_arg_possibilities = return [],
   70   command_argdefaults = nodefaults,
   71   command_advanced_options = [],
   72   command_basic_options = [files, directories, pending, nullFlag,
   73                           working_repo_dir] }
   74 
   75 show_manifest :: DarcsCommand
   76 show_manifest = command_alias "manifest" show_files {
   77   command_command = manifest_cmd to_list_manifest
   78 }
   79 
   80 to_list_files, to_list_manifest :: [DarcsFlag] -> Slurpy -> [FilePath]
   81 to_list_files    opts = files_dirs (NoFiles `notElem` opts) (NoDirectories `notElem` opts)
   82 to_list_manifest opts = files_dirs (NoFiles `notElem` opts) (Directories `elem` opts)
   83 
   84 files_dirs :: Bool -> Bool -> Slurpy -> [FilePath]
   85 files_dirs False False = \_ -> []
   86 files_dirs False True  = list_slurpy_dirs
   87 files_dirs True  False = list_slurpy_files
   88 files_dirs True  True  = list_slurpy
   89 
   90 manifest_cmd :: ([DarcsFlag] -> Slurpy -> [FilePath]) -> [DarcsFlag] -> [String] -> IO ()
   91 manifest_cmd to_list opts _ = do
   92     list <- (to_list opts) `fmap` withRepository opts slurp
   93     mapM_ output list
   94     where slurp :: RepoPatch p => Repository p C(r u r) -> IO Slurpy
   95           slurp = if NoPending `notElem` opts
   96                   then slurp_pending else slurp_recorded
   97           output_null name = do { putStr name ; putChar '\0' }
   98           output = if NullFlag `elem` opts then output_null else putStrLn
   99 \end{code}