Commit 97a4d8aea46fb45894f6e47597320ed2f6a12495

Authored by Dmitriy Zaporozhets
1 parent bacfad1c

Improve code according to new gitlab_git

Signed-off-by: Dmitriy Zaporozhets <dmitriy.zaporozhets@gmail.com>
app/helpers/gitlab_markdown_helper.rb
... ... @@ -166,13 +166,13 @@ module GitlabMarkdownHelper
166 166  
167 167 def file_exists?(path)
168 168 return false if path.nil? || path.empty?
169   - return @repository.blob_at(current_ref, path).present? || Tree.new(@repository, current_ref, path).entries.any?
  169 + return @repository.blob_at(current_ref, path).present? || @repository.tree(:head, path).entries.any?
170 170 end
171 171  
172 172 # Check if the path is pointing to a directory(tree) or a file(blob)
173 173 # eg. doc/api is directory and doc/README.md is file
174 174 def local_path(path)
175   - return "tree" if Tree.new(@repository, current_ref, path).entries.any?
  175 + return "tree" if @repository.tree(:head, path).entries.any?
176 176 return "raw" if @repository.blob_at(current_ref, path).image?
177 177 return "blob"
178 178 end
... ...
app/models/repository.rb
... ... @@ -163,7 +163,19 @@ class Repository
163 163  
164 164 def readme
165 165 Rails.cache.fetch(cache_key(:readme)) do
166   - Tree.new(self, self.root_ref).readme
  166 + tree(:head).readme
167 167 end
168 168 end
  169 +
  170 + def head_commit
  171 + commit(self.root_ref)
  172 + end
  173 +
  174 + def tree(sha = :head, path = nil)
  175 + if sha == :head
  176 + sha = head_commit.sha
  177 + end
  178 +
  179 + Tree.new(self, sha, path)
  180 + end
169 181 end
... ...
app/models/tree.rb
... ... @@ -23,4 +23,8 @@ class Tree
23 23 def submodules
24 24 @entries.select(&:submodule?)
25 25 end
  26 +
  27 + def sorted_entries
  28 + trees + blobs + submodules
  29 + end
26 30 end
... ...
lib/api/entities.rb
... ... @@ -79,7 +79,16 @@ module API
79 79 end
80 80  
81 81 class RepoObject < Grape::Entity
82   - expose :name, :commit
  82 + expose :name
  83 +
  84 + expose :commit do |repo_obj, options|
  85 + if repo_obj.respond_to?(:commit)
  86 + repo_obj.commit
  87 + elsif options[:project]
  88 + options[:project].repository.commit(repo_obj.target)
  89 + end
  90 + end
  91 +
83 92 expose :protected do |repo, options|
84 93 if options[:project]
85 94 options[:project].protected_branch? repo.name
... ... @@ -87,6 +96,16 @@ module API
87 96 end
88 97 end
89 98  
  99 + class RepoTreeObject < Grape::Entity
  100 + expose :id, :name, :type
  101 +
  102 + expose :mode do |obj, options|
  103 + filemode = obj.mode.to_s(8)
  104 + filemode = "0" + filemode if filemode.length < 6
  105 + filemode
  106 + end
  107 + end
  108 +
90 109 class RepoCommit < Grape::Entity
91 110 expose :id, :short_id, :title, :author_name, :author_email, :created_at
92 111 end
... ...
lib/api/repositories.rb
... ... @@ -82,7 +82,7 @@ module API
82 82 # Example Request:
83 83 # GET /projects/:id/repository/tags
84 84 get ":id/repository/tags" do
85   - present user_project.repo.tags.sort_by(&:name).reverse, with: Entities::RepoObject
  85 + present user_project.repo.tags.sort_by(&:name).reverse, with: Entities::RepoObject, project: user_project
86 86 end
87 87  
88 88 # Get a project repository commits
... ... @@ -141,15 +141,9 @@ module API
141 141 path = params[:path] || nil
142 142  
143 143 commit = user_project.repository.commit(ref)
144   - tree = Tree.new(user_project.repository, commit.id, path)
  144 + tree = user_project.repository.tree(commit.id, path)
145 145  
146   - trees = []
147   -
148   - %w(trees blobs submodules).each do |type|
149   - trees += tree.send(type).map { |t| {name: t.name, type: type.singularize, mode: t.mode, id: t.id} }
150   - end
151   -
152   - trees
  146 + present tree.sorted_entries, with: Entities::RepoTreeObject
153 147 end
154 148  
155 149 # Get a raw file contents
... ... @@ -233,4 +227,3 @@ module API
233 227 end
234 228 end
235 229 end
236   -
... ...
lib/extracts_path.rb
... ... @@ -117,7 +117,7 @@ module ExtractsPath
117 117 end
118 118  
119 119 def tree
120   - @tree ||= Tree.new(@repo, @commit.id, @path)
  120 + @tree ||= @repo.tree(@commit.id, @path)
121 121 end
122 122  
123 123 private
... ...