Last active
September 18, 2017 23:09
-
-
Save ziprandom/e6876924eea3a955f61d2d3c22a49ca3 to your computer and use it in GitHub Desktop.
Listing files in a git repo using libgit2 bindings
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
# 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