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.



12
13
14
15
16
17
18
19
# File 'lib/tree.rb', line 12

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)

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

#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



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/tree.rb', line 70

def self.find_tree(repository, file_path, revstring=Constants::HEAD)
  jrepo = RJGit.repository_type(repository)
  return nil if jrepo.nil?
  last_commit_hash = jrepo.resolve(revstring)
  return nil if last_commit_hash.nil?

  walk = RevWalk.new(jrepo)
  commit = walk.parse_commit(last_commit_hash)
  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.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



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

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

From Grit



52
53
54
55
56
57
58
# File 'lib/tree.rb', line 52

def /(file)
  if file =~ /\//
    file.split("/").inject(self) { |acc, x| acc/x } rescue nil
  else
    self.contents_array.find { |c| c.name == file }
  end
end

#blobsObject



43
44
45
# File 'lib/tree.rb', line 43

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

#contents_arrayObject



28
29
30
31
32
33
34
35
36
37
# File 'lib/tree.rb', line 28

def contents_array
  return @contents_ary if @contents_ary
  results = []
  RJGit::Porcelain.ls_tree(@jrepo, @jtree).each do |item|
    walk = RevWalk.new(@jrepo)
    results << Tree.new(@jrepo, item[:mode], item[:path], walk.lookup_tree(ObjectId.from_string(item[:id]))) if item[:type] == 'tree'
    results << Blob.new(@jrepo, item[:mode], item[:path], walk.lookup_blob(ObjectId.from_string(item[:id]))) if item[:type] == 'blob'
  end
  @contents_ary = results
end

#dataObject



21
22
23
24
25
26
# File 'lib/tree.rb', line 21

def data
  return @contents if @contents
  strio = StringIO.new
  RJGit::Porcelain.ls_tree(@jrepo, @jtree, options={:print => true, :io => strio})
  @contents = strio.string
end

#each(&block) ⇒ Object



39
40
41
# File 'lib/tree.rb', line 39

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

#treesObject



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

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