-
-
Notifications
You must be signed in to change notification settings - Fork 393
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixed unit test and url parsing in lib install via git
Previously go-git accepted urls in the format: https://github.com/author/repo#ref but now it refuses to fetch if the "#ref" suffix is present. The new parsing utility returns the URL cleaned up of the reference.
- Loading branch information
Showing
2 changed files
with
79 additions
and
74 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -201,8 +201,8 @@ func (lmi *Installer) InstallZipLib(ctx context.Context, archivePath *paths.Path | |
} | ||
|
||
// InstallGitLib installs a library hosted on a git repository on the specified path. | ||
func (lmi *Installer) InstallGitLib(gitURL string, overwrite bool) error { | ||
gitLibraryName, ref, err := parseGitURL(gitURL) | ||
func (lmi *Installer) InstallGitLib(argURL string, overwrite bool) error { | ||
libraryName, gitURL, ref, err := parseGitArgURL(argURL) | ||
if err != nil { | ||
return err | ||
} | ||
|
@@ -213,7 +213,7 @@ func (lmi *Installer) InstallGitLib(gitURL string, overwrite bool) error { | |
return err | ||
} | ||
defer tmp.RemoveAll() | ||
tmpInstallPath := tmp.Join(gitLibraryName) | ||
tmpInstallPath := tmp.Join(libraryName) | ||
|
||
depth := 1 | ||
if ref != "" { | ||
|
@@ -225,7 +225,7 @@ func (lmi *Installer) InstallGitLib(gitURL string, overwrite bool) error { | |
Progress: os.Stdout, | ||
}) | ||
if err != nil { | ||
return err | ||
return fmt.Errorf("error cloning git repo: %w", err) | ||
} | ||
|
||
if ref != "" { | ||
|
@@ -249,25 +249,39 @@ func (lmi *Installer) InstallGitLib(gitURL string, overwrite bool) error { | |
return nil | ||
} | ||
|
||
// parseGitURL tries to recover a library name from a git URL. | ||
// parseGitArgURL tries to recover a library name from a git URL. | ||
// Returns an error in case the URL is not a valid git URL. | ||
func parseGitURL(gitURL string) (string, plumbing.Revision, error) { | ||
var res string | ||
var rev plumbing.Revision | ||
if strings.HasPrefix(gitURL, "git@") { | ||
func parseGitArgURL(argURL string) (string, string, plumbing.Revision, error) { | ||
// Handle github-specific address in the form "[email protected]:arduino-libraries/SigFox.git" | ||
if strings.HasPrefix(argURL, "[email protected]:") { | ||
// We can't parse these as URLs | ||
i := strings.LastIndex(gitURL, "/") | ||
res = strings.TrimSuffix(gitURL[i+1:], ".git") | ||
} else if path := paths.New(gitURL); path != nil && path.Exist() { | ||
res = path.Base() | ||
} else if parsed, err := url.Parse(gitURL); parsed.String() != "" && err == nil { | ||
i := strings.LastIndex(parsed.Path, "/") | ||
res = strings.TrimSuffix(parsed.Path[i+1:], ".git") | ||
rev = plumbing.Revision(parsed.Fragment) | ||
} else { | ||
return "", "", errors.New(i18n.Tr("invalid git url")) | ||
} | ||
return res, rev, nil | ||
argURL = "https://github.com/" + strings.TrimPrefix(argURL, "[email protected]:") | ||
} | ||
|
||
parsedURL, err := url.Parse(argURL) | ||
if err != nil { | ||
return "", "", "", fmt.Errorf("%s: %w", i18n.Tr("invalid git url"), err) | ||
} | ||
if parsedURL.String() == "" { | ||
return "", "", "", errors.New(i18n.Tr("invalid git url")) | ||
} | ||
|
||
// Extract lib name from "https://github.com/arduino-libraries/SigFox.git#1.0.3" | ||
// path == "/arduino-libraries/SigFox.git" | ||
slash := strings.LastIndex(parsedURL.Path, "/") | ||
if slash == -1 { | ||
return "", "", "", errors.New(i18n.Tr("invalid git url")) | ||
} | ||
libName := strings.TrimSuffix(parsedURL.Path[slash+1:], ".git") | ||
if libName == "" { | ||
return "", "", "", errors.New(i18n.Tr("invalid git url")) | ||
} | ||
// fragment == "1.0.3" | ||
rev := plumbing.Revision(parsedURL.Fragment) | ||
// gitURL == "https://github.com/arduino-libraries/SigFox.git" | ||
parsedURL.Fragment = "" | ||
gitURL := parsedURL.String() | ||
return libName, gitURL, rev, nil | ||
} | ||
|
||
// validateLibrary verifies the dir contains a valid library, meaning it has either | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,59 +23,50 @@ import ( | |
) | ||
|
||
func TestParseGitURL(t *testing.T) { | ||
gitURL := "" | ||
libraryName, ref, err := parseGitURL(gitURL) | ||
require.Equal(t, "", libraryName) | ||
require.EqualValues(t, "", ref) | ||
require.Errorf(t, err, "invalid git url") | ||
|
||
gitURL = "https://github.com/arduino/arduino-lib.git" | ||
libraryName, ref, err = parseGitURL(gitURL) | ||
require.Equal(t, "arduino-lib", libraryName) | ||
require.EqualValues(t, "", ref) | ||
require.NoError(t, err) | ||
|
||
gitURL = "https://github.com/arduino/arduino-lib.git#0.1.2" | ||
libraryName, ref, err = parseGitURL(gitURL) | ||
require.Equal(t, "arduino-lib", libraryName) | ||
require.EqualValues(t, "0.1.2", ref) | ||
require.NoError(t, err) | ||
|
||
gitURL = "[email protected]:arduino/arduino-lib.git" | ||
libraryName, ref, err = parseGitURL(gitURL) | ||
require.Equal(t, "arduino-lib", libraryName) | ||
require.EqualValues(t, "", ref) | ||
require.NoError(t, err) | ||
|
||
gitURL = "file:///path/to/arduino-lib" | ||
libraryName, ref, err = parseGitURL(gitURL) | ||
require.Equal(t, "arduino-lib", libraryName) | ||
require.EqualValues(t, "", ref) | ||
require.NoError(t, err) | ||
|
||
gitURL = "file:///path/to/arduino-lib.git" | ||
libraryName, ref, err = parseGitURL(gitURL) | ||
require.Equal(t, "arduino-lib", libraryName) | ||
require.EqualValues(t, "", ref) | ||
require.NoError(t, err) | ||
|
||
gitURL = "/path/to/arduino-lib" | ||
libraryName, ref, err = parseGitURL(gitURL) | ||
require.Equal(t, "arduino-lib", libraryName) | ||
require.EqualValues(t, "", ref) | ||
require.NoError(t, err) | ||
|
||
gitURL = "/path/to/arduino-lib.git" | ||
libraryName, ref, err = parseGitURL(gitURL) | ||
require.Equal(t, "arduino-lib", libraryName) | ||
require.EqualValues(t, "", ref) | ||
require.NoError(t, err) | ||
|
||
gitURL = "file:///path/to/arduino-lib" | ||
libraryName, ref, err = parseGitURL(gitURL) | ||
require.Equal(t, "arduino-lib", libraryName) | ||
require.EqualValues(t, "", ref) | ||
require.NoError(t, err) | ||
{ | ||
_, _, _, err := parseGitArgURL("") | ||
require.EqualError(t, err, "invalid git url") | ||
} | ||
{ | ||
libraryName, gitURL, ref, err := parseGitArgURL("https://github.com/arduino/arduino-lib.git") | ||
require.NoError(t, err) | ||
require.Equal(t, "arduino-lib", libraryName) | ||
require.Equal(t, "https://github.com/arduino/arduino-lib.git", gitURL) | ||
require.EqualValues(t, "", ref) | ||
} | ||
{ | ||
libraryName, gitURL, ref, err := parseGitArgURL("https://github.com/arduino/arduino-lib.git#0.1.2") | ||
require.NoError(t, err) | ||
require.Equal(t, "arduino-lib", libraryName) | ||
require.Equal(t, "https://github.com/arduino/arduino-lib.git", gitURL) | ||
require.EqualValues(t, "0.1.2", ref) | ||
} | ||
{ | ||
libraryName, gitURL, ref, err := parseGitArgURL("[email protected]:arduino/arduino-lib.git") | ||
require.NoError(t, err) | ||
require.Equal(t, "arduino-lib", libraryName) | ||
require.Equal(t, "https://github.com/arduino/arduino-lib.git", gitURL) | ||
require.EqualValues(t, "", ref) | ||
} | ||
{ | ||
libraryName, gitURL, ref, err := parseGitArgURL("[email protected]:arduino/arduino-lib.git#0.1.2") | ||
require.NoError(t, err) | ||
require.Equal(t, "arduino-lib", libraryName) | ||
require.Equal(t, "https://github.com/arduino/arduino-lib.git", gitURL) | ||
require.EqualValues(t, "0.1.2", ref) | ||
} | ||
{ | ||
_, _, _, err := parseGitArgURL("https://arduino.cc") | ||
require.EqualError(t, err, "invalid git url") | ||
} | ||
{ | ||
_, _, _, err := parseGitArgURL("https://arduino.cc/") | ||
require.EqualError(t, err, "invalid git url") | ||
} | ||
{ | ||
_, _, _, err := parseGitArgURL("://not@a@url") | ||
require.EqualError(t, err, "invalid git url: parse \"://not@a@url\": missing protocol scheme") | ||
} | ||
} | ||
|
||
func TestValidateLibrary(t *testing.T) { | ||
|