--  Copyright (C) 2007 Florian Weimer
--
--  This program is free software; you can redistribute it and/or modify
--  it under the terms of the GNU General Public License as published by
--  the Free Software Foundation; either version 2, or (at your option)
--  any later version.
--
--  This program is distributed in the hope that it will be useful,
--  but WITHOUT ANY WARRANTY; without even the implied warranty of
--  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
--  GNU General Public License for more details.
--
--  You should have received a copy of the GNU General Public License
--  along with this program; see the file COPYING.  If not, write to
--  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
--  Boston, MA 02110-1301, USA.

module Darcs.UI.Commands.ShowTags
    ( showTags
    ) where

import Darcs.Prelude

import Control.Monad ( unless )
import Data.Maybe ( fromMaybe )
import System.IO ( stderr, hPutStrLn )

import Darcs.Patch.Set ( PatchSet, patchSetTags )
import Darcs.Repository ( readRepo, withRepositoryLocation, RepoJob(..) )
import Darcs.UI.Commands ( DarcsCommand(..), withStdOpts, nodefaults, findRepository )
import Darcs.UI.Completion ( noArgs )
import Darcs.UI.Flags ( DarcsFlag, useCache, getRepourl )
import Darcs.UI.Options ( oid, odesc, ocheck, defaultFlags, (?) )
import qualified Darcs.UI.Options.All as O
import Darcs.Util.Path ( AbsolutePath )
import Darcs.Util.Printer ( Doc, formatText )

showTagsDescription :: String
showTagsDescription :: String
showTagsDescription = String
"Show all tags in the repository."

showTagsHelp :: Doc
showTagsHelp :: Doc
showTagsHelp = Int -> [String] -> Doc
formatText Int
80
    [ String
"The tags command writes a list of all tags in the repository to "
      forall a. [a] -> [a] -> [a]
++ String
"standard output."
    , String
"Tab characters (ASCII character 9) in tag names are changed to spaces "
      forall a. [a] -> [a] -> [a]
++ String
"for better interoperability with shell tools. A warning is printed "
      forall a. [a] -> [a] -> [a]
++ String
"if this happens."
    ]

showTags :: DarcsCommand
showTags :: DarcsCommand
showTags = DarcsCommand
    { commandProgramName :: String
commandProgramName = String
"darcs"
    , commandName :: String
commandName = String
"tags"
    , commandHelp :: Doc
commandHelp = Doc
showTagsHelp
    , commandDescription :: String
commandDescription = String
showTagsDescription
    , commandExtraArgs :: Int
commandExtraArgs = Int
0
    , commandExtraArgHelp :: [String]
commandExtraArgHelp = []
    , commandCommand :: (AbsolutePath, AbsolutePath) -> [DarcsFlag] -> [String] -> IO ()
commandCommand = (AbsolutePath, AbsolutePath) -> [DarcsFlag] -> [String] -> IO ()
tagsCmd
    , commandPrereq :: [DarcsFlag] -> IO (Either String ())
commandPrereq = [DarcsFlag] -> IO (Either String ())
findRepository
    , commandCompleteArgs :: (AbsolutePath, AbsolutePath)
-> [DarcsFlag] -> [String] -> IO [String]
commandCompleteArgs = (AbsolutePath, AbsolutePath)
-> [DarcsFlag] -> [String] -> IO [String]
noArgs
    , commandArgdefaults :: [DarcsFlag] -> AbsolutePath -> [String] -> IO [String]
commandArgdefaults = [DarcsFlag] -> AbsolutePath -> [String] -> IO [String]
nodefaults
    , commandAdvancedOptions :: [DarcsOptDescr DarcsFlag]
commandAdvancedOptions = []
    , commandBasicOptions :: [DarcsOptDescr DarcsFlag]
commandBasicOptions = forall (d :: * -> *) f a b. OptSpec d f a b -> [d f]
odesc forall {a}. PrimOptSpec DarcsOptDescr DarcsFlag a (Maybe String)
showTagsBasicOpts
    , commandDefaults :: [DarcsFlag]
commandDefaults = forall (d :: * -> *) f b. OptSpec d f [f] b -> [f]
defaultFlags forall {a}.
DarcsOption
  a
  (Maybe String
   -> Maybe StdCmdAction
   -> Verbosity
   -> UseCache
   -> HooksConfig
   -> Bool
   -> Bool
   -> Bool
   -> a)
showTagsOpts
    , commandCheckOptions :: [DarcsFlag] -> [String]
commandCheckOptions = forall (d :: * -> *) f a b. OptSpec d f a b -> [f] -> [String]
ocheck forall {a}.
DarcsOption
  a
  (Maybe String
   -> Maybe StdCmdAction
   -> Verbosity
   -> UseCache
   -> HooksConfig
   -> Bool
   -> Bool
   -> Bool
   -> a)
showTagsOpts
    }
  where
    showTagsBasicOpts :: PrimOptSpec DarcsOptDescr DarcsFlag a (Maybe String)
