class RJGit::Tree

Attributes

contents[R]
get_name[R]
id[R]
jtree[R]
mode[R]
name[R]
path[R]
repo[R]

Public Class Methods

find_tree(repository, file_path, revstring=Constants::HEAD) click to toggle source
# File lib/tree.rb, line 98
def self.find_tree(repository, file_path, revstring=Constants::HEAD)
  jrepo = RJGit.repository_type(repository)
  return nil if jrepo.nil?
  last_commit = jrepo.resolve(revstring)
  return nil if last_commit.nil?

  walk = RevWalk.new(jrepo)
  commit = walk.parse_commit(last_commit)
  treewalk = TreeWalk.new(jrepo)
  jtree = commit.get_tree
  treewalk.add_tree(jtree)
  treewalk.set_filter(PathFilter.create(file_path))
  if treewalk.next
    jsubtree = walk.lookup_tree(treewalk.get_object_id(0))
    if jsubtree
      mode = RJGit.get_file_mode_with_path(jrepo, file_path, jtree) 
      Tree.new(jrepo, mode, file_path, jsubtree)
    end
  else
    nil
  end
end
new(repository, mode, path, jtree) click to toggle source
# File lib/tree.rb, line 13
def initialize(repository, mode, path, jtree)
  @jrepo = RJGit.repository_type(repository)
  @mode = mode
  @path = path
  @name = @path ? File.basename(path) : nil
  @jtree = jtree
  @id = ObjectId.to_string(jtree.get_id)
end
new_from_hashmap(repository, hashmap, base_tree = nil) click to toggle source
# File lib/tree.rb, line 88
def self.new_from_hashmap(repository, hashmap, base_tree = nil)
  jrepo = RJGit.repository_type(repository)
  tree_builder = Plumbing::TreeBuilder.new(jrepo)
  base_tree = RJGit.tree_type(base_tree)
  new_tree = tree_builder.build_tree(base_tree, hashmap, true)
  walk = RevWalk.new(jrepo)
  new_tree = walk.lookup_tree(new_tree)
  Tree.new(jrepo, TREE_TYPE, nil, new_tree)
end

Public Instance Methods

/(file) click to toggle source
# File lib/tree.rb, line 74
def /(file)
  if file =~ /^\/+$/
    self
  else
    treewalk = TreeWalk.forPath(@jrepo, file, @jtree)
    if treewalk
      mode = treewalk.get_file_mode(0)
      wrap_tree_or_blob(obj_type(mode), mode.get_bits, treewalk.get_path_string, treewalk.get_object_id(0))
    else
      nil
    end
  end
end
blobs() click to toggle source
# File lib/tree.rb, line 66
def blobs
  @content_blobs ||= contents_array.select {|x| x.is_a?(Blob)}
end
data() click to toggle source
# File lib/tree.rb, line 22
def data
  return @contents if @contents
  strio = StringIO.new
  RJGit::Porcelain.ls_tree(@jrepo, @path, Constants::HEAD, options={:print => true, :io => strio})
  @contents = strio.string
end
each(&block) click to toggle source
# File lib/tree.rb, line 62
def each(&block)
  contents_array.each(&block)
end
find() { |tree_entry| ... } click to toggle source
# File lib/tree.rb, line 52
def find(&block)
  return nil unless block_given?
  _find(nil) {|tree_entry| yield tree_entry}
end
find_all() { |tree_entry| ... } click to toggle source
# File lib/tree.rb, line 57
def find_all(&block)
  return nil unless block_given?
  _find(nil, true) {|tree_entry| yield tree_entry}
end
find_blob() { |tree_entry| ... } click to toggle source
# File lib/tree.rb, line 42
def find_blob(&block)
  return nil unless block_given?
  _find(:Blob) {|tree_entry| yield tree_entry }
end
find_tree() { |tree_entry| ... } click to toggle source
# File lib/tree.rb, line 47
def find_tree(&block)
  return nil unless block_given?
  _find(:Tree) {|tree_entry| yield tree_entry }
end
recursive_contents_array(limit = nil) click to toggle source
# File lib/tree.rb, line 29
def recursive_contents_array(limit = nil)
  if @recursive_contents.nil? || @recursive_contents[:limit] != limit
    @recursive_contents = {}
    @recursive_contents[:objects] = jtree_entries({recursive: true, limit: limit})
    @recursive_contents[:limit] = limit
  end
  @recursive_contents[:objects]
end
recursive_count(limit = nil) click to toggle source
# File lib/tree.rb, line 38
def recursive_count(limit = nil)
  recursive_contents_array(limit).size
end
trees() click to toggle source
# File lib/tree.rb, line 70
def trees
  @content_trees ||= contents_array.select {|x| x.is_a?(Tree)}
end

Private Instance Methods

_find(type = nil, multiple = false) { |entry| ... } click to toggle source
# File lib/tree.rb, line 127
def _find(type = nil, multiple = false, &block)
  return nil unless block_given?
  treewalk = init_walk
  results = [] if multiple

  while treewalk.next
    entry = tree_entry(treewalk)
    next if type && type != entry[:type]
    if yield entry
      result = wrap_tree_or_blob(entry[:type], entry[:mode], entry[:name], entry[:id])
      if multiple
        results << result
      else
        return result
      end
    end
  end
  return results
end
contents_array() click to toggle source
# File lib/tree.rb, line 123
def contents_array
  @contents_ary ||= jtree_entries
end
init_walk(recurse=false) click to toggle source
# File lib/tree.rb, line 147
def init_walk(recurse=false)
  treewalk = TreeWalk.new(@jrepo)
  treewalk.add_tree(@jtree)
  treewalk.set_recursive(true) if recurse
  treewalk
end
jtree_entries(options = {}) click to toggle source
# File lib/tree.rb, line 164
def jtree_entries(options = {})
  treewalk = init_walk(options[:recursive])
  entries = []
  while treewalk.next
    mode = treewalk.get_file_mode(0)
    type = obj_type(mode)

    entries << wrap_tree_or_blob(type, mode.get_bits, treewalk.get_path_string, treewalk.get_object_id(0))

    break if options[:limit] && entries.size >= options[:limit].to_i
  end
  entries
end
obj_type(mode) click to toggle source
# File lib/tree.rb, line 183
def obj_type(mode)
  mode.get_object_type == Constants::OBJ_BLOB ? :Blob : :Tree
end
tree_entry(treewalk) click to toggle source
# File lib/tree.rb, line 154
def tree_entry(treewalk)
  mode = treewalk.get_file_mode(0)
  {
    type: obj_type(mode),
    mode: mode.get_bits,
    name: treewalk.get_path_string,
    id: treewalk.get_object_id(0)
  }
end
wrap_tree_or_blob(type, mode, path, id) click to toggle source
# File lib/tree.rb, line 178
def wrap_tree_or_blob(type, mode, path, id)
  walk = RevWalk.new(@jrepo)
  RJGit.const_get(type).new(@jrepo, mode, path, walk.parse_any(id)) 
end