Last active
March 14, 2022 16:49
-
-
Save danielmt/81ae37541270714c2605b8f3d574d993 to your computer and use it in GitHub Desktop.
pygit 2 snippets - most examples assume using a bare repository.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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', '[email protected]') | |
# commit changes | |
oid = repo.create_commit('refs/heads/master', author, author, 'commit message', tree, parent) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import pygit2 | |
# open existing repository | |
repo = pygit2.Repository(pygit2.discover_repository('test_repos')) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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