Skip to content

Commit

Permalink
hsec-sync: refactor git code to ExceptT for better readability
Browse files Browse the repository at this point in the history
  • Loading branch information
blackheaven committed Mar 29, 2024
1 parent c49d316 commit 7fbbee2
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 25 deletions.
1 change: 1 addition & 0 deletions code/hsec-sync/hsec-sync.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ library
, process >=1.6 && <1.7
, text >=1.2 && <3
, time >=1.9 && <1.14
, transformers >=0.5 && <0.7
, wreq >=0.5 && <0.6

hs-source-dirs: src
Expand Down
59 changes: 34 additions & 25 deletions code/hsec-sync/src/Security/Advisories/Sync/Git.hs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ module Security.Advisories.Sync.Git
)
where

import Control.Monad (when)
import Control.Monad.IO.Class (liftIO)
import Control.Monad.Trans.Except (runExceptT, throwE)
import Data.Time (ZonedTime)
import Data.Time.Format.ISO8601 (iso8601ParseM)
import qualified System.Directory as D
Expand Down Expand Up @@ -115,31 +118,37 @@ ensureGitRepositoryWithRemote repo =

updateGitRepository :: Repository -> IO (Either GitError ())
updateGitRepository repo =
D.withCurrentDirectory (repositoryRoot repo) $ do
_ <- runGit ["remote", "add", "origin", repositoryUrl repo] -- can fail if it exists
(setUrlCmd, setUrlStatus, setUrlStdout, setUrlStderr) <-
runGit ["remote", "set-url", "origin", repositoryUrl repo]
if setUrlStatus /= ExitSuccess
then return $ Left $ GitError setUrlCmd $ GitProcessError setUrlStatus setUrlStdout setUrlStderr
else do
(fetchAllCmd, fetchAllStatus, fetchAllStdout, fetchAllStderr) <-
runGit ["fetch", "--all"]
if fetchAllStatus /= ExitSuccess
then
return $ Left $ GitError fetchAllCmd $ GitProcessError fetchAllStatus fetchAllStdout fetchAllStderr
else do
(checkoutBranchCmd, checkoutBranchStatus, checkoutBranchStdout, checkoutBranchStderr) <-
runGit ["checkout", repositoryBranch repo]
if checkoutBranchStatus /= ExitSuccess
then
return $ Left $ GitError checkoutBranchCmd $ GitProcessError checkoutBranchStatus checkoutBranchStdout checkoutBranchStderr
else do
(resetCmd, resetStatus, resetStdout, resetStderr) <-
runGit ["reset", "--hard", "origin/" <> repositoryBranch repo]
return $
if resetStatus /= ExitSuccess
then Left $ GitError resetCmd $ GitProcessError resetStatus resetStdout resetStderr
else Right ()
D.withCurrentDirectory (repositoryRoot repo) $
runExceptT $ do
_ <- liftIO $ runGit ["remote", "add", "origin", repositoryUrl repo] -- can fail if it exists
(setUrlCmd, setUrlStatus, setUrlStdout, setUrlStderr) <-
liftIO $ runGit ["remote", "set-url", "origin", repositoryUrl repo]
when (setUrlStatus /= ExitSuccess) $
throwE $
GitError setUrlCmd $
GitProcessError setUrlStatus setUrlStdout setUrlStderr

(fetchAllCmd, fetchAllStatus, fetchAllStdout, fetchAllStderr) <-
liftIO $ runGit ["fetch", "--all"]
when (fetchAllStatus /= ExitSuccess) $
throwE $
GitError fetchAllCmd $
GitProcessError fetchAllStatus fetchAllStdout fetchAllStderr

(checkoutBranchCmd, checkoutBranchStatus, checkoutBranchStdout, checkoutBranchStderr) <-
liftIO $ runGit ["checkout", repositoryBranch repo]
when (checkoutBranchStatus /= ExitSuccess) $
throwE $
GitError checkoutBranchCmd $
GitProcessError checkoutBranchStatus checkoutBranchStdout checkoutBranchStderr

(resetCmd, resetStatus, resetStdout, resetStderr) <-
liftIO $ runGit ["reset", "--hard", "origin/" <> repositoryBranch repo]

when (resetStatus /= ExitSuccess) $
throwE $
GitError resetCmd $
GitProcessError resetStatus resetStdout resetStderr

newtype GitDirectoryInfo = GitDirectoryInfo
{ lastModificationCommitDate :: ZonedTime
Expand Down

0 comments on commit 7fbbee2

Please sign in to comment.