module Text.ChangeMonger.Git (gitChanges, gitChangesAll, gitChangesSince) where

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

gitRun :: [String] -> IO String
gitRun a = run "git" $ ["--no-pager"] ++ a

-- | Ask Git for changes in general; we accept an options argument which
-- will be passed onto Git, so you can customize by using any of the many
-- options to 'log' which Git understands.
-- Tricky bits: we can't pass Git \"\", because it won't treat it as non-existent,
-- so the implementation needs to reflect this.
gitChanges :: String -> IO String
gitChanges "" = gitRun ["log"]
gitChanges a = gitRun ["log", a]

-- | Nothing fancy: just get the entire repository history.
gitChangesAll :: IO String
gitChangesAll = gitChanges []

-- | Ask Git for all changes, since the last tag. Conscientious folks
-- tag at every release, so this is a good heuristic.
-- Tricky bits: 'run' returns the name of the last tag with '\n' included,
-- so we use 'init' to drop the last character and remove '\n'.
-- Then, we ask "git log"  'tag-name..', which is its syntax for "all commits"
-- since a particular tag. In this case, our tag-name is the last tag, so we're good.
gitChangesSince :: IO String
gitChangesSince = do tag <- liftM init $ gitRun ["describe", "--abbrev=0"]
                     gitChanges (tag ++ "..")