Class: RJGit::Tree

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/tree.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(repository, mode, path, jtree) ⇒ Tree

Returns a new instance of Tree.



13
14
15
16
17
18
19
20
# 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

Instance Attribute Details

#contentsObject (readonly)

Returns the value of attribute contents.



8
9
10
# File 'lib/tree.rb', line 8

def contents
  @contents
end

#idObject (readonly) Also known as: get_name

Returns the value of attribute id.



8
9
10
# File 'lib/tree.rb', line 8

def id
  @id
end

#jtreeObject (readonly)

Returns the value of attribute jtree.



8
9
10
# File 'lib/tree.rb', line 8

def jtree
  @jtree
end

#modeObject (readonly)

Returns the value of attribute mode.



8
9
10
# File 'lib/tree.rb', line 8

def mode
  @mode
end

#nameObject (readonly)

Returns the value of attribute name.



8
9
10
# File 'lib/tree.rb', line 8

def name
  @name
end

#pathObject (readonly)

Returns the value of attribute path.



8
9
10
# File 'lib/tree.rb', line 8

def path
  @path
end

#repoObject (readonly)

Returns the value of attribute repo.



8
9
10
# File 'lib/tree.rb', line 8

def repo
  @repo
end

Class Method Details

.find_tree(repository, file_path, revstring = Constants::HEAD) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/tree.rb', line 82

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_from_hashmap(repository, hashmap, base_tree = nil) ⇒ Object



72
73
74
75
76
77
78
79
80
# File 'lib/tree.rb', line 72

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

Instance Method Details

#/(file) ⇒ Object



62
63
64
65
66
67
68
69
70
# File 'lib/tree.rb', line 62

def /(file)
  begin
    treewalk = TreeWalk.forPath(@jrepo, file, @jtree)
  rescue Java::JavaLang::IllegalArgumentException
    return self
  end
  treewalk.nil? ? nil : 
    wrap_tree_or_blob(treewalk.get_file_mode(0), treewalk.get_path_string, treewalk.get_object_id(0))
end

#blobsObject



54
55
56
# File 'lib/tree.rb', line 54

def blobs
  @content_blobs ||= contents_array.select {|x| x.is_a?(Blob)}
end

#contents_arrayObject



29
30
31
# File 'lib/tree.rb', line 29

def contents_array
  @contents_ary ||= jtree_entries
end

#countObject



33
34
35
# File 'lib/tree.rb', line 33

def count
  contents_array.size
end

#dataObject



22
23
24
25
26
27
# 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) ⇒ Object



50
51
52
# File 'lib/tree.rb', line 50

def each(&block)
  contents_array.each(&block)
end

#recursive_contents_array(limit = nil) ⇒ Object



37
38
39
40
41
42
43
44
# File 'lib/tree.rb', line 37

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) ⇒ Object



46
47
48
# File 'lib/tree.rb', line 46

def recursive_count(limit = nil)
  recursive_contents_array(limit).size
end

#treesObject



58
59
60
# File 'lib/tree.rb', line 58

def trees
  @content_trees ||= contents_array.select {|x| x.is_a?(Tree)}
end