Guest User

Untitled

a guest
Sep 18th, 2017
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 1.07 KB | None | 0 0
  1. require "git";
  2.  
  3. # open repo
  4. repo = Git::Repo.open "./"
  5. # get tree
  6. tree = repo.head.to_commit.to_tree
  7.  
  8. entries = [] of Git::C::X_TreeEntry
  9.  
  10. cb = ->(x : Pointer(UInt8) , entry : Git::C::X_TreeEntry, entries: Pointer(Void)) do
  11.   entries.as(Pointer(Array(Git::C::X_TreeEntry))).value << entry
  12.   0
  13. end
  14.  
  15. # walk tree and collect entries
  16. Git::C.tree_walk(tree.safe, Git::C::TreewalkMode::TreewalkPre, cb, pointerof(entries))
  17.  
  18. objects = [] of {String, Git::Object}
  19.  
  20. # create Objects for entries in repo
  21. entries.map do |entry|
  22.  
  23.   Git::C.tree_entry_to_object out ref, repo.safe, entry
  24.   object = Git::Object.new repo, Git::Safe::Object.free(ref)
  25.   name = String.new(Git::C.tree_entry_name entry)
  26.   objects.push({ name, object })
  27. end
  28.  
  29. #
  30. # this fails with an invalid memory access
  31. #
  32. content_pointers = objects.map do |(name, object)|
  33.   s = object.safe.as(Git::C::X_Blob)
  34.   Git::C.blob_filtered_content out ref, s, "test.txt", 1
  35.   ref
  36. end
  37.  
  38. #
  39. strings = content_pointers.map { |ref| String.new( ref.ptr, ref.size )}
  40.  
  41. # this fails with invalid memory access
  42. pp strings[0]
Add Comment
Please, Sign In to add comment