Skip to content

Commit

Permalink
Consolidate functions in libs/git (#652)
Browse files Browse the repository at this point in the history
## Changes

The functions in `libs/git/git.go` assumed global state (e.g. working
directory) and were no longer used.

This change consolidates the functionality to turn an origin URL into an
HTTPS URL.

Closes #187.

## Tests

Expanded existing unit test.
  • Loading branch information
pietern authored Aug 10, 2023
1 parent 979b680 commit 2a58253
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 117 deletions.
80 changes: 0 additions & 80 deletions libs/git/git.go

This file was deleted.

22 changes: 0 additions & 22 deletions libs/git/git_test.go

This file was deleted.

24 changes: 12 additions & 12 deletions libs/git/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,23 @@ import (
giturls "github.com/whilp/git-urls"
)

// Return an origin URL as an HTTPS URL.
// The transformations in this function are not guaranteed to work for all
// Git providers. They are only guaranteed to work for GitHub.
func ToHttpsUrl(url string) (string, error) {
originUrl, err := giturls.Parse(url)
origin, err := giturls.Parse(url)
if err != nil {
return "", err
}
if originUrl.Scheme == "https" {
return originUrl.String(), nil
// If this repository is checked out over SSH
if origin.Scheme != "https" {
origin.Scheme = "https"
}
// if current repo is checked out with a SSH key
if originUrl.Scheme != "https" {
originUrl.Scheme = "https"
}
// `git@` is not required for HTTPS
if originUrl.User != nil {
originUrl.User = nil
// Basic auth is not applicable for an HTTPS URL.
if origin.User != nil {
origin.User = nil
}
// Remove `.git` suffix, if present.
originUrl.Path = strings.TrimSuffix(originUrl.Path, ".git")
return originUrl.String(), nil
origin.Path = strings.TrimSuffix(origin.Path, ".git")
return origin.String(), nil
}
15 changes: 12 additions & 3 deletions libs/git/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,16 @@ import (
)

func TestToHttpsUrlForSsh(t *testing.T) {
url, err := ToHttpsUrl("user@foo.com:org/repo-name.git")
assert.NoError(t, err)
assert.Equal(t, "https://foo.com/org/repo-name", url)
for _, e := range []struct {
url string
expected string
}{
{"user@foo.com:org/repo-name.git", "https://foo.com/org/repo-name"},
{"git@github.com:databricks/cli.git", "https://github.com/databricks/cli"},
{"https://github.com/databricks/cli.git", "https://github.com/databricks/cli"},
} {
url, err := ToHttpsUrl(e.url)
assert.NoError(t, err)
assert.Equal(t, e.expected, url)
}
}

0 comments on commit 2a58253

Please sign in to comment.