Class: Gitlab::Git::Tree

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Tree

Returns a new instance of Tree.



71
72
73
74
75
# File 'lib/gitlab_git/tree.rb', line 71

def initialize(options)
  %w(id root_id name path type mode commit_id).each do |key|
    self.send("#{key}=", options[key.to_sym])
  end
end

Instance Attribute Details

#commit_idObject

Returns the value of attribute commit_id.



4
5
6
# File 'lib/gitlab_git/tree.rb', line 4

def commit_id
  @commit_id
end

#idObject

Returns the value of attribute id.



4
5
6
# File 'lib/gitlab_git/tree.rb', line 4

def id
  @id
end

#modeObject

Returns the value of attribute mode.



4
5
6
# File 'lib/gitlab_git/tree.rb', line 4

def mode
  @mode
end

#nameObject

Returns the value of attribute name.



4
5
6
# File 'lib/gitlab_git/tree.rb', line 4

def name
  @name
end

#pathObject

Returns the value of attribute path.



4
5
6
# File 'lib/gitlab_git/tree.rb', line 4

def path
  @path
end

#root_idObject

Returns the value of attribute root_id.



4
5
6
# File 'lib/gitlab_git/tree.rb', line 4

def root_id
  @root_id
end

#submodule_urlObject

Returns the value of attribute submodule_url.



4
5
6
# File 'lib/gitlab_git/tree.rb', line 4

def submodule_url
  @submodule_url
end

#typeObject

Returns the value of attribute type.



4
5
6
# File 'lib/gitlab_git/tree.rb', line 4

def type
  @type
end

Class Method Details

.find_id_by_path(repository, root_id, path) ⇒ Object

Recursive search of tree id for path

Ex.

blog/            # oid: 1a
  app/           # oid: 2a
    models/      # oid: 3a
    views/       # oid: 4a

Tree.find_id_by_path(repo, ‘1a’, ‘app/models’) # => ‘3a’



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/gitlab_git/tree.rb', line 52

def find_id_by_path(repository, root_id, path)
  root_tree = repository.lookup(root_id)
  path_arr = path.split('/')

  entry = root_tree.find do |entry|
    entry[:name] == path_arr[0] && entry[:type] == :tree
  end

  return nil unless entry

  if path_arr.size > 1
    path_arr.shift
    find_id_by_path(repository, entry[:oid], path_arr.join('/'))
  else
    entry[:oid]
  end
end

.where(repository, sha, path = nil) ⇒ Object

Get list of tree objects for repository based on commit sha and path Uses rugged for raw objects



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/gitlab_git/tree.rb', line 11

def where(repository, sha, path = nil)
  path = nil if path == '' || path == '/'

  commit = repository.lookup(sha)
  root_tree = commit.tree

  tree = if path
           id = Tree.find_id_by_path(repository, root_tree.oid, path)
           if id
             repository.lookup(id)
           else
             []
           end
         else
           root_tree
         end

  tree.map do |entry|
    Tree.new(
      id: entry[:oid],
      root_id: root_tree.oid,
      name: entry[:name],
      type: entry[:type],
      mode: entry[:filemode],
      path: path ? File.join(path, entry[:name]) : entry[:name],
      commit_id: sha,
    )
  end
end

Instance Method Details

#contributing?Boolean

Returns:

  • (Boolean)


93
94
95
# File 'lib/gitlab_git/tree.rb', line 93

def contributing?
  name =~ /^contributing/i
end

#dir?Boolean

Returns:

  • (Boolean)


77
78
79
# File 'lib/gitlab_git/tree.rb', line 77

def dir?
  type == :tree
end

#file?Boolean

Returns:

  • (Boolean)


81
82
83
# File 'lib/gitlab_git/tree.rb', line 81

def file?
  type == :blob
end

#readme?Boolean

Returns:

  • (Boolean)


89
90
91
# File 'lib/gitlab_git/tree.rb', line 89

def readme?
  name =~ /^readme/i
end

#submodule?Boolean

Returns:

  • (Boolean)


85
86
87
# File 'lib/gitlab_git/tree.rb', line 85

def submodule?
  type == :commit
end