libgit2 / pygit2

Python bindings for libgit2
https://www.pygit2.org/
Other
1.58k stars 382 forks source link

How to save index with "cache tree" extension? #1207

Open sergey-komissarov opened 1 year ago

sergey-komissarov commented 1 year ago

Hello,

I have a project with approx 2000 files stored on NFS partition. When I initialize repository with pygit2 all subsequent commits took about 5sec. But after I commit anything (even empty tree) via git subsequent commits via pygit2 took only 0.5sec. This is because commit via git writes cache tree (https://git-scm.com/docs/index-format#_cache_tree) extension to .git/index file.

Finally I found that reading tree back to the index builds internal "cache tree". And now I wondering if this is a correct way to buid cache tree on initial commit?

repo = pygit2.init_repository(path, bare=False, flags=pygit2.GIT_REPOSITORY_INIT_NO_REINIT)
index = repo.index
index.add_all()
tree_id = index.write_tree()
# NOTE: Adds "cache tree" extension to the index.
index.read_tree(tree_id)
index.write()
repo.create_commit("HEAD", SIGNATURE, SIGNATURE, "Inital commit", tree_id, [])