-
-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathGit.gren
More file actions
270 lines (240 loc) · 8.72 KB
/
Git.gren
File metadata and controls
270 lines (240 loc) · 8.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
module Git exposing
( Error(..)
, report
, clonePackage
, clonePackageCached
, fetchLatestVersion
, fetchVersions
, hasLocalTag
, hasNoLocalChangesSinceTag
)
import Bytes
import ChildProcess
import FileSystem
import FileSystem.Path as Path exposing (Path)
import Compiler.PackageName as PackageName exposing (PackageName)
import SemanticVersion exposing (SemanticVersion)
import Task exposing (Task)
import Terminal.Help as Help
import CLI.PrettyPrinter as PP
import Compiler.Paths
import Terminal.Report as Report exposing (Report)
type Error
= NoVersions
| NoSuchRepo
| NoSuchRepoOrVersion SemanticVersion
| FailedCommand { args : Array String, message : String }
report : String -> String -> Error -> Report
report title context err =
when err is
NoVersions ->
Report.create
title
Nothing
( PP.verticalBlock
[ PP.words (context ++ ", but I couldn't find any semver compatible tags in this repo.")
, PP.empty
, PP.words
"""
Gren packages are just git repositories with tags following the
semantic versioning scheme. However, it seems that this particular repo
doesn't have _any_ semantic version tags!
"""
]
)
NoSuchRepo ->
Report.create
title
Nothing
( PP.verticalBlock
[ PP.words (context ++ ", but I couldn't find the repo on github.")
, PP.empty
, PP.words
"""
Gren packages are just git repositories hosted on github, however
it seems like this repo doesn't exist.
"""
]
)
NoSuchRepoOrVersion vsn ->
Report.create
title
Nothing
( PP.verticalBlock
[ PP.words <| context ++ ", but I couldn't find the correct version of this package on github."
, PP.empty
, PP.words <|
"Gren packages are just git repositories hosted on github with semver formatted tags. However, it seems like this package, or version "
++ SemanticVersion.toString vsn
++ ", doesn't exist."
]
)
FailedCommand { args, message } ->
Report.create
title
Nothing
( PP.verticalBlock
[ PP.words (context ++ ", so I tried to execute:")
, PP.empty
, PP.words (String.join " " args)
|> PP.indent
, PP.empty
, PP.words "But it returned the following error message:"
, PP.empty
, PP.words message
|> PP.indent
]
)
clonePackage : ChildProcess.Permission -> Path -> PackageName -> SemanticVersion -> Task Error ChildProcess.SuccessfulRun
clonePackage cpPerm repo name version =
let
githubUrl =
"https://github.com/" ++ PackageName.toString name ++ ".git"
args =
[ "clone"
, "--branch"
, SemanticVersion.toString version
, "--depth"
, "1"
, githubUrl
, Path.toPosixString repo
]
in
ChildProcess.run
cpPerm
"git"
args
{ shell = ChildProcess.NoShell
, workingDirectory = ChildProcess.InheritWorkingDirectory
, environmentVariables = ChildProcess.InheritEnvironmentVariables
, maximumBytesWrittenToStreams = 4096
, runDuration = ChildProcess.Milliseconds 30000
}
|> Task.mapError
(\details ->
if details.exitCode == 128 then
NoSuchRepoOrVersion version
else
constructFailedCommand args details
)
clonePackageCached :
{ childProcessPermission : ChildProcess.Permission
, fileSystemPermission : FileSystem.Permission
, cacheRoot : Compiler.Paths.CacheRoot
, packageName : PackageName
, version : SemanticVersion
}
-> Task Error Path
clonePackageCached { childProcessPermission, fileSystemPermission, cacheRoot, packageName, version } =
let
packageDir =
PackageName.toString packageName ++ "__" ++ SemanticVersion.toString version
|> String.replace "/" "_"
|> String.replace "." "_"
|> Path.fromPosixString
packageSourcesDir =
Compiler.Paths.packageSources cacheRoot
repoDir =
Path.prepend packageSourcesDir packageDir
in
FileSystem.checkAccess fileSystemPermission [] repoDir
|> Task.map (\_ -> repoDir)
|> Task.onError
(\_ ->
clonePackage childProcessPermission repoDir packageName version
|> Task.map (\_ -> repoDir)
)
constructFailedCommand : Array String -> ChildProcess.FailedRun -> Error
constructFailedCommand args details =
FailedCommand
{ args = Array.pushFirst "git" args
, message = Maybe.withDefault "" (Bytes.toString details.stderr)
}
fetchLatestVersion : ChildProcess.Permission -> PackageName -> Task Error SemanticVersion
fetchLatestVersion cpPerm name =
fetchVersions cpPerm name
|> Task.andThen
(\vsns ->
when Array.last vsns is
Nothing ->
Task.fail NoVersions
Just v ->
Task.succeed v
)
fetchVersions : ChildProcess.Permission -> PackageName -> Task Error (Array SemanticVersion)
fetchVersions cpPerm name =
let
githubUrl =
"https://github.com/" ++ PackageName.toString name ++ ".git"
args =
[ "ls-remote"
, "--tags"
, githubUrl
]
in
ChildProcess.run
cpPerm
"git"
args
{ shell = ChildProcess.NoShell
, workingDirectory = ChildProcess.InheritWorkingDirectory
, environmentVariables = ChildProcess.InheritEnvironmentVariables
, maximumBytesWrittenToStreams = 4096
, runDuration = ChildProcess.Milliseconds 30000
}
|> Task.mapError
(\details ->
if details.exitCode == 128 then
NoSuchRepo
else
constructFailedCommand args details
)
|> Task.andThen
(\result ->
when Bytes.toString result.stdout is
Nothing ->
Task.fail NoVersions
Just tagList ->
-- format: commit hash \t refs/tags/<tag>
tagList
|> String.split "\n"
|> Array.map (String.split "/" >> Array.last >> Maybe.withDefault "")
|> Array.mapAndKeepJust SemanticVersion.fromString
|> Array.sortWith SemanticVersion.compare
|> (\vsns ->
if Array.isEmpty vsns then
Task.fail NoVersions
else
Task.succeed vsns
)
)
hasLocalTag : ChildProcess.Permission -> SemanticVersion -> Task Error {}
hasLocalTag permission vsn =
let
args =
[ "show", "--name-only", SemanticVersion.toString vsn ]
in
ChildProcess.run permission "git" args
{ shell = ChildProcess.NoShell
, workingDirectory = ChildProcess.InheritWorkingDirectory
, environmentVariables = ChildProcess.InheritEnvironmentVariables
, maximumBytesWrittenToStreams = 4096
, runDuration = ChildProcess.Milliseconds 5000
}
|> Task.mapError (\details -> constructFailedCommand args details)
|> Task.map (\_ -> {})
hasNoLocalChangesSinceTag : ChildProcess.Permission -> SemanticVersion -> Task Error {}
hasNoLocalChangesSinceTag permission vsn =
let
args =
[ "diff-index", "--quiet", SemanticVersion.toString vsn ]
in
ChildProcess.run permission "git" args
{ shell = ChildProcess.NoShell
, workingDirectory = ChildProcess.InheritWorkingDirectory
, environmentVariables = ChildProcess.InheritEnvironmentVariables
, maximumBytesWrittenToStreams = 4096
, runDuration = ChildProcess.Milliseconds 5000
}
|> Task.mapError (\details -> constructFailedCommand args details)
|> Task.map (\_ -> {})