The Index file and the Working copy

Repository.index

Index file.

Index read:

>>> index = repo.index
>>> index.read()
>>> oid = index['path/to/file'].oid    # from path to object id
>>> blob = repo[oid]                   # from object id to object

Iterate over all entries of the index:

>>> for entry in index:
...     print entry.path, entry.hex

Index write:

>>> index.add('path/to/file')          # git add
>>> del index['path/to/file']          # git rm
>>> index.write()                      # don't forget to save the changes

The Index type

Index.add(path)

Add or update an index entry from a file in disk.

Index.remove(path)

Removes an entry from index.

Index.clear()

Clear the contents (all the entries) of an index object.

Index.read(force=True)

Update the contents of an existing index object in memory by reading from the hard disk.Arguments:

force: if True (the default) allways reload. If False, only if the file has changed

Index.write()

Write an existing index object from memory back to disk using an atomic file lock.

Index.read_tree(tree)

Update the index file from the tree identified by the given oid.

Index.write_tree() → Oid

Create a tree object from the index file, return its oid.

Index.diff_to_tree(tree[, flag, context_lines, interhunk_lines]) → Diff

Return a Diff object with the differences between the index and the given tree.

Arguments:

tree: the tree to diff.

flag: a GIT_DIFF_* constant.

context_lines: the number of unchanged lines that define the boundary
of a hunk (and to display before and after)
interhunk_lines: the maximum number of unchanged lines between hunk
boundaries before the hunks will be merged into a one.
Index.diff_to_workdir([flag, context_lines, interhunk_lines]) → Diff

Return a Diff object with the differences between the index and the working copy.

Arguments:

flag: a GIT_DIFF_* constant.

context_lines: the number of unchanged lines that define the boundary
of a hunk (and to display before and after)
interhunk_lines: the maximum number of unchanged lines between hunk
boundaries before the hunks will be merged into a one.

The IndexEntry type

IndexEntry.oid

Object id.

IndexEntry.hex

Hex id.

IndexEntry.path

Path.

IndexEntry.mode

Mode.

Status

Repository.status() → {str: int}

Reads the status of the repository and returns a dictionary with file paths as keys and status flags as values. See pygit2.GIT_STATUS_*.

Repository.status_file(path) → int

Returns the status of the given file path.

Inspect the status of the repository:

>>> from pygit2 import GIT_STATUS_CURRENT
>>> status = repo.status()
>>> for filepath, flags in status.items():
...     if flags != GIT_STATUS_CURRENT:
...         print "Filepath %s isn't clean" % filepath

Checkout

Repository.checkout(refname=None, strategy=2)

Checkout the given reference using the given strategy, and update the HEAD. The reference may be a reference name or a Reference object. The default strategy is GIT_CHECKOUT_SAFE_CREATE.

To checkout from the HEAD, just pass ‘HEAD’:

>>> checkout('HEAD')

If no reference is given, checkout from the index.

Lower level API:

Repository.checkout_head(strategy)

Checkout the head using the given strategy.

Repository.checkout_tree(treeish, strategy)

Checkout the given tree, commit or tag, using the given strategy.

Repository.checkout_index(strategy)

Checkout the index using the given strategy.

Table Of Contents

Previous topic

Commit log

Next topic

Diff

This Page