Skip to main content

Lite, typed, python utilities for Git, SVN, Mercurial, etc.

Project description

libvcs · Python Package License Code Coverage

libvcs is a lite, typed, pythonic tool box for detection and parsing of URLs, commanding, and syncing with git, hg, and svn. Powers vcspull.

Overview

Key Features

  • URL Detection and Parsing: Validate and parse Git, Mercurial, and Subversion URLs.
  • Command Abstraction: Interact with VCS systems through a Python API.
  • Repository Synchronization: Clone and update repositories locally via Python API.
  • py.test fixtures: Create temporary local repositories and working copies for testing for unit tests.

Supports Python 3.9 and above, Git (including AWS CodeCommit), Subversion, and Mercurial.

To get started, see the quickstart guide for more information.

$ pip install --user libvcs

URL Detection and Parsing

Easily validate and parse VCS URLs using the libvcs.url module:

Validate URLs

>>> from libvcs.url.git import GitURL

>>> GitURL.is_valid(url='https://github.com/vcs-python/libvcs.git')
True

Parse and adjust Git URLs:

>>> from libvcs.url.git import GitURL

>>> git_location = GitURL(url='git@github.com:vcs-python/libvcs.git')

>>> git_location
GitURL(url=git@github.com:vcs-python/libvcs.git,
        user=git,
        hostname=github.com,
        path=vcs-python/libvcs,
        suffix=.git,
        rule=core-git-scp)

Switch repo libvcs -> vcspull:

>>> from libvcs.url.git import GitURL

>>> git_location = GitURL(url='git@github.com:vcs-python/libvcs.git')

>>> git_location.path = 'vcs-python/vcspull'

>>> git_location.to_url()
'git@github.com:vcs-python/vcspull.git'

# Switch them to gitlab:
>>> git_location.hostname = 'gitlab.com'

# Export to a `git clone` compatible URL.
>>> git_location.to_url()
'git@gitlab.com:vcs-python/vcspull.git'

See more in the parser document.

Command Abstraction

Abstracts CLI commands for git(1), hg(1), svn(1) via a lightweight subprocess wrapper.

Run Git Commands

import pathlib
from libvcs.cmd.git import Git

git = Git(path=pathlib.Path.cwd() / 'my_git_repo')
git.clone(url='https://github.com/vcs-python/libvcs.git')

Above: libvcs.cmd.git.Git using Git.clone().

Repository Synchronization

Synchronize your repositories using the libvcs.sync module.

Clone and Update Repositories

import pathlib
from libvcs.sync.git import GitSync

repo = GitSync(
   url="https://github.com/vcs-python/libvcs",
   path=pathlib.Path().cwd() / "my_repo",
   remotes={
       'gitlab': 'https://gitlab.com/vcs-python/libvcs'
   }
)

# Update / clone repo:
>>> repo.update_repo()

# Get revision:
>>> repo.get_revision()
u'5c227e6ab4aab44bf097da2e088b0ff947370ab8'

Above: libvcs.sync.git.GitSync repository object using GitSync.update_repo() and GitSync.get_revision().

Pytest plugin: Temporary VCS repositories for testing

libvcs pytest plugin provides py.test fixtures to swiftly create local VCS repositories and working repositories to test with. Repositories are automatically cleaned on test teardown.

Use temporary, local VCS in py.test

import pathlib

from libvcs.pytest_plugin import CreateRepoPytestFixtureFn
from libvcs.sync.git import GitSync


def test_repo_git_remote_checkout(
    create_git_remote_repo: CreateRepoPytestFixtureFn,
    tmp_path: pathlib.Path,
    projects_path: pathlib.Path,
) -> None:
    git_server = create_git_remote_repo()
    git_repo_checkout_dir = projects_path / "my_git_checkout"
    git_repo = GitSync(path=str(git_repo_checkout_dir), url=f"file://{git_server!s}")

    git_repo.obtain()
    git_repo.update_repo()

    assert git_repo.get_revision() == "initial"

    assert git_repo_checkout_dir.exists()
    assert pathlib.Path(git_repo_checkout_dir / ".git").exists()

Under the hood: fixtures bootstrap a temporary $HOME environment in a TmpPathFactory for automatic cleanup and pytest-xdist compatibility..

Donations

Your donations fund development of new features, testing and support. Your money will go directly to maintenance and development of the project. If you are an individual, feel free to give whatever feels right for the value you get out of the project.

See donation options at https://www.git-pull.com/support.html.

More information

Docs Build Status

Project details


Release history Release notifications | RSS feed

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

libvcs-0.32.0.tar.gz (454.2 kB view details)

Uploaded Source

Built Distribution

libvcs-0.32.0-py3-none-any.whl (78.1 kB view details)

Uploaded Python 3

File details

Details for the file libvcs-0.32.0.tar.gz.

File metadata

  • Download URL: libvcs-0.32.0.tar.gz
  • Upload date:
  • Size: 454.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.12.7

File hashes

Hashes for libvcs-0.32.0.tar.gz
Algorithm Hash digest
SHA256 b0e9172743e775c2c470ab4621cbb67e9137b5dd3e90320e04fc82d0a760e3ac
MD5 0555c38168a77ef9ed50b0c51db1d8b9
BLAKE2b-256 ffcfd654b6275e230e9bf67f4e1bb1f763236883afa75aec36301f5784304ff1

See more details on using hashes here.

Provenance

File details

Details for the file libvcs-0.32.0-py3-none-any.whl.

File metadata

  • Download URL: libvcs-0.32.0-py3-none-any.whl
  • Upload date:
  • Size: 78.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.12.7

File hashes

Hashes for libvcs-0.32.0-py3-none-any.whl
Algorithm Hash digest
SHA256 13e568fa015d26e3a6302b56abf750787ab26f898e1fa2afea645373fda3b40f
MD5 be3ddfb96f77c861544191ccc083a6c8
BLAKE2b-256 00f3f8a63a1300ebe0b98b5fb482f3d3c74dd863f81da79a6e3f672cec301b7e

See more details on using hashes here.

Provenance

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page