Unverified Commit ee076dd1 authored by Silvano Cerza's avatar Silvano Cerza Committed by GitHub

Fix lib install with git url (#1143)

* Fix lib install with git url

* Better git url handling
parent c6be6fa4
......@@ -19,6 +19,7 @@ import (
"context"
"errors"
"fmt"
"net/url"
"os"
"strings"
......@@ -111,17 +112,21 @@ func (lm *LibrariesManager) InstallZipLib(ctx context.Context, archivePath strin
}
//InstallGitLib installs a library hosted on a git repository on the specified path.
func (lm *LibrariesManager) InstallGitLib(url string) error {
func (lm *LibrariesManager) InstallGitLib(gitURL string) error {
libsDir := lm.getUserLibrariesDir()
if libsDir == nil {
return fmt.Errorf("User directory not set")
}
i := strings.LastIndex(url, "/")
folder := strings.TrimRight(url[i+1:], ".git")
path := libsDir.Join(folder)
_, err := git.PlainClone(path.String(), false, &git.CloneOptions{
URL: url,
libraryName, err := parseGitURL(gitURL)
if err != nil {
return err
}
installPath := libsDir.Join(libraryName)
_, err = git.PlainClone(installPath.String(), false, &git.CloneOptions{
URL: gitURL,
Progress: os.Stdout,
})
if err != nil {
......@@ -129,3 +134,20 @@ func (lm *LibrariesManager) InstallGitLib(url string) error {
}
return nil
}
func parseGitURL(gitURL string) (string, error) {
var res string
if strings.HasPrefix(gitURL, "git@") {
// We can't parse these as URLs
i := strings.LastIndex(gitURL, "/")
res = strings.TrimRight(gitURL[i+1:], ".git")
} else if path := paths.New(gitURL); path.Exist() {
res = path.Base()
} else if parsed, err := url.Parse(gitURL); err == nil {
i := strings.LastIndex(parsed.Path, "/")
res = strings.TrimRight(parsed.Path[i+1:], ".git")
} else {
return "", fmt.Errorf("invalid git url")
}
return res, nil
}
......@@ -29,6 +29,7 @@ import (
"github.com/arduino/arduino-cli/commands/lib"
"github.com/arduino/arduino-cli/configuration"
rpc "github.com/arduino/arduino-cli/rpc/commands"
"github.com/arduino/go-paths-helper"
"github.com/spf13/cobra"
)
......@@ -85,9 +86,18 @@ func runInstallCommand(cmd *cobra.Command, args []string) {
}
if installFlags.gitURL {
url := args[0]
if url == "." {
wd, err := paths.Getwd()
if err != nil {
feedback.Errorf("Couldn't get current working directory: %v", err)
os.Exit(errorcodes.ErrGeneric)
}
url = wd.String()
}
gitlibraryInstallReq := &rpc.GitLibraryInstallReq{
Instance: instance,
Url: args[0],
Url: url,
}
err := lib.GitLibraryInstall(context.Background(), gitlibraryInstallReq, output.TaskProgress())
if err != nil {
......
This diff is collapsed.
......@@ -20,6 +20,7 @@ black = { version = "^19.10b0", allow-prereleases = true }
filelock = "^3.0.12"
pytest-xdist = "^2.1.0"
pytest_httpserver = "^0.3.5"
GitPython = "^3.1.12"
[tool.black]
line-length = 120
......
......@@ -12,7 +12,11 @@
# otherwise use the software for commercial activities involving the Arduino
# software without disclosing the source code of your own applications. To purchase
# a commercial license, send an email to license@arduino.cc.
import platform
import simplejson as json
import pytest
from git import Repo
from pathlib import Path
......@@ -383,3 +387,99 @@ def test_lib_list_with_updatable_flag(run_command):
assert "6.11.0" == data[0]["library"]["version"]
assert "6.11.0" != data[0]["release"]["version"]
assert "" != data[0]["release"]["version"]
def test_install_with_git_url_from_current_directory(run_command, downloads_dir, data_dir):
assert run_command("update")
env = {
"ARDUINO_DATA_DIR": data_dir,
"ARDUINO_DOWNLOADS_DIR": downloads_dir,
"ARDUINO_SKETCHBOOK_DIR": data_dir,
"ARDUINO_ENABLE_UNSAFE_LIBRARY_INSTALL": "true",
}
lib_install_dir = Path(data_dir, "libraries", "WiFi101")
# Verifies library is not installed
assert not lib_install_dir.exists()
# Clone repository locally
git_url = "https://github.com/arduino-libraries/WiFi101.git"
repo_dir = Path(data_dir, "WiFi101")
assert Repo.clone_from(git_url, repo_dir)
assert run_command("lib install --git-url .", custom_working_dir=repo_dir, custom_env=env)
# Verifies library is installed to correct folder
assert lib_install_dir.exists()
@pytest.mark.skipif(
platform.system() == "Windows",
reason="Using a file uri as git url doesn't work on Windows, "
+ "this must be removed when this issue is fixed: https://github.com/go-git/go-git/issues/247",
)
def test_install_with_git_url_local_file_uri(run_command, downloads_dir, data_dir):
assert run_command("update")
env = {
"ARDUINO_DATA_DIR": data_dir,
"ARDUINO_DOWNLOADS_DIR": downloads_dir,
"ARDUINO_SKETCHBOOK_DIR": data_dir,
"ARDUINO_ENABLE_UNSAFE_LIBRARY_INSTALL": "true",
}
lib_install_dir = Path(data_dir, "libraries", "WiFi101")
# Verifies library is not installed
assert not lib_install_dir.exists()
# Clone repository locally
git_url = "https://github.com/arduino-libraries/WiFi101.git"
repo_dir = Path(data_dir, "WiFi101")
assert Repo.clone_from(git_url, repo_dir)
assert run_command(f"lib install --git-url {repo_dir.as_uri()}", custom_env=env)
def test_install_with_git_local_url(run_command, downloads_dir, data_dir):
assert run_command("update")
env = {
"ARDUINO_DATA_DIR": data_dir,
"ARDUINO_DOWNLOADS_DIR": downloads_dir,
"ARDUINO_SKETCHBOOK_DIR": data_dir,
"ARDUINO_ENABLE_UNSAFE_LIBRARY_INSTALL": "true",
}
lib_install_dir = Path(data_dir, "libraries", "WiFi101")
# Verifies library is not installed
assert not lib_install_dir.exists()
# Clone repository locally
git_url = "https://github.com/arduino-libraries/WiFi101.git"
repo_dir = Path(data_dir, "WiFi101")
assert Repo.clone_from(git_url, repo_dir)
assert run_command(f"lib install --git-url {repo_dir}", custom_env=env)
def test_install_with_git_url_relative_path(run_command, downloads_dir, data_dir):
assert run_command("update")
env = {
"ARDUINO_DATA_DIR": data_dir,
"ARDUINO_DOWNLOADS_DIR": downloads_dir,
"ARDUINO_SKETCHBOOK_DIR": data_dir,
"ARDUINO_ENABLE_UNSAFE_LIBRARY_INSTALL": "true",
}
lib_install_dir = Path(data_dir, "libraries", "WiFi101")
# Verifies library is not installed
assert not lib_install_dir.exists()
# Clone repository locally
git_url = "https://github.com/arduino-libraries/WiFi101.git"
repo_dir = Path(data_dir, "WiFi101")
assert Repo.clone_from(git_url, repo_dir)
assert run_command("lib install --git-url ./WiFi101", custom_working_dir=data_dir, custom_env=env)
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment