Skip to content

Commit 95d1a98

Browse files
authored
sourceaddrs: support query paths in github and gitlab addresses (#95)
1 parent 5dc4856 commit 95d1a98

File tree

2 files changed

+42
-4
lines changed

2 files changed

+42
-4
lines changed

sourceaddrs/source_remote.go

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,12 @@ var remoteSourceShorthands = []remoteSourceShorthand{
162162
return "", false, nil
163163
}
164164

165-
parts := strings.Split(given, "/")
165+
url, query, _ := strings.Cut(given, "?")
166+
if len(query) > 0 {
167+
query = "?" + query
168+
}
169+
170+
parts := strings.Split(url, "/")
166171
if len(parts) < 3 {
167172
return "", false, fmt.Errorf("GitHub.com shorthand addresses must start with github.com/organization/repository")
168173
}
@@ -178,7 +183,7 @@ var remoteSourceShorthands = []remoteSourceShorthand{
178183
urlStr += "//" + strings.Join(parts[3:], "/")
179184
}
180185

181-
return "git::" + urlStr, true, nil
186+
return fmt.Sprintf("git::%s%s", urlStr, query), true, nil
182187
},
183188
func(given string) (string, bool, error) {
184189
// Allows a gitlab.com repository to be presented in a scheme-less
@@ -195,7 +200,12 @@ var remoteSourceShorthands = []remoteSourceShorthand{
195200
return "", false, nil
196201
}
197202

198-
parts := strings.Split(given, "/")
203+
url, query, _ := strings.Cut(given, "?")
204+
if len(query) > 0 {
205+
query = "?" + query
206+
}
207+
208+
parts := strings.Split(url, "/")
199209
if len(parts) < 3 {
200210
return "", false, fmt.Errorf("GitLab.com shorthand addresses must start with gitlab.com/organization/repository")
201211
}
@@ -217,7 +227,7 @@ var remoteSourceShorthands = []remoteSourceShorthand{
217227
// refer to a Git repository.
218228
}
219229

220-
return "git::" + urlStr, true, nil
230+
return fmt.Sprintf("git::%s%s", urlStr, query), true, nil
221231
},
222232
}
223233

sourceaddrs/source_test.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,25 @@ func TestParseSource(t *testing.T) {
241241
subPath: "bleep",
242242
},
243243
},
244+
{
245+
Given: "github.com/hashicorp/stacks?ref=v4.8.0",
246+
Want: RemoteSource{
247+
pkg: RemotePackage{
248+
sourceType: "git",
249+
url: *mustParseURL("https://github.com/hashicorp/stacks.git?ref=v4.8.0"),
250+
},
251+
},
252+
},
253+
{
254+
Given: "github.com/hashicorp/stacks/bleep?ref=v4.8.0",
255+
Want: RemoteSource{
256+
pkg: RemotePackage{
257+
sourceType: "git",
258+
url: *mustParseURL("https://github.com/hashicorp/stacks.git?ref=v4.8.0"),
259+
},
260+
subPath: "bleep",
261+
},
262+
},
244263
{
245264
Given: "gitlab.com/hashicorp/go-slug.git",
246265
Want: RemoteSource{
@@ -259,6 +278,15 @@ func TestParseSource(t *testing.T) {
259278
},
260279
},
261280
},
281+
{
282+
Given: "gitlab.com/hashicorp/stacks?ref=v4.8.0",
283+
Want: RemoteSource{
284+
pkg: RemotePackage{
285+
sourceType: "git",
286+
url: *mustParseURL("https://gitlab.com/hashicorp/stacks.git?ref=v4.8.0"),
287+
},
288+
},
289+
},
262290
{
263291
Given: "gitlab.com/hashicorp/go-slug/bleep",
264292
// NOTE: gitlab.com _also_ hosts a Terraform Module registry, and so

0 commit comments

Comments
 (0)