1

I'm using Grit in my rails app and I'm creating a commit which i know works:

        repo = Repo.new(full_path, {:is_bare => true})
        fname = "snippet"
        File.open("#{full_path}/#{fname}", 'w') {|f| f.puts(data)}
        Dir.chdir("#{full_path}") {repo.add(fname)}
        if repo.commit_index('his amazing commit')
            logger.info "commit succeeded"
        else
            logger.info "commit failed"
        end

then, im trying to get the blobs which is showing up empty:

            tree = Tree.construct(repo, 'master')
            data = tree.blobs.map {|b| repo.blob(b.id).data}
            logger.info "data.first = #{data.first}"
            data.first

What am I doing wrong here?

LuxuryMode
  • 33,401
  • 34
  • 117
  • 188

1 Answers1

0

I guess you have no file in the root level in your repository.

tree.blobs returns files of the root level, and tree.trees returns directories. To get all files in the repository, you need to traverse the tree recursively.

I wrote some example:

require 'grit'

def traverse(tree, basename)
  tree.blobs.each do |blob|
    puts "#{basename}/#{blob.basename}"
  end
  tree.trees.each do |subtree|
    traverse(subtree, "#{basename}/#{subtree.basename}")
  end
end

repo = Grit::Repo.new('.')
root = Grit::Tree.construct(repo, 'master')
traverse(root, '')
miaout17
  • 4,715
  • 2
  • 26
  • 32
  • 1
    Thanks! Still no luck. My directory looks like this: http://cl.ly/3S0Y0j0S40231b3J0z10 so I definitely have a file in the root level of my repo and still I can't get anything even by traversing recursively. – LuxuryMode Mar 07 '12 at 03:54