showTagsBasicOpts = forall {a}. PrimOptSpec DarcsOptDescr DarcsFlag a (Maybe String)
O.possiblyRemoteRepo
    showTagsOpts :: DarcsOption
  a
  (Maybe String
   -> Maybe StdCmdAction
   -> Verbosity
   -> UseCache
   -> HooksConfig
   -> Bool
   -> Bool
   -> Bool
   -> a)
showTagsOpts = forall {a}. PrimOptSpec DarcsOptDescr DarcsFlag a (Maybe String)
showTagsBasicOpts forall b c a.
DarcsOption (Maybe StdCmdAction -> Verbosity -> b) c
-> DarcsOption
     (UseCache -> HooksConfig -> Bool -> Bool -> Bool -> a) b
-> DarcsOption a c
`withStdOpts` forall (d :: * -> *) f a. OptSpec d f a a
oid

tagsCmd :: (AbsolutePath, AbsolutePath) -> [DarcsFlag] -> [String] -> IO ()
tagsCmd :: (AbsolutePath, AbsolutePath) -> [DarcsFlag] -> [String] -> IO ()
tagsCmd (AbsolutePath, AbsolutePath)
_ [DarcsFlag]
opts [String]
_ = let repodir :: String
repodir = forall a. a -> Maybe a -> a
fromMaybe String
"." ([DarcsFlag] -> Maybe String
getRepourl [DarcsFlag]
opts) in
    forall a. UseCache -> String -> RepoJob a -> IO a
withRepositoryLocation (PrimDarcsOption UseCache
useCache forall (d :: * -> *) f v.
(forall a. PrimOptSpec d f a v) -> [f] -> v
? [DarcsFlag]
opts) String
repodir forall a b. (a -> b) -> a -> b
$ forall a.
(forall (rt :: RepoType) (p :: * -> * -> *) wR wU.
 (IsRepoType rt, RepoPatch p, ApplyState p ~ Tree) =>
 Repository rt p wR wU wR -> IO a)
-> RepoJob a
RepoJob forall a b. (a -> b) -> a -> b
$ \Repository rt p wR wU wR
repo ->
        forall (rt :: RepoType) (p :: * -> * -> *) wR wU wT.
(IsRepoType rt, RepoPatch p) =>
Repository rt p wR wU wT -> IO (PatchSet rt p Origin wR)
readRepo Repository rt p wR wU wR
repo forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= forall (rt :: RepoType) (p :: * -> * -> *) wW wZ.
PatchSet rt p wW wZ -> IO ()
printTags
  where
    printTags :: PatchSet rt p wW wZ -> IO ()
    printTags :: forall (rt :: RepoType) (p :: * -> * -> *) wW wZ.
PatchSet rt p wW wZ -> IO ()
printTags = forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
(a -> m b) -> t a -> m ()
mapM_ String -> IO ()
process forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (rt :: RepoType) (p :: * -> * -> *) wX wY.
PatchSet rt p wX wY -> [String]
patchSetTags
    process :: String -> IO ()
    process :: String -> IO ()
process String
t = String -> String -> Bool -> IO String
normalize String
t String
t Bool
False forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= String -> IO ()
putStrLn
    normalize :: String -> String -> Bool -> IO String
    normalize :: String -> String -> Bool -> IO String
normalize String
_ [] Bool
_ = forall (m :: * -> *) a. Monad m => a -> m a
return []
    normalize String
t (Char
x : String
xs) Bool
flag =
        if Char
x forall a. Eq a => a -> a -> Bool
== Char
'\t' then do
            forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless Bool
flag forall a b. (a -> b) -> a -> b
$
                Handle -> String -> IO ()
hPutStrLn Handle
stderr forall a b. (a -> b) -> a -> b
$ String
"warning: tag with TAB character: " forall a. [a] -> [a] -> [a]
++ String
t
            String
rest <- String -> String -> Bool -> IO String
normalize String
t String
xs Bool
True
            forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ Char
' ' forall a. a -> [a] -> [a]
: String
rest
        else do
            String
rest <- String -> String -> Bool -> IO String
normalize String
t String
xs Bool
flag
            forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ Char
x forall a. a -> [a] -> [a]
: String
rest