The repository

Everything starts either by creating a new repository, or by opening an existing one.

Functions

pygit2.init_repository(path, bare=False)

Creates a new Git repository in the given path.

If bare is True the repository will be bare, i.e. it will not have a working copy.

Example:

>>> from pygit2 import init_repository
>>> repo = init_repository('test')            # Creates a non-bare repository
>>> repo = init_repository('test', bare=True) # Creates a bare repository
pygit2.clone_repository(url, path, bare=False, ignore_cert_errors=False, remote_name='origin', checkout_branch=None)

Clones a new Git repository from url in the given path.

bare indicates whether a bare git repository should be created.

remote_name is the name given to the “origin” remote. The default is “origin”.

checkout_branch gives the name of the branch to checkout. None means use the remote’s HEAD.

Returns a Repository class pointing to the newly cloned repository.

If you wish to use the repo, you need to do a checkout for one of the available branches, like this:

>>> repo = repo.clone_repository("url", "path")
>>> repo.checkout(branch)  # i.e.: refs/heads/master

Example:

>>> from pygit2 import clone_repository
>>> repo_url = 'git://github.com/libgit2/pygit2.git'
>>> repo_path = '/path/to/create/repository'
>>> repo = clone_repository(repo_url, repo_path) # Clones a non-bare repository
>>> repo = clone_repository(repo_url, repo_path, bare=True) # Clones a bare repository
pygit2.discover_repository(path[, across_fs[, ceiling_dirs]]) → str

Look for a git repository and return its path.

The Repository class

class pygit2.Repository(path)

The Repository constructor only takes one argument, the path of the repository to open.

Example:

>>> from pygit2 import Repository
>>> repo = Repository('pygit2/.git')

The API of the Repository class is quite large. Since this documentation is orgaized by features, the related bits are explained in the related chapters, for instance the pygit2.Repository.checkout() method are explained in the Checkout section.

Below there are some general attributes and methods:

Repository.path

The normalized path to the git repository.

Repository.workdir

The normalized path to the working directory of the repository. If the repository is bare, None will be returned.

Repository.is_bare

Check if a repository is a bare repository.

Repository.is_empty

Check if a repository is empty.

Repository.read(oid) → type, data, size

Read raw object data from the repository.

Repository.write(type, data) → Oid

Write raw object data into the repository. First arg is the object type, the second one a buffer with data. Return the Oid of the created object.

Table Of Contents

Previous topic

General

Next topic

Object IDs

This Page