@@ -4,10 +4,12 @@ module Spago.Git
44 , fetchRepo
55 , getGit
66 , getRef
7- , listTags
7+ , getRemotes
88 , getStatus
9- , pushTag
109 , isIgnored
10+ , listTags
11+ , parseRemote
12+ , pushTag
1113 , tagCheckedOut
1214 ) where
1315
@@ -16,11 +18,15 @@ import Spago.Prelude
1618import Control.Monad.Except (ExceptT (..))
1719import Control.Monad.Except as Except
1820import Data.Array as Array
21+ import Data.Array.NonEmpty as NEA
22+ import Data.Maybe (fromJust )
1923import Data.String (Pattern (..))
2024import Data.String as String
25+ import Data.String.Regex as Regex
2126import Node.ChildProcess.Types (Exit (..))
2227import Node.Path as Path
2328import Node.Process as Process
29+ import Partial.Unsafe (unsafePartial )
2430import Registry.Version as Version
2531import Spago.Cmd as Cmd
2632import Spago.FS as FS
@@ -29,6 +35,8 @@ type Git = { cmd :: String, version :: String }
2935
3036type GitEnv a = { git :: Git , logOptions :: LogOptions , offline :: OnlineStatus | a }
3137
38+ type Remote = { name :: String , url :: String , owner :: String , repo :: String }
39+
3240runGit_ :: forall a . Array String -> Maybe FilePath -> ExceptT String (Spago (GitEnv a )) Unit
3341runGit_ args cwd = void $ runGit args cwd
3442
@@ -112,6 +120,22 @@ getRef cwd = do
112120 ]
113121 Right r -> pure $ Right r.stdout
114122
123+ getRemotes :: forall a . Maybe FilePath -> Spago (GitEnv a ) (Either Docc (Array Remote ))
124+ getRemotes = \cwd -> do
125+ let opts = Cmd .defaultExecOptions { pipeStdout = false , pipeStderr = false , cwd = cwd }
126+ { git } <- ask
127+ Cmd .exec git.cmd [ " remote" , " --verbose" ] opts <#> case _ of
128+ Left r -> Left $ toDoc
129+ [ " Could not run `git remote --verbose` to verify correct repository path. Error:"
130+ , r.stderr
131+ ]
132+ Right { stdout: " " } ->
133+ pure []
134+ Right r ->
135+ r.stdout # String .split (Pattern " \n " ) # Array .mapMaybe parseRemote # case _ of
136+ [] -> Left $ toDoc " Could not parse any remotes from the output of `git remote --verbose`."
137+ remotes -> Right $ Array .nub remotes
138+
115139tagCheckedOut :: forall a . Maybe FilePath -> Spago (GitEnv a ) (Either Docc String )
116140tagCheckedOut cwd = do
117141 let opts = Cmd .defaultExecOptions { pipeStdout = false , pipeStderr = false , cwd = cwd }
@@ -177,3 +201,16 @@ getGit = do
177201 Left r -> do
178202 logDebug $ Cmd .printExecResult r
179203 die [ " Failed to find git. Have you installed it, and is it in your PATH?" ]
204+
205+ parseRemote :: String -> Maybe Remote
206+ parseRemote = \line ->
207+ case String .split (Pattern " \t " ) line of
208+ [ name, secondPart ]
209+ | [ url, _ ] <- String .split (Pattern " " ) secondPart
210+ , Just [ _, _, Just owner, Just repo ] <- NEA .toArray <$> Regex .match gitUrlRegex url ->
211+ Just { name, url, owner, repo }
212+ _ ->
213+ Nothing
214+ where
215+ gitUrlRegex = unsafePartial $ fromJust $ hush $
216+ Regex .regex " ^(.+@.+:|https?:\\ /\\ /.+\\ /)(.*)\\ /(.+)\\ .git$" mempty
0 commit comments