Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Repo options #315

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@ Create a [Repo](https://libvcs.git-pull.com/api.html#creating-a-repo-object) obj
to inspect / checkout / update:

```python
>>> from libvcs.shortcuts import create_repo_from_pip_url, create_repo
>>> from libvcs.shortcuts import create_repo_from_pip_url, create_repo_legacy

# repo is an object representation of a vcs repository.
>>> r = create_repo(url='https://www.github.com/vcs-python/libtmux',
>>> r = create_repo_legacy(url='https://www.github.com/vcs-python/libtmux',
... vcs='git',
... repo_dir='/tmp/libtmux')

Expand Down
3 changes: 1 addition & 2 deletions libvcs/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ class BaseRepo(RepoLoggingAdapter):
#: vcs app name, e.g. 'git'
bin_name = ""

def __init__(self, url, repo_dir, progress_callback=None, *args, **kwargs):
def __init__(self, repo_dir, progress_callback=None, *args, **kwargs):
"""
Parameters
----------
Expand All @@ -58,7 +58,6 @@ def __init__(self, url, repo_dir, progress_callback=None, *args, **kwargs):
>>> create_repo(..., progress_callback=progress_cb)
"""
self.progress_callback = progress_callback
self.url = url
self.parent_dir = os.path.dirname(repo_dir)
self.repo_name = os.path.basename(os.path.normpath(repo_dir))
self.path = repo_dir
Expand Down
31 changes: 15 additions & 16 deletions libvcs/git.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,17 +134,20 @@ class RemoteDict(TypedDict):
RemotesArgs = Union[None, FullRemoteDict, Dict[str, str]]


class GitOptions(TypedDict):
remotes: RemotesArgs


class GitRepo(BaseRepo):
bin_name = "git"
schemes = ("git", "git+http", "git+https", "git+ssh", "git+git", "git+file")

def __init__(self, url: str, repo_dir: str, remotes: RemotesArgs = None, **kwargs):
def __init__(self, repo_dir: str, options: GitOptions, *args, **kwargs):
"""A git repository.

Parameters
----------
url : str
URL of repo
options : FullRemoteDict

tls_verify : bool
Should certificate for https be checked (default False)
Expand Down Expand Up @@ -190,20 +193,16 @@ def __init__(self, url: str, repo_dir: str, remotes: RemotesArgs = None, **kwarg
if "tls_verify" not in kwargs:
self.tls_verify = False

self._remotes: Union[FullRemoteDict, None]

if remotes is None:
self._remotes: FullRemoteDict = {"origin": url}
elif isinstance(remotes, dict):
self._remotes: FullRemoteDict = remotes
for remote_name, url in remotes.items():
if isinstance(str, dict):
remotes[remote_name] = {
"fetch": url,
"push": url,
}
self._remotes: FullRemoteDict = options["remotes"]
for remote_name, url in self._remotes.items():
if isinstance(str, dict):
self._remotes[remote_name] = {
"fetch": url,
"push": url,
}

BaseRepo.__init__(self, url, repo_dir, **kwargs)
BaseRepo.__init__(self, repo_dir, options, *args, **kwargs)
self.url = self._remotes["origin"]["fetch"]

@classmethod
def from_pip_url(cls, pip_url, *args, **kwargs):
Expand Down
1 change: 1 addition & 0 deletions libvcs/hg.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ class MercurialRepo(BaseRepo):

def __init__(self, url, repo_dir, **kwargs):
BaseRepo.__init__(self, url, repo_dir, **kwargs)
self.url = url

def obtain(self, *args, **kwargs):
self.ensure_dir()
Expand Down
73 changes: 63 additions & 10 deletions libvcs/shortcuts.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,32 @@


def create_repo(
url, vcs, progress_callback=None, *args, **kwargs
vcs: DEFAULT_VCS_LITERAL,
repo_dir: str,
options: Dict,
progress_callback=None,
*args,
**kwargs
) -> Union[GitRepo, MercurialRepo, SubversionRepo]:
r"""Return a object representation of a VCS repository.

Parameters
----------
progress_callback : func
See :class:`libvcs.base.BaseRepo`

Examples
--------
>>> from libvcs.shortcuts import create_repo
>>>
>>> r = create_repo(
... url='https://www.github.com/you/myrepo',
... vcs='git',
... repo_dir='/tmp/myrepo'
... repo_dir='/tmp/myrepo',
... options={ # VCS-specific params
... 'remotes': {
... 'origin': 'https://www.github.com/you/myrepo'
... }
... }
... )

>>> r.update_repo()
Expand All @@ -43,13 +57,15 @@ def create_repo(
|myrepo| (git) git pull
Already up-to-date.
"""
if vcs == "git":
return GitRepo(url, progress_callback=progress_callback, *args, **kwargs)
elif vcs == "hg":
return MercurialRepo(url, progress_callback=progress_callback, *args, **kwargs)
elif vcs == "svn":
return SubversionRepo(url, progress_callback=progress_callback, *args, **kwargs)
else:
try:
return DEFAULT_VCS_CLASS_MAP[vcs](
repo_dir=repo_dir,
options=options,
progress_callback=progress_callback,
*args,
**kwargs
)
except KeyError:
raise InvalidVCS("VCS %s is not a valid VCS" % vcs)


Expand Down Expand Up @@ -87,3 +103,40 @@ def create_repo_from_pip_url(
return SubversionRepo.from_pip_url(pip_url, **kwargs)
else:
raise InvalidPipURL(pip_url)


def create_repo_legacy(
url, vcs, progress_callback=None, *args, **kwargs
) -> Union[GitRepo, MercurialRepo, SubversionRepo]:
r"""Return a object representation of a VCS repository.

Examples
--------
>>> from libvcs.shortcuts import create_repo_legacy
>>>
>>> r = create_repo_legacy(
... url='https://www.github.com/you/myrepo',
... vcs='git',
... repo_dir='/tmp/myrepo'
... )

>>> r.update_repo()
|myrepo| (git) Repo directory for myrepo (git) does not exist @ \
/tmp/myrepo
|myrepo| (git) Cloning.
|myrepo| (git) git clone https://www.github.com/tony/myrepo \
/tmp/myrepo
Cloning into '/tmp/myrepo'...
Checking connectivity... done.
|myrepo| (git) git fetch
|myrepo| (git) git pull
Already up-to-date.
"""
if vcs == "git":
return GitRepo(url, progress_callback=progress_callback, *args, **kwargs)
elif vcs == "hg":
return MercurialRepo(url, progress_callback=progress_callback, *args, **kwargs)
elif vcs == "svn":
return SubversionRepo(url, progress_callback=progress_callback, *args, **kwargs)
else:
raise InvalidVCS("VCS %s is not a valid VCS" % vcs)
1 change: 1 addition & 0 deletions libvcs/svn.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ def __init__(self, url, repo_dir, **kwargs):
self.svn_trust_cert = False

self.rev = kwargs.get("rev")
self.url = url
BaseRepo.__init__(self, url, repo_dir, **kwargs)

def _user_pw_args(self):
Expand Down
6 changes: 4 additions & 2 deletions tests/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@
import pathlib

from libvcs.base import BaseRepo, convert_pip_url
from libvcs.shortcuts import create_repo
from libvcs.shortcuts import create_repo_legacy


def test_repr():
repo = create_repo(url="file://path/to/myrepo", repo_dir="/hello/", vcs="git")
repo = create_repo_legacy(
url="file://path/to/myrepo", repo_dir="/hello/", vcs="git"
)

str_repo = str(repo)
assert "GitRepo" in str_repo
Expand Down
4 changes: 2 additions & 2 deletions tests/test_hg.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

import pytest

from libvcs.shortcuts import create_repo, create_repo_from_pip_url
from libvcs.shortcuts import create_repo_from_pip_url, create_repo_legacy
from libvcs.util import run, which

if not which("hg"):
Expand Down Expand Up @@ -88,7 +88,7 @@ def test_vulnerability_2022_03_12_command_injection(
random_dir = tmp_path / "random"
random_dir.mkdir()
monkeypatch.chdir(str(random_dir))
mercurial_repo = create_repo(
mercurial_repo = create_repo_legacy(
url="--config=alias.clone=!touch ./HELLO", vcs="hg", repo_dir="./"
)
with pytest.raises(Exception):
Expand Down
10 changes: 6 additions & 4 deletions tests/test_shortcuts.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

from libvcs import GitRepo, MercurialRepo, SubversionRepo
from libvcs.exc import InvalidPipURL, InvalidVCS
from libvcs.shortcuts import create_repo, create_repo_from_pip_url
from libvcs.shortcuts import create_repo_from_pip_url, create_repo_legacy


@pytest.mark.parametrize(
Expand Down Expand Up @@ -67,13 +67,15 @@ def test_create_repo_from_pip_url(
),
],
)
def test_create_repo(tmp_path: pathlib.Path, repo_dict, repo_class, raises_exception):
def test_create_repo_legacy(
tmp_path: pathlib.Path, repo_dict, repo_class, raises_exception
):
# add parent_dir via fixture
repo_dict["repo_dir"] = tmp_path / "repo_name"

if raises_exception:
with pytest.raises(raises_exception):
create_repo(**repo_dict)
create_repo_legacy(**repo_dict)
else:
repo = create_repo(**repo_dict)
repo = create_repo_legacy(**repo_dict)
assert isinstance(repo, repo_class)