Skip to content

Instantly share code, notes, and snippets.

@xhacker
Created November 7, 2014 21:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xhacker/dc02b27caf9d1d46a539 to your computer and use it in GitHub Desktop.
Save xhacker/dc02b27caf9d1d46a539 to your computer and use it in GitHub Desktop.
Clone only the history of a repo into a new repo
import pygit2
from pygit2 import GIT_SORT_TOPOLOGICAL, GIT_SORT_REVERSE
repo = pygit2.Repository("/path/to/the/repo")
hist_repo = pygit2.Repository("/path/to/the/repo/for/the/history")
parents = []
for commit in repo.walk(repo.head.target,
GIT_SORT_TOPOLOGICAL | GIT_SORT_REVERSE):
print "[Duplicating] " + commit.message,
tree = hist_repo.TreeBuilder().write()
oid = hist_repo.create_commit(
'refs/heads/master',
commit.author,
commit.committer,
commit.message,
tree,
parents)
parents = [oid]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment