Skip to content

Instantly share code, notes, and snippets.

@expobrain
Forked from danielmt/commit.py
Created January 9, 2019 18:25
Show Gist options
  • Save expobrain/e6c41f1c2f35c0f671e172ec7d3a45f7 to your computer and use it in GitHub Desktop.
Save expobrain/e6c41f1c2f35c0f671e172ec7d3a45f7 to your computer and use it in GitHub Desktop.
pygit 2 snippets - most examples assume using a bare repository.
import pygit2
# open existing repository
repo = pygit2.Repository(pygit2.discover_repository('test_repos'))
# check if repos is newly created
if repo.head_is_unborn:
tb = repo.TreeBuilder()
parent = []
else:
tb = repo.TreeBuilder(repo.head.peel().tree.id)
parent = [repo.head.target]
# add file from anywhere on disk
blob_id = repo.create_blob_fromdisk("/path/to/file")
# insert into tree, and get current tree
# CAVEAT: when adding to a nested path, will need to walk the whole tree inserting new trees
# to get back their oid, and from that oid continue all operations. all operations are "nested", so,
# you will need to grab the last tree from TreeBuilder.write() to make the next change.
# https://gist.github.com/uniphil/9570964 illustrates how to do it, from method auto_insert()
# deleting from a nested path would also require walking the entire tree, like in auto_insert(), but, replacing
# the treebuilder.insert() from the base case to a treebuilder.remove().
tb.insert('file', blob_id, pygit2.GIT_FILEMODE_BLOB)
tree = tb.write()
# add blob from any data
new_blob_id = repo.create_blob('foobar');
# needs to get another treebuilder based on last operation
tb = repo.TreeBuilder(tree)
tb.insert('other_file', new_blob_id, pygit2.GIT_FILEMODE_BLOB)
tree = tb.write()
# delete a file
tb = repo.TreeBuilder(tree)
tb.remove('old_file')
tree = tb.write()
# write to index
repo.index.write()
# author of commit
author = pygit2.Signature('Author', 'author@email.com')
# commit changes
oid = repo.create_commit('refs/heads/master', author, author, 'commit message', tree, parent)
import pygit2
# open existing repository
repo = pygit2.Repository(pygit2.discover_repository('test_repos'))
# read index
repo.index.read()
# create branch from master
branch = repo.create_branch('new_branch', repo.head.peel())
# or create from another branch
other_branch = repo.lookup_branch('other_branch')
branch = repo.create_branch('new_branch', other_branch.peel())
import pygit2
# open existing repository
repo = pygit2.Repository(pygit2.discover_repository('test_repos'))
# branch against tree
branch = repo.lookup_branch('some_branch')
# master would be: repo.head.peel().tree
tree = branch.peel().tree
# use an in-memory index and read current tree
index = pygit2.Index()
index.read_tree(tree)
# add data
blob_id = repo.create_blob('foobar')
entry = pygit2.IndexEntry('full/path/to/file', blob_id, pygit2.GIT_FILEMODE_BLOB)
index.add(entry)
# can now check for conflicts
index.conflicts
# generate diff from current tree to in-memory index
diff = tree.diff_to_index(index)
# diff patch
patch = diff.patch
import pygit2
# bare repository
repo = pygit2.init_repository('test_repos', True)
# normal repository
repo = pygit2.init_repository('test_repos')
# clone repository
repo = pygit2.clone_repository('path_or_remote_host', 'new_cloned_repo', bare=True)
import pygit2
# open existing repository
repo = pygit2.Repository(pygit2.discover_repository('test_repos'))
import pygit2
# open existing repository
repo = pygit2.Repository(pygit2.discover_repository('test_repos'))
# read index
repo.index.read()
for commit in repo.walk(repo.head.target, pygit2.GIT_SORT_TOPOLOGICAL):
parent_oid = None
if len(commit.parents) > 0:
parent_oid = commit.parents[0].oid
print("{} commit: {} - parents: {}".format(commit.oid, commit.message.strip(), parent_oid))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment