Skip to content

Commit 9c84f0f

Browse files
authored
Merge branch 'main' into i4k-release-v0.10.4
2 parents d97e462 + 6477da9 commit 9c84f0f

File tree

5 files changed

+56
-16
lines changed

5 files changed

+56
-16
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,11 @@ Given a version number `MAJOR.MINOR.PATCH`, we increment the:
2626

2727
- Add `tm_hclencode()` and `tm_hcldecode()` functions for encoding and decoding HCL content.
2828

29+
### Fixed
30+
31+
- Fix `go install` by removing a not needed `replace` directive in the `go.mod`.
32+
- Fix `git` URI normalization in case the project path component begins with a number.
33+
2934
## v0.10.3
3035

3136
### Changed

e2etests/cmd/helper/main.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import (
1717
"strings"
1818
"time"
1919

20+
"github.com/terramate-io/terramate/git"
2021
"github.com/terramate-io/terramate/project"
2122
"github.com/terramate-io/tfjson"
2223
"github.com/terramate-io/tfjson/sanitize"
@@ -69,6 +70,8 @@ func main() {
6970
tfPlanSanitize(os.Args[2])
7071
case "fibonacci":
7172
fibonacci()
73+
case "git-normalization":
74+
gitnorm(os.Args[2])
7275
default:
7376
log.Fatalf("unknown command %s", os.Args[1])
7477
}
@@ -222,6 +225,18 @@ func tfPlanSanitize(fname string) {
222225
fmt.Print(string(newPlanData))
223226
}
224227

228+
func gitnorm(rawURL string) {
229+
repo, err := git.NormalizeGitURI(rawURL)
230+
if err != nil {
231+
fmt.Fprintf(os.Stderr, "error: %v\n", err)
232+
os.Exit(1)
233+
}
234+
fmt.Printf("host: %s\n", repo.Host)
235+
fmt.Printf("owner: %s\n", repo.Owner)
236+
fmt.Printf("name: %s\n", repo.Name)
237+
fmt.Printf("normalized repository: %s\n", repo.Repo)
238+
}
239+
225240
func checkerr(err error) {
226241
if err != nil {
227242
fmt.Fprintf(os.Stderr, "%v\n", err)

git/url.go

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ import (
77
"net/url"
88
"os"
99
"strings"
10-
"unicode"
1110

1211
"github.com/terramate-io/terramate/errors"
12+
"github.com/terramate-io/terramate/strconv"
1313
)
1414

1515
// ErrInvalidGitURL indicates is an error kind indicating the git URL is not valid.
@@ -69,7 +69,12 @@ func NormalizeGitURI(raw string) (Repository, error) {
6969

7070
// IsURL tells if the u URL is a supported git remote URL.
7171
func IsURL(u string) bool {
72-
return strings.HasPrefix(u, "git@") || isSupportedProtocol(u)
72+
if strings.HasPrefix(u, "git@") || isSupportedProtocol(u) {
73+
return true
74+
}
75+
index := strings.Index(u, ":")
76+
// any other <schema>:// is not supported
77+
return index > 0 && !strings.HasPrefix(u[:index], "://")
7378
}
7479

7580
func isSupportedProtocol(u string) bool {
@@ -95,20 +100,26 @@ func ParseURL(rawURL string) (u *url.URL, err error) {
95100
// Not a Windows path.
96101
!strings.ContainsRune(rawURL, '\\') {
97102
// Support scp-like syntax for ssh protocol.
103+
// We convert SCP syntax into ssh://<uri>
104+
// Examples below:
105+
// [email protected]:some/path.git -> ssh://github.com/some/path.git
106+
// [email protected]:2222/some/path.git -> ssh://github.com:2222/some/path.git
98107
index := strings.Index(rawURL, ":")
99-
if index >= 0 && len(rawURL) > index && unicode.IsDigit(rune(rawURL[index+1])) {
100-
next := strings.Index(rawURL[index+1:], ":")
101-
if next >= 0 {
102-
index = index + 1 + next
103-
} else {
104-
index = -1
108+
if index > 0 {
109+
next := strings.Index(rawURL[index+1:], "/")
110+
if next > 0 {
111+
// check if port is present
112+
_, err := strconv.Atoi64(rawURL[index+1 : index+1+next])
113+
if err == nil {
114+
index = -1
115+
}
105116
}
106117
}
107-
if index != -1 {
108-
strRunes := []rune(rawURL)
118+
strRunes := []rune(rawURL)
119+
if index > 0 {
109120
strRunes[index] = '/'
110-
rawURL = "ssh://" + string(strRunes)
111121
}
122+
rawURL = "ssh://" + string(strRunes)
112123
}
113124
u, err = url.Parse(rawURL)
114125
if err != nil {

git/url_test.go

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,17 @@ func TestNormalizeGitURL(t *testing.T) {
8282
Name: "terramate",
8383
},
8484
},
85+
{
86+
name: "github url with org name starting with number",
87+
raw: "[email protected]:123terramate-io/terramate.git",
88+
normalized: git.Repository{
89+
RawURL: "[email protected]:123terramate-io/terramate.git",
90+
Repo: "github.com/123terramate-io/terramate",
91+
Host: "github.com",
92+
Owner: "123terramate-io",
93+
Name: "terramate",
94+
},
95+
},
8596
{
8697
name: "basic gitlab ssh url",
8798
raw: "[email protected]:terramate-io/terramate.git",
@@ -162,9 +173,9 @@ func TestNormalizeGitURL(t *testing.T) {
162173
},
163174
{
164175
name: "vcs provider URL with port",
165-
raw: "[email protected]:8888:terramate-io/terramate.git",
176+
raw: "[email protected]:8888/terramate-io/terramate.git",
166177
normalized: git.Repository{
167-
RawURL: "[email protected]:8888:terramate-io/terramate.git",
178+
RawURL: "[email protected]:8888/terramate-io/terramate.git",
168179
Repo: "github.com:8888/terramate-io/terramate",
169180
Host: "github.com:8888",
170181
Owner: "terramate-io",
@@ -223,7 +234,7 @@ func TestIsURL(t *testing.T) {
223234
{
224235
name: "scp-like with no user",
225236
url: "example.com:owner/repo",
226-
want: false,
237+
want: true,
227238
},
228239
{
229240
name: "ssh",

go.mod

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -203,5 +203,3 @@ require (
203203
golang.org/x/sys v0.21.0
204204
golang.org/x/text v0.16.0 // indirect
205205
)
206-
207-
replace github.com/hashicorp/hcl/v2 v2.20.1 => github.com/opentofu/hcl/v2 v2.0.0-20240814143621-8048794c5c52

0 commit comments

Comments
 (0)