module Text.ChangeMonger.Mercurial (hgChanges, hgChangesAll, hgChangesSince) where

import Control.Monad (liftM)
import Text.ChangeMonger.Parse (run)

runHg :: [String] -> IO String
runHg = run "hg"

-- | Ask Mercurial for changes in general; we accept an options argument which
-- will be passed onto Mercurial, so you can customize by using any of the many
-- options to 'log' which Mercurial understands.
hgChanges :: String -> IO String
hgChanges a = runHg ["log", a]

-- | Nothing fancy: just get the entire repository history.
hgChangesAll :: IO String
hgChangesAll = hgChanges ""

-- | Ask Mercurial for all changes, since the last tag. Conscientious folks
-- tag at every release, so this is a good heuristic.
hgChangesSince :: IO String
hgChangesSince = do tags <- liftM lines $ runHg ["tags", "--quiet"]
                    runHg ["log", "-r", ((release tags) ++ ":tip")]

release :: [String] -> String
release []      = ""
release [""]    = ""
-- Extract the most recent tag
release (_:x:_) = x -- ["tip", last-tag, other, tags, and, so, on]
release (_:_)   = ""