Skip to content

Instantly share code, notes, and snippets.

@ziprandom
Last active September 18, 2017 23: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 ziprandom/e6876924eea3a955f61d2d3c22a49ca3 to your computer and use it in GitHub Desktop.
Save ziprandom/e6876924eea3a955f61d2d3c22a49ca3 to your computer and use it in GitHub Desktop.
Listing files in a git repo using libgit2 bindings
# coding: utf-8
require "git";
# open repo
repo = Git::Repo.open "./"
# get tree
tree = repo.head.to_commit.to_tree
paths = [] of String
objects = [] of Git::Object
# diff tree against the empty tree
Git::C.diff_tree_to_tree out diff, repo.safe, tree.safe, nil, nil
diff = Git::Diff.new Git::Safe::Diff.free diff
# get the new Objects of the diff
diff.each do |delta|
Git::C.object_lookup_bypath(
out object, tree.safe.to_unsafe.as(Git::C::X_Object),
delta.new_file.path.to_unsafe, Git::C::Otype::ObjBlob
)
objects << Git::Object.new repo, Git::Safe::Object.free object
paths << delta.new_file.path
end
# retrive the contents
contents = objects.map do |blob|
s = Git::C.blob_rawsize(blob.safe.to_unsafe.as(Git::C::X_Blob))
c = Git::C.blob_rawcontent(blob.safe.to_unsafe.as(Git::C::X_Blob)).as(Pointer(UInt8))
String.new Slice.new(c, s)
end
pp contents
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment