{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE CPP               #-}

-- |
-- Module      :  Distribution.Fedora.Branch
-- Copyright   :  (C) 2020-2022  Jens Petersen
--
-- Maintainer  :  Jens Petersen <petersen@fedoraproject.org>
--
-- Explanation: Fedora Branch type and functions

-- 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 3 of the License, or
-- (at your option) any later version.

module Distribution.Fedora.Branch
  ( Branch(..)
  , readBranch
  , readBranch'
  , eitherBranch
  , eitherBranch'
  , readActiveBranch
  , readActiveBranch'
  , eitherActiveBranch
  , newerBranch
  , releaseBranch
  , getFedoraBranches
  , getFedoraBranched
  , branchDestTag
  , branchDist
  , branchTarget
  )
where

#if (defined(MIN_VERSION_base) && MIN_VERSION_base(4,8,0))
#else
import Control.Applicative ((<$>))
#endif

import Data.Char (isDigit)
import qualified Data.Text as T

import qualified Distribution.Fedora as Dist

-- | Branch datatype
--
-- Branch can be rawhide, or a fedora or epel branch
data Branch = EPEL Int | EPELNext Int | Fedora Int | Rawhide
  deriving (Branch -> Branch -> Bool
(Branch -> Branch -> Bool)
-> (Branch -> Branch -> Bool) -> Eq Branch
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: Branch -> Branch -> Bool
$c/= :: Branch -> Branch -> Bool
== :: Branch -> Branch -> Bool
$c== :: Branch -> Branch -> Bool
Eq, Eq Branch
Eq Branch
-> (Branch -> Branch -> Ordering)
-> (Branch -> Branch -> Bool)
-> (Branch -> Branch -> Bool)
-> (Branch -> Branch -> Bool)
-> (Branch -> Branch -> Bool)
-> (Branch -> Branch -> Branch)
-> (Branch -> Branch -> Branch)
-> Ord Branch
Branch -> Branch -> Bool
Branch -> Branch -> Ordering
Branch -> Branch -> Branch
forall a.
Eq a
-> (a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
min :: Branch -> Branch -> Branch
$cmin :: Branch -> Branch -> Branch
max :: Branch -> Branch -> Branch
$cmax :: Branch -> Branch -> Branch
>= :: Branch -> Branch -> Bool
$c>= :: Branch -> Branch -> Bool
> :: Branch -> Branch -> Bool
$c> :: Branch -> Branch -> Bool
<= :: Branch -> Branch -> Bool
$c<= :: Branch -> Branch -> Bool
< :: Branch -> Branch -> Bool
$c< :: Branch -> Branch -> Bool
compare :: Branch -> Branch -> Ordering
$ccompare :: Branch -> Branch -> Ordering
$cp1Ord :: Eq Branch
Ord)

-- | Read a Fedora Branch name, otherwise return branch string
eitherBranch :: String -> Either String Branch
eitherBranch :: String -> Either String Branch
eitherBranch String
"rawhide" = Branch -> Either String Branch
forall a b. b -> Either a b
Right Branch
Rawhide
eitherBranch (Char
'f':String
ns) | (Char -> Bool) -> String -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
all Char -> Bool
isDigit String
ns = let br :: Branch
br = Int -> Branch
Fedora (String -> Int
forall a. Read a => String -> a
read String
ns) in Branch -> Either String Branch
forall a b. b -> Either a b
Right Branch
br
-- FIXME add proper parsing:
eitherBranch String
"epel8-next" = Branch -> Either String Branch
forall a b. b -> Either a b
Right (Branch -> Either String Branch) -> Branch -> Either String Branch
forall a b. (a -> b) -> a -> b
$ Int -> Branch
EPELNext Int
8
eitherBranch String
"epel9-next" = Branch -> Either String Branch
forall a b. b -> Either a b
Right (Branch -> Either String Branch) -> Branch -> Either String Branch
forall a b. (a -> b) -> a -> b
$ Int -> Branch
EPELNext Int
9
eitherBranch (Char
'e':Char
'p':Char
'e':Char
'l':String
n) | (Char -> Bool) -> String -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
all Char -> Bool
isDigit String
n = let br :: Branch
br = Int -> Branch
EPEL (String -> Int
forall a. Read a => String -> a
read String
n) in Branch -> Either String Branch
forall a b. b -> Either a b
Right Branch
br
eitherBranch (Char
'e':Char
'l':String
n) | (Char -> Bool) -> String -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
all Char -> Bool
isDigit String
n = let br :: Branch
br = Int -> Branch
EPEL (String -> Int
forall a. Read a => String -> a
read String
n) in Branch -> Either String Branch
forall a b. b -> Either a b
Right Branch
br
eitherBranch String
cs = String -> Either String Branch
forall a b. a -> Either a b
Left String
cs

-- | Read a Fedora Branch name, otherwise return an error message
eitherBranch' :: String -> Either String Branch
eitherBranch' :: String -> Either String Branch
eitherBranch' String
cs = case String -> Either String Branch
eitherBranch String
cs of
  Right Branch
br -> Branch -> Either String Branch
forall a b. b -> Either a b
Right Branch
br
  Left String
xs -> String -> Either String Branch
forall a b. a -> Either a b
Left (String -> Either String Branch) -> String -> Either String Branch
forall a b. (a -> b) -> a -> b
$ String
xs String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
" is not a known Fedora/EPEL branch"

-- | Read a Fedora Branch name
readBranch :: String -> Maybe Branch
readBranch :: String -> Maybe Branch
readBranch String
bs =
  case String -> Either String Branch
eitherBranch String
bs of
    Left String
_ -> Maybe Branch
forall a. Maybe a
Nothing
    Right Branch
br -> Branch -> Maybe Branch
forall a. a -> Maybe a
Just Branch
br

-- | Unsafely read a Fedora Branch name: errors for unknown branches
readBranch' :: String -> Branch
readBranch' :: String -> Branch
readBranch' String
bs =
  case String -> Either String Branch
eitherBranch String
bs of
    Left String
e -> String -> Branch
forall a. String -> a
error' (String -> Branch) -> String -> Branch
forall a b. (a -> b) -> a -> b
$! String
"unknown Fedora branch: " String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
e
    Right Branch
br -> Branch
br

-- | Read a Branch name (one of the list of active branches)
--
-- Provides error strings for inactive or unknown branches.
eitherActiveBranch :: [Branch] -> String -> Either String Branch
eitherActiveBranch :: [Branch] -> String -> Either String Branch
eitherActiveBranch [Branch]
active String
bs =
  case String -> Either String Branch
eitherBranch String
bs of
    Left String
e -> String -> Either String Branch
forall a b. a -> Either a b
Left String
e
    Right Branch
br -> if Branch
br Branch -> [Branch] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` [Branch]
active
                then Branch -> Either String Branch
forall a b. b -> Either a b
Right Branch
br
                else String -> Either String Branch
forall a b. a -> Either a b
Left String
bs

-- | Read a Branch name (one of the list of active branches)
--
-- Similar to eitherActiveBranch but ignores any error string
readActiveBranch :: [Branch] -> String -> Maybe Branch
readActiveBranch :: [Branch] -> String -> Maybe Branch
readActiveBranch [Branch]
active String
cs =
  case [Branch] -> String -> Either String Branch
eitherActiveBranch [Branch]
active String
cs of
    Left String
_ -> Maybe Branch
forall a. Maybe a
Nothing
    Right Branch
br -> Branch -> Maybe Branch
forall a. a -> Maybe a
Just Branch
br

-- | Read a Branch name (one of the list of active branches)
--
-- Like readActiveBranch, but errors for inactive or unknown branches.
readActiveBranch' :: [Branch] -> String -> Branch
readActiveBranch' :: [Branch] -> String -> Branch
readActiveBranch' [Branch]
active String
cs =
  case [Branch] -> String -> Either String Branch
eitherActiveBranch [Branch]
active String
cs of
    Left String
e -> String -> Branch
forall a. String -> a
error' (String -> Branch) -> String -> Branch
forall a b. (a -> b) -> a -> b
$ String
"inactive Fedora branch: " String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
e
    Right Branch
br -> Branch
br

instance Show Branch where
  show :: Branch -> String
show Branch
Rawhide = String
"rawhide"
  show (Fedora Int
n) = String
"f" String -> String -> String
forall a. [a] -> [a] -> [a]
++ Int -> String
forall a. Show a => a -> String
show Int
n
  show (EPEL Int
n) = (if Int
n Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= Int
6 then String
"el" else String
"epel") String -> String -> String
forall a. [a] -> [a] -> [a]
++ Int -> String
forall a. Show a => a -> String
show Int
n
  show (EPELNext Int
n) = String
"epel" String -> String -> String
forall a. [a] -> [a] -> [a]
++ Int -> String
forall a. Show a => a -> String
show Int
n String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
"-next"

-- | Map Branch to Koji destination tag
branchDestTag :: Branch -> String
branchDestTag :: Branch -> String
branchDestTag Branch
Rawhide = String
"rawhide"
branchDestTag (Fedora Int
n) = Branch -> String
forall a. Show a => a -> String
show (Int -> Branch
Fedora Int
n) String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
"-updates-candidate"
branchDestTag (EPEL Int
n) = Branch -> String
forall a. Show a => a -> String
show (Int -> Branch
EPEL Int
n) String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
"-testing-candidate"
branchDestTag (EPELNext Int
n) = Branch -> String
forall a. Show a => a -> String
show (Int -> Branch
EPELNext Int
n) String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
"-testing-candidate"

-- | Default build target associated with a branch
branchTarget :: Branch -> String
branchTarget :: Branch -> String
branchTarget (Fedora Int
n) = Branch -> String
forall a. Show a => a -> String
show (Int -> Branch
Fedora Int
n)
branchTarget (EPEL Int
n) = Branch -> String
forall a. Show a => a -> String
show (Int -> Branch
EPEL Int
n)
branchTarget (EPELNext Int
n) = Branch -> String
forall a. Show a => a -> String
show (Int -> Branch
EPELNext Int
n)
branchTarget Branch
Rawhide = String
"rawhide"

--getLatestBranch :: IO Branch

-- | Returns newer branch than given one from supplied active branches.
--
-- Branches should be in descending order, eg from getFedoraBranches
newerBranch :: Branch -> [Branch] -> Branch
newerBranch :: Branch -> [Branch] -> Branch
newerBranch Branch
Rawhide [Branch]
_ = Branch
Rawhide
newerBranch (Fedora Int
n) [Branch]
branches =
  if Int -> Branch
Fedora Int
n Branch -> [Branch] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` [Branch]
branches
  then if Int -> Branch
Fedora (Int
nInt -> Int -> Int
forall a. Num a => a -> a -> a
+Int
1) Branch -> [Branch] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` [Branch]
branches
       then Int -> Branch
Fedora (Int
nInt -> Int -> Int
forall a. Num a => a -> a -> a
+Int
1)
       else Branch
Rawhide
  else String -> Branch
forall a. String -> a
error' (String -> Branch) -> String -> Branch
forall a b. (a -> b) -> a -> b
$ String
"Unsupported branch: " String -> String -> String
forall a. [a] -> [a] -> [a]
++ Branch -> String
forall a. Show a => a -> String
show (Int -> Branch
Fedora Int
n)
newerBranch (EPEL Int
n) [Branch]
branches =
  if Int -> Branch
EPEL Int
n Branch -> [Branch] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` [Branch]
branches
  then if Int -> Branch
EPEL (Int
nInt -> Int -> Int
forall a. Num a => a -> a -> a
+Int
1) Branch -> [Branch] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` [Branch]
branches
       then Int -> Branch
EPEL (Int
nInt -> Int -> Int
forall a. Num a => a -> a -> a
+Int
1)
       else Int -> Branch
EPEL Int
n
  else String -> Branch
forall a. String -> a
error' (String -> Branch) -> String -> Branch
forall a b. (a -> b) -> a -> b
$ String
"Unsupported branch: " String -> String -> String
forall a. [a] -> [a] -> [a]
++ Branch -> String
forall a. Show a => a -> String
show (Int -> Branch
EPEL Int
n)
newerBranch (EPELNext Int
n) [Branch]
branches =
  if Int -> Branch
EPELNext Int
n Branch -> [Branch] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` [Branch]
branches
  then if Int -> Branch
EPELNext (Int
nInt -> Int -> Int
forall a. Num a => a -> a -> a
+Int
1) Branch -> [Branch] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` [Branch]
branches
       then Int -> Branch
EPELNext (Int
nInt -> Int -> Int
forall a. Num a => a -> a -> a
+Int
1)
       else Int -> Branch
EPELNext Int
n
  else String -> Branch
forall a. String -> a
error' (String -> Branch) -> String -> Branch
forall a b. (a -> b) -> a -> b
$ String
"Unsupported branch: " String -> String -> String
forall a. [a] -> [a] -> [a]
++ Branch -> String
forall a. Show a => a -> String
show (Int -> Branch
EPELNext Int
n)

--olderBranch :: Branch -> Branch
--olderBranch Rawhide = latestBranch
--olderBranch (Fedora n) = Fedora (n-1)

-- | Returns list of active Fedora branches, including rawhide and EPEL
getFedoraBranches :: IO [Branch]
getFedoraBranches :: IO [Branch]
getFedoraBranches = do
  -- FIXME get these from Bodhi Releases?
  let epelnext :: [Branch]
epelnext = [Int -> Branch
EPELNext Int
8, Int -> Branch
EPELNext Int
9]
  [Branch]
brs <- (Text -> Branch) -> [Text] -> [Branch]
forall a b. (a -> b) -> [a] -> [b]
map Text -> Branch
releaseBranch ([Text] -> [Branch]) -> IO [Text] -> IO [Branch]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> IO [Text]
Dist.getReleaseIds
  [Branch] -> IO [Branch]
forall (m :: * -> *) a. Monad m => a -> m a
return ([Branch] -> IO [Branch]) -> [Branch] -> IO [Branch]
forall a b. (a -> b) -> a -> b
$ [Branch]
brs [Branch] -> [Branch] -> [Branch]
forall a. [a] -> [a] -> [a]
++ [Branch]
epelnext

-- | Maps release-id to Branch
releaseBranch :: T.Text -> Branch
releaseBranch :: Text -> Branch
releaseBranch Text
"fedora-rawhide" = Branch
Rawhide
releaseBranch Text
rel | Text
"fedora-" Text -> Text -> Bool
`T.isPrefixOf` Text
rel =
                      let (Text
_,String
ver) = Text -> String
T.unpack (Text -> String) -> (Text, Text) -> (Text, String)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Text -> Text -> (Text, Text)
T.breakOnEnd Text
"-" Text
rel in
                        if (Char -> Bool) -> String -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
all Char -> Bool
isDigit String
ver
                        then Int -> Branch
Fedora (Int -> Branch) -> Int -> Branch
forall a b. (a -> b) -> a -> b
$ String -> Int
forall a. Read a => String -> a
read String
ver
                        else String -> Branch
forall a. String -> a
error' (String -> Branch) -> String -> Branch
forall a b. (a -> b) -> a -> b
$ String
"Unsupport release: " String -> String -> String
forall a. [a] -> [a] -> [a]
++ Text -> String
T.unpack Text
rel
                  | Text
"epel-" Text -> Text -> Bool
`T.isPrefixOf` Text
rel =
                      let (Text
_,String
ver) = Text -> String
T.unpack (Text -> String) -> (Text, Text) -> (Text, String)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Text -> Text -> (Text, Text)
T.breakOnEnd Text
"-" Text
rel in
                        if (Char -> Bool) -> String -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
all Char -> Bool
isDigit String
ver
                        then Int -> Branch
EPEL (Int -> Branch) -> Int -> Branch
forall a b. (a -> b) -> a -> b
$ String -> Int
forall a. Read a => String -> a
read String
ver
                        else String -> Branch
forall a. String -> a
error' (String -> Branch) -> String -> Branch
forall a b. (a -> b) -> a -> b
$ String
"Unsupport release: " String -> String -> String
forall a. [a] -> [a] -> [a]
++ Text -> String
T.unpack Text
rel
                  | Bool
otherwise = String -> Branch
forall a. String -> a
error' (String -> Branch) -> String -> Branch
forall a b. (a -> b) -> a -> b
$ String
"Unsupport release: " String -> String -> String
forall a. [a] -> [a] -> [a]
++ Text -> String
T.unpack Text
rel

-- | Returns list of active Fedora branches, excluding rawhide
getFedoraBranched :: IO [Branch]
getFedoraBranched :: IO [Branch]
getFedoraBranched = (Branch -> Bool) -> [Branch] -> [Branch]
forall a. (a -> Bool) -> [a] -> [a]
filter (Branch -> Branch -> Bool
forall a. Eq a => a -> a -> Bool
/= Branch
Rawhide) ([Branch] -> [Branch]) -> IO [Branch] -> IO [Branch]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> IO [Branch]
getFedoraBranches

-- from simple-cmd
error' :: String -> a
#if (defined(MIN_VERSION_base) && MIN_VERSION_base(4,9,0))
error' :: String -> a
error' = String -> a
forall a. String -> a
errorWithoutStackTrace
#else
error' = error
#endif

-- | Convert a Branch to a Dist
branchDist :: Branch -> IO Dist.Dist
branchDist :: Branch -> IO Dist
branchDist (Fedora Int
n) = Dist -> IO Dist
forall (m :: * -> *) a. Monad m => a -> m a
return (Dist -> IO Dist) -> Dist -> IO Dist
forall a b. (a -> b) -> a -> b
$ Int -> Dist
Dist.Fedora Int
n
branchDist (EPEL Int
n) = Dist -> IO Dist
forall (m :: * -> *) a. Monad m => a -> m a
return (Dist -> IO Dist) -> Dist -> IO Dist
forall a b. (a -> b) -> a -> b
$ Int -> Dist
Dist.EPEL Int
n
branchDist (EPELNext Int
n) = Dist -> IO Dist
forall (m :: * -> *) a. Monad m => a -> m a
return (Dist -> IO Dist) -> Dist -> IO Dist
forall a b. (a -> b) -> a -> b
$ Int -> Dist
Dist.EPELNext Int
n
branchDist Branch
Rawhide = IO Dist
Dist.getRawhideDist